JesusFreke / smali

smali/baksmali
6.29k stars 1.07k forks source link

Can't handle names with spaces in them #795

Closed drosseau closed 3 years ago

drosseau commented 3 years ago

This is a valid Kotlin program:

class ` `(private val ` `: String) {
    fun `  `() {
        println(this.` `)
    }
}

fun main(argv: Array<String>) {
    argv.forEach { ` `(it).`  `() }
}
$ kotlinc Test.kt -include-runtime -d test.jar
$ java -jar test.jar Testing this abomination.
Testing
this
abomination.

Convert to dex:

$ d2j-jar2dex.sh -f test.jar

baksmali doesn't have any errors:

java -jar baksmali.jar d test-jar2dex.dex
$ ls out
 kotlin  ' .smali'   TestKt.smali
$ java -jar smali.jar a out
out/TestKt.smali[71,21] no viable alternative at input 'L'
out/ .smali[1,22] Error for input ';': Invalid text
out/ .smali[1,20] mismatched input 'L' expecting CLASS_DESCRIPTOR
out/TestKt.smali[71,23] Error for input ';': Invalid text
out/TestKt.smali[73,30] Error for input ';': Invalid text
out/TestKt.smali[73,31] mismatched input '->' expecting OPEN_PAREN
out/TestKt.smali[75,27] Error for input ';': Invalid text
out/TestKt.smali[75,28] extraneous input '->' expecting OPEN_PAREN
out/ .smali[53,26] Error for input ';': Invalid text
out/ .smali[53,27] extraneous input '->' expecting COLON
out/ .smali[65,26] Error for input ';': Invalid text
out/ .smali[65,27] extraneous input '->' expecting COLON

Examples of some of the bad lines:

.class public final L ;
....
.field private final  :Ljava/lang/String;
....
.method public final   ()V
....
    iget-object v0, p0, L ;-> :Ljava/lang/String;
....
    new-instance v7, L ;

    invoke-direct {v7, v4}, L ;-><init>(Ljava/lang/String;)V

    invoke-virtual {v7}, L ;->  ()V

Names such as this is a class are valid too, but cause the same issues.

Versions of everything:

java -jar baksmali.jar -v
baksmali 2.4.0-c1534fc9-dirty (http://smali.org)
Copyright (C) 2010 Ben Gruver (JesusFreke@JesusFreke.com)
BSD license (http://www.opensource.org/licenses/bsd-license.php)
java -jar smali.jar -v
smali 2.4.0-c1534fc9-dirty (http://smali.org)
Copyright (C) 2010 Ben Gruver (JesusFreke@JesusFreke.com)
BSD license (http://www.opensource.org/licenses/bsd-license.php)
kotlinc -version
info: kotlinc-jvm 1.4.21 (JRE 11.0.9.1+1-post-Debian-1)
java -version
openjdk version "11.0.9.1" 2020-11-04
OpenJDK Runtime Environment (build 11.0.9.1+1-post-Debian-1)
OpenJDK 64-Bit Server VM (build 11.0.9.1+1-post-Debian-1, mixed mode, sharing)

Update:

Sadly

class `🍉` (private val `🍿`: String)  {
    fun `🐇`() {
        println(`🍿`)
    }
}
fun main(`😀`: Array<String>) {
    `😀`.forEach { `🍉`(it).`🐇`() }
}

is also valid and has the same behavior.

drosseau commented 3 years ago

I noticed that if I go into the file and rename things that are just whitespace to whitespace with backtics around it smali won't fail anymore, but I'm not sure if it is creating a valid dex.

For example,

.method public static final  (Ljava/lang/String;)V

becomes

.method public static final ` `(Ljava/lang/String;)V
JesusFreke commented 3 years ago

Hmm, yeah. Maybe a bug in the logic used to detect if a name needs to be quoted or not.

JesusFreke commented 3 years ago

Huh, apparently support for names with spaces was never added to baksmali. I could have sworn it was added at the same time support was added for smali.

jackwpa commented 3 years ago

Per the specs, white spaces and similar chars in SimpleNames are allowed only since version 40: https://source.android.com/devices/tech/dalvik/dex-format#simplename

(I remember seeing this in a JEB changelog only a few months ago.)

JesusFreke commented 3 years ago

Yeah, support was added to smali in 586ec594e2efd35dfe8d31cc221208fc886851dd. I guess I didn't think about support in baksmali at the time.

JesusFreke commented 3 years ago

This is now supported in baksmali, as of v2.5.0