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

Bodden, Eric eric.bodden at sit.fraunhofer.de
Mon Mar 3 07:28:10 EST 2014


Hmm, seems also strange to me. Can you look at the generated class using "javap -c HelloWorld" ? Maybe that gives you an idea of what might be wrong.

Eric

On 03.03.2014, at 09:49, qpj <412832527 at qq.com> wrote:

> 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
> _______________________________________________
> Soot-list mailing list
> Soot-list at sable.mcgill.ca
> http://mailman.cs.mcgill.ca/mailman/listinfo/soot-list

--
Prof. Eric Bodden, Ph.D., http://sse.ec-spride.de/ http://bodden.de/
Head of Secure Software Engineering  at Fraunhofer SIT, TU Darmstadt and EC SPRIDE
Tel: +49 6151 16-75422    Fax: +49 6151 16-72051
Room 3.2.14, Mornewegstr. 30, 64293 Darmstadt

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 841 bytes
Desc: Message signed with OpenPGP using GPGMail
Url : http://mailman.cs.mcgill.ca/pipermail/soot-list/attachments/20140303/22d04e9d/attachment.bin 


More information about the Soot-list mailing list