|
| 1 | +# `Address Sanitizer` Sample |
| 2 | + |
| 3 | +The `Address Sanitizer` sample demonstrates how to use the AddressSanitizer (ASan) memory detector with the SYCL library. |
| 4 | + |
| 5 | +| Area | Description |
| 6 | +|:--- |:--- |
| 7 | +| What you will learn | How to use the Address Sanitizer tool |
| 8 | +| Time to complete | 10 minutes |
| 9 | +| Category | Memory Management |
| 10 | + |
| 11 | + |
| 12 | +## Purpose |
| 13 | + |
| 14 | +The `Address Sanitizer` sample illustrates how to use Address Sanitizer to manage memory errors with the SYCL library. Each of the examples shows a different error and how to initialize it. |
| 15 | + |
| 16 | +All samples can be run on a CPU or a PVC GPU. |
| 17 | + |
| 18 | +>**Note**: The gfx-driver needed to run ASan is version 1.3.28986 for a level zero GPU, 2024.18.3.0.head.prerelease for a CPU and 24.11.028986 for a GPU |
| 19 | +
|
| 20 | +The sample includes nine different mini samples that showcase the usage of ASan. |
| 21 | + |
| 22 | +## Prerequisites |
| 23 | + |
| 24 | +| Optimized for | Description |
| 25 | +|:--- |:--- |
| 26 | +| OS | Ubuntu* 20.04 (or newer) |
| 27 | +| Hardware | GEN9 (or newer) |
| 28 | +| Software | Intel® oneAPI DPC++/C++ Compiler |
| 29 | + |
| 30 | +## Key Implementation Details |
| 31 | + |
| 32 | +The basic SYCL implementation explained in the code includes: |
| 33 | + |
| 34 | +- out-of-bounds |
| 35 | +- use-after-free |
| 36 | +- misalign-access |
| 37 | +- double-free |
| 38 | +- bad-free |
| 39 | +- bad-context |
| 40 | + |
| 41 | + |
| 42 | +## Set Environment Variables |
| 43 | + |
| 44 | +When working with the command-line interface (CLI), you should configure the oneAPI toolkits using environment variables. Set up your CLI environment by sourcing the `setvars` script every time you open a new terminal window. This practice ensures that your compiler, libraries, and tools are ready for development. |
| 45 | + |
| 46 | +## Build the `Address Sanitizer` Sample |
| 47 | + |
| 48 | +> **Note**: If you have not already done so, set up your CLI |
| 49 | +> environment by sourcing the `setvars` script in the root of your oneAPI installation. |
| 50 | +> |
| 51 | +> Linux*: |
| 52 | +> - For system wide installations: `. /opt/intel/oneapi/setvars.sh` |
| 53 | +> - For private installations: ` . ~/intel/oneapi/setvars.sh` |
| 54 | +> - For non-POSIX shells, like csh, use the following command: `bash -c 'source <install-dir>/setvars.sh ; exec csh'` |
| 55 | +> |
| 56 | +> For more information on configuring environment variables, see *[Use the setvars Script with Linux* or macOS*](https://www.intel.com/content/www/us/en/develop/documentation/oneapi-programming-guide/top/oneapi-development-environment-setup/use-the-setvars-script-with-linux-or-macos.html)*. |
| 57 | +
|
| 58 | +### Using Visual Studio Code* (Optional) |
| 59 | + |
| 60 | +You can use Visual Studio Code (VS Code) extensions to set your environment, create launch configurations, and browse and download samples. |
| 61 | + |
| 62 | +The basic steps to build and run a sample using VS Code include: |
| 63 | + |
| 64 | +1. Configure the oneAPI environment with the extension **Environment Configurator for Intel Software Developer Tools**. |
| 65 | +2. Download a sample using the extension **Code Sample Browser for Intel Software Developer Tools**. |
| 66 | +3. Open a terminal in VS Code (**Terminal > New Terminal**). |
| 67 | +4. Run the sample in the VS Code terminal using the instructions below. |
| 68 | + |
| 69 | +To learn more about the extensions and how to configure the oneAPI environment, see the |
| 70 | +*[Using Visual Studio Code with Intel® oneAPI Toolkits User Guide](https://software.intel.com/content/www/us/en/develop/documentation/using-vs-code-with-intel-oneapi/top.html)*. |
| 71 | + |
| 72 | +### On Linux* |
| 73 | + |
| 74 | +1. Change to the sample directory. |
| 75 | +2. Build the program. |
| 76 | + ``` |
| 77 | + mkdir build |
| 78 | + cd build |
| 79 | + cmake .. |
| 80 | + make |
| 81 | + ``` |
| 82 | +3. Run the program. |
| 83 | + ``` |
| 84 | + make run_array_reduction |
| 85 | + ``` |
| 86 | +6. Clean the project. (Optional) |
| 87 | + ``` |
| 88 | + make clean |
| 89 | + ``` |
| 90 | + |
| 91 | +> **Note**: List of all samples and command to run them. |
| 92 | +> environment by sourcing the `setvars` script in the root of your oneAPI installation. |
| 93 | +>| File Name | Run command |
| 94 | +>|:--- |:--- |
| 95 | +>|`array_reduction.cpp` | make run_array_reduction |
| 96 | +>|`bad_free.cpp` | make run_bad_free |
| 97 | +>|`device_global.cpp` | make run_device_global |
| 98 | +>|`group_local.cpp` | make run_group_local |
| 99 | +>|`local_stencil.cpp` | make run_local_stencil |
| 100 | +>|`map.cpp` | make run_map |
| 101 | +>|`matmul_broadcast.cpp` | make run_matmul_broadcast |
| 102 | +>|`misalign-long.cpp` | make run_misalign-long |
| 103 | +>|`nd_range_reduction.cpp` | make run_nd_range_reduction |
| 104 | +
|
| 105 | +## Example Output |
| 106 | + |
| 107 | +The following output is for the arrayreduction.cpp sample. |
| 108 | +``` |
| 109 | +Histogram: |
| 110 | +bin[0]: 4 |
| 111 | +bin[1]: 4 |
| 112 | +bin[2]: 4 |
| 113 | +bin[3]: 4 |
| 114 | +SUCCESS |
| 115 | +
|
| 116 | +====ERROR: DeviceSanitizer: bad-free on address 0x250d040 |
| 117 | + #0 ./array_reduction() [0x405249] |
| 118 | + #1 /lib/x86_64-linux-gnu/libc.so.6(+0x29d90) [0x735d35229d90] |
| 119 | + #2 /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0x80) [0x735d35229e40] |
| 120 | + #3 ./array_reduction() [0x4049b5] |
| 121 | +
|
| 122 | +0x250d040 may be allocated on Host Memory |
| 123 | +Segmentation fault (core dumped) |
| 124 | +make[3]: *** [src/CMakeFiles/run_array_reduction.dir/build.make:70: src/CMakeFiles/run_array_reduction] Error 139 |
| 125 | +make[2]: *** [CMakeFiles/Makefile2:392: src/CMakeFiles/run_array_reduction.dir/all] Error 2 |
| 126 | +make[1]: *** [CMakeFiles/Makefile2:399: src/CMakeFiles/run_array_reduction.dir/rule] Error 2 |
| 127 | +make: *** [Makefile:254: run_array_reduction] Error 2 |
| 128 | +``` |
| 129 | + |
| 130 | +## License |
| 131 | + |
| 132 | +Code samples are licensed under the MIT license. See |
| 133 | +[License.txt](https://github.com/oneapi-src/oneAPI-samples/blob/master/License.txt) for details. |
| 134 | + |
| 135 | +Third party program Licenses can be found here: [third-party-programs.txt](https://github.com/oneapi-src/oneAPI-samples/blob/master/third-party-programs.txt). |
0 commit comments