From b89220dfda5ac34e12d9017ad33bbf025f4ca2f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20M=C3=B6sl?= Date: Thu, 20 Aug 2020 14:16:16 +0200 Subject: [PATCH] feat: some stubs for (ternary) bit vectors (#9) --- Cargo.lock | 3 +-- Cargo.toml | 2 +- src/bitvec.rs | 11 +++++++++++ src/engine.rs | 39 +++++++++++++++++++++++++++++++++++++++ src/main.rs | 3 +++ src/ternary.rs | 25 +++++++++++++++++++++++++ 6 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 src/bitvec.rs create mode 100644 src/engine.rs create mode 100644 src/ternary.rs diff --git a/Cargo.lock b/Cargo.lock index 21110da3..681a7598 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -278,8 +278,7 @@ checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce" [[package]] name = "riscv-decode" version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "920466101a1ec4ffac2ab9b72fa780eff62defa0ae9c4f77a07fa41dfd5450e6" +source = "git+https://github.com/cksystemsgroup/riscv-decode#a0a6e21c831a6da3e078513d4a17a1f324dc7d4f" [[package]] name = "scopeguard" diff --git a/Cargo.toml b/Cargo.toml index 2170db36..b5268f7e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,7 @@ description = "Monster is a symbolic execution engine for 64-bit RISC-V code" goblin = "0.2" byteorder = "1.3.4" clap = "3.0.0-beta.1" -riscv-decode = "0.2.0" +riscv-decode = { git = "https://github.com/cksystemsgroup/riscv-decode" } petgraph = "0.5.1" [dev-dependencies] diff --git a/src/bitvec.rs b/src/bitvec.rs new file mode 100644 index 00000000..ad419c18 --- /dev/null +++ b/src/bitvec.rs @@ -0,0 +1,11 @@ +#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] +pub struct BitVector { + pub(crate) value: u64, +} + +impl BitVector { + #[allow(dead_code)] + fn new(value: u64) -> Self { + BitVector { value } + } +} diff --git a/src/engine.rs b/src/engine.rs new file mode 100644 index 00000000..7876dc1b --- /dev/null +++ b/src/engine.rs @@ -0,0 +1,39 @@ +use crate::ternary::TernaryBitVector; + +struct Machine { + #[allow(dead_code)] + regs: [TernaryBitVector; 32], +} + +impl Machine { + #[allow(dead_code)] + fn new() -> Self { + Machine { + regs: [TernaryBitVector::new(0, u64::max_value()); 32], + } + } +} + +// fn has_inv_value_for_addi(raw: IType) -> bool { +// true +// } + +#[cfg(test)] +mod tests { + use super::*; + use riscv_decode::types::*; + use riscv_decode::Instruction; + + #[test] + fn can_find_input_for_addi() { + let machine = Machine::new(); + + let raw = IType(0); + let _addi = Instruction::Addi(raw); + + let _rd = raw.rd(); + let _rs1_value = machine.regs[raw.rs1() as usize]; + + let _s = raw.imm(); + } +} diff --git a/src/main.rs b/src/main.rs index e4bf6abf..a72cc938 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,11 +2,14 @@ use clap::{App, Arg}; use std::fmt::Display; use std::path::Path; +mod bitvec; mod cfg; mod compile; mod decode; mod disassemble; mod elf; +mod engine; +mod ternary; use compile::compile_example; use disassemble::disassemble_riscu; diff --git a/src/ternary.rs b/src/ternary.rs new file mode 100644 index 00000000..dfab42c6 --- /dev/null +++ b/src/ternary.rs @@ -0,0 +1,25 @@ +use crate::bitvec::BitVector; + +#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] +pub struct TernaryBitVector { + xlo: u64, + xhi: u64, +} + +impl TernaryBitVector { + pub fn new(xlo: u64, xhi: u64) -> Self { + assert!(Self::valid(xlo, xhi), "is not a valid ternary bit vector"); + Self { xlo, xhi } + } + + /// verifies that these two values can form a valid ternary bit vector + pub fn valid(xlo: u64, xhi: u64) -> bool { + !xlo | xhi == u64::max_value() + } + + /// verifies that all constant bits of a ternary bit vector match a bit vector + #[allow(dead_code)] + pub fn mcb(&self, other: &BitVector) -> bool { + (self.xhi & other.value == other.value) && (self.xlo | other.value == other.value) + } +}