From 317445b5f8dbd455f7372a03ad12f7c9018b152b Mon Sep 17 00:00:00 2001 From: Neos Date: Sat, 5 Sep 2026 11:59:28 -0700 Subject: [PATCH 1/4] fix(shm): two more hand-restated copies of geometry the layout module owns Fifth batch from the duplication audit. 1. read_slot_inner restated shm_layout's arithmetic in two of its three arms. The colo arm calls colo_required_region_len and colo_payload_offset. Three lines below it, the split arm wrote out TOPIC_HEADER_SIZE + capacity * 8 + capacity * type_size by hand, and the serde arm did the same with slot_size - both with bare `*` on two u32s read out of a header another process wrote. A release profile has no overflow-checks, so on a 32-bit controller the product wraps and a truncated file satisfies a check for a ring that does not fit. The split arm then took its slot with mmap[a..b], which panics, where the colo arm three lines up uses .get(..)?. Both now use required_region_len_checked and data_slot_offset. The helpers compute identical values for every shape checked, so this is a substitution, not a behaviour change - what changes is that the overflow and the bounds failure now return None instead of wrapping or panicking. 2. The .meta sidecar mapping was reimplemented in horus_manager. horus_sys builds it with sanitize_namespace, which maps every byte outside [A-Za-z0-9_] to _. horus_manager's stale-topic reaper built it as name.replace('.', "_"), which agrees only when the name's sole special character is a dot. Topic names legally carry -, / and . For `robot-arm/imu`, horus_sys writes robot_arm_imu.meta and the reaper looked for robot-arm/imu.meta - a path that cannot exist, since the / names a directory. Those sidecars were never reaped and accumulated for the life of the machine. Added horus_sys::shm::topic_meta_path as the single definition, alongside topic_shm_path; both horus_sys sites and the manager use it. Purely additive to the public surface. Verified: fmt, clippy --workspace --all-targets -D warnings, horus_sys 224 tests, horus_manager 3497 tests, horus_core communication 664. --- horus_core/src/communication/topic/header.rs | 27 ++++++--- horus_manager/src/discovery/topics.rs | 8 ++- horus_sys/src/shm/mod.rs | 58 +++++++++++++++++++- 3 files changed, 82 insertions(+), 11 deletions(-) diff --git a/horus_core/src/communication/topic/header.rs b/horus_core/src/communication/topic/header.rs index 4a27c2af..1ed89d73 100644 --- a/horus_core/src/communication/topic/header.rs +++ b/horus_core/src/communication/topic/header.rs @@ -1627,28 +1627,37 @@ fn read_slot_inner( mmap.get(slot_start..end)?.to_vec() } else { // Split layout: [HEADER (640)] [SEQ_ARRAY (cap * 8)] [DATA (cap * type_size)] - let seq_array_size = capacity * std::mem::size_of::(); - let data_region_start = TOPIC_HEADER_SIZE + seq_array_size; - let required = data_region_start + capacity * type_size; + // + // Through `shm_layout`, like the colo arm three lines above. This + // used to restate the arithmetic — `TOPIC_HEADER_SIZE + capacity * 8 + // + capacity * type_size` — with `*` rather than the checked helper, + // on two u32s read straight out of a header another process wrote. + // A release profile has no overflow-checks, so on a 32-bit target + // the product wraps and a truncated file satisfies a check for a + // ring that does not fit. The slot was then taken with `mmap[a..b]`, + // which panics, where the colo arm uses `.get(..)?`. + let required = super::shm_layout::required_region_len_checked(capacity, type_size)?; if mmap.len() < required { return None; } - let slot_start = data_region_start + last_written * type_size; - mmap[slot_start..slot_start + type_size].to_vec() + let slot_start = super::shm_layout::data_slot_offset(capacity, last_written, type_size); + let end = slot_start.checked_add(type_size)?; + mmap.get(slot_start..end)?.to_vec() } } else { if slot_size < 16 || capacity == 0 { return None; } // Serde layout: [header (640B)] [seq_array (cap * 8B)] [data slots (cap * slot_size)] - let seq_array_size = capacity * std::mem::size_of::(); - let data_region_start = TOPIC_HEADER_SIZE + seq_array_size; - let required = data_region_start + capacity * slot_size; + // + // Same helpers as the POD arms, for the same reason: the multiplication + // is over header-supplied values and must not wrap silently. + let required = super::shm_layout::required_region_len_checked(capacity, slot_size)?; if mmap.len() < required { return None; } // Slot layout: [8 bytes padding][8 bytes data-len (u64 LE)][data…] - let slot_start = data_region_start + last_written * slot_size; + let slot_start = super::shm_layout::data_slot_offset(capacity, last_written, slot_size); let len_offset = slot_start + 8; let data_offset = slot_start + 16; // SAFETY: len_offset is within mmap bounds (validated by required size check above); diff --git a/horus_manager/src/discovery/topics.rs b/horus_manager/src/discovery/topics.rs index 3b3cf421..5c18ae07 100644 --- a/horus_manager/src/discovery/topics.rs +++ b/horus_manager/src/discovery/topics.rs @@ -15,7 +15,13 @@ pub(super) fn discover_shared_memory_uncached() -> HorusResult 0 { let topic_path = topics_dir.join(&t.name); - let meta_path = topics_dir.join(format!("{}.meta", t.name.replace('.', "_"))); + // horus_sys owns this mapping. This line used to be + // `t.name.replace('.', "_")`, which agrees with + // `sanitize_namespace` only for names whose sole special character + // is a dot — a topic like `robot-arm/imu` produced + // `robot-arm/imu.meta`, a path that cannot exist, so its sidecar + // was never reaped and accumulated for the life of the machine. + let meta_path = horus_sys::shm::topic_meta_path(&t.name); let _ = std::fs::remove_file(&topic_path); let _ = std::fs::remove_file(&meta_path); } diff --git a/horus_sys/src/shm/mod.rs b/horus_sys/src/shm/mod.rs index d567b974..c6191899 100644 --- a/horus_sys/src/shm/mod.rs +++ b/horus_sys/src/shm/mod.rs @@ -1081,11 +1081,25 @@ pub fn write_topic_meta(name: &str, size: usize) -> anyhow::Result<()> { Ok(()) } +/// The `.meta` sidecar path for a topic. +/// +/// The single definition of this mapping, for the same reason `topic_shm_path` +/// is the single definition of the region mapping. `sanitize_namespace` maps +/// every byte outside `[A-Za-z0-9_]` to `_`, and topic names legally contain +/// `-`, `/` and `.` — so a name like `robot-arm/imu` lands at +/// `robot_arm_imu.meta`. horus_manager's stale-topic reaper used to compute +/// this itself as `name.replace('.', "_")`, which agrees only for names whose +/// sole special character is a dot: for anything with a `-` or `/` it produced +/// a path that cannot exist, and those sidecars were never reaped. +pub fn topic_meta_path(name: &str) -> PathBuf { + shm_topics_dir().join(format!("{}.meta", sanitize_namespace(name))) +} + /// Remove a topic metadata file. Best-effort — errors are silently ignored. /// /// Called by ShmRegion::drop when the owner releases the region. pub fn remove_topic_meta(name: &str) { - let path = shm_topics_dir().join(format!("{}.meta", sanitize_namespace(name))); + let path = topic_meta_path(name); if let Err(e) = std::fs::remove_file(&path) { if e.kind() != std::io::ErrorKind::NotFound { log::debug!("Failed to remove topic meta {}: {}", path.display(), e); @@ -3263,3 +3277,45 @@ mod untrusted_meta_tests { let _ = std::fs::remove_file(&good); } } + +#[cfg(test)] +mod meta_path_tests { + use super::*; + + /// The sidecar mapping must handle every character a topic name may carry. + /// + /// horus_manager's reaper computed this as `name.replace('.', "_")`, which + /// agrees with `sanitize_namespace` only when the name's sole special + /// character is a dot. Topic names legally contain `-`, `/` and `.` + /// (horus_core topic name validation), and `horus_sys/tests/parity_shm.rs` + /// exercises `sensor.imu/v1`. For anything with a `-` or `/` the two + /// disagreed, the reaper looked for a path that cannot exist, and the + /// sidecar was never removed. + #[test] + fn the_sidecar_mapping_covers_every_legal_topic_character() { + for name in ["cmd_vel", "sensor.imu", "sensor.imu/v1", "robot-arm/imu"] { + let p = topic_meta_path(name); + let file = p.file_name().unwrap().to_str().unwrap(); + assert!( + file.ends_with(".meta"), + "{name}: expected a .meta sidecar, got {file}" + ); + let stem = file.strip_suffix(".meta").unwrap(); + assert!( + stem.chars().all(|c| c.is_ascii_alphanumeric() || c == '_'), + "{name}: the sidecar stem must be sanitised to [A-Za-z0-9_], got {stem:?} - \ + a stem carrying `/` names a directory that does not exist, which is how \ + these files stopped being reaped" + ); + } + + // The specific disagreement: dot-only replacement is not enough. + let naive = "robot-arm/imu".replace('.', "_"); + let real = topic_meta_path("robot-arm/imu"); + assert_ne!( + real.file_name().unwrap().to_str().unwrap(), + format!("{naive}.meta"), + "if these ever agree the test has stopped discriminating" + ); + } +} From 86ba706b852286024779a25bc97bbaf464b692ae Mon Sep 17 00:00:00 2001 From: Neos Date: Sat, 5 Sep 2026 12:15:33 -0700 Subject: [PATCH 2/4] fix(cargo_gen): the workspace generator dropped the features it was asked for Sixth batch from the duplication audit. horus_dep_features turns `enable = ["net"]` in horus.toml - and HORUS_ENABLE, which `horus run --net` sets - into the `net` cargo feature on the horus dependency. It has exactly one caller, write_horus_path_deps, which had exactly one caller: the single-package generator. The workspace generator carried its own copy of that loop: "{} = {{ path = \"{}\" }}" against the shared writer's "{} = {{ path = \"{}\", features = [{}] }}" Same four dependency names, same output shape, one clause missing. So `enable = ["net"]` and `horus run --net` were honoured for a single-package project and dropped for every workspace project - the feature was parsed, resolved into a Vec, and thrown away one generator over, with no error. A user asking for LAN replication in a workspace got a build without it. Worth noting what that costs: the replication seam this feature gates is the same one #80 found broken in horus_net for four months. A silently feature-less build is exactly the condition under which nobody notices. The workspace generator now calls write_horus_path_deps. Members inherit through `horus.workspace = true`. The test asserts on the emitted TOML rather than on horus_dep_features alone, because the function was never the broken part - it returned the right Vec the whole time and had no second caller to hand it to. Verified: fmt, clippy -p horus_manager --all-targets -D warnings, cargo_gen 105 tests, horus_manager lib suite. --- horus_manager/src/cargo_gen.rs | 69 ++++++++++++++++++++++++++++------ 1 file changed, 57 insertions(+), 12 deletions(-) diff --git a/horus_manager/src/cargo_gen.rs b/horus_manager/src/cargo_gen.rs index 1e511032..d29503fd 100644 --- a/horus_manager/src/cargo_gen.rs +++ b/horus_manager/src/cargo_gen.rs @@ -1813,19 +1813,18 @@ pub fn generate_workspace( // Auto-inject horus core path deps. Required — see the note on the // single-package path above; an empty section yields unresolvable // imports in every workspace member. + // + // `write_horus_path_deps`, not a second copy of its loop. The copy that + // used to be here emitted `name = { path = "…" }` and dropped the + // `features = [...]` clause, so `horus_dep_features` — which turns + // `enable = ["net"]` in horus.toml, and `HORUS_ENABLE` from + // `horus run --net`, into the `net` cargo feature — reached + // single-package projects and no workspace project at all. A user + // asking for LAN replication in a workspace got a build without it and + // no error: the feature was parsed, resolved, and dropped on the floor + // one generator over. Members inherit through `horus.workspace = true`. { - for dep_name in &["horus", "horus_core", "horus_library", "horus_macros"] { - let dep_path = horus_source.join(dep_name); - if dep_path.exists() && dep_path.join("Cargo.toml").exists() { - writeln!( - root_cargo, - "{} = {{ path = \"{}\" }}", - dep_name, - toml_path(dep_path.display()) - ) - .unwrap(); - } - } + write_horus_path_deps(&mut root_cargo, &horus_source, root_manifest); // serde is implicit — see write_implicit_deps. Members opt in via // `serde = { workspace = true }` (added in generate_member_cargo). // @@ -5145,4 +5144,50 @@ mod tests { assert!(!dir.path().join(".cargo").exists()); assert!(!horus_dir.join(".cargo/config.toml").exists()); } + + /// A workspace project must get the same cargo features a single-package + /// project gets. + /// + /// `horus_dep_features` turns `enable = ["net"]` in horus.toml — and + /// `HORUS_ENABLE`, which `horus run --net` sets — into the `net` feature on + /// the `horus` dependency. It had exactly one caller, + /// `write_horus_path_deps`, which had exactly one caller: the + /// single-package generator. The workspace generator carried its own copy + /// of that loop with the `features = [...]` clause missing, so asking for + /// LAN replication in a workspace produced a build without it and no error. + /// + /// This asserts on the emitted TOML, which is the artifact that decides + /// what cargo compiles. + #[test] + fn the_workspace_generator_emits_the_features_the_manifest_asks_for() { + let mut manifest = HorusManifest::default(); + manifest.enable = vec!["net".to_string()]; + let features = horus_dep_features(&manifest); + assert!( + features.iter().any(|f| f == "net"), + "enable = [\"net\"] must resolve to the `net` cargo feature; got {features:?}" + ); + + // The shared writer must put them in the TOML for any caller. + let tmp = tempfile::TempDir::new().unwrap(); + let src = tmp.path(); + for c in ["horus", "horus_core", "horus_macros"] { + std::fs::create_dir_all(src.join(c)).unwrap(); + std::fs::write(src.join(c).join("Cargo.toml"), "[package]\n").unwrap(); + } + let mut out = String::new(); + write_horus_path_deps(&mut out, src, &manifest); + + let horus_line = out + .lines() + .find(|l| l.starts_with("horus = ")) + .expect("the horus dependency must be written"); + assert!( + horus_line.contains("features = [\"net\"]"), + "the `horus` dep must carry the requested features. The workspace \ + generator used to emit this line without them, which is how \ + `horus run --net` silently produced a build with no replication. \ + Got: {horus_line}" + ); + } } From 5d128be61b5e4fb08d7587e7fc15810933b2add3 Mon Sep 17 00:00:00 2001 From: Neos Date: Sat, 5 Sep 2026 13:29:32 -0700 Subject: [PATCH 3/4] fix(registry): the installer's crate list omitted the one crate everyone uses Seventh batch from the duplication audit. cargo_toml_has_horus_deps gates whether `horus install` injects the [patch] path overrides that point cargo at the local HORUS source tree. It named three crates: deps.contains_key("horus_core") || deps.contains_key("horus_library") || deps.contains_key("horus_macros") `horus` is not among them - the facade crate that `horus new` writes into every scaffold and that every documented example imports as `use horus::prelude::*`. So a published package whose Cargo.toml says `horus = "0.4.1"` was not recognised as depending on HORUS at all, and the overrides were never injected for it. The check meant to catch HORUS packages caught only the shapes nobody writes by hand. error_wrapper::FIRST_PARTY_CRATES is the canonical list - thirteen names, with a doc explaining precisely why they matter and what cargo says when they cannot be resolved. The installer carried a three-name copy, and its own doc comment restated the same three, so the code and its documentation agreed with each other and disagreed with reality. That is what makes this class hard to spot by reading. Now calls is_first_party_crate. Doc corrected to name the set rather than enumerate a stale subset of it. Ablation: with the three-name list restored, the new test fails on `horus = "0.4.1"`. It also asserts an unrelated package is still NOT matched, so the check cannot pass by becoming indiscriminate. Verified: fmt, clippy -p horus_manager --all-targets -D warnings, registry 187 tests, error_wrapper 86 tests. --- horus_manager/src/registry/helpers.rs | 65 ++++++++++++++++++++++++--- 1 file changed, 60 insertions(+), 5 deletions(-) diff --git a/horus_manager/src/registry/helpers.rs b/horus_manager/src/registry/helpers.rs index ce874acd..25a90747 100644 --- a/horus_manager/src/registry/helpers.rs +++ b/horus_manager/src/registry/helpers.rs @@ -1052,7 +1052,11 @@ pub(crate) fn extract_package_dependencies(dir: &Path) -> Result bool { let cargo_toml_path = package_dir.join(CARGO_TOML); let content = match fs::read_to_string(&cargo_toml_path) { @@ -1067,9 +1071,15 @@ fn cargo_toml_has_horus_deps(package_dir: &Path) -> bool { Some(d) => d, None => return false, }; - deps.contains_key("horus_core") - || deps.contains_key("horus_library") - || deps.contains_key("horus_macros") + // The shared list, not a third copy of it. This used to name exactly + // horus_core, horus_library and horus_macros - omitting `horus`, the + // facade crate every `horus new` scaffold writes and every documented + // example imports (`use horus::prelude::*`). So a published package + // depending on `horus` was not recognised as depending on HORUS at all, + // and the path overrides that point cargo at the local source tree were + // never injected for it. + deps.keys() + .any(|k| crate::error_wrapper::is_first_party_crate(k)) } /// The command HORUS prints when it cannot find its own source tree — the "I @@ -2156,7 +2166,7 @@ pub fn generate_signing_keypair() -> Result<()> { #[cfg(test)] mod system_package_name_tests { - use super::is_safe_system_package_name; + use super::{cargo_toml_has_horus_deps, is_safe_system_package_name}; #[test] fn accepts_real_package_names() { @@ -2224,4 +2234,49 @@ mod system_package_name_tests { assert!(!is_safe_system_package_name(&"a".repeat(129))); assert!(is_safe_system_package_name(&"a".repeat(128))); } + + /// A package depending on the `horus` facade must be recognised. + /// + /// `cargo_toml_has_horus_deps` gates whether `horus install` injects the + /// path overrides that point cargo at the local HORUS source tree. It used + /// to name horus_core, horus_library and horus_macros explicitly — leaving + /// out `horus`, which is what `horus new` writes and what every documented + /// example imports. So the most ordinary package shape there is went + /// unrecognised, and the check that was supposed to catch HORUS packages + /// caught only the ones nobody writes by hand. + #[test] + fn a_package_depending_on_the_facade_is_recognised() { + let tmp = tempfile::TempDir::new().unwrap(); + + for (name, dep) in [ + ("facade", "horus = \"0.4.1\""), + ("core", "horus_core = \"0.4.1\""), + ("macros", "horus_macros = \"0.4.1\""), + ] { + let dir = tmp.path().join(name); + std::fs::create_dir_all(&dir).unwrap(); + std::fs::write( + dir.join("Cargo.toml"), + format!("[package]\nname = \"p\"\n\n[dependencies]\n{dep}\n"), + ) + .unwrap(); + assert!( + cargo_toml_has_horus_deps(&dir), + "a package depending on `{dep}` must be recognised as a HORUS package" + ); + } + + // And a package that depends on none of ours still is not one. + let other = tmp.path().join("unrelated"); + std::fs::create_dir_all(&other).unwrap(); + std::fs::write( + other.join("Cargo.toml"), + "[package]\nname = \"p\"\n\n[dependencies]\nserde = \"1\"\n", + ) + .unwrap(); + assert!( + !cargo_toml_has_horus_deps(&other), + "the check must still discriminate, or it is not a check" + ); + } } From 7d8b3bd3a90b63f6853e37c3a3e814b8471c37c2 Mon Sep 17 00:00:00 2001 From: Neos Date: Sat, 5 Sep 2026 15:57:25 -0700 Subject: [PATCH 4/4] test(shm): the sidecar guard could not fail on the bug it was written for MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two assertions in the meta_path test did not discriminate, both raised in review on #200: * Checking only file_name()/stem is blind to the actual failure. A mapping that let `/` through yields /robot-arm/imu.meta, whose file_name is imu.meta and whose stem is imu — the existing assertions pass. What breaks the reaper is the sidecar not being a direct child of the topics directory, so assert the parent. * The dot-only regression guard compared file_name() against a string containing a path separator. A file name cannot contain one, so it held regardless of what topic_meta_path returned. Compare whole paths. Ablation: restoring the naive name.replace('.', "_") mapping now fails on both assertions independently (parent, then, with the loop neutered, the whole-path comparison). --- horus_sys/src/shm/mod.rs | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/horus_sys/src/shm/mod.rs b/horus_sys/src/shm/mod.rs index c6191899..54d8904c 100644 --- a/horus_sys/src/shm/mod.rs +++ b/horus_sys/src/shm/mod.rs @@ -3307,15 +3307,36 @@ mod meta_path_tests { a stem carrying `/` names a directory that does not exist, which is how \ these files stopped being reaped" ); + // `file_name()` alone cannot see the bug this test exists for. + // A mapping that let `/` through would produce + // `/robot-arm/imu.meta`, whose file_name is `imu.meta` and + // whose stem is `imu` - both assertions above pass. What actually + // breaks the reaper is the sidecar not being a direct child of the + // topics directory, so assert that. + assert_eq!( + p.parent(), + Some(shm_topics_dir().as_path()), + "{name}: the sidecar must sit directly in {}, not in a subdirectory of it; \ + got {}", + shm_topics_dir().display(), + p.display() + ); } // The specific disagreement: dot-only replacement is not enough. - let naive = "robot-arm/imu".replace('.', "_"); + // + // Compare whole paths. Comparing `file_name()` against `"robot-arm/imu.meta"` + // - which is what this used to do - can never fail: a file name cannot + // contain a path separator, so the assertion held no matter what + // `topic_meta_path` returned. + let naive = shm_topics_dir().join(format!("{}.meta", "robot-arm/imu".replace('.', "_"))); let real = topic_meta_path("robot-arm/imu"); assert_ne!( - real.file_name().unwrap().to_str().unwrap(), - format!("{naive}.meta"), - "if these ever agree the test has stopped discriminating" + real, + naive, + "topic_meta_path must not be the naive dot-only replacement; that mapping \ + yields {} , a path under a directory that is never created", + naive.display() ); } }