Java PathFinder(一) Java PathFinder基于Eclipse的安装配置及使用

前言

在研究JPF时,发现存在两个版本的JPF,一个是在sourceforge上代码库,自2006后不再更新,但用svn签出;一个是NASA软件工程实验室自建的Mercurial版本库服务器上代码库,现在依然在开发更新,并且提供的完整的在线WIkI文档。我们使用的是NASA的.

准备材料:

  • eclipse
  • jpf-core
  • jdk 1.8或以上

安装过程

TortoiseHg

官网

  • 如果要下载JPF的相关组件,必须要使用一个名叫Mercurial的分布式版本控制系统。通过clone从JPF的官网上clone下jpf-core的源码,链接如下:jpf-core.
  • 精简版的jpf以及配置文件如下,可以直接在配置文件中修改路径使用即可下好的核心部分
  • 如果需要更加丰富的组件可以在如下链接clone,在配置文件中按照jpf-core的配置方法照猫画虎即可

Eclipse中JPF插件的安装

在Eclipse的菜单中

helpinstallnewsoftware

这里写图片描述
插件的链接如下:
eclipse jpf plugin

Location里面填这个:

http://babelfish.arc.nasa.gov/trac/jpf/raw-attachment/wiki/install/eclipse-plugin/update/

然后一路next……..

写好的那个配置文件site.properties:
JPF site cofiguration
#以下是路径,按照你自己的放置jpf-core的路径设置
jpf-core=E:\JPF\jpf-core
extensions=${jpf-core}
Eclipse插件的设置

菜单按如下路径一直点

windowpreferenceJPF preference

这里写图片描述

导入jpf path finder

菜单按如下路径一路点下去

Fileimportexitingproject

然后导入之前的jpf-core项目,我们来看它的构建的配置文件:

<?xml version="1.0" ?>

<!--
  build.xml - the JPF core build script
              using Ant (http://jakarta.apache.org/ant)
  public targets:

    build (default)   compile classes and build JPF jar files
    compile           compile JPF and its specific (modeled) environment libraries
    test              run all JPF tests
    clean             remove the files that have been generated by the build process
    buildinfo         create buildinfo properties file
-->

<project name="jpf-core" default="build" basedir=".">

  <!-- ===================== ===== COMMON SECTION ========================== -->

  <!-- 
    local props have to come first, because Ant properties are immutable
    NOTE: this file is local - it is never in the repository!
  -->
  <property file="local.properties"/>
  <property environment="env"/>

  <!-- compiler settings -->
  <property name="debug"         value="on"/>
  <property name="deprecation"   value="on"/>

  <uptodate property="build_uptodate" targetfile="build/main/gov/nasa/jpf/build.properties" srcfile="build.properties"/>


  <!-- generic classpath settings -->
  <path id="lib.path">
    <pathelement location="build/main"/>
    <pathelement location="build/peers"/>
    <pathelement location="build/annotations"/>
    <fileset dir=".">
      <include name="lib/*.jar"/>
    </fileset>
  </path>


  <!-- init: common initialization -->
  <target name="-init">
    <tstamp/>

    <mkdir dir="build"/>               <!-- the build root -->

    <!-- the things that have to be in the classpath of whatever runs Ant -->
    <available property="have_javac" classname="com.sun.tools.javac.Main"/>
    <fail unless="have_javac">no javac was found __or__ check http://babelfish.arc.nasa.gov/trac/jpf/wiki/install/build for possible solutions</fail>


    <available file="src/main"        type="dir" property="have_main"/>
    <available file="src/annotations" type="dir" property="have_annotations"/>
    <available file="src/peers"       type="dir" property="have_peers"/>
    <available file="src/classes"     type="dir" property="have_classes"/>
    <available file="src/tests"       type="dir" property="have_tests"/>
    <available file="src/examples"    type="dir" property="have_examples"/>

    <available file=".hg"             type="dir" property="have_hg"/>

    <!-- for the core, it's a fail if any of these is missing -->
    <fail unless="have_main">no src/main</fail>
    <fail unless="have_annotations">no src/annotations</fail>
    <fail unless="have_peers">no src/peers</fail>
    <fail unless="have_classes">no src/classes</fail>
    <fail unless="have_tests">no src/tests</fail>
    <fail unless="have_examples">no src/examples</fail>

    <condition property="have_java8">
        <equals arg1="${ant.java.version}" arg2="1.8"/>
    </condition>

  </target>


  <!-- ======================= COMPILE SECTION ============================= -->

  <!-- public compile -->
  <target name="compile" depends="-init,-compile-annotations,-compile-main,-compile-peers,-compile-classes,-compile-tests,-compile-examples"
          description="compile all JPF core sources" >
      <copy file="build.properties" todir="build/main/gov/nasa/jpf" failonerror="false"/>
  </target>

  <target name="-compile-annotations" if="have_annotations">
    <mkdir dir="build/annotations"/>
    <javac srcdir="src/annotations" destdir="build/annotations" includeantruntime="false"
           debug="${debug}" deprecation="${deprecation}" classpath=""/>
  </target>

  <target name="-compile-main" if="have_main">
    <mkdir dir="build/main"/>
    <javac srcdir="src/main" destdir="build/main" includeantruntime="false"
           debug="${debug}" deprecation="${deprecation}" classpathref="lib.path">
        <!--
        <compilerarg value="-XDenableSunApiLintControl"/>
        <compilerarg value="-Xlint:all"/>
        -->
    </javac>

  </target>

  <target name="-compile-peers" if="have_peers" depends="-compile-main" >
    <mkdir dir="build/peers"/>
    <javac srcdir="src/peers" destdir="build/peers" includeantruntime="false"
           debug="${debug}" deprecation="${deprecation}" classpathref="lib.path">
        <compilerarg value="-XDenableSunApiLintControl"/>
        <compilerarg value="-Xlint:all,-sunapi,-serial"/>       
     </javac>
  </target>

  <target name="-compile-classes" if="have_classes" depends="-compile-annotations,-compile-main" >
    <mkdir dir="build/classes"/>
    <javac srcdir="src/classes" destdir="build/classes" includeantruntime="false"
           debug="${debug}" deprecation="${deprecation}">
      <compilerarg value="-XDenableSunApiLintControl"/>
      <compilerarg value="-Xlint:all,-sunapi"/>   
      <classpath>
        <path refid="lib.path"/>
        <pathelement location="build/annotations"/>
      </classpath>
    </javac>
  </target>

  <target name="-compile-tests" if="have_tests" depends="-compile-annotations,-compile-main,-compile-classes">
    <mkdir dir="build/tests"/>

    <javac sourcepath="" srcdir="src/tests" destdir="build/tests" includeantruntime="false"
           debug="${debug}" deprecation="${deprecation}"
           includes="*,gov/nasa/jpf/**,classloader_specific_tests/**">
      <compilerarg value="-XDenableSunApiLintControl"/>
      <compilerarg value="-Xlint:all,-sunapi,-serial,-rawtypes,-unchecked"/>

      <include name="*gov/nasa/jpf/**"/>
      <include name="classloader_specific_tests/**"/>
      <include name="java8/**" if="have_java8"/>

      <classpath>
        <path refid="lib.path"/>
        <pathelement location="build/annotations"/>
        <pathelement location="build/classes"/>
      </classpath>       
    </javac>
  </target>

  <target name="-compile-examples" if="have_examples" depends="-compile-annotations,-compile-main">
    <mkdir dir="build/examples" />
    <javac srcdir="src/examples" destdir="build/examples" includeantruntime="false"
           debug="${debug}" deprecation="${deprecation}"
           classpathref="lib.path"/>
  </target>


  <!-- ======================= MISC SECTION ================================ -->

  <target name="-version" if="have_hg">
    <exec executable="hg" outputproperty="version" searchpath="true" failonerror="false" failifexecutionfails="false">
      <arg value="identify"/>
      <arg value="-n"/> 
    </exec>

    <!-- .version is in .hgignore -->
    <echo message="${version}${line.separator}" file=".version"/>
  </target>

  <!-- build jars -->
  <target name="build" depends="-cond-clean,compile,-version"
        description="generate the core JPF jar files" >

    <copy file=".version" todir="build/main/gov/nasa/jpf" failonerror="false"/>

    <jar jarfile="build/jpf-classes.jar">
      <fileset dir="build/classes"/>
      <fileset dir="build/annotations"/>

      <fileset dir="build/main">
          <!-- we need this one in case a SUT uses the Verify API -->
          <include name="gov/nasa/jpf/vm/Verify.class"/>

          <!-- these are required if we run TestJPF derived test classes -->
          <include name="gov/nasa/jpf/JPFShell.class"/>
          <include name="gov/nasa/jpf/util/TypeRef.class"/>
          <include name="gov/nasa/jpf/util/test/TestJPF.class"/>
          <include name="gov/nasa/jpf/util/test/TestMultiProcessJPF.class"/>
          <include name="gov/nasa/jpf/util/test/TestJPFHelper.class"/>
      </fileset>
    </jar>

    <jar jarfile="build/jpf.jar" index="true">
      <fileset dir="build/main"/>
      <fileset dir="build/peers"/>
      <!-- this is redundant, but if JPF is executed from java.class.path it wouldn't find annotations -->
      <fileset dir="build/annotations"/>

      <!-- this is for annotations used by JPF regression tests, which can also be executed outside of junit -->
      <fileset dir="build/classes">
          <include name="org/junit/*.class"/>
      </fileset>

      <manifest>
        <attribute name="Built-By" value="${user.name}"/>
        <attribute name="Implementation-Vendor" value="NASA Ames Research Center"/>
        <attribute name="Implementation-Title" value="Java Pathfinder core system"/>
        <attribute name="Implementation-Version" value="${jpf.version}"/>
      </manifest>
    </jar>

    <!-- optional jar that contains annotations to be used in non-JPF dependent apps -->
    <jar jarfile="build/jpf-annotations.jar">
      <fileset dir="build/annotations"/>
    </jar>

    <!-- this jar is needed to test classloaders, it is used by URLClassLoaderTest -->
    <jar jarfile="build/classloader_specific_tests.jar">
      <fileset dir="build/tests">
        <include name="classloader_specific_tests/*.class"/>
      </fileset>
    </jar>

    <jar jarfile="build/RunJPF.jar">
      <fileset dir="build/main">
        <include name="gov/nasa/jpf/tool/Run.class"/>
        <include name="gov/nasa/jpf/tool/RunJPF.class"/>
        <include name="gov/nasa/jpf/Config.class"/>
        <include name="gov/nasa/jpf/ConfigChangeListener.class"/>
        <include name="gov/nasa/jpf/Config$MissingRequiredKeyException.class"/>
        <include name="gov/nasa/jpf/JPFClassLoader.class"/>
        <include name="gov/nasa/jpf/JPFShell.class"/>
        <include name="gov/nasa/jpf/JPFException.class"/>
        <include name="gov/nasa/jpf/JPFConfigException.class"/>
        <include name="gov/nasa/jpf/JPFTargetException.class"/>
        <include name="gov/nasa/jpf/util/JPFSiteUtils.class"/>
        <include name="gov/nasa/jpf/util/FileUtils.class"/>
        <include name="gov/nasa/jpf/util/StringMatcher.class"/>
        <include name="gov/nasa/jpf/util/Pair.class"/>        
      </fileset>

      <manifest>
        <attribute name="Built-By" value="${user.name}"/>
        <attribute name="Implementation-Vendor" value="NASA Ames Research Center"/>
        <attribute name="Implementation-Title" value="Java Pathfinder core launch system"/>
        <attribute name="Implementation-Version" value="${jpf.version}"/>
        <attribute name="Main-Class" value="gov.nasa.jpf.tool.RunJPF"/>
      </manifest>
    </jar>

    <jar jarfile="build/RunTest.jar">
      <fileset dir="build/main">
        <include name="gov/nasa/jpf/tool/Run.class"/>
        <include name="gov/nasa/jpf/tool/RunTest.class"/>
        <include name="gov/nasa/jpf/tool/RunTest$Failed.class"/>
        <include name="gov/nasa/jpf/Config.class"/>
        <include name="gov/nasa/jpf/ConfigChangeListener.class"/>
        <include name="gov/nasa/jpf/Config$MissingRequiredKeyException.class"/>
        <include name="gov/nasa/jpf/JPFClassLoader.class"/>
        <include name="gov/nasa/jpf/JPFException.class"/>
        <include name="gov/nasa/jpf/JPFConfigException.class"/>
        <include name="gov/nasa/jpf/util/JPFSiteUtils.class"/>
        <include name="gov/nasa/jpf/util/FileUtils.class"/>
        <include name="gov/nasa/jpf/util/StringMatcher.class"/>
        <include name="gov/nasa/jpf/util/DevNullPrintStream.class"/>
      </fileset>
      <manifest>
        <attribute name="Built-By" value="${user.name}"/>
        <attribute name="Implementation-Vendor" value="NASA Ames Research Center"/>
        <attribute name="Implementation-Title" value="Java Pathfinder test launch system"/>
        <attribute name="Implementation-Version" value="${jpf.version}"/>
        <attribute name="Main-Class" value="gov.nasa.jpf.tool.RunTest"/>
      </manifest>
    </jar>

  </target>


  <!-- public clean: cleanup from previous tasks/builds -->
  <target name="clean"
          description="remove all build artifacts and temporary files">
    <delete dir="build" failonerror="false"/>
    <delete dir="tmp" failonerror="false"/>
    <delete>
      <fileset dir="." includes="**/*~" defaultexcludes="no" />
      <fileset dir="." includes="**/*.bak" defaultexcludes="no" />
      <fileset dir="." includes="**/error.xml" />
    </delete>
  </target>

  <target name="-cond-clean" unless="build_uptodate"
          description="remove all build artifacts and temporaries if build.properties has been changed">
    <antcall target="clean"/>
  </target>


  <!-- generate buildinfo file  -->
  <target name="buildinfo" description="create buildinfo properties">

    <!-- make this fail if there are uncommitted changes -->
    <exec executable="hg" outputproperty="uncommitted_changes" failifexecutionfails="true">
      <arg value="status"/>
    </exec>
    <condition property="have_uncommitted_changes">
      <length string="${uncommitted_changes}" trim="true" when="greater" length="0"/>
    </condition>
<!--
    <fail if="have_uncommitted_changes">hg status shows uncommitted changes:
      ${uncommitted_changes}
    </fail>
-->
    <exec executable="hg" outputproperty="hg.tip.id" failifexecutionfails="false">
      <arg value="tip"/>
      <arg value="--template"/>
      <arg value="{rev}:{node|short}\n"/>
    </exec>
    <exec executable="hg" outputproperty="hg.author" failifexecutionfails="false">
      <arg value="tip"/>
      <arg value="--template"/>
      <arg value="{author}\n"/>
    </exec>
    <exec executable="hg" outputproperty="hg.tip.date" failifexecutionfails="false">
      <arg value="tip"/>
      <arg value="--template"/>
      <arg value="{date|isodate}\n"/>
    </exec>
    <exec executable="hg" outputproperty="hg.paths.default" failifexecutionfails="false">
      <arg value="showconfig"/>
      <arg value="paths.default"/>
    </exec>

    <exec executable="hostname" failifexecutionfails="false" outputproperty="env.COMPUTERNAME"/>
    <property name="hostname" value="${env.COMPUTERNAME}"/>  <!-- Windows doesn't have hostname -->

    <!-- it seems the 'propertyfile' task just appends -->
    <delete file="build.properties" failonerror="false"/>

    <propertyfile file="build.properties" comment="JPF core build info">
      <entry key="revision" value="${hg.tip.id}"/>
      <entry key="date.tip" value="${hg.tip.date}"/>

      <entry key="author" value="${hg.author}"/>
      <entry key="repository" value="file://${hostname}${basedir}"/>
      <entry key="upstream" value="${hg.paths.default}"/>

      <entry key="java.version" value="${java.version}"/>

      <entry key="os.arch" value="${os.arch}"/>
      <entry key="os.name" value="${os.name}"/>
      <entry key="os.version" value="${os.version}"/>
      <entry key="user.country" value="${user.country}"/>

    </propertyfile>

  </target>

  <target name="dist" description="build binary distribution">
    <delete file="build/${ant.project.name}*.zip"/>

    <exec executable="hg" outputproperty="hg.tip.rev" failifexecutionfails="false">
      <arg value="tip"/>
      <arg value="--template"/>
      <arg value="-r{rev}"/>
    </exec>

    <!-- 2do this seems stupid - there needs to be a better way to re-base (zip basedir fails miserably) -->
    <zip destfile="build/${ant.project.name}${hg.tip.rev}.zip" update="false" excludes="*">
      <zipfileset file="jpf.properties"  prefix="${ant.project.name}"/>
      <zipfileset file="build.properties"  prefix="${ant.project.name}"/>
      <zipfileset dir="lib"  prefix="${ant.project.name}/lib"/>
      <zipfileset dir="bin"  prefix="${ant.project.name}/bin" filemode="754"/>
      <zipfileset dir="build" includes="*.jar" prefix="${ant.project.name}/build"/>
    </zip>
  </target>

  <target name="src-dist" description="build source distribution">
    <delete file="build/${ant.project.name}*-src.zip"/>

    <exec executable="hg" outputproperty="hg.tip.rev" failifexecutionfails="false">
      <arg value="tip"/>
      <arg value="--template"/>
      <arg value="-r{rev}"/>
    </exec>

    <zip destfile="build/${ant.project.name}${hg.tip.rev}-src.zip" update="false" excludes="*" whenempty="skip">
      <zipfileset file="jpf.properties"  prefix="${ant.project.name}"/>
      <zipfileset file="build.properties"  prefix="${ant.project.name}"/>
      <zipfileset file="build.xml"  prefix="${ant.project.name}"/>
        <zipfileset file="LICENSE-2.0.txt"  prefix="${ant.project.name}"/>
      <zipfileset file="README"  prefix="${ant.project.name}"/>
      <zipfileset dir="src" prefix="${ant.project.name}/src"/>
      <zipfileset dir="lib"  prefix="${ant.project.name}/lib" erroronmissingdir="false"/>
      <zipfileset dir="bin"  prefix="${ant.project.name}/bin" filemode="754"/>
      <zipfileset dir="tools" prefix="${ant.project.name}/tools" erroronmissingdir="false"/>

      <!-- IDE related configuration files -->
      <zipfileset file=".project"  prefix="${ant.project.name}"/>
      <zipfileset file=".classpath"  prefix="${ant.project.name}"/>
      <zipfileset dir="eclipse" prefix="${ant.project.name}/eclipse"/>

      <zipfileset dir="nbproject" prefix="${ant.project.name}/nbproject"/>
    </zip>
  </target>

  <!-- ======================= TEST SECTION ================================ -->



  <target name="test" depends="build"
          description="run core regression tests" if="have_tests">

    <!-- note this can be directly set in local.properties, which overrides this setting -->
    <property name="junit.home" value="${env.JUNIT_HOME}"/>

    <condition property="junit.usefile">
      <!-- don't set if this is running from within an IDE that collects output -->
      <not>
        <isset property="netbeans.home"/>  
      </not>
    </condition>

    <junit printsummary="on" showoutput="off" 
           haltonfailure="no" logfailedtests="true" failureproperty="test.failed" 
           dir="${basedir}" fork="yes" forkmode="perTest" maxmemory="1024m" outputtoformatters="true">
      <formatter type="plain" usefile="${junit.usefile}"/>

      <assertions>
        <enable/>
      </assertions>

      <classpath>
        <path refid="lib.path"/>
        <pathelement location="build/tests"/>
        <pathelement location="build/classes"/>
        <pathelement location="build/annotations"/>

        <fileset dir="${junit.home}">
          <include name="**/*.jar"/>
        </fileset>  
      </classpath>

      <batchtest todir="build/tests">
        <fileset dir="build/tests">
          <exclude name="**/JPF_*.class"/>
          <include name="**/*Test.class"/>

          <exclude name="**/SplitInputStreamTest.class"/>
        </fileset>
      </batchtest>

    </junit>

    <fail if="test.failed" />

  </target>


</project>

是使用javac进行编译的,而且源码中因为在接口中出现了Java 8的新特性default关键字,所以要保证你的执行环境和编译环境版本都要在1.8或以上

命令行查看

#执行环境
java -version
#编译环境 
javac -version

如果版本不匹配,下载了新的JDK后修改JAVA_HOME环境变量即可

如果,构建成功,出现如下:

Buildfile: E:\JPF\jpf-core\build.xml
-cond-clean:
-init:
-compile-annotations:
-compile-main:
-compile-peers:
-compile-classes:
-compile-tests:
-compile-examples:
compile:
-version:
build:
     [copy] Copying 1 file to E:\JPF\jpf-core\build\main\gov\nasa\jpf
      [jar] Building jar: E:\JPF\jpf-core\build\jpf.jar
BUILD SUCCESSFUL
Total time: 1 second

JPF的使用

首先一个普通的类


public class Racer implements Runnable {
    int d = 42;
    public void run () {
    doSomething(1001);
    d = 0; // (1)
    }
    public static void main (String[] args){
    Racer racer = new Racer();
    Thread t = new Thread(racer);
    t.start();
    doSomething(1000);
    int c = 420 / racer.d; // (2)
    System.out.println(c);
    }
    static void doSomething (int n) {
    try { Thread.sleep(n); } catch
    (InterruptedException ix) {}
    }
}

然后

Racer.javaRun asRun Configure

这里写图片描述

#执行的类
+target=Racer
#类文件的路径,不是源代码的路径
+classpath=E:\\code\\eclipse-items\\JPFTest\\bin  
#符号执行的方法
+symbolic.method=Racer.main()

虚拟机参数(vm arguments),默认即可,可以加大,但不可以缩小了,不然会出问题,也不能超过物理内存的大小

然后执行结果如下:

JavaPathfinder core system v8.0 (rev 29+) - (C) 2005-2014 United States Government. All rights reserved.


====================================================== system under test
Racer.main()

====================================================== search started: 16-5-14 下午1:12
10
10
10

====================================================== error 1
gov.nasa.jpf.vm.NoUncaughtExceptionsProperty
java.lang.ArithmeticException: division by zero
    at Racer.main(Racer.java:13)


====================================================== snapshot #1
thread java.lang.Thread:{id:0,name:main,status:RUNNING,priority:5,isDaemon:false,lockCount:0,suspendCount:0}
  call stack:
    at Racer.main(Racer.java:13)


====================================================== results
error #1: gov.nasa.jpf.vm.NoUncaughtExceptionsProperty "java.lang.ArithmeticException: division by zero  a..."

====================================================== statistics
elapsed time:       00:00:00
states:             new=11,visited=2,backtracked=6,end=3
search:             maxDepth=7,constraints=0
choice generators:  thread=10 (signal=0,lock=1,sharedRef=2,threadApi=3,reschedule=4), data=0
heap:               new=378,released=47,maxLive=357,gcCycles=9
instructions:       3440
max memory:         123MB
loaded code:        classes=66,methods=1502

====================================================== search finished: 16-5-14 下午1:12
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值