1 /*
2  * Copyright (C) 2015 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 
19 #include <iostream>
20 
21 #include "art_method.h"
22 #include "base/casts.h"
23 #include "class_linker.h"
24 #include "jit/jit.h"
25 #include "linear_alloc.h"
26 #include "nativehelper/ScopedUtfChars.h"
27 #include "runtime.h"
28 #include "scoped_thread_state_change-inl.h"
29 #include "thread-current-inl.h"
30 
31 namespace art {
32 namespace {
33 
34 class FindPointerAllocatorVisitor : public AllocatorVisitor {
35  public:
FindPointerAllocatorVisitor(void * ptr)36   explicit FindPointerAllocatorVisitor(void* ptr) : is_found(false), ptr_(ptr) {}
37 
Visit(LinearAlloc * alloc)38   bool Visit(LinearAlloc* alloc)
39       REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) override {
40     is_found = alloc->Contains(ptr_);
41     return !is_found;
42   }
43 
44   bool is_found;
45 
46  private:
47   void* ptr_;
48 };
49 
Java_Main_getArtMethod(JNIEnv * env,jclass,jobject java_method)50 extern "C" JNIEXPORT jlong JNICALL Java_Main_getArtMethod(JNIEnv* env,
51                                                           jclass,
52                                                           jobject java_method) {
53   ScopedObjectAccess soa(env);
54   ArtMethod* method = ArtMethod::FromReflectedMethod(soa, java_method);
55   return reinterpret_cast64<jlong>(method);
56 }
57 
Java_Main_reuseArenaOfMethod(JNIEnv *,jclass,jlong art_method)58 extern "C" JNIEXPORT void JNICALL Java_Main_reuseArenaOfMethod(JNIEnv*,
59                                                                jclass,
60                                                                jlong art_method) {
61   void* ptr = reinterpret_cast64<void*>(art_method);
62 
63   ReaderMutexLock mu(Thread::Current(), *Locks::mutator_lock_);
64   ReaderMutexLock mu2(Thread::Current(), *Locks::classlinker_classes_lock_);
65   // Check if the arena was already implicitly reused by boot classloader.
66   if (Runtime::Current()->GetLinearAlloc()->Contains(ptr)) {
67     return;
68   }
69 
70   // Check if the arena was already implicitly reused by some other classloader.
71   FindPointerAllocatorVisitor visitor(ptr);
72   Runtime::Current()->GetClassLinker()->VisitAllocators(&visitor);
73   if (visitor.is_found) {
74     return;
75   }
76 
77   // The arena was not reused yet. Do it explicitly.
78   // Create a new allocation and use it to request new arenas until one of them is
79   // a reused one that covers the art_method pointer.
80   std::unique_ptr<LinearAlloc> alloc(Runtime::Current()->CreateLinearAlloc());
81   do {
82     // Ask for a byte - it's sufficient to get an arena.
83     alloc->Alloc(Thread::Current(), 1);
84   } while (!alloc->Contains(ptr));
85 }
86 
87 }  // namespace
88 }  // namespace art
89