| // Copyright 2026 The Fuchsia Authors | |
| // | |
| // Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0 | |
| // <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT | |
| // license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option. | |
| // This file may not be copied, modified, or distributed except according to | |
| // those terms. | |
| //! Single-Side-Effect Normal Form (SSENF) sequencing gate. | |
| //! | |
| //! Clang's AST fixes no evaluation order for unsequenced operands (C11 6.5p2, | |
| //! 6.5.2.2p10), but `Trusted.CppAst` is a linear three-address IR. Lowering | |
| //! an expression to a single order is exact only if every order the C | |
| //! abstract machine permits yields the same observable result. This module | |
| //! proves that syntactically, failing closed otherwise: | |
| //! | |
| //! * Operands of every operator except `&&`, `||`, `,`, and `?:` are | |
| //! unsequenced siblings, as are the left (address) and right (value) | |
| //! operands of `=` / `op=`, the base and index of `[]`, and all call | |
| //! arguments (indeterminately sequenced when they contain calls). | |
| //! * Two siblings conflict if one writes a local the other reads or writes, | |
| //! or if one performs a memory write or call while the other performs a | |
| //! memory read, memory write, or call. Locals whose address is taken | |
| //! anywhere in the function are also treated as memory. | |
| //! * The store performed by `=`, `op=`, `++`, or `--` is unsequenced with the | |
| //! side effects (but not the value computations) of its operands, so the | |
| //! stored location must not also be written by an operand (`x = x++`, | |
| //! `x = (x = 1) + 1`). | |
| //! | |
| //! This is deliberately stricter than C11 (for example it rejects | |
| //! `f() + g()`, whose behavior is merely unspecified, and `x = f(x++)`), so | |
| //! acceptance implies every permitted order agrees on every state. | |
| use std::collections::{BTreeSet, HashMap, HashSet}; | |
| use crate::{ | |
| error::{LowerError, Result}, | |
| raw::{BinaryOp, CastKind, RawExpr, RawExprKind, RawStmt, RawType, UnaryOp}, | |
| }; | |
| mod escape_and_writes; | |
| mod eval; | |
| pub use self::escape_and_writes::{ | |
| children, collect_escaped, collect_escaped_with_ref_fields, collect_glvalue_root_vars, | |
| collect_written_vars_expr, collect_written_vars_stmt, | |
| }; | |
| /// Effects of evaluating one subexpression. | |
| #[derive(Debug, Clone, Default)] | |
| pub struct Footprint { | |
| pub reads: BTreeSet<u32>, | |
| pub writes: BTreeSet<u32>, | |
| /// Number of non-call memory writes. | |
| pub mem_writes: usize, | |
| /// Number of calls (each may read and write arbitrary memory). | |
| pub calls: usize, | |
| pub mem_reads: bool, | |
| } | |
| impl Footprint { | |
| pub fn mem_writes_or_calls(&self) -> usize { | |
| self.mem_writes + self.calls | |
| } | |
| fn union(mut self, other: Footprint) -> Footprint { | |
| self.reads.extend(other.reads); | |
| self.writes.extend(other.writes); | |
| self.mem_writes += other.mem_writes; | |
| self.calls += other.calls; | |
| self.mem_reads |= other.mem_reads; | |
| self | |
| } | |
| } | |
| /// The storage location designated by an lvalue. | |
| #[derive(Debug, Clone, Copy, PartialEq, Eq)] | |
| enum Loc { | |
| Var(u32), | |
| Mem, | |
| } | |
| pub struct Ssenf<'a> { | |
| /// Locals whose address is taken somewhere in the function. | |
| escaped: &'a HashSet<u32>, | |
| /// Record fields of C++ reference type, keyed by `record_id -> field_idx`. | |
| ref_fields: Option<&'a HashMap<String, HashSet<u32>>>, | |
| } | |
| fn conflict(a: &Footprint, b: &Footprint) -> Option<String> { | |
| if let Some(v) = a.writes.intersection(&b.writes).next() { | |
| return Some(format!("both operands write variable #{v}")); | |
| } | |
| if let Some(v) = a.writes.intersection(&b.reads).next() { | |
| return Some(format!("one operand writes variable #{v} that the other reads")); | |
| } | |
| if let Some(v) = b.writes.intersection(&a.reads).next() { | |
| return Some(format!("one operand writes variable #{v} that the other reads")); | |
| } | |
| if a.mem_writes_or_calls() > 0 && b.mem_writes_or_calls() > 0 { | |
| return Some("both operands write memory or call functions".to_owned()); | |
| } | |
| if (a.mem_writes_or_calls() > 0 && b.mem_reads) || (b.mem_writes_or_calls() > 0 && a.mem_reads) | |
| { | |
| return Some("one operand writes memory or calls while the other reads memory".to_owned()); | |
| } | |
| None | |
| } | |
| fn reject<T>(what: &str, why: String) -> Result<T> { | |
| Err(LowerError::UnsequencedSideEffects { detail: format!("{what}: {why}") }) | |
| } | |
| fn check_siblings(what: &str, fps: &[Footprint]) -> Result<()> { | |
| for i in 0..fps.len() { | |
| for j in (i + 1)..fps.len() { | |
| if let Some(why) = conflict(&fps[i], &fps[j]) { | |
| return reject(what, why); | |
| } | |
| } | |
| } | |
| Ok(()) | |
| } | |
| fn strip_parens(e: &RawExpr) -> &RawExpr { | |
| match &e.kind { | |
| RawExprKind::Paren { sub } => strip_parens(sub), | |
| _ => e, | |
| } | |
| } |