Skip to content

Commit 2ae81ac

Browse files
committed
Revert "Auto merge of #95295 - CAD97:layout-isize, r=scottmcm"
This reverts commit 4ec97d9, reversing changes made to 95e7764.
1 parent 1d0597c commit 2ae81ac

File tree

4 files changed

+331
-156
lines changed

4 files changed

+331
-156
lines changed

Diff for: library/alloc/tests/string.rs

+88-38
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,12 @@ fn test_try_reserve() {
693693
const MAX_CAP: usize = isize::MAX as usize;
694694
const MAX_USIZE: usize = usize::MAX;
695695

696+
// On 16/32-bit, we check that allocations don't exceed isize::MAX,
697+
// on 64-bit, we assume the OS will give an OOM for such a ridiculous size.
698+
// Any platform that succeeds for these requests is technically broken with
699+
// ptr::offset because LLVM is the worst.
700+
let guards_against_isize = usize::BITS < 64;
701+
696702
{
697703
// Note: basic stuff is checked by test_reserve
698704
let mut empty_string: String = String::new();
@@ -706,19 +712,35 @@ fn test_try_reserve() {
706712
panic!("isize::MAX shouldn't trigger an overflow!");
707713
}
708714

709-
// Check isize::MAX + 1 does count as overflow
710-
assert_matches!(
711-
empty_string.try_reserve(MAX_CAP + 1).map_err(|e| e.kind()),
712-
Err(CapacityOverflow),
713-
"isize::MAX + 1 should trigger an overflow!"
714-
);
715-
716-
// Check usize::MAX does count as overflow
717-
assert_matches!(
718-
empty_string.try_reserve(MAX_USIZE).map_err(|e| e.kind()),
719-
Err(CapacityOverflow),
720-
"usize::MAX should trigger an overflow!"
721-
);
715+
if guards_against_isize {
716+
// Check isize::MAX + 1 does count as overflow
717+
assert_matches!(
718+
empty_string.try_reserve(MAX_CAP + 1).map_err(|e| e.kind()),
719+
Err(CapacityOverflow),
720+
"isize::MAX + 1 should trigger an overflow!"
721+
);
722+
723+
// Check usize::MAX does count as overflow
724+
assert_matches!(
725+
empty_string.try_reserve(MAX_USIZE).map_err(|e| e.kind()),
726+
Err(CapacityOverflow),
727+
"usize::MAX should trigger an overflow!"
728+
);
729+
} else {
730+
// Check isize::MAX + 1 is an OOM
731+
assert_matches!(
732+
empty_string.try_reserve(MAX_CAP + 1).map_err(|e| e.kind()),
733+
Err(AllocError { .. }),
734+
"isize::MAX + 1 should trigger an OOM!"
735+
);
736+
737+
// Check usize::MAX is an OOM
738+
assert_matches!(
739+
empty_string.try_reserve(MAX_USIZE).map_err(|e| e.kind()),
740+
Err(AllocError { .. }),
741+
"usize::MAX should trigger an OOM!"
742+
);
743+
}
722744
}
723745

724746
{
@@ -731,13 +753,19 @@ fn test_try_reserve() {
731753
if let Err(CapacityOverflow) = ten_bytes.try_reserve(MAX_CAP - 10).map_err(|e| e.kind()) {
732754
panic!("isize::MAX shouldn't trigger an overflow!");
733755
}
734-
735-
assert_matches!(
736-
ten_bytes.try_reserve(MAX_CAP - 9).map_err(|e| e.kind()),
737-
Err(CapacityOverflow),
738-
"isize::MAX + 1 should trigger an overflow!"
739-
);
740-
756+
if guards_against_isize {
757+
assert_matches!(
758+
ten_bytes.try_reserve(MAX_CAP - 9).map_err(|e| e.kind()),
759+
Err(CapacityOverflow),
760+
"isize::MAX + 1 should trigger an overflow!"
761+
);
762+
} else {
763+
assert_matches!(
764+
ten_bytes.try_reserve(MAX_CAP - 9).map_err(|e| e.kind()),
765+
Err(AllocError { .. }),
766+
"isize::MAX + 1 should trigger an OOM!"
767+
);
768+
}
741769
// Should always overflow in the add-to-len
742770
assert_matches!(
743771
ten_bytes.try_reserve(MAX_USIZE).map_err(|e| e.kind()),
@@ -757,6 +785,8 @@ fn test_try_reserve_exact() {
757785
const MAX_CAP: usize = isize::MAX as usize;
758786
const MAX_USIZE: usize = usize::MAX;
759787

788+
let guards_against_isize = usize::BITS < 64;
789+
760790
{
761791
let mut empty_string: String = String::new();
762792

@@ -769,17 +799,31 @@ fn test_try_reserve_exact() {
769799
panic!("isize::MAX shouldn't trigger an overflow!");
770800
}
771801

772-
assert_matches!(
773-
empty_string.try_reserve_exact(MAX_CAP + 1).map_err(|e| e.kind()),
774-
Err(CapacityOverflow),
775-
"isize::MAX + 1 should trigger an overflow!"
776-
);
777-
778-
assert_matches!(
779-
empty_string.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind()),
780-
Err(CapacityOverflow),
781-
"usize::MAX should trigger an overflow!"
782-
);
802+
if guards_against_isize {
803+
assert_matches!(
804+
empty_string.try_reserve_exact(MAX_CAP + 1).map_err(|e| e.kind()),
805+
Err(CapacityOverflow),
806+
"isize::MAX + 1 should trigger an overflow!"
807+
);
808+
809+
assert_matches!(
810+
empty_string.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind()),
811+
Err(CapacityOverflow),
812+
"usize::MAX should trigger an overflow!"
813+
);
814+
} else {
815+
assert_matches!(
816+
empty_string.try_reserve_exact(MAX_CAP + 1).map_err(|e| e.kind()),
817+
Err(AllocError { .. }),
818+
"isize::MAX + 1 should trigger an OOM!"
819+
);
820+
821+
assert_matches!(
822+
empty_string.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind()),
823+
Err(AllocError { .. }),
824+
"usize::MAX should trigger an OOM!"
825+
);
826+
}
783827
}
784828

785829
{
@@ -795,13 +839,19 @@ fn test_try_reserve_exact() {
795839
{
796840
panic!("isize::MAX shouldn't trigger an overflow!");
797841
}
798-
799-
assert_matches!(
800-
ten_bytes.try_reserve_exact(MAX_CAP - 9).map_err(|e| e.kind()),
801-
Err(CapacityOverflow),
802-
"isize::MAX + 1 should trigger an overflow!"
803-
);
804-
842+
if guards_against_isize {
843+
assert_matches!(
844+
ten_bytes.try_reserve_exact(MAX_CAP - 9).map_err(|e| e.kind()),
845+
Err(CapacityOverflow),
846+
"isize::MAX + 1 should trigger an overflow!"
847+
);
848+
} else {
849+
assert_matches!(
850+
ten_bytes.try_reserve_exact(MAX_CAP - 9).map_err(|e| e.kind()),
851+
Err(AllocError { .. }),
852+
"isize::MAX + 1 should trigger an OOM!"
853+
);
854+
}
805855
assert_matches!(
806856
ten_bytes.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind()),
807857
Err(CapacityOverflow),

Diff for: library/alloc/tests/vec.rs

+114-52
Original file line numberDiff line numberDiff line change
@@ -1527,6 +1527,12 @@ fn test_try_reserve() {
15271527
const MAX_CAP: usize = isize::MAX as usize;
15281528
const MAX_USIZE: usize = usize::MAX;
15291529

1530+
// On 16/32-bit, we check that allocations don't exceed isize::MAX,
1531+
// on 64-bit, we assume the OS will give an OOM for such a ridiculous size.
1532+
// Any platform that succeeds for these requests is technically broken with
1533+
// ptr::offset because LLVM is the worst.
1534+
let guards_against_isize = usize::BITS < 64;
1535+
15301536
{
15311537
// Note: basic stuff is checked by test_reserve
15321538
let mut empty_bytes: Vec<u8> = Vec::new();
@@ -1540,19 +1546,35 @@ fn test_try_reserve() {
15401546
panic!("isize::MAX shouldn't trigger an overflow!");
15411547
}
15421548

1543-
// Check isize::MAX + 1 does count as overflow
1544-
assert_matches!(
1545-
empty_bytes.try_reserve(MAX_CAP + 1).map_err(|e| e.kind()),
1546-
Err(CapacityOverflow),
1547-
"isize::MAX + 1 should trigger an overflow!"
1548-
);
1549-
1550-
// Check usize::MAX does count as overflow
1551-
assert_matches!(
1552-
empty_bytes.try_reserve(MAX_USIZE).map_err(|e| e.kind()),
1553-
Err(CapacityOverflow),
1554-
"usize::MAX should trigger an overflow!"
1555-
);
1549+
if guards_against_isize {
1550+
// Check isize::MAX + 1 does count as overflow
1551+
assert_matches!(
1552+
empty_bytes.try_reserve(MAX_CAP + 1).map_err(|e| e.kind()),
1553+
Err(CapacityOverflow),
1554+
"isize::MAX + 1 should trigger an overflow!"
1555+
);
1556+
1557+
// Check usize::MAX does count as overflow
1558+
assert_matches!(
1559+
empty_bytes.try_reserve(MAX_USIZE).map_err(|e| e.kind()),
1560+
Err(CapacityOverflow),
1561+
"usize::MAX should trigger an overflow!"
1562+
);
1563+
} else {
1564+
// Check isize::MAX + 1 is an OOM
1565+
assert_matches!(
1566+
empty_bytes.try_reserve(MAX_CAP + 1).map_err(|e| e.kind()),
1567+
Err(AllocError { .. }),
1568+
"isize::MAX + 1 should trigger an OOM!"
1569+
);
1570+
1571+
// Check usize::MAX is an OOM
1572+
assert_matches!(
1573+
empty_bytes.try_reserve(MAX_USIZE).map_err(|e| e.kind()),
1574+
Err(AllocError { .. }),
1575+
"usize::MAX should trigger an OOM!"
1576+
);
1577+
}
15561578
}
15571579

15581580
{
@@ -1565,13 +1587,19 @@ fn test_try_reserve() {
15651587
if let Err(CapacityOverflow) = ten_bytes.try_reserve(MAX_CAP - 10).map_err(|e| e.kind()) {
15661588
panic!("isize::MAX shouldn't trigger an overflow!");
15671589
}
1568-
1569-
assert_matches!(
1570-
ten_bytes.try_reserve(MAX_CAP - 9).map_err(|e| e.kind()),
1571-
Err(CapacityOverflow),
1572-
"isize::MAX + 1 should trigger an overflow!"
1573-
);
1574-
1590+
if guards_against_isize {
1591+
assert_matches!(
1592+
ten_bytes.try_reserve(MAX_CAP - 9).map_err(|e| e.kind()),
1593+
Err(CapacityOverflow),
1594+
"isize::MAX + 1 should trigger an overflow!"
1595+
);
1596+
} else {
1597+
assert_matches!(
1598+
ten_bytes.try_reserve(MAX_CAP - 9).map_err(|e| e.kind()),
1599+
Err(AllocError { .. }),
1600+
"isize::MAX + 1 should trigger an OOM!"
1601+
);
1602+
}
15751603
// Should always overflow in the add-to-len
15761604
assert_matches!(
15771605
ten_bytes.try_reserve(MAX_USIZE).map_err(|e| e.kind()),
@@ -1592,13 +1620,19 @@ fn test_try_reserve() {
15921620
{
15931621
panic!("isize::MAX shouldn't trigger an overflow!");
15941622
}
1595-
1596-
assert_matches!(
1597-
ten_u32s.try_reserve(MAX_CAP / 4 - 9).map_err(|e| e.kind()),
1598-
Err(CapacityOverflow),
1599-
"isize::MAX + 1 should trigger an overflow!"
1600-
);
1601-
1623+
if guards_against_isize {
1624+
assert_matches!(
1625+
ten_u32s.try_reserve(MAX_CAP / 4 - 9).map_err(|e| e.kind()),
1626+
Err(CapacityOverflow),
1627+
"isize::MAX + 1 should trigger an overflow!"
1628+
);
1629+
} else {
1630+
assert_matches!(
1631+
ten_u32s.try_reserve(MAX_CAP / 4 - 9).map_err(|e| e.kind()),
1632+
Err(AllocError { .. }),
1633+
"isize::MAX + 1 should trigger an OOM!"
1634+
);
1635+
}
16021636
// Should fail in the mul-by-size
16031637
assert_matches!(
16041638
ten_u32s.try_reserve(MAX_USIZE - 20).map_err(|e| e.kind()),
@@ -1618,6 +1652,8 @@ fn test_try_reserve_exact() {
16181652
const MAX_CAP: usize = isize::MAX as usize;
16191653
const MAX_USIZE: usize = usize::MAX;
16201654

1655+
let guards_against_isize = size_of::<usize>() < 8;
1656+
16211657
{
16221658
let mut empty_bytes: Vec<u8> = Vec::new();
16231659

@@ -1630,17 +1666,31 @@ fn test_try_reserve_exact() {
16301666
panic!("isize::MAX shouldn't trigger an overflow!");
16311667
}
16321668

1633-
assert_matches!(
1634-
empty_bytes.try_reserve_exact(MAX_CAP + 1).map_err(|e| e.kind()),
1635-
Err(CapacityOverflow),
1636-
"isize::MAX + 1 should trigger an overflow!"
1637-
);
1638-
1639-
assert_matches!(
1640-
empty_bytes.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind()),
1641-
Err(CapacityOverflow),
1642-
"usize::MAX should trigger an overflow!"
1643-
);
1669+
if guards_against_isize {
1670+
assert_matches!(
1671+
empty_bytes.try_reserve_exact(MAX_CAP + 1).map_err(|e| e.kind()),
1672+
Err(CapacityOverflow),
1673+
"isize::MAX + 1 should trigger an overflow!"
1674+
);
1675+
1676+
assert_matches!(
1677+
empty_bytes.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind()),
1678+
Err(CapacityOverflow),
1679+
"usize::MAX should trigger an overflow!"
1680+
);
1681+
} else {
1682+
assert_matches!(
1683+
empty_bytes.try_reserve_exact(MAX_CAP + 1).map_err(|e| e.kind()),
1684+
Err(AllocError { .. }),
1685+
"isize::MAX + 1 should trigger an OOM!"
1686+
);
1687+
1688+
assert_matches!(
1689+
empty_bytes.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind()),
1690+
Err(AllocError { .. }),
1691+
"usize::MAX should trigger an OOM!"
1692+
);
1693+
}
16441694
}
16451695

16461696
{
@@ -1656,13 +1706,19 @@ fn test_try_reserve_exact() {
16561706
{
16571707
panic!("isize::MAX shouldn't trigger an overflow!");
16581708
}
1659-
1660-
assert_matches!(
1661-
ten_bytes.try_reserve_exact(MAX_CAP - 9).map_err(|e| e.kind()),
1662-
Err(CapacityOverflow),
1663-
"isize::MAX + 1 should trigger an overflow!"
1664-
);
1665-
1709+
if guards_against_isize {
1710+
assert_matches!(
1711+
ten_bytes.try_reserve_exact(MAX_CAP - 9).map_err(|e| e.kind()),
1712+
Err(CapacityOverflow),
1713+
"isize::MAX + 1 should trigger an overflow!"
1714+
);
1715+
} else {
1716+
assert_matches!(
1717+
ten_bytes.try_reserve_exact(MAX_CAP - 9).map_err(|e| e.kind()),
1718+
Err(AllocError { .. }),
1719+
"isize::MAX + 1 should trigger an OOM!"
1720+
);
1721+
}
16661722
assert_matches!(
16671723
ten_bytes.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind()),
16681724
Err(CapacityOverflow),
@@ -1683,13 +1739,19 @@ fn test_try_reserve_exact() {
16831739
{
16841740
panic!("isize::MAX shouldn't trigger an overflow!");
16851741
}
1686-
1687-
assert_matches!(
1688-
ten_u32s.try_reserve_exact(MAX_CAP / 4 - 9).map_err(|e| e.kind()),
1689-
Err(CapacityOverflow),
1690-
"isize::MAX + 1 should trigger an overflow!"
1691-
);
1692-
1742+
if guards_against_isize {
1743+
assert_matches!(
1744+
ten_u32s.try_reserve_exact(MAX_CAP / 4 - 9).map_err(|e| e.kind()),
1745+
Err(CapacityOverflow),
1746+
"isize::MAX + 1 should trigger an overflow!"
1747+
);
1748+
} else {
1749+
assert_matches!(
1750+
ten_u32s.try_reserve_exact(MAX_CAP / 4 - 9).map_err(|e| e.kind()),
1751+
Err(AllocError { .. }),
1752+
"isize::MAX + 1 should trigger an OOM!"
1753+
);
1754+
}
16931755
assert_matches!(
16941756
ten_u32s.try_reserve_exact(MAX_USIZE - 20).map_err(|e| e.kind()),
16951757
Err(CapacityOverflow),

0 commit comments

Comments
 (0)