Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
40 changes: 26 additions & 14 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ members = [
"testsuite/testcases",
"third_party/move/extensions/move-table-extension",
"third_party/move/mono-move/alloc",
"third_party/move/mono-move/orchestrator",
"third_party/move/mono-move/core",
"third_party/move/mono-move/gas",
"third_party/move/mono-move/global-context",
Expand Down Expand Up @@ -882,6 +883,7 @@ z3tracer = "0.8.0"

# MOVE DEPENDENCIES
mono-move-alloc = { path = "third_party/move/mono-move/alloc" }
mono-move-orchestrator = { path = "third_party/move/mono-move/orchestrator" }
mono-move-core = { path = "third_party/move/mono-move/core" }
mono-move-gas = { path = "third_party/move/mono-move/gas" }
mono-move-global-context = { path = "third_party/move/mono-move/global-context" }
Expand Down
12 changes: 10 additions & 2 deletions third_party/move/mono-move/alloc/src/global_arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,16 @@ impl<T: ?Sized> GlobalArenaPtr<T> {
/// Unlike arena-allocated pointers, the result is never invalidated by
/// arena reset or arena drop: the static data lives for the entire
/// lifetime of a program.
pub fn from_static(data: &'static T) -> Self {
GlobalArenaPtr(NonNull::from(data))
///
/// `const fn` so that wrappers (e.g. interned primitive-type constants)
/// can be declared as compile-time constants without a runtime init.
pub const fn from_static(data: &'static T) -> Self {
// SAFETY: A `&'static T` is always non-null and well-aligned, so
// converting it to `NonNull<T>` is sound.
//
// TODO: once the workspace MSRV is bumped to 1.89+, replace this
// with `NonNull::from_ref(data)` and drop the `unsafe` block.
GlobalArenaPtr(unsafe { NonNull::new_unchecked(data as *const T as *mut T) })
}

/// Unsafely casts this arena pointer to a reference with the specified
Expand Down
2 changes: 2 additions & 0 deletions third_party/move/mono-move/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ rust-version = { workspace = true }
mono-move-alloc = { workspace = true }
mono-move-gas = { workspace = true }
move-core-types = { workspace = true }
parking_lot = { workspace = true }
shared-dsa = { workspace = true }
142 changes: 141 additions & 1 deletion third_party/move/mono-move/core/src/executable.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
// Copyright (c) Aptos Foundation
// Licensed pursuant to the Innovation-Enabling Source Code License, available at https://github.com/aptos-labs/aptos-core/blob/main/LICENSE

use mono_move_alloc::GlobalArenaPtr;
use crate::{
types::{InternedType, Type},
Function,
};
use mono_move_alloc::{ExecutableArena, ExecutableArenaPtr, GlobalArenaPtr};
use move_core_types::account_address::AccountAddress;
use parking_lot::Mutex;
use shared_dsa::UnorderedMap;

/// Identifies an executable (module or script) by its address and name.
/// - For modules, constructed from module address and name.
Expand Down Expand Up @@ -36,3 +42,137 @@ impl ExecutableId {
self.name
}
}

// ================================================================================================
// Executable and supporting types
// ================================================================================================

/// Struct type metadata in an executable.
pub struct StructType {
/// Struct type signature. Invariant: stored type is always
/// [`Type::Struct`].
ty: InternedType,
}

impl StructType {
/// Creates a new struct type entry.
pub fn new(ty: InternedType) -> Self {
Self { ty }
}

/// Returns the underlying type pointer.
pub fn ty(&self) -> InternedType {
self.ty
}
}

/// Enum type metadata in an executable.
pub struct EnumType {
/// Enum type signature. Invariant: stored type is always
/// [`Type::Enum`].
ty: InternedType,
/// Per-variant field types, indexed by variant tag.
#[allow(dead_code)]
variants: ExecutableArenaPtr<[VariantFields]>,
}

impl EnumType {
/// Creates a new enum type entry.
pub fn new(ty: InternedType, variants: ExecutableArenaPtr<[VariantFields]>) -> Self {
Self { ty, variants }
}

/// Returns the underlying type pointer.
pub fn ty(&self) -> InternedType {
self.ty
}
}

/// Field types for a single enum variant.
#[derive(Copy, Clone)]
pub struct VariantFields {
#[allow(dead_code)]
fields: ExecutableArenaPtr<[InternedType]>,
}

impl VariantFields {
/// Creates a new variant fields entry.
pub fn new(fields: ExecutableArenaPtr<[InternedType]>) -> Self {
Self { fields }
}
}

/// A loaded executable (from module or script).
pub struct Executable {
data: ExecutableData,

/// Arena where data is allocated for this executable. **Must** be the
/// last field so that it is dropped after any data structure that holds
/// pointers into it.
#[allow(dead_code)]
arena: Mutex<ExecutableArena>,
}

struct ExecutableData {
/// Executable ID which uniquely identifies this executable.
id: GlobalArenaPtr<ExecutableId>,
/// Non-generic struct definitions. Invariant: stored type is always
/// [`Type::Struct`].
Comment thread
vineethk marked this conversation as resolved.
Outdated
structs: UnorderedMap<GlobalArenaPtr<str>, StructType>,
/// Non-generic enum definitions.
enums: UnorderedMap<GlobalArenaPtr<str>, EnumType>,
/// Non-generic functions.
functions: UnorderedMap<GlobalArenaPtr<str>, ExecutableArenaPtr<Function>>,
}

impl Executable {
/// Creates a new executable.
// TODO: the current constructor accepts pre-populated maps whose values
// are pointers into `arena` (and into the global arena), but there is
// nothing at the type level tying each pointer to the arena it came
// from. Consider replacing this with a builder API that owns the
// `ExecutableArena` internally and exposes `add_struct`/`add_enum`/
// `add_function` entry points, so external callers cannot smuggle in
// pointers backed by a different arena.
pub fn new(
id: GlobalArenaPtr<ExecutableId>,
structs: UnorderedMap<GlobalArenaPtr<str>, StructType>,
enums: UnorderedMap<GlobalArenaPtr<str>, EnumType>,
functions: UnorderedMap<GlobalArenaPtr<str>, ExecutableArenaPtr<Function>>,
arena: ExecutableArena,
Comment thread
vineethk marked this conversation as resolved.
) -> Box<Self> {
Box::new(Self {
data: ExecutableData {
id,
structs,
enums,
functions,
},
arena: Mutex::new(arena),
})
}

/// Returns a non-generic function from this executable. Returns [`None`]
/// if such function does not exist.
pub fn get_function(&self, name: GlobalArenaPtr<str>) -> Option<&Function> {
self.data.functions.get(&name).map(|ptr| {
// SAFETY: Because executable is alive, all its allocations are
// still valid.
unsafe { ptr.as_ref_unchecked() }
})
}

/// Returns a non-generic struct type from this executable. Returns [`None`]
/// if such struct does not exist.
pub fn get_struct(&self, name: GlobalArenaPtr<str>) -> Option<&Type> {
Comment thread
vineethk marked this conversation as resolved.
self.data.structs.get(&name).map(|st| {
// SAFETY: Types must be still valid
unsafe { st.ty.as_ref_unchecked() }
})
}

/// Returns the executable ID pointer.
pub fn id(&self) -> GlobalArenaPtr<ExecutableId> {
self.data.id
}
}
3 changes: 2 additions & 1 deletion third_party/move/mono-move/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ mod executable;
mod function;
mod instruction;
mod transaction_context;
pub mod types;

pub use executable::ExecutableId;
pub use executable::{EnumType, Executable, ExecutableId, StructType, VariantFields};
pub use function::{FrameLayoutInfo, Function, SafePointEntry, SortedSafePointEntries};
pub use instruction::{
CodeOffset, DescriptorId, FrameOffset, MicroOp, MicroOpGasSchedule, ENUM_DATA_OFFSET,
Expand Down
Loading
Loading