envoyproxy / envoy

Cloud-native high-performance edge/middle/service proxy
https://www.envoyproxy.io
Apache License 2.0
24.89k stars 4.78k forks source link

Building a fuzz test with coverage fails with error `relocation R_X86_64_PC32 out of range` #11802

Closed sqkerner closed 4 years ago

sqkerner commented 4 years ago

I want to build test //test/extensions/filters/http/common/fuzz:filter_fuzz_test_with_libfuzzer with coverage, so that I can see the coverage of a fuzz corpus.

In a git client with no changes:

$ bazel clean --expunge
$ FUZZ_COVERAGE=true VALIDATE_COVERAGE=false test/run_envoy_bazel_coverage.sh \ 
  //test/extensions/filters/http/common/fuzz:filter_fuzz_test_with_libfuzzer -k

(much output omitted)

ERROR: /usr/local/home/github/envoy/test/extensions/filters/http/common/fuzz/BUILD:52:1: Couldn't build file test/extensions/filters/http/common/fuzz/filter_fuzz_test_with_libfuzzer: Linking of rule '//test/extensions/filters/http/common/fuzz:filter_fuzz_test_with_libfuzzer' failed (Exit 1)
...
ld.lld: error: /home/brian/src/final/llvm-project/compiler-rt/lib/asan/asan_interceptors.cpp:217:(.text.__interceptor_pthread_create+0x94): relocation R_X86_64_PC32 out of range: 2545006200 is not in [-2147483648, 2147483647]
...
ld.lld: error: rbac.pb.cc:(function __cxx_global_var_init: .text.startup+0x4F): relocation R_X86_64_PC32 out of range: 2192967613 is not in [-2147483648, 2147483647]
...
clang-10: error: linker command failed with exit code 1 (use -v to see invocation)
Target //test/extensions/filters/http/common/fuzz:filter_fuzz_test_with_libfuzzer failed to build

FAILED: Build did NOT complete successfully
//test/extensions/filters/http/common/fuzz:filter_fuzz_test_with_libfuzzer FAILED TO BUILD

Idea 1: compile with -fPIC

The error relocation R_X86_64_PC32 out of range is normally dealt with by compiling code in a position independent way. Adding the flag --force_pic to the bazel coverage command in the script test/run_envoy_bazel_coverage.sh causes a build failure in liblua:

==== Building LuaJIT 2.1.0-beta3 ====
make -C src
make[1]: Entering directory '/tmp/tmp.ZKAMaZ4MFf/com_github_luajit_luajit/src'
HOSTCC    host/minilua.o
/opt/llvm/bin/clang  -O2 -fomit-frame-pointer -Wall  -DLUAJIT_ENABLE_LUA52COMPAT -DLUAJIT_ENABLE_GC64  -I. -DLUAJIT_TARGET=LUAJIT_ARCH_x64 -DLJ_ARCH_HASFPU=1 -DLJ_ABI_SOFTFP=0  -c -o host/minilua.o host/minilua.c
HOSTLINK  host/minilua
/opt/llvm/bin/clang  -pie -fprofile-instr-generate -fuse-ld=/usr/bin/ld.gold -Wl,-no-as-needed -Wl,-z,relro,-z,now -B/opt/llvm/bin -lm -pthread -fuse-ld=lld -l:libc++.a -l:libc++abi.a -ldl -fsanitize=address,undefined -fno-sanitize=vptr,function -fsanitize=vptr,function -L/opt/llvm/lib/clang/10.0.0/lib/linux -l:libclang_rt.ubsan_standalone-x86_64.a -l:libclang_rt.ubsan_standalone_cxx-x86_64.a   -o host/minilua host/minilua.o -lm   
ld.lld: error: can't create dynamic relocation R_X86_64_32S against local symbol in readonly segment; recompile object files with -fPIC or pass '-Wl,-z,notext' to allow text relocations in the output
>>> defined in host/minilua.o
>>> referenced by minilua.c
>>>               host/minilua.o:(main)

ld.lld: error: can't create dynamic relocation R_X86_64_32 against local symbol in readonly segment; recompile object files with -fPIC or pass '-Wl,-z,notext' to allow text relocations in the output
>>> defined in host/minilua.o
>>> referenced by minilua.c
>>>               host/minilua.o:(main)

ld.lld: error: can't create dynamic relocation R_X86_64_32S against local symbol in readonly segment; recompile object files with -fPIC or pass '-Wl,-z,notext' to allow text relocations in the output
>>> defined in host/minilua.o
>>> referenced by minilua.c
>>>               host/minilua.o:(main)

Idea 2: Dynamically link tests

If the target is dynamically linked, then we avoid having one object whose size is >2^31 bytes.

I found Issue 1407: Dynamically link tests, but it is not clear how to implement it. I changed envoy_cc_fuzz_test() in bazel/envoy_test.bzl to set linkstatic = 0, in a few places, and I can make tests build this way.

Perhaps we can enable dynamic linking for fuzz targets that need it. This would be a way to make incremental progress on Issue 1407.

asraa commented 4 years ago

I found Issue 1407: Dynamically link tests, but it is not clear how to implement it.

Can we use linkstatic = envoy_linkstatic() for the targets in envoy_test.bzl?

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed in the next 7 days unless it is tagged "help wanted" or other activity occurs. Thank you for your contributions.

sqkerner commented 4 years ago

@asraa solved this issue with https://github.com/envoyproxy/envoy/pull/11045 .