]>
Commit | Line | Data |
---|---|---|
e9ce8d39 | 1 | /* |
6465356a | 2 | * Copyright (c) 2007-2013 Apple Inc. All rights reserved. |
e9ce8d39 A |
3 | * |
4 | * @APPLE_LICENSE_HEADER_START@ | |
224c7076 | 5 | * |
734aad71 A |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. Please obtain a copy of the License at | |
10 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
11 | * file. | |
224c7076 | 12 | * |
734aad71 A |
13 | * The Original Code and all software distributed under the License are |
14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
e9ce8d39 A |
15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
734aad71 A |
17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
18 | * Please see the License for the specific language governing rights and | |
19 | * limitations under the License. | |
224c7076 | 20 | * |
e9ce8d39 A |
21 | * @APPLE_LICENSE_HEADER_END@ |
22 | */ | |
e9ce8d39 | 23 | |
507116e3 A |
24 | #include <os/assumes.h> |
25 | #include <stdint.h> | |
6465356a | 26 | #include <TargetConditionals.h> |
e9ce8d39 | 27 | |
b061a43b | 28 | __attribute__ ((visibility ("hidden"))) |
507116e3 | 29 | uint32_t __chk_assert_no_overlap = 1; |
b061a43b A |
30 | |
31 | __attribute__ ((visibility ("hidden"))) | |
32 | __attribute__ ((noreturn)) | |
33 | void | |
34 | __chk_fail_overflow (void) { | |
507116e3 | 35 | os_crash("detected buffer overflow"); |
b061a43b A |
36 | } |
37 | ||
38 | __attribute__ ((visibility ("hidden"))) | |
39 | __attribute__ ((noreturn)) | |
40 | void | |
41 | __chk_fail_overlap (void) { | |
507116e3 | 42 | os_crash("detected source and destination buffer overlap"); |
b061a43b A |
43 | } |
44 | ||
b061a43b A |
45 | __attribute__ ((visibility ("hidden"))) |
46 | void | |
47 | __chk_overlap (const void *_a, size_t an, const void *_b, size_t bn) { | |
48 | uintptr_t a = (uintptr_t)_a; | |
49 | uintptr_t b = (uintptr_t)_b; | |
50 | ||
51 | if (__builtin_expect(an == 0 || bn == 0, 0)) { | |
52 | return; | |
53 | } else if (__builtin_expect(a == b, 0)) { | |
54 | __chk_fail_overlap(); | |
55 | } else if (a < b) { | |
56 | if (__builtin_expect(a + an > b, 0)) | |
57 | __chk_fail_overlap(); | |
58 | } else { // b < a | |
59 | if (__builtin_expect(b + bn > a, 0)) | |
60 | __chk_fail_overlap(); | |
61 | } | |
62 | } |