Skip to content

fix: build error and two runtime crashes in the syscall tracer - #6

Open
ViniciusCestarii wants to merge 3 commits into
Toyota:mainfrom
ViniciusCestarii:fix-bugs
Open

fix: build error and two runtime crashes in the syscall tracer#6
ViniciusCestarii wants to merge 3 commits into
Toyota:mainfrom
ViniciusCestarii:fix-bugs

Conversation

@ViniciusCestarii

Copy link
Copy Markdown

Fixes one build failure and two runtime crashes surfaced while tracing this Cap'n Proto client. Each commit maps to one of the errors below.

1. Build error: CLOCK_MONOTONIC was not declared

rpc_message_recorder.cc used clock_gettime(CLOCK_MONOTONIC, ...) without including the header that declares them, breaking the build on GCC 16:

error: ‘CLOCK_MONOTONIC’ was not declared in this scope
error: ‘clock_gettime’ was not declared in this scope

Fix: add #include <time.h>.

2. Crash: out-of-bounds struct pointer during reassembly

capnp/layout.c++:2204: failed: expected boundsCheck(...); Message contained out-of-bounds struct pointer.
The read/write leave handlers fed the requested byte count (count / `iov_lcorrupt the reassembled Cap'n Proto stream.

Fix: use rc as the valid length for read/write, and distribute rc across iovecs for readv/writev so no stale bytes leak.

3. Crash: ptrace(PTRACE_SYSCALL): No such process

rpc_tracer.cc:351: failed: ptrace(PTRACE_SYSCALL, tid, nullptr, nullptr): No such process
A tracee can die between waitpid() and the PTRACE_SYSCALL restart, so the syscall fails with ESRCH and the hard KJ_SYSCALL aborts the whole tracer.

Fix: tolerate ESRCH

Copilot AI review requested due to automatic review settings July 2, 2026 18:07

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens the syscall-based Cap’n Proto RPC tracer by fixing a GCC build break and addressing two tracer crash paths observed during real-world tracing, improving correctness of captured byte streams and tracer robustness.

Changes:

  • Fixes a build failure by including the proper header for clock_gettime(CLOCK_MONOTONIC, ...).
  • Prevents Cap’n Proto stream corruption by reassembling only the bytes actually read/written (rc) for read/write and distributing rc across iovecs for readv/writev.
  • Avoids aborting the tracer when a tracee disappears between waitpid() and PTRACE_SYSCALL by tolerating ESRCH, and improves waitpid() error handling.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
src/rpc_tracer.h Updates syscall handler signatures to use int64_t rc for syscall return values.
src/rpc_tracer.cc Uses actual transferred byte counts for reassembly; tolerates ESRCH on PTRACE_SYSCALL; adds waitpid() error handling.
src/rpc_message_recorder.cc Adds <time.h> to fix missing CLOCK_MONOTONIC / clock_gettime declarations.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/rpc_tracer.cc
Comment on lines +125 to +128
// Use rc, not count: a partial write transfers fewer bytes than requested.
const size_t valid_len = static_cast<size_t>(rc);
std::vector<char> buf(valid_len);
ReadProcessMemory(tid, addr, valid_len, buf.data());
Comment thread src/rpc_tracer.cc
Comment on lines +208 to +211
// Use rc, not count: bytes beyond the actual read are stale and corrupt reassembly.
const size_t valid_len = static_cast<size_t>(rc);
std::vector<char> buf(valid_len);
ReadProcessMemory(tid, addr, valid_len, buf.data());
Comment thread src/rpc_tracer.cc
Comment on lines 163 to 165
struct iovec* iov = reinterpret_cast<struct iovec*>(alloca(sizeof(struct iovec) * iov_count));

ReadProcessMemory(tid, iov_addr, sizeof(struct iovec) * iov_count, reinterpret_cast<char*>(iov));
Comment thread src/rpc_tracer.cc
Comment on lines 245 to 247
struct iovec* iov = reinterpret_cast<struct iovec*>(alloca(sizeof(struct iovec) * iov_count));

ReadProcessMemory(tid, iov_addr, sizeof(struct iovec) * iov_count, reinterpret_cast<char*>(iov));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants