nfa: switch to using bytecode_ptr<NFA>

This commit is contained in:
Justin Viiret
2017-04-03 17:21:37 +10:00
committed by Matthew Barr
parent 905ac78061
commit a197074c5d
27 changed files with 251 additions and 241 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015-2016, Intel Corporation
* Copyright (c) 2015-2017, Intel Corporation
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -26,7 +26,8 @@
* POSSIBILITY OF SUCH DAMAGE.
*/
/** \file
/**
* \file
* \brief Large Bounded Repeat (LBR) engine build code.
*/
@@ -128,25 +129,24 @@ void fillNfa(NFA *nfa, lbr_common *c, ReportID report, const depth &repeatMin,
}
template <class LbrStruct> static
aligned_unique_ptr<NFA> makeLbrNfa(NFAEngineType nfa_type,
enum RepeatType rtype,
const depth &repeatMax) {
bytecode_ptr<NFA> makeLbrNfa(NFAEngineType nfa_type, enum RepeatType rtype,
const depth &repeatMax) {
size_t tableLen = 0;
if (rtype == REPEAT_SPARSE_OPTIMAL_P) {
tableLen = sizeof(u64a) * (repeatMax + 1);
}
size_t len = sizeof(NFA) + sizeof(LbrStruct) + sizeof(RepeatInfo) +
tableLen + sizeof(u64a);
aligned_unique_ptr<NFA> nfa = aligned_zmalloc_unique<NFA>(len);
auto nfa = make_bytecode_ptr<NFA>(len);
nfa->type = verify_u8(nfa_type);
nfa->length = verify_u32(len);
return nfa;
}
static
aligned_unique_ptr<NFA> buildLbrDot(const CharReach &cr, const depth &repeatMin,
const depth &repeatMax, u32 minPeriod,
bool is_reset, ReportID report) {
bytecode_ptr<NFA> buildLbrDot(const CharReach &cr, const depth &repeatMin,
const depth &repeatMax, u32 minPeriod,
bool is_reset, ReportID report) {
if (!cr.all()) {
return nullptr;
}
@@ -164,10 +164,9 @@ aligned_unique_ptr<NFA> buildLbrDot(const CharReach &cr, const depth &repeatMin,
}
static
aligned_unique_ptr<NFA> buildLbrVerm(const CharReach &cr,
const depth &repeatMin,
const depth &repeatMax, u32 minPeriod,
bool is_reset, ReportID report) {
bytecode_ptr<NFA> buildLbrVerm(const CharReach &cr, const depth &repeatMin,
const depth &repeatMax, u32 minPeriod,
bool is_reset, ReportID report) {
const CharReach escapes(~cr);
if (escapes.count() != 1) {
@@ -188,10 +187,9 @@ aligned_unique_ptr<NFA> buildLbrVerm(const CharReach &cr,
}
static
aligned_unique_ptr<NFA> buildLbrNVerm(const CharReach &cr,
const depth &repeatMin,
const depth &repeatMax, u32 minPeriod,
bool is_reset, ReportID report) {
bytecode_ptr<NFA> buildLbrNVerm(const CharReach &cr, const depth &repeatMin,
const depth &repeatMax, u32 minPeriod,
bool is_reset, ReportID report) {
const CharReach escapes(cr);
if (escapes.count() != 1) {
@@ -212,10 +210,9 @@ aligned_unique_ptr<NFA> buildLbrNVerm(const CharReach &cr,
}
static
aligned_unique_ptr<NFA> buildLbrShuf(const CharReach &cr,
const depth &repeatMin,
const depth &repeatMax, u32 minPeriod,
bool is_reset, ReportID report) {
bytecode_ptr<NFA> buildLbrShuf(const CharReach &cr, const depth &repeatMin,
const depth &repeatMax, u32 minPeriod,
bool is_reset, ReportID report) {
enum RepeatType rtype = chooseRepeatType(repeatMin, repeatMax, minPeriod,
is_reset);
auto nfa = makeLbrNfa<lbr_shuf>(LBR_NFA_SHUF, rtype, repeatMax);
@@ -233,10 +230,9 @@ aligned_unique_ptr<NFA> buildLbrShuf(const CharReach &cr,
}
static
aligned_unique_ptr<NFA> buildLbrTruf(const CharReach &cr,
const depth &repeatMin,
const depth &repeatMax, u32 minPeriod,
bool is_reset, ReportID report) {
bytecode_ptr<NFA> buildLbrTruf(const CharReach &cr, const depth &repeatMin,
const depth &repeatMax, u32 minPeriod,
bool is_reset, ReportID report) {
enum RepeatType rtype = chooseRepeatType(repeatMin, repeatMax, minPeriod,
is_reset);
auto nfa = makeLbrNfa<lbr_truf>(LBR_NFA_TRUF, rtype, repeatMax);
@@ -252,10 +248,9 @@ aligned_unique_ptr<NFA> buildLbrTruf(const CharReach &cr,
}
static
aligned_unique_ptr<NFA> constructLBR(const CharReach &cr,
const depth &repeatMin,
const depth &repeatMax, u32 minPeriod,
bool is_reset, ReportID report) {
bytecode_ptr<NFA> constructLBR(const CharReach &cr, const depth &repeatMin,
const depth &repeatMax, u32 minPeriod,
bool is_reset, ReportID report) {
DEBUG_PRINTF("bounds={%s,%s}, cr=%s (count %zu), report=%u\n",
repeatMin.str().c_str(), repeatMax.str().c_str(),
describeClass(cr, 20, CC_OUT_TEXT).c_str(), cr.count(),
@@ -263,8 +258,8 @@ aligned_unique_ptr<NFA> constructLBR(const CharReach &cr,
assert(repeatMin <= repeatMax);
assert(repeatMax.is_reachable());
aligned_unique_ptr<NFA> nfa
= buildLbrDot(cr, repeatMin, repeatMax, minPeriod, is_reset, report);
auto nfa =
buildLbrDot(cr, repeatMin, repeatMax, minPeriod, is_reset, report);
if (!nfa) {
nfa = buildLbrVerm(cr, repeatMin, repeatMax, minPeriod, is_reset,
@@ -291,10 +286,10 @@ aligned_unique_ptr<NFA> constructLBR(const CharReach &cr,
return nfa;
}
aligned_unique_ptr<NFA> constructLBR(const CastleProto &proto,
const vector<vector<CharReach>> &triggers,
const CompileContext &cc,
const ReportManager &rm) {
bytecode_ptr<NFA> constructLBR(const CastleProto &proto,
const vector<vector<CharReach>> &triggers,
const CompileContext &cc,
const ReportManager &rm) {
if (!cc.grey.allowLbr) {
return nullptr;
}
@@ -330,10 +325,10 @@ aligned_unique_ptr<NFA> constructLBR(const CastleProto &proto,
}
/** \brief Construct an LBR engine from the given graph \p g. */
aligned_unique_ptr<NFA> constructLBR(const NGHolder &g,
const vector<vector<CharReach>> &triggers,
const CompileContext &cc,
const ReportManager &rm) {
bytecode_ptr<NFA> constructLBR(const NGHolder &g,
const vector<vector<CharReach>> &triggers,
const CompileContext &cc,
const ReportManager &rm) {
if (!cc.grey.allowLbr) {
return nullptr;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015-2016, Intel Corporation
* Copyright (c) 2015-2017, Intel Corporation
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -26,7 +26,8 @@
* POSSIBILITY OF SUCH DAMAGE.
*/
/** \file
/**
* \file
* \brief Large Bounded Repeat (LBR) engine build code.
*/
@@ -34,7 +35,7 @@
#define NG_LBR_H
#include "ue2common.h"
#include "util/alloc.h"
#include "util/bytecode_ptr.h"
#include <memory>
#include <vector>
@@ -51,14 +52,16 @@ struct CompileContext;
struct Grey;
/** \brief Construct an LBR engine from the given graph \p g. */
aligned_unique_ptr<NFA>
bytecode_ptr<NFA>
constructLBR(const NGHolder &g,
const std::vector<std::vector<CharReach>> &triggers,
const CompileContext &cc, const ReportManager &rm);
/** \brief Construct an LBR engine from the given CastleProto, which should
* contain only one repeat. */
aligned_unique_ptr<NFA>
/**
* \brief Construct an LBR engine from the given CastleProto, which should
* contain only one repeat.
*/
bytecode_ptr<NFA>
constructLBR(const CastleProto &proto,
const std::vector<std::vector<CharReach>> &triggers,
const CompileContext &cc, const ReportManager &rm);

View File

@@ -26,9 +26,11 @@
* POSSIBILITY OF SUCH DAMAGE.
*/
/** \file
/**
* \file
* \brief Limex NFA construction code.
*/
#include "ng_limex.h"
#include "grey.h"
@@ -623,7 +625,7 @@ void remapReportsToPrograms(NGHolder &h, const ReportManager &rm) {
}
static
aligned_unique_ptr<NFA>
bytecode_ptr<NFA>
constructNFA(const NGHolder &h_in, const ReportManager *rm,
const map<u32, u32> &fixed_depth_tops,
const map<u32, vector<vector<CharReach>>> &triggers,
@@ -682,7 +684,7 @@ constructNFA(const NGHolder &h_in, const ReportManager *rm,
zombies, do_accel, compress_state, hint, cc);
}
aligned_unique_ptr<NFA>
bytecode_ptr<NFA>
constructNFA(const NGHolder &h_in, const ReportManager *rm,
const map<u32, u32> &fixed_depth_tops,
const map<u32, vector<vector<CharReach>>> &triggers,
@@ -696,7 +698,7 @@ constructNFA(const NGHolder &h_in, const ReportManager *rm,
#ifndef RELEASE_BUILD
// Variant that allows a hint to be specified.
aligned_unique_ptr<NFA>
bytecode_ptr<NFA>
constructNFA(const NGHolder &h_in, const ReportManager *rm,
const map<u32, u32> &fixed_depth_tops,
const map<u32, vector<vector<CharReach>>> &triggers,
@@ -709,8 +711,8 @@ constructNFA(const NGHolder &h_in, const ReportManager *rm,
#endif // RELEASE_BUILD
static
aligned_unique_ptr<NFA> constructReversedNFA_i(const NGHolder &h_in, u32 hint,
const CompileContext &cc) {
bytecode_ptr<NFA> constructReversedNFA_i(const NGHolder &h_in, u32 hint,
const CompileContext &cc) {
// Make a mutable copy of the graph that we can renumber etc.
NGHolder h;
cloneHolder(h, h_in);
@@ -739,16 +741,16 @@ aligned_unique_ptr<NFA> constructReversedNFA_i(const NGHolder &h_in, u32 hint,
zombies, false, false, hint, cc);
}
aligned_unique_ptr<NFA> constructReversedNFA(const NGHolder &h_in,
const CompileContext &cc) {
bytecode_ptr<NFA> constructReversedNFA(const NGHolder &h_in,
const CompileContext &cc) {
u32 hint = INVALID_NFA; // no hint
return constructReversedNFA_i(h_in, hint, cc);
}
#ifndef RELEASE_BUILD
// Variant that allows a hint to be specified.
aligned_unique_ptr<NFA> constructReversedNFA(const NGHolder &h_in, u32 hint,
const CompileContext &cc) {
bytecode_ptr<NFA> constructReversedNFA(const NGHolder &h_in, u32 hint,
const CompileContext &cc) {
return constructReversedNFA_i(h_in, hint, cc);
}
#endif // RELEASE_BUILD

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, Intel Corporation
* Copyright (c) 2015-2017, Intel Corporation
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -26,7 +26,8 @@
* POSSIBILITY OF SUCH DAMAGE.
*/
/** \file
/**
* \file
* \brief Limex NFA construction code.
*/
@@ -35,7 +36,7 @@
#include "ue2common.h"
#include "som/som.h"
#include "util/alloc.h"
#include "util/bytecode_ptr.h"
#include <map>
#include <memory>
@@ -51,7 +52,8 @@ class NGHolder;
class ReportManager;
struct CompileContext;
/** \brief Determine if the given graph is implementable as an NFA.
/**
* \brief Determine if the given graph is implementable as an NFA.
*
* Returns zero if the NFA is not implementable (usually because it has too
* many states for any of our models). Otherwise returns the number of states.
@@ -62,11 +64,14 @@ struct CompileContext;
u32 isImplementableNFA(const NGHolder &g, const ReportManager *rm,
const CompileContext &cc);
/** \brief Late-stage graph reductions.
/**
* \brief Late-stage graph reductions.
*
* This will call \ref removeRedundancy and apply its changes to the given
* holder only if it is implementable afterwards. */
void reduceImplementableGraph(NGHolder &g, som_type som, const ReportManager *rm,
* holder only if it is implementable afterwards.
*/
void reduceImplementableGraph(NGHolder &g, som_type som,
const ReportManager *rm,
const CompileContext &cc);
/**
@@ -79,7 +84,8 @@ void reduceImplementableGraph(NGHolder &g, som_type som, const ReportManager *rm
u32 countAccelStates(const NGHolder &g, const ReportManager *rm,
const CompileContext &cc);
/** \brief Construct an NFA from the given NFAGraph.
/**
* \brief Construct an NFA from the given graph.
*
* Returns zero if the NFA is not implementable (usually because it has too
* many states for any of our models). Otherwise returns the number of states.
@@ -90,23 +96,25 @@ u32 countAccelStates(const NGHolder &g, const ReportManager *rm,
* Note: this variant of the function allows a model to be specified with the
* \a hint parameter.
*/
aligned_unique_ptr<NFA>
bytecode_ptr<NFA>
constructNFA(const NGHolder &g, const ReportManager *rm,
const std::map<u32, u32> &fixed_depth_tops,
const std::map<u32, std::vector<std::vector<CharReach>>> &triggers,
bool compress_state, const CompileContext &cc);
/** \brief Build a reverse NFA from the graph given, which should have already
/**
* \brief Build a reverse NFA from the graph given, which should have already
* been reversed.
*
* Used for reverse NFAs used in SOM mode.
*/
aligned_unique_ptr<NFA> constructReversedNFA(const NGHolder &h,
const CompileContext &cc);
bytecode_ptr<NFA> constructReversedNFA(const NGHolder &h,
const CompileContext &cc);
#ifndef RELEASE_BUILD
/** \brief Construct an NFA (with model type hint) from the given NFAGraph.
/**
* \brief Construct an NFA (with model type hint) from the given graph.
*
* Returns zero if the NFA is not implementable (usually because it has too
* many states for any of our models). Otherwise returns the number of states.
@@ -117,19 +125,20 @@ aligned_unique_ptr<NFA> constructReversedNFA(const NGHolder &h,
* Note: this variant of the function allows a model to be specified with the
* \a hint parameter.
*/
aligned_unique_ptr<NFA>
bytecode_ptr<NFA>
constructNFA(const NGHolder &g, const ReportManager *rm,
const std::map<u32, u32> &fixed_depth_tops,
const std::map<u32, std::vector<std::vector<CharReach>>> &triggers,
bool compress_state, u32 hint, const CompileContext &cc);
/** \brief Build a reverse NFA (with model type hint) from the graph given,
/**
* \brief Build a reverse NFA (with model type hint) from the graph given,
* which should have already been reversed.
*
* Used for reverse NFAs used in SOM mode.
*/
aligned_unique_ptr<NFA> constructReversedNFA(const NGHolder &h, u32 hint,
const CompileContext &cc);
bytecode_ptr<NFA> constructReversedNFA(const NGHolder &h, u32 hint,
const CompileContext &cc);
#endif // RELEASE_BUILD

View File

@@ -26,7 +26,8 @@
* POSSIBILITY OF SUCH DAMAGE.
*/
/** \file
/**
* \file
* \brief SOM ("Start of Match") analysis.
*/
@@ -1731,19 +1732,19 @@ void clearProperInEdges(NGHolder &g, const NFAVertex sink) {
namespace {
struct SomRevNfa {
SomRevNfa(NFAVertex s, ReportID r, aligned_unique_ptr<NFA> n)
SomRevNfa(NFAVertex s, ReportID r, bytecode_ptr<NFA> n)
: sink(s), report(r), nfa(move(n)) {}
SomRevNfa(SomRevNfa&& s) // MSVC2013 needs this for emplace
: sink(s.sink), report(s.report), nfa(move(s.nfa)) {}
NFAVertex sink;
ReportID report;
aligned_unique_ptr<NFA> nfa;
bytecode_ptr<NFA> nfa;
};
}
static
aligned_unique_ptr<NFA> makeBareSomRevNfa(const NGHolder &g,
const CompileContext &cc) {
bytecode_ptr<NFA> makeBareSomRevNfa(const NGHolder &g,
const CompileContext &cc) {
// Create a reversed anchored version of this NFA which fires a zero report
// ID on accept.
NGHolder g_rev;
@@ -1759,7 +1760,7 @@ aligned_unique_ptr<NFA> makeBareSomRevNfa(const NGHolder &g,
DEBUG_PRINTF("building a rev NFA with %zu vertices\n", num_vertices(g_rev));
aligned_unique_ptr<NFA> nfa = constructReversedNFA(g_rev, cc);
auto nfa = constructReversedNFA(g_rev, cc);
if (!nfa) {
return nfa;
}
@@ -1794,7 +1795,7 @@ bool makeSomRevNfa(vector<SomRevNfa> &som_nfas, const NGHolder &g,
renumber_vertices(g2); // for findMinWidth, findMaxWidth.
aligned_unique_ptr<NFA> nfa = makeBareSomRevNfa(g2, cc);
auto nfa = makeBareSomRevNfa(g2, cc);
if (!nfa) {
DEBUG_PRINTF("couldn't build rev nfa\n");
return false;