Skip to content

Commit

Permalink
Attempt to make sure that only primitive-type parameters are used whe…
Browse files Browse the repository at this point in the history
…n generating the parameter list for the BMv2 portion of the fuzzer

Signed-off-by: zzmic <100326374 [email protected]>
  • Loading branch information
zzmic committed Jul 11, 2024
1 parent f0cc11e commit 21561a7
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 2 deletions.
58 changes: 56 additions & 2 deletions backends/p4tools/modules/smith/common/declarations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -711,13 711,16 @@ IR::Parameter *DeclarationGenerator::genParameter(IR::Direction dir, cstring p_n
return new IR::Parameter(p_name, dir, tp);
}

/// TODO(zzmic): Make sure that a `Struct` type is not used when generating the parameter list.
IR::ParameterList *DeclarationGenerator::genParameterList() {
IR::IndexedVector<IR::Parameter> params;
size_t totalParams = Utils::getRandInt(0, 3);
size_t numDirParams = (totalParams != 0U) ? Utils::getRandInt(0, totalParams - 1) : 0;
size_t numDirectionlessParams = totalParams - numDirParams;
for (size_t i = 0; i < numDirParams; i ) {
IR::Parameter *param = genTypedParameter(false);
// If the target is bmv2, we need to make sure that the parameter type is not a struct.
IR::Parameter *param =
isBmv2Target() ? genPrimitiveTypeParameter(false) : genTypedParameter(false);
if (param == nullptr) {
BUG("param is null");
}
Expand All @@ -732,7 735,8 @@ IR::ParameterList *DeclarationGenerator::genParameterList() {
}
}
for (size_t i = 0; i < numDirectionlessParams; i ) {
IR::Parameter *param = genTypedParameter(true);
IR::Parameter *param =
isBmv2Target() ? genPrimitiveTypeParameter(true) : genTypedParameter(true);

if (param == nullptr) {
BUG("param is null");
Expand All @@ -746,4 750,54 @@ IR::ParameterList *DeclarationGenerator::genParameterList() {
return new IR::ParameterList(params);
}

/// @brief (Helper function) Check if the target is bmv2.
bool DeclarationGenerator::isBmv2Target() {
if (target().spec.archName.compare("bmv2") == 0) {
return true;
}
return false;
}

/// @brief Generate a parameter of some primitive type.
// TODO(zzmic): Figure out whether "primitive type" is the correct term to use.
IR::Parameter *DeclarationGenerator::genPrimitiveTypeParameter(bool if_none_dir) {
cstring name = getRandomString(4);
const IR::Type *tp = nullptr;
IR::Direction dir;
PrimitiveTyperefProbs typePercent;

if (if_none_dir) {
typePercent = {
PCT.PARAMETER_NONEDIR_BASETYPE_BIT,
PCT.PARAMETER_NONEDIR_BASETYPE_SIGNED_BIT,
PCT.PARAMETER_NONEDIR_BASETYPE_INT,
};
dir = IR::Direction::None;
} else {
typePercent = {
PCT.PARAMETER_BASETYPE_BIT,
PCT.PARAMETER_BASETYPE_SIGNED_BIT,
PCT.PARAMETER_BASETYPE_INT,
};
std::vector<int64_t> dirPercent = {PCT.PARAMETER_DIR_IN, PCT.PARAMETER_DIR_OUT,
PCT.PARAMETER_DIR_INOUT};
switch (Utils::getRandInt(dirPercent)) {
case 0:
dir = IR::Direction::In;
break;
case 1:
dir = IR::Direction::Out;
break;
case 2:
dir = IR::Direction::InOut;
break;
default:
dir = IR::Direction::None;
}
}
tp = target().expressionGenerator().pickRndPrimitiveType(typePercent);

return new IR::Parameter(name, dir, tp);
}

} // namespace P4Tools::P4Smith
4 changes: 4 additions & 0 deletions backends/p4tools/modules/smith/common/declarations.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 79,10 @@ class DeclarationGenerator : public Generator {
virtual IR::Parameter *genParameter(IR::Direction dir, cstring p_name, cstring t_name);

virtual IR::ParameterList *genParameterList();

virtual bool isBmv2Target();

virtual IR::Parameter *genPrimitiveTypeParameter(bool if_none_dir);
};

} // namespace P4Tools::P4Smith
Expand Down
32 changes: 32 additions & 0 deletions backends/p4tools/modules/smith/common/expressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 181,38 @@ const IR::Type *ExpressionGenerator::pickRndType(TyperefProbs type_probs) {
return tp;
}

const IR::Type *ExpressionGenerator::pickRndPrimitiveType(PrimitiveTyperefProbs type_probs) {
const std::vector<int64_t> &typeProbsVector = {type_probs.p4_bit, type_probs.p4_signed_bit,
type_probs.p4_int};

if (typeProbsVector.size() != 3) {
BUG("pickRndPrimitiveType: Primitive type probabilities must be exact");
}
const IR::Type *tp = nullptr;
size_t idx = Utils::getRandInt(typeProbsVector);
switch (idx) {
case 0: {
// bit<>
tp = ExpressionGenerator::genBitType(false);
break;
}
case 1: {
// int<>
tp = ExpressionGenerator::genBitType(true);
break;
}
case 2: {
tp = ExpressionGenerator::genIntType();
break;
}
}
if (tp == nullptr) {
BUG("pickRndPrimitiveType: Chosen primitive type is Null!");
}

return tp;
}

IR::BoolLiteral *ExpressionGenerator::genBoolLiteral() {
if (Utils::getRandInt(0, 1) != 0) {
return new IR::BoolLiteral(false);
Expand Down
8 changes: 8 additions & 0 deletions backends/p4tools/modules/smith/common/expressions.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 33,12 @@ using TyperefProbs = struct TyperefProbs {
int64_t p4_match_kind;
};

using PrimitiveTyperefProbs = struct PrimitiveTyperefProbs {
int64_t p4_bit;
int64_t p4_signed_bit;
int64_t p4_int;
};

class ExpressionGenerator : public Generator {
public:
virtual ~ExpressionGenerator() = default;
Expand All @@ -53,6 59,8 @@ class ExpressionGenerator : public Generator {

virtual const IR::Type *pickRndType(TyperefProbs type_probs);

virtual const IR::Type *pickRndPrimitiveType(PrimitiveTyperefProbs type_probs);

static IR::BoolLiteral *genBoolLiteral();

static IR::Constant *genIntLiteral(size_t bit_width = INTEGER_WIDTH);
Expand Down

0 comments on commit 21561a7

Please sign in to comment.