Skip to content

Commit

Permalink
Non-SFI Mode: Build base/ library by PNaCL toolchain for nacl_helper_…
Browse files Browse the repository at this point in the history
…nonsfi.

This CL is to build base/ for nacl_helper_nonsfi.
The library is similar to base_nacl, but slightly different:
- For rand_util, rand_util_posix should be used, instead of
  rand_util_nacl, because nacl_helper_nonsfi will be running under
  Linux directly.
- MessageLoopForIO should be based on MessagePumpLibevent rather than
  MessagePumpDefault, to support IPC.
- GetKnownDeadTerminationStatus, GetTerminationStatus,
  UnixDomainSocket::SendMsg and RecvMsg are included, as these are used
  to implement nacl_helper_nonsfi binary.
- GLIB is not supported. It is unnecessary for nacl_helper_nonsfi.
Note that this library is not used yet from any binary, because this CL
is just a preparation, but the library is built actually.

BUG=358465
TEST=Ran trybot. Implement nacl_helper_nonsfi on top of this CL, and made sure
it is working.

Review URL: https://codereview.chromium.org/659513004

Cr-Commit-Position: refs/heads/master@{#300075}
  • Loading branch information
hidehiko authored and Commit bot committed Oct 17, 2014
1 parent 3e04b7b commit 2b720d2
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 16 deletions.
55 changes: 55 additions & 0 deletions base/base_nacl.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,60 @@
},
],
}],
['disable_nacl==0', {
'targets': [
{
'target_name': 'base_nacl_nonsfi',
'type': 'none',
'include_dirs': [
'<(DEPTH)/native_client/src/public/linux_syscalls',
],
'variables': {
'base_target': 1,
'nacl_untrusted_build': 1,
'nlib_target': 'libbase_nacl_nonsfi.a',
'build_glibc': 0,
'build_newlib': 0,
'build_irt': 0,
'build_pnacl_newlib': 0,
'build_nonsfi_helper': 1,

'sources': [
'base_switches.cc',
'base_switches.h',

# For PathExists and ReadFromFD.
'files/file_util_posix.cc',

# For MessageLoopForIO based on libevent.
'message_loop/message_pump_libevent.cc',
'message_loop/message_pump_libevent.h',

# For UnixDomainSocket::SendMsg and RecvMsg.
'posix/unix_domain_socket_linux.cc',

# For GetKnownDeadTerminationStatus and GetTerminationStatus.
'process/kill_posix.cc',

# Unlike libbase_nacl, for Non-SFI build, we need to use
# rand_util_posix for random implementation, instead of
# rand_util_nacl.cc, which is based on IRT. rand_util_nacl.cc is
# excluded below.
'rand_util_posix.cc',

# For CancelableSyncSocket.
'sync_socket_nacl.cc',
],
},
'sources!': [
'rand_util_nacl.cc',
],
'dependencies': [
'<(DEPTH)/native_client/tools.gyp:prep_toolchain',
'<(DEPTH)/third_party/libevent/libevent_nacl_nonsfi.gyp:event_nacl_nonsfi',
],
},
],
}],
],
}
7 changes: 7 additions & 0 deletions base/files/file_util_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@

namespace base {

#if !defined(__native_client_nonsfi__)
namespace {

#if defined(OS_BSD) || defined(OS_MACOSX) || defined(OS_NACL)
Expand Down Expand Up @@ -345,6 +346,7 @@ bool CopyDirectory(const FilePath& from_path,

return success;
}
#endif // !defined(__native_client_nonsfi__)

bool PathExists(const FilePath& path) {
ThreadRestrictions::AssertIOAllowed();
Expand All @@ -356,6 +358,7 @@ bool PathExists(const FilePath& path) {
return access(path.value().c_str(), F_OK) == 0;
}

#if !defined(__native_client_nonsfi__)
bool PathIsWritable(const FilePath& path) {
ThreadRestrictions::AssertIOAllowed();
return access(path.value().c_str(), W_OK) == 0;
Expand All @@ -368,6 +371,7 @@ bool DirectoryExists(const FilePath& path) {
return S_ISDIR(file_info.st_mode);
return false;
}
#endif // !defined(__native_client_nonsfi__)

bool ReadFromFD(int fd, char* buffer, size_t bytes) {
size_t total_read = 0;
Expand All @@ -381,6 +385,7 @@ bool ReadFromFD(int fd, char* buffer, size_t bytes) {
return total_read == bytes;
}

#if !defined(__native_client_nonsfi__)
bool CreateSymbolicLink(const FilePath& target_path,
const FilePath& symlink_path) {
DCHECK(!symlink_path.empty());
Expand Down Expand Up @@ -920,4 +925,6 @@ bool CopyFileUnsafe(const FilePath& from_path, const FilePath& to_path) {
#endif // !defined(OS_MACOSX)

} // namespace internal

#endif // !defined(__native_client_nonsfi__)
} // namespace base
6 changes: 3 additions & 3 deletions base/message_loop/message_loop.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ bool AlwaysNotifyPump(MessageLoop::Type type) {

#if defined(OS_IOS)
typedef MessagePumpIOSForIO MessagePumpForIO;
#elif defined(OS_NACL)
#elif defined(OS_NACL) && !defined(__native_client_nonsfi__)
typedef MessagePumpDefault MessagePumpForIO;
#elif defined(OS_POSIX)
typedef MessagePumpLibevent MessagePumpForIO;
Expand Down Expand Up @@ -676,7 +676,7 @@ bool MessageLoopForUI::WatchFileDescriptor(
//------------------------------------------------------------------------------
// MessageLoopForIO

#if !defined(OS_NACL)
#if !defined(OS_NACL) || defined(__native_client_nonsfi__)
void MessageLoopForIO::AddIOObserver(
MessageLoopForIO::IOObserver* io_observer) {
ToPumpIO(pump_.get())->AddIOObserver(io_observer);
Expand Down Expand Up @@ -714,6 +714,6 @@ bool MessageLoopForIO::WatchFileDescriptor(int fd,
}
#endif

#endif // !defined(OS_NACL)
#endif // !defined(OS_NACL) || defined(__native_client_nonsfi__)

} // namespace base
4 changes: 2 additions & 2 deletions base/message_loop/message_loop.h
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ class BASE_EXPORT MessageLoopForIO : public MessageLoop {
return loop && loop->type() == MessageLoop::TYPE_IO;
}

#if !defined(OS_NACL)
#if !defined(OS_NACL) || defined(__native_client_nonsfi__)

#if defined(OS_WIN)
typedef MessagePumpForIO::IOHandler IOHandler;
Expand Down Expand Up @@ -642,7 +642,7 @@ class BASE_EXPORT MessageLoopForIO : public MessageLoop {
FileDescriptorWatcher *controller,
Watcher *delegate);
#endif // defined(OS_IOS) || defined(OS_POSIX)
#endif // !defined(OS_NACL)
#endif // !defined(OS_NACL) || defined(__native_client_nonsfi__)
};

// Do not add any member variables to MessageLoopForIO! This is important b/c
Expand Down
27 changes: 24 additions & 3 deletions base/posix/unix_domain_socket_linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

#include <errno.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <unistd.h>

#include <vector>
Expand All @@ -18,8 +17,13 @@
#include "base/posix/eintr_wrapper.h"
#include "base/stl_util.h"

#if !defined(__native_client_nonsfi__)
#include <sys/uio.h>
#endif

const size_t UnixDomainSocket::kMaxFileDescriptors = 16;

#if !defined(__native_client_nonsfi__)
// Creates a connected pair of UNIX-domain SOCK_SEQPACKET sockets, and passes
// ownership of the newly allocated file descriptors to |one| and |two|.
// Returns true on success.
Expand All @@ -37,6 +41,7 @@ bool UnixDomainSocket::EnableReceiveProcessId(int fd) {
const int enable = 1;
return setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &enable, sizeof(enable)) == 0;
}
#endif // !defined(__native_client_nonsfi__)

// static
bool UnixDomainSocket::SendMsg(int fd,
Expand Down Expand Up @@ -106,8 +111,14 @@ ssize_t UnixDomainSocket::RecvMsgWithFlags(int fd,
msg.msg_iov = &iov;
msg.msg_iovlen = 1;

char control_buffer[CMSG_SPACE(sizeof(int) * kMaxFileDescriptors) +
CMSG_SPACE(sizeof(struct ucred))];
const size_t kControlBufferSize =
CMSG_SPACE(sizeof(int) * kMaxFileDescriptors)
#if !defined(__native_client_nonsfi__)
// The PNaCl toolchain for Non-SFI binary build does not support ucred.
+ CMSG_SPACE(sizeof(struct ucred))
#endif
;
char control_buffer[kControlBufferSize];
msg.msg_control = control_buffer;
msg.msg_controllen = sizeof(control_buffer);

Expand All @@ -130,21 +141,29 @@ ssize_t UnixDomainSocket::RecvMsgWithFlags(int fd,
wire_fds = reinterpret_cast<int*>(CMSG_DATA(cmsg));
wire_fds_len = payload_len / sizeof(int);
}
#if !defined(__native_client_nonsfi__)
// The PNaCl toolchain for Non-SFI binary build does not support
// SCM_CREDENTIALS.
if (cmsg->cmsg_level == SOL_SOCKET &&
cmsg->cmsg_type == SCM_CREDENTIALS) {
DCHECK(payload_len == sizeof(struct ucred));
DCHECK(pid == -1);
pid = reinterpret_cast<struct ucred*>(CMSG_DATA(cmsg))->pid;
}
#endif
}
}

#if !defined(__native_client_nonsfi__)
// The PNaCl toolchain for Non-SFI binary build does not support
// MSG_TRUNC or MSG_CTRUNC.
if (msg.msg_flags & MSG_TRUNC || msg.msg_flags & MSG_CTRUNC) {
for (unsigned i = 0; i < wire_fds_len; ++i)
close(wire_fds[i]);
errno = EMSGSIZE;
return -1;
}
#endif

if (wire_fds) {
for (unsigned i = 0; i < wire_fds_len; ++i)
Expand All @@ -165,6 +184,7 @@ ssize_t UnixDomainSocket::RecvMsgWithFlags(int fd,
return r;
}

#if !defined(__native_client_nonsfi__)
// static
ssize_t UnixDomainSocket::SendRecvMsg(int fd,
uint8_t* reply,
Expand Down Expand Up @@ -222,3 +242,4 @@ ssize_t UnixDomainSocket::SendRecvMsgWithFlags(int fd,

return reply_len;
}
#endif // !defined(__native_client_nonsfi__)
4 changes: 4 additions & 0 deletions base/posix/unix_domain_socket_linux.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ class BASE_EXPORT UnixDomainSocket {
// Maximum number of file descriptors that can be read by RecvMsg().
static const size_t kMaxFileDescriptors;

#if !defined(__native_client_nonsfi__)
// Use to enable receiving process IDs in RecvMsgWithPid. Should be called on
// the receiving socket (i.e., the socket passed to RecvMsgWithPid). Returns
// true if successful.
static bool EnableReceiveProcessId(int fd);
#endif // !defined(__native_client_nonsfi__)

// Use sendmsg to write the given msg and include a vector of file
// descriptors. Returns true if successful.
Expand All @@ -50,6 +52,7 @@ class BASE_EXPORT UnixDomainSocket {
ScopedVector<base::ScopedFD>* fds,
base::ProcessId* pid);

#if !defined(__native_client_nonsfi__)
// Perform a sendmsg/recvmsg pair.
// 1. This process creates a UNIX SEQPACKET socketpair. Using
// connection-oriented sockets (SEQPACKET or STREAM) is critical here,
Expand Down Expand Up @@ -82,6 +85,7 @@ class BASE_EXPORT UnixDomainSocket {
int recvmsg_flags,
int* result_fd,
const Pickle& request);
#endif // !defined(__native_client_nonsfi__)
private:
// Similar to RecvMsg, but allows to specify |flags| for recvmsg(2).
static ssize_t RecvMsgWithFlags(int fd,
Expand Down
6 changes: 6 additions & 0 deletions base/process/kill_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace base {

namespace {

#if !defined(__native_client_nonsfi__)
bool WaitpidWithTimeout(ProcessHandle handle,
int* status,
base::TimeDelta wait) {
Expand Down Expand Up @@ -83,6 +84,7 @@ bool WaitpidWithTimeout(ProcessHandle handle,

return ret_pid > 0;
}
#endif // !defined(__native_client_nonsfi__)

TerminationStatus GetTerminationStatusImpl(ProcessHandle handle,
bool can_block,
Expand Down Expand Up @@ -130,6 +132,7 @@ TerminationStatus GetTerminationStatusImpl(ProcessHandle handle,

} // namespace

#if !defined(__native_client_nonsfi__)
// Attempts to kill the process identified by the given process
// entry structure. Ignores specified exit_code; posix can't force that.
// Returns true if this is successful, false otherwise.
Expand Down Expand Up @@ -191,6 +194,7 @@ bool KillProcessGroup(ProcessHandle process_group_id) {
DPLOG(ERROR) << "Unable to terminate process group " << process_group_id;
return result;
}
#endif // !defined(__native_client_nonsfi__)

TerminationStatus GetTerminationStatus(ProcessHandle handle, int* exit_code) {
return GetTerminationStatusImpl(handle, false /* can_block */, exit_code);
Expand All @@ -206,6 +210,7 @@ TerminationStatus GetKnownDeadTerminationStatus(ProcessHandle handle,
return GetTerminationStatusImpl(handle, true /* can_block */, exit_code);
}

#if !defined(__native_client_nonsfi__)
bool WaitForExitCode(ProcessHandle handle, int* exit_code) {
int status;
if (HANDLE_EINTR(waitpid(handle, &status, 0)) == -1) {
Expand Down Expand Up @@ -478,5 +483,6 @@ void EnsureProcessGetsReaped(ProcessHandle process) {
}

#endif // !defined(OS_MACOSX)
#endif // !defined(__native_client_nonsfi__)

} // namespace base
6 changes: 3 additions & 3 deletions build/common.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -2562,9 +2562,6 @@
['use_cras==1', {
'defines': ['USE_CRAS=1'],
}],
['use_glib==1', {
'defines': ['USE_GLIB=1'],
}],
['use_ozone==1', {
'defines': ['USE_OZONE=1'],
}],
Expand Down Expand Up @@ -2923,6 +2920,9 @@
'USE_OPENSSL_CERTS=1',
],
}],
['<(use_glib)==1 and >(nacl_untrusted_build)==0', {
'defines': ['USE_GLIB=1'],
}],
['<(use_nss)==1 and >(nacl_untrusted_build)==0', {
'defines': ['USE_NSS=1'],
}],
Expand Down
7 changes: 2 additions & 5 deletions components/nacl_nonsfi.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,17 @@
'conditions': [
['target_arch=="ia32" or target_arch=="x64"', {
'extra_deps_newlib32_nonsfi': [
'>(tc_lib_dir_nonsfi_helper32)/libbase_nacl_nonsfi.a',
'>(tc_lib_dir_nonsfi_helper32)/libevent_nacl_nonsfi.a',
],
}],
],
},
'dependencies': [
'<(DEPTH)/base/base_nacl.gyp:base_nacl_nonsfi',
'<(DEPTH)/native_client/src/nonsfi/irt/irt.gyp:nacl_sys_private',
'<(DEPTH)/native_client/src/untrusted/nacl/nacl.gyp:nacl_lib_newlib',
'<(DEPTH)/native_client/tools.gyp:prep_toolchain',

# TODO(hidehiko): Remove this when we introduce base/.
# This dependency is introduced to make sure the library is
# actually built successfully.
'<(DEPTH)/third_party/libevent/libevent_nacl_nonsfi.gyp:event_nacl_nonsfi',
],
},
# TODO(hidehiko): Add Non-SFI version of nacl_loader_unittests.
Expand Down

0 comments on commit 2b720d2

Please sign in to comment.