This is an archive of the discontinued LLVM Phabricator instance.

[compiler-rt][builtins] Add compiler flags to catch potential errors that can lead to security vulnerabilities
ClosedPublic

Authored by ahatanak on Aug 11 2022, 1:00 PM.

Diff Detail

Event Timeline

ahatanak created this revision.Aug 11 2022, 1:00 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 11 2022, 1:00 PM
ahatanak requested review of this revision.Aug 11 2022, 1:00 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 11 2022, 1:00 PM
Herald added a subscriber: Restricted Project. · View Herald Transcript
ahatanak added inline comments.Aug 11 2022, 1:04 PM
compiler-rt/lib/builtins/CMakeLists.txt
702

I'm passing -1 here since I wasn't sure whether the definition of eprintf is needed on non-apple platforms. If it isn't needed. we can pass 0 instead.

AFAICT, on Apple's platforms, __eprintf is needed only on OSX and only if the version is older than 10.7.

https://opensource.apple.com/source/Libc/Libc-1439.40.11/include/assert.h.auto.html

ravikandhadai accepted this revision.Aug 15 2022, 3:38 PM

Thanks Akira.

This revision is now accepted and ready to land.Aug 15 2022, 3:38 PM

@dcoughlin @kubamracek @compnerd any comments before I commit this patch?

FWIW, it also broke compiler-rt standalone builds:

CMake Error at lib/builtins/CMakeLists.txt:702 (add_security_warnings):
  Unknown CMake command "add_security_warnings".

It's still failing. It looks like I have to add checks for other flags too.

haowei added a subscriber: haowei.Aug 25 2022, 1:08 PM

We are also saw failures on Fuchsia builders when using ToT clang:

[31281/267743] CXX host_x64/obj/src/lib/llvm-profdata/llvm-profdata-for-test.llvm-profdata.cc.o
FAILED: host_x64/obj/src/lib/llvm-profdata/llvm-profdata-for-test.llvm-profdata.cc.o
../../../recipe_cleanup/clang2x5u_bg_/bin/clang++ -MD -MF host_x64/obj/src/lib/llvm-profdata/llvm-profdata-for-test.llvm-profdata.cc.o.d -DHAVE_PROFDATA=1 -D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS -...
In file included from ../../src/lib/llvm-profdata/llvm-profdata.cc:224:
../../../recipe_cleanup/clang2x5u_bg_/lib/clang/16.0.0/include/profile/InstrProfData.inc:132:43: error: use of undeclared identifier 'DataSizeInitVal'
INSTR_PROF_RAW_HEADER(uint64_t, DataSize, DataSizeInitVal)
                                          ^
../../../recipe_cleanup/clang2x5u_bg_/lib/clang/16.0.0/include/profile/InstrProfData.inc:135:47: error: use of undeclared identifier 'CountersSizeInitVal'
INSTR_PROF_RAW_HEADER(uint64_t, CountersSize, CountersSizeInitVal)
                                              ^
2 errors generated.
clang++: error: failing because '-gen-reproducer' is used
Fuchsia clang version 16.0.0 (https://llvm.googlesource.com/llvm-project 0473ac8876b94fc27f145c48106675b9dedcb20d)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: ../../../recipe_cleanup/clang2x5u_bg_/bin
clang++: note: diagnostic msg:
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
clang++: note: diagnostic msg: clang-crashreports/llvm-profdata-424b79.cpp
clang++: note: diagnostic msg: clang-crashreports/llvm-profdata-424b79.sh
clang++: note: diagnostic msg:

********************

Failing task: https://ci.chromium.org/ui/p/fuchsia/builders/ci/clang_toolchain.ci.core.x64-debug-tot-build_only/b8804902579506128897/overview

Could you revert your change if it takes a while to fix forward?

After looking into the code, I think I should point out that compiler-rt/include/profile/InstrProfData.inc is a published header and its API details cannot be changed trivially without coordination with external users, so seemingly cosmetic changes should be avoided if they might require changes for users of the header, which includes changing any of the identifiers. In our case, Fuchsia uses the API in this file and due to this change, we are seeing build breakages.

At this point. I recommend to revert all related changes to this code review instead of trying to attempt to fix the problem multiple times without success.

It's still failing. It looks like I have to add checks for other flags too.

Thank you! Windows bot is fixed https://lab.llvm.org/buildbot/#/builders/127/builds/34851

ahatanak updated this revision to Diff 455796.Aug 25 2022, 10:31 PM

Revert the changes made to InstrProfData.inc and renamed the variables in InstrProfilingWriter.c instead.

wlei added a subscriber: wlei.Aug 27 2022, 12:35 PM

Hi @ahatanak

This or its child diff broke our internal build, I saw it was reverted but the reapplied diff(https://github.com/llvm/llvm-project/commit/2e9df860468425645dcd1b241c5dbf76c072e314) still broke our build.

see the log:
(1)floatundidf.S.o :

FAILED: projects/compiler-rt/lib/builtins/CMakeFiles/clang_rt.builtins-x86_64.dir/x86_64/floatundidf.S.o
/home/engshare/third-party2/gcc/11.x/centos7-native/886b5eb/bin/gcc -DVISIBILITY_HIDDEN -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -Iprojects/compiler-rt/lib/builtins -I/home/engshare/third-party2/llvm-fb/12/src/llvm-project/compiler-rt/lib/builtins -Iinclude -I/home/engshare/third-party2/llvm-fb/12/src/llvm-project/llvm/include -fdebug-prefix-map=/home/engshare/third-party2/llvm-fb/12/src/llvm-project=/home/engshare/third-party2/llvm-fb/12/src/llvm-project -fdebug-prefix-map=/home/engshare/third-party2/llvm-fb/12/src/build-platform010/build=/home/engshare/third-party2/llvm-fb/12/src/llvm-project -fPIC -O3 -DNDEBUG -m64 -fno-lto -Werror=array-bounds -Werror=uninitialized -Werror=shadow -Werror=empty-body -Werror=sizeof-pointer-memaccess -Werror=sizeof-array-argument -Werror=memset-transposed-args -Werror=format-security -std=c11 -MD -MT projects/compiler-rt/lib/builtins/CMakeFiles/clang_rt.builtins-x86_64.dir/x86_64/floatundidf.S.o -MF projects/compiler-rt/lib/builtins/CMakeFiles/clang_rt.builtins-x86_64.dir/x86_64/floatundidf.S.o.d -o projects/compiler-rt/lib/builtins/CMakeFiles/clang_rt.builtins-x86_64.dir/x86_64/floatundidf.S.o -c /home/engshare/third-party2/llvm-fb/12/src/llvm-project/compiler-rt/lib/builtins/x86_64/floatundidf.S
cc1: error: '-Wformat-security' ignored without '-Wformat' [-Werror=format-security]
cc1: some warnings being treated as errors
[794/5411] /home/engshare/third-party2/gcc/11.x/centos7-native/886b5eb/bin/gcc -DVISIBILITY_HIDDEN -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -Iprojects/compiler-rt/lib/builtins -I/home/engshare/third-party2/llvm-fb/12/src/llvm-project/compiler-rt/lib/builtins -Iinclude -I/home/engshare/third-party2/llvm-fb/12/src/llvm-project/llvm/include -fdebug-prefix-map=/home/engshare/third-party2/llvm-fb/12/src/llvm-project=/home/engshare/third-party2/llvm-fb/12/src/llvm-project -fdebug-prefix-map=/home/engshare/third-party2/llvm-fb/12/src/build-platform010/build=/home/engshare/third-party2/llvm-fb/12/src/llvm-project -fPIC -O3 -DNDEBUG -m64 -fno-lto -Werror=array-bounds -Werror=uninitialized -Werror=shadow -Werror=empty-body -Werror=sizeof-pointer-memaccess -Werror=sizeof-array-argument -Werror=memset-transposed-args -Werror=format-security -std=c11 -MD -MT projects/compiler-rt/lib/builtins/CMakeFiles/clang_rt.builtins-x86_64.dir/x86_64/floatundisf.S.o -MF projects/compiler-rt/lib/builtins/CMakeFiles/clang_rt.builtins-x86_64.dir/x86_64/floatundisf.S.o.d -o projects/compiler-rt/lib/builtins/CMakeFiles/clang_rt.builtins-x86_64.dir/x86_64/floatundisf.S.o -c /home/engshare/third-party2/llvm-fb/12/src/llvm-project/compiler-rt/lib/builtins/x86_64/floatundisf.S
FAILED: projects/compiler-rt/lib/builtins/CMakeFiles/clang_rt.builtins-x86_64.dir/x86_64/floatundisf.S.o
/home/engshare/third-party2/gcc/11.x/centos7-native/886b5eb/bin/gcc -DVISIBILITY_HIDDEN -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -Iprojects/compiler-rt/lib/builtins -I/home/engshare/third-party2/llvm-fb/12/src/llvm-project/compiler-rt/lib/builtins -Iinclude -I/home/engshare/third-party2/llvm-fb/12/src/llvm-project/llvm/include -fdebug-prefix-map=/home/engshare/third-party2/llvm-fb/12/src/llvm-project=/home/engshare/third-party2/llvm-fb/12/src/llvm-project -fdebug-prefix-map=/home/engshare/third-party2/llvm-fb/12/src/build-platform010/build=/home/engshare/third-party2/llvm-fb/12/src/llvm-project -fPIC -O3 -DNDEBUG -m64 -fno-lto -Werror=array-bounds -Werror=uninitialized -Werror=shadow -Werror=empty-body -Werror=sizeof-pointer-memaccess -Werror=sizeof-array-argument -Werror=memset-transposed-args -Werror=format-security -std=c11 -MD -MT projects/compiler-rt/lib/builtins/CMakeFiles/clang_rt.builtins-x86_64.dir/x86_64/floatundisf.S.o -MF projects/compiler-rt/lib/builtins/CMakeFiles/clang_rt.builtins-x86_64.dir/x86_64/floatundisf.S.o.d -o projects/compiler-rt/lib/builtins/CMakeFiles/clang_rt.builtins-x86_64.dir/x86_64/floatundisf.S.o -c /home/engshare/third-party2/llvm-fb/12/src/llvm-project/compiler-rt/lib/builtins/x86_64/floatundisf.S
cc1: error: '-Wformat-security' ignored without '-Wformat' [-Werror=format-security]

(2) floatundixf.S.o

FAILED: projects/compiler-rt/lib/builtins/CMakeFiles/clang_rt.builtins-x86_64.dir/x86_64/floatundixf.S.o 
/home/engshare/third-party2/gcc/11.x/centos7-native/886b5eb/bin/gcc -DVISIBILITY_HIDDEN -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -Iprojects/compiler-rt/lib/builtins -I/home/engshare/third-party2/llvm-fb/12/src/llvm-project/compiler-rt/lib/builtins -Iinclude -I/home/engshare/third-party2/llvm-fb/12/src/llvm-project/llvm/include -fdebug-prefix-map=/home/engshare/third-party2/llvm-fb/12/src/llvm-project=/home/engshare/third-party2/llvm-fb/12/src/llvm-project -fdebug-prefix-map=/home/engshare/third-party2/llvm-fb/12/src/build-platform010/build=/home/engshare/third-party2/llvm-fb/12/src/llvm-project -fPIC -O3 -DNDEBUG -m64 -fno-lto -Werror=array-bounds -Werror=uninitialized -Werror=shadow -Werror=empty-body -Werror=sizeof-pointer-memaccess -Werror=sizeof-array-argument -Werror=memset-transposed-args -Werror=format-security -std=c11 -MD -MT projects/compiler-rt/lib/builtins/CMakeFiles/clang_rt.builtins-x86_64.dir/x86_64/floatundixf.S.o -MF projects/compiler-rt/lib/builtins/CMakeFiles/clang_rt.builtins-x86_64.dir/x86_64/floatundixf.S.o.d -o projects/compiler-rt/lib/builtins/CMakeFiles/clang_rt.builtins-x86_64.dir/x86_64/floatundixf.S.o -c /home/engshare/third-party2/llvm-fb/12/src/llvm-project/compiler-rt/lib/builtins/x86_64/floatundixf.S
cc1: error: '-Wformat-security' ignored without '-Wformat' [-Werror=format-security]
cc1: some warnings being treated as errors

Please take a look and let me know if you need any thing from my side. Also please revert it if you think it will takes long time to fix it, Thank you!

Fixed in f051c1ded40970169cda84b0966331ae7ad424ed. It looks like gcc, unlike clang, doesn't allow passing -Werror=format-security without passing -format too.

wlei added a comment.Aug 29 2022, 11:56 PM

Fixed in f051c1ded40970169cda84b0966331ae7ad424ed. It looks like gcc, unlike clang, doesn't allow passing -Werror=format-security without passing -format too.

Confirmed it's fixed, thanks @ahatanak!