Skip to contentSkip to conversation

target/hexagon: fix the allocframe framekey

PATCH Superseded Brian Cain 3 participants 4 messages Oct 16 – Oct 20 Arm/Amlogic Meson SoC support

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Brian Cain

author
#0 lore raw

PATCH 1/1 target/hexagon: fix the allocframe framekey

The frame that allocframe pushes is {LR, FP}, and what is scrambled is the LR word: FRAMEKEY is XOR'd into the upper 32 bits. The generated code for fFRAME_SCRAMBLE built the key from FRAMEKEY in both halves of a 64-bit value, so it scrambled FP as well. deallocframe and dealloc_return only undo the upper word, so a FP restored from a frame pushed with a non-zero FRAMEKEY was corrupt.

Uncovered by testing some boot images using QuRT.

---
 target/hexagon/idef-parser/parser-helpers.c |  2 +-
 tests/tcg/hexagon/framekey.c                | 67 +++++++++++++++++++++
 tests/tcg/hexagon/meson.build               |  1 +
 3 files changed, 69 insertions(+), 1 deletion(-)
 create mode 100644 tests/tcg/hexagon/framekey.c
target/hexagon/idef-parser/parser-helpers.c +1 -1
index 49cc11fddd8..14840d98737 100644
@@ -1989,7 +1989,7 @@ HexValue gen_rvalue_fscr(Context *c, YYLTYPE *locp, HexValue *value)
     *value = gen_rvalue_extend(c, locp, value);
     OUT(c, locp, "gen_read_reg(", &frame_key, ", HEX_REG_FRAMEKEY);\n");
     OUT(c, locp, "tcg_gen_concat_i32_i64(",
-        &key, ", ", &frame_key, ", ", &frame_key, ");\n");
+        &key, ", tcg_constant_tl(0), ", &frame_key, ");\n");
     OUT(c, locp, "tcg_gen_xor_i64(", &res, ", ", value, ", ", &key, ");\n");
     return res;
 }
tests/tcg/hexagon/framekey.c new file +67 -0

67 lines not on this page · raw message

tests/tcg/hexagon/meson.build +1 -0
index a6ace4c77c2..0fd62ce7e4c 100644
@@ -68,6 +68,7 @@ tests += {
   'overflow.c': {'cflags': cflags, 'exe_name': 'hex-overflow'},
   'preg_alias.c': {'cflags': cflags},
   'dual_stores.c': {'cflags': cflags},
+  'framekey.c': {'cflags': cflags},
   'multi_result.c': {'cflags': cflags},
   'mem_noshuf.c': {'cflags': cflags},
   'mem_noshuf_exception.c': {'cflags': cflags},
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Brian Cain

The frame that allocframe pushes is {LR, FP}, and what is scrambled is the LR word: FRAMEKEY is XOR'd into the upper 32 bits. The generated code for fFRAME_SCRAMBLE built the key from FRAMEKEY in both halves of a 64-bit value, so it scrambled FP as well. deallocframe and dealloc_return only undo the upper word, so a FP restored from a frame pushed with a non-zero FRAMEKEY was corrupt.

Uncovered by testing some boot images using QuRT.

Quoted 8 lines
---
 target/hexagon/idef-parser/parser-helpers.c |  2 +-
 tests/tcg/hexagon/framekey.c                | 67 +++++++++++++++++++++
 tests/tcg/hexagon/meson.build               |  1 +
 3 files changed, 69 insertions(+), 1 deletion(-)
 create mode 100644 tests/tcg/hexagon/framekey.c
#2 lore raw
In reply to Brian Cain
Quoted 7 lines · Brian Cain

The frame that allocframe pushes is {LR, FP}, and what is scrambled is the LR word: FRAMEKEY is XOR'd into the upper 32 bits. The generated code for fFRAME_SCRAMBLE built the key from FRAMEKEY in both halves of a 64-bit value, so it scrambled FP as well. deallocframe and dealloc_return only undo the upper word, so a FP restored from a frame pushed with a non-zero FRAMEKEY was corrupt.

I think the commit message is not quite right. AFAICT the error only shows up when idef-parser is used, FRAMEKEY is not 0, and allocframe is followed by dealloc_return.

That also means the test doesn't cover the case that actually breaks, because it pops the frame with deallocframe. allocframe and deallocframe have the same bug, so FP comes back correct either way. Only the first check catches the bug, through the wrong FP word that allocframe stores in memory. The other checks pass with or without the fix.

author
#3 lore raw
Quoted 11 lines · Marco Liebel
Quoted 6 lines · Brian Cain

The frame that allocframe pushes is {LR, FP}, and what is scrambled is the LR word: FRAMEKEY is XOR'd into the upper 32 bits. The generated code for fFRAME_SCRAMBLE built the key from FRAMEKEY in both halves of a 64-bit value, so it scrambled FP as well. deallocframe and dealloc_return only undo the upper word, so a FP restored from a frame pushed with a non-zero FRAMEKEY was corrupt.

I think the commit message is not quite right. AFAICT the error only shows up when idef-parser is used, FRAMEKEY is not 0, and allocframe is followed by dealloc_return.

Quoted 6 lines

That also means the test doesn't cover the case that actually breaks, because it pops the frame with deallocframe. allocframe and deallocframe have the same bug, so FP comes back correct either way. Only the first check catches the bug, through the wrong FP word that allocframe stores in memory. The other checks pass with or without the fix.

fixed in v2: https://lore.kernel.org/qemu-devel/[email protected]/