[Soot-list] Problem of running HelloWorld.class created by createClass tutorial.

qpj 412832527 at qq.com
Mon Mar 3 03:49:38 EST 2014


Hi Eric,
I'd like to create a class using Soot, so I copy the code in createClass tutorial. And I can get a HelloWorld.class. The problem came when I run it in the command. 


D:\WorkSpace\Administrator\workspace\SootStudy\sootOutput>java HelloWorld
Exception in thread "main" java.lang.VerifyError: (class: HelloWorld, method: ma
in signature: ([Ljava/lang/String;)V) Incompatible argument to function
Could not find the main class: HelloWorld.  Program will exit.





code:
package study;
import soot.ArrayType;
import soot.Local;
import soot.Modifier;
import soot.RefType;
import soot.Scene;
import soot.SootClass;
import soot.SootMethod;
import soot.SourceLocator;
import soot.Type;
import soot.Unit;
import soot.VoidType;
import soot.jimple.*;
import soot.options.Options;
import soot.util.*;
import java.io.*;


import java.util.*;
/** Example of using Soot to create a classfile from scratch.
* The ’createclass’ example creates a HelloWorld class file using Soot.
* It proceeds as follows:
*
* - Create a SootClass <code>HelloWorld</code> extending java.lang.Object.
*
* - Create a ’main’ method and add it to the class.
*
* - Create an empty JimpleBody and add it to the ’main’ method.
*
* - Add locals and statements to JimpleBody.
*
* - Write the result out to a class file.
*/
public class CreateClasses
{
	public static void main(String[] args) throws FileNotFoundException, IOException
	
	{
		List<String> argsList = new ArrayList<String>(Arrays.asList(args));
		argsList.addAll(Arrays.asList(new String[] {
				"-w",
				"-pp"
				}));// main-class
		args = argsList.toArray(new String[0]);
		SootClass sClass;
		SootMethod method;
		
		Scene.v().loadClassAndSupport("java.lang.Object");
		Scene.v().loadClassAndSupport("java.lang.System");
		Scene.v().loadClassAndSupport("java.io.PrintStream");
		
		Scene.v().loadNecessaryClasses();//必须放在所有加载类之后
		// Declare ’public class HelloWorld’
		sClass = new SootClass("HelloWorld", Modifier.PUBLIC);
		// ’extends Object’
		sClass.setSuperclass(Scene.v().getSootClass("java.lang.Object"));
		Scene.v().addClass(sClass);
		// Create the method, public static void main(String[])
		ArrayType arrayType = ArrayType.v(RefType.v("java.lang.String"), 1);
		
		method = new SootMethod("main",
		Arrays.asList(new Type[] {ArrayType.v(RefType.v("java.lang.String"), 1)}),
		VoidType.v(), Modifier.PUBLIC | Modifier.STATIC);
		sClass.addMethod(method);
		System.out.println(Arrays.asList(new Type[] {ArrayType.v(RefType.v("java.lang.String"), 1)}));
		//System.out.println(method.getSignature());
		// Create the method body
		{
		// create empty body
			JimpleBody body = Jimple.v().newBody(method);
			method.setActiveBody(body);
			Chain<Unit> units = body.getUnits();
			Local arg, tmpRef;
			// Add some locals, java.lang.String l0
			arg = Jimple.v().newLocal("l0", ArrayType.v(RefType.v("java.lang.String"), 1));
			body.getLocals().add(arg);
			// Add locals, java.io.printStream tmpRef
			tmpRef = Jimple.v().newLocal("tmpRef", RefType.v("java.io.PrintStream"));
			body.getLocals().add(tmpRef);
			// add "l0 = @parameter0"
			units.add(Jimple.v().newIdentityStmt(arg,
			Jimple.v().newParameterRef(ArrayType.v(RefType.v("java.lang.String"), 1), 0)));
			// add "tmpRef = java.lang.System.out"
			units.add(Jimple.v().newAssignStmt(tmpRef, Jimple.v().newStaticFieldRef(
			Scene.v().getField("<java.lang.System: java.io.PrintStream out>").makeRef())));
		// insert "tmpRef.println("Hello world!")"
			{
				SootMethod toCall = Scene.v().getMethod("<java.io.PrintStream: void println(java.lang.String)>");
				units.add(Jimple.v().newInvokeStmt(Jimple.v().newVirtualInvokeExpr(tmpRef, toCall.makeRef(),arg)));
			
			}
		// insert "return"
			units.add(Jimple.v().newReturnVoidStmt());
		}
		String fileName = SourceLocator.v().getFileNameFor(sClass, Options.output_format_class);
		OutputStream streamOut = new JasminOutputStream(new FileOutputStream(fileName));
		PrintWriter writerOut = new PrintWriter(new OutputStreamWriter(streamOut));
		JasminClass jasminClass = new soot.jimple.JasminClass(sClass);
		jasminClass.print(writerOut);
		writerOut.flush();
		streamOut.close();
	}
}

It seems java cannot transfer ([Ljava/lang/String;)V) to Stinrg[]. Do I miss anything? Any advice will be appreciated.


Best regards.
Xiangxing
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.cs.mcgill.ca/pipermail/soot-list/attachments/20140303/c8d82c75/attachment.html 


More information about the Soot-list mailing list