Add checkstyle config and Javadoc comments

* Also do some minor rename and refactor
This commit is contained in:
Dhruv Maroo
2024-02-05 11:35:33 +05:30
parent e6f0083cf0
commit 8355b1f763
8 changed files with 535 additions and 74 deletions

378
checkstyle.xml Normal file
View File

@@ -0,0 +1,378 @@
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
"https://checkstyle.org/dtds/configuration_1_3.dtd">
<!--
Checkstyle configuration that checks (slightly modified) Google coding
conventions from slightly modified Google Java Style (the original can be
found at https://google.github.io/styleguide/javaguide.html).
Checkstyle is very configurable. Be sure to read the documentation at
http://checkstyle.org (or in your downloaded distribution).
To completely disable a check, just comment it out or delete it from the file.
To suppress certain violations please review suppression filters.
Authors: Max Vetrenko, Ruslan Diachenko, Roman Ivanov, Dhruv Maroo (modifier).
-->
<module name="Checker">
<module name="SuppressWarningsFilter"/>
<module name="SuppressWithPlainTextCommentFilter"/>
<property name="charset" value="UTF-8"/>
<property name="severity" value="warning"/>
<property name="fileExtensions" value="java, properties, xml"/>
<!-- Excludes all 'module-info.java' files -->
<!-- See https://checkstyle.org/filefilters/index.html -->
<module name="BeforeExecutionExclusionFileFilter">
<property name="fileNamePattern" value="module\-info\.java$"/>
</module>
<!-- https://checkstyle.org/filters/suppressionfilter.html -->
<module name="SuppressionFilter">
<property name="file" value="${org.checkstyle.google.suppressionfilter.config}"
default="checkstyle-suppressions.xml" />
<property name="optional" value="true"/>
</module>
<!-- Checks for whitespace -->
<!-- See http://checkstyle.org/checks/whitespace/index.html -->
<module name="FileTabCharacter">
<property name="eachLine" value="true"/>
</module>
<module name="LineLength">
<property name="fileExtensions" value="java"/>
<property name="max" value="100"/>
<property name="ignorePattern" value="^package.*|^import.*|a href|href|http://|https://|ftp://"/>
</module>
<module name="TreeWalker">
<module name="OuterTypeFilename"/>
<module name="IllegalTokenText">
<property name="tokens" value="STRING_LITERAL, CHAR_LITERAL"/>
<property name="format"
value="\\u00(09|0(a|A)|0(c|C)|0(d|D)|22|27|5(C|c))|\\(0(10|11|12|14|15|42|47)|134)"/>
<property name="message"
value="Consider using special escape sequence instead of octal value or Unicode escaped value."/>
</module>
<module name="AvoidEscapedUnicodeCharacters">
<property name="allowEscapesForControlCharacters" value="true"/>
<property name="allowByTailComment" value="true"/>
<property name="allowNonPrintableEscapes" value="true"/>
</module>
<module name="AvoidStarImport"/>
<module name="OneTopLevelClass"/>
<module name="NoLineWrap">
<property name="tokens" value="PACKAGE_DEF, IMPORT, STATIC_IMPORT"/>
</module>
<module name="EmptyBlock">
<property name="option" value="TEXT"/>
<property name="tokens"
value="LITERAL_TRY, LITERAL_FINALLY, LITERAL_IF, LITERAL_ELSE, LITERAL_SWITCH"/>
</module>
<module name="NeedBraces">
<property name="tokens"
value="LITERAL_DO, LITERAL_ELSE, LITERAL_FOR, LITERAL_IF, LITERAL_WHILE"/>
</module>
<module name="LeftCurly">
<property name="tokens"
value="ANNOTATION_DEF, CLASS_DEF, CTOR_DEF, ENUM_CONSTANT_DEF, ENUM_DEF,
INTERFACE_DEF, LAMBDA, LITERAL_CASE, LITERAL_CATCH, LITERAL_DEFAULT,
LITERAL_DO, LITERAL_ELSE, LITERAL_FINALLY, LITERAL_FOR, LITERAL_IF,
LITERAL_SWITCH, LITERAL_SYNCHRONIZED, LITERAL_TRY, LITERAL_WHILE, METHOD_DEF,
OBJBLOCK, STATIC_INIT, RECORD_DEF, COMPACT_CTOR_DEF"/>
</module>
<module name="RightCurly">
<property name="id" value="RightCurlySame"/>
<property name="tokens"
value="LITERAL_TRY, LITERAL_CATCH, LITERAL_FINALLY, LITERAL_IF, LITERAL_ELSE,
LITERAL_DO"/>
</module>
<module name="RightCurly">
<property name="id" value="RightCurlyAlone"/>
<property name="option" value="alone"/>
<property name="tokens"
value="CLASS_DEF, METHOD_DEF, CTOR_DEF, LITERAL_FOR, LITERAL_WHILE, STATIC_INIT,
INSTANCE_INIT, ANNOTATION_DEF, ENUM_DEF, INTERFACE_DEF, RECORD_DEF,
COMPACT_CTOR_DEF, LITERAL_SWITCH"/>
</module>
<module name="SuppressionXpathSingleFilter">
<!-- suppresion is required till https://github.com/checkstyle/checkstyle/issues/7541 -->
<property name="id" value="RightCurlyAlone"/>
<property name="query" value="//RCURLY[parent::SLIST[count(./*)=1]
or preceding-sibling::*[last()][self::LCURLY]]"/>
</module>
<module name="WhitespaceAfter">
<property name="tokens"
value="COMMA, SEMI, TYPECAST, LITERAL_IF, LITERAL_ELSE, LITERAL_RETURN,
LITERAL_WHILE, LITERAL_DO, LITERAL_FOR, LITERAL_FINALLY, DO_WHILE, ELLIPSIS,
LITERAL_SWITCH, LITERAL_SYNCHRONIZED, LITERAL_TRY, LITERAL_CATCH, LAMBDA,
LITERAL_YIELD, LITERAL_CASE"/>
</module>
<module name="WhitespaceAround">
<property name="allowEmptyConstructors" value="true"/>
<property name="allowEmptyLambdas" value="true"/>
<property name="allowEmptyMethods" value="true"/>
<property name="allowEmptyTypes" value="true"/>
<property name="allowEmptyLoops" value="true"/>
<property name="ignoreEnhancedForColon" value="false"/>
<property name="tokens"
value="ASSIGN, BAND, BAND_ASSIGN, BOR, BOR_ASSIGN, BSR, BSR_ASSIGN, BXOR,
BXOR_ASSIGN, COLON, DIV, DIV_ASSIGN, DO_WHILE, EQUAL, GE, GT, LAMBDA, LAND,
LCURLY, LE, LITERAL_CATCH, LITERAL_DO, LITERAL_ELSE, LITERAL_FINALLY,
LITERAL_FOR, LITERAL_IF, LITERAL_RETURN, LITERAL_SWITCH, LITERAL_SYNCHRONIZED,
LITERAL_TRY, LITERAL_WHILE, LOR, LT, MINUS, MINUS_ASSIGN, MOD, MOD_ASSIGN,
NOT_EQUAL, PLUS, PLUS_ASSIGN, QUESTION, RCURLY, SL, SLIST, SL_ASSIGN, SR,
SR_ASSIGN, STAR, STAR_ASSIGN, LITERAL_ASSERT, TYPE_EXTENSION_AND"/>
<message key="ws.notFollowed"
value="WhitespaceAround: ''{0}'' is not followed by whitespace. Empty blocks
may only be represented as '{}' when not part of a multi-block statement (4.1.3)"/>
<message key="ws.notPreceded"
value="WhitespaceAround: ''{0}'' is not preceded with whitespace."/>
</module>
<module name="OneStatementPerLine"/>
<module name="MultipleVariableDeclarations"/>
<module name="ArrayTypeStyle"/>
<module name="MissingSwitchDefault"/>
<module name="FallThrough"/>
<module name="UpperEll"/>
<module name="ModifierOrder"/>
<module name="EmptyLineSeparator">
<property name="tokens"
value="PACKAGE_DEF, IMPORT, STATIC_IMPORT, CLASS_DEF, INTERFACE_DEF, ENUM_DEF,
STATIC_INIT, INSTANCE_INIT, METHOD_DEF, CTOR_DEF, VARIABLE_DEF, RECORD_DEF,
COMPACT_CTOR_DEF"/>
<property name="allowNoEmptyLineBetweenFields" value="true"/>
</module>
<module name="SeparatorWrap">
<property name="id" value="SeparatorWrapDot"/>
<property name="tokens" value="DOT"/>
<property name="option" value="nl"/>
</module>
<module name="SeparatorWrap">
<property name="id" value="SeparatorWrapComma"/>
<property name="tokens" value="COMMA"/>
<property name="option" value="EOL"/>
</module>
<module name="SeparatorWrap">
<!-- ELLIPSIS is EOL until https://github.com/google/styleguide/issues/259 -->
<property name="id" value="SeparatorWrapEllipsis"/>
<property name="tokens" value="ELLIPSIS"/>
<property name="option" value="EOL"/>
</module>
<module name="SeparatorWrap">
<!-- ARRAY_DECLARATOR is EOL until https://github.com/google/styleguide/issues/258 -->
<property name="id" value="SeparatorWrapArrayDeclarator"/>
<property name="tokens" value="ARRAY_DECLARATOR"/>
<property name="option" value="EOL"/>
</module>
<module name="SeparatorWrap">
<property name="id" value="SeparatorWrapMethodRef"/>
<property name="tokens" value="METHOD_REF"/>
<property name="option" value="nl"/>
</module>
<module name="PackageName">
<property name="format" value="^[a-z]+(\.[a-z][a-z0-9]*)*$"/>
<message key="name.invalidPattern"
value="Package name ''{0}'' must match pattern ''{1}''."/>
</module>
<module name="TypeName">
<property name="tokens" value="CLASS_DEF, INTERFACE_DEF, ENUM_DEF,
ANNOTATION_DEF, RECORD_DEF"/>
<message key="name.invalidPattern"
value="Type name ''{0}'' must match pattern ''{1}''."/>
</module>
<module name="MemberName">
<property name="format" value="^[a-z][a-z0-9][a-zA-Z0-9]*$"/>
<message key="name.invalidPattern"
value="Member name ''{0}'' must match pattern ''{1}''."/>
</module>
<module name="ParameterName">
<property name="format" value="^[a-z]([a-z0-9][a-zA-Z0-9]*)?$"/>
<message key="name.invalidPattern"
value="Parameter name ''{0}'' must match pattern ''{1}''."/>
</module>
<module name="LambdaParameterName">
<property name="format" value="^[a-z]([a-z0-9][a-zA-Z0-9]*)?$"/>
<message key="name.invalidPattern"
value="Lambda parameter name ''{0}'' must match pattern ''{1}''."/>
</module>
<module name="CatchParameterName">
<property name="format" value="^[a-z]([a-z0-9][a-zA-Z0-9]*)?$"/>
<message key="name.invalidPattern"
value="Catch parameter name ''{0}'' must match pattern ''{1}''."/>
</module>
<module name="LocalVariableName">
<property name="format" value="^[a-z]([a-z0-9][a-zA-Z0-9]*)?$"/>
<message key="name.invalidPattern"
value="Local variable name ''{0}'' must match pattern ''{1}''."/>
</module>
<module name="PatternVariableName">
<property name="format" value="^[a-z]([a-z0-9][a-zA-Z0-9]*)?$"/>
<message key="name.invalidPattern"
value="Pattern variable name ''{0}'' must match pattern ''{1}''."/>
</module>
<module name="ClassTypeParameterName">
<property name="format" value="(^[A-Z][0-9]?)$|([A-Z][a-zA-Z0-9]*[T]$)"/>
<message key="name.invalidPattern"
value="Class type name ''{0}'' must match pattern ''{1}''."/>
</module>
<module name="RecordComponentName">
<property name="format" value="^[a-z]([a-z0-9][a-zA-Z0-9]*)?$"/>
<message key="name.invalidPattern"
value="Record component name ''{0}'' must match pattern ''{1}''."/>
</module>
<module name="RecordTypeParameterName">
<property name="format" value="(^[A-Z][0-9]?)$|([A-Z][a-zA-Z0-9]*[T]$)"/>
<message key="name.invalidPattern"
value="Record type name ''{0}'' must match pattern ''{1}''."/>
</module>
<module name="MethodTypeParameterName">
<property name="format" value="(^[A-Z][0-9]?)$|([A-Z][a-zA-Z0-9]*[T]$)"/>
<message key="name.invalidPattern"
value="Method type name ''{0}'' must match pattern ''{1}''."/>
</module>
<module name="InterfaceTypeParameterName">
<property name="format" value="(^[A-Z][0-9]?)$|([A-Z][a-zA-Z0-9]*[T]$)"/>
<message key="name.invalidPattern"
value="Interface type name ''{0}'' must match pattern ''{1}''."/>
</module>
<module name="NoFinalizer"/>
<module name="GenericWhitespace">
<message key="ws.followed"
value="GenericWhitespace ''{0}'' is followed by whitespace."/>
<message key="ws.preceded"
value="GenericWhitespace ''{0}'' is preceded with whitespace."/>
<message key="ws.illegalFollow"
value="GenericWhitespace ''{0}'' should followed by whitespace."/>
<message key="ws.notPreceded"
value="GenericWhitespace ''{0}'' is not preceded with whitespace."/>
</module>
<module name="Indentation">
<property name="basicOffset" value="4"/>
<property name="braceAdjustment" value="4"/>
<property name="caseIndent" value="4"/>
<property name="throwsIndent" value="4"/>
<property name="lineWrappingIndentation" value="4"/>
<property name="arrayInitIndent" value="4"/>
</module>
<module name="AbbreviationAsWordInName">
<property name="ignoreFinal" value="false"/>
<property name="tokens"
value="CLASS_DEF, INTERFACE_DEF, ENUM_DEF, ANNOTATION_DEF, ANNOTATION_FIELD_DEF,
PARAMETER_DEF, VARIABLE_DEF, METHOD_DEF, PATTERN_VARIABLE_DEF, RECORD_DEF,
RECORD_COMPONENT_DEF"/>
</module>
<module name="NoWhitespaceBeforeCaseDefaultColon"/>
<module name="OverloadMethodsDeclarationOrder"/>
<module name="VariableDeclarationUsageDistance"/>
<module name="MethodParamPad">
<property name="tokens"
value="CTOR_DEF, LITERAL_NEW, METHOD_CALL, METHOD_DEF,
SUPER_CTOR_CALL, ENUM_CONSTANT_DEF, RECORD_DEF"/>
</module>
<module name="NoWhitespaceBefore">
<property name="tokens"
value="COMMA, SEMI, POST_INC, POST_DEC, DOT,
LABELED_STAT, METHOD_REF"/>
<property name="allowLineBreaks" value="true"/>
</module>
<module name="ParenPad">
<property name="tokens"
value="ANNOTATION, ANNOTATION_FIELD_DEF, CTOR_CALL, CTOR_DEF, DOT, ENUM_CONSTANT_DEF,
EXPR, LITERAL_CATCH, LITERAL_DO, LITERAL_FOR, LITERAL_IF, LITERAL_NEW,
LITERAL_SWITCH, LITERAL_SYNCHRONIZED, LITERAL_WHILE, METHOD_CALL,
METHOD_DEF, QUESTION, RESOURCE_SPECIFICATION, SUPER_CTOR_CALL, LAMBDA,
RECORD_DEF"/>
</module>
<module name="OperatorWrap">
<property name="option" value="NL"/>
<property name="tokens"
value="BAND, BOR, BSR, BXOR, DIV, EQUAL, GE, GT, LAND, LE, LITERAL_INSTANCEOF, LOR,
LT, MINUS, MOD, NOT_EQUAL, PLUS, QUESTION, SL, SR, STAR, METHOD_REF,
TYPE_EXTENSION_AND "/>
</module>
<module name="AnnotationLocation">
<property name="id" value="AnnotationLocationMostCases"/>
<property name="tokens"
value="CLASS_DEF, INTERFACE_DEF, ENUM_DEF, METHOD_DEF, CTOR_DEF,
RECORD_DEF, COMPACT_CTOR_DEF"/>
</module>
<module name="AnnotationLocation">
<property name="id" value="AnnotationLocationVariables"/>
<property name="tokens" value="VARIABLE_DEF"/>
<property name="allowSamelineMultipleAnnotations" value="true"/>
</module>
<module name="NonEmptyAtclauseDescription"/>
<module name="InvalidJavadocPosition"/>
<module name="JavadocTagContinuationIndentation"/>
<module name="SummaryJavadoc">
<property name="forbiddenSummaryFragments"
value="^@return the *|^This method returns |^A [{]@code [a-zA-Z0-9]+[}]( is a )"/>
</module>
<module name="JavadocParagraph"/>
<module name="RequireEmptyLineBeforeBlockTagGroup"/>
<module name="AtclauseOrder">
<property name="tagOrder" value="@param, @return, @throws, @deprecated"/>
<property name="target"
value="CLASS_DEF, INTERFACE_DEF, ENUM_DEF, METHOD_DEF, CTOR_DEF, VARIABLE_DEF"/>
</module>
<module name="JavadocMethod">
<property name="accessModifiers" value="public"/>
<property name="allowMissingParamTags" value="true"/>
<property name="allowMissingReturnTag" value="true"/>
<property name="allowedAnnotations" value="Override, Test"/>
<property name="tokens" value="METHOD_DEF, CTOR_DEF, ANNOTATION_FIELD_DEF, COMPACT_CTOR_DEF"/>
</module>
<module name="MissingJavadocMethod">
<property name="scope" value="public"/>
<property name="minLineCount" value="2"/>
<property name="allowedAnnotations" value="Override, Test"/>
<property name="tokens" value="METHOD_DEF, CTOR_DEF, ANNOTATION_FIELD_DEF,
COMPACT_CTOR_DEF"/>
</module>
<module name="MissingJavadocType">
<property name="scope" value="protected"/>
<property name="tokens"
value="CLASS_DEF, INTERFACE_DEF, ENUM_DEF,
RECORD_DEF, ANNOTATION_DEF"/>
<property name="excludeScope" value="nothing"/>
</module>
<module name="MethodName">
<property name="format" value="^[a-z][a-z0-9]\w*$"/>
<message key="name.invalidPattern"
value="Method name ''{0}'' must match pattern ''{1}''."/>
</module>
<module name="SingleLineJavadoc"/>
<module name="EmptyCatchBlock">
<property name="exceptionVariableName" value="expected"/>
</module>
<module name="CommentsIndentation">
<property name="tokens" value="SINGLE_LINE_COMMENT, BLOCK_COMMENT_BEGIN"/>
</module>
<!-- https://checkstyle.org/filters/suppressionxpathfilter.html -->
<module name="SuppressionXpathFilter">
<property name="file" value="${org.checkstyle.google.suppressionxpathfilter.config}"
default="checkstyle-xpath-suppressions.xml" />
<property name="optional" value="true"/>
</module>
<module name="SuppressWarningsHolder" />
<module name="SuppressionCommentFilter">
<property name="offCommentFormat" value="CHECKSTYLE.OFF\: ([\w\|]+)" />
<property name="onCommentFormat" value="CHECKSTYLE.ON\: ([\w\|]+)" />
<property name="checkFormat" value="$1" />
</module>
<module name="SuppressWithNearbyCommentFilter">
<property name="commentFormat" value="CHECKSTYLE.SUPPRESS\: ([\w\|]+)"/>
<!-- $1 refers to the first match group in the regex defined in commentFormat -->
<property name="checkFormat" value="$1"/>
<!-- The check is suppressed in the next line of code after the comment -->
<property name="influenceFormat" value="1"/>
</module>
</module>
</module>

View File

@@ -13,34 +13,50 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ghidrust.analyzer;
import generic.jar.ResourceFile;
import ghidra.app.services.AbstractAnalyzer;
import ghidra.app.services.AnalysisPriority;
import ghidra.app.services.AnalyzerType;
import ghidra.app.util.importer.MessageLog;
import ghidra.program.model.address.Address;
import ghidra.program.model.address.AddressSetView;
import ghidra.program.model.listing.Program;
import ghidra.util.exception.CancelledException;
import ghidra.util.task.TaskMonitor;
import ghidra.feature.fid.db.FidFileManager;
import ghidra.framework.Application;
import java.io.IOException;
import generic.jar.ResourceFile;
/**
* Ghidra analyzer plugin to find whether the program is a Rust binary.
*/
public class RustStdAnalyzer extends AbstractAnalyzer {
private static final byte[][] rust_artifacts = {
private static final byte[][] rustArtifacts = {
"run with `RUST_BACKTRACE=1` environment variable".getBytes(),
"called `Option::unwrap()` on a `None` value".getBytes(),
"called `Result::unwrap()` on an `Err` value".getBytes()
};
private static final String ENABLED_PROPERTY = "DecompilerParameterAnalyzer.enabled";
/**
* General plugin initialization.
*/
public RustStdAnalyzer() {
super("Detect Rust libstd functions",
"Detects Rust standard library functions from saved signatures and saves analysis time.\n\nProvided by GhidRust",
// CHECKSTYLE:OFF
"""
Detects Rust standard library functions from saved signatures and saves analysis time.
Provided by GhidRust
""",
// CHECKSTYLE:ON
AnalyzerType.FUNCTION_ANALYZER);
/*
@@ -56,8 +72,8 @@ public class RustStdAnalyzer extends AbstractAnalyzer {
@Override
public boolean getDefaultEnablement(Program program) {
// Make sure the property has not been disabled
String default_enabled = System.getProperty(ENABLED_PROPERTY);
if (default_enabled != null && !Boolean.parseBoolean(default_enabled)) {
String defaultEnabled = System.getProperty(ENABLED_PROPERTY);
if (defaultEnabled != null && !Boolean.parseBoolean(defaultEnabled)) {
return false;
}
@@ -81,15 +97,15 @@ public class RustStdAnalyzer extends AbstractAnalyzer {
return false;
}
ResourceFile data_dir;
ResourceFile dataDir;
try {
data_dir = Application.getModuleDataSubDirectory("");
dataDir = Application.getModuleDataSubDirectory("");
} catch (IOException exc) {
log.appendException(exc);
return false;
}
ResourceFile[] libs = data_dir.listFiles();
ResourceFile[] libs = dataDir.listFiles();
for (ResourceFile lib : libs) {
monitor.checkCanceled();
ffm.addUserFidFile(lib.getFile(true));
@@ -103,7 +119,13 @@ public class RustStdAnalyzer extends AbstractAnalyzer {
super.analysisEnded(program);
}
/* For exposing the Rust checking code */
/**
* For exposing the Rust checking code. This can be used as an library call
* by any other plugin relying on this plugin.
*
* @param program The program being analyzed.
* @return True if it is a Rust binaru, false otherwise.
*/
public static boolean isRustBinary(Program program) {
/*
* Taken from
@@ -111,10 +133,12 @@ public class RustStdAnalyzer extends AbstractAnalyzer {
* with-rust.yml
*/
Address start_search = program.getMinAddress();
for (byte[] search_string : rust_artifacts) {
Address found_addr = program.getMemory().findBytes(start_search, search_string, null, true, null);
if (found_addr != null) {
Address startSearch = program.getMinAddress();
for (byte[] searchString : rustArtifacts) {
Address foundAddr = program.getMemory().findBytes(
startSearch, searchString, null, true, null
);
if (foundAddr != null) {
return true;
}
}

View File

@@ -13,12 +13,19 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ghidrust.decompiler;
import ghidra.app.events.ProgramActivatedPluginEvent;
import ghidra.app.events.ProgramClosedPluginEvent;
import ghidra.app.events.ProgramLocationPluginEvent;
import ghidra.app.plugin.PluginCategoryNames;
import docking.ActionContext;
import ghidra.app.DeveloperPluginPackage;
import docking.action.DockingAction;
import ghidra.app.events.*;
import docking.action.MenuData;
import docking.ActionContext;
import ghidra.framework.plugintool.Plugin;
import ghidra.framework.plugintool.PluginInfo;
import ghidra.framework.plugintool.PluginTool;
@@ -29,39 +36,51 @@ import ghidra.program.model.listing.Program;
import ghidra.program.util.ProgramLocation;
import ghidra.util.Msg;
import ghidrust.analyzer.RustStdAnalyzer;
import docking.action.MenuData;
//@formatter:off
// @formatter:off
/**
* Add the PluginInfo annotation for the Rust decompiler plugin.
*/
@PluginInfo(
status = PluginStatus.STABLE,
packageName = "HELLO",
category = PluginCategoryNames.ANALYSIS,
shortDescription = "Rust Decompiler",
description = "Decompile Rust binaries' assembly to Rust code",
status = PluginStatus.STABLE,
packageName = DeveloperPluginPackage.NAME,
category = PluginCategoryNames.ANALYSIS,
shortDescription = "Rust Decompiler",
description = "Decompile Rust binaries' assembly to Rust code",
eventsConsumed = {
ProgramActivatedPluginEvent.class, ProgramLocationPluginEvent.class, ProgramClosedPluginEvent.class
}
ProgramActivatedPluginEvent.class,
ProgramLocationPluginEvent.class,
ProgramClosedPluginEvent.class
}
)
//@formatter:on
// @formatter:on
public class RustDecPlugin extends Plugin {
Program program;
RustDecProvider provider;
/**
* Adds docking actions for opening the decompiler and checking whether the
* binary is a Rust binary.
*
* @param tool PluginTool instance responsible for managing RustDecPlugin.
*/
public RustDecPlugin(PluginTool tool) {
super(tool);
provider = new RustDecProvider(this, getName(), null);
DockingAction dec_plugin = new DockingAction("GhidRust", getName()) {
DockingAction decPlugin = new DockingAction("GhidRust", getName()) {
@Override
public void actionPerformed(ActionContext context) {
provider.activateProvider();;
provider.activateProvider();
;
}
};
dec_plugin.setEnabled(true);
dec_plugin.setMenuBarData(new MenuData(new String[] { "GhidRust", "Open decompiler" }));
decPlugin.setEnabled(true);
decPlugin.setMenuBarData(new MenuData(new String[] { "GhidRust", "Open decompiler" }));
DockingAction check_plugin = new DockingAction("GhidRust", getName()) {
DockingAction checkPlugin = new DockingAction("GhidRust", getName()) {
@Override
public void actionPerformed(ActionContext context) {
if (RustStdAnalyzer.isRustBinary(program)) {
@@ -72,11 +91,13 @@ public class RustDecPlugin extends Plugin {
}
};
check_plugin.setEnabled(true);
check_plugin.setMenuBarData(new MenuData(new String[] { "GhidRust", "Check if Rust binary" }));
checkPlugin.setEnabled(true);
checkPlugin.setMenuBarData(new MenuData(
new String[] { "GhidRust", "Check if Rust binary" }
));
tool.addAction(dec_plugin);
tool.addAction(check_plugin);
tool.addAction(decPlugin);
tool.addAction(checkPlugin);
}
@Override

View File

@@ -6,6 +6,8 @@ import ghidra.program.model.listing.Function;
import ghidra.app.decompiler.DecompInterface;
import ghidra.app.decompiler.DecompileResults;
import ghidrust.decompiler.parser.c.CFormatter;
import ghidrust.decompiler.parser.c.gen.CParser;
import javax.swing.JComponent;
import javax.swing.JLabel;
@@ -24,26 +26,35 @@ import java.awt.event.ActionEvent;
import docking.ComponentProvider;
import ghidra.util.task.ConsoleTaskMonitor;
import resources.ResourceManager;
import ghidrust.decompiler.parser.c.CFormatter;
import ghidrust.decompiler.parser.c.gen.CParser;
/**
* Responsible for decompiling and showing the decompiled code in a window.
*/
public class RustDecProvider extends ComponentProvider {
private JPanel panel;
private JTextArea code_area;
private JLabel func_title;
private JTextArea codeArea;
private JLabel funcTitle;
private Program prog;
private Address addr;
private DecompInterface decomp_ifc = null;
private DecompInterface decompInterface = null;
private static final String EMPTY_LABEL = "<none>";
/**
* Initialize the provider by creating a decompilation interface and the
* decompilation window.
*
* @param plugin Calling plugin, which in our case would be RustDecPlugin.
* @param owner Owner of the plugin.
* @param p Program on which this plugin is being used.
*/
public RustDecProvider(RustDecPlugin plugin, String owner, Program p) {
super(plugin.getTool(), owner, owner);
setIcon(ResourceManager.loadImage("images/icon.png"));
decomp_ifc = new DecompInterface();
decompInterface = new DecompInterface();
setProgram(p);
buildPanel();
@@ -57,7 +68,7 @@ public class RustDecProvider extends ComponentProvider {
private void buildPanel() {
panel = new JPanel(new BorderLayout());
func_title = new JLabel(EMPTY_LABEL);
funcTitle = new JLabel(EMPTY_LABEL);
JButton reload = new JButton(ResourceManager.loadImage("images/reload.png"));
reload.addActionListener(new ActionListener() {
@@ -68,14 +79,14 @@ public class RustDecProvider extends ComponentProvider {
JToolBar toolbar = new JToolBar("GhidRust", JToolBar.HORIZONTAL);
toolbar.setFloatable(false);
toolbar.add(func_title);
toolbar.add(funcTitle);
toolbar.add(Box.createHorizontalGlue());
toolbar.add(reload);
code_area = new JTextArea();
code_area.setEditable(false);
codeArea = new JTextArea();
codeArea.setEditable(false);
JScrollPane scroll = new JScrollPane(code_area);
JScrollPane scroll = new JScrollPane(codeArea);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
panel.add(toolbar, BorderLayout.PAGE_START);
@@ -86,12 +97,18 @@ public class RustDecProvider extends ComponentProvider {
setVisible(true);
}
/**
* We save the program in a private class variable to be used later, and
* open it using the decompiler interface.
*
* @param p program to be decompiled.
*/
public void setProgram(Program p) {
prog = p;
decomp_ifc.closeProgram();
decompInterface.closeProgram();
if (prog != null) {
decomp_ifc.openProgram(prog);
decompInterface.openProgram(prog);
}
}
@@ -99,48 +116,54 @@ public class RustDecProvider extends ComponentProvider {
addr = a;
}
/**
* When reload is called, we trigger the decompilation.
*/
public void reload() {
if (prog == null) {
func_title.setText(EMPTY_LABEL);
code_area.setText("[?] Open a program to see the decompilation!\n");
funcTitle.setText(EMPTY_LABEL);
codeArea.setText("[?] Open a program to see the decompilation!\n");
return;
}
if (addr == null) {
func_title.setText(EMPTY_LABEL);
code_area.setText("[?] Select a memory location to decompile!\n");
funcTitle.setText(EMPTY_LABEL);
codeArea.setText("[?] Select a memory location to decompile!\n");
return;
}
Function func = prog.getFunctionManager().getFunctionContaining(addr);
if (func == null) {
func_title.setText(EMPTY_LABEL);
code_area.setText("[!] No function found at " + addr.toString() + "\n");
funcTitle.setText(EMPTY_LABEL);
codeArea.setText("[!] No function found at " + addr.toString() + "\n");
return;
}
func_title.setText(func.getName());
funcTitle.setText(func.getName());
if (decomp_ifc == null) {
code_area.setText("[!] Decompiler has not been initialized!\n");
if (decompInterface == null) {
codeArea.setText("[!] Decompiler has not been initialized!\n");
return;
}
DecompileResults results = decomp_ifc.decompileFunction(func, 0, new ConsoleTaskMonitor());
if (results == null || results.getDecompiledFunction() == null || results.getDecompiledFunction().getC() == null) {
code_area.setText("[!] Failed to decompile " + func.getName() + "\n");
DecompileResults results = decompInterface.decompileFunction(
func, 0, new ConsoleTaskMonitor()
);
if (results == null || results.getDecompiledFunction() == null
|| results.getDecompiledFunction().getC() == null) {
codeArea.setText("[!] Failed to decompile " + func.getName() + "\n");
return;
}
String decompiled = results.getDecompiledFunction().getC();
String rust_code = "";
String rustCode = "";
try {
rust_code = CFormatter.format(CParser.transpile(decompiled));
rustCode = CFormatter.format(CParser.transpile(decompiled));
} catch (Exception e) {
rust_code = "/* [!] Failed to transpile " + func.getName() + " */\n" + decompiled;
rustCode = "/* [!] Failed to transpile " + func.getName() + " */\n" + decompiled;
}
code_area.setText(rust_code);
codeArea.setText(rustCode);
}
}

View File

@@ -3,6 +3,9 @@ package ghidrust.decompiler.parser;
import ghidrust.decompiler.parser.c.gen.CParser;
import ghidrust.decompiler.parser.c.CFormatter;
/**
* Run the transpiler as a command line standalone tool (for testing).
*/
public class Run {
public static void main(String[] args) {
System.out.println(CFormatter.format(CParser.transpile(System.in)));

View File

@@ -1,7 +1,10 @@
package ghidrust.decompiler.parser.c;
/**
* Format decompiled code.
*/
public class CFormatter {
static int indent_level;
static int indentLevel;
static String indent(int level) {
StringBuffer sb = new StringBuffer("");
@@ -13,38 +16,44 @@ public class CFormatter {
return sb.toString();
}
public CFormatter(int initial_indent) {
indent_level = 0;
public CFormatter(int initialIndent) {
indentLevel = initialIndent;
}
/**
* Format the code being passed in by adding newlines and semicolons.
*
* @param code Code as a string.
* @return Formatted code.
*/
public static String format(String code) {
StringBuffer pretty = new StringBuffer("");
int str_len = code.length();
pretty.append(indent(indent_level));
pretty.append(indent(indentLevel));
boolean disable = false;
for (int i = 0; i < str_len; i++) {
if (code.charAt(i) == '{') {
pretty.append("{\n");
indent_level++;
pretty.append(indent(indent_level));
indentLevel++;
pretty.append(indent(indentLevel));
} else if (code.charAt(i) == '}') {
indent_level--;
indentLevel--;
if (code.charAt(i - 1) != ';') {
pretty.append("\n");
pretty.append(indent(indent_level));
pretty.append(indent(indentLevel));
} else {
pretty.deleteCharAt(pretty.length() - 1);
}
pretty.append("}");
if (!(i + 1 < str_len && code.charAt(i + 1) == ' ')) {
pretty.append("\n");
pretty.append(indent(indent_level));
pretty.append(indent(indentLevel));
}
} else if (code.charAt(i) == ';') {
pretty.append(";\n");
pretty.append(indent(indent_level));
pretty.append(indent(indentLevel));
} else if (code.charAt(i) == '@') {
/* special character to denote no action for next char */
i++;

View File

@@ -1,5 +1,6 @@
package ghidrust.decompiler.parser.c;
// CHECKSTYLE:OFF
import ghidrust.decompiler.parser.c.gen.*;
import java.util.HashMap;
@@ -453,6 +454,8 @@ public class CVisitor implements CParserVisitor {
return defaultSpacedVisit(node, data, ", ", false);
}
}
// CHECKSTYLE:ON
/*
* JavaCC - OriginalChecksum=fd39d82df2a1b516298b94d6f4a5e997 (do not edit this
* line)

View File

@@ -1995,4 +1995,4 @@ void Constant() :
}
}
/*@egen*/
}
}