Skip to content

Commit

Permalink
Fix universal snapshots again
Browse files Browse the repository at this point in the history
  • Loading branch information
hoodmane committed Mar 28, 2024
1 parent f112045 commit 27bf3b1
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/pyodide/internal/python.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ function loadDynlib(Module, path, wasmModuleData) {
}
}

// used for checkLoadedSoFiles a snapshot sanity check
const PRELOADED_SO_FILES = [];

/**
* This loads all dynamic libraries visible in the site-packages directory. They
* are loaded before the runtime is initialized outside of the heap, using the
Expand All @@ -189,8 +192,8 @@ function loadDynlib(Module, path, wasmModuleData) {
*/
function preloadDynamicLibs(Module) {
let SO_FILES_TO_LOAD = SITE_PACKAGES_SO_FILES;
if (DSO_METADATA?.settings?.baselineSnapshot) {
SO_FILES_TO_LOAD = [[ '_lzma.so' ]];
if (IS_CREATING_BASELINE_SNAPSHOT || DSO_METADATA?.settings?.baselineSnapshot) {
SO_FILES_TO_LOAD = [[ '_lzma.so' ], [ '_ssl.so' ]];
}
try {
const sitePackages = getSitePackagesPath(Module);
Expand All @@ -203,6 +206,7 @@ function preloadDynamicLibs(Module) {
const wasmModuleData = new Uint8Array(size);
TarReader.read(contentsOffset, wasmModuleData);
const path = sitePackages + "/" + soFile.join("/");
PRELOADED_SO_FILES.push(path);
loadDynlib(Module, path, wasmModuleData);
}
} catch (e) {
Expand Down Expand Up @@ -389,6 +393,21 @@ function memorySnapshotDoImports(Module) {
simpleRunPython(Module, processScriptImportsString);
}

function checkLoadedSoFiles(dsoJSON) {
PRELOADED_SO_FILES.sort();
const keys = Object.keys(dsoJSON).filter(k => k.startsWith("/"));
keys.sort();
const msg = `Internal error taking snapshot: mismatch: ${JSON.stringify(keys)} vs ${JSON.stringify(PRELOADED_SO_FILES)}`;
if (keys.length !== PRELOADED_SO_FILES.length) {
throw new Error(msg);
}
for(let i = 0; i < keys.length; i++) {
if (PRELOADED_SO_FILES[i] !== keys[i]) {
throw new Error(msg);
}
}
}

/**
* Create memory snapshot by importing SNAPSHOT_IMPORTS to ensure these packages
* are initialized in the linear memory snapshot and then saving a copy of the
Expand All @@ -397,6 +416,7 @@ function memorySnapshotDoImports(Module) {
function makeLinearMemorySnapshot(Module) {
memorySnapshotDoImports(Module);
const dsoJSON = recordDsoHandles(Module);
checkLoadedSoFiles(dsoJSON);
return encodeSnapshot(Module.HEAP8, dsoJSON);
}

Expand Down

0 comments on commit 27bf3b1

Please sign in to comment.