diff options
-rw-r--r-- | ChangeLog | 14 | ||||
-rw-r--r-- | plugin/icedteanp/IcedTeaPluginUtils.cc | 20 | ||||
-rw-r--r-- | plugin/icedteanp/IcedTeaPluginUtils.h | 10 | ||||
-rw-r--r-- | tests/cpp-unit-tests/IcedTeaPluginUtilsTest.cc | 3 | ||||
-rw-r--r-- | tests/cpp-unit-tests/IcedTeaScriptablePluginObjectTest.cc | 52 | ||||
-rw-r--r-- | tests/cpp-unit-tests/MemoryLeakDetector.h | 85 | ||||
-rw-r--r-- | tests/cpp-unit-tests/browser_mock.cc | 15 | ||||
-rw-r--r-- | tests/cpp-unit-tests/main.cc | 10 |
8 files changed, 188 insertions, 21 deletions
@@ -1,3 +1,17 @@ +2013-06-21 Adam Domurad <[email protected]> + + * plugin/icedteanp/IcedTeaPluginUtils.cc: Add global state clearing + utility functions. + * plugin/icedteanp/IcedTeaPluginUtils.h: Same. + * tests/cpp-unit-tests/IcedTeaScriptablePluginObjectTest.cc: Test + scriptable object creation and destruction. + * tests/cpp-unit-tests/browser_mock.cc + (mock_createobject): New, mocks NPAPI 'createobject'. + * tests/cpp-unit-tests/MemoryLeakDetector.h: New, memory leak detection + utility class. + * tests/cpp-unit-tests/main.cc + (ReportTestFinish): Print which tests resulted in memory leaks. + 2013-06-21 Jiri Vanek <[email protected]> Adam Domurad <[email protected]> Omair Majid <[email protected]> diff --git a/plugin/icedteanp/IcedTeaPluginUtils.cc b/plugin/icedteanp/IcedTeaPluginUtils.cc index 6ed17bb..8e690bd 100644 --- a/plugin/icedteanp/IcedTeaPluginUtils.cc +++ b/plugin/icedteanp/IcedTeaPluginUtils.cc @@ -498,6 +498,14 @@ IcedTeaPluginUtilities::removeInstanceID(void* member_ptr) instance_map->erase(member_ptr); } +/* Clear instance_map. Useful for tests. */ +void +IcedTeaPluginUtilities::clearInstanceIDs() +{ + delete instance_map; + instance_map = new std::map<void*, NPP>(); +} + /** * Removes all mappings to a given instance, and all associated objects */ @@ -603,6 +611,18 @@ IcedTeaPluginUtilities::removeObjectMapping(std::string key) object_map->erase(key); } +/* Clear object_map. Useful for tests. */ +void +IcedTeaPluginUtilities::clearObjectMapping() +{ + std::map<std::string, NPObject*>::iterator iter = object_map->begin(); + for (; iter != object_map->end(); ++iter) { + browser_functions.releaseobject(iter->second); + } + delete object_map; + object_map = new std::map<std::string, NPObject*>(); +} + /* * Similar to printStringVector, but takes a vector of string pointers instead * diff --git a/plugin/icedteanp/IcedTeaPluginUtils.h b/plugin/icedteanp/IcedTeaPluginUtils.h index b55355b..634b675 100644 --- a/plugin/icedteanp/IcedTeaPluginUtils.h +++ b/plugin/icedteanp/IcedTeaPluginUtils.h @@ -252,9 +252,12 @@ class IcedTeaPluginUtilities static void storeInstanceID(void* member_ptr, NPP instance); - static void removeInstanceID(void* member_ptr); + static void removeInstanceID(void* member_ptr); - static NPP getInstanceFromMemberPtr(void* member_ptr); + /* Clear object_map. Useful for tests. */ + static void clearInstanceIDs(); + + static NPP getInstanceFromMemberPtr(void* member_ptr); static NPObject* getNPObjectFromJavaKey(std::string key); @@ -262,6 +265,9 @@ class IcedTeaPluginUtilities static void removeObjectMapping(std::string key); + /* Clear object_map. Useful for tests. */ + static void clearObjectMapping(); + static void invalidateInstance(NPP instance); static bool isObjectJSArray(NPP instance, NPObject* object); diff --git a/tests/cpp-unit-tests/IcedTeaPluginUtilsTest.cc b/tests/cpp-unit-tests/IcedTeaPluginUtilsTest.cc index dc16bd2..e8e9289 100644 --- a/tests/cpp-unit-tests/IcedTeaPluginUtilsTest.cc +++ b/tests/cpp-unit-tests/IcedTeaPluginUtilsTest.cc @@ -44,9 +44,6 @@ #include "IcedTeaNPPlugin.h" #include <fstream> -extern void trim(std::string& str); -extern bool file_exists(std::string filename); - TEST(NPVariantAsString) { NPVariant var; STRINGZ_TO_NPVARIANT("test", var); diff --git a/tests/cpp-unit-tests/IcedTeaScriptablePluginObjectTest.cc b/tests/cpp-unit-tests/IcedTeaScriptablePluginObjectTest.cc index 65e4d16..0827570 100644 --- a/tests/cpp-unit-tests/IcedTeaScriptablePluginObjectTest.cc +++ b/tests/cpp-unit-tests/IcedTeaScriptablePluginObjectTest.cc @@ -39,28 +39,60 @@ #include <npapi.h> #include "browser_mock.h" -#include "checked_allocations.h" +#include "MemoryLeakDetector.h" #include "IcedTeaScriptablePluginObject.h" +#include "IcedTeaPluginUtils.h" + +static NPP_t dummy_npp = {0,0}; + +SUITE(IcedTeaScriptablePluginObject) { + TEST(destructor) { + MemoryLeakDetector leak_detector; + IcedTeaScriptablePluginObject* obj = new IcedTeaScriptablePluginObject(&dummy_npp); + delete obj; + CHECK(leak_detector.memory_leaks() == 0); + } + + TEST(get_scriptable_java_object) { + MemoryLeakDetector leak_detector; + NPObject* obj = IcedTeaScriptablePluginObject::get_scriptable_java_package_object(&dummy_npp, "DummyPackage"); + browser_functions.releaseobject(obj); + CHECK(leak_detector.memory_leaks() == 0); + } +} SUITE(IcedTeaScriptableJavaObject) { TEST(deallocate) { - int pre_allocations = cpp_unfreed_allocations(); - IcedTeaScriptableJavaObject* obj = new IcedTeaScriptableJavaObject(NULL); + MemoryLeakDetector leak_detector; + IcedTeaScriptableJavaObject* obj = new IcedTeaScriptableJavaObject(&dummy_npp); IcedTeaScriptableJavaObject::deAllocate(obj); - int post_allocations = cpp_unfreed_allocations(); - - CHECK(pre_allocations == post_allocations); + CHECK(leak_detector.memory_leaks() == 0); } } SUITE(IcedTeaScriptableJavaPackageObject) { TEST(deallocate) { - int pre_allocations = cpp_unfreed_allocations(); - IcedTeaScriptableJavaPackageObject* obj = new IcedTeaScriptableJavaPackageObject(NULL); + MemoryLeakDetector leak_detector; + IcedTeaScriptableJavaPackageObject* obj = new IcedTeaScriptableJavaPackageObject(&dummy_npp); IcedTeaScriptableJavaPackageObject::deAllocate(obj); - int post_allocations = cpp_unfreed_allocations(); + CHECK(leak_detector.memory_leaks() == 0); + } + + TEST(get_scriptable_java_object) { + MemoryLeakDetector leak_detector; + + NPObject* first_obj = IcedTeaScriptableJavaPackageObject::get_scriptable_java_object(&dummy_npp, "DummyClass", "DummyInstance", false); + browser_functions.releaseobject(first_obj); + + /* After the first call, the object should be cached in the object map */ + NPObject* second_obj = IcedTeaScriptableJavaPackageObject::get_scriptable_java_object(&dummy_npp, "DummyClass", "DummyInstance", false); + + /* Objects should be the same, because of caching */ + CHECK(first_obj == second_obj); + + browser_functions.releaseobject(second_obj); - CHECK(pre_allocations == post_allocations); + CHECK(leak_detector.memory_leaks() == 0); } } diff --git a/tests/cpp-unit-tests/MemoryLeakDetector.h b/tests/cpp-unit-tests/MemoryLeakDetector.h new file mode 100644 index 0000000..2bd5e9c --- /dev/null +++ b/tests/cpp-unit-tests/MemoryLeakDetector.h @@ -0,0 +1,85 @@ +/* Copyright (C) 2013 Red Hat + + This file is part of IcedTea. + + IcedTea is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + IcedTea is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with IcedTea; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301 USA. + + Linking this library statically or dynamically with other modules is + making a combined work based on this library. Thus, the terms and + conditions of the GNU General Public License cover the whole + combination. + + As a special exception, the copyright holders of this library give you + permission to link this library with independent modules to produce an + executable, regardless of the license terms of these independent + modules, and to copy and distribute the resulting executable under + terms of your choice, provided that you also meet, for each linked + independent module, the terms and conditions of the license of that + module. An independent module is a module which is not derived from + or based on this library. If you modify this library, you may extend + this exception to your version of the library, but you are not + obligated to do so. If you do not wish to do so, delete this + exception statement from your version. */ + +// Memory leak detection helper class. +// This utilizes checked_allocations.h & browser_mock.h to query how many unfreed allocations exist. +// As well, it clears global state that is problematic for accurate measure of memory leaks. + +#ifndef MEMORYLEAKDETECTOR_H_ +#define MEMORYLEAKDETECTOR_H_ + +#include <cstdio> +#include "checked_allocations.h" +#include "IcedTeaPluginUtils.h" + +class MemoryLeakDetector { +public: + MemoryLeakDetector() { + reset(); + } + + /* Reset allocation counts and certain global state touched by the tests. + * This is necessary to ensure accurate leak reporting for some functions. */ + void reset() { + reset_global_state(); + initial_cpp_allocations = cpp_unfreed_allocations(); + initial_npapi_allocations = browsermock_unfreed_allocations(); + } + + /* Return allocation counts, after clearing global state that can conflict with the + * leak detection. */ + int memory_leaks() { + reset_global_state(); + int cpp_leaks = cpp_unfreed_allocations() - initial_cpp_allocations; + int npapi_leaks = browsermock_unfreed_allocations() - initial_npapi_allocations; + + return cpp_leaks + npapi_leaks; + } + +private: + static void reset_global_state() { + /* Clears allocations caused by storeInstanceID */ + IcedTeaPluginUtilities::clearInstanceIDs(); + /* Clears allocations caused by storeObjectMapping */ + IcedTeaPluginUtilities::clearObjectMapping(); + } + + int initial_cpp_allocations; + int initial_npapi_allocations; +}; + + +#endif /* MEMORYLEAKDETECTOR_H_ */ diff --git a/tests/cpp-unit-tests/browser_mock.cc b/tests/cpp-unit-tests/browser_mock.cc index 6b01224..e333be7 100644 --- a/tests/cpp-unit-tests/browser_mock.cc +++ b/tests/cpp-unit-tests/browser_mock.cc @@ -73,17 +73,30 @@ static void mock_releaseobject(NPObject* obj) { if (obj->_class->deallocate) { obj->_class->deallocate(obj); } else { - free(obj); + mock_memfree(obj); } } } +static NPObject* mock_createobject(NPP instance, NPClass* np_class) { + NPObject* obj; + if (np_class->allocate) { + obj = np_class->allocate(instance, np_class); + } else { + obj = (NPObject*) mock_memalloc(sizeof(NPObject)); + } + obj->referenceCount = 1; + obj->_class = np_class; + return obj; +} + void browsermock_setup_functions() { memset(&browser_functions, 0, sizeof(NPNetscapeFuncs)); browser_functions.memalloc = &mock_memalloc; browser_functions.memfree = &mock_memfree; + browser_functions.createobject = &mock_createobject; browser_functions.retainobject = &mock_retainobject; browser_functions.releaseobject= &mock_releaseobject; } diff --git a/tests/cpp-unit-tests/main.cc b/tests/cpp-unit-tests/main.cc index 2afacf2..d7e71f6 100644 --- a/tests/cpp-unit-tests/main.cc +++ b/tests/cpp-unit-tests/main.cc @@ -85,18 +85,18 @@ public: float secondsElapsed) { int posttest_allocs = cpp_unfreed_allocations(); + std::string testname = full_testname(details); if (browsermock_unfreed_allocations() > 0) { - printf("*** WARNING: Memory leak! %d more NPAPI allocations than frees!\n", - browsermock_unfreed_allocations()); + printf("*** WARNING: %s has a memory leak! %d more NPAPI allocations than frees!\n", + testname.c_str(), browsermock_unfreed_allocations()); } if (posttest_allocs > pretest_allocs) { - printf("*** WARNING: Memory leak! %d more operator 'new' allocations than 'delete's!\n", - posttest_allocs - pretest_allocs); + printf("*** WARNING: %s has a memory leak! %d more operator 'new' allocations than 'delete's!\n", + testname.c_str(), posttest_allocs - pretest_allocs); } if (did_finish_correctly) { - std::string testname = full_testname(details); printf("Passed: %s\n", testname.c_str()); } } |