From 2782c763e78bf53616746ff8d88129410a28d935 Mon Sep 17 00:00:00 2001 From: Gongyuhang Date: Tue, 14 Jun 2022 16:13:59 +0800 Subject: [PATCH] Desciption: Deal with the "use after free" error occurs while debugging ark_js_vm executable on windows. Details: Enclose the part where the LocalScope object should take effect with a pair of braces. Thus, the destructor of the LocalScope object will be called at the right brace which is above the JSNApi::DestroyJSVM() function. Issue: https://gitee.com/openharmony/ark_js_runtime/issues/I5C7XG Signed-off-by: Gongyuhang --- ecmascript/compiler/aot_compiler.cpp | 43 +++++++++++++++------------- ecmascript/js_vm/main.cpp | 20 +++++++------ 2 files changed, 34 insertions(+), 29 deletions(-) diff --git a/ecmascript/compiler/aot_compiler.cpp b/ecmascript/compiler/aot_compiler.cpp index 10a6ba63..a45c64b2 100644 --- a/ecmascript/compiler/aot_compiler.cpp +++ b/ecmascript/compiler/aot_compiler.cpp @@ -110,29 +110,32 @@ int Main(const int argc, const char **argv) return -1; } - LocalScope scope(vm); - std::string entry = entrypoint.GetValue(); - arg_list_t pandaFileNames = files.GetValue(); - std::string triple = runtimeOptions.GetTargetTriple(); - std::string outputFileName = runtimeOptions.GetAOTOutputFile(); - size_t optLevel = runtimeOptions.GetOptLevel(); - BytecodeStubCSigns::Initialize(); - CommonStubCSigns::Initialize(); - RuntimeStubCSigns::Initialize(); + { + LocalScope scope(vm); + std::string entry = entrypoint.GetValue(); + arg_list_t pandaFileNames = files.GetValue(); + std::string triple = runtimeOptions.GetTargetTriple(); + std::string outputFileName = runtimeOptions.GetAOTOutputFile(); + size_t optLevel = runtimeOptions.GetOptLevel(); + BytecodeStubCSigns::Initialize(); + CommonStubCSigns::Initialize(); + RuntimeStubCSigns::Initialize(); - std::string logMethods = vm->GetJSOptions().GetlogCompiledMethods(); - AotLog log(logMethods); - AOTFileGenerator generator(&log, vm); - PassManager passManager(vm, entry, triple, optLevel, &log); - for (const auto &fileName : pandaFileNames) { - COMPILER_LOG(INFO) << "AOT start to execute ark file: " << fileName; - if (passManager.Compile(fileName, generator) == false) { - ret = false; - break; + std::string logMethods = vm->GetJSOptions().GetlogCompiledMethods(); + AotLog log(logMethods); + AOTFileGenerator generator(&log, vm); + PassManager passManager(vm, entry, triple, optLevel, &log); + for (const auto &fileName : pandaFileNames) { + COMPILER_LOG(INFO) << "AOT start to execute ark file: " << fileName; + if (passManager.Compile(fileName, generator) == false) { + ret = false; + break; + } } + generator.SaveAOTFile(outputFileName); + generator.GenerateSnapshotFile(); } - generator.SaveAOTFile(outputFileName); - generator.GenerateSnapshotFile(); + JSNApi::DestroyJSVM(vm); paParser.DisableTail(); return ret ? 0 : -1; diff --git a/ecmascript/js_vm/main.cpp b/ecmascript/js_vm/main.cpp index c39560aa..5b33dd1e 100644 --- a/ecmascript/js_vm/main.cpp +++ b/ecmascript/js_vm/main.cpp @@ -109,16 +109,18 @@ int Main(const int argc, const char **argv) return -1; } - LocalScope scope(vm); - std::string entry = entrypoint.GetValue(); + { + LocalScope scope(vm); + std::string entry = entrypoint.GetValue(); - arg_list_t fileNames = files.GetValue(); - for (const auto &fileName : fileNames) { - auto res = JSNApi::Execute(vm, fileName, entry); - if (!res) { - std::cerr << "Cannot execute panda file '" << fileName << "' with entry '" << entry << "'" << std::endl; - ret = false; - break; + arg_list_t fileNames = files.GetValue(); + for (const auto &fileName : fileNames) { + auto res = JSNApi::Execute(vm, fileName, entry); + if (!res) { + std::cerr << "Cannot execute panda file '" << fileName << "' with entry '" << entry << "'" << std::endl; + ret = false; + break; + } } }