Skip to content

Commit f4eccd1

Browse files
targosnodejs-github-bot
authored andcommitted
[HACK] temporary FastApiTypedArray fixes
1 parent 742af03 commit f4eccd1

File tree

3 files changed

+42
-114
lines changed

3 files changed

+42
-114
lines changed

src/crypto/crypto_timing.cc

+1-26
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
namespace node {
1212

1313
using v8::FastApiCallbackOptions;
14-
using v8::FastApiTypedArray;
1514
using v8::FunctionCallbackInfo;
1615
using v8::HandleScope;
1716
using v8::Local;
@@ -50,35 +49,11 @@ void TimingSafeEqual(const FunctionCallbackInfo<Value>& args) {
5049
CRYPTO_memcmp(buf1.data(), buf2.data(), buf1.size()) == 0);
5150
}
5251

53-
bool FastTimingSafeEqual(Local<Value> receiver,
54-
const FastApiTypedArray<uint8_t>& a,
55-
const FastApiTypedArray<uint8_t>& b,
56-
// NOLINTNEXTLINE(runtime/references)
57-
FastApiCallbackOptions& options) {
58-
uint8_t* data_a;
59-
uint8_t* data_b;
60-
if (a.length() != b.length() || !a.getStorageIfAligned(&data_a) ||
61-
!b.getStorageIfAligned(&data_b)) {
62-
TRACK_V8_FAST_API_CALL("crypto.timingSafeEqual.error");
63-
HandleScope scope(options.isolate);
64-
THROW_ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH(options.isolate);
65-
return false;
66-
}
67-
68-
TRACK_V8_FAST_API_CALL("crypto.timingSafeEqual.ok");
69-
return CRYPTO_memcmp(data_a, data_b, a.length()) == 0;
70-
}
71-
72-
static v8::CFunction fast_equal(v8::CFunction::Make(FastTimingSafeEqual));
73-
7452
void Initialize(Environment* env, Local<Object> target) {
75-
SetFastMethodNoSideEffect(
76-
env->context(), target, "timingSafeEqual", TimingSafeEqual, &fast_equal);
53+
SetMethod(env->context(), target, "timingSafeEqual", TimingSafeEqual);
7754
}
7855
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
7956
registry->Register(TimingSafeEqual);
80-
registry->Register(FastTimingSafeEqual);
81-
registry->Register(fast_equal.GetTypeInfo());
8257
}
8358
} // namespace Timing
8459

src/node_buffer.cc

+34-65
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,15 @@
4242
#include "nbytes.h"
4343

4444
#define THROW_AND_RETURN_UNLESS_BUFFER(env, obj) \
45-
THROW_AND_RETURN_IF_NOT_BUFFER(env, obj, "argument") \
45+
THROW_AND_RETURN_IF_NOT_BUFFER(env, obj, "argument")
46+
47+
#define THROW_AND_RETURN_VAL_UNLESS_BUFFER(isolate, val, prefix, retval) \
48+
do { \
49+
if (!Buffer::HasInstance(val)) { \
50+
node::THROW_ERR_INVALID_ARG_TYPE(isolate, prefix " must be a buffer"); \
51+
return retval; \
52+
} \
53+
} while (0)
4654

4755
#define THROW_AND_RETURN_IF_OOB(r) \
4856
do { \
@@ -60,7 +68,6 @@ using v8::ArrayBufferView;
6068
using v8::BackingStore;
6169
using v8::Context;
6270
using v8::EscapableHandleScope;
63-
using v8::FastApiTypedArray;
6471
using v8::FunctionCallbackInfo;
6572
using v8::Global;
6673
using v8::HandleScope;
@@ -582,19 +589,17 @@ void SlowCopy(const FunctionCallbackInfo<Value>& args) {
582589

583590
// Assume caller has properly validated args.
584591
uint32_t FastCopy(Local<Value> receiver,
585-
const v8::FastApiTypedArray<uint8_t>& source,
586-
const v8::FastApiTypedArray<uint8_t>& target,
592+
Local<Value> source_obj,
593+
Local<Value> target_obj,
587594
uint32_t target_start,
588595
uint32_t source_start,
589-
uint32_t to_copy) {
590-
uint8_t* source_data;
591-
CHECK(source.getStorageIfAligned(&source_data));
592-
593-
uint8_t* target_data;
594-
CHECK(target.getStorageIfAligned(&target_data));
595-
596-
memmove(target_data + target_start, source_data + source_start, to_copy);
596+
uint32_t to_copy,
597+
// NOLINTNEXTLINE(runtime/references) This is V8 api.
598+
v8::FastApiCallbackOptions& options) {
599+
ArrayBufferViewContents<char> source(source_obj);
600+
SPREAD_BUFFER_ARG(target_obj, target);
597601

602+
memmove(target_data + target_start, source.data() + source_start, to_copy);
598603
return to_copy;
599604
}
600605

@@ -857,24 +862,6 @@ void Compare(const FunctionCallbackInfo<Value> &args) {
857862
args.GetReturnValue().Set(val);
858863
}
859864

860-
int32_t FastCompare(v8::Local<v8::Value>,
861-
const FastApiTypedArray<uint8_t>& a,
862-
const FastApiTypedArray<uint8_t>& b) {
863-
uint8_t* data_a;
864-
uint8_t* data_b;
865-
CHECK(a.getStorageIfAligned(&data_a));
866-
CHECK(b.getStorageIfAligned(&data_b));
867-
868-
size_t cmp_length = std::min(a.length(), b.length());
869-
870-
return normalizeCompareVal(
871-
cmp_length > 0 ? memcmp(data_a, data_b, cmp_length) : 0,
872-
a.length(),
873-
b.length());
874-
}
875-
876-
static v8::CFunction fast_compare(v8::CFunction::Make(FastCompare));
877-
878865
// Computes the offset for starting an indexOf or lastIndexOf search.
879866
// Returns either a valid offset in [0...<length - 1>], ie inside the Buffer,
880867
// or -1 to signal that there is no possible match.
@@ -1125,7 +1112,7 @@ int32_t IndexOfNumber(const uint8_t* buffer_data,
11251112
return ptr != nullptr ? static_cast<int32_t>(ptr_uint8 - buffer_data) : -1;
11261113
}
11271114

1128-
void SlowIndexOfNumber(const FunctionCallbackInfo<Value>& args) {
1115+
void IndexOfNumber(const FunctionCallbackInfo<Value>& args) {
11291116
CHECK(args[1]->IsUint32());
11301117
CHECK(args[2]->IsNumber());
11311118
CHECK(args[3]->IsBoolean());
@@ -1141,20 +1128,6 @@ void SlowIndexOfNumber(const FunctionCallbackInfo<Value>& args) {
11411128
buffer.data(), buffer.length(), needle, offset_i64, is_forward));
11421129
}
11431130

1144-
int32_t FastIndexOfNumber(v8::Local<v8::Value>,
1145-
const FastApiTypedArray<uint8_t>& buffer,
1146-
uint32_t needle,
1147-
int64_t offset_i64,
1148-
bool is_forward) {
1149-
uint8_t* buffer_data;
1150-
CHECK(buffer.getStorageIfAligned(&buffer_data));
1151-
return IndexOfNumber(
1152-
buffer_data, buffer.length(), needle, offset_i64, is_forward);
1153-
}
1154-
1155-
static v8::CFunction fast_index_of_number(
1156-
v8::CFunction::Make(FastIndexOfNumber));
1157-
11581131
void Swap16(const FunctionCallbackInfo<Value>& args) {
11591132
Environment* env = Environment::GetCurrent(args);
11601133
THROW_AND_RETURN_UNLESS_BUFFER(env, args[0]);
@@ -1502,21 +1475,25 @@ void SlowWriteString(const FunctionCallbackInfo<Value>& args) {
15021475

15031476
template <encoding encoding>
15041477
uint32_t FastWriteString(Local<Value> receiver,
1505-
const v8::FastApiTypedArray<uint8_t>& dst,
1478+
Local<Value> dst,
15061479
const v8::FastOneByteString& src,
15071480
uint32_t offset,
1508-
uint32_t max_length) {
1509-
uint8_t* dst_data;
1510-
CHECK(dst.getStorageIfAligned(&dst_data));
1511-
CHECK(offset <= dst.length());
1512-
CHECK(dst.length() - offset <= std::numeric_limits<uint32_t>::max());
1481+
uint32_t max_length,
1482+
// NOLINTNEXTLINE(runtime/references) This is V8 api.
1483+
v8::FastApiCallbackOptions& options) {
1484+
THROW_AND_RETURN_VAL_UNLESS_BUFFER(options.isolate, dst, "dst", 0);
1485+
SPREAD_BUFFER_ARG(dst, dst_buffer);
1486+
CHECK(dst_buffer_length <=
1487+
static_cast<size_t>(std::numeric_limits<uint32_t>::max()));
1488+
uint32_t dst_size = static_cast<uint32_t>(dst_buffer_length);
1489+
CHECK(offset <= dst_size);
15131490
TRACK_V8_FAST_API_CALL("buffer.writeString");
15141491

15151492
return WriteOneByteString<encoding>(
15161493
src.data,
15171494
src.length,
1518-
reinterpret_cast<char*>(dst_data + offset),
1519-
std::min<uint32_t>(dst.length() - offset, max_length));
1495+
reinterpret_cast<char*>(dst_buffer_data + offset),
1496+
std::min<uint32_t>(dst_size - offset, max_length));
15201497
}
15211498

15221499
static v8::CFunction fast_write_string_ascii(
@@ -1543,16 +1520,12 @@ void Initialize(Local<Object> target,
15431520
"byteLengthUtf8",
15441521
SlowByteLengthUtf8,
15451522
&fast_byte_length_utf8);
1546-
SetFastMethod(context, target, "copy", SlowCopy, &fast_copy);
1547-
SetFastMethodNoSideEffect(context, target, "compare", Compare, &fast_compare);
1523+
SetMethod(context, target, "copy", SlowCopy);
1524+
SetMethod(context, target, "compare", Compare);
15481525
SetMethodNoSideEffect(context, target, "compareOffset", CompareOffset);
15491526
SetMethod(context, target, "fill", Fill);
15501527
SetMethodNoSideEffect(context, target, "indexOfBuffer", IndexOfBuffer);
1551-
SetFastMethodNoSideEffect(context,
1552-
target,
1553-
"indexOfNumber",
1554-
SlowIndexOfNumber,
1555-
&fast_index_of_number);
1528+
SetMethodNoSideEffect(context, target, "indexOfNumber", IndexOfNumber);
15561529
SetMethodNoSideEffect(context, target, "indexOfString", IndexOfString);
15571530

15581531
SetMethod(context, target, "detachArrayBuffer", DetachArrayBuffer);
@@ -1622,14 +1595,10 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
16221595
registry->Register(fast_copy.GetTypeInfo());
16231596
registry->Register(FastCopy);
16241597
registry->Register(Compare);
1625-
registry->Register(FastCompare);
1626-
registry->Register(fast_compare.GetTypeInfo());
16271598
registry->Register(CompareOffset);
16281599
registry->Register(Fill);
16291600
registry->Register(IndexOfBuffer);
1630-
registry->Register(SlowIndexOfNumber);
1631-
registry->Register(FastIndexOfNumber);
1632-
registry->Register(fast_index_of_number.GetTypeInfo());
1601+
registry->Register(IndexOfNumber);
16331602
registry->Register(IndexOfString);
16341603

16351604
registry->Register(Swap16);

src/node_external_reference.h

+7-23
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,6 @@ using CFunctionCallbackWithStrings =
3838
bool (*)(v8::Local<v8::Value>,
3939
const v8::FastOneByteString& input,
4040
const v8::FastOneByteString& base);
41-
using CFunctionCallbackWithTwoUint8Arrays =
42-
int32_t (*)(v8::Local<v8::Value>,
43-
const v8::FastApiTypedArray<uint8_t>&,
44-
const v8::FastApiTypedArray<uint8_t>&);
45-
using CFunctionCallbackWithTwoUint8ArraysFallback =
46-
bool (*)(v8::Local<v8::Value>,
47-
const v8::FastApiTypedArray<uint8_t>&,
48-
const v8::FastApiTypedArray<uint8_t>&,
49-
v8::FastApiCallbackOptions&);
50-
using CFunctionCallbackWithUint8ArrayUint32Int64Bool =
51-
int32_t (*)(v8::Local<v8::Value>,
52-
const v8::FastApiTypedArray<uint8_t>&,
53-
uint32_t,
54-
int64_t,
55-
bool);
5641
using CFunctionWithUint32 = uint32_t (*)(v8::Local<v8::Value>,
5742
const uint32_t input);
5843
using CFunctionWithDoubleReturnDouble = double (*)(v8::Local<v8::Value>,
@@ -68,18 +53,20 @@ using CFunctionWithBool = void (*)(v8::Local<v8::Value>,
6853

6954
using CFunctionWriteString =
7055
uint32_t (*)(v8::Local<v8::Value> receiver,
71-
const v8::FastApiTypedArray<uint8_t>& dst,
56+
v8::Local<v8::Value> dst,
7257
const v8::FastOneByteString& src,
7358
uint32_t offset,
74-
uint32_t max_length);
59+
uint32_t max_length,
60+
v8::FastApiCallbackOptions&);
7561

7662
using CFunctionBufferCopy =
7763
uint32_t (*)(v8::Local<v8::Value> receiver,
78-
const v8::FastApiTypedArray<uint8_t>& source,
79-
const v8::FastApiTypedArray<uint8_t>& target,
64+
v8::Local<v8::Value> source,
65+
v8::Local<v8::Value> target,
8066
uint32_t target_start,
8167
uint32_t source_start,
82-
uint32_t to_copy);
68+
uint32_t to_copy,
69+
v8::FastApiCallbackOptions&);
8370

8471
// This class manages the external references from the V8 heap
8572
// to the C++ addresses in Node.js.
@@ -98,9 +85,6 @@ class ExternalReferenceRegistry {
9885
V(CFunctionCallbackWithBool) \
9986
V(CFunctionCallbackWithString) \
10087
V(CFunctionCallbackWithStrings) \
101-
V(CFunctionCallbackWithTwoUint8Arrays) \
102-
V(CFunctionCallbackWithTwoUint8ArraysFallback) \
103-
V(CFunctionCallbackWithUint8ArrayUint32Int64Bool) \
10488
V(CFunctionWithUint32) \
10589
V(CFunctionWithDoubleReturnDouble) \
10690
V(CFunctionWithInt64Fallback) \

0 commit comments

Comments
 (0)