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

Migrates to use JepConfig stdout, stderr from setStream method #36

Merged
merged 7 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 1 addition & 3 deletions src/main/java/ghidrathon/GhidrathonConsoleInputThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,8 @@ public void run() {

try {

python = GhidrathonInterpreter.get();
python = GhidrathonInterpreter.get(out, err);

// set Python stdout and stderr to reference our console window
python.setStreams(out, err);
python.printWelcome();

} catch (RuntimeException e) {
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/ghidrathon/GhidrathonScript.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ protected void run() {

try {

python = GhidrathonInterpreter.get();

// redirect Python stdout and stderr to console window
python.setStreams(out, err);
python = GhidrathonInterpreter.get(out, err);

// run Python script from Python interpreter
python.runScript(getSourceFile(), this);
Expand Down
44 changes: 9 additions & 35 deletions src/main/java/ghidrathon/interpreter/GhidrathonInterpreter.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import java.io.IOException;
import java.io.FileNotFoundException;

import org.apache.commons.io.output.WriterOutputStream;

import generic.jar.ResourceFile;

import ghidra.framework.Application;
Expand Down Expand Up @@ -48,7 +50,7 @@ public class GhidrathonInterpreter {
* @throws JepException
* @throws IOException
*/
private GhidrathonInterpreter() throws JepException, IOException{
private GhidrathonInterpreter(PrintWriter out, PrintWriter err) throws JepException, IOException{

// configure the Python includes path with the user's Ghdira script directory
String paths = "";
Expand All @@ -66,6 +68,10 @@ private GhidrathonInterpreter() throws JepException, IOException{

// configure Python includes Path
config.addIncludePaths(paths);

// configure Jep stdout and stderr
config.redirectStdout(new WriterOutputStream(out, "UTF-8"));
config.redirectStderr(new WriterOutputStream(err, "UTF-8"));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we foresee any potential encoding issues by manually specifying UTF-8? e.g. Windows v Linux v Mac?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

As per the apache docs Charset is a required param and specifiying it explicitly does look out for possible encoding issues

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood! Is there any way for us to determine the encoding at runtime? e.g. is there a method we can call on out or err to get the encoding used and pass it to WriterOutputStream?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added the method from java.nio.Charsets that provides us with the encoding used by the jvm (which should be the encoding used )
image

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I think this looks good. Can you please include a screenshot of the unit tests passing in your environment w/ these changes? You can run this script from within Ghidra to kick off the tests.


// we must set the native Jep library before creating a Jep instance
setJepNativeBinaryPath();
Expand Down Expand Up @@ -224,11 +230,11 @@ private void injectScriptHierarchy(GhidraScript script) throws JepException, Fil
* @return GhidrathonInterpreter
* @throws RuntimeException
*/
public static GhidrathonInterpreter get() throws RuntimeException {
public static GhidrathonInterpreter get(PrintWriter out, PrintWriter err) throws RuntimeException {

try {

return new GhidrathonInterpreter();
return new GhidrathonInterpreter(out, err);

} catch (Exception e) {

Expand Down Expand Up @@ -398,38 +404,6 @@ public void runScript(ResourceFile file, GhidraScript script) {

}

/**
* Set output and error streams for Jep instance.
*
* Output and error streams from Python interpreter are redirected to the specified streams. If these are not set, this data is lost.
*
* @param out output stream
* @param err error stream
*/
public void setStreams(PrintWriter out, PrintWriter err) {

try {

ResourceFile file = Application.getModuleDataFile(extname, "python/jepstream.py");
ooprathamm marked this conversation as resolved.
Show resolved Hide resolved

jep.set("GhidraPluginToolConsoleOutWriter", out);
jep.set("GhidraPluginToolConsoleErrWriter", err);

jep.runScript(file.getAbsolutePath());

this.out = out;
this.err = err;

} catch (JepException | FileNotFoundException e) {

// ensure stack trace prints to err stream for user
e.printStackTrace();
throw new RuntimeException(e);

}

}

public void printWelcome() {

try {
Expand Down