1 Star 0 Fork 0

liqinglucky/DockerHelloWorld

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

README

Prerequites

  1. Install Docker
  2. Clone this repo
  3. cd into the working directory

How to Run Docker Image

  1. docker build -t hello:v1 .
  2. docker run hello:v1

版本日志

新功能

  1. docker里测试Address Sanitizer

docker中测试Address Sanitizer

Docker只是提供了一个运行环境,Docker里的程序集成Address Sanitizer与Linux环境编译相比并不需要做任何额外改动。

一、代码

在编译程序时加上编译参数-fsanitize=address

RUN g++ -fsanitize=address -g -o HelloWorld HelloWorld.cpp

HelloWorld.cpp加上测试代码

int a1[10] = {0};
std::cout << a1[11];

二、编译

DockerHelloWorld# docker build -f ./Dockerfile -t hello:v1 .

Sending build context to Docker daemon  84.99kB
Step 1/5 : FROM gcc:4.9
 ---> 1b3de68a7ff8
Step 2/5 : COPY . /HelloWorld
 ---> f366acaf4880
Step 3/5 : WORKDIR /HelloWorld
 ---> Running in 70f174f6c268
Removing intermediate container 70f174f6c268
 ---> 14de1dc480b4
Step 4/5 : RUN g++ -fsanitize=address -g -o HelloWorld HelloWorld.cpp
 ---> Running in 46a451258fa1
Removing intermediate container 46a451258fa1
 ---> c363c617f6e2
Step 5/5 : CMD ["./HelloWorld"]
 ---> Running in d2b6baf583b0
Removing intermediate container d2b6baf583b0
 ---> d5e6979e98e1
Successfully built d5e6979e98e1
Successfully tagged hello:v1

三、测试

# docker run hello:v1
=================================================================
==1==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7fff655d1f3c at pc 0x400cae bp 0x7fff655d1ed0 sp 0x7fff655d1ec8
READ of size 4 at 0x7fff655d1f3c thread T0
    #0 0x400cad in main /HelloWorld/HelloWorld.cpp:12
    #1 0x7fb055e0cb44 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b44)
    #2 0x400ac8 (/HelloWorld/HelloWorld+0x400ac8)

Address 0x7fff655d1f3c is located in stack of thread T0 at offset 76 in frame
    #0 0x400ba5 in main /HelloWorld/HelloWorld.cpp:4

  This frame has 1 object(s):
    [32, 72) 'a1' <== Memory access at offset 76 overflows this variable
HINT: this may be a false positive if your program uses some custom stack unwind mechanism or swapcontext
      (longjmp and C++ exceptions *are* supported)
SUMMARY: AddressSanitizer: stack-buffer-overflow /HelloWorld/HelloWorld.cpp:12 main
Shadow bytes around the buggy address:
  0x10006cab2390: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10006cab23a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10006cab23b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10006cab23c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10006cab23d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 f1 f1
=>0x10006cab23e0: f1 f1 00 00 00 00 00[f4]f4 f4 f3 f3 f3 f3 00 00
  0x10006cab23f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10006cab2400: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10006cab2410: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10006cab2420: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10006cab2430: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07
  Heap left redzone:       fa
  Heap right redzone:      fb
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack partial redzone:   f4
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Contiguous container OOB:fc
  ASan internal:           fe
==1==ABORTING

可以看到Address Sanitizer的Log就说明Docker里程序集成Address Sanitizer编译成功!

空文件

简介

Dockerfile that compiles C++ into a Hello, world! 极简编译一个docker,启动docker后跑一个hello world程序。 展开 收起
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/liqinglucky/DockerHelloWorld.git
git@gitee.com:liqinglucky/DockerHelloWorld.git
liqinglucky
DockerHelloWorld
DockerHelloWorld
mydev

搜索帮助