1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/351564777): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "ppapi/proxy/serialized_var.h"
#include "base/check.h"
#include "base/functional/bind.h"
#include "base/notreached.h"
#include "ipc/ipc_message_utils.h"
#include "ppapi/c/pp_instance.h"
#include "ppapi/proxy/dispatcher.h"
#include "ppapi/proxy/interface_proxy.h"
#include "ppapi/proxy/ppapi_param_traits.h"
#include "ppapi/proxy/ppb_buffer_proxy.h"
#include "ppapi/shared_impl/ppapi_globals.h"
#include "ppapi/shared_impl/var.h"
#include "ppapi/thunk/enter.h"
namespace ppapi {
namespace proxy {
namespace {
void DefaultHandleWriter(base::Pickle* m, const SerializedHandle& handle) {
IPC::ParamTraits<SerializedHandle>::Write(m, handle);
}
} // namespace
// SerializedVar::Inner --------------------------------------------------------
SerializedVar::Inner::Inner()
: serialization_rules_(nullptr),
var_(PP_MakeUndefined()),
instance_(0),
cleanup_mode_(CLEANUP_NONE),
is_valid_var_(true) {
#ifndef NDEBUG
has_been_serialized_ = false;
has_been_deserialized_ = false;
#endif
}
SerializedVar::Inner::Inner(VarSerializationRules* serialization_rules)
: serialization_rules_(serialization_rules),
var_(PP_MakeUndefined()),
instance_(0),
cleanup_mode_(CLEANUP_NONE) {
#ifndef NDEBUG
has_been_serialized_ = false;
has_been_deserialized_ = false;
#endif
}
SerializedVar::Inner::~Inner() {
switch (cleanup_mode_) {
case END_SEND_PASS_REF:
serialization_rules_->EndSendPassRef(var_);
break;
case END_RECEIVE_CALLER_OWNED:
serialization_rules_->EndReceiveCallerOwned(var_);
break;
default:
break;
}
}
PP_Var SerializedVar::Inner::GetVar() {
DCHECK(serialization_rules_.get());
if (raw_var_data_.get()) {
var_ = raw_var_data_->CreatePPVar(instance_);
raw_var_data_.reset(nullptr);
}
return var_;
}
void SerializedVar::Inner::SetVar(PP_Var var) {
// Sanity check, when updating the var we should have received a
// serialization rules pointer already.
DCHECK(serialization_rules_.get());
var_ = var;
raw_var_data_.reset(nullptr);
}
void SerializedVar::Inner::SetInstance(PP_Instance instance) {
instance_ = instance;
}
void SerializedVar::Inner::ForceSetVarValueForTest(PP_Var value) {
var_ = value;
raw_var_data_.reset(nullptr);
}
void SerializedVar::Inner::WriteToMessage(base::Pickle* m) const {
// When writing to the IPC messages, a serialization rules handler should
// always have been set.
//
// When sending a message, it should be difficult to trigger this if you're
// using the SerializedVarSendInput class and giving a non-null dispatcher.
// Make sure you're using the proper "Send" helper class.
//
// It should be more common to see this when handling an incoming message
// that returns a var. This means the message handler didn't write to the
// output parameter, or possibly you used the wrong helper class
// (normally SerializedVarReturnValue).
DCHECK(serialization_rules_.get());
#ifndef NDEBUG
// We should only be serializing something once.
DCHECK(!has_been_serialized_);
has_been_serialized_ = true;
#endif
std::unique_ptr<RawVarDataGraph> data =
RawVarDataGraph::Create(var_, instance_);
if (data) {
m->WriteBool(true); // Success.
data->Write(m, base::BindRepeating(&DefaultHandleWriter));
} else {
m->WriteBool(false); // Failure.
}
}
void SerializedVar::Inner::WriteDataToMessage(
base::Pickle* m,
const HandleWriter& handle_writer) const {
if (raw_var_data_) {
m->WriteBool(true); // Success.
raw_var_data_->Write(m, handle_writer);
} else {
m->WriteBool(false); // Failure.
}
}
bool SerializedVar::Inner::ReadFromMessage(const base::Pickle* m,
base::PickleIterator* iter) {
#ifndef NDEBUG
// We should only deserialize something once or will end up with leaked
// references.
//
// One place this has happened in the past is using
// std::vector<SerializedVar>.resize(). If you're doing this manually instead
// of using the helper classes for handling in/out vectors of vars, be
// sure you use the same pattern as the SerializedVarVector classes.
DCHECK(!has_been_deserialized_);
has_been_deserialized_ = true;
#endif
// When reading, the dispatcher should be set when we get a Deserialize
// call (which will supply a dispatcher).
if (!iter->ReadBool(&is_valid_var_))
return false;
if (is_valid_var_) {
raw_var_data_ = RawVarDataGraph::Read(m, iter);
if (!raw_var_data_)
return false;
}
return true;
}
void SerializedVar::Inner::SetCleanupModeToEndSendPassRef() {
cleanup_mode_ = END_SEND_PASS_REF;
}
void SerializedVar::Inner::SetCleanupModeToEndReceiveCallerOwned() {
cleanup_mode_ = END_RECEIVE_CALLER_OWNED;
}
// SerializedVar ---------------------------------------------------------------
SerializedVar::SerializedVar() : inner_(new Inner) {
}
SerializedVar::SerializedVar(VarSerializationRules* serialization_rules)
: inner_(new Inner(serialization_rules)) {
}
SerializedVar::~SerializedVar() {
}
// SerializedVarSendInput ------------------------------------------------------
SerializedVarSendInput::SerializedVarSendInput(Dispatcher* dispatcher,
const PP_Var& var)
: SerializedVar(dispatcher->serialization_rules()) {
inner_->SetVar(dispatcher->serialization_rules()->SendCallerOwned(var));
}
// static
void SerializedVarSendInput::ConvertVector(Dispatcher* dispatcher,
const PP_Var* input,
size_t input_count,
std::vector<SerializedVar>* output) {
output->reserve(input_count);
for (size_t i = 0; i < input_count; i++)
output->push_back(SerializedVarSendInput(dispatcher, input[i]));
}
// SerializedVarSendInputShmem -------------------------------------------------
SerializedVarSendInputShmem::SerializedVarSendInputShmem(
Dispatcher* dispatcher,
const PP_Var& var,
const PP_Instance& instance)
: SerializedVar(dispatcher->serialization_rules()) {
inner_->SetVar(dispatcher->serialization_rules()->SendCallerOwned(var));
inner_->SetInstance(instance);
}
// ReceiveSerializedVarReturnValue ---------------------------------------------
ReceiveSerializedVarReturnValue::ReceiveSerializedVarReturnValue() {
}
ReceiveSerializedVarReturnValue::ReceiveSerializedVarReturnValue(
const SerializedVar& serialized)
: SerializedVar(serialized) {
}
PP_Var ReceiveSerializedVarReturnValue::Return(Dispatcher* dispatcher) {
inner_->set_serialization_rules(dispatcher->serialization_rules());
inner_->SetVar(inner_->serialization_rules()->ReceivePassRef(
inner_->GetVar()));
return inner_->GetVar();
}
// ReceiveSerializedException --------------------------------------------------
ReceiveSerializedException::ReceiveSerializedException(Dispatcher* dispatcher,
PP_Var* exception)
: SerializedVar(dispatcher->serialization_rules()),
exception_(exception) {
}
ReceiveSerializedException::~ReceiveSerializedException() {
if (exception_) {
// When an output exception is specified, it will take ownership of the
// reference.
inner_->SetVar(
inner_->serialization_rules()->ReceivePassRef(inner_->GetVar()));
*exception_ = inner_->GetVar();
} else {
// When no output exception is specified, the browser thinks we have a ref
// to an object that we don't want (this will happen only in the plugin
// since the browser will always specify an out exception for the plugin to
// write into).
//
// Strings don't need this handling since we can just avoid creating a
// Var from the std::string in the first place.
if (inner_->GetVar().type == PP_VARTYPE_OBJECT)
inner_->serialization_rules()->ReleaseObjectRef(inner_->GetVar());
}
}
bool ReceiveSerializedException::IsThrown() const {
return exception_ && exception_->type != PP_VARTYPE_UNDEFINED;
}
// ReceiveSerializedVarVectorOutParam ------------------------------------------
ReceiveSerializedVarVectorOutParam::ReceiveSerializedVarVectorOutParam(
Dispatcher* dispatcher,
uint32_t* output_count,
PP_Var** output)
: dispatcher_(dispatcher),
output_count_(output_count),
output_(output) {
}
ReceiveSerializedVarVectorOutParam::~ReceiveSerializedVarVectorOutParam() {
*output_count_ = static_cast<uint32_t>(vector_.size());
if (vector_.empty()) {
*output_ = nullptr;
return;
}
*output_ = static_cast<PP_Var*>(malloc(vector_.size() * sizeof(PP_Var)));
for (size_t i = 0; i < vector_.size(); i++) {
// Here we just mimic what happens when returning a value.
ReceiveSerializedVarReturnValue converted;
SerializedVar* serialized = &converted;
*serialized = vector_[i];
(*output_)[i] = converted.Return(dispatcher_);
}
}
std::vector<SerializedVar>* ReceiveSerializedVarVectorOutParam::OutParam() {
return &vector_;
}
// SerializedVarReceiveInput ---------------------------------------------------
SerializedVarReceiveInput::SerializedVarReceiveInput(
const SerializedVar& serialized)
: serialized_(serialized) {
}
SerializedVarReceiveInput::~SerializedVarReceiveInput() {
}
PP_Var SerializedVarReceiveInput::Get(Dispatcher* dispatcher) {
serialized_.inner_->set_serialization_rules(
dispatcher->serialization_rules());
// Ensure that when the serialized var goes out of scope it cleans up the
// stuff we're making in BeginReceiveCallerOwned.
serialized_.inner_->SetCleanupModeToEndReceiveCallerOwned();
serialized_.inner_->SetVar(
serialized_.inner_->serialization_rules()->BeginReceiveCallerOwned(
serialized_.inner_->GetVar()));
return serialized_.inner_->GetVar();
}
PP_Var SerializedVarReceiveInput::GetForInstance(Dispatcher* dispatcher,
PP_Instance instance) {
serialized_.inner_->SetInstance(instance);
return Get(dispatcher);
}
// SerializedVarVectorReceiveInput ---------------------------------------------
SerializedVarVectorReceiveInput::SerializedVarVectorReceiveInput(
const std::vector<SerializedVar>& serialized)
: serialized_(serialized) {
}
SerializedVarVectorReceiveInput::~SerializedVarVectorReceiveInput() {
for (size_t i = 0; i < deserialized_.size(); i++) {
serialized_[i].inner_->serialization_rules()->EndReceiveCallerOwned(
deserialized_[i]);
}
}
PP_Var* SerializedVarVectorReceiveInput::Get(Dispatcher* dispatcher,
uint32_t* array_size) {
deserialized_.resize(serialized_.size());
for (size_t i = 0; i < serialized_.size(); i++) {
// The vectors must be able to clean themselves up after this call is
// torn down.
serialized_[i].inner_->set_serialization_rules(
dispatcher->serialization_rules());
serialized_[i].inner_->SetVar(
serialized_[i].inner_->serialization_rules()->BeginReceiveCallerOwned(
serialized_[i].inner_->GetVar()));
deserialized_[i] = serialized_[i].inner_->GetVar();
}
*array_size = static_cast<uint32_t>(serialized_.size());
return deserialized_.empty() ? nullptr : &deserialized_[0];
}
// SerializedVarReturnValue ----------------------------------------------------
SerializedVarReturnValue::SerializedVarReturnValue(SerializedVar* serialized)
: serialized_(serialized) {
}
void SerializedVarReturnValue::Return(Dispatcher* dispatcher,
const PP_Var& var) {
serialized_->inner_->set_serialization_rules(
dispatcher->serialization_rules());
// Var must clean up after our BeginSendPassRef call.
serialized_->inner_->SetCleanupModeToEndSendPassRef();
serialized_->inner_->SetVar(
dispatcher->serialization_rules()->BeginSendPassRef(var));
}
// static
SerializedVar SerializedVarReturnValue::Convert(Dispatcher* dispatcher,
const PP_Var& var) {
// Mimic what happens in the normal case.
SerializedVar result;
SerializedVarReturnValue retvalue(&result);
retvalue.Return(dispatcher, var);
return result;
}
// SerializedVarOutParam -------------------------------------------------------
SerializedVarOutParam::SerializedVarOutParam(SerializedVar* serialized)
: serialized_(serialized),
writable_var_(PP_MakeUndefined()),
dispatcher_(nullptr) {}
SerializedVarOutParam::~SerializedVarOutParam() {
if (serialized_->inner_->serialization_rules()) {
// When unset, OutParam wasn't called. We'll just leave the var untouched
// in that case.
serialized_->inner_->SetVar(
serialized_->inner_->serialization_rules()->BeginSendPassRef(
writable_var_));
// Normally the current object will be created on the stack to wrap a
// SerializedVar and won't have a scope around the actual IPC send. So we
// need to tell the SerializedVar to do the begin/end send pass ref calls.
serialized_->inner_->SetCleanupModeToEndSendPassRef();
}
}
PP_Var* SerializedVarOutParam::OutParam(Dispatcher* dispatcher) {
dispatcher_ = dispatcher;
serialized_->inner_->set_serialization_rules(
dispatcher->serialization_rules());
return &writable_var_;
}
// SerializedVarVectorOutParam -------------------------------------------------
SerializedVarVectorOutParam::SerializedVarVectorOutParam(
std::vector<SerializedVar>* serialized)
: dispatcher_(nullptr),
serialized_(serialized),
count_(0),
array_(nullptr) {}
SerializedVarVectorOutParam::~SerializedVarVectorOutParam() {
DCHECK(dispatcher_);
// Convert the array written by the pepper code to the serialized structure.
// Note we can't use resize here, we have to allocate a new SerializedVar
// for each serialized item. See ParamTraits<vector<SerializedVar>>::Read.
serialized_->reserve(count_);
for (uint32_t i = 0; i < count_; i++) {
// Just mimic what we do for regular OutParams.
SerializedVar var;
SerializedVarOutParam out(&var);
*out.OutParam(dispatcher_) = array_[i];
serialized_->push_back(var);
}
// When returning arrays, the pepper code expects the caller to take
// ownership of the array.
free(array_);
}
PP_Var** SerializedVarVectorOutParam::ArrayOutParam(Dispatcher* dispatcher) {
DCHECK(!dispatcher_); // Should only be called once.
dispatcher_ = dispatcher;
return &array_;
}
SerializedVarTestConstructor::SerializedVarTestConstructor(
const PP_Var& pod_var) {
DCHECK(pod_var.type != PP_VARTYPE_STRING);
inner_->ForceSetVarValueForTest(pod_var);
}
SerializedVarTestConstructor::SerializedVarTestConstructor(
const std::string& str) {
inner_->ForceSetVarValueForTest(StringVar::StringToPPVar(str));
}
SerializedVarTestReader::SerializedVarTestReader(const SerializedVar& var)
: SerializedVar(var) {
}
} // namespace proxy
} // namespace ppapi
|