133c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris/*
233c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris * Copyright (C) 2015 The Android Open Source Project
333c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris *
433c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris * Licensed under the Apache License, Version 2.0 (the "License");
533c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris * you may not use this file except in compliance with the License.
633c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris * You may obtain a copy of the License at
733c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris *
833c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris *      http://www.apache.org/licenses/LICENSE-2.0
933c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris *
1033c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris * Unless required by applicable law or agreed to in writing, software
1133c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris * distributed under the License is distributed on an "AS IS" BASIS,
1233c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1333c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris * See the License for the specific language governing permissions and
1433c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris * limitations under the License.
1533c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris */
1633c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris
1733c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris#include <gtest/gtest.h>
1833c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris#include <stdint.h>
1933c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris#include <string.h>
2033c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris
2133c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris#include "Action.h"
2233c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris#include "Pointers.h"
2333c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris
2433c03d38298ff359d8829aea91bb02b26caab245Christopher FerrisTEST(ActionTest, malloc) {
2533c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  uint8_t memory[Action::MaxActionSize()];
2633c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  const char* line = "1024";
2733c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  Action* action = Action::CreateAction(0x1234, "malloc", line, memory);
2833c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  ASSERT_TRUE(action != NULL);
2933c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  ASSERT_FALSE(action->DoesFree());
3033c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  ASSERT_FALSE(action->EndThread());
3133c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris
3233c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  Pointers pointers(1);
3333c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  action->Execute(&pointers);
3433c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  void* pointer = pointers.Remove(0x1234);
3533c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  ASSERT_TRUE(pointer != nullptr);
3633c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  free(pointer);
3733c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris}
3833c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris
3933c03d38298ff359d8829aea91bb02b26caab245Christopher FerrisTEST(ActionTest, malloc_malformed) {
4033c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  uint8_t memory[128];
4133c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  const char* line = "";
4233c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  Action* action = Action::CreateAction(0x1234, "malloc", line, memory);
4333c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  ASSERT_FALSE(action != NULL);
4433c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris}
4533c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris
4633c03d38298ff359d8829aea91bb02b26caab245Christopher FerrisTEST(ActionTest, free) {
4733c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  uint8_t memory[128];
4833c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  const char* line = "";
4933c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  Action* action = Action::CreateAction(0x1234, "free", line, memory);
5033c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  ASSERT_TRUE(action != NULL);
5133c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  ASSERT_TRUE(action->DoesFree());
5233c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  ASSERT_FALSE(action->EndThread());
5333c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris
5433c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  Pointers pointers(1);
5533c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  pointers.Add(0x1234, malloc(10));
5633c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  action->Execute(&pointers);
5733c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris}
5833c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris
5933c03d38298ff359d8829aea91bb02b26caab245Christopher FerrisTEST(ActionTest, calloc) {
6033c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  uint8_t memory[128];
6133c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  const char* line = "100 10";
6233c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  Action* action = Action::CreateAction(0x1234, "calloc", line, memory);
6333c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  ASSERT_TRUE(action != NULL);
6433c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  ASSERT_FALSE(action->DoesFree());
6533c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  ASSERT_FALSE(action->EndThread());
6633c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris
6733c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  Pointers pointers(1);
6833c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  action->Execute(&pointers);
6933c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  void* pointer = pointers.Remove(0x1234);
7033c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  ASSERT_TRUE(pointer != nullptr);
7133c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  free(pointer);
7233c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris}
7333c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris
7433c03d38298ff359d8829aea91bb02b26caab245Christopher FerrisTEST(ActionTest, free_zero) {
7533c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  uint8_t memory[128];
7633c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  const char* line = "";
7733c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  Action* action = Action::CreateAction(0, "free", line, memory);
7833c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  ASSERT_TRUE(action != NULL);
7933c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  ASSERT_FALSE(action->DoesFree());
8033c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  ASSERT_FALSE(action->EndThread());
8133c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  // Should be a nop.
8233c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  action->Execute(nullptr);
8333c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris}
8433c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris
8533c03d38298ff359d8829aea91bb02b26caab245Christopher FerrisTEST(ActionTest, calloc_malformed) {
8633c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  uint8_t memory[128];
8733c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  const char* line1 = "100";
8833c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  Action* action = Action::CreateAction(0x1234, "calloc", line1, memory);
8933c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  ASSERT_FALSE(action != NULL);
9033c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris
9133c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  const char* line2 = "";
9233c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  action = Action::CreateAction(0x1234, "calloc", line2, memory);
9333c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  ASSERT_FALSE(action != NULL);
9433c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris}
9533c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris
9633c03d38298ff359d8829aea91bb02b26caab245Christopher FerrisTEST(ActionTest, realloc) {
9733c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  uint8_t memory[128];
9833c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  const char* line = "0xabcd 100";
9933c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  Action* action = Action::CreateAction(0x1234, "realloc", line, memory);
10033c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  ASSERT_TRUE(action != NULL);
10133c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  ASSERT_TRUE(action->DoesFree());
10233c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  ASSERT_FALSE(action->EndThread());
10333c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris
10433c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  Pointers pointers(1);
10533c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  pointers.Add(0xabcd, malloc(10));
10633c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  action->Execute(&pointers);
10733c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  void* pointer = pointers.Remove(0x1234);
10833c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  ASSERT_TRUE(pointer != nullptr);
10933c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  free(pointer);
11033c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris
11133c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  const char* null_line = "0x0 100";
11233c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  action = Action::CreateAction(0x1234, "realloc", null_line, memory);
11333c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  ASSERT_FALSE(action->DoesFree());
11433c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  ASSERT_FALSE(action->EndThread());
11533c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris
11633c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  action->Execute(&pointers);
11733c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  pointer = pointers.Remove(0x1234);
11833c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  ASSERT_TRUE(pointer != nullptr);
11933c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  free(pointer);
12033c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris}
12133c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris
12233c03d38298ff359d8829aea91bb02b26caab245Christopher FerrisTEST(ActionTest, realloc_malformed) {
12333c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  uint8_t memory[128];
12433c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  const char* line1 = "0x100";
12533c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  Action* action = Action::CreateAction(0x1234, "realloc", line1, memory);
12633c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  ASSERT_FALSE(action != NULL);
12733c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris
12833c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  const char* line2 = "";
12933c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  action = Action::CreateAction(0x1234, "realloc", line2, memory);
13033c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  ASSERT_FALSE(action != NULL);
13133c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris}
13233c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris
13333c03d38298ff359d8829aea91bb02b26caab245Christopher FerrisTEST(ActionTest, memalign) {
13433c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  uint8_t memory[128];
13533c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  const char* line = "16 300";
13633c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  Action* action = Action::CreateAction(0x1234, "memalign", line, memory);
13733c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  ASSERT_TRUE(action != NULL);
13833c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  ASSERT_FALSE(action->DoesFree());
13933c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  ASSERT_FALSE(action->EndThread());
14033c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris
14133c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  Pointers pointers(1);
14233c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  action->Execute(&pointers);
14333c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  void* pointer = pointers.Remove(0x1234);
14433c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  ASSERT_TRUE(pointer != nullptr);
14533c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  free(pointer);
14633c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris}
14733c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris
14833c03d38298ff359d8829aea91bb02b26caab245Christopher FerrisTEST(ActionTest, memalign_malformed) {
14933c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  uint8_t memory[128];
15033c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  const char* line1 = "100";
15133c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  Action* action = Action::CreateAction(0x1234, "memalign", line1, memory);
15233c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  ASSERT_FALSE(action != NULL);
15333c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris
15433c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  const char* line2 = "";
15533c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  action = Action::CreateAction(0x1234, "memalign", line2, memory);
15633c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  ASSERT_FALSE(action != NULL);
15733c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris}
15833c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris
15933c03d38298ff359d8829aea91bb02b26caab245Christopher FerrisTEST(ActionTest, endthread) {
16033c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  uint8_t memory[128];
16133c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  const char* line = "";
16233c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  Action* action = Action::CreateAction(0x0, "thread_done", line, memory);
16333c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  ASSERT_TRUE(action != NULL);
16433c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  ASSERT_FALSE(action->DoesFree());
16533c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  ASSERT_TRUE(action->EndThread());
16633c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris
16733c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris  action->Execute(nullptr);
16833c03d38298ff359d8829aea91bb02b26caab245Christopher Ferris}
169