all repos — mgba @ fa884d071ecaa3e05ff20b45a67bf9500dd3d6b6

mGBA Game Boy Advance Emulator

src/platform/test/fuzz-main.c (view raw)

  1/* Copyright (c) 2013-2015 Jeffrey Pfau
  2 *
  3 * This Source Code Form is subject to the terms of the Mozilla Public
  4 * License, v. 2.0. If a copy of the MPL was not distributed with this
  5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6#include <mgba/core/blip_buf.h>
  7#include <mgba/core/cheats.h>
  8#include <mgba/core/config.h>
  9#include <mgba/core/core.h>
 10#include <mgba/core/serialize.h>
 11#include <mgba/gb/core.h>
 12#include <mgba/gba/core.h>
 13#include <mgba/internal/gba/gba.h>
 14
 15#include "feature/commandline.h"
 16#include <mgba-util/memory.h>
 17#include <mgba-util/string.h>
 18#include <mgba-util/vfs.h>
 19
 20#include <errno.h>
 21#include <signal.h>
 22
 23#define FUZZ_OPTIONS "F:NO:S:V:"
 24#define FUZZ_USAGE \
 25	"\nAdditional options:\n" \
 26	"  -F FRAMES        Run for the specified number of FRAMES before exiting\n" \
 27	"  -N               Disable video rendering entirely\n" \
 28	"  -O OFFSET        Offset to apply savestate overlay\n" \
 29	"  -S FILE          Load a savestate when starting the test\n" \
 30	"  -V FILE          Overlay a second savestate over the loaded savestate\n" \
 31
 32struct FuzzOpts {
 33	bool noVideo;
 34	int frames;
 35	size_t overlayOffset;
 36	char* savestate;
 37	char* ssOverlay;
 38};
 39
 40static void _fuzzRunloop(struct mCore* core, int frames);
 41static void _fuzzShutdown(int signal);
 42static bool _parseFuzzOpts(struct mSubParser* parser, int option, const char* arg);
 43
 44static bool _dispatchExiting = false;
 45
 46int main(int argc, char** argv) {
 47	signal(SIGINT, _fuzzShutdown);
 48
 49	struct FuzzOpts fuzzOpts = { false, 0, 0, 0, 0 };
 50	struct mSubParser subparser = {
 51		.usage = FUZZ_USAGE,
 52		.parse = _parseFuzzOpts,
 53		.extraOptions = FUZZ_OPTIONS,
 54		.opts = &fuzzOpts
 55	};
 56
 57	struct mArguments args;
 58	bool parsed = parseArguments(&args, argc, argv, &subparser);
 59	if (!args.fname) {
 60		parsed = false;
 61	}
 62	if (!parsed || args.showHelp) {
 63		usage(argv[0], FUZZ_USAGE);
 64		return !parsed;
 65	}
 66	if (args.showVersion) {
 67		version(argv[0]);
 68		return 0;
 69	}
 70	struct mCore* core = mCoreFind(args.fname);
 71	core->init(core);
 72	mCoreInitConfig(core, "fuzz");
 73	applyArguments(&args, NULL, &core->config);
 74
 75	mCoreConfigSetDefaultValue(&core->config, "idleOptimization", "remove");
 76
 77	void* outputBuffer;
 78	outputBuffer = 0;
 79
 80	if (!fuzzOpts.noVideo) {
 81		outputBuffer = malloc(256 * 256 * 4);
 82		core->setVideoBuffer(core, outputBuffer, 256);
 83	}
 84
 85#ifdef M_CORE_GBA
 86	if (core->platform(core) == PLATFORM_GBA) {
 87		((struct GBA*) core->board)->hardCrash = false;
 88	}
 89#endif
 90
 91#ifdef __AFL_HAVE_MANUAL_CONTROL
 92	__AFL_INIT();
 93#endif
 94	if (args.patch) {
 95		core->loadPatch(core, VFileOpen(args.patch, O_RDONLY));
 96	}
 97	mCoreLoadFile(core, args.fname);
 98
 99	struct VFile* savestate = 0;
100	struct VFile* savestateOverlay = 0;
101	size_t overlayOffset;
102
103	if (fuzzOpts.savestate) {
104		savestate = VFileOpen(fuzzOpts.savestate, O_RDONLY);
105		free(fuzzOpts.savestate);
106	}
107	if (fuzzOpts.ssOverlay) {
108		overlayOffset = fuzzOpts.overlayOffset;
109		if (overlayOffset < core->stateSize(core)) {
110			savestateOverlay = VFileOpen(fuzzOpts.ssOverlay, O_RDONLY);
111		}
112		free(fuzzOpts.ssOverlay);
113	}
114
115	core->reset(core);
116
117	struct mCheatDevice* device;
118	if (args.cheatsFile && (device = core->cheatDevice(core))) {
119		struct VFile* vf = VFileOpen(args.cheatsFile, O_RDONLY);
120		if (vf) {
121			mCheatDeviceClear(device);
122			mCheatParseFile(device, vf);
123			vf->close(vf);
124		}
125	}
126
127	if (savestate) {
128		if (!savestateOverlay) {
129			mCoreLoadStateNamed(core, savestate, 0);
130		} else {
131			size_t size = core->stateSize(core);
132			uint8_t* state = malloc(size);
133			savestate->read(savestate, state, size);
134			savestateOverlay->read(savestateOverlay, state + overlayOffset, size - overlayOffset);
135			core->loadState(core, state);
136			free(state);
137			savestateOverlay->close(savestateOverlay);
138			savestateOverlay = 0;
139		}
140		savestate->close(savestate);
141		savestate = 0;
142	}
143
144	blip_set_rates(core->getAudioChannel(core, 0), GBA_ARM7TDMI_FREQUENCY, 0x8000);
145	blip_set_rates(core->getAudioChannel(core, 1), GBA_ARM7TDMI_FREQUENCY, 0x8000);
146
147	_fuzzRunloop(core, fuzzOpts.frames);
148
149	core->unloadROM(core);
150
151	if (savestate) {
152		savestate->close(savestate);
153	}
154	if (savestateOverlay) {
155		savestateOverlay->close(savestateOverlay);
156	}
157
158	freeArguments(&args);
159	if (outputBuffer) {
160		free(outputBuffer);
161	}
162	core->deinit(core);
163
164	return 0;
165}
166
167static void _fuzzRunloop(struct mCore* core, int frames) {
168	do {
169		core->runFrame(core);
170		blip_clear(core->getAudioChannel(core, 0));
171		blip_clear(core->getAudioChannel(core, 1));
172	} while (core->frameCounter(core) < frames && !_dispatchExiting);
173}
174
175static void _fuzzShutdown(int signal) {
176	UNUSED(signal);
177	_dispatchExiting = true;
178}
179
180static bool _parseFuzzOpts(struct mSubParser* parser, int option, const char* arg) {
181	struct FuzzOpts* opts = parser->opts;
182	errno = 0;
183	switch (option) {
184	case 'F':
185		opts->frames = strtoul(arg, 0, 10);
186		return !errno;
187	case 'N':
188		opts->noVideo = true;
189		return true;
190	case 'O':
191		opts->overlayOffset = strtoul(arg, 0, 10);
192		return !errno;
193	case 'S':
194		opts->savestate = strdup(arg);
195		return true;
196	case 'V':
197		opts->ssOverlay = strdup(arg);
198		return true;
199	default:
200		return false;
201	}
202}