ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
hil_convergence_safety_runtime_loader_harness.py
Go to the documentation of this file.
1# SPDX-License-Identifier: MIT
2# Copyright (c) 2026 Brighton Sikarskie
3"""Private loader harness text and subprocess boundary."""
4
5from __future__ import annotations
6
7import subprocess
8import sys
9from pathlib import Path
10
11_HARNESS = (
12 "import errno, os, pathlib, sys\n"
13 "main_path, process_path, cases_path = map(pathlib.Path, sys.argv[1:])\n"
14 "main_source = main_path.read_text(encoding='utf-8')\n"
15 "cases_source = cases_path.read_text(encoding='utf-8')\n"
16 "globals()['__name__'] = '_ra8_supervisor_reexec'\n"
17 "globals()['__file__'] = str(main_path)\n"
18 "sys.modules['_ra8_supervisor_reexec'] = sys.modules['__main__']\n"
19 "exec(compile(main_source, str(main_path), 'exec'), globals())\n"
20 "process_name = '_ra8_supervisor_process'\n"
21 "sentinel = object()\n"
22 "sys.modules[process_name] = sentinel\n"
23 "descriptor = os.open(process_path, os.O_RDONLY | os.O_NOFOLLOW)\n"
24 "try:\n"
25 " globals()['_load_process_api'](descriptor)\n"
26 "except RuntimeError as error:\n"
27 " if str(error) != 'supervisor process module name is already occupied':\n"
28 " raise SystemExit(7) from error\n"
29 "else:\n"
30 " raise SystemExit(8)\n"
31 "try:\n"
32 " os.fstat(descriptor)\n"
33 "except OSError as error:\n"
34 " if error.errno != errno.EBADF:\n"
35 " raise SystemExit(11) from error\n"
36 "else:\n"
37 " raise SystemExit(12)\n"
38 "if sys.modules.get(process_name) is not sentinel:\n"
39 " raise SystemExit(9)\n"
40 "del sys.modules[process_name]\n"
41 "for _attempt in range(2):\n"
42 " descriptor = os.open(process_path, os.O_RDONLY | os.O_NOFOLLOW)\n"
43 " api = globals()['_load_process_api'](descriptor)\n"
44 " try:\n"
45 " os.fstat(descriptor)\n"
46 " except OSError as error:\n"
47 " if error.errno != errno.EBADF:\n"
48 " raise SystemExit(13) from error\n"
49 " else:\n"
50 " raise SystemExit(14)\n"
51 " if process_name in sys.modules:\n"
52 " raise SystemExit(10)\n"
53 " globals()['_install_process_api'](api)\n"
54 "namespace = {'__name__': '_ra8_supervisor_cases', "
55 "'__file__': str(cases_path), '_RA8_SUPERVISOR_CASES_VERSION': 1}\n"
56 "compiled = compile(cases_source, str(cases_path), 'exec')\n"
57 "exec(compiled, namespace)\n"
58 "grant = namespace.pop('_RA8_SUPERVISOR_CASES_VERSION', None)\n"
59 "if grant != 1 or '_RA8_SUPERVISOR_CASES_VERSION' in namespace:\n"
60 " raise SystemExit(3)\n"
61 "dispatch = namespace.get('dispatch_supervisor_cases')\n"
62 "if not callable(dispatch) or dispatch.__globals__ is not namespace:\n"
63 " raise SystemExit(2)\n"
64 "try:\n"
65 " exec(compiled, dispatch.__globals__)\n"
66 "except RuntimeError as error:\n"
67 " if str(error) != 'supervisor cases module is source-only':\n"
68 " raise SystemExit(4) from error\n"
69 "else:\n"
70 " raise SystemExit(5)\n"
71 "if '_RA8_SUPERVISOR_CASES_VERSION' in namespace:\n"
72 " raise SystemExit(6)\n"
73)
74
75
76def run(
77 main_path: Path, process_path: Path, cases_path: Path, timeout: float
78) -> subprocess.CompletedProcess[bytes]:
79 """Execute the fixed private-API loader harness once."""
80 return subprocess.run( # noqa: S603 -- current interpreter and fixed private test sources
81 (
82 sys.executable,
83 "-B",
84 "-I",
85 "-S",
86 "-c",
87 _HARNESS,
88 str(main_path),
89 str(process_path),
90 str(cases_path),
91 ),
92 capture_output=True,
93 timeout=timeout,
94 check=False,
95 )