blob: e3186de2ee2bfdda4fc0dde931348f25958d9e51 [file] [log] [blame]
Brian Carlstrome24fa612011-09-29 00:53:55 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#include "oat_file.h"
4#include "oat_writer.h"
5
6#include "common_test.h"
7
8namespace art {
9
10class OatTest : public CommonTest {};
11
12TEST_F(OatTest, WriteRead) {
13 const bool compile = false; // DISABLED_ due to the time to compile libcore
14
Brian Carlstrom40381fb2011-10-19 14:13:40 -070015 SirtRef<ClassLoader> class_loader(NULL);
Brian Carlstrome24fa612011-09-29 00:53:55 -070016 if (compile) {
Brian Carlstromaded5f72011-10-07 17:15:04 -070017 compiler_.reset(new Compiler(kThumb2, false));
Brian Carlstrom40381fb2011-10-19 14:13:40 -070018 compiler_->CompileAll(class_loader.get());
Brian Carlstrome24fa612011-09-29 00:53:55 -070019 }
20
21 ScratchFile tmp;
Elliott Hughes234da572011-11-03 22:13:06 -070022 bool success = OatWriter::Create(tmp.GetFile(), class_loader.get(), *compiler_.get());
Brian Carlstrome24fa612011-09-29 00:53:55 -070023 ASSERT_TRUE(success);
24
25 if (compile) { // OatWriter strips the code, regenerate to compare
Brian Carlstrom40381fb2011-10-19 14:13:40 -070026 compiler_->CompileAll(class_loader.get());
Brian Carlstrome24fa612011-09-29 00:53:55 -070027 }
28 UniquePtr<OatFile> oat_file(OatFile::Open(std::string(tmp.GetFilename()), "", NULL));
29 ASSERT_TRUE(oat_file.get() != NULL);
30 const OatHeader& oat_header = oat_file->GetOatHeader();
31 ASSERT_EQ(1U, oat_header.GetDexFileCount());
32
Brian Carlstrom3320cf42011-10-04 14:58:28 -070033 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstrome24fa612011-09-29 00:53:55 -070034
35 const DexFile& dex_file = *java_lang_dex_file_.get();
Brian Carlstromaded5f72011-10-07 17:15:04 -070036 const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -070037 for (size_t i = 0; i < dex_file.NumClassDefs(); i++) {
38 const DexFile::ClassDef& class_def = dex_file.GetClassDef(i);
39 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -070040 size_t num_virtual_methods =0;
41 if (class_data != NULL) {
42 ClassDataItemIterator it(dex_file, class_data);
43 num_virtual_methods = it.NumVirtualMethods();
44 }
Brian Carlstrome24fa612011-09-29 00:53:55 -070045 const char* descriptor = dex_file.GetClassDescriptor(class_def);
46
Brian Carlstromaded5f72011-10-07 17:15:04 -070047 UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file->GetOatClass(i));
Brian Carlstrome24fa612011-09-29 00:53:55 -070048
Brian Carlstrom40381fb2011-10-19 14:13:40 -070049 Class* klass = class_linker->FindClass(descriptor, class_loader.get());
Brian Carlstrome24fa612011-09-29 00:53:55 -070050
51 size_t method_index = 0;
52 for (size_t i = 0; i < klass->NumDirectMethods(); i++, method_index++) {
53 Method* method = klass->GetDirectMethod(i);
Brian Carlstromaded5f72011-10-07 17:15:04 -070054 const OatFile::OatMethod oat_method = oat_class->GetOatMethod(method_index);
Ian Rogers0571d352011-11-03 19:51:38 -070055 const CompiledMethod* compiled_method =
56 compiler_->GetCompiledMethod(Compiler::MethodReference(&dex_file,
57 method->GetDexMethodIndex()));
Brian Carlstrom3320cf42011-10-04 14:58:28 -070058
59 if (compiled_method == NULL) {
60 EXPECT_TRUE(oat_method.code_ == NULL) << PrettyMethod(method) << " " << oat_method.code_;
61 EXPECT_EQ(oat_method.frame_size_in_bytes_, static_cast<uint32_t>(kStackAlignment));
Brian Carlstrom3320cf42011-10-04 14:58:28 -070062 EXPECT_EQ(oat_method.core_spill_mask_, 0U);
63 EXPECT_EQ(oat_method.fp_spill_mask_, 0U);
Brian Carlstrome24fa612011-09-29 00:53:55 -070064 } else {
Brian Carlstrom3320cf42011-10-04 14:58:28 -070065 const void* oat_code = oat_method.code_;
66 uintptr_t oat_code_aligned = RoundDown(reinterpret_cast<uintptr_t>(oat_code), 2);
67 oat_code = reinterpret_cast<const void*>(oat_code_aligned);
68
69 const std::vector<uint8_t>& code = compiled_method->GetCode();
70 size_t code_size = code.size() * sizeof(code[0]);
71 EXPECT_EQ(0, memcmp(oat_code, &code[0], code_size))
72 << PrettyMethod(method) << " " << code_size;
73 CHECK_EQ(0, memcmp(oat_code, &code[0], code_size));
74 EXPECT_EQ(oat_method.frame_size_in_bytes_, compiled_method->GetFrameSizeInBytes());
Brian Carlstrom3320cf42011-10-04 14:58:28 -070075 EXPECT_EQ(oat_method.core_spill_mask_, compiled_method->GetCoreSpillMask());
76 EXPECT_EQ(oat_method.fp_spill_mask_, compiled_method->GetFpSpillMask());
Brian Carlstrome24fa612011-09-29 00:53:55 -070077 }
78 }
79 for (size_t i = 0; i < num_virtual_methods; i++, method_index++) {
80 Method* method = klass->GetVirtualMethod(i);
Brian Carlstromaded5f72011-10-07 17:15:04 -070081 const OatFile::OatMethod oat_method = oat_class->GetOatMethod(method_index);
Ian Rogers0571d352011-11-03 19:51:38 -070082 const CompiledMethod* compiled_method =
83 compiler_->GetCompiledMethod(Compiler::MethodReference(&dex_file,
84 method->GetDexMethodIndex()));
Brian Carlstrom3320cf42011-10-04 14:58:28 -070085
86 if (compiled_method == NULL) {
87 EXPECT_TRUE(oat_method.code_ == NULL) << PrettyMethod(method) << " " << oat_method.code_;
88 EXPECT_EQ(oat_method.frame_size_in_bytes_, static_cast<uint32_t>(kStackAlignment));
Brian Carlstrom3320cf42011-10-04 14:58:28 -070089 EXPECT_EQ(oat_method.core_spill_mask_, 0U);
90 EXPECT_EQ(oat_method.fp_spill_mask_, 0U);
Brian Carlstrome24fa612011-09-29 00:53:55 -070091 } else {
Brian Carlstrom3320cf42011-10-04 14:58:28 -070092 const void* oat_code = oat_method.code_;
93 EXPECT_TRUE(oat_code != NULL) << PrettyMethod(method);
94 uintptr_t oat_code_aligned = RoundDown(reinterpret_cast<uintptr_t>(oat_code), 2);
95 oat_code = reinterpret_cast<const void*>(oat_code_aligned);
96
97 const std::vector<uint8_t>& code = compiled_method->GetCode();
98 size_t code_size = code.size() * sizeof(code[0]);
99 EXPECT_EQ(0, memcmp(oat_code, &code[0], code_size))
100 << PrettyMethod(method) << " " << code_size;
101 CHECK_EQ(0, memcmp(oat_code, &code[0], code_size));
102 EXPECT_EQ(oat_method.frame_size_in_bytes_, compiled_method->GetFrameSizeInBytes());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700103 EXPECT_EQ(oat_method.core_spill_mask_, compiled_method->GetCoreSpillMask());
104 EXPECT_EQ(oat_method.fp_spill_mask_, compiled_method->GetFpSpillMask());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700105 }
106 }
107 }
108}
109
110} // namespace art