Skip to content

Wrong local variable analysis when assembly #1018

@Fanyhsiao

Description

@Fanyhsiao

Describe the bug

it will produce wrong result when reassmbly if the method reuses local variable slot

To Reproduce

Steps to reproduce the behavior:

  1. Compile this code
import sun.misc.Unsafe;
import sun.reflect.ReflectionFactory;

import java.lang.invoke.MethodHandles;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;

public class UnsafeUtility {
    private UnsafeUtility() {
        throw new UnsupportedOperationException();
    }

    public static final MethodHandles.Lookup TRUSTED_LOOKUP;

    public static final Unsafe UNSAFE;

    static {
        {
            MethodHandles.Lookup temp;
            try {
                Constructor<?> ctor = ReflectionFactory.getReflectionFactory()
                        .newConstructorForSerialization(MethodHandles.Lookup.class, MethodHandles.Lookup.class.getDeclaredConstructor(Class.class, Class.class, int.class));
                temp = (MethodHandles.Lookup) ctor.newInstance(Object.class, null, -1);
            } catch (ReflectiveOperationException e) {
                temp = null;
            }
            TRUSTED_LOOKUP = temp;
        }


        {
            Unsafe temp;
            try {
                Field unsafeField = Unsafe.class.getDeclaredField("theUnsafe");
                unsafeField.setAccessible(true);
                temp = (Unsafe) unsafeField.get(null);
            } catch (IllegalAccessException | NoSuchFieldException e) {
                temp = null;
            }
            UNSAFE = temp;
        }
    }
}
  1. Drag and drop the class into recaf
  2. Assembly the method "<clinit>"
  3. Press "Ctrl + S"

Screenshots

Before:

Image

After:

Image

As we can see, normally the temp2 will be the UNSAFE's result, but it assembly as temp3.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugYup, thats broken

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions