Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 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 "art_method-inl.h" |
| 21 | #include "base/logging.h" |
| 22 | #include "base/macros.h" |
| 23 | |
| 24 | namespace art { |
| 25 | |
Alex Light | 185d134 | 2016-08-11 10:48:03 -0700 | [diff] [blame] | 26 | constexpr jint TEST_900_ENV_VERSION_NUMBER = 0x900FFFFF; |
| 27 | constexpr uintptr_t ENV_VALUE = 900; |
| 28 | |
| 29 | // Allow this library to be used as a plugin too so we can test the stack. |
| 30 | static jint GetEnvHandler(JavaVMExt* vm ATTRIBUTE_UNUSED, void** new_env, jint version) { |
| 31 | printf("%s called in test 900\n", __func__); |
| 32 | if (version != TEST_900_ENV_VERSION_NUMBER) { |
| 33 | return JNI_EVERSION; |
| 34 | } |
| 35 | printf("GetEnvHandler called with version 0x%x\n", version); |
| 36 | *new_env = reinterpret_cast<void*>(ENV_VALUE); |
| 37 | return JNI_OK; |
| 38 | } |
| 39 | |
| 40 | extern "C" bool ArtPlugin_Initialize() { |
| 41 | printf("%s called in test 900\n", __func__); |
| 42 | Runtime::Current()->GetJavaVM()->AddEnvironmentHook(GetEnvHandler); |
| 43 | return true; |
| 44 | } |
| 45 | |
| 46 | extern "C" bool ArtPlugin_Deinitialize() { |
| 47 | printf("%s called in test 900\n", __func__); |
| 48 | return true; |
| 49 | } |
| 50 | |
| 51 | extern "C" JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM* vm, |
Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 52 | char* options, |
| 53 | void* reserved ATTRIBUTE_UNUSED) { |
| 54 | printf("Agent_OnLoad called with options \"%s\"\n", options); |
Alex Light | 185d134 | 2016-08-11 10:48:03 -0700 | [diff] [blame] | 55 | uintptr_t env = 0; |
| 56 | jint res = vm->GetEnv(reinterpret_cast<void**>(&env), TEST_900_ENV_VERSION_NUMBER); |
| 57 | if (res != JNI_OK) { |
| 58 | printf("GetEnv(TEST_900_ENV_VERSION_NUMBER) returned non-zero\n"); |
| 59 | } |
| 60 | printf("GetEnv returned '%" PRIdPTR "' environment!\n", env); |
Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | extern "C" JNIEXPORT void JNICALL Agent_OnUnload(JavaVM* vm ATTRIBUTE_UNUSED) { |
| 65 | printf("Agent_OnUnload called\n"); |
| 66 | } |
| 67 | |
| 68 | } // namespace art |