GithubHelp home page GithubHelp logo

garghub / joernantlr Goto Github PK

View Code? Open in Web Editor NEW

This project forked from electricalwind/joernantlr

0.0 0.0 0.0 144 KB

C Parser based on JOERN : https://github.com/fabsx00/joern

ANTLR 1.41% Java 98.59%

joernantlr's Introduction

Joern

This repository is a maven adaptation of the joern project (warning: some parts have been removed).

Author:

Fabian Yamaguchi [email protected]

Contributors:

Alwin Maier [email protected]

Introduction

This library allows one to parse a C file under the form of a string from a java program. An AST, CFG or DDG can then be computed from this parsed file.

Use

This library is used in this project by the FileMetrics project

In order to retrieve the AST of a file the following steps are required (code in Kotlin):

  1. create a Walker

     class TestASTWalker : ASTWalker() {
    
         var codeItems: MutableList<ASTNode>
    
         init {
             codeItems = LinkedList<ASTNode>()
         }
    
         override fun startOfUnit(ctx: ParserRuleContext, filename: String) {
    
         }
    
         override fun endOfUnit(ctx: ParserRuleContext, filename: String) {
    
         }
    
         override fun processItem(item: ASTNode, itemStack: Stack<ASTNodeBuilder>) {
             codeItems.add(item)
         }
    
     }
    
  2. create the parser and walker elements

     val parser = ANTLRCModuleParserDriver()
     val walker = TestASTWalker()
     parser.addObserver(walker)  
    
  3. Then lexe your file, note that if a tokenize file is enough, you cat stop after this step

     val inputStream = ANTLRInputStream(compatibleFileContent())
     val lex = ModuleLexer(inputStream)
     val token = TokenSubStream(lex)
    
  4. If you need the AST, the following step are necessary:

     parser.parseAndWalkTokenStream(token)
     val listofNode: MutableList<ASTNode> = walker.codeItems
    
  5. To compute the CFG, you first need to iterate over the list of node, and in case it is a function, you can do the following:

     for (node in listnode) {
         if (node is FunctionDef) {
             val cfg = CCFGFactory.convert(node.content)
          }
     }
    
  6. In case you want to use UseDef Graph

     val cfgToUdg = CFGToUDGConverter()
     val udg = cfgToUdg.convert(cfg)
    
  7. you can also add taint source

     cfgToUdg.addTaintSource("foo", 0);
    
  8. Once this done, you can access the DDG by doing so:

     val udgToDefUse = CFGAndUDGToDefUseCFG();
     val defUseCfg = udgToDefUse.convert(cfg,udg)
     val ddg = DDGCreator().createForDefUseCFG(defUseCfg)
    

Known Issue

  • The library is not handling correctly function containing __
  • No handling of the NULL keyword

joernantlr's People

Contributors

electricalwind avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.