-
Notifications
You must be signed in to change notification settings - Fork 69
Description
Seems that ThreadSanitizer would need that the source code is compiled and linked with position-independent object code. Source: https://github.com/google/sanitizers/wiki/ThreadSanitizerDevelopment
your/fresh/gcc test.c -fsanitize=thread -g -O1 -fPIE -pie
This is a summary of how to use those options:
- Sources for shared libraries are compiled as Position Independent Code, with the option -fPIC.
- Then, shared libraries are linked also with option -fPIC.
- Sources for executables are compiled as Position Independent Executable, with the option -fPIE.
- Finally, objects of executables are linked with the options -fPIE -pie.
Example sources:
- https://wiki.debian.org/Hardening#DEB_BUILD_HARDENING_PIE_.28gcc.2Fg.2B-.2B-_-fPIE_-pie.29
- https://wiki.ubuntu.com/Security/Features#pie
- https://wiki.gentoo.org/wiki/Hardened/Toolchain
Building code for PIEs is achieved by adding '-fPIE' when compiling and '-fPIE -pie' when linking.
CMake chooses the appropriate compilation flag when the option CMAKE_POSITION_INDEPENDENT_CODE is set to ON (either -fPIC or -fPIE depending on the type of target). However, it does not do the same for the linker flags when creating the final executable.
The project Apache Arrow acknowledges this and adds the "-pie" option to the linker step:
https://github.com/apache/arrow/blob/master/cpp/cmake_modules/san-config.cmake#L75
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pie -fsanitize=thread")
Curiously enough, they don't add the option "-fPIE" to the linker flags, so their implementation may also be incomplete.
There seems to be a lot of confusion about this topic from around 2012 - 2014; I'm not sure of what was the conclusion of this topic but clearly some platforms such as Android seem to now enforce the usage of PIE executables. On the desktop, I don't know if the main Linux distributions have standardized on using PIE or not.
This is more an open call to discuss the issue rather than simply a request to add the "-pie" option to the compiler flags that get added by FindTSan.cmake.