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

            Bug ID: 129131
           Summary: taskwait depend(inout: omp_all_memory) uses the freed
                    dependence hash table
           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 66325
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=66325&action=edit
test

gomp_task_maybe_wait_for_dependencies shrinks the dependence hash table
before walking it for depend(inout: omp_all_memory):

          size_t size = htab_size (task->depend_hash);
          if (htab_elements (task->depend_hash) * 8 < size && size > 32)
            htab_expand (task->depend_hash);

htab_expand frees the old table and returns the new one, but the result is
dropped.  The walk right below then reads the freed table, and
task->depend_hash is left pointing to it, so it is freed a second time
later.

To hit it, the table has to grow past 32 slots and then become mostly
empty:

#include <stdlib.h>
#include <unistd.h>

#define N 64
int a[N], b;

int
main ()
{
  #pragma omp parallel num_threads (2)
  #pragma omp single
  {
    #pragma omp task depend(out: b)
    usleep (10000);
    for (int i = 0; i < N; i++)
      {
        #pragma omp task depend(in: b) depend(out: a[i])
        a[i] = i;
      }
    #pragma omp taskwait
    #pragma omp task depend(out: a[0])
    a[0] = 42;
    #pragma omp taskwait depend(inout: omp_all_memory)
    if (a[0] != 42)
      abort ();
  }
  return 0;
}

$ gcc -fopenmp -O2 pr.c && ./a.out
corrupted size vs. prev_size while consolidating
Aborted

valgrind on a trunk build:

Invalid read of size 8
   at htab_size (hashtab.h:173)
   by gomp_task_maybe_wait_for_dependencies (task.c:2058)
 Address 0x4ae6ba0 is 0 bytes inside a block of size 1,048 free'd
   at free
   by htab_free (task.c:46)
   by htab_expand (hashtab.h:326)
   by gomp_task_maybe_wait_for_dependencies (task.c:2052)

Same with 13.3 and with trunk (r17-6775).  The code came in with
r13-337 (omp_all_memory support).

Assigning the result fixes it:

            task->depend_hash = htab_expand (task->depend_hash);

I'll send the patch with a testcase to gcc-patches.

We found this with Specula, a model-checking based bug finder we are
developing.

Reply via email to