|
| 1 | +/* |
| 2 | + * Copyright 2020 Odnoklassniki Ltd, Mail.Ru Group |
| 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 <jvmti.h> |
| 18 | +#include <stdlib.h> |
| 19 | +#include <string.h> |
| 20 | +#include <time.h> |
| 21 | + |
| 22 | +static jlong (*real_time_millis)(JNIEnv *, jclass) = NULL; |
| 23 | +static jlong (*real_nano_time_adjustment)(JNIEnv *, jclass, jlong) = NULL; |
| 24 | + |
| 25 | +jlong JNICALL fake_time_millis(JNIEnv* env, jclass cls) |
| 26 | +{ |
| 27 | + jclass systemClass = env->FindClass("java/lang/System"); |
| 28 | + jmethodID getPropertyMethodId = env->GetStaticMethodID(systemClass, "getProperty", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"); |
| 29 | + jstring offsetPropertyName = env->NewStringUTF("faketime.offset.seconds"); |
| 30 | + jstring offsetPropertyDefault = env->NewStringUTF("0"); |
| 31 | + jstring offsetValue = (jstring)env->CallStaticObjectMethod(systemClass, getPropertyMethodId, offsetPropertyName, offsetPropertyDefault); |
| 32 | + const char *offset = env->GetStringUTFChars(offsetValue, NULL); |
| 33 | + jlong result = real_time_millis(env, cls) + atoll(offset); |
| 34 | + env->ReleaseStringUTFChars(offsetValue, offset); |
| 35 | + return result; |
| 36 | +} |
| 37 | + |
| 38 | +jlong JNICALL fake_nano_time_adjustment(JNIEnv *env, jclass cls, jlong offset_seconds) |
| 39 | +{ |
| 40 | + jclass systemClass = env->FindClass("java/lang/System"); |
| 41 | + jmethodID getPropertyMethodId = env->GetStaticMethodID(systemClass, "getProperty", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"); |
| 42 | + jstring offsetPropertyName = env->NewStringUTF("faketime.offset.seconds"); |
| 43 | + jstring offsetPropertyDefault = env->NewStringUTF("0"); |
| 44 | + jstring offsetValue = (jstring)env->CallStaticObjectMethod(systemClass, getPropertyMethodId, offsetPropertyName, offsetPropertyDefault); |
| 45 | + const char *offset = env->GetStringUTFChars(offsetValue, NULL); |
| 46 | + jlong result = real_nano_time_adjustment(env, cls, offset_seconds) + atoll(offset) * 1000000; |
| 47 | + env->ReleaseStringUTFChars(offsetValue, offset); |
| 48 | + return result; |
| 49 | +} |
| 50 | + |
| 51 | +void JNICALL NativeMethodBind(jvmtiEnv *jvmti, JNIEnv *env, jthread thread, jmethodID method, |
| 52 | + void *address, void **new_address_ptr) |
| 53 | +{ |
| 54 | + char *name; |
| 55 | + if (jvmti->GetMethodName(method, &name, NULL, NULL) == 0) |
| 56 | + { |
| 57 | + if (real_time_millis == NULL && strcmp(name, "currentTimeMillis") == 0) |
| 58 | + { |
| 59 | + real_time_millis = (jlong(*)(JNIEnv *, jclass))address; |
| 60 | + *new_address_ptr = (void *)fake_time_millis; |
| 61 | + } |
| 62 | + else if (real_nano_time_adjustment == NULL && strcmp(name, "getNanoTimeAdjustment") == 0) |
| 63 | + { |
| 64 | + real_nano_time_adjustment = (jlong(*)(JNIEnv *, jclass, jlong))address; |
| 65 | + *new_address_ptr = (void *)fake_nano_time_adjustment; |
| 66 | + } |
| 67 | + jvmti->Deallocate((unsigned char *)name); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *vm, char *options, void *reserved) |
| 72 | +{ |
| 73 | + jvmtiEnv *jvmti; |
| 74 | + vm->GetEnv((void **)&jvmti, JVMTI_VERSION_1_0); |
| 75 | + |
| 76 | + jvmtiCapabilities capabilities = {0}; |
| 77 | + capabilities.can_generate_native_method_bind_events = 1; |
| 78 | +#if JNI_VERSION_9 |
| 79 | + jvmtiCapabilities potential_capabilities; |
| 80 | + jvmti->GetPotentialCapabilities(&potential_capabilities); |
| 81 | + capabilities.can_generate_early_vmstart = potential_capabilities.can_generate_early_vmstart; |
| 82 | +#endif |
| 83 | + jvmti->AddCapabilities(&capabilities); |
| 84 | + |
| 85 | + jvmtiEventCallbacks callbacks = {0}; |
| 86 | + callbacks.NativeMethodBind = NativeMethodBind; |
| 87 | + jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks)); |
| 88 | + |
| 89 | + jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_NATIVE_METHOD_BIND, NULL); |
| 90 | + |
| 91 | + return 0; |
| 92 | +} |
0 commit comments