Skip to content

Commit 06641be

Browse files
Kenoclaude
authored andcommitted
Record vmsplice
rr treated vmsplice as unsupported, so a recording aborted when a tracee called it ("Expected ENOSYS for 'vmsplice' but got result 131072"). GNU coreutils' yes writes its output with vmsplice since version 9.11, so `yes | head` could not be recorded. vmsplice writes user memory only when fd is open for reading but not for writing (the read end of a pipe): it then copies pipe data into the iovecs, and rr records it like readv. rr gets fd's access mode from /proc/<tid>/fdinfo/<fd>. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 9cb3a1b commit 06641be

6 files changed

Lines changed: 297 additions & 3 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,6 +1520,7 @@ set(BASIC_TESTS
15201520
vfork_shared
15211521
video_capture
15221522
vm_readv_writev
1523+
vmsplice
15231524
vsyscall
15241525
vsyscall_timeslice
15251526
x86/x87env

‎src/Task.cc‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2916,7 +2916,8 @@ static void copy_tls(const Task::CapturedState& state,
29162916
RR_ARCH_FUNCTION(copy_tls_arch, remote.arch(), state, remote);
29172917
}
29182918

2919-
static int64_t fdinfo_field(Task* t, int fd, const char* field, bool must_exist) {
2919+
static int64_t fdinfo_field(Task* t, int fd, const char* field, bool must_exist,
2920+
int base = 10) {
29202921
char buf[1024];
29212922
sprintf(buf, "/proc/%d/fdinfo/%d", t->tid, fd);
29222923
ScopedFd info(buf, O_RDONLY);
@@ -2934,7 +2935,7 @@ static int64_t fdinfo_field(Task* t, int fd, const char* field, bool must_exist)
29342935
while (*p) {
29352936
if (strncmp(p, field, field_len) == 0) {
29362937
char* end;
2937-
long long int r = strtoll(p + field_len, &end, 10);
2938+
long long int r = strtoll(p + field_len, &end, base);
29382939
ASSERT(t, *end == 0 || *end == '\n');
29392940
return r;
29402941
}
@@ -2957,6 +2958,10 @@ pid_t Task::pid_of_pidfd(int fd) {
29572958
return fdinfo_field(this, fd, "Pid:", false);
29582959
}
29592960

2961+
int Task::fd_flags(int fd) {
2962+
return fdinfo_field(this, fd, "flags:", false, 8);
2963+
}
2964+
29602965
Task::CapturedState Task::capture_state() {
29612966
CapturedState state;
29622967
state.rec_tid = rec_tid;

‎src/Task.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,11 @@ class Task {
257257
* Get pid of pidfd |fd|
258258
*/
259259
pid_t pid_of_pidfd(int fd);
260+
/**
261+
* Get the file status flags of |fd| (as open(2) takes them, including the
262+
* access mode), or -1 if |fd| is not open.
263+
*/
264+
int fd_flags(int fd);
260265

261266
/**
262267
* Records the wait status of this task as |status|, e.g. if

‎src/record_syscall.cc‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4249,6 +4249,36 @@ static Switchable rec_prepare_syscall_arch(RecordTask* t,
42494249
return ALLOW_SWITCH;
42504250
}
42514251

4252+
/* ssize_t vmsplice(int fd, const struct iovec *iov, unsigned long nr_segs,
4253+
unsigned int flags); */
4254+
case Arch::vmsplice: {
4255+
// When fd is open for reading but not for writing (the read end of a
4256+
// pipe), the kernel copies pipe data into the iovecs like readv().
4257+
// Otherwise it writes no user memory.
4258+
int fd_flags = t->fd_flags((int)regs.arg1_signed());
4259+
// import_iovec() takes nr_segs as an unsigned int.
4260+
unsigned int nr_segs = (unsigned int)regs.arg3();
4261+
if (fd_flags < 0 || (fd_flags & O_ACCMODE) != O_RDONLY ||
4262+
nr_segs > IOV_MAX) {
4263+
return ALLOW_SWITCH;
4264+
}
4265+
auto iovecsp = remote_ptr<typename Arch::iovec>(regs.arg2());
4266+
bool ok = true;
4267+
auto iovecs = t->read_mem(iovecsp, nr_segs, &ok);
4268+
if (!ok) {
4269+
return ALLOW_SWITCH;
4270+
}
4271+
syscall_state.reg_parameter(2, sizeof(typename Arch::iovec) * nr_segs,
4272+
IN);
4273+
ParamSize io_size =
4274+
ParamSize::from_syscall_result<typename Arch::ssize_t>();
4275+
for (unsigned int i = 0; i < nr_segs; ++i) {
4276+
syscall_state.mem_ptr_parameter(REMOTE_PTR_FIELD(iovecsp + i, iov_base),
4277+
io_size.limit_size(iovecs[i].iov_len));
4278+
}
4279+
return ALLOW_SWITCH;
4280+
}
4281+
42524282
case Arch::sendfile: {
42534283
syscall_state.reg_parameter<typename Arch::off_t>(3, IN_OUT);
42544284
return ALLOW_SWITCH;

‎src/syscalls.py‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1517,7 +1517,15 @@ def __init__(self, **kwargs):
15171517

15181518
sync_file_range = IrregularEmulatedSyscall(x86=314, x64=277, generic=84)
15191519
tee = UnsupportedSyscall(x86=315, x64=276, generic=77)
1520-
vmsplice = UnsupportedSyscall(x86=316, x64=278, generic=75)
1520+
1521+
# ssize_t vmsplice(int fd, const struct iovec *iov, unsigned long nr_segs,
1522+
# unsigned int flags);
1523+
#
1524+
# When fd is open for writing (the write end of a pipe), vmsplice() adds
1525+
# the user pages the iovecs describe to the pipe. When fd is open only for
1526+
# reading (the read end of a pipe), it copies data from the pipe into the
1527+
# iovecs, like readv().
1528+
vmsplice = IrregularEmulatedSyscall(x86=316, x64=278, generic=75)
15211529
move_pages = UnsupportedSyscall(x86=317, x64=279, generic=239)
15221530
getcpu = EmulatedSyscall(x86=318, x64=309, generic=168, arg1="unsigned int", arg2="unsigned int")
15231531
epoll_pwait = IrregularEmulatedSyscall(x86=319, x64=281, generic=22)

‎src/test/vmsplice.c‎

Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
/* -*- Mode: C; tab-width: 8; c-basic-offset: 2; indent-tabs-mode: nil; -*- */
2+
3+
#include "util.h"
4+
5+
#define SIZE (4 * 4096)
6+
#define BLOCKING_ROUNDS 8
7+
8+
static char out[SIZE];
9+
10+
struct helper_args {
11+
pid_t main_tid;
12+
int read_fd;
13+
int write_fd;
14+
int round;
15+
};
16+
17+
static char pattern_byte(size_t i) { return (char)(i * 7 + 3); }
18+
19+
/* Checks that p holds the bytes from offset to offset + len of the pattern
20+
that buf is filled with. */
21+
static void check_pattern(const char* p, size_t offset, size_t len) {
22+
size_t i;
23+
for (i = 0; i < len; ++i) {
24+
test_assert(p[i] == pattern_byte(offset + i));
25+
}
26+
}
27+
28+
static void read_all(int fd, char* buf, size_t size) {
29+
size_t done = 0;
30+
while (done < size) {
31+
ssize_t n = read(fd, buf + done, size - done);
32+
test_assert(n > 0);
33+
done += n;
34+
}
35+
}
36+
37+
static int in_vmsplice(pid_t tid, int fd) {
38+
char path[PATH_MAX];
39+
char buf[1024];
40+
long nr;
41+
unsigned long arg1;
42+
int proc_fd;
43+
ssize_t len;
44+
45+
sprintf(path, "/proc/self/task/%d/syscall", tid);
46+
proc_fd = open(path, O_RDONLY);
47+
test_assert(proc_fd >= 0);
48+
len = read(proc_fd, buf, sizeof(buf) - 1);
49+
test_assert(len > 0);
50+
buf[len] = 0;
51+
test_assert(0 == close(proc_fd));
52+
return sscanf(buf, "%ld 0x%lx", &nr, &arg1) == 2 && nr == SYS_vmsplice &&
53+
arg1 == (unsigned long)fd;
54+
}
55+
56+
static void wait_for_vmsplice(const struct helper_args* args, int fd) {
57+
test_assert(args->main_tid > 0);
58+
while (!in_vmsplice(args->main_tid, fd)) {
59+
sched_yield();
60+
}
61+
}
62+
63+
/* Writes to the empty pipe the main thread is reading with vmsplice(), then
64+
waits until the data shows up in the main thread's buffer. */
65+
static void* do_write(void* p) {
66+
const struct helper_args* args = (const struct helper_args*)p;
67+
char data[100];
68+
69+
memset(data, 'a' + args->round, sizeof(data));
70+
wait_for_vmsplice(args, args->read_fd);
71+
test_assert(sizeof(data) == write(args->write_fd, data, sizeof(data)));
72+
while (__atomic_load_n(&out[sizeof(data) - 1], __ATOMIC_SEQ_CST) !=
73+
data[0]) {
74+
}
75+
return NULL;
76+
}
77+
78+
/* Drains the full pipe the main thread is writing with vmsplice(). */
79+
static void* do_read(void* p) {
80+
const struct helper_args* args = (const struct helper_args*)p;
81+
int pipe_size = fcntl(args->read_fd, F_GETPIPE_SZ);
82+
char* data;
83+
84+
test_assert(pipe_size > 0);
85+
data = (char*)malloc(pipe_size + 4096);
86+
test_assert(data != NULL);
87+
wait_for_vmsplice(args, args->write_fd);
88+
read_all(args->read_fd, data, pipe_size + 4096);
89+
check_pattern(data + pipe_size, 0, 4096);
90+
free(data);
91+
return NULL;
92+
}
93+
94+
int main(void) {
95+
int fds[2];
96+
int closed_fds[2];
97+
int rw_fd;
98+
int null_fd;
99+
char* buf;
100+
char* filler;
101+
char path[PATH_MAX];
102+
struct iovec iov[3];
103+
struct helper_args args;
104+
pthread_t thread;
105+
ssize_t n;
106+
size_t i;
107+
int pipe_size;
108+
int round;
109+
110+
buf = (char*)mmap(NULL, SIZE, PROT_READ | PROT_WRITE,
111+
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
112+
test_assert(buf != MAP_FAILED);
113+
for (i = 0; i < SIZE; ++i) {
114+
buf[i] = pattern_byte(i);
115+
}
116+
test_assert(0 == pipe(fds));
117+
118+
/* User memory into the pipe (fd is the write end). vmsplice() must leave
119+
buf unchanged. */
120+
iov[0].iov_base = buf;
121+
iov[0].iov_len = SIZE / 2;
122+
iov[1].iov_base = buf + SIZE / 2;
123+
iov[1].iov_len = SIZE / 2;
124+
n = vmsplice(fds[1], iov, 2, 0);
125+
test_assert(n == SIZE);
126+
check_pattern(buf, 0, SIZE);
127+
read_all(fds[0], out, SIZE);
128+
check_pattern(out, 0, SIZE);
129+
130+
/* The same with SPLICE_F_GIFT (whole pages). */
131+
iov[0].iov_base = buf;
132+
iov[0].iov_len = SIZE;
133+
n = vmsplice(fds[1], iov, 1, SPLICE_F_GIFT);
134+
test_assert(n == SIZE);
135+
check_pattern(buf, 0, SIZE);
136+
memset(out, 0, SIZE);
137+
read_all(fds[0], out, SIZE);
138+
check_pattern(out, 0, SIZE);
139+
140+
/* A pipe opened for reading and writing: vmsplice() reads user memory
141+
into the pipe. */
142+
sprintf(path, "/proc/self/fd/%d", fds[0]);
143+
rw_fd = open(path, O_RDWR);
144+
test_assert(rw_fd >= 0);
145+
iov[0].iov_base = buf;
146+
iov[0].iov_len = 4096;
147+
n = vmsplice(rw_fd, iov, 1, 0);
148+
test_assert(n == 4096);
149+
check_pattern(buf, 0, SIZE);
150+
memset(out, 0, SIZE);
151+
read_all(fds[0], out, 4096);
152+
check_pattern(out, 0, 4096);
153+
test_assert(0 == close(rw_fd));
154+
155+
/* The pipe into user memory (fd is the read end), scattered over three
156+
segments; the last is only partly filled. */
157+
test_assert(1000 == write(fds[1], buf, 1000));
158+
memset(out, 0, SIZE);
159+
iov[0].iov_base = out;
160+
iov[0].iov_len = 300;
161+
iov[1].iov_base = out + 4096;
162+
iov[1].iov_len = 500;
163+
iov[2].iov_base = out + 3 * 4096;
164+
iov[2].iov_len = 4096;
165+
n = vmsplice(fds[0], iov, 3, 0);
166+
test_assert(n == 1000);
167+
check_pattern(out, 0, 300);
168+
check_pattern(out + 4096, 300, 500);
169+
check_pattern(out + 3 * 4096, 800, 200);
170+
for (i = 0; i < 300; ++i) {
171+
test_assert(out[3 * 4096 + 200 + i] == 0);
172+
}
173+
atomic_printf("checksum %d %d %d\n", out[0] + out[299], out[4096 + 499],
174+
out[3 * 4096 + 199]);
175+
176+
/* vmsplice() from the empty pipe blocks until another thread writes. */
177+
args.main_tid = sys_gettid();
178+
args.read_fd = fds[0];
179+
args.write_fd = fds[1];
180+
for (round = 0; round < BLOCKING_ROUNDS; ++round) {
181+
memset(out, 0, SIZE);
182+
args.round = round;
183+
test_assert(0 == pthread_create(&thread, NULL, do_write, &args));
184+
iov[0].iov_base = out;
185+
iov[0].iov_len = SIZE;
186+
n = vmsplice(fds[0], iov, 1, 0);
187+
test_assert(n == 100);
188+
test_assert(0 == pthread_join(thread, NULL));
189+
for (i = 0; i < 100; ++i) {
190+
test_assert(out[i] == 'a' + round);
191+
}
192+
test_assert(out[100] == 0);
193+
}
194+
195+
/* vmsplice() into the full pipe blocks until another thread reads. */
196+
pipe_size = fcntl(fds[1], F_GETPIPE_SZ);
197+
test_assert(pipe_size > 0);
198+
filler = (char*)malloc(pipe_size);
199+
test_assert(filler != NULL);
200+
memset(filler, 'f', pipe_size);
201+
test_assert(pipe_size == write(fds[1], filler, pipe_size));
202+
free(filler);
203+
test_assert(0 == pthread_create(&thread, NULL, do_read, &args));
204+
iov[0].iov_base = buf;
205+
iov[0].iov_len = 4096;
206+
n = vmsplice(fds[1], iov, 1, SPLICE_F_GIFT);
207+
test_assert(n == 4096);
208+
test_assert(0 == pthread_join(thread, NULL));
209+
check_pattern(buf, 0, SIZE);
210+
211+
/* Without writers, vmsplice() from the empty pipe returns 0. */
212+
test_assert(0 == pipe(closed_fds));
213+
test_assert(0 == close(closed_fds[1]));
214+
iov[0].iov_base = out;
215+
iov[0].iov_len = SIZE;
216+
test_assert(0 == vmsplice(closed_fds[0], iov, 1, 0));
217+
test_assert(0 == close(closed_fds[0]));
218+
219+
/* The kernel takes nr_segs as an unsigned int. */
220+
test_assert(10 == write(fds[1], buf, 10));
221+
memset(out, 0, SIZE);
222+
n = syscall(SYS_vmsplice, fds[0], iov,
223+
(unsigned long)(((uint64_t)1 << 32) | 1), 0);
224+
test_assert(n == 10);
225+
check_pattern(out, 0, 10);
226+
227+
/* Errors. */
228+
test_assert(-1 == vmsplice(fds[0], iov, 1, SPLICE_F_NONBLOCK) &&
229+
errno == EAGAIN);
230+
test_assert(-1 == vmsplice(-1, iov, 1, 0) && errno == EBADF);
231+
null_fd = open("/dev/null", O_RDONLY);
232+
test_assert(null_fd >= 0);
233+
test_assert(-1 == vmsplice(null_fd, iov, 1, 0) && errno == EBADF);
234+
test_assert(0 == close(null_fd));
235+
test_assert(-1 == vmsplice(fds[0], iov, 1, 0x10000) && errno == EINVAL);
236+
test_assert(-1 == vmsplice(fds[0], iov, IOV_MAX + 1, 0) && errno == EINVAL);
237+
test_assert(0 == munmap(buf, SIZE));
238+
test_assert(-1 == vmsplice(fds[0], (struct iovec*)buf, 1, 0) &&
239+
errno == EFAULT);
240+
test_assert(0 == vmsplice(fds[0], iov, 0, 0));
241+
test_assert(0 == vmsplice(fds[1], iov, 0, 0));
242+
243+
atomic_puts("EXIT-SUCCESS");
244+
return 0;
245+
}

0 commit comments

Comments
 (0)