Skip to content

Arbitrary bytes are allowed to be called "main" without unsafe #50425

Closed
@MarkDDR

Description

@MarkDDR

playpen

#![no_main]
#[no_mangle]
#[allow(non_upper_case_globals)]
pub static main: [f64; 7] = [
    -2.524355105561429e-29,
    -1.300510625000004e5,
    3.2984344530918e-310,
    4.138422214507362e199,
    5.843108278877813e257,
    8.69972257894351e183,
    1.979474603311587e-258
];

When compiled, no warnings are issued and when run it prints "Main is usually a function". As I understand this code is just arbitrary bytes being interpreted as code since it has the main symbol. While unlikely to be seen in real code, this is still a way for any arbitrary (unsafe) code to be run without unsafe.

I would have expected at least a warning that a "special" symbol ("main", are there other important ones to consider?) was specified with #[no_mangle] and was not a function. Or maybe arbitrary bytes for "special" symbols must be in an unsafe block to try to avoid this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions