Skip to content
Open
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
32 changes: 30 additions & 2 deletions src/node_i18n.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ extern "C" const char U_DATA_API SMALL_ICUDATA_ENTRY_POINT[];

namespace node {

using v8::ArrayBufferView;
using v8::Context;
using v8::FunctionCallbackInfo;
using v8::FunctionTemplate;
Expand All @@ -96,7 +97,9 @@ using v8::Local;
using v8::MaybeLocal;
using v8::Object;
using v8::ObjectTemplate;
using v8::SharedArrayBuffer;
using v8::String;
using v8::Uint8Array;
using v8::Value;

namespace i18n {
Expand Down Expand Up @@ -313,8 +316,33 @@ void Transcode(const FunctionCallbackInfo<Value>&args) {
ABORT();
}

result = tfn(env, EncodingName(fromEncoding), EncodingName(toEncoding),
input.data(), input.length(), &status);
const char* source = input.data();
const size_t source_length = input.length();

// Another thread can write to a SharedArrayBuffer while it is being
// transcoded. The simdutf converters size their output in one pass over
// the source and fill it in a second one, so they need a copy that cannot
// change in between. ArrayBufferView::CopyContents() reads shared memory
// with atomic loads, so a SharedArrayBuffer passed in directly gets a view.
Local<ArrayBufferView> view;
if (args[0]->IsArrayBufferView()) {
view = args[0].As<ArrayBufferView>();
} else if (args[0]->IsSharedArrayBuffer()) {
view = Uint8Array::New(args[0].As<SharedArrayBuffer>(), 0, source_length);
}
std::unique_ptr<char[]> source_copy;
if (!view.IsEmpty() && view->Buffer()->IsSharedArrayBuffer()) {
source_copy = std::make_unique_for_overwrite<char[]>(source_length);
view->CopyContents(source_copy.get(), source_length);
source = source_copy.get();
}

result = tfn(env,
EncodingName(fromEncoding),
EncodingName(toEncoding),
source,
source_length,
&status);
} else {
status = U_ILLEGAL_ARGUMENT_ERROR;
}
Expand Down
20 changes: 17 additions & 3 deletions src/string_decoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,26 @@ void DecodeData(const FunctionCallbackInfo<Value>& args) {
CHECK_NOT_NULL(decoder);

CHECK(args[1]->IsArrayBufferView());
ArrayBufferViewContents<char> content(args[1].As<ArrayBufferView>());
Local<ArrayBufferView> view = args[1].As<ArrayBufferView>();
ArrayBufferViewContents<char> content(view);
const char* data = content.data();
size_t length = content.length();

// Another thread can write to a SharedArrayBuffer while it is being decoded.
// UTF-8 decoding sizes the string in one pass over the input and fills it in
// a second one, so it needs a copy that cannot change in between.
std::unique_ptr<char[]> data_copy;
if (view->Buffer()->IsSharedArrayBuffer()) {
data_copy.reset(new (std::nothrow) char[length]);
if (!data_copy) {
return THROW_ERR_MEMORY_ALLOCATION_FAILED(args.GetIsolate());
}
view->CopyContents(data_copy.get(), length);
data = data_copy.get();
}

Local<String> ret;
if (decoder->DecodeData(args.GetIsolate(), content.data(), &length)
.ToLocal(&ret)) {
if (decoder->DecodeData(args.GetIsolate(), data, &length).ToLocal(&ret)) {
args.GetReturnValue().Set(ret);
}
}
Expand Down
60 changes: 60 additions & 0 deletions test/parallel/test-buffer-sharedarraybuffer-concurrent-write.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Flags: --expose-gc --expose-internals
'use strict';

// Another thread can write to a SharedArrayBuffer while buffer.transcode() or
// a StringDecoder is converting it. They must convert a copy of the memory: a
// conversion that measures its input in one pass and converts it in another
// overflows its output when the input changes in between.

const common = require('../common');
const assert = require('assert');
const { transcode } = require('buffer');
const { StringDecoder } = require('string_decoder');
const { Worker } = require('worker_threads');
const { internalBinding } = require('internal/test/binding');

// StringDecoder only decodes UTF-8 in more than one pass above 1 MiB.
const sab = new SharedArrayBuffer(2 * 1024 * 1024);

// Both byte pairs are valid UTF-8 and valid UTF-16LE, and each converts to a
// different number of code units than the other, in either direction. set()
// writes them to shared memory with atomic stores.
const worker = new Worker(`
const { parentPort, workerData } = require('worker_threads');
const shared = Buffer.from(workerData);
const ascii = Buffer.alloc(shared.length, 'A\\0');
const accented = Buffer.alloc(shared.length, 'é');
parentPort.postMessage('ready');
for (;;) {
shared.set(ascii);
shared.set(accented);
}
`, { eval: true, workerData: sab });

worker.once('message', common.mustCall(() => {
const shared = Buffer.from(sab);
const decoder = new StringDecoder('utf8');

for (let i = 0; i < 100; i++) {
decoder.end(shared);
if (common.hasIntl) {
try {
transcode(shared, 'utf8', 'ucs2');
} catch (err) {
// The copy can hold a character that was only half written.
assert.strictEqual(err.code, 'U_INVALID_CHAR_FOUND');
}
// The high byte of every UTF-16LE code unit is 0x00 or 0xA9, so none of
// them is a surrogate.
transcode(shared, 'ucs2', 'utf8');
// The binding also takes the SharedArrayBuffer itself.
const result = internalBinding('icu').transcode(sab, 'ucs2', 'utf8');
assert(Buffer.isBuffer(result));
}
// Each iteration leaves several MiB of results behind; collect them so
// the test does not grow to hundreds of MiB before V8 does.
globalThis.gc();
}

worker.terminate().then(common.mustCall());
}));
Loading