Debian Codegraph @

Tools/jit/_targets.py

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