]> git.saurik.com Git - apple/libc.git/blame - secure/chk_fail.c
Libc-1353.11.2.tar.gz
[apple/libc.git] / secure / chk_fail.c
CommitLineData
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 29uint32_t __chk_assert_no_overlap = 1;
b061a43b
A
30
31__attribute__ ((visibility ("hidden")))
32__attribute__ ((noreturn))
33void
34__chk_fail_overflow (void) {
507116e3 35 os_crash("detected buffer overflow");
b061a43b
A
36}
37
38__attribute__ ((visibility ("hidden")))
39__attribute__ ((noreturn))
40void
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")))
46void
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}