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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524
|
/*
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.
*/
#include <limits.h>
#include <stddef.h>
#include <string.h>
#include <yara/ahocorasick.h>
#include <yara/arena.h>
#include <yara/base64.h>
#include <yara/error.h>
#include <yara/exec.h>
#include <yara/integers.h>
#include <yara/mem.h>
#include <yara/modules.h>
#include <yara/object.h>
#include <yara/parser.h>
#include <yara/re.h>
#include <yara/strutils.h>
#include <yara/utils.h>
#include "yara/compiler.h"
#include "yara/types.h"
#define todigit(x) \
((x) >= 'A' && (x) <= 'F') ? ((uint8_t) (x - 'A' + 10)) \
: ((uint8_t) (x - '0'))
int yr_parser_emit(
yyscan_t yyscanner,
uint8_t instruction,
YR_ARENA_REF* instruction_ref)
{
return yr_arena_write_data(
yyget_extra(yyscanner)->arena,
YR_CODE_SECTION,
&instruction,
sizeof(uint8_t),
instruction_ref);
}
int yr_parser_emit_with_arg_double(
yyscan_t yyscanner,
uint8_t instruction,
double argument,
YR_ARENA_REF* instruction_ref,
YR_ARENA_REF* argument_ref)
{
int result = yr_arena_write_data(
yyget_extra(yyscanner)->arena,
YR_CODE_SECTION,
&instruction,
sizeof(uint8_t),
instruction_ref);
if (result == ERROR_SUCCESS)
result = yr_arena_write_data(
yyget_extra(yyscanner)->arena,
YR_CODE_SECTION,
&argument,
sizeof(double),
argument_ref);
return result;
}
int yr_parser_emit_with_arg_int32(
yyscan_t yyscanner,
uint8_t instruction,
int32_t argument,
YR_ARENA_REF* instruction_ref,
YR_ARENA_REF* argument_ref)
{
int result = yr_arena_write_data(
yyget_extra(yyscanner)->arena,
YR_CODE_SECTION,
&instruction,
sizeof(uint8_t),
instruction_ref);
if (result == ERROR_SUCCESS)
result = yr_arena_write_data(
yyget_extra(yyscanner)->arena,
YR_CODE_SECTION,
&argument,
sizeof(int32_t),
argument_ref);
return result;
}
int yr_parser_emit_with_arg(
yyscan_t yyscanner,
uint8_t instruction,
int64_t argument,
YR_ARENA_REF* instruction_ref,
YR_ARENA_REF* argument_ref)
{
int result = yr_arena_write_data(
yyget_extra(yyscanner)->arena,
YR_CODE_SECTION,
&instruction,
sizeof(uint8_t),
instruction_ref);
if (result == ERROR_SUCCESS)
result = yr_arena_write_data(
yyget_extra(yyscanner)->arena,
YR_CODE_SECTION,
&argument,
sizeof(int64_t),
argument_ref);
return result;
}
int yr_parser_emit_with_arg_reloc(
yyscan_t yyscanner,
uint8_t instruction,
void* argument,
YR_ARENA_REF* instruction_ref,
YR_ARENA_REF* argument_ref)
{
YR_ARENA_REF ref = YR_ARENA_NULL_REF;
DECLARE_REFERENCE(void*, ptr) arg;
memset(&arg, 0, sizeof(arg));
arg.ptr = argument;
int result = yr_arena_write_data(
yyget_extra(yyscanner)->arena,
YR_CODE_SECTION,
&instruction,
sizeof(uint8_t),
instruction_ref);
if (result == ERROR_SUCCESS)
result = yr_arena_write_data(
yyget_extra(yyscanner)->arena,
YR_CODE_SECTION,
&arg,
sizeof(arg),
&ref);
if (result == ERROR_SUCCESS)
result = yr_arena_make_ptr_relocatable(
yyget_extra(yyscanner)->arena, YR_CODE_SECTION, ref.offset, EOL);
if (argument_ref != NULL)
*argument_ref = ref;
return result;
}
int yr_parser_emit_pushes_for_strings(
yyscan_t yyscanner,
const char* identifier,
YR_STRING_SET* strings)
{
YR_COMPILER* compiler = yyget_extra(yyscanner);
YR_RULE* current_rule = _yr_compiler_get_rule_by_idx(
compiler, compiler->current_rule_idx);
YR_STRING* string;
const char* string_identifier;
const char* target_identifier;
strings->count = 0;
strings->head = NULL;
YR_STRING_SET_ELEMENT** tail_ptr = &strings->head;
yr_rule_strings_foreach(current_rule, string)
{
// Don't generate pushes for strings chained to another one, we are
// only interested in non-chained strings or the head of the chain.
if (string->chained_to == NULL)
{
string_identifier = string->identifier;
target_identifier = identifier;
while (*target_identifier != '\0' && *string_identifier != '\0' &&
*target_identifier == *string_identifier)
{
target_identifier++;
string_identifier++;
}
if ((*target_identifier == '\0' && *string_identifier == '\0') ||
*target_identifier == '*')
{
yr_parser_emit_with_arg_reloc(yyscanner, OP_PUSH, string, NULL, NULL);
string->flags |= STRING_FLAGS_REFERENCED;
string->flags &= ~STRING_FLAGS_FIXED_OFFSET;
strings->count++;
*tail_ptr = yr_malloc(sizeof(YR_STRING_SET_ELEMENT));
yr_arena_ptr_to_ref(compiler->arena, string, &((*tail_ptr)->element));
(*tail_ptr)->next = NULL;
tail_ptr = &(*tail_ptr)->next;
}
}
}
if (strings->count == 0)
{
yr_compiler_set_error_extra_info(
compiler, identifier) return ERROR_UNDEFINED_STRING;
}
return ERROR_SUCCESS;
}
// Emit OP_PUSH_RULE instructions for all rules whose identifier has given
// prefix.
int yr_parser_emit_pushes_for_rules(
yyscan_t yyscanner,
const char* prefix,
int* count)
{
YR_COMPILER* compiler = yyget_extra(yyscanner);
// Make sure the compiler is parsing a rule
assert(compiler->current_rule_idx != UINT32_MAX);
YR_RULE* rule;
int matching = 0;
YR_NAMESPACE* ns = (YR_NAMESPACE*) yr_arena_get_ptr(
compiler->arena,
YR_NAMESPACES_TABLE,
compiler->current_namespace_idx * sizeof(struct YR_NAMESPACE));
// Can't use yr_rules_foreach here as that requires the rules to have been
// finalized (inserting a NULL rule at the end). This is done when
// yr_compiler_get_rules() is called, which also inserts a HALT instruction
// into the current position in the code arena. Obviously we aren't done
// compiling the rules yet so inserting a HALT is a bad idea. To deal with
// this I'm manually walking all the currently compiled rules (up to the
// current rule index) and comparing identifiers to see if it is one we should
// use.
//
// Further, we have to get compiler->current_rule_idx before we start because
// if we emit an OP_PUSH_RULE
rule = yr_arena_get_ptr(compiler->arena, YR_RULES_TABLE, 0);
for (uint32_t i = 0; i <= compiler->current_rule_idx; i++)
{
// Is rule->identifier prefixed by prefix?
if (strncmp(prefix, rule->identifier, strlen(prefix)) == 0)
{
uint32_t rule_idx = yr_hash_table_lookup_uint32(
compiler->rules_table, rule->identifier, ns->name);
if (rule_idx != UINT32_MAX)
{
FAIL_ON_ERROR(yr_parser_emit_with_arg(
yyscanner, OP_PUSH_RULE, rule_idx, NULL, NULL));
matching++;
}
}
rule++;
}
if (count != NULL)
{
*count = matching;
}
if (matching == 0)
{
yr_compiler_set_error_extra_info(compiler, prefix);
return ERROR_UNDEFINED_IDENTIFIER;
}
return ERROR_SUCCESS;
}
int yr_parser_emit_push_const(yyscan_t yyscanner, uint64_t argument)
{
uint8_t opcode[9];
int opcode_len = 1;
if (argument == YR_UNDEFINED)
{
opcode[0] = OP_PUSH_U;
}
else if (argument <= 0xff)
{
opcode[0] = OP_PUSH_8;
opcode[1] = (uint8_t) argument;
opcode_len += sizeof(uint8_t);
}
else if (argument <= 0xffff)
{
opcode[0] = OP_PUSH_16;
uint16_t u = (uint16_t) argument;
memcpy(opcode + 1, &u, sizeof(uint16_t));
opcode_len += sizeof(uint16_t);
}
else if (argument <= 0xffffffff)
{
opcode[0] = OP_PUSH_32;
uint32_t u = (uint32_t) argument;
memcpy(opcode + 1, &u, sizeof(uint32_t));
opcode_len += sizeof(uint32_t);
}
else
{
opcode[0] = OP_PUSH;
memcpy(opcode + 1, &argument, sizeof(uint64_t));
opcode_len += sizeof(uint64_t);
}
return yr_arena_write_data(
yyget_extra(yyscanner)->arena, YR_CODE_SECTION, opcode, opcode_len, NULL);
}
int yr_parser_check_types(
YR_COMPILER* compiler,
YR_OBJECT_FUNCTION* function,
const char* actual_args_fmt)
{
int i;
for (i = 0; i < YR_MAX_OVERLOADED_FUNCTIONS; i++)
{
if (function->prototypes[i].arguments_fmt == NULL)
break;
if (strcmp(function->prototypes[i].arguments_fmt, actual_args_fmt) == 0)
return ERROR_SUCCESS;
}
yr_compiler_set_error_extra_info(compiler, function->identifier)
return ERROR_WRONG_ARGUMENTS;
}
int yr_parser_lookup_string(
yyscan_t yyscanner,
const char* identifier,
YR_STRING** string)
{
YR_COMPILER* compiler = yyget_extra(yyscanner);
YR_RULE* current_rule = _yr_compiler_get_rule_by_idx(
compiler, compiler->current_rule_idx);
yr_rule_strings_foreach(current_rule, *string)
{
// If some string $a gets fragmented into multiple chained
// strings, all those fragments have the same $a identifier
// but we are interested in the heading fragment, which is
// that with chained_to == NULL
if ((*string)->chained_to == NULL &&
strcmp((*string)->identifier, identifier) == 0)
{
return ERROR_SUCCESS;
}
}
yr_compiler_set_error_extra_info(compiler, identifier)
* string = NULL;
return ERROR_UNDEFINED_STRING;
}
////////////////////////////////////////////////////////////////////////////////
// Searches for a variable with the given identifier in the scope of the current
// "for" loop. In case of nested "for" loops the identifier is searched starting
// at the top-level loop and going down thorough the nested loops until the
// current one. This is ok because inner loops can not re-define an identifier
// already defined by an outer loop.
//
// If the variable is found, the return value is the position that the variable
// occupies among all the currently defined variables. If the variable doesn't
// exist the return value is -1.
//
// The function can receive a pointer to a YR_EXPRESSION that will populated
// with information about the variable if found. This pointer can be NULL if
// the caller is not interested in getting that information.
//
int yr_parser_lookup_loop_variable(
yyscan_t yyscanner,
const char* identifier,
YR_EXPRESSION* expr)
{
YR_COMPILER* compiler = yyget_extra(yyscanner);
int i, j;
int var_offset = 0;
for (i = 0; i <= compiler->loop_index; i++)
{
var_offset += compiler->loop[i].vars_internal_count;
for (j = 0; j < compiler->loop[i].vars_count; j++)
{
if (compiler->loop[i].vars[j].identifier.ptr != NULL &&
strcmp(identifier, compiler->loop[i].vars[j].identifier.ptr) == 0)
{
if (expr != NULL)
*expr = compiler->loop[i].vars[j];
return var_offset + j;
}
}
var_offset += compiler->loop[i].vars_count;
}
return -1;
}
static int _yr_parser_write_string(
const char* identifier,
YR_MODIFIER modifier,
YR_COMPILER* compiler,
SIZED_STRING* str,
RE_AST* re_ast,
YR_ARENA_REF* string_ref,
int* min_atom_quality,
int* num_atom)
{
SIZED_STRING* literal_string;
YR_ATOM_LIST_ITEM* atom;
YR_ATOM_LIST_ITEM* atom_list = NULL;
int c, result;
int max_string_len;
bool free_literal = false;
FAIL_ON_ERROR(yr_arena_allocate_struct(
compiler->arena,
YR_STRINGS_TABLE,
sizeof(YR_STRING),
string_ref,
offsetof(YR_STRING, identifier),
offsetof(YR_STRING, string),
offsetof(YR_STRING, chained_to),
EOL));
YR_STRING* string = (YR_STRING*) yr_arena_ref_to_ptr(
compiler->arena, string_ref);
YR_ARENA_REF ref;
FAIL_ON_ERROR(_yr_compiler_store_string(compiler, identifier, &ref));
string->identifier = (const char*) yr_arena_ref_to_ptr(compiler->arena, &ref);
string->rule_idx = compiler->current_rule_idx;
string->idx = compiler->current_string_idx;
string->fixed_offset = YR_UNDEFINED;
compiler->current_string_idx++;
if (modifier.flags & STRING_FLAGS_HEXADECIMAL ||
modifier.flags & STRING_FLAGS_REGEXP ||
modifier.flags & STRING_FLAGS_BASE64 ||
modifier.flags & STRING_FLAGS_BASE64_WIDE)
{
literal_string = yr_re_ast_extract_literal(re_ast);
if (literal_string != NULL)
free_literal = true;
}
else
{
literal_string = str;
}
if (literal_string != NULL)
{
modifier.flags |= STRING_FLAGS_LITERAL;
result = _yr_compiler_store_data(
compiler,
literal_string->c_string,
literal_string->length + 1, // +1 to include terminating NULL
&ref);
if (result != ERROR_SUCCESS)
goto cleanup;
string->length = (uint32_t) literal_string->length;
string->string = (uint8_t*) yr_arena_ref_to_ptr(compiler->arena, &ref);
if (modifier.flags & STRING_FLAGS_WIDE)
max_string_len = string->length * 2;
else
max_string_len = string->length;
if (max_string_len <= YR_MAX_ATOM_LENGTH)
modifier.flags |= STRING_FLAGS_FITS_IN_ATOM;
result = yr_atoms_extract_from_string(
&compiler->atoms_config,
(uint8_t*) literal_string->c_string,
(int32_t) literal_string->length,
modifier,
&atom_list,
min_atom_quality);
if (result != ERROR_SUCCESS)
goto cleanup;
}
else
{
// Non-literal strings can't be marked as fixed offset because once we
// find a string atom in the scanned data we don't know the offset where
// the string should start, as the non-literal strings can contain
// variable-length portions.
modifier.flags &= ~STRING_FLAGS_FIXED_OFFSET;
// Save the position where the RE forward code starts for later reference.
yr_arena_off_t forward_code_start = yr_arena_get_current_offset(
compiler->arena, YR_RE_CODE_SECTION);
// Emit forwards code
result = yr_re_ast_emit_code(re_ast, compiler->arena, false);
if (result != ERROR_SUCCESS)
goto cleanup;
// Emit backwards code
result = yr_re_ast_emit_code(re_ast, compiler->arena, true);
if (result != ERROR_SUCCESS)
goto cleanup;
// Extract atoms from the regular expression.
result = yr_atoms_extract_from_re(
&compiler->atoms_config,
re_ast,
modifier,
&atom_list,
min_atom_quality);
if (result != ERROR_SUCCESS)
goto cleanup;
// If no atom was extracted let's add a zero-length atom.
if (atom_list == NULL)
{
atom_list = (YR_ATOM_LIST_ITEM*) yr_malloc(sizeof(YR_ATOM_LIST_ITEM));
if (atom_list == NULL)
{
result = ERROR_INSUFFICIENT_MEMORY;
goto cleanup;
}
atom_list->atom.length = 0;
atom_list->backtrack = 0;
atom_list->backward_code_ref = YR_ARENA_NULL_REF;
atom_list->next = NULL;
yr_arena_ptr_to_ref(
compiler->arena,
yr_arena_get_ptr(
compiler->arena, YR_RE_CODE_SECTION, forward_code_start),
&(atom_list->forward_code_ref));
}
}
string->flags = modifier.flags;
// Add the string to Aho-Corasick automaton.
result = yr_ac_add_string(
compiler->automaton, string, string->idx, atom_list, compiler->arena);
if (result != ERROR_SUCCESS)
goto cleanup;
atom = atom_list;
c = 0;
while (atom != NULL)
{
atom = atom->next;
c++;
}
(*num_atom) += c;
cleanup:
if (free_literal)
yr_free(literal_string);
if (atom_list != NULL)
yr_atoms_list_destroy(atom_list);
return result;
}
static int _yr_parser_check_string_modifiers(
yyscan_t yyscanner,
YR_MODIFIER modifier)
{
YR_COMPILER* compiler = yyget_extra(yyscanner);
// xor and nocase together is not implemented.
if (modifier.flags & STRING_FLAGS_XOR &&
modifier.flags & STRING_FLAGS_NO_CASE)
{
yr_compiler_set_error_extra_info(
compiler, "invalid modifier combination: xor nocase");
return ERROR_INVALID_MODIFIER;
}
// base64 and nocase together is not implemented.
if (modifier.flags & STRING_FLAGS_NO_CASE &&
(modifier.flags & STRING_FLAGS_BASE64 ||
modifier.flags & STRING_FLAGS_BASE64_WIDE))
{
yr_compiler_set_error_extra_info(
compiler,
modifier.flags & STRING_FLAGS_BASE64
? "invalid modifier combination: base64 nocase"
: "invalid modifier combination: base64wide nocase");
return ERROR_INVALID_MODIFIER;
}
// base64 and fullword together is not implemented.
if (modifier.flags & STRING_FLAGS_FULL_WORD &&
(modifier.flags & STRING_FLAGS_BASE64 ||
modifier.flags & STRING_FLAGS_BASE64_WIDE))
{
yr_compiler_set_error_extra_info(
compiler,
modifier.flags & STRING_FLAGS_BASE64
? "invalid modifier combination: base64 fullword"
: "invalid modifier combination: base64wide fullword");
return ERROR_INVALID_MODIFIER;
}
// base64 and xor together is not implemented.
if (modifier.flags & STRING_FLAGS_XOR &&
(modifier.flags & STRING_FLAGS_BASE64 ||
modifier.flags & STRING_FLAGS_BASE64_WIDE))
{
yr_compiler_set_error_extra_info(
compiler,
modifier.flags & STRING_FLAGS_BASE64
? "invalid modifier combination: base64 xor"
: "invalid modifier combination: base64wide xor");
return ERROR_INVALID_MODIFIER;
}
return ERROR_SUCCESS;
}
int yr_parser_reduce_string_declaration(
yyscan_t yyscanner,
YR_MODIFIER modifier,
const char* identifier,
SIZED_STRING* str,
YR_ARENA_REF* string_ref)
{
int result = ERROR_SUCCESS;
int min_atom_quality = YR_MAX_ATOM_QUALITY;
int atom_quality;
char message[512];
int32_t min_gap = 0;
int32_t max_gap = 0;
YR_COMPILER* compiler = yyget_extra(yyscanner);
RE_AST* re_ast = NULL;
RE_AST* remainder_re_ast = NULL;
RE_ERROR re_error;
YR_RULE* current_rule = _yr_compiler_get_rule_by_idx(
compiler, compiler->current_rule_idx);
// Determine if a string with the same identifier was already defined
// by searching for the identifier in strings_table.
uint32_t string_idx = yr_hash_table_lookup_uint32(
compiler->strings_table, identifier, NULL);
// The string was already defined, return an error.
if (string_idx != UINT32_MAX)
{
yr_compiler_set_error_extra_info(compiler, identifier);
return ERROR_DUPLICATED_STRING_IDENTIFIER;
}
// Empty strings are not allowed.
if (str->length == 0)
{
yr_compiler_set_error_extra_info(compiler, identifier);
return ERROR_EMPTY_STRING;
}
if (str->flags & SIZED_STRING_FLAGS_NO_CASE)
modifier.flags |= STRING_FLAGS_NO_CASE;
if (str->flags & SIZED_STRING_FLAGS_DOT_ALL)
modifier.flags |= STRING_FLAGS_DOT_ALL;
// Hex strings are always handled as DOT_ALL regexps.
if (modifier.flags & STRING_FLAGS_HEXADECIMAL)
modifier.flags |= STRING_FLAGS_DOT_ALL;
if (!(modifier.flags & STRING_FLAGS_WIDE) &&
!(modifier.flags & STRING_FLAGS_BASE64 ||
modifier.flags & STRING_FLAGS_BASE64_WIDE))
{
modifier.flags |= STRING_FLAGS_ASCII;
}
// The STRING_FLAGS_SINGLE_MATCH flag indicates that finding
// a single match for the string is enough. This is true in
// most cases, except when the string count (#) and string offset (@)
// operators are used. All strings are marked STRING_FLAGS_SINGLE_MATCH
// initially, and unmarked later if required.
modifier.flags |= STRING_FLAGS_SINGLE_MATCH;
// The STRING_FLAGS_FIXED_OFFSET indicates that the string doesn't
// need to be searched all over the file because the user is using the
// "at" operator. The string must be searched at a fixed offset in the
// file. All strings are marked STRING_FLAGS_FIXED_OFFSET initially,
// and unmarked later if required.
modifier.flags |= STRING_FLAGS_FIXED_OFFSET;
// If string identifier is $ this is an anonymous string, if not add the
// identifier to strings_table.
if (strcmp(identifier, "$") == 0)
{
modifier.flags |= STRING_FLAGS_ANONYMOUS;
}
else
{
FAIL_ON_ERROR(yr_hash_table_add_uint32(
compiler->strings_table,
identifier,
NULL,
compiler->current_string_idx));
}
// Make sure that the the string does not have an invalid combination of
// modifiers.
FAIL_ON_ERROR(_yr_parser_check_string_modifiers(yyscanner, modifier));
if (modifier.flags & STRING_FLAGS_HEXADECIMAL ||
modifier.flags & STRING_FLAGS_REGEXP ||
modifier.flags & STRING_FLAGS_BASE64 ||
modifier.flags & STRING_FLAGS_BASE64_WIDE)
{
if (modifier.flags & STRING_FLAGS_HEXADECIMAL)
result = yr_re_parse_hex(str->c_string, &re_ast, &re_error);
else if (modifier.flags & STRING_FLAGS_REGEXP)
{
int flags = RE_PARSER_FLAG_NONE;
if (compiler->strict_escape)
flags |= RE_PARSER_FLAG_ENABLE_STRICT_ESCAPE_SEQUENCES;
result = yr_re_parse(str->c_string, &re_ast, &re_error, flags);
}
else
result = yr_base64_ast_from_string(str, modifier, &re_ast, &re_error);
if (result != ERROR_SUCCESS)
{
if (result == ERROR_UNKNOWN_ESCAPE_SEQUENCE)
{
yywarning(yyscanner, "unknown escape sequence");
}
else
{
snprintf(
message,
sizeof(message),
"invalid %s \"%s\": %s",
(modifier.flags & STRING_FLAGS_HEXADECIMAL) ? "hex string"
: "regular expression",
identifier,
re_error.message);
yr_compiler_set_error_extra_info(compiler, message);
goto _exit;
}
}
if (re_ast->flags & RE_FLAGS_FAST_REGEXP)
modifier.flags |= STRING_FLAGS_FAST_REGEXP;
if (re_ast->flags & RE_FLAGS_GREEDY)
modifier.flags |= STRING_FLAGS_GREEDY_REGEXP;
// Regular expressions in the strings section can't mix greedy and
// ungreedy quantifiers like .* and .*?. That's because these regular
// expressions can be matched forwards and/or backwards depending on the
// atom found, and we need the regexp to be all-greedy or all-ungreedy to
// be able to properly calculate the length of the match.
if ((re_ast->flags & RE_FLAGS_GREEDY) &&
(re_ast->flags & RE_FLAGS_UNGREEDY))
{
result = ERROR_INVALID_REGULAR_EXPRESSION;
yr_compiler_set_error_extra_info(
compiler,
"greedy and ungreedy quantifiers can't be mixed in a regular "
"expression");
goto _exit;
}
if (yr_re_ast_has_unbounded_quantifier_for_dot(re_ast))
{
yywarning(
yyscanner,
"%s contains .*, .+ or .{x,} consider using .{,N}, .{1,N} or {x,N} "
"with a reasonable value for N",
identifier);
}
if (compiler->re_ast_callback != NULL)
{
compiler->re_ast_callback(
current_rule, identifier, re_ast, compiler->re_ast_clbk_user_data);
}
*string_ref = YR_ARENA_NULL_REF;
while (re_ast != NULL)
{
YR_ARENA_REF ref;
uint32_t prev_string_idx = compiler->current_string_idx - 1;
int32_t prev_min_gap = min_gap;
int32_t prev_max_gap = max_gap;
result = yr_re_ast_split_at_chaining_point(
re_ast, &remainder_re_ast, &min_gap, &max_gap);
if (result != ERROR_SUCCESS)
goto _exit;
result = _yr_parser_write_string(
identifier,
modifier,
compiler,
NULL,
re_ast,
&ref,
&atom_quality,
¤t_rule->num_atoms);
if (result != ERROR_SUCCESS)
goto _exit;
if (atom_quality < min_atom_quality)
min_atom_quality = atom_quality;
if (YR_ARENA_IS_NULL_REF(*string_ref))
{
// This is the first string in the chain, the string reference
// returned by this function must point to this string.
*string_ref = ref;
}
else
{
// This is not the first string in the chain, set the appropriate
// flags and fill the chained_to, chain_gap_min and chain_gap_max
// fields.
YR_STRING* prev_string = (YR_STRING*) yr_arena_get_ptr(
compiler->arena,
YR_STRINGS_TABLE,
prev_string_idx * sizeof(YR_STRING));
YR_STRING* new_string = (YR_STRING*) yr_arena_ref_to_ptr(
compiler->arena, &ref);
new_string->chained_to = prev_string;
new_string->chain_gap_min = prev_min_gap;
new_string->chain_gap_max = prev_max_gap;
// A string chained to another one can't have a fixed offset, only the
// head of the string chain can have a fixed offset.
new_string->flags &= ~STRING_FLAGS_FIXED_OFFSET;
// There is a previous string, but that string wasn't marked as part
// of a chain because we can't do that until knowing there will be
// another string, let's flag it now the we know.
prev_string->flags |= STRING_FLAGS_CHAIN_PART;
// There is a previous string, so this string is part of a chain, but
// there will be no more strings because there are no more AST to
// split, which means that this is the chain's tail.
if (remainder_re_ast == NULL)
new_string->flags |= STRING_FLAGS_CHAIN_PART |
STRING_FLAGS_CHAIN_TAIL;
}
yr_re_ast_destroy(re_ast);
re_ast = remainder_re_ast;
}
}
else // not a STRING_FLAGS_HEXADECIMAL or STRING_FLAGS_REGEXP or
// STRING_FLAGS_BASE64 or STRING_FLAGS_BASE64_WIDE
{
result = _yr_parser_write_string(
identifier,
modifier,
compiler,
str,
NULL,
string_ref,
&min_atom_quality,
¤t_rule->num_atoms);
if (result != ERROR_SUCCESS)
goto _exit;
}
if (min_atom_quality < compiler->atoms_config.quality_warning_threshold)
{
yywarning(yyscanner, "string \"%s\" may slow down scanning", identifier);
}
_exit:
if (re_ast != NULL)
yr_re_ast_destroy(re_ast);
if (remainder_re_ast != NULL)
yr_re_ast_destroy(remainder_re_ast);
return result;
}
static int wildcard_iterator(
void* prefix,
size_t prefix_len,
void* _value,
void* data)
{
const char* identifier = (const char*) data;
// If the identifier is prefixed by prefix, then it matches the wildcard.
if (!strncmp(prefix, identifier, prefix_len))
return ERROR_IDENTIFIER_MATCHES_WILDCARD;
return ERROR_SUCCESS;
}
int yr_parser_reduce_rule_declaration_phase_1(
yyscan_t yyscanner,
int32_t flags,
const char* identifier,
YR_ARENA_REF* rule_ref)
{
int result;
YR_FIXUP* fixup;
YR_COMPILER* compiler = yyget_extra(yyscanner);
YR_NAMESPACE* ns = (YR_NAMESPACE*) yr_arena_get_ptr(
compiler->arena,
YR_NAMESPACES_TABLE,
compiler->current_namespace_idx * sizeof(struct YR_NAMESPACE));
if (yr_hash_table_lookup_uint32(
compiler->rules_table, identifier, ns->name) != UINT32_MAX ||
yr_hash_table_lookup(compiler->objects_table, identifier, NULL) != NULL)
{
// A rule or variable with the same identifier already exists, return the
// appropriate error.
yr_compiler_set_error_extra_info(compiler, identifier);
return ERROR_DUPLICATED_IDENTIFIER;
}
// Iterate over all identifiers in wildcard_identifiers_table, and check if
// any of them are a prefix of the identifier being declared. If so, return
// ERROR_IDENTIFIER_MATCHES_WILDCARD.
result = yr_hash_table_iterate(
compiler->wildcard_identifiers_table,
ns->name,
wildcard_iterator,
(void*) identifier);
if (result == ERROR_IDENTIFIER_MATCHES_WILDCARD)
{
// This rule matches an existing wildcard rule set.
yr_compiler_set_error_extra_info(compiler, identifier);
}
FAIL_ON_ERROR(result);
FAIL_ON_ERROR(yr_arena_allocate_struct(
compiler->arena,
YR_RULES_TABLE,
sizeof(YR_RULE),
rule_ref,
offsetof(YR_RULE, identifier),
offsetof(YR_RULE, tags),
offsetof(YR_RULE, strings),
offsetof(YR_RULE, metas),
offsetof(YR_RULE, ns),
EOL));
YR_RULE* rule = (YR_RULE*) yr_arena_ref_to_ptr(compiler->arena, rule_ref);
YR_ARENA_REF ref;
FAIL_ON_ERROR(_yr_compiler_store_string(compiler, identifier, &ref));
rule->identifier = (const char*) yr_arena_ref_to_ptr(compiler->arena, &ref);
rule->flags = flags;
rule->ns = ns;
rule->num_atoms = 0;
YR_ARENA_REF jmp_offset_ref;
// We are starting to parse a new rule, set current_rule_idx accordingly.
compiler->current_rule_idx = compiler->next_rule_idx;
compiler->next_rule_idx++;
// The OP_INIT_RULE instruction behaves like a jump. When the rule is
// disabled it skips over the rule's code and go straight to the next rule's
// code. The jmp_offset_ref variable points to the jump's offset. The offset
// is set to 0 as we don't know the jump target yet. When we finish
// generating the rule's code in yr_parser_reduce_rule_declaration_phase_2
// the jump offset is set to its final value.
FAIL_ON_ERROR(yr_parser_emit_with_arg_int32(
yyscanner, OP_INIT_RULE, 0, NULL, &jmp_offset_ref));
FAIL_ON_ERROR(yr_arena_write_data(
compiler->arena,
YR_CODE_SECTION,
&compiler->current_rule_idx,
sizeof(compiler->current_rule_idx),
NULL));
// Create a fixup entry for the jump and push it in the stack
fixup = (YR_FIXUP*) yr_malloc(sizeof(YR_FIXUP));
if (fixup == NULL)
return ERROR_INSUFFICIENT_MEMORY;
fixup->ref = jmp_offset_ref;
fixup->next = compiler->fixup_stack_head;
compiler->fixup_stack_head = fixup;
// Clean strings_table as we are starting to parse a new rule.
yr_hash_table_clean(compiler->strings_table, NULL);
FAIL_ON_ERROR(yr_hash_table_add_uint32(
compiler->rules_table, identifier, ns->name, compiler->current_rule_idx));
return ERROR_SUCCESS;
}
int yr_parser_reduce_rule_declaration_phase_2(
yyscan_t yyscanner,
YR_ARENA_REF* rule_ref)
{
uint32_t max_strings_per_rule;
uint32_t strings_in_rule = 0;
YR_FIXUP* fixup;
YR_STRING* string;
YR_COMPILER* compiler = yyget_extra(yyscanner);
yr_get_configuration_uint32(
YR_CONFIG_MAX_STRINGS_PER_RULE, &max_strings_per_rule);
YR_RULE* rule = (YR_RULE*) yr_arena_ref_to_ptr(compiler->arena, rule_ref);
// Show warning if the rule is generating too many atoms. The warning is
// shown if the number of atoms is greater than 20 times the maximum number
// of strings allowed for a rule, as 20 is minimum number of atoms generated
// for a string using *nocase*, *ascii* and *wide* modifiers simultaneously.
if (rule->num_atoms > YR_ATOMS_PER_RULE_WARNING_THRESHOLD)
{
yywarning(yyscanner, "rule is slowing down scanning");
}
yr_rule_strings_foreach(rule, string)
{
// Only the heading fragment in a chain of strings (the one with
// chained_to == NULL) must be referenced. All other fragments
// are never marked as referenced.
//
// Any string identifier that starts with '_' can be unreferenced. Anonymous
// strings must always be referenced.
if (!STRING_IS_REFERENCED(string) && string->chained_to == NULL &&
(STRING_IS_ANONYMOUS(string) ||
(!STRING_IS_ANONYMOUS(string) && string->identifier[1] != '_')))
{
yr_compiler_set_error_extra_info(
compiler, string->identifier) return ERROR_UNREFERENCED_STRING;
}
// If a string is unreferenced we need to unset the FIXED_OFFSET flag so
// that it will match anywhere.
if (!STRING_IS_REFERENCED(string) && string->chained_to == NULL &&
STRING_IS_FIXED_OFFSET(string))
{
string->flags &= ~STRING_FLAGS_FIXED_OFFSET;
}
strings_in_rule++;
if (strings_in_rule > max_strings_per_rule)
{
yr_compiler_set_error_extra_info(
compiler, rule->identifier) return ERROR_TOO_MANY_STRINGS;
}
}
FAIL_ON_ERROR(yr_parser_emit_with_arg(
yyscanner, OP_MATCH_RULE, compiler->current_rule_idx, NULL, NULL));
fixup = compiler->fixup_stack_head;
int32_t* jmp_offset_addr = (int32_t*) yr_arena_ref_to_ptr(
compiler->arena, &fixup->ref);
int32_t jmp_offset = yr_arena_get_current_offset(
compiler->arena, YR_CODE_SECTION) -
fixup->ref.offset + 1;
memcpy(jmp_offset_addr, &jmp_offset, sizeof(jmp_offset));
// Remove fixup from the stack.
compiler->fixup_stack_head = fixup->next;
yr_free(fixup);
// We have finished parsing the current rule set current_rule_idx to
// UINT32_MAX indicating that we are not currently parsing a rule.
compiler->current_rule_idx = UINT32_MAX;
return ERROR_SUCCESS;
}
int yr_parser_reduce_string_identifier(
yyscan_t yyscanner,
const char* identifier,
uint8_t instruction,
uint64_t at_offset)
{
YR_STRING* string;
YR_COMPILER* compiler = yyget_extra(yyscanner);
if (strcmp(identifier, "$") == 0) // is an anonymous string ?
{
if (compiler->loop_for_of_var_index >= 0) // inside a loop ?
{
yr_parser_emit_with_arg(
yyscanner, OP_PUSH_M, compiler->loop_for_of_var_index, NULL, NULL);
yr_parser_emit(yyscanner, instruction, NULL);
YR_RULE* current_rule = _yr_compiler_get_rule_by_idx(
compiler, compiler->current_rule_idx);
yr_rule_strings_foreach(current_rule, string)
{
if (instruction != OP_FOUND)
string->flags &= ~STRING_FLAGS_SINGLE_MATCH;
if (instruction == OP_FOUND_AT)
{
// Avoid overwriting any previous fixed offset
if (string->fixed_offset == YR_UNDEFINED)
string->fixed_offset = at_offset;
// If a previous fixed offset was different, disable
// the STRING_GFLAGS_FIXED_OFFSET flag because we only
// have room to store a single fixed offset value
if (string->fixed_offset != at_offset)
string->flags &= ~STRING_FLAGS_FIXED_OFFSET;
}
else
{
string->flags &= ~STRING_FLAGS_FIXED_OFFSET;
}
}
}
else
{
// Anonymous strings not allowed outside of a loop
return ERROR_MISPLACED_ANONYMOUS_STRING;
}
}
else
{
FAIL_ON_ERROR(yr_parser_lookup_string(yyscanner, identifier, &string));
FAIL_ON_ERROR(
yr_parser_emit_with_arg_reloc(yyscanner, OP_PUSH, string, NULL, NULL));
if (instruction != OP_FOUND)
string->flags &= ~STRING_FLAGS_SINGLE_MATCH;
if (instruction == OP_FOUND_AT)
{
// Avoid overwriting any previous fixed offset
if (string->fixed_offset == YR_UNDEFINED)
string->fixed_offset = at_offset;
// If a previous fixed offset was different, disable
// the STRING_GFLAGS_FIXED_OFFSET flag because we only
// have room to store a single fixed offset value
if (string->fixed_offset == YR_UNDEFINED ||
string->fixed_offset != at_offset)
{
string->flags &= ~STRING_FLAGS_FIXED_OFFSET;
}
}
else
{
string->flags &= ~STRING_FLAGS_FIXED_OFFSET;
}
FAIL_ON_ERROR(yr_parser_emit(yyscanner, instruction, NULL));
string->flags |= STRING_FLAGS_REFERENCED;
}
return ERROR_SUCCESS;
}
int yr_parser_reduce_meta_declaration(
yyscan_t yyscanner,
int32_t type,
const char* identifier,
const char* string,
int64_t integer,
YR_ARENA_REF* meta_ref)
{
YR_ARENA_REF ref;
YR_COMPILER* compiler = yyget_extra(yyscanner);
FAIL_ON_ERROR(yr_arena_allocate_struct(
compiler->arena,
YR_METAS_TABLE,
sizeof(YR_META),
meta_ref,
offsetof(YR_META, identifier),
offsetof(YR_META, string),
EOL));
YR_META* meta = (YR_META*) yr_arena_ref_to_ptr(compiler->arena, meta_ref);
meta->type = type;
meta->integer = integer;
FAIL_ON_ERROR(_yr_compiler_store_string(compiler, identifier, &ref));
meta->identifier = (const char*) yr_arena_ref_to_ptr(compiler->arena, &ref);
if (string != NULL)
{
FAIL_ON_ERROR(_yr_compiler_store_string(compiler, string, &ref));
meta->string = (const char*) yr_arena_ref_to_ptr(compiler->arena, &ref);
}
else
{
meta->string = NULL;
}
compiler->current_meta_idx++;
return ERROR_SUCCESS;
}
static int _yr_parser_valid_module_name(SIZED_STRING* module_name)
{
if (module_name->length == 0)
return false;
if (strlen(module_name->c_string) != module_name->length)
return false;
return true;
}
int yr_parser_reduce_import(yyscan_t yyscanner, SIZED_STRING* module_name)
{
int result;
YR_ARENA_REF ref;
YR_COMPILER* compiler = yyget_extra(yyscanner);
YR_OBJECT* module_structure;
if (!_yr_parser_valid_module_name(module_name))
{
yr_compiler_set_error_extra_info(compiler, module_name->c_string);
return ERROR_INVALID_MODULE_NAME;
}
YR_NAMESPACE* ns = (YR_NAMESPACE*) yr_arena_get_ptr(
compiler->arena,
YR_NAMESPACES_TABLE,
compiler->current_namespace_idx * sizeof(struct YR_NAMESPACE));
module_structure = (YR_OBJECT*) yr_hash_table_lookup(
compiler->objects_table, module_name->c_string, ns->name);
// if module already imported, do nothing
if (module_structure != NULL)
return ERROR_SUCCESS;
FAIL_ON_ERROR(yr_object_create(
OBJECT_TYPE_STRUCTURE, module_name->c_string, NULL, &module_structure));
FAIL_ON_ERROR(yr_hash_table_add(
compiler->objects_table,
module_name->c_string,
ns->name,
module_structure));
result = yr_modules_do_declarations(module_name->c_string, module_structure);
if (result == ERROR_UNKNOWN_MODULE)
yr_compiler_set_error_extra_info(compiler, module_name->c_string);
if (result != ERROR_SUCCESS)
return result;
FAIL_ON_ERROR(
_yr_compiler_store_string(compiler, module_name->c_string, &ref));
FAIL_ON_ERROR(yr_parser_emit_with_arg_reloc(
yyscanner,
OP_IMPORT,
yr_arena_ref_to_ptr(compiler->arena, &ref),
NULL,
NULL));
return ERROR_SUCCESS;
}
static int _yr_parser_operator_to_opcode(const char* op, int expression_type)
{
int opcode = 0;
switch (expression_type)
{
case EXPRESSION_TYPE_INTEGER:
opcode = OP_INT_BEGIN;
break;
case EXPRESSION_TYPE_FLOAT:
opcode = OP_DBL_BEGIN;
break;
case EXPRESSION_TYPE_STRING:
opcode = OP_STR_BEGIN;
break;
default:
assert(false);
}
if (op[0] == '<')
{
if (op[1] == '=')
opcode += _OP_LE;
else
opcode += _OP_LT;
}
else if (op[0] == '>')
{
if (op[1] == '=')
opcode += _OP_GE;
else
opcode += _OP_GT;
}
else if (op[1] == '=')
{
if (op[0] == '=')
opcode += _OP_EQ;
else
opcode += _OP_NEQ;
}
else if (op[0] == '+')
{
opcode += _OP_ADD;
}
else if (op[0] == '-')
{
opcode += _OP_SUB;
}
else if (op[0] == '*')
{
opcode += _OP_MUL;
}
else if (op[0] == '\\')
{
opcode += _OP_DIV;
}
if (IS_INT_OP(opcode) || IS_DBL_OP(opcode) || IS_STR_OP(opcode))
{
return opcode;
}
return OP_ERROR;
}
int yr_parser_reduce_operation(
yyscan_t yyscanner,
const char* op,
YR_EXPRESSION left_operand,
YR_EXPRESSION right_operand)
{
int expression_type;
YR_COMPILER* compiler = yyget_extra(yyscanner);
if ((left_operand.type == EXPRESSION_TYPE_INTEGER ||
left_operand.type == EXPRESSION_TYPE_FLOAT) &&
(right_operand.type == EXPRESSION_TYPE_INTEGER ||
right_operand.type == EXPRESSION_TYPE_FLOAT))
{
if (left_operand.type != right_operand.type)
{
// One operand is double and the other is integer,
// cast the integer to double
FAIL_ON_ERROR(yr_parser_emit_with_arg(
yyscanner,
OP_INT_TO_DBL,
(left_operand.type == EXPRESSION_TYPE_INTEGER) ? 2 : 1,
NULL,
NULL));
}
expression_type = EXPRESSION_TYPE_FLOAT;
if (left_operand.type == EXPRESSION_TYPE_INTEGER &&
right_operand.type == EXPRESSION_TYPE_INTEGER)
{
expression_type = EXPRESSION_TYPE_INTEGER;
}
FAIL_ON_ERROR(yr_parser_emit(
yyscanner, _yr_parser_operator_to_opcode(op, expression_type), NULL));
}
else if (
left_operand.type == EXPRESSION_TYPE_STRING &&
right_operand.type == EXPRESSION_TYPE_STRING)
{
int opcode = _yr_parser_operator_to_opcode(op, EXPRESSION_TYPE_STRING);
if (opcode != OP_ERROR)
{
FAIL_ON_ERROR(yr_parser_emit(yyscanner, opcode, NULL));
}
else
{
yr_compiler_set_error_extra_info_fmt(
compiler, "strings don't support \"%s\" operation", op);
return ERROR_WRONG_TYPE;
}
}
else
{
yr_compiler_set_error_extra_info(compiler, "type mismatch");
return ERROR_WRONG_TYPE;
}
return ERROR_SUCCESS;
}
int yr_parser_mark_nonfast(
yyscan_t yyscanner,
YR_STRING_SET string_set
) {
YR_COMPILER* compiler = yyget_extra(yyscanner);
YR_STRING_SET_ELEMENT* head = string_set.head;
while (head != NULL) {
YR_STRING* string_ptr = yr_arena_ref_to_ptr(compiler->arena, &head->element);
string_ptr->flags &= ~STRING_FLAGS_SINGLE_MATCH;
head = head->next;
}
return ERROR_SUCCESS;
}
|