LLM-Assisted Code Review and Debugging: From Non-Working to Running

    Generating code with large language models is now easy, but getting that code to compile, load, and run reliably is the real challenge—especially in firmware and other low-level environments. Using a real UEFI firmware task as a backdrop, this article walks through a complete workflow for LLM-assisted code review and bug fixing: why a systematic code review should come before any edits, why debugging at the low level is harder than application work, and a two-round iterative loop of "find the problem → locate the root cause → fix → verify." Along the way it examines two concrete failures—a dangling-pointer crash caused by reading the frame buffer before SetMode, and a blank UI caused by using the wrong protocol GUID—then summarizes reusable practices for collaborating with LLMs while keeping human judgment in the loop.

    Introduction: Generating Code Is Not the Same as Running It

    Now that large language models are deeply involved in software development, one fact is becoming clear: getting a model to “write” code is no longer the hard part. The difficulty lies in making that code actually run and meet a usable standard.

    This is especially visible in low-level scenarios such as firmware, drivers, and operating system kernels. A model can produce well-structured, modular project files within minutes, but whether the code compiles, loads, and stays stable at runtime usually requires repeated review and debugging. Using a firmware-level development task as the background, this article walks through the full practice of “LLM-assisted code review and bug fixing.”

    Why Code Review Is the First Step of the Conversation

    When you are handed model-generated code that does not yet run, the most effective starting point is not to rush into edits, but to first perform a systematic code review.

    The concrete approach is to give the model a clear instruction:

    1. Please review this code, outline the overall structure (entry point, main loop,
    2. business logic, rendering, input handling), and list the problems that could
    3. cause compilation failures or runtime exceptions.

    In the ideal case, the model analyzes module by module and outputs a structured “problem list.” The value of this list lies in three things:

    1. Quickly locating risk points: It gathers scattered, hidden issues across multiple files into a single view, avoiding a line-by-line manual search.
    2. Setting expectations for changes: The review conclusions offer judgments about “what to change and why,” giving subsequent edits a clear basis.
    3. Reducing the cost of trial and error: Rather than blindly running and crashing repeatedly, it is better to eliminate some deterministic errors at the static level first.

    It is worth emphasizing that the review conclusions are only hypotheses; they must be verified through actual compilation and execution, not accepted wholesale.

    Why Debugging Low-Level Code Is Harder

    Unlike conventional application development, a firmware runtime environment (such as UEFI) has several special constraints, and these are the root of why debugging so often runs into frustration:

    • No operating system as a safety net: The program runs directly on bare metal or in a firmware environment. Once a null pointer or out-of-bounds access occurs, the result is often not an exception but an instant crash or a hang, with no readable stack trace.
    • Dependence on protocols and hardware abstraction: Capabilities such as graphics, input, and disk access are all obtained through standard protocols. Any small deviation in protocol location, initialization order, or memory lifetime will cause failures.
    • Limited debugging tools: Without a graphical debugger, you often can only rely on serial output to print logs and observe which step the program reached and where it crashed.

    These constraints mean that low-level debugging demands a “small steps, verify incrementally” strategy—which is exactly the way an LLM is good at cooperating.

    Practice Walkthrough: Two Rounds of Iteration to Get It Running

    The whole debugging process followed the industry-standard two-phase strategy—the first round gets the program running basically, and the second round refines and optimizes it. Each round advances through a closed loop of “find the problem → locate the root cause → fix → verify.”

    Round One: From “Looks Reasonable” to “Runs”

    After completing the code review and fixing a batch of deterministic errors, the program was finally able to load and enter the startup phase. But once it ran, two typical problems immediately appeared.

    Problem 1: Startup Crash Caused by a Dangling Pointer

    The symptom: the program could start, and after printing Starting Game Loop over serial, it exited immediately.

    By adding serial logs along the critical path and monitoring it segment by segment, the crash was confirmed to occur after the graphics protocol call. Further analysis revealed that the code obtained the frame buffer information before SetMode (setting the display mode), while SetMode reallocates the frame buffer memory. This invalidated the mode information pointer obtained earlier—accessing it afterward is a classic dangling (wild) pointer.

    1. // Wrong approach: querying mode information before SetMode
    2. gop->QueryMode(gop, mode, &size, &info);
    3. gop->SetMode(gop, mode);
    4. // After SetMode, the memory pointed to by info is no longer valid
    5. frameBuffer = (UINT8 *)info->FrameBufferBase; // Dangling pointer access, program crashes
    6. // Correct approach: query again after SetMode to get valid frame buffer information
    7. gop->SetMode(gop, mode);
    8. gop->QueryMode(gop, mode, &size, &info);
    9. frameBuffer = (UINT8 *)info->FrameBufferBase; // Valid address

    Root cause: Low-level memory resources have a clear lifetime. SetMode is a “write” operation that changes state, so any data that depends on that state must be re-fetched afterward. After the fix, the program no longer crashes.

    This case offers a general lesson to all low-level developers: for any call that “changes hardware or memory state,” the validity period of its return values may be invalidated as well. You must re-read them rather than caching and reusing them.

    Problem 2: Wrong Protocol GUID Configuration Causing the UI to Never Appear

    Once the program stopped crashing, the logic could run, but the graphical interface never displayed. After feeding this back to the model and analyzing it together with the runtime results, the problem was traced to the GUID location step of the graphics output protocol.

    In a firmware environment, capabilities are located and obtained through “protocol + GUID.” There are multiple graphics-related protocol GUIDs in the system; if the wrong GUID is used during location, LocateProtocol cannot find the correct graphics output protocol, and every subsequent drawing operation naturally fails.

    1. // Locate the graphics output protocol by its protocol GUID; the GUID must match the target protocol
    2. Status = gBS->LocateProtocol(
    3. &gEfiGraphicsOutputProtocolGuid, // Use the correct protocol GUID
    4. NULL,
    5. (VOID **)&gop
    6. );
    7. if (EFI_ERROR(Status) || gop == NULL) {
    8. // On failure, handle it explicitly and print the error code over serial
    9. return Status;
    10. }

    Root cause: The protocol identifier (GUID) is the single entry point; choosing the wrong constant points to an object that does not exist. After the fix and after adding proper error handling, the graphical interface rendered correctly.

    Round Two: Refinement and Optimization

    Only once the program ran stably did the second round begin. At this stage, the goal was no longer “does it run,” but “does it run well.” Typical work included:

    • Completing exception handling: Adding EFI_ERROR checks to all protocol calls that return a status code, to avoid silent failures.
    • Resource and memory management: Checking that frame buffers and buffer allocations are released in pairs, eliminating leaks.
    • Rendering performance: Reducing unnecessary full-screen refreshes and using dirty-rectangle updates to improve the frame rate.
    • Code readability: Splitting bloated functions and adding comments for key sections to lower future maintenance costs.

    Engineering Lessons for Debugging with LLMs

    Drawing on this practice, several reusable collaboration methods can be summarized:

    1. Review first, edit later. Have the model output a problem list first to form a global view, then tackle issues one by one, avoiding “treating the head when the head hurts.”
    2. Verify in small steps, advance in closed loops. Change only one or two problems at a time, then compile and run to verify immediately for fast convergence. Never pile up a large number of changes at once, or it becomes hard to attribute the problems.
    3. Make good use of debugging tools. In an environment without a graphical debugger, proactively add serial logs to critical paths to turn the “invisible black box” into an “observable execution trace,” and feed the logs back to the model verbatim.
    4. Provide sufficient context. The compilation environment, runtime environment, error messages, and approaches already tried must all be explained fully; the quality of the model’s judgments depends heavily on the quality of the context.
    5. Keep human judgment. Every suggestion from the model should be treated as a hypothesis to be verified. This is especially true for changes involving low-level memory, pointers, and lifetimes—always confirm them against actual runtime results.

    Summary

    Large models have made “generating code” cheap, but they have made the ability to “review and debug code” more critical. What truly determines the value of AI-assisted programming is not how many lines a model can write in one pass, but whether developers can build an iterative workflow of “review → locate → fix → verify” and understand the constraints of the low-level environment, such as memory lifetimes and protocol addressing. Treat the large model as an efficient but guided collaborator, combine it with solid engineering methods, and you can take fewer wrong turns on the path from “does not run” to “runs” and then to “ready to ship.”