#[cfg(all(feature = "jemalloc", not(windows), not(target_env = "musl")))]
#[allow(non_upper_case_globals)]
#[used]
#[unsafe(export_name = "_rjem_malloc_conf")]
pub static malloc_conf: &[u8] = lean_ctx::JEMALLOC_CONF;
fn main() {
lean_ctx::core::memory_guard::route_tree_sitter_to_jemalloc();
lean_ctx::core::crypto_provider::install_default();
lean_ctx::core::binary_identity::record_start();
#[cfg(unix)]
if seal_bootstrap_descriptor(std::env::args_os().skip(1)).is_err() {
eprintln!("Error: cannot seal protected GitLab descriptor");
std::process::exit(1);
}
#[cfg(target_os = "macos")]
lean_ctx::core::tcc_guard_sandbox::reexec_under_seatbelt_if_needed();
lean_ctx::core::crash_log::install_panic_hook();
let res = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
lean_ctx_cli::cli::dispatch::run();
}));
if res.is_err() {
std::process::exit(1);
}
}
#[cfg(all(test, feature = "jemalloc", not(windows), not(target_env = "musl")))]
mod jemalloc_tests {
#[test]
fn jemalloc_applies_exported_conf() {
let decay: isize =
unsafe { tikv_jemalloc_ctl::raw::read(b"opt.dirty_decay_ms\0") }.unwrap();
assert_eq!(decay, 1000);
}
}
#[cfg(unix)]
const BOOTSTRAP_FLAGS: [&str; 2] = ["--protected-gitlab-fd", "--protected-exec-fd"];
#[cfg(unix)]
fn seal_bootstrap_descriptor(
args: impl Iterator<Item = std::ffi::OsString>,
) -> std::io::Result<()> {
use std::ffi::OsStr;
fn invalid() -> std::io::Error {
std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"invalid bootstrap descriptor",
)
}
let mut args = args.peekable();
if args.next().as_deref() != Some(OsStr::new("mcp"))
|| !args
.peek()
.and_then(|arg| arg.to_str())
.is_some_and(|arg| BOOTSTRAP_FLAGS.contains(&arg))
{
return Ok(());
}
let mut sealed = 0_usize;
while let Some(flag) = args.next() {
if !flag
.to_str()
.is_some_and(|flag| BOOTSTRAP_FLAGS.contains(&flag))
{
return Err(invalid());
}
let fd = args
.next()
.and_then(|value| value.to_str()?.parse::<i32>().ok())
.filter(|fd| *fd > 2)
.ok_or_else(invalid)?;
let flags = unsafe { libc::fcntl(fd, libc::F_GETFD) };
if flags < 0 {
return Err(std::io::Error::last_os_error());
}
if unsafe { libc::fcntl(fd, libc::F_SETFD, flags | libc::FD_CLOEXEC) } < 0 {
return Err(std::io::Error::last_os_error());
}
sealed += 1;
if sealed > BOOTSTRAP_FLAGS.len() {
return Err(invalid());
}
}
Ok(())
}
#[cfg(all(test, unix))]
mod tests {
use super::seal_bootstrap_descriptor;
use std::os::fd::AsRawFd;
use std::os::unix::net::UnixStream;
#[test]
fn bootstrap_descriptor_is_sealed_before_helpers() {
let (read, _write) = UnixStream::pair().unwrap();
let fd = read.as_raw_fd();
assert_eq!(unsafe { libc::fcntl(fd, libc::F_SETFD, 0) }, 0);
let args = [
"mcp".into(),
"--protected-gitlab-fd".into(),
fd.to_string().into(),
];
seal_bootstrap_descriptor(args.into_iter()).unwrap();
assert_ne!(
unsafe { libc::fcntl(fd, libc::F_GETFD) } & libc::FD_CLOEXEC,
0
);
}
#[test]
fn both_inherited_channels_are_sealed_together() {
let (credential, _credential_peer) = UnixStream::pair().unwrap();
let (execution, _execution_peer) = UnixStream::pair().unwrap();
for socket in [&credential, &execution] {
assert_eq!(
unsafe { libc::fcntl(socket.as_raw_fd(), libc::F_SETFD, 0) },
0
);
}
let args = [
"mcp".into(),
"--protected-gitlab-fd".into(),
credential.as_raw_fd().to_string().into(),
"--protected-exec-fd".into(),
execution.as_raw_fd().to_string().into(),
];
seal_bootstrap_descriptor(args.into_iter()).unwrap();
for socket in [&credential, &execution] {
assert_ne!(
unsafe { libc::fcntl(socket.as_raw_fd(), libc::F_GETFD) } & libc::FD_CLOEXEC,
0
);
}
let repeated = [
"mcp".into(),
"--protected-exec-fd".into(),
execution.as_raw_fd().to_string().into(),
"--protected-exec-fd".into(),
execution.as_raw_fd().to_string().into(),
"--protected-exec-fd".into(),
execution.as_raw_fd().to_string().into(),
];
assert!(seal_bootstrap_descriptor(repeated.into_iter()).is_err());
}
#[test]
fn an_ordinary_mcp_start_is_untouched() {
let args = ["mcp".into(), "--some-other-flag".into(), "value".into()];
assert!(seal_bootstrap_descriptor(args.into_iter()).is_ok());
}
#[test]
fn invalid_bootstrap_fails_before_any_helper() {
for descriptor in ["0", "2", "-1", "bad", "2147483647"] {
let args = [
"mcp".into(),
"--protected-gitlab-fd".into(),
descriptor.into(),
];
assert!(seal_bootstrap_descriptor(args.into_iter()).is_err());
}
assert!(seal_bootstrap_descriptor(["mcp".into()].into_iter()).is_ok());
}
}