Skip to content

Commit

Permalink
reject imported memories early
Browse files Browse the repository at this point in the history
They were already rejected by wasmer2 before this change, just after
preparation. So this should not be a protocol change.
  • Loading branch information
Ekleog-NEAR committed Dec 7, 2022
1 parent 1f28c55 commit 58882e5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
28 changes: 6 additions & 22 deletions runtime/near-vm-runner/src/prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ pub fn prepare_contract(original_code: &[u8], config: &VMConfig) -> Result<Vec<u
// See `test_stack_instrumentation_protocol_upgrade` test.
near_vm_logic::StackLimiterVersion::V0 => pwasm_12::prepare_contract(original_code, config),
near_vm_logic::StackLimiterVersion::V1 => ContractModule::init(original_code, config)?
.scan_imports()?
.standardize_mem()
.ensure_no_internal_memory()?
.inject_gas_metering()?
.inject_stack_height_metering()?
.scan_imports()?
.into_wasm_code(),
}
}
Expand Down Expand Up @@ -212,8 +212,6 @@ impl<'a> ContractModule<'a> {
let import_entries =
module.import_section().map(elements::ImportSection::entries).unwrap_or(&[]);

let mut imported_mem_type = None;

for import in import_entries {
if import.module() != "env" {
// This import tries to import something from non-"env" module,
Expand All @@ -223,10 +221,7 @@ impl<'a> ContractModule<'a> {

let type_idx = match *import.external() {
External::Function(ref type_idx) => type_idx,
External::Memory(ref memory_type) => {
imported_mem_type = Some(memory_type);
continue;
}
External::Memory(_) => return Err(PrepareError::Memory),
_ => continue,
};

Expand All @@ -245,17 +240,6 @@ impl<'a> ContractModule<'a> {
}
*/
}
if let Some(memory_type) = imported_mem_type {
// Inspect the module to extract the initial and maximum page count.
let limits = memory_type.limits();
if limits.initial() != config.limit_config.initial_memory_pages
|| limits.maximum() != Some(config.limit_config.max_memory_pages)
{
return Err(PrepareError::Memory);
}
} else {
return Err(PrepareError::Memory);
};
Ok(Self { module, config })
}

Expand Down Expand Up @@ -443,12 +427,12 @@ mod tests {
}

#[test]
fn memory() {
fn memory_imports() {
// This test assumes that maximum page number is configured to a certain number.
assert_eq!(VMConfig::test().limit_config.max_memory_pages, 2048);

let r = parse_and_prepare_wat(r#"(module (import "env" "memory" (memory 1 1)))"#);
assert_matches!(r, Ok(_));
assert_matches!(r, Err(PrepareError::Memory));

// No memory import
let r = parse_and_prepare_wat(r#"(module)"#);
Expand All @@ -460,11 +444,11 @@ mod tests {

// no maximum
let r = parse_and_prepare_wat(r#"(module (import "env" "memory" (memory 1)))"#);
assert_matches!(r, Ok(_));
assert_matches!(r, Err(PrepareError::Memory));

// requested maximum exceed configured maximum
let r = parse_and_prepare_wat(r#"(module (import "env" "memory" (memory 1 33)))"#);
assert_matches!(r, Ok(_));
assert_matches!(r, Err(PrepareError::Memory));
}

#[test]
Expand Down
24 changes: 23 additions & 1 deletion runtime/near-vm-runner/src/tests/runtime_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn test_simple_contract() {
}

#[test]
fn test_multiple_memories() {
fn test_imported_memory() {
test_builder()
.wasm(&[
0, 97, 115, 109, 1, 0, 0, 0, 2, 12, 1, 3, 101, 110, 118, 0, 2, 1, 239, 1, 248, 1, 4, 6,
Expand All @@ -77,6 +77,28 @@ fn test_multiple_memories() {
]);
}

#[test]
fn test_multiple_memories() {
test_builder()
.wat("(module (memory 1 2) (memory 3 4))")
.opaque_error()
.protocol_features(&[
#[cfg(feature = "protocol_feature_fix_contract_loading_cost")]
ProtocolFeature::FixContractLoadingCost,
])
.expects(&[
expect![[r#"
VMOutcome: balance 4 storage_usage 12 return data None burnt gas 0 used gas 0
Err: ...
"#]],
#[cfg(feature = "protocol_feature_fix_contract_loading_cost")]
expect![[r#"
VMOutcome: balance 4 storage_usage 12 return data None burnt gas 39130713 used gas 39130713
Err: ...
"#]],
]);
}

#[test]
fn test_export_not_found() {
test_builder().wat(SIMPLE_CONTRACT)
Expand Down

0 comments on commit 58882e5

Please sign in to comment.