ziglang/zig

zig cc -c -fstack-protector stopped working

markus-oberhumer opened this issue · 7 comments

Zig Version

0.12.0-dev.1607+f8b38a174

Steps to Reproduce and Observed Behavior

At https://github.com/upx/upx-test-build-with-zig we are continuously testing building UPX for 104 different build targets, and it seems that recently -fstack-protector as used in zig cc -c -fstack-protector stopped working

Possible cause: commit 53f74d6 by @andrewrk

# create a C test file that uses some stack
echo 'void foo(void *); void bar(void) { char stack[256 * 1024]; foo(stack); }' > x.c

# works
./zig-linux-x86_64-0.12.0-dev.1504+81219586b/zig cc -target x86_64-linux-musl -fstack-protector -c x.c

# FAILS
./zig-linux-x86_64-0.12.0-dev.1607+f8b38a174/zig cc -target x86_64-linux-musl -fstack-protector -c x.c
error: unable to create compilation: StackProtectorUnsupportedByTarget

Expected Behavior

No error.

This breaks also native/non-crosscompiled code relying on stack-protectors:

git clone https://github.com/rui314/mold.git && cd mold
mkdir -p build && cd build
cd "$HOME/dev/git/cpp/mold/build"
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER="zig;c++" ..
cmake --build . -j $(nproc)
[  2%] Building CXX object third-party/tbb/src/tbb/CMakeFiles/tbb.dir/allocator.cpp.o
error: unable to create compilation: StackProtectorUnsupportedByTarget
make[2]: *** [third-party/tbb/src/tbb/CMakeFiles/tbb.dir/build.make:76: third-party/tbb/src/tbb/CMakeFiles/tbb.dir/address_waiter.cpp.o] Fehler 1
make[2]: *** Es wird auf noch nicht beendete Prozesse gewartet …
error: unable to create compilation: StackProtectorUnsupportedByTarget
make[2]: *** [third-party/tbb/src/tbb/CMakeFiles/tbb.dir/build.make:90: third-party/tbb/src/tbb/CMakeFiles/tbb.dir/allocator.cpp.o] Fehler 1
[  2%] Building C object third-party/mimalloc/CMakeFiles/mimalloc-static.dir/src/bitmap.c.o
make[1]: *** [CMakeFiles/Makefile2:1193: third-party/tbb/src/tbb/CMakeFiles/tbb.dir/all] Fehler 2
make[1]: *** Es wird auf noch nicht beendete Prozesse gewartet …
Vexu commented

Regressed by 53f74d6, use_llvm is false when there is no Zig module so zigBackend selects stage2_x86_64 which doesn't support stack protectors.

Is there is a temporary solution to this? Install a version of zig prior to 53f74d6?

Vexu commented

Adding -fllvm should work. Nevermind, it's not available for zig cc.

depending on the intended meaning of use_llvm, the fix may be along the lines of:
#18114 (comment)

Any news on this issue? It looks like zig cc could need some love...

Vexu commented

depending on the intended meaning of use_llvm, the fix may be along the lines of: #18114 (comment)

This patch looks good except the frontend check needs to go before the main_mod == null check.