Skip to content

Commit

Permalink
Merge branch 'hotfix/0.1.8'
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelRocks committed Feb 22, 2018
2 parents 4714c21 + a51e69b commit c41343b
Show file tree
Hide file tree
Showing 18 changed files with 181 additions and 37 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ buildscript {
}
dependencies {
classpath 'io.michaelrocks:paranoid-gradle-plugin:0.1.7'
classpath 'io.michaelrocks:paranoid-gradle-plugin:0.1.8'
}
}
Expand Down
9 changes: 6 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
allprojects {
ext.kotlinVersion = '1.1.51'
ext.kotlinVersion = '1.2.21'

buildscript {
repositories {
Expand Down Expand Up @@ -38,6 +38,9 @@ ext {

junitVersion = '4.12'

androidToolsVersion = '3.0.0'
supportVersion = '27.0.0'
androidToolsVersion = '3.0.1'
supportVersion = '27.0.2'

runnerVersion = '1.0.1'
espressoVersion = '3.0.1'
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 Michael Rozumyanskiy
* Copyright 2018 Michael Rozumyanskiy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -54,7 +54,8 @@ class ParanoidTransform(private val android: BaseExtension) : Transform() {
classpath = invocation.referencedInputs.flatMap {
it.jarInputs.map { it.file } + it.directoryInputs.map { it.file }
},
bootClasspath = android.bootClasspath
bootClasspath = android.bootClasspath,
projectName = invocation.context.path.replace(":transformClassesWithParanoidFor", ":").replace(':', '$')
)

try {
Expand Down Expand Up @@ -100,6 +101,10 @@ class ParanoidTransform(private val android: BaseExtension) : Transform() {
return false
}

override fun getParameterInputs(): MutableMap<String, Any> {
return mutableMapOf("version" to Build.VERSION)
}

private fun TransformOutputProvider.getContentLocation(
name: String,
contentType: QualifiedContent.ContentType,
Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.2.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.5.1-all.zip
6 changes: 3 additions & 3 deletions gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ DEFAULT_JVM_OPTS=""
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"

warn ( ) {
warn () {
echo "$*"
}

die ( ) {
die () {
echo
echo "$*"
echo
Expand Down Expand Up @@ -155,7 +155,7 @@ if $cygwin ; then
fi

# Escape application args
save ( ) {
save () {
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
echo " "
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 Michael Rozumyanskiy
* Copyright 2018 Michael Rozumyanskiy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,7 @@
package io.michaelrocks.paranoid.processor

import io.michaelrocks.paranoid.processor.logging.getLogger
import io.michaelrocks.paranoid.processor.model.Deobfuscator
import java.io.File
import java.util.Collections
import java.util.HashSet
Expand All @@ -25,7 +26,10 @@ import javax.tools.JavaFileObject
import javax.tools.StandardLocation
import javax.tools.ToolProvider

class Generator(private val stringRegistry: StringRegistry) {
class Generator(
private val deobfuscator: Deobfuscator,
private val stringRegistry: StringRegistry
) {
private val logger = getLogger()

fun generateDeobfuscator(
Expand All @@ -35,7 +39,7 @@ class Generator(private val stringRegistry: StringRegistry) {
bootClasspath: Collection<File>
) {
val sourceCode = generateDeobfuscatorSourceCode()
val sourceFile = File(sourcePath, "${DEOBFUSCATOR_TYPE.internalName}.java")
val sourceFile = File(sourcePath, "${deobfuscator.type.internalName}.java")

sourceFile.parentFile.mkdirs()
genPath.mkdirs()
Expand Down Expand Up @@ -107,7 +111,7 @@ class Generator(private val stringRegistry: StringRegistry) {
val locationChunkCount = (stringCount + chunkSize - 1) / chunkSize

return buildString {
val internalName = DEOBFUSCATOR_TYPE.internalName
val internalName = deobfuscator.type.internalName
val packageName = internalName.substringBeforeLast('/').replace('/', '.')
val className = internalName.substringAfterLast('/')
appendln("package $packageName;")
Expand Down Expand Up @@ -169,7 +173,7 @@ class Generator(private val stringRegistry: StringRegistry) {
}

appendln()
appendln(" public static String ${DEOBFUSCATION_METHOD.name}(final int id) {")
appendln(" public static String ${deobfuscator.deobfuscationMethod.name}(final int id) {")
appendln(" final int offset = locations[id * 2];")
appendln(" final int length = locations[id * 2 + 1];")
appendln(" final char[] stringChars = new char[length];")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 Michael Rozumyanskiy
* Copyright 2018 Michael Rozumyanskiy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,7 +18,11 @@ package io.michaelrocks.paranoid.processor

import io.michaelrocks.grip.Grip
import io.michaelrocks.grip.GripFactory
import io.michaelrocks.grip.mirrors.getObjectTypeByInternalName
import io.michaelrocks.paranoid.processor.logging.getLogger
import io.michaelrocks.paranoid.processor.model.Deobfuscator
import org.objectweb.asm.Type
import org.objectweb.asm.commons.Method
import java.io.File

class ParanoidProcessor(
Expand All @@ -27,7 +31,8 @@ class ParanoidProcessor(
private val sourcePath: File,
private val genPath: File,
private val classpath: Collection<File>,
private val bootClasspath: Collection<File>
private val bootClasspath: Collection<File>,
private val projectName: String
) {
private val logger = getLogger()

Expand All @@ -41,8 +46,11 @@ class ParanoidProcessor(

val analysisResult = Analyzer(grip).analyze(inputs)
analysisResult.dump()
Patcher(stringRegistry, grip.classRegistry).copyAndPatchClasses(inputs, outputs, analysisResult)
Generator(stringRegistry).generateDeobfuscator(sourcePath, genPath, outputs + classpath, bootClasspath)

val deobfuscator = createDeobfuscator()
logger.info("Prepare to generate {}", deobfuscator)
Patcher(deobfuscator, stringRegistry, grip.classRegistry).copyAndPatchClasses(inputs, outputs, analysisResult)
Generator(deobfuscator, stringRegistry).generateDeobfuscator(sourcePath, genPath, outputs + classpath, bootClasspath)
}

private fun AnalysisResult.dump() {
Expand All @@ -60,4 +68,20 @@ class ParanoidProcessor(
}
}
}

private fun createDeobfuscator(): Deobfuscator {
val deobfuscatorInternalName = "io/michaelrocks/paranoid/Deobfuscator${composeDeobfuscatorNameSuffix()}"
val deobfuscatorType = getObjectTypeByInternalName(deobfuscatorInternalName)
val deobfuscationMethod = Method("getString", Type.getType(String::class.java), arrayOf(Type.INT_TYPE))
return Deobfuscator(deobfuscatorType, deobfuscationMethod)
}

private fun composeDeobfuscatorNameSuffix(): String {
val normalizedProjectName = projectName.filter { it.isLetterOrDigit() || it == '_' || it == '$' }
return if (normalizedProjectName.isEmpty() || normalizedProjectName.startsWith('$')) {
normalizedProjectName
} else {
'$' + normalizedProjectName
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 Michael Rozumyanskiy
* Copyright 2018 Michael Rozumyanskiy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,11 +20,13 @@ import io.michaelrocks.grip.ClassRegistry
import io.michaelrocks.grip.mirrors.Type
import io.michaelrocks.grip.mirrors.getObjectTypeByInternalName
import io.michaelrocks.paranoid.processor.logging.getLogger
import io.michaelrocks.paranoid.processor.model.Deobfuscator
import org.objectweb.asm.ClassReader
import org.objectweb.asm.ClassWriter
import java.io.File

class Patcher(
private val deobfuscator: Deobfuscator,
private val stringRegistry: StringRegistry,
private val classRegistry: ClassRegistry
) {
Expand Down Expand Up @@ -73,7 +75,7 @@ class Patcher(
logger.debug(" Target: {}", targetFile)
val reader = ClassReader(sourceFile.readBytes())
val writer = StandaloneClassWriter(reader, ClassWriter.COMPUTE_MAXS or ClassWriter.COMPUTE_FRAMES, classRegistry)
val stringLiteralsPatcher = StringLiteralsClassPatcher(stringRegistry, writer)
val stringLiteralsPatcher = StringLiteralsClassPatcher(deobfuscator, stringRegistry, writer)
val stringConstantsPatcher = StringConstantsClassPatcher(configuration, stringLiteralsPatcher)
reader.accept(stringConstantsPatcher, ClassReader.SKIP_FRAMES)
targetFile.parentFile.mkdirs()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 Michael Rozumyanskiy
* Copyright 2018 Michael Rozumyanskiy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -33,7 +33,7 @@ class StringConstantsClassPatcher(
delegate: ClassVisitor
) : ClassVisitor(ASM5, delegate) {

private val logger = getLogger()
private val logger = getLogger()

private var isStaticInitializerPatched = false

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 Michael Rozumyanskiy
* Copyright 2018 Michael Rozumyanskiy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,13 +16,16 @@

package io.michaelrocks.paranoid.processor

import io.michaelrocks.grip.mirrors.toAsmType
import io.michaelrocks.paranoid.processor.logging.getLogger
import io.michaelrocks.paranoid.processor.model.Deobfuscator
import org.objectweb.asm.ClassVisitor
import org.objectweb.asm.MethodVisitor
import org.objectweb.asm.Opcodes.ASM5
import org.objectweb.asm.commons.GeneratorAdapter

class StringLiteralsClassPatcher(
private val deobfuscator: Deobfuscator,
private val stringRegistry: StringRegistry,
delegate: ClassVisitor
) : ClassVisitor(ASM5, delegate) {
Expand Down Expand Up @@ -65,7 +68,7 @@ class StringLiteralsClassPatcher(
logger.info(" Obfuscating string literal: \"{}\"", string)
val stringId = stringRegistry.registerString(string)
push(stringId)
invokeStatic(DEOBFUSCATOR_TYPE, DEOBFUSCATION_METHOD)
invokeStatic(deobfuscator.type.toAsmType(), deobfuscator.deobfuscationMethod)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 Michael Rozumyanskiy
* Copyright 2018 Michael Rozumyanskiy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,13 +16,8 @@

package io.michaelrocks.paranoid.processor

import org.objectweb.asm.Type
import org.objectweb.asm.commons.Method
import java.util.HashMap

val DEOBFUSCATOR_TYPE = Type.getObjectType("io/michaelrocks/paranoid/Deobfuscator")
val DEOBFUSCATION_METHOD: Method = Method("getString", Type.getType(String::class.java), arrayOf(Type.INT_TYPE))

interface StringRegistry {
fun registerString(string: String): Int
fun getAllIds(): Collection<Int>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2018 Michael Rozumyanskiy
*
* 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 io.michaelrocks.paranoid.processor.model

import io.michaelrocks.grip.mirrors.Type
import org.objectweb.asm.commons.Method

data class Deobfuscator(
val type: Type.Object,
val deobfuscationMethod: Method
)
11 changes: 9 additions & 2 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ apply plugin: 'io.michaelrocks.paranoid'

android {
compileSdkVersion 27
buildToolsVersion "27.0.0"
buildToolsVersion "27.0.3"

defaultConfig {
applicationId "io.michaelrocks.paranoid.sample"
minSdkVersion 16
targetSdkVersion 27
versionCode 1
versionName "1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

signingConfigs {
Expand All @@ -32,7 +34,7 @@ android {

buildTypes {
debug {
minifyEnabled true
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}

Expand All @@ -46,4 +48,9 @@ android {

dependencies {
implementation "com.android.support:appcompat-v7:$supportVersion"

testImplementation "junit:junit:$junitVersion"

androidTestImplementation "com.android.support.test:runner:$runnerVersion"
androidTestImplementation "com.android.support.test.espresso:espresso-core:$espressoVersion"
}
Loading

0 comments on commit c41343b

Please sign in to comment.