@typesol/cli 0.49.0 → 0.50.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 (56) hide show
  1. package/CHANGELOG.md +397 -0
  2. package/NOTICE +9 -0
  3. package/README.md +25 -8
  4. package/SECURITY.md +4 -3
  5. package/dist/compiler/analyze.d.ts +2 -0
  6. package/dist/compiler/analyze.js +7 -1
  7. package/dist/compiler/emit/base.d.ts +4 -3
  8. package/dist/compiler/emit/base.js +9 -3
  9. package/dist/compiler/emit/borsh.js +8 -8
  10. package/dist/compiler/emit/calls.d.ts +6 -0
  11. package/dist/compiler/emit/calls.js +71 -12
  12. package/dist/compiler/emit/cpi-core.js +3 -3
  13. package/dist/compiler/emit/cpi.js +2 -2
  14. package/dist/compiler/emit/expressions.d.ts +1 -0
  15. package/dist/compiler/emit/expressions.js +12 -0
  16. package/dist/compiler/emit/function.js +12 -8
  17. package/dist/compiler/emit/program.js +8 -2
  18. package/dist/compiler/emit/rbtree.js +1 -1
  19. package/dist/compiler/emit/shared.d.ts +12 -2
  20. package/dist/compiler/emit/shared.js +26 -6
  21. package/dist/compiler/emit/statements.d.ts +8 -1
  22. package/dist/compiler/emit/statements.js +53 -5
  23. package/dist/compiler/emit/storage.js +12 -10
  24. package/dist/compiler/emit/views.js +1 -1
  25. package/dist/compiler/index.d.ts +15 -1
  26. package/dist/compiler/index.js +50 -9
  27. package/dist/keys.d.ts +12 -1
  28. package/dist/keys.js +57 -7
  29. package/dist/workspace.d.ts +8 -0
  30. package/dist/workspace.js +90 -15
  31. package/docker/deps/0.50.0.lock.json +1206 -0
  32. package/docs/architecture.md +1 -1
  33. package/docs/audit.md +13 -12
  34. package/docs/case-study-jito-mev.md +14 -14
  35. package/docs/case-study-marginfi.md +127 -127
  36. package/docs/case-study-multisig.md +16 -4
  37. package/docs/case-study-raydium-clmm.md +44 -28
  38. package/docs/case-study-raydium-cp-swap.md +18 -18
  39. package/docs/cli.md +4 -3
  40. package/docs/language-accounts.md +16 -10
  41. package/docs/language-basics.md +4 -1
  42. package/docs/language-builtins.md +4 -2
  43. package/docs/language-cpi.md +34 -0
  44. package/docs/language-native.md +50 -4
  45. package/docs/language-statements.md +2 -1
  46. package/docs/language-types.md +13 -3
  47. package/docs/runtime.md +19 -2
  48. package/docs/security-model.md +4 -3
  49. package/docs/stability.md +5 -4
  50. package/docs/testing.md +6 -2
  51. package/lib/typesol.d.ts +25 -3
  52. package/llms-full.txt +136 -31
  53. package/package.json +1 -1
  54. package/releases/0.49.0.json +857 -0
  55. package/runtime/typesol_builtins.c +47 -2
  56. package/runtime/typesol_rt.h +129 -3
@@ -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.
@@ -761,11 +825,44 @@ static inline uint64_t ts_find_pda(const SolSignerSeed *seeds, int n, const SolP
761
825
  }
762
826
 
763
827
  /* ---- Account checks ------------------------------------------------------ */
764
- /* Copy between two places of the same type: distinct, or the very same place (then nothing to do). */
828
+ /* Copy between two places of the same type that cannot overlap (TS_COPY_LOCAL: one is a local of the function):
829
+ distinct, or the very same place (then nothing to do). */
765
830
  static inline void ts_copy(void *dst, const void *src, uint64_t n) {
766
831
  if (dst != src) ts_memcpy(dst, src, n);
767
832
  }
768
833
 
834
+ /*
835
+ * TS_COPY below TS_COPY_SYSCALL_MIN: a whole value between two places of one type reached through pointers, which may
836
+ * overlap (views of one account at run-time offsets, an account passed twice under NATIVE_ACCOUNT_WRITES). Every byte
837
+ * is loaded before any is stored, so the destination ends with the source's old bytes in either direction, as the
838
+ * memmove syscall leaves them from the threshold on. The size is known when compiling: the loops unroll into a load
839
+ * 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
840
+ * 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.
841
+ * Measured against the forward word loop it replaces (ts_copy, which re-read bytes it had already written when the
842
+ * destination was ahead of an overlapping source): 1 to 2 CU less for 16 and 40 bytes, the same for 72, 10 less for
843
+ * 6 and 79 bytes (test/language/bulk-copies.test.ts); more registers are live between the loads and the stores.
844
+ */
845
+ static inline __attribute__((always_inline)) void ts_copy_value(void *dst, const void *src, uint64_t n) {
846
+ uint8_t *d = (uint8_t *)dst;
847
+ const uint8_t *s = (const uint8_t *)src;
848
+ if (n >= 8) {
849
+ uint64_t w[TS_COPY_SYSCALL_MIN / 8];
850
+ uint64_t k = n / 8 < TS_COPY_SYSCALL_MIN / 8 ? n / 8 : TS_COPY_SYSCALL_MIN / 8;
851
+ for (uint64_t i = 0; i < k; i++) w[i] = ts_ld64(s + 8 * i);
852
+ uint64_t t = ts_ld64(s + n - 8);
853
+ for (uint64_t i = 0; i < k; i++) ts_st64(d + 8 * i, w[i]);
854
+ if (n % 8) ts_st64(d + n - 8, t);
855
+ } else if (n >= 4) {
856
+ uint32_t a = *(const ts_u32_unaligned *)s, b = *(const ts_u32_unaligned *)(s + n - 4);
857
+ *(ts_u32_unaligned *)d = a;
858
+ *(ts_u32_unaligned *)(d + n - 4) = b;
859
+ } else {
860
+ uint8_t b[3];
861
+ for (uint64_t i = 0; i < n && i < 3; i++) b[i] = s[i];
862
+ for (uint64_t i = 0; i < n && i < 3; i++) d[i] = b[i];
863
+ }
864
+ }
865
+
769
866
  static inline __attribute__((always_inline)) bool ts_key_eq(const SolPubkey *a, const SolPubkey *b);
770
867
  #ifdef TS_OWNER_CACHE
771
868
  /* Is account `a` this program's? The views of native accounts (loadRaw, loadRawAt, rawArrayAt, rbTreeAt) check it each
@@ -1419,7 +1516,7 @@ static inline uint64_t ts_resize(TsParams *params, TsAccount *payer, TsAccount *
1419
1516
  if (payer == NULL) TS_FAIL(TS_ERR_REALLOC, "TypeSOL: growing an account needs a payer");
1420
1517
  TS_TRY(ts_system_transfer(params, payer, acct, new_rent - old_rent));
1421
1518
  }
1422
- ts_memzero(acct->data + old_len, new_len - old_len);
1519
+ ts_memzero_run(acct->data + old_len, new_len - old_len);
1423
1520
  }
1424
1521
  acct->data_len = new_len;
1425
1522
  return SUCCESS;
@@ -1434,7 +1531,7 @@ static inline __attribute__((always_inline)) uint64_t ts_entry_len(const TsAccou
1434
1531
  static inline uint64_t ts_resize_raw(TsAccount *acct, uint64_t new_len, uint64_t entry_len) {
1435
1532
  if (new_len > entry_len + MAX_PERMITTED_DATA_INCREASE)
1436
1533
  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);
1534
+ if (new_len > acct->data_len) ts_memzero_run(acct->data + acct->data_len, new_len - acct->data_len);
1438
1535
  acct->data_len = new_len;
1439
1536
  return SUCCESS;
1440
1537
  }
@@ -1485,6 +1582,20 @@ static inline uint64_t ts_close(TsAccount *acct, TsAccount *dest) {
1485
1582
  ts_memcpy(&acct->owner, &TS_SYSTEM_PROGRAM_ID, 32);
1486
1583
  return SUCCESS;
1487
1584
  }
1585
+ /* ts_close for an account known to hold at least TS_ZERO_RUN_MIN bytes (its type's smallest size; TS_CLOSE decides
1586
+ when compiling): the data zeroed by the memset syscall. */
1587
+ static inline uint64_t ts_close_big(TsAccount *acct, TsAccount *dest) {
1588
+ uint64_t total;
1589
+ if (ts_add_overflow_u64(dest->lamports, acct->lamports, &total))
1590
+ TS_FAIL(TS_ERR_OVERFLOW, "TypeSOL: arithmetic overflow");
1591
+ dest->lamports = total;
1592
+ acct->lamports = 0;
1593
+ sol_memset_(acct->data, 0, acct->data_len);
1594
+ acct->data_len = 0;
1595
+ ts_memcpy(&acct->owner, &TS_SYSTEM_PROGRAM_ID, 32);
1596
+ return SUCCESS;
1597
+ }
1598
+ #define TS_CLOSE(acct, dest, min) ((min) >= TS_ZERO_RUN_MIN ? ts_close_big((acct), (dest)) : ts_close((acct), (dest)))
1488
1599
 
1489
1600
  /* A CPI to `program` (an account passed in), signed by one PDA when `nseeds` > 0. */
1490
1601
  static inline uint64_t ts_cpi(TsParams *params, const TsAccount *program, uint8_t *data, uint64_t len,
@@ -1833,6 +1944,16 @@ extern uint64_t sol_get_stack_height(void);
1833
1944
  /* The runtime's `abort` syscall (Rust's abort on SBF, e.g. the allocator's out-of-memory error): ProgramFailedToComplete,
1834
1945
  * logged as "SBF program panicked", with no location. */
1835
1946
  extern __attribute__((noreturn)) void abort(void);
1947
+ #ifdef TS_PANIC_PINOCCHIO
1948
+ /* PANIC_STYLE "pinocchio": a Rust panic as pinocchio's `default_panic_handler!` hook reports it under std: the file of
1949
+ * the panic's location verbatim (`location.file()`, no line or column, no message), then "** PANICKED **", then std's
1950
+ * abort ("SBF program panicked"). One out-of-line copy: every panic site is a call with its file. */
1951
+ static __attribute__((noinline, noreturn, cold, unused)) void ts_panic_file(const char *file, uint64_t len) {
1952
+ sol_log_(file, len);
1953
+ sol_log_("** PANICKED **", 14);
1954
+ abort();
1955
+ }
1956
+ #endif
1836
1957
 
1837
1958
  /*
1838
1959
  * ---- BigVec (SPL's big_vec) ------------------------------------------------
@@ -1843,6 +1964,11 @@ extern __attribute__((noreturn)) void abort(void);
1843
1964
  * thousands). Overlapping ranges are allowed. The "no SDK byte loops" rule is about the SDK's C loops, not this.
1844
1965
  */
1845
1966
  extern void sol_memmove_(void *dst, const void *src, uint64_t n);
1967
+ /* copyData: the memmove syscall from TS_MEMCPY_SYSCALL_MIN bytes (the ranges may overlap), the word loop below it. */
1968
+ static inline __attribute__((always_inline, unused)) void ts_memmove_bulk(void *dst, const void *src, uint64_t n) {
1969
+ if (n >= 32) sol_memmove_(dst, src, n);
1970
+ else ts_memmove(dst, src, n);
1971
+ }
1846
1972
  /* The memcpy syscall, for bulk copies of a length known only at run time (FixedVec.pushAll): max(10, n / 250) CU plus
1847
1973
  * the call, against about 0.74 CU a byte for ts_memcpy's word loop over unaligned data (measured: 1000 bytes of
1848
1974
  * instruction data), so copies under 32 bytes keep the loop. The ranges never overlap (a FixedVec's free slots and