[libFuzzer] compute base64 in-process instead of using an external lib. Since libFuzzer should not depend on anything, just re-implement base64 encoder. PR25746

llvm-svn: 254784
This commit is contained in:
Kostya Serebryany 2015-12-04 22:29:39 +00:00
parent 2f107ce132
commit 9e48cda9bc
5 changed files with 45 additions and 10 deletions

View File

@ -91,11 +91,6 @@ std::string DirPlusFile(const std::string &DirPath,
return DirPath + "/" + FileName; return DirPath + "/" + FileName;
} }
void PrintFileAsBase64(const std::string &Path) {
std::string Cmd = "base64 -w 0 < " + Path + "; echo";
ExecuteCommand(Cmd);
}
void Printf(const char *Fmt, ...) { void Printf(const char *Fmt, ...) {
va_list ap; va_list ap;
va_start(ap, Fmt); va_start(ap, Fmt);

View File

@ -42,7 +42,7 @@ void Print(const Unit &U, const char *PrintAfter = "");
void PrintASCII(const Unit &U, const char *PrintAfter = ""); void PrintASCII(const Unit &U, const char *PrintAfter = "");
std::string Hash(const Unit &U); std::string Hash(const Unit &U);
void SetTimer(int Seconds); void SetTimer(int Seconds);
void PrintFileAsBase64(const std::string &Path); std::string Base64(const Unit &U);
int ExecuteCommand(const std::string &Command); int ExecuteCommand(const std::string &Command);
// Private copy of SHA1 implementation. // Private copy of SHA1 implementation.

View File

@ -302,10 +302,8 @@ void Fuzzer::WriteUnitToFileWithPrefix(const Unit &U, const char *Prefix) {
WriteToFile(U, Path); WriteToFile(U, Path);
Printf("artifact_prefix='%s'; Test unit written to %s\n", Printf("artifact_prefix='%s'; Test unit written to %s\n",
Options.ArtifactPrefix.c_str(), Path.c_str()); Options.ArtifactPrefix.c_str(), Path.c_str());
if (U.size() <= kMaxUnitSizeToPrint) { if (U.size() <= kMaxUnitSizeToPrint)
Printf("Base64: "); Printf("Base64: %s\n", Base64(U).c_str());
PrintFileAsBase64(Path);
}
} }
void Fuzzer::SaveCorpus() { void Fuzzer::SaveCorpus() {

View File

@ -167,4 +167,33 @@ bool ParseDictionaryFile(const std::string &Text, std::vector<Unit> *Units) {
int GetPid() { return getpid(); } int GetPid() { return getpid(); }
std::string Base64(const Unit &U) {
static const char Table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
std::string Res;
size_t i;
for (i = 0; i + 2 < U.size(); i += 3) {
uint32_t x = (U[i] << 16) + (U[i + 1] << 8) + U[i + 2];
Res += Table[(x >> 18) & 63];
Res += Table[(x >> 12) & 63];
Res += Table[(x >> 6) & 63];
Res += Table[x & 63];
}
if (i + 1 == U.size()) {
uint32_t x = (U[i] << 16);
Res += Table[(x >> 18) & 63];
Res += Table[(x >> 12) & 63];
Res += "==";
} else if (i + 2 == U.size()) {
uint32_t x = (U[i] << 16) + (U[i + 1] << 8);
Res += Table[(x >> 18) & 63];
Res += Table[(x >> 12) & 63];
Res += Table[(x >> 6) & 63];
Res += "=";
}
return Res;
}
} // namespace fuzzer } // namespace fuzzer

View File

@ -360,3 +360,16 @@ TEST(FuzzerDictionary, ParseDictionaryFile) {
EXPECT_EQ(Units, EXPECT_EQ(Units,
std::vector<Unit>({Unit({'a', 'a'}), Unit({'a', 'b', 'c'})})); std::vector<Unit>({Unit({'a', 'a'}), Unit({'a', 'b', 'c'})}));
} }
TEST(FuzzerUtil, Base64) {
EXPECT_EQ("", Base64({}));
EXPECT_EQ("YQ==", Base64({'a'}));
EXPECT_EQ("eA==", Base64({'x'}));
EXPECT_EQ("YWI=", Base64({'a', 'b'}));
EXPECT_EQ("eHk=", Base64({'x', 'y'}));
EXPECT_EQ("YWJj", Base64({'a', 'b', 'c'}));
EXPECT_EQ("eHl6", Base64({'x', 'y', 'z'}));
EXPECT_EQ("YWJjeA==", Base64({'a', 'b', 'c', 'x'}));
EXPECT_EQ("YWJjeHk=", Base64({'a', 'b', 'c', 'x', 'y'}));
EXPECT_EQ("YWJjeHl6", Base64({'a', 'b', 'c', 'x', 'y', 'z'}));
}