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

[WIP]Fix calling Buffer.toString with (offset, length, encoding) #3467

Merged
merged 6 commits into from
Jul 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 40 additions & 27 deletions src/bun.js/bindings/JSBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1436,43 +1436,56 @@ static inline JSC::EncodedJSValue jsBufferPrototypeFunction_toStringBody(JSC::JS
if (length == 0)
return JSC::JSValue::encode(JSC::jsEmptyString(vm));

switch (callFrame->argumentCount()) {
case 0: {
break;
}
case 2:
case 3:
case 1: {
EnsureStillAliveScope arg1 = callFrame->uncheckedArgument(0);
if (!arg1.value().isUndefined()) {
encoding = parseEncoding(lexicalGlobalObject, scope, arg1.value());
size_t argsCount = callFrame->argumentCount();

JSC::JSValue arg1 = callFrame->argument(0);
JSC::JSValue arg2 = callFrame->argument(1);
JSC::JSValue arg3 = callFrame->argument(2);

// This method could be called in following forms:
// - toString()
// - toString(encoding)
// - toString(encoding, start)
// - toString(encoding, start, end)
// - toString(offset, length)
// - toString(offset, length, encoding)
if (argsCount == 0)
return jsBufferToString(vm, lexicalGlobalObject, castedThis, offset, length, encoding);

if (arg1.isString()) {
encoding = parseEncoding(lexicalGlobalObject, scope, arg1);
RETURN_IF_EXCEPTION(scope, JSC::JSValue::encode(jsUndefined()));
}
if (callFrame->argumentCount() == 1)
break;
}
// any
case 5: {
JSC::JSValue arg2 = callFrame->uncheckedArgument(1);
int32_t ioffset = arg2.toInt32(lexicalGlobalObject);

if (!arg3.isUndefined()) {
// length is end
length = std::min(byteLength, static_cast<uint32_t>(arg3.toInt32(lexicalGlobalObject)));
}

int32_t istart = arg2.toInt32(lexicalGlobalObject);
if (istart < 0) {
throwTypeError(lexicalGlobalObject, scope, "Start must be a positive integer"_s);
return JSC::JSValue::encode(jsUndefined());
}
offset = static_cast<uint32_t>(istart);
length = (length > offset) ? (length - offset) : 0;
} else {
int32_t ioffset = arg1.toInt32(lexicalGlobalObject);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

toInt32 can throw, we should technically check for exceptions here (we did not before)

if (ioffset < 0) {
throwTypeError(lexicalGlobalObject, scope, "Offset must be a positive integer"_s);
return JSC::JSValue::encode(jsUndefined());
}
offset = static_cast<uint32_t>(ioffset);
length = (length > offset) ? (length - offset) : 0;

if (callFrame->argumentCount() == 2)
break;
}
if (!arg3.isUndefined()) {
encoding = parseEncoding(lexicalGlobalObject, scope, arg3);
RETURN_IF_EXCEPTION(scope, JSC::JSValue::encode(jsUndefined()));
}

default: {
length = std::min(byteLength, static_cast<uint32_t>(callFrame->argument(2).toInt32(lexicalGlobalObject)));
break;
}
if (!arg2.isUndefined())
length = std::min(length, static_cast<uint32_t>(arg2.toInt32(lexicalGlobalObject)));
}

length -= std::min(offset, length);

return jsBufferToString(vm, lexicalGlobalObject, castedThis, offset, length, encoding);
}

Expand Down
32 changes: 16 additions & 16 deletions src/js/builtins/JSBufferPrototype.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,29 +427,29 @@ export function hexWrite(this: BufferExt, text, offset, length) {
return this.write(text, offset, length, "hex");
}

export function utf8Slice(this: BufferExt, offset, length) {
return this.toString(offset, length, "utf8");
export function utf8Slice(this: BufferExt, start, end) {
return this.toString("utf8", start, end);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

start and end are used instead of offset and length to maintain compatibility with nodejs.

}
export function ucs2Slice(this: BufferExt, offset, length) {
return this.toString(offset, length, "ucs2");
export function ucs2Slice(this: BufferExt, start, end) {
return this.toString("ucs2", start, end);
}
export function utf16leSlice(this: BufferExt, offset, length) {
return this.toString(offset, length, "utf16le");
export function utf16leSlice(this: BufferExt, start, end) {
return this.toString("utf16le", start, end);
}
export function latin1Slice(this: BufferExt, offset, length) {
return this.toString(offset, length, "latin1");
export function latin1Slice(this: BufferExt, start, end) {
return this.toString("latin1", start, end);
}
export function asciiSlice(this: BufferExt, offset, length) {
return this.toString(offset, length, "ascii");
export function asciiSlice(this: BufferExt, start, end) {
return this.toString("ascii", start, end);
}
export function base64Slice(this: BufferExt, offset, length) {
return this.toString(offset, length, "base64");
export function base64Slice(this: BufferExt, start, end) {
return this.toString("base64", start, end);
}
export function base64urlSlice(this: BufferExt, offset, length) {
return this.toString(offset, length, "base64url");
export function base64urlSlice(this: BufferExt, start, end) {
return this.toString("base64url", start, end);
}
export function hexSlice(this: BufferExt, offset, length) {
return this.toString(offset, length, "hex");
export function hexSlice(this: BufferExt, start, end) {
return this.toString("hex", start, end);
}

export function toJSON(this: BufferExt) {
Expand Down
79 changes: 79 additions & 0 deletions test/js/node/buffer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2353,6 +2353,85 @@
}
});

it("Buffer.toString(encoding, start, end)", () => {
const buf = Buffer.from("0123456789", "utf8");

expect(buf.toString()).toStrictEqual("0123456789");
expect(buf.toString("utf8")).toStrictEqual("0123456789");
expect(buf.toString("utf8", 3)).toStrictEqual("3456789");
expect(buf.toString("utf8", 3, 4)).toStrictEqual("3");

expect(buf.toString("utf8", 3, 100)).toStrictEqual("3456789");
expect(buf.toString("utf8", 3, 1)).toStrictEqual("");
expect(buf.toString("utf8", 100, 200)).toStrictEqual("");
expect(buf.toString("utf8", 100, 1)).toStrictEqual("");
});

it("Buffer.toString(offset, length, encoding)", () => {
const buf = Buffer.from("0123456789", "utf8");

expect(buf.toString(3, 6, "utf8")).toStrictEqual("345678");
expect(buf.toString(3, 100, "utf8")).toStrictEqual("3456789");
expect(buf.toString(100, 200, "utf8")).toStrictEqual("");
expect(buf.toString(100, 50, "utf8")).toStrictEqual("");
});

it("Buffer.asciiSlice())", () => {
const buf = Buffer.from("0123456789", "ascii");

expect(buf.asciiSlice()).toStrictEqual("0123456789");
expect(buf.asciiSlice(3)).toStrictEqual("3456789");
expect(buf.asciiSlice(3, 4)).toStrictEqual("3");

Check failure on line 2384 in test/js/node/buffer.test.js

View workflow job for this annotation

GitHub Actions / Tests linux-x64

error: expect(received).toStrictEqual(expected)

Expected: "3" Received: "3456" at /home/runner/work/bun/bun/test/js/node/buffer.test.js:2384:2

Check failure on line 2384 in test/js/node/buffer.test.js

View workflow job for this annotation

GitHub Actions / Tests linux-x64-baseline

error: expect(received).toStrictEqual(expected)

Expected: "3" Received: "3456" at /home/runner/work/bun/bun/test/js/node/buffer.test.js:2384:2

Check failure on line 2384 in test/js/node/buffer.test.js

View workflow job for this annotation

GitHub Actions / Tests bun-darwin-x64-baseline

error: expect(received).toStrictEqual(expected)

Expected: "3" Received: "3456" at /Users/runner/work/bun/bun/test/js/node/buffer.test.js:2384:2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests are failing

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh its failing because we need to commit regenerated builtins

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. I had mistakenly thought that the generated code was not required for commit.

});

it("Buffer.latin1Slice()", () => {
const buf = Buffer.from("âéö", "latin1");

expect(buf.latin1Slice()).toStrictEqual("âéö");
expect(buf.latin1Slice(1)).toStrictEqual("éö");
expect(buf.latin1Slice(1, 2)).toStrictEqual("é");

Check failure on line 2392 in test/js/node/buffer.test.js

View workflow job for this annotation

GitHub Actions / Tests linux-x64

error: expect(received).toStrictEqual(expected)

Expected: " Received: " at /home/runner/work/bun/bun/test/js/node/buffer.test.js:2392:2

Check failure on line 2392 in test/js/node/buffer.test.js

View workflow job for this annotation

GitHub Actions / Tests linux-x64-baseline

error: expect(received).toStrictEqual(expected)

Expected: " Received: " at /home/runner/work/bun/bun/test/js/node/buffer.test.js:2392:2

Check failure on line 2392 in test/js/node/buffer.test.js

View workflow job for this annotation

GitHub Actions / Tests bun-darwin-x64-baseline

error: expect(received).toStrictEqual(expected)

Expected: " Received: " at /Users/runner/work/bun/bun/test/js/node/buffer.test.js:2392:2
});

it("Buffer.utf8Slice()", () => {
const buf = Buffer.from("あいうえお", "utf8");

expect(buf.utf8Slice()).toStrictEqual("あいうえお");
expect(buf.utf8Slice(3)).toStrictEqual("いうえお");
expect(buf.utf8Slice(3, 6)).toStrictEqual("い");

Check failure on line 2400 in test/js/node/buffer.test.js

View workflow job for this annotation

GitHub Actions / Tests linux-x64

error: expect(received).toStrictEqual(expected)

Expected: " Received: " at /home/runner/work/bun/bun/test/js/node/buffer.test.js:2400:2

Check failure on line 2400 in test/js/node/buffer.test.js

View workflow job for this annotation

GitHub Actions / Tests linux-x64-baseline

error: expect(received).toStrictEqual(expected)

Expected: " Received: " at /home/runner/work/bun/bun/test/js/node/buffer.test.js:2400:2

Check failure on line 2400 in test/js/node/buffer.test.js

View workflow job for this annotation

GitHub Actions / Tests bun-darwin-x64-baseline

error: expect(received).toStrictEqual(expected)

Expected: " Received: " at /Users/runner/work/bun/bun/test/js/node/buffer.test.js:2400:2
});

it("Buffer.hexSlice()", () => {
const buf = Buffer.from("0123456789", "utf8");

expect(buf.hexSlice()).toStrictEqual("30313233343536373839");
expect(buf.hexSlice(3)).toStrictEqual("33343536373839");
expect(buf.hexSlice(3, 4)).toStrictEqual("33");

Check failure on line 2408 in test/js/node/buffer.test.js

View workflow job for this annotation

GitHub Actions / Tests linux-x64

error: expect(received).toStrictEqual(expected)

Expected: "33" Received: "33343536" at /home/runner/work/bun/bun/test/js/node/buffer.test.js:2408:2

Check failure on line 2408 in test/js/node/buffer.test.js

View workflow job for this annotation

GitHub Actions / Tests linux-x64-baseline

error: expect(received).toStrictEqual(expected)

Expected: "33" Received: "33343536" at /home/runner/work/bun/bun/test/js/node/buffer.test.js:2408:2

Check failure on line 2408 in test/js/node/buffer.test.js

View workflow job for this annotation

GitHub Actions / Tests bun-darwin-x64-baseline

error: expect(received).toStrictEqual(expected)

Expected: "33" Received: "33343536" at /Users/runner/work/bun/bun/test/js/node/buffer.test.js:2408:2
});

it("Buffer.ucs2Slice()", () => {
const buf = Buffer.from("あいうえお", "ucs2");

expect(buf.ucs2Slice()).toStrictEqual("あいうえお");
expect(buf.ucs2Slice(2)).toStrictEqual("いうえお");
expect(buf.ucs2Slice(2, 6)).toStrictEqual("いう");

Check failure on line 2416 in test/js/node/buffer.test.js

View workflow job for this annotation

GitHub Actions / Tests linux-x64

error: expect(received).toStrictEqual(expected)

Expected: " Received: " at /home/runner/work/bun/bun/test/js/node/buffer.test.js:2416:2

Check failure on line 2416 in test/js/node/buffer.test.js

View workflow job for this annotation

GitHub Actions / Tests linux-x64-baseline

error: expect(received).toStrictEqual(expected)

Expected: " Received: " at /home/runner/work/bun/bun/test/js/node/buffer.test.js:2416:2

Check failure on line 2416 in test/js/node/buffer.test.js

View workflow job for this annotation

GitHub Actions / Tests bun-darwin-x64-baseline

error: expect(received).toStrictEqual(expected)

Expected: " Received: " at /Users/runner/work/bun/bun/test/js/node/buffer.test.js:2416:2
});

it("Buffer.base64Slice()", () => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get a segmentfault in the base64-related tests on my local machine. I don't know what happened here.

Copy link
Collaborator Author

@Hanaasagi Hanaasagi Jul 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Attempting to debug a coredump file using gdb, the arguments passed to WTF__toBase64URLStringValue looks like strange.

2023-07-01_15-28

Updated:

I build the latest main branch, also find this problem. toString('base64) will segment fault. But if i use the canary release bun, not find this problem.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I vaguely remember @cirospaciari running into strange issues with .toBase64URL as well... I think we should switch to using the SIMD base64 library that Node.js also uses. Our current implementation uses a mix of WebKit's internal atob function and Zig's standard library. I would guess the cause of this is the Zig standard library usage, but it's hard to say without digging deeper

const buf = Buffer.from("0123456789", "utf8");

expect(buf.base64Slice()).toStrictEqual("MDEyMzQ1Njc4OQ==");
expect(buf.base64Slice(3)).toStrictEqual("MzQ1Njc4OQ==");
expect(buf.base64Slice(3, 4)).toStrictEqual("Mw==");

Check failure on line 2424 in test/js/node/buffer.test.js

View workflow job for this annotation

GitHub Actions / Tests linux-x64

error: expect(received).toStrictEqual(expected)

Expected: "Mw==" Received: "MzQ1Ng==" at /home/runner/work/bun/bun/test/js/node/buffer.test.js:2424:2

Check failure on line 2424 in test/js/node/buffer.test.js

View workflow job for this annotation

GitHub Actions / Tests linux-x64-baseline

error: expect(received).toStrictEqual(expected)

Expected: "Mw==" Received: "MzQ1Ng==" at /home/runner/work/bun/bun/test/js/node/buffer.test.js:2424:2

Check failure on line 2424 in test/js/node/buffer.test.js

View workflow job for this annotation

GitHub Actions / Tests bun-darwin-x64-baseline

error: expect(received).toStrictEqual(expected)

Expected: "Mw==" Received: "MzQ1Ng==" at /Users/runner/work/bun/bun/test/js/node/buffer.test.js:2424:2
});

it("Buffer.base64urlSlice()", () => {
const buf = Buffer.from("0123456789", "utf8");

expect(buf.base64urlSlice()).toStrictEqual("MDEyMzQ1Njc4OQ");
expect(buf.base64urlSlice(3)).toStrictEqual("MzQ1Njc4OQ");
expect(buf.base64urlSlice(3, 4)).toStrictEqual("Mw");

Check failure on line 2432 in test/js/node/buffer.test.js

View workflow job for this annotation

GitHub Actions / Tests linux-x64

error: expect(received).toStrictEqual(expected)

Expected: "Mw" Received: "MzQ1Ng" at /home/runner/work/bun/bun/test/js/node/buffer.test.js:2432:2

Check failure on line 2432 in test/js/node/buffer.test.js

View workflow job for this annotation

GitHub Actions / Tests linux-x64-baseline

error: expect(received).toStrictEqual(expected)

Expected: "Mw" Received: "MzQ1Ng" at /home/runner/work/bun/bun/test/js/node/buffer.test.js:2432:2

Check failure on line 2432 in test/js/node/buffer.test.js

View workflow job for this annotation

GitHub Actions / Tests bun-darwin-x64-baseline

error: expect(received).toStrictEqual(expected)

Expected: "Mw" Received: "MzQ1Ng" at /Users/runner/work/bun/bun/test/js/node/buffer.test.js:2432:2
});

it("should not crash on invalid UTF-8 byte sequence", () => {
const buf = Buffer.from([0xc0, 0xfd]);
expect(buf.length).toBe(2);
Expand Down
Loading