Skip to content

Commit

Permalink
src: fix heapSampling crash if sampleInterval is 0
Browse files Browse the repository at this point in the history
Caused by this [check](https://github.com/nodesource/nsolid/blob/3ea993c2e333ca13063bffecd518134b7849d692/deps/v8/src/profiler/sampling-heap-profiler.cc#L64)
in v8 source code.

PR-URL: #171
Reviewed-by: Rafael Gonzaga <rafael.nunu@hotmail.com>
Reviewed-by: Trevor Norris <trev.norris@gmail.com>
  • Loading branch information
santigimeno authored and trevnorris committed Aug 23, 2024
1 parent 4c32681 commit ffc7945
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/nsolid/nsolid_heap_snapshot.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions test/parallel/test-nsolid-heap-sampling-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const nsolid = require('nsolid');
const { internalBinding } = require('internal/test/binding');

const {
UV_EINVAL,
UV_ESRCH,
} = internalBinding('uv');

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit ffc7945

Please sign in to comment.