Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Panic due to Integer Overflow during sourcemap generation with custom build plugin #6513

Closed
strau0106 opened this issue Oct 15, 2023 · 4 comments
Labels
bug Something isn't working bundler Something to do with the bundler crash An issue that could cause a crash

Comments

@strau0106
Copy link

strau0106 commented Oct 15, 2023

What version of Bun is running?

Issue occured on v1.0.3 and v1.06 and further testing was done on 1.0.6-debug+a87aa2fafe05c358455173ea65877be5eefcbd63

What platform is your computer?

Linux 6.1.0-9-amd64 x86_64 unknown

What steps can reproduce the bug?

I am not entirely sure what exactly is causing the Issue. As per request from @Jarred-Sumner on Disccord I am filing this issue with the appropriate code. For context: I am writing a discord bot and I have all my commands, events etc split up into different files. After moving to bun I wanted to build the project, and as this didn't work with the dynamic imports I had previously I wrote a Build plugin to set the code that resolves when importing a folder with e.g. ?handle-commands to an array of the default exports of the files in said folder. When running this build script, during the linking of the last file (which is also the entry point) I get the following:


uh-oh: integer overflow
bun will crash now 😭😭😭

----- bun meta -----
Bun v1.0.6_debug (a87aa2fa) Linux x64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.27-1 (2023-05-08)
AutoCommand: 
Elapsed: 446ms | User: 3204ms | Sys: 270ms
RSS: 0.43GB | Peak: 0.42GB | Commit: 0.43GB | Faults: 0
----- bun meta -----

0   0x55c029ff0b6b
1   ???
2   ???
3   ???
4   ???
5   ???
6   ???
7   ???
8   ???
9   ???
10  ???
11  ???
12  ???
13  ???
14  ???

Crash report saved to:
  ~/.bun/.bun-crash/v1.0.6_debug-1697368748265.crash

Search GitHub issues https://bun.sh/issues or ask for #help in https://bun.sh/discord

thread 156457 panic: integer overflow
Unwind error at address `:0x55c02bfcb10f` (error.InvalidDebugInfo), trace may be incomplete

Jarred suggested running it with lldb which results in the following stacktrace:

raise (@raise:9)
abort (@abort:49)
os.abort (/home/strau/node_modules/@oven/zig-linux-x64/lib/std/os.zig:608)
debug.panicImpl (/home/strau/node_modules/@oven/zig-linux-x64/lib/std/debug.zig:440)
builtin.default_panic (/home/strau/node_modules/@oven/zig-linux-x64/lib/std/builtin.zig:813)
src.panic_handler.NewPanicHandler((function 'default_panic')).handle_panic (@src.main.panic:72)
src.main.panic (@src.main.panic:18)
src.sourcemap.sourcemap.encodeVLQ (@src.sourcemap.sourcemap.encodeVLQ:64)
src.sourcemap.sourcemap.encodeVLQWithLookupTable (@src.sourcemap.sourcemap.encodeVLQWithLookupTable:48)
src.sourcemap.sourcemap.appendMappingToBuffer (@src.sourcemap.sourcemap.appendMappingToBuffer:81)
src.sourcemap.sourcemap.appendSourceMapChunk (@src.sourcemap.sourcemap.appendSourceMapChunk:352)
src.bundler.bundle_v2.LinkerContext.generateSourceMapForChunk (@src.bundler.bundle_v2.LinkerContext.generateSourceMapForChunk:985)
src.bundler.bundle_v2.LinkerContext.postProcessJSChunk (@src.bundler.bundle_v2.LinkerContext.postProcessJSChunk:1355)
src.bundler.bundle_v2.LinkerContext.generateChunkJS (@src.bundler.bundle_v2.LinkerContext.generateChunkJS:27)
src.thread_pool.Do__anon_66490.RunnerTask.call (@src.thread_pool.Do__anon_66490.RunnerTask.call:30)
src.thread_pool.Thread.run (@src.thread_pool.Thread.run:128)
Thread.callFn__anon_298951 (/home/strau/node_modules/@oven/zig-linux-x64/lib/std/Thread.zig:412)
Thread.PosixThreadImpl.spawn__anon_172486.Instance.entryFn (/home/strau/node_modules/@oven/zig-linux-x64/lib/std/Thread.zig:685)
___lldb_unnamed_symbol3513 (@___lldb_unnamed_symbol3513:147)
___lldb_unnamed_symbol3938 (@___lldb_unnamed_symbol3938:7)
in appendSourceMapChunk the local variable done: `"value;\n  }\n})});\nvar databaseConnectionOptions = {\n  authSource: \"admin\"\n};\ntry {\n  await import_mongoose6.connect(environment.DATABASE_URL, databaseConnectionOptions);\n} catch {\n  throw new Error(\"Couldn't connect to Database\");\n}\nvar config7 = await config_default2.findOne({ botID: environment.BOT_ID }).lean();\nif (!config7) {\n  throw new Error(Couldn't find config for botID ${environment.BOT_ID});\n}\nclient2.config = config7;\nlogger2.info(Startin with featuers ${config7.enabledFeatures}`);\nvar enabledFeatures = features_handle_features_default.filter((feature) => config7.enabledFeatures.includes(feature.name));\nenabledFeatures.push(base_feature);\nclient2.commands = enabledFeatures.map((feature) => feature.commands);\nclient2.messageComponents = enabledFeatures.map((feature) => feature.messageComponents);\nclient2.tasks = enabledFeatures.map((feature) => feature.tasks);\nclient2.events = enabledFeatures.map((feature) => feature.events);\nvar app = await createAPIApp(client2);\napp.listen(client2.config?.port)"...

suggests that it crashes shortly before the end of the index.ts file:

const databaseConnectionOptions = {
                "authSource": "admin"
        }


try {
  await  connect(environment.DATABASE_URL, databaseConnectionOptions);
} catch {
  throw new Error("Couldn't connect to Database");
}

const config = await configModel.findOne({botID: environment.BOT_ID}).lean();

if (!config) {
  throw new Error(`Couldn't find config for botID ${environment.BOT_ID}`);
}

client.config = config;

logger.info(`Startin with featuers ${config.enabledFeatures}`);


const enabledFeatures = features.filter((feature: any) => config.enabledFeatures.includes(feature.name));
enabledFeatures.push(base_feature);

client.commands = enabledFeatures.map((feature: any) => feature.commands);
client.messageComponents = enabledFeatures.map((feature: any) => feature.messageComponents);
client.tasks = enabledFeatures.map((feature: any) => feature.tasks);
client.events = enabledFeatures.map((feature: any) => feature.events);
const app = await createAPIApp(client)
app.listen(client.config?.port);

registerEvents(client.events, client);

logger.info("Logging In");
await client.login(config.token);

And finally the build plugin and script:

import { BunPlugin, plugin } from "bun";
import { dirname, resolve, join } from "path";
import { lstatSync, read, readdirSync } from "fs";


const query = /\?handle-(?:events|commands|tasks|components|features)/;

export const PathPlugin: BunPlugin = {
  name: "demo",
  target: "bun",
  setup({ onLoad, onResolve }) {
  onResolve({ filter: query }, ({ path, namespace, importer }) => {
      return {
        path: resolve(dirname(importer), path),
        namespace: namespace,
      };
    });
    onLoad({ filter: query }, ({ path: rawPath }) => {
      const path = rawPath.replace(query, "");
    
      const handleds = readdirSync(path).map((handled) => {
    
        if (rawPath.includes("handle-features")) {
    
          return `(import(${JSON.stringify(join(path, handled, `${handled}.ts`))})).default`;
          
        } else if (rawPath.includes("handle-commands")) {
          if (handled.endsWith(".command.ts")) {
            return `(import(${JSON.stringify(join(path, handled))})).default`;

          } else if (lstatSync(join(path, handled)).isDirectory()) {
            
            return `{
              ...(import(${JSON.stringify(join(path, handled, "group.ts"))})).default,
              options: [${
                readdirSync(join(path, handled)).filter((file) => (file!=="group.ts" )).map((file) => {
                  if (file.endsWith(".command.ts")) {
                    return `{
                      ...(import(${JSON.stringify(join(path, handled, file))})).default,
                      type: 1
                    }`;
                  } else if (lstatSync(join(path, handled, file)).isDirectory()) {
                    return `{
                      ...(import(${JSON.stringify(join(path, handled, file, "group.ts"))})).default,
                      type: 2,
                      options: [${
                        readdirSync(join(path, handled, file)).filter((subFile) => (subFile!=="group.ts" )).map((subFile) => {
                          if (subFile==="group.ts") return "";
                          if (subFile.endsWith(".command.ts")) {
                            return `{
                              ...(import(${JSON.stringify(join(path, handled, file, subFile))})).default,
                              type: 1
                            }`;
                          } else {
                            return "";
                          }
                        }).filter((file) => file !== "").join(",")
                      }]
                    }`
                  }
              })
            }]}`
          }
          
        } else {
    
          return `(import(${JSON.stringify(join(path, handled))})).default`;
      
        }
      
      
      });


      const arrayString = `[${handleds.join(",")}]`;
      console.log(arrayString);
      return {
        loader: "ts",
        contents: `export default ${arrayString}`,
      };
    });
  },
};

plugin(PathPlugin);
import { PathPlugin } from "./plugins/handler.ts";

const x = await Bun.build({
  entrypoints: ["apps/bot/src/index.ts"], // change it!
  outdir: "dist",
  sourcemap: "external",
  target: "bun",
  plugins: [PathPlugin],
});
console.log(x);

What is the expected behavior?

No crash!

What do you see instead?

No response

Additional information

No response

@strau0106 strau0106 added the bug Something isn't working label Oct 15, 2023
@Electroid Electroid added crash An issue that could cause a crash bundler Something to do with the bundler labels Oct 16, 2023
@strau0106
Copy link
Author

Disabling sourcemap generation has no effect, bun still crashes, but with an error message: "missing Asset file". I will share the other stacktrace and error in a bit.

@strau0106
Copy link
Author

I removed some files using @napi-rs/canvas, which apparently fixes the issue, I dont get a crash anymore?

Should I close?

@jg-til
Copy link

jg-til commented Mar 5, 2024

I just started evaluating Bun on my Windows machine, just started to get this error out of the blue this morning, and deleting my existing package-lock.json resolved it.

@paperdave
Copy link
Collaborator

the initial crash related to source maps was fixed a while ago in #11344 (bun upgrade). i think the missing asset file crash is fixed too but i'm not certain. if it is not, please open a new issue for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working bundler Something to do with the bundler crash An issue that could cause a crash
Projects
None yet
Development

No branches or pull requests

4 participants