Commit 301c332dd84 for nodejs

commit 301c332dd84ea96737d611f0c90fe92894ff20c4
Author: dasunNimantha <dasun4@pm.me>
Date:   Tue Nov 17 10:07:23 2026 +0530

    zlib: avoid flattening strings in crc32 fast path

    zlib.crc32() and zlib.crc32c() read string input through a V8 fast API
    callback that returns a value. Reading a non-flat (cons) string flattens
    it, which allocates on the JS heap. If that allocation triggers a full
    GC that invalidates the optimized caller, V8 has to lazily deoptimize it
    after the fast call returns, which it does not support for fast calls
    with a return value: the process aborts with "Lazy deopt after a fast
    API call with return value is unsupported".

    Strings now go through a regular binding. The fast bindings are limited
    to ArrayBufferViews, which are read without allocating.

    Refs: https://github.com/nodejs/node/issues/66870
    Refs: https://github.com/nodejs/node/pull/67136
    Signed-off-by: dasunNimantha <dasun4@pm.me>
    PR-URL: https://github.com/nodejs/node/pull/67536
    Reviewed-By: James M Snell <jasnell@gmail.com>
    Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>

diff --git a/lib/zlib.js b/lib/zlib.js
index 2230913d534..2a6101975cb 100644
--- a/lib/zlib.js
+++ b/lib/zlib.js
@@ -61,7 +61,12 @@ const {
   isUint8Array,
 } = require('internal/util/types');
 const binding = internalBinding('zlib');
-const { crc32: crc32Native, crc32c: crc32cNative } = binding;
+const {
+  crc32: crc32String,
+  crc32c: crc32cString,
+  crc32Buffer,
+  crc32cBuffer,
+} = binding;
 const assert = require('internal/assert');
 const {
   Buffer,
@@ -1276,7 +1281,10 @@ function crc32(data, value = 0) {
   validateUint32(value, 'value');
   // Coerce -0 to +0.
   value += 0;
-  return crc32Native(data, value);
+  // Strings use a regular binding: reading them may flatten (allocate),
+  // which must not happen inside a value-returning fast API call.
+  return typeof data === 'string' ?
+    crc32String(data, value) : crc32Buffer(data, value);
 }

 function crc32c(data, value = 0) {
@@ -1286,7 +1294,8 @@ function crc32c(data, value = 0) {
   validateUint32(value, 'value');
   // Coerce -0 to +0.
   value += 0;
-  return crc32cNative(data, value);
+  return typeof data === 'string' ?
+    crc32cString(data, value) : crc32cBuffer(data, value);
 }

 // Legacy alias on the C++ wrapper object. This is not public API, so we may
diff --git a/src/node_zlib.cc b/src/node_zlib.cc
index 463b68c2150..b4dd23e5ae0 100644
--- a/src/node_zlib.cc
+++ b/src/node_zlib.cc
@@ -2137,6 +2137,14 @@ static void CRC32(const FunctionCallbackInfo<Value>& args) {
   args.GetReturnValue().Set(CRC32Impl(args.GetIsolate(), args[0], value));
 }

+// Strings use CRC32 above: the fast API path must not flatten them.
+static void CRC32Buffer(const FunctionCallbackInfo<Value>& args) {
+  CHECK(args[0]->IsArrayBufferView());
+  CHECK(args[1]->IsUint32());
+  uint32_t value = args[1].As<v8::Uint32>()->Value();
+  args.GetReturnValue().Set(CRC32Impl(args.GetIsolate(), args[0], value));
+}
+
 static uint32_t FastCRC32(v8::Local<v8::Value> receiver,
                           v8::Local<v8::Value> data,
                           uint32_t value,
@@ -2144,7 +2152,7 @@ static uint32_t FastCRC32(v8::Local<v8::Value> receiver,
                           v8::FastApiCallbackOptions& options) {
   TRACK_V8_FAST_API_CALL("zlib.crc32");
   v8::HandleScope handle_scope(options.isolate);
-  CHECK(data->IsArrayBufferView() || data->IsString());
+  CHECK(data->IsArrayBufferView());
   return CRC32Impl(options.isolate, data, value);
 }

@@ -2167,6 +2175,13 @@ static void CRC32C(const FunctionCallbackInfo<Value>& args) {
   args.GetReturnValue().Set(CRC32CImpl(args.GetIsolate(), args[0], value));
 }

+static void CRC32CBuffer(const FunctionCallbackInfo<Value>& args) {
+  CHECK(args[0]->IsArrayBufferView());
+  CHECK(args[1]->IsUint32());
+  uint32_t value = args[1].As<v8::Uint32>()->Value();
+  args.GetReturnValue().Set(CRC32CImpl(args.GetIsolate(), args[0], value));
+}
+
 static uint32_t FastCRC32C(v8::Local<v8::Value> receiver,
                            v8::Local<v8::Value> data,
                            uint32_t value,
@@ -2174,7 +2189,7 @@ static uint32_t FastCRC32C(v8::Local<v8::Value> receiver,
                            v8::FastApiCallbackOptions& options) {
   TRACK_V8_FAST_API_CALL("zlib.crc32c");
   v8::HandleScope handle_scope(options.isolate);
-  CHECK(data->IsArrayBufferView() || data->IsString());
+  CHECK(data->IsArrayBufferView());
   return CRC32CImpl(options.isolate, data, value);
 }

@@ -2192,8 +2207,12 @@ void Initialize(Local<Object> target,
   MakeClass<ZstdCompressStream>::Make(env, target, "ZstdCompress");
   MakeClass<ZstdDecompressStream>::Make(env, target, "ZstdDecompress");

-  SetFastMethodNoSideEffect(context, target, "crc32", CRC32, &fast_crc32_);
-  SetFastMethodNoSideEffect(context, target, "crc32c", CRC32C, &fast_crc32c_);
+  SetMethodNoSideEffect(context, target, "crc32", CRC32);
+  SetMethodNoSideEffect(context, target, "crc32c", CRC32C);
+  SetFastMethodNoSideEffect(
+      context, target, "crc32Buffer", CRC32Buffer, &fast_crc32_);
+  SetFastMethodNoSideEffect(
+      context, target, "crc32cBuffer", CRC32CBuffer, &fast_crc32c_);
   target->Set(env->context(),
               FIXED_ONE_BYTE_STRING(env->isolate(), "ZLIB_VERSION"),
               FIXED_ONE_BYTE_STRING(env->isolate(), ZLIB_VERSION)).Check();
@@ -2206,8 +2225,10 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
   MakeClass<ZstdCompressStream>::Make(registry);
   MakeClass<ZstdDecompressStream>::Make(registry);
   registry->Register(CRC32);
+  registry->Register(CRC32Buffer);
   registry->Register(fast_crc32_);
   registry->Register(CRC32C);
+  registry->Register(CRC32CBuffer);
   registry->Register(fast_crc32c_);
 }

diff --git a/typings/internalBinding/zlib.d.ts b/typings/internalBinding/zlib.d.ts
index 42833f10665..081b0f0a1eb 100644
--- a/typings/internalBinding/zlib.d.ts
+++ b/typings/internalBinding/zlib.d.ts
@@ -50,4 +50,6 @@ export interface ZlibBinding {
   ZstdDecompress: typeof InternalZlibBinding.ZstdDecompress;
   crc32(data: string | ArrayBufferView, value: number): number;
   crc32c(data: string | ArrayBufferView, value: number): number;
+  crc32Buffer(data: ArrayBufferView, value: number): number;
+  crc32cBuffer(data: ArrayBufferView, value: number): number;
 }