cfr icon indicating copy to clipboard operation
cfr copied to clipboard

Parent of primitive arrays is not Object[]

Open GraxCode opened this issue 4 years ago • 3 comments

CFR version

499f6d3b2e431bc647e1f4c1799ec178c8e9e600

Compiler

jdk 8

Description

CFR assumes the parent type of primitive arrays is Object[]. This code is invalid.

    public static void main(String[] args) {
        Object[] array = null;
        array = byteArray ? new byte[1] : new boolean[1];
        array[0] = 0;
        System.out.println(array[0]);
    }

it should be

   public static void main(String[] args) {
      Object array = null;
      array = byteArray ? new byte[1] : new boolean[1];
      ((byte[])array)[0] = 0;
      System.out.println(((byte[])array)[0]);
   }

It is also important to not ignore the cast on the bastore instruction as bastore works for byte[] and boolean[]. (I tried to make a sample that is not decompilable, but found this bug instead. And it seems like java requires a checkcast before the array store, so my idea is not really possible).

Example

ByteBooleanDesaster.class.txt

GraxCode avatar May 22 '20 18:05 GraxCode

Might be related to #132.


I tried to make a sample that is not decompilable, but found this bug instead. And it seems like java requires a checkcast before the array store, so my idea is not really possible

It would probably be good if CFR then emitted a comment noting that the array cast is a behavior change.

Marcono1234 avatar May 24 '20 17:05 Marcono1234

It would probably be good if CFR then emitted a comment noting that the array cast is a behavior change.

I meant java needs the checkcast, as the verifier would throw an error. I didn't know that and tried to make a bastore that takes either a byte or boolean array. This does not work. Also the cast would have to be placed in decompiled code either way, as the array reference is an Object, so no behavior change. So, if the array reference is not an Object, but a byte[], the cast is not needed.

GraxCode avatar May 25 '20 16:05 GraxCode

All of these things boil down to inadequate lifetime analysis. I'm going to have a go at improving that over the next month or so - but it's a big job which requires lots of surgery, so I wouldn't expect much for a while.

leibnitz27 avatar May 25 '20 16:05 leibnitz27