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

Vitaly Buka via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 9 10:26:46 PST 2023


================
@@ -6304,10 +6305,69 @@ INTERCEPTOR(int, fclose, __sanitizer_FILE *fp) {
 #endif
 
 #if SANITIZER_INTERCEPT_DLOPEN_DLCLOSE
+// Ordinary internal_readlink does not null-terminate strings.
+static int internal_readlink_with_null(const char *path, char *buf,
+                                       uptr bufsize) {
+  if (!buf) {
+    return -1;
+  }
+
+  // Ensure it is a valid string even if internal_readlink fails
+  buf[0] = '\0';
+
+  int len = internal_readlink(path, buf, bufsize);
+  if (len >= 0 && len < (int)bufsize)
+    buf[len] = '\0';
+
+  buf[bufsize - 1] = '\0';
+
+  return len;
+}
+
 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_DYNAMIC
+    VPrintf(1, "dlopen interceptor: filename: %s\n", filename);
+
+    char *filename_canonical = (char *)InternalAlloc(sizeof(char) * 640);
----------------
vitalybuka wrote:

dlopen is fragile, so would ask you to remove heap allocations and logging.
I afraid we can get deadlock or similar

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


More information about the llvm-commits mailing list