Commit 5e4eab4
authored
Rollup merge of rust-lang#128778 - RalfJung:atomic-read-read-races, r=Mark-Simulacrum
atomics: allow atomic and non-atomic reads to race
We currently define our atomics in terms of C++ `atomic_ref`. That has the unfortunate side-effect of making it UB for an atomic and a non-atomic read to race (concretely, [this code](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=d1a743774e60923db33def7fe314d754) has UB). There's really no good reason for this, all the academic models of the C++ memory model I am aware of allow this -- C++ just disallows this because of their insistence on an "object model" with typed memory, where `atomic_ref` temporarily creates an "atomic object" that may not be accesses via regular non-atomic operations.
So instead of tying our operations to `atomic_ref`, let us tie them directly to the underlying C++ memory model. I am not sure what is the best way to phrase this, so here's a first attempt.
We also carve out an exception from the "no mixed-size atomic accesses" rule to permit mixed-size atomic reads -- given that we permit mixed-size non-atomic reads, it seems odd that this would be disallowed for atomic reads. However, when an atomic write races with any other atomic operation, they must use the same size.
With this change, it is finally the case that every non-atomic access can be replaced by an atomic access without introducing UB.
Cc `@rust-lang/opsem` `@chorman0773` `@m-ou-se` `@WaffleLapkin` `@Amanieu`
Fixes rust-lang/unsafe-code-guidelines#483File tree
20 files changed
+318
-310
lines changed- library/core/src
- sync
- src/tools/miri
- src/concurrency
- tests
- fail
- data_race
- weak_memory
- pass/concurrency
20 files changed
+318
-310
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1895 | 1895 | | |
1896 | 1896 | | |
1897 | 1897 | | |
| 1898 | + | |
| 1899 | + | |
| 1900 | + | |
| 1901 | + | |
| 1902 | + | |
1898 | 1903 | | |
1899 | 1904 | | |
1900 | 1905 | | |
1901 | 1906 | | |
1902 | 1907 | | |
| 1908 | + | |
1903 | 1909 | | |
1904 | 1910 | | |
1905 | 1911 | | |
| |||
1922 | 1928 | | |
1923 | 1929 | | |
1924 | 1930 | | |
1925 | | - | |
1926 | | - | |
1927 | | - | |
1928 | | - | |
1929 | 1931 | | |
1930 | 1932 | | |
1931 | 1933 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
33 | 35 | | |
34 | | - | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
35 | 49 | | |
36 | | - | |
37 | | - | |
38 | | - | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
39 | 54 | | |
40 | | - | |
41 | | - | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
42 | 58 | | |
43 | | - | |
44 | | - | |
45 | | - | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
46 | 63 | | |
47 | 64 | | |
48 | 65 | | |
| |||
52 | 69 | | |
53 | 70 | | |
54 | 71 | | |
55 | | - | |
56 | | - | |
57 | | - | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
58 | 75 | | |
59 | 76 | | |
60 | 77 | | |
61 | | - | |
62 | | - | |
63 | | - | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
64 | 83 | | |
65 | 84 | | |
66 | 85 | | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
72 | 91 | | |
73 | 92 | | |
74 | 93 | | |
75 | | - | |
| 94 | + | |
76 | 95 | | |
77 | 96 | | |
78 | 97 | | |
| |||
81 | 100 | | |
82 | 101 | | |
83 | 102 | | |
84 | | - | |
85 | | - | |
| 103 | + | |
| 104 | + | |
86 | 105 | | |
87 | 106 | | |
88 | 107 | | |
| |||
137 | 156 | | |
138 | 157 | | |
139 | 158 | | |
140 | | - | |
| 159 | + | |
141 | 160 | | |
142 | 161 | | |
143 | 162 | | |
| |||
153 | 172 | | |
154 | 173 | | |
155 | 174 | | |
156 | | - | |
| 175 | + | |
157 | 176 | | |
158 | 177 | | |
159 | 178 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
191 | 191 | | |
192 | 192 | | |
193 | 193 | | |
194 | | - | |
| 194 | + | |
| 195 | + | |
195 | 196 | | |
196 | 197 | | |
197 | 198 | | |
| |||
265 | 266 | | |
266 | 267 | | |
267 | 268 | | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
268 | 277 | | |
269 | 278 | | |
270 | 279 | | |
| |||
305 | 314 | | |
306 | 315 | | |
307 | 316 | | |
308 | | - | |
309 | | - | |
| 317 | + | |
310 | 318 | | |
311 | 319 | | |
312 | 320 | | |
| |||
325 | 333 | | |
326 | 334 | | |
327 | 335 | | |
328 | | - | |
329 | | - | |
| 336 | + | |
| 337 | + | |
330 | 338 | | |
331 | 339 | | |
332 | 340 | | |
| |||
336 | 344 | | |
337 | 345 | | |
338 | 346 | | |
339 | | - | |
| 347 | + | |
340 | 348 | | |
341 | 349 | | |
342 | 350 | | |
| |||
383 | 391 | | |
384 | 392 | | |
385 | 393 | | |
| 394 | + | |
386 | 395 | | |
387 | 396 | | |
388 | 397 | | |
389 | 398 | | |
390 | | - | |
| 399 | + | |
391 | 400 | | |
392 | 401 | | |
393 | 402 | | |
394 | 403 | | |
395 | | - | |
396 | | - | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
397 | 411 | | |
398 | 412 | | |
399 | 413 | | |
| |||
499 | 513 | | |
500 | 514 | | |
501 | 515 | | |
502 | | - | |
| 516 | + | |
503 | 517 | | |
504 | 518 | | |
505 | 519 | | |
| |||
508 | 522 | | |
509 | 523 | | |
510 | 524 | | |
511 | | - | |
| 525 | + | |
512 | 526 | | |
513 | | - | |
514 | | - | |
515 | | - | |
516 | | - | |
517 | | - | |
518 | | - | |
| 527 | + | |
| 528 | + | |
519 | 529 | | |
520 | 530 | | |
521 | 531 | | |
| |||
527 | 537 | | |
528 | 538 | | |
529 | 539 | | |
530 | | - | |
| 540 | + | |
531 | 541 | | |
532 | 542 | | |
533 | 543 | | |
| |||
552 | 562 | | |
553 | 563 | | |
554 | 564 | | |
| 565 | + | |
555 | 566 | | |
556 | | - | |
557 | | - | |
558 | 567 | | |
559 | | - | |
560 | 568 | | |
561 | 569 | | |
562 | 570 | | |
| |||
957 | 965 | | |
958 | 966 | | |
959 | 967 | | |
960 | | - | |
961 | | - | |
962 | | - | |
| 968 | + | |
963 | 969 | | |
964 | 970 | | |
965 | 971 | | |
| |||
977 | 983 | | |
978 | 984 | | |
979 | 985 | | |
980 | | - | |
| 986 | + | |
981 | 987 | | |
982 | 988 | | |
983 | | - | |
| 989 | + | |
984 | 990 | | |
985 | 991 | | |
986 | 992 | | |
| |||
1007 | 1013 | | |
1008 | 1014 | | |
1009 | 1015 | | |
1010 | | - | |
1011 | | - | |
1012 | | - | |
1013 | | - | |
| 1016 | + | |
1014 | 1017 | | |
1015 | 1018 | | |
1016 | 1019 | | |
| |||
This file was deleted.
0 commit comments