| 1 |
/* Copyright (c) 2023, 2026, Oracle and/or its affiliates. |
| 2 |
|
| 3 |
This program is free software; you can redistribute it and/or modify |
| 4 |
it under the terms of the GNU General Public License, version 2.0, |
| 5 |
as published by the Free Software Foundation. |
| 6 |
|
| 7 |
This program is designed to work with certain software (including |
| 8 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 9 |
as designated in a particular file or component or in included license |
| 10 |
documentation. The authors of MySQL hereby grant you an additional |
| 11 |
permission to link the program and your derivative works with the |
| 12 |
separately licensed software that they have either included with |
| 13 |
the program or referenced in the documentation. |
| 14 |
|
| 15 |
This program is distributed in the hope that it will be useful, |
| 16 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
GNU General Public License, version 2.0, for more details. |
| 19 |
|
| 20 |
You should have received a copy of the GNU General Public License |
| 21 |
along with this program; if not, write to the Free Software |
| 22 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 |
|
| 24 |
#include "compression/payload_event_buffer_istream.h" |
| 25 |
|
| 26 |
#include <byteorder.h> |
| 27 |
#include "string/concat.h" |
| 28 |
#include "wrapper_functions.h" |
| 29 |
|
| 30 |
using mysqlns::string::concat; |
| 31 |
|
| 32 |
namespace binary_log::transaction::compression { |
| 33 |
|
| 34 |
Payload_event_buffer_istream::Payload_event_buffer_istream( |
| 35 |
const Transaction_payload_event &transaction_payload_log_event, |
| 36 |
Size_t default_buffer_size, const Memory_resource_t &memory_resource) |
| 37 |
: Payload_event_buffer_istream( |
| 38 |
reinterpret_cast<const Char_t *>( |
| 39 |
transaction_payload_log_event.get_payload()), |
| 40 |
transaction_payload_log_event.get_payload_size(), |
| 41 |
transaction_payload_log_event.get_compression_type(), |
| 42 |
default_buffer_size, memory_resource) { |
| 43 |
m_grow_calculator.set_max_size(max_log_event_size); |
| 44 |
} |
| 45 |
|
| 46 |
Payload_event_buffer_istream::Payload_event_buffer_istream( |
| 47 |
const std::shared_ptr<const Transaction_payload_event> &tple, |
| 48 |
Size_t default_buffer_size, const Memory_resource_t &memory_resource) |
| 49 |
: Payload_event_buffer_istream(*tple, default_buffer_size, |
| 50 |
memory_resource) { |
| 51 |
// Keep a reference just to protect deletion. |
| 52 |
// Nolint: this cannot be an initializer since we delegate |
| 53 |
// initialization to another constructor. |
| 54 |
// NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer) |
| 55 |
m_tple = tple; |
| 56 |
|
| 57 |
m_grow_calculator.set_max_size(max_log_event_size); |
| 58 |
} |
| 59 |
|
| 60 |
Payload_event_buffer_istream::~Payload_event_buffer_istream() { |
| 61 |
#ifndef NDEBUG |
| 62 |
// Payload_event_buffer_istream has reported error or EOF, but the |
| 63 |
// calling code has not checked which case it is. This is probably a |
| 64 |
// programming mistake. Remember to always check for error after a |
| 65 |
// read loop has ended. |
| 66 |
assert(!m_outstanding_error); |
| 67 |
#endif |
| 68 |
} |
| 69 |
|
| 70 |
bool Payload_event_buffer_istream::operator!() const { |
| 71 |
return m_status != Status_t::success; |
| 72 |
} |
| 73 |
|
| 74 |
Payload_event_buffer_istream::operator bool() const { |
| 75 |
return m_status == Status_t::success; |
| 76 |
} |
| 77 |
|
| 78 |
Payload_event_buffer_istream::Status_t |
| 79 |
Payload_event_buffer_istream::get_status() const { |
| 80 |
#ifndef NDEBUG |
| 81 |
m_outstanding_error = false; |
| 82 |
#endif |
| 83 |
return m_status; |
| 84 |
} |
| 85 |
|
| 86 |
void Payload_event_buffer_istream::set_status(const Status_t status) { |
| 87 |
BAPI_TRACE; |
| 88 |
BAPI_LOG("info", "status=" << status); |
| 89 |
#ifndef NDEBUG |
| 90 |
if (status != Status_t::success) m_outstanding_error = true; |
| 91 |
#endif |
| 92 |
m_status = status; |
| 93 |
} |
| 94 |
|
| 95 |
bool Payload_event_buffer_istream::has_error() const { |
| 96 |
#ifndef NDEBUG |
| 97 |
m_outstanding_error = false; |
| 98 |
#endif |
| 99 |
return m_status != Status_t::end && m_status != Status_t::success; |
| 100 |
} |
| 101 |
|
| 102 |
std::string Payload_event_buffer_istream::get_error_str() const { |
| 103 |
#ifndef NDEBUG |
| 104 |
m_outstanding_error = false; |
| 105 |
#endif |
| 106 |
return m_error_str; |
| 107 |
} |
| 108 |
|
| 109 |
void Payload_event_buffer_istream::set_error_str(const std::string &s) { |
| 110 |
BAPI_TRACE; |
| 111 |
BAPI_LOG("info", "Error: " << s); |
| 112 |
m_error_str = s; |
| 113 |
} |
| 114 |
|
| 115 |
void Payload_event_buffer_istream::initialize() { |
| 116 |
try { |
| 117 |
m_decompressor = Factory_t::build_decompressor(m_compression_algorithm); |
| 118 |
if (m_decompressor == nullptr) { |
| 119 |
set_error_str( |
| 120 |
concat("Unknown compression algorithm in Payload_log_event: ", |
| 121 |
m_compression_algorithm, ".")); |
| 122 |
set_status(Status_t::corrupted); |
| 123 |
return; |
| 124 |
} |
| 125 |
m_decompressor->feed(m_compressed_buffer, m_compressed_buffer_size); |
| 126 |
} catch (const std::bad_alloc &) { |
| 127 |
set_error_str( |
| 128 |
"Out of memory while allocating decompressor in Payload_log_event."); |
| 129 |
set_status(Status_t::out_of_memory); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
void Payload_event_buffer_istream::update_buffer() { |
| 134 |
if (m_status != Status_t::success) { |
| 135 |
BAPI_LOG("info", "returning pre-existing status " |
| 136 |
<< (int)m_status << " with error " << get_error_str()); |
| 137 |
return; |
| 138 |
} |
| 139 |
|
| 140 |
// Allocate a new buffer if it has not yet been allocated, or if |
| 141 |
// caller still holds a reference to the existing one. |
| 142 |
if (m_managed_buffer_ptr.use_count() != 1) { |
| 143 |
#ifndef NDEBUG |
| 144 |
// "nolint": clang-tidy reports an unnecessary warning since 'if' |
| 145 |
// and 'else' branches may compile to the same. Suppressing that. |
| 146 |
// NOLINTBEGIN(bugprone-branch-clone) |
| 147 |
if (m_managed_buffer_ptr.use_count() == 0) |
| 148 |
BAPI_LOG("info", "Allocating managed buffer for the first time."); |
| 149 |
else |
| 150 |
BAPI_LOG("info", |
| 151 |
"Allocating new managed buffer. " |
| 152 |
"The previous one cannot be reused since there are " |
| 153 |
<< m_managed_buffer_ptr.use_count() |
| 154 |
<< ">1 shared pointer references to " |
| 155 |
"it."); |
| 156 |
// NOLINTEND(bugprone-branch-clone) |
| 157 |
#endif |
| 158 |
try { |
| 159 |
auto allocator = Allocator_t<Managed_buffer_t>(m_memory_resource); |
| 160 |
auto *managed_buffer = allocator.allocate(1); |
| 161 |
new (managed_buffer) |
| 162 |
Managed_buffer_t(m_default_buffer_size, m_memory_resource); |
| 163 |
m_managed_buffer_ptr.reset(managed_buffer, allocator.get_deleter()); |
| 164 |
} catch (const std::bad_alloc &) { |
| 165 |
set_error_str( |
| 166 |
"Out of memory while allocating output buffer in " |
| 167 |
"Payload_log_event."); |
| 168 |
set_status(Status_t::out_of_memory); |
| 169 |
return; |
| 170 |
} |
| 171 |
} else { |
| 172 |
BAPI_LOG("info", "Reusing and overwriting existing managed buffer."); |
| 173 |
} |
| 174 |
m_managed_buffer_ptr->set_position(0); |
| 175 |
// Combine the compressor's Grow_constraint with the user-specified |
| 176 |
// Grow_policy |
| 177 |
const auto &decompressor_constraint = |
| 178 |
m_decompressor->get_grow_constraint_hint(); |
| 179 |
const auto grow_calculator = |
| 180 |
decompressor_constraint.combine_with(m_grow_calculator); |
| 181 |
m_managed_buffer_ptr->set_grow_calculator(grow_calculator); |
| 182 |
} |
| 183 |
|
| 184 |
void Payload_event_buffer_istream::read_event() { |
| 185 |
// Read up to and including the event length. |
| 186 |
constexpr uint32_t header_length = EVENT_LEN_OFFSET + 4; |
| 187 |
uint32_t read_length = header_length; |
| 188 |
auto status = m_decompressor->decompress(*m_managed_buffer_ptr, read_length); |
| 189 |
if (status == Status_t::success) { |
| 190 |
// Read up to and including the type code. |
| 191 |
Event_reader reader{reinterpret_cast<const char *>( |
| 192 |
m_managed_buffer_ptr->read_part().begin()), |
| 193 |
read_length}; |
| 194 |
reader.go_to(EVENT_TYPE_OFFSET); |
| 195 |
auto event_type = reader.read<uint8_t>(); |
| 196 |
// Error is impossible; we decompressed EVENT_LEN_OFFSET+4 bytes and |
| 197 |
// EVENT_LEN_OFFSET > EVENT_TYPE_OFFSET. |
| 198 |
assert(!reader.get_error()); |
| 199 |
|
| 200 |
// Check validity of type code. |
| 201 |
if (event_type == binary_log::TRANSACTION_PAYLOAD_EVENT) { |
| 202 |
// A valid TPLE cannot contain a TPLE, so detect it and error |
| 203 |
// out. Without such a check, a maliciously crafted TPLE may |
| 204 |
// contain itself compressed (a "quine"), which might lead to |
| 205 |
// infinite loops or infinite recursion in API client code that |
| 206 |
// uses the iterator, like: |
| 207 |
// void handle_event(Log_event *e): |
| 208 |
// if e is a TRANSACTION_PAYLOAD_EVENT: |
| 209 |
// for each event e' contained in the payload of e: |
| 210 |
// handle_event(e'); |
| 211 |
set_error_str( |
| 212 |
"Payload_log_event corrupted: contains an embedded " |
| 213 |
"Payload_log_event"); |
| 214 |
set_status(Status_t::corrupted); |
| 215 |
return; |
| 216 |
} |
| 217 |
|
| 218 |
// Decode event length. |
| 219 |
reader.go_to(EVENT_LEN_OFFSET); |
| 220 |
read_length = reader.read<uint32_t>(); |
| 221 |
if (read_length < LOG_EVENT_HEADER_LEN) { |
| 222 |
set_error_str(concat( |
| 223 |
"Length field of embedded event in Payload_log_event is only ", |
| 224 |
read_length, " bytes, but ", LOG_EVENT_HEADER_LEN, " are required.")); |
| 225 |
set_status(Status_t::corrupted); |
| 226 |
return; |
| 227 |
} |
| 228 |
// Error is impossible; we have EVENT_LEN_OFFSET+4 bytes. |
| 229 |
assert(!reader.get_error()); |
| 230 |
|
| 231 |
// Decompress the rest of the event. |
| 232 |
status = m_decompressor->decompress(*m_managed_buffer_ptr, |
| 233 |
read_length - header_length); |
| 234 |
if (status == Status_t::end) { |
| 235 |
// The read operation encountered 'END' after reading just a |
| 236 |
// partial header, so the event as a whole was 'TRUNCATED'. |
| 237 |
status = Status_t::truncated; |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
switch (status) { |
| 242 |
case Status_t::success: |
| 243 |
return; |
| 244 |
case Status_t::end: |
| 245 |
set_status(status); |
| 246 |
return; |
| 247 |
case Status_t::truncated: |
| 248 |
set_error_str( |
| 249 |
"Payload_log_event corrupted: the compressed payload has been " |
| 250 |
"truncated."); |
| 251 |
// This is a form of corruption; don't distinguish the error status. |
| 252 |
set_status(Status_t::corrupted); |
| 253 |
return; |
| 254 |
case Status_t::corrupted: |
| 255 |
set_error_str( |
| 256 |
"Payload_log_event corrupted: compression stream is " |
| 257 |
"corrupted."); |
| 258 |
set_status(status); |
| 259 |
return; |
| 260 |
case Status_t::exceeds_max_size: { |
| 261 |
set_error_str( |
| 262 |
concat("Length field of embedded event in Payload_log_event is ", |
| 263 |
read_length, " bytes, exceeding the maximum of ", |
| 264 |
m_managed_buffer_ptr->get_grow_calculator().get_max_size(), |
| 265 |
" bytes.")); |
| 266 |
set_status(status); |
| 267 |
return; |
| 268 |
} |
| 269 |
case Status_t::out_of_memory: |
| 270 |
set_error_str( |
| 271 |
"Out of memory while decompressing event embedded in " |
| 272 |
"Payload_log_event."); |
| 273 |
set_status(status); |
| 274 |
return; |
| 275 |
default: |
| 276 |
// Can't happen |
| 277 |
break; |
| 278 |
} |
| 279 |
assert(false); // Can't happen |
| 280 |
set_status(Status_t::corrupted); |
| 281 |
} |
| 282 |
|
| 283 |
Payload_event_buffer_istream &Payload_event_buffer_istream::operator>>( |
| 284 |
Buffer_ptr_t &out) { |
| 285 |
// Remove one from the use_count. In the common case where it is the |
| 286 |
// only other pointer to m_managed_buffer_ptr, this allows us to |
| 287 |
// reuse m_managed_buffer_ptr. Also, in the less common case where |
| 288 |
// user passes the last pointer to some other buffer, we free that |
| 289 |
// memory before we allocate more. |
| 290 |
out.reset(); |
| 291 |
// Produce the object in m_managed_buffer_ptr. |
| 292 |
next(); |
| 293 |
// On success, share the read part with the caller. |
| 294 |
if (m_status == Status_t::success) |
| 295 |
out = |
| 296 |
Buffer_ptr_t(m_managed_buffer_ptr, &m_managed_buffer_ptr->read_part()); |
| 297 |
return *this; |
| 298 |
} |
| 299 |
|
| 300 |
Payload_event_buffer_istream &Payload_event_buffer_istream::operator>>( |
| 301 |
Managed_buffer_ptr_t &out) { |
| 302 |
// Remove one from the use_count. In the common case where it is the |
| 303 |
// only other pointer to m_managed_buffer_ptr, this allows us to |
| 304 |
// reuse m_managed_buffer_ptr. Also, in the less common case where |
| 305 |
// user passes the last pointer to some other buffer, we free that |
| 306 |
// memory before we allocate more. |
| 307 |
out.reset(); |
| 308 |
// Produce the object in m_managed_buffer_ptr. |
| 309 |
next(); |
| 310 |
// On success, share the managed buffer with the caller. |
| 311 |
if (m_status == Status_t::success) out = m_managed_buffer_ptr; |
| 312 |
return *this; |
| 313 |
} |
| 314 |
|
| 315 |
void Payload_event_buffer_istream::next() { |
| 316 |
BAPI_TRACE; |
| 317 |
|
| 318 |
// If we failed once, we failed and can't be repaired. (It would |
| 319 |
// have been "nice" if, after `out_of_memory` or `exceeds_max_size`, |
| 320 |
// users had the chance to free memory or increase the max size and |
| 321 |
// then retry. But `read_event` is not idempotent; it may have |
| 322 |
// decompressed a half event header before the failure, in which |
| 323 |
// case it has already advanced the stream, making it impossible to |
| 324 |
// resume.) |
| 325 |
if (m_status != Status_t::success) return; |
| 326 |
update_buffer(); |
| 327 |
if (m_status != Status_t::success) return; |
| 328 |
read_event(); |
| 329 |
BAPI_LOG("info", "Status: " << debug_string(m_status)); |
| 330 |
} |
| 331 |
|
| 332 |
const Payload_event_buffer_istream::Grow_calculator_t & |
| 333 |
Payload_event_buffer_istream::get_grow_calculator() const { |
| 334 |
return m_grow_calculator; |
| 335 |
} |
| 336 |
|
| 337 |
void Payload_event_buffer_istream::set_grow_calculator( |
| 338 |
const Payload_event_buffer_istream::Grow_calculator_t &grow_calculator) { |
| 339 |
m_grow_calculator = grow_calculator; |
| 340 |
} |
| 341 |
|
| 342 |
} // namespace binary_log::transaction::compression |