将JVM字节码往返的文本表示和返回的故障安全方法 [英] Fail-safe way of round-tripping JVM byte-code to text-representation and back

查看:178
本文介绍了将JVM字节码往返的文本表示和返回的故障安全方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种在JVM类文件和文本表示之间往返的故障安全方法,然后再回来。



一个严格的要求是只要文本表示保持不变,生成的往返JVM类文件在功能上与原始JVM类文件完全等效。



此外,文本表示必须是人类可读和可编辑。应该可以对文本表示进行小的更改(例如更改文本字符串或类名等),这些更改会反映在生成的类文件表示中。



最简单的解决方案是使用Java反编译器(如JAD)生成文本表示,在这种情况下,它只是重新创建的Java源代码。然后使用javac生成字节码。但是,鉴于免费Java反编译器的状态,这种方法在所有情况下都不起作用。创建混淆的字节代码相当容易,这些字节代码不能在完整的往返类文件/ java-source / class-file中存活(部分原因是因为JVM字节代码之间没有1:1的映射) Java源代码)。



根据上述要求,是否存在实现JVM类文件/文本表示/类文件往返的故障安全方法? / p>

更新:在回答之前 - 通过阅读上述所有要求节省时间和精力,并具体说明:




  • JVM字节码的文本表示并不一定意味着Java源代码。


解决方案

BCEL项目提供 JasminVisitor 将类文件转换为 jasmin 程序集。



可以修改它,然后重新组合成类文件。如果没有进行编辑并且版本保持兼容,则往返应该导致相同的类文件,除了行号映射可能丢失。如果您需要为往返案例提供一点点相同的副本,您可能需要更改该工具以获取纯元数据的代码方面。



jasmin相当陈旧,并不是很容易在程序集中编写完整的程序,但是为了修改字符串常量表和常量,它应该是足够的。


I'm looking for a fail-safe way to round-trip between a JVM class file and a text representation and back again.

One strict requirement is that the resulting round-tripped JVM class file is exactly functionally equivalent to the original JVM class file as long as the text representation is left unchanged.

Furthermore, the text representation must be human-readable and editable. It should be possible to make small changes to the the text representation (such as changing a text string or a class name, etc.) which are reflected in the resulting class file representation.

The simplest solution would be to use a Java decompiler such as JAD to generate the text representation, which in this case would simply be the re-created Java source code. And then use javac to generate the byte-code. However, given the state of the free Java decompilers this approach does not work under all circumstances. It is rather easy to create obfuscated byte-code that does not survive a full round-trip class-file/java-source/class-file (in part because there simply isn't a 1:1 mapping between JVM byte-code and Java source code).

Is there a fail-safe way to achieve JVM class-file/text-representation/class-file round-tripping given the requirements above?

Update: Before answering - save time and effort by reading all the requirements above, and note specifically:

  • "Text-representation of JVM bytecode" does not necessarily mean "Java source-code".

解决方案

The BCEL project provides a JasminVisitor which will convert class files into jasmin assembly.

This can be modified and then reassembled into class files. If no edits are made and the versions are kept compatible the the round trip should result in identical class files except that line number mapping may be lost. If you require a bit for bit identical copy for the round trip case you will likely need to alter the tool to take aspects of the code which are pure meta data as well.

jasmin is rather old and is not designed with ease of actually writing full blown programs in assembly but for modifying string constant tables and constants it should be more than adequate.

这篇关于将JVM字节码往返的文本表示和返回的故障安全方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆