microsoft / mimalloc

mimalloc is a compact general purpose allocator with excellent performance.
MIT License
9.74k stars 793 forks source link

Mimalloc detects wrong libatomic for static linking #843

Open Oipo opened 5 months ago

Oipo commented 5 months ago

When running the following Dockerfile:

FROM alpine:3.19

RUN apk update
RUN apk add gcc g++ build-base cmake git wget make nano sed linux-headers perl

WORKDIR /opt
RUN git clone https://github.com/microsoft/mimalloc.git
RUN mkdir -p /opt/mimalloc/build

ENTRYPOINT ["/bin/sh", "-c"]

CMD ["cd /opt/mimalloc/build && cmake -DMI_BUILD_STATIC=1 -DMI_BUILD_SHARED=0 -DMI_BUILD_OBJECT=0 .."]

The output is

$ docker run -it ichor-temp
-- The C compiler identification is GNU 13.2.1
-- The CXX compiler identification is GNU 13.2.1
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- 
-- No build type selected, default to: Release
-- Override standard malloc (MI_OVERRIDE=ON)
-- 
-- Library base name: mimalloc
-- Version          : 2.1
-- Build type       : release
-- C Compiler       : /usr/bin/cc
-- Compiler flags   : -Wall;-Wextra;-Wno-unknown-pragmas;-fvisibility=hidden;-Wstrict-prototypes;-ftls-model=initial-exec;-fno-builtin-malloc
-- Compiler defines : 
-- Link libraries   : /usr/lib/libpthread.a;/usr/lib/librt.a;/usr/lib/libatomic.so
-- Build targets    : static;tests
-- 
-- Configuring done (0.2s)
-- Generating done (0.0s)
-- Build files have been written to: /opt/mimalloc/build

libatomic.a should be detected, as linking and executable with mimalloc-static and the -static compiler flag results in the following:

[ 84%] Linking CXX executable /opt/ichor/src/bin/AsyncManualResetEventTests
cd /opt/ichor/build/test && /usr/bin/cmake -E cmake_link_script CMakeFiles/AsyncManualResetEventTests.dir/link.txt --verbose=1
/usr/bin/c++ -O2 -std=c++20 -static -fstack-protector-strong -fcf-protection -fstack-clash-protection -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=3 -D_GLIBCXX_ASSERTIONS -D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_FAST -Wno-maybe-uninitialized  -ggdb3 -O3 -DNDEBUG -static-libgcc -static-libstdc++ -static -Wl,-z,nodlopen -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -static-libgcc -static-libstdc++ -pie -Wl,-z,nodlopen -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now CMakeFiles/AsyncManualResetEventTests.dir/AsyncManualResetEventTests.cpp.o -o /opt/ichor/src/bin/AsyncManualResetEventTests  /opt/ichor/src/bin/libichor.a /opt/ichor/src/bin/libCatch2Main.a /opt/ichor/src/bin/libmimalloc-secure.a /usr/lib/libpthread.a /usr/lib/librt.a /usr/lib/libatomic.so /usr/lib/libboost_coroutine.a /usr/lib/libboost_fiber.a /usr/lib/libboost_context.a /usr/lib/libboost_filesystem.a /usr/lib/libssl.a /usr/lib/libcrypto.a -ldl -lhiredis -ldl -lrt /opt/ichor/src/bin/libCatch2.a 
/usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: attempted static link of dynamic object `/usr/lib/libatomic.so'
collect2: error: ld returned 1 exit status
make[2]: *** [test/CMakeFiles/AsyncManualResetEventTests.dir/build.make:113: /opt/ichor/src/bin/AsyncManualResetEventTests] Error 1
make[2]: Leaving directory '/opt/ichor/build'
make[1]: *** [CMakeFiles/Makefile2:1017: test/CMakeFiles/AsyncManualResetEventTests.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....

Changing find_library(MI_LIBATOMIC atomic) to find_library(MI_LIBATOMIC NAMES libatomic.a) "fixes" it for this specific use case, but will create issues for non-static builds.