feat(python): decode CPython 3.14 and 3.15 pyc code objects

Decode marshal slice constants from magic 3608 onward and pin the 3.11 code
layout and inline-cache tables for the 3.14 and 3.15 magics.

Refs #8206
This commit is contained in:
Abhishek Sharma authored and Ysopwnable committed 2026-11-14 13:37:55 +01:00
1 parent 5451d3cb2d
commit 1d08253ff2
14 files changed
+471 -69

No files matched your search

+2 -1
View File
@@ -27,7 +27,8 @@ const PYTHON_BYTECODE_IDENTITIES: ReadonlyMap<number, PythonBytecodeIdentity> =
...identities(range(3550, 3571), "3.13"),
...identities([3627], "3.14"),
// Reported against Python 3.15, the default `python3` on current Homebrew.
...identities([3666], "3.15"),
// 3663 is the 3.15.0a8 magic, read back from a real interpreter.
...identities([3663, 3666], "3.15"),
]);
export const pythonBytecodeIdentity = (
+16 -3
View File
@@ -76,17 +76,30 @@ it("keeps pinned CPython identity separate from standalone marshal coverage", ()
code_layout: "py311",
negative_code_fields: "rejected",
});
// Recognizing a newer CPython identity does not authorize its code layout.
expect(pythonBytecodeIdentity(3627)).toEqual({
pythonVersion: "3.14",
headerBytes: 16,
});
expect(pythonPycMarshalProfile(3627)).toEqual({
code_layout: "py311",
negative_code_fields: "rejected",
});
expect(pythonBytecodeIdentity(3666)).toEqual({
pythonVersion: "3.15",
headerBytes: 16,
});
expect(pythonPycMarshalProfile(3627)).toBeNull();
expect(pythonPycMarshalProfile(3666)).toBeNull();
expect(pythonBytecodeIdentity(3663)).toEqual({
pythonVersion: "3.15",
headerBytes: 16,
});
expect(pythonPycMarshalProfile(3663)?.code_layout).toBe("py311");
expect(pythonPycMarshalProfile(3666)?.code_layout).toBe("py311");
// Only magics checked against real CPython are pinned: the neighbouring
// 3.14 and 3.15 pre-release magics and the 3.16 magics stay unknown.
for (const magic of [3600, 3626, 3650, 3662, 3664, 3665, 3706]) {
expect(pythonBytecodeIdentity(magic), String(magic)).toBeNull();
expect(pythonPycMarshalProfile(magic), String(magic)).toBeNull();
}
expect(pythonPycMarshalProfile(3450)).toBeNull();
expect(pythonPycMarshalProfile(62171)).toBeNull();
expect(PYTHON_PYC_MARSHAL_PROFILES.size).toBeGreaterThan(80);
+11 -1
View File
@@ -8,6 +8,8 @@ import {
export const PYTHON_PYC_HEADER_BYTES = 16;
export const PYTHON_PYC_HASH_BASED = 0x1;
export const PYTHON_PYC_CHECK_SOURCE = 0x2;
/** CPython 3.14a1 magic 3608 added TYPE_SLICE to marshal (gh-125063). */
export const PYTHON_PYC_SLICE_MAGIC = 3608;
const uint = z.number().int().min(0).max(0xffffffff);
const index = z.number().int().nonnegative();
@@ -59,6 +61,13 @@ export const PYTHON_PYC_MARSHAL_PROFILES: ReadonlyMap<
...consecutiveProfiles(3494, 3495, "py311"),
...consecutiveProfiles(3500, 3531, "py311"),
...consecutiveProfiles(3550, 3571, "py311"),
// 3.14 and 3.15 keep the 3.11 code layout; 3.14a1 magic 3608 adds slice
// constants to marshal, which the reader gates by magic. Only magics checked
// against real CPython are pinned: 3627 (3.14.4) and 3663 (3.15.0a8) were
// read back from those interpreters; 3666 (3.15.2) was checked with real
// 3.15.2 release bytes decoded by the 3.15.0a8 interpreter. Other
// pre-release magics stay unpinned.
...profiles([3627, 3663, 3666], "py311"),
]);
export const pythonPycMarshalProfile = (
@@ -160,6 +169,7 @@ const pythonConstSchema = z.discriminatedUnion("kind", [
z.strictObject({ kind: z.literal("frozenset"), length: uint }),
z.strictObject({ kind: z.literal("dict"), length: uint }),
z.strictObject({ kind: z.literal("ellipsis") }),
z.strictObject({ kind: z.literal("slice") }),
z.strictObject({
kind: z.literal("complex"),
real: pythonFloatSchema,
@@ -253,7 +263,7 @@ export const pythonPycHeaderSchema = z
});
});
/** Bounded CPython 3.7–3.13 pyc observation; marshal is walked, bytecode is not decompiled. */
/** Bounded CPython 3.7–3.15 pyc observation; marshal is walked, bytecode is not decompiled. */
export const pythonPycSchema = z
.strictObject({
artifact: z.strictObject({