Native compilation with -fsanitize=thread flag

Hi guys,

I’m working on a native configuration of Luos and I would like to perform some sanity check.
To do so I want to use the -fsanitize=thread flag to check on data race.
On my platformio.ini I added some flag like this :

build_flags =
    ...
    -fsanitize=thread
    -Wl,-fsanitize=thread

I’m on mac M1 and gcc --version give me :

Apple clang version 14.0.0 (clang-1400.0.29.202)
Target: arm64-apple-darwin21.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

When I compile using this flag I get the error : ld: unknown option: -fsanitize=thread

So I tried to verbose build to check the link command and the line sounds good :

gcc -o .pio/build/native/program -Wl,-fsanitize=thread -Og -g2 -ggdb2 .pio/build/native/src/main.o -L.pio/build/native .pio/build/native/libdf9/libluos_engine.a .pio/build/native/libb5c/libpipe.a .pio/build/native/lib440/libgate.a .pio/build/native/lib16b/libButton.a

I’m not sure that the -Wl, should be on my command…
I tried to run the command on my terminal without the -Wl, and I got some strange incompatibility errors such as :

ld: warning: ignoring file .pio/build/native/lib16b/libButton.a, building for macOS-arm64 but attempting to link with file built for macOS-x86_64
ld: warning: ignoring file .pio/build/native/src/main.o, building for macOS-arm64 but attempting to link with file built for unknown-x86_64
...

Did anyone have any thoughts about it?

What linking errors does it give without that?

I got errors indicating that the linker script doesn’t know some sanitizer functions for x86_64 (but I’m in ARM64):

Undefined symbols for architecture x86_64:
  "___tsan_func_entry", referenced from:
      _main in main.o
      _clear_screen in libButton.a(button.o)
      _Button_Init in libButton.a(button.o)
      _kbhit in libButton.a(button.o)
      _Button_Loop in libButton.a(button.o)
      _Gate_Init in libgate.a(gate.o)
      _Gate_Loop in libgate.a(gate.o)
      ...
  "___tsan_func_exit", referenced from:
      _clear_screen in libButton.a(button.o)
      _Button_Init in libButton.a(button.o)
      _kbhit in libButton.a(button.o)
      _Button_Loop in libButton.a(button.o)
      _Gate_Init in libgate.a(gate.o)
      _Gate_Loop in libgate.a(gate.o)
      _TimeOD_TimeTo_ms in libgate.a(gate.o)
...

I see. Maybe the final linker command needs just -fsanitize=thread, not -Wl,...

Can you instead of

Create the file add_sanatizer.py in the root of the project with content

Import("env")
env.Append(CCFLAGS=["-fsanitize=thread"], LINKFLAGS=["-fsanitize=thread"])

then add

extra_scripts = add_sanatizer.py

in the platformio.ini.

1 Like

It worked!
Thank you @maxgerhardt.