Skip to content

Commit

Permalink
investigate
Browse files Browse the repository at this point in the history
  • Loading branch information
edg-l committed Nov 29, 2023
1 parent 6a729ed commit 7a5a006
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 12 deletions.
2 changes: 1 addition & 1 deletion examples/aot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct SyscallHandler;

pub fn main() -> Result<(), Box<dyn Error>> {
let path = Path::new("programs/examples/hello_starknet.cairo");
let libpath = Path::new("aot.so");
let libpath = Path::new("./aot.so");

let contract = compile_path(
path,
Expand Down
25 changes: 18 additions & 7 deletions src/aot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ pub fn call_contract_library<T: StarkNetSyscallHandler>(
syscall_handler: &mut T,
reg: &ProgramRegistry<CoreType, CoreLibfunc>,
) -> Result<(), Box<dyn Error>> {
dbg!(&entry_point.signature);
// dbg!(&entry_point.signature);
let symbol: &str = entry_point.id.debug_name.as_deref().unwrap();

// todo: verify signature matches that of a contract, so unsafe is "safe"

let felt = Felt252Abi([1; 32]);
let payload = (addr_of!(felt), 1, 1);
//let felt = Felt252Abi([1; 32]);
//let payload = (addr_of!(felt), 1, 1);

let calldata = Calldata { calldata: payload };
//let calldata = Calldata { calldata: payload };

unsafe {
let lib = libloading::Library::new(path)?;
Expand All @@ -90,15 +90,21 @@ pub fn call_contract_library<T: StarkNetSyscallHandler>(
let arena = Bump::new();

let ty = &entry_point.params[3].ty;
dbg!(ty);
// dbg!(ty);

let calldata = JITValue::Struct {
fields: vec![JITValue::Array(vec![JITValue::Felt252(1.into())])],
fields: vec![JITValue::Array(vec![
JITValue::Felt252(1.into()),
JITValue::Felt252(1.into()),
JITValue::Felt252(1.into()),
])],
debug_name: None,
}
.to_jit(&arena, reg, ty)
.unwrap();

dbg!(&calldata);

let syscall_handler_meta = SyscallHandlerMeta::new(syscall_handler);

let syscall_addr = syscall_handler_meta.as_ptr().as_ptr() as *const () as usize;
Expand All @@ -119,7 +125,12 @@ pub fn call_contract_library<T: StarkNetSyscallHandler>(

let gas: u128 = u64::MAX.into();
let range_check = arena.alloc_layout(Layout::new::<()>()).as_ptr().cast();
let result = func(range_check, gas_ptr, syscall_alloc.cast(), calldata.as_ptr().cast());
let result = func(
range_check,
gas_ptr,
syscall_alloc.cast(),
calldata.as_ptr().cast(),
);

// fix tag, because in llvm we use tag as a i1, the padding bytes may have garbage

Expand Down
12 changes: 8 additions & 4 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pub fn module_to_object(module: &Module<'_>) -> Result<Vec<u8>, LLVMCompileError
target_triple.cast(),
target_cpu.cast(),
target_cpu_features.cast(),
LLVMCodeGenOptLevel::LLVMCodeGenLevelAggressive,
LLVMCodeGenOptLevel::LLVMCodeGenLevelNone,
LLVMRelocMode::LLVMRelocDynamicNoPic,
LLVMCodeModel::LLVMCodeModelDefault,
);
Expand Down Expand Up @@ -161,7 +161,7 @@ pub fn object_to_shared_lib(object: &[u8], output_filename: &Path) -> Result<(),
"-dylib",
"-L/usr/local/lib",
"-L/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib",
"-L/Users/edgar/Documents/cairo_sierra_to_mlir/target/debug/",
"-L/Users/edgar/Documents/cairo_sierra_to_mlir/target/debug/", // change me
&file.display().to_string(),
"-o",
&output_filename.display().to_string(),
Expand All @@ -175,11 +175,15 @@ pub fn object_to_shared_lib(object: &[u8], output_filename: &Path) -> Result<(),
"--hash-style=gnu",
"--eh-frame-hdr",
"-shared",
"-o",
&output_filename.display().to_string(),
"-L/data2/edgar/work/native/target/debug", // change me
"-rpath=/data2/edgar/work/native/target/debug", // change me
"-rpath-link=/data2/edgar/work/native/target/debug", // change me
"-L/lib/../lib64",
"-L/usr/lib/../lib64",
"-o",
&output_filename.display().to_string(),
"-lc",
"-lcairo_native_runtime",
&file.display().to_string(),
]
}
Expand Down
15 changes: 15 additions & 0 deletions src/libfuncs/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,11 @@ where

let new_elem_ptr = op.result(0)?.into();

metadata
.get_mut::<DebugUtils>()
.unwrap()
.debug_breakpoint_trap(block_not_empty, location)?;

let op = block_not_empty.append_operation(llvm::load(
context,
elem_ptr,
Expand All @@ -698,6 +703,11 @@ where
));
let elem_value = op.result(0)?.into();

metadata
.get_mut::<DebugUtils>()
.unwrap()
.debug_breakpoint_trap(block_not_empty, location)?;

block_not_empty.append_operation(llvm::store(
context,
elem_value,
Expand All @@ -709,6 +719,11 @@ where
))),
));

metadata
.get_mut::<DebugUtils>()
.unwrap()
.debug_breakpoint_trap(block_not_empty, location)?;

let op = block_not_empty.append_operation(arith::constant(
context,
IntegerAttribute::new(1, len.r#type()).into(),
Expand Down
13 changes: 13 additions & 0 deletions src/metadata/debug_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ use melior::{
dialect::{func, llvm},
ir::{
attribute::{FlatSymbolRefAttribute, StringAttribute, TypeAttribute},
operation::OperationBuilder,
r#type::{FunctionType, IntegerType},
Block, Identifier, Location, Module, Region, Value,
},
Expand Down Expand Up @@ -148,6 +149,18 @@ impl DebugUtils {
Ok(())
}

pub fn debug_breakpoint_trap<'c, 'a>(
&mut self,
block: &'a Block<'c>,
location: Location<'c>,
) -> Result<()>
where
'c: 'a,
{
block.append_operation(OperationBuilder::new("llvm.intr.debugtrap", location).build()?);
Ok(())
}

pub fn print_pointer<'c, 'a>(
&mut self,
context: &'c Context,
Expand Down
1 change: 1 addition & 0 deletions src/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ impl JITValue {
.unwrap()
.cast()
.as_mut() = cap;
dbg!(&target);
target.cast()
} else {
Err(ErrorImpl::UnexpectedValue(format!(
Expand Down

0 comments on commit 7a5a006

Please sign in to comment.