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 };
87
FindAgent(char * name)88 static AgentLib* FindAgent(char* name) {
89 for (AgentLib& l : agents) {
90 if (strncmp(l.name, name, strlen(l.name)) == 0) {
91 return &l;
92 }
93 }
94 return nullptr;
95 }
96
FindAgentNameAndOptions(char * options,char ** name,char ** other_options)97 static bool FindAgentNameAndOptions(char* options,
98 /*out*/char** name,
99 /*out*/char** other_options) {
100 // Name is the first element.
101 *name = options;
102 char* rest = options;
103 // name is the first thing in the options
104 while (*rest != '\0' && *rest != ',') {
105 rest++;
106 }
107 if (*rest == ',') {
108 *rest = '\0';
109 rest++;
110 }
111 *other_options = rest;
112 return true;
113 }
114
SetIsJVM(const char * options)115 static void SetIsJVM(const char* options) {
116 SetJVM(strncmp(options, "jvm", 3) == 0);
117 }
118
119 } // namespace
120
Agent_OnLoad(JavaVM * vm,char * options,void * reserved)121 extern "C" JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM* vm, char* options, void* reserved) {
122 char* remaining_options = nullptr;
123 char* name_option = nullptr;
124 if (!FindAgentNameAndOptions(options, &name_option, &remaining_options)) {
125 printf("Unable to find agent name in options: %s\n", options);
126 return -1;
127 }
128
129 SetIsJVM(remaining_options);
130
131 AgentLib* lib = FindAgent(name_option);
132 OnLoad fn = nullptr;
133 if (lib == nullptr) {
134 fn = &MinimalOnLoad;
135 } else {
136 if (lib->load == nullptr) {
137 printf("agent: %s does not include an OnLoad method.\n", name_option);
138 return -3;
139 }
140 fn = lib->load;
141 }
142 return fn(vm, remaining_options, reserved);
143 }
144
Agent_OnAttach(JavaVM * vm,char * options,void * reserved)145 extern "C" JNIEXPORT jint JNICALL Agent_OnAttach(JavaVM* vm, char* options, void* reserved) {
146 char* remaining_options = nullptr;
147 char* name_option = nullptr;
148 if (!FindAgentNameAndOptions(options, &name_option, &remaining_options)) {
149 printf("Unable to find agent name in options: %s\n", options);
150 return -1;
151 }
152
153 AgentLib* lib = FindAgent(name_option);
154 if (lib == nullptr) {
155 printf("Unable to find agent named: %s, add it to the list in test/ti-agent/common_load.cc\n",
156 name_option);
157 return -2;
158 }
159 if (lib->attach == nullptr) {
160 printf("agent: %s does not include an OnAttach method.\n", name_option);
161 return -3;
162 }
163 SetIsJVM(remaining_options);
164 return lib->attach(vm, remaining_options, reserved);
165 }
166
167 } // namespace art
168