Validate correct string data in fuzz test
This commit is contained in:
54
src/fuzz.cpp
54
src/fuzz.cpp
@@ -1,14 +1,17 @@
|
||||
#include "callbacks.h"
|
||||
#include "json_value.h"
|
||||
#include "parser3.h"
|
||||
|
||||
#include <simdjson.h>
|
||||
|
||||
std::pair<std::string, parser3::Status> runStreaming(std::string copy) {
|
||||
std::pair<std::string, parser3::Status> runStreaming(std::string copy,
|
||||
int stride) {
|
||||
SerializeState state;
|
||||
auto c = serializeCallbacks();
|
||||
parser3::Parser3 parser(&c, &state);
|
||||
for (int i = 0; i < copy.size(); ++i) {
|
||||
auto s = parser.parse(copy.data() + i, 1);
|
||||
for (int i = 0; i < copy.size(); i += stride) {
|
||||
auto s =
|
||||
parser.parse(copy.data() + i, std::min<int>(stride, copy.size() - i));
|
||||
if (s != parser3::S_AGAIN) {
|
||||
return {state.result, s};
|
||||
}
|
||||
@@ -36,20 +39,23 @@ std::pair<std::string, parser3::Status> runBatch(std::string copy) {
|
||||
}
|
||||
|
||||
void testStreaming(std::string const &json) {
|
||||
auto streaming = runStreaming(json);
|
||||
auto batch = runBatch(json);
|
||||
if (streaming != batch) {
|
||||
if (streaming.second == batch.second && streaming.second != parser3::S_OK) {
|
||||
// It's ok if the processed data doesn't match if parsing failed
|
||||
return;
|
||||
for (int stride = 1; stride < 16; ++stride) {
|
||||
auto streaming = runStreaming(json, stride);
|
||||
if (streaming != batch) {
|
||||
if (streaming.second == batch.second &&
|
||||
streaming.second != parser3::S_OK) {
|
||||
// It's ok if the processed data doesn't match if parsing failed
|
||||
return;
|
||||
}
|
||||
printf("streaming: %s, %s\n",
|
||||
streaming.second == parser3::S_OK ? "accept" : "reject",
|
||||
streaming.first.c_str());
|
||||
printf("batch: %s, %s\n",
|
||||
streaming.second == parser3::S_OK ? "accept" : "reject",
|
||||
batch.first.c_str());
|
||||
abort();
|
||||
}
|
||||
printf("streaming: %s, %s\n",
|
||||
streaming.second == parser3::S_OK ? "accept" : "reject",
|
||||
streaming.first.c_str());
|
||||
printf("batch: %s, %s\n",
|
||||
streaming.second == parser3::S_OK ? "accept" : "reject",
|
||||
batch.first.c_str());
|
||||
abort();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,9 +98,27 @@ void compareWithSimdjson(std::string const &json) {
|
||||
}
|
||||
}
|
||||
|
||||
void testStringRoundTrip(std::string_view s) {
|
||||
if (!simdjson::validate_utf8(s.data(), s.size())) {
|
||||
// You can't encode non utf-8 data in a json string, even with escaping
|
||||
return;
|
||||
}
|
||||
for (int stride = 0; stride < 16; ++stride) {
|
||||
auto escaped = "\"" + escapeAsJsonString(s) + "\"";
|
||||
auto parsed = toValue(std::move(escaped));
|
||||
if (!parsed.has_value()) {
|
||||
abort();
|
||||
}
|
||||
if (std::get<std::string>(*parsed) != s) {
|
||||
abort();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
|
||||
auto s = std::string((const char *)data, size);
|
||||
testStreaming(s);
|
||||
compareWithSimdjson(s);
|
||||
testStringRoundTrip(s);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user