Commit 98e34077 by Corentin Wallez Committed by Commit Bot

translator_fuzzer: destroy translator cache on exit.

BUG=chromium:679725 Change-Id: I73b45b9ff2f1a270afce8c77a701e2659fd2b7c9 Reviewed-on: https://chromium-review.googlesource.com/426017Reviewed-by: 's avatarJamie Madill <jmadill@chromium.org> Reviewed-by: 's avatarGeoff Lang <geofflang@chromium.org> Commit-Queue: Corentin Wallez <cwallez@chromium.org>
parent 2a9ef3ea
...@@ -6,13 +6,14 @@ ...@@ -6,13 +6,14 @@
// translator_fuzzer.cpp: A libfuzzer fuzzer for the shader translator. // translator_fuzzer.cpp: A libfuzzer fuzzer for the shader translator.
#include <stddef.h> #include <cstddef>
#include <stdint.h> #include <cstdint>
#include <unordered_map>
#include <iostream> #include <iostream>
#include <memory>
#include <unordered_map>
#include "compiler/translator/Compiler.h"
#include "angle_gl.h" #include "angle_gl.h"
#include "compiler/translator/Compiler.h"
using namespace sh; using namespace sh;
...@@ -42,7 +43,14 @@ struct hash<TranslatorCacheKey> ...@@ -42,7 +43,14 @@ struct hash<TranslatorCacheKey>
}; };
} // namespace std } // namespace std
static std::unordered_map<TranslatorCacheKey, TCompiler *> translators; struct TCompilerDeleter
{
void operator()(TCompiler *compiler) const { DeleteCompiler(compiler); }
};
using UniqueTCompiler = std::unique_ptr<TCompiler, TCompilerDeleter>;
static std::unordered_map<TranslatorCacheKey, UniqueTCompiler> translators;
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{ {
...@@ -117,10 +125,10 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) ...@@ -117,10 +125,10 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
if (translators.find(key) == translators.end()) if (translators.find(key) == translators.end())
{ {
TCompiler *translator = ConstructCompiler(type, static_cast<ShShaderSpec>(spec), UniqueTCompiler translator(ConstructCompiler(type, static_cast<ShShaderSpec>(spec),
static_cast<ShShaderOutput>(output)); static_cast<ShShaderOutput>(output)));
if (!translator) if (translator == nullptr)
{ {
return 0; return 0;
} }
...@@ -146,14 +154,13 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) ...@@ -146,14 +154,13 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
if (!translator->Init(resources)) if (!translator->Init(resources))
{ {
DeleteCompiler(translator);
return 0; return 0;
} }
translators[key] = translator; translators[key] = std::move(translator);
} }
TCompiler *translator = translators[key]; auto &translator = translators[key];
const char *shaderStrings[] = {reinterpret_cast<const char *>(data)}; const char *shaderStrings[] = {reinterpret_cast<const char *>(data)};
translator->compile(shaderStrings, 1, options); translator->compile(shaderStrings, 1, options);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment