Skip to content

Commit

Permalink
oss-fuzz/type2.cpp: avoid usage of expensive std::ostringstream
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave committed Feb 14, 2024
1 parent 3de152d commit 0986e9f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 33 deletions.
4 changes: 2 additions & 2 deletions oss-fuzz/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ SRC_FILES=main.cpp type2.cpp ${CPPCHECK_DIR}/externals/simplecpp/simplecpp.cpp $
all: oss-fuzz-client translate

oss-fuzz-client: main.cpp type2.cpp type2.h
${CXX} -std=c++11 -g ${CXXFLAGS} -o oss-fuzz-client ${INCLUDE_DIR} ${SRC_FILES} ${LIB_FUZZING_ENGINE}
${CXX} -std=c++11 -g -lstdc++ ${CXXFLAGS} -o oss-fuzz-client ${INCLUDE_DIR} ${SRC_FILES} ${LIB_FUZZING_ENGINE}

translate: translate.cpp type2.cpp type2.h
${CXX} -std=c++11 -g ${CXXFLAGS} -o translate type2.cpp translate.cpp
${CXX} -std=c++11 -g -lstdc++ ${CXXFLAGS} -o translate type2.cpp translate.cpp

clean:
rm -f oss-fuzz-client translate
87 changes: 56 additions & 31 deletions oss-fuzz/type2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "type2.h"

#include <cstring>
#include <sstream>

static int getValue(const uint8_t *data, size_t dataSize, uint8_t maxValue, bool *done = nullptr)
{
Expand Down Expand Up @@ -54,19 +53,20 @@ static std::string generateExpression2_lvalue(const uint8_t *data, size_t dataSi

static std::string generateExpression2_Op(const uint8_t *data, size_t dataSize, uint8_t numberOfGlobalConstants)
{
std::ostringstream code;
std::string code;
switch (getValue(data, dataSize, 3)) {
case 0:
code << generateExpression2_lvalue(data, dataSize);
code += generateExpression2_lvalue(data, dataSize);
break;
case 1:
code << "globalconstant" << (1 + getValue(data, dataSize, numberOfGlobalConstants));
code += "globalconstant";
code += (1 + getValue(data, dataSize, numberOfGlobalConstants));
break;
case 2:
code << (getValue(data, dataSize, 0x80) * 0x80 + getValue(data, dataSize, 0x80));
code += (getValue(data, dataSize, 0x80) * 0x80 + getValue(data, dataSize, 0x80));
break;
}
return code.str();
return code;
}

static std::string generateExpression2_Expr(const uint8_t *data, size_t dataSize, uint8_t numberOfGlobalConstants, int depth=0)
Expand Down Expand Up @@ -130,12 +130,14 @@ static std::string generateExpression2_conditionalCode(const std::string &indent
size_t dataSize,
uint8_t numberOfGlobalConstants)
{
std::ostringstream code;
std::string code;

if (indent.empty())
code << functionStart();
else
code << indent << "{\n";
code += functionStart();
else {
code += indent;
code += "{\n";
}

for (int line = 0; line < 4 || indent.empty(); ++line) {
bool done = false;
Expand All @@ -150,37 +152,60 @@ static std::string generateExpression2_conditionalCode(const std::string &indent
((type1 >= 5) ? mostLikelyType : type1);

if (type2 == 0) {
code << indent << " var" << getValue(data, dataSize, 5) << "=" << generateExpression2_Expr(data, dataSize, numberOfGlobalConstants) << ";\n";
code += indent;
code += " var";
code += getValue(data, dataSize, 5);
code += "=";
code += generateExpression2_Expr(data, dataSize, numberOfGlobalConstants);
code += ";\n";
} else if (type2 == 1) {
code << indent << " if (" << generateExpression2_Cond(data, dataSize, numberOfGlobalConstants) << ")\n";
code << generateExpression2_conditionalCode(indent + " ", data, dataSize, numberOfGlobalConstants);
code += indent;
code += " if (";
code += generateExpression2_Cond(data, dataSize, numberOfGlobalConstants);
code += ")\n";
code += generateExpression2_conditionalCode(indent + " ", data, dataSize, numberOfGlobalConstants);
} else if (type2 == 2) {
code << indent << " if (" << generateExpression2_Cond(data, dataSize, numberOfGlobalConstants) << ")\n";
code << generateExpression2_conditionalCode(indent + " ", data, dataSize, numberOfGlobalConstants);
code << indent << " else\n";
code << generateExpression2_conditionalCode(indent + " ", data, dataSize, numberOfGlobalConstants);
code += indent;
code += " if (";
code += generateExpression2_Cond(data, dataSize, numberOfGlobalConstants);
code += ")\n";
code += generateExpression2_conditionalCode(indent + " ", data, dataSize, numberOfGlobalConstants);
code += indent;
code += " else\n";
code += generateExpression2_conditionalCode(indent + " ", data, dataSize, numberOfGlobalConstants);
} else if (type2 == 3) {
code << indent << " while (" << generateExpression2_Cond(data, dataSize, numberOfGlobalConstants) << ")\n";
code << generateExpression2_conditionalCode(indent + " ", data, dataSize, numberOfGlobalConstants);
code += indent;
code += " while (";
code += generateExpression2_Cond(data, dataSize, numberOfGlobalConstants);
code += ")\n";
code += generateExpression2_conditionalCode(indent + " ", data, dataSize, numberOfGlobalConstants);
} else if (type2 == 4) {
code << indent << " return " << generateExpression2_Expr(data, dataSize, numberOfGlobalConstants) << ";\n";
if (indent.empty())
code << "}\n\n" << functionStart();
code += indent;
code += " return ";
code += generateExpression2_Expr(data, dataSize, numberOfGlobalConstants);
code += ";\n";
if (indent.empty()) {
code += "}\n\n";
code += functionStart();
}
else
break;
}
}

if (!indent.empty())
code << indent << "}\n";
else
code << " return 0;\n}\n";
return code.str();
if (!indent.empty()) {
code += indent;
code += "}\n";
}
else {
code += " return 0;\n}\n";
}
return code;
}

std::string generateCode2(const uint8_t *data, size_t dataSize)
{
std::ostringstream code;
std::string code;

// create global constants
constexpr uint8_t numberOfGlobalConstants = 0;
Expand All @@ -192,15 +217,15 @@ std::string generateCode2(const uint8_t *data, size_t dataSize)
}
*/

code << "int var1 = 1;\n"
code += "int var1 = 1;\n"
"int var2 = 0;\n"
"int var3 = 1;\n"
"int var4 = 0;\n"
"int var5 = -1;\n\n";

code << generateExpression2_conditionalCode("", data, dataSize, numberOfGlobalConstants);
code += generateExpression2_conditionalCode("", data, dataSize, numberOfGlobalConstants);

return code.str();
return code;
}


0 comments on commit 0986e9f

Please sign in to comment.