| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| import * as vscode from 'vscode'; |
| import {extensionConfig} from './config'; |
| import {getApi, FileDownloader} from '@microsoft/vscode-file-downloader-api'; |
|
|
| const fs = require('fs'); |
| const {spawn} = require('node:child_process'); |
| const statusBarComp = vscode.window.createStatusBarItem( |
| vscode.StatusBarAlignment.Right, |
| 100 |
| ); |
|
|
| import {println, printRaw, debugPrintln} from './logger'; |
|
|
| export async function setStatusText(statusText: string) { |
| |
| statusBarComp.text = '$(megaphone) OSS-Fuzz: ' + statusText; |
| statusBarComp.show(); |
| } |
|
|
| export async function downloadRemoteURL( |
| urlString: string, |
| targetFile: string, |
| context: vscode.ExtensionContext |
| ) { |
| const fileDownloader: FileDownloader = await getApi(); |
| println('URL: ' + urlString); |
| let codeCoverageFile: vscode.Uri; |
| try { |
| codeCoverageFile = await fileDownloader.downloadFile( |
| vscode.Uri.parse(urlString), |
| targetFile, |
| context |
| ); |
| } catch (err) { |
| println('Could not get the coverage summary file'); |
| return false; |
| } |
| return codeCoverageFile; |
| } |
|
|
| export async function getLocalOutBuildDir(projectName: string) { |
| const summaryCovPath = |
| extensionConfig.ossFuzzPepositoryWorkPath + '/build/out/' + projectName; |
| return summaryCovPath; |
| } |
|
|
| export async function getOSSFuzzCloudURL(projectName: string) { |
| const currentDate = new Date(); |
| const yesterday = new Date(currentDate); |
| yesterday.setDate(yesterday.getDate() - 1); |
|
|
| const day = yesterday.getDate(); |
| const month = yesterday.getMonth(); |
| const year = yesterday.getFullYear(); |
|
|
| let urlString = |
| 'https://storage.googleapis.com/oss-fuzz-coverage/' + |
| projectName + |
| '/reports/' + |
| year.toString(); |
|
|
| if (month < 10) { |
| urlString += '0'; |
| } |
| urlString += month.toString(); |
| if (day < 10) { |
| urlString += '0'; |
| } |
| urlString += day.toString(); |
|
|
| return urlString; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| export async function hasOssFuzzInWorkspace() { |
| const workspaceFolder = vscode.workspace.workspaceFolders; |
|
|
| if (!workspaceFolder) { |
| return false; |
| } |
|
|
| |
| const wsPath = workspaceFolder[0].uri.fsPath; |
| const ossfuzzDockerFilepath = vscode.Uri.file(wsPath + '/OSS-Fuzz/'); |
| try { |
| if (await vscode.workspace.fs.readDirectory(ossfuzzDockerFilepath)) { |
| for (const [name, type] of await vscode.workspace.fs.readDirectory( |
| ossfuzzDockerFilepath |
| )) { |
| |
| if (type === 2) { |
| |
| println('Found the relevant directory: ' + name); |
| return true; |
| } |
| } |
| } |
| } catch { |
| |
| } |
| return false; |
| } |
|
|
| |
| |
| |
| |
| export async function getOssFuzzWorkspaceProjectName() { |
| const workspaceFolder = vscode.workspace.workspaceFolders; |
| if (!workspaceFolder) { |
| return 'N/A'; |
| } |
|
|
| |
| const wsPath = workspaceFolder[0].uri.fsPath; |
| const ossfuzzDockerFilepath = vscode.Uri.file(wsPath + '/OSS-Fuzz/'); |
| try { |
| if (await vscode.workspace.fs.readDirectory(ossfuzzDockerFilepath)) { |
| for (const [name, type] of await vscode.workspace.fs.readDirectory( |
| ossfuzzDockerFilepath |
| )) { |
| if (type === 2) { |
| |
| |
| return name; |
| } |
| } |
| } |
| } catch { |
| |
| } |
|
|
| return 'N/A'; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| export async function listFuzzersForProject( |
| projectName: string, |
| ossFuzzRepositoryPath: string |
| ) { |
| const projectOssFuzzBuildPath = vscode.Uri.file( |
| ossFuzzRepositoryPath + '/build/out/' + projectName |
| ); |
| const fuzzersInProject: Array<string> = []; |
| for (const [name, type] of await vscode.workspace.fs.readDirectory( |
| projectOssFuzzBuildPath |
| )) { |
| |
| if (type === 1) { |
| const filepath = |
| ossFuzzRepositoryPath + '/build/out/' + projectName + '/' + name; |
| const binary = fs.readFileSync(filepath); |
|
|
| |
| |
| if (binary.lastIndexOf('LLVMFuzzerTestOneInput') !== -1) { |
| fuzzersInProject.push(name); |
| } |
| } |
| } |
| println('Successfully build the project.'); |
| println('The fuzzers in project'); |
| for (const fuzzName of fuzzersInProject) { |
| println(fuzzName); |
| } |
| return fuzzersInProject; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| export async function determineWorkspaceLanguage() { |
| const pythonFiles = await vscode.workspace.findFiles('**/*.py'); |
| const cppFiles = await vscode.workspace.findFiles('**/*.c++'); |
| const cfiles = await vscode.workspace.findFiles('**/*.c'); |
| const rustFiles = await vscode.workspace.findFiles('**/*.rust'); |
| const golangFiles = await vscode.workspace.findFiles('**/*.go'); |
|
|
| println('Number of python files: ' + pythonFiles.length); |
| println('Number of C++ files: ' + cppFiles.length); |
| println('Number of C files: ' + cfiles.length); |
| println('Number of rustFiles files: ' + rustFiles.length); |
| println('Number of golangFiles files: ' + golangFiles.length); |
|
|
| const maxCount = Math.max( |
| pythonFiles.length, |
| cppFiles.length, |
| cfiles.length, |
| rustFiles.length, |
| golangFiles.length |
| ); |
| let target = ''; |
| if (maxCount === pythonFiles.length) { |
| target = 'python'; |
| } else if (maxCount === cppFiles.length) { |
| target = 'c++'; |
| } else if (maxCount === cfiles.length) { |
| target = 'c'; |
| } else if (maxCount === rustFiles.length) { |
| target = 'rust'; |
| } else if (maxCount === golangFiles.length) { |
| target = 'golang'; |
| } else { |
| target = 'not implemented'; |
| } |
|
|
| println('Target language: ' + target); |
| return target; |
| } |
|
|
| |
| |
| |
| export async function systemSync(cmd: string, args: Array<string | undefined>) { |
| debugPrintln('Running command'); |
| debugPrintln(cmd); |
| debugPrintln(args.toString()); |
| debugPrintln('<<<<<<<<<<<<'); |
|
|
| |
| const command = spawn(cmd, args); |
|
|
| |
| command.stdout.on('data', (x: {toString: () => string}) => { |
| printRaw(x.toString()); |
| }); |
| command.stderr.on('data', (x: {toString: () => string}) => { |
| printRaw(x.toString()); |
| }); |
|
|
| |
| let hasChildExited = 0; |
| let childExitCode = 0; |
| command.on('exit', (code: any, signal: any) => { |
| |
| childExitCode = code; |
| hasChildExited = 1; |
| }); |
|
|
| |
| const snooze = (ms: number) => |
| new Promise(resolve => setTimeout(resolve, ms)); |
|
|
| let idx = 0; |
| const maxSeconds = 1800; |
| debugPrintln('Child exited: ' + hasChildExited); |
|
|
| |
| |
| while (hasChildExited === 0 && idx < maxSeconds) { |
| idx += 1; |
| await snooze(1000); |
| } |
|
|
| |
| if (childExitCode !== 0) { |
| println('Command execution errored'); |
| return [false, command.toString()]; |
| } |
| |
| return [true, command.toString()]; |
| } |
|
|
| export async function systemSyncLogIfFailure( |
| cmd: string, |
| args: Array<string | undefined> |
| ): Promise<boolean> { |
| const [res, cmdMsg] = await systemSync(cmd, args); |
| if (res === false) { |
| println(cmdMsg); |
| return false; |
| } |
| return true; |
| } |
|
|