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

Macro crashes with SIGSEGV when non-ASCII character in string template literal with interpolation #3641

Closed
maxmilton opened this issue Jul 16, 2023 · 4 comments · Fixed by #3643
Labels
bug Something isn't working bundler Something to do with the bundler crash An issue that could cause a crash

Comments

@maxmilton
Copy link
Contributor

What version of Bun is running?

0.6.14

What platform is your computer?

Linux 6.4.3-arch1-1 x86_64 unknown

What steps can reproduce the bug?

  1. Create file index.ts:
import { fn } from './macro' assert { type: 'macro' };

fn(${''}`); // <-- note the copyright symbol character
  1. Create file macro.ts:
export function fn(str) {
  return str;
}
  1. Run bun build ./index.ts or bun ./index.ts.

What is the expected behavior?

Template literals are processed correctly as Unicode when passed as macro arguments.
Bun process runs without a segmentation fault.

What do you see instead?

~/P/bun-macro-sigsegv master• ❱ bun build ./index.ts
fish: Job 1, 'bun build ./index.ts' terminated by signal SIGSEGV (Address boundary error)

Additional information

When there is no interpolation in the template literal the macro works as expected, even when it contains non-ASCII characters.

Where there are only ASCII characters, interpolation in the template literal the macro works as expected.

Happens on both stable v0.6.14 (b5665739) and current canary v0.6.15 (c39c11e1).

@maxmilton maxmilton added the bug Something isn't working label Jul 16, 2023
@Jarred-Sumner
Copy link
Collaborator

Interesting, this code is supposed to panic instead of segfault.

https://github.com/oven-sh/bun/blob/main/src/js_ast.zig#L10026-L10028

@maxmilton
Copy link
Contributor Author

I've only seen that condition trigger when trying to use a macro as a tagged template literal function, like so:

import { fn } from './macro' assert { type: 'macro' };

fn`...`;

@robobun robobun added bundler Something to do with the bundler crash An issue that could cause a crash labels Jul 16, 2023
@maxmilton
Copy link
Contributor Author

maxmilton commented Jul 16, 2023

Using the macro in a test sheds some light on why it's seg faulting. There is an extra character being prepended to Unicode characters outside the ASCII range.

Try:

  1. Create file index.test.ts:
import { test, expect } from 'bun:test';
import { fn } from './macro' assert { type: 'macro' };

test('fails', () => {
  expect(fn('©')).toBe('©');
});
  1. Run bun test.

Result:

~/P/_/bun-macro-sigsegv ❱ bun test
bun test v0.6.14 (b5665739)

index.test.ts:
1 | import { test, expect } from 'bun:test';
2 | import { fn } from './macro' assert { type: 'macro' };
3 | 
4 | test('fails', () => {
5 |   expect(fn('©')).toBe('©');
      ^
error: expect(received).toBe(expected)

Expected: "©"
Received: "©"

      at /home/xxxx/xxxx/bun-macro-sigsegv/index.test.ts:5:2
✗ fails [0.12ms]

 0 pass
 1 fail
 1 expect() calls
Ran 1 tests across 1 files. 1 total [6.00ms]

So it's likely something to do with string handling in macro arguments and probably not template literals specifically since the test uses a regular string.

Jarred-Sumner added a commit that referenced this issue Jul 16, 2023
Jarred-Sumner added a commit that referenced this issue Jul 17, 2023
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
@maxmilton
Copy link
Contributor Author

maxmilton commented Jul 23, 2023

Continued in: #3830

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

Successfully merging a pull request may close this issue.

3 participants