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
|
#include "common_header.h"
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include <assert.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <ctype.h>
#include <signal.h>
#include <search.h>
#include <sched.h>
#ifdef __linux__
# define HAVE_PERSONALITY
#endif
#ifdef HAVE_PERSONALITY
# include <sys/personality.h>
#endif
#include "structdef.h"
#include "forwarddecl.h"
#include "preimpl.h"
#include "revdb_def.h"
#include "src/rtyper.h"
#include "src/mem.h"
#include "src/threadlocal.h"
#include "src-revdb/revdb_include.h"
#define RDB_SIGNATURE "RevDB:"
#define RDB_VERSION 0x00FF0003
#define WEAKREF_AFTERWARDS_DEAD ((char)0xf2)
#define WEAKREF_AFTERWARDS_ALIVE ((char)0xeb)
#define ASYNC_FINALIZER_TRIGGER ((int16_t)0xff46)
#define ASYNC_THREAD_SWITCH ((int16_t)0xff54)
#define FID_REGULAR_MODE 'R'
#define FID_SAVED_STATE 'S'
#define FID_POTENTIAL_IO 'I'
typedef struct {
Signed version;
uint64_t main_thread_id;
uint64_t reserved2;
void *ptr1, *ptr2;
int reserved3;
int argc;
char **argv;
} rdb_header_t;
rpy_revdb_t rpy_revdb;
static char rpy_rev_buffer[16384]; /* max. 32768 */
int rpy_rev_fileno = -1;
static char flag_io_disabled = FID_REGULAR_MODE;
__thread int rpy_active_thread;
static int *rpy_active_thread_ptr;
static void setup_record_mode(int argc, char *argv[]);
static void setup_replay_mode(int *argc_p, char **argv_p[]);
static void check_at_end(uint64_t stop_points);
static void ensure_fixed_address_space(char *argv[])
{
#ifdef HAVE_PERSONALITY
int pers = personality(0xffffffff);
if (pers == -1) {
perror("personality");
exit(1);
}
if (!(pers & ADDR_NO_RANDOMIZE)) {
pers |= ADDR_NO_RANDOMIZE;
if (personality(pers) == -1) {
perror("personality");
exit(1);
}
pers = personality(0xffffffff);
if (pers == -1 || !(pers & ADDR_NO_RANDOMIZE)) {
fprintf(stderr, "cannot set ADDR_NO_RANDOMIZE\n");
exit(1);
}
/* After setting this personality(), we need to restart the
current process. It will then reload the libpypy-c.so at a
non-randomized address.
Potentially buggy to use argv[0] here, but good enough I
suppose. For this reason ensure_fixed_address_space() is
not called when running manually without any REVDB
environment variable set.
*/
execv(argv[0], argv);
perror("execv");
exit(1);
}
#endif
}
RPY_EXTERN
void rpy_reverse_db_setup(int *argc_p, char **argv_p[])
{
/* init-time setup */
int replay_asked = (*argc_p >= 2 && !strcmp((*argv_p)[1],"--revdb-replay"));
#ifdef RPY_RDB_DYNAMIC_REPLAY
RPY_RDB_REPLAY = replay_asked;
#else
if (replay_asked != RPY_RDB_REPLAY) {
fprintf(stderr, "This executable was only compiled for %s mode.\n",
RPY_RDB_REPLAY ? "replay" : "record");
exit(1);
}
#endif
if (RPY_RDB_REPLAY)
setup_replay_mode(argc_p, argv_p);
else
setup_record_mode(*argc_p, *argv_p);
}
static void reverse_db_lock_and_flush(void)
{
_RPY_REVDB_LOCK();
rpy_reverse_db_flush();
_RPY_REVDB_UNLOCK();
}
RPY_EXTERN
void rpy_reverse_db_teardown(void)
{
uint64_t stop_points;
if (!RPY_RDB_REPLAY) {
_RPY_REVDB_LOCK();
}
else {
/* hack: prevents RPY_REVDB_EMIT() from calling
rpy_reverse_db_fetch(), which has nothing more to fetch now */
rpy_revdb.buf_limit += 1;
}
_RPY_REVDB_EMIT_L(stop_points = rpy_revdb.stop_point_seen;,
uint64_t _e, stop_points);
if (!RPY_RDB_REPLAY) {
rpy_reverse_db_flush();
if (rpy_rev_fileno >= 0) {
close(rpy_rev_fileno);
rpy_rev_fileno = -1;
}
_RPY_REVDB_UNLOCK();
}
else
check_at_end(stop_points);
}
static void record_stop_point(void);
static void replay_stop_point(void);
static long current_place;
RPY_EXTERN
void rpy_reverse_db_stop_point(long place)
{
if (!RPY_RDB_REPLAY)
record_stop_point();
else {
current_place = place;
replay_stop_point();
}
}
/* ------------------------------------------------------------ */
/* Recording mode */
/* ------------------------------------------------------------ */
static void write_all(const void *buf, ssize_t count)
{
while (count > 0) {
ssize_t wsize = write(rpy_rev_fileno, buf, count);
if (wsize <= 0) {
if (wsize == 0)
fprintf(stderr, "writing to RevDB file: "
"unexpected non-blocking mode\n");
else
fprintf(stderr, "Fatal error: writing to RevDB file: %m\n");
abort();
}
buf += wsize;
count -= wsize;
}
}
static void close_revdb_fileno_in_fork_child(void)
{
if (rpy_rev_fileno >= 0) {
close(rpy_rev_fileno);
rpy_rev_fileno = -1;
}
}
static void setup_record_mode(int argc, char *argv[])
{
char *filename = getenv("REVDB");
rdb_header_t h;
int i;
assert(RPY_RDB_REPLAY == 0);
if (filename && *filename) {
ensure_fixed_address_space(argv);
putenv("REVDB=");
rpy_rev_fileno = open(filename, O_RDWR | O_CLOEXEC |
O_CREAT | O_NOCTTY | O_TRUNC, 0600);
if (rpy_rev_fileno < 0) {
fprintf(stderr, "Fatal error: can't create REVDB file '%s'\n",
filename);
abort();
}
atexit(reverse_db_lock_and_flush);
write_all(RDB_SIGNATURE, strlen(RDB_SIGNATURE));
for (i = 0; i < argc; i++) {
write_all("\t", 1);
write_all(argv[i], strlen(argv[i]));
}
write_all("\n\0", 2);
memset(&h, 0, sizeof(h));
h.version = RDB_VERSION;
h.ptr1 = &rpy_reverse_db_stop_point;
h.ptr2 = &rpy_revdb;
h.argc = argc;
h.argv = argv;
h.main_thread_id = (uint64_t)pthread_self();
write_all((const char *)&h, sizeof(h));
/* write the whole content of rpy_rdb_struct */
/*write_all((const char *)&rpy_rdb_struct, sizeof(rpy_rdb_struct));*/
fprintf(stderr, "PID %d: recording revdb log to '%s'\n",
(int)getpid(), filename);
}
else {
fprintf(stderr, "PID %d starting, log file disabled "
"(use REVDB=logfile)\n", (int)getpid());
}
if (rpy_rev_fileno >= 0)
rpy_revdb.buf_p = rpy_rev_buffer + sizeof(int16_t);
else
rpy_revdb.buf_p = NULL;
rpy_revdb.buf_limit = rpy_rev_buffer + sizeof(rpy_rev_buffer) - 32;
rpy_revdb.unique_id_seen = 1;
rpy_active_thread = 1;
rpy_active_thread_ptr = &rpy_active_thread;
pthread_atfork(NULL, NULL, close_revdb_fileno_in_fork_child);
}
static void flush_buffer(void)
{
/* must be called with the lock held */
ssize_t full_size;
assert(rpy_revdb.lock);
if (rpy_revdb.buf_p == NULL)
return;
assert(rpy_rev_fileno >= 0);
/* write the current buffer content to the OS */
full_size = rpy_revdb.buf_p - rpy_rev_buffer;
rpy_revdb.buf_p = rpy_rev_buffer + sizeof(int16_t);
write_all(rpy_rev_buffer, full_size);
}
static ssize_t current_packet_size(void)
{
/* must be called with the lock held */
if (rpy_revdb.buf_p != NULL)
return rpy_revdb.buf_p - (rpy_rev_buffer + sizeof(int16_t));
else
return 0;
}
RPY_EXTERN
void rpy_reverse_db_flush(void)
{
/* must be called with the lock held */
ssize_t content_size;
assert(rpy_revdb.lock);
content_size = current_packet_size();
if (content_size != 0) {
char *p = rpy_rev_buffer;
assert(0 < content_size && content_size <= 32767);
*(int16_t *)p = content_size;
flush_buffer();
}
}
void boehm_gc_finalizer_notifier(void)
{
/* This is called by Boehm when there are pending finalizers.
They are only invoked when we call GC_invoke_finalizers(),
which we only do at stop points in the case of revdb.
*/
assert(!RPY_RDB_REPLAY);
assert(rpy_revdb.stop_point_break <= rpy_revdb.stop_point_seen + 1);
rpy_revdb.stop_point_break = rpy_revdb.stop_point_seen + 1;
}
static void fq_trigger(void)
{
int i = 0;
while (boehm_fq_trigger[i])
boehm_fq_trigger[i++]();
}
static long in_invoke_finalizers;
static void emit_async_block(int async_code, uint64_t content)
{
/* must be called with the lock held */
char *p = rpy_rev_buffer;
assert(rpy_revdb.lock);
rpy_reverse_db_flush();
assert(current_packet_size() == 0);
if (rpy_rev_fileno < 0)
return;
/* should not be here from the middle of a @c_only function */
assert(rpy_revdb.buf_p != NULL);
*(int16_t *)p = async_code;
memcpy(rpy_revdb.buf_p, &content, sizeof(uint64_t));
rpy_revdb.buf_p += sizeof(uint64_t);
flush_buffer();
}
RPY_EXTERN
void rpy_reverse_db_lock_acquire(bool_t lock_contention)
{
uint64_t pself;
assert(!RPY_RDB_REPLAY);
while (lock_contention) {
if (rpy_revdb.lock == 0) {
if (pypy_lock_test_and_set(&rpy_revdb.lock, 1) == 0)
break; /* done */
}
sched_yield();
}
/* we have acquired the lock here */
*rpy_active_thread_ptr = 0;
rpy_active_thread = 1;
rpy_active_thread_ptr = &rpy_active_thread;
pself = (uint64_t)pthread_self();
emit_async_block(ASYNC_THREAD_SWITCH, pself);
_RPY_REVDB_PRINT("[THRD]", pself);
}
static void record_stop_point(void)
{
/* ===== FINALIZERS =====
When the GC wants to invoke some finalizers, it causes this to
be called at the stop point. (This is not called at *every*
stop point.) The new-style finalizers are only enqueued at
this point. The old-style finalizers run immediately,
conceptually just *after* the stop point.
*/
int i;
int64_t done;
/* Write an ASYNC_FINALIZER_TRIGGER packet */
_RPY_REVDB_LOCK();
emit_async_block(ASYNC_FINALIZER_TRIGGER, rpy_revdb.stop_point_seen);
_RPY_REVDB_UNLOCK();
/* Invoke all Boehm finalizers. For new-style finalizers, this
will only cause them to move to the queues, where
boehm_fq_next_dead() will be able to fetch them later. For
old-style finalizers, this will really call the finalizers,
which first emit to the rdb log the uid of the object. So
after we do that any number of times, we emit the uid -1 to
mean "now done, continue with the rest of the program".
*/
in_invoke_finalizers++;
GC_invoke_finalizers();
in_invoke_finalizers--;
RPY_REVDB_EMIT(done = -1;, int64_t _e, done);
/* Now we're back in normal mode. We trigger the finalizer
queues here. */
fq_trigger();
}
RPY_EXTERN
void rpy_reverse_db_call_destructor(void *obj)
{
/* old-style finalizers. Should occur only from the
GC_invoke_finalizers() call above.
*/
int64_t uid;
if (RPY_RDB_REPLAY)
return;
if (!in_invoke_finalizers) {
fprintf(stderr, "call_destructor: called at an unexpected time\n");
exit(1);
}
RPY_REVDB_EMIT(uid = ((struct pypy_header0 *)obj)->h_uid;, int64_t _e, uid);
}
RPY_EXTERN
Signed rpy_reverse_db_identityhash(struct pypy_header0 *obj)
{
/* Boehm only */
if (obj->h_hash == 0) {
/* We never need to record anything: if h_hash is zero (which
is the case for all newly allocated objects), then we just
copy h_uid. This gives a stable answer. This would give
0 for all prebuilt objects, but these should not have a
null h_hash anyway.
*/
obj->h_hash = obj->h_uid;
}
return obj->h_hash;
}
RPY_EXTERN
Signed rpy_reverse_db_cast_ptr_to_int(struct pypy_header0 *obj)
{
/* Returns the "id" of the object. Should return a unique number
among all objects, including prebuilt ones.
*/
if (obj->h_uid == 0) {
/* prebuilt object: the object address is good enough, because
such addresses should not change between recording and
replaying. The address is even and non-null, so the
following formula gives a unique negative result.
*/
return (Signed)(-(((Unsigned)obj) >> 1));
}
else {
/* regular object: for now, may fail to work on 32-bit, where
h_uid is a 64-bit number that may grow bigger than 31 bits
if the program runs for long enough. Print a warning if it
is the case. */
if (((Signed)(obj->h_uid)) != obj->h_uid) {
static int warning_printed = 0;
if (!warning_printed) {
fprintf(stderr, "WARNING: the program executes for long enough "
"that it creates more than 2**31 objects. In "
"this situation, the id() function may return "
"non-unique results.\n");
warning_printed = 1;
}
}
/* on 64-bit, the range is large enough anyway, so shift 3 times
to get a result that is a multiple of 8. The Boehm translation
will additionally return the 'int_invert' of that. So
we add 7 to make the user-level id() be a multiple of 8. */
if (sizeof(obj->h_uid) == sizeof(Signed))
return (obj->h_uid << 3) + 7;
else
return (Signed)(obj->h_uid);
}
}
static uint64_t recording_offset(void)
{
/* must be called with the lock held */
off_t base_offset;
ssize_t extra_size = rpy_revdb.buf_p - rpy_rev_buffer;
assert(rpy_revdb.lock);
if (rpy_rev_fileno < 0)
return 1;
/* should not be here from the middle of a @c_only function */
assert(rpy_revdb.buf_p != NULL);
base_offset = lseek(rpy_rev_fileno, 0, SEEK_CUR);
if (base_offset < 0) {
perror("lseek");
exit(1);
}
return base_offset + extra_size;
}
static void patch_prev_offset(int64_t offset, char old, char new)
{
/* must be called with the lock held */
off_t base_offset;
assert(rpy_revdb.lock);
if (rpy_rev_fileno < 0)
return;
/* should not be here from the middle of a @c_only function */
assert(rpy_revdb.buf_p != NULL);
base_offset = lseek(rpy_rev_fileno, 0, SEEK_CUR);
if (base_offset < 0) {
perror("lseek");
exit(1);
}
if (offset < base_offset) {
char got;
if (pread(rpy_rev_fileno, &got, 1, offset) != 1) {
fprintf(stderr, "can't read log position %lld for checking: %m\n",
(long long)offset);
exit(1);
}
if (got != old) {
fprintf(stderr,
"bad byte at log position %lld (%d instead of %d)\n",
(long long)offset, got, old);
exit(1);
}
if (pwrite(rpy_rev_fileno, &new, 1, offset) != 1) {
fprintf(stderr, "can't patch log position %lld\n",
(long long)offset);
exit(1);
}
}
else {
ssize_t buffered_size = rpy_revdb.buf_p - rpy_rev_buffer;
int64_t buf_ofs = offset - base_offset;
if (buf_ofs >= buffered_size) {
fprintf(stderr, "invalid patch position %lld\n",
(long long)offset);
exit(1);
}
if (rpy_rev_buffer[buf_ofs] != old) {
fprintf(stderr,
"bad byte at log position %lld (%d instead of %d)\n",
(long long)offset, rpy_rev_buffer[buf_ofs], old);
exit(1);
}
rpy_rev_buffer[buf_ofs] = new;
}
}
/* keep in sync with 'REVDB_WEAKLINK' in rpython.memory.gctransform.boehm */
struct WEAKLINK {
void *re_addr;
long long re_off_prev;
};
RPY_EXTERN
void *rpy_reverse_db_weakref_create(void *target)
{
/* see comments in ../test/test_weak.py */
struct WEAKLINK *r;
if (!RPY_RDB_REPLAY)
r = GC_MALLOC_ATOMIC(sizeof(struct WEAKLINK));
else
r = GC_MALLOC(sizeof(struct WEAKLINK));
if (!r) {
fprintf(stderr, "out of memory for a weakref\n");
exit(1);
}
r->re_addr = target;
r->re_off_prev = 0;
if (flag_io_disabled == FID_REGULAR_MODE) {
char alive;
/* Emit WEAKREF_AFTERWARDS_DEAD, but remember where we emit it.
If we deref the weakref and it is still alive, we will patch
it with WEAKREF_AFTERWARDS_ALIVE. */
if (!RPY_RDB_REPLAY) {
_RPY_REVDB_LOCK();
r->re_off_prev = recording_offset();
}
else
r->re_off_prev = 1; /* any number > 0 */
_RPY_REVDB_EMIT_L(alive = WEAKREF_AFTERWARDS_DEAD;, char _e, alive);
if (!RPY_RDB_REPLAY) {
_RPY_REVDB_UNLOCK();
OP_BOEHM_DISAPPEARING_LINK(&r->re_addr, target, /*nothing*/);
}
else {
/* replaying: we don't make the weakref actually weak at all,
but instead we always know if we're going to need the
weakref value later or not */
switch (alive) {
case WEAKREF_AFTERWARDS_DEAD:
r->re_addr = NULL;
break;
case WEAKREF_AFTERWARDS_ALIVE:
break;
default:
fprintf(stderr, "bad weakref_create byte in log\n");
exit(1);
}
}
}
return r;
}
RPY_EXTERN
void *rpy_reverse_db_weakref_deref(void *weakref)
{
struct WEAKLINK *r = (struct WEAKLINK *)weakref;
void *result = r->re_addr;
if (result && flag_io_disabled == FID_REGULAR_MODE) {
if (r->re_off_prev < 0) {
fprintf(stderr, "bug in weakrefs: bad previous offset %lld\n",
(long long)r->re_off_prev);
exit(1);
}
if (r->re_off_prev == 0) {
/* A prebuilt weakref. Don't record anything */
}
else {
char alive;
if (!RPY_RDB_REPLAY) {
_RPY_REVDB_LOCK();
patch_prev_offset(r->re_off_prev, WEAKREF_AFTERWARDS_DEAD,
WEAKREF_AFTERWARDS_ALIVE);
r->re_off_prev = recording_offset();
}
_RPY_REVDB_EMIT_L(alive = WEAKREF_AFTERWARDS_DEAD;, char _e, alive);
if (!RPY_RDB_REPLAY) {
_RPY_REVDB_UNLOCK();
}
else {
switch (alive) {
case WEAKREF_AFTERWARDS_DEAD:
r->re_addr = NULL;
break;
case WEAKREF_AFTERWARDS_ALIVE:
break;
default:
fprintf(stderr, "bad weakref_deref byte in log\n");
exit(1);
}
}
}
}
return result;
}
RPY_EXTERN
void rpy_reverse_db_callback_loc(int locnum)
{
locnum += 300;
assert(locnum < 0xFC00);
if (!RPY_RDB_REPLAY) {
_RPY_REVDB_LOCK();
_RPY_REVDB_EMIT_RECORD_L(unsigned char _e, (locnum >> 8));
_RPY_REVDB_EMIT_RECORD_L(unsigned char _e, (locnum & 0xFF));
_RPY_REVDB_UNLOCK();
}
}
/* ------------------------------------------------------------ */
/* Replaying mode */
/* ------------------------------------------------------------ */
/* How it works: we run the same executable with different flags to
run it in "replay" mode. In this mode, it reads commands from
stdin (in binary format) and writes the results to stdout.
Notably, there is a command to ask it to fork, passing a new pair
of pipes to the forked copy as its new stdin/stdout. This is how
we implement the illusion of going backward: we throw away the
current fork, start from an earlier fork, make a new fork again,
and go forward by the correct number of steps. This is all
controlled by a pure Python wrapper that is roughly generic
(i.e. able to act as a debugger for any language).
*/
#include "src-revdb/fd_recv.c"
#include "src/stacklet/stacklet.c" /* for replaying threads */
#define INIT_VERSION_NUMBER 0xd80100
#define CMD_FORK (-1)
#define CMD_QUIT (-2)
#define CMD_FORWARD (-3)
#define CMD_FUTUREIDS (-4)
#define CMD_PING (-5)
#define ANSWER_INIT (-20)
#define ANSWER_READY (-21)
#define ANSWER_FORKED (-22)
#define ANSWER_AT_END (-23)
#define ANSWER_BREAKPOINT (-24)
#define ANSWER_ATTEMPT_IO (-25)
#define RECORD_BKPT_NUM 50
typedef void (*rpy_revdb_command_fn)(rpy_revdb_command_t *, RPyString *);
static int rpy_rev_sockfd;
static const char *rpy_rev_filename;
static uint64_t interactive_break = 1, finalizer_break = -1, uid_break = -1;
static uint64_t total_stop_points;
static void (*pending_after_forward)(void);
static RPyString *empty_string;
static uint64_t last_recorded_breakpoint_loc;
static int n_last_recorded_breakpoints;
static int last_recorded_breakpoint_nums[RECORD_BKPT_NUM];
static char breakpoint_mode = 'i';
static uint64_t *future_ids, *future_next_id;
static void *finalizer_tree, *destructor_tree;
static stacklet_thread_handle st_thread;
static stacklet_handle st_outer_controller_h;
static uint64_t current_thread_id, target_thread_id;
static uint64_t current_thread_num, next_thread_num, break_thread_num;
static void *thread_tree_root;
struct replay_thread_main_s {
Signed (*entry_point)(Signed, char **);
int argc;
char **argv;
};
struct replay_thread_s {
uint64_t tid, tnum;
stacklet_handle h;
struct pypy_threadlocal_s tloc;
};
static stacklet_handle replay_thread_main(stacklet_handle h, void *arg)
{
/* main thread starts */
struct replay_thread_main_s *m = arg;
st_outer_controller_h = h;
m->entry_point(m->argc, m->argv);
/* main thread finished, program stops */
rpy_reverse_db_teardown();
/* unreachable */
abort();
}
static void replay_invoke_callback(unsigned char e);
static stacklet_handle replay_thread_sub(stacklet_handle h, void *ignored)
{
/* A non-main thread starts. What is does is invoke a "callback",
which is the argument passed to rthread.ll_start_new_thread().
We get it here because the first thing stored in the log about
this thread should be a callback identifier.
*/
unsigned char e1;
st_outer_controller_h = h;
if (rpy_revdb.buf_limit >= rpy_revdb.buf_p)
rpy_reverse_db_fetch(__FILE__, __LINE__);
_RPY_REVDB_EMIT_REPLAY(unsigned char _e, e1)
replay_invoke_callback(e1);
/* the thread finishes here. Return to the outer controller. */
return st_outer_controller_h;
}
static int compare_replay_thread(const void *a, const void *b)
{
uint64_t ta = ((const struct replay_thread_s *)a)->tid;
uint64_t tb = ((const struct replay_thread_s *)b)->tid;
if (ta < tb)
return -1;
if (ta == tb)
return 0;
else
return 1;
}
static void set_current_thread_num(uint64_t tnum)
{
if (break_thread_num == current_thread_num || break_thread_num == tnum)
rpy_reverse_db_breakpoint(-4);
current_thread_num = tnum;
}
RPY_EXTERN
int rpy_reverse_db_main(Signed entry_point(Signed, char**),
int argc, char **argv)
{
if (!RPY_RDB_REPLAY) {
int exitcode = (int)entry_point(argc, argv);
rpy_reverse_db_teardown();
return exitcode;
}
else {
/* start the entry point inside a new stacklet, so that we
can switch it away at any point later */
struct replay_thread_main_s m;
stacklet_handle h;
struct pypy_threadlocal_s *real_tloc = NULL;
m.entry_point = entry_point;
m.argc = argc;
m.argv = argv;
h = stacklet_new(st_thread, replay_thread_main, &m);
/* We reach this point only if we start a second thread. This
is done by revdb_switch_thread(), which switches back to
'st_outer_controller_h'. This is the outer controller
loop.
*/
while (1) {
struct replay_thread_s *node, **item, dummy;
if (real_tloc == NULL) {
char *p;
_OP_THREADLOCALREF_ADDR_SIGHANDLER(p);
real_tloc = (struct pypy_threadlocal_s *)p;
}
if (h == NULL)
goto out_of_memory;
if (h != EMPTY_STACKLET_HANDLE) {
/* save 'h' as the stacklet handle for the thread
'current_thread_id' */
node = malloc(sizeof(struct replay_thread_s));
if (!node)
goto out_of_memory;
node->tid = current_thread_id;
node->tnum = current_thread_num;
node->h = h;
/* save the thread-locals, if any */
if (real_tloc != NULL)
node->tloc = *real_tloc;
else
memset(&node->tloc, 0, sizeof(node->tloc));
item = tsearch(node, &thread_tree_root, compare_replay_thread);
if (item == NULL)
goto out_of_memory;
if (*item != node) {
fprintf(stderr, "thread switch: duplicate thread\n");
exit(1);
}
}
else {
/* current_thread_id terminated */
}
/* fetch out (and delete) the handle for the target thread */
current_thread_id = target_thread_id;
dummy.tid = target_thread_id;
item = tfind(&dummy, &thread_tree_root, compare_replay_thread);
if (item == NULL) {
/* it's a new thread, start it now */
set_current_thread_num(next_thread_num++);
if (real_tloc != NULL)
memset(((char *)real_tloc) + RPY_TLOFSFIRST, 0,
sizeof(struct pypy_threadlocal_s) - RPY_TLOFSFIRST);
h = stacklet_new(st_thread, replay_thread_sub, NULL);
}
else {
node = *item;
assert(node->tid == target_thread_id);
set_current_thread_num(node->tnum);
h = node->h;
tdelete(node, &thread_tree_root, compare_replay_thread);
if (real_tloc != NULL)
*real_tloc = node->tloc;
free(node);
h = stacklet_switch(h);
}
}
abort(); /* unreachable */
out_of_memory:
fprintf(stderr, "thread switch: out of memory\n");
exit(1);
}
}
RPY_EXTERN
void attach_gdb(void)
{
char cmdline[80];
sprintf(cmdline, "term -c \"gdb --pid=%d\"", getpid());
system(cmdline);
sleep(1);
}
static ssize_t read_at_least(void *buf, ssize_t count_min, ssize_t count_max)
{
ssize_t result = 0;
assert(count_min <= count_max);
while (result < count_min) {
ssize_t rsize = read(rpy_rev_fileno, buf + result, count_max - result);
if (rsize <= 0) {
if (rsize == 0)
fprintf(stderr, "RevDB file appears truncated (cannot read "
"more after offset %lld)\n",
(long long)lseek(rpy_rev_fileno, 0, SEEK_CUR));
else
fprintf(stderr, "RevDB file read error: %m\n");
exit(1);
}
result += rsize;
}
return result;
}
static void read_all(void *buf, ssize_t count)
{
(void)read_at_least(buf, count, count);
}
static void read_sock(void *buf, ssize_t count)
{
while (count > 0) {
ssize_t got = read(rpy_rev_sockfd, buf, count);
if (got <= 0) {
fprintf(stderr, "subprocess: cannot read from control socket\n");
exit(1);
}
count -= got;
buf += got;
}
}
static void write_sock(const void *buf, ssize_t count)
{
while (count > 0) {
ssize_t wrote = write(rpy_rev_sockfd, buf, count);
if (wrote <= 0) {
fprintf(stderr, "subprocess: cannot write to control socket\n");
exit(1);
}
count -= wrote;
buf += wrote;
}
}
static void write_answer(int cmd, int64_t arg1, int64_t arg2, int64_t arg3)
{
rpy_revdb_command_t c;
memset(&c, 0, sizeof(c));
c.cmd = cmd;
c.arg1 = arg1;
c.arg2 = arg2;
c.arg3 = arg3;
write_sock(&c, sizeof(c));
}
static RPyString *make_rpy_string(size_t length)
{
RPyString *s = malloc(sizeof(RPyString) + length);
if (s == NULL) {
fprintf(stderr, "out of memory for a string of %llu chars\n",
(unsigned long long)length);
exit(1);
}
/* xxx assumes Boehm here for now */
memset(s, 0, sizeof(RPyString));
RPyString_Size(s) = length;
return s;
}
static void reopen_revdb_file(const char *filename)
{
rpy_rev_fileno = open(filename, O_RDONLY | O_NOCTTY);
if (rpy_rev_fileno < 0) {
fprintf(stderr, "Can't open file '%s': %m\n", filename);
exit(1);
}
}
static void set_revdb_breakpoints(void)
{
/* note: these are uint64_t, so '-1' is bigger than positive values */
rpy_revdb.stop_point_break = (interactive_break < finalizer_break ?
interactive_break : finalizer_break);
rpy_revdb.unique_id_break = uid_break;
rpy_revdb.watch_enabled = (breakpoint_mode != 'i');
}
static void setup_replay_mode(int *argc_p, char **argv_p[])
{
int argc = *argc_p;
char **argv = *argv_p;
rdb_header_t h;
char input[16];
ssize_t count;
if (argc != 4) {
fprintf(stderr, "syntax: %s --revdb-replay <RevDB-file> <socket_fd>\n",
argv[0]);
exit(2);
}
ensure_fixed_address_space(*argv_p);
rpy_rev_filename = argv[2];
reopen_revdb_file(rpy_rev_filename);
rpy_rev_sockfd = atoi(argv[3]);
assert(RPY_RDB_REPLAY == 1);
read_all(input, strlen(RDB_SIGNATURE));
if (strncmp(input, RDB_SIGNATURE, strlen(RDB_SIGNATURE)) != 0) {
fprintf(stderr, "'%s' is not a RevDB file (or wrong platform)\n",
rpy_rev_filename);
exit(1);
}
fprintf(stderr, "%s", RDB_SIGNATURE);
while ((read_all(input, 1), input[0] != 0))
fputc(input[0] == '\t' ? ' ' : input[0], stderr);
read_all(&h, sizeof(h));
if (h.version != RDB_VERSION) {
fprintf(stderr, "RevDB file version mismatch (got %lx, expected %lx)\n",
(long)h.version, (long)RDB_VERSION);
exit(1);
}
current_thread_id = h.main_thread_id;
current_thread_num = 0;
next_thread_num = 1;
break_thread_num = (uint64_t)-1;
if (h.ptr1 != &rpy_reverse_db_stop_point ||
h.ptr2 != &rpy_revdb) {
fprintf(stderr,
"\n"
"In the replaying process, the addresses are different than\n"
"in the recording process. Make sure that the executable\n"
"\n"
" %s\n"
"\n"
"is the same one as the one that was used during recording.\n"
"If it is, then you may be hitting an issue with Address\n"
"Space Layout Randomization. On Linux, ASLR should be\n"
"automatically disabled, but just in case, the following\n"
"command disables it manually:\n"
"\n"
" echo 0 | sudo tee /proc/sys/kernel/randomize_va_space\n"
"\n"
"It has been reported that on Linux kernel 4.12.4-1-ARCH,\n"
"ASLR cannot be disabled at all for libpypy-c.so. It works\n"
"again in kernel 4.19 (and maybe sooner). Either change\n"
"kernels, or translate with --no-shared (but then you loose\n"
"PyPy's cpyext ability).\n"
"\n", argv[0]);
exit(1);
}
*argc_p = h.argc;
*argv_p = h.argv;
count = lseek(rpy_rev_fileno, 0, SEEK_CUR);
if (count < 0 ||
lseek(rpy_rev_fileno, -(off_t)sizeof(uint64_t), SEEK_END) < 0 ||
(read_all(&total_stop_points, sizeof(uint64_t)),
lseek(rpy_rev_fileno, count, SEEK_SET)) != count) {
fprintf(stderr, "%s: invalid total_stop_points (%m)\n",
rpy_rev_filename);
exit(1);
}
/* read the whole content of rpy_rdb_struct */
/*read_all((char *)&rpy_rdb_struct, sizeof(rpy_rdb_struct));*/
rpy_revdb.buf_p = rpy_rev_buffer;
rpy_revdb.buf_limit = rpy_rev_buffer;
rpy_revdb.buf_readend = rpy_rev_buffer;
rpy_revdb.stop_point_seen = 0;
rpy_revdb.unique_id_seen = 1;
set_revdb_breakpoints();
empty_string = make_rpy_string(0);
st_thread = stacklet_newthread(); /* replaying doesn't use real threads */
write_answer(ANSWER_INIT, INIT_VERSION_NUMBER, total_stop_points, 0);
/* ignore the SIGCHLD signals so that child processes don't become
zombies */
signal(SIGCHLD, SIG_IGN);
/* initiate the read, which is always at least one byte ahead of
RPY_REVDB_EMIT() in order to detect the ASYNC_* operations
early enough. */
rpy_reverse_db_fetch(__FILE__, __LINE__);
}
static void fetch_more(ssize_t keep, ssize_t expected_size)
{
ssize_t rsize;
if (rpy_revdb.buf_p != rpy_rev_buffer)
memmove(rpy_rev_buffer, rpy_revdb.buf_p, keep);
rsize = read_at_least(rpy_rev_buffer + keep,
expected_size - keep,
sizeof(rpy_rev_buffer) - keep);
rpy_revdb.buf_p = rpy_rev_buffer;
rpy_revdb.buf_readend = rpy_rev_buffer + keep + rsize;
/* rpy_revdb.buf_limit is not set */
}
static uint64_t fetch_async_block(void)
{
ssize_t full_packet_size = sizeof(int16_t) + sizeof(int64_t);
ssize_t keep = rpy_revdb.buf_readend - rpy_revdb.buf_p;
uint64_t result;
if (keep < full_packet_size)
fetch_more(keep, full_packet_size);
memcpy(&result, rpy_revdb.buf_p + sizeof(int16_t), sizeof(int64_t));
rpy_revdb.buf_p += full_packet_size;
return result;
}
RPY_EXTERN
void rpy_reverse_db_fetch(const char *file, int line)
{
if (flag_io_disabled == FID_REGULAR_MODE) {
ssize_t keep;
ssize_t full_packet_size;
int16_t header;
if (finalizer_break != (uint64_t)-1) {
fprintf(stderr, "reverse_db_fetch: finalizer_break != -1\n");
exit(1);
}
if (rpy_revdb.buf_limit != rpy_revdb.buf_p) {
fprintf(stderr, "bad log format: incomplete packet\n");
exit(1);
}
keep = rpy_revdb.buf_readend - rpy_revdb.buf_p;
assert(keep >= 0);
if (keep < sizeof(int16_t)) {
/* 'keep' does not even contain the next packet header */
fetch_more(keep, sizeof(int16_t));
keep = rpy_revdb.buf_readend - rpy_rev_buffer;
}
header = *(int16_t *)rpy_revdb.buf_p;
if (header < 0) {
int64_t bp;
switch (header) {
case ASYNC_FINALIZER_TRIGGER:
//fprintf(stderr, "ASYNC_FINALIZER_TRIGGER\n");
if (finalizer_break != (uint64_t)-1) {
fprintf(stderr, "unexpected multiple "
"ASYNC_FINALIZER_TRIGGER\n");
exit(1);
}
bp = fetch_async_block();
if (bp <= rpy_revdb.stop_point_seen) {
fprintf(stderr, "invalid finalizer break point\n");
exit(1);
}
finalizer_break = bp;
set_revdb_breakpoints();
/* Now we should not fetch anything more until we reach
that finalizer_break point. */
rpy_revdb.buf_limit = rpy_revdb.buf_p;
return;
case ASYNC_THREAD_SWITCH:
target_thread_id = fetch_async_block();
_RPY_REVDB_PRINT("[THRD]", target_thread_id);
rpy_revdb.buf_limit = rpy_revdb.buf_p;
if (target_thread_id != current_thread_id) {
st_outer_controller_h = stacklet_switch(
st_outer_controller_h);
}
if (rpy_revdb.buf_limit == rpy_revdb.buf_p)
rpy_reverse_db_fetch(__FILE__, __LINE__);
return;
default:
fprintf(stderr, "bad packet header %d\n", (int)header);
exit(1);
}
}
full_packet_size = sizeof(int16_t) + header;
if (keep < full_packet_size)
fetch_more(keep, full_packet_size);
rpy_revdb.buf_limit = rpy_revdb.buf_p + full_packet_size;
rpy_revdb.buf_p += sizeof(int16_t);
}
else {
/* this is called when we are in execute_rpy_command(): we are
running some custom code now, and we can't just perform I/O
or access raw memory---because there is no raw memory!
*/
fprintf(stderr, "%s:%d: Attempted to do I/O or access raw memory\n",
file, line);
if (flag_io_disabled != FID_POTENTIAL_IO) {
fprintf(stderr, "but we are not in a protected section\n");
exit(1);
}
write_answer(ANSWER_ATTEMPT_IO, 0, 0, 0);
exit(0);
}
}
static rpy_revdb_t saved_state;
static void *saved_exc[2];
static void change_flag_io_disabled(char oldval, char newval)
{
if (flag_io_disabled != oldval) {
fprintf(stderr, "change_flag_io_disabled(%c, %c) but got %c\n",
oldval, newval, flag_io_disabled);
exit(1);
}
flag_io_disabled = newval;
}
static void save_state(void)
{
/* The program is switching from replaying execution to
time-paused mode. In time-paused mode, we can run more
app-level code like watch points or interactive prints,
but they must not be matched against the log, and they must
not involve generic I/O.
*/
change_flag_io_disabled(FID_REGULAR_MODE, FID_SAVED_STATE);
saved_state = rpy_revdb; /* save the complete struct */
rpy_revdb.unique_id_seen = (-1ULL) << 63;
rpy_revdb.watch_enabled = 0;
rpy_revdb.stop_point_break = -1;
rpy_revdb.unique_id_break = -1;
rpy_revdb.buf_p = rpy_rev_buffer; /* anything readable */
rpy_revdb.buf_limit = rpy_rev_buffer; /* same as buf_p */
}
static void restore_state(void)
{
/* The program is switching from time-paused mode to replaying
execution. */
change_flag_io_disabled(FID_SAVED_STATE, FID_REGULAR_MODE);
/* restore the complete struct */
rpy_revdb = saved_state;
/* set the breakpoint fields to the current value of the *_break
global variables, which may be different from what is in
'save_state' */
set_revdb_breakpoints();
}
static void protect_potential_io(void)
{
change_flag_io_disabled(FID_SAVED_STATE, FID_POTENTIAL_IO);
saved_exc[0] = pypy_g_ExcData.ed_exc_type;
saved_exc[1] = pypy_g_ExcData.ed_exc_value;
pypy_g_ExcData.ed_exc_type = NULL;
pypy_g_ExcData.ed_exc_value = NULL;
}
static void unprotect_potential_io(void)
{
change_flag_io_disabled(FID_POTENTIAL_IO, FID_SAVED_STATE);
if (pypy_g_ExcData.ed_exc_type != NULL) {
fprintf(stderr, "Command crashed with %.*s\n",
(int)(pypy_g_ExcData.ed_exc_type->ov_name->rs_chars.length),
pypy_g_ExcData.ed_exc_type->ov_name->rs_chars.items);
write_answer(ANSWER_ATTEMPT_IO, 1, 0, 0);
exit(0);
}
pypy_g_ExcData.ed_exc_type = saved_exc[0];
pypy_g_ExcData.ed_exc_value = saved_exc[1];
}
static void execute_rpy_function(rpy_revdb_command_fn func,
rpy_revdb_command_t *cmd,
RPyString *extra)
{
protect_potential_io();
func(cmd, extra);
unprotect_potential_io();
}
static void check_at_end(uint64_t stop_points)
{
char dummy[1];
if (rpy_revdb.buf_p != rpy_revdb.buf_limit - 1 ||
read(rpy_rev_fileno, dummy, 1) > 0) {
fprintf(stderr, "RevDB file error: too much data: corrupted file, "
"revdb bug, or non-deterministic run, e.g. a "
"watchpoint with side effects)\n");
exit(1);
}
if (stop_points != rpy_revdb.stop_point_seen) {
fprintf(stderr, "Bad number of stop points "
"(seen %lld, recorded %lld)\n",
(long long)rpy_revdb.stop_point_seen,
(long long)stop_points);
exit(1);
}
if (stop_points != total_stop_points) {
fprintf(stderr, "RevDB file modified while reading?\n");
exit(1);
}
write_answer(ANSWER_AT_END, 0, 0, 0);
exit(0);
}
static void command_fork(int activate)
{
int child_sockfd;
int child_pid;
off_t rev_offset = lseek(rpy_rev_fileno, 0, SEEK_CUR);
if (ancil_recv_fd(rpy_rev_sockfd, &child_sockfd) < 0) {
fprintf(stderr, "cannot fetch child control socket: %m\n");
exit(1);
}
child_pid = fork();
if (child_pid == -1) {
perror("fork");
exit(1);
}
if (child_pid == 0) {
/* in the child */
if (close(rpy_rev_sockfd) < 0) {
perror("close");
exit(1);
}
rpy_rev_sockfd = child_sockfd;
/* The 'activate' flag of CMD_FORK tells if the child process
must die or not when receiving SIGINT. Active children
die; inactive children (stored in 'pgroup.paused') don't. */
signal(SIGINT, activate ? SIG_DFL : SIG_IGN);
/* Close and re-open the revdb log file in the child process.
This is the simplest way I found to give 'rpy_rev_fileno'
its own offset, independent from the parent. It assumes
that the revdb log file is still the same. So for Linux,
we try to open "/proc/self/fd/%d" instead. */
char fd_filename[48];
struct stat st;
const char *filename;
int old_fd = rpy_rev_fileno;
sprintf(fd_filename, "/proc/self/fd/%d", old_fd);
if (lstat(fd_filename, &st) == 0)
filename = fd_filename;
else
filename = rpy_rev_filename;
reopen_revdb_file(filename);
if (close(old_fd) < 0) {
perror("close");
exit(1);
}
if (lseek(rpy_rev_fileno, rev_offset, SEEK_SET) < 0) {
perror("lseek");
exit(1);
}
}
else {
/* in the parent */
write_answer(ANSWER_FORKED, child_pid, 0, 0);
close(child_sockfd);
}
}
static void answer_recorded_breakpoint(void)
{
int i;
for (i = 0; i < n_last_recorded_breakpoints; i++)
write_answer(ANSWER_BREAKPOINT, last_recorded_breakpoint_loc,
0, last_recorded_breakpoint_nums[i]);
n_last_recorded_breakpoints = 0;
}
static void command_forward(rpy_revdb_command_t *cmd)
{
if (cmd->arg1 < 0) {
fprintf(stderr, "CMD_FORWARD: negative step\n");
exit(1);
}
assert(flag_io_disabled == FID_SAVED_STATE);
interactive_break = saved_state.stop_point_seen + cmd->arg1;
breakpoint_mode = (char)cmd->arg2;
if (breakpoint_mode == 'r') {
n_last_recorded_breakpoints = 0;
pending_after_forward = &answer_recorded_breakpoint;
}
}
static void command_future_ids(rpy_revdb_command_t *cmd, char *extra)
{
free(future_ids);
if (cmd->extra_size == 0) {
future_ids = NULL;
uid_break = 0;
}
else {
assert(cmd->extra_size % sizeof(uint64_t) == 0);
future_ids = malloc(cmd->extra_size + sizeof(uint64_t));
if (future_ids == NULL) {
fprintf(stderr, "out of memory for a buffer of %llu chars\n",
(unsigned long long)cmd->extra_size);
exit(1);
}
memcpy(future_ids, extra, cmd->extra_size);
future_ids[cmd->extra_size / sizeof(uint64_t)] = 0;
uid_break = *future_ids;
}
future_next_id = future_ids;
}
static void command_default(rpy_revdb_command_t *cmd, char *extra)
{
RPyString *s;
int i;
for (i = 0; rpy_revdb_commands.rp_names[i] != cmd->cmd; i++) {
if (rpy_revdb_commands.rp_names[i] == 0) {
fprintf(stderr, "unknown command %d\n", cmd->cmd);
exit(1);
}
}
if (cmd->extra_size == 0) {
s = empty_string;
}
else {
s = make_rpy_string(cmd->extra_size);
memcpy(_RPyString_AsString(s), extra, cmd->extra_size);
}
execute_rpy_function(rpy_revdb_commands.rp_funcs[i], cmd, s);
}
RPY_EXTERN
void rpy_reverse_db_watch_save_state(void)
{
save_state();
}
RPY_EXTERN
void rpy_reverse_db_watch_restore_state(bool_t any_watch_point)
{
restore_state();
rpy_revdb.watch_enabled = any_watch_point;
}
static void replay_call_destructors(void);
static void replay_stop_point(void)
{
if (finalizer_break != (uint64_t)-1)
replay_call_destructors();
if (rpy_revdb.stop_point_break != interactive_break) {
fprintf(stderr, "mismatch between interactive_break and "
"stop_point_break\n");
exit(1);
}
while (rpy_revdb.stop_point_break == rpy_revdb.stop_point_seen) {
save_state();
if (pending_after_forward) {
void (*fn)(void) = pending_after_forward;
pending_after_forward = NULL;
fn();
}
else {
rpy_revdb_command_t cmd;
write_answer(ANSWER_READY,
saved_state.stop_point_seen,
saved_state.unique_id_seen,
current_thread_num);
read_sock(&cmd, sizeof(cmd));
char extra[cmd.extra_size + 1];
extra[cmd.extra_size] = 0;
if (cmd.extra_size > 0)
read_sock(extra, cmd.extra_size);
switch (cmd.cmd) {
case CMD_FORK:
command_fork(cmd.arg1);
break;
case CMD_QUIT:
exit(0);
break;
case CMD_FORWARD:
command_forward(&cmd);
break;
case CMD_FUTUREIDS:
command_future_ids(&cmd, extra);
break;
case CMD_PING: /* to get only the ANSWER_READY */
break;
default:
command_default(&cmd, extra);
break;
}
}
restore_state();
}
}
RPY_EXTERN
void rpy_reverse_db_send_answer(int cmd, int64_t arg1, int64_t arg2,
int64_t arg3, RPyString *extra)
{
rpy_revdb_command_t c;
size_t extra_size = RPyString_Size(extra);
c.cmd = cmd;
c.extra_size = extra_size;
if (c.extra_size != extra_size) {
fprintf(stderr, "string too large (more than 4GB)\n");
exit(1);
}
c.arg1 = arg1;
c.arg2 = arg2;
c.arg3 = arg3;
write_sock(&c, sizeof(c));
if (extra_size > 0)
write_sock(_RPyString_AsString(extra), extra_size);
}
RPY_EXTERN
void rpy_reverse_db_breakpoint(int64_t num)
{
if (flag_io_disabled != FID_REGULAR_MODE) {
/* called from a debug command, ignore */
return;
}
switch (breakpoint_mode) {
case 'i':
return; /* ignored breakpoints */
case 'r': /* record the breakpoint but continue */
if (last_recorded_breakpoint_loc != rpy_revdb.stop_point_seen) {
last_recorded_breakpoint_loc = rpy_revdb.stop_point_seen;
n_last_recorded_breakpoints = 0;
}
if (n_last_recorded_breakpoints < RECORD_BKPT_NUM) {
last_recorded_breakpoint_nums[n_last_recorded_breakpoints] = num;
n_last_recorded_breakpoints++;
}
return;
case 'b': /* default handling of breakpoints */
interactive_break = rpy_revdb.stop_point_seen + 1;
set_revdb_breakpoints();
write_answer(ANSWER_BREAKPOINT, rpy_revdb.stop_point_break, 0, num);
return;
default:
fprintf(stderr, "bad value %d of breakpoint_mode\n",
(int)breakpoint_mode);
exit(1);
}
}
RPY_EXTERN
long long rpy_reverse_db_get_value(char value_id)
{
switch (value_id) {
case 'c': /* current_time() */
return (flag_io_disabled == FID_REGULAR_MODE ?
rpy_revdb.stop_point_seen :
saved_state.stop_point_seen);
case 't': /* total_time() */
return total_stop_points;
case 'b': /* current_break_time() */
return interactive_break;
case 'u': /* currently_created_objects() */
return (flag_io_disabled == FID_REGULAR_MODE ?
rpy_revdb.unique_id_seen :
saved_state.unique_id_seen);
case 'p': /* current_place() */
return current_place;
case 'i': /* flag_io_disabled() */
return flag_io_disabled;
default:
return -1;
}
}
RPY_EXTERN
uint64_t rpy_reverse_db_unique_id_break(void *new_object)
{
uint64_t uid = rpy_revdb.unique_id_seen;
bool_t watch_enabled = rpy_revdb.watch_enabled;
if (!new_object) {
fprintf(stderr, "out of memory: allocation failed, cannot continue\n");
exit(1);
}
save_state();
if (rpy_revdb_commands.rp_alloc) {
protect_potential_io();
/* invoke the "ALLOCATING" callback from RPython */
rpy_revdb_commands.rp_alloc(uid, new_object);
unprotect_potential_io();
}
uid_break = *++future_next_id;
restore_state();
rpy_revdb.watch_enabled = watch_enabled;
return uid;
}
struct destructor_s {
void *d_obj;
void (*d_callback)(void *);
};
static int _ftree_compare(const void *obj1, const void *obj2)
{
const struct destructor_s *d1 = obj1;
const struct destructor_s *d2 = obj2;
struct pypy_header0 *h1 = d1->d_obj;
struct pypy_header0 *h2 = d2->d_obj;
if (h1->h_uid < h2->h_uid)
return -1;
if (h1->h_uid == h2->h_uid)
return 0;
else
return 1;
}
static void _ftree_add(void **tree, void *obj, void (*callback)(void *))
{
/* Note: we always allocate an indirection through a
struct destructor_s, so that Boehm knows that 'obj' must be
kept alive. */
struct destructor_s *node, **item;
node = GC_MALLOC_UNCOLLECTABLE(sizeof(struct destructor_s));
node->d_obj = obj;
node->d_callback = callback;
item = tsearch(node, tree, _ftree_compare);
if (item == NULL) {
fprintf(stderr, "_ftree_add: out of memory\n");
exit(1);
}
if (*item != node) {
fprintf(stderr, "_ftree_add: duplicate object\n");
exit(1);
}
}
static struct pypy_header0 *_ftree_pop(void **tree, uint64_t uid,
void (**callback_out)(void *))
{
struct destructor_s d_dummy, *entry, **item;
struct pypy_header0 o_dummy, *result;
d_dummy.d_obj = &o_dummy;
o_dummy.h_uid = uid;
item = tfind(&d_dummy, tree, _ftree_compare);
if (item == NULL) {
fprintf(stderr, "_ftree_pop: object not found\n");
exit(1);
}
entry = *item;
result = entry->d_obj;
if (callback_out)
*callback_out = entry->d_callback;
assert(result->h_uid == uid);
tdelete(entry, tree, _ftree_compare);
GC_FREE(entry);
return result;
}
RPY_EXTERN
int rpy_reverse_db_fq_register(void *obj)
{
/*fprintf(stderr, "FINALIZER_TREE: %lld -> %p\n",
((struct pypy_header0 *)obj)->h_uid, obj);*/
if (!RPY_RDB_REPLAY) {
return 0; /* recording */
}
else {
/* add the object into the finalizer_tree, keyed by the h_uid. */
_ftree_add(&finalizer_tree, obj, NULL);
return 1; /* replaying */
}
}
RPY_EXTERN
void *rpy_reverse_db_next_dead(void *result)
{
int64_t uid;
RPY_REVDB_EMIT(uid = result ? ((struct pypy_header0 *)result)->h_uid : -1;,
int64_t _e, uid);
/*fprintf(stderr, "next_dead: object %lld\n", uid);*/
if (RPY_RDB_REPLAY) {
if (uid == -1) {
result = NULL;
}
else {
/* fetch and remove the object from the finalizer_tree */
result = _ftree_pop(&finalizer_tree, uid, NULL);
}
}
return result;
}
RPY_EXTERN
void rpy_reverse_db_register_destructor(void *obj, void (*callback)(void *))
{
if (!RPY_RDB_REPLAY) {
GC_REGISTER_FINALIZER(obj, (GC_finalization_proc)callback,
NULL, NULL, NULL);
}
else {
_ftree_add(&destructor_tree, obj, callback);
}
}
static void replay_call_destructors(void)
{
/* Re-enable fetching (disabled when we saw ASYNC_FINALIZER_TRIGGER),
and fetch the uid's of dying objects with old-style destructors.
*/
finalizer_break = -1;
set_revdb_breakpoints();
rpy_reverse_db_fetch(__FILE__, __LINE__);
while (1) {
int64_t uid;
struct pypy_header0 *obj;
void (*callback)(void *);
RPY_REVDB_EMIT(abort();, int64_t _e, uid);
if (uid == -1)
break;
obj = _ftree_pop(&destructor_tree, uid, &callback);
callback(obj);
}
/* Now we're back in normal mode. We trigger the finalizer
queues here. */
fq_trigger();
}
static void *callbacklocs[] = {
RPY_CALLBACKLOCS /* macro from revdb_def.h */
};
static void replay_invoke_callback(unsigned char e)
{
unsigned long index;
unsigned char e2;
void (*pfn)(void);
_RPY_REVDB_EMIT_REPLAY(unsigned char _e, e2)
index = (e << 8) | e2;
index -= 300;
if (index >= (sizeof(callbacklocs) / sizeof(callbacklocs[0]))) {
fprintf(stderr, "bad callback index %lx\n", index);
exit(1);
}
pfn = callbacklocs[index];
pfn();
}
RPY_EXTERN
void rpy_reverse_db_invoke_callback(unsigned char e)
{
/* Replaying: we have read the byte which follows calls, expecting
to see 0xFC, but we saw something else. It's part of a two-bytes
callback identifier. */
do {
replay_invoke_callback(e);
_RPY_REVDB_EMIT_REPLAY(unsigned char _e, e)
} while (e != 0xFC);
}
RPY_EXTERN
void rpy_reverse_db_bad_acquire_gil(const char *name)
{
fprintf(stderr, "out of sync: unexpected byte in log "
" (at %s_gil)\n", name);
exit(1);
}
RPY_EXTERN
void rpy_reverse_db_set_thread_breakpoint(int64_t tnum)
{
break_thread_num = (uint64_t)tnum;
}
#define INVALID_STRTOD (-3.46739514239368e+113)
RPY_EXTERN
double rpy_reverse_db_strtod(RPyString *s)
{
/* approximate hacks only */
double result;
char *endptr = NULL;
char buffer[8192];
size_t size = RPyString_Size(s);
if (size >= sizeof(buffer))
return INVALID_STRTOD;
memcpy(buffer, _RPyString_AsString(s), size);
buffer[size] = '\0';
result = strtod(buffer, &endptr);
if (endptr == NULL || *endptr != '\0')
return INVALID_STRTOD;
return result;
}
RPY_EXTERN
RPyString *rpy_reverse_db_dtoa(double d)
{
char buffer[128], *p;
RPyString *result;
int size;
size = snprintf(buffer, sizeof(buffer) - 3, "%.17g", d);
if (size < 0)
size = 0;
for (p = buffer; '0' <= *p && *p <= '9'; p++) {
}
if (*p == 0) { /* a pure integer */
buffer[size++] = '.';
buffer[size++] = '0';
}
result = make_rpy_string(size);
memcpy(_RPyString_AsString(result), buffer, size);
return result;
}
#ifdef RPY_USES_RAWREFCOUNT
static void *rawrefcount_tree; /* {pyobj: gcobj} */
struct rawrefcount_link2_s {
void *pyobj;
void *gcobj;
};
static int _rrtree_compare(const void *obj1, const void *obj2)
{
const struct rawrefcount_link2_s *r1 = obj1;
const struct rawrefcount_link2_s *r2 = obj2;
void *p1 = r1->pyobj;
void *p2 = r2->pyobj;
if (p1 < p2)
return -1;
if (p1 == p2)
return 0;
else
return 1;
}
static void _rrtree_add(void *pyobj, void *gcobj)
{
/* Note: we always allocate an indirection through a
struct rawrefcount_link2_s, so that Boehm knows that
'gcobj' must be kept alive. */
struct rawrefcount_link2_s *node, **item;
node = GC_MALLOC_UNCOLLECTABLE(sizeof(struct rawrefcount_link2_s));
node->pyobj = pyobj;
node->gcobj = gcobj;
item = tsearch(node, &rawrefcount_tree, _rrtree_compare);
if (item == NULL) {
fprintf(stderr, "_rrtree_add: out of memory\n");
exit(1);
}
if (*item != node) {
fprintf(stderr, "_rrtree_add: duplicate object\n");
exit(1);
}
}
RPY_EXTERN
void rpy_reverse_db_rawrefcount_create_link_pypy(void *gcobj, void *pyobj)
{
if (!RPY_RDB_REPLAY) {
gc_rawrefcount_create_link_pypy(gcobj, pyobj);
}
else {
_rrtree_add(pyobj, gcobj);
}
}
RPY_EXTERN
void *rpy_reverse_db_rawrefcount_from_obj(void *gcobj)
{
void *r;
RPY_REVDB_EMIT(r = gc_rawrefcount_from_obj(gcobj);, void *_e, r);
return r;
}
RPY_EXTERN
void *rpy_reverse_db_rawrefcount_to_obj(void *pyobj)
{
unsigned char flag;
if (!RPY_RDB_REPLAY) {
void *r = gc_rawrefcount_to_obj(pyobj);
RPY_REVDB_EMIT(flag = 0xEE + !r;, unsigned char _e, flag);
return r;
}
else {
RPY_REVDB_EMIT(abort();, unsigned char _e, flag);
switch (flag) {
case 0xEF:
/* when recording, this call to to_obj() returned NULL */
return NULL;
case 0xEE:
/* when recording, this call to to_obj() didn't return NULL */
break;
default:
fprintf(stderr, "bad byte in rawrefcount_to_obj\n");
exit(1);
}
struct rawrefcount_link2_s **item, dummy;
dummy.pyobj = pyobj;
item = tfind(&dummy, &rawrefcount_tree, _rrtree_compare);
if (item == NULL) {
fprintf(stderr, "rawrefcount_to_obj: not found in tree\n");
exit(1);
}
return (*item)->gcobj;
}
}
RPY_EXTERN
void *rpy_reverse_db_rawrefcount_next_dead(void)
{
unsigned char flag;
if (!RPY_RDB_REPLAY) {
void *r = gc_rawrefcount_next_dead();
RPY_REVDB_EMIT(flag = 0xEC + !r;, unsigned char _e, flag);
if (r) {
RPY_REVDB_EMIT(;, void *_e, r);
}
return r;
}
else {
/* Note: when replaying, the lifetime of the gcobj is a bit
extended when compared with recording. That shouldn't have
a visible effect. More precisely, when replaying,
create_link_pypy() calls _rrtree_add(), which makes a
struct rawrefcount_link2_s, which keeps gcobj alive; and
that structure is only freed here, when we call next_dead()
and return the corresponding pyobj. When recording, the
cause-and-effect relationship is in the opposite direction:
when Boehm frees the gcobj, it causes the pyobj to show up
(sometimes later) in a next_dead() call.
*/
RPY_REVDB_EMIT(abort();, unsigned char _e, flag);
switch (flag) {
case 0xED:
/* when recording, this call to next_dead() returned NULL */
return NULL;
case 0xEC:
/* when recording, this call to next_dead() didn't return NULL */
break;
default:
fprintf(stderr, "bad byte in rawrefcount_next_dead\n");
exit(1);
}
void *pyobj;
RPY_REVDB_EMIT(abort();, void *_e, pyobj);
struct rawrefcount_link2_s **item, *entry, dummy;
dummy.pyobj = pyobj;
item = tfind(&dummy, &rawrefcount_tree, _rrtree_compare);
if (item == NULL) {
fprintf(stderr, "rawrefcount_next_dead: not found in tree\n");
exit(1);
}
entry = *item;
tdelete(entry, &rawrefcount_tree, _rrtree_compare);
GC_FREE(entry);
return pyobj;
}
}
#endif /* RPY_USES_RAWREFCOUNT */
/* ------------------------------------------------------------ */
RPY_EXTERN
void seeing_uid(uint64_t uid)
{
}
|