public void SuperClassTest() { var classFile = JavaClassFile.FromReader(new MemoryBigEndianReader(Properties.Resources.SimpleModel)); var image = new JavaClassImage(classFile); Assert.Equal("java/lang/Object", image.RootClass.SuperClass.Name); }
public void AccessFlagsTest() { var classFile = JavaClassFile.FromReader(new MemoryBigEndianReader(Properties.Resources.SimpleModel)); var image = new JavaClassImage(classFile); Assert.Equal(ClassAccessFlags.Public | ClassAccessFlags.Super, image.RootClass.AccessFlags); }
/// <summary> /// Builds the class image and verifies the output of the file. /// </summary> /// <param name="image">The class to compile.</param> /// <param name="expectedOutput">The expected output of the file.</param> /// <param name="regex">Specifies if <paramref name="expectedOutput"/> should be interpreted as a regular expression pattern.</param> public void BuildAndVerifyOutput(JavaClassImage image, string expectedOutput, bool regex = false) { var builder = new JavaClassFileBuilder(); var file = builder.CreateClassFile(image); WriteAndVerifyOutput(file, expectedOutput, regex); }
public void FieldsTest() { var classFile = JavaClassFile.FromReader(new MemoryBigEndianReader(Properties.Resources.SimpleModel)); var image = new JavaClassImage(classFile); Assert.Equal(3, image.RootClass.Fields.Count); }
internal ExceptionHandler(JavaClassImage classImage, ByteCodeMethodBody byteCodeMethodBody, ExceptionHandlerInfo info) { Start = byteCodeMethodBody.Instructions.GetByOffset(info.StartOffset); End = byteCodeMethodBody.Instructions.GetByOffset(info.EndOffset); HandlerStart = byteCodeMethodBody.Instructions.GetByOffset(info.HandlerOffset); CatchType = classImage.ResolveClass(info.CatchType); }
public static JavaClassImage RebuildClassImage(JavaClassImage image) { var builder = new JavaClassFileBuilder(); var file = builder.CreateClassFile(image); return(new JavaClassImage(file)); }
public MethodDefinitionTest() { var classFile = JavaClassFile.FromReader(new MemoryBigEndianReader(Properties.Resources.SimpleModel)); var image = new JavaClassImage(classFile); var classDef = image.RootClass; _constructor = classDef.Methods.FirstOrDefault(x => x.Name == "<init>"); }
public void ExceptionsTest() { var classFile = JavaClassFile.FromReader(new MemoryBigEndianReader(Properties.Resources.Exceptions)); var image = new JavaClassImage(classFile); var classDef = image.RootClass; var throwsMethod = classDef.Methods.First(x => x.Name == "Throws"); Assert.Single(throwsMethod.Exceptions); Assert.Equal("CustomException", throwsMethod.Exceptions[0].Name); }
private static FieldDefinition CreateDummyField() { var field = new FieldDefinition("SomeField", new FieldDescriptor(BaseType.Int)); var classDef = new ClassDefinition("SomeClass", new ClassReference("java/lang/Object")); classDef.Fields.Add(field); var image = new JavaClassImage(classDef); return(field); }
public FieldDefinitionTest() { var classFile = JavaClassFile.FromReader(new MemoryBigEndianReader(Properties.Resources.SimpleModel)); var image = new JavaClassImage(classFile); var classDef = image.RootClass; _myIntField = classDef.Fields.FirstOrDefault(x => x.Name == "myIntField"); _myStringField = classDef.Fields.FirstOrDefault(x => x.Name == "myStringField"); _myModelField = classDef.Fields.FirstOrDefault(x => x.Name == "myModelField"); }
public Context(string FilePath) { this.FilePath = FilePath; this.Engine = new Engine(this); this.StringGenerator = new StringGenerator(); this.Image = JavaClassImage.FromFile(this.FilePath); this.Logger = new ConsoleLogger(); this.Class = this.Image.RootClass; this.OutputPath = GetOutputPath; }
private static MethodDefinition CreateDummyMethod() { var method = new MethodDefinition("SomeMethod", new MethodDescriptor(BaseType.Int)); var classDef = new ClassDefinition("SomeClass", new ClassReference("java/lang/Object")); classDef.Methods.Add(method); var image = new JavaClassImage(classDef); return(method); }
private static JavaClassImage DumpImage(JavaClassFile classFile) { var image = new JavaClassImage(classFile); var rootClass = image.RootClass; Console.WriteLine("Basic information:"); DumpStructure(rootClass); Console.WriteLine("Disassembly of each method:"); DumpByteCode(rootClass); return(image); }
internal LocalVariable(JavaClassImage classImage, ByteCodeMethodBody body, LocalVariableInfo variableInfo) { _name = new LazyValue <string>(() => classImage.ClassFile.ConstantPool.ResolveString(variableInfo.NameIndex)); _descriptor = new LazyValue <FieldDescriptor>(() => classImage.ResolveFieldDescriptor(variableInfo.DescriptorIndex)); Index = variableInfo.LocalIndex; Start = body.Instructions.GetByOffset(variableInfo.StartOffset); End = body.Instructions.GetByOffset(variableInfo.StartOffset + variableInfo.Length); }
public JavaClassFile CreateClassFile(JavaClassImage image) { var context = new BuildingContext(this); // Basic structure. var file = new JavaClassFile { MajorVersion = image.MajorVersion, MinorVersion = image.MinorVersion, }; var thisReference = new ClassReference(image.RootClass.Name); file.ThisClass = (ushort)ConstantPoolBuffer.GetClassIndex(thisReference); if (image.RootClass.SuperClass != null) { file.SuperClass = (ushort)ConstantPoolBuffer.GetClassIndex(image.RootClass.SuperClass); } file.AccessFlags = image.RootClass.AccessFlags; // Fields foreach (var field in image.RootClass.Fields) { file.Fields.Add(CreateFieldInfo(context, field)); } // Methods foreach (var method in image.RootClass.Methods) { file.Methods.Add(CreateMethodInfo(context, method)); } // Source file if (image.SourceFile != null) { file.Attributes.Add(CreateAttribute(context, new SingleIndexAttribute( SingleIndexAttribute.SourceFileAttribute, (ushort)ConstantPoolBuffer.GetUtf8Index(image.SourceFile)))); } if (BootstrapMethodsAttribute.BootstrapMethods.Count > 0) { file.Attributes.Add(CreateAttribute(context, BootstrapMethodsAttribute)); } AddAttributes(context, file, image); file.ConstantPool = ConstantPoolBuffer.CreateConstantPool(); return(file); }
public static void Main(string[] args) { string path = args[0].Replace("\"", ""); var classFile = JavaClassFile.FromFile(path); var image = new JavaClassImage(classFile); var rootClass = image.RootClass; Console.WriteLine("Basic information:"); DumpStructure(rootClass); Console.WriteLine("Disassembly of each method:"); DumpByteCode(rootClass); }
public void BuildSimpleHelloWorld() { // Create main method. var mainMethod = new MethodDefinition("main", new MethodDescriptor(BaseType.Void, ObjectType.String.CreateArrayType())) { AccessFlags = MethodAccessFlags.Public | MethodAccessFlags.Static, }; // Build references. var printStream = new ClassReference("java/io/PrintStream"); var systemOut = new FieldReference("out", new ClassReference("java/lang/System"), new FieldDescriptor(new ObjectType("java/io/PrintStream"))); var println = new MethodReference("println", printStream, new MethodDescriptor(BaseType.Void, ObjectType.String)); // Build body. mainMethod.Body = new ByteCodeMethodBody { Variables = { new LocalVariable("args", new FieldDescriptor(mainMethod.Descriptor.ParameterTypes[0])) }, Instructions = { new ByteCodeInstruction(ByteOpCodes.GetStatic, systemOut), new ByteCodeInstruction(ByteOpCodes.Ldc, "Hello, world!"), new ByteCodeInstruction(ByteOpCodes.InvokeVirtual, println), new ByteCodeInstruction(ByteOpCodes.Return) } }; mainMethod.Body.Variables[0].Start = mainMethod.Body.Instructions[0]; // Create container class. var classDef = new ClassDefinition("HelloWorld") { Methods = { mainMethod }, SuperClass = new ClassReference("java/lang/Object"), }; var classImage = new JavaClassImage(classDef); // Verify. _fixture.BuildAndVerifyOutput(classImage, "Hello, world!" + Environment.NewLine); }
internal ByteCodeMethodBody(JavaClassImage classImage, CodeAttribute attribute) { MaxStack = attribute.MaxStack; // Read instructions. var disassembler = new ByteCodeDisassembler(new MemoryBigEndianReader(attribute.Code)) { OperandResolver = new DefaultOperandResolver(classImage) }; foreach (var instruction in disassembler.ReadInstructions()) { Instructions.Add(instruction); } // Read exception handlers. foreach (var handler in attribute.ExceptionHandlers) { ExceptionHandlers.Add(new ExceptionHandler(classImage, this, handler)); } // Read attributes. foreach (var attr in attribute.Attributes) { string name = classImage.ClassFile.ConstantPool.ResolveString(attr.NameIndex); switch (name) { // Local variables case LocalVariableTableAttribute.AttributeName: var localsTable = LocalVariableTableAttribute.FromReader(new MemoryBigEndianReader(attr.Contents)); foreach (var info in localsTable.LocalVariables) { Variables.Add(new LocalVariable(classImage, this, info)); } break; default: ExtraAttributes.Add(name, attr.Clone()); break; } } }
public static void Main(string[] args) { if (args.Length != 1) { Console.WriteLine("Usage: JavaResolver.SimpleRenamer inputfile.class"); return; } // Open file. string filePath = args[0].Replace("\"", ""); var classImage = JavaClassImage.FromFile(filePath); // Rename symbols. RenameInClass(classImage.RootClass); // Reassemble. var newClassFile = classImage.CreateClassFile(); // Save. newClassFile.Write(Path.ChangeExtension(filePath, "obfuscated.class")); }
public DefaultOperandResolver(JavaClassImage classImage) { _classImage = classImage ?? throw new ArgumentNullException(nameof(classImage)); }