From 12f54b55a2e92c81a15468315dc2eadde4f6c472 Mon Sep 17 00:00:00 2001 From: Bhargava Shastry Date: Mon, 29 Jul 2019 16:38:24 +0200 Subject: [PATCH] Pseudo randomly add checks --- test/tools/ossfuzz/protoToAbiV2.cpp | 53 +++++++++++++++++------------ test/tools/ossfuzz/protoToAbiV2.h | 13 +++++-- 2 files changed, 41 insertions(+), 25 deletions(-) diff --git a/test/tools/ossfuzz/protoToAbiV2.cpp b/test/tools/ossfuzz/protoToAbiV2.cpp index 84c4ab0be..daea61c9a 100644 --- a/test/tools/ossfuzz/protoToAbiV2.cpp +++ b/test/tools/ossfuzz/protoToAbiV2.cpp @@ -353,20 +353,20 @@ void ProtoConverter::visit(ValueType const& _x) { switch (_x.value_type_oneof_case()) { - case ValueType::kInty: - visit(_x.inty()); - break; - case ValueType::kByty: - visit(_x.byty()); - break; - case ValueType::kAdty: - visit(_x.adty()); - break; - case ValueType::kBoolty: - visit(_x.boolty()); - break; - case ValueType::VALUE_TYPE_ONEOF_NOT_SET: - break; + case ValueType::kInty: + visit(_x.inty()); + break; + case ValueType::kByty: + visit(_x.byty()); + break; + case ValueType::kAdty: + visit(_x.adty()); + break; + case ValueType::kBoolty: + visit(_x.boolty()); + break; + case ValueType::VALUE_TYPE_ONEOF_NOT_SET: + break; } } @@ -500,8 +500,10 @@ unsigned ProtoConverter::resizeDimension( addVarDef(lhs, rhs); } - // if (c.length != l) - checkResizeOp(_param, length); + // Add checks on array length pseudo randomly + if (addCheck(getNextCounter())) + // if (c.length != l) + checkResizeOp(_param, length); return length; } @@ -517,12 +519,19 @@ void ProtoConverter::resizeHelper( // (depth-first) recurse otherwise. if (_arrInfoVec.empty()) { - // expression name is _var - // value is a value of base type - std::string value = getValueByBaseType(_x); - // add assignment and check - DataType dataType = getDataTypeByBaseType(_x); - addCheckedVarDef(dataType, _varName, _paramName, value); + // We are at the leaf node now. + // To ensure we do not create a very large test case + // especially for multidimensional dynamic arrays, + // we create a checked assignment pseudo randomly. + if (addCheck(getNextCounter())) + { + // expression name is _var + // value is a value of base type + std::string value = getValueByBaseType(_x); + // add assignment and check + DataType dataType = getDataTypeByBaseType(_x); + addCheckedVarDef(dataType, _varName, _paramName, value); + } } else { diff --git a/test/tools/ossfuzz/protoToAbiV2.h b/test/tools/ossfuzz/protoToAbiV2.h index 5dde8b697..50ca4dc9c 100644 --- a/test/tools/ossfuzz/protoToAbiV2.h +++ b/test/tools/ossfuzz/protoToAbiV2.h @@ -310,7 +310,7 @@ private: static unsigned getIntWidth(IntegerType const& _x) { - return 8 * ((_x.width() % 32) + 1); + return 8 * (_x.width() % 32 + 1); } static bool isIntSigned(IntegerType const& _x) @@ -325,12 +325,12 @@ private: static std::string getIntTypeAsString(IntegerType const& _x) { - return ((isIntSigned(_x) ? "int" : "uint") + std::to_string(getIntWidth(_x))); + return (isIntSigned(_x) ? "int" : "uint") + std::to_string(getIntWidth(_x)); } static unsigned getFixedByteWidth(FixedByteType const& _x) { - return (_x.width() % 32) + 1; + return _x.width() % 32 + 1; } static std::string getFixedByteTypeAsString(FixedByteType const& _x) @@ -413,6 +413,10 @@ private: _counter, _isHexLiteral ); + + static bool addCheck(unsigned _counter) + { + return _counter % s_arrayCheckFrequency == 0; } /// Contains the test program @@ -434,6 +438,9 @@ private: static unsigned constexpr s_maxArrayLength = 4; static unsigned constexpr s_maxArrayDimensions = 4; static unsigned constexpr s_maxDynArrayLength = 256; + /// Add check only if counter returned by getNextCounter() + /// is divisible by s_arrayCheckFrequency + static unsigned constexpr s_arrayCheckFrequency = 11; /// Prefixes for declared and parameterized variable names static auto constexpr s_varNamePrefix = "x_"; static auto constexpr s_paramNamePrefix = "c_";