{"record":{"id":"b4be7164a7096add","repo":"dotnet/runtime","slug":"posix-memalign-failed","errorCode":null,"errorMessage":"posix_memalign failed","messagePattern":"posix_memalign failed","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"critical","filePath":"src/native/libs/Common/JavaScript/host/assets.ts","lineNumber":35,"sourceCode":"    let fileName = lastSlash > 0 ? virtualPath.substring(lastSlash + 1) : virtualPath;\n    if (fileName.startsWith(\"/\")) {\n        fileName = fileName.substring(1);\n    }\n    if (!parentDirectory.startsWith(\"/\")) {\n        parentDirectory = browserVirtualAppBase + parentDirectory;\n    }\n\n    _ems_.dotnetLogger.debug(`Registering PDB '${fileName}' in directory '${parentDirectory}'`);\n    _ems_.FS.createPath(\"/\", parentDirectory, true, true);\n    _ems_.FS.createDataFile(parentDirectory, fileName, bytes, true /* canRead */, true /* canWrite */, true /* canOwn */);\n}\n\nexport function registerDllBytes(bytes: Uint8Array, virtualPath: string, shortName: string) {\n    const sp = _ems_.stackSave();\n    try {\n        const ptrPtr = _ems_.stackAlloc(sizeOfPtr);\n        if (_ems_._posix_memalign(ptrPtr as any, 16, bytes.length)) {\n            throw new Error(\"posix_memalign failed\");\n        }\n\n        const ptr = _ems_.HEAPU32[ptrPtr as any >>> 2];\n        _ems_.HEAPU8.set(bytes, ptr >>> 0);\n\n        _ems_.dotnetLogger.debug(`Registered assembly '${virtualPath}' (shortName: '${shortName}') at ${ptr.toString(16)} length ${bytes.length}`);\n        loadedAssemblies.set(virtualPath, { ptr, length: bytes.length });\n        loadedAssemblies.set(shortName, { ptr, length: bytes.length });\n    } finally {\n        _ems_.stackRestore(sp);\n    }\n}\n\nexport async function instantiateWebcilModule(webcilPromise: Promise<Response>, memory: WebAssembly.Memory, virtualPath: string, tableSize?: number, payloadSize?: number): Promise<void> {\n    // The boot config carries payloadSize for every webcil asset (and tableSize for R2R images), so\n    // the loader never buffers the bytes, parses the data section or calls getWebcilSize. Assets\n    // without a tableSize are plain (Webcil wrapper version 0) images.\n    if (typeof payloadSize !== \"number\" || payloadSize === 0) {","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/dotnet/runtime/blob/60108ba66eb7d1d12f595480091b4ad80a24b172/src/native/libs/Common/JavaScript/host/assets.ts#L17-L53","documentation":"Thrown by registerDllBytes() (src/native/libs/Common/JavaScript/host/assets.ts:30) when _ems_._posix_memalign returns non-zero. posix_memalign is the native aligned-allocation used to place a managed assembly's bytes into the wasm heap at a 16-byte boundary; a non-zero return means the allocator could not satisfy the request, i.e. the linear memory is exhausted.","triggerScenarios":"The loader fetched an assembly's bytes and calls registerDllBytes during assembly load; _posix_memalign(ptrPtr,16,bytes.length) fails. This happens when the wasm Memory cannot grow to fit bytes.length (it would exceed the maximum memory, or memory.max was hit), or the assembly is pathologically large.","commonSituations":"Loading many/large assemblies into a runtime whose maximum memory was set too low; loading an assembly whose size exceeds remaining memory growth headroom; a memory leak in the host leaving no room for the next assembly.","solutions":["Raise the runtime's maximum WebAssembly.Memory (set the memory settings / config so the heap can grow enough for all assemblies).","Reduce the working set loaded eagerly; use lazy assembly loading (BlazorWebAssemblyLazyLoad) to defer large or rarely-used assemblies.","Investigate a memory leak in the host if the failure appears mid-session after earlier loads succeeded."],"exampleFix":"// before\n// runtime config: memory not set, small default -> posix_memalign failed\n\n// after\n// dotnet native host config\n{\n  \"memory\": { \"initial\": 1, \"maximum\": 4294 } // give the heap room to grow\n}","handlingStrategy":"try-catch","validationCode":"// coarse pre-check: estimate whether the heap can grow for the assembly set\nfunction canFitBytes(approxBytes: number): boolean {\n  const mem = (globalThis as any).Module?.HEAP8;\n  return !mem || mem.buffer.byteLength + approxBytes < 2 * 1024 * 1024 * 1024;\n}","typeGuard":null,"tryCatchPattern":"try {\n  await dotnetBrowserHostExports.registerDllBytes(bytes, virtualPath, shortName);\n} catch (err) {\n  if (/posix_memalign failed/.test(String((err as Error).message))) {\n    // out of linear memory: raise max memory or lazy-load fewer assemblies\n  }\n  throw err;\n}","preventionTips":["Size the runtime maximum memory to fit all eagerly-loaded assemblies.","Lazy-load large or rarely-used assemblies.","Watch for heap growth during long sessions to catch leaks early."],"tags":["memory","assembly-loading","native"],"backgroundTag":null,"analyzedSha":"60108ba66eb7d1d12f595480091b4ad80a24b172","analyzedAt":"2026-08-10T18:54:11.478Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}