From 6b78ae7c46210702fbd8abec18b02669de2fffca Mon Sep 17 00:00:00 2001 From: Thomas Jarosch Date: Fri, 12 Dec 2014 23:45:56 +0100 Subject: [PATCH] Add mmap() / mmap64() / munmap() to posix.cfg. Enables basic leak tracking Includes function prototype in posix.cfg as comment for easier overview of the function arguments. --- cfg/posix.cfg | 64 ++++++++++++++++++++++++++++++++++++++++++++ test/testmemleak.cpp | 50 +++++++++++++++++++++++++++++++++- test/testother.cpp | 15 +++++++++++ 3 files changed, 128 insertions(+), 1 deletion(-) diff --git a/cfg/posix.cfg b/cfg/posix.cfg index 01a839572..3ab0a1b3c 100644 --- a/cfg/posix.cfg +++ b/cfg/posix.cfg @@ -348,12 +348,76 @@ + + + + false + + + + + + + + + + + + + + + + + + + + + + + + false + + + + + + + + + + + + + + + + + + + + + + + false + + + + + + + + free strdup strndup wcsdup + + munmap + mmap + mmap64 + close open diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index afd8e2f94..52e7f35f3 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -367,6 +367,7 @@ private: // test that the cfg files are configured correctly TEST_CASE(posixcfg); + TEST_CASE(posixcfg_mmap); } std::string getcode(const char code[], const char varname[], bool classfunc=false) { @@ -4243,7 +4244,6 @@ private: ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: p\n", errout.str()); } - // Test that posix.cfg is configured correctly void posixcfg() { Settings settings; @@ -4303,6 +4303,54 @@ private: "}", &settings); ASSERT_EQUALS("[test.cpp:6]: (error) Memory leak: s\n", errout.str()); } + + void posixcfg_mmap() { + Settings settings; + settings.standards.posix = true; + LOAD_LIB_2(settings.library, "posix.cfg"); + + // normal mmap + check("void f(int fd) {\n" + " char *addr = mmap(NULL, 255, PROT_NONE, MAP_PRIVATE, fd, 0);\n" + " munmap(addr, 255);\n" + "}", &settings); + ASSERT_EQUALS("", errout.str()); + + // mmap64 - large file support + check("void f(int fd) {\n" + " char *addr = mmap64(NULL, 255, PROT_NONE, MAP_PRIVATE, fd, 0);\n" + " munmap(addr, 255);\n" + "}", &settings); + ASSERT_EQUALS("", errout.str()); + + // pass in fixed address + check("void f(int fd) {\n" + " void *fixed_addr = 123;\n" + " void *mapped_addr = mmap(fixed_addr, 255, PROT_NONE, MAP_PRIVATE, fd, 0);\n" + " munmap(mapped_addr, 255);\n" + "}", &settings); + ASSERT_EQUALS("", errout.str()); + + // no munmap() + check("void f(int fd) {\n" + " void *addr = mmap(NULL, 255, PROT_NONE, MAP_PRIVATE, fd, 0);\n" + "}", &settings); + ASSERT_EQUALS("[test.cpp:3]: (error) Memory leak: addr\n", errout.str()); + + // wrong deallocator + check("void f(int fd) {\n" + " void *addr = mmap(NULL, 255, PROT_NONE, MAP_PRIVATE, fd, 0);\n" + " free(addr);\n" + "}", &settings); + ASSERT_EQUALS("[test.cpp:3]: (error) Mismatching allocation and deallocation: addr\n", errout.str()); + + // wrong deallocator for mmap64 + check("void f(int fd) {\n" + " void *addr = mmap64(NULL, 255, PROT_NONE, MAP_PRIVATE, fd, 0);\n" + " free(addr);\n" + "}", &settings); + ASSERT_EQUALS("[test.cpp:3]: (error) Mismatching allocation and deallocation: addr\n", errout.str()); + } }; static TestMemleakInFunction testMemleakInFunction; diff --git a/test/testother.cpp b/test/testother.cpp index fe8f4e6b6..544a34ebd 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -6383,6 +6383,21 @@ private: &settings_posix ); ASSERT_EQUALS("[test.cpp:2]: (warning) Return value of function strdupa() is not used.\n", errout.str()); + + // mmap(): error about unused return address since fixed_addr is just a hint + check("void f(int fd) {\n" + " void *fixed_addr = 123;\n" + " mmap(fixed_addr, 255, PROT_NONE, MAP_PRIVATE, fd, 0);\n" + " munmap(fixed_addr, 255);\n" + "}", + "test.cpp", + false, // experimental + false, // inconclusive + true, // posix + false, // runSimpleChecks + &settings_posix + ); + ASSERT_EQUALS("[test.cpp:3]: (warning) Return value of function mmap() is not used.\n", errout.str()); } };