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

Allow symbols as WeakMap and WeakSet keys #58

Merged
merged 1 commit into from
Nov 16, 2023

Conversation

bnoordhuis
Copy link
Contributor

This is not a proper fix but it shows why #56 crashes.

map_add_record() treat the key as a JSObject when it can be a JSAtomStruct/JSString. As a quick hack I added the necessary struct JSMapRecord to JSString.

I'll think about this some more tomorrow, it's late now. :-)

@bnoordhuis bnoordhuis marked this pull request as ready for review November 13, 2023 22:56
@bnoordhuis
Copy link
Contributor Author

bnoordhuis commented Nov 13, 2023

Is github having trouble? I'm pretty sure there aren't conflicts. The CI doesn't start either. - it picked the wrong merge base somehow

@bnoordhuis bnoordhuis changed the base branch from weakmap-symbol to master November 13, 2023 22:59
@saghul
Copy link
Contributor

saghul commented Nov 14, 2023

Ah nice! I think you accidentally added support for putting them in a weakest too!

@saghul
Copy link
Contributor

saghul commented Nov 14, 2023

The PR LGTM as is. A thought: looking at implementing WeakRef and FinalizationRegistry we'd likely need to move away from the current model to accomodate that I think.

@bnoordhuis
Copy link
Contributor Author

Yes, I think so too. What I don't like about this PR is that it makes JSString bigger by an otherwise unused pointer.

Branching on the tag to get the field offset is kind of yuck too although that could be fixed by unifying the JSObject and JSString header, but that puts a mostly unused field in precious small offset-from-base territory.

@bnoordhuis bnoordhuis changed the title [wip] weakmap symbol keys Allow symbols as WeakMap and WeakSet keys Nov 14, 2023
quickjs.c Show resolved Hide resolved
Copy link
Contributor

@saghul saghul left a comment

Choose a reason for hiding this comment

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

Sweet!

@bnoordhuis
Copy link
Contributor Author

I don't think the asan bot is wrong to complain. I'll investigate more tomorrow.

@bnoordhuis
Copy link
Contributor Author

Diagnosis: JS_FreeRuntime calls free_object, which then calls JS_FreeAtomStruct before js_map_finalizer, resulting in a UAF when js_map_finalizer calls delete_weak_ref.

Atoms probably need to add themselves to rt->gc_zero_ref_count_list first now, just like JSObjects do.

@saghul
Copy link
Contributor

saghul commented Nov 15, 2023

That does sound like it would fix the dump free issue too maybe?

@bnoordhuis
Copy link
Contributor Author

Nah, turns out the bug was mine, I missed a delete_weak_ref call somewhere. The !atom_is_free(p) bug is still at large but symbols-in-weak{maps,sets} seem to be working now.

Copy link
Contributor

@saghul saghul left a comment

Choose a reason for hiding this comment

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

Awesome stuff!

@bnoordhuis bnoordhuis merged commit d2e632e into quickjs-ng:master Nov 16, 2023
16 checks passed
@bnoordhuis bnoordhuis deleted the weakmap-symbol branch November 16, 2023 08:08
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.

2 participants