https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129146

            Bug ID: 129146
           Summary: omp_fulfill_event from a non-OpenMP thread can use the
                    team after gomp_team_end has freed it
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libgomp
          Assignee: unassigned at gcc dot gnu.org
          Reporter: cq at smail dot nju.edu.cn
                CC: jakub at gcc dot gnu.org
  Target Milestone: ---

Created attachment 66331
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=66331&action=edit
test

When omp_fulfill_event is called from a thread outside the team (as in
task-detach-13.c), it keeps using the team after the task_count decrement that
lets the team finish:

  team->task_count--;
  team->task_detach_count--;
  ...
  if (!shackled_thread_p
      && !do_wake
      && team->task_detach_count == 0
      && gomp_team_barrier_waiting_for_tasks (&team->barrier))
    do_wake = 1;

  /* If we are running in an unshackled thread, the team might vanish before
     gomp_team_barrier_wake is run if we release the lock first, so keep the
     lock for the call in that case.  */
  ...
  if (!shackled_thread_p)
    gomp_mutex_unlock (&team->task_lock);

Holding task_lock doesn't keep the team alive, though. 
gomp_team_barrier_wait_final checks task_count without taking task_lock, so as
soon as the count reaches zero the primary thread can leave the final barrier,
and gomp_team_end frees the team (for a nested team) or keeps it for reuse,
while the external thread is still between the decrement and the unlock.

The window is only a few instructions wide, so to show it I widened it with a
usleep right after the task_count decrement (attached delay.diff, test only). 
The attached testcase runs one nested region whose detached task is fulfilled
by a plain pthread.  Under valgrind with the delay:

==1381904== Invalid read of size 4
==1381904==    at 0x487DA21: omp_fulfill_event (task.c:2817)
==1381904==    by 0x4013BA: fulfiller (fulfill-team-end.c:25)
==1381904==  Address 0x4ae25fc is 1,404 bytes inside a block of size 2,136
free'd
==1381904==    at 0x484988F: free (in
/usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==1381904==    by 0x4873CA5: GOMP_parallel (parallel.c:178)
==1381904==    by 0x4011B2: main (fulfill-team-end.c:42)
==1381904==  Block was alloc'd at
==1381904==    at 0x484DECC: memalign (in
/usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==1381904==    by 0x48694C8: gomp_aligned_alloc (alloc.c:71)
==1381904==    by 0x487E524: gomp_new_team (team.c:315)
==1381904==    by 0x4873C88: GOMP_parallel (parallel.c:176)

followed by an invalid write at the same line (task_detach_count--) and invalid
reads in gomp_team_barrier_waiting_for_tasks and gomp_mutex_unlock (6 errors
from 4 contexts).  With the delay but without valgrind the program doesn't
crash, but running 16 such regions in a row (the new test in the patch) hangs
in the final barrier of a later region every time (3 of 3); with the fix
described below it completes.

Without the delay I couldn't make the use-after-free show up.  With the sleeps
removed from the testcase, the program hangs almost every time instead (28 of
30 single-region runs), with or without the fix, which looks like PR124620. 
That one is a missed wake-up in the same function; this one is about the team's
lifetime.

A smaller related point: the decrement that the barrier reads without the lock
is a plain store made under task_lock, so the lock-free check has no
release/acquire pairing with the rest of omp_fulfill_event.  On x86 this
doesn't show.

I'll send a patch to gcc-patches that takes and releases task_lock in
gomp_team_end right after the final barrier, so the team isn't torn down until
omp_fulfill_event has dropped the lock.

We found this with Specular (https://github.com/specular-org/Specular), a
model-checking based bug finder we are developing.

Reply via email to