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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
|
/*
* Copyright 2023 Vsevolod Stakhov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef RSPAMD_SYMCACHE_ITEM_HXX
#define RSPAMD_SYMCACHE_ITEM_HXX
#pragma once
#include <utility>
#include <vector>
#include <string>
#include <string_view>
#include <memory>
#include <variant>
#include <algorithm>
#include <optional>
#include "rspamd_symcache.h"
#include "symcache_id_list.hxx"
#include "contrib/expected/expected.hpp"
#include "contrib/libev/ev.h"
#include "symcache_runtime.hxx"
#include "libutil/cxx/hash_util.hxx"
namespace rspamd::symcache {
class symcache;
struct cache_item;
using cache_item_ptr = std::shared_ptr<cache_item>;
enum class symcache_item_type {
CONNFILTER, /* Executed on connection stage */
PREFILTER, /* Executed before all filters */
FILTER, /* Normal symbol with a callback */
POSTFILTER, /* Executed after all filters */
IDEMPOTENT, /* Executed after postfilters, cannot change results */
CLASSIFIER, /* A virtual classifier symbol */
COMPOSITE, /* A virtual composite symbol */
VIRTUAL, /* A virtual symbol... */
};
/*
* Compare item types: earlier stages symbols are > than later stages symbols
* Order for virtual stuff is not defined.
*/
bool operator<(symcache_item_type lhs, symcache_item_type rhs);
constexpr static auto item_type_to_str(symcache_item_type t) -> const char *
{
switch (t) {
case symcache_item_type::CONNFILTER:
return "connfilter";
case symcache_item_type::PREFILTER:
return "prefilter";
case symcache_item_type::FILTER:
return "filter";
case symcache_item_type::POSTFILTER:
return "postfilter";
case symcache_item_type::IDEMPOTENT:
return "idempotent";
case symcache_item_type::CLASSIFIER:
return "classifier";
case symcache_item_type::COMPOSITE:
return "composite";
case symcache_item_type::VIRTUAL:
return "virtual";
}
}
/**
* This is a public helper to convert a legacy C type to a more static type
* @param type input type as a C enum
* @return pair of type safe symcache_item_type + the remaining flags or an error
*/
auto item_type_from_c(int type) -> tl::expected<std::pair<symcache_item_type, int>, std::string>;
struct item_condition {
private:
lua_State *L = nullptr;
int cb = -1;
public:
explicit item_condition(lua_State *L_, int cb_) noexcept
: L(L_), cb(cb_)
{
}
item_condition(item_condition &&other) noexcept
{
*this = std::move(other);
}
/* Make it move only */
item_condition(const item_condition &) = delete;
item_condition &operator=(item_condition &&other) noexcept
{
std::swap(other.L, L);
std::swap(other.cb, cb);
return *this;
}
~item_condition();
auto check(std::string_view sym_name, struct rspamd_task *task) const -> bool;
};
class normal_item {
private:
symbol_func_t func = nullptr;
void *user_data = nullptr;
std::vector<cache_item *> virtual_children;
std::vector<item_condition> conditions;
public:
explicit normal_item(symbol_func_t _func, void *_user_data)
: func(_func), user_data(_user_data)
{
}
auto add_condition(lua_State *L, int cbref) -> void
{
conditions.emplace_back(L, cbref);
}
auto call(struct rspamd_task *task, struct rspamd_symcache_dynamic_item *item) const -> void
{
func(task, item, user_data);
}
auto check_conditions(std::string_view sym_name, struct rspamd_task *task) const -> bool
{
return std::all_of(std::begin(conditions), std::end(conditions),
[&](const auto &cond) { return cond.check(sym_name, task); });
}
auto get_cbdata() const -> auto
{
return user_data;
}
auto add_child(cache_item *ptr) -> void
{
virtual_children.push_back(ptr);
}
auto get_childen() const -> const std::vector<cache_item *> &
{
return virtual_children;
}
};
class virtual_item {
private:
int parent_id = -1;
cache_item *parent = nullptr;
public:
explicit virtual_item(int _parent_id)
: parent_id(_parent_id)
{
}
auto get_parent(const symcache &cache) const -> const cache_item *;
auto get_parent_mut(const symcache &cache) -> cache_item *;
auto resolve_parent(const symcache &cache) -> bool;
};
struct cache_dependency {
cache_item *item; /* Real dependency */
std::string sym; /* Symbolic dep name */
int id; /* Real from */
int vid; /* Virtual from */
public:
/* Default piecewise constructor */
explicit cache_dependency(cache_item *_item, std::string _sym, int _id, int _vid)
: item(_item), sym(std::move(_sym)), id(_id), vid(_vid)
{
}
};
/*
* Used to store augmentation values
*/
struct item_augmentation {
std::variant<std::monostate, std::string, double> value;
int weight;
explicit item_augmentation(int weight)
: value(std::monostate{}), weight(weight)
{
}
explicit item_augmentation(std::string str_value, int weight)
: value(str_value), weight(weight)
{
}
explicit item_augmentation(double double_value, int weight)
: value(double_value), weight(weight)
{
}
};
struct cache_item : std::enable_shared_from_this<cache_item> {
/* The following fields will live in shared memory */
struct rspamd_symcache_item_stat *st = nullptr;
struct rspamd_counter_data *cd = nullptr;
/* Unique id - counter */
int id;
std::uint64_t last_count = 0;
std::string symbol;
symcache_item_type type;
int flags;
/* Condition of execution */
bool enabled = true;
/* Priority */
int priority = 0;
/* Topological order */
unsigned int order = 0;
int frequency_peaks = 0;
/* Specific data for virtual and callback symbols */
std::variant<normal_item, virtual_item> specific;
/* Settings ids */
id_list allowed_ids;
/* Allows execution but not symbols insertion */
id_list exec_only_ids;
id_list forbidden_ids;
/* Set of augmentations */
ankerl::unordered_dense::map<std::string, item_augmentation,
rspamd::smart_str_hash, rspamd::smart_str_equal>
augmentations;
/* Dependencies */
std::vector<cache_dependency> deps;
/* Reverse dependencies */
std::vector<cache_dependency> rdeps;
public:
/**
* Create a normal item with a callback
* @param name
* @param priority
* @param func
* @param user_data
* @param type
* @param flags
* @return
*/
template<typename T>
static auto create_with_function(rspamd_mempool_t *pool,
int id,
T &&name,
int priority,
symbol_func_t func,
void *user_data,
symcache_item_type type,
int flags) -> cache_item_ptr
{
return std::shared_ptr<cache_item>(new cache_item(pool,
id, std::forward<T>(name), priority,
func, user_data,
type, flags));
}
/**
* Create a virtual item
* @param name
* @param priority
* @param parent
* @param type
* @param flags
* @return
*/
template<typename T>
static auto create_with_virtual(rspamd_mempool_t *pool,
int id,
T &&name,
int parent,
symcache_item_type type,
int flags) -> cache_item_ptr
{
return std::shared_ptr<cache_item>(new cache_item(pool, id, std::forward<T>(name),
parent, type, flags));
}
/**
* Share ownership on the item
* @return
*/
auto getptr() -> cache_item_ptr
{
return shared_from_this();
}
/**
* Process and resolve dependencies for the item
* @param cache
*/
auto process_deps(const symcache &cache) -> void;
auto is_virtual() const -> bool
{
return std::holds_alternative<virtual_item>(specific);
}
auto is_filter() const -> bool
{
return std::holds_alternative<normal_item>(specific) &&
(type == symcache_item_type::FILTER);
}
/**
* Returns true if a symbol should have some score defined
* @return
*/
auto is_scoreable() const -> bool
{
return !(flags & SYMBOL_TYPE_CALLBACK) &&
((type == symcache_item_type::FILTER) ||
is_virtual() ||
(type == symcache_item_type::COMPOSITE) ||
(type == symcache_item_type::CLASSIFIER));
}
auto is_ghost() const -> bool
{
return flags & SYMBOL_TYPE_GHOST;
}
auto get_parent(const symcache &cache) const -> const cache_item *;
auto get_parent_mut(const symcache &cache) -> cache_item *;
auto resolve_parent(const symcache &cache) -> bool;
auto get_type() const -> auto
{
return type;
}
auto get_type_str() const -> const char *;
auto get_name() const -> const std::string &
{
return symbol;
}
auto get_flags() const -> auto
{
return flags;
};
auto add_condition(lua_State *L, int cbref) -> bool
{
if (!is_virtual()) {
auto &normal = std::get<normal_item>(specific);
normal.add_condition(L, cbref);
return true;
}
return false;
}
auto update_counters_check_peak(lua_State *L,
struct ev_loop *ev_loop,
double cur_time,
double last_resort) -> bool;
/**
* Increase frequency for a symbol
*/
auto inc_frequency(const char *sym_name, symcache &cache) -> void;
/**
* Check if an item is allowed to be executed not checking item conditions
* @param task
* @param exec_only
* @return
*/
auto is_allowed(struct rspamd_task *task, bool exec_only) const -> bool;
/**
* Returns callback data
* @return
*/
auto get_cbdata() const -> void *
{
if (std::holds_alternative<normal_item>(specific)) {
const auto &filter_data = std::get<normal_item>(specific);
return filter_data.get_cbdata();
}
return nullptr;
}
/**
* Check all conditions for an item
* @param task
* @return
*/
auto check_conditions(struct rspamd_task *task) const -> auto
{
if (std::holds_alternative<normal_item>(specific)) {
const auto &filter_data = std::get<normal_item>(specific);
return filter_data.check_conditions(symbol, task);
}
return false;
}
auto call(struct rspamd_task *task, cache_dynamic_item *dyn_item) const -> void
{
if (std::holds_alternative<normal_item>(specific)) {
const auto &filter_data = std::get<normal_item>(specific);
filter_data.call(task, (struct rspamd_symcache_dynamic_item *) dyn_item);
}
}
/**
* Add an augmentation to the item, returns `true` if augmentation is known and unique, false otherwise
* @param augmentation
* @return
*/
auto add_augmentation(const symcache &cache, std::string_view augmentation,
std::optional<std::string_view> value) -> bool;
/**
* Return sum weight of all known augmentations
* @return
*/
auto get_augmentation_weight() const -> int;
/**
* Returns numeric augmentation value
* @param name
* @return
*/
auto get_numeric_augmentation(std::string_view name) const -> std::optional<double>;
/**
* Returns string augmentation value
* @param name
* @return
*/
auto get_string_augmentation(std::string_view name) const -> std::optional<std::string_view>;
/**
* Add a virtual symbol as a child of some normal symbol
* @param ptr
*/
auto add_child(cache_item *ptr) -> void
{
if (std::holds_alternative<normal_item>(specific)) {
auto &filter_data = std::get<normal_item>(specific);
filter_data.add_child(ptr);
}
else {
g_assert("add child is called for a virtual symbol!");
}
}
/**
* Returns virtual children for a normal item
* @param ptr
* @return
*/
auto get_children() const -> const std::vector<cache_item *> *
{
if (std::holds_alternative<normal_item>(specific)) {
const auto &filter_data = std::get<normal_item>(specific);
return &filter_data.get_childen();
}
return nullptr;
}
private:
/**
* Constructor for a normal symbols with callback
* @param name
* @param _priority
* @param func
* @param user_data
* @param _type
* @param _flags
*/
cache_item(rspamd_mempool_t *pool,
int _id,
std::string &&name,
int _priority,
symbol_func_t func,
void *user_data,
symcache_item_type _type,
int _flags)
: id(_id),
symbol(std::move(name)),
type(_type),
flags(_flags),
priority(_priority),
specific(normal_item{func, user_data})
{
/* These structures are kept trivial, so they need to be explicitly reset */
forbidden_ids.reset();
allowed_ids.reset();
exec_only_ids.reset();
st = rspamd_mempool_alloc0_shared_type(pool, std::remove_pointer_t<decltype(st)>);
cd = rspamd_mempool_alloc0_shared_type(pool, std::remove_pointer_t<decltype(cd)>);
}
/**
* Constructor for a virtual symbol
* @param name
* @param _priority
* @param parent
* @param _type
* @param _flags
*/
cache_item(rspamd_mempool_t *pool,
int _id,
std::string &&name,
int parent,
symcache_item_type _type,
int _flags)
: id(_id),
symbol(std::move(name)),
type(_type),
flags(_flags),
specific(virtual_item{parent})
{
/* These structures are kept trivial, so they need to be explicitly reset */
forbidden_ids.reset();
allowed_ids.reset();
exec_only_ids.reset();
st = rspamd_mempool_alloc0_shared_type(pool, std::remove_pointer_t<decltype(st)>);
cd = rspamd_mempool_alloc0_shared_type(pool, std::remove_pointer_t<decltype(cd)>);
}
};
}// namespace rspamd::symcache
#endif//RSPAMD_SYMCACHE_ITEM_HXX
|