Skip to content

Commit c5ffc52

Browse files
devnexenjoaosaffran
authored andcommitted
[compiler-rt][sanitizer_common] copy_file_range syscall interception. (llvm#125816)
1 parent bdac505 commit c5ffc52

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

compiler-rt/lib/sanitizer_common/sanitizer_common_syscalls.inc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3205,6 +3205,28 @@ POST_SYSCALL(futex)
32053205
COMMON_SYSCALL_BLOCKING_END();
32063206
}
32073207

3208+
PRE_SYSCALL(copy_file_range)
3209+
(int fdin, __sanitizer___kernel_off_t *offin, int fdout,
3210+
__sanitizer___kernel_off_t *offout, SIZE_T size, unsigned int flags) {
3211+
if (offin != nullptr) {
3212+
PRE_READ(offin, sizeof(*offin));
3213+
}
3214+
if (offout != nullptr) {
3215+
PRE_READ(offout, sizeof(*offout));
3216+
}
3217+
}
3218+
3219+
POST_SYSCALL(copy_file_range)
3220+
(SSIZE_T, int fdin, __sanitizer___kernel_off_t *offin, int fdout,
3221+
__sanitizer___kernel_off_t *offout, SIZE_T size, unsigned int flags) {
3222+
if (offin != nullptr) {
3223+
POST_WRITE(offin, sizeof(*offin));
3224+
}
3225+
if (offout != nullptr) {
3226+
POST_WRITE(offout, sizeof(*offout));
3227+
}
3228+
}
3229+
32083230
} // extern "C"
32093231

32103232
# undef PRE_SYSCALL
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// RUN: %clangxx -O0 %s -o %t
2+
3+
// REQUIRES: glibc
4+
5+
#ifndef _GNU_SOURCE
6+
# define _GNU_SOURCE
7+
#endif
8+
#include <assert.h>
9+
#include <fcntl.h>
10+
#include <stdlib.h>
11+
#include <sys/syscall.h>
12+
#include <unistd.h>
13+
14+
#if !defined(__GLIBC_PREREQ)
15+
# define __GLIBC_PREREQ(a, b) 0
16+
#endif
17+
18+
#if !__GLIBC_PREREQ(2, 27)
19+
# define copy_file_range(a, b, c, d, e) \
20+
(ssize_t) syscall(__NR_copy_file_range, a, b, c, d, e)
21+
#endif
22+
23+
int main(void) {
24+
int fdin = open("/proc/self/maps", O_RDONLY);
25+
assert(fdin > 0);
26+
char tmp[] = "/tmp/map.XXXXXX";
27+
int fdout = mkstemp(tmp);
28+
assert(fdout > 0);
29+
off_t offin = -1, offout = 0;
30+
ssize_t cpy = copy_file_range(fdin, &offin, fdout, &offout, 8, 0);
31+
assert(cpy < 0);
32+
offin = 0;
33+
offout = 16;
34+
cpy = copy_file_range(fdin, &offin, fdout, &offout, 8, 0);
35+
assert(cpy < 0);
36+
offout = 0;
37+
cpy = copy_file_range(fdin, &offin, fdout, &offout, 8, 0);
38+
assert(cpy == 8);
39+
close(fdout);
40+
close(fdin);
41+
return 0;
42+
}

0 commit comments

Comments
 (0)