summaryrefslogtreecommitdiffstats
path: root/jack/src/com/android/jack/shrob/obfuscation/Renamer.java
blob: 8f5e282f5e2330b669acf8f6cbf690f3d41b93c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package com.android.jack.shrob.obfuscation;

import com.android.jack.Jack;
import com.android.jack.ir.ast.CanBeRenamed;
import com.android.jack.ir.ast.HasName;
import com.android.jack.ir.ast.JClassOrInterface;
import com.android.jack.ir.ast.JDefinedClassOrInterface;
import com.android.jack.ir.ast.JField;
import com.android.jack.ir.ast.JFieldId;
import com.android.jack.ir.ast.JMethod;
import com.android.jack.ir.ast.JMethodId;
import com.android.jack.ir.ast.JPackage;
import com.android.jack.ir.ast.JSession;
import com.android.jack.ir.ast.JVisitor;
import com.android.jack.shrob.obfuscation.nameprovider.NameProvider;
import com.android.jack.shrob.proguard.GrammarActions;
import com.android.jack.transformations.request.ChangeEnclosingPackage;
import com.android.jack.transformations.request.TransformationRequest;
import com.android.jack.util.PackageCodec;
import com.android.sched.item.Description;
import com.android.sched.marker.MarkerManager;
import com.android.sched.schedulable.Constraint;
import com.android.sched.schedulable.RunnableSchedulable;
import com.android.sched.schedulable.Transform;
import com.android.sched.schedulable.Use;
import com.android.sched.util.codec.PathCodec;
import com.android.sched.util.config.HasKeyId;
import com.android.sched.util.config.ThreadConfig;
import com.android.sched.util.config.id.BooleanPropertyId;
import com.android.sched.util.config.id.PropertyId;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

/**
 * Visitor that renames {@code JNode}s
 */
@HasKeyId
@Description("Visitor that renames JNodes")
@Constraint(need = {KeepNameMarker.class, OriginalNames.class}, no = FinalNames.class)
@Transform(remove = OriginalNames.class,
    add = {OriginalNameMarker.class, OriginalPackageMarker.class, FinalNames.class})
@Use(MappingApplier.class)
public class Renamer implements RunnableSchedulable<JSession> {

  @Nonnull
  public static final
      BooleanPropertyId USE_PACKAGE_OBFUSCATION_DICTIONARY = BooleanPropertyId.create(
          "jack.obfuscation.packagedictionary",
          "Use obfuscation dictionary for packages").addDefaultValue(Boolean.FALSE);

  @Nonnull
  public static final PropertyId<File> PACKAGE_OBFUSCATION_DICTIONARY = PropertyId.create(
      "jack.obfuscation.packagedictionary.file", "Obfuscation dictionary for packages",
      new PathCodec()).requiredIf(Renamer.USE_PACKAGE_OBFUSCATION_DICTIONARY.getValue().isTrue());

  @Nonnull
  public static final BooleanPropertyId USE_CLASS_OBFUSCATION_DICTIONARY = BooleanPropertyId.create(
      "jack.obfuscation.classdictionary", "Use obfuscation dictionary for classes")
      .addDefaultValue(Boolean.FALSE);

  @Nonnull
  public static final PropertyId<File> CLASS_OBFUSCATION_DICTIONARY = PropertyId.create(
      "jack.obfuscation.classdictionary.file", "Obfuscation dictionary for classes",
      new PathCodec()).requiredIf(USE_CLASS_OBFUSCATION_DICTIONARY.getValue().isTrue());

  @Nonnull
  public static final BooleanPropertyId USE_OBFUSCATION_DICTIONARY = BooleanPropertyId.create(
      "jack.obfuscation.dictionary", "Use obfuscation dictionary for members")
      .addDefaultValue(Boolean.FALSE);

  @Nonnull
  public static final PropertyId<File> OBFUSCATION_DICTIONARY = PropertyId.create(
      "jack.obfuscation.dictionary.file", "Obfuscation dictionary for members",
      new PathCodec()).requiredIf(USE_OBFUSCATION_DICTIONARY.getValue().isTrue());

  @Nonnull
  public static final BooleanPropertyId USE_MAPPING = BooleanPropertyId.create(
      "jack.obfuscation.mapping", "Use mapping for types and members")
      .addDefaultValue(Boolean.FALSE);

  @Nonnull
  public static final PropertyId<File> MAPPING_FILE = PropertyId.create(
      "jack.obfuscation.mapping.file",
      "File containing the mapping of all types and members", new PathCodec())
      .addDefaultValue("mapping.txt");

  @Nonnull
  public static final BooleanPropertyId REPACKAGE_CLASSES = BooleanPropertyId.create(
      "jack.obfuscation.repackageclasses",
      "Change package for all renamed classes")
      .addDefaultValue(Boolean.FALSE);

  @Nonnull
  public static final PropertyId<String> PACKAGE_FOR_RENAMED_CLASSES = PropertyId.create(
      "jack.obfuscation.repackageclasses.package",
      "Enclosing package for all renamed classes", new PackageCodec())
      .requiredIf(REPACKAGE_CLASSES.getValue().isTrue());

  @Nonnull
  public static final BooleanPropertyId FLATTEN_PACKAGE = BooleanPropertyId.create(
      "jack.obfuscation.flattenpackage",
      "Change package for all renamed packages")
      .addDefaultValue(Boolean.FALSE);

  @Nonnull
  public static final PropertyId<String> PACKAGE_FOR_RENAMED_PACKAGES = PropertyId.create(
      "jack.obfuscation.flattenpackage.package",
      "Enclosing package for all renamed packages", new PackageCodec())
      .requiredIf(FLATTEN_PACKAGE.getValue().isTrue());

  @Nonnull
  public static final BooleanPropertyId USE_UNIQUE_CLASSMEMBERNAMES = BooleanPropertyId.create(
      "jack.obfuscation.uniqueclassmembernames",
      "All members with the same name must have the same obfuscated name")
      .addDefaultValue(Boolean.FALSE);

  public static boolean mustBeRenamed(@Nonnull MarkerManager node) {
    return !node.containsMarker(KeepNameMarker.class)
        && !node.containsMarker(OriginalNameMarker.class);
  }

  @Nonnull
  static String getFieldKey(@Nonnull JFieldId fieldId) {
    return fieldId.getName() + ':'
        + GrammarActions.getSignatureFormatter().getName(fieldId.getType());
  }

  @Nonnull
  static String getKey(@Nonnull HasName namedElement) {
    if (namedElement instanceof JFieldId) {
      return Renamer.getFieldKey((JFieldId) namedElement);
    } else if (namedElement instanceof JMethodId) {
      JMethodId mid = (JMethodId) namedElement;
      return GrammarActions.getSignatureFormatter().getNameWithoutReturnType(mid);
    } else {
      return namedElement.getName();
    }
  }

  private static void rename(@Nonnull CanBeRenamed node, @Nonnull NameProvider nameProvider) {
    if (mustBeRenamed((MarkerManager) node)) {
      String newName = nameProvider.getNewName(getKey(node));
      ((MarkerManager) node).addMarker(new OriginalNameMarker(node.getName()));
      node.setName(newName);
    }
  }

  private class Visitor extends JVisitor {

    @Override
    public boolean visit(@Nonnull JPackage pack) {
      List<JPackage> subPackages = pack.getSubPackages();
      NameProvider packageNameProvider = nameProviderFactory.getPackageNameProvider(subPackages);
      for (JPackage subPack : subPackages) {
        rename(subPack, packageNameProvider);
      }

      List<JDefinedClassOrInterface> types = pack.getTypes();
      NameProvider classNameProvider = nameProviderFactory.getClassNameProvider(types);
      for (JClassOrInterface type : types) {
        if (type instanceof JDefinedClassOrInterface) {
          rename((JDefinedClassOrInterface) type, classNameProvider);
        }
      }
      return super.visit(pack);
    }

    @Nonnull
    private Collection<JFieldId> collectAllFieldIdsInHierarchy(
        @Nonnull JDefinedClassOrInterface referenceType) {
      List<JFieldId> collectedFields = new ArrayList<JFieldId>();
      assert allTypes != null;
      for (JDefinedClassOrInterface type : allTypes) {
        if (referenceType.canBeSafelyUpcast(type) || type.canBeSafelyUpcast(referenceType)) {
          for (JField field : type.getFields()) {
            collectedFields.add(field.getId());
          }
        }
      }
      return collectedFields;
    }

    @Nonnull
    private Collection<JMethodId> collectAllMethodIdsInHierarchy(
        @Nonnull JDefinedClassOrInterface referenceType) {
      Set<JMethodId> collectedMethods = new HashSet<JMethodId>();
      assert allTypes != null;
      for (JDefinedClassOrInterface type : allTypes) {
        if (referenceType.canBeSafelyUpcast(type) || type.canBeSafelyUpcast(referenceType)) {
          for (JMethod method : type.getMethods()) {
            collectedMethods.add(method.getMethodId());
          }
        }
      }
      return collectedMethods;
    }

    @Override
    public boolean visit(@Nonnull JDefinedClassOrInterface type) {
      if (!type.isExternal()) {
        Collection<JFieldId> allFieldsInHierarchy = collectAllFieldIdsInHierarchy(type);
        NameProvider fieldNameProvider =
            nameProviderFactory.getFieldNameProvider(allFieldsInHierarchy);
        for (JField field : type.getFields()) {
          rename(field.getId(), fieldNameProvider);
        }

        Collection<JMethodId> allMethodsInHierarchy = collectAllMethodIdsInHierarchy(type);
        NameProvider methodNameProvider =
            nameProviderFactory.getMethodNameProvider(allMethodsInHierarchy);
        for (JMethod method : type.getMethods()) {
          rename(method.getMethodId(), methodNameProvider);
        }
      }

      return false;
    }
  }

  private class RepackagerVisitor extends Visitor {

    @Nonnull
    private final TransformationRequest request;

    @Nonnull
    private final String packageNameForRenamedClasses =
        ThreadConfig.get(PACKAGE_FOR_RENAMED_CLASSES);

    @Nonnull
    private final JPackage packageForRenamedClasses
      = Jack.getSession().getLookup().getOrCreatePackage(packageNameForRenamedClasses);

    @Nonnull
    private final NameProvider classNameProvider =
          nameProviderFactory.getClassNameProvider(packageForRenamedClasses.getTypes());

    private RepackagerVisitor(@Nonnull TransformationRequest request) {
      this.request = request;
    }

    @Override
    public boolean visit(@Nonnull JPackage pack) {

      for (JClassOrInterface type : pack.getTypes()) {
        if (mustBeRenamed((MarkerManager) type)) {
          JPackage oldEnclosingPackage = type.getEnclosingPackage();
          assert oldEnclosingPackage != null;
          ((MarkerManager) type).addMarker(
              new OriginalPackageMarker(oldEnclosingPackage));
          request.append(new ChangeEnclosingPackage(type, packageForRenamedClasses));
          rename((JDefinedClassOrInterface) type, classNameProvider);
        }
      }

      return super.visit(pack);
    }
  }

  private class FlattenerVisitor extends Visitor {

    @Nonnull
    private final TransformationRequest request;

    @Nonnull
    private final String packageNameForRenamedPackages =
        ThreadConfig.get(PACKAGE_FOR_RENAMED_PACKAGES);

    @Nonnull
    private final JPackage packageForRenamedPackages
      = Jack.getSession().getLookup().getOrCreatePackage(packageNameForRenamedPackages);

    @Nonnull
    private final NameProvider packageNameProvider =
        nameProviderFactory.getPackageNameProvider(packageForRenamedPackages.getSubPackages());

    private FlattenerVisitor(@Nonnull TransformationRequest request) {
      this.request = request;
    }

    @Override
    public boolean visit(@Nonnull JPackage pack) {
      List<JPackage> subPackages = pack.getSubPackages();
      for (JPackage subPack : subPackages) {
        if (mustBeRenamed(subPack) && !subPack.equals(packageForRenamedPackages)) {
          request.append(new ChangeEnclosingPackage(subPack, packageForRenamedPackages));
          subPack.addMarker(new OriginalPackageMarker(pack));
          rename(subPack, packageNameProvider);
        }
      }

      List<JDefinedClassOrInterface> types = pack.getTypes();
      NameProvider classNameProvider = nameProviderFactory.getClassNameProvider(types);
      for (JClassOrInterface type : types) {
        if (type instanceof JDefinedClassOrInterface) {
          rename((JDefinedClassOrInterface) type, classNameProvider);
        }
      }

      return true;
    }
  }

  @Nonnull
  private final NameProviderFactory nameProviderFactory;

  @CheckForNull
  private Collection<JDefinedClassOrInterface> allTypes;

  public Renamer() {
    File dictionary = null;
    if (ThreadConfig.get(USE_OBFUSCATION_DICTIONARY).booleanValue()) {
      dictionary = ThreadConfig.get(OBFUSCATION_DICTIONARY);
    }
    File classDictionary = null;
    if (ThreadConfig.get(USE_CLASS_OBFUSCATION_DICTIONARY).booleanValue()) {
      classDictionary = ThreadConfig.get(CLASS_OBFUSCATION_DICTIONARY);
    }
    File packageDictionary = null;
    if (ThreadConfig.get(USE_PACKAGE_OBFUSCATION_DICTIONARY).booleanValue()) {
      packageDictionary = ThreadConfig.get(PACKAGE_OBFUSCATION_DICTIONARY);
    }

    this.nameProviderFactory = new NameProviderFactory(
        dictionary,
        classDictionary,
        packageDictionary);
  }

  @Override
  public void run(@Nonnull JSession session) throws Exception {
    allTypes = session.getTypesToEmit();
    Map<String, String> fieldNames = new HashMap<String, String>();
    Map<String, String> methodNames = new HashMap<String, String>();
    boolean useUniqueClassMemberNames =
        ThreadConfig.get(USE_UNIQUE_CLASSMEMBERNAMES).booleanValue();
    if (ThreadConfig.get(USE_MAPPING).booleanValue()) {
      TransformationRequest request = new TransformationRequest(session);
      MappingApplier mappingApplier;
      if (useUniqueClassMemberNames) {
        mappingApplier = new CollectingMappingApplier(request);
        fieldNames = ((CollectingMappingApplier) mappingApplier).getFieldNames();
        methodNames = ((CollectingMappingApplier) mappingApplier).getMethodNames();
      } else {
        mappingApplier = new MappingApplier(request);
      }
      mappingApplier.applyMapping(ThreadConfig.get(MAPPING_FILE), session);
      request.commit();
    }

    if (useUniqueClassMemberNames) {
      Set<JFieldId> allFieldIds = new HashSet<JFieldId>();
      Set<JMethodId> allMethodIds = new HashSet<JMethodId>();
      for (JDefinedClassOrInterface type : allTypes) {
        for (JField field : type.getFields()) {
          allFieldIds.add(field.getId());
        }
        for (JMethod method : type.getMethods()) {
          allMethodIds.add(method.getMethodId());
        }
      }
      nameProviderFactory.createGlobalFieldNameProvider(fieldNames, allFieldIds);
      nameProviderFactory.createGlobalMethodNameProvider(methodNames, allMethodIds);
    }

    if (ThreadConfig.get(REPACKAGE_CLASSES).booleanValue()) {
      TransformationRequest request = new TransformationRequest(session);
      Visitor visitor = new RepackagerVisitor(request);
      visitor.accept(session);
      request.commit();
    } else if (ThreadConfig.get(FLATTEN_PACKAGE).booleanValue()) {
      TransformationRequest request = new TransformationRequest(session);
      Visitor visitor = new FlattenerVisitor(request);
      visitor.accept(session);
      request.commit();
    } else {
      Visitor visitor = new Visitor();
      visitor.accept(session);
    }
  }
}