Skip to content

Segmentation fault when re-evaluating an ES module involved in a circular dependency #1320

@imzlh

Description

@imzlh

Title

Segmentation fault in js_create_module_function with circular ES module dependencies

Description

These notes are generated by claude. Maybe it's my problem? Please tell me how to?

Summary

QuickJS-ng crashes with a segmentation fault when calling eval() on ES modules with circular dependencies. The crash occurs because js_create_module_function receives a NULL pointer when recursively processing circular module dependencies.

Environment

  • QuickJS-ng version: git master
  • Platform: Linux
  • Project: circu.js(txiki.js fork)

Steps to Reproduce

  1. Create two modules with circular dependency:

a.ts:

import './b';
export const a = 'module a';

b.ts:

import './a';
export const b = 'module b';
  1. Load and evaluate module 'a' - works fine
  2. Load and evaluate module 'b' - crashes with SEGV

Expected Behavior

Circular module dependencies should be handled gracefully without crashing, similar to how Node.js and other JS engines handle circular imports.

Actual Behavior

Program crashes with segmentation fault:

Program received signal SIGSEGV, Segmentation fault.

0x000055555563bbca in js_create_module_function (ctx=0x555555763fd0, m=0x0)
    at /root/txiki.ts/deps/quickjs/quickjs.c:28852
28852       if (m->func_created)

Stack Trace

#0  0x000055555563bbca in js_create_module_function (ctx=0x555555763fd0, m=0x0)
    at /root/txiki.ts/deps/quickjs/quickjs.c:28852
#1  0x000055555563bcc9 in js_create_module_function (ctx=0x555555763fd0, m=0x55555580c840)
    at /root/txiki.ts/deps/quickjs/quickjs.c:28878
#2  0x000055555563bcc9 in js_create_module_function (ctx=0x555555763fd0, m=0x5555559f1960)
    at /root/txiki.ts/deps/quickjs/quickjs.c:28878
#3  0x000055555565100b in JS_EvalFunctionInternal (ctx=0x555555763fd0, fun_obj=..., 
    this_obj=..., var_refs=0x0, sf=0x0) at /root/txiki.ts/deps/quickjs/quickjs.c:35376
#4  0x0000555555651125 in JS_EvalFunction (ctx=0x555555763fd0, fun_obj=...)
    at /root/txiki.ts/deps/quickjs/quickjs.c:35394

Root Cause

the function js_create_module_function attempts to access m->func_created without checking if m is NULL:

if (m->func_created)  // CRASH: m is NULL here
    return m->func_obj;

When processing circular dependencies, the recursive call chain eventually passes a NULL module pointer, causing the segfault.

Additional Notes

  • Evaluating module 'a' works fine
  • Evaluating module 'b' (which depends on 'a', which depends on 'b') crashes
  • The issue occurs specifically when using module.eval() on modules with circular dependencies
  • Without calling eval(), the circular dependency itself doesn't cause issues

Workaround

Currently avoiding the use of eval() on modules with potential circular dependencies.

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