use super::Backend;
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum OperationKind {
Encode,
StrictDecode,
SecretDecode,
}
impl OperationKind {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Encode => "encode",
Self::StrictDecode => "strict-decode",
Self::SecretDecode => "secret-decode",
}
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct BackendIdentifier(&'static str);
impl BackendIdentifier {
pub(crate) const SCALAR_CT_ORIENTED: Self = Self("scalar-constant-time-oriented");
pub(crate) const fn ordinary(backend: Backend) -> Self {
Self(backend.as_str())
}
#[must_use]
pub const fn as_str(self) -> &'static str {
self.0
}
}
impl core::fmt::Display for BackendIdentifier {
fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
formatter.write_str(self.0)
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum OperationSecurityPosture {
OrdinaryScalar,
OrdinaryAccelerated,
ScalarConstantTimeOriented,
}
impl OperationSecurityPosture {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::OrdinaryScalar => "ordinary-scalar",
Self::OrdinaryAccelerated => "ordinary-accelerated",
Self::ScalarConstantTimeOriented => "scalar-constant-time-oriented",
}
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum BackendHealthPosture {
ScalarFixed,
NeverRun,
Testing,
Healthy,
Quarantined,
SynchronizationUnavailable,
SecretPolicyFixed,
}
impl BackendHealthPosture {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::ScalarFixed => "scalar-fixed",
Self::NeverRun => "never-run",
Self::Testing => "testing",
Self::Healthy => "healthy",
Self::Quarantined => "quarantined",
Self::SynchronizationUnavailable => "synchronization-unavailable",
Self::SecretPolicyFixed => "secret-policy-fixed",
}
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum WasmArtifactPosture {
NotWasm,
ScalarArtifact,
Simd128Artifact,
}
impl WasmArtifactPosture {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::NotWasm => "not-wasm",
Self::ScalarArtifact => "wasm-scalar-artifact",
Self::Simd128Artifact => "wasm-simd128-artifact",
}
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum WasmRuntimePosture {
NotWasm,
HostRuntimeUnidentified,
}
impl WasmRuntimePosture {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::NotWasm => "not-wasm",
Self::HostRuntimeUnidentified => "wasm-host-runtime-unidentified",
}
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct OperationBackendReport {
pub operation: OperationKind,
pub backend: BackendIdentifier,
pub security_posture: OperationSecurityPosture,
pub health_posture: BackendHealthPosture,
pub health_generation: usize,
pub backend_fault: Option<crate::BackendFault>,
}
impl OperationBackendReport {
pub(crate) fn ordinary(operation: OperationKind, backend: Backend, candidate: Backend) -> Self {
let security_posture = if matches!(backend, Backend::Scalar) {
OperationSecurityPosture::OrdinaryScalar
} else {
OperationSecurityPosture::OrdinaryAccelerated
};
let health = crate::v2::backend_health::snapshot(operation, candidate);
let health_posture = if candidate == Backend::Scalar {
BackendHealthPosture::ScalarFixed
} else {
match health.state {
crate::BackendHealthState::NeverRun => {
if cfg!(target_has_atomic = "ptr") {
BackendHealthPosture::NeverRun
} else {
BackendHealthPosture::SynchronizationUnavailable
}
}
crate::BackendHealthState::Testing => BackendHealthPosture::Testing,
crate::BackendHealthState::Healthy => BackendHealthPosture::Healthy,
crate::BackendHealthState::Quarantined => BackendHealthPosture::Quarantined,
}
};
Self {
operation,
backend: BackendIdentifier::ordinary(backend),
security_posture,
health_posture,
health_generation: health.generation,
backend_fault: health.fault,
}
}
pub(crate) const fn secret_decode() -> Self {
Self {
operation: OperationKind::SecretDecode,
backend: BackendIdentifier::SCALAR_CT_ORIENTED,
security_posture: OperationSecurityPosture::ScalarConstantTimeOriented,
health_posture: BackendHealthPosture::SecretPolicyFixed,
health_generation: 1,
backend_fault: None,
}
}
#[must_use]
pub const fn snapshot(self) -> OperationBackendSnapshot {
OperationBackendSnapshot {
operation: self.operation.as_str(),
backend: self.backend.as_str(),
security_posture: self.security_posture.as_str(),
health_posture: self.health_posture.as_str(),
health_generation: self.health_generation,
backend_fault: match self.backend_fault {
Some(fault) => Some(fault.as_str()),
None => None,
},
}
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct OperationBackendSnapshot {
pub operation: &'static str,
pub backend: &'static str,
pub security_posture: &'static str,
pub health_posture: &'static str,
pub health_generation: usize,
pub backend_fault: Option<&'static str>,
}
pub(crate) const fn wasm_artifact_posture() -> WasmArtifactPosture {
#[cfg(all(feature = "simd", target_arch = "wasm32"))]
if crate::simd::wasm_simd128_artifact_selected() {
return WasmArtifactPosture::Simd128Artifact;
}
if cfg!(target_arch = "wasm32") {
WasmArtifactPosture::ScalarArtifact
} else {
WasmArtifactPosture::NotWasm
}
}
pub(crate) const fn wasm_runtime_posture() -> WasmRuntimePosture {
if cfg!(target_arch = "wasm32") {
WasmRuntimePosture::HostRuntimeUnidentified
} else {
WasmRuntimePosture::NotWasm
}
}