[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:52:55 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);
+ if (filename_canonical)
+ internal_readlink_with_null(filename, filename_canonical, 640);
+ VPrintf(1, "dlopen interceptor: filename (canonical): %s\n",
+ filename_canonical);
+
+ // We care about dlopen(<main program>,...), which will only happen
+ // for statically linked ASan.
+ const char *self_fname;
+ int ret = dladdr_self_fname(&self_fname);
----------------
thurstond wrote:
Ended up doing strcmp in case they do an intermediate strcpy
https://github.com/llvm/llvm-project/pull/71715
More information about the llvm-commits
mailing list