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;

    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",;
        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=addressCode language: Bash (bash)

environment variables


For more verbosity


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 *