blob: 441ef8e117089faf6f5ff7a4b4e55e052bf65646 [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
Vladimir Marko9bdf1082016-01-21 12:15:52 +000020#include <zlib.h>
Vladimir Markoc91df2d2015-04-23 09:29:21 +000021
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070022#include <cstring>
23#include <map>
24#include <set>
25#include <vector>
26
Andreas Gampe57943812017-12-06 21:39:13 -080027#include <android-base/logging.h>
28
Vladimir Marko80afd022015-05-19 18:08:00 +010029#include "base/bit_utils.h"
Mathieu Chartier79c87da2017-10-10 11:54:29 -070030#include "dex_file_loader.h"
Mathieu Chartier292567e2017-10-12 13:24:38 -070031#include "standard_dex_file.h"
Vladimir Markoc91df2d2015-04-23 09:29:21 +000032
33namespace art {
34
35class TestDexFileBuilder {
36 public:
37 TestDexFileBuilder()
38 : strings_(), types_(), fields_(), protos_(), dex_file_data_() {
39 }
40
41 void AddString(const std::string& str) {
42 CHECK(dex_file_data_.empty());
43 auto it = strings_.emplace(str, IdxAndDataOffset()).first;
44 CHECK_LT(it->first.length(), 128u); // Don't allow multi-byte length in uleb128.
45 }
46
47 void AddType(const std::string& descriptor) {
48 CHECK(dex_file_data_.empty());
49 AddString(descriptor);
50 types_.emplace(descriptor, 0u);
51 }
52
53 void AddField(const std::string& class_descriptor, const std::string& type,
54 const std::string& name) {
55 CHECK(dex_file_data_.empty());
56 AddType(class_descriptor);
57 AddType(type);
58 AddString(name);
59 FieldKey key = { class_descriptor, type, name };
60 fields_.emplace(key, 0u);
61 }
62
63 void AddMethod(const std::string& class_descriptor, const std::string& signature,
64 const std::string& name) {
65 CHECK(dex_file_data_.empty());
66 AddType(class_descriptor);
67 AddString(name);
68
69 ProtoKey proto_key = CreateProtoKey(signature);
70 AddString(proto_key.shorty);
71 AddType(proto_key.return_type);
72 for (const auto& arg_type : proto_key.args) {
73 AddType(arg_type);
74 }
75 auto it = protos_.emplace(proto_key, IdxAndDataOffset()).first;
76 const ProtoKey* proto = &it->first; // Valid as long as the element remains in protos_.
77
78 MethodKey method_key = {
79 class_descriptor, name, proto
80 };
81 methods_.emplace(method_key, 0u);
82 }
83
84 // NOTE: The builder holds the actual data, so it must live as long as the dex file.
85 std::unique_ptr<const DexFile> Build(const std::string& dex_location) {
86 CHECK(dex_file_data_.empty());
87 union {
88 uint8_t data[sizeof(DexFile::Header)];
89 uint64_t force_alignment;
90 } header_data;
91 std::memset(header_data.data, 0, sizeof(header_data.data));
92 DexFile::Header* header = reinterpret_cast<DexFile::Header*>(&header_data.data);
Mathieu Chartier292567e2017-10-12 13:24:38 -070093 std::copy_n(StandardDexFile::kDexMagic, 4u, header->magic_);
94 std::copy_n(StandardDexFile::kDexMagicVersions[0], 4u, header->magic_ + 4u);
Andreas Gampe3a2bd292016-01-26 17:23:47 -080095 header->header_size_ = sizeof(DexFile::Header);
Vladimir Markoc91df2d2015-04-23 09:29:21 +000096 header->endian_tag_ = DexFile::kDexEndianConstant;
97 header->link_size_ = 0u; // Unused.
98 header->link_off_ = 0u; // Unused.
Andreas Gampe3a2bd292016-01-26 17:23:47 -080099 header->map_off_ = 0u; // Unused. TODO: This is wrong. Dex files created by this builder
100 // cannot be verified. b/26808512
Vladimir Markoc91df2d2015-04-23 09:29:21 +0000101
102 uint32_t data_section_size = 0u;
103
104 uint32_t string_ids_offset = sizeof(DexFile::Header);
105 uint32_t string_idx = 0u;
106 for (auto& entry : strings_) {
107 entry.second.idx = string_idx;
108 string_idx += 1u;
109 entry.second.data_offset = data_section_size;
110 data_section_size += entry.first.length() + 1u /* length */ + 1u /* null-terminator */;
111 }
112 header->string_ids_size_ = strings_.size();
113 header->string_ids_off_ = strings_.empty() ? 0u : string_ids_offset;
114
115 uint32_t type_ids_offset = string_ids_offset + strings_.size() * sizeof(DexFile::StringId);
116 uint32_t type_idx = 0u;
117 for (auto& entry : types_) {
118 entry.second = type_idx;
119 type_idx += 1u;
120 }
121 header->type_ids_size_ = types_.size();
122 header->type_ids_off_ = types_.empty() ? 0u : type_ids_offset;
123
124 uint32_t proto_ids_offset = type_ids_offset + types_.size() * sizeof(DexFile::TypeId);
125 uint32_t proto_idx = 0u;
126 for (auto& entry : protos_) {
127 entry.second.idx = proto_idx;
128 proto_idx += 1u;
129 size_t num_args = entry.first.args.size();
130 if (num_args != 0u) {
131 entry.second.data_offset = RoundUp(data_section_size, 4u);
132 data_section_size = entry.second.data_offset + 4u + num_args * sizeof(DexFile::TypeItem);
133 } else {
134 entry.second.data_offset = 0u;
135 }
136 }
137 header->proto_ids_size_ = protos_.size();
138 header->proto_ids_off_ = protos_.empty() ? 0u : proto_ids_offset;
139
140 uint32_t field_ids_offset = proto_ids_offset + protos_.size() * sizeof(DexFile::ProtoId);
141 uint32_t field_idx = 0u;
142 for (auto& entry : fields_) {
143 entry.second = field_idx;
144 field_idx += 1u;
145 }
146 header->field_ids_size_ = fields_.size();
147 header->field_ids_off_ = fields_.empty() ? 0u : field_ids_offset;
148
149 uint32_t method_ids_offset = field_ids_offset + fields_.size() * sizeof(DexFile::FieldId);
150 uint32_t method_idx = 0u;
151 for (auto& entry : methods_) {
152 entry.second = method_idx;
153 method_idx += 1u;
154 }
155 header->method_ids_size_ = methods_.size();
156 header->method_ids_off_ = methods_.empty() ? 0u : method_ids_offset;
157
158 // No class defs.
159 header->class_defs_size_ = 0u;
160 header->class_defs_off_ = 0u;
161
162 uint32_t data_section_offset = method_ids_offset + methods_.size() * sizeof(DexFile::MethodId);
163 header->data_size_ = data_section_size;
164 header->data_off_ = (data_section_size != 0u) ? data_section_offset : 0u;
165
166 uint32_t total_size = data_section_offset + data_section_size;
167
168 dex_file_data_.resize(total_size);
Vladimir Markoc91df2d2015-04-23 09:29:21 +0000169
170 for (const auto& entry : strings_) {
171 CHECK_LT(entry.first.size(), 128u);
172 uint32_t raw_offset = data_section_offset + entry.second.data_offset;
173 dex_file_data_[raw_offset] = static_cast<uint8_t>(entry.first.size());
174 std::memcpy(&dex_file_data_[raw_offset + 1], entry.first.c_str(), entry.first.size() + 1);
175 Write32(string_ids_offset + entry.second.idx * sizeof(DexFile::StringId), raw_offset);
176 }
177
178 for (const auto& entry : types_) {
179 Write32(type_ids_offset + entry.second * sizeof(DexFile::TypeId), GetStringIdx(entry.first));
180 ++type_idx;
181 }
182
183 for (const auto& entry : protos_) {
184 size_t num_args = entry.first.args.size();
185 uint32_t type_list_offset =
186 (num_args != 0u) ? data_section_offset + entry.second.data_offset : 0u;
187 uint32_t raw_offset = proto_ids_offset + entry.second.idx * sizeof(DexFile::ProtoId);
188 Write32(raw_offset + 0u, GetStringIdx(entry.first.shorty));
189 Write16(raw_offset + 4u, GetTypeIdx(entry.first.return_type));
190 Write32(raw_offset + 8u, type_list_offset);
191 if (num_args != 0u) {
192 CHECK_NE(entry.second.data_offset, 0u);
193 Write32(type_list_offset, num_args);
194 for (size_t i = 0; i != num_args; ++i) {
195 Write16(type_list_offset + 4u + i * sizeof(DexFile::TypeItem),
196 GetTypeIdx(entry.first.args[i]));
197 }
198 }
199 }
200
201 for (const auto& entry : fields_) {
202 uint32_t raw_offset = field_ids_offset + entry.second * sizeof(DexFile::FieldId);
203 Write16(raw_offset + 0u, GetTypeIdx(entry.first.class_descriptor));
204 Write16(raw_offset + 2u, GetTypeIdx(entry.first.type));
205 Write32(raw_offset + 4u, GetStringIdx(entry.first.name));
206 }
207
208 for (const auto& entry : methods_) {
209 uint32_t raw_offset = method_ids_offset + entry.second * sizeof(DexFile::MethodId);
210 Write16(raw_offset + 0u, GetTypeIdx(entry.first.class_descriptor));
211 auto it = protos_.find(*entry.first.proto);
212 CHECK(it != protos_.end());
213 Write16(raw_offset + 2u, it->second.idx);
214 Write32(raw_offset + 4u, GetStringIdx(entry.first.name));
215 }
216
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000217 // Leave signature as zeros.
218
219 header->file_size_ = dex_file_data_.size();
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800220
221 // Write the complete header early, as part of it needs to be checksummed.
222 std::memcpy(&dex_file_data_[0], header_data.data, sizeof(DexFile::Header));
223
224 // Checksum starts after the checksum field.
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000225 size_t skip = sizeof(header->magic_) + sizeof(header->checksum_);
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800226 header->checksum_ = adler32(adler32(0L, Z_NULL, 0),
227 dex_file_data_.data() + skip,
228 dex_file_data_.size() - skip);
229
230 // Write the complete header again, just simpler that way.
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000231 std::memcpy(&dex_file_data_[0], header_data.data, sizeof(DexFile::Header));
Vladimir Markoc91df2d2015-04-23 09:29:21 +0000232
Aart Bik37d6a3b2016-06-21 18:30:10 -0700233 static constexpr bool kVerify = false;
234 static constexpr bool kVerifyChecksum = false;
Vladimir Markoc91df2d2015-04-23 09:29:21 +0000235 std::string error_msg;
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700236 std::unique_ptr<const DexFile> dex_file(DexFileLoader::Open(
Aart Bik37d6a3b2016-06-21 18:30:10 -0700237 &dex_file_data_[0],
238 dex_file_data_.size(),
239 dex_location,
240 0u,
241 nullptr,
242 kVerify,
243 kVerifyChecksum,
244 &error_msg));
Vladimir Markoc91df2d2015-04-23 09:29:21 +0000245 CHECK(dex_file != nullptr) << error_msg;
Pirama Arumuga Nainar0600cdc2015-09-14 11:00:16 -0700246 return dex_file;
Vladimir Markoc91df2d2015-04-23 09:29:21 +0000247 }
248
249 uint32_t GetStringIdx(const std::string& type) {
250 auto it = strings_.find(type);
251 CHECK(it != strings_.end());
252 return it->second.idx;
253 }
254
255 uint32_t GetTypeIdx(const std::string& type) {
256 auto it = types_.find(type);
257 CHECK(it != types_.end());
258 return it->second;
259 }
260
261 uint32_t GetFieldIdx(const std::string& class_descriptor, const std::string& type,
262 const std::string& name) {
263 FieldKey key = { class_descriptor, type, name };
264 auto it = fields_.find(key);
265 CHECK(it != fields_.end());
266 return it->second;
267 }
268
269 uint32_t GetMethodIdx(const std::string& class_descriptor, const std::string& signature,
270 const std::string& name) {
271 ProtoKey proto_key = CreateProtoKey(signature);
272 MethodKey method_key = { class_descriptor, name, &proto_key };
273 auto it = methods_.find(method_key);
274 CHECK(it != methods_.end());
275 return it->second;
276 }
277
278 private:
279 struct IdxAndDataOffset {
280 uint32_t idx;
281 uint32_t data_offset;
282 };
283
284 struct FieldKey {
285 const std::string class_descriptor;
286 const std::string type;
287 const std::string name;
288 };
289 struct FieldKeyComparator {
290 bool operator()(const FieldKey& lhs, const FieldKey& rhs) const {
291 if (lhs.class_descriptor != rhs.class_descriptor) {
292 return lhs.class_descriptor < rhs.class_descriptor;
293 }
294 if (lhs.name != rhs.name) {
295 return lhs.name < rhs.name;
296 }
297 return lhs.type < rhs.type;
298 }
299 };
300
301 struct ProtoKey {
302 std::string shorty;
303 std::string return_type;
304 std::vector<std::string> args;
305 };
306 struct ProtoKeyComparator {
307 bool operator()(const ProtoKey& lhs, const ProtoKey& rhs) const {
308 if (lhs.return_type != rhs.return_type) {
309 return lhs.return_type < rhs.return_type;
310 }
311 size_t min_args = std::min(lhs.args.size(), rhs.args.size());
312 for (size_t i = 0; i != min_args; ++i) {
313 if (lhs.args[i] != rhs.args[i]) {
314 return lhs.args[i] < rhs.args[i];
315 }
316 }
317 return lhs.args.size() < rhs.args.size();
318 }
319 };
320
321 struct MethodKey {
322 std::string class_descriptor;
323 std::string name;
324 const ProtoKey* proto;
325 };
326 struct MethodKeyComparator {
327 bool operator()(const MethodKey& lhs, const MethodKey& rhs) const {
328 if (lhs.class_descriptor != rhs.class_descriptor) {
329 return lhs.class_descriptor < rhs.class_descriptor;
330 }
331 if (lhs.name != rhs.name) {
332 return lhs.name < rhs.name;
333 }
334 return ProtoKeyComparator()(*lhs.proto, *rhs.proto);
335 }
336 };
337
338 ProtoKey CreateProtoKey(const std::string& signature) {
339 CHECK_EQ(signature[0], '(');
340 const char* args = signature.c_str() + 1;
341 const char* args_end = std::strchr(args, ')');
342 CHECK(args_end != nullptr);
343 const char* return_type = args_end + 1;
344
345 ProtoKey key = {
346 std::string() + ((*return_type == '[') ? 'L' : *return_type),
347 return_type,
348 std::vector<std::string>()
349 };
350 while (args != args_end) {
351 key.shorty += (*args == '[') ? 'L' : *args;
352 const char* arg_start = args;
353 while (*args == '[') {
354 ++args;
355 }
356 if (*args == 'L') {
357 do {
358 ++args;
359 CHECK_NE(args, args_end);
360 } while (*args != ';');
361 }
362 ++args;
363 key.args.emplace_back(arg_start, args);
364 }
365 return key;
366 }
367
368 void Write32(size_t offset, uint32_t value) {
369 CHECK_LE(offset + 4u, dex_file_data_.size());
370 CHECK_EQ(dex_file_data_[offset + 0], 0u);
371 CHECK_EQ(dex_file_data_[offset + 1], 0u);
372 CHECK_EQ(dex_file_data_[offset + 2], 0u);
373 CHECK_EQ(dex_file_data_[offset + 3], 0u);
374 dex_file_data_[offset + 0] = static_cast<uint8_t>(value >> 0);
375 dex_file_data_[offset + 1] = static_cast<uint8_t>(value >> 8);
376 dex_file_data_[offset + 2] = static_cast<uint8_t>(value >> 16);
377 dex_file_data_[offset + 3] = static_cast<uint8_t>(value >> 24);
378 }
379
380 void Write16(size_t offset, uint32_t value) {
381 CHECK_LE(value, 0xffffu);
382 CHECK_LE(offset + 2u, dex_file_data_.size());
383 CHECK_EQ(dex_file_data_[offset + 0], 0u);
384 CHECK_EQ(dex_file_data_[offset + 1], 0u);
385 dex_file_data_[offset + 0] = static_cast<uint8_t>(value >> 0);
386 dex_file_data_[offset + 1] = static_cast<uint8_t>(value >> 8);
387 }
388
389 std::map<std::string, IdxAndDataOffset> strings_;
390 std::map<std::string, uint32_t> types_;
391 std::map<FieldKey, uint32_t, FieldKeyComparator> fields_;
392 std::map<ProtoKey, IdxAndDataOffset, ProtoKeyComparator> protos_;
393 std::map<MethodKey, uint32_t, MethodKeyComparator> methods_;
394
395 std::vector<uint8_t> dex_file_data_;
396};
397
398} // namespace art
399
400#endif // ART_COMPILER_UTILS_TEST_DEX_FILE_BUILDER_H_