首页 > 解决方案 > Alpine Linux 上的 Valgrind

问题描述

尝试在 gitlab-ci 上设置自动内存泄漏测试时,我注意到 Valgrind 的 musl libc 存在一些问题。

.gitlab-ci.yml:

.alpine_cmake: &alpine_cmake
  image: alpine
  before_script:
    - apk update
    - apk add build-base gcc abuild binutils
    - apk add cmake extra-cmake-modules
    - apk add valgrind

make:
  stage: build
  <<: *alpine_cmake
  script:
    - ./configure
    - cd build
    - make all
  artifacts:
    expose_as: build
    paths: [build]

test_and_memcheck:
  stage: test
  <<: *alpine_cmake
  script:
    - cd build
    - make test_memcheck
    - '[ -s "Testing/Temporary/MemoryChecker.*.log" ]'

CMakeLists.txt:

cmake_minimum_required(VERSION 3.0.0)
project(TEST VERSION 1.0.0 LANGUAGES C)
set(CMAKE_C_STANDARD 99)

# TEST TARGET
include (CTest)
add_executable(test test.c)
add_test(TEST test)
add_custom_target(
  test_memcheck
  COMMAND ${CMAKE_CTEST_COMMAND} --force-new-ctest-process --test-action memcheck
  COMMAND cat "${CMAKE_BINARY_DIR}/Testing/Temporary/MemoryChecker.*.log"
)

结果:

$ make test_memcheck
-- Configuring done
-- Generating done
-- Build files have been written to: /builds/<PATH>/build
Scanning dependencies of target test_memcheck
   Site: runner-ed2dce3a-project-18678923-concurrent-0
   Build name: Linux-cc
Create new tag: 20200513-1037 - Experimental
Memory check project /builds/<PATH>/build
    Start 1: TEST
1/1 MemCheck #1: TEST ...........................   Passed    0.26 sec
100% tests passed, 0 tests failed out of 1
Total Test time (real) =   0.52 sec
-- Processing memory checking output:
1/1 MemCheck: #1: TEST .............................   Defects: 4
MemCheck log files can be found here: ( * corresponds to test number)
/builds/<PATH>/build/Testing/Temporary/MemoryChecker.*.log
Memory checking results:
Potential Memory Leak - 8
==39== 32 bytes in 3 blocks are still reachable in loss record 1 of 4
==39==    at 0x48A16F2: calloc (vg_replace_malloc.c:762)
==39==    by 0x4059049: ??? (in /lib/ld-musl-x86_64.so.1)
==39== 
==39== 40 bytes in 1 blocks are still reachable in loss record 2 of 4
==39==    at 0x48A16F2: calloc (vg_replace_malloc.c:762)
==39==    by 0x4059049: ??? (in /lib/ld-musl-x86_64.so.1)
==39==    by 0x74C8CD95484B3A94: ???
==39==    by 0x73646C6975622F58: ???
==39==    by 0x636E616D4450442E: ???
==39==    by 0x65676F72702F6C74: ???
==39==    by 0x6E6962616C6F7473: ???
==39==    by 0x6975622F666E6966: ???
==39==    by 0x2F747365742F646B: ???
==39==    by 0x745F726F74636575: ???
==39==    by 0x5F36387800747364: ???
==39== 
==39== 420 bytes in 1 blocks are still reachable in loss record 3 of 4
==39==    at 0x48A16F2: calloc (vg_replace_malloc.c:762)
==39==    by 0x4058E11: ??? (in /lib/ld-musl-x86_64.so.1)
==39==    by 0x4059A64: __dls3 (in /lib/ld-musl-x86_64.so.1)
==39==    by 0x74C8CD95484B3A94: ???
==39==    by 0x73646C6975622F58: ???
==39==    by 0x636E616D4450442E: ???
==39==    by 0x65676F72702F6C74: ???
==39==    by 0x6E6962616C6F7473: ???
==39==    by 0x6975622F666E6966: ???
==39==    by 0x2F747365742F646B: ???
==39==    by 0x745F726F74636575: ???
==39==    by 0x5F36387800747364: ???
==39== 
==39== 427 bytes in 1 blocks are still reachable in loss record 4 of 4
==39==    at 0x48A16F2: calloc (vg_replace_malloc.c:762)
==39==    by 0x4058E11: ??? (in /lib/ld-musl-x86_64.so.1)
==39==    by 0x40590D2: ??? (in /lib/ld-musl-x86_64.so.1)
==39== 
Built target test_memcheck
$ [ -s "Testing/Temporary/MemoryChecker.*.log" ]
Running after_script
00:01
Uploading artifacts for failed job
00:02
ERROR: Job failed: exit code 1

在我的机器上(带有 gcc 和 glibc 的 Manjaro)它不会标记任何内存泄漏。

我可以改用 Debian,但是 Alpine 非常有用,因为它可以快速加载其 docker 映像,所以我正在寻找是否有办法在 Alpine 上使用 Valgrind。

== 更新 ==
我用一个例子创建了一个仓库:https ://gitlab.com/DPDmancul/valgrindtest

标签: linuxgitlab-civalgrindalpinemusl

解决方案


推荐阅读