Skip to contentSkip to conversation

tcg: Avoid undefined behaviour on unaligned stores

RFC In review Peter Maydell 3 participants 8 messages Mar 28, 2014

lore
Share & tools
lore
#0 lore raw

cover tcg: Avoid undefined behaviour on unaligned stores

These patches fix various cases in the x86 backend and the runtime TB-jump-patching code where we cast an unaligned pointer to a uint32_t* and store into it. Unaligned accesses are OK on x86 hardware, of course, but this is still undefined behaviour in C, and the clang sanitizer complains.

Sent out as an RFC to get a feel for whether we want to go down some road like this. Personally I think it is worthwhile for two reasons:
(1) it's easier to see sanitizer warnings that actually matter if
they're not hidden in among a lot of warnings that don't.
(2) it's increasingly clear that it's a bad idea to trust
compiler engineers, who will happily throw real applications under the bus for a 0.05% improvement in Dhrystone scores; so if we can reasonably avoid undefined behaviour we should.

It would probably be better to split the tcg_out functions into separate ones for "I know this pointer is aligned" and "may be unaligned", to avoid overhead on hosts which don't have cheap unaligned stores. Also I haven't tried to extend the tcg_patch* usage to the other backends.

Peter Maydell (3):
exec-all.h: Use stl_p to avoid undefined behaviour patching x86 jumps
tcg: Avoid stores to unaligned addresses
tcg: Avoid undefined behaviour patching code at unaligned addresses

 include/exec/exec-all.h |  2 +-
 tcg/i386/tcg-target.c   | 12 ++++++------
 tcg/tcg.c               | 26 +++++++++++++++++++++++---
 3 files changed, 30 insertions(+), 10 deletions(-)
Signature · 1 line
1.9.0
#1 lore raw

RFC 1/3 exec-all.h: Use stl_p to avoid undefined behaviour patching x86 jumps

The code which patches x86 jump instructions assumes it can do an unaligned write of a uint32_t. This is actually safe on x86, but it's still undefined behaviour. We have infrastructure for doing efficient unaligned accesses which doesn't engage in undefined behaviour, so use it.

This is technically fractionally less efficient, at least with gcc 4.6; instead of one instruction:
7b2: 89 3e mov %edi,(%rsi)
we get an extra spurious store to the stack slot:

 7b2:   89 7c 24 64             mov    %edi,0x64(%rsp)
 7b6:   89 3e                   mov    %edi,(%rsi)
---
 include/exec/exec-all.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/exec/exec-all.h +1 -1
index f9ac332..1c49a21 100644
@@ -229,7 +229,7 @@ void ppc_tb_set_jmp_target(unsigned long jmp_addr, unsigned long addr);
 static inline void tb_set_jmp_target1(uintptr_t jmp_addr, uintptr_t addr)
 {
     /* patch the branch destination */
-    *(uint32_t *)jmp_addr = addr - (jmp_addr + 4);
+    stl_p((void*)jmp_addr, addr - (jmp_addr + 4));
     /* no need to flush icache explicitly */
 }
 #elif defined(__aarch64__)
Signature · 1 line
1.9.0
#2 lore raw

RFC 2/3 tcg: Avoid stores to unaligned addresses

In reply to Peter Maydell

Avoid stores to unaligned addresses in TCG code generation, by using the usual memcpy() approach. (Using bswap.h would drag a lot of QEMU baggage into TCG, so it's simpler just to do direct memcpy() here.)

---
 tcg/tcg.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tcg/tcg.c +3 -3
index f1e0763..60f06c5 100644
@@ -125,21 +125,21 @@ static inline void tcg_out8(TCGContext *s, uint8_t v)
 static inline void tcg_out16(TCGContext *s, uint16_t v)
 {
     uint8_t *p = s->code_ptr;
-    *(uint16_t *)p = v;
+    memcpy(p, &v, sizeof(v));
     s->code_ptr = p + 2;
 }
 
 static inline void tcg_out32(TCGContext *s, uint32_t v)
 {
     uint8_t *p = s->code_ptr;
-    *(uint32_t *)p = v;
+    memcpy(p, &v, sizeof(v));
     s->code_ptr = p + 4;
 }
 
 static inline void tcg_out64(TCGContext *s, uint64_t v)
 {
     uint8_t *p = s->code_ptr;
-    *(uint64_t *)p = v;
+    memcpy(p, &v, sizeof(v));
     s->code_ptr = p + 8;
 }
 
Signature · 1 line
1.9.0
Reviewed-by on v1 2/3
#3 lore raw

Re: [Qemu-devel] [RFC 2/3] tcg: Avoid stores to unaligned addresses

Am 28.03.2014 16:29, schrieb Peter Maydell:

Quoted 5 lines

Avoid stores to unaligned addresses in TCG code generation, by using the usual memcpy() approach. (Using bswap.h would drag a lot of QEMU baggage into TCG, so it's simpler just to do direct memcpy() here.)

Andreas

Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#4 lore raw

RFC 3/3 tcg: Avoid undefined behaviour patching code at unaligned addresses

In reply to Peter Maydell

To avoid C undefined behaviour when patching generated code, provide wrappers tcg_patch8/16/32/64 which use the usual memcpy trick, and use them in the i386 backend.

---
 tcg/i386/tcg-target.c | 12 ++++++------
 tcg/tcg.c             | 20 ++++++++++++++++++++
 2 files changed, 26 insertions(+), 6 deletions(-)
tcg/i386/tcg-target.c +6 -6
index f832282..a0f9dff 100644
@@ -151,14 +151,14 @@ static void patch_reloc(uint8_t *code_ptr, int type,
         if (value != (int32_t)value) {
             tcg_abort();
         }
-        *(uint32_t *)code_ptr = value;
+        tcg_patch32(code_ptr, value);
         break;
     case R_386_PC8:
         value -= (uintptr_t)code_ptr;
         if (value != (int8_t)value) {
             tcg_abort();
         }
-        *(uint8_t *)code_ptr = value;
+        tcg_patch8(code_ptr, value);
         break;
     default:
         tcg_abort();
@@ -1276,9 +1276,9 @@ static void tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *l)
     uint8_t **label_ptr = &l->label_ptr[0];
 
     /* resolve label address */
-    *(uint32_t *)label_ptr[0] = (uint32_t)(s->code_ptr - label_ptr[0] - 4);
+    tcg_patch32(label_ptr[0], s->code_ptr - label_ptr[0] - 4);
     if (TARGET_LONG_BITS > TCG_TARGET_REG_BITS) {
-        *(uint32_t *)label_ptr[1] = (uint32_t)(s->code_ptr - label_ptr[1] - 4);
+        tcg_patch32(label_ptr[1], s->code_ptr - label_ptr[1] - 4);
     }
 
     if (TCG_TARGET_REG_BITS == 32) {
@@ -1360,9 +1360,9 @@ static void tcg_out_qemu_st_slow_path(TCGContext *s, TCGLabelQemuLdst *l)
     TCGReg retaddr;
 
     /* resolve label address */
-    *(uint32_t *)label_ptr[0] = (uint32_t)(s->code_ptr - label_ptr[0] - 4);
+    tcg_patch32(label_ptr[0], s->code_ptr - label_ptr[0] - 4);
     if (TARGET_LONG_BITS > TCG_TARGET_REG_BITS) {
-        *(uint32_t *)label_ptr[1] = (uint32_t)(s->code_ptr - label_ptr[1] - 4);
+        tcg_patch32(label_ptr[1], s->code_ptr - label_ptr[1] - 4);
     }
 
     if (TCG_TARGET_REG_BITS == 32) {
tcg/tcg.c +20 -0
index 60f06c5..727112d 100644
@@ -143,6 +143,26 @@ static inline void tcg_out64(TCGContext *s, uint64_t v)
     s->code_ptr = p + 8;
 }
 
+static inline void tcg_patch8(uint8_t *p, uint8_t v)
+{
+    memcpy(p, &v, sizeof(v));
+}
+
+static inline void tcg_patch16(uint8_t *p, uint16_t v)
+{
+    memcpy(p, &v, sizeof(v));
+}
+
+static inline void tcg_patch32(uint8_t *p, uint32_t v)
+{
+    memcpy(p, &v, sizeof(v));
+}
+
+static inline void tcg_patch64(uint8_t *p, uint64_t v)
+{
+    memcpy(p, &v, sizeof(v));
+}
+
 /* label relocation processing */
 
 static void tcg_out_reloc(TCGContext *s, uint8_t *code_ptr, int type,
Signature · 1 line
1.9.0
#6 lore raw
Quoted 7 lines · Richard Henderson
Quoted 4 lines · Peter Maydell

Peter Maydell (3):
exec-all.h: Use stl_p to avoid undefined behaviour patching x86 jumps
tcg: Avoid stores to unaligned addresses
tcg: Avoid undefined behaviour patching code at unaligned addresses

I was expecting more argument :-)

I'll do a respin with separate tcg_out*_unaligned and tcg_out*, and update all the backends.

thanks
-- PMM

#7 lore raw
Quoted 7 lines · Peter Maydell
Quoted 6 lines · Richard Henderson
Quoted 4 lines · Peter Maydell

Peter Maydell (3):
exec-all.h: Use stl_p to avoid undefined behaviour patching x86 jumps
tcg: Avoid stores to unaligned addresses
tcg: Avoid undefined behaviour patching code at unaligned addresses

Quoted 6 lines
Quoted 1 line

I was expecting more argument :-)

I'll do a respin with separate tcg_out*_unaligned and tcg_out*, and update all the backends.

Please don't. I'd rather make different changes there, making s->code_ptr be more appropriate to the elemental type of the native insn.

Since x86 wouldn't change its elemental type, I think your change is the best of the possible solutions there.

r~