DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (409f3966645a)

VCS Links

Line Code
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 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "ProcessRedirect.h"

#include "mozilla/Maybe.h"

#include "HashTable.h"
#include "Lock.h"
#include "MemorySnapshot.h"
#include "ProcessRecordReplay.h"
#include "ProcessRewind.h"

#include <dlfcn.h>
#include <fcntl.h>
#include <signal.h>

#include <bsm/audit.h>
#include <bsm/audit_session.h>
#include <mach/clock.h>
#include <mach/mach.h>
#include <mach/mach_time.h>
#include <mach/mach_vm.h>
#include <mach/vm_map.h>
#include <sys/attr.h>
#include <sys/event.h>
#include <sys/mman.h>
#include <sys/mount.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <time.h>

#include <Carbon/Carbon.h>
#include <SystemConfiguration/SystemConfiguration.h>
#include <objc/objc-runtime.h>

namespace mozilla {
namespace recordreplay {

#define FOR_EACH_REDIRECTION(MACRO)             \
  /* System call wrappers */                    \
  MACRO(kevent)                                 \
  MACRO(kevent64)                               \
  MACRO(madvise)                                \
  MACRO(mprotect)                               \
  MACRO(mmap)                                   \
  MACRO(munmap)                                 \
  MACRO(read)                                   \
  MACRO(__read_nocancel)                        \
  MACRO(pread)                                  \
  MACRO(write)                                  \
  MACRO(__write_nocancel)                       \
  MACRO(open)                                   \
  MACRO(__open_nocancel)                        \
  MACRO(recv)                                   \
  MACRO(recvmsg)                                \
  MACRO(sendmsg)                                \
  MACRO(shm_open)                               \
  MACRO(socket)                                 \
  MACRO(kqueue)                                 \
  MACRO(pipe)                                   \
  MACRO(close)                                  \
  MACRO(__close_nocancel)                       \
  MACRO(mkdir)                                  \
  MACRO(dup)                                    \
  MACRO(access)                                 \
  MACRO(lseek)                                  \
  MACRO(socketpair)                             \
  MACRO(fileport_makeport)                      \
  MACRO(getsockopt)                             \
  MACRO(gettimeofday)                           \
  MACRO(getuid)                                 \
  MACRO(geteuid)                                \
  MACRO(getgid)                                 \
  MACRO(getegid)                                \
  MACRO(issetugid)                              \
  MACRO(__gettid)                               \
  MACRO(getpid)                                 \
  MACRO(fcntl)                                  \
  MACRO(fstat$INODE64)                          \
  MACRO(lstat$INODE64)                          \
  MACRO(stat$INODE64)                           \
  MACRO(getattrlist)                            \
  MACRO(statfs$INODE64)                         \
  MACRO(fstatfs$INODE64)                        \
  MACRO(readlink)                               \
  MACRO(__getdirentries64)                      \
  MACRO(getdirentriesattr)                      \
  MACRO(getrusage)                              \
  MACRO(__getrlimit)                            \
  MACRO(__setrlimit)                            \
  MACRO(sigprocmask)                            \
  MACRO(sigaltstack)                            \
  MACRO(sigaction)                              \
  MACRO(__pthread_sigmask)                      \
  MACRO(__fsgetpath)                            \
  MACRO(__disable_threadsignal)                 \
  MACRO(__sysctl)                               \
  MACRO(__mac_syscall)                          \
  MACRO(getaudit_addr)                          \
  MACRO(umask)                                  \
  MACRO(__select)                               \
  MACRO(__process_policy)                       \
  MACRO(__kdebug_trace)                         \
  MACRO(guarded_kqueue_np)                      \
  MACRO(csops)                                  \
  MACRO(__getlogin)                             \
  MACRO(__workq_kernreturn)                     \
  MACRO(start_wqthread)                         \
  /* pthreads interface functions */            \
  MACRO(pthread_cond_wait)                      \
  MACRO(pthread_cond_timedwait)                 \
  MACRO(pthread_cond_timedwait_relative_np)     \
  MACRO(pthread_create)                         \
  MACRO(pthread_join)                           \
  MACRO(pthread_mutex_init)                     \
  MACRO(pthread_mutex_destroy)                  \
  MACRO(pthread_mutex_lock)                     \
  MACRO(pthread_mutex_trylock)                  \
  MACRO(pthread_mutex_unlock)                   \
  /* C Library functions */                     \
  MACRO(dlclose)                                \
  MACRO(dlopen)                                 \
  MACRO(dlsym)                                  \
  MACRO(fclose)                                 \
  MACRO(fopen)                                  \
  MACRO(fread)                                  \
  MACRO(fseek)                                  \
  MACRO(ftell)                                  \
  MACRO(fwrite)                                 \
  MACRO(getenv)                                 \
  MACRO(localtime_r)                            \
  MACRO(gmtime_r)                               \
  MACRO(localtime)                              \
  MACRO(gmtime)                                 \
  MACRO(mktime)                                 \
  MACRO(setlocale)                              \
  MACRO(strftime)                               \
  MACRO(arc4random)                             \
  MACRO(mach_absolute_time)                     \
  MACRO(mach_msg)                               \
  MACRO(mach_timebase_info)                     \
  MACRO(mach_vm_allocate)                       \
  MACRO(mach_vm_deallocate)                     \
  MACRO(mach_vm_protect)                        \
  MACRO(sandbox_free_error)                     \
  MACRO(sandbox_init)                           \
  MACRO(sandbox_init_with_parameters)           \
  MACRO(task_threads)                           \
  MACRO(vm_copy)                                \
  MACRO(vm_purgable_control)                    \
  MACRO(tzset)                                  \
  /* NSPR functions */                          \
  MACRO(PL_NewHashTable)                        \
  MACRO(PL_HashTableDestroy)                    \
  /* Objective C functions */                   \
  MACRO(class_getClassMethod)                   \
  MACRO(class_getInstanceMethod)                \
  MACRO(method_exchangeImplementations)         \
  MACRO(objc_autoreleasePoolPop)                \
  MACRO(objc_autoreleasePoolPush)               \
  MACRO(objc_msgSend)                           \
  /* Cocoa functions */                         \
  MACRO(AcquireFirstMatchingEventInQueue)       \
  MACRO(CFArrayAppendValue)                     \
  MACRO(CFArrayCreate)                          \
  MACRO(CFArrayGetCount)                        \
  MACRO(CFArrayGetValueAtIndex)                 \
  MACRO(CFArrayRemoveValueAtIndex)              \
  MACRO(CFAttributedStringCreate)               \
  MACRO(CFBundleCopyExecutableURL)              \
  MACRO(CFBundleCopyInfoDictionaryForURL)       \
  MACRO(CFBundleCreate)                         \
  MACRO(CFBundleGetBundleWithIdentifier)        \
  MACRO(CFBundleGetDataPointerForName)          \
  MACRO(CFBundleGetFunctionPointerForName)      \
  MACRO(CFBundleGetIdentifier)                  \
  MACRO(CFBundleGetInfoDictionary)              \
  MACRO(CFBundleGetMainBundle)                  \
  MACRO(CFBundleGetValueForInfoDictionaryKey)   \
  MACRO(CFDataGetBytePtr)                       \
  MACRO(CFDataGetLength)                        \
  MACRO(CFDateFormatterCreate)                  \
  MACRO(CFDateFormatterGetFormat)               \
  MACRO(CFDictionaryAddValue)                   \
  MACRO(CFDictionaryCreate)                     \
  MACRO(CFDictionaryCreateMutable)              \
  MACRO(CFDictionaryCreateMutableCopy)          \
  MACRO(CFDictionaryGetValue)                   \
  MACRO(CFDictionaryGetValueIfPresent)          \
  MACRO(CFDictionaryReplaceValue)               \
  MACRO(CFEqual)                                \
  MACRO(CFGetTypeID)                            \
  MACRO(CFLocaleCopyCurrent)                    \
  MACRO(CFLocaleCopyPreferredLanguages)         \
  MACRO(CFLocaleCreate)                         \
  MACRO(CFLocaleGetIdentifier)                  \
  MACRO(CFNotificationCenterAddObserver)        \
  MACRO(CFNotificationCenterGetLocalCenter)     \
  MACRO(CFNotificationCenterRemoveObserver)     \
  MACRO(CFNumberCreate)                         \
  MACRO(CFNumberGetValue)                       \
  MACRO(CFNumberIsFloatType)                    \
  MACRO(CFPreferencesAppValueIsForced)          \
  MACRO(CFPreferencesCopyAppValue)              \
  MACRO(CFPreferencesCopyValue)                 \
  MACRO(CFPropertyListCreateFromXMLData)        \
  MACRO(CFPropertyListCreateWithStream)         \
  MACRO(CFReadStreamClose)                      \
  MACRO(CFReadStreamCreateWithFile)             \
  MACRO(CFReadStreamOpen)                       \
  MACRO(CFRelease)                              \
  MACRO(CFRetain)                               \
  MACRO(CFRunLoopAddSource)                     \
  MACRO(CFRunLoopGetCurrent)                    \
  MACRO(CFRunLoopRemoveSource)                  \
  MACRO(CFRunLoopSourceCreate)                  \
  MACRO(CFRunLoopSourceSignal)                  \
  MACRO(CFRunLoopWakeUp)                        \
  MACRO(CFStringAppendCharacters)               \
  MACRO(CFStringCompare)                        \
  MACRO(CFStringCreateArrayBySeparatingStrings) \
  MACRO(CFStringCreateMutable)                  \
  MACRO(CFStringCreateWithBytes)                \
  MACRO(CFStringCreateWithBytesNoCopy)          \
  MACRO(CFStringCreateWithCharactersNoCopy)     \
  MACRO(CFStringCreateWithCString)              \
  MACRO(CFStringCreateWithFormat)               \
  MACRO(CFStringGetBytes)                       \
  MACRO(CFStringGetCharacters)                  \
  MACRO(CFStringGetCString)                     \
  MACRO(CFStringGetCStringPtr)                  \
  MACRO(CFStringGetIntValue)                    \
  MACRO(CFStringGetLength)                      \
  MACRO(CFStringGetMaximumSizeForEncoding)      \
  MACRO(CFStringHasPrefix)                      \
  MACRO(CFStringTokenizerAdvanceToNextToken)    \
  MACRO(CFStringTokenizerCreate)                \
  MACRO(CFStringTokenizerGetCurrentTokenRange)  \
  MACRO(CFURLCreateFromFileSystemRepresentation) \
  MACRO(CFURLCreateFromFSRef)                   \
  MACRO(CFURLCreateWithFileSystemPath)          \
  MACRO(CFURLCreateWithString)                  \
  MACRO(CFURLGetFileSystemRepresentation)       \
  MACRO(CFURLGetFSRef)                          \
  MACRO(CFUUIDCreate)                           \
  MACRO(CFUUIDCreateString)                     \
  MACRO(CFUUIDGetUUIDBytes)                     \
  MACRO(CGAffineTransformConcat)                \
  MACRO(CGBitmapContextCreateImage)             \
  MACRO(CGBitmapContextCreateWithData)          \
  MACRO(CGBitmapContextGetBytesPerRow)          \
  MACRO(CGBitmapContextGetHeight)               \
  MACRO(CGBitmapContextGetWidth)                \
  MACRO(CGColorRelease)                         \
  MACRO(CGColorSpaceCopyICCProfile)             \
  MACRO(CGFontCreateCopyWithVariations)         \
  MACRO(CGColorSpaceCreateDeviceRGB)            \
  MACRO(CGColorSpaceCreatePattern)              \
  MACRO(CGColorSpaceRelease)                    \
  MACRO(CGContextBeginTransparencyLayerWithRect) \
  MACRO(CGContextClipToRects)                   \
  MACRO(CGContextConcatCTM)                     \
  MACRO(CGContextDrawImage)                     \
  MACRO(CGContextEndTransparencyLayer)          \
  MACRO(CGContextFillRect)                      \
  MACRO(CGContextGetClipBoundingBox)            \
  MACRO(CGContextGetCTM)                        \
  MACRO(CGContextGetType)                       \
  MACRO(CGContextGetUserSpaceToDeviceSpaceTransform) \
  MACRO(CGContextRestoreGState)                 \
  MACRO(CGContextSaveGState)                    \
  MACRO(CGContextSetAllowsFontSubpixelPositioning) \
  MACRO(CGContextSetAllowsFontSubpixelQuantization) \
  MACRO(CGContextSetAlpha)                      \
  MACRO(CGContextSetBaseCTM)                    \
  MACRO(CGContextSetCTM)                        \
  MACRO(CGContextSetGrayFillColor)              \
  MACRO(CGContextSetRGBFillColor)               \
  MACRO(CGContextSetShouldAntialias)            \
  MACRO(CGContextSetShouldSmoothFonts)          \
  MACRO(CGContextSetShouldSubpixelPositionFonts) \
  MACRO(CGContextSetShouldSubpixelQuantizeFonts) \
  MACRO(CGContextSetTextDrawingMode)            \
  MACRO(CGContextSetTextMatrix)                 \
  MACRO(CGContextScaleCTM)                      \
  MACRO(CGContextTranslateCTM)                  \
  MACRO(CGDataProviderCreateWithData)           \
  MACRO(CGDisplayCopyColorSpace)                \
  MACRO(CGDisplayIOServicePort)                 \
  MACRO(CGEventSourceCounterForEventType)       \
  MACRO(CGFontCopyTableForTag)                  \
  MACRO(CGFontCopyTableTags)                    \
  MACRO(CGFontCopyVariations)                   \
  MACRO(CGFontCreateWithDataProvider)           \
  MACRO(CGFontCreateWithFontName)               \
  MACRO(CGFontCreateWithPlatformFont)           \
  MACRO(CGFontGetAscent)                        \
  MACRO(CGFontGetCapHeight)                     \
  MACRO(CGFontGetDescent)                       \
  MACRO(CGFontGetFontBBox)                      \
  MACRO(CGFontGetGlyphAdvances)                 \
  MACRO(CGFontGetGlyphBBoxes)                   \
  MACRO(CGFontGetGlyphPath)                     \
  MACRO(CGFontGetLeading)                       \
  MACRO(CGFontGetUnitsPerEm)                    \
  MACRO(CGFontGetXHeight)                       \
  MACRO(CGImageGetHeight)                       \
  MACRO(CGImageGetWidth)                        \
  MACRO(CGImageRelease)                         \
  MACRO(CGMainDisplayID)                        \
  MACRO(CGPathAddPath)                          \
  MACRO(CGPathApply)                            \
  MACRO(CGPathContainsPoint)                    \
  MACRO(CGPathCreateMutable)                    \
  MACRO(CGPathGetBoundingBox)                   \
  MACRO(CGPathGetCurrentPoint)                  \
  MACRO(CGPathIsEmpty)                          \
  MACRO(CGSSetDebugOptions)                     \
  MACRO(CGSShutdownServerConnections)           \
  MACRO(CTFontCopyFamilyName)                   \
  MACRO(CTFontCopyFontDescriptor)               \
  MACRO(CTFontCopyGraphicsFont)                 \
  MACRO(CTFontCopyTable)                        \
  MACRO(CTFontCopyVariationAxes)                \
  MACRO(CTFontCreateForString)                  \
  MACRO(CTFontCreatePathForGlyph)               \
  MACRO(CTFontCreateWithFontDescriptor)         \
  MACRO(CTFontCreateWithGraphicsFont)           \
  MACRO(CTFontCreateWithName)                   \
  MACRO(CTFontDescriptorCopyAttribute)          \
  MACRO(CTFontDescriptorCreateCopyWithAttributes) \
  MACRO(CTFontDescriptorCreateMatchingFontDescriptors) \
  MACRO(CTFontDescriptorCreateWithAttributes)   \
  MACRO(CTFontDrawGlyphs)                       \
  MACRO(CTFontGetAdvancesForGlyphs)             \
  MACRO(CTFontGetAscent)                        \
  MACRO(CTFontGetBoundingBox)                   \
  MACRO(CTFontGetBoundingRectsForGlyphs)        \
  MACRO(CTFontGetCapHeight)                     \
  MACRO(CTFontGetDescent)                       \
  MACRO(CTFontGetGlyphCount)                    \
  MACRO(CTFontGetGlyphsForCharacters)           \
  MACRO(CTFontGetLeading)                       \
  MACRO(CTFontGetSize)                          \
  MACRO(CTFontGetSymbolicTraits)                \
  MACRO(CTFontGetUnderlinePosition)             \
  MACRO(CTFontGetUnderlineThickness)            \
  MACRO(CTFontGetUnitsPerEm)                    \
  MACRO(CTFontGetXHeight)                       \
  MACRO(CTFontManagerCopyAvailableFontFamilyNames) \
  MACRO(CTFontManagerRegisterFontsForURLs)      \
  MACRO(CTFontManagerSetAutoActivationSetting)  \
  MACRO(CTLineCreateWithAttributedString)       \
  MACRO(CTLineGetGlyphRuns)                     \
  MACRO(CTRunGetAttributes)                     \
  MACRO(CTRunGetGlyphCount)                     \
  MACRO(CTRunGetGlyphsPtr)                      \
  MACRO(CTRunGetPositionsPtr)                   \
  MACRO(CTRunGetStringIndicesPtr)               \
  MACRO(CTRunGetStringRange)                    \
  MACRO(CTRunGetTypographicBounds)              \
  MACRO(CUIDraw)                                \
  MACRO(FSCompareFSRefs)                        \
  MACRO(FSGetVolumeInfo)                        \
  MACRO(FSFindFolder)                           \
  MACRO(Gestalt)                                \
  MACRO(GetEventClass)                          \
  MACRO(GetCurrentEventQueue)                   \
  MACRO(GetCurrentProcess)                      \
  MACRO(GetEventAttributes)                     \
  MACRO(GetEventDispatcherTarget)               \
  MACRO(GetEventKind)                           \
  MACRO(HIThemeDrawButton)                      \
  MACRO(HIThemeDrawFrame)                       \
  MACRO(HIThemeDrawGroupBox)                    \
  MACRO(HIThemeDrawGrowBox)                     \
  MACRO(HIThemeDrawMenuBackground)              \
  MACRO(HIThemeDrawMenuItem)                    \
  MACRO(HIThemeDrawMenuSeparator)               \
  MACRO(HIThemeDrawSeparator)                   \
  MACRO(HIThemeDrawTabPane)                     \
  MACRO(HIThemeDrawTrack)                       \
  MACRO(HIThemeGetGrowBoxBounds)                \
  MACRO(HIThemeSetFill)                         \
  MACRO(IORegistryEntrySearchCFProperty)        \
  MACRO(LSCopyAllHandlersForURLScheme)          \
  MACRO(LSCopyApplicationForMIMEType)           \
  MACRO(LSCopyItemAttribute)                    \
  MACRO(LSCopyKindStringForMIMEType)            \
  MACRO(LSGetApplicationForInfo)                \
  MACRO(LSGetApplicationForURL)                 \
  MACRO(NSClassFromString)                      \
  MACRO(NSRectFill)                             \
  MACRO(NSSearchPathForDirectoriesInDomains)    \
  MACRO(NSSetFocusRingStyle)                    \
  MACRO(NSTemporaryDirectory)                   \
  MACRO(OSSpinLockLock)                         \
  MACRO(ReleaseEvent)                           \
  MACRO(RemoveEventFromQueue)                   \
  MACRO(RetainEvent)                            \
  MACRO(SendEventToEventTarget)                 \
  MACRO(SLDisplayCopyColorSpace)                \
  MACRO(SLDisplayIOServicePort)                 \
  MACRO(SLEventSourceCounterForEventType)       \
  MACRO(SLMainDisplayID)                        \
  MACRO(SLSSetDenyWindowServerConnections)      \
  MACRO(SLSShutdownServerConnections)

#define MAKE_CALL_EVENT(aName)  CallEvent_ ##aName ,

enum CallEvent {                                \
  FOR_EACH_REDIRECTION(MAKE_CALL_EVENT)         \
  CallEvent_Count                               \
};

#undef MAKE_CALL_EVENT

///////////////////////////////////////////////////////////////////////////////
// Callbacks
///////////////////////////////////////////////////////////////////////////////

enum CallbackEvent {
  CallbackEvent_CFRunLoopPerformCallBack,
  CallbackEvent_CGPathApplierFunction
};

typedef void (*CFRunLoopPerformCallBack)(void*);

static void
CFRunLoopPerformCallBackWrapper(void* aInfo)
{
  RecordReplayCallback(CFRunLoopPerformCallBack, &aInfo);
  rrc.mFunction(aInfo);

  // Make sure we service any callbacks that have been posted for the main
  // thread whenever the main thread's message loop has any activity.
  PauseMainThreadAndServiceCallbacks();
}

static size_t
CGPathElementPointCount(CGPathElement* aElement)
{
  switch (aElement->type) {
  case kCGPathElementCloseSubpath:         return 0;
  case kCGPathElementMoveToPoint:
  case kCGPathElementAddLineToPoint:       return 1;
  case kCGPathElementAddQuadCurveToPoint:  return 2;
  case kCGPathElementAddCurveToPoint:      return 3;
  default: MOZ_CRASH();
  }
}

static void
CGPathApplierFunctionWrapper(void* aInfo, CGPathElement* aElement)
{
  RecordReplayCallback(CGPathApplierFunction, &aInfo);

  CGPathElement replayElement;
  if (IsReplaying()) {
    aElement = &replayElement;
  }

  aElement->type = (CGPathElementType) RecordReplayValue(aElement->type);

  size_t npoints = CGPathElementPointCount(aElement);
  if (IsReplaying()) {
    aElement->points = new CGPoint[npoints];
  }
  RecordReplayBytes(aElement->points, npoints * sizeof(CGPoint));

  rrc.mFunction(aInfo, aElement);

  if (IsReplaying()) {
    delete[] aElement->points;
  }
}

void
ReplayInvokeCallback(size_t aCallbackId)
{
  MOZ_RELEASE_ASSERT(IsReplaying());
  switch (aCallbackId) {
  case CallbackEvent_CFRunLoopPerformCallBack:
    CFRunLoopPerformCallBackWrapper(nullptr);
    break;
  case CallbackEvent_CGPathApplierFunction:
    CGPathApplierFunctionWrapper(nullptr, nullptr);
    break;
  default:
    MOZ_CRASH();
  }
}

///////////////////////////////////////////////////////////////////////////////
// system call redirections
///////////////////////////////////////////////////////////////////////////////

static ssize_t
RR_recv(int aSockFd, void* aBuf, size_t aLength, int aFlags)
{
  RecordReplayFunction(recv, ssize_t, aSockFd, aBuf, aLength, aFlags);
  events.CheckInput(aLength);
  if (RecordOrReplayHadErrorNegative(rrf)) {
    return rval;
  }
  MOZ_RELEASE_ASSERT((size_t) rval <= aLength);
  events.RecordOrReplayBytes(aBuf, rval);
  return rval;
}

static ssize_t
RR_recvmsg(int aSockFd, struct msghdr* aMsg, int aFlags)
{
  size_t* initialLengths = nullptr;
  if (!AreThreadEventsPassedThrough()) {
    initialLengths = new size_t[2 + aMsg->msg_iovlen];
    initialLengths[0] = aMsg->msg_controllen;
    initialLengths[1] = aMsg->msg_iovlen;
    for (int i = 0; i < aMsg->msg_iovlen; i++) {
      initialLengths[i + 2] = aMsg->msg_iov[i].iov_len;
    }
  }

  RecordReplayFunction(recvmsg, ssize_t, aSockFd, aMsg, aFlags);

  for (size_t i = 0; i < 2 + initialLengths[1]; i++) {
    events.CheckInput(initialLengths[i]);
  }
  delete[] initialLengths;

  if (RecordOrReplayHadErrorNegative(rrf)) {
    return rval;
  }

  events.RecordOrReplayValue(&aMsg->msg_flags);
  events.RecordOrReplayValue(&aMsg->msg_controllen);
  events.RecordOrReplayBytes(aMsg->msg_control, aMsg->msg_controllen);

  size_t nbytes = (size_t) rval;
  events.RecordOrReplayValue(&aMsg->msg_iovlen);
  for (int i = 0; i < aMsg->msg_iovlen && nbytes; i++) {
    struct iovec* iov = &aMsg->msg_iov[i];
    events.RecordOrReplayValue(&iov->iov_len);

    size_t iovbytes = nbytes <= iov->iov_len ? nbytes : iov->iov_len;
    events.RecordOrReplayBytes(iov->iov_base, iovbytes);
    nbytes -= iovbytes;
  }
  MOZ_RELEASE_ASSERT(nbytes == 0);
  return rval;
}

static ssize_t
RR_sendmsg(int aFd, struct msghdr* aMsg, int aFlags)
{
  RecordReplayFunction(sendmsg, ssize_t, aFd, aMsg, aFlags);
  events.CheckInput(aMsg->msg_iovlen);
  for (size_t i = 0; i < (size_t) aMsg->msg_iovlen; i++) {
    events.CheckInput(aMsg->msg_iov[i].iov_len);
  }
  RecordOrReplayHadErrorNegative(rrf);
  return rval;
}

static ssize_t
RR_kevent(int aKq, const struct kevent* aChanges, int aChangeCount,
          struct kevent* aEvents, int aEventCount,
          const struct timespec* aTimeout)
{
  RecordReplayFunction(kevent, ssize_t,
                       aKq, aChanges, aChangeCount, aEvents, aEventCount, aTimeout);
  if (RecordOrReplayHadErrorNegative(rrf)) {
    return rval;
  }

  // Copy the kevents verbatim. Note that kevent has an opaque pointer field
  // |udata| whose value could potentially vary between recording and replay.
  // Fortunately, all mozilla callers of this function only use scalar values
  // in |udata| so we don't need to be smarter here.
  events.CheckInput(aEventCount);
  events.RecordOrReplayBytes(aEvents, aEventCount * sizeof(struct kevent));
  return rval;
}

static ssize_t
RR_kevent64(int aKq, const struct kevent64_s* aChanges, int aChangeCount,
            struct kevent64_s* aEvents, int aEventCount, unsigned int aFlags,
            const struct timespec* aTimeout)
{
  RecordReplayFunction(kevent64, ssize_t,
                       aKq, aChanges, aChangeCount, aEvents, aEventCount, aFlags, aTimeout);
  if (RecordOrReplayHadErrorNegative(rrf)) {
    return rval;
  }

  // Copy the kevents verbatim, as for kevent().
  events.CheckInput(aEventCount);
  events.RecordOrReplayBytes(aEvents, aEventCount * sizeof(struct kevent64_s));
  return rval;
}

static ssize_t
RR_madvise(void* aAddress, size_t aSize, int aFlags)
{
  // Don't do anything with madvise for now, it is called by the JS GC.
  return OriginalCall(madvise, int, aAddress, aSize, aFlags);
}

static ssize_t
RR_mprotect(void* aAddress, size_t aSize, int aFlags)
{
  if (!HasSavedCheckpoint()) {
    return OriginalCall(mprotect, int, aAddress, aSize, aFlags);
  }
  return 0;
}

static void*
RR_mmap(void* aAddress, size_t aSize, int aProt, int aFlags, int aFd, off_t aOffset)
{
  MOZ_RELEASE_ASSERT(aAddress == PageBase(aAddress));

  // Make sure that fixed mappings do not interfere with snapshot state.
  if (aFlags & MAP_FIXED) {
    CheckFixedMemory(aAddress, RoundupSizeToPageBoundary(aSize));
  }

  void* memory = nullptr;
  if ((aFlags & MAP_ANON) || (IsReplaying() && !AreThreadEventsPassedThrough())) {
    // Get an anonymous mapping for the result.
    if (aFlags & MAP_FIXED) {
      // For fixed allocations, make sure this memory region is mapped and zero.
      if (!HasSavedCheckpoint()) {
        // Make sure this memory region is writable.
        OriginalCall(mprotect, int, aAddress, aSize, PROT_READ | PROT_WRITE | PROT_EXEC);
      }
      memset(aAddress, 0, aSize);
      memory = aAddress;
    } else {
      memory = AllocateMemoryTryAddress(aAddress, RoundupSizeToPageBoundary(aSize),
                                        MemoryKind::Tracked);
    }
  } else {
    // We have to call mmap itself, which can change memory protection flags
    // for memory that is already allocated. If we haven't saved a checkpoint
    // then this is no problem, but after saving a checkpoint we have to make
    // sure that protection flags are what we expect them to be.
    int newProt = HasSavedCheckpoint() ? (PROT_READ | PROT_EXEC) : aProt;
    memory = OriginalCall(mmap, void*, aAddress, aSize, newProt, aFlags, aFd, aOffset);

    if (aFlags & MAP_FIXED) {
      MOZ_RELEASE_ASSERT(memory == aAddress);
      RestoreWritableFixedMemory(memory, RoundupSizeToPageBoundary(aSize));
    } else if (memory && memory != (void*)-1) {
      RegisterAllocatedMemory(memory, RoundupSizeToPageBoundary(aSize), MemoryKind::Tracked);
    }
  }

  if (!(aFlags & MAP_ANON) && !AreThreadEventsPassedThrough()) {
    // Include the data just mapped in the recording.
    MOZ_RELEASE_ASSERT(memory && memory != (void*)-1);
    RecordReplayAssert("mmap");
    MOZ_RELEASE_ASSERT(aSize == RecordReplayValue(aSize));
    RecordReplayBytes(memory, aSize);
  }

  return memory;
}

static ssize_t
RR_munmap(void* aAddress, size_t aSize)
{
  DeallocateMemory(aAddress, aSize, MemoryKind::Tracked);
  return 0;
}

static ssize_t
RR_read(int aFd, void* aBuf, size_t aCount)
{
  RecordReplayFunction(read, ssize_t, aFd, aBuf, aCount);
  if (RecordOrReplayHadErrorNegative(rrf)) {
    return rval;
  }
  events.CheckInput(aCount);
  MOZ_RELEASE_ASSERT((size_t)rval <= aCount);
  events.RecordOrReplayBytes(aBuf, rval);
  return rval;
}

static ssize_t
RR___read_nocancel(int aFd, void* aBuf, size_t aCount)
{
  RecordReplayFunction(__read_nocancel, ssize_t, aFd, aBuf, aCount);
  if (RecordOrReplayHadErrorNegative(rrf)) {
    return rval;
  }
  events.CheckInput(aCount);
  MOZ_RELEASE_ASSERT((size_t)rval <= aCount);
  events.RecordOrReplayBytes(aBuf, rval);
  return rval;
}

static ssize_t
RR_pread(int aFd, void* aBuf, size_t aCount, size_t aOffset)
{
  RecordReplayFunction(pread, ssize_t, aFd, aBuf, aCount, aOffset);
  if (RecordOrReplayHadErrorNegative(rrf)) {
    return rval;
  }
  events.CheckInput(aCount);
  MOZ_RELEASE_ASSERT(!aBuf || (size_t) rval <= aCount);
  events.RecordOrReplayBytes(aBuf, rval);
  return rval;
}

static ssize_t
RR_write(int aFd, void* aBuf, size_t aCount)
{
  RecordReplayFunction(write, ssize_t, aFd, aBuf, aCount);
  RecordOrReplayHadErrorNegative(rrf);
  return rval;
}

RRFunctionNegError3(__write_nocancel)
RRFunctionNegError3(open)
RRFunctionNegError3(__open_nocancel)
RRFunctionNegError3(shm_open)
RRFunctionNegError3(socket)
RRFunctionNegError0(kqueue)
RRFunctionNegError1(close)
RRFunctionNegError1(__close_nocancel)
RRFunctionNegError2(mkdir)
RRFunctionNegError1(dup)
RRFunctionNegError2(access)
RRFunctionNegError3(lseek)

static ssize_t
RR_pipe(int* aFds)
{
  RecordReplayFunction(pipe, ssize_t, aFds);
  if (RecordOrReplayHadErrorNegative(rrf)) {
    return rval;
  }
  events.RecordOrReplayBytes(aFds, 2 * sizeof(int));
  return rval;
}

static ssize_t
RR_socketpair(int aDomain, int aType, int aProtocol, int* aFds)
{
  RecordReplayFunction(socketpair, ssize_t, aDomain, aType, aProtocol, aFds);
  if (RecordOrReplayHadErrorNegative(rrf)) {
    return rval;
  }
  events.RecordOrReplayBytes(aFds, 2 * sizeof(int));
  return rval;
}

static ssize_t
RR_fileport_makeport(int aFd, size_t* aPortName)
{
  RecordReplayFunction(fileport_makeport, ssize_t, aFd, aPortName);
  if (RecordOrReplayHadErrorNegative(rrf)) {
    return rval;
  }
  events.RecordOrReplayValue(aPortName);
  return rval;
}

static ssize_t
RR_getsockopt(int aSockFd, int aLevel, int aOptName, void* aOptVal, int* aOptLen)
{
  int initoptlen = *aOptLen;
  RecordReplayFunction(getsockopt, ssize_t, aSockFd, aLevel, aOptName, aOptVal, aOptLen);
  if (RecordOrReplayHadErrorNegative(rrf)) {
    return rval;
  }
  events.CheckInput((size_t) initoptlen);
  events.RecordOrReplayValue(aOptLen);
  MOZ_RELEASE_ASSERT(*aOptLen <= initoptlen);
  events.RecordOrReplayBytes(aOptVal, *aOptLen);
  return rval;
}

static ssize_t
RR_gettimeofday(struct timeval* aTimeVal, struct timezone* aTimeZone)
{
  // If we have diverged from the recording, just get the actual current time
  // rather than causing the current debugger operation to fail. This function
  // is frequently called via e.g. JS natives which the debugger will execute.
  if (HasDivergedFromRecording()) {
    AutoEnsurePassThroughThreadEvents pt;
    return OriginalCall(gettimeofday, ssize_t, aTimeVal, aTimeZone);
  }

  RecordReplayFunction(gettimeofday, ssize_t, aTimeVal, aTimeZone);
  if (RecordOrReplayHadErrorNegative(rrf)) {
    return rval;
  }
  events.CheckInput((aTimeVal ? 1 : 0) | (aTimeZone ? 2 : 0));
  if (aTimeVal) {
    events.RecordOrReplayBytes(aTimeVal, sizeof(struct timeval));
  }
  if (aTimeZone) {
    events.RecordOrReplayBytes(aTimeZone, sizeof(struct timezone));
  }
  return rval;
}

RRFunction0(getuid)
RRFunction0(geteuid)
RRFunction0(getgid)
RRFunction0(getegid)
RRFunction0(issetugid)
RRFunction0(__gettid)
RRFunction0(getpid)

static ssize_t
RR_fcntl(int aFd, int aCmd, size_t aArg)
{
  switch (aCmd) {
  case F_GETFL:
  case F_SETFL:
  case F_GETFD:
  case F_SETFD:
  case F_NOCACHE:
  case F_SETLK:
  case F_SETLKW:
    break;
  default:
    MOZ_CRASH();
  }

  RecordReplayFunction(fcntl, ssize_t, aFd, aCmd, aArg);
  RecordOrReplayHadErrorNegative(rrf);
  return rval;
}

static ssize_t
RR_getattrlist(const char* aPath, struct attrlist* aAttrList,
               void* aAttrBuf, size_t aAttrBufSize, unsigned long aOptions)
{
  RecordReplayFunction(getattrlist, ssize_t, aPath, aAttrList, aAttrBuf, aAttrBufSize, aOptions);
  if (RecordOrReplayHadErrorNegative(rrf)) {
    return rval;
  }
  events.CheckInput(aAttrBufSize);
  events.RecordOrReplayBytes(aAttrBuf, aAttrBufSize);
  return rval;
}

#define RecordReplayStatFunction(aName, aBufType)       \
  static ssize_t                                        \
  RR_ ##aName(size_t a0, aBufType* aBuf)                \
  {                                                     \
    RecordReplayFunction(aName, ssize_t, a0, aBuf);     \
    if (RecordOrReplayHadErrorNegative(rrf)) {          \
      return rval;                                      \
    }                                                   \
    events.RecordOrReplayBytes(aBuf, sizeof(aBufType)); \
    return rval;                                        \
  }

RecordReplayStatFunction(fstat$INODE64, struct stat)
RecordReplayStatFunction(lstat$INODE64, struct stat)
RecordReplayStatFunction(stat$INODE64, struct stat)
RecordReplayStatFunction(statfs$INODE64, struct statfs)
RecordReplayStatFunction(fstatfs$INODE64, struct statfs)

static ssize_t
RR_readlink(const char* aPath, char* aBuf, size_t aBufSize)
{
  RecordReplayFunction(readlink, ssize_t, aPath, aBuf, aBufSize);
  if (RecordOrReplayHadErrorNegative(rrf)) {
    return rval;
  }
  events.CheckInput(aBufSize);
  events.RecordOrReplayBytes(aBuf, aBufSize);
  return rval;
}

static ssize_t
RR___getdirentries64(int aFd, void* aBuf, size_t aBufSize, size_t* aPosition)
{
  RecordReplayFunction(__getdirentries64, ssize_t, aFd, aBuf, aBufSize, aPosition);
  if (RecordOrReplayHadErrorNegative(rrf)) {
    return rval;
  }
  events.CheckInput(aBufSize);
  events.RecordOrReplayBytes(aBuf, aBufSize);
  events.RecordOrReplayValue(aPosition);
  return rval;
}

static ssize_t
RR_getdirentriesattr(int aFd, struct attrlist* aAttrList,
                     void* aBuffer, size_t aBuffersize,
                     size_t* aCount, size_t* aBase, size_t* aNewState, size_t aOptions)
{
  RecordReplayFunction(getdirentriesattr, ssize_t,
                       aFd, aAttrList, aBuffer, aBuffersize, aCount, aBase, aNewState, aOptions);
  if (RecordOrReplayHadErrorNegative(rrf)) {
    return rval;
  }
  events.CheckInput(aBuffersize);
  events.RecordOrReplayBytes(aAttrList, sizeof(struct attrlist));
  events.RecordOrReplayBytes(aBuffer, aBuffersize);
  events.RecordOrReplayValue(aCount);
  events.RecordOrReplayValue(aBase);
  events.RecordOrReplayValue(aNewState);
  return rval;
}

static ssize_t
RR_getrusage(int aWho, struct rusage* aUsage)
{
  RecordReplayFunction(getrusage, ssize_t, aWho, aUsage);
  if (RecordOrReplayHadErrorNegative(rrf)) {
    return rval;
  }
  events.RecordOrReplayBytes(aUsage, sizeof(*aUsage));
  return rval;
}

static ssize_t
RR___getrlimit(int aWho, struct rlimit* aLimit)
{
  RecordReplayFunction(__getrlimit, ssize_t, aWho, aLimit);
  if (RecordOrReplayHadErrorNegative(rrf)) {
    return rval;
  }
  events.RecordOrReplayBytes(aLimit, sizeof(*aLimit));
  return rval;
}

RRFunctionNegError2(__setrlimit)

static ssize_t
RR_sigprocmask(int aHow, const sigset_t* aSet, sigset_t* aOldSet)
{
  RecordReplayFunction(sigprocmask, ssize_t, aHow, aSet, aOldSet);
  if (RecordOrReplayHadErrorNegative(rrf)) {
    return rval;
  }
  events.CheckInput(!!aOldSet);
  if (aOldSet) {
    events.RecordOrReplayBytes(aOldSet, sizeof(*aOldSet));
  }
  return rval;
}

static ssize_t
RR_sigaltstack(const stack_t* aSs, stack_t* aOldSs)
{
  RecordReplayFunction(sigaltstack, ssize_t, aSs, aOldSs);
  if (RecordOrReplayHadErrorNegative(rrf)) {
    return rval;
  }
  events.CheckInput(!!aOldSs);
  if (aOldSs) {
    events.RecordOrReplayBytes(aOldSs, sizeof(*aOldSs));
  }
  return rval;
}

static ssize_t
RR_sigaction(int aSignum, const struct sigaction* aAct, struct sigaction* aOldAct)
{
  RecordReplayFunction(sigaction, ssize_t, aSignum, aAct, aOldAct);
  if (RecordOrReplayHadErrorNegative(rrf)) {
    return rval;
  }
  events.CheckInput(!!aOldAct);
  if (aOldAct) {
    events.RecordOrReplayBytes(aOldAct, sizeof(struct sigaction));
  }
  return rval;
}

static ssize_t
RR___pthread_sigmask(int aHow, const sigset_t* aSet, sigset_t* aOldSet)
{
  RecordReplayFunction(__pthread_sigmask, ssize_t, aHow, aSet, aOldSet);
  if (RecordOrReplayHadErrorNegative(rrf)) {
    return rval;
  }
  events.CheckInput(!!aOldSet);
  if (aOldSet) {
    events.RecordOrReplayBytes(aOldSet, sizeof(sigset_t));
  }
  return rval;
}

static ssize_t
RR___fsgetpath(void* aBuf, size_t aBufSize, void* aFsId, uint64_t aObjId)
{
  RecordReplayFunction(__fsgetpath, ssize_t, aBuf, aBufSize, aFsId, aObjId);
  if (RecordOrReplayHadErrorNegative(rrf)) {
    return rval;
  }
  events.CheckInput(aBufSize);
  events.RecordOrReplayBytes(aBuf, aBufSize);
  return rval;
}

static ssize_t
RR___disable_threadsignal(int a0)
{
  // __disable_threadsignal is called when a thread finishes. During replay a
  // terminated thread can cause problems such as changing access bits on
  // tracked memory behind the scenes.
  //
  // Ideally, threads will never try to finish when we are replaying, since we
  // are supposed to have control over all threads in the system and only spawn
  // threads which will run forever. Unfortunately, GCD might have already
  // spawned threads before we were able to install our redirections, so use a
  // fallback here to keep these threads from terminating.
  if (IsReplaying()) {
    Thread::WaitForeverNoIdle();
  }
  return OriginalCall(__disable_threadsignal, ssize_t, a0);
}

static ssize_t
RR___sysctl(int* aName, size_t aNamelen, void* aOld, size_t* aOldlenp,
            void* aNewData, size_t aNewLen)
{
  size_t initlen = aOldlenp ? *aOldlenp : 0;
  RecordReplayFunction(__sysctl, ssize_t, aName, aNamelen, aOld, aOldlenp, aNewData, aNewLen);
  if (RecordOrReplayHadErrorNegative(rrf)) {
    return rval;
  }
  events.CheckInput((aOld ? 1 : 0) | (aOldlenp ? 2 : 0));
  events.CheckInput(initlen);
  if (aOldlenp) {
    events.RecordOrReplayValue(aOldlenp);
  }
  if (aOld) {
    MOZ_RELEASE_ASSERT(aOldlenp);
    events.RecordOrReplayBytes(aOld, *aOldlenp);
  }
  return rval;
}

RRFunctionNegError3(__mac_syscall)

static ssize_t
RR_getaudit_addr(auditinfo_addr_t* aAuditInfo, size_t aLength)
{
  RecordReplayFunction(getaudit_addr, ssize_t, aAuditInfo, aLength);
  if (RecordOrReplayHadErrorNegative(rrf)) {
    return rval;
  }
  events.RecordOrReplayBytes(aAuditInfo, sizeof(*aAuditInfo));
  return rval;
}

RRFunction1(umask)

static ssize_t
RR___select(int aFdCount, fd_set* aReadFds, fd_set* aWriteFds,
            fd_set* aExceptFds, timeval* aTimeout)
{
  RecordReplayFunction(__select, ssize_t, aFdCount, aReadFds, aWriteFds, aExceptFds, aTimeout);
  if (RecordOrReplayHadErrorNegative(rrf)) {
    return rval;
  }
  events.RecordOrReplayBytes(aReadFds, sizeof(fd_set));
  events.RecordOrReplayBytes(aWriteFds, sizeof(fd_set));
  events.RecordOrReplayBytes(aExceptFds, sizeof(fd_set));
  events.CheckInput(!!aTimeout);
  if (aTimeout) {
    events.RecordOrReplayBytes(aTimeout, sizeof(timeval));
  }
  return rval;
}

RRFunctionNegError3(__process_policy);
RRFunctionNegError6(__kdebug_trace);

static ssize_t
RR_guarded_kqueue_np(size_t* aGuard, int aFlags)
{
  RecordReplayFunction(guarded_kqueue_np, ssize_t, aGuard, aFlags);
  if (RecordOrReplayHadErrorNegative(rrf)) {
    return rval;
  }
  events.RecordOrReplayValue(aGuard);
  return rval;
}

static ssize_t
RR_csops(pid_t aPid, uint32_t aOps, void* aBuf, size_t aBufSize)
{
  RecordReplayFunction(csops, ssize_t, aPid, aOps, aBuf, aBufSize);
  if (RecordOrReplayHadErrorNegative(rrf)) {
    return rval;
  }
  events.CheckInput(aBufSize);
  events.RecordOrReplayBytes(aBuf, aBufSize);
  return rval;
}

static ssize_t
RR___getlogin(char* aName, size_t aLength)
{
  RecordReplayFunction(__getlogin, ssize_t, aName, aLength);
  if (RecordOrReplayHadErrorNegative(rrf)) {
    return rval;
  }
  events.CheckInput(aLength);
  events.RecordOrReplayBytes(aName, aLength);
  return rval;
}

static ssize_t
RR___workq_kernreturn(size_t a0, size_t a1, size_t a2, size_t a3)
{
  // Busy-wait until initialization is complete.
  while (!gInitialized) {
    ThreadYield();
  }

  // Make sure we know this thread exists.
  Thread::Current();

  return OriginalCall(__workq_kernreturn, ssize_t, a0, a1, a2, a3);
}

static void
RR_start_wqthread(size_t a0, size_t a1, size_t a2, size_t a3, size_t a4, size_t a5, size_t a6, size_t a7)
{
  // When replaying we don't want system threads to run, but by the time we
  // initialize the record/replay system GCD has already started running.
  // Use this redirection to watch for new threads being spawned by GCD, and
  // suspend them immediately.
  if (IsReplaying()) {
    Thread::WaitForeverNoIdle();
  }

  OriginalCall(start_wqthread, void, a0, a1, a2, a3, a4, a5, a6, a7);
}

///////////////////////////////////////////////////////////////////////////////
// pthreads redirections
///////////////////////////////////////////////////////////////////////////////

static void
DirectLockMutex(pthread_mutex_t* aMutex)
{
  AutoPassThroughThreadEvents pt;
  ssize_t rv = OriginalCall(pthread_mutex_lock, ssize_t, aMutex);
  MOZ_RELEASE_ASSERT(rv == 0);
}

static void
DirectUnlockMutex(pthread_mutex_t* aMutex)
{
  AutoPassThroughThreadEvents pt;
  ssize_t rv = OriginalCall(pthread_mutex_unlock, ssize_t, aMutex);
  MOZ_RELEASE_ASSERT(rv == 0);
}

// Handle a redirection which releases a mutex, waits in some way for a cvar,
// and reacquires the mutex before returning.
static ssize_t
WaitForCvar(pthread_mutex_t* aMutex, bool aRecordReturnValue,
            const std::function<ssize_t()>& aCallback)
{
  Lock* lock = Lock::Find(aMutex);
  if (!lock) {
    AutoEnsurePassThroughThreadEvents pt;
    return aCallback();
  }
  RecordReplayAssert("WaitForCvar %d", (int) lock->Id());
  ssize_t rv = 0;
  if (IsRecording()) {
    AutoPassThroughThreadEvents pt;
    rv = aCallback();
  } else {
    DirectUnlockMutex(aMutex);
  }
  lock->Exit();
  lock->Enter();
  if (IsReplaying()) {
    DirectLockMutex(aMutex);
  }
  if (aRecordReturnValue) {
    return RecordReplayValue(rv);
  }
  MOZ_RELEASE_ASSERT(rv == 0);
  return 0;
}

static ssize_t
RR_pthread_cond_wait(pthread_cond_t* aCond, pthread_mutex_t* aMutex)
{
  return WaitForCvar(aMutex, false,
                     [=]() { return OriginalCall(pthread_cond_wait, ssize_t, aCond, aMutex); });
}

static ssize_t
RR_pthread_cond_timedwait(pthread_cond_t* aCond, pthread_mutex_t* aMutex,
                          timespec* aTimeout)
{
  return WaitForCvar(aMutex, true,
                     [=]() { return OriginalCall(pthread_cond_timedwait, ssize_t,
                                                 aCond, aMutex, aTimeout); });
}

static ssize_t
RR_pthread_cond_timedwait_relative_np(pthread_cond_t* aCond, pthread_mutex_t* aMutex,
                                      timespec* aTimeout)
{
  return WaitForCvar(aMutex, true,
                     [=]() { return OriginalCall(pthread_cond_timedwait_relative_np, ssize_t,
                                                 aCond, aMutex, aTimeout); });
}

static ssize_t
RR_pthread_create(pthread_t* aToken, const pthread_attr_t* aAttr,
                  void* (*aStart)(void*), void* aStartArg)
{
  if (AreThreadEventsPassedThrough()) {
    return OriginalCall(pthread_create, ssize_t, aToken, aAttr, aStart, aStartArg);
  }

  int detachState;
  int rv = pthread_attr_getdetachstate(aAttr, &detachState);
  MOZ_RELEASE_ASSERT(rv == 0);

  *aToken = Thread::StartThread((Thread::Callback) aStart, aStartArg,
                                detachState == PTHREAD_CREATE_JOINABLE);
  return 0;
}

static ssize_t
RR_pthread_join(pthread_t aToken, void** aPtr)
{
  if (AreThreadEventsPassedThrough()) {
    return OriginalCall(pthread_join, int, aToken, aPtr);
  }

  Thread* thread = Thread::GetByNativeId(aToken);
  thread->Join();

  *aPtr = nullptr;
  return 0;
}

static ssize_t
RR_pthread_mutex_init(pthread_mutex_t* aMutex, const pthread_mutexattr_t* aAttr)
{
  Lock::New(aMutex);
  return OriginalCall(pthread_mutex_init, ssize_t, aMutex, aAttr);
}

static ssize_t
RR_pthread_mutex_destroy(pthread_mutex_t* aMutex)
{
  Lock::Destroy(aMutex);
  return OriginalCall(pthread_mutex_destroy, ssize_t, aMutex);
}

static ssize_t
RR_pthread_mutex_lock(pthread_mutex_t* aMutex)
{
  Lock* lock = Lock::Find(aMutex);
  if (!lock) {
    AutoEnsurePassThroughThreadEventsUseStackPointer pt;
    return OriginalCall(pthread_mutex_lock, ssize_t, aMutex);
  }
  ssize_t rv = 0;
  if (IsRecording()) {
    AutoPassThroughThreadEvents pt;
    rv = OriginalCall(pthread_mutex_lock, ssize_t, aMutex);
  }
  rv = RecordReplayValue(rv);
  MOZ_RELEASE_ASSERT(rv == 0 || rv == EDEADLK);
  if (rv == 0) {
    lock->Enter();
    if (IsReplaying()) {
      DirectLockMutex(aMutex);
    }
  }
  return rv;
}

static ssize_t
RR_pthread_mutex_trylock(pthread_mutex_t* aMutex)
{
  Lock* lock = Lock::Find(aMutex);
  if (!lock) {
    AutoEnsurePassThroughThreadEvents pt;
    return OriginalCall(pthread_mutex_trylock, ssize_t, aMutex);
  }
  ssize_t rv = 0;
  if (IsRecording()) {
    AutoPassThroughThreadEvents pt;
    rv = OriginalCall(pthread_mutex_trylock, ssize_t, aMutex);
  }
  rv = RecordReplayValue(rv);
  MOZ_RELEASE_ASSERT(rv == 0 || rv == EBUSY);
  if (rv == 0) {
    lock->Enter();
    if (IsReplaying()) {
      DirectLockMutex(aMutex);
    }
  }
  return rv;
}

static ssize_t
RR_pthread_mutex_unlock(pthread_mutex_t* aMutex)
{
  Lock* lock = Lock::Find(aMutex);
  if (!lock) {
    AutoEnsurePassThroughThreadEventsUseStackPointer pt;
    return OriginalCall(pthread_mutex_unlock, ssize_t, aMutex);
  }
  lock->Exit();
  DirectUnlockMutex(aMutex);
  return 0;
}

///////////////////////////////////////////////////////////////////////////////
// stdlib redirections
///////////////////////////////////////////////////////////////////////////////

static ssize_t
RR_dlclose(void* aHandle)
{
  return 0;
}

static void*
RR_dlopen(const char* aFilename, int aFlags)
{
  AutoEnsurePassThroughThreadEvents pt;
  return OriginalCall(dlopen, void*, aFilename, aFlags);
}

static void*
RR_dlsym(void* aHandle, const char* aSymbol)
{
  AutoEnsurePassThroughThreadEvents pt;
  return OriginalCall(dlsym, void*, aHandle, aSymbol);
}

RRFunctionNegError1(fclose)
RRFunctionZeroError2(fopen)

static size_t
RR_fread(void *aBuf, size_t aElemSize, size_t aCapacity, FILE *aFile)
{
  RecordReplayFunction(fread, size_t, aBuf, aElemSize, aCapacity, aFile);
  events.RecordOrReplayValue(&rval);
  events.CheckInput(aElemSize);
  events.CheckInput(aCapacity);
  MOZ_RELEASE_ASSERT((size_t)rval <= aCapacity);
  events.RecordOrReplayBytes(aBuf, rval * aElemSize);
  return rval;
}

RRFunctionNegError3(fseek)
RRFunctionNegError1(ftell)
RRFunction4(fwrite)

static void
RecordReplayCString(Stream& aEvents, char** aString)
{
  size_t len = (IsRecording() && *aString) ? strlen(*aString) + 1 : 0;
  aEvents.RecordOrReplayValue(&len);
  if (IsReplaying()) {
    *aString = len ? NewLeakyArray<char>(len) : nullptr;
  }
  if (len) {
    aEvents.RecordOrReplayBytes(*aString, len);
  }
}

static char*
RR_getenv(char* aName)
{
  RecordReplayFunction(getenv, char*, aName);
  RecordReplayCString(events, &rval);
  return rval;
}

#define RecordReplayTimeFunction(aName)                                 \
  static struct tm*                                                     \
  RR_ ##aName(const time_t* aTime, struct tm* aResult)                  \
  {                                                                     \
    MOZ_RELEASE_ASSERT(aResult);                                        \
    RecordReplayFunction(aName, struct tm*, aTime, aResult);            \
    if (RecordOrReplayHadErrorZero(rrf)) {                              \
      return nullptr;                                                   \
    }                                                                   \
    events.RecordOrReplayBytes(aResult, sizeof(struct tm));             \
    if (IsRecording()) {                                                \
      MOZ_RELEASE_ASSERT(rval == aResult);                              \
    }                                                                   \
    return aResult;                                                     \
  }

RecordReplayTimeFunction(localtime_r)
RecordReplayTimeFunction(gmtime_r)

static struct tm gGlobalTM;

// localtime and gmtime behave the same as localtime_r and gmtime_r, except
// they are not reentrant.
#define RecordReplayNonReentrantTimeFunction(aName)                     \
  static struct tm*                                                     \
  RR_ ##aName(const time_t* aTime)                                      \
  {                                                                     \
    return RR_ ##aName ##_r(aTime, &gGlobalTM);                         \
  }

RecordReplayNonReentrantTimeFunction(localtime)
RecordReplayNonReentrantTimeFunction(gmtime)

static time_t
RR_mktime(struct tm* aTime)
{
  RecordReplayFunction(mktime, time_t, aTime);
  events.RecordOrReplayBytes(aTime, sizeof(struct tm));
  events.RecordOrReplayValue(&rval);
  return rval;
}

static char*
RR_setlocale(int aCategory, const char* aLocale)
{
  RecordReplayFunction(setlocale, char*, aCategory, aLocale);
  RecordReplayCString(events, &rval);
  return rval;
}

static size_t
RR_strftime(char* aStr, size_t aSize, const char* aFormat, const struct tm* aTime)
{
  RecordReplayFunction(strftime, size_t, aStr, aSize, aFormat, aTime);
  events.CheckInput(aSize);
  events.RecordOrReplayValue(&rval);
  MOZ_RELEASE_ASSERT(rval < aSize);
  events.RecordOrReplayBytes(aStr, rval + 1);
  return rval;
}

static size_t
RR_arc4random()
{
  RecordReplayFunction(arc4random, size_t);
  events.RecordOrReplayValue(&rval);
  return rval;
}

static uint64_t
RR_mach_absolute_time()
{
  // This function might be called through OSSpinLock while setting gTlsThreadKey.
  Thread* thread = Thread::GetByStackPointer(&thread);
  if (!thread || thread->PassThroughEvents()) {
    return OriginalCall(mach_absolute_time, uint64_t);
  }

  RecordReplayFunction(mach_absolute_time, uint64_t);
  events.RecordOrReplayValue(&rval);
  return rval;
}

static mach_msg_return_t
RR_mach_msg(mach_msg_header_t* aMsg, mach_msg_option_t aOption, mach_msg_size_t aSendSize,
            mach_msg_size_t aReceiveLimit, mach_port_t aReceiveName, mach_msg_timeout_t aTimeout,
            mach_port_t aNotify)
{
  RecordReplayFunction(mach_msg, mach_msg_return_t,
                       aMsg, aOption, aSendSize, aReceiveLimit, aReceiveName, aTimeout, aNotify);
  AutoOrderedAtomicAccess(); // mach_msg may be used for inter-thread synchronization.
  events.RecordOrReplayValue(&rval);
  events.CheckInput(aReceiveLimit);
  if (aReceiveLimit) {
    events.RecordOrReplayBytes(aMsg, aReceiveLimit);
  }
  return rval;
}

static kern_return_t
RR_mach_timebase_info(mach_timebase_info_data_t* aInfo)
{
  RecordReplayFunction(mach_timebase_info, kern_return_t, aInfo);
  events.RecordOrReplayValue(&rval);
  events.RecordOrReplayBytes(aInfo, sizeof(*aInfo));
  return rval;
}

static void
RR_sandbox_free_error(char* aBuf)
{
  if (IsRecording()) {
    OriginalCall(sandbox_free_error, void, aBuf);
  } else {
    delete[] aBuf;
  }
}

static ssize_t
RR_sandbox_init(const char* aProfile, uint64_t aFlags, char** aErrorBuf)
{
  // By passing through events when initializing the sandbox, we ensure both
  // that we actually initialize the process sandbox while replaying as well as
  // while recording, and that any activity in here does not interfere with the
  // replay.
  AutoEnsurePassThroughThreadEvents pt;
  return OriginalCall(sandbox_init, ssize_t,
                      aProfile, aFlags, aErrorBuf);
}

static ssize_t
RR_sandbox_init_with_parameters(const char* aProfile, uint64_t aFlags,
                                const char *const aParameters[], char** aErrorBuf)
{
  // As for sandbox_init, call this function even while replaying.
  AutoEnsurePassThroughThreadEvents pt;
  return OriginalCall(sandbox_init_with_parameters, ssize_t,
                      aProfile, aFlags, aParameters, aErrorBuf);
}

static kern_return_t
RR_task_threads(mach_port_t aTask, thread_act_array_t *aThreads,
                mach_msg_type_number_t *aNumThreads)
{
  // Make sure events are passed through here so that replaying processes can
  // inspect their own threads.
  AutoEnsurePassThroughThreadEvents pt;
  return OriginalCall(task_threads, kern_return_t, aTask, aThreads, aNumThreads);
}

static kern_return_t
RR_mach_vm_allocate(vm_map_t aTarget, mach_vm_address_t* aAddress,
                    mach_vm_size_t aSize, int aFlags)
{
  *aAddress = (mach_vm_address_t) AllocateMemory(aSize, MemoryKind::Tracked);
  return KERN_SUCCESS;
}

static kern_return_t
RR_mach_vm_deallocate(vm_map_t aTarget, mach_vm_address_t aAddress, mach_vm_size_t aSize)
{
  DeallocateMemory((void*) aAddress, aSize, MemoryKind::Tracked);
  return KERN_SUCCESS;
}

static kern_return_t
RR_mach_vm_protect(vm_map_t aTarget, mach_vm_address_t aAddress, mach_vm_size_t aSize,
                   boolean_t aSetMaximum, vm_prot_t aNewProtection)
{
  if (!HasSavedCheckpoint()) {
    return OriginalCall(mach_vm_protect, kern_return_t,
                        aTarget, aAddress, aSize, aSetMaximum, aNewProtection);
  }
  return KERN_SUCCESS;
}

static kern_return_t
RR_vm_purgable_control(vm_map_t aTarget, mach_vm_address_t aAddress,
                       vm_purgable_t aControl, int* aState)
{
  // Never allow purging of volatile memory, to simplify memory snapshots.
  *aState = VM_PURGABLE_NONVOLATILE;
  return KERN_SUCCESS;
}

static kern_return_t
RR_vm_copy(vm_map_t aTarget, vm_address_t aSourceAddress,
           vm_size_t aSize, vm_address_t aDestAddress)
{
  // Asking the kernel to copy memory doesn't work right if the destination is
  // non-writable, so do the copy manually.
  memcpy((void*) aDestAddress, (void*) aSourceAddress, aSize);
  return KERN_SUCCESS;
}

RRFunctionVoid0(tzset)

///////////////////////////////////////////////////////////////////////////////
// NSPR redirections
///////////////////////////////////////////////////////////////////////////////

// Even though NSPR is compiled as part of firefox, it is easier to just
// redirect this stuff than get record/replay related changes into the source.

static PLHashTable*
RR_PL_NewHashTable(PRUint32 aNumBuckets, PLHashFunction aKeyHash,
                   PLHashComparator aKeyCompare, PLHashComparator aValueCompare,
                   const PLHashAllocOps *aAllocOps, void *aAllocPriv)
{
  GeneratePLHashTableCallbacks(&aKeyHash, &aKeyCompare, &aValueCompare, &aAllocOps, &aAllocPriv);
  return OriginalCall(PL_NewHashTable, PLHashTable*,
                      aNumBuckets, aKeyHash, aKeyCompare, aValueCompare, aAllocOps, aAllocPriv);
}

static void
RR_PL_HashTableDestroy(PLHashTable* aTable)
{
  void* priv = aTable->allocPriv;
  OriginalCall(PL_HashTableDestroy, void, aTable);
  DestroyPLHashTableCallbacks(priv);
}

///////////////////////////////////////////////////////////////////////////////
// Objective C redirections
///////////////////////////////////////////////////////////////////////////////

RRFunction2(class_getClassMethod)
RRFunction2(class_getInstanceMethod)
RRFunctionVoid2(method_exchangeImplementations)
RRFunctionVoid1(objc_autoreleasePoolPop)
RRFunction0(objc_autoreleasePoolPush)

// Capture the arguments that can be passed to an Objective C message. We only
// need to capture enough argument data here for messages sent directly from
// Gecko code (i.e. where events are not passed through). Messages sent while
// events are passed through are performed with the same stack and register
// state as when they were initially invoked.
struct MessageArguments
{
  size_t obj;       // 0
  const char* msg;  // 8
  size_t arg0;      // 16
  size_t arg1;      // 24
  size_t arg2;      // 32
  size_t arg3;      // 40
  double floatarg0; // 48
  size_t scratch;   // 56
  size_t stack[64]; // 64
};                  // 576

extern "C" {

extern size_t
RecordReplayInvokeObjCMessage(MessageArguments* aArguments, void* aFnPtr);

__asm(
"_RecordReplayInvokeObjCMessage:"

  // Save function pointer in rax.
  "movq %rsi, %rax;"

  // Save arguments on the stack. This also aligns the stack.
  "push %rdi;"

  // Count how many stack arguments we need to copy.
  "movq $64, %rsi;"

  // Enter the loop below. The compiler might not place this block of code
  // adjacent to the loop, so perform the jump explicitly.
  "jmp _RecordReplayInvokeObjCMessage_Loop;"

  // Copy each stack argument to the stack.
"_RecordReplayInvokeObjCMessage_Loop:"
  "subq $1, %rsi;"
  "movq 64(%rdi, %rsi, 8), %rdx;"
  "push %rdx;"
  "testq %rsi, %rsi;"
  "jne _RecordReplayInvokeObjCMessage_Loop;"

  // Copy each register argument into the appropriate register.
  "movq 8(%rdi), %rsi;"
  "movq 16(%rdi), %rdx;"
  "movq 24(%rdi), %rcx;"
  "movq 32(%rdi), %r8;"
  "movq 40(%rdi), %r9;"
  "movsd 48(%rdi), %xmm0;"
  "movq 0(%rdi), %rdi;"

  // Call the saved function pointer.
  "callq *%rax;"

  // Pop the copied stack arguments.
  "addq $512, %rsp;"

  // Save any floating point rval to the arguments.
  "pop %rdi;"
  "movsd %xmm0, 48(%rdi);"

  "ret;"
);

} // extern "C"

static bool
TestObjCObjectClass(size_t aObj, const char* aName)
{
  // When recording we can test to see what the class of an object is, but we
  // have to record the result of the test because the object "pointers" we
  // have when replaying are not valid.
  bool found = false;
  if (IsRecording()) {
    Class cls = object_getClass((id)aObj);
    while (cls) {
      if (!strcmp(class_getName(cls), aName)) {
        found = true;
        break;
      }
      cls = class_getSuperclass(cls);
    }
  }
  return RecordReplayValue(found);
}

// From Foundation.h, which has lots of Objective C declarations and can't be
// included here :(
struct NSFastEnumerationState
{
  unsigned long state;
  id* itemsPtr;
  unsigned long* mutationsPtr;
  unsigned long extra[5];
};

// Emulation of NSFastEnumeration on arrays does not replay any exceptions
// thrown by mutating the array while it is being iterated over.
static unsigned long gNeverChange;

extern "C" {

size_t __attribute__((used))
RecordReplayInterceptObjCMessage(MessageArguments* aArguments)
{
  if (AreThreadEventsPassedThrough()) {
    aArguments->scratch = (size_t) OriginalFunction(CallEvent_objc_msgSend);
    return 1;
  }
  EnsureNotDivergedFromRecording();

  RecordReplayAssert("objc_msgSend: %s", aArguments->msg);

  size_t rval = 0;
  double floatRval = 0;
  bool handled = false;

  // Watch for some top level NSApplication messages that can cause Gecko
  // events to be processed.
  if ((!strcmp(aArguments->msg, "run") ||
       !strcmp(aArguments->msg, "nextEventMatchingMask:untilDate:inMode:dequeue:")) &&
      TestObjCObjectClass(aArguments->obj, "NSApplication"))
  {
    PassThroughThreadEventsAllowCallbacks([&]() {
        rval = RecordReplayInvokeObjCMessage(aArguments, OriginalFunction(CallEvent_objc_msgSend));
      });
    handled = true;
  }

  // Other messages are performed as normal.
  if (!handled && IsRecording()) {
    AutoPassThroughThreadEvents pt;
    rval = RecordReplayInvokeObjCMessage(aArguments, OriginalFunction(CallEvent_objc_msgSend));
    floatRval = aArguments->floatarg0;
  }

  rval = RecordReplayValue(rval);
  RecordReplayBytes(&floatRval, sizeof(double));

  // Do some extra recording on messages that return additional state.

  if (!strcmp(aArguments->msg, "countByEnumeratingWithState:objects:count:") &&
      TestObjCObjectClass(aArguments->obj, "NSArray"))
  {
    NSFastEnumerationState* state = (NSFastEnumerationState*)aArguments->arg0;
    if (IsReplaying()) {
      state->itemsPtr = NewLeakyArray<id>(rval);
      state->mutationsPtr = &gNeverChange;
    }
    RecordReplayBytes(state->itemsPtr, rval * sizeof(id));
  }

  if (!strcmp(aArguments->msg, "getCharacters:") &&
      TestObjCObjectClass(aArguments->obj, "NSString"))
  {
    size_t len = 0;
    if (IsRecording()) {
      AutoPassThroughThreadEvents pt;
      len = CFStringGetLength((CFStringRef) aArguments->obj);
    }
    len = RecordReplayValue(len);
    RecordReplayBytes((void*)aArguments->arg0, len * sizeof(wchar_t));
  }

  if ((!strcmp(aArguments->msg, "UTF8String") ||
       !strcmp(aArguments->msg, "cStringUsingEncoding:")) &&
      TestObjCObjectClass(aArguments->obj, "NSString"))
  {
    size_t len = RecordReplayValue(IsRecording() ? strlen((const char*) rval) : 0);
    if (IsReplaying()) {
      rval = (size_t) NewLeakyArray<uint8_t>(len + 1);
    }
    RecordReplayBytes((void*) rval, len + 1);
  }

  // Remember the return value for the caller.
  aArguments->scratch = rval;
  aArguments->floatarg0 = floatRval;
  return 0;
}

extern size_t
RR_objc_msgSend(id, SEL, ...);

__asm(
"_RR_objc_msgSend:"

  // Make space for a MessageArguments struct on the stack.
  "subq $576, %rsp;"

  // Fill in the structure's contents.
  "movq %rdi, 0(%rsp);"
  "movq %rsi, 8(%rsp);"
  "movq %rdx, 16(%rsp);"
  "movq %rcx, 24(%rsp);"
  "movq %r8, 32(%rsp);"
  "movq %r9, 40(%rsp);"
  "movsd %xmm0, 48(%rsp);"

  // Count how many stack arguments we need to save.
  "movq $64, %rsi;"

  // Enter the loop below. The compiler might not place this block of code
  // adjacent to the loop, so perform the jump explicitly.
  "jmp _RR_objc_msgSend_Loop;"

  // Save stack arguments into the structure.
"_RR_objc_msgSend_Loop:"
  "subq $1, %rsi;"
  "movq 584(%rsp, %rsi, 8), %rdx;" // Ignore the return ip on the stack.
  "movq %rdx, 64(%rsp, %rsi, 8);"
  "testq %rsi, %rsi;"
  "jne _RR_objc_msgSend_Loop;"

  // Place the structure's address into the first argument register.
  "movq %rsp, %rdi;"

  // The stack must be have 16 byte alignment. The MessageArguments structure
  // uses 576 bytes, and the frame pointer pushed by the callee uses 8 bytes,
  // so push 8 bytes of empty space.
  "subq $8, %rsp;"
  "call _RecordReplayInterceptObjCMessage;"
  "addq $8, %rsp;"

  "testq %rax, %rax;"
  "je RR_objc_msgSend_done;"

  // Events are passed through. Invoke the message with the original stack and
  // register state.
  "movq 0(%rsp), %rdi;"
  "movq 8(%rsp), %rsi;"
  "movq 16(%rsp), %rdx;"
  "movq 24(%rsp), %rcx;"
  "movq 32(%rsp), %r8;"
  "movq 40(%rsp), %r9;"
  "movsd 48(%rsp), %xmm0;"
  "movq 56(%rsp), %rax;"
  "addq $576, %rsp;"
  "jmpq *%rax;"

  // The message has been recorded/replayed.
"RR_objc_msgSend_done:"
  // Restore scalar and floating point return values.
  "movsd 48(%rsp), %xmm0;"
  "movq 56(%rsp), %rax;"

  // Pop the structure from the stack.
  "addq $576, %rsp;"

  // Return to caller.
  "ret;"
);

} // extern "C"

///////////////////////////////////////////////////////////////////////////////
// Cocoa redirections
///////////////////////////////////////////////////////////////////////////////

RRFunction4(AcquireFirstMatchingEventInQueue)

static void
RR_CFArrayAppendValue(CFArrayRef aArray, CFTypeRef aValue)
{
  RecordReplayFunctionVoid(CFArrayAppendValue, aArray, aValue);
}

RRFunction4(CFArrayCreate)
RRFunction1(CFArrayGetCount)
RRFunction2(CFArrayGetValueAtIndex)
RRFunctionVoid2(CFArrayRemoveValueAtIndex)
RRFunction3(CFAttributedStringCreate)
RRFunction1(CFBundleCopyExecutableURL)
RRFunction1(CFBundleCopyInfoDictionaryForURL)
RRFunction2(CFBundleCreate)
RRFunction1(CFBundleGetBundleWithIdentifier)

static void*
RR_CFBundleGetDataPointerForName(CFBundleRef aBundle, CFStringRef aSymbolName)
{
  if (AreThreadEventsPassedThrough()) {
    return OriginalCall(CFBundleGetDataPointerForName, void*, aBundle, aSymbolName);
  }
  return nullptr;
}

static void*
RR_CFBundleGetFunctionPointerForName(CFBundleRef aBundle, CFStringRef aFunctionName)
{
  if (AreThreadEventsPassedThrough()) {
    return OriginalCall(CFBundleGetFunctionPointerForName, void*, aBundle, aFunctionName);
  }
  return nullptr;
}

RRFunction1(CFBundleGetIdentifier)
RRFunction2(CFBundleGetValueForInfoDictionaryKey)
RRFunction1(CFBundleGetInfoDictionary)
RRFunction0(CFBundleGetMainBundle)

static const UInt8*
RR_CFDataGetBytePtr(CFDataRef aData)
{
  RecordReplayFunction(CFDataGetBytePtr, const UInt8*, aData);
  size_t len = 0;
  if (IsRecording()) {
    len = OriginalCall(CFDataGetLength, size_t, aData);
  }
  events.RecordOrReplayValue(&len);
  if (IsReplaying()) {
    rval = NewLeakyArray<UInt8>(len);
  }
  events.RecordOrReplayBytes((void*) rval, len);
  return rval;
}

RRFunction1(CFDataGetLength)
RRFunction4(CFDateFormatterCreate)
RRFunction1(CFDateFormatterGetFormat)
RRFunctionVoid3(CFDictionaryAddValue)
RRFunction6(CFDictionaryCreate)
RRFunction4(CFDictionaryCreateMutable)
RRFunction3(CFDictionaryCreateMutableCopy)
RRFunction2(CFDictionaryGetValue)

static Boolean
RR_CFDictionaryGetValueIfPresent(CFDictionaryRef aDict, const void *aKey, const void **aValue)
{
  RecordReplayFunction(CFDictionaryGetValueIfPresent, Boolean, aDict, aKey, aValue);
  events.RecordOrReplayValue(aValue);
  events.RecordOrReplayValue(&rval);
  return rval;
}

RRFunctionVoid3(CFDictionaryReplaceValue)
RRFunction2(CFEqual)
RRFunction1(CFGetTypeID)
RRFunction0(CFLocaleCopyCurrent)
RRFunction0(CFLocaleCopyPreferredLanguages)
RRFunction2(CFLocaleCreate)
RRFunction1(CFLocaleGetIdentifier)

static void DummyCFNotificationCallback(CFNotificationCenterRef aCenter, void* aObserver,
                                        CFStringRef aName, const void* aObject,
                                        CFDictionaryRef aUserInfo)
{
  // FIXME
  //MOZ_CRASH();
}

static void
RR_CFNotificationCenterAddObserver(CFNotificationCenterRef aCenter, const void* aObserver,
                                   CFNotificationCallback aCallback, CFStringRef aName,
                                   const void* aObject,
                                   CFNotificationSuspensionBehavior aSuspensionBehavior)
{
  if (!AreThreadEventsPassedThrough()) {
    aCallback = DummyCFNotificationCallback;
  }

  RecordReplayFunctionVoid(CFNotificationCenterAddObserver,
                           aCenter, aObserver, aCallback, aName, aObject, aSuspensionBehavior);
}

RRFunction0(CFNotificationCenterGetLocalCenter)
RRFunctionVoid4(CFNotificationCenterRemoveObserver)
RRFunction3(CFNumberCreate)

static size_t
CFNumberTypeBytes(CFNumberType aType)
{
  switch (aType) {
  case kCFNumberSInt8Type: return sizeof(int8_t);
  case kCFNumberSInt16Type: return sizeof(int16_t);
  case kCFNumberSInt32Type: return sizeof(int32_t);
  case kCFNumberSInt64Type: return sizeof(int64_t);
  case kCFNumberFloat32Type: return sizeof(float);
  case kCFNumberFloat64Type: return sizeof(double);
  case kCFNumberCharType: return sizeof(char);
  case kCFNumberShortType: return sizeof(short);
  case kCFNumberIntType: return sizeof(int);
  case kCFNumberLongType: return sizeof(long);
  case kCFNumberLongLongType: return sizeof(long long);
  case kCFNumberFloatType: return sizeof(float);
  case kCFNumberDoubleType: return sizeof(double);
  case kCFNumberCFIndexType: return sizeof(CFIndex);
  case kCFNumberNSIntegerType: return sizeof(long);
  case kCFNumberCGFloatType: return sizeof(CGFloat);
  default: MOZ_CRASH();
  }
}

static Boolean
RR_CFNumberGetValue(CFNumberRef aNumber, CFNumberType aType, void* aValuePtr)
{
  RecordReplayFunction(CFNumberGetValue, Boolean, aNumber, aType, aValuePtr);
  events.CheckInput(aType);
  events.RecordOrReplayValue(&rval);
  events.RecordOrReplayBytes(aValuePtr, CFNumberTypeBytes(aType));
  return rval;
}

RRFunction1(CFNumberIsFloatType)
RRFunction2(CFPreferencesAppValueIsForced)
RRFunction2(CFPreferencesCopyAppValue)
RRFunction4(CFPreferencesCopyValue)
RRFunction4(CFPropertyListCreateFromXMLData)
RRFunction6(CFPropertyListCreateWithStream)
RRFunctionVoid1(CFReadStreamClose)
RRFunction2(CFReadStreamCreateWithFile)
RRFunction1(CFReadStreamOpen)
RRFunction1(CFRelease)
RRFunction1(CFRetain)
RRFunctionVoid3(CFRunLoopAddSource)
RRFunction0(CFRunLoopGetCurrent)
RRFunctionVoid3(CFRunLoopRemoveSource)

static CFRunLoopSourceRef
RR_CFRunLoopSourceCreate(CFAllocatorRef aAllocator, CFIndex aOrder, CFRunLoopSourceContext* aCx)
{
  if (AreThreadEventsPassedThrough()) {
    return OriginalCall(CFRunLoopSourceCreate, CFRunLoopSourceRef, aAllocator, aOrder, aCx);
  }

  RegisterCallbackData(BitwiseCast<void*>(aCx->perform));
  RegisterCallbackData(aCx->info);

  if (IsRecording()) {
    CallbackWrapperData* wrapperData = new CallbackWrapperData(aCx->perform, aCx->info);
    aCx->perform = CFRunLoopPerformCallBackWrapper;
    aCx->info = wrapperData;
  }

  RecordReplayFunction(CFRunLoopSourceCreate, CFRunLoopSourceRef, aAllocator, aOrder, aCx);
  events.RecordOrReplayValue((size_t*)&rval);
  return rval;
}

RRFunctionVoid1(CFRunLoopSourceSignal)
RRFunctionVoid1(CFRunLoopWakeUp)
RRFunctionVoid3(CFStringAppendCharacters)
RRFunction3(CFStringCompare)
RRFunction3(CFStringCreateArrayBySeparatingStrings)
RRFunction2(CFStringCreateMutable)
RRFunction5(CFStringCreateWithBytes)
RRFunction6(CFStringCreateWithBytesNoCopy)
RRFunction4(CFStringCreateWithCharactersNoCopy)
RRFunction3(CFStringCreateWithCString)
RRFunction10(CFStringCreateWithFormat) // Hope this is enough arguments...

static CFIndex
RR_CFStringGetBytes(CFStringRef aString, CFRange aRange, CFStringEncoding aEncoding,
                    UInt8 aLossByte, Boolean aIsExternalRepresentation,
                    UInt8* aBuffer, CFIndex aMaxBufLen, CFIndex* aUsedBufLen)
{
  RecordReplayFunction(CFStringGetBytes, CFIndex,
                       aString, aRange, aEncoding, aLossByte, aIsExternalRepresentation,
                       aBuffer, aMaxBufLen, aUsedBufLen);
  events.CheckInput(aMaxBufLen);
  events.CheckInput((aUsedBufLen ? 1 : 0) | (aBuffer ? 2 : 0));
  events.RecordOrReplayValue(&rval);
  if (aUsedBufLen) {
    events.RecordOrReplayValue(aUsedBufLen);
  }
  if (aBuffer) {
    events.RecordOrReplayBytes(aBuffer, aUsedBufLen ? *aUsedBufLen : aMaxBufLen);
  }
  return rval;
}

static void
RR_CFStringGetCharacters(CFStringRef aString, CFRange aRange, UniChar* aBuffer)
{
  RecordReplayFunctionVoid(CFStringGetCharacters, aString, aRange, aBuffer);
  events.CheckInput(aRange.length);
  events.RecordOrReplayBytes(aBuffer, aRange.length * sizeof(UniChar));
}

RRFunction1(CFStringGetIntValue)

static Boolean
RR_CFStringGetCString(CFStringRef aString, char* aBuffer, CFIndex aBufferSize,
                      CFStringEncoding aEncoding)
{
  RecordReplayFunction(CFStringGetCString, Boolean,
                       aString, aBuffer, aBufferSize, aEncoding);
  events.CheckInput(aBufferSize);
  events.RecordOrReplayValue(&rval);
  events.RecordOrReplayBytes(aBuffer, aBufferSize);
  return rval;
}

static const char*
RR_CFStringGetCStringPtr(CFStringRef aString, CFStringEncoding aEncoding)
{
  if (AreThreadEventsPassedThrough()) {
    return OriginalCall(CFStringGetCStringPtr, const char*, aString, aEncoding);
  }
  return nullptr;
}

RRFunction1(CFStringGetLength)
RRFunction2(CFStringGetMaximumSizeForEncoding)
RRFunction2(CFStringHasPrefix)
RRFunction1(CFStringTokenizerAdvanceToNextToken)
RRFunction5(CFStringTokenizerCreate)
RRFunctionTypes1(CFStringTokenizerGetCurrentTokenRange, CFRange, CFStringTokenizerRef)
RRFunction4(CFURLCreateFromFileSystemRepresentation)
RRFunction2(CFURLCreateFromFSRef)
RRFunction3(CFURLCreateWithString)
RRFunction4(CFURLCreateWithFileSystemPath)

static Boolean
RR_CFURLGetFileSystemRepresentation(CFURLRef aUrl, Boolean aResolveAgainstBase,
                                    UInt8* aBuffer, CFIndex aMaxBufLen)
{
  RecordReplayFunction(CFURLGetFileSystemRepresentation, Boolean,
                       aUrl, aResolveAgainstBase, aBuffer, aMaxBufLen);
  events.CheckInput(aMaxBufLen);
  events.RecordOrReplayValue(&rval);
  events.RecordOrReplayBytes(aBuffer, aMaxBufLen);
  return rval;
}

static Boolean
RR_CFURLGetFSRef(CFURLRef aUrl, struct FSRef* aFsRef)
{
  RecordReplayFunction(CFURLGetFSRef, Boolean, aUrl, aFsRef);
  events.RecordOrReplayValue(&rval);
  events.RecordOrReplayBytes(aFsRef, sizeof(FSRef));
  return rval;
}

RRFunction1(CFUUIDCreate)
RRFunction2(CFUUIDCreateString)
RRFunctionTypes1(CFUUIDGetUUIDBytes, CFUUIDBytes, CFUUIDRef)
RRFunctionTypes2(CGAffineTransformConcat, CGAffineTransform, CGAffineTransform, CGAffineTransform)
RRFunction1(CGBitmapContextCreateImage)

struct ContextDataInfo
{
  CGContextRef mContext;
  void* mData;
  size_t mDataSize;

  ContextDataInfo(CGContextRef aContext, void* aData, size_t aDataSize)
    : mContext(aContext), mData(aData), mDataSize(aDataSize)
  {}
};

static StaticInfallibleVector<ContextDataInfo> gContextData;

static CGContextRef
RR_CGBitmapContextCreateWithData(void *aData, size_t aWidth, size_t aHeight,
                                 size_t aBitsPerComponent, size_t aBytesPerRow,
                                 CGColorSpaceRef aSpace, uint32_t aBitmapInfo,
                                 CGBitmapContextReleaseDataCallback aReleaseCallback,
                                 void *aReleaseInfo)
{
  RecordReplayFunction(CGBitmapContextCreateWithData, CGContextRef,
                       aData, aWidth, aHeight, aBitsPerComponent, aBytesPerRow, aSpace,
                       aBitmapInfo, aReleaseCallback, aReleaseInfo);
  events.RecordOrReplayValue(&rval);

  MOZ_RELEASE_ASSERT(Thread::CurrentIsMainThread());
  gContextData.emplaceBack(rval, aData, aHeight * aBytesPerRow);
  return rval;
}

static void
FlushContext(CGContextRef aContext)
{
  for (int i = gContextData.length() - 1; i >= 0; i--) {
    if (aContext == gContextData[i].mContext) {
      RecordReplayBytes(gContextData[i].mData, gContextData[i].mDataSize);
      return;
    }
  }
  MOZ_CRASH();
}

RRFunction1(CGBitmapContextGetBytesPerRow)
RRFunction1(CGBitmapContextGetHeight)
RRFunction1(CGBitmapContextGetWidth)
RRFunction1(CGColorRelease)
RRFunction0(CGColorSpaceCreateDeviceRGB)
RRFunction1(CGColorSpaceCreatePattern)
RRFunction1(CGColorSpaceCopyICCProfile)
RRFunction1(CGColorSpaceRelease)
RRFunctionTypesVoid3(CGContextBeginTransparencyLayerWithRect,
                     CGContextRef, CGRect, CFDictionaryRef)
RRFunction3(CGContextClipToRects)
RRFunctionTypesVoid2(CGContextConcatCTM, CGContextRef, CGAffineTransform)

static void
RR_CGContextDrawImage(CGContextRef aCx, CGRect aRect, CGImageRef aImage)
{
  RecordReplayFunctionVoid(CGContextDrawImage, aCx, aRect, aImage);
  FlushContext(aCx);
}

RRFunctionVoid1(CGContextEndTransparencyLayer)

static void
RR_CGContextFillRect(CGContextRef aCx, CGRect aRect)
{
  RecordReplayFunctionVoid(CGContextFillRect, aCx, aRect);
  FlushContext(aCx);
}

RRFunctionTypes1(CGContextGetClipBoundingBox, CGRect, CGContextRef)
RRFunctionTypes1(CGContextGetCTM, CGAffineTransform, CGContextRef)
RRFunction1(CGContextGetType)
RRFunctionTypes1(CGContextGetUserSpaceToDeviceSpaceTransform, CGAffineTransform, CGContextRef)

static void
RR_CGContextRestoreGState(CGContextRef aCx)
{
  if (!IsReplaying()) {
    Maybe<AutoPassThroughThreadEvents> pt;
    if (!AreThreadEventsPassedThrough()) {
      pt.emplace();
    }
    OriginalCall(CGContextRestoreGState, void, aCx);
  }
}

RRFunctionVoid1(CGContextSaveGState)
RRFunctionVoid2(CGContextSetAllowsFontSubpixelPositioning)
RRFunctionVoid2(CGContextSetAllowsFontSubpixelQuantization)
RRFunctionTypesVoid2(CGContextSetAlpha, CGContextRef, CGFloat)
RRFunctionTypesVoid2(CGContextSetBaseCTM, CGContextRef, CGAffineTransform)
RRFunctionTypesVoid2(CGContextSetCTM, CGContextRef, CGAffineTransform)
RRFunctionTypesVoid3(CGContextSetGrayFillColor, CGContextRef, CGFloat, CGFloat)
RRFunctionTypesVoid5(CGContextSetRGBFillColor, CGContextRef, CGFloat, CGFloat, CGFloat, CGFloat)
RRFunctionVoid2(CGContextSetShouldAntialias)
RRFunctionVoid2(CGContextSetShouldSmoothFonts)
RRFunctionVoid2(CGContextSetShouldSubpixelPositionFonts)
RRFunctionVoid2(CGContextSetShouldSubpixelQuantizeFonts)
RRFunctionVoid2(CGContextSetTextDrawingMode)
RRFunctionTypesVoid2(CGContextSetTextMatrix, CGContextRef, CGAffineTransform)
RRFunctionVoid3(CGContextScaleCTM)
RRFunctionVoid3(CGContextTranslateCTM)

static CGDataProviderRef
RR_CGDataProviderCreateWithData(void* aInfo, const void* aData,
                                size_t aSize, CGDataProviderReleaseDataCallback aReleaseData)
{
  RecordReplayFunction(CGDataProviderCreateWithData, CGDataProviderRef,
                       aInfo, aData, aSize, aReleaseData);
  events.RecordOrReplayValue((size_t*)&rval);
  if (IsReplaying()) {
    // Immediately release the data, since there is no data provider to do it for us.
    aReleaseData(aInfo, aData, aSize);
  }
  return rval;
}

RRFunction1(CGDisplayCopyColorSpace)
RRFunction1(CGDisplayIOServicePort)
RRFunction2(CGEventSourceCounterForEventType)
RRFunction2(CGFontCopyTableForTag)
RRFunction1(CGFontCopyTableTags)
RRFunction1(CGFontCopyVariations)
RRFunction2(CGFontCreateCopyWithVariations)
RRFunction1(CGFontCreateWithDataProvider)
RRFunction1(CGFontCreateWithFontName)
RRFunction1(CGFontCreateWithPlatformFont)
RRFunction1(CGFontGetAscent)
RRFunction1(CGFontGetCapHeight)
RRFunction1(CGFontGetDescent)
RRFunctionTypes1(CGFontGetFontBBox, CGRect, CGFontRef)

static bool
RR_CGFontGetGlyphAdvances(CGFontRef aFont, const CGGlyph* aGlyphs, size_t aCount, int* aAdvances)
{
  RecordReplayFunction(CGFontGetGlyphAdvances, bool, aFont, aGlyphs, aCount, aAdvances);
  events.CheckInput(aCount);
  events.RecordOrReplayValue(&rval);
  events.RecordOrReplayBytes(aAdvances, aCount * sizeof(int));
  return rval;
}

static bool
RR_CGFontGetGlyphBBoxes(CGFontRef aFont, const CGGlyph* aGlyphs, size_t aCount, CGRect* aBoxes)
{
  RecordReplayFunction(CGFontGetGlyphBBoxes, bool, aFont, aGlyphs, aCount, aBoxes);
  events.CheckInput(aCount);
  events.RecordOrReplayValue(&rval);
  events.RecordOrReplayBytes(aBoxes, aCount * sizeof(CGRect));
  return rval;
}

RRFunction4(CGFontGetGlyphPath)
RRFunction1(CGFontGetLeading)
RRFunction1(CGFontGetUnitsPerEm)
RRFunction1(CGFontGetXHeight)
RRFunction1(CGImageGetHeight)
RRFunction1(CGImageGetWidth)
RRFunction1(CGImageRelease)
RRFunction0(CGMainDisplayID)
RRFunctionVoid3(CGPathAddPath)

static void
RR_CGPathApply(CGPathRef aPath, void* aData, CGPathApplierFunction aFunction)
{
  if (AreThreadEventsPassedThrough()) {
    OriginalCall(CGPathApply, void, aPath, aData, aFunction);
  } else {
    RegisterCallbackData(BitwiseCast<void*>(aFunction));
    RegisterCallbackData(aData);
    PassThroughThreadEventsAllowCallbacks([&]() {
        CallbackWrapperData wrapperData(aFunction, aData);
        OriginalCall(CGPathApply, void, aPath, &wrapperData, CGPathApplierFunctionWrapper);
      });
    RemoveCallbackData(aData);
  }
}

RRFunction4(CGPathContainsPoint)
RRFunction0(CGPathCreateMutable)
RRFunctionTypes1(CGPathGetBoundingBox, CGRect, CGPathRef)

static CGRect
RR_CTFontGetBoundingRectsForGlyphs(CTFontRef aFont, CTFontOrientation aOrientation,
                                   const CGGlyph* aGlyphs, CGRect* aRects, CFIndex aCount)
{
  RecordReplayFunction(CTFontGetBoundingRectsForGlyphs, CGRect, aFont, aOrientation,
                       aGlyphs, aRects, aCount);
  events.RecordOrReplayBytes(&rval, sizeof(CGRect));
  events.CheckInput(!!aRects);
  if (aRects) {
    events.CheckInput(aCount);
    events.RecordOrReplayBytes(aRects, aCount * sizeof(CGRect));
  }
  return rval;
}

RRFunctionTypes1(CGPathGetCurrentPoint, CGPoint, CGPathRef)
RRFunction1(CGPathIsEmpty)
RRFunction1(CGSSetDebugOptions)
RRFunctionVoid0(CGSShutdownServerConnections)
RRFunction1(CTFontCopyFamilyName)
RRFunction1(CTFontCopyFontDescriptor)
RRFunction2(CTFontCopyGraphicsFont)
RRFunction3(CTFontCopyTable)
RRFunction1(CTFontCopyVariationAxes)
RRFunctionTypes3(CTFontCreateForString, CTFontRef,
                 CTFontRef, CFStringRef, CFRange)
RRFunction3(CTFontCreatePathForGlyph)
RRFunctionTypes3(CTFontCreateWithFontDescriptor, CTFontRef,
                 CTFontDescriptorRef, CGFloat, CGAffineTransform*)
RRFunctionTypes4(CTFontCreateWithGraphicsFont, CTFontRef,
                 CGFontRef, CGFloat, CGAffineTransform*, CTFontDescriptorRef)
RRFunctionTypes3(CTFontCreateWithName, CTFontRef,
                 CFStringRef, CGFloat, CGAffineTransform*)

static void
RR_CTFontDrawGlyphs(CTFontRef aFont, CGGlyph* aGlyphs, CGPoint* aPositions,
                    size_t aCount, CGContextRef aContext)
{
  RecordReplayFunctionVoid(CTFontDrawGlyphs, aFont, aGlyphs, aPositions, aCount, aContext);
  FlushContext(aContext);
}

RRFunction2(CTFontDescriptorCopyAttribute)
RRFunction2(CTFontDescriptorCreateCopyWithAttributes)
RRFunction2(CTFontDescriptorCreateMatchingFontDescriptors)
RRFunction1(CTFontDescriptorCreateWithAttributes)

static double
RR_CTFontGetAdvancesForGlyphs(CTFontRef aFont, CTFontOrientation aOrientation,
                              const CGGlyph* aGlyphs, CGSize* aAdvances, CFIndex aCount)
{
  RecordReplayFunction(CTFontGetAdvancesForGlyphs, double, aFont, aOrientation,
                       aGlyphs, aAdvances, aCount);
  events.RecordOrReplayBytes(&rval, sizeof(double));
  events.CheckInput(!!aAdvances);
  if (aAdvances) {
    events.CheckInput(aCount);
    events.RecordOrReplayBytes(aAdvances, aCount * sizeof(CGSize));
  }
  return rval;
}

RRFunctionTypes1(CTFontGetAscent, CGFloat, CTFontRef)
RRFunctionTypes1(CTFontGetBoundingBox, CGRect, CTFontRef)
RRFunctionTypes1(CTFontGetCapHeight, CGFloat, CTFontRef)
RRFunctionTypes1(CTFontGetDescent, CGFloat, CTFontRef)
RRFunction1(CTFontGetGlyphCount)

static bool
RR_CTFontGetGlyphsForCharacters(CTFontRef aFont, const UniChar* aCharacters,
                                CGGlyph* aGlyphs, CFIndex aCount)
{
  RecordReplayFunction(CTFontGetGlyphsForCharacters, bool, aFont, aCharacters, aGlyphs, aCount);
  events.RecordOrReplayValue(&rval);
  events.CheckInput(aCount);
  events.RecordOrReplayBytes(aGlyphs, aCount * sizeof(CGGlyph));
  return rval;
}

RRFunctionTypes1(CTFontGetLeading, CGFloat, CTFontRef)
RRFunctionTypes1(CTFontGetSize, CGFloat, CTFontRef)
RRFunction1(CTFontGetSymbolicTraits)
RRFunctionTypes1(CTFontGetUnderlinePosition, CGFloat, CTFontRef)
RRFunctionTypes1(CTFontGetUnderlineThickness, CGFloat, CTFontRef)
RRFunction1(CTFontGetUnitsPerEm)
RRFunctionTypes1(CTFontGetXHeight, CGFloat, CTFontRef)
RRFunction0(CTFontManagerCopyAvailableFontFamilyNames)
RRFunction3(CTFontManagerRegisterFontsForURLs)
RRFunctionVoid2(CTFontManagerSetAutoActivationSetting)
RRFunction1(CTLineCreateWithAttributedString)
RRFunction1(CTLineGetGlyphRuns)
RRFunction1(CTRunGetAttributes)
RRFunction1(CTRunGetGlyphCount)

// Note: We only redirect CTRunGetGlyphsPtr, not CTRunGetGlyphs. The latter may
// be implemented with a loop that jumps back into the code we overwrite with a
// jump, a pattern which ProcessRedirect.cpp does not handle. For the moment,
// Gecko code only calls CTRunGetGlyphs if CTRunGetGlyphsPtr fails, so make
// sure that CTRunGetGlyphsPtr always succeeds when it is being recorded.
static const CGGlyph*
RR_CTRunGetGlyphsPtr(CTRunRef aRun)
{
  RecordReplayFunction(CTRunGetGlyphsPtr, CGGlyph*, aRun);
  size_t numGlyphs;
  if (IsRecording()) {
    AutoPassThroughThreadEvents pt;
    numGlyphs = CTRunGetGlyphCount(aRun);
    if (!rval) {
      rval = NewLeakyArray<CGGlyph>(numGlyphs);
      CTRunGetGlyphs(aRun, CFRangeMake(0, 0), rval);
    }
  }
  events.RecordOrReplayValue(&numGlyphs);
  if (IsReplaying()) {
    rval = NewLeakyArray<CGGlyph>(numGlyphs);
  }
  events.RecordOrReplayBytes(rval, numGlyphs * sizeof(CGGlyph));
  return rval;
}

// Note: The same concerns apply here as for CTRunGetGlyphsPtr.
static const CGPoint*
RR_CTRunGetPositionsPtr(CTRunRef aRun)
{
  RecordReplayFunction(CTRunGetPositionsPtr, CGPoint*, aRun);
  size_t numGlyphs;
  if (IsRecording()) {
    AutoPassThroughThreadEvents pt;
    numGlyphs = CTRunGetGlyphCount(aRun);
    if (!rval) {
      rval = NewLeakyArray<CGPoint>(numGlyphs);
      CTRunGetPositions(aRun, CFRangeMake(0, 0), rval);
    }
  }
  events.RecordOrReplayValue(&numGlyphs);
  if (IsReplaying()) {
    rval = NewLeakyArray<CGPoint>(numGlyphs);
  }
  events.RecordOrReplayBytes(rval, numGlyphs * sizeof(CGPoint));
  return rval;
}

// Note: The same concerns apply here as for CTRunGetGlyphsPtr.
static const CFIndex*
RR_CTRunGetStringIndicesPtr(CTRunRef aRun)
{
  RecordReplayFunction(CTRunGetStringIndicesPtr, CFIndex*, aRun);
  size_t numGlyphs;
  if (IsRecording()) {
    AutoPassThroughThreadEvents pt;
    numGlyphs = CTRunGetGlyphCount(aRun);
    if (!rval) {
      rval = NewLeakyArray<CFIndex>(numGlyphs);
      CTRunGetStringIndices(aRun, CFRangeMake(0, 0), rval);
    }
  }
  events.RecordOrReplayValue(&numGlyphs);
  if (IsReplaying()) {
    rval = NewLeakyArray<CFIndex>(numGlyphs);
  }
  events.RecordOrReplayBytes(rval, numGlyphs * sizeof(CFIndex));
  return rval;
}

RRFunctionTypes1(CTRunGetStringRange, CFRange, CTRunRef)

static double
RR_CTRunGetTypographicBounds(CTRunRef aRun, CFRange aRange,
                             CGFloat* aAscent, CGFloat* aDescent, CGFloat* aLeading)
{
  RecordReplayFunction(CTRunGetTypographicBounds, double,
                       aRun, aRange, aAscent, aDescent, aLeading);
  events.CheckInput((aAscent ? 1 : 0) | (aDescent ? 2 : 0) | (aLeading ? 4 : 0));
  events.RecordOrReplayBytes(&rval, sizeof(double));
  if (aAscent) {
    events.RecordOrReplayBytes(aAscent, sizeof(CGFloat));
  }
  if (aDescent) {
    events.RecordOrReplayBytes(aDescent, sizeof(CGFloat));
  }
  if (aLeading) {
    events.RecordOrReplayBytes(aLeading, sizeof(CGFloat));
  }
  return rval;
}

RRFunctionTypesVoid5(CUIDraw, void*, CGRect, CGContextRef, CFDictionaryRef, CFDictionaryRef*)
RRFunction2(FSCompareFSRefs)

static OSErr
RR_FSGetVolumeInfo(size_t a0, size_t a1, size_t a2, size_t a3, size_t a4,
                   HFSUniStr255* aVolumeName, FSRef* aFsRef)
{
  RecordReplayFunction(FSGetVolumeInfo, OSErr, a0, a1, a2, a3, a4, aVolumeName, aFsRef);
  events.RecordOrReplayValue(&rval);
  events.RecordOrReplayBytes(aVolumeName, sizeof(HFSUniStr255));
  events.RecordOrReplayBytes(aFsRef, sizeof(FSRef));
  return rval;
}

static OSErr
RR_FSFindFolder(size_t a0, size_t a1, size_t a2, FSRef* aFsRef)
{
  RecordReplayFunction(FSFindFolder, OSErr, a0, a1, a2, aFsRef);
  events.RecordOrReplayValue(&rval);
  events.RecordOrReplayBytes(aFsRef, sizeof(FSRef));
  return rval;
}

static OSErr
RR_Gestalt(OSType aSelector, SInt32* aResponse)
{
  RecordReplayFunction(Gestalt, OSErr, aSelector, aResponse);
  events.RecordOrReplayValue(&rval);
  events.RecordOrReplayValue(aResponse);
  return rval;
}

RRFunction1(GetEventClass)
RRFunction0(GetCurrentEventQueue)

static OSErr
RR_GetCurrentProcess(ProcessSerialNumber* aPSN)
{
  RecordReplayFunction(GetCurrentProcess, OSErr, aPSN);
  events.RecordOrReplayValue(&rval);
  events.RecordOrReplayBytes(aPSN, sizeof(*aPSN));
  return rval;
}

RRFunction1(GetEventAttributes)
RRFunction0(GetEventDispatcherTarget)
RRFunction1(GetEventKind)
RRFunction5(HIThemeDrawButton)
RRFunction4(HIThemeDrawFrame)
RRFunction4(HIThemeDrawGroupBox)
RRFunction4(HIThemeDrawGrowBox)
RRFunction4(HIThemeDrawMenuBackground)
RRFunction6(HIThemeDrawMenuItem)
RRFunction5(HIThemeDrawMenuSeparator)
RRFunction4(HIThemeDrawSeparator)
RRFunction4(HIThemeDrawTabPane)
RRFunction4(HIThemeDrawTrack)

static OSStatus
RR_HIThemeGetGrowBoxBounds(const HIPoint* aOrigin, const HIThemeGrowBoxDrawInfo* aDrawInfo,
                           HIRect* aBounds)
{
  RecordReplayFunction(HIThemeGetGrowBoxBounds, OSStatus, aOrigin, aDrawInfo, aBounds);
  events.RecordOrReplayValue(&rval);
  events.RecordOrReplayBytes(aBounds, sizeof(*aBounds));
  return rval;
}

RRFunction4(HIThemeSetFill)
RRFunction5(IORegistryEntrySearchCFProperty)
RRFunction1(LSCopyAllHandlersForURLScheme)

static OSStatus
RR_LSCopyApplicationForMIMEType(CFStringRef aInMIMEType, LSRolesMask aInRoleMask,
                                CFURLRef* aOutAppURL)
{
  RecordReplayFunction(LSCopyApplicationForMIMEType, OSStatus,
                       aInMIMEType, aInRoleMask, aOutAppURL);
  events.CheckInput(!!aOutAppURL);
  events.RecordOrReplayValue(&rval);
  if (aOutAppURL) {
    events.RecordOrReplayValue((size_t*)aOutAppURL);
  }
  return rval;
}

static OSStatus
RR_LSCopyItemAttribute(const FSRef* aInItem, LSRolesMask aInRoles,
                       CFStringRef aInAttributeName, CFTypeRef* aOutValue)
{
  RecordReplayFunction(LSCopyItemAttribute, OSStatus,
                       aInItem, aInRoles, aInAttributeName, aOutValue);
  events.CheckInput(!!aOutValue);
  events.RecordOrReplayValue(&rval);
  if (aOutValue) {
    events.RecordOrReplayValue((size_t*)aOutValue);
  }
  return rval;
}

static OSStatus
RR_LSCopyKindStringForMIMEType(CFStringRef aInMIMEType, CFStringRef* aOutKindString)
{
  RecordReplayFunction(LSCopyKindStringForMIMEType, OSStatus, aInMIMEType, aOutKindString);
  events.CheckInput(!!aOutKindString);
  events.RecordOrReplayValue(&rval);
  if (aOutKindString) {
    events.RecordOrReplayValue((size_t*)aOutKindString);
  }
  return rval;
}

static void
RecordReplayFSRefAndURLRef(Stream& aStream, OSStatus* aOutStatus,
                           FSRef* aOutAppRef, CFURLRef* aOutAppURL)
{
  aStream.CheckInput((aOutAppRef ? 1 : 0) | (aOutAppURL ? 2 : 0));
  aStream.RecordOrReplayValue(aOutStatus);
  if (aOutAppRef) {
    aStream.RecordOrReplayBytes(aOutAppRef, sizeof(FSRef));
  }
  if (aOutAppURL) {
    aStream.RecordOrReplayValue((size_t*)aOutAppURL);
  }
}

static OSStatus
RR_LSGetApplicationForInfo(OSType aInType, OSType aInCreator,
                           CFStringRef aInExtension, LSRolesMask aInRoleMask,
                           FSRef* aOutAppRef, CFURLRef* aOutAppURL)
{
  RecordReplayFunction(LSGetApplicationForInfo, OSStatus,
                       aInType, aInCreator, aInExtension, aInRoleMask, aOutAppRef, aOutAppURL);
  RecordReplayFSRefAndURLRef(events, &rval, aOutAppRef, aOutAppURL);
  return rval;
}

static OSStatus
RR_LSGetApplicationForURL(CFURLRef aInURL, LSRolesMask aInRoleMask,
                          FSRef* aOutAppRef, CFURLRef* aOutAppURL)
{
  RecordReplayFunction(LSGetApplicationForURL, OSStatus,
                       aInURL, aInRoleMask, aOutAppRef, aOutAppURL);
  RecordReplayFSRefAndURLRef(events, &rval, aOutAppRef, aOutAppURL);
  return rval;
}

RRFunction1(NSClassFromString)
RRFunctionTypesVoid1(NSRectFill, CGRect /* Actually NSRect, but they appear to always be the same */)
RRFunction3(NSSearchPathForDirectoriesInDomains)
RRFunction1(NSSetFocusRingStyle)
RRFunction0(NSTemporaryDirectory)
RRFunction1(ReleaseEvent)

static void
RR_OSSpinLockLock(OSSpinLock *lock)
{
  // These spin locks never need to be recorded, but they are used by malloc
  // and can end up calling other recorded functions like mach_absolute_time,
  // so make sure events are passed through here. Note that we don't have to
  // redirect OSSpinLockUnlock, as it doesn't have these issues.
  AutoEnsurePassThroughThreadEventsUseStackPointer pt;
  OriginalCall(OSSpinLockLock, void, lock);
}

RRFunction2(RemoveEventFromQueue)
RRFunction1(RetainEvent)
RRFunction2(SendEventToEventTarget)

// These are not public APIs, but other redirected functions may be aliases for
// these which are dynamically installed on the first call in a way that our
// redirection mechanism doesn't completely account for.
RRFunction2(SLEventSourceCounterForEventType)
RRFunction1(SLDisplayCopyColorSpace)
RRFunction1(SLDisplayIOServicePort)
RRFunction0(SLMainDisplayID)
RRFunction1(SLSSetDenyWindowServerConnections)
RRFunctionVoid0(SLSShutdownServerConnections)

///////////////////////////////////////////////////////////////////////////////
// Redirection generation
///////////////////////////////////////////////////////////////////////////////

#define MAKE_REDIRECTION_ENTRY(aName)    \
  { #aName, nullptr, (uint8_t*) RR_ ##aName },

Redirection gRedirections[] = {
  FOR_EACH_REDIRECTION(MAKE_REDIRECTION_ENTRY)
  { }
};

#undef MAKE_REDIRECTION_ENTRY

///////////////////////////////////////////////////////////////////////////////
// Direct system call API
///////////////////////////////////////////////////////////////////////////////

const char*
SymbolNameRaw(void* aPtr)
{
  Dl_info info;
  return (dladdr(aPtr, &info) && info.dli_sname) ? info.dli_sname : "???";
}

void*
DirectAllocateMemory(void* aAddress, size_t aSize)
{
  void* res = OriginalCall(mmap, void*,
                           aAddress, aSize, PROT_READ | PROT_WRITE | PROT_EXEC,
                           MAP_ANON | MAP_PRIVATE, -1, 0);
  MOZ_RELEASE_ASSERT(res && res != (void*)-1);
  return res;
}

void
DirectDeallocateMemory(void* aAddress, size_t aSize)
{
  ssize_t rv = OriginalCall(munmap, int, aAddress, aSize);
  MOZ_RELEASE_ASSERT(rv >= 0);
}

void
DirectWriteProtectMemory(void* aAddress, size_t aSize, bool aExecutable,
                         bool aIgnoreFailures /* = false */)
{
  ssize_t rv = OriginalCall(mprotect, int, aAddress, aSize,
                            PROT_READ | (aExecutable ? PROT_EXEC : 0));
  MOZ_RELEASE_ASSERT(aIgnoreFailures || rv == 0);
}

void
DirectUnprotectMemory(void* aAddress, size_t aSize, bool aExecutable,
                      bool aIgnoreFailures /* = false */)
{
  ssize_t rv = OriginalCall(mprotect, int, aAddress, aSize,
                            PROT_READ | PROT_WRITE | (aExecutable ? PROT_EXEC : 0));
  MOZ_RELEASE_ASSERT(aIgnoreFailures || rv == 0);
}

// From chromium/src/base/eintr_wrapper.h
#define HANDLE_EINTR(x) ({ \
  typeof(x) __eintr_result__; \
  do { \
    __eintr_result__ = x; \
  } while (__eintr_result__ == -1 && errno == EINTR); \
  __eintr_result__;\
})

void
DirectSeekFile(FileHandle aFd, uint64_t aOffset)
{
  static_assert(sizeof(uint64_t) == sizeof(off_t), "off_t should have 64 bits");
  ssize_t rv = HANDLE_EINTR(OriginalCall(lseek, int, aFd, aOffset, SEEK_SET));
  MOZ_RELEASE_ASSERT(rv >= 0);
}

FileHandle
DirectOpenFile(const char* aFilename, bool aWriting)
{
  int flags = aWriting ? (O_WRONLY | O_CREAT | O_TRUNC) : O_RDONLY;
  int perms = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
  int fd = HANDLE_EINTR(OriginalCall(open, int, aFilename, flags, perms));
  MOZ_RELEASE_ASSERT(fd > 0);
  return fd;
}

void
DirectCloseFile(FileHandle aFd)
{
  ssize_t rv = HANDLE_EINTR(OriginalCall(close, int, aFd));
  MOZ_RELEASE_ASSERT(rv >= 0);
}

void
DirectDeleteFile(const char* aFilename)
{
  ssize_t rv = unlink(aFilename);
  MOZ_RELEASE_ASSERT(rv >= 0 || errno == ENOENT);
}

void
DirectWrite(FileHandle aFd, const void* aData, size_t aSize)
{
  ssize_t rv = HANDLE_EINTR(OriginalCall(write, int, aFd, aData, aSize));
  MOZ_RELEASE_ASSERT((size_t) rv == aSize);
}

void
DirectPrint(const char* aString)
{
  DirectWrite(STDERR_FILENO, aString, strlen(aString));
}

size_t
DirectRead(FileHandle aFd, void* aData, size_t aSize)
{
  // Clear the memory in case it is write protected by the memory snapshot
  // mechanism.
  memset(aData, 0, aSize);
  ssize_t rv = HANDLE_EINTR(OriginalCall(read, int, aFd, aData, aSize));
  MOZ_RELEASE_ASSERT(rv >= 0);
  return (size_t) rv;
}

void
DirectCreatePipe(FileHandle* aWriteFd, FileHandle* aReadFd)
{
  int fds[2];
  ssize_t rv = OriginalCall(pipe, int, fds);
  MOZ_RELEASE_ASSERT(rv >= 0);
  *aWriteFd = fds[1];
  *aReadFd = fds[0];
}

static double gAbsoluteToNanosecondsRate;

void
InitializeCurrentTime()
{
  AbsoluteTime time = { 1000000, 0 };
  Nanoseconds rate = AbsoluteToNanoseconds(time);
  MOZ_RELEASE_ASSERT(!rate.hi);
  gAbsoluteToNanosecondsRate = rate.lo / 1000000.0;
}

double
CurrentTime()
{
  return OriginalCall(mach_absolute_time, int64_t) * gAbsoluteToNanosecondsRate / 1000.0;
}

void
DirectSpawnThread(void (*aFunction)(void*), void* aArgument)
{
  MOZ_RELEASE_ASSERT(IsMiddleman() || AreThreadEventsPassedThrough());

  pthread_attr_t attr;
  int rv = pthread_attr_init(&attr);
  MOZ_RELEASE_ASSERT(rv == 0);

  rv = pthread_attr_setstacksize(&attr, 2 * 1024 * 1024);
  MOZ_RELEASE_ASSERT(rv == 0);

  rv = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
  MOZ_RELEASE_ASSERT(rv == 0);

  pthread_t pthread;
  rv = OriginalCall(pthread_create, int, &pthread, &attr, (void* (*)(void*)) aFunction, aArgument);
  MOZ_RELEASE_ASSERT(rv == 0);

  rv = pthread_attr_destroy(&attr);
  MOZ_RELEASE_ASSERT(rv == 0);
}

} // recordreplay
} // mozilla