Unconsistent behavior by overriding `lto` in Cargo.toml

⚓ rust    📅 2025-05-20    👤 surdeus    👁️ 3      

surdeus

Warning

This post was published 40 days ago. The information described in this article may have changed.

I came with a minimal template to run in Linux-compatible OS with a x86_64 architecture.

This template consists of barely the same function in different locations.
The function is a write syscall invocation to standard output of an &str.

fn _print(msg: &str) {
    let bytes = msg.as_bytes();
    unsafe {
        core::arch::asm!(
            "syscall",
            in("rax") 1usize,
            in("rdi") 1usize,
            in("rsi") bytes.as_ptr(),
            in("rdx") bytes.len(),
            out("rcx") _,
            out("r11") _,
            lateout("rax") _,
        );
    }
}

There are several flavors (locations) of this same function:

./src/main.rs
./src/lib.rs
./src/mamod.rs
./crates/print/src/lib.rs

The Cargo.toml overrides the default lto in release mode to true;

[profile.release]
lto = true

Then by running:

git clone git@github.com:ze-gois/rust_template_x86_64
cd rust_template_x86_64
cargo run --release

Works fine,

❯ cargo run --release
    Finished `release` profile [optimized] target(s) in 0.04s
     Running `target/x86_64-unknown-none/release/template`
Test 0: Local inline assembly works
Test 1: Local module inline assembly works
Test 2: Lib inline assembly works
Test 3: Crate inline assembly works
Panicking 1;Panicking 2;.....

however the

cargo run

Does not work.

❯ cargo run
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.00s
     Running `target/x86_64-unknown-none/debug/template`
Test 0: Local inline assembly works
Test 1: Local module inline assembly works
[1]    46473 segmentation fault (core dumped)  cargo run

7 posts - 3 participants

Read full topic

🏷️ rust_feed