@typesol/cli 0.37.0 → 0.39.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 (52) hide show
  1. package/CHANGELOG.md +352 -1
  2. package/README.md +31 -14
  3. package/SECURITY.md +1 -1
  4. package/dist/compiler/analyze.d.ts +7 -6
  5. package/dist/compiler/analyze.js +350 -38
  6. package/dist/compiler/emit/alias.js +3 -2
  7. package/dist/compiler/emit/base.d.ts +5 -0
  8. package/dist/compiler/emit/base.js +2 -0
  9. package/dist/compiler/emit/borsh.js +63 -2
  10. package/dist/compiler/emit/calls.d.ts +9 -1
  11. package/dist/compiler/emit/calls.js +147 -18
  12. package/dist/compiler/emit/cpi-core.js +21 -6
  13. package/dist/compiler/emit/cpi.js +81 -27
  14. package/dist/compiler/emit/create.d.ts +7 -0
  15. package/dist/compiler/emit/create.js +27 -0
  16. package/dist/compiler/emit/expressions.js +7 -4
  17. package/dist/compiler/emit/function.d.ts +7 -0
  18. package/dist/compiler/emit/function.js +168 -30
  19. package/dist/compiler/emit/program.js +15 -2
  20. package/dist/compiler/emit/shared.d.ts +5 -5
  21. package/dist/compiler/emit/shared.js +5 -0
  22. package/dist/compiler/emit/statements.d.ts +8 -0
  23. package/dist/compiler/emit/statements.js +93 -6
  24. package/dist/compiler/emit/storage.d.ts +10 -1
  25. package/dist/compiler/emit/storage.js +50 -11
  26. package/dist/compiler/idl.d.ts +2 -2
  27. package/dist/compiler/idl.js +48 -13
  28. package/dist/compiler/model.d.ts +47 -10
  29. package/dist/compiler/model.js +14 -4
  30. package/dist/testing.js +8 -0
  31. package/dist/tools/clientgen.js +10 -1
  32. package/dist/tools/portgen.d.ts +6 -0
  33. package/dist/tools/portgen.js +68 -17
  34. package/docs/architecture.md +5 -4
  35. package/docs/audit.md +31 -19
  36. package/docs/case-study-escrow.md +5 -5
  37. package/docs/case-study-jito-mev.md +171 -0
  38. package/docs/case-study-marginfi.md +797 -0
  39. package/docs/case-study-multisig.md +3 -3
  40. package/docs/case-study-raydium-clmm.md +151 -101
  41. package/docs/case-study-raydium-cp-swap.md +3 -3
  42. package/docs/cli.md +5 -5
  43. package/docs/client.md +1 -1
  44. package/docs/errors.md +15 -4
  45. package/docs/language.md +39 -26
  46. package/docs/migrate-from-anchor.md +4 -1
  47. package/docs/runtime.md +1 -1
  48. package/docs/security-model.md +2 -1
  49. package/docs/testing.md +33 -1
  50. package/lib/typesol.d.ts +48 -9
  51. package/package.json +9 -7
  52. package/runtime/typesol_rt.h +111 -17
@@ -38,6 +38,7 @@
38
38
  #define TS_ERR_DUPLICATE_MUT 2040 /* ConstraintDuplicateMutableAccount */
39
39
  #define TS_ERR_ALREADY_INITIALIZED 3000 /* AccountDiscriminatorAlreadySet */
40
40
  #define TS_ERR_INIT_PAYER 4101 /* TryingToInitPayerAsProgramAccount */
41
+ #define TS_ERR_DISC_NOT_FOUND 3001 /* AccountDiscriminatorNotFound */
41
42
  #define TS_ERR_DISCRIMINATOR 3002 /* AccountDiscriminatorMismatch */
42
43
  #define TS_ERR_ACCOUNT_TOO_SMALL 3003 /* AccountDidNotDeserialize */
43
44
  #define TS_ERR_DID_NOT_SERIALIZE 3004 /* AccountDidNotSerialize */
@@ -73,6 +74,10 @@ static __attribute__((noinline)) uint64_t ts_fail(uint64_t code, const char *msg
73
74
  return code;
74
75
  }
75
76
  #define TS_FAIL(code, msg) return ts_fail((code), (msg), sol_strlen(msg))
77
+ /* require(condition, code) with a code computed at run time: 0 would read as success, so it fails as an overflow. */
78
+ static __attribute__((noinline, unused)) uint64_t ts_fail_code(uint32_t code, const char *msg) {
79
+ return ts_fail(code ? code : TS_ERR_OVERFLOW, msg, sol_strlen(msg));
80
+ }
76
81
 
77
82
  #define TS_TRY(expr) \
78
83
  do { \
@@ -355,17 +360,25 @@ static __attribute__((noinline)) void ts_divmod32(const uint32_t *u, int mmax, T
355
360
  }
356
361
  if (rem) *rem = ts_u256_from32(r);
357
362
  }
363
+ /* Two 64-bit values: one 128-bit product. Otherwise schoolbook over the limbs that are not zero (two 88-bit values
364
+ take 9 products, not 64). */
358
365
  static inline bool ts_u256_mul(TsU256 a, TsU256 b, TsU256 *out) {
359
- if (ts_u256_fits(a, 128) && ts_u256_fits(b, 128) && ((a.w[1] | b.w[1]) == 0)) {
366
+ if (((a.w[1] | a.w[2] | a.w[3]) | (b.w[1] | b.w[2] | b.w[3])) == 0) {
360
367
  *out = ts_u256_from_u128((unsigned __int128)a.w[0] * b.w[0]);
361
368
  return false;
362
369
  }
363
370
  uint32_t x[8], y[8], r[16];
364
371
  ts_u256_to32(a, x);
365
372
  ts_u256_to32(b, y);
366
- ts_mul32(x, 8, y, 8, r);
373
+ int n = ts_len32(x, 8), m = ts_len32(y, 8);
374
+ if (n == 0 || m == 0) {
375
+ *out = ts_u256_lit(0, 0, 0, 0);
376
+ return false;
377
+ }
378
+ ts_mul32(x, n, y, m, r);
379
+ for (int i = n + m; i < 16; i++) r[i] = 0;
367
380
  *out = ts_u256_from32(r);
368
- for (int i = 8; i < 16; i++)
381
+ for (int i = 8; i < n + m; i++)
369
382
  if (r[i]) return true;
370
383
  return false;
371
384
  }
@@ -711,6 +724,18 @@ static inline uint64_t ts_check_program_account(const TsAccount *a, const SolPub
711
724
  return SUCCESS;
712
725
  }
713
726
 
727
+ /* A zero-copy (`@serialization bytemuck`) account, as Anchor's AccountLoader checks one: owner, then a discriminator to
728
+ read and match, with no "not initialized" case. Anchor's load() then panics on data shorter than the struct; this
729
+ refuses it (3003). */
730
+ static inline uint64_t ts_check_loader_account(const TsAccount *a, const SolPubkey *program_id, const uint8_t disc[8],
731
+ uint64_t size) {
732
+ if (!ts_key_eq(&a->owner, program_id)) TS_FAIL(TS_ERR_WRONG_OWNER, "TypeSOL: account not owned by this program");
733
+ if (a->data_len < 8) TS_FAIL(TS_ERR_DISC_NOT_FOUND, "TypeSOL: account has no discriminator");
734
+ if (!ts_mem_eq(a->data, disc, 8)) TS_FAIL(TS_ERR_DISCRIMINATOR, "TypeSOL: account discriminator mismatch");
735
+ if (a->data_len < 8 + size) TS_FAIL(TS_ERR_ACCOUNT_TOO_SMALL, "TypeSOL: account data too small");
736
+ return SUCCESS;
737
+ }
738
+
714
739
  /* An account ending in Vec<T>: data must be exactly fixed part + len * element size. */
715
740
  static inline uint64_t ts_check_vec_len(const TsAccount *a, uint64_t fixed, uint32_t len, uint64_t elem) {
716
741
  if (a->data_len != 8 + fixed + (uint64_t)len * elem)
@@ -756,6 +781,44 @@ static inline uint64_t ts_claim_mut_at(TsParams *params, uint64_t at) {
756
781
  return SUCCESS;
757
782
  }
758
783
 
784
+ /* A loadAccountMut inside a loop whose view cannot outlive the iteration: the claim's index is kept in `*slot`, so the
785
+ iteration's end can drop it (ts_release_mut) and the next iteration may load the same account again.
786
+ A remaining account may be passed more than once here (a route through one pool twice): its slots share one record,
787
+ and the claim bit is the last slot's, so two live views of it are still refused. It may not also be one of the
788
+ instruction's own accounts (`declared`: their count), whose views last the whole instruction. */
789
+ static inline uint64_t ts_claim_mut_rec(TsParams *params, const TsAccount *a, uint64_t *slot, uint64_t declared) {
790
+ uint64_t seen = 0, at = 0, first = (uint64_t)-1;
791
+ for (uint64_t i = 0; i < params->ka_num; i++)
792
+ if (params->ka[i] == a) {
793
+ seen++, at = i;
794
+ if (first == (uint64_t)-1) first = i;
795
+ }
796
+ if (seen > 1 && first < declared) TS_FAIL(TS_ERR_DUPLICATE_MUT, "TypeSOL: account passed more than once");
797
+ #if TS_MAX_ACCOUNTS > 64
798
+ if (params->loaded_mut[at >> 6] >> (at & 63) & 1) TS_FAIL(TS_ERR_DUPLICATE_MUT, "TypeSOL: account already loaded with loadAccountMut");
799
+ params->loaded_mut[at >> 6] |= (uint64_t)1 << (at & 63);
800
+ #else
801
+ if (params->loaded_mut >> at & 1) TS_FAIL(TS_ERR_DUPLICATE_MUT, "TypeSOL: account already loaded with loadAccountMut");
802
+ params->loaded_mut |= (uint64_t)1 << at;
803
+ #endif
804
+ *slot = at;
805
+ return SUCCESS;
806
+ }
807
+ static inline uint64_t ts_claim_mut_at_rec(TsParams *params, uint64_t at, uint64_t *slot) {
808
+ TS_TRY(ts_claim_mut_at(params, at));
809
+ *slot = at;
810
+ return SUCCESS;
811
+ }
812
+ static inline void ts_release_mut(TsParams *params, uint64_t *slot) {
813
+ if (*slot == (uint64_t)-1) return;
814
+ #if TS_MAX_ACCOUNTS > 64
815
+ params->loaded_mut[*slot >> 6] &= ~((uint64_t)1 << (*slot & 63));
816
+ #else
817
+ params->loaded_mut &= ~((uint64_t)1 << *slot);
818
+ #endif
819
+ *slot = (uint64_t)-1;
820
+ }
821
+
759
822
  static inline uint64_t ts_check_address(const TsAccount *a, const SolPubkey *expected) {
760
823
  if (!ts_key_eq(&a->key, expected)) TS_FAIL(TS_ERR_WRONG_PROGRAM, "TypeSOL: unexpected program address");
761
824
  return SUCCESS;
@@ -778,13 +841,19 @@ static inline bool ts_is_token_program(const SolPubkey *k) {
778
841
  }
779
842
  /* A Token-2022 Multisig is 355 bytes and its signer bytes are attacker-chosen: never accept that length. */
780
843
  #define TS_MULTISIG_LEN 355
844
+ /* A token account or mint not owned by a token program. As Anchor's (Interface)Account: an empty system account (never
845
+ created) is "not initialized", anything else a wrong owner. Out of line: the checks that pass stay as small as before. */
846
+ static __attribute__((noinline)) uint64_t ts_token_owner_fail(const TsAccount *a) {
847
+ if (a->lamports == 0 && ts_key_eq(&a->owner, &TS_SYSTEM_PROGRAM_ID)) TS_FAIL(TS_ERR_NOT_INITIALIZED, "TypeSOL: account is not initialized");
848
+ TS_FAIL(TS_ERR_WRONG_OWNER, "TypeSOL: not owned by a token program");
849
+ }
781
850
  static inline uint64_t ts_check_token_account(const TsAccount *a) {
782
851
  bool ok = false;
783
852
  if (ts_key_eq(&a->owner, &TS_TOKEN_PROGRAM_ID)) ok = a->data_len == TS_TOKEN_ACCOUNT_LEN;
784
853
  else if (ts_key_eq(&a->owner, &TS_TOKEN_2022_PROGRAM_ID))
785
854
  ok = a->data_len == TS_TOKEN_ACCOUNT_LEN ||
786
855
  (a->data_len > TS_TOKEN_ACCOUNT_LEN && a->data_len != TS_MULTISIG_LEN && a->data[TS_TOKEN_ACCOUNT_LEN] == 2);
787
- else TS_FAIL(TS_ERR_WRONG_OWNER, "TypeSOL: token account not owned by a token program");
856
+ else return ts_token_owner_fail(a);
788
857
  if (!ok || a->data[108] == 0) TS_FAIL(TS_ERR_TOKEN_ACCOUNT, "TypeSOL: not an initialized token account");
789
858
  return SUCCESS;
790
859
  }
@@ -795,7 +864,7 @@ static inline uint64_t ts_check_mint(const TsAccount *a) {
795
864
  else if (ts_key_eq(&a->owner, &TS_TOKEN_2022_PROGRAM_ID))
796
865
  ok = a->data_len == TS_MINT_LEN ||
797
866
  (a->data_len > TS_TOKEN_ACCOUNT_LEN && a->data_len != TS_MULTISIG_LEN && a->data[TS_TOKEN_ACCOUNT_LEN] == 1);
798
- else TS_FAIL(TS_ERR_WRONG_OWNER, "TypeSOL: mint not owned by a token program");
867
+ else return ts_token_owner_fail(a);
799
868
  if (!ok || a->data[45] != 1) TS_FAIL(TS_ERR_MINT, "TypeSOL: not an initialized mint");
800
869
  return SUCCESS;
801
870
  }
@@ -998,10 +1067,13 @@ static inline uint64_t ts_init_account(TsAccount *a, const SolPubkey *program_id
998
1067
  return SUCCESS;
999
1068
  }
1000
1069
 
1001
- /* Pubkey order: Rust's Ord on [u8; 32], lexicographic. */
1070
+ /* Pubkey order: Rust's Ord on [u8; 32], lexicographic. Eight bytes at a time: a word read big-endian orders as its
1071
+ bytes do, so the first word that differs decides. */
1002
1072
  static inline int64_t ts_key_cmp(const SolPubkey *a, const SolPubkey *b) {
1003
- for (int i = 0; i < 32; i++)
1004
- if (a->x[i] != b->x[i]) return a->x[i] < b->x[i] ? -1 : 1;
1073
+ for (int i = 0; i < 32; i += 8) {
1074
+ uint64_t x = ts_ld64(a->x + i), y = ts_ld64(b->x + i);
1075
+ if (x != y) return __builtin_bswap64(x) < __builtin_bswap64(y) ? -1 : 1;
1076
+ }
1005
1077
  return 0;
1006
1078
  }
1007
1079
 
@@ -1034,12 +1106,12 @@ static inline uint64_t ts_system_transfer(TsParams *params, TsAccount *from, TsA
1034
1106
  }
1035
1107
 
1036
1108
  /*
1037
- * Create `acct` as a PDA of this program. `seeds` must have room for one extra
1038
- * entry: the bump is appended there. Writes the discriminator on success.
1109
+ * Create `acct` as a PDA of this program, owned by `owner` (Anchor's `init` with `owner = …`). `seeds` must have room
1110
+ * for one extra entry: the bump is appended there.
1039
1111
  */
1040
- static inline uint64_t ts_create_pda(TsParams *params, TsAccount *payer, TsAccount *acct,
1041
- SolSignerSeed *seeds, int n, const uint8_t disc[8], uint64_t space,
1042
- uint8_t *bump_out) {
1112
+ static inline uint64_t ts_create_pda_owned(TsParams *params, TsAccount *payer, TsAccount *acct,
1113
+ SolSignerSeed *seeds, int n, const SolPubkey *owner, uint64_t space,
1114
+ uint8_t *bump_out) {
1043
1115
  SolPubkey derived;
1044
1116
  uint8_t bump;
1045
1117
  if (ts_find_pda(seeds, n, params->program_id, &derived, &bump) != SUCCESS ||
@@ -1059,7 +1131,7 @@ static inline uint64_t ts_create_pda(TsParams *params, TsAccount *payer, TsAccou
1059
1131
  ts_memcpy(data, &tag, 4);
1060
1132
  ts_memcpy(data + 4, &lamports, 8);
1061
1133
  ts_memcpy(data + 12, &space, 8);
1062
- ts_memcpy(data + 20, params->program_id, 32);
1134
+ ts_memcpy(data + 20, owner, 32);
1063
1135
  SolAccountMeta metas[2] = {{&payer->key, true, true}, {&acct->key, true, true}};
1064
1136
  SolInstruction ix = {(SolPubkey *)&TS_SYSTEM_PROGRAM_ID, metas, 2, data, sizeof(data)};
1065
1137
  TS_TRY(ts_invoke(params, &ix, &signer, 1));
@@ -1077,17 +1149,25 @@ static inline uint64_t ts_create_pda(TsParams *params, TsAccount *payer, TsAccou
1077
1149
  uint8_t assign[36];
1078
1150
  uint32_t assign_tag = 1; /* Assign */
1079
1151
  ts_memcpy(assign, &assign_tag, 4);
1080
- ts_memcpy(assign + 4, params->program_id, 32);
1152
+ ts_memcpy(assign + 4, owner, 32);
1081
1153
  SolInstruction assign_ix = {(SolPubkey *)&TS_SYSTEM_PROGRAM_ID, one, 1, assign, sizeof(assign)};
1082
1154
  TS_TRY(ts_invoke(params, &assign_ix, &signer, 1));
1083
1155
  }
1084
1156
 
1085
1157
  acct->data_len = space;
1086
- ts_memcpy(acct->data, disc, 8);
1087
1158
  *bump_out = bump;
1088
1159
  return SUCCESS;
1089
1160
  }
1090
1161
 
1162
+ /* Create `acct` as a PDA of this program, owned by it; writes the discriminator on success. */
1163
+ static inline uint64_t ts_create_pda(TsParams *params, TsAccount *payer, TsAccount *acct,
1164
+ SolSignerSeed *seeds, int n, const uint8_t disc[8], uint64_t space,
1165
+ uint8_t *bump_out) {
1166
+ TS_TRY(ts_create_pda_owned(params, payer, acct, seeds, n, params->program_id, space, bump_out));
1167
+ ts_memcpy(acct->data, disc, 8);
1168
+ return SUCCESS;
1169
+ }
1170
+
1091
1171
  /*
1092
1172
  * Create `acct` with `space` bytes owned by `owner` (a token program), funded by `payer`: a PDA of this program when
1093
1173
  * `n` > 0 (seeds without the bump; a pre-funded address is topped up, allocated and assigned), otherwise a new keypair
@@ -1239,6 +1319,15 @@ static inline uint64_t ts_resize(TsParams *params, TsAccount *payer, TsAccount *
1239
1319
  return SUCCESS;
1240
1320
  }
1241
1321
 
1322
+ /* resizeAccount(): the data length only (AccountInfo::resize), new bytes zeroed; no rent moves. */
1323
+ static inline uint64_t ts_resize_raw(TsAccount *acct, uint64_t new_len, uint64_t entry_len) {
1324
+ if (new_len > entry_len + MAX_PERMITTED_DATA_INCREASE)
1325
+ TS_FAIL(TS_ERR_REALLOC, "TypeSOL: account would grow more than 10 KiB in one instruction");
1326
+ if (new_len > acct->data_len) ts_memzero(acct->data + acct->data_len, new_len - acct->data_len);
1327
+ acct->data_len = new_len;
1328
+ return SUCCESS;
1329
+ }
1330
+
1242
1331
  /*
1243
1332
  * InitIfNeeded: create the PDA like ts_create_pda, or accept it when it already exists as this program's
1244
1333
  * account of the right type (owner, discriminator, size). The PDA is re-derived either way.
@@ -1511,7 +1600,12 @@ static const SolPubkey TS_INSTRUCTIONS_SYSVAR_ID = {{6,167,213,23,24,123,209,102
1511
1600
  * difference as uint8_t, so any difference is positive.)
1512
1601
  */
1513
1602
  static inline int ts_bytes_cmp(const uint8_t *a, const uint8_t *b, uint64_t n) {
1514
- for (uint64_t i = 0; i < n; i++)
1603
+ uint64_t i = 0;
1604
+ for (; i + 8 <= n; i += 8) {
1605
+ uint64_t x = ts_ld64(a + i), y = ts_ld64(b + i);
1606
+ if (x != y) return __builtin_bswap64(x) < __builtin_bswap64(y) ? -1 : 1;
1607
+ }
1608
+ for (; i < n; i++)
1515
1609
  if (a[i] != b[i]) return a[i] < b[i] ? -1 : 1;
1516
1610
  return 0;
1517
1611
  }