Skip to content

Commit 30f8ec4

Browse files
fweigdavidrohr
authored andcommitted
TPCClusterFinder: Fix buffer overflow in compaction step.
1 parent 35ac97d commit 30f8ec4

3 files changed

Lines changed: 10 additions & 7 deletions

File tree

GPU/GPUTracking/TPCClusterFinder/GPUTPCCFStreamCompaction.cxx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ template <>
2525
GPUdii() void GPUTPCCFStreamCompaction::Thread<GPUTPCCFStreamCompaction::nativeScanUpStart>(int nBlocks, int nThreads, int iBlock, int iThread, GPUSharedMemory& smem, processorType& clusterer, int iBuf, int stage)
2626
{
2727
int nElems = compactionElems(clusterer, stage);
28-
nativeScanUpStartImpl(get_num_groups(0), get_local_size(0), get_group_id(0), get_local_id(0), smem, clusterer.mPisPeak, clusterer.mPbuf + (iBuf - 1) * clusterer.mBufSize, clusterer.mPbuf + iBuf * clusterer.mBufSize, nElems);
28+
size_t bufferSize = (stage) ? clusterer.mNMaxClusters : clusterer.mNMaxPeaks;
29+
nativeScanUpStartImpl(get_num_groups(0), get_local_size(0), get_group_id(0), get_local_id(0), smem, clusterer.mPisPeak, clusterer.mPbuf + (iBuf - 1) * clusterer.mBufSize, clusterer.mPbuf + iBuf * clusterer.mBufSize, nElems, bufferSize);
2930
}
3031

3132
GPUd() void GPUTPCCFStreamCompaction::nativeScanUpStartImpl(int nBlocks, int nThreads, int iBlock, int iThread, GPUSharedMemory& smem,
@@ -122,6 +123,7 @@ template <>
122123
GPUdii() void GPUTPCCFStreamCompaction::Thread<GPUTPCCFStreamCompaction::compactDigit>(int nBlocks, int nThreads, int iBlock, int iThread, GPUSharedMemory& smem, processorType& clusterer, int iBuf, int stage, deprecated::PackedDigit* in, deprecated::PackedDigit* out)
123124
{
124125
unsigned int nElems = compactionElems(clusterer, stage);
126+
125127
compactDigitImpl(get_num_groups(0), get_local_size(0), get_group_id(0), get_local_id(0), smem, in, out, clusterer.mPisPeak, clusterer.mPbuf + (iBuf - 1) * clusterer.mBufSize, clusterer.mPbuf + iBuf * clusterer.mBufSize, nElems);
126128
unsigned int lastId = get_global_size(0) - 1;
127129
if ((unsigned int)get_global_id(0) == lastId) {
@@ -151,17 +153,18 @@ GPUd() void GPUTPCCFStreamCompaction::compactDigitImpl(int nBlocks, int nThreads
151153
int pred = (iAmDummy) ? 0 : predicate[idx];
152154
int scanRes = work_group_scan_inclusive_add(pred);
153155

154-
int compIdx = scanRes;
156+
size_t compIdx = scanRes;
155157
if (gid) {
156158
compIdx += incr[gid - 1];
157159
}
158160

159-
if (pred) {
160-
out[compIdx - 1] = in[idx];
161+
size_t tgtIdx = compIdx - 1;
162+
if (pred && tgtIdx < bufferSize) {
163+
out[tgtIdx] = in[idx];
161164
}
162165

163166
if (idx == lastItem) {
164-
newIdx[idx] = compIdx; // TODO: Eventually, we can just return the last value, no need to store to memory
167+
newIdx[idx] = CAMath::Min(compIdx, bufferSize); // TODO: Eventually, we can just return the last value, no need to store to memory
165168
}
166169
}
167170

GPU/GPUTracking/TPCClusterFinder/GPUTPCCFStreamCompaction.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class GPUTPCCFStreamCompaction
5555
static GPUd() void compactDigitImpl(int, int, int, int, GPUSharedMemory&,
5656
const deprecated::Digit*, deprecated::Digit*,
5757
const uchar*, int*, const int*,
58-
int);
58+
int, size_t)
5959

6060
#ifdef HAVE_O2HEADERS
6161
typedef GPUTPCClusterFinder processorType;

GPU/GPUTracking/TPCClusterFinder/GPUTPCClusterFinder.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ void GPUTPCClusterFinder::RegisterMemoryAllocation()
6868
void GPUTPCClusterFinder::SetMaxData(const GPUTrackingInOutPointers& io)
6969
{
7070
mNMaxPeaks = 0.5f * mNMaxDigits;
71-
mNMaxClusters = 0.2f * mNMaxPeaks;
71+
mNMaxClusters = mNMaxPeaks;
7272
mNMaxClusterPerRow = 0.01f * mNMaxDigits;
7373
mBufSize = nextMultipleOf<std::max<int>(GPUCA_MEMALIGN, mScanWorkGroupSize)>(mNMaxDigits);
7474
mNBufs = getNSteps(mBufSize);

0 commit comments

Comments
 (0)