forked from hillu/go-yara
-
Notifications
You must be signed in to change notification settings - Fork 7
/
re.c
2625 lines (2130 loc) · 70.2 KB
/
re.c
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
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2013. The YARA Authors. All Rights Reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. 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.
3. Neither the name of the copyright holder 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 HOLDER 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.
*/
/*
This module implements a regular expressions engine based on Thompson's
algorithm as described by Russ Cox in http://swtch.com/~rsc/regexp/regexp2.html.
What the article names a "thread" has been named a "fiber" in this code, in
order to avoid confusion with operating system threads.
*/
#include <assert.h>
#include <string.h>
#include <yara_compiler.h>
#include <yara_error.h>
#include <yara_globals.h>
#include <yara_hex_lexer.h>
#include <yara_limits.h>
#include <yara_mem.h>
#include <yara_re.h>
#include <yara_re_lexer.h>
#include <yara_strutils.h>
#include <yara_threading.h>
#include <yara_unaligned.h>
#include <yara_utils.h>
#define EMIT_BACKWARDS 0x01
#define EMIT_DONT_SET_FORWARDS_CODE 0x02
#define EMIT_DONT_SET_BACKWARDS_CODE 0x04
#ifndef INT16_MAX
#define INT16_MAX (32767)
#endif
typedef uint8_t RE_SPLIT_ID_TYPE;
// RE_REPEAT_ARGS and RE_REPEAT_ANY_ARGS are structures that are embedded in
// the regexp's instruction stream. As such, they are not always aligned to
// 8-byte boundaries, so they need to be "packed" structures in order to prevent
// issues due to unaligned memory accesses.
#pragma pack(push)
#pragma pack(1)
typedef struct _RE_REPEAT_ARGS
{
uint16_t min;
uint16_t max;
int32_t offset;
} RE_REPEAT_ARGS;
typedef struct _RE_REPEAT_ANY_ARGS
{
uint16_t min;
uint16_t max;
} RE_REPEAT_ANY_ARGS;
#pragma pack(pop)
typedef struct _RE_EMIT_CONTEXT
{
YR_ARENA* arena;
RE_SPLIT_ID_TYPE next_split_id;
} RE_EMIT_CONTEXT;
#define CHAR_IN_CLASS(cls, chr) ((cls)[(chr) / 8] & 1 << ((chr) % 8))
static bool _yr_re_is_char_in_class(
RE_CLASS* re_class,
uint8_t chr,
int case_insensitive)
{
int result = CHAR_IN_CLASS(re_class->bitmap, chr);
if (case_insensitive)
result |= CHAR_IN_CLASS(re_class->bitmap, yr_altercase[chr]);
if (re_class->negated)
result = !result;
return result;
}
static bool _yr_re_is_word_char(const uint8_t* input, uint8_t character_size)
{
int result = ((yr_isalnum(input) || (*input) == '_'));
if (character_size == 2)
result = result && (*(input + 1) == 0);
return result;
}
RE_NODE* yr_re_node_create(int type)
{
RE_NODE* result = (RE_NODE*) yr_malloc(sizeof(RE_NODE));
if (result != NULL)
{
result->type = type;
result->children_head = NULL;
result->children_tail = NULL;
result->prev_sibling = NULL;
result->next_sibling = NULL;
result->greedy = true;
result->forward_code_ref = YR_ARENA_NULL_REF;
result->backward_code_ref = YR_ARENA_NULL_REF;
}
return result;
}
void yr_re_node_destroy(RE_NODE* node)
{
RE_NODE* child = node->children_head;
RE_NODE* next_child;
while (child != NULL)
{
next_child = child->next_sibling;
yr_re_node_destroy(child);
child = next_child;
}
if (node->type == RE_NODE_CLASS)
yr_free(node->re_class);
yr_free(node);
}
////////////////////////////////////////////////////////////////////////////////
// Appends a node to the end of the children list.
//
void yr_re_node_append_child(RE_NODE* node, RE_NODE* child)
{
if (node->children_head == NULL)
node->children_head = child;
if (node->children_tail != NULL)
node->children_tail->next_sibling = child;
child->prev_sibling = node->children_tail;
node->children_tail = child;
}
////////////////////////////////////////////////////////////////////////////////
// Appends a node to the beginning of the children list.
//
void yr_re_node_prepend_child(RE_NODE* node, RE_NODE* child)
{
child->next_sibling = node->children_head;
if (node->children_head != NULL)
node->children_head->prev_sibling = child;
node->children_head = child;
if (node->children_tail == NULL)
node->children_tail = child;
}
int yr_re_ast_create(RE_AST** re_ast)
{
*re_ast = (RE_AST*) yr_malloc(sizeof(RE_AST));
if (*re_ast == NULL)
return ERROR_INSUFFICIENT_MEMORY;
(*re_ast)->flags = 0;
(*re_ast)->root_node = NULL;
return ERROR_SUCCESS;
}
void yr_re_ast_destroy(RE_AST* re_ast)
{
if (re_ast->root_node != NULL)
yr_re_node_destroy(re_ast->root_node);
yr_free(re_ast);
}
////////////////////////////////////////////////////////////////////////////////
// Parses a regexp but don't emit its code. A further call to
// yr_re_ast_emit_code is required to get the code.
//
int yr_re_parse(
const char* re_string,
RE_AST** re_ast,
RE_ERROR* error,
int flags)
{
return yr_parse_re_string(re_string, re_ast, error, flags);
}
////////////////////////////////////////////////////////////////////////////////
// Parses a hex string but don't emit its code. A further call to
// yr_re_ast_emit_code is required to get the code.
//
int yr_re_parse_hex(const char* hex_string, RE_AST** re_ast, RE_ERROR* error)
{
return yr_parse_hex_string(hex_string, re_ast, error);
}
////////////////////////////////////////////////////////////////////////////////
// Parses the regexp and emit its code to the provided to the
// YR_RE_CODE_SECTION in the specified arena.
//
int yr_re_compile(
const char* re_string,
int flags,
int parser_flags,
YR_ARENA* arena,
YR_ARENA_REF* ref,
RE_ERROR* error)
{
RE_AST* re_ast;
RE _re;
int result;
result = yr_re_parse(re_string, &re_ast, error, parser_flags);
if (result != ERROR_UNKNOWN_ESCAPE_SEQUENCE)
FAIL_ON_ERROR(result);
_re.flags = flags;
FAIL_ON_ERROR_WITH_CLEANUP(
yr_arena_write_data(arena, YR_RE_CODE_SECTION, &_re, sizeof(_re), ref),
yr_re_ast_destroy(re_ast));
FAIL_ON_ERROR_WITH_CLEANUP(
yr_re_ast_emit_code(re_ast, arena, false), yr_re_ast_destroy(re_ast));
yr_re_ast_destroy(re_ast);
return result;
}
////////////////////////////////////////////////////////////////////////////////
// Verifies if the target string matches the pattern
//
// Args:
// context: Scan context
// re: A pointer to a compiled regexp
// target: Target string
//
// Returns:
// See return codes for yr_re_exec
//
int yr_re_match(YR_SCAN_CONTEXT* context, RE* re, const char* target)
{
int result;
yr_re_exec(
context,
re->code,
(uint8_t*) target,
strlen(target),
0,
re->flags | RE_FLAGS_SCAN,
NULL,
NULL,
&result);
return result;
}
////////////////////////////////////////////////////////////////////////////////
// Verifies if the provided regular expression is just a literal string
// like "abc", "12345", without any wildcard, operator, etc. In that case
// returns the string as a SIZED_STRING, or returns NULL if otherwise.
//
// The caller is responsible for deallocating the returned SIZED_STRING by
// calling yr_free.
//
SIZED_STRING* yr_re_ast_extract_literal(RE_AST* re_ast)
{
SIZED_STRING* string;
RE_NODE* child;
int length = 0;
if (re_ast->root_node->type == RE_NODE_LITERAL)
{
length = 1;
}
else if (re_ast->root_node->type == RE_NODE_CONCAT)
{
child = re_ast->root_node->children_tail;
while (child != NULL && child->type == RE_NODE_LITERAL)
{
length++;
child = child->prev_sibling;
}
if (child != NULL)
return NULL;
}
else
{
return NULL;
}
string = (SIZED_STRING*) yr_malloc(sizeof(SIZED_STRING) + length);
if (string == NULL)
return NULL;
string->length = length;
string->flags = 0;
if (re_ast->root_node->type == RE_NODE_LITERAL)
{
string->c_string[0] = re_ast->root_node->value;
}
else
{
child = re_ast->root_node->children_tail;
while (child != NULL)
{
string->c_string[--length] = child->value;
child = child->prev_sibling;
}
}
string->c_string[string->length] = '\0';
return string;
}
int _yr_re_node_has_unbounded_quantifier_for_dot(RE_NODE* re_node)
{
RE_NODE* child;
if ((re_node->type == RE_NODE_STAR || re_node->type == RE_NODE_PLUS) &&
re_node->children_head->type == RE_NODE_ANY)
return true;
if (re_node->type == RE_NODE_RANGE_ANY && re_node->end == RE_MAX_RANGE)
return true;
if (re_node->type == RE_NODE_CONCAT)
{
child = re_node->children_tail;
while (child != NULL)
{
if (_yr_re_node_has_unbounded_quantifier_for_dot(child))
return true;
child = child->prev_sibling;
}
}
return false;
}
////////////////////////////////////////////////////////////////////////////////
// Detects the use of .*, .+ or .{x,} in a regexp. The use of wildcards with
// quantifiers that don't have a reasonably small upper bound causes a
// performance penalty. This function detects such cases in order to warn the
// user about this.
//
int yr_re_ast_has_unbounded_quantifier_for_dot(RE_AST* re_ast)
{
return _yr_re_node_has_unbounded_quantifier_for_dot(re_ast->root_node);
}
////////////////////////////////////////////////////////////////////////////////
// In some cases splitting a regular expression (or hex string) in two parts is
// convenient for increasing performance. This happens when the pattern contains
// a large gap (a.k.a jump), for example: { 01 02 03 [0-999] 04 05 06 }
// In this case the string is splitted in { 01 02 03 } and { 04 05 06 } where
// the latter is chained to the former. This means that { 01 02 03 } and
// { 04 05 06 } are handled as individual strings, and when both of them are
// found, YARA verifies if the distance between the matches complies with the
// [0-999] restriction.
//
// This function traverses a regexp's AST looking for nodes where it should be
// splitted. It must be noticed that this only applies to two-level ASTs (i.e.
// an AST consisting in a RE_NODE_CONCAT at the root where all the children are
// leaves).
//
// For example, { 01 02 03 [0-1000] 04 05 06 [500-2000] 07 08 09 } has the
// following AST:
//
// RE_NODE_CONCAT
// |
// |- RE_NODE_LITERAL (01)
// |- RE_NODE_LITERAL (02)
// |- RE_NODE_LITERAL (03)
// |- RE_NODE_RANGE_ANY (start=0, end=1000)
// |- RE_NODE_LITERAL (04)
// |- RE_NODE_LITERAL (05)
// |- RE_NODE_LITERAL (06)
// |- RE_NODE_RANGE_ANY (start=500, end=2000)
// |- RE_NODE_LITERAL (07)
// |- RE_NODE_LITERAL (08)
// |- RE_NODE_LITERAL (09)
//
// If the AST above is passed in the re_ast argument, it will be trimmed to:
//
// RE_NODE_CONCAT
// |
// |- RE_NODE_LITERAL (01)
// |- RE_NODE_LITERAL (02)
// |- RE_NODE_LITERAL (03)
//
// While remainder_re_ast will be:
//
// RE_NODE_CONCAT
// |
// |- RE_NODE_LITERAL (04)
// |- RE_NODE_LITERAL (05)
// |- RE_NODE_LITERAL (06)
// |- RE_NODE_RANGE_ANY (start=500, end=2000)
// |- RE_NODE_LITERAL (07)
// |- RE_NODE_LITERAL (08)
// |- RE_NODE_LITERAL (09)
//
// The caller is responsible for freeing the new AST in remainder_re_ast by
// calling yr_re_ast_destroy.
//
// The integers pointed to by min_gap and max_gap will be filled with the
// minimum and maximum gap size between the sub-strings represented by the
// two ASTs.
//
int yr_re_ast_split_at_chaining_point(
RE_AST* re_ast,
RE_AST** remainder_re_ast,
int32_t* min_gap,
int32_t* max_gap)
{
RE_NODE* child;
RE_NODE* concat;
int result;
*remainder_re_ast = NULL;
*min_gap = 0;
*max_gap = 0;
if (re_ast->root_node->type != RE_NODE_CONCAT)
return ERROR_SUCCESS;
child = re_ast->root_node->children_head;
while (child != NULL)
{
if (!child->greedy && child->type == RE_NODE_RANGE_ANY &&
child->prev_sibling != NULL && child->next_sibling != NULL &&
(child->start > YR_STRING_CHAINING_THRESHOLD ||
child->end > YR_STRING_CHAINING_THRESHOLD))
{
result = yr_re_ast_create(remainder_re_ast);
if (result != ERROR_SUCCESS)
return result;
concat = yr_re_node_create(RE_NODE_CONCAT);
if (concat == NULL)
return ERROR_INSUFFICIENT_MEMORY;
concat->children_head = child->next_sibling;
concat->children_tail = re_ast->root_node->children_tail;
re_ast->root_node->children_tail = child->prev_sibling;
child->prev_sibling->next_sibling = NULL;
child->next_sibling->prev_sibling = NULL;
*min_gap = child->start;
*max_gap = child->end;
(*remainder_re_ast)->root_node = concat;
(*remainder_re_ast)->flags = re_ast->flags;
yr_re_node_destroy(child);
return ERROR_SUCCESS;
}
child = child->next_sibling;
}
return ERROR_SUCCESS;
}
int _yr_emit_inst(
RE_EMIT_CONTEXT* emit_context,
uint8_t opcode,
YR_ARENA_REF* instruction_ref)
{
FAIL_ON_ERROR(yr_arena_write_data(
emit_context->arena,
YR_RE_CODE_SECTION,
&opcode,
sizeof(uint8_t),
instruction_ref));
return ERROR_SUCCESS;
}
int _yr_emit_inst_arg_uint8(
RE_EMIT_CONTEXT* emit_context,
uint8_t opcode,
uint8_t argument,
YR_ARENA_REF* instruction_ref,
YR_ARENA_REF* argument_ref)
{
FAIL_ON_ERROR(yr_arena_write_data(
emit_context->arena,
YR_RE_CODE_SECTION,
&opcode,
sizeof(uint8_t),
instruction_ref));
FAIL_ON_ERROR(yr_arena_write_data(
emit_context->arena,
YR_RE_CODE_SECTION,
&argument,
sizeof(uint8_t),
argument_ref));
return ERROR_SUCCESS;
}
int _yr_emit_inst_arg_uint16(
RE_EMIT_CONTEXT* emit_context,
uint8_t opcode,
uint16_t argument,
YR_ARENA_REF* instruction_ref,
YR_ARENA_REF* argument_ref)
{
FAIL_ON_ERROR(yr_arena_write_data(
emit_context->arena,
YR_RE_CODE_SECTION,
&opcode,
sizeof(uint8_t),
instruction_ref));
FAIL_ON_ERROR(yr_arena_write_data(
emit_context->arena,
YR_RE_CODE_SECTION,
&argument,
sizeof(uint16_t),
argument_ref));
return ERROR_SUCCESS;
}
int _yr_emit_inst_arg_uint32(
RE_EMIT_CONTEXT* emit_context,
uint8_t opcode,
uint32_t argument,
YR_ARENA_REF* instruction_ref,
YR_ARENA_REF* argument_ref)
{
FAIL_ON_ERROR(yr_arena_write_data(
emit_context->arena,
YR_RE_CODE_SECTION,
&opcode,
sizeof(uint8_t),
instruction_ref));
FAIL_ON_ERROR(yr_arena_write_data(
emit_context->arena,
YR_RE_CODE_SECTION,
&argument,
sizeof(uint32_t),
argument_ref));
return ERROR_SUCCESS;
}
int _yr_emit_inst_arg_int16(
RE_EMIT_CONTEXT* emit_context,
uint8_t opcode,
int16_t argument,
YR_ARENA_REF* instruction_ref,
YR_ARENA_REF* argument_ref)
{
FAIL_ON_ERROR(yr_arena_write_data(
emit_context->arena,
YR_RE_CODE_SECTION,
&opcode,
sizeof(uint8_t),
instruction_ref));
FAIL_ON_ERROR(yr_arena_write_data(
emit_context->arena,
YR_RE_CODE_SECTION,
&argument,
sizeof(int16_t),
argument_ref));
return ERROR_SUCCESS;
}
int _yr_emit_inst_arg_struct(
RE_EMIT_CONTEXT* emit_context,
uint8_t opcode,
void* structure,
size_t structure_size,
YR_ARENA_REF* instruction_ref,
YR_ARENA_REF* argument_ref)
{
FAIL_ON_ERROR(yr_arena_write_data(
emit_context->arena,
YR_RE_CODE_SECTION,
&opcode,
sizeof(uint8_t),
instruction_ref));
FAIL_ON_ERROR(yr_arena_write_data(
emit_context->arena,
YR_RE_CODE_SECTION,
structure,
structure_size,
argument_ref));
return ERROR_SUCCESS;
}
int _yr_emit_split(
RE_EMIT_CONTEXT* emit_context,
uint8_t opcode,
int16_t argument,
YR_ARENA_REF* instruction_ref,
YR_ARENA_REF* argument_ref)
{
assert(opcode == RE_OPCODE_SPLIT_A || opcode == RE_OPCODE_SPLIT_B);
if (emit_context->next_split_id == RE_MAX_SPLIT_ID)
return ERROR_REGULAR_EXPRESSION_TOO_COMPLEX;
FAIL_ON_ERROR(yr_arena_write_data(
emit_context->arena,
YR_RE_CODE_SECTION,
&opcode,
sizeof(uint8_t),
instruction_ref));
FAIL_ON_ERROR(yr_arena_write_data(
emit_context->arena,
YR_RE_CODE_SECTION,
&emit_context->next_split_id,
sizeof(RE_SPLIT_ID_TYPE),
NULL));
emit_context->next_split_id++;
FAIL_ON_ERROR(yr_arena_write_data(
emit_context->arena,
YR_RE_CODE_SECTION,
&argument,
sizeof(int16_t),
argument_ref));
return ERROR_SUCCESS;
}
static int _yr_re_emit(
RE_EMIT_CONTEXT* emit_context,
RE_NODE* re_node,
int flags,
YR_ARENA_REF* code_ref)
{
int16_t jmp_offset;
yr_arena_off_t bookmark_1 = 0;
yr_arena_off_t bookmark_2 = 0;
yr_arena_off_t bookmark_3 = 0;
yr_arena_off_t bookmark_4 = 0;
bool emit_split;
bool emit_repeat;
bool emit_prolog;
bool emit_epilog;
RE_REPEAT_ARGS repeat_args;
RE_REPEAT_ARGS* repeat_start_args_addr;
RE_REPEAT_ANY_ARGS repeat_any_args;
RE_NODE* child;
int16_t* split_offset_addr = NULL;
int16_t* jmp_offset_addr = NULL;
YR_ARENA_REF instruction_ref = YR_ARENA_NULL_REF;
YR_ARENA_REF split_offset_ref;
YR_ARENA_REF jmp_instruction_ref;
YR_ARENA_REF jmp_offset_ref;
YR_ARENA_REF repeat_start_args_ref;
switch (re_node->type)
{
case RE_NODE_LITERAL:
FAIL_ON_ERROR(_yr_emit_inst_arg_uint8(
emit_context,
RE_OPCODE_LITERAL,
re_node->value,
&instruction_ref,
NULL));
break;
case RE_NODE_NOT_LITERAL:
FAIL_ON_ERROR(_yr_emit_inst_arg_uint8(
emit_context,
RE_OPCODE_NOT_LITERAL,
re_node->value,
&instruction_ref,
NULL));
break;
case RE_NODE_MASKED_LITERAL:
FAIL_ON_ERROR(_yr_emit_inst_arg_uint16(
emit_context,
RE_OPCODE_MASKED_LITERAL,
re_node->mask << 8 | re_node->value,
&instruction_ref,
NULL));
break;
case RE_NODE_MASKED_NOT_LITERAL:
FAIL_ON_ERROR(_yr_emit_inst_arg_uint16(
emit_context,
RE_OPCODE_MASKED_NOT_LITERAL,
re_node->mask << 8 | re_node->value,
&instruction_ref,
NULL));
break;
case RE_NODE_WORD_CHAR:
FAIL_ON_ERROR(
_yr_emit_inst(emit_context, RE_OPCODE_WORD_CHAR, &instruction_ref));
break;
case RE_NODE_NON_WORD_CHAR:
FAIL_ON_ERROR(
_yr_emit_inst(emit_context, RE_OPCODE_NON_WORD_CHAR, &instruction_ref));
break;
case RE_NODE_WORD_BOUNDARY:
FAIL_ON_ERROR(
_yr_emit_inst(emit_context, RE_OPCODE_WORD_BOUNDARY, &instruction_ref));
break;
case RE_NODE_NON_WORD_BOUNDARY:
FAIL_ON_ERROR(_yr_emit_inst(
emit_context, RE_OPCODE_NON_WORD_BOUNDARY, &instruction_ref));
break;
case RE_NODE_SPACE:
FAIL_ON_ERROR(
_yr_emit_inst(emit_context, RE_OPCODE_SPACE, &instruction_ref));
break;
case RE_NODE_NON_SPACE:
FAIL_ON_ERROR(
_yr_emit_inst(emit_context, RE_OPCODE_NON_SPACE, &instruction_ref));
break;
case RE_NODE_DIGIT:
FAIL_ON_ERROR(
_yr_emit_inst(emit_context, RE_OPCODE_DIGIT, &instruction_ref));
break;
case RE_NODE_NON_DIGIT:
FAIL_ON_ERROR(
_yr_emit_inst(emit_context, RE_OPCODE_NON_DIGIT, &instruction_ref));
break;
case RE_NODE_ANY:
FAIL_ON_ERROR(_yr_emit_inst(emit_context, RE_OPCODE_ANY, &instruction_ref));
break;
case RE_NODE_CLASS:
FAIL_ON_ERROR(
_yr_emit_inst(emit_context, RE_OPCODE_CLASS, &instruction_ref));
FAIL_ON_ERROR(yr_arena_write_data(
emit_context->arena,
YR_RE_CODE_SECTION,
re_node->re_class,
sizeof(*re_node->re_class),
NULL));
break;
case RE_NODE_ANCHOR_START:
FAIL_ON_ERROR(_yr_emit_inst(
emit_context, RE_OPCODE_MATCH_AT_START, &instruction_ref));
break;
case RE_NODE_ANCHOR_END:
FAIL_ON_ERROR(
_yr_emit_inst(emit_context, RE_OPCODE_MATCH_AT_END, &instruction_ref));
break;
case RE_NODE_CONCAT:
FAIL_ON_ERROR(_yr_re_emit(
emit_context,
(flags & EMIT_BACKWARDS) ? re_node->children_tail
: re_node->children_head,
flags,
&instruction_ref));
if (flags & EMIT_BACKWARDS)
child = re_node->children_tail->prev_sibling;
else
child = re_node->children_head->next_sibling;
while (child != NULL)
{
FAIL_ON_ERROR(_yr_re_emit(emit_context, child, flags, NULL));
child = (flags & EMIT_BACKWARDS) ? child->prev_sibling
: child->next_sibling;
}
break;
case RE_NODE_PLUS:
// Code for e+ looks like:
//
// L1: code for e
// split L1, L2
// L2:
//
FAIL_ON_ERROR(_yr_re_emit(
emit_context, re_node->children_head, flags, &instruction_ref));
bookmark_1 = yr_arena_get_current_offset(
emit_context->arena, YR_RE_CODE_SECTION);
if (instruction_ref.offset - bookmark_1 < INT16_MIN)
return ERROR_REGULAR_EXPRESSION_TOO_LARGE;
jmp_offset = (int16_t) (instruction_ref.offset - bookmark_1);
FAIL_ON_ERROR(_yr_emit_split(
emit_context,
re_node->greedy ? RE_OPCODE_SPLIT_B : RE_OPCODE_SPLIT_A,
jmp_offset,
NULL,
NULL));
break;
case RE_NODE_STAR:
// Code for e* looks like:
//
// L1: split L1, L2
// code for e
// jmp L1
// L2:
FAIL_ON_ERROR(_yr_emit_split(
emit_context,
re_node->greedy ? RE_OPCODE_SPLIT_A : RE_OPCODE_SPLIT_B,
0,
&instruction_ref,
&split_offset_ref));
FAIL_ON_ERROR(
_yr_re_emit(emit_context, re_node->children_head, flags, NULL));
bookmark_1 = yr_arena_get_current_offset(
emit_context->arena, YR_RE_CODE_SECTION);
if (instruction_ref.offset - bookmark_1 < INT16_MIN)
return ERROR_REGULAR_EXPRESSION_TOO_LARGE;
jmp_offset = (int16_t) (instruction_ref.offset - bookmark_1);
// Emit jump with offset set to 0.
FAIL_ON_ERROR(_yr_emit_inst_arg_int16(
emit_context, RE_OPCODE_JUMP, jmp_offset, NULL, NULL));
bookmark_1 = yr_arena_get_current_offset(
emit_context->arena, YR_RE_CODE_SECTION);
if (bookmark_1 - instruction_ref.offset > INT16_MAX)
return ERROR_REGULAR_EXPRESSION_TOO_LARGE;
jmp_offset = (int16_t) (bookmark_1 - instruction_ref.offset);
// Update split offset.
split_offset_addr = (int16_t*) yr_arena_ref_to_ptr(
emit_context->arena, &split_offset_ref);
memcpy(split_offset_addr, &jmp_offset, sizeof(jmp_offset));
break;
case RE_NODE_ALT:
// Code for e1|e2 looks like:
//
// split L1, L2
// L1: code for e1
// jmp L3
// L2: code for e2
// L3:
// Emit a split instruction with offset set to 0 temporarily. Offset
// will be updated after we know the size of the code generated for
// the left node (e1).
FAIL_ON_ERROR(_yr_emit_split(
emit_context,
RE_OPCODE_SPLIT_A,
0,
&instruction_ref,
&split_offset_ref));
FAIL_ON_ERROR(
_yr_re_emit(emit_context, re_node->children_head, flags, NULL));
// Emit jump with offset set to 0.
FAIL_ON_ERROR(_yr_emit_inst_arg_int16(
emit_context,
RE_OPCODE_JUMP,
0,
&jmp_instruction_ref,
&jmp_offset_ref));
bookmark_1 = yr_arena_get_current_offset(
emit_context->arena, YR_RE_CODE_SECTION);
if (bookmark_1 - instruction_ref.offset > INT16_MAX)
return ERROR_REGULAR_EXPRESSION_TOO_LARGE;
jmp_offset = (int16_t) (bookmark_1 - instruction_ref.offset);
// Update split offset.
split_offset_addr = (int16_t*) yr_arena_ref_to_ptr(
emit_context->arena, &split_offset_ref);
memcpy(split_offset_addr, &jmp_offset, sizeof(jmp_offset));
FAIL_ON_ERROR(
_yr_re_emit(emit_context, re_node->children_tail, flags, NULL));
bookmark_1 = yr_arena_get_current_offset(
emit_context->arena, YR_RE_CODE_SECTION);
if (bookmark_1 - jmp_instruction_ref.offset > INT16_MAX)
return ERROR_REGULAR_EXPRESSION_TOO_LARGE;
jmp_offset = (int16_t) (bookmark_1 - jmp_instruction_ref.offset);
// Update offset for jmp instruction.
jmp_offset_addr = (int16_t*) yr_arena_ref_to_ptr(
emit_context->arena, &jmp_offset_ref);
memcpy(jmp_offset_addr, &jmp_offset, sizeof(jmp_offset));
break;
case RE_NODE_RANGE_ANY:
repeat_any_args.min = re_node->start;
repeat_any_args.max = re_node->end;
FAIL_ON_ERROR(_yr_emit_inst_arg_struct(
emit_context,
re_node->greedy ? RE_OPCODE_REPEAT_ANY_GREEDY
: RE_OPCODE_REPEAT_ANY_UNGREEDY,
&repeat_any_args,
sizeof(repeat_any_args),