diff --git a/BUILD.gn b/BUILD.gn old mode 100644 new mode 100755 index 0a79626ff3ad76f3494c154a9a892e2681bedb2a..aa8c7c92f335929a8a1657684729b4c418903874 --- a/BUILD.gn +++ b/BUILD.gn @@ -311,6 +311,7 @@ if (!ark_standalone_build) { "$ark_root/assembler/tests:host_unittest", "$ark_root/bytecode_optimizer/tests:host_unittest", "$ark_root/compiler/tests:host_unittest", + "$ark_root/disassembler/tests:host_unittest", "$ark_root/libpandabase/tests:host_unittest", "$ark_root/libpandafile/tests:host_unittest", "$ark_root/libziparchive/tests:host_unittest", diff --git a/disassembler/accumulators.h b/disassembler/accumulators.h old mode 100644 new mode 100755 index 84c7fa042f83139fa145c81fb3e54013256d0cf3..e6bf113d1b0c3e72f4be7d9097a027a2f6c1c2f8 --- a/disassembler/accumulators.h +++ b/disassembler/accumulators.h @@ -33,6 +33,8 @@ struct MethodInfo { panda_file::LineNumberTable line_number_table; + panda_file::ColumnNumberTable column_number_table; + panda_file::LocalVariableTable local_variable_table; }; diff --git a/disassembler/disassembler.cpp b/disassembler/disassembler.cpp old mode 100644 new mode 100755 index 3c2d80c150065129495eb22728a423678a24b5b0..aae81d0c223bb05244512cb7897930c99890484b --- a/disassembler/disassembler.cpp +++ b/disassembler/disassembler.cpp @@ -1084,6 +1084,7 @@ void Disassembler::GetMethodInfo(const panda_file::File::EntityId &method_id, Me if (method_accessor.GetCodeId()) { ASSERT(debug_info_extractor_ != nullptr); method_info->line_number_table = debug_info_extractor_->GetLineNumberTable(method_id); + method_info->column_number_table = debug_info_extractor_->GetColumnNumberTable(method_id); method_info->local_variable_table = debug_info_extractor_->GetLocalVariableTable(method_id); // Add information about parameters into the table @@ -1511,6 +1512,7 @@ void Disassembler::Serialize(const pandasm::Function &method, std::ostream &os, if (print_method_info) { const MethodInfo &method_info = method_info_it->second; SerializeLineNumberTable(method_info.line_number_table, os); + SerializeColumnNumberTable(method_info.column_number_table, os); SerializeLocalVariableTable(method_info.local_variable_table, method, os); } @@ -1605,6 +1607,19 @@ void Disassembler::SerializeLineNumberTable(const panda_file::LineNumberTable &l } } +void Disassembler::SerializeColumnNumberTable(const panda_file::ColumnNumberTable &column_number_table, + std::ostream &os) const +{ + if (column_number_table.empty()) { + return; + } + + os << "\n# COLUMN_NUMBER_TABLE:\n"; + for (const auto &column_info : column_number_table) { + os << "#\tcolumn " << column_info.column << ": " << column_info.offset << "\n"; + } +} + void Disassembler::SerializeLocalVariableTable(const panda_file::LocalVariableTable &local_variable_table, const pandasm::Function &method, std::ostream &os) const { @@ -1773,4 +1788,15 @@ IdList Disassembler::GetInstructions(pandasm::Function *method, panda_file::File return unknown_external_methods; } +std::vector Disassembler::GetColumnNumber() +{ + std::vector columnNumber; + for (const auto &method_info : prog_info_.methods_info) { + for (const auto &column_number : method_info.second.column_number_table) { + columnNumber.push_back(column_number.column); + } + } + return columnNumber; +} + } // namespace panda::disasm diff --git a/disassembler/disassembler.h b/disassembler/disassembler.h old mode 100644 new mode 100755 index 8f3a3b50bcab9907867c6325161eabba547858fd..701ea83d83001b39264f85935c07135a855c3fea --- a/disassembler/disassembler.h +++ b/disassembler/disassembler.h @@ -76,6 +76,8 @@ public: return prog_info_; } + std::vector GetColumnNumber(); + private: void GetLiteralArrays(); void FillLiteralData(pandasm::LiteralArray *lit_array, const panda_file::LiteralDataAccessor::LiteralValue &value, @@ -133,6 +135,8 @@ private: void Serialize(const pandasm::Function::CatchBlock &catch_block, std::ostream &os) const; void Serialize(const pandasm::ItemMetadata &meta, const AnnotationList &ann_list, std::ostream &os) const; void SerializeLineNumberTable(const panda_file::LineNumberTable &line_number_table, std::ostream &os) const; + void SerializeColumnNumberTable(const panda_file::ColumnNumberTable &column_number_table, + std::ostream &os) const; void SerializeLocalVariableTable(const panda_file::LocalVariableTable &local_variable_table, const pandasm::Function &method, std::ostream &os) const; bool IsModuleLiteralOffset(const panda_file::File::EntityId &id) const; diff --git a/disassembler/tests/BUILD.gn b/disassembler/tests/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..16eeea5fa9586d88490ab1492986ba0a5e0c83b3 --- /dev/null +++ b/disassembler/tests/BUILD.gn @@ -0,0 +1,83 @@ +# 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. + +import("//arkcompiler/ets_frontend/es2panda/es2abc_config.gni") +import("//arkcompiler/runtime_core/ark_config.gni") +import("$ark_root/tests/test_helper.gni") + +disasm_test_configs = [ + "$ark_root:ark_config", + "$ark_root/libpandabase:arkbase_public_config", + "$ark_root/libpandafile:arkfile_public_config", + sdk_libc_secshared_config, +] + +disasm_test_deps = [ + "$ark_root/disassembler:arkdisassembler", + "$ark_root/libpandabase:libarkbase_static", + "$ark_root/libpandafile:libarkfile_static", +] + +disasm_test_js_files = [ + "column-number1", + "column-number2", + "column-number3", + "column-number4", + "column-number5", +] + +test_js_path = "//arkcompiler/runtime_core/disassembler/tests/js/" + +foreach(file, disasm_test_js_files) { + es2abc_gen_abc("gen_${file}_abc") { + test_js = "${test_js_path}${file}.js" + test_abc = "$target_out_dir/${file}.abc" + src_js = rebase_path(test_js) + dst_file = rebase_path(test_abc) + extra_args = [ + "--debug", + "--module", + ] + in_puts = [ test_js ] + out_puts = [ test_abc ] + } +} + +disasm_include_dirs = [ + "$ark_root/disassembler", + "$ark_root/assembler", + "$target_gen_dir", + "$target_gen_dir/../", + "$target_gen_dir/../../assembler", +] + +script_sources = [ "disassember_column_number_test.cpp" ] + +host_unittest_action("DisasmScriptTest") { + module_out_path = module_output_path + sources = script_sources + include_dirs = disasm_include_dirs + configs = disasm_test_configs + deps = disasm_test_deps + test_abc_dir = rebase_path(target_out_dir) + defines = [ "GRAPH_TEST_ABC_DIR=\"${test_abc_dir}/\"" ] + + foreach(file, disasm_test_js_files) { + deps += [ ":gen_${file}_abc" ] + } +} + +group("host_unittest") { + testonly = true + deps = [ ":DisasmScriptTestAction" ] +} diff --git a/disassembler/tests/disassember_column_number_test.cpp b/disassembler/tests/disassember_column_number_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cd871efe6df2f4fcda16003f049162a9fb2d90fc --- /dev/null +++ b/disassembler/tests/disassember_column_number_test.cpp @@ -0,0 +1,160 @@ +/* + * 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 +#include +#include "disassembler.h" + +using namespace testing::ext; + +namespace panda::disasm { +class DisasmTest : public testing::Test { +public: + static void SetUpTestCase(void) {}; + static void TearDownTestCase(void) {}; + void SetUp() {}; + void TearDown() {}; +}; + +/** +* @tc.name: disassembler_column_number_test_001 +* @tc.desc: Check abc file column number function. +* @tc.type: FUNC +* @tc.require: file path and name +*/ +HWTEST_F(DisasmTest, disassembler_column_number_test_001, TestSize.Level1) +{ + const std::string file_name = GRAPH_TEST_ABC_DIR "column-number1.abc"; + panda::disasm::Disassembler disasm {}; + disasm.Disassemble(file_name, false, false); + disasm.CollectInfo(); + // The known column number in the abc file + std::vector expectedColumnNumber = {10, 14, 10, 6, 2, 8, 4, 8, 4, 0}; + std::vector columnNumber = disasm.GetColumnNumber(); + EXPECT_TRUE(expectedColumnNumber.size() == columnNumber.size()); + bool res = true; + for (size_t i = 0; i < expectedColumnNumber.size(); ++i) { + if (expectedColumnNumber[i] != columnNumber[i]) { + res = false; + break; + } + } + EXPECT_TRUE(res); +} + +/** +* @tc.name: disassembler_column_number_test_002 +* @tc.desc: Check abc file column number function. +* @tc.type: FUNC +* @tc.require: file path and name +*/ +HWTEST_F(DisasmTest, disassembler_column_number_test_002, TestSize.Level1) +{ + const std::string file_name = GRAPH_TEST_ABC_DIR "column-number2.abc"; + panda::disasm::Disassembler disasm {}; + disasm.Disassemble(file_name, false, false); + disasm.CollectInfo(); + // The known column number in the abc file + std::vector expectedColumnNumber = {10, 6, 10, 6, 10, 6, 2, 0}; + std::vector columnNumber = disasm.GetColumnNumber(); + EXPECT_TRUE(expectedColumnNumber.size() == columnNumber.size()); + bool res = true; + for (size_t i = 0; i < expectedColumnNumber.size(); ++i) { + if (expectedColumnNumber[i] != columnNumber[i]) { + res = false; + break; + } + } + EXPECT_TRUE(res); +} + +/** +* @tc.name: disassembler_column_number_test_003 +* @tc.desc: Check abc file column number function. +* @tc.type: FUNC +* @tc.require: file path and name +*/ +HWTEST_F(DisasmTest, disassembler_column_number_test_003, TestSize.Level1) +{ + const std::string file_name = GRAPH_TEST_ABC_DIR "column-number3.abc"; + panda::disasm::Disassembler disasm {}; + disasm.Disassemble(file_name, false, false); + disasm.CollectInfo(); + // The known column number in the abc file + std::vector expectedColumnNumber = {13, 4, 16, 4, 15, 4, 14, 6, 14, 6, 0}; + std::vector columnNumber = disasm.GetColumnNumber(); + EXPECT_TRUE(expectedColumnNumber.size() == columnNumber.size()); + bool res = true; + for (size_t i = 0; i < expectedColumnNumber.size(); ++i) { + if (expectedColumnNumber[i] != columnNumber[i]) { + res = false; + break; + } + } + EXPECT_TRUE(res); +} + +/** +* @tc.name: disassembler_column_number_test_004 +* @tc.desc: Check abc file column number function. +* @tc.type: FUNC +* @tc.require: file path and name +*/ +HWTEST_F(DisasmTest, disassembler_column_number_test_004, TestSize.Level1) +{ + const std::string file_name = GRAPH_TEST_ABC_DIR "column-number4.abc"; + panda::disasm::Disassembler disasm {}; + disasm.Disassemble(file_name, false, false); + disasm.CollectInfo(); + std::vector expectedColumnNumber = {10, 14, 10, 6, 10, 6, 10, 14, 10, 6, 9, 2, 2, 4, 8, 4, 8, 4, 0}; + std::vector columnNumber = disasm.GetColumnNumber(); + EXPECT_TRUE(expectedColumnNumber.size() == columnNumber.size()); + bool res = true; + for (size_t i = 0; i < expectedColumnNumber.size(); ++i) { + if (expectedColumnNumber[i] != columnNumber[i]) { + res = false; + break; + } + } + EXPECT_TRUE(res); +} + +/** +* @tc.name: disassembler_column_number_test_005 +* @tc.desc: Check abc file column number function. +* @tc.type: FUNC +* @tc.require: file path and name +*/ +HWTEST_F(DisasmTest, disassembler_column_number_test_005, TestSize.Level1) +{ + const std::string file_name = GRAPH_TEST_ABC_DIR "column-number5.abc"; + panda::disasm::Disassembler disasm {}; + disasm.Disassemble(file_name, false, false); + disasm.CollectInfo(); + // The known column number in the abc file + std::vector expectedColumnNumber = {13, 4, 16, 4, 15, 4, 10, 6, 10, 6, 10, 14, 10, 6, + 10, 14, 10, 6, 9, 2, 2, 14, 6, 14, 6, 8, 4, 0}; + std::vector columnNumber = disasm.GetColumnNumber(); + EXPECT_TRUE(expectedColumnNumber.size() == columnNumber.size()); + bool res = true; + for (size_t i = 0; i < expectedColumnNumber.size(); ++i) { + if (expectedColumnNumber[i] != columnNumber[i]) { + res = false; + break; + } + } + EXPECT_TRUE(res); +} +} diff --git a/disassembler/tests/js/column-number1.js b/disassembler/tests/js/column-number1.js new file mode 100644 index 0000000000000000000000000000000000000000..03e49b363d343c0182887c68652100860e2eff2b --- /dev/null +++ b/disassembler/tests/js/column-number1.js @@ -0,0 +1,22 @@ +/* + * 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. + */ + + +let a = 0; +let b = 1; + +function funcA() { + let m = a + b; +} \ No newline at end of file diff --git a/disassembler/tests/js/column-number2.js b/disassembler/tests/js/column-number2.js new file mode 100644 index 0000000000000000000000000000000000000000..736b757c0afa689447e578197d4e8e7046d7a677 --- /dev/null +++ b/disassembler/tests/js/column-number2.js @@ -0,0 +1,21 @@ +/* + * 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. + */ + + +function funcB() { + let a = 0; + let b = 1; + let c = 0; +} \ No newline at end of file diff --git a/disassembler/tests/js/column-number3.js b/disassembler/tests/js/column-number3.js new file mode 100644 index 0000000000000000000000000000000000000000..0a84639359189cef48ed0569c3ce7f3c934a6c5c --- /dev/null +++ b/disassembler/tests/js/column-number3.js @@ -0,0 +1,22 @@ +/* + * 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. + */ + + +class Student { + constructor() { + this.name = 'Tom'; + this.age = 1; + } +} \ No newline at end of file diff --git a/disassembler/tests/js/column-number4.js b/disassembler/tests/js/column-number4.js new file mode 100644 index 0000000000000000000000000000000000000000..c07be1b3680a84c37b273e9e764ea3b4f307f191 --- /dev/null +++ b/disassembler/tests/js/column-number4.js @@ -0,0 +1,31 @@ +/* + * 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. + */ + + +let a = 0; +let b = 1; + +function funcH() { + let d = a + b; + let e = 3; + let m = d + e; + return m; +} + +function funcI() { + function funcJ() { + funcH(); + } +} \ No newline at end of file diff --git a/disassembler/tests/js/column-number5.js b/disassembler/tests/js/column-number5.js new file mode 100644 index 0000000000000000000000000000000000000000..c8a9cf2a94107f45e873752848b3cf1254e23102 --- /dev/null +++ b/disassembler/tests/js/column-number5.js @@ -0,0 +1,36 @@ +/* + * 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. + */ + + +class Student { + constructor() { + this.name = 'Tom'; + this.age = 1; + } +} + +let c = 9; + +function foo1() { + let a = 2; + let b = 1; + let m = a + b; + let n = m + c; + return n; +} + +function foo2() { + foo1(); +}