Commit d827b11b670f3331e5d924eadfe35c497b08035a

Patrick Steinhardt 2019-06-28T13:20:54

tests: execute leak checker via CTest directly Right now, we have an awful hack in our test CI setup that extracts the test command from CTest's output and then prepends the leak checker. This is dependent on non-machine-parseable output from CMake and also breaks on various ocassions, like for example when we have spaces in the current path or when the path contains backslashes. Both conditions may easily be triggered on Win32 systems, and in fact they do break our Azure Pipelines builds. Remove the awful hack in favour of a new CMake build option "USE_LEAK_CHECKER". If specifying e.g. "-DUSE_LEAK_CHECKER=valgrind", then we will set up all tests to be run under valgrind. Like this, we can again simply execute ctest without needing to rely on evil sourcery.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 292fadd..6e6443a 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -57,6 +57,7 @@ OPTION(USE_HTTPS			"Enable HTTPS support. Can be set to a specific backend" ON)
 OPTION(USE_SHA1				"Enable SHA1. Can be set to CollisionDetection(ON)/HTTPS/Generic" ON)
 OPTION(USE_GSSAPI			"Link with libgssapi for SPNEGO auth"			OFF)
 OPTION(USE_STANDALONE_FUZZERS		"Enable standalone fuzzers (compatible with gcc)"	OFF)
+OPTION(USE_LEAK_CHECKER			"Run tests with leak checker"				OFF)
 OPTION(VALGRIND				"Configure build for valgrind"				OFF)
 OPTION(DEBUG_POOL			"Enable debug pool allocator"				OFF)
 OPTION(ENABLE_WERROR			"Enable compilation with -Werror"			OFF)
diff --git a/azure-pipelines.yml b/azure-pipelines.yml
index 959960f..0f92df3 100644
--- a/azure-pipelines.yml
+++ b/azure-pipelines.yml
@@ -16,8 +16,7 @@ jobs:
       imageName: 'libgit2/trusty-amd64:latest'
       environmentVariables: |
        CC=gcc
-       CMAKE_OPTIONS=-DUSE_HTTPS=OpenSSL -DREGEX_BACKEND=builtin -DDEPRECATE_HARD=ON
-       LEAK_CHECK=valgrind
+       CMAKE_OPTIONS=-DUSE_HTTPS=OpenSSL -DREGEX_BACKEND=builtin -DDEPRECATE_HARD=ON -DUSE_LEAK_CHECKER=valgrind
 
 - job: linux_amd64_trusty_gcc_mbedtls
   displayName: 'Linux (amd64; Trusty; GCC; mbedTLS)'
@@ -29,8 +28,7 @@ jobs:
       imageName: 'libgit2/trusty-amd64:latest'
       environmentVariables: |
        CC=gcc
-       CMAKE_OPTIONS=-DUSE_HTTPS=mbedTLS -DUSE_SHA1=HTTPS -DDEPRECATE_HARD=ON
-       LEAK_CHECK=valgrind
+       CMAKE_OPTIONS=-DUSE_HTTPS=mbedTLS -DUSE_SHA1=HTTPS -DDEPRECATE_HARD=ON -DUSE_LEAK_CHECKER=valgrind
 
 - job: linux_amd64_trusty_clang_openssl
   displayName: 'Linux (amd64; Trusty; Clang; OpenSSL)'
@@ -42,8 +40,7 @@ jobs:
       imageName: 'libgit2/trusty-amd64:latest'
       environmentVariables: |
        CC=clang
-       CMAKE_OPTIONS=-DUSE_HTTPS=OpenSSL -DDEPRECATE_HARD=ON
-       LEAK_CHECK=valgrind
+       CMAKE_OPTIONS=-DUSE_HTTPS=OpenSSL -DDEPRECATE_HARD=ON -DUSE_LEAK_CHECKER=valgrind
 
 - job: linux_amd64_trusty_clang_mbedtls
   displayName: 'Linux (amd64; Trusty; Clang; mbedTLS)'
@@ -55,8 +52,7 @@ jobs:
       imageName: 'libgit2/trusty-amd64:latest'
       environmentVariables: |
        CC=clang
-       CMAKE_OPTIONS=-DUSE_HTTPS=mbedTLS -DUSE_SHA1=HTTPS -DREGEX_BACKEND=pcre -DDEPRECATE_HARD=ON
-       LEAK_CHECK=valgrind
+       CMAKE_OPTIONS=-DUSE_HTTPS=mbedTLS -DUSE_SHA1=HTTPS -DREGEX_BACKEND=pcre -DDEPRECATE_HARD=ON -DUSE_LEAK_CHECKER=valgrind
 
 - job: macos
   displayName: 'macOS'
@@ -70,8 +66,7 @@ jobs:
       environmentVariables:
         TMPDIR: $(Agent.TempDirectory)
         PKG_CONFIG_PATH: /usr/local/opt/openssl/lib/pkgconfig
-        LEAK_CHECK: leaks
-        CMAKE_OPTIONS: -G Ninja -DREGEX_BACKEND=regcomp_l -DDEPRECATE_HARD=ON
+        CMAKE_OPTIONS: -G Ninja -DREGEX_BACKEND=regcomp_l -DDEPRECATE_HARD=ON -DUSE_LEAK_CHECKER=leaks
         SKIP_SSH_TESTS: true
 
 - job: windows_vs_amd64
diff --git a/azure-pipelines/nightly.yml b/azure-pipelines/nightly.yml
index c299716..a79e926 100644
--- a/azure-pipelines/nightly.yml
+++ b/azure-pipelines/nightly.yml
@@ -12,8 +12,7 @@ jobs:
       imageName: 'libgit2/trusty-amd64:latest'
       environmentVariables: |
        CC=gcc
-       CMAKE_OPTIONS=-DUSE_HTTPS=OpenSSL -DREGEX_BACKEND=builtin -DDEPRECATE_HARD=ON
-       LEAK_CHECK=valgrind
+       CMAKE_OPTIONS=-DUSE_HTTPS=OpenSSL -DREGEX_BACKEND=builtin -DDEPRECATE_HARD=ON -DUSE_LEAK_CHECKER=valgrind
        RUN_INVASIVE_TESTS=true
 
 - job: linux_amd64_trusty_gcc_mbedtls
@@ -26,8 +25,7 @@ jobs:
       imageName: 'libgit2/trusty-amd64:latest'
       environmentVariables: |
        CC=gcc
-       CMAKE_OPTIONS=-DUSE_HTTPS=mbedTLS -DUSE_SHA1=HTTPS -DDEPRECATE_HARD=ON
-       LEAK_CHECK=valgrind
+       CMAKE_OPTIONS=-DUSE_HTTPS=mbedTLS -DUSE_SHA1=HTTPS -DDEPRECATE_HARD=ON -DUSE_LEAK_CHECKER=valgrind
        RUN_INVASIVE_TESTS=true
 
 - job: linux_amd64_trusty_clang_openssl
@@ -40,8 +38,7 @@ jobs:
       imageName: 'libgit2/trusty-amd64:latest'
       environmentVariables: |
        CC=clang
-       CMAKE_OPTIONS=-DUSE_HTTPS=OpenSSL -DDEPRECATE_HARD=ON
-       LEAK_CHECK=valgrind
+       CMAKE_OPTIONS=-DUSE_HTTPS=OpenSSL -DDEPRECATE_HARD=ON -DUSE_LEAK_CHECKER=valgrind
        RUN_INVASIVE_TESTS=true
 
 - job: linux_amd64_trusty_clang_mbedtls
@@ -54,8 +51,7 @@ jobs:
       imageName: 'libgit2/trusty-amd64:latest'
       environmentVariables: |
        CC=clang
-       CMAKE_OPTIONS=-DUSE_HTTPS=mbedTLS -DUSE_SHA1=HTTPS -DREGEX_BACKEND=pcre -DDEPRECATE_HARD=ON
-       LEAK_CHECK=valgrind
+       CMAKE_OPTIONS=-DUSE_HTTPS=mbedTLS -DUSE_SHA1=HTTPS -DREGEX_BACKEND=pcre -DDEPRECATE_HARD=ON -DUSE_LEAK_CHECKER=valgrind
        RUN_INVASIVE_TESTS=true
 
 - job: macos
@@ -70,8 +66,7 @@ jobs:
       environmentVariables:
         TMPDIR: $(Agent.TempDirectory)
         PKG_CONFIG_PATH: /usr/local/opt/openssl/lib/pkgconfig
-        LEAK_CHECK: leaks
-        CMAKE_OPTIONS: -G Ninja -DREGEX_BACKEND=regcomp_l -DDEPRECATE_HARD=ON
+        CMAKE_OPTIONS: -G Ninja -DREGEX_BACKEND=regcomp_l -DDEPRECATE_HARD=ON -DUSE_LEAK_CHECKER=leaks
         RUN_INVASIVE_TESTS: true
         SKIP_SSH_TESTS: true
 
@@ -139,8 +134,7 @@ jobs:
       imageName: 'libgit2/bionic-x86:latest'
       environmentVariables: |
        CC=gcc
-       CMAKE_OPTIONS=-DUSE_HTTPS=OpenSSL -DDEPRECATE_HARD=ON
-       LEAK_CHECK=valgrind
+       CMAKE_OPTIONS=-DUSE_HTTPS=OpenSSL -DDEPRECATE_HARD=ON -DUSE_LEAK_CHECKER=valgrind
        RUN_INVASIVE_TESTS=true
 
 - job: linux_x86_bionic_clang_openssl
@@ -154,8 +148,7 @@ jobs:
       imageName: 'libgit2/bionic-x86:latest'
       environmentVariables: |
        CC=clang
-       CMAKE_OPTIONS=-DUSE_HTTPS=OpenSSL -DDEPRECATE_HARD=ON
-       LEAK_CHECK=valgrind
+       CMAKE_OPTIONS=-DUSE_HTTPS=OpenSSL -DDEPRECATE_HARD=ON -DUSE_LEAK_CHECKER=valgrind
        RUN_INVASIVE_TESTS=true
 
 - job: linux_arm32_bionic_gcc_openssl
diff --git a/ci/test.sh b/ci/test.sh
index cbc3925..69b4273 100755
--- a/ci/test.sh
+++ b/ci/test.sh
@@ -13,9 +13,6 @@ USER=${USER:-$(whoami)}
 
 SUCCESS=1
 
-VALGRIND="valgrind --leak-check=full --show-reachable=yes --error-exitcode=125 --num-callers=50 --suppressions=\"$SOURCE_DIR/libgit2_clar.supp\""
-LEAKS="MallocStackLogging=1 MallocScribble=1 MallocLogFile=/dev/null CLAR_AT_EXIT=\"leaks -quiet \$PPID\""
-
 cleanup() {
 	echo "Cleaning up..."
 
@@ -32,29 +29,7 @@ cleanup() {
 	echo "Done."
 }
 
-# Ask ctest what it would run if we were to invoke it directly.  This lets
-# us manage the test configuration in a single place (tests/CMakeLists.txt)
-# instead of running clar here as well.  But it allows us to wrap our test
-# harness with a leak checker like valgrind.  Append the option to write
-# JUnit-style XML files.
 run_test() {
-	TEST_CMD=$(ctest -N -V -R "^${1}$" | sed -n 's/^[0-9]*: Test command: //p')
-
-	if [ -z "$TEST_CMD" ]; then
-		echo "Could not find tests: $1"
-		exit 1
-	fi
-
-	TEST_CMD="${TEST_CMD} -r${BUILD_DIR}/results_${1}.xml"
-
-	if [ "$LEAK_CHECK" = "valgrind" ]; then
-		RUNNER="$VALGRIND $TEST_CMD"
-	elif [ "$LEAK_CHECK" = "leaks" ]; then
-		RUNNER="$LEAKS $TEST_CMD"
-	else
-		RUNNER="$TEST_CMD"
-	fi
-
 	if [[ "$GITTEST_FLAKY_RETRY" > 0 ]]; then
 		ATTEMPTS_REMAIN=$GITTEST_FLAKY_RETRY
 	else
@@ -70,7 +45,8 @@ run_test() {
 		fi
 
 		RETURN_CODE=0
-		eval $RUNNER || RETURN_CODE=$? && true
+
+		CLAR_SUMMARY="${BUILD_DIR}/results_${1}.xml" ctest -V -R "^${1}$" || RETURN_CODE=$? && true
 
 		if [ "$RETURN_CODE" -eq 0 ]; then
 			break
diff --git a/libgit2_clar.supp b/libgit2_clar.supp
deleted file mode 100644
index b747919..0000000
--- a/libgit2_clar.supp
+++ /dev/null
@@ -1,114 +0,0 @@
-{
-	ignore-zlib-errors-cond
-	Memcheck:Cond
-	obj:*libz.so*
-}
-
-{
-	ignore-giterror-set-leak
-	Memcheck:Leak
-	...
-	fun:giterror_set
-}
-
-{
-	ignore-git-global-state-leak
-	Memcheck:Leak
-	...
-	fun:git__global_state
-}
-
-{
-	ignore-openssl-ssl-leak
-	Memcheck:Leak
-	...
-	obj:*libssl.so*
-	...
-}
-
-{
-	ignore-openssl-crypto-leak
-	Memcheck:Leak
-	...
-	obj:*libcrypto.so*
-	...
-}
-
-{
-	ignore-openssl-crypto-cond
-	Memcheck:Cond
-	obj:*libcrypto.so*
-	...
-}
-
-{
-	ignore-glibc-getaddrinfo-cache
-	Memcheck:Leak
-	...
-	fun:__check_pf
-}
-
-{
-	ignore-curl-global-init
-	Memcheck:Leak
-	...
-	fun:curl_global_init
-}
-
-{
-	ignore-libssh2-gcrypt-control-leak
-	Memcheck:Leak
-	...
-	fun:gcry_control
-	obj:*libssh2.so*
-}
-
-{
-	ignore-libssh2-gcrypt-mpinew-leak
-	Memcheck:Leak
-	...
-	fun:gcry_mpi_new
-	obj:*libssh2.so*
-}
-
-{
-	ignore-libssh2-gcrypt-mpiscan-leak
-	Memcheck:Leak
-	...
-	fun:gcry_mpi_scan
-	obj:*libssh2.so*
-}
-
-{
-	ignore-libssh2-gcrypt-randomize-leak
-	Memcheck:Leak
-	...
-	fun:gcry_randomize
-	obj:*libssh2.so*
-}
-
-{
-	ignore-libssh2-gcrypt-sexpfindtoken-leak
-	Memcheck:Leak
-	...
-	fun:gcry_sexp_find_token
-	obj:*libssh2.so*
-}
-
-{
-	ignore-libssh2-gcrypt-pksign-leak
-	Memcheck:Leak
-	...
-	fun:gcry_pk_sign
-	obj:*libssh2.so*
-}
-
-{
-	ignore-noai6ai_cached-double-free
-	Memcheck:Free
-	fun:free
-	fun:__libc_freeres
-	...
-	fun:exit
-	...
-}
diff --git a/script/leaks.sh b/script/leaks.sh
new file mode 100755
index 0000000..efeead5
--- /dev/null
+++ b/script/leaks.sh
@@ -0,0 +1,6 @@
+#!/bin/sh
+export MallocStackLogging=1
+export MallocScribble=1
+export MallocLogFile=/dev/null
+export CLAR_AT_EXIT="leaks -quiet \$PPID"
+exec "$@"
diff --git a/script/valgrind.sh b/script/valgrind.sh
new file mode 100755
index 0000000..b5deed2
--- /dev/null
+++ b/script/valgrind.sh
@@ -0,0 +1,2 @@
+#!/bin/bash
+exec valgrind --leak-check=full --show-reachable=yes --error-exitcode=125 --num-callers=50 --suppressions="$(dirname "${BASH_SOURCE[0]}")/valgrind.supp" "$@"
diff --git a/script/valgrind.supp b/script/valgrind.supp
new file mode 100644
index 0000000..b747919
--- /dev/null
+++ b/script/valgrind.supp
@@ -0,0 +1,114 @@
+{
+	ignore-zlib-errors-cond
+	Memcheck:Cond
+	obj:*libz.so*
+}
+
+{
+	ignore-giterror-set-leak
+	Memcheck:Leak
+	...
+	fun:giterror_set
+}
+
+{
+	ignore-git-global-state-leak
+	Memcheck:Leak
+	...
+	fun:git__global_state
+}
+
+{
+	ignore-openssl-ssl-leak
+	Memcheck:Leak
+	...
+	obj:*libssl.so*
+	...
+}
+
+{
+	ignore-openssl-crypto-leak
+	Memcheck:Leak
+	...
+	obj:*libcrypto.so*
+	...
+}
+
+{
+	ignore-openssl-crypto-cond
+	Memcheck:Cond
+	obj:*libcrypto.so*
+	...
+}
+
+{
+	ignore-glibc-getaddrinfo-cache
+	Memcheck:Leak
+	...
+	fun:__check_pf
+}
+
+{
+	ignore-curl-global-init
+	Memcheck:Leak
+	...
+	fun:curl_global_init
+}
+
+{
+	ignore-libssh2-gcrypt-control-leak
+	Memcheck:Leak
+	...
+	fun:gcry_control
+	obj:*libssh2.so*
+}
+
+{
+	ignore-libssh2-gcrypt-mpinew-leak
+	Memcheck:Leak
+	...
+	fun:gcry_mpi_new
+	obj:*libssh2.so*
+}
+
+{
+	ignore-libssh2-gcrypt-mpiscan-leak
+	Memcheck:Leak
+	...
+	fun:gcry_mpi_scan
+	obj:*libssh2.so*
+}
+
+{
+	ignore-libssh2-gcrypt-randomize-leak
+	Memcheck:Leak
+	...
+	fun:gcry_randomize
+	obj:*libssh2.so*
+}
+
+{
+	ignore-libssh2-gcrypt-sexpfindtoken-leak
+	Memcheck:Leak
+	...
+	fun:gcry_sexp_find_token
+	obj:*libssh2.so*
+}
+
+{
+	ignore-libssh2-gcrypt-pksign-leak
+	Memcheck:Leak
+	...
+	fun:gcry_pk_sign
+	obj:*libssh2.so*
+}
+
+{
+	ignore-noai6ai_cached-double-free
+	Memcheck:Free
+	fun:free
+	fun:__libc_freeres
+	...
+	fun:exit
+	...
+}
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index e39fd6f..a97e8fe 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -57,9 +57,17 @@ IF (MSVC_IDE)
 	SET_SOURCE_FILES_PROPERTIES("precompiled.c" COMPILE_FLAGS "/Ycprecompiled.h")
 ENDIF ()
 
-ADD_TEST(offline   "${libgit2_BINARY_DIR}/libgit2_clar" -v -xonline)
-ADD_TEST(invasive  "${libgit2_BINARY_DIR}/libgit2_clar" -v -score::ftruncate -sfilter::stream::bigfile -sodb::largefiles -siterator::workdir::filesystem_gunk -srepo::init -srepo::init::at_filesystem_root)
-ADD_TEST(online    "${libgit2_BINARY_DIR}/libgit2_clar" -v -sonline)
-ADD_TEST(gitdaemon "${libgit2_BINARY_DIR}/libgit2_clar" -v -sonline::push)
-ADD_TEST(ssh       "${libgit2_BINARY_DIR}/libgit2_clar" -v -sonline::push -sonline::clone::ssh_cert -sonline::clone::ssh_with_paths)
-ADD_TEST(proxy     "${libgit2_BINARY_DIR}/libgit2_clar" -v -sonline::clone::proxy)
+FUNCTION(ADD_CLAR_TEST name)
+	IF (NOT USE_LEAK_CHECKER STREQUAL "OFF")
+		ADD_TEST(${name} "${libgit2_SOURCE_DIR}/script/${USE_LEAK_CHECKER}.sh" "${libgit2_BINARY_DIR}/libgit2_clar" ${ARGN})
+	ELSE()
+		ADD_TEST(${name} "${libgit2_BINARY_DIR}/libgit2_clar" ${ARGN})
+	ENDIF()
+ENDFUNCTION(ADD_CLAR_TEST)
+
+ADD_CLAR_TEST(offline   -v -xonline)
+ADD_CLAR_TEST(invasive  -v -score::ftruncate -sfilter::stream::bigfile -sodb::largefiles -siterator::workdir::filesystem_gunk -srepo::init -srepo::init::at_filesystem_root)
+ADD_CLAR_TEST(online    -v -sonline)
+ADD_CLAR_TEST(gitdaemon -v -sonline::push)
+ADD_CLAR_TEST(ssh       -v -sonline::push -sonline::clone::ssh_cert -sonline::clone::ssh_with_paths)
+ADD_CLAR_TEST(proxy     -v -sonline::clone::proxy)