-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
773 lines (640 loc) · 23.3 KB
/
util.py
File metadata and controls
773 lines (640 loc) · 23.3 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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
import os
import random
import re
import textwrap
from bisect import bisect_right
from functools import lru_cache
import clang.cindex
import config
INTEGER_LITERAL_RE = re.compile(
r"^(?P<body>(?:0[xX][0-9a-fA-F']+)|(?:0[bB][01']+)|(?:0[0-7']*)|(?:[1-9][0-9']*|0))(?P<suffix>(?:[uU](?:ll|LL|l|L)?|(?:ll|LL|l|L)[uU]?|z[uU]?|[uU]z)?)$"
)
FLOAT_LITERAL_RE = re.compile(
r"^(?P<body>(?:(?:\d+\.\d*|\.\d+|\d+[eE][+-]?\d+|\d+\.\d*[eE][+-]?\d+|\.\d+[eE][+-]?\d+)))(?P<suffix>[fFlL]?)$"
)
IDENTIFIER_RE = re.compile(r"[A-Za-z_]\w*(?:::\w+)*(?:<[^;=(){}]+>)?")
CPP_KEYWORDS = {
"alignas", "alignof", "asm", "auto", "bool", "break", "case", "catch", "char",
"class", "const", "constexpr", "const_cast", "continue", "decltype", "default",
"delete", "do", "double", "else", "enum", "explicit", "export", "extern",
"false", "float", "for", "friend", "goto", "if", "inline", "int", "long",
"mutable", "namespace", "new", "noexcept", "nullptr", "operator", "private",
"protected", "public", "register", "reinterpret_cast", "return", "short",
"signed", "sizeof", "static", "static_assert", "static_cast", "struct", "switch",
"template", "this", "throw", "true", "try", "typedef", "typeid", "typename",
"union", "unsigned", "using", "virtual", "void", "volatile", "while",
}
FUNCTION_HEADER_SCAN_WINDOW = 300
FUNCTION_LIKE_SKIP_NAMES = {"if", "for", "while", "switch", "catch"}
VM_SKIP_MARKER = "/*__VM_SKIP__*/"
CHERRY_SKIP_MARKER = "/*__CHERRY_SKIP__*/"
def vlog(tag: str, message: str) -> None:
"""Verbose logger controlled by config.VERBOSE_LOGGING."""
if getattr(config, "VERBOSE_LOGGING", False):
print(f"[{tag}] {message}")
def generate_barcode_name(length=16):
"""Generates a unique identifier composed of 'l', 'I', and '1'."""
prefix = random.choice(["l", "I"])
return prefix + "".join(random.choices(["l", "I", "1"], k=length - 1))
@lru_cache(maxsize=None)
def normalize_path(path):
return os.path.realpath(path)
def is_local(node, target_realpath):
"""Checks if the AST node belongs to the target source file."""
if not node.location.file:
return False
return normalize_path(node.location.file.name) == target_realpath
def build_name_map(node, target_realpath, name_map):
"""Scans the AST for user-defined declarations to rename."""
is_translation_unit = node.kind == clang.cindex.CursorKind.TRANSLATION_UNIT
local_node = is_local(node, target_realpath)
if not is_translation_unit and not local_node:
return
if local_node and node.kind.is_declaration() and node.spelling and node.spelling != "main":
if node.spelling not in name_map:
name_map[node.spelling] = generate_barcode_name()
for child in node.get_children():
build_name_map(child, target_realpath, name_map)
def parse_integer_literal(value_str):
"""Returns parsed integer literal metadata for safe mutations."""
match = INTEGER_LITERAL_RE.fullmatch(value_str)
if not match:
return None
body = match.group("body")
suffix = match.group("suffix")
normalized_body = body.replace("'", "")
if normalized_body.startswith(("0x", "0X")):
base = 16
elif normalized_body.startswith(("0b", "0B")):
base = 2
elif normalized_body.startswith("0") and normalized_body != "0":
base = 8
else:
base = 10
return {
"body": body,
"suffix": suffix,
"value": int(normalized_body, base),
}
def parse_floating_literal(value_str):
"""Returns parsed floating literal metadata for safe mutations."""
match = FLOAT_LITERAL_RE.fullmatch(value_str)
if not match:
return None
body = match.group("body")
suffix = match.group("suffix")
try:
value = float(body)
except ValueError:
return None
if suffix in {"f", "F"}:
cast_type = "float"
elif suffix in {"l", "L"}:
cast_type = "long double"
else:
cast_type = "double"
return {
"body": body,
"suffix": suffix,
"value": value,
"cast_type": cast_type,
}
def format_literal(value, suffix):
return f"{value}{suffix}"
def is_escaped(text, index):
backslashes = 0
cursor = index - 1
while cursor >= 0 and text[cursor] == "\\":
backslashes += 1
cursor -= 1
return backslashes % 2 == 1
def get_constant_mutation(value_str, runtime_wrapper=None):
"""Generates safe bitwise identities for integer constants."""
literal = parse_integer_literal(value_str)
if literal is None:
return value_str
source = value_str
suffix = literal["suffix"]
value = literal["value"]
key_min = 1
key_max = max(0xFF, min(max(value, 1) * 2, 0xFFFF))
key1 = format_literal(random.randint(key_min, key_max), suffix)
key2 = format_literal(random.randint(key_min, key_max), suffix)
strategies = [
f"(({source} ^ {key1}) ^ {key1})",
f"(({source} & {key1}) | ({source} & ~{key1}))",
]
if value == 0:
strategies.append(f"({key1} ^ {key1})")
strategies.append(f"({key2} & ~{key2})")
elif value == 1:
odd_key = format_literal(random.randrange(1, 0xFFFF, 2), suffix)
strategies.append(f"({odd_key} & {format_literal(1, suffix)})")
result = random.choice(strategies)
if random.random() > 0.5:
extra_key = format_literal(random.randint(1, 0xFF), suffix)
result = f"(({result} ^ {extra_key}) ^ {extra_key})"
if config.ENABLE_RUNTIME_CONSTANT_OBFUSCATION and runtime_wrapper is not None:
return runtime_wrapper(result)
return result
def get_floating_constant_mutation(value_str):
"""Replaces floating literals with lookup-table based runtime helpers."""
literal = parse_floating_literal(value_str)
if literal is None or not getattr(config, "ENABLE_FLOATING_CONSTANT_ENCODING", False):
return value_str
import state
state.init_floating_constant_names()
for entry in state.FLOAT_CONSTANT_ENTRIES:
if entry["source"] == value_str:
ticket = entry["ticket"]
break
else:
ticket = random.randint(0x1000, 0x7FFFFFFF)
state.FLOAT_CONSTANT_ENTRIES.append(
{
"source": value_str,
"ticket": ticket,
"value": literal["value"],
"cast_type": literal["cast_type"],
}
)
helper_expr = f"{state.FLOAT_CONSTANT_HELPER_NAME}({format_literal(ticket, '')}U)"
if literal["cast_type"] == "double":
return helper_expr
return f"static_cast<{literal['cast_type']}>({helper_expr})"
def strip_comments(source_text):
"""Removes line and block comments while preserving line structure."""
if not config.ENABLE_COMMENT_STRIPPING:
return source_text
result = []
index = 0
in_string = False
in_char = False
in_line_comment = False
in_block_comment = False
while index < len(source_text):
char = source_text[index]
next_char = source_text[index + 1] if index + 1 < len(source_text) else ""
if in_line_comment:
if char == "\n":
result.append("\n")
in_line_comment = False
index += 1
continue
if in_block_comment:
if char == "\n":
result.append("\n")
if char == "*" and next_char == "/":
in_block_comment = False
index += 2
else:
index += 1
continue
if in_string:
result.append(char)
if char == '"' and not is_escaped(source_text, index):
in_string = False
index += 1
continue
if in_char:
result.append(char)
if char == "'" and not is_escaped(source_text, index):
in_char = False
index += 1
continue
if char == "/" and next_char == "/":
in_line_comment = True
index += 2
continue
if char == "/" and next_char == "*":
in_block_comment = True
index += 2
continue
if char == '"':
in_string = True
elif char == "'":
in_char = True
result.append(char)
index += 1
return "".join(result)
def replace_keywords_with_macros(source_text, keyword_to_macro):
"""Rewrites keyword tokens outside strings/comments/preprocessor lines."""
result = []
index = 0
line_start = True
in_string = False
in_char = False
in_line_comment = False
in_block_comment = False
in_preprocessor = False
while index < len(source_text):
char = source_text[index]
next_char = source_text[index + 1] if index + 1 < len(source_text) else ""
if in_line_comment:
result.append(char)
if char == "\n":
in_line_comment = False
line_start = True
in_preprocessor = False
index += 1
continue
if in_block_comment:
result.append(char)
if char == "*" and next_char == "/":
result.append(next_char)
in_block_comment = False
index += 2
else:
index += 1
continue
if in_string:
result.append(char)
if char == '"' and not is_escaped(source_text, index):
in_string = False
if char == "\n":
line_start = True
index += 1
continue
if in_char:
result.append(char)
if char == "'" and not is_escaped(source_text, index):
in_char = False
if char == "\n":
line_start = True
index += 1
continue
if line_start:
whitespace_end = index
while whitespace_end < len(source_text) and source_text[whitespace_end] in " \t":
whitespace_end += 1
if whitespace_end < len(source_text) and source_text[whitespace_end] == "#":
in_preprocessor = True
if char == "/" and next_char == "/":
result.append(char)
result.append(next_char)
in_line_comment = True
index += 2
continue
if char == "/" and next_char == "*":
result.append(char)
result.append(next_char)
in_block_comment = True
index += 2
continue
if char == '"':
result.append(char)
in_string = True
index += 1
line_start = False
continue
if char == "'":
result.append(char)
in_char = True
index += 1
line_start = False
continue
if not in_preprocessor and (char.isalpha() or char == "_"):
start = index
index += 1
while index < len(source_text) and (source_text[index].isalnum() or source_text[index] == "_"):
index += 1
token = source_text[start:index]
result.append(keyword_to_macro.get(token, token))
line_start = False
continue
result.append(char)
if char == "\n":
line_start = True
in_preprocessor = False
elif char not in " \t":
line_start = False
index += 1
return "".join(result)
def split_top_level_statements(body_text):
"""Splits a function body into top-level statements."""
statements = []
current = []
paren_depth = 0
bracket_depth = 0
brace_depth = 0
in_string = False
in_char = False
in_line_comment = False
in_block_comment = False
for index, char in enumerate(body_text):
next_char = body_text[index + 1] if index + 1 < len(body_text) else ""
current.append(char)
if in_line_comment:
if char == "\n":
in_line_comment = False
continue
if in_block_comment:
if char == "*" and next_char == "/":
current.append(next_char)
in_block_comment = False
continue
if in_string:
if char == '"' and not is_escaped(body_text, index):
in_string = False
continue
if in_char:
if char == "'" and not is_escaped(body_text, index):
in_char = False
continue
if char == "/" and next_char == "/":
in_line_comment = True
continue
if char == "/" and next_char == "*":
in_block_comment = True
continue
if char == '"':
in_string = True
continue
if char == "'":
in_char = True
continue
if char == "(":
paren_depth += 1
elif char == ")":
paren_depth = max(0, paren_depth - 1)
elif char == "[":
bracket_depth += 1
elif char == "]":
bracket_depth = max(0, bracket_depth - 1)
elif char == "{":
brace_depth += 1
elif char == "}":
brace_depth = max(0, brace_depth - 1)
if paren_depth == 0 and bracket_depth == 0 and brace_depth == 0:
stripped = "".join(current).strip()
if char == ";" or (char == "}" and stripped and stripped != "}"):
statements.append("".join(current).strip())
current = []
trailing = "".join(current).strip()
if trailing:
statements.append(trailing)
return statements
def looks_like_declaration(statement):
stripped = statement.strip()
if not stripped.endswith(";"):
return False
forbidden_prefixes = (
"return", "break", "continue", "goto", "throw", "if", "for", "while",
"switch", "case", "default", "else", "do", "try", "catch", "asm",
"__asm__", "static_assert",
)
if re.match(rf"^(?:{'|'.join(re.escape(prefix) for prefix in forbidden_prefixes)})\b", stripped):
return False
head = stripped[:-1].strip()
if not head:
return False
if head.startswith(("typedef ", "using ", "enum ", "struct ", "class ")):
return True
if "=" in head:
prefix = head.split("=", 1)[0].strip()
else:
prefix = head
identifiers = IDENTIFIER_RE.findall(prefix)
if len(identifiers) >= 2:
return True
type_keywords = {
"const", "constexpr", "static", "volatile", "unsigned", "signed", "short",
"long", "int", "char", "float", "double", "bool", "auto", "size_t",
"ssize_t", "typename", "mutable", "register",
}
return any(keyword in prefix.split() for keyword in type_keywords)
def indent_block(text, indent):
lines = textwrap.dedent(text).strip().splitlines()
if not lines:
return [indent.rstrip()]
return [indent + line.rstrip() for line in lines]
def find_matching_brace(source_text, brace_index):
depth = 1
cursor = brace_index + 1
in_string = False
in_char = False
in_line_comment = False
in_block_comment = False
while cursor < len(source_text) and depth > 0:
char = source_text[cursor]
next_char = source_text[cursor + 1] if cursor + 1 < len(source_text) else ""
if in_line_comment:
if char == "\n":
in_line_comment = False
cursor += 1
continue
if in_block_comment:
if char == "*" and next_char == "/":
in_block_comment = False
cursor += 2
continue
cursor += 1
continue
if in_string:
if char == '"' and not is_escaped(source_text, cursor):
in_string = False
cursor += 1
continue
if in_char:
if char == "'" and not is_escaped(source_text, cursor):
in_char = False
cursor += 1
continue
if char == "/" and next_char == "/":
in_line_comment = True
cursor += 2
continue
if char == "/" and next_char == "*":
in_block_comment = True
cursor += 2
continue
if char == '"':
in_string = True
elif char == "'":
in_char = True
elif char == "{":
depth += 1
elif char == "}":
depth -= 1
cursor += 1
return cursor if depth == 0 else None
def extract_function_like_metadata(source_text, brace_index):
header = source_text[max(0, brace_index - FUNCTION_HEADER_SCAN_WINDOW):brace_index]
stripped_header = header.rstrip()
if not stripped_header.endswith(")"):
return None
open_paren_index = stripped_header.rfind("(")
if open_paren_index == -1:
return None
prefix = stripped_header[:open_paren_index].rstrip()
name_match = re.search(r"([A-Za-z_]\w*)\s*$", prefix)
if not name_match:
return None
name = name_match.group(1)
if name in FUNCTION_LIKE_SKIP_NAMES:
return None
if prefix.endswith(("namespace", "class", "struct", "enum")) or "template" in prefix:
return None
return {
"name": name,
"prefix": prefix,
"params_text": stripped_header[open_paren_index + 1:],
"open_paren_index": open_paren_index,
"header_text": stripped_header,
}
def iter_function_definitions(source_text):
scan_index = 0
while scan_index < len(source_text):
brace_index = source_text.find("{", scan_index)
if brace_index == -1:
break
metadata = extract_function_like_metadata(source_text, brace_index)
if metadata is None:
scan_index = brace_index + 1
continue
end_index = find_matching_brace(source_text, brace_index)
if end_index is None:
break
line_start = source_text.rfind("\n", 0, brace_index) + 1
base_indent = re.match(r"\s*", source_text[line_start:brace_index]).group(0)
body_text = source_text[brace_index + 1:end_index - 1]
skip_structural = (
has_vm_skip_marker(source_text, line_start)
or CHERRY_SKIP_MARKER in body_text[:512]
or is_performance_sensitive_function(
metadata["header_text"],
body_text,
)
)
yield {
"brace_index": brace_index,
"end_index": end_index,
"body_text": body_text,
"base_indent": base_indent,
"skip_structural": skip_structural,
**metadata,
}
scan_index = end_index
def has_vm_skip_marker(source_text, line_start):
marker_index = source_text.rfind(VM_SKIP_MARKER, 0, line_start)
if marker_index == -1:
return False
# The VM skip marker is emitted immediately ahead of the wrapped function
# signature. If a prior function already closed before this line, the marker
# belongs to that previous function and should not suppress new transforms.
closing_brace_index = source_text.find("}", marker_index, line_start)
return closing_brace_index == -1
def is_performance_sensitive_function(header_text, body_text):
combined = f"{header_text}\n{body_text}"
stl_search_tokens = (
"upper_bound(",
"lower_bound(",
"binary_search(",
"equal_range(",
"std::upper_bound(",
"std::lower_bound(",
"std::binary_search(",
"std::equal_range(",
)
if any(token in combined for token in stl_search_tokens):
return True
# Passing STL containers by value is already expensive; avoid piling on
# structural transforms that amplify the cost in query-heavy code.
container_by_value_patterns = (
r"\b(?:std::)?vector\s*<[^>]+>\s+[A-Za-z_]\w*",
r"\b(?:std::)?deque\s*<[^>]+>\s+[A-Za-z_]\w*",
r"\b(?:std::)?string\s+[A-Za-z_]\w*",
)
return any(re.search(pattern, header_text) for pattern in container_by_value_patterns)
def find_vm_protected_regions(source_text):
regions = []
search_index = 0
while True:
marker_index = source_text.find(VM_SKIP_MARKER, search_index)
if marker_index == -1:
break
brace_index = source_text.find("{", marker_index)
if brace_index == -1:
break
end_index = find_matching_brace(source_text, brace_index)
if end_index is None:
break
regions.append((marker_index, end_index))
search_index = end_index
return regions
def split_parameter_list(params_text):
parts = []
current = []
angle_depth = 0
paren_depth = 0
bracket_depth = 0
for char in params_text:
if char == "<":
angle_depth += 1
elif char == ">":
angle_depth = max(0, angle_depth - 1)
elif char == "(":
paren_depth += 1
elif char == ")":
paren_depth = max(0, paren_depth - 1)
elif char == "[":
bracket_depth += 1
elif char == "]":
bracket_depth = max(0, bracket_depth - 1)
if char == "," and angle_depth == 0 and paren_depth == 0 and bracket_depth == 0:
parts.append("".join(current).strip())
current = []
continue
current.append(char)
trailing = "".join(current).strip()
if trailing:
parts.append(trailing)
return parts
def extract_parameter_name(param_text):
normalized = param_text.strip()
if not normalized or normalized in {"void", "..."}:
return None
normalized = re.sub(r"\s*=\s*.*$", "", normalized)
match = re.search(r"([A-Za-z_]\w*)\s*(?:\[[^\]]*\])?\s*$", normalized)
if not match:
return None
candidate = match.group(1)
return None if candidate in CPP_KEYWORDS else candidate
def extract_declared_local_name(statement):
stripped = statement.strip()
if not stripped.endswith(";") or "," in stripped:
return None
head = stripped[:-1].strip()
prefix = head.split("=", 1)[0].strip()
identifiers = IDENTIFIER_RE.findall(prefix)
if len(identifiers) < 2:
return None
candidate = identifiers[-1]
return None if candidate in CPP_KEYWORDS else candidate
def replace_identifier_text(source_text, old_name, new_name):
return re.sub(rf"\b{re.escape(old_name)}\b", new_name, source_text)
def apply_name_map_to_fragment(fragment, name_map):
rewritten = fragment
for original_name, obfuscated_name in sorted(name_map.items(), key=lambda item: len(item[0]), reverse=True):
rewritten = replace_identifier_text(rewritten, original_name, obfuscated_name)
return rewritten
def build_protected_range_index(ranges):
if not ranges:
return [], []
merged = []
for start, end in sorted(ranges):
if not merged or start > merged[-1][1]:
merged.append([start, end])
else:
merged[-1][1] = max(merged[-1][1], end)
intervals = [(start, end) for start, end in merged]
starts = [start for start, _ in intervals]
return intervals, starts
def is_in_protected_range(token_start, token_end, protected_ranges, protected_range_starts):
if not protected_ranges:
return False
index = bisect_right(protected_range_starts, token_start) - 1
if index < 0:
return False
start, end = protected_ranges[index]
return start <= token_start and token_end <= end