From c29845e4a187050a11421494d8e595e00438d771 Mon Sep 17 00:00:00 2001 From: abel <67806187+standard3@users.noreply.github.com> Date: Thu, 14 Nov 2024 18:22:03 +0100 Subject: [PATCH] refactor: made cargo happy --- src/block.rs | 86 +++++------------- src/byte_interval.rs | 16 ++++ src/cfg.rs | 70 +++++---------- src/ir.rs | 18 ++-- src/lib.rs | 1 - src/module.rs | 168 ++++++++-------------------------- src/node.rs | 3 +- src/section.rs | 56 +++--------- src/symbol.rs | 20 ++--- src/symbolic_expression.rs | 179 ++++--------------------------------- 10 files changed, 147 insertions(+), 470 deletions(-) diff --git a/src/block.rs b/src/block.rs index 6297863..2ea7fb7 100644 --- a/src/block.rs +++ b/src/block.rs @@ -4,9 +4,9 @@ //! - [ ] Implement the `DataBlock` struct and Block trait //! - [ ] Implement the `ProxyBlock` struct and Block trait -use crate::node::Node; +// use crate::node::Node; -use uuid::{uuid, Uuid}; +use uuid::Uuid; /// Block represents a base trait for blocks. [`Symbol`] objects may have references to any kind of Block. // pub trait Block { @@ -17,79 +17,39 @@ use uuid::{uuid, Uuid}; // fn get_references(&self) -> Vec; // } +#[derive(Clone, PartialEq)] +pub struct Block { + pub offset: u64, + pub value: Option, +} + +#[derive(Clone, PartialEq)] +pub enum BlockValue { + Code(CodeBlock), + Data(DataBlock), +} + #[derive(Clone, PartialEq)] pub struct ProxyBlock { - pub uuid: Vec, + pub uuid: Uuid, } + #[derive(Clone, PartialEq)] pub struct CodeBlock { - pub uuid: Vec, - + pub uuid: Uuid, pub size: u64, - - pub decode_mode: i32, + pub decode_mode: DecodeMode, } + #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] #[repr(i32)] pub enum DecodeMode { - AllDefault = 0, - ArmThumb = 1, -} -impl DecodeMode { - /// String value of the enum field names used in the ProtoBuf definition. - /// - /// The values are not transformed in any way and thus are considered stable - /// (if the ProtoBuf definition does not change) and safe for programmatic use. - pub fn as_str_name(&self) -> &'static str { - match self { - Self::AllDefault => "All_Default", - Self::ArmThumb => "ARM_Thumb", - } - } - /// Creates an enum from field names used in the ProtoBuf definition. - pub fn from_str_name(value: &str) -> ::core::option::Option { - match value { - "All_Default" => Some(Self::AllDefault), - "ARM_Thumb" => Some(Self::ArmThumb), - _ => None, - } - } -} -#[derive(Clone, PartialEq)] -pub struct DataBlock { - pub uuid: Vec, - - pub size: u64, -} - -#[derive(Clone, PartialEq)] -pub struct Block { - pub offset: u64, - - pub value: ::core::option::Option, + AllDefault, + ArmThumb, } -/// Nested message and enum types in `Block`. -pub mod block { - #[derive(Clone, PartialEq)] - pub enum Value { - Code(super::CodeBlock), - Data(super::DataBlock), - } -} #[derive(Clone, PartialEq)] -pub struct ByteInterval { - pub uuid: Vec, - - pub blocks: Vec, - - pub symbolic_expressions: ::std::collections::HashMap, - - pub has_address: bool, - - pub address: u64, - +pub struct DataBlock { + pub uuid: Uuid, pub size: u64, - - pub contents: Vec, } diff --git a/src/byte_interval.rs b/src/byte_interval.rs index 8b13789..5556eea 100644 --- a/src/byte_interval.rs +++ b/src/byte_interval.rs @@ -1 +1,17 @@ +use std::collections::HashMap; +use crate::block::Block; +use crate::symbolic_expression::SymbolicExpression; + +use uuid::Uuid; + +#[derive(Clone, PartialEq)] +pub struct ByteInterval { + pub uuid: Uuid, + pub blocks: Vec, + pub symbolic_expressions: HashMap, + pub has_address: bool, + pub address: u64, + pub size: u64, + pub contents: Vec, +} diff --git a/src/cfg.rs b/src/cfg.rs index 311f2e3..b9d3a59 100644 --- a/src/cfg.rs +++ b/src/cfg.rs @@ -1,60 +1,32 @@ -#[derive(Clone, Copy, PartialEq)] -pub struct EdgeLabel { - pub conditional: bool, +use uuid::Uuid; - pub direct: bool, - - pub r#type: i32, +#[derive(Clone, PartialEq)] +pub struct Cfg { + pub vertices: Vec>, + pub edges: Vec, } + #[derive(Clone, PartialEq)] pub struct Edge { - pub source_uuid: Vec, - - pub target_uuid: Vec, - - pub label: ::core::option::Option, + pub source_uuid: Uuid, + pub target_uuid: Uuid, + pub label: Option, } -#[derive(Clone, PartialEq)] -pub struct Cfg { - pub vertices: Vec>, - pub edges: Vec, +#[derive(Clone, Copy, PartialEq)] +pub struct EdgeLabel { + pub conditional: bool, + pub direct: bool, + pub r#type: EdgeType, } + #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] #[repr(i32)] pub enum EdgeType { - TypeBranch = 0, - TypeCall = 1, - TypeFallthrough = 2, - TypeReturn = 3, - TypeSyscall = 4, - TypeSysret = 5, -} -impl EdgeType { - /// String value of the enum field names used in the ProtoBuf definition. - /// - /// The values are not transformed in any way and thus are considered stable - /// (if the ProtoBuf definition does not change) and safe for programmatic use. - pub fn as_str_name(&self) -> &'static str { - match self { - Self::TypeBranch => "Type_Branch", - Self::TypeCall => "Type_Call", - Self::TypeFallthrough => "Type_Fallthrough", - Self::TypeReturn => "Type_Return", - Self::TypeSyscall => "Type_Syscall", - Self::TypeSysret => "Type_Sysret", - } - } - /// Creates an enum from field names used in the ProtoBuf definition. - pub fn from_str_name(value: &str) -> ::core::option::Option { - match value { - "Type_Branch" => Some(Self::TypeBranch), - "Type_Call" => Some(Self::TypeCall), - "Type_Fallthrough" => Some(Self::TypeFallthrough), - "Type_Return" => Some(Self::TypeReturn), - "Type_Syscall" => Some(Self::TypeSyscall), - "Type_Sysret" => Some(Self::TypeSysret), - _ => None, - } - } + Branch, + Call, + Fallthrough, + Return, + Syscall, + Sysret, } diff --git a/src/ir.rs b/src/ir.rs index 797b2f6..2c4f452 100644 --- a/src/ir.rs +++ b/src/ir.rs @@ -1,12 +1,16 @@ -#[derive(Clone, PartialEq)] -pub struct Ir { - pub uuid: Vec, +use crate::auxdata::AuxData; +use crate::cfg::Cfg; +use crate::module::Module; - pub modules: Vec, +use std::collections::HashMap; - pub aux_data: ::std::collections::HashMap, +use uuid::Uuid; +#[derive(Clone, PartialEq)] +pub struct Ir { + pub uuid: Uuid, + pub modules: Vec, + pub aux_data: HashMap, pub version: u32, - - pub cfg: ::core::option::Option, + pub cfg: Option, } diff --git a/src/lib.rs b/src/lib.rs index ace7ce3..1b607c9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,7 +8,6 @@ pub mod cfg; pub mod ir; pub mod module; pub mod node; -pub mod offset; pub mod section; pub mod symbol; pub mod symbolic_expression; diff --git a/src/module.rs b/src/module.rs index f3aad48..a5dcbc7 100644 --- a/src/module.rs +++ b/src/module.rs @@ -1,154 +1,62 @@ +use crate::auxdata::AuxData; +use crate::block::ProxyBlock; +use crate::section::Section; +use crate::symbol::Symbol; + +use std::collections::HashMap; + +use uuid::Uuid; + #[derive(Clone, PartialEq)] pub struct Module { - pub uuid: Vec, - + pub uuid: Uuid, pub binary_path: String, - pub preferred_addr: u64, - pub rebase_delta: i64, - pub file_format: i32, - - pub isa: i32, - + pub isa: Isa, pub name: String, - pub symbols: Vec, - pub proxies: Vec, - pub sections: Vec
, - - pub aux_data: ::std::collections::HashMap, - + pub aux_data: HashMap, pub entry_point: Vec, - - pub byte_order: i32, + pub byte_order: ByteOrder, } + #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] #[repr(i32)] pub enum FileFormat { - FormatUndefined = 0, - Coff = 1, - Elf = 2, - Pe = 3, - IdaProDb32 = 4, - IdaProDb64 = 5, - Xcoff = 6, - Macho = 7, - Raw = 8, -} -impl FileFormat { - /// String value of the enum field names used in the ProtoBuf definition. - /// - /// The values are not transformed in any way and thus are considered stable - /// (if the ProtoBuf definition does not change) and safe for programmatic use. - pub fn as_str_name(&self) -> &'static str { - match self { - Self::FormatUndefined => "Format_Undefined", - Self::Coff => "COFF", - Self::Elf => "ELF", - Self::Pe => "PE", - Self::IdaProDb32 => "IdaProDb32", - Self::IdaProDb64 => "IdaProDb64", - Self::Xcoff => "XCOFF", - Self::Macho => "MACHO", - Self::Raw => "RAW", - } - } - /// Creates an enum from field names used in the ProtoBuf definition. - pub fn from_str_name(value: &str) -> ::core::option::Option { - match value { - "Format_Undefined" => Some(Self::FormatUndefined), - "COFF" => Some(Self::Coff), - "ELF" => Some(Self::Elf), - "PE" => Some(Self::Pe), - "IdaProDb32" => Some(Self::IdaProDb32), - "IdaProDb64" => Some(Self::IdaProDb64), - "XCOFF" => Some(Self::Xcoff), - "MACHO" => Some(Self::Macho), - "RAW" => Some(Self::Raw), - _ => None, - } - } + FormatUndefined, + Coff, + Elf, + Pe, + IdaProDb32, + IdaProDb64, + Xcoff, + Macho, + Raw, } + #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] #[repr(i32)] pub enum Isa { - Undefined = 0, - Ia32 = 1, - Ppc32 = 2, - X64 = 3, - Arm = 4, - ValidButUnsupported = 5, - Ppc64 = 6, - Arm64 = 7, - Mips32 = 8, - Mips64 = 9, -} -impl Isa { - /// String value of the enum field names used in the ProtoBuf definition. - /// - /// The values are not transformed in any way and thus are considered stable - /// (if the ProtoBuf definition does not change) and safe for programmatic use. - pub fn as_str_name(&self) -> &'static str { - match self { - Self::Undefined => "ISA_Undefined", - Self::Ia32 => "IA32", - Self::Ppc32 => "PPC32", - Self::X64 => "X64", - Self::Arm => "ARM", - Self::ValidButUnsupported => "ValidButUnsupported", - Self::Ppc64 => "PPC64", - Self::Arm64 => "ARM64", - Self::Mips32 => "MIPS32", - Self::Mips64 => "MIPS64", - } - } - /// Creates an enum from field names used in the ProtoBuf definition. - pub fn from_str_name(value: &str) -> ::core::option::Option { - match value { - "ISA_Undefined" => Some(Self::Undefined), - "IA32" => Some(Self::Ia32), - "PPC32" => Some(Self::Ppc32), - "X64" => Some(Self::X64), - "ARM" => Some(Self::Arm), - "ValidButUnsupported" => Some(Self::ValidButUnsupported), - "PPC64" => Some(Self::Ppc64), - "ARM64" => Some(Self::Arm64), - "MIPS32" => Some(Self::Mips32), - "MIPS64" => Some(Self::Mips64), - _ => None, - } - } + Undefined, + Ia32, + Ppc32, + X64, + Arm, + ValidButUnsupported, + Ppc64, + Arm64, + Mips32, + Mips64, } + #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] #[repr(i32)] pub enum ByteOrder { - Undefined = 0, - BigEndian = 1, - LittleEndian = 2, -} -impl ByteOrder { - /// String value of the enum field names used in the ProtoBuf definition. - /// - /// The values are not transformed in any way and thus are considered stable - /// (if the ProtoBuf definition does not change) and safe for programmatic use. - pub fn as_str_name(&self) -> &'static str { - match self { - Self::Undefined => "ByteOrder_Undefined", - Self::BigEndian => "BigEndian", - Self::LittleEndian => "LittleEndian", - } - } - /// Creates an enum from field names used in the ProtoBuf definition. - pub fn from_str_name(value: &str) -> ::core::option::Option { - match value { - "ByteOrder_Undefined" => Some(Self::Undefined), - "BigEndian" => Some(Self::BigEndian), - "LittleEndian" => Some(Self::LittleEndian), - _ => None, - } - } + Undefined, + BigEndian, + LittleEndian, } diff --git a/src/node.rs b/src/node.rs index 12b405e..c82700a 100644 --- a/src/node.rs +++ b/src/node.rs @@ -3,8 +3,7 @@ use serde::{Deserialize, Serialize}; use uuid::Uuid; -/// Parent trait for all GTIRB components. -/// Should be implemented by all components. +/// Parent trait for many GTIRB components. pub trait Node { type UUID: Eq + Default; diff --git a/src/section.rs b/src/section.rs index 6e1331f..e56330d 100644 --- a/src/section.rs +++ b/src/section.rs @@ -1,51 +1,23 @@ +use crate::byte_interval::ByteInterval; + +use uuid::Uuid; + #[derive(Clone, PartialEq)] pub struct Section { - pub uuid: Vec, - + pub uuid: Uuid, pub name: String, - pub byte_intervals: Vec, - - pub section_flags: Vec, + pub section_flags: Vec, } + #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] #[repr(i32)] pub enum SectionFlag { - SectionUndefined = 0, - Readable = 1, - Writable = 2, - Executable = 3, - Loaded = 4, - Initialized = 5, - ThreadLocal = 6, -} -impl SectionFlag { - /// String value of the enum field names used in the ProtoBuf definition. - /// - /// The values are not transformed in any way and thus are considered stable - /// (if the ProtoBuf definition does not change) and safe for programmatic use. - pub fn as_str_name(&self) -> &'static str { - match self { - Self::SectionUndefined => "Section_Undefined", - Self::Readable => "Readable", - Self::Writable => "Writable", - Self::Executable => "Executable", - Self::Loaded => "Loaded", - Self::Initialized => "Initialized", - Self::ThreadLocal => "ThreadLocal", - } - } - /// Creates an enum from field names used in the ProtoBuf definition. - pub fn from_str_name(value: &str) -> ::core::option::Option { - match value { - "Section_Undefined" => Some(Self::SectionUndefined), - "Readable" => Some(Self::Readable), - "Writable" => Some(Self::Writable), - "Executable" => Some(Self::Executable), - "Loaded" => Some(Self::Loaded), - "Initialized" => Some(Self::Initialized), - "ThreadLocal" => Some(Self::ThreadLocal), - _ => None, - } - } + Undefined, + Readable, + Writable, + Executable, + Loaded, + Initialized, + ThreadLocal, } diff --git a/src/symbol.rs b/src/symbol.rs index 35c0a96..b5b91de 100644 --- a/src/symbol.rs +++ b/src/symbol.rs @@ -1,19 +1,15 @@ +use uuid::Uuid; + #[derive(Clone, PartialEq)] pub struct Symbol { - pub uuid: Vec, - + pub uuid: Uuid, pub name: String, - pub at_end: bool, - - pub optional_payload: ::core::option::Option, + pub optional_payload: Option, } -/// Nested message and enum types in `Symbol`. -pub mod symbol { - #[derive(Clone, PartialEq)] - pub enum OptionalPayload { - Value(u64), - ReferentUuid(Vec), - } +#[derive(Clone, PartialEq)] +pub enum OptionalPayload { + Value(u64), + ReferentUuid(Vec), } diff --git a/src/symbolic_expression.rs b/src/symbolic_expression.rs index cc9b4cd..ac5b082 100644 --- a/src/symbolic_expression.rs +++ b/src/symbolic_expression.rs @@ -1,40 +1,37 @@ +use uuid::Uuid; + #[derive(Clone, PartialEq)] pub struct SymStackConst { pub offset: i32, - - pub symbol_uuid: Vec, + pub symbol_uuid: Uuid, } + #[derive(Clone, PartialEq)] pub struct SymAddrConst { pub offset: i64, - - pub symbol_uuid: Vec, + pub symbol_uuid: Uuid, } + #[derive(Clone, PartialEq)] pub struct SymAddrAddr { pub scale: i64, - pub offset: i64, - - pub symbol1_uuid: Vec, - - pub symbol2_uuid: Vec, + pub symbol1_uuid: Uuid, + pub symbol2_uuid: Uuid, } + #[derive(Clone, PartialEq)] pub struct SymbolicExpression { pub attribute_flags: Vec, - - pub value: ::core::option::Option, + pub value: Option, } -/// Nested message and enum types in `SymbolicExpression`. -pub mod symbolic_expression { - #[derive(Clone, PartialEq)] - pub enum Value { - AddrConst(super::SymAddrConst), - AddrAddr(super::SymAddrAddr), - } +#[derive(Clone, PartialEq)] +pub enum SymbolicExpressionValue { + AddrConst(SymAddrConst), + AddrAddr(SymAddrAddr), } + /// NOTE: /// We do not generalize or otherwise unify relocation attributes across /// architectures and instead prefer an explicit mapping of attributes names @@ -113,149 +110,3 @@ pub enum SymAttribute { Toc = 4008, Notoc = 4009, } -impl SymAttribute { - /// String value of the enum field names used in the ProtoBuf definition. - /// - /// The values are not transformed in any way and thus are considered stable - /// (if the ProtoBuf definition does not change) and safe for programmatic use. - pub fn as_str_name(&self) -> &'static str { - match self { - Self::Got => "GOT", - Self::Gotpc => "GOTPC", - Self::Gotoff => "GOTOFF", - Self::Gotrel => "GOTREL", - Self::Plt => "PLT", - Self::Pltoff => "PLTOFF", - Self::Pcrel => "PCREL", - Self::Secrel => "SECREL", - Self::Tls => "TLS", - Self::Tlsgd => "TLSGD", - Self::Tlsld => "TLSLD", - Self::Tlsldm => "TLSLDM", - Self::Tlscall => "TLSCALL", - Self::Tlsdesc => "TLSDESC", - Self::Tprel => "TPREL", - Self::Tpoff => "TPOFF", - Self::Dtprel => "DTPREL", - Self::Dtpoff => "DTPOFF", - Self::Ntpoff => "NTPOFF", - Self::Dtpmod => "DTPMOD", - Self::Page => "PAGE", - Self::Pageoff => "PAGEOFF", - Self::Call => "CALL", - Self::Lo => "LO", - Self::Hi => "HI", - Self::Higher => "HIGHER", - Self::Highest => "HIGHEST", - Self::Gotntpoff => "GOTNTPOFF", - Self::Indntpoff => "INDNTPOFF", - Self::G0 => "G0", - Self::G1 => "G1", - Self::G2 => "G2", - Self::G3 => "G3", - Self::Upper16 => "UPPER16", - Self::Lower16 => "LOWER16", - Self::Lo12 => "LO12", - Self::Lo15 => "LO15", - Self::Lo14 => "LO14", - Self::Hi12 => "HI12", - Self::Hi21 => "HI21", - Self::S => "S", - Self::Pg => "PG", - Self::Nc => "NC", - Self::Abs => "ABS", - Self::Prel => "PREL", - Self::Prel31 => "PREL31", - Self::Target1 => "TARGET1", - Self::Target2 => "TARGET2", - Self::Sbrel => "SBREL", - Self::Tlsldo => "TLSLDO", - Self::Hi16 => "HI16", - Self::Lo16 => "LO16", - Self::Gprel => "GPREL", - Self::Disp => "DISP", - Self::Ofst => "OFST", - Self::H => "H", - Self::L => "L", - Self::Ha => "HA", - Self::High => "HIGH", - Self::Higha => "HIGHA", - Self::Highera => "HIGHERA", - Self::Highesta => "HIGHESTA", - Self::Tocbase => "TOCBASE", - Self::Toc => "TOC", - Self::Notoc => "NOTOC", - } - } - /// Creates an enum from field names used in the ProtoBuf definition. - pub fn from_str_name(value: &str) -> ::core::option::Option { - match value { - "GOT" => Some(Self::Got), - "GOTPC" => Some(Self::Gotpc), - "GOTOFF" => Some(Self::Gotoff), - "GOTREL" => Some(Self::Gotrel), - "PLT" => Some(Self::Plt), - "PLTOFF" => Some(Self::Pltoff), - "PCREL" => Some(Self::Pcrel), - "SECREL" => Some(Self::Secrel), - "TLS" => Some(Self::Tls), - "TLSGD" => Some(Self::Tlsgd), - "TLSLD" => Some(Self::Tlsld), - "TLSLDM" => Some(Self::Tlsldm), - "TLSCALL" => Some(Self::Tlscall), - "TLSDESC" => Some(Self::Tlsdesc), - "TPREL" => Some(Self::Tprel), - "TPOFF" => Some(Self::Tpoff), - "DTPREL" => Some(Self::Dtprel), - "DTPOFF" => Some(Self::Dtpoff), - "NTPOFF" => Some(Self::Ntpoff), - "DTPMOD" => Some(Self::Dtpmod), - "PAGE" => Some(Self::Page), - "PAGEOFF" => Some(Self::Pageoff), - "CALL" => Some(Self::Call), - "LO" => Some(Self::Lo), - "HI" => Some(Self::Hi), - "HIGHER" => Some(Self::Higher), - "HIGHEST" => Some(Self::Highest), - "GOTNTPOFF" => Some(Self::Gotntpoff), - "INDNTPOFF" => Some(Self::Indntpoff), - "G0" => Some(Self::G0), - "G1" => Some(Self::G1), - "G2" => Some(Self::G2), - "G3" => Some(Self::G3), - "UPPER16" => Some(Self::Upper16), - "LOWER16" => Some(Self::Lower16), - "LO12" => Some(Self::Lo12), - "LO15" => Some(Self::Lo15), - "LO14" => Some(Self::Lo14), - "HI12" => Some(Self::Hi12), - "HI21" => Some(Self::Hi21), - "S" => Some(Self::S), - "PG" => Some(Self::Pg), - "NC" => Some(Self::Nc), - "ABS" => Some(Self::Abs), - "PREL" => Some(Self::Prel), - "PREL31" => Some(Self::Prel31), - "TARGET1" => Some(Self::Target1), - "TARGET2" => Some(Self::Target2), - "SBREL" => Some(Self::Sbrel), - "TLSLDO" => Some(Self::Tlsldo), - "HI16" => Some(Self::Hi16), - "LO16" => Some(Self::Lo16), - "GPREL" => Some(Self::Gprel), - "DISP" => Some(Self::Disp), - "OFST" => Some(Self::Ofst), - "H" => Some(Self::H), - "L" => Some(Self::L), - "HA" => Some(Self::Ha), - "HIGH" => Some(Self::High), - "HIGHA" => Some(Self::Higha), - "HIGHERA" => Some(Self::Highera), - "HIGHESTA" => Some(Self::Highesta), - "TOCBASE" => Some(Self::Tocbase), - "TOC" => Some(Self::Toc), - "NOTOC" => Some(Self::Notoc), - _ => None, - } - } -}