llvm-c166/offload/test/offloading/firstprivate_packed_buffer_lifetime.c
Abhinav Gaba a1d93f19be
[OpenMP][Offload] Fix use-after-free of the packed firstprivate transfer buffer (#212905)
`packAndTransfer()` called `submitData()`, which captured the address of
the data of
the `SmallVector` member of `PrivateArgumentManagerTy`, as the host
source of an
async transfer. The manager was later moved into an `AsyncInfo`
post-processing
lambda, changing the underlying address of the data:
`addPostProcessingFunction()`
copies the closure rather than moving it, but even if it "moved", the
address of the data owned by `SmallVector`'s inline capacity would have
changed.

The AMDGPU plugin read from the stale host source address when doing the
actual transfer.

Fix: allocate the packed buffer via
`getOrCreateSourceBufferForSubmitData()`, so
that `AsyncInfo` owns it. The non-packed
corresponding-pointer-initialization
path in this same class already built its source buffer that way.
2026-07-30 16:17:20 -07:00

53 lines
1.7 KiB
C

// RUN: %libomptarget-compile-generic
//
// RUN: env LIBOMPTARGET_DEBUG=1 LIBOMPTARGET_INFO=32 \
// RUN: %libomptarget-run-generic 2>&1 \
// RUN: | %fcheck-generic -check-prefix=DEBUG
//
// RUN: %libomptarget-run-generic 2>&1 | %fcheck-generic -check-prefix=CHECK
//
// REQUIRES: libomptarget-debug
// Ensure that the packed host buffer used to send firstprivate data to the
// device is released only after the transfer is done, i.e. that the buffer
// submitData() reads from is still alive when the queued device operations
// complete.
//
// The firstprivate payload has to be larger than a pointer for the runtime to
// allocate a separate host buffer for it, and small enough to be packed rather
// than transferred on its own, so use an explicitly-sized 48-byte array.
#include <stdint.h>
#include <stdio.h>
#define N 6
int main(void) {
uint64_t a[N];
for (int i = 0; i < N; ++i)
a[i] = i + 1;
int ok = 0;
// clang-format off
// The packed buffer is what gets submitted as the host source, ...
// DEBUG: Firstprivate array 0x{{.*}} of size 48 will be packed
// DEBUG: Submitting packed firstprivate arguments from host buffer at 0x[[#%x,BUF:]]
// DEBUG: Copying data from host to device, HstPtr=0x{{0*}}[[#BUF]], {{.*}}, Size=48
// ... and it is still alive once the queued operations have completed, which
// is when the release runs.
// DEBUG: Synchronization complete, running post-processing
// DEBUG: Releasing submitData source buffer at 0x{{0*}}[[#BUF]]
// clang-format on
#pragma omp target firstprivate(a) map(from : ok)
{
ok = 1;
for (int i = 0; i < N; ++i)
if (a[i] != i + 1)
ok = 0;
}
// CHECK: values_intact=1
printf("values_intact=%d\n", ok);
return 0;
}