Skip to content

Commit 0067a0b

Browse files
committed
crypto: use cppgc to manage Hash
1 parent 8284613 commit 0067a0b

File tree

3 files changed

+56
-17
lines changed

3 files changed

+56
-17
lines changed

src/crypto/crypto_hash.cc

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "crypto/crypto_hash.h"
22
#include "async_wrap-inl.h"
33
#include "base_object-inl.h"
4+
#include "cppgc/allocation.h"
45
#include "env-inl.h"
56
#include "memory_tracker-inl.h"
67
#include "string_bytes.h"
@@ -26,14 +27,20 @@ using v8::Object;
2627
using v8::Uint32;
2728
using v8::Value;
2829

30+
#ifdef ASSIGN_OR_RETURN_UNWRAP
31+
#undef ASSIGN_OR_RETURN_UNWRAP
32+
#endif
33+
34+
#define ASSIGN_OR_RETURN_UNWRAP ASSIGN_OR_RETURN_UNWRAP_CPPGC
2935
namespace crypto {
30-
Hash::Hash(Environment* env, Local<Object> wrap) : BaseObject(env, wrap) {
31-
MakeWeak();
36+
Hash::Hash(Environment* env, Local<Object> wrap) {
37+
InitializeCppgc(this, env, wrap);
3238
}
3339

34-
void Hash::MemoryInfo(MemoryTracker* tracker) const {
35-
tracker->TrackFieldWithSize("mdctx", mdctx_ ? kSizeOf_EVP_MD_CTX : 0);
36-
tracker->TrackFieldWithSize("md", digest_ ? md_len_ : 0);
40+
void Hash::Trace(cppgc::Visitor* visitor) const {
41+
CppgcMixin::Trace(visitor);
42+
visitor->TraceExternal(&mdctx_);
43+
visitor->TraceExternal(&digest_);
3744
}
3845

3946
#if OPENSSL_VERSION_MAJOR >= 3
@@ -321,7 +328,8 @@ void Hash::New(const FunctionCallbackInfo<Value>& args) {
321328
xof_md_len = Just<unsigned int>(args[1].As<Uint32>()->Value());
322329
}
323330

324-
Hash* hash = new Hash(env, args.This());
331+
Hash* hash = cppgc::MakeGarbageCollected<Hash>(
332+
env->isolate()->GetCppHeap()->GetAllocationHandle(), env, args.This());
325333
if (md == nullptr || !hash->HashInit(md, xof_md_len)) {
326334
return ThrowCryptoError(env, ERR_get_error(),
327335
"Digest method not supported");

src/crypto/crypto_hash.h

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,41 @@
33

44
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
55

6-
#include "base_object.h"
6+
#include "cppgc_helpers.h"
77
#include "crypto/crypto_keys.h"
88
#include "crypto/crypto_util.h"
99
#include "env.h"
1010
#include "memory_tracker.h"
1111
#include "v8.h"
12+
#include "cppgc/external.h"
1213

1314
namespace node {
1415
namespace crypto {
15-
class Hash final : public BaseObject {
16+
17+
class ExternalEVPCtx final : public cppgc::External {
18+
public:
19+
virtual size_t GetSize() const override {
20+
return ptr_ ? kSizeOf_EVP_MD_CTX : 0;
21+
}
22+
virtual const char* GetHumanReadableName() const override { return "EVP_MD_CTX"; }
23+
virtual void Trace(cppgc::Visitor* v) const override {}
24+
25+
EVP_MD_CTX* get() const { return ptr_.get(); }
26+
void reset(EVP_MD_CTX* ptr = nullptr) { ptr_.reset(ptr); }
27+
explicit operator bool() const { return !!ptr_; }
28+
29+
private:
30+
EVPMDCtxPointer ptr_{};
31+
};
32+
33+
class Hash final : public cppgc::GarbageCollected<Hash>,
34+
public cppgc::NameProvider,
35+
public CppgcMixin {
1636
public:
1737
static void Initialize(Environment* env, v8::Local<v8::Object> target);
1838
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
19-
20-
void MemoryInfo(MemoryTracker* tracker) const override;
21-
SET_MEMORY_INFO_NAME(Hash)
22-
SET_SELF_SIZE(Hash)
39+
const char* GetHumanReadableName() const final { return "Node / Hash"; }
40+
void Trace(cppgc::Visitor* visitor) const final;
2341

2442
bool HashInit(const EVP_MD* md, v8::Maybe<unsigned int> xof_md_len);
2543
bool HashUpdate(const char* data, size_t len);
@@ -28,15 +46,15 @@ class Hash final : public BaseObject {
2846
static void GetCachedAliases(const v8::FunctionCallbackInfo<v8::Value>& args);
2947
static void OneShotDigest(const v8::FunctionCallbackInfo<v8::Value>& args);
3048

49+
Hash(Environment* env, v8::Local<v8::Object> wrap);
50+
3151
protected:
3252
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
3353
static void HashUpdate(const v8::FunctionCallbackInfo<v8::Value>& args);
3454
static void HashDigest(const v8::FunctionCallbackInfo<v8::Value>& args);
3555

36-
Hash(Environment* env, v8::Local<v8::Object> wrap);
37-
3856
private:
39-
EVPMDCtxPointer mdctx_{};
57+
ExternalEVPCtx mdctx_{};
4058
unsigned int md_len_ = 0;
4159
ByteSource digest_;
4260
};

src/crypto/crypto_util.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
55

66
#include "async_wrap.h"
7+
#include "cppgc_helpers.h"
78
#include "env.h"
89
#include "node_errors.h"
910
#include "node_external_reference.h"
1011
#include "node_internals.h"
1112
#include "string_bytes.h"
1213
#include "util.h"
1314
#include "v8.h"
15+
#include "cppgc/external.h"
1416

1517
#include "ncrypto.h"
1618

@@ -101,7 +103,12 @@ void Decode(const v8::FunctionCallbackInfo<v8::Value>& args,
101103
void (*callback)(T*, const v8::FunctionCallbackInfo<v8::Value>&,
102104
const char*, size_t)) {
103105
T* ctx;
104-
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.This());
106+
if constexpr (std::is_base_of_v<BaseObject, T>) {
107+
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.This());
108+
} else {
109+
ctx = CppgcMixin::Unwrap<T>(args.This());
110+
if (ctx == nullptr) return;
111+
}
105112

106113
if (args[0]->IsString()) {
107114
StringBytes::InlineDecoder decoder;
@@ -192,7 +199,7 @@ T* MallocOpenSSL(size_t count) {
192199

193200
// A helper class representing a read-only byte array. When deallocated, its
194201
// contents are zeroed.
195-
class ByteSource {
202+
class ByteSource : public cppgc::External {
196203
public:
197204
class Builder {
198205
public:
@@ -307,6 +314,12 @@ class ByteSource {
307314
static ByteSource FromSecretKeyBytes(
308315
Environment* env, v8::Local<v8::Value> value);
309316

317+
virtual size_t GetSize() const {
318+
return size_;
319+
}
320+
virtual const char* GetHumanReadableName() const { return "Node / ByteSource"; }
321+
virtual void Trace(cppgc::Visitor* v) const {}
322+
310323
private:
311324
const void* data_ = nullptr;
312325
void* allocated_data_ = nullptr;

0 commit comments

Comments
 (0)