Home | History | Annotate | Download | only in runtime
      1 /*
      2  * Copyright (C) 2014 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 <jni.h>
     18 #include <vector>
     19 
     20 #include "art_field-inl.h"
     21 #include "base/enums.h"
     22 #include "class_linker-inl.h"
     23 #include "common_compiler_test.h"
     24 #include "mirror/class-inl.h"
     25 #include "mirror/field-inl.h"
     26 #include "mirror/method.h"
     27 #include "scoped_thread_state_change-inl.h"
     28 
     29 namespace art {
     30 
     31 class ProxyTest : public CommonCompilerTest {
     32  public:
     33   // Generate a proxy class with the given name and interfaces. This is a simplification from what
     34   // libcore does to fit to our test needs. We do not check for duplicated interfaces or methods and
     35   // we do not declare exceptions.
     36   mirror::Class* GenerateProxyClass(ScopedObjectAccess& soa, jobject jclass_loader,
     37                                     const char* className,
     38                                     const std::vector<mirror::Class*>& interfaces)
     39       REQUIRES_SHARED(Locks::mutator_lock_) {
     40     mirror::Class* javaLangObject = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
     41     CHECK(javaLangObject != nullptr);
     42 
     43     jclass javaLangClass = soa.AddLocalReference<jclass>(mirror::Class::GetJavaLangClass());
     44 
     45     // Builds the interfaces array.
     46     jobjectArray proxyClassInterfaces = soa.Env()->NewObjectArray(interfaces.size(), javaLangClass,
     47                                                                   nullptr);
     48     soa.Self()->AssertNoPendingException();
     49     for (size_t i = 0; i < interfaces.size(); ++i) {
     50       soa.Env()->SetObjectArrayElement(proxyClassInterfaces, i,
     51                                        soa.AddLocalReference<jclass>(interfaces[i]));
     52     }
     53 
     54     // Builds the method array.
     55     jsize methods_count = 3;  // Object.equals, Object.hashCode and Object.toString.
     56     for (mirror::Class* interface : interfaces) {
     57       methods_count += interface->NumVirtualMethods();
     58     }
     59     jobjectArray proxyClassMethods = soa.Env()->NewObjectArray(
     60         methods_count, soa.AddLocalReference<jclass>(mirror::Method::StaticClass()), nullptr);
     61     soa.Self()->AssertNoPendingException();
     62 
     63     jsize array_index = 0;
     64     // Fill the method array
     65     DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
     66     ArtMethod* method = javaLangObject->FindDeclaredVirtualMethod(
     67         "equals", "(Ljava/lang/Object;)Z", kRuntimePointerSize);
     68     CHECK(method != nullptr);
     69     DCHECK(!Runtime::Current()->IsActiveTransaction());
     70     soa.Env()->SetObjectArrayElement(
     71         proxyClassMethods, array_index++, soa.AddLocalReference<jobject>(
     72             mirror::Method::CreateFromArtMethod<kRuntimePointerSize, false>(soa.Self(), method)));
     73     method = javaLangObject->FindDeclaredVirtualMethod("hashCode", "()I", kRuntimePointerSize);
     74     CHECK(method != nullptr);
     75     soa.Env()->SetObjectArrayElement(
     76         proxyClassMethods, array_index++, soa.AddLocalReference<jobject>(
     77             mirror::Method::CreateFromArtMethod<kRuntimePointerSize, false>(soa.Self(), method)));
     78     method = javaLangObject->FindDeclaredVirtualMethod(
     79         "toString", "()Ljava/lang/String;", kRuntimePointerSize);
     80     CHECK(method != nullptr);
     81     soa.Env()->SetObjectArrayElement(
     82         proxyClassMethods, array_index++, soa.AddLocalReference<jobject>(
     83             mirror::Method::CreateFromArtMethod<kRuntimePointerSize, false>(soa.Self(), method)));
     84     // Now adds all interfaces virtual methods.
     85     for (mirror::Class* interface : interfaces) {
     86       for (auto& m : interface->GetDeclaredVirtualMethods(kRuntimePointerSize)) {
     87         soa.Env()->SetObjectArrayElement(
     88             proxyClassMethods, array_index++, soa.AddLocalReference<jobject>(
     89                 mirror::Method::CreateFromArtMethod<kRuntimePointerSize, false>(soa.Self(), &m)));
     90       }
     91     }
     92     CHECK_EQ(array_index, methods_count);
     93 
     94     // Builds an empty exception array.
     95     jobjectArray proxyClassThrows = soa.Env()->NewObjectArray(0, javaLangClass, nullptr);
     96     soa.Self()->AssertNoPendingException();
     97 
     98     mirror::Class* proxyClass = class_linker_->CreateProxyClass(
     99         soa, soa.Env()->NewStringUTF(className), proxyClassInterfaces, jclass_loader,
    100         proxyClassMethods, proxyClassThrows);
    101     soa.Self()->AssertNoPendingException();
    102     return proxyClass;
    103   }
    104 };
    105 
    106 // Creates a proxy class and check ClassHelper works correctly.
    107 TEST_F(ProxyTest, ProxyClassHelper) {
    108   ScopedObjectAccess soa(Thread::Current());
    109   jobject jclass_loader = LoadDex("Interfaces");
    110   StackHandleScope<4> hs(soa.Self());
    111   Handle<mirror::ClassLoader> class_loader(
    112       hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
    113 
    114   Handle<mirror::Class> I(hs.NewHandle(
    115       class_linker_->FindClass(soa.Self(), "LInterfaces$I;", class_loader)));
    116   Handle<mirror::Class> J(hs.NewHandle(
    117       class_linker_->FindClass(soa.Self(), "LInterfaces$J;", class_loader)));
    118   ASSERT_TRUE(I != nullptr);
    119   ASSERT_TRUE(J != nullptr);
    120 
    121   std::vector<mirror::Class*> interfaces;
    122   interfaces.push_back(I.Get());
    123   interfaces.push_back(J.Get());
    124   Handle<mirror::Class> proxy_class(hs.NewHandle(
    125       GenerateProxyClass(soa, jclass_loader, "$Proxy1234", interfaces)));
    126   interfaces.clear();  // Don't least possibly stale objects in the array as good practice.
    127   ASSERT_TRUE(proxy_class != nullptr);
    128   ASSERT_TRUE(proxy_class->IsProxyClass());
    129   ASSERT_TRUE(proxy_class->IsInitialized());
    130 
    131   EXPECT_EQ(2U, proxy_class->NumDirectInterfaces());  // Interfaces$I and Interfaces$J.
    132   EXPECT_OBJ_PTR_EQ(I.Get(), mirror::Class::GetDirectInterface(soa.Self(), proxy_class.Get(), 0));
    133   EXPECT_OBJ_PTR_EQ(J.Get(), mirror::Class::GetDirectInterface(soa.Self(), proxy_class.Get(), 1));
    134   std::string temp;
    135   const char* proxy_class_descriptor = proxy_class->GetDescriptor(&temp);
    136   EXPECT_STREQ("L$Proxy1234;", proxy_class_descriptor);
    137   EXPECT_EQ(nullptr, proxy_class->GetSourceFile());
    138 }
    139 
    140 // Creates a proxy class and check FieldHelper works correctly.
    141 TEST_F(ProxyTest, ProxyFieldHelper) {
    142   ScopedObjectAccess soa(Thread::Current());
    143   jobject jclass_loader = LoadDex("Interfaces");
    144   StackHandleScope<9> hs(soa.Self());
    145   Handle<mirror::ClassLoader> class_loader(
    146       hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
    147 
    148   Handle<mirror::Class> I(hs.NewHandle(
    149       class_linker_->FindClass(soa.Self(), "LInterfaces$I;", class_loader)));
    150   Handle<mirror::Class> J(hs.NewHandle(
    151       class_linker_->FindClass(soa.Self(), "LInterfaces$J;", class_loader)));
    152   ASSERT_TRUE(I != nullptr);
    153   ASSERT_TRUE(J != nullptr);
    154 
    155   Handle<mirror::Class> proxyClass;
    156   {
    157     std::vector<mirror::Class*> interfaces;
    158     interfaces.push_back(I.Get());
    159     interfaces.push_back(J.Get());
    160     proxyClass = hs.NewHandle(GenerateProxyClass(soa, jclass_loader, "$Proxy1234", interfaces));
    161   }
    162 
    163   ASSERT_TRUE(proxyClass != nullptr);
    164   ASSERT_TRUE(proxyClass->IsProxyClass());
    165   ASSERT_TRUE(proxyClass->IsInitialized());
    166 
    167   EXPECT_TRUE(proxyClass->GetIFieldsPtr() == nullptr);
    168 
    169   LengthPrefixedArray<ArtField>* static_fields = proxyClass->GetSFieldsPtr();
    170   ASSERT_TRUE(static_fields != nullptr);
    171   ASSERT_EQ(2u, proxyClass->NumStaticFields());
    172 
    173   Handle<mirror::Class> interfacesFieldClass(
    174       hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/Class;")));
    175   ASSERT_TRUE(interfacesFieldClass != nullptr);
    176   Handle<mirror::Class> throwsFieldClass(
    177       hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "[[Ljava/lang/Class;")));
    178   ASSERT_TRUE(throwsFieldClass != nullptr);
    179 
    180   // Test "Class[] interfaces" field.
    181   ArtField* field = &static_fields->At(0);
    182   EXPECT_STREQ("interfaces", field->GetName());
    183   EXPECT_STREQ("[Ljava/lang/Class;", field->GetTypeDescriptor());
    184   EXPECT_OBJ_PTR_EQ(interfacesFieldClass.Get(), field->GetType<true>());
    185   std::string temp;
    186   EXPECT_STREQ("L$Proxy1234;", field->GetDeclaringClass()->GetDescriptor(&temp));
    187   EXPECT_FALSE(field->IsPrimitiveType());
    188 
    189   // Test "Class[][] throws" field.
    190   field = &static_fields->At(1);
    191   EXPECT_STREQ("throws", field->GetName());
    192   EXPECT_STREQ("[[Ljava/lang/Class;", field->GetTypeDescriptor());
    193   EXPECT_OBJ_PTR_EQ(throwsFieldClass.Get(), field->GetType<true>());
    194   EXPECT_STREQ("L$Proxy1234;", field->GetDeclaringClass()->GetDescriptor(&temp));
    195   EXPECT_FALSE(field->IsPrimitiveType());
    196 }
    197 
    198 // Creates two proxy classes and check the art/mirror fields of their static fields.
    199 TEST_F(ProxyTest, CheckArtMirrorFieldsOfProxyStaticFields) {
    200   ScopedObjectAccess soa(Thread::Current());
    201   jobject jclass_loader = LoadDex("Interfaces");
    202   StackHandleScope<7> hs(soa.Self());
    203 
    204   Handle<mirror::Class> proxyClass0;
    205   Handle<mirror::Class> proxyClass1;
    206   {
    207     std::vector<mirror::Class*> interfaces;
    208     proxyClass0 = hs.NewHandle(GenerateProxyClass(soa, jclass_loader, "$Proxy0", interfaces));
    209     proxyClass1 = hs.NewHandle(GenerateProxyClass(soa, jclass_loader, "$Proxy1", interfaces));
    210   }
    211 
    212   ASSERT_TRUE(proxyClass0 != nullptr);
    213   ASSERT_TRUE(proxyClass0->IsProxyClass());
    214   ASSERT_TRUE(proxyClass0->IsInitialized());
    215   ASSERT_TRUE(proxyClass1 != nullptr);
    216   ASSERT_TRUE(proxyClass1->IsProxyClass());
    217   ASSERT_TRUE(proxyClass1->IsInitialized());
    218 
    219   LengthPrefixedArray<ArtField>* static_fields0 = proxyClass0->GetSFieldsPtr();
    220   ASSERT_TRUE(static_fields0 != nullptr);
    221   ASSERT_EQ(2u, static_fields0->size());
    222   LengthPrefixedArray<ArtField>* static_fields1 = proxyClass1->GetSFieldsPtr();
    223   ASSERT_TRUE(static_fields1 != nullptr);
    224   ASSERT_EQ(2u, static_fields1->size());
    225 
    226   EXPECT_OBJ_PTR_EQ(static_fields0->At(0).GetDeclaringClass(), proxyClass0.Get());
    227   EXPECT_OBJ_PTR_EQ(static_fields0->At(1).GetDeclaringClass(), proxyClass0.Get());
    228   EXPECT_OBJ_PTR_EQ(static_fields1->At(0).GetDeclaringClass(), proxyClass1.Get());
    229   EXPECT_OBJ_PTR_EQ(static_fields1->At(1).GetDeclaringClass(), proxyClass1.Get());
    230 
    231   ASSERT_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
    232   ASSERT_FALSE(Runtime::Current()->IsActiveTransaction());
    233   Handle<mirror::Field> field00 =
    234       hs.NewHandle(mirror::Field::CreateFromArtField<kRuntimePointerSize, false>(
    235           soa.Self(), &static_fields0->At(0), true));
    236   Handle<mirror::Field> field01 =
    237       hs.NewHandle(mirror::Field::CreateFromArtField<kRuntimePointerSize, false>(
    238           soa.Self(), &static_fields0->At(1), true));
    239   Handle<mirror::Field> field10 =
    240       hs.NewHandle(mirror::Field::CreateFromArtField<kRuntimePointerSize, false>(
    241           soa.Self(), &static_fields1->At(0), true));
    242   Handle<mirror::Field> field11 =
    243       hs.NewHandle(mirror::Field::CreateFromArtField<kRuntimePointerSize, false>(
    244           soa.Self(), &static_fields1->At(1), true));
    245   EXPECT_EQ(field00->GetArtField(), &static_fields0->At(0));
    246   EXPECT_EQ(field01->GetArtField(), &static_fields0->At(1));
    247   EXPECT_EQ(field10->GetArtField(), &static_fields1->At(0));
    248   EXPECT_EQ(field11->GetArtField(), &static_fields1->At(1));
    249 }
    250 
    251 }  // namespace art
    252