Skip to content

Commit 01862fd

Browse files
authored
thread_ptrace: careful not to deref NULL pointer (#1334)
1 parent c997d4d commit 01862fd

1 file changed

Lines changed: 17 additions & 16 deletions

File tree

src/main/host/thread_ptrace.c

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -452,26 +452,27 @@ static void _threadptrace_enterStateSignalled(ThreadPtrace* thread,
452452
if (signal == SIGSEGV) {
453453
_threadptrace_getregs(thread);
454454
trace("threadptrace_enterStateSignalled regs: %s", _regs_to_str(&thread->regs.value));
455-
uint64_t eip = thread->regs.value.rip;
456-
const uint8_t* buf = process_getReadablePtr(thread->base.process, (PluginPtr){eip}, 4);
457-
if (isRdtsc(buf)) {
458-
trace("emulating rdtsc");
459-
Tsc_emulateRdtsc(&thread->tsc, &thread->regs.value, worker_getCurrentTime() / SIMTIME_ONE_NANOSECOND);
460-
thread->regs.dirty = true;
461-
return;
462-
}
463-
if (isRdtscp(buf)) {
464-
trace("emulating rdtscp");
465-
Tsc_emulateRdtscp(
466-
&thread->tsc, &thread->regs.value, worker_getCurrentTime() / SIMTIME_ONE_NANOSECOND);
467-
thread->regs.dirty = true;
468-
return;
455+
uint64_t rip = thread->regs.value.rip;
456+
uint8_t buf[4];
457+
if (process_readPtr(thread->base.process, buf, (PluginPtr){rip}, sizeof(buf)) == 0) {
458+
if (isRdtsc(buf)) {
459+
trace("emulating rdtsc");
460+
Tsc_emulateRdtsc(&thread->tsc, &thread->regs.value, worker_getCurrentTime() / SIMTIME_ONE_NANOSECOND);
461+
thread->regs.dirty = true;
462+
return;
463+
}
464+
if (isRdtscp(buf)) {
465+
trace("emulating rdtscp");
466+
Tsc_emulateRdtscp(
467+
&thread->tsc, &thread->regs.value, worker_getCurrentTime() / SIMTIME_ONE_NANOSECOND);
468+
thread->regs.dirty = true;
469+
return;
470+
}
469471
}
470472
// Do not use `panic` here, since that'll cause us to immediately abort
471473
// in debug builds. Better to let the SIGSEGV be delivered so that it
472474
// can generate a core file for debugging.
473-
warning("Unhandled SIGSEGV addr:%016lx contents:%x %x %x %x", eip, buf[0], buf[1], buf[2],
474-
buf[3]);
475+
warning("Unhandled SIGSEGV at rip:%016lx", rip);
475476
// fall through
476477
} else if (signal == SIGSTOP) {
477478
trace("Suppressing SIGSTOP");

0 commit comments

Comments
 (0)