aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/regex/hyperscan/hyperscan.cpp
blob: 98e402962da87786f4e0112a86e8bbb6399d0a69 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include "hyperscan.h"

#include <contrib/libs/hyperscan/runtime_core2/hs_common.h>
#include <contrib/libs/hyperscan/runtime_core2/hs_runtime.h>
#include <contrib/libs/hyperscan/runtime_corei7/hs_common.h>
#include <contrib/libs/hyperscan/runtime_corei7/hs_runtime.h>
#include <contrib/libs/hyperscan/runtime_avx2/hs_common.h>
#include <contrib/libs/hyperscan/runtime_avx2/hs_runtime.h>
#include <contrib/libs/hyperscan/runtime_avx512/hs_common.h>
#include <contrib/libs/hyperscan/runtime_avx512/hs_runtime.h>

#include <util/generic/singleton.h>

namespace NHyperscan {
    using TSerializedDatabase = THolder<char, TDeleter<decltype(&free), &free>>;

    using TCompileError = THolder<hs_compile_error_t, TDeleter<decltype(&hs_free_compile_error), &hs_free_compile_error>>;

    namespace NPrivate {
        ERuntime DetectCurrentRuntime() {
            if (NX86::HaveAVX512F() && NX86::HaveAVX512BW()) {
                return ERuntime::AVX512;
            } else if (NX86::HaveAVX() && NX86::HaveAVX2()) {
                return ERuntime::AVX2;
            } else if (NX86::HaveSSE42() && NX86::HavePOPCNT()) {
                return ERuntime::Corei7;
            } else {
                return ERuntime::Core2;
            }
        }

        TCPUFeatures RuntimeCpuFeatures(ERuntime runtime) {
            switch (runtime) {
                default:
                    Y_ASSERT(false);
                    [[fallthrough]];
                case ERuntime::Core2:
                case ERuntime::Corei7:
                    return 0;
                case ERuntime::AVX2:
                    return CPU_FEATURES_AVX2;
                case ERuntime::AVX512:
                    return CPU_FEATURES_AVX512;
            }
        }

        hs_platform_info_t MakePlatformInfo(TCPUFeatures cpuFeatures) {
            hs_platform_info_t platformInfo{HS_TUNE_FAMILY_GENERIC, cpuFeatures, 0, 0};
            return platformInfo;
        }

        hs_platform_info_t MakeCurrentPlatformInfo() {
            return MakePlatformInfo(RuntimeCpuFeatures(DetectCurrentRuntime()));
        }

        TImpl::TImpl(ERuntime runtime) {
            switch (runtime) {
                default:
                    Y_ASSERT(false);
                    [[fallthrough]];
                case ERuntime::Core2:
                    AllocScratch = core2_hs_alloc_scratch;
                    Scan = core2_hs_scan;
                    SerializeDatabase = core2_hs_serialize_database;
                    DeserializeDatabase = core2_hs_deserialize_database;
                    break;
                case ERuntime::Corei7:
                    AllocScratch = corei7_hs_alloc_scratch;
                    Scan = corei7_hs_scan;
                    SerializeDatabase = corei7_hs_serialize_database;
                    DeserializeDatabase = corei7_hs_deserialize_database;
                    break;
                case ERuntime::AVX2:
                    AllocScratch = avx2_hs_alloc_scratch;
                    Scan = avx2_hs_scan;
                    SerializeDatabase = avx2_hs_serialize_database;
                    DeserializeDatabase = avx2_hs_deserialize_database;
                    break;
                case ERuntime::AVX512:
                    AllocScratch = avx512_hs_alloc_scratch;
                    Scan = avx512_hs_scan;
                    SerializeDatabase = avx512_hs_serialize_database;
                    DeserializeDatabase = avx512_hs_deserialize_database;
            }
        }

        TDatabase Compile(const TStringBuf& regex, unsigned int flags, hs_platform_info_t* platform) {
            hs_database_t* rawDb = nullptr;
            hs_compile_error_t* rawCompileErr = nullptr;
            hs_error_t status = hs_compile(
                    regex.begin(),
                    flags,
                    HS_MODE_BLOCK,
                    platform,
                    &rawDb,
                    &rawCompileErr);
            TDatabase db(rawDb);
            NHyperscan::TCompileError compileError(rawCompileErr);
            if (status != HS_SUCCESS) {
                ythrow TCompileException()
                        << "Failed to compile regex: " << regex << ". "
                        << "Error message (hyperscan): " << compileError->message;
            }
            return db;
        }

        TDatabase CompileMulti(
                const TVector<const char*>& regexs,
                const TVector<unsigned int>& flags,
                const TVector<unsigned int>& ids,
                hs_platform_info_t* platform,
                const TVector<const hs_expr_ext_t*>* extendedParameters) {
            unsigned int count = regexs.size();
            if (flags.size() != count) {
                ythrow yexception()
                        << "Mismatch of sizes vectors passed to CompileMulti. "
                        << "size(regexs) = " << regexs.size() << ". "
                        << "size(flags) = " << flags.size() << ".";
            }
            if (ids.size() != count) {
                ythrow yexception()
                        << "Mismatch of sizes vectors passed to CompileMulti. "
                        << "size(regexs) = " << regexs.size() << ". "
                        << "size(ids) = " << ids.size() << ".";
            }
            if (extendedParameters && extendedParameters->size() != count) {
                ythrow yexception()
                        << "Mismatch of sizes vectors passed to CompileMulti. "
                        << "size(regexs) = " << regexs.size() << ". "
                        << "size(extendedParameters) = " << extendedParameters->size() << ".";
            }
            hs_database_t* rawDb = nullptr;
            hs_compile_error_t* rawCompileErr = nullptr;
            hs_error_t status = hs_compile_ext_multi(
                    regexs.data(),
                    flags.data(),
                    ids.data(),
                    extendedParameters ? extendedParameters->data() : nullptr,
                    count,
                    HS_MODE_BLOCK,
                    platform,
                    &rawDb,
                    &rawCompileErr);
            TDatabase db(rawDb);
            NHyperscan::TCompileError compileError(rawCompileErr);
            if (status != HS_SUCCESS) {
                if (compileError->expression >= 0) {
                    const char* regex = regexs[compileError->expression];
                    ythrow TCompileException()
                            << "Failed to compile regex: " << regex << ". "
                            << "Error message (hyperscan): " << compileError->message;
                } else {
                    ythrow TCompileException()
                            << "Failed to compile multiple regexs. "
                            << "Error message (hyperscan): " << compileError->message;
                }
            }
            return db;
        }

        bool Matches(
                const TDatabase& db,
                const TScratch& scratch,
                const TStringBuf& text,
                const TImpl& impl) {
            bool result = false;
            auto callback = [&](unsigned int /* id */, unsigned long long /* from */, unsigned long long /* to */) {
                result = true;
                return 1; // stop scan
            };
            Scan(
                    db,
                    scratch,
                    text,
                    callback,
                    impl);
            return result;
        }
    } // namespace NPrivate

    TDatabase Compile(const TStringBuf& regex, unsigned int flags) {
        auto platformInfo = NPrivate::MakeCurrentPlatformInfo();
        return NPrivate::Compile(regex, flags, &platformInfo);
    }

    TDatabase Compile(const TStringBuf& regex, unsigned int flags, TCPUFeatures cpuFeatures) {
        auto platformInfo = NPrivate::MakePlatformInfo(cpuFeatures);
        return NPrivate::Compile(regex, flags, &platformInfo);
    }

    TDatabase CompileMulti(
            const TVector<const char*>& regexs,
            const TVector<unsigned int>& flags,
            const TVector<unsigned int>& ids,
            const TVector<const hs_expr_ext_t*>* extendedParameters)
    {
        auto platformInfo = NPrivate::MakeCurrentPlatformInfo();
        return NPrivate::CompileMulti(regexs, flags, ids, &platformInfo, extendedParameters);
    }

    TDatabase CompileMulti(
        const TVector<const char*>& regexs,
        const TVector<unsigned int>& flags,
        const TVector<unsigned int>& ids,
        TCPUFeatures cpuFeatures,
        const TVector<const hs_expr_ext_t*>* extendedParameters)
    {
        auto platformInfo = NPrivate::MakePlatformInfo(cpuFeatures);
        return NPrivate::CompileMulti(regexs, flags, ids, &platformInfo, extendedParameters);
    }

    TScratch MakeScratch(const TDatabase& db) {
        hs_scratch_t* rawScratch = nullptr;
        hs_error_t status = Singleton<NPrivate::TImpl>()->AllocScratch(db.Get(), &rawScratch);
        NHyperscan::TScratch scratch(rawScratch);
        if (status != HS_SUCCESS) {
            ythrow yexception() << "Failed to make scratch for hyperscan database";
        }
        return scratch;
    }

    void GrowScratch(TScratch& scratch, const TDatabase& db) {
        hs_scratch_t* rawScratch = scratch.Get();
        hs_error_t status = Singleton<NPrivate::TImpl>()->AllocScratch(db.Get(), &rawScratch);
        if (rawScratch != scratch.Get()) {
            Y_UNUSED(scratch.Release()); // freed by hs_alloc_scratch
            scratch.Reset(rawScratch);
        }
        if (status != HS_SUCCESS) {
            ythrow yexception() << "Failed to make grow scratch for hyperscan database";
        }
    }

    TScratch CloneScratch(const TScratch& scratch) {
        hs_scratch_t* rawScratch = nullptr;
        hs_error_t status = hs_clone_scratch(scratch.Get(), &rawScratch);
        TScratch scratchCopy(rawScratch);
        if (status != HS_SUCCESS) {
            ythrow yexception() << "Failed to clone scratch for hyperscan database";
        }
        return scratchCopy;
    }

    bool Matches(
        const TDatabase& db,
        const TScratch& scratch,
        const TStringBuf& text)
    {
        return NPrivate::Matches(db, scratch, text, *Singleton<NPrivate::TImpl>());
    }

    TString Serialize(const TDatabase& db) { 
        char* databaseBytes = nullptr;
        size_t databaseLength;
        hs_error_t status = Singleton<NPrivate::TImpl>()->SerializeDatabase(
            db.Get(),
            &databaseBytes,
            &databaseLength);
        TSerializedDatabase serialization(databaseBytes);
        if (status != HS_SUCCESS) {
            ythrow yexception() << "Failed to serialize hyperscan database";
        }
        return TString(serialization.Get(), databaseLength); 
    }

    TDatabase Deserialize(const TStringBuf& serialization) {
        hs_database_t* rawDb = nullptr;
        hs_error_t status = Singleton<NPrivate::TImpl>()->DeserializeDatabase(
            serialization.begin(),
            serialization.size(),
            &rawDb);
        TDatabase db(rawDb);
        if (status != HS_SUCCESS) {
            if (status == HS_DB_PLATFORM_ERROR) {
                ythrow yexception() << "Serialized Hyperscan database is incompatible with current CPU";
            } else {
                ythrow yexception() << "Failed to deserialize hyperscan database";
            }
        }
        return db;
    }
}