Skip to content

Commit 9ea1f74

Browse files
committed
Add PC again
1 parent 30154c2 commit 9ea1f74

File tree

3 files changed

+93
-78
lines changed

3 files changed

+93
-78
lines changed

llvm/lib/Transforms/Instrumentation/GPUSan.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ class GPUSanImpl final {
206206
FunctionCallee getNewFn(PtrOrigin PO) {
207207
assert(PO <= GLOBAL && "Origin does not need handling.");
208208
return getOrCreateFn(NewFn[PO], "ompx_new" + getSuffix(PO), getPtrTy(PO),
209-
{getPtrTy(PO), Int64Ty, Int64Ty, Int64Ty});
209+
{getPtrTy(PO), Int64Ty, Int64Ty, Int64Ty, Int64Ty});
210210
}
211211
FunctionCallee getFreeFn(PtrOrigin PO) {
212212
assert(PO <= GLOBAL && "Origin does not need handling.");
@@ -220,14 +220,14 @@ class GPUSanImpl final {
220220
assert(PO <= GLOBAL && "Origin does not need handling.");
221221
return getOrCreateFn(CheckFn[PO], "ompx_check" + getSuffix(PO),
222222
getPtrTy(PO),
223-
{getPtrTy(PO), Int64Ty, Int64Ty, Int64Ty});
223+
{getPtrTy(PO), Int64Ty, Int64Ty, Int64Ty, Int64Ty});
224224
}
225225
FunctionCallee getCheckWithBaseFn(PtrOrigin PO) {
226226
assert(PO >= LOCAL && PO <= GLOBAL && "Origin does not need handling.");
227227
return getOrCreateFn(CheckWithBaseFn[PO],
228228
"ompx_check_with_base" + getSuffix(PO), getPtrTy(PO),
229229
{getPtrTy(PO), getPtrTy(PO), Int64Ty, Int32Ty, Int64Ty,
230-
Int64Ty, Int64Ty});
230+
Int64Ty, Int64Ty, Int64Ty});
231231
}
232232
FunctionCallee getAllocationInfoFn(PtrOrigin PO) {
233233
assert(PO >= LOCAL && PO <= GLOBAL && "Origin does not need handling.");
@@ -567,7 +567,7 @@ Value *GPUSanImpl::instrumentAllocation(Instruction &I, Value &Size,
567567
auto *CB =
568568
createCall(IRB, Fn,
569569
{PlainI, &Size, ConstantInt::get(Int64Ty, AllocationId++),
570-
getSourceIndex(I)},
570+
getSourceIndex(I), getPC(IRB)},
571571
I.getName() + ".san");
572572
SmallVector<LifetimeIntrinsic *> Lifetimes;
573573
I.replaceUsesWithIf(
@@ -636,12 +636,13 @@ void GPUSanImpl::instrumentAccess(LoopInfo &LI, Instruction &I, int PtrIdx,
636636
if (Start) {
637637
CB = createCall(IRB, getCheckWithBaseFn(PO),
638638
{PlainPtrOp, Start, Length, Tag, Size,
639-
ConstantInt::get(Int64Ty, AccessId), getSourceIndex(I)},
639+
ConstantInt::get(Int64Ty, AccessId), getSourceIndex(I),
640+
getPC(IRB)},
640641
I.getName() + ".san");
641642
} else {
642643
CB = createCall(IRB, getCheckFn(PO),
643644
{PlainPtrOp, Size, ConstantInt::get(Int64Ty, AccessId),
644-
getSourceIndex(I)},
645+
getSourceIndex(I), getPC(IRB)},
645646
I.getName() + ".san");
646647
}
647648
I.setOperand(PtrIdx,

offload/DeviceRTL/src/Sanitizer.cpp

Lines changed: 52 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,17 @@ template <AllocationKind AK> struct AllocationTracker {
7373
uint32_t AllocationId = AP.AllocationId;
7474
if (OMP_UNLIKELY(AllocationId >= SanitizerConfig<AK>::SLOTS))
7575
return {P, 0, (uint32_t)-1};
76-
auto &A = getAllocation<AK>(AP);
76+
auto &A = getAllocation<AK>(AP, /*AccessId=*/0, /*PC=*/0);
7777
return {A.Start, A.Length, (uint32_t)A.Tag};
7878
}
7979

8080
[[clang::disable_sanitizer_instrumentation]] static _AS_PTR(void, AK)
8181
create(_AS_PTR(void, AK) Start, uint64_t Length, int64_t AllocationId,
82-
uint64_t Slot, int64_t SourceId) {
82+
uint64_t Slot, int64_t SourceId, uint64_t PC) {
8383
if constexpr (SanitizerConfig<AK>::OFFSET_BITS < 64)
8484
if (OMP_UNLIKELY(Length >= (1UL << (SanitizerConfig<AK>::OFFSET_BITS))))
8585
__sanitizer_trap_info_ptr->exceedsAllocationLength<AK>(
86-
Start, Length, AllocationId, Slot, SourceId);
86+
Start, Length, AllocationId, Slot, SourceId, /*PC=*/0);
8787

8888
// Reserve the 0 element for the null pointer in global space.
8989
auto &AllocArr = getAllocationArray<AK>();
@@ -94,7 +94,7 @@ template <AllocationKind AK> struct AllocationTracker {
9494
uint64_t NumSlots = SanitizerConfig<AK>::SLOTS;
9595
if (OMP_UNLIKELY(Slot >= NumSlots))
9696
__sanitizer_trap_info_ptr->exceedsAllocationSlots<AK>(
97-
Start, Length, AllocationId, Slot, SourceId);
97+
Start, Length, AllocationId, Slot, SourceId, /*PC=*/0);
9898

9999
auto &A = AllocArr.Arr[Slot];
100100

@@ -148,36 +148,38 @@ template <AllocationKind AK> struct AllocationTracker {
148148
[[clang::disable_sanitizer_instrumentation]] static _AS_PTR(void, AK)
149149
checkWithBase(_AS_PTR(void, AK) P, _AS_PTR(void, AK) Start,
150150
int64_t Length, uint32_t Tag, int64_t Size,
151-
int64_t AccessId, int64_t SourceId) {
151+
int64_t AccessId, int64_t SourceId, uint64_t PC) {
152152
AllocationPtrTy<AK> AP = AllocationPtrTy<AK>::get(P);
153153
if constexpr (AK == AllocationKind::LOCAL)
154154
if (Length == 0)
155-
Length = getAllocation<AK>(AP, AccessId).Length;
155+
Length = getAllocation<AK>(AP, AccessId, PC).Length;
156156
if constexpr (AK == AllocationKind::GLOBAL)
157157
if (AP.Magic != SanitizerConfig<AllocationKind::GLOBAL>::MAGIC)
158-
__sanitizer_trap_info_ptr->garbagePointer<AK>(AP, (void *)P, SourceId);
158+
__sanitizer_trap_info_ptr->garbagePointer<AK>(AP, (void *)P, SourceId,
159+
PC);
159160
int64_t Offset = AP.Offset;
160161
if (OMP_UNLIKELY(
161162
Offset > Length - Size ||
162163
(SanitizerConfig<AK>::useTags() && Tag != AP.AllocationTag))) {
163-
__sanitizer_trap_info_ptr->accessError<AK>(AP, Size, AccessId, SourceId);
164+
__sanitizer_trap_info_ptr->accessError<AK>(AP, Size, AccessId, SourceId,
165+
PC);
164166
}
165167
return utils::advancePtr(Start, Offset);
166168
}
167169

168170
[[clang::disable_sanitizer_instrumentation]] static _AS_PTR(void, AK)
169171
check(_AS_PTR(void, AK) P, int64_t Size, int64_t AccessId,
170-
int64_t SourceId) {
172+
int64_t SourceId, uint64_t PC) {
171173
AllocationPtrTy<AK> AP = AllocationPtrTy<AK>::get(P);
172-
auto &Alloc = getAllocation<AK>(AP, AccessId);
174+
auto &Alloc = getAllocation<AK>(AP, AccessId, PC);
173175
return checkWithBase(P, Alloc.Start, Alloc.Length, Alloc.Tag, Size,
174-
AccessId, SourceId);
176+
AccessId, SourceId, PC);
175177
}
176178

177179
[[clang::disable_sanitizer_instrumentation]] static _AS_PTR(void, AK)
178-
unpack(_AS_PTR(void, AK) P, int64_t SourceId = 0) {
180+
unpack(_AS_PTR(void, AK) P, int64_t SourceId, uint64_t PC) {
179181
AllocationPtrTy<AK> AP = AllocationPtrTy<AK>::get(P);
180-
auto &A = getAllocation<AK>(AP);
182+
auto &A = getAllocation<AK>(AP, SourceId, PC);
181183
uint64_t Offset = AP.Offset;
182184
_AS_PTR(void, AK) Ptr = utils::advancePtr(A.Start, Offset);
183185
return Ptr;
@@ -186,15 +188,15 @@ template <AllocationKind AK> struct AllocationTracker {
186188
[[clang::disable_sanitizer_instrumentation]] static void
187189
lifetimeStart(_AS_PTR(void, AK) P, uint64_t Length) {
188190
AllocationPtrTy<AK> AP = AllocationPtrTy<AK>::get(P);
189-
auto &A = getAllocation<AK>(AP);
191+
auto &A = getAllocation<AK>(AP, /*AccessId=*/0, /*PC=*/0);
190192
// TODO: Check length
191193
A.Length = Length;
192194
}
193195

194196
[[clang::disable_sanitizer_instrumentation]] static void
195197
lifetimeEnd(_AS_PTR(void, AK) P, uint64_t Length) {
196198
AllocationPtrTy<AK> AP = AllocationPtrTy<AK>::get(P);
197-
auto &A = getAllocation<AK>(AP);
199+
auto &A = getAllocation<AK>(AP, /*AccessId=*/0, /*PC=*/0);
198200
// TODO: Check length
199201
A.Length = 0;
200202
}
@@ -214,17 +216,18 @@ template <AllocationKind AK>
214216
AllocationArrayTy<AK>
215217
Allocations<AK>::Arr[SanitizerConfig<AK>::NUM_ALLOCATION_ARRAYS];
216218

217-
static void checkForMagic(bool IsGlobal, void *P, int64_t SourceId) {
219+
static void checkForMagic(bool IsGlobal, void *P, int64_t SourceId,
220+
uint64_t PC) {
218221
if (IsGlobal) {
219222
auto AP = AllocationPtrTy<AllocationKind::GLOBAL>::get(P);
220223
if (AP.Magic != SanitizerConfig<AllocationKind::GLOBAL>::MAGIC)
221224
__sanitizer_trap_info_ptr->garbagePointer<AllocationKind::GLOBAL>(
222-
AP, P, SourceId);
225+
AP, P, SourceId, PC);
223226
} else {
224227
auto AP = AllocationPtrTy<AllocationKind::LOCAL>::get(P);
225228
if (AP.Magic != SanitizerConfig<AllocationKind::LOCAL>::MAGIC)
226229
__sanitizer_trap_info_ptr->garbagePointer<AllocationKind::LOCAL>(
227-
AP, P, SourceId);
230+
AP, P, SourceId, PC);
228231
}
229232
}
230233

@@ -236,34 +239,34 @@ extern "C" {
236239
[[clang::disable_sanitizer_instrumentation, gnu::flatten, gnu::always_inline,
237240
gnu::used, gnu::retain]] _AS_PTR(void, AllocationKind::LOCAL)
238241
ompx_new_local(_AS_PTR(void, AllocationKind::LOCAL) Start, uint64_t Length,
239-
int64_t AllocationId, int64_t SourceId) {
242+
int64_t AllocationId, int64_t SourceId, uint64_t PC) {
240243
return AllocationTracker<AllocationKind::LOCAL>::create(
241-
Start, Length, AllocationId, 0, SourceId);
244+
Start, Length, AllocationId, 0, SourceId, PC);
242245
}
243246
[[clang::disable_sanitizer_instrumentation, gnu::flatten, gnu::always_inline,
244247
gnu::used, gnu::retain]] _AS_PTR(void, AllocationKind::GLOBAL)
245248
ompx_new_global(_AS_PTR(void, AllocationKind::GLOBAL) Start,
246249
uint64_t Length, int64_t AllocationId, uint32_t Slot,
247-
int64_t SourceId) {
250+
int64_t SourceId, uint64_t PC) {
248251
return AllocationTracker<AllocationKind::GLOBAL>::create(
249-
Start, Length, AllocationId, Slot, SourceId);
252+
Start, Length, AllocationId, Slot, SourceId, PC);
250253
}
251254
[[clang::disable_sanitizer_instrumentation, gnu::flatten, gnu::always_inline,
252255
gnu::used, gnu::retain]] void
253256
__sanitizer_register_host(_AS_PTR(void, AllocationKind::GLOBAL) Start,
254257
uint64_t Length, uint64_t Slot, int64_t SourceId) {
255258
AllocationTracker<AllocationKind::GLOBAL>::create(Start, Length, Slot, Slot,
256-
SourceId);
259+
SourceId, /*PC=*/0);
257260
}
258261
[[clang::disable_sanitizer_instrumentation, gnu::flatten, gnu::always_inline,
259262
gnu::used, gnu::retain]] void *
260263
ompx_new(void *Start, uint64_t Length, int64_t AllocationId, uint32_t Slot,
261-
int64_t SourceId) {
264+
int64_t SourceId, uint64_t PC) {
262265
if (REAL_PTR_IS_LOCAL(Start))
263266
return (void *)ompx_new_local((_AS_PTR(void, AllocationKind::LOCAL))Start,
264-
Length, AllocationId, SourceId);
267+
Length, AllocationId, SourceId, PC);
265268
return (void *)ompx_new_global((_AS_PTR(void, AllocationKind::GLOBAL))Start,
266-
Length, AllocationId, Slot, SourceId);
269+
Length, AllocationId, Slot, SourceId, PC);
267270
}
268271
[[clang::disable_sanitizer_instrumentation, gnu::flatten, gnu::always_inline,
269272
gnu::used, gnu::retain]] void
@@ -287,9 +290,9 @@ ompx_free_global(_AS_PTR(void, AllocationKind::GLOBAL) P, int64_t SourceId) {
287290
}
288291
[[clang::disable_sanitizer_instrumentation, gnu::flatten, gnu::always_inline,
289292
gnu::used, gnu::retain]] void
290-
ompx_free(void *P, int64_t SourceId) {
293+
ompx_free(void *P, int64_t SourceId, uint64_t PC) {
291294
bool IsGlobal = IS_GLOBAL(P);
292-
checkForMagic(IsGlobal, P, SourceId);
295+
checkForMagic(IsGlobal, P, SourceId, PC);
293296
if (IsGlobal)
294297
return ompx_free_global((_AS_PTR(void, AllocationKind::GLOBAL))P, SourceId);
295298
return ompx_free_local((_AS_PTR(void, AllocationKind::LOCAL))P, SourceId);
@@ -312,7 +315,7 @@ ompx_free(void *P, int64_t SourceId) {
312315
gnu::used, gnu::retain]] void *
313316
ompx_gep(void *P, uint64_t Offset, int64_t SourceId) {
314317
bool IsGlobal = IS_GLOBAL(P);
315-
checkForMagic(IsGlobal, P, SourceId);
318+
checkForMagic(IsGlobal, P, SourceId, /*PC=*/0);
316319
if (IsGlobal)
317320
return (void *)ompx_gep_global((_AS_PTR(void, AllocationKind::GLOBAL))P,
318321
Offset, SourceId);
@@ -323,66 +326,71 @@ ompx_gep(void *P, uint64_t Offset, int64_t SourceId) {
323326
[[clang::disable_sanitizer_instrumentation, gnu::flatten, gnu::always_inline,
324327
gnu::used, gnu::retain]] _AS_PTR(void, AllocationKind::LOCAL)
325328
ompx_check_local(_AS_PTR(void, AllocationKind::LOCAL) P, uint64_t Size,
326-
uint64_t AccessId, int64_t SourceId) {
329+
uint64_t AccessId, int64_t SourceId, uint64_t PC) {
327330
return AllocationTracker<AllocationKind::LOCAL>::check(P, Size, AccessId,
328-
SourceId);
331+
SourceId, PC);
329332
}
330333
[[clang::disable_sanitizer_instrumentation, gnu::flatten, gnu::always_inline,
331334
gnu::used, gnu::retain]] _AS_PTR(void, AllocationKind::GLOBAL)
332335
ompx_check_global(_AS_PTR(void, AllocationKind::GLOBAL) P, uint64_t Size,
333-
uint64_t AccessId, int64_t SourceId) {
336+
uint64_t AccessId, int64_t SourceId, uint64_t PC) {
334337
return AllocationTracker<AllocationKind::GLOBAL>::check(P, Size, AccessId,
335-
SourceId);
338+
SourceId, PC);
336339
}
337340
[[clang::disable_sanitizer_instrumentation, gnu::flatten, gnu::always_inline,
338341
gnu::used, gnu::retain]] void *
339-
ompx_check(void *P, uint64_t Size, uint64_t AccessId, int64_t SourceId) {
342+
ompx_check(void *P, uint64_t Size, uint64_t AccessId, int64_t SourceId,
343+
uint64_t PC) {
340344
bool IsGlobal = IS_GLOBAL(P);
341-
checkForMagic(IsGlobal, P, SourceId);
345+
checkForMagic(IsGlobal, P, SourceId, PC);
342346
if (IsGlobal)
343347
return (void *)ompx_check_global((_AS_PTR(void, AllocationKind::GLOBAL))P,
344-
Size, AccessId, SourceId);
348+
Size, AccessId, SourceId, PC);
345349
return (void *)ompx_check_local((_AS_PTR(void, AllocationKind::LOCAL))P, Size,
346-
AccessId, SourceId);
350+
AccessId, SourceId, PC);
347351
}
348352

349353
[[clang::disable_sanitizer_instrumentation, gnu::flatten, gnu::always_inline,
350354
gnu::used, gnu::retain]] _AS_PTR(void, AllocationKind::LOCAL)
351355
ompx_check_with_base_local(_AS_PTR(void, AllocationKind::LOCAL) P,
352356
_AS_PTR(void, AllocationKind::LOCAL) Start,
353357
uint64_t Length, uint32_t Tag, uint64_t Size,
354-
uint64_t AccessId, int64_t SourceId) {
358+
uint64_t AccessId, int64_t SourceId,
359+
uint64_t PC) {
355360
return AllocationTracker<AllocationKind::LOCAL>::checkWithBase(
356-
P, Start, Length, Tag, Size, AccessId, SourceId);
361+
P, Start, Length, Tag, Size, AccessId, SourceId, PC);
357362
}
358363

359364
[[clang::disable_sanitizer_instrumentation, gnu::flatten, gnu::always_inline,
360365
gnu::used, gnu::retain]] _AS_PTR(void, AllocationKind::GLOBAL)
361366
ompx_check_with_base_global(_AS_PTR(void, AllocationKind::GLOBAL) P,
362367
_AS_PTR(void, AllocationKind::GLOBAL) Start,
363368
uint64_t Length, uint32_t Tag, uint64_t Size,
364-
uint64_t AccessId, int64_t SourceId) {
369+
uint64_t AccessId, int64_t SourceId,
370+
uint64_t PC) {
365371
return AllocationTracker<AllocationKind::GLOBAL>::checkWithBase(
366-
P, Start, Length, Tag, Size, AccessId, SourceId);
372+
P, Start, Length, Tag, Size, AccessId, SourceId, PC);
367373
}
368374

369375
[[clang::disable_sanitizer_instrumentation, gnu::flatten, gnu::always_inline,
370376
gnu::used, gnu::retain]] _AS_PTR(void, AllocationKind::LOCAL)
371377
ompx_unpack_local(_AS_PTR(void, AllocationKind::LOCAL) P,
372378
int64_t SourceId) {
373-
return AllocationTracker<AllocationKind::LOCAL>::unpack(P, SourceId);
379+
return AllocationTracker<AllocationKind::LOCAL>::unpack(P, SourceId,
380+
/*PC=*/0);
374381
}
375382
[[clang::disable_sanitizer_instrumentation, gnu::flatten, gnu::always_inline,
376383
gnu::used, gnu::retain]] _AS_PTR(void, AllocationKind::GLOBAL)
377384
ompx_unpack_global(_AS_PTR(void, AllocationKind::GLOBAL) P,
378385
int64_t SourceId) {
379-
return AllocationTracker<AllocationKind::GLOBAL>::unpack(P, SourceId);
386+
return AllocationTracker<AllocationKind::GLOBAL>::unpack(P, SourceId,
387+
/*PC=*/0);
380388
}
381389
[[clang::disable_sanitizer_instrumentation, gnu::flatten, gnu::always_inline,
382390
gnu::used, gnu::retain]] void *
383391
ompx_unpack(void *P, int64_t SourceId) {
384392
bool IsGlobal = IS_GLOBAL(P);
385-
checkForMagic(IsGlobal, P, SourceId);
393+
checkForMagic(IsGlobal, P, SourceId, /*PC=*/0);
386394
if (IsGlobal)
387395
return (void *)ompx_unpack_global((_AS_PTR(void, AllocationKind::GLOBAL))P,
388396
SourceId);

0 commit comments

Comments
 (0)