1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include <dlfcn.h> 17 #include <hwext/gtest-ext.h> 18 #include <hwext/gtest-tag.h> 19 20 #include "hook_client.h" 21 #include "musl_preinit_common.h" 22 23 using namespace testing::ext; 24 25 namespace { 26 constexpr uint32_t g_size = 1024; 27 constexpr uint32_t g_resize = 2048; 28 29 class NativeHookTest : public ::testing::Test { 30 public: SetUpTestCase()31 static void SetUpTestCase() {} TearDownTestCase()32 static void TearDownTestCase() {} SetUp()33 void SetUp() {} TearDown()34 void TearDown() {} 35 }; 36 37 /** 38 * @tc.name: native hook 39 * @tc.desc: Test hook malloc normal process. 40 * @tc.type: FUNC 41 */ 42 HWTEST_F(NativeHookTest, NormalMallocHookTest, TestSize.Level1) 43 { 44 EXPECT_TRUE(ohos_malloc_hook_initialize(&__libc_malloc_default_dispatch, nullptr, nullptr)); 45 EXPECT_TRUE(ohos_malloc_hook_on_start()); 46 47 void* mallocBlack = ohos_malloc_hook_malloc(g_size); 48 EXPECT_NE(mallocBlack, nullptr); 49 50 ohos_malloc_hook_free(mallocBlack); 51 52 EXPECT_TRUE(ohos_malloc_hook_on_end()); 53 } 54 55 /** 56 * @tc.name: native hook 57 * @tc.desc: Test hook realloc normal process. 58 * @tc.type: FUNC 59 */ 60 HWTEST_F(NativeHookTest, NormalHookReallocTest, TestSize.Level1) 61 { 62 EXPECT_TRUE(ohos_malloc_hook_initialize(&__libc_malloc_default_dispatch, nullptr, nullptr)); 63 EXPECT_TRUE(ohos_malloc_hook_on_start()); 64 65 void* mallocBlack = ohos_malloc_hook_malloc(g_size); 66 EXPECT_NE(mallocBlack, nullptr); 67 void* reallocBlack = ohos_malloc_hook_realloc(mallocBlack, g_resize); 68 EXPECT_NE(reallocBlack, nullptr); 69 70 ohos_malloc_hook_free(reallocBlack); 71 72 EXPECT_TRUE(ohos_malloc_hook_on_end()); 73 } 74 75 /** 76 * @tc.name: native hook 77 * @tc.desc: Test hook calloc normal process. 78 * @tc.type: FUNC 79 */ 80 HWTEST_F(NativeHookTest, NormalHookCallocTest, TestSize.Level1) 81 { 82 EXPECT_TRUE(ohos_malloc_hook_initialize(&__libc_malloc_default_dispatch, nullptr, nullptr)); 83 EXPECT_TRUE(ohos_malloc_hook_on_start()); 84 85 void* callocBlack = ohos_malloc_hook_calloc(g_size, g_resize); 86 EXPECT_NE(callocBlack, nullptr); 87 88 ohos_malloc_hook_free(callocBlack); 89 90 EXPECT_TRUE(ohos_malloc_hook_on_end()); 91 } 92 93 /** 94 * @tc.name: native hook 95 * @tc.desc: Test hook valloc normal process. 96 * @tc.type: FUNC 97 */ 98 HWTEST_F(NativeHookTest, NormalHookVallocTest, TestSize.Level1) 99 { 100 EXPECT_TRUE(ohos_malloc_hook_initialize(&__libc_malloc_default_dispatch, nullptr, nullptr)); 101 EXPECT_TRUE(ohos_malloc_hook_on_start()); 102 103 void* vallocBlack = ohos_malloc_hook_valloc(g_size); 104 EXPECT_EQ(vallocBlack, nullptr); 105 106 ohos_malloc_hook_free(vallocBlack); 107 108 EXPECT_TRUE(ohos_malloc_hook_on_end()); 109 } 110 111 /** 112 * @tc.name: native hook 113 * @tc.desc: Test hook memalign normal process. 114 * @tc.type: FUNC 115 */ 116 HWTEST_F(NativeHookTest, NormalHookMemalignTest, TestSize.Level1) 117 { 118 EXPECT_TRUE(ohos_malloc_hook_initialize(&__libc_malloc_default_dispatch, nullptr, nullptr)); 119 EXPECT_TRUE(ohos_malloc_hook_on_start()); 120 121 void* memalignBlack = ohos_malloc_hook_memalign(g_size, g_resize); 122 EXPECT_EQ(memalignBlack, nullptr); 123 EXPECT_EQ(static_cast<int>(ohos_malloc_hook_malloc_usable_size(memalignBlack)), 0); 124 125 ohos_malloc_hook_free(memalignBlack); 126 127 EXPECT_TRUE(ohos_malloc_hook_on_end()); 128 } 129 130 /** 131 * @tc.name: native hook 132 * @tc.desc: Test multi hook malloc normal process. 133 * @tc.type: FUNC 134 */ 135 HWTEST_F(NativeHookTest, NormalTest, TestSize.Level1) 136 { 137 EXPECT_TRUE(ohos_malloc_hook_initialize(&__libc_malloc_default_dispatch, nullptr, nullptr)); 138 EXPECT_TRUE(ohos_malloc_hook_on_start()); 139 140 void* mallocBlack = ohos_malloc_hook_malloc(g_size); 141 EXPECT_NE(mallocBlack, nullptr); 142 void* reallocBlack = ohos_malloc_hook_realloc(mallocBlack, g_resize); 143 EXPECT_NE(reallocBlack, nullptr); 144 void* callocBlack = ohos_malloc_hook_calloc(g_size, g_resize); 145 EXPECT_NE(callocBlack, nullptr); 146 void* vallocBlack = ohos_malloc_hook_valloc(g_size); 147 EXPECT_EQ(vallocBlack, nullptr); 148 void* memalignBlack = ohos_malloc_hook_memalign(g_size, g_resize); 149 EXPECT_EQ(memalignBlack, nullptr); 150 EXPECT_EQ(static_cast<int>(ohos_malloc_hook_malloc_usable_size(memalignBlack)), 0); 151 152 ohos_malloc_hook_free(memalignBlack); 153 ohos_malloc_hook_free(vallocBlack); 154 ohos_malloc_hook_free(callocBlack); 155 ohos_malloc_hook_free(reallocBlack); 156 157 EXPECT_TRUE(ohos_malloc_hook_on_end()); 158 } 159 160 /** 161 * @tc.name: native hook 162 * @tc.desc: Test other process. 163 * @tc.type: FUNC 164 */ 165 HWTEST_F(NativeHookTest, NormalOtherTest, TestSize.Level1) 166 { 167 EXPECT_TRUE(ohos_malloc_hook_initialize(&__libc_malloc_default_dispatch, nullptr, nullptr)); 168 EXPECT_TRUE(ohos_malloc_hook_on_start()); 169 170 ohos_malloc_hook_finalize(); 171 172 EXPECT_TRUE(ohos_malloc_hook_on_end()); 173 } 174 175 /** 176 * @tc.name: native hook 177 * @tc.desc: Test failure process. 178 * @tc.type: FUNC 179 */ 180 HWTEST_F(NativeHookTest, FailureTest, TestSize.Level1) 181 { 182 EXPECT_TRUE(ohos_malloc_hook_initialize(nullptr, nullptr, nullptr)); 183 EXPECT_TRUE(ohos_malloc_hook_on_start()); 184 EXPECT_TRUE(ohos_malloc_hook_on_end()); 185 } 186 187 /** 188 * @tc.name: native hook 189 * @tc.desc: Test set and get malloc hook flag. 190 * @tc.type: FUNC 191 */ 192 HWTEST_F(NativeHookTest, MallocHookFlag, TestSize.Level1) 193 { 194 bool flag = true; 195 bool flagPrev = ohos_malloc_hook_set_hook_flag(flag); 196 197 EXPECT_EQ(ohos_malloc_hook_get_hook_flag(), flag); 198 EXPECT_EQ(ohos_malloc_hook_set_hook_flag(flagPrev), flag); 199 EXPECT_EQ(ohos_malloc_hook_get_hook_flag(), flagPrev); 200 } 201 } // namespace 202