File size: 5,834 Bytes
e66a594
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////////

import * as vscode from 'vscode';
const fs = require('fs');
import {println} from '../logger';
import {commandHistory} from '../commandUtils';
import {buildFuzzersFromWorkspace, runFuzzerHandler} from '../ossfuzzWrappers';
import {listFuzzersForProject, systemSync, setStatusText} from '../utils';
import {
  compareLocalToRemoteCoverage,
  loadCoverageIntoWorkspace,
} from '../coverageHelper';
import {extensionConfig} from '../config';

/**
 * Performs the activities:
 * 1) Build a project using address sanitizer
 * 2) Run each fuzzer of the project, saving corpus
 * 3) Build project using coverage sanitizer
 * 4) Collect coverage
 * @param context
 * @returns
 */
export async function runEndToEndAndGetCoverage(
  context: vscode.ExtensionContext
) {
  println('Getting code coverage');
  await setStatusText('end-to-end coverage: getting input');
  const ossFuzzProjectNameInput = await vscode.window.showInputBox({
    value: '',
    placeHolder: 'The OSS-Fuzz project name',
  });
  if (!ossFuzzProjectNameInput) {
    println('Did not get a ossFuzzTargetProject');
    return;
  }
  const secondsToRunEachFuzzer = await vscode.window.showInputBox({
    value: '',
    placeHolder: 'Seconds to run each fuzzer',
  });
  if (!secondsToRunEachFuzzer) {
    println('Did not get number of seconds to run each fuzzer');
    return;
  }

  // Create an history object
  const args = new Object({
    projectName: ossFuzzProjectNameInput.toString(),
    secondsToRun: secondsToRunEachFuzzer.toString(),
    vsContext: context,
  });

  const commandObject = new Object({
    commandType: 'oss-fuzz.cmdDispatchEndToEndRun',
    Arguments: args,
    dispatcherFunc: cmdDispatchEndToEndRun,
  });
  console.log('L1: ' + commandHistory.length);
  commandHistory.push(commandObject);

  await cmdDispatchEndToEndRun(args);
  return;
}

async function cmdDispatchEndToEndRun(args: any) {
  await setStatusText('end-to-end coverage: starting');
  const res = await endToEndRun(
    args.projectName,
    args.secondsToRun,
    args.vsContext
  );
  if (res) {
    await setStatusText('end-to-end coverage: finished succesfully');
  } else {
    await setStatusText('end-to-end coverage: failed');
  }
  return;
}

async function endToEndRun(
  ossFuzzProjectNameInput: string,
  secondsToRunEachFuzzer: string,
  context: vscode.ExtensionContext
) {
  await setStatusText('end-to-end coverage: build with ASAN');
  vscode.window.showInformationMessage(
    'Building project: ' + ossFuzzProjectNameInput.toString()
  );
  if (
    (await buildFuzzersFromWorkspace(
      ossFuzzProjectNameInput.toString(),
      '',
      true
    )) === false
  ) {
    println('Failed to build project');
    return false;
  }
  println('Build projects');

  // List all of the fuzzers in the project
  const fuzzersInProject = await listFuzzersForProject(
    ossFuzzProjectNameInput,
    extensionConfig.ossFuzzPepositoryWorkPath
  );

  // Run all of the fuzzers in the project
  await setStatusText('end-to-end coverage: collecting corpus');
  println('Fuzzers found in project: ' + fuzzersInProject.toString());
  println('Running each of the fuzzers to collect a corpus');
  for (const fuzzName of fuzzersInProject) {
    println('Running fuzzer: ' + fuzzName);
    await setStatusText('end-to-end coverage: collecting corpus: ' + fuzzName);
    // Corpus directory
    const fuzzerCorpusPath =
      extensionConfig.ossFuzzPepositoryWorkPath +
      '/build/corpus/' +
      ossFuzzProjectNameInput +
      '/' +
      fuzzName;

    await systemSync('mkdir', ['-p', fuzzerCorpusPath]);

    await runFuzzerHandler(
      ossFuzzProjectNameInput,
      fuzzName,
      secondsToRunEachFuzzer.toString(),
      fuzzerCorpusPath
    );
  }

  // Build with code coverage
  println('Building project with coverage sanitizer');
  await setStatusText('end-to-end coverage: building with coverage');
  await buildFuzzersFromWorkspace(
    ossFuzzProjectNameInput.toString(),
    'coverage',
    true
  );

  // Run coverage command
  println('Collecting code coverage');
  await setStatusText('end-to-end coverage: collecting coverage');
  const args: Array<string> = [
    extensionConfig.ossFuzzPepositoryWorkPath + '/infra/helper.py',
    'coverage',
    '--port',
    '',
    '--no-corpus-download',
    ossFuzzProjectNameInput.toString(),
  ];
  await systemSync('python3', args);
  await setStatusText('end-to-end coverage: finished collecting coverage');
  println('Load coverage report with the command:');
  println(
    'python3 -m http.server 8008 --directory /tmp/oss-fuzz/build/out/' +
      ossFuzzProjectNameInput.toString() +
      '/report/'
  );

  println('Trying to load code coverage in IDE');
  const allCovPath =
    extensionConfig.ossFuzzPepositoryWorkPath +
    '/build/out/' +
    ossFuzzProjectNameInput.toString() +
    '/textcov_reports/all_cov.json';
  if (fs.existsSync(allCovPath)) {
    const generatedCodeCoverageFile = vscode.Uri.file(allCovPath);
    await loadCoverageIntoWorkspace(context, generatedCodeCoverageFile);
  }

  await compareLocalToRemoteCoverage(
    context,
    ossFuzzProjectNameInput.toString()
  );

  return true;
}