Hex Artifact Content

Artifact a80110599b855d62832856ab2c8d68d659819296:


0000: 23 21 20 2f 62 69 6e 2f 62 61 73 68 0a 0a 4c 45  #! /bin/bash..LE
0010: 41 4b 43 48 45 43 4b 46 49 4c 45 3d 22 24 31 22  AKCHECKFILE="$1"
0020: 0a 54 4d 50 46 49 4c 45 3d 22 24 7b 54 4d 50 44  .TMPFILE="${TMPD
0030: 49 52 3a 2d 2f 74 6d 70 7d 2f 6d 61 6c 6c 6f 63  IR:-/tmp}/malloc
0040: 2d 66 72 65 65 2d 63 68 65 63 6b 2d 24 24 24 7b  -free-check-$$${
0050: 52 41 4e 44 4f 4d 7d 24 7b 52 41 4e 44 4f 4d 7d  RANDOM}${RANDOM}
0060: 24 7b 52 41 4e 44 4f 4d 7d 2e 74 6d 70 22 0a 65  ${RANDOM}.tmp".e
0070: 78 70 6f 72 74 20 4c 45 41 4b 43 48 45 43 4b 46  xport LEAKCHECKF
0080: 49 4c 45 20 54 4d 50 46 49 4c 45 0a 0a 69 66 20  ILE TMPFILE..if 
0090: 5b 20 2d 7a 20 22 24 7b 4c 45 41 4b 43 48 45 43  [ -z "${LEAKCHEC
00a0: 4b 46 49 4c 45 7d 22 20 5d 3b 20 74 68 65 6e 0a  KFILE}" ]; then.
00b0: 09 65 63 68 6f 20 22 55 73 61 67 65 3a 20 6c 65  .echo "Usage: le
00c0: 61 6b 63 68 65 63 6b 20 3c 66 69 6c 65 6e 61 6d  akcheck <filenam
00d0: 65 3e 22 20 3e 26 32 0a 09 65 63 68 6f 20 22 20  e>" >&2..echo " 
00e0: 20 20 20 20 20 20 20 20 66 69 6c 65 6e 61 6d 65          filename
00f0: 20 20 20 20 20 20 20 4e 61 6d 65 20 6f 66 20 66         Name of f
0100: 69 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 64  ile containing d
0110: 65 62 75 67 67 69 6e 67 20 6f 75 74 70 75 74 22  ebugging output"
0120: 20 3e 26 32 0a 0a 09 65 78 69 74 20 31 0a 66 69   >&2...exit 1.fi
0130: 0a 0a 65 67 72 65 70 20 27 28 4d 41 4c 4c 4f 43  ..egrep '(MALLOC
0140: 7c 46 52 45 45 7c 52 45 41 4c 4c 4f 43 29 27 20  |FREE|REALLOC)' 
0150: 22 24 7b 4c 45 41 4b 43 48 45 43 4b 46 49 4c 45  "${LEAKCHECKFILE
0160: 7d 22 20 7c 20 73 65 64 20 27 73 40 5e 2e 2a 46  }" | sed 's@^.*F
0170: 52 45 45 28 5c 28 30 78 5b 30 2d 39 61 2d 66 5d  REE(\(0x[0-9a-f]
0180: 2a 5c 29 29 2e 2a 24 40 66 72 65 65 20 5c 31 40  *\)).*$@free \1@
0190: 3b 73 40 5e 2e 2a 4d 41 4c 4c 4f 43 28 29 20 3d  ;s@^.*MALLOC() =
01a0: 20 40 6d 61 6c 6c 6f 63 20 40 3b 73 40 5e 2e 2a   @malloc @;s@^.*
01b0: 52 45 41 4c 4c 4f 43 28 5c 28 30 78 5b 30 2d 39  REALLOC(\(0x[0-9
01c0: 61 2d 66 5d 2a 5c 29 29 20 3d 20 40 72 65 61 6c  a-f]*\)) = @real
01d0: 6c 6f 63 20 5c 31 20 40 27 20 3e 20 22 24 7b 54  loc \1 @' > "${T
01e0: 4d 50 46 49 4c 45 7d 22 0a 0a 63 61 74 20 22 24  MPFILE}"..cat "$
01f0: 7b 54 4d 50 46 49 4c 45 7d 22 20 7c 20 77 68 69  {TMPFILE}" | whi
0200: 6c 65 20 72 65 61 64 20 6f 70 20 61 64 64 72 20  le read op addr 
0210: 6e 65 77 61 64 64 72 3b 20 64 6f 0a 09 63 61 73  newaddr; do..cas
0220: 65 20 22 24 7b 6f 70 7d 22 20 69 6e 0a 09 09 6d  e "${op}" in...m
0230: 61 6c 6c 6f 63 29 0a 09 09 09 69 66 20 5b 20 2d  alloc)....if [ -
0240: 7a 20 22 24 7b 61 6c 6c 6f 63 6c 69 73 74 7d 22  z "${alloclist}"
0250: 20 5d 3b 20 74 68 65 6e 0a 09 09 09 09 61 6c 6c   ]; then.....all
0260: 6f 63 6c 69 73 74 3d 22 24 7b 61 64 64 72 7d 22  oclist="${addr}"
0270: 0a 09 09 09 65 6c 73 65 0a 09 09 09 09 61 6c 6c  ....else.....all
0280: 6f 63 6c 69 73 74 3d 22 24 7b 61 6c 6c 6f 63 6c  oclist="${allocl
0290: 69 73 74 7d 20 24 7b 61 64 64 72 7d 22 0a 09 09  ist} ${addr}"...
02a0: 09 66 69 0a 09 09 09 3b 3b 0a 09 09 66 72 65 65  .fi....;;...free
02b0: 29 0a 09 09 09 69 66 20 21 20 65 63 68 6f 20 22  )....if ! echo "
02c0: 20 24 7b 61 6c 6c 6f 63 6c 69 73 74 7d 20 22 20   ${alloclist} " 
02d0: 7c 20 67 72 65 70 20 22 20 24 7b 61 64 64 72 7d  | grep " ${addr}
02e0: 20 22 20 3e 2f 64 65 76 2f 6e 75 6c 6c 3b 20 74   " >/dev/null; t
02f0: 68 65 6e 0a 09 09 09 09 69 66 20 5b 20 2d 7a 20  hen.....if [ -z 
0300: 22 24 7b 61 6c 6c 6f 63 6c 69 73 74 7d 22 20 5d  "${alloclist}" ]
0310: 3b 20 74 68 65 6e 0a 09 09 09 09 09 61 6c 6c 6f  ; then......allo
0320: 63 6c 69 73 74 3d 22 21 24 7b 61 64 64 72 7d 22  clist="!${addr}"
0330: 0a 09 09 09 09 65 6c 73 65 0a 09 09 09 09 09 61  .....else......a
0340: 6c 6c 6f 63 6c 69 73 74 3d 22 24 7b 61 6c 6c 6f  lloclist="${allo
0350: 63 6c 69 73 74 7d 20 21 24 7b 61 64 64 72 7d 22  clist} !${addr}"
0360: 0a 09 09 09 09 66 69 0a 09 09 09 65 6c 73 65 0a  .....fi....else.
0370: 09 09 09 09 61 6c 6c 6f 63 6c 69 73 74 3d 22 24  ....alloclist="$
0380: 28 65 63 68 6f 20 22 20 24 7b 61 6c 6c 6f 63 6c  (echo " ${allocl
0390: 69 73 74 7d 20 22 20 7c 20 73 65 64 20 22 73 40  ist} " | sed "s@
03a0: 20 24 7b 61 64 64 72 7d 20 40 20 40 3b 73 40 5e   ${addr} @ @;s@^
03b0: 20 20 2a 40 40 3b 73 40 20 20 2a 5c 24 40 40 22    *@@;s@  *\$@@"
03c0: 29 22 0a 09 09 09 66 69 0a 09 09 09 3b 3b 0a 09  )"....fi....;;..
03d0: 09 72 65 61 6c 6c 6f 63 29 0a 09 09 09 61 6c 6c  .realloc)....all
03e0: 6f 63 6c 69 73 74 3d 22 24 28 65 63 68 6f 20 22  oclist="$(echo "
03f0: 20 24 7b 61 6c 6c 6f 63 6c 69 73 74 7d 20 22 20   ${alloclist} " 
0400: 7c 20 73 65 64 20 22 73 40 20 24 7b 61 64 64 72  | sed "s@ ${addr
0410: 7d 20 40 20 24 7b 6e 65 77 61 64 64 72 7d 20 40  } @ ${newaddr} @
0420: 3b 73 40 5e 20 20 2a 40 40 3b 73 40 20 20 2a 5c  ;s@^  *@@;s@  *\
0430: 24 40 40 22 29 22 0a 09 09 09 3b 3b 0a 09 65 73  $@@")"....;;..es
0440: 61 63 0a 0a 09 65 63 68 6f 20 22 24 7b 61 6c 6c  ac...echo "${all
0450: 6f 63 6c 69 73 74 7d 22 0a 64 6f 6e 65 20 7c 20  oclist}".done | 
0460: 74 61 69 6c 20 2d 31 20 7c 20 77 68 69 6c 65 20  tail -1 | while 
0470: 72 65 61 64 20 6c 65 66 74 6f 76 65 72 73 3b 20  read leftovers; 
0480: 64 6f 0a 09 66 6f 72 20 6c 65 66 74 6f 76 65 72  do..for leftover
0490: 20 69 6e 20 24 7b 6c 65 66 74 6f 76 65 72 73 7d   in ${leftovers}
04a0: 3b 20 64 6f 0a 09 09 63 61 73 65 20 22 24 7b 6c  ; do...case "${l
04b0: 65 66 74 6f 76 65 72 7d 22 20 69 6e 0a 09 09 09  eftover}" in....
04c0: 21 2a 29 0a 09 09 09 09 6c 65 66 74 6f 76 65 72  !*).....leftover
04d0: 3d 22 24 28 65 63 68 6f 20 22 24 7b 6c 65 66 74  ="$(echo "${left
04e0: 6f 76 65 72 7d 22 20 7c 20 63 75 74 20 2d 63 20  over}" | cut -c 
04f0: 32 2d 29 22 0a 0a 09 09 09 09 65 63 68 6f 20 22  2-)"......echo "
0500: 44 6f 75 62 6c 65 20 66 72 65 65 64 20 6f 72 20  Double freed or 
0510: 6e 65 76 65 72 20 61 6c 6c 6f 63 61 74 65 64 20  never allocated 
0520: 24 7b 6c 65 66 74 6f 76 65 72 7d 3a 22 0a 09 09  ${leftover}:"...
0530: 09 09 67 72 65 70 20 22 24 7b 6c 65 66 74 6f 76  ..grep "${leftov
0540: 65 72 7d 22 20 22 24 7b 4c 45 41 4b 43 48 45 43  er}" "${LEAKCHEC
0550: 4b 46 49 4c 45 7d 22 20 7c 20 73 65 64 20 27 73  KFILE}" | sed 's
0560: 40 5e 40 20 20 20 20 40 27 0a 09 09 09 09 65 63  @^@    @'.....ec
0570: 68 6f 20 27 27 0a 09 09 09 09 3b 3b 0a 09 09 09  ho ''.....;;....
0580: 2a 29 0a 09 09 09 09 65 63 68 6f 20 22 55 6e 66  *).....echo "Unf
0590: 72 65 65 64 20 6d 65 6d 6f 72 79 20 24 7b 6c 65  reed memory ${le
05a0: 66 74 6f 76 65 72 7d 3a 22 0a 09 09 09 09 67 72  ftover}:".....gr
05b0: 65 70 20 22 24 7b 6c 65 66 74 6f 76 65 72 7d 22  ep "${leftover}"
05c0: 20 22 24 7b 4c 45 41 4b 43 48 45 43 4b 46 49 4c   "${LEAKCHECKFIL
05d0: 45 7d 22 20 7c 20 73 65 64 20 27 73 40 5e 40 20  E}" | sed 's@^@ 
05e0: 20 20 20 40 27 0a 09 09 09 09 65 63 68 6f 20 27     @'.....echo '
05f0: 27 0a 09 09 09 09 3b 3b 0a 09 09 65 73 61 63 0a  '.....;;...esac.
0600: 09 64 6f 6e 65 0a 64 6f 6e 65 0a 0a 72 6d 20 2d  .done.done..rm -
0610: 66 20 22 24 7b 54 4d 50 46 49 4c 45 7d 22 0a     f "${TMPFILE}".