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/config.h"
7#include "gba/context/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 mSubParser* parser, 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 mSubParser 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 mCoreOptions opts = {}; // TODO: Put back idle loops
57 mCoreConfigLoadDefaults(&context.config, &opts);
58 mCoreConfigFreeOpts(&opts);
59
60 struct mArguments args;
61 bool parsed = parseArguments(&args, argc, argv, &subparser);
62 if (!parsed || args.showHelp) {
63 usage(argv[0], FUZZ_USAGE);
64 GBAContextDeinit(&context);
65 return !parsed;
66 }
67 if (args.showVersion) {
68 version(argv[0]);
69 GBAContextDeinit(&context);
70 return 0;
71 }
72 applyArguments(&args, NULL, &context.config);
73
74 struct GBAVideoSoftwareRenderer renderer;
75 renderer.outputBuffer = 0;
76
77 if (!fuzzOpts.noVideo) {
78 GBAVideoSoftwareRendererCreate(&renderer);
79 renderer.outputBuffer = malloc(256 * 256 * 4);
80 renderer.outputBufferStride = 256;
81 context.renderer = &renderer.d;
82 }
83
84#ifdef __AFL_HAVE_MANUAL_CONTROL
85 __AFL_INIT();
86#endif
87
88 struct VFile* rom = VFileOpen(args.fname, O_RDONLY);
89
90 context.gba->hardCrash = false;
91 GBAContextLoadROMFromVFile(&context, rom, 0);
92
93 struct VFile* savestate = 0;
94 struct VFile* savestateOverlay = 0;
95 size_t overlayOffset;
96
97 GBAContextStart(&context);
98
99 if (fuzzOpts.savestate) {
100 savestate = VFileOpen(fuzzOpts.savestate, O_RDONLY);
101 free(fuzzOpts.savestate);
102 }
103 if (fuzzOpts.ssOverlay) {
104 overlayOffset = fuzzOpts.overlayOffset;
105 if (overlayOffset < sizeof(struct GBASerializedState)) {
106 savestateOverlay = VFileOpen(fuzzOpts.ssOverlay, O_RDONLY);
107 }
108 free(fuzzOpts.ssOverlay);
109 }
110 if (savestate) {
111 if (!savestateOverlay) {
112 GBALoadStateNamed(context.gba, savestate, 0);
113 } else {
114 struct GBASerializedState* state = GBAAllocateState();
115 savestate->read(savestate, state, sizeof(*state));
116 savestateOverlay->read(savestateOverlay, (uint8_t*) state + overlayOffset, sizeof(*state) - overlayOffset);
117 GBADeserialize(context.gba, state);
118 GBADeallocateState(state);
119 savestateOverlay->close(savestateOverlay);
120 savestateOverlay = 0;
121 }
122 savestate->close(savestate);
123 savestate = 0;
124 }
125
126 blip_set_rates(context.gba->audio.psg.left, GBA_ARM7TDMI_FREQUENCY, 0x8000);
127 blip_set_rates(context.gba->audio.psg.right, GBA_ARM7TDMI_FREQUENCY, 0x8000);
128
129 _GBAFuzzRunloop(&context, fuzzOpts.frames);
130
131 GBAContextStop(&context);
132 GBAContextUnloadROM(&context);
133
134 if (savestate) {
135 savestate->close(savestate);
136 }
137 if (savestateOverlay) {
138 savestateOverlay->close(savestateOverlay);
139 }
140
141 freeArguments(&args);
142 if (renderer.outputBuffer) {
143 free(renderer.outputBuffer);
144 }
145 GBAContextDeinit(&context);
146
147 return 0;
148}
149
150static void _GBAFuzzRunloop(struct GBAContext* context, int frames) {
151 do {
152 GBAContextFrame(context, 0);
153 } while (context->gba->video.frameCounter < frames && !_dispatchExiting);
154}
155
156static void _GBAFuzzShutdown(int signal) {
157 UNUSED(signal);
158 _dispatchExiting = true;
159}
160
161static bool _parseFuzzOpts(struct mSubParser* parser, int option, const char* arg) {
162 struct FuzzOpts* opts = parser->opts;
163 errno = 0;
164 switch (option) {
165 case 'F':
166 opts->frames = strtoul(arg, 0, 10);
167 return !errno;
168 case 'N':
169 opts->noVideo = true;
170 return true;
171 case 'O':
172 opts->overlayOffset = strtoul(arg, 0, 10);
173 return !errno;
174 case 'S':
175 opts->savestate = strdup(arg);
176 return true;
177 case 'V':
178 opts->ssOverlay = strdup(arg);
179 return true;
180 default:
181 return false;
182 }
183}