Skip to content

Commit 909c8e7

Browse files
AllanZynekbenzie
andauthored
[DeviceASAN] Fix throw "UR_RESULT_ERROR_INVALID_ARGUMENT" exception when catching free related error (#16706)
UR: oneapi-src/unified-runtime#2592 --------- Co-authored-by: Kenneth Benzie (Benie) <[email protected]>
1 parent 5f7043d commit 909c8e7

File tree

7 files changed

+31
-14
lines changed

7 files changed

+31
-14
lines changed

sycl/test-e2e/AddressSanitizer/bad-free/bad-free-host.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// REQUIRES: linux, cpu || (gpu && level_zero)
22
// RUN: %{build} %device_asan_flags -O0 -g -o %t
3-
// RUN: %force_device_asan_rt %{run} not --crash %t 2>&1 | FileCheck %s
3+
// RUN: %force_device_asan_rt %{run} not %t 2>&1 | FileCheck %s
44
#include <sycl/usm.hpp>
55

66
constexpr size_t N = 64;
@@ -13,3 +13,4 @@ int main() {
1313
}
1414
// CHECK: ERROR: DeviceSanitizer: bad-free on address [[ADDR:0x.*]]
1515
// CHECK: [[ADDR]] may be allocated on Host Memory
16+
// CHECK-NOT: terminate called after throwing an instance

sycl/test-e2e/AddressSanitizer/bad-free/bad-free-minus1.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// REQUIRES: linux, cpu || (gpu && level_zero)
22
// RUN: %{build} %device_asan_flags -O0 -g -o %t1.out
3-
// RUN: %force_device_asan_rt %{run} not --crash %t1.out 2>&1 | FileCheck --check-prefixes CHECK,CHECK-DEVICE %s
3+
// RUN: %force_device_asan_rt %{run} not %t1.out 2>&1 | FileCheck --check-prefixes CHECK,CHECK-DEVICE %s
44
// RUN: %{build} %device_asan_flags -DMALLOC_HOST -O0 -g -o %t2.out
5-
// RUN: %force_device_asan_rt %{run} not --crash %t2.out 2>&1 | FileCheck --check-prefixes CHECK,CHECK-HOST %s
5+
// RUN: %force_device_asan_rt %{run} not %t2.out 2>&1 | FileCheck --check-prefixes CHECK,CHECK-HOST %s
66
// RUN: %{build} %device_asan_flags -DMALLOC_SHARED -O0 -g -o %t3.out
7-
// RUN: %force_device_asan_rt %{run} not --crash %t3.out 2>&1 | FileCheck --check-prefixes CHECK,CHECK-SHARED %s
7+
// RUN: %force_device_asan_rt %{run} not %t3.out 2>&1 | FileCheck --check-prefixes CHECK,CHECK-SHARED %s
88
#include <sycl/usm.hpp>
99

1010
constexpr size_t N = 64;
@@ -31,3 +31,4 @@ int main() {
3131
// CHECK-HOST: in main {{.*bad-free-minus1.cpp:}}[[@LINE-15]]
3232
// CHECK-SHARED: in main {{.*bad-free-minus1.cpp:}}[[@LINE-14]]
3333
// CHECK-DEVICE: in main {{.*bad-free-minus1.cpp:}}[[@LINE-13]]
34+
// CHECK-NOT: terminate called after throwing an instance

sycl/test-e2e/AddressSanitizer/bad-free/bad-free-plus1.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// REQUIRES: linux, cpu || (gpu && level_zero)
22
// RUN: %{build} %device_asan_flags -O0 -g -o %t1.out
3-
// RUN: %force_device_asan_rt %{run} not --crash %t1.out 2>&1 | FileCheck --check-prefixes CHECK,CHECK-DEVICE %s
3+
// RUN: %force_device_asan_rt %{run} not %t1.out 2>&1 | FileCheck --check-prefixes CHECK,CHECK-DEVICE %s
44
// RUN: %{build} %device_asan_flags -DMALLOC_HOST -O0 -g -o %t2.out
5-
// RUN: %force_device_asan_rt %{run} not --crash %t2.out 2>&1 | FileCheck --check-prefixes CHECK,CHECK-HOST %s
5+
// RUN: %force_device_asan_rt %{run} not %t2.out 2>&1 | FileCheck --check-prefixes CHECK,CHECK-HOST %s
66
// RUN: %{build} %device_asan_flags -DMALLOC_SHARED -O0 -g -o %t3.out
7-
// RUN: %force_device_asan_rt %{run} not --crash %t3.out 2>&1 | FileCheck --check-prefixes CHECK,CHECK-SHARED %s
7+
// RUN: %force_device_asan_rt %{run} not %t3.out 2>&1 | FileCheck --check-prefixes CHECK,CHECK-SHARED %s
88
#include <sycl/usm.hpp>
99

1010
constexpr size_t N = 64;
@@ -31,3 +31,4 @@ int main() {
3131
// CHECK-DEVICE: in main {{.*bad-free-plus1.cpp:}}[[@LINE-11]]
3232
return 0;
3333
}
34+
// CHECK-NOT: terminate called after throwing an instance

sycl/test-e2e/AddressSanitizer/double-free/double-free.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// REQUIRES: linux, cpu || (gpu && level_zero)
22
// RUN: %{build} %device_asan_flags -O0 -g -o %t1.out
3-
// 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
3+
// 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
44
// RUN: %{build} %device_asan_flags -DMALLOC_HOST -O0 -g -o %t2.out
5-
// 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
5+
// 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
66
// RUN: %{build} %device_asan_flags -DMALLOC_SHARED -O0 -g -o %t3.out
7-
// 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
7+
// 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
88
#include <sycl/usm.hpp>
99

1010
constexpr size_t N = 64;

unified-runtime/source/loader/layers/sanitizer/asan/asan_interceptor.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,10 @@ ur_result_t AsanInterceptor::releaseMemory(ur_context_handle_t Context,
175175
if (!AllocInfoItOp) {
176176
// "Addr" might be a host pointer
177177
ReportBadFree(Addr, GetCurrentBacktrace(), nullptr);
178-
return UR_RESULT_ERROR_INVALID_ARGUMENT;
178+
if (getOptions().HaltOnError) {
179+
exitWithErrors();
180+
}
181+
return UR_RESULT_SUCCESS;
179182
}
180183

181184
auto AllocInfoIt = *AllocInfoItOp;
@@ -190,17 +193,26 @@ ur_result_t AsanInterceptor::releaseMemory(ur_context_handle_t Context,
190193
// "Addr" might be a host pointer
191194
ReportBadFree(Addr, GetCurrentBacktrace(), nullptr);
192195
}
193-
return UR_RESULT_ERROR_INVALID_ARGUMENT;
196+
if (getOptions().HaltOnError) {
197+
exitWithErrors();
198+
}
199+
return UR_RESULT_SUCCESS;
194200
}
195201

196202
if (Addr != AllocInfo->UserBegin) {
197203
ReportBadFree(Addr, GetCurrentBacktrace(), AllocInfo);
198-
return UR_RESULT_ERROR_INVALID_ARGUMENT;
204+
if (getOptions().HaltOnError) {
205+
exitWithErrors();
206+
}
207+
return UR_RESULT_SUCCESS;
199208
}
200209

201210
if (AllocInfo->IsReleased) {
202211
ReportDoubleFree(Addr, GetCurrentBacktrace(), AllocInfo);
203-
return UR_RESULT_ERROR_INVALID_ARGUMENT;
212+
if (getOptions().HaltOnError) {
213+
exitWithErrors();
214+
}
215+
return UR_RESULT_SUCCESS;
204216
}
205217

206218
AllocInfo->IsReleased = true;

unified-runtime/source/loader/layers/sanitizer/asan/asan_options.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ AsanOptions::AsanOptions() {
9090
SetBoolOption("detect_privates", DetectPrivates);
9191
SetBoolOption("print_stats", PrintStats);
9292
SetBoolOption("detect_leaks", DetectLeaks);
93+
SetBoolOption("halt_on_error", HaltOnError);
9394

9495
auto KV = OptionsEnvMap->find("quarantine_size_mb");
9596
if (KV != OptionsEnvMap->end()) {

unified-runtime/source/loader/layers/sanitizer/asan/asan_options.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ struct AsanOptions {
2828
bool PrintStats = false;
2929
bool DetectKernelArguments = true;
3030
bool DetectLeaks = true;
31+
bool HaltOnError = true;
3132

3233
explicit AsanOptions();
3334
};

0 commit comments

Comments
 (0)