-
Notifications
You must be signed in to change notification settings - Fork 4
Undefined Behavior when T is zero-sized #5
Comments
I also want to point out that this is very much NOT fine, as ALL references must be non-null. Lines 537 to 540 in fb1d8d1
Line 547 in fb1d8d1
|
Are you certain this is the case? This contradicts the nomicon:
|
Opened rust-lang/nomicon#198 |
Not at a computer, but I'm pretty sure if you look into the Unique::empty
implementation, it actually does create a properly aligned pointer. Feel
free to correct me if I'm wrong, but that is what I remember from looking
at it myself in the past.
…On Fri, Feb 7, 2020, 13:11 Nathan West ***@***.***> wrote:
I also want to point out that this is very much NOT fine, as ALL
references must be non-null.
I'm fixing this anyway, but this contradicts the nomicon
<https://doc.rust-lang.org/nomicon/vec-zsts.html#allocating-zero-sized-types>
:
So if the allocator API doesn't support zero-sized allocations, what on
earth do we store as our allocation? Unique::empty() of course! Almost
every operation with a ZST is a no-op since ZSTs have exactly one value,
and therefore no state needs to be considered to store or load them. This
actually extends to ptr::read and ptr::write: they won't actually look at
the pointer at all. As such we never need to change the pointer.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#5?email_source=notifications&email_token=AHDX4NSEZEJJ5WARXTU5KBTRBWP4BA5CNFSM4KRKXAE2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOELEARNY#issuecomment-583534775>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AHDX4NWBEGZUPN63JUHXHQ3RBWP4BANCNFSM4KRKXAEQ>
.
|
… On Fri, Feb 7, 2020, 13:11 Nathan West ***@***.***> wrote:
I also want to point out that this is very much NOT fine, as ALL
references must be non-null.
I'm fixing this anyway, but this contradicts the nomicon
<https://doc.rust-lang.org/nomicon/vec-zsts.html#allocating-zero-sized-types>
:
So if the allocator API doesn't support zero-sized allocations, what on
earth do we store as our allocation? Unique::empty() of course! Almost
every operation with a ZST is a no-op since ZSTs have exactly one value,
and therefore no state needs to be considered to store or load them. This
actually extends to ptr::read and ptr::write: they won't actually look at
the pointer at all. As such we never need to change the pointer.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#5?email_source=notifications&email_token=AHDX4NSEZEJJ5WARXTU5KBTRBWP4BA5CNFSM4KRKXAE2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOELEARNY#issuecomment-583534775>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AHDX4NWBEGZUPN63JUHXHQ3RBWP4BANCNFSM4KRKXAEQ>
.
|
Also from the nomicon https://doc.rust-lang.org/nomicon/exotic-sizes.html
Note that references to ZSTs (including empty slices), just like all other
references, must be non-null and suitably aligned. Dereferencing a null or
unaligned pointer to a ZST is undefined behavior
<https://doc.rust-lang.org/nomicon/what-unsafe-does.html>, just like for
any other type.
…On Fri, Feb 7, 2020, 13:26 Nathan West ***@***.***> wrote:
Opened rust-lang/nomicon#198
<rust-lang/nomicon#198>
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#5?email_source=notifications&email_token=AHDX4NWPCZC3DJDAIE4K3LLRBWRXDA5CNFSM4KRKXAE2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOELECHIQ#issuecomment-583541666>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AHDX4NTJL2FFNQQSMNFSBODRBWRXDANCNFSM4KRKXAEQ>
.
|
Here is an assembly comparison between Box and Stowaway using |
stowaway/src/lib.rs
Lines 212 to 217 in fb1d8d1
stowaway/src/lib.rs
Line 283 in fb1d8d1
Per ptr::read: Note that even if T has size 0, the pointer must be non-NULL and properly aligned.
https://doc.rust-lang.org/std/ptr/fn.read.html
The text was updated successfully, but these errors were encountered: