Skip to content
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

Fix some runtime exception #1571

Merged
merged 7 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ fn test_session_key_rooch() {
ROOCH_FRAMEWORK_ADDRESS,
Empty::MODULE_NAME.as_str(),
Empty::EMPTY_FUNCTION_NAME.as_str(),
);
)
.unwrap();
let max_inactive_interval = 100;
let action = rooch_types::framework::session_key::SessionKeyModule::create_session_key_action(
session_auth_key.as_ref().to_vec(),
Expand Down
18 changes: 12 additions & 6 deletions crates/rooch-types/src/framework/session_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,18 @@ pub struct SessionScope {
}

impl SessionScope {
pub fn new(module_address: AccountAddress, module_name: &str, function_name: &str) -> Self {
Self {
pub fn new(
module_address: AccountAddress,
module_name: &str,
function_name: &str,
) -> Result<Self> {
let module_name_value = MoveAsciiString::from_str(module_name)?;
let function_name_value = MoveAsciiString::from_str(function_name)?;
Ok(Self {
module_address,
module_name: MoveAsciiString::from_str(module_name).expect("invalid module name"),
function_name: MoveAsciiString::from_str(function_name).expect("invalid function name"),
}
module_name: module_name_value,
function_name: function_name_value,
})
}

fn is_asterisk(s: &MoveAsciiString) -> bool {
Expand Down Expand Up @@ -104,7 +110,7 @@ impl FromStr for SessionScope {
let function_name = parts
.next()
.ok_or(anyhow::anyhow!("invalid session scope"))?;
Ok(Self::new(module_address, module_name, function_name))
Self::new(module_address, module_name, function_name)
}
}

Expand Down
27 changes: 27 additions & 0 deletions frameworks/bitcoin-move/doc/utxo.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
- [Resource `UTXO`](#0x4_utxo_UTXO)
- [Struct `UTXOSeal`](#0x4_utxo_UTXOSeal)
- [Resource `BitcoinUTXOStore`](#0x4_utxo_BitcoinUTXOStore)
- [Struct `CreatingUTXOEvent`](#0x4_utxo_CreatingUTXOEvent)
- [Struct `RemovingUTXOEvent`](#0x4_utxo_RemovingUTXOEvent)
- [Struct `TempState`](#0x4_utxo_TempState)
- [Constants](#@Constants_0)
- [Function `genesis_init`](#0x4_utxo_genesis_init)
Expand Down Expand Up @@ -41,6 +43,7 @@

<pre><code><b>use</b> <a href="">0x1::string</a>;
<b>use</b> <a href="">0x2::bag</a>;
<b>use</b> <a href="">0x2::event</a>;
<b>use</b> <a href="">0x2::object</a>;
<b>use</b> <a href="">0x2::simple_multimap</a>;
<b>use</b> <a href="">0x2::type_info</a>;
Expand Down Expand Up @@ -83,6 +86,30 @@ The UTXO Object



<a name="0x4_utxo_CreatingUTXOEvent"></a>

## Struct `CreatingUTXOEvent`

Event for creating UTXO


<pre><code><b>struct</b> <a href="utxo.md#0x4_utxo_CreatingUTXOEvent">CreatingUTXOEvent</a> <b>has</b> drop, store
</code></pre>



<a name="0x4_utxo_RemovingUTXOEvent"></a>

## Struct `RemovingUTXOEvent`

Event for remove UTXO


<pre><code><b>struct</b> <a href="utxo.md#0x4_utxo_RemovingUTXOEvent">RemovingUTXOEvent</a> <b>has</b> drop, store
</code></pre>



<a name="0x4_utxo_TempState"></a>

## Struct `TempState`
Expand Down
6 changes: 5 additions & 1 deletion frameworks/bitcoin-move/src/natives/ord/bitseed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use move_vm_types::values::Value;
use smallvec::smallvec;

use moveos_stdlib::natives::helpers::{make_module_natives, make_native};
use moveos_stdlib::natives::moveos_stdlib::wasm::E_CBOR_MARSHAL_FAILED;

#[derive(Debug, Clone)]
pub struct ArgsPackingGasParameters {
Expand Down Expand Up @@ -77,7 +78,10 @@ pub fn native_pack_inscribe_generate_args(
));

let mut top_buffer = Vec::new();
ciborium::into_writer(&ciborium::Value::Map(cbor_buffer_map_pair), &mut top_buffer).expect("");
match ciborium::into_writer(&ciborium::Value::Map(cbor_buffer_map_pair), &mut top_buffer) {
Ok(_) => {}
Err(_) => return Ok(NativeResult::err(gas_params.base, E_CBOR_MARSHAL_FAILED)),
}

let mut cost = gas_params.base;
let total_length = user_input_key.len()
Expand Down
159 changes: 147 additions & 12 deletions frameworks/moveos-stdlib/src/natives/moveos_stdlib/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ pub const E_VALUE_NOT_I32: u64 = 11;
pub const E_MEMORY_NOT_FOUND: u64 = 12;
pub const E_INCORRECT_LENGTH_OF_ARGS: u64 = 13;
pub const E_CBOR_UNMARSHAL_FAILED: u64 = 14;
pub const E_GET_INSTANCE_POOL_FAILED: u64 = 15;
pub const E_UNPACK_STRUCT_FAILED: u64 = 16;
pub const E_WASM_INSTANCE_CREATION_FAILED: u64 = 17;
pub const E_WASM_REMOVE_INSTANCE_FAILED: u64 = 18;

#[derive(Debug, Clone)]
pub struct WASMCreateInstanceGasParameters {
Expand All @@ -61,9 +65,31 @@ fn native_create_wasm_instance(
_ty_args: Vec<Type>,
mut args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
if args.len() != 1 {
return Ok(NativeResult::err(
gas_params.base_create_instance,
E_INCORRECT_LENGTH_OF_ARGS,
));
}
let wasm_bytes = pop_arg!(args, Vec<u8>);
let wasm_instance = create_wasm_instance(&wasm_bytes);
let instance_id = insert_wasm_instance(wasm_instance);
let wasm_instance = match create_wasm_instance(&wasm_bytes) {
Ok(v) => v,
Err(_) => {
return Ok(NativeResult::err(
gas_params.base_create_instance,
E_WASM_INSTANCE_CREATION_FAILED,
))
}
};
let instance_id = match insert_wasm_instance(wasm_instance) {
Ok(v) => v,
Err(_) => {
return Ok(NativeResult::err(
gas_params.base_create_instance,
E_WASM_INSTANCE_CREATION_FAILED,
))
}
};

let mut cost = gas_params.base_create_instance;
cost += gas_params.per_byte_instance * NumBytes::new(wasm_bytes.len() as u64);
Expand Down Expand Up @@ -97,6 +123,13 @@ fn native_create_cbor_values(
_ty_args: Vec<Type>,
mut args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
if args.len() != 1 {
return Ok(NativeResult::err(
gas_params.base,
E_INCORRECT_LENGTH_OF_ARGS,
));
}

let value_list = pop_arg!(args, Vec<Value>);

let mut func_args = Vec::new();
Expand Down Expand Up @@ -179,6 +212,13 @@ fn native_add_length_with_data(
_ty_args: Vec<Type>,
mut args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
if args.len() != 1 {
return Ok(NativeResult::err(
gas_params.base,
E_INCORRECT_LENGTH_OF_ARGS,
));
}

let mut data = pop_arg!(args, Vec<u8>);

let mut buffer_final = Vec::new();
Expand Down Expand Up @@ -219,6 +259,13 @@ fn native_create_wasm_args_in_memory(
_ty_args: Vec<Type>,
mut args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
if args.len() != 3 {
return Ok(NativeResult::err(
gas_params.base_create_args,
E_INCORRECT_LENGTH_OF_ARGS,
));
}

let func_args_value = pop_arg!(args, Vec<Value>);
let _func_name = pop_arg!(args, Vec<u8>); // TODO: check the length of function arguments
let instance_id = pop_arg!(args, u64);
Expand All @@ -241,7 +288,16 @@ fn native_create_wasm_args_in_memory(
let mut data_ptr_list = Vec::new();

let instance_pool = get_instance_pool();
match instance_pool.lock().unwrap().get_mut(&instance_id) {
let mut pool_object = match instance_pool.lock() {
Ok(v) => v,
Err(_) => {
return Ok(NativeResult::err(
gas_params.base_create_args,
E_GET_INSTANCE_POOL_FAILED,
))
}
};
match pool_object.get_mut(&instance_id) {
None => {
return Ok(NativeResult::err(
gas_params.base_create_args,
Expand All @@ -260,8 +316,16 @@ fn native_create_wasm_args_in_memory(
let mut arg_buffer = Vec::new();
// arg_buffer.append(&mut (arg.len() as u32).to_be_bytes().to_vec());
arg_buffer.append(&mut c_arg.into_bytes_with_nul());
let buffer_final_ptr =
put_data_on_stack(stack_alloc_func, &mut instance.store, arg_buffer.as_slice());
let buffer_final_ptr = match put_data_on_stack(
stack_alloc_func,
&mut instance.store,
arg_buffer.as_slice(),
) {
Ok(v) => v,
Err(_) => {
return build_err(gas_params.base_create_args, E_WASM_EXECUTION_FAILED)
}
};

data_ptr_list.push(buffer_final_ptr as u64);
args_bytes_total += arg.len();
Expand Down Expand Up @@ -306,12 +370,28 @@ fn native_execute_wasm_function(
_ty_args: Vec<Type>,
mut args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
if args.len() != 3 {
return Ok(NativeResult::err(
gas_params.base_create_execution,
E_INCORRECT_LENGTH_OF_ARGS,
));
}

let func_args = pop_arg!(args, Vec<u64>);
let func_name = pop_arg!(args, Vec<u8>);
let instance_id = pop_arg!(args, u64);

let instance_pool = get_instance_pool();
let ret = match instance_pool.lock().unwrap().get_mut(&instance_id) {
let mut pool_object = match instance_pool.lock() {
Ok(v) => v,
Err(_) => {
return Ok(NativeResult::err(
gas_params.base_create_execution,
E_GET_INSTANCE_POOL_FAILED,
))
}
};
let ret = match pool_object.get_mut(&instance_id) {
None => Ok(NativeResult::err(
gas_params.base_create_execution,
E_INSTANCE_NO_EXISTS,
Expand Down Expand Up @@ -400,11 +480,27 @@ fn native_read_data_length(
_ty_args: Vec<Type>,
mut args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
if args.len() != 2 {
return Ok(NativeResult::err(
gas_params.base,
E_INCORRECT_LENGTH_OF_ARGS,
));
}

let data_ptr = pop_arg!(args, u64);
let instance_id = pop_arg!(args, u64);

let instance_pool = get_instance_pool();
let ret = match instance_pool.lock().unwrap().get_mut(&instance_id) {
let mut pool_object = match instance_pool.lock() {
Ok(v) => v,
Err(_) => {
return Ok(NativeResult::err(
gas_params.base,
E_GET_INSTANCE_POOL_FAILED,
))
}
};
let ret = match pool_object.get_mut(&instance_id) {
None => Ok(NativeResult::err(gas_params.base, E_INSTANCE_NO_EXISTS)),
Some(instance) => {
let memory = match instance.instance.exports.get_memory("memory") {
Expand Down Expand Up @@ -456,12 +552,28 @@ fn native_read_data_from_heap(
_ty_args: Vec<Type>,
mut args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
if args.len() != 3 {
return Ok(NativeResult::err(
gas_params.base,
E_INCORRECT_LENGTH_OF_ARGS,
));
}

let data_length = pop_arg!(args, u32);
let data_ptr = pop_arg!(args, u32);
let instance_id = pop_arg!(args, u64);

let instance_pool = get_instance_pool();
let ret = match instance_pool.lock().unwrap().get_mut(&instance_id) {
let mut pool_object = match instance_pool.lock() {
Ok(v) => v,
Err(_) => {
return Ok(NativeResult::err(
gas_params.base,
E_GET_INSTANCE_POOL_FAILED,
))
}
};
let ret = match pool_object.get_mut(&instance_id) {
None => Ok(NativeResult::err(gas_params.base, E_INSTANCE_NO_EXISTS)),
Some(instance) => {
let memory = match instance.instance.exports.get_memory("memory") {
Expand Down Expand Up @@ -542,21 +654,44 @@ fn native_release_wasm_instance(
Some(v) => v,
None => return build_err(gas_params.base, E_INCORRECT_LENGTH_OF_ARGS),
};
let mut fiedls = value.value_as::<Struct>()?.unpack()?;
let val = fiedls.next().ok_or_else(|| {
let mut fields = match value.value_as::<Struct>() {
Ok(struct_) => match struct_.unpack() {
Ok(fields_iterator) => fields_iterator,
Err(_) => return Ok(NativeResult::err(gas_params.base, E_UNPACK_STRUCT_FAILED)),
},
Err(_) => return Ok(NativeResult::err(gas_params.base, E_UNPACK_STRUCT_FAILED)),
};
let val = fields.next().ok_or_else(|| {
PartialVMError::new(StatusCode::TYPE_RESOLUTION_FAILURE)
.with_message("There must have only one field".to_owned())
})?;

let instance_id = val.value_as::<u64>()?;

let instance_pool = get_instance_pool();
match instance_pool.lock().unwrap().get_mut(&instance_id) {
let mut pool_object = match instance_pool.lock() {
Ok(v) => v,
Err(_) => {
return Ok(NativeResult::err(
gas_params.base,
E_GET_INSTANCE_POOL_FAILED,
))
}
};
match pool_object.get_mut(&instance_id) {
None => return Ok(NativeResult::err(gas_params.base, E_INSTANCE_NO_EXISTS)),
Some(_) => {}
};

moveos_wasm::wasm::remove_instance(instance_id);
match moveos_wasm::wasm::remove_instance(instance_id) {
Ok(_) => {}
Err(_) => {
return Ok(NativeResult::err(
gas_params.base,
E_GET_INSTANCE_POOL_FAILED,
))
}
};

Ok(NativeResult::Success {
cost: gas_params.base,
Expand Down
3 changes: 2 additions & 1 deletion moveos/moveos-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ serde_json = { workspace = true }
once_cell = { workspace = true }
wasmer = { workspace = true }
ciborium = { workspace = true }
rand = { workspace = true }
rand = { workspace = true }
anyhow = { workspace = true }
Loading
Loading