dotnet/runtime · critical · Error

posix_memalign failed

Error message

posix_memalign failed

What it means

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.

Source

Thrown at src/native/libs/Common/JavaScript/host/assets.ts:35

    let fileName = lastSlash > 0 ? virtualPath.substring(lastSlash + 1) : virtualPath;
    if (fileName.startsWith("/")) {
        fileName = fileName.substring(1);
    }
    if (!parentDirectory.startsWith("/")) {
        parentDirectory = browserVirtualAppBase + parentDirectory;
    }

    _ems_.dotnetLogger.debug(`Registering PDB '${fileName}' in directory '${parentDirectory}'`);
    _ems_.FS.createPath("/", parentDirectory, true, true);
    _ems_.FS.createDataFile(parentDirectory, fileName, bytes, true /* canRead */, true /* canWrite */, true /* canOwn */);
}

export function registerDllBytes(bytes: Uint8Array, virtualPath: string, shortName: string) {
    const sp = _ems_.stackSave();
    try {
        const ptrPtr = _ems_.stackAlloc(sizeOfPtr);
        if (_ems_._posix_memalign(ptrPtr as any, 16, bytes.length)) {
            throw new Error("posix_memalign failed");
        }

        const ptr = _ems_.HEAPU32[ptrPtr as any >>> 2];
        _ems_.HEAPU8.set(bytes, ptr >>> 0);

        _ems_.dotnetLogger.debug(`Registered assembly '${virtualPath}' (shortName: '${shortName}') at ${ptr.toString(16)} length ${bytes.length}`);
        loadedAssemblies.set(virtualPath, { ptr, length: bytes.length });
        loadedAssemblies.set(shortName, { ptr, length: bytes.length });
    } finally {
        _ems_.stackRestore(sp);
    }
}

export async function instantiateWebcilModule(webcilPromise: Promise<Response>, memory: WebAssembly.Memory, virtualPath: string, tableSize?: number, payloadSize?: number): Promise<void> {
    // The boot config carries payloadSize for every webcil asset (and tableSize for R2R images), so
    // the loader never buffers the bytes, parses the data section or calls getWebcilSize. Assets
    // without a tableSize are plain (Webcil wrapper version 0) images.
    if (typeof payloadSize !== "number" || payloadSize === 0) {

View on GitHub (pinned to 60108ba66e)

Solutions

  1. Raise the runtime's maximum WebAssembly.Memory (set the memory settings / config so the heap can grow enough for all assemblies).
  2. Reduce the working set loaded eagerly; use lazy assembly loading (BlazorWebAssemblyLazyLoad) to defer large or rarely-used assemblies.
  3. Investigate a memory leak in the host if the failure appears mid-session after earlier loads succeeded.

Example fix

// before
// runtime config: memory not set, small default -> posix_memalign failed

// after
// dotnet native host config
{
  "memory": { "initial": 1, "maximum": 4294 } // give the heap room to grow
}
Defensive patterns

Strategy: try-catch

Validate before calling

// coarse pre-check: estimate whether the heap can grow for the assembly set
function canFitBytes(approxBytes: number): boolean {
  const mem = (globalThis as any).Module?.HEAP8;
  return !mem || mem.buffer.byteLength + approxBytes < 2 * 1024 * 1024 * 1024;
}

Try / catch

try {
  await dotnetBrowserHostExports.registerDllBytes(bytes, virtualPath, shortName);
} catch (err) {
  if (/posix_memalign failed/.test(String((err as Error).message))) {
    // out of linear memory: raise max memory or lazy-load fewer assemblies
  }
  throw err;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of dotnet/runtime@60108ba66e (2026-08-10). Data as JSON: /api/errors/b4be7164a7096add. Report an issue: GitHub.