Skip to content

Commit 4e5554c

Browse files
committed
Optimistically strip writable data
1 parent 202fb6f commit 4e5554c

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

Tools/jit/_stencils.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,16 @@ class HoleValue(enum.Enum):
1919
CODE = enum.auto()
2020
# The base address of the read-only data for this uop:
2121
DATA = enum.auto()
22+
# The base address of the machine code for the error jump target (exposed as _JIT_ERROR_TARGET):
23+
ERROR_TARGET = enum.auto()
2224
# The address of the current executor (exposed as _JIT_EXECUTOR):
2325
EXECUTOR = enum.auto()
2426
# The base address of the "global" offset table located in the read-only data.
2527
# Shouldn't be present in the final stencils, since these are all replaced with
2628
# equivalent DATA values:
2729
GOT = enum.auto()
30+
# The base address of the machine code for the jump target (exposed as _JIT_JUMP_TARGET):
31+
JUMP_TARGET = enum.auto()
2832
# The current uop's oparg (exposed as _JIT_OPARG):
2933
OPARG = enum.auto()
3034
# The current uop's operand0 on 64-bit platforms (exposed as _JIT_OPERAND0):
@@ -39,10 +43,9 @@ class HoleValue(enum.Enum):
3943
OPERAND1_LO = enum.auto()
4044
# The current uop's target (exposed as _JIT_TARGET):
4145
TARGET = enum.auto()
42-
# The base address of the machine code for the jump target (exposed as _JIT_JUMP_TARGET):
43-
JUMP_TARGET = enum.auto()
44-
# The base address of the machine code for the error jump target (exposed as _JIT_ERROR_TARGET):
45-
ERROR_TARGET = enum.auto()
46+
# Writable data, which we don't support! Optimistically remove their data
47+
# from the stencil, and raise later if they're actually used:
48+
WRITABLE = enum.auto()
4649
# A hardcoded value of zero (used for symbol lookups):
4750
ZERO = enum.auto()
4851

@@ -96,9 +99,11 @@ class HoleValue(enum.Enum):
9699
_HOLE_EXPRS = {
97100
HoleValue.CODE: "(uintptr_t)code",
98101
HoleValue.DATA: "(uintptr_t)data",
102+
HoleValue.ERROR_TARGET: "state->instruction_starts[instruction->error_target]",
99103
HoleValue.EXECUTOR: "(uintptr_t)executor",
100104
# These should all have been turned into DATA values by process_relocations:
101105
# HoleValue.GOT: "",
106+
HoleValue.JUMP_TARGET: "state->instruction_starts[instruction->jump_target]",
102107
HoleValue.OPARG: "instruction->oparg",
103108
HoleValue.OPERAND0: "instruction->operand0",
104109
HoleValue.OPERAND0_HI: "(instruction->operand0 >> 32)",
@@ -107,8 +112,8 @@ class HoleValue(enum.Enum):
107112
HoleValue.OPERAND1_HI: "(instruction->operand1 >> 32)",
108113
HoleValue.OPERAND1_LO: "(instruction->operand1 & UINT32_MAX)",
109114
HoleValue.TARGET: "instruction->target",
110-
HoleValue.JUMP_TARGET: "state->instruction_starts[instruction->jump_target]",
111-
HoleValue.ERROR_TARGET: "state->instruction_starts[instruction->error_target]",
115+
# These should all have raised an error if they were actually used:
116+
# HoleValue.WRITABLE: "",
112117
HoleValue.ZERO: "",
113118
}
114119

@@ -246,6 +251,12 @@ def process_relocations(self, known_symbols: dict[str, int]) -> None:
246251
self.data.pad(8)
247252
for stencil in [self.code, self.data]:
248253
for hole in stencil.holes:
254+
if hole.symbol in self.symbols:
255+
value, _ = self.symbols[hole.symbol]
256+
if value is HoleValue.WRITABLE:
257+
raise ValueError(
258+
f"Writable data ({hole.symbol}) is not supported!"
259+
)
249260
if hole.value is HoleValue.GOT:
250261
assert hole.symbol is not None
251262
hole.value = HoleValue.DATA

Tools/jit/_targets.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,8 @@ def _handle_section(
268268
if "SectionData" in section:
269269
section_data_bytes = section["SectionData"]["Bytes"]
270270
else:
271-
# Zeroed BSS data, seen with printf debugging calls:
271+
# Zeroed BSS data:
272272
section_data_bytes = [0] * section["RawDataSize"]
273-
# XXX
274-
assert section["RawDataSize"] == 0, (group.symbols, section["Symbols"])
275273
if "IMAGE_SCN_MEM_EXECUTE" in flags:
276274
value = _stencils.HoleValue.CODE
277275
stencil = group.code
@@ -280,6 +278,10 @@ def _handle_section(
280278
stencil = group.data
281279
else:
282280
return
281+
if "IMAGE_SCN_MEM_WRITE" in flags:
282+
assert value is _stencils.HoleValue.DATA
283+
value = _stencils.HoleValue.WRITABLE
284+
section_data_bytes = []
283285
base = len(stencil.body)
284286
group.symbols[section["Number"]] = value, base
285287
stencil.body.extend(section_data_bytes)
@@ -382,7 +384,7 @@ def _handle_section(
382384
if value is _stencils.HoleValue.CODE:
383385
stencil = group.code
384386
else:
385-
assert value is _stencils.HoleValue.DATA
387+
assert value in (_stencils.HoleValue.DATA, _stencils.HoleValue.WRITABLE)
386388
stencil = group.data
387389
for wrapped_relocation in section["Relocations"]:
388390
relocation = wrapped_relocation["Relocation"]
@@ -397,14 +399,19 @@ def _handle_section(
397399
else:
398400
value = _stencils.HoleValue.DATA
399401
stencil = group.data
402+
section_data_bytes = section["SectionData"]["Bytes"]
403+
if "SHF_WRITE" in flags:
404+
assert value is _stencils.HoleValue.DATA
405+
value = _stencils.HoleValue.WRITABLE
406+
section_data_bytes = []
400407
group.symbols[section["Index"]] = value, len(stencil.body)
401408
for wrapped_symbol in section["Symbols"]:
402409
symbol = wrapped_symbol["Symbol"]
403410
offset = len(stencil.body) + symbol["Value"]
404411
name = symbol["Name"]["Name"]
405412
name = name.removeprefix(self.symbol_prefix)
406413
group.symbols[name] = value, offset
407-
stencil.body.extend(section["SectionData"]["Bytes"])
414+
stencil.body.extend(section_data_bytes)
408415
assert not section["Relocations"]
409416
else:
410417
assert section_type in {

0 commit comments

Comments
 (0)