We have all used Box<T> before in our Rust code. It’s a glorious type, with great ergonomics and flexibitility. We can use it to put our values on the heap, but it can do even more than that!

struct Fields {
@@ -88,8 +88,8 @@ borrow stack of the byte that was accessed. This is something about stacked borr
 that is implemented in Miri. For an excellent introduction, see this part of the great book Learning Rust With Entirely Too Many Linked Lists.

In short: each pointer has a unique tag attached to it. Each byte in memory has its own ‘borrow stack’ of these tags, and only the pointers that have their tag in the stack are allowed to access it. Tags can be pushed and popped from the stack through various operations, for example borrowing.

In the code example above, we get a nice little hint where the tag was created. When we created a reference (that was then coerced into a raw pointer) from our box, it got a new tag called <3314>. Then, when we moved the box into the function, -something happened: The tag was invalidated and popped off the borrow stack. That’s because box invalidates all tags when it’s -moved. The tag was popped off the borrow stack and we tried to read from it anyways - undefined behaviour happened!

And that’s how our code wasn’t a miscompilation, but undefined behaviour. Quite surprising, isn’t it?

noalias, nothanks

Many people, myself included, don’t think that this is a good thing.

First of all, it introduces more UB that could have been defined behaviour instead. This is true for almost all UB, but usually, +something happened: The tag was popped off the borrow stack and therefore invalidated. That’s because box invalidates all tags +when it’s moved. The tag was popped off the borrow stack and we tried to read with it anyways - undefined behaviour happened!

And that’s how our code wasn’t a miscompilation, but undefined behaviour. Quite surprising, isn’t it?

noalias, nothanks

Many people, myself included, don’t think that this is a good thing.

First of all, it introduces more UB that could have been defined behaviour instead. This is true for almost all UB, but usually, there is something gained from the UB that justifies it. We will look at this later. But allowing such behaviour is fairly easy: If box didn’t invalidate pointers on move and instead behaved like a normal raw pointer, the code above would be sound.

But more importantly, this is not behaviour generally expected by users. While it can be argued that box is like a T, but on the heap, and therefore moving it should invalidate pointers, since moving T definitely has to invalidate pointers to it, diff --git a/posts/index.xml b/posts/index.xml index 18ffc14..157b693 100644 --- a/posts/index.xml +++ b/posts/index.xml @@ -111,8 +111,8 @@ that is implemented in Miri. For an excellent introduction, see this part of the and only the pointers that have their tag in the stack are allowed to access it. Tags can be pushed and popped from the stack through various operations, for example borrowing.</p> <p>In the code example above, we get a nice little hint where the tag was created. When we created a reference (that was then coerced into a raw pointer) from our box, it got a new tag called <code>&lt;3314&gt;</code>. Then, when we moved the box into the function, -something happened: The tag was invalidated and popped off the borrow stack. That&rsquo;s because box invalidates all tags when it&rsquo;s -moved. The tag was popped off the borrow stack and we tried to read from it anyways - undefined behaviour happened!</p> +something happened: The tag was popped off the borrow stack and therefore invalidated. That&rsquo;s because box invalidates all tags +when it&rsquo;s moved. The tag was popped off the borrow stack and we tried to read with it anyways - undefined behaviour happened!</p> <p>And that&rsquo;s how our code wasn&rsquo;t a miscompilation, but undefined behaviour. Quite surprising, isn&rsquo;t it?</p> <h1 id="noalias-nothanks">noalias, nothanks</h1> <p>Many people, myself included, don&rsquo;t think that this is a good thing.</p> diff --git a/tags/rust/index.xml b/tags/rust/index.xml index c73ee86..0a33f2e 100644 --- a/tags/rust/index.xml +++ b/tags/rust/index.xml @@ -111,8 +111,8 @@ that is implemented in Miri. For an excellent introduction, see this part of the and only the pointers that have their tag in the stack are allowed to access it. Tags can be pushed and popped from the stack through various operations, for example borrowing.</p> <p>In the code example above, we get a nice little hint where the tag was created. When we created a reference (that was then coerced into a raw pointer) from our box, it got a new tag called <code>&lt;3314&gt;</code>. Then, when we moved the box into the function, -something happened: The tag was invalidated and popped off the borrow stack. That&rsquo;s because box invalidates all tags when it&rsquo;s -moved. The tag was popped off the borrow stack and we tried to read from it anyways - undefined behaviour happened!</p> +something happened: The tag was popped off the borrow stack and therefore invalidated. That&rsquo;s because box invalidates all tags +when it&rsquo;s moved. The tag was popped off the borrow stack and we tried to read with it anyways - undefined behaviour happened!</p> <p>And that&rsquo;s how our code wasn&rsquo;t a miscompilation, but undefined behaviour. Quite surprising, isn&rsquo;t it?</p> <h1 id="noalias-nothanks">noalias, nothanks</h1> <p>Many people, myself included, don&rsquo;t think that this is a good thing.</p> diff --git a/tags/unsafe-code/index.xml b/tags/unsafe-code/index.xml index 5a45a12..ff77669 100644 --- a/tags/unsafe-code/index.xml +++ b/tags/unsafe-code/index.xml @@ -111,8 +111,8 @@ that is implemented in Miri. For an excellent introduction, see this part of the and only the pointers that have their tag in the stack are allowed to access it. Tags can be pushed and popped from the stack through various operations, for example borrowing.</p> <p>In the code example above, we get a nice little hint where the tag was created. When we created a reference (that was then coerced into a raw pointer) from our box, it got a new tag called <code>&lt;3314&gt;</code>. Then, when we moved the box into the function, -something happened: The tag was invalidated and popped off the borrow stack. That&rsquo;s because box invalidates all tags when it&rsquo;s -moved. The tag was popped off the borrow stack and we tried to read from it anyways - undefined behaviour happened!</p> +something happened: The tag was popped off the borrow stack and therefore invalidated. That&rsquo;s because box invalidates all tags +when it&rsquo;s moved. The tag was popped off the borrow stack and we tried to read with it anyways - undefined behaviour happened!</p> <p>And that&rsquo;s how our code wasn&rsquo;t a miscompilation, but undefined behaviour. Quite surprising, isn&rsquo;t it?</p> <h1 id="noalias-nothanks">noalias, nothanks</h1> <p>Many people, myself included, don&rsquo;t think that this is a good thing.</p>