Skip to content

Commit

Permalink
Python memory snapshot: add magic number and version
Browse files Browse the repository at this point in the history
  • Loading branch information
hoodmane committed Mar 25, 2024
1 parent 7c33ab4 commit da5b4ce
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/pyodide/internal/python.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,20 +419,27 @@ function setUploadFunction(toUpload) {
};
}

// "\x00snp"
const SNAPSHOT_MAGIC = 0x6d736100;
const SNAPSHOT_VERSION = 1;

/**
* Encode heap and dsoJSON into the memory snapshot artifact that we'll upload
*/
function encodeSnapshot(heap, dsoJSON) {
const dsoString = JSON.stringify(dsoJSON);
let sx = 8 + 2 * dsoString.length;
let snapshotOffset = 8 + 2 * dsoString.length;
// align to 8 bytes
sx = Math.ceil(sx / 8) * 8;
const toUpload = new Uint8Array(sx + heap.length);
snapshotOffset = Math.ceil(snapshotOffset / 8) * 8;
const toUpload = new Uint8Array(snapshotOffset + heap.length);
const encoder = new TextEncoder();
const { written } = encoder.encodeInto(dsoString, toUpload.subarray(8));
const jsonLength = written;
const uint32View = new Uint32Array(toUpload.buffer);
uint32View[0] = sx;
uint32View[1] = written;
uint32View[0] = SNAPSHOT_MAGIC;
uint32View[1] = SNAPSHOT_VERSION;
uint32View[2] = snapshotOffset;
uint32View[3] = jsonLength;
toUpload.subarray(sx).set(heap);
return toUpload;
}
Expand All @@ -442,8 +449,13 @@ function encodeSnapshot(heap, dsoJSON) {
*/
function decodeSnapshot(memorySnapshot) {
const uint32View = new Uint32Array(memorySnapshot);
const snapshotOffset = uint32View[0];
const jsonLength = uint32View[1];
if (uint32View[0] !== SNAPSHOT_MAGIC) {
throw new Error("Memory snapshot doesn't start with snapshot magic number");
}
const snapshotVersion = uint32View[1];
// Currently we don't do anything with snapshotVersion.
const snapshotOffset = uint32View[2];
const jsonLength = uint32View[3];
const jsonView = new Uint8Array(memorySnapshot, 8, jsonLength);
DSO_METADATA = JSON.parse(new TextDecoder().decode(jsonView));
MEMORY = new Uint8Array(memorySnapshot, snapshotOffset);
Expand Down

0 comments on commit da5b4ce

Please sign in to comment.