<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/55073>55073</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Segfault in clangs ompt_callback system
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          SpinTensor
      </td>
    </tr>
</table>

<pre>
    Hi,

please consider the following test code:
```
#include <stdio.h>
#include <omp.h>
#include <omp-tools.h>

static void my_ompt_callback_implicit_task(ompt_scope_endpoint_t endpoint,
                                           ompt_data_t *parallel_data,
                                           ompt_data_t *task_data,
                                           unsigned int actual_parallelism,
                                           unsigned int index,
                                           int flags) {
   switch (endpoint) {
      case ompt_scope_begin:
         fprintf(stderr, "implicit_task begin:    t=%d l=%d\n",
                 omp_get_thread_num(), omp_get_level());
         break;
      case ompt_scope_end:
         fprintf(stderr, "implicit_task end:      t=%d l=%d\n",
                 omp_get_thread_num(), omp_get_level());
         break;
      case ompt_scope_beginend:
         fprintf(stderr, "implicit_task beginend: t=%d l=%d\n",
                 omp_get_thread_num(), omp_get_level());
         break;
      default:
         break;
   }
}

// initialize callbacks
int my_ompt_initialize(ompt_function_lookup_t lookup, int initial_device_num,
                       ompt_data_t *tool_data) {
   // Get the set_callback function pointer
   ompt_set_callback_t ompt_set_callback = (ompt_set_callback_t)lookup("ompt_set_callback");
   // register the available callback functions
   ompt_callback_implicit_task_t f_ompt_callback_implicit_task = &my_ompt_callback_implicit_task;
   ompt_set_callback(ompt_callback_implicit_task, (ompt_callback_t)f_ompt_callback_implicit_task);

   return 1; // success: activates tool
}
void my_ompt_finalize(ompt_data_t *tool_data) {
   (void) tool_data;
}
// start tool
ompt_start_tool_result_t *ompt_start_tool(unsigned int omp_version,
                                          const char *runtime_version) {
   static ompt_start_tool_result_t ompt_start_tool_result;
   ompt_start_tool_result.initialize = &my_ompt_initialize;
   ompt_start_tool_result.finalize = &my_ompt_finalize;
   return &ompt_start_tool_result; // success: registers tool
}

int main(int argc, char **argv) {
   #pragma omp parallel num_threads(2)
   {
      int threadnum = omp_get_thread_num();
   }

   return 0;
}
```
In the code I register the implicit task callback function for the ompt_callback system.
Then I compile it with the latest git version of llvm:
`clang -fopenmp -o test.x test.c`
```
clang --version
clang version 15.0.0 (/home/fuhl/software/llvm_src/clang 34312f1f0c4f56ae78577783ec62bee3fb5dab90)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /opt/bm/llvm-14.0.0/bin
```
```
$ ldd test.x 
        linux-vdso.so.1 =>  (0x00007ffcac3a1000)
        libomp.so => /opt/bm/llvm-14.0.0/lib/libomp.so (0x00002b75f1ab4000)
        libpthread.so.0 => /lib64/libpthread.so.0 (0x00002b75f1dc2000)
        libc.so.6 => /lib64/libc.so.6 (0x00002b75f1fde000)
        librt.so.1 => /lib64/librt.so.1 (0x00002b75f23ac000)
        libdl.so.2 => /lib64/libdl.so.2 (0x00002b75f25b4000)
        /lib64/ld-linux-x86-64.so.2 (0x00002b75f168d000)
```
When executing the resulting program I get the following output:
```
$ ./test.x 
implicit_task begin:    t=0 l=0
implicit_task begin:    t=0 l=1
implicit_task begin:    t=1 l=1
implicit_task end:      t=0 l=1
implicit_task end:      t=0 l=0
Segmentation fault (core dumped)
```
The segfault can be circumvented if for `ompt_scope_end` in the `implicit_task_callback` the `omp_get_level()` call is omitted.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzNV0tz4ygQ_jXyhbJLD0uWDz7MJJvdOU-q9uhCgGQ2CFSAnMz--m3Qw5KteFKZPYxLZcvQ_dEN3V83haI_Dn_xIH4Iwscg_NJ9N4JhwxBR0nDKNLInhkolhHrlskKWGQtzlAVJLx9kYf90f-OESyJaylCQPBhLudqcguSPpVlVN_fm1lYpYaYS_ttYbDlBZ8Upqn8cQdAeCRaiwOTlyOtGcMLt0WLzEsS5nzV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