target/riscv: RVF-only GDB register writes read eight bytes from a four-byte packet
The source excerpts below are from QEMU master at ff1d2d19d7e24893e2012d879f8e73077e17b9bd.
Host environment
- Operating system: Linux
- OS/kernel version: 6.0
- Architecture: x86
- QEMU flavor:
qemu-system-riscv64 - QEMU version: qemu.git
ff1d2d19d7e24893e2012d879f8e73077e17b9bd
Emulated/Virtualized environment
- Operating system: Any stopped RISC-V guest exposing RVF without RVD
- Architecture: RISC-V
- Accelerator:
TCG or KVM gdbstub - Relevant CPU/device configuration: RVF enabled, RVD disabled, QEMU gdbstub active
Description of problem
Background and terminology
QEMU describes target registers to GDB with XML. The callback for each register must consume and return exactly the width advertised by that XML. An RVF-only CPU advertises 32-bit floating-point registers, while an RVD CPU advertises 64-bit registers. Reading eight bytes from a four-byte GDB packet crosses the register-buffer boundary and also disagrees with the read callback.
Affected code path
The affected implementation locations are:
target/riscv/gdbstub.c: riscv_gdb_get_fpu() and riscv_gdb_set_fpu()
Registration selects riscv-32bit-fpu.xml for RVF-only CPUs. The read callback returns four bytes in that case, but the common write callback unconditionally loads and reports eight bytes.
Current source evidence
104 |
105 | static int riscv_gdb_get_fpu(CPUState *cs, GByteArray *buf, int n)
106 | {
107 | RISCVCPU *cpu = RISCV_CPU(cs);
108 | CPURISCVState *env = &cpu->env;
109 |
110 | if (n < 32) {
111 | if (env->misa_ext & RVD) {
112 | return gdb_get_reg64(buf, env->fpr[n]);
113 | }
114 | if (env->misa_ext & RVF) {
115 | return gdb_get_reg32(buf, env->fpr[n]);
116 | }
117 | }
118 | return 0;
119 | }
120 |
121 | static int riscv_gdb_set_fpu(CPUState *cs, uint8_t *mem_buf, int n)
122 | {
123 | RISCVCPU *cpu = RISCV_CPU(cs);
124 | CPURISCVState *env = &cpu->env;
125 |
126 | if (n < 32) {
127 | env->fpr[n] = ldn(env, mem_buf, 8); /* always 64-bit */
128 | return sizeof(uint64_t);
129 | }
130 | return 0;
131 | }
132 |
133 | static int riscv_gdb_get_vector(CPUState *cs, GByteArray *buf, int n)109 |
110 | if (n < 32) {
111 | if (env->misa_ext & RVD) {
112 | return gdb_get_reg64(buf, env->fpr[n]);
113 | }
114 | if (env->misa_ext & RVF) {
115 | return gdb_get_reg32(buf, env->fpr[n]);
116 | }
117 | }
118 | return 0;
119 | }
120 |
121 | static int riscv_gdb_set_fpu(CPUState *cs, uint8_t *mem_buf, int n)
122 | {
123 | RISCVCPU *cpu = RISCV_CPU(cs);
124 | CPURISCVState *env = &cpu->env;
125 | Root cause analysis
The GDB core passes the logical bytes for one XML-described register. For an RVF-only CPU the current GByteArray length is four bytes. riscv_gdb_set_fpu() calls ldn(..., 8) and returns eight, so it reads outside that logical register payload and reports consuming twice its advertised width. The backing allocation may have spare capacity, so this is not evidence of an inevitable heap-boundary fault; the upper four bytes can instead be stale/unrelated storage. It also makes the setter asymmetric with the four-byte getter.
The reasoning, step by step:
- QEMU advertises each RVF register as 32 bits to GDB.
- GDB sends four bytes when writing one floating-point register.
- The setter reads eight bytes and reports an eight-byte consumption.
- Bytes outside the logical register value can influence the internal upper half, and the caller is told that eight bytes were consumed.
- Readback returns only four bytes, making get and set asymmetric.
Worked example and boundary cases
For F without D, an XML register is four bytes. A P-packet carrying one 32-bit FPR value gives the callback four logical bytes, while the setter loads eight. Spare GByteArray capacity can keep the extra read inside an allocation, but it does not make those bytes part of this register. Verify both P-packet and bulk G-packet cursor behavior.
Conditions needed to reach the problem
- RVF is present and RVD is absent.
- A debugger writes an FPU register through the remote protocol.
- The affected common FPU setter is registered.
Expected or potential effect
- The gdbstub reads beyond the current register payload even if allocation capacity prevents a host fault.
- The written internal upper half and all-register packet cursor can depend on unrelated adjacent bytes.
- RVD systems hide the mismatch because their XML width is eight bytes.
Expected behavior
For an RVF-only CPU, GDB reads and writes each floating-point register as the advertised 32-bit XML register.
Steps to reproduce
The following validation design covers the expected and current-code results.
- start an RVF-only qemu-system-riscv64 guest with
-S -gdb tcp::PORT. - Connect GDB and write the last floating-point register to maximize boundary visibility.
- Add a gdbstub unit test whose logical buffer length is four and whose spare capacity is filled with a distinguishable poison pattern.
- Read the register back and compare with the requested 32-bit value.
- Repeat on an RVD CPU as an eight-byte control.
Expected result: The setter consumes four bytes for RVF and eight bytes for RVD, matching the advertised XML and getter.
Current behavior: The setter always reads and returns eight bytes.
Suggested fix direction
Mirror the getter's RVF/RVD width selection. For RVF, load four bytes, place them in QEMU's internal FPR representation using the established single-precision/NaN-box convention, and return four. For RVD, retain the eight-byte path.
The fix should be covered by the following focused regression checks:
- RVF-only first and last FPR writes with a four-byte buffer.
- RVD eight-byte writes.
- Get/set round-trip and sanitizer coverage.
References
- QEMU source baseline: ff1d2d19
Additional information
- Source baseline: QEMU
masteratff1d2d19d7e24893e2012d879f8e73077e17b9bd(checked 2026-09-05).