-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[rtsan] Fix comment in fcntl, fix va_args type #108440
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
Conversation
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
@llvm/pr-subscribers-compiler-rt-sanitizer Author: Chris Apple (cjappl) ChangesTwo changes here:
Full diff: https://github.com/llvm/llvm-project/pull/108440.diff 1 Files Affected:
diff --git a/compiler-rt/lib/rtsan/rtsan_interceptors.cpp b/compiler-rt/lib/rtsan/rtsan_interceptors.cpp
index 409e27c3ad3234..6ce34e0abb9f7c 100644
--- a/compiler-rt/lib/rtsan/rtsan_interceptors.cpp
+++ b/compiler-rt/lib/rtsan/rtsan_interceptors.cpp
@@ -98,22 +98,38 @@ INTERCEPTOR(int, creat, const char *path, mode_t mode) {
INTERCEPTOR(int, fcntl, int filedes, int cmd, ...) {
__rtsan_expect_not_realtime("fcntl");
- va_list args;
- va_start(args, cmd);
-
// Following precedent here. The linux source (fcntl.c, do_fcntl) accepts the
// final argument in a variable that will hold the largest of the possible
- // argument types (pointers and ints are typical in fcntl) It is then assumed
- // that the implementation of fcntl will cast it properly depending on cmd.
+ // argument types. It is then assumed that the implementation of fcntl will
+ // cast it properly depending on cmd.
+ //
+ // The two types we expect for possible args are `struct flock*` and `int`
+ // we will cast to `intptr_t` which should hold both comfortably.
+ // Why `intptr_t`? It should fit both types, and it follows the freeBSD
+ // approach linked below.
+ using arg_type = intptr_t;
+ static_assert(sizeof(arg_type) >= sizeof(struct flock *));
+ static_assert(sizeof(arg_type) >= sizeof(int));
+
+ // Some cmds will not actually have an argument passed in this va_list.
+ // Calling va_arg when no arg exists is UB, however all currently
+ // supported architectures will give us a result in all three cases
+ // (no arg/int arg/struct flock* arg)
+ // va_arg() will generally read the next argument register or the
+ // stack. If we ever support an arch like CHERI with bounds checking, we
+ // may have to re-evaluate this approach.
//
- // This is also similar to what is done in
- // sanitizer_common/sanitizer_common_syscalls.inc
- const unsigned long arg = va_arg(args, unsigned long);
- int result = REAL(fcntl)(filedes, cmd, arg);
+ // More discussion, and other examples following this approach
+ // https://discourse.llvm.org/t/how-to-write-an-interceptor-for-fcntl/81203
+ // https://reviews.freebsd.org/D46403
+ // https://github.com/bminor/glibc/blob/c444cc1d8335243c5c4e636d6a26c472df85522c/sysdeps/unix/sysv/linux/fcntl64.c#L37-L46
+ va_list args;
+ va_start(args, cmd);
+ const arg_type arg = va_arg(args, arg_type);
va_end(args);
- return result;
+ return REAL(fcntl)(filedes, cmd, arg);
}
INTERCEPTOR(int, close, int filedes) {
|
Friendly ping of reviewers @jyknight and @brooksdavis |
baa7aca
to
248394e
Compare
jyknight
approved these changes
Sep 27, 2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks
248394e
to
1a6601f
Compare
Sterling-Augustine
pushed a commit
to Sterling-Augustine/llvm-project
that referenced
this pull request
Sep 27, 2024
Two changes here: 1. Add significantly more detail on why this is OK, from the conversation here: https://discourse.llvm.org/t/how-to-write-an-interceptor-for-fcntl/81203 2. Change the type we expect from va_args to intptr_t, which was also a suggestion in that thread.
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.
Two changes here: