Skip to content

Commit

Permalink
rust lint
Browse files Browse the repository at this point in the history
  • Loading branch information
samliok committed Dec 10, 2023
1 parent cc96b79 commit a17803d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 42 deletions.
21 changes: 6 additions & 15 deletions x/programs/rust/wasmlanche_sdk/src/host/program.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! The `program` module provides functions for calling other programs.
use crate::memory::to_smart_ptr;
use crate::program::Program;
use crate::memory::to_ptr_arg;

#[link(wasm_import_module = "program")]
extern "C" {
Expand All @@ -23,21 +23,12 @@ pub(crate) fn call(
function_name: &str,
args: &[u8],
) -> i64 {

let caller_id = caller.id();
let caller = to_ptr_arg(&caller_id).unwrap();
let caller = to_smart_ptr(&caller_id).unwrap();
let target_id = target.id();
let target = to_ptr_arg(&target_id).unwrap();
let function = to_ptr_arg(function_name.as_bytes()).unwrap();
let args = to_ptr_arg(args).unwrap();
let target = to_smart_ptr(&target_id).unwrap();
let function = to_smart_ptr(function_name.as_bytes()).unwrap();
let args = to_smart_ptr(args).unwrap();

unsafe {
_call_program(
caller,
target,
max_units,
function,
args,
)
}
unsafe { _call_program(caller, target, max_units, function, args) }
}
12 changes: 6 additions & 6 deletions x/programs/rust/wasmlanche_sdk/src/host/state.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! The `state` module provides functions for interacting with persistent
//! storage exposed by the host.
use crate::errors::StateError;
use crate::memory::to_ptr_arg;
use crate::memory::to_smart_ptr;
use crate::{program::Program, state::Key};
use borsh::{to_vec, BorshSerialize};

Expand All @@ -22,9 +22,9 @@ where
let value_bytes = to_vec(value).map_err(|_| StateError::Serialization)?;
// prepend length to both key & value
let caller_id = caller.id();
let caller = to_ptr_arg(&caller_id)?;
let value = to_ptr_arg(&value_bytes)?;
let key = to_ptr_arg(key)?;
let caller = to_smart_ptr(&caller_id)?;
let value = to_smart_ptr(&value_bytes)?;
let key = to_smart_ptr(key)?;

match unsafe { _put(caller, key, value) } {
0 => Ok(()),
Expand All @@ -36,7 +36,7 @@ where
pub(crate) unsafe fn get_bytes(caller: &Program, key: &Key) -> i64 {
// prepend length to key
let caller_id = caller.id();
let caller = to_ptr_arg(&caller_id).unwrap();
let key = to_ptr_arg(key).unwrap();
let caller = to_smart_ptr(&caller_id).unwrap();
let key = to_smart_ptr(key).unwrap();
unsafe { _get(caller, key) }
}
57 changes: 36 additions & 21 deletions x/programs/rust/wasmlanche_sdk/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,42 @@ impl Memory {
}
}

// Converts a pointer to a i64 with the first 4 bytes of the pointer
// representing the length of the memory block.
pub fn to_ptr_arg(arg: &[u8]) -> Result<i64, StateError> {
let mut ptr =
u32::try_from(arg.as_ptr() as i64).map_err(|_| StateError::IntegerConversion)? as i64;
let len = u32::try_from(arg.len()).map_err(|_| StateError::IntegerConversion)? as i64;
ptr |= len << 32;
Ok(ptr)
/// `SmartPtr` is an i64 where the first 4 bytes represent the length of the bytes
/// and the following 4 bytes represent a pointer to WASM memeory where the bytes are stored.
pub type SmartPtr = i64;

/// Converts a pointer to a i64 with the first 4 bytes of the pointer
/// representing the length of the memory block.
/// # Errors
/// Returns an `StateError` if the pointer or length of [args] exceeds
/// the maximum size of a u32.
#[allow(clippy::cast_possible_truncation)]
pub fn to_smart_ptr(arg: &[u8]) -> Result<SmartPtr, StateError> {
let ptr = arg.as_ptr() as usize;
let len = arg.len();

// Make sure the pointer and length fit into u32
if ptr > u32::MAX as usize || len > u32::MAX as usize {
return Err(StateError::IntegerConversion);
}

let smart_ptr = i64::from(ptr as u32) | (i64::from(len as u32) << 32);
Ok(smart_ptr)
}

// Converts a i64 to a pointer with the first 4 bytes of the pointer
// representing the length of the memory block.
pub fn from_ptr_arg(arg: i64) -> (i64, usize) {
let len = (arg >> 32) as usize;
/// Converts a i64 to a pointer with the first 4 bytes of the pointer
/// representing the length of the memory block.
/// # Panics
/// Panics if arg is negative.
#[must_use]
#[allow(clippy::cast_sign_loss)]
pub fn from_smart_ptr(arg: SmartPtr) -> (i64, usize) {
assert!(arg >= 0);

let len = arg >> 32;
let mask: u32 = !0;
let ptr = arg & (mask as i64);
(ptr, len)
let ptr = arg & i64::from(mask);
(ptr, len as usize)
}

/// Converts a raw pointer to a deserialized value.
Expand All @@ -108,17 +127,13 @@ where
}

/// Returns a tuple of the bytes and length of the argument.
/// PtrArg is encoded using Big Endian as an i64.
/// The first 32 bits representing the length of the bytes and
/// the last 32 representing the ptr.
/// # Panics
/// Panics if the value cannot be converted from i32 to usize.
/// `smart_ptr` is encoded using Big Endian as an i64.
/// # Safety
/// This function is unsafe because it dereferences raw pointers.
#[must_use]
pub unsafe fn bytes_and_length(ptr_arg: i64) -> (Vec<u8>, usize) {
pub unsafe fn bytes_and_length(smart_ptr: i64) -> (Vec<u8>, usize) {
// grab length from ptrArg
let (ptr, len) = from_ptr_arg(ptr_arg);
let (ptr, len) = from_smart_ptr(smart_ptr);
let value = unsafe { std::slice::from_raw_parts(ptr as *const u8, len) };
(value.to_vec(), len)
}
Expand Down

0 comments on commit a17803d

Please sign in to comment.