From 7779fc244928f0f695ecdeb55df0b1e19bbf331e Mon Sep 17 00:00:00 2001 From: Afloat Date: Mon, 9 Nov 2026 23:34:53 +0800 Subject: [PATCH] Advance ELF core mapping paths by their original bytes (R3.12) (#1366) A core dump's file mapping paths are read one after another, and the reader moved on by the length of the decoded path, which differs from the stored bytes when a path isn't valid UTF-8, so the paths after it went wrong. It now moves on by the bytes it read. --- crates/formats/src/elf.rs | 4 +- crates/formats/tests/elf_core_path_cursor.rs | 52 ++++++++++++++++++++ docs/ROADMAP.md | 2 + docs/STATUS.md | 1 + 4 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 crates/formats/tests/elf_core_path_cursor.rs diff --git a/crates/formats/src/elf.rs b/crates/formats/src/elf.rs index 1245efc6d..9f65cd93b 100644 --- a/crates/formats/src/elf.rs +++ b/crates/formats/src/elf.rs @@ -992,7 +992,9 @@ pub fn parse_core(data: &[u8]) -> Result { let o = table + i * 3 * ws; let (Some(s), Some(en), Some(po)) = (d.word(o), d.word(o + ws), d.word(o + 2 * ws)) else { break }; let path = d.r.cstr(names_off, 4096).unwrap_or_default(); - names_off = names_off.saturating_add(path.len() + 1); + let raw = desc.get(names_off..).unwrap_or_default(); + let consumed = raw.iter().position(|&byte| byte == 0).map_or(raw.len(), |end| end + 1); + names_off = names_off.saturating_add(consumed); core.mapped_files.push(MappedFile { start: s, end: en, file_offset: po.saturating_mul(page), path }); } } else { diff --git a/crates/formats/tests/elf_core_path_cursor.rs b/crates/formats/tests/elf_core_path_cursor.rs new file mode 100644 index 000000000..41e8e2fb6 --- /dev/null +++ b/crates/formats/tests/elf_core_path_cursor.rs @@ -0,0 +1,52 @@ +use craft_anything_formats::elf; +use craft_anything_testkit::elf64_core; + +fn core_with_names(names: &[u8], count: u64) -> Vec { + let mut desc = Vec::new(); + desc.extend_from_slice(&count.to_le_bytes()); + desc.extend_from_slice(&4096u64.to_le_bytes()); + for row in 0..count { + let start = (row + 1) * 4096; + desc.extend_from_slice(&start.to_le_bytes()); + desc.extend_from_slice(&(start + 4096).to_le_bytes()); + desc.extend_from_slice(&0u64.to_le_bytes()); + } + desc.extend_from_slice(names); + let mut note = Vec::new(); + note.extend_from_slice(&5u32.to_le_bytes()); + note.extend_from_slice(&(desc.len() as u32).to_le_bytes()); + note.extend_from_slice(&0x4649_4c45u32.to_le_bytes()); + note.extend_from_slice(b"CORE\0\0\0\0"); + note.extend_from_slice(&desc); + let mut data = elf64_core(0, 0); + let offset = data.len() as u64; + data[0x48..0x50].copy_from_slice(&offset.to_le_bytes()); + data[0x60..0x68].copy_from_slice(&(note.len() as u64).to_le_bytes()); + data[0x68..0x70].copy_from_slice(&(note.len() as u64).to_le_bytes()); + data.extend_from_slice(¬e); + data +} + +#[test] +fn lossy_path_text_does_not_shift_the_next_mapping() { + let core = elf::parse_core(&core_with_names(b"/bad-\xff\0/second\0", 2)).unwrap(); + assert_eq!(core.mapped_files[0].path, "/bad-\u{fffd}"); + assert_eq!(core.mapped_files[1].path, "/second"); + assert_eq!(core.mapped_files[1].start, 8192); +} + +#[test] +fn capped_path_preview_skips_the_whole_original_path() { + let mut names = vec![b'a'; 5000]; + names.extend_from_slice(b"\0/second\0"); + let core = elf::parse_core(&core_with_names(&names, 2)).unwrap(); + assert_eq!(core.mapped_files[0].path, "a".repeat(4096)); + assert_eq!(core.mapped_files[1].path, "/second"); +} + +#[test] +fn complete_utf8_paths_and_an_unterminated_last_path_keep_their_text() { + let core = elf::parse_core(&core_with_names("/sample/界\0/last".as_bytes(), 2)).unwrap(); + assert_eq!(core.mapped_files[0].path, "/sample/界"); + assert_eq!(core.mapped_files[1].path, "/last"); +} diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index e9a385781..dfa9024d3 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -1620,6 +1620,8 @@ Owner: open · Status: in progress — claimed: Afloat16, branch bugfix/r3.12-dart-note - [x] Locate ELF core mapping names after the complete declared table. — claimed: Afloat16, branch bugfix/r3.12-elf-core-file-name-count-cap-20261105 +- [x] Advance ELF core NT_FILE path cursors by complete original byte strings, even when a displayed path is lossy or capped. + — claimed: Afloat16, branch bugfix/r3.12-elf-core-path-cursor-20261109 - [x] Validate index source map section offsets and stop an unresolved section from borrowing mappings from the preceding section. — claimed: Afloat16, branch bugfix/r3.12-index-map-boundaries - [x] Preserve the URL delimiter in source-map roots when resolving a relative source file. diff --git a/docs/STATUS.md b/docs/STATUS.md index c010d86c6..42239c51c 100644 --- a/docs/STATUS.md +++ b/docs/STATUS.md @@ -83,6 +83,7 @@ workspace clippy, layers, fmt and the secret scan of all history were clean. Chi | Preserve complete Unicode characters in shortened .NET strings | .NET user-string limits preserve supplementary Unicode characters. | Synthetic byte fixtures in `dotnet_user_string_surrogate_limit` integration regressions. Cover supplementary characters at several limits, a zero-character limit, and unpaired surrogates followed by valid characters. | | ELF dynamic library names | Needed libraries and dynamic path names stay within DT_STRSZ when the executable declares it. | An ELF64 fixture moves the bound to the beginning and to the terminator of a needed library name. | | Locate ELF core mapping names after the complete declared table | ELF core filenames follow the full declared mapping table. | Synthetic byte fixtures in `elf_core_file_name_count_cap` integration regressions. Cover 65,537 declared mappings with capped output, an impossible declared table, and a small complete mapping table. | +| ELF core mapping path cursors | NT_FILE mapping paths advance by the original NUL-terminated byte strings, even when the displayed path is lossy or capped. | Invalid UTF-8 and long first paths followed by another mapping; valid UTF-8 and unterminated final-path controls. | | Keep DOS relocations inside the declared executable header | DOS relocation records stay inside the declared MZ header. | Synthetic byte fixtures in `dos_relocation_header_boundary` integration regressions. Cover records extending into the load module, a partial final record, and complete records ending exactly at the header boundary. | | ELF symbol-version section bounds | Version definitions, requirements and symbol-version indices stay inside their declared section byte ranges. | Both byte orders with escaping auxiliary records and indices, plus complete version-table controls. | | Java decompilation | `java_vineflower_decompile` decompiles a real JAR to source with the pinned Vineflower 1.12.0, run through the configured Java | Live run on Linux x86-64 (OpenJDK 21.0.12.1) against Apache Commons IO 2.16.1 (497 KB, 374 entries): 253 source files, exit 0; `org.apache.commons.io.FileUtils` returned 1,350 lines with the class and its file-reading methods present. The engine install matched the pinned SHA-256. Not checked: Windows and macOS, a `.class` input, or a second JAR |