Skip to content

Commit

Permalink
Initialize
Browse files Browse the repository at this point in the history
  • Loading branch information
emesare committed Oct 22, 2024
0 parents commit 64aa057
Show file tree
Hide file tree
Showing 71 changed files with 6,682 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[env]
# HACK: To get the workspace directory. (See: https://github.com/rust-lang/cargo/issues/3946#issuecomment-973132993)
# TODO: If we had &[u8] api in flatbuffers-build we could include the file with relative path like that.
CARGO_WORKSPACE_DIR = { value = "", relative = true }
61 changes: 61 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Binaries for programs and dependencies
/target/
**/target/

# Remove Cargo.lock from gitignore if it's already checked in
Cargo.lock

# Best practice for something like this, the python scripts are not meant to be "deployed" from this repo state.
poetry.lock

gen_flatbuffers

# These are backup files generated by rustfmt
**/*.rs.bk

# Temporary files generated by editors
*~
*.swp
*.swo

# IntelliJ and CLion directories
.idea/
*.iml
*.iws
*.ipr

# MacOS specific ignores
.DS_Store
._*

# Rust documentations
/target/doc/

# Build files
**/build/

# Generated files and directories
generated/

# Node.js directories (if any)
/node_modules/

# Logs
*.log

# Output directories
*.out
*.log.*

**/[Tt]humbs.db
**/[Dd]esktop.ini

**/ubuntu
**/downloads
**/msvc
**/windows

**/out

*.vsix
*.deb
16 changes: 16 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[workspace]
resolver = "2"
members = [
"fbcg_rust",
"sigem",
"signaturebuild",
"sigview",
"symbolbuild",
"typebuild",
"warp_binja"
]

[profile.release]
panic = "abort"
lto = true
debug = "full"
10 changes: 10 additions & 0 deletions fbcg_rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "fbcg_rust"
version = "0.1.0"
edition = "2021"

[dependencies]
flatbuffers = "24.3.25"

[build-dependencies]
flatbuffers-build = "0.2.0"
16 changes: 16 additions & 0 deletions fbcg_rust/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use flatbuffers_build::BuilderOptions;
use std::path::PathBuf;

pub fn main() {
// Remove leftover symlink dir.
let _ = std::fs::remove_dir_all("src/gen_flatbuffers");
let workspace_dir: PathBuf = env!("CARGO_WORKSPACE_DIR").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")
.compile()
.expect("flatbuffer compilation failed");
}
66 changes: 66 additions & 0 deletions fbcg_rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#[allow(warnings)]
pub 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;

impl From<u16> for gen_flatbuffers::type_bin::BitWidth {
fn from(value: u16) -> Self {
Self::new(value)
}
}

impl From<&gen_flatbuffers::type_bin::BitWidth> for u16 {
fn from(value: &gen_flatbuffers::type_bin::BitWidth) -> Self {
value.value()
}
}

impl From<u64> for gen_flatbuffers::type_bin::BitSize {
fn from(value: u64) -> Self {
Self::new(value)
}
}

impl From<&gen_flatbuffers::type_bin::BitSize> for u64 {
fn from(value: &gen_flatbuffers::type_bin::BitSize) -> Self {
value.value()
}
}

impl From<u64> for gen_flatbuffers::type_bin::UnsignedBitOffset {
fn from(value: u64) -> Self {
Self::new(value)
}
}

impl From<&gen_flatbuffers::type_bin::UnsignedBitOffset> for u64 {
fn from(value: &gen_flatbuffers::type_bin::UnsignedBitOffset) -> Self {
value.value()
}
}

impl From<i64> for gen_flatbuffers::type_bin::BitOffset {
fn from(value: i64) -> Self {
Self::new(value)
}
}

impl From<&gen_flatbuffers::type_bin::BitOffset> for i64 {
fn from(value: &gen_flatbuffers::type_bin::BitOffset) -> Self {
value.value()
}
}

impl From<i64> for gen_flatbuffers::type_bin::BitShift {
fn from(value: i64) -> Self {
Self::new(value)
}
}

impl From<&gen_flatbuffers::type_bin::BitShift> for i64 {
fn from(value: &gen_flatbuffers::type_bin::BitShift) -> Self {
value.value()
}
}
20 changes: 20 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[tool.poetry]
name = "typebin"
version = "0.1.0"
description = ""
authors = ["Mason Reed <[email protected]>"]
readme = "README.md"

[tool.poetry.dependencies]
python = "^3.9"
aiohttp = "^3.10.8"
beautifulsoup4 = "^4.12.3"
ar = "^1.0.0"
zstandard = "^0.23.0"
xtarfile = "^0.2.1"


[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

21 changes: 21 additions & 0 deletions sigem/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "sigem"
version = "0.1.0"
edition = "2021"

[dependencies]
binaryninja = { path = "../../binaryninja/api/rust", features = ["rayon"] }
binaryninjacore-sys = { path = "../../binaryninja/api/rust/binaryninjacore-sys" }
env_logger = "0.11.5"
log = "0.4"
signaturebuild = { path = "../signaturebuild" }
warp_binja = { path = "../warp_binja" }
clap = { version = "4.5.16", features = ["derive"] }
rayon = "1.10.0"
ar = { git = "https://github.com/mdsteele/rust-ar" }
tempdir = "0.3.7"
serde_json = "1.0.132"


[build-dependencies]
cc = "1.1.28"
68 changes: 68 additions & 0 deletions sigem/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use std::path::PathBuf;
use std::process::Command;

fn compile_rust(file: PathBuf) -> bool {
let out_dir = std::env::var_os("OUT_DIR").unwrap();
let rustc = std::env::var_os("RUSTC").unwrap();
let rustc = rustc.to_str().unwrap();
let mut rustc = rustc.split('\x1f');
let mut cmd = Command::new(rustc.next().unwrap());
cmd.args(rustc)
.arg("--crate-type=rlib")
.arg("--out-dir")
.arg(out_dir)
.arg(file);
cmd.status().expect("failed to invoke rustc").success()
}

fn main() {
let link_path =
std::env::var_os("DEP_BINARYNINJACORE_PATH").expect("DEP_BINARYNINJACORE_PATH specified");
let out_dir = std::env::var_os("OUT_DIR").expect("OUT_DIR specified");
let out_dir_path = PathBuf::from(out_dir);

println!("cargo::rustc-link-lib=dylib=binaryninjacore");
println!("cargo::rustc-link-search={}", link_path.to_str().unwrap());

#[cfg(not(target_os = "windows"))]
{
println!(
"cargo::rustc-link-arg=-Wl,-rpath,{0},-L{0}",
link_path.to_string_lossy()
);
}

// Copy all binaries to OUT_DIR for unit tests.
let bin_dir: PathBuf = "fixtures/bin".into();
if let Ok(entries) = std::fs::read_dir(bin_dir) {
for entry in entries {
let entry = entry.unwrap();
let path = entry.path();
if path.is_file() {
let file_name = path.file_name().unwrap();
let dest_path = out_dir_path.join(file_name);
std::fs::copy(&path, &dest_path).expect("failed to copy binary to OUT_DIR");
}
}
}

// Compile all .c files in fixtures/src directory for unit tests.
let src_dir: PathBuf = "fixtures/src".into();
if let Ok(entries) = std::fs::read_dir(src_dir) {
for entry in entries {
let entry = entry.unwrap();
let path = entry.path();
match path.extension().map(|s| s.to_str().unwrap()) {
Some("c") => {
cc::Build::new()
.file(&path)
.compile(path.file_stem().unwrap().to_str().unwrap());
}
Some("rs") => {
compile_rust(path);
}
_ => {}
}
}
}
}
Binary file added sigem/fixtures/bin/librustlibrary.rlib
Binary file not shown.
33 changes: 33 additions & 0 deletions sigem/fixtures/src/library.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <stdio.h>

#include "library.h"

int myFunction(int x)
{
printf("%d\n", x);
return x;
}

int recursiveFunc(int x);
int otherFunction(int x)
{
x += 5;
if (x < 10) return otherFunction(x);
return recursiveFunc(x);
}

int recursiveFunc(int x)
{
if (x <= 0) return 0;
return x + otherFunction(x - 1);
}

struct MyStruct myFunction2(int x)
{
printf("MyStruct %d\n", x);
struct MyStruct myStruct;
myStruct.a = recursiveFunc(x);
myStruct.b = x * 10;
myStruct.c = "my struct";
return myStruct;
}
11 changes: 11 additions & 0 deletions sigem/fixtures/src/library.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

struct MyStruct {
int a;
int b;
const char* c;
struct MyStruct* d;
};

int myFunction(int x);
struct MyStruct myFunction2(int x);
22 changes: 22 additions & 0 deletions sigem/fixtures/src/library.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#[derive(Debug, Hash, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub struct MyStruct {
a: i32,
b: u32,
c: *mut MyStruct,
}

impl MyStruct {
pub fn new(a: i32, b: u32, c: *mut MyStruct) -> MyStruct {
MyStruct { a, b, c }
}

pub fn hello(&self) {
println!("hello from MyStruct! a: {} b: {}", self.a, self.b);
}
}

pub fn main() {
let my_struct = MyStruct::new(1, 2, 0xfffff as *mut MyStruct);
my_struct.hello();
println!("{:#?}", my_struct);
}
15 changes: 15 additions & 0 deletions sigem/fixtures/src/simple.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <stdio.h>

#include "library.h"

int simple() {
printf("This is main!\n");

printf("calling myFunction\n");
int returnVal = myFunction(55);

printf("calling myFunction2\n");
struct MyStruct myStruct = myFunction2(returnVal);

return myStruct.b;
}
Loading

0 comments on commit 64aa057

Please sign in to comment.