|
| 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