-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[sanitizer_common] Add experimental flag to tweak dlopen(<main program>) #71715
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2a73a5f
[sanitizer_common] Add experimental flag to tweak dlopen(<main program>)
thurstond da65373
Check validity of filename string before reading
thurstond c798bf1
internal_is_suffix fixes per Kirill's suggestion
thurstond 17be36d
Use dladdr to obtain the name of the main program, and then
thurstond 3783ba1
Simplify code, assuming that we don't need to canonicalize paths.
thurstond 85092c5
Capitalized CamelCase
thurstond File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.