Skip to content

Commit ebff06b

Browse files
kvakildanielleadams
authored andcommitted
src: remove usages of GetBackingStore in crypto
This removes all usages of GetBackingStore in `crypto`. See the linked issue for an explanation. Note: I am not sure of the lifetime semantics intended by `ArrayBufferOrViewContents` -- I am pretty sure it is correct based on a manual audit of the callsites, but please ensure that it is correct. Refs: #32226 Refs: #43921 PR-URL: #44079 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Feng Yu <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent e057480 commit ebff06b

File tree

3 files changed

+29
-16
lines changed

3 files changed

+29
-16
lines changed

src/crypto/README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,15 @@ the `ByteSource::Builder` without releasing it as a `ByteSource`.
112112

113113
### `ArrayBufferOrViewContents`
114114

115-
The `ArrayBufferOfViewContents` class is a helper utility that abstracts
115+
The `ArrayBufferOrViewContents` class is a helper utility that abstracts
116116
`ArrayBuffer`, `TypedArray`, or `DataView` inputs and provides access to
117117
their underlying data pointers. It is used extensively through `src/crypto`
118118
to make it easier to deal with inputs that allow any `ArrayBuffer`-backed
119119
object.
120120

121+
The lifetime of `ArrayBufferOrViewContents` should not exceed the
122+
lifetime of its input.
123+
121124
### Key objects
122125

123126
Most crypto operations involve the use of keys -- cryptographic inputs

src/crypto/crypto_cipher.cc

+6-9
Original file line numberDiff line numberDiff line change
@@ -523,9 +523,8 @@ void CipherBase::InitIv(const FunctionCallbackInfo<Value>& args) {
523523
if (UNLIKELY(key_buf.size() > INT_MAX))
524524
return THROW_ERR_OUT_OF_RANGE(env, "key is too big");
525525

526-
ArrayBufferOrViewContents<unsigned char> iv_buf;
527-
if (!args[2]->IsNull())
528-
iv_buf = ArrayBufferOrViewContents<unsigned char>(args[2]);
526+
ArrayBufferOrViewContents<unsigned char> iv_buf(
527+
!args[2]->IsNull() ? args[2] : Local<Value>());
529528

530529
if (UNLIKELY(!iv_buf.CheckSizeInt32()))
531530
return THROW_ERR_OUT_OF_RANGE(env, "iv is too big");
@@ -1048,12 +1047,10 @@ void PublicKeyCipher::Cipher(const FunctionCallbackInfo<Value>& args) {
10481047
return THROW_ERR_OSSL_EVP_INVALID_DIGEST(env);
10491048
}
10501049

1051-
ArrayBufferOrViewContents<unsigned char> oaep_label;
1052-
if (!args[offset + 3]->IsUndefined()) {
1053-
oaep_label = ArrayBufferOrViewContents<unsigned char>(args[offset + 3]);
1054-
if (UNLIKELY(!oaep_label.CheckSizeInt32()))
1055-
return THROW_ERR_OUT_OF_RANGE(env, "oaep_label is too big");
1056-
}
1050+
ArrayBufferOrViewContents<unsigned char> oaep_label(
1051+
!args[offset + 3]->IsUndefined() ? args[offset + 3] : Local<Value>());
1052+
if (UNLIKELY(!oaep_label.CheckSizeInt32()))
1053+
return THROW_ERR_OUT_OF_RANGE(env, "oaep_label is too big");
10571054

10581055
std::unique_ptr<BackingStore> out;
10591056
if (!Cipher<operation, EVP_PKEY_cipher_init, EVP_PKEY_cipher>(

src/crypto/crypto_util.h

+19-6
Original file line numberDiff line numberDiff line change
@@ -696,24 +696,30 @@ template <typename T>
696696
class ArrayBufferOrViewContents {
697697
public:
698698
ArrayBufferOrViewContents() = default;
699+
ArrayBufferOrViewContents(const ArrayBufferOrViewContents&) = delete;
700+
void operator=(const ArrayBufferOrViewContents&) = delete;
699701

700702
inline explicit ArrayBufferOrViewContents(v8::Local<v8::Value> buf) {
703+
if (buf.IsEmpty()) {
704+
return;
705+
}
706+
701707
CHECK(IsAnyByteSource(buf));
702708
if (buf->IsArrayBufferView()) {
703709
auto view = buf.As<v8::ArrayBufferView>();
704710
offset_ = view->ByteOffset();
705711
length_ = view->ByteLength();
706-
store_ = view->Buffer()->GetBackingStore();
712+
data_ = view->Buffer()->Data();
707713
} else if (buf->IsArrayBuffer()) {
708714
auto ab = buf.As<v8::ArrayBuffer>();
709715
offset_ = 0;
710716
length_ = ab->ByteLength();
711-
store_ = ab->GetBackingStore();
717+
data_ = ab->Data();
712718
} else {
713719
auto sab = buf.As<v8::SharedArrayBuffer>();
714720
offset_ = 0;
715721
length_ = sab->ByteLength();
716-
store_ = sab->GetBackingStore();
722+
data_ = sab->Data();
717723
}
718724
}
719725

@@ -723,7 +729,7 @@ class ArrayBufferOrViewContents {
723729
// length is zero, so we have to return something.
724730
if (size() == 0)
725731
return &buf;
726-
return reinterpret_cast<T*>(store_->Data()) + offset_;
732+
return reinterpret_cast<T*>(data_) + offset_;
727733
}
728734

729735
inline T* data() {
@@ -732,7 +738,7 @@ class ArrayBufferOrViewContents {
732738
// length is zero, so we have to return something.
733739
if (size() == 0)
734740
return &buf;
735-
return reinterpret_cast<T*>(store_->Data()) + offset_;
741+
return reinterpret_cast<T*>(data_) + offset_;
736742
}
737743

738744
inline size_t size() const { return length_; }
@@ -772,7 +778,14 @@ class ArrayBufferOrViewContents {
772778
T buf = 0;
773779
size_t offset_ = 0;
774780
size_t length_ = 0;
775-
std::shared_ptr<v8::BackingStore> store_;
781+
void* data_ = nullptr;
782+
783+
// Declaring operator new and delete as deleted is not spec compliant.
784+
// Therefore declare them private instead to disable dynamic alloc
785+
void* operator new(size_t);
786+
void* operator new[](size_t);
787+
void operator delete(void*);
788+
void operator delete[](void*);
776789
};
777790

778791
v8::MaybeLocal<v8::Value> EncodeBignum(

0 commit comments

Comments
 (0)