| 1 | // Copyright (c) 2019-2022, The Monero Project |
| 2 | // |
| 3 | // All rights reserved. |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without modification, are |
| 6 | // permitted provided that the following conditions are met: |
| 7 | // |
| 8 | // 1. Redistributions of source code must retain the above copyright notice, this list of |
| 9 | // conditions and the following disclaimer. |
| 10 | // |
| 11 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 12 | // of conditions and the following disclaimer in the documentation and/or other |
| 13 | // materials provided with the distribution. |
| 14 | // |
| 15 | // 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 16 | // used to endorse or promote products derived from this software without specific |
| 17 | // prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 20 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 22 | // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 26 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 27 | // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | |
| 29 | #include <atomic> |
| 30 | #include <cstdlib> |
| 31 | #include <cstring> |
| 32 | #include <limits> |
| 33 | #include <stdexcept> |
| 34 | #include <utility> |
| 35 | |
| 36 | #include "byte_slice.h" |
| 37 | #include "byte_stream.h" |
| 38 | |
| 39 | namespace |
| 40 | { |
| 41 | const std::size_t page_size = 4096; |
| 42 | } |
| 43 | |
| 44 | namespace epee |
| 45 | { |
| 46 | struct byte_slice_data |
| 47 | { |
| 48 | byte_slice_data() noexcept |
| 49 | : ref_count(1) |
| 50 | {} |
| 51 | |
| 52 | virtual ~byte_slice_data() noexcept |
| 53 | {} |
| 54 | |
| 55 | std::atomic<std::size_t> ref_count; |
| 56 | }; |
| 57 | |
| 58 | void release_byte_slice::call(void*, void* ptr) noexcept |
| 59 | { |
| 60 | if (ptr) |
| 61 | { |
| 62 | byte_slice_data* self = static_cast<byte_slice_data*>(ptr); |
| 63 | if (--(self->ref_count) == 0) |
| 64 | { |
| 65 | self->~byte_slice_data(); |
| 66 | free(self); |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | namespace |
| 72 | { |
| 73 | template<typename T> |
| 74 | struct adapted_byte_slice final : byte_slice_data |
| 75 | { |
| 76 | explicit adapted_byte_slice(T&& buffer) |
| 77 | : byte_slice_data(), buffer(std::move(buffer)) |
| 78 | {} |
| 79 | |
| 80 | virtual ~adapted_byte_slice() noexcept final override |
| 81 | {} |
| 82 | |
| 83 | const T buffer; |
| 84 | }; |
| 85 | |
| 86 | // bytes "follow" this structure in memory slab |
| 87 | struct raw_byte_slice final : byte_slice_data |
| 88 | { |
| 89 | raw_byte_slice() noexcept |
| 90 | : byte_slice_data() |
| 91 | {} |
| 92 | |
| 93 | virtual ~raw_byte_slice() noexcept final override |
| 94 | {} |
| 95 | }; |
| 96 | |
| 97 | /* This technique is not-standard, but allows for the reference count and |
| 98 | memory for the bytes (when given a list of spans) to be allocated in a |
| 99 | single call. In that situation, the dynamic sized bytes are after/behind |
| 100 | the raw_byte_slice class. The C runtime has to track the number of bytes |
| 101 | allocated regardless, so free'ing is relatively easy. */ |
| 102 | |
| 103 | template<typename T, typename... U> |
| 104 | std::unique_ptr<T, release_byte_slice> allocate_slice(std::size_t extra_bytes, U&&... args) |
| 105 | { |
| 106 | if (std::numeric_limits<std::size_t>::max() - sizeof(T) < extra_bytes) |
| 107 | throw std::bad_alloc{}; |
| 108 | |
| 109 | void* const ptr = malloc(sizeof(T) + extra_bytes); |
| 110 | if (ptr == nullptr) |
| 111 | throw std::bad_alloc{}; |
| 112 | |
| 113 | try |
| 114 | { |
| 115 | new (ptr) T{std::forward<U>(args)...}; |
| 116 | } |
| 117 | catch (...) |
| 118 | { |
| 119 | free(ptr); |
| 120 | throw; |
| 121 | } |
| 122 | return std::unique_ptr<T, release_byte_slice>{reinterpret_cast<T*>(ptr)}; |
| 123 | } |
| 124 | } // anonymous |
| 125 | |
| 126 | void release_byte_buffer::operator()(std::uint8_t* buf) const noexcept |
| 127 | { |
| 128 | if (buf) |
| 129 | std::free(buf - sizeof(raw_byte_slice)); |
| 130 | } |
| 131 | |
| 132 | byte_slice::byte_slice(byte_slice_data* storage, span<const std::uint8_t> portion) noexcept |
| 133 | : storage_(storage), portion_(portion) |
| 134 | { |
| 135 | if (storage_) |
| 136 | ++(storage_->ref_count); |
| 137 | } |
| 138 | |
| 139 | template<typename T> |
| 140 | byte_slice::byte_slice(const adapt_buffer, T&& buffer) |
| 141 | : storage_(nullptr), portion_(nullptr) |
| 142 | { |
| 143 | if (!buffer.empty()) |
| 144 | { |
| 145 | storage_ = allocate_slice<adapted_byte_slice<T>>(0, std::move(buffer)); |
| 146 | portion_ = to_byte_span(to_span(static_cast<adapted_byte_slice<T> *>(storage_.get())->buffer)); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | byte_slice::byte_slice(std::initializer_list<span<const std::uint8_t>> sources) |
| 151 | : byte_slice() |
| 152 | { |
| 153 | std::size_t space_needed = 0; |
| 154 | for (const auto& source : sources) |
| 155 | { |
| 156 | if (std::numeric_limits<std::size_t>::max() - space_needed < source.size()) |
| 157 | throw std::bad_alloc{}; |
| 158 | space_needed += source.size(); |
| 159 | } |
| 160 | |
| 161 | if (space_needed) |
| 162 | { |
| 163 | auto storage = allocate_slice<raw_byte_slice>(space_needed); |
| 164 | span<std::uint8_t> out{reinterpret_cast<std::uint8_t*>(storage.get() + 1), space_needed}; |
| 165 | portion_ = {out.data(), out.size()}; |
| 166 | |
| 167 | for (const auto& source : sources) |
| 168 | { |
| 169 | assert(source.size() <= out.size()); // see check above |
| 170 | std::memcpy(out.data(), source.data(), source.size()); |
| 171 | out.remove_prefix(source.size()); |
| 172 | } |
| 173 | storage_ = std::move(storage); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | byte_slice::byte_slice(std::string&& buffer) |
| 178 | : byte_slice(adapt_buffer{}, std::move(buffer)) |
| 179 | {} |
| 180 | |
| 181 | byte_slice::byte_slice(std::vector<std::uint8_t>&& buffer) |
| 182 | : byte_slice(adapt_buffer{}, std::move(buffer)) |
| 183 | {} |
| 184 | |
| 185 | byte_slice::byte_slice(byte_stream&& stream, const bool shrink) |
| 186 | : storage_(nullptr), portion_(stream.data(), stream.size()) |
| 187 | { |
| 188 | if (portion_.size()) |
| 189 | { |
| 190 | byte_buffer buf; |
| 191 | if (shrink && page_size <= stream.available()) |
| 192 | { |
| 193 | buf = byte_buffer_resize(stream.take_buffer(), portion_.size()); |
| 194 | if (!buf) |
| 195 | throw std::bad_alloc{}; |
| 196 | portion_ = {buf.get(), portion_.size()}; |
| 197 | } |
| 198 | else // no need to shrink buffer |
| 199 | buf = stream.take_buffer(); |
| 200 | |
| 201 | std::uint8_t* const data = buf.release() - sizeof(raw_byte_slice); |
| 202 | new (data) raw_byte_slice{}; |
| 203 | storage_.reset(reinterpret_cast<raw_byte_slice*>(data)); |
| 204 | } |
| 205 | else // empty stream |
| 206 | portion_ = nullptr; |
| 207 | } |
| 208 | |
| 209 | byte_slice::byte_slice(byte_slice&& source) noexcept |
| 210 | : storage_(std::move(source.storage_)), portion_(source.portion_) |
| 211 | { |
| 212 | source.portion_ = epee::span<const std::uint8_t>{}; |
| 213 | } |
| 214 | |
| 215 | byte_slice& byte_slice::operator=(byte_slice&& source) noexcept |
| 216 | { |
| 217 | storage_ = std::move(source.storage_); |
| 218 | portion_ = source.portion_; |
| 219 | if (source.storage_ == nullptr) |
| 220 | source.portion_ = epee::span<const std::uint8_t>{}; |
| 221 | |
| 222 | return *this; |
| 223 | } |
| 224 | |
| 225 | std::size_t byte_slice::remove_prefix(std::size_t max_bytes) noexcept |
| 226 | { |
| 227 | max_bytes = portion_.remove_prefix(max_bytes); |
| 228 | if (portion_.empty()) |
| 229 | storage_ = nullptr; |
| 230 | return max_bytes; |
| 231 | } |
| 232 | |
| 233 | byte_slice byte_slice::take_slice(const std::size_t max_bytes) noexcept |
| 234 | { |
| 235 | byte_slice out{}; |
| 236 | |
| 237 | if (max_bytes) |
| 238 | { |
| 239 | std::uint8_t const* const ptr = data(); |
| 240 | out.portion_ = {ptr, portion_.remove_prefix(max_bytes)}; |
| 241 | |
| 242 | if (portion_.empty()) |
| 243 | out.storage_ = std::move(storage_); // no atomic inc/dec |
| 244 | else |
| 245 | out = {storage_.get(), out.portion_}; |
| 246 | } |
| 247 | return out; |
| 248 | } |
| 249 | |
| 250 | byte_slice byte_slice::get_slice(const std::size_t begin, const std::size_t end) const |
| 251 | { |
| 252 | if (end < begin || portion_.size() < end) |
| 253 | throw std::out_of_range{"bad slice range"}; |
| 254 | |
| 255 | if (begin == end) |
| 256 | return {}; |
| 257 | return {storage_.get(), {portion_.begin() + begin, end - begin}}; |
| 258 | } |
| 259 | |
| 260 | std::unique_ptr<byte_slice_data, release_byte_slice> byte_slice::take_buffer() noexcept |
| 261 | { |
| 262 | std::unique_ptr<byte_slice_data, release_byte_slice> out{std::move(storage_)}; |
| 263 | portion_ = nullptr; |
| 264 | return out; |
| 265 | } |
| 266 | |
| 267 | byte_buffer byte_buffer_resize(byte_buffer buf, const std::size_t length) noexcept |
| 268 | { |
| 269 | if (std::numeric_limits<std::size_t>::max() - sizeof(raw_byte_slice) < length) |
| 270 | return nullptr; |
| 271 | |
| 272 | std::uint8_t* data = buf.get(); |
| 273 | if (data != nullptr) |
| 274 | data -= sizeof(raw_byte_slice); |
| 275 | |
| 276 | data = static_cast<std::uint8_t*>(std::realloc(data, sizeof(raw_byte_slice) + length)); |
| 277 | if (data == nullptr) |
| 278 | return nullptr; |
| 279 | |
| 280 | buf.release(); |
| 281 | buf.reset(data + sizeof(raw_byte_slice)); |
| 282 | return buf; |
| 283 | } |
| 284 | |
| 285 | byte_buffer byte_buffer_increase(byte_buffer buf, const std::size_t current, const std::size_t more) |
| 286 | { |
| 287 | if (std::numeric_limits<std::size_t>::max() - current < more) |
| 288 | throw std::range_error{"byte_buffer_increase size_t overflow"}; |
| 289 | return byte_buffer_resize(std::move(buf), current + more); |
| 290 | } |
| 291 | } // epee |