Skip to content

Truly independent web browser

LibJS/JIT: Forbid containers of the heap in the compiler with a lint

Andreas Klingc24eccc2f4a8
diff --git a/Libraries/LibJS/JIT/Rust/build.rs b/Libraries/LibJS/JIT/Rust/build.rs
index 4381f2e900a..fec4f0284ec 100644
--- a/Libraries/LibJS/JIT/Rust/build.rs
+++ b/Libraries/LibJS/JIT/Rust/build.rs
@@ -11,6 +11,9 @@
//! the `Instruction` enum and the field readers are shared with
//! `Libraries/LibJS/Rust/build.rs` through `flapc::rust_bytecode`.
+// NB: The rule against containers of the heap (see clippy.toml) is for the compiler. This script runs at build time.
+#![allow(clippy::disallowed_types, clippy::disallowed_macros)]
+
use flapc::metadata::Field;
use flapc::metadata::InstructionDefinition;
use flapc::metadata::ParameterMode;
diff --git a/Libraries/LibJS/JIT/Rust/clippy.toml b/Libraries/LibJS/JIT/Rust/clippy.toml
new file mode 100644
index 00000000000..112251f6b15
--- /dev/null
+++ b/Libraries/LibJS/JIT/Rust/clippy.toml
@@ -0,0 +1,79 @@
+# NB: Clippy reads only the nearest clippy.toml, so this file repeats the list of /clippy.toml. Keep the two in sync.
+disallowed-methods = [
+ { path = "std::slice::from_raw_parts", reason = "a null pointer is undefined behavior even for an empty slice, and an empty AK container lends one: use ak::slice_from_raw_parts" },
+ { path = "std::slice::from_raw_parts_mut", reason = "a null pointer is undefined behavior even for an empty slice, and an empty AK container lends one: use ak::slice_from_raw_parts_mut" },
+ { path = "f32::acos", reason = "the C library of each platform computes this differently: use ak::math::acos" },
+ { path = "f32::asin", reason = "the C library of each platform computes this differently: use ak::math::asin" },
+ { path = "f32::atan", reason = "the C library of each platform computes this differently: use ak::math::atan" },
+ { path = "f32::atan2", reason = "the C library of each platform computes this differently: use ak::math::atan2" },
+ { path = "f32::cbrt", reason = "the C library of each platform computes this differently: use ak::math::cbrt" },
+ { path = "f32::cos", reason = "the C library of each platform computes this differently: use ak::math::cos" },
+ { path = "f32::cosh", reason = "the C library of each platform computes this differently: use ak::math::cosh" },
+ { path = "f32::exp", reason = "the C library of each platform computes this differently: use ak::math::exp" },
+ { path = "f32::exp2", reason = "the C library of each platform computes this differently: use ak::math::exp2" },
+ { path = "f32::exp_m1", reason = "the C library of each platform computes this differently: use ak::math::expm1" },
+ { path = "f32::hypot", reason = "the C library of each platform computes this differently: use ak::math::hypot" },
+ { path = "f32::ln", reason = "the C library of each platform computes this differently: use ak::math::log" },
+ { path = "f32::ln_1p", reason = "the C library of each platform computes this differently: use ak::math::log1p" },
+ { path = "f32::log", reason = "the C library of each platform computes this differently: use ak::math::log(x) / log(base)" },
+ { path = "f32::log10", reason = "the C library of each platform computes this differently: use ak::math::log10" },
+ { path = "f32::log2", reason = "the C library of each platform computes this differently: use ak::math::log2" },
+ { path = "f32::powf", reason = "the C library of each platform computes this differently: use ak::math::pow" },
+ { path = "f32::sin", reason = "the C library of each platform computes this differently: use ak::math::sin" },
+ { path = "f32::sin_cos", reason = "the C library of each platform computes this differently: use ak::math::sincos" },
+ { path = "f32::sinh", reason = "the C library of each platform computes this differently: use ak::math::sinh" },
+ { path = "f32::tan", reason = "the C library of each platform computes this differently: use ak::math::tan" },
+ { path = "f32::tanh", reason = "the C library of each platform computes this differently: use ak::math::tanh" },
+ { path = "f32::asinh", reason = "the C library of each platform computes this differently: use ak::math::asinh" },
+ { path = "f32::acosh", reason = "the C library of each platform computes this differently: use ak::math::acosh" },
+ { path = "f32::atanh", reason = "the C library of each platform computes this differently: use ak::math::atanh" },
+ { path = "f64::acos", reason = "the C library of each platform computes this differently: use ak::math::acos" },
+ { path = "f64::asin", reason = "the C library of each platform computes this differently: use ak::math::asin" },
+ { path = "f64::atan", reason = "the C library of each platform computes this differently: use ak::math::atan" },
+ { path = "f64::atan2", reason = "the C library of each platform computes this differently: use ak::math::atan2" },
+ { path = "f64::cbrt", reason = "the C library of each platform computes this differently: use ak::math::cbrt" },
+ { path = "f64::cos", reason = "the C library of each platform computes this differently: use ak::math::cos" },
+ { path = "f64::cosh", reason = "the C library of each platform computes this differently: use ak::math::cosh" },
+ { path = "f64::exp", reason = "the C library of each platform computes this differently: use ak::math::exp" },
+ { path = "f64::exp2", reason = "the C library of each platform computes this differently: use ak::math::exp2" },
+ { path = "f64::exp_m1", reason = "the C library of each platform computes this differently: use ak::math::expm1" },
+ { path = "f64::hypot", reason = "the C library of each platform computes this differently: use ak::math::hypot" },
+ { path = "f64::ln", reason = "the C library of each platform computes this differently: use ak::math::log" },
+ { path = "f64::ln_1p", reason = "the C library of each platform computes this differently: use ak::math::log1p" },
+ { path = "f64::log", reason = "the C library of each platform computes this differently: use ak::math::log(x) / log(base)" },
+ { path = "f64::log10", reason = "the C library of each platform computes this differently: use ak::math::log10" },
+ { path = "f64::log2", reason = "the C library of each platform computes this differently: use ak::math::log2" },
+ { path = "f64::powf", reason = "the C library of each platform computes this differently: use ak::math::pow" },
+ { path = "f64::sin", reason = "the C library of each platform computes this differently: use ak::math::sin" },
+ { path = "f64::sin_cos", reason = "the C library of each platform computes this differently: use ak::math::sincos" },
+ { path = "f64::sinh", reason = "the C library of each platform computes this differently: use ak::math::sinh" },
+ { path = "f64::tan", reason = "the C library of each platform computes this differently: use ak::math::tan" },
+ { path = "f64::tanh", reason = "the C library of each platform computes this differently: use ak::math::tanh" },
+ { path = "f64::asinh", reason = "the C library of each platform computes this differently: use ak::math::asinh" },
+ { path = "f64::acosh", reason = "the C library of each platform computes this differently: use ak::math::acosh" },
+ { path = "f64::atanh", reason = "the C library of each platform computes this differently: use ak::math::atanh" },
+]
+
+# The compiler keeps its data in zones (see ak::Zone), so that all the memory of a compile can go back to the system.
+# A container of the heap would leave blocks behind in the heap of the process.
+disallowed-types = [
+ { path = "std::vec::Vec", reason = "the compiler makes no block in the heap: use ak::ZoneVec, or a slice in a zone" },
+ { path = "std::collections::VecDeque", reason = "the compiler makes no block in the heap: use ak::ZoneVec" },
+ { path = "std::collections::BinaryHeap", reason = "the compiler makes no block in the heap: use ak::ZoneVec" },
+ { path = "std::collections::LinkedList", reason = "the compiler makes no block in the heap: use ak::ZoneVec" },
+ { path = "std::collections::HashMap", reason = "the compiler makes no block in the heap: use crate::zone_map::ZoneMap" },
+ { path = "std::collections::HashSet", reason = "the compiler makes no block in the heap: use crate::zone_map::ZoneSet" },
+ { path = "std::collections::BTreeMap", reason = "the compiler makes no block in the heap: use crate::zone_map::ZoneMap, or a sorted ak::ZoneVec" },
+ { path = "std::collections::BTreeSet", reason = "the compiler makes no block in the heap: use crate::zone_map::ZoneSet, or a sorted ak::ZoneVec" },
+ { path = "std::boxed::Box", reason = "the compiler makes no block in the heap: use Zone::alloc" },
+ { path = "std::rc::Rc", reason = "the compiler makes no block in the heap: share a reference into a zone" },
+ { path = "std::sync::Arc", reason = "the compiler makes no block in the heap: share a reference into a zone" },
+ { path = "libjs_jit::bitset::BitSet", reason = "the compiler makes no block in the heap: use crate::bitset::ZoneBitSet" },
+ { path = "libjs_jit::sparse_bitset::SparseBitSet", reason = "the compiler makes no block in the heap: use crate::bitset::ZoneBitSet" },
+ { path = "libjs_jit::inline_vec::InlineVec", reason = "the compiler makes no block in the heap: use ak::ZoneVec" },
+ { path = "libjs_jit::fast_hash::HashMap", reason = "the compiler makes no block in the heap: use crate::zone_map::ZoneMap" },
+ { path = "libjs_jit::fast_hash::HashSet", reason = "the compiler makes no block in the heap: use crate::zone_map::ZoneSet" },
+]
+disallowed-macros = [
+ { path = "std::vec", reason = "the compiler makes no block in the heap: use a slice, or ak::ZoneVec" },
+]
diff --git a/Libraries/LibJS/JIT/Rust/src/lib.rs b/Libraries/LibJS/JIT/Rust/src/lib.rs
index bfbf8ea9968..06b88d5623d 100644
--- a/Libraries/LibJS/JIT/Rust/src/lib.rs
+++ b/Libraries/LibJS/JIT/Rust/src/lib.rs
@@ -11,23 +11,42 @@
//! thread). It calls nothing in the runtime and has no access to the GC heap, so
//! it can run on a worker thread.
+// NB: The tests keep their own data in the heap.
+#![cfg_attr(test, allow(clippy::disallowed_types, clippy::disallowed_macros))]
+
+// NB: A module with `allow(clippy::disallowed_types)` still uses containers of the heap (see clippy.toml). It loses
+// the `allow` when its data is in zones.
+#[allow(clippy::disallowed_types, clippy::disallowed_macros)]
pub mod artifact;
+#[allow(clippy::disallowed_types, clippy::disallowed_macros)]
pub mod asm;
+#[allow(clippy::disallowed_types, clippy::disallowed_macros)]
pub mod bitset;
pub mod builder;
pub mod bytecode;
+#[allow(clippy::disallowed_types, clippy::disallowed_macros)]
pub mod code;
+#[allow(clippy::disallowed_types, clippy::disallowed_macros)]
pub mod codegen;
+#[allow(clippy::disallowed_types, clippy::disallowed_macros)]
pub mod compile_pool;
+#[allow(clippy::disallowed_types, clippy::disallowed_macros)]
pub mod coverage;
+#[allow(clippy::disallowed_types, clippy::disallowed_macros)]
pub mod fast_hash;
+#[allow(clippy::disallowed_types, clippy::disallowed_macros)]
pub mod inline_vec;
+#[allow(clippy::disallowed_types, clippy::disallowed_macros)]
pub mod ir;
+#[allow(clippy::disallowed_types, clippy::disallowed_macros)]
pub mod options;
pub mod passes;
+#[allow(clippy::disallowed_types, clippy::disallowed_macros)]
pub mod regalloc;
pub mod scratch;
+#[allow(clippy::disallowed_types, clippy::disallowed_macros)]
pub mod snapshot;
+#[allow(clippy::disallowed_types, clippy::disallowed_macros)]
pub mod sparse_bitset;
pub mod tables;
pub mod zone_map;
@@ -130,6 +149,8 @@ fn compile_into<M: asm::PortableMacroAssembler>(
}
/// The stages of `compile_into()`, which leave their data in `scratch`.
+// NB: The coverage keys of a compile are still a list in the heap.
+#[allow(clippy::disallowed_types)]
fn run_stages<M: asm::PortableMacroAssembler>(
snapshot: &Snapshot,
has_run: &dyn Fn(u32) -> bool,
diff --git a/Libraries/LibJS/JIT/Rust/src/passes/mod.rs b/Libraries/LibJS/JIT/Rust/src/passes/mod.rs
index 54afe1bdf3a..879c6fe29d5 100644
--- a/Libraries/LibJS/JIT/Rust/src/passes/mod.rs
+++ b/Libraries/LibJS/JIT/Rust/src/passes/mod.rs
@@ -13,26 +13,48 @@
//! tests check the graph's invariants (see `verify`) after building and after
//! every pass.
+// NB: A pass with `allow(clippy::disallowed_types)` still uses containers of the heap (see clippy.toml in the
+// directory of the crate). It loses the `allow` when its data is in zones.
+#[allow(clippy::disallowed_types, clippy::disallowed_macros)]
mod bounds_check_elimination;
+#[allow(clippy::disallowed_types, clippy::disallowed_macros)]
mod branch_fusion;
+#[allow(clippy::disallowed_types, clippy::disallowed_macros)]
mod branch_splitting;
+#[allow(clippy::disallowed_types, clippy::disallowed_macros)]
mod check_elimination;
+#[allow(clippy::disallowed_types, clippy::disallowed_macros)]
mod dce;
+#[allow(clippy::disallowed_types, clippy::disallowed_macros)]
pub mod dominators;
+#[allow(clippy::disallowed_types, clippy::disallowed_macros)]
pub mod edit;
mod enumerated_keys;
+#[allow(clippy::disallowed_types, clippy::disallowed_macros)]
mod escape_analysis;
+#[allow(clippy::disallowed_types, clippy::disallowed_macros)]
mod exit_unboxing;
+#[allow(clippy::disallowed_types, clippy::disallowed_macros)]
mod facts;
+#[allow(clippy::disallowed_types, clippy::disallowed_macros)]
mod frame_initialization;
+#[allow(clippy::disallowed_types, clippy::disallowed_macros)]
mod gvn;
+#[allow(clippy::disallowed_types, clippy::disallowed_macros)]
mod interrupt_check_elimination;
+#[allow(clippy::disallowed_types, clippy::disallowed_macros)]
mod key_strings;
+#[allow(clippy::disallowed_types, clippy::disallowed_macros)]
mod licm;
+#[allow(clippy::disallowed_types, clippy::disallowed_macros)]
mod loop_rotation;
+#[allow(clippy::disallowed_types, clippy::disallowed_macros)]
mod representation;
+#[allow(clippy::disallowed_types, clippy::disallowed_macros)]
mod simplify;
+#[allow(clippy::disallowed_types, clippy::disallowed_macros)]
mod truncation;
+#[allow(clippy::disallowed_types, clippy::disallowed_macros)]
pub mod verify;
#[cfg(test)]