Skip to content

Commit 186830c

Browse files
committed
chore: update linnker functions
1 parent 04c5c77 commit 186830c

File tree

1 file changed

+85
-17
lines changed

1 file changed

+85
-17
lines changed

clarity/src/vm/clarity_wasm.rs

Lines changed: 85 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3258,7 +3258,7 @@ fn link_stx_burn_fn(linker: &mut Linker<ClarityWasmContext>) -> Result<(), Error
32583258
amount_hi: i64,
32593259
principal_offset: i32,
32603260
principal_length: i32| {
3261-
let amount = (amount_hi as u128) << 64 | (amount_lo as u128);
3261+
let amount = (amount_hi as u128) << 64 | ((amount_lo as u64) as u128);
32623262

32633263
// Get the memory from the caller
32643264
let memory = caller
@@ -3358,7 +3358,7 @@ fn link_stx_transfer_fn(linker: &mut Linker<ClarityWasmContext>) -> Result<(), E
33583358
recipient_length: i32,
33593359
memo_offset: i32,
33603360
memo_length: i32| {
3361-
let amount = (amount_hi as u128) << 64 | (amount_lo as u128);
3361+
let amount = (amount_hi as u128) << 64 | ((amount_lo as u64) as u128);
33623362

33633363
// Get the memory from the caller
33643364
let memory = caller
@@ -3624,7 +3624,7 @@ fn link_ft_burn_fn(linker: &mut Linker<ClarityWasmContext>) -> Result<(), Error>
36243624
let token_name = ClarityName::try_from(name.clone())?;
36253625

36263626
// Compute the amount
3627-
let amount = (amount_hi as u128) << 64 | (amount_lo as u128);
3627+
let amount = (amount_hi as u128) << 64 | ((amount_lo as u64) as u128);
36283628

36293629
// Read the sender principal from the Wasm memory
36303630
let value = read_from_wasm(
@@ -3754,7 +3754,7 @@ fn link_ft_mint_fn(linker: &mut Linker<ClarityWasmContext>) -> Result<(), Error>
37543754
let token_name = ClarityName::try_from(name.clone())?;
37553755

37563756
// Compute the amount
3757-
let amount = (amount_hi as u128) << 64 | (amount_lo as u128);
3757+
let amount = (amount_hi as u128) << 64 | ((amount_lo as u64) as u128);
37583758

37593759
// Read the sender principal from the Wasm memory
37603760
let value = read_from_wasm(
@@ -3804,7 +3804,9 @@ fn link_ft_mint_fn(linker: &mut Linker<ClarityWasmContext>) -> Result<(), Error>
38043804

38053805
// This `expect` is safe because the `checked_increase_token_supply` call above
38063806
// would have failed if the addition would have overflowed.
3807-
let final_to_bal = to_bal.checked_add(amount).expect("FT overflow");
3807+
let final_to_bal = to_bal
3808+
.checked_add(amount)
3809+
.ok_or(Error::Runtime(RuntimeErrorType::ArithmeticOverflow, None))?;
38083810

38093811
caller
38103812
.data_mut()
@@ -3882,7 +3884,7 @@ fn link_ft_transfer_fn(linker: &mut Linker<ClarityWasmContext>) -> Result<(), Er
38823884
let token_name = ClarityName::try_from(name.clone())?;
38833885

38843886
// Compute the amount
3885-
let amount = (amount_hi as u128) << 64 | (amount_lo as u128);
3887+
let amount = (amount_hi as u128) << 64 | ((amount_lo as u64) as u128);
38863888

38873889
// Read the sender principal from the Wasm memory
38883890
let value = read_from_wasm(
@@ -4034,8 +4036,8 @@ fn link_nft_get_owner_fn(linker: &mut Linker<ClarityWasmContext>) -> Result<(),
40344036
|mut caller: Caller<'_, ClarityWasmContext>,
40354037
name_offset: i32,
40364038
name_length: i32,
4037-
asset_offset: i32,
4038-
asset_length: i32,
4039+
mut asset_offset: i32,
4040+
mut asset_length: i32,
40394041
return_offset: i32,
40404042
_return_length: i32| {
40414043
// Get the memory from the caller
@@ -4064,6 +4066,10 @@ fn link_nft_get_owner_fn(linker: &mut Linker<ClarityWasmContext>) -> Result<(),
40644066
let expected_asset_type = &nft_metadata.key_type;
40654067

40664068
// Read in the NFT identifier from the Wasm memory
4069+
if is_in_memory_type(expected_asset_type) {
4070+
(asset_offset, asset_length) =
4071+
read_indirect_offset_and_length(memory, &mut caller, asset_offset)?;
4072+
}
40674073
let asset = read_from_wasm(
40684074
memory,
40694075
&mut caller,
@@ -4130,8 +4136,8 @@ fn link_nft_burn_fn(linker: &mut Linker<ClarityWasmContext>) -> Result<(), Error
41304136
|mut caller: Caller<'_, ClarityWasmContext>,
41314137
name_offset: i32,
41324138
name_length: i32,
4133-
asset_offset: i32,
4134-
asset_length: i32,
4139+
mut asset_offset: i32,
4140+
mut asset_length: i32,
41354141
sender_offset: i32,
41364142
sender_length: i32| {
41374143
// Get the memory from the caller
@@ -4161,6 +4167,10 @@ fn link_nft_burn_fn(linker: &mut Linker<ClarityWasmContext>) -> Result<(), Error
41614167
let expected_asset_type = &nft_metadata.key_type;
41624168

41634169
// Read in the NFT identifier from the Wasm memory
4170+
if is_in_memory_type(expected_asset_type) {
4171+
(asset_offset, asset_length) =
4172+
read_indirect_offset_and_length(memory, &mut caller, asset_offset)?;
4173+
}
41644174
let asset = read_from_wasm(
41654175
memory,
41664176
&mut caller,
@@ -4265,8 +4275,8 @@ fn link_nft_mint_fn(linker: &mut Linker<ClarityWasmContext>) -> Result<(), Error
42654275
|mut caller: Caller<'_, ClarityWasmContext>,
42664276
name_offset: i32,
42674277
name_length: i32,
4268-
asset_offset: i32,
4269-
asset_length: i32,
4278+
mut asset_offset: i32,
4279+
mut asset_length: i32,
42704280
recipient_offset: i32,
42714281
recipient_length: i32| {
42724282
// Get the memory from the caller
@@ -4296,6 +4306,10 @@ fn link_nft_mint_fn(linker: &mut Linker<ClarityWasmContext>) -> Result<(), Error
42964306
let expected_asset_type = &nft_metadata.key_type;
42974307

42984308
// Read in the NFT identifier from the Wasm memory
4309+
if is_in_memory_type(expected_asset_type) {
4310+
(asset_offset, asset_length) =
4311+
read_indirect_offset_and_length(memory, &mut caller, asset_offset)?;
4312+
}
42994313
let asset = read_from_wasm(
43004314
memory,
43014315
&mut caller,
@@ -4389,8 +4403,8 @@ fn link_nft_transfer_fn(linker: &mut Linker<ClarityWasmContext>) -> Result<(), E
43894403
|mut caller: Caller<'_, ClarityWasmContext>,
43904404
name_offset: i32,
43914405
name_length: i32,
4392-
asset_offset: i32,
4393-
asset_length: i32,
4406+
mut asset_offset: i32,
4407+
mut asset_length: i32,
43944408
sender_offset: i32,
43954409
sender_length: i32,
43964410
recipient_offset: i32,
@@ -4422,6 +4436,10 @@ fn link_nft_transfer_fn(linker: &mut Linker<ClarityWasmContext>) -> Result<(), E
44224436
let expected_asset_type = &nft_metadata.key_type;
44234437

44244438
// Read in the NFT identifier from the Wasm memory
4439+
if is_in_memory_type(expected_asset_type) {
4440+
(asset_offset, asset_length) =
4441+
read_indirect_offset_and_length(memory, &mut caller, asset_offset)?;
4442+
}
44254443
let asset = read_from_wasm(
44264444
memory,
44274445
&mut caller,
@@ -5849,21 +5867,45 @@ fn link_secp256k1_recover_fn(linker: &mut Linker<ClarityWasmContext>) -> Result<
58495867
.and_then(|export| export.into_memory())
58505868
.ok_or(Error::Wasm(WasmError::MemoryNotFound))?;
58515869

5870+
let ret_ty = TypeSignature::new_response(BUFF_33.clone(), TypeSignature::UIntType)?;
5871+
let repr_size = get_type_size(&ret_ty);
5872+
58525873
// Read the message bytes from the memory
58535874
let msg_bytes = read_bytes_from_wasm(memory, &mut caller, msg_offset, msg_length)?;
5875+
// To match the interpreter behavior, if the message is the
5876+
// wrong length, throw a runtime type error.
5877+
if msg_bytes.len() != 32 {
5878+
return Err(CheckErrors::TypeValueError(
5879+
BUFF_32.clone(),
5880+
Value::buff_from(msg_bytes)?,
5881+
)
5882+
.into());
5883+
}
58545884

58555885
// Read the signature bytes from the memory
58565886
let sig_bytes = read_bytes_from_wasm(memory, &mut caller, sig_offset, sig_length)?;
5887+
// To match the interpreter behavior, if the signature is the
5888+
// wrong length, return a Clarity error.
5889+
if sig_bytes.len() != 65 || sig_bytes[64] > 3 {
5890+
let result = Value::err_uint(2);
5891+
write_to_wasm(
5892+
caller,
5893+
memory,
5894+
&ret_ty,
5895+
return_offset,
5896+
return_offset + repr_size,
5897+
&result,
5898+
true,
5899+
)?;
5900+
return Ok(());
5901+
}
58575902

58585903
let result = match secp256k1_recover(&msg_bytes, &sig_bytes) {
58595904
Ok(pubkey) => Value::okay(Value::buff_from(pubkey.to_vec()).unwrap()).unwrap(),
58605905
_ => Value::err_uint(1),
58615906
};
58625907

58635908
// Write the result to the return buffer
5864-
let ret_ty =
5865-
TypeSignature::new_response(BUFF_33.clone(), TypeSignature::UIntType).unwrap();
5866-
let repr_size = get_type_size(&ret_ty);
58675909
write_to_wasm(
58685910
caller,
58695911
memory,
@@ -5910,12 +5952,38 @@ fn link_secp256k1_verify_fn(linker: &mut Linker<ClarityWasmContext>) -> Result<(
59105952

59115953
// Read the message bytes from the memory
59125954
let msg_bytes = read_bytes_from_wasm(memory, &mut caller, msg_offset, msg_length)?;
5955+
// To match the interpreter behavior, if the message is the
5956+
// wrong length, throw a runtime type error.
5957+
if msg_bytes.len() != 32 {
5958+
return Err(CheckErrors::TypeValueError(
5959+
BUFF_32.clone(),
5960+
Value::buff_from(msg_bytes)?,
5961+
)
5962+
.into());
5963+
}
59135964

59145965
// Read the signature bytes from the memory
59155966
let sig_bytes = read_bytes_from_wasm(memory, &mut caller, sig_offset, sig_length)?;
5967+
// To match the interpreter behavior, if the signature is the
5968+
// wrong length, return a Clarity error.
5969+
if sig_bytes.len() < 64
5970+
|| sig_bytes.len() > 65
5971+
|| sig_bytes.len() == 65 && sig_bytes[64] > 3
5972+
{
5973+
return Ok(0i32);
5974+
}
59165975

59175976
// Read the public-key bytes from the memory
59185977
let pk_bytes = read_bytes_from_wasm(memory, &mut caller, pk_offset, pk_length)?;
5978+
// To match the interpreter behavior, if the public key is the
5979+
// wrong length, throw a runtime type error.
5980+
if pk_bytes.len() != 33 {
5981+
return Err(CheckErrors::TypeValueError(
5982+
BUFF_33.clone(),
5983+
Value::buff_from(pk_bytes)?,
5984+
)
5985+
.into());
5986+
}
59195987

59205988
Ok(secp256k1_verify(&msg_bytes, &sig_bytes, &pk_bytes).map_or(0i32, |_| 1i32))
59215989
},

0 commit comments

Comments
 (0)