1
+ // Copyright (C) 2024 Intel Corporation
2
+ // Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
3
+ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
+
5
+ #include " utils.hpp"
6
+ #include < iostream>
7
+
8
+ namespace fuzz {
9
+
10
+ constexpr int MAX_PROVIDER_VECTOR_SIZE = 1024 ;
11
+ constexpr int MAX_POOLS_VECTOR_SIZE = 20 ;
12
+ constexpr int MAX_POOLS_ALLOC_SIZE = 1 * 1024 ; // 1 kB
13
+ constexpr int MAX_PROVIDER_ALLOC_SIZE = 100 * 1024 ; // 100 kB
14
+
15
+ int umf_memory_provider_create (TestState &test_state) {
16
+ std::cout << " begin umf_memory_provider_create" << std::endl;
17
+ umf_memory_provider_ops_t *provider_ops = umfOsMemoryProviderOps ();
18
+ umf_os_memory_provider_params_t params = umfOsMemoryProviderParamsDefault ();
19
+ umf_result_t res =
20
+ umfMemoryProviderCreate (provider_ops, ¶ms, &test_state.provider );
21
+
22
+ if (res != UMF_RESULT_SUCCESS) {
23
+ std::cout << " Failed to create a memory provider! " << res << std::endl;
24
+ return -1 ;
25
+ }
26
+ std::cout << " OS memory provider created at " << (void *)test_state.provider
27
+ << std::endl;
28
+ return 0 ;
29
+ }
30
+
31
+ int get_alloc_size (TestState &state, size_t &alloc_size, int max_alloc_size) {
32
+ if (state.get_next_input_data_in_range <size_t >(&alloc_size, 0 ,
33
+ max_alloc_size) != 0 ) {
34
+ return -1 ;
35
+ }
36
+ return 0 ;
37
+ }
38
+
39
+ int umf_memory_provider_alloc (TestState &test_state) {
40
+ std::cout << " begin umf_memory_provider_alloc" << std::endl;
41
+ void *ptr;
42
+ size_t alloc_size;
43
+ constexpr size_t alignment = 0 ;
44
+
45
+ if (test_state.allocated_memory .size () >= MAX_PROVIDER_VECTOR_SIZE) {
46
+ return -1 ;
47
+ }
48
+
49
+ int ret = get_alloc_size (test_state, alloc_size, MAX_PROVIDER_ALLOC_SIZE);
50
+ if (ret != 0 ) {
51
+ return -1 ;
52
+ }
53
+
54
+ umf_result_t res = umfMemoryProviderAlloc (test_state.provider , alloc_size,
55
+ alignment, &ptr);
56
+ if (res != UMF_RESULT_SUCCESS) {
57
+ std::cout << " Failed to create a memory provider! " << res << std::endl;
58
+ return -1 ;
59
+ }
60
+ test_state.allocated_memory .push_back (std::make_pair (ptr, alloc_size));
61
+ std::cout << " Allocated memory at " << ptr << " with alloc_size "
62
+ << alloc_size << std::endl;
63
+ std::cout << " Vector size is: " << test_state.allocated_memory .size ()
64
+ << std::endl;
65
+ return 0 ;
66
+ }
67
+
68
+ int umf_memory_provider_free (TestState &test_state) {
69
+ printf (" begin umf_memory_provider_free\n " );
70
+ if (test_state.allocated_memory .empty ()) {
71
+ return -1 ;
72
+ }
73
+
74
+ std::pair<void *, size_t > alloc = test_state.allocated_memory .back ();
75
+ umf_result_t res =
76
+ umfMemoryProviderFree (test_state.provider , alloc.first , alloc.second );
77
+
78
+ if (res != UMF_RESULT_SUCCESS) {
79
+ std::cout << " Failed to free memory to the provider! " << res
80
+ << std::endl;
81
+ ;
82
+ return -1 ;
83
+ }
84
+
85
+ std::cout << " Freed memory at " << alloc.first << " with alloc_size "
86
+ << alloc.second << std::endl;
87
+ test_state.allocated_memory .pop_back ();
88
+ return 0 ;
89
+ }
90
+
91
+ int umf_pool_create (TestState &test_state) {
92
+ if (test_state.pools .size () > MAX_POOLS_VECTOR_SIZE) {
93
+ return -1 ;
94
+ }
95
+
96
+ umf_memory_pool_ops_t *pool_ops = umfScalablePoolOps ();
97
+ void *pool_params = NULL ;
98
+ umf_pool_create_flags_t flags = 0 ;
99
+ umf_memory_pool_handle_t pool;
100
+ umf_result_t res =
101
+ umfPoolCreate (pool_ops, test_state.provider , pool_params, flags, &pool);
102
+
103
+ if (res != UMF_RESULT_SUCCESS) {
104
+ std::cout << " Failed to create a pool! " << res << std::endl;
105
+ return -1 ;
106
+ }
107
+
108
+ test_state.pools .push_back (pool);
109
+ std::cout << " Scalable memory pool created at " << pool
110
+ << " and Pools' vector size is: " << test_state.pools .size ()
111
+ << std::endl;
112
+ return 0 ;
113
+ }
114
+
115
+ int umf_pool_destroy (TestState &test_state) {
116
+ if (test_state.pools .empty ()) {
117
+ return -1 ;
118
+ }
119
+ umfPoolDestroy (test_state.pools .back ());
120
+ test_state.pools .pop_back ();
121
+ return 0 ;
122
+ }
123
+
124
+ int umf_pool_malloc (TestState &test_state) {
125
+ if (test_state.pools .empty ()) {
126
+ return -1 ;
127
+ }
128
+ size_t alloc_size;
129
+ int ret = get_alloc_size (test_state, alloc_size, MAX_POOLS_ALLOC_SIZE);
130
+ if (ret != 0 ) {
131
+ return -1 ;
132
+ }
133
+
134
+ void *ptr = umfPoolMalloc (test_state.pools .back (), alloc_size);
135
+ if (!ptr) {
136
+ std::cout << " Failed to allocate memory in the pool!" << std::endl;
137
+ return -1 ;
138
+ }
139
+ test_state.pools_alloc .push_back (ptr);
140
+ return 0 ;
141
+ }
142
+
143
+ int umf_free (TestState &test_state) {
144
+ if (test_state.pools_alloc .empty ()) {
145
+ return -1 ;
146
+ }
147
+ umfFree (test_state.pools_alloc .back ());
148
+ test_state.pools_alloc .pop_back ();
149
+ return 0 ;
150
+ }
151
+
152
+ void cleanup (TestState &test_state) {
153
+ for (auto &alloc : test_state.allocated_memory ) {
154
+ umfMemoryProviderFree (test_state.provider , alloc.first , alloc.second );
155
+ }
156
+ std::cout << " Freed all allocated memory\n " ;
157
+
158
+ for (auto &alloc : test_state.pools_alloc ) {
159
+ umfFree (alloc);
160
+ }
161
+ for (auto &pool : test_state.pools ) {
162
+ umfPoolDestroy (pool);
163
+ }
164
+
165
+ umfMemoryProviderDestroy (test_state.provider );
166
+ }
167
+
168
+ extern " C" int LLVMFuzzerTestOneInput (uint8_t *data, size_t size) {
169
+ int next_api_call;
170
+ auto data_provider = std::make_unique<FuzzedDataProvider>(data, size);
171
+ TestState test_state (std::move (data_provider));
172
+ int ret = -1 ;
173
+
174
+ int (*api_wrappers[])(TestState &) = {
175
+ umf_memory_provider_alloc, umf_memory_provider_free, umf_pool_create,
176
+ umf_pool_destroy, umf_pool_malloc, umf_free,
177
+ };
178
+ umf_memory_provider_create (test_state);
179
+
180
+ while ((next_api_call = test_state.get_next_api_call ()) != -1 ) {
181
+ ret = api_wrappers[next_api_call](test_state);
182
+ if (ret) {
183
+ cleanup (test_state);
184
+ return -1 ;
185
+ }
186
+ }
187
+
188
+ cleanup (test_state);
189
+ return 0 ;
190
+ }
191
+ } // namespace fuzz
0 commit comments