Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OMPT] Segment fault caused by calling NULL tsan function pointers #93524

Closed
FLZ101 opened this issue May 28, 2024 · 3 comments · Fixed by #93568
Closed

[OMPT] Segment fault caused by calling NULL tsan function pointers #93524

FLZ101 opened this issue May 28, 2024 · 3 comments · Fixed by #93568
Labels

Comments

@FLZ101
Copy link
Contributor

FLZ101 commented May 28, 2024

The LLVM version tested:

clang --version
clang version 19.0.0git (https://github.com/FLZ101/llvm-project.git 179fb2e51e08d7eec1844dd07c01d396728a1d0f)
Target: aarch64-unknown-linux-gnu
Thread model: posix

How I built LLVM:

cmake -G Ninja \
	  -DCMAKE_BUILD_TYPE=Release \
	  -DLLVM_ENABLE_ASSERTIONS=ON \
	  -DLLVM_TARGETS_TO_BUILD="AArch64" \
	  -DLLVM_ENABLE_PROJECTS="clang;lld;openmp" \
	  -DLLVM_ENABLE_RUNTIMES="compiler-rt;libc;libcxx;libcxxabi;libunwind" \
	  -DLLVM_BINUTILS_INCDIR=/usr/include \
	  -B build -S llvm

A test case

greet.c:

#include <Python.h>
#include <omp.h>

static omp_lock_t lock_timer;

static PyObject * greet_name(PyObject *self, PyObject *args) {
    const char *name;
    if (!PyArg_ParseTuple(args, "s", &name))
        return NULL;
    printf("Hello %s!\n", name);

    omp_init_lock(&lock_timer);
    omp_set_lock(&lock_timer);
    omp_unset_lock(&lock_timer);

    Py_RETURN_NONE;
}

static PyMethodDef GreetMethods[] = {
    {"greet", greet_name, METH_VARARGS, "Greet an entity."},
    {NULL, NULL, 0, NULL}
};

static struct PyModuleDef greet = { PyModuleDef_HEAD_INIT, "greet", "", -1, GreetMethods };

PyMODINIT_FUNC PyInit_greet(void) { return PyModule_Create(&greet); }

main.py:

if __name__ == '__main__':
  import greet
  greet.greet('World')

go.sh:

#!/bin/bash

set -e

llvm_dir=/home/z30026696/opt/CompilerKernel-2/BiSheng/install-vscode
export PATH=$llvm_dir/bin:$PATH

export LD_LIBRARY_PATH=$llvm_dir/lib:$llvm_dir/lib/aarch64-unknown-linux-gnu

clang -g -fPIC --shared $(python3-config --includes) greet.c -o greet.so -fopenmp

export OMP_TOOL_VERBOSE_INIT=STDOUT
# gdb --args
python main.py

result:

$ bash go.sh
Hello World!
----- START LOGGING OF TOOL REGISTRATION -----
Search for OMP tool in current address space... Failed.
No OMP_TOOL_LIBRARIES defined.
...searching tool libraries failed. Using archer tool.
Opening libarcher.so... Success.
Searching for ompt_start_tool in libarcher.so... Success.
Tool was started and is using the OMPT interface.
----- END LOGGING OF TOOL REGISTRATION -----
Unable to find TSan function AnnotateHappensAfter.
Unable to find TSan function AnnotateHappensBefore.
Unable to find TSan function __tsan_func_entry.
Unable to find TSan function __tsan_func_exit.
Warning: please export TSAN_OPTIONS='ignore_noninstrumented_modules=1' to avoid false positive reports from the OpenMP runtime!
go.sh: line 20: 56331 Segmentation fault      (core dumped) python main.py
@FLZ101
Copy link
Contributor Author

FLZ101 commented May 28, 2024

The /usr/lib64/libpython3.so in the Linux machine I used has a RunningOnValgrind symbol in it which makes ompt_start_tool() in openmp/tools/archer/ompt-tsan.cpp "think" those tsan functions (e.g. AnnotateHappensAfter) exit.

Since those tsan functions are missing, pointers to them will not be set and remain NULL. Later calling those NULL pointers will cause a segment fault.

$ objdump -T /usr/lib64/libpython3.7m.so.1.0 | grep RunningOnValgrind
000000000015b2f0 g    DF .text  000000000000006c  Base        RunningOnValgrind

@FLZ101
Copy link
Contributor Author

FLZ101 commented May 28, 2024

I'll create a fix PR soon.

@llvmbot
Copy link
Member

llvmbot commented May 28, 2024

@llvm/issue-subscribers-openmp

Author: Franklin Zhang (FLZ101)

The LLVM version tested:
clang --version
clang version 19.0.0git (https://github.com/FLZ101/llvm-project.git 179fb2e51e08d7eec1844dd07c01d396728a1d0f)
Target: aarch64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/z30026696/opt/llvm/src/llvm-project/build-x/bin
Build config: +assertions

How I built LLVM:

cmake -G Ninja \
	  -DCMAKE_BUILD_TYPE=Release \
	  -DLLVM_ENABLE_ASSERTIONS=ON \
	  -DLLVM_TARGETS_TO_BUILD="AArch64" \
	  -DLLVM_ENABLE_PROJECTS="clang;lld;openmp" \
	  -DLLVM_ENABLE_RUNTIMES="compiler-rt;libc;libcxx;libcxxabi;libunwind" \
	  -DLLVM_BINUTILS_INCDIR=/usr/include \
	  -B build -S llvm

A test case

greet.c:

#include &lt;Python.h&gt;
#include &lt;omp.h&gt;

static omp_lock_t lock_timer;

static PyObject * greet_name(PyObject *self, PyObject *args) {
    const char *name;
    if (!PyArg_ParseTuple(args, "s", &amp;name))
        return NULL;
    printf("Hello %s!\n", name);

    omp_init_lock(&amp;lock_timer);
    omp_set_lock(&amp;lock_timer);
    omp_unset_lock(&amp;lock_timer);

    Py_RETURN_NONE;
}

static PyMethodDef GreetMethods[] = {
    {"greet", greet_name, METH_VARARGS, "Greet an entity."},
    {NULL, NULL, 0, NULL}
};

static struct PyModuleDef greet = { PyModuleDef_HEAD_INIT, "greet", "", -1, GreetMethods };

PyMODINIT_FUNC PyInit_greet(void) { return PyModule_Create(&amp;greet); }

main.py:

if __name__ == '__main__':
  import greet
  greet.greet('World')

go.sh:

#!/bin/bash

set -e

llvm_dir=/home/z30026696/opt/CompilerKernel-2/BiSheng/install-vscode
export PATH=$llvm_dir/bin:$PATH

export LD_LIBRARY_PATH=$llvm_dir/lib:$llvm_dir/lib/aarch64-unknown-linux-gnu

clang -g -fPIC --shared $(python3-config --includes) greet.c -o greet.so -fopenmp

export OMP_TOOL_VERBOSE_INIT=STDOUT
# gdb --args
python main.py

result:

$ bash go.sh
Hello World!
----- START LOGGING OF TOOL REGISTRATION -----
Search for OMP tool in current address space... Failed.
No OMP_TOOL_LIBRARIES defined.
...searching tool libraries failed. Using archer tool.
Opening libarcher.so... Success.
Searching for ompt_start_tool in libarcher.so... Success.
Tool was started and is using the OMPT interface.
----- END LOGGING OF TOOL REGISTRATION -----
Unable to find TSan function AnnotateHappensAfter.
Unable to find TSan function AnnotateHappensBefore.
Unable to find TSan function __tsan_func_entry.
Unable to find TSan function __tsan_func_exit.
Warning: please export TSAN_OPTIONS='ignore_noninstrumented_modules=1' to avoid false positive reports from the OpenMP runtime!
go.sh: line 20: 56331 Segmentation fault      (core dumped) python main.py

jprotze added a commit that referenced this issue May 28, 2024
Avoid calling NULL function pointers in cases where ompt_start_tool
succeeds but those tsan functions
do not really exist.

Fix #93524

---------

Co-authored-by: Joachim <protze@rz.rwth-aachen.de>
@EugeneZelenko EugeneZelenko added the compiler-rt:lsan Leak sanitizer label May 28, 2024
vg0204 pushed a commit to vg0204/llvm-project that referenced this issue May 29, 2024
Avoid calling NULL function pointers in cases where ompt_start_tool
succeeds but those tsan functions
do not really exist.

Fix llvm#93524

---------

Co-authored-by: Joachim <protze@rz.rwth-aachen.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants