Compile some tests for the host

Compile some tests for the host to ease debugging with valgrind or gdb.

Bug: 27208635
Change-Id: Ib46fcfa333ceb721f26efca00b2fa60b9fba44e6
This commit is contained in:
Colin Cross 2016-03-02 17:52:56 -08:00
parent a996c2963a
commit b8e20f557f
8 changed files with 165 additions and 105 deletions

View File

@ -370,11 +370,11 @@ void* HeapImpl::Alloc(size_t size) {
}
void* HeapImpl::AllocLocked(size_t size) {
if (__predict_false(size > kMaxBucketAllocationSize)) {
if (size > kMaxBucketAllocationSize) {
return MapAlloc(size);
}
int bucket = size_to_bucket(size);
if (__predict_false(free_chunks_[bucket].empty())) {
if (free_chunks_[bucket].empty()) {
Chunk *chunk = new Chunk(this, bucket);
free_chunks_[bucket].insert(chunk->node_);
}

View File

@ -12,6 +12,7 @@ memunreachable_srcs := \
memunreachable_test_srcs := \
tests/Allocator_test.cpp \
tests/DisableMalloc_test.cpp \
tests/HeapWalker_test.cpp \
tests/MemUnreachable_test.cpp \
tests/ThreadCapture_test.cpp \
@ -41,3 +42,19 @@ LOCAL_CLANG := true
LOCAL_SHARED_LIBRARIES := libmemunreachable libbase liblog
include $(BUILD_NATIVE_TEST)
include $(CLEAR_VARS)
LOCAL_MODULE := memunreachable_test
LOCAL_SRC_FILES := \
Allocator.cpp \
HeapWalker.cpp \
tests/Allocator_test.cpp \
tests/HeapWalker_test.cpp \
tests/HostMallocStub.cpp \
LOCAL_CFLAGS := -std=c++14 -Wall -Wextra -Werror
LOCAL_CLANG := true
LOCAL_SHARED_LIBRARIES := libbase liblog
include $(BUILD_HOST_NATIVE_TEST)

View File

@ -18,6 +18,7 @@
#define LIBMEMUNREACHABLE_SCOPED_ALARM_H_
#include <signal.h>
#include <sys/time.h>
#include <chrono>
#include <functional>

View File

@ -18,6 +18,8 @@
#define LIBMEMUNREACHABLE_BIONIC_H_
#include <sys/cdefs.h>
#include <stdint.h>
#include <stdlib.h>
__BEGIN_DECLS

View File

@ -15,12 +15,6 @@
*/
#include <Allocator.h>
#include <sys/time.h>
#include <chrono>
#include <functional>
#include <list>
#include <vector>
#include <gtest/gtest.h>
#include <ScopedDisableMalloc.h>
@ -28,8 +22,6 @@
std::function<void()> ScopedAlarm::func_;
using namespace std::chrono_literals;
class AllocatorTest : public testing::Test {
protected:
AllocatorTest() : heap(), disable_malloc_() {}
@ -180,94 +172,3 @@ TEST_F(AllocatorTest, unique) {
ASSERT_NE(ptr, nullptr);
}
class DisableMallocTest : public ::testing::Test {
protected:
void alarm(std::chrono::microseconds us) {
std::chrono::seconds s = std::chrono::duration_cast<std::chrono::seconds>(us);
itimerval t = itimerval();
t.it_value.tv_sec = s.count();
t.it_value.tv_usec = (us - s).count();
setitimer(ITIMER_REAL, &t, NULL);
}
};
TEST_F(DisableMallocTest, reenable) {
ASSERT_EXIT({
alarm(100ms);
void *ptr1 = malloc(128);
ASSERT_NE(ptr1, nullptr);
free(ptr1);
{
ScopedDisableMalloc disable_malloc;
}
void *ptr2 = malloc(128);
ASSERT_NE(ptr2, nullptr);
free(ptr2);
_exit(1);
}, ::testing::ExitedWithCode(1), "");
}
TEST_F(DisableMallocTest, deadlock_allocate) {
ASSERT_DEATH({
void *ptr = malloc(128);
ASSERT_NE(ptr, nullptr);
free(ptr);
{
alarm(100ms);
ScopedDisableMalloc disable_malloc;
void* ptr = malloc(128);
ASSERT_NE(ptr, nullptr);
free(ptr);
}
}, "");
}
TEST_F(DisableMallocTest, deadlock_new) {
ASSERT_DEATH({
char* ptr = new(char);
ASSERT_NE(ptr, nullptr);
delete(ptr);
{
alarm(100ms);
ScopedDisableMalloc disable_malloc;
char* ptr = new(char);
ASSERT_NE(ptr, nullptr);
delete(ptr);
}
}, "");
}
TEST_F(DisableMallocTest, deadlock_delete) {
ASSERT_DEATH({
char* ptr = new(char);
ASSERT_NE(ptr, nullptr);
{
alarm(250ms);
ScopedDisableMalloc disable_malloc;
delete(ptr);
}
}, "");
}
TEST_F(DisableMallocTest, deadlock_free) {
ASSERT_DEATH({
void *ptr = malloc(128);
ASSERT_NE(ptr, nullptr);
{
alarm(100ms);
ScopedDisableMalloc disable_malloc;
free(ptr);
}
}, "");
}
TEST_F(DisableMallocTest, deadlock_fork) {
ASSERT_DEATH({
{
alarm(100ms);
ScopedDisableMalloc disable_malloc;
fork();
}
}, "");
}

View File

@ -0,0 +1,116 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* 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 <sys/time.h>
#include <chrono>
#include <functional>
#include <gtest/gtest.h>
#include <ScopedDisableMalloc.h>
using namespace std::chrono_literals;
class DisableMallocTest : public ::testing::Test {
protected:
void alarm(std::chrono::microseconds us) {
std::chrono::seconds s = std::chrono::duration_cast<std::chrono::seconds>(us);
itimerval t = itimerval();
t.it_value.tv_sec = s.count();
t.it_value.tv_usec = (us - s).count();
setitimer(ITIMER_REAL, &t, NULL);
}
};
TEST_F(DisableMallocTest, reenable) {
ASSERT_EXIT({
alarm(100ms);
void *ptr1 = malloc(128);
ASSERT_NE(ptr1, nullptr);
free(ptr1);
{
ScopedDisableMalloc disable_malloc;
}
void *ptr2 = malloc(128);
ASSERT_NE(ptr2, nullptr);
free(ptr2);
_exit(1);
}, ::testing::ExitedWithCode(1), "");
}
TEST_F(DisableMallocTest, deadlock_allocate) {
ASSERT_DEATH({
void *ptr = malloc(128);
ASSERT_NE(ptr, nullptr);
free(ptr);
{
alarm(100ms);
ScopedDisableMalloc disable_malloc;
void* ptr = malloc(128);
ASSERT_NE(ptr, nullptr);
free(ptr);
}
}, "");
}
TEST_F(DisableMallocTest, deadlock_new) {
ASSERT_DEATH({
char* ptr = new(char);
ASSERT_NE(ptr, nullptr);
delete(ptr);
{
alarm(100ms);
ScopedDisableMalloc disable_malloc;
char* ptr = new(char);
ASSERT_NE(ptr, nullptr);
delete(ptr);
}
}, "");
}
TEST_F(DisableMallocTest, deadlock_delete) {
ASSERT_DEATH({
char* ptr = new(char);
ASSERT_NE(ptr, nullptr);
{
alarm(250ms);
ScopedDisableMalloc disable_malloc;
delete(ptr);
}
}, "");
}
TEST_F(DisableMallocTest, deadlock_free) {
ASSERT_DEATH({
void *ptr = malloc(128);
ASSERT_NE(ptr, nullptr);
{
alarm(100ms);
ScopedDisableMalloc disable_malloc;
free(ptr);
}
}, "");
}
TEST_F(DisableMallocTest, deadlock_fork) {
ASSERT_DEATH({
{
alarm(100ms);
ScopedDisableMalloc disable_malloc;
fork();
}
}, "");
}

View File

@ -107,8 +107,8 @@ TEST_F(HeapWalkerTest, live) {
heap_walker.Root(buffer_begin(buffer1), buffer_end(buffer1));
allocator::vector<Range> leaked(heap_);
size_t num_leaks = SIZE_T_MAX;
size_t leaked_bytes = SIZE_T_MAX;
size_t num_leaks = SIZE_MAX;
size_t leaked_bytes = SIZE_MAX;
ASSERT_EQ(true, heap_walker.Leaked(leaked, 100, &num_leaks, &leaked_bytes));
EXPECT_EQ(0U, num_leaks);
@ -133,8 +133,8 @@ TEST_F(HeapWalkerTest, unaligned) {
heap_walker.Root(buffer_begin(buffer1) + i, buffer_end(buffer1) - j);
allocator::vector<Range> leaked(heap_);
size_t num_leaks = SIZE_T_MAX;
size_t leaked_bytes = SIZE_T_MAX;
size_t num_leaks = SIZE_MAX;
size_t leaked_bytes = SIZE_MAX;
ASSERT_EQ(true, heap_walker.Leaked(leaked, 100, &num_leaks, &leaked_bytes));
EXPECT_EQ(0U, num_leaks);

View File

@ -0,0 +1,23 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* 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 "bionic.h"
void malloc_disable() {
}
void malloc_enable() {
}