Skip to content

Commit c5898aa

Browse files
qukhantensorflower-gardener
authored andcommitted
#litert Detect address sanitizers in cc:litert_shared_library to disable RTLD_DEEPBIND.
Trying to load a library using `RTLD_DEEPBIND` is not supported by address sanitizers. In an effort to enable testing we strip the flag. If this leads to unintended behaviour, either remove the `RTLD_DEEPBIND` flag or run without an address sanitizer. See google/sanitizers#611 for more information. PiperOrigin-RevId: 734549844
1 parent e796ae9 commit c5898aa

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

tensorflow/lite/experimental/litert/cc/BUILD

+3-2
Original file line numberDiff line numberDiff line change
@@ -524,9 +524,10 @@ cc_library(
524524
srcs = ["litert_shared_library.cc"],
525525
hdrs = ["litert_shared_library.h"],
526526
deps = [
527+
":litert_expected",
528+
":litert_macros",
527529
"//tensorflow/lite/experimental/litert/c:litert_common",
528-
"//tensorflow/lite/experimental/litert/cc:litert_expected",
529-
"//tensorflow/lite/experimental/litert/cc:litert_macros",
530+
"//tensorflow/lite/experimental/litert/c:litert_logging",
530531
"@com_google_absl//absl/strings:str_format",
531532
"@com_google_absl//absl/strings:string_view",
532533
],

tensorflow/lite/experimental/litert/cc/litert_shared_library.cc

+32-1
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,38 @@
3030
#include "absl/strings/str_format.h"
3131
#include "absl/strings/string_view.h"
3232
#include "tensorflow/lite/experimental/litert/c/litert_common.h"
33+
#include "tensorflow/lite/experimental/litert/c/litert_logging.h" // IWYU pragma: keep
3334
#include "tensorflow/lite/experimental/litert/cc/litert_expected.h"
3435

36+
// When using an address sanitizer, `RTLD_DEEPBIND` is not supported. When using
37+
// one, we discard the flag and log an error.
38+
#ifdef __SANITIZE_ADDRESS__
39+
#define LITERT_ADDRESS_SANITIZER 1
40+
#elif defined(__has_feature) && __has_feature(address_sanitizer)
41+
#define LITERT_ADDRESS_SANITIZER 1
42+
#endif
43+
44+
#if LITERT_ADDRESS_SANITIZER
45+
namespace litert {
46+
namespace {
47+
RtldFlags SanitizeFlagsInCaseOfAsan(RtldFlags flags) {
48+
LITERT_LOG(
49+
LITERT_WARNING,
50+
"Trying to load a library using `RTLD_DEEPBIND` is not supported by "
51+
"address sanitizers. In an effort to enable testing we strip the flag. "
52+
"If this leads to unintended behaviour, either remove the "
53+
"`RTLD_DEEPBIND` flag or run without an address sanitizer. "
54+
"See https://github.com/google/sanitizers/issues/611 for more "
55+
"information.");
56+
flags.flags &= ~RTLD_DEEPBIND;
57+
return flags;
58+
}
59+
} // namespace
60+
} // namespace litert
61+
#else
62+
#define SanitizeFlagsInCaseOfAsan(flags) (flags)
63+
#endif
64+
3565
#if LITERT_WINDOWS_OS
3666
// Implement dummy functions from dlfnc.h on Windows.
3767
namespace {
@@ -107,7 +137,8 @@ Expected<SharedLibrary> SharedLibrary::LoadImpl(
107137
"Cannot not load shared library: empty path.");
108138
}
109139
lib.path_ = path;
110-
lib.handle_ = dlopen(lib.Path().c_str(), flags);
140+
lib.handle_ =
141+
dlopen(lib.Path().c_str(), SanitizeFlagsInCaseOfAsan(flags));
111142
if (!lib.handle_) {
112143
return Error(kLiteRtStatusErrorDynamicLoading,
113144
absl::StrFormat("Could not load shared library %s: %s.",

0 commit comments

Comments
 (0)