Skip to content

register_dynamic_module: error branches segfault when context==nullptr (Quiet not fully respected) #2580

@borisbat

Description

@borisbat

Description

register_dynamic_module(path, mod_name, on_error, Context*, LineInfoArg*) in src/builtin/module_builtin_fio.cpp:1097 accepts a Quiet mode (on_error=0) for callers that don't have a Context, but only one of three error branches actually checks the flag. The other two unconditionally call context->to_err(...), which segfaults when context==nullptr.

Branch Condition Quiet respected?
:1109-1117 dlopen failed ✅ yes (guarded by RegisterOnError::Quiet)
:1122-1130 registrator symbol __getModule_<X> missing ❌ unconditional context->to_err
:1134-1140 DAS_BUILD_ID mismatch ❌ unconditional context->to_err

Reproducer

Either the existing jit_register_dynamic_module(path, mod_name) (module_jit.cpp:1042) or the new jit_register_dynamic_module_resolve (PR #2579) calls register_dynamic_module with nullptr for both Context* and LineInfoArg* and on_error=0/*Quiet*/. A standalone exe whose .shared_module artifact is corrupt (missing registrator symbol) or built against a different DAS_BUILD_ID will hit one of the unguarded branches and segfault during startup, instead of returning nullptr cleanly.

Suggested fix

Guard both unconditional context->to_err(...) sites with the same RegisterOnError::Quiet check that the dlopen branch already uses, e.g.:

if (static_cast<RegisterOnError>(on_error) != RegisterOnError::Quiet) {
    context->to_err(at, err_msg.c_str());
}

Optionally also guard context==nullptr defensively before any context->to_err call so non-Quiet callers can't crash either.

Context

Surfaced by Copilot review on PR #2579 (discussion_r3189589151). Pre-existing latent bug, not introduced by that PR; deferred to a follow-up so the resolution-tier fix stays scoped.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions