blob: b38f16fa34b83e6a33f8efb417be0d0fa1633e8e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
#!/bin/sh
set -eu
ROOT=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
SYNC_BIN=${SYNC_BIN:-"$ROOT/out/sync"}
CC=${CC:-cc}
TMPDIR=${TMPDIR:-/tmp}
WORKDIR=$(mktemp -d "$TMPDIR/sync-test.XXXXXX")
HOOK_C="$WORKDIR/sync-hook.c"
HOOK_SO="$WORKDIR/sync-hook.so"
STDOUT_FILE="$WORKDIR/stdout"
STDERR_FILE="$WORKDIR/stderr"
SYNC_MARKER="$WORKDIR/sync-marker"
trap 'rm -rf "$WORKDIR"' EXIT INT TERM
export LC_ALL=C
fail() {
printf '%s\n' "FAIL: $1" >&2
exit 1
}
assert_eq() {
name=$1
expected=$2
actual=$3
if [ "$expected" != "$actual" ]; then
printf '%s\n' "FAIL: $name" >&2
printf '%s\n' "--- expected ---" >&2
printf '%s' "$expected" >&2
printf '\n%s\n' "--- actual ---" >&2
printf '%s' "$actual" >&2
printf '\n' >&2
exit 1
fi
}
assert_empty() {
name=$1
text=$2
if [ -n "$text" ]; then
printf '%s\n' "FAIL: $name" >&2
printf '%s\n' "--- expected empty ---" >&2
printf '%s\n' "--- actual ---" >&2
printf '%s' "$text" >&2
printf '\n' >&2
exit 1
fi
}
assert_status() {
name=$1
expected=$2
actual=$3
if [ "$expected" -ne "$actual" ]; then
printf '%s\n' "FAIL: $name" >&2
printf '%s\n' "expected status: $expected" >&2
printf '%s\n' "actual status: $actual" >&2
exit 1
fi
}
run_capture() {
if "$@" >"$STDOUT_FILE" 2>"$STDERR_FILE"; then
LAST_STATUS=0
else
LAST_STATUS=$?
fi
LAST_STDOUT=$(cat "$STDOUT_FILE")
LAST_STDERR=$(cat "$STDERR_FILE")
}
build_sync_hook() {
cat >"$HOOK_C" <<'EOF'
#define _GNU_SOURCE 1
#include <dlfcn.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
typedef void (*sync_fn_t)(void);
static void
write_marker(const char *path)
{
static const char marker[] = "sync-called\n";
size_t offset;
int fd;
if (path == NULL || path[0] == '\0')
return;
fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600);
if (fd < 0)
return;
offset = 0;
while (offset < sizeof(marker) - 1) {
ssize_t written;
written = write(fd, marker + offset, sizeof(marker) - 1 - offset);
if (written < 0) {
if (errno == EINTR)
continue;
break;
}
offset += (size_t)written;
}
(void)close(fd);
}
void
sync(void)
{
static sync_fn_t real_sync;
static int resolved;
if (!resolved) {
real_sync = (sync_fn_t)dlsym(RTLD_NEXT, "sync");
resolved = 1;
}
write_marker(getenv("SYNC_HOOK_OUTPUT"));
if (real_sync != NULL)
real_sync();
}
EOF
"$CC" -shared -fPIC -O2 -std=c17 -Wall -Wextra -Werror \
"$HOOK_C" -o "$HOOK_SO" -ldl || fail "failed to build sync hook with $CC"
}
[ -x "$SYNC_BIN" ] || fail "missing binary: $SYNC_BIN"
build_sync_hook
run_capture "$SYNC_BIN"
assert_status "sync status" 0 "$LAST_STATUS"
assert_empty "sync stdout" "$LAST_STDOUT"
assert_empty "sync stderr" "$LAST_STDERR"
run_capture "$SYNC_BIN" unexpected
assert_status "operand status" 1 "$LAST_STATUS"
assert_empty "operand stdout" "$LAST_STDOUT"
assert_eq "operand stderr" \
"sync: unexpected argument: unexpected
usage: sync" \
"$LAST_STDERR"
run_capture "$SYNC_BIN" --
assert_status "double dash status" 1 "$LAST_STATUS"
assert_empty "double dash stdout" "$LAST_STDOUT"
assert_eq "double dash stderr" \
"sync: unexpected argument: --
usage: sync" \
"$LAST_STDERR"
run_capture "$SYNC_BIN" first second
assert_status "too many args status" 1 "$LAST_STATUS"
assert_empty "too many args stdout" "$LAST_STDOUT"
assert_eq "too many args stderr" \
"sync: unexpected argument: first
usage: sync" \
"$LAST_STDERR"
rm -f "$SYNC_MARKER"
run_capture env LD_PRELOAD="$HOOK_SO" SYNC_HOOK_OUTPUT="$SYNC_MARKER" "$SYNC_BIN"
assert_status "hooked sync status" 0 "$LAST_STATUS"
assert_empty "hooked sync stdout" "$LAST_STDOUT"
assert_empty "hooked sync stderr" "$LAST_STDERR"
[ -f "$SYNC_MARKER" ] || fail "sync() was not invoked"
assert_eq "hook marker" "sync-called" "$(cat "$SYNC_MARKER")"
rm -f "$SYNC_MARKER"
run_capture env LD_PRELOAD="$HOOK_SO" SYNC_HOOK_OUTPUT="$SYNC_MARKER" \
"$SYNC_BIN" unexpected
assert_status "hooked invalid status" 1 "$LAST_STATUS"
assert_empty "hooked invalid stdout" "$LAST_STDOUT"
assert_eq "hooked invalid stderr" \
"sync: unexpected argument: unexpected
usage: sync" \
"$LAST_STDERR"
[ ! -e "$SYNC_MARKER" ] || fail "sync() ran on invalid arguments"
printf '%s\n' "PASS"
|