Loading...
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
/*
 * Copyright (c) 2000-2021 Apple Computer, Inc. All rights reserved.
 *
 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
 *
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Apple Public Source License
 * Version 2.0 (the 'License'). You may not use this file except in
 * compliance with the License. The rights granted to you under the License
 * may not be used to create, or enable the creation or redistribution of,
 * unlawful or unlicensed copies of an Apple operating system, or to
 * circumvent, violate, or enable the circumvention or violation of, any
 * terms of an Apple operating system software license agreement.
 *
 * Please obtain a copy of the License at
 * http://www.opensource.apple.com/apsl/ and read it before using this file.
 *
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 * Please see the License for the specific language governing rights and
 * limitations under the License.
 *
 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
 */
/*
 * @OSF_COPYRIGHT@
 */
/*
 * Mach Operating System
 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
 * All Rights Reserved.
 *
 * Permission to use, copy, modify and distribute this software and its
 * documentation is hereby granted, provided that both the copyright
 * notice and this permission notice appear in all copies of the
 * software, derivative works or modified versions, and any portions
 * thereof, and that both notices appear in supporting documentation.
 *
 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
 *
 * Carnegie Mellon requests users of this software to return to
 *
 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
 *  School of Computer Science
 *  Carnegie Mellon University
 *  Pittsburgh PA 15213-3890
 *
 * any improvements or extensions that they make and grant Carnegie Mellon
 * the rights to redistribute these changes.
 */

#ifdef  KERNEL_PRIVATE

#ifndef _KERN_KALLOC_H_
#define _KERN_KALLOC_H_

#include <mach/machine/vm_types.h>
#include <mach/boolean.h>
#include <mach/vm_types.h>
#include <kern/zalloc.h>
#include <libkern/section_keywords.h>
#include <os/alloc_util.h>
#if XNU_KERNEL_PRIVATE
#include <kern/counter.h>
#endif /* XNU_KERNEL_PRIVATE */

__BEGIN_DECLS __ASSUME_PTR_ABI_SINGLE_BEGIN

/*!
 * @const KALLOC_SAFE_ALLOC_SIZE
 *
 * @brief
 * The maximum allocation size that is safe to allocate with Z_NOFAIL in kalloc.
 */
#define KALLOC_SAFE_ALLOC_SIZE  (16u * 1024u)

#if XNU_KERNEL_PRIVATE
/*!
 * @typedef kalloc_heap_t
 *
 * @abstract
 * A kalloc heap view represents a sub-accounting context
 * for a given kalloc heap.
 */
typedef struct kalloc_heap {
	zone_stats_t        kh_stats;
	const char         *__unsafe_indexable kh_name;
	zone_kheap_id_t     kh_heap_id;
	vm_tag_t            kh_tag;
	uint16_t            kh_type_hash;
	zone_id_t           kh_zstart;
	struct kalloc_heap *kh_views;
} *kalloc_heap_t;

/*!
 * @macro KALLOC_HEAP_DECLARE
 *
 * @abstract
 * (optionally) declare a kalloc heap view in a header.
 *
 * @discussion
 * Unlike kernel zones, new full blown heaps cannot be instantiated.
 * However new accounting views of the base heaps can be made.
 */
#define KALLOC_HEAP_DECLARE(var) \
	extern struct kalloc_heap var[1]

/**
 * @const KHEAP_DATA_PRIVATE
 *
 * @brief
 * The builtin heap for bags of pure bytes.
 *
 * @discussion
 * This set of kalloc zones should contain pure bags of bytes with no pointers
 * or length/offset fields.
 *
 * The zones forming the heap aren't sequestered from each other, however the
 * entire heap lives in a different submap from any other kernel allocation.
 *
 * The main motivation behind this separation is due to the fact that a lot of
 * these objects have been used by attackers to spray the heap to make it more
 * predictable while exploiting use-after-frees or overflows.
 *
 * Common attributes that make these objects useful for spraying includes
 * control of:
 * - Data in allocation
 * - Time of alloc and free (lifetime)
 * - Size of allocation
 */
KALLOC_HEAP_DECLARE(KHEAP_DATA_PRIVATE);

/**
 * @const KHEAP_DATA_SHARED
 *
 * @brief
 * The builtin heap for bags of pure bytes that get shared across components.
 *
 * @discussion
 * There's a further distinction that we can make between kalloc zones that
 * contain bag of bytes, which is based on their intended use. In particular
 * a number of pure data allocations are intended to be shared between kernel
 * and user or kernel and coprocessors (DMA). These allocations cannot
 * sustain the security XNU_KERNEL_RESTRICTED checks, therefore we isolate
 * them in a separated heap, to further increase the security guarantees
 * around KHEAP_DATA_PRIVATE.
 */
KALLOC_HEAP_DECLARE(KHEAP_DATA_SHARED);

/**
 * @const KHEAP_DEFAULT
 *
 * @brief
 * The builtin default core kernel kalloc heap.
 *
 * @discussion
 * This set of kalloc zones should contain other objects that don't have their
 * own security mitigations. The individual zones are themselves sequestered.
 */
KALLOC_HEAP_DECLARE(KHEAP_DEFAULT);

/**
 * @const KHEAP_KT_VAR
 *
 * @brief
 * Temporary heap for variable sized kalloc type allocations
 *
 * @discussion
 * This heap will be removed when logic for kalloc_type_var_views is added
 *
 */
KALLOC_HEAP_DECLARE(KHEAP_KT_VAR);

/*!
 * @macro KALLOC_HEAP_DEFINE
 *
 * @abstract
 * Defines a given kalloc heap view and what it points to.
 *
 * @discussion
 * Kalloc heaps are views over one of the pre-defined builtin heaps
 * (such as @c KHEAP_DATA_PRIVATE or @c KHEAP_DEFAULT). Instantiating
 * a new one allows for accounting of allocations through this view.
 *
 * Kalloc heap views are initialized during the @c STARTUP_SUB_ZALLOC phase,
 * as the last rank. If views on zones are created, these must have been
 * created before this stage.
 *
 * @param var           the name for the zone view.
 * @param name          a string describing the zone view.
 * @param heap_id       a @c KHEAP_ID_* constant.
 */
#define KALLOC_HEAP_DEFINE(var, name, heap_id) \
	SECURITY_READ_ONLY_LATE(struct kalloc_heap) var[1] = { { \
	    .kh_name = (name), \
	    .kh_heap_id = (heap_id), \
	} }; \
	STARTUP_ARG(ZALLOC, STARTUP_RANK_MIDDLE, kheap_startup_init, var)

/*
 * Helper functions to query the status of security policies
 * regarding data allocations. This has consequences for things
 * like dedicated kmem range for shared data allocations
 */
extern bool kalloc_is_restricted_data_mode_telemetry(void);
extern bool kalloc_is_restricted_data_mode_enforced(void);

/*
 * Allocations of type SO_NAME are known to not have pointers for
 * most platforms -- for macOS this is not guaranteed
 */
#if XNU_TARGET_OS_OSX
#define KHEAP_SONAME KHEAP_DEFAULT
#else /* XNU_TARGET_OS_OSX */
#define KHEAP_SONAME KHEAP_DATA_PRIVATE
#endif /* XNU_TARGET_OS_OSX */

#endif /* XNU_KERNEL_PRIVATE */

/*!
 * @enum kalloc_type_flags_t
 *
 * @brief
 * Flags that can be passed to @c KALLOC_TYPE_DEFINE
 *
 * @discussion
 * These flags can be used to request for a specific accounting
 * behavior.
 *
 * @const KT_DEFAULT
 * Passing this flag will provide default accounting behavior
 * i.e shared accounting unless toggled with KT_OPTIONS_ACCT is
 * set in kt boot-arg.
 *
 * @const KT_PRIV_ACCT
 * Passing this flag will provide individual stats for your
 * @c kalloc_type_view that is defined.
 *
 * @const KT_SHARED_ACCT
 * Passing this flag will accumulate stats as a part of the
 * zone that your @c kalloc_type_view points to.
 *
 * @const KT_DATA_ONLY
 * Represents that the type is "data-only" and is never shared
 * with another security domain. Adopters should not set
 * this flag manually, it is meant for the compiler to set
 * automatically when KALLOC_TYPE_CHECK(DATA) passes.
 *
 * @const KT_VM
 * Represents that the type is large enough to use the VM. Adopters
 * should not set this flag manually, it is meant for the compiler
 * to set automatically when KALLOC_TYPE_VM_SIZE_CHECK passes.
 *
 * @const KT_PTR_ARRAY
 * Represents that the type is an array of pointers. Adopters should not
 * set this flag manually, it is meant for the compiler to set
 * automatically when KALLOC_TYPE_CHECK(PTR) passes.
 *
 * @const KT_CHANGED*
 * Represents a change in the version of the kalloc_type_view. This
 * is required inorder to decouple requiring kexts to be rebuilt to
 * use the new defintions right away. This flags should not be used
 * manually at a callsite, it is meant for internal use only. Future
 * changes to kalloc_type_view defintion should toggle this flag.
 *
 #if XNU_KERNEL_PRIVATE
 * @const KT_NOEARLY
 * This flags will force the callsite to bypass the early (shared) zone and
 * directly allocate from the assigned zone. This can only be used
 * with KT_PRIV_ACCT right now. If you still require this behavior
 * but don't want private stats use Z_SET_NOT_EARLY at the allocation
 * callsite instead.
 *
 * @const KT_SLID
 * To indicate that strings in the view were slid during early boot.
 *
 * @const KT_PROCESSED
 * This flag is set once the view is parse during early boot. Views
 * that are not in BootKC on macOS aren't parsed and therefore will
 * not have this flag set. The runtime can use this as an indication
 * to appropriately redirect the call.
 *
 * @const KT_HASH
 * Hash of signature used by kmem_*_guard to determine range and
 * direction for allocation
 #endif
 */
__options_decl(kalloc_type_flags_t, uint32_t, {
	KT_DEFAULT        = 0x0001,
	KT_PRIV_ACCT      = 0x0002,
	KT_SHARED_ACCT    = 0x0004,
	KT_DATA_ONLY      = 0x0008,
	KT_VM             = 0x0010,
	KT_CHANGED        = 0x0020,
	KT_CHANGED2       = 0x0040,
	KT_PTR_ARRAY      = 0x0080,
#if XNU_KERNEL_PRIVATE
	KT_NOEARLY       = 0x2000,
	KT_SLID           = 0x4000,
	KT_PROCESSED      = 0x8000,
	KT_HASH           = 0xffff0000,
#endif
});

/*!
 * @typedef kalloc_type_view_t
 *
 * @abstract
 * A kalloc type view is a structure used to redirect callers
 * of @c kalloc_type to a particular zone based on the signature of
 * their type.
 *
 * @discussion
 * These structures are automatically created under the hood for every
 * @c kalloc_type and @c kfree_type callsite. They are ingested during startup
 * and are assigned zones based on the security policy for their signature.
 *
 * These structs are protected by the kernel lockdown and can't be initialized
 * dynamically. They must be created using @c KALLOC_TYPE_DEFINE() or
 * @c kalloc_type or @c kfree_type.
 *
 */
#if XNU_KERNEL_PRIVATE
struct kalloc_type_view {
	struct zone_view        kt_zv;
	const char             *kt_signature __unsafe_indexable;
	kalloc_type_flags_t     kt_flags;
	uint32_t                kt_size;
	zone_t                  kt_zearly;
	zone_t                  kt_zsig;
};
#else /* XNU_KERNEL_PRIVATE */
struct kalloc_type_view {
	struct zone_view        kt_zv;
	const char             *kt_signature __unsafe_indexable;
	kalloc_type_flags_t     kt_flags;
	uint32_t                kt_size;
	void                   *unused1;
	void                   *unused2;
};
#endif /* XNU_KERNEL_PRIVATE */

/*
 * The set of zones used by all kalloc heaps are defined by the constants
 * below.
 *
 * KHEAP_START_SIZE: Size of the first sequential zone.
 * KHEAP_MAX_SIZE  : Size of the last sequential zone.
 * KHEAP_STEP_WIDTH: Number of zones created at every step (power of 2).
 * KHEAP_STEP_START: Size of the first step.
 * We also create some extra initial zones that don't follow the sequence
 * for sizes 8 (on armv7 only), 16 and 32.
 *
 * idx step_increment   zone_elem_size
 * 0       -                  16
 * 1       -                  32
 * 2       16                 48
 * 3       16                 64
 * 4       32                 96
 * 5       32                 128
 * 6       64                 192
 * 7       64                 256
 * 8       128                384
 * 9       128                512
 * 10      256                768
 * 11      256                1024
 * 12      512                1536
 * 13      512                2048
 * 14      1024               3072
 * 15      1024               4096
 * 16      2048               6144
 * 17      2048               8192
 * 18      4096               12288
 * 19      4096               16384
 * 20      8192               24576
 * 21      8192               32768
 */
#define kalloc_log2down(mask)   (31 - __builtin_clz(mask))
#define KHEAP_START_SIZE        32
#if  __x86_64__
#define KHEAP_MAX_SIZE          (16 * 1024)
#define KHEAP_EXTRA_ZONES       2
#else
#define KHEAP_MAX_SIZE          (32 * 1024)
#define KHEAP_EXTRA_ZONES       2
#endif
#define KHEAP_STEP_WIDTH        2
#define KHEAP_STEP_START        16
#define KHEAP_START_IDX         kalloc_log2down(KHEAP_START_SIZE)
#define KHEAP_NUM_STEPS         (kalloc_log2down(KHEAP_MAX_SIZE) - \
	                                kalloc_log2down(KHEAP_START_SIZE))
#define KHEAP_NUM_ZONES         (KHEAP_NUM_STEPS * KHEAP_STEP_WIDTH + \
	                                KHEAP_EXTRA_ZONES)

/*!
 * @enum kalloc_type_version_t
 *
 * @brief
 * Enum that holds versioning information for @c kalloc_type_var_view
 *
 * @const KT_V1
 * Version 1
 *
 */
__options_decl(kalloc_type_version_t, uint16_t, {
	KT_V1             = 0x0001,
});

/*!
 * @typedef kalloc_type_var_view_t
 *
 * @abstract
 * This structure is analoguous to @c kalloc_type_view but handles
 * @c kalloc_type callsites that are variable in size.
 *
 * @discussion
 * These structures are automatically created under the hood for every
 * variable sized @c kalloc_type and @c kfree_type callsite. They are ingested
 * during startup and are assigned zones based on the security policy for
 * their signature.
 *
 * These structs are protected by the kernel lockdown and can't be initialized
 * dynamically. They must be created using @c KALLOC_TYPE_VAR_DEFINE() or
 * @c kalloc_type or @c kfree_type.
 *
 */
struct kalloc_type_var_view {
	kalloc_type_version_t   kt_version;
	uint16_t                kt_size_hdr;
	/*
	 * Temporary: Needs to be 32bits cause we have many structs that use
	 * IONew/Delete that are larger than 32K.
	 */
	uint32_t                kt_size_type;
	zone_stats_t            kt_stats;
	const char             *__unsafe_indexable kt_name;
	zone_view_t             kt_next;
	zone_id_t               kt_heap_start;
	uint8_t                 kt_zones[KHEAP_NUM_ZONES];
	const char             * __unsafe_indexable kt_sig_hdr;
	const char             * __unsafe_indexable kt_sig_type;
	kalloc_type_flags_t     kt_flags;
};

typedef struct kalloc_type_var_view *kalloc_type_var_view_t;

/*!
 * @macro KALLOC_TYPE_DECLARE
 *
 * @abstract
 * (optionally) declares a kalloc type view (in a header).
 *
 * @param var           the name for the kalloc type view.
 */
#define KALLOC_TYPE_DECLARE(var) \
	extern struct kalloc_type_view var[1]

/*!
 * @macro KALLOC_TYPE_DEFINE
 *
 * @abstract
 * Defines a given kalloc type view with prefered accounting
 *
 * @discussion
 * This macro allows you to define a kalloc type with private
 * accounting. The defined kalloc_type_view can be used with
 * kalloc_type_impl/kfree_type_impl to allocate/free memory.
 * zalloc/zfree can also be used from inside xnu. However doing
 * so doesn't handle freeing a NULL pointer or the use of tags.
 *
 * @param var           the name for the kalloc type view.
 * @param type          the type of your allocation.
 * @param flags         a @c KT_* flag.
 */
#define KALLOC_TYPE_DEFINE(var, type, flags) \
	_KALLOC_TYPE_DEFINE(var, type, flags); \
	__ZONE_DECLARE_TYPE(var, type)

/*!
 * @macro KALLOC_TYPE_VAR_DECLARE
 *
 * @abstract
 * (optionally) declares a kalloc type var view (in a header).
 *
 * @param var           the name for the kalloc type var view.
 */
#define KALLOC_TYPE_VAR_DECLARE(var) \
	extern struct kalloc_type_var_view var[1]

/*!
 * @macro KALLOC_TYPE_VAR_DEFINE
 *
 * @abstract
 * Defines a given kalloc type view with prefered accounting for
 * variable sized typed allocations.
 *
 * @discussion
 * As the views aren't yet being ingested, individual stats aren't
 * available. The defined kalloc_type_var_view should be used with
 * kalloc_type_var_impl/kfree_type_var_impl to allocate/free memory.
 *
 * This macro comes in 2 variants:
 *
 * 1. @c KALLOC_TYPE_VAR_DEFINE(var, e_ty, flags)
 * 2. @c KALLOC_TYPE_VAR_DEFINE(var, h_ty, e_ty, flags)
 *
 * @param var           the name for the kalloc type var view.
 * @param h_ty          the type of header in the allocation.
 * @param e_ty          the type of repeating part in the allocation.
 * @param flags         a @c KT_* flag.
 */
#define KALLOC_TYPE_VAR_DEFINE(...) KALLOC_DISPATCH(KALLOC_TYPE_VAR_DEFINE, ##__VA_ARGS__)

#ifdef XNU_KERNEL_PRIVATE

/*
 * These versions allow specifying the kalloc heap to allocate memory
 * from
 */
#define kheap_alloc_tag(kalloc_heap, size, flags, itag) \
	__kheap_alloc(kalloc_heap, size, __zone_flags_mix_tag(flags, itag), NULL)
#define kheap_alloc(kalloc_heap, size, flags) \
	kheap_alloc_tag(kalloc_heap, size, flags, VM_ALLOC_SITE_TAG())

/*
 * These versions should be used for allocating pure data bytes that
 * do not contain any pointers
 */
#define kalloc_data_tag(size, flags, itag) \
	kheap_alloc_tag(KHEAP_DATA_PRIVATE, size, flags, itag)
#define kalloc_data(size, flags) \
	kheap_alloc(KHEAP_DATA_PRIVATE, size, flags)

#define kalloc_shared_data_tag(size, flags, itag) \
	kheap_alloc_tag(KHEAP_DATA_SHARED, size, flags, itag)
#define kalloc_shared_data(size, flags) \
	kheap_alloc(KHEAP_DATA_SHARED, size, flags)

#define krealloc_data_tag(elem, old_size, new_size, flags, itag) \
	__kheap_realloc(KHEAP_DATA_PRIVATE, elem, old_size, new_size, \
	    __zone_flags_mix_tag(flags, itag), NULL)
#define krealloc_data(elem, old_size, new_size, flags) \
	krealloc_data_tag(elem, old_size, new_size, flags, \
	    VM_ALLOC_SITE_TAG())

#define krealloc_shared_data_tag(elem, old_size, new_size, flags, itag) \
	__kheap_realloc(KHEAP_DATA_SHARED, elem, old_size, new_size, \
	    __zone_flags_mix_tag(flags, itag), NULL)
#define krealloc_shared_data(elem, old_size, new_size, flags) \
	krealloc_shared_data_tag(elem, old_size, new_size, flags, \
	    VM_ALLOC_SITE_TAG())

#define kfree_data(elem, size) \
	kheap_free(KHEAP_DATA_PRIVATE, elem, size);

#define kfree_data_addr(elem) \
	kheap_free_addr(KHEAP_DATA_PRIVATE, elem);

#define kfree_shared_data(elem, size) \
	kheap_free(KHEAP_DATA_SHARED, elem, size);

#define kfree_shared_data_addr(elem) \
	kheap_free_addr(KHEAP_DATA_SHARED, elem);

extern void kheap_free_bounded(
	kalloc_heap_t heap,
	void         *addr __unsafe_indexable,
	vm_size_t     min_sz,
	vm_size_t     max_sz);

extern void kalloc_data_require(
	void         *data __unsafe_indexable,
	vm_size_t     size);

extern void kalloc_non_data_require(
	void         *data __unsafe_indexable,
	vm_size_t     size);

extern bool kalloc_is_data_private(
	void         *addr,
	vm_size_t    size);

#else /* XNU_KERNEL_PRIVATE */

extern void *__sized_by(size) kalloc(
	vm_size_t           size) __attribute__((malloc, alloc_size(1)));

extern void *__unsafe_indexable kalloc_data(
	vm_size_t           size,
	zalloc_flags_t      flags);

extern void *__unsafe_indexable kalloc_shared_data(
	vm_size_t           size,
	zalloc_flags_t      flags);

__attribute__((malloc, alloc_size(1)))
static inline void *
__sized_by(size)
__kalloc_data(vm_size_t size, zalloc_flags_t flags)
{
	void *__unsafe_indexable addr = (kalloc_data)(size, flags);
	if (flags & Z_NOFAIL) {
		__builtin_assume(addr != NULL);
	}
	return addr ? __unsafe_forge_bidi_indexable(uint8_t *, addr, size) : NULL;
}

__attribute__((malloc, alloc_size(1)))
static inline void *
__sized_by(size)
__kalloc_shared_data(vm_size_t size, zalloc_flags_t flags)
{
	void *__unsafe_indexable addr = (kalloc_shared_data)(size, flags);
	if (flags & Z_NOFAIL) {
		__builtin_assume(addr != NULL);
	}
	return addr ? __unsafe_forge_bidi_indexable(uint8_t *, addr, size) : NULL;
}

#define kalloc_data(size, fl) __kalloc_data(size, fl)

#define kalloc_shared_data(size, fl) __kalloc_shared_data(size, fl)

extern void *__unsafe_indexable krealloc_data(
	void               *ptr __unsafe_indexable,
	vm_size_t           old_size,
	vm_size_t           new_size,
	zalloc_flags_t      flags);

extern void *__unsafe_indexable krealloc_shared_data(
	void               *ptr __unsafe_indexable,
	vm_size_t           old_size,
	vm_size_t           new_size,
	zalloc_flags_t      flags);

__attribute__((malloc, alloc_size(3)))
static inline void *
__sized_by(new_size)
__krealloc_data(
	void               *ptr __sized_by(old_size),
	vm_size_t           old_size,
	vm_size_t           new_size,
	zalloc_flags_t      flags)
{
	void *__unsafe_indexable addr = (krealloc_data)(ptr, old_size, new_size, flags);
	if (flags & Z_NOFAIL) {
		__builtin_assume(addr != NULL);
	}
	return addr ? __unsafe_forge_bidi_indexable(uint8_t *, addr, new_size) : NULL;
}

__attribute__((malloc, alloc_size(3)))
static inline void *
__sized_by(new_size)
__krealloc_shared_data(
	void               *ptr __sized_by(old_size),
	vm_size_t           old_size,
	vm_size_t           new_size,
	zalloc_flags_t      flags)
{
	void *__unsafe_indexable addr = (krealloc_shared_data)(ptr, old_size, new_size, flags);
	if (flags & Z_NOFAIL) {
		__builtin_assume(addr != NULL);
	}
	return addr ? __unsafe_forge_bidi_indexable(uint8_t *, addr, new_size) : NULL;
}

#define krealloc_data(ptr, old_size, new_size, fl) \
	__krealloc_data(ptr, old_size, new_size, fl)

#define krealloc_shared_data(ptr, old_size, new_size, fl) \
	__krealloc_shared_data(ptr, old_size, new_size, fl)

extern void kfree(
	void               *data __unsafe_indexable,
	vm_size_t           size);

extern void kfree_data(
	void               *ptr __unsafe_indexable,
	vm_size_t           size);

extern void kfree_data_addr(
	void               *ptr __unsafe_indexable);

extern void kfree_shared_data(
	void               *ptr __unsafe_indexable,
	vm_size_t           size);

extern void kfree_shared_data_addr(
	void               *ptr __unsafe_indexable);

#endif /* !XNU_KERNEL_PRIVATE */

/*!
 * @macro kalloc_type
 *
 * @abstract
 * Allocates element of a particular type
 *
 * @discussion
 * This family of allocators segregate kalloc allocations based on their type.
 *
 * This macro comes in 3 variants:
 *
 * 1. @c kalloc_type(type, flags)
 *    Use this macro for fixed sized allocation of a particular type.
 *
 * 2. @c kalloc_type(e_type, count, flags)
 *    Use this macro for variable sized allocations that form an array,
 *    do note that @c kalloc_type(e_type, 1, flags) is not equivalent to
 *    @c kalloc_type(e_type, flags).
 *
 * 3. @c kalloc_type(hdr_type, e_type, count, flags)
 *    Use this macro for variable sized allocations formed with
 *    a header of type @c hdr_type followed by a variable sized array
 *    with elements of type @c e_type, equivalent to this:
 *
 *    <code>
 *    struct {
 *        hdr_type hdr;
 *        e_type   arr[];
 *    }
 *    </code>
 *
 * @param flags         @c zalloc_flags_t that get passed to zalloc_internal
 */
#define kalloc_type(...)  KALLOC_DISPATCH(kalloc_type, ##__VA_ARGS__)

/*!
 * @macro kfree_type
 *
 * @abstract
 * Frees element of a particular type
 *
 * @discussion
 * This pairs with the @c kalloc_type() that was made to allocate this element.
 * Arguments passed to @c kfree_type() must match the one passed at allocation
 * time precisely.
 *
 * This macro comes in the same 3 variants kalloc_type() does:
 *
 * 1. @c kfree_type(type, elem)
 * 2. @c kfree_type(e_type, count, elem)
 * 3. @c kfree_type(hdr_type, e_type, count, elem)
 *
 * @param elem          The address of the element to free
 */
#define kfree_type(...)  KALLOC_DISPATCH(kfree_type, ##__VA_ARGS__)
#define kfree_type_counted_by(type, count, elem) \
	kfree_type_counted_by_3(type, count, elem)

#ifdef XNU_KERNEL_PRIVATE
#define kalloc_type_tag(...)     KALLOC_DISPATCH(kalloc_type_tag, ##__VA_ARGS__)
#define krealloc_type_tag(...)   KALLOC_DISPATCH(krealloc_type_tag, ##__VA_ARGS__)
#define krealloc_type(...)       KALLOC_DISPATCH(krealloc_type, ##__VA_ARGS__)

/*
 * kalloc_type_require can't be made available to kexts as the
 * kalloc_type_view's zone could be NULL in the following cases:
 * - Size greater than KALLOC_SAFE_ALLOC_SIZE
 * - On macOS, if call is not in BootKC
 * - All allocations in kext for armv7
 */
#define kalloc_type_require(type, value) ({                                    \
	static _KALLOC_TYPE_DEFINE(kt_view_var, type, KT_SHARED_ACCT);         \
	zone_require(kt_view_var->kt_zv.zv_zone, value);                       \
})

#endif

/*!
 * @enum kt_granule_t
 *
 * @brief
 * Granule encodings used by the compiler for the type signature.
 *
 * @discussion
 * Given a type, the XNU signature type system (__builtin_xnu_type_signature)
 * produces a signature by analyzing its memory layout, in chunks of 8 bytes,
 * which we call granules. The encoding produced for each granule is the
 * bitwise or of the encodings of all the types of the members included
 * in that granule.
 *
 * @const KT_GRANULE_PADDING
 * Represents padding inside a record type.
 *
 * @const KT_GRANULE_POINTER
 * Represents a pointer type.
 *
 * @const KT_GRANULE_DATA
 * Represents a scalar type that is not a pointer.
 *
 * @const KT_GRANULE_DUAL
 * Currently unused.
 *
 * @const KT_GRANULE_PAC
 * Represents a pointer which is subject to PAC.
 */
__options_decl(kt_granule_t, uint32_t, {
	KT_GRANULE_PADDING = 0,
	KT_GRANULE_POINTER = 1,
	KT_GRANULE_DATA    = 2,
	KT_GRANULE_DUAL    = 4,
	KT_GRANULE_PAC     = 8
});

#define KT_GRANULE_MAX                                                \
	(KT_GRANULE_PADDING | KT_GRANULE_POINTER | KT_GRANULE_DATA |  \
	    KT_GRANULE_DUAL | KT_GRANULE_PAC)

/*
 * Convert a granule encoding to the index of the bit that
 * represents such granule in the type summary.
 *
 * The XNU type summary (__builtin_xnu_type_summary) produces a 32-bit
 * summary of the type signature of a given type. If the bit at index
 * (1 << G) is set in the summary, that means that the type contains
 * one or more granules with encoding G.
 */
#define KT_SUMMARY_GRANULE_TO_IDX(g)  (1UL << (g))

#define KT_SUMMARY_MASK_TYPE_BITS  (0xffff)

#define KT_SUMMARY_MASK_DATA                             \
	(KT_SUMMARY_GRANULE_TO_IDX(KT_GRANULE_PADDING) |  \
	    KT_SUMMARY_GRANULE_TO_IDX(KT_GRANULE_DATA))

#define KT_SUMMARY_MASK_PTR                              \
	(KT_SUMMARY_GRANULE_TO_IDX(KT_GRANULE_PADDING) |     \
	    KT_SUMMARY_GRANULE_TO_IDX(KT_GRANULE_POINTER) |  \
	    KT_SUMMARY_GRANULE_TO_IDX(KT_GRANULE_PAC))

#define KT_SUMMARY_MASK_ALL_GRANULES                        \
	(KT_SUMMARY_GRANULE_TO_IDX(KT_GRANULE_PADDING) |     \
	    KT_SUMMARY_GRANULE_TO_IDX(KT_GRANULE_POINTER) |  \
	    KT_SUMMARY_GRANULE_TO_IDX(KT_GRANULE_DATA) |     \
	    KT_SUMMARY_GRANULE_TO_IDX(KT_GRANULE_DUAL) |     \
	    KT_SUMMARY_GRANULE_TO_IDX(KT_GRANULE_PAC))

/*!
 * @macro KT_SUMMARY_GRANULES
 *
 * @abstract
 * Return the granule type summary for a given type
 *
 * @discussion
 * This macro computes the type summary of a type, and it then extracts the
 * bits which carry information about the granules in the memory layout.
 *
 * Note: you should never have to use __builtin_xnu_type_summary
 * directly, as we reserve the right to use the remaining bits with
 * different semantics.
 *
 * @param type          The type to analyze
 */
#define KT_SUMMARY_GRANULES(type) \
	(__builtin_xnu_type_summary(type) & KT_SUMMARY_MASK_TYPE_BITS)

/*!
 * @macro KALLOC_TYPE_SIG_CHECK
 *
 * @abstract
 * Return whether a given type is only made up of granules specified in mask
 *
 * @param mask          Granules to check for
 * @param type          The type to analyze
 */
#define KALLOC_TYPE_SIG_CHECK(mask, type) \
	((KT_SUMMARY_GRANULES(type) & ~(mask)) == 0)

/*!
 * @macro KALLOC_TYPE_IS_DATA_ONLY
 *
 * @abstract
 * Return whether a given type is considered a data-only type.
 *
 * @param type          The type to analyze
 */
#define KALLOC_TYPE_IS_DATA_ONLY(type) \
	KALLOC_TYPE_SIG_CHECK(KT_SUMMARY_MASK_DATA, type)

/*!
 * @macro KALLOC_TYPE_HAS_OVERLAPS
 *
 * @abstract
 * Return whether a given type has overlapping granules.
 *
 * @discussion
 * This macro returns whether the memory layout for a given type contains
 * overlapping granules. An overlapping granule is a granule which includes
 * members with types that have different encodings under the XNU signature
 * type system.
 *
 * @param type          The type to analyze
 */
#define KALLOC_TYPE_HAS_OVERLAPS(type) \
	((KT_SUMMARY_GRANULES(type) & ~KT_SUMMARY_MASK_ALL_GRANULES) != 0)

/*!
 * @macro KALLOC_TYPE_IS_COMPATIBLE_PTR
 *
 * @abstract
 * Return whether pointer is compatible with a given type, in the XNU
 * signature type system.
 *
 * @discussion
 * This macro returns whether type pointed to by @c ptr is either the same
 * type as @c type, or it has the same signature. The implementation relies
 * on the @c __builtin_xnu_types_compatible builtin, and the value returned
 * can be evaluated at compile time in both C and C++.
 *
 * Note: void pointers are treated as wildcards, and are thus compatible
 * with any given type.
 *
 * @param ptr           the pointer whose type needs to be checked.
 * @param type          the type which the pointer will be checked against.
 */
#define KALLOC_TYPE_IS_COMPATIBLE_PTR(ptr, type)                         \
	(__builtin_xnu_types_compatible(os_get_pointee_type(ptr), type) ||   \
	    __builtin_xnu_types_compatible(os_get_pointee_type(ptr), void))  \

#define KALLOC_TYPE_ASSERT_COMPATIBLE_POINTER(ptr, type) \
	_Static_assert(KALLOC_TYPE_IS_COMPATIBLE_PTR(ptr, type), \
	    "Pointer type is not compatible with specified type")


/*!
 * @const KALLOC_ARRAY_SIZE_MAX
 *
 * @brief
 * The maximum size that can be allocated with the @c KALLOC_ARRAY interface.
 *
 * @discussion
 * This size is:
 * - ~256M on 4k or PAC systems with 16k pages
 * - ~1G on other 16k systems.
 */
#if __arm64e__ || KASAN_TBI
#define KALLOC_ARRAY_SIZE_MAX   ((uint32_t)PAGE_MASK << PAGE_SHIFT)
#define KALLOC_ARRAY_GRANULE    32ul
#else
#define KALLOC_ARRAY_SIZE_MAX   ((uint32_t)UINT16_MAX << PAGE_SHIFT)
#define KALLOC_ARRAY_GRANULE    16ul
#endif

/*!
 * @macro KALLOC_ARRAY_TYPE_DECL
 *
 * @brief
 * Declares a type used as a packed kalloc array type.
 *
 * @discussion
 * This macro comes in two variants
 *
 * - KALLOC_ARRAY_TYPE_DECL(name, e_ty)
 * - KALLOC_ARRAY_TYPE_DECL(name, h_ty, e_ty)
 *
 * The first one defines an array of elements of type @c e_ty,
 * and the second a header of type @c h_ty followed by
 * an array of elements of type @c e_ty.
 *
 * Those macros will then define the type @c ${name}_t as a typedef
 * to a non existent structure type, in order to avoid accidental
 * dereference of those pointers.
 *
 * kalloc array pointers are actually pointers that in addition to encoding
 * the array base pointer, also encode the allocation size (only sizes
 * up to @c KALLOC_ARRAY_SIZE_MAX bytes).
 *
 * Such pointers can be signed with data PAC properly, which will provide
 * integrity of both the base pointer, and its size.
 *
 * kalloc arrays are useful to use instead of embedding the length
 * of the allocation inside of itself, which tends to be driven by:
 *
 * - a desire to not grow the outer structure holding the pointer
 *   to this array with an extra "length" field for optional arrays,
 *   in order to save memory (see the @c ip_requests field in ports),
 *
 * - a need to be able to atomically consult the size of an allocation
 *   with respect to loading its pointer (where address dependencies
 *   traditionally gives this property) for lockless algorithms
 *   (see the IPC space table).
 *
 * Using a kalloc array is preferable for two reasons:
 *
 * - embedding lengths inside the allocation is self-referential
 *   and an appetizing target for post-exploitation strategies,
 *
 * - having a dependent load to get to the length loses out-of-order
 *   opportunities for the CPU and prone to back-to-back cache misses.
 *
 * Holding information such as a level of usage of this array
 * within itself is fine provided those quantities are validated
 * against the "count" (number of elements) or "size" (allocation
 * size in bytes) of the array before use.
 *
 *
 * This macro will define a series of functions:
 *
 * - ${name}_count_to_size() and ${name}_size_to_count()
 *   to convert between memory sizes and array element counts
 *   (taking the header size into account when it exists);
 *
 *   Note that those functions assume the count/size are corresponding
 *   to a valid allocation size within [0, KALLOC_ARRAY_SIZE_MAX].
 *
 * - ${name}_next_size() to build good allocation growth policies;
 *
 * - ${name}_base() returning a (bound-checked indexable) pointer
 *   to the header of the array (or its first element when there is
 *   no header);
 *
 * - ${name}_begin() returning a (bound-checked indexable)
 *   pointer to the first element of the the array;
 *
 * - ${name}_contains() to check if an element index is within
 *   the valid range of this allocation;
 *
 * - ${name}_next_elem() to get the next element of an array.
 *
 * - ${name}_get() and ${name}_get_nocheck() to return a pointer
 *   to a given cell of the array with (resp. without) a bound
 *   check against the array size. The bound-checked variant
 *   returns NULL for invalid indexes.
 *
 * - ${name}_alloc_by_count() and ${name}_alloc_by_size()
 *   to allocate a new array able to hold at least that many elements
 *   (resp. bytes).
 *
 * - ${name}_realloc_by_count() and ${name}_realloc_by_size()
 *   to re-allocate a new array able to hold at least that many elements
 *   (resp. bytes).
 *
 * - ${name}_free() and ${name}_free_noclear() to free such an array
 *   (resp. without nil-ing the pointer). The non-clearing variant
 *   is to be used only when nil-ing out the pointer is otherwise
 *   not allowed by C (const value, unable to take address of, ...),
 *   otherwise the normal ${name}_free() must be used.
 */
#define KALLOC_ARRAY_TYPE_DECL(...) \
	KALLOC_DISPATCH(KALLOC_ARRAY_TYPE_DECL, ##__VA_ARGS__)

#if XNU_KERNEL_PRIVATE

#define KALLOC_ARRAY_TYPE_DECL_(name, h_type_t, h_sz, e_type_t, e_sz) \
	KALLOC_TYPE_VAR_DECLARE(name ## _kt_view);                              \
	typedef struct name * __unsafe_indexable name ## _t;                    \
                                                                                \
	__pure2                                                                 \
	static inline uint32_t                                                  \
	name ## _count_to_size(uint32_t count)                                  \
	{                                                                       \
	        return (uint32_t)((h_sz) + (e_sz) * count);                     \
	}                                                                       \
                                                                                \
	__pure2                                                                 \
	static inline uint32_t                                                  \
	name ## _size_to_count(vm_size_t size)                                  \
	{                                                                       \
	        return (uint32_t)((size - (h_sz)) / (e_sz));                    \
	}                                                                       \
                                                                                \
	__pure2                                                                 \
	static inline uint32_t                                                  \
	name ## _size(name ## _t array)                                         \
	{                                                                       \
	        return __kalloc_array_size((vm_address_t)array);                \
	}                                                                       \
                                                                                \
	__pure2                                                                 \
	static inline uint32_t                                                  \
	name ## _next_size(                                                     \
	        uint32_t                min_count,                              \
	        vm_size_t               cur_size,                               \
	        uint32_t                vm_period)                              \
	{                                                                       \
	        vm_size_t size;                                                 \
                                                                                \
	        if (cur_size) {                                                 \
	                size = cur_size + (e_sz) - 1;                           \
	        } else {                                                        \
	                size = kt_size(h_sz, e_sz, min_count) - 1;              \
	        }                                                               \
	        size  = kalloc_next_good_size(size, vm_period);                 \
	        if (size <= KALLOC_ARRAY_SIZE_MAX) {                            \
	               return (uint32_t)size;                                   \
	        }                                                               \
	        return 2 * KALLOC_ARRAY_SIZE_MAX; /* will fail */               \
	}                                                                       \
                                                                                \
	__pure2                                                                 \
	static inline uint32_t                                                  \
	name ## _count(name ## _t array)                                        \
	{                                                                       \
	        return name ## _size_to_count(name ## _size(array));            \
	}                                                                       \
                                                                                \
	__pure2                                                                 \
	static inline h_type_t *__header_bidi_indexable                         \
	name ## _base(name ## _t array)                                         \
	{                                                                       \
	        vm_address_t base = __kalloc_array_base((vm_address_t)array);   \
	        uint32_t     size = __kalloc_array_size((vm_address_t)array);   \
                                                                                \
	        (void)size;                                                     \
	        return __unsafe_forge_bidi_indexable(h_type_t *, base, size);   \
	}                                                                       \
                                                                                \
	__pure2                                                                 \
	static inline e_type_t *__header_bidi_indexable                         \
	name ## _begin(name ## _t array)                                        \
	{                                                                       \
	        vm_address_t base = __kalloc_array_base((vm_address_t)array);   \
	        uint32_t     size = __kalloc_array_size((vm_address_t)array);   \
                                                                                \
	        (void)size;                                                     \
	        return __unsafe_forge_bidi_indexable(e_type_t *, base, size);   \
	}                                                                       \
                                                                                \
	__pure2                                                                 \
	static inline e_type_t *                                                \
	name ## _next_elem(name ## _t array, e_type_t *e)                       \
	{                                                                       \
	        vm_address_t end = __kalloc_array_end((vm_address_t)array);     \
	        vm_address_t ptr = (vm_address_t)e + sizeof(e_type_t);          \
                                                                                \
	        if (ptr + sizeof(e_type_t) <= end) {                            \
	                return __unsafe_forge_single(e_type_t *, ptr);          \
	        }                                                               \
	        return NULL;                                                    \
	}                                                                       \
                                                                                \
	__pure2                                                                 \
	static inline bool                                                      \
	name ## _contains(name ## _t array, vm_size_t i)                        \
	{                                                                       \
	        vm_size_t offs = (e_sz) + (h_sz);                               \
	        vm_size_t s;                                                    \
                                                                                \
	        if (__improbable(os_mul_and_add_overflow(i, e_sz, offs, &s))) { \
	                return false;                                           \
	        }                                                               \
	        if (__improbable(s > name ## _size(array))) {                   \
	                return false;                                           \
	        }                                                               \
	        return true;                                                    \
	}                                                                       \
                                                                                \
	__pure2                                                                 \
	static inline e_type_t * __single                                       \
	name ## _get_nocheck(name ## _t array, vm_size_t i)                     \
	{                                                                       \
	        return name ## _begin(array) + i;                               \
	}                                                                       \
                                                                                \
	__pure2                                                                 \
	static inline e_type_t * __single                                       \
	name ## _get(name ## _t array, vm_size_t i)                             \
	{                                                                       \
	        if (__probable(name ## _contains(array, i))) {                  \
	            return name ## _get_nocheck(array, i);                      \
	        }                                                               \
	        return NULL;                                                    \
	}                                                                       \
                                                                                \
	static inline name ## _t                                                \
	name ## _alloc_by_size(vm_size_t size, zalloc_flags_t fl)               \
	{                                                                       \
	        fl |= Z_KALLOC_ARRAY;                                           \
	        fl = __zone_flags_mix_tag(fl, VM_ALLOC_SITE_TAG());             \
	        return (name ## _t)kalloc_type_var_impl(name ## _kt_view,       \
	                        size, fl, NULL);                                \
	}                                                                       \
                                                                                \
	static inline name ## _t                                                \
	name ## _alloc_by_count(uint32_t count, zalloc_flags_t fl)              \
	{                                                                       \
	        return name ## _alloc_by_size(kt_size(h_sz, e_sz, count), fl);  \
	}                                                                       \
                                                                                \
	static inline name ## _t                                                \
	name ## _realloc_by_size(                                               \
	        name ## _t              array,                                  \
	        vm_size_t               new_size,                               \
	        zalloc_flags_t          fl)                                     \
	{                                                                       \
	        vm_address_t base = __kalloc_array_base((vm_address_t)array);   \
	        vm_size_t    size = __kalloc_array_size((vm_address_t)array);   \
                                                                                \
	        fl |= Z_KALLOC_ARRAY;                                           \
	        fl = __zone_flags_mix_tag(fl, VM_ALLOC_SITE_TAG());             \
	        return (name ## _t)(krealloc_ext)(                              \
	                        kt_mangle_var_view(name ## _kt_view),           \
	                        (void *)base, size, new_size, fl, NULL).addr;   \
	}                                                                       \
                                                                                \
	static inline name ## _t                                                \
	name ## _realloc_by_count(                                              \
	        name ## _t              array,                                  \
	        uint32_t                new_count,                              \
	        zalloc_flags_t          fl)                                     \
	{                                                                       \
	        vm_size_t new_size = kt_size(h_sz, e_sz, new_count);            \
                                                                                \
	        return name ## _realloc_by_size(array, new_size, fl);           \
	}                                                                       \
                                                                                \
	static inline void                                                      \
	name ## _free_noclear(name ## _t array)                                 \
	{                                                                       \
	        kfree_type_var_impl(name ## _kt_view,                           \
	            name ## _base(array), name ## _size(array));                \
	}                                                                       \
                                                                                \
	static inline void                                                      \
	name ## _free(name ## _t *arrayp)                                       \
	{                                                                       \
	        name ## _t array = *arrayp;                                     \
                                                                                \
	        *arrayp = NULL;                                                 \
	        kfree_type_var_impl(name ## _kt_view,                           \
	            name ## _base(array), name ## _size(array));                \
	}


/*!
 * @macro KALLOC_ARRAY_TYPE_DEFINE()
 *
 * @description
 * Defines the data structures required to pair with a KALLOC_ARRAY_TYPE_DECL()
 * kalloc array declaration.
 *
 * @discussion
 * This macro comes in two variants
 *
 * - KALLOC_ARRAY_TYPE_DEFINE(name, e_ty, flags)
 * - KALLOC_ARRAY_TYPE_DEFINE(name, h_ty, e_ty, flags)
 *
 * Those must pair with the KALLOC_ARRAY_TYPE_DECL() form being used.
 * The flags must be valid @c kalloc_type_flags_t flags.
 */
#define KALLOC_ARRAY_TYPE_DEFINE(...) \
	KALLOC_DISPATCH(KALLOC_ARRAY_TYPE_DEFINE, ##__VA_ARGS__)

/*!
 * @function kalloc_next_good_size()
 *
 * @brief
 * Allows to implement "allocation growth policies" that work well
 * with the allocator.
 *
 * @discussion
 * Note that if the caller tracks a number of elements for an array,
 * where the elements are of size S, and the current count is C,
 * then it is possible for kalloc_next_good_size(C * S, ..) to hit
 * a fixed point, clients must call with a size at least of ((C + 1) * S).
 *
 * @param size         the current "size" of the allocation (in bytes).
 * @param period       the "period" (power of 2) for the allocation growth
 *                     policy once hitting the VM sized allocations.
 */
extern vm_size_t kalloc_next_good_size(
	vm_size_t               size,
	uint32_t                period);

#pragma mark kalloc_array implementation details

#define KALLOC_ARRAY_TYPE_DECL_2(name, e_type_t) \
	KALLOC_ARRAY_TYPE_DECL_(name, e_type_t, 0, e_type_t, sizeof(e_type_t))

#define KALLOC_ARRAY_TYPE_DECL_3(name, h_type_t, e_type_t) \
	KALLOC_ARRAY_TYPE_DECL_(name,                                           \
	    h_type_t, kt_realign_sizeof(h_type_t, e_type_t),                    \
	    e_type_t, sizeof(e_type_t))                                         \

#define KALLOC_ARRAY_TYPE_DEFINE_3(name, e_type_t, flags) \
	KALLOC_TYPE_VAR_DEFINE_3(name ## _kt_view, e_type_t, flags)

#define KALLOC_ARRAY_TYPE_DEFINE_4(name, h_type_t, e_type_t, flags) \
	KALLOC_TYPE_VAR_DEFINE_4(name ## _kt_view, h_type_t, e_type_t, flags)

extern struct kalloc_result __kalloc_array_decode(
	vm_address_t            array) __pure2;

__pure2
static inline uint32_t
__kalloc_array_size(vm_address_t array)
{
	vm_address_t size = __kalloc_array_decode(array).size;

	__builtin_assume(size <= KALLOC_ARRAY_SIZE_MAX);
	return (uint32_t)size;
}

__pure2
static inline vm_address_t
__kalloc_array_base(vm_address_t array)
{
	return (vm_address_t)__kalloc_array_decode(array).addr;
}

__pure2
static inline vm_address_t
__kalloc_array_begin(vm_address_t array, vm_size_t hdr_size)
{
	return (vm_address_t)__kalloc_array_decode(array).addr + hdr_size;
}

__pure2
static inline vm_address_t
__kalloc_array_end(vm_address_t array)
{
	struct kalloc_result kr = __kalloc_array_decode(array);

	return (vm_address_t)kr.addr + kr.size;
}

#else /* !XNU_KERNEL_PRIVATE */

#define KALLOC_ARRAY_TYPE_DECL_(name, h_type_t, h_sz, e_type_t, e_sz) \
	typedef struct name * __unsafe_indexable name ## _t

#endif /* !XNU_KERNEL_PRIVATE */
#pragma mark implementation details


static inline void *__unsafe_indexable
kt_mangle_var_view(kalloc_type_var_view_t kt_view)
{
	return (void *__unsafe_indexable)((uintptr_t)kt_view | 1ul);
}

static inline kalloc_type_var_view_t __unsafe_indexable
kt_demangle_var_view(void *ptr)
{
	return (kalloc_type_var_view_t __unsafe_indexable)((uintptr_t)ptr & ~1ul);
}

#define kt_is_var_view(ptr)  ((uintptr_t)(ptr) & 1)

#define kt_realign_sizeof(h_ty, e_ty) \
	((sizeof(h_ty) + _Alignof(e_ty) - 1) & -_Alignof(e_ty))

static inline vm_size_t
kt_size(vm_size_t s1, vm_size_t s2, vm_size_t c2)
{
	/* kalloc_large() will reject this size before even asking the VM  */
	const vm_size_t limit = 1ull << (8 * sizeof(vm_size_t) - 1);

	if (os_mul_and_add_overflow(s2, c2, s1, &s1) || (s1 & limit)) {
		return limit;
	}
	return s1;
}

#define kalloc_type_2(type, flags) ({                                          \
	static _KALLOC_TYPE_DEFINE(kt_view_var, type, KT_SHARED_ACCT);         \
	__unsafe_forge_single(type *, kalloc_type_impl(kt_view_var, flags));   \
})

#define kfree_type_2(type, elem) ({                                            \
	KALLOC_TYPE_ASSERT_COMPATIBLE_POINTER(elem, type);                     \
	static _KALLOC_TYPE_DEFINE(kt_view_var, type, KT_SHARED_ACCT);         \
	kfree_type_impl(kt_view_var, os_ptr_load_and_erase(elem));             \
})

#define kfree_type_3(type, count, elem) ({                                     \
	KALLOC_TYPE_ASSERT_COMPATIBLE_POINTER(elem, type);                     \
	static KALLOC_TYPE_VAR_DEFINE_3(kt_view_var, type, KT_SHARED_ACCT);    \
	__auto_type __kfree_count = (count);                                   \
	kfree_type_var_impl(kt_view_var, os_ptr_load_and_erase(elem),          \
	    kt_size(0, sizeof(type), __kfree_count));                          \
})

// rdar://123257599
#define kfree_type_counted_by_3(type, count_var, elem_var) ({                  \
	void *__header_bidi_indexable __elem_copy = (elem_var);                \
	__auto_type __kfree_count = (count_var);                               \
	(elem_var) = 0;                                                        \
	(count_var) = 0;                                                       \
	KALLOC_TYPE_ASSERT_COMPATIBLE_POINTER(                                 \
	    (os_get_pointee_type(elem_var) *)NULL, type);                      \
	static KALLOC_TYPE_VAR_DEFINE_3(kt_view_var, type, KT_SHARED_ACCT);    \
	kfree_type_var_impl(kt_view_var, __elem_copy,                          \
	    kt_size(0, sizeof(type), __kfree_count));                          \
})

#define kfree_type_4(hdr_ty, e_ty, count, elem) ({                             \
	KALLOC_TYPE_ASSERT_COMPATIBLE_POINTER(elem, hdr_ty);                   \
	static KALLOC_TYPE_VAR_DEFINE_4(kt_view_var, hdr_ty, e_ty,             \
	    KT_SHARED_ACCT);                                                   \
	__auto_type __kfree_count = (count);                                   \
	kfree_type_var_impl(kt_view_var,                                       \
	    os_ptr_load_and_erase(elem),                                       \
	    kt_size(kt_realign_sizeof(hdr_ty, e_ty), sizeof(e_ty),             \
	    __kfree_count));                                                   \
})

#ifdef XNU_KERNEL_PRIVATE
#define kalloc_type_tag_3(type, flags, tag) ({                                 \
	static _KALLOC_TYPE_DEFINE(kt_view_var, type, KT_SHARED_ACCT);         \
	__unsafe_forge_single(type *, kalloc_type_impl(kt_view_var,            \
	    Z_VM_TAG(flags, tag)));                                            \
})

#define kalloc_type_tag_4(type, count, flags, tag) ({                          \
	static KALLOC_TYPE_VAR_DEFINE_3(kt_view_var, type, KT_SHARED_ACCT);    \
	(type *)kalloc_type_var_impl(kt_view_var,                              \
	    kt_size(0, sizeof(type), count),                                   \
	    __zone_flags_mix_tag(flags, tag), NULL);                           \
})
#define kalloc_type_3(type, count, flags)  \
	kalloc_type_tag_4(type, count, flags, VM_ALLOC_SITE_TAG())

#define kalloc_type_tag_5(hdr_ty, e_ty, count, flags, tag) ({                  \
	static KALLOC_TYPE_VAR_DEFINE_4(kt_view_var, hdr_ty, e_ty,             \
	    KT_SHARED_ACCT);                                                   \
	(hdr_ty *)kalloc_type_var_impl(kt_view_var,                            \
	    kt_size(kt_realign_sizeof(hdr_ty, e_ty), sizeof(e_ty), count),     \
	    __zone_flags_mix_tag(flags, tag), NULL);                           \
})
#define kalloc_type_4(hdr_ty, e_ty, count, flags) \
	kalloc_type_tag_5(hdr_ty, e_ty, count, flags, VM_ALLOC_SITE_TAG())

#define krealloc_type_tag_6(type, old_count, new_count, elem, flags, tag) ({   \
	static KALLOC_TYPE_VAR_DEFINE_3(kt_view_var, type, KT_SHARED_ACCT);    \
	KALLOC_TYPE_ASSERT_COMPATIBLE_POINTER(elem, type);                     \
	(type *)__krealloc_type(kt_view_var, elem,                             \
	    kt_size(0, sizeof(type), old_count),                               \
	    kt_size(0, sizeof(type), new_count),                               \
	    __zone_flags_mix_tag(flags, tag), NULL);                           \
})
#define krealloc_type_5(type, old_count, new_count, elem, flags) \
	krealloc_type_tag_6(type, old_count, new_count, elem, flags, \
	    VM_ALLOC_SITE_TAG())

#define krealloc_type_tag_7(hdr_ty, e_ty, old_count, new_count, elem,          \
	    flags, tag) ({                                                     \
	static KALLOC_TYPE_VAR_DEFINE_4(kt_view_var, hdr_ty, e_ty,             \
	    KT_SHARED_ACCT);                                                   \
	KALLOC_TYPE_ASSERT_COMPATIBLE_POINTER(elem, hdr_ty);                   \
	(hdr_ty *)__krealloc_type(kt_view_var, elem,                           \
	    kt_size(kt_realign_sizeof(hdr_ty, e_ty), sizeof(e_ty), old_count), \
	    kt_size(kt_realign_sizeof(hdr_ty, e_ty), sizeof(e_ty), new_count), \
	    __zone_flags_mix_tag(flags, tag), NULL);                           \
})
#define krealloc_type_6(hdr_ty, e_ty, old_count, new_count, elem, flags) \
	krealloc_type_tag_7(hdr_ty, e_ty, old_count, new_count, elem, flags,   \
	    VM_ALLOC_SITE_TAG())

#else /* XNU_KERNEL_PRIVATE */

#define kalloc_type_3(type, count, flags) ({                                   \
	static KALLOC_TYPE_VAR_DEFINE_3(kt_view_var, type, KT_SHARED_ACCT);    \
	(type *)kalloc_type_var_impl(kt_view_var,                              \
	    kt_size(0, sizeof(type), count), flags, NULL);                     \
})

#define kalloc_type_4(hdr_ty, e_ty, count, flags) ({                           \
	static KALLOC_TYPE_VAR_DEFINE_4(kt_view_var, hdr_ty, e_ty,             \
	    KT_SHARED_ACCT);                                                   \
	(hdr_ty *)kalloc_type_var_impl(kt_view_var,                            \
	    kt_size(kt_realign_sizeof(hdr_ty, e_ty), sizeof(e_ty), count),     \
	    flags, NULL);                                                      \
})

#endif /* !XNU_KERNEL_PRIVATE */

/*
 * All k*free macros set "elem" to NULL on free.
 *
 * Note: all values passed to k*free() might be in the element to be freed,
 *       temporaries must be taken, and the resetting to be done prior to free.
 */
#ifdef XNU_KERNEL_PRIVATE

#define kheap_free(heap, elem, size) ({                                        \
	kalloc_heap_t __kfree_heap = (heap);                                   \
	__auto_type __kfree_size = (size);                                     \
	__builtin_assume(!kt_is_var_view(__kfree_heap));                       \
	kfree_ext((void *)__kfree_heap,                                        \
	    (void *)os_ptr_load_and_erase(elem), __kfree_size);                \
})

#define kheap_free_addr(heap, elem) ({                                         \
	kalloc_heap_t __kfree_heap = (heap);                                   \
	kfree_addr_ext(__kfree_heap, (void *)os_ptr_load_and_erase(elem));     \
})

#define kheap_free_bounded(heap, elem, min_sz, max_sz) ({                      \
	static_assert(max_sz <= KALLOC_SAFE_ALLOC_SIZE);                       \
	kalloc_heap_t __kfree_heap = (heap);                                   \
	__auto_type __kfree_min_sz = (min_sz);                                 \
	__auto_type __kfree_max_sz = (max_sz);                                 \
	(kheap_free_bounded)(__kfree_heap,                                     \
	    (void *)os_ptr_load_and_erase(elem),                               \
	    __kfree_min_sz, __kfree_max_sz);                                   \
})

#else /* XNU_KERNEL_PRIVATE */

#define kfree_data(elem, size) ({                                              \
	__auto_type __kfree_size = (size);                                     \
	(kfree_data)((void *)os_ptr_load_and_erase(elem), __kfree_size);       \
})

#define kfree_data_addr(elem) \
	(kfree_data_addr)((void *)os_ptr_load_and_erase(elem))

#define kfree_shared_data(elem, size) ({                                    \
	__auto_type __kfree_size = (size);                                      \
	(kfree_shared_data)((void *)os_ptr_load_and_erase(elem), __kfree_size); \
})

#define kfree_shared_data_addr(elem) \
	(kfree_shared_data_addr)((void *)os_ptr_load_and_erase(elem))

#endif /* !XNU_KERNEL_PRIVATE */

#define __kfree_data_elem_count_size(elem_var, count_var, size) ({              \
	void *__header_bidi_indexable __elem_copy = (elem_var);                 \
	(elem_var) = 0;                                                         \
	(count_var) = 0;                                                        \
	kfree_data(__elem_copy, size);                                          \
})

#define __kfree_data_addr_count_size(addr_var, count_var) ({                    \
	void *__header_bidi_indexable __addr_copy = (addr_var);                 \
	(addr_var) = 0;                                                         \
	(count_var) = 0;                                                        \
	kfree_data_addr(__addr_copy);                                           \
})

/*
 * kfree_data_sized_by is the kfree_data equivalent that is compatible with
 * -fbounds-safety's __sized_by pointers. Consistently with the -fbounds-safety
 * semantics, `size` must be the byte size of the allocation that is freed (for
 * instance, 20 for an array of 5 uint32_t).
 */
#define kfree_data_sized_by(elem, size) ({                                      \
	__auto_type __size = (size);                                            \
	__kfree_data_elem_count_size(elem, size, __size);                       \
})

#define kfree_data_addr_sized_by(addr, size) ({                                 \
	__kfree_data_addr_count_size(addr, size);                               \
})

/*
 * kfree_data_counted_by is the kfree_data equivalent that is compatible with
 * -fbounds-safety's __counted_by pointers. Consistently with the
 * -fbounds-safety semantics, `count` must be the object count of the allocation
 * that is freed (for instance, 5 for an array of 5 uint32_t).
 */
#define kfree_data_counted_by(elem, count) ({                                  \
	__auto_type __size = (count) * sizeof(*(elem));                        \
	__kfree_data_elem_count_size(elem, count, __size);                     \
})

#if __has_feature(address_sanitizer)
# define __kalloc_no_kasan __attribute__((no_sanitize("address")))
#else
# define __kalloc_no_kasan
#endif

#define KALLOC_CONCAT(x, y) __CONCAT(x,y)

#define KALLOC_COUNT_ARGS1(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, N, ...) N
#define KALLOC_COUNT_ARGS(...) \
	KALLOC_COUNT_ARGS1(, ##__VA_ARGS__, _9, _8, _7, _6, _5, _4, _3, _2, _1, _0)
#define KALLOC_DISPATCH1(base, N, ...) __CONCAT(base, N)(__VA_ARGS__)
#define KALLOC_DISPATCH(base, ...) \
	KALLOC_DISPATCH1(base, KALLOC_COUNT_ARGS(__VA_ARGS__), ##__VA_ARGS__)
#define KALLOC_DISPATCH1_R(base, N, ...) __CONCAT(base, N)(__VA_ARGS__)
#define KALLOC_DISPATCH_R(base, ...) \
	KALLOC_DISPATCH1_R(base, KALLOC_COUNT_ARGS(__VA_ARGS__), ##__VA_ARGS__)

#define kt_view_var \
	KALLOC_CONCAT(kalloc_type_view_, __LINE__)

#ifndef __BUILDING_XNU_LIBRARY__
#define KALLOC_TYPE_SEGMENT "__DATA_CONST"
#else /* __BUILDING_XNU_LIBRARY__ */
/* Special segments are not used when building for user-mode */
#define KALLOC_TYPE_SEGMENT "__DATA"
#endif /* __BUILDING_XNU_LIBRARY__ */

/*
 * When kalloc_type_impl is called from xnu, it calls zalloc_flags
 * directly and doesn't redirect zone-less sites to kheap_alloc.
 * Passing a size larger than KHEAP_MAX_SIZE for these allocations will
 * lead to a panic as the zone is null. Therefore assert that size
 * is less than KALLOC_SAFE_ALLOC_SIZE.
 */
#if XNU_KERNEL_PRIVATE || defined(KALLOC_TYPE_STRICT_SIZE_CHECK)
#define KALLOC_TYPE_SIZE_CHECK(size)                           \
	_Static_assert(size <= KALLOC_SAFE_ALLOC_SIZE,             \
	"type is too large");
#else
#define KALLOC_TYPE_SIZE_CHECK(size)
#endif

#define KALLOC_TYPE_CHECK_2(check, type) \
	(KALLOC_TYPE_SIG_CHECK(check, type))

#define KALLOC_TYPE_CHECK_3(check, type1, type2) \
	(KALLOC_TYPE_SIG_CHECK(check, type1) && \
	    KALLOC_TYPE_SIG_CHECK(check, type2))

#define KALLOC_TYPE_CHECK(...) \
	KALLOC_DISPATCH_R(KALLOC_TYPE_CHECK, ##__VA_ARGS__)

#define KALLOC_TYPE_VM_SIZE_CHECK_1(type) \
	(sizeof(type) > KHEAP_MAX_SIZE)

#define KALLOC_TYPE_VM_SIZE_CHECK_2(type1, type2) \
	(sizeof(type1) + sizeof(type2) > KHEAP_MAX_SIZE)

#define KALLOC_TYPE_VM_SIZE_CHECK(...) \
	KALLOC_DISPATCH_R(KALLOC_TYPE_VM_SIZE_CHECK, ##__VA_ARGS__)

#define KALLOC_TYPE_TRAILING_DATA_CHECK(hdr_ty, elem_ty)     \
	_Static_assert((KALLOC_TYPE_IS_DATA_ONLY(hdr_ty) ||  \
	    !KALLOC_TYPE_IS_DATA_ONLY(elem_ty)),             \
	"cannot allocate data-only array of " #elem_ty       \
	" contiguously to " #hdr_ty)

#ifdef __cplusplus
#define KALLOC_TYPE_CAST_FLAGS(flags) static_cast<kalloc_type_flags_t>(flags)
#else
#define KALLOC_TYPE_CAST_FLAGS(flags) (kalloc_type_flags_t)(flags)
#endif

/*
 * Don't emit signature if type is "data-only" or is large enough that it
 * uses the VM.
 *
 * Note: sig_type is the type you want to emit signature for. The variable
 * args can be used to provide other types in the allocation, to make the
 * decision of whether to emit the signature.
 */
#define KALLOC_TYPE_EMIT_SIG(sig_type, ...)                              \
	(KALLOC_TYPE_CHECK(KT_SUMMARY_MASK_DATA, sig_type, ##__VA_ARGS__) || \
	KALLOC_TYPE_VM_SIZE_CHECK(sig_type, ##__VA_ARGS__))?                 \
	"" : __builtin_xnu_type_signature(sig_type)

/*
 * Kalloc type flags are adjusted to indicate if the type is "data-only" or
 * will use the VM or is a pointer array.
 *
 * There is no need to create another sig for data shared. We expect shareable
 * allocations to be marked explicitly by the callers, by using the dedicated
 * allocation API for shared data.
 */
#define KALLOC_TYPE_ADJUST_FLAGS(flags, ...)                                 \
	KALLOC_TYPE_CAST_FLAGS((flags | KT_CHANGED | KT_CHANGED2 |               \
	(KALLOC_TYPE_CHECK(KT_SUMMARY_MASK_DATA, __VA_ARGS__)? KT_DATA_ONLY: 0) |\
	(KALLOC_TYPE_CHECK(KT_SUMMARY_MASK_PTR, __VA_ARGS__)? KT_PTR_ARRAY: 0) | \
	(KALLOC_TYPE_VM_SIZE_CHECK(__VA_ARGS__)? KT_VM : 0)))

#define _KALLOC_TYPE_DEFINE(var, type, flags)                       \
	__kalloc_no_kasan                                               \
	__PLACE_IN_SECTION(KALLOC_TYPE_SEGMENT ", __kalloc_type, "      \
	    "regular, live_support")                                    \
	struct kalloc_type_view var[1] = { {                            \
	    .kt_zv.zv_name = "site." #type,                             \
	    .kt_flags = KALLOC_TYPE_ADJUST_FLAGS(flags, type),          \
	    .kt_size = sizeof(type),                                    \
	    .kt_signature = KALLOC_TYPE_EMIT_SIG(type),                 \
	} };                                                            \
	KALLOC_TYPE_SIZE_CHECK(sizeof(type));

#define KALLOC_TYPE_VAR_DEFINE_3(var, type, flags)                  \
	__kalloc_no_kasan                                               \
	__PLACE_IN_SECTION(KALLOC_TYPE_SEGMENT ", __kalloc_var, "       \
	    "regular, live_support")                                    \
	struct kalloc_type_var_view var[1] = { {                        \
	    .kt_version = KT_V1,                                        \
	    .kt_name = "site." #type,                                   \
	    .kt_flags = KALLOC_TYPE_ADJUST_FLAGS(flags, type),          \
	    .kt_size_type = sizeof(type),                               \
	    .kt_sig_type = KALLOC_TYPE_EMIT_SIG(type),                  \
	} };                                                            \
	KALLOC_TYPE_SIZE_CHECK(sizeof(type));

#define KALLOC_TYPE_VAR_DEFINE_4(var, hdr, type, flags)             \
	__kalloc_no_kasan                                               \
	__PLACE_IN_SECTION(KALLOC_TYPE_SEGMENT ", __kalloc_var, "       \
	    "regular, live_support")                                    \
	struct kalloc_type_var_view var[1] = { {                        \
	    .kt_version = KT_V1,                                        \
	    .kt_name = "site." #hdr "." #type,                          \
	    .kt_flags = KALLOC_TYPE_ADJUST_FLAGS(flags, hdr, type),     \
	    .kt_size_hdr = sizeof(hdr),                                 \
	    .kt_size_type = sizeof(type),                               \
	    .kt_sig_hdr = KALLOC_TYPE_EMIT_SIG(hdr, type),              \
	    .kt_sig_type = KALLOC_TYPE_EMIT_SIG(type, hdr),             \
	} };                                                            \
	KALLOC_TYPE_SIZE_CHECK(sizeof(hdr));                            \
	KALLOC_TYPE_SIZE_CHECK(sizeof(type));                           \
	KALLOC_TYPE_TRAILING_DATA_CHECK(hdr, type);

#ifndef XNU_KERNEL_PRIVATE
/*
 * This macro is currently used by AppleImage4
 */
#define KALLOC_TYPE_DEFINE_SITE(var, type, flags)       \
	static _KALLOC_TYPE_DEFINE(var, type, flags)

#endif /* !XNU_KERNEL_PRIVATE */

#ifdef XNU_KERNEL_PRIVATE

extern struct kalloc_result kalloc_ext(
	void                   *kheap_or_kt_view __unsafe_indexable,
	vm_size_t               size,
	zalloc_flags_t          flags,
	void                   *site);

static inline struct kalloc_result
__kalloc_ext(
	void                   *kheap_or_kt_view __unsafe_indexable,
	vm_size_t               size,
	zalloc_flags_t          flags,
	void                   *site)
{
	struct kalloc_result kr;

	kr    = (kalloc_ext)(kheap_or_kt_view, size, flags, site);
	if (flags & Z_NOFAIL) {
		__builtin_assume(kr.addr != NULL);
	}
	return kr;
}

#define kalloc_ext(hov, size, fl, site) __kalloc_ext(hov, size, fl, site)

extern void kfree_ext(
	void                   *kheap_or_kt_view __unsafe_indexable,
	void                   *addr __unsafe_indexable,
	vm_size_t               size);

// rdar://87559422
static inline void *__unsafe_indexable
kalloc_type_var_impl(
	kalloc_type_var_view_t    kt_view,
	vm_size_t                 size,
	zalloc_flags_t            flags,
	void                      *site)
{
	struct kalloc_result kr;

	kr = kalloc_ext(kt_mangle_var_view(kt_view), size, flags, site);
	return kr.addr;
}

static inline void
kfree_type_var_impl(
	kalloc_type_var_view_t      kt_view,
	void                       *ptr __unsafe_indexable,
	vm_size_t                   size)
{
	kfree_ext(kt_mangle_var_view(kt_view), ptr, size);
}

#else /* XNU_KERNEL_PRIVATE */

extern void *__unsafe_indexable kalloc_type_var_impl(
	kalloc_type_var_view_t  kt_view,
	vm_size_t               size,
	zalloc_flags_t          flags,
	void                   *site);

extern void kfree_type_var_impl(
	kalloc_type_var_view_t  kt_view,
	void                   *ptr __unsafe_indexable,
	vm_size_t               size);

#endif /* !XNU_KERNEL_PRIVATE */

__attribute__((malloc, alloc_size(2)))
static inline void *
__sized_by(size)
__kalloc_type_var_impl(
	kalloc_type_var_view_t  kt_view,
	vm_size_t               size,
	zalloc_flags_t          flags,
	void                   *site)
{
	void *__unsafe_indexable addr;

	addr = (kalloc_type_var_impl)(kt_view, size, flags, site);
	if (flags & Z_NOFAIL) {
		__builtin_assume(addr != NULL);
	}
	return __unsafe_forge_bidi_indexable(void *, addr, size);
}

#define kalloc_type_var_impl(ktv, size, fl, site) \
	__kalloc_type_var_impl(ktv, size, fl, site)

extern void *kalloc_type_impl_external(
	kalloc_type_view_t  kt_view,
	zalloc_flags_t      flags);

extern void kfree_type_impl_external(
	kalloc_type_view_t  kt_view,
	void               *ptr __unsafe_indexable);

extern void *OSObject_typed_operator_new(
	kalloc_type_view_t  ktv,
	vm_size_t           size);

extern void OSObject_typed_operator_delete(
	kalloc_type_view_t  ktv,
	void               *mem __unsafe_indexable,
	vm_size_t           size);

#ifdef XNU_KERNEL_PRIVATE
#pragma GCC visibility push(hidden)

#define KALLOC_TYPE_SIZE_MASK  0xffffff
#define KALLOC_TYPE_IDX_SHIFT  24
#define KALLOC_TYPE_IDX_MASK   0xff

static inline uint32_t
kalloc_type_get_size(uint32_t kt_size)
{
	return kt_size & KALLOC_TYPE_SIZE_MASK;
}

static inline uint32_t
kalloc_type_size(kalloc_type_view_t ktv)
{
	return kalloc_type_get_size(ktv->kt_size);
}

extern bool IOMallocType_from_vm(
	kalloc_type_view_t ktv);

/* Used by kern_os_* and operator new */
KALLOC_HEAP_DECLARE(KERN_OS_MALLOC);

extern void kheap_startup_init(kalloc_heap_t heap);
extern void kheap_var_startup_init(kalloc_heap_t heap);

__attribute__((malloc, alloc_size(2)))
static inline void *
__sized_by(size)
__kheap_alloc(
	kalloc_heap_t           kheap,
	vm_size_t               size,
	zalloc_flags_t          flags,
	void                   *site)
{
	struct kalloc_result kr;
	__builtin_assume(!kt_is_var_view(kheap));
	kr = kalloc_ext(kheap, size, flags, site);
	return __unsafe_forge_bidi_indexable(void *, kr.addr, size);
}

extern struct kalloc_result krealloc_ext(
	void                   *kheap_or_kt_view __unsafe_indexable,
	void                   *addr __unsafe_indexable,
	vm_size_t               old_size,
	vm_size_t               new_size,
	zalloc_flags_t          flags,
	void                   *site);

static inline struct kalloc_result
__krealloc_ext(
	void                   *kheap_or_kt_view __unsafe_indexable,
	void                   *addr __sized_by(old_size),
	vm_size_t               old_size,
	vm_size_t               new_size,
	zalloc_flags_t          flags,
	void                   *site)
{
	struct kalloc_result kr = (krealloc_ext)(kheap_or_kt_view, addr, old_size,
	    new_size, flags, site);
	if (flags & Z_NOFAIL) {
		__builtin_assume(kr.addr != NULL);
	}
	return kr;
}

#define krealloc_ext(hov, addr, old_size, new_size, fl, site) \
	__krealloc_ext(hov, addr, old_size, new_size, fl, site)

__attribute__((malloc, alloc_size(4)))
static inline void *
__sized_by(new_size)
__kheap_realloc(
	kalloc_heap_t           kheap,
	void                   *addr __sized_by(old_size),
	vm_size_t               old_size,
	vm_size_t               new_size,
	zalloc_flags_t          flags,
	void                   *site)
{
	struct kalloc_result kr;
	__builtin_assume(!kt_is_var_view(kheap));
	kr = krealloc_ext(kheap, addr, old_size, new_size, flags, site);
	return __unsafe_forge_bidi_indexable(void *, kr.addr, new_size);
}

__attribute__((malloc, alloc_size(4)))
static inline void *
__sized_by(new_size)
__krealloc_type(
	kalloc_type_var_view_t  kt_view,
	void                   *addr __sized_by(old_size),
	vm_size_t               old_size,
	vm_size_t               new_size,
	zalloc_flags_t          flags,
	void                   *site)
{
	struct kalloc_result kr;
	kr = krealloc_ext(kt_mangle_var_view(kt_view), addr,
	    old_size, new_size, flags, site);
	return __unsafe_forge_bidi_indexable(void *, kr.addr, new_size);
}

extern void kfree_addr_ext(
	kalloc_heap_t           kheap,
	void                   *addr __unsafe_indexable);

extern zone_t kalloc_zone_for_size(
	zone_id_t             zid,
	vm_size_t             size);

extern vm_size_t kalloc_large_max;
SCALABLE_COUNTER_DECLARE(kalloc_large_count);
SCALABLE_COUNTER_DECLARE(kalloc_large_total);

extern void kern_os_typed_free(
	kalloc_type_view_t    ktv,
	void                 *addr __unsafe_indexable,
	vm_size_t             esize);

#pragma GCC visibility pop
#endif  /* !XNU_KERNEL_PRIVATE */

extern void kern_os_zfree(
	zone_t        zone,
	void         *addr __unsafe_indexable,
	vm_size_t     size);

__ASSUME_PTR_ABI_SINGLE_END __END_DECLS

#endif  /* _KERN_KALLOC_H_ */

#endif  /* KERNEL_PRIVATE */