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

Compiler generated main symbol has unavoidable pointless stack check #14912

Closed
huonw opened this issue Jun 15, 2014 · 6 comments
Closed

Compiler generated main symbol has unavoidable pointless stack check #14912

huonw opened this issue Jun 15, 2014 · 6 comments
Labels
A-codegen Area: Code generation

Comments

@huonw
Copy link
Member

huonw commented Jun 15, 2014

#[no_split_stack]
#[start]
fn main(_: int, _: **u8) -> int {
    0
}

compiles to

    .text
    .file   "-.rs"
    .section    .text._ZN4main20he8340d7809f38c39eaa4v0.0E,"ax",@progbits
    .align  16, 0x90
    .type   _ZN4main20he8340d7809f38c39eaa4v0.0E,@function
_ZN4main20he8340d7809f38c39eaa4v0.0E:
    .cfi_startproc
    movabsq $0, %rax
    movq    %rdi, -8(%rsp)
    movq    %rsi, -16(%rsp)
    retq
.Ltmp0:
    .size   _ZN4main20he8340d7809f38c39eaa4v0.0E, .Ltmp0-_ZN4main20he8340d7809f38c39eaa4v0.0E
    .cfi_endproc

    .section    .text.main,"ax",@progbits
    .globl  main
    .align  16, 0x90
    .type   main,@function
main:
    .cfi_startproc
    cmpq    %fs:112, %rsp
    ja  .LBB1_0
    movabsq $8, %r10
    movabsq $0, %r11
    callq   __morestack
    retq
.LBB1_0:
    pushq   %rax
.Ltmp1:
    .cfi_def_cfa_offset 16
    callq   _ZN4main20he8340d7809f38c39eaa4v0.0E
    popq    %rdx
    retq
.Ltmp2:
    .size   main, .Ltmp2-main
    .cfi_endproc

    .section    ".note.GNU-stack","",@progbits

In particular the main function has a stack check, which can't be removed (there's no way to put a #[no_split_stack] on it: the compiler creates it automagically), and, (I think, I could easily be wrong) it's always pointless: when would the entry point of a program be overflowing the stack immediately?

@Florob
Copy link
Contributor

Florob commented Jun 15, 2014

I think that if the main function allocates a sufficient amount of data on the stack this might be necessary.
Being a bit annoyed by the unneeded stack checks I implemented avoiding them upstream a while ago: http://llvm.org/viewvc/llvm-project?view=revision&revision=209436. Once Rust's LLVM is updated to a newer revision we should get this. In particular it should get rid of the stack check in this specific case.

@alexcrichton
Copy link
Member

Note that all platforms we run on have the initial stack limit as 0, which means that the initial thread has an "infinite stack". Fixing main is also not enough, one would have to fix all functions down to when the initial stack limit is set, and that's a lot of functions!

@mahkoh
Copy link
Contributor

mahkoh commented Oct 22, 2014

Still broken but you can disable all stack checks, including this one, with -C no-stack-check now.

@steveklabnik
Copy link
Member

Updated code:

#![feature(start)]

#[no_stack_check]
#[start]
fn main(_: isize, _: *const *const u8) -> isize {
        0
}

I am bad at assembly, but

        .text
        .file   "hello.0.rs"
        .section        .text._ZN4main20h9021d633acfe0488eaaE,"ax",@progbits
        .align  16, 0x90
        .type   _ZN4main20h9021d633acfe0488eaaE,@function
_ZN4main20h9021d633acfe0488eaaE:
        .cfi_startproc
        xorl    %eax, %eax
        movq    %rdi, -8(%rsp)
        movq    %rsi, -16(%rsp)
        retq
.Ltmp0:
        .size   _ZN4main20h9021d633acfe0488eaaE, .Ltmp0-_ZN4main20h9021d633acfe0488eaaE
        .cfi_endproc

        .section        .text.main,"ax",@progbits
        .globl  main
        .align  16, 0x90
        .type   main,@function
main:
        .cfi_startproc
        cmpq    %fs:112, %rsp
        ja      .LBB1_0
        movabsq $8, %r10
        movabsq $0, %r11
        callq   __morestack
        retq
.LBB1_0:
        pushq   %rax
.Ltmp1:
        .cfi_def_cfa_offset 16
        callq   _ZN4main20h9021d633acfe0488eaaE
        popq    %rdx
        retq
.Ltmp2:
        .size   main, .Ltmp2-main
        .cfi_endproc


        .section        ".note.GNU-stack","",@progbits

@steveklabnik steveklabnik added the A-codegen Area: Code generation label Jan 23, 2015
@dotdash
Copy link
Contributor

dotdash commented Mar 1, 2015

With the new LLVM version, that stack check gets removed in an optimized build. Can we close this?

    .text
    .file   "issue14912.0.rs"
    .section    .text.main,"ax",@progbits
    .globl  main
    .align  16, 0x90
    .type   main,@function
main:
    xorl    %eax, %eax
    retq
.Ltmp0:
    .size   main, .Ltmp0-main


    .section    ".note.GNU-stack","",@progbits

@alexcrichton
Copy link
Member

great, thanks @dotdash!

bors added a commit to rust-lang-ci/rust that referenced this issue Jun 5, 2023
Don't add --all-targets to runnables for no-std crates

Fixes rust-lang/rust-analyzer#14155
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-codegen Area: Code generation
Projects
None yet
Development

No branches or pull requests

6 participants