Debugging: Add code to print backtrace for guest on SIGSEGV
[nativeclient.git] / intermodule_comm / nacl / nacl_imc.cc
blob536f7963ef5077948da443579a7f0874f9921809
1 /*
2 * Copyright 2008, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 // NaCl inter-module communication primitives.
35 #ifdef __native_client__
36 #include <nacl/nacl_imc.h>
37 #else
38 #include "native_client/intermodule_comm/nacl_imc.h"
39 #endif // __native_client__
41 #include <bits/mman.h>
42 #include <errno.h>
43 #include <stdio.h>
44 #include <string.h>
45 #include <sys/nacl_syscalls.h>
47 #include <algorithm>
49 namespace nacl {
51 bool WouldBlock() {
52 return (errno == EAGAIN) ? true : false;
55 int GetLastErrorString(char* buffer, size_t length) {
56 // Note newlib provides only GNU version of strerror_r().
57 if (buffer == NULL || length == 0) {
58 errno = ERANGE;
59 return -1;
61 char* message = strerror_r(errno, buffer, length);
62 if (message != buffer) {
63 size_t message_bytes = strlen(message) + 1;
64 length = std::min(message_bytes, length);
65 memmove(buffer, message, length);
66 buffer[length - 1] = '\0';
68 return 0;
71 Handle BoundSocket(const SocketAddress* address) {
72 // TODO: Switch to the following once the make_bound_sock() prototype
73 // is cleaned up.
74 // return make_bound_sock(address);
75 return -1;
78 int SocketPair(Handle pair[2]) {
79 return imc_socketpair(pair);
82 int Close(Handle handle) {
83 return close(handle);
86 int SendDatagram(Handle handle, const MessageHeader* message, int flags) {
87 return imc_sendmsg(handle, reinterpret_cast<const NaClImcMsgHdr*>(message),
88 flags);
91 int SendDatagramTo(Handle handle, const MessageHeader* message, int flags,
92 const SocketAddress* name) {
93 return -1; // TODO: how to implement this for NaCl?
96 int ReceiveDatagram(Handle handle, MessageHeader* message, int flags) {
97 return imc_recvmsg(handle, reinterpret_cast<NaClImcMsgHdr*>(message), flags);
100 Handle CreateMemoryObject(size_t length) {
101 return imc_mem_obj_create(length);
104 void* Map(void* start, size_t length, int prot, int flags,
105 Handle memory, off_t offset) {
106 static int posix_prot[4] = {
107 PROT_NONE,
108 PROT_READ,
109 PROT_WRITE,
110 PROT_READ | PROT_WRITE
113 int adjusted = 0;
114 if (flags & kMapShared) {
115 adjusted |= MAP_SHARED;
117 if (flags & kMapPrivate) {
118 adjusted |= MAP_PRIVATE;
120 if (flags & kMapFixed) {
121 adjusted |= MAP_FIXED;
123 return mmap(start, length, posix_prot[prot & 3], adjusted, memory, offset);
126 int Unmap(void* start, size_t length) {
127 return munmap(start, length);
130 } // namespace nacl