fork(2) download
  1. import sun.misc.Unsafe;
  2. import java.lang.reflect.Field;
  3.  
  4. public class Main {
  5.  
  6. public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException, InstantiationException {
  7. /* too lazy to run with VM args, use Reflection */
  8. Field f = Unsafe.class.getDeclaredField("theUnsafe");
  9. f.setAccessible(true);
  10. /* get array address */
  11. Unsafe unsafe = (Unsafe)f.get(null);
  12. byte four_bytes[] = {25, 25, 25, 25};
  13. Object trash[] = new Object[] { four_bytes };
  14. long base_offset_bytes = unsafe.arrayBaseOffset(Object[].class);
  15. long four_bytes_address = unsafe.getLong(trash, base_offset_bytes); // <- this is it
  16. long ints_addr = unsafe.allocateMemory(16); // allocate 4 * 4 bytes, i.e. 4 ints
  17. unsafe.copyMemory(four_bytes_address + base_offset_bytes, ints_addr, 4); // copy all four bytes
  18. for(int i = 0; i < 4; i++) {
  19. System.out.println(unsafe.getInt(ints_addr + i)); //run through entire allocated int[],
  20. // get some intestines
  21. }
  22. System.out.println("************************************");
  23. for(int i = 0; i < 16; i++) {
  24. System.out.println(unsafe.getByte(ints_addr + i)); //run through entire allocated int[],
  25. // get some intestines
  26. }
  27. }
  28. }
Success #stdin #stdout 0.06s 380224KB
stdin
Standard input is empty
stdout
421075225
1644825
6425
25
************************************
25
25
25
25
0
0
0
0
0
0
0
0
115
0
0
0