Skip to content
This repository has been archived by the owner on Mar 20, 2024. It is now read-only.

Commit

Permalink
Fix ABI of move_rt_str_cmp_eq and move_rt_deserialize (#393)
Browse files Browse the repository at this point in the history
  • Loading branch information
brson authored Nov 8, 2023
1 parent d237c03 commit 22cfcca
Showing 1 changed file with 34 additions and 9 deletions.
43 changes: 34 additions & 9 deletions language/move-native/src/rt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,17 @@ unsafe extern "C" fn vec_cmp_eq(
}

#[export_name = "move_rt_str_cmp_eq"]
unsafe fn str_cmp_eq(s1: &str, s2: &str) -> bool {
*s1 == *s2
unsafe extern "C" fn str_cmp_eq(
s1_ptr: *const u8,
s1_len: u64,
s2_ptr: *const u8,
s2_len: u64,
) -> bool {
let s1 = core::slice::from_raw_parts(s1_ptr, usize::try_from(s1_len).expect("usize"));
let s1 = core::str::from_utf8_unchecked(s1); // assume utf8
let s2 = core::slice::from_raw_parts(s2_ptr, usize::try_from(s2_len).expect("usize"));
let s2 = core::str::from_utf8_unchecked(s2); // assume utf8
s1 == s2
}

#[export_name = "move_rt_struct_cmp_eq"]
Expand All @@ -55,10 +64,23 @@ unsafe extern "C" fn struct_cmp_eq(type_ve: &MoveType, s1: &AnyValue, s2: &AnyVa
}

/// Maximum number of bytes a program may add to an account during a single realloc
pub const MAX_PERMITTED_DATA_INCREASE: usize = 1_024 * 10;
const MAX_PERMITTED_DATA_INCREASE: usize = 1_024 * 10;

/// `assert_eq(std::mem::align_of::<u128>(), 8)` is true for BPF but not for some host machines
pub const BPF_ALIGN_OF_U128: usize = 8;
const BPF_ALIGN_OF_U128: usize = 8;

#[repr(C)]
struct DeserializeResult<'a> {
instruction_data: Slice,
program_id: &'a SolanaPubkey,
account_info: MoveUntypedVector, // of SolanaAccountInfo
}

#[repr(C)]
struct Slice {
ptr: *const u8,
len: u64,
}

/// Deserialize the input arguments in encoded in borsh
/// https://github.com/solana-labs/solana/blob/master/sdk/program/src/lib.rs
Expand All @@ -77,7 +99,7 @@ pub const BPF_ALIGN_OF_U128: usize = 8;
#[allow(clippy::arithmetic_side_effects)]
#[allow(clippy::type_complexity)]
#[export_name = "move_rt_deserialize"]
pub unsafe fn deserialize<'a>(input: *mut u8) -> (&'a [u8], &'a SolanaPubkey, MoveUntypedVector) {
unsafe extern "C" fn deserialize<'a>(input: *mut u8) -> DeserializeResult<'a> {
use alloc::vec::Vec;
use core::mem::size_of;
let mut offset: usize = 0;
Expand Down Expand Up @@ -164,9 +186,12 @@ pub unsafe fn deserialize<'a>(input: *mut u8) -> (&'a [u8], &'a SolanaPubkey, Mo

let program_id: &SolanaPubkey = &*(input.add(offset) as *const SolanaPubkey);

(
instruction_data,
DeserializeResult {
instruction_data: Slice {
ptr: instruction_data.as_ptr(),
len: u64::try_from(instruction_data.len()).expect("u64"),
},
program_id,
MoveUntypedVector::from_rust_vec::<SolanaAccountInfo>(accounts),
)
account_info: MoveUntypedVector::from_rust_vec::<SolanaAccountInfo>(accounts),
}
}

0 comments on commit 22cfcca

Please sign in to comment.