1 /*
2 * Copyright (C) 2019 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 <cstdio>
18 #include <memory>
19 #include <mutex>
20 #include <string>
21 #include <vector>
22
23 #include "class_linker.h"
24 #include "class_root.h"
25 #include "jni.h"
26 #include "jni/jni_internal.h"
27 #include "mirror/class.h"
28 #include "mirror/method_handle_impl.h"
29 #include "mirror/object-inl.h"
30 #include "mirror/object_array-alloc-inl.h"
31 #include "reflection.h"
32 #include "reflective_handle.h"
33 #include "reflective_handle_scope-inl.h"
34 #include "runtime.h"
35 #include "scoped_thread_state_change-inl.h"
36 #include "thread-inl.h"
37
38 namespace art {
39 namespace Test1985StructuralRedefineStackScope {
40
Java_Main_NativeFieldScopeCheck(JNIEnv * env,jclass,jobject field,jobject runnable)41 extern "C" JNICALL jobject JNIEXPORT Java_Main_NativeFieldScopeCheck(JNIEnv* env,
42 jclass,
43 jobject field,
44 jobject runnable) {
45 jfieldID fid = env->FromReflectedField(field);
46 jclass runnable_klass = env->FindClass("java/lang/Runnable");
47 jmethodID run = env->GetMethodID(runnable_klass, "run", "()V");
48 ScopedObjectAccess soa(Thread::Current());
49 StackHandleScope<4> hs(soa.Self());
50 StackArtFieldHandleScope<1> fhs(soa.Self());
51 StackArtFieldHandleScope<1> bhs(soa.Self());
52 ReflectiveHandle<ArtField> rf(fhs.NewHandle(jni::DecodeArtField(fid)));
53 ReflectiveHandle<ArtField> bf(bhs.NewHandle(jni::DecodeArtField(fid)));
54 ArtField* pre_ptr = rf.Get();
55 {
56 ScopedThreadSuspension sts(soa.Self(), ThreadState::kNative);
57 // Upcall to perform redefinition.
58 env->CallVoidMethod(runnable, run);
59 }
60 Handle<mirror::ObjectArray<mirror::Class>> mt_arr(
61 hs.NewHandle(mirror::ObjectArray<mirror::Class>::Alloc(
62 soa.Self(),
63 Runtime::Current()->GetClassLinker()->FindArrayClass(soa.Self(),
64 GetClassRoot<mirror::Class>()),
65 0)));
66 Handle<mirror::MethodType> mt(hs.NewHandle(mirror::MethodType::Create(
67 soa.Self(), hs.NewHandle(GetClassRoot<mirror::Object>()), mt_arr)));
68 Handle<mirror::MethodHandleImpl> mhi(hs.NewHandle(
69 mirror::MethodHandleImpl::Create(soa.Self(),
70 reinterpret_cast<uintptr_t>(rf.Get()),
71 (rf->IsStatic() ? mirror::MethodHandle::Kind::kStaticGet
72 : mirror::MethodHandle::Kind::kInstanceGet),
73 mt)));
74 CHECK_EQ(rf.Get(), bf.Get()) << "rf: " << rf->PrettyField() << " bf: " << bf->PrettyField();
75 // TODO Modify this to work for when run doesn't cause a change.
76 CHECK_NE(pre_ptr, rf.Get()) << "pre_ptr: " << pre_ptr->PrettyField()
77 << " rf: " << rf->PrettyField();
78 CHECK_EQ(fid, jni::EncodeArtField(rf));
79 return soa.AddLocalReference<jobject>(mhi.Get());
80 }
81
82 } // namespace Test1985StructuralRedefineStackScope
83 } // namespace art
84