1/* Copyright JS Foundation and other contributors, http://js.foundation 2 * 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16#include "config.h" 17#include "jerryscript.h" 18#include "jerryscript-port.h" 19#include "jerryscript-port-default.h" 20#include "test-common.h" 21#include <gtest/gtest.h> 22 23static jerry_value_t 24vm_exec_stop_callback (void *user_p) 25{ 26 int *int_p = (int *) user_p; 27 28 if (*int_p > 0) 29 { 30 (*int_p)--; 31 32 return jerry_create_undefined (); 33 } 34 35 return jerry_create_string ((const jerry_char_t *) "Abort script"); 36} /* vm_exec_stop_callback */ 37 38class ExecStopTest : public testing::Test{ 39public: 40 static void SetUpTestCase() 41 { 42 GTEST_LOG_(INFO) << "ExecStopTest SetUpTestCase"; 43 } 44 45 static void TearDownTestCase() 46 { 47 GTEST_LOG_(INFO) << "ExecStopTest TearDownTestCase"; 48 } 49 50 void SetUp() override {} 51 void TearDown() override {} 52 53}; 54 55static constexpr size_t JERRY_SCRIPT_MEM_SIZE = 50 * 1024 * 1024; 56static void* context_alloc_fn(size_t size, void* cb_data) 57{ 58 (void)cb_data; 59 size_t newSize = size > JERRY_SCRIPT_MEM_SIZE ? JERRY_SCRIPT_MEM_SIZE : size; 60 return malloc(newSize); 61} 62HWTEST_F(ExecStopTest, Test001, testing::ext::TestSize.Level1) 63{ 64 TEST_INIT (); 65 66 /* Test stopping an infinite loop. */ 67 if (!jerry_is_feature_enabled (JERRY_FEATURE_VM_EXEC_STOP)) 68 { 69 jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Exec Stop is disabled!\n"); 70 // jerry_cleanup (); 71 } 72 else{ 73 jerry_context_t *ctx_p = jerry_create_context (1024, context_alloc_fn, NULL); 74 jerry_port_default_set_current_context (ctx_p); 75 jerry_init (JERRY_INIT_EMPTY); 76 77 int countdown = 6; 78 jerry_set_vm_exec_stop_callback (vm_exec_stop_callback, &countdown, 16); 79 80 const jerry_char_t inf_loop_code_src1[] = "while(true) {}"; 81 jerry_value_t parsed_code_val = jerry_parse (NULL, 82 0, 83 inf_loop_code_src1, 84 sizeof (inf_loop_code_src1) - 1, 85 JERRY_PARSE_NO_OPTS); 86 87 TEST_ASSERT (!jerry_value_is_error (parsed_code_val)); 88 jerry_value_t res = jerry_run (parsed_code_val); 89 TEST_ASSERT (countdown == 0); 90 91 TEST_ASSERT (jerry_value_is_error (res)); 92 93 jerry_release_value (res); 94 jerry_release_value (parsed_code_val); 95 96 /* A more complex example. Although the callback error is captured 97 * by the catch block, it is automatically thrown again. */ 98 99 /* We keep the callback function, only the countdown is reset. */ 100 countdown = 6; 101 102 const jerry_char_t inf_loop_code_src2[] = TEST_STRING_LITERAL ( 103 "function f() { while (true) ; }\n" 104 "try { f(); } catch(e) {}" 105 ); 106 107 parsed_code_val = jerry_parse (NULL, 108 0, 109 inf_loop_code_src2, 110 sizeof (inf_loop_code_src2) - 1, 111 JERRY_PARSE_NO_OPTS); 112 113 TEST_ASSERT (!jerry_value_is_error (parsed_code_val)); 114 res = jerry_run (parsed_code_val); 115 TEST_ASSERT (countdown == 0); 116 117 /* The result must have an error flag which proves that 118 * the error is thrown again inside the catch block. */ 119 TEST_ASSERT (jerry_value_is_error (res)); 120 121 jerry_release_value (res); 122 jerry_release_value (parsed_code_val); 123 124 jerry_cleanup (); 125 free (ctx_p); 126 } 127} 128