mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-18 08:15:29 +00:00
Expose shared read-only EVM inspection with retained bounds and regressions
This commit is contained in:
1 parent
76e8a7607f
commit
5a74c5ef36
8 files changed
+171
-15
No files matched your search
@@ -141,3 +141,7 @@ Sanitization, animation fitting and numeric output policy remain caller-owned.
|
||||
## HTTP Archive reader
|
||||
|
||||
`crates/har` preserves Craft Anything `crates/formats/src/har.rs` from `5979f9b13f16dcf1df3c7ae7f8e7735f452866f2` (MIT OR Apache-2.0). Brandon Thomas introduced the typed reader in `f64ae3bf3c8863a982af1304e06de20653803968`; BFlat added UTF-8 BOM compatibility in `cb0bb9d8fa4536ba44ef2c86f5ed759b879cc2d3`. Both source snapshots retain primary author attribution. Shared wiring, explicit byte admission and its tests are subsequent Dylan M. Taylor changes. Redaction and analysis remain app-owned.
|
||||
|
||||
## EVM bytecode inspection
|
||||
|
||||
`crates/evm` extracts Craft Anything `crates/formats/src/evm.rs` at `a31e995858f4312e0f1d59a007112da191ef2d26` (MIT OR Apache-2.0). Eight separate source-history ports preserve Brandon Thomas, Afloat, RRFZ and NetterSilver as primary authors with immutable source references. Synthetic EVM fixtures and deterministic mutation helpers come from the same app source; regression files retain their source provenance. Neutral errors and workspace integration are subsequent changes. No code execution or transaction behavior is imported.
|
||||
Generated
+16
-7
@@ -1881,6 +1881,15 @@ dependencies = [
|
||||
"zip 9.1.3",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-evm"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"serde",
|
||||
"serde_json",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-ews"
|
||||
version = "0.1.0"
|
||||
@@ -2122,13 +2131,6 @@ dependencies = [
|
||||
"flate2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-hdr"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-har"
|
||||
version = "0.1.0"
|
||||
@@ -2137,6 +2139,13 @@ dependencies = [
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-hdr"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-heif"
|
||||
version = "0.1.0"
|
||||
|
||||
@@ -11,6 +11,7 @@ rust-version = "1.95"
|
||||
repository = "https://github.com/storytold/craft-libs"
|
||||
|
||||
[workspace.dependencies]
|
||||
craft-evm = { path = "crates/evm" }
|
||||
craft-har = { path = "crates/har" }
|
||||
craft-jsonc = { path = "crates/jsonc" }
|
||||
craft-palette = { path = "crates/palette" }
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
[package]
|
||||
name = "craft-evm"
|
||||
description = "Read-only EVM bytecode dispatcher and compiler metadata inspection"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
rust-version.workspace = true
|
||||
repository.workspace = true
|
||||
[lints]
|
||||
workspace = true
|
||||
[dependencies]
|
||||
serde = { workspace = true }
|
||||
serde_json = { workspace = true }
|
||||
thiserror = { workspace = true }
|
||||
@@ -0,0 +1,7 @@
|
||||
# craft-evm
|
||||
|
||||
Read-only EVM bytecode inspection: strict raw/hex admission, opcode listing, dispatcher selector and jump-pattern facts, and Solidity/Vyper CBOR metadata. No VM execution, RPC, wallet, signing or transaction support.
|
||||
|
||||
Preserves Craft Anything's parser behavior and limits: 16 MiB decoded bytecode, 4,096 reported functions and 400 listing entries, plus existing bounded CBOR recursion/collection parsing. These are not a total allocator-byte guarantee. Dispatcher facts are heuristics, not a full control-flow or semantic proof. EOF containers remain unsupported.
|
||||
|
||||
Application file I/O, evidence policy and selector naming remain outside this crate.
|
||||
+29
-7
@@ -1,9 +1,28 @@
|
||||
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
|
||||
#![forbid(unsafe_code)]
|
||||
|
||||
//! EVM bytecode: the public function selectors a contract's dispatcher compares against, where
|
||||
//! each one jumps, whether it refuses payments, fallback/receive presence, and the compiler
|
||||
//! metadata (CBOR) appended to the code. Nothing is executed; this is a linear sweep that
|
||||
//! respects PUSH data plus pattern matching over the instruction stream.
|
||||
|
||||
use crate::FormatError;
|
||||
use EvmError as FormatError;
|
||||
|
||||
/// Neutral inspection failure; no code is executed.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
|
||||
pub enum EvmError {
|
||||
#[error("truncated input: {0}")]
|
||||
Truncated(String),
|
||||
#[error("malformed input: {0}")]
|
||||
Malformed(String),
|
||||
#[error("unsupported: {0}")]
|
||||
Unsupported(String),
|
||||
#[error("input too large: {0}")]
|
||||
TooLarge(String),
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test_support;
|
||||
use serde::Serialize;
|
||||
use serde_json::{Map, Value};
|
||||
use std::collections::BTreeSet;
|
||||
@@ -446,7 +465,9 @@ pub fn analyze(code: &[u8]) -> Result<EvmInterface, FormatError> {
|
||||
{
|
||||
Some("div-2^224")
|
||||
}
|
||||
0x63 if imm(body, n) == [0xff, 0xff, 0xff, 0xff] && (op_at(k + d + 1) == Some(0x16) || op_at(k + d + 2) == Some(0x16)) => Some("and-mask"),
|
||||
0x63 if imm(body, n) == [0xff, 0xff, 0xff, 0xff] && (op_at(k + d + 1) == Some(0x16) || op_at(k + d + 2) == Some(0x16)) => {
|
||||
Some("and-mask")
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
if let Some(kind) = kind {
|
||||
@@ -720,7 +741,7 @@ fn reverts_soon(code: &[u8], ins: &[Ins], mut k: usize, index_of: &dyn Fn(usize)
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use craft_anything_testkit::formats as fx;
|
||||
use crate::test_support::formats as fx;
|
||||
|
||||
#[test]
|
||||
fn hex_signs_are_not_hex_digits() {
|
||||
@@ -796,8 +817,9 @@ mod tests {
|
||||
#[test]
|
||||
fn vyper_style_fallthrough() {
|
||||
// PUSH1 0 CALLDATALOAD PUSH1 0xe0 SHR PUSH1 0 MSTORE PUSH4 sel PUSH1 0 MLOAD EQ ISZERO PUSH2 next JUMPI <body: CALLVALUE ...>
|
||||
let mut c =
|
||||
vec![0x60, 0x00, 0x35, 0x60, 0xe0, 0x1c, 0x60, 0x00, 0x52, 0x63, 0x12, 0x34, 0x56, 0x78, 0x60, 0x00, 0x51, 0x14, 0x15, 0x61, 0x00, 0x20, 0x57];
|
||||
let mut c = vec![
|
||||
0x60, 0x00, 0x35, 0x60, 0xe0, 0x1c, 0x60, 0x00, 0x52, 0x63, 0x12, 0x34, 0x56, 0x78, 0x60, 0x00, 0x51, 0x14, 0x15, 0x61, 0x00, 0x20, 0x57,
|
||||
];
|
||||
let body = c.len();
|
||||
c.extend_from_slice(&[0x34, 0x15, 0x00]);
|
||||
let r = analyze(&c).unwrap();
|
||||
@@ -835,14 +857,14 @@ mod tests {
|
||||
#[test]
|
||||
fn hostile_inputs_do_not_panic() {
|
||||
let (code, _) = fx::evm_runtime();
|
||||
for m in craft_anything_testkit::mutations(&code) {
|
||||
for m in crate::test_support::mutations(&code) {
|
||||
let _ = analyze(&m);
|
||||
}
|
||||
for n in 0..code.len() {
|
||||
let _ = analyze(&code[..n]);
|
||||
}
|
||||
for seed in 0..200 {
|
||||
let n = craft_anything_testkit::noise(seed, 300);
|
||||
let n = crate::test_support::noise(seed, 300);
|
||||
let _ = analyze(&n);
|
||||
let _ = decode_input(&n, Encoding::Auto);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
pub mod formats {
|
||||
pub const EVM_SELECTORS: [u32; 3] = [0xa905_9cbb, 0x70a0_8231, 0xd0e3_0db0];
|
||||
|
||||
/// Runtime bytecode with a compiler-style dispatcher, a fallback that reverts, two non-payable
|
||||
/// functions (CALLVALUE check), one payable function, and a CBOR metadata tail (ipfs + solc 0.8.19).
|
||||
/// Returns (bytecode, jump destinations of the three functions).
|
||||
pub fn evm_runtime() -> (Vec<u8>, [usize; 3]) {
|
||||
let mut c: Vec<u8> = vec![0x60, 0x80, 0x60, 0x40, 0x52];
|
||||
// if calldatasize < 4 goto fallback
|
||||
c.extend_from_slice(&[0x60, 0x04, 0x36, 0x10, 0x61, 0, 0]);
|
||||
let fb_patch = c.len() - 2;
|
||||
c.push(0x57);
|
||||
c.extend_from_slice(&[0x60, 0x00, 0x35, 0x60, 0xe0, 0x1c]);
|
||||
let mut patches = Vec::new();
|
||||
for sel in EVM_SELECTORS {
|
||||
c.push(0x80);
|
||||
c.push(0x63);
|
||||
c.extend_from_slice(&sel.to_be_bytes());
|
||||
c.extend_from_slice(&[0x14, 0x61, 0, 0]);
|
||||
patches.push(c.len() - 2);
|
||||
c.push(0x57);
|
||||
}
|
||||
let fallback = c.len();
|
||||
c.extend_from_slice(&[0x5b, 0x60, 0x00, 0x80, 0xfd]);
|
||||
let mut dests = [0usize; 3];
|
||||
for (i, dest) in dests.iter_mut().enumerate() {
|
||||
*dest = c.len();
|
||||
if i < 2 {
|
||||
// JUMPDEST CALLVALUE DUP1 ISZERO PUSH2 ok JUMPI PUSH1 0 DUP1 REVERT JUMPDEST POP STOP
|
||||
c.extend_from_slice(&[0x5b, 0x34, 0x80, 0x15, 0x61, 0, 0]);
|
||||
let ok_patch = c.len() - 2;
|
||||
c.extend_from_slice(&[0x57, 0x60, 0x00, 0x80, 0xfd]);
|
||||
let ok = c.len();
|
||||
c.extend_from_slice(&[0x5b, 0x50, 0x00]);
|
||||
c[ok_patch..ok_patch + 2].copy_from_slice(&(ok as u16).to_be_bytes());
|
||||
} else {
|
||||
c.extend_from_slice(&[0x5b, 0x60, 0x01, 0x60, 0x00, 0x55, 0x00]);
|
||||
}
|
||||
}
|
||||
c[fb_patch..fb_patch + 2].copy_from_slice(&(fallback as u16).to_be_bytes());
|
||||
for (p, d) in patches.iter().zip(dests) {
|
||||
c[*p..*p + 2].copy_from_slice(&(d as u16).to_be_bytes());
|
||||
}
|
||||
c.push(0xfe);
|
||||
let mut meta = vec![0xa2, 0x64, b'i', b'p', b'f', b's', 0x58, 0x22, 0x12, 0x20];
|
||||
meta.extend((0..32u8).map(|i| i.wrapping_mul(7)));
|
||||
meta.extend_from_slice(&[0x64, b's', b'o', b'l', b'c', 0x43, 0x00, 0x08, 0x13]);
|
||||
let len = meta.len() as u16;
|
||||
c.extend_from_slice(&meta);
|
||||
c.extend_from_slice(&len.to_be_bytes());
|
||||
(c, dests)
|
||||
}
|
||||
|
||||
/// Bytecode as `0x`-prefixed lowercase hex.
|
||||
pub fn to_hex(b: &[u8]) -> String {
|
||||
let mut s = String::from("0x");
|
||||
for x in b {
|
||||
s.push_str(&format!("{x:02x}"));
|
||||
}
|
||||
s
|
||||
}
|
||||
}
|
||||
pub fn noise(seed: u64, len: usize) -> Vec<u8> {
|
||||
let mut x = seed.wrapping_mul(0x9e37_79b9_7f4a_7c15) | 1;
|
||||
(0..len)
|
||||
.map(|_| {
|
||||
x ^= x << 13;
|
||||
x ^= x >> 7;
|
||||
x ^= x << 17;
|
||||
x as u8
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
pub fn mutations(sample: &[u8]) -> Vec<Vec<u8>> {
|
||||
let mut out = Vec::new();
|
||||
let n = sample.len();
|
||||
let step = (n / 64).max(1);
|
||||
let mut len = 0;
|
||||
while len < n {
|
||||
out.push(sample[..len].to_vec());
|
||||
len += step;
|
||||
}
|
||||
for (i, seed) in (0..n).step_by(step).zip(1u64..) {
|
||||
let mut m = sample.to_vec();
|
||||
let noise = noise(seed, 8);
|
||||
for (j, v) in noise.iter().enumerate() {
|
||||
if let Some(b) = m.get_mut(i + j) {
|
||||
*b = *v;
|
||||
}
|
||||
}
|
||||
out.push(m);
|
||||
let mut m = sample.to_vec();
|
||||
if let Some(b) = m.get_mut(i) {
|
||||
*b = 0xff;
|
||||
}
|
||||
out.push(m);
|
||||
}
|
||||
out
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
use craft_anything_formats::evm::analyze;
|
||||
use craft_evm::analyze;
|
||||
|
||||
fn contract_with_trailer(cbor: &[u8]) -> Vec<u8> {
|
||||
let mut code = vec![0x5b, 0x00];
|
||||
|
||||
Reference in new issue
Block a user