mirror of
https://github.com/VectorCamp/vectorscan.git
synced 2025-09-29 11:16:29 +03:00
Logical Combination of patterns.
This commit is contained in:
77
src/util/logical.h
Normal file
77
src/util/logical.h
Normal file
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Copyright (c) 2018, Intel Corporation
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of Intel Corporation nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/** \file
|
||||
* \brief Inline functions for manipulating logical combinations.
|
||||
*/
|
||||
|
||||
#ifndef LOGICAL_H
|
||||
#define LOGICAL_H
|
||||
|
||||
#include "ue2common.h"
|
||||
|
||||
/** Index meaning a given logical key is invalid. */
|
||||
#define INVALID_LKEY (~(u32)0)
|
||||
#define INVALID_CKEY INVALID_LKEY
|
||||
|
||||
/** Logical operation type, the priority is from high to low. */
|
||||
enum LogicalOpType {
|
||||
LOGICAL_OP_NOT,
|
||||
LOGICAL_OP_AND,
|
||||
LOGICAL_OP_OR,
|
||||
LAST_LOGICAL_OP = LOGICAL_OP_OR //!< Sentinel.
|
||||
};
|
||||
|
||||
#define UNKNOWN_OP (~(u32)0)
|
||||
|
||||
/** Logical Operation is consist of 4 parts. */
|
||||
struct LogicalOp {
|
||||
u32 id; //!< logical operator/operation id
|
||||
u32 op; //!< LogicalOpType
|
||||
u32 lo; //!< left operand
|
||||
u32 ro; //!< right operand
|
||||
};
|
||||
|
||||
/** Each logical combination has its info:
|
||||
* It occupies a region in LogicalOp vector.
|
||||
* It has an exhaustion key for single-match mode. */
|
||||
struct CombInfo {
|
||||
u32 id;
|
||||
u32 ekey; //!< exhaustion key
|
||||
u32 start; //!< ckey of logical operation to start calculating
|
||||
u32 result; //!< ckey of logical operation to give final result
|
||||
u64a min_offset;
|
||||
u64a max_offset;
|
||||
};
|
||||
|
||||
/** Temporarily use to seperate operations' id from reports' lkey
|
||||
* when building logicalTree in shunting yard algorithm,
|
||||
* operations' id will be finally renumbered following reports' lkey. */
|
||||
#define LOGICAL_OP_BIT 0x80000000UL
|
||||
|
||||
#endif
|
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2015-2017, Intel Corporation
|
||||
* Copyright (c) 2015-2018, Intel Corporation
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
@@ -36,6 +36,7 @@
|
||||
|
||||
#include "ue2common.h"
|
||||
#include "util/exhaust.h" // for INVALID_EKEY
|
||||
#include "util/logical.h" // for INVALID_LKEY
|
||||
#include "util/hash.h"
|
||||
#include "util/order_check.h"
|
||||
|
||||
@@ -107,6 +108,16 @@ struct Report {
|
||||
* exhaustible, this will be INVALID_EKEY. */
|
||||
u32 ekey = INVALID_EKEY;
|
||||
|
||||
/** \brief Logical Combination key in each combination.
|
||||
*
|
||||
* If in Logical Combination, the lkey to check before reporting a match.
|
||||
* Additionally before checking the lkey will be set. If not
|
||||
* in Logical Combination, this will be INVALID_LKEY. */
|
||||
u32 lkey = INVALID_LKEY;
|
||||
|
||||
/** \brief Quiet flag for expressions in any logical combination. */
|
||||
bool quiet = false;
|
||||
|
||||
/** \brief Adjustment to add to the match offset when we report a match.
|
||||
*
|
||||
* This is usually used for reports attached to states that form part of a
|
||||
@@ -207,16 +218,17 @@ bool operator==(const Report &a, const Report &b) {
|
||||
}
|
||||
|
||||
static inline
|
||||
Report makeECallback(u32 report, s32 offsetAdjust, u32 ekey) {
|
||||
Report makeECallback(u32 report, s32 offsetAdjust, u32 ekey, bool quiet) {
|
||||
Report ir(EXTERNAL_CALLBACK, report);
|
||||
ir.offsetAdjust = offsetAdjust;
|
||||
ir.ekey = ekey;
|
||||
ir.quiet = (u8)quiet;
|
||||
return ir;
|
||||
}
|
||||
|
||||
static inline
|
||||
Report makeCallback(u32 report, s32 offsetAdjust) {
|
||||
return makeECallback(report, offsetAdjust, INVALID_EKEY);
|
||||
return makeECallback(report, offsetAdjust, INVALID_EKEY, false);
|
||||
}
|
||||
|
||||
static inline
|
||||
|
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2015-2017, Intel Corporation
|
||||
* Copyright (c) 2015-2018, Intel Corporation
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
@@ -95,6 +95,31 @@ u32 ReportManager::getExhaustibleKey(u32 a) {
|
||||
return it->second;
|
||||
}
|
||||
|
||||
const set<u32> &ReportManager::getRelateCKeys(u32 lkey) {
|
||||
auto it = pl.lkey2ckeys.find(lkey);
|
||||
assert(it != pl.lkey2ckeys.end());
|
||||
return it->second;
|
||||
}
|
||||
|
||||
void ReportManager::logicalKeyRenumber() {
|
||||
pl.logicalKeyRenumber();
|
||||
// assign to corresponding report
|
||||
for (u32 i = 0; i < reportIds.size(); i++) {
|
||||
Report &ir = reportIds[i];
|
||||
if (contains(pl.toLogicalKeyMap, ir.onmatch)) {
|
||||
ir.lkey = pl.toLogicalKeyMap.at(ir.onmatch);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const vector<LogicalOp> &ReportManager::getLogicalTree() const {
|
||||
return pl.logicalTree;
|
||||
}
|
||||
|
||||
const vector<CombInfo> &ReportManager::getCombInfoMap() const {
|
||||
return pl.combInfoMap;
|
||||
}
|
||||
|
||||
u32 ReportManager::getUnassociatedExhaustibleKey(void) {
|
||||
u32 rv = toExhaustibleKeyMap.size();
|
||||
bool inserted;
|
||||
@@ -115,6 +140,18 @@ u32 ReportManager::numEkeys() const {
|
||||
return (u32) toExhaustibleKeyMap.size();
|
||||
}
|
||||
|
||||
u32 ReportManager::numLogicalKeys() const {
|
||||
return (u32) pl.toLogicalKeyMap.size();
|
||||
}
|
||||
|
||||
u32 ReportManager::numLogicalOps() const {
|
||||
return (u32) pl.logicalTree.size();
|
||||
}
|
||||
|
||||
u32 ReportManager::numCkeys() const {
|
||||
return (u32) pl.toCombKeyMap.size();
|
||||
}
|
||||
|
||||
bool ReportManager::patternSetCanExhaust() const {
|
||||
return global_exhaust && !toExhaustibleKeyMap.empty();
|
||||
}
|
||||
@@ -219,7 +256,7 @@ Report ReportManager::getBasicInternalReport(const ExpressionInfo &expr,
|
||||
ekey = getExhaustibleKey(expr.report);
|
||||
}
|
||||
|
||||
return makeECallback(expr.report, adj, ekey);
|
||||
return makeECallback(expr.report, adj, ekey, expr.quiet);
|
||||
}
|
||||
|
||||
void ReportManager::setProgramOffset(ReportID id, u32 programOffset) {
|
||||
|
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2015-2017, Intel Corporation
|
||||
* Copyright (c) 2015-2018, Intel Corporation
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
@@ -38,6 +38,7 @@
|
||||
#include "util/compile_error.h"
|
||||
#include "util/noncopyable.h"
|
||||
#include "util/report.h"
|
||||
#include "parser/logical_combination.h"
|
||||
|
||||
#include <map>
|
||||
#include <set>
|
||||
@@ -80,6 +81,15 @@ public:
|
||||
/** \brief Total number of exhaustion keys. */
|
||||
u32 numEkeys() const;
|
||||
|
||||
/** \brief Total number of logical keys. */
|
||||
u32 numLogicalKeys() const;
|
||||
|
||||
/** \brief Total number of logical operators. */
|
||||
u32 numLogicalOps() const;
|
||||
|
||||
/** \brief Total number of combination keys. */
|
||||
u32 numCkeys() const;
|
||||
|
||||
/** \brief True if the pattern set can exhaust (i.e. all patterns are
|
||||
* highlander). */
|
||||
bool patternSetCanExhaust() const;
|
||||
@@ -110,6 +120,19 @@ public:
|
||||
* assigning one if necessary. */
|
||||
u32 getExhaustibleKey(u32 expressionIndex);
|
||||
|
||||
/** \brief Get lkey's corresponding ckeys. */
|
||||
const std::set<u32> &getRelateCKeys(u32 lkey);
|
||||
|
||||
/** \brief Renumber lkey for logical operations, after parsed
|
||||
* all logical expressions. */
|
||||
void logicalKeyRenumber();
|
||||
|
||||
/** \brief Used in Rose for writing bytecode. */
|
||||
const std::vector<LogicalOp> &getLogicalTree() const;
|
||||
|
||||
/** \brief Used in Rose for writing bytecode. */
|
||||
const std::vector<CombInfo> &getCombInfoMap() const;
|
||||
|
||||
/** \brief Fetch the dedupe key associated with the given report. Returns
|
||||
* ~0U if no dkey is needed. */
|
||||
u32 getDkey(const Report &r) const;
|
||||
@@ -122,6 +145,9 @@ public:
|
||||
* set. */
|
||||
u32 getProgramOffset(ReportID id) const;
|
||||
|
||||
/** \brief Parsed logical combination structure. */
|
||||
ParsedLogical pl;
|
||||
|
||||
private:
|
||||
/** \brief Grey box ref, for checking resource limits. */
|
||||
const Grey &grey;
|
||||
|
Reference in New Issue
Block a user