Skip to contentSkip to conversation

tcg: Remove null pointer arithmetic in tcg_malloc()

PATCH Superseded Ilya Leoshkevich 2 participants 3 messages Jun 18, 2025

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tcg: Remove null pointer arithmetic in tcg_malloc()

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.

---
 tcg/tcg.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tcg/tcg.c +2 -1
index d714ae2889c..afcc7ec8849 100644
@@ -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;
 }
 
Signature · 1 line
2.49.0
#1 lore raw
Quoted 30 lines · Ilya Leoshkevich

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.

---
 tcg/tcg.c | 3 ++-

 1 file changed, 2 insertions(+), 1 deletion(-)
tcg/tcg.c +0 -0
index d714ae2889c..afcc7ec8849 100644
@@ -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) {

Quoted 8 lines

         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;
 }

Hmm, come to think of it, something like &dummy_pool + 2 is not allowed either, it's just that UBSAN doesn't complain about it. I will send a v2.

#2 lore raw
In reply to Ilya Leoshkevich
Quoted 10 lines · Ilya Leoshkevich

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~

Quoted 26 lines
---
  tcg/tcg.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
tcg/tcg.c +2 -1
index d714ae2889c..afcc7ec8849 100644
@@ -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;
  }