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

ASLR on Windows breaks thread-local variables #17684

Closed
thestinger opened this issue Oct 1, 2014 · 7 comments · Fixed by #75406
Closed

ASLR on Windows breaks thread-local variables #17684

thestinger opened this issue Oct 1, 2014 · 7 comments · Fixed by #75406
Labels
A-security Area: Security (example: address space layout randomization). A-thread-locals Area: Thread local storage (TLS) C-bug Category: This is a bug. O-windows-gnu Toolchain: GNU, Operating system: Windows T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@thestinger
Copy link
Contributor

This appears to be a MinGW-w64 linker bug.

@thestinger thestinger added the A-security Area: Security (example: address space layout randomization). label Oct 1, 2014
@thestinger thestinger added the O-windows Operating system: Windows label Oct 1, 2014
@steveklabnik
Copy link
Member

Triage: I am not sure if there has been any change, nor steps to reproduce.

@brson
Copy link
Contributor

brson commented Feb 6, 2016

If this was a mingw-specific bug, we may be able to reactivate it on the msvc builds.

@retep998
Copy link
Member

retep998 commented Feb 6, 2016

According to MSDN By default, /DYNAMICBASE is on. so we've already been effectively using ASLR with -msvc targets.

@thestinger thestinger changed the title ASLR on Windows breaks TLS ASLR on Windows breaks thread-local variables Jun 23, 2016
@Mark-Simulacrum
Copy link
Member

Can someone comment on whether thread locals are broken on Windows MinGW-w64 today? What are the reproduction steps?

@retep998 retep998 added O-windows-gnu Toolchain: GNU, Operating system: Windows and removed O-windows Operating system: Windows labels May 6, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-bug Category: This is a bug. label Jul 22, 2017
@sipsorcery
Copy link

sipsorcery commented Oct 2, 2017

I've done a cursory check of TLS with 64 bit binaries compiled with msvc and mingw32-g++ and both are working correctly.

// http://en.cppreference.com/w/cpp/language/storage_duration
// msvc on win64: cl tls.cpp /link
// mingw on linux64: x86_64-w64-mingw32-g++ tls.cpp -static -static-libstdc++

#include <iostream>
#include <string>
#include <thread>
#include <mutex>

thread_local unsigned int rage = 1;
std::mutex cout_mutex;

void increase_rage(const std::string& thread_name)
{
	++rage; // modifying outside a lock is okay; this is a thread-local variable
	std::lock_guard<std::mutex> lock(cout_mutex);
	std::cout << "Rage counter for " << thread_name << ": " << rage << '\n';
}

int main()
{
	std::thread a(increase_rage, "a"), b(increase_rage, "b");

	{
		std::lock_guard<std::mutex> lock(cout_mutex);
		std::cout << "Rage counter for main: " << rage << '\n';
	}

	a.join();
	b.join();

	getchar();
}

msvc output:

f:\Temp>tls
Rage counter for a: 2
Rage counter for b: 2
Rage counter for main: 1

mingw32-g++ output:

f:\Temp>tls_mingw.exe
Rage counter for main: 1
Rage counter for a: 2
Rage counter for b: 2

64 bit binaries on Windows use ASLR by default unless explicitly disabled with a linker option. Both binaries in this test had the NX Compatible flag set in the executable image header.

Happy to test further if anyone has a pointer to the problem code.

@retep998
Copy link
Member

retep998 commented Oct 2, 2017

@sipsorcery The key thing is #[thread_local] statics in Rust. The NX compatible bit does not indicate ASLR, rather it needs to have the Dynamic base bit.

@jonas-schievink jonas-schievink added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-thread-locals Area: Thread local storage (TLS) labels Jan 12, 2020
@mati865
Copy link
Contributor

mati865 commented Aug 11, 2020

Does anybody have reproducer?
Example from the docs still works for me after enabling ASLR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-security Area: Security (example: address space layout randomization). A-thread-locals Area: Thread local storage (TLS) C-bug Category: This is a bug. O-windows-gnu Toolchain: GNU, Operating system: Windows T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants