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