2017-04-12 79 views
0

我一直在尝试为Java编程语言找到“跟踪方法参数”的确切术语,我通常发现“污点分析”,但仍不确定我是否处于正确的路径。如何跟踪方法参数?

我想要的是跟踪一个方法的参数,看看该方法的哪一部分(在范围内)做参数效果。例如,如果一个参数被分配给另一个变量,我也想保持跟踪所分配的变量。通过提及“部分”,它可以是代码行,语句或控制流图的分支。

我也检查过工具,遇到了Checker FrameworkFindbugs,但是看起来他们并不完全满足我的需要,或者我无法让他们满足我的需求。

请告诉我,“污点分析”是否是我正在寻找的正确术语。此外,欢迎任何其他工具建议。

Checker Framework Live Demo下面有一个编辑的代码。我期望的是,在processRequest()里面当变量String input被污染时,我期望在executeQuery()方法中的所有行中都会得到警告或错误。因为一个被污染的变量被传递给它的参数。

import org.checkerframework.checker.tainting.qual.*; 

public class TaintingExampleWithWarnings { 
    String getUserInput() { 
     return "taintedStr"; 
    } 

    void processRequest() { 
     @Tainted String input = getUserInput(); 
     executeQuery(input); //error: pass tainted string to executeQeury() 
    } 

    public void executeQuery(@Untainted String input) { 
     // Do some SQL Query 
     String token = input + " Hello World"; 
     String tokens[] = token.split(" "); 

     for(int i=0; i<tokens.length; i++) 
     { 
       System.out.println((i+1)+"String: "+tokens[i]) 
     } 

    } 


    /* To eliminate warning in line 10, replace line 10 by 
    * executeQuery(validate(input)); */ 
    /*@Untainted*/ public String validate(String userInput) { 
     // Do some validation here 
     @SuppressWarnings("tainting") 
     @Untainted String result = userInput; 
     return result; 
    } 
} 
+0

对于不同的语言,有不同的污点检查工具。你没有指定一种语言,所以我不能推荐任何东西,但谷歌是你最好的朋友 –

+0

@UlugToprak感谢提醒,我通过指定语言为“Java”来编辑问题。 – Ekin

+0

IntelliJ IDEA集成了可供您使用的工具。 Checkstyle将是我的选择,也可以集成到IDE的 –

回答

1

Checker FrameworkTainting Checker上究竟代码的缺陷线上发出警告:

% javac -g TaintingExampleWithWarnings.java -processor tainting 
TaintingExampleWithWarnings.java:10: error: [argument.type.incompatible] incompatible types in argument. 
     executeQuery(input); //error: pass tainted string to executeQeury() 
        ^
    found : @Tainted String 
    required: @Untainted String 
1 error 

这查明缺陷,准确地表明,你需要在你的程序来解决的。

我希望能进去 executeQuery()方法

executeQuery()的实施是正确的所有行的警告或错误;这是使用executeQuery()是有问题的。

(背景:A 模块化分析是一个在一个时间工作的一种方法的模块化分析依赖于方法的规格。)

类型检查是一个模块化分析的一个例子。其规格是用户编写的形式参数注释。

  • 当类型检查的executeQuery()身体,类型检验器处于 ,正式参数声明是正确的。
  • 当类型检查对executeQuery()的调用时,类型检查器会验证参数是否合法。

如果在程序中某处存在一个类型检查错误,那么你的程序可能会不安全地运行(可能在某个其他位置)。

如果您想知道程序中可能流入的所有可能的位置,那么您需要使用非模块化的全程序分析。此外,整个程序分析需要忽略程序中每个用户编写的注释。这样的分析是可以做到的,而且是一个合理的愿望,但是在你的问题中你没有提到你所提到的工具。