1 /*
2  * Copyright (C) 2017 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 <jvmti.h>
19 
20 #include "android-base/logging.h"
21 #include "jni_binder.h"
22 #include "jvmti_helper.h"
23 #include "scoped_local_ref.h"
24 #include "test_env.h"
25 
26 namespace art {
27 
28 extern void register_art_Main(jvmtiEnv*, JNIEnv*);
29 extern void register_android_jvmti_cts_JvmtiRedefineClassesTest(jvmtiEnv*, JNIEnv*);
30 extern void register_android_jvmti_cts_JvmtiTaggingTest(jvmtiEnv*, JNIEnv*);
31 extern void register_android_jvmti_cts_JvmtiTrackingTest(jvmtiEnv*, JNIEnv*);
32 extern void register_android_jvmti_cts_JvmtiRunTestBasedTest(jvmtiEnv*, JNIEnv*);
33 
InformMainAttach(jvmtiEnv * jenv,JNIEnv * env,const char * class_name,const char * method_name)34 static void InformMainAttach(jvmtiEnv* jenv,
35                              JNIEnv* env,
36                              const char* class_name,
37                              const char* method_name) {
38   // Register native methods from available classes
39   // The agent is used with each test class, but we don't know which class is currently available.
40   // For that reason, we try to register the native methods in each one. Each function returns
41   // without throwing an error if the specified class can't be found.
42   register_art_Main(jenv, env);
43   register_android_jvmti_cts_JvmtiRedefineClassesTest(jenv, env);
44   register_android_jvmti_cts_JvmtiTaggingTest(jenv, env);
45   register_android_jvmti_cts_JvmtiTrackingTest(jenv, env);
46   register_android_jvmti_cts_JvmtiRunTestBasedTest(jenv, env);
47 
48   // Use JNI to load the class.
49   ScopedLocalRef<jclass> klass(env, GetClass(jenv, env, class_name, nullptr));
50   CHECK(klass.get() != nullptr) << class_name;
51 
52   jmethodID method = env->GetStaticMethodID(klass.get(), method_name, "()V");
53   CHECK(method != nullptr);
54 
55   env->CallStaticVoidMethod(klass.get(), method);
56 }
57 
58 static constexpr const char* kMainClass = "art/CtsMain";
59 static constexpr const char* kMainClassStartup = "startup";
60 
Agent_OnLoad(JavaVM * vm,char * options ATTRIBUTE_UNUSED,void * reserved ATTRIBUTE_UNUSED)61 extern "C" JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM* vm,
62                                                char* options ATTRIBUTE_UNUSED,
63                                                void* reserved ATTRIBUTE_UNUSED) {
64   if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0) != 0) {
65     LOG(FATAL) << "Could not get shared jvmtiEnv";
66   }
67 
68   SetStandardCapabilities(jvmti_env);
69   return 0;
70 }
71 
Agent_OnAttach(JavaVM * vm,char * options ATTRIBUTE_UNUSED,void * reserved ATTRIBUTE_UNUSED)72 extern "C" JNIEXPORT jint JNICALL Agent_OnAttach(JavaVM* vm,
73                                                  char* options ATTRIBUTE_UNUSED,
74                                                  void* reserved ATTRIBUTE_UNUSED) {
75   JNIEnv* env;
76   CHECK_EQ(0, vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6))
77       << "Could not get JNIEnv";
78 
79   if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0) != 0) {
80     LOG(FATAL) << "Could not get shared jvmtiEnv";
81   }
82 
83   SetStandardCapabilities(jvmti_env);
84   InformMainAttach(jvmti_env, env, kMainClass, kMainClassStartup);
85   return 0;
86 }
87 
88 }  // namespace art
89