Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testsuite: add valgrind coverage #120

Merged
merged 9 commits into from
Feb 5, 2024
Merged

testsuite: add valgrind coverage #120

merged 9 commits into from
Feb 5, 2024

Conversation

garlick
Copy link
Member

@garlick garlick commented Feb 5, 2024

This adds a valgrind test and fixes a few memory leaks but not all of them.The test currently is an expected failure, but it can be run with -d and the failures can be picked out of the trash. I suppose we could merge this as is and pick off the rest of the leaks as time permits. (I tried but didn't find anything obvious)

$ ./t0033-valgrind.t -d
valgrind-3.16.1
ok 1 - create test powerman.conf
not ok 2 - run powermand --stdio under valgrind # TODO known breakage
# still have 1 known breakage(s)
# passed all remaining 1 test(s)
1..2
$ cat trash-directory.t0033-valgrind/valgrind.err
==27863== Memcheck, a memory error detector
==27863== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==27863== Using Valgrind-3.16.1 and LibVEX; rerun with -h for copyright info
==27863== Command: /nfshome/garlick/proj/powerman/t/../src/powerman/powermand --stdio -c powerman.conf
==27863== 
==27863== Warning: noted but unhandled ioctl 0x5441 with no size/direction hints.
==27863==    This could cause spurious value errors to appear.
==27863==    See README_MISSING_SYSCALL_OR_IOCTL for guidance on writing a proper wrapper.
powermand: _pipe_connect(test0): opened on /dev/pts/3
powermand: client poll: hangup
powermand: _pipe_disconnect(test0): /nfshome/garlick/proj/powerman/t/../t/simulators/vpcd terminated with signal 1
==27863== 
==27863== HEAP SUMMARY:
==27863==     in use at exit: 24,930 bytes in 15 blocks
==27863==   total heap usage: 1,995 allocs, 1,980 frees, 216,254 bytes allocated
==27863== 
==27863== 512 bytes in 1 blocks are possibly lost in loss record 4 of 13
==27863==    at 0x4849E4C: malloc (vg_replace_malloc.c:307)
==27863==    by 0x11A35B: list_alloc_aux (list.c:782)
==27863==    by 0x11A35B: list_node_alloc (list.c:738)
==27863==    by 0x11A35B: list_node_create (list.c:665)
==27863==    by 0x11423B: yyparse (parse_tab.y:219)
==27863==    by 0x114ABB: parse_config_file (parse_tab.y:368)
==27863==    by 0x114C43: conf_init (parse_util.c:90)
==27863==    by 0x10AE9F: main (powermand.c:124)
==27863== 
==27863== 512 bytes in 1 blocks are possibly lost in loss record 5 of 13
==27863==    at 0x4849E4C: malloc (vg_replace_malloc.c:307)
==27863==    by 0x11A35B: list_alloc_aux (list.c:782)
==27863==    by 0x11A35B: list_node_alloc (list.c:738)
==27863==    by 0x11A35B: list_node_create (list.c:665)
==27863==    by 0x113C7B: yyparse (parse_tab.y:292)
==27863==    by 0x114ABB: parse_config_file (parse_tab.y:368)
==27863==    by 0x114C43: conf_init (parse_util.c:90)
==27863==    by 0x10AE9F: main (powermand.c:124)
==27863== 
==27863== 512 bytes in 1 blocks are possibly lost in loss record 6 of 13
==27863==    at 0x4849E4C: malloc (vg_replace_malloc.c:307)
==27863==    by 0x11A35B: list_alloc_aux (list.c:782)
==27863==    by 0x11A35B: list_node_alloc (list.c:738)
==27863==    by 0x11A35B: list_node_create (list.c:665)
==27863==    by 0x113C43: yyparse (parse_tab.y:288)
==27863==    by 0x114ABB: parse_config_file (parse_tab.y:368)
==27863==    by 0x114C43: conf_init (parse_util.c:90)
==27863==    by 0x10AE9F: main (powermand.c:124)
==27863== 
==27863== 512 bytes in 1 blocks are possibly lost in loss record 7 of 13
==27863==    at 0x4849E4C: malloc (vg_replace_malloc.c:307)
==27863==    by 0x11A35B: list_alloc_aux (list.c:782)
==27863==    by 0x11A35B: list_node_alloc (list.c:738)
==27863==    by 0x11A35B: list_node_create (list.c:665)
==27863==    by 0x113103: makeDevice (parse_tab.y:685)
==27863==    by 0x114787: yyparse (parse_tab.y:176)
==27863==    by 0x114ABB: parse_config_file (parse_tab.y:368)
==27863==    by 0x114C43: conf_init (parse_util.c:90)
==27863==    by 0x10AE9F: main (powermand.c:124)
==27863== 
==27863== 512 bytes in 1 blocks are possibly lost in loss record 8 of 13
==27863==    at 0x4849E4C: malloc (vg_replace_malloc.c:307)
==27863==    by 0x11A35B: list_alloc_aux (list.c:782)
==27863==    by 0x11A35B: list_node_alloc (list.c:738)
==27863==    by 0x11A35B: list_node_create (list.c:665)
==27863==    by 0x112B2B: makeStmt (parse_tab.y:582)
==27863==    by 0x1130F7: makeDevice (parse_tab.y:685)
==27863==    by 0x114787: yyparse (parse_tab.y:176)
==27863==    by 0x114ABB: parse_config_file (parse_tab.y:368)
==27863==    by 0x114C43: conf_init (parse_util.c:90)
==27863==    by 0x10AE9F: main (powermand.c:124)
==27863== 
==27863== 1,280 bytes in 1 blocks are possibly lost in loss record 11 of 13
==27863==    at 0x4849E4C: malloc (vg_replace_malloc.c:307)
==27863==    by 0x11B2AF: list_alloc_aux (list.c:782)
==27863==    by 0x11B2AF: list_iterator_alloc (list.c:753)
==27863==    by 0x11B2AF: list_iterator_create (list.c:518)
==27863==    by 0x1152CF: pluglist_create (pluglist.c:81)
==27863==    by 0x1130B3: makeDevice (parse_tab.y:669)
==27863==    by 0x114787: yyparse (parse_tab.y:176)
==27863==    by 0x114ABB: parse_config_file (parse_tab.y:368)
==27863==    by 0x114C43: conf_init (parse_util.c:90)
==27863==    by 0x10AE9F: main (powermand.c:124)
==27863== 
==27863== 1,536 bytes in 3 blocks are possibly lost in loss record 12 of 13
==27863==    at 0x4849E4C: malloc (vg_replace_malloc.c:307)
==27863==    by 0x11A35B: list_alloc_aux (list.c:782)
==27863==    by 0x11A35B: list_node_alloc (list.c:738)
==27863==    by 0x11A35B: list_node_create (list.c:665)
==27863==    by 0x1119A3: yylex (parse_lex.l:85)
==27863==    by 0x113653: yyparse (parse_tab.c:1424)
==27863==    by 0x114ABB: parse_config_file (parse_tab.y:368)
==27863==    by 0x114C43: conf_init (parse_util.c:90)
==27863==    by 0x10AE9F: main (powermand.c:124)
==27863== 
==27863== LEAK SUMMARY:
==27863==    definitely lost: 0 bytes in 0 blocks
==27863==    indirectly lost: 0 bytes in 0 blocks
==27863==      possibly lost: 5,376 bytes in 9 blocks
==27863==    still reachable: 19,554 bytes in 6 blocks
==27863==         suppressed: 0 bytes in 0 blocks
==27863== Reachable blocks (those to which a pointer was found) are not shown.
==27863== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==27863== 
==27863== For lists of detected and suppressed errors, rerun with: -s
==27863== ERROR SUMMARY: 7 errors from 7 contexts (suppressed: 0 from 0)

Copy link
Member

@chu11 chu11 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Problem: powermand does not call destructors unless
terminated by a signal.

Instead of calling destructors only from the signal handler,
call them upon exit of the select loop, and have the signal handler
wake up the select loop by writing to a pipe.

Fixes chaos#119
Problem:  the config parser leaks the list of listen
addresses.

==13356== 40 bytes in 1 blocks are possibly lost in loss record 1 of 14
==13356==    at 0x4849E4C: malloc (vg_replace_malloc.c:307)
==13356==    by 0x120647: xmalloc (xmalloc.c:53)
==13356==    by 0x12098B: xstrdup (xmalloc.c:115)
==13356==    by 0x114FC7: conf_add_listen (parse_util.c:219)
==13356==    by 0x114723: yyparse (parse_tab.y:170)
==13356==    by 0x114B4B: parse_config_file (parse_tab.y:370)
==13356==    by 0x114CD3: conf_init (parse_util.c:92)
==13356==    by 0x10AE9F: main (powermand.c:124)

Destroy the listen address list in conf_fini().
Problem: the list of aliases is not destroyed when the config
parser is destroyed.

==14147== 128 bytes in 1 blocks are possibly lost in loss record 8 of 19
==14147==    at 0x484C164: calloc (vg_replace_malloc.c:760)
==14147==    by 0x115CAF: hostlist_new (hostlist.c:1069)
==14147==    by 0x1191BF: _hostlist_create_bracketed.constprop.0 (hostlist.c:1500)
==14147==    by 0x115243: _alias_create (parse_util.c:328)
==14147==    by 0x115243: conf_add_alias (parse_util.c:344)
==14147==    by 0x1147EB: makeAlias (parse_tab.y:713)
==14147==    by 0x1147EB: yyparse (parse_tab.y:187)
==14147==    by 0x114B4B: parse_config_file (parse_tab.y:370)
==14147==    by 0x114CD3: conf_init (parse_util.c:92)
==14147==    by 0x10AE9F: main (powermand.c:124)

Destroy the alias list in conf_fini().
Problem: a comment in parse_util.c::conf_init() refers to
source files and data structures that no longer exist.

Drop those references from the comment.
Problem: _clear_current_spec() is a function that just
zero-initializes a struct.

Replace _clear_current_spec() with memset(0) and rely on
BSS zero-initialization to zero the structure at startup.
Problem: _copy_current_spec() copies an array that was
already copied by structure assignment

Don't do that.
problem: destroyStmt() doesn't free plug_name which was
allocated in makeStmt().

Add missing free.
Problem: destroyStmt() destroys stmt->u.foreach.stmts
when it should destroy stmt->u.ifonoff.stmts.

This happens to work because the structs are aligned,
but it is fragile.

Destroy the correct member.
Problem: there is not valgrind coverage for powermand.

Add a test script.
Currently the test is expected to fail, but at least now
we know some things that need to be fixed.
@mergify mergify bot merged commit 7ae1f5d into chaos:master Feb 5, 2024
8 checks passed
@garlick garlick deleted the valgrind branch February 5, 2024 21:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants