apparmor/parser/tst/valgrind_simple.py
Tyler Hicks 4b950117f9 parser: Quiet search dir valgrind warning and remove suppression
When passing an include directory on the command line to
apparmor_parser, valgrind emits a warning:

 Invalid read of size 4
    at 0x404DA6: add_search_dir(char const*) (parser_include.c:152)
    by 0x40BB37: process_arg(int, char*) (parser_main.c:457)
    by 0x403D43: main (parser_main.c:590)
  Address 0x572207c is 28 bytes inside a block of size 29 alloc'd
    at 0x4C2A420: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
    by 0x53E31C9: strdup (strdup.c:42)
    by 0x404D94: add_search_dir(char const*) (parser_include.c:145)
    by 0x40BB37: process_arg(int, char*) (parser_main.c:457)
    by 0x403D43: main (parser_main.c:590)

This patch quiets the warning by removing strlen() calls on the t char
array. Instead, it only calls strlen() on the dir char array. t is a
dupe of dir and strlen(dir) does not trigger the valgrind warning.

Additionally, this patch adds a bit of defensive programming to the
while loop to ensure that index into the t array is never negative.

Finally, the valgrind suppression is removed from valgrind_simple.py.

Signed-off-by: Tyler Hicks <tyhicks@canonical.com>
Acked-by: Steve Beattie <steve@nxnw.org>
2014-02-05 15:17:32 -05:00

121 lines
3.9 KiB
Python
Executable file

#!/usr/bin/env python3
# ------------------------------------------------------------------
#
# Copyright (C) 2013 Canonical Ltd.
# Author: Steve Beattie <steve@nxnw.org>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of version 2 of the GNU General Public
# License published by the Free Software Foundation.
#
# ------------------------------------------------------------------
# TODO
# - finish adding suppressions for valgrind false positives
from argparse import ArgumentParser # requires python 2.7 or newer
import os
import tempfile
import unittest
import testlib
DEFAULT_TESTDIR = "./simple_tests/vars"
VALGRIND_ERROR_CODE = 151
VALGRIND_ARGS = ['--leak-check=full', '--error-exitcode=%d' % (VALGRIND_ERROR_CODE)]
VALGRIND_SUPPRESSIONS = '''
{
valgrind-serialize_profile-obsessive-overreads
Memcheck:Addr4
fun:_Z*sd_serialize_profile*
...
fun:_Z*__sd_serialize_profile*
fun:_Z*load_profile*
fun:_Z*load_policy_list*
}'''
class AAParserValgrindTests(testlib.AATestTemplate):
def setUp(self):
# REPORT ALL THE OUTPUT
self.maxDiff = None
def _runtest(self, testname, config):
parser_args = ['-Q', '-I', config.testdir]
failure_rc = [VALGRIND_ERROR_CODE, testlib.TIMEOUT_ERROR_CODE]
command = ['valgrind']
command.extend(VALGRIND_ARGS)
command.append(config.parser)
command.extend(parser_args)
command.append(testname)
rc, output = self.run_cmd(command, timeout=120)
self.assertNotIn(rc, failure_rc,
"valgrind returned error code %d, gave the following output\n%s\ncommand run: %s" % (rc, output, " ".join(command)))
def find_testcases(testdir):
'''dig testcases out of passed directory'''
for (fdir, direntries, files) in os.walk(testdir):
for f in files:
if f.endswith(".sd"):
yield os.path.join(fdir, f)
def create_suppressions():
'''generate valgrind suppressions file'''
handle, name = tempfile.mkstemp(suffix='.suppressions', prefix='aa-parser-valgrind')
os.close(handle)
handle = open(name,"w+")
handle.write(VALGRIND_SUPPRESSIONS)
handle.close()
return name
def main():
rc = 0
p = ArgumentParser()
p.add_argument('-p', '--parser', default=testlib.DEFAULT_PARSER, action="store", dest='parser')
p.add_argument('-v', '--verbose', action="store_true", dest="verbose")
p.add_argument('-s', '--skip-suppressions', action="store_true", dest="skip_suppressions",
help="Don't use valgrind suppressions to skip false positives")
p.add_argument('--dump-suppressions', action="store_true", dest="dump_suppressions",
help="Dump valgrind suppressions to stdout")
p.add_argument('testdir', action="store", default=DEFAULT_TESTDIR, nargs='?', metavar='dir',
help="run tests on alternate directory [default = %(default)s]")
config = p.parse_args()
if config.dump_suppressions:
print(VALGRIND_SUPPRESSIONS)
return rc
verbosity = 1
if config.verbose:
verbosity = 2
if not config.skip_suppressions:
suppression_file = create_suppressions()
VALGRIND_ARGS.append('--suppressions=%s' % (suppression_file))
for f in find_testcases(config.testdir):
def stub_test(self, testname=f):
self._runtest(testname, config)
stub_test.__doc__ = "test %s" % (f)
setattr(AAParserValgrindTests, 'test_%s' % (f), stub_test)
test_suite = unittest.TestSuite()
test_suite.addTest(unittest.TestLoader().loadTestsFromTestCase(AAParserValgrindTests))
try:
result = unittest.TextTestRunner(verbosity=verbosity).run(test_suite)
if not result.wasSuccessful():
rc = 1
except:
rc = 1
finally:
os.remove(suppression_file)
return rc
if __name__ == "__main__":
rc = main()
exit(rc)