Home | History | Annotate | Download | only in runtime
      1 /*
      2  * Copyright (C) 2011 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 #include <memory>
     18 
     19 #include "class_linker.h"
     20 #include "common_runtime_test.h"
     21 #include "dex_file.h"
     22 #include "dex_file-inl.h"
     23 #include "gtest/gtest.h"
     24 #include "leb128.h"
     25 #include "mirror/class-inl.h"
     26 #include "mirror/object_array-inl.h"
     27 #include "mirror/object-inl.h"
     28 #include "mirror/stack_trace_element.h"
     29 #include "runtime.h"
     30 #include "scoped_thread_state_change.h"
     31 #include "handle_scope-inl.h"
     32 #include "thread.h"
     33 #include "vmap_table.h"
     34 
     35 namespace art {
     36 
     37 class ExceptionTest : public CommonRuntimeTest {
     38  protected:
     39   virtual void SetUp() {
     40     CommonRuntimeTest::SetUp();
     41 
     42     ScopedObjectAccess soa(Thread::Current());
     43     StackHandleScope<2> hs(soa.Self());
     44     Handle<mirror::ClassLoader> class_loader(
     45         hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("ExceptionHandle"))));
     46     my_klass_ = class_linker_->FindClass(soa.Self(), "LExceptionHandle;", class_loader);
     47     ASSERT_TRUE(my_klass_ != nullptr);
     48     Handle<mirror::Class> klass(hs.NewHandle(my_klass_));
     49     class_linker_->EnsureInitialized(soa.Self(), klass, true, true);
     50     my_klass_ = klass.Get();
     51 
     52     dex_ = my_klass_->GetDexCache()->GetDexFile();
     53 
     54     uint32_t code_size = 12;
     55     for (size_t i = 0 ; i < code_size; i++) {
     56       fake_code_.push_back(0x70 | i);
     57     }
     58 
     59     fake_mapping_data_.PushBackUnsigned(4);  // first element is count
     60     fake_mapping_data_.PushBackUnsigned(4);  // total (non-length) elements
     61     fake_mapping_data_.PushBackUnsigned(2);  // count of pc to dex elements
     62                                       // ---  pc to dex table
     63     fake_mapping_data_.PushBackUnsigned(3 - 0);  // offset 3
     64     fake_mapping_data_.PushBackSigned(3 - 0);    // maps to dex offset 3
     65                                       // ---  dex to pc table
     66     fake_mapping_data_.PushBackUnsigned(3 - 0);  // offset 3
     67     fake_mapping_data_.PushBackSigned(3 - 0);    // maps to dex offset 3
     68 
     69     fake_vmap_table_data_.PushBackUnsigned(0 + VmapTable::kEntryAdjustment);
     70 
     71     fake_gc_map_.push_back(0);  // 0 bytes to encode references and native pc offsets.
     72     fake_gc_map_.push_back(0);
     73     fake_gc_map_.push_back(0);  // 0 entries.
     74     fake_gc_map_.push_back(0);
     75 
     76     const std::vector<uint8_t>& fake_vmap_table_data = fake_vmap_table_data_.GetData();
     77     const std::vector<uint8_t>& fake_mapping_data = fake_mapping_data_.GetData();
     78     uint32_t vmap_table_offset = sizeof(OatQuickMethodHeader) + fake_vmap_table_data.size();
     79     uint32_t mapping_table_offset = vmap_table_offset + fake_mapping_data.size();
     80     uint32_t gc_map_offset = mapping_table_offset + fake_gc_map_.size();
     81     OatQuickMethodHeader method_header(mapping_table_offset, vmap_table_offset, gc_map_offset,
     82                                        4 * sizeof(void*), 0u, 0u, code_size);
     83     fake_header_code_and_maps_.resize(sizeof(method_header));
     84     memcpy(&fake_header_code_and_maps_[0], &method_header, sizeof(method_header));
     85     fake_header_code_and_maps_.insert(fake_header_code_and_maps_.begin(),
     86                                       fake_vmap_table_data.begin(), fake_vmap_table_data.end());
     87     fake_header_code_and_maps_.insert(fake_header_code_and_maps_.begin(),
     88                                       fake_mapping_data.begin(), fake_mapping_data.end());
     89     fake_header_code_and_maps_.insert(fake_header_code_and_maps_.begin(),
     90                                       fake_gc_map_.begin(), fake_gc_map_.end());
     91     fake_header_code_and_maps_.insert(fake_header_code_and_maps_.end(),
     92                                       fake_code_.begin(), fake_code_.end());
     93 
     94     // NOTE: Don't align the code (it will not be executed) but check that the Thumb2
     95     // adjustment will be a NOP, see ArtMethod::EntryPointToCodePointer().
     96     CHECK_EQ(mapping_table_offset & 1u, 0u);
     97     const uint8_t* code_ptr = &fake_header_code_and_maps_[gc_map_offset];
     98 
     99     method_f_ = my_klass_->FindVirtualMethod("f", "()I", sizeof(void*));
    100     ASSERT_TRUE(method_f_ != nullptr);
    101     method_f_->SetEntryPointFromQuickCompiledCode(code_ptr);
    102 
    103     method_g_ = my_klass_->FindVirtualMethod("g", "(I)V", sizeof(void*));
    104     ASSERT_TRUE(method_g_ != nullptr);
    105     method_g_->SetEntryPointFromQuickCompiledCode(code_ptr);
    106   }
    107 
    108   const DexFile* dex_;
    109 
    110   std::vector<uint8_t> fake_code_;
    111   Leb128EncodingVector fake_mapping_data_;
    112   Leb128EncodingVector fake_vmap_table_data_;
    113   std::vector<uint8_t> fake_gc_map_;
    114   std::vector<uint8_t> fake_header_code_and_maps_;
    115 
    116   ArtMethod* method_f_;
    117   ArtMethod* method_g_;
    118 
    119  private:
    120   mirror::Class* my_klass_;
    121 };
    122 
    123 TEST_F(ExceptionTest, FindCatchHandler) {
    124   ScopedObjectAccess soa(Thread::Current());
    125   const DexFile::CodeItem* code_item = dex_->GetCodeItem(method_f_->GetCodeItemOffset());
    126 
    127   ASSERT_TRUE(code_item != nullptr);
    128 
    129   ASSERT_EQ(2u, code_item->tries_size_);
    130   ASSERT_NE(0u, code_item->insns_size_in_code_units_);
    131 
    132   const DexFile::TryItem *t0, *t1;
    133   t0 = dex_->GetTryItems(*code_item, 0);
    134   t1 = dex_->GetTryItems(*code_item, 1);
    135   EXPECT_LE(t0->start_addr_, t1->start_addr_);
    136   {
    137     CatchHandlerIterator iter(*code_item, 4 /* Dex PC in the first try block */);
    138     EXPECT_STREQ("Ljava/io/IOException;", dex_->StringByTypeIdx(iter.GetHandlerTypeIndex()));
    139     ASSERT_TRUE(iter.HasNext());
    140     iter.Next();
    141     EXPECT_STREQ("Ljava/lang/Exception;", dex_->StringByTypeIdx(iter.GetHandlerTypeIndex()));
    142     ASSERT_TRUE(iter.HasNext());
    143     iter.Next();
    144     EXPECT_FALSE(iter.HasNext());
    145   }
    146   {
    147     CatchHandlerIterator iter(*code_item, 8 /* Dex PC in the second try block */);
    148     EXPECT_STREQ("Ljava/io/IOException;", dex_->StringByTypeIdx(iter.GetHandlerTypeIndex()));
    149     ASSERT_TRUE(iter.HasNext());
    150     iter.Next();
    151     EXPECT_FALSE(iter.HasNext());
    152   }
    153   {
    154     CatchHandlerIterator iter(*code_item, 11 /* Dex PC not in any try block */);
    155     EXPECT_FALSE(iter.HasNext());
    156   }
    157 }
    158 
    159 TEST_F(ExceptionTest, StackTraceElement) {
    160   Thread* thread = Thread::Current();
    161   thread->TransitionFromSuspendedToRunnable();
    162   bool started = runtime_->Start();
    163   CHECK(started);
    164   JNIEnv* env = thread->GetJniEnv();
    165   ScopedObjectAccess soa(env);
    166 
    167   std::vector<uintptr_t> fake_stack;
    168   Runtime* r = Runtime::Current();
    169   r->SetInstructionSet(kRuntimeISA);
    170   ArtMethod* save_method = r->CreateCalleeSaveMethod();
    171   r->SetCalleeSaveMethod(save_method, Runtime::kSaveAll);
    172   QuickMethodFrameInfo frame_info = save_method->GetQuickFrameInfo();
    173 
    174   ASSERT_EQ(kStackAlignment, 16U);
    175   // ASSERT_EQ(sizeof(uintptr_t), sizeof(uint32_t));
    176 
    177 
    178   // Create three fake stack frames with mapping data created in SetUp. We map offset 3 in the
    179   // code to dex pc 3.
    180   const uint32_t dex_pc = 3;
    181 
    182   // Create the stack frame for the callee save method, expected by the runtime.
    183   fake_stack.push_back(reinterpret_cast<uintptr_t>(save_method));
    184   for (size_t i = 0; i < frame_info.FrameSizeInBytes() - 2 * sizeof(uintptr_t);
    185        i += sizeof(uintptr_t)) {
    186     fake_stack.push_back(0);
    187   }
    188 
    189   fake_stack.push_back(method_g_->ToNativeQuickPc(dex_pc));  // return pc
    190 
    191   // Create/push fake 16byte stack frame for method g
    192   fake_stack.push_back(reinterpret_cast<uintptr_t>(method_g_));
    193   fake_stack.push_back(0);
    194   fake_stack.push_back(0);
    195   fake_stack.push_back(method_f_->ToNativeQuickPc(dex_pc));  // return pc
    196 
    197   // Create/push fake 16byte stack frame for method f
    198   fake_stack.push_back(reinterpret_cast<uintptr_t>(method_f_));
    199   fake_stack.push_back(0);
    200   fake_stack.push_back(0);
    201   fake_stack.push_back(0xEBAD6070);  // return pc
    202 
    203   // Push Method* of null to terminate the trace
    204   fake_stack.push_back(0);
    205 
    206   // Push null values which will become null incoming arguments.
    207   fake_stack.push_back(0);
    208   fake_stack.push_back(0);
    209   fake_stack.push_back(0);
    210 
    211   // Set up thread to appear as if we called out of method_g_ at pc dex 3
    212   thread->SetTopOfStack(reinterpret_cast<ArtMethod**>(&fake_stack[0]));
    213 
    214   jobject internal = thread->CreateInternalStackTrace<false>(soa);
    215   ASSERT_TRUE(internal != nullptr);
    216   jobjectArray ste_array = Thread::InternalStackTraceToStackTraceElementArray(soa, internal);
    217   ASSERT_TRUE(ste_array != nullptr);
    218   auto* trace_array = soa.Decode<mirror::ObjectArray<mirror::StackTraceElement>*>(ste_array);
    219 
    220   ASSERT_TRUE(trace_array != nullptr);
    221   ASSERT_TRUE(trace_array->Get(0) != nullptr);
    222   EXPECT_STREQ("ExceptionHandle",
    223                trace_array->Get(0)->GetDeclaringClass()->ToModifiedUtf8().c_str());
    224   EXPECT_STREQ("ExceptionHandle.java",
    225                trace_array->Get(0)->GetFileName()->ToModifiedUtf8().c_str());
    226   EXPECT_STREQ("g", trace_array->Get(0)->GetMethodName()->ToModifiedUtf8().c_str());
    227   EXPECT_EQ(37, trace_array->Get(0)->GetLineNumber());
    228 
    229   ASSERT_TRUE(trace_array->Get(1) != nullptr);
    230   EXPECT_STREQ("ExceptionHandle",
    231                trace_array->Get(1)->GetDeclaringClass()->ToModifiedUtf8().c_str());
    232   EXPECT_STREQ("ExceptionHandle.java",
    233                trace_array->Get(1)->GetFileName()->ToModifiedUtf8().c_str());
    234   EXPECT_STREQ("f", trace_array->Get(1)->GetMethodName()->ToModifiedUtf8().c_str());
    235   EXPECT_EQ(22, trace_array->Get(1)->GetLineNumber());
    236 
    237   thread->SetTopOfStack(nullptr);  // Disarm the assertion that no code is running when we detach.
    238 }
    239 
    240 }  // namespace art
    241