java-topology/whitepaper/outreach/mgba-0001.md
russell@unturf.com 652608142a feat: close outreach doc gap — 276 docs (batches 11-16)
All projects with patches now have outreach docs. 276 new docs covering
CWE-407, CWE-312, CWE-362 across C, C++, Java, Python, Go, Rust, C#,
PHP, Ruby, JavaScript, Dart, Erlang, R, and more.

Outreach gap: 276 -> 0.
2026-04-15 13:57:42 -04:00

2.7 KiB
Raw Blame History

mGBA — CWE-407 Disclosure Brief (mgba-0001)

2026-04-13 · Patch available — awaiting upstream merge

Finding

One O(N) defect in mGBA's SM83 (Game Boy CPU) debugger breakpoint checking. Patched. The breakpoint check fires on every CPU instruction and linearly scans the entire breakpoint list.

The Defect

mgba-0001 (PATCHED — MEDIUM): src/sm83/debugger/debugger.c:28

// In SM83DebuggerCheckBreakpoints() — fires every CPU instruction:
for (i = 0; i < mBreakpointListSize(&debugger->breakpoints); ++i) {
    struct mBreakpoint* breakpoint = mBreakpointListGetPointer(&debugger->breakpoints, i);
    if (breakpoint->address != cpu->pc) {
        continue;
    }
    // ... check segment, condition, fire breakpoint
}

Every SM83 instruction pays O(N) to scan all breakpoints. The SM83 runs at ~4 MHz (Game Boy) or ~8 MHz (Game Boy Color). With N=10 breakpoints, this wastes ~40-80 million comparisons per second.

Complexity Proof

At N=10 breakpoints, 4 MHz CPU:

  • Defective: 10 comparisons × 4,000,000 instructions/sec = 40M comparisons/sec
  • Fixed: bloom filter check (4 bit tests) + early exit = ~16M bit-tests/sec, 0 full scans on miss
  • ~10× op reduction in the common case (no breakpoint hit).

Impact

mGBA is one of the most popular Game Boy Advance emulators, widely used for development, speedrunning, and preservation. The debugger breakpoint check fires on every emulated instruction. With multiple breakpoints set during debugging sessions, the linear scan adds measurable overhead proportional to breakpoint count.

The Fix

Add a bloom filter (bpBloom) for O(1) fast-path rejection before the linear scan:

// Before: O(N) scan on every instruction
for (i = 0; i < mBreakpointListSize(...); ++i) { ... }

// After: bloom filter fast-path, skip O(N) scan when no match possible
if (mBreakpointListSize(&debugger->breakpoints) > 0 &&
    !_checkBpBloom(debugger, cpu->pc)) {
    return;  // O(1) rejection
}
// Only reach here on bloom filter hit (rare)

Patch

Fix available: defects/mgba-0001/patch/mgba-0001-sm83-breakpoint-linear-scan.patch

Adds bloom filter infrastructure mirroring the ARM debugger's existing bpBloom pattern. Rebuilds on breakpoint add/remove/enable/disable. ~10× speedup during debugging with 10+ breakpoints.

What We Ask

A patch is ready for review.

  1. Confirm receipt and assign a GitHub issue reference (mgba-emu/mgba).
  2. Assess severity — fires on every emulated CPU instruction during debugging.
  3. Coordinate a disclosure date — we are targeting 90 days from first contact.
  4. We will credit the mGBA team in the public disclosure. Preferred acknowledgment format welcome.

Contact: see cover email. This brief is confidential until coordinated disclosure.