Skip to contentSkip to conversation

ppc/spapr: Fix ubsan warning with unaligned pointer access

PATCH In review Daniel Hoffman 3 participants 3 messages Dec 17, 2023 – Dec 22, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ppc/spapr: Fix ubsan warning with unaligned pointer access

Found while running QTest with UBsan. Unaligned pointers appear to be valid, so moving the read to an explicit memcpy to an intermediate.

---
 hw/ppc/vof.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
hw/ppc/vof.c +4 -1
index e3b430a81f4..609a51c645d 100644
@@ -646,7 +646,10 @@ static void vof_dt_memory_available(void *fdt, GArray *claimed, uint64_t base)
     mem0_reg = fdt_getprop(fdt, offset, "reg", &proplen);
     g_assert(mem0_reg && proplen == sizeof(uint32_t) * (ac + sc));
     if (sc == 2) {
-        mem0_end = be64_to_cpu(*(uint64_t *)(mem0_reg + sizeof(uint32_t) * ac));
+        /* Pointer may be unaligned */
+        uint64_t mem0_end_copy;
+        memcpy(&mem0_end_copy, mem0_reg + sizeof(uint32_t) * ac, sizeof(mem0_end_copy));
+        mem0_end = be64_to_cpu(mem0_end_copy);
     } else {
         mem0_end = be32_to_cpu(*(uint32_t *)(mem0_reg + sizeof(uint32_t) * ac));
     }
Signature · 1 line
2.40.1
#1 lore raw
Quoted 11 lines · Daniel Hoffman

Found while running QTest with UBsan. Unaligned pointers appear to be valid, so moving the read to an explicit memcpy to an intermediate.

---
  hw/ppc/vof.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
hw/ppc/vof.c +0 -0
index e3b430a81f4..609a51c645d 100644

hw/ppc/vof.c static void vof_dt_memory_available(void *fdt, GArray *claimed, uint64_t base) · line 652

Quoted 8 lines
hw/ppc/vof.c +4 -1
@@ -646,7 +646,10 @@ static void vof_dt_memory_available(void *fdt, GArray *claimed, uint64_t base)
      mem0_reg = fdt_getprop(fdt, offset, "reg", &proplen);
      g_assert(mem0_reg && proplen == sizeof(uint32_t) * (ac + sc));
      if (sc == 2) {
-        mem0_end = be64_to_cpu(*(uint64_t *)(mem0_reg + sizeof(uint32_t) * ac));
+        /* Pointer may be unaligned */
+        uint64_t mem0_end_copy;
+        memcpy(&mem0_end_copy, mem0_reg + sizeof(uint32_t) * ac, sizeof(mem0_end_copy));
+        mem0_end = be64_to_cpu(mem0_end_copy);

mem0_end = ldq_be_p(mem0_reg + sizeof(uint32_t) * ac);

r~

#2 lore raw
Quoted 17 lines · Richard Henderson
Quoted 16 lines · Daniel Hoffman

Found while running QTest with UBsan. Unaligned pointers appear to be valid, so moving the read to an explicit memcpy to an intermediate.
---
  hw/ppc/vof.c | 5 ++++-

  1 file changed, 4 insertions(+), 1 deletion(-)
hw/ppc/vof.c +0 -1
index e3b430a81f4..609a51c645d 100644
@@ -646,7 +646,10 @@ static void vof_dt_memory_available(void *fdt, 
 GArray *claimed, uint64_t base)
       mem0_reg = fdt_getprop(fdt, offset, "reg", &proplen);
       g_assert(mem0_reg && proplen == sizeof(uint32_t) * (ac + sc));
       if (sc == 2) {
-        mem0_end = be64_to_cpu(*(uint64_t *)(mem0_reg + 
Quoted 8 lines

hw/ppc/vof.c static void vof_dt_memory_available(void *fdt, GArray *claimed, uint64_t base) · line 652

Quoted 6 lines
hw/ppc/vof.c +4 -0
 sizeof(uint32_t) * ac));
+        /* Pointer may be unaligned */
+        uint64_t mem0_end_copy;
+        memcpy(&mem0_end_copy, mem0_reg + sizeof(uint32_t) * ac, 
 sizeof(mem0_end_copy));
+        mem0_end = be64_to_cpu(mem0_end_copy);

mem0_end = ldq_be_p(mem0_reg + sizeof(uint32_t) * ac);

+1 for ldq_be_p(). Or read lo&hi 32bit chunks and combine. Thanks,

Quoted 3 lines

r~

Signature · 1 line
Alexey