Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
365fe90
instrumentor: remove unused function
kyakdan Jan 16, 2023
935e483
fuzzer: use 0 as the first edge ID
kyakdan Jan 16, 2023
b3a4888
fuzzer: move counter-related code into a dedicated file
kyakdan Jan 16, 2023
e74b57d
fuzzer: refactor addon types/functions into dedicated file
kyakdan Jan 16, 2023
f95e4f1
fuzzer: refactor tracing functions into dedicated file
kyakdan Jan 16, 2023
82bea2e
instrumentor: add edge ID generation strategy interface
kyakdan Jan 16, 2023
b1989cf
instrumentor: add file sync edge ID strategy for multi-process modes
kyakdan Jan 18, 2023
79321a2
core: use file sync strategy for libFuzzer's multi-process modes
kyakdan Jan 18, 2023
4a6375b
fuzzer: refactor the fuzzer object and type
kyakdan Jan 21, 2023
817d2f4
instrumentor: use os.EOL for adding a new line into the ID sync file
kyakdan Jan 22, 2023
4c01791
instrumentor: move counter-generating functions into codeCoverage()
kyakdan Jan 21, 2023
9ba1ca8
instrumentor: use returned release function to release lock on id file
kyakdan Jan 22, 2023
430c6cf
instrumentor: extract EdgeIdStrategy into an interface
kyakdan Jan 22, 2023
40e3c55
instrumentor: extract coverage tracking functions into CoverageTracker
kyakdan Jan 22, 2023
718269c
instrumentor: fix naming in mock function
kyakdan Jan 22, 2023
8a69526
instrumentor: extract compare tracing functions into dedicated object
kyakdan Jan 22, 2023
116aa71
cli: mark the id_sync_file as hidden
kyakdan Jan 23, 2023
dafff5a
instrumentor: mark fatalExitCode of ID file sync strategy as readonly
kyakdan Jan 23, 2023
fa950e3
fuzzer: do not export the native addon through the fuzzer interface
kyakdan Jan 23, 2023
185ac86
instrumentor: add an edge ID strategy returning zero IDs
kyakdan Jan 23, 2023
1d59313
instrumentor: extract instrumentor into own class
kyakdan Jan 24, 2023
e36b000
instrumentor: add unit test for sync file ID strategy
kyakdan Jan 24, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions packages/core/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ yargs(process.argv.slice(2))
})
.hide("fuzzFunction")

.option("id_sync_file", {
describe:
"File used to sync edge ID generation. " +
"Needed when fuzzing in multi-process modes",
type: "string",
default: undefined,
group: "Fuzzer:",
})
.hide("id_sync_file")

.option("sync", {
describe: "Run the fuzz target synchronously.",
type: "boolean",
Expand Down Expand Up @@ -167,6 +177,7 @@ yargs(process.argv.slice(2))
fuzzerOptions: args.corpus.concat(args._),
customHooks: args.custom_hooks.map(ensureFilepath),
expectedErrors: args.expected_errors,
idSyncFile: args.id_sync_file,
});
}
)
Expand Down
36 changes: 31 additions & 5 deletions packages/core/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,20 @@
*/

import path from "path";
import * as fuzzer from "@jazzer.js/fuzzer";
import * as hooking from "@jazzer.js/hooking";
import { registerInstrumentor } from "@jazzer.js/instrumentor";
import { trackedHooks } from "@jazzer.js/hooking";
import * as process from "process";
import * as tmp from "tmp";
import * as fs from "fs";

import * as fuzzer from "@jazzer.js/fuzzer";
import * as hooking from "@jazzer.js/hooking";
import { trackedHooks } from "@jazzer.js/hooking";
import {
registerInstrumentor,
Instrumentor,
FileSyncIdStrategy,
MemorySyncIdStrategy,
} from "@jazzer.js/instrumentor";

// Remove temporary files on exit
tmp.setGracefulCleanup();

Expand All @@ -44,6 +50,7 @@ export interface Options {
customHooks: string[];
expectedErrors: string[];
timeout?: number;
idSyncFile?: string;
}

interface FuzzModule {
Expand All @@ -60,7 +67,16 @@ export async function initFuzzing(options: Options) {
registerGlobals();
await Promise.all(options.customHooks.map(importModule));
if (!options.dryRun) {
registerInstrumentor(options.includes, options.excludes);
registerInstrumentor(
new Instrumentor(
options.includes,
options.excludes,

options.idSyncFile !== undefined
? new FileSyncIdStrategy(options.idSyncFile)
: new MemorySyncIdStrategy()
)
);
}
}

Expand Down Expand Up @@ -121,6 +137,16 @@ function createWrapperScript(fuzzerOptions: string[]) {
(arg) => arg !== "--" && fuzzerOptions.indexOf(arg) === -1
);

if (jazzerArgs.indexOf("--id_sync_file") === -1) {
const idSyncFile = tmp.fileSync({
mode: 0o600,
prefix: "jazzer.js",
postfix: "idSync",
});
jazzerArgs.push("--id_sync_file", idSyncFile.name);
fs.closeSync(idSyncFile.fd);
}

const isWindows = process.platform === "win32";

const scriptContent = `${isWindows ? "@echo off" : "#!/usr/bin/env sh"}
Expand Down
8 changes: 4 additions & 4 deletions packages/core/jazzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

import { addon } from "@jazzer.js/fuzzer";
import { fuzzer } from "@jazzer.js/fuzzer";

/**
* Instructs the fuzzer to guide its mutations towards making `current` equal to `target`
Expand All @@ -29,7 +29,7 @@ import { addon } from "@jazzer.js/fuzzer";
* @param id a (probabilistically) unique identifier for this particular compare hint
*/
function guideTowardsEquality(current: string, target: string, id: number) {
addon.traceUnequalStrings(id, current, target);
fuzzer.tracer.traceUnequalStrings(id, current, target);
}

/**
Expand All @@ -45,7 +45,7 @@ function guideTowardsEquality(current: string, target: string, id: number) {
* @param id a (probabilistically) unique identifier for this particular compare hint
*/
function guideTowardsContainment(needle: string, haystack: string, id: number) {
addon.traceStringContainment(id, needle, haystack);
fuzzer.tracer.traceStringContainment(id, needle, haystack);
}

/**
Expand All @@ -63,7 +63,7 @@ function guideTowardsContainment(needle: string, haystack: string, id: number) {
* @param id a (probabilistically) unique identifier for this particular state hint
*/
function exploreState(state: number, id: number) {
addon.tracePcIndir(id, state);
fuzzer.tracer.tracePcIndir(id, state);
}

export interface Jazzer {
Expand Down
65 changes: 65 additions & 0 deletions packages/fuzzer/addon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2023 Code Intelligence GmbH
*
* 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 { default as bind } from "bindings";

export type FuzzTargetAsyncOrValue = (data: Buffer) => void | Promise<void>;
export type FuzzTargetCallback = (
data: Buffer,
done: (e?: Error) => void
) => void;
export type FuzzTarget = FuzzTargetAsyncOrValue | FuzzTargetCallback;
export type FuzzOpts = string[];

export type StartFuzzingSyncFn = (
fuzzFn: FuzzTarget,
fuzzOpts: FuzzOpts
) => void;
export type StartFuzzingAsyncFn = (
fuzzFn: FuzzTarget,
fuzzOpts: FuzzOpts
) => Promise<void>;

type NativeAddon = {
registerCoverageMap: (buffer: Buffer) => void;
registerNewCounters: (oldNumCounters: number, newNumCounters: number) => void;

traceUnequalStrings: (
hookId: number,
current: string,
target: string
) => void;

traceStringContainment: (
hookId: number,
needle: string,
haystack: string
) => void;
traceIntegerCompare: (
hookId: number,
current: number,
target: number
) => void;

tracePcIndir: (hookId: number, state: number) => void;

printVersion: () => void;
startFuzzing: StartFuzzingSyncFn;
startFuzzingAsync: StartFuzzingAsyncFn;
stopFuzzingAsync: (status?: number) => void;
};

export const addon: NativeAddon = bind("jazzerjs");
70 changes: 70 additions & 0 deletions packages/fuzzer/coverage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright 2023 Code Intelligence GmbH
*
* 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 { addon } from "./addon";

export class CoverageTracker {
private static readonly MAX_NUM_COUNTERS: number = 1 << 20;
private static readonly INITIAL_NUM_COUNTERS: number = 1 << 9;
private readonly coverageMap: Buffer;
private currentNumCounters: number;

constructor() {
this.coverageMap = Buffer.alloc(CoverageTracker.MAX_NUM_COUNTERS, 0);
this.currentNumCounters = CoverageTracker.INITIAL_NUM_COUNTERS;
addon.registerCoverageMap(this.coverageMap);
addon.registerNewCounters(0, this.currentNumCounters);
}

enlargeCountersBufferIfNeeded(nextEdgeId: number) {
// Enlarge registered counters if needed
let newNumCounters = this.currentNumCounters;
while (nextEdgeId >= newNumCounters) {
newNumCounters = 2 * newNumCounters;
if (newNumCounters > CoverageTracker.MAX_NUM_COUNTERS) {
throw new Error(
`Maximum number (${CoverageTracker.MAX_NUM_COUNTERS}) of coverage counts exceeded.`
);
}
}

// Register new counters if enlarged
if (newNumCounters > this.currentNumCounters) {
addon.registerNewCounters(this.currentNumCounters, newNumCounters);
this.currentNumCounters = newNumCounters;
console.log(
`INFO: New number of coverage counters ${this.currentNumCounters}`
);
}
}

/**
* Increments the coverage counter for a given ID.
* This function implements the NeverZero policy from AFL++.
* See https://aflplus.plus//papers/aflpp-woot2020.pdf
* @param edgeId the edge ID of the coverage counter to increment
*/
incrementCounter(edgeId: number) {
const counter = this.coverageMap.readUint8(edgeId);
this.coverageMap.writeUint8(counter == 255 ? 1 : counter + 1, edgeId);
}

readCounter(edgeId: number): number {
return this.coverageMap.readUint8(edgeId);
}
}

export const coverageTracker = new CoverageTracker();
Loading