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

Disallow globals in protocol 6. #398

Merged
merged 9 commits into from
Jun 26, 2023
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
6 changes: 5 additions & 1 deletion .diff-wat-wasm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ NO_CHECK_FILES=(
'./mut-global-offset-test.wat',

# This module is invalid because it tries to initialize a global value with the reference of another global value.
'./init-global-with-ref-test.wat'
'./init-global-with-ref-test.wat',

# Valid according to protocols P1-P5, not valid in P6 with stricter validation.
'./global-data-section-test.wat',
'./global-element-section-test.wat'
)

RET=0
Expand Down
Binary file not shown.
23 changes: 23 additions & 0 deletions smart-contracts/testdata/contracts/global-data-section-test.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
(module

;; This module used to be valid, but has then been
;; made invalid by stricter rules for appearance of globals
;; in data sections.
;;
;; To compile with wat2wasm, use the --no-check option.

(global $g0 i32 (i32.const 0))

(func (export "init_test") (param i64) (result i32)
(i32.const 0) ;; Successful init
)

(func (export "test.receive") (param i64) (result i32)
(i32.const 0) ;; success
)

;; This is the invalid part. Globals cannot be used for offsets in the data section.
(data (offset (global.get $g0)) "Hello, ")

(memory 1)
)
Binary file not shown.
24 changes: 24 additions & 0 deletions smart-contracts/testdata/contracts/global-element-section-test.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
(module

;; This module used to be valid, but has then been
;; made invalid by stricter rules for appearance of globals
;; in element sections.
;;
;; To compile with wat2wasm, use the --no-check option.

(global $g0 i32 (i32.const 0))

(func $init (export "init_test") (param i64) (result i32)
(i32.const 0) ;; Successful init
)

(func (export "test.receive") (param i64) (result i32)
(i32.const 0) ;; success
)

(table 1 funcref)
;; This is the invalid part. Globals cannot be used for offsets in the element section.
(elem (offset (global.get $g0)) $init)

(memory 1)
)
6 changes: 6 additions & 0 deletions smart-contracts/wasm-chain-integration/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## Unreleased changes

- Functions that process V1 smart contract modules
(`invoke_receive_*_from_source` and `invoke_init_*_from_source`) are now
parameterized by a `ValidationConfig` which determines which Wasm features are
allowed.


## concordium-smart-contract-engine 2.0.0 (2023-06-16)

- Bump concordium-contracts-common to version 7.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ use concordium_smart_contract_engine::{
},
InterpreterEnergy,
};
use concordium_wasm::{machine, parse, validate};
use concordium_wasm::{
machine, parse,
validate::{self, ValidationConfig},
};
use criterion::{black_box, criterion_group, criterion_main, BatchSize, Criterion};
use sha2::Digest;
use std::time::Duration;
Expand Down Expand Up @@ -67,6 +70,7 @@ pub fn criterion_benchmark(c: &mut Criterion) {
let skeleton = parse::parse_skeleton(black_box(CONTRACT_BYTES_HOST_FUNCTIONS)).unwrap();
let module = {
let mut module = validate::validate_module(
ValidationConfig::V1,
&ConcordiumAllowedImports {
support_upgrade: true,
},
Expand Down
89 changes: 69 additions & 20 deletions smart-contracts/wasm-chain-integration/benches/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use concordium_wasm::{
artifact::{ArtifactNamedImport, TryFromImport},
machine::{Host, NoInterrupt, Value},
types::{FunctionType, ValueType},
validate::ValidationConfig,
*,
};
use criterion::{black_box, criterion_group, criterion_main, Criterion};
Expand Down Expand Up @@ -157,7 +158,12 @@ pub fn criterion_benchmark(c: &mut Criterion) {
let skeleton =
parse::parse_skeleton(black_box(CONTRACT_BYTES_SIMPLE_GAME)).unwrap();
assert!(
validate::validate_module(&ConcordiumAllowedImports, &skeleton).is_ok(),
validate::validate_module(
ValidationConfig::V1,
&ConcordiumAllowedImports,
&skeleton
)
.is_ok(),
"Cannot validate module."
)
})
Expand All @@ -167,8 +173,12 @@ pub fn criterion_benchmark(c: &mut Criterion) {
b.iter(move || {
let skeleton =
parse::parse_skeleton(black_box(CONTRACT_BYTES_SIMPLE_GAME)).unwrap();
let mut module =
validate::validate_module(&ConcordiumAllowedImports, &skeleton).unwrap();
let mut module = validate::validate_module(
ValidationConfig::V1,
&ConcordiumAllowedImports,
&skeleton,
)
.unwrap();
assert!(module.inject_metering().is_ok(), "Metering injection failed.")
})
});
Expand All @@ -177,8 +187,12 @@ pub fn criterion_benchmark(c: &mut Criterion) {
b.iter(move || {
let skeleton =
parse::parse_skeleton(black_box(CONTRACT_BYTES_SIMPLE_GAME)).unwrap();
let mut module =
validate::validate_module(&ConcordiumAllowedImports, &skeleton).unwrap();
let mut module = validate::validate_module(
ValidationConfig::V1,
&ConcordiumAllowedImports,
&skeleton,
)
.unwrap();
module.inject_metering().unwrap();
assert!(module.compile::<ProcessedImports>().is_ok(), "Compilation failed.")
})
Expand All @@ -194,7 +208,11 @@ pub fn criterion_benchmark(c: &mut Criterion) {
group.bench_function("validate", |b| {
b.iter(|| {
let skeleton = parse::parse_skeleton(black_box(CONTRACT_BYTES_MINIMAL)).unwrap();
if let Err(e) = validate::validate_module(&ConcordiumAllowedImports, &skeleton) {
if let Err(e) = validate::validate_module(
ValidationConfig::V1,
&ConcordiumAllowedImports,
&skeleton,
) {
panic!("{}", e)
}
})
Expand All @@ -203,17 +221,25 @@ pub fn criterion_benchmark(c: &mut Criterion) {
group.bench_function("validate + inject metering", |b| {
b.iter(move || {
let skeleton = parse::parse_skeleton(black_box(CONTRACT_BYTES_MINIMAL)).unwrap();
let mut module =
validate::validate_module(&ConcordiumAllowedImports, &skeleton).unwrap();
let mut module = validate::validate_module(
ValidationConfig::V1,
&ConcordiumAllowedImports,
&skeleton,
)
.unwrap();
assert!(module.inject_metering().is_ok(), "Metering injection failed.")
})
});

group.bench_function("validate + inject metering + compile", |b| {
b.iter(move || {
let skeleton = parse::parse_skeleton(black_box(CONTRACT_BYTES_MINIMAL)).unwrap();
let mut module =
validate::validate_module(&ConcordiumAllowedImports, &skeleton).unwrap();
let mut module = validate::validate_module(
ValidationConfig::V1,
&ConcordiumAllowedImports,
&skeleton,
)
.unwrap();
module.inject_metering().unwrap();
assert!(module.compile::<ProcessedImports>().is_ok(), "Compilation failed.")
})
Expand All @@ -231,7 +257,12 @@ pub fn criterion_benchmark(c: &mut Criterion) {
b.iter(|| {
let skeleton = parse::parse_skeleton(black_box(CONTRACT_BYTES_COUNTER)).unwrap();
assert!(
validate::validate_module(&ConcordiumAllowedImports, &skeleton).is_ok(),
validate::validate_module(
ValidationConfig::V1,
&ConcordiumAllowedImports,
&skeleton
)
.is_ok(),
"Cannot validate module."
)
})
Expand All @@ -240,17 +271,25 @@ pub fn criterion_benchmark(c: &mut Criterion) {
group.bench_function("validate + inject metering", |b| {
b.iter(move || {
let skeleton = parse::parse_skeleton(black_box(CONTRACT_BYTES_COUNTER)).unwrap();
let mut module =
validate::validate_module(&ConcordiumAllowedImports, &skeleton).unwrap();
let mut module = validate::validate_module(
ValidationConfig::V1,
&ConcordiumAllowedImports,
&skeleton,
)
.unwrap();
assert!(module.inject_metering().is_ok(), "Metering injection failed.")
})
});

group.bench_function("validate + inject metering + compile", |b| {
b.iter(move || {
let skeleton = parse::parse_skeleton(black_box(CONTRACT_BYTES_COUNTER)).unwrap();
let mut module =
validate::validate_module(&ConcordiumAllowedImports, &skeleton).unwrap();
let mut module = validate::validate_module(
ValidationConfig::V1,
&ConcordiumAllowedImports,
&skeleton,
)
.unwrap();
module.inject_metering().unwrap();
assert!(module.compile::<ProcessedImports>().is_ok(), "Compilation failed.")
})
Expand All @@ -266,7 +305,9 @@ pub fn criterion_benchmark(c: &mut Criterion) {
group.measurement_time(Duration::from_secs(10));

let skeleton = parse::parse_skeleton(black_box(CONTRACT_BYTES_INSTRUCTIONS)).unwrap();
let module = validate::validate_module(&TestHost::uninitialized(), &skeleton).unwrap();
let module =
validate::validate_module(ValidationConfig::V1, &TestHost::uninitialized(), &skeleton)
.unwrap();
let artifact = module.compile::<ArtifactNamedImport>().unwrap();
for n in [0, 1, 10000, 100000, 200000].iter() {
group.bench_with_input(format!("execute n = {}", n), n, |b, m| {
Expand All @@ -283,7 +324,9 @@ pub fn criterion_benchmark(c: &mut Criterion) {

let skeleton =
parse::parse_skeleton(black_box(CONTRACT_BYTES_MEMORY_INSTRUCTIONS)).unwrap();
let module = validate::validate_module(&TestHost::uninitialized(), &skeleton).unwrap();
let module =
validate::validate_module(ValidationConfig::V1, &TestHost::uninitialized(), &skeleton)
.unwrap();
let artifact = module.compile::<ArtifactNamedImport>().unwrap();
for n in [1, 10, 50, 100, 250, 500, 1000, 1024].iter() {
group.bench_with_input(format!("allocate n = {} pages", n), n, |b, m| {
Expand Down Expand Up @@ -374,7 +417,9 @@ pub fn criterion_benchmark(c: &mut Criterion) {
.throughput(criterion::Throughput::Elements(nrg));

let skeleton = parse::parse_skeleton(black_box(CONTRACT_BYTES_LOOP)).unwrap();
let mut module = validate::validate_module(&TestHost::uninitialized(), &skeleton).unwrap();
let mut module =
validate::validate_module(ValidationConfig::V1, &TestHost::uninitialized(), &skeleton)
.unwrap();
module.inject_metering().unwrap();
let artifact = module.compile::<MeteringImport>().unwrap();

Expand Down Expand Up @@ -510,8 +555,12 @@ pub fn criterion_benchmark(c: &mut Criterion) {

let skeleton = parse::parse_skeleton(black_box(CONTRACT_BYTES_HOST_FUNCTIONS)).unwrap();
let module = {
let mut module =
validate::validate_module(&ConcordiumAllowedImports, &skeleton).unwrap();
let mut module = validate::validate_module(
ValidationConfig::V1,
&ConcordiumAllowedImports,
&skeleton,
)
.unwrap();
module.inject_metering().expect("Metering injection should succeed.");
module
};
Expand Down
18 changes: 12 additions & 6 deletions smart-contracts/wasm-chain-integration/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use concordium_wasm::{
machine::{self, NoInterrupt, Value},
parse::{parse_custom, parse_skeleton},
types::{ExportDescription, Module, Name},
utils, validate,
utils,
validate::{self, ValidationConfig},
};
use rand::{prelude::*, RngCore};
use std::{collections::BTreeMap, default::Default};
Expand Down Expand Up @@ -221,7 +222,8 @@ type TestResult = (String, Option<(ReportError, bool)>);
/// The return value is a list of test results.
pub fn run_module_tests(module_bytes: &[u8], seed: u64) -> ExecResult<Vec<TestResult>> {
let host = TestHost::new(SmallRng::seed_from_u64(seed));
let artifact = utils::instantiate::<ArtifactNamedImport, _>(&host, module_bytes)?;
let artifact =
utils::instantiate::<ArtifactNamedImport, _>(ValidationConfig::V1, &host, module_bytes)?;
let mut out = Vec::with_capacity(artifact.export.len());
for name in artifact.export.keys() {
if let Some(test_name) = name.as_ref().strip_prefix("concordium_test ") {
Expand Down Expand Up @@ -257,7 +259,8 @@ pub fn generate_contract_schema_v0(
module_bytes: &[u8],
) -> ExecResult<schema::VersionedModuleSchema> {
let host = TestHost::uninitialized(); // The RNG is not relevant for schema generation
let artifact = utils::instantiate::<ArtifactNamedImport, _>(&host, module_bytes)?;
let artifact =
utils::instantiate::<ArtifactNamedImport, _>(ValidationConfig::V0, &host, module_bytes)?;

let mut contract_schemas = BTreeMap::new();

Expand Down Expand Up @@ -311,7 +314,8 @@ pub fn generate_contract_schema_v1(
module_bytes: &[u8],
) -> ExecResult<schema::VersionedModuleSchema> {
let host = TestHost::uninitialized(); // The RNG is not relevant for schema generation
let artifact = utils::instantiate::<ArtifactNamedImport, _>(&host, module_bytes)?;
let artifact =
utils::instantiate::<ArtifactNamedImport, _>(ValidationConfig::V1, &host, module_bytes)?;

let mut contract_schemas = BTreeMap::new();

Expand Down Expand Up @@ -355,7 +359,8 @@ pub fn generate_contract_schema_v2(
module_bytes: &[u8],
) -> ExecResult<schema::VersionedModuleSchema> {
let host = TestHost::uninitialized(); // The RNG is not relevant for schema generation
let artifact = utils::instantiate::<ArtifactNamedImport, _>(&host, module_bytes)?;
let artifact =
utils::instantiate::<ArtifactNamedImport, _>(ValidationConfig::V1, &host, module_bytes)?;

let mut contract_schemas = BTreeMap::new();

Expand Down Expand Up @@ -399,7 +404,8 @@ pub fn generate_contract_schema_v3(
module_bytes: &[u8],
) -> ExecResult<schema::VersionedModuleSchema> {
let host = TestHost::uninitialized(); // The RNG is not relevant for schema generation
let artifact = utils::instantiate::<ArtifactNamedImport, _>(&host, module_bytes)?;
let artifact =
utils::instantiate::<ArtifactNamedImport, _>(ValidationConfig::V1, &host, module_bytes)?;

let mut contract_schemas = BTreeMap::new();

Expand Down
6 changes: 5 additions & 1 deletion smart-contracts/wasm-chain-integration/src/v0/ffi.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::v0::*;
use concordium_wasm::{artifact::CompiledFunctionBytes, output::Output, utils::parse_artifact};
use concordium_wasm::{
artifact::CompiledFunctionBytes, output::Output, utils::parse_artifact,
validate::ValidationConfig,
};
use libc::size_t;

/// All functions in this module operate on serialized artifact bytes. For
Expand Down Expand Up @@ -188,6 +191,7 @@ unsafe extern "C" fn validate_and_process_v0(
) -> *mut u8 {
let wasm_bytes = slice_from_c_bytes!(wasm_bytes_ptr, wasm_bytes_len);
match utils::instantiate_with_metering::<ProcessedImports, _>(
ValidationConfig::V0,
&ConcordiumAllowedImports,
wasm_bytes,
) {
Expand Down
Loading