-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | A parser for JVM bytecode files -- -- A parser for JVM bytecode (.class and .jar) files @package jvm-parser @version 0.2.1 -- | Basic datatypes and utilities for the JVM parser. module Language.JVM.Common -- | Replace / characters with . characters slashesToDots :: String -> String -- | Replace . characters with / characters dotsToSlashes :: String -> String -- | JVM Type data Type ArrayType :: Type -> Type BooleanType :: Type ByteType :: Type CharType :: Type -- | ClassType with name of packages separated by slash / ClassType :: String -> Type DoubleType :: Type FloatType :: Type IntType :: Type LongType :: Type ShortType :: Type stringTy :: Type intArrayTy :: Type byteArrayTy :: Type charArrayTy :: Type -- | Returns true if type is an integer value. isIValue :: Type -> Bool -- | Returns true if type is a reference value. isRValue :: Type -> Bool -- | Returns true if Java type is a primitive type. Primitive types are the -- Boolean type or numeric types. isPrimitiveType :: Type -> Bool -- | Returns number of bits that a Java type is expected to take on the -- stack. Type should be a primitive type. stackWidth :: Type -> Int -- | Returns true if Java type denotes a floating point. isFloatType :: Type -> Bool -- | Returns true if Java type denotes a reference. isRefType :: Type -> Bool -- | Unique identifier of field data FieldId FieldId :: !String -> !String -> !Type -> FieldId -- | Class name fieldIdClass :: FieldId -> !String -- | Field name fieldIdName :: FieldId -> !String -- | Field type fieldIdType :: FieldId -> !Type ppFldId :: FieldId -> String -- | A unique identifier for looking up a method in a class. data MethodKey MethodKey :: String -> [Type] -> Maybe Type -> MethodKey methodKeyName :: MethodKey -> String methodKeyParameterTypes :: MethodKey -> [Type] methodKeyReturnType :: MethodKey -> Maybe Type ppMethodKey :: MethodKey -> Doc -- | A value stored in the constant pool. data ConstantPoolValue Long :: Int64 -> ConstantPoolValue Float :: Float -> ConstantPoolValue Double :: Double -> ConstantPoolValue Integer :: Int32 -> ConstantPoolValue String :: String -> ConstantPoolValue ClassRef :: String -> ConstantPoolValue -- | A local variable index. type LocalVariableIndex = Word16 -- | A program counter value. type PC = Word16 -- | A JVM Instruction data Instruction Aaload :: Instruction Aastore :: Instruction Aconst_null :: Instruction Aload :: LocalVariableIndex -> Instruction Areturn :: Instruction Arraylength :: Instruction Astore :: LocalVariableIndex -> Instruction Athrow :: Instruction Baload :: Instruction Bastore :: Instruction Caload :: Instruction Castore :: Instruction Checkcast :: Type -> Instruction D2f :: Instruction D2i :: Instruction D2l :: Instruction Dadd :: Instruction Daload :: Instruction Dastore :: Instruction Dcmpg :: Instruction Dcmpl :: Instruction Ddiv :: Instruction Dload :: LocalVariableIndex -> Instruction Dmul :: Instruction Dneg :: Instruction Drem :: Instruction Dreturn :: Instruction Dstore :: LocalVariableIndex -> Instruction Dsub :: Instruction Dup :: Instruction Dup_x1 :: Instruction Dup_x2 :: Instruction Dup2 :: Instruction Dup2_x1 :: Instruction Dup2_x2 :: Instruction F2d :: Instruction F2i :: Instruction F2l :: Instruction Fadd :: Instruction Faload :: Instruction Fastore :: Instruction Fcmpg :: Instruction Fcmpl :: Instruction Fdiv :: Instruction Fload :: LocalVariableIndex -> Instruction Fmul :: Instruction Fneg :: Instruction Frem :: Instruction Freturn :: Instruction Fstore :: LocalVariableIndex -> Instruction Fsub :: Instruction -- | getfield instruction Getfield :: FieldId -> Instruction Getstatic :: FieldId -> Instruction Goto :: PC -> Instruction I2b :: Instruction I2c :: Instruction I2d :: Instruction I2f :: Instruction I2l :: Instruction I2s :: Instruction Iadd :: Instruction Iaload :: Instruction Iand :: Instruction Iastore :: Instruction Idiv :: Instruction If_acmpeq :: PC -> Instruction If_acmpne :: PC -> Instruction If_icmpeq :: PC -> Instruction If_icmpne :: PC -> Instruction If_icmplt :: PC -> Instruction If_icmpge :: PC -> Instruction If_icmpgt :: PC -> Instruction If_icmple :: PC -> Instruction Ifeq :: PC -> Instruction Ifne :: PC -> Instruction Iflt :: PC -> Instruction Ifge :: PC -> Instruction Ifgt :: PC -> Instruction Ifle :: PC -> Instruction Ifnonnull :: PC -> Instruction Ifnull :: PC -> Instruction Iinc :: LocalVariableIndex -> Int16 -> Instruction Iload :: LocalVariableIndex -> Instruction Imul :: Instruction Ineg :: Instruction Instanceof :: Type -> Instruction Invokeinterface :: String -> MethodKey -> Instruction Invokespecial :: Type -> MethodKey -> Instruction Invokestatic :: String -> MethodKey -> Instruction Invokevirtual :: Type -> MethodKey -> Instruction Ior :: Instruction Irem :: Instruction Ireturn :: Instruction Ishl :: Instruction Ishr :: Instruction Istore :: LocalVariableIndex -> Instruction Isub :: Instruction Iushr :: Instruction Ixor :: Instruction Jsr :: PC -> Instruction L2d :: Instruction L2f :: Instruction L2i :: Instruction Ladd :: Instruction Laload :: Instruction Land :: Instruction Lastore :: Instruction Lcmp :: Instruction Ldc :: ConstantPoolValue -> Instruction Ldiv :: Instruction Lload :: LocalVariableIndex -> Instruction Lmul :: Instruction Lneg :: Instruction Lookupswitch :: PC -> [(Int32, PC)] -> Instruction Lor :: Instruction Lrem :: Instruction Lreturn :: Instruction Lshl :: Instruction Lshr :: Instruction Lstore :: LocalVariableIndex -> Instruction Lsub :: Instruction Lushr :: Instruction Lxor :: Instruction Monitorenter :: Instruction Monitorexit :: Instruction Multianewarray :: Type -> Word8 -> Instruction New :: String -> Instruction Newarray :: Type -> Instruction Nop :: Instruction Pop :: Instruction Pop2 :: Instruction Putfield :: FieldId -> Instruction Putstatic :: FieldId -> Instruction Ret :: LocalVariableIndex -> Instruction Return :: Instruction Saload :: Instruction Sastore :: Instruction Swap :: Instruction Tableswitch :: PC -> Int32 -> Int32 -> [PC] -> Instruction -- | TODO: improve this ppInstruction :: Instruction -> Doc -- | An entry in the exception table for a method data ExceptionTableEntry ExceptionTableEntry :: PC -> PC -> PC -> Maybe Type -> ExceptionTableEntry -- | The starting program counter value where the exception handler applies startPc :: ExceptionTableEntry -> PC -- | The ending program counter value where the exception handler applies. endPc :: ExceptionTableEntry -> PC -- | The program counter value to jump to when an exception is caught. handlerPc :: ExceptionTableEntry -> PC -- | The type of exception that should be caught or Nothing if all types of -- exceptions should be caught. catchType :: ExceptionTableEntry -> Maybe Type type ExceptionTable = [ExceptionTableEntry] type InstructionStream = Array PC (Maybe Instruction) canThrowException :: Instruction -> Bool isArrayLoad :: Instruction -> Bool isReturn :: Instruction -> Bool breaksControlFlow :: Instruction -> Bool nextPcPrim :: InstructionStream -> PC -> PC safeNextPcPrim :: InstructionStream -> PC -> Maybe PC ppType :: Type -> Doc instance Eq Type instance Ord Type instance Eq FieldId instance Ord FieldId instance Show FieldId instance Eq MethodKey instance Ord MethodKey instance Show MethodKey instance Eq ConstantPoolValue instance Show ConstantPoolValue instance Eq Instruction instance Show Instruction instance Eq ExceptionTableEntry instance Show ExceptionTableEntry instance Show Type -- | Control Flow Graphs of JVM bytecode. For now, this module simply -- builds CFGs from instruction streams and does post-dominator analysis. module Language.JVM.CFG -- | A basic block consists of an identifier and the instructions contained -- in that block. data BasicBlock -- | Identifies basic blocks by their position in the instruction stream, -- or by the special BBIdEntry or BBIdExit constructors. data BBId BBIdEntry :: BBId BBIdExit :: BBId BBId :: PC -> BBId ppBBId :: BBId -> Doc data CFG -- | Build a control-flow graph from an instruction stream. We assume that -- the first instruction in the instruction stream is the only external -- entry point in the sequence (typically, the method entry point) buildCFG :: ExceptionTable -> InstructionStream -> CFG -- | fetch an instruction from a CFG by position cfgInstByPC :: CFG -> PC -> Maybe Instruction ppBB :: BasicBlock -> String ppInst :: Instruction -> String -- | Render the CFG of a method into Graphviz .dot format cfgToDot :: ExceptionTable -> CFG -> String -> String -- | isImmediatePostDominator g x y returns True if -- y immediately post-dominates x in control-flow graph -- g. isImmediatePostDominator :: CFG -> BBId -> BBId -> Bool -- | Calculate the post-dominators of a given basic block getPostDominators :: CFG -> BBId -> [BBId] instance Eq BBId instance Ord BBId instance Show BBId instance Show BasicBlock instance Eq BasicBlock instance Eq CFG instance Show CFG instance Enum BBId -- | Parser for the JVM bytecode format. module Language.JVM.Parser -- | JVM Type data Type ArrayType :: Type -> Type BooleanType :: Type ByteType :: Type CharType :: Type -- | ClassType with name of packages separated by slash / ClassType :: String -> Type DoubleType :: Type FloatType :: Type IntType :: Type LongType :: Type ShortType :: Type -- | Returns true if type is an integer value. isIValue :: Type -> Bool -- | Returns true if Java type is a primitive type. Primitive types are the -- Boolean type or numeric types. isPrimitiveType :: Type -> Bool -- | Returns true if type is a reference value. isRValue :: Type -> Bool -- | Returns number of bits that a Java type is expected to take on the -- stack. Type should be a primitive type. stackWidth :: Type -> Int -- | Returns true if Java type denotes a floating point. isFloatType :: Type -> Bool -- | Returns true if Java type denotes a reference. isRefType :: Type -> Bool -- | A value stored in the constant pool. data ConstantPoolValue Long :: Int64 -> ConstantPoolValue Float :: Float -> ConstantPoolValue Double :: Double -> ConstantPoolValue Integer :: Int32 -> ConstantPoolValue String :: String -> ConstantPoolValue ClassRef :: String -> ConstantPoolValue -- | An uninterpreted user defined attribute in the class file. data Attribute Attribute :: String -> ByteString -> Attribute attributeName :: Attribute -> String attributeData :: Attribute -> ByteString data Visibility Default :: Visibility Private :: Visibility Protected :: Visibility Public :: Visibility getClass :: Get Class -- | A JVM class or interface. data Class -- | Returns name of the class className :: Class -> String -- | Returns name of the super class of this class or Nothing if this class -- has no super class. superClass :: Class -> Maybe String -- | Returns true if class is public. classIsPublic :: Class -> Bool -- | Returns true if class is final. classIsFinal :: Class -> Bool -- | Returns true if class is an interface classIsInterface :: Class -> Bool -- | Returns true if class is abstract. classIsAbstract :: Class -> Bool -- | Returns true if class was annotated with the super attribute. classHasSuperAttribute :: Class -> Bool -- | Returns interfaces this clas implements classInterfaces :: Class -> [String] -- | Returns fields in the class classFields :: Class -> [Field] -- | Returns methods in class classMethods :: Class -> [Method] -- | Returns user-defined attributes on class. classAttributes :: Class -> [Attribute] -- | Loads class at given path. loadClass :: FilePath -> IO Class -- | Returns method with given key in class or Nothing if no method with -- that key is found. lookupMethod :: Class -> MethodKey -> Maybe Method showClass :: Class -> String -- | Unique identifier of field data FieldId FieldId :: !String -> !String -> !Type -> FieldId -- | Class name fieldIdClass :: FieldId -> !String -- | Field name fieldIdName :: FieldId -> !String -- | Field type fieldIdType :: FieldId -> !Type -- | A class instance of static field data Field -- | Returns name of field. fieldName :: Field -> String -- | Returns type of field. fieldType :: Field -> Type -- | Returns visibility of field. fieldVisibility :: Field -> Visibility -- | Returns true if field is static. fieldIsStatic :: Field -> Bool -- | Returns true if field is final. fieldIsFinal :: Field -> Bool -- | Returns true if field is volatile. fieldIsVolatile :: Field -> Bool -- | Returns true if field is transient. fieldIsTransient :: Field -> Bool -- | Returns initial value of field or Nothing if not assigned. -- -- Only static fields may have a constant value. fieldConstantValue :: Field -> Maybe ConstantPoolValue -- | Returns true if field is synthetic. fieldIsSynthetic :: Field -> Bool -- | Returns true if field is deprecated. fieldIsDeprecated :: Field -> Bool -- | Returns true if field is transient. fieldIsEnum :: Field -> Bool fieldSignature :: Field -> Maybe String fieldAttributes :: Field -> [Attribute] -- | A unique identifier for looking up a method in a class. data MethodKey MethodKey :: String -> [Type] -> Maybe Type -> MethodKey methodKeyName :: MethodKey -> String methodKeyParameterTypes :: MethodKey -> [Type] methodKeyReturnType :: MethodKey -> Maybe Type -- | Returns method key with the given name and descriptor. makeMethodKey :: String -> String -> MethodKey data Method -- | Returns name of method methodName :: Method -> String -- | Return parameter types for method. methodParameterTypes :: Method -> [Type] -- | Returns the local variable index that the parameter is stored in when -- the method is invoked. localIndexOfParameter :: Method -> Int -> LocalVariableIndex -- | Return parameter types for method. methodReturnType :: Method -> Maybe Type -- | Returns maxinum number of local variables in method. methodMaxLocals :: Method -> LocalVariableIndex methodIsNative :: Method -> Bool -- | Returns true if method is abstract. methodIsAbstract :: Method -> Bool methodBody :: Method -> MethodBody data MethodBody Code :: Word16 -> Word16 -> CFG -> [ExceptionTableEntry] -> LineNumberTable -> LocalVariableTable -> [Attribute] -> MethodBody AbstractMethod :: MethodBody NativeMethod :: MethodBody -- | Exception table entries for method. methodExceptionTable :: Method -> [ExceptionTableEntry] methodKey :: Method -> MethodKey methodIsStatic :: Method -> Bool -- | A local variable index. type LocalVariableIndex = Word16 data LocalVariableTableEntry LocalVariableTableEntry :: PC -> PC -> String -> Type -> LocalVariableIndex -> LocalVariableTableEntry localStart :: LocalVariableTableEntry -> PC localExtent :: LocalVariableTableEntry -> PC localName :: LocalVariableTableEntry -> String localType :: LocalVariableTableEntry -> Type localIdx :: LocalVariableTableEntry -> LocalVariableIndex -- | A program counter value. type PC = Word16 -- | A JVM Instruction data Instruction Aaload :: Instruction Aastore :: Instruction Aconst_null :: Instruction Aload :: LocalVariableIndex -> Instruction Areturn :: Instruction Arraylength :: Instruction Astore :: LocalVariableIndex -> Instruction Athrow :: Instruction Baload :: Instruction Bastore :: Instruction Caload :: Instruction Castore :: Instruction Checkcast :: Type -> Instruction D2f :: Instruction D2i :: Instruction D2l :: Instruction Dadd :: Instruction Daload :: Instruction Dastore :: Instruction Dcmpg :: Instruction Dcmpl :: Instruction Ddiv :: Instruction Dload :: LocalVariableIndex -> Instruction Dmul :: Instruction Dneg :: Instruction Drem :: Instruction Dreturn :: Instruction Dstore :: LocalVariableIndex -> Instruction Dsub :: Instruction Dup :: Instruction Dup_x1 :: Instruction Dup_x2 :: Instruction Dup2 :: Instruction Dup2_x1 :: Instruction Dup2_x2 :: Instruction F2d :: Instruction F2i :: Instruction F2l :: Instruction Fadd :: Instruction Faload :: Instruction Fastore :: Instruction Fcmpg :: Instruction Fcmpl :: Instruction Fdiv :: Instruction Fload :: LocalVariableIndex -> Instruction Fmul :: Instruction Fneg :: Instruction Frem :: Instruction Freturn :: Instruction Fstore :: LocalVariableIndex -> Instruction Fsub :: Instruction -- | getfield instruction Getfield :: FieldId -> Instruction Getstatic :: FieldId -> Instruction Goto :: PC -> Instruction I2b :: Instruction I2c :: Instruction I2d :: Instruction I2f :: Instruction I2l :: Instruction I2s :: Instruction Iadd :: Instruction Iaload :: Instruction Iand :: Instruction Iastore :: Instruction Idiv :: Instruction If_acmpeq :: PC -> Instruction If_acmpne :: PC -> Instruction If_icmpeq :: PC -> Instruction If_icmpne :: PC -> Instruction If_icmplt :: PC -> Instruction If_icmpge :: PC -> Instruction If_icmpgt :: PC -> Instruction If_icmple :: PC -> Instruction Ifeq :: PC -> Instruction Ifne :: PC -> Instruction Iflt :: PC -> Instruction Ifge :: PC -> Instruction Ifgt :: PC -> Instruction Ifle :: PC -> Instruction Ifnonnull :: PC -> Instruction Ifnull :: PC -> Instruction Iinc :: LocalVariableIndex -> Int16 -> Instruction Iload :: LocalVariableIndex -> Instruction Imul :: Instruction Ineg :: Instruction Instanceof :: Type -> Instruction Invokeinterface :: String -> MethodKey -> Instruction Invokespecial :: Type -> MethodKey -> Instruction Invokestatic :: String -> MethodKey -> Instruction Invokevirtual :: Type -> MethodKey -> Instruction Ior :: Instruction Irem :: Instruction Ireturn :: Instruction Ishl :: Instruction Ishr :: Instruction Istore :: LocalVariableIndex -> Instruction Isub :: Instruction Iushr :: Instruction Ixor :: Instruction Jsr :: PC -> Instruction L2d :: Instruction L2f :: Instruction L2i :: Instruction Ladd :: Instruction Laload :: Instruction Land :: Instruction Lastore :: Instruction Lcmp :: Instruction Ldc :: ConstantPoolValue -> Instruction Ldiv :: Instruction Lload :: LocalVariableIndex -> Instruction Lmul :: Instruction Lneg :: Instruction Lookupswitch :: PC -> [(Int32, PC)] -> Instruction Lor :: Instruction Lrem :: Instruction Lreturn :: Instruction Lshl :: Instruction Lshr :: Instruction Lstore :: LocalVariableIndex -> Instruction Lsub :: Instruction Lushr :: Instruction Lxor :: Instruction Monitorenter :: Instruction Monitorexit :: Instruction Multianewarray :: Type -> Word8 -> Instruction New :: String -> Instruction Newarray :: Type -> Instruction Nop :: Instruction Pop :: Instruction Pop2 :: Instruction Putfield :: FieldId -> Instruction Putstatic :: FieldId -> Instruction Ret :: LocalVariableIndex -> Instruction Return :: Instruction Saload :: Instruction Sastore :: Instruction Swap :: Instruction Tableswitch :: PC -> Int32 -> Int32 -> [PC] -> Instruction lookupInstruction :: Method -> PC -> Instruction nextPc :: Method -> PC -> PC -- | An entry in the exception table for a method data ExceptionTableEntry -- | The type of exception that should be caught or Nothing if all types of -- exceptions should be caught. catchType :: ExceptionTableEntry -> Maybe Type -- | The starting program counter value where the exception handler applies startPc :: ExceptionTableEntry -> PC -- | The ending program counter value where the exception handler applies. endPc :: ExceptionTableEntry -> PC -- | The program counter value to jump to when an exception is caught. handlerPc :: ExceptionTableEntry -> PC byteArrayTy :: Type charArrayTy :: Type getElemTy :: Type -> Type intArrayTy :: Type stringTy :: Type unparseMethodDescriptor :: MethodKey -> String mainKey :: MethodKey -- | Returns true if method has debug informaiton available. hasDebugInfo :: Method -> Bool -- | Returns name of source file where class was defined. classSourceFile :: Class -> Maybe String sourceLineNumberInfo :: Method -> [(Word16, PC)] -- | Returns source line number of an instruction in a method at a given -- PC, or the line number of the nearest predecessor instruction, or -- Nothing if neither is available. sourceLineNumberOrPrev :: Method -> PC -> Maybe Word16 -- | Returns the starting PC for the source at the given line number. lookupLineStartPC :: Method -> Word16 -> Maybe PC -- | Returns the enclosing method and starting PC for the source at the -- given line number. lookupLineMethodStartPC :: Class -> Word16 -> Maybe (Method, PC) localVariableEntries :: Method -> PC -> [LocalVariableTableEntry] -- | Returns local variable entry at given PC and local variable index or -- Nothing if no mapping is found. lookupLocalVariableByIdx :: Method -> PC -> LocalVariableIndex -> Maybe LocalVariableTableEntry -- | Returns local variable entry at given PC and local variable string or -- Nothing if no mapping is found. lookupLocalVariableByName :: Method -> PC -> String -> Maybe LocalVariableTableEntry ppInst :: Instruction -> String -- | Replace / characters with . characters slashesToDots :: String -> String -- | Render the CFG of a method into Graphviz .dot format cfgToDot :: ExceptionTable -> CFG -> String -> String instance Eq Visibility instance Show ConstantPoolInfo instance Eq Attribute instance Show Attribute instance Show Field instance Eq LineNumberTable instance Show LineNumberTable instance Eq LocalVariableTableEntry instance Show LocalVariableTableEntry instance Eq MethodBody instance Show MethodBody instance Eq Method instance Show Method instance Show Class instance Ord Method instance Show Visibility -- | A quick-n-dirty reader for JAR files. MANY keep-it-simple concessions -- have been made regarding the zip structure of jars (e.g. no ZIP64, -- Deflate-type compression only, etc., etc.). Please don't mistake this -- module for any kind of fully-fledged unzip implementation! -- -- Info on the zip file format can be found at: -- http://www.pkware.com/documents/casestudies/APPNOTE.TXT module Language.JVM.JarReader -- | Datatype representing a collection of .jar archives. Classfiles can be -- loaded from a JarReader using loadClassFromJar. newtype JarReader JR :: Map ByteString (FilePath, DirEnt) -> JarReader unJR :: JarReader -> Map ByteString (FilePath, DirEnt) -- | Add a .jar archive to a JarReader addJar :: JarReader -> FilePath -> IO JarReader -- | Print all the directory entries known to this JarReader onto the -- console dumpJarReader :: JarReader -> IO () -- | Load a class from the given JarReader. loadClassFromJar :: String -> JarReader -> IO (Maybe Class) -- | Create a new JarReader from the given collection of .jar -- archives newJarReader :: [FilePath] -> IO JarReader instance Show DirEnt instance Show JarReader instance Show DirEnd