strawberry/tests/process_monitor_live_smoke.cpp
trvon f69fcb0e19
Some checks failed
Build and Test / build (debug, clang) (push) Has been cancelled
Build and Test / build (debug, gcc) (push) Has been cancelled
Build and Test / build (release, clang) (push) Has been cancelled
Build and Test / build (release, gcc) (push) Has been cancelled
Build and Test / docker (push) Has been cancelled
Attach the fork tracepoint on current kernels
sched_process_fork's comm fields became 4-byte __data_loc offsets in
newer kernels, moving child_pid from offset 44 to 20. The hand-written
tracepoint struct read past the event, so the kernel refused to attach
the program and parent-PID enrichment was always off ("Fork tracepoint
✘" in the app). The struct is now CO-RE (preserve_access_index), so
parent_pid/child_pid relocate against the running kernel's BTF. Checked
in a privileged container on kernel 7.2: attaches now, did not before.

The exec smoke test reports whether the tracepoint attached and, when it
did, requires a parent pid on the exec event. The eBPF smokes still
miss the test's own exec inside the container (an older problem, noted
in the live-test runner and tracked separately).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 21:33:39 -06:00

162 lines
5.2 KiB
C++

#include "strawberry/process_monitor.hpp"
#include <chrono>
#include <condition_variable>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <mutex>
#include <string>
#include <thread>
#include <vector>
#if defined(__linux__)
#include <sys/mount.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#endif
#ifndef STRAWBERRY_HAVE_LIBBPF
#define STRAWBERRY_HAVE_LIBBPF 0
#endif
namespace {
#if defined(__linux__) && STRAWBERRY_HAVE_LIBBPF
bool ends_with(const std::string& value, const std::string& suffix) {
return value.size() >= suffix.size() &&
value.compare(value.size() - suffix.size(), suffix.size(), suffix) == 0;
}
bool ensure_tracefs_mounted() {
if (::mount("tracefs", "/sys/kernel/tracing", "tracefs", 0, nullptr) == 0) {
return true;
}
if (errno == EBUSY) {
return true;
}
std::cerr << "warning: mount(tracefs) failed: " << std::strerror(errno) << std::endl;
return false;
}
#endif
} // namespace
int main() {
#if !defined(__linux__) || !STRAWBERRY_HAVE_LIBBPF
std::cout << "SKIP: process_monitor_live_smoke requires a Linux build with libbpf support" << std::endl;
return 0;
#else
::setenv("STRAWBERRY_PROCESS_MONITOR_BACKEND", "bpf", 1);
(void) ensure_tracefs_mounted();
strawberry::ProcessMonitor monitor;
if (!monitor.initialize()) {
std::cerr << "Failed to initialize ProcessMonitor with forced BPF backend." << std::endl;
std::cerr << "Tip: run this on Linux with sufficient privileges (for example, inside a privileged container)." << std::endl;
return 1;
}
std::mutex mutex;
std::condition_variable cv;
std::vector<strawberry::ProcessInfo> events;
if (!monitor.start([&](const strawberry::ProcessInfo& info) {
{
std::lock_guard<std::mutex> lock(mutex);
events.push_back(info);
}
cv.notify_all();
})) {
std::cerr << "Failed to start ProcessMonitor." << std::endl;
return 1;
}
pid_t child = ::fork();
if (child < 0) {
std::cerr << "fork() failed: " << std::strerror(errno) << std::endl;
monitor.stop();
return 1;
}
if (child == 0) {
char* const argv[] = {
const_cast<char*>("echo"),
const_cast<char*>("strawberry_process_monitor_live_smoke"),
nullptr,
};
::execv("/bin/echo", argv);
std::perror("execv");
_exit(127);
}
int status = 0;
(void) ::waitpid(child, &status, 0);
strawberry::ProcessInfo matched_event;
bool observed = false;
{
std::unique_lock<std::mutex> lock(mutex);
observed = cv.wait_for(lock, std::chrono::seconds(5), [&]() {
for (const auto& event : events) {
if (event.executable_path == "/bin/echo" || ends_with(event.executable_path, "/echo")) {
matched_event = event;
return true;
}
}
return false;
});
}
const bool fork_tracepoint = monitor.fork_tracepoint_attached();
monitor.stop();
// Optional enrichment, but it should attach on current kernels (it failed
// on 7.x until the tracepoint struct used CO-RE). When attached, the
// exec event must carry a parent pid from it.
std::cout << "fork tracepoint attached: " << (fork_tracepoint ? "yes" : "no") << std::endl;
if (!observed) {
std::cerr << "Did not observe the expected exec event for child PID " << child << std::endl;
std::lock_guard<std::mutex> lock(mutex);
for (const auto& event : events) {
std::cerr << "Observed event: pid=" << event.pid
<< " executable_path=" << event.executable_path
<< " cmdline=" << event.cmdline << std::endl;
}
return 1;
}
if (matched_event.image_identity.empty()) {
std::cerr << "Observed exec event, but the executable image was not resolved." << std::endl;
return 1;
}
if (matched_event.cmdline.find("strawberry_process_monitor_live_smoke") == std::string::npos) {
std::cerr << "Observed exec event, but the cmdline did not contain the expected argument." << std::endl;
std::cerr << "cmdline=" << matched_event.cmdline << std::endl;
return 1;
}
if (fork_tracepoint && matched_event.parent_pid <= 0) {
std::cerr << "fork tracepoint attached, but the exec event has no parent pid" << std::endl;
return 1;
}
if (matched_event.pid != child) {
std::cout << "NOTE: observed exec event path match, but callback pid " << matched_event.pid
<< " differed from child pid " << child
<< " (possible namespace/kernel pid difference)." << std::endl;
}
std::cout << "Observed exec event:" << std::endl;
std::cout << " pid=" << matched_event.pid << std::endl;
std::cout << " executable_path=" << matched_event.executable_path << std::endl;
std::cout << " cmdline=" << matched_event.cmdline << std::endl;
std::cout << " image=" << matched_event.image_identity << std::endl;
std::cout << "PASS: live libbpf exec-event smoke succeeded" << std::endl;
return 0;
#endif
}