Skip to content

Commit a86bb99

Browse files
authored
Merge pull request #5135 from stacks-network/fix/wasm-print-stub
Clarity-Wasm: fix for the print host function
2 parents db7040b + 8d191d9 commit a86bb99

File tree

1 file changed

+36
-7
lines changed

1 file changed

+36
-7
lines changed

clarity/src/vm/clarity_wasm.rs

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use stacks_common::util::secp256k1::{secp256k1_recover, secp256k1_verify, Secp25
1111
use wasmtime::{AsContextMut, Caller, Engine, Linker, Memory, Module, Store, Trap, Val, ValType};
1212

1313
use super::analysis::{CheckError, CheckErrors};
14+
use super::ast::parse;
1415
use super::callables::{DefineType, DefinedFunction};
1516
use super::contracts::Contract;
1617
use super::costs::{constants as cost_constants, CostTracker, LimitedCostTracker};
@@ -1673,6 +1674,25 @@ fn pass_argument_to_wasm(
16731674
}
16741675
}
16751676

1677+
pub fn signature_from_string(
1678+
val: &str,
1679+
version: ClarityVersion,
1680+
epoch: StacksEpochId,
1681+
) -> Result<TypeSignature, Error> {
1682+
let expr = parse(
1683+
&QualifiedContractIdentifier::transient(),
1684+
val,
1685+
version,
1686+
epoch,
1687+
)?;
1688+
let expr = expr.first().ok_or(CheckErrors::InvalidTypeDescription)?;
1689+
Ok(TypeSignature::parse_type_repr(
1690+
StacksEpochId::latest(),
1691+
expr,
1692+
&mut (),
1693+
)?)
1694+
}
1695+
16761696
/// Reserve space on the Wasm stack for the return value of a function, if
16771697
/// needed, and return a vector of `Val`s that can be passed to `call`, as a
16781698
/// place to store the return value, along with the new offset, which is the
@@ -5546,7 +5566,11 @@ fn link_print_fn(linker: &mut Linker<ClarityWasmContext>) -> Result<(), Error> {
55465566
.func_wrap(
55475567
"clarity",
55485568
"print",
5549-
|mut caller: Caller<'_, ClarityWasmContext>, value_offset: i32, value_length: i32| {
5569+
|mut caller: Caller<'_, ClarityWasmContext>,
5570+
value_offset: i32,
5571+
_value_length: i32,
5572+
serialized_ty_offset: i32,
5573+
serialized_ty_length: i32| {
55505574
// runtime_cost(ClarityCostFunction::Print, env, input.size())?;
55515575

55525576
// Get the memory from the caller
@@ -5555,14 +5579,19 @@ fn link_print_fn(linker: &mut Linker<ClarityWasmContext>) -> Result<(), Error> {
55555579
.and_then(|export| export.into_memory())
55565580
.ok_or(Error::Wasm(WasmError::MemoryNotFound))?;
55575581

5558-
// Read in the bytes from the Wasm memory
5559-
let bytes = read_bytes_from_wasm(memory, &mut caller, value_offset, value_length)?;
5582+
let serialized_ty = String::from_utf8(read_bytes_from_wasm(
5583+
memory,
5584+
&mut caller,
5585+
serialized_ty_offset,
5586+
serialized_ty_length,
5587+
)?)?;
55605588

5561-
let clarity_val = Value::deserialize_read(&mut bytes.as_slice(), None, false)?;
5589+
let epoch = caller.data().global_context.epoch_id;
5590+
let version = caller.data().contract_context().get_clarity_version();
55625591

5563-
if cfg!(feature = "developer-mode") {
5564-
debug!("{}", &clarity_val);
5565-
}
5592+
let value_ty = signature_from_string(&serialized_ty, *version, epoch)?;
5593+
let clarity_val =
5594+
read_from_wasm_indirect(memory, &mut caller, &value_ty, value_offset, epoch)?;
55665595

55675596
caller.data_mut().register_print_event(clarity_val)?;
55685597

0 commit comments

Comments
 (0)