From 86ecd1a4b8d9efad0d12f71f5687ea1205dcd435 Mon Sep 17 00:00:00 2001 From: Pawel Wilma Date: Fri, 23 Nov 2018 09:43:06 +0100 Subject: [PATCH] Memory leak reported for long string debug variables in ULTs This commit eliminates ULTs memory leaks reported for string debug variables exceeding small string optimization size by calling shrink_to_fit() method on string debug variables in DebugManagerStateRestore destructor. Change-Id: I304c5c5c23c80f01fdf13f38cea5b4eceb18a94f Signed-off-by: Pawel Wilma --- runtime/os_interface/debug_settings_manager.h | 25 +++++++++------- .../helpers/debug_manager_state_restore.h | 30 ++++++++----------- .../debug_settings_manager_tests.cpp | 6 ++++ 3 files changed, 33 insertions(+), 28 deletions(-) diff --git a/runtime/os_interface/debug_settings_manager.h b/runtime/os_interface/debug_settings_manager.h index da84b9948c..dc57b9d4fd 100644 --- a/runtime/os_interface/debug_settings_manager.h +++ b/runtime/os_interface/debug_settings_manager.h @@ -49,17 +49,20 @@ class SettingsReader; #define DECLARE_DEBUG_VARIABLE(dataType, variableName, defaultValue, description) \ struct DebugVar##variableName \ { \ - DebugVar##variableName() { \ - value = (dataType)defaultValue; \ - } \ - dataType get() const { \ - return value; \ - } \ - void set(dataType data) { \ - value = data; \ - } \ -private: \ - dataType value; \ + DebugVar##variableName() { \ + value = (dataType)defaultValue; \ + } \ + dataType get() const { \ + return value; \ + } \ + void set(dataType data) { \ + value = data; \ + } \ + dataType& getRef() { \ + return value; \ + } \ + private: \ + dataType value; \ }; #include "debug_variables.inl" diff --git a/unit_tests/helpers/debug_manager_state_restore.h b/unit_tests/helpers/debug_manager_state_restore.h index d6175294ed..53fb206617 100644 --- a/unit_tests/helpers/debug_manager_state_restore.h +++ b/unit_tests/helpers/debug_manager_state_restore.h @@ -1,23 +1,8 @@ /* - * Copyright (c) 2017, Intel Corporation + * Copyright (C) 2017-2018 Intel Corporation * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: + * SPDX-License-Identifier: MIT * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR - * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, - * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR - * OTHER DEALINGS IN THE SOFTWARE. */ #pragma once @@ -35,9 +20,20 @@ class DebugManagerStateRestore { ~DebugManagerStateRestore() { DebugManager.flags = debugVarSnapshot; DebugManager.injectFcn = injectFcnSnapshot; +#undef DECLARE_DEBUG_VARIABLE +#define DECLARE_DEBUG_VARIABLE(dataType, variableName, defaultValue, description) shrink(DebugManager.flags.variableName.getRef()); +#include "debug_variables.inl" +#undef DECLARE_DEBUG_VARIABLE } DebugSettingsManager::DebugVariables debugVarSnapshot; void *injectFcnSnapshot = nullptr; + + protected: + void shrink(std::string &flag) { + flag.shrink_to_fit(); + } + void shrink(int32_t &flag) {} + void shrink(bool &flag) {} }; class RegistryReaderMock : public SettingsReader { diff --git a/unit_tests/os_interface/debug_settings_manager_tests.cpp b/unit_tests/os_interface/debug_settings_manager_tests.cpp index c522293c96..c7a359206f 100644 --- a/unit_tests/os_interface/debug_settings_manager_tests.cpp +++ b/unit_tests/os_interface/debug_settings_manager_tests.cpp @@ -10,6 +10,7 @@ #include "gmock/gmock.h" #include "runtime/helpers/file_io.h" #include "runtime/helpers/string_helpers.h" +#include "unit_tests/helpers/debug_manager_state_restore.h" #include "runtime/os_interface/debug_settings_manager.h" #include "runtime/utilities/directory.h" #include "unit_tests/mocks/mock_kernel.h" @@ -941,3 +942,8 @@ TEST(DebugSettingsManager, givenTwoPossibleVariantsOfHardwareInfoOverrideStringT debugManager.getHardwareInfoOverride(hwInfoConfig); EXPECT_EQ(str1, hwInfoConfig); } + +TEST(DebugSettingsManager, givenStringDebugVariableWhenLongValueExeedingSmallStringOptimizationIsAssignedThenMemoryLeakIsNotReported) { + DebugManagerStateRestore debugManagerStateRestore; + DebugManager.flags.AUBDumpCaptureFileName.set("ThisIsVeryLongStringValueThatExceedSizeSpecifiedBySmallStringOptimizationAndCausesInternalStringBufferResize"); +} \ No newline at end of file