Avoid interceptor recursion when loaded in a separate DSO with sanitizers. PiperOrigin-RevId: 1026697395
diff --git a/framework_shared/internal/BUILD b/framework_shared/internal/BUILD index 79e45e9..256a5d7 100644 --- a/framework_shared/internal/BUILD +++ b/framework_shared/internal/BUILD
@@ -16,6 +16,7 @@ # @com_google_fuzztest//framework_shared. load("@rules_cc//cc:cc_library.bzl", "cc_library") +load("@rules_cc//cc:cc_shared_library.bzl", "cc_shared_library") load("@rules_cc//cc:cc_test.bzl", "cc_test") DEFAULT_VISIBILITY = [ @@ -147,7 +148,13 @@ name = "interceptor_utils", hdrs = ["interceptor_utils.h"], copts = DISABLE_SANCOV_COPTS, - linkopts = ["-ldl"], # for dlsym + linkopts = ["-ldl"] + select({ + "@platforms//os:macos": [ + # For compatible behavior on weak __interceptor_* symbols. + "-Wl,-undefined,dynamic_lookup", + ], + "//conditions:default": [], + }), # -ldl for dladdr, dlsym # TODO(b/436308818): Drop centipede visibility once centipede_runner depends on :sancov_runtime. visibility = DEFAULT_VISIBILITY + [ "@com_google_fuzztest//centipede:__pkg__", @@ -155,6 +162,67 @@ ) cc_library( + name = "interceptor_utils_test_target_dso", + testonly = True, + srcs = ["interceptor_utils_test_target_dso.cc"], + hdrs = ["interceptor_utils_test_target_dso.h"], + visibility = ["//visibility:private"], +) + +cc_shared_library( + name = "interceptor_utils_test_target_dso_shared", + testonly = True, + visibility = ["//visibility:private"], + deps = [":interceptor_utils_test_target_dso"], +) + +cc_library( + name = "interceptor_utils_test_dso", + testonly = True, + srcs = ["interceptor_utils_test_dso.cc"], + hdrs = ["interceptor_utils_test_dso.h"], + visibility = ["//visibility:private"], + deps = [ + ":interceptor_utils", + ":interceptor_utils_test_target_dso", + ], +) + +cc_shared_library( + name = "interceptor_utils_test_dso_shared", + testonly = True, + dynamic_deps = [":interceptor_utils_test_target_dso_shared"], + user_link_flags = select({ + "@platforms//os:macos": [ + # For compatible behavior on weak symbols. + "-Wl,-undefined,dynamic_lookup", + ], + "//conditions:default": [], + }), + visibility = ["//visibility:private"], + deps = [ + ":interceptor_utils", + ":interceptor_utils_test_dso", + ], +) + +cc_test( + name = "interceptor_utils_test", + srcs = ["interceptor_utils_test.cc"], + dynamic_deps = [ + ":interceptor_utils_test_dso_shared", + ":interceptor_utils_test_target_dso_shared", + ], + linkopts = ["-ldl"], # for dlsym + deps = [ + ":interceptor_utils", + ":interceptor_utils_test_dso", + ":interceptor_utils_test_target_dso", + "@googletest//:gtest_main", + ], +) + +cc_library( name = "reverse_pc_table", hdrs = ["reverse_pc_table.h"], visibility = ["//visibility:private"],
diff --git a/framework_shared/internal/interceptor_utils.h b/framework_shared/internal/interceptor_utils.h index aa664bc..15c5967 100644 --- a/framework_shared/internal/interceptor_utils.h +++ b/framework_shared/internal/interceptor_utils.h
@@ -15,29 +15,43 @@ #ifndef FUZZTEST_FRAMEWORK_SHARED_INTERNAL_INTERCEPTOR_UTILS_H_ #define FUZZTEST_FRAMEWORK_SHARED_INTERNAL_INTERCEPTOR_UTILS_H_ -#include <dlfcn.h> // for dlsym() +#include <dlfcn.h> // for dladdr(), dlsym() namespace fuzztest::internal { // Wrapper for dlsym(). // Returns the pointer to the real function `function_name`. -// In most cases we need FuncAddr("foo") to be called before the first call to -// foo(), which means we either need to do this very early at startup -// (e.g. pre-init array), or on the first call. -// Currently, we do this on the first call via function-scope static. +// Marked always_inline so RTLD_NEXT resolves relative to the calling shared +// object rather than an interposed out-of-line instantiation. template <typename FunctionT> -FunctionT FuncAddr(const char* function_name) { +__attribute__((always_inline)) inline FunctionT FuncAddr( + const char* function_name) { void* addr = dlsym(RTLD_NEXT, function_name); return reinterpret_cast<FunctionT>(addr); } +// Returns true if `addr1` and `addr2` belong to the same loaded binary or +// shared object. +inline bool InSameSharedObject(const void* addr1, const void* addr2) { + Dl_info info1 = {}; + Dl_info info2 = {}; + return dladdr(addr1, &info1) != 0 && dladdr(addr2, &info2) != 0 && + info1.dli_fbase == info2.dli_fbase; +} + } // namespace fuzztest::internal // A sanitizer-compatible way to intercept functions that are potentially // intercepted by sanitizers, in which case the symbol __interceptor_X would be -// defined for intercepted function X. So we always forward an intercepted call -// to the sanitizer interceptor if it exists, and fall back to the next -// definition following dlsym. +// defined for intercepted function X. +// +// When our interceptor is linked into the same binary/DSO as the sanitizer +// runtime, our strong definition of X overrides the sanitizer's weak alias, so +// RTLD_NEXT would skip the sanitizer; we therefore forward directly to +// __interceptor_X. When our interceptor lives in a separate DSO (e.g. via +// LD_PRELOAD), the dynamic linker's RTLD_NEXT chain already orders the two +// interceptors without looping, whereas jumping back to __interceptor_X in an +// earlier DSO would cause infinite recursion. // // We define the fuzztest_X_orig pointers that are statically initialized to // FuzzTestGetOrig_X() with the aforementioned logic to fill the pointers early, @@ -56,14 +70,21 @@ FUZZTEST_SANITIZER_INTERCEPTOR_NAME(orig_func_name)) args; \ static decltype(&FUZZTEST_SANITIZER_INTERCEPTOR_NAME(orig_func_name)) \ FuzzTestGetOrig_##orig_func_name() { \ - if (auto p = &FUZZTEST_SANITIZER_INTERCEPTOR_NAME(orig_func_name)) \ - return p; \ + if (auto sanitizer_interceptor = \ + &FUZZTEST_SANITIZER_INTERCEPTOR_NAME(orig_func_name); \ + sanitizer_interceptor != nullptr && \ + ::fuzztest::internal::InSameSharedObject( \ + reinterpret_cast<const void*>(sanitizer_interceptor), \ + reinterpret_cast<const void*>( \ + &FuzzTestGetOrig_##orig_func_name))) { \ + return sanitizer_interceptor; \ + } \ return ::fuzztest::internal::FuncAddr< \ decltype(&FUZZTEST_SANITIZER_INTERCEPTOR_NAME(orig_func_name))>( \ #orig_func_name); \ } \ static ret_type(*fuzztest_##orig_func_name##_orig) args; \ - __attribute__((constructor)) void \ + static __attribute__((constructor)) void \ FuzzTestInitializeOrig_##orig_func_name() { \ fuzztest_##orig_func_name##_orig = FuzzTestGetOrig_##orig_func_name(); \ }
diff --git a/framework_shared/internal/interceptor_utils_test.cc b/framework_shared/internal/interceptor_utils_test.cc new file mode 100644 index 0000000..7f429ac --- /dev/null +++ b/framework_shared/internal/interceptor_utils_test.cc
@@ -0,0 +1,98 @@ +// Copyright 2026 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "./framework_shared/internal/interceptor_utils.h" + +#include <dlfcn.h> + +#include "gtest/gtest.h" +#include "./framework_shared/internal/interceptor_utils_test_dso.h" + +// Simulates a sanitizer interceptor linked into the same binary as +// FUZZTEST_DECLARE_ORIG_FUNC. Declare via the macro first so its weak +// attribute precedes the definition. +FUZZTEST_DECLARE_ORIG_FUNC(int, fuzztest_test_same_dso_fn, (int)); + +extern "C" __attribute__((visibility("default"))) int +__interceptor_fuzztest_test_same_dso_fn(int x) { + return x + 100; +} + +// Simulates a function with no sanitizer interceptor defined anywhere. +FUZZTEST_DECLARE_ORIG_FUNC(int, fuzztest_test_no_sanitizer_fn, (int)); + +// Simulates a sanitizer runtime in the main executable whose REAL lookup +// resolves via RTLD_NEXT to the interceptor in a separately loaded DSO. +// If the DSO mistakenly chains back to __interceptor_fuzztest_test_cross_dso_fn +// in the main executable, this would infinitely recurse (caught here via +// `recursion_depth`). +extern "C" __attribute__((visibility("default"))) int +__interceptor_fuzztest_test_cross_dso_fn(int x) { + static int recursion_depth = 0; + if (++recursion_depth > 1) { + --recursion_depth; + return -999; + } + using FnType = int (*)(int); + auto next = + reinterpret_cast<FnType>(dlsym(RTLD_NEXT, "fuzztest_test_cross_dso_fn")); + const int result = next != nullptr ? next(x) + 100 : -1; + --recursion_depth; + return result; +} + +extern "C" __attribute__((visibility("default"))) int +fuzztest_test_cross_dso_fn(int x) { + return __interceptor_fuzztest_test_cross_dso_fn(x); +} + +namespace fuzztest::internal { +namespace { + +void SameObjectMarkerA() {} +void SameObjectMarkerB() {} + +TEST(InterceptorUtilsTest, InSameSharedObjectDistinguishesMainAndDso) { + EXPECT_TRUE( + InSameSharedObject(reinterpret_cast<const void*>(&SameObjectMarkerA), + reinterpret_cast<const void*>(&SameObjectMarkerB))); + EXPECT_FALSE(InSameSharedObject( + reinterpret_cast<const void*>(&SameObjectMarkerA), GetDsoCodeAddress())); +} + +TEST(InterceptorUtilsTest, SameSharedObjectUsesSanitizerInterceptor) { + EXPECT_EQ(FUZZTEST_REAL(fuzztest_test_same_dso_fn)(5), 105); +} + +TEST(InterceptorUtilsTest, NoSanitizerInterceptorFallsBackToRtldNext) { + EXPECT_EQ(FUZZTEST_REAL(fuzztest_test_no_sanitizer_fn)(5), 12); +} + +TEST(InterceptorUtilsTest, SeparateDsoUsesRtldNextInsteadOfRecursingIntoMain) { + // Confirm the DSO genuinely sees the main binary's __interceptor_* symbol + // (the precondition that previously caused mutual recursion). + EXPECT_EQ( + GetCrossDsoSanitizerInterceptorAddrFromDso(), + reinterpret_cast<const void*>(&__interceptor_fuzztest_test_cross_dso_fn)); + + // Inside the DSO, FUZZTEST_REAL must skip main's __interceptor_* and call + // the next library in RTLD_NEXT order (+1), not recurse into main. + EXPECT_EQ(CallCrossDsoOrigFromDso(5), 6); + + // Full chain: main sanitizer (+100) -> DSO interceptor (+10) -> target (+1). + EXPECT_EQ(fuzztest_test_cross_dso_fn(5), 116); +} + +} // namespace +} // namespace fuzztest::internal
diff --git a/framework_shared/internal/interceptor_utils_test_dso.cc b/framework_shared/internal/interceptor_utils_test_dso.cc new file mode 100644 index 0000000..903359e --- /dev/null +++ b/framework_shared/internal/interceptor_utils_test_dso.cc
@@ -0,0 +1,44 @@ +// Copyright 2026 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "./framework_shared/internal/interceptor_utils_test_dso.h" + +#include "./framework_shared/internal/interceptor_utils.h" +#include "./framework_shared/internal/interceptor_utils_test_target_dso.h" + +FUZZTEST_DECLARE_ORIG_FUNC(int, fuzztest_test_cross_dso_fn, (int)); + +extern "C" __attribute__((visibility("default"))) int +fuzztest_test_cross_dso_fn(int x) { + return FUZZTEST_REAL(fuzztest_test_cross_dso_fn)(x) + 10; +} + +namespace fuzztest::internal { + +int CallCrossDsoOrigFromDso(int x) { + // Reference the target DSO so --as-needed keeps its DT_NEEDED edge. + if (InterceptorUtilsTestTargetDsoMarker() == 0) return -1; + return FUZZTEST_REAL(fuzztest_test_cross_dso_fn)(x); +} + +const void* GetCrossDsoSanitizerInterceptorAddrFromDso() { + return reinterpret_cast<const void*>( + &FUZZTEST_SANITIZER_INTERCEPTOR_NAME(fuzztest_test_cross_dso_fn)); +} + +const void* GetDsoCodeAddress() { + return reinterpret_cast<const void*>(&CallCrossDsoOrigFromDso); +} + +} // namespace fuzztest::internal
diff --git a/framework_shared/internal/interceptor_utils_test_dso.h b/framework_shared/internal/interceptor_utils_test_dso.h new file mode 100644 index 0000000..6054ba7 --- /dev/null +++ b/framework_shared/internal/interceptor_utils_test_dso.h
@@ -0,0 +1,32 @@ +// Copyright 2026 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef FUZZTEST_FRAMEWORK_SHARED_INTERNAL_INTERCEPTOR_UTILS_TEST_DSO_H_ +#define FUZZTEST_FRAMEWORK_SHARED_INTERNAL_INTERCEPTOR_UTILS_TEST_DSO_H_ + +namespace fuzztest::internal { + +// Invokes FUZZTEST_REAL(fuzztest_test_cross_dso_fn) from inside the test DSO. +int CallCrossDsoOrigFromDso(int x); + +// Returns the address of __interceptor_fuzztest_test_cross_dso_fn as resolved +// from inside the test DSO. +const void* GetCrossDsoSanitizerInterceptorAddrFromDso(); + +// Returns the address of a function defined inside the test DSO. +const void* GetDsoCodeAddress(); + +} // namespace fuzztest::internal + +#endif // FUZZTEST_FRAMEWORK_SHARED_INTERNAL_INTERCEPTOR_UTILS_TEST_DSO_H_
diff --git a/framework_shared/internal/interceptor_utils_test_target_dso.cc b/framework_shared/internal/interceptor_utils_test_target_dso.cc new file mode 100644 index 0000000..22dcf59 --- /dev/null +++ b/framework_shared/internal/interceptor_utils_test_target_dso.cc
@@ -0,0 +1,36 @@ +// Copyright 2026 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "./framework_shared/internal/interceptor_utils_test_target_dso.h" + +extern "C" { + +// Simulates a real underlying libc function loaded after the interceptor DSO. +__attribute__((visibility("default"))) int fuzztest_test_cross_dso_fn(int x) { + return x + 1; +} + +// Simulates a real underlying function with no sanitizer interceptor defined. +__attribute__((visibility("default"))) int fuzztest_test_no_sanitizer_fn( + int x) { + return x + 7; +} + +} // extern "C" + +namespace fuzztest::internal { + +int InterceptorUtilsTestTargetDsoMarker() { return 1; } + +} // namespace fuzztest::internal
diff --git a/framework_shared/internal/interceptor_utils_test_target_dso.h b/framework_shared/internal/interceptor_utils_test_target_dso.h new file mode 100644 index 0000000..72d1858 --- /dev/null +++ b/framework_shared/internal/interceptor_utils_test_target_dso.h
@@ -0,0 +1,26 @@ +// Copyright 2026 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef FUZZTEST_FRAMEWORK_SHARED_INTERNAL_INTERCEPTOR_UTILS_TEST_TARGET_DSO_H_ +#define FUZZTEST_FRAMEWORK_SHARED_INTERNAL_INTERCEPTOR_UTILS_TEST_TARGET_DSO_H_ + +namespace fuzztest::internal { + +// Anchor symbol used to ensure the interceptor test DSO retains a DT_NEEDED +// edge to this target DSO under --as-needed. +int InterceptorUtilsTestTargetDsoMarker(); + +} // namespace fuzztest::internal + +#endif // FUZZTEST_FRAMEWORK_SHARED_INTERNAL_INTERCEPTOR_UTILS_TEST_TARGET_DSO_H_