Skip to content

Technically unsafe behavior in safe rust using #![no_main] #47685

Closed
@CensoredUsername

Description

@CensoredUsername

Using #![no_main], and a #[no_mange] pub static main array the linker will assume that the main array is actually the main function and execute whatever is in there. Outside of a warning for nonstandard naming this compiles fine on stable and nightly on both debug and release mode.

Example

This example will print the text Main is usually a function\n on x64 linux and probably crash violently on anything else.

It's rather hard to trigger this behavior without trying, but it should probably not compile without errors. Maybe add a check for #![no_main] crates that whatever's declared as #[no_mangle] main is actually a function with the correct signature.

Meta

The issue exists on rustc 1.25.0-nightly, rustc 1.23.0 and anything in between.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.T-compilerRelevant to the compiler 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