1/*
2 * Copyright (C) 2010 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "MachUtilities.h"
28
29#include <mach/task.h>
30
31void setMachPortQueueLength(mach_port_t receivePort, mach_port_msgcount_t queueLength)
32{
33    mach_port_limits_t portLimits;
34    portLimits.mpl_qlimit = queueLength;
35
36    mach_port_set_attributes(mach_task_self(), receivePort, MACH_PORT_LIMITS_INFO, reinterpret_cast<mach_port_info_t>(&portLimits), MACH_PORT_LIMITS_INFO_COUNT);
37}
38
39mach_port_t machExceptionPort()
40{
41    exception_mask_t exceptionMasks[EXC_TYPES_COUNT];
42    exception_port_t exceptionHandlers[EXC_TYPES_COUNT];
43    exception_behavior_t exceptionBehaviors[EXC_TYPES_COUNT];
44    thread_state_flavor_t exceptionFlavors[EXC_TYPES_COUNT];
45    mach_msg_type_number_t numExceptionMasks;
46
47    kern_return_t kr = task_get_exception_ports(mach_task_self(), EXC_MASK_CRASH, exceptionMasks, &numExceptionMasks, exceptionHandlers, exceptionBehaviors, exceptionFlavors);
48    if (kr != KERN_SUCCESS) {
49        ASSERT_NOT_REACHED();
50        return MACH_PORT_NULL;
51    }
52
53    // We're just interested in the first exception handler.
54    return exceptionHandlers[0];
55}
56
57void setMachExceptionPort(mach_port_t exceptionPort)
58{
59    // Assert that we dont try to call setMachExceptionPort more than once per process.
60#if !ASSERT_DISABLED
61    static mach_port_t taskExceptionPort = MACH_PORT_NULL;
62    ASSERT(taskExceptionPort == MACH_PORT_NULL);
63    taskExceptionPort = exceptionPort;
64#endif
65
66    if (task_set_exception_ports(mach_task_self(), EXC_MASK_CRASH, exceptionPort, EXCEPTION_STATE_IDENTITY | MACH_EXCEPTION_CODES, MACHINE_THREAD_STATE) != KERN_SUCCESS)
67        ASSERT_NOT_REACHED();
68}
69