Fix zero-len slice translations
This commit is contained in:
@ -83,6 +83,16 @@ fn process_instruction(
|
|||||||
invoke(&instruction, accounts)?;
|
invoke(&instruction, accounts)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
info!("Test no instruction data");
|
||||||
|
{
|
||||||
|
let instruction = create_instruction(
|
||||||
|
*accounts[INVOKED_PROGRAM_INDEX].key,
|
||||||
|
&[(accounts[ARGUMENT_INDEX].key, true, true)],
|
||||||
|
vec![],
|
||||||
|
);
|
||||||
|
invoke(&instruction, accounts)?;
|
||||||
|
}
|
||||||
|
|
||||||
info!("Test return error");
|
info!("Test return error");
|
||||||
{
|
{
|
||||||
let instruction = create_instruction(
|
let instruction = create_instruction(
|
||||||
|
@ -26,6 +26,10 @@ fn process_instruction(
|
|||||||
) -> ProgramResult {
|
) -> ProgramResult {
|
||||||
info!("Invoked program");
|
info!("Invoked program");
|
||||||
|
|
||||||
|
if instruction_data.is_empty() {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
match instruction_data[0] {
|
match instruction_data[0] {
|
||||||
TEST_VERIFY_TRANSLATIONS => {
|
TEST_VERIFY_TRANSLATIONS => {
|
||||||
info!("verify data translations");
|
info!("verify data translations");
|
||||||
|
@ -169,6 +169,9 @@ macro_rules! translate_type {
|
|||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! translate_slice_mut {
|
macro_rules! translate_slice_mut {
|
||||||
($t:ty, $vm_addr:expr, $len: expr, $regions:expr) => {
|
($t:ty, $vm_addr:expr, $len: expr, $regions:expr) => {
|
||||||
|
if $len == 0 {
|
||||||
|
Ok(unsafe { from_raw_parts_mut(0x1 as *mut $t, $len as usize) })
|
||||||
|
} else {
|
||||||
match translate_addr::<BPFError>(
|
match translate_addr::<BPFError>(
|
||||||
$vm_addr as u64,
|
$vm_addr as u64,
|
||||||
$len as usize * size_of::<$t>(),
|
$len as usize * size_of::<$t>(),
|
||||||
@ -179,6 +182,7 @@ macro_rules! translate_slice_mut {
|
|||||||
Ok(value) => Ok(unsafe { from_raw_parts_mut(value as *mut $t, $len as usize) }),
|
Ok(value) => Ok(unsafe { from_raw_parts_mut(value as *mut $t, $len as usize) }),
|
||||||
Err(e) => Err(e),
|
Err(e) => Err(e),
|
||||||
}
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
@ -935,6 +939,20 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_translate_slice() {
|
fn test_translate_slice() {
|
||||||
|
// zero len
|
||||||
|
let good_data = vec![1u8, 2, 3, 4, 5];
|
||||||
|
let data: Vec<u8> = vec![];
|
||||||
|
assert_eq!(0x1 as *const u8, data.as_ptr());
|
||||||
|
let addr = good_data.as_ptr() as *const _ as u64;
|
||||||
|
let regions = vec![MemoryRegion {
|
||||||
|
addr_host: addr,
|
||||||
|
addr_vm: 100,
|
||||||
|
len: good_data.len() as u64,
|
||||||
|
}];
|
||||||
|
let translated_data = translate_slice!(u8, data.as_ptr(), data.len(), ®ions).unwrap();
|
||||||
|
assert_eq!(data, translated_data);
|
||||||
|
assert_eq!(0, translated_data.len());
|
||||||
|
|
||||||
// u8
|
// u8
|
||||||
let mut data = vec![1u8, 2, 3, 4, 5];
|
let mut data = vec![1u8, 2, 3, 4, 5];
|
||||||
let addr = data.as_ptr() as *const _ as u64;
|
let addr = data.as_ptr() as *const _ as u64;
|
||||||
|
Reference in New Issue
Block a user