hw/misc/bcm2835_thermal: Handle invalid address accesses gracefully
Subsystems (2)
PATCH 1/1 hw/misc/bcm2835_thermal: Handle invalid address accesses gracefully
This commit handles invalid address accesses gracefully in both read and write functions. Instead of asserting and aborting, it logs an error message and returns a neutral value for read operations and does nothing for write operations.
Error log:
ERROR:hw/misc/bcm2835_thermal.c:55:bcm2835_thermal_read: code should not be reached Bail out! ERROR:hw/misc/bcm2835_thermal.c:55:bcm2835_thermal_read: code should not be reached Aborted
Reproducer:
cat << EOF | qemu-system-aarch64 -display \
none -machine accel=qtest, -m 512M -machine raspi3b -m 1G -qtest stdio readw 0x3f212003
EOF
--- hw/misc/bcm2835_thermal.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
hw/misc/bcm2835_thermal.c +8 -4
@@ -51,8 +51,10 @@ static uint64_t bcm2835_thermal_read(void *opaque, hwaddr addr, unsigned size) val =FIELD_DP32 (bcm2835_thermal_temp2adc (25 ),STAT ,VALID , true);break ;default : -/* MemoryRegionOps are aligned, so this can not happen. */ -g_assert_not_reached (); +qemu_log_mask (LOG_GUEST_ERROR , +"bcm2835_thermal_read: invalid address 0x%" + HWADDR_PRIx"\n" , addr); + val =0 ; }return val; }
@@ -72,8 +74,10 @@ static void bcm2835_thermal_write(void *opaque, hwaddr addr, __func__, value, addr);break ;default : -/* MemoryRegionOps are aligned, so this can not happen. */ -g_assert_not_reached (); +qemu_log_mask (LOG_GUEST_ERROR , +"bcm2835_thermal_write: invalid address 0x%" + HWADDR_PRIx"\n" , addr); +break ; } }
Signature · 1 line
2.34.1
Hi, zheyu
Quoted 55 lines
-----Original Message-----
From: [email protected] <[email protected]> On Behalf Of Zheyu Ma
Sent: Sunday, June 30, 2024 11:14 PM
To: Peter Maydell <[email protected]>; Philippe Mathieu-Daudé <[email protected]>
Cc: Zheyu Ma <[email protected]>; [email protected]; [email protected]
Subject: [PATCH] hw/misc/bcm2835_thermal: Handle invalid address accesses gracefullyThis commit handles invalid address accesses gracefully in both read and write functions. Instead of asserting and aborting, it logs an error message and returns a neutral value for read operations and does nothing for write operations.
Error log:
ERROR:hw/misc/bcm2835_thermal.c:55:bcm2835_thermal_read: code should not be reached
Bail out! ERROR:hw/misc/bcm2835_thermal.c:55:bcm2835_thermal_read: code should not be reached
AbortedReproducer:
cat << EOF | qemu-system-aarch64 -display \
none -machine accel=qtest, -m 512M -machine raspi3b -m 1G -qtest stdio readw0x3f212003
EOFSigned-off-by Zheyu Ma <[email protected]>--- hw/misc/bcm2835_thermal.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)hw/misc/bcm2835_thermal.c +4
-2@@ -51,8 +51,10 @@ static uint64_t bcm2835_thermal_read(void *opaque, hwaddr addr, unsigned size) val =FIELD_DP32 (bcm2835_thermal_temp2adc (25 ),STAT ,VALID , true);break ;default : -/* MemoryRegionOps are aligned, so this can not happen. */ -g_assert_not_reached (); +qemu_log_mask (LOG_GUEST_ERROR , +"bcm2835_thermal_read: invalid address 0x%" + HWADDR_PRIx"\n" , addr); + val =0 ; }return val;}
hw/misc/bcm2835_thermal.c +0
-0@@ -72,8 +74,10 @@ static void bcm2835_thermal_write(void *opaque, hwaddr addr, __func__, value, addr);
hw/misc/bcm2835_thermal.c static void bcm2835_thermal_write(void *opaque, hwaddr · line 83
Quoted 10 lines
hw/misc/bcm2835_thermal.c +4
-2break ;default : -/* MemoryRegionOps are aligned, so this can not happen. */ -g_assert_not_reached (); +qemu_log_mask (LOG_GUEST_ERROR , +"bcm2835_thermal_write: invalid address 0x%" + HWADDR_PRIx"\n" , addr); +break ; } }
the default branch will never be reached in normal access, so I think this modification is not needed.
Quoted 4 lines
--
2.34.1
Hi Zheyu,
Quoted 9 lines · Zheyu Ma
This commit handles invalid address accesses gracefully in both read and write functions. Instead of asserting and aborting, it logs an error message and returns a neutral value for read operations and does nothing for write operations.
Error log:
ERROR:hw/misc/bcm2835_thermal.c:55:bcm2835_thermal_read: code should not be reached Bail out! ERROR:hw/misc/bcm2835_thermal.c:55:bcm2835_thermal_read: code should not be reached Aborted
Quoted 4 lines
Reproducer:
cat << EOF | qemu-system-aarch64 -display \
none -machine accel=qtest, -m 512M -machine raspi3b -m 1G -qtest stdio readw0x3f212003
Thanks for this very interesting bug report (and reproducer).
Quoted 12 lines
EOF
Signed-off-by Zheyu Ma <[email protected]>--- hw/misc/bcm2835_thermal.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)hw/misc/bcm2835_thermal.c +0
-0
hw/misc/bcm2835_thermal.c static uint64_t bcm2835_thermal_read(void *opaque, hwaddr addr, unsigned size) · line 55
Quoted 5 lines
hw/misc/bcm2835_thermal.c +0
-2@@ -51,8 +51,10 @@ static uint64_t bcm2835_thermal_read(void *opaque, hwaddr addr, unsigned size) val =FIELD_DP32 (bcm2835_thermal_temp2adc (25 ),STAT ,VALID , true);break ;default : -/* MemoryRegionOps are aligned, so this can not happen. */ -g_assert_not_reached ();
Like Xingtao Yao mentioned, I believe the current code is correct and shouldn't be reached.
Why is it reached? You might have uncovered a core memory bug.
Likely around access_with_adjusted_size() in system/memory.c.
I'll keep investigating, but so far it reminds me a previous patch from Andrew, but it isn't the fix:
https://patchwork.ozlabs.org/project/qemu-devel/patch/[email protected]/
Quoted 7 lines
hw/misc/bcm2835_thermal.c +4
-0+qemu_log_mask (LOG_GUEST_ERROR , +"bcm2835_thermal_read: invalid address 0x%" + HWADDR_PRIx"\n" , addr); + val =0 ; }return val; }
Quoted 41 lines · Philippe Mathieu-Daudé
Hi Zheyu,
Quoted 9 lines · Zheyu Ma
This commit handles invalid address accesses gracefully in both read and write functions. Instead of asserting and aborting, it logs an error message and returns a neutral value for read operations and does nothing for write operations.
Error log:
ERROR:hw/misc/bcm2835_thermal.c:55:bcm2835_thermal_read: code should not be reached Bail out! ERROR:hw/misc/bcm2835_thermal.c:55:bcm2835_thermal_read: code should not be reached AbortedQuoted 4 lines
Reproducer:
cat << EOF | qemu-system-aarch64 -display \
none -machine accel=qtest, -m 512M -machine raspi3b -m 1G -qtest stdio readw0x3f212003Thanks for this very interesting bug report (and reproducer).
Quoted 12 lines
EOF
Signed-off-by Zheyu Ma <[email protected]>--- hw/misc/bcm2835_thermal.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)hw/misc/bcm2835_thermal.c +0
-0hw/misc/bcm2835_thermal.c static uint64_t bcm2835_thermal_read(void *opaque, hwaddr addr, unsigned size) · line 55
Quoted 5 lines
hw/misc/bcm2835_thermal.c +0
-2@@ -51,8 +51,10 @@ static uint64_t bcm2835_thermal_read(void *opaque, hwaddr addr, unsigned size) val =FIELD_DP32 (bcm2835_thermal_temp2adc (25 ),STAT ,VALID , true);break ;default : -/* MemoryRegionOps are aligned, so this can not happen. */ -g_assert_not_reached ();Like Xingtao Yao mentioned, I believe the current code is correct and shouldn't be reached.
Quoted 1 line
Why is it reached? You might have uncovered a core memory bug.
I think we get here because the bcm2835_thermal_ops MemoryRegionOps sets impl.max_access_size and valid.max_access_size to 4, but it leaves impl.min_access_size and valid.min_access_size unset, which means "default to 1". So the memory system is presented with an access of size 2 at offset 3, and it tries to synthesize it as a pair of byte accesses at offsets 3 and 4; but the offset 3 trips our assert above. So I think the fix is:
@@ -80,8 +80,10 @@ static void bcm2835_thermal_write(void *opaque, hwaddr addr,
static const MemoryRegionOps bcm2835_thermal_ops = {
.read = bcm2835_thermal_read,
.write = bcm2835_thermal_write,
+ .impl.min_access_size = 4,
.impl.max_access_size = 4,
.valid.min_access_size = 4,+ .valid.max_access_size = 4,
.endianness = DEVICE_NATIVE_ENDIAN,
};
The .valid.max_access_size change is because otherwise we fall over in a different way: the memory system tries to synthesize a read using a 4-byte access at offset 3 even though the device doesn't say it permits unaligned accesses. This part does seem like a bug in the access_with_adjusted_size code. If we want to look at that we should probably start with this patch series from Tomoyuki Hirose from earlier this year:
https://lore.kernel.org/qemu-devel/[email protected]/
which attempted to improve that code.
thanks
-- PMM