Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using -Z build-std-features=panic_immediate_abort on Linux results in invalid executable. #97602

Closed
A1-Triard opened this issue May 31, 2022 · 0 comments
Labels
C-bug Category: This is a bug.

Comments

@A1-Triard
Copy link

Cross-compiling windows executable on Linux ends with invalid executable if -Z build-std-features=panic_immediate_abort feature is used.

I have reproduced it with GNU toolchain, and with Wine'd MSVC toolchain. Also I have reproduced it while compiling x32 and x64 targets. And it can be reproduced with no_std and ordinary project. But it seems it is not reproducible when windows is a compilation host.

#![feature(lang_items)]
#![feature(start)]
#![no_std]
#![windows_subsystem="console"]

#[lang="eh_personality"] extern fn rust_eh_personality() {}
#[no_mangle] pub extern fn rust_eh_register_frames () {}
#[no_mangle] pub extern fn rust_eh_unregister_frames () {}

#[cfg(target_env="msvc")]
#[link(name="libcmt")]
extern { }

use core::mem::{MaybeUninit, transmute};
use core::panic::PanicInfo;

fn exit(exit_code: u8) -> ! {
    unsafe { winapi::um::processthreadsapi::ExitProcess(exit_code as _) };
    loop { }
}

#[panic_handler]
pub extern fn panic(_info: &PanicInfo) -> ! {
    exit(99)
}

#[start]
pub fn main(_argc: isize, _argv: *const *const u8) -> isize {
    let mut buf: [MaybeUninit<u8>; 13] = unsafe { MaybeUninit::uninit().assume_init() };
    (&mut buf[.. 11]).copy_from_slice(unsafe { transmute(&b"Source12345"[..]) });
    buf[11].write(b'6');
    buf[12].write(0);
    let buf: [u8; 13] = unsafe { transmute(buf) };
    assert!(buf[5] > 120);
    exit(0)
}

Cargo.toml:

[package]
name = "test_panic_immediate_abort"
version = "0.1.0"
edition = "2021"

[profile.dev]
panic = "abort"

[profile.release]
panic = "abort"

[dependencies]
winapi = { version = "0.3.9", default-features = false, features = ["processthreadsapi"] }

When I compile with the following command:

$ cargo  nightly build -Z build-std=core,panic_abort --target=i686-pc-windows-gnu

the result is correct executable which could be normally executed and ends with error code 1 (as expected).

But if I compile the project with the following command:

$ cargo  nightly build -Z build-std=core,panic_abort -Z build-std-features=panic_immediate_abort --target=i686-pc-windows-gnu

then the result executable is not valid anymore. For example, a try to run it with wine ends with "Unhandled illegal instruction at address 004019A6 (thread 0024), starting debugger..." error.

@A1-Triard A1-Triard added the C-bug Category: This is a bug. label May 31, 2022
@A1-Triard A1-Triard changed the title Cross-compiling windows executable on Linux ends with invalid executable if -Z build-std-features=panic_immediate_abort feature is used. Using -Z build-std-features=panic_immediate_abort on Linux results in invalid executable. Jun 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

1 participant