Commit 7112951
authored
Rollup merge of rust-lang#138303 - DiuDiu777:rc-fix, r=Mark-Simulacrum
Fix Ptr inconsistency in {Rc,Arc}
### PR Description
This pr aims to address the problem discussed on [zulip](https://rust-lang.zulipchat.com/#narrow/channel/219381-t-libs/topic/Inconsistency.20in.20.7BRc.2CArc.7D's.20ptr.20requirements/with/504259637).
### Problem Clarification
As this post presents, the `{Rc, Arc}::{in/de-crement_strong_count_/in}` do not limit the layout of the memory that `ptr` points to, while internally `Rc::from_raw_in` is called directly.
UB doesn't just appear when the strong count is decremented to zero. Miri also detects the UB of `out-of-bounds pointer use` when increment strong count is called on a pointer with an incorrect layout(shown as below).
```rust
use std::rc::Rc;
#[repr(align(8))]
struct Aligned8(u64);
#[repr(align(16))]
struct Aligned16(u64);
fn main() {
let rc: Rc<Aligned8> = Rc::new(Aligned8(42));
let raw_ptr = Rc::into_raw(rc);
unsafe {
Rc::increment_strong_count(raw_ptr as *const Aligned16);
}
}
```
Miri output:
```
error: Undefined Behavior: out-of-bounds pointer use: expected a pointer to 32 bytes of memory, but got alloc954 which is only 24 bytes from the end of the allocation
```2 files changed
+44
-20
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1327 | 1327 | | |
1328 | 1328 | | |
1329 | 1329 | | |
1330 | | - | |
1331 | | - | |
| 1330 | + | |
| 1331 | + | |
| 1332 | + | |
1332 | 1333 | | |
1333 | 1334 | | |
1334 | 1335 | | |
| 1336 | + | |
| 1337 | + | |
1335 | 1338 | | |
1336 | 1339 | | |
1337 | 1340 | | |
| |||
1360 | 1363 | | |
1361 | 1364 | | |
1362 | 1365 | | |
1363 | | - | |
1364 | | - | |
| 1366 | + | |
| 1367 | + | |
| 1368 | + | |
1365 | 1369 | | |
1366 | 1370 | | |
1367 | 1371 | | |
1368 | 1372 | | |
| 1373 | + | |
| 1374 | + | |
1369 | 1375 | | |
1370 | 1376 | | |
1371 | 1377 | | |
| |||
1623 | 1629 | | |
1624 | 1630 | | |
1625 | 1631 | | |
1626 | | - | |
1627 | | - | |
| 1632 | + | |
| 1633 | + | |
| 1634 | + | |
1628 | 1635 | | |
1629 | | - | |
| 1636 | + | |
| 1637 | + | |
| 1638 | + | |
1630 | 1639 | | |
1631 | 1640 | | |
1632 | 1641 | | |
| |||
1665 | 1674 | | |
1666 | 1675 | | |
1667 | 1676 | | |
1668 | | - | |
1669 | | - | |
| 1677 | + | |
| 1678 | + | |
| 1679 | + | |
1670 | 1680 | | |
1671 | | - | |
1672 | | - | |
| 1681 | + | |
| 1682 | + | |
| 1683 | + | |
| 1684 | + | |
1673 | 1685 | | |
1674 | 1686 | | |
1675 | 1687 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1453 | 1453 | | |
1454 | 1454 | | |
1455 | 1455 | | |
1456 | | - | |
1457 | | - | |
| 1456 | + | |
| 1457 | + | |
| 1458 | + | |
1458 | 1459 | | |
1459 | 1460 | | |
1460 | 1461 | | |
| 1462 | + | |
| 1463 | + | |
1461 | 1464 | | |
1462 | 1465 | | |
1463 | 1466 | | |
| |||
1488 | 1491 | | |
1489 | 1492 | | |
1490 | 1493 | | |
1491 | | - | |
1492 | | - | |
| 1494 | + | |
| 1495 | + | |
| 1496 | + | |
1493 | 1497 | | |
1494 | 1498 | | |
1495 | 1499 | | |
1496 | 1500 | | |
1497 | 1501 | | |
| 1502 | + | |
| 1503 | + | |
1498 | 1504 | | |
1499 | 1505 | | |
1500 | 1506 | | |
| |||
1806 | 1812 | | |
1807 | 1813 | | |
1808 | 1814 | | |
1809 | | - | |
1810 | | - | |
1811 | | - | |
| 1815 | + | |
| 1816 | + | |
| 1817 | + | |
| 1818 | + | |
1812 | 1819 | | |
1813 | 1820 | | |
| 1821 | + | |
| 1822 | + | |
1814 | 1823 | | |
1815 | 1824 | | |
1816 | 1825 | | |
| |||
1850 | 1859 | | |
1851 | 1860 | | |
1852 | 1861 | | |
1853 | | - | |
1854 | | - | |
| 1862 | + | |
| 1863 | + | |
| 1864 | + | |
1855 | 1865 | | |
1856 | 1866 | | |
1857 | 1867 | | |
1858 | 1868 | | |
1859 | 1869 | | |
| 1870 | + | |
| 1871 | + | |
1860 | 1872 | | |
1861 | 1873 | | |
1862 | 1874 | | |
| |||
0 commit comments