[PATCH] platform: generic: ariane: fix stack corruption on early init

marouene.boubakri at oss.nxp.com marouene.boubakri at oss.nxp.com
Sat Nov 28 17:48:49 PST 2026


From: Marouene Boubakri <marouene.boubakri at oss.nxp.com>

struct plic_data ends with the flexible array member context_map[], and
the static "plic" instance initializes context_map[0].
ariane_early_init() copies it into a local "struct plic_data plic_data",
which only has room for the fixed part of the structure. GCC still
copies the initialized context_map[0] entry, so 4 bytes are written
past the end of the local variable.

With GCC 13.3 (rv64imafdc, -O2) these 4 bytes land on the stack slot
where ariane_early_init() saved s2, so s2 is corrupted when the
function returns. sbi_init() keeps the cold boot HART id in s2: it
turns from 0 into 0x10000, no longer matches the boot HART of the root
domain, and the boot of the CVA6 APU on the Digilent Genesys 2 stops
with:

  sbi_domain_startup: failed to start boot HART 0 for root (error -3)
  init_coldboot: domain startup failed (error -3)

Parse the PLIC node directly into the static instance. This is
equivalent, as fdt_parse_plic() does not modify the structure when it
fails.

Fixes: c75f468a ("platform: ariane: parse dtb for getting some initial parameters")
Signed-off-by: Marouene Boubakri <marouene.boubakri at oss.nxp.com>
---

Notes:
    Tested on the CVA6 APU on the Digilent Genesys 2 on top of OpenSBI v1.9,
    where this change makes the board boot to Linux (U-Boot SPL, OpenSBI
    fw_dynamic, U-Boot, Linux 7.3). On the current master it is build tested
    for the generic platform with GCC 13.3; the disassembly of
    ariane_early_init() shows the out-of-bounds store before the patch and
    none after.

 platform/generic/openhwgroup/ariane.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/platform/generic/openhwgroup/ariane.c b/platform/generic/openhwgroup/ariane.c
index ce2f5512..9909391a 100644
--- a/platform/generic/openhwgroup/ariane.c
+++ b/platform/generic/openhwgroup/ariane.c
@@ -60,7 +60,6 @@ static struct aclint_mtimer_data mtimer = {
 static int ariane_early_init(bool cold_boot)
 {
 	const void *fdt;
-	struct plic_data plic_data = plic;
 	unsigned long aclint_freq;
 	uint64_t clint_addr;
 	int rc;
@@ -87,9 +86,12 @@ static int ariane_early_init(bool cold_boot)
 				       ACLINT_DEFAULT_MTIMECMP_OFFSET;
 	}
 
-	rc = fdt_parse_plic(fdt, &plic_data, "riscv,plic0");
-	if (!rc)
-		plic = plic_data;
+	/*
+	 * struct plic_data ends with a flexible array member, so it must
+	 * not be copied to or from a local variable. fdt_parse_plic() leaves
+	 * the defaults untouched when it fails.
+	 */
+	fdt_parse_plic(fdt, &plic, "riscv,plic0");
 
 	return aclint_mswi_cold_init(&mswi);
 }
-- 
2.43.0




More information about the opensbi mailing list