summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoriximeow <me@iximeow.net>2023-12-23 15:41:09 -0800
committeriximeow <me@iximeow.net>2023-12-23 15:41:18 -0800
commit6668e8f87a3d2cf814c1ddd77a0eb6fec668c3eb (patch)
tree1f3e0c132196e6ab04a05c19ccc2742f902b14d4
parent92260064e2073a6b3e18c0d2c042002740711c7b (diff)
more fuzz targets
-rw-r--r--fuzz/Cargo.toml8
-rw-r--r--fuzz/fuzz_targets/fresh-decode.rs28
-rw-r--r--src/lib.rs19
3 files changed, 51 insertions, 4 deletions
diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml
index cf84c01..29a2307 100644
--- a/fuzz/Cargo.toml
+++ b/fuzz/Cargo.toml
@@ -18,13 +18,13 @@ yaxpeax-arch = "0.2.7"
members = ["."]
[[bin]]
-name = "fuzz_target_1"
-path = "fuzz_targets/fuzz_target_1.rs"
+name = "no-panic"
+path = "fuzz_targets/no-panic.rs"
test = false
doc = false
[[bin]]
-name = "no-panic"
-path = "fuzz_targets/no-panic.rs"
+name = "fresh-decode"
+path = "fuzz_targets/fresh-decode.rs"
test = false
doc = false
diff --git a/fuzz/fuzz_targets/fresh-decode.rs b/fuzz/fuzz_targets/fresh-decode.rs
new file mode 100644
index 0000000..94664aa
--- /dev/null
+++ b/fuzz/fuzz_targets/fresh-decode.rs
@@ -0,0 +1,28 @@
+//! decoding into a pre-existing instruction should not result in different outcomes compared to
+//! decoding into a fresh instruction. if decoding succeeds, both outcomes should be equal.
+
+#![no_main]
+use libfuzzer_sys::fuzz_target;
+
+use yaxpeax_arch::Decoder;
+
+fuzz_target!(|data: &[u8]| {
+
+ let decoders = [
+ yaxpeax_rx::InstDecoder::v1(),
+ yaxpeax_rx::InstDecoder::v2(),
+ yaxpeax_rx::InstDecoder::v3(),
+ ];
+
+ let mut reused_inst = yaxpeax_rx::Instruction::default();
+
+ for decoder in decoders {
+ let mut words = yaxpeax_arch::U8Reader::new(data);
+ // test decoding, may be ok or not, but should not panic
+ if let Ok(()) = decoder.decode_into(&mut reused_inst, &mut words) {
+ let mut words = yaxpeax_arch::U8Reader::new(data);
+ let fresh_inst = decoder.decode(&mut words).expect("decoded before, can decode again");
+ assert_eq!(reused_inst, fresh_inst);
+ }
+ }
+});
diff --git a/src/lib.rs b/src/lib.rs
index 67272b0..68f0819 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -22,6 +22,25 @@ pub struct Instruction {
length: u8,
}
+impl PartialEq for Instruction {
+ fn eq(&self, other: &Self) -> bool {
+ let Instruction {
+ opcode: left_opcode,
+ operands: _, // we won't use this `operands`, but `.operands()` to not use stale extra operands
+ length: left_length,
+ } = self;
+ let Instruction {
+ opcode: right_opcode,
+ operands: _, // we won't use this `operands`, but `.operands()` to not use stale extra operands
+ length: right_length,
+ } = other;
+
+ left_opcode == right_opcode &&
+ left_length == right_length &&
+ self.operands() == other.operands()
+ }
+}
+
impl Instruction {
pub fn opcode(&self) -> Opcode {
self.opcode