-
Notifications
You must be signed in to change notification settings - Fork 121
Implement nom::Offset
on BitPtr
#21
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
Comments
The
Is there some documentation on what you are doing here and why you are deliberately causing UB? Basically I am curious if there is something you want to do that is just not possible UB-free with the current language; that would be interesting input for the future development of our UB spec. |
Did not realize GitHub would retarget those, cool. I mangle raw pointers using bit masks when I know that there are low bits in the pointer available for use, and crash the program when there aren't. The last time I ran Miri, it complained about my use of integer bit operations on pointers to (a) look at the low bits, and (b) shove other bits in there. The document Bit Patterns describes the encoding scheme; encoding is implemented in I've written most of a draft for defining pointer encodings that I intend to submit to the Unsafe Code Working Group after I review it again. I'm honestly uncertain if my creation of |
Ah, I see. That would not have been a UB issue then, just with Miri not supporting int-ptr-casts yet. They have been implemented in the mean time, so I encourage you to try again. :) (But also keep in mind that Miri does not detect all UB, just a lot of it.)
All right. :)
I see the EDIT: Ah, it is also a ZST. That should help, yes. :) Not sure if it is enough, there seems to be quite a few coding tricks going on here. |
I have filed #916 on Miri, which will (when landed) allow me to run |
Those errors were likely not indicating UB but just operations Miri does not support. The error messages are not great at telling these two cases apart. |
Done |
Feature request by @fasterthanlime
Implementation notes:
pointer-to-pointer arithmetic is, according to C++ and thus likely LLVM,
defined only between two pointers known to be in the same allocation region.
(article 1) I do not know at this time what Miri thinks of pointer-to-pointer
arithmetic, but I strongly suspect, given Miri’s implementation of pointers in
its evaluator model (article 2) that this is suspect at best and a candidate
for rejection at worst.
Given that the rest of the crate is also UB in Miri that the compiler merely
happens to not yet reject, I am not too concerned about adding yet more
pointer operations that cause errors in Miri.
See @RalfJung’s blog article (article 3) for more about how Miri (and by
extension, eventually, rustc) treats pointer manipulation.
BitPtr
-to-BitPtr
arithmetic is required by the constraints above to onlybe meaningful within the same allocation region, and thus, with the same type.
This means that the function signature is not required to generalize as
but may be kept specific as just
We cannot enforce this in the type system, but this is an
unsafe fn
with theprecondition that
other
andself
be derived from the same overarchingBitSlice<_, T>
region. It is firmly undefined behavior, even by theUB-adjacent standards of this crate, to call
ptr_diff
with pointers from twodifferent regions.
Prior art in
fn store::BitStore::offset
indicates that the return typeshould be the anonymous record
{ elts: isize, bits: i8 }
(canonicalized asthe tuple
(isize, i8)
). That function produces(isize, BitIdx)
because itcomputes a jump value for
ptr::offset
and an absolute bit index in theelement to which the jump value refers. This function produces a jump value,
and a bit distance between the start pointer
self
and the end pointerother
.BitPtr<T: BitStore>
is notably missing aC: Cursor
type parameter. It isdefined behavior for two
BitSlice<C: Cursor, T: BitStore>
handles drawn fromthe same region to have different
C
type parameters. How should this behandled?
BitPtr::ptr_diff
can only computeBitIdx
differences. This willnecessarily produce a bit distance that describes the index difference
rather than the electrical difference, but, this is unavoidable with the
information available.
could convert both
self
andother
’sBitIdx
values toBitPos
usingtheir provided
Cursor
implementations, then constructBitPtr<T>
pointersfrom the
BitPos
values and callptr_diff
on them. This would compute theelectrical bit distance between the two
BitSlice
handles.Nom integration notes
bitvec
currently relies on behavior that is compiled as expected in currentrustc
but is marked as UB in Miri, and will likely eventually be rejected bythe compiler. For this reason, I am hesitant to encourage the
nom
crate tobegin depending on
bitvec
for its bit-stream parsing.However, it is advantageous to have
BitSlice
integrate withnom
’s traits,so that clients who wish to use
bitvec
for bitstreamnom
parsing are ableto drop
BitSlice
into any trait-drivennom
functions.Once
bitvec
depends onnom
, it becomes illegal fornom
to also depend onbitvec
. I don’t know the dependency rules offhand, but I am hoping that ifbitvec
depends onnom
optionally, the default feature set ofbitvec
isdependable by
nom
without creating a dependency cycle.If
nom
elects to depend onbitvec
,bitvec
’s nom integration will beremoved and placed in
nom
instead. This may constitute a major-breakingchange if I release a
1.0
.The text was updated successfully, but these errors were encountered: