* throw the expected exceptions.
*/
public void testGetObjectAtAddress()
{
JavaHeap heap = defaultJavaHeap();
JavaRuntime runtime = defaultJavaRuntime();
ImageAddressSpace addressSpace = defaultAddressSpace();
ImagePointer address=null;
ImagePointer unalignedAddress=null;
Iterator heapSections = heap.getSections().iterator();
//determine the heap start and end address
long heapStartAddress = Long.MAX_VALUE;
long heapEndAddress = 0;
Object nextElement = null;
ImageSection currentSection = null;
long sectionStart = 0;
while (heapSections.hasNext()) {
nextElement = heapSections.next();
if (nextElement instanceof ImageSection) {
currentSection = (ImageSection) nextElement;
sectionStart = currentSection.getBaseAddress().getAddress();
if (sectionStart < heapStartAddress) {
heapStartAddress = sectionStart;
}
if (sectionStart + currentSection.getSize() > heapEndAddress) {
heapEndAddress = sectionStart + currentSection.getSize();
}
}
}
//check that every object in the heap can be retrieved by address
boolean exception=false;
for (Iterator objects = heap.getObjects().iterator();objects.hasNext();) {
Object potentialObject = objects.next();
JavaObject object = null;
if (potentialObject instanceof JavaObject) {
object = (JavaObject)potentialObject;
} else {
continue;
}
address=(ImagePointer)object.getID();
try {
runtime.getObjectAtAddress(address).getJavaClass();
if (unalignedAddress == null) {
unalignedAddress = address.add(1);
}
} catch (Exception e) {
e.printStackTrace();
exception=true;
break;
}
}
assertFalse(exception);
// Check an address BEFORE the start of the heap. For some JVMs, off-heap objects are supported
exception=false;
try {
long addr1 = ((heapStartAddress-1000) & (Long.MAX_VALUE-7L));
runtime.getObjectAtAddress(addressSpace.getPointer(addr1));
} catch (IllegalArgumentException e) {
e.printStackTrace();
exception=true;
} catch (Exception e) {
}
assertFalse(exception);
// Check an address AFTER the first object but not aligned with an object boundary.
// unalignedAddress is expected to throw a IllegalArgumentException
exception=false;
try {
runtime.getObjectAtAddress(unalignedAddress);
} catch (IllegalArgumentException e) {
// IllegalArgumentException is the expected behaviour
} catch (CorruptDataException e) {
exception=true;
e.printStackTrace();