|
| 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