Skip to content

Commit

Permalink
[language][test-generation] remove diem dependencies
Browse files Browse the repository at this point in the history
This makes test-generation no longer depend on diem crates.
Specifically, DiemVM is replaced with MoveVM and diem-logger is replaced
with tracing.

Closes: diem#8631
  • Loading branch information
vgao1996 authored and bors-libra committed Jun 30, 2021
1 parent 18dae65 commit 21ea569
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 48 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions language/testing-infra/test-generation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ itertools = "0.10.0"
hex = "0.4.3"
getrandom = "0.2.2"
crossbeam-channel = "0.5.0"
tracing = "0.1.26"
tracing-subscriber = "0.2.18"
once_cell = "1.7.2"

bytecode-verifier = { path = "../../bytecode-verifier" }
diem-config = { path = "../../../config" }
diem-logger = { path = "../../../common/logger" }
diem-state-view = { path = "../../../storage/state-view" }
diem-workspace-hack = { path = "../../../common/workspace-hack" }
move-core-types = { path = "../../move-core/types" }
move-vm-runtime = { path = "../../move-vm/runtime" }
move-vm-test-utils = { path = "../../move-vm/test-utils" }
move-vm-types = { path = "../../move-vm/types" }
module-generation = { path = "../module-generation" }
move-binary-format = { path = "../../move-binary-format" }
diem-vm = { path = "../../diem-vm" }
language-e2e-tests = { path = "../e2e-tests" }
diem-types = { path = "../../../types" }
move-stdlib = { path = "../../move-stdlib" }
move-lang = { path = "../../move-lang" }

[features]
mirai-contracts = []
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::{
control_flow_graph::CFG,
substitute, summaries,
};
use diem_logger::{debug, error, warn};
use move_binary_format::{
access::ModuleAccess,
file_format::{
Expand All @@ -21,6 +20,7 @@ use move_binary_format::{
},
};
use rand::{rngs::StdRng, Rng};
use tracing::{debug, error, warn};

/// This type represents bytecode instructions that take a `LocalIndex`
type LocalIndexToBytecode = fn(LocalIndex) -> Bytecode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
// SPDX-License-Identifier: Apache-2.0

use crate::abstract_state::{AbstractValue, BorrowState};
use diem_logger::debug;
use move_binary_format::file_format::{AbilitySet, Bytecode, Signature, SignatureToken};
use rand::{rngs::StdRng, Rng};
use std::collections::{HashMap, VecDeque};
use tracing::debug;

/// This type holds basic block identifiers
type BlockIDSize = u16;
Expand Down
77 changes: 48 additions & 29 deletions language/testing-infra/test-generation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@ use crate::config::{Args, EXECUTE_UNVERIFIED_MODULE, RUN_ON_VM};
use bytecode_generator::BytecodeGenerator;
use bytecode_verifier::verify_module;
use crossbeam_channel::{bounded, unbounded, Receiver, Sender};
use diem_logger::{debug, error, info};
use diem_state_view::StateView;
use diem_types::{account_address::AccountAddress, vm_status::StatusCode};
use diem_vm::DiemVM;
use getrandom::getrandom;
use language_e2e_tests::executor::FakeExecutor;
use module_generation::generate_module;
use move_binary_format::{
access::ModuleAccess,
Expand All @@ -33,10 +28,21 @@ use move_binary_format::{
StructHandleIndex,
},
};
use move_core_types::{language_storage::TypeTag, value::MoveValue, vm_status::VMStatus};
use move_core_types::{
account_address::AccountAddress,
effects::ChangeSet,
language_storage::TypeTag,
value::MoveValue,
vm_status::{StatusCode, VMStatus},
};
use move_lang::{compiled_unit::CompiledUnit, Compiler};
use move_vm_runtime::{data_cache::MoveStorage, move_vm::MoveVM};
use move_vm_test_utils::{DeltaStorage, InMemoryStorage};
use move_vm_types::gas_schedule::GasStatus;
use once_cell::sync::Lazy;
use rand::{rngs::StdRng, Rng, SeedableRng};
use std::{fs, io::Write, panic, thread};
use tracing::{debug, error, info};

/// This function calls the Bytecode verifier to test it
fn run_verifier(module: CompiledModule) -> Result<CompiledModule, String> {
Expand All @@ -49,6 +55,23 @@ fn run_verifier(module: CompiledModule) -> Result<CompiledModule, String> {
}
}

static STORAGE_WITH_MOVE_STDLIB: Lazy<InMemoryStorage> = Lazy::new(|| {
let mut storage = InMemoryStorage::new();
let (_, compiled_units) = Compiler::new(&move_stdlib::move_stdlib_files(), &[])
.build_and_report()
.unwrap();
let compiled_modules = compiled_units.into_iter().map(|unit| match unit {
CompiledUnit::Module { module, .. } => module,
CompiledUnit::Script { .. } => panic!("Unexpected Script in stdlib"),
});
for module in compiled_modules {
let mut blob = vec![];
module.serialize(&mut blob).unwrap();
storage.publish_or_overwrite_module(module.self_id(), blob);
}
storage
});

/// This function runs a verified module in the VM runtime
fn run_vm(module: CompiledModule) -> Result<(), VMStatus> {
// By convention the 0'th index function definition is the entrypoint to the module (i.e. that
Expand All @@ -75,23 +98,22 @@ fn run_vm(module: CompiledModule) -> Result<(), VMStatus> {
})
.collect();

let executor = FakeExecutor::from_genesis_file();
execute_function_in_module(
module,
entry_idx,
vec![],
main_args,
executor.get_state_view(),
&*STORAGE_WITH_MOVE_STDLIB,
)
}

/// Execute the first function in a module
fn execute_function_in_module<S: StateView>(
fn execute_function_in_module(
module: CompiledModule,
idx: FunctionDefinitionIndex,
ty_args: Vec<TypeTag>,
args: Vec<Vec<u8>>,
state_view: &S,
storage: &impl MoveStorage,
) -> Result<(), VMStatus> {
let module_id = module.self_id();
let entry_name = {
Expand All @@ -100,25 +122,22 @@ fn execute_function_in_module<S: StateView>(
module.identifier_at(entry_name_idx)
};
{
let diem_vm = DiemVM::new(state_view);

let internals = diem_vm.internals();

internals.with_txn_data_cache(state_view, |mut txn_context| {
let sender = AccountAddress::random();
let mut mod_blob = vec![];
module
.serialize(&mut mod_blob)
.expect("Module serialization error");
let mut gas_status = GasStatus::new_unmetered();
txn_context
.publish_module(mod_blob, sender, &mut gas_status)
.map_err(|e| e.into_vm_status())?;
txn_context
.execute_function(&module_id, &entry_name, ty_args, args, &mut gas_status)
.map_err(|e| e.into_vm_status())?;
Ok(())
})
let vm = MoveVM::new(move_stdlib::natives::all_natives(
AccountAddress::from_hex_literal("0x1").unwrap(),
))
.unwrap();

let mut changeset = ChangeSet::new();
let mut blob = vec![];
module.serialize(&mut blob).unwrap();
changeset.publish_or_overwrite_module(module_id.clone(), blob);
let delta_storage = DeltaStorage::new(storage, &changeset);
let mut sess = vm.new_session(&delta_storage);

let mut gas_status = GasStatus::new_unmetered();
sess.execute_function(&module_id, entry_name, ty_args, args, &mut gas_status)?;

Ok(())
}
}

Expand Down
6 changes: 1 addition & 5 deletions language/testing-infra/test-generation/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@

#![forbid(unsafe_code)]

use std::env;
use structopt::StructOpt;
use test_generation::{config::Args, run_generation};

fn setup_log() {
if env::var("RUST_LOG").is_err() {
env::set_var("RUST_LOG", "info");
}
::diem_logger::Logger::new().init();
tracing::subscriber::set_global_default(tracing_subscriber::FmtSubscriber::new()).unwrap();
}

pub fn main() {
Expand Down

0 comments on commit 21ea569

Please sign in to comment.