blob: 17a00e4fc49c38c00ee612304d99e1f07b486ad5 [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
Jesse Wilson254db0f2011-11-16 16:44:11 -050014 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstrome24fa612011-09-29 00:53:55 -070015
Brian Carlstrom40381fb2011-10-19 14:13:40 -070016 SirtRef<ClassLoader> class_loader(NULL);
Brian Carlstrome24fa612011-09-29 00:53:55 -070017 if (compile) {
Brian Carlstromaded5f72011-10-07 17:15:04 -070018 compiler_.reset(new Compiler(kThumb2, false));
Jesse Wilson254db0f2011-11-16 16:44:11 -050019 compiler_->CompileAll(class_loader.get(), class_linker->GetBootClassPath());
Brian Carlstrome24fa612011-09-29 00:53:55 -070020 }
21
22 ScratchFile tmp;
Elliott Hughes234da572011-11-03 22:13:06 -070023 bool success = OatWriter::Create(tmp.GetFile(), class_loader.get(), *compiler_.get());
Brian Carlstrome24fa612011-09-29 00:53:55 -070024 ASSERT_TRUE(success);
25
26 if (compile) { // OatWriter strips the code, regenerate to compare
Jesse Wilson254db0f2011-11-16 16:44:11 -050027 compiler_->CompileAll(class_loader.get(), class_linker->GetBootClassPath());
Brian Carlstrome24fa612011-09-29 00:53:55 -070028 }
29 UniquePtr<OatFile> oat_file(OatFile::Open(std::string(tmp.GetFilename()), "", NULL));
30 ASSERT_TRUE(oat_file.get() != NULL);
31 const OatHeader& oat_header = oat_file->GetOatHeader();
32 ASSERT_EQ(1U, oat_header.GetDexFileCount());
33
Brian Carlstrome24fa612011-09-29 00:53:55 -070034 const DexFile& dex_file = *java_lang_dex_file_.get();
Brian Carlstromaded5f72011-10-07 17:15:04 -070035 const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -070036 for (size_t i = 0; i < dex_file.NumClassDefs(); i++) {
37 const DexFile::ClassDef& class_def = dex_file.GetClassDef(i);
38 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -070039 size_t num_virtual_methods =0;
40 if (class_data != NULL) {
41 ClassDataItemIterator it(dex_file, class_data);
42 num_virtual_methods = it.NumVirtualMethods();
43 }
Brian Carlstrome24fa612011-09-29 00:53:55 -070044 const char* descriptor = dex_file.GetClassDescriptor(class_def);
45
Brian Carlstromaded5f72011-10-07 17:15:04 -070046 UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file->GetOatClass(i));
Brian Carlstrome24fa612011-09-29 00:53:55 -070047
Brian Carlstrom40381fb2011-10-19 14:13:40 -070048 Class* klass = class_linker->FindClass(descriptor, class_loader.get());
Brian Carlstrome24fa612011-09-29 00:53:55 -070049
50 size_t method_index = 0;
51 for (size_t i = 0; i < klass->NumDirectMethods(); i++, method_index++) {
52 Method* method = klass->GetDirectMethod(i);
Brian Carlstromaded5f72011-10-07 17:15:04 -070053 const OatFile::OatMethod oat_method = oat_class->GetOatMethod(method_index);
Ian Rogers0571d352011-11-03 19:51:38 -070054 const CompiledMethod* compiled_method =
55 compiler_->GetCompiledMethod(Compiler::MethodReference(&dex_file,
56 method->GetDexMethodIndex()));
Brian Carlstrom3320cf42011-10-04 14:58:28 -070057
58 if (compiled_method == NULL) {
59 EXPECT_TRUE(oat_method.code_ == NULL) << PrettyMethod(method) << " " << oat_method.code_;
60 EXPECT_EQ(oat_method.frame_size_in_bytes_, static_cast<uint32_t>(kStackAlignment));
Brian Carlstrom3320cf42011-10-04 14:58:28 -070061 EXPECT_EQ(oat_method.core_spill_mask_, 0U);
62 EXPECT_EQ(oat_method.fp_spill_mask_, 0U);
Brian Carlstrome24fa612011-09-29 00:53:55 -070063 } else {
Brian Carlstrom3320cf42011-10-04 14:58:28 -070064 const void* oat_code = oat_method.code_;
65 uintptr_t oat_code_aligned = RoundDown(reinterpret_cast<uintptr_t>(oat_code), 2);
66 oat_code = reinterpret_cast<const void*>(oat_code_aligned);
67
68 const std::vector<uint8_t>& code = compiled_method->GetCode();
69 size_t code_size = code.size() * sizeof(code[0]);
70 EXPECT_EQ(0, memcmp(oat_code, &code[0], code_size))
71 << PrettyMethod(method) << " " << code_size;
72 CHECK_EQ(0, memcmp(oat_code, &code[0], code_size));
73 EXPECT_EQ(oat_method.frame_size_in_bytes_, compiled_method->GetFrameSizeInBytes());
Brian Carlstrom3320cf42011-10-04 14:58:28 -070074 EXPECT_EQ(oat_method.core_spill_mask_, compiled_method->GetCoreSpillMask());
75 EXPECT_EQ(oat_method.fp_spill_mask_, compiled_method->GetFpSpillMask());
Brian Carlstrome24fa612011-09-29 00:53:55 -070076 }
77 }
78 for (size_t i = 0; i < num_virtual_methods; i++, method_index++) {
79 Method* method = klass->GetVirtualMethod(i);
Brian Carlstromaded5f72011-10-07 17:15:04 -070080 const OatFile::OatMethod oat_method = oat_class->GetOatMethod(method_index);
Ian Rogers0571d352011-11-03 19:51:38 -070081 const CompiledMethod* compiled_method =
82 compiler_->GetCompiledMethod(Compiler::MethodReference(&dex_file,
83 method->GetDexMethodIndex()));
Brian Carlstrom3320cf42011-10-04 14:58:28 -070084
85 if (compiled_method == NULL) {
86 EXPECT_TRUE(oat_method.code_ == NULL) << PrettyMethod(method) << " " << oat_method.code_;
87 EXPECT_EQ(oat_method.frame_size_in_bytes_, static_cast<uint32_t>(kStackAlignment));
Brian Carlstrom3320cf42011-10-04 14:58:28 -070088 EXPECT_EQ(oat_method.core_spill_mask_, 0U);
89 EXPECT_EQ(oat_method.fp_spill_mask_, 0U);
Brian Carlstrome24fa612011-09-29 00:53:55 -070090 } else {
Brian Carlstrom3320cf42011-10-04 14:58:28 -070091 const void* oat_code = oat_method.code_;
92 EXPECT_TRUE(oat_code != NULL) << PrettyMethod(method);
93 uintptr_t oat_code_aligned = RoundDown(reinterpret_cast<uintptr_t>(oat_code), 2);
94 oat_code = reinterpret_cast<const void*>(oat_code_aligned);
95
96 const std::vector<uint8_t>& code = compiled_method->GetCode();
97 size_t code_size = code.size() * sizeof(code[0]);
98 EXPECT_EQ(0, memcmp(oat_code, &code[0], code_size))
99 << PrettyMethod(method) << " " << code_size;
100 CHECK_EQ(0, memcmp(oat_code, &code[0], code_size));
101 EXPECT_EQ(oat_method.frame_size_in_bytes_, compiled_method->GetFrameSizeInBytes());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700102 EXPECT_EQ(oat_method.core_spill_mask_, compiled_method->GetCoreSpillMask());
103 EXPECT_EQ(oat_method.fp_spill_mask_, compiled_method->GetFpSpillMask());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700104 }
105 }
106 }
107}
108
109} // namespace art