Skip to content

refactor: isolate PTX pass execution in runner - #583

Draft
yunwei37 wants to merge 2 commits into
masterfrom
codex/ptxpass-runner-isolation
Draft

refactor: isolate PTX pass execution in runner#583
yunwei37 wants to merge 2 commits into
masterfrom
codex/ptxpass-runner-isolation

Conversation

@yunwei37

@yunwei37 yunwei37 commented Jun 15, 2026

Copy link
Copy Markdown
Member

Summary

Split the PTX pass isolation change from the GPU runtime race fix.

  • add a dedicated bpftime_ptxpass_runner executable for PTX pass print_config and process_input calls
  • run pass libraries in a subprocess with LD_PRELOAD and LD_AUDIT cleared
  • reserve runner stdout for JSON by redirecting pass stdout to stderr
  • restore executable permissions when artifact transfer drops the executable bit

Current status

This remains a draft and is not merge-ready. The prerequisite GPU race fix was merged in #582, but this branch now conflicts with master. After rebasing, the remaining non-outdated review findings must be addressed: bounded process output allocation, inherited file descriptors in the spawned runner, and config pointer constness.

Validation recorded on the branch

  • git diff --check
  • configured and built the GPU/runner targets with CUDA 12.9 and Ninja
  • the earlier combined branch passed its checks before this change was split out

Copilot AI review requested due to automatic review settings June 15, 2026 21:07

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 introduces a dedicated bpftime_ptxpass_runner executable to execute PTX pass print_config/process_input outside the CUDA target process, aiming to improve isolation and reduce coupling between injected CUDA runtime behavior and PTX pass dynamic loading/execution.

Changes:

  • Add bpftime_ptxpass_runner binary that dlopen()s a pass library and invokes print_config / process_input, keeping stdout reserved for JSON output.
  • Update nv_attach_impl to invoke PTX passes via the runner subprocess (clearing LD_PRELOAD/LD_AUDIT), instead of dlmopen/dlsym in-process.
  • Extend build config to generate and use DEFAULT_PTX_PASS_RUNNER_EXECUTABLE.

Reviewed changes

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

File Description
attach/nv_attach_impl/pass/ptxpass_runner.cpp New runner executable that loads PTX pass libs and prints JSON results to stdout while redirecting pass stdout to stderr.
attach/nv_attach_impl/nv_attach_impl.hpp Removes in-process PTX pass function pointer/handle storage from pass config struct.
attach/nv_attach_impl/nv_attach_impl.cpp Adds subprocess-based PTX pass execution helper and switches config/patch paths to use it.
attach/nv_attach_impl/CMakeLists.txt Builds the new runner and emits its path into generated config header.

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

Comment on lines +120 to +132
std::vector<std::string> args = { mode, pass_library.string() };
boost::process::environment child_env =
boost::this_process::environment();
child_env["LD_AUDIT"] = "";
child_env["LD_PRELOAD"] = "";
if (access(runner_path.c_str(), X_OK) != 0 && errno == EACCES) {
std::error_code ec;
std::filesystem::permissions(
runner_path,
std::filesystem::perms::owner_exec |
std::filesystem::perms::group_exec |
std::filesystem::perms::others_exec,
std::filesystem::perm_options::add, ec);
Comment on lines +139 to +146
try {
boost::process::child child(
runner_path.string(), boost::process::args(args),
boost::process::env = child_env,
boost::process::std_in < stdin_path.string(),
boost::process::std_out > stdout_path.string(),
boost::process::std_err > stderr_path.string());
child.wait();
@yunwei37
yunwei37 force-pushed the codex/ptxpass-runner-isolation branch from 2b2ec29 to c9d205a Compare June 16, 2026 00:33
@yunwei37
yunwei37 requested a review from Copilot June 16, 2026 00:36

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

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

Comment on lines +54 to +60
if (!parse_size(argv[++i], output_bytes) ||
output_bytes == 0 ||
output_bytes >
(size_t)std::numeric_limits<int>::max()) {
std::cerr << "Invalid --output-bytes value\n";
return 64;
}
Comment on lines +207 to +215
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
std::ifstream efs(stderr_path);
std::string err((std::istreambuf_iterator<char>(efs)),
std::istreambuf_iterator<char>());
SPDLOG_ERROR("PTX pass runner {} failed for {} with status {}: {}",
runner_path.c_str(), pass_library.c_str(), status,
err);
return std::nullopt;
}
Comment on lines +1149 to +1153
current_ptx));
}
std::lock_guard<std::mutex> _guard(map_mutex);
ptx_out["patched." + file_name] = std::make_tuple(
current_ptx, should_add_trampoline);
});
}();
@yunwei37
yunwei37 force-pushed the codex/ptxpass-runner-isolation branch from c9d205a to 1ad684d Compare June 16, 2026 00:43
@yunwei37
yunwei37 requested a review from Copilot June 16, 2026 00:43

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

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

Comment on lines +203 to +208
int status = 0;
if (waitpid(child_pid, &status, 0) < 0) {
SPDLOG_ERROR("waitpid failed for PTX pass runner {}: {}",
runner_path.c_str(), strerror(errno));
return std::nullopt;
}
Comment on lines +724 to +726
auto json = nlohmann::json::parse(*output);
ptxpass::pass_config::from_json(json, config);
SPDLOG_INFO("Retrived config of {}", path.c_str());
SPDLOG_INFO("Retrieved config of {}", path.c_str());
@yunwei37
yunwei37 force-pushed the codex/ptxpass-runner-isolation branch from 1ad684d to cd30f81 Compare June 16, 2026 00:48
@yunwei37
yunwei37 requested a review from Copilot June 16, 2026 00:49

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

Comment on lines +77 to +89
auto restore_stdout = [&]() -> bool {
fflush(stdout);
if (dup2(saved_stdout, STDOUT_FILENO) == -1) {
std::cerr << "Unable to restore stdout: "
<< std::strerror(errno) << "\n";
close(saved_stdout);
saved_stdout = -1;
return false;
}
close(saved_stdout);
saved_stdout = -1;
return true;
};
@yunwei37
yunwei37 force-pushed the codex/ptxpass-runner-isolation branch from cd30f81 to 044de31 Compare June 16, 2026 00:54
@yunwei37
yunwei37 requested a review from Copilot June 16, 2026 00:54

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

Comment on lines +49 to +67
const std::string mode = argv[1];
const char *library_path = argv[2];
size_t output_bytes = kDefaultOutputBytes;
for (int i = 3; i < argc; i++) {
if (std::strcmp(argv[i], "--output-bytes") == 0 &&
i + 1 < argc) {
if (!parse_size(argv[++i], output_bytes) ||
output_bytes == 0 || output_bytes > kMaxOutputBytes ||
output_bytes >
(size_t)std::numeric_limits<int>::max()) {
std::cerr << "Invalid --output-bytes value\n";
return 64;
}
continue;
}
usage(argv[0]);
return 64;
}

@yunwei37
yunwei37 force-pushed the codex/ptxpass-runner-isolation branch from 044de31 to fce5e2c Compare June 16, 2026 01:00
@yunwei37
yunwei37 requested a review from Copilot June 16, 2026 01:01

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

Comment on lines 83 to 90
struct pass_cfg_with_exec_path {
std::filesystem::path executable_path;
ptxpass::pass_config::PassConfig pass_config;
print_config_fn print_config;
process_input_fn process_input;

void *handle;

pass_cfg_with_exec_path(std::filesystem::path path,
ptxpass::pass_config::PassConfig config,
print_config_fn print_config,
process_input_fn process_input, void *handle)
: executable_path(path), pass_config(config),
print_config(print_config), process_input(process_input),
handle(handle)
ptxpass::pass_config::PassConfig config)
: executable_path(path), pass_config(config)
{
@yunwei37
yunwei37 force-pushed the codex/ptxpass-runner-isolation branch from fce5e2c to c572c5b Compare June 16, 2026 01:07
@yunwei37
yunwei37 requested a review from Copilot June 16, 2026 01:07

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

Comment on lines +19 to +21
constexpr size_t kDefaultConfigOutputBytes = 256U << 10;
constexpr size_t kDefaultProcessOutputBytes = 64U << 20;
constexpr size_t kMaxOutputBytes = 64U << 20;
@yunwei37
yunwei37 force-pushed the codex/ptxpass-runner-isolation branch from c572c5b to 26af3a6 Compare June 16, 2026 01:15
@yunwei37
yunwei37 requested a review from Copilot June 16, 2026 01:16

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

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

Comment on lines +201 to +209
pid_t child_pid = -1;
spawn_rc = posix_spawnp(&child_pid, runner_path.c_str(), &file_actions,
nullptr, argv.data(), envp.data());
if (spawn_rc != 0) {
SPDLOG_ERROR("Unable to spawn PTX pass runner {} for {}: {}",
runner_path.c_str(), pass_library.c_str(),
strerror(spawn_rc));
return std::nullopt;
}
Comment on lines +124 to +125
std::cout.write(output.data(),
strnlen(output.data(), output.size()));
Comment on lines +144 to +145
std::cout.write(output.data(),
strnlen(output.data(), output.size()));
Comment on lines 79 to 81
std::optional<std::string> extras;
struct pass_cfg_with_exec_path *config;
struct pass_cfg_with_library_path *config;
};
@yunwei37
yunwei37 force-pushed the codex/ptxpass-runner-isolation branch from 26af3a6 to b815e03 Compare June 16, 2026 01:25
@yunwei37
yunwei37 requested a review from Copilot June 16, 2026 01:25

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

Comment on lines +122 to +123
if (stdin_payload != nullptr)
ofs << *stdin_payload;
@yunwei37
yunwei37 force-pushed the codex/ptxpass-runner-isolation branch from b815e03 to 6f161ab Compare June 16, 2026 01:33
@yunwei37
yunwei37 requested a review from Copilot June 16, 2026 01:33

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

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

Comment on lines +751 to 755
SPDLOG_INFO("Found path: {}, loading config..", path.c_str());
ptxpass::pass_config::PassConfig config;
auto output = run_ptxpass_runner("--config", path, nullptr);
if (!output)
continue;
Comment on lines +1185 to +1186
if (!output)
return;
@yunwei37
yunwei37 force-pushed the codex/ptxpass-runner-isolation branch from 6f161ab to c33d91f Compare June 16, 2026 01:41
@yunwei37
yunwei37 requested a review from Copilot June 16, 2026 01:41

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

return 64;
}

fflush(stdout);
@yunwei37
yunwei37 force-pushed the codex/ptxpass-runner-isolation branch from c33d91f to 38a2ffe Compare June 16, 2026 01:48
@yunwei37
yunwei37 requested a review from Copilot June 16, 2026 01:48

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

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

@yunwei37
yunwei37 marked this pull request as draft June 16, 2026 04:35
Base automatically changed from codex/fix-gpu-examples to master June 16, 2026 06:04
@yunwei37 yunwei37 changed the title [codex] isolate PTX pass execution in runner refactor: isolate PTX pass execution in runner Jul 19, 2026
@yunwei37

yunwei37 commented Jul 19, 2026

Copy link
Copy Markdown
Member Author

The PR title/body now match the current scope, and the review threads have been rechecked. This draft is currently blocked by a conflict with master plus non-outdated findings on the 64 MiB per-run output allocation, inherited file descriptors in the runner subprocess, and config pointer constness. Because conflict resolution can change the affected code, these should be fixed and re-reviewed after rebasing; the current head is not merge-ready.

AI-generated response; a maintainer will review and follow up later

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants