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