This is an archive of the discontinued Mercurial Phabricator instance.

rust-nodemap: input/output primitives
ClosedPublic

Authored by gracinet on Jan 6 2020, 2:25 PM.

Details

Summary

These allow to initiate a NodeTree from an immutable opaque
sequence of bytes, which could be passed over from Python
(extracted from a PyBuffer) or directly mmapped from a file.

Conversely, we can consume
a NodeTree, extracting the bytes that express what
has been added to the immutable part, together with the
original immutable part.
This gives callers the choice to start a new Nodetree.
After writing to disk, some would prefer to reread for
best guarantees (very cheap if mmapping), some others will
find it more convenient to grow the memory that was considered
immutable in the NodeTree and continue from there.

This is enough to build examples running on real data and
start gathering performance hints.

Diff Detail

Repository
rHG Mercurial
Branch
default
Lint
No Linters Available
Unit
No Unit Test Coverage

Event Timeline

gracinet created this revision.Jan 6 2020, 2:25 PM
gracinet updated this revision to Diff 19137.Jan 10 2020, 10:03 AM
gracinet updated this revision to Diff 19329.Jan 15 2020, 6:48 PM
kevincox requested changes to this revision.Jan 16 2020, 6:24 AM
kevincox added inline comments.
rust/hg-core/src/revlog/nodemap.rs
268

This makes me uncomfortable. There is really no guarantee that there is no padding around Block or RawElement. If there is any padding this is UB. I would much prefer one of:

  1. Store growable as a set of bytes and convert on get/set.
  2. Add a method that outputs the bytes to any Write. In theory this will be slower but it is probably immaterial (especially if there is no padding).
272

If someone panics between the from_raw_parts and mem::forget this is a double free. Right now I think this can't happen but it is completely possible that the code changes between then and now. I would prefer using Vec::from_raw_parts to make sure that there is only one Vec alive with that pointer at a time. This risks a leak in the face of panic but I think that is much better.

433

This is a weird API. Why does new take a buffer and an offset? Can it take either a slice or just remove the offset parameter and always have the buffer start at 0? It is very strange to be passing in data that we own but isn't ever used.

This revision now requires changes to proceed.Jan 16 2020, 6:24 AM
kevincox added inline comments.Jan 16 2020, 7:10 AM
rust/hg-core/src/revlog/nodemap.rs
268

I thought about this more and I think I am okay doing it this way. It seems like this should be well defined as long as there is no padding. However on that note I would want to add a check that there is no padding as expected. I would also like to ensure that this fails to compile if there is ever padding, even in release mode. I think this can be accomplished by something like:

let _: [u8; 4 * BLOCK_SIZE] = std::mem::transmute([Block::new(); 4]);

I would probably want to repeat this check near any code that is relying on this invariant.

@kevincox (replying hastily because I'm also finishing something unrelated today).
Doesn't mem::size_of guarantee to take any padding into account? At least that's what the doc seems to say: https://doc.rust-lang.org/std/mem/fn.size_of.html

Ah yes. So my test would be ineffective.

The problem that IIUC padding is not guaranteed to be initialized, and in rust all values need to be initialized. So if you cast a type with padding to a u8 it is undefined behaviour.

So we would need a test that shows that the type is the same size as the sum of the contained i32s.

Maybe what we should do is keep the test I proposed, but change the definition of BLOCK_SIZE to be based upon the number of contained i32 instead of size_of.

gracinet edited the summary of this revision. (Show Details)Jan 27 2020, 10:50 AM
gracinet updated this revision to Diff 19636.
gracinet marked 2 inline comments as done.Jan 27 2020, 11:07 AM
gracinet added inline comments.
rust/hg-core/src/revlog/nodemap.rs
268

About a method to output to a writer: for the time being, we're avoiding doing I/O directly in Rust because most of it is shared with Python through layers that perform various sanitizations, lock management etc. That's why the simplest is to transfer bytes out.

Context note: the data structure (Block) is directly taken from the C version, which is non-persistent, but I believe that these 64 bytes are just some low-level programmer reflex. It's probably not a coincidence that IIRC that's also the length of cache lines on most current CPUs.

Anyway, the point of all of this is to get on disk without padding, so yes, we could implement our own non-padding by changing the definition of Block to [u8; 64]. In the end, we are forced to trust what's on disk anyway.

272

Not sure to understand what you suggest here, since I'm already using Vec::from_raw_parts in that method. I couldn't find an into_raw_parts.
Anyway, the official example has the mem::forget before Vec::from_raw_parts. Would that be sufficient?

I agree that a leak upon some exceptional-cannot-happen condition is better than a double free.

Also, forgot to came back to that one in the latest phab update

433

Owning some memory and not using the whole of it is anyway what happens when that memory region is just obtained from a mmap (which is the whole point of carrying theses Deref all around).
Technically, in a mmap, I suppose we actually only own the file descriptor and the resulting pointer, but we can't just convert it to an inert slice.

Anyway it's now confirmed that we won't be needing the offset finally, so I've removed it. Some data at the end of the bytes slice may be ignored, too : it would be the result of aborted transactions.

Note: the Python layer will maintain the amount of validated blocks in a separate small file which is updated atomically. Any future reimplementation of this in Rust would have to do the same.

I don't see anything wrong in this changeset, but there have been a lots of exchange between @kevincox and @gracinet. @kevincox Is there still code you are unconfortable with?

marmoute updated this revision to Diff 20025.Feb 8 2020, 12:44 PM
durin42 requested changes to this revision.Feb 12 2020, 11:55 AM

I'm just not comfortable with the unsafe block here, especially with the comments from @kevincox . That said, if the unsafe can't disappear, it probably needs a pretty thorough explanation of why it's correct for a novice rust reader to follow?

This revision now requires changes to proceed.Feb 12 2020, 11:55 AM

@kevincox To re-iterate, I've taken over this series, so excuse any additional inaccuracies.

rust/hg-core/src/revlog/nodemap.rs
268

@kevincox @durin42: I've changed the code to use the type-level ManuallyDrop instead of mem::forget, added the transmute to be extra paranoid and added a comment.

Alphare updated this revision to Diff 20215.Feb 14 2020, 6:02 AM
kevincox added inline comments.Feb 14 2020, 6:49 AM
rust/hg-core/src/revlog/nodemap.rs
268

This doesn't address the primary problem of padding. There is nothing in the code that guarantees that there will be no padding inserted into Block (although it is unlikely to ever happen). This is because if there is padding it won't be guaranteed to be initialized and reading uninitialized data is undefined behaviour (not to mention that it will give incorrect results). I would like to add something that guarantees this. The only thing I can think of that will give us this confidence is a check sizeof::<Block>() == 4 * 16. I was suggesting the transmute to assert this (since transpose checks the size of its type arguments are the same. However as I originally suggested and as currently written it doesn't do that.

My current proposal is change BLOCK_SIZE to be defined as 4 * 16 (possibly extracting that 16 into a ELEMENTS_PER_BLOCK) and keep the current transmute assertion. That paired with some comments explaining why we are doing that will make me confident that there is no padding and we aren't performing any undefined behaviour.

272

Ah, is missed that into_raw_parts is nightly-only at the moment. I think the forget is sufficient for now. Maybe leave a TODO to use into_raw_parts once stabilized.

yuja added a subscriber: yuja.Feb 14 2020, 8:19 AM

+ ) -> (Box<dyn Deref<Target = [Block]> + Send>, Vec<u8>) {
+ let (readonly, vec) = self.into_readonly_and_added();
+ Prevent running v's destructor so we are in complete control
+
of the allocation.
+ let vec = mem::ManuallyDrop::new(vec);
+
+ let bytes = unsafe {
+ This is safe because we check at compile-time that there is no
+
padding.
+ // /!\ Any use of vec after this is use-after-free.
+
+ let _: [u8; 4 * BLOCK_SIZE] =
+ std::mem::transmute([Block::new(); 4]);
+ Vec::from_raw_parts(
+ vec.as_ptr() as *mut u8,
+ vec.len() * BLOCK_SIZE,
+ vec.capacity() * BLOCK_SIZE,
+ )

Appears that this is unsafe. The doc states that the source type must have the
exact same alignment as Vec<u8> probably because the allocator may use
separate bucket per alignment.

https://doc.rust-lang.org/std/vec/struct.Vec.html#method.from_raw_parts

"It's also not safe to build one from a Vec<u16> and its length, because the
allocator cares about the alignment, and these two types have different alignments."

Can't we instead implement as_bytes() -> &[u8]?

Alphare added inline comments.Feb 14 2020, 12:26 PM
rust/hg-core/src/revlog/nodemap.rs
268

I think that would be sufficient indeed. How about making Block into a [u8; 64]? It will be less expressive, but this will no longer be needed.

kevincox added inline comments.Feb 14 2020, 12:34 PM
rust/hg-core/src/revlog/nodemap.rs
268

I like that. It makes it much more explicit that it is a backing array and forces us to have proper endiness hygiene.

I'm also willing to assume that [u8; 64] will be 64 bytes with no padding forever.

Alphare updated this revision to Diff 20235.Feb 15 2020, 6:49 AM
kevincox accepted this revision.Feb 24 2020, 5:44 AM
kevincox added inline comments.
rust/hg-core/src/revlog/nodemap.rs
141

A comment why 255 is a good number would be nice.

273

This should be 4 * BLOCK_SIZE to match the code below.

Alphare updated this revision to Diff 20281.Feb 24 2020, 8:14 AM
gracinet marked an inline comment as done.Feb 26 2020, 10:32 AM
This revision was not accepted when it landed; it landed in state Needs Review.
This revision was automatically updated to reflect the committed changes.