Skip to content

[Question] Shouldn't TSan add the flag "-pie"? #13

@j1elo

Description

@j1elo

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:

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions