Debugging Legacy Code with LLMs: A UEFI Graphics Application Case Study
This article documents a practical experiment in using a large language model to debug a non-running UEFI graphics application left behind by an AI coding tool. It walks through a two-round iteration process—from static code review and restoring basic runnability to hardening error handling, resource lifecycles, input, and rendering—and distills a reusable four-step methodology for AI-assisted legacy code debugging, along with common pitfalls in low-level graphics programming.
1. Background: An Experiment in Taking Over a Mess
As AI-assisted programming becomes part of everyday development, a common scenario is not “generating a project from scratch” but taking over code someone else left behind that no longer runs. This kind of task demands more from the model: it must understand an unfamiliar code structure and, without the original author’s context, reproduce the problem, identify the root cause, and fix it.
This article records one such exercise: a graphics application generated by an AI coding tool was left in a source directory—structurally complete but completely non-functional. The goal was to have another large language model make it runnable and then optimize it through two rounds of iteration, without rewriting the architecture.
The value of this experiment is not in comparing tools, but in distilling a transferable debugging methodology—especially for the hidden defects in low-level graphics programming that “compile cleanly but crash at runtime.”
2. The Initial State of the Legacy Code
The code was not a mess. It showed a typical “AI-generated style”:
- Seven source files, split by responsibility;
- Clear entry module, main loop, game logic, rendering layer, and input handling layer;
- Consistent naming and a clean directory structure.
The problem: clean structure ≠ correct semantics. Such code commonly ignores call ordering, resource lifecycles, and platform protocol constraints—issues the compiler cannot catch and that only surface at runtime.
3. Round 1: From Non-Runnable to Runnable
3.1 The First Step Was Review, Not Editing
The first instruction was a single sentence: review this code and list potential problems. The model scanned the entry function, main loop, logic layer, rendering layer, and input handling module by module, and produced a list of issues sorted by risk level.
The point was to build a problem map: turning the vague symptom “it won’t run” into a set of concrete, verifiable hypotheses, avoiding blind trial and error.
3.2 Problem One: Immediate Exit on Startup (Frame Buffer Invalidated)
The program printed the main-loop startup log and then exited immediately. After adding serial debug output, the fault was traced to a call made after the graphics output protocol invocation.
The root cause was a classic pointer lifetime error: the pointer obtained when querying mode information pointed to memory that became invalid after the mode switch—a dangling pointer. The fix was to reorder the calls and re-fetch the frame buffer information after the mode was set.
// Wrong order: query first, switch mode, pointer becomes invalidQueryMode(gop, mode, &info);SetMode(gop, mode);frame = info->FrameBufferBase; // info is no longer valid// Correct order: re-query after switching modeSetMode(gop, mode);QueryMode(gop, mode, &info);frame = info->FrameBufferBase;
The general principle: any pattern that “acquires a handle/pointer first and changes state afterward” must re-validate the handle.
3.3 Problem Two: Runs but No Display (Wrong Protocol Identifier)
The program no longer crashed, but the screen stayed blank. From the logs, the model determined the graphics output protocol identifier was misconfigured: the platform headers contained several similar protocol identifiers, and picking the wrong one prevented correct binding of the graphics output capability.
The fix was to consistently use the canonical graphics output protocol identifier and to validate the binding result at the entry point, returning a clear error code on failure instead of continuing.
if (EFI_ERROR(status) || gop == NULL) {DEBUG(("gop init failed: %r\n", status));return status;}
3.4 Round 1 Summary
After the fixes, the program ran. Three methodological conclusions:
- Review before editing, turning symptoms into a hypothesis list;
- Strengthen observability, using serial/log output to move the crash point forward to a specific call;
- Re-acquire resources after state changes—a high-frequency fix pattern in low-level programming.
4. Round 2: Refinement and Hardening
Running was only the starting point. The second round focused on engineering quality:
- Complete error handling: check every platform API return value; never “ignore the return value and continue”;
- Consolidated resource lifecycles: release the frame buffer, events, and timers uniformly on the exit path to avoid leftovers;
- Input debouncing: add repeat-code filtering and a state machine to keyboard scanning to prevent multiple triggers from a single keypress;
- Rendering optimization: reduce full-screen redraws using dirty rectangles or double buffering to lower flicker;
- Portability isolation: converge platform-specific calls into a separate adaptation layer for easier migration across firmware environments.
5. Methodology: A Four-Step Approach to AI-Assisted Legacy Code Debugging
- Static review: have the model produce a structured issue list sorted by risk;
- Observability first: add logs and error codes before changing logic;
- Minimal fix units: verify one hypothesis at a time to avoid attribution difficulty from changing multiple points at once;
- Two-round iteration: round one for “make it run,” round two for “robustness and performance.”
6. Common Pitfalls
- Reusing an old pointer after a mode switch;
- Misbinding due to similar protocol identifiers;
- Ignoring API return values, which pushes the fault further downstream;
- No serial/log output, making low-level issues nearly impossible to locate;
- Committing a large batch of changes at once, preventing bisection.
7. Conclusion
The difficulty in debugging low-level graphics programs often lies not in algorithms but in resource lifecycles and platform contracts. Large language models can significantly compress the “read code—form hypotheses—modify code” loop, but only if the developer provides clear symptom descriptions and observability. Treating AI as an efficient executor while keeping methodology and verification discipline on the human side is where the real value of this kind of practice lies.
