llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.82k stars 11.91k forks source link

Windows implementation of segmented stacks uses invalid TCB slot #21455

Open llvmbot opened 10 years ago

llvmbot commented 10 years ago
Bugzilla Link 21081
Version trunk
OS Linux
Reporter LLVM Bugzilla Contributor
CC @rnk

Extended Description

The implementation of segmented stacks for i686 and x86_64 Windows uses the 'pvArbitrary' (aka 'ArbitraryUserPointer') field of the TCB to store the stack limit ($fs:0x14 on win32 or $gs:0x28 on win64).

As indicated by Raymond Chen1 this slot is, contrary to some information, not free for use by applications, and will cause various mysterious bugs.

This has been a major source of pain in Rust24, which at this point only uses segmented stacks for stack overflow protection, and is presently switching to stack probes instead.

I'm not aware of other candidate TCB slots to use for this purpose, so segmented stacks on Windows may not be viable with the current technique.

rnk commented 10 years ago

Well that's unfortunate.

We could do slightly better by always using the last static TLS slot in the TEB, ie TlsSlots[63]. That would still require the runtime to carefully mark that slot used in the free TLS bitmap during startup, which isn't very user friendly.

The "normal" Unix-y thing to do is to create a thread-local variable with the generaldynamic TLS model described in LLVM langref, but I don't think that's implementable on Windows without heavy function calls on every access.