From 212c5f28dbb83887cec63b3dbe2bf3a4657a6b94 Mon Sep 17 00:00:00 2001 From: Lucas Steuernagel Date: Fri, 31 May 2024 11:28:43 -0300 Subject: [PATCH] Add shuttle testing infrastructure --- .github/workflows/main.yml | 1 + Cargo.lock | 259 ++++++++++++++++++++++++++++++++++++- Cargo.toml | 2 + src/assembler.rs | 8 +- src/elf.rs | 8 +- src/jit.rs | 11 +- src/vm.rs | 17 ++- 7 files changed, 291 insertions(+), 15 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 17caf6f72..adf5fecdf 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -37,6 +37,7 @@ jobs: run: | export RUSTFLAGS="-D warnings" cargo build --verbose + cargo build --features="shuttle-test" cargo test --verbose cargo test --test exercise_instructions --verbose shell: bash diff --git a/Cargo.lock b/Cargo.lock index 067325a34..33a4ac27e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,12 @@ version = "0.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eab1c04a571841102f5345a8fc0f6bb3d31c315dec879b5c6e42e40ce7ffa34e" +[[package]] +name = "assoc" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfdc70193dadb9d7287fa4b633f15f90c876915b31f6af17da307fc59c9859a8" + [[package]] name = "autocfg" version = "1.1.0" @@ -29,6 +35,18 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "bitvec" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bc2832c24239b0141d5674bb9174f9d68a8b5b3f2753311927c172ca46f7e9c" +dependencies = [ + "funty", + "radium", + "tap", + "wyz", +] + [[package]] name = "byteorder" version = "0.5.3" @@ -41,6 +59,12 @@ version = "1.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" +[[package]] +name = "cc" +version = "1.0.97" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "099a5357d84c4c61eb35fc8eafa9a79a902c2f76911e5747ced4e032edd8d9b4" + [[package]] name = "cfg-if" version = "1.0.0" @@ -86,6 +110,12 @@ dependencies = [ "byteorder 0.5.3", ] +[[package]] +name = "funty" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" + [[package]] name = "gdbstub" version = "0.6.3" @@ -100,6 +130,20 @@ dependencies = [ "paste", ] +[[package]] +name = "generator" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "186014d53bc231d0090ef8d6f03e0920c54d85a5ed22f4f2f74315ec56cf83fb" +dependencies = [ + "cc", + "cfg-if", + "libc", + "log", + "rustversion", + "windows", +] + [[package]] name = "getrandom" version = "0.2.6" @@ -131,6 +175,12 @@ dependencies = [ "byteorder 1.4.3", ] +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + [[package]] name = "json" version = "0.12.4" @@ -145,12 +195,9 @@ checksum = "ec647867e2bf0772e28c8bcde4f0d19a9216916e890543b5a03ed8ef27b8f259" [[package]] name = "log" -version = "0.4.16" +version = "0.4.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6389c490849ff5bc16be905ae24bc913a9c8892e19b2341dbc175e14c341c2b8" -dependencies = [ - "cfg-if", -] +checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" [[package]] name = "managed" @@ -173,12 +220,30 @@ dependencies = [ "autocfg", ] +[[package]] +name = "once_cell" +version = "1.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" + +[[package]] +name = "owo-colors" +version = "3.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1b04fb49957986fdce4d6ee7a65027d55d4b6d2265e5848bbb507b58ccfdb6f" + [[package]] name = "paste" version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b1de2e551fb905ac83f73f7aedf2f0cb4a0da7e35efa24a202a936269f1f18e1" +[[package]] +name = "pin-project-lite" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02" + [[package]] name = "plain" version = "0.2.3" @@ -209,6 +274,12 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "radium" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09" + [[package]] name = "rand" version = "0.8.5" @@ -232,19 +303,40 @@ dependencies = [ [[package]] name = "rand_core" -version = "0.6.3" +version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" dependencies = [ "getrandom", ] +[[package]] +name = "rand_pcg" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59cad018caf63deb318e5a4586d99a24424a364f40f1e5778c29aca23f4fc73e" +dependencies = [ + "rand_core", +] + [[package]] name = "rustc-demangle" version = "0.1.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7ef03e0a2b150c7a90d01faf6254c9c48a41e95fb2a8c2ac1c6f0d2b9aefc342" +[[package]] +name = "rustversion" +version = "1.0.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "092474d1a01ea8278f69e6a358998405fae5b8b963ddaeb2b0b04a128bf1dfb0" + +[[package]] +name = "scoped-tls" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294" + [[package]] name = "scroll" version = "0.11.0" @@ -265,6 +357,31 @@ dependencies = [ "syn", ] +[[package]] +name = "shuttle" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa904d5f504c34f1aa43218ee180173970a360124f44bdbaeda4e3d8d53e24c3" +dependencies = [ + "assoc", + "bitvec", + "generator", + "hex", + "owo-colors", + "rand", + "rand_core", + "rand_pcg", + "scoped-tls", + "smallvec", + "tracing", +] + +[[package]] +name = "smallvec" +version = "1.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" + [[package]] name = "solana_rbpf" version = "0.8.0" @@ -282,6 +399,7 @@ dependencies = [ "rand", "rustc-demangle", "scroll", + "shuttle", "test_utils", "thiserror", "winapi", @@ -298,6 +416,12 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "tap" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" + [[package]] name = "test_utils" version = "0.8.0" @@ -326,6 +450,25 @@ dependencies = [ "syn", ] +[[package]] +name = "tracing" +version = "0.1.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" +dependencies = [ + "pin-project-lite", + "tracing-core", +] + +[[package]] +name = "tracing-core" +version = "0.1.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" +dependencies = [ + "once_cell", +] + [[package]] name = "unicode-xid" version = "0.2.2" @@ -374,3 +517,105 @@ name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows" +version = "0.54.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9252e5725dbed82865af151df558e754e4a3c2c30818359eb17465f1346a1b49" +dependencies = [ + "windows-core", + "windows-targets", +] + +[[package]] +name = "windows-core" +version = "0.54.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12661b9c89351d684a50a8a643ce5f608e20243b9fb84687800163429f161d65" +dependencies = [ + "windows-result", + "windows-targets", +] + +[[package]] +name = "windows-result" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "749f0da9cc72d82e600d8d2e44cadd0b9eedb9038f71a1c58556ac1c5791813b" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f0713a46559409d202e70e28227288446bf7841d3211583a4b53e3f6d96e7eb" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7088eed71e8b8dda258ecc8bac5fb1153c5cffaf2578fc8ff5d61e23578d3263" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9985fd1504e250c615ca5f281c3f7a6da76213ebd5ccc9561496568a2752afb6" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88ba073cf16d5372720ec942a8ccbf61626074c6d4dd2e745299726ce8b89670" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87f4261229030a858f36b459e748ae97545d6f1ec60e5e0d6a3d32e0dc232ee9" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db3c2bf3d13d5b658be73463284eaf12830ac9a26a90c717b7f771dfe97487bf" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e4246f76bdeff09eb48875a0fd3e2af6aada79d409d33011886d3e1581517d9" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "852298e482cd67c356ddd9570386e2862b5673c85bd5f88df9ab6802b334c596" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" + +[[package]] +name = "wyz" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05f360fc0b24296329c78fda852a1e9ae82de9cf7b27dae4b7f62f118f77b9ed" +dependencies = [ + "tap", +] diff --git a/Cargo.toml b/Cargo.toml index 5486da280..b7acdfa7a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,6 +29,7 @@ log = "0.4.2" rand = { version = "0.8.5", features = ["small_rng"]} rustc-demangle = "0.1" scroll = "0.11" +shuttle = { version = "0.7.0", optional = true } thiserror = "1.0.26" [target.'cfg(windows)'.dependencies] @@ -42,6 +43,7 @@ default = ["jit"] jit = ["libc", "winapi"] fuzzer-not-safe-for-production = ["arbitrary"] debugger = ["gdbstub"] +shuttle-test = ["dep:shuttle"] [dev-dependencies] elf = "0.0.10" diff --git a/src/assembler.rs b/src/assembler.rs index ff124b700..4d3271ca6 100644 --- a/src/assembler.rs +++ b/src/assembler.rs @@ -22,7 +22,13 @@ use crate::{ program::{BuiltinProgram, FunctionRegistry, SBPFVersion}, vm::ContextObject, }; -use std::{collections::HashMap, sync::Arc}; +use std::collections::HashMap; + +#[cfg(not(feature = "shuttle-test"))] +use std::sync::Arc; + +#[cfg(feature = "shuttle-test")] +use shuttle::sync::Arc; #[derive(Clone, Copy, Debug, PartialEq)] enum InstructionType { diff --git a/src/elf.rs b/src/elf.rs index 0d4658363..e8813ea83 100644 --- a/src/elf.rs +++ b/src/elf.rs @@ -30,7 +30,13 @@ use crate::{ #[cfg(all(feature = "jit", not(target_os = "windows"), target_arch = "x86_64"))] use crate::jit::{JitCompiler, JitProgram}; use byteorder::{ByteOrder, LittleEndian}; -use std::{collections::BTreeMap, fmt::Debug, mem, ops::Range, str, sync::Arc}; +use std::{collections::BTreeMap, fmt::Debug, mem, ops::Range, str}; + +#[cfg(not(feature = "shuttle-test"))] +use std::sync::Arc; + +#[cfg(feature = "shuttle-test")] +use shuttle::sync::Arc; /// Error definitions #[derive(Debug, thiserror::Error, PartialEq, Eq)] diff --git a/src/jit.rs b/src/jit.rs index 69782f754..2d6aa4c13 100644 --- a/src/jit.rs +++ b/src/jit.rs @@ -10,7 +10,12 @@ // the MIT license , at your option. This file may not be // copied, modified, or distributed except according to those terms. -use rand::{rngs::SmallRng, Rng, SeedableRng}; +#[cfg(not(feature = "shuttle-test"))] +use rand::{rngs::SmallRng, thread_rng, Rng, SeedableRng}; + +#[cfg(feature = "shuttle-test")] +use shuttle::rand::{rngs::SmallRng, thread_rng, Rng, SeedableRng}; + use std::{fmt::Debug, mem, ptr}; use crate::{ @@ -352,8 +357,8 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> { debug_assert!(code_length_estimate < (i32::MAX as usize)); let runtime_environment_key = get_runtime_environment_key(); - let mut diversification_rng = SmallRng::from_rng(rand::thread_rng()).map_err(|_| EbpfError::JitNotCompiled)?; - + let mut diversification_rng = SmallRng::from_rng(thread_rng()).map_err(|_| EbpfError::JitNotCompiled)?; + Ok(Self { result: JitProgram::new(pc, code_length_estimate)?, text_section_jumps: vec![], diff --git a/src/vm.rs b/src/vm.rs index 40c222a75..10b6e8226 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -21,8 +21,19 @@ use crate::{ program::{BuiltinFunction, BuiltinProgram, FunctionRegistry, SBPFVersion}, static_analysis::{Analysis, TraceLogEntry}, }; -use rand::Rng; -use std::{collections::BTreeMap, fmt::Debug, sync::Arc}; +use std::{collections::BTreeMap, fmt::Debug}; + +#[cfg(not(feature = "shuttle-test"))] +use { + rand::{thread_rng, Rng}, + std::sync::Arc, +}; + +#[cfg(feature = "shuttle-test")] +use shuttle::{ + rand::{thread_rng, Rng}, + sync::Arc, +}; /// Shift the RUNTIME_ENVIRONMENT_KEY by this many bits to the LSB /// @@ -33,7 +44,7 @@ static RUNTIME_ENVIRONMENT_KEY: std::sync::OnceLock = std::sync::OnceLock:: /// Returns (and if not done before generates) the encryption key for the VM pointer pub fn get_runtime_environment_key() -> i32 { *RUNTIME_ENVIRONMENT_KEY - .get_or_init(|| rand::thread_rng().gen::() >> PROGRAM_ENVIRONMENT_KEY_SHIFT) + .get_or_init(|| thread_rng().gen::() >> PROGRAM_ENVIRONMENT_KEY_SHIFT) } /// VM configuration settings