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

Appease miri #515

Merged
merged 12 commits into from Nov 7, 2021
Merged

Appease miri #515

merged 12 commits into from Nov 7, 2021

Conversation

Noah-Kennedy
Copy link
Contributor

@Noah-Kennedy Noah-Kennedy commented Nov 6, 2021

Rewrote the ledger in test_bytes_vec_alloc.rs to not piss off miri. The ledger is now a table within the allocator, which seems to satisfy miri. The old solution was to bundle an extra usize into the beginning of each allocation and then index past the start when deallocating data to get the size.

There is one janky bit here with a transmute, but there isn't a better way to initialize an array with zeroed data in a const context that I am aware of sadly. Thanks to @paolobarbolini for finding a better way to do this!

@Noah-Kennedy Noah-Kennedy marked this pull request as ready for review November 6, 2021 20:54
@paolobarbolini
Copy link
Contributor

Looks like the nightly job is failing because of the very old version

nightly: nightly-2021-04-13

@Noah-Kennedy
Copy link
Contributor Author

Yeah that would do it

@Noah-Kennedy
Copy link
Contributor Author

Noah-Kennedy commented Nov 6, 2021

I'll change it to a more recent one

@Noah-Kennedy
Copy link
Contributor Author

Fixed an issue with the tests that was causing CI to hang (not sure why it didn't fail outright).

Copy link
Contributor

@paolobarbolini paolobarbolini left a comment

Choose a reason for hiding this comment

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

Since this PR already captured my attention, even though I'm not an official reviewer, I saw the const fn limitation and there actually is a simpler way of constructing an array of Copy values

Comment on lines 18 to 20
// equivalent to size of (AtomicPtr<u8>, AtomicUsize), hopefully
#[cfg(target_pointer_width = "64")]
let tricky_bits = 0u128;
Copy link
Contributor

Choose a reason for hiding this comment

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

Stupid GitHub UI not allowing suggestions across deleted lines

Suggested change
// equivalent to size of (AtomicPtr<u8>, AtomicUsize), hopefully
#[cfg(target_pointer_width = "64")]
let tricky_bits = 0u128;

Comment on lines 22 to 29
#[cfg(target_pointer_width = "32")]
let tricky_bits = 0u64;

let magic_table = [tricky_bits; 512];

// i know this looks bad but all the good ways to do this are unstable or not yet
// supported in const contexts (even though they should be!)
let alloc_table = unsafe { mem::transmute(magic_table) };
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
#[cfg(target_pointer_width = "32")]
let tricky_bits = 0u64;
let magic_table = [tricky_bits; 512];
// i know this looks bad but all the good ways to do this are unstable or not yet
// supported in const contexts (even though they should be!)
let alloc_table = unsafe { mem::transmute(magic_table) };
const ELEM: (AtomicPtr<u8>, AtomicUsize) =
(AtomicPtr::new(null_mut()), AtomicUsize::new(0));
let alloc_table = [ELEM; 512];

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 problem is that they aren't Copy according to Rustc

Copy link
Contributor

Choose a reason for hiding this comment

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

It appears to work correctly if you put the element in a const, like I did in the suggestion

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Huh

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'll try that once this CI run finishes

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, works

@Noah-Kennedy
Copy link
Contributor Author

Still failing, weird, works on my machine.

.compare_exchange(ptr, null_mut(), Ordering::SeqCst, Ordering::SeqCst)
.is_ok()
{
return entry_size.swap(0, Ordering::Relaxed);
Copy link
Contributor

Choose a reason for hiding this comment

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

This swap might be problematic. Using load should be enough. The entry_size will we overwritten when the next allocation is done

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How so?

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 realize it isn't needed because we already zero the ptr, but I don't see how this would cause issues.

Copy link
Contributor

Choose a reason for hiding this comment

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

This is a test so after all going down this level of detail might not make sense, but I think it's totally possible that between the compare_exchange and the swap another thread allocates. swap could then zero the entry_size of the next allocation.
IDK just mentioned it because I also noticed the hang with the CI, it would probably never happen with this very small test

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, good catch! The hang was because I wasn't removing it because I wasn't handling realloc cases correctly.

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 think even a load would still have issues, as we could read the allocation from the other thread. I suppose setting this to the max value would be better.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Might wanna make the ledger slightly bigger just in case.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think even a load would still have issues, as we could read the allocation from the other thread

True 😃

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I think this should be fixed in my latest commit!

@Noah-Kennedy
Copy link
Contributor Author

rustfmt will be the death of me

@Noah-Kennedy
Copy link
Contributor Author

@paolobarbolini thanks for the review and help!

Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

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

This seems fine to me.

tests/test_bytes_vec_alloc.rs Outdated Show resolved Hide resolved
Co-authored-by: Alice Ryhl <alice@ryhl.io>
@Noah-Kennedy Noah-Kennedy merged commit ba5c5c9 into master Nov 7, 2021
@Noah-Kennedy Noah-Kennedy deleted the noah/appease-miri branch November 7, 2021 16:23
lelongg pushed a commit to lelongg/bytes that referenced this pull request Jan 9, 2023
Rewrote the ledger in test_bytes_vec_alloc.rs to not piss off miri. The ledger is now a table within the allocator, which seems to satisfy miri. The old solution was to bundle an extra usize into the beginning of each allocation and then index past the start when deallocating data to get the size.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants