Debian Codegraph @

testsuite/source-change-size-continues_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Regression guard for a source file changing size during send_files().
3 #
4 # The sender hardening for source reads now opens content through the confined
5 # sender path instead of the old plain open. A source file can still change
6 # after the file list recorded its original size and after send_files() fstat()s
7 # the opened fd. That per-file read failure may be diagnosed as a partial
8 # transfer, but it must not abort the sender/receiver pair before unrelated
9 # later file-list entries are transferred.
10 #
11 # To avoid a timing race, this test runs rsync under a scratch-built LD_PRELOAD
12 # shim. The shim watches the exact source fd via /proc/self/fd and truncates it
13 # immediately before the first read() from that fd. This leaves the file-list
14 # and send_files() fstat() sizes at the original value, then deterministically
15 # exercises fileio.c's "file changed mid transfer" map_ptr() path.
16
17 import os
18 import shlex
19 import shutil
20 import subprocess
21 import sys
22
23 from rsyncfns import (
24 RSYNC, SCRATCHDIR, assert_same, make_data_file, makepath, rmtree,
25 rsync_argv, test_fail, test_skipped, split_rsync_cmd,
26 )
27
28
29 SHIM_C = r'''
30 #define _GNU_SOURCE 1
31 #include <dlfcn.h>
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <limits.h>
35 #include <stddef.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40
41 static char target_path[PATH_MAX];
42 static int triggered;
43 static int resolving;
44
45 __attribute__((constructor))
46 static void mark_loaded(void)
47 {
48 const char *mark = getenv("RSYNC_SHRINK_LOAD_MARK");
49 int fd;
50
51 if (!mark || !*mark)
52 return;
53 fd = open(mark, O_WRONLY | O_CREAT | O_TRUNC, 0600);
54 if (fd >= 0) {
55 (void)write(fd, "loaded\n", 7);
56 (void)close(fd);
57 }
58 }
59
60 static void init_target(void)
61 {
62 const char *env;
63
64 if (target_path[0])
65 return;
66 env = getenv("RSYNC_SHRINK_TARGET");
67 if (!env || !*env)
68 return;
69 if (!realpath(env, target_path)) {
70 strncpy(target_path, env, sizeof target_path - 1);
71 target_path[sizeof target_path - 1] = '\0';
72 }
73 }
74
75 static int fd_is_target(int fd)
76 {
77 char procpath[64];
78 char actual[PATH_MAX];
79 ssize_t n;
80
81 init_target();
82 if (!target_path[0])
83 return 0;
84 snprintf(procpath, sizeof procpath, "/proc/self/fd/%d", fd);
85 n = readlink(procpath, actual, sizeof actual - 1);
86 if (n < 0)
87 return 0;
88 actual[n] = '\0';
89 return strcmp(actual, target_path) == 0;
90 }
91
92 ssize_t read(int fd, void *buf, size_t count)
93 {
94 static ssize_t (*real_read)(int, void *, size_t);
95
96 if (!real_read) {
97 real_read = (ssize_t (*)(int, void *, size_t))dlsym(RTLD_NEXT, "read");
98 if (!real_read) {
99 errno = EIO;
100 return -1;
101 }
102 }
103
104 if (!triggered && !resolving) {
105 resolving = 1;
106 if (fd_is_target(fd)) {
107 const char *mark = getenv("RSYNC_SHRINK_MARK");
108 triggered = 1;
109 if (truncate(target_path, 0) == 0 && mark && *mark) {
110 int mfd = open(mark, O_WRONLY | O_CREAT | O_TRUNC, 0600);
111 if (mfd >= 0) {
112 (void)write(mfd, "shrunk\n", 7);
113 (void)close(mfd);
114 }
115 }
116 }
117 resolving = 0;
118 }
119
120 return real_read(fd, buf, count);
121 }
122 '''
123
124
125 def compiler():
126 cc = os.environ.get('CC')
127 if cc:
128 return shlex.split(cc)
129 for name in ('cc', 'gcc', 'clang'):
130 path = shutil.which(name)
131 if path:
132 return [path]
133 return None
134
135
136 def build_shim():
137 if not sys.platform.startswith('linux'):
138 test_skipped('source-size-change regression uses LD_PRELOAD and '
139 '/proc/self/fd to synchronise the source read')
140 cc = compiler()
141 if not cc:
142 test_skipped('no C compiler available to build the source-read shim')
143
144 cfile = SCRATCHDIR / 'source_shrink_read.c'
145 sofile = SCRATCHDIR / 'source_shrink_read.so'
146 cfile.write_text(SHIM_C)
147 proc = subprocess.run(
148 cc + ['-shared', '-fPIC', '-O2', '-Wall', '-Wextra',
149 '-o', str(sofile), str(cfile), '-ldl'],
150 stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
151 if proc.returncode != 0 or not sofile.is_file():
152 test_skipped('could not build the source-read shim:\n'
153 + (proc.stdout or '').strip())
154 return sofile
155
156
157 first = split_rsync_cmd(RSYNC)[0] if RSYNC else ''
158 if os.path.basename(first) == 'valgrind':
159 test_skipped('LD_PRELOAD read hook is not meaningful under valgrind')
160
161 base = SCRATCHDIR / 'source-change-size'
162 src = base / 'src'
163 dest = base / 'dest'
164 rmtree(base)
165 makepath(src, dest)
166
167 changing = src / '01-changing.bin'
168 later = src / '02-later.txt'
169 make_data_file(changing, 1024 * 1024)
170 later.write_text('later file must still transfer\n')
171
172 shim = build_shim()
173 mark = SCRATCHDIR / 'source_shrink_read.mark'
174 load_mark = SCRATCHDIR / 'source_shrink_load.mark'
175 mark.unlink(missing_ok=True)
176 load_mark.unlink(missing_ok=True)
177 env = os.environ.copy()
178 env['LD_PRELOAD'] = str(shim)
179 env['RSYNC_SHRINK_TARGET'] = str(changing.resolve())
180 env['RSYNC_SHRINK_MARK'] = str(mark)
181 env['RSYNC_SHRINK_LOAD_MARK'] = str(load_mark)
182
183 argv = rsync_argv('-a', '--no-whole-file', f'{src}/', f'{dest}/')
184 print('Running:', ' '.join(argv))
185 proc = subprocess.run(argv, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
186 text=True, env=env)
187 out = proc.stdout or ''
188 print(out, end='')
189
190 if not load_mark.exists():
191 test_skipped('source-read shim was not exercised; rsync may be static or '
192 'the platform ignored LD_PRELOAD')
193 if not mark.exists():
194 test_fail('source-read shim loaded but did not target the changing source '
195 'file')
196
197 if proc.returncode == 0:
198 test_fail('rsync succeeded even though the shim forced a source read error; '
199 'expected a partial-transfer diagnosis')
200 if proc.returncode != 23:
201 test_fail(f'expected partial-transfer exit 23 after the source changed '
202 f'size; got {proc.returncode}:\n{out}')
203
204 if not (dest / later.name).is_file():
205 test_fail('sender aborted before transferring the later file-list entry')
206 assert_same(later, dest / later.name, label='later file')
207
208 if 'read errors mapping' not in out:
209 test_fail('rsync exited 23 but did not report the sender-side changed-source '
210 'diagnosis:\n' + out)
211
212 print('source-change-size-continues: changed source file produced a partial '
213 'transfer while a later file-list entry still transferred')