solana.c 23 KB

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