From 4e63bb13eabe7e35e687bcac48e23677dfbd6033 Mon Sep 17 00:00:00 2001 From: Evgeniy Nikeytsev Date: Mon, 16 Jan 2023 19:09:09 +0300 Subject: [PATCH] Adding GC test to check if uncaught exception is correctly processed with moving GC Signed-off-by: Evgeniy Nikeytsev --- tests/runtime/common/gc/CMakeLists.txt | 35 +++++++++++++ .../gc/moving_gc_when_pending_exception.js | 29 +++++++++++ .../common/gc/pending_exception_tests.cpp | 51 +++++++++++++++++++ 3 files changed, 115 insertions(+) create mode 100644 tests/runtime/common/gc/moving_gc_when_pending_exception.js create mode 100644 tests/runtime/common/gc/pending_exception_tests.cpp diff --git a/tests/runtime/common/gc/CMakeLists.txt b/tests/runtime/common/gc/CMakeLists.txt index dde610e69..59f10c55c 100644 --- a/tests/runtime/common/gc/CMakeLists.txt +++ b/tests/runtime/common/gc/CMakeLists.txt @@ -94,6 +94,39 @@ function(panda_add_ecma_gc_test_aot) add_dependencies(gc_ecma_common_tests "${TEST_NAME}-aot") endfunction() +function(panda_add_ecma_jsnapi_gc_test) + set(prefix ARG) + set(singleValues FILE) + set(multiValues OPTIONS) + cmake_parse_arguments(${prefix} "" "${singleValues}" "${multiValues}" ${ARGN}) + + get_filename_component(TEST_NAME "${ARG_FILE}" NAME_WE) + get_filename_component(TEST_FILE "${ARG_FILE}" REALPATH) + set(BINARY_FILE "${CMAKE_CURRENT_BINARY_DIR}/${TEST_NAME}.abc") + + compile_file_ecma(FILE ${TEST_FILE} OPTIONS "--module" OUTPUT_FILE ${BINARY_FILE} WORKING_DIR ${CMAKE_CURRENT_BINARY_DIR}) + + add_custom_target(source_js_file_${TEST_NAME} + DEPENDS ${BINARY_FILE}) + + if (TARGET ecmascript_pending_exception_tests) + add_dependencies(ecmascript_pending_exception_tests source_js_file_${TEST_NAME}) + else() + panda_add_gtest( + NO_CORES + NAME ecmascript_pending_exception_tests + SOURCES + pending_exception_tests.cpp + LIBRARIES + arkruntime arkassembler + SANITIZERS + ${PANDA_SANITIZERS_LIST} + DEPS_TARGETS + source_js_file_${TEST_NAME} + ) + endif() +endfunction() + # Use --gc-trigger-type=debug-never to control GC in the test. # In the test we don't need the runtime triggers GC during allocation. panda_add_ecma_gc_test(FILE startGc.js OPTIONS "--gc-trigger-type=debug-never") @@ -107,3 +140,5 @@ panda_add_ecma_gc_test(FILE hclass_collected_before_object.js OPTIONS "--gc-type panda_add_ecma_gc_test(FILE write_prebarrier_primitive_value.js OPTIONS "--gc-type=g1-gc" "--gc-trigger-type=debug-never" "--g1-track-freed-objects=true") # Running tests in AOT configuration panda_add_ecma_gc_test_aot(FILE write_prebarrier_primitive_value.js OPTIONS "--gc-type=g1-gc" "--gc-trigger-type=debug-never" "--g1-track-freed-objects=true") +# Running tests via API calls +panda_add_ecma_jsnapi_gc_test(FILE moving_gc_when_pending_exception.js OPTIONS "--gc-type=g1-gc" "--gc-trigger-type=debug-never") diff --git a/tests/runtime/common/gc/moving_gc_when_pending_exception.js b/tests/runtime/common/gc/moving_gc_when_pending_exception.js new file mode 100644 index 000000000..46cb746ed --- /dev/null +++ b/tests/runtime/common/gc/moving_gc_when_pending_exception.js @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Call moving GC when we have pending exception. Check that + * exception is correctly processed + * (objects are wrapped with handles) + */ + +// create object of error class +let obj = new Object("foo"); + +// adding a couple Full GC to be run without waiting for its completion +// in order to have GC running during exception processing +startGC("full"); +startGC("full"); +throw obj; diff --git a/tests/runtime/common/gc/pending_exception_tests.cpp b/tests/runtime/common/gc/pending_exception_tests.cpp new file mode 100644 index 000000000..9834ed7e7 --- /dev/null +++ b/tests/runtime/common/gc/pending_exception_tests.cpp @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "plugins/ecmascript/tests/runtime/common/test_helper.h" + +namespace panda::test { +class PendingExceptionTests : public testing::TestWithParam { +public: + void SetUp() override + { + RuntimeOptions options; + options.SetLoadRuntimes({"ecmascript"}); + options.SetGcType("g1-gc"); + options.SetRunGcInPlace(false); + options.SetCompilerEnableJit(false); + options.SetGcTriggerType("debug-never"); + options.SetShouldLoadBootPandaFiles(false); + bool success = Runtime::Create(options); + ASSERT_TRUE(success) << "Cannot create Runtime"; + } + + void TearDown() override + { + bool success = Runtime::Destroy(); + ASSERT_TRUE(success) << "Cannot destroy Runtime"; + } +}; + +TEST_P(PendingExceptionTests, MovingGc) +{ + const std::string mainFunc = "_GLOBAL::func_main_0"; + const std::string fileName = "moving_gc_when_pending_exception.abc"; + auto ret2 = Runtime::GetCurrent()->ExecutePandaFile(fileName.c_str(), mainFunc.c_str(), {}); + ASSERT_TRUE(ret2.HasValue()); + ASSERT_EQ(ret2.Value(), 1); +} + +INSTANTIATE_TEST_SUITE_P(MovingGcTests, PendingExceptionTests, ::testing::Range(0, 8)); +} // namespace panda::test -- Gitee