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

Reduce uses of uninitialized arrays #3261

Merged
merged 6 commits into from
Oct 20, 2023
Merged

Conversation

kornelski
Copy link
Contributor

I'm trying to remove UB of Aligned::uninitialized()#1572

@kornelski kornelski force-pushed the init-me-maybe branch 3 times, most recently from 3aecd94 to 39380df Compare October 3, 2023 21:28
@kornelski
Copy link
Contributor Author

CI fails due to #3263

@kornelski
Copy link
Contributor Author

Any comments for this PR? Is the direction good? Can it be merged despite the CI issue?

@lu-zero
Copy link
Collaborator

lu-zero commented Oct 8, 2023

Lots of fixes should land since they are good. I hope we'll fix the dav1d situation so we can have back the tests :/

@codecov
Copy link

codecov bot commented Oct 9, 2023

Codecov Report

Attention: 2 lines in your changes are missing coverage. Please review.

Files Coverage Δ
src/asm/shared/predict.rs 99.33% <100.00%> (-0.03%) ⬇️
src/asm/shared/transform/inverse.rs 100.00% <100.00%> (ø)
src/asm/x86/quantize.rs 98.62% <100.00%> (+0.01%) ⬆️
src/asm/x86/transform/forward.rs 97.84% <100.00%> (+0.01%) ⬆️
src/context/block_unit.rs 93.71% <100.00%> (-0.01%) ⬇️
src/context/mod.rs 91.00% <100.00%> (-0.09%) ⬇️
src/context/transform_unit.rs 89.78% <100.00%> (ø)
src/quantize/mod.rs 97.28% <100.00%> (+0.03%) ⬆️
src/transform/forward.rs 100.00% <100.00%> (ø)
src/transform/mod.rs 90.71% <100.00%> (+0.05%) ⬆️
... and 2 more

📢 Thoughts on this report? Let us know!.

Copy link
Collaborator

Choose a reason for hiding this comment

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

If all the changes in this PR were like this file, I would approve it in a heartbeat.
This is the pattern we should be following to avoid undefined behavior and double initialization.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't understand the comment. Does that mean other files than inverse.rs are not acceptable? What do I need to change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

BTW, I've wanted to use a pattern of:

let initialized = function(&mut maybe_uninit);

but that required reliably tracking the length inside the function, which would regress performance. This lead me to try some loop optimizations like #3262 but I found it wasn't always possible to have guaranteed length for free. Plus there's A LOT of uninitialized to deal with.

There are more functions using which I haven't converted yet, because I'm not actually sure they fully initialize their data!

Copy link
Collaborator

Choose a reason for hiding this comment

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

Firstly, thank you for detailing the current sites of potential undefined behavior. Everything that maintains the current intent can be merged. Any changes that replace uninitialized() with zero-fill should be dropped and can be addressed in a follow-up PR.

BTW, I've wanted to use a pattern of:

let initialized = function(&mut maybe_uninit);

I have recently tackled this issue in a toy project:
https://github.com/barrbrain/sudoku/blob/main/src/lib.rs#L27-L40
Some care is required with ownership semantics when crossing the unsafe boundary. This pattern is well defined:
https://github.com/barrbrain/sudoku/blob/main/src/lib.rs#L355-L356
I will see if we can add some infrastructure to ease converting the current code to well-defined.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Once MaybeUninit::uninit_array hits stable I guess a lot would be easier.
Probably we could change some Vec in Box<[]> to make more evident that the size is fixed meanwhile.

(also, thank you again for taking care of this :))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The only cases of zero-fill (I assume you mean from_fn(|| 0)) are in benchmarks, outside of the benchmarked section. I assume they're not important, since they're neither in the library, nor performance-critical. So I've just changed them for simplicity and determinism.

Copy link
Collaborator

@barrbrain barrbrain left a comment

Choose a reason for hiding this comment

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

Looks good to me. Sorry that I missed the zero-fill changes were limited to tests and benchmarks.

@barrbrain barrbrain merged commit 620d541 into xiph:master Oct 20, 2023
25 checks passed
@kornelski kornelski deleted the init-me-maybe branch October 20, 2023 14:34
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.

3 participants