1 /**
2 * Copyright (c) 2023 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 <malloc.h>
17 #include <stdint.h>
18 #include <sys/wait.h>
19 #include "gwp_asan_test.h"
20 #include "test.h"
21
22 #define NUM_OF_MALLOC_FOR_OOM 10000
23 #define SIZE_OF_SINGLE_MALLOC 20
24
test_gwp_asan_oom()25 void test_gwp_asan_oom()
26 {
27 if (libc_gwp_asan_has_free_mem()) {
28 for (size_t i = 0; i < NUM_OF_MALLOC_FOR_OOM; i++) {
29 void *ptr = malloc(SIZE_OF_SINGLE_MALLOC);
30 if (!ptr) {
31 t_error("FAIL malloc failed!\n");
32 }
33 }
34 } else {
35 t_error("FAIL can't use gwp_asan!\n");
36 }
37 if (!libc_gwp_asan_has_free_mem()) {
38 // test we can call default malloc when gwp_asan doesn't have free mem.
39 void *ptr = malloc(SIZE_OF_SINGLE_MALLOC);
40 if (!ptr) {
41 t_error("FAIL malloc failed!\n");
42 }
43 if (libc_gwp_asan_ptr_is_mine(ptr)) {
44 t_error("FAIL this addr(%p) shouldn't in gwp_asan area!\n", ptr);
45 } else {
46 printf("It's ok call default malloc\n");
47 }
48 } else {
49 t_error("FAIL gwp_asan doesn't oom!\n");
50 }
51 }
52
main()53 int main()
54 {
55 config_gwp_asan_environment(false);
56
57 void *ptr = malloc(2);
58 if (!ptr) {
59 t_error("malloc failed.");
60 cancel_gwp_asan_environment(false);
61 return t_status;
62 }
63 if (!libc_gwp_asan_ptr_is_mine(ptr)) {
64 t_error("Memory is not allocated by gwp_asan.");
65 cancel_gwp_asan_environment(false);
66 return t_status;
67 }
68 int size = malloc_usable_size(ptr);
69 void *new_addr = realloc(ptr, 40);
70 if (!new_addr) {
71 t_error("realloc failed.");
72 cancel_gwp_asan_environment(false);
73 return t_status;
74 }
75 char c = *(char *)new_addr;
76 printf("c:%c size:%d.\n", c, size);
77
78 void *calloc_ptr = calloc(5, 4);
79 if (!calloc_ptr) {
80 t_error("calloc failed.");
81 cancel_gwp_asan_environment(false);
82 return t_status;
83 }
84 int value = *(int *)calloc_ptr;
85 if (value != 0) {
86 t_error("FAIL the memory value of gwp_asan calloc isn't 0 get %d", value);
87 }
88
89 free(new_addr);
90
91 pid_t pid = fork();
92 if (pid < 0) {
93 t_error("FAIL fork failed.");
94 } else if (pid == 0) { // child process
95 test_gwp_asan_oom();
96 } else { // parent process
97 int status;
98 if (waitpid(pid, &status, 0) != pid) {
99 t_error("gwp_asan_smoke_test waitpid failed.");
100 }
101
102 if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
103 t_error("gwp_asan_smoke_test failed.");
104 }
105
106 cancel_gwp_asan_environment(false);
107 }
108
109 return t_status;
110 }