@gameslate/sdk 0.3.6 → 0.3.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +37 -0
- package/bin/gameslate-plugin.mjs +75 -0
- package/build/init-plugin.mjs +2 -0
- package/build/verify-effects.mjs +188 -0
- package/lib/assets.d.ts +155 -8
- package/lib/assets.js +15 -2
- package/lib/catalogue.d.ts +6 -6
- package/lib/host/addresses.d.ts +103 -0
- package/lib/host/addresses.js +150 -0
- package/lib/host/asset-sandbox.d.ts +17 -3
- package/lib/host/asset-sandbox.js +27 -6
- package/lib/host/index.d.ts +2 -0
- package/lib/host/index.js +2 -0
- package/lib/host/inspect.js +1 -1
- package/lib/host/live-sandbox.d.ts +11 -2
- package/lib/host/live-sandbox.js +40 -6
- package/lib/host/quickjs-guest.d.ts +3 -0
- package/lib/host/quickjs-guest.js +17 -1
- package/lib/host/verify.d.ts +907 -0
- package/lib/host/verify.js +292 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/n64-address.d.ts +5 -0
- package/lib/n64-address.js +25 -0
- package/lib/pack.d.ts +6 -6
- package/lib/plugin/contract.d.ts +9 -0
- package/lib/plugin/sandbox-guest.js +4 -2
- package/lib/plugin-manifest.d.ts +19 -1
- package/lib/plugin-manifest.js +33 -6
- package/lib/plugins-api.d.ts +12 -0
- package/lib/plugins-api.js +3 -0
- package/lib/sandbox-protocol.d.ts +8 -8
- package/package.json +1 -1
- package/template/claude/skills/build-gameslate-plugin/SKILL.md +280 -0
- package/template/claude/skills/verify-gameslate-plugin/SKILL.md +194 -0
- package/template/gitignore +5 -0
- package/template/test/live.test.ts +5 -2
package/lib/catalogue.d.ts
CHANGED
|
@@ -43,17 +43,17 @@ export declare const CounterSchema: z.ZodObject<{
|
|
|
43
43
|
id: string;
|
|
44
44
|
icon?: string | undefined;
|
|
45
45
|
max?: string | number | undefined;
|
|
46
|
+
format?: "number" | "percent" | undefined;
|
|
46
47
|
display?: Record<string, string> | undefined;
|
|
47
48
|
role?: "keys" | "health" | "magic" | "armor" | undefined;
|
|
48
|
-
format?: "number" | "percent" | undefined;
|
|
49
49
|
}, {
|
|
50
50
|
label: string;
|
|
51
51
|
id: string;
|
|
52
52
|
icon?: string | undefined;
|
|
53
53
|
max?: string | number | undefined;
|
|
54
|
+
format?: "number" | "percent" | undefined;
|
|
54
55
|
display?: Record<string, string> | undefined;
|
|
55
56
|
role?: "keys" | "health" | "magic" | "armor" | undefined;
|
|
56
|
-
format?: "number" | "percent" | undefined;
|
|
57
57
|
}>;
|
|
58
58
|
export type CatalogueCounter = z.infer<typeof CounterSchema>;
|
|
59
59
|
export type CounterRole = NonNullable<CatalogueCounter['role']>;
|
|
@@ -613,17 +613,17 @@ export declare const CatalogueSchema: z.ZodObject<{
|
|
|
613
613
|
id: string;
|
|
614
614
|
icon?: string | undefined;
|
|
615
615
|
max?: string | number | undefined;
|
|
616
|
+
format?: "number" | "percent" | undefined;
|
|
616
617
|
display?: Record<string, string> | undefined;
|
|
617
618
|
role?: "keys" | "health" | "magic" | "armor" | undefined;
|
|
618
|
-
format?: "number" | "percent" | undefined;
|
|
619
619
|
}, {
|
|
620
620
|
label: string;
|
|
621
621
|
id: string;
|
|
622
622
|
icon?: string | undefined;
|
|
623
623
|
max?: string | number | undefined;
|
|
624
|
+
format?: "number" | "percent" | undefined;
|
|
624
625
|
display?: Record<string, string> | undefined;
|
|
625
626
|
role?: "keys" | "health" | "magic" | "armor" | undefined;
|
|
626
|
-
format?: "number" | "percent" | undefined;
|
|
627
627
|
}>, "many">;
|
|
628
628
|
/** flag sets the pack's decoder streams (bit conditions may only reference these) */
|
|
629
629
|
flagSets: z.ZodArray<z.ZodString, "many">;
|
|
@@ -1048,9 +1048,9 @@ export declare const CatalogueSchema: z.ZodObject<{
|
|
|
1048
1048
|
id: string;
|
|
1049
1049
|
icon?: string | undefined;
|
|
1050
1050
|
max?: string | number | undefined;
|
|
1051
|
+
format?: "number" | "percent" | undefined;
|
|
1051
1052
|
display?: Record<string, string> | undefined;
|
|
1052
1053
|
role?: "keys" | "health" | "magic" | "armor" | undefined;
|
|
1053
|
-
format?: "number" | "percent" | undefined;
|
|
1054
1054
|
}[];
|
|
1055
1055
|
flagSets: string[];
|
|
1056
1056
|
items: {
|
|
@@ -1167,9 +1167,9 @@ export declare const CatalogueSchema: z.ZodObject<{
|
|
|
1167
1167
|
id: string;
|
|
1168
1168
|
icon?: string | undefined;
|
|
1169
1169
|
max?: string | number | undefined;
|
|
1170
|
+
format?: "number" | "percent" | undefined;
|
|
1170
1171
|
display?: Record<string, string> | undefined;
|
|
1171
1172
|
role?: "keys" | "health" | "magic" | "armor" | undefined;
|
|
1172
|
-
format?: "number" | "percent" | undefined;
|
|
1173
1173
|
}[];
|
|
1174
1174
|
flagSets: string[];
|
|
1175
1175
|
items: {
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
export declare const AddressSpecSchema: z.ZodObject<{
|
|
3
|
+
/** the decomp checkout: maps, headers and clang run from here */
|
|
4
|
+
decomp: z.ZodString;
|
|
5
|
+
/** symbol files, relative to the decomp: an ld `.map`, or splat `symbol_addrs*.txt` */
|
|
6
|
+
symbolFiles: z.ZodArray<z.ZodString, "many">;
|
|
7
|
+
/** the decomp builds another revision: subtract this from every symbol (check it against
|
|
8
|
+
* verified leads first: build-gameslate-plugin § 4) */
|
|
9
|
+
subtract: z.ZodDefault<z.ZodUnion<[z.ZodNumber, z.ZodEffects<z.ZodString, number, string>]>>;
|
|
10
|
+
/** headers the probe includes, in order */
|
|
11
|
+
includes: z.ZodDefault<z.ZodArray<z.ZodString, "many">>;
|
|
12
|
+
/** headers the decomp generates at build time, given as empty files so an unbuilt checkout
|
|
13
|
+
* compiles; a layout that really needs one still fails to compile, loudly */
|
|
14
|
+
stubHeaders: z.ZodDefault<z.ZodArray<z.ZodString, "many">>;
|
|
15
|
+
/** clang's arguments besides the probe itself (target, ABI, defines, include paths) */
|
|
16
|
+
clang: z.ZodObject<{
|
|
17
|
+
binary: z.ZodDefault<z.ZodString>;
|
|
18
|
+
args: z.ZodArray<z.ZodString, "many">;
|
|
19
|
+
}, "strip", z.ZodTypeAny, {
|
|
20
|
+
binary: string;
|
|
21
|
+
args: string[];
|
|
22
|
+
}, {
|
|
23
|
+
args: string[];
|
|
24
|
+
binary?: string | undefined;
|
|
25
|
+
}>;
|
|
26
|
+
symbols: z.ZodDefault<z.ZodArray<z.ZodString, "many">>;
|
|
27
|
+
/** `Type.field.path` (a type may be `struct Name`) */
|
|
28
|
+
offsets: z.ZodDefault<z.ZodArray<z.ZodString, "many">>;
|
|
29
|
+
sizes: z.ZodDefault<z.ZodArray<z.ZodString, "many">>;
|
|
30
|
+
/** any integer constant expression: enum values, macros */
|
|
31
|
+
constants: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
32
|
+
}, "strip", z.ZodTypeAny, {
|
|
33
|
+
includes: string[];
|
|
34
|
+
decomp: string;
|
|
35
|
+
symbolFiles: string[];
|
|
36
|
+
subtract: number;
|
|
37
|
+
stubHeaders: string[];
|
|
38
|
+
clang: {
|
|
39
|
+
binary: string;
|
|
40
|
+
args: string[];
|
|
41
|
+
};
|
|
42
|
+
symbols: string[];
|
|
43
|
+
offsets: string[];
|
|
44
|
+
sizes: string[];
|
|
45
|
+
constants: Record<string, string>;
|
|
46
|
+
}, {
|
|
47
|
+
decomp: string;
|
|
48
|
+
symbolFiles: string[];
|
|
49
|
+
clang: {
|
|
50
|
+
args: string[];
|
|
51
|
+
binary?: string | undefined;
|
|
52
|
+
};
|
|
53
|
+
includes?: string[] | undefined;
|
|
54
|
+
subtract?: string | number | undefined;
|
|
55
|
+
stubHeaders?: string[] | undefined;
|
|
56
|
+
symbols?: string[] | undefined;
|
|
57
|
+
offsets?: string[] | undefined;
|
|
58
|
+
sizes?: string[] | undefined;
|
|
59
|
+
constants?: Record<string, string> | undefined;
|
|
60
|
+
}>;
|
|
61
|
+
export type AddressSpec = z.infer<typeof AddressSpecSchema>;
|
|
62
|
+
/** Symbols from an ld map (` 0x800dc50c gGamestate`) or a splat file (`gGameState = 0x80177834;`). */
|
|
63
|
+
export declare function parseSymbolFile(text: string): Map<string, number>;
|
|
64
|
+
/** `struct CourseTable.vertexStart` → the type and the member path. */
|
|
65
|
+
export declare function splitMember(spec: string): {
|
|
66
|
+
type: string;
|
|
67
|
+
member: string;
|
|
68
|
+
};
|
|
69
|
+
/** One probe entry per value: `const long probe_N = (expr);`. */
|
|
70
|
+
export declare function probeEntries(spec: Pick<AddressSpec, 'offsets' | 'sizes' | 'constants'>): ({
|
|
71
|
+
kind: "offsets";
|
|
72
|
+
name: string;
|
|
73
|
+
expr: string;
|
|
74
|
+
} | {
|
|
75
|
+
kind: "sizes";
|
|
76
|
+
name: string;
|
|
77
|
+
expr: string;
|
|
78
|
+
} | {
|
|
79
|
+
kind: "constants";
|
|
80
|
+
name: string;
|
|
81
|
+
expr: string;
|
|
82
|
+
})[];
|
|
83
|
+
export declare function probeSource(includes: readonly string[], entries: ReadonlyArray<{
|
|
84
|
+
expr: string;
|
|
85
|
+
}>): string;
|
|
86
|
+
/** probe_N's value from clang's assembly (`probe_3:` then `.4byte 612` or `.word 612`). */
|
|
87
|
+
export declare function parseProbeAssembly(asm: string, count: number): number[];
|
|
88
|
+
export interface AddressTable {
|
|
89
|
+
symbols: Record<string, number>;
|
|
90
|
+
offsets: Record<string, number>;
|
|
91
|
+
sizes: Record<string, number>;
|
|
92
|
+
constants: Record<string, number>;
|
|
93
|
+
}
|
|
94
|
+
export interface AddressEffects {
|
|
95
|
+
/** a file under the decomp, as text */
|
|
96
|
+
read(file: string): string;
|
|
97
|
+
/** clang over the probe source; its assembly */
|
|
98
|
+
compile(source: string): string;
|
|
99
|
+
}
|
|
100
|
+
/** The table a spec names; throws on any symbol the maps don't have or value clang didn't fold. */
|
|
101
|
+
export declare function buildAddressTable(spec: AddressSpec, effects: AddressEffects): AddressTable;
|
|
102
|
+
/** The table as a TypeScript module, with where every number came from. */
|
|
103
|
+
export declare function addressModule(table: AddressTable, provenance: string): string;
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
/*
|
|
3
|
+
* `gameslate-plugin addresses <spec.json>`: a game's address table from its decompilation, so a
|
|
4
|
+
* plugin's addresses have a source instead of being typed in. Globals come from the build's
|
|
5
|
+
* symbol map (a GNU ld map, or splat's `symbol_addrs` files), less a constant when the decomp
|
|
6
|
+
* builds another revision than the player's ROM. Struct offsets, sizes and constants come from a
|
|
7
|
+
* probe compiled with clang against the decomp's own headers for the console's ABI: no code runs,
|
|
8
|
+
* the compiler folds each value into the assembly. The effects (files, clang) are parameters.
|
|
9
|
+
*/
|
|
10
|
+
const Hex = z.union([
|
|
11
|
+
z.number().int(),
|
|
12
|
+
z
|
|
13
|
+
.string()
|
|
14
|
+
.regex(/^-?0x[0-9a-fA-F]+$/, 'a hex number such as 0xA190')
|
|
15
|
+
.transform(h => Number.parseInt(h, 16)),
|
|
16
|
+
]);
|
|
17
|
+
export const AddressSpecSchema = z.object({
|
|
18
|
+
/** the decomp checkout: maps, headers and clang run from here */
|
|
19
|
+
decomp: z.string(),
|
|
20
|
+
/** symbol files, relative to the decomp: an ld `.map`, or splat `symbol_addrs*.txt` */
|
|
21
|
+
symbolFiles: z.array(z.string()).min(1),
|
|
22
|
+
/** the decomp builds another revision: subtract this from every symbol (check it against
|
|
23
|
+
* verified leads first: build-gameslate-plugin § 4) */
|
|
24
|
+
subtract: Hex.default(0),
|
|
25
|
+
/** headers the probe includes, in order */
|
|
26
|
+
includes: z.array(z.string()).default([]),
|
|
27
|
+
/** headers the decomp generates at build time, given as empty files so an unbuilt checkout
|
|
28
|
+
* compiles; a layout that really needs one still fails to compile, loudly */
|
|
29
|
+
stubHeaders: z.array(z.string().regex(/^[A-Za-z0-9_./-]+\.h$/)).default([]),
|
|
30
|
+
/** clang's arguments besides the probe itself (target, ABI, defines, include paths) */
|
|
31
|
+
clang: z.object({ binary: z.string().default('clang-18'), args: z.array(z.string()) }),
|
|
32
|
+
symbols: z.array(z.string()).default([]),
|
|
33
|
+
/** `Type.field.path` (a type may be `struct Name`) */
|
|
34
|
+
offsets: z.array(z.string()).default([]),
|
|
35
|
+
sizes: z.array(z.string()).default([]),
|
|
36
|
+
/** any integer constant expression: enum values, macros */
|
|
37
|
+
constants: z.record(z.string(), z.string()).default({}),
|
|
38
|
+
});
|
|
39
|
+
const LD_MAP_LINE = /^\s+0x([0-9a-fA-F]{8,16})\s+([A-Za-z_][A-Za-z0-9_]*)\s*$/;
|
|
40
|
+
const SPLAT_LINE = /^\s*([A-Za-z_][A-Za-z0-9_]*)\s*=\s*0x([0-9a-fA-F]+)\s*;/;
|
|
41
|
+
/** Symbols from an ld map (` 0x800dc50c gGamestate`) or a splat file (`gGameState = 0x80177834;`). */
|
|
42
|
+
export function parseSymbolFile(text) {
|
|
43
|
+
const out = new Map();
|
|
44
|
+
for (const line of text.split('\n')) {
|
|
45
|
+
const ld = LD_MAP_LINE.exec(line);
|
|
46
|
+
if (ld?.[1] && ld[2]) {
|
|
47
|
+
out.set(ld[2], Number.parseInt(ld[1].slice(-8), 16));
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
const splat = SPLAT_LINE.exec(line);
|
|
51
|
+
if (splat?.[1] && splat[2])
|
|
52
|
+
out.set(splat[1], Number.parseInt(splat[2], 16));
|
|
53
|
+
}
|
|
54
|
+
return out;
|
|
55
|
+
}
|
|
56
|
+
/** `struct CourseTable.vertexStart` → the type and the member path. */
|
|
57
|
+
export function splitMember(spec) {
|
|
58
|
+
const m = /^((?:struct |union )?[A-Za-z_][A-Za-z0-9_]*)\.(.+)$/.exec(spec);
|
|
59
|
+
if (!m?.[1] || !m[2])
|
|
60
|
+
throw new Error(`"${spec}" isn't Type.member`);
|
|
61
|
+
return { type: m[1], member: m[2] };
|
|
62
|
+
}
|
|
63
|
+
/** One probe entry per value: `const long probe_N = (expr);`. */
|
|
64
|
+
export function probeEntries(spec) {
|
|
65
|
+
return [
|
|
66
|
+
...spec.offsets.map(o => {
|
|
67
|
+
const { type, member } = splitMember(o);
|
|
68
|
+
return {
|
|
69
|
+
kind: 'offsets',
|
|
70
|
+
name: o,
|
|
71
|
+
expr: `__builtin_offsetof(${type}, ${member})`,
|
|
72
|
+
};
|
|
73
|
+
}),
|
|
74
|
+
...spec.sizes.map(t => ({ kind: 'sizes', name: t, expr: `sizeof(${t})` })),
|
|
75
|
+
...Object.entries(spec.constants).map(([name, expr]) => ({
|
|
76
|
+
kind: 'constants',
|
|
77
|
+
name,
|
|
78
|
+
expr,
|
|
79
|
+
})),
|
|
80
|
+
];
|
|
81
|
+
}
|
|
82
|
+
export function probeSource(includes, entries) {
|
|
83
|
+
return [
|
|
84
|
+
'/* generated by gameslate-plugin addresses: each value folds into the assembly */',
|
|
85
|
+
...includes.map(h => `#include ${h.startsWith('<') ? h : `"${h}"`}`),
|
|
86
|
+
...entries.map((e, i) => `const long probe_${i} = (long)(${e.expr});`),
|
|
87
|
+
'',
|
|
88
|
+
].join('\n');
|
|
89
|
+
}
|
|
90
|
+
/** probe_N's value from clang's assembly (`probe_3:` then `.4byte 612` or `.word 612`). */
|
|
91
|
+
export function parseProbeAssembly(asm, count) {
|
|
92
|
+
const values = new Map();
|
|
93
|
+
const lines = asm.split('\n');
|
|
94
|
+
lines.forEach((line, i) => {
|
|
95
|
+
const label = /^probe_(\d+):/.exec(line);
|
|
96
|
+
const data = /^\s*\.(?:4byte|word|long)\s+(-?\d+)/.exec(lines[i + 1] ?? '');
|
|
97
|
+
/* clang writes a word's bits unsigned (-103 as 4294967193): read them as a signed long */
|
|
98
|
+
if (label?.[1] && data?.[1])
|
|
99
|
+
values.set(Number(label[1]), Number(data[1]) | 0);
|
|
100
|
+
});
|
|
101
|
+
return Array.from({ length: count }, (_, i) => {
|
|
102
|
+
const v = values.get(i);
|
|
103
|
+
if (v === undefined)
|
|
104
|
+
throw new Error(`clang's output has no value for probe_${i}`);
|
|
105
|
+
return v;
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
/** The table a spec names; throws on any symbol the maps don't have or value clang didn't fold. */
|
|
109
|
+
export function buildAddressTable(spec, effects) {
|
|
110
|
+
const known = new Map();
|
|
111
|
+
for (const f of spec.symbolFiles)
|
|
112
|
+
for (const [k, v] of parseSymbolFile(effects.read(f)))
|
|
113
|
+
known.set(k, v);
|
|
114
|
+
const missing = spec.symbols.filter(s => !known.has(s));
|
|
115
|
+
if (missing.length)
|
|
116
|
+
throw new Error(`not in ${spec.symbolFiles.join(', ')}: ${missing.join(', ')}`);
|
|
117
|
+
const table = { symbols: {}, offsets: {}, sizes: {}, constants: {} };
|
|
118
|
+
for (const s of spec.symbols)
|
|
119
|
+
table.symbols[s] = ((known.get(s) ?? 0) - spec.subtract) >>> 0;
|
|
120
|
+
const entries = probeEntries(spec);
|
|
121
|
+
if (entries.length) {
|
|
122
|
+
const values = parseProbeAssembly(effects.compile(probeSource(spec.includes, entries)), entries.length);
|
|
123
|
+
entries.forEach((e, i) => {
|
|
124
|
+
table[e.kind][e.name] = values[i] ?? 0;
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
return table;
|
|
128
|
+
}
|
|
129
|
+
const hex = (n) => (n < 0 ? `-0x${(-n).toString(16)}` : `0x${n.toString(16)}`);
|
|
130
|
+
const key = (k) => (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(k) ? k : JSON.stringify(k));
|
|
131
|
+
/** The table as a TypeScript module, with where every number came from. */
|
|
132
|
+
export function addressModule(table, provenance) {
|
|
133
|
+
const block = (name, values, asHex) => [
|
|
134
|
+
`export const ${name} = {`,
|
|
135
|
+
...Object.entries(values).map(([k, v]) => ` ${key(k)}: ${asHex ? hex(v) : String(v)},`),
|
|
136
|
+
'} as const;',
|
|
137
|
+
'',
|
|
138
|
+
];
|
|
139
|
+
return [
|
|
140
|
+
'/*',
|
|
141
|
+
...provenance.split('\n').map(l => ` * ${l}`.trimEnd()),
|
|
142
|
+
' * Generated: rerun `gameslate-plugin addresses` rather than editing.',
|
|
143
|
+
' */',
|
|
144
|
+
'',
|
|
145
|
+
...block('SYMBOLS', table.symbols, true),
|
|
146
|
+
...block('OFFSETS', table.offsets, true),
|
|
147
|
+
...block('SIZES', table.sizes, true),
|
|
148
|
+
...block('CONSTANTS', table.constants, false),
|
|
149
|
+
].join('\n');
|
|
150
|
+
}
|
|
@@ -4,17 +4,29 @@ export declare const ASSET_LIMITS: {
|
|
|
4
4
|
readonly memoryBytes: number;
|
|
5
5
|
readonly stackBytes: number;
|
|
6
6
|
readonly loadMs: 10000;
|
|
7
|
-
/**
|
|
8
|
-
|
|
7
|
+
/**
|
|
8
|
+
* One asset. Extraction runs in a worker thread, away from the daemon, so this is a guard
|
|
9
|
+
* against a plugin that never returns, not a speed target: a Zelda map layer with an HD
|
|
10
|
+
* texture pack takes 40–70 s in the interpreter on a fast machine.
|
|
11
|
+
*/
|
|
12
|
+
readonly callMs: 600000;
|
|
9
13
|
/** the largest single input read */
|
|
10
14
|
readonly maxReadBytes: number;
|
|
11
15
|
/** all output bytes of one asset */
|
|
12
16
|
readonly maxOutputBytes: number;
|
|
13
17
|
};
|
|
14
18
|
/** The player's files for one run, opened by the core and read by range. */
|
|
19
|
+
/** An input's name as the plugin sees it: relative to the folder the player picked (`mods/x.o2r`),
|
|
20
|
+
* or just the file's name when files were picked one by one. */
|
|
21
|
+
export declare const inputName: (file: string, root: string | undefined) => string;
|
|
15
22
|
export declare class InputSet {
|
|
16
23
|
private readonly files;
|
|
17
|
-
|
|
24
|
+
/** `named`: files from outside the picked folder, under the name the plugin sees (the core's
|
|
25
|
+
* registry data for the game, `registry/manifest.json`) */
|
|
26
|
+
constructor(paths: readonly string[], root?: string, named?: ReadonlyArray<{
|
|
27
|
+
path: string;
|
|
28
|
+
name: string;
|
|
29
|
+
}>);
|
|
18
30
|
/** what the guest sees: handles, names and sizes, never paths */
|
|
19
31
|
describe(): SandboxInputFile[];
|
|
20
32
|
read(handle: number, offset: number, length: number): Uint8Array;
|
|
@@ -37,6 +49,8 @@ export declare class AssetSandbox {
|
|
|
37
49
|
inputs: InputSet;
|
|
38
50
|
log: (line: string) => void;
|
|
39
51
|
});
|
|
52
|
+
/** whether it broke a limit (out of memory, over budget) and refuses every call from now on */
|
|
53
|
+
get switchedOff(): boolean;
|
|
40
54
|
games(): string[];
|
|
41
55
|
list(gameId: string, pick: InputPick): AssetList;
|
|
42
56
|
build(gameId: string, id: string): BuiltAsset;
|
|
@@ -12,20 +12,37 @@ export const ASSET_LIMITS = {
|
|
|
12
12
|
memoryBytes: 1024 * 1024 * 1024,
|
|
13
13
|
stackBytes: 1024 * 1024,
|
|
14
14
|
loadMs: 10_000,
|
|
15
|
-
/**
|
|
16
|
-
|
|
15
|
+
/**
|
|
16
|
+
* One asset. Extraction runs in a worker thread, away from the daemon, so this is a guard
|
|
17
|
+
* against a plugin that never returns, not a speed target: a Zelda map layer with an HD
|
|
18
|
+
* texture pack takes 40–70 s in the interpreter on a fast machine.
|
|
19
|
+
*/
|
|
20
|
+
callMs: 600_000,
|
|
17
21
|
/** the largest single input read */
|
|
18
22
|
maxReadBytes: 64 * 1024 * 1024,
|
|
19
23
|
/** all output bytes of one asset */
|
|
20
24
|
maxOutputBytes: 256 * 1024 * 1024,
|
|
21
25
|
};
|
|
22
26
|
/** The player's files for one run, opened by the core and read by range. */
|
|
27
|
+
/** An input's name as the plugin sees it: relative to the folder the player picked (`mods/x.o2r`),
|
|
28
|
+
* or just the file's name when files were picked one by one. */
|
|
29
|
+
export const inputName = (file, root) => root ? path.relative(root, file).split(path.sep).join('/') : path.basename(file);
|
|
23
30
|
export class InputSet {
|
|
24
31
|
files = [];
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
32
|
+
/** `named`: files from outside the picked folder, under the name the plugin sees (the core's
|
|
33
|
+
* registry data for the game, `registry/manifest.json`) */
|
|
34
|
+
constructor(paths, root, named = []) {
|
|
35
|
+
const all = [...paths.map(p => ({ path: p, name: inputName(p, root) })), ...named];
|
|
36
|
+
try {
|
|
37
|
+
for (const { path: p, name } of all) {
|
|
38
|
+
const fd = fs.openSync(p, 'r');
|
|
39
|
+
this.files.push({ fd, name, size: fs.fstatSync(fd).size });
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
catch (e) {
|
|
43
|
+
/* the daemon runs for days: close what was opened before the one that failed */
|
|
44
|
+
this.close();
|
|
45
|
+
throw e;
|
|
29
46
|
}
|
|
30
47
|
}
|
|
31
48
|
/** what the guest sees: handles, names and sizes, never paths */
|
|
@@ -74,6 +91,10 @@ export class AssetSandbox {
|
|
|
74
91
|
log,
|
|
75
92
|
});
|
|
76
93
|
}
|
|
94
|
+
/** whether it broke a limit (out of memory, over budget) and refuses every call from now on */
|
|
95
|
+
get switchedOff() {
|
|
96
|
+
return this.guest.switchedOff;
|
|
97
|
+
}
|
|
77
98
|
games() {
|
|
78
99
|
const games = this.guest.call('games', []);
|
|
79
100
|
if (!Array.isArray(games) || !games.every(g => typeof g === 'string')) {
|
package/lib/host/index.d.ts
CHANGED
package/lib/host/index.js
CHANGED
package/lib/host/inspect.js
CHANGED
|
@@ -39,7 +39,7 @@ function describedProblem(manifest, d) {
|
|
|
39
39
|
}
|
|
40
40
|
function loadLive(quickJs, dir, manifest, log) {
|
|
41
41
|
const code = fs.readFileSync(path.join(dir, manifest.code.file), 'utf8');
|
|
42
|
-
const sandbox = new PluginSandbox(quickJs, code, manifest.id, log);
|
|
42
|
+
const sandbox = new PluginSandbox(quickJs, code, manifest.id, { log });
|
|
43
43
|
try {
|
|
44
44
|
const description = sandbox.describe();
|
|
45
45
|
const problem = describedProblem(manifest, description);
|
|
@@ -12,13 +12,22 @@ export declare const SANDBOX_LIMITS: {
|
|
|
12
12
|
};
|
|
13
13
|
/** One plugin's live code in its own runtime. */
|
|
14
14
|
export declare class PluginSandbox {
|
|
15
|
+
private readonly quickJs;
|
|
16
|
+
private readonly code;
|
|
15
17
|
private readonly name;
|
|
18
|
+
private guest;
|
|
19
|
+
private readonly restarts;
|
|
16
20
|
private readonly log;
|
|
17
|
-
private readonly
|
|
21
|
+
private readonly now;
|
|
18
22
|
private readonly memories;
|
|
19
23
|
private nextHandle;
|
|
20
24
|
private readonly viewsCache;
|
|
21
|
-
constructor(quickJs: QuickJSWASMModule, code: string, name: string,
|
|
25
|
+
constructor(quickJs: QuickJSWASMModule, code: string, name: string, opts: {
|
|
26
|
+
log: (line: string) => void;
|
|
27
|
+
now?: () => number;
|
|
28
|
+
});
|
|
29
|
+
private newGuest;
|
|
30
|
+
private restartIfSwitchedOff;
|
|
22
31
|
describe(): SandboxDescription;
|
|
23
32
|
/** The plugin as the core uses it: sources and packs whose code runs in here. */
|
|
24
33
|
module(description: SandboxDescription): PluginModule;
|
package/lib/host/live-sandbox.js
CHANGED
|
@@ -8,9 +8,15 @@ export { loadQuickJs } from './quickjs-guest.js';
|
|
|
8
8
|
* validated before the core uses it.
|
|
9
9
|
*
|
|
10
10
|
* Calls run on the daemon's thread: a poll costs well under a millisecond (the spike measured
|
|
11
|
-
* 0.26 ms) and the budget bounds the worst case. A plugin that breaks a limit is switched off
|
|
12
|
-
* a
|
|
11
|
+
* 0.26 ms) and the budget bounds the worst case. A plugin that breaks a limit is switched off; the
|
|
12
|
+
* next game session it's asked for gets it a fresh runtime, a few times an hour at most. One
|
|
13
|
+
* stalled poll on a loaded machine (seen 2026-11-30, with an emulator at 270% CPU) then costs a
|
|
14
|
+
* session, not live reading for the rest of the day, and a runaway plugin still stalls the
|
|
15
|
+
* daemon only a few times an hour.
|
|
13
16
|
*/
|
|
17
|
+
/* fresh runtimes after a plugin broke a limit, per plugin, within RESTART_WINDOW_MS */
|
|
18
|
+
const MAX_RESTARTS = 3;
|
|
19
|
+
const RESTART_WINDOW_MS = 60 * 60 * 1000;
|
|
14
20
|
export const SANDBOX_LIMITS = {
|
|
15
21
|
/* the real plugins use about 3.5 MB after loading */
|
|
16
22
|
memoryBytes: 32 * 1024 * 1024,
|
|
@@ -22,17 +28,28 @@ export const SANDBOX_LIMITS = {
|
|
|
22
28
|
};
|
|
23
29
|
/** One plugin's live code in its own runtime. */
|
|
24
30
|
export class PluginSandbox {
|
|
31
|
+
quickJs;
|
|
32
|
+
code;
|
|
25
33
|
name;
|
|
26
|
-
log;
|
|
27
34
|
guest;
|
|
35
|
+
restarts = [];
|
|
36
|
+
log;
|
|
37
|
+
now;
|
|
28
38
|
memories = new Map();
|
|
29
39
|
nextHandle = 1;
|
|
30
40
|
/* each state's views, so a pack's three view accessors cost one call */
|
|
31
41
|
viewsCache = new WeakMap();
|
|
32
|
-
constructor(quickJs, code, name,
|
|
42
|
+
constructor(quickJs, code, name, opts) {
|
|
43
|
+
this.quickJs = quickJs;
|
|
44
|
+
this.code = code;
|
|
33
45
|
this.name = name;
|
|
34
|
-
this.log = log;
|
|
35
|
-
this.
|
|
46
|
+
this.log = opts.log;
|
|
47
|
+
this.now = opts.now ?? Date.now;
|
|
48
|
+
this.guest = this.newGuest();
|
|
49
|
+
}
|
|
50
|
+
newGuest() {
|
|
51
|
+
const { quickJs, code, name, log } = this;
|
|
52
|
+
return new QuickJsGuest(quickJs, {
|
|
36
53
|
name,
|
|
37
54
|
code,
|
|
38
55
|
file: 'live.js',
|
|
@@ -42,6 +59,22 @@ export class PluginSandbox {
|
|
|
42
59
|
log,
|
|
43
60
|
});
|
|
44
61
|
}
|
|
62
|
+
/* A fresh runtime for a plugin that broke a limit, unless it has used up its restarts. Its
|
|
63
|
+
* old sessions die with the old runtime (they already failed every call). */
|
|
64
|
+
restartIfSwitchedOff() {
|
|
65
|
+
if (!this.guest.switchedOff)
|
|
66
|
+
return;
|
|
67
|
+
const now = this.now();
|
|
68
|
+
while (this.restarts.length && now - (this.restarts[0] ?? 0) > RESTART_WINDOW_MS)
|
|
69
|
+
this.restarts.shift();
|
|
70
|
+
if (this.restarts.length >= MAX_RESTARTS)
|
|
71
|
+
return;
|
|
72
|
+
this.restarts.push(now);
|
|
73
|
+
this.guest.dispose();
|
|
74
|
+
this.memories.clear();
|
|
75
|
+
this.guest = this.newGuest();
|
|
76
|
+
this.log(`[plugins] ${this.name}: restarted after breaking a sandbox limit`);
|
|
77
|
+
}
|
|
45
78
|
describe() {
|
|
46
79
|
const raw = this.call('describe', []);
|
|
47
80
|
if (typeof raw !== 'string')
|
|
@@ -88,6 +121,7 @@ export class PluginSandbox {
|
|
|
88
121
|
};
|
|
89
122
|
}
|
|
90
123
|
createSession(gameId, memory) {
|
|
124
|
+
this.restartIfSwitchedOff();
|
|
91
125
|
const handle = this.nextHandle++;
|
|
92
126
|
this.memories.set(handle, memory);
|
|
93
127
|
const info = {
|
|
@@ -15,6 +15,7 @@ export declare class QuickJsGuest {
|
|
|
15
15
|
private readonly runtime;
|
|
16
16
|
private readonly api;
|
|
17
17
|
private disabled;
|
|
18
|
+
private corrupted;
|
|
18
19
|
constructor(quickJs: QuickJSWASMModule, opts: {
|
|
19
20
|
name: string;
|
|
20
21
|
code: string;
|
|
@@ -28,6 +29,8 @@ export declare class QuickJsGuest {
|
|
|
28
29
|
});
|
|
29
30
|
/** Calls a guest function within the budget; its JSON-able result, dumped to the host. */
|
|
30
31
|
call(fn: string, args: ReadonlyArray<string | number>): unknown;
|
|
32
|
+
/** whether it broke a limit and refuses every call from now on */
|
|
33
|
+
get switchedOff(): boolean;
|
|
31
34
|
dispose(): void;
|
|
32
35
|
private disable;
|
|
33
36
|
private callGuest;
|
|
@@ -23,12 +23,16 @@ export async function loadQuickJs() {
|
|
|
23
23
|
return newQuickJSWASMModuleFromVariant(newVariant(releaseSync, { wasmBinary }));
|
|
24
24
|
}
|
|
25
25
|
export const errorText = (e) => (e instanceof Error ? e.message : String(e));
|
|
26
|
+
const CORRUPTED_GUESTS = [];
|
|
26
27
|
export class QuickJsGuest {
|
|
27
28
|
opts;
|
|
28
29
|
vm;
|
|
29
30
|
runtime;
|
|
30
31
|
api;
|
|
31
32
|
disabled = null;
|
|
33
|
+
/* runaway guest recursion exhausts the host's stack mid-call and leaves the runtime holding
|
|
34
|
+
* objects it can't account for: freeing it then aborts the whole QuickJS module */
|
|
35
|
+
corrupted = false;
|
|
32
36
|
constructor(quickJs, opts) {
|
|
33
37
|
this.opts = opts;
|
|
34
38
|
this.runtime = quickJs.newRuntime();
|
|
@@ -66,7 +70,9 @@ export class QuickJsGuest {
|
|
|
66
70
|
}
|
|
67
71
|
catch (e) {
|
|
68
72
|
const message = errorText(e);
|
|
69
|
-
if (/
|
|
73
|
+
if (/maximum call stack/i.test(message))
|
|
74
|
+
this.corrupted = true;
|
|
75
|
+
if (/interrupted|out of memory|stack overflow|maximum call stack/i.test(message))
|
|
70
76
|
this.disable(message);
|
|
71
77
|
throw e;
|
|
72
78
|
}
|
|
@@ -78,7 +84,17 @@ export class QuickJsGuest {
|
|
|
78
84
|
}
|
|
79
85
|
}
|
|
80
86
|
}
|
|
87
|
+
/** whether it broke a limit and refuses every call from now on */
|
|
88
|
+
get switchedOff() {
|
|
89
|
+
return this.disabled !== null;
|
|
90
|
+
}
|
|
81
91
|
dispose() {
|
|
92
|
+
if (this.corrupted) {
|
|
93
|
+
/* never freed (see `corrupted`); kept referenced so the library's finaliser doesn't
|
|
94
|
+
* free it either. Its memory goes with the thread or process. */
|
|
95
|
+
CORRUPTED_GUESTS.push(this);
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
82
98
|
if (this.api.alive)
|
|
83
99
|
this.api.dispose();
|
|
84
100
|
this.vm.dispose();
|