Chris Low-level systems developer
← Back to all posts

The Misaligned Access That Never Trapped

ISA

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.

MICROARCH

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.

FIRMWARE

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.

KERNEL

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.

ALGORITHM

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 */
Blueprint-style diagram of the misaligned access experiment: eight bytes of the scratch buffer at 0x80000c00 holding the control fill D4 C3 B2 A1 D5 C3 B2 A1; the lw span covers base+1 through base+4 and the sw span covers base+2 through base+5, both crossing 4-byte boundaries while the trap handler never runs; below, the manual's two choices, handle invisibly as this machine did, or raise the address-misaligned trap, mcause 4 for loads and 6 for stores.
Both accesses crossed a 4-byte boundary and neither trapped. The manual permits either outcome; this machine chose the invisible one.
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

  1. 01
    RISC-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.

  2. 02
    src/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

  1. 01
    RISC-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.

  2. 02
    src/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).

Related work: kernel and driver correctness work, fixed price. .

Choose how to get in touch

The project brief is filled in for you.

Compose in Gmail Use my email app