solana.c 23 KB

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