{"record":{"id":"8cc9e90a9e83f831","repo":"dotnet/runtime","slug":"posix-memalign-failed-for-webcil-payload","errorCode":null,"errorMessage":"posix_memalign failed for Webcil payload","messagePattern":"posix_memalign failed for Webcil payload","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"critical","filePath":"src/native/libs/Common/JavaScript/host/assets.ts","lineNumber":98,"sourceCode":"    }\n}\n\nasync function checkWebcilResponse(webcilPromise: Promise<Response>, virtualPath: string): Promise<Response> {\n    const res = await webcilPromise;\n    if (!res || res.ok === false) {\n        throw new Error(`Failed to load Webcil module '${virtualPath}'. HTTP status: ${(res as Response)?.status} ${(res as Response)?.statusText}`);\n    }\n    return res;\n}\n\n// Allocates a 16-byte-aligned buffer for the Webcil payload. The pointer is heap memory that\n// outlives the stack frame, so it can be passed as the imageBase import.\nfunction allocWebcilPayload(payloadSize: number): number {\n    const sp = _ems_.stackSave();\n    try {\n        const ptrPtr = _ems_.stackAlloc(sizeOfPtr);\n        if (_ems_._posix_memalign(ptrPtr as any, 16, payloadSize)) {\n            throw new Error(\"posix_memalign failed for Webcil payload\");\n        }\n        return _ems_.HEAPU32[ptrPtr as any >>> 2];\n    } finally {\n        _ems_.stackRestore(sp);\n    }\n}\n\n// Builds the `webcil` import object. For R2R images (tableSize > 0) the module imports the runtime's\n// stack pointer, exception tag, indirect-call table and base globals; this also grows the table.\n// These import names and the webcilVersion/getWebcilPayload/fillWebcilTable handshake in\n// finishWebcilInstance are the R2R Webcil-in-Wasm host ABI defined by crossgen's WasmObjectWriter\n// (src/coreclr/tools/Common/Compiler/ObjectWriter/WasmObjectWriter.cs, CreateDefaultGlobalImports/\n// WriteExports). Keep in sync with the corerun host\n// (src/coreclr/hosts/corerun/wasm/libCorerun.js, BrowserHost_ExternalAssemblyProbe). Unlike corerun,\n// which parses data segment 0 for payloadSize/tableSize, this loader receives them from boot config.\nfunction buildWebcilImports(memory: WebAssembly.Memory, payloadPtr: number, tableSize: number): Record<string, WebAssembly.ImportValue> {\n    const webcilImports: Record<string, WebAssembly.ImportValue> = { memory };\n    if (tableSize > 0) {","sourceCodeStart":80,"sourceCodeEnd":116,"githubUrl":"https://github.com/dotnet/runtime/blob/60108ba66eb7d1d12f595480091b4ad80a24b172/src/native/libs/Common/JavaScript/host/assets.ts#L80-L116","documentation":"Thrown by allocWebcilPayload() (src/native/libs/Common/JavaScript/host/assets.ts:93) when _posix_memalign fails to allocate the 16-byte-aligned buffer that backs a Webcil image. This buffer holds the image payload passed as the imageBase import and must outlive the instantiate call, so it is heap-allocated rather than stack-allocated.","triggerScenarios":"instantiateWebcilModule computes payloadSize from the boot config and calls allocWebcilPayload(payloadSize); posix_memalign returns non-zero because the wasm heap cannot accommodate payloadSize bytes (would exceed maximum memory).","commonSituations":"An R2R/large webcil assembly larger than remaining memory headroom; a runtime whose maximum memory is too small; many concurrent webcil instantiations exhausting the heap.","solutions":["Increase the runtime's maximum WebAssembly.Memory so the heap can hold the largest webcil image plus the rest of the working set.","Reduce the size or number of R2R webcil assemblies loaded concurrently (lazy-load large assemblies).","If it appears after earlier successes, look for a heap memory leak in the host."],"exampleFix":"// before: default memory cap, large R2R assembly -> posix_memalign failed for Webcil payload\n\n// after\n{ \"memory\": { \"initial\": 1, \"maximum\": 4294 } }","handlingStrategy":"try-catch","validationCode":"// approximate headroom before instantiating a large webcil image\nfunction hasRoomFor(payloadSize: number): boolean {\n  const max = 2 * 1024 * 1024 * 1024; // typical wasm memory ceiling\n  const used = (globalThis as any).Module?.HEAP8?.buffer.byteLength ?? 0;\n  return used + payloadSize < max;\n}","typeGuard":null,"tryCatchPattern":"try {\n  await instantiateWebcilModule(webcilPromise, memory, virtualPath, tableSize, payloadSize);\n} catch (err) {\n  if (/posix_memalign failed for Webcil payload/.test(String((err as Error).message))) {\n    // raise maximum memory or lazy-load the oversized R2R assembly\n  }\n  throw err;\n}","preventionTips":["Raise the runtime maximum memory to fit the largest R2R webcil image.","Lazy-load large R2R assemblies instead of eager-loading them all.","Cap concurrent webcil instantiations."],"tags":["memory","webcil","native"],"backgroundTag":null,"analyzedSha":"60108ba66eb7d1d12f595480091b4ad80a24b172","analyzedAt":"2026-08-10T18:54:11.478Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}