PE loader: export ordinal base + index overflows (panic in debug, wraps in release) #691

Open
opened 2026-11-26 04:29:35 +00:00 by github-bot · 2 comments
Owner

@⁠lordon1a on GitHub · 2026-11-24 03:32 UTC · original

OwnedExports::from_pe (crates/xcl/src/pe.rs) adds the export directory's Base, read from the file, to each function index with plain u32 addition:

out.by_ordinal.insert(ex.ordinal_base + i as u32, t);

With a Base near u32::MAX this panics with an overflow in debug builds. The loader calls it for every mapped game DLL (loader.rs:133, OwnedExports::from_pe(&pe).unwrap_or_default()), so unwrap_or_default does not help. In release builds the sum wraps to low ordinals, and the entry can shadow a real ordinal 0, 1, … The rest of pe.rs already uses checked arithmetic for values from the file (#106). This one was missed.

Only a malformed or unusual export table hits this. Valid images keep Base + NumberOfFunctions inside 16 bits.

Failing test (in pe.rs's mod tests, uses the existing directory_at_rva helper):

/// The ordinal base comes from the file: an export past u32::MAX is no ordinal and is skipped
/// (it panicked on overflow in debug builds and wrapped to ordinal 0 in release ones).
#[test]
fn export_ordinals_past_u32_max_are_skipped() {
    let rva = 0x1000u32;
    let mut payload = vec![0u8; 48];
    payload[16..20].copy_from_slice(&u32::MAX.to_le_bytes()); // Base
    payload[20..24].copy_from_slice(&2u32.to_le_bytes()); // NumberOfFunctions
    payload[28..32].copy_from_slice(&(rva + 40).to_le_bytes()); // AddressOfFunctions
    payload[40..44].copy_from_slice(&0x2000u32.to_le_bytes());
    payload[44..48].copy_from_slice(&0x2004u32.to_le_bytes());
    let data = directory_at_rva(DIR_EXPORT, rva, &payload);
    let pe = Pe::parse(&data, Layout::File).unwrap();
    let ex = OwnedExports::from_pe(&pe).unwrap();
    assert_eq!(ex.by_ordinal.len(), 1);
    assert_eq!(ex.by_ordinal.get(&u32::MAX), Some(&ExportTarget::Rva(0x2000)));
    assert!(!ex.by_ordinal.contains_key(&0));
}

On main (91f9a5e): cargo test -p xcl --lib pe::tests::export fails with attempt to add with overflow at crates\xcl\src\pe.rs:610:43.

Proposed fix: skip entries past u32::MAX and keep the others:

             for (i, &rva) in ex.functions.iter().enumerate() {
+                // the base comes from the file: an entry past u32::MAX has no ordinal
+                let Some(ordinal) = ex.ordinal_base.checked_add(i as u32) else { break };
                 if let Some(t) = pe.export_target(&ex, rva) {
-                    out.by_ordinal.insert(ex.ordinal_base + i as u32, t);
+                    out.by_ordinal.insert(ordinal, t);
                 }
             }

With the fix: cargo test -p xcl --lib -- --test-threads=4 gave 653 passed, 0 failed, 21 ignored (652 on main without the new test).

Tested

  • i7-12700H, Intel Iris Xe + RTX 3070 Ti Laptop, Windows 11 build 26200
  • rustc 1.100.0, x86_64-pc-windows-gnullvm, debug. This is not the MSVC toolchain the project uses.
  • Only --lib tests were run.

Not tested

  • cargo test --workspace (integration, e2e and GPU tests), the MSVC toolchain
  • scripts/smoke.ps1: I have no title to run it on. The fix is a runtime change, so a PR needs someone with a title for the two xcl-smoke blocks. Branch with the test and the fix (one commit on 91f9a5e): https://github.com/lordon1a/xcl/tree/pe-export-ordinal-overflow

AI use

  • Claude Code, Claude Opus 5.5: found the bug, wrote the test and the fix, wrote this issue. I checked the test failing and passing myself.
**@⁠lordon1a** on GitHub · 2026-11-24 03:32 UTC · [original](https://github.com/xcl-dev/xcl/issues/691) `OwnedExports::from_pe` (`crates/xcl/src/pe.rs`) adds the export directory's `Base`, read from the file, to each function index with plain `u32` addition: ```rust out.by_ordinal.insert(ex.ordinal_base + i as u32, t); ``` With a `Base` near `u32::MAX` this panics with an overflow in debug builds. The loader calls it for every mapped game DLL (`loader.rs:133`, `OwnedExports::from_pe(&pe).unwrap_or_default()`), so `unwrap_or_default` does not help. In release builds the sum wraps to low ordinals, and the entry can shadow a real ordinal 0, 1, … The rest of `pe.rs` already uses checked arithmetic for values from the file (#106). This one was missed. Only a malformed or unusual export table hits this. Valid images keep `Base + NumberOfFunctions` inside 16 bits. **Failing test** (in `pe.rs`'s `mod tests`, uses the existing `directory_at_rva` helper): ```rust /// The ordinal base comes from the file: an export past u32::MAX is no ordinal and is skipped /// (it panicked on overflow in debug builds and wrapped to ordinal 0 in release ones). #[test] fn export_ordinals_past_u32_max_are_skipped() { let rva = 0x1000u32; let mut payload = vec![0u8; 48]; payload[16..20].copy_from_slice(&u32::MAX.to_le_bytes()); // Base payload[20..24].copy_from_slice(&2u32.to_le_bytes()); // NumberOfFunctions payload[28..32].copy_from_slice(&(rva + 40).to_le_bytes()); // AddressOfFunctions payload[40..44].copy_from_slice(&0x2000u32.to_le_bytes()); payload[44..48].copy_from_slice(&0x2004u32.to_le_bytes()); let data = directory_at_rva(DIR_EXPORT, rva, &payload); let pe = Pe::parse(&data, Layout::File).unwrap(); let ex = OwnedExports::from_pe(&pe).unwrap(); assert_eq!(ex.by_ordinal.len(), 1); assert_eq!(ex.by_ordinal.get(&u32::MAX), Some(&ExportTarget::Rva(0x2000))); assert!(!ex.by_ordinal.contains_key(&0)); } ``` On `main` (`91f9a5e`): `cargo test -p xcl --lib pe::tests::export` fails with `attempt to add with overflow` at `crates\xcl\src\pe.rs:610:43`. **Proposed fix**: skip entries past `u32::MAX` and keep the others: ```diff for (i, &rva) in ex.functions.iter().enumerate() { + // the base comes from the file: an entry past u32::MAX has no ordinal + let Some(ordinal) = ex.ordinal_base.checked_add(i as u32) else { break }; if let Some(t) = pe.export_target(&ex, rva) { - out.by_ordinal.insert(ex.ordinal_base + i as u32, t); + out.by_ordinal.insert(ordinal, t); } } ``` With the fix: `cargo test -p xcl --lib -- --test-threads=4` gave 653 passed, 0 failed, 21 ignored (652 on `main` without the new test). **Tested** - i7-12700H, Intel Iris Xe + RTX 3070 Ti Laptop, Windows 11 build 26200 - rustc 1.100.0, `x86_64-pc-windows-gnullvm`, debug. This is not the MSVC toolchain the project uses. - Only `--lib` tests were run. **Not tested** - `cargo test --workspace` (integration, e2e and GPU tests), the MSVC toolchain - `scripts/smoke.ps1`: I have no title to run it on. The fix is a runtime change, so a PR needs someone with a title for the two `xcl-smoke` blocks. Branch with the test and the fix (one commit on `91f9a5e`): https://github.com/lordon1a/xcl/tree/pe-export-ordinal-overflow **AI use** - Claude Code, Claude Opus 5.5: found the bug, wrote the test and the fix, wrote this issue. I checked the test failing and passing myself.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:29:35 +00:00
Author
Owner

@⁠Anon64 on GitHub · 2026-11-24 03:46 UTC · original

Test report for the branch lordon1a/xcl pe-export-ordinal-overflow at 88311e4, which is based on main 91f9a5e: the MSVC toolchain, the whole workspace, and the two regression runs. If you open the PR from this commit, you can paste both blocks into Regression run.

Tested

  • NVIDIA GeForce RTX 3060 (driver 581.80, wave32), Intel Core i9-14900KF, Windows 11 25H2 26200.9974, Rust 1.90.0 x86_64-pc-windows-msvc
  • cargo test --workspace --no-fail-fast -- --test-threads=4 (debug) on 88311e4: 692 passed, 1 failed, 41 ignored. pe::tests::export_ordinals_past_u32_max_are_skipped passes. The one failure is hle::kernelx::tests::xmem_virtual_query_marks_cpu_readable_memory, which fails the same way on main on this machine (#650 / #656).
  • without the checked_add change the new test panics: attempt to add with overflow at crates\xcl\src\pe.rs:611:43
  • SKIPPED: gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware (no wave64)

Regression run (scripts/smoke.ps1, release, same title, 60 s each; pasted unedited)

main:

xcl-smoke v1
commit: 91f9a5e9f03156ea82dc55a18f7704862da7a5db
gpu: NVIDIA GeForce RTX 3060
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1648 (PresentX 1648)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14684 MB

88311e4:

xcl-smoke v1
commit: 88311e4d59f1e49e1ac692482bd6e4406d3b1e8a
gpu: NVIDIA GeForce RTX 3060
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1642 (PresentX 1642)
presents per 2 s (last 3): 60 61 60
device removals: 0
crashes: 0
peak commit: 14676 MB

Not tested

  • an image whose export table actually has such a base; the smoke run only shows that the title's DLLs still load as before
  • AMD, wave64

Report written with Claude Code (Claude Opus 5.5) from runs on this machine.

**@⁠Anon64** on GitHub · 2026-11-24 03:46 UTC · [original](https://github.com/xcl-dev/xcl/issues/691#issuecomment-7121399561) Test report for the branch `lordon1a/xcl` `pe-export-ordinal-overflow` at `88311e4`, which is based on `main` `91f9a5e`: the MSVC toolchain, the whole workspace, and the two regression runs. If you open the PR from this commit, you can paste both blocks into **Regression run**. **Tested** - NVIDIA GeForce RTX 3060 (driver 581.80, wave32), Intel Core i9-14900KF, Windows 11 25H2 26200.9974, Rust 1.90.0 `x86_64-pc-windows-msvc` - `cargo test --workspace --no-fail-fast -- --test-threads=4` (debug) on `88311e4`: 692 passed, 1 failed, 41 ignored. `pe::tests::export_ordinals_past_u32_max_are_skipped` passes. The one failure is `hle::kernelx::tests::xmem_virtual_query_marks_cpu_readable_memory`, which fails the same way on `main` on this machine (#650 / #656). - without the `checked_add` change the new test panics: `attempt to add with overflow` at `crates\xcl\src\pe.rs:611:43` - `SKIPPED`: `gpu_geometry_wave32_prefix_count_over_a_mask`, `gpu_wave32_program_in_wave64_hardware` (no wave64) **Regression run** (`scripts/smoke.ps1`, release, same title, 60 s each; pasted unedited) main: ``` xcl-smoke v1 commit: 91f9a5e9f03156ea82dc55a18f7704862da7a5db gpu: NVIDIA GeForce RTX 3060 title: retail-title seconds: 60 exit: 0x0 frames shown: 1648 (PresentX 1648) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 14684 MB ``` `88311e4`: ``` xcl-smoke v1 commit: 88311e4d59f1e49e1ac692482bd6e4406d3b1e8a gpu: NVIDIA GeForce RTX 3060 title: retail-title seconds: 60 exit: 0x0 frames shown: 1642 (PresentX 1642) presents per 2 s (last 3): 60 61 60 device removals: 0 crashes: 0 peak commit: 14676 MB ``` **Not tested** - an image whose export table actually has such a base; the smoke run only shows that the title's DLLs still load as before - AMD, wave64 Report written with Claude Code (Claude Opus 5.5) from runs on this machine.
Author
Owner

@⁠towiice on GitHub · 2026-11-24 03:48 UTC · original

PR with the fix and the test from this issue, rebased on current main: #700 (draft until someone with a title adds the xcl-smoke blocks — I have none). Claude Code (Claude Sonnet 5.5).

**@⁠towiice** on GitHub · 2026-11-24 03:48 UTC · [original](https://github.com/xcl-dev/xcl/issues/691#issuecomment-7121435117) PR with the fix and the test from this issue, rebased on current main: #700 (draft until someone with a title adds the `xcl-smoke` blocks — I have none). Claude Code (Claude Sonnet 5.5).
This discussion has been locked. Commenting is limited to contributors.
No description provided.