Skip to content

Commit 385589d

Browse files
committed
[compiler-rt][rtsan] adding Linux's clone call interception.
1 parent a779af3 commit 385589d

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,6 +1361,31 @@ INTERCEPTOR(int, execve, const char *filename, char *const argv[],
13611361
return REAL(execve)(filename, argv, envp);
13621362
}
13631363

1364+
#if SANITIZER_GLIBC
1365+
INTERCEPTOR(int, clone, int (*f)(void *), void *stack, int flags, void *arg,
1366+
...) {
1367+
__rtsan_notify_intercepted_call("clone");
1368+
1369+
if ((flags & CLONE_PIDFD) || (flags & CLONE_SETTLS) ||
1370+
(flags & CLONE_PARENT_SETTID) || (flags & CLONE_CHILD_SETTID) ||
1371+
(flags & CLONE_CHILD_CLEARTID)) {
1372+
va_list args;
1373+
va_start(args, arg);
1374+
pid_t *parent = va_arg(args, pid_t *);
1375+
void *tls = va_arg(args, void *);
1376+
pid_t *child = va_arg(args, pid_t *);
1377+
va_end(args);
1378+
1379+
return REAL(clone)(f, stack, flags, arg, parent, tls, child);
1380+
}
1381+
1382+
return REAL(clone)(f, stack, flags, arg);
1383+
}
1384+
#define RTSAN_MAYBE_INTERCEPT_CLONE INTERCEPT_FUNCTION(clone)
1385+
#else
1386+
#define RTSAN_MAYBE_INTERCEPT_CLONE
1387+
#endif
1388+
13641389
#if SANITIZER_INTERCEPT_PROCESS_VM_READV
13651390
INTERCEPTOR(ssize_t, process_vm_readv, pid_t pid, const struct iovec *local_iov,
13661391
unsigned long liovcnt, const struct iovec *remote_iov,
@@ -1595,6 +1620,7 @@ void __rtsan::InitializeInterceptors() {
15951620

15961621
INTERCEPT_FUNCTION(fork);
15971622
INTERCEPT_FUNCTION(execve);
1623+
RTSAN_MAYBE_INTERCEPT_CLONE;
15981624

15991625
RTSAN_MAYBE_INTERCEPT_PROCESS_VM_READV;
16001626
RTSAN_MAYBE_INTERCEPT_PROCESS_VM_WRITEV;

compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,6 +1245,20 @@ TEST_F(PthreadRwlockTest, PthreadRwlockWrlockSurvivesWhenNonRealtime) {
12451245
ExpectNonRealtimeSurvival(Func);
12461246
}
12471247

1248+
#if SANITIZER_GLIBC
1249+
TEST(TestRtsanInterceptors, CloneDiesWhenRealtime) {
1250+
std::vector<char> buf;
1251+
buf.resize(1024 * 1024);
1252+
auto Call = [](void *a) { return 0; };
1253+
auto Func = [&buf, &Call]() {
1254+
clone(Call, buf.data() + buf.size(), 0, nullptr);
1255+
};
1256+
1257+
ExpectRealtimeDeath(Func, "clone");
1258+
ExpectNonRealtimeSurvival(Func);
1259+
}
1260+
#endif
1261+
12481262
/*
12491263
Sockets
12501264
*/

0 commit comments

Comments
 (0)