1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
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 "symbolTable.h"
17
18 #include <fstream>
19 #include <iostream>
20
21 namespace panda::es2panda::util {
22 const std::string SymbolTable::FIRST_LEVEL_SEPERATOR = "|";
23 const std::string SymbolTable::SECOND_LEVEL_SEPERATOR = ";";
24 const size_t FUNCTION_ITEM_NUMBER = 3;
25 const size_t MODULE_ITEM_NUMBER = 1;
26
Initialize()27 bool SymbolTable::Initialize()
28 {
29 if (!symbolTable_.empty() && !ReadSymbolTable(symbolTable_)) {
30 std::cerr << "Failed to read symbol table: " << symbolTable_ << ". Stop generating patch" << std::endl;
31 return false;
32 }
33
34 if (!dumpSymbolTable_.empty()) {
35 std::fstream fs;
36 fs.open(panda::os::file::File::GetExtendedFilePath(dumpSymbolTable_),
37 std::ios_base::out | std::ios_base::trunc);
38 if (!fs.is_open()) {
39 std::cerr << "Failed to create output symbol table: " << dumpSymbolTable_ << std::endl;
40 return false;
41 }
42 fs.close();
43 }
44
45 return true;
46 }
47
ReadSymbolTable(const std::string & symbolTable)48 bool SymbolTable::ReadSymbolTable(const std::string &symbolTable)
49 {
50 std::ifstream ifs;
51 std::string line;
52 ifs.open(panda::os::file::File::GetExtendedFilePath(symbolTable));
53 if (!ifs.is_open()) {
54 std::cerr << "Failed to open symbol table: " << symbolTable << std::endl;
55 return false;
56 }
57
58 while (std::getline(ifs, line)) {
59 auto itemList = GetStringItems(line, FIRST_LEVEL_SEPERATOR);
60
61 if (itemList.size() == FUNCTION_ITEM_NUMBER) {
62 // read function info
63 struct OriginFunctionInfo info(&allocator_);
64 auto funcItems = GetStringItems(itemList[0], SECOND_LEVEL_SEPERATOR);
65 auto classItems = GetStringItems(itemList[1], SECOND_LEVEL_SEPERATOR);
66 auto lexItems = GetStringItems(itemList[2], SECOND_LEVEL_SEPERATOR);
67
68 info.funcName = funcItems[0];
69 info.funcInternalName = funcItems[1];
70 info.funcHash = funcItems[2];
71 for (size_t i = 0; i < classItems.size(); i = i + 2) {
72 info.classHash.insert(std::pair<std::string, std::string>(classItems[i], classItems[i + 1]));
73 }
74 for (size_t i = 0; i < lexItems.size(); i = i + 3) {
75 auto name = std::string(lexItems[i]);
76 auto slot = std::atoi(std::string(lexItems[i + 1]).c_str());
77 auto type = std::atoi(std::string(lexItems[i + 2]).c_str());
78 info.lexenv.insert({slot, std::pair<std::string, int>(name, type)});
79 }
80
81 originFunctionInfo_.insert(std::pair<std::string, OriginFunctionInfo>(info.funcInternalName, info));
82 } else if (itemList.size() == MODULE_ITEM_NUMBER) {
83 // read module info
84 auto moduleItems = GetStringItems(itemList[0], SECOND_LEVEL_SEPERATOR);
85 originModuleInfo_.insert(std::pair<std::string, std::string>(moduleItems[0], moduleItems[1]));
86 } else {
87 std::cerr << "Failed to read symbol table: Unrecognized format" << std::endl;
88 }
89 }
90 return true;
91 }
92
WriteSymbolTable(const std::string & content)93 void SymbolTable::WriteSymbolTable(const std::string &content)
94 {
95 std::lock_guard<std::mutex> lock(m_);
96 std::fstream fs;
97 fs.open(panda::os::file::File::GetExtendedFilePath(dumpSymbolTable_),
98 std::ios_base::app | std::ios_base::in);
99 if (fs.is_open()) {
100 fs << content;
101 fs.close();
102 }
103 }
104
GetStringItems(std::string_view input,const std::string & separator)105 std::vector<std::string_view> SymbolTable::GetStringItems(std::string_view input, const std::string &separator)
106 {
107 std::vector<std::string_view> items;
108 size_t curPos = 0;
109 size_t lastPos = 0;
110
111 while ((curPos = input.find(separator, lastPos)) != std::string_view::npos) {
112 auto token = input.substr(lastPos, curPos - lastPos);
113 if (!token.empty()) {
114 items.push_back(token);
115 }
116 lastPos = curPos + separator.size();
117 }
118
119 auto tail = input.substr(lastPos);
120 if (!tail.empty()) {
121 items.push_back(tail);
122 }
123
124 return items;
125 }
126 } // namespace panda::es2panda::util