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