Skip to content

Commit 37a9191

Browse files
committed
Position serialization buffer after language statics in wasm linear memory
1 parent 78efd96 commit 37a9191

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

lib/src/wasm_store.c

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ struct TSWasmStore {
101101
wasm_globaltype_t *const_i32_type;
102102
bool has_error;
103103
uint32_t lexer_address;
104-
uint32_t serialization_buffer_address;
105104
};
106105

107106
typedef Array(char) StringData;
@@ -162,7 +161,7 @@ typedef struct {
162161
static volatile uint32_t NEXT_LANGUAGE_ID;
163162

164163
// Linear memory layout:
165-
// [ <-- stack | stdlib statics | lexer | serialization_buffer | language statics --> | heap --> ]
164+
// [ <-- stack | stdlib statics | lexer | language statics --> | serialization_buffer | heap --> ]
166165
#define MAX_MEMORY_SIZE (128 * 1024 * 1024 / MEMORY_PAGE_SIZE)
167166

168167
/************************
@@ -888,8 +887,7 @@ TSWasmStore *ts_wasm_store_new(TSWasmEngine *engine, TSWasmError *wasm_error) {
888887

889888
self->current_function_table_offset = table_index;
890889
self->lexer_address = initial_memory_pages * MEMORY_PAGE_SIZE;
891-
self->serialization_buffer_address = self->lexer_address + sizeof(LexerInWasmMemory);
892-
self->current_memory_offset = self->serialization_buffer_address + TREE_SITTER_SERIALIZATION_BUFFER_SIZE;
890+
self->current_memory_offset = self->lexer_address + sizeof(LexerInWasmMemory);
893891

894892
// Grow the memory enough to hold the builtin lexer and serialization buffer.
895893
uint32_t new_pages_needed = (self->current_memory_offset - self->lexer_address - 1) / MEMORY_PAGE_SIZE + 1;
@@ -940,6 +938,14 @@ size_t ts_wasm_store_language_count(const TSWasmStore *self) {
940938
return result;
941939
}
942940

941+
static uint32_t ts_wasm_store__heap_address(TSWasmStore *self) {
942+
return self->current_memory_offset + TREE_SITTER_SERIALIZATION_BUFFER_SIZE;
943+
}
944+
945+
static uint32_t ts_wasm_store__serialization_buffer_address(TSWasmStore *self) {
946+
return self->current_memory_offset;
947+
}
948+
943949
static bool ts_wasm_store__instantiate(
944950
TSWasmStore *self,
945951
wasmtime_module_t *module,
@@ -966,7 +972,7 @@ static bool ts_wasm_store__instantiate(
966972
}
967973

968974
// Grow the memory to make room for the new data.
969-
uint32_t needed_memory_size = self->current_memory_offset + dylink_info->memory_size;
975+
uint32_t needed_memory_size = ts_wasm_store__heap_address(self) + dylink_info->memory_size;
970976
uint32_t current_memory_size = wasmtime_memory_data_size(context, &self->memory);
971977
if (needed_memory_size > current_memory_size) {
972978
uint32_t pages_to_grow = (
@@ -1475,7 +1481,7 @@ void ts_wasm_store_reset_heap(TSWasmStore *self) {
14751481
};
14761482
wasm_trap_t *trap = NULL;
14771483
wasmtime_val_t args[1] = {
1478-
{.of.i32 = self->current_memory_offset, .kind = WASMTIME_I32},
1484+
{.of.i32 = ts_wasm_store__heap_address(self), .kind = WASMTIME_I32},
14791485
};
14801486

14811487
wasmtime_error_t *error = wasmtime_func_call(context, &func, args, 1, NULL, 0, &trap);
@@ -1633,10 +1639,11 @@ uint32_t ts_wasm_store_call_scanner_serialize(
16331639
) {
16341640
wasmtime_context_t *context = wasmtime_store_context(self->store);
16351641
uint8_t *memory_data = wasmtime_memory_data(context, &self->memory);
1642+
uint32_t serialization_buffer_address = ts_wasm_store__serialization_buffer_address(self);
16361643

16371644
wasmtime_val_raw_t args[2] = {
16381645
{.i32 = scanner_address},
1639-
{.i32 = self->serialization_buffer_address},
1646+
{.i32 = serialization_buffer_address},
16401647
};
16411648
ts_wasm_store__call(self, self->current_instance->scanner_serialize_fn_index, args, 2);
16421649
if (self->has_error) return 0;
@@ -1650,7 +1657,7 @@ uint32_t ts_wasm_store_call_scanner_serialize(
16501657
if (length > 0) {
16511658
memcpy(
16521659
((Lexer *)self->current_lexer)->debug_buffer,
1653-
&memory_data[self->serialization_buffer_address],
1660+
&memory_data[serialization_buffer_address],
16541661
length
16551662
);
16561663
}
@@ -1665,18 +1672,19 @@ void ts_wasm_store_call_scanner_deserialize(
16651672
) {
16661673
wasmtime_context_t *context = wasmtime_store_context(self->store);
16671674
uint8_t *memory_data = wasmtime_memory_data(context, &self->memory);
1675+
uint32_t serialization_buffer_address = ts_wasm_store__serialization_buffer_address(self);
16681676

16691677
if (length > 0) {
16701678
memcpy(
1671-
&memory_data[self->serialization_buffer_address],
1679+
&memory_data[serialization_buffer_address],
16721680
buffer,
16731681
length
16741682
);
16751683
}
16761684

16771685
wasmtime_val_raw_t args[3] = {
16781686
{.i32 = scanner_address},
1679-
{.i32 = self->serialization_buffer_address},
1687+
{.i32 = serialization_buffer_address},
16801688
{.i32 = length},
16811689
};
16821690
ts_wasm_store__call(self, self->current_instance->scanner_deserialize_fn_index, args, 3);

0 commit comments

Comments
 (0)