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

move core to no_std #33

Closed
wants to merge 6 commits into from
Closed
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
17 changes: 15 additions & 2 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions language/move-binary-format/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ ref-cast = "1.0.6"
variant_count = "1.1.0"
move-core-types = { path = "../move-core/types", version = "0.0.4" }
serde = { version = "1.0.124", default-features = false }
hashbrown = "0.12.3"
core2 = "0.3"

[dev-dependencies]
proptest = "1.0.0"
Expand All @@ -28,3 +30,6 @@ serde_json = "1.0.64"
[features]
default = []
fuzzing = ["proptest", "proptest-derive", "move-core-types/fuzzing"]
nostd = [
"move-core-types/nostd"
]
1 change: 1 addition & 0 deletions language/move-binary-format/serializer-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ move-binary-format = { path = "../", features = ["fuzzing"] }

[features]
fuzzing = ["move-binary-format/fuzzing"]
nostd = ["move-binary-format/nostd"]
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use move_binary_format::file_format::CompiledModule;
use proptest::prelude::*;

#[cfg(not(feature = "nostd"))]
proptest! {
#[test]
fn serializer_roundtrip(module in CompiledModule::valid_strategy(20)) {
Expand All @@ -18,6 +19,7 @@ proptest! {
}
}

#[cfg(not(feature = "nostd"))]
proptest! {
// Generating arbitrary compiled modules is really slow, possibly because of
// https://github.com/AltSysrq/proptest/issues/143.
Expand Down
3 changes: 3 additions & 0 deletions language/move-binary-format/src/access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ use move_core_types::{
language_storage::ModuleId,
};

#[cfg(feature = "nostd")]
use alloc::{borrow::ToOwned, vec::Vec};

/// Represents accessors for a compiled module.
///
/// This is a trait to allow working across different wrappers for `CompiledModule`.
Expand Down
3 changes: 3 additions & 0 deletions language/move-binary-format/src/binary_views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ use move_core_types::{
vm_status::StatusCode,
};

#[cfg(feature = "nostd")]
use alloc::{borrow::ToOwned, vec, vec::Vec};

// A `BinaryIndexedView` provides table indexed access for both `CompiledModule` and
// `CompiledScript`.
// Operations that are not allowed for `CompiledScript` return an error.
Expand Down
4 changes: 3 additions & 1 deletion language/move-binary-format/src/check_bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ use crate::{
IndexKind,
};
use move_core_types::vm_status::StatusCode;
use std::u8;

#[cfg(feature = "nostd")]
use alloc::format;

enum BoundsCheckingContext {
Module,
Expand Down
3 changes: 3 additions & 0 deletions language/move-binary-format/src/compatibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use crate::{
file_format::{AbilitySet, StructTypeParameter, Visibility},
normalized::Module,
};
#[cfg(feature = "nostd")]
use alloc::collections::BTreeSet;
#[cfg(not(feature = "nostd"))]
use std::collections::BTreeSet;

/// The result of a linking and layout compatibility check. Here is what the different combinations
Expand Down
3 changes: 3 additions & 0 deletions language/move-binary-format/src/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use crate::file_format::{Constant, SignatureToken};

use move_core_types::value::{MoveTypeLayout, MoveValue};

#[cfg(feature = "nostd")]
use alloc::boxed::Box;

fn sig_to_ty(sig: &SignatureToken) -> Option<MoveTypeLayout> {
match sig {
SignatureToken::Signer => Some(MoveTypeLayout::Signer),
Expand Down
34 changes: 24 additions & 10 deletions language/move-binary-format/src/control_flow_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@

//! This module defines the control-flow graph uses for bytecode verification.
use crate::file_format::{Bytecode, CodeOffset};
#[cfg(feature = "nostd")]
use alloc::{
boxed::Box,
collections::{BTreeMap, BTreeSet},
vec,
vec::Vec,
};
#[cfg(not(feature = "nostd"))]
use std::collections::{BTreeMap, BTreeSet};

// BTree/Hash agnostic type wrappers
Expand Down Expand Up @@ -63,13 +71,16 @@ pub struct VMControlFlowGraph {

impl BasicBlock {
pub fn display(&self, entry: BlockId) {
println!("+=======================+");
println!("| Enter: {} |", entry);
println!("+-----------------------+");
println!("==> Children: {:?}", self.successors);
println!("+-----------------------+");
println!("| Exit: {} |", self.exit);
println!("+=======================+");
#[cfg(not(feature = "nostd"))]
{
println!("+=======================+");
println!("| Enter: {} |", entry);
println!("+-----------------------+");
println!("==> Children: {:?}", self.successors);
println!("+-----------------------+");
println!("| Exit: {} |", self.exit);
println!("+=======================+");
}
}
}

Expand Down Expand Up @@ -190,10 +201,13 @@ impl VMControlFlowGraph {
}

pub fn display(&self) {
for (entry, block) in &self.blocks {
block.display(*entry);
#[cfg(not(feature = "nostd"))]
{
for (entry, block) in &self.blocks {
block.display(*entry);
}
println!("Traversal: {:#?}", self.traversal_successors);
}
println!("Traversal: {:#?}", self.traversal_successors);
}

fn is_end_of_block(pc: CodeOffset, code: &[Bytecode], block_ids: &Set<BlockId>) -> bool {
Expand Down
18 changes: 18 additions & 0 deletions language/move-binary-format/src/deserializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,19 @@
// SPDX-License-Identifier: Apache-2.0

use crate::{check_bounds::BoundsChecker, errors::*, file_format::*, file_format_common::*};
#[cfg(feature = "nostd")]
use alloc::{borrow::ToOwned, boxed::Box, format, string::ToString, vec, vec::Vec};
#[cfg(feature = "nostd")]
use core::convert::TryInto;
#[cfg(feature = "nostd")]
use core2::io::Read;
#[cfg(feature = "nostd")]
use hashbrown::HashSet;
use move_core_types::{
account_address::AccountAddress, identifier::Identifier, metadata::Metadata,
vm_status::StatusCode,
};
#[cfg(not(feature = "nostd"))]
use std::{collections::HashSet, convert::TryInto, io::Read};

impl CompiledScript {
Expand Down Expand Up @@ -881,13 +890,22 @@ fn load_signature_tokens(cursor: &mut VersionedCursor) -> BinaryLoaderResult<Vec
Ok(tokens)
}

#[cfg(not(feature = "nostd"))]
#[cfg(test)]
pub fn load_signature_token_test_entry(
cursor: std::io::Cursor<&[u8]>,
) -> BinaryLoaderResult<SignatureToken> {
load_signature_token(&mut VersionedCursor::new_for_test(VERSION_MAX, cursor))
}

#[cfg(feature = "nostd")]
#[cfg(test)]
pub fn load_signature_token_test_entry(
cursor: core2::io::Cursor<&[u8]>,
) -> BinaryLoaderResult<SignatureToken> {
load_signature_token(&mut VersionedCursor::new_for_test(VERSION_MAX, cursor))
}

/// Deserializes a `SignatureToken`.
fn load_signature_token(cursor: &mut VersionedCursor) -> BinaryLoaderResult<SignatureToken> {
// The following algorithm works by storing partially constructed types on a stack.
Expand Down
25 changes: 25 additions & 0 deletions language/move-binary-format/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,31 @@ use crate::{
file_format::{CodeOffset, FunctionDefinitionIndex, TableIndex},
IndexKind,
};
#[cfg(feature = "nostd")]
use core::fmt;
use move_core_types::{
language_storage::ModuleId,
vm_status::{self, StatusCode, StatusType, VMStatus},
};
#[cfg(not(feature = "nostd"))]
use std::fmt;

#[cfg(not(feature = "nostd"))]
pub type VMResult<T> = ::std::result::Result<T, VMError>;
#[cfg(not(feature = "nostd"))]
pub type BinaryLoaderResult<T> = ::std::result::Result<T, PartialVMError>;
#[cfg(not(feature = "nostd"))]
pub type PartialVMResult<T> = ::std::result::Result<T, PartialVMError>;

#[cfg(feature = "nostd")]
pub type VMResult<T> = ::core::result::Result<T, VMError>;
#[cfg(feature = "nostd")]
pub type BinaryLoaderResult<T> = ::core::result::Result<T, PartialVMError>;
#[cfg(feature = "nostd")]
pub type PartialVMResult<T> = ::core::result::Result<T, PartialVMError>;
#[cfg(feature = "nostd")]
use alloc::{format, string::String, vec, vec::Vec};

#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum Location {
Undefined,
Expand Down Expand Up @@ -210,7 +225,10 @@ impl VMError {
}
}

#[cfg(not(feature = "nostd"))]
impl std::error::Error for VMError {}
#[cfg(feature = "nostd")]
impl core::error::Error for VMError {}

#[derive(Debug, Clone)]
pub struct PartialVMError {
Expand Down Expand Up @@ -477,8 +495,15 @@ pub fn verification_error(status: StatusCode, kind: IndexKind, idx: TableIndex)
PartialVMError::new(status).at_index(kind, idx)
}

#[cfg(not(feature = "nostd"))]
impl std::error::Error for PartialVMError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
None
}
}
#[cfg(feature = "nostd")]
impl core::error::Error for PartialVMError {
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
None
}
}
Loading