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/mach_init.h>
30#include <mach/task.h>
31
32void setMachPortQueueLength(mach_port_t receivePort, mach_port_msgcount_t queueLength)
33{
34    mach_port_limits_t portLimits;
35    portLimits.mpl_qlimit = queueLength;
36
37    mach_port_set_attributes(mach_task_self(), receivePort, MACH_PORT_LIMITS_INFO, reinterpret_cast<mach_port_info_t>(&portLimits), MACH_PORT_LIMITS_INFO_COUNT);
38}
39
40mach_port_t machExceptionPort()
41{
42    exception_mask_t exceptionMasks[EXC_TYPES_COUNT];
43    exception_port_t exceptionHandlers[EXC_TYPES_COUNT];
44    exception_behavior_t exceptionBehaviors[EXC_TYPES_COUNT];
45    thread_state_flavor_t exceptionFlavors[EXC_TYPES_COUNT];
46    mach_msg_type_number_t numExceptionMasks;
47
48    kern_return_t kr = task_get_exception_ports(mach_task_self(), EXC_MASK_CRASH, exceptionMasks, &numExceptionMasks, exceptionHandlers, exceptionBehaviors, exceptionFlavors);
49    if (kr != KERN_SUCCESS) {
50        ASSERT_NOT_REACHED();
51        return MACH_PORT_NULL;
52    }
53
54    // We're just interested in the first exception handler.
55    return exceptionHandlers[0];
56}
57
58void setMachExceptionPort(mach_port_t exceptionPort)
59{
60    // Assert that we dont try to call setMachExceptionPort more than once per process.
61#if !ASSERT_DISABLED
62    static mach_port_t taskExceptionPort = MACH_PORT_NULL;
63    ASSERT(taskExceptionPort == MACH_PORT_NULL);
64    taskExceptionPort = exceptionPort;
65#endif
66
67    if (task_set_exception_ports(mach_task_self(), EXC_MASK_CRASH, exceptionPort, EXCEPTION_STATE_IDENTITY | MACH_EXCEPTION_CODES, MACHINE_THREAD_STATE) != KERN_SUCCESS)
68        ASSERT_NOT_REACHED();
69}
70