Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specify sanitizers using IGN_SANITIZERS cmake variable #210

Merged
merged 9 commits into from
Apr 1, 2022

Conversation

j-rivero
Copy link
Contributor

@j-rivero j-rivero commented Mar 11, 2022

🎉 New feature

Part of gazebo-tooling/release-tools#404

Summary

The PR integrates the sanitizers compilers flags into ignition-cmake consumers to facilitate how to run them. It adds a CMake option of IGN_SANITIZERS that can be populated with different kind of sanitizers.

The work mostly integrated the code from https:/StableCoder/cmake-scripts#sanitizer-builds-sanitizerscmake.

I added a test to run against the FAKE_INSTALL installation that checks that the compiler flag is being added to the compiler when using the CMake parameter.

Test it

It can be used directly without colcon by passing -DIGN_SANITIZERS=Address or using colcon together with for example ignition-transport11:

colcon build --merge-install --executor sequential --cmake-args -DIGN_SANITIZER=Address -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON --event-handlers console_direct+
colcon test --merge-install

Some tests are failing due to problems with the sanitizer:

33: [ RUN      ] ignTest.ServiceRequest                                                                                                                                                                                           
33: /home/jrivero/code/ignition/ws_asan/src/ign-transport/src/cmd/ign_TEST.cc:390: Failure                                                                                                                                        
33: Expected equality of these values:                                                                                                                                                                                            
33:   output                                                                                                                                                                                                                      
33:     Which is: "Library error: libignition-tools-backward.so not found. Improved backtrace generation will be disabled\ndata: 10\n\n\n=================================================================\n==109340==ERROR: LeakSanitizer: detected memory leaks\n\nDirect leak of 64 byte(s
33:   "data: " + value + "\n\n"                                                                                                                                                                                                   
33:     Which is: "data: 10\n\n"                                                                                                                                                                                                  
33: With diff:                                                                                                                                                                                                                    
33: @@ -1,20 +1,2 @@                                                                                                                                                                                                              
33: -Library error: libignition-tools-backward.so not found. Improved backtrace generation will be disabled                                                                                                                       
33:  data: 10                                                                                                                                                                                                                     
33: -                                                                                                                                                                                                                             
33: -                                                                                                                                                                                                                             
33: -=================================================================                                                                                                                                                            
33: -==109340==ERROR: LeakSanitizer: detected memory leaks                                                                                                                                                                        
33: -                                                                                                                                                                                                                             
33: -Direct leak of 64 byte(s) in 2 object(s) allocated from:                                                                                                                                                                     
33: -    #0 0x7f632490a587 in operator new(unsigned long) ../../../../src/libsanitizer/asan/asan_new_delete.cc:104                                                                                                                
33: -    #1 0x7f63243a7ee9 in ignition::msgs::Int32* google::protobuf::Arena::CreateMaybeMessage<ignition::msgs::Int32>(google::protobuf::Arena*) (/lib/x86_64-linux-gnu/libignition-msgs8.so.8+0x226ee9)                         
33: -    #2 0x5591192b87b6 in cmdServiceReq /home/jrivero/code/ignition/ws_asan/src/ign-transport/src/cmd/ign.cc:240                                                                                                              
33: -    #3 0x5591191db204 in std::function<void ()>::operator()() const /usr/include/c++/9/bits/std_function.h:688                                                                                                               
33: -    #4 0x5591191db204 in CLI::App::run_callback(bool) /usr/include/ignition/utils1/ignition/utils/cli/App.hpp:1918                                                                                                           
33: -    #5 0x5591191db204 in CLI::App::run_callback(bool) /usr/include/ignition/utils1/ignition/utils/cli/App.hpp:1898                                                                                                           
33: -    #6 0x55911929435e in CLI::App::parse(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&&) /usr/include/ignition/utils1/ignition/utils/
33: -    #7 0x55911929435e in CLI::App::parse(int, char const* const*) /usr/include/ignition/utils1/ignition/utils/cli/App.hpp:1233                                                                                               
33: -    #8 0x5591191c6eeb in main /home/jrivero/code/ignition/ws_asan/src/ign-transport/src/cmd/service_main.cc:134                                                                                                              
33: -    #9 0x7f63237730b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x240b2)                                                                                                                                         
33: -                                                                                                                                                                                                                             
33: -SUMMARY: AddressSanitizer: 64 byte(s) leaked in 2 allocation(s).\n                                                                                                                                                           
33: +\n                                                                                                                                                                                                                           
33:·                                                                                                                                                                                                                              
33: [  FAILED  ] ignTest.ServiceRequest (494 ms)                                                                                     

Checklist

Note to maintainers: Remember to use Squash-Merge and edit the commit message to match the pull request summary while retaining Signed-off-by messages.

@github-actions github-actions bot added Gazebo 1️1️ Dependency of Gazebo classic version 11 🌱 garden Ignition Garden 🏢 edifice Ignition Edifice 🏯 fortress Ignition Fortress 🏰 citadel Ignition Citadel labels Mar 11, 2022
@chapulina chapulina requested a review from azeey March 14, 2022 18:38
Copy link
Contributor

@azeey azeey left a comment

Choose a reason for hiding this comment

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

Looks great! Just a couple minor comments. I tried it on sdformat and it worked great except the tests that use ign which gave the error:

ASan runtime does not come first in initial library list; you should either link runtime to your application or manually preload it with LD_PRELOAD.

I think this is because the libraries are loaded through ruby when using ign.

When setting up CI, we'll have to find a way to ignore tests that run ign.

cmake/IgnSanitizers.cmake Outdated Show resolved Hide resolved
endif()
append("${SANITIZER_MEM_FLAG}" SANITIZER_SELECTED_FLAGS)

if(AFL)
Copy link
Contributor

Choose a reason for hiding this comment

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

AFL is not being used here, so we can remove this if block. Here and elsewhere below.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True. I'm in favor of leave it in place just in the case that someone (or even us in the future) is using that support now that we have it implemented.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sure. I'm okay with that, but I haven't tested it. Not sure if you've had a chance.

@j-rivero
Copy link
Contributor Author

I think this is because the libraries are loaded through ruby when using ign.
When setting up CI, we'll have to find a way to ignore tests that run ign.

Correct, we would need to exclude them somehow when executing the test suite with the sanitizers. Not sure right now that we can do anything for them in this ign-cmake support.

@j-rivero
Copy link
Contributor Author

Made a little change from option to set in f169762. option is designed to work mostly for ON/OFF, using it for a string support can deal to unexpected behaviors.

Signed-off-by: Jose Luis Rivero <[email protected]>
@j-rivero
Copy link
Contributor Author

Found the place to update the documentation and place a copy of the original docs about how to use the new parameter c1bc190

Copy link
Contributor

@azeey azeey left a comment

Choose a reason for hiding this comment

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

Looking good. I think we should clarify which CMake variable to use after the latest changes. Also Windows CI is failing.

endif()
append("${SANITIZER_MEM_FLAG}" SANITIZER_SELECTED_FLAGS)

if(AFL)
Copy link
Contributor

Choose a reason for hiding this comment

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

Sure. I'm okay with that, but I haven't tested it. Not sure if you've had a chance.

- [MemorySanitizer](https://clang.llvm.org/docs/MemorySanitizer.html) detects uninitialized reads.
- [Control Flow Integrity](https://clang.llvm.org/docs/ControlFlowIntegrity.html) is designed to detect certain forms of undefined behaviour that can potentially allow attackers to subvert the program's control flow.

These are used by declaring the `IGN_USE_SANITIZER` CMake variable as string containing any of:
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure what the correct CMake variable is: I see USE_SANITIZER and IGN_SANITIZER in the cmake code, but not IGN_USE_SANITIZER.

examples/CMakeLists.txt Outdated Show resolved Hide resolved
cmake/IgnSanitizers.cmake Outdated Show resolved Hide resolved
Signed-off-by: Jose Luis Rivero <[email protected]>
@j-rivero
Copy link
Contributor Author

Looking good. I think we should clarify which CMake variable to use after the latest changes. Also Windows CI is failing.

ok, it is not a good to make changes without caffeine. Sorry for the mess, IGN_SANITIZER is the right one, I think I fixed all the occurrences in 86f2091

Signed-off-by: Jose Luis Rivero <[email protected]>
@scpeters scpeters changed the title Ign sanitizers Specify sanitizers using IGN_SANITIZERS cmake variable Mar 30, 2022
Copy link
Contributor

@azeey azeey left a comment

Choose a reason for hiding this comment

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

LGTM!


Note: Address sanitizer may have an impact on the performance of execution.
`IGN_SANITIZER` CMake parameter can be used with different compilers to support the detection of different problems in the code.
[Check the documentation for `IGN_SANITIZER` flag](ign_cmake_sanitizers.md)
Copy link
Contributor

Choose a reason for hiding this comment

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

We need to add ign_cmake_sanitizers.md to tutorials.md.in

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The problem we have is that the ign_cmake_sanitizers is not really a tutorial but more a documentation on how to use the feature. Not sure if we can consider it as a "tutorial". It is referenced from the "Developing with Ignition CMake" and place inside the tutorials since I did not find a better place for it.

Copy link
Contributor

Choose a reason for hiding this comment

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

We've been using the term "tutorial" to refer to all kinds of documentation, guides, instructions, etc. So I wouldn't worry too much about it. It's good to give the document more visibility by placing it on the main page.

In any case, note that we're not publishing tutorials for ign-cmake on the main site yet:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks @azeey and @chapulina for the information. I added the entry in c55d115

@j-rivero j-rivero merged commit ca66305 into ign-cmake2 Apr 1, 2022
@j-rivero j-rivero deleted the ign_sanitizers branch April 1, 2022 16:38
harshmahesheka pushed a commit to harshmahesheka/ign-cmake that referenced this pull request Apr 5, 2022
* Support for running compiler sanitizers

Signed-off-by: Jose Luis Rivero <[email protected]>
Co-authored-by: Addisu Z. Taddese <[email protected]>
Signed-off-by: Harsh Mahesheka <[email protected]>
@osrf-triage
Copy link

This pull request has been mentioned on Gazebo Community. There might be relevant details there:

https://community.gazebosim.org/t/new-releases-2022-04-27-fortress-citadel/1389/1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏰 citadel Ignition Citadel 🏢 edifice Ignition Edifice 🏯 fortress Ignition Fortress 🌱 garden Ignition Garden Gazebo 1️1️ Dependency of Gazebo classic version 11
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants