Skip to content

[DeviceASAN] Fix throw "UR_RESULT_ERROR_INVALID_ARGUMENT" exception when catching free related error #16706

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Feb 20, 2025
3 changes: 2 additions & 1 deletion sycl/test-e2e/AddressSanitizer/bad-free/bad-free-host.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// REQUIRES: linux, cpu || (gpu && level_zero)
// RUN: %{build} %device_asan_flags -O0 -g -o %t
// RUN: %force_device_asan_rt %{run} not --crash %t 2>&1 | FileCheck %s
// RUN: %force_device_asan_rt %{run} not %t 2>&1 | FileCheck %s
#include <sycl/usm.hpp>

constexpr size_t N = 64;
Expand All @@ -13,3 +13,4 @@ int main() {
}
// CHECK: ERROR: DeviceSanitizer: bad-free on address [[ADDR:0x.*]]
// CHECK: [[ADDR]] may be allocated on Host Memory
// CHECK-NOT: terminate called after throwing an instance
7 changes: 4 additions & 3 deletions sycl/test-e2e/AddressSanitizer/bad-free/bad-free-minus1.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// REQUIRES: linux, cpu || (gpu && level_zero)
// RUN: %{build} %device_asan_flags -O0 -g -o %t1.out
// RUN: %force_device_asan_rt %{run} not --crash %t1.out 2>&1 | FileCheck --check-prefixes CHECK,CHECK-DEVICE %s
// RUN: %force_device_asan_rt %{run} not %t1.out 2>&1 | FileCheck --check-prefixes CHECK,CHECK-DEVICE %s
// RUN: %{build} %device_asan_flags -DMALLOC_HOST -O0 -g -o %t2.out
// RUN: %force_device_asan_rt %{run} not --crash %t2.out 2>&1 | FileCheck --check-prefixes CHECK,CHECK-HOST %s
// RUN: %force_device_asan_rt %{run} not %t2.out 2>&1 | FileCheck --check-prefixes CHECK,CHECK-HOST %s
// RUN: %{build} %device_asan_flags -DMALLOC_SHARED -O0 -g -o %t3.out
// RUN: %force_device_asan_rt %{run} not --crash %t3.out 2>&1 | FileCheck --check-prefixes CHECK,CHECK-SHARED %s
// RUN: %force_device_asan_rt %{run} not %t3.out 2>&1 | FileCheck --check-prefixes CHECK,CHECK-SHARED %s
#include <sycl/usm.hpp>

constexpr size_t N = 64;
Expand All @@ -31,3 +31,4 @@ int main() {
// CHECK-HOST: in main {{.*bad-free-minus1.cpp:}}[[@LINE-15]]
// CHECK-SHARED: in main {{.*bad-free-minus1.cpp:}}[[@LINE-14]]
// CHECK-DEVICE: in main {{.*bad-free-minus1.cpp:}}[[@LINE-13]]
// CHECK-NOT: terminate called after throwing an instance
7 changes: 4 additions & 3 deletions sycl/test-e2e/AddressSanitizer/bad-free/bad-free-plus1.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// REQUIRES: linux, cpu || (gpu && level_zero)
// RUN: %{build} %device_asan_flags -O0 -g -o %t1.out
// RUN: %force_device_asan_rt %{run} not --crash %t1.out 2>&1 | FileCheck --check-prefixes CHECK,CHECK-DEVICE %s
// RUN: %force_device_asan_rt %{run} not %t1.out 2>&1 | FileCheck --check-prefixes CHECK,CHECK-DEVICE %s
// RUN: %{build} %device_asan_flags -DMALLOC_HOST -O0 -g -o %t2.out
// RUN: %force_device_asan_rt %{run} not --crash %t2.out 2>&1 | FileCheck --check-prefixes CHECK,CHECK-HOST %s
// RUN: %force_device_asan_rt %{run} not %t2.out 2>&1 | FileCheck --check-prefixes CHECK,CHECK-HOST %s
// RUN: %{build} %device_asan_flags -DMALLOC_SHARED -O0 -g -o %t3.out
// RUN: %force_device_asan_rt %{run} not --crash %t3.out 2>&1 | FileCheck --check-prefixes CHECK,CHECK-SHARED %s
// RUN: %force_device_asan_rt %{run} not %t3.out 2>&1 | FileCheck --check-prefixes CHECK,CHECK-SHARED %s
#include <sycl/usm.hpp>

constexpr size_t N = 64;
Expand All @@ -31,3 +31,4 @@ int main() {
// CHECK-DEVICE: in main {{.*bad-free-plus1.cpp:}}[[@LINE-11]]
return 0;
}
// CHECK-NOT: terminate called after throwing an instance
6 changes: 3 additions & 3 deletions sycl/test-e2e/AddressSanitizer/double-free/double-free.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// REQUIRES: linux, cpu || (gpu && level_zero)
// RUN: %{build} %device_asan_flags -O0 -g -o %t1.out
// RUN: %force_device_asan_rt UR_LAYER_ASAN_OPTIONS="quarantine_size_mb:1;detect_kernel_arguments:0" %{run} not --crash %t1.out 2>&1 | FileCheck --check-prefixes CHECK,CHECK-DEVICE %s
// RUN: %force_device_asan_rt UR_LAYER_ASAN_OPTIONS="quarantine_size_mb:1;detect_kernel_arguments:0" %{run} not %t1.out 2>&1 | FileCheck --check-prefixes CHECK,CHECK-DEVICE %s
// RUN: %{build} %device_asan_flags -DMALLOC_HOST -O0 -g -o %t2.out
// RUN: %force_device_asan_rt UR_LAYER_ASAN_OPTIONS="quarantine_size_mb:1;detect_kernel_arguments:0" %{run} not --crash %t2.out 2>&1 | FileCheck --check-prefixes CHECK,CHECK-HOST %s
// RUN: %force_device_asan_rt UR_LAYER_ASAN_OPTIONS="quarantine_size_mb:1;detect_kernel_arguments:0" %{run} not %t2.out 2>&1 | FileCheck --check-prefixes CHECK,CHECK-HOST %s
// RUN: %{build} %device_asan_flags -DMALLOC_SHARED -O0 -g -o %t3.out
// RUN: %force_device_asan_rt UR_LAYER_ASAN_OPTIONS="quarantine_size_mb:1;detect_kernel_arguments:0" %{run} not --crash %t3.out 2>&1 | FileCheck --check-prefixes CHECK,CHECK-SHARED %s
// RUN: %force_device_asan_rt UR_LAYER_ASAN_OPTIONS="quarantine_size_mb:1;detect_kernel_arguments:0" %{run} not %t3.out 2>&1 | FileCheck --check-prefixes CHECK,CHECK-SHARED %s
#include <sycl/usm.hpp>

constexpr size_t N = 64;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,10 @@ ur_result_t AsanInterceptor::releaseMemory(ur_context_handle_t Context,
if (!AllocInfoItOp) {
// "Addr" might be a host pointer
ReportBadFree(Addr, GetCurrentBacktrace(), nullptr);
return UR_RESULT_ERROR_INVALID_ARGUMENT;
if (getOptions().HaltOnError) {
exitWithErrors();
}
return UR_RESULT_SUCCESS;
}

auto AllocInfoIt = *AllocInfoItOp;
Expand All @@ -190,17 +193,26 @@ ur_result_t AsanInterceptor::releaseMemory(ur_context_handle_t Context,
// "Addr" might be a host pointer
ReportBadFree(Addr, GetCurrentBacktrace(), nullptr);
}
return UR_RESULT_ERROR_INVALID_ARGUMENT;
if (getOptions().HaltOnError) {
exitWithErrors();
}
return UR_RESULT_SUCCESS;
}

if (Addr != AllocInfo->UserBegin) {
ReportBadFree(Addr, GetCurrentBacktrace(), AllocInfo);
return UR_RESULT_ERROR_INVALID_ARGUMENT;
if (getOptions().HaltOnError) {
exitWithErrors();
}
return UR_RESULT_SUCCESS;
}

if (AllocInfo->IsReleased) {
ReportDoubleFree(Addr, GetCurrentBacktrace(), AllocInfo);
return UR_RESULT_ERROR_INVALID_ARGUMENT;
if (getOptions().HaltOnError) {
exitWithErrors();
}
return UR_RESULT_SUCCESS;
}

AllocInfo->IsReleased = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ AsanOptions::AsanOptions() {
SetBoolOption("detect_privates", DetectPrivates);
SetBoolOption("print_stats", PrintStats);
SetBoolOption("detect_leaks", DetectLeaks);
SetBoolOption("halt_on_error", HaltOnError);

auto KV = OptionsEnvMap->find("quarantine_size_mb");
if (KV != OptionsEnvMap->end()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ struct AsanOptions {
bool PrintStats = false;
bool DetectKernelArguments = true;
bool DetectLeaks = true;
bool HaltOnError = true;

explicit AsanOptions();
};
Expand Down
Loading