{"record":{"id":"a76ab8e47cb7ca2c","repo":"oven-sh/bun","slug":"invalidpesignature","errorCode":null,"errorMessage":"InvalidPESignature","messagePattern":"InvalidPESignature","errorType":"error_code","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/exe_format/pe.rs","lineNumber":21,"sourceCode":"\nuse core::mem::{offset_of, size_of};\nuse core::ptr;\nuse core::slice;\n\n// New error types for PE manipulation\n#[derive(thiserror::Error, strum::IntoStaticStr, Debug, Copy, Clone, Eq, PartialEq)]\npub enum Error {\n    #[error(\"OutOfBounds\")]\n    OutOfBounds,\n    #[error(\"BadAlignment\")]\n    BadAlignment,\n    #[error(\"Overflow\")]\n    Overflow,\n    #[error(\"InvalidPEFile\")]\n    InvalidPEFile,\n    #[error(\"InvalidDOSSignature\")]\n    InvalidDOSSignature,\n    #[error(\"InvalidPESignature\")]\n    InvalidPESignature,\n    #[error(\"UnsupportedPEFormat\")]\n    UnsupportedPEFormat,\n    #[error(\"InsufficientHeaderSpace\")]\n    InsufficientHeaderSpace,\n    #[error(\"TooManySections\")]\n    TooManySections,\n    #[error(\"SectionExists\")]\n    SectionExists,\n    #[error(\"InputIsSigned\")]\n    InputIsSigned,\n    #[error(\"InvalidSecurityDirectory\")]\n    InvalidSecurityDirectory,\n    #[error(\"SecurityDirInsideImage\")]\n    SecurityDirInsideImage,\n    #[error(\"UnexpectedOverlayPresent\")]\n    UnexpectedOverlayPresent,\n    #[error(\"InsufficientSpace\")]","sourceCodeStart":3,"sourceCodeEnd":39,"githubUrl":"https://github.com/oven-sh/bun/blob/8c5296ac459e8252d3cd702f3fbcbb0c249d95d5/src/exe_format/pe.rs#L3-L39","documentation":"The PE parser found a valid DOS header (MZ) but the 4-byte signature at the e_lfanew offset is not 'PE\\0\\0' (0x4550). Bun's standalone-compile pipeline for Windows targets parses the base executable byte-for-byte (PEFile::init, src/exe_format/pe.rs:272), and this check guards against truncated or non-PE binaries that merely start with MZ. It aborts the compile with 'Error initializing PE file: InvalidPESignature'.","triggerScenarios":"Running `bun build --compile --target=windows` (or on a Windows host) where the base executable (the copied Bun binary passed via `--compile`'s base, i.e. the runtime executable being extended) is corrupted mid-copy, was byte-patched, is truncated by a partial download/clone with git LFS, or is actually a DOS/other binary that happens to begin with 'MZ'. Thrown from PEFile::init at src/exe_format/pe.rs:272-274 when pe_header.signature != 0x00004550.","commonSituations":"A custom base executable (`--bytecode`/base exe workflows, BUN_COMPILE_CACHE or copied bun.exe) that got corrupted; CI caches storing a truncated exe; antivirus quarantining/altering bun.exe; using a Git-LFS pointer file or HTML error page saved as .exe as the base.","solutions":["Re-download or rebuild the base bun.exe for the exact target (windows-x64 / windows-arm64) and retry the compile.","If using a custom base executable, verify it runs on a Windows machine (or under Wine) before using it as the --compile base.","Check the file is not truncated: compare its SHA-256 against the published checksum for that Bun release.","Inspect the header manually: `xxd -l 2 base.exe` must show '4d 5a' (MZ) and the dword at offset e_lfanew (file offset 0x3c) must point at bytes '50 45 00 00'."],"exampleFix":"# before: base exe came from an unverified cache\nBUN_COMPILE_CACHE_DIR=./cache bun build app.ts --compile --target=windows-x64\n# after: verify the base exe signature before compiling\nxxd -s 0x3c -l 4 base.exe   # read e_lfanew, then check 'PE\\0\\0' there\nbun build app.ts --compile --target=windows-x64  # with a fresh, verified bun.exe","handlingStrategy":"validation","validationCode":"// Run before `bun build --compile --base base.exe`\nconst bytes = await Bun.file('base.exe').arrayBuffer();\nconst dv = new DataView(bytes);\nfunction isPE64(buf, dv) {\n  if (buf.byteLength < 0x40 || dv.getUint16(0, true) !== 0x5a4d) return false; // 'MZ'\n  const peOff = dv.getUint32(0x3c, true);\n  if (peOff + 4 > buf.byteLength) return false;\n  return dv.getUint32(peOff, true) === 0x00004550; // 'PE\\0\\0'\n}\nif (!isPE64(bytes, dv)) throw new Error('base.exe is not a valid PE image');","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pin base executables by checksum and verify before every compile","Never source the base exe from LFS/mirrors that can truncate files","Keep compile input/output directories separate so outputs can't feed back as bases"],"tags":["windows","pe","compile","standalone","binary-format"],"backgroundTag":null,"analyzedSha":"8c5296ac459e8252d3cd702f3fbcbb0c249d95d5","analyzedAt":"2026-08-16T08:01:58.794Z","schemaVersion":2},"datasetVersion":"2026-08-16T08:17:34.114Z"}