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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
|
/*
* 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.
*/
/**
* Internal C++ structures and classes for symcache
*/
#ifndef RSPAMD_SYMCACHE_INTERNAL_HXX
#define RSPAMD_SYMCACHE_INTERNAL_HXX
#pragma once
#include <cmath>
#include <cstdlib>
#include <cstdint>
#include <utility>
#include <vector>
#include <string>
#include <string_view>
#include <memory>
#include <variant>
#include "rspamd_symcache.h"
#include "contrib/libev/ev.h"
#include "contrib/ankerl/unordered_dense.h"
#include "contrib/expected/expected.hpp"
#include "cfg_file.h"
#include "symcache_id_list.hxx"
#define msg_err_cache(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \
"symcache", log_tag(), \
RSPAMD_LOG_FUNC, \
__VA_ARGS__)
#define msg_err_cache_lambda(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \
"symcache", log_tag(), \
log_func, \
__VA_ARGS__)
#define msg_err_cache_task(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \
"symcache", task->task_pool->tag.uid, \
RSPAMD_LOG_FUNC, \
__VA_ARGS__)
#define msg_warn_cache(...) rspamd_default_log_function(G_LOG_LEVEL_WARNING, \
"symcache", log_tag(), \
RSPAMD_LOG_FUNC, \
__VA_ARGS__)
#define msg_info_cache(...) rspamd_default_log_function(G_LOG_LEVEL_INFO, \
"symcache", log_tag(), \
RSPAMD_LOG_FUNC, \
__VA_ARGS__)
#define msg_debug_cache(...) rspamd_conditional_debug_fast(NULL, NULL, \
::rspamd::symcache::rspamd_symcache_log_id, "symcache", log_tag(), \
RSPAMD_LOG_FUNC, \
__VA_ARGS__)
#define msg_debug_cache_lambda(...) rspamd_conditional_debug_fast(NULL, NULL, \
::rspamd::symcache::rspamd_symcache_log_id, "symcache", log_tag(), \
log_func, \
__VA_ARGS__)
#define msg_debug_cache_task(...) rspamd_conditional_debug_fast(NULL, NULL, \
::rspamd::symcache::rspamd_symcache_log_id, "symcache", task->task_pool->tag.uid, \
RSPAMD_LOG_FUNC, \
__VA_ARGS__)
#define msg_debug_cache_task_lambda(...) rspamd_conditional_debug_fast(NULL, NULL, \
::rspamd::symcache::rspamd_symcache_log_id, "symcache", task->task_pool->tag.uid, \
log_func, \
__VA_ARGS__)
struct lua_State;
namespace rspamd::symcache {
/* Defined in symcache_impl.cxx */
extern int rspamd_symcache_log_id;
static const std::uint8_t symcache_magic[8] = {'r', 's', 'c', 2, 0, 0, 0, 0};
struct symcache_header {
std::uint8_t magic[8];
unsigned int nitems;
std::uint8_t checksum[64];
std::uint8_t unused[128];
};
struct cache_item;
using cache_item_ptr = std::shared_ptr<cache_item>;
/**
* This structure is intended to keep the current ordering for all symbols
* It is designed to be shared among all tasks and keep references to the real
* symbols.
* If some symbol has been added or removed to the symbol cache, it will not affect
* the current order, and it will only be regenerated for the subsequent tasks.
* This allows safe and no copy sharing and keeping track of all symbols in the
* cache runtime.
*/
struct order_generation {
/* All items ordered */
std::vector<cache_item_ptr> d;
/* Mapping from symbol name to the position in the order array */
ankerl::unordered_dense::map<std::string_view, unsigned int> by_symbol;
/* Mapping from symbol id to the position in the order array */
ankerl::unordered_dense::map<unsigned int, unsigned int> by_cache_id;
/* It matches cache->generation_id; if not, a fresh ordering is required */
unsigned int generation_id;
explicit order_generation(std::size_t nelts, unsigned id)
: generation_id(id)
{
d.reserve(nelts);
by_symbol.reserve(nelts);
by_cache_id.reserve(nelts);
}
auto size() const -> auto
{
return d.size();
}
};
using order_generation_ptr = std::shared_ptr<order_generation>;
struct delayed_cache_dependency {
std::string from;
std::string to;
delayed_cache_dependency(std::string_view _from, std::string_view _to)
: from(_from), to(_to)
{
}
};
struct delayed_cache_condition {
std::string sym;
int cbref;
lua_State *L;
public:
delayed_cache_condition(std::string_view sym, int cbref, lua_State *L)
: sym(sym), cbref(cbref), L(L)
{
}
};
class delayed_symbol_elt {
private:
std::variant<std::string, rspamd_regexp_t *> content;
public:
/* Disable copy */
delayed_symbol_elt() = delete;
delayed_symbol_elt(const delayed_symbol_elt &) = delete;
delayed_symbol_elt &operator=(const delayed_symbol_elt &) = delete;
/* Enable move */
delayed_symbol_elt(delayed_symbol_elt &&other) noexcept = default;
delayed_symbol_elt &operator=(delayed_symbol_elt &&other) noexcept = default;
explicit delayed_symbol_elt(std::string_view elt) noexcept
{
if (!elt.empty() && elt[0] == '/') {
/* Possibly regexp */
auto *re = rspamd_regexp_new_len(elt.data(), elt.size(), nullptr, nullptr);
if (re != nullptr) {
std::get<rspamd_regexp_t *>(content) = re;
}
else {
std::get<std::string>(content) = elt;
}
}
else {
std::get<std::string>(content) = elt;
}
}
~delayed_symbol_elt()
{
if (std::holds_alternative<rspamd_regexp_t *>(content)) {
rspamd_regexp_unref(std::get<rspamd_regexp_t *>(content));
}
}
auto matches(std::string_view what) const -> bool
{
return std::visit([&](auto &elt) {
using T = typeof(elt);
if constexpr (std::is_same_v<T, rspamd_regexp_t *>) {
if (rspamd_regexp_match(elt, what.data(), what.size(), false)) {
return true;
}
}
else if constexpr (std::is_same_v<T, std::string>) {
return elt == what;
}
return false;
},
content);
}
auto to_string_view() const -> std::string_view
{
return std::visit([&](auto &elt) {
using T = typeof(elt);
if constexpr (std::is_same_v<T, rspamd_regexp_t *>) {
return std::string_view{rspamd_regexp_get_pattern(elt)};
}
else if constexpr (std::is_same_v<T, std::string>) {
return std::string_view{elt};
}
return std::string_view{};
},
content);
}
};
struct delayed_symbol_elt_equal {
using is_transparent = void;
auto operator()(const delayed_symbol_elt &a, const delayed_symbol_elt &b) const
{
return a.to_string_view() == b.to_string_view();
}
auto operator()(const delayed_symbol_elt &a, const std::string_view &b) const
{
return a.to_string_view() == b;
}
auto operator()(const std::string_view &a, const delayed_symbol_elt &b) const
{
return a == b.to_string_view();
}
};
struct delayed_symbol_elt_hash {
using is_transparent = void;
auto operator()(const delayed_symbol_elt &a) const
{
return ankerl::unordered_dense::hash<std::string_view>()(a.to_string_view());
}
auto operator()(const std::string_view &a) const
{
return ankerl::unordered_dense::hash<std::string_view>()(a);
}
};
class symcache {
private:
using items_ptr_vec = std::vector<cache_item *>;
/* Map indexed by symbol name: all symbols must have unique names, so this map holds ownership */
ankerl::unordered_dense::map<std::string_view, cache_item *> items_by_symbol;
ankerl::unordered_dense::map<int, cache_item_ptr> items_by_id;
/* Items sorted into some order */
order_generation_ptr items_by_order;
unsigned int cur_order_gen;
/* Specific vectors for execution/iteration */
items_ptr_vec connfilters;
items_ptr_vec prefilters;
items_ptr_vec filters;
items_ptr_vec postfilters;
items_ptr_vec composites;
items_ptr_vec idempotent;
items_ptr_vec classifiers;
items_ptr_vec virtual_symbols;
/* These are stored within pointer to clean up after init */
std::unique_ptr<std::vector<delayed_cache_dependency>> delayed_deps;
std::unique_ptr<std::vector<delayed_cache_condition>> delayed_conditions;
/* Delayed statically enabled or disabled symbols */
using delayed_symbol_names = ankerl::unordered_dense::set<delayed_symbol_elt,
delayed_symbol_elt_hash, delayed_symbol_elt_equal>;
std::unique_ptr<delayed_symbol_names> disabled_symbols;
std::unique_ptr<delayed_symbol_names> enabled_symbols;
rspamd_mempool_t *static_pool;
std::uint64_t cksum;
double total_weight;
std::size_t stats_symbols_count;
private:
std::uint64_t total_hits;
struct rspamd_config *cfg;
lua_State *L;
double reload_time;
double last_profile;
private:
int peak_cb;
int cache_id;
private:
/* Internal methods */
auto load_items() -> bool;
auto resort() -> void;
auto get_item_specific_vector(const cache_item &) -> items_ptr_vec &;
/* Helper for g_hash_table_foreach */
static auto metric_connect_cb(void *k, void *v, void *ud) -> void;
public:
explicit symcache(struct rspamd_config *cfg)
: cfg(cfg)
{
/* XXX: do we need a special pool for symcache? I don't think so */
static_pool = cfg->cfg_pool;
reload_time = cfg->cache_reload_time;
total_hits = 1;
total_weight = 1.0;
cksum = 0xdeadbabe;
peak_cb = -1;
cache_id = rspamd_random_uint64_fast();
L = (lua_State *) cfg->lua_state;
delayed_conditions = std::make_unique<std::vector<delayed_cache_condition>>();
delayed_deps = std::make_unique<std::vector<delayed_cache_dependency>>();
}
virtual ~symcache();
/**
* Saves items on disk (if possible)
* @return
*/
auto save_items() const -> bool;
/**
* Get an item by ID
* @param id
* @param resolve_parent
* @return
*/
auto get_item_by_id(int id, bool resolve_parent) const -> const cache_item *;
auto get_item_by_id_mut(int id, bool resolve_parent) const -> cache_item *;
/**
* Get an item by it's name
* @param name
* @param resolve_parent
* @return
*/
auto get_item_by_name(std::string_view name, bool resolve_parent) const -> const cache_item *;
/**
* Get an item by it's name, mutable pointer
* @param name
* @param resolve_parent
* @return
*/
auto get_item_by_name_mut(std::string_view name, bool resolve_parent) const -> cache_item *;
/**
* Add a direct dependency
* @param id_from
* @param to
* @param virtual_id_from
* @return
*/
auto add_dependency(int id_from, std::string_view to, int virtual_id_from) -> void;
/**
* Add a delayed dependency between symbols that will be resolved on the init stage
* @param from
* @param to
*/
auto add_delayed_dependency(std::string_view from, std::string_view to) -> void
{
if (!delayed_deps) {
delayed_deps = std::make_unique<std::vector<delayed_cache_dependency>>();
}
delayed_deps->emplace_back(from, to);
}
/**
* Adds a symbol to the list of the disabled symbols
* @param sym
* @return
*/
auto disable_symbol_delayed(std::string_view sym) -> bool
{
if (!disabled_symbols) {
disabled_symbols = std::make_unique<delayed_symbol_names>();
}
if (!disabled_symbols->contains(sym)) {
disabled_symbols->emplace(sym);
return true;
}
return false;
}
/**
* Adds a symbol to the list of the enabled symbols
* @param sym
* @return
*/
auto enable_symbol_delayed(std::string_view sym) -> bool
{
if (!enabled_symbols) {
enabled_symbols = std::make_unique<delayed_symbol_names>();
}
if (!enabled_symbols->contains(sym)) {
enabled_symbols->emplace(sym);
return true;
}
return false;
}
/**
* Initialises the symbols cache, must be called after all symbols are added
* and the config file is loaded
*/
auto init() -> bool;
/**
* Log helper that returns cfg checksum
* @return
*/
auto log_tag() const -> const char *
{
return cfg->checksum;
}
/**
* Helper to return a memory pool associated with the cache
* @return
*/
auto get_pool() const
{
return static_pool;
}
/**
* A method to add a generic symbol with a callback to couple with C API
* @param name name of the symbol, unlike C API it must be "" for callback only (compat) symbols, in this case an automatic name is generated
* @param priority
* @param func
* @param user_data
* @param flags_and_type mix of flags and type in a messy C enum
* @return id of a new symbol or -1 in case of failure
*/
auto add_symbol_with_callback(std::string_view name,
int priority,
symbol_func_t func,
void *user_data,
int flags_and_type) -> int;
/**
* A method to add a generic virtual symbol with no function associated
* @param name must have some value, or a fatal error will strike you
* @param parent_id if this param is -1 then this symbol is associated with nothing
* @param flags_and_type mix of flags and type in a messy C enum
* @return id of a new symbol or -1 in case of failure
*/
auto add_virtual_symbol(std::string_view name, int parent_id,
int flags_and_type) -> int;
/**
* Sets a lua callback to be called on peaks in execution time
* @param cbref
*/
auto set_peak_cb(int cbref) -> void;
/**
* Add a delayed condition for a symbol that might not be registered yet
* @param sym
* @param cbref
*/
auto add_delayed_condition(std::string_view sym, int cbref) -> void;
/**
* Returns number of symbols that needs to be checked in statistical algorithm
* @return
*/
auto get_stats_symbols_count() const
{
return stats_symbols_count;
}
/**
* Returns a checksum for the cache
* @return
*/
auto get_cksum() const
{
return cksum;
}
/**
* Validate symbols in the cache
* @param strict
* @return
*/
auto validate(bool strict) -> bool;
/**
* Returns counters for the cache
* @return
*/
auto counters() const -> ucl_object_t *;
/**
* Adjusts stats of the cache for the periodic counter
*/
auto periodic_resort(struct ev_loop *ev_loop, double cur_time, double last_resort) -> void;
/**
* A simple helper to get the reload time
* @return
*/
auto get_reload_time() const
{
return reload_time;
};
/**
* Iterate over all symbols using a specific functor
* @tparam Functor
* @param f
*/
template<typename Functor>
auto symbols_foreach(Functor f) -> void
{
for (const auto &sym_it: items_by_symbol) {
f(sym_it.second);
}
}
/**
* Iterate over all composites using a specific functor
* @tparam Functor
* @param f
*/
template<typename Functor>
auto composites_foreach(Functor f) -> void
{
for (const auto &sym_it: composites) {
f(sym_it);
}
}
/**
* Iterate over all composites using a specific functor
* @tparam Functor
* @param f
*/
template<typename Functor>
auto connfilters_foreach(Functor f) -> bool
{
return std::all_of(std::begin(connfilters), std::end(connfilters),
[&](const auto &sym_it) {
return f(sym_it);
});
}
template<typename Functor>
auto prefilters_foreach(Functor f) -> bool
{
return std::all_of(std::begin(prefilters), std::end(prefilters),
[&](const auto &sym_it) {
return f(sym_it);
});
}
template<typename Functor>
auto postfilters_foreach(Functor f) -> bool
{
return std::all_of(std::begin(postfilters), std::end(postfilters),
[&](const auto &sym_it) {
return f(sym_it);
});
}
template<typename Functor>
auto idempotent_foreach(Functor f) -> bool
{
return std::all_of(std::begin(idempotent), std::end(idempotent),
[&](const auto &sym_it) {
return f(sym_it);
});
}
template<typename Functor>
auto filters_foreach(Functor f) -> bool
{
return std::all_of(std::begin(filters), std::end(filters),
[&](const auto &sym_it) {
return f(sym_it);
});
}
/**
* Resort cache if anything has been changed since last time
* @return
*/
auto maybe_resort() -> bool;
/**
* Returns current set of items ordered for sharing ownership
* @return
*/
auto get_cache_order() const -> auto
{
return items_by_order;
}
/**
* Get last profile timestamp
* @return
*/
auto get_last_profile() const -> auto
{
return last_profile;
}
/**
* Sets last profile timestamp
* @param last_profile
* @return
*/
auto set_last_profile(double last_profile)
{
symcache::last_profile = last_profile;
}
/**
* Process settings elt identified by id
* @param elt
*/
auto process_settings_elt(struct rspamd_config_settings_elt *elt) -> void;
/**
* Returns maximum timeout that is requested by all rules
* @return
*/
auto get_max_timeout(std::vector<std::pair<double, const cache_item *>> &elts) const -> double;
};
}// namespace rspamd::symcache
#endif//RSPAMD_SYMCACHE_INTERNAL_HXX
|