oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
74.12k stars 2.76k forks source link

crash in `ASTMemoryAllocator` when using extremely large sources and `--compile` #13103

Open paperdave opened 3 months ago

paperdave commented 3 months ago

When using this test case

itBundled("bun/SourceMapLargeFile", {
  target: "bun",
  compile: true,
  files: {
    "/entry.ts": /* js */ `
      // this test is not checking behaviors with any of these libraries but
      // just using them to increase the size of the binary to see if large
      // payloads have issues. accuracy of large sourcemaps is tested elsewhere.
      import * as aws from ${JSON.stringify(require.resolve("aws-cdk-lib"))};
      import * as webpack from ${JSON.stringify(require.resolve("webpack"))};
      import * as mysql2 from ${JSON.stringify(require.resolve("mysql2"))};

      // this file has comments and weird whitespace, intentionally
      // to make it obvious if sourcemaps were generated and mapped properly
      if           (true) code();
      function code() {
        // hello world
                throw   new
          Error("Hello World");
      }

      console.log(aws, webpack, mysql2);
    `,
  },
  sourceMap: "external",
  onAfterBundle(api) {
    rmSync(api.join("entry.ts"), {}); // Hide the source files for errors
  },
  run: {
    exitCode: 1,
    validate({ stderr }) {
      expect(stderr).toStartWith(
        `1 | // this file has comments and weird whitespace, intentionally
2 | // to make it obvious if sourcemaps were generated and mapped properly
3 | if           (true) code();
4 | function code() {
5 |   // hello world
6 |           throw   new
                    ^
error: Hello World`,
      );
      expect(stderr).toInclude("entry.ts:6:19");
    },
  },
});

I get a crash while bundling in ASTMemoryAllocator.

image

My proposed solution to this is to just delete ASTMemoryAllocator, as it was sort of leftover code from https://github.com/oven-sh/bun/pull/12388. NewStore is a better bump allocator. Looking at the remaining usages of it, I think it could be replaced with two things.

EDIT: The above solution is not satisfactory as AstMemoryAllocator is used to hold onto an ast for a lifetime longer than what the store allows for.

Opening an issue to look into later instead of just doing a quick fix, since that quick fix is unlikely to be reliable. the better fix is the harder thing.

paperdave commented 3 months ago

In a release build there is undefined behavior causing a module resolve error lol

70 |         fallbackBindings = require('@swc/wasm');
                                        ^
error: Could not resolve: "@swc/wasm". Maybe you need to "bun install"?
    at /Users/dave/code/bun/test/node_modules/@swc/core/index.js:70:36