A panic solidcore's guard catches reaches the engine as EngineError::Internal
smlib's run() catches a solidcore panic and replaces the kernel (kept: the app does not die natively), returning Error::Internal; the kernel turned it into KernelError::Failed and the engine into Other, so hostile_params_never_panic and the fuzzer no longer saw kernel panics. Now the kernel's wrapper errors go through KernelError::smlib, which keeps Internal; a body the wrapper could not take because of a panic is Internal too. A feature whose evaluation hit it records the result as internal (not cached: the kernel was replaced), and the command that made it, or any command whose kernel call returns it, fails with EngineError::Internal under its own id. Crash reports mark a panic inside a solidcore call recovered (kernel::guarded), and the crash test's kernel mode panics inside a solidcore call. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DtL5M41G8Nua7HvuFPZkmJ
This commit is contained in:
@@ -123,8 +123,8 @@ mod imp {
|
||||
use crate::native::{body_from_smlib_ordered, smlib_body_ordered};
|
||||
use crate::{KernelError, Result, guard, record};
|
||||
|
||||
fn failed(what: &str, step: &str, e: impl std::fmt::Display) -> KernelError {
|
||||
KernelError::Failed(format!("SMLib {what}: {step}: {e}"))
|
||||
fn failed(what: &str, step: &str, e: impl crate::Cause) -> KernelError {
|
||||
KernelError::smlib(format!("SMLib {what}: {step}"), e)
|
||||
}
|
||||
|
||||
/// How far a pick may lie from its edge.
|
||||
|
||||
@@ -420,6 +420,8 @@ pub struct Refusal {
|
||||
pub surface: Option<SurfaceKind>,
|
||||
/// SMLib's reason for the whole body, as the log words it.
|
||||
pub why: String,
|
||||
/// The reason is a panic the wrapper caught, not a refusal ([`KernelError::Internal`]).
|
||||
pub internal: bool,
|
||||
}
|
||||
|
||||
/// Where a point lies against a body ([`Body::classify_point`]).
|
||||
@@ -451,13 +453,16 @@ const MAX_REFUSAL_PROBES: usize = 20_000;
|
||||
impl Refusal {
|
||||
/// The refusal of `record` for `error`, with the face SMLib refuses on its own.
|
||||
fn of(record: &Brep, error: &solidcraft_smlib::Error) -> Refusal {
|
||||
if let solidcraft_smlib::Error::Internal(why) = error {
|
||||
return Refusal { face: None, surface: None, why: why.clone(), internal: true };
|
||||
}
|
||||
let _span = solidcraft_profile::span("kernel", "smlib_refusal_face");
|
||||
let alone = |f: usize| crate::surfaces::Parts::of(record, &[(f, false)]).and_then(|p| p.canonical()).map(|c| c.brep);
|
||||
let face = (record.faces.len() <= MAX_REFUSAL_PROBES)
|
||||
.then(|| (0..record.faces.len()).find(|&f| alone(f).is_ok_and(|r| solidcraft_smlib::Brep::from_record(&r, true).is_err())))
|
||||
.flatten();
|
||||
let surface = face.and_then(|f| record.faces.get(f)).map(|f| SurfaceKind::of(&f.surface));
|
||||
Refusal { face, surface, why: error.to_string() }
|
||||
Refusal { face, surface, why: error.to_string(), internal: false }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -540,7 +545,7 @@ impl Body {
|
||||
self.smlib
|
||||
.get_or_init(|| solidcraft_smlib::Brep::from_record(&self.record, true).map_err(|e| Refusal::of(&self.record, &e)))
|
||||
.as_ref()
|
||||
.map_err(|r| KernelError::Failed(format!("SMLib: {}", r.why)))
|
||||
.map_err(|r| if r.internal { KernelError::Internal(format!("SMLib: {}", r.why)) } else { KernelError::Failed(format!("SMLib: {}", r.why)) })
|
||||
}
|
||||
|
||||
/// Why SMLib refuses the body, if it does: the body is sent into SMLib as an operation
|
||||
@@ -1047,7 +1052,7 @@ impl Body {
|
||||
if self.is_mesh() {
|
||||
return Ok(self.outline_bounds());
|
||||
}
|
||||
let (lo, hi) = self.smlib()?.bounds().map_err(|e| KernelError::Failed(format!("SMLib bounds: {e}")))?;
|
||||
let (lo, hi) = self.smlib()?.bounds().map_err(|e| KernelError::smlib("SMLib bounds", e))?;
|
||||
Ok(Aabb3 { min: v3(lo), max: v3(hi) })
|
||||
}
|
||||
|
||||
@@ -1380,7 +1385,7 @@ impl Body {
|
||||
PointClass::Outside
|
||||
});
|
||||
}
|
||||
let class = self.smlib()?.classify_point([p.x, p.y, p.z]).map_err(|e| KernelError::Failed(format!("SMLib point classification: {e}")))?;
|
||||
let class = self.smlib()?.classify_point([p.x, p.y, p.z]).map_err(|e| KernelError::smlib("SMLib point classification", e))?;
|
||||
Ok(match class {
|
||||
solidcraft_smlib::PointClass::Inside => PointClass::Inside,
|
||||
solidcraft_smlib::PointClass::Outside => PointClass::Outside,
|
||||
|
||||
@@ -919,7 +919,7 @@ pub fn fair_faces(body: &Body, faces: &[usize], opts: &FairOptions) -> Result<(B
|
||||
if changed.is_empty() {
|
||||
return Ok((body.clone(), report));
|
||||
}
|
||||
let smlib = solidcraft_smlib::Brep::from_record(&record, true).map_err(|e| KernelError::Failed(format!("fair: SMLib: {e}")))?;
|
||||
let smlib = solidcraft_smlib::Brep::from_record(&record, true).map_err(|e| KernelError::smlib("fair: SMLib", e))?;
|
||||
let (result, order) = crate::native::body_from_smlib_ordered(&smlib)?;
|
||||
// The topology is the body's own, so SMLib's order is the body's: each face kept or
|
||||
// modified, each edge kept. Where SMLib's order is not the record's the history is not known.
|
||||
|
||||
@@ -795,7 +795,7 @@ fn patch_body(body: &Body, record: &b::Brep, chain: &[(usize, bool)], surface: b
|
||||
}
|
||||
let mut made = assemble(Parts { vertices, edges, faces: vec![PartFace { surface, forward: true, loops: vec![uses] }], tolerance: tol }, false)?;
|
||||
hold_gap(&mut made.brep, tol);
|
||||
let smlib = solidcraft_smlib::Brep::from_record(&made.brep, true).map_err(|e| KernelError::Failed(format!("patch: SMLib: {e}")))?;
|
||||
let smlib = solidcraft_smlib::Brep::from_record(&made.brep, true).map_err(|e| KernelError::smlib("patch: SMLib", e))?;
|
||||
let (patch, order) = crate::native::body_from_smlib_ordered(&smlib)?;
|
||||
// The patch's edges in its own order, each from the loop edge it is.
|
||||
let Some(order) = order else { return Ok(patch) };
|
||||
@@ -920,7 +920,7 @@ fn quilt_body(body: &Body, record: &b::Brep, chain: &[Piece], fill: &IsoFill, to
|
||||
}
|
||||
let mut made = assemble(Parts { vertices, edges, faces, tolerance: tol }, false)?;
|
||||
hold_gap(&mut made.brep, tol);
|
||||
let smlib = solidcraft_smlib::Brep::from_record(&made.brep, true).map_err(|e| KernelError::Failed(format!("patch: SMLib: {e}")))?;
|
||||
let smlib = solidcraft_smlib::Brep::from_record(&made.brep, true).map_err(|e| KernelError::smlib("patch: SMLib", e))?;
|
||||
let (sheet, order) = crate::native::body_from_smlib_ordered(&smlib)?;
|
||||
let Some(order) = order else { return Ok(sheet) };
|
||||
let place = |list: &[usize], c: usize| if list.is_empty() { Some(c) } else { list.get(c).copied() };
|
||||
|
||||
@@ -22,8 +22,8 @@ pub struct MeetingEdge {
|
||||
pub points: Vec<Vec3>,
|
||||
}
|
||||
|
||||
fn failed(what: &str, e: impl std::fmt::Display) -> KernelError {
|
||||
KernelError::Failed(format!("SMLib intersection curve: {what}: {e}"))
|
||||
fn failed(what: &str, e: impl crate::Cause) -> KernelError {
|
||||
KernelError::smlib(format!("SMLib intersection curve: {what}"), e)
|
||||
}
|
||||
|
||||
/// `body` itself, or its faces `faces` (in its face order) alone as a surface body.
|
||||
|
||||
@@ -256,6 +256,48 @@ pub enum KernelError {
|
||||
|
||||
pub type Result<T> = std::result::Result<T, KernelError>;
|
||||
|
||||
/// Why a kernel step failed: a message, or an error of the solidcore wrapper, which may be a
|
||||
/// panic the wrapper caught ([`solidcraft_smlib::Error::Internal`]).
|
||||
pub(crate) trait Cause: std::fmt::Display {
|
||||
/// The panic's message, when the cause is one.
|
||||
fn panic(&self) -> Option<&str> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl Cause for solidcraft_smlib::Error {
|
||||
fn panic(&self) -> Option<&str> {
|
||||
match self {
|
||||
solidcraft_smlib::Error::Internal(m) => Some(m),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Cause for str {}
|
||||
impl Cause for String {}
|
||||
impl Cause for solidcraft_smlib::CheckReport {}
|
||||
impl Cause for solidcraft_smlib::export::ExportError {}
|
||||
impl Cause for solidcraft_geom::brep::history::Failed {}
|
||||
impl Cause for solidcraft_geom::brep::pcurve::Error {}
|
||||
impl<T: Cause + ?Sized> Cause for &T {
|
||||
fn panic(&self) -> Option<&str> {
|
||||
(**self).panic()
|
||||
}
|
||||
}
|
||||
|
||||
impl KernelError {
|
||||
/// A kernel step's failure, `context` before its cause: a panic the solidcore wrapper caught
|
||||
/// (its kernel replaced by a fresh one) stays an internal error ([`KernelError::Internal`]),
|
||||
/// so the engine reports it as one; any other cause is the operation's failure.
|
||||
pub(crate) fn smlib(context: impl std::fmt::Display, e: impl Cause) -> KernelError {
|
||||
match e.panic() {
|
||||
Some(m) => KernelError::Internal(format!("{context}: {m}")),
|
||||
None => KernelError::Failed(format!("{context}: {e}")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The native kernel call running right now (SMLib), for crash reports.
|
||||
pub fn native_call_in_flight() -> Option<String> {
|
||||
solidcraft_smlib::current_call()
|
||||
@@ -268,7 +310,13 @@ thread_local! {
|
||||
/// Is this thread inside [`guard`]? (A panic now will be caught and turned into an error; the
|
||||
/// engine's crash reports mark it recovered.)
|
||||
pub fn guarded() -> bool {
|
||||
GUARDED.with(std::cell::Cell::get) > 0
|
||||
GUARDED.with(std::cell::Cell::get) > 0 || solidcraft_smlib::in_call()
|
||||
}
|
||||
|
||||
/// A panic inside a solidcore call, caught there and reported as the kernel's internal error
|
||||
/// ([`KernelError::Internal`]): the crash-report check (`diagnostics.crash_test`'s kernel mode).
|
||||
pub fn crash_test() -> Result<()> {
|
||||
solidcraft_smlib::crash_test().map_err(|e| KernelError::smlib("crash test", e))
|
||||
}
|
||||
|
||||
/// Run third-party kernel code, converting a panic into an error.
|
||||
|
||||
@@ -102,7 +102,7 @@ pub fn measure_mass(b: &Body) -> Result<MassMeasure> {
|
||||
return Ok(MassMeasure { volume: m.volume, area: m.area, centroid: m.centroid, bbox: m.bbox, measured_by: m.measured_by, notes: m.notes });
|
||||
}
|
||||
let m = crate::guard("SMLib mass properties", || {
|
||||
b.smlib()?.mass_properties(ACCURACY).map_err(|e| crate::KernelError::Failed(format!("SMLib mass properties: {e}")))
|
||||
b.smlib()?.mass_properties(ACCURACY).map_err(|e| crate::KernelError::smlib("SMLib mass properties", e))
|
||||
})?;
|
||||
let [x, y, z] = m.centroid;
|
||||
Ok(MassMeasure { volume: m.volume, area: m.area, centroid: Vec3::new(x, y, z), bbox: b.exact_bounds()?, measured_by: "smlib", notes: Vec::new() })
|
||||
|
||||
@@ -87,7 +87,7 @@ pub fn brep_record(body: &Body) -> Result<solidcraft_geom::brep::Brep> {
|
||||
/// The body in SMLib (see [`Body::smlib`]), as an owned copy.
|
||||
pub fn smlib_body(body: &Body) -> Result<solidcraft_smlib::Brep> {
|
||||
let _span = solidcraft_profile::span("kernel", "smlib_body");
|
||||
body.smlib()?.try_clone().map_err(|e| KernelError::Failed(format!("SMLib: {e}")))
|
||||
body.smlib()?.try_clone().map_err(|e| KernelError::smlib("SMLib", e))
|
||||
}
|
||||
|
||||
/// Decode bounded untrusted data. Validate indices before topology extraction.
|
||||
@@ -140,7 +140,7 @@ pub(crate) fn body_from_smlib_ordered(brep: &solidcraft_smlib::Brep) -> Result<(
|
||||
/// the body's record does. `None` where it does not.
|
||||
pub(crate) fn smlib_body_ordered(body: &Body) -> Result<(solidcraft_smlib::Brep, Option<solidcraft_smlib::history::Order>)> {
|
||||
let brep = smlib_body(body)?;
|
||||
Ok((brep.try_clone().map_err(|e| KernelError::Failed(format!("SMLib: {e}")))?, numbering(body.record(), &brep)))
|
||||
Ok((brep.try_clone().map_err(|e| KernelError::smlib("SMLib", e))?, numbering(body.record(), &brep)))
|
||||
}
|
||||
|
||||
/// `record` in SMLib, imported as given (SMLib's own form; a record it refuses is an error that
|
||||
@@ -149,7 +149,7 @@ pub(crate) fn smlib_record_ordered(
|
||||
record: &solidcraft_geom::brep::Brep,
|
||||
) -> Result<(solidcraft_smlib::Brep, Option<solidcraft_smlib::history::Order>)> {
|
||||
let _span = solidcraft_profile::span("kernel", "smlib_record_ordered");
|
||||
let brep = solidcraft_smlib::Brep::from_record(record, true).map_err(|e| KernelError::Failed(format!("SMLib: {e}")))?;
|
||||
let brep = solidcraft_smlib::Brep::from_record(record, true).map_err(|e| KernelError::smlib("SMLib", e))?;
|
||||
let order = numbering(record, &brep);
|
||||
Ok((brep, order))
|
||||
}
|
||||
|
||||
@@ -22,8 +22,8 @@ use crate::history::{Change, EdgeOrigin, FaceOrigin, History};
|
||||
use crate::native::{body_from_smlib_ordered, smlib_body_ordered};
|
||||
use crate::{KernelError, Result, record};
|
||||
|
||||
fn failed(op: &str, what: &str, e: impl std::fmt::Display) -> KernelError {
|
||||
KernelError::Failed(format!("SMLib {op}: {what}: {e}"))
|
||||
fn failed(op: &str, what: &str, e: impl crate::Cause) -> KernelError {
|
||||
KernelError::smlib(format!("SMLib {op}: {what}"), e)
|
||||
}
|
||||
|
||||
/// The error for SMLib's refusal of `op`, in the words of the operation; `wall` is its wall's
|
||||
|
||||
@@ -206,9 +206,9 @@ fn moved_from(history: Option<&History>, f: usize) -> Option<usize> {
|
||||
|
||||
/// `body` moved by a matrix that is not a rigid motion, in SMLib, its history from SMLib's tags.
|
||||
fn transformed(body: &Body, m: &[[f64; 4]; 4]) -> Result<Body> {
|
||||
let failed = |what: &str, e: &dyn std::fmt::Display| KernelError::Failed(format!("SMLib transform: {what}: {e}"));
|
||||
let failed = |what: &str, e: solidcraft_smlib::Error| KernelError::smlib(format!("SMLib transform: {what}"), e);
|
||||
let (handle, order) = crate::native::smlib_body_ordered(body)?;
|
||||
let moved = handle.tagged(0).map_err(|e| failed("tag", &e))?.transformed(m).map_err(|e| failed("operation", &e))?;
|
||||
let moved = handle.tagged(0).map_err(|e| failed("tag", e))?.transformed(m).map_err(|e| failed("operation", e))?;
|
||||
let (result, result_order) = crate::native::body_from_smlib_ordered(&moved)?;
|
||||
let history = match (order, result_order) {
|
||||
(Some(o), Some(r)) => moved.history().ok().and_then(|h| h.history(&[o], &r).ok()),
|
||||
|
||||
@@ -21,8 +21,8 @@ fn edge_middle(e: &brep::Edge) -> Option<Vec3> {
|
||||
CurveEval::new(e.parameters()).ok()?.point(0.5 * (t0 + t1))
|
||||
}
|
||||
|
||||
fn failed(e: impl std::fmt::Display) -> KernelError {
|
||||
KernelError::Failed(format!("SMLib planar regularization: {e}"))
|
||||
fn failed(e: impl crate::Cause) -> KernelError {
|
||||
KernelError::smlib("SMLib planar regularization", e)
|
||||
}
|
||||
|
||||
impl Body {
|
||||
|
||||
@@ -118,8 +118,8 @@ mod sheets {
|
||||
use super::*;
|
||||
use crate::prism_curve::{Run, projected};
|
||||
|
||||
fn failed(what: &str, e: impl std::fmt::Display) -> KernelError {
|
||||
KernelError::Failed(format!("SMLib planar regions: {what}: {e}"))
|
||||
fn failed(what: &str, e: impl crate::Cause) -> KernelError {
|
||||
KernelError::smlib(format!("SMLib planar regions: {what}"), e)
|
||||
}
|
||||
|
||||
/// `regions` (in the plane's own coordinates) as one sheet on z = 0. SMLib's face builder
|
||||
|
||||
@@ -1221,7 +1221,7 @@ fn sew_impl(faces: &[FaceSpec], touching: bool, sources: Option<&[Option<crate::
|
||||
}
|
||||
}
|
||||
let made = assemble(Parts { vertices, edges, faces: out, tolerance: tol }, true)?;
|
||||
let smlib = solidcraft_smlib::Brep::from_record(&made.brep, true).map_err(|e| KernelError::Failed(format!("sew: SMLib: {e}")))?;
|
||||
let smlib = solidcraft_smlib::Brep::from_record(&made.brep, true).map_err(|e| KernelError::smlib("sew: SMLib", e))?;
|
||||
let (body, order) = crate::native::body_from_smlib_ordered(&smlib)?;
|
||||
// The face of `faces` each face of the body was built from: record face `c` is SMLib's
|
||||
// face `c`, which the order places in the body.
|
||||
|
||||
@@ -16,8 +16,8 @@ mod smlib {
|
||||
use crate::validation::{Checked, Step};
|
||||
use crate::{KernelError, Result, record};
|
||||
|
||||
fn failed(what: &str, e: impl std::fmt::Display) -> KernelError {
|
||||
KernelError::Failed(format!("SMLib boolean: {what}: {e}"))
|
||||
fn failed(what: &str, e: impl crate::Cause) -> KernelError {
|
||||
KernelError::smlib(format!("SMLib boolean: {what}"), e)
|
||||
}
|
||||
|
||||
/// `a` and `b` combined by `op` in SMLib, with the history SMLib's tags give (none where a
|
||||
|
||||
@@ -93,8 +93,8 @@ mod imp {
|
||||
use crate::provenance::{closest_on_tri, face_tris};
|
||||
use crate::record;
|
||||
|
||||
fn failed(what: &str, e: impl std::fmt::Display) -> KernelError {
|
||||
KernelError::Failed(format!("SMLib {what}: {e}"))
|
||||
fn failed(what: &str, e: impl crate::Cause) -> KernelError {
|
||||
KernelError::smlib(format!("SMLib {what}"), e)
|
||||
}
|
||||
|
||||
fn corner(c: crate::DraftCorner) -> DraftCorner {
|
||||
|
||||
@@ -49,8 +49,8 @@ fn invalid(what: impl Into<String>) -> KernelError {
|
||||
KernelError::Invalid(what.into())
|
||||
}
|
||||
|
||||
fn failed(what: &str, e: impl std::fmt::Display) -> KernelError {
|
||||
KernelError::Failed(format!("SMLib {what}: {e}"))
|
||||
fn failed(what: &str, e: impl crate::Cause) -> KernelError {
|
||||
KernelError::smlib(format!("SMLib {what}"), e)
|
||||
}
|
||||
|
||||
fn a3(v: Vec3) -> [f64; 3] {
|
||||
|
||||
@@ -387,8 +387,8 @@ fn back_to_bounds(body: &Body, faces: &[usize]) -> Result<Body> {
|
||||
Ok(back)
|
||||
}
|
||||
|
||||
fn failed(what: &str, e: impl std::fmt::Display) -> KernelError {
|
||||
KernelError::Failed(format!("SMLib extend: {what}: {e}"))
|
||||
fn failed(what: &str, e: impl crate::Cause) -> KernelError {
|
||||
KernelError::smlib(format!("SMLib extend: {what}"), e)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -102,7 +102,7 @@ pub fn trim_keep(body: &Body, tool: &SplitTool, keep: TrimKeep) -> Result<Body>
|
||||
fn face_sides(body: &Body, side: &ToolSide) -> Result<Vec<f64>> {
|
||||
let (sm, order) = crate::native::smlib_body_ordered(body)?;
|
||||
let order = order.ok_or_else(|| KernelError::Failed("trim: SMLib numbers the pieces' faces differently from their record".into()))?;
|
||||
let points = sm.face_points().map_err(|e| KernelError::Failed(format!("SMLib trim: a point inside each piece: {e}")))?;
|
||||
let points = sm.face_points().map_err(|e| KernelError::smlib("SMLib trim: a point inside each piece", e))?;
|
||||
let mut sides = vec![f64::NAN; points.len()];
|
||||
for (i, p) in points.iter().enumerate() {
|
||||
let face = if order.faces.is_empty() { i } else { order.faces.get(i).copied().unwrap_or(usize::MAX) };
|
||||
|
||||
@@ -102,8 +102,8 @@ pub fn untrim(body: &Body, face: usize, holes: UntrimHoles, outer: bool, in_plac
|
||||
Ok(out.with_checked_history(history, &crate::record::counts(&[body])))
|
||||
}
|
||||
|
||||
fn failed(what: &str, e: impl std::fmt::Display) -> KernelError {
|
||||
KernelError::Failed(format!("SMLib untrim: {what}: {e}"))
|
||||
fn failed(what: &str, e: impl crate::Cause) -> KernelError {
|
||||
KernelError::smlib(format!("SMLib untrim: {what}"), e)
|
||||
}
|
||||
|
||||
/// A point inside face `face` of `body`: the centre of its largest mesh triangle.
|
||||
|
||||
@@ -1236,7 +1236,7 @@ mod imp {
|
||||
use super::*;
|
||||
|
||||
fn sm(what: &'static str) -> impl Fn(solidcraft_smlib::Error) -> KernelError {
|
||||
move |e| failed(format!("SMLib {what}: {e}"))
|
||||
move |e| KernelError::smlib(format!("SMLib {what}"), e)
|
||||
}
|
||||
|
||||
pub(super) fn refuse(what: &str) -> KernelError {
|
||||
|
||||
Reference in New Issue
Block a user