Skip to content

Commit 63b0232

Browse files
vscode: bump introspector and add command for getting optimal targets (#12942)
Signed-off-by: David Korczynski <[email protected]>
1 parent c42856e commit 63b0232

File tree

6 files changed

+155
-69
lines changed

6 files changed

+155
-69
lines changed

tools/vscode-extension/package.json

+5
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@
120120
"command": "oss-fuzz.runFuzzIntrospector",
121121
"title": "OSS-Fuzz: Run Full Fuzz Introspector",
122122
"description": "Runs a full Fuzz Introspector"
123+
},
124+
{
125+
"command": "oss-fuzz.GetOptimalTargets",
126+
"title": "OSS-Fuzz: Get optimal targets",
127+
"description": "Gets the optimal targets of this project"
123128
}
124129
],
125130
"walkthroughs":[
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
////////////////////////////////////////////////////////////////////////////////
16+
17+
import {getOptimalTargetsFromIntrospector} from '../fuzzIntrospectorHelper';
18+
19+
export async function runGetOptimalTargetsHandler() {
20+
getOptimalTargetsFromIntrospector();
21+
}

tools/vscode-extension/src/commands/cmdRunFI.ts

+2-32
Original file line numberDiff line numberDiff line change
@@ -14,41 +14,11 @@
1414
//
1515
////////////////////////////////////////////////////////////////////////////////
1616

17-
import * as vscode from 'vscode';
18-
// import path = require('path');
19-
import {println} from '../logger';
20-
import {extensionConfig} from '../config';
21-
import {isPathValidOssFuzzPath} from '../ossfuzzWrappers';
22-
import {systemSync} from '../utils';
17+
import {runFuzzIntrospector} from '../fuzzIntrospectorHelper';
2318

2419
/**
2520
* Function for setting up Fuzz Introspector by way of a Python virtual env.
2621
*/
2722
export async function runFuzzIntrospectorHandler() {
28-
println('Setting up oss-fuzz in /tmp/');
29-
30-
const workspaceFolder = vscode.workspace.workspaceFolders;
31-
if (!workspaceFolder) {
32-
return;
33-
}
34-
const pathOfLocal = workspaceFolder[0].uri.fsPath;
35-
println('path of local: ' + pathOfLocal);
36-
37-
// First check if we already have Fuzz Introspector installed.
38-
const tmpOssFuzzRepositoryPath = '/tmp/fi-tmp-env';
39-
40-
if ((await isPathValidOssFuzzPath(tmpOssFuzzRepositoryPath)) === true) {
41-
println('Fuzz Introspector virtual env already exists in /tmp/fi-tmp-env');
42-
extensionConfig.ossFuzzPepositoryWorkPath = tmpOssFuzzRepositoryPath;
43-
return;
44-
}
45-
46-
const cmdToExec = '/tmp/fi-tmp-env/bin/fuzz-introspector';
47-
const args: Array<string> = ['full', '--target_dir=' + pathOfLocal];
48-
const [res, output] = await systemSync(cmdToExec, args);
49-
if (res === false) {
50-
println('Failed run FI');
51-
println(output);
52-
return;
53-
}
23+
runFuzzIntrospector();
5424
}

tools/vscode-extension/src/commands/cmdSetupFI.ts

+2-37
Original file line numberDiff line numberDiff line change
@@ -14,46 +14,11 @@
1414
//
1515
////////////////////////////////////////////////////////////////////////////////
1616

17-
import {println} from '../logger';
18-
import {extensionConfig} from '../config';
19-
import {isPathValidOssFuzzPath} from '../ossfuzzWrappers';
20-
import {systemSync} from '../utils';
17+
import {setUpFuzzIntrospector} from '../fuzzIntrospectorHelper';
2118

2219
/**
2320
* Function for setting up Fuzz Introspector by way of a Python virtual env.
2421
*/
2522
export async function setUpFuzzIntrospectorHandler() {
26-
println('Setting up oss-fuzz in /tmp/');
27-
28-
// First check if we already have Fuzz Introspector installed.
29-
const tmpOssFuzzRepositoryPath = '/tmp/fi-tmp-env';
30-
31-
if ((await isPathValidOssFuzzPath(tmpOssFuzzRepositoryPath)) === true) {
32-
println('Fuzz Introspector virtual env already exists in /tmp/fi-tmp-env');
33-
extensionConfig.ossFuzzPepositoryWorkPath = tmpOssFuzzRepositoryPath;
34-
return;
35-
}
36-
37-
const cmdToExec = 'python3.11';
38-
const args: Array<string> = ['-m', 'virtualenv', tmpOssFuzzRepositoryPath];
39-
const [res, output] = await systemSync(cmdToExec, args);
40-
if (res === false) {
41-
println('Failed to create virtual environment');
42-
println(output);
43-
return;
44-
}
45-
46-
const cmdToExec2 = '/tmp/fi-tmp-env/bin/python3.11';
47-
const args2: Array<string> = [
48-
'-m',
49-
'pip',
50-
'install',
51-
'fuzz-introspector==0.1.5',
52-
];
53-
const [res2, output2] = await systemSync(cmdToExec2, args2);
54-
if (res2 === false) {
55-
println('Failed to create virtual environment');
56-
println(output2);
57-
return;
58-
}
23+
setUpFuzzIntrospector();
5924
}

tools/vscode-extension/src/extension.ts

+8
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {runEndToEndAndGetCoverage} from './commands/cmdEndToEndCoverage';
3333
import {listFuzzersHandler} from './commands/cmdListFuzzers';
3434
import {cmdInputCollectorReproduceTestcase} from './commands/cmdReproduceTestcase';
3535
import {cmdDispatcherTemplate} from './commands/cmdTemplate';
36+
import {runGetOptimalTargetsHandler} from './commands/cmdFIGetOptimalTargets';
3637
import {setUpFuzzIntrospectorHandler} from './commands/cmdSetupFI';
3738
import {runFuzzIntrospectorHandler} from './commands/cmdRunFI';
3839
import {cmdDispatcherGenerateClusterfuzzLite} from './commands/cmdDispatcherGenerateClusterfuzzLite';
@@ -211,6 +212,13 @@ export function activate(context: vscode.ExtensionContext) {
211212
}
212213
)
213214
);
215+
216+
context.subscriptions.push(
217+
vscode.commands.registerCommand('oss-fuzz.GetOptimalTargets', async () => {
218+
println('CMD start: run GetOptimalTargets');
219+
await runGetOptimalTargetsHandler();
220+
})
221+
);
214222
}
215223

216224
// This method is called when your extension is deactivated
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
////////////////////////////////////////////////////////////////////////////////
16+
17+
import * as vscode from 'vscode';
18+
19+
import {println} from './logger';
20+
import {extensionConfig} from './config';
21+
import {isPathValidOssFuzzPath} from './ossfuzzWrappers';
22+
import {systemSync} from './utils';
23+
24+
const fs = require('fs');
25+
26+
export async function setUpFuzzIntrospector() {
27+
println('Setting up oss-fuzz in /tmp/');
28+
29+
// First check if we already have Fuzz Introspector installed.
30+
const tmpOssFuzzRepositoryPath = '/tmp/fi-tmp-env';
31+
32+
if ((await isPathValidOssFuzzPath(tmpOssFuzzRepositoryPath)) === true) {
33+
println('Fuzz Introspector virtual env already exists in /tmp/fi-tmp-env');
34+
extensionConfig.ossFuzzPepositoryWorkPath = tmpOssFuzzRepositoryPath;
35+
return;
36+
}
37+
38+
const cmdToExec = 'python3.11';
39+
const args: Array<string> = ['-m', 'virtualenv', tmpOssFuzzRepositoryPath];
40+
const [res, output] = await systemSync(cmdToExec, args);
41+
if (res === false) {
42+
println('Failed to create virtual environment');
43+
println(output);
44+
return;
45+
}
46+
47+
const cmdToExec2 = '/tmp/fi-tmp-env/bin/python3.11';
48+
const args2: Array<string> = [
49+
'-m',
50+
'pip',
51+
'install',
52+
'fuzz-introspector==0.1.6',
53+
];
54+
const [res2, output2] = await systemSync(cmdToExec2, args2);
55+
if (res2 === false) {
56+
println('Failed to create virtual environment');
57+
println(output2);
58+
return;
59+
}
60+
}
61+
62+
export async function runFuzzIntrospector() {
63+
println('Setting up oss-fuzz in /tmp/');
64+
65+
const workspaceFolder = vscode.workspace.workspaceFolders;
66+
if (!workspaceFolder) {
67+
return;
68+
}
69+
const pathOfLocal = workspaceFolder[0].uri.fsPath;
70+
println('path of local: ' + pathOfLocal);
71+
72+
// First check if we already have Fuzz Introspector installed.
73+
const tmpOssFuzzRepositoryPath = '/tmp/fi-tmp-env';
74+
75+
if ((await isPathValidOssFuzzPath(tmpOssFuzzRepositoryPath)) === true) {
76+
println('Fuzz Introspector virtual env already exists in /tmp/fi-tmp-env');
77+
extensionConfig.ossFuzzPepositoryWorkPath = tmpOssFuzzRepositoryPath;
78+
return;
79+
}
80+
81+
await systemSync('mkdir', ['-p', '/tmp/out-fi/']);
82+
83+
const cmdToExec = '/tmp/fi-tmp-env/bin/fuzz-introspector';
84+
const args: Array<string> = [
85+
'full',
86+
'--target_dir=' + pathOfLocal,
87+
'--out-dir=/tmp/out-fi',
88+
];
89+
const [res, output] = await systemSync(cmdToExec, args);
90+
if (res === false) {
91+
println('Failed run FI');
92+
println(output);
93+
return;
94+
}
95+
}
96+
97+
export async function getOptimalTargetsFromIntrospector() {
98+
if (!fs.existsSync('/tmp/out-fi/summary.json')) {
99+
println('There are no introspector reports. Please run introspector first');
100+
}
101+
const json_data = fs.readFileSync('/tmp/out-fi/summary.json');
102+
// println(json_data);
103+
104+
const jsonCodeCoverage = JSON.parse(json_data);
105+
106+
println('Optimal targets');
107+
Object.entries(jsonCodeCoverage['analyses']['OptimalTargets']).forEach(
108+
entry => {
109+
const [key, value] = entry;
110+
const objectDictionary: any = value as any;
111+
println(JSON.stringify(objectDictionary, null, 2));
112+
}
113+
);
114+
println('--------------------------');
115+
116+
return;
117+
}

0 commit comments

Comments
 (0)