2016-01-13 20:57:57 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Fuzz.h"
|
2016-01-15 13:46:54 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <signal.h>
|
2016-01-13 20:57:57 +00:00
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
2016-01-15 13:46:54 +00:00
|
|
|
if (argc < 3) {
|
|
|
|
SkDebugf("Usage: %s <fuzz name> <path/to/fuzzed.data>\n", argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
2016-01-14 12:59:42 +00:00
|
|
|
const char* name = argv[1];
|
|
|
|
const char* path = argv[2];
|
|
|
|
|
|
|
|
SkAutoTUnref<SkData> bytes(SkData::NewFromFileName(path));
|
|
|
|
Fuzz fuzz(bytes);
|
2016-01-13 20:57:57 +00:00
|
|
|
|
|
|
|
for (auto r = SkTRegistry<Fuzzable>::Head(); r; r = r->next()) {
|
|
|
|
auto fuzzable = r->factory();
|
2016-01-14 12:59:42 +00:00
|
|
|
if (0 == strcmp(name, fuzzable.name)) {
|
2016-01-15 13:46:54 +00:00
|
|
|
SkDebugf("Running %s\n", fuzzable.name);
|
2016-01-13 20:57:57 +00:00
|
|
|
fuzzable.fn(&fuzz);
|
2016-01-14 12:59:42 +00:00
|
|
|
return 0;
|
2016-01-13 20:57:57 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-14 12:59:42 +00:00
|
|
|
return 1;
|
2016-01-13 20:57:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-14 12:59:42 +00:00
|
|
|
Fuzz::Fuzz(SkData* bytes) : fBytes(SkSafeRef(bytes)), fNextByte(0) {}
|
|
|
|
|
2016-01-15 13:46:54 +00:00
|
|
|
void Fuzz::signalBug () { raise(SIGSEGV); }
|
|
|
|
void Fuzz::signalBoring() { exit(0); }
|
|
|
|
|
2016-01-14 12:59:42 +00:00
|
|
|
template <typename T>
|
2016-01-15 13:46:54 +00:00
|
|
|
T Fuzz::nextT() {
|
|
|
|
if (fNextByte + sizeof(T) > fBytes->size()) {
|
|
|
|
this->signalBoring();
|
2016-01-14 12:59:42 +00:00
|
|
|
}
|
2016-01-15 13:46:54 +00:00
|
|
|
|
2016-01-14 12:59:42 +00:00
|
|
|
T val;
|
2016-01-15 13:46:54 +00:00
|
|
|
memcpy(&val, fBytes->bytes() + fNextByte, sizeof(T));
|
|
|
|
fNextByte += sizeof(T);
|
2016-01-14 12:59:42 +00:00
|
|
|
return val;
|
|
|
|
}
|
2016-01-13 20:57:57 +00:00
|
|
|
|
2016-01-15 13:46:54 +00:00
|
|
|
uint8_t Fuzz::nextB() { return this->nextT<uint8_t >(); }
|
|
|
|
uint32_t Fuzz::nextU() { return this->nextT<uint32_t>(); }
|
|
|
|
float Fuzz::nextF() { return this->nextT<float >(); }
|
2016-01-13 20:57:57 +00:00
|
|
|
|