Skip to content

Commit

Permalink
[tests] Test Leak Sanitizer with sample output file
Browse files Browse the repository at this point in the history
- LeakSanitizer is tested with lsan.out
  • Loading branch information
jay24rajput committed Jul 5, 2021
1 parent 1c0605e commit 1311357
Show file tree
Hide file tree
Showing 4 changed files with 240 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <stdlib.h>
void *p;
int main()
{
p = malloc(7);
p = 0; // The memory is leaked here.
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
==23646==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 7 byte(s) in 1 object(s) allocated from:
#0 0x4af01b in __interceptor_malloc /projects/compiler-rt/lib/asan/asan_malloc_linux.cc:52:3
#1 0x4da26a in main files/lsan.c:4:7
#2 0x7f076fd9cec4 in __libc_start_main libc-start.c:287
SUMMARY: AddressSanitizer: 7 byte(s) leaked in 1 allocation(s)
111 changes: 111 additions & 0 deletions tools/report-converter/tests/unit/lsan_output_test_files/lsan.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>diagnostics</key>
<array>
<dict>
<key>category</key>
<string>unknown</string>
<key>check_name</key>
<string>LeakSanitizer</string>
<key>description</key>
<string>detected memory leaks</string>
<key>issue_hash_content_of_line_in_context</key>
<string>37e6fc6c7fb792d5e91ac6c3dbbe46b0</string>
<key>location</key>
<dict>
<key>col</key>
<integer>7</integer>
<key>file</key>
<integer>0</integer>
<key>line</key>
<integer>4</integer>
</dict>
<key>notes</key>
<array>
<dict>
<key>depth</key>
<integer>0</integer>
<key>kind</key>
<string>note</string>
<key>location</key>
<dict>
<key>col</key>
<integer>7</integer>
<key>file</key>
<integer>0</integer>
<key>line</key>
<integer>4</integer>
</dict>
<key>message</key>
<string>Direct leak of 7 byte(s) in 1 object(s) allocated from:
#0 0x4af01b in __interceptor_malloc /projects/compiler-rt/lib/asan/asan_malloc_linux.cc:52:3
#1 0x4da26a in main files/lsan.c:4:7
#2 0x7f076fd9cec4 in __libc_start_main libc-start.c:287
SUMMARY: AddressSanitizer: 7 byte(s) leaked in 1 allocation(s)
</string>
</dict>
</array>
<key>path</key>
<array>
<dict>
<key>depth</key>
<integer>0</integer>
<key>kind</key>
<string>event</string>
<key>location</key>
<dict>
<key>col</key>
<integer>7</integer>
<key>file</key>
<integer>0</integer>
<key>line</key>
<integer>4</integer>
</dict>
<key>message</key>
<string> #1 0x4da26a in main files/lsan.c:4:7</string>
</dict>
<dict>
<key>depth</key>
<integer>0</integer>
<key>kind</key>
<string>event</string>
<key>location</key>
<dict>
<key>col</key>
<integer>7</integer>
<key>file</key>
<integer>0</integer>
<key>line</key>
<integer>4</integer>
</dict>
<key>message</key>
<string>detected memory leaks</string>
</dict>
</array>
<key>type</key>
<string>lsan</string>
</dict>
</array>
<key>files</key>
<array>
<string>files/lsan.c</string>
</array>
<key>metadata</key>
<dict>
<key>analyzer</key>
<dict>
<key>name</key>
<string>lsan</string>
</dict>
<key>generated_by</key>
<dict>
<key>name</key>
<string>report-converter</string>
<key>version</key>
<string>x.y.z</string>
</dict>
</dict>
</dict>
</plist>
115 changes: 115 additions & 0 deletions tools/report-converter/tests/unit/test_lsan_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# -------------------------------------------------------------------------
#
# Part of the CodeChecker project, under the Apache License v2.0 with
# LLVM Exceptions. See LICENSE for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#
# -------------------------------------------------------------------------

"""
This module tests the correctness of the OutputParser and PListConverter, which
used in sequence transform LeakSanitizer output to a plist file.
"""


import os
import plistlib
import shutil
import tempfile
import unittest

from codechecker_report_converter.output_parser import Event, Message
from codechecker_report_converter.sanitizers.leak.output_parser import \
LSANParser
from codechecker_report_converter.sanitizers.leak.analyzer_result import \
LSANAnalyzerResult

OLD_PWD = None


def setup_module():
"""Setup the test tidy reprs for the test classes in the module."""
global OLD_PWD
OLD_PWD = os.getcwd()
os.chdir(os.path.join(os.path.dirname(__file__),
'lsan_output_test_files'))


def teardown_module():
"""Restore environment after tests have ran."""
global OLD_PWD
os.chdir(OLD_PWD)


class LSANPListConverterTestCase(unittest.TestCase):
""" Test the output of the LSANAnalyzerResult. """

def setUp(self):
""" Setup the test. """
self.analyzer_result = LSANAnalyzerResult()
self.cc_result_dir = tempfile.mkdtemp()

def tearDown(self):
""" Clean temporary directory. """
shutil.rmtree(self.cc_result_dir)

def test_san(self):
""" Test for the lsan.plist file. """
self.analyzer_result.transform('lsan.out', self.cc_result_dir)

with open('lsan.plist', mode='rb') as pfile:
exp = plistlib.load(pfile)

plist_file = os.path.join(self.cc_result_dir, 'lsan.c_lsan.plist')
with open(plist_file, mode='rb') as pfile:
res = plistlib.load(pfile)

# Use relative path for this test.
res['files'][0] = 'files/lsan.c'

self.assertTrue(res['metadata']['generated_by']['version'])
res['metadata']['generated_by']['version'] = "x.y.z"

self.assertEqual(res, exp)


class LSANOutputParserTestCase(unittest.TestCase):
"""
Tests the output of the OutputParser, which converts an Leak Sanitizer
output file to zero or more Message object.
"""

def setUp(self):
""" Setup the OutputParser. """
self.parser = LSANParser()
self.lsan_repr = [
Message(
os.path.abspath('files/lsan.c'),
4, 7,
"detected memory leaks",
"LeakSanitizer",
[Event(
os.path.abspath('files/lsan.c'),
4, 7,
" #1 0x4da26a in main files/lsan.c:4:7"
)],
[Event(
os.path.abspath('files/lsan.c'),
4, 7,
"Direct leak of 7 byte(s) in 1 object(s) allocated from:\n"
" #0 0x4af01b in __interceptor_malloc /projects/"
"compiler-rt/lib/asan/asan_malloc_linux.cc:52:3\n"
" #1 0x4da26a in main files/lsan.c:4:7\n"
" #2 0x7f076fd9cec4 in __libc_start_main "
"libc-start.c:287\n"
"SUMMARY: AddressSanitizer: 7 byte(s) "
"leaked in 1 allocation(s)\n"
)]),
]

def test_lsan(self):
""" Test the generated Messages of lsan.out. """
messages = self.parser.parse_messages_from_file('lsan.out')
self.assertEqual(len(messages), len(self.lsan_repr))
for message in messages:
self.assertIn(message, self.lsan_repr)

0 comments on commit 1311357

Please sign in to comment.