Skip to content

Port recent OOM-handling and memory leak fixes from bellard/quickjs#1069

Merged
bnoordhuis merged 2 commits intoquickjs-ng:masterfrom
past-due:bellard_oom_handling_1
May 26, 2025
Merged

Port recent OOM-handling and memory leak fixes from bellard/quickjs#1069
bnoordhuis merged 2 commits intoquickjs-ng:masterfrom
past-due:bellard_oom_handling_1

Conversation

@past-due
Copy link
Copy Markdown
Contributor

@past-due past-due commented May 25, 2025

Fixes #1066

Ports:
bellard/quickjs@db3d3f0
bellard/quickjs@3dc7ef1 (except changes to build_backtrace)

@past-due past-due changed the title Port recent OOM-handling and memory leak fixes from upstream May 25, 2025
Copy link
Copy Markdown
Contributor

@bnoordhuis bnoordhuis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One comment, otherwise LGTM.

quickjs.c Outdated
Comment on lines -6966 to -6964

if (add_backtrace)
build_backtrace(ctx, obj, JS_UNDEFINED, NULL, 0, 0, 0);
}
if (add_backtrace)
build_backtrace(ctx, obj, JS_UNDEFINED, NULL, 0, 0, 0);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change looks kind of dubious to me. Ceteris paribus, it's better to have a backtrace.

Copy link
Copy Markdown
Contributor Author

@past-due past-due May 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the reason this was part of the upstream commit is that all but one (?) of the reasons JS_NewString (JS_NewStringLen) might fail are due to an allocation / OOM situation.

EDIT: And since the case of an invalid message is already handled above:

    msg = JS_NewString(ctx, message);
    if (JS_IsException(msg))
        msg = JS_NewString(ctx, "Invalid error message");

The only case where msg is still an exception at this point should be if JS_NewStringLen failed due to allocation failure (OOM). Avoiding the call to build_backtrace in this case seems reasonable (it looks like it tries to allocate plenty of things itself)?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My thinking is that it's better to try and fail than not try at all, so can you move it outside the block again?

Copy link
Copy Markdown
Contributor Author

@past-due past-due May 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My thinking is that it's better to try and fail than not try at all, so can you move it outside the block again?

Done. 👍

Of note: Since it has diverged from bellard/quickjs, I did not fully audit build_backtrace to ensure it properly handles all memory allocation failures internally - would be worth looking at in the future.

Port changes from bellard/quickjs@3dc7ef1 by Fabrice Bellard (except changes to build_backtrace)

Co-Authored-By: Fabrice Bellard <fabrice@bellard.org>
@past-due past-due force-pushed the bellard_oom_handling_1 branch from d083b48 to d221d5a Compare May 26, 2025 15:48
@bnoordhuis bnoordhuis merged commit 3c78c95 into quickjs-ng:master May 26, 2025
127 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants