| 1 | /* |
| 2 | * Copyright (c) 2015-2017, Intel Corporation |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are met: |
| 6 | * |
| 7 | * * Redistributions of source code must retain the above copyright notice, |
| 8 | * this list of conditions and the following disclaimer. |
| 9 | * * Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * * Neither the name of Intel Corporation nor the names of its contributors |
| 13 | * may be used to endorse or promote products derived from this software |
| 14 | * without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 | * POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | /** \file |
| 30 | * \brief Hamster Wheel Literal Matcher: build API. |
| 31 | */ |
| 32 | |
| 33 | #ifndef HWLM_BUILD_H |
| 34 | #define HWLM_BUILD_H |
| 35 | |
| 36 | #include "hwlm.h" |
| 37 | #include "hwlm_literal.h" |
| 38 | #include "ue2common.h" |
| 39 | #include "util/bytecode_ptr.h" |
| 40 | |
| 41 | #include <map> |
| 42 | #include <memory> |
| 43 | #include <vector> |
| 44 | |
| 45 | struct HWLM; |
| 46 | |
| 47 | namespace ue2 { |
| 48 | |
| 49 | class FDREngineDescription; |
| 50 | class TeddyEngineDescription; |
| 51 | struct CompileContext; |
| 52 | struct Grey; |
| 53 | |
| 54 | /** \brief Class representing a literal matcher prototype. */ |
| 55 | struct HWLMProto { |
| 56 | /** |
| 57 | * \brief Engine type to distinguish noodle from FDR and Teddy. |
| 58 | */ |
| 59 | u8 engType; |
| 60 | |
| 61 | /** |
| 62 | * \brief FDR engine description. |
| 63 | */ |
| 64 | std::unique_ptr<FDREngineDescription> fdrEng; |
| 65 | |
| 66 | /** |
| 67 | * \brief Teddy engine description. |
| 68 | */ |
| 69 | std::unique_ptr<TeddyEngineDescription> teddyEng; |
| 70 | |
| 71 | /** |
| 72 | * \brief HWLM literals passed from Rose. |
| 73 | */ |
| 74 | std::vector<hwlmLiteral> lits; |
| 75 | |
| 76 | /** |
| 77 | * \brief Bucket assignment info in FDR and Teddy |
| 78 | */ |
| 79 | std::map<u32, std::vector<u32>> bucketToLits; |
| 80 | |
| 81 | /** |
| 82 | * \brief Flag to optimise matcher for small size from Rose. |
| 83 | */ |
| 84 | bool make_small = false; |
| 85 | |
| 86 | HWLMProto(u8 engType_in, std::vector<hwlmLiteral> lits_in); |
| 87 | |
| 88 | HWLMProto(u8 engType_in, std::unique_ptr<FDREngineDescription> eng_in, |
| 89 | std::vector<hwlmLiteral> lits_in, |
| 90 | std::map<u32, std::vector<u32>> bucketToLits_in, |
| 91 | bool make_small_in); |
| 92 | |
| 93 | HWLMProto(u8 engType_in, std::unique_ptr<TeddyEngineDescription> eng_in, |
| 94 | std::vector<hwlmLiteral> lits_in, |
| 95 | std::map<u32, std::vector<u32>> bucketToLits_in, |
| 96 | bool make_small_in); |
| 97 | |
| 98 | ~HWLMProto(); |
| 99 | }; |
| 100 | |
| 101 | /** \brief Build an \ref HWLM literal matcher runtime structure for a group of |
| 102 | * literals. |
| 103 | * |
| 104 | * \param proto Literal matcher prototype. |
| 105 | * \param cc Compile context. |
| 106 | * \param expected_groups FIXME: document me! |
| 107 | * |
| 108 | * Build failures are generally a result of memory allocation failure. These |
| 109 | * may result in a nullptr return value, or a std::bad_alloc exception being |
| 110 | * thrown. |
| 111 | */ |
| 112 | bytecode_ptr<HWLM> hwlmBuild(const HWLMProto &proto, const CompileContext &cc, |
| 113 | hwlm_group_t expected_groups = HWLM_ALL_GROUPS); |
| 114 | |
| 115 | std::unique_ptr<HWLMProto> |
| 116 | hwlmBuildProto(std::vector<hwlmLiteral> &lits, bool make_small, |
| 117 | const CompileContext &cc); |
| 118 | |
| 119 | /** |
| 120 | * Returns an estimate of the number of repeated characters on the end of a |
| 121 | * literal that will make a literal set of size \a numLiterals suffer |
| 122 | * performance degradation. |
| 123 | */ |
| 124 | size_t hwlmFloodProneSuffixLen(size_t numLiterals, const CompileContext &cc); |
| 125 | |
| 126 | /** \brief Return the size in bytes of an HWLM structure. */ |
| 127 | size_t hwlmSize(const HWLM *h); |
| 128 | |
| 129 | } // namespace |
| 130 | |
| 131 | #endif // HWLM_BUILD_H |
| 132 | |