forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[sanitizer_common] Add experimental flag to tweak dlopen(<main progra…
…m>) (llvm#71715) This introduces an experimental flag 'test_only_replace_dlopen_main_program'. When enabled, this will replace dlopen(main program,...) with dlopen(NULL,...), which is the correct way to get a handle to the main program. This can be useful when ASan is statically linked, since dladdr((void*)pthread_join) or similar will return the path to the main program. Note that dlopen(main program,...) never ends well: - PIE in recent glibc versions (glibc bugzilla 24323), or non-PIE: return an error - PIE in current GRTE and older glibc: attempt to load the main program again, leading to reinitializing ASan and failing to remap the shadow memory. --------- Co-authored-by: Thurston Dang <thurston@google.com>
- Loading branch information
Showing
7 changed files
with
152 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
//===-- sanitizer_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 has helper functions that depend on libc's dynamic loading | ||
// introspection. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "sanitizer_dl.h" | ||
|
||
#include <dlfcn.h> | ||
|
||
#include "sanitizer_common/sanitizer_platform.h" | ||
|
||
namespace __sanitizer { | ||
extern const char *SanitizerToolName; | ||
|
||
const char *DladdrSelfFName(void) { | ||
#if SANITIZER_GLIBC | ||
Dl_info info; | ||
int ret = dladdr((void *)&SanitizerToolName, &info); | ||
if (ret) { | ||
return info.dli_fname; | ||
} | ||
#endif | ||
|
||
return nullptr; | ||
} | ||
|
||
} // namespace __sanitizer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//===-- sanitizer_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 has helper functions that depend on libc's dynamic loading | ||
// introspection. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef SANITIZER_DL_H | ||
#define SANITIZER_DL_H | ||
|
||
namespace __sanitizer { | ||
|
||
// Returns the path to the shared object or - in the case of statically linked | ||
// sanitizers | ||
// - the main program itself, that contains the sanitizer. | ||
const char* DladdrSelfFName(void); | ||
|
||
} // namespace __sanitizer | ||
|
||
#endif // SANITIZER_DL_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
compiler-rt/test/sanitizer_common/TestCases/Linux/replace_dlopen_main_program_test.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Test 'test_only_replace_dlopen_main_program' flag | ||
|
||
// RUN: %clangxx %s -pie -fPIE -o %t | ||
// RUN: env %tool_options='test_only_replace_dlopen_main_program=true' %run %t | ||
// RUN: env %tool_options='test_only_replace_dlopen_main_program=false' not %run %t | ||
|
||
// dladdr is 'nonstandard GNU extensions that are also present on Solaris' | ||
// REQUIRES: glibc | ||
|
||
// Does not intercept dlopen | ||
// UNSUPPORTED: hwasan, lsan, ubsan | ||
|
||
// Flag has no effect with dynamic runtime | ||
// UNSUPPORTED: asan-dynamic-runtime | ||
|
||
#include <dlfcn.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
// We can't use the address of 'main' (error: ISO C++ does not allow 'main' to be used by a program [-Werror,-Wmain]') | ||
// so we add this function. | ||
__attribute__((noinline, no_sanitize("address"))) void foo() { | ||
printf("Hello World!\n"); | ||
} | ||
|
||
int main(int argc, char *argv[]) { | ||
foo(); | ||
|
||
// "If filename is NULL, then the returned handle is for the main program." | ||
void *correct_handle = dlopen(NULL, RTLD_LAZY); | ||
printf("dlopen(NULL,...): %p\n", correct_handle); | ||
|
||
Dl_info info; | ||
if (dladdr((void *)&foo, &info) == 0) { | ||
printf("dladdr failed\n"); | ||
return 1; | ||
} | ||
printf("dladdr(&foo): %s\n", info.dli_fname); | ||
void *test_handle = dlopen(info.dli_fname, RTLD_LAZY); | ||
printf("dlopen(%s,...): %p\n", info.dli_fname, test_handle); | ||
|
||
if (test_handle != correct_handle) { | ||
printf("Error: handles do not match\n"); | ||
return 1; | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters