Skip to content

Commit d65b622

Browse files
author
rocky
committed
Add graal_readSparseArray() and add more tests.
1 parent 8616eb9 commit d65b622

File tree

9 files changed

+53
-38
lines changed

9 files changed

+53
-38
lines changed
997 Bytes
Binary file not shown.
2.12 KB
Binary file not shown.
971 Bytes
Binary file not shown.
2.01 KB
Binary file not shown.
972 Bytes
Binary file not shown.
2.02 KB
Binary file not shown.

xdis/bin/pydisasm.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import os
1010
import os.path as osp
1111
import sys
12-
from typing import List
1312

1413
import click
1514

xdis/load.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,15 +310,21 @@ def load_module_from_file_object(
310310
source_size = unpack("<I", fp.read(4))[0] # size mod 2**32
311311

312312
if get_code:
313-
is_graal = magic_int in GRAAL3_MAGICS
314313
# Graal uses the same magic int for separate major/minor releases!
315-
graal_weirdness_check = not is_graal or PYTHON_VERSION_TRIPLE == version
314+
# So we can't get the major minor number using just a magic check.
315+
# Instead we'd also need to seek for a "graal number" typically
316+
# lower number like 25, 29, or 85. to distinguish.
317+
# Furthermore, runnin unmarshal in graal on graal, can cause the
318+
# the graal python interpreter to crash!
319+
# For these reasons, we are better off using our marshal routines
320+
# for Graal Python.
321+
is_graal = magic_int in GRAAL3_MAGICS
316322
if save_file_offsets and not is_graal:
317323
co, file_offsets = xdis.unmarshal.load_code_and_get_file_offsets(
318324
fp, magic_int, code_objects
319325
)
320326

321-
elif my_magic_int == magic_int and graal_weirdness_check:
327+
elif my_magic_int == magic_int and not is_graal:
322328
bytecode = fp.read()
323329
co = marshal.loads(bytecode)
324330
# Python 3.10 returns a tuple here?

xdis/unmarshal.py

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -354,12 +354,21 @@ def graal_readStringArray(self) -> tuple[str, ...]:
354354
Python equvalent of Python Graal's readObjectArray() from
355355
MarshalModuleBuiltins.java
356356
"""
357-
length: int = int(unpack("<i", self.fp.read(4))[0])
357+
length: int = self.graal_readInt()
358358
return tuple([self.graal_readString() for _ in range(length)])
359359

360-
def graal_readSparseTable(self):
361-
""" """
362-
pass
360+
def graal_readSparseTable(self) -> Dict[int, tuple]:
361+
"""
362+
Python equvalent of Python Graal's readObjectArray() from
363+
MarshalModuleBuiltins.java
364+
"""
365+
self.graal_readInt() # the length return value isn't used.
366+
table = {} # new int[length][];
367+
while True:
368+
i = self.graal_readInt()
369+
if i == -1:
370+
return table
371+
table[i] = self.graal_readIntArray()
363372

364373
def load(self):
365374
"""
@@ -876,6 +885,8 @@ def t_code_graal(self, save_ref, bytes_for_s: bool = False):
876885
else:
877886
code = self.t_graal_CodeUnit(save_ref=False, bytes_for_s=False)
878887
assert self.graal_code_info["co_flags"] == code.co_flags
888+
889+
# FIXME: add an assert self.fp.tell() has advanced to save_position?
879890
self.fp.seek(saved_position)
880891

881892
self.code_to_file_offsets[code] = (
@@ -995,20 +1006,20 @@ def t_graal_CodeUnit(self, save_ref, bytes_for_s: bool = False):
9951006
code. In particular, instructions are JVM bytecode.
9961007
"""
9971008

998-
# This is Java code for how a CodeUnit (type "U") is dumped
999-
# writeByte(Compiler.BYTECODE_VERSION);
1000-
# writeString(code.name);
1001-
# writeString(code.qualname);
1002-
# writeInt(code.argCount);
1003-
# writeInt(code.kwOnlyArgCount);
1004-
# writeInt(code.positionalOnlyArgCount);
1005-
# writeInt(code.stacksize);
1006-
# writeBytes(code.code);
1007-
# writeBytes(code.srcOffsetTable);
1008-
# writeInt(code.flags);
1009-
10101009
graal_bytecode_version = self.graal_readByte()
10111010
assert (21000 + graal_bytecode_version * 10) in GRAAL3_MAGICS
1011+
1012+
# This is Java code for how a CodeUnit (type "U") is read
1013+
# TruffleString name = readString();
1014+
# TruffleString qualname = readString();
1015+
# int argCount = readInt();
1016+
# int kwOnlyArgCount = readInt();
1017+
# int positionalOnlyArgCount = readInt();
1018+
# int stacksize = readInt();
1019+
# byte[] code = readBytes();
1020+
# byte[] srcOffsetTable = readBytes();
1021+
# int flags = readInt();
1022+
10121023
co_name = self.graal_readString()
10131024
co_qualname = self.graal_readString()
10141025
co_argcount = self.graal_readInt()
@@ -1031,12 +1042,11 @@ def t_graal_CodeUnit(self, save_ref, bytes_for_s: bool = False):
10311042
co_cellvars = self.graal_readStringArray()
10321043
co_freevars = self.graal_readStringArray()
10331044

1034-
# if (code.cell2arg != null) {
1035-
# writeIntArray(code.cell2arg);
1036-
# } else {
1037-
# writeIntArray(PythonUtils.EMPTY_INT_ARRAY);
1038-
# }
1039-
# writeObjectArray(code.constants);
1045+
# int[] cell2arg = readIntArray();
1046+
# if (cell2arg.length == 0) {
1047+
# cell2arg = null;
1048+
# }
1049+
# Object[] constants = readObjectArray();
10401050

10411051
cell2arg = self.graal_readIntArray()
10421052
co_consts = self.graal_readObjectArray()
@@ -1069,23 +1079,23 @@ def t_graal_CodeUnit(self, save_ref, bytes_for_s: bool = False):
10691079
[self.graal_code_info["co_codeunit_position"], co_code_offset_in_file]
10701080
)
10711081

1072-
# writeLongArray(code.primitiveConstants);
1073-
# writeIntArray(code.exceptionHandlerRanges);
1074-
# writeInt(code.conditionProfileCount);
1075-
# writeInt(code.startLine);
1076-
# writeInt(code.startColumn);
1077-
# writeInt(code.endLine);
1078-
# writeInt(code.endColumn);
1079-
# writeBytes(code.outputCanQuicken);
1080-
# writeBytes(code.variableShouldUnbox);
1081-
# writeSparseTable(code.generalizeInputsMap);
1082-
# writeSparseTable(code.generalizeVarsMap);
1083-
10841082
# The data from the below is not used, but we run the
10851083
# the extraction to keep the self.fp location where it should for
10861084
# the situation that we have code objects inside the codes' co_consts
10871085
# table marshaled as an ObjectArray.
10881086

1087+
# long[] primitiveConstants = readLongArray();
1088+
# int[] exceptionHandlerRanges = readIntArray();
1089+
# int conditionProfileCount = readInt();
1090+
# int startLine = readInt();
1091+
# int startColumn = readInt();
1092+
# int endLine = readInt();
1093+
# int endColumn = readInt();
1094+
# byte[] outputCanQuicken = readBytes();
1095+
# byte[] variableShouldUnbox = readBytes();
1096+
# int[][] generalizeInputsMap = readSparseTable();
1097+
# int[][] generalizeVarsMap = readSparseTable();
1098+
10891099
primitive_constants = self.graal_readLongArray()
10901100
exception_handler_ranges = self.graal_readIntArray()
10911101
condition_profileCount = self.graal_readInt()

0 commit comments

Comments
 (0)