Skip to content

fix(wasm): realloc should handle smaller new size#5234

Closed
trim21 wants to merge 7 commits intotree-sitter:masterfrom
trim21:fix/wasm32-malloc
Closed

fix(wasm): realloc should handle smaller new size#5234
trim21 wants to merge 7 commits intotree-sitter:masterfrom
trim21:fix/wasm32-malloc

Conversation

@trim21
Copy link
Contributor

@trim21 trim21 commented Jan 14, 2026

fix #5205

I know I said I won't spend time on it, but I tried LLM on it and it's amazing, it find this very easily and I think this is right. also tested locally in dprint it works as expected.

In realloc we didn't check the src size and dest size and truncate the memory in memcpy.

@trim21 trim21 force-pushed the fix/wasm32-malloc branch 2 times, most recently from bab781e to b196410 Compare January 14, 2026 19:18
@trim21

This comment was marked as outdated.

@WillLillis
Copy link
Member

Changes look good to me, but I'd like to spend some time testing them out. In the meantime, if you could try to come up with a test case so we could have some coverage here that would help a great deal. This can be especially difficult for wasm but it's worth a shot. You may be able to mimic the allocation pattern that causes the failure in a new fixture grammar's external scanner. There are some examples you can work off of in crates/cli/src/tests/wasm_language_tests.rs (in particular, loading a fixture grammar in test_load_fixture_language_wasm).

@trim21 trim21 force-pushed the fix/wasm32-malloc branch 2 times, most recently from 2ef3bb1 to b795182 Compare January 14, 2026 21:06
@trim21 trim21 changed the title fix(wasm32): when realloc, it should truncate the old memory region fix(wasm): realloc should handle smaller new size Jan 14, 2026
@trim21 trim21 force-pushed the fix/wasm32-malloc branch from b795182 to e6ad068 Compare January 14, 2026 21:08
@trim21
Copy link
Contributor Author

trim21 commented Jan 14, 2026

Changes look good to me, but I'd like to spend some time testing them out. In the meantime, if you could try to come up with a test case so we could have some coverage here that would help a great deal. This can be especially difficult for wasm but it's worth a shot. You may be able to mimic the allocation pattern that causes the failure in a new fixture grammar's external scanner. There are some examples you can work off of in crates/cli/src/tests/wasm_language_tests.rs (in particular, loading a fixture grammar in test_load_fixture_language_wasm).

Sorry, I can't reproduce this in testing

@trim21 trim21 requested a review from WillLillis January 14, 2026 22:44
@WillLillis
Copy link
Member

Changes look good to me, but I'd like to spend some time testing them out. In the meantime, if you could try to come up with a test case so we could have some coverage here that would help a great deal. This can be especially difficult for wasm but it's worth a shot. You may be able to mimic the allocation pattern that causes the failure in a new fixture grammar's external scanner. There are some examples you can work off of in crates/cli/src/tests/wasm_language_tests.rs (in particular, loading a fixture grammar in test_load_fixture_language_wasm).

Sorry, I can't reproduce this in testing

I'll spend some time trying to come up with one when I have a chance then.

@WillLillis
Copy link
Member

CC @maxbrunsfeld if you have a minute. Are the free calls added to realloc here ok? I'm assuming they were left out because the free implementation pre- 69c4245 didn't re-use free'd blocks in most cases, so it made sense to "leak" then. Given that's been updated, is there any reason to not free when appropriate in realloc now?

@trim21
Copy link
Contributor Author

trim21 commented Jan 15, 2026

CC @maxbrunsfeld if you have a minute. Are the free calls added to realloc here ok? I'm assuming they were left out because the free implementation pre- 69c4245 didn't re-use free'd blocks in most cases, so it made sense to "leak" then. Given that's been updated, is there any reason to not free when appropriate in realloc now?

Since #5205 (comment) I never be able to connect the dots how wasm32 grammer file works, do you create a new wasm instance for each parsing so free in realloc is not necessary?

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.

malloc for rust wasm32 target is buggy

3 participants