forked from hillu/go-yara
-
Notifications
You must be signed in to change notification settings - Fork 7
/
exec.c
2401 lines (2010 loc) · 63.1 KB
/
exec.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-2014. 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.
*/
#include <assert.h>
#include <float.h>
#include <math.h>
#include <string.h>
#include <yara.h>
#include <yara_arena.h>
#include <yara_endian.h>
#include <yara_error.h>
#include <yara_exec.h>
#include <yara_globals.h>
#include <yara_limits.h>
#include <yara_mem.h>
#include <yara_modules.h>
#include <yara_object.h>
#include <yara_re.h>
#include <yara_sizedstr.h>
#include <yara_stopwatch.h>
#include <yara_strutils.h>
#include <yara_unaligned.h>
#include <yara_utils.h>
#define MEM_SIZE YR_MAX_LOOP_NESTING*(YR_MAX_LOOP_VARS + YR_INTERNAL_LOOP_VARS)
#define push(x) \
if (stack.sp < stack.capacity) \
{ \
stack.items[stack.sp++] = (x); \
} \
else \
{ \
result = ERROR_EXEC_STACK_OVERFLOW; \
stop = true; \
break; \
}
#define pop(x) \
{ \
assert(stack.sp > 0); \
x = stack.items[--stack.sp]; \
}
#define is_undef(x) IS_UNDEFINED((x).i)
#define ensure_defined(x) \
if (is_undef(x)) \
{ \
r1.i = YR_UNDEFINED; \
push(r1); \
break; \
}
#define ensure_within_mem(x) \
if (x < 0 || x >= MEM_SIZE) \
{ \
stop = true; \
result = ERROR_INTERNAL_FATAL_ERROR; \
break; \
}
// Make sure that the string pointer is within the rules arena.
#define ensure_within_rules_arena(x) \
{ \
YR_ARENA_REF ref; \
if (yr_arena_ptr_to_ref(context->rules->arena, x, &ref) == 0) \
{ \
stop = true; \
result = ERROR_INTERNAL_FATAL_ERROR; \
break; \
} \
}
#define check_object_canary(o) \
if (o->canary != context->canary) \
{ \
stop = true; \
result = ERROR_INTERNAL_FATAL_ERROR; \
break; \
}
#define little_endian_uint8_t(x) (x)
#define little_endian_int8_t(x) (x)
#define little_endian_uint16_t(x) yr_le16toh(x)
#define little_endian_int16_t(x) yr_le16toh(x)
#define little_endian_uint32_t(x) yr_le32toh(x)
#define little_endian_int32_t(x) yr_le32toh(x)
#define big_endian_uint8_t(x) (x)
#define big_endian_int8_t(x) (x)
#define big_endian_uint16_t(x) yr_be16toh(x)
#define big_endian_int16_t(x) yr_be16toh(x)
#define big_endian_uint32_t(x) yr_be32toh(x)
#define big_endian_int32_t(x) yr_be32toh(x)
#define function_read(type, endianess) \
int64_t read_##type##_##endianess( \
YR_MEMORY_BLOCK_ITERATOR* iterator, size_t offset) \
{ \
YR_MEMORY_BLOCK* block = iterator->first(iterator); \
while (block != NULL) \
{ \
if (offset >= block->base && block->size >= sizeof(type) && \
offset <= block->base + block->size - sizeof(type)) \
{ \
type result; \
const uint8_t* data = yr_fetch_block_data(block); \
if (data == NULL) \
return YR_UNDEFINED; \
result = *(type*) (data + offset - block->base); \
result = endianess##_##type(result); \
return result; \
} \
block = iterator->next(iterator); \
} \
return YR_UNDEFINED; \
};
function_read(uint8_t, little_endian);
function_read(uint16_t, little_endian);
function_read(uint32_t, little_endian);
function_read(int8_t, little_endian);
function_read(int16_t, little_endian);
function_read(int32_t, little_endian);
function_read(uint8_t, big_endian);
function_read(uint16_t, big_endian);
function_read(uint32_t, big_endian);
function_read(int8_t, big_endian);
function_read(int16_t, big_endian);
function_read(int32_t, big_endian);
static const uint8_t* jmp_if(int condition, const uint8_t* ip)
{
int32_t off = 0;
if (condition)
{
// The condition is true, the instruction pointer (ip) is incremented in
// the amount specified by the jump's offset, which is a int32_t following
// the jump opcode. The ip is currently past the opcode and pointing to
// the offset.
// Copy the offset from the instruction stream to a local variable.
off = yr_unaligned_u32(ip);
// The offset is relative to the jump opcode, but now the ip is one byte
// past the opcode, so we need to decrement it by one.
off -= 1;
}
else
{
// The condition is false, the execution flow proceeds with the instruction
// right after the jump.
off = sizeof(int32_t);
}
return ip + off;
}
static int iter_array_next(YR_ITERATOR* self, YR_VALUE_STACK* stack)
{
// Check that there's two available slots in the stack, one for the next
// item returned by the iterator and another one for the boolean that
// indicates if there are more items.
if (stack->sp + 1 >= stack->capacity)
return ERROR_EXEC_STACK_OVERFLOW;
// If the array that must be iterated is undefined stop the iteration right
// aways, as if the array would be empty.
if (IS_UNDEFINED(self->array_it.array))
goto _stop_iter;
// If the current index is equal or larger than array's length the iterator
// has reached the end of the array.
if (self->array_it.index >= yr_object_array_length(self->array_it.array))
goto _stop_iter;
// Push the false value that indicates that the iterator is not exhausted.
stack->items[stack->sp++].i = 0;
YR_OBJECT* obj = yr_object_array_get_item(
self->array_it.array, 0, self->array_it.index);
if (obj != NULL)
stack->items[stack->sp++].o = obj;
else
stack->items[stack->sp++].i = YR_UNDEFINED;
self->array_it.index++;
return ERROR_SUCCESS;
_stop_iter:
// Push true for indicating the iterator has been exhausted.
stack->items[stack->sp++].i = 1;
// Push YR_UNDEFINED as a placeholder for the next item.
stack->items[stack->sp++].i = YR_UNDEFINED;
return ERROR_SUCCESS;
}
static int iter_dict_next(YR_ITERATOR* self, YR_VALUE_STACK* stack)
{
// Check that there's three available slots in the stack, two for the next
// item returned by the iterator and its key, and another one for the boolean
// that indicates if there are more items.
if (stack->sp + 2 >= stack->capacity)
return ERROR_EXEC_STACK_OVERFLOW;
// If the dictionary that must be iterated is undefined, stop the iteration
// right away, as if the dictionary would be empty.
if (IS_UNDEFINED(self->dict_it.dict))
goto _stop_iter;
YR_DICTIONARY_ITEMS* items = object_as_dictionary(self->dict_it.dict)->items;
// If the dictionary has no items or the iterator reached the last item, abort
// the iteration, if not push the next key and value.
if (items == NULL || self->dict_it.index == items->used)
goto _stop_iter;
// Push the false value that indicates that the iterator is not exhausted.
stack->items[stack->sp++].i = 0;
if (items->objects[self->dict_it.index].obj != NULL)
{
stack->items[stack->sp++].o = items->objects[self->dict_it.index].obj;
stack->items[stack->sp++].p = items->objects[self->dict_it.index].key;
}
else
{
stack->items[stack->sp++].i = YR_UNDEFINED;
stack->items[stack->sp++].i = YR_UNDEFINED;
}
self->dict_it.index++;
return ERROR_SUCCESS;
_stop_iter:
// Push true for indicating the iterator has been exhausted.
stack->items[stack->sp++].i = 1;
// Push YR_UNDEFINED as a placeholder for the next key and value.
stack->items[stack->sp++].i = YR_UNDEFINED;
stack->items[stack->sp++].i = YR_UNDEFINED;
return ERROR_SUCCESS;
}
static int iter_int_range_next(YR_ITERATOR* self, YR_VALUE_STACK* stack)
{
// Check that there's two available slots in the stack, one for the next
// item returned by the iterator and another one for the boolean that
// indicates if there are more items.
if (stack->sp + 1 >= stack->capacity)
return ERROR_EXEC_STACK_OVERFLOW;
if (!IS_UNDEFINED(self->int_range_it.next) &&
!IS_UNDEFINED(self->int_range_it.last) &&
self->int_range_it.next <= self->int_range_it.last)
{
// Push the false value that indicates that the iterator is not exhausted.
stack->items[stack->sp++].i = 0;
stack->items[stack->sp++].i = self->int_range_it.next;
self->int_range_it.next++;
}
else
{
// Push true for indicating the iterator has been exhausted.
stack->items[stack->sp++].i = 1;
// Push YR_UNDEFINED as a placeholder for the next item.
stack->items[stack->sp++].i = YR_UNDEFINED;
}
return ERROR_SUCCESS;
}
static int iter_int_enum_next(YR_ITERATOR* self, YR_VALUE_STACK* stack)
{
// Check that there's two available slots in the stack, one for the next
// item returned by the iterator and another one for the boolean that
// indicates if there are more items.
if (stack->sp + 1 >= stack->capacity)
return ERROR_EXEC_STACK_OVERFLOW;
if (!IS_UNDEFINED(self->int_enum_it.next) &&
!IS_UNDEFINED(self->int_enum_it.count) &&
self->int_enum_it.next < self->int_enum_it.count)
{
// Push the false value that indicates that the iterator is not exhausted.
stack->items[stack->sp++].i = 0;
stack->items[stack->sp++].i =
self->int_enum_it.items[self->int_enum_it.next];
self->int_enum_it.next++;
}
else
{
// Push true for indicating the iterator has been exhausted.
stack->items[stack->sp++].i = 1;
// Push YR_UNDEFINED as a placeholder for the next item.
stack->items[stack->sp++].i = YR_UNDEFINED;
}
return ERROR_SUCCESS;
}
static int iter_string_set_next(YR_ITERATOR* self, YR_VALUE_STACK* stack)
{
// Check that there's two available slots in the stack, one for the next
// item returned by the iterator and another one for the boolean that
// indicates if there are more items.
if (stack->sp + 1 >= stack->capacity)
return ERROR_EXEC_STACK_OVERFLOW;
// If the current index is equal or larger than array's length the iterator
// has reached the end of the array.
if (self->string_set_it.index >= self->string_set_it.count)
goto _stop_iter;
// Push the false value that indicates that the iterator is not exhausted.
stack->items[stack->sp++].i = 0;
stack->items[stack->sp++].s =
self->string_set_it.strings[self->string_set_it.index];
self->string_set_it.index++;
return ERROR_SUCCESS;
_stop_iter:
// Push true for indicating the iterator has been exhausted.
stack->items[stack->sp++].i = 1;
// Push YR_UNDEFINED as a placeholder for the next item.
stack->items[stack->sp++].i = YR_UNDEFINED;
return ERROR_SUCCESS;
}
static int iter_text_string_set_next(YR_ITERATOR* self, YR_VALUE_STACK* stack)
{
// Check that there's two available slots in the stack, one for the next
// item returned by the iterator and another one for the boolean that
// indicates if there are more items.
if (stack->sp + 1 >= stack->capacity)
return ERROR_EXEC_STACK_OVERFLOW;
// If the current index is equal or larger than array's length the iterator
// has reached the end of the array.
if (self->text_string_set_it.index >= self->text_string_set_it.count)
goto _stop_iter;
// Push the false value that indicates that the iterator is not exhausted.
stack->items[stack->sp++].i = 0;
stack->items[stack->sp++].ss =
self->text_string_set_it.strings[self->text_string_set_it.index];
self->text_string_set_it.index++;
return ERROR_SUCCESS;
_stop_iter:
// Push true for indicating the iterator has been exhausted.
stack->items[stack->sp++].i = 1;
// Push YR_UNDEFINED as a placeholder for the next item.
stack->items[stack->sp++].i = YR_UNDEFINED;
return ERROR_SUCCESS;
}
// Global table that contains the "next" function for different types of
// iterators. The reason for using this table is to avoid storing pointers
// in the YARA's VM stack. Instead of the pointers we store an index within
// this table.
static YR_ITERATOR_NEXT_FUNC iter_next_func_table[] = {
iter_array_next,
iter_dict_next,
iter_int_range_next,
iter_int_enum_next,
iter_string_set_next,
iter_text_string_set_next,
};
#define ITER_NEXT_ARRAY 0
#define ITER_NEXT_DICT 1
#define ITER_NEXT_INT_RANGE 2
#define ITER_NEXT_INT_ENUM 3
#define ITER_NEXT_STRING_SET 4
#define ITER_NEXT_TEXT_STRING_SET 5
int yr_execute_code(YR_SCAN_CONTEXT* context)
{
YR_DEBUG_FPRINTF(2, stderr, "+ %s() {\n", __FUNCTION__);
const uint8_t* ip = context->rules->code_start;
YR_VALUE mem[MEM_SIZE];
YR_VALUE args[YR_MAX_FUNCTION_ARGS];
YR_VALUE r1;
YR_VALUE r2;
YR_VALUE r3;
YR_VALUE r4;
YR_VALUE_STACK stack;
uint64_t elapsed_time;
#ifdef YR_PROFILING_ENABLED
uint64_t start_time;
#endif
uint32_t current_rule_idx = 0;
YR_RULE* current_rule = NULL;
YR_RULE* rule;
YR_MATCH* match;
YR_OBJECT_FUNCTION* function;
YR_OBJECT** obj_ptr;
YR_ARENA* obj_arena;
YR_NOTEBOOK* it_notebook;
char* identifier;
char* args_fmt;
int found;
int count;
int result = ERROR_SUCCESS;
int cycle = 0;
int obj_count = 0;
bool stop = false;
uint8_t opcode;
yr_get_configuration_uint32(YR_CONFIG_STACK_SIZE, &stack.capacity);
stack.sp = 0;
stack.items = (YR_VALUE*) yr_malloc(stack.capacity * sizeof(YR_VALUE));
if (stack.items == NULL)
return ERROR_INSUFFICIENT_MEMORY;
FAIL_ON_ERROR_WITH_CLEANUP(
yr_arena_create(1, 512 * sizeof(YR_OBJECT*), &obj_arena),
yr_free(stack.items));
FAIL_ON_ERROR_WITH_CLEANUP(
yr_notebook_create(512 * sizeof(YR_ITERATOR), &it_notebook),
yr_arena_release(obj_arena);
yr_free(stack.items));
#ifdef YR_PROFILING_ENABLED
start_time = yr_stopwatch_elapsed_ns(&context->stopwatch);
#endif
#if YR_PARANOID_EXEC
memset(mem, 0, MEM_SIZE * sizeof(mem[0]));
#endif
while (!stop)
{
// Read the opcode from the address indicated by the instruction pointer.
opcode = *ip;
// Advance the instruction pointer, which now points past the opcode.
ip++;
switch (opcode)
{
case OP_NOP:
YR_DEBUG_FPRINTF(2, stderr, "- case OP_NOP: // %s()\n", __FUNCTION__);
break;
case OP_HALT:
YR_DEBUG_FPRINTF(2, stderr, "- case OP_HALT: // %s()\n", __FUNCTION__);
assert(stack.sp == 0); // When HALT is reached the stack should be empty.
stop = true;
break;
case OP_ITER_START_ARRAY:
YR_DEBUG_FPRINTF(
2, stderr, "- case OP_ITER_START_ARRAY: // %s()\n", __FUNCTION__);
r2.p = yr_notebook_alloc(it_notebook, sizeof(YR_ITERATOR));
if (r2.p == NULL)
{
result = ERROR_INSUFFICIENT_MEMORY;
}
else
{
pop(r1);
r2.it->array_it.array = r1.o;
r2.it->array_it.index = 0;
r2.it->next_func_idx = ITER_NEXT_ARRAY;
push(r2);
}
stop = (result != ERROR_SUCCESS);
break;
case OP_ITER_START_DICT:
YR_DEBUG_FPRINTF(
2, stderr, "- case OP_ITER_START_DICT: // %s()\n", __FUNCTION__);
r2.p = yr_notebook_alloc(it_notebook, sizeof(YR_ITERATOR));
if (r2.p == NULL)
{
result = ERROR_INSUFFICIENT_MEMORY;
}
else
{
pop(r1);
r2.it->dict_it.dict = r1.o;
r2.it->dict_it.index = 0;
r2.it->next_func_idx = ITER_NEXT_DICT;
push(r2);
}
stop = (result != ERROR_SUCCESS);
break;
case OP_ITER_START_INT_RANGE:
YR_DEBUG_FPRINTF(
2, stderr, "- case OP_ITER_START_INT_RANGE: // %s()\n", __FUNCTION__);
// Creates an iterator for an integer range. The higher bound of the
// range is at the top of the stack followed by the lower bound.
r3.p = yr_notebook_alloc(it_notebook, sizeof(YR_ITERATOR));
if (r3.p == NULL)
{
result = ERROR_INSUFFICIENT_MEMORY;
}
else
{
pop(r2);
pop(r1);
r3.it->int_range_it.next = r1.i;
r3.it->int_range_it.last = r2.i;
r3.it->next_func_idx = ITER_NEXT_INT_RANGE;
push(r3);
}
stop = (result != ERROR_SUCCESS);
break;
case OP_ITER_START_INT_ENUM:
YR_DEBUG_FPRINTF(
2, stderr, "- case OP_ITER_START_INT_ENUM: // %s()\n", __FUNCTION__);
// Creates an iterator for an integer enumeration. The number of items
// in the enumeration is at the top of the stack, followed by the
// items in reverse order.
pop(r1);
r3.p = yr_notebook_alloc(
it_notebook, sizeof(YR_ITERATOR) + sizeof(uint64_t) * (size_t) r1.i);
if (r3.p == NULL)
{
result = ERROR_INSUFFICIENT_MEMORY;
}
else
{
r3.it->int_enum_it.count = r1.i;
r3.it->int_enum_it.next = 0;
r3.it->next_func_idx = ITER_NEXT_INT_ENUM;
for (int64_t i = r1.i; i > 0; i--)
{
pop(r2);
r3.it->int_enum_it.items[i - 1] = r2.i;
}
push(r3);
}
stop = (result != ERROR_SUCCESS);
break;
case OP_ITER_START_STRING_SET:
YR_DEBUG_FPRINTF(
2,
stderr,
"- case OP_ITER_START_STRING_SET: // %s()\n",
__FUNCTION__);
pop(r1);
r3.p = yr_notebook_alloc(
it_notebook,
sizeof(YR_ITERATOR) + sizeof(YR_STRING*) * (size_t) r1.i);
if (r3.p == NULL)
{
result = ERROR_INSUFFICIENT_MEMORY;
}
else
{
r3.it->string_set_it.count = r1.i;
r3.it->string_set_it.index = 0;
r3.it->next_func_idx = ITER_NEXT_STRING_SET;
for (int64_t i = r1.i; i > 0; i--)
{
pop(r2);
r3.it->string_set_it.strings[i - 1] = r2.s;
}
// One last pop of the UNDEFINED string
pop(r2);
push(r3);
}
stop = (result != ERROR_SUCCESS);
break;
case OP_ITER_START_TEXT_STRING_SET:
YR_DEBUG_FPRINTF(
2,
stderr,
"- case OP_ITER_START_TEXT_STRING_SET: // %s()\n",
__FUNCTION__);
pop(r1);
r3.p = yr_notebook_alloc(
it_notebook,
sizeof(YR_ITERATOR) + sizeof(SIZED_STRING*) * (size_t) r1.i);
if (r3.p == NULL)
{
result = ERROR_INSUFFICIENT_MEMORY;
}
else
{
r3.it->text_string_set_it.count = r1.i;
r3.it->text_string_set_it.index = 0;
r3.it->next_func_idx = ITER_NEXT_TEXT_STRING_SET;
for (int64_t i = r1.i; i > 0; i--)
{
pop(r2);
r3.it->text_string_set_it.strings[i - 1] = r2.ss;
}
push(r3);
}
stop = (result != ERROR_SUCCESS);
break;
case OP_ITER_NEXT:
YR_DEBUG_FPRINTF(
2, stderr, "- case OP_ITER_NEXT: // %s()\n", __FUNCTION__);
// Loads the iterator in r1, but leaves the iterator in the stack.
pop(r1);
push(r1);
if (r1.it->next_func_idx <
sizeof(iter_next_func_table) / sizeof(YR_ITERATOR_NEXT_FUNC))
{
// The iterator's next function is responsible for pushing the next
// item in the stack, and a boolean indicating if there are more items
// to retrieve. The boolean will be at the top of the stack after
// calling "next".
result = iter_next_func_table[r1.it->next_func_idx](r1.it, &stack);
}
else
{
// next_func_idx is outside the valid range, this should not happend.
result = ERROR_INTERNAL_FATAL_ERROR;
}
stop = (result != ERROR_SUCCESS);
break;
case OP_ITER_CONDITION:
YR_DEBUG_FPRINTF(
2, stderr, "- case OP_ITER_CONDITION: // %s()\n", __FUNCTION__);
// Evaluate the iteration condition of the loop. This instruction
// evaluates to 1 if the loop should continue and 0 if it shouldn't
// (due to short-circuit evaluation).
pop(r2); // min. expression - all, any, none, integer
pop(r3); // number of true expressions
pop(r4); // last expression result
// In case of 'all' loop, end once we the body failed
if (is_undef(r2))
{
r1.i = r4.i != 0 ? 1 : 0;
}
// In case of 'none' loop, end once the body succeed
else if (r2.i == 0)
{
r1.i = r4.i != 1 ? 1 : 0;
}
// In case of other loops, end once we satified min. expr.
else
{
r1.i = r3.i + r4.i < r2.i ? 1 : 0;
}
// Push whether loop should continue and repush
// the last expression result
push(r1);
push(r4);
break;
case OP_ITER_END:
YR_DEBUG_FPRINTF(
2, stderr, "- case OP_ITER_END: // %s()\n", __FUNCTION__);
// Evaluate the whole loop. Whether it was successful or not
// and whether it satisfied it's quantifier.
pop(r2); // min. expression - all, any, none, integer
pop(r3); // number of true expressions
pop(r4); // number of total iterations
// If there was 0 iterations in total, it doesn't
// matter what other numbers show. We can't evaluate
// the loop as true.
if (r4.i == 0)
{
r1.i = 0;
}
else if (is_undef(r2))
{
r1.i = r3.i == r4.i ? 1 : 0;
}
else if (r2.i == 0)
{
r1.i = r3.i == 0 ? 1 : 0;
}
else
{
r1.i = r3.i >= r2.i ? 1 : 0;
}
push(r1);
break;
case OP_PUSH:
YR_DEBUG_FPRINTF(2, stderr, "- case OP_PUSH: // %s()\n", __FUNCTION__);
r1.i = yr_unaligned_u64(ip);
ip += sizeof(uint64_t);
push(r1);
break;
case OP_PUSH_8:
r1.i = *ip;
YR_DEBUG_FPRINTF(
2,
stderr,
"- case OP_PUSH_8: r1.i=%" PRId64 " // %s()\n",
r1.i,
__FUNCTION__);
ip += sizeof(uint8_t);
push(r1);
break;
case OP_PUSH_16:
r1.i = yr_unaligned_u16(ip);
YR_DEBUG_FPRINTF(
2,
stderr,
"- case OP_PUSH_16: r1.i=%" PRId64 " // %s()\n",
r1.i,
__FUNCTION__);
ip += sizeof(uint16_t);
push(r1);
break;
case OP_PUSH_32:
r1.i = yr_unaligned_u32(ip);
YR_DEBUG_FPRINTF(
2,
stderr,
"- case OP_PUSH_32: r1.i=%" PRId64 " // %s()\n",
r1.i,
__FUNCTION__);
ip += sizeof(uint32_t);
push(r1);
break;
case OP_PUSH_U:
YR_DEBUG_FPRINTF(2, stderr, "- case OP_PUSH_U: // %s()\n", __FUNCTION__);
r1.i = YR_UNDEFINED;
push(r1);
break;
case OP_POP:
YR_DEBUG_FPRINTF(2, stderr, "- case OP_POP: // %s()\n", __FUNCTION__);
pop(r1);
break;
case OP_CLEAR_M:
YR_DEBUG_FPRINTF(2, stderr, "- case OP_CLEAR_M: // %s()\n", __FUNCTION__);
r1.i = yr_unaligned_u64(ip);
ip += sizeof(uint64_t);
#if YR_PARANOID_EXEC
ensure_within_mem(r1.i);
#endif
mem[r1.i].i = 0;
break;
case OP_ADD_M:
YR_DEBUG_FPRINTF(2, stderr, "- case OP_ADD_M: // %s()\n", __FUNCTION__);
r1.i = yr_unaligned_u64(ip);
ip += sizeof(uint64_t);
#if YR_PARANOID_EXEC
ensure_within_mem(r1.i);
#endif
pop(r2);
if (!is_undef(r2))
mem[r1.i].i += r2.i;
break;
case OP_INCR_M:
YR_DEBUG_FPRINTF(2, stderr, "- case OP_INCR_M: // %s()\n", __FUNCTION__);
r1.i = yr_unaligned_u64(ip);
ip += sizeof(uint64_t);
#if YR_PARANOID_EXEC
ensure_within_mem(r1.i);
#endif
mem[r1.i].i++;
break;
case OP_PUSH_M:
YR_DEBUG_FPRINTF(2, stderr, "- case OP_PUSH_M: // %s()\n", __FUNCTION__);
r1.i = yr_unaligned_u64(ip);
ip += sizeof(uint64_t);
#if YR_PARANOID_EXEC
ensure_within_mem(r1.i);
#endif
r1 = mem[r1.i];
push(r1);
break;
case OP_POP_M:
YR_DEBUG_FPRINTF(2, stderr, "- case OP_POP_M: // %s()\n", __FUNCTION__);
r1.i = yr_unaligned_u64(ip);
ip += sizeof(uint64_t);
#if YR_PARANOID_EXEC
ensure_within_mem(r1.i);
#endif
pop(r2);
mem[r1.i] = r2;
break;
case OP_SET_M:
YR_DEBUG_FPRINTF(2, stderr, "- case OP_SET_M: // %s()\n", __FUNCTION__);
r1.i = yr_unaligned_u64(ip);
ip += sizeof(uint64_t);
#if YR_PARANOID_EXEC
ensure_within_mem(r1.i);
#endif
pop(r2);
push(r2);
if (!is_undef(r2))
mem[r1.i] = r2;
break;
case OP_SWAPUNDEF:
YR_DEBUG_FPRINTF(
2, stderr, "- case OP_SWAPUNDEF: // %s()\n", __FUNCTION__);
r1.i = yr_unaligned_u64(ip);
ip += sizeof(uint64_t);
#if YR_PARANOID_EXEC
ensure_within_mem(r1.i);
#endif
pop(r2);
if (is_undef(r2))
{
r1 = mem[r1.i];
push(r1);
}
else
{
push(r2);
}
break;
case OP_JNUNDEF:
// Jump if the top the stack is not undefined without modifying the stack.
YR_DEBUG_FPRINTF(2, stderr, "- case OP_JNUNDEF: // %s()\n", __FUNCTION__);
pop(r1);
push(r1);
ip = jmp_if(!is_undef(r1), ip);
break;
case OP_JUNDEF_P:
// Removes a value from the top of the stack and jump if the value is not
// undefined.
YR_DEBUG_FPRINTF(
2, stderr, "- case OP_JUNDEF_P: // %s()\n", __FUNCTION__);
pop(r1);
ip = jmp_if(is_undef(r1), ip);
break;
case OP_JL_P:
// Pops two values A and B from the stack and jump if A < B. B is popped
// first, and then A.
YR_DEBUG_FPRINTF(2, stderr, "- case OP_JL_P: // %s()\n", __FUNCTION__);
pop(r2);
pop(r1);
ip = jmp_if(r1.i < r2.i, ip);
break;
case OP_JLE_P:
// Pops two values A and B from the stack and jump if A <= B. B is popped
// first, and then A.
YR_DEBUG_FPRINTF(2, stderr, "- case OP_JLE_P: // %s()\n", __FUNCTION__);
pop(r2);
pop(r1);
ip = jmp_if(r1.i <= r2.i, ip);
break;
case OP_JTRUE:
// Jump if the top of the stack is true without modifying the stack. If
// the top of the stack is undefined the jump is not taken.
YR_DEBUG_FPRINTF(2, stderr, "- case OP_JTRUE: // %s()\n", __FUNCTION__);
pop(r1);
push(r1);
ip = jmp_if(!is_undef(r1) && r1.i, ip);
break;
case OP_JTRUE_P:
// Removes a value from the stack and jump if it is true. If the value
// is undefined the jump is not taken.
YR_DEBUG_FPRINTF(2, stderr, "- case OP_JTRUE_P: // %s()\n", __FUNCTION__);
pop(r1);
ip = jmp_if(!is_undef(r1) && r1.i, ip);
break;
case OP_JFALSE:
// Jump if the top of the stack is false without modifying the stack. If
// the top of the stack is undefined the jump is not taken.
YR_DEBUG_FPRINTF(2, stderr, "- case OP_JFALSE: // %s()\n", __FUNCTION__);
pop(r1);
push(r1);
ip = jmp_if(!is_undef(r1) && !r1.i, ip);
break;
case OP_JFALSE_P:
// Removes a value from the stack and jump if it is false. If the value
// is undefined the jump is not taken.
YR_DEBUG_FPRINTF(
2, stderr, "- case OP_JFALSE_P: // %s()\n", __FUNCTION__);
pop(r1);
ip = jmp_if(!is_undef(r1) && !r1.i, ip);
break;
case OP_JZ:
// Jump if the value at the top of the stack is 0 without modifying the
// stack.
YR_DEBUG_FPRINTF(2, stderr, "- case OP_JZ: // %s()\n", __FUNCTION__);
pop(r1);
push(r1);
ip = jmp_if(r1.i == 0, ip);
break;
case OP_JZ_P:
// Removes a value from the stack and jump if the value is 0.
YR_DEBUG_FPRINTF(2, stderr, "- case OP_JZ_P: // %s()\n", __FUNCTION__);
pop(r1);
ip = jmp_if(r1.i == 0, ip);
break;
case OP_AND:
YR_DEBUG_FPRINTF(2, stderr, "- case OP_AND: // %s()\n", __FUNCTION__);
pop(r2);