Skip to contentSkip to conversation

Fix unaligned reads in the tcg/tci.c

PATCH In review Anatoly Trosinenko 3 participants 14 messages Jan 27, 2018 – Mar 12, 2018

lore
Share & tools
lore
#0 lore raw

cover Fix unaligned reads in the tcg/tci.c

The code in tcg/tci.c reads some data from TCI bytecode through pointer dereferencing. As far as I know unaligned reads in such a way are undefined behavior and compiling with -fsanitize=undefined enumerated them as such at run-time.

I have replaced such reads with invocations of ld{l,q}_he_p. A comment in include/qemu/bswap.h:310 suggests they should be properly translated by the compiler. I didn't added signed/unsigned casts since bswap.h does contain separate signed/unsigned versions for 16-bit integers but does not for 32- and 64-bit ones, so I supposed the developers of the bswap.h already arranged everything so integer promotions don't mess things up. I can add casts in case I'm not right about it.

Anatoly Trosinenko (1):
tci: eliminate UB due to unaligned reads

 tcg/tci.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
Signature · 1 line
2.14.1
#1 lore raw

PATCH 1/1 tci: eliminate UB due to unaligned reads

Use ldl_he_p / ldq_he_p functions instead of a plain memory access through pointer.

---
 tcg/tci.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
tcg/tci.c +11 -5
index 33edca1903..410817cf6a 100644
@@ -159,7 +159,13 @@ static uint64_t tci_uint64(uint32_t high, uint32_t low)
 /* Read constant (native size) from bytecode. */
 static tcg_target_ulong tci_read_i(uint8_t **tb_ptr)
 {
-    tcg_target_ulong value = *(tcg_target_ulong *)(*tb_ptr);
+#if TCG_TARGET_REG_BITS == 32
+    tcg_target_ulong value = ldl_he_p(*tb_ptr);
+#elif TCG_TARGET_REG_BITS == 64
+    tcg_target_ulong value = ldq_he_p(*tb_ptr);
+#else
+#error unsupported
+#endif
     *tb_ptr += sizeof(value);
     return value;
 }
@@ -167,7 +173,7 @@ static tcg_target_ulong tci_read_i(uint8_t **tb_ptr)
 /* Read unsigned constant (32 bit) from bytecode. */
 static uint32_t tci_read_i32(uint8_t **tb_ptr)
 {
-    uint32_t value = *(uint32_t *)(*tb_ptr);
+    uint32_t value = ldl_he_p(*tb_ptr);
     *tb_ptr += sizeof(value);
     return value;
 }
@@ -175,7 +181,7 @@ static uint32_t tci_read_i32(uint8_t **tb_ptr)
 /* Read signed constant (32 bit) from bytecode. */
 static int32_t tci_read_s32(uint8_t **tb_ptr)
 {
-    int32_t value = *(int32_t *)(*tb_ptr);
+    int32_t value = ldl_he_p(*tb_ptr);
     *tb_ptr += sizeof(value);
     return value;
 }
@@ -184,7 +190,7 @@ static int32_t tci_read_s32(uint8_t **tb_ptr)
 /* Read constant (64 bit) from bytecode. */
 static uint64_t tci_read_i64(uint8_t **tb_ptr)
 {
-    uint64_t value = *(uint64_t *)(*tb_ptr);
+    uint64_t value = ldq_he_p(*tb_ptr);
     *tb_ptr += sizeof(value);
     return value;
 }
@@ -1094,7 +1100,7 @@ uintptr_t tcg_qemu_tb_exec(CPUArchState *env, uint8_t *tb_ptr)
             /* QEMU specific operations. */
 
         case INDEX_op_exit_tb:
-            ret = *(uint64_t *)tb_ptr;
+            ret = ldq_he_p(tb_ptr);
             goto exit;
             break;
         case INDEX_op_goto_tb:
Signature · 1 line
2.14.1
#2 lore raw

Re: [Qemu-devel] [PATCH 1/1] tci: eliminate UB due to unaligned reads

Am 27.01.2018 um 14:49 schrieb Anatoly Trosinenko:

Quoted 7 lines

Use ldl_he_p / ldq_he_p functions instead of a plain memory access through pointer.

---
 tcg/tci.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

A better alternative might be aligning the relevant data when generating the bytecode. See also my comment on alignment in tcg/tci/README.

Stefan

#3 lore raw

Re: [Qemu-devel] [PATCH 1/1] tci: eliminate UB due to unaligned reads

My patch is kind of trivial quick fix that just eliminates these unaligned reads and doesn't seem to require complicated testing supposing my code properly handles integer promotion (and hope it will not slow the interpreter down).

Aligning everything, on the other hand, can not only remove the UB but also speed things up, but if I get it right, requires O(opcode count) manual work and subsequent less trivial testing that every opcode's argument layout match on generation and interpretation side (errors should be significantly localized due to present assertion on operation size, though). So my patch may be considered as temporary solution.

In fact, I had to similarly make these unaligned reads explicit when porting QEMU to JavaScript because Emscripten hugely relies on absence of some kinds of UB such as "implicit" unaligned accesses, and such fix seemed to resolve this issue for me on "host with special alignment requirement".

2018-01-27 19:38 GMT+03:00 Stefan Weil <[email protected]>:

Quoted 15 lines

Am 27.01.2018 um 14:49 schrieb Anatoly Trosinenko:

Quoted 7 lines

Use ldl_he_p / ldq_he_p functions instead of a plain memory access through pointer.

---
 tcg/tci.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

A better alternative might be aligning the relevant data when generating the bytecode. See also my comment on alignment in tcg/tci/README.

Stefan

Signature · 3 lines
С уважением,
Анатолий Тросиненко
e-mail: [email protected]
#13 lore raw
In reply to Anatoly Trosinenko

Ping.
Patchwork link: http://patchwork.ozlabs.org/patch/866732/
Patchew link:
http://patchew.org/QEMU/[email protected]/

(Initially forgot to add Richard Henderson to CC.)

2018-01-27 16:49 GMT+03:00 Anatoly Trosinenko <[email protected]> :

Quoted 24 lines

The code in tcg/tci.c reads some data from TCI bytecode through pointer dereferencing. As far as I know unaligned reads in such a way are undefined behavior and compiling with -fsanitize=undefined enumerated them as such at run-time.

I have replaced such reads with invocations of ld{l,q}_he_p. A comment in include/qemu/bswap.h:310 suggests they should be properly translated by the compiler. I didn't added signed/unsigned casts since bswap.h does contain separate signed/unsigned versions for 16-bit integers but does not for 32- and 64-bit ones, so I supposed the developers of the bswap.h already arranged everything so integer promotions don't mess things up. I can add casts in case I'm not right about it.

Anatoly Trosinenko (1):
tci: eliminate UB due to unaligned reads

 tcg/tci.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

--
2.14.1

Signature · 2 lines
Best regards,
Anatoly Trosinenko
#4 lore raw

Re: [Qemu-devel] [PATCH 1/1] tci: eliminate UB due to unaligned reads

In reply to Anatoly Trosinenko

Ping.
Patchwork link: http://patchwork.ozlabs.org/patch/866732/
Patchew link: http://patchew.org/QEMU/20180127134908.24095-1-
[email protected]/

Original cover letter:
The code in tcg/tci.c reads some data from TCI bytecode through pointer dereferencing. As far as I know unaligned reads in such a way are undefined behavior and compiling with -fsanitize=undefined enumerated them as such at run-time.

I have replaced such reads with invocations of ld{l,q}_he_p. A comment in include/qemu/bswap.h:310 suggests they should be properly translated by the compiler. I didn't added signed/unsigned casts since bswap.h does contain separate signed/unsigned versions for 16-bit integers but does not for 32- and 64-bit ones, so I supposed the developers of the bswap.h already arranged everything so integer promotions don't mess things up. I can add casts in case I'm not right about it.
http://lists.nongnu.org/archive/html/qemu-devel/2018-01/msg06516.html

2018-01-28 9:42 GMT+03:00 Anatoly Trosinenko <[email protected]>:

Quoted 35 lines

My patch is kind of trivial quick fix that just eliminates these unaligned reads and doesn't seem to require complicated testing supposing my code properly handles integer promotion (and hope it will not slow the interpreter down).

Aligning everything, on the other hand, can not only remove the UB but also speed things up, but if I get it right, requires O(opcode count) manual work and subsequent less trivial testing that every opcode's argument layout match on generation and interpretation side (errors should be significantly localized due to present assertion on operation size, though). So my patch may be considered as temporary solution.

In fact, I had to similarly make these unaligned reads explicit when porting QEMU to JavaScript because Emscripten hugely relies on absence of some kinds of UB such as "implicit" unaligned accesses, and such fix seemed to resolve this issue for me on "host with special alignment requirement".

2018-01-27 19:38 GMT+03:00 Stefan Weil <[email protected]>:

Quoted 15 lines

Am 27.01.2018 um 14:49 schrieb Anatoly Trosinenko:

Quoted 7 lines

Use ldl_he_p / ldq_he_p functions instead of a plain memory access through pointer.

---
 tcg/tci.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

A better alternative might be aligning the relevant data when generating the bytecode. See also my comment on alignment in tcg/tci/README.

Stefan

#5 lore raw

Re: [Qemu-devel] [PATCH 1/1] tci: eliminate UB due to unaligned reads

In reply to Anatoly Trosinenko

Ping.
Patchwork link: http://patchwork.ozlabs.org/patch/866732/
Patchew link: http://patchew.org/QEMU/20180127134908.24095-1-anatoly.
[email protected]/

The code in tcg/tci.c reads some data from TCI bytecode through pointer dereferencing. As far as I know unaligned reads in such a way are undefined behavior and compiling with -fsanitize=undefined enumerated them as such at run-time.

I have replaced such reads with invocations of ld{l,q}_he_p. A comment in include/qemu/bswap.h:310 suggests they should be properly translated by the compiler. I didn't added signed/unsigned casts since bswap.h does contain separate signed/unsigned versions for 16-bit integers but does not for 32- and 64-bit ones, so I supposed the developers of the bswap.h already arranged everything so integer promotions don't mess things up. I can add casts in case I'm not right about it.
http://lists.nongnu.org/archive/html/qemu-devel/2018-01/msg06516.html

2018-01-28 9:42 GMT+03:00 Anatoly Trosinenko <[email protected]>:

Quoted 42 lines

My patch is kind of trivial quick fix that just eliminates these unaligned reads and doesn't seem to require complicated testing supposing my code properly handles integer promotion (and hope it will not slow the interpreter down).

Aligning everything, on the other hand, can not only remove the UB but also speed things up, but if I get it right, requires O(opcode count) manual work and subsequent less trivial testing that every opcode's argument layout match on generation and interpretation side (errors should be significantly localized due to present assertion on operation size, though). So my patch may be considered as temporary solution.

In fact, I had to similarly make these unaligned reads explicit when porting QEMU to JavaScript because Emscripten hugely relies on absence of some kinds of UB such as "implicit" unaligned accesses, and such fix seemed to resolve this issue for me on "host with special alignment requirement".

2018-01-27 19:38 GMT+03:00 Stefan Weil <[email protected]>:

Quoted 9 lines

Am 27.01.2018 um 14:49 schrieb Anatoly Trosinenko:

Quoted 7 lines

Use ldl_he_p / ldq_he_p functions instead of a plain memory access through pointer.

---
 tcg/tci.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
Quoted 6 lines

A better alternative might be aligning the relevant data when generating the bytecode. See also my comment on alignment in tcg/tci/README.

Stefan

--
С уважением,
Анатолий Тросиненко
e-mail: [email protected]

Signature · 3 lines
С уважением,
Анатолий Тросиненко
e-mail: [email protected]
#6 lore raw

Re: [Qemu-devel] [PATCH 1/1] tci: eliminate UB due to unaligned reads

Quoted 11 lines · Anatoly Trosinenko

Ping.
Patchwork link: http://patchwork.ozlabs.org/patch/866732/
<http://patchwork.ozlabs.org/patch/866732/>
Patchew link:
http://patchew.org/QEMU/[email protected]/
<http://patchew.org/QEMU/[email protected]/>

The code in tcg/tci.c reads some data from TCI bytecode through pointer dereferencing. As far as I know unaligned reads in such a way are undefined behavior and compiling with -fsanitize=undefined enumerated them as such at run-time.

This is exactly one of the reasons why I have urged for TCI to be abandoned.

While your patch works, it is *enormously* inefficient for hosts that require it.

r~

#7 lore raw

Re: [Qemu-devel] [PATCH 1/1] tci: eliminate UB due to unaligned reads

Can rewriting TCI in such a way that every operation is aligned at 4- or even 8-byte boundary fix the situation or are there some more serious problems?

2018-03-03 16:57 GMT+03:00 Richard Henderson <[email protected]>:

Quoted 24 lines · Anatoly Trosinenko
Quoted 5 lines

Ping.
Patchwork link: http://patchwork.ozlabs.org/patch/866732/
<http://patchwork.ozlabs.org/patch/866732/>
Patchew link:
http://patchew.org/QEMU/20180127134908.24095-1-

[email protected]/

Quoted 1 line

<http://patchew.org/QEMU/20180127134908.24095-1-

[email protected]/>

Quoted 5 lines

The code in tcg/tci.c reads some data from TCI bytecode through pointer dereferencing. As far as I know unaligned reads in such a way are undefined behavior and compiling with -fsanitize=undefined enumerated them as such at run-time.

This is exactly one of the reasons why I have urged for TCI to be abandoned.

While your patch works, it is *enormously* inefficient for hosts that require it.

r~

Signature · 3 lines
С уважением,
Анатолий Тросиненко
e-mail: [email protected]
#8 lore raw

Re: [Qemu-devel] [PATCH 1/1] tci: eliminate UB due to unaligned reads

Quoted 2 lines · Anatoly Trosinenko

Can rewriting TCI in such a way that every operation is aligned at 4- or even 8-byte boundary fix the situation or are there some more serious problems?

With the current TCI, there are also problems with calls to helper functions. The only portable way to do this is to use a library such as libffi.

I once rewrote TCI completely in order to address both problems, but that only brought questions as to why TCI is useful at all.

So. Why do you want to use TCI instead of a native TCG backend?

r~

#9 lore raw

Re: [Qemu-devel] [PATCH 1/1] tci: eliminate UB due to unaligned reads

Quoted 1 line

So. Why do you want to use TCI instead of a native TCG backend?

Frankly speaking, personally I just have a strange experiment on porting QEMU to JavaScript. :) I used the TCI bytecode as some intermediate patchable form for rarely executing BBs and for (re)generating asm.js from it when required. I used a Python script to generate wrappers with exactly 10 arguments around helper functions. In fact, it may be worth for me to create WebAssembly TCG backend and interpret **that** bytecode if required.

TCI may still be useful for someone else running QEMU on unsupported host, though.

2018-03-03 17:13 GMT+03:00 Richard Henderson <[email protected]>:

Quoted 10 lines · Anatoly Trosinenko
Quoted 1 line

Can rewriting TCI in such a way that every operation is aligned at 4- or

even

Quoted 1 line

8-byte boundary fix the situation or are there some more serious

problems?

With the current TCI, there are also problems with calls to helper functions.
The only portable way to do this is to use a library such as libffi.

Quoted 9 lines

I once rewrote TCI completely in order to address both problems, but that only
brought questions as to why TCI is useful at all.

So. Why do you want to use TCI instead of a native TCG backend?

r~

--
Best regards,
Anatoly

#10 lore raw

Re: [Qemu-devel] [PATCH 1/1] tci: eliminate UB due to unaligned reads

In reply to Anatoly Trosinenko

Am 03.03.2018 um 15:07 schrieb Anatoly Trosinenko:

Quoted 3 lines

Can rewriting TCI in such a way that every operation is aligned at 4- or even 8-byte boundary fix the situation or are there some more serious problems?

That's my preferred solution. Are there cases which would require 8-byte alignment?

Richard, the discussion about non-portable calls to helper functions is not new, but I still have no test case which fails. Do you think of a special case (architecture, helper function)?

Stefan

#11 lore raw

Re: [Qemu-devel] [PATCH 1/1] tci: eliminate UB due to unaligned reads

2018-03-03 18:41 GMT+03:00 Stefan Weil <[email protected]>:

Quoted 8 lines

Am 03.03.2018 um 15:07 schrieb Anatoly Trosinenko:

Quoted 3 lines

Can rewriting TCI in such a way that every operation is aligned at 4- or even 8-byte boundary fix the situation or are there some more serious problems?

That's my preferred solution. Are there cases which would require 8-byte alignment?

And what if create some function like

uint8_t *align_and_increment(uint8_t **ptr, int pow2) {
size_t size = 1 << pow2;
uint8_t *result = (uint8_t*)((((uintptr)*ptr) + size - 1) & ~(size - 1)); *ptr = result + size;
return result;
}

and rewrite get / put functions like this:

static uint32_t tci_read_i32(uint8_t **tb_ptr)

{
    uint32_t value = *(uint32_t *)align_and_increment(tb_ptr, 2);
    return value;
}

On one hand, it involves some slightly obscure pointer calculations (just in one place), on the other hand, no modifications will probably be required for TCI TCG backend or interpreter loop code (they can still be useful for **optimizations** of bytecode size, but it should just work as is).

--
Best regards,
Anatoly

#12 lore raw

Re: [Qemu-devel] [PATCH 1/1] tci: eliminate UB due to unaligned reads

In reply to Stefan Weil
Quoted 3 lines · Stefan Weil

Richard, the discussion about non-portable calls to helper functions is not new, but I still have no test case which fails. Do you think of a special case (architecture, helper function)?

The two that come to mind immediately are:

i386 with a compiler defaulting to stdcall (which I admit is unlikely; it also would not work for TCG without modification).

m68k (pointers passed in address registers; integrals passed in data registers).

r~