From e3c09ffce89761c46c9062bfe0cb8eb638259a52 Mon Sep 17 00:00:00 2001 From: Esteve Soler Arderiu Date: Wed, 5 Jun 2024 16:02:49 +0200 Subject: [PATCH 1/4] Fix dict deserialization. --- src/executor.rs | 31 ++++++----------------- src/libfuncs/felt252_dict.rs | 40 +++++++++++++++++++++++++++++- src/types/squashed_felt252_dict.rs | 3 --- src/values.rs | 6 +++-- tests/common.rs | 6 +---- 5 files changed, 51 insertions(+), 35 deletions(-) diff --git a/src/executor.rs b/src/executor.rs index 51141b219..bd1691b5a 100644 --- a/src/executor.rs +++ b/src/executor.rs @@ -27,7 +27,7 @@ use starknet_types_core::felt::Felt; use std::{ alloc::Layout, arch::global_asm, - ptr::{null_mut, NonNull}, + ptr::{addr_of_mut, null_mut, NonNull}, rc::Rc, }; @@ -820,29 +820,12 @@ fn parse_result( Ok(JitValue::from_jit(return_ptr.unwrap(), type_id, registry)) } } - CoreTypeConcrete::Felt252Dict(_) => match return_ptr { - Some(return_ptr) => Ok(JitValue::from_jit( - unsafe { *return_ptr.cast::>().as_ref() }, - type_id, - registry, - )), - None => Ok(JitValue::from_jit( - NonNull::new(ret_registers[0] as *mut ()).unwrap(), - type_id, - registry, - )), - }, - CoreTypeConcrete::SquashedFelt252Dict(_) => match return_ptr { - Some(return_ptr) => Ok(JitValue::from_jit( - unsafe { *return_ptr.cast::>().as_ref() }, - type_id, - registry, - )), - None => Ok(JitValue::from_jit( - NonNull::new(ret_registers[0] as *mut ()).unwrap(), - type_id, - registry, - )), + CoreTypeConcrete::Felt252Dict(_) | CoreTypeConcrete::SquashedFelt252Dict(_) => unsafe { + let ptr = return_ptr.unwrap_or(NonNull::new_unchecked( + addr_of_mut!(ret_registers[0]) as *mut () + )); + let value = JitValue::from_jit(ptr, type_id, registry); + Ok(value) }, // Builtins are handled before the call to parse_result diff --git a/src/libfuncs/felt252_dict.rs b/src/libfuncs/felt252_dict.rs index bed9a30b4..6dd597075 100644 --- a/src/libfuncs/felt252_dict.rs +++ b/src/libfuncs/felt252_dict.rs @@ -116,7 +116,10 @@ pub fn build_squash<'ctx, 'this>( #[cfg(test)] mod test { - use crate::utils::test::{jit_dict, jit_struct, load_cairo, run_program_assert_output}; + use crate::{ + utils::test::{jit_dict, jit_struct, load_cairo, run_program_assert_output}, + values::JitValue, + }; #[test] fn run_dict_new() { @@ -208,4 +211,39 @@ mod test { ), ); } + + #[test] + // #[ignore] + fn run_dict_deserialize2() { + let program = load_cairo!( + use traits::Default; + use dict::Felt252DictTrait; + + fn run_test(mut dict: Felt252Dict) -> (felt252, Felt252Dict) { + (0, dict) + } + ); + + run_program_assert_output( + &program, + "run_test", + &[jit_dict!( + 1 => 2u32, + 2 => 3u32, + 3 => 4u32, + 4 => 5u32, + 5 => 6u32, + )], + jit_struct!( + JitValue::Felt252(0.into()), + jit_dict!( + 1 => 2u32, + 2 => 3u32, + 3 => 4u32, + 4 => 5u32, + 5 => 6u32, + ) + ), + ); + } } diff --git a/src/types/squashed_felt252_dict.rs b/src/types/squashed_felt252_dict.rs index 3b7a05621..0ee19a818 100644 --- a/src/types/squashed_felt252_dict.rs +++ b/src/types/squashed_felt252_dict.rs @@ -25,8 +25,5 @@ pub fn build<'ctx>( _metadata: &mut MetadataStorage, _info: WithSelf, ) -> Result> { - //let inner = registry.get_type(&info.ty)?; - //let layout = inner.layout(registry)?; - Ok(llvm::r#type::pointer(context, 0)) } diff --git a/src/values.rs b/src/values.rs index c26366430..fa649cccf 100644 --- a/src/values.rs +++ b/src/values.rs @@ -705,8 +705,10 @@ impl JitValue { } CoreTypeConcrete::Felt252Dict(info) | CoreTypeConcrete::SquashedFelt252Dict(info) => { - let map = Box::from_raw( - ptr.cast::>>() + let (map, _) = *Box::from_raw( + ptr.cast::>() + .as_ref() + .cast::<(HashMap<[u8; 32], NonNull>, u64)>() .as_ptr(), ); diff --git a/tests/common.rs b/tests/common.rs index 1c9d48011..f69e5a1c8 100644 --- a/tests/common.rs +++ b/tests/common.rs @@ -145,11 +145,7 @@ pub fn load_cairo_path(program_path: &str) -> (String, Program, SierraCasmRunner let mut db = RootDatabase::default(); init_dev_corelib( &mut db, - Path::new( - &var("CARGO_MANIFEST_DIR") - .unwrap_or_else(|_| "/Users/esteve/Documents/LambdaClass/cairo_native".to_string()), - ) - .join("corelib/src"), + Path::new(&var("CARGO_MANIFEST_DIR").unwrap()).join("corelib/src"), ); let main_crate_ids = setup_project(&mut db, program_file).unwrap(); let program = compile_prepared_db( From f4d16aa9a9b5c9e57381dbddd582d8f2480ec09d Mon Sep 17 00:00:00 2001 From: Esteve Soler Arderiu Date: Wed, 5 Jun 2024 16:11:20 +0200 Subject: [PATCH 2/4] Remove commented line. --- src/libfuncs/felt252_dict.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libfuncs/felt252_dict.rs b/src/libfuncs/felt252_dict.rs index 6dd597075..6446ae368 100644 --- a/src/libfuncs/felt252_dict.rs +++ b/src/libfuncs/felt252_dict.rs @@ -213,7 +213,6 @@ mod test { } #[test] - // #[ignore] fn run_dict_deserialize2() { let program = load_cairo!( use traits::Default; From c703c375bba0298f1f9acc6ed4370f5e4faf81a5 Mon Sep 17 00:00:00 2001 From: Esteve Soler Arderiu Date: Wed, 5 Jun 2024 17:25:41 +0200 Subject: [PATCH 3/4] Fix and enable fixed tests. --- tests/common.rs | 24 ++++++++++++++++++++++++ tests/tests/cases.rs | 4 ++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/tests/common.rs b/tests/common.rs index f69e5a1c8..5d3e812fd 100644 --- a/tests/common.rs +++ b/tests/common.rs @@ -324,6 +324,7 @@ pub fn compare_outputs( | CoreTypeConcrete::Sint32(_) | CoreTypeConcrete::Sint16(_) | CoreTypeConcrete::Sint8(_) + | CoreTypeConcrete::Box(_) | CoreTypeConcrete::Nullable(_) => 1, CoreTypeConcrete::Enum(info) => { 1 + info @@ -343,6 +344,7 @@ pub fn compare_outputs( CoreTypeConcrete::Snapshot(info) => { map_vm_sizes(size_cache, registry, &info.ty) } + CoreTypeConcrete::SquashedFelt252Dict(_) => 2, x => todo!("vm size not yet implemented: {:?}", x.info()), }; size_cache.insert(ty.clone(), type_size); @@ -507,6 +509,28 @@ pub fn compare_outputs( ), } } + CoreTypeConcrete::Box(info) => { + assert_eq!(values.len(), 1); + + let ty_size = map_vm_sizes(size_cache, registry, &info.ty); + match values[0].to_usize().unwrap() { + ptr if ty_size == 0 => { + assert_eq!(ptr, 1); + map_vm_values(size_cache, registry, memory, &[], &info.ty) + } + ptr => map_vm_values( + size_cache, + registry, + memory, + &memory[ptr..ptr + ty_size] + .iter() + .cloned() + .map(Option::unwrap) + .collect::>(), + &info.ty, + ), + } + } CoreTypeConcrete::NonZero(info) => { map_vm_values(size_cache, registry, memory, values, &info.ty) } diff --git a/tests/tests/cases.rs b/tests/tests/cases.rs index b2f99d8ad..ff85fbe3a 100644 --- a/tests/tests/cases.rs +++ b/tests/tests/cases.rs @@ -123,8 +123,8 @@ use test_case::test_case; #[test_case("tests/cases/cairo_vm/hello.cairo")] #[test_case("tests/cases/cairo_vm/my_rectangle.cairo")] #[test_case("tests/cases/cairo_vm/null_ret.cairo")] -// #[test_case("tests/cases/cairo_vm/nullable_box_vec.cairo")] -// #[test_case("tests/cases/cairo_vm/nullable_dict.cairo")] +#[test_case("tests/cases/cairo_vm/nullable_box_vec.cairo")] +#[test_case("tests/cases/cairo_vm/nullable_dict.cairo")] #[test_case("tests/cases/cairo_vm/ops.cairo")] #[test_case("tests/cases/cairo_vm/pedersen_example.cairo")] #[test_case("tests/cases/cairo_vm/poseidon.cairo")] From 604603845792eb6081ca7f45bd4b67600d7b5e33 Mon Sep 17 00:00:00 2001 From: Esteve Soler Arderiu Date: Thu, 6 Jun 2024 15:18:41 +0200 Subject: [PATCH 4/4] Add tests for dicts of structs and enums. --- src/libfuncs/felt252_dict.rs | 61 +++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/src/libfuncs/felt252_dict.rs b/src/libfuncs/felt252_dict.rs index 6446ae368..7f47b74bf 100644 --- a/src/libfuncs/felt252_dict.rs +++ b/src/libfuncs/felt252_dict.rs @@ -117,7 +117,7 @@ pub fn build_squash<'ctx, 'this>( #[cfg(test)] mod test { use crate::{ - utils::test::{jit_dict, jit_struct, load_cairo, run_program_assert_output}, + utils::test::{jit_dict, jit_enum, jit_struct, load_cairo, run_program_assert_output}, values::JitValue, }; @@ -245,4 +245,63 @@ mod test { ), ); } + + #[test] + fn run_dict_deserialize_struct() { + let program = load_cairo! { + use core::{dict::Felt252DictTrait, nullable::Nullable}; + + fn run_test() -> Felt252Dict> { + let mut x: Felt252Dict> = Default::default(); + x.insert(0, NullableTrait::new((1_u32, 2_u64, 3_u128))); + x.insert(1, NullableTrait::new((2_u32, 3_u64, 4_u128))); + x.insert(2, NullableTrait::new((3_u32, 4_u64, 5_u128))); + x + } + }; + + run_program_assert_output( + &program, + "run_test", + &[], + jit_dict!( + 0 => jit_struct!(1u32.into(), 2u64.into(), 3u128.into()), + 1 => jit_struct!(2u32.into(), 3u64.into(), 4u128.into()), + 2 => jit_struct!(3u32.into(), 4u64.into(), 5u128.into()), + ), + ); + } + + #[test] + fn run_dict_deserialize_enum() { + let program = load_cairo! { + use core::{dict::Felt252DictTrait, nullable::Nullable}; + + #[derive(Drop)] + enum MyEnum { + A: u32, + B: u64, + C: u128, + } + + fn run_test() -> Felt252Dict> { + let mut x: Felt252Dict> = Default::default(); + x.insert(0, NullableTrait::new(MyEnum::A(1))); + x.insert(1, NullableTrait::new(MyEnum::B(2))); + x.insert(2, NullableTrait::new(MyEnum::C(3))); + x + } + }; + + run_program_assert_output( + &program, + "run_test", + &[], + jit_dict!( + 0 => jit_enum!(0, 1u32.into()), + 1 => jit_enum!(1, 2u64.into()), + 2 => jit_enum!(2, 3u128.into()), + ), + ); + } }