Skip to content

waf: make --asan work with gcc - #34779

Closed
shine-mild wants to merge 2 commits into
ArduPilot:masterfrom
shine-mild:pr/asan-gcc-build
Closed

shine-mild wants to merge 2 commits into
ArduPilot:masterfrom
shine-mild:pr/asan-gcc-build

Conversation

@shine-mild

@shine-mild shine-mild commented Oct 15, 2026 •

Copy link
Copy Markdown

Summary

./waf configure --board sitl --debug --asan currently only works with clang. With gcc the build fails in two places; this PR makes the gcc path work.

  1. Link flags (Tools/ardupilotwaf/boards.py): -fsanitize=address was added to LINKFLAGS only when clang++ was the compiler, so with gcc every object is instrumented and the final link fails with hundreds of undefined reference to '__asan_report_load8' etc. Add the flag whenever --asan is given.
  2. RCProtocolTest frame-size check: ASan redzones inflate stack frames, so the example's -Wframe-larger-than=2000 pragma fails (-Werror, frame becomes ~3040 bytes). Skip the check when __SANITIZE_ADDRESS__ is defined. (The equivalent EKF check is already limited to ChibiOS on master, so no change is needed there.)

The --asan help text already says clang is required; with these changes gcc 15 builds and runs arducopter, examples/RCProtocolTest and tests/test_gsof with ASan instrumentation.

Note

Example/test binaries (APM_BUILD_UNKNOWN) still trip UBSan's null check at startup via AP_Vehicle& vehicle = *AP_Vehicle::get_singleton(); (AP_Vehicle.cpp:305) when --ubsan-abort is also enabled. That is unrelated to ASan and left for a separate change; -fno-sanitize=null is a local workaround.

Testing

CC=gcc CXX=g++ ./waf configure --board sitl --debug --asan --ubsan-abort && ./waf copter --target examples/RCProtocolTest,tests/test_gsof on Ubuntu 26.04 / gcc 15.2: links and runs; a deliberate stack overflow test program confirms the ASan runtime is active.

🤖 Generated with Claude Code

@khancyr khancyr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.
Test change don't impact flight code so no worries on this.

if asan is now working with gcc we need to remove the comment on g.add_option('--asan') that state that is for clang.

I was able to build copter with gcc 15 and gcc 13 and asan but not running it :

╰─$ /home/pierre/Workspace/ardupilot/ardupilot/build/sitl/bin/arducopter --model + --speedup 1 --slave 0 --sim-address=127.0.0.1 -I0
Setting SIM_SPEEDUP=1.000000
../../libraries/AP_Param/AP_Param.cpp:1508:32: runtime error: 65535 is outside the range of representable values of type 'short int'

So I would say to just remove the config comment and we should be able to use this ! Thanks

@khancyr

khancyr commented Oct 15, 2026

Copy link
Copy Markdown
Contributor

collide with #33976

shinedream1 and others added 2 commits October 15, 2026 22:14
-fsanitize=address was only added to LINKFLAGS when the compiler was
clang++, so "CC=gcc CXX=g++ ./waf configure --board sitl --debug --asan"
compiled every object with ASan instrumentation and then failed to link
with hundreds of undefined references to __asan_*. Add the link flag
whenever --asan is given.

Co-Authored-By: Claude Mythos 5.1 <noreply@anthropic.com>
The example enforces -Wframe-larger-than=2000 on loop(); ASan redzones
push the frame to ~3040 bytes and the --asan build fails. Keep the
check for non-ASan builds.

Co-Authored-By: Claude Mythos 5.1 <noreply@anthropic.com>
@shine-mild

Copy link
Copy Markdown
Author

Thanks for the review. Updated the --asan help text to say gcc or clang both work (force-pushed).

The runtime error you hit (AP_Param.cpp: 65535 is outside the range of representable values of type 'short int') is the SITL relay-mask default going through a float into an AP_Int16; the fix for that is in #34778 together with a SoftSerial shift UB. With both applied, SITL starts and runs under --asan --ubsan-abort with gcc 15 here.

On the overlap with #33976: that PR carries the same link-flag change plus the C-side instrumentation and lsan symbol handling, which is more complete. I'm happy to drop the boards.py hunk here and keep only the RCProtocolTest frame-size guard if #33976 is going to land first, or close this one entirely if you'd prefer to take it all through #33976.

@github-actions

github-actions Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Global Size Summary (Total Flash delta in bytes)

Built from 4d6589e rebased onto b032bcb

Board Periph tracker blimp bootloader copter heli iofw plane rover sub
DrotekP3Pro _ _ _ * _ _ _ _ _ _
Durandal _ * * _ * * _ * * *
Hitec-Airspeed * _ _ _ _ _ _ _ _ _
KakuteF7 _ * * _ * * _ * * *
MambaH743v4 _ * * _ * * _ * * *
MatekF405-CAN _ _ _ * _ _ _ _ _ _
MatekF405 _ * * _ * * _ * * *
MatekH743-bdshot _ * * _ * * _ * * *
Pixhawk1-1M _ * * _ * * _ * * *
disco _ * * _ * * _ * * *
f103-GPS * _ _ _ _ _ _ _ _ _

@shine-mild

Copy link
Copy Markdown
Author

Closing: #33976 has been merged and master now carries all of this (the gcc link flag, the __SANITIZE_ADDRESS__ guard in RCProtocolTest and the updated --asan help text). Thanks for the review, @khancyr.

@shine-mild shine-mild closed this Oct 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants