Skip to content

Commit aa7f14b

Browse files
cloutiertylerjdetter
authored andcommitted
Revert "Switch to using imported module idents for marking version requirements (#335)"
This reverts commit 4c6c947.
1 parent 53c5f10 commit aa7f14b

File tree

11 files changed

+131
-108
lines changed

11 files changed

+131
-108
lines changed

crates/bindings-csharp/Runtime/bindings.c

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,7 @@ static MonoArray* stdb_buffer_consume(Buffer buf);
100100
// return out;
101101
// }
102102

103-
#define STDB_IMPORT_MODULE_MINOR(minor) "spacetime_6." #minor
104-
#define STDB_IMPORT_MODULE STDB_IMPORT_MODULE_MINOR(0)
105-
106-
__attribute__((import_module(STDB_IMPORT_MODULE),
103+
__attribute__((import_module("spacetime"),
107104
import_name("_get_table_id"))) extern uint16_t
108105
_get_table_id(const char* name, size_t name_len, uint32_t* out);
109106

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

123-
__attribute__((import_module(STDB_IMPORT_MODULE),
120+
__attribute__((import_module("spacetime"),
124121
import_name("_create_index"))) extern uint16_t
125122
_create_index(const char* index_name,
126123
size_t index_name_len,
@@ -144,7 +141,7 @@ static void stdb_create_index(MonoString* index_name_,
144141
check_result(result);
145142
}
146143

147-
__attribute__((import_module(STDB_IMPORT_MODULE),
144+
__attribute__((import_module("spacetime"),
148145
import_name("_iter_by_col_eq"))) extern uint16_t
149146
_iter_by_col_eq(uint32_t table_id,
150147
uint32_t col_id,
@@ -166,7 +163,7 @@ static MonoArray* stdb_iter_by_col_eq(uint32_t table_id,
166163
return stdb_buffer_consume(out);
167164
}
168165

169-
__attribute__((import_module(STDB_IMPORT_MODULE),
166+
__attribute__((import_module("spacetime"),
170167
import_name("_insert"))) extern uint16_t
171168
_insert(uint32_t table_id, uint8_t* row, size_t row_len);
172169

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

181-
// __attribute__((import_module(STDB_IMPORT_MODULE),
178+
// __attribute__((import_module("spacetime"),
182179
// import_name("_delete_pk"))) extern uint16_t
183180
// _delete_pk(uint32_t table_id, const uint8_t* pk, size_t pk_len);
184181

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

193-
// __attribute__((import_module(STDB_IMPORT_MODULE),
190+
// __attribute__((import_module("spacetime"),
194191
// import_name("_delete_value"))) extern uint16_t
195192
// _delete_value(uint32_t table_id, const uint8_t* row, size_t row_len);
196193

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

205-
__attribute__((import_module(STDB_IMPORT_MODULE),
202+
__attribute__((import_module("spacetime"),
206203
import_name("_delete_by_col_eq"))) extern uint16_t
207204
_delete_by_col_eq(uint32_t table_id,
208205
uint32_t col_id,
@@ -224,7 +221,7 @@ static uint32_t stdb_delete_by_col_eq(uint32_t table_id,
224221
return out;
225222
}
226223

227-
// __attribute__((import_module(STDB_IMPORT_MODULE),
224+
// __attribute__((import_module("spacetime"),
228225
// import_name("_delete_range"))) extern uint16_t
229226
// _delete_range(uint32_t table_id,
230227
// uint32_t col_id,
@@ -251,7 +248,7 @@ static uint32_t stdb_delete_by_col_eq(uint32_t table_id,
251248
// return out;
252249
// }
253250

254-
__attribute__((import_module(STDB_IMPORT_MODULE),
251+
__attribute__((import_module("spacetime"),
255252
import_name("_iter_start"))) extern uint16_t
256253
_iter_start(uint32_t table_id, BufferIter* out);
257254

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

264-
__attribute__((import_module(STDB_IMPORT_MODULE),
261+
__attribute__((import_module("spacetime"),
265262
import_name("_iter_start_filtered"))) extern uint16_t
266263
_iter_start_filtered(uint32_t table_id,
267264
const uint8_t* filter,
@@ -279,7 +276,7 @@ static void stdb_iter_start_filtered(uint32_t table_id,
279276
check_result(result);
280277
}
281278

282-
__attribute__((import_module(STDB_IMPORT_MODULE),
279+
__attribute__((import_module("spacetime"),
283280
import_name("_iter_next"))) extern uint16_t
284281
_iter_next(BufferIter iter, Buffer* out);
285282

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

295-
__attribute__((import_module(STDB_IMPORT_MODULE),
292+
__attribute__((import_module("spacetime"),
296293
import_name("_iter_drop"))) extern uint16_t
297294
_iter_drop(BufferIter iter);
298295

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

313-
__attribute__((import_module(STDB_IMPORT_MODULE),
310+
__attribute__((import_module("spacetime"),
314311
import_name("_console_log"))) extern void
315312
_console_log(uint8_t level,
316313
const char* target,
@@ -338,7 +335,7 @@ static void stdb_console_log(MonoString* text_,
338335
free_string(filename);
339336
}
340337

341-
__attribute__((import_module(STDB_IMPORT_MODULE),
338+
__attribute__((import_module("spacetime"),
342339
import_name("_schedule_reducer"))) extern void
343340
_schedule_reducer(const char* name,
344341
size_t name_len,
@@ -363,19 +360,19 @@ static void stdb_schedule_reducer(
363360
free_string(name);
364361
}
365362

366-
__attribute__((import_module(STDB_IMPORT_MODULE),
363+
__attribute__((import_module("spacetime"),
367364
import_name("_cancel_reducer"))) extern void
368365
_cancel_reducer(ScheduleToken token);
369366

370367
static void stdb_cancel_reducer(ScheduleToken* token) {
371368
_cancel_reducer(*token);
372369
}
373370

374-
__attribute__((import_module(STDB_IMPORT_MODULE),
371+
__attribute__((import_module("spacetime"),
375372
import_name("_buffer_len"))) extern size_t
376373
_buffer_len(Buffer buf);
377374

378-
__attribute__((import_module(STDB_IMPORT_MODULE),
375+
__attribute__((import_module("spacetime"),
379376
import_name("_buffer_consume"))) extern void
380377
_buffer_consume(Buffer buf, uint8_t* into, size_t len);
381378

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

393-
__attribute__((import_module(STDB_IMPORT_MODULE),
390+
__attribute__((import_module("spacetime"),
394391
import_name("_buffer_alloc"))) extern Buffer
395392
_buffer_alloc(const uint8_t* data, size_t data_len);
396393

@@ -779,3 +776,8 @@ __attribute__((export_name("__preinit__10_init_csharp"))) void
779776
__preinit__10_init_csharp() {
780777
_start();
781778
}
779+
780+
// __attribute__((export_name("SPACETIME_ABI_VERSION"))) -
781+
// doesn't work on non-functions, must specify on command line
782+
const uint32_t SPACETIME_ABI_VERSION = /* 5.0 */ (5 << 16) | 0;
783+
const uint8_t SPACETIME_ABI_VERSION_IS_ADDR = 1;

crates/bindings-csharp/Runtime/build/SpacetimeDB.Runtime.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
<ItemGroup>
44
<WasiNativeFileReference Include="$(MSBuildThisFileDirectory)../bindings.c" />
5+
<WasiNativeFileReference Include="-Wl,--export=SPACETIME_ABI_VERSION,--export=SPACETIME_ABI_VERSION_IS_ADDR" />
56
<WasiAfterRuntimeLoaded Include="mono_stdb_attach_bindings" />
67
</ItemGroup>
78

crates/bindings-sys/src/lib.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,35 @@ use std::ptr;
1313

1414
use alloc::boxed::Box;
1515

16-
use spacetimedb_primitives::{ColId, TableId};
16+
/// The current version of the ABI.
17+
///
18+
/// Exported as `SPACETIME_ABI_VERSION`, a `u32` WASM global.
19+
/// If this global contains an address into linear memory at which the version is stored,
20+
/// then a WASM global named `SPACETIME_ABI_VERSION_IS_ADDR` is also be exported.
21+
///
22+
/// In rust this looks like:
23+
/// ```rust,ignore
24+
/// #[no_mangle]
25+
/// static SPACETIME_ABI_VERSION: u32 = _; // right now, rust `static`s always export as an address.
26+
/// #[no_mangle]
27+
/// static SPACETIME_ABI_VERSION_IS_ADDR: () = ();
28+
/// ```
29+
///
30+
/// The (big-endian) first 2 bytes constitute the major version (`A`) of the ABI,
31+
/// and the last 2 bytes constitute the minor version (`B`).
32+
///
33+
/// The semantics of a version number `A.B` is that a host implementing version `A.B`
34+
/// can run a module declaring `X.Y` if and only if `X == A && Y <= B`.
35+
/// So, the minor version is intended for backwards-compatible changes, e.g. adding a new function,
36+
/// and the major version is for fully breaking changes.
37+
pub const ABI_VERSION: u32 = 0x0005_0000;
1738

1839
/// Provides a raw set of sys calls which abstractions can be built atop of.
1940
pub mod raw {
2041
use core::mem::ManuallyDrop;
2142
use spacetimedb_primitives::{ColId, TableId};
2243

23-
// this module identifier determines the abi version that modules built with this crate depend
24-
// on. Any non-breaking additions to the abi surface should be put in a new `extern {}` block
25-
// with a module identifier with a minor version 1 above the previous highest minor version.
26-
// For breaking changes, all functions should be moved into one new `spacetime_X.0` block.
27-
#[link(wasm_import_module = "spacetime_6.0")]
44+
#[link(wasm_import_module = "spacetime")]
2845
extern "C" {
2946
/*
3047
/// Create a table with `name`, a UTF-8 slice in WASM memory lasting `name_len` bytes,

crates/bindings/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ pub use log;
3838

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

41+
#[no_mangle]
42+
static SPACETIME_ABI_VERSION: u32 = {
43+
assert!(spacetimedb_lib::MODULE_ABI_VERSION.to_u32() == sys::ABI_VERSION);
44+
sys::ABI_VERSION
45+
};
46+
#[no_mangle]
47+
static SPACETIME_ABI_VERSION_IS_ADDR: () = ();
48+
4149
/// A context that any reducer is provided with.
4250
#[non_exhaustive]
4351
#[derive(Copy, Clone)]

crates/cli/src/subcommands/generate/mod.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use clap::ArgAction::SetTrue;
66
use convert_case::{Case, Casing};
77
use duct::cmd;
88
use spacetimedb_lib::sats::{AlgebraicType, Typespace};
9-
use spacetimedb_lib::{bsatn, MiscModuleExport, ModuleDef, ReducerDef, TableDef, TypeAlias, MODULE_ABI_MAJOR_VERSION};
10-
use wasmtime::{AsContext, Caller};
9+
use spacetimedb_lib::{bsatn, MiscModuleExport, ModuleDef, ReducerDef, TableDef, TypeAlias};
10+
use wasmtime::{AsContext, Caller, ExternType};
1111

1212
mod code_indenter;
1313
pub mod csharp;
@@ -343,10 +343,18 @@ fn extract_descriptions(wasm_file: &Path) -> anyhow::Result<ModuleDef> {
343343
};
344344
let mut store = wasmtime::Store::new(&engine, ctx);
345345
let mut linker = wasmtime::Linker::new(&engine);
346-
linker.allow_shadowing(true).define_unknown_imports_as_traps(&module)?;
347-
let module_name = &*format!("spacetime_{MODULE_ABI_MAJOR_VERSION}.0");
346+
linker.allow_shadowing(true);
347+
for imp in module.imports() {
348+
if let ExternType::Func(func_type) = imp.ty() {
349+
linker
350+
.func_new(imp.module(), imp.name(), func_type, |_, _, _| {
351+
anyhow::bail!("don't call me!!")
352+
})
353+
.unwrap();
354+
}
355+
}
348356
linker.func_wrap(
349-
module_name,
357+
"spacetime",
350358
"_console_log",
351359
|caller: Caller<'_, WasmCtx>,
352360
_level: u32,
@@ -366,7 +374,7 @@ fn extract_descriptions(wasm_file: &Path) -> anyhow::Result<ModuleDef> {
366374
}
367375
},
368376
)?;
369-
linker.func_wrap(module_name, "_buffer_alloc", WasmCtx::buffer_alloc)?;
377+
linker.func_wrap("spacetime", "_buffer_alloc", WasmCtx::buffer_alloc)?;
370378
let instance = linker.instantiate(&mut store, &module)?;
371379
let memory = Memory {
372380
mem: instance.get_memory(&mut store, "memory").unwrap(),

crates/core/src/host/wasm_common.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ pub const INIT_DUNDER: &str = "__init__";
1919
/// the reducer with this name is invoked when updating the database
2020
pub const UPDATE_DUNDER: &str = "__update__";
2121

22+
pub const STDB_ABI_SYM: &str = "SPACETIME_ABI_VERSION";
23+
pub const STDB_ABI_IS_ADDR_SYM: &str = "SPACETIME_ABI_VERSION_IS_ADDR";
24+
2225
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
2326
#[allow(unused)]
2427
pub enum WasmType {
@@ -209,7 +212,6 @@ impl FuncNames {
209212
pub enum ModuleCreationError {
210213
WasmCompileError(anyhow::Error),
211214
Init(#[from] module_host_actor::InitializationError),
212-
Abi(#[from] abi::AbiVersionError),
213215
}
214216

215217
pub trait ResourceIndex {

crates/core/src/host/wasm_common/abi.rs

Lines changed: 0 additions & 57 deletions
This file was deleted.

crates/core/src/host/wasm_common/module_host_actor.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use anyhow::{anyhow, Context};
1010
use bytes::Bytes;
1111
use spacetimedb_lib::buffer::DecodeError;
1212
use spacetimedb_lib::identity::AuthCtx;
13-
use spacetimedb_lib::{bsatn, Address, ModuleDef};
13+
use spacetimedb_lib::{bsatn, Address, IndexType, ModuleDef, VersionTuple};
1414
use spacetimedb_vm::expr::CrudExpr;
1515

1616
use crate::database_instance_context::DatabaseInstanceContext;
@@ -88,8 +88,20 @@ pub(crate) struct WasmModuleHostActor<T: WasmModule> {
8888
energy_monitor: Arc<dyn EnergyMonitor>,
8989
}
9090

91+
#[derive(thiserror::Error, Debug)]
92+
pub enum AbiVersionError {
93+
#[error("module doesn't indicate spacetime ABI version")]
94+
NoVersion,
95+
#[error("abi version is malformed somehow (out-of-bounds, etc)")]
96+
Malformed,
97+
#[error("abi version {got} is not supported (host implements {implement})")]
98+
UnsupportedVersion { got: VersionTuple, implement: VersionTuple },
99+
}
100+
91101
#[derive(thiserror::Error, Debug)]
92102
pub enum InitializationError {
103+
#[error(transparent)]
104+
Abi(#[from] AbiVersionError),
93105
#[error(transparent)]
94106
Validation(#[from] ValidationError),
95107
#[error("setup function returned an error: {0}")]

0 commit comments

Comments
 (0)