[compiler-rt] [sanitizer] Fix race condition in GetNamedMappingFd with decorate_pro… (PR #190981)

via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 10 07:07:55 PDT 2026


================
@@ -357,9 +357,9 @@ int GetNamedMappingFd(const char *name, uptr size, int *flags) {
   if (!common_flags()->decorate_proc_maps || !name)
     return -1;
   char shmname[200];
-  CHECK(internal_strlen(name) < sizeof(shmname) - 10);
-  internal_snprintf(shmname, sizeof(shmname), "/dev/shm/%zu [%s]",
-                    internal_getpid(), name);
+  CHECK(internal_strlen(name) < sizeof(shmname) - 30);
+  internal_snprintf(shmname, sizeof(shmname), "/dev/shm/%zu.%zu [%s]",
----------------
martin0413133 wrote:

Reproduction steps: also can be see in https://github.com/llvm/llvm-project/issues/190604

1. Compile the test case:
$ cat > /tmp/test.c << 'EOF'
#include <pthread.h>
#include <unistd.h>
void* f(void* p) { sleep(5); return NULL; }
int main(int argc, char** argv) {
  int n = 6;
  pthread_t t[n];
  for (int i = 0; i < n; i++) pthread_create(&t[i], NULL, f, NULL);
  for (int i = 0; i < n; i++) pthread_join(t[i], NULL);
  return 0;
}
EOF
$ clang -fsanitize=address /tmp/test.c -o /tmp/test -lpthread

2. Run with ASAN:
$ for i in {1..5}; do ASAN_OPTIONS=decorate_proc_maps=1 /tmp/test 2>&1 | head -5; done

Without this patch, 6+ threads easily hit CHECK failures due to race conditions on the same filename.
With this patch (PID.TID in filename), each thread has a unique file, so races are eliminated.

Repeated calls from the same test/thread are safe because the same thread always uses the same filename, and accesses are sequential.



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


More information about the llvm-commits mailing list