starkware-libs/cairo

bug: `handle_main_return_value` cannot return a ByteArray

enitrat opened this issue · 2 comments

Bug Report

Cairo version:
2.8.4

Current behavior:

/// Handling the main return value to create a `RunResultValue`.
pub fn handle_main_return_value(
inner_type_size: Option<i16>,
values: Vec<Felt252>,
cells: &[Option<Felt252>],
) -> RunResultValue {
if let Some(inner_type_size) = inner_type_size {
// The function includes a panic wrapper.
if values[0] == Felt252::from(0) {
// The run resulted successfully, returning the inner value.
let inner_ty_size = inner_type_size as usize;
let skip_size = values.len() - inner_ty_size;
RunResultValue::Success(values.into_iter().skip(skip_size).collect())
} else {
// The run resulted in a panic, returning the error data.
let err_data_start = values[values.len() - 2].to_usize().unwrap();
let err_data_end = values[values.len() - 1].to_usize().unwrap();
RunResultValue::Panic(
cells[err_data_start..err_data_end]
.iter()
.cloned()
.map(Option::unwrap)
.collect(),
)
}
} else {
// No panic wrap - so always successful.
RunResultValue::Success(values)
}
}

Where a println! would print: [1, 2, 3, 420], the returned values are [0x1e50, 0x1e50, 0x225b312c20322c20332c203432305d22, 0x10]

It seems that bytearrays, and in general, any pointer-based struct, cannot be returned.

Expected behavior:
This should return the serialized bytearray representation for the above, which would be [0, 0x225b312c20322c20332c203432305d22, 0x10]

what are you trying to use it for?
note that this is a structure for only for debug and test runs for the time being - not for actual provable calculation.

A custom test runner. I use cairo-run to output JSON representations of my types as bytearrays. The goal is to use this test runner for property testing / diff testing