[llvm] [compiler-rt] [sanitizer_common] Add experimental flag to tweak dlopen(<main program>) (PR #71715)

Thurston Dang via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 9 10:17:09 PST 2023


================
@@ -6304,10 +6304,38 @@ INTERCEPTOR(int, fclose, __sanitizer_FILE *fp) {
 #endif
 
 #if SANITIZER_INTERCEPT_DLOPEN_DLCLOSE
+// Returns 1 if 'suffix' matches the end of 'str', 0 otherwise
+static int internal_is_suffix(const char *suffix, const char *str) {
+  if (!suffix || !str)
+    return 0;
+
+  if (internal_strlen(suffix) > internal_strlen(str))
+    return 0;
+
+  return internal_strcmp(str + internal_strlen(str) - internal_strlen(suffix),
+                         suffix) == 0;
+}
+
+#  if SANITIZER_GLIBC
+extern char *__progname;
+#  endif
+
 INTERCEPTOR(void*, dlopen, const char *filename, int flag) {
   void *ctx;
   COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlopen, filename, flag);
-  if (filename) COMMON_INTERCEPTOR_READ_STRING(ctx, filename, 0);
+
+  if (filename) {
+    COMMON_INTERCEPTOR_READ_STRING(ctx, filename, 0);
+#  if SANITIZER_GLIBC
+    if (common_flags()->test_only_replace_dlopen_main_program &&
+        internal_is_suffix(__progname, filename)) {
----------------
thurstond wrote:

Per off-line discussion, changed to use dladdr of a symbol that appears in the main binary. (Since 'ISO C++ does not allow 'main' to be used by a program [-Werror,-Wmain]', I ended up using 'dladdr((void *)&SanitizerToolName, &info);').

https://github.com/llvm/llvm-project/pull/71715


More information about the llvm-commits mailing list