| 1 |
#include "common_header.h" |
| 2 |
#include <stdlib.h> |
| 3 |
#include <stdio.h> |
| 4 |
#include <stddef.h> |
| 5 |
#include <string.h> |
| 6 |
#include <assert.h> |
| 7 |
#include <sys/stat.h> |
| 8 |
#include <sys/types.h> |
| 9 |
#include <fcntl.h> |
| 10 |
#include <unistd.h> |
| 11 |
#include <ctype.h> |
| 12 |
#include <signal.h> |
| 13 |
#include <search.h> |
| 14 |
#include <sched.h> |
| 15 |
|
| 16 |
#ifdef __linux__ |
| 17 |
# define HAVE_PERSONALITY |
| 18 |
#endif |
| 19 |
#ifdef HAVE_PERSONALITY |
| 20 |
# include <sys/personality.h> |
| 21 |
#endif |
| 22 |
|
| 23 |
#include "structdef.h" |
| 24 |
#include "forwarddecl.h" |
| 25 |
#include "preimpl.h" |
| 26 |
#include "revdb_def.h" |
| 27 |
#include "src/rtyper.h" |
| 28 |
#include "src/mem.h" |
| 29 |
#include "src/threadlocal.h" |
| 30 |
#include "src-revdb/revdb_include.h" |
| 31 |
|
| 32 |
#define RDB_SIGNATURE "RevDB:" |
| 33 |
#define RDB_VERSION 0x00FF0003 |
| 34 |
|
| 35 |
#define WEAKREF_AFTERWARDS_DEAD ((char)0xf2) |
| 36 |
#define WEAKREF_AFTERWARDS_ALIVE ((char)0xeb) |
| 37 |
|
| 38 |
#define ASYNC_FINALIZER_TRIGGER ((int16_t)0xff46) |
| 39 |
#define ASYNC_THREAD_SWITCH ((int16_t)0xff54) |
| 40 |
|
| 41 |
#define FID_REGULAR_MODE 'R' |
| 42 |
#define FID_SAVED_STATE 'S' |
| 43 |
#define FID_POTENTIAL_IO 'I' |
| 44 |
|
| 45 |
|
| 46 |
typedef struct { |
| 47 |
Signed version; |
| 48 |
uint64_t main_thread_id; |
| 49 |
uint64_t reserved2; |
| 50 |
void *ptr1, *ptr2; |
| 51 |
int reserved3; |
| 52 |
int argc; |
| 53 |
char **argv; |
| 54 |
} rdb_header_t; |
| 55 |
|
| 56 |
|
| 57 |
rpy_revdb_t rpy_revdb; |
| 58 |
static char rpy_rev_buffer[16384]; |
| 59 |
int rpy_rev_fileno = -1; |
| 60 |
static char flag_io_disabled = FID_REGULAR_MODE; |
| 61 |
__thread int rpy_active_thread; |
| 62 |
static int *rpy_active_thread_ptr; |
| 63 |
|
| 64 |
|
| 65 |
static void setup_record_mode(int argc, char *argv[]); |
| 66 |
static void setup_replay_mode(int *argc_p, char **argv_p[]); |
| 67 |
static void check_at_end(uint64_t stop_points); |
| 68 |
|
| 69 |
static void ensure_fixed_address_space(char *argv[]) |
| 70 |
{ |
| 71 |
#ifdef HAVE_PERSONALITY |
| 72 |
int pers = personality(0xffffffff); |
| 73 |
if (pers == -1) { |
| 74 |
perror("personality"); |
| 75 |
exit(1); |
| 76 |
} |
| 77 |
if (!(pers & ADDR_NO_RANDOMIZE)) { |
| 78 |
pers |= ADDR_NO_RANDOMIZE; |
| 79 |
if (personality(pers) == -1) { |
| 80 |
perror("personality"); |
| 81 |
exit(1); |
| 82 |
} |
| 83 |
pers = personality(0xffffffff); |
| 84 |
if (pers == -1 || !(pers & ADDR_NO_RANDOMIZE)) { |
| 85 |
fprintf(stderr, "cannot set ADDR_NO_RANDOMIZE\n"); |
| 86 |
exit(1); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
execv(argv[0], argv); |
| 98 |
|
| 99 |
perror("execv"); |
| 100 |
exit(1); |
| 101 |
} |
| 102 |
#endif |
| 103 |
} |
| 104 |
|
| 105 |
RPY_EXTERN |
| 106 |
void rpy_reverse_db_setup(int *argc_p, char **argv_p[]) |
| 107 |
{ |
| 108 |
|
| 109 |
|
| 110 |
int replay_asked = (*argc_p >= 2 && !strcmp((*argv_p)[1],"--revdb-replay")); |
| 111 |
|
| 112 |
#ifdef RPY_RDB_DYNAMIC_REPLAY |
| 113 |
RPY_RDB_REPLAY = replay_asked; |
| 114 |
#else |
| 115 |
if (replay_asked != RPY_RDB_REPLAY) { |
| 116 |
fprintf(stderr, "This executable was only compiled for %s mode.\n", |
| 117 |
RPY_RDB_REPLAY ? "replay" : "record"); |
| 118 |
exit(1); |
| 119 |
} |
| 120 |
#endif |
| 121 |
|
| 122 |
if (RPY_RDB_REPLAY) |
| 123 |
setup_replay_mode(argc_p, argv_p); |
| 124 |
else |
| 125 |
setup_record_mode(*argc_p, *argv_p); |
| 126 |
} |
| 127 |
|
| 128 |
static void reverse_db_lock_and_flush(void) |
| 129 |
{ |
| 130 |
_RPY_REVDB_LOCK(); |
| 131 |
rpy_reverse_db_flush(); |
| 132 |
_RPY_REVDB_UNLOCK(); |
| 133 |
} |
| 134 |
|
| 135 |
RPY_EXTERN |
| 136 |
void rpy_reverse_db_teardown(void) |
| 137 |
{ |
| 138 |
uint64_t stop_points; |
| 139 |
if (!RPY_RDB_REPLAY) { |
| 140 |
_RPY_REVDB_LOCK(); |
| 141 |
} |
| 142 |
else { |
| 143 |
|
| 144 |
|
| 145 |
rpy_revdb.buf_limit += 1; |
| 146 |
} |
| 147 |
_RPY_REVDB_EMIT_L(stop_points = rpy_revdb.stop_point_seen;, |
| 148 |
uint64_t _e, stop_points); |
| 149 |
|
| 150 |
if (!RPY_RDB_REPLAY) { |
| 151 |
rpy_reverse_db_flush(); |
| 152 |
if (rpy_rev_fileno >= 0) { |
| 153 |
close(rpy_rev_fileno); |
| 154 |
rpy_rev_fileno = -1; |
| 155 |
} |
| 156 |
_RPY_REVDB_UNLOCK(); |
| 157 |
} |
| 158 |
else |
| 159 |
check_at_end(stop_points); |
| 160 |
} |
| 161 |
|
| 162 |
static void record_stop_point(void); |
| 163 |
static void replay_stop_point(void); |
| 164 |
static long current_place; |
| 165 |
|
| 166 |
RPY_EXTERN |
| 167 |
void rpy_reverse_db_stop_point(long place) |
| 168 |
{ |
| 169 |
if (!RPY_RDB_REPLAY) |
| 170 |
record_stop_point(); |
| 171 |
else { |
| 172 |
current_place = place; |
| 173 |
replay_stop_point(); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
static void write_all(const void *buf, ssize_t count) |
| 184 |
{ |
| 185 |
while (count > 0) { |
| 186 |
ssize_t wsize = write(rpy_rev_fileno, buf, count); |
| 187 |
if (wsize <= 0) { |
| 188 |
if (wsize == 0) |
| 189 |
fprintf(stderr, "writing to RevDB file: " |
| 190 |
"unexpected non-blocking mode\n"); |
| 191 |
else |
| 192 |
fprintf(stderr, "Fatal error: writing to RevDB file: %m\n"); |
| 193 |
abort(); |
| 194 |
} |
| 195 |
buf += wsize; |
| 196 |
count -= wsize; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
static void close_revdb_fileno_in_fork_child(void) |
| 201 |
{ |
| 202 |
if (rpy_rev_fileno >= 0) { |
| 203 |
close(rpy_rev_fileno); |
| 204 |
rpy_rev_fileno = -1; |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
static void setup_record_mode(int argc, char *argv[]) |
| 209 |
{ |
| 210 |
char *filename = getenv("REVDB"); |
| 211 |
rdb_header_t h; |
| 212 |
int i; |
| 213 |
|
| 214 |
assert(RPY_RDB_REPLAY == 0); |
| 215 |
|
| 216 |
if (filename && *filename) { |
| 217 |
ensure_fixed_address_space(argv); |
| 218 |
|
| 219 |
putenv("REVDB="); |
| 220 |
rpy_rev_fileno = open(filename, O_RDWR | O_CLOEXEC | |
| 221 |
O_CREAT | O_NOCTTY | O_TRUNC, 0600); |
| 222 |
if (rpy_rev_fileno < 0) { |
| 223 |
fprintf(stderr, "Fatal error: can't create REVDB file '%s'\n", |
| 224 |
filename); |
| 225 |
abort(); |
| 226 |
} |
| 227 |
atexit(reverse_db_lock_and_flush); |
| 228 |
|
| 229 |
write_all(RDB_SIGNATURE, strlen(RDB_SIGNATURE)); |
| 230 |
for (i = 0; i < argc; i++) { |
| 231 |
write_all("\t", 1); |
| 232 |
write_all(argv[i], strlen(argv[i])); |
| 233 |
} |
| 234 |
write_all("\n\0", 2); |
| 235 |
|
| 236 |
memset(&h, 0, sizeof(h)); |
| 237 |
h.version = RDB_VERSION; |
| 238 |
h.ptr1 = &rpy_reverse_db_stop_point; |
| 239 |
h.ptr2 = &rpy_revdb; |
| 240 |
h.argc = argc; |
| 241 |
h.argv = argv; |
| 242 |
h.main_thread_id = (uint64_t)pthread_self(); |
| 243 |
write_all((const char *)&h, sizeof(h)); |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
fprintf(stderr, "PID %d: recording revdb log to '%s'\n", |
| 249 |
(int)getpid(), filename); |
| 250 |
} |
| 251 |
else { |
| 252 |
fprintf(stderr, "PID %d starting, log file disabled " |
| 253 |
"(use REVDB=logfile)\n", (int)getpid()); |
| 254 |
} |
| 255 |
|
| 256 |
if (rpy_rev_fileno >= 0) |
| 257 |
rpy_revdb.buf_p = rpy_rev_buffer + sizeof(int16_t); |
| 258 |
else |
| 259 |
rpy_revdb.buf_p = NULL; |
| 260 |
rpy_revdb.buf_limit = rpy_rev_buffer + sizeof(rpy_rev_buffer) - 32; |
| 261 |
rpy_revdb.unique_id_seen = 1; |
| 262 |
|
| 263 |
rpy_active_thread = 1; |
| 264 |
rpy_active_thread_ptr = &rpy_active_thread; |
| 265 |
|
| 266 |
pthread_atfork(NULL, NULL, close_revdb_fileno_in_fork_child); |
| 267 |
} |
| 268 |
|
| 269 |
static void flush_buffer(void) |
| 270 |
{ |
| 271 |
|
| 272 |
ssize_t full_size; |
| 273 |
assert(rpy_revdb.lock); |
| 274 |
|
| 275 |
if (rpy_revdb.buf_p == NULL) |
| 276 |
return; |
| 277 |
assert(rpy_rev_fileno >= 0); |
| 278 |
|
| 279 |
|
| 280 |
full_size = rpy_revdb.buf_p - rpy_rev_buffer; |
| 281 |
rpy_revdb.buf_p = rpy_rev_buffer + sizeof(int16_t); |
| 282 |
write_all(rpy_rev_buffer, full_size); |
| 283 |
} |
| 284 |
|
| 285 |
static ssize_t current_packet_size(void) |
| 286 |
{ |
| 287 |
|
| 288 |
if (rpy_revdb.buf_p != NULL) |
| 289 |
return rpy_revdb.buf_p - (rpy_rev_buffer + sizeof(int16_t)); |
| 290 |
else |
| 291 |
return 0; |
| 292 |
} |
| 293 |
|
| 294 |
RPY_EXTERN |
| 295 |
void rpy_reverse_db_flush(void) |
| 296 |
{ |
| 297 |
|
| 298 |
ssize_t content_size; |
| 299 |
assert(rpy_revdb.lock); |
| 300 |
|
| 301 |
content_size = current_packet_size(); |
| 302 |
if (content_size != 0) { |
| 303 |
char *p = rpy_rev_buffer; |
| 304 |
assert(0 < content_size && content_size <= 32767); |
| 305 |
*(int16_t *)p = content_size; |
| 306 |
flush_buffer(); |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
void boehm_gc_finalizer_notifier(void) |
| 311 |
{ |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
assert(!RPY_RDB_REPLAY); |
| 317 |
assert(rpy_revdb.stop_point_break <= rpy_revdb.stop_point_seen + 1); |
| 318 |
rpy_revdb.stop_point_break = rpy_revdb.stop_point_seen + 1; |
| 319 |
} |
| 320 |
|
| 321 |
static void fq_trigger(void) |
| 322 |
{ |
| 323 |
int i = 0; |
| 324 |
while (boehm_fq_trigger[i]) |
| 325 |
boehm_fq_trigger[i++](); |
| 326 |
} |
| 327 |
|
| 328 |
static long in_invoke_finalizers; |
| 329 |
|
| 330 |
static void emit_async_block(int async_code, uint64_t content) |
| 331 |
{ |
| 332 |
|
| 333 |
char *p = rpy_rev_buffer; |
| 334 |
assert(rpy_revdb.lock); |
| 335 |
|
| 336 |
rpy_reverse_db_flush(); |
| 337 |
assert(current_packet_size() == 0); |
| 338 |
|
| 339 |
if (rpy_rev_fileno < 0) |
| 340 |
return; |
| 341 |
|
| 342 |
assert(rpy_revdb.buf_p != NULL); |
| 343 |
|
| 344 |
*(int16_t *)p = async_code; |
| 345 |
memcpy(rpy_revdb.buf_p, &content, sizeof(uint64_t)); |
| 346 |
rpy_revdb.buf_p += sizeof(uint64_t); |
| 347 |
flush_buffer(); |
| 348 |
} |
| 349 |
|
| 350 |
RPY_EXTERN |
| 351 |
void rpy_reverse_db_lock_acquire(bool_t lock_contention) |
| 352 |
{ |
| 353 |
uint64_t pself; |
| 354 |
assert(!RPY_RDB_REPLAY); |
| 355 |
while (lock_contention) { |
| 356 |
if (rpy_revdb.lock == 0) { |
| 357 |
if (pypy_lock_test_and_set(&rpy_revdb.lock, 1) == 0) |
| 358 |
break; |
| 359 |
} |
| 360 |
sched_yield(); |
| 361 |
} |
| 362 |
|
| 363 |
*rpy_active_thread_ptr = 0; |
| 364 |
rpy_active_thread = 1; |
| 365 |
rpy_active_thread_ptr = &rpy_active_thread; |
| 366 |
pself = (uint64_t)pthread_self(); |
| 367 |
emit_async_block(ASYNC_THREAD_SWITCH, pself); |
| 368 |
_RPY_REVDB_PRINT("[THRD]", pself); |
| 369 |
} |
| 370 |
|
| 371 |
static void record_stop_point(void) |
| 372 |
{ |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
int i; |
| 382 |
int64_t done; |
| 383 |
|
| 384 |
|
| 385 |
_RPY_REVDB_LOCK(); |
| 386 |
emit_async_block(ASYNC_FINALIZER_TRIGGER, rpy_revdb.stop_point_seen); |
| 387 |
_RPY_REVDB_UNLOCK(); |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
in_invoke_finalizers++; |
| 398 |
GC_invoke_finalizers(); |
| 399 |
in_invoke_finalizers--; |
| 400 |
RPY_REVDB_EMIT(done = -1;, int64_t _e, done); |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
fq_trigger(); |
| 405 |
} |
| 406 |
|
| 407 |
RPY_EXTERN |
| 408 |
void rpy_reverse_db_call_destructor(void *obj) |
| 409 |
{ |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
int64_t uid; |
| 414 |
|
| 415 |
if (RPY_RDB_REPLAY) |
| 416 |
return; |
| 417 |
if (!in_invoke_finalizers) { |
| 418 |
fprintf(stderr, "call_destructor: called at an unexpected time\n"); |
| 419 |
exit(1); |
| 420 |
} |
| 421 |
RPY_REVDB_EMIT(uid = ((struct pypy_header0 *)obj)->h_uid;, int64_t _e, uid); |
| 422 |
} |
| 423 |
|
| 424 |
RPY_EXTERN |
| 425 |
Signed rpy_reverse_db_identityhash(struct pypy_header0 *obj) |
| 426 |
{ |
| 427 |
|
| 428 |
if (obj->h_hash == 0) { |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
obj->h_hash = obj->h_uid; |
| 436 |
} |
| 437 |
return obj->h_hash; |
| 438 |
} |
| 439 |
|
| 440 |
RPY_EXTERN |
| 441 |
Signed rpy_reverse_db_cast_ptr_to_int(struct pypy_header0 *obj) |
| 442 |
{ |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
if (obj->h_uid == 0) { |
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
return (Signed)(-(((Unsigned)obj) >> 1)); |
| 453 |
} |
| 454 |
else { |
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
if (((Signed)(obj->h_uid)) != obj->h_uid) { |
| 460 |
static int warning_printed = 0; |
| 461 |
if (!warning_printed) { |
| 462 |
fprintf(stderr, "WARNING: the program executes for long enough " |
| 463 |
"that it creates more than 2**31 objects. In " |
| 464 |
"this situation, the id() function may return " |
| 465 |
"non-unique results.\n"); |
| 466 |
warning_printed = 1; |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
if (sizeof(obj->h_uid) == sizeof(Signed)) |
| 474 |
return (obj->h_uid << 3) + 7; |
| 475 |
else |
| 476 |
return (Signed)(obj->h_uid); |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
static uint64_t recording_offset(void) |
| 481 |
{ |
| 482 |
|
| 483 |
off_t base_offset; |
| 484 |
ssize_t extra_size = rpy_revdb.buf_p - rpy_rev_buffer; |
| 485 |
assert(rpy_revdb.lock); |
| 486 |
|
| 487 |
if (rpy_rev_fileno < 0) |
| 488 |
return 1; |
| 489 |
|
| 490 |
assert(rpy_revdb.buf_p != NULL); |
| 491 |
|
| 492 |
base_offset = lseek(rpy_rev_fileno, 0, SEEK_CUR); |
| 493 |
if (base_offset < 0) { |
| 494 |
perror("lseek"); |
| 495 |
exit(1); |
| 496 |
} |
| 497 |
return base_offset + extra_size; |
| 498 |
} |
| 499 |
|
| 500 |
static void patch_prev_offset(int64_t offset, char old, char new) |
| 501 |
{ |
| 502 |
|
| 503 |
off_t base_offset; |
| 504 |
assert(rpy_revdb.lock); |
| 505 |
|
| 506 |
if (rpy_rev_fileno < 0) |
| 507 |
return; |
| 508 |
|
| 509 |
assert(rpy_revdb.buf_p != NULL); |
| 510 |
|
| 511 |
base_offset = lseek(rpy_rev_fileno, 0, SEEK_CUR); |
| 512 |
if (base_offset < 0) { |
| 513 |
perror("lseek"); |
| 514 |
exit(1); |
| 515 |
} |
| 516 |
if (offset < base_offset) { |
| 517 |
char got; |
| 518 |
if (pread(rpy_rev_fileno, &got, 1, offset) != 1) { |
| 519 |
fprintf(stderr, "can't read log position %lld for checking: %m\n", |
| 520 |
(long long)offset); |
| 521 |
exit(1); |
| 522 |
} |
| 523 |
if (got != old) { |
| 524 |
fprintf(stderr, |
| 525 |
"bad byte at log position %lld (%d instead of %d)\n", |
| 526 |
(long long)offset, got, old); |
| 527 |
exit(1); |
| 528 |
} |
| 529 |
if (pwrite(rpy_rev_fileno, &new, 1, offset) != 1) { |
| 530 |
fprintf(stderr, "can't patch log position %lld\n", |
| 531 |
(long long)offset); |
| 532 |
exit(1); |
| 533 |
} |
| 534 |
} |
| 535 |
else { |
| 536 |
ssize_t buffered_size = rpy_revdb.buf_p - rpy_rev_buffer; |
| 537 |
int64_t buf_ofs = offset - base_offset; |
| 538 |
if (buf_ofs >= buffered_size) { |
| 539 |
fprintf(stderr, "invalid patch position %lld\n", |
| 540 |
(long long)offset); |
| 541 |
exit(1); |
| 542 |
} |
| 543 |
if (rpy_rev_buffer[buf_ofs] != old) { |
| 544 |
fprintf(stderr, |
| 545 |
"bad byte at log position %lld (%d instead of %d)\n", |
| 546 |
(long long)offset, rpy_rev_buffer[buf_ofs], old); |
| 547 |
exit(1); |
| 548 |
} |
| 549 |
rpy_rev_buffer[buf_ofs] = new; |
| 550 |
} |
| 551 |
} |
| 552 |
|
| 553 |
|
| 554 |
struct WEAKLINK { |
| 555 |
void *re_addr; |
| 556 |
long long re_off_prev; |
| 557 |
}; |
| 558 |
|
| 559 |
RPY_EXTERN |
| 560 |
void *rpy_reverse_db_weakref_create(void *target) |
| 561 |
{ |
| 562 |
|
| 563 |
struct WEAKLINK *r; |
| 564 |
if (!RPY_RDB_REPLAY) |
| 565 |
r = GC_MALLOC_ATOMIC(sizeof(struct WEAKLINK)); |
| 566 |
else |
| 567 |
r = GC_MALLOC(sizeof(struct WEAKLINK)); |
| 568 |
|
| 569 |
if (!r) { |
| 570 |
fprintf(stderr, "out of memory for a weakref\n"); |
| 571 |
exit(1); |
| 572 |
} |
| 573 |
r->re_addr = target; |
| 574 |
r->re_off_prev = 0; |
| 575 |
|
| 576 |
if (flag_io_disabled == FID_REGULAR_MODE) { |
| 577 |
char alive; |
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
if (!RPY_RDB_REPLAY) { |
| 582 |
_RPY_REVDB_LOCK(); |
| 583 |
r->re_off_prev = recording_offset(); |
| 584 |
} |
| 585 |
else |
| 586 |
r->re_off_prev = 1; |
| 587 |
|
| 588 |
_RPY_REVDB_EMIT_L(alive = WEAKREF_AFTERWARDS_DEAD;, char _e, alive); |
| 589 |
|
| 590 |
if (!RPY_RDB_REPLAY) { |
| 591 |
_RPY_REVDB_UNLOCK(); |
| 592 |
OP_BOEHM_DISAPPEARING_LINK(&r->re_addr, target, ); |
| 593 |
} |
| 594 |
else { |
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
switch (alive) { |
| 599 |
case WEAKREF_AFTERWARDS_DEAD: |
| 600 |
r->re_addr = NULL; |
| 601 |
break; |
| 602 |
case WEAKREF_AFTERWARDS_ALIVE: |
| 603 |
break; |
| 604 |
default: |
| 605 |
fprintf(stderr, "bad weakref_create byte in log\n"); |
| 606 |
exit(1); |
| 607 |
} |
| 608 |
} |
| 609 |
} |
| 610 |
return r; |
| 611 |
} |
| 612 |
|
| 613 |
RPY_EXTERN |
| 614 |
void *rpy_reverse_db_weakref_deref(void *weakref) |
| 615 |
{ |
| 616 |
struct WEAKLINK *r = (struct WEAKLINK *)weakref; |
| 617 |
void *result = r->re_addr; |
| 618 |
if (result && flag_io_disabled == FID_REGULAR_MODE) { |
| 619 |
if (r->re_off_prev < 0) { |
| 620 |
fprintf(stderr, "bug in weakrefs: bad previous offset %lld\n", |
| 621 |
(long long)r->re_off_prev); |
| 622 |
exit(1); |
| 623 |
} |
| 624 |
if (r->re_off_prev == 0) { |
| 625 |
|
| 626 |
} |
| 627 |
else { |
| 628 |
char alive; |
| 629 |
if (!RPY_RDB_REPLAY) { |
| 630 |
_RPY_REVDB_LOCK(); |
| 631 |
patch_prev_offset(r->re_off_prev, WEAKREF_AFTERWARDS_DEAD, |
| 632 |
WEAKREF_AFTERWARDS_ALIVE); |
| 633 |
r->re_off_prev = recording_offset(); |
| 634 |
} |
| 635 |
_RPY_REVDB_EMIT_L(alive = WEAKREF_AFTERWARDS_DEAD;, char _e, alive); |
| 636 |
|
| 637 |
if (!RPY_RDB_REPLAY) { |
| 638 |
_RPY_REVDB_UNLOCK(); |
| 639 |
} |
| 640 |
else { |
| 641 |
switch (alive) { |
| 642 |
case WEAKREF_AFTERWARDS_DEAD: |
| 643 |
r->re_addr = NULL; |
| 644 |
break; |
| 645 |
case WEAKREF_AFTERWARDS_ALIVE: |
| 646 |
break; |
| 647 |
default: |
| 648 |
fprintf(stderr, "bad weakref_deref byte in log\n"); |
| 649 |
exit(1); |
| 650 |
} |
| 651 |
} |
| 652 |
} |
| 653 |
} |
| 654 |
return result; |
| 655 |
} |
| 656 |
|
| 657 |
RPY_EXTERN |
| 658 |
void rpy_reverse_db_callback_loc(int locnum) |
| 659 |
{ |
| 660 |
locnum += 300; |
| 661 |
assert(locnum < 0xFC00); |
| 662 |
if (!RPY_RDB_REPLAY) { |
| 663 |
_RPY_REVDB_LOCK(); |
| 664 |
_RPY_REVDB_EMIT_RECORD_L(unsigned char _e, (locnum >> 8)); |
| 665 |
_RPY_REVDB_EMIT_RECORD_L(unsigned char _e, (locnum & 0xFF)); |
| 666 |
_RPY_REVDB_UNLOCK(); |
| 667 |
} |
| 668 |
} |
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
#include "src-revdb/fd_recv.c" |
| 689 |
#include "src/stacklet/stacklet.c" |
| 690 |
|
| 691 |
#define INIT_VERSION_NUMBER 0xd80100 |
| 692 |
|
| 693 |
#define CMD_FORK (-1) |
| 694 |
#define CMD_QUIT (-2) |
| 695 |
#define CMD_FORWARD (-3) |
| 696 |
#define CMD_FUTUREIDS (-4) |
| 697 |
#define CMD_PING (-5) |
| 698 |
|
| 699 |
#define ANSWER_INIT (-20) |
| 700 |
#define ANSWER_READY (-21) |
| 701 |
#define ANSWER_FORKED (-22) |
| 702 |
#define ANSWER_AT_END (-23) |
| 703 |
#define ANSWER_BREAKPOINT (-24) |
| 704 |
#define ANSWER_ATTEMPT_IO (-25) |
| 705 |
|
| 706 |
#define RECORD_BKPT_NUM 50 |
| 707 |
|
| 708 |
typedef void (*rpy_revdb_command_fn)(rpy_revdb_command_t *, RPyString *); |
| 709 |
|
| 710 |
static int rpy_rev_sockfd; |
| 711 |
static const char *rpy_rev_filename; |
| 712 |
static uint64_t interactive_break = 1, finalizer_break = -1, uid_break = -1; |
| 713 |
static uint64_t total_stop_points; |
| 714 |
static void (*pending_after_forward)(void); |
| 715 |
static RPyString *empty_string; |
| 716 |
static uint64_t last_recorded_breakpoint_loc; |
| 717 |
static int n_last_recorded_breakpoints; |
| 718 |
static int last_recorded_breakpoint_nums[RECORD_BKPT_NUM]; |
| 719 |
static char breakpoint_mode = 'i'; |
| 720 |
static uint64_t *future_ids, *future_next_id; |
| 721 |
static void *finalizer_tree, *destructor_tree; |
| 722 |
|
| 723 |
static stacklet_thread_handle st_thread; |
| 724 |
static stacklet_handle st_outer_controller_h; |
| 725 |
static uint64_t current_thread_id, target_thread_id; |
| 726 |
static uint64_t current_thread_num, next_thread_num, break_thread_num; |
| 727 |
static void *thread_tree_root; |
| 728 |
|
| 729 |
|
| 730 |
struct replay_thread_main_s { |
| 731 |
Signed (*entry_point)(Signed, char **); |
| 732 |
int argc; |
| 733 |
char **argv; |
| 734 |
}; |
| 735 |
struct replay_thread_s { |
| 736 |
uint64_t tid, tnum; |
| 737 |
stacklet_handle h; |
| 738 |
struct pypy_threadlocal_s tloc; |
| 739 |
}; |
| 740 |
|
| 741 |
static stacklet_handle replay_thread_main(stacklet_handle h, void *arg) |
| 742 |
{ |
| 743 |
|
| 744 |
struct replay_thread_main_s *m = arg; |
| 745 |
st_outer_controller_h = h; |
| 746 |
m->entry_point(m->argc, m->argv); |
| 747 |
|
| 748 |
|
| 749 |
rpy_reverse_db_teardown(); |
| 750 |
|
| 751 |
|
| 752 |
abort(); |
| 753 |
} |
| 754 |
|
| 755 |
static void replay_invoke_callback(unsigned char e); |
| 756 |
|
| 757 |
static stacklet_handle replay_thread_sub(stacklet_handle h, void *ignored) |
| 758 |
{ |
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
unsigned char e1; |
| 765 |
st_outer_controller_h = h; |
| 766 |
|
| 767 |
if (rpy_revdb.buf_limit >= rpy_revdb.buf_p) |
| 768 |
rpy_reverse_db_fetch(__FILE__, __LINE__); |
| 769 |
|
| 770 |
_RPY_REVDB_EMIT_REPLAY(unsigned char _e, e1) |
| 771 |
replay_invoke_callback(e1); |
| 772 |
|
| 773 |
|
| 774 |
return st_outer_controller_h; |
| 775 |
} |
| 776 |
|
| 777 |
static int compare_replay_thread(const void *a, const void *b) |
| 778 |
{ |
| 779 |
uint64_t ta = ((const struct replay_thread_s *)a)->tid; |
| 780 |
uint64_t tb = ((const struct replay_thread_s *)b)->tid; |
| 781 |
if (ta < tb) |
| 782 |
return -1; |
| 783 |
if (ta == tb) |
| 784 |
return 0; |
| 785 |
else |
| 786 |
return 1; |
| 787 |
} |
| 788 |
|
| 789 |
static void set_current_thread_num(uint64_t tnum) |
| 790 |
{ |
| 791 |
if (break_thread_num == current_thread_num || break_thread_num == tnum) |
| 792 |
rpy_reverse_db_breakpoint(-4); |
| 793 |
current_thread_num = tnum; |
| 794 |
} |
| 795 |
|
| 796 |
RPY_EXTERN |
| 797 |
int rpy_reverse_db_main(Signed entry_point(Signed, char**), |
| 798 |
int argc, char **argv) |
| 799 |
{ |
| 800 |
if (!RPY_RDB_REPLAY) { |
| 801 |
int exitcode = (int)entry_point(argc, argv); |
| 802 |
rpy_reverse_db_teardown(); |
| 803 |
return exitcode; |
| 804 |
} |
| 805 |
else { |
| 806 |
|
| 807 |
|
| 808 |
struct replay_thread_main_s m; |
| 809 |
stacklet_handle h; |
| 810 |
struct pypy_threadlocal_s *real_tloc = NULL; |
| 811 |
m.entry_point = entry_point; |
| 812 |
m.argc = argc; |
| 813 |
m.argv = argv; |
| 814 |
h = stacklet_new(st_thread, replay_thread_main, &m); |
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
while (1) { |
| 822 |
struct replay_thread_s *node, **item, dummy; |
| 823 |
|
| 824 |
if (real_tloc == NULL) { |
| 825 |
char *p; |
| 826 |
_OP_THREADLOCALREF_ADDR_SIGHANDLER(p); |
| 827 |
real_tloc = (struct pypy_threadlocal_s *)p; |
| 828 |
} |
| 829 |
|
| 830 |
if (h == NULL) |
| 831 |
goto out_of_memory; |
| 832 |
|
| 833 |
if (h != EMPTY_STACKLET_HANDLE) { |
| 834 |
|
| 835 |
|
| 836 |
node = malloc(sizeof(struct replay_thread_s)); |
| 837 |
if (!node) |
| 838 |
goto out_of_memory; |
| 839 |
node->tid = current_thread_id; |
| 840 |
node->tnum = current_thread_num; |
| 841 |
node->h = h; |
| 842 |
|
| 843 |
if (real_tloc != NULL) |
| 844 |
node->tloc = *real_tloc; |
| 845 |
else |
| 846 |
memset(&node->tloc, 0, sizeof(node->tloc)); |
| 847 |
|
| 848 |
item = tsearch(node, &thread_tree_root, compare_replay_thread); |
| 849 |
if (item == NULL) |
| 850 |
goto out_of_memory; |
| 851 |
|
| 852 |
if (*item != node) { |
| 853 |
fprintf(stderr, "thread switch: duplicate thread\n"); |
| 854 |
exit(1); |
| 855 |
} |
| 856 |
} |
| 857 |
else { |
| 858 |
|
| 859 |
} |
| 860 |
|
| 861 |
|
| 862 |
current_thread_id = target_thread_id; |
| 863 |
dummy.tid = target_thread_id; |
| 864 |
item = tfind(&dummy, &thread_tree_root, compare_replay_thread); |
| 865 |
if (item == NULL) { |
| 866 |
|
| 867 |
set_current_thread_num(next_thread_num++); |
| 868 |
if (real_tloc != NULL) |
| 869 |
memset(((char *)real_tloc) + RPY_TLOFSFIRST, 0, |
| 870 |
sizeof(struct pypy_threadlocal_s) - RPY_TLOFSFIRST); |
| 871 |
h = stacklet_new(st_thread, replay_thread_sub, NULL); |
| 872 |
} |
| 873 |
else { |
| 874 |
node = *item; |
| 875 |
assert(node->tid == target_thread_id); |
| 876 |
set_current_thread_num(node->tnum); |
| 877 |
h = node->h; |
| 878 |
tdelete(node, &thread_tree_root, compare_replay_thread); |
| 879 |
if (real_tloc != NULL) |
| 880 |
*real_tloc = node->tloc; |
| 881 |
free(node); |
| 882 |
|
| 883 |
h = stacklet_switch(h); |
| 884 |
} |
| 885 |
} |
| 886 |
abort(); |
| 887 |
|
| 888 |
out_of_memory: |
| 889 |
fprintf(stderr, "thread switch: out of memory\n"); |
| 890 |
exit(1); |
| 891 |
} |
| 892 |
} |
| 893 |
|
| 894 |
RPY_EXTERN |
| 895 |
void attach_gdb(void) |
| 896 |
{ |
| 897 |
char cmdline[80]; |
| 898 |
sprintf(cmdline, "term -c \"gdb --pid=%d\"", getpid()); |
| 899 |
system(cmdline); |
| 900 |
sleep(1); |
| 901 |
} |
| 902 |
|
| 903 |
static ssize_t read_at_least(void *buf, ssize_t count_min, ssize_t count_max) |
| 904 |
{ |
| 905 |
ssize_t result = 0; |
| 906 |
assert(count_min <= count_max); |
| 907 |
while (result < count_min) { |
| 908 |
ssize_t rsize = read(rpy_rev_fileno, buf + result, count_max - result); |
| 909 |
if (rsize <= 0) { |
| 910 |
if (rsize == 0) |
| 911 |
fprintf(stderr, "RevDB file appears truncated (cannot read " |
| 912 |
"more after offset %lld)\n", |
| 913 |
(long long)lseek(rpy_rev_fileno, 0, SEEK_CUR)); |
| 914 |
else |
| 915 |
fprintf(stderr, "RevDB file read error: %m\n"); |
| 916 |
exit(1); |
| 917 |
} |
| 918 |
result += rsize; |
| 919 |
} |
| 920 |
return result; |
| 921 |
} |
| 922 |
|
| 923 |
static void read_all(void *buf, ssize_t count) |
| 924 |
{ |
| 925 |
(void)read_at_least(buf, count, count); |
| 926 |
} |
| 927 |
|
| 928 |
static void read_sock(void *buf, ssize_t count) |
| 929 |
{ |
| 930 |
while (count > 0) { |
| 931 |
ssize_t got = read(rpy_rev_sockfd, buf, count); |
| 932 |
if (got <= 0) { |
| 933 |
fprintf(stderr, "subprocess: cannot read from control socket\n"); |
| 934 |
exit(1); |
| 935 |
} |
| 936 |
count -= got; |
| 937 |
buf += got; |
| 938 |
} |
| 939 |
} |
| 940 |
|
| 941 |
static void write_sock(const void *buf, ssize_t count) |
| 942 |
{ |
| 943 |
while (count > 0) { |
| 944 |
ssize_t wrote = write(rpy_rev_sockfd, buf, count); |
| 945 |
if (wrote <= 0) { |
| 946 |
fprintf(stderr, "subprocess: cannot write to control socket\n"); |
| 947 |
exit(1); |
| 948 |
} |
| 949 |
count -= wrote; |
| 950 |
buf += wrote; |
| 951 |
} |
| 952 |
} |
| 953 |
|
| 954 |
static void write_answer(int cmd, int64_t arg1, int64_t arg2, int64_t arg3) |
| 955 |
{ |
| 956 |
rpy_revdb_command_t c; |
| 957 |
memset(&c, 0, sizeof(c)); |
| 958 |
c.cmd = cmd; |
| 959 |
c.arg1 = arg1; |
| 960 |
c.arg2 = arg2; |
| 961 |
c.arg3 = arg3; |
| 962 |
write_sock(&c, sizeof(c)); |
| 963 |
} |
| 964 |
|
| 965 |
static RPyString *make_rpy_string(size_t length) |
| 966 |
{ |
| 967 |
RPyString *s = malloc(sizeof(RPyString) + length); |
| 968 |
if (s == NULL) { |
| 969 |
fprintf(stderr, "out of memory for a string of %llu chars\n", |
| 970 |
(unsigned long long)length); |
| 971 |
exit(1); |
| 972 |
} |
| 973 |
|
| 974 |
memset(s, 0, sizeof(RPyString)); |
| 975 |
RPyString_Size(s) = length; |
| 976 |
return s; |
| 977 |
} |
| 978 |
|
| 979 |
static void reopen_revdb_file(const char *filename) |
| 980 |
{ |
| 981 |
rpy_rev_fileno = open(filename, O_RDONLY | O_NOCTTY); |
| 982 |
if (rpy_rev_fileno < 0) { |
| 983 |
fprintf(stderr, "Can't open file '%s': %m\n", filename); |
| 984 |
exit(1); |
| 985 |
} |
| 986 |
} |
| 987 |
|
| 988 |
static void set_revdb_breakpoints(void) |
| 989 |
{ |
| 990 |
|
| 991 |
rpy_revdb.stop_point_break = (interactive_break < finalizer_break ? |
| 992 |
interactive_break : finalizer_break); |
| 993 |
rpy_revdb.unique_id_break = uid_break; |
| 994 |
rpy_revdb.watch_enabled = (breakpoint_mode != 'i'); |
| 995 |
} |
| 996 |
|
| 997 |
static void setup_replay_mode(int *argc_p, char **argv_p[]) |
| 998 |
{ |
| 999 |
int argc = *argc_p; |
| 1000 |
char **argv = *argv_p; |
| 1001 |
rdb_header_t h; |
| 1002 |
char input[16]; |
| 1003 |
ssize_t count; |
| 1004 |
|
| 1005 |
if (argc != 4) { |
| 1006 |
fprintf(stderr, "syntax: %s --revdb-replay <RevDB-file> <socket_fd>\n", |
| 1007 |
argv[0]); |
| 1008 |
exit(2); |
| 1009 |
} |
| 1010 |
ensure_fixed_address_space(*argv_p); |
| 1011 |
|
| 1012 |
rpy_rev_filename = argv[2]; |
| 1013 |
reopen_revdb_file(rpy_rev_filename); |
| 1014 |
rpy_rev_sockfd = atoi(argv[3]); |
| 1015 |
|
| 1016 |
assert(RPY_RDB_REPLAY == 1); |
| 1017 |
|
| 1018 |
read_all(input, strlen(RDB_SIGNATURE)); |
| 1019 |
if (strncmp(input, RDB_SIGNATURE, strlen(RDB_SIGNATURE)) != 0) { |
| 1020 |
fprintf(stderr, "'%s' is not a RevDB file (or wrong platform)\n", |
| 1021 |
rpy_rev_filename); |
| 1022 |
exit(1); |
| 1023 |
} |
| 1024 |
fprintf(stderr, "%s", RDB_SIGNATURE); |
| 1025 |
while ((read_all(input, 1), input[0] != 0)) |
| 1026 |
fputc(input[0] == '\t' ? ' ' : input[0], stderr); |
| 1027 |
|
| 1028 |
read_all(&h, sizeof(h)); |
| 1029 |
if (h.version != RDB_VERSION) { |
| 1030 |
fprintf(stderr, "RevDB file version mismatch (got %lx, expected %lx)\n", |
| 1031 |
(long)h.version, (long)RDB_VERSION); |
| 1032 |
exit(1); |
| 1033 |
} |
| 1034 |
current_thread_id = h.main_thread_id; |
| 1035 |
current_thread_num = 0; |
| 1036 |
next_thread_num = 1; |
| 1037 |
break_thread_num = (uint64_t)-1; |
| 1038 |
if (h.ptr1 != &rpy_reverse_db_stop_point || |
| 1039 |
h.ptr2 != &rpy_revdb) { |
| 1040 |
fprintf(stderr, |
| 1041 |
"\n" |
| 1042 |
"In the replaying process, the addresses are different than\n" |
| 1043 |
"in the recording process. Make sure that the executable\n" |
| 1044 |
"\n" |
| 1045 |
" %s\n" |
| 1046 |
"\n" |
| 1047 |
"is the same one as the one that was used during recording.\n" |
| 1048 |
"If it is, then you may be hitting an issue with Address\n" |
| 1049 |
"Space Layout Randomization. On Linux, ASLR should be\n" |
| 1050 |
"automatically disabled, but just in case, the following\n" |
| 1051 |
"command disables it manually:\n" |
| 1052 |
"\n" |
| 1053 |
" echo 0 | sudo tee /proc/sys/kernel/randomize_va_space\n" |
| 1054 |
"\n" |
| 1055 |
"It has been reported that on Linux kernel 4.12.4-1-ARCH,\n" |
| 1056 |
"ASLR cannot be disabled at all for libpypy-c.so. It works\n" |
| 1057 |
"again in kernel 4.19 (and maybe sooner). Either change\n" |
| 1058 |
"kernels, or translate with --no-shared (but then you loose\n" |
| 1059 |
"PyPy's cpyext ability).\n" |
| 1060 |
"\n", argv[0]); |
| 1061 |
exit(1); |
| 1062 |
} |
| 1063 |
*argc_p = h.argc; |
| 1064 |
*argv_p = h.argv; |
| 1065 |
|
| 1066 |
count = lseek(rpy_rev_fileno, 0, SEEK_CUR); |
| 1067 |
if (count < 0 || |
| 1068 |
lseek(rpy_rev_fileno, -(off_t)sizeof(uint64_t), SEEK_END) < 0 || |
| 1069 |
(read_all(&total_stop_points, sizeof(uint64_t)), |
| 1070 |
lseek(rpy_rev_fileno, count, SEEK_SET)) != count) { |
| 1071 |
fprintf(stderr, "%s: invalid total_stop_points (%m)\n", |
| 1072 |
rpy_rev_filename); |
| 1073 |
exit(1); |
| 1074 |
} |
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
rpy_revdb.buf_p = rpy_rev_buffer; |
| 1080 |
rpy_revdb.buf_limit = rpy_rev_buffer; |
| 1081 |
rpy_revdb.buf_readend = rpy_rev_buffer; |
| 1082 |
rpy_revdb.stop_point_seen = 0; |
| 1083 |
rpy_revdb.unique_id_seen = 1; |
| 1084 |
set_revdb_breakpoints(); |
| 1085 |
|
| 1086 |
empty_string = make_rpy_string(0); |
| 1087 |
st_thread = stacklet_newthread(); |
| 1088 |
|
| 1089 |
write_answer(ANSWER_INIT, INIT_VERSION_NUMBER, total_stop_points, 0); |
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
signal(SIGCHLD, SIG_IGN); |
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
rpy_reverse_db_fetch(__FILE__, __LINE__); |
| 1099 |
} |
| 1100 |
|
| 1101 |
static void fetch_more(ssize_t keep, ssize_t expected_size) |
| 1102 |
{ |
| 1103 |
ssize_t rsize; |
| 1104 |
if (rpy_revdb.buf_p != rpy_rev_buffer) |
| 1105 |
memmove(rpy_rev_buffer, rpy_revdb.buf_p, keep); |
| 1106 |
rsize = read_at_least(rpy_rev_buffer + keep, |
| 1107 |
expected_size - keep, |
| 1108 |
sizeof(rpy_rev_buffer) - keep); |
| 1109 |
rpy_revdb.buf_p = rpy_rev_buffer; |
| 1110 |
rpy_revdb.buf_readend = rpy_rev_buffer + keep + rsize; |
| 1111 |
|
| 1112 |
} |
| 1113 |
|
| 1114 |
static uint64_t fetch_async_block(void) |
| 1115 |
{ |
| 1116 |
ssize_t full_packet_size = sizeof(int16_t) + sizeof(int64_t); |
| 1117 |
ssize_t keep = rpy_revdb.buf_readend - rpy_revdb.buf_p; |
| 1118 |
uint64_t result; |
| 1119 |
if (keep < full_packet_size) |
| 1120 |
fetch_more(keep, full_packet_size); |
| 1121 |
memcpy(&result, rpy_revdb.buf_p + sizeof(int16_t), sizeof(int64_t)); |
| 1122 |
rpy_revdb.buf_p += full_packet_size; |
| 1123 |
return result; |
| 1124 |
} |
| 1125 |
|
| 1126 |
RPY_EXTERN |
| 1127 |
void rpy_reverse_db_fetch(const char *file, int line) |
| 1128 |
{ |
| 1129 |
if (flag_io_disabled == FID_REGULAR_MODE) { |
| 1130 |
ssize_t keep; |
| 1131 |
ssize_t full_packet_size; |
| 1132 |
int16_t header; |
| 1133 |
|
| 1134 |
if (finalizer_break != (uint64_t)-1) { |
| 1135 |
fprintf(stderr, "reverse_db_fetch: finalizer_break != -1\n"); |
| 1136 |
exit(1); |
| 1137 |
} |
| 1138 |
if (rpy_revdb.buf_limit != rpy_revdb.buf_p) { |
| 1139 |
fprintf(stderr, "bad log format: incomplete packet\n"); |
| 1140 |
exit(1); |
| 1141 |
} |
| 1142 |
keep = rpy_revdb.buf_readend - rpy_revdb.buf_p; |
| 1143 |
assert(keep >= 0); |
| 1144 |
|
| 1145 |
if (keep < sizeof(int16_t)) { |
| 1146 |
|
| 1147 |
fetch_more(keep, sizeof(int16_t)); |
| 1148 |
keep = rpy_revdb.buf_readend - rpy_rev_buffer; |
| 1149 |
} |
| 1150 |
header = *(int16_t *)rpy_revdb.buf_p; |
| 1151 |
if (header < 0) { |
| 1152 |
int64_t bp; |
| 1153 |
|
| 1154 |
switch (header) { |
| 1155 |
|
| 1156 |
case ASYNC_FINALIZER_TRIGGER: |
| 1157 |
|
| 1158 |
if (finalizer_break != (uint64_t)-1) { |
| 1159 |
fprintf(stderr, "unexpected multiple " |
| 1160 |
"ASYNC_FINALIZER_TRIGGER\n"); |
| 1161 |
exit(1); |
| 1162 |
} |
| 1163 |
bp = fetch_async_block(); |
| 1164 |
if (bp <= rpy_revdb.stop_point_seen) { |
| 1165 |
fprintf(stderr, "invalid finalizer break point\n"); |
| 1166 |
exit(1); |
| 1167 |
} |
| 1168 |
finalizer_break = bp; |
| 1169 |
set_revdb_breakpoints(); |
| 1170 |
|
| 1171 |
|
| 1172 |
rpy_revdb.buf_limit = rpy_revdb.buf_p; |
| 1173 |
return; |
| 1174 |
|
| 1175 |
case ASYNC_THREAD_SWITCH: |
| 1176 |
target_thread_id = fetch_async_block(); |
| 1177 |
_RPY_REVDB_PRINT("[THRD]", target_thread_id); |
| 1178 |
rpy_revdb.buf_limit = rpy_revdb.buf_p; |
| 1179 |
if (target_thread_id != current_thread_id) { |
| 1180 |
st_outer_controller_h = stacklet_switch( |
| 1181 |
st_outer_controller_h); |
| 1182 |
} |
| 1183 |
if (rpy_revdb.buf_limit == rpy_revdb.buf_p) |
| 1184 |
rpy_reverse_db_fetch(__FILE__, __LINE__); |
| 1185 |
return; |
| 1186 |
|
| 1187 |
default: |
| 1188 |
fprintf(stderr, "bad packet header %d\n", (int)header); |
| 1189 |
exit(1); |
| 1190 |
} |
| 1191 |
} |
| 1192 |
full_packet_size = sizeof(int16_t) + header; |
| 1193 |
if (keep < full_packet_size) |
| 1194 |
fetch_more(keep, full_packet_size); |
| 1195 |
rpy_revdb.buf_limit = rpy_revdb.buf_p + full_packet_size; |
| 1196 |
rpy_revdb.buf_p += sizeof(int16_t); |
| 1197 |
} |
| 1198 |
else { |
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
|
| 1203 |
fprintf(stderr, "%s:%d: Attempted to do I/O or access raw memory\n", |
| 1204 |
file, line); |
| 1205 |
if (flag_io_disabled != FID_POTENTIAL_IO) { |
| 1206 |
fprintf(stderr, "but we are not in a protected section\n"); |
| 1207 |
exit(1); |
| 1208 |
} |
| 1209 |
write_answer(ANSWER_ATTEMPT_IO, 0, 0, 0); |
| 1210 |
exit(0); |
| 1211 |
} |
| 1212 |
} |
| 1213 |
|
| 1214 |
static rpy_revdb_t saved_state; |
| 1215 |
static void *saved_exc[2]; |
| 1216 |
|
| 1217 |
static void change_flag_io_disabled(char oldval, char newval) |
| 1218 |
{ |
| 1219 |
if (flag_io_disabled != oldval) { |
| 1220 |
fprintf(stderr, "change_flag_io_disabled(%c, %c) but got %c\n", |
| 1221 |
oldval, newval, flag_io_disabled); |
| 1222 |
exit(1); |
| 1223 |
} |
| 1224 |
flag_io_disabled = newval; |
| 1225 |
} |
| 1226 |
|
| 1227 |
static void save_state(void) |
| 1228 |
{ |
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
|
| 1235 |
change_flag_io_disabled(FID_REGULAR_MODE, FID_SAVED_STATE); |
| 1236 |
|
| 1237 |
saved_state = rpy_revdb; |
| 1238 |
|
| 1239 |
rpy_revdb.unique_id_seen = (-1ULL) << 63; |
| 1240 |
rpy_revdb.watch_enabled = 0; |
| 1241 |
rpy_revdb.stop_point_break = -1; |
| 1242 |
rpy_revdb.unique_id_break = -1; |
| 1243 |
rpy_revdb.buf_p = rpy_rev_buffer; |
| 1244 |
rpy_revdb.buf_limit = rpy_rev_buffer; |
| 1245 |
} |
| 1246 |
|
| 1247 |
static void restore_state(void) |
| 1248 |
{ |
| 1249 |
|
| 1250 |
|
| 1251 |
change_flag_io_disabled(FID_SAVED_STATE, FID_REGULAR_MODE); |
| 1252 |
|
| 1253 |
|
| 1254 |
rpy_revdb = saved_state; |
| 1255 |
|
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
set_revdb_breakpoints(); |
| 1260 |
} |
| 1261 |
|
| 1262 |
static void protect_potential_io(void) |
| 1263 |
{ |
| 1264 |
change_flag_io_disabled(FID_SAVED_STATE, FID_POTENTIAL_IO); |
| 1265 |
saved_exc[0] = pypy_g_ExcData.ed_exc_type; |
| 1266 |
saved_exc[1] = pypy_g_ExcData.ed_exc_value; |
| 1267 |
pypy_g_ExcData.ed_exc_type = NULL; |
| 1268 |
pypy_g_ExcData.ed_exc_value = NULL; |
| 1269 |
} |
| 1270 |
|
| 1271 |
static void unprotect_potential_io(void) |
| 1272 |
{ |
| 1273 |
change_flag_io_disabled(FID_POTENTIAL_IO, FID_SAVED_STATE); |
| 1274 |
if (pypy_g_ExcData.ed_exc_type != NULL) { |
| 1275 |
fprintf(stderr, "Command crashed with %.*s\n", |
| 1276 |
(int)(pypy_g_ExcData.ed_exc_type->ov_name->rs_chars.length), |
| 1277 |
pypy_g_ExcData.ed_exc_type->ov_name->rs_chars.items); |
| 1278 |
write_answer(ANSWER_ATTEMPT_IO, 1, 0, 0); |
| 1279 |
exit(0); |
| 1280 |
} |
| 1281 |
pypy_g_ExcData.ed_exc_type = saved_exc[0]; |
| 1282 |
pypy_g_ExcData.ed_exc_value = saved_exc[1]; |
| 1283 |
} |
| 1284 |
|
| 1285 |
static void execute_rpy_function(rpy_revdb_command_fn func, |
| 1286 |
rpy_revdb_command_t *cmd, |
| 1287 |
RPyString *extra) |
| 1288 |
{ |
| 1289 |
protect_potential_io(); |
| 1290 |
func(cmd, extra); |
| 1291 |
unprotect_potential_io(); |
| 1292 |
} |
| 1293 |
|
| 1294 |
static void check_at_end(uint64_t stop_points) |
| 1295 |
{ |
| 1296 |
char dummy[1]; |
| 1297 |
if (rpy_revdb.buf_p != rpy_revdb.buf_limit - 1 || |
| 1298 |
read(rpy_rev_fileno, dummy, 1) > 0) { |
| 1299 |
fprintf(stderr, "RevDB file error: too much data: corrupted file, " |
| 1300 |
"revdb bug, or non-deterministic run, e.g. a " |
| 1301 |
"watchpoint with side effects)\n"); |
| 1302 |
exit(1); |
| 1303 |
} |
| 1304 |
if (stop_points != rpy_revdb.stop_point_seen) { |
| 1305 |
fprintf(stderr, "Bad number of stop points " |
| 1306 |
"(seen %lld, recorded %lld)\n", |
| 1307 |
(long long)rpy_revdb.stop_point_seen, |
| 1308 |
(long long)stop_points); |
| 1309 |
exit(1); |
| 1310 |
} |
| 1311 |
if (stop_points != total_stop_points) { |
| 1312 |
fprintf(stderr, "RevDB file modified while reading?\n"); |
| 1313 |
exit(1); |
| 1314 |
} |
| 1315 |
|
| 1316 |
write_answer(ANSWER_AT_END, 0, 0, 0); |
| 1317 |
exit(0); |
| 1318 |
} |
| 1319 |
|
| 1320 |
static void command_fork(int activate) |
| 1321 |
{ |
| 1322 |
int child_sockfd; |
| 1323 |
int child_pid; |
| 1324 |
off_t rev_offset = lseek(rpy_rev_fileno, 0, SEEK_CUR); |
| 1325 |
|
| 1326 |
if (ancil_recv_fd(rpy_rev_sockfd, &child_sockfd) < 0) { |
| 1327 |
fprintf(stderr, "cannot fetch child control socket: %m\n"); |
| 1328 |
exit(1); |
| 1329 |
} |
| 1330 |
child_pid = fork(); |
| 1331 |
if (child_pid == -1) { |
| 1332 |
perror("fork"); |
| 1333 |
exit(1); |
| 1334 |
} |
| 1335 |
if (child_pid == 0) { |
| 1336 |
|
| 1337 |
if (close(rpy_rev_sockfd) < 0) { |
| 1338 |
perror("close"); |
| 1339 |
exit(1); |
| 1340 |
} |
| 1341 |
rpy_rev_sockfd = child_sockfd; |
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
|
| 1346 |
signal(SIGINT, activate ? SIG_DFL : SIG_IGN); |
| 1347 |
|
| 1348 |
|
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
char fd_filename[48]; |
| 1354 |
struct stat st; |
| 1355 |
const char *filename; |
| 1356 |
int old_fd = rpy_rev_fileno; |
| 1357 |
|
| 1358 |
sprintf(fd_filename, "/proc/self/fd/%d", old_fd); |
| 1359 |
if (lstat(fd_filename, &st) == 0) |
| 1360 |
filename = fd_filename; |
| 1361 |
else |
| 1362 |
filename = rpy_rev_filename; |
| 1363 |
reopen_revdb_file(filename); |
| 1364 |
|
| 1365 |
if (close(old_fd) < 0) { |
| 1366 |
perror("close"); |
| 1367 |
exit(1); |
| 1368 |
} |
| 1369 |
if (lseek(rpy_rev_fileno, rev_offset, SEEK_SET) < 0) { |
| 1370 |
perror("lseek"); |
| 1371 |
exit(1); |
| 1372 |
} |
| 1373 |
} |
| 1374 |
else { |
| 1375 |
|
| 1376 |
write_answer(ANSWER_FORKED, child_pid, 0, 0); |
| 1377 |
close(child_sockfd); |
| 1378 |
} |
| 1379 |
} |
| 1380 |
|
| 1381 |
static void answer_recorded_breakpoint(void) |
| 1382 |
{ |
| 1383 |
int i; |
| 1384 |
for (i = 0; i < n_last_recorded_breakpoints; i++) |
| 1385 |
write_answer(ANSWER_BREAKPOINT, last_recorded_breakpoint_loc, |
| 1386 |
0, last_recorded_breakpoint_nums[i]); |
| 1387 |
n_last_recorded_breakpoints = 0; |
| 1388 |
} |
| 1389 |
|
| 1390 |
static void command_forward(rpy_revdb_command_t *cmd) |
| 1391 |
{ |
| 1392 |
if (cmd->arg1 < 0) { |
| 1393 |
fprintf(stderr, "CMD_FORWARD: negative step\n"); |
| 1394 |
exit(1); |
| 1395 |
} |
| 1396 |
assert(flag_io_disabled == FID_SAVED_STATE); |
| 1397 |
interactive_break = saved_state.stop_point_seen + cmd->arg1; |
| 1398 |
breakpoint_mode = (char)cmd->arg2; |
| 1399 |
if (breakpoint_mode == 'r') { |
| 1400 |
n_last_recorded_breakpoints = 0; |
| 1401 |
pending_after_forward = &answer_recorded_breakpoint; |
| 1402 |
} |
| 1403 |
} |
| 1404 |
|
| 1405 |
static void command_future_ids(rpy_revdb_command_t *cmd, char *extra) |
| 1406 |
{ |
| 1407 |
free(future_ids); |
| 1408 |
if (cmd->extra_size == 0) { |
| 1409 |
future_ids = NULL; |
| 1410 |
uid_break = 0; |
| 1411 |
} |
| 1412 |
else { |
| 1413 |
assert(cmd->extra_size % sizeof(uint64_t) == 0); |
| 1414 |
future_ids = malloc(cmd->extra_size + sizeof(uint64_t)); |
| 1415 |
if (future_ids == NULL) { |
| 1416 |
fprintf(stderr, "out of memory for a buffer of %llu chars\n", |
| 1417 |
(unsigned long long)cmd->extra_size); |
| 1418 |
exit(1); |
| 1419 |
} |
| 1420 |
memcpy(future_ids, extra, cmd->extra_size); |
| 1421 |
future_ids[cmd->extra_size / sizeof(uint64_t)] = 0; |
| 1422 |
uid_break = *future_ids; |
| 1423 |
} |
| 1424 |
future_next_id = future_ids; |
| 1425 |
} |
| 1426 |
|
| 1427 |
static void command_default(rpy_revdb_command_t *cmd, char *extra) |
| 1428 |
{ |
| 1429 |
RPyString *s; |
| 1430 |
int i; |
| 1431 |
for (i = 0; rpy_revdb_commands.rp_names[i] != cmd->cmd; i++) { |
| 1432 |
if (rpy_revdb_commands.rp_names[i] == 0) { |
| 1433 |
fprintf(stderr, "unknown command %d\n", cmd->cmd); |
| 1434 |
exit(1); |
| 1435 |
} |
| 1436 |
} |
| 1437 |
|
| 1438 |
if (cmd->extra_size == 0) { |
| 1439 |
s = empty_string; |
| 1440 |
} |
| 1441 |
else { |
| 1442 |
s = make_rpy_string(cmd->extra_size); |
| 1443 |
memcpy(_RPyString_AsString(s), extra, cmd->extra_size); |
| 1444 |
} |
| 1445 |
execute_rpy_function(rpy_revdb_commands.rp_funcs[i], cmd, s); |
| 1446 |
} |
| 1447 |
|
| 1448 |
RPY_EXTERN |
| 1449 |
void rpy_reverse_db_watch_save_state(void) |
| 1450 |
{ |
| 1451 |
save_state(); |
| 1452 |
} |
| 1453 |
|
| 1454 |
RPY_EXTERN |
| 1455 |
void rpy_reverse_db_watch_restore_state(bool_t any_watch_point) |
| 1456 |
{ |
| 1457 |
restore_state(); |
| 1458 |
rpy_revdb.watch_enabled = any_watch_point; |
| 1459 |
} |
| 1460 |
|
| 1461 |
static void replay_call_destructors(void); |
| 1462 |
|
| 1463 |
static void replay_stop_point(void) |
| 1464 |
{ |
| 1465 |
if (finalizer_break != (uint64_t)-1) |
| 1466 |
replay_call_destructors(); |
| 1467 |
|
| 1468 |
if (rpy_revdb.stop_point_break != interactive_break) { |
| 1469 |
fprintf(stderr, "mismatch between interactive_break and " |
| 1470 |
"stop_point_break\n"); |
| 1471 |
exit(1); |
| 1472 |
} |
| 1473 |
|
| 1474 |
while (rpy_revdb.stop_point_break == rpy_revdb.stop_point_seen) { |
| 1475 |
save_state(); |
| 1476 |
|
| 1477 |
if (pending_after_forward) { |
| 1478 |
void (*fn)(void) = pending_after_forward; |
| 1479 |
pending_after_forward = NULL; |
| 1480 |
fn(); |
| 1481 |
} |
| 1482 |
else { |
| 1483 |
rpy_revdb_command_t cmd; |
| 1484 |
write_answer(ANSWER_READY, |
| 1485 |
saved_state.stop_point_seen, |
| 1486 |
saved_state.unique_id_seen, |
| 1487 |
current_thread_num); |
| 1488 |
read_sock(&cmd, sizeof(cmd)); |
| 1489 |
|
| 1490 |
char extra[cmd.extra_size + 1]; |
| 1491 |
extra[cmd.extra_size] = 0; |
| 1492 |
if (cmd.extra_size > 0) |
| 1493 |
read_sock(extra, cmd.extra_size); |
| 1494 |
|
| 1495 |
switch (cmd.cmd) { |
| 1496 |
|
| 1497 |
case CMD_FORK: |
| 1498 |
command_fork(cmd.arg1); |
| 1499 |
break; |
| 1500 |
|
| 1501 |
case CMD_QUIT: |
| 1502 |
exit(0); |
| 1503 |
break; |
| 1504 |
|
| 1505 |
case CMD_FORWARD: |
| 1506 |
command_forward(&cmd); |
| 1507 |
break; |
| 1508 |
|
| 1509 |
case CMD_FUTUREIDS: |
| 1510 |
command_future_ids(&cmd, extra); |
| 1511 |
break; |
| 1512 |
|
| 1513 |
case CMD_PING: |
| 1514 |
break; |
| 1515 |
|
| 1516 |
default: |
| 1517 |
command_default(&cmd, extra); |
| 1518 |
break; |
| 1519 |
} |
| 1520 |
} |
| 1521 |
restore_state(); |
| 1522 |
} |
| 1523 |
} |
| 1524 |
|
| 1525 |
RPY_EXTERN |
| 1526 |
void rpy_reverse_db_send_answer(int cmd, int64_t arg1, int64_t arg2, |
| 1527 |
int64_t arg3, RPyString *extra) |
| 1528 |
{ |
| 1529 |
rpy_revdb_command_t c; |
| 1530 |
size_t extra_size = RPyString_Size(extra); |
| 1531 |
c.cmd = cmd; |
| 1532 |
c.extra_size = extra_size; |
| 1533 |
if (c.extra_size != extra_size) { |
| 1534 |
fprintf(stderr, "string too large (more than 4GB)\n"); |
| 1535 |
exit(1); |
| 1536 |
} |
| 1537 |
c.arg1 = arg1; |
| 1538 |
c.arg2 = arg2; |
| 1539 |
c.arg3 = arg3; |
| 1540 |
write_sock(&c, sizeof(c)); |
| 1541 |
if (extra_size > 0) |
| 1542 |
write_sock(_RPyString_AsString(extra), extra_size); |
| 1543 |
} |
| 1544 |
|
| 1545 |
RPY_EXTERN |
| 1546 |
void rpy_reverse_db_breakpoint(int64_t num) |
| 1547 |
{ |
| 1548 |
if (flag_io_disabled != FID_REGULAR_MODE) { |
| 1549 |
|
| 1550 |
return; |
| 1551 |
} |
| 1552 |
|
| 1553 |
switch (breakpoint_mode) { |
| 1554 |
case 'i': |
| 1555 |
return; |
| 1556 |
|
| 1557 |
case 'r': |
| 1558 |
if (last_recorded_breakpoint_loc != rpy_revdb.stop_point_seen) { |
| 1559 |
last_recorded_breakpoint_loc = rpy_revdb.stop_point_seen; |
| 1560 |
n_last_recorded_breakpoints = 0; |
| 1561 |
} |
| 1562 |
if (n_last_recorded_breakpoints < RECORD_BKPT_NUM) { |
| 1563 |
last_recorded_breakpoint_nums[n_last_recorded_breakpoints] = num; |
| 1564 |
n_last_recorded_breakpoints++; |
| 1565 |
} |
| 1566 |
return; |
| 1567 |
|
| 1568 |
case 'b': |
| 1569 |
interactive_break = rpy_revdb.stop_point_seen + 1; |
| 1570 |
set_revdb_breakpoints(); |
| 1571 |
write_answer(ANSWER_BREAKPOINT, rpy_revdb.stop_point_break, 0, num); |
| 1572 |
return; |
| 1573 |
|
| 1574 |
default: |
| 1575 |
fprintf(stderr, "bad value %d of breakpoint_mode\n", |
| 1576 |
(int)breakpoint_mode); |
| 1577 |
exit(1); |
| 1578 |
} |
| 1579 |
} |
| 1580 |
|
| 1581 |
RPY_EXTERN |
| 1582 |
long long rpy_reverse_db_get_value(char value_id) |
| 1583 |
{ |
| 1584 |
switch (value_id) { |
| 1585 |
case 'c': |
| 1586 |
return (flag_io_disabled == FID_REGULAR_MODE ? |
| 1587 |
rpy_revdb.stop_point_seen : |
| 1588 |
saved_state.stop_point_seen); |
| 1589 |
case 't': |
| 1590 |
return total_stop_points; |
| 1591 |
case 'b': |
| 1592 |
return interactive_break; |
| 1593 |
case 'u': |
| 1594 |
return (flag_io_disabled == FID_REGULAR_MODE ? |
| 1595 |
rpy_revdb.unique_id_seen : |
| 1596 |
saved_state.unique_id_seen); |
| 1597 |
case 'p': |
| 1598 |
return current_place; |
| 1599 |
case 'i': |
| 1600 |
return flag_io_disabled; |
| 1601 |
default: |
| 1602 |
return -1; |
| 1603 |
} |
| 1604 |
} |
| 1605 |
|
| 1606 |
RPY_EXTERN |
| 1607 |
uint64_t rpy_reverse_db_unique_id_break(void *new_object) |
| 1608 |
{ |
| 1609 |
uint64_t uid = rpy_revdb.unique_id_seen; |
| 1610 |
bool_t watch_enabled = rpy_revdb.watch_enabled; |
| 1611 |
|
| 1612 |
if (!new_object) { |
| 1613 |
fprintf(stderr, "out of memory: allocation failed, cannot continue\n"); |
| 1614 |
exit(1); |
| 1615 |
} |
| 1616 |
|
| 1617 |
save_state(); |
| 1618 |
if (rpy_revdb_commands.rp_alloc) { |
| 1619 |
protect_potential_io(); |
| 1620 |
|
| 1621 |
rpy_revdb_commands.rp_alloc(uid, new_object); |
| 1622 |
unprotect_potential_io(); |
| 1623 |
} |
| 1624 |
uid_break = *++future_next_id; |
| 1625 |
restore_state(); |
| 1626 |
rpy_revdb.watch_enabled = watch_enabled; |
| 1627 |
return uid; |
| 1628 |
} |
| 1629 |
|
| 1630 |
struct destructor_s { |
| 1631 |
void *d_obj; |
| 1632 |
void (*d_callback)(void *); |
| 1633 |
}; |
| 1634 |
|
| 1635 |
static int _ftree_compare(const void *obj1, const void *obj2) |
| 1636 |
{ |
| 1637 |
const struct destructor_s *d1 = obj1; |
| 1638 |
const struct destructor_s *d2 = obj2; |
| 1639 |
struct pypy_header0 *h1 = d1->d_obj; |
| 1640 |
struct pypy_header0 *h2 = d2->d_obj; |
| 1641 |
if (h1->h_uid < h2->h_uid) |
| 1642 |
return -1; |
| 1643 |
if (h1->h_uid == h2->h_uid) |
| 1644 |
return 0; |
| 1645 |
else |
| 1646 |
return 1; |
| 1647 |
} |
| 1648 |
|
| 1649 |
static void _ftree_add(void **tree, void *obj, void (*callback)(void *)) |
| 1650 |
{ |
| 1651 |
|
| 1652 |
|
| 1653 |
|
| 1654 |
struct destructor_s *node, **item; |
| 1655 |
node = GC_MALLOC_UNCOLLECTABLE(sizeof(struct destructor_s)); |
| 1656 |
node->d_obj = obj; |
| 1657 |
node->d_callback = callback; |
| 1658 |
item = tsearch(node, tree, _ftree_compare); |
| 1659 |
if (item == NULL) { |
| 1660 |
fprintf(stderr, "_ftree_add: out of memory\n"); |
| 1661 |
exit(1); |
| 1662 |
} |
| 1663 |
if (*item != node) { |
| 1664 |
fprintf(stderr, "_ftree_add: duplicate object\n"); |
| 1665 |
exit(1); |
| 1666 |
} |
| 1667 |
} |
| 1668 |
|
| 1669 |
static struct pypy_header0 *_ftree_pop(void **tree, uint64_t uid, |
| 1670 |
void (**callback_out)(void *)) |
| 1671 |
{ |
| 1672 |
struct destructor_s d_dummy, *entry, **item; |
| 1673 |
struct pypy_header0 o_dummy, *result; |
| 1674 |
|
| 1675 |
d_dummy.d_obj = &o_dummy; |
| 1676 |
o_dummy.h_uid = uid; |
| 1677 |
item = tfind(&d_dummy, tree, _ftree_compare); |
| 1678 |
if (item == NULL) { |
| 1679 |
fprintf(stderr, "_ftree_pop: object not found\n"); |
| 1680 |
exit(1); |
| 1681 |
} |
| 1682 |
entry = *item; |
| 1683 |
result = entry->d_obj; |
| 1684 |
if (callback_out) |
| 1685 |
*callback_out = entry->d_callback; |
| 1686 |
assert(result->h_uid == uid); |
| 1687 |
tdelete(entry, tree, _ftree_compare); |
| 1688 |
GC_FREE(entry); |
| 1689 |
return result; |
| 1690 |
} |
| 1691 |
|
| 1692 |
RPY_EXTERN |
| 1693 |
int rpy_reverse_db_fq_register(void *obj) |
| 1694 |
{ |
| 1695 |
|
| 1696 |
|
| 1697 |
if (!RPY_RDB_REPLAY) { |
| 1698 |
return 0; |
| 1699 |
} |
| 1700 |
else { |
| 1701 |
|
| 1702 |
_ftree_add(&finalizer_tree, obj, NULL); |
| 1703 |
return 1; |
| 1704 |
} |
| 1705 |
} |
| 1706 |
|
| 1707 |
RPY_EXTERN |
| 1708 |
void *rpy_reverse_db_next_dead(void *result) |
| 1709 |
{ |
| 1710 |
int64_t uid; |
| 1711 |
RPY_REVDB_EMIT(uid = result ? ((struct pypy_header0 *)result)->h_uid : -1;, |
| 1712 |
int64_t _e, uid); |
| 1713 |
|
| 1714 |
if (RPY_RDB_REPLAY) { |
| 1715 |
if (uid == -1) { |
| 1716 |
result = NULL; |
| 1717 |
} |
| 1718 |
else { |
| 1719 |
|
| 1720 |
result = _ftree_pop(&finalizer_tree, uid, NULL); |
| 1721 |
} |
| 1722 |
} |
| 1723 |
return result; |
| 1724 |
} |
| 1725 |
|
| 1726 |
RPY_EXTERN |
| 1727 |
void rpy_reverse_db_register_destructor(void *obj, void (*callback)(void *)) |
| 1728 |
{ |
| 1729 |
if (!RPY_RDB_REPLAY) { |
| 1730 |
GC_REGISTER_FINALIZER(obj, (GC_finalization_proc)callback, |
| 1731 |
NULL, NULL, NULL); |
| 1732 |
} |
| 1733 |
else { |
| 1734 |
_ftree_add(&destructor_tree, obj, callback); |
| 1735 |
} |
| 1736 |
} |
| 1737 |
|
| 1738 |
static void replay_call_destructors(void) |
| 1739 |
{ |
| 1740 |
|
| 1741 |
|
| 1742 |
|
| 1743 |
finalizer_break = -1; |
| 1744 |
set_revdb_breakpoints(); |
| 1745 |
rpy_reverse_db_fetch(__FILE__, __LINE__); |
| 1746 |
|
| 1747 |
while (1) { |
| 1748 |
int64_t uid; |
| 1749 |
struct pypy_header0 *obj; |
| 1750 |
void (*callback)(void *); |
| 1751 |
|
| 1752 |
RPY_REVDB_EMIT(abort();, int64_t _e, uid); |
| 1753 |
if (uid == -1) |
| 1754 |
break; |
| 1755 |
|
| 1756 |
obj = _ftree_pop(&destructor_tree, uid, &callback); |
| 1757 |
callback(obj); |
| 1758 |
} |
| 1759 |
|
| 1760 |
|
| 1761 |
|
| 1762 |
fq_trigger(); |
| 1763 |
} |
| 1764 |
|
| 1765 |
static void *callbacklocs[] = { |
| 1766 |
RPY_CALLBACKLOCS |
| 1767 |
}; |
| 1768 |
|
| 1769 |
static void replay_invoke_callback(unsigned char e) |
| 1770 |
{ |
| 1771 |
unsigned long index; |
| 1772 |
unsigned char e2; |
| 1773 |
void (*pfn)(void); |
| 1774 |
_RPY_REVDB_EMIT_REPLAY(unsigned char _e, e2) |
| 1775 |
index = (e << 8) | e2; |
| 1776 |
index -= 300; |
| 1777 |
if (index >= (sizeof(callbacklocs) / sizeof(callbacklocs[0]))) { |
| 1778 |
fprintf(stderr, "bad callback index %lx\n", index); |
| 1779 |
exit(1); |
| 1780 |
} |
| 1781 |
pfn = callbacklocs[index]; |
| 1782 |
pfn(); |
| 1783 |
} |
| 1784 |
|
| 1785 |
RPY_EXTERN |
| 1786 |
void rpy_reverse_db_invoke_callback(unsigned char e) |
| 1787 |
{ |
| 1788 |
|
| 1789 |
|
| 1790 |
|
| 1791 |
|
| 1792 |
do { |
| 1793 |
replay_invoke_callback(e); |
| 1794 |
_RPY_REVDB_EMIT_REPLAY(unsigned char _e, e) |
| 1795 |
} while (e != 0xFC); |
| 1796 |
} |
| 1797 |
|
| 1798 |
RPY_EXTERN |
| 1799 |
void rpy_reverse_db_bad_acquire_gil(const char *name) |
| 1800 |
{ |
| 1801 |
fprintf(stderr, "out of sync: unexpected byte in log " |
| 1802 |
" (at %s_gil)\n", name); |
| 1803 |
exit(1); |
| 1804 |
} |
| 1805 |
|
| 1806 |
RPY_EXTERN |
| 1807 |
void rpy_reverse_db_set_thread_breakpoint(int64_t tnum) |
| 1808 |
{ |
| 1809 |
break_thread_num = (uint64_t)tnum; |
| 1810 |
} |
| 1811 |
|
| 1812 |
#define INVALID_STRTOD (-3.46739514239368e+113) |
| 1813 |
|
| 1814 |
RPY_EXTERN |
| 1815 |
double rpy_reverse_db_strtod(RPyString *s) |
| 1816 |
{ |
| 1817 |
|
| 1818 |
double result; |
| 1819 |
char *endptr = NULL; |
| 1820 |
char buffer[8192]; |
| 1821 |
size_t size = RPyString_Size(s); |
| 1822 |
|
| 1823 |
if (size >= sizeof(buffer)) |
| 1824 |
return INVALID_STRTOD; |
| 1825 |
memcpy(buffer, _RPyString_AsString(s), size); |
| 1826 |
buffer[size] = '\0'; |
| 1827 |
result = strtod(buffer, &endptr); |
| 1828 |
if (endptr == NULL || *endptr != '\0') |
| 1829 |
return INVALID_STRTOD; |
| 1830 |
return result; |
| 1831 |
} |
| 1832 |
|
| 1833 |
RPY_EXTERN |
| 1834 |
RPyString *rpy_reverse_db_dtoa(double d) |
| 1835 |
{ |
| 1836 |
char buffer[128], *p; |
| 1837 |
RPyString *result; |
| 1838 |
int size; |
| 1839 |
size = snprintf(buffer, sizeof(buffer) - 3, "%.17g", d); |
| 1840 |
if (size < 0) |
| 1841 |
size = 0; |
| 1842 |
for (p = buffer; '0' <= *p && *p <= '9'; p++) { |
| 1843 |
} |
| 1844 |
if (*p == 0) { |
| 1845 |
buffer[size++] = '.'; |
| 1846 |
buffer[size++] = '0'; |
| 1847 |
} |
| 1848 |
result = make_rpy_string(size); |
| 1849 |
memcpy(_RPyString_AsString(result), buffer, size); |
| 1850 |
return result; |
| 1851 |
} |
| 1852 |
|
| 1853 |
|
| 1854 |
#ifdef RPY_USES_RAWREFCOUNT |
| 1855 |
|
| 1856 |
static void *rawrefcount_tree; |
| 1857 |
|
| 1858 |
struct rawrefcount_link2_s { |
| 1859 |
void *pyobj; |
| 1860 |
void *gcobj; |
| 1861 |
}; |
| 1862 |
|
| 1863 |
static int _rrtree_compare(const void *obj1, const void *obj2) |
| 1864 |
{ |
| 1865 |
const struct rawrefcount_link2_s *r1 = obj1; |
| 1866 |
const struct rawrefcount_link2_s *r2 = obj2; |
| 1867 |
void *p1 = r1->pyobj; |
| 1868 |
void *p2 = r2->pyobj; |
| 1869 |
if (p1 < p2) |
| 1870 |
return -1; |
| 1871 |
if (p1 == p2) |
| 1872 |
return 0; |
| 1873 |
else |
| 1874 |
return 1; |
| 1875 |
} |
| 1876 |
|
| 1877 |
static void _rrtree_add(void *pyobj, void *gcobj) |
| 1878 |
{ |
| 1879 |
|
| 1880 |
|
| 1881 |
|
| 1882 |
struct rawrefcount_link2_s *node, **item; |
| 1883 |
node = GC_MALLOC_UNCOLLECTABLE(sizeof(struct rawrefcount_link2_s)); |
| 1884 |
node->pyobj = pyobj; |
| 1885 |
node->gcobj = gcobj; |
| 1886 |
item = tsearch(node, &rawrefcount_tree, _rrtree_compare); |
| 1887 |
if (item == NULL) { |
| 1888 |
fprintf(stderr, "_rrtree_add: out of memory\n"); |
| 1889 |
exit(1); |
| 1890 |
} |
| 1891 |
if (*item != node) { |
| 1892 |
fprintf(stderr, "_rrtree_add: duplicate object\n"); |
| 1893 |
exit(1); |
| 1894 |
} |
| 1895 |
} |
| 1896 |
|
| 1897 |
RPY_EXTERN |
| 1898 |
void rpy_reverse_db_rawrefcount_create_link_pypy(void *gcobj, void *pyobj) |
| 1899 |
{ |
| 1900 |
if (!RPY_RDB_REPLAY) { |
| 1901 |
gc_rawrefcount_create_link_pypy(gcobj, pyobj); |
| 1902 |
} |
| 1903 |
else { |
| 1904 |
_rrtree_add(pyobj, gcobj); |
| 1905 |
} |
| 1906 |
} |
| 1907 |
|
| 1908 |
RPY_EXTERN |
| 1909 |
void *rpy_reverse_db_rawrefcount_from_obj(void *gcobj) |
| 1910 |
{ |
| 1911 |
void *r; |
| 1912 |
RPY_REVDB_EMIT(r = gc_rawrefcount_from_obj(gcobj);, void *_e, r); |
| 1913 |
return r; |
| 1914 |
} |
| 1915 |
|
| 1916 |
RPY_EXTERN |
| 1917 |
void *rpy_reverse_db_rawrefcount_to_obj(void *pyobj) |
| 1918 |
{ |
| 1919 |
unsigned char flag; |
| 1920 |
|
| 1921 |
if (!RPY_RDB_REPLAY) { |
| 1922 |
void *r = gc_rawrefcount_to_obj(pyobj); |
| 1923 |
RPY_REVDB_EMIT(flag = 0xEE + !r;, unsigned char _e, flag); |
| 1924 |
return r; |
| 1925 |
} |
| 1926 |
else { |
| 1927 |
RPY_REVDB_EMIT(abort();, unsigned char _e, flag); |
| 1928 |
switch (flag) { |
| 1929 |
|
| 1930 |
case 0xEF: |
| 1931 |
|
| 1932 |
return NULL; |
| 1933 |
|
| 1934 |
case 0xEE: |
| 1935 |
|
| 1936 |
break; |
| 1937 |
|
| 1938 |
default: |
| 1939 |
fprintf(stderr, "bad byte in rawrefcount_to_obj\n"); |
| 1940 |
exit(1); |
| 1941 |
} |
| 1942 |
|
| 1943 |
struct rawrefcount_link2_s **item, dummy; |
| 1944 |
dummy.pyobj = pyobj; |
| 1945 |
item = tfind(&dummy, &rawrefcount_tree, _rrtree_compare); |
| 1946 |
if (item == NULL) { |
| 1947 |
fprintf(stderr, "rawrefcount_to_obj: not found in tree\n"); |
| 1948 |
exit(1); |
| 1949 |
} |
| 1950 |
return (*item)->gcobj; |
| 1951 |
} |
| 1952 |
} |
| 1953 |
|
| 1954 |
RPY_EXTERN |
| 1955 |
void *rpy_reverse_db_rawrefcount_next_dead(void) |
| 1956 |
{ |
| 1957 |
unsigned char flag; |
| 1958 |
|
| 1959 |
if (!RPY_RDB_REPLAY) { |
| 1960 |
void *r = gc_rawrefcount_next_dead(); |
| 1961 |
RPY_REVDB_EMIT(flag = 0xEC + !r;, unsigned char _e, flag); |
| 1962 |
if (r) { |
| 1963 |
RPY_REVDB_EMIT(;, void *_e, r); |
| 1964 |
} |
| 1965 |
return r; |
| 1966 |
} |
| 1967 |
else { |
| 1968 |
|
| 1969 |
|
| 1970 |
|
| 1971 |
|
| 1972 |
|
| 1973 |
|
| 1974 |
|
| 1975 |
|
| 1976 |
|
| 1977 |
|
| 1978 |
|
| 1979 |
RPY_REVDB_EMIT(abort();, unsigned char _e, flag); |
| 1980 |
switch (flag) { |
| 1981 |
|
| 1982 |
case 0xED: |
| 1983 |
|
| 1984 |
return NULL; |
| 1985 |
|
| 1986 |
case 0xEC: |
| 1987 |
|
| 1988 |
break; |
| 1989 |
|
| 1990 |
default: |
| 1991 |
fprintf(stderr, "bad byte in rawrefcount_next_dead\n"); |
| 1992 |
exit(1); |
| 1993 |
} |
| 1994 |
|
| 1995 |
void *pyobj; |
| 1996 |
RPY_REVDB_EMIT(abort();, void *_e, pyobj); |
| 1997 |
|
| 1998 |
struct rawrefcount_link2_s **item, *entry, dummy; |
| 1999 |
dummy.pyobj = pyobj; |
| 2000 |
item = tfind(&dummy, &rawrefcount_tree, _rrtree_compare); |
| 2001 |
if (item == NULL) { |
| 2002 |
fprintf(stderr, "rawrefcount_next_dead: not found in tree\n"); |
| 2003 |
exit(1); |
| 2004 |
} |
| 2005 |
entry = *item; |
| 2006 |
tdelete(entry, &rawrefcount_tree, _rrtree_compare); |
| 2007 |
GC_FREE(entry); |
| 2008 |
|
| 2009 |
return pyobj; |
| 2010 |
} |
| 2011 |
} |
| 2012 |
|
| 2013 |
#endif |
| 2014 |
|
| 2015 |
|
| 2016 |
|
| 2017 |
|
| 2018 |
|
| 2019 |
RPY_EXTERN |
| 2020 |
void seeing_uid(uint64_t uid) |
| 2021 |
{ |
| 2022 |
} |