Skip to content

Instantly share code, notes, and snippets.

@jhurliman
Created May 8, 2018 17:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhurliman/2598ea8fc9f0ca18a556db2b91839fac to your computer and use it in GitHub Desktop.
Save jhurliman/2598ea8fc9f0ca18a556db2b91839fac to your computer and use it in GitHub Desktop.
Directly access a compiler-generated Thread Local Storage guard variable
// Tested with clang-6.0 on Ubuntu 14.04 using:
// clang --std=c++14 -stdlib=libstdc++ -lstcd++ -fasm-blocks -O3 tls_guardvar.cpp -o tls_guardvar
// I will be genuinely surprised if this works in any other circumstance.
#include <iostream>
struct S {
int x;
S() { x = 42; }
};
int main() {
uint32_t value1;
uint32_t value2;
__asm {
mov eax, fs:[0xfffffffffffffffc]
mov value1, eax
}
static thread_local S s;
__asm {
mov eax, fs:[0xfffffffffffffffc]
mov value2, eax
}
std::cout << "value1=" << value1 << ", value2=" << value2 << ", s.x=" << s.x << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment