catchorg / catchorg/Catch2

Segfault on launch when using clang + safe-stack on Alpine

Open
#2,461 2 comments 0 reactions 0 assignees View on GitHub
Possible bug
Dominant language
C++
Stars
21.5k
Forks
3.5k
Avg merge
3d 16h
Merged PRs (30d)
2

Description

When compiling a test application linked against Catch2 v3.0.1 using Clang with `-fsanitize=safe-stack` a `SIGSEGV` is encountered at program start with the following backtrace:

```
* thread #1, name = 'poc', stop reason = signal SIGSEGV: invalid address (fault address: 0xfffffffffffffff8)
* frame #0: 0x000055555566efaf poc`::__cxx_global_var_init() at main.cpp:0
frame #1: 0x000055555566f0b9 poc`_GLOBAL__sub_I_main.cpp at main.cpp:0
frame #2: 0x00007ffff7fc0d8b ld-musl-x86_64.so.1
```
This occurs irrespective of any tests preset in the test binary and only occurs if the line `Catch::Session().run(argc, argv)` is present in the test application (irrespective of if it actually runs).

Removing `-fsanitize=safe-stack` causes the program to execute correctly. Additionally, adding `-fsanitize=safe-stack` while compiling Catch2 (so that all code is compiled with the flag) does not affect the presence of the bug. The compile optimization level (e.g. debug vs release) also does not affect the behavior.

The program also executes correctly when not run on Alpine (tested with Arch Linux) under the same conditions. Perhaps musl vs glibc has an effect?

**Expected behavior**

The program should run correctly without segfaulting when compiled with `-fsanitize=safe-stack`.

**Reproduction steps**

A minimally reproducible CMake file is provided below:

```cmake
cmake_minimum_required(VERSION 3.14...3.23)

project("POC" LANGUAGES CXX)

file(WRITE main.cpp "#include
#include

int main(int argc, char** argv) {
return Catch::Session().run(argc, argv);
}

TEST_CASE(\"poc\", \"[poc]\"){
REQUIRE(true);
}
")

add_executable(poc main.cpp)

target_compile_options(poc PRIVATE -fsanitize=safe-stack)
target_link_libraries(poc PRIVATE -fsanitize=safe-stack)

include(FetchContent)

FetchContent_Declare(Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v3.0.1
GIT_SHALLOW true
)
FetchContent_MakeAvailable(Catch2)

target_link_libraries(poc PRIVATE Catch2::Catch2)
```
The file will obtain and compile Catch2 whilst linking into the main executable `poc` whilst adding `-fsanitize=safe-stack` to the main executable's compile flags. When built through CMake on Alpine, the example above should yield a `SIGSEGV` when the main executable is ran.

**Platform information:**

- OS: **Alpine Linux 3.16** or edge.
- Compiler+version: **Clang 13.0.1** or later.
- Catch version: **v3.0.1**

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.