Skip to content

Conversation

@RalfJung
Copy link
Member

No description provided.

@RalfJung
Copy link
Member Author

r? @Centril

Copy link
Contributor

@Centril Centril left a comment

Choose a reason for hiding this comment

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

This looks good but I have one question.

Safe code need not worry about ZSTs, but *unsafe* code must be careful about the
consequence of types with no size. In particular, pointer offsets are no-ops,
and standard allocators may return `null` when a zero-sized allocation is
requested, which is indistinguishable from the out of memory result.
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you say a bit about why the bit about "indistinguishable from the OOM result" is being nixed?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it's plain wrong. OOM is well-defined and we call the OOM handler; asking the allocator for a zero-sized allocation is UB.

Copy link
Contributor

Choose a reason for hiding this comment

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

clarification for historians: in C(++?) it's "only" implementation-defined to pass 0 to malloc: https://en.cppreference.com/w/c/memory/malloc

Since all the major allocators target this API, they all have this semantic. I'm guessing you're saying that the Rust interface makes a stricter claim? (which is fine by me, since the implementation-defined-ness might as well be UB in practice).

Copy link
Member Author

Choose a reason for hiding this comment

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

This is my understanding of the Rust docs, yes. I do not know the motivation for why we say UB where C++ says impl-defined.

Cc @pnkfelix @sfackler @alexcrichton @gnzlbg

Copy link
Contributor

@gnzlbg gnzlbg Oct 10, 2019

Choose a reason for hiding this comment

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

C and C++ don't support ZSTs so the behavior of malloc(0) isn't really relevant there as long as nothing breaks if that ever happens, which doesn't happen often in C, and probably almost never in C++ (new T[0] doesn't call malloc(0) since the array needs a unique address and has non-zero size even though it has zero elements..).

In Rust, code that deals with ZSTs often ends up doing completely different things for the zero-sized and non-zero-sized cases. For the zero-sized case the answer is almost never malloc(0) because of, e.g., alignment requirements.

So the reason GlobalAlloc does not support zero-size allocations is probably that there just weren't any compelling use cases for doing so; I can't at least think of any.

@Centril Centril merged commit 0f469dc into rust-lang:master Sep 14, 2019
@RalfJung RalfJung deleted the zst branch September 15, 2019 12:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants