Skip to content

Commit f5334f5

Browse files
authored
[OpenMP] Add debug checks for divide by zero (#83300)
1 parent 9ac0315 commit f5334f5

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

openmp/runtime/src/kmp_affinity.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1777,6 +1777,8 @@ static bool __kmp_affinity_create_hwloc_map(kmp_i18n_id_t *const msg_id) {
17771777
__kmp_nThreadsPerCore = __kmp_hwloc_get_nobjs_under_obj(o, HWLOC_OBJ_PU);
17781778
else
17791779
__kmp_nThreadsPerCore = 1; // no CORE found
1780+
if (__kmp_nThreadsPerCore == 0)
1781+
__kmp_nThreadsPerCore = 1;
17801782
__kmp_ncores = __kmp_xproc / __kmp_nThreadsPerCore;
17811783
if (nCoresPerPkg == 0)
17821784
nCoresPerPkg = 1; // to prevent possible division by 0

openmp/runtime/src/kmp_sched.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ char const *traits_t<long>::spec = "ld";
5252
} else if (i > 0) { \
5353
t = (u - l) / i + 1; \
5454
} else { \
55+
KMP_DEBUG_ASSERT(i != 0); \
5556
t = (l - u) / (-i) + 1; \
5657
} \
5758
KMP_COUNT_VALUE(stat, t); \
@@ -284,6 +285,7 @@ static void __kmp_for_static_init(ident_t *loc, kmp_int32 global_tid,
284285
// upper-lower can exceed the limit of signed type
285286
trip_count = (UT)(*pupper - *plower) / incr + 1;
286287
} else {
288+
KMP_DEBUG_ASSERT(incr != 0);
287289
trip_count = (UT)(*plower - *pupper) / (-incr) + 1;
288290
}
289291

@@ -318,6 +320,7 @@ static void __kmp_for_static_init(ident_t *loc, kmp_int32 global_tid,
318320
if (plastiter != NULL)
319321
*plastiter = (tid == trip_count - 1);
320322
} else {
323+
KMP_DEBUG_ASSERT(nth != 0);
321324
if (__kmp_static == kmp_sch_static_balanced) {
322325
UT small_chunk = trip_count / nth;
323326
UT extras = trip_count % nth;
@@ -358,6 +361,7 @@ static void __kmp_for_static_init(ident_t *loc, kmp_int32 global_tid,
358361
case kmp_sch_static_chunked: {
359362
ST span;
360363
UT nchunks;
364+
KMP_DEBUG_ASSERT(chunk != 0);
361365
if (chunk < 1)
362366
chunk = 1;
363367
else if ((UT)chunk > trip_count)
@@ -383,6 +387,7 @@ static void __kmp_for_static_init(ident_t *loc, kmp_int32 global_tid,
383387
}
384388
case kmp_sch_static_balanced_chunked: {
385389
T old_upper = *pupper;
390+
KMP_DEBUG_ASSERT(nth != 0);
386391
// round up to make sure the chunk is enough to cover all iterations
387392
UT span = (trip_count + nth - 1) / nth;
388393

@@ -398,8 +403,10 @@ static void __kmp_for_static_init(ident_t *loc, kmp_int32 global_tid,
398403
} else if (*pupper < old_upper)
399404
*pupper = old_upper;
400405

401-
if (plastiter != NULL)
406+
if (plastiter != NULL) {
407+
KMP_DEBUG_ASSERT(chunk != 0);
402408
*plastiter = (tid == ((trip_count - 1) / (UT)chunk));
409+
}
403410
break;
404411
}
405412
default:
@@ -417,6 +424,7 @@ static void __kmp_for_static_init(ident_t *loc, kmp_int32 global_tid,
417424
// Calculate chunk in case it was not specified; it is specified for
418425
// kmp_sch_static_chunked
419426
if (schedtype == kmp_sch_static) {
427+
KMP_DEBUG_ASSERT(nth != 0);
420428
cur_chunk = trip_count / nth + ((trip_count % nth) ? 1 : 0);
421429
}
422430
// 0 - "static" schedule
@@ -547,6 +555,7 @@ static void __kmp_dist_for_static_init(ident_t *loc, kmp_int32 gtid,
547555
// upper-lower can exceed the limit of signed type
548556
trip_count = (UT)(*pupper - *plower) / incr + 1;
549557
} else {
558+
KMP_DEBUG_ASSERT(incr != 0);
550559
trip_count = (UT)(*plower - *pupper) / (-incr) + 1;
551560
}
552561

@@ -568,6 +577,7 @@ static void __kmp_dist_for_static_init(ident_t *loc, kmp_int32 gtid,
568577
*plastiter = (tid == 0 && team_id == trip_count - 1);
569578
} else {
570579
// Get the team's chunk first (each team gets at most one chunk)
580+
KMP_DEBUG_ASSERT(nteams != 0);
571581
if (__kmp_static == kmp_sch_static_balanced) {
572582
UT chunkD = trip_count / nteams;
573583
UT extras = trip_count % nteams;
@@ -619,6 +629,7 @@ static void __kmp_dist_for_static_init(ident_t *loc, kmp_int32 gtid,
619629
// upper-lower can exceed the limit of signed type
620630
trip_count = (UT)(*pupperDist - *plower) / incr + 1;
621631
} else {
632+
KMP_DEBUG_ASSERT(incr != 0);
622633
trip_count = (UT)(*plower - *pupperDist) / (-incr) + 1;
623634
}
624635
KMP_DEBUG_ASSERT(trip_count);
@@ -637,6 +648,7 @@ static void __kmp_dist_for_static_init(ident_t *loc, kmp_int32 gtid,
637648
if (*plastiter != 0 && !(tid == trip_count - 1))
638649
*plastiter = 0;
639650
} else {
651+
KMP_DEBUG_ASSERT(nth != 0);
640652
if (__kmp_static == kmp_sch_static_balanced) {
641653
UT chunkL = trip_count / nth;
642654
UT extras = trip_count % nth;
@@ -684,9 +696,11 @@ static void __kmp_dist_for_static_init(ident_t *loc, kmp_int32 gtid,
684696
*pstride = span * nth;
685697
*plower = *plower + (span * tid);
686698
*pupper = *plower + span - incr;
687-
if (plastiter != NULL)
699+
if (plastiter != NULL) {
700+
KMP_DEBUG_ASSERT(chunk != 0);
688701
if (*plastiter != 0 && !(tid == ((trip_count - 1) / (UT)chunk) % nth))
689702
*plastiter = 0;
703+
}
690704
break;
691705
}
692706
default:
@@ -809,6 +823,7 @@ static void __kmp_team_static_init(ident_t *loc, kmp_int32 gtid,
809823
// upper-lower can exceed the limit of signed type
810824
trip_count = (UT)(upper - lower) / incr + 1;
811825
} else {
826+
KMP_DEBUG_ASSERT(incr != 0);
812827
trip_count = (UT)(lower - upper) / (-incr) + 1;
813828
}
814829
if (chunk < 1)
@@ -817,8 +832,10 @@ static void __kmp_team_static_init(ident_t *loc, kmp_int32 gtid,
817832
*p_st = span * nteams;
818833
*p_lb = lower + (span * team_id);
819834
*p_ub = *p_lb + span - incr;
820-
if (p_last != NULL)
835+
if (p_last != NULL) {
836+
KMP_DEBUG_ASSERT(chunk != 0);
821837
*p_last = (team_id == ((trip_count - 1) / (UT)chunk) % nteams);
838+
}
822839
// Correct upper bound if needed
823840
if (incr > 0) {
824841
if (*p_ub < *p_lb) // overflow?

0 commit comments

Comments
 (0)