From 2d6c8f5eb2fa66302523466b840514c2cef53dd7 Mon Sep 17 00:00:00 2001 From: xingshunxiang Date: Fri, 20 Jun 2025 15:40:49 +0800 Subject: [PATCH] Fix the SegV in callexpr check Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICGNVH?from=project-issue Description: Fix the SegV in callexpr check Reason: in some case the callexpr cause the endless loop Tests: ninja tests passed tests/tests-u-runner/runner.sh --ets-cts --show-progress --build-dir x64.release --processes=all passed tests/tests-u-runner/runner.sh --ets-func-tests --show-progress --build-dir x64.release --processes=all passed tests/tests-u-runner/runner.sh --astchecker --show-progress --build-dir x64.release --processes=all passed tests/tests-u-runner/runner.sh --ets-runtime --show-progress --build-dir x64.release --processes=all passed tests/tests-u-runner/runner.sh --parser --no-js --show-progress --build-dir x64.release --processes=all passed Signed-off-by: xingshunxiang --- ets2panda/checker/ETSAnalyzer.cpp | 15 ++++++++++- .../ast/compiler/ets/recursive_function.ets | 20 +++++++++++++++ .../ast/parser/ets/recursive_function.ets | 25 +++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 ets2panda/test/ast/compiler/ets/recursive_function.ets create mode 100644 ets2panda/test/ast/parser/ets/recursive_function.ets diff --git a/ets2panda/checker/ETSAnalyzer.cpp b/ets2panda/checker/ETSAnalyzer.cpp index 3aa4109c2e..c77a4836c3 100644 --- a/ets2panda/checker/ETSAnalyzer.cpp +++ b/ets2panda/checker/ETSAnalyzer.cpp @@ -1448,6 +1448,17 @@ static checker::SavedCheckerContext ReconstructOwnerClassContext(ETSChecker *che return SavedCheckerContext(checker, status, owner); } +static bool IsInSelfScope(checker::ETSChecker *const checker, ir::MethodDefinition *const methodDef) +{ + // Prevent endless check: function foo() {foo();} + auto scopeScript = checker->Scope()->Node(); + ir::ScriptFunction *currentScript = nullptr; + if (methodDef->AsMethodDefinition()->Value()->IsFunctionExpression()) { + currentScript = methodDef->AsMethodDefinition()->Value()->AsFunctionExpression()->Function(); + } + return currentScript != nullptr && scopeScript != nullptr && currentScript == scopeScript; +} + checker::Type *ETSAnalyzer::GetCallExpressionReturnType(ir::CallExpression *expr, checker::Type *calleeType) const { ETSChecker *checker = GetETSChecker(); @@ -1478,7 +1489,9 @@ checker::Type *ETSAnalyzer::GetCallExpressionReturnType(ir::CallExpression *expr ES2PANDA_ASSERT(methodDef != nullptr); } ES2PANDA_ASSERT(methodDef->IsMethodDefinition()); - methodDef->Check(checker); + if (!IsInSelfScope(checker, methodDef->AsMethodDefinition())) { + methodDef->Check(checker); + } if (!signature->Function()->HasBody()) { return signature->ReturnType(); diff --git a/ets2panda/test/ast/compiler/ets/recursive_function.ets b/ets2panda/test/ast/compiler/ets/recursive_function.ets new file mode 100644 index 0000000000..4228041c0b --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/recursive_function.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 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. + */ + +// this case is from fuzzer, it should be compile success +function foo() { + foo(); + return; +} diff --git a/ets2panda/test/ast/parser/ets/recursive_function.ets b/ets2panda/test/ast/parser/ets/recursive_function.ets new file mode 100644 index 0000000000..627824c452 --- /dev/null +++ b/ets2panda/test/ast/parser/ets/recursive_function.ets @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 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. + */ + +// this case is from fuzzer. +function foo() { + foo()/* @@ label */e/* @@ label2 */{ + return; +} + +/* @@@ label Error SyntaxError: Unexpected token 'e'. */ +/* @@@ label Error TypeError: Unresolved reference e */ +/* @@@ label2 Error SyntaxError: Unexpected token '{'. */ +/* @@? 26:1 Error SyntaxError: Expected '}', got 'end of stream'. */ -- Gitee