* INTC Contribution to the O-RAN F Release for O-DU Low
[o-du/phy.git] / fapi_5g / source / utils / nr5g_fapi_memory.c
1 /******************************************************************************
2 *
3 *   Copyright (c) 2021 Intel.
4 *
5 *   Licensed under the Apache License, Version 2.0 (the "License");
6 *   you may not use this file except in compliance with the License.
7 *   You may obtain a copy of the License at
8 *
9 *       http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *   Unless required by applicable law or agreed to in writing, software
12 *   distributed under the License is distributed on an "AS IS" BASIS,
13 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *   See the License for the specific language governing permissions and
15 *   limitations under the License.
16 *
17 *******************************************************************************/
18
19 /**
20  * @file This file defines all the functions used for 
21  * memory operations. 
22  *
23  **/
24
25 #include "nr5g_fapi_memory.h"
26
27 #include <rte_memcpy.h>
28 #include <string.h>
29 #include "nr5g_fapi_wls.h"
30
31 inline uint8_t nr5g_fapi_memcpy_bound_check(
32     void * const d,
33     size_t x,
34     const void * const s,
35     size_t n)
36 {
37     // Memory block size and destination/source boundary check
38     if ((n >= (MSG_MAXSIZE)) || (n > x)) {
39         printf
40             ("[MEMCPY FAILED] %s : %d : max block size: %d, memcpy dst size: %ld, memcpy src size: %ld\n",
41             __func__, __LINE__, MSG_MAXSIZE, x, n);
42         return FAILURE;
43     }
44     // Overlap check: source pointer overruns destination pointer
45     if ((char *)s < (char *)d) {
46         if ((((char *)s + x) >= (char *)d) || (((char *)s + n) >= (char *)d)) {
47             printf("[MEMCPY FAILED] %s : %d : max block size: %d, \
48                     memcpy dst size: %ld, strcpy src size: %ld, \
49                     Source pointer %p overlaps into destination pointer %p\n", __func__, __LINE__, MSG_MAXSIZE, x, n, s, d);
50             return FAILURE;
51         }
52     }
53     // Overlap check: destination pointer overruns source pointer
54     if ((char *)d < (char *)s) {
55         if ((((char *)d + x) >= (char *)s) || (((char *)d + n) >= (char *)s)) {
56             printf("[MEMCPY FAILED] %s : %d : max block size: %d, \
57                     memcpy dst size: %ld, strcpy src size: %ld, \
58                     Destination pointer %p overlaps into source pointer %p\n", __func__, __LINE__, MSG_MAXSIZE, x, n, d, s);
59             return FAILURE;
60         }
61     }
62     rte_memcpy(d, s, n);
63     return SUCCESS;
64 }
65
66 inline uint8_t nr5g_fapi_memset_bound_check(
67     void * const s,
68     size_t x,
69     int32_t c,
70     size_t n)
71 {
72     // Memory block size and destination/source boundary check
73     if ((n >= MSG_MAXSIZE) || (n > x)) {
74         printf
75             ("[MEMSET FAILED] %s : %d : max block size: %d, memset dst size: %ld, memset src size: %ld\n",
76             __func__, __LINE__, MSG_MAXSIZE, x, n);
77         return FAILURE;
78     }
79     memset(s, c, n);
80     return SUCCESS;
81 }
82
83 inline uint8_t nr5g_fapi_strcpy_bound_check(
84     char * const d,
85     size_t x,
86     const char * const s,
87     size_t n)
88 {
89     // Memory block size and destination/source boundary check
90     if ((n >= MSG_MAXSIZE) || (n > x)) {
91         printf("[STRNCPY FAILED] %s : %d : max block size: %d, \
92             strcpy dst size: %ld, strcpy src size: %ld\n", __func__, __LINE__, MSG_MAXSIZE, x, n);
93         return FAILURE;
94     }
95     // Overlap check: source pointer overruns destination pointer
96     if ((char *)s < (char *)d) {
97         if ((((char *)s + x) >= (char *)d) || (((char *)s + n) >= (char *)d)) {
98             printf("[STRNCPY FAILED] %s : %d : max block size: %d, \
99                     strcpy dst size: %ld, strcpy src size: %ld, \
100                     Source pointer %p overlaps into destination pointer %p\n", __func__, __LINE__, MSG_MAXSIZE, x, n, s, d);
101             return FAILURE;
102         }
103     }
104     // Overlap check: destination pointer overruns source pointer
105     if ((char *)d < (char *)s) {
106         if ((((char *)d + x) >= (char *)s) || (((char *)d + n) >= (char *)s)) {
107             printf("[STRNCPY FAILED] %s : %d : max block size: %d, \
108                     strcpy dst size: %ld, strcpy src size: %ld, \
109                     Destination pointer %p overlaps into source pointer %p\n", __func__, __LINE__, MSG_MAXSIZE, x, n, d, s);
110             return FAILURE;
111         }
112     }
113     strncpy(d, s, n);
114     return SUCCESS;
115 }