|
| 1 | +package dex |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/binary" |
| 5 | + "fmt" |
| 6 | + "unsafe" |
| 7 | +) |
| 8 | + |
| 9 | +// MethodSignatures maps method names to their parameter type descriptor lists. |
| 10 | +// Example: {"registerClient": ["Landroid/os/ParcelUuid;", "Landroid/bluetooth/IBluetoothGattCallback;", "Z", "Landroid/content/AttributionSource;"]} |
| 11 | +type MethodSignatures map[string][]string |
| 12 | + |
| 13 | +// objectMethodNames contains method names inherited from java.lang.Object |
| 14 | +// that should be skipped when extracting proxy method signatures. |
| 15 | +var objectMethodNames = map[string]struct{}{ |
| 16 | + "hashCode": {}, |
| 17 | + "equals": {}, |
| 18 | + "toString": {}, |
| 19 | + "getClass": {}, |
| 20 | + "notify": {}, |
| 21 | + "notifyAll": {}, |
| 22 | + "wait": {}, |
| 23 | + "finalize": {}, |
| 24 | + "clone": {}, |
| 25 | + "asBinder": {}, |
| 26 | + "getInterfaceDescriptor": {}, |
| 27 | +} |
| 28 | + |
| 29 | +// extractProxyMethodSignatures reads virtual methods from a $Stub$Proxy |
| 30 | +// class_def and returns the method name to parameter type descriptors mapping. |
| 31 | +// |
| 32 | +// classDefOff is the offset of the class_def_item within data. |
| 33 | +func extractProxyMethodSignatures( |
| 34 | + f *dexFile, |
| 35 | + data []byte, |
| 36 | + classDefOff uint32, |
| 37 | +) (MethodSignatures, error) { |
| 38 | + classDataOff := binary.LittleEndian.Uint32(data[classDefOff+0x18:]) |
| 39 | + if classDataOff == 0 { |
| 40 | + return nil, nil |
| 41 | + } |
| 42 | + |
| 43 | + dataLen := uint32(len(data)) |
| 44 | + if classDataOff >= dataLen { |
| 45 | + return nil, fmt.Errorf("class_data_off 0x%x out of bounds (data len %d)", classDataOff, dataLen) |
| 46 | + } |
| 47 | + |
| 48 | + // Parse class_data_item header: |
| 49 | + // static_fields_size, instance_fields_size, direct_methods_size, virtual_methods_size |
| 50 | + pos := classDataOff |
| 51 | + staticFieldsSize, pos, err := readULEB128(data, pos) |
| 52 | + if err != nil { |
| 53 | + return nil, fmt.Errorf("reading static_fields_size: %w", err) |
| 54 | + } |
| 55 | + |
| 56 | + instanceFieldsSize, pos, err := readULEB128(data, pos) |
| 57 | + if err != nil { |
| 58 | + return nil, fmt.Errorf("reading instance_fields_size: %w", err) |
| 59 | + } |
| 60 | + |
| 61 | + directMethodsSize, pos, err := readULEB128(data, pos) |
| 62 | + if err != nil { |
| 63 | + return nil, fmt.Errorf("reading direct_methods_size: %w", err) |
| 64 | + } |
| 65 | + |
| 66 | + virtualMethodsSize, pos, err := readULEB128(data, pos) |
| 67 | + if err != nil { |
| 68 | + return nil, fmt.Errorf("reading virtual_methods_size: %w", err) |
| 69 | + } |
| 70 | + |
| 71 | + if virtualMethodsSize == 0 { |
| 72 | + return nil, nil |
| 73 | + } |
| 74 | + |
| 75 | + // Skip static fields. |
| 76 | + for i := uint32(0); i < staticFieldsSize; i++ { |
| 77 | + _, pos, err = readULEB128(data, pos) // field_idx_diff |
| 78 | + if err != nil { |
| 79 | + return nil, fmt.Errorf("skipping static_field[%d] idx_diff: %w", i, err) |
| 80 | + } |
| 81 | + _, pos, err = readULEB128(data, pos) // access_flags |
| 82 | + if err != nil { |
| 83 | + return nil, fmt.Errorf("skipping static_field[%d] access_flags: %w", i, err) |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + // Skip instance fields. |
| 88 | + for i := uint32(0); i < instanceFieldsSize; i++ { |
| 89 | + _, pos, err = readULEB128(data, pos) // field_idx_diff |
| 90 | + if err != nil { |
| 91 | + return nil, fmt.Errorf("skipping instance_field[%d] idx_diff: %w", i, err) |
| 92 | + } |
| 93 | + _, pos, err = readULEB128(data, pos) // access_flags |
| 94 | + if err != nil { |
| 95 | + return nil, fmt.Errorf("skipping instance_field[%d] access_flags: %w", i, err) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + // Skip direct methods. |
| 100 | + for i := uint32(0); i < directMethodsSize; i++ { |
| 101 | + _, pos, err = readULEB128(data, pos) // method_idx_diff |
| 102 | + if err != nil { |
| 103 | + return nil, fmt.Errorf("skipping direct_method[%d] idx_diff: %w", i, err) |
| 104 | + } |
| 105 | + _, pos, err = readULEB128(data, pos) // access_flags |
| 106 | + if err != nil { |
| 107 | + return nil, fmt.Errorf("skipping direct_method[%d] access_flags: %w", i, err) |
| 108 | + } |
| 109 | + _, pos, err = readULEB128(data, pos) // code_off |
| 110 | + if err != nil { |
| 111 | + return nil, fmt.Errorf("skipping direct_method[%d] code_off: %w", i, err) |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + // Iterate virtual methods — these are the proxy methods. |
| 116 | + sigs := MethodSignatures{} |
| 117 | + var methodIdx uint32 |
| 118 | + for i := uint32(0); i < virtualMethodsSize; i++ { |
| 119 | + diff, newPos, err := readULEB128(data, pos) |
| 120 | + if err != nil { |
| 121 | + return nil, fmt.Errorf("reading virtual_method[%d] idx_diff: %w", i, err) |
| 122 | + } |
| 123 | + pos = newPos |
| 124 | + |
| 125 | + _, pos, err = readULEB128(data, pos) // access_flags |
| 126 | + if err != nil { |
| 127 | + return nil, fmt.Errorf("reading virtual_method[%d] access_flags: %w", i, err) |
| 128 | + } |
| 129 | + |
| 130 | + _, pos, err = readULEB128(data, pos) // code_off |
| 131 | + if err != nil { |
| 132 | + return nil, fmt.Errorf("reading virtual_method[%d] code_off: %w", i, err) |
| 133 | + } |
| 134 | + |
| 135 | + methodIdx += diff |
| 136 | + |
| 137 | + _, protoIdx, nameIdx, err := f.readMethodID(methodIdx) |
| 138 | + if err != nil { |
| 139 | + return nil, fmt.Errorf("reading method_id[%d]: %w", methodIdx, err) |
| 140 | + } |
| 141 | + |
| 142 | + nameBytes, err := f.readStringBytes(nameIdx) |
| 143 | + if err != nil { |
| 144 | + return nil, fmt.Errorf("reading method name for method_id[%d]: %w", methodIdx, err) |
| 145 | + } |
| 146 | + |
| 147 | + // Skip Object-inherited and binder-internal methods. |
| 148 | + nameStr := unsafe.String(&nameBytes[0], len(nameBytes)) |
| 149 | + if _, skip := objectMethodNames[nameStr]; skip { |
| 150 | + continue |
| 151 | + } |
| 152 | + |
| 153 | + paramTypeIndices, err := f.readProtoParams(uint32(protoIdx)) |
| 154 | + if err != nil { |
| 155 | + return nil, fmt.Errorf("reading proto params for method_id[%d]: %w", methodIdx, err) |
| 156 | + } |
| 157 | + |
| 158 | + paramDescs := make([]string, len(paramTypeIndices)) |
| 159 | + for pi, typeIdx := range paramTypeIndices { |
| 160 | + desc, err := f.readTypeDescriptor(typeIdx) |
| 161 | + if err != nil { |
| 162 | + return nil, fmt.Errorf("reading param type[%d] for method %s: %w", pi, nameBytes, err) |
| 163 | + } |
| 164 | + paramDescs[pi] = desc |
| 165 | + } |
| 166 | + |
| 167 | + // Allocate a proper string for the map key (nameBytes points |
| 168 | + // into f.data which may be reused). |
| 169 | + sigs[string(nameBytes)] = paramDescs |
| 170 | + } |
| 171 | + |
| 172 | + return sigs, nil |
| 173 | +} |
0 commit comments