Skip to content
This repository was archived by the owner on Apr 30, 2021. It is now read-only.

Commit e7af3d0

Browse files
author
Yevgeny Pats
committed
Add coverage dump in istunbul/nyc format
1 parent e8ac837 commit e7af3d0

File tree

7 files changed

+50
-5
lines changed

7 files changed

+50
-5
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22
node_modules
33
build
44
crash-*
5-
corpus/*
5+
corpus/*
6+
coverage
7+
.nyc_output
8+

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,16 @@ jsfuzz tries to mimic some of the arguments and output style from [libFuzzer](ht
109109
More fuzz targets examples (for real and popular libraries) are located under the examples directory and
110110
bugs that were found using those targets are listed in the trophies section.
111111

112+
### Coverage
113+
114+
Coverage in Istanbul/NYC format is written to .nyc_output/out.json It can be viewer with `nyc` cli. For example:
115+
116+
```bash
117+
nyc report --reporter=html --exclude-node-modules=false
118+
```
119+
120+
This will save the html report to `coverage` directory
121+
112122
## Other languages
113123

114124
Currently this library is also ported to python via [pythonfuzz](https://github.com/fuzzitdev/jsfuzz)

examples/csv/fuzz.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
const parse = require('csv-parse/lib/sync');
22

3+
function isASCII(str) {
4+
return /^[\x00-\x7F]*$/.test(str);
5+
}
6+
37
function fuzz(buf) {
8+
const str = buf.toString();
9+
if (!isASCII(str)) {
10+
return
11+
}
412
try {
5-
parse(buf.toString());
13+
parse(str);
614
} catch (e) {
715
// Those are "valid" exceptions. we can't catch them in one line as
816
// jpeg-js doesn't export/inherit from one exception class/style.

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jsfuzz",
3-
"version": "1.0.10",
3+
"version": "1.0.11",
44
"description": "Coverage Guided Javascript Fuzzer",
55
"main": "build/src/index.js",
66
"types": "build/src/inde.d.ts",

src/fuzzer.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import {BuildVerse, Verse} from "./versifier";
88
const crypto = require('crypto');
99
const util = require('util');
1010
const pidusage = require('pidusage');
11+
process.on('SIGINT', function() {
12+
// ignore sigint as this propagates to worker as well.
13+
console.log('Received SIGINT. shutting down gracefully');
14+
});
1115

1216

1317
export class Fuzzer {
@@ -144,10 +148,11 @@ export class Fuzzer {
144148
});
145149

146150
this.worker.on('exit', (code, signal) => {
147-
if (signal) {
151+
if (signal && code !== 0) {
148152
console.log('Worker killed');
149153
this.writeCrash(buf);
150154
}
155+
console.log('Worker exited');
151156
this.clearIntervals();
152157
});
153158

src/worker.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
const fs = require('fs');
12
import * as path from "path";
23
import {ManageMessageType, ManagerMessage, WorkerMessageType} from "./protocol";
34

45
const {createInstrumenter} = require('istanbul-lib-instrument');
56
const {hookRequire} = require('istanbul-lib-hook');
7+
let sigint = false;
8+
process.on('SIGINT', function() {
9+
console.log('Received SIGINT. shutting down gracefully');
10+
sigint = true;
11+
});
612

713
class Worker {
814
private readonly fn: (buf: Buffer) => void;
@@ -37,11 +43,23 @@ class Worker {
3743
return total
3844
}
3945

46+
dump_coverage() {
47+
// @ts-ignore
48+
const data = JSON.stringify(global["__coverage__"]);
49+
if (!fs.existsSync('./.nyc_output')){
50+
fs.mkdirSync('./.nyc_output');
51+
}
52+
fs.writeFileSync('./.nyc_output/cov.json', data);
53+
}
4054

4155
start() {
4256
process.on('message', async (m: ManagerMessage) => {
4357
try {
4458
if (m.type === ManageMessageType.WORK) {
59+
if (sigint) {
60+
this.dump_coverage();
61+
process.exit(0);
62+
}
4563
if (this.fn.constructor.name === 'AsyncFunction') {
4664
// @ts-ignore
4765
await this.fn(Buffer.from(m.buf.data));
@@ -59,6 +77,7 @@ class Worker {
5977
} catch (e) {
6078
console.log("=================================================================");
6179
console.log(e);
80+
this.dump_coverage();
6281
// @ts-ignore
6382
process.send({
6483
type: WorkerMessageType.CRASH,

0 commit comments

Comments
 (0)