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