Skip to content

Commit 9ac4a10

Browse files
committed
Oh god
1 parent 22fa403 commit 9ac4a10

File tree

9 files changed

+107
-10
lines changed

9 files changed

+107
-10
lines changed

jingle/src/modeling/state/space.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ impl<'ctx> ModeledSpace<'ctx> {
2525
/// Create a new modeling space with the given z3 context, using the provided space metadata
2626
pub(crate) fn new(jingle: &JingleContext<'ctx>, space_info: &SpaceInfo) -> Self {
2727
let domain = Sort::bitvector(jingle.z3, space_info.index_size_bytes * 8);
28+
dbg!(&domain);
2829
let range = Sort::bitvector(jingle.z3, space_info.word_size_bytes * 8);
30+
dbg!(&jingle.z3);
31+
dbg!(&range);
2932
Self {
3033
endianness: space_info.endianness,
3134
data: Array::fresh_const(jingle.z3, &space_info.name, &domain, &range),

jingle_python/Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,9 @@ crate-type = ["cdylib"]
1010

1111
[dependencies]
1212
pyo3 = "0.24.0"
13-
jingle = {path = "../jingle", features = ["pyo3", "gimli"]}
13+
jingle = {path = "../jingle", features = ["pyo3", "gimli"]}
14+
z3 = { git = "https://github.com/prove-rs/z3.rs.git", branch = "master" }
15+
z3-sys = { git = "https://github.com/prove-rs/z3.rs.git", branch = "master" }
16+
17+
[build-dependencies]
18+
pkg-config = "0.3.32"

jingle_python/build.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
fn main() {
3+
let var = std::env::var("Z3_PYTHON_LIB").unwrap_or_else(|_| "/Users/maroed/RustroverProjects/jingle/jingle_python/.venv/lib/python3.13/site-packages/z3/lib/".to_string());
4+
// Get the directory where Python's Z3 library is located
5+
let z3_python_lib = std::path::Path::new(&var);
6+
7+
// Set the environment variable for Rust to use the same library
8+
println!("cargo:rerun-if-changed=build.rs");
9+
println!("cargo:libdir={}", z3_python_lib.display());
10+
11+
// Optionally, set the rpath for dynamic libraries
12+
println!("cargo:rustc-link-search=native={}", z3_python_lib.display());
13+
}

jingle_python/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ classifiers = [
1212
]
1313
dynamic = ["version"]
1414
dependencies = [
15-
"maturin>=1.8.3",
15+
"z3-solver==4.12.4.0",
1616
]
1717
[tool.maturin]
1818
features = ["pyo3/extension-module"]

jingle_python/script.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import z3
2+
from jingle import *
3+
sleigh = create_sleigh_context("/Users/maroed/RustroverProjects/code_reuse_synthesis_artifacts/crackers/libz.so.1", "/Applications/ghidra")
4+
sleigh.make_jingle_context()
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use crate::context_switcheroo;
2+
use jingle::sleigh::ArchInfoProvider;
3+
use jingle::JingleContext;
4+
use pyo3::prelude::*;
5+
use z3_sys::Z3_context;
6+
7+
#[pyclass(unsendable)]
8+
pub struct PythonJingleContext {
9+
#[allow(unused)]
10+
context: JingleContext<'static>,
11+
}
12+
13+
impl PythonJingleContext {
14+
pub fn make_jingle_context<T: ArchInfoProvider>(i: &T) -> PyResult<PythonJingleContext> {
15+
Python::with_gil(|py| {
16+
let z3_mod = PyModule::import(py, "z3")?;
17+
let global_ctx = z3_mod.getattr("main_ctx")?.call0()?;
18+
let z3_ptr: usize = global_ctx
19+
.getattr("ref")?
20+
.call0()?
21+
.getattr("value")?
22+
.extract()?;
23+
println!("z3_ptr: {:x}", z3_ptr);
24+
let raw_ctx: Z3_context = z3_ptr as Z3_context;
25+
let ctx = context_switcheroo( raw_ctx );
26+
let ctx = JingleContext::new(ctx, i);
27+
ctx.fresh_state();
28+
Ok(PythonJingleContext { context: ctx })
29+
})
30+
}
31+
}

jingle_python/src/lib.rs

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,39 @@
1-
mod sleigh_context;
21
mod instruction;
2+
mod jingle_context;
3+
mod sleigh_context;
34

4-
use pyo3::prelude::*;
5-
use ::jingle::sleigh::{IndirectVarNode, PcodeOperation, VarNode};
6-
use ::jingle::sleigh::Instruction;
7-
use sleigh_context::create_sleigh_context;
85
use crate::instruction::PythonInstruction;
96
use crate::sleigh_context::LoadedSleighContextWrapper;
7+
use ::jingle::sleigh::{IndirectVarNode, PcodeOperation, VarNode};
8+
use pyo3::prelude::*;
9+
use sleigh_context::create_sleigh_context;
10+
use std::cell::RefCell;
11+
use std::ffi::CString;
12+
use std::mem;
13+
use z3::Context;
14+
use z3_sys::Z3_context;
1015

16+
thread_local! {
17+
pub static CONTEXT: RefCell<Z3_context> = RefCell::new(std::ptr::null_mut());
18+
}
19+
20+
thread_local! {
21+
pub static CTX_REF: &'static Context = CONTEXT.with_borrow(|ctx| unsafe {
22+
mem::transmute(ctx)
23+
});
24+
}
25+
pub fn context_switcheroo(z3: Z3_context) -> &'static Context {
26+
CONTEXT.replace(z3);
27+
CTX_REF.with(|ctx| {
28+
println!("{:p}", *ctx);
29+
dbg!(*ctx)
30+
})
31+
}
1132

1233
/// A Python module implemented in Rust.
1334
#[pymodule]
1435
fn jingle(m: &Bound<'_, PyModule>) -> PyResult<()> {
36+
m.py().run(&CString::new("import z3")?, None, None)?;
1537
m.add_class::<VarNode>()?;
1638
m.add_class::<IndirectVarNode>()?;
1739
m.add_class::<PcodeOperation>()?;
@@ -20,3 +42,19 @@ fn jingle(m: &Bound<'_, PyModule>) -> PyResult<()> {
2042
m.add_function(wrap_pyfunction!(create_sleigh_context, m)?)?;
2143
Ok(())
2244
}
45+
46+
#[cfg(test)]
47+
mod tests {
48+
use crate::sleigh_context::{create_sleigh_context, LoadedSleighContextWrapper};
49+
50+
#[test]
51+
fn ctx() {
52+
pyo3::prepare_freethreaded_python();
53+
let ctx = create_sleigh_context(
54+
"/Users/maroed/RustroverProjects/code_reuse_synthesis_artifacts/crackers/libz.so.1",
55+
"/Applications/ghidra",
56+
)
57+
.unwrap();
58+
ctx.make_jingle_context().unwrap();
59+
}
60+
}

jingle_python/src/sleigh_context.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
use std::os::unix::raw::off_t;
21
use jingle::sleigh::context::image::gimli::load_with_gimli;
32
use jingle::sleigh::context::loaded::LoadedSleighContext;
4-
use jingle::sleigh::Instruction;
53
use pyo3::{pyclass, pyfunction, pymethods, PyResult};
64
use crate::instruction::PythonInstruction;
5+
use crate::jingle_context::PythonJingleContext;
76

87
#[pyfunction]
98
pub fn create_sleigh_context(
@@ -34,4 +33,8 @@ impl LoadedSleighContextWrapper {
3433
pub fn get_base_address(&mut self) -> u64 {
3534
self.context.get_base_address()
3635
}
36+
37+
pub fn make_jingle_context(&self) -> PyResult<PythonJingleContext> {
38+
PythonJingleContext::make_jingle_context(&self.context)
39+
}
3740
}

jingle_sleigh/ghidra

Submodule ghidra updated 4956 files

0 commit comments

Comments
 (0)