sanitizers: actually enable UBSAN#13293
Conversation
|
I'd suggest to also backport this to the 5.2 and 4.14 branches, I can open PRs for that if desired once this one is reviewed/merged. |
|
I notice there is an earlier PR that modifies this script too. |
|
Thanks a lot for the work and the very clear and readable explanations!
#13219 just got merged so it should be possible to rebase.
May I please aksk to send a comment here when done?
|
5bc4b9c to
814c007
Compare
|
Rebased and test passed in the Inria CI: https://ci.inria.fr/ocaml/job/precheck-sanitizers/6/ This is a pure CI PR, so I think For reference here is how a ubsan error report looks like from the CI logs (stacktraces are working and you get file:line information): And here is how an ASAN error report looks like: |
|
Török Edwin (2024/07/19 06:41 -0700):
Rebased and test passed in the Inria CI:
https://ci.inria.fr/ocaml/job/precheck-sanitizers/6/
Thanks!
This is a pure CI PR, so I think `no-change-entry-needed` is also
needed as a label.
I did add the label, I think it would not be unfair to add a Changes
entry for this PR, just for the sake of crediting its author. As you
prefer.
Overall I very much like this PR, both what it does and how it does it
(in a series of super easy to review commits, with all the explanations
needed).
I have one nit pick and two very minor quesitons.
Nitpick: would youmind ensuring thatall the commit messages start with
an uppercase letter, please?
Not that I am proud of this obsession, but apparently I have it.
Re: you commit: `sanitizers: add a test that ASAN/UBSAN works`
I was wondering whether one wouldn't want to remove all the files after
the test has been completed?
I was also a bit uncomfortable with the use of singular and plural in
the commit title. Perhaps I'd have felt better with
Sanitizers: add a test that ASAN/UBSAN work (no s) for isntance...
Also, re:the commit `sanitizers: add -g flag`, I was unsure if `-g`
shouldn't be added to `LDFLAGS`, too, but I guess it's not super
important since the next commit adds `-Og` to both `CFLAGS` and
`LDFLAGS`.
Thanks again for your great work!
|
The clang-14 docs are not clear about this, but the latest docs say: > -fsanitize-trap= and -fsanitize-recover= are a no-op in the absence of a -fsanitize= option. There is no unused command line option warning. Avoid this trap and specify `-fsanitize=$ubsan` too. Signed-off-by: Edwin Török <edwin.torok@cloud.com>
Apparently >-O0 needs to be supplied when linking too: ``` clang: warning: the object size sanitizer has no effect at -O0, but is explicitly enabled: -fsanitize=bool,builtin,bounds,enum,nonnull-attribute,nullability,object-size,pointer-overflow,returns-nonnull-attribute,shift-exponent,unreachable [-Winvalid-command-line-argument] ``` Signed-off-by: Edwin Török <edwin.torok@cloud.com>
Move compiler flag definitions to shell variables. This will enable testing them. No functional change yet. Signed-off-by: Edwin Török <edwin.torok@cloud.com>
Compile 2 small test programs, and check expected output. The test programs are deleted on success. Signed-off-by: Edwin Török <edwin.torok@cloud.com>
Still stop on first error, but instead of dumping core print a message, and stacktrace, like ASAN. This may be more convenient when used in a CI than a coredump. Signed-off-by: Edwin Török <edwin.torok@cloud.com>
For better stacktraces. Signed-off-by: Edwin Török <edwin.torok@cloud.com>
814c007 to
7c08154
Compare
This is currently equivalent to -O1 on clang, but may improve debuggability in the future compared to -O1. GCC already disables certain optimizations at -Og, and recommends it when using the sanitizers: ``` To get more accurate stack traces, it is possible to use options such as -O0, -O1, or -Og (which, for instance, prevent most function inlining) ``` (there are also other flag recommendations, but I think they are only relevant if you want to use -O2 or higher, they wouldn't be active at -O1/-Og) Also don't use -O0. Even for pure debugging, the manual recommends -Og: ``` It is a better choice than -O0 for producing debuggable code because some compiler passes that collect debug information are disabled at -O0 ``` Signed-off-by: Edwin Török <edwin.torok@cloud.com>
7c08154 to
307f1e1
Compare
Thanks, adding a Changes entry has a tendency to conflict,
I updated the commit messages to match your suggestions.
I've added an
I think that |
|
Many thanks for your prompt andgood willing repsonse!
I will approve now and merge as soon as CI is done.
|
|
Thanks for the review! (I think I have a few more sanitizer related PRs, I'll update those later). |
|
Török Edwin (2024/07/19 10:05 -0700):
Thanks for the review! (I think I have a few more sanitizer related
PRs, I'll update those later).
You're welcome, and happy to review other PRs along these lines.
|
While working on #13290 (comment) I noticed that UBSAN is not active.
Using
-fsanitize-trapis a trap: it doesn't actually do anything on its own (I would've expected it to at least warn me, but it doesn't).The latest https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html#usage documents this:
The fix is simple: use
-fsanitize=$ubsan -fsanitize-trap=$ubsaninstead of just-fsanitize-trap=$ubsan.Interestingly it was then complaining that
-O0is not valid for-fsanitize=object-size(this extra message would cause a testsuite failure), and the only way to fix that is to put an-O1intoLDFLAGS.To avoid falling into similar traps I've added 2 small tests in the CI script itself, which compiles 2 known buggy C programs, and checks whether the sanitizers find the expected bug (fails early otherwise).
I've also replaced
-fsanitize-trapwith-fno-sanitize-recover=allandUBSAN_OPTIONS=print_stacktrace=1. This should make UBSAN work a bit like ASAN, I think in the CI a stacktrace is better than a coredump.If a coredump is desired for local debugging then the script can be changed to
UBSAN_OPTIONS="abort_on_error=1".Successful run in Inria's CI: https://ci.inria.fr/ocaml/job/precheck-sanitizers/2/consoleText
(sorry I couldn't figure how to create a user, so created/launched it as anonymous user)
This PR probably doesn't need a Changes entry, it only changes the CI.