14
14
//
15
15
// ===----------------------------------------------------------------------===//
16
16
17
+ // Define __STDC_WANT_LIB_EXT1__ to get memset_s on platforms that have it.
18
+ // Other files may have included string.h without it already, so we also set
19
+ // this with a -D flag when building, but this allows tests to build without
20
+ // additional trouble.
21
+ #define __STDC_WANT_LIB_EXT1__ 1
22
+ #include < string.h>
23
+
17
24
#include " swift/ABI/MetadataValues.h"
18
25
#include " swift/Runtime/Debug.h"
19
26
#include " llvm/Support/Alignment.h"
@@ -118,6 +125,22 @@ class StackAllocator {
118
125
return headerSize () + size;
119
126
}
120
127
128
+ // / Clear the fake metadata pointer. Call before freeing so that leftover
129
+ // / heap garbage doesn't have slab metadata pointers in it.
130
+ void clearMetadata () {
131
+ // Use memset_s or explicit_bzero where available. Fall back to a plain
132
+ // assignment on unknown platforms. This is not necessary for correctness,
133
+ // just as an aid to analysis tools, so it's OK if the fallback gets
134
+ // optimized out.
135
+ #if defined(__APPLE__)
136
+ memset_s (&metadata, sizeof (metadata), 0 , sizeof (metadata));
137
+ #elif defined(__linux__)
138
+ explicit_bzero (&metadata, sizeof (metadata));
139
+ #else
140
+ metadata = 0 ;
141
+ #endif
142
+ }
143
+
121
144
// / Return the payload buffer address at \p atOffset.
122
145
// /
123
146
// / Note: it's valid to call this function on a not-yet-constructed slab.
@@ -247,6 +270,7 @@ class StackAllocator {
247
270
while (slab) {
248
271
Slab *next = slab->next ;
249
272
freedCapacity += slab->capacity ;
273
+ slab->clearMetadata ();
250
274
free (slab);
251
275
numAllocatedSlabs--;
252
276
slab = next;
@@ -272,6 +296,8 @@ class StackAllocator {
272
296
~StackAllocator () {
273
297
if (lastAllocation)
274
298
SWIFT_FATAL_ERROR (0 , " not all allocations are deallocated" );
299
+ if (firstSlabIsPreallocated)
300
+ firstSlab->clearMetadata ();
275
301
(void )freeAllSlabs (firstSlabIsPreallocated ? firstSlab->next : firstSlab);
276
302
assert (getNumAllocatedSlabs () == 0 );
277
303
}
0 commit comments