Debian Codegraph @

third_party/rust/presser/README.md

raw
1 <!-- Allow this file to not have a first line heading -->
2 <!-- markdownlint-disable-file MD041 no-emphasis-as-heading -->
3
4 <!-- inline html -->
5 <!-- markdownlint-disable-file MD033 -->
6
7 <div align="center">
8
9 # `🗜 presser`
10
11 **Utilities to help make copying data around into raw, possibly-uninitialized buffers easier and safer.**
12
13 [![Embark](https://img.shields.io/badge/embark-open%20source-blueviolet.svg)](https://embark.dev)
14 [![Embark](https://img.shields.io/badge/discord-ark-%237289da.svg?logo=discord)](https://discord.gg/dAuKfZS)
15 [![Crates.io](https://img.shields.io/crates/v/presser.svg)](https://crates.io/crates/presser)
16 [![Published Docs](https://docs.rs/presser/badge.svg)](https://docs.rs/presser)
17 [![Git Docs](https://img.shields.io/badge/git%20main%20docs-published-blue?style=plastic)](https://embarkstudios.github.io/presser/presser/index.html)
18 [![dependency status](https://deps.rs/repo/github/EmbarkStudios/presser/status.svg)](https://deps.rs/repo/github/EmbarkStudios/presser)
19 </div>
20
21 `presser` can help you when copying data into raw buffers. One primary use-case is copying data into
22 graphics-api-allocated buffers which will then be accessed by the GPU. Common methods for doing this
23 right now in Rust can often invoke UB in subtle and hard-to-see ways. For example, viewing an allocated
24 but uninitialized buffer as an `&mut [u8]` **is instantly undefined behavior**\*, and `transmute`ing even a
25 `T: Copy` type which has *any padding bytes in its layout* as a `&[u8]` to be the source of a copy is
26 **also instantly undefined behavior**, in both cases because it is *invalid* to create a reference to an invalid
27 value (and uninitialized memory is an invalid `u8`), *even if* your code never actually accesses that memory.
28 This immediately makes what seems like the most straightforward way to copy data into buffers unsound 😬
29
30 \* *If you're currently thinking to yourself "bah! what's the issue? surely an uninit u8 is just any random bit pattern
31 and that's fine we don't care," [check out this blog post](https://www.ralfj.de/blog/2019/07/14/uninit.html) by
32 @RalfJung, one of the people leading the effort to better define Rust's memory and execution model. As is explored
33 in that blog post, an*uninit*piece of memory is not simply*an arbitrary bit pattern*, it is a wholly separate
34 state about a piece of memory, outside of its value, which lets the compiler perform optimizations that reorder,
35 delete, and otherwise change the actual execution flow of your program in ways that cannot be described simply
36 by "the value could have*some*possible bit pattern". LLVM and Clang are changing themselves to require special
37 `noundef` attribute to perform many important optimizations that are otherwise unsound. For a concrete example
38 of the sorts of problems this can cause, [see this issue @scottmcm hit](https://github.com/rust-lang/rust/pull/98919#issuecomment-1186106387).*
39
40 ## A bad example
41
42 ```rust
43 #[derive(Clone, Copy)]
44 #[repr(C)]
45 struct MyDataStruct {
46 a: u8,
47 b: u32,
48 }
49
50 let my_data = MyDataStruct { a: 0, b: 42 };
51
52 // 🚨 MyDataStruct contains 3 padding bytes after `a`, which are
53 // uninit, therefore getting a slice that includes them is UB!
54 let my_data_bytes: &[u8] = transmute(&my_data);
55
56 // allocate an uninit buffer of some size
57 let my_buffer: MyBufferType = some_api.alloc_buffer_size(2048);
58
59 // 🚨 this is UB for the same reason, these bytes are uninit!
60 let buffer_as_bytes: &mut [u8] =
61 slice::from_raw_parts(my_buffer.ptr(), my_buffer.size());
62
63 // 🚨 this is UB because not only are both slices invalid,
64 // this is not ensuring proper alignment!
65 buffer_as_bytes.copy_from_slice(my_data_bytes);
66 ```
67
68 `presser` helps with this by allowing you to view raw allocated memory of some size as a "`Slab`" of memory and then
69 provides *safe, valid* ways to copy data into that memory:
70
71 ### A good example
72
73 ```rust
74 #[derive(Clone, Copy)]
75 #[repr(C)]
76 struct MyDataStruct {
77 a: u8,
78 b: u32,
79 }
80
81 let my_data = MyDataStruct { a: 0, b: 42 };
82
83 // allocate an uninit buffer of some size
84 let my_buffer: MyBufferType = some_api.alloc_buffer_size(2048);
85
86 // use `RawAllocation` helper to allow access to a presser `Slab`.
87 // alternatively, you could implement the `Slab` on your buffer type directly if that
88 // type is owned by your code!
89 let raw_allocation = presser::RawAllocation::from_raw_parts(my_buffer.ptr(), my_buffer.size());
90
91 // here we assert that we have exclusive access to the data in the buffer, and get the actual
92 // `Slab` to use to copy into.
93 let slab = unsafe { raw_allocation.borrow_as_slab(); }
94
95 // now we may safely copy `my_data` into `my_buffer`, starting at a minimum offset of 0 into the buffer
96 let copy_record = presser::copy_to_offset(&my_data, &mut slab, 0)?;
97
98 // note that due to alignment requirements of `my_data`, the *actual* start of the bytes of
99 // `my_data` may be placed at a different offset than requested. so, we check the returned
100 // `CopyRecord` to check the actual start offset of the copied data.
101 let actual_start_offset = copy_record.copy_start_offset;
102 ```
103
104 Note that actually accessing the copied data is a completely separate issue which `presser` does not
105 (as of now) concern itself with. BE CAREFUL!
106
107 See more in [the git `main` docs](https://embarkstudios.github.io/presser/presser/index.html)
108 or [the released version docs](https://docs.rs/presser).
109
110 ## Contribution
111
112 [![Contributor Covenant](https://img.shields.io/badge/contributor%20covenant-v1.4-ff69b4.svg)](CODE_OF_CONDUCT.md)
113
114 We welcome community contributions to this project.
115
116 Please read our [Contributor Guide](CONTRIBUTING.md) for more information on how to get started.
117 Please also read our [Contributor Terms](CONTRIBUTING.md#contributor-terms) before you make any contributions.
118
119 Any contribution intentionally submitted for inclusion in an Embark Studios project, shall comply with the Rust standard licensing model (MIT OR Apache 2.0) and therefore be dual licensed as described below, without any additional terms or conditions:
120
121 ### License
122
123 This contribution is dual licensed under EITHER OF
124
125 * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or <http://www.apache.org/licenses/LICENSE-2.0>)
126 * MIT license ([LICENSE-MIT](LICENSE-MIT) or <http://opensource.org/licenses/MIT>)
127
128 at your option.
129
130 For clarity, "your" refers to Embark or any other licensee/user of the contribution.