Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
        /// <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);
        }
Exemplo n.º 4
0
        public void FieldsTest()
        {
            var classFile = JavaClassFile.FromReader(new MemoryBigEndianReader(Properties.Resources.SimpleModel));
            var image     = new JavaClassImage(classFile);

            Assert.Equal(3, image.RootClass.Fields.Count);
        }
Exemplo n.º 5
0
 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);
 }
Exemplo n.º 6
0
        public static JavaClassImage RebuildClassImage(JavaClassImage image)
        {
            var builder = new JavaClassFileBuilder();
            var file    = builder.CreateClassFile(image);

            return(new JavaClassImage(file));
        }
Exemplo n.º 7
0
        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>");
        }
Exemplo n.º 8
0
        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);
        }
Exemplo n.º 9
0
        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);
        }
Exemplo n.º 10
0
        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");
        }
Exemplo n.º 11
0
 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;
 }
Exemplo n.º 12
0
        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);
        }
Exemplo n.º 13
0
        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);
        }
Exemplo n.º 14
0
        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);
        }
Exemplo n.º 15
0
        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);
        }
Exemplo n.º 16
0
        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);
        }
Exemplo n.º 17
0
        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);
        }
Exemplo n.º 18
0
        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;
                }
            }
        }
Exemplo n.º 19
0
        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"));
        }
Exemplo n.º 20
0
 public DefaultOperandResolver(JavaClassImage classImage)
 {
     _classImage = classImage ?? throw new ArgumentNullException(nameof(classImage));
 }