From b4262287bfe670b7bfbaeb8d6a25444533e27635 Mon Sep 17 00:00:00 2001 From: Santiago Gimeno Date: Fri, 23 Aug 2024 16:36:18 +0200 Subject: [PATCH] src: fix heapSampling crash if sampleInterval is 0 Caused by this [check](https://github.com/nodesource/nsolid/blob/3ea993c2e333ca13063bffecd518134b7849d692/deps/v8/src/profiler/sampling-heap-profiler.cc#L64) in v8 source code. --- src/nsolid/nsolid_heap_snapshot.cc | 5 +++++ test/parallel/test-nsolid-heap-sampling-stream.js | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/nsolid/nsolid_heap_snapshot.cc b/src/nsolid/nsolid_heap_snapshot.cc index 32cb36190f..aaf916e4c7 100644 --- a/src/nsolid/nsolid_heap_snapshot.cc +++ b/src/nsolid/nsolid_heap_snapshot.cc @@ -19,6 +19,11 @@ int NSolidHeapSnapshot::StartSamplingProfiler( uint64_t duration, internal::user_data data, Snapshot::snapshot_proxy_sig proxy) { + // Using a sampleInterval of 0 causes a v8 crash. + if (sample_interval == 0) { + return UV_EINVAL; + } + uint64_t thread_id = envinst->thread_id(); uint64_t snaphot_id = in_progress_timers_.fetch_add(1, std::memory_order_relaxed); diff --git a/test/parallel/test-nsolid-heap-sampling-stream.js b/test/parallel/test-nsolid-heap-sampling-stream.js index 47de047f4b..f0cca8a763 100644 --- a/test/parallel/test-nsolid-heap-sampling-stream.js +++ b/test/parallel/test-nsolid-heap-sampling-stream.js @@ -7,6 +7,7 @@ const nsolid = require('nsolid'); const { internalBinding } = require('internal/test/binding'); const { + UV_EINVAL, UV_ESRCH, } = internalBinding('uv'); @@ -99,6 +100,17 @@ const { })); } +{ + // Using a sampleInterval of 0 should result in an error as it causes a crash + // on v8. + const opts = { sampleInterval: 0 }; + const stream = nsolid.heapSamplingStream(0, 12000, opts); + stream.on('error', common.mustCall((err) => { + assert.strictEqual(err.message, 'Heap sampling could not be started'); + assert.strictEqual(err.code, UV_EINVAL); + })); +} + { let profile = ''; const stream = nsolid.heapSamplingStream(0, 1200);