@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.
Files changed (79) hide show
  1. package/CHANGELOG.md +541 -0
  2. package/NOTICE +9 -0
  3. package/README.md +34 -15
  4. package/SECURITY.md +6 -3
  5. package/dist/cli.js +36 -5
  6. package/dist/compiler/analyze.d.ts +10 -1
  7. package/dist/compiler/analyze.js +234 -20
  8. package/dist/compiler/diagnostics.d.ts +4 -0
  9. package/dist/compiler/diagnostics.js +5 -1
  10. package/dist/compiler/emit/base.d.ts +35 -4
  11. package/dist/compiler/emit/base.js +46 -4
  12. package/dist/compiler/emit/borsh.js +8 -8
  13. package/dist/compiler/emit/calls.d.ts +6 -0
  14. package/dist/compiler/emit/calls.js +98 -18
  15. package/dist/compiler/emit/cpi-core.d.ts +3 -0
  16. package/dist/compiler/emit/cpi-core.js +11 -5
  17. package/dist/compiler/emit/cpi.js +6 -4
  18. package/dist/compiler/emit/create.js +9 -9
  19. package/dist/compiler/emit/expressions.d.ts +9 -1
  20. package/dist/compiler/emit/expressions.js +77 -0
  21. package/dist/compiler/emit/function.d.ts +2 -0
  22. package/dist/compiler/emit/function.js +71 -15
  23. package/dist/compiler/emit/outline.d.ts +35 -0
  24. package/dist/compiler/emit/outline.js +547 -0
  25. package/dist/compiler/emit/program.js +19 -4
  26. package/dist/compiler/emit/rbtree.js +1 -1
  27. package/dist/compiler/emit/shared.d.ts +69 -4
  28. package/dist/compiler/emit/shared.js +44 -9
  29. package/dist/compiler/emit/statements.d.ts +36 -2
  30. package/dist/compiler/emit/statements.js +332 -29
  31. package/dist/compiler/emit/storage.d.ts +16 -0
  32. package/dist/compiler/emit/storage.js +134 -13
  33. package/dist/compiler/emit/views.js +1 -1
  34. package/dist/compiler/hints.d.ts +1 -1
  35. package/dist/compiler/hints.js +9 -1
  36. package/dist/compiler/idl.js +1 -1
  37. package/dist/compiler/index.d.ts +30 -2
  38. package/dist/compiler/index.js +94 -16
  39. package/dist/compiler/model.d.ts +13 -1
  40. package/dist/compiler/stack.d.ts +71 -0
  41. package/dist/compiler/stack.js +309 -0
  42. package/dist/compiler/toolchain.d.ts +87 -5
  43. package/dist/compiler/toolchain.js +384 -19
  44. package/dist/keys.d.ts +12 -1
  45. package/dist/keys.js +57 -7
  46. package/dist/workspace.d.ts +9 -0
  47. package/dist/workspace.js +91 -16
  48. package/docker/deps/0.50.0.lock.json +1206 -0
  49. package/docker/deps/0.51.0.lock.json +1206 -0
  50. package/docs/architecture.md +1 -1
  51. package/docs/audit.md +16 -13
  52. package/docs/case-study-escrow.md +1 -1
  53. package/docs/case-study-jito-mev.md +14 -14
  54. package/docs/case-study-marginfi.md +127 -127
  55. package/docs/case-study-multisig.md +16 -4
  56. package/docs/case-study-raydium-clmm.md +44 -28
  57. package/docs/case-study-raydium-cp-swap.md +18 -18
  58. package/docs/cli.md +5 -4
  59. package/docs/glossary.md +2 -1
  60. package/docs/language-accounts.md +16 -10
  61. package/docs/language-basics.md +29 -2
  62. package/docs/language-builtins.md +4 -2
  63. package/docs/language-cpi.md +34 -0
  64. package/docs/language-native.md +50 -4
  65. package/docs/language-pdas.md +6 -2
  66. package/docs/language-statements.md +103 -12
  67. package/docs/language-types.md +93 -8
  68. package/docs/runtime.md +20 -3
  69. package/docs/security-model.md +6 -3
  70. package/docs/stability.md +10 -5
  71. package/docs/testing.md +6 -2
  72. package/docs/troubleshooting.md +45 -1
  73. package/lib/typesol.d.ts +25 -3
  74. package/llms-full.txt +403 -54
  75. package/package.json +1 -1
  76. package/releases/0.49.0.json +857 -0
  77. package/releases/0.50.0.json +874 -0
  78. package/runtime/typesol_builtins.c +47 -2
  79. package/runtime/typesol_rt.h +254 -54
@@ -50,7 +50,9 @@ export function record(stats: Mut<Stats>, by: u64, change: i64): void {
50
50
  u8; convert with u64(...) etc.` Comparisons (`<`, `<=`, `===`, …) need equal types too.
51
51
  - **Every operation is checked.** `+ - * / % **` and their assignments (`+=` … `**=`) fail the instruction on
52
52
  overflow (5100) or division by zero (5101); `**` with a negative exponent fails as well (JavaScript throws there).
53
- Unlike JavaScript there are no silent wraps and no `Infinity`.
53
+ Unlike JavaScript there are no silent wraps and no `Infinity`. A signed type's minimum divided by -1 overflows, and
54
+ so does its remainder by -1 (`x % -1n` with an `i8` holding -128 fails with 5100, as Rust's checked `%` does, where
55
+ JavaScript gives 0); a typed constant computing it is a compile error ([constants](language-basics.md#constants)).
54
56
  - **Bitwise operators.** `& | ^` work on equal types; `~` flips the bits within the type's width (`~1n` as a `u8` is
55
57
  254). Both shifts fail (5100) when the amount is negative or not below the width (`x >> 64n` on a `u64`), and `<<`
56
58
  also when bits would be lost; `>>` is arithmetic on signed types. Unlike JavaScript, where a bigint shift never
@@ -199,7 +201,8 @@ export function report(owner: Pubkey, amount: u64, limit: Option<u64>): void {
199
201
  or `Hash` prints as base58 and an `Option` of an integer, boolean, `Pubkey` or `Hash` as its value or `null`, as
200
202
  JavaScript prints it (after `x !== null`, the value). A template is joined in one allocation; text known when the
201
203
  program compiles (literals, string and `Pubkey` constants) is folded in; `s += …` grows the newest heap string in
202
- place. Tagged templates (`` String.raw`…` ``) are refused.
204
+ place. The left side of `s += …` is read before the right side runs, as in JavaScript: a right side that assigns `s`
205
+ (a closure it calls) does not change what is appended to. Tagged templates (`` String.raw`…` ``) are refused.
203
206
 
204
207
  ## Enums
205
208
 
@@ -262,6 +265,7 @@ export function apply(vault: Mut<Vault>, action: Action): void {
262
265
 
263
266
  - A data enum is a union of object types, each with a `kind` string literal: Rust's enum with fields, Borsh-encoded as
264
267
  Anchor encodes one. `a.kind === "Deposit"` or `switch (a.kind)`, and TypeScript narrowing picks the variant.
268
+ Two data enums are not compared with `===` (refused): compare their `kind`, then the fields of the narrowed variants.
265
269
  - Reading a variant's field is checked again at run time: when the value is not that variant, 5112.
266
270
  - In account data: 1 tag byte then room for the largest variant. In arguments and events: the tag, then that
267
271
  variant's fields.
@@ -297,12 +301,87 @@ export function step(player: Mut<Player>, dx: u64): void {
297
301
  ```
298
302
 
299
303
  - An `interface` with fixed-size fields is a struct; exported ones are in the IDL. Fields are stored in declaration
300
- order, back to back, with no padding.
304
+ order, back to back, with no padding. An object type alias is the same struct: `export type Point = { x: u64; y: u64 }`
305
+ works wherever `export interface Point { … }` does (account data, arguments, results, events, helper parameters,
306
+ fields of other structs). An object type with `kind: "Name"` is a [data enum](#data-enums) of one variant instead.
307
+ Account groups are interfaces only.
301
308
  - A struct literal names every field (`{ x, y: 2n }`); a missing or unknown field is an error. An object literal needs
302
309
  a known struct type: a typed local (`const p: Point = { … }`), a field, a helper parameter, `emit<T>(…)`.
303
- - `a.home = b.home` copies the whole struct, as Rust and Anchor do; how `const` binds one is in
304
- [copies and references](language-statements.md#copies-and-references).
310
+ - `a.home = b.home` copies the whole struct, as Rust and Anchor do; how `const` binds one is in [copies and
311
+ references](language-statements.md#copies-and-references). A copy between places reached through pointers (account
312
+ data, a view, a helper's struct parameter) of 80 bytes or more is the memmove syscall (16 compute units up to 2,749
313
+ bytes, one more per 250 bytes past that), as Rust's memcpy is; below, 8-byte loads and stores (26 units for 80
314
+ bytes, 54 for 192). A copy into or out of a local of the function (a local copy, a helper's result, a struct literal
315
+ built in a temporary) takes the syscall from 400 bytes, where clang stops unrolling the loads and stores (and about
316
+ 1 unit a byte is the loop's cost): below that the optimizer drops what nothing reads, which a call would prevent.
317
+ The same holds wherever TypeSOL copies a whole value itself (Borsh fields, instruction arguments, a helper's copy of
318
+ a struct argument, events), and a value it zeroes whole (`x.o = null` on an `Option<struct>`, a data-enum literal)
319
+ takes the memset syscall from 192 bytes in account data and 600 in a local.
320
+ - `===`, `!==`, `==` and `!=` on two structs are refused (`=== on two structs is not supported`): a struct is held by
321
+ reference and copied where JavaScript would share it, so the comparison could not give JavaScript's answer. Compare
322
+ the fields you need: `a.x === b.x && a.y === b.y`. The same holds for data enums, lists and Options of a struct
323
+ ([conditions](language-statements.md#conditions)).
305
324
  - A struct cannot contain itself.
325
+ - Generic structs (`interface Pair<T>`, `type Pair<T> = { … }`) and intersection types (`type B = A & { … }`) are
326
+ refused; write the struct out, or extend it ([below](#structs-that-extend-others)).
327
+
328
+ ### Structs that extend others
329
+
330
+ ```ts
331
+ import { type Mut, type Pubkey, type u32, type u64, type u8 } from "typesol";
332
+
333
+ export interface Base {
334
+ id: u32;
335
+ owner: Pubkey;
336
+ }
337
+
338
+ export type Tagged = { tag: u8 };
339
+
340
+ // Fields, in this order: id, owner (Base's), tag (Tagged's), then qty.
341
+ export interface Item extends Base, Tagged {
342
+ qty: u64;
343
+ }
344
+
345
+ export interface Shelf {
346
+ base: Base;
347
+ item: Item;
348
+ }
349
+
350
+ export function stock(shelf: Mut<Shelf>, qty: u64): void {
351
+ shelf.item = { ...shelf.base, tag: 1n, qty }; // a parent's fields, then the child's own
352
+ shelf.base = { ...shelf.item }; // a child's copy of the parent's fields
353
+ }
354
+ ```
355
+
356
+ - `interface B extends A, C { … }` (with `A` and `C` structs of the program, interfaces or object types) has A's fields,
357
+ then C's, then its own, in that order: that is its layout in account data, arguments and events, and its order in
358
+ the IDL. In the fixed layout growable `Vec`s still go last ([fixed layout](#structs)); a `BigVec` must stay the last
359
+ field, so a struct cannot extend one that ends in a `BigVec` and add fields.
360
+ - A field in two parents is one field when it has the same type in both (`C.a and A.a have different types; B cannot
361
+ extend both` otherwise). A struct cannot declare an inherited field again, extend a generic type, a data enum or an
362
+ account group, or extend the same struct twice. Doc tags (`@layout`, `@discriminator`) are each struct's own.
363
+ - A `B` is not passed or assigned where an `A` is expected (`a Child is not used as its parent Base (TypeSOL copies
364
+ structs where JavaScript shares the object): …`): JavaScript would pass the object itself, extra fields and all,
365
+ which TypeSOL, copying structs, could not reproduce. Write the parent's copy as a spread, `{ ...child }`, or field by
366
+ field.
367
+
368
+ ### Spreads in struct literals
369
+
370
+ - `{ ...p, x: 1n }` and `{ x: 1n, ...p }` build a struct from p's fields and the entries given: entries run left to
371
+ right, each once, and a later one overwrites an earlier one, as in JavaScript. `...p` reads p's fields at its place,
372
+ so an entry after it that changes p (a helper writing the account) does not change what was copied, and an entry
373
+ before it does.
374
+ - p is a struct place (a local, a field, a list element, a helper's struct parameter) or a struct account; of the
375
+ literal's type, of a struct the literal's type extends (its fields are copied, the rest are given), or of one that
376
+ extends it (the literal's fields are copied from it).
377
+ - A spread copies as assignment does ([copies and references](language-statements.md#copies-and-references)). For
378
+ scalars, strings and Options of scalars that is JavaScript's result. A field holding a struct, list or data enum
379
+ would be shared by JavaScript between p and the new value, so it must be given after the spread:
380
+ `{ ...p, pos: { ...p.pos } }` is a copy in JavaScript too, while `{ ...p }` alone is refused (`...p would copy pos,
381
+ a Point: JavaScript shares that object between the two values, TypeSOL copies it; give pos after the spread …`).
382
+ - `acct.p = { ...acct.p, x: 1n }` reads the old fields: the new value is built before it is stored.
383
+ - Spreads are for struct literals of a known struct type. In `emit<T>(…)`, a data enum literal, a list literal or a
384
+ built-in's object argument they are refused.
306
385
 
307
386
  ### File-local structs
308
387
 
@@ -330,6 +409,7 @@ export function bid(auction: Mut<Auction>, amount: u64, cap: Option<u64>): void
330
409
  ```
331
410
 
332
411
  - `Option<T>` (or `T | null`) is a value or `null`. Compare with `=== null` / `!== null`; TypeScript narrows it.
412
+ Two Options of a struct are not compared with `===` (refused): check each against `null`, then compare fields.
333
413
  - Using an empty option where a value is needed fails with 5108; `x!` is the value, or 5108.
334
414
  - `a ?? b` takes `b` only when `a` is empty, and evaluates `b` only then; the result is an Option when `b` can be null.
335
415
  - `===` between options, or with a value, follows JavaScript: `null === null`, and an empty option never equals a
@@ -444,8 +524,8 @@ own PDA must sign, which only its `executeTransaction` does, for a transaction e
444
524
  - Setting one up costs about the same at any size: `fixedVec([])` of 1280 bytes is about 10 CU (the free slots are
445
525
  zeroed by the memset syscall, the elements written in place, also as a struct literal's field).
446
526
  - **Size limit.** One list holds at most 2 KiB of elements (the FixedVec's u32 length aside: `FixedVec<u32, 512>`
447
- fits). The 4 KiB stack frame is shared with the rest of the function, and a frame that overflows is reported with
448
- its biggest locals; a bigger list is a compile error naming its size. [`@heap`](#heap-values) moves one to the heap.
527
+ fits). The 4 KiB stack frame (on SBPF v1 and v2, at most 32,767 bytes) is shared with the rest of the function, and
528
+ a frame that overflows is reported with its biggest locals; a bigger list is a compile error naming its size. [`@heap`](#heap-values) moves one to the heap.
449
529
  - A `FixedVec<u8, N>` is also a byte buffer: `pushLE`, `pushKey`, `pushBorsh`, `setLE`, `truncate`
450
530
  ([byte builders](language-native.md#byte-strings-built-by-hand)).
451
531
 
@@ -581,6 +661,8 @@ Locals live on the stack. These live on the heap (32 KiB by default;
581
661
  - strings built at run time (5110 when the heap is full);
582
662
  - a `@layout borsh` struct's `Vec` fields while the instruction runs;
583
663
  - the CPI scratch lists of a program that declares `MAX_ACCOUNTS`;
664
+ - a list or struct local of an instruction that the build moved there because the program did not fit the stack
665
+ ([Memory](language-basics.md#memory-stack-and-heap)), as it moves a helper's struct result the instruction holds;
584
666
  - a list or struct local marked `/** @heap */`:
585
667
 
586
668
  ```ts
@@ -596,6 +678,9 @@ export function collect(n: u8): void {
596
678
 
597
679
  `/** @heap */` on an instruction's list or struct local keeps it on the heap instead of the stack: one allocation per
598
680
  declaration, reused when a loop declares it again, up to the heap's size, for about 18 CU. Not in helpers, which may
599
- run many times while the heap is never freed.
681
+ run many times while the heap is never freed. A local the build moves to the heap to fit the stack is handled the same
682
+ way (one allocation per declaration, reused by a loop; for a helper's struct result, one per call site), and only while
683
+ the instruction's `@heap` and moved locals together fit in the heap; the note gives its bytes. `/** @stack */` on a
684
+ list or struct local (or on `const r = helper(…)` returning a struct) keeps it off the heap; a local is not both.
600
685
 
601
686
  **Next:** [Statements and functions](language-statements.md): which TypeScript is TypeSOL.
package/docs/runtime.md CHANGED
@@ -9,7 +9,13 @@ Since 0.45.0 the runtime is no longer header-only: `runtime/typesol_builtins.c`
9
9
  `__divdi3`, …). It is compiled with every program by the same clang and linked after it, in place of platform-tools'
10
10
  `compiler-builtins` library, whose layout differed by host package
11
11
  ([stability.md](stability.md#what-builds-the-same-bytes-where)); `test/language/builtins.test.ts` checks it against
12
- BigInt and its compute units against compiler-builtins'.
12
+ BigInt and its compute units against compiler-builtins'. Multiplication and shifts are assembly written for the 128-bit
13
+ libcall convention of clang 20 (platform-tools v1.52) and earlier: the result's address in r1, the operands after it.
14
+ clang 22 (platform-tools v1.57) passes the operands from r1 and returns the result in registers, so for a clang above
15
+ 20 the file compiles C definitions of the same four functions instead (`TS_ASM_LIBCALLS`), which take whatever
16
+ convention that clang gives their C signatures; with the assembly, a program built by clang 22 stored its first
17
+ 128-bit product through the operand's low half ("Access violation writing 8 bytes at address …"). The test runs the C
18
+ definitions too, compiled by the tested clang as a clang above 20 would select them.
13
19
 
14
20
  ## Conventions
15
21
 
@@ -36,6 +42,17 @@ BigInt and its compute units against compiler-builtins'.
36
42
  - **No byte loops from the SDK.** The SDK's `sol_memcpy`, `sol_memcmp` and `SolPubkey_same` move one byte per
37
43
  iteration. The runtime uses `ts_ld64` / `ts_st64` (unaligned 8-byte loads and stores, which SBF allows) in
38
44
  `ts_memcpy`, `ts_memzero`, `ts_mem_eq`, `ts_key_eq` and `ts_memmove`.
45
+ - **The memory syscalls where they cost less.** `sol_memmove_` and `sol_memset_` cost about 16 and 14 compute units
46
+ whatever the length up to 2,749 bytes. Whole values of a size known when compiling are copied by `TS_COPY` from 80
47
+ bytes and zeroed by `TS_ZERO` from 192 between places reached through pointers (account data, views, a helper's
48
+ pointer parameters), where the unrolled loads and stores cost at least 10 units more. Where a side is a local of the
49
+ function, `TS_COPY_LOCAL` / `TS_MEMCPY` take the syscall from 400 bytes and `TS_ZERO_LOCAL` from 600, where clang
50
+ stops unrolling: below that the optimizer drops the stores and loads nothing reads (a struct literal built in place, a
51
+ helper's result read field by field), which a call would prevent. The size folds when compiling, so smaller copies
52
+ compile to exactly the code they were. Run-time lengths, always loops, take the syscall from 64 bytes: the bytes an
53
+ account grows by (`ts_memzero_run`, one unit more below 64) and an account's data at `close` (`TS_CLOSE`, decided from
54
+ the type's smallest size). The measured table is above these macros in `typesol_rt.h`. Copies always use memmove:
55
+ `sol_memcpy_` fails the transaction on overlapping ranges, which two views of one account can give.
39
56
  - **Same results as the reference.** Fast paths must give bit-identical results to what they replace: `ts_add_overflow_u64`
40
57
  matches `__builtin_add_overflow`, the rent fast path and `ts_rent_slow` match the double computation, PDA search
41
58
  matches `sol_try_find_program_address`. A change here needs a test against the reference, not only against itself.
@@ -47,14 +64,14 @@ In file order:
47
64
  | Section | What it provides |
48
65
  | --- | --- |
49
66
  | Error codes, `TS_FAIL`, `TS_TRY` | The codes above and the two macros |
50
- | Word-wise memory helpers | `ts_ld64`, `ts_st64`, `ts_memcpy`, `ts_memmove`, `ts_memzero`, `ts_mem_eq`; checked u64 add/subtract/multiply |
67
+ | Word-wise memory helpers | `ts_ld64`, `ts_st64`, `ts_memcpy`, `ts_memmove`, `ts_memzero`, `ts_mem_eq`; the memory syscalls past their thresholds (`TS_COPY`, `TS_MEMCPY`, `TS_ZERO`, `ts_memzero_run`); checked u64 add/subtract/multiply |
51
68
  | Well-known program ids | System, Token, Token-2022, ATA, Metaplex and the other programs the language names |
52
69
  | Strings | `TS_Str` views (pointer, length), UTF-8 validation, comparison, `FixedString<N>` storage |
53
70
  | Entry point input | `TsAccount` (the loader's serialized account, read in place), `TsParams`, `ts_deserialize` |
54
71
  | Account checks | owner, discriminator, signer, writable, executable, address, size, uniqueness (`ts_check_*`), growable Vec lengths; `ts_owned`, the native views' owner check with its one-account cache (`TS_OWNER_CACHE`, forgotten at every CPI and owner write by `TS_OWNER_FORGET`) |
55
72
  | Little-endian IO | `ts_read_le` and friends for argument decoding and Borsh |
56
73
  | Sysvars | clock, rent (`ts_rent`, `ts_minimum_balance_of`, `ts_minimum_balance`), program data |
57
- | PDA | `ts_pda_hash`, `ts_create_pda_address`, `ts_find_pda` (canonical bump), `ts_require_pda` |
74
+ | PDA | `ts_pda_hash`, `ts_create_pda_address`, `ts_find_pda` (canonical bump: 255 down to 1, as the syscall), `ts_require_pda`; their `_fit` forms, without the run-time check of the seed limits, for seed lists the compiler proved within them (`TS_PDA_SEEDS_FIT` for the account-creating helpers when every list of the program is) |
58
75
  | System program CPIs | create, assign, allocate, transfer, `ts_create_pda`, `ts_resize` / `ts_realloc`, close |
59
76
  | Token-2022 extensions | TLV walking for the extensions the language reads |
60
77
  | Metaplex Core and Bubblegum | account readers (`coreAssetOwner`, …) |
@@ -180,9 +180,12 @@ exposed to them.
180
180
  The compiler and runtime have had internal security reviews, the latest of 0.44.0 (above); its findings are fixed in
181
181
  0.44.1. It was an internal review, not an independent audit; it covered the token, Token-2022, Metaplex and Stake
182
182
  wrappers, the oracles and SBPF v1/v2 builds of the examples that the review of 0.43.0 left out, but not the Metaplex
183
- wrappers for unusual argument shapes beyond reading them, the large ports on SBPF v1/v2, or Windows. 0.45.0 to 0.49.0
183
+ wrappers for unusual argument shapes beyond reading them, the large ports on SBPF v1/v2, or Windows. 0.45.0 to 0.51.0
184
184
  have had no security review. They add, among others, `runtime/typesol_builtins.c` (the
185
185
  compiler builtins as TypeSOL's own C, 0.45.0), `transferCheckedWithHook`, `EVENT_AUTHORITY_CHECK` and the reads of
186
- processed sibling instructions (0.48.0), and `zeroData`, `log64`,
186
+ processed sibling instructions (0.48.0), `zeroData`, `log64`,
187
187
  `PANIC_STYLE = "solana-1.18"`, `executable` on remaining accounts and lamport writes on remaining accounts under
188
- `NATIVE_ACCOUNT_WRITES` (0.49.0). There has been no external audit: see [audit.md](audit.md).
188
+ `NATIVE_ACCOUNT_WRITES` (0.49.0), `copyData`, `getReturnData()`, `PANIC_STYLE = "pinocchio"` and whole values
189
+ copied and zeroed by the memory syscalls (0.50.0), and the base58 text of keys from a table, derivations without
190
+ the run-time seed-limit check, object-type structs, `extends`, struct spreads, labels, logical assignment and the
191
+ stack layout the build chooses for a program that does not fit (0.51.0). There has been no external audit: see [audit.md](audit.md).
package/docs/stability.md CHANGED
@@ -68,10 +68,11 @@ arm64 has no Agave CLI, so there the package is set up by hand with `TYPESOL_SBF
68
68
  [troubleshooting.md](troubleshooting.md#linux-arm64-the-agave-installer-fails-with-a-404)): for
69
69
  SBPF v0 (the default), the 76 programs then in the sources (`scripts/release-manifest.ts`, every program under
70
70
  `examples/` and `showcase/`); for SBPF v1 and v2, the 14 programs of `test/golden/binaries.json` (counter, vault,
71
- escrow, multisig and the 10 below). Programs added since were not measured on the three hosts, and two of them that
72
- divide 128-bit and wider integers, the Meteora DBC port (0.48.0; u128 and u256 division) and the Percolator port
73
- (0.49.0; U256 and U512), are not in `test/golden/binaries.json`, so CI's cross-host check does not build them yet
74
- (adding them needs the file regenerated in the `--docker` reference environment).
71
+ escrow, multisig and the 10 below). Programs added since were not measured on the three hosts, and three of them that
72
+ divide 128-bit and wider integers, the Meteora DBC port (0.48.0; u128 and u256 division), the Percolator port
73
+ (0.49.0; U256 and U512) and the Percolator v16 port (0.50.0; u256), are not in `test/golden/binaries.json`, so CI's
74
+ cross-host check does not build them yet (adding them needs the file regenerated in the `--docker` reference
75
+ environment).
75
76
 
76
77
  | What | Same on the three hosts? |
77
78
  | --- | --- |
@@ -137,7 +138,11 @@ Useful today, but may still change before 1.0 (each change listed in the CHANGEL
137
138
  - Constant expressions (0.43.0): top-level integer constants folded from literals and earlier constants, a typed
138
139
  one computed in its type.
139
140
  - Comma-separated `for` updates and initializers (0.43.0).
140
- - Stack room (0.43.0): `/** @noinline */` helpers and `/** @heap */` locals.
141
+ - Stack room (0.43.0): `/** @noinline */` helpers and `/** @heap */` locals. A program that does not fit the stack is
142
+ laid out by the build (statements in blocks of their own, helpers kept out of line, an instruction's locals on the
143
+ heap), with `STACK_AUTO`, `--stack-auto`, `/** @inline */` and `/** @stack */` to turn it off (unreleased). Which
144
+ steps it takes, and so the bytes of a program that did not fit as written, may change between releases; a program
145
+ that fits is never laid out.
141
146
  - Local `FixedVec` / `FixedArray` lists of structs, `FixedVec.pushAll`, the instruction data as a list (`slice`,
142
147
  `InstructionBytes` helper parameters), `invokeInstruction` in helpers (0.42.0); local FixedVecs given for read-only
143
148
  `FixedVec<T, N>` / `Vec<T>` helper parameters (0.43.0).
package/docs/testing.md CHANGED
@@ -59,8 +59,12 @@ or later):
59
59
  from 0.45.0 on, of the runtime's compiler builtins);
60
60
  - the sha256 of the SBF binary, IDL and C that every program under `examples/`, `showcase/mainnet/` and
61
61
  `showcase/program-examples/` builds to (`scripts/release-manifest.ts`);
62
- - the sha256 of the reference binaries the differential tests run against (`test/programs/`);
63
- - a hash of the test corpus.
62
+ - the sha256 of the reference binaries the differential tests run against (`test/programs/`); the script refuses to
63
+ make a release manifest when `test/programs/SOURCES.md` lists a binary the tree lacks (with
64
+ `--allow-missing-references` it lists them under `referencesMissing` instead);
65
+ - a hash of the test corpus: every file under `test/` and `showcase/test/` (the directories the tests run from, the
66
+ mainnet ports' differential tests, helpers and fixtures included) but the reference binaries, with the directories
67
+ it covers (`corpus.roots`). Manifests before this change hashed `test/` alone.
64
68
 
65
69
  It also carries `typesol-<version>-binary-diff.txt` (older releases: `binary-diff.txt`): the previous release's sources built by the previous package and by this one,
66
70
  compared program by program. That is the check behind a changelog line such as "programs that use none of these compile to the
@@ -122,7 +122,8 @@ a ComputeBudget `requestHeapFrame` instruction to the transaction.
122
122
  ### "instruction X (with the helpers inlined into it) needs N bytes of stack; SBPF v0 gives each function 4096"
123
123
 
124
124
  Under SBPF v0 (the default) every function gets a fixed 4 KiB stack frame, and going past it corrupts memory, so the
125
- build refuses it. SBPF v1 and v2 (`--sbpf v1` / `v2`) grow frames dynamically, so this limit does not apply there. Large
125
+ build refuses it. SBPF v1 and v2 (`--sbpf v1` / `v2`) size each function's frame, so the 4 KiB limit is lifted there,
126
+ but they have limits of their own (the next two entries). Large
126
127
  structs and arrays copied into typed locals (`const q: Big = acct.big`) are the usual cause: bind them without a type
127
128
  (`const q = acct.big`, a reference into the account data). The message lists the biggest locals (clang's frame layout: a
128
129
  local's name and the source line near it; the locals of an inlined helper show as unnamed, with the helpers' locals that
@@ -130,6 +131,49 @@ may be them). Helpers are often inlined into their instruction, so their locals
130
131
  @noinline */` to give it a 4 KiB frame of its own (a few compute units per call), or declare a large local list or
131
132
  struct of an instruction `/** @heap */` to keep it on the heap (about 18 CU for the allocation).
132
133
 
134
+ The build tries those itself before it gives this error ([Memory](language-basics.md#memory-stack-and-heap)): when they
135
+ were not enough, the message ends with "To fit, the build also tried …", and lists the locals the heap could not hold.
136
+ What is left is usually a helper's own locals (the build keeps a helper's locals on the stack: it may run many times,
137
+ and the heap is never freed) or one local larger than the heap. With `const STACK_AUTO = false` (or `--stack-auto off`)
138
+ the build does not try, and gives the error as it is.
139
+
140
+ ### "note: instruction X did not fit the stack …"
141
+
142
+ Not an error: the program did not fit the stack as written, and the build laid it out another way (the notes after it
143
+ say how, and `<name>.build.json` records it under `stack`). Its binary differs from what the source would give if it
144
+ fitted, so a port comparing compute units with its original should look at the notes: a helper kept out of line costs
145
+ about 10 compute units per call, a local on the heap about 17 and its bytes of heap. Declaring the same yourself
146
+ (`/** @noinline */`, `/** @heap */`) or binding large values as references instead of copies gives the same or better,
147
+ and `/** @inline */` or `/** @stack */` keeps the build from choosing a helper or a local.
148
+
149
+ ### "… needs N bytes of stack; SBPF v1 allows one function at most 32767"
150
+
151
+ On SBPF v1 and v2 a load or store reaches its stack slot with a signed 16-bit offset, so a frame over 32,767 bytes
152
+ cannot be addressed: clang's SBF backend builds it anyway, with the large offsets wrapped, and the program reads and
153
+ writes the wrong memory. The build refuses such a frame. The remedies are the ones above: references instead of copies,
154
+ `/** @heap */` on a large local of an instruction, or a `/** @noinline */` helper for work with large locals of its
155
+ own.
156
+
157
+ ### "the deepest call path needs N bytes of stack; SBPF v1 gives all the frames of a call path together 262144"
158
+
159
+ On SBPF v1 and v2 every frame of a call path (the entrypoint, the instruction, each helper it calls in turn) comes out
160
+ of one 256 KiB stack, and past it the program fails with an access violation. After linking, the build measures the
161
+ deepest path from the program's own code (its calls and the frame each function reserves, so a helper clang inlined
162
+ counts in its caller) and refuses a program whose deepest path is over 262,144 bytes. The message lists the path with
163
+ each function's frame. Shorten it: keep large values in account data or on the heap, or call the helpers with large
164
+ frames one after another rather than from each other. (An indirect call, which TypeSOL does not generate, cannot be
165
+ followed: a program with one is refused with a request to report it.)
166
+
167
+ ### "… a call passes arguments on the stack past the end of the 4096-byte frame of SBPF v0"
168
+
169
+ A helper call with more than 5 arguments in C (its parameters, plus one for the program's context and one for a
170
+ returned value) passes the rest on the stack, after the caller's locals. When the caller's frame is at the limit,
171
+ those arguments overwrite its own values. clang's SBF backend reports this ("A function call in method … overwrites
172
+ values in the frame") but still builds, and such a program computed wrong results; the build now refuses it, naming the
173
+ function and the helpers it calls with arguments on the stack. Give the helper fewer parameters (a struct parameter is
174
+ passed as one pointer) or make room in the caller as above. The backend's other message, "Stack offset of N exceeded
175
+ max offset of M", is refused the same way.
176
+
133
177
  ### "… is missing, but typesol.json records …"
134
178
 
135
179
  The program keypair under `target/deploy/` was deleted or replaced. Restore it from your backup. Without it you cannot
package/lib/typesol.d.ts CHANGED
@@ -1006,12 +1006,13 @@ export declare const AuthorityType: {
1006
1006
  * Resize an account of this program (Anchor `realloc`, struct accounts only): growing charges `payer` the extra rent
1007
1007
  * (never the account's own lamports, which may be deposits); shrinking keeps every lamport in the account — move
1008
1008
  * freed rent out yourself if your program allows it. At most 10 KiB of growth per instruction (3016); never below
1009
- * the struct size. Accounts ending in a Vec size themselves through push/pop/clear.
1009
+ * the struct size (3016; for a `@layout borsh` struct its smallest Borsh form, and the value written back must fit,
1010
+ * 3004). Accounts ending in a Vec size themselves through push/pop/clear.
1010
1011
  */
1011
1012
  export declare function realloc(account: AccountMeta, newSize: u64, payer: Mut<Signer>): void;
1012
1013
  /**
1013
1014
  * Resize a `Mut<AccountInfo>` or a remaining account of this program to `newSize` bytes (Rust's `AccountInfo::resize` /
1014
- * `realloc(newSize, true)`): the data length only, the new bytes zeroed. No rent moves: fund the account first
1015
+ * `realloc(newSize, true)`): the data length only, the new bytes zeroed (64 or more by the memset syscall). No rent moves: fund the account first
1015
1016
  * (`rentExempt(newSize)`), the runtime refuses an account left short. At most 10 KiB past its size when the
1016
1017
  * instruction started (3016). A remaining account must be writable (3006) unless the program declares
1017
1018
  * `NATIVE_ACCOUNT_WRITES`, which also lets an `AccountInfo` parameter be resized without `Mut<…>`. A view of the
@@ -2491,7 +2492,9 @@ export declare enum ProgramError {
2491
2492
  * Under `export const PANIC_STYLE = "solana-1.14"` (a program built with solana-program 1.14 to 1.17) it logs
2492
2493
  * `panicked at '<message>', <location>` and aborts, so the runtime reports "SBF program panicked", as that panic
2493
2494
  * handler does; under `"solana-1.18"` (rustc 1.73 and later) it logs `panicked at <location>:` with the message on the
2494
- * next line, in one log line, and aborts. A message is then required.
2495
+ * next line, in one log line, and aborts. A message is then required. Under `"pinocchio"` (pinocchio's
2496
+ * `entrypoint!` panic hook) it logs the location's file alone, then `** PANICKED **`, and aborts: the message is not
2497
+ * logged (it may be null), and `location` may be the file alone (`"src/v16.rs"`).
2495
2498
  */
2496
2499
  export declare function panic(message?: string | null, location?: string): never;
2497
2500
  /**
@@ -2570,6 +2573,16 @@ export declare function emit<T>(event: T): void;
2570
2573
  */
2571
2574
  export declare function returnData<T extends bigint | boolean | Pubkey | Hash | object>(program: AccountMeta): T;
2572
2575
 
2576
+ /**
2577
+ * The return data as the runtime holds it, of any length and from any program (Rust's `get_return_data()`), for a
2578
+ * port that checks it itself: `const [program, data] = getReturnData()`. `program` is the program that set it and
2579
+ * `data` its bytes (up to 1,024, the runtime's maximum), read with the methods of instruction data
2580
+ * (`data.length`, `data.readU64(8n)`, `data.slice(a, b)`). Rust's `None` is `data.length === 0n` (the runtime keeps
2581
+ * no empty return data), `program` then all zeros. Nothing is checked: compare `program` and the length yourself,
2582
+ * failing with your own errors. The bytes take 1 KiB of the calling function's stack frame.
2583
+ */
2584
+ export declare function getReturnData(): [program: Pubkey, data: InstructionBytes];
2585
+
2573
2586
  /**
2574
2587
  * Set this instruction's return data to `bytes` as they are (Rust's `set_return_data(&bytes)`): a list of u8 such as a
2575
2588
  * local `FixedVec<u8, N>`, a Vec or FixedVec in account data, or the instruction data or a slice of it. For a native
@@ -2602,6 +2615,15 @@ export declare function log64(a: u64, b: u64, c: u64, d: u64, e: u64): void;
2602
2615
  */
2603
2616
  export declare function zeroData(account: RemainingAccount | Mut<AccountInfo>, offset: u64, length: u64): void;
2604
2617
 
2618
+ /**
2619
+ * Copy `length` bytes of an account's data from offset `from` to offset `to`, as a native program's
2620
+ * `data.copy_within(from..from + length, to)` (or a struct copied out of the account and written back at another
2621
+ * offset): the memmove syscall, about constant compute units whatever the length; the ranges may overlap. A remaining
2622
+ * account (or a `Mut<AccountInfo>` parameter); a range past the end of the data fails with 3003 and copies nothing.
2623
+ * Only this program's accounts can change: check the owner first.
2624
+ */
2625
+ export declare function copyData(account: RemainingAccount | Mut<AccountInfo>, from: u64, to: u64, length: u64): void;
2626
+
2605
2627
  /**
2606
2628
  * Verify `account.key` is the PDA for `seeds` + `bump` under this program. Any bump that gives a PDA passes (Anchor's
2607
2629
  * `bump = expr`): use a stored bump, not one from the arguments, or the caller picks among up to 256 addresses.