Skip to content

ASan Support#3918

Closed
tiennou wants to merge 2 commits into
libgit2:mainfrom
tiennou:asan
Closed

ASan Support#3918
tiennou wants to merge 2 commits into
libgit2:mainfrom
tiennou:asan

Conversation

@tiennou
Copy link
Copy Markdown
Contributor

@tiennou tiennou commented Aug 29, 2016

Extracted from #3883, with CMake support thanks to arsenm/sanitizers-cmake.

Add -DASAN=ON -DBUILD_CLAR=ON to your favorite CMake invocation.

Notes:

  • This only enable ASan on libgit2_clar, so the main library isn't instrumented.
  • It blanket-enables all sanitizers (eg. address, thread, memory and undefined). Apple's clang only supports address atm, so I'm interested to see what happens on the Linux build 😉
  • If your compiler doesn't support some of the sanitizers, cmake will spew warnings, which may or may not be wanted.

Comment thread include/git2/common.h Outdated

#if defined(__has_feature)
#if __has_feature(address_sanitizer)
#define GIT_ASAN 1
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not sure I understand what's going on here. When is __has_feature(address_sanitizer) true? It seems like it's true when address_sanitizer is available, not when libgit2 was built with it, no?

If your goal is to provide some functionality to callers about whether libgit2 was built with address_sanitizer, then please have cmake set -DGIT_ASAN and expose a function that will check and return that. (Probably a query key on git_libgit2_opts makes sense.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The feature is enabled when AddressSanitizer is available and currently being used for the current compile run.

The CMake modules take care of automatically passing in the correct flags to the compiler when you -DASAN=ON, which will in turn cause that define to exist. The ultimate goal is to be able to tell at compile-time whether ASan is enabled or not (because the modules don't set anything since there's a clang feature macro for it), so we can skip the oom test below (e.g. doing weird things that makes ASan unhappy, like allocating the whole memory). That's the only point of it actually, so if having that in common.h feels wrong, I can move it somewhere else.

I don't really get the point about providing functionality. Because ASan incurs a typical 2x slowdown, it's not something I expect to be queried at runtime. It can be used as a debugging tool (build libgit2 with ASan, link your app with that version, run and wait).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Got it - yeah, I don't think that a public header is the best place for this. I would suggest src/common.h if you do want to put this in a header file.

But my preference would be to put this in the CMakeLists.txt itself, which I think makes it a bit more discoverable. (And there's some prior art there - the similar functionality of DEBUG_POOL and MSVC_CRTDBG just fiddles compiler flags.)

I would just put:

ADD_DEFINITIONS(-D GIT_ASAN)

in the IF (ASAN) block.

Comment thread CMakeLists.txt Outdated
@tiennou
Copy link
Copy Markdown
Contributor Author

tiennou commented Jan 14, 2019

Rebased (ping @lhchavez).

Caveats/notes :

  • I've removed -DASAN in favor of the (module-provided) individual SANITIZE_.* flags, since there are compatibility issues between each.
  • It should work on the library as well (though there's no selection possible). YMMV.
  • Sanitizers shows up in the CMake feature list.
  • It seems there's no way to know if a specific sanitizer could be successfully enabled. It might be possible to unset the "offending" variable, but it's in nested functions so scoping might be an issue. This can make both the feature list wrong, or impact pkgconfig generation.
  • It can be submoduled. Dunno if we want that though.

(Speaking about pkgconfig generation, I've written a module to do this for the mbedTLS project)

Copy link
Copy Markdown
Contributor

@lhchavez lhchavez left a comment

Choose a reason for hiding this comment

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

works for me! thanks for making this change :D

Comment thread CMakeLists.txt
Comment thread tests/CMakeLists.txt Outdated
@tiennou
Copy link
Copy Markdown
Contributor Author

tiennou commented Apr 4, 2019

Rebased, I've added the -DSANITIZERS=address,whatever flag. Do we want that as part of CI/fuzzing ? I have no idea how fuzzing works though…

Copy link
Copy Markdown
Contributor

@lhchavez lhchavez left a comment

Choose a reason for hiding this comment

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

I'd advocate for enabling a subset of the sanitizers when fuzzing since that catches a lot more bugs. But we (most likely I) can enable that later if you don't want to do it now.

I wrote some docs for how to use fuzzing: https://github.com/libgit2/libgit2/blob/master/docs/fuzzing.md

The CI build adds the USE_STANDALONE_FUZZER, which only lets you run the pre-canned corpus to avoid taking too much time.

OSS Fuzz builds things a bit differently, though: https://github.com/google/oss-fuzz/blob/master/projects/libgit2/build.sh

@tiennou
Copy link
Copy Markdown
Contributor Author

tiennou commented Apr 5, 2019

But we (most likely I) can enable that later if you don't want to do it now.

I'll happily defer that to you, then 😉. Feel free to push those tweaks there or ping me to cherrypick something, in case merging takes longer.

@pks-t
Copy link
Copy Markdown
Member

pks-t commented Apr 5, 2019

The OSS project already enables sanitizers by itself, so there's nothing we need to change there. And I don't think it makes a lot of sense for our standalone fuzzers: they're mainly intended as a way to verify that our fuzzers build, all the real work is deferred to OSS Fuzz.

Base automatically changed from master to main January 7, 2021 10:09
@ethomson ethomson dismissed a stale review via 51bbbc8 July 27, 2021 14:18
@ethomson
Copy link
Copy Markdown
Member

I'm going through some older PRs — I think that this is quite outdated, apologies. We have a lot of fuzzing in main, but I'd love to add more if there's something here that we're missing.

@ethomson ethomson closed this Dec 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants