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