Skip to content

Fix try/catch control flow in eBPF and missing catch body in userspace #10

@SiyuanSun0736

Description

@SiyuanSun0736

Description

The current implementation of the try/catch mechanism contains two logic gaps that prevent examples/error_handling_demo.ks from executing correctly:

  1. eBPF Control Flow: In the eBPF backend, throw (via IRThrow) only assigned a value to the error variable but did not interrupt the execution of the try block. Subsequent instructions in the try block were still executed.
  2. Userspace Code Generation: The userspace backend generated a C switch statement with a placeholder comment (/* Handle error here */) instead of recursing into the actual instructions defined within the catch block.

Technical Breakdown

eBPF Backend (src/ebpf_c_codegen.ml)

  • Context Variable: Added current_catch_label to the c_context type.

  • Label Management:

  • IRTry now generates a unique catch_label and stores it in the context before processing the try instructions.

  • A label definition (label:) is now emitted after the try block but before the catch logic.

  • Jump Logic: IRThrow now emits a goto statement targeting the catch_label, ensuring immediate exit from the try block upon an error.

Userspace Backend (src/userspace_codegen.ml)

  • Recursive Generation: Replaced the static placeholder string in the catch case with a mapping of generate_c_instruction_from_ir over the catch_body list.
  • String Integration: The generated instructions are now concatenated and properly indented within the C case statement.

error_handling.patch


These changes ensure that:

  • In eBPF programs, a throw effectively skips the remainder of the try block.

  • In Userspace programs, the logic defined inside a catch block is actually compiled into the final C code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions