xref: /aosp_15_r20/art/test/936-search-onload/search_onload.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2017 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #include "search_onload.h"
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker #include <inttypes.h>
20*795d594fSAndroid Build Coastguard Worker 
21*795d594fSAndroid Build Coastguard Worker #include <android-base/macros.h>
22*795d594fSAndroid Build Coastguard Worker #include <android-base/stringprintf.h>
23*795d594fSAndroid Build Coastguard Worker 
24*795d594fSAndroid Build Coastguard Worker #include "base/macros.h"
25*795d594fSAndroid Build Coastguard Worker #include "jni.h"
26*795d594fSAndroid Build Coastguard Worker #include "jvmti.h"
27*795d594fSAndroid Build Coastguard Worker #include "scoped_utf_chars.h"
28*795d594fSAndroid Build Coastguard Worker 
29*795d594fSAndroid Build Coastguard Worker // Test infrastructure
30*795d594fSAndroid Build Coastguard Worker #include "jvmti_helper.h"
31*795d594fSAndroid Build Coastguard Worker #include "test_env.h"
32*795d594fSAndroid Build Coastguard Worker 
33*795d594fSAndroid Build Coastguard Worker namespace art {
34*795d594fSAndroid Build Coastguard Worker namespace Test936SearchOnload {
35*795d594fSAndroid Build Coastguard Worker 
OnLoad(JavaVM * vm,char * options,void * reserved)36*795d594fSAndroid Build Coastguard Worker jint OnLoad(JavaVM* vm,
37*795d594fSAndroid Build Coastguard Worker             [[maybe_unused]] char* options,
38*795d594fSAndroid Build Coastguard Worker             [[maybe_unused]] void* reserved) {
39*795d594fSAndroid Build Coastguard Worker   if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0)) {
40*795d594fSAndroid Build Coastguard Worker     printf("Unable to get jvmti env!\n");
41*795d594fSAndroid Build Coastguard Worker     return 1;
42*795d594fSAndroid Build Coastguard Worker   }
43*795d594fSAndroid Build Coastguard Worker   SetStandardCapabilities(jvmti_env);
44*795d594fSAndroid Build Coastguard Worker 
45*795d594fSAndroid Build Coastguard Worker   char* dex_loc = getenv("DEX_LOCATION");
46*795d594fSAndroid Build Coastguard Worker   std::string dex1 = android::base::StringPrintf("%s/936-search-onload.jar", dex_loc);
47*795d594fSAndroid Build Coastguard Worker   std::string dex2 = android::base::StringPrintf("%s/936-search-onload-ex.jar", dex_loc);
48*795d594fSAndroid Build Coastguard Worker 
49*795d594fSAndroid Build Coastguard Worker   jvmtiError result = jvmti_env->AddToBootstrapClassLoaderSearch(dex1.c_str());
50*795d594fSAndroid Build Coastguard Worker   if (result != JVMTI_ERROR_NONE) {
51*795d594fSAndroid Build Coastguard Worker     printf("Could not add to bootstrap classloader.\n");
52*795d594fSAndroid Build Coastguard Worker     return 1;
53*795d594fSAndroid Build Coastguard Worker   }
54*795d594fSAndroid Build Coastguard Worker 
55*795d594fSAndroid Build Coastguard Worker   result = jvmti_env->AddToSystemClassLoaderSearch(dex2.c_str());
56*795d594fSAndroid Build Coastguard Worker   if (result != JVMTI_ERROR_NONE) {
57*795d594fSAndroid Build Coastguard Worker     printf("Could not add to system classloader.\n");
58*795d594fSAndroid Build Coastguard Worker     return 1;
59*795d594fSAndroid Build Coastguard Worker   }
60*795d594fSAndroid Build Coastguard Worker 
61*795d594fSAndroid Build Coastguard Worker   return JNI_OK;
62*795d594fSAndroid Build Coastguard Worker }
63*795d594fSAndroid Build Coastguard Worker 
64*795d594fSAndroid Build Coastguard Worker }  // namespace Test936SearchOnload
65*795d594fSAndroid Build Coastguard Worker }  // namespace art
66