Skip to content

Commit c01de04

Browse files
authored
Python: Fail loading dynamic library if table base doesn't match (#5349)
If this happens, we will segfault if we ever try to use this dynamic library. Save ourselves some debugging pain by crashing early.
1 parent 9f36a62 commit c01de04

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/pyodide/internal/snapshot.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,12 +228,22 @@ function getMemoryPatched(
228228
const libName = libPath.split('/').at(-1)!;
229229
// 1. Is it loaded in the snapshot? Replay the memory base.
230230
{
231-
const { soMemoryBases } = LOADED_SNAPSHOT_META ?? {};
231+
const { soMemoryBases, soTableBases } = LOADED_SNAPSHOT_META ?? {};
232232
// If we loaded this library before taking the snapshot, we already allocated the memory and the
233233
// allocator remembers because its state is in the linear memory. We just have to look it up.
234-
const base = soMemoryBases?.[libPath] ?? soMemoryBases?.[libName];
235-
if (base) {
236-
return base;
234+
const tableBase = Module.wasmTable.length;
235+
const expectedTableBase =
236+
soTableBases?.[libPath] ?? soTableBases?.[libName];
237+
if (expectedTableBase && tableBase !== expectedTableBase) {
238+
// If this happens, we will segfault if we ever try to use this dynamic library.
239+
// Save ourselves some debugging pain by crashing early.
240+
throw new PythonRuntimeError(
241+
`Error loading ${libName}: Expected table base ${expectedTableBase} but got table base ${tableBase}`
242+
);
243+
}
244+
const memoryBase = soMemoryBases?.[libPath] ?? soMemoryBases?.[libName];
245+
if (memoryBase) {
246+
return memoryBase;
237247
}
238248
}
239249
// 2. It's not loaded in the snapshot. Record

0 commit comments

Comments
 (0)