stdlib.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. // clang --target=wasm32 -c -emit-llvm -O3 -ffreestanding -fno-builtin -Wall stdlib.c
  2. #include <stdint.h>
  3. #include <stddef.h>
  4. #include <stdbool.h>
  5. #include "stdlib.h"
  6. /*
  7. */
  8. void __memset8(void *_dest, uint64_t val, size_t length)
  9. {
  10. uint64_t *dest = _dest;
  11. do
  12. {
  13. *dest++ = val;
  14. } while (--length);
  15. }
  16. void __memset(uint8_t *dest, uint8_t val, size_t length)
  17. {
  18. do
  19. {
  20. *dest++ = val;
  21. } while (--length);
  22. }
  23. /*
  24. * Our memcpy can only deal with multiples of 8 bytes. This is enough for
  25. * simple allocator below.
  26. */
  27. void __memcpy8(void *_dest, void *_src, size_t length)
  28. {
  29. uint64_t *dest = _dest;
  30. uint64_t *src = _src;
  31. do
  32. {
  33. *dest++ = *src++;
  34. } while (--length);
  35. }
  36. void __memcpy(uint8_t *dest, uint8_t *src, size_t length)
  37. {
  38. do
  39. {
  40. *dest++ = *src++;
  41. } while (--length);
  42. }
  43. /*
  44. * Fast-ish clear, 8 bytes at a time.
  45. */
  46. void __bzero8(void *_dest, size_t length)
  47. {
  48. uint64_t *dest = _dest;
  49. do
  50. *dest++ = 0;
  51. while (--length);
  52. }
  53. /*
  54. * Fast-ish set, 8 bytes at a time.
  55. */
  56. void __bset8(void *_dest, size_t length)
  57. {
  58. int64_t *dest = _dest;
  59. do
  60. *dest++ = -1;
  61. while (--length);
  62. }
  63. /*
  64. There are many tradeoffs in heaps. I think for Solidity, we want:
  65. - small code size to reduce code length
  66. - not many malloc objects
  67. - not much memory
  68. So I think we should avoid fragmentation by neighbour merging. The most
  69. costly is walking the doubly linked list looking for free space.
  70. */
  71. struct chunk
  72. {
  73. struct chunk *next, *prev;
  74. size_t length;
  75. bool allocated;
  76. };
  77. void __init_heap()
  78. {
  79. struct chunk *first = (struct chunk *)0x10000;
  80. first->next = first->prev = NULL;
  81. first->allocated = false;
  82. first->length = (size_t)(__builtin_wasm_memory_size(0) -
  83. (size_t)first - sizeof(struct chunk));
  84. }
  85. void __attribute__((noinline)) __free(void *m)
  86. {
  87. struct chunk *cur = m;
  88. cur--;
  89. if (m)
  90. {
  91. cur->allocated = false;
  92. struct chunk *next = cur->next;
  93. if (next && !next->allocated)
  94. {
  95. // merge with next
  96. if ((cur->next = next->next) != NULL)
  97. cur->next->prev = cur;
  98. cur->length += next->length + sizeof(struct chunk);
  99. next = cur->next;
  100. }
  101. struct chunk *prev = cur->prev;
  102. if (prev && !prev->allocated)
  103. {
  104. // merge with previous
  105. prev->next = next;
  106. next->prev = prev;
  107. prev->length += cur->length + sizeof(struct chunk);
  108. }
  109. }
  110. }
  111. static void shrink_chunk(struct chunk *cur, size_t size)
  112. {
  113. // round up to nearest 8 bytes
  114. size = (size + 7) & ~7;
  115. if (cur->length - size >= (8 + sizeof(struct chunk)))
  116. {
  117. // split and return
  118. void *data = (cur + 1);
  119. struct chunk *new = data + size;
  120. if ((new->next = cur->next) != NULL)
  121. new->next->prev = new;
  122. cur->next = new;
  123. new->prev = cur;
  124. new->allocated = false;
  125. new->length = cur->length - size - sizeof(struct chunk);
  126. cur->length = size;
  127. }
  128. }
  129. void *__attribute__((noinline)) __malloc(size_t size)
  130. {
  131. struct chunk *cur = (struct chunk *)0x10000;
  132. while (cur && (cur->allocated || size > cur->length))
  133. cur = cur->next;
  134. if (cur)
  135. {
  136. shrink_chunk(cur, size);
  137. cur->allocated = true;
  138. return ++cur;
  139. }
  140. else
  141. {
  142. // go bang
  143. __builtin_unreachable();
  144. }
  145. }
  146. void *__realloc(void *m, size_t size)
  147. {
  148. struct chunk *cur = m;
  149. cur--;
  150. struct chunk *next = cur->next;
  151. if (next && !next->allocated && size <= (cur->length + next->length + sizeof(struct chunk)))
  152. {
  153. // merge with next
  154. cur->next = next->next;
  155. cur->next->prev = cur;
  156. cur->length += next->length + sizeof(struct chunk);
  157. // resplit ..
  158. shrink_chunk(cur, size);
  159. return m;
  160. }
  161. else
  162. {
  163. void *n = __malloc(size);
  164. __memcpy8(n, m, size / 8);
  165. __free(m);
  166. return n;
  167. }
  168. }
  169. // This function is used for abi decoding integers.
  170. // ABI encoding is big endian, and can have integers of 8 to 256 bits
  171. // (1 to 32 bytes). This function copies length bytes and reverses the
  172. // order since wasm is little endian.
  173. void __be32toleN(uint8_t *from, uint8_t *to, uint32_t length)
  174. {
  175. from += 31;
  176. do
  177. {
  178. *to++ = *from--;
  179. } while (--length);
  180. }
  181. void __beNtoleN(uint8_t *from, uint8_t *to, uint32_t length)
  182. {
  183. from += length;
  184. do
  185. {
  186. *to++ = *--from;
  187. } while (--length);
  188. }
  189. // This function is for used for abi encoding integers
  190. // ABI encoding is big endian.
  191. void __leNtobe32(uint8_t *from, uint8_t *to, uint32_t length)
  192. {
  193. to += 31;
  194. do
  195. {
  196. *to-- = *from++;
  197. } while (--length);
  198. }
  199. void __leNtobeN(uint8_t *from, uint8_t *to, uint32_t length)
  200. {
  201. to += length;
  202. do
  203. {
  204. *--to = *from++;
  205. } while (--length);
  206. }
  207. /*
  208. In wasm, the instruction for multiplying two 64 bit values results in a 64 bit value. In
  209. other words, the result is truncated. The largest values we can multiply without truncation
  210. is 32 bit (by casting to 64 bit and doing a 64 bit multiplication). So, we divvy the work
  211. up into a 32 bit multiplications.
  212. No overflow checking is done.
  213. 0 0 0 r5 r4 r3 r2 r1
  214. 0 0 0 0 l4 l3 l2 l1 *
  215. ------------------------------------------------------------
  216. 0 0 0 r5*l1 r4*l1 r3*l1 r2*l1 r1*l1
  217. 0 0 r5*l2 r4*l2 r3*l2 r2*l2 r1*l2 0
  218. 0 r5*l3 r4*l3 r3*l3 r2*l3 r1*l3 0 0
  219. r5*l4 r4*l4 r3*l4 r2*l4 r1*l4 0 0 0 +
  220. ------------------------------------------------------------
  221. */
  222. void __mul32(uint32_t left[], uint32_t right[], uint32_t out[], int len)
  223. {
  224. uint64_t val1 = 0, carry = 0;
  225. int left_len = len, right_len = len;
  226. while (left_len > 0 && !left[left_len - 1])
  227. left_len--;
  228. while (right_len > 0 && !right[right_len - 1])
  229. right_len--;
  230. int right_start = 0, right_end = 0;
  231. int left_start = 0;
  232. for (int l = 0; l < len; l++)
  233. {
  234. int i = 0;
  235. if (l >= left_len)
  236. right_start++;
  237. if (l >= right_len)
  238. left_start++;
  239. if (right_end < right_len)
  240. right_end++;
  241. for (int r = right_end - 1; r >= right_start; r--)
  242. {
  243. uint64_t m = (uint64_t)left[left_start + i] * (uint64_t)right[r];
  244. i++;
  245. if (__builtin_add_overflow(val1, m, &val1))
  246. carry += 0x100000000;
  247. }
  248. out[l] = val1;
  249. val1 = (val1 >> 32) | carry;
  250. carry = 0;
  251. }
  252. }
  253. // Some compiler runtime builtins we need.
  254. // 128 bit shift left.
  255. typedef union {
  256. __uint128_t all;
  257. struct
  258. {
  259. uint64_t low;
  260. uint64_t high;
  261. };
  262. } two64;
  263. // This assumes r >= 0 && r <= 127
  264. __uint128_t __ashlti3(__uint128_t val, int r)
  265. {
  266. two64 in;
  267. two64 result;
  268. in.all = val;
  269. if (r == 0)
  270. {
  271. // nothing to do
  272. result.all = in.all;
  273. }
  274. else if (r & 64)
  275. {
  276. // Shift more than or equal 64
  277. result.low = 0;
  278. result.high = in.low << (r & 63);
  279. }
  280. else
  281. {
  282. // Shift less than 64
  283. result.low = in.low << r;
  284. result.high = (in.high << r) | (in.low >> (64 - r));
  285. }
  286. return result.all;
  287. }
  288. // This assumes r >= 0 && r <= 127
  289. __uint128_t __lshrti3(__uint128_t val, int r)
  290. {
  291. two64 in;
  292. two64 result;
  293. in.all = val;
  294. if (r == 0)
  295. {
  296. // nothing to do
  297. result.all = in.all;
  298. }
  299. else if (r & 64)
  300. {
  301. // Shift more than or equal 64
  302. result.low = in.high >> (r & 63);
  303. result.high = 0;
  304. }
  305. else
  306. {
  307. // Shift less than 64
  308. result.low = (in.low >> r) | (in.high << (64 - r));
  309. result.high = in.high >> r;
  310. }
  311. return result.all;
  312. }
  313. // sabre wants the storage keys as a hex string. Convert the uint256 pointed
  314. // to be by v into a hex string
  315. char *__u256ptohex(uint8_t *v, char *str)
  316. {
  317. // the uint256 will be stored little endian so fill it in reverse
  318. str += 63;
  319. for (int i = 0; i < 32; i++)
  320. {
  321. uint8_t l = (v[i] & 0x0f);
  322. *str-- = l > 9 ? l + 'a' : '0' + l;
  323. uint8_t h = (v[i] >> 4);
  324. *str-- = h > 9 ? h + 'a' : '0' + h;
  325. }
  326. return str;
  327. }
  328. // Create a new vector. If initial is -1 then clear the data. This is done since a null pointer valid in wasm
  329. struct vector *vector_new(uint32_t members, uint32_t size, uint8_t *initial)
  330. {
  331. struct vector *v;
  332. size_t size_array = members * size;
  333. v = __malloc(sizeof(*v) + size_array);
  334. v->len = members;
  335. v->size = members;
  336. uint8_t *data = v->data;
  337. if ((int)initial != -1)
  338. {
  339. do
  340. {
  341. *data++ = *initial++;
  342. } while (size_array--);
  343. }
  344. else
  345. {
  346. do
  347. {
  348. *data++ = 0;
  349. } while (size_array--);
  350. }
  351. return v;
  352. }
  353. bool memcmp(uint8_t *left, uint32_t left_len, uint8_t *right, uint32_t right_len)
  354. {
  355. if (left_len != right_len)
  356. return false;
  357. while (left_len--)
  358. {
  359. if (*left++ != *right++)
  360. return false;
  361. }
  362. return true;
  363. }
  364. struct vector *concat(uint8_t *left, uint32_t left_len, uint8_t *right, uint32_t right_len)
  365. {
  366. size_t size_array = left_len + right_len;
  367. struct vector *v = __malloc(sizeof(*v) + size_array);
  368. v->len = size_array;
  369. v->size = size_array;
  370. uint8_t *data = v->data;
  371. while (left_len--)
  372. {
  373. *data++ = *left++;
  374. }
  375. while (right_len--)
  376. {
  377. *data++ = *right++;
  378. }
  379. return v;
  380. }
  381. // Encode an 32 bit integer as as scale compact integer
  382. // https://substrate.dev/docs/en/conceptual/core/codec#vectors-lists-series-sets
  383. uint8_t *compact_encode_u32(uint8_t *dest, uint32_t val)
  384. {
  385. if (val < 64)
  386. {
  387. *dest++ = val << 2;
  388. }
  389. else if (val < 0x4000)
  390. {
  391. *((uint16_t *)dest) = (val << 2) | 1;
  392. dest += 2;
  393. }
  394. else if (val < 0x40000000)
  395. {
  396. *((uint32_t *)dest) = (val << 2) | 2;
  397. dest += 4;
  398. }
  399. else
  400. {
  401. *dest++ = 3;
  402. *((uint32_t *)dest) = val;
  403. dest += 4;
  404. }
  405. return dest;
  406. }
  407. uint8_t *scale_encode_string(uint8_t *dest, struct vector *s)
  408. {
  409. uint32_t len = s->len;
  410. uint8_t *data_dst = compact_encode_u32(dest, len);
  411. uint8_t *data = s->data;
  412. while (len--)
  413. {
  414. *data_dst++ = *data++;
  415. }
  416. return data_dst;
  417. }
  418. struct vector *scale_decode_string(uint8_t **from)
  419. {
  420. uint8_t *src = *from;
  421. uint8_t upperbits = *src >> 2;
  422. uint32_t size_array;
  423. switch (*src & 0x03)
  424. {
  425. case 0:
  426. size_array = upperbits;
  427. src += 1;
  428. break;
  429. case 1:
  430. size_array = *((uint16_t *)src) >> 2;
  431. src += 2;
  432. break;
  433. case 2:
  434. size_array = *((uint32_t *)src) >> 2;
  435. src += 4;
  436. break;
  437. default:
  438. // sizes of 2**30 (1GB) or larger are not allowed
  439. __builtin_unreachable();
  440. }
  441. struct vector *v = __malloc(sizeof(*v) + size_array);
  442. v->len = size_array;
  443. v->size = size_array;
  444. uint8_t *data = v->data;
  445. while (size_array--)
  446. {
  447. *data++ = *src++;
  448. }
  449. *from = src;
  450. return v;
  451. }