Skip to content

wip: crypto: use cppgc to manage Hash #51017

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
22 changes: 17 additions & 5 deletions src/crypto/crypto_hash.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "crypto/crypto_hash.h"
#include "async_wrap-inl.h"
#include "base_object-inl.h"
#include "cppgc/allocation.h"
#include "cppgc_helpers-inl.h"
#include "env-inl.h"
#include "memory_tracker-inl.h"
#include "string_bytes.h"
Expand Down Expand Up @@ -31,14 +33,23 @@ using v8::Object;
using v8::Uint32;
using v8::Value;

#ifdef ASSIGN_OR_RETURN_UNWRAP
#undef ASSIGN_OR_RETURN_UNWRAP
#endif

#define ASSIGN_OR_RETURN_UNWRAP ASSIGN_OR_RETURN_UNWRAP_CPPGC
namespace crypto {
Hash::Hash(Environment* env, Local<Object> wrap) : BaseObject(env, wrap) {
MakeWeak();
Hash::Hash(Environment* env, Local<Object> wrap) {
CppgcMixin::Wrap(this, env, wrap);
}

void Hash::Trace(cppgc::Visitor* visitor) const {
CppgcMixin::Trace(visitor);
}

void Hash::MemoryInfo(MemoryTracker* tracker) const {
tracker->TrackFieldWithSize("mdctx", mdctx_ ? kSizeOf_EVP_MD_CTX : 0);
tracker->TrackFieldWithSize("md", digest_ ? md_len_ : 0);
tracker->TrackFieldWithSize("mdctx", mdctx_ ? kSizeOf_EVP_MD_CTX : 0, "EVP_MD_CTX");
tracker->TrackFieldWithSize("md", digest_ ? md_len_ : 0, "ByteSource");
}

#if OPENSSL_VERSION_MAJOR >= 3
Expand Down Expand Up @@ -312,7 +323,8 @@ void Hash::New(const FunctionCallbackInfo<Value>& args) {
xof_md_len = Just<unsigned int>(args[1].As<Uint32>()->Value());
}

Hash* hash = new Hash(env, args.This());
Hash* hash = cppgc::MakeGarbageCollected<Hash>(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind adding a comment to here why we don't call new Hash but prefer the recommended change to here.

env->isolate()->GetCppHeap()->GetAllocationHandle(), env, args.This());
if (md == nullptr || !hash->HashInit(md, xof_md_len)) {
return ThrowCryptoError(env, ERR_get_error(),
"Digest method not supported");
Expand Down
17 changes: 9 additions & 8 deletions src/crypto/crypto_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

#include "base_object.h"
#include "cppgc_helpers.h"
#include "crypto/crypto_keys.h"
#include "crypto/crypto_util.h"
#include "env.h"
Expand All @@ -12,29 +12,30 @@

namespace node {
namespace crypto {
class Hash final : public BaseObject {

class Hash final : CPPGC_MIXIN(Hash) {
public:
SET_CPPGC_NAME(Hash)
void Trace(cppgc::Visitor* visitor) const final;
void MemoryInfo(MemoryTracker* tracker) const override;

static void Initialize(Environment* env, v8::Local<v8::Object> target);
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);

void MemoryInfo(MemoryTracker* tracker) const override;
SET_MEMORY_INFO_NAME(Hash)
SET_SELF_SIZE(Hash)

bool HashInit(const EVP_MD* md, v8::Maybe<unsigned int> xof_md_len);
bool HashUpdate(const char* data, size_t len);

static void GetHashes(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetCachedAliases(const v8::FunctionCallbackInfo<v8::Value>& args);
static void OneShotDigest(const v8::FunctionCallbackInfo<v8::Value>& args);

Hash(Environment* env, v8::Local<v8::Object> wrap);

protected:
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
static void HashUpdate(const v8::FunctionCallbackInfo<v8::Value>& args);
static void HashDigest(const v8::FunctionCallbackInfo<v8::Value>& args);

Hash(Environment* env, v8::Local<v8::Object> wrap);

private:
ncrypto::EVPMDCtxPointer mdctx_{};
unsigned int md_len_ = 0;
Expand Down
8 changes: 7 additions & 1 deletion src/crypto/crypto_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

#include "async_wrap.h"
#include "cppgc_helpers.h"
#include "env.h"
#include "node_errors.h"
#include "node_external_reference.h"
Expand Down Expand Up @@ -57,7 +58,12 @@ void Decode(const v8::FunctionCallbackInfo<v8::Value>& args,
void (*callback)(T*, const v8::FunctionCallbackInfo<v8::Value>&,
const char*, size_t)) {
T* ctx;
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.This());
if constexpr (std::is_base_of_v<BaseObject, T>) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pattern is likely to become fairly common assuming we get more use of the new mixins... likely a good idea to separate this unwrapping of T* out into a separate utility method.

ASSIGN_OR_RETURN_UNWRAP(&ctx, args.This());
} else {
ctx = CppgcMixin::Unwrap<T>(args.This());
if (ctx == nullptr) return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In what scenario can ctx be nullptr?

}

if (args[0]->IsString()) {
StringBytes::InlineDecoder decoder;
Expand Down
33 changes: 33 additions & 0 deletions test/pummel/test-heapdump-hash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';
require('../common');
const { validateByRetainingPath } = require('../common/heap');
const { createHash } = require('crypto');
const assert = require('assert');

// In case the bootstrap process creates any Hash objects, capture a snapshot first
// and save the initial length.
const originalNodes = validateByRetainingPath('Node / Hash', [
{ edge_name: 'mdctx' },
], true);

const count = 5;
const arr = [];
for (let i = 0; i < count; ++i) {
arr.push(createHash('sha1'));
}

const nodesWithCtx = validateByRetainingPath('Node / Hash', [
{ edge_name: 'mdctx', node_name: 'Node / EVP_MD_CTX' },
]);

assert.strictEqual(nodesWithCtx.length - originalNodes.length, count);

for (let i = 0; i < count; ++i) {
arr[i].update('test').digest('hex');
}

const nodesWithDigest = validateByRetainingPath('Node / Hash', [
{ edge_name: 'md', node_name: 'Node / ByteSource' },
]);

assert.strictEqual(nodesWithDigest.length - originalNodes.length, count);