Skip to content

Commit

Permalink
Commit the changes to the java code
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Feb 2, 2014
1 parent 7596f93 commit 32b8353
Show file tree
Hide file tree
Showing 7 changed files with 1,058 additions and 66 deletions.
59 changes: 59 additions & 0 deletions Cheat Engine/Java/CEJVMTI/CEJVMTI/CEJVMTI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,30 @@
void JNICALL AgentThread(jvmtiEnv* jvmti_env, JNIEnv* jni_env, void* arg)
{
CJavaServer *s=new CJavaServer(jvmti_env, jni_env);

s->Start();

delete s;
OutputDebugStringA("Still alive");

}



jvmtiIterationControl JNICALL initialHeapIterate(jlong class_tag, jlong size, jlong* tag_ptr, void* user_data)
{
//OutputDebugStringA("Tagging object\n");
*tag_ptr=1;
return JVMTI_ITERATION_CONTINUE;
}


JNIEXPORT jint JNICALL Agent_OnAttach(JavaVM* vm, char* options, void* reserved)
{
jvmtiEnv *env;
JNIEnv *jni;
jvmtiError error;

jint r=vm->GetEnv((void **)&env, JVMTI_VERSION);
if (r!=JNI_OK)
{
Expand All @@ -34,6 +47,52 @@ JNIEXPORT jint JNICALL Agent_OnAttach(JavaVM* vm, char* options, void* reserved)
}


jvmtiCapabilities cap, wantedcap;

env->GetPotentialCapabilities(&cap);



if (cap.can_tag_objects)
{
env->GetCapabilities(&wantedcap);
wantedcap.can_tag_objects=1;
error=env->AddCapabilities(&wantedcap);

/*
if (error==JVMTI_ERROR_NONE)
{
error=env->IterateOverHeap(JVMTI_HEAP_OBJECT_EITHER, initialHeapIterate, NULL);
if (error==JVMTI_ERROR_NONE)
{
int i;
jlong tags[1];
jint count;
jobject *list;
jlong *taglist;
tags[0]=1;
error=env->GetObjectsWithTags(1, tags, &count, &list, &taglist);
for (i=0; i<count; i++)
{
jobject j1,j2;
j1=list[i];
j2=jni->NewGlobalRef(j1);
if (j2==NULL)
OutputDebugStringA("Failure\n");
}
}
}
*/
}






jclass threadclass=jni->FindClass("java/lang/Thread");
if (threadclass==0)
{
Expand Down
8 changes: 8 additions & 0 deletions Cheat Engine/Java/CEJVMTI/CEJVMTI/CEJVMTI.vcproj
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,10 @@
/>
</FileConfiguration>
</File>
<File
RelativePath=".\JavaEventServer.cpp"
>
</File>
<File
RelativePath=".\JavaServer.cpp"
>
Expand Down Expand Up @@ -422,6 +426,10 @@
RelativePath=".\CEJVMTI.h"
>
</File>
<File
RelativePath=".\JavaEventServer.h"
>
</File>
<File
RelativePath=".\JavaServer.h"
>
Expand Down
276 changes: 276 additions & 0 deletions Cheat Engine/Java/CEJVMTI/CEJVMTI/JavaEventServer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,276 @@
#include "StdAfx.h"
#include "JavaEventServer.h"


CJavaEventServer *old_eventserver=NULL;
CJavaEventServer *eventserver=NULL;


void JNICALL MethodLoad(jvmtiEnv *jvmti_env, jmethodID method, jint code_size, const void* code_addr, jint map_length,
const jvmtiAddrLocationMap* map, const void* compile_info)
{
if (eventserver)
eventserver->MethodLoad(jvmti_env, method, code_size, code_addr);

}

void JNICALL MethodUnload(jvmtiEnv *jvmti_env, jmethodID method, const void* code_addr)
{
if (eventserver)
eventserver->MethodUnload(jvmti_env, method, code_addr);
}

void JNICALL DynamicCodeGenerated(jvmtiEnv *jvmti_env, const char* name, const void* address, jint length)
{
if (eventserver)
eventserver->DynamicCodeGenerated(jvmti_env, name, address,length);

}

CJavaEventServer::CJavaEventServer(jvmtiEnv *jvmti_env)
{
jvmtiEventCallbacks callbacks;
jvmtiError error;
jvmtiCapabilities cap, wantedcap;

this->jvmti_env=jvmti_env;

if (eventserver) //extra check
{
CJavaEventServer *old=eventserver;
eventserver=NULL;
delete eventserver;
}



swprintf(pipename, 256,L"\\\\.\\pipe\\cejavaevents_pid%d", GetCurrentProcessId());
pipehandle=CreateNamedPipe(pipename, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT, 1,256*1024, 16, INFINITE, NULL);
ConnectNamedPipe(pipehandle, NULL);


eventserver=this;



jvmti_env->GetPotentialCapabilities(&cap);


if (cap.can_generate_compiled_method_load_events)
{

jvmti_env->GetCapabilities(&wantedcap);
wantedcap.can_generate_compiled_method_load_events=1;
error=jvmti_env->AddCapabilities(&wantedcap);
if (error!=JVMTI_ERROR_NONE)
{
OutputDebugStringA("Failure adding can_generate_compiled_method_load_events to my capabilities");
}
else
{
memset(&callbacks, 0, sizeof(callbacks));


callbacks.CompiledMethodLoad=::MethodLoad;
callbacks.CompiledMethodUnload=::MethodUnload;
callbacks.DynamicCodeGenerated=::DynamicCodeGenerated;

error=jvmti_env->SetEventCallbacks(&callbacks, sizeof(callbacks));

jvmti_env->ForceGarbageCollection();

if (error==JVMTI_ERROR_NONE)
{
jvmti_env->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_COMPILED_METHOD_LOAD, NULL);
jvmti_env->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_COMPILED_METHOD_UNLOAD, NULL);
jvmti_env->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_DYNAMIC_CODE_GENERATED, NULL);
error=jvmti_env->GenerateEvents(JVMTI_EVENT_COMPILED_METHOD_LOAD);
error=jvmti_env->GenerateEvents(JVMTI_EVENT_DYNAMIC_CODE_GENERATED);
}

}
}
else
{
OutputDebugStringA("can_generate_compiled_method_load_events == FALSE");
}





}

CJavaEventServer::~CJavaEventServer(void)
{
Terminate();


}

void CJavaEventServer::MethodLoad(jvmtiEnv *jvmti_env, jmethodID method, jint code_size, const void* code_addr)
{
/*
OutputDebugStringA("MethodLoad");
char *name=NULL, *sig=NULL, *gen=NULL;
char *classsig=NULL, *classgen=NULL;
jclass klass;
jvmti_env->GetMethodDeclaringClass(method, &klass);
jvmti_env->GetClassSignature(klass, &classsig, &classgen);
OutputDebugStringA(classsig);
jvmti_env->GetMethodName(method, &name, &sig, &gen);
OutputDebugStringA(name);
OutputDebugStringA("\n");
*/
Lock();
try
{
jclass klass;
char *name=NULL, *sig=NULL, *gen=NULL;
char *classsig=NULL, *classgen=NULL;
WORD len;

WriteByte(EVENTCMD_METHODLOAD);
WriteQword((UINT_PTR)method);
WriteDword(code_size);
WriteQword((UINT_PTR)code_addr);

jvmti_env->GetMethodDeclaringClass(method, &klass); //when this function returns jklass gets dereferenced. If this was the server I'd have to use DeleteLocalRef on this
jvmti_env->GetClassSignature(klass, &classsig, &classgen);
jvmti_env->GetMethodName(method, &name, &sig, &gen);

if (classsig)
{
len=strlen(classsig);
WriteWord(len);
if (len)
Write(classsig, len);

jvmti_env->Deallocate((unsigned char *)classsig);
}
else
WriteWord(0);


if (classgen)
jvmti_env->Deallocate((unsigned char *)classgen);


if (name)
{
len=strlen(name);
WriteWord(len);
if (len)
Write(name, len);

jvmti_env->Deallocate((unsigned char *)name);
}
else
WriteWord(0);

if (sig)
{
len=strlen(sig);
WriteWord(len);
if (len)
Write(sig, len);

jvmti_env->Deallocate((unsigned char *)sig);
}
else
WriteWord(0);

if (gen)
jvmti_env->Deallocate((unsigned char *)gen);



}
catch (char *e)
{
OutputDebugStringA(e);
//no connection yet
}

Unlock();

}

void CJavaEventServer::MethodUnload(jvmtiEnv *jvmti_env, jmethodID method, const void* code_addr)
{
Lock();
try
{
WriteByte(EVENTCMD_METHODUNLOAD);
WriteQword((UINT_PTR)method);
WriteQword((UINT_PTR)code_addr);
}
catch (char *e)
{
OutputDebugStringA(e);
//no connection yet
}
Unlock();
}

void CJavaEventServer::DynamicCodeGenerated(jvmtiEnv *jvmti_env, const char* name, const void* address, jint length)
{
Lock();
try
{
WriteByte(EVENTCMD_DYNAMICCODEGENERATED);
WriteQword((UINT_PTR)address);
WriteDword(length);
WriteWord(strlen(name));
Write((void *)name, strlen(name));

}
catch (char *e)
{
OutputDebugStringA(e);
//no connection yet
}
Unlock();
}

void CJavaEventServer::Terminate(void)
{

jvmtiCapabilities caps;

jvmti_env->SetEventNotificationMode(JVMTI_DISABLE, JVMTI_EVENT_COMPILED_METHOD_LOAD, NULL);
jvmti_env->SetEventNotificationMode(JVMTI_DISABLE, JVMTI_EVENT_COMPILED_METHOD_UNLOAD, NULL);
jvmti_env->SetEventNotificationMode(JVMTI_DISABLE, JVMTI_EVENT_DYNAMIC_CODE_GENERATED, NULL);

ZeroMemory(&caps, sizeof(caps));

caps.can_generate_compiled_method_load_events=1;

jvmti_env->RelinquishCapabilities(&caps);

eventserver=NULL;


Lock();
try
{
WriteByte(EVENTCMD_TERMINATED);
}
catch (char *e)
{
OutputDebugStringA(e);
}
Unlock();
Sleep(500);
}

Loading

0 comments on commit 32b8353

Please sign in to comment.