43
43
44
44
using namespace swift ;
45
45
46
+ // / Returns true if the pointer passed to a native retain or release is valid.
47
+ // / If false, the operation should immediately return.
48
+ static inline bool isValidPointerForNativeRetain (const void *p) {
49
+ #if defined(__x86_64__) || defined(__arm64__)
50
+ // On these platforms, the upper half of address space is reserved for the
51
+ // kernel, so we can assume that pointer values in this range are invalid.
52
+ return (intptr_t )p > 0 ;
53
+ #else
54
+ return p != nullptr ;
55
+ #endif
56
+ }
57
+
46
58
HeapObject *swift::swift_allocObject (HeapMetadata const *metadata,
47
59
size_t requiredSize,
48
60
size_t requiredAlignmentMask)
@@ -185,7 +197,7 @@ OpaqueValue *swift::swift_projectBox(HeapObject *o) {
185
197
// for boxes of empty type. The address of an empty value is always undefined,
186
198
// so we can just return nil back in this case.
187
199
if (!o)
188
- return reinterpret_cast <OpaqueValue*>(o) ;
200
+ return nullptr ;
189
201
auto metadata = static_cast <const GenericBoxHeapMetadata *>(o->metadata );
190
202
return metadata->project (o);
191
203
}
@@ -206,7 +218,7 @@ void swift::swift_nonatomic_retain(HeapObject *object) {
206
218
SWIFT_RT_ENTRY_IMPL_VISIBILITY
207
219
extern " C"
208
220
void SWIFT_RT_ENTRY_IMPL (swift_nonatomic_retain)(HeapObject *object) {
209
- if (object)
221
+ if (isValidPointerForNativeRetain ( object) )
210
222
object->refCounts .incrementNonAtomic (1 );
211
223
}
212
224
@@ -217,15 +229,15 @@ void swift::swift_nonatomic_release(HeapObject *object) {
217
229
SWIFT_RT_ENTRY_IMPL_VISIBILITY
218
230
extern " C"
219
231
void SWIFT_RT_ENTRY_IMPL (swift_nonatomic_release)(HeapObject *object) {
220
- if (object)
232
+ if (isValidPointerForNativeRetain ( object) )
221
233
object->refCounts .decrementAndMaybeDeinitNonAtomic (1 );
222
234
}
223
235
224
236
SWIFT_RT_ENTRY_IMPL_VISIBILITY
225
237
extern " C"
226
238
void SWIFT_RT_ENTRY_IMPL (swift_retain)(HeapObject *object)
227
239
SWIFT_CC(RegisterPreservingCC_IMPL) {
228
- if (object)
240
+ if (isValidPointerForNativeRetain ( object) )
229
241
object->refCounts .increment (1 );
230
242
}
231
243
@@ -238,7 +250,7 @@ SWIFT_RT_ENTRY_IMPL_VISIBILITY
238
250
extern " C"
239
251
void SWIFT_RT_ENTRY_IMPL (swift_retain_n)(HeapObject *object, uint32_t n)
240
252
SWIFT_CC(RegisterPreservingCC_IMPL) {
241
- if (object)
253
+ if (isValidPointerForNativeRetain ( object) )
242
254
object->refCounts .increment (n);
243
255
}
244
256
@@ -251,7 +263,7 @@ SWIFT_RT_ENTRY_IMPL_VISIBILITY
251
263
extern " C"
252
264
void SWIFT_RT_ENTRY_IMPL (swift_nonatomic_retain_n)(HeapObject *object, uint32_t n)
253
265
SWIFT_CC(RegisterPreservingCC_IMPL) {
254
- if (object)
266
+ if (isValidPointerForNativeRetain ( object) )
255
267
object->refCounts .incrementNonAtomic (n);
256
268
}
257
269
@@ -264,7 +276,7 @@ SWIFT_RT_ENTRY_IMPL_VISIBILITY
264
276
extern " C"
265
277
void SWIFT_RT_ENTRY_IMPL (swift_release)(HeapObject *object)
266
278
SWIFT_CC(RegisterPreservingCC_IMPL) {
267
- if (object)
279
+ if (isValidPointerForNativeRetain ( object) )
268
280
object->refCounts .decrementAndMaybeDeinit (1 );
269
281
}
270
282
@@ -277,7 +289,7 @@ SWIFT_RT_ENTRY_IMPL_VISIBILITY
277
289
extern " C"
278
290
void SWIFT_RT_ENTRY_IMPL (swift_release_n)(HeapObject *object, uint32_t n)
279
291
SWIFT_CC(RegisterPreservingCC_IMPL) {
280
- if (object)
292
+ if (isValidPointerForNativeRetain ( object) )
281
293
object->refCounts .decrementAndMaybeDeinit (n);
282
294
}
283
295
@@ -294,7 +306,7 @@ SWIFT_RT_ENTRY_IMPL_VISIBILITY
294
306
extern " C"
295
307
void SWIFT_RT_ENTRY_IMPL (swift_nonatomic_release_n)(HeapObject *object, uint32_t n)
296
308
SWIFT_CC(RegisterPreservingCC_IMPL) {
297
- if (object)
309
+ if (isValidPointerForNativeRetain ( object) )
298
310
object->refCounts .decrementAndMaybeDeinitNonAtomic (n);
299
311
}
300
312
@@ -308,15 +320,15 @@ size_t swift::swift_unownedRetainCount(HeapObject *object) {
308
320
309
321
void swift::swift_unownedRetain (HeapObject *object)
310
322
SWIFT_CC(RegisterPreservingCC_IMPL) {
311
- if (!object)
323
+ if (!isValidPointerForNativeRetain ( object) )
312
324
return ;
313
325
314
326
object->refCounts .incrementUnowned (1 );
315
327
}
316
328
317
329
void swift::swift_unownedRelease (HeapObject *object)
318
330
SWIFT_CC(RegisterPreservingCC_IMPL) {
319
- if (!object)
331
+ if (!isValidPointerForNativeRetain ( object) )
320
332
return ;
321
333
322
334
// Only class objects can be unowned-retained and unowned-released.
@@ -334,15 +346,15 @@ void swift::swift_unownedRelease(HeapObject *object)
334
346
335
347
void swift::swift_unownedRetain_n (HeapObject *object, int n)
336
348
SWIFT_CC(RegisterPreservingCC_IMPL) {
337
- if (!object)
349
+ if (!isValidPointerForNativeRetain ( object) )
338
350
return ;
339
351
340
352
object->refCounts .incrementUnowned (n);
341
353
}
342
354
343
355
void swift::swift_unownedRelease_n (HeapObject *object, int n)
344
356
SWIFT_CC(RegisterPreservingCC_IMPL) {
345
- if (!object)
357
+ if (!isValidPointerForNativeRetain ( object) )
346
358
return ;
347
359
348
360
// Only class objects can be unowned-retained and unowned-released.
@@ -359,7 +371,7 @@ void swift::swift_unownedRelease_n(HeapObject *object, int n)
359
371
360
372
HeapObject *swift::swift_tryPin (HeapObject *object)
361
373
SWIFT_CC(RegisterPreservingCC_IMPL) {
362
- assert (object);
374
+ assert (isValidPointerForNativeRetain ( object) );
363
375
364
376
// Try to set the flag. If this succeeds, the caller will be
365
377
// responsible for clearing it.
@@ -373,7 +385,7 @@ HeapObject *swift::swift_tryPin(HeapObject *object)
373
385
374
386
void swift::swift_unpin (HeapObject *object)
375
387
SWIFT_CC(RegisterPreservingCC_IMPL) {
376
- if (object)
388
+ if (isValidPointerForNativeRetain ( object) )
377
389
object->refCounts .decrementAndUnpinAndMaybeDeinit ();
378
390
}
379
391
@@ -398,15 +410,15 @@ HeapObject *swift::swift_nonatomic_tryPin(HeapObject *object)
398
410
399
411
void swift::swift_nonatomic_unpin (HeapObject *object)
400
412
SWIFT_CC(RegisterPreservingCC_IMPL) {
401
- if (object)
413
+ if (isValidPointerForNativeRetain ( object) )
402
414
object->refCounts .decrementAndUnpinAndMaybeDeinitNonAtomic ();
403
415
}
404
416
405
417
SWIFT_RT_ENTRY_IMPL_VISIBILITY
406
418
extern " C"
407
419
HeapObject *SWIFT_RT_ENTRY_IMPL (swift_tryRetain)(HeapObject *object)
408
420
SWIFT_CC(RegisterPreservingCC_IMPL) {
409
- if (!object)
421
+ if (!isValidPointerForNativeRetain ( object) )
410
422
return nullptr ;
411
423
412
424
if (object->refCounts .tryIncrement ()) return object;
@@ -421,14 +433,14 @@ bool swift_isDeallocating(HeapObject *object) {
421
433
SWIFT_RT_ENTRY_IMPL_VISIBILITY
422
434
extern " C"
423
435
bool SWIFT_RT_ENTRY_IMPL (swift_isDeallocating)(HeapObject *object) {
424
- if (!object)
436
+ if (!isValidPointerForNativeRetain ( object) )
425
437
return false ;
426
438
return object->refCounts .isDeiniting ();
427
439
}
428
440
429
441
void swift::swift_unownedRetainStrong (HeapObject *object)
430
442
SWIFT_CC(RegisterPreservingCC_IMPL) {
431
- if (!object)
443
+ if (!isValidPointerForNativeRetain ( object) )
432
444
return ;
433
445
assert (object->refCounts .getUnownedCount () &&
434
446
" object is not currently unowned-retained" );
@@ -439,7 +451,7 @@ void swift::swift_unownedRetainStrong(HeapObject *object)
439
451
440
452
void swift::swift_unownedRetainStrongAndRelease (HeapObject *object)
441
453
SWIFT_CC(RegisterPreservingCC_IMPL) {
442
- if (!object)
454
+ if (!isValidPointerForNativeRetain ( object) )
443
455
return ;
444
456
assert (object->refCounts .getUnownedCount () &&
445
457
" object is not currently unowned-retained" );
@@ -454,7 +466,7 @@ void swift::swift_unownedRetainStrongAndRelease(HeapObject *object)
454
466
}
455
467
456
468
void swift::swift_unownedCheck (HeapObject *object) {
457
- if (!object) return ;
469
+ if (!isValidPointerForNativeRetain ( object) ) return ;
458
470
assert (object->refCounts .getUnownedCount () &&
459
471
" object is not currently unowned-retained" );
460
472
0 commit comments