[Openmp-commits] [PATCH] D29914: [OpenMP] libomptarget: Protect parent struct from being deallocated

George Rokos via Phabricator via Openmp-commits openmp-commits at lists.llvm.org
Mon Feb 13 14:56:52 PST 2017


grokos created this revision.

Under certain circumstances, a parent struct may be deallocated accidentally when one of the mapped members is being unmapped. This patch fixes the problem.


Repository:
  rL LLVM

https://reviews.llvm.org/D29914

Files:
  libomptarget/src/omptarget.cpp


Index: libomptarget/src/omptarget.cpp
===================================================================
--- libomptarget/src/omptarget.cpp
+++ libomptarget/src/omptarget.cpp
@@ -1433,19 +1433,22 @@
   bool *is_ptr_old = (bool *) alloca(arg_num * sizeof(bool));
   // old entry is member of member_of[old] cmb_entry
   int *member_of = (int *) alloca(arg_num * sizeof(int));
+  // temporary storage for modifications of the original arg_types
+  int32_t *mod_arg_types = (int32_t *) alloca(arg_num  *sizeof(int32_t));
 
   DP("Translating %d map entries\n", arg_num);
   for (int i = 0; i < arg_num; ++i) {
     member_of[i] = -1;
     is_ptr_old[i] = false;
+    mod_arg_types[i] = arg_types[i];
     // Scan previous entries to see whether this entry shares the same base
     for (int j = 0; j < i; ++j) {
       void *new_begin_addr = NULL;
       void *new_end_addr = NULL;
 
-      if (arg_types[i] & OMP_TGT_OLDMAPTYPE_MAP_PTR) {
+      if (mod_arg_types[i] & OMP_TGT_OLDMAPTYPE_MAP_PTR) {
         if (args_base[i] == args[j]) {
-          if (!(arg_types[j] & OMP_TGT_OLDMAPTYPE_MAP_PTR)) {
+          if (!(mod_arg_types[j] & OMP_TGT_OLDMAPTYPE_MAP_PTR)) {
             DP("Entry %d has the same base as entry %d's begin address\n", i,
                 j);
             new_begin_addr = args_base[i];
@@ -1455,10 +1458,18 @@
           } else {
             DP("Entry %d has the same base as entry %d's begin address, but "
                 "%d's base was a MAP_PTR too\n", i, j, j);
+            int32_t to_from_always_delete =
+                OMP_TGT_OLDMAPTYPE_TO | OMP_TGT_OLDMAPTYPE_FROM |
+                OMP_TGT_OLDMAPTYPE_ALWAYS | OMP_TGT_OLDMAPTYPE_DELETE;
+            if (mod_arg_types[j] & to_from_always_delete) {
+              DP("Resetting to/from/always/delete flags for entry %d because "
+                  "it is only a pointer to pointer\n", j);
+              mod_arg_types[j] &= ~to_from_always_delete;
+            }
           }
         }
       } else {
-        if (!(arg_types[i] & OMP_TGT_OLDMAPTYPE_FIRST_MAP) &&
+        if (!(mod_arg_types[i] & OMP_TGT_OLDMAPTYPE_FIRST_MAP) &&
             args_base[i] == args_base[j]) {
           DP("Entry %d has the same base address as entry %d\n", i, j);
           new_begin_addr = args[i];
@@ -1476,7 +1487,7 @@
           // Initialize new entry
           cmb_entries[id].num_members = 1;
           cmb_entries[id].base_addr = args_base[j];
-          if (arg_types[j] & OMP_TGT_OLDMAPTYPE_MAP_PTR) {
+          if (mod_arg_types[j] & OMP_TGT_OLDMAPTYPE_MAP_PTR) {
             cmb_entries[id].begin_addr = args_base[j];
             cmb_entries[id].end_addr = (char *)args_base[j] + arg_sizes[j];
           } else {
@@ -1554,7 +1565,7 @@
     new_args_base[nid] = args_base[i];
     new_args[nid] = args[i];
     new_arg_sizes[nid] = arg_sizes[i];
-    int64_t old_type = arg_types[i];
+    int64_t old_type = mod_arg_types[i];
 
     if (is_ptr_old[i]) {
       // Reset TO and FROM flags
@@ -1787,13 +1798,13 @@
         " - is%s last\n", arg_sizes[i], DPxPTR(TgtPtrBegin),
         (IsLast ? "" : " not"));
 
+    bool DelEntry = IsLast || ForceDelete;
+
     if ((arg_types[i] & OMP_TGT_MAPTYPE_MEMBER_OF) &&
         !(arg_types[i] & OMP_TGT_MAPTYPE_PTR_AND_OBJ)) {
-      IsLast = false; // protect parent struct from being deallocated
+      DelEntry = false; // protect parent struct from being deallocated
     }
 
-    bool DelEntry = IsLast || ForceDelete;
-
     if ((arg_types[i] & OMP_TGT_MAPTYPE_FROM) || DelEntry) {
       // Move data back to the host
       if (arg_types[i] & OMP_TGT_MAPTYPE_FROM) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D29914.88264.patch
Type: text/x-patch
Size: 3613 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/openmp-commits/attachments/20170213/53c77bf6/attachment.bin>


More information about the Openmp-commits mailing list