solana.c 25 KB

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