@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
@@ -218,8 +218,51 @@ TS_BUILTIN ts_i64 __moddi3(ts_i64 a, ts_i64 b) {
218
218
  * The assembly. In: r1 the result's address, r2 and r3 the first operand's low and high halves, r4 and r5 the second's
219
219
  * (__multi3) or r4 the shift amount, 0 to 127. Out: 16 bytes at r1. r6 to r9 and the stack frame at r10 are the
220
220
  * callee's, as in any SBF function.
221
+ *
222
+ * That is the convention of clang 20 (platform-tools v1.52, the tested toolchain) and earlier. clang 22 (platform-tools
223
+ * v1.57) passes a 128-bit libcall's operands from r1 and returns its result in registers, so the assembly would store
224
+ * the result through the first operand's low half (a program built with it faults with "Access violation writing 8
225
+ * bytes at address …" at its first 128-bit multiplication). TS_ASM_LIBCALLS selects the assembly only for a clang whose
226
+ * convention it was written for; any other clang gets the C definitions after it, which take the convention clang
227
+ * gives their C signatures, the one its libcalls use.
221
228
  */
222
- #ifdef TS_SBPF_V0
229
+ #if defined(__clang_major__) && __clang_major__ <= 20
230
+ #define TS_ASM_LIBCALLS 1
231
+ #endif
232
+
233
+ #ifndef TS_ASM_LIBCALLS
234
+ /* The low 128 bits of a * b. */
235
+ TS_BUILTIN ts_u128 __multi3(ts_u128 a, ts_u128 b) {
236
+ ts_u64 al = TS_LO(a), bl = TS_LO(b), hi;
237
+ ts_u64 lo = ts_mul_wide(al, bl, &hi);
238
+ return TS_MK(hi + al * TS_HI(b) + TS_HI(a) * bl, lo);
239
+ }
240
+
241
+ /* a << (n & 127), from 64-bit halves (only constant 128-bit shifts, which are register moves). */
242
+ TS_BUILTIN ts_u128 __ashlti3(ts_u128 a, int b) {
243
+ ts_u64 n = (ts_u64)b & 127, lo = TS_LO(a), hi = TS_HI(a);
244
+ if (n & 64) return TS_MK(lo << (n & 63), 0);
245
+ if (n == 0) return a;
246
+ return TS_MK((hi << n) | (lo >> (64 - n)), lo << n);
247
+ }
248
+
249
+ /* a >> (n & 127), logical. */
250
+ TS_BUILTIN ts_u128 __lshrti3(ts_u128 a, int b) {
251
+ ts_u64 n = (ts_u64)b & 127, lo = TS_LO(a), hi = TS_HI(a);
252
+ if (n & 64) return TS_MK(0, hi >> (n & 63));
253
+ if (n == 0) return a;
254
+ return TS_MK(hi >> n, (lo >> n) | (hi << (64 - n)));
255
+ }
256
+
257
+ /* a >> (n & 127), arithmetic. */
258
+ TS_BUILTIN ts_i128 __ashrti3(ts_i128 a, int b) {
259
+ ts_u64 n = (ts_u64)b & 127, lo = TS_LO(a), hi = TS_HI(a);
260
+ ts_u64 sign = (ts_u64)((ts_i64)hi >> 63);
261
+ if (n & 64) return (ts_i128)TS_MK(sign, (ts_u64)((ts_i64)hi >> (n & 63)));
262
+ if (n == 0) return a;
263
+ return (ts_i128)TS_MK((ts_u64)((ts_i64)hi >> n), (lo >> n) | (hi << (64 - n)));
264
+ }
265
+ #elif defined(TS_SBPF_V0)
223
266
  __asm__(
224
267
  ".section .text.__multi3,\"ax\",@progbits\n"
225
268
  ".hidden __multi3\n"
@@ -284,7 +327,9 @@ TS_BUILTIN ts_u128 __multi3(ts_u128 a, ts_u128 b) {
284
327
  }
285
328
  #endif
286
329
 
287
- #if defined(TS_SBPF_V0) || defined(TS_SBPF_V1)
330
+ #ifndef TS_ASM_LIBCALLS
331
+ /* The C definitions above. */
332
+ #elif defined(TS_SBPF_V0) || defined(TS_SBPF_V1)
288
333
  /* SBPF v0 and v1 (compiler-builtins has the same instructions for both). */
289
334
  __asm__(
290
335
  ".section .text.__ashlti3,\"ax\",@progbits\n"
@@ -176,6 +176,70 @@ static inline __attribute__((always_inline)) void ts_memzero(void *dst, uint64_t
176
176
  for (; n; n--) *d++ = 0;
177
177
  }
178
178
 
179
+ /*
180
+ * ---- Whole values and account data by the memory syscalls -----------------
181
+ * sol_memmove_ and sol_memset_ cost max(10, n / 250) compute units, plus the call (its arguments and the call itself):
182
+ * 16 CU for a copy and 14 for a zero-fill of up to 2,749 bytes, measured in LiteSVM (one more CU per 250 bytes past
183
+ * that). The word loops above cost, measured in the same programs:
184
+ *
185
+ * bytes 16 64 80 128 192 256 392 400 424 512 576 600 726 1024 2048 4096
186
+ * copy, size known 6 22 26 38 54 70 104 405 429 515 581 605 560 1027 2051 4099
187
+ * zero, size known 3 9 11 17 25 33 50 53 56 65 75 378 366 642 1282 2562
188
+ * zero, run-time length 17 41 49 73 105 137 - - 221 265 - - 394 521 1033 2057
189
+ *
190
+ * (a length 1 to 7 bytes past a multiple of 8 adds up to 18 CU for the byte loop; the run-time row includes the
191
+ * resize around it). Up to 392 bytes clang unrolls a copy of a known size into a load and a store per 8 bytes, and up
192
+ * to 576 a zero-fill into a store per 8 bytes; past that it emits a loop (8 and 5 CU per 8 bytes).
193
+ *
194
+ * - Between places reached through pointers (account data, views, a helper's pointer parameters), which the optimizer
195
+ * must copy whole: TS_COPY from 80 bytes and TS_ZERO from 192, where the loop costs at least 10 CU more than the
196
+ * call (a call that needs a live value saved around it still costs less).
197
+ * - Where a side is a local of the function (a struct literal built in a temporary, a helper's result, a local copy):
198
+ * TS_COPY_LOCAL / TS_MEMCPY from 400 bytes and TS_ZERO_LOCAL from 600, where clang emits a loop. Below that it
199
+ * unrolls them and drops the stores and loads nothing reads (the literal built in place, the fields nobody reads),
200
+ * which a call, opaque to it, would prevent: with 80 and 192 there, SPL Stake Pool's UpdateValidatorListBalance cost
201
+ * up to 2,825 CU more and Phoenix's instructions 7 to 98 more (measured).
202
+ *
203
+ * The sizes are known when compiling (a sizeof, a constant): the condition folds, so a program none of whose copies
204
+ * reaches a threshold neither calls nor imports the syscalls, and its code is exactly what it was. TS_COPY and
205
+ * TS_COPY_LOCAL are copies between two places of one type (TS_COPY's may overlap, through native views of one account
206
+ * at different offsets: below the threshold ts_copy_value loads every byte before storing any; TS_COPY_LOCAL's cannot,
207
+ * one side being a local: ts_copy), TS_MEMCPY any other whole-value copy. All call memmove, never
208
+ * memcpy (which fails the transaction on overlapping ranges): it costs the same.
209
+ *
210
+ * ts_memzero_run (the bytes an account grows by) and TS_CLOSE (an account's data when it is closed) take a run-time
211
+ * length, always a loop before. ts_memzero_run tests the length at run time: one CU more than before below
212
+ * TS_ZERO_RUN_MIN. TS_CLOSE decides when compiling, from the smallest size the account's type allows.
213
+ */
214
+ extern void sol_memmove_(void *dst, const void *src, uint64_t n);
215
+ extern void sol_memset_(void *dst, uint8_t c, uint64_t n);
216
+ #ifndef TS_COPY_SYSCALL_MIN
217
+ #define TS_COPY_SYSCALL_MIN 80
218
+ #endif
219
+ #ifndef TS_ZERO_SYSCALL_MIN
220
+ #define TS_ZERO_SYSCALL_MIN 192
221
+ #endif
222
+ #ifndef TS_LOCAL_COPY_SYSCALL_MIN
223
+ #define TS_LOCAL_COPY_SYSCALL_MIN 400
224
+ #endif
225
+ #ifndef TS_LOCAL_ZERO_SYSCALL_MIN
226
+ #define TS_LOCAL_ZERO_SYSCALL_MIN 600
227
+ #endif
228
+ #ifndef TS_ZERO_RUN_MIN
229
+ #define TS_ZERO_RUN_MIN 64
230
+ #endif
231
+ #define TS_COPY(dst, src, n) ((n) >= TS_COPY_SYSCALL_MIN ? sol_memmove_((dst), (src), (n)) : ts_copy_value((dst), (src), (n)))
232
+ #define TS_COPY_LOCAL(dst, src, n) \
233
+ ((n) >= TS_LOCAL_COPY_SYSCALL_MIN ? sol_memmove_((dst), (src), (n)) : ts_copy((dst), (src), (n)))
234
+ #define TS_MEMCPY(dst, src, n) \
235
+ ((n) >= TS_LOCAL_COPY_SYSCALL_MIN ? sol_memmove_((dst), (src), (n)) : ts_memcpy((dst), (src), (n)))
236
+ #define TS_ZERO(dst, n) ((n) >= TS_ZERO_SYSCALL_MIN ? sol_memset_((dst), 0, (n)) : ts_memzero((dst), (n)))
237
+ #define TS_ZERO_LOCAL(dst, n) ((n) >= TS_LOCAL_ZERO_SYSCALL_MIN ? sol_memset_((dst), 0, (n)) : ts_memzero((dst), (n)))
238
+ static inline __attribute__((always_inline)) void ts_memzero_run(void *dst, uint64_t n) {
239
+ if (n >= TS_ZERO_RUN_MIN) sol_memset_(dst, 0, n);
240
+ else ts_memzero(dst, n);
241
+ }
242
+
179
243
  /*
180
244
  * Checked u64 addition and subtraction. Same results as __builtin_add/sub_overflow, but written as one compare
181
245
  * on the operands, which clang lowers to a single branch on SBF instead of materializing the carry flag.
@@ -684,7 +748,9 @@ static inline __attribute__((always_inline)) uint64_t ts_invoke(TsParams *params
684
748
  * The same addresses sol_create_program_address / sol_try_find_program_address compute — sha256 of the seeds, the
685
749
  * program id and "ProgramDerivedAddress", valid only off the ed25519 curve — from the sha256 and curve-validation
686
750
  * syscalls, which cost a fraction of the 1500 compute units the PDA syscalls charge per attempt. The seed limits are the
687
- * syscalls' (at most 16 seeds of at most 32 bytes, the bump included), and so is the result for every input.
751
+ * syscalls' (at most 16 seeds of at most 32 bytes, the bump included), and so is the result for every input. The
752
+ * `_fit` forms leave the limits out: the compiler calls them for seed lists it proved within them (every seed of a
753
+ * length known at compile time, and few enough seeds), where the check cannot fail.
688
754
  */
689
755
  uint64_t sol_curve_validate_point(uint64_t curve_id, const uint8_t *point, uint8_t *result);
690
756
  #define TS_MAX_PDA_SEEDS 16
@@ -714,6 +780,10 @@ static inline uint64_t ts_create_pda_address(const SolSignerSeed *seeds, int n,
714
780
  if (!ts_pda_seeds_ok(seeds, n, TS_MAX_PDA_SEEDS) || !ts_pda_hash(seeds, n, NULL, program, out)) return 1; /* not SUCCESS: the PDA syscalls fail the same inputs */
715
781
  return SUCCESS;
716
782
  }
783
+ static inline uint64_t ts_create_pda_address_fit(const SolSignerSeed *seeds, int n, const SolPubkey *program, SolPubkey *out) {
784
+ if (!ts_pda_hash(seeds, n, NULL, program, out)) return 1; /* not SUCCESS: on the curve, no PDA */
785
+ return SUCCESS;
786
+ }
717
787
 
718
788
  /* msg!("{prefix}{base64(bytes)}") with standard padding, as Raydium's ray_log writes it (at most 300 bytes in). */
719
789
  static __attribute__((noinline, unused)) void ts_log_base64(const char *prefix, uint64_t plen, const uint8_t *data, uint64_t len) {
@@ -737,9 +807,12 @@ static __attribute__((noinline, unused)) void ts_log_base64(const char *prefix,
737
807
  sol_log_(out, o);
738
808
  }
739
809
 
740
- /* sol_try_find_program_address: the canonical (highest) bump. */
741
- static inline uint64_t ts_find_pda(const SolSignerSeed *seeds, int n, const SolPubkey *program, SolPubkey *out, uint8_t *bump_out) {
742
- if (!ts_pda_seeds_ok(seeds, n, TS_MAX_PDA_SEEDS - 1)) return 1; /* not SUCCESS: the PDA syscalls fail the same inputs */
810
+ /*
811
+ * sol_try_find_program_address: the canonical (highest) bump. As the syscall (and Pubkey::find_program_address), it
812
+ * tries the bumps 255 down to 1: bump 0 is never tried, and seeds whose 255 attempts all land on the curve have no
813
+ * address.
814
+ */
815
+ static inline uint64_t ts_find_pda_fit(const SolSignerSeed *seeds, int n, const SolPubkey *program, SolPubkey *out, uint8_t *bump_out) {
743
816
  /* The seed list is built once; each attempt only changes the bump byte it points at. */
744
817
  SolBytes parts[TS_MAX_PDA_SEEDS + 2];
745
818
  int k = 0;
@@ -748,7 +821,7 @@ static inline uint64_t ts_find_pda(const SolSignerSeed *seeds, int n, const SolP
748
821
  parts[k++] = (SolBytes){&bump, 1};
749
822
  parts[k++] = (SolBytes){program->x, 32};
750
823
  parts[k++] = (SolBytes){(const uint8_t *)"ProgramDerivedAddress", 21};
751
- for (int b = 255; b >= 0; b--) {
824
+ for (int b = 255; b >= 1; b--) {
752
825
  bump = (uint8_t)b;
753
826
  sol_sha256(parts, k, out->x);
754
827
  uint8_t ignored;
@@ -759,13 +832,62 @@ static inline uint64_t ts_find_pda(const SolSignerSeed *seeds, int n, const SolP
759
832
  }
760
833
  return 1; /* not SUCCESS: the PDA syscalls fail the same inputs */
761
834
  }
835
+ static inline uint64_t ts_find_pda(const SolSignerSeed *seeds, int n, const SolPubkey *program, SolPubkey *out, uint8_t *bump_out) {
836
+ if (!ts_pda_seeds_ok(seeds, n, TS_MAX_PDA_SEEDS - 1)) return 1; /* not SUCCESS: the PDA syscalls fail the same inputs */
837
+ return ts_find_pda_fit(seeds, n, program, out, bump_out);
838
+ }
839
+
840
+ /*
841
+ * The account-creating helpers below (ts_create_pda_owned, ts_create_pda, ts_create_pda_if_needed, ts_create_owned,
842
+ * ts_token_existing) take seed lists from the program. Where the compiler proved every list a program passes them within the limits, it
843
+ * defines TS_PDA_SEEDS_FIT and they leave the check out; a program with one list it could not prove keeps the check in
844
+ * all of them, so that they stay one copy each.
845
+ */
846
+ #ifdef TS_PDA_SEEDS_FIT
847
+ #define ts_find_pda_given ts_find_pda_fit
848
+ #else
849
+ #define ts_find_pda_given ts_find_pda
850
+ #endif
762
851
 
763
852
  /* ---- Account checks ------------------------------------------------------ */
764
- /* Copy between two places of the same type: distinct, or the very same place (then nothing to do). */
853
+ /* Copy between two places of the same type that cannot overlap (TS_COPY_LOCAL: one is a local of the function):
854
+ distinct, or the very same place (then nothing to do). */
765
855
  static inline void ts_copy(void *dst, const void *src, uint64_t n) {
766
856
  if (dst != src) ts_memcpy(dst, src, n);
767
857
  }
768
858
 
859
+ /*
860
+ * TS_COPY below TS_COPY_SYSCALL_MIN: a whole value between two places of one type reached through pointers, which may
861
+ * overlap (views of one account at run-time offsets, an account passed twice under NATIVE_ACCOUNT_WRITES). Every byte
862
+ * is loaded before any is stored, so the destination ends with the source's old bytes in either direction, as the
863
+ * memmove syscall leaves them from the threshold on. The size is known when compiling: the loops unroll into a load
864
+ * per 8 bytes, then a store per 8 bytes; a length past a multiple of 8 ends with one 8-byte load and store of the last
865
+ * 8 bytes (they overlap the previous word), 4 to 7 bytes are two 4-byte loads and stores, 1 to 3 bytes byte by byte.
866
+ * Measured against the forward word loop it replaces (ts_copy, which re-read bytes it had already written when the
867
+ * destination was ahead of an overlapping source): 1 to 2 CU less for 16 and 40 bytes, the same for 72, 10 less for
868
+ * 6 and 79 bytes (test/language/bulk-copies.test.ts); more registers are live between the loads and the stores.
869
+ */
870
+ static inline __attribute__((always_inline)) void ts_copy_value(void *dst, const void *src, uint64_t n) {
871
+ uint8_t *d = (uint8_t *)dst;
872
+ const uint8_t *s = (const uint8_t *)src;
873
+ if (n >= 8) {
874
+ uint64_t w[TS_COPY_SYSCALL_MIN / 8];
875
+ uint64_t k = n / 8 < TS_COPY_SYSCALL_MIN / 8 ? n / 8 : TS_COPY_SYSCALL_MIN / 8;
876
+ for (uint64_t i = 0; i < k; i++) w[i] = ts_ld64(s + 8 * i);
877
+ uint64_t t = ts_ld64(s + n - 8);
878
+ for (uint64_t i = 0; i < k; i++) ts_st64(d + 8 * i, w[i]);
879
+ if (n % 8) ts_st64(d + n - 8, t);
880
+ } else if (n >= 4) {
881
+ uint32_t a = *(const ts_u32_unaligned *)s, b = *(const ts_u32_unaligned *)(s + n - 4);
882
+ *(ts_u32_unaligned *)d = a;
883
+ *(ts_u32_unaligned *)(d + n - 4) = b;
884
+ } else {
885
+ uint8_t b[3];
886
+ for (uint64_t i = 0; i < n && i < 3; i++) b[i] = s[i];
887
+ for (uint64_t i = 0; i < n && i < 3; i++) d[i] = b[i];
888
+ }
889
+ }
890
+
769
891
  static inline __attribute__((always_inline)) bool ts_key_eq(const SolPubkey *a, const SolPubkey *b);
770
892
  #ifdef TS_OWNER_CACHE
771
893
  /* Is account `a` this program's? The views of native accounts (loadRaw, loadRawAt, rawArrayAt, rbTreeAt) check it each
@@ -1107,7 +1229,7 @@ static inline uint64_t ts_check_program_data(const TsAccount *a, const SolPubkey
1107
1229
  SolSignerSeed seed = {(const uint8_t *)program_id, 32};
1108
1230
  SolPubkey expected;
1109
1231
  uint8_t bump;
1110
- if (ts_find_pda(&seed, 1, &TS_BPF_UPGRADEABLE_LOADER_ID, &expected, &bump) != SUCCESS || !ts_key_eq(&expected, &a->key))
1232
+ if (ts_find_pda_fit(&seed, 1, &TS_BPF_UPGRADEABLE_LOADER_ID, &expected, &bump) != SUCCESS || !ts_key_eq(&expected, &a->key))
1111
1233
  TS_FAIL(TS_ERR_ADDRESS, "TypeSOL: not this program's ProgramData account");
1112
1234
  return SUCCESS;
1113
1235
  }
@@ -1117,7 +1239,7 @@ static const uint8_t TS_EVENT_IX_TAG[8] = {0xe4, 0x45, 0xa5, 0x2e, 0x51, 0xcb, 0
1117
1239
  static inline uint64_t ts_check_event_authority(const TsAccount *a, const SolPubkey *program_id, uint8_t *bump) {
1118
1240
  SolSignerSeed seed = {(const uint8_t *)"__event_authority", 17};
1119
1241
  SolPubkey expected;
1120
- if (ts_find_pda(&seed, 1, program_id, &expected, bump) != SUCCESS || !ts_key_eq(&expected, &a->key))
1242
+ if (ts_find_pda_fit(&seed, 1, program_id, &expected, bump) != SUCCESS || !ts_key_eq(&expected, &a->key))
1121
1243
  TS_FAIL(TS_ERR_SEEDS, "TypeSOL: not this program's event authority");
1122
1244
  return SUCCESS;
1123
1245
  }
@@ -1191,6 +1313,12 @@ static inline uint64_t ts_require_pda(const TsAccount *a, const SolSignerSeed *s
1191
1313
  TS_FAIL(TS_ERR_SEEDS, "TypeSOL: PDA seeds do not match account");
1192
1314
  return SUCCESS;
1193
1315
  }
1316
+ static inline uint64_t ts_require_pda_fit(const TsAccount *a, const SolSignerSeed *seeds, int n, const SolPubkey *program_id) {
1317
+ SolPubkey derived;
1318
+ if (ts_create_pda_address_fit(seeds, n, program_id, &derived) != SUCCESS || !ts_key_eq(&derived, &a->key))
1319
+ TS_FAIL(TS_ERR_SEEDS, "TypeSOL: PDA seeds do not match account");
1320
+ return SUCCESS;
1321
+ }
1194
1322
 
1195
1323
  /* ---- System program CPIs ------------------------------------------------- */
1196
1324
  static inline uint64_t ts_system_transfer_signed(TsParams *params, TsAccount *from, TsAccount *to,
@@ -1220,7 +1348,7 @@ static inline uint64_t ts_create_pda_owned(TsParams *params, TsAccount *payer, T
1220
1348
  uint8_t *bump_out) {
1221
1349
  SolPubkey derived;
1222
1350
  uint8_t bump;
1223
- if (ts_find_pda(seeds, n, params->program_id, &derived, &bump) != SUCCESS ||
1351
+ if (ts_find_pda_given(seeds, n, params->program_id, &derived, &bump) != SUCCESS ||
1224
1352
  !ts_key_eq(&derived, &acct->key))
1225
1353
  TS_FAIL(TS_ERR_SEEDS, "TypeSOL: PDA seeds do not match account");
1226
1354
  /* No check that the address is free: as Anchor's init, the System Program refuses one in use (AccountAlreadyInUse,
@@ -1284,7 +1412,7 @@ static inline uint64_t ts_create_owned(TsParams *params, TsAccount *payer, TsAcc
1284
1412
  uint8_t bump = 0;
1285
1413
  if (n > 0) {
1286
1414
  SolPubkey derived;
1287
- if (ts_find_pda(seeds, n, params->program_id, &derived, &bump) != SUCCESS || !ts_key_eq(&derived, &acct->key))
1415
+ if (ts_find_pda_given(seeds, n, params->program_id, &derived, &bump) != SUCCESS || !ts_key_eq(&derived, &acct->key))
1288
1416
  TS_FAIL(TS_ERR_SEEDS, "TypeSOL: PDA seeds do not match account");
1289
1417
  seeds[n].addr = &bump;
1290
1418
  seeds[n].len = 1;
@@ -1378,7 +1506,7 @@ static inline uint64_t ts_token_existing(TsParams *params, TsAccount *acct, TsAc
1378
1506
  *bump_out = 0;
1379
1507
  if (n > 0) {
1380
1508
  SolPubkey derived;
1381
- if (ts_find_pda(seeds, n, params->program_id, &derived, bump_out) != SUCCESS || !ts_key_eq(&derived, &acct->key))
1509
+ if (ts_find_pda_given(seeds, n, params->program_id, &derived, bump_out) != SUCCESS || !ts_key_eq(&derived, &acct->key))
1382
1510
  TS_FAIL(TS_ERR_SEEDS, "TypeSOL: PDA seeds do not match account");
1383
1511
  }
1384
1512
  if (!ts_key_eq(&acct->owner, &tp->key)) TS_FAIL(TS_ERR_WRONG_OWNER, "TypeSOL: account not owned by the token program");
@@ -1419,7 +1547,7 @@ static inline uint64_t ts_resize(TsParams *params, TsAccount *payer, TsAccount *
1419
1547
  if (payer == NULL) TS_FAIL(TS_ERR_REALLOC, "TypeSOL: growing an account needs a payer");
1420
1548
  TS_TRY(ts_system_transfer(params, payer, acct, new_rent - old_rent));
1421
1549
  }
1422
- ts_memzero(acct->data + old_len, new_len - old_len);
1550
+ ts_memzero_run(acct->data + old_len, new_len - old_len);
1423
1551
  }
1424
1552
  acct->data_len = new_len;
1425
1553
  return SUCCESS;
@@ -1434,7 +1562,7 @@ static inline __attribute__((always_inline)) uint64_t ts_entry_len(const TsAccou
1434
1562
  static inline uint64_t ts_resize_raw(TsAccount *acct, uint64_t new_len, uint64_t entry_len) {
1435
1563
  if (new_len > entry_len + MAX_PERMITTED_DATA_INCREASE)
1436
1564
  TS_FAIL(TS_ERR_REALLOC, "TypeSOL: account would grow more than 10 KiB in one instruction");
1437
- if (new_len > acct->data_len) ts_memzero(acct->data + acct->data_len, new_len - acct->data_len);
1565
+ if (new_len > acct->data_len) ts_memzero_run(acct->data + acct->data_len, new_len - acct->data_len);
1438
1566
  acct->data_len = new_len;
1439
1567
  return SUCCESS;
1440
1568
  }
@@ -1460,7 +1588,7 @@ static inline uint64_t ts_create_pda_if_needed(TsParams *params, TsAccount *paye
1460
1588
  if (ts_key_eq(&acct->owner, params->program_id)) {
1461
1589
  SolPubkey derived;
1462
1590
  uint8_t bump;
1463
- if (ts_find_pda(seeds, n, params->program_id, &derived, &bump) != SUCCESS ||
1591
+ if (ts_find_pda_given(seeds, n, params->program_id, &derived, &bump) != SUCCESS ||
1464
1592
  !ts_key_eq(&derived, &acct->key))
1465
1593
  TS_FAIL(TS_ERR_SEEDS, "TypeSOL: PDA seeds do not match account");
1466
1594
  if (acct->data_len < min_space) TS_FAIL(TS_ERR_ACCOUNT_TOO_SMALL, "TypeSOL: account data too small");
@@ -1485,6 +1613,20 @@ static inline uint64_t ts_close(TsAccount *acct, TsAccount *dest) {
1485
1613
  ts_memcpy(&acct->owner, &TS_SYSTEM_PROGRAM_ID, 32);
1486
1614
  return SUCCESS;
1487
1615
  }
1616
+ /* ts_close for an account known to hold at least TS_ZERO_RUN_MIN bytes (its type's smallest size; TS_CLOSE decides
1617
+ when compiling): the data zeroed by the memset syscall. */
1618
+ static inline uint64_t ts_close_big(TsAccount *acct, TsAccount *dest) {
1619
+ uint64_t total;
1620
+ if (ts_add_overflow_u64(dest->lamports, acct->lamports, &total))
1621
+ TS_FAIL(TS_ERR_OVERFLOW, "TypeSOL: arithmetic overflow");
1622
+ dest->lamports = total;
1623
+ acct->lamports = 0;
1624
+ sol_memset_(acct->data, 0, acct->data_len);
1625
+ acct->data_len = 0;
1626
+ ts_memcpy(&acct->owner, &TS_SYSTEM_PROGRAM_ID, 32);
1627
+ return SUCCESS;
1628
+ }
1629
+ #define TS_CLOSE(acct, dest, min) ((min) >= TS_ZERO_RUN_MIN ? ts_close_big((acct), (dest)) : ts_close((acct), (dest)))
1488
1630
 
1489
1631
  /* A CPI to `program` (an account passed in), signed by one PDA when `nseeds` > 0. */
1490
1632
  static inline uint64_t ts_cpi(TsParams *params, const TsAccount *program, uint8_t *data, uint64_t len,
@@ -1833,6 +1975,16 @@ extern uint64_t sol_get_stack_height(void);
1833
1975
  /* The runtime's `abort` syscall (Rust's abort on SBF, e.g. the allocator's out-of-memory error): ProgramFailedToComplete,
1834
1976
  * logged as "SBF program panicked", with no location. */
1835
1977
  extern __attribute__((noreturn)) void abort(void);
1978
+ #ifdef TS_PANIC_PINOCCHIO
1979
+ /* PANIC_STYLE "pinocchio": a Rust panic as pinocchio's `default_panic_handler!` hook reports it under std: the file of
1980
+ * the panic's location verbatim (`location.file()`, no line or column, no message), then "** PANICKED **", then std's
1981
+ * abort ("SBF program panicked"). One out-of-line copy: every panic site is a call with its file. */
1982
+ static __attribute__((noinline, noreturn, cold, unused)) void ts_panic_file(const char *file, uint64_t len) {
1983
+ sol_log_(file, len);
1984
+ sol_log_("** PANICKED **", 14);
1985
+ abort();
1986
+ }
1987
+ #endif
1836
1988
 
1837
1989
  /*
1838
1990
  * ---- BigVec (SPL's big_vec) ------------------------------------------------
@@ -1843,6 +1995,11 @@ extern __attribute__((noreturn)) void abort(void);
1843
1995
  * thousands). Overlapping ranges are allowed. The "no SDK byte loops" rule is about the SDK's C loops, not this.
1844
1996
  */
1845
1997
  extern void sol_memmove_(void *dst, const void *src, uint64_t n);
1998
+ /* copyData: the memmove syscall from TS_MEMCPY_SYSCALL_MIN bytes (the ranges may overlap), the word loop below it. */
1999
+ static inline __attribute__((always_inline, unused)) void ts_memmove_bulk(void *dst, const void *src, uint64_t n) {
2000
+ if (n >= 32) sol_memmove_(dst, src, n);
2001
+ else ts_memmove(dst, src, n);
2002
+ }
1846
2003
  /* The memcpy syscall, for bulk copies of a length known only at run time (FixedVec.pushAll): max(10, n / 250) CU plus
1847
2004
  * the call, against about 0.74 CU a byte for ts_memcpy's word loop over unaligned data (measured: 1000 bytes of
1848
2005
  * instruction data), so copies under 32 bytes keep the loop. The ranges never overlap (a FixedVec's free slots and
@@ -2339,46 +2496,89 @@ static uint64_t ts_metas_scratch(TsParams *params, SolAccountMeta **out) {
2339
2496
  }
2340
2497
  #endif
2341
2498
 
2342
- /* Base58 text of a 32-byte value (a Pubkey or Hash in a template), on the heap. The value is eight big-endian u32
2343
- limbs divided by 58^5 at a time, so each pass gives five digits with one 64-bit division per limb. */
2499
+ /* The base-58^5 limbs of a value below 2^96 (the last 12 bytes of `in`; the first 20 are zero), by long division:
2500
+ cheaper there than the products of ts_base58_32. Five limbs hold it (58^25 > 2^96); the four above are zero. */
2501
+ static __attribute__((noinline)) void ts_base58_low(const uint8_t *in, uint64_t r[9]) {
2502
+ const uint64_t R = 656356768ULL; /* 58^5 */
2503
+ uint64_t a = __builtin_bswap32(*(const ts_u32_unaligned *)(in + 20));
2504
+ uint64_t b = __builtin_bswap32(*(const ts_u32_unaligned *)(in + 24));
2505
+ uint64_t c = __builtin_bswap32(*(const ts_u32_unaligned *)(in + 28));
2506
+ for (int j = 0; j < 4; j++) r[j] = 0;
2507
+ for (int j = 8; j >= 4; j--) {
2508
+ uint64_t q = a / R, rem = a - q * R;
2509
+ a = q;
2510
+ uint64_t cur = (rem << 32) | b;
2511
+ q = cur / R;
2512
+ rem = cur - q * R;
2513
+ b = q;
2514
+ cur = (rem << 32) | c;
2515
+ q = cur / R;
2516
+ r[j] = cur - q * R;
2517
+ c = q;
2518
+ }
2519
+ }
2520
+
2521
+ /*
2522
+ * Base58 text of a 32-byte value (a Pubkey or Hash in a template), on the heap. The value is eight big-endian u32 limbs
2523
+ * (limb i weighs 2^(32*(7-i))), turned into nine limbs of base 58^5 that give five digits each, at once: row i of T is
2524
+ * 2^(32*(7-i)) written in base 58^5, most significant limb first, so limb j of the result is the sum over i of
2525
+ * limb[i] * T[i][j] plus the carries from the limbs after it (Firedancer's fd_base58 does the same). A value below
2526
+ * 2^96 takes a long division instead (ts_base58_low). Both give the same text for every value
2527
+ * (test/language/base58.test.ts).
2528
+ */
2344
2529
  static uint64_t ts_base58_32(const uint8_t *in, TS_Str *out) {
2345
2530
  static const char digits[] = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
2346
- const uint64_t D = 656356768ULL; /* 58^5 */
2347
- uint64_t limb[8];
2348
- for (int i = 0; i < 8; i++) limb[i] = __builtin_bswap32(*(const ts_u32_unaligned *)(in + 4 * i)); /* big-endian words */
2349
- /* Digits are written as characters from the end of `buf`, five per pass; 32 bytes need at most 44. */
2350
- char buf[50];
2531
+ const uint64_t R = 656356768ULL; /* 58^5 */
2532
+ uint64_t r[9];
2533
+ if ((*(const ts_u64_unaligned *)in | *(const ts_u64_unaligned *)(in + 8) | *(const ts_u32_unaligned *)(in + 16)) == 0) {
2534
+ if ((*(const ts_u64_unaligned *)(in + 20) | *(const ts_u32_unaligned *)(in + 28)) == 0) {
2535
+ /* Zero (the System Program's address): a '1' for each byte. */
2536
+ uint8_t *p;
2537
+ TS_TRY(ts_alloc(32, &p));
2538
+ for (int i = 0; i < 4; i++) *(ts_u64_unaligned *)(p + 8 * i) = 0x3131313131313131ULL; /* "11111111" */
2539
+ *out = ts_str(p, 32);
2540
+ return SUCCESS;
2541
+ }
2542
+ ts_base58_low(in, r);
2543
+ } else {
2544
+ static const uint32_t T[8][9] = {
2545
+ {0u, 513735u, 77223048u, 437087610u, 300156666u, 605448490u, 214625350u, 141436834u, 379377856u},
2546
+ {0u, 0u, 78508u, 646269101u, 118408823u, 91512303u, 209184527u, 413102373u, 153715680u},
2547
+ {0u, 0u, 0u, 11997u, 486083817u, 3737691u, 294005210u, 247894721u, 289024608u},
2548
+ {0u, 0u, 0u, 0u, 1833u, 324463681u, 385795061u, 551597588u, 21339008u},
2549
+ {0u, 0u, 0u, 0u, 0u, 280u, 127692781u, 389432875u, 357132832u},
2550
+ {0u, 0u, 0u, 0u, 0u, 0u, 42u, 537767569u, 410450016u},
2551
+ {0u, 0u, 0u, 0u, 0u, 0u, 0u, 6u, 356826688u},
2552
+ {0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 1u},
2553
+ };
2554
+ uint64_t limb[8];
2555
+ for (int i = 0; i < 8; i++) limb[i] = __builtin_bswap32(*(const ts_u32_unaligned *)(in + 4 * i)); /* big-endian words */
2556
+ for (int j = 0; j < 9; j++) {
2557
+ /* Each product is below 2^32 * 58^5 < 2^62, so four of them fit in 64 bits: two sums of four. */
2558
+ uint64_t a = 0, b = 0;
2559
+ for (int i = 0; i < 4; i++) a += limb[i] * (uint64_t)T[i][j];
2560
+ for (int i = 4; i < 8; i++) b += limb[i] * (uint64_t)T[i][j];
2561
+ r[j] = a % R + b % R;
2562
+ if (j > 0) r[j - 1] += a / R + b / R; /* column 0 of T is 0: r[0] gets only carries */
2563
+ }
2564
+ for (int j = 8; j > 0; j--) {
2565
+ r[j - 1] += r[j] / R;
2566
+ r[j] %= R;
2567
+ }
2568
+ }
2569
+ /* r[0] < 58^5 now, as the value is below 2^256 < 58^45. Five digits per limb from the first nonzero one (the limbs
2570
+ above it give none). */
2571
+ int top = 0;
2572
+ while (top < 8 && r[top] == 0) top++;
2573
+ /* Digits are written from the end of `buf`; 32 bytes need at most 44. */
2574
+ char buf[45];
2351
2575
  uint64_t pos = sizeof(buf);
2352
- int start = 0;
2353
- while (start < 8 && limb[start] == 0) start++;
2354
- while (start < 8) {
2355
- uint64_t rem = 0;
2356
- /* One long-division pass from the first nonzero limb, unrolled. */
2357
- #define TS_B58_STEP(i) \
2358
- { \
2359
- uint64_t cur = (rem << 32) | limb[i]; \
2360
- uint64_t q = cur / D; \
2361
- rem = cur - q * D; \
2362
- limb[i] = q; \
2363
- }
2364
- switch (start) {
2365
- case 0: TS_B58_STEP(0) /* fallthrough */
2366
- case 1: TS_B58_STEP(1) /* fallthrough */
2367
- case 2: TS_B58_STEP(2) /* fallthrough */
2368
- case 3: TS_B58_STEP(3) /* fallthrough */
2369
- case 4: TS_B58_STEP(4) /* fallthrough */
2370
- case 5: TS_B58_STEP(5) /* fallthrough */
2371
- case 6: TS_B58_STEP(6) /* fallthrough */
2372
- default: TS_B58_STEP(7)
2576
+ for (int j = 8; j >= top; j--) {
2577
+ uint64_t v = r[j];
2578
+ for (int k = 0; k < 5; k++) {
2579
+ buf[--pos] = digits[v % 58];
2580
+ v /= 58;
2373
2581
  }
2374
- #undef TS_B58_STEP
2375
- while (start < 8 && limb[start] == 0) start++;
2376
- uint64_t q;
2377
- q = rem / 58; buf[--pos] = digits[rem - q * 58]; rem = q;
2378
- q = rem / 58; buf[--pos] = digits[rem - q * 58]; rem = q;
2379
- q = rem / 58; buf[--pos] = digits[rem - q * 58]; rem = q;
2380
- q = rem / 58; buf[--pos] = digits[rem - q * 58]; rem = q;
2381
- buf[--pos] = digits[rem];
2382
2582
  }
2383
2583
  /* Leading zero digits of the number are not printed; each leading zero byte is a '1'. */
2384
2584
  while (pos < sizeof(buf) && buf[pos] == '1') pos++;
@@ -2757,7 +2957,7 @@ static const SolPubkey TS_MPL_METADATA_ID = {{11,112,101,177,227,209,124,69,56,1
2757
2957
  static inline uint64_t ts_ata_address(const SolPubkey *owner, const SolPubkey *mint, const SolPubkey *token_program, SolPubkey *out) {
2758
2958
  SolSignerSeed seeds[3] = {{(const uint8_t *)owner, 32}, {(const uint8_t *)token_program, 32}, {(const uint8_t *)mint, 32}};
2759
2959
  uint8_t bump;
2760
- if (ts_find_pda(seeds, 3, &TS_ATA_PROGRAM_ID, out, &bump) != SUCCESS)
2960
+ if (ts_find_pda_fit(seeds, 3, &TS_ATA_PROGRAM_ID, out, &bump) != SUCCESS)
2761
2961
  TS_FAIL(TS_ERR_SEEDS, "TypeSOL: cannot derive the associated token address");
2762
2962
  return SUCCESS;
2763
2963
  }
@@ -3081,7 +3281,7 @@ static __attribute__((unused)) uint64_t ts_token_transfer_checked_hook(TsParams
3081
3281
  static inline uint64_t ts_metadata_address(const SolPubkey *mint, SolPubkey *out) {
3082
3282
  SolSignerSeed seeds[3] = {{(const uint8_t *)"metadata", 8}, {(const uint8_t *)&TS_MPL_METADATA_ID, 32}, {(const uint8_t *)mint, 32}};
3083
3283
  uint8_t bump;
3084
- if (ts_find_pda(seeds, 3, &TS_MPL_METADATA_ID, out, &bump) != SUCCESS)
3284
+ if (ts_find_pda_fit(seeds, 3, &TS_MPL_METADATA_ID, out, &bump) != SUCCESS)
3085
3285
  TS_FAIL(TS_ERR_SEEDS, "TypeSOL: cannot derive the metadata address");
3086
3286
  return SUCCESS;
3087
3287
  }
@@ -3092,7 +3292,7 @@ static inline uint64_t ts_master_edition_address(const SolPubkey *mint, SolPubke
3092
3292
  {(const uint8_t *)mint, 32},
3093
3293
  {(const uint8_t *)"edition", 7}};
3094
3294
  uint8_t bump;
3095
- if (ts_find_pda(seeds, 4, &TS_MPL_METADATA_ID, out, &bump) != SUCCESS)
3295
+ if (ts_find_pda_fit(seeds, 4, &TS_MPL_METADATA_ID, out, &bump) != SUCCESS)
3096
3296
  TS_FAIL(TS_ERR_SEEDS, "TypeSOL: cannot derive the master edition address");
3097
3297
  return SUCCESS;
3098
3298
  }
@@ -3252,7 +3452,7 @@ static inline uint64_t ts_update_extra_metas(TsParams *params, TsAccount *payer,
3252
3452
  SolSignerSeed seeds[2] = {{(const uint8_t *)"extra-account-metas", 19}, {(const uint8_t *)mint, 32}};
3253
3453
  SolPubkey pda;
3254
3454
  uint8_t bump;
3255
- if (ts_find_pda(seeds, 2, params->program_id, &pda, &bump) != SUCCESS || !ts_key_eq(&pda, &acct->key))
3455
+ if (ts_find_pda_fit(seeds, 2, params->program_id, &pda, &bump) != SUCCESS || !ts_key_eq(&pda, &acct->key))
3256
3456
  TS_FAIL(TS_ERR_SEEDS, "TypeSOL: not the mint's extra-account-metas PDA");
3257
3457
  if (!ts_key_eq(&acct->owner, params->program_id)) TS_FAIL(TS_ERR_WRONG_OWNER, "TypeSOL: the extra-account-metas account is not this program's");
3258
3458
  if (acct->data_len < 16 || ts_ld64(acct->data) != ts_ld64(TS_TRANSFER_HOOK_EXECUTE_DISC))