solana.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. // SPDX-License-Identifier: Apache-2.0
  2. #include <stdint.h>
  3. #include <stddef.h>
  4. #include "stdlib.h"
  5. #include "solana_sdk.h"
  6. extern uint64_t solang_dispatch(SolParameters *param);
  7. extern void __init_heap();
  8. // The address 'SysvarC1ock11111111111111111111111111111111' base58 decoded
  9. static const SolPubkey clock_address = {0x06, 0xa7, 0xd5, 0x17, 0x18, 0xc7, 0x74, 0xc9, 0x28, 0x56, 0x63,
  10. 0x98, 0x69, 0x1d, 0x5e, 0xb6, 0x8b, 0x5e, 0xb8, 0xa3, 0x9b, 0x4b,
  11. 0x6d, 0x5c, 0x73, 0x55, 0x5b, 0x21, 0x00, 0x00, 0x00, 0x00};
  12. // The address 'Sysvar1nstructions1111111111111111111111111' base58 decoded
  13. static const SolPubkey instructions_address = {0x06, 0xa7, 0xd5, 0x17, 0x18, 0x7b, 0xd1, 0x66, 0x35, 0xda, 0xd4,
  14. 0x04, 0x55, 0xfd, 0xc2, 0xc0, 0xc1, 0x24, 0xc6, 0x8f, 0x21, 0x56,
  15. 0x75, 0xa5, 0xdb, 0xba, 0xcb, 0x5f, 0x08, 0x00, 0x00, 0x00};
  16. // The address 'Ed25519SigVerify111111111111111111111111111' base58 decoded
  17. static const SolPubkey ed25519_address = {0x03, 0x7d, 0x46, 0xd6, 0x7c, 0x93, 0xfb, 0xbe, 0x12, 0xf9, 0x42,
  18. 0x8f, 0x83, 0x8d, 0x40, 0xff, 0x05, 0x70, 0x74, 0x49, 0x27, 0xf4,
  19. 0x8a, 0x64, 0xfc, 0xca, 0x70, 0x44, 0x80, 0x00, 0x00, 0x00};
  20. #ifndef TEST
  21. uint64_t entrypoint(const uint8_t *input)
  22. {
  23. SolParameters params;
  24. uint64_t ret = sol_deserialize(input, &params);
  25. if (ret)
  26. {
  27. return ret;
  28. }
  29. params.ka_clock = NULL;
  30. params.ka_instructions = NULL;
  31. for (int account_no = 0; account_no < params.ka_num; account_no++)
  32. {
  33. const SolAccountInfo *acc = &params.ka[account_no];
  34. if (SolPubkey_same(&clock_address, acc->key))
  35. {
  36. params.ka_clock = acc;
  37. }
  38. else if (SolPubkey_same(&instructions_address, acc->key))
  39. {
  40. params.ka_instructions = acc;
  41. }
  42. }
  43. __init_heap();
  44. return solang_dispatch(&params);
  45. }
  46. uint64_t *sol_account_lamport(uint8_t *address, SolParameters *params)
  47. {
  48. SolPubkey *pubkey = (SolPubkey *)address;
  49. for (int i = 0; i < params->ka_num; i++)
  50. {
  51. if (SolPubkey_same(pubkey, params->ka[i].key))
  52. {
  53. return params->ka[i].lamports;
  54. }
  55. }
  56. sol_log_pubkey(pubkey);
  57. sol_log("account missing from transaction");
  58. sol_panic();
  59. return NULL;
  60. }
  61. void sol_transfer(uint8_t *to_address, uint64_t lamports, SolParameters *params)
  62. {
  63. uint64_t *from = params->ka[0].lamports;
  64. uint64_t *to = sol_account_lamport(to_address, params);
  65. uint64_t from_balance;
  66. uint64_t to_balance;
  67. if (__builtin_sub_overflow(*from, lamports, &from_balance))
  68. {
  69. sol_log("sender does not have enough balance");
  70. sol_panic();
  71. }
  72. if (__builtin_add_overflow(*to, lamports, &to_balance))
  73. {
  74. sol_log("recipient lamports overflows");
  75. sol_panic();
  76. }
  77. *from = from_balance;
  78. *to = to_balance;
  79. }
  80. bool sol_try_transfer(uint8_t *to_address, uint64_t lamports, SolParameters *params)
  81. {
  82. uint64_t *from = params->ka[0].lamports;
  83. uint64_t *to = sol_account_lamport(to_address, params);
  84. uint64_t from_balance;
  85. uint64_t to_balance;
  86. if (__builtin_sub_overflow(*from, lamports, &from_balance))
  87. {
  88. return false;
  89. }
  90. if (__builtin_add_overflow(*to, lamports, &to_balance))
  91. {
  92. return false;
  93. }
  94. *from = from_balance;
  95. *to = to_balance;
  96. return true;
  97. }
  98. #endif
  99. uint64_t address_hash(uint8_t data[32])
  100. {
  101. uint64_t hash = 0;
  102. uint32_t i;
  103. for (i = 0; i < 32; i++)
  104. {
  105. hash += data[i];
  106. }
  107. return hash;
  108. }
  109. bool address_equal(void *a, void *b)
  110. {
  111. uint64_t *left = a;
  112. uint64_t *right = b;
  113. for (uint32_t i = 0; i < 4; i++)
  114. {
  115. if (left[i] != right[i])
  116. {
  117. return false;
  118. }
  119. }
  120. return true;
  121. }
  122. struct ed25519_instruction_sig
  123. {
  124. uint16_t signature_offset;
  125. uint16_t signature_instruction_index;
  126. uint16_t public_key_offset;
  127. uint16_t public_key_instruction_index;
  128. uint16_t message_offset;
  129. uint16_t message_size;
  130. uint16_t message_instruction_index;
  131. uint8_t public_key[SIZE_PUBKEY];
  132. uint8_t signature[64];
  133. uint8_t message[0];
  134. };
  135. struct ed25519_instruction
  136. {
  137. uint8_t num_signatures;
  138. uint8_t padding;
  139. struct ed25519_instruction_sig sig[0];
  140. };
  141. uint64_t signature_verify(uint8_t *public_key, struct vector *message, struct vector *signature, SolParameters *params)
  142. {
  143. if (params->ka_instructions)
  144. {
  145. uint16_t *data = (uint16_t *)params->ka_instructions->data;
  146. uint64_t instr_count = data[0];
  147. // for each instruction
  148. for (uint64_t instr_no = 0; instr_no < instr_count; instr_no++)
  149. {
  150. uint8_t *instr = params->ka_instructions->data + data[1 + instr_no];
  151. // step over the accounts
  152. uint64_t accounts = *((uint16_t *)instr);
  153. instr += accounts * 33 + 2;
  154. if (sol_memcmp(&ed25519_address, instr, sizeof(ed25519_address)))
  155. {
  156. continue;
  157. }
  158. // step over program_id and length
  159. instr += 2 + 32;
  160. struct ed25519_instruction *ed25519 = (struct ed25519_instruction *)instr;
  161. for (uint64_t sig_no = 0; sig_no < ed25519->num_signatures; sig_no++)
  162. {
  163. struct ed25519_instruction_sig *sig = &ed25519->sig[sig_no];
  164. if (sig->public_key_instruction_index != instr_no || sig->signature_instruction_index != instr_no ||
  165. sig->message_instruction_index != instr_no)
  166. continue;
  167. if (sol_memcmp(public_key, instr + sig->public_key_offset, SIZE_PUBKEY))
  168. {
  169. continue;
  170. }
  171. if (sol_memcmp(signature->data, instr + sig->signature_offset, 64))
  172. {
  173. continue;
  174. }
  175. if (sig->message_size != message->len)
  176. {
  177. continue;
  178. }
  179. if (sol_memcmp(message->data, instr + sig->message_offset, message->len))
  180. {
  181. continue;
  182. }
  183. return 0;
  184. }
  185. }
  186. }
  187. sol_log("could not find verified signature");
  188. return 1;
  189. }
  190. struct clock_layout
  191. {
  192. uint64_t slot;
  193. uint64_t epoch_start_timestamp;
  194. uint64_t epoch;
  195. uint64_t leader_schedule_epoch;
  196. uint64_t unix_timestamp;
  197. };
  198. struct clock_layout *sol_clock(SolParameters *params)
  199. {
  200. if (!params->ka_clock)
  201. {
  202. sol_log("clock account missing from transaction");
  203. sol_panic();
  204. }
  205. struct clock_layout *clock_data = (struct clock_layout *)params->ka_clock->data;
  206. return clock_data;
  207. }
  208. struct account_data_header
  209. {
  210. uint32_t magic;
  211. uint32_t returndata_len;
  212. uint32_t returndata_offset;
  213. uint32_t heap_offset;
  214. };
  215. // Simple heap for account data
  216. //
  217. // The heap is a doubly-linked list of objects, so we can merge with neighbours when we free.
  218. // We should use offsets rather than pointers as the layout in memory will be different each
  219. // time it is called.
  220. // We don't expect the account data to exceed 4GB so we use 32 bit offsets.
  221. // The account data can grow so the last entry always has length = 0 and offset_next = 0.
  222. struct chunk
  223. {
  224. uint32_t offset_next, offset_prev;
  225. uint32_t length;
  226. uint32_t allocated;
  227. };
  228. #define ROUND_UP(n, d) (((n) + (d)-1) & ~(d - 1))
  229. uint64_t account_data_alloc(SolAccountInfo *ai, uint32_t size, uint32_t *res)
  230. {
  231. void *data = ai->data;
  232. struct account_data_header *hdr = data;
  233. if (!size)
  234. {
  235. *res = 0;
  236. return 0;
  237. }
  238. uint32_t offset = hdr->heap_offset;
  239. uint32_t alloc_size = ROUND_UP(size, 8);
  240. uint32_t offset_prev = 0;
  241. for (;;)
  242. {
  243. struct chunk *chunk = data + offset;
  244. if (!chunk->allocated)
  245. {
  246. if (!chunk->length)
  247. {
  248. offset += sizeof(struct chunk);
  249. if (offset + alloc_size + sizeof(struct chunk) >= ai->data_len)
  250. {
  251. return ERROR_ACCOUNT_DATA_TOO_SMALL;
  252. }
  253. chunk->offset_next = offset + alloc_size;
  254. chunk->offset_prev = offset_prev;
  255. chunk->allocated = true;
  256. chunk->length = size;
  257. struct chunk *next = data + chunk->offset_next;
  258. next->offset_prev = offset - sizeof(struct chunk);
  259. next->length = 0;
  260. next->offset_next = 0;
  261. next->allocated = false;
  262. *res = offset;
  263. return 0;
  264. }
  265. else if (chunk->length < alloc_size)
  266. {
  267. // too small
  268. }
  269. else if (alloc_size + sizeof(struct chunk) + 8 > chunk->length)
  270. {
  271. // just right
  272. chunk->allocated = true;
  273. chunk->length = size;
  274. *res = offset + sizeof(struct chunk);
  275. return 0;
  276. }
  277. else
  278. {
  279. // too big, split
  280. uint32_t next = chunk->offset_next;
  281. uint32_t prev = offset;
  282. uint32_t next_offset = offset + sizeof(struct chunk) + alloc_size;
  283. chunk->offset_next = next_offset;
  284. chunk->length = size;
  285. chunk->allocated = true;
  286. chunk = data + next_offset;
  287. chunk->offset_prev = prev;
  288. chunk->offset_next = next;
  289. chunk->length = next - next_offset - sizeof(struct chunk);
  290. chunk->allocated = false;
  291. if (next)
  292. {
  293. struct chunk *chunk = data + next;
  294. chunk->offset_prev = next_offset;
  295. }
  296. *res = offset + sizeof(struct chunk);
  297. return 0;
  298. }
  299. }
  300. offset_prev = offset;
  301. offset = chunk->offset_next;
  302. }
  303. }
  304. uint32_t account_data_len(void *data, uint32_t offset)
  305. {
  306. // Nothing to do
  307. if (!offset)
  308. return 0;
  309. offset -= sizeof(struct chunk);
  310. struct chunk *chunk = data + offset;
  311. return chunk->length;
  312. }
  313. void account_data_free(void *data, uint32_t offset)
  314. {
  315. // Nothing to do
  316. if (!offset)
  317. return;
  318. offset -= sizeof(struct chunk);
  319. struct chunk *chunk = data + offset;
  320. chunk->allocated = false;
  321. // merge with previous chunk?
  322. if (chunk->offset_prev)
  323. {
  324. struct chunk *prev = data + chunk->offset_prev;
  325. if (!prev->allocated)
  326. {
  327. // merge
  328. offset = chunk->offset_prev;
  329. if (chunk->offset_next)
  330. {
  331. prev->length = chunk->offset_next - offset - sizeof(struct chunk);
  332. prev->offset_next = chunk->offset_next;
  333. struct chunk *next = data + chunk->offset_next;
  334. next->offset_prev = offset;
  335. }
  336. else
  337. {
  338. prev->offset_next = 0;
  339. prev->length = 0;
  340. }
  341. chunk = prev;
  342. }
  343. }
  344. // merge with next chunk?
  345. if (chunk->offset_next)
  346. {
  347. struct chunk *next = data + chunk->offset_next;
  348. if (!next->allocated)
  349. {
  350. // merge
  351. if (next->offset_next)
  352. {
  353. chunk->offset_next = next->offset_next;
  354. chunk->length = chunk->offset_next - offset - sizeof(struct chunk);
  355. struct chunk *next = data + chunk->offset_next;
  356. next->offset_prev = offset;
  357. }
  358. else
  359. {
  360. chunk->offset_next = 0;
  361. chunk->length = 0;
  362. }
  363. }
  364. }
  365. }
  366. uint64_t account_data_realloc(SolAccountInfo *ai, uint32_t offset, uint32_t size, uint32_t *res)
  367. {
  368. if (!size)
  369. {
  370. account_data_free(ai->data, offset);
  371. *res = 0;
  372. return 0;
  373. }
  374. if (!offset)
  375. {
  376. return account_data_alloc(ai, size, res);
  377. }
  378. void *data = ai->data;
  379. uint32_t chunk_offset = offset - sizeof(struct chunk);
  380. struct chunk *chunk = data + chunk_offset;
  381. struct chunk *next = data + chunk->offset_next;
  382. uint32_t existing_size = chunk->offset_next - offset;
  383. uint32_t alloc_size = ROUND_UP(size, 8);
  384. // 1. Is the existing chunk big enough
  385. if (size <= existing_size)
  386. {
  387. chunk->length = size;
  388. // can we free up some space
  389. if (existing_size >= alloc_size + sizeof(struct chunk) + 8)
  390. {
  391. uint32_t new_next_offset = offset + alloc_size;
  392. if (!next->allocated)
  393. {
  394. // merge with next chunk
  395. if (!next->offset_next)
  396. {
  397. // the trailing free chunk
  398. chunk->offset_next = new_next_offset;
  399. next = data + new_next_offset;
  400. next->offset_prev = chunk_offset;
  401. next->offset_next = 0;
  402. next->allocated = false;
  403. next->length = 0;
  404. }
  405. else
  406. {
  407. // merge with next chunk
  408. chunk->offset_next = new_next_offset;
  409. uint32_t offset_next_next = next->offset_next;
  410. next = data + new_next_offset;
  411. next->offset_prev = chunk_offset;
  412. next->offset_next = offset_next_next;
  413. next->allocated = false;
  414. next->length = offset_next_next - new_next_offset - sizeof(struct chunk);
  415. next = data + offset_next_next;
  416. next->offset_prev = new_next_offset;
  417. }
  418. }
  419. else
  420. {
  421. // insert a new chunk
  422. uint32_t offset_next_next = chunk->offset_next;
  423. chunk->offset_next = new_next_offset;
  424. next = data + new_next_offset;
  425. next->offset_prev = chunk_offset;
  426. next->offset_next = offset_next_next;
  427. next->allocated = false;
  428. next->length = offset_next_next - new_next_offset - sizeof(struct chunk);
  429. next = data + offset_next_next;
  430. next->offset_prev = new_next_offset;
  431. }
  432. }
  433. *res = offset;
  434. return 0;
  435. }
  436. // 2. Can we use the next chunk to expand our chunk to fit
  437. // Note because we always merge neighbours, free chunks do not have free
  438. // neighbours.
  439. if (!next->allocated)
  440. {
  441. if (next->offset_next)
  442. {
  443. uint32_t merged_size = next->offset_next - offset;
  444. if (size < merged_size)
  445. {
  446. if (merged_size - alloc_size < 8 + sizeof(struct chunk))
  447. {
  448. // merge the two chunks
  449. chunk->offset_next = next->offset_next;
  450. chunk->length = size;
  451. next = data + chunk->offset_next;
  452. next->offset_prev = chunk_offset;
  453. }
  454. else
  455. {
  456. // expand our chunk to fit and shrink the next chunk
  457. uint32_t offset_next = offset + alloc_size;
  458. uint32_t offset_next_next = next->offset_next;
  459. chunk->offset_next = offset_next;
  460. chunk->length = size;
  461. next = data + offset_next;
  462. next->offset_prev = chunk_offset;
  463. next->offset_next = offset_next_next;
  464. next->length = offset_next_next - offset_next - sizeof(struct chunk);
  465. next->allocated = false;
  466. next = data + offset_next_next;
  467. next->offset_prev = offset_next;
  468. }
  469. *res = offset;
  470. return 0;
  471. }
  472. }
  473. else
  474. {
  475. if (offset + alloc_size + sizeof(struct chunk) < ai->data_len)
  476. {
  477. chunk->offset_next = offset + alloc_size;
  478. chunk->length = size;
  479. next = data + chunk->offset_next;
  480. next->offset_prev = chunk_offset;
  481. next->offset_next = 0;
  482. next->allocated = false;
  483. next->length = 0;
  484. *res = offset;
  485. return 0;
  486. }
  487. }
  488. }
  489. uint32_t old_length = account_data_len(data, offset);
  490. uint32_t new_offset;
  491. uint64_t rc = account_data_alloc(ai, size, &new_offset);
  492. if (rc)
  493. return rc;
  494. __memcpy(data + new_offset, data + offset, old_length);
  495. account_data_free(data, offset);
  496. *res = new_offset;
  497. return 0;
  498. }
  499. #ifdef TEST
  500. // To run the test:
  501. // clang -DTEST -DSOL_TEST -O3 -Wall solana.c stdlib.c -o test && ./test
  502. #include <assert.h>
  503. #include <string.h>
  504. #include <stdlib.h>
  505. #include <time.h>
  506. void validate_heap(void *data, uint32_t offs[100], uint32_t lens[100])
  507. {
  508. struct account_data_header *hdr = data;
  509. uint32_t offset = hdr->heap_offset;
  510. uint32_t last_offset = 0;
  511. for (;;)
  512. {
  513. struct chunk *chk = data + offset;
  514. // printf("chunk: offset:%x prev:%x next:%x length:%x allocated:%d\n", offset, chk->offset_prev, chk->offset_next, chk->length, chk->allocated);
  515. if (chk->length == 0 || chk->offset_next == 0)
  516. {
  517. assert(chk->length == 0 && chk->offset_next == 0 && chk->offset_prev == last_offset);
  518. // printf("last object at 0x%08x\n", offset);
  519. return;
  520. }
  521. assert(chk->offset_prev == last_offset && chk->length != 0);
  522. // printf("found object length %x at 0x%08lx allocated %d\n", chk->length, offset + sizeof(struct chunk), chk->allocated);
  523. assert(chk->offset_next - offset - sizeof(struct chunk) >= chk->length);
  524. if (chk->allocated)
  525. {
  526. bool found = false;
  527. uint32_t off = offset + sizeof(struct chunk);
  528. for (int i = 0; i < 100; i++)
  529. {
  530. if (offs[i] == off)
  531. {
  532. assert(!found);
  533. found = true;
  534. uint8_t *mem = data + off;
  535. for (int x = 0; x < lens[i]; x++)
  536. {
  537. assert(mem[x] == i);
  538. }
  539. }
  540. }
  541. assert(found);
  542. }
  543. else
  544. {
  545. // make sure we do not have this in our allocated list
  546. uint32_t off = offset + sizeof(struct chunk);
  547. for (int i = 0; i < 100; i++)
  548. {
  549. assert(offs[i] != off);
  550. }
  551. }
  552. last_offset = offset;
  553. offset = chk->offset_next;
  554. }
  555. }
  556. int main()
  557. {
  558. uint8_t data[0x10000];
  559. SolAccountInfo ai;
  560. ai.data = data;
  561. ai.data_len = sizeof(data);
  562. uint32_t offs[100], lens[100];
  563. uint32_t allocs = 0;
  564. memset(data, 0, sizeof(data));
  565. struct account_data_header *hdr = data;
  566. hdr->magic = 0x41424344;
  567. hdr->heap_offset = 0x20;
  568. memset(offs, 0, sizeof(offs));
  569. int seed = time(NULL);
  570. printf("seed: %d\n", seed);
  571. srand(seed);
  572. uint32_t new_offset;
  573. uint64_t status;
  574. for (;;)
  575. {
  576. validate_heap(data, offs, lens);
  577. int n = rand() % 100;
  578. if (offs[n] == 0)
  579. {
  580. // printf("STEP: alloc %d\n", n);
  581. status = account_data_alloc(&ai, 100, &new_offset);
  582. assert(status == 0);
  583. offs[n] = new_offset;
  584. memset(data + offs[n], n, 100);
  585. lens[n] = 100;
  586. }
  587. else if (rand() % 2)
  588. {
  589. // printf("STEP: free %d (0x%x)\n", n, offs[n]);
  590. account_data_free(ai.data, offs[n]);
  591. offs[n] = 0;
  592. }
  593. else
  594. {
  595. // printf("STEP: realloc %d (0x%x)\n", n, offs[n]);
  596. int size = (rand() % 200) + 10;
  597. int old_size = account_data_len(ai.data, offs[n]);
  598. status = account_data_realloc(&ai, offs[n], size, &new_offset);
  599. assert(status == 0);
  600. offs[n] = new_offset;
  601. if (size > old_size)
  602. memset(data + offs[n] + old_size, n, size - old_size);
  603. lens[n] = size;
  604. }
  605. if (time(NULL) - seed > 120)
  606. {
  607. printf("No error found after running for two minutes\n");
  608. break;
  609. }
  610. }
  611. }
  612. void sol_panic_(const char *s, uint64_t len, uint64_t line, uint64_t column)
  613. {
  614. printf("panic: %s line %lld", s, line);
  615. }
  616. #endif