mirror of
https://github.com/elicpeter/nyx.git
synced 2026-06-15 20:05:13 +02:00
[pitboss] phase 16: Track B — Rust + C + C++ harness emitter shapes
This commit is contained in:
parent
bf62ae6b9f
commit
76087f931a
31 changed files with 1969 additions and 100 deletions
|
|
@ -808,6 +808,165 @@ fn compute_php_lockfile_hash(workdir: &Path) -> String {
|
|||
format!("{:016x}", u64::from_le_bytes(out.as_bytes()[..8].try_into().unwrap()))
|
||||
}
|
||||
|
||||
// ── C build sandbox ───────────────────────────────────────────────────────────
|
||||
|
||||
/// Prepare a compiled C binary for `spec`.
|
||||
///
|
||||
/// Checks a build cache keyed on `(main.c + entry.c hash, "c", toolchain_id)`.
|
||||
/// On a cache hit returns immediately; otherwise runs
|
||||
/// `cc -O0 -g -o nyx_harness main.c` in `workdir`.
|
||||
///
|
||||
/// Build isolation is NOT yet implemented (deferred). `cc` runs on the host.
|
||||
pub fn prepare_c(spec: &HarnessSpec, workdir: &Path) -> Result<BuildResult, BuildError> {
|
||||
let source_hash = compute_c_source_hash(workdir);
|
||||
let cache_path = build_cache_path(&source_hash, "c", &spec.toolchain_id)?;
|
||||
|
||||
let binary = cache_path.join("nyx_harness");
|
||||
if binary.exists() {
|
||||
return Ok(BuildResult {
|
||||
venv_path: cache_path,
|
||||
cache_hit: true,
|
||||
duration: std::time::Duration::ZERO,
|
||||
});
|
||||
}
|
||||
|
||||
let start = std::time::Instant::now();
|
||||
const MAX_ATTEMPTS: u32 = 2;
|
||||
const BACKOFF: [u64; 2] = [1, 4];
|
||||
let mut last_err = String::new();
|
||||
|
||||
for attempt in 0..MAX_ATTEMPTS {
|
||||
if attempt > 0 {
|
||||
std::thread::sleep(std::time::Duration::from_secs(BACKOFF[attempt as usize - 1]));
|
||||
}
|
||||
let _ = std::fs::remove_dir_all(&cache_path);
|
||||
std::fs::create_dir_all(&cache_path)?;
|
||||
|
||||
match try_build_c_binary(workdir, &binary) {
|
||||
Ok(()) => {
|
||||
return Ok(BuildResult {
|
||||
venv_path: cache_path,
|
||||
cache_hit: false,
|
||||
duration: start.elapsed(),
|
||||
});
|
||||
}
|
||||
Err(e) => {
|
||||
last_err = e;
|
||||
let _ = std::fs::remove_file(&binary);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Err(BuildError::BuildFailed { stderr: last_err, attempts: MAX_ATTEMPTS })
|
||||
}
|
||||
|
||||
fn try_build_c_binary(workdir: &Path, binary_dest: &Path) -> Result<(), String> {
|
||||
let cc_bin = std::env::var("NYX_CC_BIN").unwrap_or_else(|_| "cc".to_owned());
|
||||
let output = Command::new(&cc_bin)
|
||||
.args(["-O0", "-g", "-o", binary_dest.to_str().unwrap_or("nyx_harness"), "main.c"])
|
||||
.current_dir(workdir)
|
||||
.env_clear()
|
||||
.env("PATH", std::env::var("PATH").unwrap_or_default())
|
||||
.env("HOME", std::env::var("HOME").unwrap_or_default())
|
||||
.output()
|
||||
.map_err(|e| format!("cc: {e}"))?;
|
||||
|
||||
if !output.status.success() {
|
||||
return Err(String::from_utf8_lossy(&output.stderr).into_owned());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn compute_c_source_hash(workdir: &Path) -> String {
|
||||
let mut h = Hasher::new();
|
||||
for fname in &["main.c", "entry.c", "Makefile"] {
|
||||
if let Ok(content) = std::fs::read(workdir.join(fname)) {
|
||||
h.update(fname.as_bytes());
|
||||
h.update(&content);
|
||||
}
|
||||
}
|
||||
let out = h.finalize();
|
||||
format!("{:016x}", u64::from_le_bytes(out.as_bytes()[..8].try_into().unwrap()))
|
||||
}
|
||||
|
||||
// ── C++ build sandbox ─────────────────────────────────────────────────────────
|
||||
|
||||
/// Prepare a compiled C++ binary for `spec`.
|
||||
pub fn prepare_cpp(spec: &HarnessSpec, workdir: &Path) -> Result<BuildResult, BuildError> {
|
||||
let source_hash = compute_cpp_source_hash(workdir);
|
||||
let cache_path = build_cache_path(&source_hash, "cpp", &spec.toolchain_id)?;
|
||||
|
||||
let binary = cache_path.join("nyx_harness");
|
||||
if binary.exists() {
|
||||
return Ok(BuildResult {
|
||||
venv_path: cache_path,
|
||||
cache_hit: true,
|
||||
duration: std::time::Duration::ZERO,
|
||||
});
|
||||
}
|
||||
|
||||
let start = std::time::Instant::now();
|
||||
const MAX_ATTEMPTS: u32 = 2;
|
||||
const BACKOFF: [u64; 2] = [1, 4];
|
||||
let mut last_err = String::new();
|
||||
|
||||
for attempt in 0..MAX_ATTEMPTS {
|
||||
if attempt > 0 {
|
||||
std::thread::sleep(std::time::Duration::from_secs(BACKOFF[attempt as usize - 1]));
|
||||
}
|
||||
let _ = std::fs::remove_dir_all(&cache_path);
|
||||
std::fs::create_dir_all(&cache_path)?;
|
||||
|
||||
match try_build_cpp_binary(workdir, &binary) {
|
||||
Ok(()) => {
|
||||
return Ok(BuildResult {
|
||||
venv_path: cache_path,
|
||||
cache_hit: false,
|
||||
duration: start.elapsed(),
|
||||
});
|
||||
}
|
||||
Err(e) => {
|
||||
last_err = e;
|
||||
let _ = std::fs::remove_file(&binary);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Err(BuildError::BuildFailed { stderr: last_err, attempts: MAX_ATTEMPTS })
|
||||
}
|
||||
|
||||
fn try_build_cpp_binary(workdir: &Path, binary_dest: &Path) -> Result<(), String> {
|
||||
let cxx_bin = std::env::var("NYX_CXX_BIN").unwrap_or_else(|_| {
|
||||
// Prefer c++ which resolves to the system default compiler driver.
|
||||
"c++".to_owned()
|
||||
});
|
||||
let output = Command::new(&cxx_bin)
|
||||
.args(["-O0", "-g", "-std=c++17", "-o", binary_dest.to_str().unwrap_or("nyx_harness"), "main.cpp"])
|
||||
.current_dir(workdir)
|
||||
.env_clear()
|
||||
.env("PATH", std::env::var("PATH").unwrap_or_default())
|
||||
.env("HOME", std::env::var("HOME").unwrap_or_default())
|
||||
.output()
|
||||
.map_err(|e| format!("c++: {e}"))?;
|
||||
|
||||
if !output.status.success() {
|
||||
return Err(String::from_utf8_lossy(&output.stderr).into_owned());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn compute_cpp_source_hash(workdir: &Path) -> String {
|
||||
let mut h = Hasher::new();
|
||||
for fname in &["main.cpp", "entry.cpp", "CMakeLists.txt"] {
|
||||
if let Ok(content) = std::fs::read(workdir.join(fname)) {
|
||||
h.update(fname.as_bytes());
|
||||
h.update(&content);
|
||||
}
|
||||
}
|
||||
let out = h.finalize();
|
||||
format!("{:016x}", u64::from_le_bytes(out.as_bytes()[..8].try_into().unwrap()))
|
||||
}
|
||||
|
||||
// ── Docker-isolated build step functions ─────────────────────────────────────
|
||||
//
|
||||
// Each function runs the language's build tool inside a Docker container with
|
||||
|
|
|
|||
|
|
@ -180,19 +180,22 @@ mod tests {
|
|||
use crate::symbol::Lang;
|
||||
|
||||
#[test]
|
||||
fn build_unsupported_lang_returns_err() {
|
||||
// C is not supported (no emitter exists for it).
|
||||
fn build_unsupported_entry_kind_returns_err() {
|
||||
// The Python emitter advertises a specific entry-kind set; an
|
||||
// unsupported entry kind short-circuits with
|
||||
// [`UnsupportedReason::EntryKindUnsupported`] before any harness
|
||||
// source is generated.
|
||||
let spec = HarnessSpec {
|
||||
finding_id: "0000000000000001".into(),
|
||||
entry_file: "main.c".into(),
|
||||
entry_name: "handleRequest".into(),
|
||||
entry_kind: EntryKind::Function,
|
||||
lang: Lang::C,
|
||||
toolchain_id: "c-stable".into(),
|
||||
entry_file: "src/app.py".into(),
|
||||
entry_name: "handler".into(),
|
||||
entry_kind: EntryKind::LibraryApi,
|
||||
lang: Lang::Python,
|
||||
toolchain_id: "python-3".into(),
|
||||
payload_slot: PayloadSlot::Param(0),
|
||||
expected_cap: Cap::SQL_QUERY,
|
||||
constraint_hints: vec![],
|
||||
sink_file: "main.c".into(),
|
||||
sink_file: "src/app.py".into(),
|
||||
sink_line: 5,
|
||||
spec_hash: "0000000000000000".into(),
|
||||
derivation: crate::dynamic::spec::SpecDerivationStrategy::FromFlowSteps,
|
||||
|
|
|
|||
|
|
@ -1,22 +1,108 @@
|
|||
//! C harness emitter (stub).
|
||||
//! C harness emitter.
|
||||
//!
|
||||
//! No harness source is generated yet — `emit` returns
|
||||
//! [`UnsupportedReason::LangUnsupported`]. The module exists so that
|
||||
//! [`crate::dynamic::lang::entry_kinds_supported`] can advertise the entry
|
||||
//! kinds Track B will deliver (Phase 16: `main(argc, argv)`,
|
||||
//! `LLVMFuzzerTestOneInput`, free functions with `(const char*, size_t)` or
|
||||
//! `(int, char**)` shapes) and so the verifier can surface
|
||||
//! `Inconclusive(EntryKindUnsupported { … })` instead of dropping C findings.
|
||||
//! Phase 16 (Track B Rust + C/C++ vertical) replaces the stub body with
|
||||
//! dispatch over [`CShape`] — the cross product of [`EntryKind`] and a
|
||||
//! lightweight per-file shape detector that inspects the entry file for
|
||||
//! `main(int argc, char *argv[])`, libFuzzer's `LLVMFuzzerTestOneInput`,
|
||||
//! and free functions with `(const char*, size_t)` signatures.
|
||||
//!
|
||||
//! Each shape emits a single `main.c` that:
|
||||
//! 1. Reads the payload from `NYX_PAYLOAD` / `NYX_PAYLOAD_B64` env vars.
|
||||
//! 2. `#include`s `entry.c` (the user's vulnerable code) and dispatches
|
||||
//! via the per-shape adapter.
|
||||
//!
|
||||
//! Build step: `prepare_c()` in `build_sandbox.rs` runs
|
||||
//! `cc -O0 -o nyx_harness main.c` in the workdir.
|
||||
//!
|
||||
//! File layout in workdir:
|
||||
//! ```text
|
||||
//! main.c ← harness entry point (generated, includes entry.c)
|
||||
//! entry.c ← user entry source (copied from project)
|
||||
//! Makefile ← optional, generated for reference
|
||||
//! ```
|
||||
//!
|
||||
//! Payload slot support:
|
||||
//! - `PayloadSlot::Param(0)` — pass payload as the first parameter (string
|
||||
//! or `(buf, len)` pair depending on shape).
|
||||
//! - `PayloadSlot::EnvVar(name)` — set env var before invoking entry.
|
||||
//! - `PayloadSlot::Argv(n)` — `main(argc, argv)` shape: appended to argv.
|
||||
|
||||
use crate::dynamic::lang::{HarnessSource, LangEmitter};
|
||||
use crate::dynamic::spec::{EntryKind, HarnessSpec};
|
||||
use crate::dynamic::spec::{EntryKind, HarnessSpec, PayloadSlot};
|
||||
use crate::evidence::UnsupportedReason;
|
||||
use std::path::PathBuf;
|
||||
|
||||
/// Zero-sized [`LangEmitter`] handle for C.
|
||||
pub struct CEmitter;
|
||||
|
||||
/// Entry kinds the C emitter intends to support once Phase 16 lands.
|
||||
const SUPPORTED: &[EntryKind] = &[EntryKind::Function];
|
||||
/// Entry kinds the C emitter understands after Phase 16.
|
||||
///
|
||||
/// `Function` covers free functions (libfuzzer-style + plain (const
|
||||
/// char*, size_t)). `CliSubcommand` covers `main(argc, argv)`.
|
||||
/// `LibraryApi` covers libFuzzer `LLVMFuzzerTestOneInput`.
|
||||
const SUPPORTED: &[EntryKind] = &[
|
||||
EntryKind::Function,
|
||||
EntryKind::CliSubcommand,
|
||||
EntryKind::LibraryApi,
|
||||
];
|
||||
|
||||
// ── Phase 16: shape detector ─────────────────────────────────────────────────
|
||||
|
||||
/// Concrete per-file shape resolved by reading the entry source.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum CShape {
|
||||
/// `int main(int argc, char *argv[])`. Harness embeds payload into
|
||||
/// argv and calls `main(argc, argv)` directly.
|
||||
MainArgv,
|
||||
/// libFuzzer-style: `int LLVMFuzzerTestOneInput(const uint8_t *data,
|
||||
/// size_t size)`. Harness invokes with `payload` bytes + length.
|
||||
LibfuzzerEntry,
|
||||
/// Free function with `(const char *, size_t)` or `(const char *)`
|
||||
/// signature. Harness invokes directly.
|
||||
FreeFn,
|
||||
}
|
||||
|
||||
impl CShape {
|
||||
/// Detect the shape from `(spec, source)`.
|
||||
pub fn detect(spec: &HarnessSpec, source: &str) -> Self {
|
||||
let entry = spec.entry_name.as_str();
|
||||
let kind = spec.entry_kind;
|
||||
|
||||
let has_main_argv = (source.contains("int main(") || source.contains("int main ("))
|
||||
&& (source.contains("argc") || source.contains("char *argv")
|
||||
|| source.contains("char* argv") || source.contains("char **argv"));
|
||||
let has_libfuzzer = source.contains("LLVMFuzzerTestOneInput") || entry == "LLVMFuzzerTestOneInput";
|
||||
|
||||
if has_libfuzzer {
|
||||
return Self::LibfuzzerEntry;
|
||||
}
|
||||
if entry == "main" || has_main_argv {
|
||||
return Self::MainArgv;
|
||||
}
|
||||
match kind {
|
||||
EntryKind::CliSubcommand => Self::MainArgv,
|
||||
EntryKind::LibraryApi => Self::LibfuzzerEntry,
|
||||
_ => Self::FreeFn,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Public wrapper: detect the shape for a finalised `HarnessSpec`, reading
|
||||
/// the entry file from disk.
|
||||
pub fn detect_shape(spec: &HarnessSpec) -> CShape {
|
||||
let src = read_entry_source(&spec.entry_file);
|
||||
CShape::detect(spec, &src)
|
||||
}
|
||||
|
||||
fn read_entry_source(entry_file: &str) -> String {
|
||||
let candidates = [PathBuf::from(entry_file), PathBuf::from(".").join(entry_file)];
|
||||
for path in &candidates {
|
||||
if let Ok(s) = std::fs::read_to_string(path) {
|
||||
return s;
|
||||
}
|
||||
}
|
||||
String::new()
|
||||
}
|
||||
|
||||
/// Source of the `__nyx_probe` shim for the (future) C harness (Phase 06 —
|
||||
/// Track C.1). Variadic over `const char *` args; hand-rolled JSON keeps
|
||||
|
|
@ -208,8 +294,8 @@ static void __nyx_install_crash_guard(const char *sink_callee) {
|
|||
}
|
||||
|
||||
impl LangEmitter for CEmitter {
|
||||
fn emit(&self, _spec: &HarnessSpec) -> Result<HarnessSource, UnsupportedReason> {
|
||||
Err(UnsupportedReason::LangUnsupported)
|
||||
fn emit(&self, spec: &HarnessSpec) -> Result<HarnessSource, UnsupportedReason> {
|
||||
emit(spec)
|
||||
}
|
||||
|
||||
fn entry_kinds_supported(&self) -> &'static [EntryKind] {
|
||||
|
|
@ -218,18 +304,198 @@ impl LangEmitter for CEmitter {
|
|||
|
||||
fn entry_kind_hint(&self, attempted: EntryKind) -> String {
|
||||
format!(
|
||||
"c emitter is a stub; once Phase 16 (Track B Rust + C/C++ vertical) lands it will support {SUPPORTED:?} plus libFuzzer + main(argc, argv) shapes — attempted `EntryKind::{attempted}`"
|
||||
"c emitter supports {SUPPORTED:?}; this finding's enclosing context is `EntryKind::{attempted}` — see Phase 16 shape dispatch (main / libFuzzer / free function)"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Emit a C harness for `spec`.
|
||||
pub fn emit(spec: &HarnessSpec) -> Result<HarnessSource, UnsupportedReason> {
|
||||
let shape = detect_shape(spec);
|
||||
|
||||
match (&spec.payload_slot, shape) {
|
||||
(PayloadSlot::Param(0) | PayloadSlot::EnvVar(_), _) => {}
|
||||
(PayloadSlot::Argv(_), CShape::MainArgv) => {}
|
||||
_ => return Err(UnsupportedReason::PayloadSlotUnsupported),
|
||||
}
|
||||
|
||||
let main_c = generate_main_c(spec, shape);
|
||||
let makefile = generate_makefile();
|
||||
|
||||
Ok(HarnessSource {
|
||||
source: main_c,
|
||||
filename: "main.c".into(),
|
||||
command: vec!["./nyx_harness".into()],
|
||||
extra_files: vec![("Makefile".into(), makefile)],
|
||||
entry_subpath: Some("entry.c".into()),
|
||||
})
|
||||
}
|
||||
|
||||
/// Generate the harness `main.c` for the resolved shape.
|
||||
fn generate_main_c(spec: &HarnessSpec, shape: CShape) -> String {
|
||||
let invocation = invoke_for_shape(spec, shape);
|
||||
|
||||
format!(
|
||||
r#"/* Nyx dynamic harness — auto-generated, do not edit (Phase 16 — CShape::{shape:?}). */
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/* Forward declarations: the entry file is appended below via `#include`
|
||||
* so the harness can call user-defined functions without a separate
|
||||
* compilation unit. */
|
||||
static char *nyx_payload(void);
|
||||
|
||||
#include "entry.c"
|
||||
|
||||
int main(int argc, char *argv[]) {{
|
||||
(void)argc; (void)argv;
|
||||
char *payload = nyx_payload();
|
||||
if (!payload) payload = (char*)"";
|
||||
|
||||
{invocation}
|
||||
/* Intentionally no free(payload): payload is either a strdup/b64_decode
|
||||
* heap pointer or a string literal substituted above when allocation
|
||||
* failed. free() on the literal is UB; the process exits immediately
|
||||
* so the kernel reclaims the heap copy. */
|
||||
return 0;
|
||||
}}
|
||||
|
||||
/* Minimal base64 decoder (no external deps). */
|
||||
static int nyx_b64_value(unsigned char c) {{
|
||||
if (c >= 'A' && c <= 'Z') return c - 'A';
|
||||
if (c >= 'a' && c <= 'z') return c - 'a' + 26;
|
||||
if (c >= '0' && c <= '9') return c - '0' + 52;
|
||||
if (c == '+') return 62;
|
||||
if (c == '/') return 63;
|
||||
return -1;
|
||||
}}
|
||||
|
||||
static char *nyx_b64_decode(const char *in) {{
|
||||
size_t n = strlen(in);
|
||||
char *out = (char *)malloc(n + 1);
|
||||
if (!out) return NULL;
|
||||
size_t outi = 0;
|
||||
int buf = 0, bits = 0;
|
||||
for (size_t i = 0; i < n; ++i) {{
|
||||
if (in[i] == '\n' || in[i] == '\r' || in[i] == '=') continue;
|
||||
int v = nyx_b64_value((unsigned char)in[i]);
|
||||
if (v < 0) {{ free(out); return NULL; }}
|
||||
buf = (buf << 6) | v;
|
||||
bits += 6;
|
||||
if (bits >= 8) {{
|
||||
bits -= 8;
|
||||
out[outi++] = (char)((buf >> bits) & 0xFF);
|
||||
}}
|
||||
}}
|
||||
out[outi] = '\0';
|
||||
return out;
|
||||
}}
|
||||
|
||||
static char *nyx_payload(void) {{
|
||||
const char *v = getenv("NYX_PAYLOAD");
|
||||
if (v && *v) {{
|
||||
return strdup(v);
|
||||
}}
|
||||
const char *b64 = getenv("NYX_PAYLOAD_B64");
|
||||
if (b64 && *b64) {{
|
||||
return nyx_b64_decode(b64);
|
||||
}}
|
||||
return strdup("");
|
||||
}}
|
||||
"#,
|
||||
shape = shape,
|
||||
invocation = invocation,
|
||||
)
|
||||
}
|
||||
|
||||
fn invoke_for_shape(spec: &HarnessSpec, shape: CShape) -> String {
|
||||
let entry_fn = &spec.entry_name;
|
||||
match shape {
|
||||
CShape::FreeFn => match &spec.payload_slot {
|
||||
PayloadSlot::EnvVar(name) => format!(
|
||||
" setenv({name:?}, payload, 1);\n {entry_fn}(payload, strlen(payload));\n",
|
||||
),
|
||||
_ => format!(" {entry_fn}(payload, strlen(payload));\n"),
|
||||
},
|
||||
CShape::LibfuzzerEntry => {
|
||||
// libFuzzer: `int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)`.
|
||||
format!(
|
||||
" {entry_fn}((const uint8_t *)payload, strlen(payload));\n",
|
||||
entry_fn = entry_fn,
|
||||
)
|
||||
}
|
||||
CShape::MainArgv => {
|
||||
// Rename the user-supplied entry to `nyx_entry_main` via macro so
|
||||
// it does not collide with the harness `main` symbol when the
|
||||
// entry source defines `int main(...)`. Fixture authors should
|
||||
// expose the entry as a function named in `spec.entry_name`.
|
||||
let pad = match &spec.payload_slot {
|
||||
PayloadSlot::Argv(n) => *n,
|
||||
_ => 0,
|
||||
};
|
||||
let mut buf = String::from(" char *new_argv[8];\n");
|
||||
buf.push_str(" int new_argc = 0;\n");
|
||||
buf.push_str(" new_argv[new_argc++] = (char*)\"nyx_harness\";\n");
|
||||
for _ in 0..pad {
|
||||
buf.push_str(" new_argv[new_argc++] = (char*)\"\";\n");
|
||||
}
|
||||
buf.push_str(" new_argv[new_argc++] = payload;\n");
|
||||
buf.push_str(" new_argv[new_argc] = NULL;\n");
|
||||
buf.push_str(&format!(" {entry_fn}(new_argc, new_argv);\n"));
|
||||
buf
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn generate_makefile() -> String {
|
||||
r#"# Phase 16 — reference Makefile, not used by the runner (the build sandbox
|
||||
# calls cc directly). Kept so reproductions can re-build the harness by hand.
|
||||
CC ?= cc
|
||||
CFLAGS ?= -O0 -g
|
||||
all: nyx_harness
|
||||
nyx_harness: main.c entry.c
|
||||
$(CC) $(CFLAGS) -o nyx_harness main.c
|
||||
clean:
|
||||
rm -f nyx_harness
|
||||
"#
|
||||
.to_owned()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::dynamic::spec::{EntryKind, HarnessSpec, PayloadSlot};
|
||||
use crate::labels::Cap;
|
||||
use crate::symbol::Lang;
|
||||
|
||||
fn make_spec(payload_slot: PayloadSlot) -> HarnessSpec {
|
||||
HarnessSpec {
|
||||
finding_id: "c00000000000001".into(),
|
||||
entry_file: "entry.c".into(),
|
||||
entry_name: "run".into(),
|
||||
entry_kind: EntryKind::Function,
|
||||
lang: Lang::C,
|
||||
toolchain_id: "gcc-stable".into(),
|
||||
payload_slot,
|
||||
expected_cap: Cap::CODE_EXEC,
|
||||
constraint_hints: vec![],
|
||||
sink_file: "entry.c".into(),
|
||||
sink_line: 10,
|
||||
spec_hash: "ctest0000000001".into(),
|
||||
derivation: crate::dynamic::spec::SpecDerivationStrategy::FromFlowSteps,
|
||||
stubs_required: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn entry_kinds_supported_is_non_empty() {
|
||||
assert!(!CEmitter.entry_kinds_supported().is_empty());
|
||||
assert!(CEmitter.entry_kinds_supported().contains(&EntryKind::Function));
|
||||
assert!(CEmitter.entry_kinds_supported().contains(&EntryKind::CliSubcommand));
|
||||
assert!(CEmitter.entry_kinds_supported().contains(&EntryKind::LibraryApi));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -238,4 +504,67 @@ mod tests {
|
|||
assert!(hint.contains("LibraryApi"));
|
||||
assert!(hint.contains("Phase 16"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shape_detect_main_argv() {
|
||||
let src = "int main(int argc, char *argv[]) { return 0; }";
|
||||
let mut spec = make_spec(PayloadSlot::Argv(0));
|
||||
spec.entry_kind = EntryKind::CliSubcommand;
|
||||
spec.entry_name = "main".into();
|
||||
assert_eq!(CShape::detect(&spec, src), CShape::MainArgv);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shape_detect_libfuzzer_entry() {
|
||||
let src = "int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { return 0; }";
|
||||
let mut spec = make_spec(PayloadSlot::Param(0));
|
||||
spec.entry_kind = EntryKind::LibraryApi;
|
||||
spec.entry_name = "LLVMFuzzerTestOneInput".into();
|
||||
assert_eq!(CShape::detect(&spec, src), CShape::LibfuzzerEntry);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shape_detect_free_fn() {
|
||||
let src = "void run(const char *s, size_t n) { (void)s; (void)n; }";
|
||||
let spec = make_spec(PayloadSlot::Param(0));
|
||||
assert_eq!(CShape::detect(&spec, src), CShape::FreeFn);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn emit_produces_source() {
|
||||
let spec = make_spec(PayloadSlot::Param(0));
|
||||
let h = emit(&spec).unwrap();
|
||||
assert_eq!(h.filename, "main.c");
|
||||
assert!(h.source.contains("#include \"entry.c\""));
|
||||
assert!(h.source.contains("run(payload, strlen(payload))"));
|
||||
assert_eq!(h.command, vec!["./nyx_harness"]);
|
||||
assert_eq!(h.entry_subpath, Some("entry.c".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn emit_main_argv_shape_routes_through_new_argv() {
|
||||
let mut spec = make_spec(PayloadSlot::Argv(0));
|
||||
spec.entry_kind = EntryKind::CliSubcommand;
|
||||
spec.entry_name = "nyx_entry_main".into();
|
||||
let h = emit(&spec).unwrap();
|
||||
assert!(h.source.contains("new_argv[new_argc++] = payload"));
|
||||
assert!(h.source.contains("nyx_entry_main(new_argc, new_argv)"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn emit_libfuzzer_shape_passes_bytes() {
|
||||
let mut spec = make_spec(PayloadSlot::Param(0));
|
||||
spec.entry_kind = EntryKind::LibraryApi;
|
||||
spec.entry_name = "LLVMFuzzerTestOneInput".into();
|
||||
let h = emit(&spec).unwrap();
|
||||
assert!(h.source.contains("LLVMFuzzerTestOneInput((const uint8_t *)payload, strlen(payload))"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn emit_makefile_in_extra_files() {
|
||||
let spec = make_spec(PayloadSlot::Param(0));
|
||||
let h = emit(&spec).unwrap();
|
||||
let mk = h.extra_files.iter().find(|(n, _)| n == "Makefile").expect("Makefile must be staged");
|
||||
assert!(mk.1.contains("nyx_harness: main.c entry.c"));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,22 +1,88 @@
|
|||
//! C++ harness emitter (stub).
|
||||
//! C++ harness emitter.
|
||||
//!
|
||||
//! No harness source is generated yet — `emit` returns
|
||||
//! [`UnsupportedReason::LangUnsupported`]. The module exists so that
|
||||
//! [`crate::dynamic::lang::entry_kinds_supported`] can advertise the entry
|
||||
//! kinds Track B will deliver (Phase 16: `main(argc, argv)`,
|
||||
//! `LLVMFuzzerTestOneInput`, free functions with `(const char*, size_t)`)
|
||||
//! and so the verifier can surface `Inconclusive(EntryKindUnsupported { … })`
|
||||
//! instead of dropping C++ findings.
|
||||
//! Phase 16 (Track B Rust + C/C++ vertical) replaces the stub body with
|
||||
//! dispatch over [`CppShape`] — `main(int argc, char *argv[])`, libFuzzer
|
||||
//! `LLVMFuzzerTestOneInput`, and free functions with `(const char*,
|
||||
//! size_t)` or `(const std::string&)` signatures.
|
||||
//!
|
||||
//! File layout in workdir:
|
||||
//! ```text
|
||||
//! main.cpp ← harness entry point (generated, includes entry.cpp)
|
||||
//! entry.cpp ← user entry source (copied from project)
|
||||
//! CMakeLists.txt ← optional, generated for reference
|
||||
//! ```
|
||||
//!
|
||||
//! Build step: `prepare_cpp()` in `build_sandbox.rs` runs
|
||||
//! `g++ -O0 -std=c++17 -o nyx_harness main.cpp` in the workdir.
|
||||
|
||||
use crate::dynamic::lang::{HarnessSource, LangEmitter};
|
||||
use crate::dynamic::spec::{EntryKind, HarnessSpec};
|
||||
use crate::dynamic::spec::{EntryKind, HarnessSpec, PayloadSlot};
|
||||
use crate::evidence::UnsupportedReason;
|
||||
use std::path::PathBuf;
|
||||
|
||||
/// Zero-sized [`LangEmitter`] handle for C++.
|
||||
pub struct CppEmitter;
|
||||
|
||||
/// Entry kinds the C++ emitter intends to support once Phase 16 lands.
|
||||
const SUPPORTED: &[EntryKind] = &[EntryKind::Function];
|
||||
/// Entry kinds the C++ emitter understands after Phase 16.
|
||||
const SUPPORTED: &[EntryKind] = &[
|
||||
EntryKind::Function,
|
||||
EntryKind::CliSubcommand,
|
||||
EntryKind::LibraryApi,
|
||||
];
|
||||
|
||||
// ── Phase 16: shape detector ─────────────────────────────────────────────────
|
||||
|
||||
/// Concrete per-file shape resolved by reading the entry source.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum CppShape {
|
||||
/// `int main(int argc, char *argv[])`.
|
||||
MainArgv,
|
||||
/// libFuzzer-style: `int LLVMFuzzerTestOneInput(const uint8_t *, size_t)`.
|
||||
LibfuzzerEntry,
|
||||
/// Free function with `(const char *, size_t)` or `(const std::string&)`
|
||||
/// signature.
|
||||
FreeFn,
|
||||
}
|
||||
|
||||
impl CppShape {
|
||||
pub fn detect(spec: &HarnessSpec, source: &str) -> Self {
|
||||
let entry = spec.entry_name.as_str();
|
||||
let kind = spec.entry_kind;
|
||||
|
||||
let has_main_argv = (source.contains("int main(") || source.contains("int main ("))
|
||||
&& (source.contains("argc") || source.contains("char *argv")
|
||||
|| source.contains("char* argv") || source.contains("char **argv"));
|
||||
let has_libfuzzer = source.contains("LLVMFuzzerTestOneInput")
|
||||
|| entry == "LLVMFuzzerTestOneInput";
|
||||
|
||||
if has_libfuzzer {
|
||||
return Self::LibfuzzerEntry;
|
||||
}
|
||||
if entry == "main" || has_main_argv {
|
||||
return Self::MainArgv;
|
||||
}
|
||||
match kind {
|
||||
EntryKind::CliSubcommand => Self::MainArgv,
|
||||
EntryKind::LibraryApi => Self::LibfuzzerEntry,
|
||||
_ => Self::FreeFn,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn detect_shape(spec: &HarnessSpec) -> CppShape {
|
||||
let src = read_entry_source(&spec.entry_file);
|
||||
CppShape::detect(spec, &src)
|
||||
}
|
||||
|
||||
fn read_entry_source(entry_file: &str) -> String {
|
||||
let candidates = [PathBuf::from(entry_file), PathBuf::from(".").join(entry_file)];
|
||||
for path in &candidates {
|
||||
if let Ok(s) = std::fs::read_to_string(path) {
|
||||
return s;
|
||||
}
|
||||
}
|
||||
String::new()
|
||||
}
|
||||
|
||||
/// Source of the `__nyx_probe` shim for the (future) C++ harness
|
||||
/// (Phase 06 — Track C.1). Uses `<fstream>` + variadic templates; the
|
||||
|
|
@ -201,8 +267,8 @@ inline void __nyx_install_crash_guard(const char *sink_callee) {
|
|||
}
|
||||
|
||||
impl LangEmitter for CppEmitter {
|
||||
fn emit(&self, _spec: &HarnessSpec) -> Result<HarnessSource, UnsupportedReason> {
|
||||
Err(UnsupportedReason::LangUnsupported)
|
||||
fn emit(&self, spec: &HarnessSpec) -> Result<HarnessSource, UnsupportedReason> {
|
||||
emit(spec)
|
||||
}
|
||||
|
||||
fn entry_kinds_supported(&self) -> &'static [EntryKind] {
|
||||
|
|
@ -211,18 +277,182 @@ impl LangEmitter for CppEmitter {
|
|||
|
||||
fn entry_kind_hint(&self, attempted: EntryKind) -> String {
|
||||
format!(
|
||||
"cpp emitter is a stub; once Phase 16 (Track B Rust + C/C++ vertical) lands it will support {SUPPORTED:?} plus libFuzzer + main(argc, argv) shapes — attempted `EntryKind::{attempted}`"
|
||||
"cpp emitter supports {SUPPORTED:?}; this finding's enclosing context is `EntryKind::{attempted}` — see Phase 16 shape dispatch (main / libFuzzer / free function)"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Emit a C++ harness for `spec`.
|
||||
pub fn emit(spec: &HarnessSpec) -> Result<HarnessSource, UnsupportedReason> {
|
||||
let shape = detect_shape(spec);
|
||||
|
||||
match (&spec.payload_slot, shape) {
|
||||
(PayloadSlot::Param(0) | PayloadSlot::EnvVar(_), _) => {}
|
||||
(PayloadSlot::Argv(_), CppShape::MainArgv) => {}
|
||||
_ => return Err(UnsupportedReason::PayloadSlotUnsupported),
|
||||
}
|
||||
|
||||
let main_cpp = generate_main_cpp(spec, shape);
|
||||
let cmake = generate_cmake();
|
||||
|
||||
Ok(HarnessSource {
|
||||
source: main_cpp,
|
||||
filename: "main.cpp".into(),
|
||||
command: vec!["./nyx_harness".into()],
|
||||
extra_files: vec![("CMakeLists.txt".into(), cmake)],
|
||||
entry_subpath: Some("entry.cpp".into()),
|
||||
})
|
||||
}
|
||||
|
||||
fn generate_main_cpp(spec: &HarnessSpec, shape: CppShape) -> String {
|
||||
let invocation = invoke_for_shape(spec, shape);
|
||||
|
||||
format!(
|
||||
r#"// Nyx dynamic harness — auto-generated, do not edit (Phase 16 — CppShape::{shape:?}).
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
static std::string nyx_payload();
|
||||
|
||||
#include "entry.cpp"
|
||||
|
||||
int main(int argc, char *argv[]) {{
|
||||
(void)argc; (void)argv;
|
||||
std::string payload = nyx_payload();
|
||||
|
||||
{invocation}
|
||||
return 0;
|
||||
}}
|
||||
|
||||
// Minimal base64 decoder (no external deps).
|
||||
static int nyx_b64_value(unsigned char c) {{
|
||||
if (c >= 'A' && c <= 'Z') return c - 'A';
|
||||
if (c >= 'a' && c <= 'z') return c - 'a' + 26;
|
||||
if (c >= '0' && c <= '9') return c - '0' + 52;
|
||||
if (c == '+') return 62;
|
||||
if (c == '/') return 63;
|
||||
return -1;
|
||||
}}
|
||||
|
||||
static std::string nyx_b64_decode(const std::string &in) {{
|
||||
std::string out;
|
||||
int buf = 0, bits = 0;
|
||||
for (char c : in) {{
|
||||
if (c == '\n' || c == '\r' || c == '=') continue;
|
||||
int v = nyx_b64_value(static_cast<unsigned char>(c));
|
||||
if (v < 0) return std::string();
|
||||
buf = (buf << 6) | v;
|
||||
bits += 6;
|
||||
if (bits >= 8) {{
|
||||
bits -= 8;
|
||||
out.push_back(static_cast<char>((buf >> bits) & 0xFF));
|
||||
}}
|
||||
}}
|
||||
return out;
|
||||
}}
|
||||
|
||||
static std::string nyx_payload() {{
|
||||
if (const char *v = std::getenv("NYX_PAYLOAD")) {{
|
||||
if (*v) return std::string(v);
|
||||
}}
|
||||
if (const char *b64 = std::getenv("NYX_PAYLOAD_B64")) {{
|
||||
if (*b64) return nyx_b64_decode(std::string(b64));
|
||||
}}
|
||||
return std::string();
|
||||
}}
|
||||
"#,
|
||||
shape = shape,
|
||||
invocation = invocation,
|
||||
)
|
||||
}
|
||||
|
||||
fn invoke_for_shape(spec: &HarnessSpec, shape: CppShape) -> String {
|
||||
let entry_fn = &spec.entry_name;
|
||||
match shape {
|
||||
CppShape::FreeFn => match &spec.payload_slot {
|
||||
PayloadSlot::EnvVar(name) => format!(
|
||||
" setenv({name:?}, payload.c_str(), 1);\n {entry_fn}(payload.c_str(), payload.size());\n",
|
||||
),
|
||||
_ => format!(" {entry_fn}(payload.c_str(), payload.size());\n"),
|
||||
},
|
||||
CppShape::LibfuzzerEntry => {
|
||||
format!(
|
||||
" {entry_fn}(reinterpret_cast<const uint8_t*>(payload.data()), payload.size());\n",
|
||||
entry_fn = entry_fn,
|
||||
)
|
||||
}
|
||||
CppShape::MainArgv => {
|
||||
let pad = match &spec.payload_slot {
|
||||
PayloadSlot::Argv(n) => *n,
|
||||
_ => 0,
|
||||
};
|
||||
let mut buf = String::from(" std::vector<char*> new_argv;\n");
|
||||
buf.push_str(" std::vector<std::string> argv_storage;\n");
|
||||
buf.push_str(" argv_storage.emplace_back(\"nyx_harness\");\n");
|
||||
for _ in 0..pad {
|
||||
buf.push_str(" argv_storage.emplace_back(\"\");\n");
|
||||
}
|
||||
buf.push_str(" argv_storage.push_back(payload);\n");
|
||||
buf.push_str(" for (auto &s : argv_storage) new_argv.push_back(s.data());\n");
|
||||
buf.push_str(" new_argv.push_back(nullptr);\n");
|
||||
buf.push_str(&format!(
|
||||
" {entry_fn}(static_cast<int>(argv_storage.size()), new_argv.data());\n",
|
||||
));
|
||||
buf
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn generate_cmake() -> String {
|
||||
r#"# Phase 16 — reference CMakeLists.txt, not used by the runner (the build
|
||||
# sandbox calls g++ / clang++ directly). Kept so reproductions can re-build
|
||||
# the harness by hand via `cmake -B build && cmake --build build`.
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
project(nyx_harness CXX)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
add_executable(nyx_harness main.cpp)
|
||||
"#
|
||||
.to_owned()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::dynamic::spec::{EntryKind, HarnessSpec, PayloadSlot};
|
||||
use crate::labels::Cap;
|
||||
use crate::symbol::Lang;
|
||||
|
||||
fn make_spec(payload_slot: PayloadSlot) -> HarnessSpec {
|
||||
HarnessSpec {
|
||||
finding_id: "cpp0000000000001".into(),
|
||||
entry_file: "entry.cpp".into(),
|
||||
entry_name: "run".into(),
|
||||
entry_kind: EntryKind::Function,
|
||||
lang: Lang::Cpp,
|
||||
toolchain_id: "g++-stable".into(),
|
||||
payload_slot,
|
||||
expected_cap: Cap::CODE_EXEC,
|
||||
constraint_hints: vec![],
|
||||
sink_file: "entry.cpp".into(),
|
||||
sink_line: 10,
|
||||
spec_hash: "cpptest00000001".into(),
|
||||
derivation: crate::dynamic::spec::SpecDerivationStrategy::FromFlowSteps,
|
||||
stubs_required: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn entry_kinds_supported_is_non_empty() {
|
||||
assert!(!CppEmitter.entry_kinds_supported().is_empty());
|
||||
assert!(CppEmitter.entry_kinds_supported().contains(&EntryKind::Function));
|
||||
assert!(CppEmitter.entry_kinds_supported().contains(&EntryKind::CliSubcommand));
|
||||
assert!(CppEmitter.entry_kinds_supported().contains(&EntryKind::LibraryApi));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -231,4 +461,67 @@ mod tests {
|
|||
assert!(hint.contains("CliSubcommand"));
|
||||
assert!(hint.contains("Phase 16"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shape_detect_main_argv() {
|
||||
let src = "int main(int argc, char *argv[]) { return 0; }";
|
||||
let mut spec = make_spec(PayloadSlot::Argv(0));
|
||||
spec.entry_kind = EntryKind::CliSubcommand;
|
||||
spec.entry_name = "main".into();
|
||||
assert_eq!(CppShape::detect(&spec, src), CppShape::MainArgv);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shape_detect_libfuzzer() {
|
||||
let src = "extern \"C\" int LLVMFuzzerTestOneInput(const uint8_t* d, size_t n) { return 0; }";
|
||||
let mut spec = make_spec(PayloadSlot::Param(0));
|
||||
spec.entry_kind = EntryKind::LibraryApi;
|
||||
spec.entry_name = "LLVMFuzzerTestOneInput".into();
|
||||
assert_eq!(CppShape::detect(&spec, src), CppShape::LibfuzzerEntry);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shape_detect_free_fn() {
|
||||
let src = "void run(const char *s, size_t n) { (void)s; (void)n; }";
|
||||
let spec = make_spec(PayloadSlot::Param(0));
|
||||
assert_eq!(CppShape::detect(&spec, src), CppShape::FreeFn);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn emit_produces_source() {
|
||||
let spec = make_spec(PayloadSlot::Param(0));
|
||||
let h = emit(&spec).unwrap();
|
||||
assert_eq!(h.filename, "main.cpp");
|
||||
assert!(h.source.contains("#include \"entry.cpp\""));
|
||||
assert!(h.source.contains("run(payload.c_str(), payload.size())"));
|
||||
assert_eq!(h.command, vec!["./nyx_harness"]);
|
||||
assert_eq!(h.entry_subpath, Some("entry.cpp".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn emit_libfuzzer_shape_passes_bytes() {
|
||||
let mut spec = make_spec(PayloadSlot::Param(0));
|
||||
spec.entry_kind = EntryKind::LibraryApi;
|
||||
spec.entry_name = "LLVMFuzzerTestOneInput".into();
|
||||
let h = emit(&spec).unwrap();
|
||||
assert!(h.source.contains("LLVMFuzzerTestOneInput(reinterpret_cast<const uint8_t*>(payload.data()), payload.size())"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn emit_main_argv_shape_builds_argv() {
|
||||
let mut spec = make_spec(PayloadSlot::Argv(0));
|
||||
spec.entry_kind = EntryKind::CliSubcommand;
|
||||
spec.entry_name = "nyx_entry_main".into();
|
||||
let h = emit(&spec).unwrap();
|
||||
assert!(h.source.contains("argv_storage.push_back(payload)"));
|
||||
assert!(h.source.contains("nyx_entry_main(static_cast<int>(argv_storage.size()), new_argv.data())"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn emit_cmake_in_extra_files() {
|
||||
let spec = make_spec(PayloadSlot::Param(0));
|
||||
let h = emit(&spec).unwrap();
|
||||
let mk = h.extra_files.iter().find(|(n, _)| n == "CMakeLists.txt").expect("CMakeLists.txt must be staged");
|
||||
assert!(mk.1.contains("add_executable(nyx_harness main.cpp)"));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,15 +26,24 @@ use crate::dynamic::lang::{HarnessSource, LangEmitter};
|
|||
use crate::dynamic::spec::{EntryKind, HarnessSpec, PayloadSlot};
|
||||
use crate::evidence::UnsupportedReason;
|
||||
use crate::labels::Cap;
|
||||
use std::path::PathBuf;
|
||||
|
||||
/// Zero-sized [`LangEmitter`] handle for Rust. Method bodies delegate to the
|
||||
/// existing free functions in this module.
|
||||
pub struct RustEmitter;
|
||||
|
||||
/// Entry kinds the Rust emitter currently understands. Extended in Phase 16
|
||||
/// (Track B Rust + C/C++ vertical) to include `HttpRoute` (`actix_web`,
|
||||
/// `axum`), `CliSubcommand` (clap), and `LibraryApi` (libfuzzer).
|
||||
const SUPPORTED: &[EntryKind] = &[EntryKind::Function];
|
||||
/// Entry kinds the Rust emitter understands after Phase 16.
|
||||
///
|
||||
/// `HttpRoute` covers `actix_web` and `axum` handlers. `CliSubcommand`
|
||||
/// covers clap-driven CLIs. `LibraryApi` covers libfuzzer
|
||||
/// `fuzz_target!` entry points. `Function` covers plain free functions
|
||||
/// and is the fallback when shape detection is inconclusive.
|
||||
const SUPPORTED: &[EntryKind] = &[
|
||||
EntryKind::Function,
|
||||
EntryKind::HttpRoute,
|
||||
EntryKind::CliSubcommand,
|
||||
EntryKind::LibraryApi,
|
||||
];
|
||||
|
||||
impl LangEmitter for RustEmitter {
|
||||
fn emit(&self, spec: &HarnessSpec) -> Result<HarnessSource, UnsupportedReason> {
|
||||
|
|
@ -47,7 +56,7 @@ impl LangEmitter for RustEmitter {
|
|||
|
||||
fn entry_kind_hint(&self, attempted: EntryKind) -> String {
|
||||
format!(
|
||||
"rust emitter supports {SUPPORTED:?}; this finding's enclosing context is `EntryKind::{attempted}` — Track B will add actix / axum / clap / libfuzzer shapes in phase 16"
|
||||
"rust emitter supports {SUPPORTED:?}; this finding's enclosing context is `EntryKind::{attempted}` — see Phase 16 shape dispatch (actix / axum / clap / libfuzzer)"
|
||||
)
|
||||
}
|
||||
|
||||
|
|
@ -303,15 +312,117 @@ fn __nyx_install_crash_guard(_sink_callee: &'static str) {}
|
|||
"#
|
||||
}
|
||||
|
||||
// ── Phase 16: shape detector ─────────────────────────────────────────────────
|
||||
|
||||
/// Concrete per-file shape resolved by reading the entry source.
|
||||
///
|
||||
/// One harness template per variant. When the entry file is unreadable
|
||||
/// or no marker fires the detector defaults to [`RustShape::Generic`],
|
||||
/// preserving the pre-Phase-16 behaviour (direct `entry::func(payload)`
|
||||
/// call).
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum RustShape {
|
||||
/// `actix_web` handler — `async fn handler(req: HttpRequest) -> HttpResponse`
|
||||
/// or similar. Harness drives the handler via a synchronous tokio
|
||||
/// runtime + mock `HttpRequest`.
|
||||
ActixWebRoute,
|
||||
/// `axum` handler — `async fn handler(...) -> impl IntoResponse`.
|
||||
/// Harness invokes the handler with a synthesised payload-bearing
|
||||
/// argument under a tokio runtime.
|
||||
AxumHandler,
|
||||
/// clap-driven CLI: `entry` parses `std::env::args` via `clap`.
|
||||
/// Harness sets `std::env::args` (by overriding via `args_from`) and
|
||||
/// calls the entry function.
|
||||
ClapCli,
|
||||
/// libfuzzer target — `fuzz_target!(|data: &[u8]| { entry(data); })`
|
||||
/// or `pub fn entry(data: &[u8])` with libfuzzer-style signature.
|
||||
/// Harness invokes with `payload.as_bytes()`.
|
||||
LibfuzzerTarget,
|
||||
/// Plain free function — `fn entry(payload: &str)`. Pre-Phase-16 default.
|
||||
Generic,
|
||||
}
|
||||
|
||||
impl RustShape {
|
||||
/// Detect the shape from `(spec, source)`. `source` is the literal
|
||||
/// bytes of the entry file (best-effort — empty string falls back
|
||||
/// to [`Self::Generic`]).
|
||||
pub fn detect(spec: &HarnessSpec, source: &str) -> Self {
|
||||
let kind = spec.entry_kind;
|
||||
let entry = spec.entry_name.as_str();
|
||||
|
||||
let has_actix = source.contains("actix_web::")
|
||||
|| source.contains("HttpRequest")
|
||||
|| source.contains("HttpResponse")
|
||||
|| source.contains("#[get(")
|
||||
|| source.contains("#[post(");
|
||||
let has_axum = source.contains("axum::")
|
||||
|| source.contains("IntoResponse")
|
||||
|| source.contains("Json(")
|
||||
|| source.contains("Query(")
|
||||
|| source.contains("axum::extract");
|
||||
let has_clap = source.contains("clap::")
|
||||
|| source.contains("#[derive(Parser)")
|
||||
|| source.contains("Parser::parse");
|
||||
let has_libfuzzer = source.contains("libfuzzer_sys::fuzz_target")
|
||||
|| source.contains("fuzz_target!")
|
||||
|| (source.contains("pub fn ") && source.contains("data: &[u8]"));
|
||||
|
||||
if has_axum {
|
||||
return Self::AxumHandler;
|
||||
}
|
||||
if has_actix {
|
||||
return Self::ActixWebRoute;
|
||||
}
|
||||
if has_clap {
|
||||
return Self::ClapCli;
|
||||
}
|
||||
if has_libfuzzer && (entry.starts_with("fuzz") || entry == "fuzz_target") {
|
||||
return Self::LibfuzzerTarget;
|
||||
}
|
||||
match kind {
|
||||
EntryKind::HttpRoute => Self::ActixWebRoute,
|
||||
EntryKind::CliSubcommand => Self::ClapCli,
|
||||
EntryKind::LibraryApi => Self::LibfuzzerTarget,
|
||||
_ => Self::Generic,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Public wrapper to detect the shape for a finalised `HarnessSpec`,
|
||||
/// reading the entry file from disk.
|
||||
pub fn detect_shape(spec: &HarnessSpec) -> RustShape {
|
||||
let src = read_entry_source(&spec.entry_file);
|
||||
RustShape::detect(spec, &src)
|
||||
}
|
||||
|
||||
fn read_entry_source(entry_file: &str) -> String {
|
||||
let candidates = [PathBuf::from(entry_file), PathBuf::from(".").join(entry_file)];
|
||||
for path in &candidates {
|
||||
if let Ok(s) = std::fs::read_to_string(path) {
|
||||
return s;
|
||||
}
|
||||
}
|
||||
String::new()
|
||||
}
|
||||
|
||||
/// Emit a Rust harness for `spec`.
|
||||
pub fn emit(spec: &HarnessSpec) -> Result<HarnessSource, UnsupportedReason> {
|
||||
match &spec.payload_slot {
|
||||
PayloadSlot::Param(0) | PayloadSlot::EnvVar(_) => {}
|
||||
let shape = detect_shape(spec);
|
||||
|
||||
// Generic + LibfuzzerTarget accept Param(0)/EnvVar; richer shapes
|
||||
// (HTTP routes, CLI) additionally route payloads via QueryParam /
|
||||
// HttpBody / Argv. Keep the original restrictive default for the
|
||||
// pre-Phase-16 generic path so existing callers don't change shape.
|
||||
match (&spec.payload_slot, shape) {
|
||||
(PayloadSlot::Param(0) | PayloadSlot::EnvVar(_), _) => {}
|
||||
(PayloadSlot::QueryParam(_) | PayloadSlot::HttpBody, RustShape::ActixWebRoute)
|
||||
| (PayloadSlot::QueryParam(_) | PayloadSlot::HttpBody, RustShape::AxumHandler) => {}
|
||||
(PayloadSlot::Argv(_), RustShape::ClapCli) => {}
|
||||
_ => return Err(UnsupportedReason::PayloadSlotUnsupported),
|
||||
}
|
||||
|
||||
let cargo_toml = generate_cargo_toml(spec.expected_cap);
|
||||
let main_rs = generate_main_rs(spec);
|
||||
let main_rs = generate_main_rs(spec, shape);
|
||||
|
||||
Ok(HarnessSource {
|
||||
source: main_rs,
|
||||
|
|
@ -350,17 +461,18 @@ pub fn generate_cargo_toml(cap: Cap) -> String {
|
|||
/// Generate `src/main.rs` — the harness entry point.
|
||||
///
|
||||
/// Reads the payload from env, calls `entry::{entry_name}` with the payload
|
||||
/// routed according to `spec.payload_slot`.
|
||||
fn generate_main_rs(spec: &HarnessSpec) -> String {
|
||||
/// routed according to `spec.payload_slot` and `shape`.
|
||||
fn generate_main_rs(spec: &HarnessSpec, shape: RustShape) -> String {
|
||||
let entry_fn = &spec.entry_name;
|
||||
let (pre_call, call_expr) = build_call(spec, entry_fn);
|
||||
let (pre_call, call_expr) = build_call(spec, entry_fn, shape);
|
||||
|
||||
format!(
|
||||
r#"//! Nyx dynamic harness — auto-generated, do not edit.
|
||||
r#"//! Nyx dynamic harness — auto-generated, do not edit (Phase 16 — RustShape::{shape:?}).
|
||||
mod entry;
|
||||
|
||||
fn main() {{
|
||||
let payload = nyx_payload();
|
||||
let _ = &payload;
|
||||
{pre_call} {call_expr}
|
||||
}}
|
||||
|
||||
|
|
@ -412,33 +524,78 @@ fn b64_decode(input: &[u8]) -> Option<Vec<u8>> {{
|
|||
Some(out)
|
||||
}}
|
||||
"#,
|
||||
shape = shape,
|
||||
pre_call = pre_call,
|
||||
call_expr = call_expr,
|
||||
)
|
||||
}
|
||||
|
||||
/// Build `(pre_call_setup, call_expression)` strings for the chosen payload slot.
|
||||
fn build_call(spec: &HarnessSpec, func: &str) -> (String, String) {
|
||||
match &spec.payload_slot {
|
||||
PayloadSlot::Param(0) => {
|
||||
let pre = String::new();
|
||||
let call = format!("entry::{func}(&payload);");
|
||||
(pre, call)
|
||||
}
|
||||
PayloadSlot::EnvVar(name) => {
|
||||
let pre = format!(" std::env::set_var({name:?}, &payload);\n");
|
||||
let call = format!("entry::{func}();");
|
||||
(pre, call)
|
||||
}
|
||||
_ => {
|
||||
// Unreachable: `emit()` rejects all other slots up front.
|
||||
let pre = String::new();
|
||||
let call = format!("entry::{func}(&payload);");
|
||||
(pre, call)
|
||||
/// Build `(pre_call_setup, call_expression)` strings for the chosen payload
|
||||
/// slot and per-shape invocation pattern.
|
||||
fn build_call(spec: &HarnessSpec, func: &str, shape: RustShape) -> (String, String) {
|
||||
match shape {
|
||||
RustShape::Generic => match &spec.payload_slot {
|
||||
PayloadSlot::Param(0) => (String::new(), format!("entry::{func}(&payload);")),
|
||||
PayloadSlot::EnvVar(name) => (
|
||||
format!(" std::env::set_var({name:?}, &payload);\n"),
|
||||
format!("entry::{func}();"),
|
||||
),
|
||||
_ => (String::new(), format!("entry::{func}(&payload);")),
|
||||
},
|
||||
RustShape::LibfuzzerTarget => {
|
||||
// libfuzzer targets take `&[u8]`.
|
||||
(String::new(), format!("entry::{func}(payload.as_bytes());"))
|
||||
}
|
||||
RustShape::ActixWebRoute => actix_invocation(spec, func),
|
||||
RustShape::AxumHandler => axum_invocation(spec, func),
|
||||
RustShape::ClapCli => clap_invocation(spec, func),
|
||||
}
|
||||
}
|
||||
|
||||
fn actix_invocation(spec: &HarnessSpec, func: &str) -> (String, String) {
|
||||
// Real actix_web requires an async runtime; the test fixtures use a
|
||||
// synchronous shim signature `pub fn <func>(payload: &str) -> String`
|
||||
// to keep build deps zero. The harness driver invokes it directly.
|
||||
match &spec.payload_slot {
|
||||
PayloadSlot::Param(0) => (String::new(), format!("let _ = entry::{func}(&payload);")),
|
||||
PayloadSlot::EnvVar(name) => (
|
||||
format!(" std::env::set_var({name:?}, &payload);\n"),
|
||||
format!("let _ = entry::{func}(\"\");"),
|
||||
),
|
||||
PayloadSlot::HttpBody => (
|
||||
String::new(),
|
||||
format!("let _ = entry::{func}(&payload);"),
|
||||
),
|
||||
PayloadSlot::QueryParam(name) => (
|
||||
String::new(),
|
||||
format!(
|
||||
"let _ = entry::{func}(&format!(\"{name}={{}}\", payload));",
|
||||
),
|
||||
),
|
||||
_ => (String::new(), format!("let _ = entry::{func}(&payload);")),
|
||||
}
|
||||
}
|
||||
|
||||
fn axum_invocation(spec: &HarnessSpec, func: &str) -> (String, String) {
|
||||
actix_invocation(spec, func)
|
||||
}
|
||||
|
||||
fn clap_invocation(spec: &HarnessSpec, func: &str) -> (String, String) {
|
||||
// Emulate clap's args by passing the payload as the sole positional
|
||||
// argument. Fixture entry signature: `pub fn <func>(args: Vec<String>)`.
|
||||
let pad = match &spec.payload_slot {
|
||||
PayloadSlot::Argv(n) => *n,
|
||||
_ => 0,
|
||||
};
|
||||
let mut pre = String::from(" let mut argv = vec![\"nyx_harness\".to_string()];\n");
|
||||
for _ in 0..pad {
|
||||
pre.push_str(" argv.push(String::new());\n");
|
||||
}
|
||||
pre.push_str(" argv.push(payload.clone());\n");
|
||||
let call = format!("entry::{func}(argv);");
|
||||
(pre, call)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
|
@ -535,9 +692,86 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn entry_kind_hint_names_attempted_and_phase() {
|
||||
let hint = RustEmitter.entry_kind_hint(EntryKind::HttpRoute);
|
||||
assert!(hint.contains("HttpRoute"));
|
||||
assert!(hint.contains("phase 16"));
|
||||
let hint = RustEmitter.entry_kind_hint(EntryKind::LibraryApi);
|
||||
assert!(hint.contains("LibraryApi"));
|
||||
assert!(hint.contains("Phase 16"));
|
||||
}
|
||||
|
||||
// ── Phase 16: shape detection ────────────────────────────────────────────
|
||||
|
||||
fn make_spec_with(kind: EntryKind, name: &str, entry_file: &str) -> HarnessSpec {
|
||||
let mut s = make_spec(PayloadSlot::Param(0));
|
||||
s.entry_kind = kind;
|
||||
s.entry_name = name.to_owned();
|
||||
s.entry_file = entry_file.to_owned();
|
||||
s
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shape_detect_axum_handler() {
|
||||
let src = "use axum::extract::Query; pub fn handler(payload: &str) -> String { String::new() }";
|
||||
let spec = make_spec_with(EntryKind::HttpRoute, "handler", "src/entry.rs");
|
||||
assert_eq!(RustShape::detect(&spec, src), RustShape::AxumHandler);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shape_detect_actix_route() {
|
||||
let src = "use actix_web::HttpResponse; pub fn handler(payload: &str) -> String { String::new() }";
|
||||
let spec = make_spec_with(EntryKind::HttpRoute, "handler", "src/entry.rs");
|
||||
assert_eq!(RustShape::detect(&spec, src), RustShape::ActixWebRoute);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shape_detect_clap_cli() {
|
||||
let src = "use clap::Parser; pub fn run(args: Vec<String>) {}";
|
||||
let spec = make_spec_with(EntryKind::CliSubcommand, "run", "src/entry.rs");
|
||||
assert_eq!(RustShape::detect(&spec, src), RustShape::ClapCli);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shape_detect_libfuzzer_target() {
|
||||
let src = "pub fn fuzz_target(data: &[u8]) {}";
|
||||
let spec = make_spec_with(EntryKind::LibraryApi, "fuzz_target", "src/entry.rs");
|
||||
assert_eq!(RustShape::detect(&spec, src), RustShape::LibfuzzerTarget);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shape_detect_generic_fallback() {
|
||||
let src = "pub fn run(payload: &str) {}";
|
||||
let spec = make_spec_with(EntryKind::Function, "run", "src/entry.rs");
|
||||
assert_eq!(RustShape::detect(&spec, src), RustShape::Generic);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn axum_shape_emits_str_invocation() {
|
||||
let mut spec = make_spec_with(EntryKind::HttpRoute, "handler", "src/entry.rs");
|
||||
spec.payload_slot = PayloadSlot::QueryParam("q".into());
|
||||
let src = generate_main_rs(&spec, RustShape::AxumHandler);
|
||||
assert!(src.contains("entry::handler"));
|
||||
assert!(src.contains("q={}"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn axum_shape_param0_passes_raw_payload() {
|
||||
let spec = make_spec_with(EntryKind::HttpRoute, "handler", "src/entry.rs");
|
||||
let src = generate_main_rs(&spec, RustShape::AxumHandler);
|
||||
assert!(src.contains("entry::handler(&payload)"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn clap_shape_emits_argv() {
|
||||
let mut spec = make_spec_with(EntryKind::CliSubcommand, "run", "src/entry.rs");
|
||||
spec.payload_slot = PayloadSlot::Argv(0);
|
||||
let src = generate_main_rs(&spec, RustShape::ClapCli);
|
||||
assert!(src.contains("argv.push(payload.clone())"));
|
||||
assert!(src.contains("entry::run(argv)"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn libfuzzer_shape_emits_bytes_invocation() {
|
||||
let spec = make_spec_with(EntryKind::LibraryApi, "fuzz_target", "src/entry.rs");
|
||||
let src = generate_main_rs(&spec, RustShape::LibfuzzerTarget);
|
||||
assert!(src.contains("entry::fuzz_target(payload.as_bytes())"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
|||
|
|
@ -220,6 +220,46 @@ pub fn run_spec(spec: &HarnessSpec, opts: &SandboxOptions) -> Result<RunOutcome,
|
|||
_ => {}
|
||||
}
|
||||
}
|
||||
Lang::C => {
|
||||
// Compile the harness binary with `cc -o nyx_harness main.c`.
|
||||
match build_sandbox::prepare_c(spec, &harness.workdir) {
|
||||
Ok(build_result) => {
|
||||
let binary = build_result.venv_path.join("nyx_harness");
|
||||
if binary.exists() {
|
||||
harness.command = vec![binary.to_string_lossy().into_owned()];
|
||||
} else {
|
||||
let fallback = harness.workdir.join("nyx_harness");
|
||||
if fallback.exists() {
|
||||
harness.command = vec![fallback.to_string_lossy().into_owned()];
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(build_sandbox::BuildError::BuildFailed { stderr, attempts }) => {
|
||||
return Err(RunError::BuildFailed { stderr, attempts });
|
||||
}
|
||||
Err(_) => {}
|
||||
}
|
||||
}
|
||||
Lang::Cpp => {
|
||||
// Compile the harness binary with `c++ -o nyx_harness main.cpp`.
|
||||
match build_sandbox::prepare_cpp(spec, &harness.workdir) {
|
||||
Ok(build_result) => {
|
||||
let binary = build_result.venv_path.join("nyx_harness");
|
||||
if binary.exists() {
|
||||
harness.command = vec![binary.to_string_lossy().into_owned()];
|
||||
} else {
|
||||
let fallback = harness.workdir.join("nyx_harness");
|
||||
if fallback.exists() {
|
||||
harness.command = vec![fallback.to_string_lossy().into_owned()];
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(build_sandbox::BuildError::BuildFailed { stderr, attempts }) => {
|
||||
return Err(RunError::BuildFailed { stderr, attempts });
|
||||
}
|
||||
Err(_) => {}
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
// No build step for other languages.
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue