forked frompostgres/postgres
- Notifications
You must be signed in to change notification settings - Fork6
Commit0e87dfe
committed
Harden memory context allocators against bogus chunk pointers.
Before commitc6e0fe1, functions such as AllocSetFree could prettysafely presume that they were given a valid chunk pointer for theirown type of context, because the indirect call through a memorycontext object and method struct would be very unlikely to workotherwise. But now, if pfree() is mistakenly invoked on a pointerto garbage, we have three chances in eight of ending up at one ofthese functions. That means we need to take extra measures toverify that we are looking at what we're supposed to be looking at,especially in debug builds.Hence, add code to verify that the chunk's back-link to a block headerleads to a memory context object that satisfies the right sort ofIsA() check. This is still a bit weaker than what we did before,but for the moment assume that an IsA() check is sufficient.As a compromise between speed and safety, implement these checksas Asserts when dealing with small chunks but plain test-and-elogswhen dealing with large (external) chunks. The latter case shouldnot be too performance-critical, but the former case probably is.In slab.c, all chunks are small; but nonetheless use a plain testin SlabRealloc, because that is certainly not performance-critical,indeed we should be suspicious that it's being called in error.In aset.c, additionally add some assertions that the "value" fieldof the chunk header is within the small range allowed for freelistindexes. Without that, we might find ourselves trying to wipemost of memory when CLOBBER_FREED_MEMORY is enabled, or scribblingon a "freelist header" that's far away from the context object.Eventually, field experience might show us that it's smarter forthese tests to be active always, but for now we'll try to getaway with just having them as assertions.While at it, also be more uniform about asserting that contextobjects passed as parameters are of the type we expect. Someplaces missed that altogether, and slab.c was for no very goodreason doing it differently from the other allocators.Discussion:https://postgr.es/m/3578387.1665244345@sss.pgh.pa.us1 parent235eb4d commit0e87dfe
3 files changed
+187
-51
lines changedLines changed: 84 additions & 30 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
132 | 132 |
| |
133 | 133 |
| |
134 | 134 |
| |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
135 | 139 |
| |
136 | 140 |
| |
137 | 141 |
| |
| |||
202 | 206 |
| |
203 | 207 |
| |
204 | 208 |
| |
205 |
| - | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
206 | 218 |
| |
207 | 219 |
| |
208 | 220 |
| |
| |||
530 | 542 |
| |
531 | 543 |
| |
532 | 544 |
| |
533 |
| - | |
534 |
| - | |
| 545 | + | |
535 | 546 |
| |
536 | 547 |
| |
537 | 548 |
| |
| |||
540 | 551 |
| |
541 | 552 |
| |
542 | 553 |
| |
| 554 | + | |
| 555 | + | |
| 556 | + | |
543 | 557 |
| |
544 | 558 |
| |
545 | 559 |
| |
| |||
598 | 612 |
| |
599 | 613 |
| |
600 | 614 |
| |
601 |
| - | |
602 |
| - | |
| 615 | + | |
603 | 616 |
| |
604 | 617 |
| |
605 | 618 |
| |
| |||
608 | 621 |
| |
609 | 622 |
| |
610 | 623 |
| |
| 624 | + | |
| 625 | + | |
| 626 | + | |
611 | 627 |
| |
612 | 628 |
| |
613 | 629 |
| |
| |||
994 | 1010 |
| |
995 | 1011 |
| |
996 | 1012 |
| |
997 |
| - | |
| 1013 | + | |
998 | 1014 |
| |
999 | 1015 |
| |
| 1016 | + | |
| 1017 | + | |
| 1018 | + | |
| 1019 | + | |
| 1020 | + | |
| 1021 | + | |
| 1022 | + | |
1000 | 1023 |
| |
1001 | 1024 |
| |
1002 | 1025 |
| |
| |||
1011 | 1034 |
| |
1012 | 1035 |
| |
1013 | 1036 |
| |
1014 |
| - | |
1015 |
| - | |
1016 |
| - | |
1017 |
| - | |
1018 |
| - | |
1019 |
| - | |
1020 |
| - | |
1021 |
| - | |
1022 | 1037 |
| |
1023 | 1038 |
| |
1024 | 1039 |
| |
| |||
1036 | 1051 |
| |
1037 | 1052 |
| |
1038 | 1053 |
| |
1039 |
| - | |
1040 | 1054 |
| |
1041 |
| - | |
| 1055 | + | |
| 1056 | + | |
1042 | 1057 |
| |
| 1058 | + | |
| 1059 | + | |
| 1060 | + | |
| 1061 | + | |
| 1062 | + | |
| 1063 | + | |
| 1064 | + | |
1043 | 1065 |
| |
1044 | 1066 |
| |
| 1067 | + | |
| 1068 | + | |
| 1069 | + | |
| 1070 | + | |
1045 | 1071 |
| |
1046 | 1072 |
| |
1047 | 1073 |
| |
| |||
1089 | 1115 |
| |
1090 | 1116 |
| |
1091 | 1117 |
| |
| 1118 | + | |
1092 | 1119 |
| |
1093 | 1120 |
| |
1094 | 1121 |
| |
| |||
1105 | 1132 |
| |
1106 | 1133 |
| |
1107 | 1134 |
| |
1108 |
| - | |
| 1135 | + | |
| 1136 | + | |
| 1137 | + | |
| 1138 | + | |
| 1139 | + | |
| 1140 | + | |
| 1141 | + | |
| 1142 | + | |
1109 | 1143 |
| |
1110 | 1144 |
| |
| 1145 | + | |
| 1146 | + | |
1111 | 1147 |
| |
1112 | 1148 |
| |
1113 | 1149 |
| |
| |||
1116 | 1152 |
| |
1117 | 1153 |
| |
1118 | 1154 |
| |
1119 |
| - | |
1120 |
| - | |
1121 |
| - | |
1122 |
| - | |
1123 |
| - | |
1124 |
| - | |
1125 |
| - | |
1126 | 1155 |
| |
1127 | 1156 |
| |
1128 | 1157 |
| |
| |||
1201 | 1230 |
| |
1202 | 1231 |
| |
1203 | 1232 |
| |
1204 |
| - | |
| 1233 | + | |
| 1234 | + | |
| 1235 | + | |
| 1236 | + | |
| 1237 | + | |
| 1238 | + | |
| 1239 | + | |
| 1240 | + | |
1205 | 1241 |
| |
1206 | 1242 |
| |
| 1243 | + | |
| 1244 | + | |
| 1245 | + | |
| 1246 | + | |
1207 | 1247 |
| |
1208 | 1248 |
| |
1209 | 1249 |
| |
| |||
1328 | 1368 |
| |
1329 | 1369 |
| |
1330 | 1370 |
| |
| 1371 | + | |
1331 | 1372 |
| |
1332 | 1373 |
| |
1333 | 1374 |
| |
| |||
1342 | 1383 |
| |
1343 | 1384 |
| |
1344 | 1385 |
| |
| 1386 | + | |
1345 | 1387 |
| |
1346 | 1388 |
| |
1347 | 1389 |
| |
1348 | 1390 |
| |
1349 | 1391 |
| |
| 1392 | + | |
1350 | 1393 |
| |
1351 | 1394 |
| |
1352 | 1395 |
| |
1353 |
| - | |
1354 |
| - | |
| 1396 | + | |
| 1397 | + | |
| 1398 | + | |
1355 | 1399 |
| |
1356 | 1400 |
| |
1357 | 1401 |
| |
| |||
1361 | 1405 |
| |
1362 | 1406 |
| |
1363 | 1407 |
| |
| 1408 | + | |
| 1409 | + | |
1364 | 1410 |
| |
1365 | 1411 |
| |
1366 | 1412 |
| |
| |||
1394 | 1440 |
| |
1395 | 1441 |
| |
1396 | 1442 |
| |
| 1443 | + | |
| 1444 | + | |
1397 | 1445 |
| |
1398 | 1446 |
| |
1399 | 1447 |
| |
| |||
1405 | 1453 |
| |
1406 | 1454 |
| |
1407 | 1455 |
| |
| 1456 | + | |
1408 | 1457 |
| |
1409 | 1458 |
| |
1410 | 1459 |
| |
1411 | 1460 |
| |
1412 |
| - | |
1413 | 1461 |
| |
1414 | 1462 |
| |
1415 |
| - | |
| 1463 | + | |
1416 | 1464 |
| |
1417 | 1465 |
| |
1418 | 1466 |
| |
| |||
1522 | 1570 |
| |
1523 | 1571 |
| |
1524 | 1572 |
| |
1525 |
| - | |
| 1573 | + | |
| 1574 | + | |
| 1575 | + | |
| 1576 | + | |
| 1577 | + | |
| 1578 | + | |
| 1579 | + | |
1526 | 1580 |
| |
1527 | 1581 |
| |
1528 | 1582 |
| |
|
Lines changed: 51 additions & 2 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
105 | 105 |
| |
106 | 106 |
| |
107 | 107 |
| |
| 108 | + | |
108 | 109 |
| |
109 | 110 |
| |
110 |
| - | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
111 | 119 |
| |
112 |
| - | |
| 120 | + | |
| 121 | + | |
113 | 122 |
| |
114 | 123 |
| |
115 | 124 |
| |
| |||
345 | 354 |
| |
346 | 355 |
| |
347 | 356 |
| |
| 357 | + | |
| 358 | + | |
348 | 359 |
| |
349 | 360 |
| |
350 | 361 |
| |
| |||
625 | 636 |
| |
626 | 637 |
| |
627 | 638 |
| |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
628 | 647 |
| |
629 | 648 |
| |
630 | 649 |
| |
631 | 650 |
| |
632 | 651 |
| |
633 | 652 |
| |
634 | 653 |
| |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
635 | 662 |
| |
636 | 663 |
| |
637 | 664 |
| |
| |||
723 | 750 |
| |
724 | 751 |
| |
725 | 752 |
| |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
726 | 761 |
| |
727 | 762 |
| |
728 | 763 |
| |
729 | 764 |
| |
730 | 765 |
| |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
731 | 774 |
| |
732 | 775 |
| |
733 | 776 |
| |
| |||
845 | 888 |
| |
846 | 889 |
| |
847 | 890 |
| |
| 891 | + | |
848 | 892 |
| |
849 | 893 |
| |
850 | 894 |
| |
| |||
863 | 907 |
| |
864 | 908 |
| |
865 | 909 |
| |
| 910 | + | |
866 | 911 |
| |
867 | 912 |
| |
868 | 913 |
| |
| |||
881 | 926 |
| |
882 | 927 |
| |
883 | 928 |
| |
| 929 | + | |
| 930 | + | |
884 | 931 |
| |
885 | 932 |
| |
886 | 933 |
| |
| |||
917 | 964 |
| |
918 | 965 |
| |
919 | 966 |
| |
| 967 | + | |
| 968 | + | |
920 | 969 |
| |
921 | 970 |
| |
922 | 971 |
| |
|
0 commit comments
Comments
(0)