David Brazdil | 1f9d3c3 | 2018-05-02 16:53:06 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | #ifndef ART_RUNTIME_PROXY_TEST_H_ |
| 18 | #define ART_RUNTIME_PROXY_TEST_H_ |
| 19 | |
| 20 | #include <jni.h> |
| 21 | #include <vector> |
| 22 | |
| 23 | #include "art_method-inl.h" |
| 24 | #include "class_linker-inl.h" |
Vladimir Marko | 679730e | 2018-05-25 15:06:48 +0100 | [diff] [blame] | 25 | #include "class_root.h" |
David Brazdil | 1f9d3c3 | 2018-05-02 16:53:06 +0100 | [diff] [blame] | 26 | #include "mirror/class-inl.h" |
| 27 | #include "mirror/method.h" |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame^] | 28 | #include "obj_ptr-inl.h" |
David Brazdil | 1f9d3c3 | 2018-05-02 16:53:06 +0100 | [diff] [blame] | 29 | |
| 30 | namespace art { |
| 31 | namespace proxy_test { |
| 32 | |
| 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. |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame^] | 36 | ObjPtr<mirror::Class> GenerateProxyClass(ScopedObjectAccess& soa, |
| 37 | jobject jclass_loader, |
| 38 | ClassLinker* class_linker, |
| 39 | const char* className, |
| 40 | const std::vector<Handle<mirror::Class>>& interfaces) |
David Brazdil | 1f9d3c3 | 2018-05-02 16:53:06 +0100 | [diff] [blame] | 41 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 42 | StackHandleScope<1> hs(soa.Self()); |
| 43 | Handle<mirror::Class> javaLangObject = hs.NewHandle( |
| 44 | class_linker->FindSystemClass(soa.Self(), "Ljava/lang/Object;")); |
David Brazdil | 1f9d3c3 | 2018-05-02 16:53:06 +0100 | [diff] [blame] | 45 | CHECK(javaLangObject != nullptr); |
| 46 | |
| 47 | jclass javaLangClass = soa.AddLocalReference<jclass>(mirror::Class::GetJavaLangClass()); |
| 48 | |
| 49 | // Builds the interfaces array. |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame^] | 50 | jobjectArray proxyClassInterfaces = |
| 51 | soa.Env()->NewObjectArray(interfaces.size(), javaLangClass, /* initialElement */ nullptr); |
David Brazdil | 1f9d3c3 | 2018-05-02 16:53:06 +0100 | [diff] [blame] | 52 | soa.Self()->AssertNoPendingException(); |
| 53 | for (size_t i = 0; i < interfaces.size(); ++i) { |
| 54 | soa.Env()->SetObjectArrayElement(proxyClassInterfaces, i, |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame^] | 55 | soa.AddLocalReference<jclass>(interfaces[i].Get())); |
David Brazdil | 1f9d3c3 | 2018-05-02 16:53:06 +0100 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | // Builds the method array. |
| 59 | jsize methods_count = 3; // Object.equals, Object.hashCode and Object.toString. |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame^] | 60 | for (Handle<mirror::Class> interface : interfaces) { |
David Brazdil | 1f9d3c3 | 2018-05-02 16:53:06 +0100 | [diff] [blame] | 61 | methods_count += interface->NumVirtualMethods(); |
| 62 | } |
| 63 | jobjectArray proxyClassMethods = soa.Env()->NewObjectArray( |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame^] | 64 | methods_count, |
| 65 | soa.AddLocalReference<jclass>(GetClassRoot<mirror::Method>()), |
| 66 | /* initialElement */ nullptr); |
David Brazdil | 1f9d3c3 | 2018-05-02 16:53:06 +0100 | [diff] [blame] | 67 | soa.Self()->AssertNoPendingException(); |
| 68 | |
| 69 | jsize array_index = 0; |
| 70 | // Fill the method array |
| 71 | DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize); |
| 72 | ArtMethod* method = javaLangObject->FindClassMethod( |
| 73 | "equals", "(Ljava/lang/Object;)Z", kRuntimePointerSize); |
| 74 | CHECK(method != nullptr); |
| 75 | CHECK(!method->IsDirect()); |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 76 | CHECK(method->GetDeclaringClass() == javaLangObject.Get()); |
David Brazdil | 1f9d3c3 | 2018-05-02 16:53:06 +0100 | [diff] [blame] | 77 | DCHECK(!Runtime::Current()->IsActiveTransaction()); |
| 78 | soa.Env()->SetObjectArrayElement( |
| 79 | proxyClassMethods, array_index++, soa.AddLocalReference<jobject>( |
| 80 | mirror::Method::CreateFromArtMethod<kRuntimePointerSize, false>(soa.Self(), method))); |
| 81 | method = javaLangObject->FindClassMethod("hashCode", "()I", kRuntimePointerSize); |
| 82 | CHECK(method != nullptr); |
| 83 | CHECK(!method->IsDirect()); |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 84 | CHECK(method->GetDeclaringClass() == javaLangObject.Get()); |
David Brazdil | 1f9d3c3 | 2018-05-02 16:53:06 +0100 | [diff] [blame] | 85 | soa.Env()->SetObjectArrayElement( |
| 86 | proxyClassMethods, array_index++, soa.AddLocalReference<jobject>( |
| 87 | mirror::Method::CreateFromArtMethod<kRuntimePointerSize, false>(soa.Self(), method))); |
| 88 | method = javaLangObject->FindClassMethod( |
| 89 | "toString", "()Ljava/lang/String;", kRuntimePointerSize); |
| 90 | CHECK(method != nullptr); |
| 91 | CHECK(!method->IsDirect()); |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 92 | CHECK(method->GetDeclaringClass() == javaLangObject.Get()); |
David Brazdil | 1f9d3c3 | 2018-05-02 16:53:06 +0100 | [diff] [blame] | 93 | soa.Env()->SetObjectArrayElement( |
| 94 | proxyClassMethods, array_index++, soa.AddLocalReference<jobject>( |
| 95 | mirror::Method::CreateFromArtMethod<kRuntimePointerSize, false>(soa.Self(), method))); |
| 96 | // Now adds all interfaces virtual methods. |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame^] | 97 | for (Handle<mirror::Class> interface : interfaces) { |
David Brazdil | 1f9d3c3 | 2018-05-02 16:53:06 +0100 | [diff] [blame] | 98 | for (auto& m : interface->GetDeclaredVirtualMethods(kRuntimePointerSize)) { |
| 99 | soa.Env()->SetObjectArrayElement( |
| 100 | proxyClassMethods, array_index++, soa.AddLocalReference<jobject>( |
| 101 | mirror::Method::CreateFromArtMethod<kRuntimePointerSize, false>(soa.Self(), &m))); |
| 102 | } |
| 103 | } |
| 104 | CHECK_EQ(array_index, methods_count); |
| 105 | |
| 106 | // Builds an empty exception array. |
| 107 | jobjectArray proxyClassThrows = soa.Env()->NewObjectArray(0, javaLangClass, nullptr); |
| 108 | soa.Self()->AssertNoPendingException(); |
| 109 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame^] | 110 | ObjPtr<mirror::Class> proxyClass = class_linker->CreateProxyClass( |
| 111 | soa, |
| 112 | soa.Env()->NewStringUTF(className), |
| 113 | proxyClassInterfaces, |
| 114 | jclass_loader, |
| 115 | proxyClassMethods, |
| 116 | proxyClassThrows); |
David Brazdil | 1f9d3c3 | 2018-05-02 16:53:06 +0100 | [diff] [blame] | 117 | soa.Self()->AssertNoPendingException(); |
| 118 | return proxyClass; |
| 119 | } |
| 120 | |
| 121 | } // namespace proxy_test |
| 122 | } // namespace art |
| 123 | |
| 124 | #endif // ART_RUNTIME_PROXY_TEST_H_ |