-
Notifications
You must be signed in to change notification settings - Fork 6
/
jvmfaketime.c
32 lines (27 loc) · 1019 Bytes
/
jvmfaketime.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <jni.h>
#include <sys/time.h>
#include <stdlib.h>
long long system_time() {
struct timeval now;
gettimeofday(&now, NULL);
return now.tv_sec * 1000LL + now.tv_usec / 1000LL;
}
JNIEXPORT jlong JNICALL Fake_CurrentTimeMillis(JNIEnv* env, jclass jc) {
jfieldID field = (*env)->GetStaticFieldID(env, jc, "fakeTimeOffset", "J");
if (field == NULL || (*env)->ExceptionCheck(env)) {
(*env)->ExceptionClear(env);
return 0;
}
jlong offset = (*env)->GetStaticLongField(env, jc, field);
return system_time() + offset;
}
JNIEXPORT jlong JNICALL Fake_TrueCurrentTimeMillis(JNIEnv* env, jclass jc) {
return system_time();
}
static JNINativeMethod fake_method[] = {
{ "currentTimeMillis", "()J", (void*) &Fake_CurrentTimeMillis },
{ "trueCurrentTimeMillis", "()J", (void*) &Fake_TrueCurrentTimeMillis }
};
JNIEXPORT void JNICALL Java_java_lang_System_startFakingTime(JNIEnv *env, jclass cls) {
(*env)->RegisterNatives(env, cls, fake_method, sizeof(fake_method) / sizeof(fake_method[0]));
}