第4讲-软件静态分析
4 Variables were declared but never used
5 UR data flow anomalies found 6 Recursion in procedure calls found 7 DU data flow anomalies found 8 DD data flow anomalies found 9 Defined parameter has possible clear path
张虹
2011/4/29
软件测试技术—第4讲—软件静态分析
23
3. 编码规则检查
编程规则检查流程
• 例如:使用Testbed
定义编码规则 培训软件 编程人员 编程
cpen.dat cpppen.dat asmpen.dat
Testbed 自动检查
creport.dat cppreport.dat asmreport.dat
软件测试技术—第4讲—软件静态分析
5
软件静态分析的主要内容
1.
2. 3. 4. 5.
数据流分析
控制流分析
编码规则检查
接口分析
软件度量分析
张虹
2011/4/29
软件测试技术—第4讲—软件静态分析
6
1. 数据流分析
用数据流图来分析数据处理的异常现象(数 据异常),这些异常包括初始化、赋值、或 引用数据等的序列的异常。
(D) Overlapping loops
张虹
2011/4/29
软件测试技术—第4讲—软件静态分析
19
(C) Loop with multiple entry points
(E) Parallel loops
张虹
2011/4/29
软件测试技术—第4讲—软件静态分析
20
结构化编程验证过程
如果程序是完全结构化的,则控制流图可简化 为最后一个节点。
数据流反常
• UR
» 未定义就引用; » 真实错误(genuine error). 1 void proc () 2 { 3 int x,y,z,t; 4 x = 1; 5 if (y > 0) 6 x = 2; 7 /* end if */ 8 z = x + 1; 9 } 10
• DU
» 定义后未引用; » 可疑错误(suspicious error)
if I=0 then go to label: end if;
--------------------------------------------------------------------236 237 START 238 239 FINISH 240 } z = 5 ; z = 9 ; l6 : z = 10 ; 0 UNREACHABLE ***** 0 UNREACHABLE ***** 1 1 1
14
控制流分析
结构化编程验证;
不可达代码检查。
张虹
2011/4/29
软件测试技术—第4讲—软件静态分析
15
结构化编程验证(SPV)
如果使用不当,C语言某些结构很容易引起错误 或缺陷:
• CASE语句;
» SWICH,CASE,DEFAULT,BREAK
• IF-THEN语句;
• FOR循环结构;
V(G) = 10 - 8 + 2 =4 EV(G)=4 Knots=0
张虹
2011/4/29
软件测试技术—第4讲—软件静态分析
22
不可达代码
如果从程序起始没有任何 路径到达指定代码行,代 码将被标为不可达。
• 通常由于使用goto语句; • 或调用子程序不能返回。
FINISH 235 goto l6 ; 2
3 Actual parameter is also global to procedure.
10 Globals used inside procedure. The procedure uses some global variables. 11 Parameters do not match expected actions 12 Referenced parameter has possible clear path. 13 Global accessed in procedure matches local parameter. 14 Assignment made to value parameter.
编码规则报告
张虹
2011/4/29
软件测试技术—第4讲—软件静态分析
24
编码规则检查
cpen.dat编码规则文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 0 1 0 1 500 1 50 1 0 1 0 1 0 1 0 1 0 0 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 6 1 0 1 0 1 0 1 S Procedure name reused. 2 S Label name reused. MISRA/DERA 12 3 S More than *** executable reformatted lines in file. 4 S Procedure exceeds *** reformatted lines. 5 S Empty then clause. DERA 141 6 S Procedure pointer declared. 7 S Jump out of procedure. 8 S Empty else clause. 9 S Assignment operator in expression. MISRA/DERA 33, 35, 40, 46 10 S Not used 11 S No brackets to loop body (added by Testbed). MISRA/DERA 59 12 S No brackets to then/else (added by Testbed). MISRA/DERA 59 13 S goto detected. MISRA/DERA 56 14 S Procedural parameter declared. DERA 149 15 S Anonymous field to structure. MISRA/DERA 108, 113 16 S Multiple labels declared. 17 S Code insert found. MISRA/DERA 3 18 S More than *** parameters in procedure. 19 S Procedural para used in an uncalled procedure. 20 S Parameter not declared explicitly. 21 S Number of parameters does not match. MISRA/DERA 78 软件测试技术—第4讲—软件静态分析
12
23 Function has parameter side effects
24 Procedure definition has no associated prototype
张虹
2011/4/29
软件测试技术—第4讲—软件静态分析
2. 控制流分析
使用控制流图系统地检查程序的控制结构。 按照结构化程序规则和程序结构的基本要求进行 程序结构检查。 控制流图描述了程序元素和它们的执行顺序 之间的联系。一个程序元素通常是一个条件、一 个简单的语句,或者一块语句(多个连续语句) 。
15 Unused procedural parameter.
16 Identical actual parameters in call.
张虹
2011/4/29
软件测试技术—第4讲—软件静态分析
11
数据流标准反常-2
17 Identifier exceeds *** significant chars 18 Identifier name reused 19 Procedure called before being defined 20 Variable used before being declared 21 Procedure defined in inner block 22 Function has global variable side effects
• 基本圈复杂度(Essential Cyclomatic Complexity) »通过结构化流程简化后,最终圈复杂度; »完全结构化程序,基本圈复杂度值为1。
张虹
2011/4/29
软件测试技术—第4讲—软件静态分析
21
结构化编程验证过程
G
18 Edges
G’
10 Edges 8 Nodes
14 Nodes V(G) = 18 - 14 + 2 = 6 2 Proper subgraphs EV(G) = 6 - 2 = 4 Knots=0
张虹
2011/4/29
软件测试技术—第4讲—软件静分析
13
控制流图
if (state_1.eof==0 && status.eof==1) { status.eof=0; k=1; } else { status.eof=1; k=0; }
1
2
3
4
if then else
张虹
2011/4/29
软件测试技术—第4讲—软件静态分析
如:x = y + 10