Skip to contentSkip to conversation

Enable UBSAN on ppc

PATCH Superseded Shawn Anastasio 2 participants 6 messages Feb 21, 2025 – Feb 26, 2025

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Enable UBSAN on ppc

Extend Andrew's UBSAN enablement work to ppc.

Andrew Cooper (1):
PPC: Activate UBSAN in testing

Shawn Anastasio (1):
xen/ppc: Fix opal.c's misaligned DT reads to avoid tripping UBSAN

 automation/gitlab-ci/build.yaml      |  3 +++
 xen/arch/ppc/Kconfig                 |  1 +
 xen/arch/ppc/include/asm/processor.h |  2 ++
 xen/arch/ppc/opal.c                  | 18 +++++++++++-------
 xen/arch/ppc/stubs.c                 |  2 +-
 5 files changed, 18 insertions(+), 8 deletions(-)

--
2.30.2

#1 lore raw

PATCH 1/2 xen/ppc: Fix opal.c's misaligned DT reads to avoid tripping UBSAN

Fix two misaligned reads from the FDT in the opal setup code to avoid tripping UBSAN failures. Without this change, UBSAN-enabled builds on PPC will fail on boot before the serial console is even initialized.

---
 xen/arch/ppc/opal.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)
xen/arch/ppc/opal.c +11 -7
index 1183b7d5ef..3d0e4daf27 100644
@@ -34,8 +34,9 @@ static void opal_putchar(char c)
 void __init boot_opal_init(const void *fdt)
 {
     int opal_node;
-    const __be64 *opal_base;
-    const __be64 *opal_entry;
+    const __be64 *opal_base_p;
+    const __be64 *opal_entry_p;
+    __be64 opal_base, opal_entry;
 
     if ( fdt_check_header(fdt) < 0 )
     {
@@ -54,17 +55,20 @@ void __init boot_opal_init(const void *fdt)
         die();
     }
 
-    opal_base = fdt_getprop(fdt, opal_node, "opal-base-address", NULL);
-    opal_entry = fdt_getprop(fdt, opal_node, "opal-entry-address", NULL);
-    if ( !opal_base || !opal_entry )
+    opal_base_p = fdt_getprop(fdt, opal_node, "opal-base-address", NULL);
+    opal_entry_p = fdt_getprop(fdt, opal_node, "opal-entry-address", NULL);
+    if ( !opal_base_p || !opal_entry_p )
     {
         early_printk("Failed to get opal-base-address/opal-entry-address "
                      "property from DT!\n");
         die();
     }
 
-    opal.base = be64_to_cpu(*opal_base);
-    opal.entry = be64_to_cpu(*opal_entry);
+    memcpy(&opal_base, opal_base_p, sizeof(opal_base));
+    memcpy(&opal_entry, opal_entry_p, sizeof(opal_entry));
+
+    opal.base = be64_to_cpu(opal_base);
+    opal.entry = be64_to_cpu(opal_entry);
 
     early_printk_init(opal_putchar);
 
Signature · 1 line
2.30.2
#2 lore raw

Re: [PATCH 1/2] xen/ppc: Fix opal.c's misaligned DT reads to avoid tripping UBSAN

Quoted 39 lines · Shawn Anastasio

Fix two misaligned reads from the FDT in the opal setup code to avoid tripping UBSAN failures. Without this change, UBSAN-enabled builds on PPC will fail on boot before the serial console is even initialized.

---
 xen/arch/ppc/opal.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)
xen/arch/ppc/opal.c +6 -5
index 1183b7d5ef..3d0e4daf27 100644
@@ -34,8 +34,9 @@ static void opal_putchar(char c)
 void __init boot_opal_init(const void *fdt)
 {
     int opal_node;
-    const __be64 *opal_base;
-    const __be64 *opal_entry;
+    const __be64 *opal_base_p;
+    const __be64 *opal_entry_p;
+    __be64 opal_base, opal_entry;
 
     if ( fdt_check_header(fdt) < 0 )
     {
@@ -54,17 +55,20 @@ void __init boot_opal_init(const void *fdt)
         die();
     }
 
-    opal_base = fdt_getprop(fdt, opal_node, "opal-base-address", NULL);
-    opal_entry = fdt_getprop(fdt, opal_node, "opal-entry-address", NULL);
-    if ( !opal_base || !opal_entry )
+    opal_base_p = fdt_getprop(fdt, opal_node, "opal-base-address", NULL);
+    opal_entry_p = fdt_getprop(fdt, opal_node, "opal-entry-address", NULL);
+    if ( !opal_base_p || !opal_entry_p )
     {
         early_printk("Failed to get opal-base-address/opal-entry-address "
                      "property from DT!\n");

xen/arch/ppc/opal.c void __init boot_opal_init(const void *fdt) · line 71

Quoted 10 lines
xen/arch/ppc/opal.c +5 -2
         die();
     }
 
-    opal.base = be64_to_cpu(*opal_base);
-    opal.entry = be64_to_cpu(*opal_entry);
+    memcpy(&opal_base, opal_base_p, sizeof(opal_base));
+    memcpy(&opal_entry, opal_entry_p, sizeof(opal_entry));
+
+    opal.base = be64_to_cpu(opal_base);
+    opal.entry = be64_to_cpu(opal_entry);

Thanks for getting to the bottom of this.

However, you can use get_unaligned_*() and friends which is probably a bit nicer, and doesn't involve the extra local variables.

~Andrew

#3 lore raw

Re: [PATCH 1/2] xen/ppc: Fix opal.c's misaligned DT reads to avoid tripping UBSAN

Quoted 52 lines · Andrew Cooper
Quoted 39 lines · Shawn Anastasio

Fix two misaligned reads from the FDT in the opal setup code to avoid tripping UBSAN failures. Without this change, UBSAN-enabled builds on PPC will fail on boot before the serial console is even initialized.

---
 xen/arch/ppc/opal.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)
xen/arch/ppc/opal.c +6 -5
index 1183b7d5ef..3d0e4daf27 100644
@@ -34,8 +34,9 @@ static void opal_putchar(char c)
 void __init boot_opal_init(const void *fdt)
 {
     int opal_node;
-    const __be64 *opal_base;
-    const __be64 *opal_entry;
+    const __be64 *opal_base_p;
+    const __be64 *opal_entry_p;
+    __be64 opal_base, opal_entry;
 
     if ( fdt_check_header(fdt) < 0 )
     {
@@ -54,17 +55,20 @@ void __init boot_opal_init(const void *fdt)
         die();
     }
 
-    opal_base = fdt_getprop(fdt, opal_node, "opal-base-address", NULL);
-    opal_entry = fdt_getprop(fdt, opal_node, "opal-entry-address", NULL);
-    if ( !opal_base || !opal_entry )
+    opal_base_p = fdt_getprop(fdt, opal_node, "opal-base-address", NULL);
+    opal_entry_p = fdt_getprop(fdt, opal_node, "opal-entry-address", NULL);
+    if ( !opal_base_p || !opal_entry_p )
     {
         early_printk("Failed to get opal-base-address/opal-entry-address "
                      "property from DT!\n");

xen/arch/ppc/opal.c void __init boot_opal_init(const void *fdt) · line 71

Quoted 10 lines
xen/arch/ppc/opal.c +5 -2
         die();
     }
 
-    opal.base = be64_to_cpu(*opal_base);
-    opal.entry = be64_to_cpu(*opal_entry);
+    memcpy(&opal_base, opal_base_p, sizeof(opal_base));
+    memcpy(&opal_entry, opal_entry_p, sizeof(opal_entry));
+
+    opal.base = be64_to_cpu(opal_base);
+    opal.entry = be64_to_cpu(opal_entry);

Thanks for getting to the bottom of this.

Quoted 2 lines

However, you can use get_unaligned_*() and friends which is probably a bit nicer, and doesn't involve the extra local variables.

Sorry, the other thing to say is that if PPC is generally fine with unaligned accesses, then you might want to follow what x86 does.

We use -fno-sanitize=alignment generally, because there's a whole pile of things which are misaigned and spec'd that way for backwards compatibility.

~Andrew

#4 lore raw

Re: [PATCH 1/2] xen/ppc: Fix opal.c's misaligned DT reads to avoid tripping UBSAN

Quoted 6 lines · Andrew Cooper

Sorry, the other thing to say is that if PPC is generally fine with unaligned accesses, then you might want to follow what x86 does.

We use -fno-sanitize=alignment generally, because there's a whole pile of things which are misaigned and spec'd that way for backwards compatibility.

Oh perfect -- I did do an initial grep to see if this was done but couldn't immediately find it. The Power ISA does guarantee that unaligned word/doubleword reads are handled transparently by the hardware so enabling this seems like a reasonable approach on PPC as well. I'll submit a v2 that does this.

Quoted 1 line

~Andrew

Thanks,
Shawn

#5 lore raw

PATCH 2/2 PPC: Activate UBSAN in testing

In reply to Shawn Anastasio

From: Andrew Cooper <[email protected]>

---
Changes from Andrew's v1:
- Define dump_execution_state in the same manner as risc-v

 automation/gitlab-ci/build.yaml      | 3 +++
 xen/arch/ppc/Kconfig                 | 1 +
 xen/arch/ppc/include/asm/processor.h | 2 ++
 xen/arch/ppc/stubs.c                 | 2 +-
 4 files changed, 7 insertions(+), 1 deletion(-)
automation/gitlab-ci/build.yaml +3 -0
index 35e224366f..6a2e491534 100644
@@ -352,6 +352,9 @@ debian-12-ppc64le-gcc-debug:
     CONTAINER: debian:12-ppc64le
     KBUILD_DEFCONFIG: ppc64_defconfig
     HYPERVISOR_ONLY: y
+    EXTRA_XEN_CONFIG: |
+      CONFIG_UBSAN=y
+      CONFIG_UBSAN_FATAL=y
 
 debian-12-riscv64-gcc-debug:
   extends: .gcc-riscv64-cross-build-debug
xen/arch/ppc/Kconfig +1 -0
index 6db575a48d..917f5d53a6 100644
@@ -2,6 +2,7 @@ config PPC
 	def_bool y
 	select FUNCTION_ALIGNMENT_4B
 	select HAS_DEVICE_TREE
+	select HAS_UBSAN
 	select HAS_VMAP
 
 config PPC64
xen/arch/ppc/include/asm/processor.h +2 -0
index a01b62b8a4..50161cc32d 100644
@@ -219,6 +219,8 @@ static inline void noreturn die(void)
  */
 #define cpu_relax() asm volatile ( "or %r1, %r1, %r1; or %r2, %r2, %r2" )
 
+#define dump_execution_state() run_in_exception_handler(show_execution_state)
+
 #endif /* __ASSEMBLY__ */
 
 #endif /* _ASM_PPC_PROCESSOR_H */
xen/arch/ppc/stubs.c +1 -1
index fff82f5cf3..671e71aa0a 100644
@@ -47,7 +47,7 @@ void send_timer_event(struct vcpu *v)
 
 void show_execution_state(const struct cpu_user_regs *regs)
 {
-    BUG_ON("unimplemented");
+    printk("TODO: Implement show_execution_state(regs)\n");
 }
 
 void arch_hypercall_tasklet_result(struct vcpu *v, long res)

--
2.30.2