Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cyclic import/export segfault #568

Merged
merged 2 commits into from
Oct 16, 2024
Merged

Conversation

bnoordhuis
Copy link
Contributor

Consider the following two files:

// a.js
import {f} from "b.js"
export {f}

And:

// b.js
import * as a from "a.js"
export function f() {}

Before this commit, it crashed with a nullptr dereference. Throw a "circular reference when looking for export" SyntaxError.

No test because the test suite currently isn't equipped for tests that throw exceptions at import time.

Fixes: #567

@malbarbo
Copy link

malbarbo commented Oct 1, 2024

Is throwing an exception the best approach here? The examples works in both node and deno (with extensions .mjs and ./ path in the import)

@richarddavison
Copy link

This is quite complicated. Node, deno, bun etc all handle the case in the issue. However they do not handle this case:

//index.js
import { a } from "./a.js";
console.log(a);

//a.js
import { b } from "./b.js";
export const a = "aloha";
console.log("from a", b);

//b.js
import { a } from "./a.js";
export const b = "bye";
console.log("from b", a);

The difference is when we export an object it should work as we can return the same reference, from b which later get's initialized in a as per Node.js documentation:

When main.js loads a.js, then a.js in turn loads b.js. At that point, b.js tries to load a.js. In order to prevent an infinite loop, an unfinished copy of the a.js exports object is returned to the b.js module. b.js then finishes loading, and its exports object is provided to the a.js module.

More info on this behavior is found here
https://nodejs.org/api/modules.html#cycles

@saghul
Copy link
Contributor

saghul commented Oct 2, 2024

The Node docs are not relevant here, what does the ES spec say?

@richarddavison
Copy link

richarddavison commented Oct 2, 2024

The Node docs are not relevant here, what does the ES spec say?

It's a bit hard to decipher but this seems relevant:
https://tc39.es/ecma262/#figure-module-graph-cycle

Also the issue use case also works in all major browsers

@saghul
Copy link
Contributor

saghul commented Oct 3, 2024

Thanks for the link! Still a bit confusing since your example and the original behave different but seem the same on the surface.

Hum, certainly throwing an error is a better solution than to crash, so there is that.

I haven't touched the import code much, so I'm not sure right off the bat how hard it would be to make this work the way the other platforms do.

It's also somewhat surprising test262 doesn't catch this :-/

@bnoordhuis
Copy link
Contributor Author

After lots of yak shaving to enable negative tests...

I'm going to land this first in order to fix the segfault (because SyntaxError > segfault) and then implement the proper behavior at some later time, because that's going to be a more elaborate change.

@bnoordhuis
Copy link
Contributor Author

ffs, the windows buildbots still trip up on output from tests...

The netbsd failure is this;

  tests/test_std.js:234: Error: assertion failed: got |0|, expected |15|

Which is a flake. Also happened regularly on cygwin, see #184.

@bnoordhuis
Copy link
Contributor Author

Argh.... fixing up the line endings by stubbing out the \r breaks some of the test262 tests. Okay, back to the drawing board.

Before this commit it segfaulted, now it throws a SyntaxError.
That's still not correct behavior but better than segfaulting.
To be continued.

Refs: quickjs-ng#567
@bnoordhuis
Copy link
Contributor Author

netbsd again... "VM is booting" for the last 13 minutes and counting. Everyting else is green though!

@bnoordhuis bnoordhuis merged commit 36227a5 into quickjs-ng:master Oct 16, 2024
51 of 52 checks passed
@bnoordhuis bnoordhuis deleted the fix567 branch October 16, 2024 08:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Segmentation fault with cyclic imports
4 participants