From 667b9add73e0e38b4a0d688f0087069a479032be Mon Sep 17 00:00:00 2001 From: Thurston Dang Date: Fri, 7 Jul 2023 00:21:28 +0000 Subject: [PATCH] Revert "Reland '[msan] Intercept dladdr1, and refactor dladdr'" This reverts my commit 015dabd7672f936cdb5bdcad20fe80b17f05c9ca due to breaking non-glibc builds. --- compiler-rt/lib/msan/CMakeLists.txt | 2 - compiler-rt/lib/msan/msan_dl.cpp | 63 -------------------- compiler-rt/lib/msan/msan_dl.h | 28 --------- compiler-rt/lib/msan/msan_interceptors.cpp | 28 ++++----- compiler-rt/test/msan/dladdr1_test.c | 95 ------------------------------ 5 files changed, 13 insertions(+), 203 deletions(-) delete mode 100644 compiler-rt/lib/msan/msan_dl.cpp delete mode 100644 compiler-rt/lib/msan/msan_dl.h delete mode 100644 compiler-rt/test/msan/dladdr1_test.c diff --git a/compiler-rt/lib/msan/CMakeLists.txt b/compiler-rt/lib/msan/CMakeLists.txt index 08b765d..b115f68 100644 --- a/compiler-rt/lib/msan/CMakeLists.txt +++ b/compiler-rt/lib/msan/CMakeLists.txt @@ -5,7 +5,6 @@ set(MSAN_RTL_SOURCES msan.cpp msan_allocator.cpp msan_chained_origin_depot.cpp - msan_dl.cpp msan_interceptors.cpp msan_linux.cpp msan_report.cpp @@ -21,7 +20,6 @@ set(MSAN_RTL_HEADERS msan.h msan_allocator.h msan_chained_origin_depot.h - msan_dl.h msan_flags.h msan_flags.inc msan_interface_internal.h diff --git a/compiler-rt/lib/msan/msan_dl.cpp b/compiler-rt/lib/msan/msan_dl.cpp deleted file mode 100644 index 9814703..0000000 --- a/compiler-rt/lib/msan/msan_dl.cpp +++ /dev/null @@ -1,63 +0,0 @@ -//===-- msan_dl.cpp -------------------------------------------------------===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// -// -// This file is a part of MemorySanitizer. -// -// Helper functions for unpoisoning results of dladdr and dladdr1. -//===----------------------------------------------------------------------===// - -#include "msan_dl.h" - -#include -#include -#include - -#include "msan_poisoning.h" - -namespace __msan { - -void UnpoisonDllAddrInfo(void *info) { - Dl_info *ptr = (Dl_info *)(info); - __msan_unpoison(ptr, sizeof(*ptr)); - if (ptr->dli_fname) - __msan_unpoison(ptr->dli_fname, internal_strlen(ptr->dli_fname) + 1); - if (ptr->dli_sname) - __msan_unpoison(ptr->dli_sname, internal_strlen(ptr->dli_sname) + 1); -} - -void UnpoisonDllAddr1ExtraInfo(void **extra_info, int flags) { - if (flags == RTLD_DL_SYMENT) { - __msan_unpoison(extra_info, sizeof(void *)); - - ElfW(Sym) *s = *((ElfW(Sym) **)(extra_info)); - __msan_unpoison(s, sizeof(ElfW(Sym))); - } else if (flags == RTLD_DL_LINKMAP) { - __msan_unpoison(extra_info, sizeof(void *)); - - struct link_map *map = *((struct link_map **)(extra_info)); - - // Walk forward - for (auto *ptr = map; ptr; ptr = ptr->l_next) { - __msan_unpoison(ptr, sizeof(struct link_map)); - if (ptr->l_name) - __msan_unpoison(ptr->l_name, internal_strlen(ptr->l_name) + 1); - } - - if (!map) - return; - - // Walk backward - for (auto *ptr = map->l_prev; ptr; ptr = ptr->l_prev) { - __msan_unpoison(ptr, sizeof(struct link_map)); - if (ptr->l_name) - __msan_unpoison(ptr->l_name, internal_strlen(ptr->l_name) + 1); - } - } -} - -} // namespace __msan diff --git a/compiler-rt/lib/msan/msan_dl.h b/compiler-rt/lib/msan/msan_dl.h deleted file mode 100644 index 53d1319..0000000 --- a/compiler-rt/lib/msan/msan_dl.h +++ /dev/null @@ -1,28 +0,0 @@ -//===-- msan_dl.h ---------------------------------------------------------===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// -// -// This file is a part of MemorySanitizer. -// -// Helper functions for unpoisoning results of dladdr and dladdr1. -//===----------------------------------------------------------------------===// - -#ifndef MSAN_DL_H -#define MSAN_DL_H - -#include "msan.h" -#include "sanitizer_common/sanitizer_common.h" - -namespace __msan { - -void UnpoisonDllAddrInfo(void *info); -// void UnpoisonDllAddrInfo(Dl_info *info); -void UnpoisonDllAddr1ExtraInfo(void **extra_info, int flags); - -} // namespace __msan - -#endif // MSAN_DL_H diff --git a/compiler-rt/lib/msan/msan_interceptors.cpp b/compiler-rt/lib/msan/msan_interceptors.cpp index 6707104..6f57c33 100644 --- a/compiler-rt/lib/msan/msan_interceptors.cpp +++ b/compiler-rt/lib/msan/msan_interceptors.cpp @@ -19,7 +19,6 @@ #include "interception/interception.h" #include "msan.h" #include "msan_chained_origin_depot.h" -#include "msan_dl.h" #include "msan_origin.h" #include "msan_poisoning.h" #include "msan_report.h" @@ -1521,23 +1520,23 @@ INTERCEPTOR(const char *, strsignal, int sig) { return res; } -INTERCEPTOR(int, dladdr, void *addr, void *info) { +struct dlinfo { + char *dli_fname; + void *dli_fbase; + char *dli_sname; + void *dli_saddr; +}; + +INTERCEPTOR(int, dladdr, void *addr, dlinfo *info) { void *ctx; COMMON_INTERCEPTOR_ENTER(ctx, dladdr, addr, info); int res = REAL(dladdr)(addr, info); - if (res != 0) - UnpoisonDllAddrInfo(info); - return res; -} - -INTERCEPTOR(int, dladdr1, void *addr, void *info, void **extra_info, - int flags) { - void *ctx; - COMMON_INTERCEPTOR_ENTER(ctx, dladdr1, addr, info, extra_info, flags); - int res = REAL(dladdr1)(addr, info, extra_info, flags); if (res != 0) { - UnpoisonDllAddrInfo(info); - UnpoisonDllAddr1ExtraInfo(extra_info, flags); + __msan_unpoison(info, sizeof(*info)); + if (info->dli_fname) + __msan_unpoison(info->dli_fname, internal_strlen(info->dli_fname) + 1); + if (info->dli_sname) + __msan_unpoison(info->dli_sname, internal_strlen(info->dli_sname) + 1); } return res; } @@ -1789,7 +1788,6 @@ void InitializeInterceptors() { MSAN_MAYBE_INTERCEPT_EPOLL_PWAIT; INTERCEPT_FUNCTION(strsignal); INTERCEPT_FUNCTION(dladdr); - INTERCEPT_FUNCTION(dladdr1); INTERCEPT_FUNCTION(dlerror); INTERCEPT_FUNCTION(dl_iterate_phdr); INTERCEPT_FUNCTION(getrusage); diff --git a/compiler-rt/test/msan/dladdr1_test.c b/compiler-rt/test/msan/dladdr1_test.c deleted file mode 100644 index dc5f6fd..0000000 --- a/compiler-rt/test/msan/dladdr1_test.c +++ /dev/null @@ -1,95 +0,0 @@ -/* RUN: %clang_msan -g %s -o %t - RUN: %clang_msan -g %s -DBUILD_SO -fPIC -o %t-so.so -shared - RUN: %run %t 2>&1 | FileCheck %s - - REQUIRES: glibc{{.*}} -*/ - -#define _GNU_SOURCE - -#ifndef BUILD_SO -#include -#include -#include -#include -#include -#include - -typedef volatile long *(* get_t)(); -get_t GetTls; - -int main(int argc, char *argv[]) { - char path[4096]; - snprintf(path, sizeof(path), "%s-so.so", argv[0]); - int i; - - void *handle = dlopen(path, RTLD_LAZY); - if (!handle) fprintf(stderr, "%s\n", dlerror()); - assert(handle != 0); - GetTls = (get_t)dlsym(handle, "GetTls"); - assert(dlerror() == 0); - - { - printf("Testing RTLD_DL_LINKMAP\n"); - fflush(stdout); - - Dl_info info; - struct link_map *map_ptr; - int ret = dladdr1(GetTls, &info, (void**)(&map_ptr), RTLD_DL_LINKMAP); - assert(ret != 0); - printf("fname: %s\n", info.dli_fname); - printf("fbase: %p\n", info.dli_fbase); - printf("sname: %s\n", info.dli_sname); - // CHECK: sname: GetTls - printf("saddr: %p\n", info.dli_saddr); - - assert(map_ptr != NULL); - printf("map_ptr: %p\n", map_ptr); - fflush(stdout); - - // Find the start of the link map - while(map_ptr->l_prev != NULL) { - fflush(stdout); - map_ptr = map_ptr->l_prev; - } - - fflush(stdout); - while(map_ptr != NULL) { - assert(map_ptr->l_name != NULL); - printf("0x%lx: '%s', %p\n", map_ptr->l_addr, map_ptr->l_name, map_ptr->l_ld); - fflush(stdout); - map_ptr = map_ptr->l_next; - } - // CHECK: libc.so - // CHECK: dladdr1_test - } - - // Test RTLD_DL_SYMENT - - { - printf("Testing RTLD_DL_SYMENT\n"); - fflush(stdout); - - Dl_info info; - ElfW(Sym) *sym; - int ret = dladdr1(GetTls, &info, (void**)(&sym), RTLD_DL_SYMENT); - assert(ret != 0); - printf("fname: %s\n", info.dli_fname); - printf("fbase: %p\n", info.dli_fbase); - printf("sname: %s\n", info.dli_sname); - // CHECK: sname: GetTls - printf("saddr: %p\n", info.dli_saddr); - - printf("sym: %d %d %d %d %lu %lu\n", - sym->st_name, sym->st_info, sym->st_other, - sym->st_shndx, sym->st_value, sym->st_size); - // CHECK: sym: - } - return 0; -} -#else // BUILD_SO -long var; -long *GetTls() { - return &var; -} -#endif -- 2.7.4