Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix felt252 and enum deserialization bugs. #844

Merged
merged 7 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,11 +361,13 @@ fn parse_result(
return Err(Error::ParseAttributeError);

#[cfg(target_arch = "aarch64")]
Ok(Value::Felt252(
starknet_types_core::felt::Felt::from_bytes_le(unsafe {
std::mem::transmute::<&[u64; 4], &[u8; 32]>(&ret_registers)
}),
))
Ok(Value::Felt252({
let data = unsafe {
std::mem::transmute::<&mut [u64; 4], &mut [u8; 32]>(&mut ret_registers)
};
data[31] &= 0x0F; // Filter out first 4 bits (they're outside an i252).
starknet_types_core::felt::Felt::from_bytes_le(data)
}))
}
},
CoreTypeConcrete::Bytes31(_) => match return_ptr {
Expand Down Expand Up @@ -479,6 +481,12 @@ fn parse_result(
}
};

// Filter out bits that are not part of the enum's tag.
let tag = tag
& 1usize
.wrapping_shl(info.variants.len().next_power_of_two().trailing_zeros())
.wrapping_sub(1);

(
tag,
Ok(unsafe {
Expand Down
5 changes: 4 additions & 1 deletion src/executor/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,8 @@ impl AotContractExecutor {
};

let tag = *unsafe { enum_ptr.cast::<u8>().as_ref() } as usize;
let tag = tag & 0x01; // Filter out bits that are not part of the enum's tag.

// layout of both enum variants, both are a array of felts
let value_layout = unsafe { Layout::from_size_align_unchecked(24, 8) };
let value_ptr = unsafe {
Expand Down Expand Up @@ -382,7 +384,8 @@ impl AotContractExecutor {
for i in 0..num_elems {
// safe to create a NonNull because if the array has elements, the data_ptr can't be null.
let cur_elem_ptr = NonNull::new(unsafe { data_ptr.byte_add(elem_stride * i) }).unwrap();
let data = unsafe { cur_elem_ptr.cast::<[u8; 32]>().as_ref() };
let data = unsafe { cur_elem_ptr.cast::<[u8; 32]>().as_mut() };
data[31] &= 0x0F; // Filter out first 4 bits (they're outside an i252).
let data = Felt::from_bytes_le_slice(data);

array_value.push(data);
Expand Down
34 changes: 26 additions & 8 deletions src/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,12 +575,20 @@ impl Value {
value
}
CoreTypeConcrete::EcPoint(_) => {
let data = ptr.cast::<[[u8; 32]; 2]>().as_ref();
let data = ptr.cast::<[[u8; 32]; 2]>().as_mut();

data[0][31] &= 0x0F; // Filter out first 4 bits (they're outside an i252).
data[1][31] &= 0x0F; // Filter out first 4 bits (they're outside an i252).

Self::EcPoint(Felt::from_bytes_le(&data[0]), Felt::from_bytes_le(&data[1]))
}
CoreTypeConcrete::EcState(_) => {
let data = ptr.cast::<[[u8; 32]; 4]>().as_ref();
let data = ptr.cast::<[[u8; 32]; 4]>().as_mut();

data[0][31] &= 0x0F; // Filter out first 4 bits (they're outside an i252).
data[1][31] &= 0x0F; // Filter out first 4 bits (they're outside an i252).
data[2][31] &= 0x0F; // Filter out first 4 bits (they're outside an i252).
data[3][31] &= 0x0F; // Filter out first 4 bits (they're outside an i252).

Self::EcState(
Felt::from_bytes_le(&data[0]),
Expand All @@ -590,7 +598,8 @@ impl Value {
)
}
CoreTypeConcrete::Felt252(_) => {
let data = ptr.cast::<[u8; 32]>().as_ref();
let data = ptr.cast::<[u8; 32]>().as_mut();
data[31] &= 0x0F; // Filter out first 4 bits (they're outside an i252).
let data = Felt::from_bytes_le_slice(data);
Self::Felt252(data)
}
Expand Down Expand Up @@ -645,6 +654,12 @@ impl Value {
},
};

// Filter out bits that are not part of the enum's tag.
let tag_value = tag_value
& 1usize
.wrapping_shl(info.variants.len().next_power_of_two().trailing_zeros())
.wrapping_sub(1);

let payload_ty = registry.get_type(&info.variants[tag_value])?;
let payload_layout = payload_ty.layout(registry)?;

Expand Down Expand Up @@ -695,21 +710,23 @@ impl Value {
);

let mut output_map = HashMap::with_capacity(inner.len());
for (key, val_ptr) in inner.iter() {
for (mut key, val_ptr) in inner.into_iter() {
if val_ptr.is_null() {
continue;
}

let key = Felt::from_bytes_le(key);
key[31] &= 0x0F; // Filter out first 4 bits (they're outside an i252).

let key = Felt::from_bytes_le(&key);
output_map.insert(
key,
Self::from_ptr(
NonNull::new(*val_ptr).unwrap().cast(),
NonNull::new(val_ptr).unwrap().cast(),
&info.ty,
registry,
)?,
);
libc_free(*val_ptr);
libc_free(val_ptr);
}

Self::Felt252Dict {
Expand Down Expand Up @@ -737,7 +754,8 @@ impl Value {
| StarkNetTypeConcrete::StorageBaseAddress(_)
| StarkNetTypeConcrete::StorageAddress(_) => {
// felt values
let data = ptr.cast::<[u8; 32]>().as_ref();
let data = ptr.cast::<[u8; 32]>().as_mut();
data[31] &= 0x0F; // Filter out first 4 bits (they're outside an i252).
let data = Felt::from_bytes_le(data);
Self::Felt252(data)
}
Expand Down
Loading