|
1 | 1 | #!/usr/bin/env python3 |
2 | | -"""Fail closed if a native/generic include root reaches a source/Pine header. |
| 2 | +"""Guard native source boundaries, member definitions and sync portability. |
3 | 3 |
|
4 | 4 | This is the inexpensive textual companion to check_native_include_independence: |
5 | 5 | the latter proves the installed closure and object symbols after a build; this |
6 | 6 | guard runs in preflight and rejects a forbidden include before compilation. |
7 | 7 | """ |
8 | 8 | from __future__ import annotations |
9 | 9 |
|
| 10 | +from bisect import bisect_right |
10 | 11 | from contextlib import redirect_stderr |
11 | 12 | import io |
12 | 13 | from pathlib import Path |
@@ -123,6 +124,13 @@ def blank(start: int, end: int) -> None: |
123 | 124 | blank(index, end) |
124 | 125 | index = end |
125 | 126 | continue |
| 127 | + # C++17 digit separators are not opening character-literal quotes. |
| 128 | + if (text[index].isdigit() |
| 129 | + and (index == 0 or not re.match(r"\w", text[index - 1]))): |
| 130 | + index += 1 |
| 131 | + while index < length and (text[index].isalnum() or text[index] in "_.'"): |
| 132 | + index += 1 |
| 133 | + continue |
126 | 134 | if text[index] in {'"', "'"}: |
127 | 135 | quote = text[index] |
128 | 136 | end = index + 1 |
@@ -298,7 +306,209 @@ def run_scan(pattern: re.Pattern[str], paths: list[Path], label: str, |
298 | 306 | return 0 |
299 | 307 |
|
300 | 308 |
|
| 309 | +def _apple_condition(expression: str) -> set[bool]: |
| 310 | + """Possible truth values on Apple; unsupported syntax stays unknown.""" |
| 311 | + tokens = re.findall(r"[A-Za-z_]\w*|\d+|&&|\|\||[!()]|\S", expression) |
| 312 | + position = 0 |
| 313 | + |
| 314 | + def take() -> str: |
| 315 | + nonlocal position |
| 316 | + if position == len(tokens): |
| 317 | + raise ValueError("incomplete condition") |
| 318 | + token = tokens[position] |
| 319 | + position += 1 |
| 320 | + return token |
| 321 | + |
| 322 | + def parse(level: int = 0) -> set[bool]: |
| 323 | + nonlocal position |
| 324 | + if level < 2: |
| 325 | + left = parse(level + 1) |
| 326 | + operator = ("||", "&&")[level] |
| 327 | + while position < len(tokens) and tokens[position] == operator: |
| 328 | + position += 1 |
| 329 | + right = parse(level + 1) |
| 330 | + left = {(a or b) if level == 0 else (a and b) |
| 331 | + for a in left for b in right} |
| 332 | + return left |
| 333 | + token = take() |
| 334 | + if token == "!": |
| 335 | + return {not value for value in parse(2)} |
| 336 | + if token == "(": |
| 337 | + value = parse() |
| 338 | + if take() != ")": |
| 339 | + raise ValueError("unclosed condition") |
| 340 | + return value |
| 341 | + if token == "defined": |
| 342 | + token = take() |
| 343 | + if token == "(": |
| 344 | + token = take() |
| 345 | + if take() != ")": |
| 346 | + raise ValueError("unclosed defined") |
| 347 | + if not re.fullmatch(r"[A-Za-z_]\w*", token): |
| 348 | + raise ValueError("invalid defined") |
| 349 | + if token in {"__APPLE__", "1"}: |
| 350 | + return {True} |
| 351 | + if token in {"__linux__", "0"}: |
| 352 | + return {False} |
| 353 | + return {False, True} |
| 354 | + |
| 355 | + try: |
| 356 | + result = parse() |
| 357 | + return result if position == len(tokens) else {False, True} |
| 358 | + except ValueError: |
| 359 | + return {False, True} |
| 360 | + |
| 361 | + |
| 362 | +def _apple_fdatasync(text: str) -> tuple[int, list[int]]: |
| 363 | + """Count lexical calls and locate those not provably excluded on Apple.""" |
| 364 | + # Splice before masking, as C++ does, including continued // comments. |
| 365 | + # Keep enough offset information to report physical source line numbers. |
| 366 | + splices = [match.start() - 2 * index for index, match in |
| 367 | + enumerate(re.finditer(r"\\\n", text))] |
| 368 | + masked = _mask_cpp(text.replace("\\\n", "")) |
| 369 | + calls: dict[int, list[int]] = {} |
| 370 | + count = 0 |
| 371 | + for match in re.finditer(r"::\s*fdatasync\s*\(", masked): |
| 372 | + logical_line = masked.count("\n", 0, match.start()) + 1 |
| 373 | + physical_offset = match.start() + 2 * bisect_right(splices, match.start()) |
| 374 | + calls.setdefault(logical_line, []).append( |
| 375 | + text.count("\n", 0, physical_offset) + 1) |
| 376 | + count += 1 |
| 377 | + if not count: |
| 378 | + return 0, [] |
| 379 | + |
| 380 | + # Each frame holds the parent's reachability, whether a later arm may |
| 381 | + # still be selected, and whether #else has already appeared. |
| 382 | + stack: list[tuple[bool, bool, bool]] = [] |
| 383 | + reachable = True |
| 384 | + exposed: list[int] = [] |
| 385 | + for line_number, line in enumerate(masked.splitlines(), 1): |
| 386 | + directive = re.match(r"^\s*#\s*(if|ifdef|ifndef|elif|else|endif)\b(.*)", line) |
| 387 | + if directive: |
| 388 | + kind, expression = directive.groups() |
| 389 | + if kind in {"if", "ifdef", "ifndef"}: |
| 390 | + if kind != "if": |
| 391 | + expression = ("!" if kind == "ifndef" else "") + ( |
| 392 | + "defined(" + expression.strip() + ")") |
| 393 | + condition = _apple_condition(expression) |
| 394 | + stack.append((reachable, False in condition, False)) |
| 395 | + reachable = reachable and True in condition |
| 396 | + else: |
| 397 | + if not stack or (kind != "endif" and stack[-1][2]): |
| 398 | + raise ValueError(f"line {line_number}: unexpected #{kind}") |
| 399 | + parent, remaining, _ = stack[-1] |
| 400 | + if kind == "endif": |
| 401 | + reachable = parent |
| 402 | + stack.pop() |
| 403 | + else: |
| 404 | + condition = _apple_condition(expression) if kind == "elif" else {True} |
| 405 | + reachable = parent and remaining and True in condition |
| 406 | + stack[-1] = (parent, remaining and False in condition, kind == "else") |
| 407 | + if reachable: |
| 408 | + exposed.extend(calls.get(line_number, [])) |
| 409 | + if stack: |
| 410 | + raise ValueError("unterminated preprocessor conditional") |
| 411 | + return count, exposed |
| 412 | + |
| 413 | + |
| 414 | +def run_portability_scan(root: Path) -> int: |
| 415 | + """Read every C/C++ source/header in both roots; fail on incomplete input.""" |
| 416 | + suffixes = {".c", ".cc", ".cpp", ".cxx", ".h", ".hh", ".hpp", ".hxx", ".inc", ".inl"} |
| 417 | + findings: list[str] = [] |
| 418 | + calls = 0 |
| 419 | + path = root |
| 420 | + try: |
| 421 | + for relative in ("src", "include"): |
| 422 | + path = root / relative |
| 423 | + if not path.is_dir(): |
| 424 | + raise OSError("missing source directory") |
| 425 | + pending = [path] |
| 426 | + files = 0 |
| 427 | + while pending: |
| 428 | + path = pending.pop() |
| 429 | + if path.is_symlink(): |
| 430 | + raise OSError("refusing symbolic link in source tree") |
| 431 | + if path.is_dir(): |
| 432 | + pending.extend(sorted(path.iterdir(), reverse=True)) |
| 433 | + continue |
| 434 | + if path.suffix.lower() not in suffixes: |
| 435 | + continue |
| 436 | + text = path.read_text(encoding="utf-8") |
| 437 | + files += 1 |
| 438 | + count, exposed = _apple_fdatasync(text) |
| 439 | + calls += count |
| 440 | + findings.extend( |
| 441 | + f"{path}:{line}: ::fdatasync call may be compiled on Apple; " |
| 442 | + "exclude Apple and use its portable sync branch\n" |
| 443 | + for line in exposed) |
| 444 | + if not files: |
| 445 | + raise OSError("no C/C++ source/header inputs") |
| 446 | + if not calls: |
| 447 | + raise ValueError("no ::fdatasync calls scanned") |
| 448 | + except (OSError, UnicodeError, ValueError) as error: |
| 449 | + print(f"native source guard: portability scan failed: {path}: {error}", |
| 450 | + file=sys.stderr) |
| 451 | + return 1 |
| 452 | + if findings: |
| 453 | + print("native source guard: Apple-exposed ::fdatasync call(s) found", |
| 454 | + file=sys.stderr) |
| 455 | + sys.stderr.writelines(findings) |
| 456 | + return 1 |
| 457 | + return 0 |
| 458 | + |
| 459 | + |
| 460 | +def portability_self_test() -> int: |
| 461 | + # Independent discriminators exercise the same scanner as the real tree; |
| 462 | + # no production path, call count or line number is an acceptance whitelist. |
| 463 | + cases = ( |
| 464 | + ("unguarded", "::fdatasync(fd);\n", 1, [1]), |
| 465 | + ("Apple else", "#if defined(__APPLE__)\n::fsync(fd);\n" |
| 466 | + "#else\n::fdatasync(fd);\n#endif\n", 1, []), |
| 467 | + ("Linux else", "#if defined(__linux__)\n::fdatasync(fd);\n" |
| 468 | + "#else\n::fsync(fd);\n#endif\n", 1, []), |
| 469 | + ("unrelated Apple arm", "#ifndef __APPLE__\n::fsync(fd);\n#endif\n" |
| 470 | + "::fdatasync(fd);\n", 1, [4]), |
| 471 | + ("Apple positive", "#if defined(__APPLE__)\n::fdatasync(fd);\n#endif\n", 1, [2]), |
| 472 | + ("wrong nested arm", "#ifndef __APPLE__\n::fsync(fd);\n#else\n" |
| 473 | + "#if FEATURE\n::fdatasync(fd);\n#endif\n#endif\n", 1, [5]), |
| 474 | + ("outer exclusion", "#ifndef __APPLE__\n#if FEATURE\n" |
| 475 | + "::fdatasync(fd);\n#endif\n#endif\n", 1, []), |
| 476 | + ("elif exclusion", "#if FEATURE\n::fsync(fd);\n" |
| 477 | + "#elif defined(__linux__)\n::fdatasync(fd);\n" |
| 478 | + "#elif defined(__APPLE__)\n::fsync(fd);\n#endif\n", 1, []), |
| 479 | + ("elif Apple", "#if defined(__linux__)\n::fsync(fd);\n" |
| 480 | + "#elif defined(__APPLE__)\n::fdatasync(fd);\n#endif\n", 1, [4]), |
| 481 | + ("unknown else", "#if FEATURE\n::fsync(fd);\n#else\n" |
| 482 | + "::fdatasync(fd);\n#endif\n", 1, [4]), |
| 483 | + ("boolean exclusion", "#if !defined(__APPLE__) && FEATURE\n" |
| 484 | + "::fdatasync(fd);\n#endif\n", 1, []), |
| 485 | + ("boolean exposure", "#if !defined(__APPLE__) || FEATURE\n" |
| 486 | + "::fdatasync(fd);\n#endif\n", 1, [2]), |
| 487 | + ("unsupported condition", "#if __APPLE__ == 1\n::fdatasync(fd);\n#endif\n", 1, [2]), |
| 488 | + ("comments and strings", "// #ifndef __APPLE__\n/* __APPLE__ */\n" |
| 489 | + 'const char* marker = "__APPLE__";\n::fdatasync(fd);\n', 1, [4]), |
| 490 | + ("masked calls", "// ::fdatasync(fd);\n/* ::fdatasync(fd); */\n" |
| 491 | + 'const char* text = "::fdatasync(fd);";\n' |
| 492 | + 'const char* raw = R"tag(::fdatasync(fd);)tag";\n', 0, []), |
| 493 | + ("continued condition", "#if !defined(\\\n__APPLE__)\n" |
| 494 | + "::fdatasync(fd);\n#endif\n", 1, []), |
| 495 | + ("continued comment", "// ignored \\\n#ifndef __APPLE__\n" |
| 496 | + "::fdatasync(fd);\n", 1, [3]), |
| 497 | + ("split call", "::\nfdatasync\n(fd);\n", 1, [1]), |
| 498 | + ("digit separator", "auto size = 1'024;\n::fdatasync(fd);\n", 1, [2]), |
| 499 | + ) |
| 500 | + for label, text, expected_count, expected in cases: |
| 501 | + count, exposed = _apple_fdatasync(text) |
| 502 | + if (count, exposed) != (expected_count, expected): |
| 503 | + print(f"native source guard: portability self-test failed: {label}: " |
| 504 | + f"{(count, exposed)!r} != {(expected_count, expected)!r}", file=sys.stderr) |
| 505 | + return 1 |
| 506 | + return 0 |
| 507 | + |
| 508 | + |
301 | 509 | def self_test() -> int: |
| 510 | + if portability_self_test(): |
| 511 | + return 1 |
302 | 512 | with tempfile.TemporaryDirectory(prefix="pf-native-source-guard-") as temporary: |
303 | 513 | fixture = Path(temporary) / "include/pineforge/engine.hpp" |
304 | 514 | fixture.parent.mkdir(parents=True) |
@@ -363,6 +573,8 @@ def self_test() -> int: |
363 | 573 | def main() -> int: |
364 | 574 | if self_test(): |
365 | 575 | return 1 |
| 576 | + if run_portability_scan(ROOT): |
| 577 | + return 1 |
366 | 578 | roots = native_roots() |
367 | 579 | if not all(path.is_file() for path in roots): |
368 | 580 | missing = [str(path) for path in roots if not path.is_file()] |
|
0 commit comments