<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Misuse of libomptarget plugin interface"
   href="https://bugs.llvm.org/show_bug.cgi?id=49207">49207</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Misuse of libomptarget plugin interface
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>OpenMP
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>release blocker
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Runtime Library
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>tianshilei1992@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>This bug was reported by Guilherme Valarini via Openmp-dev
(<a href="mailto:openmp-dev@lists.llvm.org">openmp-dev@lists.llvm.org</a>). The link is
<a href="https://lists.llvm.org/pipermail/openmp-dev/2021-February/003867.html">https://lists.llvm.org/pipermail/openmp-dev/2021-February/003867.html</a>

The following is the bug description.

In both cases, the address to a local variable is passed as the host data to be
copied to the target device, but since this function can be asynchronously
executed, such variables can get out of scope when the actual submission is
done.


```
// File: openmp/libomptarget/src/omptarget.cpp:419
if (arg_types[i] & OMP_TGT_MAPTYPE_PTR_AND_OBJ && !IsHostPtr) {
  DP("Update pointer (" DPxMOD ") -> [" DPxMOD "]\n",
      DPxPTR(PointerTgtPtrBegin), DPxPTR(TgtPtrBegin));
  uint64_t Delta = (uint64_t)HstPtrBegin - (uint64_t)HstPtrBase;
  void *TgtPtrBase = (void *)((uint64_t)TgtPtrBegin - Delta);
  int rt = Device.submitData(PointerTgtPtrBegin, &TgtPtrBase,
                              sizeof(void *), async_info_ptr);
  if (rt != OFFLOAD_SUCCESS) {
    REPORT("Copying data to device failed.\n");
    return OFFLOAD_FAIL;
  }
  // create shadow pointers for this entry
  Device.ShadowMtx.lock();
  Device.ShadowPtrMap[Pointer_HstPtrBegin] = {
      HstPtrBase, PointerTgtPtrBegin, TgtPtrBase};
  Device.ShadowMtx.unlock();
}
```

```
// File: openmp/libomptarget/src/omptarget.cpp:1141
DP("Parent lambda base " DPxMOD "\n", DPxPTR(TgtPtrBase));
uint64_t Delta = (uint64_t)HstPtrBegin - (uint64_t)HstPtrBase;
void *TgtPtrBegin = (void *)((uintptr_t)TgtPtrBase + Delta);
void *PointerTgtPtrBegin = Device.getTgtPtrBegin(
    HstPtrVal, ArgSizes[I], IsLast, false, IsHostPtr);
if (!PointerTgtPtrBegin) {
  DP("No lambda captured variable mapped (" DPxMOD ") - ignored\n",
      DPxPTR(HstPtrVal));
  continue;
}
if (PM->RTLs.RequiresFlags & OMP_REQ_UNIFIED_SHARED_MEMORY &&
    TgtPtrBegin == HstPtrBegin) {
  DP("Unified memory is active, no need to map lambda captured"
      "variable (" DPxMOD ")\n",
      DPxPTR(HstPtrVal));
  continue;
}
DP("Update lambda reference (" DPxMOD ") -> [" DPxMOD "]\n",
    DPxPTR(PointerTgtPtrBegin), DPxPTR(TgtPtrBegin));
Ret = Device.submitData(TgtPtrBegin, &PointerTgtPtrBegin,
                        sizeof(void *), AsyncInfo);
if (Ret != OFFLOAD_SUCCESS) {
  REPORT("Copying data to device failed.\n");
  return OFFLOAD_FAIL;
}
```</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>