Skip to contentSkip to conversation

tests/tcg/plugins: Fix sanitizer issues

PATCH In review Peter Maydell 2 participants 8 messages Mar 5

lore
Share & tools
lore
#0 lore raw

cover tests/tcg/plugins: Fix sanitizer issues

This patchset fixes some minor issues in the tests/tcg/plugins code which the sanitizers detected.

Patch 1 is the one I sent earlier today, and has been reviewed. Patches 2 and 3 are new.

thanks
-- PMM

Peter Maydell (3):
tests/tcg/plugins/mem: Don't access unaligned memory tests/tcg/plugins/mem: Correct hash iteration code in plugin_exit() tests/tcg/plugins/patch: Free read_data in patch_hwaddr()

 tests/tcg/plugins/mem.c   | 95 +++++++++++++++++----------------------
 tests/tcg/plugins/patch.c |  2 +-
 2 files changed, 42 insertions(+), 55 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/3 tests/tcg/plugins/mem: Don't access unaligned memory

In commit eb3f69cac62670 we removed the dependency of this mem plugin on the QEMU headers, but in doing that we introduced undefined behaviour when the plugin accesses unaligned memory. This shows up if you build with the gcc or clang undefined behaviour sanitizer (--enable-ubsan) and run 'make check-tcg', in numerous warnings like:

../../tests/tcg/plugins/mem.c:167:27: runtime error: load of misaligned address 0x7f1f300354b1 for type 'uint16_t' (aka 'unsigned short'), which requires 2 byte alignment 0x7f1f300354b1: note: pointer points here
00 00 00 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c

              ^

SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior ../../tests/tcg/plugins/mem.c:167:27

Fix this by rearranging the data reads and writes to use memcpy() instead.

Fixes eb3f69cac62670 ("tests/tcg/plugins/mem.c: remove dependency on qemu headers")
---
 tests/tcg/plugins/mem.c | 71 +++++++++++++++++------------------------
 1 file changed, 30 insertions(+), 41 deletions(-)
tests/tcg/plugins/mem.c +30 -41
index 7d64e7018f..f3992abc8f 100644
@@ -123,6 +123,9 @@ static void update_region_info(uint64_t region, uint64_t offset,
     bool is_store = qemu_plugin_mem_is_store(meminfo);
     RegionInfo *ri;
     bool unseen_data = false;
+    void *val_ptr;
+    unsigned int val_size;
+    qemu_plugin_mem_value swapped_value;
 
     g_assert(offset + size <= region_size);
 
@@ -144,61 +147,46 @@ static void update_region_info(uint64_t region, uint64_t offset,
     }
 
     void *ri_data = &ri->data[offset];
+
+    swapped_value.type = value.type;
     switch (value.type) {
     case QEMU_PLUGIN_MEM_VALUE_U8:
-    {
-        uint8_t val = value.data.u8;
-        uint8_t *p = ri_data;
-        if (is_store) {
-            *p = val;
-        } else {
-            unseen_data = *p != val;
-        }
+        swapped_value.data.u8 = value.data.u8;
+        val_ptr = &swapped_value.data.u8;
+        val_size = 1;
         break;
-    }
     case QEMU_PLUGIN_MEM_VALUE_U16:
-    {
-        uint16_t val = be ? GUINT16_FROM_BE(value.data.u16) :
-                            GUINT16_FROM_LE(value.data.u16);
-        uint16_t *p = ri_data;
-        if (is_store) {
-            *p = val;
-        } else {
-            unseen_data = *p != val;
-        }
+        swapped_value.data.u16 = be ? GUINT16_FROM_BE(value.data.u16) :
+            GUINT16_FROM_LE(value.data.u16);
+        val_ptr = &swapped_value.data.u16;
+        val_size = 2;
         break;
-    }
     case QEMU_PLUGIN_MEM_VALUE_U32:
-    {
-        uint32_t val = be ? GUINT32_FROM_BE(value.data.u32) :
-                            GUINT32_FROM_LE(value.data.u32);
-        uint32_t *p = ri_data;
-        if (is_store) {
-            *p = val;
-        } else {
-            unseen_data = *p != val;
-        }
+        swapped_value.data.u32 = be ? GUINT32_FROM_BE(value.data.u32) :
+            GUINT32_FROM_LE(value.data.u32);
+        val_ptr = &swapped_value.data.u32;
+        val_size = 4;
         break;
-    }
     case QEMU_PLUGIN_MEM_VALUE_U64:
-    {
-        uint64_t val = be ? GUINT64_FROM_BE(value.data.u64) :
-                            GUINT64_FROM_LE(value.data.u64);
-        uint64_t *p = ri_data;
-        if (is_store) {
-            *p = val;
-        } else {
-            unseen_data = *p != val;
-        }
+        swapped_value.data.u64 = be ? GUINT64_FROM_BE(value.data.u64) :
+            GUINT64_FROM_LE(value.data.u64);
+        val_ptr = &swapped_value.data.u64;
+        val_size = 8;
         break;
-    }
     case QEMU_PLUGIN_MEM_VALUE_U128:
-        /* non in test so skip */
-        break;
+        /* none in test so skip */
+        goto done;
     default:
         g_assert_not_reached();
     }
 
+    /* ri_data may not be aligned, so we use memcpy/memcmp */
+    if (is_store) {
+        memcpy(ri_data, val_ptr, val_size);
+    } else {
+        unseen_data = memcmp(ri_data, val_ptr, val_size) != 0;
+    }
+
     /*
      * This is expected for regions initialised by QEMU (.text etc) but we
      * expect to see all data read and written to the test_data region
@@ -213,6 +201,7 @@ static void update_region_info(uint64_t region, uint64_t offset,
         ri->seen_all = false;
     }
 
+done:
     g_mutex_unlock(&lock);
 }
 
Signature · 1 line
2.43.0
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] tests/tcg/plugins/mem: Don't access unaligned memory

Quoted 15 lines · Peter Maydell

In commit eb3f69cac62670 we removed the dependency of this mem plugin on the QEMU headers, but in doing that we introduced undefined behaviour when the plugin accesses unaligned memory. This shows up if you build with the gcc or clang undefined behaviour sanitizer (--enable-ubsan) and run 'make check-tcg', in numerous warnings like:

../../tests/tcg/plugins/mem.c:167:27: runtime error: load of misaligned address 0x7f1f300354b1 for type 'uint16_t' (aka 'unsigned short'), which requires 2 byte alignment 0x7f1f300354b1: note: pointer points here
00 00 00 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c

               ^

SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior ../../tests/tcg/plugins/mem.c:167:27

Fix this by rearranging the data reads and writes to use memcpy() instead.

Quoted 8 lines
Fixes eb3f69cac62670 ("tests/tcg/plugins/mem.c: remove dependency on qemu headers")
---
  tests/tcg/plugins/mem.c | 71 +++++++++++++++++------------------------
  1 file changed, 30 insertions(+), 41 deletions(-)
#3 lore raw

PATCH 2/3 tests/tcg/plugins/mem: Correct hash iteration code in plugin_exit()

In reply to Peter Maydell

In plugin_exit() we call g_hash_table_get_values() to get a GList which we look at to print some information. This code has multiple issues:
* it names the local variable for the GList "count", which shadows the "qemu_plugin_scoreboard *count". This isn't incorrect, but it is unnecessarily confusing
* it doesn't free the list, and the leak sanitizer complains:

Indirect leak of 2328 byte(s) in 97 object(s) allocated from:

    #0 0x5589b0b72293 in malloc (/home/pm215/qemu/build/x86-tgt-san/qemu-system-i386+0x1a2f293) (BuildId: 26964cad9e3f81d35fc144d7cc88b53adf6f60c7)
    #1 0x78fd8cfa1ac9 in g_malloc (/lib/x86_64-linux-gnu/libglib-2.0.so.0+0x62ac9) (BuildId: 116e142b9b52c8a4dfd403e759e71ab8f95d8bb3)
    #2 0x78fd8cf96e4a in g_list_prepend (/lib/x86_64-linux-gnu/libglib-2.0.so.0+0x57e4a) (BuildId: 116e142b9b52c8a4dfd403e759e71ab8f95d8bb3)
    #3 0x78fd8cf8b318 in g_hash_table_get_values (/lib/x86_64-linux-gnu/libglib-2.0.so.0+0x4c318) (BuildId: 116e142b9b52c8a4dfd403e759e71ab8f95d8bb3)
    #4 0x78fd84d1a90c in plugin_exit /home/pm215/qemu/build/x86-tgt-san/../../tests/tcg/plugins/mem.c:87:25

* in iterating through the list it updates "count", so by the time we get to the end of the loop we no longer have a pointer to the head of the list that we could use to free it
* it checks for the list being NULL twice (once in an if() and once in the for() loop's "while" condition), which is redundant
* it skips the loop if g_list_next(counts) is NULL, which means it will wrongly skip the loop if the list has only one entry

Rewrite the iteration code to fix these problems.

---
I am assuming that not printing the region info when there is exactly one region is a mistake, but perhaps it is intentional?

 tests/tcg/plugins/mem.c | 24 +++++++++++-------------
 1 file changed, 11 insertions(+), 13 deletions(-)
tests/tcg/plugins/mem.c +11 -13
index f3992abc8f..1ee257f855 100644
@@ -84,24 +84,22 @@ static void plugin_exit(qemu_plugin_id_t id, void *p)
 
 
     if (do_region_summary) {
-        GList *counts = g_hash_table_get_values(regions);
+        g_autoptr(GList) regionlist = g_hash_table_get_values(regions);
 
-        counts = g_list_sort_with_data(counts, addr_order, NULL);
+        regionlist = g_list_sort_with_data(regionlist, addr_order, NULL);
 
         g_string_printf(out, "Region Base, Reads, Writes, Seen all\n");
 
-        if (counts && g_list_next(counts)) {
-            for (/* counts */; counts; counts = counts->next) {
-                RegionInfo *ri = (RegionInfo *) counts->data;
+        for (GList *l = regionlist; l; l = g_list_next(l)) {
+            RegionInfo *ri = (RegionInfo *) l->data;
 
-                g_string_append_printf(out,
-                                       "0x%016"PRIx64", "
-                                       "%"PRId64", %"PRId64", %s\n",
-                                       ri->region_address,
-                                       ri->reads,
-                                       ri->writes,
-                                       ri->seen_all ? "true" : "false");
-            }
+            g_string_append_printf(out,
+                                   "0x%016"PRIx64", "
+                                   "%"PRId64", %"PRId64", %s\n",
+                                   ri->region_address,
+                                   ri->reads,
+                                   ri->writes,
+                                   ri->seen_all ? "true" : "false");
         }
         qemu_plugin_outs(out->str);
     }
Signature · 1 line
2.43.0
Reviewed-by on v1 2/3
#4 lore raw

Re: [PATCH 2/3] tests/tcg/plugins/mem: Correct hash iteration code in plugin_exit()

Quoted 25 lines · Peter Maydell

In plugin_exit() we call g_hash_table_get_values() to get a GList which we look at to print some information. This code has multiple issues:
* it names the local variable for the GList "count", which shadows the "qemu_plugin_scoreboard *count". This isn't incorrect, but it is unnecessarily confusing
* it doesn't free the list, and the leak sanitizer complains:

    Indirect leak of 2328 byte(s) in 97 object(s) allocated from:
     #0 0x5589b0b72293 in malloc (/home/pm215/qemu/build/x86-tgt-san/qemu-system-i386+0x1a2f293) (BuildId: 26964cad9e3f81d35fc144d7cc88b53adf6f60c7)
     #1 0x78fd8cfa1ac9 in g_malloc (/lib/x86_64-linux-gnu/libglib-2.0.so.0+0x62ac9) (BuildId: 116e142b9b52c8a4dfd403e759e71ab8f95d8bb3)
     #2 0x78fd8cf96e4a in g_list_prepend (/lib/x86_64-linux-gnu/libglib-2.0.so.0+0x57e4a) (BuildId: 116e142b9b52c8a4dfd403e759e71ab8f95d8bb3)
     #3 0x78fd8cf8b318 in g_hash_table_get_values (/lib/x86_64-linux-gnu/libglib-2.0.so.0+0x4c318) (BuildId: 116e142b9b52c8a4dfd403e759e71ab8f95d8bb3)
     #4 0x78fd84d1a90c in plugin_exit /home/pm215/qemu/build/x86-tgt-san/../../tests/tcg/plugins/mem.c:87:25

* in iterating through the list it updates "count", so by the time we get to the end of the loop we no longer have a pointer to the head of the list that we could use to free it
* it checks for the list being NULL twice (once in an if() and once in the for() loop's "while" condition), which is redundant
* it skips the loop if g_list_next(counts) is NULL, which means it will wrongly skip the loop if the list has only one entry

Rewrite the iteration code to fix these problems.

Quoted 8 lines

---
I am assuming that not printing the region info when there is exactly one region is a mistake, but perhaps it is intentional?

  tests/tcg/plugins/mem.c | 24 +++++++++++-------------
  1 file changed, 11 insertions(+), 13 deletions(-)
#5 lore raw

PATCH 3/3 tests/tcg/plugins/patch: Free read_data in patch_hwaddr()

In reply to Peter Maydell

In patch_hwaddr() we allocate a GByteArray for the data we read back from the guest; however we forget to free it, and the leak sanitizer complains:

Direct leak of 40 byte(s) in 1 object(s) allocated from:

    #0 0x56c00ad48293 in malloc (/home/pm215/qemu/build/x86-tgt-san/qemu-system-x86_64+0x1a9f293) (BuildId: 62e2a7dbe5ff146b2fa14d26e24e443f1967edd9)
    #1 0x7b3e4cc91ac9 in g_malloc (/lib/x86_64-linux-gnu/libglib-2.0.so.0+0x62ac9) (BuildId: 116e142b9b52c8a4dfd403e759e71ab8f95d8bb3)
    #2 0x7b3e4cc54c12 in g_array_sized_new (/lib/x86_64-linux-gnu/libglib-2.0.so.0+0x25c12) (BuildId: 116e142b9b52c8a4dfd403e759e71ab8f95d8bb3)
    #3 0x7b3e44b06b49 in patch_hwaddr /home/pm215/qemu/build/x86-tgt-san/../../tests/tcg/plugins/patch.c:68:29

Indirect leak of 16 byte(s) in 1 object(s) allocated from:

    #0 0x56c00ad486b0 in realloc (/home/pm215/qemu/build/x86-tgt-san/qemu-system-x86_64+0x1a9f6b0) (BuildId: 62e2a7dbe5ff146b2fa14d26e24e443f1967edd9)
    #1 0x7b3e4cc92819 in g_realloc (/lib/x86_64-linux-gnu/libglib-2.0.so.0+0x63819) (BuildId: 116e142b9b52c8a4dfd403e759e71ab8f95d8bb3)
    #2 0x7b3e4cc54b36  (/lib/x86_64-linux-gnu/libglib-2.0.so.0+0x25b36) (BuildId: 116e142b9b52c8a4dfd403e759e71ab8f95d8bb3)
    #3 0x7b3e4cc55276 in g_array_set_size (/lib/x86_64-linux-gnu/libglib-2.0.so.0+0x26276) (BuildId: 116e142b9b52c8a4dfd403e759e71ab8f95d8bb3)
    #4 0x7b3e4cc55574 in g_byte_array_set_size (/lib/x86_64-linux-gnu/libglib-2.0.so.0+0x26574) (BuildId: 116e142b9b52c8a4dfd403e759e71ab8f95d8bb3)
    #5 0x56c00be2ccc1 in qemu_plugin_read_memory_hwaddr /home/pm215/qemu/build/x86-tgt-san/../../plugins/api.c:524:5

Mark the variable as g_autoptr(), as we already do in the equivalent code in patch_vaddr().

---
 tests/tcg/plugins/patch.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tests/tcg/plugins/patch.c +1 -1
index 111c5c1f16..eba2f8b8d6 100644
@@ -65,7 +65,7 @@ static void patch_hwaddr(unsigned int vcpu_index, void *userdata)
         return;
     }
 
-    GByteArray *read_data = g_byte_array_new();
+    g_autoptr(GByteArray) read_data = g_byte_array_new();
 
     result = qemu_plugin_read_memory_hwaddr(addr, read_data,
                                             patch_data->len);
Signature · 1 line
2.43.0
Reviewed-by on v1 3/3
#6 lore raw

Re: [PATCH 3/3] tests/tcg/plugins/patch: Free read_data in patch_hwaddr()

Quoted 18 lines · Peter Maydell

In patch_hwaddr() we allocate a GByteArray for the data we read back from the guest; however we forget to free it, and the leak sanitizer complains:

Direct leak of 40 byte(s) in 1 object(s) allocated from:

     #0 0x56c00ad48293 in malloc (/home/pm215/qemu/build/x86-tgt-san/qemu-system-x86_64+0x1a9f293) (BuildId: 62e2a7dbe5ff146b2fa14d26e24e443f1967edd9)
     #1 0x7b3e4cc91ac9 in g_malloc (/lib/x86_64-linux-gnu/libglib-2.0.so.0+0x62ac9) (BuildId: 116e142b9b52c8a4dfd403e759e71ab8f95d8bb3)
     #2 0x7b3e4cc54c12 in g_array_sized_new (/lib/x86_64-linux-gnu/libglib-2.0.so.0+0x25c12) (BuildId: 116e142b9b52c8a4dfd403e759e71ab8f95d8bb3)
     #3 0x7b3e44b06b49 in patch_hwaddr /home/pm215/qemu/build/x86-tgt-san/../../tests/tcg/plugins/patch.c:68:29

Indirect leak of 16 byte(s) in 1 object(s) allocated from:

     #0 0x56c00ad486b0 in realloc (/home/pm215/qemu/build/x86-tgt-san/qemu-system-x86_64+0x1a9f6b0) (BuildId: 62e2a7dbe5ff146b2fa14d26e24e443f1967edd9)
     #1 0x7b3e4cc92819 in g_realloc (/lib/x86_64-linux-gnu/libglib-2.0.so.0+0x63819) (BuildId: 116e142b9b52c8a4dfd403e759e71ab8f95d8bb3)
     #2 0x7b3e4cc54b36  (/lib/x86_64-linux-gnu/libglib-2.0.so.0+0x25b36) (BuildId: 116e142b9b52c8a4dfd403e759e71ab8f95d8bb3)
     #3 0x7b3e4cc55276 in g_array_set_size (/lib/x86_64-linux-gnu/libglib-2.0.so.0+0x26276) (BuildId: 116e142b9b52c8a4dfd403e759e71ab8f95d8bb3)
     #4 0x7b3e4cc55574 in g_byte_array_set_size (/lib/x86_64-linux-gnu/libglib-2.0.so.0+0x26574) (BuildId: 116e142b9b52c8a4dfd403e759e71ab8f95d8bb3)
     #5 0x56c00be2ccc1 in qemu_plugin_read_memory_hwaddr /home/pm215/qemu/build/x86-tgt-san/../../plugins/api.c:524:5
Quoted 8 lines

Mark the variable as g_autoptr(), as we already do in the equivalent code in patch_vaddr().

---
  tests/tcg/plugins/patch.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
#7 lore raw
In reply to Peter Maydell
Quoted 10 lines · Peter Maydell

This patchset fixes some minor issues in the tests/tcg/plugins code which the sanitizers detected.

Patch 1 is the one I sent earlier today, and has been reviewed. Patches 2 and 3 are new.

thanks
-- PMM

Quoted 9 lines

Peter Maydell (3):
tests/tcg/plugins/mem: Don't access unaligned memory tests/tcg/plugins/mem: Correct hash iteration code in plugin_exit() tests/tcg/plugins/patch: Free read_data in patch_hwaddr()

  tests/tcg/plugins/mem.c   | 95 +++++++++++++++++----------------------
  tests/tcg/plugins/patch.c |  2 +-
  2 files changed, 42 insertions(+), 55 deletions(-)

Thanks Peter,
I'll pick up this in next PR.