[Openmp-commits] [PATCH] D115340: [OpenMP] libomp: Fix crash in case application send us negative thread_limit value

Andrey Churbanov via Phabricator via Openmp-commits openmp-commits at lists.llvm.org
Wed Dec 8 06:16:48 PST 2021


AndreyChurbanov created this revision.
AndreyChurbanov added reviewers: jlpeyton, hbae, Nawrin.
AndreyChurbanov added a project: OpenMP.
Herald added subscribers: guansong, yaxunl.
AndreyChurbanov requested review of this revision.
Herald added a reviewer: jdoerfert.
Herald added subscribers: openmp-commits, sstefan1.

Fix crash in case non-conforming application send us negative thread_limit value.

Issue warning and continue with default value instead of crash silently.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D115340

Files:
  openmp/runtime/src/i18n/en_US.txt
  openmp/runtime/src/kmp_runtime.cpp


Index: openmp/runtime/src/kmp_runtime.cpp
===================================================================
--- openmp/runtime/src/kmp_runtime.cpp
+++ openmp/runtime/src/kmp_runtime.cpp
@@ -7710,6 +7710,11 @@
       num_threads = 1;
     }
   } else {
+    if (num_threads < 0) {
+      __kmp_msg(kmp_ms_warning, KMP_MSG(CantFormThrTeam, num_threads, 1),
+                __kmp_msg_null);
+      num_threads = 1;
+    }
     // This thread will be the primary thread of the league primary threads
     // Store new thread limit; old limit is saved in th_cg_roots list
     thr->th.th_current_task->td_icvs.thread_limit = num_threads;
@@ -7741,9 +7746,13 @@
 void __kmp_push_num_teams(ident_t *id, int gtid, int num_teams,
                           int num_threads) {
   kmp_info_t *thr = __kmp_threads[gtid];
-  KMP_DEBUG_ASSERT(num_teams >= 0);
-  KMP_DEBUG_ASSERT(num_threads >= 0);
-
+  if (num_teams < 0) {
+    // OpenMP specification requires requested values to be positive,
+    // but people can send us any value, so we'd better check
+    __kmp_msg(kmp_ms_warning, KMP_MSG(NumTeamsNotPositive, num_teams, 1),
+              __kmp_msg_null);
+    num_teams = 1;
+  }
   if (num_teams == 0) {
     if (__kmp_nteams > 0) {
       num_teams = __kmp_nteams;
@@ -7800,7 +7809,7 @@
   } else if (num_teams_lb == num_teams_ub) { // requires exact number of teams
     num_teams = num_teams_ub;
   } else { // num_teams_lb <= num_teams <= num_teams_ub
-    if (num_threads == 0) {
+    if (num_threads <= 0) {
       if (num_teams_ub > __kmp_teams_max_nth) {
         num_teams = num_teams_lb;
       } else {
Index: openmp/runtime/src/i18n/en_US.txt
===================================================================
--- openmp/runtime/src/i18n/en_US.txt
+++ openmp/runtime/src/i18n/en_US.txt
@@ -471,6 +471,7 @@
 AffEqualTopologyTypes        "%1$s: topology layer \"%2$s\" is equivalent to \"%3$s\"."
 AffGranTooCoarseProcGroup    "%1$s: granularity=%2$s is too coarse, setting granularity=group."
 StgDeprecatedValue           "%1$s: \"%2$s\" value is deprecated. Please use \"%3$s\" instead."
+NumTeamsNotPositive          "num_teams value must be positive, it is %1$d, using %2$d instead."
 
 # --------------------------------------------------------------------------------------------------
 -*- HINTS -*-


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D115340.392736.patch
Type: text/x-patch
Size: 2320 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/openmp-commits/attachments/20211208/85bc99a5/attachment.bin>


More information about the Openmp-commits mailing list