ARM semihosting HEAPINFO results wrote to wrong address
Public bug reported:
This affects latest development branch of QEMU.
According to the ARM spec of the HEAPINFO semihosting call:
https://developer.arm.com/documentation/100863/0300/Semihosting-
operations/SYS-HEAPINFO--0x16-?lang=en
Quoted 1 line
the PARAMETER REGISTER contains the address of a pointer to a four-
field data block.
However, QEMU treated the PARAMETER REGISTER as pointing to a four-field data block directly.
Here is a simple program that can demonstrate this problem:
https://github.com/iNvEr7/qemu-learn/tree/newlib-bug/semihosting-newlib
This code links with newlib with semihosting mode, which will call the HEAPINFO SVC during crt0 routine. When running in QEMU (make run), it may crash the program either because of invalid write or memory curruption, depending on the compiled program structure.
Also refer to my discussion with newlib folks:
https://sourceware.org/pipermail/newlib/2021/018260.html
** Affects: qemu
Importance: Undecided
Status: New
** Tags: semihosting
Signature · 17 lines
You received this bug notification because you are a member of qemu- devel-ml, which is subscribed to QEMU. https://bugs.launchpad.net/bugs/1915925 Title: ARM semihosting HEAPINFO results wrote to wrong address Status in QEMU: New Bug description: This affects latest development branch of QEMU. According to the ARM spec of the HEAPINFO semihosting call: https://developer.arm.com/documentation/100863/0300/Semihosting- operations/SYS-HEAPINFO--0x16-?lang=en
Quoted 1 line
the PARAMETER REGISTER contains the address of a pointer to a four-
field data block.
However, QEMU treated the PARAMETER REGISTER as pointing to a four- field data block directly.
Here is a simple program that can demonstrate this problem:
https://github.com/iNvEr7/qemu-learn/tree/newlib-bug/semihosting-
newlib
This code links with newlib with semihosting mode, which will call the HEAPINFO SVC during crt0 routine. When running in QEMU (make run), it may crash the program either because of invalid write or memory curruption, depending on the compiled program structure.
Also refer to my discussion with newlib folks:
https://sourceware.org/pipermail/newlib/2021/018260.html
To manage notifications about this bug go to:
https://bugs.launchpad.net/qemu/+bug/1915925/+subscriptions
Broken in commit 3c37cfe0b1e8a49, I think.
Signature · 17 lines
You received this bug notification because you are a member of qemu- devel-ml, which is subscribed to QEMU. https://bugs.launchpad.net/bugs/1915925 Title: ARM semihosting HEAPINFO results wrote to wrong address Status in QEMU: New Bug description: This affects latest development branch of QEMU. According to the ARM spec of the HEAPINFO semihosting call: https://developer.arm.com/documentation/100863/0300/Semihosting- operations/SYS-HEAPINFO--0x16-?lang=en
Quoted 1 line
the PARAMETER REGISTER contains the address of a pointer to a four-
field data block.
However, QEMU treated the PARAMETER REGISTER as pointing to a four- field data block directly.
Here is a simple program that can demonstrate this problem:
https://github.com/iNvEr7/qemu-learn/tree/newlib-bug/semihosting-
newlib
This code links with newlib with semihosting mode, which will call the HEAPINFO SVC during crt0 routine. When running in QEMU (make run), it may crash the program either because of invalid write or memory curruption, depending on the compiled program structure.
Also refer to my discussion with newlib folks:
https://sourceware.org/pipermail/newlib/2021/018260.html
To manage notifications about this bug go to:
https://bugs.launchpad.net/qemu/+bug/1915925/+subscriptions
** Changed in: qemu
Status: New => Confirmed
Signature · 17 lines
You received this bug notification because you are a member of qemu- devel-ml, which is subscribed to QEMU. https://bugs.launchpad.net/bugs/1915925 Title: ARM semihosting HEAPINFO results wrote to wrong address Status in QEMU: Confirmed Bug description: This affects latest development branch of QEMU. According to the ARM spec of the HEAPINFO semihosting call: https://developer.arm.com/documentation/100863/0300/Semihosting- operations/SYS-HEAPINFO--0x16-?lang=en
Quoted 1 line
the PARAMETER REGISTER contains the address of a pointer to a four-
field data block.
However, QEMU treated the PARAMETER REGISTER as pointing to a four- field data block directly.
Here is a simple program that can demonstrate this problem:
https://github.com/iNvEr7/qemu-learn/tree/newlib-bug/semihosting-
newlib
This code links with newlib with semihosting mode, which will call the HEAPINFO SVC during crt0 routine. When running in QEMU (make run), it may crash the program either because of invalid write or memory curruption, depending on the compiled program structure.
Also refer to my discussion with newlib folks:
https://sourceware.org/pipermail/newlib/2021/018260.html
To manage notifications about this bug go to:
https://bugs.launchpad.net/qemu/+bug/1915925/+subscriptions
** Tags added: testcase
Signature · 17 lines
You received this bug notification because you are a member of qemu- devel-ml, which is subscribed to QEMU. https://bugs.launchpad.net/bugs/1915925 Title: ARM semihosting HEAPINFO results wrote to wrong address Status in QEMU: Confirmed Bug description: This affects latest development branch of QEMU. According to the ARM spec of the HEAPINFO semihosting call: https://developer.arm.com/documentation/100863/0300/Semihosting- operations/SYS-HEAPINFO--0x16-?lang=en
Quoted 1 line
the PARAMETER REGISTER contains the address of a pointer to a four-
field data block.
However, QEMU treated the PARAMETER REGISTER as pointing to a four- field data block directly.
Here is a simple program that can demonstrate this problem:
https://github.com/iNvEr7/qemu-learn/tree/newlib-bug/semihosting-
newlib
This code links with newlib with semihosting mode, which will call the HEAPINFO SVC during crt0 routine. When running in QEMU (make run), it may crash the program either because of invalid write or memory curruption, depending on the compiled program structure.
Also refer to my discussion with newlib folks:
https://sourceware.org/pipermail/newlib/2021/018260.html
To manage notifications about this bug go to:
https://bugs.launchpad.net/qemu/+bug/1915925/+subscriptions
** Changed in: qemu
Assignee: (unassigned) => Alex Bennée (ajbennee)
Signature · 17 lines
You received this bug notification because you are a member of qemu- devel-ml, which is subscribed to QEMU. https://bugs.launchpad.net/bugs/1915925 Title: ARM semihosting HEAPINFO results wrote to wrong address Status in QEMU: Confirmed Bug description: This affects latest development branch of QEMU. According to the ARM spec of the HEAPINFO semihosting call: https://developer.arm.com/documentation/100863/0300/Semihosting- operations/SYS-HEAPINFO--0x16-?lang=en
Quoted 1 line
the PARAMETER REGISTER contains the address of a pointer to a four-
field data block.
However, QEMU treated the PARAMETER REGISTER as pointing to a four- field data block directly.
Here is a simple program that can demonstrate this problem:
https://github.com/iNvEr7/qemu-learn/tree/newlib-bug/semihosting-
newlib
This code links with newlib with semihosting mode, which will call the HEAPINFO SVC during crt0 routine. When running in QEMU (make run), it may crash the program either because of invalid write or memory curruption, depending on the compiled program structure.
Also refer to my discussion with newlib folks:
https://sourceware.org/pipermail/newlib/2021/018260.html
To manage notifications about this bug go to:
https://bugs.launchpad.net/qemu/+bug/1915925/+subscriptions
PATCH v3 3/4 semihosting/arm-compat-semi: don't use SET_ARG to report SYS_HEAPINFO
In reply to iNvEr7As per the spec:
the PARAMETER REGISTER contains the address of a pointer to a four-field data block.
So we need to follow arg0 and place the results of SYS_HEAPINFO there.
---
v3
- just revert the old behaviour
--- semihosting/arm-compat-semi.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
semihosting/arm-compat-semi.c +5 -1
@@ -1214,7 +1214,11 @@ target_ulong do_common_semihosting(CPUState *cs)for (i =0 ; i <ARRAY_SIZE (retvals); i++) {bool fail; - fail =SET_ARG (i, retvals[i]); +if (is_64bit_semihosting (env)) { + fail =put_user_u64 (retvals[i], arg0 + i *8 ); + }else { + fail =put_user_u32 (retvals[i], arg0 + i *4 ); + }if (fail) {/* Couldn't write back to argument block */
Signature · 20 lines
2.20.1 -- You received this bug notification because you are a member of qemu- devel-ml, which is subscribed to QEMU. https://bugs.launchpad.net/bugs/1915925 Title: ARM semihosting HEAPINFO results wrote to wrong address Status in QEMU: Confirmed Bug description: This affects latest development branch of QEMU. According to the ARM spec of the HEAPINFO semihosting call: https://developer.arm.com/documentation/100863/0300/Semihosting- operations/SYS-HEAPINFO--0x16-?lang=en
Quoted 1 line
the PARAMETER REGISTER contains the address of a pointer to a four-
field data block.
However, QEMU treated the PARAMETER REGISTER as pointing to a four- field data block directly.
Here is a simple program that can demonstrate this problem:
https://github.com/iNvEr7/qemu-learn/tree/newlib-bug/semihosting-
newlib
This code links with newlib with semihosting mode, which will call the HEAPINFO SVC during crt0 routine. When running in QEMU (make run), it may crash the program either because of invalid write or memory curruption, depending on the compiled program structure.
Also refer to my discussion with newlib folks:
https://sourceware.org/pipermail/newlib/2021/018260.html
To manage notifications about this bug go to:
https://bugs.launchpad.net/qemu/+bug/1915925/+subscriptions
** Changed in: qemu
Status: Confirmed => In Progress
Signature · 17 lines
You received this bug notification because you are a member of qemu- devel-ml, which is subscribed to QEMU. https://bugs.launchpad.net/bugs/1915925 Title: ARM semihosting HEAPINFO results wrote to wrong address Status in QEMU: In Progress Bug description: This affects latest development branch of QEMU. According to the ARM spec of the HEAPINFO semihosting call: https://developer.arm.com/documentation/100863/0300/Semihosting- operations/SYS-HEAPINFO--0x16-?lang=en
Quoted 1 line
the PARAMETER REGISTER contains the address of a pointer to a four-
field data block.
However, QEMU treated the PARAMETER REGISTER as pointing to a four- field data block directly.
Here is a simple program that can demonstrate this problem:
https://github.com/iNvEr7/qemu-learn/tree/newlib-bug/semihosting-
newlib
This code links with newlib with semihosting mode, which will call the HEAPINFO SVC during crt0 routine. When running in QEMU (make run), it may crash the program either because of invalid write or memory curruption, depending on the compiled program structure.
Also refer to my discussion with newlib folks:
https://sourceware.org/pipermail/newlib/2021/018260.html
To manage notifications about this bug go to:
https://bugs.launchpad.net/qemu/+bug/1915925/+subscriptions
This is now merged and while be available in the 6.0 release.
** Changed in: qemu
Status: In Progress => Fix Committed
Signature · 17 lines
You received this bug notification because you are a member of qemu- devel-ml, which is subscribed to QEMU. https://bugs.launchpad.net/bugs/1915925 Title: ARM semihosting HEAPINFO results wrote to wrong address Status in QEMU: Fix Committed Bug description: This affects latest development branch of QEMU. According to the ARM spec of the HEAPINFO semihosting call: https://developer.arm.com/documentation/100863/0300/Semihosting- operations/SYS-HEAPINFO--0x16-?lang=en
Quoted 1 line
the PARAMETER REGISTER contains the address of a pointer to a four-
field data block.
However, QEMU treated the PARAMETER REGISTER as pointing to a four- field data block directly.
Here is a simple program that can demonstrate this problem:
https://github.com/iNvEr7/qemu-learn/tree/newlib-bug/semihosting-
newlib
This code links with newlib with semihosting mode, which will call the HEAPINFO SVC during crt0 routine. When running in QEMU (make run), it may crash the program either because of invalid write or memory curruption, depending on the compiled program structure.
Also refer to my discussion with newlib folks:
https://sourceware.org/pipermail/newlib/2021/018260.html
To manage notifications about this bug go to:
https://bugs.launchpad.net/qemu/+bug/1915925/+subscriptions
** Changed in: qemu
Status: Fix Committed => Fix Released
Signature · 17 lines
You received this bug notification because you are a member of qemu- devel-ml, which is subscribed to QEMU. https://bugs.launchpad.net/bugs/1915925 Title: ARM semihosting HEAPINFO results wrote to wrong address Status in QEMU: Fix Released Bug description: This affects latest development branch of QEMU. According to the ARM spec of the HEAPINFO semihosting call: https://developer.arm.com/documentation/100863/0300/Semihosting- operations/SYS-HEAPINFO--0x16-?lang=en
Quoted 1 line
the PARAMETER REGISTER contains the address of a pointer to a four-
field data block.
However, QEMU treated the PARAMETER REGISTER as pointing to a four- field data block directly.
Here is a simple program that can demonstrate this problem:
https://github.com/iNvEr7/qemu-learn/tree/newlib-bug/semihosting-
newlib
This code links with newlib with semihosting mode, which will call the HEAPINFO SVC during crt0 routine. When running in QEMU (make run), it may crash the program either because of invalid write or memory curruption, depending on the compiled program structure.
Also refer to my discussion with newlib folks:
https://sourceware.org/pipermail/newlib/2021/018260.html
To manage notifications about this bug go to:
https://bugs.launchpad.net/qemu/+bug/1915925/+subscriptions
v2 cover semihosting/next (SYS_HEAPINFO fix)
Hi,
OK I took another tilt at fixing the SYS_HEAPINFO bug with a lot less code motion and also expanding the tcg tests. Currently based on my in-flight PR:
pull-testing-docs-xen-updates-080321-1
which already moves the semihosting code out of hw/. Please review:
- tests/tcg: add HeapInfo checking to semihosting test
- semihosting/arm-compat-semi: deref parameter register for SYS_HEAPINFO
- semihosting/arm-compat-semi: unify GET/SET_ARG helpers
- semihosting: move semihosting tests to multiarch
Alex Bennée (4):
semihosting: move semihosting tests to multiarch semihosting/arm-compat-semi: unify GET/SET_ARG helpers semihosting/arm-compat-semi: deref parameter register for SYS_HEAPINFO tests/tcg: add HeapInfo checking to semihosting test
tests/tcg/aarch64/semicall.h | 18 ++++++
tests/tcg/arm/semicall.h | 15 +----
tests/tcg/riscv64/semicall.h | 22 +++++++
semihosting/arm-compat-semi.c | 56 ++++++-----------
tests/tcg/arm/semihosting.c | 26 --------
.../arm-compat-semi}/semiconsole.c | 2 +
.../multiarch/arm-compat-semi/semihosting.c | 62 +++++++++++++++++++
tests/tcg/Makefile.target | 3 +
tests/tcg/aarch64/Makefile.target | 18 ------
tests/tcg/arm/Makefile.target | 16 +----
tests/tcg/multiarch/Makefile.target | 31 ++++++++++
11 files changed, 161 insertions(+), 108 deletions(-)
create mode 100644 tests/tcg/aarch64/semicall.h
create mode 100644 tests/tcg/riscv64/semicall.h
delete mode 100644 tests/tcg/arm/semihosting.c
rename tests/tcg/{arm => multiarch/arm-compat-semi}/semiconsole.c (93%)
create mode 100644 tests/tcg/multiarch/arm-compat-semi/semihosting.cSignature · 1 line
2.20.1
PATCH v2 1/4 semihosting: move semihosting tests to multiarch
It may be arm-compat-semihosting but more than one architecture uses it so lets move the tests into the multiarch area. We gate it on the feature and split the semicall.h header between the arches.
Also clean-up a bit of the Makefile messing about to one common set of runners.
---
tests/tcg/aarch64/semicall.h | 18 +++++++++++
tests/tcg/arm/semicall.h | 15 +--------
tests/tcg/riscv64/semicall.h | 22 +++++++++++++
.../arm-compat-semi}/semiconsole.c | 2 ++
.../arm-compat-semi}/semihosting.c | 5 ++-
tests/tcg/Makefile.target | 3 ++
tests/tcg/aarch64/Makefile.target | 18 -----------
tests/tcg/arm/Makefile.target | 16 ++--------
tests/tcg/multiarch/Makefile.target | 31 +++++++++++++++++++
9 files changed, 83 insertions(+), 47 deletions(-)
create mode 100644 tests/tcg/aarch64/semicall.h
create mode 100644 tests/tcg/riscv64/semicall.h
rename tests/tcg/{arm => multiarch/arm-compat-semi}/semiconsole.c (93%)
rename tests/tcg/{arm => multiarch/arm-compat-semi}/semihosting.c (85%)tests/tcg/aarch64/semicall.h new file +18 -0
@@ -0,0 +1,18 @@ +/* +* Semihosting Tests - AArch64 helper +* +* Copyright (c) 2019 +* Written by Alex Bennée <[email protected]> +* +* SPDX-License-Identifier: GPL-3.0-or-later +*/ + +uintptr_t __semi_call (uintptr_t type,uintptr_t arg0) +{ + register uintptr_t t asm("x0") = type; + register uintptr_t a0 asm("x1") = arg0; + asm("hlt 0xf000" + :"=r" (t) + :"r" (t),"r" (a0)); +return t; +}
tests/tcg/arm/semicall.h +1 -14
@@ -1,5 +1,5 @@/* -* Semihosting Tests +* Semihosting Tests - ARM Helper * * Copyright (c) 2019 * Written by Alex Bennée <[email protected]>
@@ -7,13 +7,8 @@* SPDX-License-Identifier: GPL-3.0-or-later */ -#define SYS_WRITE0 0x04 -#define SYS_READC 0x07 -#define SYS_REPORTEXC 0x18 -uintptr_t __semi_call (uintptr_t type,uintptr_t arg0) { -#if defined(__arm__) register uintptr_t t asm("r0") = type; register uintptr_t a0 asm("r1") = arg0;#ifdef __thumb__
@@ -23,13 +18,5 @@ uintptr_t __semi_call(uintptr_t type, uintptr_t arg0)#endif asm(SVC : "=r" (t) : "r" (t), "r" (a0)); -#else - register uintptr_t t asm("x0") = type; - register uintptr_t a0 asm("x1") = arg0; - asm("hlt 0xf000" - :"=r" (t) - :"r" (t),"r" (a0)); -#endif -return t; }
tests/tcg/riscv64/semicall.h new file +22 -0
@@ -0,0 +1,22 @@ +/* +* Semihosting Tests - RiscV64 Helper +* +* Copyright (c) 2021 +* Written by Alex Bennée <[email protected]> +* +* SPDX-License-Identifier: GPL-3.0-or-later +*/ + +uintptr_t __semi_call (uintptr_t type,uintptr_t arg0) +{ + register uintptr_t t asm("a0") = type; + register uintptr_t a0 asm("a1") = arg0; + asm(".option norvc\n\t" +".balign 16\n\t" +"slli zero, zero, 0x1f\n\t" +"ebreak\n\t" +"srai zero, zero, 0x7\n\t" + :"=r" (t) + :"r" (t),"r" (a0)); +return t; +}
tests/tcg/multiarch/arm-compat-semi/semiconsole.c renamed from tests/tcg/arm/semiconsole.c +2 -0
@@ -7,6 +7,8 @@* SPDX-License-Identifier: GPL-3.0-or-later */ +#define SYS_READC 0x07 +#include <stdio.h> #include <stdint.h> #include "semicall.h"
tests/tcg/multiarch/arm-compat-semi/semihosting.c renamed from tests/tcg/arm/semihosting.c +4 -1
@@ -7,12 +7,15 @@* SPDX-License-Identifier: GPL-3.0-or-later */ +#define SYS_WRITE0 0x04 +#define SYS_REPORTEXC 0x18 +#include <stdint.h> #include "semicall.h" int main (int argc,char *argv[argc]) { -#if defined(__arm__) +#if UINTPTR_MAX ==UINT32_MAX uintptr_t exit_code =0x20026 ;#else uintptr_t exit_block[2 ] = {0x20026 ,0 };
tests/tcg/Makefile.target +3 -0
@@ -32,6 +32,9 @@ all: -include ../../../config-host.mak -include ../config-$(TARGET).mak +ifeq ($(CONFIG_USER_ONLY),y) +-include $(SRC_PATH)/default-configs/targets/$(TARGET).mak +endif # for including , in command strings COMMA := ,
tests/tcg/aarch64/Makefile.target +0 -18
@@ -41,24 +41,6 @@ AARCH64_TESTS += mte-1 mte-2 mte-3 mte-4 mte-%: CFLAGS += -march=armv8.5-a+memtag endif -# Semihosting smoke test for linux-user -AARCH64_TESTS += semihosting -run-semihosting: semihosting - $(call run-test,$<,$(QEMU) $< 2> $<.err, "$< on $(TARGET_NAME)") - -run-plugin-semihosting-with-%: - $(call run-test, $@, $(QEMU) $(QEMU_OPTS) \ - -plugin $(PLUGIN_LIB)/$(call extract-plugin,$@) \ - $(call strip-plugin,$<) 2> $<.err, \ - "$< on $(TARGET_NAME) with $*") - -AARCH64_TESTS += semiconsole -run-semiconsole: semiconsole - $(call skip-test, $<, "MANUAL ONLY") - -run-plugin-semiconsole-with-%: - $(call skip-test, $<, "MANUAL ONLY") - ifneq ($(DOCKER_IMAGE)$(CROSS_CC_HAS_SVE),) # System Registers Tests AARCH64_TESTS += sysregs
tests/tcg/arm/Makefile.target +2 -14
@@ -30,37 +30,25 @@ run-fcvt: fcvt $(call diff-out,fcvt,$(ARM_SRC)/fcvt.ref) # Semihosting smoke test for linux-user -ARM_TESTS += semihosting semihosting: CFLAGS += -mthumb -run-semihosting: semihosting - $(call run-test,$<,$(QEMU) $< 2> $<.err, "$< on $(TARGET_NAME)") ARM_TESTS += semihosting-arm semihosting-arm: CFLAGS += -marm -semihosting-arm: semihosting.c - $(CC) $(CFLAGS) $(EXTRA_CFLAGS) $< -o $@ $(LDFLAGS) run-semihosting-arm: semihosting-arm $(call run-test,$<,$(QEMU) $< 2> $<.err, "$< on $(TARGET_NAME)") -run-plugin-semihosting-with-%: +run-plugin-semihosting-arm-with-%: $(call run-test, $@, $(QEMU) $(QEMU_OPTS) \ -plugin $(PLUGIN_LIB)/$(call extract-plugin,$@) \ $(call strip-plugin,$<) 2> $<.err, \ "$< on $(TARGET_NAME) with $*") -ARM_TESTS += semiconsole semiconsole-arm +ARM_TESTS += semiconsole-arm semiconsole: CFLAGS += -mthumb -run-semiconsole: semiconsole - $(call skip-test, $<, "MANUAL ONLY") - -run-plugin-semiconsole-with-%: - $(call skip-test, $<, "MANUAL ONLY") semiconsole-arm: CFLAGS += -marm -semiconsole-arm: semiconsole.c - $(CC) $(CFLAGS) $(EXTRA_CFLAGS) $< -o $@ $(LDFLAGS) run-semiconsole-arm: semiconsole-arm $(call skip-test, $<, "MANUAL ONLY")
tests/tcg/multiarch/Makefile.target +31 -0
@@ -69,6 +69,37 @@ run-gdbstub-%: endif EXTRA_RUNS += run-gdbstub-sha1 run-gdbstub-qxfer-auxv-read +# ARM Compatible Semi Hosting Tests +# +# Despite having ARM in the name we actually have several +# architectures that implement it. We gate the tests on the feature +# appearing in config. +# +ifeq ($(CONFIG_ARM_COMPATIBLE_SEMIHOSTING),y) +VPATH += $(MULTIARCH_SRC)/arm-compat-semi + +# Add -I path back to TARGET_NAME for semicall.h +semihosting: CFLAGS+=-I$(SRC_PATH)/tests/tcg/$(TARGET_NAME) + +run-semihosting: semihosting + $(call run-test,$<,$(QEMU) $< 2> $<.err, "$< on $(TARGET_NAME)") + +run-plugin-semihosting-with-%: + $(call run-test, $@, $(QEMU) $(QEMU_OPTS) \ + -plugin $(PLUGIN_LIB)/$(call extract-plugin,$@) \ + $(call strip-plugin,$<) 2> $<.err, \ + "$< on $(TARGET_NAME) with $*") + +semiconsole: CFLAGS+=-I$(SRC_PATH)/tests/tcg/$(TARGET_NAME) + +run-semiconsole: semiconsole + $(call skip-test, $<, "MANUAL ONLY") + +run-plugin-semiconsole-with-%: + $(call skip-test, $<, "MANUAL ONLY") + +TESTS += semihosting semiconsole +endif # Update TESTS TESTS += $(MULTIARCH_TESTS)
Signature · 1 line
2.20.1
PATCH v2 2/4 semihosting/arm-compat-semi: unify GET/SET_ARG helpers
In reply to Alex BennéeFrom the semihosting point of view what we want to know is the current mode of the processor. Unify this into a single helper and allow us to use the same GET/SET_ARG helpers for the rest of the code.
Note: we aren't currently testing riscv32 due to missing toolchain for check-tcg tests.
--- semihosting/arm-compat-semi.c | 54 ++++++++++++----------------------- 1 file changed, 18 insertions(+), 36 deletions(-)
semihosting/arm-compat-semi.c +18 -36
@@ -767,15 +767,28 @@ static const GuestFDFunctions guestfd_fns[] = { }, }; -/* Read the input value from the argument block; fail the semihosting -* call if the memory read fails. +/* +* Read the input value from the argument block; fail the semihosting +* call if the memory read fails. Eventually we could use a generic +* CPUState helper function here. */ +static inline bool is_64bit_semihosting (CPUArchState *env) +{#ifdef TARGET_ARM +return is_a64 (env); +#elif defined(TARGET_RISCV ) +return !riscv_cpu_is_32bit (env); +#else +#error un-handled architecture +#endif +} + +#define GET_ARG (n) do { \ - if (is_a64(env)) { \ + if (is_64bit_semihosting(env)) { \ if (get_user_u64(arg ## n, args + (n) * 8)) { \ errno = EFAULT; \ - return set_swi_errno(cs, -1); \ + return set_swi_errno(cs, -1); \ } \ } else { \ if (get_user_u32(arg ## n, args + (n) * 4)) { \
@@ -786,41 +799,10 @@ static const GuestFDFunctions guestfd_fns[] = { }while (0 ) #define SET_ARG(n, val) \ - (is_a64(env) ? \ + (is_64bit_semihosting (env) ? \put_user_u64 (val, args + (n) *8 ) : \put_user_u32 (val, args + (n) *4 )) -#endif -#ifdef TARGET_RISCV - -/* -* get_user_ual is defined as get_user_u32 in softmmu-semi.h, -* we need a macro that fetches a target_ulong -*/ -#define get_user_utl (arg, p) \ - ((sizeof(target_ulong) == 8) ? \ - get_user_u64(arg, p) : \ - get_user_u32(arg, p)) - -/* -* put_user_ual is defined as put_user_u32 in softmmu-semi.h, -* we need a macro that stores a target_ulong -*/ -#define put_user_utl (arg, p) \ - ((sizeof(target_ulong) == 8) ? \ - put_user_u64(arg, p) : \ - put_user_u32(arg, p)) - -#define GET_ARG (n) do { \ - if (get_user_utl(arg ## n, args + (n) * sizeof(target_ulong))) { \ - errno = EFAULT; \ - return set_swi_errno(cs, -1); \ - } \ - } while (0) - -#define SET_ARG (n, val) \ - put_user_utl(val, args + (n) * sizeof(target_ulong)) -#endif /* * Do a semihosting call.
Signature · 1 line
2.20.1
Re: [PATCH v2 2/4] semihosting/arm-compat-semi: unify GET/SET_ARG helpers
Quoted 23 lines · Alex Bennée
From the semihosting point of view what we want to know is the current mode of the processor. Unify this into a single helper and allow us to use the same GET/SET_ARG helpers for the rest of the code.
Note: we aren't currently testing riscv32 due to missing toolchain for check-tcg tests.
Signed-off-by Alex Bennée <[email protected]>--- semihosting/arm-compat-semi.c | 54 ++++++++++++----------------------- 1 file changed, 18 insertions(+), 36 deletions(-)semihosting/arm-compat-semi.c +0
-2@@ -767,15 +767,28 @@ static const GuestFDFunctions guestfd_fns[] = { }, }; -/* Read the input value from the argument block; fail the semihosting -* call if the memory read fails.
semihosting/arm-compat-semi.c static const GuestFDFunctions guestfd_fns[] = { · line 779
Quoted 10 lines
semihosting/arm-compat-semi.c +8
-0+/* +* Read the input value from the argument block; fail the semihosting +* call if the memory read fails. Eventually we could use a generic +* CPUState helper function here. */ +static inline bool is_64bit_semihosting (CPUArchState *env) +{#ifdef TARGET_ARM +return is_a64 (env); +#elif defined(TARGET_RISCV )
can we be consistent about whether we use defined() or not, please ?
semihosting/arm-compat-semi.c static const GuestFDFunctions guestfd_fns[] = { · line 783
Quoted 4 lines
semihosting/arm-compat-semi.c +4
-0+return !riscv_cpu_is_32bit (env); +#else +#error un-handled architecture +#endif
Otherwise
thanks
-- PMM
Re: [PATCH v2 2/4] semihosting/arm-compat-semi: unify GET/SET_ARG helpers
In reply to Alex BennéeQuoted 2 lines · Alex Bennée
Note: we aren't currently testing riscv32 due to missing toolchain for check-tcg tests.
That's surprising -- the usual risc-v toolchain supports both 64- and 32- bit targets.
Othewise, this patch is
Signature · 1 line
-keith
Re: [PATCH v2 2/4] semihosting/arm-compat-semi: unify GET/SET_ARG helpers
Quoted 7 lines · Keith Packard
Quoted 2 lines · Alex Bennée
Note: we aren't currently testing riscv32 due to missing toolchain for check-tcg tests.
That's surprising -- the usual risc-v toolchain supports both 64- and 32- bit targets.
If you can give me the runes for it I can tweak check-tcg to generate the 32 bit binaries.
Quoted 4 lines
Othewise, this patch is
Reviewed-by Keith Packard <[email protected]>
Signature · 1 line
Alex Bennée
PATCH v2 3/4 semihosting/arm-compat-semi: deref parameter register for SYS_HEAPINFO
In reply to Alex BennéeAs per the spec:
the PARAMETER REGISTER contains the address of a pointer to a four-field data block.
So we need to follow the pointer and place the results of SYS_HEAPINFO there.
--- semihosting/arm-compat-semi.c | 2 ++ 1 file changed, 2 insertions(+)
semihosting/arm-compat-semi.c +2 -0
@@ -1210,6 +1210,8 @@ target_ulong do_common_semihosting(CPUState *cs) retvals[2 ] = rambase + limit;/* Stack base */ retvals[3 ] = rambase;/* Stack limit. */ #endif +/* The result array is pointed to by arg0 */ + args = arg0;for (i =0 ; i <ARRAY_SIZE (retvals); i++) {bool fail;
Signature · 1 line
2.20.1
[Bug 1915925] Re: [PATCH v2 3/4] semihosting/arm-compat-semi: deref parameter register for SYS_HEAPINFO
Quoted 22 lines · Alex Bennée
As per the spec:
the PARAMETER REGISTER contains the address of a pointer to a four-field data block.
So we need to follow the pointer and place the results of
SYS_HEAPINFOthere.Cc Bug 1915925 <[email protected]>Cc Keith Packard <[email protected]>Signed-off-by Alex Bennée <[email protected]>--- semihosting/arm-compat-semi.c | 2 ++ 1 file changed, 2 insertions(+)semihosting/arm-compat-semi.c +0
-0
semihosting/arm-compat-semi.c target_ulong do_common_semihosting(CPUState *cs)
Quoted 9 lines
semihosting/arm-compat-semi.c +2
-1@@ -1210,6 +1210,8 @@ target_ulong do_common_semihosting(CPUState *cs) retvals[2 ] = rambase + limit;/* Stack base */ retvals[3 ] = rambase;/* Stack limit. */ #endif +/* The result array is pointed to by arg0 */ + args = arg0;for (i =0 ; i <ARRAY_SIZE (retvals); i++) {bool fail; --
No, 'args' is the argument array. That's not the same thing as the data block we're writing, and we shouldn't reassign that variable here.
What was wrong with the old arm-semi.c code, which just did appropriate loads and stores here, and worked fine and was not buggy ?
thanks
-- PMM
Signature · 17 lines
You received this bug notification because you are a member of qemu- devel-ml, which is subscribed to QEMU. https://bugs.launchpad.net/bugs/1915925 Title: ARM semihosting HEAPINFO results wrote to wrong address Status in QEMU: Confirmed Bug description: This affects latest development branch of QEMU. According to the ARM spec of the HEAPINFO semihosting call: https://developer.arm.com/documentation/100863/0300/Semihosting- operations/SYS-HEAPINFO--0x16-?lang=en
Quoted 1 line
the PARAMETER REGISTER contains the address of a pointer to a four-
field data block.
However, QEMU treated the PARAMETER REGISTER as pointing to a four- field data block directly.
Here is a simple program that can demonstrate this problem:
https://github.com/iNvEr7/qemu-learn/tree/newlib-bug/semihosting-
newlib
This code links with newlib with semihosting mode, which will call the HEAPINFO SVC during crt0 routine. When running in QEMU (make run), it may crash the program either because of invalid write or memory curruption, depending on the compiled program structure.
Also refer to my discussion with newlib folks:
https://sourceware.org/pipermail/newlib/2021/018260.html
To manage notifications about this bug go to:
https://bugs.launchpad.net/qemu/+bug/1915925/+subscriptions
Re: [PATCH v2 3/4] semihosting/arm-compat-semi: deref parameter register for SYS_HEAPINFO
Quoted 37 lines · Peter Maydell
Quoted 22 lines · Alex Bennée
As per the spec:
the PARAMETER REGISTER contains the address of a pointer to a four-field data block.
So we need to follow the pointer and place the results of
SYS_HEAPINFOthere.Cc Bug 1915925 <[email protected]>Cc Keith Packard <[email protected]>Signed-off-by Alex Bennée <[email protected]>--- semihosting/arm-compat-semi.c | 2 ++ 1 file changed, 2 insertions(+)semihosting/arm-compat-semi.c +0
-0semihosting/arm-compat-semi.c target_ulong do_common_semihosting(CPUState *cs)
Quoted 9 lines
semihosting/arm-compat-semi.c +2
-1@@ -1210,6 +1210,8 @@ target_ulong do_common_semihosting(CPUState *cs) retvals[2 ] = rambase + limit;/* Stack base */ retvals[3 ] = rambase;/* Stack limit. */ #endif +/* The result array is pointed to by arg0 */ + args = arg0;for (i =0 ; i <ARRAY_SIZE (retvals); i++) {bool fail; --No, 'args' is the argument array. That's not the same thing as the data block we're writing, and we shouldn't reassign that variable here.
Quoted 3 lines
What was wrong with the old
arm-semi.ccode, which just did appropriate loads and stores here, and worked fine and was not buggy ?
I was just trying avoid repeating too much verbiage. But OK I've reverted to the original code with the new helper:
for (i = 0; i < ARRAY_SIZE(retvals); i++) {
bool fail;
if (is_64bit_semihosting(env)) {
fail = put_user_u64(retvals[i], arg0 + i * 8);
} else {
fail = put_user_u32(retvals[i], arg0 + i * 4);
}
if (fail) {
/* Couldn't write back to argument block */
errno = EFAULT;
return set_swi_errno(cs, -1);
}
}
return 0;Quoted 3 lines
thanks
-- PMM
Signature · 1 line
Alex Bennée
PATCH v2 4/4 tests/tcg: add HeapInfo checking to semihosting test
In reply to Alex BennéeQuery the SYS_HEAPINFO semicall and do some basic verification of the information via libc calls.
--- .../multiarch/arm-compat-semi/semihosting.c | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-)
tests/tcg/multiarch/arm-compat-semi/semihosting.c +34 -1
@@ -8,9 +8,16 @@*/ #define SYS_WRITE0 0x04 +#define SYS_HEAPINFO 0x16#define SYS_REPORTEXC 0x18 +#define _GNU_SOURCE/* asprintf is a GNU extension */ +#include <stdint.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> #include "semicall.h" int main (int argc,char *argv[argc])
@@ -21,8 +28,34 @@ int main(int argc, char *argv[argc])uintptr_t exit_block[2 ] = {0x20026 ,0 };uintptr_t exit_code = (uintptr_t ) &exit_block;#endif +struct { +void *heap_base; +void *heap_limit; +void *stack_base; +void *stack_limit; + } info; +void *ptr_to_info = (void *) &info; +char *heap_info, *stack_info; +void *brk =sbrk (0 ); + +__semi_call (SYS_WRITE0 , (uintptr_t )"Checking HeapInfo\n" ); + +memset (&info,0 ,sizeof (info)); +__semi_call (SYS_HEAPINFO , (uintptr_t ) &ptr_to_info); + +asprintf (&heap_info,"heap: %p -> %p\n" , info.heap_base, info.heap_limit); +__semi_call (SYS_WRITE0 , (uintptr_t ) heap_info); +if (info.heap_base != brk) { +sprintf (heap_info,"heap mismatch: %p\n" , brk); +__semi_call (SYS_WRITE0 , (uintptr_t ) heap_info); +return -1 ; + } + +asprintf (&stack_info,"stack: %p -> %p\n" , info.stack_base, info.stack_limit); +__semi_call (SYS_WRITE0 , (uintptr_t ) stack_info); +free (heap_info); +free (stack_info); -__semi_call (SYS_WRITE0 , (uintptr_t )"Hello World" );__semi_call (SYS_REPORTEXC , exit_code);/* if we get here we failed */ return -1 ;
Signature · 1 line
2.20.1
Re: [PATCH v2 4/4] tests/tcg: add HeapInfo checking to semihosting test
tests/tcg/multiarch/arm-compat-semi/semihosting.c int main(int argc, char *argv[argc]) · line 48
Quoted 3 lines · Alex Bennée
tests/tcg/multiarch/arm-compat-semi/semihosting.c +3
-0+asprintf (&heap_info,"heap: %p -> %p\n" , info.heap_base, info.heap_limit); +__semi_call (SYS_WRITE0 , (uintptr_t ) heap_info); +if (info.heap_base != brk) {
That requires qemu to know a lot about the run-time environment, which it rarely does in my experience of embedded systems...
All I've been able to check is whether the heap base is not below the heap limit and the stack base is not above the stack limit. Not exactly great validation, but at least it caught the case where I set the stack limit to the top of the stack?
if (block.heap_base != NULL && block.heap_limit != NULL) {
/* Error if heap base is above limit */
if ((uintptr_t) block.heap_base >= (uintptr_t) block.heap_limit) {
printf("heap base %p >= heap_limit %p\n",
block.heap_base, block.heap_limit);
exit(1);
}
}
if (block.stack_base != NULL && block.stack_limit != NULL) {
/* Error if stack base is below limit */
if ((uintptr_t) block.stack_base < (uintptr_t) block.stack_limit) {
printf("stack base %p < stack_limit %p\n",
block.stack_base, block.stack_limit);
exit(2);
}
}
exit(0);Signature · 1 line
-keith