How to print heap location using java for process?

I want to check if ASLR works, which randomizes the heap location for the process.

0
source share
3 answers

You cannot do this in pure Java.

  • The machine address (pointers) does not apply to Java applications.

  • Even if that were the case, there is no Java API that tells you that this is a bunch.

I suggest that you could use the values ​​returned by System.identityHashcode() as the addresses of ersatz machines. If you wrote a simple Java test application that examined the hash code of the identifier of a sample object, then ran it several times with ASLR turned on and off, you might see a difference in predictability.

+1
source

You can call Unsafe.allocateMemory(size) on some JVMs. This returns a memory location.

I do not recommend that you do this, and I do not recommend that you worry about ASLR with Java.

+3
source

You should be able to do this using JNI. Select an array of long sizes of your heap or a little smaller and call the JNI testing method.

 package com.example.aslrtest; public class Test { private static native void test(long[] heapa); public void doTest() { long[] a=new long[size_of_available_heap/8]; for (long i=0; i!=a.length; i++) a[i]=i; test(a); System.out.println(a[0]); } } 

Then with something like this JNI code, you can get the address of this array.

 JNIEXPORT void JNICALL Java_com_example_aslrtest_Test_test(JNIEnv * env, jclass jc, jlongArray heapa) { jlong* heapp; jboolean jniNoCopy = JNI_FALSE; heapp = (*env)->GetLongArrayElements(env, heapa, &jniNoCopy); heapp[0] = (jlong)heapp; (*env)->ReleaseLongArrayElements(env,heapa,heapp,0); } 

When this code is returned, the first element of the array must contain the address of the array.

Does it make sense is another question. Depends on what you do. I guess, probably not, but this is your call.

0
source

All Articles