@typesol/cli 0.44.0 → 0.44.1
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 +50 -0
- package/dist/compiler/emit/alias.d.ts +16 -2
- package/dist/compiler/emit/alias.js +62 -4
- package/dist/compiler/emit/calls.js +15 -7
- package/dist/compiler/emit/cpi-core.d.ts +6 -0
- package/dist/compiler/emit/cpi-core.js +18 -5
- package/dist/compiler/emit/create.js +19 -11
- package/dist/compiler/emit/function.js +5 -5
- package/dist/compiler/emit/metaplex.js +17 -4
- package/dist/compiler/emit/token.js +64 -39
- package/docs/architecture.md +1 -1
- package/docs/audit.md +45 -6
- package/docs/errors.md +1 -1
- package/docs/language.md +6 -4
- package/docs/security-model.md +46 -11
- package/llms-full.txt +53 -16
- package/package.json +1 -1
- package/runtime/typesol_rt.h +8 -5
package/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,56 @@ codes, and says so here.
|
|
|
6
6
|
|
|
7
7
|
## Unreleased
|
|
8
8
|
|
|
9
|
+
## 0.44.1 — 2026-11-11
|
|
10
|
+
|
|
11
|
+
Fixes from a second internal review, of 0.44.0 (docs/audit.md). Of the 76 programs of the 0.44.0 sources, 74 compile
|
|
12
|
+
to the same binary as in 0.44.0 and 2 to a different one (mint-close-authority and allow-block-list-token, below); no
|
|
13
|
+
program of those sources is refused.
|
|
14
|
+
|
|
15
|
+
- Fixed: `mint.create(…, { transferFee })` with a `null` (or run-time None) config or withdraw authority made a mint
|
|
16
|
+
with fee 0 and no withdraw authority. Token-2022 reads a None COption as one byte and the compiler sent 33, so the
|
|
17
|
+
32 zero bytes were read as the next fields. A None authority is now one byte, for `transferFee`'s two and
|
|
18
|
+
`closeAuthority` (where Token-2022 ignored the extra bytes, now exact too); `initializeTransferFeeConfig` and the
|
|
19
|
+
other wrappers already encoded it right. Programs using `transferFee` or `closeAuthority` in `mint.create` /
|
|
20
|
+
`createIfNeeded` compile differently (transfer-fee to the same binary, mint-close-authority to another)
|
|
21
|
+
(test/integrations/token-init.test.ts, decoded by the Token-2022 decoder for each authority null, constant and
|
|
22
|
+
run-time).
|
|
23
|
+
- Fixed: a reference into account data held across a resize made through another parameter that may be the same
|
|
24
|
+
account was not refused: with `a: Dup<Mut<Bag>>` and `b: Mut<Bag>` (or a read-only `a: Account<Bag>`, or a struct
|
|
25
|
+
loaded with `loadAccount` / `loadAccountMut`) one account in both slots, `const e = a.entries.get(1n); b.tags.pop();
|
|
26
|
+
e.score = v` wrote another record, and a `for (const x of a.entries)` loop read shifted fields. The reference rule
|
|
27
|
+
now treats parameters that may be one account (a Dup pair, a read-only and a writable account of one type, a loaded
|
|
28
|
+
struct) as one name for push, pop, clear and a helper given the account: such a program is a compile error, as with
|
|
29
|
+
one name. Programs that hold no reference across the resize compile as before; none of the examples and ports is
|
|
30
|
+
refused (test/security/dup.test.ts).
|
|
31
|
+
- Fixed: twelve token wrappers (approve, approveChecked, mintToChecked, burnChecked, setAuthority,
|
|
32
|
+
updateTransferHook, updateInterestRate, transferCheckedWithFee, setTransferFee, initializeTokenGroup,
|
|
33
|
+
updateGroupMaxSize, updateGroupAuthority) and the Stake program's split, authorize and withdraw read
|
|
34
|
+
their signer seeds before an earlier argument ran: `approve(src, d, owner, next(st), ["vault", st.count, bump])`
|
|
35
|
+
signed for the count before `next(st)` changed it. The seeds are now lowered after the other arguments when an
|
|
36
|
+
argument runs code; calls whose arguments run none compile as before (test/integrations/tokenops.test.ts).
|
|
37
|
+
- Fixed: arguments read after later ones ran. The amount of `transfer`, `transferChecked`, `mintTo`, `burn` and
|
|
38
|
+
`systemProgram.transfer` was read at the CPI, after a seed list that runs code (`["vault", next(st), bump]`); account
|
|
39
|
+
text passed to `initializeMetadata`, `removeMetadataKey` and Metaplex `updateMetadataAccountsV2` (and that
|
|
40
|
+
call's `primarySaleHappened` / `isMutable`) changed when a later argument rewrote it. Each is now read first, only
|
|
41
|
+
where a later argument runs code, so other calls compile as before (test/integrations/tokenops.test.ts).
|
|
42
|
+
- Fixed: `mint.create` with extensions adding up to exactly 355 bytes (a Multisig's length) always failed: Token-2022
|
|
43
|
+
sizes such a mint 357 bytes and refuses any other size. The mint is now made 357 bytes; `createIfNeeded` accepts
|
|
44
|
+
that size already, and cannot reach 355 with the extensions it checks (test/integrations/token-init.test.ts).
|
|
45
|
+
- Fixed: `updateExtraAccountMetas` limited growth to 10 KiB past the account's current length instead of its length
|
|
46
|
+
when the instruction started, so after `resizeAccount(metas, metas.dataLen + 10240n)` it could write past the room
|
|
47
|
+
the runtime leaves, into the next account's record. It now counts from the length at entry (3016), in an
|
|
48
|
+
instruction and in a helper. Programs that call it compile differently (allow-block-list-token, to another binary)
|
|
49
|
+
(test/integrations/hook.test.ts).
|
|
50
|
+
- Fixed: the runtime grew a heap Vec (`ts_hvec_reserve`) through its own struct type while the program reads it
|
|
51
|
+
through the generated one, which the C aliasing rules let the optimizer treat as unrelated (no miscompile was
|
|
52
|
+
observed). The runtime's type is `may_alias`; no build flag changed, and no program of the 0.44.0 sources (heap Vecs
|
|
53
|
+
included) builds to another binary (test/language/borsh-vec.test.ts).
|
|
54
|
+
- Docs: docs/security-model.md and docs/language.md say what `pythPrice` and `switchboardValue` leave to the author
|
|
55
|
+
(the price's sign and zero, the confidence, a publish time or result slot in the future, rounding toward zero when
|
|
56
|
+
scaling) and that Switchboard staleness counts from `result.slot`; the review of 0.44.0 is recorded in
|
|
57
|
+
docs/audit.md and docs/security-model.md.
|
|
58
|
+
|
|
9
59
|
## 0.44.0 — 2026-11-11
|
|
10
60
|
|
|
11
61
|
Fixes from an internal review of 0.43.0, and the means to check a release. A helper's Pubkey, Option, string or u256
|
|
@@ -15,7 +15,9 @@ import { EmitterBase } from "./base.js";
|
|
|
15
15
|
* assignment whose target is reached through a Vec element (`writesThroughVec`) is then refused (statements.ts).
|
|
16
16
|
*
|
|
17
17
|
* Accounts passed twice are a separate rule: function.ts refuses two writable parameters naming one account (2040)
|
|
18
|
-
* unless `Dup<…>` allows it for in-place accounts.
|
|
18
|
+
* unless `Dup<…>` allows it for in-place accounts. Where data moves (a Vec resized, an account given to a helper:
|
|
19
|
+
* `moves`), `assertNotAliased` also treats the names that may be one account (`maySharePlace`: a Dup pair, a
|
|
20
|
+
* read-only and a writable account of one type, a loaded struct) as one, since the data moves under each of them.
|
|
19
21
|
*/
|
|
20
22
|
export declare abstract class AliasEmitter extends EmitterBase {
|
|
21
23
|
/**
|
|
@@ -38,7 +40,19 @@ export declare abstract class AliasEmitter extends EmitterBase {
|
|
|
38
40
|
* to the old object unchanged. Here a reference is a pointer into account data, so it would see the new
|
|
39
41
|
* value. Refuse when a live reference into the replaced place is still used afterwards.
|
|
40
42
|
*/
|
|
41
|
-
protected assertNotAliased(replaced: string[] | undefined, at: ts.Node, helper?: string, inPlace?: boolean): void;
|
|
43
|
+
protected assertNotAliased(replaced: string[] | undefined, at: ts.Node, helper?: string, inPlace?: boolean, moves?: boolean): void;
|
|
44
|
+
/**
|
|
45
|
+
* A reference path (`a.entries[]`) inside one of the `replaced` places named through another root that may be the
|
|
46
|
+
* same account (`b.entries` when `a` and `b` may be one account): the path, the two roots and why.
|
|
47
|
+
*/
|
|
48
|
+
private sharedPlace;
|
|
49
|
+
/**
|
|
50
|
+
* Whether two reference roots may name one account's data, read in place: both structs of one type (a struct
|
|
51
|
+
* account parameter or group field not `@layout borsh`, or a struct loaded with loadAccount / loadAccountMut), and
|
|
52
|
+
* accounts the entry checks do not keep apart (`mayBeSameAccount`: a Dup<…> or read-only one, a helper's). Returns
|
|
53
|
+
* why, for the message.
|
|
54
|
+
*/
|
|
55
|
+
protected maySharePlace(a: string, b: string): string | undefined;
|
|
42
56
|
/**
|
|
43
57
|
* Whether the variable declared by `decl` is read after `at` (later in its block, or anywhere in a loop around `at`).
|
|
44
58
|
* A parameter (of a closure or helper expanded here) is read after `at` when its function reads it after `at`; when
|
|
@@ -17,7 +17,9 @@ const TREE_NODE_METHODS = new Set(["min", "max", "nodeAt", "remove", "removeAt"]
|
|
|
17
17
|
* assignment whose target is reached through a Vec element (`writesThroughVec`) is then refused (statements.ts).
|
|
18
18
|
*
|
|
19
19
|
* Accounts passed twice are a separate rule: function.ts refuses two writable parameters naming one account (2040)
|
|
20
|
-
* unless `Dup<…>` allows it for in-place accounts.
|
|
20
|
+
* unless `Dup<…>` allows it for in-place accounts. Where data moves (a Vec resized, an account given to a helper:
|
|
21
|
+
* `moves`), `assertNotAliased` also treats the names that may be one account (`maySharePlace`: a Dup pair, a
|
|
22
|
+
* read-only and a writable account of one type, a loaded struct) as one, since the data moves under each of them.
|
|
21
23
|
*/
|
|
22
24
|
export class AliasEmitter extends EmitterBase {
|
|
23
25
|
/**
|
|
@@ -69,7 +71,7 @@ export class AliasEmitter extends EmitterBase {
|
|
|
69
71
|
* to the old object unchanged. Here a reference is a pointer into account data, so it would see the new
|
|
70
72
|
* value. Refuse when a live reference into the replaced place is still used afterwards.
|
|
71
73
|
*/
|
|
72
|
-
assertNotAliased(replaced, at, helper, inPlace = false) {
|
|
74
|
+
assertNotAliased(replaced, at, helper, inPlace = false, moves = false) {
|
|
73
75
|
if (!replaced)
|
|
74
76
|
return;
|
|
75
77
|
for (const scope of this.scopes)
|
|
@@ -78,14 +80,70 @@ export class AliasEmitter extends EmitterBase {
|
|
|
78
80
|
continue;
|
|
79
81
|
// A helper given an account changes it in place (`inPlace`): a local naming the whole account (a struct loaded
|
|
80
82
|
// with loadAccountMut) then reads what JS's object would. Only references into it are stale.
|
|
81
|
-
const
|
|
82
|
-
|
|
83
|
+
const inside = (p, r) => (p === r && !inPlace) || p.startsWith(`${r}.`) || p.startsWith(`${r}[]`);
|
|
84
|
+
const hit = replaced.find((r) => l.paths.some((p) => inside(p, r)));
|
|
85
|
+
// Data that moves (`moves`: a Vec resized, so the Vecs after it shift; an account a helper may resize) moves
|
|
86
|
+
// under every name of the account: a reference made through another parameter (or loaded struct) that may be
|
|
87
|
+
// this account is stale too.
|
|
88
|
+
const shared = !hit && moves ? this.sharedPlace(replaced, l.paths, inside) : undefined;
|
|
89
|
+
if ((!hit && !shared) || !this.usedAfter(l.decl, at))
|
|
83
90
|
continue;
|
|
91
|
+
if (shared)
|
|
92
|
+
this.fail(at, `"${name}" still refers to ${this.showPath(shared.path)}, and ${this.showPath(shared.root)} may be the same account as ${this.showPath(shared.other)} (${shared.why}): ${helper ? `${helper}() may move` : "this moves"} that data, so "${name}" would then reach another record. Copy the fields you need into locals first`);
|
|
84
93
|
this.fail(at, helper
|
|
85
94
|
? `"${name}" still refers to data in ${this.showPath(hit)}, and ${helper}() may replace it (then "${name}" would read the new value; in JS it keeps the old one). Copy the fields you need into locals first`
|
|
86
95
|
: `"${name}" still refers to ${this.showPath(hit)}; replacing it here would change what "${name}" reads (in JS it keeps the old value). Copy the fields you need into locals first`);
|
|
87
96
|
}
|
|
88
97
|
}
|
|
98
|
+
/**
|
|
99
|
+
* A reference path (`a.entries[]`) inside one of the `replaced` places named through another root that may be the
|
|
100
|
+
* same account (`b.entries` when `a` and `b` may be one account): the path, the two roots and why.
|
|
101
|
+
*/
|
|
102
|
+
sharedPlace(replaced, paths, inside) {
|
|
103
|
+
const rootOf = (p) => p.split(/[.[]/)[0];
|
|
104
|
+
for (const r of replaced)
|
|
105
|
+
for (const p of paths) {
|
|
106
|
+
const [R, P] = [rootOf(r), rootOf(p)];
|
|
107
|
+
if (R === P)
|
|
108
|
+
continue;
|
|
109
|
+
const why = this.maySharePlace(R, P);
|
|
110
|
+
if (why && inside(p, P + r.slice(R.length)))
|
|
111
|
+
return { path: p, root: P, other: R, why };
|
|
112
|
+
}
|
|
113
|
+
return undefined;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Whether two reference roots may name one account's data, read in place: both structs of one type (a struct
|
|
117
|
+
* account parameter or group field not `@layout borsh`, or a struct loaded with loadAccount / loadAccountMut), and
|
|
118
|
+
* accounts the entry checks do not keep apart (`mayBeSameAccount`: a Dup<…> or read-only one, a helper's). Returns
|
|
119
|
+
* why, for the message.
|
|
120
|
+
*/
|
|
121
|
+
maySharePlace(a, b) {
|
|
122
|
+
const struct = (root) => {
|
|
123
|
+
if (root.startsWith("@")) {
|
|
124
|
+
for (const s of this.scopes)
|
|
125
|
+
for (const l of s.values())
|
|
126
|
+
if (`@${l.c}` === root && l.loaded)
|
|
127
|
+
return l.loaded.struct;
|
|
128
|
+
return undefined;
|
|
129
|
+
}
|
|
130
|
+
const k = this.accounts.get(root)?.kind;
|
|
131
|
+
return k?.kind === "account" && k.struct.layout !== "borsh" ? k.struct.name : undefined;
|
|
132
|
+
};
|
|
133
|
+
const s = struct(a);
|
|
134
|
+
if (!s || s !== struct(b))
|
|
135
|
+
return undefined;
|
|
136
|
+
if (a.startsWith("@") || b.startsWith("@"))
|
|
137
|
+
return "a struct loaded from an account";
|
|
138
|
+
if (!this.mayBeSameAccount(a, b))
|
|
139
|
+
return undefined;
|
|
140
|
+
const [x, y] = [this.accounts.get(a), this.accounts.get(b)];
|
|
141
|
+
if (x.constraints.dup || y.constraints.dup)
|
|
142
|
+
return "Dup<…>";
|
|
143
|
+
if (!isWritable(x.kind) || !isWritable(y.kind))
|
|
144
|
+
return "a read-only account may be a writable one";
|
|
145
|
+
return "the entry checks do not keep them apart";
|
|
146
|
+
}
|
|
89
147
|
/**
|
|
90
148
|
* Whether the variable declared by `decl` is read after `at` (later in its block, or anywhere in a loop around `at`).
|
|
91
149
|
* A parameter (of a closure or helper expanded here) is read after `at` when its function reads it after `at`; when
|
|
@@ -717,8 +717,14 @@ export class CallEmitter extends ExpressionEmitter {
|
|
|
717
717
|
if (sys.kind.kind !== "system")
|
|
718
718
|
this.fail(args[3], "pass the SystemProgram parameter");
|
|
719
719
|
const { name: entries, count } = args[4] ? this.extraAccountMetas(args[4]) : { name: "NULL", count: 0 };
|
|
720
|
-
|
|
721
|
-
|
|
720
|
+
if (builtin === "initExtraAccountMetas")
|
|
721
|
+
this.line(`TS_TRY(ts_init_extra_metas(params, a_${payer.name}, a_${acct.name}, &a_${mint.name}->key, ${entries}, ${count}));`);
|
|
722
|
+
else {
|
|
723
|
+
// The growth limit counts from the account's length when the instruction started: entry_ in an instruction
|
|
724
|
+
// (function.ts), the length the entrypoint recorded in the record in a helper.
|
|
725
|
+
const entry = this.ix ? `entry_${acct.name}` : `ts_entry_len(a_${acct.name})`;
|
|
726
|
+
this.line(`TS_TRY(ts_update_extra_metas(params, a_${payer.name}, a_${acct.name}, &a_${mint.name}->key, ${entries}, ${count}, ${entry}));`);
|
|
727
|
+
}
|
|
722
728
|
return undefined;
|
|
723
729
|
}
|
|
724
730
|
case "remainingAccounts":
|
|
@@ -1146,7 +1152,7 @@ export class CallEmitter extends ExpressionEmitter {
|
|
|
1146
1152
|
const tailStart = () => {
|
|
1147
1153
|
if (!tail)
|
|
1148
1154
|
return undefined;
|
|
1149
|
-
this.assertNotAliased(tail.paths, node);
|
|
1155
|
+
this.assertNotAliased(tail.paths, node, undefined, false, true);
|
|
1150
1156
|
const at = this.tmp("tail");
|
|
1151
1157
|
const n = this.tmp("tail_n");
|
|
1152
1158
|
this.line(`uint8_t *${at} = ${tail.end};`);
|
|
@@ -1233,7 +1239,9 @@ export class CallEmitter extends ExpressionEmitter {
|
|
|
1233
1239
|
if (args.length !== 0)
|
|
1234
1240
|
this.fail(node, "pop() takes no arguments");
|
|
1235
1241
|
needWritable();
|
|
1236
|
-
|
|
1242
|
+
// A Vec of an account shrinks: the popped element is cut off under every name of the account.
|
|
1243
|
+
const growable = info.cap === undefined && !info.heap;
|
|
1244
|
+
this.assertNotAliased(elemPaths(), node, undefined, false, growable);
|
|
1237
1245
|
const st = this.storageValueType(vt.elem);
|
|
1238
1246
|
const n = this.tmp("n");
|
|
1239
1247
|
this.line(`uint64_t ${n} = ${count};`);
|
|
@@ -1254,7 +1262,7 @@ export class CallEmitter extends ExpressionEmitter {
|
|
|
1254
1262
|
this.line(`${valueC(st)} ${out} = ${val.c};`);
|
|
1255
1263
|
result = st.kind === "pubkey" ? { c: `(&${out})`, type: st } : { c: out, type: st };
|
|
1256
1264
|
}
|
|
1257
|
-
const t =
|
|
1265
|
+
const t = growable ? tailStart() : undefined;
|
|
1258
1266
|
if (t)
|
|
1259
1267
|
this.line(`ts_memmove(${t.at} - ${info.elemSize}, ${t.at}, ${t.n});`);
|
|
1260
1268
|
this.line(`${info.len} = (uint32_t)(${n} - 1);`);
|
|
@@ -1265,7 +1273,7 @@ export class CallEmitter extends ExpressionEmitter {
|
|
|
1265
1273
|
if (args.length !== 0)
|
|
1266
1274
|
this.fail(node, "clear() takes no arguments");
|
|
1267
1275
|
needWritable();
|
|
1268
|
-
this.assertNotAliased(elemPaths(), node);
|
|
1276
|
+
this.assertNotAliased(elemPaths(), node, undefined, false, info.cap === undefined && !info.heap);
|
|
1269
1277
|
const t = info.cap === undefined && !info.heap ? tailStart() : undefined;
|
|
1270
1278
|
if (t)
|
|
1271
1279
|
this.line(`ts_memmove((uint8_t *)${info.items}, ${t.at}, ${t.n});`);
|
|
@@ -2582,7 +2590,7 @@ export class CallEmitter extends ExpressionEmitter {
|
|
|
2582
2590
|
// account however it is named here (a parameter, an account group's field, a struct loaded with loadAccountMut).
|
|
2583
2591
|
for (const [i, p] of h.params.entries())
|
|
2584
2592
|
if (p.account && p.account.kind === "account" && p.account.mut)
|
|
2585
|
-
this.assertNotAliased(this.pathsOf(argNodes[i]), node, h.display, true);
|
|
2593
|
+
this.assertNotAliased(this.pathsOf(argNodes[i]), node, h.display, true, true);
|
|
2586
2594
|
const args = [
|
|
2587
2595
|
"params",
|
|
2588
2596
|
...pieces.map((x) => (typeof x === "number" ? (passedByPointer(values[x].type) ? this.pointerArg(values[x], mayWrite) : values[x].c) : x)),
|
|
@@ -13,6 +13,12 @@ export declare abstract class CpiCoreEmitter extends ViewEmitter {
|
|
|
13
13
|
count: number;
|
|
14
14
|
idl: IdlSeed[] | undefined;
|
|
15
15
|
};
|
|
16
|
+
/**
|
|
17
|
+
* Whether evaluating `n` may run code or write (a call other than a pure one, an assignment, ++ / --). `readers`:
|
|
18
|
+
* also count as pure the byte readers (`data.readU64(i)`) and a stake sysvar slot's `named(account, address)`
|
|
19
|
+
* marker, which only read (their arguments still count).
|
|
20
|
+
*/
|
|
21
|
+
protected runsCode(n: ts.Node, readers?: boolean): boolean;
|
|
16
22
|
/** A call that runs none of the program's code and writes nothing: an integer cast, min / max, a list's get(i). */
|
|
17
23
|
protected pureCall(n: ts.CallExpression): boolean;
|
|
18
24
|
protected describeSeed(el: ts.Expression): IdlSeed | undefined;
|
|
@@ -21,11 +21,7 @@ export class CpiCoreEmitter extends ViewEmitter {
|
|
|
21
21
|
// the list is written, as JS reads them then, unless nothing can run between the list and the CPI that reads it:
|
|
22
22
|
// no calls or writes among the seeds, nor in the other arguments of the call consuming the list (evaluated before
|
|
23
23
|
// or after it). A list bound to a name or passed to a helper (`bound`) outlives its expression: always copied.
|
|
24
|
-
const mutates = (n) =>
|
|
25
|
-
(ts.isBinaryExpression(n) && n.operatorToken.kind >= ts.SyntaxKind.FirstAssignment && n.operatorToken.kind <= ts.SyntaxKind.LastAssignment) ||
|
|
26
|
-
(ts.isPrefixUnaryExpression(n) && (n.operator === ts.SyntaxKind.PlusPlusToken || n.operator === ts.SyntaxKind.MinusMinusToken)) ||
|
|
27
|
-
ts.isPostfixUnaryExpression(n) ||
|
|
28
|
-
ts.forEachChild(n, mutates) === true;
|
|
24
|
+
const mutates = (n) => this.runsCode(n);
|
|
29
25
|
// The argument holding the list (`signer([…])`, a list of signer lists, an object of CPI options) and the call it
|
|
30
26
|
// is an argument of.
|
|
31
27
|
let consumer = node.parent;
|
|
@@ -121,6 +117,23 @@ export class CpiCoreEmitter extends ViewEmitter {
|
|
|
121
117
|
this.seedArrays.set(key, { name, scope: this.scopes[this.scopes.length - 1], at: this.lines.length - 1, text: this.lines.at(-1).trim() });
|
|
122
118
|
return { name, count: parts.length, idl };
|
|
123
119
|
}
|
|
120
|
+
/**
|
|
121
|
+
* Whether evaluating `n` may run code or write (a call other than a pure one, an assignment, ++ / --). `readers`:
|
|
122
|
+
* also count as pure the byte readers (`data.readU64(i)`) and a stake sysvar slot's `named(account, address)`
|
|
123
|
+
* marker, which only read (their arguments still count).
|
|
124
|
+
*/
|
|
125
|
+
runsCode(n, readers = false) {
|
|
126
|
+
if (readers &&
|
|
127
|
+
ts.isCallExpression(n) &&
|
|
128
|
+
((ts.isPropertyAccessExpression(n.expression) && /^read(?:U8|U16|U32|U64|U128|Pubkey)$/.test(n.expression.name.text)) ||
|
|
129
|
+
this.exportName(n.expression) === "named"))
|
|
130
|
+
return ts.forEachChild(n, (c) => this.runsCode(c, true)) === true;
|
|
131
|
+
return ((ts.isCallExpression(n) && !this.pureCall(n)) ||
|
|
132
|
+
(ts.isBinaryExpression(n) && n.operatorToken.kind >= ts.SyntaxKind.FirstAssignment && n.operatorToken.kind <= ts.SyntaxKind.LastAssignment) ||
|
|
133
|
+
(ts.isPrefixUnaryExpression(n) && (n.operator === ts.SyntaxKind.PlusPlusToken || n.operator === ts.SyntaxKind.MinusMinusToken)) ||
|
|
134
|
+
ts.isPostfixUnaryExpression(n) ||
|
|
135
|
+
ts.forEachChild(n, (c) => this.runsCode(c, readers)) === true);
|
|
136
|
+
}
|
|
124
137
|
/** A call that runs none of the program's code and writes nothing: an integer cast, min / max, a list's get(i). */
|
|
125
138
|
pureCall(n) {
|
|
126
139
|
const fn = this.exportName(n.expression);
|
|
@@ -321,18 +321,20 @@ export class CreateEmitter extends CpiCoreEmitter {
|
|
|
321
321
|
// Each initializer: its bytes, then a CPI with the mint as the only (writable) account.
|
|
322
322
|
const lines = [];
|
|
323
323
|
const cpi = (bytes) => {
|
|
324
|
-
const total = bytes.reduce((n,
|
|
324
|
+
const total = bytes.reduce((n, b) => n + (Array.isArray(b) ? b[1] : 33), 0);
|
|
325
325
|
const buf = this.tmp("xd");
|
|
326
326
|
lines.push(`{ uint8_t ${buf}[${total}]; uint64_t at = 0;`);
|
|
327
|
-
for (const
|
|
328
|
-
|
|
327
|
+
for (const b of bytes)
|
|
328
|
+
if (Array.isArray(b))
|
|
329
|
+
lines.push(` ts_memcpy(${buf} + at, ${b[0]}, ${b[1]}); at += ${b[1]};`);
|
|
330
|
+
else
|
|
331
|
+
lines.push(` ${buf}[at++] = ${b.option}.some ? 1 : 0;`, ` if (${b.option}.some) { ts_memcpy(${buf} + at, &${b.option}.v, 32); at += 32; }`);
|
|
329
332
|
lines.push(` SolAccountMeta m[1] = {{&${acct}->key, true, false}};`, ` TS_TRY(ts_cpi(params, ${tp}, ${buf}, at, m, 1, NULL, 0)); }`);
|
|
330
333
|
};
|
|
331
334
|
const tag = (n) => `(const uint8_t[]){${n}}`;
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
];
|
|
335
|
+
// Token-2022's COption<Pubkey> in instruction data (pack_pubkey_option): a tag byte, then the key only when Some.
|
|
336
|
+
// A None is one byte, and Token-2022 reads the next field right after it.
|
|
337
|
+
const coption = (t) => [{ option: t }];
|
|
336
338
|
let space = 0;
|
|
337
339
|
for (const name of Object.keys(KINDS)) {
|
|
338
340
|
const v = held.get(name);
|
|
@@ -441,8 +443,11 @@ export class CreateEmitter extends CpiCoreEmitter {
|
|
|
441
443
|
checks.push(`{ SolPubkey k, w = ${want}; if (!ts_t22_key(${acct}, ${c.type}, ${at}, &k)) ts_memzero(&k, 32); if (!ts_key_eq(&k, &w)) TS_FAIL(${c.code + 1 + i}, "TypeSOL: the mint's ${name} ${f === "v" ? "key" : f} differs"); }`);
|
|
442
444
|
});
|
|
443
445
|
}
|
|
444
|
-
// Base account (165, the mint padded to an account's size), the account type byte, then each TLV entry.
|
|
445
|
-
|
|
446
|
+
// Base account (165, the mint padded to an account's size), the account type byte, then each TLV entry. A total of
|
|
447
|
+
// 355 is a Multisig's length, which Token-2022 never gives a mint: its own size calculation adds 2 bytes then
|
|
448
|
+
// (try_calculate_account_len), and InitializeMint2 refuses an account of any other size.
|
|
449
|
+
const total = 165 + 1 + space;
|
|
450
|
+
return { space: total === 355 ? total + 2 : total, lines, checks };
|
|
446
451
|
}
|
|
447
452
|
/**
|
|
448
453
|
* `ata.create(payer)` / `ata.createIfNeeded(payer)` on `Ata<Init|InitIfNeeded<TokenAccount>, "owner", "mint">`
|
|
@@ -536,9 +541,12 @@ export class CreateEmitter extends CpiCoreEmitter {
|
|
|
536
541
|
if (a.length === 3)
|
|
537
542
|
this.line(`TS_TRY(ts_system_transfer(params, ${from.c}, ${to.c}, ${amt.c}));`);
|
|
538
543
|
else {
|
|
539
|
-
// A system-owned PDA of this program (a SOL vault) pays.
|
|
544
|
+
// A system-owned PDA of this program (a SOL vault) pays. JS reads the amount before the seed list: copied first
|
|
545
|
+
// when the list runs code that may change it.
|
|
546
|
+
const mark = this.lines.length;
|
|
540
547
|
const s = this.signingSeeds(a[3]);
|
|
541
|
-
this.
|
|
548
|
+
const lamports = this.runsCode(a[3], true) ? this.settle(amt, mark) : amt;
|
|
549
|
+
this.line(`TS_TRY(ts_system_transfer_signed(params, ${from.c}, ${to.c}, ${lamports.c}, ${s.name}, ${s.count}));`);
|
|
542
550
|
}
|
|
543
551
|
return undefined;
|
|
544
552
|
}
|
|
@@ -152,14 +152,14 @@ export class FunctionEmitter extends StatementEmitter {
|
|
|
152
152
|
head.push(...vecLenCheck(k.struct, v, `d_${a.name}`));
|
|
153
153
|
}
|
|
154
154
|
}
|
|
155
|
-
// realloc(x, …) needs the size the account had when the instruction started (the 10 KiB growth limit)
|
|
155
|
+
// realloc(x, …) needs the size the account had when the instruction started (the 10 KiB growth limit), as does
|
|
156
|
+
// updateExtraAccountMetas(payer, x, …).
|
|
156
157
|
const reallocated = (() => {
|
|
157
158
|
let found = false;
|
|
158
159
|
const visit = (nd) => {
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
this.accountAt(nd.arguments[0]) === a)
|
|
160
|
+
const fn = ts.isCallExpression(nd) ? (this.exportName(nd.expression) ?? "") : "";
|
|
161
|
+
const target = ts.isCallExpression(nd) ? nd.arguments[fn === "updateExtraAccountMetas" ? 1 : 0] : undefined;
|
|
162
|
+
if (["realloc", "resizeAccount", "updateExtraAccountMetas"].includes(fn) && target && this.accountAt(target) === a)
|
|
163
163
|
found = true;
|
|
164
164
|
ts.forEachChild(nd, visit);
|
|
165
165
|
};
|
|
@@ -327,9 +327,12 @@ export class MetaplexEmitter extends TokenEmitter {
|
|
|
327
327
|
for (const k of ["name", "symbol", "uri", "sellerFeeBasisPoints"])
|
|
328
328
|
if (!props.has(k))
|
|
329
329
|
this.fail(a[2], `data needs ${k} (it replaces the whole DataV2)`);
|
|
330
|
+
// A view of account text is read at the call: copied first when another argument runs code that may rewrite it.
|
|
331
|
+
const others = (n) => [...props.values(), ...a.slice(3)].some((o) => o !== n && this.runsCode(o, true));
|
|
330
332
|
const text = (k) => {
|
|
331
333
|
const t = this.tmp("md");
|
|
332
|
-
|
|
334
|
+
const v = this.expect(this.expr(props.get(k), STRING), STRING, props.get(k));
|
|
335
|
+
this.line(`TS_Str ${t} = ${(others(props.get(k)) ? this.ownedString(v) : v).c};`);
|
|
333
336
|
return t;
|
|
334
337
|
};
|
|
335
338
|
[name, symbol, uri] = ["name", "symbol", "uri"].map(text);
|
|
@@ -353,9 +356,19 @@ export class MetaplexEmitter extends TokenEmitter {
|
|
|
353
356
|
this.line(`SolPubkey ${t} = *(${key.c});`);
|
|
354
357
|
next = `&${t}`;
|
|
355
358
|
}
|
|
356
|
-
const flag = (
|
|
357
|
-
|
|
358
|
-
|
|
359
|
+
const flag = (i) => {
|
|
360
|
+
if (isNull(a[i]))
|
|
361
|
+
return "-1";
|
|
362
|
+
const f = `((${this.expect(this.expr(a[i], BOOL), BOOL, a[i]).c}) ? 1 : 0)`;
|
|
363
|
+
if (!a.slice(i + 1).some((n) => this.runsCode(n, true)))
|
|
364
|
+
return f;
|
|
365
|
+
// Read before a later argument that runs code, as JS reads it.
|
|
366
|
+
const t = this.tmp("flag");
|
|
367
|
+
this.line(`int ${t} = ${f};`);
|
|
368
|
+
return t;
|
|
369
|
+
};
|
|
370
|
+
const primary = flag(4);
|
|
371
|
+
const mutable = flag(5);
|
|
359
372
|
const seeds = this.signerSeeds(a[6], ua, a[1]);
|
|
360
373
|
this.line(`TS_TRY(ts_mpl_update_v2(params, a_${program.name}, a_${metadata.name}, a_${ua.name}, ${hasData}, ${name}, ${symbol}, ${uri}, ${fee}, ${creators}, ${ncreators}, ${collection}, ${next}, ${primary}, ${mutable}, ${seeds.name}, ${seeds.count}));`);
|
|
361
374
|
return undefined;
|