From 3e548401fd062797fed39ba8abe51e35dc5a75ff Mon Sep 17 00:00:00 2001 From: Robert Fancsik Date: Tue, 19 Jan 2021 16:20:23 +0100 Subject: [PATCH] Invalid regexp literals should throw syntax error in ES11 (#4506) JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik frobert@inf.u-szeged.hu --- jerry-core/include/jerryscript-snapshot.h | 2 +- jerry-core/parser/js/byte-code.c | 2 +- jerry-core/parser/js/byte-code.h | 2 - jerry-core/parser/js/js-lexer.c | 84 +--- jerry-core/parser/js/js-parser-expr.c | 9 - jerry-core/parser/js/js-parser.c | 10 +- jerry-core/vm/vm.c | 8 - jerry-core/vm/vm.h | 2 - .../es.next/regression-test-issue-2058.js | 2 +- .../es.next/regression-test-issue-4408.js | 4 +- tests/test262-esnext-excludelist.xml | 423 ------------------ 11 files changed, 12 insertions(+), 536 deletions(-) diff --git a/jerry-core/include/jerryscript-snapshot.h b/jerry-core/include/jerryscript-snapshot.h index a0db21ab0..831bb4922 100644 --- a/jerry-core/include/jerryscript-snapshot.h +++ b/jerry-core/include/jerryscript-snapshot.h @@ -30,7 +30,7 @@ extern "C" /** * Jerry snapshot format version. */ -#define JERRY_SNAPSHOT_VERSION (62u) +#define JERRY_SNAPSHOT_VERSION (63u) /** * Flags for jerry_generate_snapshot and jerry_generate_function_snapshot. diff --git a/jerry-core/parser/js/byte-code.c b/jerry-core/parser/js/byte-code.c index 101be4e1b..ef63c3d5b 100644 --- a/jerry-core/parser/js/byte-code.c +++ b/jerry-core/parser/js/byte-code.c @@ -27,7 +27,7 @@ JERRY_STATIC_ASSERT ((sizeof (cbc_uint16_arguments_t) % sizeof (jmem_cpointer_t) */ JERRY_STATIC_ASSERT (CBC_END == 238, number_of_cbc_opcodes_changed); -JERRY_STATIC_ASSERT (CBC_EXT_END == 149, +JERRY_STATIC_ASSERT (CBC_EXT_END == 148, number_of_cbc_ext_opcodes_changed); #if ENABLED (JERRY_PARSER) || ENABLED (JERRY_PARSER_DUMP_BYTE_CODE) diff --git a/jerry-core/parser/js/byte-code.h b/jerry-core/parser/js/byte-code.h index dfe7a2922..91e2489fa 100644 --- a/jerry-core/parser/js/byte-code.h +++ b/jerry-core/parser/js/byte-code.h @@ -610,8 +610,6 @@ VM_OC_LINE) \ CBC_OPCODE (CBC_EXT_THROW_REFERENCE_ERROR, CBC_NO_FLAG, 1, \ VM_OC_THROW_REFERENCE_ERROR) \ - CBC_OPCODE (CBC_EXT_THROW_SYNTAX_ERROR, CBC_HAS_LITERAL_ARG, 1, \ - VM_OC_THROW_SYNTAX_ERROR | VM_OC_GET_LITERAL) \ CBC_OPCODE (CBC_EXT_THROW_ASSIGN_CONST_ERROR, CBC_NO_FLAG, 0, \ VM_OC_THROW_CONST_ERROR) \ CBC_OPCODE (CBC_EXT_REQUIRE_OBJECT_COERCIBLE, CBC_NO_FLAG, 0, \ diff --git a/jerry-core/parser/js/js-lexer.c b/jerry-core/parser/js/js-lexer.c index b64ba70ea..2e6f90846 100644 --- a/jerry-core/parser/js/js-lexer.c +++ b/jerry-core/parser/js/js-lexer.c @@ -95,42 +95,6 @@ lexer_hex_to_code_point (const uint8_t *source_p, /**< current source position * #if ENABLED (JERRY_ESNEXT) -/** - * Find a string literal in the literal pool matching with the given buffer's content - * - * @return PARSER_INVALID_LITERAL_INDEX - if the literal is not present in the literal pool - * literal's index in the pool - otherwise - */ -static uint16_t -parser_find_string_literal (parser_context_t *context_p, /**< context */ - lexer_literal_t **out_literal_p, /**< [out] found literal */ - uint8_t *buffer_p, /**< character buffer */ - lit_utf8_size_t size) /**< buffer's size */ -{ - JERRY_ASSERT (out_literal_p != NULL); - JERRY_ASSERT (buffer_p != NULL); - - uint16_t literal_index = 0; - lexer_literal_t *literal_p; - parser_list_iterator_t literal_iterator; - parser_list_iterator_init (&context_p->literal_pool, &literal_iterator); - - while ((literal_p = (lexer_literal_t *) parser_list_iterator_next (&literal_iterator)) != NULL) - { - if (literal_p->type == LEXER_STRING_LITERAL - && literal_p->prop.length == size - && memcmp (literal_p->u.char_p, buffer_p, size) == 0) - { - *out_literal_p = literal_p; - return literal_index; - } - - literal_index++; - } - - return PARSER_INVALID_LITERAL_INDEX; -} /* parser_find_string_literal */ - /** * Parse hexadecimal character sequence enclosed in braces * @@ -3104,56 +3068,14 @@ lexer_construct_regexp_object (parser_context_t *context_p, /**< context */ re_compiled_code_t *re_bytecode_p = re_compile_bytecode (pattern_str_p, current_flags); ecma_deref_ecma_string (pattern_str_p); - lexer_literal_t *literal_p = NULL; - uint8_t literal_type = LEXER_REGEXP_LITERAL; - if (JERRY_UNLIKELY (re_bytecode_p == NULL)) { -#if ENABLED (JERRY_ESNEXT) - ecma_value_t error = jcontext_take_exception (); - ecma_property_t *prop_p = ecma_find_named_property (ecma_get_object_from_value (error), - ecma_get_magic_string (LIT_MAGIC_STRING_MESSAGE)); - const char default_msg[] = "Invalid regular expression"; - lit_utf8_byte_t *buffer_p = (lit_utf8_byte_t *) default_msg; - lit_utf8_size_t size = sizeof (buffer_p) - 1; - - if (prop_p != NULL) - { - ecma_string_t *message_p = ecma_get_string_from_value (ECMA_PROPERTY_VALUE_PTR (prop_p)->value); - JERRY_ASSERT (!ECMA_IS_DIRECT_STRING (message_p)); - JERRY_ASSERT (ECMA_STRING_GET_CONTAINER (message_p) == ECMA_STRING_CONTAINER_HEAP_ASCII_STRING); - buffer_p = ECMA_ASCII_STRING_GET_BUFFER (message_p); - size = ECMA_ASCII_STRING_GET_SIZE (message_p); - } - - uint16_t literal_index = parser_find_string_literal (context_p, &literal_p, buffer_p, size); - - if (literal_index != PARSER_INVALID_LITERAL_INDEX) - { - ecma_free_value (error); - context_p->lit_object.literal_p = literal_p; - context_p->lit_object.index = literal_index; - return; - } - - literal_p = (lexer_literal_t *) parser_list_append (context_p, &context_p->literal_pool); - literal_p->u.char_p = (uint8_t *) jmem_heap_alloc_block (size); - memcpy ((uint8_t *) literal_p->u.char_p, buffer_p, size); - literal_type = LEXER_STRING_LITERAL; - length = size; - - ecma_free_value (error); -#else /* !ENABLED (JERRY_ESNEXT) */ parser_raise_error (context_p, PARSER_ERR_INVALID_REGEXP); -#endif /* ENABLED (JERRY_ESNEXT) */ - } - else - { - literal_p = (lexer_literal_t *) parser_list_append (context_p, &context_p->literal_pool); - literal_p->u.bytecode_p = (ecma_compiled_code_t *) re_bytecode_p; } - literal_p->type = literal_type; + lexer_literal_t *literal_p = (lexer_literal_t *) parser_list_append (context_p, &context_p->literal_pool); + literal_p->u.bytecode_p = (ecma_compiled_code_t *) re_bytecode_p; + literal_p->type = LEXER_REGEXP_LITERAL; literal_p->prop.length = (prop_length_t) length; literal_p->status_flags = 0; diff --git a/jerry-core/parser/js/js-parser-expr.c b/jerry-core/parser/js/js-parser-expr.c index 3f40c1205..7f5f9f747 100644 --- a/jerry-core/parser/js/js-parser-expr.c +++ b/jerry-core/parser/js/js-parser-expr.c @@ -2177,15 +2177,6 @@ parser_parse_unary_expression (parser_context_t *context_p, /**< context */ case LEXER_ASSIGN_DIVIDE: { lexer_construct_regexp_object (context_p, false); - -#if ENABLED (JERRY_ESNEXT) - if (JERRY_UNLIKELY (context_p->lit_object.literal_p->type == LEXER_STRING_LITERAL)) - { - parser_emit_cbc_ext_literal (context_p, CBC_EXT_THROW_SYNTAX_ERROR, context_p->lit_object.index); - break; - } -#endif /* ENABLED (JERRY_ESNEXT) */ - uint16_t literal_index = (uint16_t) (context_p->literal_count - 1); if (context_p->last_cbc_opcode == CBC_PUSH_LITERAL) diff --git a/jerry-core/parser/js/js-parser.c b/jerry-core/parser/js/js-parser.c index a64a37aa0..94b26e3f5 100644 --- a/jerry-core/parser/js/js-parser.c +++ b/jerry-core/parser/js/js-parser.c @@ -2832,7 +2832,6 @@ parser_parse_script (const uint8_t *arg_list_p, /**< function argument list */ #if ENABLED (JERRY_ERROR_MESSAGES) ecma_string_t *err_str_p; -#if !ENABLED (JERRY_ESNEXT) if (parser_error.error == PARSER_ERR_INVALID_REGEXP) { ecma_value_t error = jcontext_take_exception (); @@ -2844,7 +2843,6 @@ parser_parse_script (const uint8_t *arg_list_p, /**< function argument list */ ecma_ref_ecma_string (err_str_p); } else -#endif /* !ENABLED (JERRY_ESNEXT) */ { const lit_utf8_byte_t *err_bytes_p = (const lit_utf8_byte_t *) parser_error_to_string (parser_error.error); lit_utf8_size_t err_bytes_size = lit_zt_utf8_string_size (err_bytes_p); @@ -2865,12 +2863,12 @@ parser_parse_script (const uint8_t *arg_list_p, /**< function argument list */ ecma_free_value (line_str_val); ecma_deref_ecma_string (err_str_p); #else /* !ENABLED (JERRY_ERROR_MESSAGES) */ -#if !ENABLED (JERRY_ESNEXT) - if (parser_error.error != PARSER_ERR_INVALID_REGEXP) -#endif /* !ENABLED (JERRY_ESNEXT) */ + if (parser_error.error == PARSER_ERR_INVALID_REGEXP) { - ecma_raise_syntax_error (""); + jcontext_release_exception (); } + + ecma_raise_syntax_error (""); #endif /* ENABLED (JERRY_ERROR_MESSAGES) */ return NULL; diff --git a/jerry-core/vm/vm.c b/jerry-core/vm/vm.c index 77f722ca3..9a3820553 100644 --- a/jerry-core/vm/vm.c +++ b/jerry-core/vm/vm.c @@ -1740,14 +1740,6 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */ result = ecma_raise_type_error (ECMA_ERR_MSG ("Constant bindings cannot be reassigned.")); goto error; } - case VM_OC_THROW_SYNTAX_ERROR: - { - ecma_string_t *msg_p = ecma_get_string_from_value (left_value); - ecma_object_t *error_obj_p = ecma_new_standard_error (ECMA_ERROR_SYNTAX, msg_p); - jcontext_raise_exception (ecma_make_object_value (error_obj_p)); - result = ECMA_VALUE_ERROR; - goto error; - } case VM_OC_COPY_TO_GLOBAL: { uint32_t literal_index; diff --git a/jerry-core/vm/vm.h b/jerry-core/vm/vm.h index 4a519e5bf..b567789df 100644 --- a/jerry-core/vm/vm.h +++ b/jerry-core/vm/vm.h @@ -243,7 +243,6 @@ typedef enum VM_OC_ASSIGN_LET_CONST, /**< assign values to let/const declarations */ VM_OC_INIT_BINDING, /**< create and intialize a binding */ VM_OC_THROW_CONST_ERROR, /**< throw invalid assignment to const variable error */ - VM_OC_THROW_SYNTAX_ERROR, /**< throw syntax error */ VM_OC_COPY_TO_GLOBAL, /**< copy value to global lex env */ VM_OC_COPY_FROM_ARG, /**< copy value from arg lex env */ VM_OC_CLONE_CONTEXT, /**< clone lexical environment with let/const declarations */ @@ -331,7 +330,6 @@ typedef enum VM_OC_ASSIGN_LET_CONST = VM_OC_NONE, /**< assign values to let/const declarations */ VM_OC_INIT_BINDING = VM_OC_NONE, /**< create and intialize a binding */ VM_OC_THROW_CONST_ERROR = VM_OC_NONE, /**< throw invalid assignment to const variable error */ - VM_OC_THROW_SYNTAX_ERROR = VM_OC_NONE, /**< throw syntax error */ VM_OC_COPY_TO_GLOBAL = VM_OC_NONE, /**< copy value to global lex env */ VM_OC_COPY_FROM_ARG = VM_OC_NONE, /**< copy value from arg lex env */ VM_OC_CLONE_CONTEXT = VM_OC_NONE, /**< clone lexical environment with let/const declarations */ diff --git a/tests/jerry/es.next/regression-test-issue-2058.js b/tests/jerry/es.next/regression-test-issue-2058.js index 7906b6066..7074974f1 100644 --- a/tests/jerry/es.next/regression-test-issue-2058.js +++ b/tests/jerry/es.next/regression-test-issue-2058.js @@ -13,7 +13,7 @@ // limitations under the License. try { - /?:/ + eval('/?:/'); assert(false); } catch (e) { assert(e instanceof SyntaxError); diff --git a/tests/jerry/es.next/regression-test-issue-4408.js b/tests/jerry/es.next/regression-test-issue-4408.js index 4af6e919b..4951667d6 100644 --- a/tests/jerry/es.next/regression-test-issue-4408.js +++ b/tests/jerry/es.next/regression-test-issue-4408.js @@ -13,13 +13,13 @@ // limitations under the License. try { - /(?<=^abc)def/; + eval('/(?<=^abc)def/'); } catch(e) { assert(e instanceof SyntaxError); } try { - /(?a)/; + eval('/(?a)/;') } catch(e) { assert(e instanceof SyntaxError); } diff --git a/tests/test262-esnext-excludelist.xml b/tests/test262-esnext-excludelist.xml index bc584442a..6a153dbe8 100644 --- a/tests/test262-esnext-excludelist.xml +++ b/tests/test262-esnext-excludelist.xml @@ -388,7 +388,6 @@ - @@ -667,232 +666,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -6575,84 +6348,12 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -7057,130 +6758,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -