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

How can you disable stack checking? #65751

Open
Lokathor opened this issue Oct 24, 2019 · 5 comments
Open

How can you disable stack checking? #65751

Lokathor opened this issue Oct 24, 2019 · 5 comments
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@Lokathor
Copy link
Contributor

On Windows-MSCV-x86_64 I am attempting to create a no_std binary with Stable (1.38). I'm getting an unresolved symbol for __chkstk:

D:\dev\quixotic>cargo run
   Compiling quixotic v0.0.0 (D:\dev\quixotic)
error: linking with `link.exe` failed: exit code: 1120
  |
  = note: "C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\BuildTools\\VC\\Tools\\MSVC\\14.13.26128\\bin\\HostX64\\x64\\link.exe" "/NOLOGO" "/NXCOMPAT" "/LIBPATH:C:\\Users\\Daniel\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib" "D:\\dev\\quixotic\\target\\debug\\deps\\main-13addd73d3845cd6.1z81p1fkccyc5iby.rcgu.o" "D:\\dev\\quixotic\\target\\debug\\deps\\main-13addd73d3845cd6.22n4xq61sdy0azbs.rcgu.o" "D:\\dev\\quixotic\\target\\debug\\deps\\main-13addd73d3845cd6.255oap8fzr2s5w11.rcgu.o" "D:\\dev\\quixotic\\target\\debug\\deps\\main-13addd73d3845cd6.2u5094on6j0uypp3.rcgu.o" "/OUT:D:\\dev\\quixotic\\target\\debug\\deps\\main-13addd73d3845cd6.exe" "/SUBSYSTEM:console" "/OPT:REF,NOICF" "/DEBUG" "/NATVIS:C:\\Users\\Daniel\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\intrinsic.natvis" "/NATVIS:C:\\Users\\Daniel\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\liballoc.natvis" "/NATVIS:C:\\Users\\Daniel\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\libcore.natvis" "/LIBPATH:D:\\dev\\quixotic\\target\\debug\\deps" "/LIBPATH:C:\\Users\\Daniel\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib" "D:\\dev\\quixotic\\target\\debug\\deps\\libwinapi-f9a577440f324f5e.rlib" "C:\\Users\\Daniel\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\librustc_std_workspace_core-6391a360e3eeafba.rlib" "C:\\Users\\Daniel\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libcore-6c8df881cdc2afb2.rlib" "C:\\Users\\Daniel\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libcompiler_builtins-f998976453a15b70.rlib" "advapi32.lib" "cfgmgr32.lib" "gdi32.lib" "kernel32.lib" "msimg32.lib" "opengl32.lib" "user32.lib" "winspool.lib"
  = note: main-13addd73d3845cd6.1z81p1fkccyc5iby.rcgu.o : error LNK2019: unresolved external symbol 
__chkstk referenced in function mainCRTStartup
          main-13addd73d3845cd6.255oap8fzr2s5w11.rcgu.o : error LNK2001: unresolved external symbol 
__chkstk
          D:\dev\quixotic\target\debug\deps\main-13addd73d3845cd6.exe : fatal error LNK1120: 1 unresolved externals


error: aborting due to previous error

error: Could not compile `quixotic`.

To learn more, run the command again with --verbose.

Normally, you resolve this in the C/C++ MSVC compiler with /GS- to disable stack security checks, /Gs999999999 to disable stack probes, and telling the linker /STACK:0x100000,0x100000 to force it to commit the entire 1MB of stack right away instead of using all the pages individually.

I can pass the linker arg to link.exe easily enough, but what would the rustc equivalents to disable stack checking and stack probing be?

@Lokathor Lokathor changed the title How can you disable stack checking and set a stack size? How can you disable stack checking? Oct 24, 2019
@hellow554
Copy link
Contributor

Can't you just define a dummy function, e.g. #[no_mangle] extern "C" fn __chkstk () {}? There is a similar issue with _Unwind_Backtrace I encounter from time to time.

@jonas-schievink
Copy link
Contributor

This is unsound, so I'm not sure if it should be offered as a regular command line flag. What's the use case of writing a no_std MSVC binary?

@Mark-Simulacrum Mark-Simulacrum added C-feature-request Category: A feature request, i.e: not implemented / a PR. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Oct 24, 2019
@Lokathor
Copy link
Contributor Author

@jonas-schievink Producing a maximally portable binary. Or even just as a "C parity" issue. We can already do all sorts of unsound things with the command line flags, this wouldn't be breaking new ground.

@hellow554 I'm lead to believe that the stack itself can be fragmented because of how rust/llvm builds things, so it's not sane to make it simply "do nothing", but I will experiment with such a brute force approach when I have time and report back.

@memoryruins
Copy link
Contributor

rustc has a deprecated flag no-stack-check

-C no-stack-check -- the `--no-stack-check` flag is deprecated and does nothing
// previously     -- disable checks for stack exhaustion (a memory-safety hazard!)

but looking further it doesn't seem like it ever affected __chkstk bc3831b#commitcomment-8110134
If you have a custom target.json, e.g. with xargo, try adding "stack_probes": true, like in #56913
Note: I have no idea if that will work.

@retep998
Copy link
Member

You need stack probes on Windows, otherwise functions that use over one page of stack space might overstep the guard page and do horrible things. Please don't pursue this course of action.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants