您的位置:首页 > 编程语言

无用代码——DO-178B/ED-12B学习笔记之一

2009-12-21 17:41 363 查看
无用代码
­——DO-178B/ED-12B学习笔记之一

1. 无用代码的定义

  DO-178B/ED-12B对无用代码(Dead code)的定义是:
  Executable object code (or data) which, as a result of a design error cannot be executed (code) or used (data) in a operational configuration of the target computer environment and is not traceable to a system or software requirement. An exception is embedded identifiers.
  试译如下:
  因设计错误而产生的,在目标机环境的运行配置中不能执行的(代码)或不能使用的(数据),并且不能追踪到一个系统或软件需求的可执行的目标代码(或数据 )。嵌入的标识符号除外。
  为了准确把握无用代码的定义,还需了解DO-178B/ED-12B对目标码和源码的定义。
  DO-178B/ED-12B对目标码(Object Code)的定义是:
  A low-level representation of the computer program not usually in a form directly usable by the target computer but in a form which includes relocation information in addition to the processor instruction information.
  试译如下:
  计算机程序的一种低级表示,其格式通常不是目标(target)计算机直接可用的,而是包含了计算机指令信息以及重定位信息。
  DO-178B/ED-12B对源码(Source code)的定义是:
  Code written in source languages, such as assembly language and/or high level language, in a machine-readable form for input to an assembler or a compiler.
  试译如下:
  用源语言(如汇编语言或高级语言)编写的代码,其格式是机器可读的,以作为汇编程序或编译程序的输入。
  DO-178B/ED-12B没有定义可执行的目标码(executable object code)。那么按照上述目标码的定义和常识,可执行的目标码应是目标码经过连接程序(linker)处理后生成的目标(target)计算机直接可用的代码。
  由此可见:
  ——无用代码是针对可执行的目标码而不是源码定义的;
  ——无用代码包括代码和数据;
  ——认定为无用代码有两个条件,一是执行不到,即程序的控制流达不到该代码或数据流达不到该数据;二是追踪不到,即从该代码或数据不能追踪到任何一条系统或软件需求。
  针对可执行的目标码来定义无用代码,这可能因为最终装机的是可执行的目标码,而源码与目标码不是一一对应的。然而,为了消除可执行的目标码中的无用代码,我们首先要消除源码中的无用代码。
  源码中有些语句,如类型定义,不会直接产生目标码,因此也不会变成无用代码。但是,从软件验证角度有必要分析这些没有使用的语句,可能从中发现其它问题。
  从代码追踪到系统或软件需求有时是很困难的,尤其是当需求描述不够详细时。DO-178B/ED-12B的软件需求包括高级需求和低级需求,还有派生需求(derived requirements)。有些代码往往只能追踪到派生需求。
  无用代码的两个条件是与的关系还是或的关系?从文字上看好像是与的关系。但是,如果要两个条件都成立,那么能追踪但不能执行的就不算无用代码?

2. 无用代码的识别

  我们在DO-178B/ED-12B和DO-248B/ED-94B中只找到了以下两段与无用代码的识别有关联的论述。
  DO-178B/ED-12B在6.4.4.3条的开头有这样一句,“Structural coverage analysis may reveal code structure that was not exercised during testing.”。
  DO-248B/ED-94B在FAQ#74中谈论MC/DC时有这样一句,“Additionally, any dead code or unreachable paths in the code may be identified.”。
  由此可见:
  ——DO-178B/ED-12B和DO-248B/ED-94B都没有明确规定识别的方法或途径
  ——结构覆盖分析可以(may)但不是必定揭示无用代码
  为了理解无用代码识别与结构覆盖分析的关系,需要理解DO-178B/ED-12B对结构覆盖分析的论述。
  DO-178B/ED-12B规定了三种结构覆盖,即语句覆盖、判定覆盖、MC/DC。
  DO-178B/ED-12B的6.4.4.2.b条的第一句:The structural coverage analysis may be performed on the Source Code, unless the software level is A and the compiler generates object code that is not directly traceable to Source Code statements. Then, additional verification should be performed on the object code to establish the correctness of such generated code sequences.(试译如下:一般情况下,可以只对源代码进行结构覆盖分析。但是,当软件等级是A级,以及编译器产生了不能直接追踪到源代码语句的目标码,还应对目标码进行额外验证以确定所产生的代码序列的正确性。)
  这三种结构覆盖都是面向可执行语句的,可以揭示未执行的代码,但不一定能揭示未使用的数据。因此还需要使用面向数据的覆盖测试技术,如数据覆盖测试[3][4]等,来识别未使用的数据。
  DO-178B/ED-12B的6.4.2.1条论述了Normal Range Test Cases(正常范围测试用例),6.4.2.2条论述了Robustness Test Cases(健壮性测试用例)。这两类测试用例也可能揭示未使用的数据。
  以下用一个例子说明结构覆盖不足以揭示未使用的数据。设有如下C函数:
  int convert_1(int i)
  {
   int r;
   if (i == 1)
   r = 10;
   else if (i == 2)
   r = 20;
   else if (i == 3)
   r = 30;
   else
   r = 0;
   return r;
  }
  设输入参数i分别为1、2、3、4,用这样4个测试用例测试函数convert_1,可以达到语句覆盖,并测试了其中的3个转换因子10、20、30。
  如果把代码改写为:
  int convert_2(int i)
  {
   static int factor_array[3] = {10, 20, 30};
   int r;
   if (i >= 1 && i <= 3)
   r = factor_array[i-1];
   else
   r = 0;
   return r;
  }
  这样用2个测试用例就可以达到语句覆盖,但不能揭示数组factor_array的三个分量是否都被使用了。
  如果是以下形式的代码:
  typedef struct
  {
   int factor_1;
   int factor_2;
   int factor_3;
  } factor_t;
  factor_t factor_record = {10, 20, 30};
  int convert_3(int i)
  {
   if (i == 1) return factor_record.factor_1;
   else return factor_record.factor_2;
  }
  某些静态数据流分析工具不能揭示factor_record.factor_3未被使用的问题。

3. 无用代码的处理

  DO-178B/ED-12B的6.4.4.3.c条规定:
  Dead code: The code should be removed and an analysis performed to assess the effect and the need for reverification.
  试译如下:
  无用代码:应消除这种代码,并进行分析以评定消除后的影响以及是否需要重新验证。
  DO-248B/ED-94B的FAQ#28强调和诠释了上述要求:
  It is important to realize that DO-178B/ED-12B not only states that dead code “should be removed”: Section 6.4.4.3c continues with the statement that "an analysis (should be) performed to assess the effect (of removing the dead code) and the need for reverification."
  由此可见,无用代码处理包括两件事,一是消除,二是分析,必要时还要做第三件事——重新验证。
参考文献

[1] DO-178B/ED-12B, Software Considerations in Airborne Systems and Equipment Certification, December 1, 1992
[2] DO-248B/ED-94B,Final Report for Clarification of DO-178B, October 12, 2001
[3] 古乐、史九林,软件测试技术概论,清华大学出版社,2004
[4] Elfriede Dustin,Jeff Rashka, John Paul, Automated Software Testing, 1999
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: