1 /* Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights
2 reserved.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is also distributed with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have included with MySQL.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software Foundation,
22 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
23
24 /**
25 @file storage/perfschema/pfs_global.cc
26 Miscellaneous global dependencies (implementation).
27 */
28
29 #include "my_global.h"
30 #include "my_sys.h"
31 #include "pfs_global.h"
32 #include "pfs_builtin_memory.h"
33 #include "log.h"
34
35 #include <stdlib.h>
36 #include <string.h>
37
38 #ifdef HAVE_UNISTD_H
39 #include <unistd.h>
40 #endif
41 #ifdef _WIN32
42 #include <winsock2.h>
43 #endif
44 #ifdef HAVE_ARPA_INET_H
45 #include <arpa/inet.h>
46 #endif
47 #ifdef HAVE_NETINET_IN_H
48 #include <netinet/in.h>
49 #endif
50
51 bool pfs_initialized= false;
52
53 /**
54 Memory allocation for the performance schema.
55 The memory used internally in the performance schema implementation.
56 It is allocated at startup, or during runtime with scalable buffers.
57 */
pfs_malloc(PFS_builtin_memory_class * klass,size_t size,myf flags)58 void *pfs_malloc(PFS_builtin_memory_class *klass, size_t size, myf flags)
59 {
60 assert(klass != NULL);
61 assert(size > 0);
62
63 void *ptr= NULL;
64
65 #ifdef PFS_ALIGNEMENT
66 #ifdef HAVE_POSIX_MEMALIGN
67 /* Linux */
68 if (unlikely(posix_memalign(& ptr, PFS_ALIGNEMENT, size)))
69 return NULL;
70 #else
71 #ifdef HAVE_MEMALIGN
72 /* Solaris */
73 ptr= memalign(PFS_ALIGNEMENT, size);
74 if (unlikely(ptr == NULL))
75 return NULL;
76 #else
77 #ifdef HAVE_ALIGNED_MALLOC
78 /* Windows */
79 ptr= _aligned_malloc(size, PFS_ALIGNEMENT);
80 if (unlikely(ptr == NULL))
81 return NULL;
82 #else
83 #error "Missing implementation for PFS_ALIGNENT"
84 #endif /* HAVE_ALIGNED_MALLOC */
85 #endif /* HAVE_MEMALIGN */
86 #endif /* HAVE_POSIX_MEMALIGN */
87 #else /* PFS_ALIGNMENT */
88 /* Everything else */
89 ptr= malloc(size);
90 if (unlikely(ptr == NULL))
91 return NULL;
92 #endif
93
94 klass->count_alloc(size);
95
96 if (flags & MY_ZEROFILL)
97 memset(ptr, 0, size);
98 return ptr;
99 }
100
pfs_free(PFS_builtin_memory_class * klass,size_t size,void * ptr)101 void pfs_free(PFS_builtin_memory_class *klass, size_t size, void *ptr)
102 {
103 if (ptr == NULL)
104 return;
105
106 #ifdef HAVE_POSIX_MEMALIGN
107 /* Allocated with posix_memalign() */
108 free(ptr);
109 #else
110 #ifdef HAVE_MEMALIGN
111 /* Allocated with memalign() */
112 free(ptr);
113 #else
114 #ifdef HAVE_ALIGNED_MALLOC
115 /* Allocated with _aligned_malloc() */
116 _aligned_free(ptr);
117 #else
118 /* Allocated with malloc() */
119 free(ptr);
120 #endif /* HAVE_ALIGNED_MALLOC */
121 #endif /* HAVE_MEMALIGN */
122 #endif /* HAVE_POSIX_MEMALIGN */
123
124 klass->count_free(size);
125 }
126
127 /**
128 Array allocation for the performance schema.
129 Checks for overflow of n * size before allocating.
130 @param klass performance schema memory class
131 @param n number of array elements
132 @param size element size
133 @param flags malloc flags
134 @return pointer to memory on success, else NULL
135 */
pfs_malloc_array(PFS_builtin_memory_class * klass,size_t n,size_t size,myf flags)136 void *pfs_malloc_array(PFS_builtin_memory_class *klass, size_t n, size_t size, myf flags)
137 {
138 assert(klass != NULL);
139 assert(n > 0);
140 assert(size > 0);
141 void *ptr= NULL;
142 size_t array_size= n * size;
143 /* Check for overflow before allocating. */
144 if (is_overflow(array_size, n, size))
145 {
146 sql_print_warning("Failed to allocate memory for %zu chunks each of size "
147 "%zu for buffer '%s' due to overflow", n, size,
148 klass->m_class.m_name);
149 return NULL;
150 }
151
152 if(NULL == (ptr= pfs_malloc(klass, array_size, flags)))
153 {
154 sql_print_warning("Failed to allocate %zu bytes for buffer '%s' due to "
155 "out-of-memory", array_size, klass->m_class.m_name);
156 }
157 return ptr;
158 }
159
160 /**
161 Free array allocated by @sa pfs_malloc_array.
162 @param klass performance schema memory class
163 @param n number of array elements
164 @param size element size
165 @param ptr pointer to memory
166 */
pfs_free_array(PFS_builtin_memory_class * klass,size_t n,size_t size,void * ptr)167 void pfs_free_array(PFS_builtin_memory_class *klass, size_t n, size_t size, void *ptr)
168 {
169 if (ptr == NULL)
170 return;
171 size_t array_size= n * size;
172 /* Overflow should have been detected by pfs_malloc_array. */
173 assert(!is_overflow(array_size, n, size));
174 return pfs_free(klass, array_size, ptr);
175 }
176
177 /**
178 Detect multiplication overflow.
179 @param product multiplication product
180 @param n1 operand
181 @param n2 operand
182 @return true if multiplication caused an overflow.
183 */
is_overflow(size_t product,size_t n1,size_t n2)184 bool is_overflow(size_t product, size_t n1, size_t n2)
185 {
186 if (n1 != 0 && (product / n1 != n2))
187 return true;
188 else
189 return false;
190 }
191
pfs_print_error(const char * format,...)192 void pfs_print_error(const char *format, ...)
193 {
194 va_list args;
195 va_start(args, format);
196 /*
197 Printing to anything else, like the error log, would generate even more
198 recursive calls to the performance schema implementation
199 (file io is instrumented), so that could lead to catastrophic results.
200 Printing to something safe, and low level: stderr only.
201 */
202 vfprintf(stderr, format, args);
203 va_end(args);
204 fflush(stderr);
205 }
206
207
208 /** Convert raw ip address into readable format. Do not do a reverse DNS lookup. */
209
pfs_get_socket_address(char * host,uint host_len,uint * port,const struct sockaddr_storage * src_addr,socklen_t src_len)210 uint pfs_get_socket_address(char *host,
211 uint host_len,
212 uint *port,
213 const struct sockaddr_storage *src_addr,
214 socklen_t src_len)
215 {
216 assert(host);
217 assert(src_addr);
218 assert(port);
219
220 memset(host, 0, host_len);
221 *port= 0;
222
223 switch (src_addr->ss_family)
224 {
225 case AF_INET:
226 {
227 if (host_len < INET_ADDRSTRLEN+1)
228 return 0;
229 struct sockaddr_in *sa4= (struct sockaddr_in *)(src_addr);
230 #ifdef _WIN32
231 /* Older versions of Windows do not support inet_ntop() */
232 getnameinfo((struct sockaddr *)sa4, sizeof(struct sockaddr_in),
233 host, host_len, NULL, 0, NI_NUMERICHOST);
234 #else
235 inet_ntop(AF_INET, &(sa4->sin_addr), host, INET_ADDRSTRLEN);
236 #endif
237 *port= ntohs(sa4->sin_port);
238 }
239 break;
240
241 #ifdef HAVE_IPV6
242 case AF_INET6:
243 {
244 if (host_len < INET6_ADDRSTRLEN+1)
245 return 0;
246 struct sockaddr_in6 *sa6= (struct sockaddr_in6 *)(src_addr);
247 #ifdef _WIN32
248 /* Older versions of Windows do not support inet_ntop() */
249 getnameinfo((struct sockaddr *)sa6, sizeof(struct sockaddr_in6),
250 host, host_len, NULL, 0, NI_NUMERICHOST);
251 #else
252 inet_ntop(AF_INET6, &(sa6->sin6_addr), host, INET6_ADDRSTRLEN);
253 #endif
254 *port= ntohs(sa6->sin6_port);
255 }
256 break;
257 #endif
258
259 default:
260 break;
261 }
262
263 /* Return actual IP address string length */
264 return (strlen((const char*)host));
265 }
266