@typesol/cli 0.49.0 → 0.51.0
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/CHANGELOG.md +541 -0
- package/NOTICE +9 -0
- package/README.md +34 -15
- package/SECURITY.md +6 -3
- package/dist/cli.js +36 -5
- package/dist/compiler/analyze.d.ts +10 -1
- package/dist/compiler/analyze.js +234 -20
- package/dist/compiler/diagnostics.d.ts +4 -0
- package/dist/compiler/diagnostics.js +5 -1
- package/dist/compiler/emit/base.d.ts +35 -4
- package/dist/compiler/emit/base.js +46 -4
- package/dist/compiler/emit/borsh.js +8 -8
- package/dist/compiler/emit/calls.d.ts +6 -0
- package/dist/compiler/emit/calls.js +98 -18
- package/dist/compiler/emit/cpi-core.d.ts +3 -0
- package/dist/compiler/emit/cpi-core.js +11 -5
- package/dist/compiler/emit/cpi.js +6 -4
- package/dist/compiler/emit/create.js +9 -9
- package/dist/compiler/emit/expressions.d.ts +9 -1
- package/dist/compiler/emit/expressions.js +77 -0
- package/dist/compiler/emit/function.d.ts +2 -0
- package/dist/compiler/emit/function.js +71 -15
- package/dist/compiler/emit/outline.d.ts +35 -0
- package/dist/compiler/emit/outline.js +547 -0
- package/dist/compiler/emit/program.js +19 -4
- package/dist/compiler/emit/rbtree.js +1 -1
- package/dist/compiler/emit/shared.d.ts +69 -4
- package/dist/compiler/emit/shared.js +44 -9
- package/dist/compiler/emit/statements.d.ts +36 -2
- package/dist/compiler/emit/statements.js +332 -29
- package/dist/compiler/emit/storage.d.ts +16 -0
- package/dist/compiler/emit/storage.js +134 -13
- package/dist/compiler/emit/views.js +1 -1
- package/dist/compiler/hints.d.ts +1 -1
- package/dist/compiler/hints.js +9 -1
- package/dist/compiler/idl.js +1 -1
- package/dist/compiler/index.d.ts +30 -2
- package/dist/compiler/index.js +94 -16
- package/dist/compiler/model.d.ts +13 -1
- package/dist/compiler/stack.d.ts +71 -0
- package/dist/compiler/stack.js +309 -0
- package/dist/compiler/toolchain.d.ts +87 -5
- package/dist/compiler/toolchain.js +384 -19
- package/dist/keys.d.ts +12 -1
- package/dist/keys.js +57 -7
- package/dist/workspace.d.ts +9 -0
- package/dist/workspace.js +91 -16
- package/docker/deps/0.50.0.lock.json +1206 -0
- package/docker/deps/0.51.0.lock.json +1206 -0
- package/docs/architecture.md +1 -1
- package/docs/audit.md +16 -13
- package/docs/case-study-escrow.md +1 -1
- package/docs/case-study-jito-mev.md +14 -14
- package/docs/case-study-marginfi.md +127 -127
- package/docs/case-study-multisig.md +16 -4
- package/docs/case-study-raydium-clmm.md +44 -28
- package/docs/case-study-raydium-cp-swap.md +18 -18
- package/docs/cli.md +5 -4
- package/docs/glossary.md +2 -1
- package/docs/language-accounts.md +16 -10
- package/docs/language-basics.md +29 -2
- package/docs/language-builtins.md +4 -2
- package/docs/language-cpi.md +34 -0
- package/docs/language-native.md +50 -4
- package/docs/language-pdas.md +6 -2
- package/docs/language-statements.md +103 -12
- package/docs/language-types.md +93 -8
- package/docs/runtime.md +20 -3
- package/docs/security-model.md +6 -3
- package/docs/stability.md +10 -5
- package/docs/testing.md +6 -2
- package/docs/troubleshooting.md +45 -1
- package/lib/typesol.d.ts +25 -3
- package/llms-full.txt +403 -54
- package/package.json +1 -1
- package/releases/0.49.0.json +857 -0
- package/releases/0.50.0.json +874 -0
- package/runtime/typesol_builtins.c +47 -2
- package/runtime/typesol_rt.h +254 -54
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* When a program does not fit the stack (a frame over the SBPF limit, a call that passes arguments past its frame, a
|
|
3
|
+
* v1/v2 call path over 256 KiB), the build lays it out another way before it gives up, in a fixed order, measuring
|
|
4
|
+
* with clang after each step:
|
|
5
|
+
*
|
|
6
|
+
* 1. scope: the statements of each function that does not fit (and of the helpers inlined into it) get C blocks of
|
|
7
|
+
* their own, so clang can give the temporaries of different statements one slot. No behaviour changes.
|
|
8
|
+
* 2. noinline: helpers inlined into a function that does not fit are kept out of line, as if each were `@noinline`
|
|
9
|
+
* (largest frame first; never one the program marks `@inline`, one that takes a function, or one clang already
|
|
10
|
+
* keeps out of line). No behaviour changes; about 10 compute units per call.
|
|
11
|
+
* 3. heap: lists and structs of an instruction that does not fit are kept on the heap, as if each were `@heap`
|
|
12
|
+
* (largest first, while the instruction's heap locals together fit in the heap; never one marked `@stack`).
|
|
13
|
+
* About 17 compute units per declaration and its bytes of heap.
|
|
14
|
+
*
|
|
15
|
+
* A program that fits builds as before: none of this runs, and its bytes do not change. Every decision comes from the
|
|
16
|
+
* source and clang's measurements, in a fixed order, so the same sources and toolchain give the same layout and bytes
|
|
17
|
+
* (`typesol verify` and --docker builds run the same steps). `const STACK_AUTO = false` (or `--stack-auto off`) turns
|
|
18
|
+
* it off: the build then fails as it would without it.
|
|
19
|
+
*/
|
|
20
|
+
import ts from "typescript";
|
|
21
|
+
import { STACK_FRAME, STACK_FRAME_V1, StackLimitError } from "./toolchain.js";
|
|
22
|
+
/** At most this many layouts are tried after the first build fails. */
|
|
23
|
+
const ROUNDS = 12;
|
|
24
|
+
/** At most this many rounds of noinline for one function (each round keeps out of line what should be enough). */
|
|
25
|
+
const NOINLINE_ROUNDS = 4;
|
|
26
|
+
/** Bytes kept free beyond what a frame is over by, when choosing locals for the heap (clang's padding and spills). */
|
|
27
|
+
const MARGIN = 128;
|
|
28
|
+
/** How the program names a C function: "instruction f", "helper g()", "a loop of instruction f". */
|
|
29
|
+
export function describeFunction(fn) {
|
|
30
|
+
const loop = /^(.*)__loop\d+$/.exec(fn);
|
|
31
|
+
if (loop)
|
|
32
|
+
return `a loop of ${describeFunction(loop[1])}`;
|
|
33
|
+
if (fn.startsWith("ix_"))
|
|
34
|
+
return `instruction ${fn.slice(3)}`;
|
|
35
|
+
if (fn.startsWith("fn_"))
|
|
36
|
+
return `helper ${fn.slice(3)}()`;
|
|
37
|
+
return fn;
|
|
38
|
+
}
|
|
39
|
+
/** The C function that emits `fn`: an outlined loop's is the instruction or helper it was moved out of. */
|
|
40
|
+
const ownerOf = (fn) => fn.replace(/__loop\d+$/, "");
|
|
41
|
+
/** The generated C's functions: body lines, whether declared noinline, and the helpers and loops each calls. */
|
|
42
|
+
export function cFunctionsOf(c) {
|
|
43
|
+
const out = new Map();
|
|
44
|
+
let current;
|
|
45
|
+
for (const l of c.split("\n")) {
|
|
46
|
+
const m = /^(__attribute__\(\(noinline\)\) )?static (__attribute__\(\(noinline\)\) )?uint64_t ((?:fn|ix)_\w+)\(.*\) \{$/.exec(l);
|
|
47
|
+
if (m) {
|
|
48
|
+
current = [];
|
|
49
|
+
out.set(m[3], { body: current, noinline: !!(m[1] || m[2]) });
|
|
50
|
+
}
|
|
51
|
+
else if (l === "}")
|
|
52
|
+
current = undefined;
|
|
53
|
+
else
|
|
54
|
+
current?.push(l);
|
|
55
|
+
}
|
|
56
|
+
return out;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* The helpers whose bodies clang may inline into `fn`, directly or through other such helpers, each with whether a
|
|
60
|
+
* call on the way is inside a loop. Helpers declared noinline (and loops moved out) have frames of their own.
|
|
61
|
+
*/
|
|
62
|
+
export function inlinedInto(fns, fn) {
|
|
63
|
+
const out = new Map();
|
|
64
|
+
const visit = (f, inLoop) => {
|
|
65
|
+
const loops = [];
|
|
66
|
+
for (const raw of fns.get(f)?.body ?? []) {
|
|
67
|
+
const l = raw.replace(/"(?:[^"\\]|\\.)*"/g, '""');
|
|
68
|
+
// A line closing a block first ("} else {") pops before it pushes.
|
|
69
|
+
let at = 0;
|
|
70
|
+
for (const ch of l) {
|
|
71
|
+
if (ch === "}")
|
|
72
|
+
loops.pop();
|
|
73
|
+
else if (ch === "{")
|
|
74
|
+
loops.push(/^\s*(?:while|for|do)\b/.test(l.slice(0, at)));
|
|
75
|
+
at++;
|
|
76
|
+
}
|
|
77
|
+
const loop = inLoop || loops.some(Boolean) || /^\s*(?:while|for)\b/.test(l);
|
|
78
|
+
for (const call of l.matchAll(/\b(fn_\w+)\(/g)) {
|
|
79
|
+
const callee = call[1];
|
|
80
|
+
const info = fns.get(callee);
|
|
81
|
+
if (!info || info.noinline || /__loop\d+$/.test(callee))
|
|
82
|
+
continue;
|
|
83
|
+
const seen = out.get(callee);
|
|
84
|
+
if (seen === undefined || (!seen && loop)) {
|
|
85
|
+
out.set(callee, loop || !!seen);
|
|
86
|
+
visit(callee, loop);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
visit(fn, false);
|
|
92
|
+
return out;
|
|
93
|
+
}
|
|
94
|
+
/** The SBPF frame limit of one function. */
|
|
95
|
+
const frameLimit = (sbpf) => (sbpf === "v0" ? STACK_FRAME : STACK_FRAME_V1);
|
|
96
|
+
/**
|
|
97
|
+
* Build `steps`, laying the program out another way when it does not fit the stack (see the module comment). Returns
|
|
98
|
+
* the generated program that built, and what was done (undefined when it built as it is). Throws the first build's
|
|
99
|
+
* error when nothing made it fit, with a line saying what was tried, and that error alone when `auto` is false.
|
|
100
|
+
*/
|
|
101
|
+
export function buildWithStackLayout(steps, first, opts) {
|
|
102
|
+
try {
|
|
103
|
+
steps.compile(first.c, false);
|
|
104
|
+
return { gen: first };
|
|
105
|
+
}
|
|
106
|
+
catch (e) {
|
|
107
|
+
if (!(e instanceof StackLimitError) || !opts.auto)
|
|
108
|
+
throw e;
|
|
109
|
+
return relayout(steps, first, e, opts.sbpf);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
function relayout(steps, first, original, sbpf) {
|
|
113
|
+
const model = first.model;
|
|
114
|
+
const helpers = new Map([...model.helpers.values()].map((h) => [`fn_${h.name}`, h]));
|
|
115
|
+
const scope = new Set();
|
|
116
|
+
const noinline = new Set();
|
|
117
|
+
const heap = new Set();
|
|
118
|
+
const actions = [];
|
|
119
|
+
const frames = {};
|
|
120
|
+
const noinlineRounds = new Map();
|
|
121
|
+
const skipped = [];
|
|
122
|
+
/** Each candidate helper's own frame, measured with every candidate out of line. */
|
|
123
|
+
let ownFrames;
|
|
124
|
+
let failures = original.failures;
|
|
125
|
+
let gen = first;
|
|
126
|
+
let measured;
|
|
127
|
+
try {
|
|
128
|
+
measured = steps.measure(first.c);
|
|
129
|
+
}
|
|
130
|
+
catch {
|
|
131
|
+
measured = new Map();
|
|
132
|
+
}
|
|
133
|
+
for (const f of failures)
|
|
134
|
+
if (f.fn !== "entrypoint")
|
|
135
|
+
frames[f.fn] = { before: f.kind === "path" ? measured.get(f.fn) : (f.bytes ?? measured.get(f.fn)) };
|
|
136
|
+
/** The heap candidates the last generate listed (none yet: the first build was generated without a layout). */
|
|
137
|
+
let candidates;
|
|
138
|
+
const plan = (extra = {}) => ({ scope, noinline, heap, candidates: [], ...extra });
|
|
139
|
+
const tried = () => actions.length
|
|
140
|
+
? `\nTo fit, the build also tried (it did not fit with these either; with const STACK_AUTO = false it does not try): ${actions.map(describeAction).join("; ")}` +
|
|
141
|
+
(skipped.length ? `; not moved to the heap: ${[...new Set(skipped)].join("; ")}` : "")
|
|
142
|
+
: skipped.length
|
|
143
|
+
? `\nTo fit, the build would have moved locals to the heap, but they do not fit there: ${[...new Set(skipped)].join("; ")}`
|
|
144
|
+
: "";
|
|
145
|
+
for (let round = 0; round < ROUNDS; round++) {
|
|
146
|
+
const fns = cFunctionsOf(gen.c);
|
|
147
|
+
// The functions that did not fit, in a fixed order (the instruction on a v1/v2 path, or each on it).
|
|
148
|
+
const failing = [...new Map(failures.map((f) => [f.fn, f])).values()].sort((a, b) => (a.fn < b.fn ? -1 : a.fn > b.fn ? 1 : 0));
|
|
149
|
+
const onPath = (f) => (f.kind === "path" ? (f.path?.map((p) => p.fn).filter((n) => fns.has(n)) ?? []) : [f.fn]);
|
|
150
|
+
// 1. Scope the temporaries of each function that does not fit, and of the helpers inlined into it.
|
|
151
|
+
let changed = false;
|
|
152
|
+
for (const f of failing)
|
|
153
|
+
for (const fn of onPath(f)) {
|
|
154
|
+
const owner = ownerOf(fn);
|
|
155
|
+
if (!fns.has(owner) && !fns.has(fn))
|
|
156
|
+
continue;
|
|
157
|
+
const add = [owner, ...[...inlinedInto(fns, fn).keys()].sort()].filter((n) => !scope.has(n));
|
|
158
|
+
for (const n of add) {
|
|
159
|
+
scope.add(n);
|
|
160
|
+
actions.push({ action: "scope", function: describeFunction(n), for: describeFunction(fn) });
|
|
161
|
+
changed = true;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
// 2. Keep helpers out of line (not for a v1/v2 path: that only adds frames to it).
|
|
165
|
+
if (!changed) {
|
|
166
|
+
for (const f of failing) {
|
|
167
|
+
if (f.kind === "path" || !fns.has(f.fn))
|
|
168
|
+
continue;
|
|
169
|
+
const done = noinlineRounds.get(f.fn) ?? 0;
|
|
170
|
+
if (done >= NOINLINE_ROUNDS)
|
|
171
|
+
continue;
|
|
172
|
+
const inlined = inlinedInto(fns, f.fn);
|
|
173
|
+
const candidates = [...inlined.keys()].filter((n) => {
|
|
174
|
+
const h = helpers.get(n);
|
|
175
|
+
return (!!h &&
|
|
176
|
+
!h.noinline &&
|
|
177
|
+
!h.inline &&
|
|
178
|
+
!noinline.has(n) &&
|
|
179
|
+
// A helper that takes a function is expanded where it is given a closure; keep its C as it is.
|
|
180
|
+
!h.node.parameters.some((p) => !!p.type && ts.isFunctionTypeNode(p.type)));
|
|
181
|
+
});
|
|
182
|
+
if (!candidates.length)
|
|
183
|
+
continue;
|
|
184
|
+
// Measured with every candidate out of line: each one's own frame, and what this function's frame would be.
|
|
185
|
+
// (A helper clang keeps out of line may still be inlined at some calls: that it has a frame says little.)
|
|
186
|
+
const probe = steps.generate(plan({ noinline: new Set([...noinline, ...candidates]) }));
|
|
187
|
+
const sizes = steps.measure(probe.c);
|
|
188
|
+
ownFrames = new Map([...(ownFrames ?? []), ...candidates.map((n) => [n, sizes.get(n) ?? 0])]);
|
|
189
|
+
const now = measured.get(f.fn);
|
|
190
|
+
if (now !== undefined && (sizes.get(f.fn) ?? now) >= now) {
|
|
191
|
+
// Out of line, they would not make this frame smaller.
|
|
192
|
+
noinlineRounds.set(f.fn, NOINLINE_ROUNDS);
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
const ranked = candidates
|
|
196
|
+
.filter((n) => (ownFrames.get(n) ?? 0) > 0)
|
|
197
|
+
.sort((a, b) => Number(inlined.get(a)) - Number(inlined.get(b)) || ownFrames.get(b) - ownFrames.get(a) || (a < b ? -1 : a > b ? 1 : 0));
|
|
198
|
+
const over = f.kind === "call" ? 1 : Math.max(1, (f.bytes ?? measured.get(f.fn) ?? 0) - (f.limit ?? frameLimit(sbpf)));
|
|
199
|
+
let gained = 0;
|
|
200
|
+
for (const n of ranked) {
|
|
201
|
+
if (gained >= over)
|
|
202
|
+
break;
|
|
203
|
+
noinline.add(n);
|
|
204
|
+
gained += ownFrames.get(n);
|
|
205
|
+
actions.push({ action: "noinline", helper: describeFunction(n), for: describeFunction(f.fn), calls: inlined.get(n) ? "in a loop" : "outside loops" });
|
|
206
|
+
changed = true;
|
|
207
|
+
}
|
|
208
|
+
if (gained)
|
|
209
|
+
noinlineRounds.set(f.fn, done + 1);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
// 3. Keep an instruction's own lists and structs on the heap.
|
|
213
|
+
if (!changed) {
|
|
214
|
+
const kib = model.constants.get("HEAP_SIZE_KIB")?.value;
|
|
215
|
+
const room = (typeof kib === "bigint" ? Number(kib) : 32) * 1024 - 8;
|
|
216
|
+
const byKey = new Map();
|
|
217
|
+
if (!candidates) {
|
|
218
|
+
const p = plan();
|
|
219
|
+
steps.generate(p);
|
|
220
|
+
candidates = p.candidates;
|
|
221
|
+
}
|
|
222
|
+
for (const c of candidates)
|
|
223
|
+
if (!byKey.has(c.key))
|
|
224
|
+
byKey.set(c.key, c);
|
|
225
|
+
const all = [...byKey.values()];
|
|
226
|
+
for (const f of failing) {
|
|
227
|
+
const where = onPath(f).filter((fn) => ownerOf(fn).startsWith("ix_"));
|
|
228
|
+
if (!where.length)
|
|
229
|
+
continue;
|
|
230
|
+
const ix = ownerOf(where[0]);
|
|
231
|
+
// Held in the frame that does not fit: declared in that C function's body.
|
|
232
|
+
const held = (c) => where.some((fn) => (fns.get(fn)?.body ?? []).some((l) => new RegExp(`^\\s*[A-Za-z_]\\w*\\s+${c.c}(?:\\[|;)`).test(l)));
|
|
233
|
+
let used = all.filter((c) => c.fn === ix && (c.heap || heap.has(c.key))).reduce((n, c) => n + c.bytes, 0);
|
|
234
|
+
const ranked = all
|
|
235
|
+
.filter((c) => c.fn === ix && !c.heap && !c.stack && !heap.has(c.key) && held(c))
|
|
236
|
+
.sort((a, b) => b.bytes - a.bytes || (a.at < b.at ? -1 : a.at > b.at ? 1 : 0) || (a.key < b.key ? -1 : 1));
|
|
237
|
+
const limit = f.limit ?? frameLimit(sbpf);
|
|
238
|
+
const size = f.kind === "path" ? (f.bytes ?? 0) : (f.bytes ?? measured.get(f.fn) ?? limit);
|
|
239
|
+
const need = Math.max(0, size - limit) + MARGIN;
|
|
240
|
+
let moved = 0;
|
|
241
|
+
for (const c of ranked) {
|
|
242
|
+
if (moved >= need)
|
|
243
|
+
break;
|
|
244
|
+
if (used + c.bytes > room) {
|
|
245
|
+
skipped.push(`${c.name} (${c.at}) takes ${c.bytes} bytes, and the heap holds ${room}${used ? ` (${used} of them taken by the instruction's other heap locals)` : ""}`);
|
|
246
|
+
continue;
|
|
247
|
+
}
|
|
248
|
+
heap.add(c.key);
|
|
249
|
+
used += c.bytes;
|
|
250
|
+
moved += c.bytes;
|
|
251
|
+
actions.push({ action: "heap", local: c.name, at: c.at, bytes: c.bytes, for: describeFunction(f.fn) });
|
|
252
|
+
changed = true;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
if (!changed)
|
|
257
|
+
break;
|
|
258
|
+
const p = plan();
|
|
259
|
+
gen = steps.generate(p);
|
|
260
|
+
candidates = p.candidates;
|
|
261
|
+
try {
|
|
262
|
+
steps.compile(gen.c, true);
|
|
263
|
+
}
|
|
264
|
+
catch (e) {
|
|
265
|
+
if (!(e instanceof StackLimitError))
|
|
266
|
+
throw new StackLimitError(original.failures, `${original.message}\nTo fit, the build tried another layout, and it did not compile (please report this with the program): ${e.message.split("\n").slice(0, 3).join(" ")}`);
|
|
267
|
+
failures = e.failures;
|
|
268
|
+
try {
|
|
269
|
+
measured = steps.measure(gen.c);
|
|
270
|
+
}
|
|
271
|
+
catch {
|
|
272
|
+
measured = new Map();
|
|
273
|
+
}
|
|
274
|
+
continue;
|
|
275
|
+
}
|
|
276
|
+
let after = new Map();
|
|
277
|
+
try {
|
|
278
|
+
after = steps.measure(gen.c);
|
|
279
|
+
}
|
|
280
|
+
catch {
|
|
281
|
+
/* the frames after are not known */
|
|
282
|
+
}
|
|
283
|
+
for (const fn of Object.keys(frames))
|
|
284
|
+
frames[fn].after = after.get(fn);
|
|
285
|
+
return { gen, report: { actions, frames } };
|
|
286
|
+
}
|
|
287
|
+
throw new StackLimitError(original.failures, original.message + tried());
|
|
288
|
+
}
|
|
289
|
+
/** One action as a note's text. */
|
|
290
|
+
export function describeAction(a) {
|
|
291
|
+
switch (a.action) {
|
|
292
|
+
case "scope":
|
|
293
|
+
return a.function === a.for
|
|
294
|
+
? `the temporaries of ${a.for} put in blocks of their own`
|
|
295
|
+
: `the temporaries of ${a.function} (inlined into ${a.for}) put in blocks of their own`;
|
|
296
|
+
case "noinline":
|
|
297
|
+
return `${a.helper} kept out of line, for ${a.for} (about 10 compute units more per call; called ${a.calls})`;
|
|
298
|
+
case "heap":
|
|
299
|
+
return `${a.local} (${a.at}) of ${a.for.replace(/^a loop of /, "")} kept on the heap: ${a.bytes} bytes of heap, about 17 compute units`;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
/** The notes a build prints for what it did: one per action, then the frames before and after. */
|
|
303
|
+
export function stackNotes(report, sbpf) {
|
|
304
|
+
const limit = frameLimit(sbpf);
|
|
305
|
+
return [
|
|
306
|
+
...Object.entries(report.frames).map(([fn, f]) => `${describeFunction(fn)} did not fit the stack${f.before !== undefined ? ` (${f.before} bytes` + (f.before > limit ? `; SBPF ${sbpf} allows ${limit})` : ")") : ""}${f.after !== undefined ? `; now ${f.after} bytes` : ""}`),
|
|
307
|
+
...report.actions.map(describeAction),
|
|
308
|
+
];
|
|
309
|
+
}
|
|
@@ -35,16 +35,33 @@ export declare function findToolchain(sbpf?: Sbpf, host?: {
|
|
|
35
35
|
platform: string;
|
|
36
36
|
arch: string;
|
|
37
37
|
}): Toolchain;
|
|
38
|
-
/**
|
|
39
|
-
/** The SBF stack frame of one function. */
|
|
38
|
+
/** SBPF v0: the fixed stack frame of every function. */
|
|
40
39
|
export declare const STACK_FRAME = 4096;
|
|
40
|
+
/**
|
|
41
|
+
* SBPF v1 and v2: the largest frame of one function. Frames are sized per function there, but a load or store
|
|
42
|
+
* addresses its slot with a signed 16-bit offset from the frame pointer, and the backend encodes a larger offset
|
|
43
|
+
* wrapped (+34,284 becomes -31,252) without a message: a bigger frame reads and writes the wrong memory.
|
|
44
|
+
*/
|
|
45
|
+
export declare const STACK_FRAME_V1 = 32767;
|
|
46
|
+
/** SBPF v1 and v2: the stack every frame on a call path shares (64 frames of 4 KiB); past it the program fails. */
|
|
47
|
+
export declare const STACK_TOTAL_V1 = 262144;
|
|
41
48
|
/** clang's flags for SBF objects (the SDK's sbf.mk for the chosen SBPF version, plus TypeSOL's warnings). */
|
|
42
49
|
export declare function sbfCflags(sbpf?: Sbpf): string[];
|
|
43
50
|
/** The flags runtime/typesol_builtins.c is compiled with: the program's, and the SBPF version (its assembly differs). */
|
|
44
51
|
export declare function builtinsCflags(sbpf?: Sbpf): string[];
|
|
45
|
-
|
|
52
|
+
/**
|
|
53
|
+
* Compile generated C to an SBF shared object (the SDK's sbf.mk flags for the chosen SBPF version), refusing any
|
|
54
|
+
* program whose stack does not fit: a frame over the limit (clang's -Wframe-larger-than), a frame the SBF backend
|
|
55
|
+
* reports it cannot lay out (its own messages, printed while clang still succeeds), and on SBPF v1 and v2 a call path
|
|
56
|
+
* whose frames together pass 256 KiB (from the linked program's calls and frame-pointer adjustments).
|
|
57
|
+
*/
|
|
58
|
+
export declare function buildSharedObject(cFile: string, outSo: string, sbpf?: Sbpf, tc?: Toolchain, opts?: {
|
|
59
|
+
brief?: boolean;
|
|
60
|
+
}): void;
|
|
46
61
|
/** Link SBF objects into a program: libc from platform-tools, nothing from its Rust libraries. */
|
|
47
|
-
export declare function linkSharedObject(objects: string[], outSo: string, tc: Toolchain, extra?: string[]
|
|
62
|
+
export declare function linkSharedObject(objects: string[], outSo: string, tc: Toolchain, extra?: string[], opts?: {
|
|
63
|
+
keepSymbols?: boolean;
|
|
64
|
+
}): void;
|
|
48
65
|
/**
|
|
49
66
|
* The syscalls a TypeSOL program may leave to the SBF loader: every one the SDK's headers declare (`@SYSCALL` in
|
|
50
67
|
* platform-tools-sdk/sbf/c/inc/sol/inc/*.inc) and every one runtime/typesol_rt.h declares itself, with `abort`.
|
|
@@ -130,6 +147,34 @@ export declare function toolchainStrict(): boolean;
|
|
|
130
147
|
* programs), or an error in strict mode. `info` is the active toolchain's, unless given.
|
|
131
148
|
*/
|
|
132
149
|
export declare function checkToolchain(info?: ToolchainInfo, warn?: (message: string) => void): ToolchainInfo;
|
|
150
|
+
/** One way a program's stack does not fit, by C function (`ix_<name>`, `fn_<name>`, a loop's `…__loopN`, `entrypoint`). */
|
|
151
|
+
export interface StackFailure {
|
|
152
|
+
/**
|
|
153
|
+
* "frame": the function's frame is over the limit (clang's -Wframe-larger-than); "call": a call in it passes
|
|
154
|
+
* arguments past its frame, "offset": its frame passes the backend's largest offset (the SBF backend's messages);
|
|
155
|
+
* "path": SBPF v1/v2, the deepest call path's frames together pass 256 KiB (`fn` is the instruction on it).
|
|
156
|
+
*/
|
|
157
|
+
kind: "frame" | "call" | "offset" | "path";
|
|
158
|
+
fn: string;
|
|
159
|
+
/** The frame (or, for a path, the path's frames together) in bytes, and the limit, when the message gives them. */
|
|
160
|
+
bytes?: number;
|
|
161
|
+
limit?: number;
|
|
162
|
+
/** A path's functions from the entrypoint, with their frames, when the linked program could be named. */
|
|
163
|
+
path?: {
|
|
164
|
+
fn: string;
|
|
165
|
+
frame: number;
|
|
166
|
+
}[];
|
|
167
|
+
}
|
|
168
|
+
/** A build refused because the program's stack does not fit: the message, and what did not fit. */
|
|
169
|
+
export declare class StackLimitError extends Error {
|
|
170
|
+
readonly failures: StackFailure[];
|
|
171
|
+
constructor(failures: StackFailure[], message: string);
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* The stack frame of every function clang keeps in the object (helpers it inlined everywhere have none), from a
|
|
175
|
+
* compile of `cFile` with the build's flags but no frame limit, and -fstack-usage. Nothing is kept of that compile.
|
|
176
|
+
*/
|
|
177
|
+
export declare function measureFrames(cFile: string, sbpf?: Sbpf, tc?: Toolchain): Map<string, number>;
|
|
133
178
|
/** One variable slot of a function's stack frame as clang laid it out: its size and the C variables sharing it. */
|
|
134
179
|
export interface FrameSlot {
|
|
135
180
|
size: number;
|
|
@@ -144,4 +189,41 @@ export declare function frameLayouts(remarks: string): Map<string, FrameSlot[]>;
|
|
|
144
189
|
* clang's frame-size errors in the program's terms: which instruction or helper, how much, the locals that take the
|
|
145
190
|
* room (from `layouts`, clang's frame layout of the generated C `c`) and what to do.
|
|
146
191
|
*/
|
|
147
|
-
export declare function stackError(message: string, layouts?: Map<string, FrameSlot[]>, c?: string):
|
|
192
|
+
export declare function stackError(message: string, layouts?: Map<string, FrameSlot[]>, c?: string, sbpf?: Sbpf): StackLimitError;
|
|
193
|
+
/**
|
|
194
|
+
* The SBF backend's own stack messages in clang's stderr, in the program's terms, or undefined when there are none.
|
|
195
|
+
* The backend prints them and clang still exits 0: "A function call in method F overwrites values in the frame" (a
|
|
196
|
+
* call that passes arguments on the stack, past a frame at the limit) and "Function F Stack offset of N exceeded max
|
|
197
|
+
* offset of M". Either way the program it builds may compute wrong results.
|
|
198
|
+
*/
|
|
199
|
+
export declare function backendStackError(stderr: string, sbpf?: Sbpf, c?: string): StackLimitError | undefined;
|
|
200
|
+
/** The deepest call path of a linked SBPF v1/v2 program: its functions (by address) and their frames together. */
|
|
201
|
+
export interface CallPath {
|
|
202
|
+
/** The frames of the path's functions together, in bytes. */
|
|
203
|
+
bytes: number;
|
|
204
|
+
/** The path from the entrypoint, each function's address (in .text) and frame. */
|
|
205
|
+
path: {
|
|
206
|
+
address: number;
|
|
207
|
+
frame: number;
|
|
208
|
+
}[];
|
|
209
|
+
/** Indirect calls (callx) in functions reachable from the entrypoint, whose targets the check cannot follow. */
|
|
210
|
+
indirect: number;
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* The deepest call path of a linked SBPF v1/v2 program, from its own instructions: functions start at the
|
|
214
|
+
* entrypoint and at every target of an internal call (`call` with a pc-relative target; a call the loader resolves
|
|
215
|
+
* through a relocation is a syscall, which runs outside the program's stack), and a function's frame is what it
|
|
216
|
+
* subtracts from the frame pointer r10 (`add64 r10, -N`). Since this is the code clang produced, a helper it inlined
|
|
217
|
+
* adds its locals to its caller's frame and its calls to the caller's calls.
|
|
218
|
+
*
|
|
219
|
+
* Not followed: indirect calls (`callx`), counted in `indirect`; TypeSOL emits none (closures are expanded where
|
|
220
|
+
* they are called). A cycle (recursion) throws: TypeSOL refuses recursion, so it would be a compiler bug.
|
|
221
|
+
*/
|
|
222
|
+
export declare function deepestCallPath(elf: Buffer): CallPath;
|
|
223
|
+
/**
|
|
224
|
+
* Function names by .text offset, from `named` (the same program linked with its symbols), when its code is the code
|
|
225
|
+
* of `stripped`; empty otherwise.
|
|
226
|
+
*/
|
|
227
|
+
export declare function functionSymbols(named: Buffer, stripped: Buffer): Map<number, string>;
|
|
228
|
+
/** The error for a call path whose frames pass SBPF v1/v2's 256 KiB of stack. */
|
|
229
|
+
export declare function callPathError(deepest: CallPath, names: Map<number, string>, sbpf: Sbpf): StackLimitError;
|