Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 54a16104 authored by Colin Cross's avatar Colin Cross
Browse files

Compile some tests for the host

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

Bug: 27208635
Change-Id: Ib46fcfa333ceb721f26efca00b2fa60b9fba44e6
(cherry picked from commit b8e20f55)
parent a6680f78
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -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_);
  }
+17 −0
Original line number Diff line number Diff line
@@ -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)
+1 −0
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@
#define LIBMEMUNREACHABLE_SCOPED_ALARM_H_

#include <signal.h>
#include <sys/time.h>

#include <chrono>
#include <functional>
+2 −0
Original line number Diff line number Diff line
@@ -18,6 +18,8 @@
#define LIBMEMUNREACHABLE_BIONIC_H_

#include <sys/cdefs.h>
#include <stdint.h>
#include <stdlib.h>

__BEGIN_DECLS

+0 −99
Original line number Diff line number Diff line
@@ -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();
    }
  }, "");
}
Loading