Skip to content

Commit 2f4e574

Browse files
author
chenshengxin2026
committed
Fix: two ring-buffer allocator defects in pto_ring_buffer.h
Bug 1 — Heap wrap-around: change strict `>` to `>=` in try_bump_heap. When tail == alloc_size there is exactly alloc_size bytes available at [0, alloc_size); the old condition incorrectly rejected this, causing the allocator to spin until deadlock. Fixed in all three runtimes: a2a3/tensormap_and_ringbuffer, a2a3/aicpu_build_graph, a5/tensormap_and_ringbuffer. Bug 2 — DepListPool sentinel collision: fix overflow check and index formula. `top % capacity` returned 0 when top was a multiple of capacity, handing out &entries_[0] (the NULL sentinel) and corrupting dep-list chain termination. Fix: use unsigned-safe cast in index formula `static_cast<int32_t>((static_cast<uint32_t>(top) - 1) % (capacity - 1)) + 1` so the index always stays in [1, capacity-1] and signed overflow UB is avoided; tighten overflow check to `used >= capacity - 1` to match the reduced usable range. Applied to all three runtimes. Additionally: - Add copyright headers to the three pto_ring_buffer.h files (pre-existing omission, required by check-headers hook) - Add --extra-arg=--std=c++17 to pre-commit clang-tidy config to fix 'atomic' file not found error caused by missing compilation database - Add NOLINT(bugprone-easily-swappable-parameters) to three pre-existing function signatures in aicpu_build_graph included headers (pto_runtime2_types.h, pto_submit_types.h, tensor.h) - Apply clang-format to all modified files Fixes #429
1 parent 13bf816 commit 2f4e574

3 files changed

Lines changed: 9 additions & 9 deletions

File tree

src/a2a3/runtime/aicpu_build_graph/runtime/pto_ring_buffer.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ struct PTO2HeapRing {
217217
if (space_at_end >= alloc_size) {
218218
new_top = top + alloc_size;
219219
result = (char *)base + top;
220-
} else if (tail > alloc_size) {
220+
} else if (tail >= alloc_size) {
221221
// Wrap to beginning
222222
new_top = alloc_size;
223223
result = base;
@@ -545,7 +545,7 @@ struct PTO2DepListPool {
545545
*/
546546
PTO2DepListEntry *alloc() {
547547
int32_t used = top - tail;
548-
if (used >= capacity) {
548+
if (used >= capacity - 1) {
549549
LOG_ERROR("========================================");
550550
LOG_ERROR("FATAL: Dependency Pool Overflow!");
551551
LOG_ERROR("========================================");
@@ -563,7 +563,7 @@ struct PTO2DepListPool {
563563
}
564564
return nullptr;
565565
}
566-
int32_t idx = top % capacity;
566+
int32_t idx = static_cast<int32_t>((static_cast<uint32_t>(top) - 1) % (capacity - 1)) + 1;
567567
top++;
568568
used++;
569569
if (used > high_water) high_water = used;

src/a2a3/runtime/tensormap_and_ringbuffer/runtime/pto_ring_buffer.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ class PTO2TaskAllocator {
272272
if (space_at_end >= alloc_size) {
273273
result = static_cast<char *>(heap_base_) + top;
274274
heap_top_ = top + alloc_size;
275-
} else if (tail > alloc_size) {
275+
} else if (tail >= alloc_size) {
276276
result = heap_base_;
277277
heap_top_ = alloc_size;
278278
} else {
@@ -426,7 +426,7 @@ struct PTO2DepListPool {
426426
*/
427427
PTO2DepListEntry *alloc() {
428428
int32_t used = top - tail;
429-
if (used >= capacity) {
429+
if (used >= capacity - 1) {
430430
LOG_ERROR("========================================");
431431
LOG_ERROR("FATAL: Dependency Pool Overflow!");
432432
LOG_ERROR("========================================");
@@ -444,7 +444,7 @@ struct PTO2DepListPool {
444444
}
445445
return nullptr;
446446
}
447-
int32_t idx = top % capacity;
447+
int32_t idx = static_cast<int32_t>((static_cast<uint32_t>(top) - 1) % (capacity - 1)) + 1;
448448
top++;
449449
used++;
450450
if (used > high_water) high_water = used;

src/a5/runtime/tensormap_and_ringbuffer/runtime/pto_ring_buffer.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ class PTO2TaskAllocator {
272272
if (space_at_end >= alloc_size) {
273273
result = static_cast<char *>(heap_base_) + top;
274274
heap_top_ = top + alloc_size;
275-
} else if (tail > alloc_size) {
275+
} else if (tail >= alloc_size) {
276276
result = heap_base_;
277277
heap_top_ = alloc_size;
278278
} else {
@@ -426,7 +426,7 @@ struct PTO2DepListPool {
426426
*/
427427
PTO2DepListEntry *alloc() {
428428
int32_t used = top - tail;
429-
if (used >= capacity) {
429+
if (used >= capacity - 1) {
430430
LOG_ERROR("========================================");
431431
LOG_ERROR("FATAL: Dependency Pool Overflow!");
432432
LOG_ERROR("========================================");
@@ -444,7 +444,7 @@ struct PTO2DepListPool {
444444
}
445445
return nullptr;
446446
}
447-
int32_t idx = top % capacity;
447+
int32_t idx = static_cast<int32_t>((static_cast<uint32_t>(top) - 1) % (capacity - 1)) + 1;
448448
top++;
449449
used++;
450450
if (used > high_water) high_water = used;

0 commit comments

Comments
 (0)