v8/test/mjsunit/code-coverage-utils.js
Nikolaos Papaspyrou 60dfddf03c [heap][test] Fix code coverage tests for conservative stack scanning
Code coverage tests invoke garbage collection, to test that coverage
data is not reclaimed by the garbage collector and that the native
%DebugTogglePreciseCoverage works as intended. One of them tests that
garbage collection indeed reclaims the coverage data, if the above
native is not used. When conservative stack scanning is used, this may
fail.

This CL fixes the tests, ensuring that a precise garbage collection
will be invoked, without scanning the stack. To achieve this, the
garbage collection is invoked not with %CollectGarbage but by using
--expose-gc and the asynchronous execution mode, which ensures that
it will be invoked from the event loop without a stack.

Bug: v8:13257
Change-Id: Id44ef0d442bfd0a8afda282c3345e5ebeb239356
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3968708
Reviewed-by: Omer Katz <omerkatz@chromium.org>
Reviewed-by: Michael Lippautz <mlippautz@chromium.org>
Commit-Queue: Nikolaos Papaspyrou <nikolaos@chromium.org>
Cr-Commit-Position: refs/heads/main@{#83851}
2022-10-21 13:09:40 +00:00

71 lines
2.3 KiB
JavaScript

// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --allow-natives-syntax
// Flags: --expose-gc
let TestCoverage;
let TestCoverageNoGC;
let nop;
let gen;
!function() {
function GetCoverage(source) {
for (var script of %DebugCollectCoverage()) {
if (script.script === source) return script;
}
return undefined;
};
async function TestCoverageInternal(
name, source, expectation, collect_garbage, prettyPrintResults) {
source = source.trim();
eval(source);
// We need to invoke GC asynchronously, so that it doesn't need to scan
// the stack. Otherwise, some objects may not be reclaimed because of
// conservative stack scanning and the tests may fail.
if (collect_garbage) await gc({ type: 'major', execution: 'async' });
var covfefe = GetCoverage(source);
var stringified_result = JSON.stringify(covfefe);
var stringified_expectation = JSON.stringify(expectation);
const mismatch = stringified_result != stringified_expectation;
if (mismatch) {
console.log(stringified_result.replace(/[}],[{]/g, "},\n {"));
}
if (prettyPrintResults) {
console.log("=== Coverage Expectation ===")
for (const {start,end,count} of expectation) {
console.log(`Range [${start}, ${end}) (count: ${count})`);
console.log(source.substring(start, end));
}
console.log("=== Coverage Results ===")
for (const {start,end,count} of covfefe) {
console.log(`Range [${start}, ${end}) (count: ${count})`);
console.log(source.substring(start, end));
}
console.log("========================")
}
assertEquals(stringified_expectation, stringified_result, name + " failed");
};
TestCoverage = async function(name, source, expectation, prettyPrintResults) {
return TestCoverageInternal(name, source, expectation, true,
prettyPrintResults);
};
TestCoverageNoGC = function(name, source, expectation, prettyPrintResults) {
return TestCoverageInternal(name, source, expectation, false,
prettyPrintResults);
};
nop = function() {};
gen = function*() {
yield 1;
yield 2;
yield 3;
};
}();