Add BootstrapCheckIn and its test.

TEST=util_test Bootstrap.BootstrapCheckIn
R=rsesek@chromium.org

Review URL: https://codereview.chromium.org/478713003
This commit is contained in:
Mark Mentovai 2014-08-18 20:41:00 -07:00
parent 8fe32b7b9c
commit 280c4345c5
4 changed files with 271 additions and 0 deletions

88
util/mach/bootstrap.cc Normal file
View File

@ -0,0 +1,88 @@
// Copyright 2014 The Crashpad Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "util/mach/bootstrap.h"
#include <AvailabilityMacros.h>
#include <servers/bootstrap.h>
#include "base/basictypes.h"
#include "base/mac/scoped_mach_port.h"
#include "util/mac/mac_util.h"
#if MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_5
namespace {
// Wraps bootstrap_register to avoid the deprecation warning. It needs to be
// used on 10.5.
kern_return_t BootstrapRegister(mach_port_t bp,
name_t service_name,
mach_port_t sp) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
return bootstrap_register(bp, service_name, sp);
#pragma GCC diagnostic pop
}
} // namespace
#endif
namespace crashpad {
kern_return_t BootstrapCheckIn(mach_port_t bp,
const std::string& service_name,
mach_port_t* service_port) {
// bootstrap_check_in (until the 10.6 SDK) and bootstrap_register (all SDKs)
// are declared with a char* argument, but they dont actually modify the char
// data, so this is safe.
char* service_name_mutable = const_cast<char*>(service_name.c_str());
kern_return_t kr = bootstrap_check_in(bp, service_name_mutable, service_port);
#if MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_5
if (kr == BOOTSTRAP_UNKNOWN_SERVICE && MacOSXMinorVersion() <= 5) {
// This code path should only be entered on 10.5 or earlier.
mach_port_t local_service_port;
kr = mach_port_allocate(
mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &local_service_port);
if (kr != KERN_SUCCESS) {
return kr;
}
base::mac::ScopedMachReceiveRight service_port_receive_right_owner(
local_service_port);
kr = mach_port_insert_right(mach_task_self(),
local_service_port,
local_service_port,
MACH_MSG_TYPE_MAKE_SEND);
if (kr != KERN_SUCCESS) {
return kr;
}
base::mac::ScopedMachSendRight service_port_send_right_owner(
local_service_port);
kr = BootstrapRegister(bp, service_name_mutable, local_service_port);
if (kr != BOOTSTRAP_SUCCESS) {
return kr;
}
ignore_result(service_port_receive_right_owner.release());
*service_port = local_service_port;
}
#endif
return kr;
}
} // namespace crashpad

46
util/mach/bootstrap.h Normal file
View File

@ -0,0 +1,46 @@
// Copyright 2014 The Crashpad Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef CRASHPAD_UTIL_MACH_BOOTSTRAP_H_
#define CRASHPAD_UTIL_MACH_BOOTSTRAP_H_
#include <mach/mach.h>
#include <string>
namespace crashpad {
//! \brief Calls `bootstrap_check_in()` to check in with the bootstrap server.
//!
//! \param[in] bp The bootstrap server to check in with.
//! \param[in] service_name The name of the service to check in.
//! \param[out] service_port The receive right for the checked-in service.
//!
//! \return `BOOTSTRAP_SUCCESS` on success, with \a service_port set
//! appropriately. Otherwise, any error that might be returned by
//! `bootstrap_check_in()`.
//!
//! This function is a wrapper around `bootstrap_check_in()`, checking in with
//! the bootstrap server at \a bp. It exists primarily for compatibility with
//! Mac OS X 10.5, where it is not possible to call `bootstrap_check_in()` for a
//! \a service_name that has not already been registered with the bootstrap
//! server using `bootstrap_register()`. `bootstrap_register()` was deprecated
//! in Mac OS X 10.5.
kern_return_t BootstrapCheckIn(mach_port_t bp,
const std::string& service_name,
mach_port_t* service_port);
} // namespace crashpad
#endif // CRASHPAD_UTIL_MACH_MACH_MESSAGE_SERVER_H_

134
util/mach/bootstrap_test.cc Normal file
View File

@ -0,0 +1,134 @@
// Copyright 2014 The Crashpad Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "util/mach/bootstrap.h"
#include <mach/mach.h>
#include <servers/bootstrap.h>
#include <string.h>
#include <algorithm>
#include <string>
#include "base/basictypes.h"
#include "base/mac/scoped_mach_port.h"
#include "base/rand_util.h"
#include "gtest/gtest.h"
#include "util/test/mac/mach_errors.h"
namespace {
using namespace crashpad;
using namespace crashpad::test;
using namespace testing;
TEST(Bootstrap, BootstrapCheckIn) {
std::string service_name = "com.googlecode.crashpad.test.bootstrap.";
for (int index = 0; index < 16; ++index) {
service_name.append(1, base::RandInt('A', 'Z'));
}
// The service shouldnt be registered in the bootstrap namespace yet.
mach_port_t client_port = MACH_PORT_NULL;
kern_return_t kr =
bootstrap_look_up(bootstrap_port, service_name.c_str(), &client_port);
ASSERT_EQ(BOOTSTRAP_UNKNOWN_SERVICE, kr)
<< BootstrapErrorMessage(kr, "bootstrap_look_up");
// Check in, getting a receive right.
mach_port_t server_port;
kr = BootstrapCheckIn(bootstrap_port, service_name.c_str(), &server_port);
ASSERT_EQ(BOOTSTRAP_SUCCESS, kr)
<< BootstrapErrorMessage(kr, "bootstrap_check_in");
ASSERT_NE(static_cast<mach_port_t>(MACH_PORT_NULL), server_port);
base::mac::ScopedMachReceiveRight server_port_owner(server_port);
// A subsequent checkin attempt should fail.
mach_port_t fail_port = MACH_PORT_NULL;
kr = BootstrapCheckIn(bootstrap_port, service_name.c_str(), &fail_port);
EXPECT_EQ(BOOTSTRAP_SERVICE_ACTIVE, kr);
EXPECT_EQ(static_cast<mach_port_t>(MACH_PORT_NULL), fail_port);
// Look up the service, getting a send right.
kr = bootstrap_look_up(bootstrap_port, service_name.c_str(), &client_port);
ASSERT_EQ(BOOTSTRAP_SUCCESS, kr)
<< BootstrapErrorMessage(kr, "bootstrap_look_up");
base::mac::ScopedMachSendRight client_port_owner(client_port);
EXPECT_NE(static_cast<mach_port_t>(MACH_PORT_NULL), client_port);
// Have the “client” send a message to the “server”.
struct SendMessage {
mach_msg_header_t header;
char data[64];
};
SendMessage send_message = {};
send_message.header.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0);
send_message.header.msgh_size = sizeof(send_message);
send_message.header.msgh_remote_port = client_port;
const char kMessageData[] = "Mach messaging is not a big truck";
const size_t message_data_size =
std::min(sizeof(kMessageData), sizeof(send_message.data));
memcpy(send_message.data, kMessageData, message_data_size);
// The receive operation happens in this same thread after the send, so use a
// non-blocking send (MACH_SEND_TIMEOUT with MACH_MSG_TIMEOUT_NONE). This is a
// small and simple message and the destination ports queue should be empty
// so a non-blocking send should be successful.
kr = mach_msg(&send_message.header,
MACH_SEND_MSG | MACH_SEND_TIMEOUT,
send_message.header.msgh_size,
0,
MACH_PORT_NULL,
MACH_MSG_TIMEOUT_NONE,
MACH_PORT_NULL);
ASSERT_EQ(MACH_MSG_SUCCESS, kr) << MachErrorMessage(kr, "mach_msg");
struct ReceiveMessage : public SendMessage {
mach_msg_trailer_t trailer;
};
ReceiveMessage receive_message;
memset(&receive_message, 0xa5, sizeof(receive_message));
kr = mach_msg(&receive_message.header,
MACH_RCV_MSG | MACH_RCV_TIMEOUT,
0,
sizeof(receive_message),
server_port,
MACH_MSG_TIMEOUT_NONE,
MACH_PORT_NULL);
ASSERT_EQ(MACH_MSG_SUCCESS, kr) << MachErrorMessage(kr, "mach_msg");
EXPECT_EQ(sizeof(send_message), receive_message.header.msgh_size);
EXPECT_EQ(0, memcmp(receive_message.data, kMessageData, message_data_size));
// Make sure that the bootstrap servers mapping disappears if the service
// does.
server_port_owner.reset();
server_port = MACH_PORT_NULL;
// With the server port gone, the client port should have become a dead name.
mach_port_type_t client_port_type;
kr = mach_port_type(mach_task_self(), client_port, &client_port_type);
ASSERT_EQ(KERN_SUCCESS, kr) << MachErrorMessage(kr, "mach_port_type");
EXPECT_EQ(MACH_PORT_TYPE_DEAD_NAME, client_port_type);
client_port_owner.reset();
client_port = MACH_PORT_NULL;
kr = bootstrap_look_up(bootstrap_port, service_name.c_str(), &client_port);
ASSERT_EQ(BOOTSTRAP_UNKNOWN_SERVICE, kr)
<< BootstrapErrorMessage(kr, "bootstrap_look_up");
}
} // namespace

View File

@ -38,6 +38,8 @@
'mac/mac_util.h',
'mac/service_management.cc',
'mac/service_management.h',
'mach/bootstrap.cc',
'mach/bootstrap.h',
'mach/task_memory.cc',
'mach/task_memory.h',
'misc/initialization_state.h',
@ -93,6 +95,7 @@
'mac/launchd_test.mm',
'mac/mac_util_test.mm',
'mac/service_management_test.mm',
'mach/bootstrap_test.cc',
'mach/task_memory_test.cc',
'misc/initialization_state_dcheck_test.cc',
'misc/initialization_state_test.cc',