Skip to content

Allocate memory for contract-call? args representation #6009

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

Open
wants to merge 4 commits into
base: feat/clarity-wasm-develop
Choose a base branch
from
Open
Changes from 1 commit
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
27 changes: 24 additions & 3 deletions clarity/src/vm/clarity_wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,10 +542,12 @@ pub fn call_function<'a>(
.get_memory(&mut store, "memory")
.ok_or(Error::Wasm(WasmError::MemoryNotFound))?;

// Determine how much space is needed for arguments
// The only argument that needs space to be written at `offset` is the list
// type according to pass_argument_to_wasm, the function that writes to
// memory. Because of this we only allocate space for lists.
let mut arg_size = 0;
for arg in func_types.get_arg_types() {
arg_size += get_type_in_memory_size(arg, false);
for arg in args {
arg_size += get_list_size(arg);
}
let mut in_mem_offset = offset + arg_size;

Expand Down Expand Up @@ -613,6 +615,25 @@ pub const CONTRACT_NAME_MAX_LENGTH: usize = 128;
// Standard principal, but at most 128 character function name
pub const PRINCIPAL_BYTES_MAX: usize = STANDARD_PRINCIPAL_BYTES + CONTRACT_NAME_MAX_LENGTH;

// Return the number of bytes required to represent a list in memory.
fn get_list_size(val: &Value) -> i32 {
match val {
Value::Sequence(SequenceData::List(list)) =>
// 8 bytes for the offset and length
{
8 + list
.data
.iter()
.map(|item| match item {
Value::Sequence(SequenceData::List(_)) => get_list_size(item),
_ => get_type_size(list.type_signature.get_list_item_type()),
})
.sum::<i32>()
}
_ => 0,
}
}

/// Return the number of bytes required to representation of a value of the
/// type `ty`. For in-memory types, this is just the size of the offset and
/// length. For non-in-memory types, this is the size of the value itself.
Expand Down