replace a lock xchg with AtomicU8::swap #64
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
i tripped over this crate after looking at usdt (unfortunately, eBPF USDT and DTrace USDT are different things, and so this crate turned out not so useful for me :) ) - one of the things that stuck out is that some of the
asm!()use doesn't seem so necessary. so i've tugged on the threads a bit and this is the first/simplest/easiest thing to fall out.while this is about equivalent, it makes an unsoundness a little
more apparent: the
&u8to be swapped into is a shared reference, e.g.Rust can expect that it is not going to be mutated. we will mutate the
referent by
AtomicU8::swap'ing it. there was a comment here talkingabout using
AtomicU8::from_mutto get anAtomicU8which would haveexposed the same issue. the
databeing modified needs to be&mut [u8]to make any impl of this function sound w.r.t borrowing rules.
now, i'm not making that change in this commit, and here's why:
read_and_update_record_versionis called by ..process_probe_record, which is called by ..process_section, which is called by ..extract_probe_records_from_section. this function talks aboutstoring symbols and data in a mutable section so it's loaded in a way we
can mutate, so the
from_raw_partshere could defensibly replacedwith a
from_raw_parts_mutand get us towards a&mut [u8]-takingprocess_section...dusty::main::probe_records. this is a bigger problem. thisreads a file (sure, could be made
mut data), then usesobjecttoparse the object and find a section containing dtrace probes.
objectis not designed to be handing out mutable refs to sections, or even
mutate the object being parsed at all. we could lean into the
unsoundness and "errmmm trust me" of what's happening here, transmute
the section to
&mut, and hope that this generally continues working.it probably will?
but before doing this i'd like to at least discuss options a bit :)
full disclosure: i don't have dtrace on my linux system, and don't have an alternate setup with dtrace support immediately handy. so i've run the in-crate tests to the extent i can (i can't
sudo dtrace, for example!) and those seem to pass. it could be that switching from thelock xchgasm to anAtomicU8lets rustc move some code around that makes this before/after less equivalent than it should be, due to the unsoundness of mutating data in a&[u8].