Skip to content

Comments

fix: Do not alias fields of tracked_struct Values when updating#741

Merged
Veykril merged 2 commits intosalsa-rs:masterfrom
Veykril:veykril/push-ooqmumwlopur
Dec 31, 2025
Merged

fix: Do not alias fields of tracked_struct Values when updating#741
Veykril merged 2 commits intosalsa-rs:masterfrom
Veykril:veykril/push-ooqmumwlopur

Conversation

@Veykril
Copy link
Member

@Veykril Veykril commented Feb 28, 2025

No description provided.

@netlify
Copy link

netlify bot commented Feb 28, 2025

Deploy Preview for salsa-rs canceled.

Name Link
🔨 Latest commit 9f47a9e
🔍 Latest deploy log https://app.netlify.com/projects/salsa-rs/deploys/6954d37d37ea520008702256

@Veykril Veykril force-pushed the veykril/push-ooqmumwlopur branch from 9bfedd4 to 0cf0cc7 Compare February 28, 2025 14:37
@codspeed-hq
Copy link

codspeed-hq bot commented Feb 28, 2025

CodSpeed Performance Report

Merging #741 will improve performance by 7.17%

Comparing Veykril:veykril/push-ooqmumwlopur (9f47a9e) with master (9b62468)

Summary

⚡ 2 improvements
✅ 11 untouched

Benchmarks breakdown

Benchmark BASE HEAD Efficiency
amortized[SupertypeInput] 3 µs 2.8 µs +6.2%
amortized[InternedInput] 2.2 µs 2 µs +7.17%

@Veykril Veykril changed the title Do not alias fields of tracked_struct Values when updating Do not alias fields of tracked_struct Values when updating Feb 28, 2025
@Veykril Veykril marked this pull request as ready for review February 28, 2025 14:58
@MichaReiser
Copy link
Contributor

Does this fix an unsoundness because it otherwise seems to regress perf a little bit.

@Veykril
Copy link
Member Author

Veykril commented Feb 28, 2025

I believe (if I understand the code correctly) this fixes a theoretical unsoundness when a user leaks a tracked struct value across threads. In that scenario we might try to acquire the read lock and write lock at the same time which could cause aliasing (before we inevitably panic).

The perf regression likely comes from taking the read lock in tracked_field now. I believe we are required to take the lock there as otherwise the access could alias (given the above scenario).

@Veykril
Copy link
Member Author

Veykril commented Feb 28, 2025

I'll see if we can craft a test that actually hits that lock setup resulting in a panic (to check if miri might be able to diagnose the theoretical issue)

@Veykril Veykril force-pushed the veykril/push-ooqmumwlopur branch from 8a61975 to aa3446d Compare March 1, 2025 12:33
@Veykril Veykril force-pushed the veykril/push-ooqmumwlopur branch 2 times, most recently from b9b5e91 to ba4f5c6 Compare March 6, 2025 14:47
@Veykril Veykril force-pushed the veykril/push-ooqmumwlopur branch from ba4f5c6 to b0680ac Compare March 6, 2025 14:50
@Veykril Veykril changed the title Do not alias fields of tracked_struct Values when updating fix: Do not alias fields of tracked_struct Values when updating Mar 10, 2025
@Veykril Veykril marked this pull request as draft March 15, 2025 11:09
@MichaReiser
Copy link
Contributor

I think I'm running into the aliasing bug with Red Knot's WASM based playground.

We see instances where it panics with

access to field whilst the value is being initialized

I rebased your PR and I'm no longer seeing the same panic (or it at least makes it much less likely to reproduce 😆)

I forgot the specifics of what's left. Should I take this PR from you?

@Veykril
Copy link
Member Author

Veykril commented Mar 18, 2025

Just whatever is left regarding the review comments I've put up I think, though I am not sure that what you observe would be fixed by this PR (unless we are in fact running into UB!). Feel free to take it over, I'm busy this week with a rust-analyzer refactor

@Veykril Veykril force-pushed the veykril/push-ooqmumwlopur branch 3 times, most recently from 84e9a6b to 3bc9d3a Compare May 27, 2025 10:55
@Veykril Veykril marked this pull request as ready for review May 27, 2025 11:01
@MichaReiser
Copy link
Contributor

MichaReiser commented May 29, 2025

@ibraheemdev you're probably the better reviewer for this than I.

@Veykril it seems that this PR uses some unstable language features. We could upgrade the MSRV if that helps (I think we're now on 1.85)

@Veykril
Copy link
Member Author

Veykril commented May 29, 2025

That would, iirc raw borrows were stabilized in 1.81

Copy link
Member

@ibraheemdev ibraheemdev left a comment

Choose a reason for hiding this comment

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

I think this code could be simplified by using an UnsafeCell on the fields that may be exclusively borrowed, to avoid the pointer casting. I did something similar with interned values. Do you mind if I pick this up?

@Veykril
Copy link
Member Author

Veykril commented Jun 2, 2025

Go ahead

@Veykril Veykril force-pushed the veykril/push-ooqmumwlopur branch from 3bc9d3a to ae59d83 Compare December 26, 2025 10:48
@Veykril Veykril force-pushed the veykril/push-ooqmumwlopur branch from ae59d83 to 8f4669f Compare December 26, 2025 10:53
@Veykril
Copy link
Member Author

Veykril commented Dec 26, 2025

Rebased and changed the revisions to be atomic. (things got simpler since last time I touched this PR)

@Veykril Veykril force-pushed the veykril/push-ooqmumwlopur branch from 09702f6 to 922f8d2 Compare December 26, 2025 11:25
@Veykril
Copy link
Member Author

Veykril commented Dec 26, 2025

I did not go with the UnsafeCell changes though, we can still do that later I guess.

@Veykril Veykril force-pushed the veykril/push-ooqmumwlopur branch from 922f8d2 to ce33fee Compare December 26, 2025 11:26
@Veykril Veykril force-pushed the veykril/push-ooqmumwlopur branch from ce33fee to c2a4c00 Compare December 30, 2025 08:23
@Veykril Veykril force-pushed the veykril/push-ooqmumwlopur branch from c2a4c00 to 9f47a9e Compare December 31, 2025 07:40
@Veykril Veykril enabled auto-merge December 31, 2025 07:40
@Veykril Veykril added this pull request to the merge queue Dec 31, 2025
Merged via the queue into salsa-rs:master with commit 77c296f Dec 31, 2025
12 checks passed
@Veykril Veykril deleted the veykril/push-ooqmumwlopur branch December 31, 2025 07:59
@github-actions github-actions bot mentioned this pull request Dec 31, 2025
@MichaReiser
Copy link
Contributor

I think this PR regressed memory usage for ty because, if I understand it correctly, we now store the memo_table pointer for each tracked struct. Is this necessary or could we retrieve the memo table on demand as we did before?

@Veykril
Copy link
Member Author

Veykril commented Jan 5, 2026

We stored it before too though

@Veykril
Copy link
Member Author

Veykril commented Jan 5, 2026

The Value and ValueWithMetadata struct split affects layouting though, so if you store a value with a big alignment I think this new setup will waste more space on unused padding. So might be good to check if we can undo that split again, which will make the code a bit more ugly though (or repr(packed) it?)

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.

4 participants