|
30 | 30 | #include "absl/strings/str_format.h"
|
31 | 31 | #include "absl/strings/string_view.h"
|
32 | 32 | #include "tensorflow/lite/experimental/litert/c/litert_common.h"
|
| 33 | +#include "tensorflow/lite/experimental/litert/c/litert_logging.h" // IWYU pragma: keep |
33 | 34 | #include "tensorflow/lite/experimental/litert/cc/litert_expected.h"
|
34 | 35 |
|
| 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 | + |
35 | 65 | #if LITERT_WINDOWS_OS
|
36 | 66 | // Implement dummy functions from dlfnc.h on Windows.
|
37 | 67 | namespace {
|
@@ -107,7 +137,8 @@ Expected<SharedLibrary> SharedLibrary::LoadImpl(
|
107 | 137 | "Cannot not load shared library: empty path.");
|
108 | 138 | }
|
109 | 139 | lib.path_ = path;
|
110 |
| - lib.handle_ = dlopen(lib.Path().c_str(), flags); |
| 140 | + lib.handle_ = |
| 141 | + dlopen(lib.Path().c_str(), SanitizeFlagsInCaseOfAsan(flags)); |
111 | 142 | if (!lib.handle_) {
|
112 | 143 | return Error(kLiteRtStatusErrorDynamicLoading,
|
113 | 144 | absl::StrFormat("Could not load shared library %s: %s.",
|
|
0 commit comments