1 /*
2  * Copyright (C) 2017 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 "search_onload.h"
18 
19 #include <inttypes.h>
20 
21 #include "android-base/stringprintf.h"
22 #include "base/logging.h"
23 #include "base/macros.h"
24 #include "jni.h"
25 #include "jvmti.h"
26 #include "scoped_utf_chars.h"
27 
28 // Test infrastructure
29 #include "jvmti_helper.h"
30 #include "test_env.h"
31 
32 namespace art {
33 namespace Test936SearchOnload {
34 
OnLoad(JavaVM * vm,char * options ATTRIBUTE_UNUSED,void * reserved ATTRIBUTE_UNUSED)35 jint OnLoad(JavaVM* vm,
36             char* options ATTRIBUTE_UNUSED,
37             void* reserved ATTRIBUTE_UNUSED) {
38   if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0)) {
39     printf("Unable to get jvmti env!\n");
40     return 1;
41   }
42   SetAllCapabilities(jvmti_env);
43 
44   char* dex_loc = getenv("DEX_LOCATION");
45   std::string dex1 = android::base::StringPrintf("%s/936-search-onload.jar", dex_loc);
46   std::string dex2 = android::base::StringPrintf("%s/936-search-onload-ex.jar", dex_loc);
47 
48   jvmtiError result = jvmti_env->AddToBootstrapClassLoaderSearch(dex1.c_str());
49   if (result != JVMTI_ERROR_NONE) {
50     printf("Could not add to bootstrap classloader.\n");
51     return 1;
52   }
53 
54   result = jvmti_env->AddToSystemClassLoaderSearch(dex2.c_str());
55   if (result != JVMTI_ERROR_NONE) {
56     printf("Could not add to system classloader.\n");
57     return 1;
58   }
59 
60   return JNI_OK;
61 }
62 
63 }  // namespace Test936SearchOnload
64 }  // namespace art
65