qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Richard Henderson <richard.henderson@linaro.org>
To: Ilya Leoshkevich <iii@linux.ibm.com>
Cc: qemu-devel@nongnu.org
Subject: Re: [PATCH] tcg: Remove null pointer arithmetic in tcg_malloc()
Date: Wed, 18 Jun 2025 05:01:38 -0700	[thread overview]
Message-ID: <b82b85a1-1cb1-40a8-9d01-bb4b88c024c0@linaro.org> (raw)
In-Reply-To: <20250618103555.2020-1-iii@linux.ibm.com>

On 6/18/25 03:35, Ilya Leoshkevich wrote:
> Clang 20.1.6 (Fedora 20.1.6-1.fc42)'s UBSAN complains:
> 
>      qemu/include/tcg/tcg.h:715:19: runtime error: applying non-zero offset 64 to null pointer
> 
> The code uses NULL as pool's initial start and end, with the intention
> that `pool_cur + size > pool_end` should trigger the allocation.
> Unfortunately C prohibits adding non-zero to NULL, even if the result
> is not dereferenced.
> 
> Fix by using a dummy pool.

If we want to get uselessly technical, &dummy_pool + x, for x > 1 isn't legal either. 
This is just waiting for another ubsan update to break.

Does clang accept it if you rearrange the arithmetic to

   pool_end - pool_cur <= size

?

Otherwise, we may have to change the pool pointers to uintptr_t, so that we opt out of all 
ubsan silliness.


r~



> 
> Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
> ---
>   tcg/tcg.c | 3 ++-
>   1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/tcg/tcg.c b/tcg/tcg.c
> index d714ae2889c..afcc7ec8849 100644
> --- a/tcg/tcg.c
> +++ b/tcg/tcg.c
> @@ -1358,13 +1358,14 @@ void *tcg_malloc_internal(TCGContext *s, int size)
>   
>   void tcg_pool_reset(TCGContext *s)
>   {
> +    static uint8_t dummy_pool;
>       TCGPool *p, *t;
>       for (p = s->pool_first_large; p; p = t) {
>           t = p->next;
>           g_free(p);
>       }
>       s->pool_first_large = NULL;
> -    s->pool_cur = s->pool_end = NULL;
> +    s->pool_cur = s->pool_end = &dummy_pool;
>       s->pool_current = NULL;
>   }
>   



      parent reply	other threads:[~2025-06-18 12:02 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-18 10:35 [PATCH] tcg: Remove null pointer arithmetic in tcg_malloc() Ilya Leoshkevich
2025-06-18 11:47 ` Ilya Leoshkevich
2025-06-18 12:01 ` Richard Henderson [this message]

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=b82b85a1-1cb1-40a8-9d01-bb4b88c024c0@linaro.org \
    --to=richard.henderson@linaro.org \
    --cc=iii@linux.ibm.com \
    --cc=qemu-devel@nongnu.org \
    /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).