制作该 Image 的目的

由于 Infer 工具刚刚推出来,很多人想试用该工具,但是又苦于环境的配置略显复杂,最近有很多人找我解决关于环境配置的问题,这阻碍了 Infer 的推广。而 Docker 技术具有易移植的特性,将该工具的环境制作成 Image,你只需下载在到本地就可以体验该新工具的特性,就可以对其做一个简单的了解。

注意:OC 和 iOS 项目的分析是不支持的

Dockerfile

我并没有利用 Dockerfile 来制作该 Image,但是我将我制作的 Dockerfile 贴出来,方便大家了解我的制作过程:

# Base image
FROM heikomaass/android-sdk

# Author
MAINTAINER doctorq <542113578@qq.com>

# Upgrade apt-get tool
RUN apt-get update
RUN apt-get -y upgrade

# Install tools
RUN apt-get install -y git m4 zlib1g-dev python-software-properties \
    build-essential libgmp-dev libmpfr-dev libmpc-dev unzip software-properties-common python-software-properties



# Install opam
RUN add-apt-repository ppa:avsm/ppa
RUN apt-get update
RUN apt-get install -y ocaml ocaml-native-compilers camlp4-extra opam
RUN opam init --comp=4.01.0  # (answer 'y' to the question)
ENV CAML_LD_LIBRARY_PATH /root/.opam/4.01.0/lib/stublibs
ENV MANPATH :/root/.opam/4.01.0/man
ENV PERL5LIB /root/.opam/4.01.0/lib/perl5
ENV OCAML_TOPLEVEL_PATH /root/.opam/4.01.0/lib/toplevel
ENV OPAM_HOME /root/.opam/4.01.0/bin
ENV PATH ${PATH}:${CAML_LD_LIBRARY_PATH}:${MANPATH}:${PERL5LIB}:${OCAML_TOPLEVEL_PATH}:${OPAM_HOME}
#RUN opam update
RUN opam install sawja.1.5.1 atdgen.1.6.0 javalib.2.3.1 extlib.1.5.4


# Download infer source code
RUN git clone https://github.com/facebook/infer.git

# Install facebook-clang-plugin,clang
RUN cd infer;./update-fcp.sh;
RUN cd infer;apt-get install -y groff
RUN cd infer;../facebook-clang-plugin/clang/setup.sh;
RUN eval `opam config env`;cd infer;./compile-fcp.sh
RUN eval `opam config env`;cd infer;make -C infer
ENV INFER_HOME /infer/infer
ENV PATH ${PATH}:${INFER_HOME}/bin

#Install gradle-2.5
RUN add-apt-repository ppa:cwchien/gradle
RUN apt-get update
#RUN apt-cache search gradle
RUN apt-get install gradle-2.5
CMD eval `opam config env`

(已验证成功,使用该 dockerfile 文件可以成功制作)

上传

在经历了 1 个礼拜的上传失败后,公司的网络终于给力了,infer 的 image 终于上传 Docker Hub 成功.

成功信息


58deMacBook-Pro:~ wuxian$ docker push doctorq/infer:v1.1
The push refers to a repository [doctorq/infer] (len: 1)
334f8536c724: Image already exists 
79c86a401c9e: Image already exists 
b08982ff2ae7: Image already exists 
295452c2332e: Image already exists 
6f7ac6b3c22e: Image successfully pushed 
2f723dfaaf02: Image successfully pushed 
4f8c30699e9d: Image successfully pushed 
cd797a7e1a37: Image successfully pushed 
3ebbcb595d4f: Image successfully pushed 
2d901fe5706a: Image successfully pushed 
2ab20d71784e: Image already exists 
86f5a419baeb: Image successfully pushed 
87936850fe94: Image already exists 
6a34f2ecf1ae: Image already exists 
896c84d68ec8: Image successfully pushed 
f95fdfd19566: Image successfully pushed 
68f48e28c14b: Image successfully pushed 
918558524beb: Image successfully pushed 
e81d2d3af07f: Image successfully pushed 
569a964ec2cc: Image successfully pushed 
1885bd64f8f7: Image successfully pushed 
c00c3cf3b2b4: Image successfully pushed 
d473f52c6131: Image successfully pushed 
8d9b9414ed80: Image successfully pushed 
76a68924d522: Image already exists 
6be21d1e5d1e: Image already exists 
020431a88ee9: Image successfully pushed 
602e15acbcda: Image successfully pushed 
cab7c4dcf81b: Image successfully pushed 
Digest: sha256:ba58f6ad6d2725e2bc94bfc381217b2de7b477d4448eafd8366474aab873f609

成功截图

这里写图片描述

使用步骤

前提是你的 docker 环境已经配置完成

下载到本地

进入容器

docker run -it doctorq/infer:v1.0 /bin/bash

infer 源码位置

infer

root@f71395f22159:/# cd infer
root@f71395f22159:~/infer# ls -l
total 56
-rw-r--r-- 1 root root  235 Jul 13 04:11 CONTRIBUTING.md
-rw-r--r-- 1 root root 1206 Jul 13 04:11 FILES.md
-rw-r--r-- 1 root root 3795 Jul 13 04:11 INSTALL.md
-rw-r--r-- 1 root root 1501 Jul 13 04:11 LICENSE
-rw-r--r-- 1 root root 1982 Jul 13 04:11 PATENTS
-rw-r--r-- 1 root root  429 Jul 13 04:11 README.md
-rwxr-xr-x 1 root root 1787 Jul 13 04:11 compile-fcp.sh
drwxr-xr-x 4 root root 4096 Jul 13 04:11 dependencies
drwxr-xr-x 6 root root 4096 Jul 16 07:39 examples
drwxr-xr-x 9 root root 4096 Jul 13 04:19 infer
-rw-r--r-- 1 root root  304 Jul 13 04:11 infer.install
-rw-r--r-- 1 root root  545 Jul 13 04:11 opam
drwxr-xr-x 2 root root 4096 Jul 13 04:11 scripts
-rwxr-xr-x 1 root root  739 Jul 13 04:11 update-fcp.sh

分析

我们以源码中的例子来分析,首先进入 examples 目录。

root@f71395f22159:~/infer# cd examples/
root@f71395f22159:~/infer/examples# ls -l
total 40
-rw-r--r-- 1 root root  396 Jul 16 07:39 Hello.class
-rw-r--r-- 1 root root   79 Jul 13 04:11 Hello.java
-rw-r--r-- 1 root root  182 Jul 13 04:11 Hello.m
-rw-r--r-- 1 root root 1221 Jul 13 04:11 README
drwxr-xr-x 7 root root 4096 Jul 13 09:16 android_hello
drwxr-xr-x 3 root root 4096 Jul 13 07:58 c_hello
-rw-r--r-- 1 root root   65 Jul 13 04:11 hello.c
-rw-r--r-- 1 root root 1240 Jul 13 07:58 hello.o
drwxr-xr-x 8 root root 4096 Jul 16 07:39 infer-out
drwxr-xr-x 6 root root 4096 Jul 13 08:00 ios_hello

Java 文件

root@f71395f22159:~/infer/examples# infer -- javac Hello.java
Starting analysis (Infer version git-1356fd331f3db485be9ee38446a7389c9310d344)
Analysis done

1 file analyzed


/root/infer/examples/Hello.java:4: error: NULL_DEREFERENCE
  object s last assigned on line 3 could be null and is dereferenced at line 4

root@f71395f22159:~/infer/examples# 

Android 项目

root@f71395f22159:~/infer/examples/android_hello# gradle clean
:app:clean

BUILD SUCCESSFUL

Total time: 6.522 secs

This build could be faster, please consider using the Gradle Daemon: http://gradle.org/docs/2.5/userguide/gradle_daemon.html
root@f71395f22159:~/infer/examples/android_hello# infer -- gradle build
08:11:22.183 [ERROR] [org.gradle.api.Project] /root/infer/examples/android_hello/app/build/intermediates/exploded-aar/com.android.support/appcompat-v7/22.0.0/res/drawable-hdpi-v4/abc_spinner_mtrl_am_alpha.9.png: libpng warning: iCCP: Not recognizing known sRGB profile that has been edited
08:11:36.470 [ERROR] [org.gradle.api.Project] /root/infer/examples/android_hello/app/build/intermediates/exploded-aar/com.android.support/appcompat-v7/22.0.0/res/drawable-hdpi-v4/abc_spinner_mtrl_am_alpha.9.png: libpng warning: iCCP: Not recognizing known sRGB profile that has been edited
Starting analysis (Infer version git-1356fd331f3db485be9ee38446a7389c9310d344)
Analysis done

5 files analyzed


/root/infer/examples/android_hello/app/src/main/java/infer/inferandroidexample/MainActivity.java:20: error: NULL_DEREFERENCE
  object s last assigned on line 19 could be null and is dereferenced at line 20

/root/infer/examples/android_hello/app/src/main/java/infer/inferandroidexample/MainActivity.java:37: error: RESOURCE_LEAK
   resource acquired to fis by call to FileOutputStream(...) at line 34 is not released after line 37

root@f71395f22159:~/infer/examples/android_hello# 

C 文件

root@f71395f22159:~/infer/examples# infer -- gcc -c hello.c
Starting analysis (Infer version git-1356fd331f3db485be9ee38446a7389c9310d344)
Analysis done

1 file analyzed


hello.c:5: error: NULL_DEREFERENCE
  pointer s last assigned on line 4 could be null and is dereferenced at line 5, column 3

C 项目


root@f71395f22159:~/infer/examples/c_hello# make clean
rm -rf example.o
root@f71395f22159:~/infer/examples/c_hello# infer -- make
cc -c example.c
Starting analysis (Infer version git-1356fd331f3db485be9ee38446a7389c9310d344)
Analysis done

1 file analyzed


example.c:22: error: NULL_DEREFERENCE
  pointer max last assigned on line 21 could be null and is dereferenced at line 22, column 10

example.c:36: error: NULL_DEREFERENCE
  pointer joe last assigned on line 35 could be null and is dereferenced by call to get_age() at line 36, column 10

example.c:45: error: RESOURCE_LEAK
   resource acquired to fd by call to open() at line 41, column 12 is not released after line 45, column 5

example.c:51: error: MEMORY_LEAK
   memory dynamically allocated to p by call to malloc() at line 51, column 14 is not reachable after line 51, column 3

example.c:57: error: MEMORY_LEAK
   memory dynamically allocated to p by call to malloc() at line 56, column 14 is not reachable after line 57, column 3

root@f71395f22159:~/infer/examples/c_hello# 

Docker Hub

doctorq/infer


↙↙↙阅读原文可查看相关链接,并与作者交流