Skip to content

Commit 0992ced

Browse files
committed
Improve README
Signed-off-by: Uilian Ries <uilianr@jfrog.com>
1 parent b43cc0d commit 0992ced

File tree

1 file changed

+79
-1
lines changed
  • examples/dev_flow/sanitizers/compiler_sanitizers

1 file changed

+79
-1
lines changed
Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,81 @@
11
# Compiler Sanitizers Example
22

3-
This example follows the documented page https://docs.conan.io/2/examples/dev_flow/sanitizers/compiler_sanitizers.
3+
This example follows the documented page https://docs.conan.io/2/examples/dev_flow/sanitizers/compiler_sanitizers.
4+
5+
## Examples
6+
7+
Here are some examples of using compiler sanitizers with Conan.
8+
9+
### Signed Integer Overflow
10+
11+
This example demonstrates how to detect signed integer overflow using compiler sanitizers. The provided C++ code intentionally causes a signed integer overflow, which can be detected when running the program with the appropriate sanitizer flags.
12+
13+
It explores the [Undefined Behavior Sanitizer](https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html), **ONLY** available in Clang and GCC; MSVC does not support it (yet).
14+
15+
In order to try the example, you may run the following commands:
16+
17+
```
18+
conan create signed_integer_overflow/ -pr profiles/asan_ubsan
19+
conan install --requires=signed_integer_overflow/0.1.0 -pr profiles/asan_ubsan -of install
20+
source install/conanrun.sh
21+
signed_integer_overflow
22+
```
23+
It's expected to observe a runtime error indicating a signed integer overflow has occurred:
24+
25+
```
26+
Address sanitizer not enabled
27+
/home/conan/.conan2/p/b/signe3b8ad6d59f30b/b/main.cpp:13:9: runtime error: signed integer overflow: 2147483647 + 1 cannot be represented in type 'int'
28+
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /home/conan/.conan2/p/b/signe3b8ad6d59f30b/b/main.cpp:13:9
29+
```
30+
31+
### Index Out of Bounds
32+
33+
This example demonstrates how to detect out-of-bounds memory access using compiler sanitizers. The provided C++ code intentionally accesses an out-of-bounds index in an array, which can be detected when running the program with the appropriate sanitizer flags.
34+
35+
It explores the [Address Sanitizer](https://clang.llvm.org/docs/AddressSanitizer.html), available in Clang, GCC and MSVC.
36+
37+
In order to try the example, you may run the following commands:
38+
39+
```
40+
conan create index_out_of_bounds/ -pr profiles/asan
41+
conan install --requires=index_out_of_bounds/0.1.0 -pr profiles/asan -of install
42+
source install/conanrun.sh
43+
index_out_of_bounds
44+
```
45+
46+
It's expected to observe a runtime error indicating an out-of-bounds memory access has occurred:
47+
48+
```
49+
==357155==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ffcddcc40e0 at pc 0x5946a605f2eb bp 0x7ffcddcc3f10 sp 0x7ffcddcc3f00
50+
WRITE of size 4 at 0x7ffcddcc40e0 thread T0
51+
#0 0x5946a605f2ea in main (/home/conan/.conan2/p/b/index7e914f42d466f/p/bin/index_out_of_bounds+0x12ea)
52+
#1 0x7722f0c29d8f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
53+
#2 0x7722f0c29e3f in __libc_start_main_impl ../csu/libc-start.c:392
54+
#3 0x5946a605f3d4 in _start (/home/conan/.conan2/p/b/index7e914f42d466f/p/bin/index_out_of_bounds+0x13d4)
55+
56+
Address 0x7ffcddcc40e0 is located in stack of thread T0 at offset 448 in frame
57+
#0 0x5946a605f1ef in main (/home/conan/.conan2/p/b/index7e914f42d466f/p/bin/index_out_of_bounds+0x11ef)
58+
59+
This frame has 1 object(s):
60+
[48, 448) 'foo' (line 11) <== Memory access at offset 448 overflows this variable
61+
HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork
62+
(longjmp and C++ exceptions *are* supported)
63+
SUMMARY: AddressSanitizer: stack-buffer-overflow (/home/conan/.conan2/p/b/index7e914f42d466f/p/bin/index_out_of_bounds+0x12ea) in main
64+
```
65+
66+
## Customizing Sanitizers
67+
68+
### Using Environment Variables
69+
70+
The `ASAN_OPTIONS` and `UBSAN_OPTIONS` environment variables can be used to customize the behavior of AddressSanitizer and UndefinedBehaviorSanitizer, respectively. For example, you can set the `ASAN_OPTIONS` variable to control the reporting format, enable or disable specific checks, and more.
71+
72+
To set these environment variables, you can use the `export` command in your terminal before running your program:
73+
74+
```bash
75+
export ASAN_OPTIONS=detect_leaks=1:log_path=asan.log
76+
export UBSAN_OPTIONS=print_stacktrace=1
77+
```
78+
79+
This will enable leak detection for AddressSanitizer and print stack traces for UndefinedBehaviorSanitizer.
80+
81+
For more advanced configurations, you can refer to the [Clang AddressSanitizer](https://github.com/google/sanitizers/wiki/addresssanitizerflags#run-time-flags) and [MSVC AddressSanitizer](https://learn.microsoft.com/en-us/cpp/sanitizers/asan?view=msvc-170#differences) documentation.

0 commit comments

Comments
 (0)