forked from DKapture/libdkapture
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch_skel.py
More file actions
362 lines (309 loc) · 12.8 KB
/
Copy pathpatch_skel.py
File metadata and controls
362 lines (309 loc) · 12.8 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#!/usr/bin/env python3
# SPDX-FileCopyrightText: 2025 UnionTech Software Technology Co., Ltd
#
# SPDX-License-Identifier: LGPL-2.1
"""
Rewrite xxx__elf_bytes() in a BPF skeleton header (xxx.skel.h) to map the
corresponding xxx.bpf.o file into memory and return its address.
Usage:
patch_skel_elf_bytes.py /abs/path/to/file.skel.h [--dry-run]
Notes:
- The function to patch is detected by the pattern '__elf_bytes(size_t *sz)'.
- The mapped object file path is inferred by replacing '.skel.h' with '.bpf.o'
and resolved to an absolute path. That absolute path is embedded into the
generated function body.
- Required system includes are injected if missing.
"""
import argparse
import os
import re
import sys
from typing import Tuple
INCLUDES_TO_ENSURE = [
"#include <sys/types.h>",
"#include <sys/stat.h>",
"#include <sys/mman.h>",
"#include <fcntl.h>",
"#include <unistd.h>",
"#include <errno.h>",
"#include <stdio.h>",
]
def find_function_span(src: str) -> Tuple[int, int, str]:
"""Find the byte-span [start_idx, end_idx) of the xxx__elf_bytes function body,
including the signature and braces, and return the function prefix name.
Returns: (start_index, end_index, func_name)
Raises ValueError if not found or malformed.
"""
# Match the function signature, capturing the function name prefix
sig_re = re.compile(
r"static\s+inline\s+const\s+void\s*\*\s*(?P<name>[A-Za-z0-9_]+__elf_bytes)\s*\(\s*size_t\s*\*\s*sz\s*\)",
re.MULTILINE,
)
def next_non_space(idx: int) -> int:
n = len(src)
while idx < n and src[idx] in "\r\n\t ":
idx += 1
return idx
m = None
for cand in sig_re.finditer(src):
j = next_non_space(cand.end())
if j < len(src) and src[j] == '{':
m = cand
break
if not m:
raise ValueError("__elf_bytes() function signature not found")
start = m.start()
name = m.group("name")
# Find the opening brace following the signature (on same or next line)
brace_start = src.find("{", m.end())
if brace_start == -1:
raise ValueError("Opening brace not found after signature")
depth = 0
i = brace_start
n = len(src)
while i < n:
ch = src[i]
if ch == '{':
depth += 1
elif ch == '}':
depth -= 1
if depth == 0:
# include the closing brace and trailing newline if any
end = i + 1
# Swallow following newlines/spaces to keep formatting stable
while end < n and src[end] in "\r\n\t ":
end += 1
return start, end, name
i += 1
raise ValueError("Matching closing brace for function not found")
def ensure_includes(src: str) -> str:
"""Ensure the required system includes are present. Insert them after the last
existing #include line near the top of the file.
"""
lines = src.splitlines(keepends=True)
present = set()
last_include_idx = -1
for idx, line in enumerate(lines):
if line.lstrip().startswith('#include '):
last_include_idx = idx
present.add(line.strip())
# Stop scanning includes if we reach a non-include after includes start
elif last_include_idx != -1 and line.strip() and not line.strip().startswith('#'):
break
missing = [inc for inc in INCLUDES_TO_ENSURE if inc not in present]
if not missing:
return src
insertion = ''.join(inc + "\n" for inc in missing)
if last_include_idx == -1:
# Insert after the header guard and any initial comments; default to start
return insertion + src
else:
insert_pos = 0
for i in range(last_include_idx + 1):
insert_pos += len(lines[i])
return src[:insert_pos] + insertion + src[insert_pos:]
def make_replacement_body(func_name: str, abs_obj_path: str) -> str:
"""Generate the replacement function implementation body.
Indentation uses tabs to match the auto-generated style.
"""
prefix = func_name.rsplit("__elf_bytes", 1)[0]
mapped_addr = f"{prefix}__mapped_addr"
mapped_size = f"{prefix}__mapped_size"
return (
f"static inline const void *{func_name}(size_t *sz)\n"
"{\n"
f"\tif (!{mapped_addr}) {{\n"
f"\t\tconst char *path = \"{abs_obj_path}\";\n"
"\t\tint fd = open(path, O_RDONLY);\n"
"\t\tif (fd < 0) {\n"
"\t\t\tfprintf(stderr, \"Error: Failed to open BPF object file '%s': %s\\n\", path, strerror(errno));\n"
"\t\t\treturn NULL;\n"
"\t\t}\n"
"\t\tstruct stat st;\n"
"\t\tif (fstat(fd, &st) != 0) {\n"
"\t\t\tfprintf(stderr, \"Error: Failed to stat BPF object file '%s': %s\\n\", path, strerror(errno));\n"
"\t\t\tclose(fd);\n"
"\t\t\treturn NULL;\n"
"\t\t}\n"
f"\t\t{mapped_size} = (size_t)st.st_size;\n"
f"\t\tvoid *addr = mmap(NULL, {mapped_size}, PROT_READ, MAP_PRIVATE, fd, 0);\n"
"\t\tclose(fd);\n"
"\t\tif (addr == MAP_FAILED) {\n"
"\t\t\tfprintf(stderr, \"Error: Failed to mmap BPF object file '%s': %s\\n\", path, strerror(errno));\n"
f"\t\t\t{mapped_addr} = NULL;\n"
f"\t\t\t{mapped_size} = 0;\n"
"\t\t\treturn NULL;\n"
"\t\t}\n"
f"\t\t{mapped_addr} = addr;\n"
"\t}\n"
f"\t*sz = {mapped_size};\n"
f"\treturn {mapped_addr};\n"
"}\n"
)
def ensure_globals(src: str, mapped_addr: str, mapped_size: str) -> str:
"""Ensure a single declaration of the static globals exists after includes.
- Remove any existing duplicate declarations anywhere in the file
- Insert exactly one pair after the last #include line
"""
# Remove any existing declarations (robust to whitespace)
pattern_addr = re.compile(rf"^\s*static\s+void\s*\*\s*{re.escape(mapped_addr)}\s*=\s*NULL\s*;\s*$", re.MULTILINE)
pattern_size = re.compile(rf"^\s*static\s+size_t\s+{re.escape(mapped_size)}\s*=\s*0\s*;\s*$", re.MULTILINE)
src = pattern_addr.sub("", src)
src = pattern_size.sub("", src)
# Also collapse multiple consecutive newlines left behind
src = re.sub(r"\n{3,}", "\n\n", src)
# Find insertion point (after last include)
lines = src.splitlines(keepends=True)
last_include_idx = -1
for idx, line in enumerate(lines):
if line.lstrip().startswith('#include '):
last_include_idx = idx
elif last_include_idx != -1 and line.strip() and not line.strip().startswith('#'):
break
insertion = (
f"\nstatic void *{mapped_addr} = NULL;\n"
f"static size_t {mapped_size} = 0;\n"
f"\n"
)
if last_include_idx == -1:
return insertion + src
insert_pos = 0
for i in range(last_include_idx + 1):
insert_pos += len(lines[i])
return src[:insert_pos] + insertion + src[insert_pos:]
def find_destroy_span(src: str, struct_name: str) -> Tuple[int, int, int]:
"""Find span of '<name>__destroy(' function and return (start, body_start, end)."""
needle = f"{struct_name}__destroy("
idx = src.find(needle)
if idx == -1:
raise ValueError("__destroy() function signature not found")
# Move left to include optional preceding 'static void' line break
start = idx
# Find the opening brace after the signature
brace_start = src.find('{', idx)
if brace_start == -1:
raise ValueError("Opening brace for __destroy not found")
# Balance braces to find end
depth = 0
i = brace_start
n = len(src)
while i < n:
ch = src[i]
if ch == '{':
depth += 1
elif ch == '}':
depth -= 1
if depth == 0:
end = i + 1
return start, brace_start + 1, end
i += 1
raise ValueError("Matching closing brace for __destroy not found")
def find_and_patch_elf_bytes_call(src: str, struct_name: str) -> str:
"""Find and patch the s->data = (void *)xxx__elf_bytes(&s->data_sz); line
to add error handling when s->data is NULL.
"""
# Check if error handling already exists
if "if (!s->data)" in src and "err = -errno" in src:
return src
# Pattern to match the assignment line
pattern = re.compile(
rf"s->data\s*=\s*\(void\s*\*\)\s*{re.escape(struct_name)}__elf_bytes\s*\(\s*&s->data_sz\s*\)\s*;",
re.MULTILINE
)
def replace_assignment(match):
# Get the full line with proper indentation
line = match.group(0)
# Extract indentation from the original line - use tabs for consistency
leading_whitespace = line[:len(line) - len(line.lstrip())]
# Create the replacement with error handling using tabs
# The leading_whitespace should be a tab character for proper indentation
replacement = (
f"{line}\n"
f"{leading_whitespace}\tif (!s->data) {{\n"
f"{leading_whitespace}\t\terr = -errno;\n"
f"{leading_whitespace}\t\tgoto err;\n"
f"{leading_whitespace}\t}}"
)
return replacement
# Apply the replacement
new_src = pattern.sub(replace_assignment, src)
return new_src
def patch_header(header_path: str, dry_run: bool = False, obj_dir: str | None = None) -> None:
if not header_path.endswith('.skel.h'):
raise ValueError("Input file must end with .skel.h")
with open(header_path, 'r', encoding='utf-8') as f:
src = f.read()
start, end, func_name = find_function_span(src)
# Infer the corresponding object file path and ensure it exists
if obj_dir:
stem = os.path.basename(header_path)[:-len('.skel.h')]
obj_path = os.path.join(obj_dir, stem + '.bpf.o')
else:
obj_path = header_path[:-len('.skel.h')] + '.bpf.o'
abs_obj_path = os.path.abspath(obj_path)
# Prepare new function body with mmap logic
new_func = make_replacement_body(func_name, abs_obj_path)
# Replace function implementation
new_src = src[:start] + new_func + src[end:]
# Patch __destroy to munmap if we added globals
struct_name = func_name.rsplit("__elf_bytes", 1)[0]
mapped_addr = f"{struct_name}__mapped_addr"
mapped_size = f"{struct_name}__mapped_size"
# Ensure globals are present near top includes
new_src = ensure_globals(new_src, mapped_addr, mapped_size)
try:
d_start, d_body_start, d_end = find_destroy_span(new_src, struct_name)
# Insert munmap logic at the beginning of the destroy body
munmap_code = (
"\n\tif ({mapped_addr}) {{\n"
"\t\tmunmap({mapped_addr}, {mapped_size});\n"
"\t\t{mapped_addr} = NULL;\n"
"\t\t{mapped_size} = 0;\n"
"\t}}\n"
).format(mapped_addr=mapped_addr, mapped_size=mapped_size)
new_src = new_src[:d_body_start] + munmap_code + new_src[d_body_start:]
except ValueError:
# If __destroy not found, proceed without patching it
pass
# Fallback: if munmap not present, try simple brace insertion based on function name
if "munmap(" not in new_src:
needle = f"{struct_name}__destroy("
pos = new_src.find(needle)
if pos != -1:
brace = new_src.find('{', pos)
if brace != -1:
munmap_code = (
"\n\tif ({mapped_addr}) {{\n"
"\t\tmunmap({mapped_addr}, {mapped_size});\n"
"\t\t{mapped_addr} = NULL;\n"
"\t\t{mapped_size} = 0;\n"
"\t}}\n"
).format(mapped_addr=mapped_addr, mapped_size=mapped_size)
insert_at = brace + 1
new_src = new_src[:insert_at] + munmap_code + new_src[insert_at:]
# Ensure required includes are present
new_src = ensure_includes(new_src)
# Add error handling for s->data assignment
new_src = find_and_patch_elf_bytes_call(new_src, struct_name)
if dry_run:
sys.stdout.write(new_src)
return
with open(header_path, 'w', encoding='utf-8') as f:
f.write(new_src)
def main() -> None:
parser = argparse.ArgumentParser(description="Patch xxx__elf_bytes in .skel.h to mmap xxx.bpf.o")
parser.add_argument('header', help='Path to the .skel.h file (absolute or relative)')
parser.add_argument('--dry-run', action='store_true', help='Print result to stdout without writing')
parser.add_argument('--obj-dir', dest='obj_dir', default=None,
help='Directory containing <stem>.bpf.o; overrides default alongside header')
args = parser.parse_args()
header_path = args.header
if not os.path.isabs(header_path):
header_path = os.path.abspath(header_path)
patch_header(header_path, dry_run=args.dry_run, obj_dir=args.obj_dir)
if __name__ == '__main__':
try:
main()
except Exception as e:
print(f"Error: {e}", file=sys.stderr)
sys.exit(1)