Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions contrib/codeql/lib/policy.qll
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ predicate isOpaqueType(TypeItem t) {
isSingleTupleField(t)
}

/** Holds if `t` is a compile-time marker type (empty enum, zero-sized). */
predicate isMarkerType(TypeItem t) {
t instanceof Enum and
t.(Enum).hasVariantList() and
count(t.(Enum).getVariantList().getAVariant()) = 0
}

/** Materialises (TypeItem, fieldTypeName, crate) for join efficiency. */
pragma[nomagic]
private predicate fieldTypeInCrate(TypeItem t, string fieldTypeName, string crate) {
Expand Down Expand Up @@ -117,6 +124,9 @@ predicate isCodecType(TypeItem t) {
t.getName().getText() = "ArrayBuf"
}

/** Holds if `name` is a trait whose methods must have a body in exactly one layer. */
predicate isMutexTrait(string name) { name = "BlsScheme" }

/** Holds if `t` lives in a crate with no public API. */
predicate isPrivateCrate(TypeItem t) {
exists(string path |
Expand Down Expand Up @@ -223,6 +233,8 @@ predicate isSerdeExempt(TypeItem t) {
or
isOpaqueType(t)
or
isMarkerType(t)
or
hasLifetime(t)
or
// Single-field wrappers without PartialEq are exempt.
Expand Down
19 changes: 19 additions & 0 deletions contrib/codeql/lib/traits.qll
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,25 @@ string implSelfName(Impl i) {
result = i.getSelfTy().(PathTypeRepr).getPath().getSegment().getIdentifier().getText()
}

/** Gets a method defined in `i`'s associated item list. */
Function implMethod(Impl i) { result = i.getAssocItemList().getAnAssocItem() }

/**
* Holds if `over` in `i` overrides the default body that `decl` supplies
* in trait `t`, i.e. both layers define the same method name and the
* trait's declaration carries a body of its own.
*/
predicate overridesDefault(Trait t, Function decl, Impl i, Function over) {
decl = traitMethod(t) and
decl.hasBody() and
implTraitName(i) = t.getName().getText() and
over = implMethod(i) and
over.getName().getText() = decl.getName().getText()
}

/** Gets a method declared directly in `t`'s associated item list. */
Function traitMethod(Trait t) { result = t.getAssocItemList().getAnAssocItem() }

/** Holds if `t` has a derived impl for `traitName`. */
predicate hasDerivedImpl(TypeItem t, string traitName) {
exists(MacroItems expansion, Impl i |
Expand Down
29 changes: 29 additions & 0 deletions contrib/codeql/trait.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright (c) 2026-present, The Dash Core developers
* SPDX-License-Identifier: MIT
* See the accompanying file LICENSE or https://opensource.org/license/MIT
*
* @id base-sdk/trait-rules
* @name Trait definition and implementation rules
* @description Enforces that a method body lives in one layer only.
* @kind problem
* @precision very-high
* @problem.severity error
* @tags correctness maintainability
*/

import lib.fmt
import lib.policy
import lib.traits
import rust

from Function over, string message
where
exists(Trait t, Function decl, Impl i |
isMutexTrait(t.getName().getText()) and
overridesDefault(t, decl, i, over) and
message =
fmt("{0} overrides the default {1} provides for {2}", implSelfName(i), t.getName().getText(),
fmt("{0}()", decl.getName().getText()))
)
select over, message
9 changes: 9 additions & 0 deletions contrib/semgrep/cargo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
rules:
- id: cargo-serde-json-restrict
message: "serde-json{,5} dependency unexpected outside dash-dev crate"
severity: ERROR
languages: [generic]
paths:
include: [/pkgs/**/Cargo.toml]
exclude: [/pkgs/dev/Cargo.toml]
pattern-regex: \b(serde[-_]json|json5)\b
1 change: 1 addition & 0 deletions pkgs/dev/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ xxhash-rust = { version = "0.8", features = ["xxh32"] }
bitcoin-consensus-encoding = { version = "0.2", default-features = false, features = [
"alloc",
] }
cfg-if = "1"
dash-num = { version = "0.0.0", path = "../num", optional = true }
dash-params = { version = "0.0.0", path = "../params", optional = true }
dash-pow = { version = "0.0.0", path = "../pow", optional = true }
Expand Down
177 changes: 110 additions & 67 deletions pkgs/dev/src/corpus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,75 +8,42 @@

use crate::prelude::*;

/// A typed corpus entry pairing raw wire hex with expected details.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
pub struct CorpusEntry<T> {
pub raw: String,
pub details: T,
}
use hex_conservative::FromHex;
use serde::{de::DeserializeOwned, Deserialize, Serialize};

/// Reads a corpus JSON5 file from disk.
///
/// The file lives at `<manifest_dir>/corpus/<file>.json5`.
///
/// # Panics
///
/// Panics if the file cannot be read.
#[cfg(feature = "std")]
pub fn load_corpus_file(manifest_dir: &str, file: &str) -> String {
let path = format!("{manifest_dir}/corpus/{file}.json5");
std::fs::read_to_string(&path).unwrap_or_else(|e| panic!("{path}: {e}"))
}
use core::fmt;
use std::fs;

/// Reads a corpus section from JSON5 text.
/// Verifies the serde round-trip for a set of corpus entries.
///
/// Parses `text` as `{ "section": { "label": { raw, details } } }`,
/// hex-decodes `raw` to bytes, calls `check(raw_bytes, &details,
/// label)` for each entry, and returns all details keyed by label.
/// Writes `items` to JSON via [`write_corpus`], reads them back through
/// [`Corpus::entries`] (no-op check), and asserts equality.
///
/// # Panics
///
/// Panics if the section is missing, empty, or the check function
/// panics.
#[cfg(all(feature = "std", feature = "serde"))]
pub fn read_corpus<T: ::serde::de::DeserializeOwned>(
text: &str,
section: &str,
mut check: impl FnMut(&[u8], &T, &str),
) -> BTreeMap<String, T> {
use hex_conservative::FromHex;

let mut outer: BTreeMap<String, serde_json::Value> =
json5::from_str(text).unwrap_or_else(|e| panic!("{section}: parse: {e}"));
let section_val = outer.remove(section).unwrap_or_else(|| panic!("{section}: not found"));
let entries: BTreeMap<String, CorpusEntry<T>> =
serde_json::from_value(section_val).unwrap_or_else(|e| panic!("{section}: {e}"));
assert!(!entries.is_empty(), "{section}: empty");

let mut result = BTreeMap::new();
for (label, entry) in entries {
let bytes = Vec::<u8>::from_hex(&entry.raw).unwrap_or_else(|e| panic!("{section}/{label}: hex: {e}"));
check(&bytes, &entry.details, &label);
result.insert(label, entry.details);
}
result
/// Panics on round-trip mismatch.
pub fn assert_serde_rt<T>(section: &str, items: &BTreeMap<String, T>)
where
T: DeserializeOwned + Serialize + PartialEq + fmt::Debug,
{
let json = write_corpus(section, items);
let rt = Corpus::parse(section, &json).entries::<T>(section, |_, _, _| {});
assert_eq!(*items, rt, "{section}: serde round-trip");
}

/// Serializes corpus entries to JSON in `{ raw, details }` format,
/// wrapped in a section key.
///
/// Produces `{ "section": { "label": { "raw": "", "details": T } } }`
/// so the output can be read back by [`read_corpus`] with a no-op
/// so the output can be read back by [`Corpus::entries`] with a no-op
/// check function to verify the serde round-trip.
///
/// # Panics
///
/// Panics if serialization fails.
#[cfg(all(feature = "std", feature = "serde"))]
pub fn write_corpus<T: ::serde::Serialize>(section: &str, entries: &BTreeMap<String, T>) -> String {
#[derive(::serde::Serialize)]
struct Raw<'a, T: ::serde::Serialize> {
pub(crate) fn write_corpus<T: Serialize>(section: &str, entries: &BTreeMap<String, T>) -> String {
#[derive(Serialize)]
struct Raw<'a, T: Serialize> {
raw: &'a str,
details: &'a T,
}
Expand All @@ -88,20 +55,96 @@ pub fn write_corpus<T: ::serde::Serialize>(section: &str, entries: &BTreeMap<Str
serde_json::to_string(&outer).unwrap_or_else(|e| panic!("write_corpus: {e}"))
}

/// Verifies the serde round-trip for a set of corpus entries.
///
/// Writes `items` to JSON via [`write_corpus`], reads them back
/// with [`read_corpus`] (no-op check), and asserts equality.
///
/// # Panics
/// A typed corpus entry pairing raw wire hex with expected details.
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub(crate) struct CorpusEntry<T> {
pub raw: String,
pub details: T,
}

/// A parsed corpus file, opened once and queried by section.
///
/// Panics on round-trip mismatch.
#[cfg(all(feature = "std", feature = "serde"))]
pub fn assert_serde_rt<T>(section: &str, items: &BTreeMap<String, T>)
where
T: ::serde::de::DeserializeOwned + ::serde::Serialize + PartialEq + core::fmt::Debug,
{
let json = write_corpus(section, items);
let rt = read_corpus::<T>(&json, section, |_, _, _| {});
assert_eq!(*items, rt, "{section}: serde round-trip");
/// Serves both operation KATs via [`Corpus::vectors`] (array sections) and
/// wire round-trip corpora via [`Corpus::entries`] (raw/details sections).
#[derive(Clone, Debug)]
pub struct Corpus {
name: String,
root: serde_json::Value,
}

impl Corpus {
/// Parses corpus text (JSON5) under a diagnostic `name`.
///
/// # Panics
///
/// Panics if the text is not valid JSON5.
pub(crate) fn parse(name: &str, text: &str) -> Self {
let root = json5::from_str(text).unwrap_or_else(|e| panic!("{name}: parse: {e}"));
Self {
name: name.into(),
root,
}
}

/// Consumes the handle and returns the parsed root value.
pub fn into_value(self) -> serde_json::Value {
self.root
}

/// Returns a named `{ label: { raw, details } }` section.
///
/// Hex-decodes each `raw`, calls `check(raw_bytes, &details, label)`, and
/// returns the details keyed by label.
///
/// # Panics
///
/// Panics if the section is missing, empty, or `check` panics.
pub fn entries<T: DeserializeOwned>(
&self,
section: &str,
mut check: impl FnMut(&[u8], &T, &str),
) -> BTreeMap<String, T> {
let val = self
.root
.get(section)
.unwrap_or_else(|| panic!("{}: missing section '{section}'", self.name));
let entries: BTreeMap<String, CorpusEntry<T>> =
serde_json::from_value(val.clone()).unwrap_or_else(|e| panic!("{}: section '{section}': {e}", self.name));
assert!(!entries.is_empty(), "{}: section '{section}' empty", self.name);

let mut result = BTreeMap::new();
for (label, entry) in entries {
let bytes = Vec::<u8>::from_hex(&entry.raw).unwrap_or_else(|e| panic!("{section}/{label}: hex: {e}"));
Comment thread
coderabbitai[bot] marked this conversation as resolved.
check(&bytes, &entry.details, &label);
result.insert(label, entry.details);
}
result
}

/// Opens and parses `<manifest_dir>/corpus/<name>.json5`.
///
/// # Panics
///
/// Panics if the file cannot be read or parsed.
pub fn open(manifest_dir: &str, name: &str) -> Self {
let path = format!("{manifest_dir}/corpus/{name}.json5");
let text = fs::read_to_string(&path).unwrap_or_else(|e| panic!("cannot read {path}: {e}"));
Self::parse(name, &text)
}

/// Returns a named array section as typed vectors: `{ section: [T, ...] }`.
///
/// # Panics
///
/// Panics if the section is missing, empty, or is not an array of `T`.
pub fn vectors<T: ::serde::de::DeserializeOwned>(&self, section: &str) -> Vec<T> {
let val = self
.root
.get(section)
.unwrap_or_else(|| panic!("{}: missing section '{section}'", self.name));
let out: Vec<T> =
serde_json::from_value(val.clone()).unwrap_or_else(|e| panic!("{}: section '{section}': {e}", self.name));
assert!(!out.is_empty(), "{}: section '{section}' empty", self.name);
out
}
}
Loading
Loading