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

reject imported memories early #8146

Merged
merged 2 commits into from
Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
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));
Copy link
Collaborator

@nagisa nagisa Dec 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So in the past the preparation would pass, and then the code would get passed into Wasmer2VM::compile_uncached() which then returned WasmerCompileError { ... } rather than a PrepareError::Memory.

This then moves on to be encoded into, I believe, the chain as the error message. At least that’s what I think matklad has explained to me in the past. So in the end this is still a protocol change, isn’t it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on the chat here, I think it isn’t :)


// 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