Skip to content

Commit

Permalink
feat: read/write arg lengths from/to memory as bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
BowTiedWoo committed Oct 25, 2024
1 parent e8936cc commit 341ec46
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
10 changes: 4 additions & 6 deletions clar2wasm/src/error_mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use clarity::vm::{ClarityVersion, Value};
use wasmtime::{AsContextMut, Instance, Trap};

use crate::wasm_utils::{
read_from_wasm_indirect, read_identifier_from_wasm, signature_from_string,
read_bytes_from_wasm, read_from_wasm_indirect, read_identifier_from_wasm, signature_from_string,
};

const LOG2_ERROR_MESSAGE: &str = "log2 must be passed a positive integer";
Expand Down Expand Up @@ -315,14 +315,12 @@ fn get_global_i32(instance: &Instance, store: &mut impl AsContextMut, name: &str
/// A tuple `(expected, got)` where:
/// - `expected` is the number of arguments expected.
/// - `got` is the number of arguments actually received.
fn extract_expected_and_got(arg_lengths: &str) -> (usize, usize) {
let bytes = arg_lengths.as_bytes();

fn extract_expected_and_got(bytes: &[u8]) -> (usize, usize) {
// Assuming the first 4 bytes represent the expected value
let expected = u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]) as usize;

// Assuming the next 4 bytes represent the got value
let got = u32::from_le_bytes([bytes[16], bytes[17], bytes[18], bytes[19]]) as usize;
let got = u32::from_le_bytes([bytes[4], bytes[5], bytes[6], bytes[7]]) as usize;

(expected, got)
}
Expand Down Expand Up @@ -379,7 +377,7 @@ fn get_runtime_error_arg_lengths(
let memory = instance
.get_memory(&mut *store, "memory")
.unwrap_or_else(|| panic!("Could not find wasm instance memory"));
let arg_lengths = read_identifier_from_wasm(
let arg_lengths = read_bytes_from_wasm(
memory,
store,
runtime_error_arg_offset,
Expand Down
7 changes: 4 additions & 3 deletions clar2wasm/src/wasm_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1674,11 +1674,12 @@ pub fn check_argument_count(
actual: usize,
check: ArgumentCountCheck,
) -> Result<(), GeneratorError> {
let expected = expected as u32;
let actual = actual as u32;
let mut handle_mismatch = |error_map: ErrorMap| -> Result<(), GeneratorError> {
let (arg_name_offset_start, arg_name_len_expected) =
generator.add_literal(&clarity::vm::Value::UInt(expected as u128))?;
let (_, arg_name_len_got) =
generator.add_literal(&clarity::vm::Value::UInt(actual as u128))?;
generator.add_bytes_literal(&expected.to_le_bytes())?;
let (_, arg_name_len_got) = generator.add_bytes_literal(&actual.to_le_bytes())?;
builder
.i32_const(arg_name_offset_start as i32)
.global_set(get_global(&generator.module, "runtime-error-arg-offset")?)
Expand Down

0 comments on commit 341ec46

Please sign in to comment.