Skip to content

Commit

Permalink
Merge pull request #3 from LycsHub/main
Browse files Browse the repository at this point in the history
fix issue#2,compatible with unix
  • Loading branch information
SummerSec authored Mar 3, 2022
2 parents 097316b + 736f3fc commit 82051ac
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 8 deletions.
22 changes: 18 additions & 4 deletions src/main/java/com/sumsec/core/ast/util/DotHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,37 @@ public String[] RunCmd(String[] dotPath){
String[] cmd;
mkdir(ConstatField.ResultTemp);
ArrayList<String> imgPaths = new ArrayList<>();
// String path = ConstatField.ResultTemp + ConstatField.separator + name + ".png";

for (int i = 0; i < dotPath.length; i++) {
String imgPath = ConstatField.ASTResultTemp + ConstatField.separator + dotPath[i].substring(dotPath[i].lastIndexOf("\\") + 1, dotPath[i].lastIndexOf(".")) + ".png";

if (isWindowsOS) {
//windows下的命令
cmd = new String[]{"cmd.exe", "/c", "dot", "-Tpng", dotPath[i], "-o", imgPath};
} else {
//linux下的命令
cmd = new String[]{"/bin/sh", "-c", "dot", "-Tpng" , dotPath[i] , "-o", imgPath};

// fix #issue2
// on unix use / instead \
// has tow slash in imgPath
imgPath = ConstatField.ASTResultTemp + ConstatField.separator + dotPath[i].substring(dotPath[i].lastIndexOf("/") + 1, dotPath[i].lastIndexOf(".")) + ".png";
// fixme: use `/usr/bash -c` may has problem in some bash such as zsh
cmd = new String[]{"dot", "-Tpng" , dotPath[i] , "-o", imgPath};
}
try {

logger.info("开始生成图片 " + dotPath[i]);
logger.info("正在执行命令 "+ Arrays.toString(cmd));
sleep(1000);
String charsetName = isWindowsOS ? "GBK" : "UTF-8";
byte[] bytes = new Scanner(Runtime.getRuntime().exec(cmd).getInputStream(), charsetName).useDelimiter("\\A").next().getBytes(charsetName);
Runtime.getRuntime().exec(cmd);

byte[] bytes = null;
Scanner runCmd = new Scanner(Runtime.getRuntime().exec(cmd).getInputStream(), charsetName).useDelimiter("\\A");
//fix issue#2 : on linux ,detect hasNext otherwise will throw an NoSuchElementException
if (runCmd.hasNext())
bytes = runCmd.next().getBytes(charsetName);


File file = new File(imgPath);
if (!file.exists()) {
logger.info("bytes.length = " + bytes.length);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/sumsec/core/cfg/Generate.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;

import static cn.hutool.core.io.FileUtil.mkdir;
import static soot.SootClass.SIGNATURES;
Expand Down Expand Up @@ -56,6 +57,7 @@ public boolean DotG(String graphtype,String filename){
Options.v().set_prepend_classpath(true);
Options.v().set_allow_phantom_refs(true);
Options.v().set_soot_classpath(ConstatField.CFGHOMETemp);

}

if (filename.endsWith(".class")){
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/sumsec/core/cfg/core/CFGViewerOver.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public static Object parse_options(CFGViewer viewer,String[] args) {
public static void main(CFGViewer viewer,String[] args) {
Object obj = parse_options(viewer, args);
Scene.v().addBasicClass(args[0], SIGNATURES);


System.out.println(args[0]);
ConstatField.main.run((String[]) obj);
}
Expand All @@ -43,6 +45,7 @@ public static CFGViewer CFGViewerOver(){
Transform printTransform = new Transform("jtp.printcfg", viewer);
printTransform.setDeclaredOptions("enabled alt-class-path graph-type ir multipages brief ");
printTransform.setDefaultOptions("enabled alt-class-path: graph-type:BriefUnitGraph ir:jimple multipages:false brief:false ");

PackManager.v().getPack("jtp").add(printTransform);


Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/sumsec/core/cfg/uitls/GenClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static void genClassM(String mName,String mContext){
log.info("开始生成class: " + className);
byte[] bytes = clzz.toBytecode();
// ByteArrayOutputStream baos = new ByteArrayOutputStream();
String file = ConstatField.CFGHOMETemp ;
String file = ConstatField.CFGHOMETemp;
mkdir(file);
FileOutputStream fos = new FileOutputStream(file + ConstatField.separator + className + ".class");
fos.write(bytes);
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/com/sumsec/core/cfg/uitls/OSUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public String[] RunCmd(String[] dotPath){
String[] cmd;
mkdir(ConstatField.ResultTemp);
ArrayList<String> imgPaths = new ArrayList<>();
// String path = ConstatField.ResultTemp + ConstatField.separator + name + ".png";

logger.info(dotPath.length);
for (int i = 0; i < dotPath.length; i++) {
String imgPath = ConstatField.ResultTemp + ConstatField.separator + dotPath[i].substring(dotPath[i].lastIndexOf("\\") + 1, dotPath[i].lastIndexOf(".")) + ".png";
Expand All @@ -48,15 +48,24 @@ public String[] RunCmd(String[] dotPath){
cmd = new String[]{"cmd.exe", "/c", "dot", "-Tpng", dotPath[i], "-o", imgPath};
} else {
//linux下的命令
cmd = new String[]{"/bin/sh", "-c", "dot", "-Tpng" , dotPath[i] ,"-o", imgPath};
// fix #issue2
imgPath = ConstatField.ResultTemp + ConstatField.separator + dotPath[i].substring(dotPath[i].lastIndexOf("/") + 1, dotPath[i].lastIndexOf(".")) + ".png";
cmd = new String[]{"dot", "-Tpng" , dotPath[i] ,"-o", imgPath};
}
try {

logger.info("开始生成图片 " + dotPath[i]);
logger.info("正在执行命令 " + Arrays.toString(cmd));
sleep(1000);
String charsetName = isWindowsOS ? "GBK" : "UTF-8";
byte[] bytes = new Scanner(Runtime.getRuntime().exec(cmd).getInputStream(), charsetName).useDelimiter("\\A").next().getBytes(charsetName);

byte[] bytes = null;
Scanner runCmd = new Scanner(Runtime.getRuntime().exec(cmd).getInputStream(), charsetName).useDelimiter("\\A");
// fix #issue2: on linux : detect hasNext otherwise will throw an NoSuchElementException
if (runCmd.hasNext())
bytes = runCmd.next().getBytes(charsetName);


File file = new File(imgPath);
if (!file.exists()) {
logger.info("bytes.length = " + bytes.length);
Expand Down

0 comments on commit 82051ac

Please sign in to comment.