Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try using CFR for decompilation #13

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion deps.edn
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{:paths ["src"]
:deps {org.clojure/clojure {:mvn/version "1.11.3" :mvn/scope "provided"}
org.bitbucket.mstrobel/procyon-compilertools {:mvn/version "0.6.0"}}
org.bitbucket.mstrobel/procyon-compilertools {:mvn/version "0.6.0"}
org.benf/cfr {:mvn/version "0.152"}
}

:aliases
{:build {:deps {io.github.clojure/tools.build {:mvn/version "0.10.0"}
Expand Down
54 changes: 44 additions & 10 deletions src/clj_java_decompiler/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
(com.strobel.decompiler DecompilationOptions DecompilerSettings
PlainTextOutput)
com.strobel.decompiler.languages.Languages
(org.benf.cfr.reader.api CfrDriver$Builder
OutputSinkFactory
OutputSinkFactory$Sink
OutputSinkFactory$SinkClass
OutputSinkFactory$SinkType)
java.io.File))

(defonce ^:private tmp-dir
Expand Down Expand Up @@ -146,19 +151,34 @@
(-> s simplify-members replace-consts-with-var-names)
s))

(defn- decompile-classfile
(defn- bytecode-decompile-classfile
"Decompile the given classfile and print the result to stdout."
[file options]
(let [type (resolve-class-from-file file)
decompiler (if (= (:decompiler options) :bytecode)
bytecode-decompiler
java-decompiler)
[file]
(let [type' (resolve-class-from-file file)
decomp-options (doto (DecompilationOptions.)
(.setSettings (doto (DecompilerSettings.)
(.setSimplifyMemberReferences true))))
output (PlainTextOutput.)]
(println "\n// Decompiling class:" (.getInternalName type))
(.decompileType decompiler type output decomp-options)
(.decompileType bytecode-decompiler type' output decomp-options)
(println (postprocess-decompiler-output (str output)))))

(defn- java-decompile-classfile
[file]
(let [output (java.io.StringWriter.)
sink (reify OutputSinkFactory
(getSupportedSinks [_ _ _]
[OutputSinkFactory$SinkClass/STRING])
(getSink [_ sink-type _]
(reify OutputSinkFactory$Sink
(write
[_ obj]
(when (= OutputSinkFactory$SinkType/JAVA sink-type)
(.append output (str obj)))))))
cfr-driver (.. (CfrDriver$Builder.)
(withOutputSink sink)
(withBuiltOptions nil)
(build))]
(.analyse cfr-driver [(str file)])
(println (postprocess-decompiler-output (str output)))))

(defn decompile-form
Expand All @@ -167,8 +187,14 @@
[options form]
(try
(aot-compile form)
(let [first-sym (when (seq? form) (first form))]
(run! #(decompile-classfile % options) (list-compiled-classes first-sym)))
(let [first-sym (when (seq? form) (first form))
files (list-compiled-classes first-sym)
decompiler (if (= (:decompiler options) :bytecode)
bytecode-decompile-classfile
java-decompile-classfile)]
(doseq [file files]
(println "\n// Decompiling class:" (.getInternalName (resolve-class-from-file file)))
(decompiler file)))
(finally (cleanup-tmp-dir))))

(defmacro decompile
Expand All @@ -184,6 +210,8 @@

(comment

(decompile (str "hello"))

(binding [*compiler-options* {:direct-linking true}]
(decompile
(defn turtles "mydocs" []
Expand All @@ -192,6 +220,12 @@
(binding [*compiler-options* {:direct-linking true}]
(decompile (fn [] (println (str "Hello, decompiler!")))))

(decompile
(letfn [(foo [s]
(println
(str "Hello," s " decompiler!")))]
(foo "world and")))

(disassemble (fn [] (println "Hello, decompiler!")))

(decompile
Expand Down