Arrays

Explanation of the way to work with arrays in Frida

**** NOTE: BLOG MOVED TO https://cmrodriguez.me/ ****

Arrays are transformed in Frida from Javascript to Java transparently, so there is no special consideration, as shown in the following examples:

var intArray = [1, 2, 3];
console.log(ArrayType.sumArray(intArray));

This example shows a transformation from a javascript Array to a Java array.

ArrayType.sumArray.overload("[I").implementation = function (arrayList) {
    var total = 0;
    for (var i = 0; i < arrayList.length; i++) {
        total += arrayList[i];
    }
    console.log("Entra en arrayInt sumArray: " + total);
    return total;
}

This method receives an array in Java. When the Frida user writes the reimplementation function, they will receive a Javascript Array.

The forEach structure (used in Javascript) to iterate on an array does not work.

Whenever the following code is called:

var total = 0.0;
arrayList.forEach(function (element) { total += element; });

the frida server generates an error:

TypeError: undefined not callable (property 'forEach' of [object Object])
    at [anon] (../../../frida-gum/bindings/gumjs/duktape.c:65012)
    at /examples.js:278
    at input:1

Working with array of Objects is like working with native types, with the exception that a position in the Array can be null, so it must be taken in consideration when the script is being developed, as in the following example:

var peopleArray = ArrayType.getAllPeople();
for (var i = 0; i < peopleArray.length; i++) {
    if (peopleArray[i] == null) {
        console.log(i + " - null");
    } else {
        console.log(peopleArray[i].getId()+" - "+peopleArray[i].getName() + " - " + peopleArray[i].getAge());    
    }
}

A null value in the Java array is translated to a null javascript value automatically by the framework.

Last updated