Skip to content

Commit d3c1b50

Browse files
uilianriesczoido
andauthored
Add Sanitizers documentation (#4199)
* Add Sanitizers documentation Signed-off-by: Uilian Ries <uilianr@jfrog.com> * Add Sanitizers Signed-off-by: Uilian Ries <uilianr@jfrog.com> * Add more sections Signed-off-by: Uilian Ries <uilianr@jfrog.com> * Sanitizer setting is optional Signed-off-by: Uilian Ries <uilianr@jfrog.com> * Add toolchain and hook section Signed-off-by: Uilian Ries <uilianr@jfrog.com> * Improve description Signed-off-by: Uilian Ries <uilianr@jfrog.com> * Remove unsed header. Co-authored-by: Carlos Zoido <mrgalleta@gmail.com> * Update sanitizers warning * Improve document description Co-authored-by: Carlos Zoido <mrgalleta@gmail.com> * Remove Sanitizers with Conan hooks section Signed-off-by: Uilian Ries <uilianr@jfrog.com> * Add Sanitizers documentation Signed-off-by: Uilian Ries <uilianr@jfrog.com> * Add Sanitizers Signed-off-by: Uilian Ries <uilianr@jfrog.com> * Add more sections Signed-off-by: Uilian Ries <uilianr@jfrog.com> * Sanitizer setting is optional Signed-off-by: Uilian Ries <uilianr@jfrog.com> * Add toolchain and hook section Signed-off-by: Uilian Ries <uilianr@jfrog.com> * Improve description Signed-off-by: Uilian Ries <uilianr@jfrog.com> * Remove unsed header. Co-authored-by: Carlos Zoido <mrgalleta@gmail.com> * Update sanitizers warning * Improve document description Co-authored-by: Carlos Zoido <mrgalleta@gmail.com> * Remove Sanitizers with Conan hooks section Signed-off-by: Uilian Ries <uilianr@jfrog.com> * Add tables Signed-off-by: Uilian Ries <uilianr@jfrog.com> * Fix warning message Signed-off-by: Uilian Ries <uilianr@jfrog.com> * Reorganize reader flow 1 Signed-off-by: Uilian Ries <uilianr@jfrog.com> * Reestructured page Signed-off-by: Uilian Ries <uilianr@jfrog.com> * Add ABI compatibility section Signed-off-by: Uilian Ries <uilianr@jfrog.com> * Restore back sanitizers page Signed-off-by: Uilian Ries <uilianr@jfrog.com> * Add note about having multiple sanitizers in settings Signed-off-by: Uilian Ries <uilianr@jfrog.com> * Use Conan build command instead of install --build * Move sanitizers page to security folder Signed-off-by: Uilian Ries <uilianr@jfrog.com> * Update folder name for examples Signed-off-by: Uilian Ries <uilianr@jfrog.com> * Simplify index out of bounds commands Signed-off-by: Uilian Ries <uilianr@jfrog.com> * Simplify signed integer overflow commands Signed-off-by: Uilian Ries <uilianr@jfrog.com> * Be more consistent about mixing sanitizers Signed-off-by: Uilian Ries <uilianr@jfrog.com> * Improve environment variables descriptioN Signed-off-by: Uilian Ries <uilianr@jfrog.com> * Windows support Asan for both x86 and x64 Signed-off-by: Uilian Ries <uilianr@jfrog.com> * Separare sanitizers examples Signed-off-by: Uilian Ries <uilianr@jfrog.com> * Improve example title page Signed-off-by: Uilian Ries <uilianr@jfrog.com> * Improve example title page Signed-off-by: Uilian Ries <uilianr@jfrog.com> * Improve sanitizers title Signed-off-by: Uilian Ries <uilianr@jfrog.com> * Make explicit profile Signed-off-by: Uilian Ries <uilianr@jfrog.com> * Use explicit profiles Signed-off-by: Uilian Ries <uilianr@jfrog.com> * Fix profile names Signed-off-by: Uilian Ries <uilianr@jfrog.com> * Move sanitizer example to parent folder Signed-off-by: Uilian Ries <uilianr@jfrog.com> * Update example link reference Signed-off-by: Uilian Ries <uilianr@jfrog.com> * Explicit note about using cmake toolchain Signed-off-by: Uilian Ries <uilianr@jfrog.com> * Change clang profiles to gcc Signed-off-by: Uilian Ries <uilianr@jfrog.com> * Add GCC and MSVC to user_settings Signed-off-by: Uilian Ries <uilianr@jfrog.com> * Give back leak sanitizer to gcc Signed-off-by: Uilian Ries <uilianr@jfrog.com> --------- Signed-off-by: Uilian Ries <uilianr@jfrog.com> Co-authored-by: Carlos Zoido <mrgalleta@gmail.com>
1 parent c42c5e7 commit d3c1b50

File tree

5 files changed

+553
-0
lines changed

5 files changed

+553
-0
lines changed

examples.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ Examples
1616
examples/dev_flow
1717
examples/commands
1818
examples/runners
19+
examples/security

examples/security.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.. _examples_security:
2+
3+
Conan security examples
4+
=======================
5+
6+
.. toctree::
7+
:maxdepth: 2
8+
9+
security/sanitizers

examples/security/sanitizers.rst

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
.. _examples_security_sanitizers:
2+
3+
Using Compiler Sanitizers with Conan
4+
====================================
5+
6+
To better illustrate the :ref:`sanitizers integration with Conan <security_sanitizers>`,
7+
this section provides practical examples using AddressSanitizer (ASan) and
8+
UndefinedBehaviorSanitizer (UBSan) with simple C++ programs.
9+
10+
As a first step, please clone the sources to recreate this project. You can find them in the
11+
`examples2 repository <https://github.com/conan-io/examples2>`_ on GitHub:
12+
13+
.. code-block:: bash
14+
15+
git clone https://github.com/conan-io/examples2.git
16+
cd examples2/examples/security/sanitizers/compiler_sanitizers
17+
18+
In this example we will see how to prepare Conan to use sanitizers in different ways.
19+
20+
To show how to use sanitizers in your builds, let's consider two examples.
21+
22+
AddressSanitizer: index out of bounds
23+
-------------------------------------
24+
25+
In this example, we will build a simple C++ program that intentionally accesses an out-of-bounds index
26+
in an array, which should trigger ASan when running the program. We will be using a Conan profile to enable ASan:
27+
28+
.. code-block:: ini
29+
:caption: profiles/gcc_asan
30+
:emphasize-lines: 10
31+
32+
[settings]
33+
arch=x86_64
34+
os=Linux
35+
build_type=Debug
36+
compiler=gcc
37+
compiler.cppstd=gnu20
38+
compiler.libcxx=libstdc++11
39+
compiler.version=15
40+
compiler.sanitizer=Address
41+
42+
[conf]
43+
tools.build:cflags=['-fsanitize=address']
44+
tools.build:cxxflags=['-fsanitize=address']
45+
tools.build:exelinkflags=['-fsanitize=address']
46+
tools.build:sharedlinkflags+=["-fsanitize=address"]
47+
48+
[runenv]
49+
ASAN_OPTIONS="halt_on_error=1:detect_leaks=1"
50+
51+
Note that in this profile we set the ``compiler.sanitizer=Address`` does not
52+
define what compiler flags to use, but it is a settings to make explicit that
53+
both ASan and UBSan are intended to be used.
54+
55+
And for further illustration, we also use environment variable
56+
``ASAN_OPTIONS="halt_on_error=1:detect_leaks=1"`` for runtime configuration,
57+
to manage ASan to halt execution on the first error and to
58+
detect memory leaks when the program exits.
59+
60+
.. code-block:: cpp
61+
:caption: index_out_of_bounds/main.cpp
62+
:emphasize-lines: 11
63+
64+
#include <iostream>
65+
#include <cstdlib>
66+
67+
int main() {
68+
#ifdef __SANITIZE_ADDRESS__
69+
std::cout << "Address sanitizer enabled\n";
70+
#else
71+
std::cout << "Address sanitizer not enabled\n";
72+
#endif
73+
74+
int foo[100];
75+
foo[100] = 42; // Out-of-bounds write
76+
77+
return EXIT_SUCCESS;
78+
}
79+
80+
**Note:** The preprocessor check above is portable for GCC, Clang and MSVC.
81+
The define ``__SANITIZE_ADDRESS__`` is present when **ASan** is active;
82+
83+
**To build and run this example using Conan:**
84+
85+
.. code-block:: bash
86+
87+
cd index_out_of_bounds/
88+
conan build . -pr ../profiles/gcc_asan
89+
build/Debug/index_out_of_bounds
90+
91+
**Expected output (abbreviated):**
92+
93+
.. code-block:: text
94+
95+
Address sanitizer enabled
96+
==32018==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7fffbe04a6d0 ...
97+
WRITE of size 4 at 0x7fffbe04a6d0 thread T0
98+
#0 ... in main .../index_out_of_bounds+0x12ea
99+
...
100+
SUMMARY: AddressSanitizer: stack-buffer-overflow ... in main
101+
This frame has 1 object(s):
102+
[48, 448) 'foo' (line 11) <== Memory access at offset 448 overflows this variable
103+
104+
UndefinedBehaviorSanitizer: signed integer overflow
105+
---------------------------------------------------
106+
107+
This example demonstrates how to use UBSan to detect signed integer overflow. It combines ASan and UBSan.
108+
Create a dedicated profile:
109+
110+
.. code-block:: ini
111+
:caption: profiles/gcc_asan_ubsan
112+
:emphasize-lines: 7
113+
114+
[settings]
115+
arch=x86_64
116+
os=Linux
117+
build_type=Debug
118+
compiler=gcc
119+
compiler.cppstd=gnu20
120+
compiler.libcxx=libstdc++11
121+
compiler.version=15
122+
compiler.sanitizer=AddressUndefinedBehavior
123+
124+
[conf]
125+
tools.build:cflags+=["-fsanitize=address,undefined", "-fno-omit-frame-pointer"]
126+
tools.build:cxxflags+=["-fsanitize=address,undefined", "-fno-omit-frame-pointer"]
127+
tools.build:exelinkflags+=["-fsanitize=address,undefined"]
128+
tools.build:sharedlinkflags+=["-fsanitize=address,undefined"]
129+
130+
It is supported by GCC and Clang. MSVC does not support UBSan.
131+
132+
**Source code:**
133+
134+
.. code-block:: cpp
135+
:caption: signed_integer_overflow/main.cpp
136+
:emphasize-lines: 14
137+
138+
#include <iostream>
139+
#include <cstdlib>
140+
#include <climits>
141+
142+
int main() {
143+
#ifdef __SANITIZE_ADDRESS__
144+
std::cout << "Address sanitizer enabled\n";
145+
#else
146+
std::cout << "Address sanitizer not enabled\n";
147+
#endif
148+
149+
int x = INT_MAX;
150+
x += 42; // signed integer overflow
151+
152+
return EXIT_SUCCESS;
153+
}
154+
155+
**Build and run:**
156+
157+
.. code-block:: bash
158+
159+
cd signed_integer_overflow/
160+
conan build . -pr ../profiles/gcc_asan_ubsan
161+
build/Debug/signed_integer_overflow
162+
163+
**Expected output (abbreviated):**
164+
165+
.. code-block:: text
166+
167+
Address sanitizer enabled
168+
.../main.cpp:16:9: runtime error: signed integer overflow: 2147483647 + 1 cannot be represented in type 'int'
169+
170+
When executing the example application, UBSan detects the signed integer overflow and reports it as expected.

0 commit comments

Comments
 (0)