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