blob: 392a8228d98e3e772074c7af4bc3171e5baab369 [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
15 const ClassLoader* class_loader = NULL;
16 if (compile) {
Brian Carlstromaded5f72011-10-07 17:15:04 -070017 compiler_.reset(new Compiler(kThumb2, false));
Brian Carlstrome24fa612011-09-29 00:53:55 -070018 compiler_->CompileAll(class_loader);
19 }
20
21 ScratchFile tmp;
Brian Carlstrom3320cf42011-10-04 14:58:28 -070022 bool success = OatWriter::Create(tmp.GetFilename(), class_loader, *compiler_.get());
Brian Carlstrome24fa612011-09-29 00:53:55 -070023 ASSERT_TRUE(success);
24
25 if (compile) { // OatWriter strips the code, regenerate to compare
26 compiler_->CompileAll(class_loader);
27 }
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);
40 DexFile::ClassDataHeader header = dex_file.ReadClassDataHeader(&class_data);
41 size_t num_virtual_methods = header.virtual_methods_size_;
42 const char* descriptor = dex_file.GetClassDescriptor(class_def);
43
Brian Carlstromaded5f72011-10-07 17:15:04 -070044 UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file->GetOatClass(i));
Brian Carlstrome24fa612011-09-29 00:53:55 -070045
46 Class* klass = class_linker->FindClass(descriptor, class_loader);
47
48 size_t method_index = 0;
49 for (size_t i = 0; i < klass->NumDirectMethods(); i++, method_index++) {
50 Method* method = klass->GetDirectMethod(i);
Brian Carlstromaded5f72011-10-07 17:15:04 -070051 const OatFile::OatMethod oat_method = oat_class->GetOatMethod(method_index);
Brian Carlstrom3320cf42011-10-04 14:58:28 -070052 const CompiledMethod* compiled_method = compiler_->GetCompiledMethod(method);
53
54 if (compiled_method == NULL) {
55 EXPECT_TRUE(oat_method.code_ == NULL) << PrettyMethod(method) << " " << oat_method.code_;
56 EXPECT_EQ(oat_method.frame_size_in_bytes_, static_cast<uint32_t>(kStackAlignment));
57 EXPECT_EQ(oat_method.return_pc_offset_in_bytes_, 0U);
58 EXPECT_EQ(oat_method.core_spill_mask_, 0U);
59 EXPECT_EQ(oat_method.fp_spill_mask_, 0U);
Brian Carlstrome24fa612011-09-29 00:53:55 -070060 } else {
Brian Carlstrom3320cf42011-10-04 14:58:28 -070061 const void* oat_code = oat_method.code_;
62 uintptr_t oat_code_aligned = RoundDown(reinterpret_cast<uintptr_t>(oat_code), 2);
63 oat_code = reinterpret_cast<const void*>(oat_code_aligned);
64
65 const std::vector<uint8_t>& code = compiled_method->GetCode();
66 size_t code_size = code.size() * sizeof(code[0]);
67 EXPECT_EQ(0, memcmp(oat_code, &code[0], code_size))
68 << PrettyMethod(method) << " " << code_size;
69 CHECK_EQ(0, memcmp(oat_code, &code[0], code_size));
70 EXPECT_EQ(oat_method.frame_size_in_bytes_, compiled_method->GetFrameSizeInBytes());
71 EXPECT_EQ(oat_method.return_pc_offset_in_bytes_, compiled_method->GetReturnPcOffsetInBytes());
72 EXPECT_EQ(oat_method.core_spill_mask_, compiled_method->GetCoreSpillMask());
73 EXPECT_EQ(oat_method.fp_spill_mask_, compiled_method->GetFpSpillMask());
Brian Carlstrome24fa612011-09-29 00:53:55 -070074 }
75 }
76 for (size_t i = 0; i < num_virtual_methods; i++, method_index++) {
77 Method* method = klass->GetVirtualMethod(i);
Brian Carlstromaded5f72011-10-07 17:15:04 -070078 const OatFile::OatMethod oat_method = oat_class->GetOatMethod(method_index);
Brian Carlstrom3320cf42011-10-04 14:58:28 -070079 const CompiledMethod* compiled_method = compiler_->GetCompiledMethod(method);
80
81 if (compiled_method == NULL) {
82 EXPECT_TRUE(oat_method.code_ == NULL) << PrettyMethod(method) << " " << oat_method.code_;
83 EXPECT_EQ(oat_method.frame_size_in_bytes_, static_cast<uint32_t>(kStackAlignment));
84 EXPECT_EQ(oat_method.return_pc_offset_in_bytes_, 0U);
85 EXPECT_EQ(oat_method.core_spill_mask_, 0U);
86 EXPECT_EQ(oat_method.fp_spill_mask_, 0U);
Brian Carlstrome24fa612011-09-29 00:53:55 -070087 } else {
Brian Carlstrom3320cf42011-10-04 14:58:28 -070088 const void* oat_code = oat_method.code_;
89 EXPECT_TRUE(oat_code != NULL) << PrettyMethod(method);
90 uintptr_t oat_code_aligned = RoundDown(reinterpret_cast<uintptr_t>(oat_code), 2);
91 oat_code = reinterpret_cast<const void*>(oat_code_aligned);
92
93 const std::vector<uint8_t>& code = compiled_method->GetCode();
94 size_t code_size = code.size() * sizeof(code[0]);
95 EXPECT_EQ(0, memcmp(oat_code, &code[0], code_size))
96 << PrettyMethod(method) << " " << code_size;
97 CHECK_EQ(0, memcmp(oat_code, &code[0], code_size));
98 EXPECT_EQ(oat_method.frame_size_in_bytes_, compiled_method->GetFrameSizeInBytes());
99 EXPECT_EQ(oat_method.return_pc_offset_in_bytes_, compiled_method->GetReturnPcOffsetInBytes());
100 EXPECT_EQ(oat_method.core_spill_mask_, compiled_method->GetCoreSpillMask());
101 EXPECT_EQ(oat_method.fp_spill_mask_, compiled_method->GetFpSpillMask());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700102 }
103 }
104 }
105}
106
107} // namespace art