blob: e57a5406697841a6886df76faa8c9c3a516d4220 [file] [log] [blame]
Vladimir Markoc91df2d2015-04-23 09:29:21 +00001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_UTILS_TEST_DEX_FILE_BUILDER_H_
18#define ART_COMPILER_UTILS_TEST_DEX_FILE_BUILDER_H_
19
20#include <cstring>
21#include <set>
22#include <map>
23#include <vector>
Vladimir Marko9bdf1082016-01-21 12:15:52 +000024#include <zlib.h>
Vladimir Markoc91df2d2015-04-23 09:29:21 +000025
Vladimir Marko80afd022015-05-19 18:08:00 +010026#include "base/bit_utils.h"
27#include "base/logging.h"
Vladimir Markoc91df2d2015-04-23 09:29:21 +000028#include "dex_file.h"
Vladimir Markoc91df2d2015-04-23 09:29:21 +000029
30namespace art {
31
32class TestDexFileBuilder {
33 public:
34 TestDexFileBuilder()
35 : strings_(), types_(), fields_(), protos_(), dex_file_data_() {
36 }
37
38 void AddString(const std::string& str) {
39 CHECK(dex_file_data_.empty());
40 auto it = strings_.emplace(str, IdxAndDataOffset()).first;
41 CHECK_LT(it->first.length(), 128u); // Don't allow multi-byte length in uleb128.
42 }
43
44 void AddType(const std::string& descriptor) {
45 CHECK(dex_file_data_.empty());
46 AddString(descriptor);
47 types_.emplace(descriptor, 0u);
48 }
49
50 void AddField(const std::string& class_descriptor, const std::string& type,
51 const std::string& name) {
52 CHECK(dex_file_data_.empty());
53 AddType(class_descriptor);
54 AddType(type);
55 AddString(name);
56 FieldKey key = { class_descriptor, type, name };
57 fields_.emplace(key, 0u);
58 }
59
60 void AddMethod(const std::string& class_descriptor, const std::string& signature,
61 const std::string& name) {
62 CHECK(dex_file_data_.empty());
63 AddType(class_descriptor);
64 AddString(name);
65
66 ProtoKey proto_key = CreateProtoKey(signature);
67 AddString(proto_key.shorty);
68 AddType(proto_key.return_type);
69 for (const auto& arg_type : proto_key.args) {
70 AddType(arg_type);
71 }
72 auto it = protos_.emplace(proto_key, IdxAndDataOffset()).first;
73 const ProtoKey* proto = &it->first; // Valid as long as the element remains in protos_.
74
75 MethodKey method_key = {
76 class_descriptor, name, proto
77 };
78 methods_.emplace(method_key, 0u);
79 }
80
81 // NOTE: The builder holds the actual data, so it must live as long as the dex file.
82 std::unique_ptr<const DexFile> Build(const std::string& dex_location) {
83 CHECK(dex_file_data_.empty());
84 union {
85 uint8_t data[sizeof(DexFile::Header)];
86 uint64_t force_alignment;
87 } header_data;
88 std::memset(header_data.data, 0, sizeof(header_data.data));
89 DexFile::Header* header = reinterpret_cast<DexFile::Header*>(&header_data.data);
90 std::copy_n(DexFile::kDexMagic, 4u, header->magic_);
91 std::copy_n(DexFile::kDexMagicVersion, 4u, header->magic_ + 4u);
92 header->header_size_ = sizeof(header);
93 header->endian_tag_ = DexFile::kDexEndianConstant;
94 header->link_size_ = 0u; // Unused.
95 header->link_off_ = 0u; // Unused.
96 header->map_off_ = 0u; // Unused.
97
98 uint32_t data_section_size = 0u;
99
100 uint32_t string_ids_offset = sizeof(DexFile::Header);
101 uint32_t string_idx = 0u;
102 for (auto& entry : strings_) {
103 entry.second.idx = string_idx;
104 string_idx += 1u;
105 entry.second.data_offset = data_section_size;
106 data_section_size += entry.first.length() + 1u /* length */ + 1u /* null-terminator */;
107 }
108 header->string_ids_size_ = strings_.size();
109 header->string_ids_off_ = strings_.empty() ? 0u : string_ids_offset;
110
111 uint32_t type_ids_offset = string_ids_offset + strings_.size() * sizeof(DexFile::StringId);
112 uint32_t type_idx = 0u;
113 for (auto& entry : types_) {
114 entry.second = type_idx;
115 type_idx += 1u;
116 }
117 header->type_ids_size_ = types_.size();
118 header->type_ids_off_ = types_.empty() ? 0u : type_ids_offset;
119
120 uint32_t proto_ids_offset = type_ids_offset + types_.size() * sizeof(DexFile::TypeId);
121 uint32_t proto_idx = 0u;
122 for (auto& entry : protos_) {
123 entry.second.idx = proto_idx;
124 proto_idx += 1u;
125 size_t num_args = entry.first.args.size();
126 if (num_args != 0u) {
127 entry.second.data_offset = RoundUp(data_section_size, 4u);
128 data_section_size = entry.second.data_offset + 4u + num_args * sizeof(DexFile::TypeItem);
129 } else {
130 entry.second.data_offset = 0u;
131 }
132 }
133 header->proto_ids_size_ = protos_.size();
134 header->proto_ids_off_ = protos_.empty() ? 0u : proto_ids_offset;
135
136 uint32_t field_ids_offset = proto_ids_offset + protos_.size() * sizeof(DexFile::ProtoId);
137 uint32_t field_idx = 0u;
138 for (auto& entry : fields_) {
139 entry.second = field_idx;
140 field_idx += 1u;
141 }
142 header->field_ids_size_ = fields_.size();
143 header->field_ids_off_ = fields_.empty() ? 0u : field_ids_offset;
144
145 uint32_t method_ids_offset = field_ids_offset + fields_.size() * sizeof(DexFile::FieldId);
146 uint32_t method_idx = 0u;
147 for (auto& entry : methods_) {
148 entry.second = method_idx;
149 method_idx += 1u;
150 }
151 header->method_ids_size_ = methods_.size();
152 header->method_ids_off_ = methods_.empty() ? 0u : method_ids_offset;
153
154 // No class defs.
155 header->class_defs_size_ = 0u;
156 header->class_defs_off_ = 0u;
157
158 uint32_t data_section_offset = method_ids_offset + methods_.size() * sizeof(DexFile::MethodId);
159 header->data_size_ = data_section_size;
160 header->data_off_ = (data_section_size != 0u) ? data_section_offset : 0u;
161
162 uint32_t total_size = data_section_offset + data_section_size;
163
164 dex_file_data_.resize(total_size);
Vladimir Markoc91df2d2015-04-23 09:29:21 +0000165
166 for (const auto& entry : strings_) {
167 CHECK_LT(entry.first.size(), 128u);
168 uint32_t raw_offset = data_section_offset + entry.second.data_offset;
169 dex_file_data_[raw_offset] = static_cast<uint8_t>(entry.first.size());
170 std::memcpy(&dex_file_data_[raw_offset + 1], entry.first.c_str(), entry.first.size() + 1);
171 Write32(string_ids_offset + entry.second.idx * sizeof(DexFile::StringId), raw_offset);
172 }
173
174 for (const auto& entry : types_) {
175 Write32(type_ids_offset + entry.second * sizeof(DexFile::TypeId), GetStringIdx(entry.first));
176 ++type_idx;
177 }
178
179 for (const auto& entry : protos_) {
180 size_t num_args = entry.first.args.size();
181 uint32_t type_list_offset =
182 (num_args != 0u) ? data_section_offset + entry.second.data_offset : 0u;
183 uint32_t raw_offset = proto_ids_offset + entry.second.idx * sizeof(DexFile::ProtoId);
184 Write32(raw_offset + 0u, GetStringIdx(entry.first.shorty));
185 Write16(raw_offset + 4u, GetTypeIdx(entry.first.return_type));
186 Write32(raw_offset + 8u, type_list_offset);
187 if (num_args != 0u) {
188 CHECK_NE(entry.second.data_offset, 0u);
189 Write32(type_list_offset, num_args);
190 for (size_t i = 0; i != num_args; ++i) {
191 Write16(type_list_offset + 4u + i * sizeof(DexFile::TypeItem),
192 GetTypeIdx(entry.first.args[i]));
193 }
194 }
195 }
196
197 for (const auto& entry : fields_) {
198 uint32_t raw_offset = field_ids_offset + entry.second * sizeof(DexFile::FieldId);
199 Write16(raw_offset + 0u, GetTypeIdx(entry.first.class_descriptor));
200 Write16(raw_offset + 2u, GetTypeIdx(entry.first.type));
201 Write32(raw_offset + 4u, GetStringIdx(entry.first.name));
202 }
203
204 for (const auto& entry : methods_) {
205 uint32_t raw_offset = method_ids_offset + entry.second * sizeof(DexFile::MethodId);
206 Write16(raw_offset + 0u, GetTypeIdx(entry.first.class_descriptor));
207 auto it = protos_.find(*entry.first.proto);
208 CHECK(it != protos_.end());
209 Write16(raw_offset + 2u, it->second.idx);
210 Write32(raw_offset + 4u, GetStringIdx(entry.first.name));
211 }
212
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000213 // Leave signature as zeros.
214
215 header->file_size_ = dex_file_data_.size();
216 size_t skip = sizeof(header->magic_) + sizeof(header->checksum_);
217 header->checksum_ = adler32(0u, dex_file_data_.data() + skip, dex_file_data_.size() - skip);
218 std::memcpy(&dex_file_data_[0], header_data.data, sizeof(DexFile::Header));
Vladimir Markoc91df2d2015-04-23 09:29:21 +0000219
220 std::string error_msg;
221 std::unique_ptr<const DexFile> dex_file(DexFile::Open(
222 &dex_file_data_[0], dex_file_data_.size(), dex_location, 0u, nullptr, &error_msg));
223 CHECK(dex_file != nullptr) << error_msg;
Pirama Arumuga Nainar0600cdc2015-09-14 11:00:16 -0700224 return dex_file;
Vladimir Markoc91df2d2015-04-23 09:29:21 +0000225 }
226
227 uint32_t GetStringIdx(const std::string& type) {
228 auto it = strings_.find(type);
229 CHECK(it != strings_.end());
230 return it->second.idx;
231 }
232
233 uint32_t GetTypeIdx(const std::string& type) {
234 auto it = types_.find(type);
235 CHECK(it != types_.end());
236 return it->second;
237 }
238
239 uint32_t GetFieldIdx(const std::string& class_descriptor, const std::string& type,
240 const std::string& name) {
241 FieldKey key = { class_descriptor, type, name };
242 auto it = fields_.find(key);
243 CHECK(it != fields_.end());
244 return it->second;
245 }
246
247 uint32_t GetMethodIdx(const std::string& class_descriptor, const std::string& signature,
248 const std::string& name) {
249 ProtoKey proto_key = CreateProtoKey(signature);
250 MethodKey method_key = { class_descriptor, name, &proto_key };
251 auto it = methods_.find(method_key);
252 CHECK(it != methods_.end());
253 return it->second;
254 }
255
256 private:
257 struct IdxAndDataOffset {
258 uint32_t idx;
259 uint32_t data_offset;
260 };
261
262 struct FieldKey {
263 const std::string class_descriptor;
264 const std::string type;
265 const std::string name;
266 };
267 struct FieldKeyComparator {
268 bool operator()(const FieldKey& lhs, const FieldKey& rhs) const {
269 if (lhs.class_descriptor != rhs.class_descriptor) {
270 return lhs.class_descriptor < rhs.class_descriptor;
271 }
272 if (lhs.name != rhs.name) {
273 return lhs.name < rhs.name;
274 }
275 return lhs.type < rhs.type;
276 }
277 };
278
279 struct ProtoKey {
280 std::string shorty;
281 std::string return_type;
282 std::vector<std::string> args;
283 };
284 struct ProtoKeyComparator {
285 bool operator()(const ProtoKey& lhs, const ProtoKey& rhs) const {
286 if (lhs.return_type != rhs.return_type) {
287 return lhs.return_type < rhs.return_type;
288 }
289 size_t min_args = std::min(lhs.args.size(), rhs.args.size());
290 for (size_t i = 0; i != min_args; ++i) {
291 if (lhs.args[i] != rhs.args[i]) {
292 return lhs.args[i] < rhs.args[i];
293 }
294 }
295 return lhs.args.size() < rhs.args.size();
296 }
297 };
298
299 struct MethodKey {
300 std::string class_descriptor;
301 std::string name;
302 const ProtoKey* proto;
303 };
304 struct MethodKeyComparator {
305 bool operator()(const MethodKey& lhs, const MethodKey& rhs) const {
306 if (lhs.class_descriptor != rhs.class_descriptor) {
307 return lhs.class_descriptor < rhs.class_descriptor;
308 }
309 if (lhs.name != rhs.name) {
310 return lhs.name < rhs.name;
311 }
312 return ProtoKeyComparator()(*lhs.proto, *rhs.proto);
313 }
314 };
315
316 ProtoKey CreateProtoKey(const std::string& signature) {
317 CHECK_EQ(signature[0], '(');
318 const char* args = signature.c_str() + 1;
319 const char* args_end = std::strchr(args, ')');
320 CHECK(args_end != nullptr);
321 const char* return_type = args_end + 1;
322
323 ProtoKey key = {
324 std::string() + ((*return_type == '[') ? 'L' : *return_type),
325 return_type,
326 std::vector<std::string>()
327 };
328 while (args != args_end) {
329 key.shorty += (*args == '[') ? 'L' : *args;
330 const char* arg_start = args;
331 while (*args == '[') {
332 ++args;
333 }
334 if (*args == 'L') {
335 do {
336 ++args;
337 CHECK_NE(args, args_end);
338 } while (*args != ';');
339 }
340 ++args;
341 key.args.emplace_back(arg_start, args);
342 }
343 return key;
344 }
345
346 void Write32(size_t offset, uint32_t value) {
347 CHECK_LE(offset + 4u, dex_file_data_.size());
348 CHECK_EQ(dex_file_data_[offset + 0], 0u);
349 CHECK_EQ(dex_file_data_[offset + 1], 0u);
350 CHECK_EQ(dex_file_data_[offset + 2], 0u);
351 CHECK_EQ(dex_file_data_[offset + 3], 0u);
352 dex_file_data_[offset + 0] = static_cast<uint8_t>(value >> 0);
353 dex_file_data_[offset + 1] = static_cast<uint8_t>(value >> 8);
354 dex_file_data_[offset + 2] = static_cast<uint8_t>(value >> 16);
355 dex_file_data_[offset + 3] = static_cast<uint8_t>(value >> 24);
356 }
357
358 void Write16(size_t offset, uint32_t value) {
359 CHECK_LE(value, 0xffffu);
360 CHECK_LE(offset + 2u, dex_file_data_.size());
361 CHECK_EQ(dex_file_data_[offset + 0], 0u);
362 CHECK_EQ(dex_file_data_[offset + 1], 0u);
363 dex_file_data_[offset + 0] = static_cast<uint8_t>(value >> 0);
364 dex_file_data_[offset + 1] = static_cast<uint8_t>(value >> 8);
365 }
366
367 std::map<std::string, IdxAndDataOffset> strings_;
368 std::map<std::string, uint32_t> types_;
369 std::map<FieldKey, uint32_t, FieldKeyComparator> fields_;
370 std::map<ProtoKey, IdxAndDataOffset, ProtoKeyComparator> protos_;
371 std::map<MethodKey, uint32_t, MethodKeyComparator> methods_;
372
373 std::vector<uint8_t> dex_file_data_;
374};
375
376} // namespace art
377
378#endif // ART_COMPILER_UTILS_TEST_DEX_FILE_BUILDER_H_