add cpp coverage (#54637)

Summary:
Changelog: [Internal]

Pull Request resolved: https://github.com/facebook/react-native/pull/54637

Add c++ code-coverage mode for Fantom and set up custom output path for it.

Reviewed By: sammy-SC

Differential Revision: D87548065

fbshipit-source-id: 5462582ca467051f7e9a155fd82bfb7045380097
This commit is contained in:
Andrew Datsenko authored and meta-codesync[bot] committed 2026-03-03 09:28:25 -08:00
1 parent c4a982920d
commit e1c296869d
7 files changed
+395 -86

No files matched your search

+24 -18
View File
@@ -24,22 +24,22 @@ import fs from 'fs';
import os from 'os';
import path from 'path';
type TesterOptions = Readonly<{
isOptimizedMode: boolean,
type HermescOptions = Readonly<{
enableCoverage: boolean,
enableOptimized: boolean,
hermesVariant: HermesVariant,
}>;
function getHermesCompilerPath({
isOptimizedMode,
enableOptimized,
hermesVariant,
}: TesterOptions): string {
}: HermescOptions): string {
return path.join(
NATIVE_BUILD_OUTPUT_PATH,
`hermesc-${(hermesVariant as string).toLowerCase()}-${isOptimizedMode ? 'opt' : 'dev'}`,
`hermesc-${(hermesVariant as string).toLowerCase()}-${enableOptimized ? 'opt' : 'dev'}`,
);
}
export function build(options: TesterOptions): void {
export function build(options: HermescOptions): void {
const destPath = getHermesCompilerPath(options);
if (fs.existsSync(destPath)) {
return;
@@ -54,14 +54,20 @@ export function build(options: TesterOptions): void {
const destTmpPath = destPath + '-' + Date.now() + '-' + process.pid;
try {
const result = runBuck2Sync([
'build',
...getBuckModesForPlatform(options.isOptimizedMode),
...getBuckOptionsForHermes(options.hermesVariant),
getHermesCompilerTarget(options.hermesVariant),
'--out',
tmpPath,
]);
const result = runBuck2Sync(
[
'build',
...getBuckModesForPlatform({
enableOptimized: options.enableOptimized,
enableCoverage: options.enableCoverage,
}),
...getBuckOptionsForHermes(options.hermesVariant),
getHermesCompilerTarget(options.hermesVariant),
'--out',
tmpPath,
],
{},
);
if (result.status !== 0) {
throw new Error(getDebugInfoFromCommandResult(result));
@@ -75,7 +81,7 @@ export function build(options: TesterOptions): void {
// Remove extended attributes to avoid "Operation not permitted" errors
// when copying to EdenFS/NFS-backed directories on macOS
if (os.platform() === 'darwin') {
runCommandSync('xattr', ['-rc', tmpPath]);
runCommandSync('xattr', ['-rc', tmpPath], {});
}
fs.copyFileSync(tmpPath, destTmpPath);
@@ -104,11 +110,11 @@ export function build(options: TesterOptions): void {
export function run(
args: ReadonlyArray<string>,
options: TesterOptions,
options: HermescOptions,
): SyncCommandResult {
if (!isCI) {
build(options);
}
return runCommandSync(getHermesCompilerPath(options), args);
return runCommandSync(getHermesCompilerPath(options), args, {});
}
+54 -31
View File
@@ -8,7 +8,11 @@
* @format
*/
import type {AsyncCommandResult, HermesVariant} from '../utils';
import type {
AsyncCommandResult,
EnvironmentOverrides,
HermesVariant,
} from '../utils';
import {debugCpp, isCI, profileCpp} from '../EnvironmentOptions';
import {CPP_TRACES_OUTPUT_PATH, NATIVE_BUILD_OUTPUT_PATH} from '../paths';
@@ -29,21 +33,22 @@ const FANTOM_TESTER_BUCK_TARGET =
'fbsource//xplat/js/react-native-github/private/react-native-fantom/tester:tester';
type TesterOptions = Readonly<{
isOptimizedMode: boolean,
enableCoverage: boolean,
enableOptimized: boolean,
hermesVariant: HermesVariant,
}>;
function getFantomTesterPath({
isOptimizedMode,
export function getFantomTesterPath({
hermesVariant,
...options
}: TesterOptions): string {
return path.join(
NATIVE_BUILD_OUTPUT_PATH,
`fantom-tester-${(hermesVariant as string).toLowerCase()}-${isOptimizedMode ? 'opt' : 'dev'}`,
`fantom-tester-${(hermesVariant as string).toLowerCase()}-${options.enableOptimized ? 'opt' : 'dev'}${options.enableCoverage ? '-coverage' : ''}`,
);
}
export function build(options: TesterOptions): void {
export function build(options: TesterOptions, env: EnvironmentOverrides): void {
const destPath = getFantomTesterPath(options);
if (fs.existsSync(destPath)) {
return;
@@ -58,14 +63,23 @@ export function build(options: TesterOptions): void {
const destTmpPath = destPath + '-' + Date.now() + '-' + process.pid;
try {
const result = runBuck2Sync([
'build',
...getBuckModesForPlatform(options.isOptimizedMode),
...getBuckOptionsForHermes(options.hermesVariant),
FANTOM_TESTER_BUCK_TARGET,
'--out',
tmpPath,
]);
const result = runBuck2Sync(
[
'build',
...getBuckModesForPlatform({
enableOptimized: options.enableOptimized,
enableCoverage: options.enableCoverage,
}),
...getBuckOptionsForHermes(options.hermesVariant),
FANTOM_TESTER_BUCK_TARGET,
'--out',
tmpPath,
],
env,
{
withFDB: false,
},
);
if (result.status !== 0) {
throw new Error(getDebugInfoFromCommandResult(result));
@@ -79,7 +93,7 @@ export function build(options: TesterOptions): void {
// Remove extended attributes to avoid "Operation not permitted" errors
// when copying to EdenFS/NFS-backed directories on macOS
if (os.platform() === 'darwin') {
runCommandSync('xattr', ['-rc', tmpPath]);
runCommandSync('xattr', ['-rc', tmpPath], {});
}
fs.copyFileSync(tmpPath, destTmpPath);
@@ -109,6 +123,7 @@ export function build(options: TesterOptions): void {
export function run(
args: ReadonlyArray<string>,
options: TesterOptions,
env: EnvironmentOverrides,
): AsyncCommandResult {
if (isCI && debugCpp) {
throw new Error('Cannot run Fantom with C++ debugging on CI');
@@ -119,19 +134,23 @@ export function run(
}
if (!isCI && !debugCpp) {
build(options);
build(options, env);
}
if (debugCpp) {
return runBuck2(
[
'run',
...getBuckModesForPlatform(options.isOptimizedMode),
...getBuckModesForPlatform({
enableOptimized: options.enableOptimized,
enableCoverage: options.enableCoverage,
}),
...getBuckOptionsForHermes(options.hermesVariant),
FANTOM_TESTER_BUCK_TARGET,
'--',
...args,
],
env,
{
withFDB: true,
},
@@ -156,19 +175,23 @@ export function run(
// -g: enable call-graph (stack traces)
// -F 997: sample at 997 Hz (prime number to avoid aliasing)
// --call-graph dwarf: use DWARF for accurate stack traces
const result = runCommand('perf', [
'record',
'-g',
'-F',
'997',
'--call-graph',
'dwarf',
'-o',
perfOutputPath,
'--',
testerPath,
...args,
]);
const result = runCommand(
'perf',
[
'record',
'-g',
'-F',
'997',
'--call-graph',
'dwarf',
'-o',
perfOutputPath,
'--',
testerPath,
...args,
],
{},
);
// Log the output path after the command starts
console.log(
@@ -179,5 +202,5 @@ export function run(
return result;
}
return runCommand(testerPath, args);
return runCommand(testerPath, args, env);
}
+12 -4
View File
@@ -8,6 +8,8 @@
* @format
*/
import type {EnvironmentOverrides} from '../utils';
import {createBundle} from '../bundling';
import {isCI} from '../EnvironmentOptions';
import {build as buildHermesCompiler} from '../executables/hermesc';
@@ -37,7 +39,10 @@ async function tryOrLog(
}
}
export default async function build(): Promise<void> {
export default async function build(
enableCoverage: boolean,
env: EnvironmentOverrides,
): Promise<void> {
try {
fs.rmSync(NATIVE_BUILD_OUTPUT_PATH, {recursive: true});
} catch {}
@@ -45,10 +50,13 @@ export default async function build(): Promise<void> {
fs.mkdirSync(NATIVE_BUILD_OUTPUT_PATH, {recursive: true});
if (isCI) {
for (const isOptimizedMode of [false, true]) {
for (const enableOptimized of [false, true]) {
for (const hermesVariant of HermesVariant.members()) {
buildFantomTester({isOptimizedMode, hermesVariant});
buildHermesCompiler({isOptimizedMode, hermesVariant});
buildFantomTester(
{enableOptimized, hermesVariant, enableCoverage},
env,
);
buildHermesCompiler({enableOptimized, hermesVariant, enableCoverage});
}
}
@@ -12,6 +12,7 @@ import type {DevToolLauncher} from '@react-native/dev-middleware';
import type {ConfigT} from 'metro-config';
import {isOSS, validateEnvironmentVariables} from '../EnvironmentOptions';
import {getTestBuildOutputPath} from '../paths';
import build from './build';
import {createDevMiddleware} from '@react-native/dev-middleware';
import connect from 'connect';
@@ -20,7 +21,10 @@ import {Server} from 'net';
import path from 'path';
export default async function globalSetup(
globalConfig: {...},
globalConfig: {
collectCoverage: boolean,
...
},
projectConfig: {...},
): Promise<void> {
process.env.__FANTOM_RUN_ID__ ??= `run-${Date.now()}`;
@@ -30,7 +34,11 @@ export default async function globalSetup(
await startMetroServer();
if (!isOSS) {
await build();
await build(globalConfig.collectCoverage, {
LLVM_PROFILE_FILE: globalConfig.collectCoverage
? getTestBuildOutputPath() + 'fantom.profraw'
: undefined,
});
}
}
@@ -0,0 +1,188 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
*/
import type {
LLVMFileData,
LLVMFunctionData,
} from '../coverage/convertLLVMCoverage';
import type {FileCoverageData} from '../coverage/types.flow';
import convertLLVMCoverage from '../coverage/convertLLVMCoverage';
import * as EnvironmentOptions from '../EnvironmentOptions';
import {
getDebugInfoFromCommandResult,
printConsoleLog,
runCommand,
runCommandSync,
} from '../utils';
import fs from 'fs';
import os from 'os';
import path from 'path';
const FANTOM_DIR = path.resolve(__dirname, '..', '..');
const RN_ROOT = path.resolve(FANTOM_DIR, '..', '..');
let _repoRoot: ?string = null;
function getRepoRoot(): string {
if (_repoRoot != null) {
return _repoRoot;
}
const res = runCommandSync('hg', ['root'], {});
if (res.status !== 0) {
throw new Error(
`Failed to get repo root: ${getDebugInfoFromCommandResult(res)}`,
);
}
_repoRoot = res.stdout.trim();
return _repoRoot;
}
function getLLVMCommand(command: 'llvm-profdata' | 'llvm-cov'): string {
return path.join(
getRepoRoot(),
'arvr',
'third-party',
'toolchains',
'platform010',
'build',
'llvm-fb',
'17',
'bin',
command,
);
}
export default async function processLLVMCoverage(
profrawFilePath: string,
binaryPath: string,
): Promise<{[string]: FileCoverageData}> {
if (!fs.existsSync(profrawFilePath)) {
printConsoleLog({
type: 'console-log',
level: 'warn',
message: `No .profraw file found at ${profrawFilePath}, skipping C++ coverage`,
});
return {};
}
const profdataFilePath = profrawFilePath.replace('.profraw', '.profdata');
const coverageJsonPath = profrawFilePath.replace('.profraw', '-cpp.json');
if (os.platform() !== 'linux') {
printConsoleLog({
type: 'console-log',
level: 'warn',
message: `C++ coverage is only supported on Linux`,
});
return {};
}
if (EnvironmentOptions.isOSS) {
printConsoleLog({
type: 'console-log',
level: 'warn',
message: `C++ coverage is not supported in OSS mode`,
});
return {};
}
try {
const mergeResult = runCommandSync(
getLLVMCommand('llvm-profdata'),
['merge', '-sparse', profrawFilePath, '-o', profdataFilePath],
{},
);
if (mergeResult.status !== 0) {
printConsoleLog({
type: 'console-log',
level: 'error',
message: `Failed to merge .profraw file`,
});
return {};
}
const exportCommand = runCommand(
'sh',
[
'-c',
`${getLLVMCommand('llvm-cov')} ${[
'export',
`-instr-profile=${profdataFilePath}`,
'-format=text',
`-path-equivalence=/re_cwd,${getRepoRoot()}`,
binaryPath,
'--sources',
RN_ROOT,
]
.map(arg => `"${arg}"`)
.join(' ')} > "${coverageJsonPath}"`,
],
{},
);
const exportResult = await exportCommand.done;
if (exportResult.status !== 0) {
printConsoleLog({
type: 'console-log',
level: 'error',
message: `Failed to export coverage data: ${getDebugInfoFromCommandResult(exportResult)}`,
});
return {};
}
const coverageData = fs.readFileSync(coverageJsonPath, 'utf8');
// Parse the JSON data
const {data} = JSON.parse(coverageData) as {
data: Array<{
files: Array<LLVMFileData>,
functions: Array<LLVMFunctionData>,
}>,
};
// Group functions by filename
const functionsMap: Map<string, Array<LLVMFunctionData>> = new Map();
for (const {functions} of data) {
for (const func of functions) {
for (const filename of func.filenames) {
let funcList = functionsMap.get(filename);
if (funcList == null) {
funcList = [];
functionsMap.set(filename, funcList);
}
funcList.push(func);
}
}
}
const result: {[filename: string]: FileCoverageData} = {};
for (const {files} of data) {
for (const file of files) {
const funcList = functionsMap.get(file.filename) ?? [];
const fileCoverage = convertLLVMCoverage(file, funcList);
fileCoverage.path = fileCoverage.path.replace(
/^\/re_cwd/,
getRepoRoot(),
);
result[fileCoverage.path] = fileCoverage;
}
}
return result;
} catch (error) {
printConsoleLog({
type: 'console-log',
level: 'error',
message: `Error processing C++ coverage: ${String(error)}`,
});
return {};
}
}
+62 -18
View File
@@ -19,6 +19,7 @@ import type {CoverageMap} from './coverage/types.flow';
import type {
AsyncCommandResult,
ConsoleLogMessage,
EnvironmentOverrides,
HermesVariant,
} from './utils';
@@ -28,9 +29,13 @@ import {shouldCollectCoverage} from './coverageUtils';
import entrypointTemplate from './entrypoint-template';
import * as EnvironmentOptions from './EnvironmentOptions';
import {run as runHermesCompiler} from './executables/hermesc';
import {run as runFantomTester} from './executables/tester';
import {
getFantomTesterPath,
run as runFantomTester,
} from './executables/tester';
import formatFantomConfig from './formatFantomConfig';
import getFantomTestConfigs from './getFantomTestConfigs';
import processLLVMCoverage from './llvm/processLLVMCoverage';
import {
JS_HEAP_SNAPSHOTS_OUTPUT_PATH,
JS_TRACES_OUTPUT_PATH,
@@ -163,18 +168,20 @@ async function processRNTesterCommandResult(
function generateBytecodeBundle({
sourcePath,
bytecodePath,
isOptimizedMode,
enableOptimized,
hermesVariant,
enableCoverage,
}: {
sourcePath: string,
bytecodePath: string,
isOptimizedMode: boolean,
enableCoverage: boolean,
enableOptimized: boolean,
hermesVariant: HermesVariant,
sourcePath: string,
}): void {
const hermesCompilerCommandResult = runHermesCompiler(
[
'-emit-binary',
isOptimizedMode ? '-O' : null,
enableOptimized ? '-O' : null,
'-max-diagnostic-width',
'80',
'-out',
@@ -182,8 +189,9 @@ function generateBytecodeBundle({
sourcePath,
].filter(Boolean),
{
isOptimizedMode,
enableOptimized,
hermesVariant,
enableCoverage,
},
);
@@ -208,7 +216,7 @@ module.exports = async function runTest(
runtime: {...},
testPath: string,
): unknown {
let coverageMap: CoverageMap | void;
let coverageMap: CoverageMap = {};
const snapshotResolver = await buildSnapshotResolver(config);
const snapshotPath = snapshotResolver.resolveSnapshotPath(testPath);
const snapshotState = new SnapshotState(snapshotPath, {
@@ -326,6 +334,7 @@ module.exports = async function runTest(
);
const testJSBundlePath = entrypointPath + '.bundle.js';
const testBytecodeBundlePath = testJSBundlePath + '.hbc';
const testCoveragePath = entrypointPath + '.profraw';
fs.mkdirSync(path.dirname(entrypointPath), {recursive: true});
fs.writeFileSync(entrypointPath, entrypointContents, 'utf8');
@@ -335,6 +344,12 @@ module.exports = async function runTest(
path.basename(testJSBundlePath, '.js') + '.map',
);
const collectCoverage = shouldCollectCoverage(
testPath,
testContents,
globalConfig,
);
const bundleOptions = {
testPath,
entry: entrypointPath,
@@ -344,11 +359,7 @@ module.exports = async function runTest(
sourceMap: true,
sourceMapUrl: sourceMapPath,
customTransformOptions: {
collectCoverage: shouldCollectCoverage(
testPath,
testContents,
globalConfig,
),
collectCoverage,
},
};
@@ -361,7 +372,8 @@ module.exports = async function runTest(
generateBytecodeBundle({
sourcePath: testJSBundlePath,
bytecodePath: testBytecodeBundlePath,
isOptimizedMode: testConfig.isJsOptimized,
enableCoverage: collectCoverage,
enableOptimized: testConfig.isJsOptimized,
hermesVariant: testConfig.hermesVariant,
});
}
@@ -382,15 +394,22 @@ module.exports = async function runTest(
);
}
const env: EnvironmentOverrides = {
LLVM_PROFILE_FILE: collectCoverage ? testCoveragePath : undefined,
};
const rnTesterOptions = {
enableOptimized: testConfig.isNativeOptimized,
hermesVariant: testConfig.hermesVariant,
enableCoverage: collectCoverage,
};
const rnTesterCommandResult = EnvironmentOptions.isOSS
? runCommand(
path.join(__dirname, '..', 'build', 'tester', 'fantom_tester'),
rnTesterCommandArgs,
env,
)
: runFantomTester(rnTesterCommandArgs, {
isOptimizedMode: testConfig.isNativeOptimized,
hermesVariant: testConfig.hermesVariant,
});
: runFantomTester(rnTesterCommandArgs, rnTesterOptions, env);
const [processedResult, benchmarkResult] =
await processRNTesterCommandResult(rnTesterCommandResult);
@@ -468,7 +487,32 @@ module.exports = async function runTest(
testResultsByConfig.push(testResults);
coverageMap = processedResult.coverageMap;
coverageMap = processedResult.coverageMap || {};
if (globalConfig.collectCoverage) {
try {
const binaryPath = EnvironmentOptions.isOSS
? path.join(__dirname, '..', 'build', 'tester', 'fantom_tester')
: getFantomTesterPath(rnTesterOptions);
const cppCoverageMap = await processLLVMCoverage(
testCoveragePath,
binaryPath,
);
Object.entries(cppCoverageMap || {}).forEach(
([filename, fileCoverage]) => {
coverageMap[filename] = fileCoverage;
},
);
} catch (error) {
printConsoleLog({
type: 'console-log',
level: 'warn',
message: `Failed to process C++ coverage: ${String(error)} ${String(error.stack)}`,
});
}
}
}
const endTime = Date.now();
+45 -13
View File
@@ -24,6 +24,10 @@ export enum HermesVariant {
StaticHermesExperimental, // Static Hermes Trunk
}
export type EnvironmentOverrides = {
LLVM_PROFILE_FILE?: string,
};
export function getBuckOptionsForHermes(
variant: HermesVariant,
): ReadonlyArray<string> {
@@ -51,16 +55,16 @@ export function getHermesCompilerTarget(variant: HermesVariant): string {
}
}
export function getBuckModesForPlatform(
enableRelease: boolean = false,
): ReadonlyArray<string> {
let modes = ['@//xplat/mode/react-native/granite'];
if (enableRelease) {
modes.push('@//xplat/mode/hermes/opt');
}
export function getBuckModesForPlatform({
enableCoverage,
enableOptimized,
}: {
enableCoverage: boolean,
enableOptimized: boolean,
}): $ReadOnlyArray<string> {
let mode = enableCoverage ? 'code-coverage' : enableOptimized ? 'opt' : 'dev';
let mode = enableRelease ? 'opt' : 'dev';
if (enableRelease) {
if (enableOptimized) {
if (EnvironmentOptions.enableASAN || EnvironmentOptions.enableTSAN) {
printConsoleLog({
type: 'console-log',
@@ -105,8 +109,21 @@ export function getBuckModesForPlatform(
throw new Error(`Unsupported platform: ${os.platform()}`);
}
modes.push(osPlatform);
return modes;
const result: Array<string> = [
'@//xplat/mode/react-native/granite',
osPlatform,
];
if (enableCoverage) {
result.push(
'-c',
'code_coverage.enabled=filtered',
'-c',
'code_coverage.folder_path_filter=xplat/js/react-native-github',
);
}
return result;
}
export type AsyncCommandResult = {
@@ -137,9 +154,19 @@ function maybeLogCommand(command: string, args: ReadonlyArray<string>): void {
}
}
function toEnv(env: EnvironmentOverrides): {[string]: string} {
return Object.keys(env).reduce<{[string]: string}>((acc, key) => {
if (env[key] != null) {
acc[key] = env[key];
}
return acc;
}, {});
}
export function runCommand(
command: string,
args: ReadonlyArray<string>,
env: EnvironmentOverrides,
): AsyncCommandResult {
maybeLogCommand(command, args);
@@ -151,6 +178,7 @@ export function runCommand(
encoding: 'utf8',
env: {
...process.env,
...toEnv(env),
PATH: `/usr/local/bin:/usr/bin:${process.env.PATH ?? ''}`,
},
},
@@ -184,6 +212,7 @@ export function runCommand(
export function runCommandSync(
command: string,
args: ReadonlyArray<string>,
env: EnvironmentOverrides,
): SyncCommandResult {
maybeLogCommand(command, args);
@@ -191,6 +220,7 @@ export function runCommandSync(
encoding: 'utf8',
env: {
...process.env,
...toEnv(env),
PATH: `/usr/local/bin:/usr/bin:${process.env.PATH ?? ''}`,
},
});
@@ -249,6 +279,7 @@ function getCommandAndArgsWithFDB(
export function runBuck2(
args: Array<string>,
env: EnvironmentOverrides,
options?: {withFDB: boolean},
): AsyncCommandResult {
const [actualCommand, actualArgs] = getCommandAndArgsWithFDB(
@@ -256,11 +287,12 @@ export function runBuck2(
processArgsForBuck(args),
options?.withFDB ?? false,
);
return runCommand(actualCommand, actualArgs);
return runCommand(actualCommand, actualArgs, env);
}
export function runBuck2Sync(
args: Array<string>,
env: EnvironmentOverrides,
options?: {withFDB: boolean},
): SyncCommandResult {
const [actualCommand, actualArgs] = getCommandAndArgsWithFDB(
@@ -268,7 +300,7 @@ export function runBuck2Sync(
processArgsForBuck(args),
options?.withFDB ?? false,
);
return runCommandSync(actualCommand, actualArgs);
return runCommandSync(actualCommand, actualArgs, env);
}
function processArgsForBuck(args: Array<string>): Array<string> {