rust-lang / rust-lang/rust

RISC-V memcpy routine uses misaligned loads and stores

Open
#145,769 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug O-riscv S-needs-repro T-libs
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the provided copy_nonoverlapping example for riscv64gc-unknown-none-elf and inspect the generated RISC-V behavior for the misaligned destination. Compare it with the working write_volatile byte loop; done means the 16-byte copy preserves every byte at the shown addresses.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers, embedded-iot
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.