Skip to content

Commit

Permalink
Revert "Switch to using imported module idents for marking version re…
Browse files Browse the repository at this point in the history
…quirements (#335)"

This reverts commit 4c6c947.
  • Loading branch information
cloutiertyler committed Oct 18, 2023
1 parent d1ce99b commit f25df58
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 108 deletions.
44 changes: 23 additions & 21 deletions crates/bindings-csharp/Runtime/bindings.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,7 @@ static MonoArray* stdb_buffer_consume(Buffer buf);
// return out;
// }

#define STDB_IMPORT_MODULE_MINOR(minor) "spacetime_6." #minor
#define STDB_IMPORT_MODULE STDB_IMPORT_MODULE_MINOR(0)

__attribute__((import_module(STDB_IMPORT_MODULE),
__attribute__((import_module("spacetime"),
import_name("_get_table_id"))) extern uint16_t
_get_table_id(const char* name, size_t name_len, uint32_t* out);

Expand All @@ -120,7 +117,7 @@ static uint32_t stdb_get_table_id(MonoString* name_) {
return out;
}

__attribute__((import_module(STDB_IMPORT_MODULE),
__attribute__((import_module("spacetime"),
import_name("_create_index"))) extern uint16_t
_create_index(const char* index_name,
size_t index_name_len,
Expand All @@ -144,7 +141,7 @@ static void stdb_create_index(MonoString* index_name_,
check_result(result);
}

__attribute__((import_module(STDB_IMPORT_MODULE),
__attribute__((import_module("spacetime"),
import_name("_iter_by_col_eq"))) extern uint16_t
_iter_by_col_eq(uint32_t table_id,
uint32_t col_id,
Expand All @@ -166,7 +163,7 @@ static MonoArray* stdb_iter_by_col_eq(uint32_t table_id,
return stdb_buffer_consume(out);
}

__attribute__((import_module(STDB_IMPORT_MODULE),
__attribute__((import_module("spacetime"),
import_name("_insert"))) extern uint16_t
_insert(uint32_t table_id, uint8_t* row, size_t row_len);

Expand All @@ -178,7 +175,7 @@ static void stdb_insert(uint32_t table_id, MonoArray* row_) {
check_result(result);
}

// __attribute__((import_module(STDB_IMPORT_MODULE),
// __attribute__((import_module("spacetime"),
// import_name("_delete_pk"))) extern uint16_t
// _delete_pk(uint32_t table_id, const uint8_t* pk, size_t pk_len);

Expand All @@ -190,7 +187,7 @@ static void stdb_insert(uint32_t table_id, MonoArray* row_) {
// check_result(result);
// }

// __attribute__((import_module(STDB_IMPORT_MODULE),
// __attribute__((import_module("spacetime"),
// import_name("_delete_value"))) extern uint16_t
// _delete_value(uint32_t table_id, const uint8_t* row, size_t row_len);

Expand All @@ -202,7 +199,7 @@ static void stdb_insert(uint32_t table_id, MonoArray* row_) {
// check_result(result);
// }

__attribute__((import_module(STDB_IMPORT_MODULE),
__attribute__((import_module("spacetime"),
import_name("_delete_by_col_eq"))) extern uint16_t
_delete_by_col_eq(uint32_t table_id,
uint32_t col_id,
Expand All @@ -224,7 +221,7 @@ static uint32_t stdb_delete_by_col_eq(uint32_t table_id,
return out;
}

// __attribute__((import_module(STDB_IMPORT_MODULE),
// __attribute__((import_module("spacetime"),
// import_name("_delete_range"))) extern uint16_t
// _delete_range(uint32_t table_id,
// uint32_t col_id,
Expand All @@ -251,7 +248,7 @@ static uint32_t stdb_delete_by_col_eq(uint32_t table_id,
// return out;
// }

__attribute__((import_module(STDB_IMPORT_MODULE),
__attribute__((import_module("spacetime"),
import_name("_iter_start"))) extern uint16_t
_iter_start(uint32_t table_id, BufferIter* out);

Expand All @@ -261,7 +258,7 @@ static void stdb_iter_start(uint32_t table_id, BufferIter* iter) {
check_result(result);
}

__attribute__((import_module(STDB_IMPORT_MODULE),
__attribute__((import_module("spacetime"),
import_name("_iter_start_filtered"))) extern uint16_t
_iter_start_filtered(uint32_t table_id,
const uint8_t* filter,
Expand All @@ -279,7 +276,7 @@ static void stdb_iter_start_filtered(uint32_t table_id,
check_result(result);
}

__attribute__((import_module(STDB_IMPORT_MODULE),
__attribute__((import_module("spacetime"),
import_name("_iter_next"))) extern uint16_t
_iter_next(BufferIter iter, Buffer* out);

Expand All @@ -292,7 +289,7 @@ static MonoArray* stdb_iter_next(BufferIter iter) {
return stdb_buffer_consume(out);
}

__attribute__((import_module(STDB_IMPORT_MODULE),
__attribute__((import_module("spacetime"),
import_name("_iter_drop"))) extern uint16_t
_iter_drop(BufferIter iter);

Expand All @@ -310,7 +307,7 @@ static void stdb_iter_drop(BufferIter* iter) {
check_result(result);
}

__attribute__((import_module(STDB_IMPORT_MODULE),
__attribute__((import_module("spacetime"),
import_name("_console_log"))) extern void
_console_log(uint8_t level,
const char* target,
Expand Down Expand Up @@ -338,7 +335,7 @@ static void stdb_console_log(MonoString* text_,
free_string(filename);
}

__attribute__((import_module(STDB_IMPORT_MODULE),
__attribute__((import_module("spacetime"),
import_name("_schedule_reducer"))) extern void
_schedule_reducer(const char* name,
size_t name_len,
Expand All @@ -363,19 +360,19 @@ static void stdb_schedule_reducer(
free_string(name);
}

__attribute__((import_module(STDB_IMPORT_MODULE),
__attribute__((import_module("spacetime"),
import_name("_cancel_reducer"))) extern void
_cancel_reducer(ScheduleToken token);

static void stdb_cancel_reducer(ScheduleToken* token) {
_cancel_reducer(*token);
}

__attribute__((import_module(STDB_IMPORT_MODULE),
__attribute__((import_module("spacetime"),
import_name("_buffer_len"))) extern size_t
_buffer_len(Buffer buf);

__attribute__((import_module(STDB_IMPORT_MODULE),
__attribute__((import_module("spacetime"),
import_name("_buffer_consume"))) extern void
_buffer_consume(Buffer buf, uint8_t* into, size_t len);

Expand All @@ -390,7 +387,7 @@ static MonoArray* stdb_buffer_consume(Buffer buf) {
return result;
}

__attribute__((import_module(STDB_IMPORT_MODULE),
__attribute__((import_module("spacetime"),
import_name("_buffer_alloc"))) extern Buffer
_buffer_alloc(const uint8_t* data, size_t data_len);

Expand Down Expand Up @@ -779,3 +776,8 @@ __attribute__((export_name("__preinit__10_init_csharp"))) void
__preinit__10_init_csharp() {
_start();
}

// __attribute__((export_name("SPACETIME_ABI_VERSION"))) -
// doesn't work on non-functions, must specify on command line
const uint32_t SPACETIME_ABI_VERSION = /* 5.0 */ (5 << 16) | 0;
const uint8_t SPACETIME_ABI_VERSION_IS_ADDR = 1;
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<ItemGroup>
<WasiNativeFileReference Include="$(MSBuildThisFileDirectory)../bindings.c" />
<WasiNativeFileReference Include="-Wl,--export=SPACETIME_ABI_VERSION,--export=SPACETIME_ABI_VERSION_IS_ADDR" />
<WasiAfterRuntimeLoaded Include="mono_stdb_attach_bindings" />
</ItemGroup>

Expand Down
29 changes: 24 additions & 5 deletions crates/bindings-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,34 @@ use std::ptr;

use alloc::boxed::Box;

/// The current version of the ABI.
///
/// Exported as `SPACETIME_ABI_VERSION`, a `u32` WASM global.
/// If this global contains an address into linear memory at which the version is stored,
/// then a WASM global named `SPACETIME_ABI_VERSION_IS_ADDR` is also be exported.
///
/// In rust this looks like:
/// ```rust,ignore
/// #[no_mangle]
/// static SPACETIME_ABI_VERSION: u32 = _; // right now, rust `static`s always export as an address.
/// #[no_mangle]
/// static SPACETIME_ABI_VERSION_IS_ADDR: () = ();
/// ```
///
/// The (big-endian) first 2 bytes constitute the major version (`A`) of the ABI,
/// and the last 2 bytes constitute the minor version (`B`).
///
/// The semantics of a version number `A.B` is that a host implementing version `A.B`
/// can run a module declaring `X.Y` if and only if `X == A && Y <= B`.
/// So, the minor version is intended for backwards-compatible changes, e.g. adding a new function,
/// and the major version is for fully breaking changes.
pub const ABI_VERSION: u32 = 0x0005_0000;

/// Provides a raw set of sys calls which abstractions can be built atop of.
pub mod raw {
use core::mem::ManuallyDrop;

// this module identifier determines the abi version that modules built with this crate depend
// on. Any non-breaking additions to the abi surface should be put in a new `extern {}` block
// with a module identifier with a minor version 1 above the previous highest minor version.
// For breaking changes, all functions should be moved into one new `spacetime_X.0` block.
#[link(wasm_import_module = "spacetime_6.0")]
#[link(wasm_import_module = "spacetime")]
extern "C" {
/*
/// Create a table with `name`, a UTF-8 slice in WASM memory lasting `name_len` bytes,
Expand Down
8 changes: 8 additions & 0 deletions crates/bindings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ pub use log;

pub type Result<T = (), E = Errno> = core::result::Result<T, E>;

#[no_mangle]
static SPACETIME_ABI_VERSION: u32 = {
assert!(spacetimedb_lib::MODULE_ABI_VERSION.to_u32() == sys::ABI_VERSION);
sys::ABI_VERSION
};
#[no_mangle]
static SPACETIME_ABI_VERSION_IS_ADDR: () = ();

/// A context that any reducer is provided with.
#[non_exhaustive]
#[derive(Copy, Clone)]
Expand Down
20 changes: 14 additions & 6 deletions crates/cli/src/subcommands/generate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use clap::ArgAction::SetTrue;
use convert_case::{Case, Casing};
use duct::cmd;
use spacetimedb_lib::sats::{AlgebraicType, Typespace};
use spacetimedb_lib::{bsatn, MiscModuleExport, ModuleDef, ReducerDef, TableDef, TypeAlias, MODULE_ABI_MAJOR_VERSION};
use wasmtime::{AsContext, Caller};
use spacetimedb_lib::{bsatn, MiscModuleExport, ModuleDef, ReducerDef, TableDef, TypeAlias};
use wasmtime::{AsContext, Caller, ExternType};

mod code_indenter;
pub mod csharp;
Expand Down Expand Up @@ -343,10 +343,18 @@ fn extract_descriptions(wasm_file: &Path) -> anyhow::Result<ModuleDef> {
};
let mut store = wasmtime::Store::new(&engine, ctx);
let mut linker = wasmtime::Linker::new(&engine);
linker.allow_shadowing(true).define_unknown_imports_as_traps(&module)?;
let module_name = &*format!("spacetime_{MODULE_ABI_MAJOR_VERSION}.0");
linker.allow_shadowing(true);
for imp in module.imports() {
if let ExternType::Func(func_type) = imp.ty() {
linker
.func_new(imp.module(), imp.name(), func_type, |_, _, _| {
anyhow::bail!("don't call me!!")
})
.unwrap();
}
}
linker.func_wrap(
module_name,
"spacetime",
"_console_log",
|caller: Caller<'_, WasmCtx>,
_level: u32,
Expand All @@ -366,7 +374,7 @@ fn extract_descriptions(wasm_file: &Path) -> anyhow::Result<ModuleDef> {
}
},
)?;
linker.func_wrap(module_name, "_buffer_alloc", WasmCtx::buffer_alloc)?;
linker.func_wrap("spacetime", "_buffer_alloc", WasmCtx::buffer_alloc)?;
let instance = linker.instantiate(&mut store, &module)?;
let memory = Memory {
mem: instance.get_memory(&mut store, "memory").unwrap(),
Expand Down
4 changes: 3 additions & 1 deletion crates/core/src/host/wasm_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ pub const INIT_DUNDER: &str = "__init__";
/// the reducer with this name is invoked when updating the database
pub const UPDATE_DUNDER: &str = "__update__";

pub const STDB_ABI_SYM: &str = "SPACETIME_ABI_VERSION";
pub const STDB_ABI_IS_ADDR_SYM: &str = "SPACETIME_ABI_VERSION_IS_ADDR";

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[allow(unused)]
pub enum WasmType {
Expand Down Expand Up @@ -209,7 +212,6 @@ impl FuncNames {
pub enum ModuleCreationError {
WasmCompileError(anyhow::Error),
Init(#[from] module_host_actor::InitializationError),
Abi(#[from] abi::AbiVersionError),
}

pub trait ResourceIndex {
Expand Down
57 changes: 0 additions & 57 deletions crates/core/src/host/wasm_common/abi.rs

This file was deleted.

14 changes: 13 additions & 1 deletion crates/core/src/host/wasm_common/module_host_actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use bytes::Bytes;
use nonempty::NonEmpty;
use spacetimedb_lib::buffer::DecodeError;
use spacetimedb_lib::identity::AuthCtx;
use spacetimedb_lib::{bsatn, Address, IndexType, ModuleDef};
use spacetimedb_lib::{bsatn, Address, IndexType, ModuleDef, VersionTuple};
use spacetimedb_vm::expr::CrudExpr;

use crate::client::ClientConnectionSender;
Expand Down Expand Up @@ -97,8 +97,20 @@ pub(crate) struct WasmModuleHostActor<T: WasmModule> {
energy_monitor: Arc<dyn EnergyMonitor>,
}

#[derive(thiserror::Error, Debug)]
pub enum AbiVersionError {
#[error("module doesn't indicate spacetime ABI version")]
NoVersion,
#[error("abi version is malformed somehow (out-of-bounds, etc)")]
Malformed,
#[error("abi version {got} is not supported (host implements {implement})")]
UnsupportedVersion { got: VersionTuple, implement: VersionTuple },
}

#[derive(thiserror::Error, Debug)]
pub enum InitializationError {
#[error(transparent)]
Abi(#[from] AbiVersionError),
#[error(transparent)]
Validation(#[from] ValidationError),
#[error("setup function returned an error: {0}")]
Expand Down
Loading

0 comments on commit f25df58

Please sign in to comment.