solana.c 23 KB

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