Catch2 icon indicating copy to clipboard operation
Catch2 copied to clipboard

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

Open initramfs opened this issue 2 years ago • 2 comments

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_minimum_required(VERSION 3.14...3.23)

project("POC" LANGUAGES CXX)

file(WRITE main.cpp "#include <catch2/catch_session.hpp>
#include <catch2/catch_test_macros.hpp>

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

initramfs avatar Jun 15 '22 13:06 initramfs

Does this also happen if you compile Catch2 with safe stack?

horenmar avatar Jun 15 '22 16:06 horenmar

Does this also happen if you compile Catch2 with safe stack?

Yes, I have tried this too to no change.

initramfs avatar Jun 15 '22 17:06 initramfs