Skip to content

Commit

Permalink
Refactor crate for use as a library
Browse files Browse the repository at this point in the history
  • Loading branch information
emesare committed Oct 24, 2024
1 parent 14bd9dd commit aa37c92
Show file tree
Hide file tree
Showing 70 changed files with 529 additions and 3,693 deletions.
4 changes: 0 additions & 4 deletions .cargo/config.toml

This file was deleted.

40 changes: 24 additions & 16 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
[workspace]
resolver = "2"
members = [
"fbcg_rust",
"sigem",
"signaturebuild",
"sigview",
"symbolbuild",
"typebuild",
"warp_binja"
]
[package]
name = "warp"
version = "0.1.0"
edition = "2021"
license = "Apache-2.0"

[workspace.dependencies]
#binaryninja = { path = "../../binaryninja/api/rust", features = ["rayon"] }
# NOTE: You must update the revision manually when making changes in the `binaryninja` crate.
binaryninja = { git = "https://github.com/Vector35/binaryninja-api.git", rev = "bca07d7", features = ["rayon"] }
[lib]
path = "rust/lib.rs"

[dependencies]
flatbuffers = "24.3.25"
bon = "2.3.0"
uuid = { version = "1.11.0", features = ["v5"]}
rand = "0.8.5"

[dev-dependencies]
criterion = "0.5.1"

[build-dependencies]
flatbuffers-build = { git = "https://github.com/emesare/flatbuffers-build" }

[profile.release]
panic = "abort"
lto = true
debug = "full"
debug = "full"

[[example]]
name = "simple"
path = "rust/examples/simple.rs"
13 changes: 13 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright 2020-2024 Vector 35 Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
4 changes: 2 additions & 2 deletions fbcg_rust/build.rs → build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ pub fn main() {

// Remove leftover symlink dir.
let _ = std::fs::remove_dir_all("src/gen_flatbuffers");
let workspace_dir: PathBuf = std::env::var("CARGO_WORKSPACE_DIR").unwrap().into();
let workspace_dir: PathBuf = std::env::var("CARGO_MANIFEST_DIR").unwrap().into();
BuilderOptions::new_with_files([
workspace_dir.join("type.fbs"),
workspace_dir.join("symbol.fbs"),
workspace_dir.join("signature.fbs"),
])
.set_symlink_directory("src/gen_flatbuffers")
.set_symlink_directory("rust/gen_flatbuffers")
.compile()
.expect("flatbuffer compilation failed");
}
10 changes: 0 additions & 10 deletions fbcg_rust/Cargo.toml

This file was deleted.

20 changes: 0 additions & 20 deletions pyproject.toml

This file was deleted.

File renamed without changes.
7 changes: 6 additions & 1 deletion typebuild/examples/simple.rs → rust/examples/simple.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use typebuild::prelude::*;
use warp::r#type::class::{
ArrayClass, BooleanClass, EnumerationClass, EnumerationMember, FunctionClass, FunctionMember,
IntegerClass, PointerClass, StructureClass, StructureMember, TypeClass, UnionClass,
UnionMember,
};
use warp::r#type::Type;

fn main() {
let void_type = Type::builder()
Expand Down
12 changes: 8 additions & 4 deletions fbcg_rust/src/lib.rs → rust/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
pub mod signature;
pub mod symbol;
pub mod r#type;

#[allow(warnings)]
#[rustfmt::skip]
pub mod gen_flatbuffers;
mod gen_flatbuffers;

pub use gen_flatbuffers::sig_bin as fb_sig;
pub use gen_flatbuffers::symbol_bin as fb_symbol;
pub use gen_flatbuffers::type_bin as fb_type;
use gen_flatbuffers::sig_bin as fb_sig;
use gen_flatbuffers::symbol_bin as fb_symbol;
use gen_flatbuffers::type_bin as fb_type;

impl From<u16> for gen_flatbuffers::type_bin::BitWidth {
fn from(value: u16) -> Self {
Expand Down
14 changes: 3 additions & 11 deletions signaturebuild/src/lib.rs → rust/signature.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
use crate::function::Function;
use fbcg_rust::fb_sig as fb;
use crate::fb_sig as fb;
use crate::r#type::ComputedType;
use crate::signature::function::Function;
use flatbuffers::{FlatBufferBuilder, WIPOffset};
use typebuild::prelude::ComputedType;

pub mod basic_block;
pub mod function;

pub mod prelude {
pub use crate::{
basic_block::{BasicBlock, BasicBlockGUID},
function::{constraints::FunctionConstraints, Function, FunctionGUID},
Data,
};
}

#[derive(Clone, Debug, Eq, PartialEq, Default)]
pub struct Data {
pub functions: Vec<Function>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use fbcg_rust::fb_sig as fb;
use crate::fb_sig as fb;
use flatbuffers::{FlatBufferBuilder, WIPOffset};
use std::fmt::{Display, Formatter};
use std::str::FromStr;
Expand Down
61 changes: 22 additions & 39 deletions signaturebuild/src/function.rs → rust/signature/function.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use crate::basic_block::BasicBlock;
use crate::prelude::{BasicBlockGUID, FunctionConstraints};
use fbcg_rust::fb_sig as fb;
use crate::fb_sig as fb;
use crate::r#type::Type;
use crate::signature::basic_block::{BasicBlock, BasicBlockGUID};
use crate::signature::function::constraints::FunctionConstraints;
use crate::symbol::Symbol;
use flatbuffers::{FlatBufferBuilder, WIPOffset};
use std::fmt::{Display, Formatter};
use std::str::FromStr;
use symbolbuild::{Create, Symbol};
use typebuild::prelude::Type;
use typebuild::Build;
use uuid::{uuid, Uuid};

pub mod constraints;
Expand Down Expand Up @@ -126,9 +125,13 @@ impl From<fb::Function<'_>> for Function {

#[cfg(test)]
mod tests {
use crate::prelude::*;
use symbolbuild::prelude::*;
use typebuild::prelude::*;
use crate::r#type::class::TypeClass;
use crate::r#type::Type;
use crate::signature::function::constraints::FunctionConstraints;
use crate::signature::function::{Function, FunctionGUID};
use crate::signature::Data;
use crate::symbol::class::SymbolClass;
use crate::symbol::{Symbol, SymbolModifiers};
use uuid::{uuid, Uuid};

const EMPTY_FN_UUID: Uuid = uuid!("db867a3e-416a-5d7f-aa6d-b8ae6be36da2");
Expand All @@ -142,18 +145,22 @@ mod tests {
assert_eq!(FunctionGUID::from(EMPTY_FN_UUID), empty_fn_guid());
}

#[test]
fn test_data_from_bytes() {
let signature = Function {
fn empty_function() -> Function {
Function {
guid: empty_fn_guid(),
symbol: Symbol::new("test", SymbolClass::Data, vec![]),
symbol: Symbol::new("test", SymbolClass::Data, SymbolModifiers::empty()),
ty: Type::builder()
.name("aghhgh")
.class(TypeClass::Void)
.build(),
constraints: FunctionConstraints::default(),
entry: None,
};
}
}

#[test]
fn test_data_from_bytes() {
let signature = empty_function();
let data = Data {
functions: vec![signature.clone()],
types: vec![],
Expand All @@ -165,33 +172,9 @@ mod tests {

#[test]
fn test_function_from_bytes() {
let signature = Function {
guid: empty_fn_guid(),
symbol: Symbol::new("test", SymbolClass::Data, vec![]),
ty: Type::builder()
.name("aghhgh")
.class(TypeClass::Void)
.build(),
constraints: FunctionConstraints::default(),
entry: None,
};
let signature = empty_function();
let bytes = signature.to_bytes();
let from_bytes_sig = Function::from_bytes(&bytes).unwrap();
assert_eq!(signature, from_bytes_sig)
}

#[test]
fn it_works() {
let signature = Function {
guid: empty_fn_guid(),
symbol: Symbol::new("test", SymbolClass::Data, vec![]),
ty: Type::builder()
.name("aghhgh")
.class(TypeClass::Void)
.build(),
constraints: FunctionConstraints::default(),
entry: None,
};
dbg!(&signature);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::function::FunctionGUID;
use fbcg_rust::fb_sig as fb;
use crate::fb_sig as fb;
use crate::signature::function::FunctionGUID;
use crate::symbol::Symbol;
use flatbuffers::{FlatBufferBuilder, WIPOffset};
use std::collections::HashSet;
use symbolbuild::{Create, Symbol};

#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct FunctionConstraint {
Expand Down
83 changes: 83 additions & 0 deletions rust/symbol.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
pub mod class;

use crate::fb_symbol as fb;
use crate::symbol::class::SymbolClass;
use flatbuffers::{FlatBufferBuilder, WIPOffset};

pub use fb::SymbolModifiers;

#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct Symbol {
pub name: String,
pub modifiers: SymbolModifiers,
pub class: SymbolClass,
}

impl Symbol {
pub fn new(name: impl Into<String>, class: SymbolClass, modifiers: SymbolModifiers) -> Self {
Self {
name: name.into(),
modifiers,
class,
}
}

pub(crate) fn create<'a>(
&self,
builder: &mut FlatBufferBuilder<'a>,
) -> WIPOffset<fb::Symbol<'a>> {
let name = builder.create_string(&self.name);
let class_type = self.class.ty();
let class = self.class.create(builder);

fb::Symbol::create(
builder,
&fb::SymbolArgs {
name: Some(name),
modifiers: self.modifiers,
class_type,
class: Some(class),
},
)
}
}

impl From<fb::Symbol<'_>> for Symbol {
fn from(value: fb::Symbol<'_>) -> Self {
let name = value.name().unwrap().to_string();
// TODO: I would like this conversion to be on `SymbolClass` instead.
let class = match value.class_type() {
fb::SymbolClass::FunctionSymbolClass => {
SymbolClass::from(value.class_as_function_symbol_class().unwrap())
}
fb::SymbolClass::DataSymbolClass => {
SymbolClass::from(value.class_as_data_symbol_class().unwrap())
}
_ => unreachable!(),
};

Self {
name,
modifiers: value.modifiers(),
class,
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use flatbuffers::FlatBufferBuilder;

#[test]
fn it_works() {
let mut builder = FlatBufferBuilder::with_capacity(100);
let symbol = Symbol {
name: "".to_string(),
modifiers: SymbolModifiers::empty(),
class: SymbolClass::Data,
};
let created_symbol = symbol.create(&mut builder);

Check failure on line 80 in rust/symbol.rs

View workflow job for this annotation

GitHub Actions / cargo test

unused variable: `created_symbol`
// TODO: Add actual tests.
}
}
Loading

0 comments on commit aa37c92

Please sign in to comment.