Skip to content

Commit 07bf3c1

Browse files
authored
[DeviceSanitizer] Always cleanup private shadow memory before using it (#15065)
For global, local shadow memory, we will cleanup it before using it. So, we also need to do this for private shadow memory to avoid shadow value pollutions.
1 parent 206e2e9 commit 07bf3c1

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4079,6 +4079,15 @@ void FunctionStackPoisoner::processStaticAllocas() {
40794079
// Poison the stack red zones at the entry.
40804080
Value *ShadowBase =
40814081
ASan.memToShadow(LocalStackBase, IRB, kSpirOffloadPrivateAS);
4082+
4083+
// FIXME: For device sanitizer, we always cleanup shadow memory before using
4084+
// it. So, unpoison stack before ret instructions is unnecessary.
4085+
if (TargetTriple.isSPIROrSPIRV()) {
4086+
SmallVector<uint8_t, 64> ShadowMask(ShadowAfterScope.size(), 1);
4087+
SmallVector<uint8_t, 64> ShadowBytes(ShadowAfterScope.size(), 0);
4088+
copyToShadow(ShadowMask, ShadowBytes, IRB, ShadowBase, true);
4089+
}
4090+
40824091
// As mask we must use most poisoned case: red zones and after scope.
40834092
// As bytes we can use either the same or just red zones only.
40844093
copyToShadow(ShadowAfterScope, ShadowAfterScope, IRB, ShadowBase,

llvm/test/Instrumentation/AddressSanitizer/SPIRV/instrument_private_address_space.ll

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,16 @@ define spir_kernel void @kernel() #0 {
2121
entry:
2222
%p.i = alloca [4 x i32], align 4
2323
; CHECK: %shadow_ptr = call i64 @__asan_mem_to_shadow(i64 %0, i32 0)
24-
; CHECK: call void @__asan_set_shadow_private(i64 %4, i64 2, i8 -15)
25-
; CHECK: call void @__asan_set_shadow_private(i64 %5, i64 1, i8 -13)
24+
; CHECK: call void @__asan_set_shadow_private(i64 %4, i64 4, i8 0)
25+
; CHECK: call void @__asan_set_shadow_private(i64 %5, i64 2, i8 -15)
26+
; CHECK: call void @__asan_set_shadow_private(i64 %6, i64 1, i8 -13)
2627
call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %p.i)
2728
call void @llvm.memcpy.p0.p1.i64(ptr align 4 %p.i, ptr addrspace(1) align 4 @__const._ZZZ4mainENKUlRN4sycl3_V17handlerEE_clES2_ENKUlvE_clEv.p, i64 16, i1 false)
2829
%arraydecay.i = getelementptr inbounds [4 x i32], ptr %p.i, i64 0, i64 0
2930
%0 = addrspacecast ptr %arraydecay.i to ptr addrspace(4)
3031
%call.i = call spir_func i32 @_Z3fooPii(ptr addrspace(4) %0)
31-
; CHECK: call void @__asan_set_shadow_private(i64 %7, i64 2, i8 0)
32-
; CHECK: call void @__asan_set_shadow_private(i64 %8, i64 1, i8 0)
32+
; CHECK: call void @__asan_set_shadow_private(i64 %8, i64 2, i8 0)
33+
; CHECK: call void @__asan_set_shadow_private(i64 %9, i64 1, i8 0)
3334
ret void
3435
}
3536

0 commit comments

Comments
 (0)