0

Although there are similar questions (such as A, B and C), their answers do not solve my problem.

I am using Android Studio 1.5.1 targeting Android API 18 (before Android KitKat 4.4, so I’m dealing with Dalvik, not ART runtime).

My questions are:

(1) When I use the following code, I can print a list of all sun.misc.Unsafe methods available in Android, so I think I have access to them using reflection but I do not know how to call them using reflection.

(2) If (1) is possible, how to find the magicNumber (in the code below) address using sun.misc.Unsafe methods in Android via reflection?

(3) If (1) is possible but (2) is not possible, how to put an integer number (say int test=123) in any native memory address and print its memory address using sun.misc.Unsafe methods in Android via reflection?

        String ClassName = "sun.misc.Unsafe";
        int magicNumber = 0x23420023 ;
    try {
        Class classToInvestigate = Class.forName(ClassName);
        Constructor[] aClassConstructors = classToInvestigate.getDeclaredConstructors();
        for(Constructor c : aClassConstructors){
            System.out.println("********************* constructor="+c);
        }

        Method[] aClassMethods = classToInvestigate.getDeclaredMethods();
        for(Method m : aClassMethods){
            System.out.println("********************* method="+m);

        }
        Field theUnsafe = classToInvestigate.getDeclaredField("THE_ONE");
        theUnsafe.setAccessible(true);
        Object unsafe = theUnsafe.get(null);

    } catch (ClassNotFoundException e) {
        // Class not found!
    }
    catch (Exception e) {
        // Unknown exception
    }
Community
  • 1
  • 1
User
  • 277
  • 4
  • 13
  • I don't see any method in `Unsafe` that will let you get the address of a local variable, and I fail to see the relevance of any of your cited questions, especially the C# one. – user207421 Jan 10 '16 at 07:24

2 Answers2

2

Unfortunately sun.misc.Unsafe class not included in android standard library and you should use it between reflection but I found another solution: to use it between pre-compiled direct proxy class from my library: https://github.com/iamironz/unsafe

-3

This class will return you an Unsafe instance on almost all existing JVM implementations including Android (both VM versions): https://github.com/noctarius/tengi/blob/master/java/tengi-core/src/main/java/com/noctarius/tengi/core/impl/UnsafeUtil.java

noctarius
  • 5,979
  • 19
  • 20
  • Can you give the steps how to include UnsafeUtil.java in my app using Android Studio? I mean do I need to copy it some folder and how to include it in my Android Studio project? – User Jan 16 '16 at 22:38
  • Just copy the source to some class – noctarius Jan 17 '16 at 10:41
  • Copying the source into some class does not work, it gives me three errors in Android Studio: (1) unexpected token for: package com.noctarius.tengi.core.impl; (2) cannot resolve symbol: import com.noctarius.tengi.core.model.Identifier; and (3) cannot resolve symbol: import sun.misc.Unsafe; – User Jan 17 '16 at 23:46
  • If I remove **import sun.misc.Unsafe** which is the most important item, nothing works. – User Jan 18 '16 at 19:40
  • I'm pretty sure it is an Android Studio thing to "prevent" access to sun.misc.Unsafe. Have you tried to compile on command line? – noctarius Jan 19 '16 at 05:19