-
Notifications
You must be signed in to change notification settings - Fork 12.3k
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
[compiler-rt][rtsan] fdopen/freopen(64) support. #119100
Merged
Merged
Conversation
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
|
@llvm/pr-subscribers-compiler-rt-sanitizer Author: David CARLIER (devnexen) ChangesFull diff: https://github.com/llvm/llvm-project/pull/119100.diff 2 Files Affected:
diff --git a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
index f99d68defa613b..5f9d1b4248be6e 100644
--- a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
+++ b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
@@ -244,14 +244,28 @@ INTERCEPTOR(FILE *, fopen, const char *path, const char *mode) {
return REAL(fopen)(path, mode);
}
+INTERCEPTOR(FILE *, freopen, const char *path, const char *mode, FILE *stream) {
+ __rtsan_notify_intercepted_call("freopen");
+ return REAL(freopen)(path, mode, stream);
+}
+
+// Streams
+
#if SANITIZER_INTERCEPT_FOPEN64
INTERCEPTOR(FILE *, fopen64, const char *path, const char *mode) {
__rtsan_notify_intercepted_call("fopen64");
return REAL(fopen64)(path, mode);
}
-#define RTSAN_MAYBE_INTERCEPT_FOPEN64 INTERCEPT_FUNCTION(fopen64)
+
+INTERCEPTOR(FILE *, freopen64, const char *path, const char *mode, FILE *stream) {
+ __rtsan_notify_intercepted_call("freopen64");
+ return REAL(freopen64)(path, mode, stream);
+}
+#define RTSAN_MAYBE_INTERCEPT_FOPEN64 INTERCEPT_FUNCTION(fopen64);
+#define RTSAN_MAYBE_INTERCEPT_FREOPEN64 INTERCEPT_FUNCTION(freopen64);
#else
#define RTSAN_MAYBE_INTERCEPT_FOPEN64
+#define RTSAN_MAYBE_INTERCEPT_FREOPEN64
#endif // SANITIZER_INTERCEPT_FOPEN64
INTERCEPTOR(size_t, fread, void *ptr, size_t size, size_t nitems,
@@ -276,7 +290,11 @@ INTERCEPTOR(int, fputs, const char *s, FILE *stream) {
return REAL(fputs)(s, stream);
}
-// Streams
+INTERCEPTOR(FILE *, fdopen, int fd, const char *mode) {
+ __rtsan_notify_intercepted_call("fdopen");
+ return REAL(fdopen)(fd, mode);
+}
+
INTERCEPTOR(int, puts, const char *s) {
__rtsan_notify_intercepted_call("puts");
return REAL(puts)(s);
@@ -904,6 +922,7 @@ void __rtsan::InitializeInterceptors() {
INTERCEPT_FUNCTION(close);
INTERCEPT_FUNCTION(fopen);
RTSAN_MAYBE_INTERCEPT_FOPEN64;
+ RTSAN_MAYBE_INTERCEPT_FREOPEN64;
INTERCEPT_FUNCTION(fread);
INTERCEPT_FUNCTION(read);
INTERCEPT_FUNCTION(write);
@@ -921,6 +940,8 @@ void __rtsan::InitializeInterceptors() {
RTSAN_MAYBE_INTERCEPT_CREAT64;
INTERCEPT_FUNCTION(puts);
INTERCEPT_FUNCTION(fputs);
+ INTERCEPT_FUNCTION(fdopen);
+ INTERCEPT_FUNCTION(freopen);
INTERCEPT_FUNCTION(lseek);
RTSAN_MAYBE_INTERCEPT_LSEEK64;
INTERCEPT_FUNCTION(dup);
diff --git a/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp b/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
index 6e7cc3b3b3481c..491882198d44c9 100644
--- a/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
+++ b/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
@@ -391,6 +391,27 @@ TEST_F(RtsanOpenedFileTest, IoctlBehavesWithOutputArg) {
EXPECT_THAT(arg, Ge(0));
}
+
+TEST_F(RtsanOpenedFileTest, FdopenDiesWhenRealtime) {
+ auto Func = [&]() {
+ FILE *f = fdopen(GetOpenFd(), "w");
+ EXPECT_THAT(f, Ne(nullptr));
+ };
+
+ ExpectRealtimeDeath(Func, "fdopen");
+ ExpectNonRealtimeSurvival(Func);
+}
+
+TEST_F(RtsanOpenedFileTest, FreopenDiesWhenRealtime) {
+ auto Func = [&]() {
+ FILE *newfile = freopen(GetTemporaryFilePath(), "w", GetOpenFile());
+ EXPECT_THAT(newfile, Ne(nullptr));
+ };
+
+ ExpectRealtimeDeath(Func, MAYBE_APPEND_64("freopen"));
+ ExpectNonRealtimeSurvival(Func);
+}
+
TEST(TestRtsanInterceptors, IoctlBehavesWithOutputPointer) {
// These initial checks just see if we CAN run these tests.
// If we can't (can't open a socket, or can't find an interface, just
|
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
b4c4753 to
33e23ca
Compare
33e23ca to
9bf5450
Compare
cjappl
approved these changes
Dec 8, 2024
broxigarchen
pushed a commit
to broxigarchen/llvm-project
that referenced
this pull request
Dec 10, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
No description provided.