The truly free, open-source, local-first, cross-platform CapCut replacement. Scriptable through a JSON-RPC API & MCP.
Rust ffmpegfree-video-editormodel-context-protocolnon-linear-editoroffline-firstopen-source-video-editorself-hostedvideo-editingvideo-editing-automationvideo-editing-softwarevideo-editing-toolsvideo-editorvoice-cloningyoutube-automationyoutube-shorts
feat(logs): a native crash writes what it was and where to the log, and every log starts with the machine it ran on (#326)
Jareer8f5b1411e6a9
diff --git a/scripts/ci/smoke-windows.ps1 b/scripts/ci/smoke-windows.ps1index bf053236..01e68ce1 100644--- a/scripts/ci/smoke-windows.ps1+++ b/scripts/ci/smoke-windows.ps1@@ -62,6 +62,27 @@ $meanings = @{[int]0xC0E90002 = "a code integrity block (Smart App Control) on an unsigned or untrusted file"}+# What Windows wrote down about a program that crashed since $since: the+# Application Error event (1000) names the faulting module, its version+# and the offset into it, which is the one thing an exit code cannot say.+# It covers a crash before main as well as after, where the app's own+# handler (concat-host's crash.rs) is not installed yet. Windows writes the+# event a moment after the process is gone, so it is waited for briefly.+function Show-Crash($file, $since) {+ $exe = [IO.Path]::GetFileName($file)+ for ($try = 0; $try -lt 10; $try++) {+ $event = Get-WinEvent -FilterHashtable @{ LogName = "Application"; Id = 1000; StartTime = $since } -ErrorAction SilentlyContinue |+ Where-Object { $_.Message -match [regex]::Escape($exe) } | Select-Object -First 1+ if ($event) {+ Say "what Windows recorded about the crash"+ $event.Message -split "`n" | ForEach-Object { Write-Host " $($_.TrimEnd())" }+ return+ }+ Start-Sleep -Milliseconds 500+ }+ Write-Host " (Windows recorded no Application Error event for $exe)"+}+# Starts a program with the given PATH and waits for it, up to a limit;# fails unless it exits 0. Its output is shown when there is any.function Expect-Ok($what, $file, [string[]]$arguments, $path, $seconds) {@@ -75,6 +96,7 @@ function Expect-Ok($what, $file, [string[]]$arguments, $path, $seconds) {# in the folder it was started from.$info.WorkingDirectory = [IO.Path]::GetTempPath()$info.Environment["PATH"] = $path+ $started = Get-Date$process = [System.Diagnostics.Process]::Start($info)$stdout = $process.StandardOutput.ReadToEndAsync()$stderr = $process.StandardError.ReadToEndAsync()@@ -87,6 +109,7 @@ function Expect-Ok($what, $file, [string[]]$arguments, $path, $seconds) {if ($out) { $out -split "`n" | Select-Object -Last 60 | ForEach-Object { Write-Host " $_" } }$code = $process.ExitCodeif ($code -ne 0) {+ Show-Crash $file $started$meaning = $meanings[[int]$code]Fail ("${what}: exited with 0x{0:X8}{1}" -f $code, $(if ($meaning) { " - $meaning" } else { "" }))}diff --git a/src/crates/concat-cli/src/main.rs b/src/crates/concat-cli/src/main.rsindex 7d9305d1..ca99c221 100644--- a/src/crates/concat-cli/src/main.rs+++ b/src/crates/concat-cli/src/main.rs@@ -123,6 +123,9 @@ enum Command {}fn main() -> Result<(), Box<dyn Error>> {+ // A crash names the module it happened in on standard error, as the+ // window's does; see concat-host's crash.rs.+ concat_host::crash::catch();match Cli::parse().command {Command::Probe { path } => probe(&path),Command::Render {diff --git a/src/crates/concat-host/Cargo.toml b/src/crates/concat-host/Cargo.tomlindex c300ee30..36d4b987 100644--- a/src/crates/concat-host/Cargo.toml+++ b/src/crates/concat-host/Cargo.toml@@ -50,7 +50,20 @@ memmap2 = "0.9"libc = "0.2"[target.'cfg(windows)'.dependencies]-windows-sys = { version = "0.61", features = ["Win32_Media", "Win32_System_SystemInformation"] }+windows-sys = { version = "0.61", features = [+ "Win32_Media",+ "Win32_System_SystemInformation",+ # crash.rs: the unhandled-exception filter and what it reads and writes.+ "Win32_Foundation",+ "Win32_Storage_FileSystem",+ "Win32_System_Console",+ "Win32_System_Diagnostics_Debug",+ "Win32_System_IO",+ "Win32_System_Kernel",+ "Win32_System_LibraryLoader",+ # machine.rs: the Windows version and the CPU's name.+ "Win32_System_Registry",+] }[lints]workspace = truediff --git a/src/crates/concat-host/src/crash.rs b/src/crates/concat-host/src/crash.rsnew file mode 100644index 00000000..b80da248--- /dev/null+++ b/src/crates/concat-host/src/crash.rs@@ -0,0 +1,556 @@+// SPDX-License-Identifier: AGPL-3.0-or-later+// SPDX-FileCopyrightText: 2026 Jareer and Concat contributors++//! What a native crash leaves behind: one line in the log saying what it+//! was and where.+//!+//! A panic has a hook (see `logs::catch_panics`), but a crash is not a+//! panic. An access violation in a DLL, an instruction the CPU does not+//! have, a segmentation fault in a driver: each ended the program with a+//! status code and nothing else, and the report that came back was "it+//! closes" (#313, #318, #326). The handlers here write one line before the+//! crash goes on to wherever it was going:+//!+//! ```text+//! FATAL [concat-worker-1] crash: SIGSEGV (bad memory access) at 0x10 in libonnxruntime.so.1.28.0+0x1a2b3c+//! FATAL [main] crash: 0xC0000005 EXCEPTION_ACCESS_VIOLATION, reading 0x10, in onnxruntime.dll+0x1a2b3c+//! ```+//!+//! The module and the offset into it are what a shipped build can say: it+//! is stripped, so there are no function names to give, but the offset+//! against the same release's library is an exact place. The line goes to+//! standard error and to this run's log file.+//!+//! Nothing is handled. After the line is written the crash carries on as+//! it would have: on Windows the previous filter and Windows Error+//! Reporting see it, and on Unix the previous handler (Rust's own, which+//! names a stack overflow) is put back and the fault happens again under+//! it, so the process dies of the same signal with the same core dump.+//!+//! A crash handler runs in a broken process, so this one allocates+//! nothing and takes no lock: the line is formatted into a buffer on the+//! stack and written with one system call per destination, to a file+//! handle duplicated for it when the log opened.++use std::sync::Once;+use std::sync::atomic::{AtomicBool, Ordering};++/// Set by the first crash, so a crash inside the handler, or on a second+/// thread at the same moment, does not write over the first one's line.+static CRASHING: AtomicBool = AtomicBool::new(false);++static INSTALL: Once = Once::new();++/// Installs the handlers. Call as early as a run can - before anything+/// loads a library that might crash. Safe to call more than once.+pub fn catch() {+ INSTALL.call_once(|| {+ // SAFETY: called once, before any crash can reach the handlers,+ // which only read what is set here.+ unsafe { platform::install() }+ });+}++/// Gives the handlers this run's log file to write to as well as standard+/// error. The file is duplicated and the duplicate kept open for the rest+/// of the run, so the handlers never touch the logger's own copy, or its+/// lock.+pub(crate) fn write_to(file: &std::fs::File) {+ if let Ok(file) = file.try_clone() {+ platform::keep(file);+ }+}++/// One line, built on the stack. Formatting into it allocates nothing;+/// what does not fit is cut off.+struct Line {+ buf: [u8; 1024],+ len: usize,+}++impl Line {+ const fn new() -> Self {+ Self {+ buf: [0; 1024],+ len: 0,+ }+ }++ fn bytes(&self) -> &[u8] {+ &self.buf[..self.len]+ }+}++impl std::fmt::Write for Line {+ fn write_str(&mut self, text: &str) -> std::fmt::Result {+ let room = self.buf.len() - self.len;+ let n = text.len().min(room);+ self.buf[self.len..self.len + n].copy_from_slice(&text.as_bytes()[..n]);+ self.len += n;+ Ok(())+ }+}++/// The last component of a path, for a module's name: the folder it was+/// loaded from is the install folder, which says nothing.+fn file_name(path: &str) -> &str {+ path.rsplit(['/', '\\']).next().unwrap_or(path)+}++#[cfg(unix)]+mod platform {+ use super::{CRASHING, Line, Ordering, file_name};+ use std::cell::UnsafeCell;+ use std::ffi::{CStr, c_int, c_void};+ use std::fmt::Write as _;+ use std::mem::MaybeUninit;+ use std::os::fd::IntoRawFd;+ use std::sync::atomic::AtomicI32;++ /// The log file's duplicate, or -1 before the log opens.+ static FILE: AtomicI32 = AtomicI32::new(-1);++ /// The signals a crash arrives as, and what each one means.+ const SIGNALS: [(c_int, &str, &str); 5] = [+ (libc::SIGSEGV, "SIGSEGV", "bad memory access"),+ (libc::SIGBUS, "SIGBUS", "bad memory access"),+ (+ libc::SIGILL,+ "SIGILL",+ "an instruction this CPU does not have",+ ),+ (libc::SIGFPE, "SIGFPE", "an arithmetic fault"),+ (libc::SIGABRT, "SIGABRT", "aborted"),+ ];++ /// The handler each signal had before this one, put back when it fires.+ struct Previous(UnsafeCell<[MaybeUninit<libc::sigaction>; SIGNALS.len()]>);+ // SAFETY: written once by `install` before any handler is registered,+ // then only read.+ unsafe impl Sync for Previous {}+ static PREVIOUS: Previous = Previous(UnsafeCell::new(+ [const { MaybeUninit::uninit() }; SIGNALS.len()],+ ));++ pub(super) fn keep(file: std::fs::File) {+ let fd = file.into_raw_fd();+ let old = FILE.swap(fd, Ordering::SeqCst);+ if old >= 0 {+ // SAFETY: `old` was a descriptor this module owned.+ unsafe { libc::close(old) };+ }+ }++ pub(super) unsafe fn install() {+ let previous = PREVIOUS.0.get();+ for (index, (signal, _, _)) in SIGNALS.iter().enumerate() {+ // SAFETY: plain sigaction calls on a zeroed struct; PREVIOUS+ // is written before the handler that reads it is installed.+ unsafe {+ let mut action: libc::sigaction = std::mem::zeroed();+ action.sa_sigaction = on_signal+ as extern "C" fn(c_int, *mut libc::siginfo_t, *mut c_void)+ as libc::sighandler_t;+ action.sa_flags = libc::SA_SIGINFO | libc::SA_ONSTACK;+ libc::sigemptyset(&mut action.sa_mask);+ let slot = (*previous)[index].as_mut_ptr();+ if libc::sigaction(*signal, std::ptr::null(), slot) != 0 {+ continue;+ }+ libc::sigaction(*signal, &action, std::ptr::null_mut());+ }+ }+ }++ extern "C" fn on_signal(signal: c_int, info: *mut libc::siginfo_t, context: *mut c_void) {+ let Some(index) = SIGNALS.iter().position(|(s, _, _)| *s == signal) else {+ return;+ };+ if !CRASHING.swap(true, Ordering::SeqCst) {+ // SAFETY: the kernel hands a valid siginfo and context to an+ // SA_SIGINFO handler.+ let line = unsafe { describe(index, info, context) };+ write(&line);+ }+ // SAFETY: the slot was filled by `install`. Putting the old handler+ // back and returning makes a fault happen again under it.+ unsafe {+ let previous = (*PREVIOUS.0.get())[index].as_ptr();+ libc::sigaction(signal, previous, std::ptr::null_mut());+ // Sent by another process (kill) or by raise(): returning would+ // not happen again, so send it again.+ if (*info).si_code <= 0 {+ libc::raise(signal);+ }+ }+ }++ unsafe fn describe(index: usize, info: *mut libc::siginfo_t, context: *mut c_void) -> Line {+ let (_, name, meaning) = SIGNALS[index];+ let mut line = Line::new();+ let _ = write!(line, "FATAL [");+ thread_name(&mut line);+ let _ = write!(line, "] crash: {name} ({meaning})");+ // SAFETY: as `on_signal` says.+ let address = unsafe { fault_address(info) };+ if matches!(name, "SIGSEGV" | "SIGBUS") {+ let _ = write!(line, " at {address:#x}");+ }+ // SAFETY: as `on_signal` says.+ match unsafe { program_counter(context) } {+ Some(pc) => where_(&mut line, pc),+ None => where_(&mut line, address),+ }+ let _ = writeln!(line);+ line+ }++ /// ` in libfoo.so+0x1234`, or the bare address when no module has it.+ fn where_(line: &mut Line, pc: usize) {+ if pc == 0 {+ return;+ }+ // SAFETY: dladdr only reads the loader's tables; the strings it+ // returns live as long as the module.+ unsafe {+ let mut info: libc::Dl_info = std::mem::zeroed();+ if libc::dladdr(pc as *const c_void, &mut info) != 0 && !info.dli_fname.is_null() {+ let path = CStr::from_ptr(info.dli_fname).to_string_lossy();+ let offset = pc.wrapping_sub(info.dli_fbase as usize);+ let _ = write!(line, " in {}+{offset:#x}", file_name(&path));+ return;+ }+ }+ let _ = write!(line, " at pc {pc:#x}");+ }++ unsafe fn fault_address(info: *mut libc::siginfo_t) -> usize {+ // SAFETY: as `on_signal` says.+ unsafe {+ #[cfg(any(target_os = "linux", target_os = "android"))]+ {+ (*info).si_addr() as usize+ }+ #[cfg(not(any(target_os = "linux", target_os = "android")))]+ {+ (*info).si_addr as usize+ }+ }+ }++ /// Where the faulting instruction is, on the targets Concat ships to.+ #[allow(unused_variables)]+ unsafe fn program_counter(context: *mut c_void) -> Option<usize> {+ if context.is_null() {+ return None;+ }+ // SAFETY: an SA_SIGINFO handler's third argument is a ucontext_t.+ unsafe {+ let context = context as *const libc::ucontext_t;+ #[cfg(all(+ any(target_os = "linux", target_os = "android"),+ target_arch = "x86_64"+ ))]+ return Some((*context).uc_mcontext.gregs[libc::REG_RIP as usize] as usize);+ #[cfg(all(+ any(target_os = "linux", target_os = "android"),+ target_arch = "aarch64"+ ))]+ return Some((*context).uc_mcontext.pc as usize);+ #[cfg(all(target_vendor = "apple", target_arch = "aarch64"))]+ return (*context)+ .uc_mcontext+ .as_ref()+ .map(|m| m.__ss.__pc as usize);+ #[cfg(all(target_vendor = "apple", target_arch = "x86_64"))]+ return (*context)+ .uc_mcontext+ .as_ref()+ .map(|m| m.__ss.__rip as usize);+ #[allow(unreachable_code)]+ None+ }+ }++ /// The thread's name as the kernel has it - what Rust gave the threads+ /// it started, cut to 15 bytes on Linux - or its id.+ fn thread_name(line: &mut Line) {+ let mut name = [0u8; 64];+ // SAFETY: each call writes at most the buffer's length, NUL+ // included.+ let named = unsafe {+ #[cfg(any(target_os = "linux", target_os = "android"))]+ {+ libc::prctl(libc::PR_GET_NAME, name.as_mut_ptr()) == 0+ }+ #[cfg(target_vendor = "apple")]+ {+ libc::pthread_getname_np(libc::pthread_self(), name.as_mut_ptr().cast(), name.len())+ == 0+ }+ #[cfg(not(any(target_os = "linux", target_os = "android", target_vendor = "apple")))]+ {+ false+ }+ };+ let name = CStr::from_bytes_until_nul(&name)+ .ok()+ .and_then(|name| name.to_str().ok())+ .filter(|name| named && !name.is_empty());+ match name {+ Some(name) => {+ let _ = write!(line, "{name}");+ }+ None => {+ let _ = write!(line, "?");+ }+ }+ }++ fn write(line: &Line) {+ let bytes = line.bytes();+ // SAFETY: write(2) on descriptors this process holds; a failed+ // write is ignored, there being nowhere left to say so.+ unsafe {+ libc::write(libc::STDERR_FILENO, bytes.as_ptr().cast(), bytes.len());+ let fd = FILE.load(Ordering::SeqCst);+ if fd >= 0 {+ libc::write(fd, bytes.as_ptr().cast(), bytes.len());+ }+ }+ }+}++#[cfg(windows)]+mod platform {+ use super::{CRASHING, Line, Ordering, file_name};+ use std::fmt::Write as _;+ use std::os::windows::io::IntoRawHandle;+ use std::sync::atomic::AtomicPtr;+ use windows_sys::Win32::Foundation::{HANDLE, HMODULE};+ use windows_sys::Win32::Storage::FileSystem::WriteFile;+ use windows_sys::Win32::System::Console::{GetStdHandle, STD_ERROR_HANDLE};+ use windows_sys::Win32::System::Diagnostics::Debug::{+ EXCEPTION_POINTERS, LPTOP_LEVEL_EXCEPTION_FILTER, SetUnhandledExceptionFilter,+ };+ use windows_sys::Win32::System::LibraryLoader::{+ GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,+ GetModuleFileNameW, GetModuleHandleExW,+ };++ /// The log file's duplicate, or null before the log opens.+ static FILE: AtomicPtr<std::ffi::c_void> = AtomicPtr::new(std::ptr::null_mut());++ /// The filter that was there before, which is called after this one.+ static mut PREVIOUS: LPTOP_LEVEL_EXCEPTION_FILTER = None;++ /// What the codes a crash arrives as mean.+ const CODES: [(u32, &str); 9] = [+ (0xC000_0005, "EXCEPTION_ACCESS_VIOLATION"),+ (0xC000_0006, "EXCEPTION_IN_PAGE_ERROR"),+ (+ 0xC000_001D,+ "EXCEPTION_ILLEGAL_INSTRUCTION: an instruction this CPU does not have",+ ),+ (0xC000_0094, "EXCEPTION_INT_DIVIDE_BY_ZERO"),+ (0xC000_0096, "EXCEPTION_PRIV_INSTRUCTION"),+ (0xC000_00FD, "EXCEPTION_STACK_OVERFLOW"),+ (0xC000_0409, "STATUS_STACK_BUFFER_OVERRUN"),+ (0xC000_0374, "STATUS_HEAP_CORRUPTION"),+ (0xE06D_7363, "an uncaught C++ exception"),+ ];++ pub(super) fn keep(file: std::fs::File) {+ let handle = file.into_raw_handle();+ let old = FILE.swap(handle, Ordering::SeqCst);+ if !old.is_null() {+ // SAFETY: `old` was a handle this module owned.+ unsafe { windows_sys::Win32::Foundation::CloseHandle(old) };+ }+ }++ pub(super) unsafe fn install() {+ // SAFETY: PREVIOUS is written here, once, before the filter that+ // reads it can run.+ unsafe { PREVIOUS = SetUnhandledExceptionFilter(Some(on_exception)) };+ }++ unsafe extern "system" fn on_exception(pointers: *const EXCEPTION_POINTERS) -> i32 {+ if !CRASHING.swap(true, Ordering::SeqCst) {+ // SAFETY: Windows hands the filter a valid record.+ let line = unsafe { describe(pointers) };+ write(&line);+ }+ // SAFETY: as `install` says.+ match unsafe { PREVIOUS } {+ // SAFETY: the filter that was installed before this one.+ Some(previous) => unsafe { previous(pointers) },+ // EXCEPTION_CONTINUE_SEARCH: on to Windows Error Reporting.+ None => 0,+ }+ }++ unsafe fn describe(pointers: *const EXCEPTION_POINTERS) -> Line {+ let mut line = Line::new();+ let _ = write!(line, "FATAL [");+ // A thread Rust started has its name already, so this is a read;+ // on one a library started it is "?".+ let _ = write!(line, "{}", std::thread::current().name().unwrap_or("?"));+ let _ = write!(line, "] crash");+ // SAFETY: as `on_exception` says.+ let record = unsafe { (*pointers).ExceptionRecord.as_ref() };+ let Some(record) = record else {+ let _ = writeln!(line);+ return line;+ };+ let code = record.ExceptionCode as u32;+ let _ = write!(line, ": {code:#010X}");+ if let Some((_, name)) = CODES.iter().find(|(c, _)| *c == code) {+ let _ = write!(line, " {name}");+ }+ if matches!(code, 0xC000_0005 | 0xC000_0006) && record.NumberParameters >= 2 {+ let what = match record.ExceptionInformation[0] {+ 0 => "reading",+ 1 => "writing",+ 8 => "executing",+ _ => "at",+ };+ let _ = write!(line, ", {what} {:#x},", record.ExceptionInformation[1]);+ }+ where_(&mut line, record.ExceptionAddress as usize);+ let _ = writeln!(line);+ line+ }++ /// ` in foo.dll+0x1234`, or the bare address when no module has it.+ fn where_(line: &mut Line, address: usize) {+ let mut module: HMODULE = std::ptr::null_mut();+ let mut path = [0u16; 260];+ // SAFETY: the flags make GetModuleHandleExW take an address and+ // leave the module's reference count alone; the path is written+ // within the buffer's length.+ let found = unsafe {+ GetModuleHandleExW(+ GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS+ | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,+ address as *const u16,+ &mut module,+ ) != 0+ };+ let len = if found {+ // SAFETY: as above.+ unsafe { GetModuleFileNameW(module, path.as_mut_ptr(), path.len() as u32) as usize }+ } else {+ 0+ };+ if len == 0 {+ let _ = write!(line, " at {address:#x}");+ return;+ }+ // The name, decoded into a buffer on the stack.+ let mut name = Line::new();+ for c in char::decode_utf16(path[..len].iter().copied()) {+ let _ = name.write_char(c.unwrap_or(char::REPLACEMENT_CHARACTER));+ }+ let name = std::str::from_utf8(name.bytes()).unwrap_or("?");+ let offset = address.wrapping_sub(module as usize);+ let _ = write!(line, " in {}+{offset:#x}", file_name(name));+ }++ fn write(line: &Line) {+ let bytes = line.bytes();+ let put = |handle: HANDLE| {+ if handle.is_null() || handle as isize == -1 {+ return;+ }+ let mut written = 0u32;+ // SAFETY: a handle this process holds; a failed write is+ // ignored, there being nowhere left to say so.+ unsafe {+ WriteFile(+ handle,+ bytes.as_ptr(),+ bytes.len() as u32,+ &mut written,+ std::ptr::null_mut(),+ );+ }+ };+ // SAFETY: GetStdHandle only reads the process's parameters.+ put(unsafe { GetStdHandle(STD_ERROR_HANDLE) });+ put(FILE.load(Ordering::SeqCst));+ }+}++#[cfg(not(any(unix, windows)))]+mod platform {+ pub(super) fn keep(_: std::fs::File) {}+ pub(super) unsafe fn install() {}+}++#[cfg(test)]+mod tests {+ use super::*;+ use std::fmt::Write as _;++ #[test]+ fn a_line_that_does_not_fit_is_cut_off() {+ let mut line = Line::new();+ for _ in 0..200 {+ let _ = write!(line, "0123456789");+ }+ assert_eq!(line.bytes().len(), 1024);+ }++ /// Crashes a copy of this test binary on purpose and reads what it+ /// left on standard error.+ #[test]+ fn a_crash_says_what_it_was_and_where() {+ const CHILD: &str = "CONCAT_CRASH_TEST_CHILD";+ if std::env::var_os(CHILD).is_some() {+ catch();+ // SAFETY: none; this is the crash.+ unsafe { std::ptr::without_provenance_mut::<u8>(0x10).write_volatile(1) };+ unreachable!("the write above should have crashed");+ }+ let out = std::process::Command::new(std::env::current_exe().unwrap())+ .args([+ "--exact",+ "crash::tests::a_crash_says_what_it_was_and_where",+ ])+ .args(["--nocapture", "--test-threads=1"])+ .env(CHILD, "1")+ .output()+ .unwrap();+ let stderr = String::from_utf8_lossy(&out.stderr);+ assert!(!out.status.success(), "the child did not crash: {stderr}");+ let line = stderr+ .lines()+ .find(|line| line.starts_with("FATAL ["))+ .unwrap_or_else(|| panic!("no crash line in: {stderr}"));+ #[cfg(unix)]+ assert!(+ line.contains("SIGSEGV") || line.contains("SIGBUS"),+ "{line}"+ );+ #[cfg(windows)]+ assert!(+ line.contains("EXCEPTION_ACCESS_VIOLATION, writing 0x10"),+ "{line}"+ );+ assert!(line.contains(" in "), "no module in: {line}");+ }++ #[test]+ fn a_module_is_named_without_its_folder() {+ assert_eq!(+ file_name(r"C:\Program Files\Concat\onnxruntime.dll"),+ "onnxruntime.dll"+ );+ assert_eq!(+ file_name("/usr/lib/libonnxruntime.so.1.28.0"),+ "libonnxruntime.so.1.28.0"+ );+ assert_eq!(file_name("concat"), "concat");+ }+}diff --git a/src/crates/concat-host/src/lib.rs b/src/crates/concat-host/src/lib.rsindex 6a978b10..727862f8 100644--- a/src/crates/concat-host/src/lib.rs+++ b/src/crates/concat-host/src/lib.rs@@ -20,12 +20,14 @@pub mod brush;pub mod cards;+pub mod crash;pub mod cutout;pub mod dirs;pub mod enhance;pub mod export;pub mod jobs;pub mod logs;+pub mod machine;pub mod media;pub mod memory;pub mod models;diff --git a/src/crates/concat-host/src/logs.rs b/src/crates/concat-host/src/logs.rsindex 98256c00..af7ee3cf 100644--- a/src/crates/concat-host/src/logs.rs+++ b/src/crates/concat-host/src/logs.rs@@ -75,6 +75,7 @@ pub fn open(dirs: &AppDirs, extra: Option<Box<dyn log::Log>>) -> Result<PathBuf,Ok((path, file)) => match file.try_clone() {Ok(file) => {let _ = CURRENT.set(path.clone());+ crate::crash::write_to(&file);Some(Sink {file,written: 0,@@ -108,12 +109,10 @@ fn install(sink: Option<Sink>, extra: Option<Box<dyn log::Log>>) -> Result<(), Slog::set_boxed_logger(Box::new(logger)).map(|()| log::set_max_level(level)).map_err(|_| "a logger was already installed".to_owned())?;- log::info!(- "Concat {} · {} {} · {level}",- env!("CARGO_PKG_VERSION"),- std::env::consts::OS,- std::env::consts::ARCH,- );+ // What every report is asked first, answered before it is asked.+ log::info!("{} · log level {level}", crate::machine::build());+ log::info!("system: {}", crate::machine::system());+ log::info!("cpu: {}", crate::machine::cpu());Ok(())}diff --git a/src/crates/concat-host/src/machine.rs b/src/crates/concat-host/src/machine.rsnew file mode 100644index 00000000..e169988d--- /dev/null+++ b/src/crates/concat-host/src/machine.rs@@ -0,0 +1,334 @@+// SPDX-License-Identifier: AGPL-3.0-or-later+// SPDX-FileCopyrightText: 2026 Jareer and Concat contributors++//! The machine a run is on, as the first lines of its log say it.+//!+//! The questions every crash report starts with - which Windows, which+//! CPU, does it have AVX2, which build is this - each took a round trip+//! with the reporter (#313, #318). The log now answers them before it is+//! asked. Nothing here is sent anywhere; it is written into the run's own+//! log file like every other line.++/// One line about the build: the version, the target, and the commit CI+/// built it from - "local" for a build from a working tree.+pub fn build() -> String {+ format!(+ "Concat {} · {}-{} · commit {}",+ env!("CARGO_PKG_VERSION"),+ std::env::consts::OS,+ std::env::consts::ARCH,+ option_env!("GITHUB_SHA").map_or("local", |sha| &sha[..sha.len().min(12)]),+ )+}++/// One line about the system: its name and version, and the device where+/// a phone says which one it is.+pub fn system() -> String {+ os::describe().unwrap_or_else(|| std::env::consts::OS.to_owned())+}++/// One line about the CPU: its name where the system says it, how many+/// threads it runs, and which of the instruction sets the libraries Concat+/// links care about it has - the ones a build for a newer CPU than the+/// one it is on dies over.+pub fn cpu() -> String {+ let mut line = cpu_name().unwrap_or_else(|| std::env::consts::ARCH.to_owned());+ let threads = std::thread::available_parallelism().map_or(0, |n| n.get());+ line.push_str(&format!(" · {threads} threads"));+ let (has, lacks) = features();+ if !has.is_empty() {+ line.push_str(&format!(" · has {}", has.join(" ")));+ }+ if !lacks.is_empty() {+ line.push_str(&format!(" · lacks {}", lacks.join(" ")));+ }+ line+}++/// The instruction sets worth naming, split into those the CPU has and+/// those it does not.+fn features() -> (Vec<&'static str>, Vec<&'static str>) {+ #[allow(unused_mut)]+ let mut found: Vec<(&'static str, bool)> = Vec::new();+ #[cfg(target_arch = "x86_64")]+ {+ found.extend([+ ("sse4.2", std::arch::is_x86_feature_detected!("sse4.2")),+ ("avx", std::arch::is_x86_feature_detected!("avx")),+ ("avx2", std::arch::is_x86_feature_detected!("avx2")),+ ("fma", std::arch::is_x86_feature_detected!("fma")),+ ("bmi2", std::arch::is_x86_feature_detected!("bmi2")),+ ("f16c", std::arch::is_x86_feature_detected!("f16c")),+ ("avx512f", std::arch::is_x86_feature_detected!("avx512f")),+ ]);+ }+ #[cfg(target_arch = "aarch64")]+ {+ found.extend([+ ("neon", std::arch::is_aarch64_feature_detected!("neon")),+ ("fp16", std::arch::is_aarch64_feature_detected!("fp16")),+ (+ "dotprod",+ std::arch::is_aarch64_feature_detected!("dotprod"),+ ),+ ("i8mm", std::arch::is_aarch64_feature_detected!("i8mm")),+ ("sve", std::arch::is_aarch64_feature_detected!("sve")),+ ]);+ }+ let has = found+ .iter()+ .filter(|(_, on)| *on)+ .map(|(n, _)| *n)+ .collect();+ let lacks = found+ .iter()+ .filter(|(_, on)| !*on)+ .map(|(n, _)| *n)+ .collect();+ (has, lacks)+}++/// The CPU's marketing name.+fn cpu_name() -> Option<String> {+ #[cfg(windows)]+ {+ os::registry(+ r"HARDWARE\DESCRIPTION\System\CentralProcessor\0",+ "ProcessorNameString",+ )+ }+ #[cfg(target_vendor = "apple")]+ {+ os::sysctl("machdep.cpu.brand_string")+ }+ #[cfg(all(not(windows), not(target_vendor = "apple"), target_arch = "x86_64"))]+ {+ x86_brand()+ }+ #[cfg(all(+ not(windows),+ not(target_vendor = "apple"),+ not(target_arch = "x86_64")+ ))]+ {+ None+ }+}++/// The brand string CPUID leaves 0x80000002-4 spell out.+#[cfg(all(not(windows), not(target_vendor = "apple"), target_arch = "x86_64"))]+fn x86_brand() -> Option<String> {+ use std::arch::x86_64::__cpuid;+ // SAFETY: CPUID is on every x86-64 CPU.+ let top = unsafe { __cpuid(0x8000_0000) }.eax;+ if top < 0x8000_0004 {+ return None;+ }+ let mut bytes = Vec::with_capacity(48);+ for leaf in 0x8000_0002u32..=0x8000_0004 {+ // SAFETY: as above, and the leaf is within the range just read.+ let r = unsafe { __cpuid(leaf) };+ for word in [r.eax, r.ebx, r.ecx, r.edx] {+ bytes.extend_from_slice(&word.to_le_bytes());+ }+ }+ let name = String::from_utf8_lossy(&bytes);+ let name = name.trim_matches(char::from(0)).trim();+ (!name.is_empty()).then(|| name.to_owned())+}++#[cfg(windows)]+mod os {+ use windows_sys::Win32::System::Registry::{HKEY_LOCAL_MACHINE, RRF_RT_REG_SZ, RegGetValueW};++ const VERSION_KEY: &str = r"SOFTWARE\Microsoft\Windows NT\CurrentVersion";++ /// "Windows 11 Pro 24H2 (build 26100.4061)". The product name in the+ /// registry still says Windows 10 on Windows 11, so the build number+ /// decides which it is.+ pub(super) fn describe() -> Option<String> {+ let build: u32 = registry(VERSION_KEY, "CurrentBuildNumber")?.parse().ok()?;+ let product = registry(VERSION_KEY, "ProductName").unwrap_or_default();+ let product = if build >= 22000 {+ product.replace("Windows 10", "Windows 11")+ } else {+ product+ };+ let display = registry(VERSION_KEY, "DisplayVersion").unwrap_or_default();+ let revision =+ registry_dword(VERSION_KEY, "UBR").map_or(String::new(), |ubr| format!(".{ubr}"));+ Some(format!("{product} {display} (build {build}{revision})").replace(" ", " "))+ }++ /// A string value under HKEY_LOCAL_MACHINE.+ pub(super) fn registry(key: &str, value: &str) -> Option<String> {+ let key = wide(key);+ let value = wide(value);+ let mut buf = [0u16; 256];+ let mut size = std::mem::size_of_val(&buf) as u32;+ // SAFETY: the buffers are NUL-terminated and `size` is the+ // output buffer's length in bytes.+ let status = unsafe {+ RegGetValueW(+ HKEY_LOCAL_MACHINE,+ key.as_ptr(),+ value.as_ptr(),+ RRF_RT_REG_SZ,+ std::ptr::null_mut(),+ buf.as_mut_ptr().cast(),+ &mut size,+ )+ };+ if status != 0 {+ return None;+ }+ let len = (size as usize / 2).saturating_sub(1).min(buf.len());+ let text = String::from_utf16_lossy(&buf[..len]);+ let text = text.trim_matches(char::from(0)).trim();+ (!text.is_empty()).then(|| text.to_owned())+ }++ /// A number value under HKEY_LOCAL_MACHINE.+ fn registry_dword(key: &str, value: &str) -> Option<u32> {+ use windows_sys::Win32::System::Registry::RRF_RT_REG_DWORD;+ let key = wide(key);+ let value = wide(value);+ let mut out = 0u32;+ let mut size = 4u32;+ // SAFETY: as in `registry`, with a four-byte output.+ let status = unsafe {+ RegGetValueW(+ HKEY_LOCAL_MACHINE,+ key.as_ptr(),+ value.as_ptr(),+ RRF_RT_REG_DWORD,+ std::ptr::null_mut(),+ (&mut out as *mut u32).cast(),+ &mut size,+ )+ };+ (status == 0).then_some(out)+ }++ fn wide(text: &str) -> Vec<u16> {+ text.encode_utf16().chain(std::iter::once(0)).collect()+ }+}++#[cfg(target_vendor = "apple")]+mod os {+ /// "macOS 15.6.1 · Mac15,6", or "iOS 26.0 · iPhone17,1".+ pub(super) fn describe() -> Option<String> {+ let version = sysctl("kern.osproductversion")?;+ let name = if cfg!(target_os = "ios") {+ "iOS"+ } else {+ "macOS"+ };+ let model = sysctl(if cfg!(target_os = "ios") {+ "hw.machine"+ } else {+ "hw.model"+ });+ Some(match model {+ Some(model) => format!("{name} {version} · {model}"),+ None => format!("{name} {version}"),+ })+ }++ /// A string the kernel answers by name.+ pub(super) fn sysctl(name: &str) -> Option<String> {+ let name = std::ffi::CString::new(name).ok()?;+ let mut buf = [0u8; 256];+ let mut len = buf.len();+ // SAFETY: `len` is the buffer's length, and sysctlbyname writes no+ // more than that.+ let status = unsafe {+ libc::sysctlbyname(+ name.as_ptr(),+ buf.as_mut_ptr().cast(),+ &mut len,+ std::ptr::null_mut(),+ 0,+ )+ };+ if status != 0 {+ return None;+ }+ let text = std::ffi::CStr::from_bytes_until_nul(&buf[..len.min(buf.len())])+ .ok()?+ .to_str()+ .ok()?+ .trim();+ (!text.is_empty()).then(|| text.to_owned())+ }+}++#[cfg(target_os = "android")]+mod os {+ /// "Android 11 (API 30) · Google Pixel 4a".+ pub(super) fn describe() -> Option<String> {+ let release = property("ro.build.version.release")?;+ let api = property("ro.build.version.sdk").unwrap_or_default();+ let maker = property("ro.product.manufacturer").unwrap_or_default();+ let model = property("ro.product.model").unwrap_or_default();+ Some(+ format!("Android {release} (API {api}) · {maker} {model}")+ .trim()+ .to_owned(),+ )+ }++ fn property(name: &str) -> Option<String> {+ let name = std::ffi::CString::new(name).ok()?;+ let mut buf = [0u8; 92]; // PROP_VALUE_MAX+ // SAFETY: the buffer is PROP_VALUE_MAX bytes, all a value can be.+ let len = unsafe { libc::__system_property_get(name.as_ptr(), buf.as_mut_ptr().cast()) };+ if len <= 0 {+ return None;+ }+ let text = String::from_utf8_lossy(&buf[..len as usize])+ .trim()+ .to_owned();+ (!text.is_empty()).then_some(text)+ }+}++#[cfg(all(unix, not(target_vendor = "apple"), not(target_os = "android")))]+mod os {+ /// "Linux Mint 22.3 · kernel 7.0.0-10-generic".+ pub(super) fn describe() -> Option<String> {+ let release = std::fs::read_to_string("/etc/os-release")+ .ok()+ .and_then(|text| {+ text.lines()+ .find_map(|line| line.strip_prefix("PRETTY_NAME="))+ .map(|name| name.trim_matches('"').to_owned())+ })+ .unwrap_or_else(|| "Linux".to_owned());+ let kernel = std::fs::read_to_string("/proc/sys/kernel/osrelease").ok();+ Some(match kernel {+ Some(kernel) => format!("{release} · kernel {}", kernel.trim()),+ None => release,+ })+ }+}++#[cfg(not(any(unix, windows)))]+mod os {+ pub(super) fn describe() -> Option<String> {+ None+ }+}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn the_lines_say_something() {+ assert!(build().starts_with(&format!("Concat {}", env!("CARGO_PKG_VERSION"))));+ assert!(!system().is_empty());+ assert!(cpu().contains("threads"));+ }+}diff --git a/src/crates/concat/src/lib.rs b/src/crates/concat/src/lib.rsindex 0fed29b9..2ba05931 100644--- a/src/crates/concat/src/lib.rs+++ b/src/crates/concat/src/lib.rs@@ -103,6 +103,7 @@ use ui::*;/// warnings, a panic, the lines below - is written down in/// `<app data>/logs/` as well as said out loud. See `concat_host::logs`.pub fn open_logging(extra: Option<Box<dyn log::Log>>) {+ concat_host::crash::catch();concat_host::logs::catch_panics();let opened = match concat_host::AppDirs::locate() {Ok(dirs) => concat_host::logs::open(&dirs, extra).map(|_| ()),diff --git a/src/crates/concat/src/main.rs b/src/crates/concat/src/main.rsindex 6f54e8a8..564c8e43 100644--- a/src/crates/concat/src/main.rs+++ b/src/crates/concat/src/main.rs@@ -12,6 +12,11 @@ mod launch;use launch::Launch;fn main() -> Result<(), slint::PlatformError> {+ // First of all, so that even `--version` says where it crashed if it+ // does: v0.2.7-rc.4's Windows on ARM build died there with nothing+ // but an exit code (#326). It writes to standard error until the log+ // opens, and to both after.+ concat_host::crash::catch();let launch = launch::parse(std::env::args_os().skip(1));// Before the log, the GPU and the window: all this proves is that the// executable and the libraries it links were loaded, and it should