Skip to content

Commit

Permalink
reading-list: add "Box Is a Unique Type"
Browse files Browse the repository at this point in the history
  • Loading branch information
zjp-CN committed Jul 13, 2024
1 parent 344fc28 commit bc6a27e
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/reading-list/2024a.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,79 @@
* [Benchmarking rust compilation speedups and slowdowns from sccache and -Zthreads](https://neosmart.net/blog/benchmarking-rust-compilation-speedups-and-slowdowns-from-sccache-and-zthreads/)
由于 WSL1 的 IO 性能较差,在那使用 sccache 可能并不会加速增量构建,反而会拖慢编译

# unsafe

* [Box Is a Unique Type](https://nilstrieb.dev/blog/posts/box-is-a-unique-type/) by nilstrieb
* 用 Miri 分析/解释 UB 的示例;Box 在 noalias 语义上具有分歧 —— UB 与 Rust 语义紧密联系
* [Too Many Linked Lists: Attempting To Understand Stacked Borrows](https://rust-unofficial.github.io/too-many-lists/fifth-stacked-borrows.html)
* [UCG#326: What are the uniqueness guarantees of Box and Vec?](https://github.com/rust-lang/unsafe-code-guidelines/issues/326)
* [The Unsafe Chronicles: Exhibit A: Aliasing Boxes](https://www.youtube.com/watch?v=EY7Wi9fV5bk&ab_channel=JonGjengset) by Jonhoo

```rust
use aliasable::boxed::AliasableBox;

fn main() {
let b = Box::new(0);
let b = AliasableBox::from_unique(b);
let ptr: *const u8 = &*b;

takes_box_and_ptr_to_it(b, ptr);
}

fn takes_box_and_ptr_to_it(mut b: AliasableBox<u8>, ptr: *const u8) { // no UB
let value = unsafe { *ptr };
*b = 5;
let value2 = unsafe { *b };
assert_ne!(value, value2);
}
```

```rust
fn main() {
let b = Box::new(0);
let ptr: *const u8 = &*b;

takes_box_and_ptr_to_it(b, ptr);
}

fn takes_box_and_ptr_to_it(mut b: Box<u8>, ptr: *const u8) { // UB
let value = unsafe { *ptr };
*b = 5;
let value2 = unsafe { *b };
assert_ne!(value, value2);
}

error: Undefined Behavior: attempting a read access using <2248> at alloc1032[0x0], but that tag does not exist in the borrow stack for this location
--> src/main.rs:13:26
|
13 | let value = unsafe { *ptr };
| ^^^^
| |
| attempting a read access using <2248> at alloc1032[0x0], but that tag does not exist in the borrow stack for this location
| this error occurs as part of an access at alloc1032[0x0..0x1]
|
= help: this indicates a potential bug in the program: it performed an invalid operation, but the Stacked Borrows rules it violated are still experimental
= help: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md for further information
help: <2248> was created by a SharedReadOnly retag at offsets [0x0..0x1]
--> src/main.rs:6:26
|
6 | let ptr: *const u8 = &*b;
| ^^^
help: <2248> was later invalidated at offsets [0x0..0x1] by a Unique retag
--> src/main.rs:8:29
|
8 | takes_box_and_ptr_to_it(b, ptr);
| ^
= note: BACKTRACE (of the first span):
= note: inside `takes_box_and_ptr_to_it` at src/main.rs:13:26: 13:30
note: inside `main`
--> src/main.rs:8:5
|
8 | takes_box_and_ptr_to_it(b, ptr);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
```


# TODO

<https://blog.shrirambalaji.com/posts/resolving-rust-symbols/>:解析 ELF 中的 Rust 符号
Expand Down

0 comments on commit bc6a27e

Please sign in to comment.