feat(research): isolate candidates and add reviewed paper promotion
This commit is contained in:
parent
26b97265c6
commit
25b5cbf481
128 changed files with 13038 additions and 1388 deletions
218
apps/stock/orchestrator/src/backtest/sandbox/guardian.rs
Normal file
218
apps/stock/orchestrator/src/backtest/sandbox/guardian.rs
Normal file
|
|
@ -0,0 +1,218 @@
|
|||
//! Minimal Linux x86_64 PID1. This executable contains no strategy/data logic.
|
||||
use std::{
|
||||
os::{fd::FromRawFd, unix::process::CommandExt},
|
||||
process::{Command, Stdio},
|
||||
sync::{
|
||||
atomic::{AtomicBool, Ordering},
|
||||
Arc,
|
||||
},
|
||||
time::{Duration, Instant, SystemTime, UNIX_EPOCH},
|
||||
};
|
||||
#[repr(C)]
|
||||
struct Filter {
|
||||
code: u16,
|
||||
jt: u8,
|
||||
jf: u8,
|
||||
k: u32,
|
||||
}
|
||||
#[repr(C)]
|
||||
struct Program {
|
||||
len: u16,
|
||||
filter: *const Filter,
|
||||
}
|
||||
#[repr(C)]
|
||||
struct PollFd {
|
||||
fd: i32,
|
||||
events: i16,
|
||||
revents: i16,
|
||||
}
|
||||
#[repr(C)]
|
||||
struct Limit {
|
||||
current: u64,
|
||||
maximum: u64,
|
||||
}
|
||||
unsafe extern "C" {
|
||||
fn prctl(option: i32, ...) -> i32;
|
||||
fn setgid(gid: u32) -> i32;
|
||||
fn setuid(uid: u32) -> i32;
|
||||
fn setgroups(size: usize, list: *const u32) -> i32;
|
||||
fn setrlimit(resource: i32, limit: *const Limit) -> i32;
|
||||
fn poll(fds: *mut PollFd, nfds: usize, timeout: i32) -> i32;
|
||||
fn kill(pid: i32, signal: i32) -> i32;
|
||||
}
|
||||
fn stmt(code: u16, k: u32) -> Filter {
|
||||
Filter {
|
||||
code,
|
||||
jt: 0,
|
||||
jf: 0,
|
||||
k,
|
||||
}
|
||||
}
|
||||
fn equal(k: u32, jt: u8, jf: u8) -> Filter {
|
||||
Filter {
|
||||
code: 0x15,
|
||||
jt,
|
||||
jf,
|
||||
k,
|
||||
}
|
||||
}
|
||||
fn error() -> std::io::Error {
|
||||
std::io::Error::last_os_error()
|
||||
}
|
||||
fn main() {
|
||||
if !cfg!(all(target_os = "linux", target_arch = "x86_64")) {
|
||||
std::process::exit(126);
|
||||
}
|
||||
let deadline: u128 = std::env::args()
|
||||
.nth(1)
|
||||
.and_then(|s| s.parse().ok())
|
||||
.unwrap_or(0);
|
||||
let now = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.expect("system clock")
|
||||
.as_millis();
|
||||
if deadline <= now || deadline - now > 300_000 {
|
||||
std::process::exit(124);
|
||||
}
|
||||
let remaining = Duration::from_millis((deadline - now) as u64);
|
||||
let clock = Instant::now();
|
||||
let mut command = Command::new("/usr/bin/bun");
|
||||
command
|
||||
.arg("/app/candidate-host.js")
|
||||
.current_dir("/tmp")
|
||||
.env_clear()
|
||||
.stdin(Stdio::inherit())
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::inherit());
|
||||
unsafe {
|
||||
command.pre_exec(|| {
|
||||
if setgroups(0, std::ptr::null()) != 0 || setgid(65534) != 0 || setuid(65534) != 0 {
|
||||
return Err(error());
|
||||
}
|
||||
if setrlimit(
|
||||
0,
|
||||
&Limit {
|
||||
current: 300,
|
||||
maximum: 300,
|
||||
},
|
||||
) != 0
|
||||
|| setrlimit(
|
||||
4,
|
||||
&Limit {
|
||||
current: 0,
|
||||
maximum: 0,
|
||||
},
|
||||
) != 0
|
||||
{
|
||||
return Err(error());
|
||||
}
|
||||
if prctl(38, 1, 0, 0, 0) != 0 {
|
||||
return Err(error());
|
||||
}
|
||||
// Check architecture and reject x32 syscall-number aliases before any allow rule.
|
||||
let mut filter = vec![
|
||||
stmt(0x20, 4),
|
||||
equal(0xc000003e, 1, 0),
|
||||
stmt(0x06, 0x80000000),
|
||||
stmt(0x20, 0),
|
||||
Filter {
|
||||
code: 0x35,
|
||||
jt: 0,
|
||||
jf: 1,
|
||||
k: 0x40000000,
|
||||
},
|
||||
stmt(0x06, 0x00050001),
|
||||
];
|
||||
// fork, vfork, socket, ptrace, mount, umount2, unshare, setns, bpf, perf_event_open.
|
||||
for nr in [57, 58, 41, 101, 165, 166, 272, 308, 321, 298] {
|
||||
filter.push(equal(nr, 0, 1));
|
||||
filter.push(stmt(0x06, 0x00050001));
|
||||
}
|
||||
// clone3 cannot inspect the pointed-to flags; make libc use checked clone instead.
|
||||
filter.push(equal(435, 0, 1));
|
||||
filter.push(stmt(0x06, 0x00050026));
|
||||
filter.push(equal(56, 0, 4));
|
||||
filter.push(stmt(0x20, 16));
|
||||
filter.push(Filter {
|
||||
code: 0x45,
|
||||
jt: 0,
|
||||
jf: 1,
|
||||
k: 0x00010000,
|
||||
});
|
||||
filter.push(stmt(0x06, 0x7fff0000));
|
||||
filter.push(stmt(0x06, 0x00050001));
|
||||
filter.push(stmt(0x06, 0x7fff0000));
|
||||
let program = Program {
|
||||
len: filter.len() as u16,
|
||||
filter: filter.as_ptr(),
|
||||
};
|
||||
if prctl(22, 2, &program as *const Program) != 0 {
|
||||
return Err(error());
|
||||
}
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
let mut child = command
|
||||
.spawn()
|
||||
.expect("restricted host initialization failed");
|
||||
let mut output = child.stdout.take().expect("restricted host stdout missing");
|
||||
let output_closed = Arc::new(AtomicBool::new(false));
|
||||
let relay_closed = Arc::clone(&output_closed);
|
||||
// A bounded std::io::copy buffer relays bytes only. A separate watchdog remains
|
||||
// responsive even if the child floods stdout or the consumer stops reading.
|
||||
// Direct File writes avoid line-buffering the binary protocol.
|
||||
std::thread::spawn(move || {
|
||||
let mut destination = unsafe { std::fs::File::from_raw_fd(1) };
|
||||
let _ = std::io::copy(&mut output, &mut destination);
|
||||
relay_closed.store(true, Ordering::Release);
|
||||
});
|
||||
let mut exit_status = None;
|
||||
let mut stdout_closed_at = None;
|
||||
loop {
|
||||
if exit_status.is_none() {
|
||||
if let Some(status) = child.try_wait().expect("restricted host wait failed") {
|
||||
exit_status = Some(status.code().unwrap_or(125));
|
||||
}
|
||||
}
|
||||
if output_closed.load(Ordering::Acquire) {
|
||||
if let Some(code) = exit_status {
|
||||
// Drain the complete final frame before ending a normal finish/transpile.
|
||||
std::process::exit(code);
|
||||
}
|
||||
stdout_closed_at.get_or_insert_with(Instant::now);
|
||||
}
|
||||
// EOF can precede the child's normal exit by a few scheduler turns.
|
||||
let protocol_closed =
|
||||
stdout_closed_at.is_some_and(|at| at.elapsed() >= Duration::from_millis(100));
|
||||
let mut input = PollFd {
|
||||
fd: 0,
|
||||
events: 0,
|
||||
revents: 0,
|
||||
};
|
||||
let ready = unsafe { poll(&mut input, 1, 0) };
|
||||
let disconnected = ready < 0 || input.revents & (0x10 | 0x08 | 0x20) != 0;
|
||||
if disconnected || protocol_closed || clock.elapsed() >= remaining {
|
||||
unsafe {
|
||||
kill(child.id() as i32, 9);
|
||||
}
|
||||
let _ = child.wait();
|
||||
eprintln!(
|
||||
"Candidate host {}",
|
||||
if disconnected {
|
||||
"supervisor disconnected"
|
||||
} else if protocol_closed {
|
||||
"protocol stdout closed"
|
||||
} else {
|
||||
"wall deadline exceeded"
|
||||
}
|
||||
);
|
||||
// Exiting namespace PID1 kills remaining threads/processes independently of JS.
|
||||
std::process::exit(if disconnected || protocol_closed {
|
||||
125
|
||||
} else {
|
||||
124
|
||||
});
|
||||
}
|
||||
std::thread::sleep(Duration::from_millis(10));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue