From a635d2d2a6543a2fa7d205a09043085e2745f395 Mon Sep 17 00:00:00 2001 From: Ahmet Bilal Can Date: Thu, 6 Jun 2024 15:23:38 +0300 Subject: [PATCH] mkdex: Fix field item ordering (#322) To match what dex_file_verifier.cc does. Fixes #296. --- lib/mkdex.js | 18 +++++++++++++++++- test/re/frida/ClassCreationTest.java | 1 + 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/mkdex.js b/lib/mkdex.js index d2a947a0..50d9da72 100644 --- a/lib/mkdex.js +++ b/lib/mkdex.js @@ -655,6 +655,7 @@ function computeModel (classes) { stringToIndex[fieldName] ]; }); + fieldItems.sort(compareFieldItems); const methodItems = methods.map(method => { const [klass, protoId, name, annotationsId] = method; @@ -744,7 +745,7 @@ function computeModel (classes) { const instanceFields = fieldItems.reduce((result, field, index) => { const [holder] = field; if (holder === classIndex) { - result.push([index, kAccPublic]); + result.push([index > 0 ? 1 : 0, kAccPublic]); } return result; }, []); @@ -848,6 +849,21 @@ function compareProtoItems (a, b) { return 0; } +function compareFieldItems (a, b) { + const [aClass, aType, aName] = a; + const [bClass, bType, bName] = b; + + if (aClass !== bClass) { + return aClass - bClass; + } + + if (aName !== bName) { + return aName - bName; + } + + return aType - bType; +} + function compareMethodItems (a, b) { const [aClass, aProto, aName] = a; const [bClass, bProto, bName] = b; diff --git a/test/re/frida/ClassCreationTest.java b/test/re/frida/ClassCreationTest.java index fbe431d6..4022b6de 100644 --- a/test/re/frida/ClassCreationTest.java +++ b/test/re/frida/ClassCreationTest.java @@ -321,6 +321,7 @@ public void classWithUserDefinedFieldsCanBeImplemented() throws ClassNotFoundExc " fields: {" + " lastInt: 'int'," + " lastStr: 'java.lang.String'," + + " lastByteArr: '[B'," + " }," + " methods: {" + " format: [{" +