Commit 47c15a4
committed
Auto merge of rust-lang#117329 - RalfJung:offset-by-zero, r=oli-obk,scottmcm
offset: allow zero-byte offset on arbitrary pointers
As per prior `@rust-lang/opsem` [discussion](rust-lang/opsem-team#10) and [FCP](rust-lang/unsafe-code-guidelines#472 (comment)):
- Zero-sized reads and writes are allowed on all sufficiently aligned pointers, including the null pointer
- Inbounds-offset-by-zero is allowed on all pointers, including the null pointer
- `offset_from` on two pointers derived from the same allocation is always allowed when they have the same address
This removes surprising UB (in particular, even C++ allows "nullptr + 0", which we currently disallow), and it brings us one step closer to an important theoretical property for our semantics ("provenance monotonicity": if operations are valid on bytes without provenance, then adding provenance can't make them invalid).
The minimum LLVM we require (v17) includes https://reviews.llvm.org/D154051, so we can finally implement this.
The `offset_from` change is needed to maintain the equivalence with `offset`: if `let ptr2 = ptr1.offset(N)` is well-defined, then `ptr2.offset_from(ptr1)` should be well-defined and return N. Now consider the case where N is 0 and `ptr1` dangles: we want to still allow offset_from here.
I think we should change offset_from further, but that's a separate discussion.
Fixes rust-lang#65108
[Tracking issue](rust-lang#117945) | [T-lang summary](rust-lang#117329 (comment))
Cc `@nikic`4 files changed
+34
-33
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1483 | 1483 | | |
1484 | 1484 | | |
1485 | 1485 | | |
1486 | | - | |
1487 | | - | |
1488 | | - | |
1489 | | - | |
| 1486 | + | |
| 1487 | + | |
| 1488 | + | |
| 1489 | + | |
1490 | 1490 | | |
1491 | 1491 | | |
1492 | 1492 | | |
| |||
1502 | 1502 | | |
1503 | 1503 | | |
1504 | 1504 | | |
1505 | | - | |
| 1505 | + | |
1506 | 1506 | | |
1507 | 1507 | | |
1508 | 1508 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
465 | 465 | | |
466 | 466 | | |
467 | 467 | | |
468 | | - | |
469 | | - | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
470 | 471 | | |
471 | 472 | | |
472 | 473 | | |
| |||
676 | 677 | | |
677 | 678 | | |
678 | 679 | | |
679 | | - | |
680 | | - | |
| 680 | + | |
681 | 681 | | |
682 | | - | |
683 | | - | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
684 | 685 | | |
685 | 686 | | |
686 | 687 | | |
| |||
951 | 952 | | |
952 | 953 | | |
953 | 954 | | |
954 | | - | |
955 | | - | |
| 955 | + | |
| 956 | + | |
| 957 | + | |
956 | 958 | | |
957 | 959 | | |
958 | 960 | | |
| |||
1035 | 1037 | | |
1036 | 1038 | | |
1037 | 1039 | | |
1038 | | - | |
1039 | | - | |
| 1040 | + | |
| 1041 | + | |
| 1042 | + | |
1040 | 1043 | | |
1041 | 1044 | | |
1042 | 1045 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | | - | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
19 | 21 | | |
20 | 22 | | |
21 | 23 | | |
22 | 24 | | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | 25 | | |
31 | 26 | | |
32 | 27 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
480 | 480 | | |
481 | 481 | | |
482 | 482 | | |
483 | | - | |
484 | | - | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
485 | 486 | | |
486 | 487 | | |
487 | 488 | | |
| |||
904 | 905 | | |
905 | 906 | | |
906 | 907 | | |
907 | | - | |
908 | | - | |
| 908 | + | |
909 | 909 | | |
910 | | - | |
911 | | - | |
| 910 | + | |
| 911 | + | |
| 912 | + | |
912 | 913 | | |
913 | 914 | | |
914 | 915 | | |
| |||
1095 | 1096 | | |
1096 | 1097 | | |
1097 | 1098 | | |
1098 | | - | |
1099 | | - | |
| 1099 | + | |
| 1100 | + | |
| 1101 | + | |
1100 | 1102 | | |
1101 | 1103 | | |
1102 | 1104 | | |
| |||
1179 | 1181 | | |
1180 | 1182 | | |
1181 | 1183 | | |
1182 | | - | |
1183 | | - | |
| 1184 | + | |
| 1185 | + | |
| 1186 | + | |
1184 | 1187 | | |
1185 | 1188 | | |
1186 | 1189 | | |
| |||
0 commit comments