solana.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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. #endif
  47. uint64_t address_hash(uint8_t data[32])
  48. {
  49. uint64_t hash = 0;
  50. uint32_t i;
  51. for (i = 0; i < 32; i++)
  52. {
  53. hash += data[i];
  54. }
  55. return hash;
  56. }
  57. bool address_equal(void *a, void *b)
  58. {
  59. uint64_t *left = a;
  60. uint64_t *right = b;
  61. for (uint32_t i = 0; i < 4; i++)
  62. {
  63. if (left[i] != right[i])
  64. {
  65. return false;
  66. }
  67. }
  68. return true;
  69. }
  70. struct ed25519_instruction_sig
  71. {
  72. uint16_t signature_offset;
  73. uint16_t signature_instruction_index;
  74. uint16_t public_key_offset;
  75. uint16_t public_key_instruction_index;
  76. uint16_t message_offset;
  77. uint16_t message_size;
  78. uint16_t message_instruction_index;
  79. uint8_t public_key[SIZE_PUBKEY];
  80. uint8_t signature[64];
  81. uint8_t message[0];
  82. };
  83. struct ed25519_instruction
  84. {
  85. uint8_t num_signatures;
  86. uint8_t padding;
  87. struct ed25519_instruction_sig sig[0];
  88. };
  89. uint64_t signature_verify(uint8_t *public_key, struct vector *message, struct vector *signature, SolParameters *params)
  90. {
  91. if (params->ka_instructions)
  92. {
  93. uint16_t *data = (uint16_t *)params->ka_instructions->data;
  94. uint64_t instr_count = data[0];
  95. // for each instruction
  96. for (uint64_t instr_no = 0; instr_no < instr_count; instr_no++)
  97. {
  98. uint8_t *instr = params->ka_instructions->data + data[1 + instr_no];
  99. // step over the accounts
  100. uint64_t accounts = *((uint16_t *)instr);
  101. instr += accounts * 33 + 2;
  102. if (sol_memcmp(&ed25519_address, instr, sizeof(ed25519_address)))
  103. {
  104. continue;
  105. }
  106. // step over program_id and length
  107. instr += 2 + 32;
  108. struct ed25519_instruction *ed25519 = (struct ed25519_instruction *)instr;
  109. for (uint64_t sig_no = 0; sig_no < ed25519->num_signatures; sig_no++)
  110. {
  111. struct ed25519_instruction_sig *sig = &ed25519->sig[sig_no];
  112. if (sig->public_key_instruction_index != instr_no || sig->signature_instruction_index != instr_no ||
  113. sig->message_instruction_index != instr_no)
  114. continue;
  115. if (sol_memcmp(public_key, instr + sig->public_key_offset, SIZE_PUBKEY))
  116. {
  117. continue;
  118. }
  119. if (sol_memcmp(signature->data, instr + sig->signature_offset, 64))
  120. {
  121. continue;
  122. }
  123. if (sig->message_size != message->len)
  124. {
  125. continue;
  126. }
  127. if (sol_memcmp(message->data, instr + sig->message_offset, message->len))
  128. {
  129. continue;
  130. }
  131. return 0;
  132. }
  133. }
  134. }
  135. sol_log("could not find verified signature");
  136. return 1;
  137. }
  138. struct clock_layout
  139. {
  140. uint64_t slot;
  141. uint64_t epoch_start_timestamp;
  142. uint64_t epoch;
  143. uint64_t leader_schedule_epoch;
  144. uint64_t unix_timestamp;
  145. };
  146. struct clock_layout *sol_clock(SolParameters *params)
  147. {
  148. if (!params->ka_clock)
  149. {
  150. sol_log("clock account missing from transaction");
  151. sol_panic();
  152. }
  153. struct clock_layout *clock_data = (struct clock_layout *)params->ka_clock->data;
  154. return clock_data;
  155. }
  156. struct account_data_header
  157. {
  158. uint32_t magic;
  159. uint32_t returndata_len;
  160. uint32_t returndata_offset;
  161. uint32_t heap_offset;
  162. };
  163. // Simple heap for account data
  164. //
  165. // The heap is a doubly-linked list of objects, so we can merge with neighbours when we free.
  166. // We should use offsets rather than pointers as the layout in memory will be different each
  167. // time it is called.
  168. // We don't expect the account data to exceed 4GB so we use 32 bit offsets.
  169. // The account data can grow so the last entry always has length = 0 and offset_next = 0.
  170. struct chunk
  171. {
  172. uint32_t offset_next, offset_prev;
  173. uint32_t length;
  174. uint32_t allocated;
  175. };
  176. #define ROUND_UP(n, d) (((n) + (d)-1) & ~(d - 1))
  177. uint64_t account_data_alloc(SolAccountInfo *ai, uint32_t size, uint32_t *res)
  178. {
  179. void *data = ai->data;
  180. struct account_data_header *hdr = data;
  181. if (!size)
  182. {
  183. *res = 0;
  184. return 0;
  185. }
  186. uint32_t offset = hdr->heap_offset;
  187. uint32_t alloc_size = ROUND_UP(size, 8);
  188. uint32_t offset_prev = 0;
  189. for (;;)
  190. {
  191. struct chunk *chunk = data + offset;
  192. if (!chunk->allocated)
  193. {
  194. if (!chunk->length)
  195. {
  196. offset += sizeof(struct chunk);
  197. if (offset + alloc_size + sizeof(struct chunk) >= ai->data_len)
  198. {
  199. return ERROR_ACCOUNT_DATA_TOO_SMALL;
  200. }
  201. chunk->offset_next = offset + alloc_size;
  202. chunk->offset_prev = offset_prev;
  203. chunk->allocated = true;
  204. chunk->length = size;
  205. struct chunk *next = data + chunk->offset_next;
  206. next->offset_prev = offset - sizeof(struct chunk);
  207. next->length = 0;
  208. next->offset_next = 0;
  209. next->allocated = false;
  210. *res = offset;
  211. return 0;
  212. }
  213. else if (chunk->length < alloc_size)
  214. {
  215. // too small
  216. }
  217. else if (alloc_size + sizeof(struct chunk) + 8 > chunk->length)
  218. {
  219. // just right
  220. chunk->allocated = true;
  221. chunk->length = size;
  222. *res = offset + sizeof(struct chunk);
  223. return 0;
  224. }
  225. else
  226. {
  227. // too big, split
  228. uint32_t next = chunk->offset_next;
  229. uint32_t prev = offset;
  230. uint32_t next_offset = offset + sizeof(struct chunk) + alloc_size;
  231. chunk->offset_next = next_offset;
  232. chunk->length = size;
  233. chunk->allocated = true;
  234. chunk = data + next_offset;
  235. chunk->offset_prev = prev;
  236. chunk->offset_next = next;
  237. chunk->length = next - next_offset - sizeof(struct chunk);
  238. chunk->allocated = false;
  239. if (next)
  240. {
  241. struct chunk *chunk = data + next;
  242. chunk->offset_prev = next_offset;
  243. }
  244. *res = offset + sizeof(struct chunk);
  245. return 0;
  246. }
  247. }
  248. offset_prev = offset;
  249. offset = chunk->offset_next;
  250. }
  251. }
  252. uint32_t account_data_len(void *data, uint32_t offset)
  253. {
  254. // Nothing to do
  255. if (!offset)
  256. return 0;
  257. offset -= sizeof(struct chunk);
  258. struct chunk *chunk = data + offset;
  259. return chunk->length;
  260. }
  261. void account_data_free(void *data, uint32_t offset)
  262. {
  263. // Nothing to do
  264. if (!offset)
  265. return;
  266. offset -= sizeof(struct chunk);
  267. struct chunk *chunk = data + offset;
  268. chunk->allocated = false;
  269. // merge with previous chunk?
  270. if (chunk->offset_prev)
  271. {
  272. struct chunk *prev = data + chunk->offset_prev;
  273. if (!prev->allocated)
  274. {
  275. // merge
  276. offset = chunk->offset_prev;
  277. if (chunk->offset_next)
  278. {
  279. prev->length = chunk->offset_next - offset - sizeof(struct chunk);
  280. prev->offset_next = chunk->offset_next;
  281. struct chunk *next = data + chunk->offset_next;
  282. next->offset_prev = offset;
  283. }
  284. else
  285. {
  286. prev->offset_next = 0;
  287. prev->length = 0;
  288. }
  289. chunk = prev;
  290. }
  291. }
  292. // merge with next chunk?
  293. if (chunk->offset_next)
  294. {
  295. struct chunk *next = data + chunk->offset_next;
  296. if (!next->allocated)
  297. {
  298. // merge
  299. if (next->offset_next)
  300. {
  301. chunk->offset_next = next->offset_next;
  302. chunk->length = chunk->offset_next - offset - sizeof(struct chunk);
  303. struct chunk *next = data + chunk->offset_next;
  304. next->offset_prev = offset;
  305. }
  306. else
  307. {
  308. chunk->offset_next = 0;
  309. chunk->length = 0;
  310. }
  311. }
  312. }
  313. }
  314. uint64_t account_data_realloc(SolAccountInfo *ai, uint32_t offset, uint32_t size, uint32_t *res)
  315. {
  316. if (!size)
  317. {
  318. account_data_free(ai->data, offset);
  319. *res = 0;
  320. return 0;
  321. }
  322. if (!offset)
  323. {
  324. return account_data_alloc(ai, size, res);
  325. }
  326. void *data = ai->data;
  327. uint32_t chunk_offset = offset - sizeof(struct chunk);
  328. struct chunk *chunk = data + chunk_offset;
  329. struct chunk *next = data + chunk->offset_next;
  330. uint32_t existing_size = chunk->offset_next - offset;
  331. uint32_t alloc_size = ROUND_UP(size, 8);
  332. // 1. Is the existing chunk big enough
  333. if (size <= existing_size)
  334. {
  335. chunk->length = size;
  336. // can we free up some space
  337. if (existing_size >= alloc_size + sizeof(struct chunk) + 8)
  338. {
  339. uint32_t new_next_offset = offset + alloc_size;
  340. if (!next->allocated)
  341. {
  342. // merge with next chunk
  343. if (!next->offset_next)
  344. {
  345. // the trailing free chunk
  346. chunk->offset_next = new_next_offset;
  347. next = data + new_next_offset;
  348. next->offset_prev = chunk_offset;
  349. next->offset_next = 0;
  350. next->allocated = false;
  351. next->length = 0;
  352. }
  353. else
  354. {
  355. // merge with next chunk
  356. chunk->offset_next = new_next_offset;
  357. uint32_t offset_next_next = next->offset_next;
  358. next = data + new_next_offset;
  359. next->offset_prev = chunk_offset;
  360. next->offset_next = offset_next_next;
  361. next->allocated = false;
  362. next->length = offset_next_next - new_next_offset - sizeof(struct chunk);
  363. next = data + offset_next_next;
  364. next->offset_prev = new_next_offset;
  365. }
  366. }
  367. else
  368. {
  369. // insert a new chunk
  370. uint32_t offset_next_next = chunk->offset_next;
  371. chunk->offset_next = new_next_offset;
  372. next = data + new_next_offset;
  373. next->offset_prev = chunk_offset;
  374. next->offset_next = offset_next_next;
  375. next->allocated = false;
  376. next->length = offset_next_next - new_next_offset - sizeof(struct chunk);
  377. next = data + offset_next_next;
  378. next->offset_prev = new_next_offset;
  379. }
  380. }
  381. *res = offset;
  382. return 0;
  383. }
  384. // 2. Can we use the next chunk to expand our chunk to fit
  385. // Note because we always merge neighbours, free chunks do not have free
  386. // neighbours.
  387. if (!next->allocated)
  388. {
  389. if (next->offset_next)
  390. {
  391. uint32_t merged_size = next->offset_next - offset;
  392. if (size < merged_size)
  393. {
  394. if (merged_size - alloc_size < 8 + sizeof(struct chunk))
  395. {
  396. // merge the two chunks
  397. chunk->offset_next = next->offset_next;
  398. chunk->length = size;
  399. next = data + chunk->offset_next;
  400. next->offset_prev = chunk_offset;
  401. }
  402. else
  403. {
  404. // expand our chunk to fit and shrink the next chunk
  405. uint32_t offset_next = offset + alloc_size;
  406. uint32_t offset_next_next = next->offset_next;
  407. chunk->offset_next = offset_next;
  408. chunk->length = size;
  409. next = data + offset_next;
  410. next->offset_prev = chunk_offset;
  411. next->offset_next = offset_next_next;
  412. next->length = offset_next_next - offset_next - sizeof(struct chunk);
  413. next->allocated = false;
  414. next = data + offset_next_next;
  415. next->offset_prev = offset_next;
  416. }
  417. *res = offset;
  418. return 0;
  419. }
  420. }
  421. else
  422. {
  423. if (offset + alloc_size + sizeof(struct chunk) < ai->data_len)
  424. {
  425. chunk->offset_next = offset + alloc_size;
  426. chunk->length = size;
  427. next = data + chunk->offset_next;
  428. next->offset_prev = chunk_offset;
  429. next->offset_next = 0;
  430. next->allocated = false;
  431. next->length = 0;
  432. *res = offset;
  433. return 0;
  434. }
  435. }
  436. }
  437. uint32_t old_length = account_data_len(data, offset);
  438. uint32_t new_offset;
  439. uint64_t rc = account_data_alloc(ai, size, &new_offset);
  440. if (rc)
  441. return rc;
  442. __memcpy(data + new_offset, data + offset, old_length);
  443. account_data_free(data, offset);
  444. *res = new_offset;
  445. return 0;
  446. }
  447. #ifdef TEST
  448. // To run the test:
  449. // clang -DTEST -DSOL_TEST -O3 -Wall solana.c stdlib.c -o test && ./test
  450. #include <assert.h>
  451. #include <string.h>
  452. #include <stdlib.h>
  453. #include <time.h>
  454. void validate_heap(void *data, uint32_t offs[100], uint32_t lens[100])
  455. {
  456. struct account_data_header *hdr = data;
  457. uint32_t offset = hdr->heap_offset;
  458. uint32_t last_offset = 0;
  459. for (;;)
  460. {
  461. struct chunk *chk = data + offset;
  462. // printf("chunk: offset:%x prev:%x next:%x length:%x allocated:%d\n", offset, chk->offset_prev, chk->offset_next, chk->length, chk->allocated);
  463. if (chk->length == 0 || chk->offset_next == 0)
  464. {
  465. assert(chk->length == 0 && chk->offset_next == 0 && chk->offset_prev == last_offset);
  466. // printf("last object at 0x%08x\n", offset);
  467. return;
  468. }
  469. assert(chk->offset_prev == last_offset && chk->length != 0);
  470. // printf("found object length %x at 0x%08lx allocated %d\n", chk->length, offset + sizeof(struct chunk), chk->allocated);
  471. assert(chk->offset_next - offset - sizeof(struct chunk) >= chk->length);
  472. if (chk->allocated)
  473. {
  474. bool found = false;
  475. uint32_t off = offset + sizeof(struct chunk);
  476. for (int i = 0; i < 100; i++)
  477. {
  478. if (offs[i] == off)
  479. {
  480. assert(!found);
  481. found = true;
  482. uint8_t *mem = data + off;
  483. for (int x = 0; x < lens[i]; x++)
  484. {
  485. assert(mem[x] == i);
  486. }
  487. }
  488. }
  489. assert(found);
  490. }
  491. else
  492. {
  493. // make sure we do not have this in our allocated list
  494. uint32_t off = offset + sizeof(struct chunk);
  495. for (int i = 0; i < 100; i++)
  496. {
  497. assert(offs[i] != off);
  498. }
  499. }
  500. last_offset = offset;
  501. offset = chk->offset_next;
  502. }
  503. }
  504. int main()
  505. {
  506. uint8_t data[0x10000];
  507. SolAccountInfo ai;
  508. ai.data = data;
  509. ai.data_len = sizeof(data);
  510. uint32_t offs[100], lens[100];
  511. uint32_t allocs = 0;
  512. memset(data, 0, sizeof(data));
  513. struct account_data_header *hdr = data;
  514. hdr->magic = 0x41424344;
  515. hdr->heap_offset = 0x20;
  516. memset(offs, 0, sizeof(offs));
  517. int seed = time(NULL);
  518. printf("seed: %d\n", seed);
  519. srand(seed);
  520. uint32_t new_offset;
  521. uint64_t status;
  522. for (;;)
  523. {
  524. validate_heap(data, offs, lens);
  525. int n = rand() % 100;
  526. if (offs[n] == 0)
  527. {
  528. // printf("STEP: alloc %d\n", n);
  529. status = account_data_alloc(&ai, 100, &new_offset);
  530. assert(status == 0);
  531. offs[n] = new_offset;
  532. memset(data + offs[n], n, 100);
  533. lens[n] = 100;
  534. }
  535. else if (rand() % 2)
  536. {
  537. // printf("STEP: free %d (0x%x)\n", n, offs[n]);
  538. account_data_free(ai.data, offs[n]);
  539. offs[n] = 0;
  540. }
  541. else
  542. {
  543. // printf("STEP: realloc %d (0x%x)\n", n, offs[n]);
  544. int size = (rand() % 200) + 10;
  545. int old_size = account_data_len(ai.data, offs[n]);
  546. status = account_data_realloc(&ai, offs[n], size, &new_offset);
  547. assert(status == 0);
  548. offs[n] = new_offset;
  549. if (size > old_size)
  550. memset(data + offs[n] + old_size, n, size - old_size);
  551. lens[n] = size;
  552. }
  553. if (time(NULL) - seed > 120)
  554. {
  555. printf("No error found after running for two minutes\n");
  556. break;
  557. }
  558. }
  559. }
  560. void sol_panic_(const char *s, uint64_t len, uint64_t line, uint64_t column)
  561. {
  562. printf("panic: %s line %lld", s, line);
  563. }
  564. #endif