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