Debian Codegraph @

Tools/jit/_targets.py

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 """Target-specific code generation, parsing, and processing."""
2
3 import asyncio
4 import dataclasses
5 import hashlib
6 import json
7 import os
8 import pathlib
9 import re
10 import sys
11 import tempfile
12 import typing
13 import shlex
14
15 import _llvm
16 import _schema
17 import _stencils
18 import _writer
19
20 if sys.version_info < (3, 11):
21 raise RuntimeError("Building the JIT compiler requires Python 3.11 or newer!")
22
23 TOOLS_JIT_BUILD = pathlib.Path(__file__).resolve()
24 TOOLS_JIT = TOOLS_JIT_BUILD.parent
25 TOOLS = TOOLS_JIT.parent
26 CPYTHON = TOOLS.parent
27 EXTERNALS = CPYTHON / "externals"
28 PYTHON_EXECUTOR_CASES_C_H = CPYTHON / "Python" / "executor_cases.c.h"
29 TOOLS_JIT_TEMPLATE_C = TOOLS_JIT / "template.c"
30
31 ASYNCIO_RUNNER = asyncio.Runner()
32
33 _S = typing.TypeVar("_S", _schema.COFFSection, _schema.ELFSection, _schema.MachOSection)
34 _R = typing.TypeVar(
35 "_R", _schema.COFFRelocation, _schema.ELFRelocation, _schema.MachORelocation
36 )
37
38
39 @dataclasses.dataclass
40 class _Target(typing.Generic[_S, _R]):
41 triple: str
42 condition: str
43 _: dataclasses.KW_ONLY
44 alignment: int = 1
45 args: typing.Sequence[str] = ()
46 prefix: str = ""
47 stable: bool = False
48 debug: bool = False
49 verbose: bool = False
50 cflags: str = ""
51 known_symbols: dict[str, int] = dataclasses.field(default_factory=dict)
52 pyconfig_dir: pathlib.Path = pathlib.Path.cwd().resolve()
53
54 def _get_nop(self) -> bytes:
55 if re.fullmatch(r"aarch64-.*", self.triple):
56 nop = b"\x1f\x20\x03\xd5"
57 elif re.fullmatch(r"x86_64-.*|i686.*", self.triple):
58 nop = b"\x90"
59 else:
60 raise ValueError(f"NOP not defined for {self.triple}")
61 return nop
62
63 def _compute_digest(self) -> str:
64 hasher = hashlib.sha256()
65 hasher.update(self.triple.encode())
66 hasher.update(self.debug.to_bytes())
67 hasher.update(self.cflags.encode())
68 # These dependencies are also reflected in _JITSources in regen.targets:
69 hasher.update(PYTHON_EXECUTOR_CASES_C_H.read_bytes())
70 hasher.update((self.pyconfig_dir / "pyconfig.h").read_bytes())
71 for dirpath, _, filenames in sorted(os.walk(TOOLS_JIT)):
72 # Exclude cache files from digest computation to ensure reproducible builds.
73 if dirpath.endswith("__pycache__"):
74 continue
75 for filename in sorted(filenames):
76 hasher.update(pathlib.Path(dirpath, filename).read_bytes())
77 return hasher.hexdigest()
78
79 async def _parse(self, path: pathlib.Path) -> _stencils.StencilGroup:
80 group = _stencils.StencilGroup()
81 args = ["--disassemble", "--reloc", f"{path}"]
82 output = await _llvm.maybe_run("llvm-objdump", args, echo=self.verbose)
83 if output is not None:
84 # Make sure that full paths don't leak out (for reproducibility):
85 long, short = str(path), str(path.name)
86 group.code.disassembly.extend(
87 line.expandtabs().strip().replace(long, short)
88 for line in output.splitlines()
89 )
90 args = [
91 "--elf-output-style=JSON",
92 "--expand-relocs",
93 # "--pretty-print",
94 "--section-data",
95 "--section-relocations",
96 "--section-symbols",
97 "--sections",
98 f"{path}",
99 ]
100 output = await _llvm.run("llvm-readobj", args, echo=self.verbose)
101 # --elf-output-style=JSON is only *slightly* broken on Mach-O...
102 output = output.replace("PrivateExtern\n", "\n")
103 output = output.replace("Extern\n", "\n")
104 # ...and also COFF:
105 output = output[output.index("[", 1, None) :]
106 output = output[: output.rindex("]", None, -1) + 1]
107 sections: list[dict[typing.Literal["Section"], _S]] = json.loads(output)
108 for wrapped_section in sections:
109 self._handle_section(wrapped_section["Section"], group)
110 assert group.symbols["_JIT_ENTRY"] == (_stencils.HoleValue.CODE, 0)
111 if group.data.body:
112 line = f"0: {str(bytes(group.data.body)).removeprefix('b')}"
113 group.data.disassembly.append(line)
114 return group
115
116 def _handle_section(self, section: _S, group: _stencils.StencilGroup) -> None:
117 raise NotImplementedError(type(self))
118
119 def _handle_relocation(
120 self, base: int, relocation: _R, raw: bytearray
121 ) -> _stencils.Hole:
122 raise NotImplementedError(type(self))
123
124 async def _compile(
125 self, opname: str, c: pathlib.Path, tempdir: pathlib.Path
126 ) -> _stencils.StencilGroup:
127 o = tempdir / f"{opname}.o"
128 args = [
129 f"--target={self.triple}",
130 "-DPy_BUILD_CORE_MODULE",
131 "-D_DEBUG" if self.debug else "-DNDEBUG",
132 f"-D_JIT_OPCODE={opname}",
133 "-D_PyJIT_ACTIVE",
134 "-D_Py_JIT",
135 f"-I{self.pyconfig_dir}",
136 f"-I{CPYTHON / 'Include'}",
137 f"-I{CPYTHON / 'Include' / 'internal'}",
138 f"-I{CPYTHON / 'Include' / 'internal' / 'mimalloc'}",
139 f"-I{CPYTHON / 'Python'}",
140 f"-I{CPYTHON / 'Tools' / 'jit'}",
141 "-O3",
142 "-c",
143 # Shorten full absolute file paths in the generated code (like the
144 # __FILE__ macro and assert failure messages) for reproducibility:
145 f"-ffile-prefix-map={CPYTHON}=.",
146 f"-ffile-prefix-map={tempdir}=.",
147 # This debug info isn't necessary, and bloats out the JIT'ed code.
148 # We *may* be able to re-enable this, process it, and JIT it for a
149 # nicer debugging experience... but that needs a lot more research:
150 "-fno-asynchronous-unwind-tables",
151 # Don't call built-in functions that we can't find or patch:
152 "-fno-builtin",
153 # Emit relaxable 64-bit calls/jumps, so we don't have to worry about
154 # about emitting in-range trampolines for out-of-range targets.
155 # We can probably remove this and emit trampolines in the future:
156 "-fno-plt",
157 # Don't call stack-smashing canaries that we can't find or patch:
158 "-fno-stack-protector",
159 "-std=c11",
160 "-o",
161 f"{o}",
162 f"{c}",
163 *self.args,
164 # Allow user-provided CFLAGS to override any defaults
165 *shlex.split(self.cflags),
166 ]
167 await _llvm.run("clang", args, echo=self.verbose)
168 return await self._parse(o)
169
170 async def _build_stencils(self) -> dict[str, _stencils.StencilGroup]:
171 generated_cases = PYTHON_EXECUTOR_CASES_C_H.read_text()
172 cases_and_opnames = sorted(
173 re.findall(
174 r"\n {8}(case (\w+): \{\n.*?\n {8}\})", generated_cases, flags=re.DOTALL
175 )
176 )
177 tasks = []
178 with tempfile.TemporaryDirectory() as tempdir:
179 work = pathlib.Path(tempdir).resolve()
180 async with asyncio.TaskGroup() as group:
181 coro = self._compile("shim", TOOLS_JIT / "shim.c", work)
182 tasks.append(group.create_task(coro, name="shim"))
183 template = TOOLS_JIT_TEMPLATE_C.read_text()
184 for case, opname in cases_and_opnames:
185 # Write out a copy of the template with *only* this case
186 # inserted. This is about twice as fast as #include'ing all
187 # of executor_cases.c.h each time we compile (since the C
188 # compiler wastes a bunch of time parsing the dead code for
189 # all of the other cases):
190 c = work / f"{opname}.c"
191 c.write_text(template.replace("CASE", case))
192 coro = self._compile(opname, c, work)
193 tasks.append(group.create_task(coro, name=opname))
194 stencil_groups = {task.get_name(): task.result() for task in tasks}
195 for stencil_group in stencil_groups.values():
196 stencil_group.process_relocations(
197 known_symbols=self.known_symbols,
198 alignment=self.alignment,
199 nop=self._get_nop(),
200 )
201 return stencil_groups
202
203 def build(
204 self,
205 *,
206 comment: str = "",
207 force: bool = False,
208 jit_stencils: pathlib.Path,
209 ) -> None:
210 """Build jit_stencils.h in the given directory."""
211 jit_stencils.parent.mkdir(parents=True, exist_ok=True)
212 if not self.stable:
213 warning = f"JIT support for {self.triple} is still experimental!"
214 request = "Please report any issues you encounter.".center(len(warning))
215 outline = "=" * len(warning)
216 print("\n".join(["", outline, warning, request, outline, ""]))
217 digest = f"// {self._compute_digest()}\n"
218 if (
219 not force
220 and jit_stencils.exists()
221 and jit_stencils.read_text().startswith(digest)
222 ):
223 return
224 stencil_groups = ASYNCIO_RUNNER.run(self._build_stencils())
225 jit_stencils_new = jit_stencils.parent / "jit_stencils.h.new"
226 try:
227 with jit_stencils_new.open("w") as file:
228 file.write(digest)
229 if comment:
230 file.write(f"// {comment}\n")
231 file.write("\n")
232 for line in _writer.dump(stencil_groups, self.known_symbols):
233 file.write(f"{line}\n")
234 try:
235 jit_stencils_new.replace(jit_stencils)
236 except FileNotFoundError:
237 # another process probably already moved the file
238 if not jit_stencils.is_file():
239 raise
240 finally:
241 jit_stencils_new.unlink(missing_ok=True)
242
243
244 class _COFF(
245 _Target[_schema.COFFSection, _schema.COFFRelocation]
246 ): # pylint: disable = too-few-public-methods
247 def _handle_section(
248 self, section: _schema.COFFSection, group: _stencils.StencilGroup
249 ) -> None:
250 flags = {flag["Name"] for flag in section["Characteristics"]["Flags"]}
251 if "SectionData" in section:
252 section_data_bytes = section["SectionData"]["Bytes"]
253 else:
254 # Zeroed BSS data, seen with printf debugging calls:
255 section_data_bytes = [0] * section["RawDataSize"]
256 if "IMAGE_SCN_MEM_EXECUTE" in flags:
257 value = _stencils.HoleValue.CODE
258 stencil = group.code
259 elif "IMAGE_SCN_MEM_READ" in flags:
260 value = _stencils.HoleValue.DATA
261 stencil = group.data
262 else:
263 return
264 base = len(stencil.body)
265 group.symbols[section["Number"]] = value, base
266 stencil.body.extend(section_data_bytes)
267 for wrapped_symbol in section["Symbols"]:
268 symbol = wrapped_symbol["Symbol"]
269 offset = base + symbol["Value"]
270 name = symbol["Name"]
271 name = name.removeprefix(self.prefix)
272 if name not in group.symbols:
273 group.symbols[name] = value, offset
274 for wrapped_relocation in section["Relocations"]:
275 relocation = wrapped_relocation["Relocation"]
276 hole = self._handle_relocation(base, relocation, stencil.body)
277 stencil.holes.append(hole)
278
279 def _unwrap_dllimport(self, name: str) -> tuple[_stencils.HoleValue, str | None]:
280 if name.startswith("__imp_"):
281 name = name.removeprefix("__imp_")
282 name = name.removeprefix(self.prefix)
283 return _stencils.HoleValue.GOT, name
284 name = name.removeprefix(self.prefix)
285 return _stencils.symbol_to_value(name)
286
287 def _handle_relocation(
288 self, base: int, relocation: _schema.COFFRelocation, raw: bytearray
289 ) -> _stencils.Hole:
290 match relocation:
291 case {
292 "Offset": offset,
293 "Symbol": s,
294 "Type": {"Name": "IMAGE_REL_I386_DIR32" as kind},
295 }:
296 offset += base
297 value, symbol = self._unwrap_dllimport(s)
298 addend = int.from_bytes(raw[offset : offset + 4], "little")
299 case {
300 "Offset": offset,
301 "Symbol": s,
302 "Type": {
303 "Name": "IMAGE_REL_AMD64_REL32" | "IMAGE_REL_I386_REL32" as kind
304 },
305 }:
306 offset += base
307 value, symbol = self._unwrap_dllimport(s)
308 addend = (
309 int.from_bytes(raw[offset : offset + 4], "little", signed=True) - 4
310 )
311 case {
312 "Offset": offset,
313 "Symbol": s,
314 "Type": {
315 "Name": "IMAGE_REL_ARM64_BRANCH26"
316 | "IMAGE_REL_ARM64_PAGEBASE_REL21"
317 | "IMAGE_REL_ARM64_PAGEOFFSET_12A"
318 | "IMAGE_REL_ARM64_PAGEOFFSET_12L" as kind
319 },
320 }:
321 offset += base
322 value, symbol = self._unwrap_dllimport(s)
323 addend = 0
324 case _:
325 raise NotImplementedError(relocation)
326 return _stencils.Hole(offset, kind, value, symbol, addend)
327
328
329 class _ELF(
330 _Target[_schema.ELFSection, _schema.ELFRelocation]
331 ): # pylint: disable = too-few-public-methods
332 def _handle_section(
333 self, section: _schema.ELFSection, group: _stencils.StencilGroup
334 ) -> None:
335 section_type = section["Type"]["Name"]
336 flags = {flag["Name"] for flag in section["Flags"]["Flags"]}
337 if section_type == "SHT_RELA":
338 assert "SHF_INFO_LINK" in flags, flags
339 assert not section["Symbols"]
340 maybe_symbol = group.symbols.get(section["Info"])
341 if maybe_symbol is None:
342 # These are relocations for a section we're not emitting. Skip:
343 return
344 value, base = maybe_symbol
345 if value is _stencils.HoleValue.CODE:
346 stencil = group.code
347 else:
348 assert value is _stencils.HoleValue.DATA
349 stencil = group.data
350 for wrapped_relocation in section["Relocations"]:
351 relocation = wrapped_relocation["Relocation"]
352 hole = self._handle_relocation(base, relocation, stencil.body)
353 stencil.holes.append(hole)
354 elif section_type == "SHT_PROGBITS":
355 if "SHF_ALLOC" not in flags:
356 return
357 if "SHF_EXECINSTR" in flags:
358 value = _stencils.HoleValue.CODE
359 stencil = group.code
360 else:
361 value = _stencils.HoleValue.DATA
362 stencil = group.data
363 group.symbols[section["Index"]] = value, len(stencil.body)
364 for wrapped_symbol in section["Symbols"]:
365 symbol = wrapped_symbol["Symbol"]
366 offset = len(stencil.body) + symbol["Value"]
367 name = symbol["Name"]["Name"]
368 name = name.removeprefix(self.prefix)
369 group.symbols[name] = value, offset
370 stencil.body.extend(section["SectionData"]["Bytes"])
371 assert not section["Relocations"]
372 else:
373 assert section_type in {
374 "SHT_GROUP",
375 "SHT_LLVM_ADDRSIG",
376 "SHT_NOTE",
377 "SHT_NULL",
378 "SHT_STRTAB",
379 "SHT_SYMTAB",
380 }, section_type
381
382 def _handle_relocation(
383 self, base: int, relocation: _schema.ELFRelocation, raw: bytearray
384 ) -> _stencils.Hole:
385 symbol: str | None
386 match relocation:
387 case {
388 "Addend": addend,
389 "Offset": offset,
390 "Symbol": {"Name": s},
391 "Type": {
392 "Name": "R_AARCH64_ADR_GOT_PAGE"
393 | "R_AARCH64_LD64_GOT_LO12_NC"
394 | "R_X86_64_GOTPCREL"
395 | "R_X86_64_GOTPCRELX"
396 | "R_X86_64_REX_GOTPCRELX" as kind
397 },
398 }:
399 offset += base
400 s = s.removeprefix(self.prefix)
401 value, symbol = _stencils.HoleValue.GOT, s
402 case {
403 "Addend": addend,
404 "Offset": offset,
405 "Symbol": {"Name": s},
406 "Type": {"Name": kind},
407 }:
408 offset += base
409 s = s.removeprefix(self.prefix)
410 value, symbol = _stencils.symbol_to_value(s)
411 case _:
412 raise NotImplementedError(relocation)
413 return _stencils.Hole(offset, kind, value, symbol, addend)
414
415
416 class _MachO(
417 _Target[_schema.MachOSection, _schema.MachORelocation]
418 ): # pylint: disable = too-few-public-methods
419 def _handle_section(
420 self, section: _schema.MachOSection, group: _stencils.StencilGroup
421 ) -> None:
422 assert section["Address"] >= len(group.code.body)
423 assert "SectionData" in section
424 flags = {flag["Name"] for flag in section["Attributes"]["Flags"]}
425 name = section["Name"]["Value"]
426 name = name.removeprefix(self.prefix)
427 if "Debug" in flags:
428 return
429 if "SomeInstructions" in flags:
430 value = _stencils.HoleValue.CODE
431 stencil = group.code
432 start_address = 0
433 group.symbols[name] = value, section["Address"] - start_address
434 else:
435 value = _stencils.HoleValue.DATA
436 stencil = group.data
437 start_address = len(group.code.body)
438 group.symbols[name] = value, len(group.code.body)
439 base = section["Address"] - start_address
440 group.symbols[section["Index"]] = value, base
441 stencil.body.extend(
442 [0] * (section["Address"] - len(group.code.body) - len(group.data.body))
443 )
444 stencil.body.extend(section["SectionData"]["Bytes"])
445 assert "Symbols" in section
446 for wrapped_symbol in section["Symbols"]:
447 symbol = wrapped_symbol["Symbol"]
448 offset = symbol["Value"] - start_address
449 name = symbol["Name"]["Name"]
450 name = name.removeprefix(self.prefix)
451 group.symbols[name] = value, offset
452 assert "Relocations" in section
453 for wrapped_relocation in section["Relocations"]:
454 relocation = wrapped_relocation["Relocation"]
455 hole = self._handle_relocation(base, relocation, stencil.body)
456 stencil.holes.append(hole)
457
458 def _handle_relocation(
459 self, base: int, relocation: _schema.MachORelocation, raw: bytearray
460 ) -> _stencils.Hole:
461 symbol: str | None
462 match relocation:
463 case {
464 "Offset": offset,
465 "Symbol": {"Name": s},
466 "Type": {
467 "Name": "ARM64_RELOC_GOT_LOAD_PAGE21"
468 | "ARM64_RELOC_GOT_LOAD_PAGEOFF12" as kind
469 },
470 }:
471 offset += base
472 s = s.removeprefix(self.prefix)
473 value, symbol = _stencils.HoleValue.GOT, s
474 addend = 0
475 case {
476 "Offset": offset,
477 "Symbol": {"Name": s},
478 "Type": {"Name": "X86_64_RELOC_GOT" | "X86_64_RELOC_GOT_LOAD" as kind},
479 }:
480 offset += base
481 s = s.removeprefix(self.prefix)
482 value, symbol = _stencils.HoleValue.GOT, s
483 addend = (
484 int.from_bytes(raw[offset : offset + 4], "little", signed=True) - 4
485 )
486 case {
487 "Offset": offset,
488 "Section": {"Name": s},
489 "Type": {"Name": "X86_64_RELOC_SIGNED" as kind},
490 } | {
491 "Offset": offset,
492 "Symbol": {"Name": s},
493 "Type": {"Name": "X86_64_RELOC_BRANCH" | "X86_64_RELOC_SIGNED" as kind},
494 }:
495 offset += base
496 s = s.removeprefix(self.prefix)
497 value, symbol = _stencils.symbol_to_value(s)
498 addend = (
499 int.from_bytes(raw[offset : offset + 4], "little", signed=True) - 4
500 )
501 case {
502 "Offset": offset,
503 "Section": {"Name": s},
504 "Type": {"Name": kind},
505 } | {
506 "Offset": offset,
507 "Symbol": {"Name": s},
508 "Type": {"Name": kind},
509 }:
510 offset += base
511 s = s.removeprefix(self.prefix)
512 value, symbol = _stencils.symbol_to_value(s)
513 addend = 0
514 case _:
515 raise NotImplementedError(relocation)
516 return _stencils.Hole(offset, kind, value, symbol, addend)
517
518
519 def get_target(host: str) -> _COFF | _ELF | _MachO:
520 """Build a _Target for the given host "triple" and options."""
521 target: _COFF | _ELF | _MachO
522 if re.fullmatch(r"aarch64-apple-darwin.*", host):
523 condition = "defined(__aarch64__) && defined(__APPLE__)"
524 target = _MachO(host, condition, alignment=8, prefix="_")
525 elif re.fullmatch(r"aarch64-pc-windows-msvc", host):
526 args = ["-fms-runtime-lib=dll", "-fplt"]
527 condition = "defined(_M_ARM64)"
528 target = _COFF(host, condition, alignment=8, args=args)
529 elif re.fullmatch(r"aarch64-.*-linux-gnu", host):
530 args = [
531 "-fpic",
532 # On aarch64 Linux, intrinsics were being emitted and this flag
533 # was required to disable them.
534 "-mno-outline-atomics",
535 ]
536 condition = "defined(__aarch64__) && defined(__linux__)"
537 target = _ELF(host, condition, alignment=8, args=args)
538 elif re.fullmatch(r"i686-pc-windows-msvc", host):
539 args = [
540 "-DPy_NO_ENABLE_SHARED",
541 # __attribute__((preserve_none)) is not supported
542 "-Wno-ignored-attributes",
543 ]
544 condition = "defined(_M_IX86)"
545 target = _COFF(host, condition, args=args, prefix="_")
546 elif re.fullmatch(r"x86_64-apple-darwin.*", host):
547 condition = "defined(__x86_64__) && defined(__APPLE__)"
548 target = _MachO(host, condition, prefix="_")
549 elif re.fullmatch(r"x86_64-pc-windows-msvc", host):
550 args = ["-fms-runtime-lib=dll"]
551 condition = "defined(_M_X64)"
552 target = _COFF(host, condition, args=args)
553 elif re.fullmatch(r"x86_64-.*-linux-gnu", host):
554 args = ["-fno-pic", "-mcmodel=medium", "-mlarge-data-threshold=0"]
555 condition = "defined(__x86_64__) && defined(__linux__)"
556 target = _ELF(host, condition, args=args)
557 else:
558 raise ValueError(host)
559 return target