all repos — mgba @ 5966f46355d18ba472404a7f7ed3cbeebf846635

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 "gba/supervisor/config.h"
  7#include "gba/supervisor/context.h"
  8#include "gba/gba.h"
  9#include "gba/renderers/video-software.h"
 10#include "gba/serialize.h"
 11
 12#include "platform/commandline.h"
 13#include "util/memory.h"
 14#include "util/string.h"
 15#include "util/vfs.h"
 16
 17#include <errno.h>
 18#include <signal.h>
 19
 20#define FUZZ_OPTIONS "F:NO:S:V:"
 21#define FUZZ_USAGE \
 22	"\nAdditional options:\n" \
 23	"  -F FRAMES        Run for the specified number of FRAMES before exiting\n" \
 24	"  -N               Disable video rendering entirely\n" \
 25	"  -O OFFSET        Offset to apply savestate overlay\n" \
 26	"  -S FILE          Load a savestate when starting the test\n" \
 27	"  -V FILE          Overlay a second savestate over the loaded savestate\n" \
 28
 29struct FuzzOpts {
 30	bool noVideo;
 31	int frames;
 32	size_t overlayOffset;
 33	char* savestate;
 34	char* ssOverlay;
 35};
 36
 37static void _GBAFuzzRunloop(struct GBAContext* context, int frames);
 38static void _GBAFuzzShutdown(int signal);
 39static bool _parseFuzzOpts(struct SubParser* parser, struct GBAConfig* config, int option, const char* arg);
 40
 41static bool _dispatchExiting = false;
 42
 43int main(int argc, char** argv) {
 44	signal(SIGINT, _GBAFuzzShutdown);
 45
 46	struct FuzzOpts fuzzOpts = { false, 0, 0, 0, 0 };
 47	struct SubParser subparser = {
 48		.usage = FUZZ_USAGE,
 49		.parse = _parseFuzzOpts,
 50		.extraOptions = FUZZ_OPTIONS,
 51		.opts = &fuzzOpts
 52	};
 53
 54	struct GBAContext context;
 55	GBAContextInit(&context, "fuzz");
 56	struct GBAOptions opts = {
 57		.idleOptimization = IDLE_LOOP_DETECT
 58	};
 59	GBAConfigLoadDefaults(&context.config, &opts);
 60	GBAConfigFreeOpts(&opts);
 61
 62	struct GBAArguments args;
 63	bool parsed = parseArguments(&args, &context.config, argc, argv, &subparser);
 64	if (!parsed || args.showHelp) {
 65		usage(argv[0], FUZZ_USAGE);
 66		freeArguments(&args);
 67		GBAContextDeinit(&context);
 68		return !parsed;
 69	}
 70
 71	struct VFile* rom = VFileOpen(args.fname, O_RDONLY);
 72
 73	context.gba->hardCrash = false;
 74	GBAContextLoadROMFromVFile(&context, rom, 0);
 75
 76	struct GBAVideoSoftwareRenderer renderer;
 77	renderer.outputBuffer = 0;
 78
 79	struct VFile* savestate = 0;
 80	struct VFile* savestateOverlay = 0;
 81	size_t overlayOffset;
 82
 83	if (!fuzzOpts.noVideo) {
 84		GBAVideoSoftwareRendererCreate(&renderer);
 85		renderer.outputBuffer = malloc(256 * 256 * 4);
 86		renderer.outputBufferStride = 256;
 87		GBAVideoAssociateRenderer(&context.gba->video, &renderer.d);
 88	}
 89
 90	GBAContextStart(&context);
 91
 92	if (fuzzOpts.savestate) {
 93		savestate = VFileOpen(fuzzOpts.savestate, O_RDONLY);
 94		free(fuzzOpts.savestate);
 95	}
 96	if (fuzzOpts.ssOverlay) {
 97		overlayOffset = fuzzOpts.overlayOffset;
 98		if (overlayOffset < sizeof(struct GBASerializedState)) {
 99			savestateOverlay = VFileOpen(fuzzOpts.ssOverlay, O_RDONLY);
100		}
101		free(fuzzOpts.ssOverlay);
102	}
103	if (savestate) {
104		if (!savestateOverlay) {
105			GBALoadStateNamed(context.gba, savestate);
106		} else {
107			struct GBASerializedState* state = GBAAllocateState();
108			savestate->read(savestate, state, sizeof(*state));
109			savestateOverlay->read(savestateOverlay, (uint8_t*) state + overlayOffset, sizeof(*state) - overlayOffset);
110			GBADeserialize(context.gba, state);
111			GBADeallocateState(state);
112			savestateOverlay->close(savestateOverlay);
113			savestateOverlay = 0;
114		}
115		savestate->close(savestate);
116		savestate = 0;
117	}
118
119	blip_set_rates(context.gba->audio.left, GBA_ARM7TDMI_FREQUENCY, 0x8000);
120	blip_set_rates(context.gba->audio.right, GBA_ARM7TDMI_FREQUENCY, 0x8000);
121
122	_GBAFuzzRunloop(&context, fuzzOpts.frames);
123
124	if (savestate) {
125		savestate->close(savestate);
126	}
127	if (savestateOverlay) {
128		savestateOverlay->close(savestateOverlay);
129	}
130	GBAContextStop(&context);
131	GBAContextDeinit(&context);
132	freeArguments(&args);
133	if (renderer.outputBuffer) {
134		free(renderer.outputBuffer);
135	}
136
137	return 0;
138}
139
140static void _GBAFuzzRunloop(struct GBAContext* context, int frames) {
141	do {
142		GBAContextFrame(context, 0);
143	} while (context->gba->video.frameCounter < frames && !_dispatchExiting);
144}
145
146static void _GBAFuzzShutdown(int signal) {
147	UNUSED(signal);
148	_dispatchExiting = true;
149}
150
151static bool _parseFuzzOpts(struct SubParser* parser, struct GBAConfig* config, int option, const char* arg) {
152	UNUSED(config);
153	struct FuzzOpts* opts = parser->opts;
154	errno = 0;
155	switch (option) {
156	case 'F':
157		opts->frames = strtoul(arg, 0, 10);
158		return !errno;
159	case 'N':
160		opts->noVideo = true;
161		return true;
162	case 'O':
163		opts->overlayOffset = strtoul(arg, 0, 10);
164		return !errno;
165	case 'S':
166		opts->savestate = strdup(arg);
167		return true;
168	case 'V':
169		opts->ssOverlay = strdup(arg);
170		return true;
171	default:
172		return false;
173	}
174}