The Misaligned Access That Never Trapped
The verdict is the ISA's own latitude: the manual leaves misaligned loads and stores to the execution environment, which may handle them invisibly or raise an address-misaligned exception. The run shows which choice this machine made: both accesses completed with no trap, the handler never running.
No claim depends on how any core implements anything. No cache, pipeline, or timing behavior is asserted or measured; QEMU serves as a functional executor only, and the invisible-handling verdict is read off the trap registers, not the microarchitecture.
No boot code, machine-mode service, or privilege handoff takes part. The M-mode trap handler is the experiment harness, and it never ran: the seen flag stayed zero and mcause, mepc, and mtval read back zero.
No kernel is involved at any step. The test binary runs on bare QEMU with no operating system, and no system interface appears in the argument.
The claim is one fixed machine behavior exercised as two fixed accesses with readback, not a computation proved against an independent oracle. No invariant is proved beyond the two observed outcomes.
The manual splits every load and store in two. When the effective address is naturally aligned, divisible by the access size, no implementation may raise an address-misaligned exception. When it is not, the choice belongs to the execution environment: it may handle the access invisibly, in hardware or with a hidden trap, or it may raise an address-misaligned exception instead. The trap is optional. [1]
This machine chose the invisible path. A bare-metal M-mode program on QEMU 8.2.2 installed a trap handler recording mcause, mepc, and mtval, then issued a word load from 0x80000c01, one byte past a four-byte boundary, and a word store of 0x12345678 to 0x80000c02, two bytes past. Neither access trapped: the handler never ran, and all three trap registers read back zero. [2]
The values came back byte-exact. The load returned 0xffffffffd5a1b2c3: the four bytes C3 B2 A1 D5, assembled little-endian into 0xd5a1b2c3 and sign-extended to 64 bits, because lw sign-extends. The store wrote exactly its four bytes, 78 56 34 12, and the byte just outside the stored range kept its old value, 0xC3. The machine did the unaligned work in software and told nobody. [2]
The two accesses, reduced to their results:
lw @ 0x80000c01 -> 0xffffffffd5a1b2c3 /* no trap */
sw @ 0x80000c02 <- 0x12345678 /* no trap, bytes 78 56 34 12 */
Diagram: hand-built by Chris Dillinger for this post
Another core is allowed the other choice: raise the address-misaligned trap, mcause 4 for the load and 6 for the store, and make your handler earn its keep. [2] A test that passes here proves the code runs on QEMU; it proves nothing about alignment. The habit: align what the manual says to align, and never let a green run on one machine certify the code for another.
Sources
- 01RISC-V ISA Manual, Volume I: Unprivileged Architecture, section 2.6RISC-V International
The misaligned-access rule, paraphrased: naturally aligned loads and stores shall not raise an address-misaligned exception on any implementation; for the rest, the execution environment may handle them invisibly (in hardware, with a hidden trap, or both) or raise an address-misaligned or access-fault exception. Misaligned accesses are also not guaranteed atomic.
- 02src/misaligned: module README and PROOF.mdChris Dillinger
The firsthand QEMU run behind the post, re-verified on this run: a word load from 0x80000c01 and a word store of 0x12345678 to 0x80000c02, each past a four-byte boundary; neither trapped (seen=0, mcause/mepc/mtval all zero); the load returned 0xffffffffd5a1b2c3 and the store round-tripped byte-exact with the neighboring byte untouched. PROOF.md records 0 mismatches and notes real cores may instead trap with mcause 4 (load) or 6 (store).
Sources
- 01RISC-V ISA Manual, Volume I: Unprivileged Architecture, section 2.6RISC-V International
The misaligned-access rule, paraphrased: naturally aligned loads and stores shall not raise an address-misaligned exception on any implementation; for the rest, the execution environment may handle them invisibly (in hardware, with a hidden trap, or both) or raise an address-misaligned or access-fault exception. Misaligned accesses are also not guaranteed atomic.
- 02src/misaligned: module README and PROOF.mdChris Dillinger
The firsthand QEMU run behind the post, re-verified on this run: a word load from 0x80000c01 and a word store of 0x12345678 to 0x80000c02, each past a four-byte boundary; neither trapped (seen=0, mcause/mepc/mtval all zero); the load returned 0xffffffffd5a1b2c3 and the store round-tripped byte-exact with the neighboring byte untouched. PROOF.md records 0 mismatches and notes real cores may instead trap with mcause 4 (load) or 6 (store).