Skip to content

Commit

Permalink
types: Add support for arrays of array
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Marie Mineau authored and oleavr committed May 10, 2024
1 parent eac1a62 commit ef90caa
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
21 changes: 19 additions & 2 deletions lib/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ const primitiveTypes = {
}
};

const primitiveTypesNames = new Set(Object.values(primitiveTypes).map(t => t.name));

function getPrimitiveType (name) {
const result = primitiveTypes[name];
return (result !== undefined) ? result : null;
Expand Down Expand Up @@ -436,10 +438,27 @@ function getArrayType (typeName, unbox, factory) {
let elementTypeName = typeName.substring(1);
const elementType = getType(elementTypeName, unbox, factory);

let numInternalArrays = 0;
const end = elementTypeName.length;
while (numInternalArrays !== end && elementTypeName[numInternalArrays] === '[') {
numInternalArrays++;
}
elementTypeName = elementTypeName.substring(numInternalArrays);

if (elementTypeName[0] === 'L' && elementTypeName[elementTypeName.length - 1] === ';') {
elementTypeName = elementTypeName.substring(1, elementTypeName.length - 1);
}

// The type name we get is not always the correct representation of the type so we make it so here.
let internalElementTypeName = elementTypeName.replace(/\./g, '/');
if (primitiveTypesNames.has(internalElementTypeName)) {
internalElementTypeName = '['.repeat(numInternalArrays) + internalElementTypeName;
} else {
internalElementTypeName = '['.repeat(numInternalArrays) + 'L' + internalElementTypeName + ';';
}
const internalTypeName = '[' + internalElementTypeName;
elementTypeName = '['.repeat(numInternalArrays) + elementTypeName;

return {
name: typeName.replace(/\./g, '/'),
type: 'pointer',
Expand Down Expand Up @@ -476,8 +495,6 @@ function getArrayType (typeName, unbox, factory) {
}
}

// The type name we get is not always the correct representation of the type so we make it so here.
const internalTypeName = '[L' + elementTypeName.replace(/\./g, '/') + ';';
try {
result.$w = factory.cast(arr, factory.use(internalTypeName), owned);
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion test/config.mk
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
ANDROID_SDK_ROOT ?= $(shell echo ~/Android/Sdk)
ANDROID_NDK_ROOT ?= $(shell echo ~/.local/opt/android-ndk-r25b
ANDROID_NDK_ROOT ?= $(shell echo ~/.local/opt/android-ndk-r25b)
ANDROID_ARCH ?= arm64
ANDROID_ABI ?= arm64-v8a
ANDROID_API_LEVEL ?= 33
Expand Down
32 changes: 32 additions & 0 deletions test/re/frida/MethodTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,28 @@ public void primitiveArrayCanBePassed() {
assertEquals("12", script.getNextMessage());
}

@Test
public void primitive2DArrayCanBePassed() {
loadScript("var Buffinator = Java.use('re.frida.Buffinator');" +
"var buffinator = Buffinator.$new();" +
"send(buffinator.sum2d([[1, 2, 3], [4, 5, 6], [7, 8, 9]]));");
assertEquals("45", script.getNextMessage());
}

@Test
public void methodWith2DArrayCanBeHooked() {
loadScript("var Buffinator = Java.use('re.frida.Buffinator');" +
"var sum2d = Buffinator.sum2d;" +
"sum2d.implementation = function (arrs) {" +
"send(JSON.stringify(arrs));" +
"return sum2d.call(this, arrs);" +
"};");
Buffinator buffinator = new Buffinator();
byte[][] arrs = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
assertEquals(45, buffinator.sum2d(arrs));
assertEquals("[[1,2,3],[4,5,6],[7,8,9]]", script.getNextMessage());
}

@Test
public void primitiveArrayCanBeModified() {
loadScript("var Buffinator = Java.use('re.frida.Buffinator');" +
Expand Down Expand Up @@ -620,6 +642,16 @@ public int sum(byte[] values) {
return result;
}

public int sum2d(byte[][] values) {
int result = 0;
for (byte[] array : values) {
for (byte value : array) {
result += value;
}
}
return result;
}

public void bump(int[] values) {
for (int i = 0; i != values.length; i++) {
values[i] += 1000;
Expand Down

0 comments on commit ef90caa

Please sign in to comment.