{"record":{"id":"49db5cbd4f3075ce","repo":"vosen/ZLUDA","slug":"io-error-new-io-errorkind-invaliddata-e-to-st","errorCode":null,"errorMessage":"io::Error::new(io::ErrorKind::InvalidData, e.to_string())","messagePattern":"io::Error::new\\(io::ErrorKind::InvalidData, e\\.to_string\\(\\)\\)","errorType":"exception","errorClass":"io::Error","httpStatus":null,"severity":"error","filePath":"kernel_metadata/src/lib.rs","lineNumber":80,"sourceCode":"    }\n\n    pub fn copy_object<'a>(elf_bytes: &'a [u8]) -> Option<ModuleMetadata32Bit> {\n        read_object::<ArchivedModuleMetadata32Bit>(elf_bytes, Self::SECTION, Self::VERSION)\n            .and_then(|archived| rkyv::deserialize::<_, rkyv::rancor::Failure>(archived).ok())\n    }\n}\n\npub fn write_object(\n    this: &impl for<'a, 'b> Serialize<\n        HighSerializer<AlignedVec, ArenaHandle<'b>, rkyv::rancor::Failure>,\n    >,\n    section: &str,\n    version: u64,\n    main_elf: &[u8],\n    writer: &mut impl io::Write,\n) -> io::Result<()> {\n    let main_header = elf::FileHeader64::<object::Endianness>::parse(main_elf)\n        .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e.to_string()))?;\n    let endian = main_header\n        .endian()\n        .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e.to_string()))?;\n    let data = rkyv::to_bytes::<rkyv::rancor::Failure>(this)\n        .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;\n    let mut buf = Vec::new();\n    let mut writer_elf = elf_write::Writer::new(endian, true, &mut buf);\n    writer_elf.reserve_file_header();\n    let section_name = writer_elf.add_section_name(section.as_bytes());\n    writer_elf.reserve_section_index();\n    let section_offset = writer_elf.reserve(data.len() + mem::size_of::<u64>(), 8);\n    writer_elf.reserve_shstrtab_section_index();\n    writer_elf.reserve_shstrtab();\n    writer_elf.reserve_section_headers();\n    writer_elf\n        .write_file_header(&elf_write::FileHeader {\n            os_abi: main_header.e_ident().os_abi,\n            abi_version: main_header.e_ident().abi_version,","sourceCodeStart":62,"sourceCodeEnd":98,"githubUrl":"https://github.com/vosen/ZLUDA/blob/9c8b43f242985150f86a7f485218b7b82c3e96ca/kernel_metadata/src/lib.rs#L62-L98","documentation":"`write_object` wraps any failure from parsing the supplied `main_elf` bytes as an ELF64 file header into an `io::Error` with kind `InvalidData`. The library needs the input ELF's OS/ABI, machine type, and flags to emit a companion metadata object file, so if the bytes are not a valid ELF64 the whole write is aborted. This keeps the failure surfaced as a plain `io::Result` error rather than a panic.","triggerScenarios":"Calling `write_object` (directly or via `ModuleMetadata::write_object`) with a `main_elf` slice that is truncated (<64 bytes), empty, starts with bytes other than the `\\x7fELF` magic, is a 32-bit ELF where a 64-bit header is expected, or is otherwise a corrupted/non-ELF binary.","commonSituations":"Pointing the build script at a wrong file path, passing a `.ptx`/text file or an intermediate object instead of the final ELF, a compiler/linker change producing ELF32 output, or the ELF being cut off by a failed write/read upstream.","solutions":["Verify `main_elf` begins with the 4-byte ELF magic `\\x7fELF` before calling `write_object`","Check the file is a complete ELF64 (e_ident[EI_CLASS] == ELFCLASS64) and at least 64 bytes long","Make sure you pass the final linked ELF (e.g. from the CUDA/cubin compilation step), not a PTX file or a partial buffer","Log the first 16 bytes of `main_elf` when the error occurs to spot wrong-file wiring"],"exampleFix":"// before\nlet mut elf = Vec::new();\nFile::open(\"kernel.ptx\")?.read_to_end(&mut elf)?;\nwrite_object(&meta, \".zluda\", 1, &elf, &mut out)?;\n// after\nlet mut elf = Vec::new();\nFile::open(\"kernel.cubin\")?.read_to_end(&mut elf)?;\nassert_eq!(&elf[..4], b\"\\x7fELF\", \"not an ELF file\");\nwrite_object(&meta, \".zluda\", 1, &elf, &mut out)?;","handlingStrategy":"validation","validationCode":"fn is_elf64(bytes: &[u8]) -> bool {\n    bytes.len() >= 64 && &bytes[..4] == b\"\\x7fELF\" && bytes[4] == 2 // EI_CLASS = ELFCLASS64\n}\nif !is_elf64(&main_elf) {\n    return Err(io::Error::new(io::ErrorKind::InvalidData, \"main_elf is not a valid ELF64\"));\n}","typeGuard":"fn looks_like_elf(bytes: &[u8]) -> bool {\n    bytes.len() >= 4 && &bytes[..4] == b\"\\x7fELF\"\n}","tryCatchPattern":"match write_object(&meta, \".zluda\", VERSION, &main_elf, &mut out) {\n    Ok(()) => (),\n    Err(e) if e.kind() == io::ErrorKind::InvalidData =>\n        eprintln!(\"bad ELF input: {e}\"),\n    Err(e) => return Err(e),\n}","preventionTips":["Always assert the ELF magic and EI_CLASS before passing bytes to write_object","Pass the final linked ELF output, never PTX text or intermediate objects","Check that the producer of the ELF completed successfully (file size > header size)"],"tags":["elf","invalid-data","rust","binary-format"],"backgroundTag":"invalid-argument-format","analyzedSha":"9c8b43f242985150f86a7f485218b7b82c3e96ca","analyzedAt":"2026-09-06T09:21:08.049Z","contentChangedAt":"2026-09-06T09:21:08.049Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}