Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2018 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef OSRF_TESTING_TOOLS_CPP__MEMORY_TOOLS__IS_WORKING_HPP_
#define OSRF_TESTING_TOOLS_CPP__MEMORY_TOOLS__IS_WORKING_HPP_

#include <string>
#include <cstdlib>

#include "./visibility_control.hpp"

namespace osrf_testing_tools_cpp
{
namespace memory_tools
{

/// Return true if memory tools is enabled, installed (LD_PRELOAD was done), and working.
/**
* This works by temporarily installing a on_malloc hook and then calling
* malloc in a way that cannot be optimized out by the compiler.
*/
OSRF_TESTING_TOOLS_CPP_MEMORY_TOOLS_PUBLIC
bool
is_working();

/// Copy an input string into allocated memory, guaranteeing malloc is called.
/**
* Makes sure that the compiler doesn't optimize the malloc and free out.
* The content of the input string doesn't matter, but should be non-empty.
*/
OSRF_TESTING_TOOLS_CPP_MEMORY_TOOLS_PUBLIC
void
guaranteed_malloc(const std::string & str);

} // namespace memory_tools
} // namespace osrf_testing_tools_cpp

#endif // OSRF_TESTING_TOOLS_CPP__MEMORY_TOOLS__IS_WORKING_HPP_
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#define OSRF_TESTING_TOOLS_CPP__MEMORY_TOOLS__MEMORY_TOOLS_HPP_

#include "./initialize.hpp"
#include "./is_working.hpp"
#include "./memory_tools_service.hpp"
#include "./monitoring.hpp"
#include "./register_hooks.hpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ OSRF_TESTING_TOOLS_CPP_MEMORY_TOOLS_PUBLIC
void
on_malloc(AnyMemoryToolsCallback callback);

/// Get the current on_malloc callback if set, otherwise null.
OSRF_TESTING_TOOLS_CPP_MEMORY_TOOLS_PUBLIC
AnyMemoryToolsCallback
get_on_malloc();

/// Call the registered callback for malloc.
OSRF_TESTING_TOOLS_CPP_MEMORY_TOOLS_PUBLIC
void
Expand All @@ -65,6 +70,11 @@ OSRF_TESTING_TOOLS_CPP_MEMORY_TOOLS_PUBLIC
void
on_realloc(AnyMemoryToolsCallback callback);

/// Get the current on_realloc callback if set, otherwise null.
OSRF_TESTING_TOOLS_CPP_MEMORY_TOOLS_PUBLIC
AnyMemoryToolsCallback
get_on_realloc();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that we don't actually use get_on_realloc/get_on_calloc/get_on_free, should we bother exposing them? I generally like to only expose the things that I'm using, to keep the API smaller.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered the same thing, but I errored on the side of symmetry. So unless there's some other reason I'd opt to not touch it again.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fine, we can leave it.


/// Call the registered callback for realloc.
OSRF_TESTING_TOOLS_CPP_MEMORY_TOOLS_PUBLIC
void
Expand All @@ -80,6 +90,11 @@ OSRF_TESTING_TOOLS_CPP_MEMORY_TOOLS_PUBLIC
void
on_calloc(AnyMemoryToolsCallback callback);

/// Get the current on_calloc callback if set, otherwise null.
OSRF_TESTING_TOOLS_CPP_MEMORY_TOOLS_PUBLIC
AnyMemoryToolsCallback
get_on_calloc();

/// Call the registered callback for calloc.
OSRF_TESTING_TOOLS_CPP_MEMORY_TOOLS_PUBLIC
void
Expand All @@ -95,6 +110,11 @@ OSRF_TESTING_TOOLS_CPP_MEMORY_TOOLS_PUBLIC
void
on_free(AnyMemoryToolsCallback callback);

/// Get the current on_free callback if set, otherwise null.
OSRF_TESTING_TOOLS_CPP_MEMORY_TOOLS_PUBLIC
AnyMemoryToolsCallback
get_on_free();

/// Call the registered callback for free.
OSRF_TESTING_TOOLS_CPP_MEMORY_TOOLS_PUBLIC
void
Expand Down
1 change: 1 addition & 0 deletions osrf_testing_tools_cpp/src/memory_tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ add_library(memory_tools SHARED
custom_memory_functions.cpp
implementation_monitoring_override.cpp
initialize.cpp
is_working.cpp
memory_tools_service.cpp
monitoring.cpp
register_hooks.cpp
Expand Down
55 changes: 55 additions & 0 deletions osrf_testing_tools_cpp/src/memory_tools/is_working.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2018 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "osrf_testing_tools_cpp/memory_tools/is_working.hpp"

#include <string>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: #include <cstdlib> for std::malloc/std::free.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do.


#include "osrf_testing_tools_cpp/memory_tools/register_hooks.hpp"

namespace osrf_testing_tools_cpp
{
namespace memory_tools
{

volatile char side_effect[1024];

void
guaranteed_malloc(const std::string & str)
{
void * some_memory = std::malloc(1024);
// We need to do something with the malloc'ed memory to make sure this
// function doesn't get optimized away. memset isn't enough, so we do a
// memcpy from a passed in string, and then copy *that* out to an array that
// is globally visible (assuring we have a side-effect). This is enough to
// keep the optimizer away.
memcpy(some_memory, str.c_str(), str.length());
memcpy((void *)side_effect, some_memory, str.length());
std::free(some_memory);
}

bool
is_working()
{
auto original_on_malloc = get_on_malloc();
bool malloc_was_called = false;
on_malloc([&]() {malloc_was_called = true;});
std::string tmp("doesn't matter");
guaranteed_malloc(tmp);
on_malloc(original_on_malloc);
return malloc_was_called;
}

} // namespace memory_tools
} // namespace osrf_testing_tools_cpp
40 changes: 40 additions & 0 deletions osrf_testing_tools_cpp/src/memory_tools/register_hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ on_malloc(AnyMemoryToolsCallback callback)
}
}

AnyMemoryToolsCallback
get_on_malloc()
{
auto current = g_on_malloc_callback.load();
if (current) {
return *current;
}
return nullptr;
}

void
dispatch_malloc(MemoryToolsService & service)
{
Expand All @@ -53,6 +63,16 @@ on_realloc(AnyMemoryToolsCallback callback)
delete g_on_realloc_callback.exchange(new AnyMemoryToolsCallback(callback));
}

AnyMemoryToolsCallback
get_on_realloc()
{
auto current = g_on_realloc_callback.load();
if (current) {
return *current;
}
return nullptr;
}

void
dispatch_realloc(MemoryToolsService & service)
{
Expand All @@ -68,6 +88,16 @@ on_calloc(AnyMemoryToolsCallback callback)
delete g_on_calloc_callback.exchange(new AnyMemoryToolsCallback(callback));
}

AnyMemoryToolsCallback
get_on_calloc()
{
auto current = g_on_calloc_callback.load();
if (current) {
return *current;
}
return nullptr;
}

void
dispatch_calloc(MemoryToolsService & service)
{
Expand All @@ -83,6 +113,16 @@ on_free(AnyMemoryToolsCallback callback)
delete g_on_free_callback.exchange(new AnyMemoryToolsCallback(callback));
}

AnyMemoryToolsCallback
get_on_free()
{
auto current = g_on_free_callback.load();
if (current) {
return *current;
}
return nullptr;
}

void
dispatch_free(MemoryToolsService & service)
{
Expand Down
11 changes: 1 addition & 10 deletions osrf_testing_tools_cpp/test/memory_tools/test_memory_tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,18 +149,9 @@ TEST(TestMemoryTools, test_allocation_checking_tools) {
EXPECT_EQ(4u, unexpected_frees);
}

volatile char side_effect[1024];
void my_first_function(const std::string& str)
{
void * some_memory = std::malloc(1024);
// We need to do something with the malloc'ed memory to make sure this
// function doesn't get optimized away. memset isn't enough, so we do a
// memcpy from a passed in string, and then copy *that* out to an array that
// is globally visible (assuring we have a side-effect). This is enough to
// keep the optimizer away.
memcpy(some_memory, str.c_str(), str.length());
memcpy((void *)side_effect, some_memory, str.length());
std::free(some_memory);
osrf_testing_tools_cpp::memory_tools::guaranteed_malloc(str);
}

int my_second_function(int a, int b)
Expand Down
9 changes: 9 additions & 0 deletions test_osrf_testing_tools_cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,14 @@ if(BUILD_TESTING)
COMMAND "$<TARGET_FILE:test_example_memory_tools_gtest>"
ENV ${extra_env_vars}
)

add_executable(test_is_not_working_gtest test/test_is_not_working.cpp)
target_link_libraries(test_is_not_working_gtest
gtest_main
osrf_testing_tools_cpp::memory_tools
)
osrf_testing_tools_cpp_add_test(test_is_not_working_gtest
COMMAND "$<TARGET_FILE:test_is_not_working_gtest>"
)
endif()
endif()
14 changes: 3 additions & 11 deletions test_osrf_testing_tools_cpp/test/test_example_memory_tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,9 @@
#include "osrf_testing_tools_cpp/memory_tools/memory_tools.hpp"
#include "osrf_testing_tools_cpp/scope_exit.hpp"

volatile char side_effect[1024];
void my_first_function(const std::string& str)
void my_first_function(const std::string & str)
{
void * some_memory = std::malloc(1024);
// We need to do something with the malloc'ed memory to make sure this
// function doesn't get optimized away. memset isn't enough, so we do a
// memcpy from a passed in string, and then copy *that* out to an array that
// is globally visible (assuring we have a side-effect). This is enough to
// keep the optimizer away.
memcpy(some_memory, str.c_str(), str.length());
memcpy((void *)side_effect, some_memory, str.length());
std::free(some_memory);
osrf_testing_tools_cpp::memory_tools::guaranteed_malloc(str);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, I'll update it to modify that instance as well, thanks!

}

int my_second_function(int a, int b)
Expand Down Expand Up @@ -68,6 +59,7 @@ TEST(TestMemoryTools, test_example) {
// enabling monitoring will allow checking to begin, but the default state is
// that dynamic memory calls are expected, so again either function will pass
osrf_testing_tools_cpp::memory_tools::enable_monitoring();
ASSERT_TRUE(osrf_testing_tools_cpp::memory_tools::is_working());
my_first_function(dummy);
EXPECT_EQ(my_second_function(1, 2), 3);

Expand Down
32 changes: 32 additions & 0 deletions test_osrf_testing_tools_cpp/test/test_is_not_working.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2015 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <gtest/gtest.h>
#include <gtest/gtest-spi.h>

#include "osrf_testing_tools_cpp/memory_tools/memory_tools.hpp"
#include "osrf_testing_tools_cpp/scope_exit.hpp"

TEST(TestMemoryTools, test_is_not_working) {

// you must initialize memory tools, but uninitialization is optional
osrf_testing_tools_cpp::memory_tools::initialize();
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({
osrf_testing_tools_cpp::memory_tools::uninitialize();
});

osrf_testing_tools_cpp::memory_tools::enable_monitoring();

ASSERT_FALSE(osrf_testing_tools_cpp::memory_tools::is_working());
}