1 /*
2  * Copyright 2016 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 <stdio.h>
19 
20 #include <android-base/logging.h>
21 
22 #include "base/macros.h"
23 
24 #include "jni_binder.h"
25 #include "jvmti_helper.h"
26 #include "test_env.h"
27 
28 #include "901-hello-ti-agent/basics.h"
29 #include "909-attach-agent/attach.h"
30 #include "936-search-onload/search_onload.h"
31 #include "1919-vminit-thread-start-timing/vminit.h"
32 
33 namespace art {
34 
35 namespace common_redefine {
36 jint OnLoad(JavaVM* vm, char* options, void* reserved);
37 }  // namespace common_redefine
38 
39 namespace common_retransform {
40 jint OnLoad(JavaVM* vm, char* options, void* reserved);
41 }  // namespace common_retransform
42 
43 namespace common_transform {
44 jint OnLoad(JavaVM* vm, char* options, void* reserved);
45 }  // namespace common_transform
46 
47 namespace {
48 
49 using OnLoad   = jint (*)(JavaVM* vm, char* options, void* reserved);
50 using OnAttach = jint (*)(JavaVM* vm, char* options, void* reserved);
51 
52 struct AgentLib {
53   const char* name;
54   OnLoad load;
55   OnAttach attach;
56 };
57 
58 // A trivial OnLoad implementation that only initializes the global jvmti_env.
MinimalOnLoad(JavaVM * vm,char * options ATTRIBUTE_UNUSED,void * reserved ATTRIBUTE_UNUSED)59 static jint MinimalOnLoad(JavaVM* vm,
60                           char* options ATTRIBUTE_UNUSED,
61                           void* reserved ATTRIBUTE_UNUSED) {
62   if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0) != 0) {
63     printf("Unable to get jvmti env!\n");
64     return 1;
65   }
66   SetStandardCapabilities(jvmti_env);
67   return 0;
68 }
69 
70 // A list of all non-standard the agents we have for testing. All other agents will use
71 // MinimalOnLoad.
72 static AgentLib agents[] = {
73   { "901-hello-ti-agent", Test901HelloTi::OnLoad, nullptr },
74   { "909-attach-agent", nullptr, Test909AttachAgent::OnAttach },
75   { "916-obsolete-jit", common_redefine::OnLoad, nullptr },
76   { "921-hello-failure", common_retransform::OnLoad, nullptr },
77   { "934-load-transform", common_retransform::OnLoad, nullptr },
78   { "935-non-retransformable", common_transform::OnLoad, nullptr },
79   { "936-search-onload", Test936SearchOnload::OnLoad, nullptr },
80   { "937-hello-retransform-package", common_retransform::OnLoad, nullptr },
81   { "938-load-transform-bcp", common_retransform::OnLoad, nullptr },
82   { "939-hello-transformation-bcp", common_redefine::OnLoad, nullptr },
83   { "941-recursive-obsolete-jit", common_redefine::OnLoad, nullptr },
84   { "943-private-recursive-jit", common_redefine::OnLoad, nullptr },
85   { "1919-vminit-thread-start-timing", Test1919VMInitThreadStart::OnLoad, nullptr },
86   { "2031-zygote-compiled-frame-deopt", nullptr, MinimalOnLoad },
87 };
88 
FindAgent(char * name)89 static AgentLib* FindAgent(char* name) {
90   for (AgentLib& l : agents) {
91     if (strncmp(l.name, name, strlen(l.name)) == 0) {
92       return &l;
93     }
94   }
95   return nullptr;
96 }
97 
FindAgentNameAndOptions(char * options,char ** name,char ** other_options)98 static bool FindAgentNameAndOptions(char* options,
99                                     /*out*/char** name,
100                                     /*out*/char** other_options) {
101   // Name is the first element.
102   *name = options;
103   char* rest = options;
104   // name is the first thing in the options
105   while (*rest != '\0' && *rest != ',') {
106     rest++;
107   }
108   if (*rest == ',') {
109     *rest = '\0';
110     rest++;
111   }
112   *other_options = rest;
113   return true;
114 }
115 
SetIsJVM(const char * options)116 static void SetIsJVM(const char* options) {
117   SetJVM(strncmp(options, "jvm", 3) == 0);
118 }
119 
120 }  // namespace
121 
Agent_OnLoad(JavaVM * vm,char * options,void * reserved)122 extern "C" JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM* vm, char* options, void* reserved) {
123   char* remaining_options = nullptr;
124   char* name_option = nullptr;
125   if (!FindAgentNameAndOptions(options, &name_option, &remaining_options)) {
126     printf("Unable to find agent name in options: %s\n", options);
127     return -1;
128   }
129 
130   SetIsJVM(remaining_options);
131 
132   AgentLib* lib = FindAgent(name_option);
133   OnLoad fn = nullptr;
134   if (lib == nullptr) {
135     fn = &MinimalOnLoad;
136   } else {
137     if (lib->load == nullptr) {
138       printf("agent: %s does not include an OnLoad method.\n", name_option);
139       return -3;
140     }
141     fn = lib->load;
142   }
143   return fn(vm, remaining_options, reserved);
144 }
145 
Agent_OnAttach(JavaVM * vm,char * options,void * reserved)146 extern "C" JNIEXPORT jint JNICALL Agent_OnAttach(JavaVM* vm, char* options, void* reserved) {
147   char* remaining_options = nullptr;
148   char* name_option = nullptr;
149   if (!FindAgentNameAndOptions(options, &name_option, &remaining_options)) {
150     printf("Unable to find agent name in options: %s\n", options);
151     return -1;
152   }
153 
154   AgentLib* lib = FindAgent(name_option);
155   if (lib == nullptr) {
156     printf("Unable to find agent named: %s, add it to the list in test/ti-agent/common_load.cc\n",
157            name_option);
158     return -2;
159   }
160   if (lib->attach == nullptr) {
161     printf("agent: %s does not include an OnAttach method.\n", name_option);
162     return -3;
163   }
164   SetIsJVM(remaining_options);
165   return lib->attach(vm, remaining_options, reserved);
166 }
167 
168 }  // namespace art
169