From: "René Scharfe" <l.s.r@web.de>
To: phillip.wood@dunelm.org.uk, Git List <git@vger.kernel.org>
Cc: Jameson Miller <jamill@microsoft.com>
Subject: Re: [PATCH] mem-pool: fix big allocations
Date: Thu, 28 Dec 2023 19:56:01 +0100 [thread overview]
Message-ID: <34f5913f-b187-43c3-99b7-3d57065dba12@web.de> (raw)
In-Reply-To: <e1e43a6c-3e06-4453-88a3-f00476132bcd@gmail.com>
Am 28.12.23 um 17:48 schrieb phillip.wood123@gmail.com:
> On 28/12/2023 16:05, René Scharfe wrote:
>> Am 28.12.23 um 16:10 schrieb Phillip Wood:
>>> The diff at the end of
>>> this email shows a possible implementation of a check_ptr() macro for
>>> the unit test library. I'm wary of adding it though because I'm not sure
>>> printing the pointer values is actually very useful most of the
>>> time. I'm also concerned that the rules around pointer arithmetic and
>>> comparisons mean that many pointer tests such as
>>>
>>> check_ptr(pool->mp_block->next_free, <=, pool->mp_block->end);
>>>
>>> will be undefined if they fail.
>>
>> True, the compiler could legally emit mush when it finds out that the
>> pointers are for different objects. And the error being fixed produces
>> such unrelated pointer pairs -- oops.
>>
>> This check is not important here, we can just drop it.
>>
>> mem_pool_contains() has the same problem, by the way.
>>
>> Restricting ourselves to only equality comparisons for pointers prevents
>> some interesting sanity checks, though. Casting to intptr_t or
>> uintptr_t would allow arbitrary comparisons without risk of undefined
>> behavior, though. Perhaps that would make a check_ptr() macro viable
>> and useful.
>
> That certainly helps and the check_ptr() macro in my previous email
> casts the pointers to uintptr_t before comparing them. Maybe I'm
> worrying too much, but my concern is that in a failing comparison it
> is likely one of the pointers is invalid (for example it is the
> result of some undefined pointer arithmetic) and the program is
> undefined from the point the invalid pointer is created.
There are no restrictions on integer comparisons. So comparing after
casting to uintptr_t should not invoke undefined behavior. If undefined
behavior was involved in calculating the pointers in the first place
then the compiler might still legally go crazy, but not due to the
comparison. Right?
Whether the result of a uintptr_t-cast comparison of pointers to
different objects is meaningful is a different question. Hopefully
range checks are possible.
> The
> documentation for check_ptr() in my previous mail contains the
> following example
>
> For example if `start` and `end` are pointers to the beginning and
> end of an allocation and `offset` is an integer then
>
> check_ptr(start + offset, <=, end)
>
> is undefined when `offset` is larger than `end - start`. Rewriting
> the comparison as
>
> check_uint(offset, <=, end - start)
>
> avoids undefined behavior when offset is too large, but is still
> undefined if there is a bug that means `start` and `end` do not
> point to the same allocation.
True, but in such a unit test we'd need additional checks verifying
that start and end belong to the same object. Or perhaps use a
numerical size instead of an end pointer.
René
next prev parent reply other threads:[~2023-12-28 18:56 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-12-21 23:13 [PATCH] mem-pool: fix big allocations René Scharfe
2023-12-24 3:11 ` Elijah Newren
2023-12-24 9:30 ` René Scharfe
2023-12-28 15:10 ` Phillip Wood
2023-12-28 16:05 ` René Scharfe
2023-12-28 16:48 ` phillip.wood123
2023-12-28 18:56 ` René Scharfe [this message]
2023-12-28 19:34 ` phillip.wood123
2023-12-29 6:53 ` René Scharfe
2023-12-28 19:19 ` [PATCH v2] " René Scharfe
2023-12-28 19:36 ` phillip.wood123
2024-01-02 22:29 ` Taylor Blau
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=34f5913f-b187-43c3-99b7-3d57065dba12@web.de \
--to=l.s.r@web.de \
--cc=git@vger.kernel.org \
--cc=jamill@microsoft.com \
--cc=phillip.wood@dunelm.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).