Store API Out of Bounds Memory Access

The problem I am encountering involves the Store API, which presumably has some memory restrictions to prevent abuse and over-use. Previously, I used this API with success to record and display a few pages of text upon request by a character stored under a number of different keys.

However, just the other day, I tried to make a change to one of these pages of text and received an error I had never received before. I never changed the code.

wasm error: out of bounds memory access
wasm stack trace:
  .~lib/string/String.UTF8.encode@varargs(i32) i32
  .~lib/host/Store.setString<~lib/string/String>(i32,i32)
  .../../script/setFile(i32,i32,i32)
  .../../script/cmdFileSet(i32)
  .../../script/onCommand(i32,i32)

The actual change to the file was a typo correction, and the total memory usage across all of the different files was <12 KB. Later tinkering revealed that this error did not occur when using Store.setString() to set a key to a value with under 512 bytes of data. Most of my files were between 2–3 KB of text.

Here are some snippets from the affected code:

@json
class objFile {
	@alias("name")
	name: string

	@alias("content")
	content: string

	@alias("owner")
	owner: string

	constructor(name: string, content: string, owner: string) {
		this.name = name
		this.content = content
		this.owner = owner
	}
}
function setFile(name: string, content: string, owner: string): void {
	const file = new objFile(name, content, owner)
	Store.setString(`file.${name}`, JSON.stringify<objFile>(file))
}
2 Likes

I will look into it.

I doubt that it is in the Store api though. The out of bounds memory access error means that the wasm-script tried to write outside its own allocated memory - not that you exceeded the Store limits.

I am afraid the bug may be related to json-as, which has previously shown to have bugs where it has written in unknown memory space, causing highly unpredictable errors.

When it comes to store limits, these are the current applied limits:

Property Size Desc
MaxScriptStoreKeySize 256 bytes The max length of the key used for the store.
MaxScriptStoreItemSize 65536 bytes The max size of the ArrayBuffer value stored.
ScriptStoreSizePerItem 32 bytes Overhead bytes counted on the total stored size.
MaxScriptStoreSizeTotal 10 MiB The total allowed storage per script

These numbers may change in the future, and especially to become more generous for supporters. But these are the current limits.

Exceeding these limits will log proper errors such as:

store keys can be max 256 bytes
store items can be max 65536 bytes

1 Like