diff --git a/src/node_i18n.cc b/src/node_i18n.cc index e743941734a7..740b07811ff9 100644 --- a/src/node_i18n.cc +++ b/src/node_i18n.cc @@ -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; @@ -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 { @@ -313,8 +316,33 @@ void Transcode(const FunctionCallbackInfo&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 view; + if (args[0]->IsArrayBufferView()) { + view = args[0].As(); + } else if (args[0]->IsSharedArrayBuffer()) { + view = Uint8Array::New(args[0].As(), 0, source_length); + } + std::unique_ptr source_copy; + if (!view.IsEmpty() && view->Buffer()->IsSharedArrayBuffer()) { + source_copy = std::make_unique_for_overwrite(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; } diff --git a/src/string_decoder.cc b/src/string_decoder.cc index 33f3ad94599a..51c375de3442 100644 --- a/src/string_decoder.cc +++ b/src/string_decoder.cc @@ -260,12 +260,26 @@ void DecodeData(const FunctionCallbackInfo& args) { CHECK_NOT_NULL(decoder); CHECK(args[1]->IsArrayBufferView()); - ArrayBufferViewContents content(args[1].As()); + Local view = args[1].As(); + ArrayBufferViewContents 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 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 ret; - if (decoder->DecodeData(args.GetIsolate(), content.data(), &length) - .ToLocal(&ret)) { + if (decoder->DecodeData(args.GetIsolate(), data, &length).ToLocal(&ret)) { args.GetReturnValue().Set(ret); } } diff --git a/test/parallel/test-buffer-sharedarraybuffer-concurrent-write.js b/test/parallel/test-buffer-sharedarraybuffer-concurrent-write.js new file mode 100644 index 000000000000..63701c59e1c4 --- /dev/null +++ b/test/parallel/test-buffer-sharedarraybuffer-concurrent-write.js @@ -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()); +}));