Skip to content

RISC-V memcpy routine uses misaligned loads and stores #145769

@ds84182

Description

@ds84182

I tried this code:

// Using the riscv64gc-unknown-none-elf target.
// Should be reproduced by:
#[inline(never)]
fn do_copy(len: usize) {
  // len is 16.
  core::ptr::copy_nonoverlapping(0x237c0 as *const u8, 0x43adc as *mut u8, len);
}

I expected to see this happen: Byte copy should succeed without missing bytes. A byte copy loop using write_volatile works fine.

Instead, this happened:

Debugging output over UART (with annotations)

stream read 16 [87, 79, 79, 70, 66, 79, 79, 84, 16, 0, 0, 0, 3, 0, 0, 0] (.data)
ptrs: 0x237c0 (.data) 0x43adc (stack, 4-byte aligned)
head (stack): [87, 79, 79, 70, 0, 0, 0, 0, 16, 0, 0, 0, 3, 0, 0, 0]

Meta

rustc --version --verbose:

rustc 1.91.0-nightly (2e2642e64 2025-08-16)
binary: rustc
commit-hash: 2e2642e641a941f0a1400c7827fd89aa86fef8f4
commit-date: 2025-08-16
host: x86_64-unknown-linux-gnu
release: 1.91.0-nightly
LLVM version: 21.1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.O-riscvTarget: RISC-V architectureS-needs-reproStatus: This issue has no reproduction and needs a reproduction to make progress.T-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions