[loongarch*-linux] -Crelocation-model=static should not access external data directly · Issue #118053 · rust-lang/rust (original) (raw)

I tried this code:

use std::ffi::CStr; use std::os::raw::c_char;

extern "C" { static environ: *const *const c_char; }

fn main() { unsafe { let mut env_ptr = environ;

    while !(*env_ptr).is_null() {
        let c_str_ptr = *env_ptr;
        let rust_str = CStr::from_ptr(c_str_ptr);

        println!("{:?}", rust_str.to_string_lossy());
        env_ptr = env_ptr.offset(1);
    }
}

}

I expected to see the environments variables printed.

Instead, this happened: Aborted (core dumped)

Meta

rustc --version --verbose:

rustc 1.74.0 (79e9716c9 2023-11-13)
binary: rustc
commit-hash: 79e9716c980570bfd1f666e3b16ac583f0168962
commit-date: 2023-11-13
host: loongarch64-unknown-linux-gnu
release: 1.74.0
LLVM version: 17.0.4

Backtrace

thread 'main' panicked at t.rs:12:16:
misaligned pointer dereference: address must be a multiple of 0x8 but is 0x28ee81ef1c000aef
stack backtrace:
   0: rust_begin_unwind
             at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/std/src/panicking.rs:597:5
   1: core::panicking::panic_nounwind_fmt
             at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/core/src/panicking.rs:106:14
   2: core::panicking::panic_misaligned_pointer_dereference
             at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/core/src/panicking.rs:193:5
   3: t::main
             at ./t.rs:12:16
   4: core::ops::function::FnOnce::call_once
             at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
thread caused non-unwinding panic. aborting.
Aborted (core dumped)