softmmu/memory: use memcpy for multi-byte accesses
PATCH 1/1 softmmu/memory: use memcpy for multi-byte accesses
Avoids unaligned pointer issues.
--- system/memory.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
system/memory.c +8 -8
@@ -1343,16 +1343,16 @@ static uint64_t memory_region_ram_device_read(void *opaque,switch (size) {case 1 : - data = *(uint8_t *)(mr->ram_block->host + addr); +memcpy (&data, mr->ram_block->host + addr,sizeof (uint8_t ));break ;case 2 : - data = *(uint16_t *)(mr->ram_block->host + addr); +memcpy (&data, mr->ram_block->host + addr,sizeof (uint16_t ));break ;case 4 : - data = *(uint32_t *)(mr->ram_block->host + addr); +memcpy (&data, mr->ram_block->host + addr,sizeof (uint32_t ));break ;case 8 : - data = *(uint64_t *)(mr->ram_block->host + addr); +memcpy (&data, mr->ram_block->host + addr,sizeof (uint64_t ));break ; }
@@ -1370,16 +1370,16 @@ static void memory_region_ram_device_write(void *opaque, hwaddr addr,switch (size) {case 1 : - *(uint8_t *)(mr->ram_block->host + addr) = (uint8_t )data; +memcpy (mr->ram_block->host + addr, &data,sizeof (uint8_t ));break ;case 2 : - *(uint16_t *)(mr->ram_block->host + addr) = (uint16_t )data; +memcpy (mr->ram_block->host + addr, &data,sizeof (uint16_t ));break ;case 4 : - *(uint32_t *)(mr->ram_block->host + addr) = (uint32_t )data; +memcpy (mr->ram_block->host + addr, &data,sizeof (uint32_t ));break ;case 8 : - *(uint64_t *)(mr->ram_block->host + addr) = data; +memcpy (mr->ram_block->host + addr, &data,sizeof (uint64_t ));break ; } }
Signature · 1 line
2.43.0.rc0.421.g78406f8d94-goog
Quoted 14 lines · Patrick Venture
Avoids unaligned pointer issues.
Reviewed-by Chris Rauer <[email protected]>Reviewed-by Peter Foley <[email protected]>Signed-off-by Patrick Venture <[email protected]>--- system/memory.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)system/memory.c +0
-0
system/memory.c static uint64_t memory_region_ram_device_read(void *opaque, · line 1346
Quoted 5 lines
system/memory.c +1
-1@@ -1343,16 +1343,16 @@ static uint64_t memory_region_ram_device_read(void *opaque,switch (size) {case 1 : - data = *(uint8_t *)(mr->ram_block->host + addr); +memcpy (&data, mr->ram_block->host + addr,sizeof (uint8_t ));
This is incorrect, especially for big-endian hosts.
You want to use "qemu/bswap.h", ld*_he_p(), st*_he_p().
r~
Quoted 23 lines · Richard Henderson
system/memory.c static uint64_t
Quoted 14 lines · Patrick Venture
Avoids unaligned pointer issues.
Reviewed-by Chris Rauer <[email protected]>Reviewed-by Peter Foley <[email protected]>Signed-off-by Patrick Venture <[email protected]>--- system/memory.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)system/memory.c +0
-0@@ -1343,16 +1343,16 @@ static uint64_tmemory_region_ram_device_read(void *opaque,
system/memory.c static uint64_t memory_region_ram_device_read(void *opaque, · line 1346
Quoted 5 lines
system/memory.c +1
-1switch (size) {case 1 : - data = *(uint8_t *)(mr->ram_block->host + addr); +memcpy (&data, mr->ram_block->host + addr,sizeof (uint8_t ));
Quoted 4 lines
This is incorrect, especially for big-endian hosts.
You want to use "
qemu/bswap.h", ld*_he_p(), st*_he_p().
Thanks, I'll take a look.
Quoted 4 lines
r~
Quoted 25 lines · Richard Henderson
Quoted 14 lines · Patrick Venture
Avoids unaligned pointer issues.
Reviewed-by Chris Rauer <[email protected]>Reviewed-by Peter Foley <[email protected]>Signed-off-by Patrick Venture <[email protected]>--- system/memory.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)system/memory.c +0
-0system/memory.c static uint64_t memory_region_ram_device_read(void *opaque, · line 1346
Quoted 5 lines
system/memory.c +1
-1@@ -1343,16 +1343,16 @@ static uint64_t memory_region_ram_device_read(void *opaque,switch (size) {case 1 : - data = *(uint8_t *)(mr->ram_block->host + addr); +memcpy (&data, mr->ram_block->host + addr,sizeof (uint8_t ));This is incorrect, especially for big-endian hosts.
Quoted 1 line
You want to use "
qemu/bswap.h", ld*_he_p(), st*_he_p().
More specifically, we have a ldn_he_p() and stn_he_p() that take the size in bytes of the data to read, so we should be able to replace the switch-on-size in these functions with a single call to the appropriate one of those.
thanks
-- PMM
Quoted 2 lines · Patrick Venture
Avoids unaligned pointer issues.
It would be nice to be more specific in the commit message here, by describing what kind of guest behaviour or machine config runs into this problem, and whether this happens in a situation users are likely to run into. If the latter, we should consider tagging the commit with "Cc: [email protected]" to have it backported to the stable release branches.
thanks
-- PMM
Quoted 26 lines · Peter Maydell
Quoted 11 lines · Richard Henderson
Quoted 9 lines · Patrick Venture
Avoids unaligned pointer issues.
Reviewed-by Chris Rauer <[email protected]>Reviewed-by Peter Foley <[email protected]>Signed-off-by Patrick Venture <[email protected]>--- system/memory.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)Quoted 5 lines
system/memory.c static uint64_t
Quoted 5 lines
system/memory.c +0
-0@@ -1343,16 +1343,16 @@ static uint64_tmemory_region_ram_device_read(void *opaque,
Quoted 7 lines
Quoted 5 lines
system/memory.c +1
-1switch (size) {case 1 : - data = *(uint8_t *)(mr->ram_block->host + addr); +memcpy (&data, mr->ram_block->host + addr,sizeof (uint8_t ));
Quoted 9 lines
Quoted 3 lines
This is incorrect, especially for big-endian hosts.
You want to use "
qemu/bswap.h", ld*_he_p(), st*_he_p().More specifically, we have a
ldn_he_p()andstn_he_p()that take the size in bytes of the data to read, so we should be able to replace the switch-on-size in these functions with a single call to the appropriate one of those.
Thanks!
Quoted 4 lines
thanks
-- PMM
Quoted 11 lines · Peter Maydell
Quoted 2 lines · Patrick Venture
Avoids unaligned pointer issues.
It would be nice to be more specific in the commit message here, by describing what kind of guest behaviour or machine config runs into this problem, and whether this happens in a situation users are likely to run into. If the latter, we should consider tagging the commit with "Cc: [email protected]" to have it backported to the stable release branches.
Thanks! I'll update the commit message with v2. We were seeing this in our infrastructure with unaligned accesses using the pointer dereference as there are no guarantees on alignment of the incoming values.
Quoted 4 lines
thanks
-- PMM
Quoted 18 lines · Patrick Venture
On Wed, Nov 15, 2023 at 2:35 AM Peter Maydell <[email protected] <mailto:[email protected]>> wrote:
On Tue, 14 Nov 2023 at 20:55, Patrick Venture <[email protected] <mailto:[email protected]>> wrote:
> Avoids unaligned pointer issues.
>It would be nice to be more specific in the commit message here, by describing what kind of guest behaviour or machine config runs into this problem, and whether this happens in a situation users are likely to run into. If the latter, we should consider tagging the commit with "Cc: [email protected] <mailto:[email protected]>" to have it backported to the
stable release branches.
Quoted 1 line
Thanks! I'll update the commit message with v2. We were seeing this in our infrastructure with unaligned accesses using the pointer dereference as there are no guarantees on alignment of the incoming values.
Which host cpu, for reference? There aren't many that generate unaligned traps these days...
r~
Quoted 30 lines · Patrick Venture
Quoted 25 lines · Richard Henderson
Quoted 5 lines · Patrick Venture
On Wed, Nov 15, 2023 at 2:35 AM Peter Maydell <[email protected] <mailto:[email protected]>> wrote:
Quoted 7 lines
On Tue, 14 Nov 2023 at 20:55, Patrick Venture <[email protected] <mailto:[email protected]>> wrote:
> Avoids unaligned pointer issues.
>It would be nice to be more specific in the commit message here, by describing what kind of guest behaviour or machine config runs into
this
Quoted 3 lines
problem, and whether this happens in a situation users are likely to run into. If the latter, we should consider tagging the commit with "Cc: [email protected] <mailto:[email protected]>"
to have it
Quoted 5 lines
backported to the
stable release branches.Thanks! I'll update the commit message with v2. We were seeing this in
our
Quoted 1 line
infrastructure with unaligned accesses using the pointer dereference as
Quoted 9 lines
Quoted 7 lines
there are no
Quoted 1 line
guarantees on alignment of the incoming values.
Which host cpu, for reference? There aren't many that generate unaligned traps these days...
Here's the sanitizer log/qemu log, the host-cpu was an amd64.
AMD ROME
Quoted 62 lines
qemu-kvm-system-x86_64: warning: host doesn't support requested feature:
CPUID.01H:ECX.pcid [bit 17]
qemu-kvm-system-x86_64: warning: host doesn't support requested feature:
CPUID.07H:EBX.erms [bit 9]
qemu-kvm-system-x86_64: warning: host doesn't support requested feature:
CPUID.07H:EBX.invpcid [bit 10]
qemu-kvm-system-x86_64: warning: host doesn't support requested feature:
CPUID.01H:ECX.pcid [bit 17]
qemu-kvm-system-x86_64: warning: host doesn't support requested feature:
CPUID.07H:EBX.erms [bit 9]
qemu-kvm-system-x86_64: warning: host doesn't support requested feature:
CPUID.07H:EBX.invpcid [bit 10]third_party/qemu/softmmu/memory.c:1341:16: runtime error: load of misaligned address0x52500020b10dfor type 'uint32_t' (aka 'unsigned int'), which requires 4 byte alignment0x52500020b10d: note: pointer points here
ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab^ #0 0x55b34f8ef9d8 in memory_region_ram_device_read third_party/qemu/softmmu/memory.c:1341:16 #1 0x55b34f8ee8a8 in memory_region_read_accessor third_party/qemu/softmmu/memory.c:441:11 #2 0x55b34f8e06db in access_with_adjusted_size third_party/qemu/softmmu/memory.c:569:18 #3 0x55b34f8dfcb4 in memory_region_dispatch_read1 third_party/qemu/softmmu/memory.c #4 0x55b34f8dfcb4 in memory_region_dispatch_read third_party/qemu/softmmu/memory.c:1476:9 #5 0x55b34f8fa8b0 in flatview_read_continue third_party/qemu/softmmu/physmem.c:2744:23 #6 0x55b34f8fb0db in flatview_read third_party/qemu/softmmu/physmem.c:2786:12 #7 0x55b34f8faefa in address_space_read_full third_party/qemu/softmmu/physmem.c:2799:18 #8 0x55b34f8fb5b4 in address_space_rw third_party/qemu/softmmu/physmem.c:2827:16 #9 0x55b34f71eab5 in kvm_cpu_exec third_party/qemu/accel/kvm/kvm-all.c:3062:13 #10 0x55b34f7172e3 in kvm_vcpu_thread_fn third_party/qemu/accel/kvm/kvm-accel-ops.c:51:17 #11 0x55b350467044 in qemu_thread_start third_party/qemu/util/qemu-thread-posix.c:541:9 #12 0x55b34f6dba10 in asan_thread_start(void*) third_party/llvm/llvm-project/compiler-rt/lib/asan/asan_interceptors.cpp:234:28 #13 0x7f5e1c81a7d8 in start_thread (/usr/grte/v5/lib64/libpthread.so.0+0xb7d8) (BuildId: 3ccc1600b9140e48da03ed16e0210354) #14 0x7f5e1c77169e in clone (/usr/grte/v5/lib64/libc.so.6+0x13969e)(BuildId:
280088eab084c30a3992a9bce5c35b44)SUMMARY: UndefinedBehaviorSanitizer: misaligned-pointer-use
third_party/qemu/softmmu/memory.c:1341:16inQuoted 4 lines
r~
Quoted 22 lines · Richard Henderson
Quoted 5 lines · Patrick Venture
On Wed, Nov 15, 2023 at 2:35 AM Peter Maydell <[email protected] <mailto:[email protected]>> wrote:
Quoted 7 lines
On Tue, 14 Nov 2023 at 20:55, Patrick Venture <[email protected] <mailto:[email protected]>> wrote:
> Avoids unaligned pointer issues.
>It would be nice to be more specific in the commit message here, by describing what kind of guest behaviour or machine config runs into
this
Quoted 3 lines
problem, and whether this happens in a situation users are likely to run into. If the latter, we should consider tagging the commit with "Cc: [email protected] <mailto:[email protected]>"
to have it
Quoted 4 lines
backported to the
stable release branches.
Quoted 10 lines
Quoted 1 line
Thanks! I'll update the commit message with v2. We were seeing this in
our
Quoted 1 line
infrastructure with unaligned accesses using the pointer dereference as
there are no
Quoted 1 line
guarantees on alignment of the incoming values.
Which host cpu, for reference? There aren't many that generate unaligned traps these days...
Here's the sanitizer log/qemu log, the host-cpu was an amd64.
qemu-kvm-system-x86_64: warning: host doesn't support requested feature:
CPUID.01H:ECX.pcid [bit 17]
qemu-kvm-system-x86_64: warning: host doesn't support requested feature:
CPUID.07H:EBX.erms [bit 9]
qemu-kvm-system-x86_64: warning: host doesn't support requested feature:
CPUID.07H:EBX.invpcid [bit 10]
qemu-kvm-system-x86_64: warning: host doesn't support requested feature:
CPUID.01H:ECX.pcid [bit 17]
qemu-kvm-system-x86_64: warning: host doesn't support requested feature:
CPUID.07H:EBX.erms [bit 9]
qemu-kvm-system-x86_64: warning: host doesn't support requested feature:
CPUID.07H:EBX.invpcid [bit 10] third_party/qemu/softmmu/memory.c:1341:16: runtime error: load of misaligned address 0x52500020b10d for type 'uint32_t' (aka 'unsigned int'), which requires 4 byte alignment0x52500020b10d: note: pointer points here
ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab
ab ab ab ab ab ab ab ab
^
#0 0x55b34f8ef9d8 in memory_region_ram_device_read
third_party/qemu/softmmu/memory.c:1341:16
#1 0x55b34f8ee8a8 in memory_region_read_accessor
third_party/qemu/softmmu/memory.c:441:11
#2 0x55b34f8e06db in access_with_adjusted_size
third_party/qemu/softmmu/memory.c:569:18
#3 0x55b34f8dfcb4 in memory_region_dispatch_read1
third_party/qemu/softmmu/memory.c
#4 0x55b34f8dfcb4 in memory_region_dispatch_read
third_party/qemu/softmmu/memory.c:1476:9
#5 0x55b34f8fa8b0 in flatview_read_continue
third_party/qemu/softmmu/physmem.c:2744:23
#6 0x55b34f8fb0db in flatview_read
third_party/qemu/softmmu/physmem.c:2786:12
#7 0x55b34f8faefa in address_space_read_full
third_party/qemu/softmmu/physmem.c:2799:18
#8 0x55b34f8fb5b4 in address_space_rw
third_party/qemu/softmmu/physmem.c:2827:16
#9 0x55b34f71eab5 in kvm_cpu_exec
third_party/qemu/accel/kvm/kvm-all.c:3062:13
#10 0x55b34f7172e3 in kvm_vcpu_thread_fn
third_party/qemu/accel/kvm/kvm-accel-ops.c:51:17
#11 0x55b350467044 in qemu_thread_start
third_party/qemu/util/qemu-thread-posix.c:541:9
#12 0x55b34f6dba10 in asan_thread_start(void*)
third_party/llvm/llvm-project/compiler-rt/lib/asan/asan_interceptors.cpp:234:28
#13 0x7f5e1c81a7d8 in start_thread
(/usr/grte/v5/lib64/libpthread.so.0+0xb7d8) (BuildId:
3ccc1600b9140e48da03ed16e0210354)
#14 0x7f5e1c77169e in clone (/usr/grte/v5/lib64/libc.so.6+0x13969e)(BuildId: 280088eab084c30a3992a9bce5c35b44)
SUMMARY: UndefinedBehaviorSanitizer: misaligned-pointer-use third_party/qemu/softmmu/memory.c:1341:16 in
Quoted 4 lines
r~
Quoted 32 lines · Patrick Venture
Quoted 21 lines · Richard Henderson
Quoted 19 lines · Patrick Venture
On Wed, Nov 15, 2023 at 2:35 AM Peter Maydell <[email protected] <mailto:[email protected]>> wrote:
On Tue, 14 Nov 2023 at 20:55, Patrick Venture <[email protected] <mailto:[email protected]>> wrote:
> Avoids unaligned pointer issues.
>It would be nice to be more specific in the commit message here, by describing what kind of guest behaviour or machine config runs into this problem, and whether this happens in a situation users are likely to run into. If the latter, we should consider tagging the commit with "Cc: [email protected] <mailto:[email protected]>" to have it backported to the
stable release branches.Quoted 6 lines
Quoted 3 lines
Thanks! I'll update the commit message with v2. We were seeing this in our infrastructure with unaligned accesses using the pointer dereference as there are no guarantees on alignment of the incoming values.
Which host cpu, for reference? There aren't many that generate unaligned traps these days...
Quoted 7 lines
Here's the sanitizer log/qemu log, the host-cpu was an amd64.
third_party/qemu/softmmu/memory.c:1341:16: runtime error: load of misaligned address0x52500020b10dfor type 'uint32_t' (aka 'unsigned int'), which requires 4 byte alignment0x52500020b10d: note: pointer points here
ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab^SUMMARY: UndefinedBehaviorSanitizer: misaligned-pointer-use
third_party/qemu/softmmu/memory.c:1341:16in
Ah, right, so the clang/gcc undefined-behaviour sanitizers rather than the actual host hardware barfing. (We definitely want to fix these regardless.)
thanks
-- PMM