Skip to content

Commit 836e6f2

Browse files
committed
add a reference TOC and fill it in a bit
1 parent d97fa21 commit 836e6f2

19 files changed

+156
-1
lines changed

README.md

+30-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,31 @@
11
# unsafe-code-guidelines
2-
Home for the Unsafe Code Guidelines effort.
2+
3+
Home for the Unsafe Code Guidelines effort. The goal of the Unsafe
4+
Code Guidelines effort is to collaboratively produce a "reference
5+
guide" to write unsafe code. This guide should ultimately be a formal
6+
specification of what kinds of things unsafe code can and cannot do.
7+
However, we are not there yet.
8+
9+
## The reference
10+
11+
The primary document in this repo is the "Unsafe Code Guidelines
12+
Reference". It is largely a work-in-progress right now. It serves as
13+
a kind of table of contents to the sorts of questions that are on our
14+
radar, as well as place to collect notes and summaries about those
15+
questions.
16+
17+
Since we are not yet at the point where we can provide complete,
18+
definitive answers, the current goal for each section is to describe
19+
the "contours of the space". For example, we would try to specify what
20+
kinds of invariants **must** hold, as well as things that we expect to
21+
**never** be true. For other cases where there is disagreement, we
22+
will try to summarize the various options on the table -- you would do
23+
well in your unsafe code to steer clear of relying on those details.
24+
25+
## How to participate
26+
27+
The Unsafe Code Guidelines WG is in the process of being rebooted. We
28+
expect to be holding regular meetings focused on particular topics.
29+
Stay tuned for more announcements. -- @nikomatsakis
30+
31+

reference/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
book

reference/book.toml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[book]
2+
authors = ["Niko Matsakis"]
3+
multilingual = false
4+
src = "src"
5+
title = "Unsafe Code Guidelines Reference"

reference/src/SUMMARY.md

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Summary
2+
3+
- [Introduction](./introduction.md)
4+
- [Areas of active discussion](./active_discussion.md)
5+
- [Aliasing and memory model](./active_discussion/aliasing.md)
6+
- [Cryptographic concerns](./active_discussion/crypto_concerns.md)
7+
- [Keeping secrets](./active_discussion/crypto_concerns/keeping_secrets.md)
8+
- [Constant time code](./active_discussion/crypto_concerns/constant_time_code.md)
9+
- [Zeroing](./active_discussion/crypto_concerns/zeroing.md)
10+
- [Data structure representation](./active_discussion/representation.md)
11+
- [Stable addresses](./active_discussion/stable_addresses.md)
12+
- [Storage liveness](./active_discussion/storage_liveness.md)
13+
- [Uninhabited types like `!` and exhaustiveness](./active_discussion/uninhabited_types.md)
14+
- [Unions](./active_discussion/unions.md)
15+
- [Uninitialized memory](./active_discussion/uninitialized_memory.md)
16+
- [Optimizations](./optimizations.md)
17+
- [Optimizing immutable memory](./optimizations/immutable_memory.md)

reference/src/active_discussion.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Areas of active discussion
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Aliasing and memory model
2+
3+
## Brief Summary
4+
5+
Rust's borrow checker enforces some particular aliasing
6+
requirements. For example, an `&u32` reference is always guaranteed to
7+
be valid (dereferenceable) and immutable while it is in active
8+
use. Similarly, an `&mut` reference cannot alias any other active
9+
reference. It is less clear how those invariants translate to unsafe
10+
code that is using raw pointers.
11+
12+
## See also
13+
14+
- ACA model https://github.com/nikomatsakis/rust-memory-model/issues/26
15+
- Capability-based model https://github.com/nikomatsakis/rust-memory-model/issues/28
16+
- A formal C memory model supporting integer-pointer casts https://github.com/nikomatsakis/rust-memory-model/issues/30
17+
- Promising semantics for relaxed-memory concurrency https://github.com/nikomatsakis/rust-memory-model/issues/32
18+
- Tootsie Pop model https://github.com/nikomatsakis/rust-memory-model/issues/21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Cryptographic concerns
2+
3+
There are a number of concerns that arise when writing cryptographic
4+
code. This chapter summarizes some of them.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Constant time code
2+
3+
It is often important to be able to turn off optimizations and
4+
guarantee that code executes in constant time to prevent side-channel
5+
attacks based on timing.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Keeping secrets
2+
3+
When storing cryptographic keys, crypto code wants to be sure that the
4+
compiler will not insert loads or stores that were not present in the
5+
source. Moreover, it wants to be able to zero memory and know that no
6+
bits from that memory "escape" into registers etc.
7+
8+
## See also
9+
10+
- https://internals.rust-lang.org/t/volatile-and-sensitive-memory/3188
11+
- https://github.com/nikomatsakis/rust-memory-model/issues/16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Data structure representation
2+
3+
In general, Rust makes few guarantees about memory layout, unless you
4+
define your structs as `#[repr(rust)]`. But there are some things that
5+
we do guarantee. Let's write about them.
6+
7+
TODO:
8+
9+
- Find and link to the various RFCs
10+
- Enumerate things that we *might* in fact guarantee, even for non-C types:
11+
- e.g., `&T` and `Option<&T>` are both pointer sized
12+
- size of `extern fn` etc (at least on some platforms)?
13+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Stable addresses
2+
3+
Clearly, if you have a `&T` reference, the actual pointer address of
4+
that memory must remain valid while **that reference** is in active
5+
use. But how stable are the memory addresses of local variables in
6+
between borrows? Consider:
7+
8+
```rust
9+
let x = 22;
10+
foo(&x);
11+
foo(&x);
12+
13+
fn foo(y: &usize) { .. }
14+
```
15+
16+
Is `foo` guaranteed to be given the same pointer each time? Note that
17+
safe code can observe the pointer value by doing `y as *const usize as
18+
usize`. If, however, the answer is no, that is helpful to the compiler
19+
since it can spill `x` only while it is borrowed but otherwise simply
20+
store `x` in a register.
21+
22+
**Range of possible answers:**
23+
24+
- local variables have stable addresses (de facto true today)
25+
- local variables have stable addresses while borrowed, but may change betwen borrows (would be nice)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Storage liveness
2+
3+
If you move out from a variable, can you still use the underlying stack space?
4+
5+
```rust
6+
{
7+
let mut x: Vec<u32> = ....;
8+
let p: *mut Vec<u32> = &mut x;
9+
drop(x); // compiler can see `x` is uninitialized
10+
11+
// what happens if you use `p` here?
12+
} // StorageDead(x)
13+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Uninhabited types like `!` and exhaustiveness
2+
3+
TBD
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Uninitialized memory
2+
3+
TBD
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Unions
2+
3+
TBD

reference/src/chapter_1.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Chapter 1

reference/src/introduction.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Introduction

reference/src/optimizations.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Optimizations
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Optimizing immutable memory

0 commit comments

Comments
 (0)