| ... | ... | @@ -91,7 +91,7 @@ int main(int argc, char **argv) { |
| 91 | 91 | InputStream_readByte(&in) != 'm') panic("input is not a zstd-compressed wasm file"); |
| 92 | 92 | if (InputStream_readLittle_u32(&in) != 1) panic("unsupported wasm version"); |
| 93 | 93 | |
| 94 | | FILE *out = fopen(argv[2], "w"); |
| 94 | FILE *out = fopen(argv[2], "wb"); |
| 95 | 95 | if (out == NULL) panic("unable to open output file"); |
| 96 | 96 | fputs("#include <math.h>\n" |
| 97 | 97 | "#include <stdint.h>\n" |
| ... | ... | @@ -225,14 +225,22 @@ int main(int argc, char **argv) { |
| 225 | 225 | " return dst;\n" |
| 226 | 226 | "}\n" |
| 227 | 227 | "\n" |
| 228 | | "static uint32_t memory_grow(uint8_t **m, uint32_t *p, uint32_t n) {\n" |
| 228 | "static uint32_t memory_grow(uint8_t **m, uint32_t *p, uint32_t *c, uint32_t n) {\n" |
| 229 | " uint8_t *new_m = *m;\n" |
| 229 | 230 | " uint32_t r = *p;\n" |
| 230 | 231 | " uint32_t new_p = r + n;\n" |
| 231 | | " uint8_t *new_m = realloc(*m, new_p << 16);\n" |
| 232 | | " if (new_m == NULL) return UINT32_C(0xFFFFFFF);\n" |
| 233 | | " memset(&new_m[r << 16], 0, n << 16);\n" |
| 234 | | " *m = new_m;\n" |
| 232 | " if (new_p > UINT32_C(0x10000)) return UINT32_C(0xFFFFFFF);\n" |
| 233 | " uint32_t new_c = *c;\n" |
| 234 | " if (new_c < new_p) {\n" |
| 235 | " do new_c += new_c / 2 + 8; while (new_c < new_p);\n" |
| 236 | " if (new_c > UINT32_C(0x10000)) new_c = UINT32_C(0x10000);\n" |
| 237 | " new_m = realloc(new_m, new_c << 16);\n" |
| 238 | " if (new_m == NULL) return UINT32_C(0xFFFFFFF);\n" |
| 239 | " *m = new_m;\n" |
| 240 | " *c = new_c;\n" |
| 241 | " }\n" |
| 235 | 242 | " *p = new_p;\n" |
| 243 | " memset(&new_m[r << 16], 0, n << 16);\n" |
| 236 | 244 | " return r;\n" |
| 237 | 245 | "}\n" |
| 238 | 246 | "\n" |
| ... | ... | @@ -369,7 +377,8 @@ int main(int argc, char **argv) { |
| 369 | 377 | for (uint32_t i = 0; i < mems_len; i += 1) { |
| 370 | 378 | mems[i].limits = InputStream_readLimits(&in); |
| 371 | 379 | fprintf(out, "static uint8_t *m%" PRIu32 ";\n" |
| 372 | | "static uint32_t p%" PRIu32 ";\n", i, i); |
| 380 | "static uint32_t p%" PRIu32 ";\n" |
| 381 | "static uint32_t c%" PRIu32 ";\n", i, i, i); |
| 373 | 382 | } |
| 374 | 383 | fputc('\n', out); |
| 375 | 384 | } |
| ... | ... | @@ -1319,8 +1328,8 @@ int main(int argc, char **argv) { |
| 1319 | 1328 | if (unreachable_depth == 0) { |
| 1320 | 1329 | uint32_t pages = FuncGen_stackPop(&fg); |
| 1321 | 1330 | FuncGen_stackPush(&fg, out, WasmValType_i32); |
| 1322 | | fprintf(out, "memory_grow(&m%" PRIu32 ", &p%" PRIu32 ", l%" PRIu32 ");\n", |
| 1323 | | mem_idx, mem_idx, pages); |
| 1331 | fprintf(out, "memory_grow(&m%" PRIu32 ", &p%" PRIu32 ", &c%" PRIu32 |
| 1332 | ", l%" PRIu32 ");\n", mem_idx, mem_idx, mem_idx, pages); |
| 1324 | 1333 | } |
| 1325 | 1334 | break; |
| 1326 | 1335 | } |
| ... | ... | @@ -2449,8 +2458,9 @@ int main(int argc, char **argv) { |
| 2449 | 2458 | fputs("static void init_data(void) {\n", out); |
| 2450 | 2459 | for (uint32_t i = 0; i < mems_len; i += 1) |
| 2451 | 2460 | fprintf(out, " p%" PRIu32 " = UINT32_C(%" PRIu32 ");\n" |
| 2452 | | " m%" PRIu32 " = calloc(p%" PRIu32 ", UINT32_C(1) << 16);\n", |
| 2453 | | i, mems[i].limits.min, i, i); |
| 2461 | " c%" PRIu32 " = p%" PRIu32 ";\n" |
| 2462 | " m%" PRIu32 " = calloc(c%" PRIu32 ", UINT32_C(1) << 16);\n", |
| 2463 | i, mems[i].limits.min, i, i, i, i); |
| 2454 | 2464 | for (uint32_t segment_i = 0; segment_i < len; segment_i += 1) { |
| 2455 | 2465 | uint32_t mem_idx; |
| 2456 | 2466 | switch (InputStream_readLeb128_u32(&in)) { |