1 /*
2 * Copyright 2017 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #ifndef SkMalloc_DEFINED
9 #define SkMalloc_DEFINED
10
11 #include <cstring>
12
13 #include "include/private/base/SkAPI.h"
14
15 /*
16 memory wrappers to be implemented by the porting layer (platform)
17 */
18
19
20 /** Free memory returned by sk_malloc(). It is safe to pass null. */
21 SK_API extern void sk_free(void*);
22
23 /**
24 * Called internally if we run out of memory. The platform implementation must
25 * not return, but should either throw an exception or otherwise exit.
26 */
27 SK_API extern void sk_out_of_memory(void);
28
29 enum {
30 /**
31 * If this bit is set, the returned buffer must be zero-initialized. If this bit is not set
32 * the buffer can be uninitialized.
33 */
34 SK_MALLOC_ZERO_INITIALIZE = 1 << 0,
35
36 /**
37 * If this bit is set, the implementation must throw/crash/quit if the request cannot
38 * be fulfilled. If this bit is not set, then it should return nullptr on failure.
39 */
40 SK_MALLOC_THROW = 1 << 1,
41 };
42 /**
43 * Return a block of memory (at least 4-byte aligned) of at least the specified size.
44 * If the requested memory cannot be returned, either return nullptr or throw/exit, depending
45 * on the SK_MALLOC_THROW bit. If the allocation succeeds, the memory will be zero-initialized
46 * if the SK_MALLOC_ZERO_INITIALIZE bit was set.
47 *
48 * To free the memory, call sk_free()
49 */
50 SK_API extern void* sk_malloc_flags(size_t size, unsigned flags);
51
52 /** Same as standard realloc(), but this one never returns null on failure. It will throw
53 * if it fails.
54 * If size is 0, it will call sk_free on buffer and return null. (This behavior is implementation-
55 * defined for normal realloc. We follow what glibc does.)
56 */
57 SK_API extern void* sk_realloc_throw(void* buffer, size_t size);
58
sk_malloc_throw(size_t size)59 static inline void* sk_malloc_throw(size_t size) {
60 return sk_malloc_flags(size, SK_MALLOC_THROW);
61 }
62
sk_calloc_throw(size_t size)63 static inline void* sk_calloc_throw(size_t size) {
64 return sk_malloc_flags(size, SK_MALLOC_THROW | SK_MALLOC_ZERO_INITIALIZE);
65 }
66
sk_calloc_canfail(size_t size)67 static inline void* sk_calloc_canfail(size_t size) {
68 #if defined(SK_BUILD_FOR_FUZZER)
69 // To reduce the chance of OOM, pretend we can't allocate more than 200kb.
70 if (size > 200000) {
71 return nullptr;
72 }
73 #endif
74 return sk_malloc_flags(size, SK_MALLOC_ZERO_INITIALIZE);
75 }
76
77 // Performs a safe multiply count * elemSize, checking for overflow
78 SK_API extern void* sk_calloc_throw(size_t count, size_t elemSize);
79 SK_API extern void* sk_malloc_throw(size_t count, size_t elemSize);
80 SK_API extern void* sk_realloc_throw(void* buffer, size_t count, size_t elemSize);
81
82 /**
83 * These variants return nullptr on failure
84 */
sk_malloc_canfail(size_t size)85 static inline void* sk_malloc_canfail(size_t size) {
86 #if defined(SK_BUILD_FOR_FUZZER)
87 // To reduce the chance of OOM, pretend we can't allocate more than 200kb.
88 if (size > 200000) {
89 return nullptr;
90 }
91 #endif
92 return sk_malloc_flags(size, 0);
93 }
94 SK_API extern void* sk_malloc_canfail(size_t count, size_t elemSize);
95
96 // bzero is safer than memset, but we can't rely on it, so... sk_bzero()
sk_bzero(void * buffer,size_t size)97 static inline void sk_bzero(void* buffer, size_t size) {
98 // Please c.f. sk_careful_memcpy. It's undefined behavior to call memset(null, 0, 0).
99 if (size) {
100 memset(buffer, 0, size);
101 }
102 }
103
104 /**
105 * sk_careful_memcpy() is just like memcpy(), but guards against undefined behavior.
106 *
107 * It is undefined behavior to call memcpy() with null dst or src, even if len is 0.
108 * If an optimizer is "smart" enough, it can exploit this to do unexpected things.
109 * memcpy(dst, src, 0);
110 * if (src) {
111 * printf("%x\n", *src);
112 * }
113 * In this code the compiler can assume src is not null and omit the if (src) {...} check,
114 * unconditionally running the printf, crashing the program if src really is null.
115 * Of the compilers we pay attention to only GCC performs this optimization in practice.
116 */
sk_careful_memcpy(void * dst,const void * src,size_t len)117 static inline void* sk_careful_memcpy(void* dst, const void* src, size_t len) {
118 // When we pass >0 len we had better already be passing valid pointers.
119 // So we just need to skip calling memcpy when len == 0.
120 if (len) {
121 memcpy(dst,src,len);
122 }
123 return dst;
124 }
125
sk_careful_memmove(void * dst,const void * src,size_t len)126 static inline void* sk_careful_memmove(void* dst, const void* src, size_t len) {
127 // When we pass >0 len we had better already be passing valid pointers.
128 // So we just need to skip calling memcpy when len == 0.
129 if (len) {
130 memmove(dst,src,len);
131 }
132 return dst;
133 }
134
sk_careful_memcmp(const void * a,const void * b,size_t len)135 static inline int sk_careful_memcmp(const void* a, const void* b, size_t len) {
136 // When we pass >0 len we had better already be passing valid pointers.
137 // So we just need to skip calling memcmp when len == 0.
138 if (len == 0) {
139 return 0; // we treat zero-length buffers as "equal"
140 }
141 return memcmp(a, b, len);
142 }
143
144 #endif // SkMalloc_DEFINED
145