Skip to content
This repository was archived by the owner on Apr 5, 2024. It is now read-only.

Commit 8d2b593

Browse files
committed
add the first litmus test, and a template
0 parents  commit 8d2b593

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*~

README.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
This repository is intended to help us develop a coherent and clear
2+
"memory model" for Rust. The goal of a memory model specifically is to
3+
define:
4+
5+
- what kinds of aliasing and accesses unsafe code can perform;
6+
- what kinds of transformations and optimizations the compiler can do.
7+
8+
This in turn implies the kinds of things that unsafe code can rely on.
9+
10+
We're still in the 'data gathering' stage of this effort. Therefore,
11+
this repository currently consists of a series of examples scraped
12+
from various comment threads. These are broken into two categories:
13+
14+
- `litmus_tests` -- bits of unsafe code that someone might write;
15+
eventually, we may decide that some of these bits of unsafe code are
16+
illegal and hence might trigger undefined behavior.
17+
- `optimizations` -- transformations the compiler might want to
18+
perform; eventually, we may decide that some of these bits of unsafe
19+
code are illegal and hence the compiler could not do them.
20+
21+
Naturally these two things are in tension. That is, the more
22+
optimizations the compiler can do, the fewer litmus tests will be
23+
legal. Eventually I would like to make a nice chart indicating which
24+
tests are in conflict with which optimizations.
25+
26+
For each example, I include a markdown file
27+
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
### Example
2+
3+
```
4+
fn compare_dangling_pointers()
5+
{
6+
let p1: *const i32;
7+
{
8+
let x = Box::new(0);
9+
p1 = &*x;
10+
}
11+
{
12+
let y = Box::new(0);
13+
let p2: *const _ = &*y;
14+
let b = p1 == p2;
15+
println!("Are they equal? {} {}", b, b, p1 == p2);
16+
}
17+
}
18+
```
19+
20+
### Explanation
21+
22+
In C, once an object is deallocated, all pointers to it have an
23+
indeterminate value. Hence the value of `p1` would be indeterminate
24+
here. But it is accessible in safe Rust.
25+
26+
However, LLVM has a [different model][sunfish1], where it thinks only
27+
about "aliasing". In other words, even if two pointers are equal, they
28+
might be considered not to alias, in the case that one of them is
29+
invalidated. This makes the above code perfectly legal.
30+
31+
[sunfish1]: https://internals.rust-lang.org/t/comparing-dangling-pointers/3019/22?u=nikomatsakis
32+
33+
### Source
34+
35+
https://internals.rust-lang.org/t/comparing-dangling-pointers/3019

template.md

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
### Example
2+
3+
```
4+
rust source code
5+
```
6+
7+
(In the case of a compiler transform, include the transformed source
8+
code as well.)
9+
10+
### Explanation
11+
12+
What makes this example interesting? A few words
13+
can go a long way.
14+
15+
### Source
16+
17+
Where did this example come from?

0 commit comments

Comments
 (0)