| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright 2008 The Native Client Authors. All rights reserved. | |
| 3 * Use of this source code is governed by a BSD-style license that can | |
| 4 * be found in the LICENSE file. | |
| 5 */ | |
| 6 | |
| 7 | |
| 8 #include "native_client/src/trusted/plugin/srpc/stream_shm_buffer.h" | |
| 9 | |
| 10 #include <stdio.h> | |
| 11 #include <string.h> | |
| 12 #include <limits> | |
| 13 | |
| 14 #include "native_client/src/include/checked_cast.h" | |
| 15 #include "native_client/src/include/nacl_macros.h" | |
| 16 #include "native_client/src/include/portability.h" | |
| 17 | |
| 18 #include "native_client/src/trusted/plugin/srpc/utility.h" | |
| 19 | |
| 20 #include "native_client/src/trusted/service_runtime/gio_shm_unbounded.h" | |
| 21 #include "native_client/src/trusted/service_runtime/nacl_config.h" | |
| 22 #include "native_client/src/trusted/service_runtime/sel_util.h" | |
| 23 | |
| 24 using nacl::assert_cast; | |
| 25 | |
| 26 namespace plugin { | |
| 27 | |
| 28 StreamShmBuffer::StreamShmBuffer() { | |
| 29 shmbufp_ = reinterpret_cast<NaClGioShmUnbounded *>(malloc(sizeof *shmbufp_)); | |
| 30 // If NULL == shmbufp_, object is not completely initialized. | |
| 31 // Therefore all member functions need to check before running. | |
| 32 if (NULL == shmbufp_) { | |
| 33 PLUGIN_PRINTF(("StreamShmBuffer: malloc failed\n")); | |
| 34 return; | |
| 35 } | |
| 36 if (!NaClGioShmUnboundedCtor(shmbufp_)) { | |
| 37 PLUGIN_PRINTF(("StreamShmBuffer: NaClGioShmUnboundedCtor failed\n")); | |
| 38 free(shmbufp_); | |
| 39 shmbufp_ = NULL; | |
| 40 } | |
| 41 } | |
| 42 | |
| 43 StreamShmBuffer::~StreamShmBuffer() { | |
| 44 if (NULL == shmbufp_) { | |
| 45 return; | |
| 46 } | |
| 47 shmbufp_->base.vtbl->Dtor(&shmbufp_->base); | |
| 48 free(shmbufp_); | |
| 49 } | |
| 50 | |
| 51 int32_t StreamShmBuffer::write(int32_t offset, int32_t len, void* buf) { | |
| 52 // Errors are reported to NPAPI by using a negative return value. | |
| 53 // If the constructor failed to set things up corretly, we return failure. | |
| 54 if (NULL == shmbufp_) { | |
| 55 return -1; | |
| 56 } | |
| 57 // If the offset or length is negative, return failure. | |
| 58 if (0 > offset || 0 > len) { | |
| 59 return -1; | |
| 60 } | |
| 61 if (0 > shmbufp_->base.vtbl->Seek(&shmbufp_->base, offset, SEEK_SET)) { | |
| 62 return -1; | |
| 63 } | |
| 64 ssize_t rv = shmbufp_->base.vtbl->Write(&shmbufp_->base, buf, len); | |
| 65 if (rv != len) { | |
| 66 PLUGIN_PRINTF(("StreamShmBuffer::write returned %" NACL_PRIdS | |
| 67 ", not %" NACL_PRIdS "\n", | |
| 68 rv, static_cast<ssize_t>(len))); | |
| 69 } | |
| 70 return static_cast<int32_t>(rv); | |
| 71 } | |
| 72 | |
| 73 int32_t StreamShmBuffer::read(int32_t offset, int32_t len, void* buf) { | |
| 74 // Errors are reported to NPAPI by using a negative return value. | |
| 75 // If the constructor failed to set things up corretly, we return failure. | |
| 76 if (NULL == shmbufp_) { | |
| 77 return -1; | |
| 78 } | |
| 79 // If the offset or length is negative, return failure. | |
| 80 if (0 > offset || 0 > len) { | |
| 81 return -1; | |
| 82 } | |
| 83 if (0 > shmbufp_->base.vtbl->Seek(&shmbufp_->base, offset, SEEK_SET)) { | |
| 84 return -1; | |
| 85 } | |
| 86 ssize_t rv = shmbufp_->base.vtbl->Read(&shmbufp_->base, buf, len); | |
| 87 if (rv != len) { | |
| 88 PLUGIN_PRINTF(("StreamShmBuffer::read returned %" NACL_PRIdS | |
| 89 ", not %" NACL_PRIdS "\n", | |
| 90 rv, static_cast<ssize_t>(len))); | |
| 91 } | |
| 92 return static_cast<int32_t>(rv); | |
| 93 } | |
| 94 | |
| 95 NaClDesc* StreamShmBuffer::shm(int32_t* size) const { | |
| 96 size_t actual_size; | |
| 97 NaClDesc *ndp; | |
| 98 | |
| 99 ndp = NaClGioShmUnboundedGetNaClDesc(shmbufp_, &actual_size); | |
| 100 if (static_cast<size_t>(std::numeric_limits<int32_t>::max()) < actual_size) { | |
| 101 // fail -- not representable | |
| 102 *size = 0; | |
| 103 return NULL; | |
| 104 } | |
| 105 *size = assert_cast<int32_t>(actual_size); | |
| 106 return ndp; | |
| 107 } | |
| 108 | |
| 109 } // namespace plugin | |
| OLD | NEW |