Using libasan with CUDA

Consider the following simple program

#include "cuda_runtime.h" #include <iostream> #define CUDA_CHK(fmt) do { error = (fmt); if (error != cudaSuccess) {std::cout << "cuda error: " << int(error) << " - " << cudaGetErrorString(error) << std::endl;}} while (0) int main(int argc, char *argv[]) { cudaError_t error; int count = 0; CUDA_CHK(cudaGetDeviceCount(&count)); printf("Device count: %d\n", count); for (int i = 0; i < count; i++) { cudaDeviceProp prop; CUDA_CHK(cudaGetDeviceProperties(&prop, i)); printf("Device Number: %d\n", i); printf(" Device name: %s\n", prop.name); printf(" Memory Clock Rate (KHz): %d\n", prop.memoryClockRate); printf(" Memory Bus Width (bits): %d\n", prop.memoryBusWidth); printf(" Peak Memory Bandwidth (GB/s): %f\n\n", 2.0*prop.memoryClockRate*(prop.memoryBusWidth/8)/1.0e6); } return 0; }
Code language: C++ (cpp)

If you only link libasan and set the compiler flags, cudaGetDeviceCount will fail with error cudaErrorMemoryAllocation(2) and the CUDA runtime won’t work.

I got it working using these steps

project configuration

CXXFLAGS +=-Og -fsanitize=address -fno-omit-frame-pointer -fno-common -fsanitize-address-use-after-scope CFLAGS +=-Og -fsanitize=address -fno-omit-frame-pointer -fno-common -fsanitize-address-use-after-scope LFLAGS +=-fsanitize=address
Code language: Bash (bash)

environment variables

ASAN_OPTIONS=protect_shadow_gap=0:replace_intrin=0:detect_stack_use_after_return=0:exitcode=0

For more verbosity

LSAN_OPTIONS=verbosity=1:log_threads=1

GDB configuration

This will make gdb stop sending the SIGSTOP signal to the IDE (QtCreator in my case) and interrupting the code execution.

handle SIGSTOP noprint nopass

Leave a Reply

Your email address will not be published. Required fields are marked *