Skip to content

Commit

Permalink
trigger valgrind failure on memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
n5m committed Oct 18, 2020
1 parent 78cbb52 commit d6160ef
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 2 deletions.
2 changes: 1 addition & 1 deletion testament/specs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type
reFilesDiffer, # expected and given filenames differ
reLinesDiffer, # expected and given line numbers differ
reOutputsDiffer,
reExitcodesDiffer,
reExitcodesDiffer, # exit codes of program or of valgrind differ
reTimeout,
reInvalidPeg,
reCodegenFailure,
Expand Down
2 changes: 1 addition & 1 deletion testament/testament.nim
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ proc testSpecHelper(r: var TResults, test: var TTest, expected: TSpec,
else:
exeCmd = exeFile.dup(normalizeExe)
if expected.useValgrind:
args = @["--error-exitcode=1"] & exeCmd & args
args = @["--error-exitcode=1", "--leak-check=yes"] & exeCmd & args
exeCmd = "valgrind"
var (_, buf, exitCode) = execCmdEx2(exeCmd, args, input = expected.input)
# Treat all failure codes from nodejs as 1. Older versions of nodejs used
Expand Down
7 changes: 7 additions & 0 deletions testament/tests/shouldfail/tvalgrind.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
discard """
valgrind: true
cmd: "nim $target --gc:arc -d:useMalloc $options $file"
"""

# test that a memory leak is caught by valgrind
discard alloc(1)
2 changes: 2 additions & 0 deletions tests/testament/tshould_not_work.nim
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ FAIL: tests/shouldfail/tsortoutput.nim C
Failure: reOutputsDiffer
FAIL: tests/shouldfail/ttimeout.nim C
Failure: reTimeout
FAIL: tests/shouldfail/tvalgrind.nim C
Failure: reExitcodesDiffer
'''
"""

Expand Down
9 changes: 9 additions & 0 deletions tests/valgrind/tleak_arc_no_malloc.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
discard """
valgrind: true
cmd: "nim $target --gc:arc $options $file"
exitcode: 0
"""

# unlike tleak_arc_useMalloc.nim, tleak_arc_noUseMalloc.nim is powerless to
# detect the memory leak because malloc is not used
discard alloc(1)
9 changes: 9 additions & 0 deletions tests/valgrind/tleak_arc_useMalloc.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
discard """
valgrind: true
cmd: "nim $target --gc:arc -d:useMalloc $options $file"
exitcode: 1
outputsub: " definitely lost: 7 bytes in 2 blocks"
"""

discard alloc(3)
discard alloc(4)
9 changes: 9 additions & 0 deletions tests/valgrind/tleak_orc_no_malloc.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
discard """
valgrind: true
cmd: "nim $target --gc:orc $options $file"
exitcode: 0
"""

# unlike tleak_orc_useMalloc.nim, tleak_orc_noUseMalloc.nim is powerless to
# detect the memory leak because malloc is not used
discard alloc(1)
9 changes: 9 additions & 0 deletions tests/valgrind/tleak_orc_useMalloc.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
discard """
valgrind: true
cmd: "nim $target --gc:orc -d:useMalloc $options $file"
exitcode: 1
outputsub: " definitely lost: 7 bytes in 2 blocks"
"""

discard alloc(3)
discard alloc(4)
8 changes: 8 additions & 0 deletions tests/valgrind/tleak_refc.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
discard """
valgrind: true
cmd: "nim $target --gc:refc -d:useMalloc $options $file"
exitcode: 0
"""

# Valgrind cannot detect this leak even if malloc is used because --gc:refc
discard alloc(1)

0 comments on commit d6160ef

Please sign in to comment.