test_r_package_valgrind.sh 3.22 KB
Newer Older
1
2
#!/bin/bash

3
4
set -e -E -u -o pipefail

5
RDscriptvalgrind -e "install.packages(c('R6', 'data.table', 'jsonlite', 'Matrix', 'RhpcBLASctl', 'testthat'), repos = 'https://cran.rstudio.com')" || exit 1
6
7
sh build-cran-package.sh \
  --r-executable=RDvalgrind \
8
  --no-build-vignettes \
9
  || exit 1
10

11
RDvalgrind CMD INSTALL --preclean --install-tests lightgbm_*.tar.gz || exit 1
12

13
14
15
16
17
18
19
20
21
22
cd R-package/tests

ALL_LOGS_FILE="out.log"
VALGRIND_LOGS_FILE="valgrind-logs.log"

RDvalgrind \
  --no-readline \
  --vanilla \
  -d "valgrind --tool=memcheck --leak-check=full --track-origins=yes" \
  -f testthat.R \
23
  > ${ALL_LOGS_FILE} 2>&1 || exit 1
24
25
26

cat ${ALL_LOGS_FILE}

27
echo "writing valgrind output to ${VALGRIND_LOGS_FILE}"
28
29
30
31
32
33
34
35
cat ${ALL_LOGS_FILE} | grep -E "^\=" > ${VALGRIND_LOGS_FILE}

bytes_definitely_lost=$(
  cat ${VALGRIND_LOGS_FILE} \
      | grep -E "definitely lost\: .*" \
      | sed 's/^.*definitely lost\: \(.*\) bytes.*$/\1/' \
      | tr -d ","
)
36
echo "valgrind found ${bytes_definitely_lost} bytes definitely lost"
37
if [[ ${bytes_definitely_lost} -gt 0 ]]; then
38
    exit 1
39
40
41
42
43
44
45
46
fi

bytes_indirectly_lost=$(
    cat ${VALGRIND_LOGS_FILE} \
    | grep -E "indirectly lost\: .*" \
    | sed 's/^.*indirectly lost\: \(.*\) bytes.*$/\1/' \
    | tr -d ","
)
47
echo "valgrind found ${bytes_indirectly_lost} bytes indirectly lost"
48
if [[ ${bytes_indirectly_lost} -gt 0 ]]; then
49
    exit 1
50
51
52
fi

# one error caused by a false positive between valgrind and openmp is allowed
53
# ==2063== 352 bytes in 1 blocks are possibly lost in loss record 153 of 2,709
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# ==2063==    at 0x483DD99: calloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
# ==2063==    by 0x40149CA: allocate_dtv (dl-tls.c:286)
# ==2063==    by 0x40149CA: _dl_allocate_tls (dl-tls.c:532)
# ==2063==    by 0x5702322: allocate_stack (allocatestack.c:622)
# ==2063==    by 0x5702322: pthread_create@@GLIBC_2.2.5 (pthread_create.c:660)
# ==2063==    by 0x56D0DDA: ??? (in /usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0)
# ==2063==    by 0x56C88E0: GOMP_parallel (in /usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0)
# ==2063==    by 0x1544D29C: LGBM_DatasetCreateFromCSC (c_api.cpp:1286)
# ==2063==    by 0x1546F980: LGBM_DatasetCreateFromCSC_R (lightgbm_R.cpp:91)
# ==2063==    by 0x4941E2F: R_doDotCall (dotcode.c:634)
# ==2063==    by 0x494CCC6: do_dotcall (dotcode.c:1281)
# ==2063==    by 0x499FB01: bcEval (eval.c:7078)
# ==2063==    by 0x498B67F: Rf_eval (eval.c:727)
# ==2063==    by 0x498E414: R_execClosure (eval.c:1895)
68
69
70
71
72
73
bytes_possibly_lost=$(
    cat ${VALGRIND_LOGS_FILE} \
    | grep -E "possibly lost\: .*" \
    | sed 's/^.*possibly lost\: \(.*\) bytes.*$/\1/' \
    | tr -d ","
)
74
echo "valgrind found ${bytes_possibly_lost} bytes possibly lost"
75
if [[ ${bytes_possibly_lost} -gt 1104 ]]; then
76
    exit 1
77
fi
78

79
80
81
82
# ensure 'grep --count' doesn't cause failures
set +e

echo "checking for invalid reads"
83
84
85
86
87
88
invalid_reads=$(
  cat ${VALGRIND_LOGS_FILE} \
    | grep --count -i "Invalid read"
)
if [[ ${invalid_reads} -gt 0 ]]; then
    echo "valgrind found invalid reads: ${invalid_reads}"
89
    exit 1
90
91
fi

92
echo "checking for invalid writes"
93
94
95
96
97
98
invalid_writes=$(
  cat ${VALGRIND_LOGS_FILE} \
    | grep --count -i "Invalid write"
)
if [[ ${invalid_writes} -gt 0 ]]; then
    echo "valgrind found invalid writes: ${invalid_writes}"
99
    exit 1
100
fi