DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (95ab8f81aa22)

VCS Links

Line Code
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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "MiddlemanCall.h"

#include <unordered_map>

namespace mozilla {
namespace recordreplay {

typedef std::unordered_map<const void*, MiddlemanCall*> MiddlemanCallMap;

// State used for keeping track of middleman calls in either a replaying
// process or middleman process.
struct MiddlemanCallState {
  // In a replaying or middleman process, all middleman calls that have been
  // encountered, indexed by their ID.
  InfallibleVector<MiddlemanCall*> mCalls;

  // In a replaying or middleman process, association between values produced by
  // a middleman call and the call itself.
  MiddlemanCallMap mCallMap;

  // In a middleman process, any buffers allocated for performed calls.
  InfallibleVector<void*> mAllocatedBuffers;
};

// In a replaying process, all middleman call state. In a middleman process,
// state for the child currently being processed.
static MiddlemanCallState* gState;

// In a middleman process, middleman call state for each child process, indexed
// by the child ID.
static StaticInfallibleVector<MiddlemanCallState*> gStatePerChild;

// In a replaying process, lock protecting middleman call state. In the
// middleman, all accesses occur on the main thread.
static Monitor* gMonitor;

void InitializeMiddlemanCalls() {
  MOZ_RELEASE_ASSERT(IsRecordingOrReplaying() || IsMiddleman());

  if (IsReplaying()) {
    gState = new MiddlemanCallState();
    gMonitor = new Monitor();
  }
}

// Apply the ReplayInput phase to aCall and any calls it depends on that have
// not been sent to the middleman yet, filling aOutgoingCalls with the set of
// such calls.
static bool GatherDependentCalls(
    InfallibleVector<MiddlemanCall*>& aOutgoingCalls, MiddlemanCall* aCall) {
  MOZ_RELEASE_ASSERT(!aCall->mSent);
  aCall->mSent = true;

  const Redirection& redirection = GetRedirection(aCall->mCallId);

  CallArguments arguments;
  aCall->mArguments.CopyTo(&arguments);

  InfallibleVector<MiddlemanCall*> dependentCalls;

  MiddlemanCallContext cx(aCall, &arguments, MiddlemanCallPhase::ReplayInput);
  cx.mDependentCalls = &dependentCalls;
  redirection.mMiddlemanCall(cx);
  if (cx.mFailed) {
    if (child::CurrentRepaintCannotFail()) {
      child::ReportFatalError(Nothing(), "Middleman call input failed: %s\n",
                              redirection.mName);
    }
    return false;
  }

  for (MiddlemanCall* dependent : dependentCalls) {
    if (!dependent->mSent && !GatherDependentCalls(aOutgoingCalls, dependent)) {
      return false;
    }
  }

  aOutgoingCalls.append(aCall);
  return true;
}

bool SendCallToMiddleman(size_t aCallId, CallArguments* aArguments,
                         bool aDiverged) {
  MOZ_RELEASE_ASSERT(IsReplaying());

  const Redirection& redirection = GetRedirection(aCallId);
  MOZ_RELEASE_ASSERT(redirection.mMiddlemanCall);

  MonitorAutoLock lock(*gMonitor);

  // Allocate and fill in a new MiddlemanCall.
  size_t id = gState->mCalls.length();
  MiddlemanCall* newCall = new MiddlemanCall();
  gState->mCalls.emplaceBack(newCall);
  newCall->mId = id;
  newCall->mCallId = aCallId;
  newCall->mArguments.CopyFrom(aArguments);

  // Perform the ReplayPreface phase on the new call.
  {
    MiddlemanCallContext cx(newCall, aArguments,
                            MiddlemanCallPhase::ReplayPreface);
    redirection.mMiddlemanCall(cx);
    if (cx.mFailed) {
      delete newCall;
      gState->mCalls.popBack();
      if (child::CurrentRepaintCannotFail()) {
        child::ReportFatalError(Nothing(),
                                "Middleman call preface failed: %s\n",
                                redirection.mName);
      }
      return false;
    }
  }

  // Other phases will not run if we have not diverged from the recording.
  // Any outputs for the call have been handled by the SaveOutput hook.
  if (!aDiverged) {
    return true;
  }

  // Perform the ReplayInput phase on the new call and any others it depends on.
  InfallibleVector<MiddlemanCall*> outgoingCalls;
  if (!GatherDependentCalls(outgoingCalls, newCall)) {
    for (MiddlemanCall* call : outgoingCalls) {
      call->mSent = false;
    }
    return false;
  }

  // Encode all calls we are sending to the middleman.
  InfallibleVector<char> inputData;
  BufferStream inputStream(&inputData);
  for (MiddlemanCall* call : outgoingCalls) {
    call->EncodeInput(inputStream);
  }

  // Perform the calls synchronously in the middleman.
  InfallibleVector<char> outputData;
  child::SendMiddlemanCallRequest(inputData.begin(), inputData.length(),
                                  &outputData);

  // Decode outputs for the calls just sent, and perform the ReplayOutput phase
  // on any older dependent calls we sent.
  BufferStream outputStream(outputData.begin(), outputData.length());
  for (MiddlemanCall* call : outgoingCalls) {
    call->DecodeOutput(outputStream);

    if (call != newCall) {
      CallArguments oldArguments;
      call->mArguments.CopyTo(&oldArguments);
      MiddlemanCallContext cx(call, &oldArguments,
                              MiddlemanCallPhase::ReplayOutput);
      cx.mReplayOutputIsOld = true;
      GetRedirection(call->mCallId).mMiddlemanCall(cx);
    }
  }

  // Perform the ReplayOutput phase to fill in outputs for the current call.
  newCall->mArguments.CopyTo(aArguments);
  MiddlemanCallContext cx(newCall, aArguments,
                          MiddlemanCallPhase::ReplayOutput);
  redirection.mMiddlemanCall(cx);
  return true;
}

void ProcessMiddlemanCall(size_t aChildId, const char* aInputData,
                          size_t aInputSize,
                          InfallibleVector<char>* aOutputData) {
  MOZ_RELEASE_ASSERT(IsMiddleman());

  while (aChildId >= gStatePerChild.length()) {
    gStatePerChild.append(nullptr);
  }
  if (!gStatePerChild[aChildId]) {
    gStatePerChild[aChildId] = new MiddlemanCallState();
  }
  gState = gStatePerChild[aChildId];

  BufferStream inputStream(aInputData, aInputSize);
  BufferStream outputStream(aOutputData);

  while (!inputStream.IsEmpty()) {
    MiddlemanCall* call = new MiddlemanCall();
    call->DecodeInput(inputStream);

    const Redirection& redirection = GetRedirection(call->mCallId);
    MOZ_RELEASE_ASSERT(redirection.mMiddlemanCall);

    CallArguments arguments;
    call->mArguments.CopyTo(&arguments);

    bool skipCall;
    {
      MiddlemanCallContext cx(call, &arguments,
                              MiddlemanCallPhase::MiddlemanInput);
      redirection.mMiddlemanCall(cx);
      skipCall = cx.mSkipCallInMiddleman;
    }

    if (!skipCall) {
      RecordReplayInvokeCall(redirection.mBaseFunction, &arguments);
    }

    {
      MiddlemanCallContext cx(call, &arguments,
                              MiddlemanCallPhase::MiddlemanOutput);
      redirection.mMiddlemanCall(cx);
    }

    call->mArguments.CopyFrom(&arguments);
    call->EncodeOutput(outputStream);

    while (call->mId >= gState->mCalls.length()) {
      gState->mCalls.emplaceBack(nullptr);
    }
    MOZ_RELEASE_ASSERT(!gState->mCalls[call->mId]);
    gState->mCalls[call->mId] = call;
  }

  gState = nullptr;
}

void* MiddlemanCallContext::AllocateBytes(size_t aSize) {
  void* rv = malloc(aSize);

  // In a middleman process, any buffers we allocate live until the calls are
  // reset. In a replaying process, the buffers will either live forever
  // (if they are allocated in the ReplayPreface phase, to match the lifetime
  // of the MiddlemanCall itself) or will be recovered when we rewind after we
  // are done with our divergence from the recording (any other phase).
  if (IsMiddleman()) {
    gState->mAllocatedBuffers.append(rv);
  }

  return rv;
}

void ResetMiddlemanCalls(size_t aChildId) {
  MOZ_RELEASE_ASSERT(IsMiddleman());

  if (aChildId >= gStatePerChild.length()) {
    return;
  }

  gState = gStatePerChild[aChildId];
  if (!gState) {
    return;
  }

  for (MiddlemanCall* call : gState->mCalls) {
    if (call) {
      CallArguments arguments;
      call->mArguments.CopyTo(&arguments);

      MiddlemanCallContext cx(call, &arguments,
                              MiddlemanCallPhase::MiddlemanRelease);
      GetRedirection(call->mCallId).mMiddlemanCall(cx);
    }
  }

  // Delete the calls in a second pass. The MiddlemanRelease phase depends on
  // previous middleman calls still existing.
  for (MiddlemanCall* call : gState->mCalls) {
    delete call;
  }

  gState->mCalls.clear();
  for (auto buffer : gState->mAllocatedBuffers) {
    free(buffer);
  }
  gState->mAllocatedBuffers.clear();
  gState->mCallMap.clear();

  gState = nullptr;
}

///////////////////////////////////////////////////////////////////////////////
// System Values
///////////////////////////////////////////////////////////////////////////////

static void AddMiddlemanCallValue(const void* aThing, MiddlemanCall* aCall) {
  gState->mCallMap.erase(aThing);
  gState->mCallMap.insert(MiddlemanCallMap::value_type(aThing, aCall));
}

static MiddlemanCall* LookupMiddlemanCall(const void* aThing) {
  MiddlemanCallMap::const_iterator iter = gState->mCallMap.find(aThing);
  if (iter != gState->mCallMap.end()) {
    return iter->second;
  }
  return nullptr;
}

static const void* GetMiddlemanCallValue(size_t aId) {
  MOZ_RELEASE_ASSERT(IsMiddleman());
  MOZ_RELEASE_ASSERT(aId < gState->mCalls.length() && gState->mCalls[aId] &&
                     gState->mCalls[aId]->mMiddlemanValue.isSome());
  return gState->mCalls[aId]->mMiddlemanValue.ref();
}

bool MM_SystemInput(MiddlemanCallContext& aCx, const void** aThingPtr) {
  MOZ_RELEASE_ASSERT(aCx.AccessPreface());

  if (!*aThingPtr) {
    // Null values are handled by the normal argument copying logic.
    return true;
  }

  Maybe<size_t> callId;
  if (aCx.mPhase == MiddlemanCallPhase::ReplayPreface) {
    // Determine any middleman call this object came from, before the pointer
    // has a chance to be clobbered by another call between this and the
    // ReplayInput phase.
    MiddlemanCall* call = LookupMiddlemanCall(*aThingPtr);
    if (call) {
      callId.emplace(call->mId);
    }
  }
  aCx.ReadOrWritePrefaceBytes(&callId, sizeof(callId));

  switch (aCx.mPhase) {
    case MiddlemanCallPhase::ReplayPreface:
      return true;
    case MiddlemanCallPhase::ReplayInput:
      if (callId.isSome()) {
        aCx.WriteInputScalar(callId.ref());
        aCx.mDependentCalls->append(gState->mCalls[callId.ref()]);
        return true;
      }
      return false;
    case MiddlemanCallPhase::MiddlemanInput:
      if (callId.isSome()) {
        size_t callIndex = aCx.ReadInputScalar();
        *aThingPtr = GetMiddlemanCallValue(callIndex);
        return true;
      }
      return false;
    default:
      MOZ_CRASH("Bad phase");
  }
}

// Pointer system values are preserved during the replay so that null tests
// and equality tests work as expected. We additionally mangle the
// pointers here by setting one of the two highest bits, depending on whether
// the pointer came from the recording or from the middleman. This avoids
// accidentally conflating pointers that happen to have the same value but
// which originate from different processes.
static const void* MangleSystemValue(const void* aValue, bool aFromRecording) {
  return (const void*)((size_t)aValue | (1ULL << (aFromRecording ? 63 : 62)));
}

void MM_SystemOutput(MiddlemanCallContext& aCx, const void** aOutput,
                     bool aUpdating) {
  if (!*aOutput) {
    if (aCx.mPhase == MiddlemanCallPhase::MiddlemanOutput) {
      aCx.mCall->SetMiddlemanValue(*aOutput);
    }
    return;
  }

  switch (aCx.mPhase) {
    case MiddlemanCallPhase::ReplayPreface:
      if (!HasDivergedFromRecording()) {
        // If we haven't diverged from the recording, use the output value saved
        // in the recording.
        if (!aUpdating) {
          *aOutput = MangleSystemValue(*aOutput, true);
        }
        aCx.mCall->SetRecordingValue(*aOutput);
        AddMiddlemanCallValue(*aOutput, aCx.mCall);
      }
      break;
    case MiddlemanCallPhase::MiddlemanOutput:
      aCx.mCall->SetMiddlemanValue(*aOutput);
      AddMiddlemanCallValue(*aOutput, aCx.mCall);
      break;
    case MiddlemanCallPhase::ReplayOutput: {
      if (!aUpdating) {
        *aOutput = MangleSystemValue(*aOutput, false);
      }
      aCx.mCall->SetMiddlemanValue(*aOutput);

      // Associate the value produced by the middleman with this call. If the
      // call previously went through the ReplayPreface phase when we did not
      // diverge from the recording, we will associate values from both the
      // recording and middleman processes with this call. If a call made after
      // diverging produced the same value as a call made before diverging, use
      // the value saved in the recording for the first call, so that equality
      // tests on the value work as expected.
      MiddlemanCall* previousCall = LookupMiddlemanCall(*aOutput);
      if (previousCall) {
        if (previousCall->mRecordingValue.isSome()) {
          *aOutput = previousCall->mRecordingValue.ref();
        }
      } else {
        AddMiddlemanCallValue(*aOutput, aCx.mCall);
      }
      break;
    }
    default:
      return;
  }
}

///////////////////////////////////////////////////////////////////////////////
// MiddlemanCall
///////////////////////////////////////////////////////////////////////////////

void MiddlemanCall::EncodeInput(BufferStream& aStream) const {
  aStream.WriteScalar(mId);
  aStream.WriteScalar(mCallId);
  aStream.WriteBytes(&mArguments, sizeof(CallRegisterArguments));
  aStream.WriteScalar(mPreface.length());
  aStream.WriteBytes(mPreface.begin(), mPreface.length());
  aStream.WriteScalar(mInput.length());
  aStream.WriteBytes(mInput.begin(), mInput.length());
}

void MiddlemanCall::DecodeInput(BufferStream& aStream) {
  mId = aStream.ReadScalar();
  mCallId = aStream.ReadScalar();
  aStream.ReadBytes(&mArguments, sizeof(CallRegisterArguments));
  size_t prefaceLength = aStream.ReadScalar();
  mPreface.appendN(0, prefaceLength);
  aStream.ReadBytes(mPreface.begin(), prefaceLength);
  size_t inputLength = aStream.ReadScalar();
  mInput.appendN(0, inputLength);
  aStream.ReadBytes(mInput.begin(), inputLength);
}

void MiddlemanCall::EncodeOutput(BufferStream& aStream) const {
  aStream.WriteBytes(&mArguments, sizeof(CallRegisterArguments));
  aStream.WriteScalar(mOutput.length());
  aStream.WriteBytes(mOutput.begin(), mOutput.length());
}

void MiddlemanCall::DecodeOutput(BufferStream& aStream) {
  // Only update the return value when decoding arguments, so that we don't
  // clobber the call's arguments with any changes made in the middleman.
  CallRegisterArguments newArguments;
  aStream.ReadBytes(&newArguments, sizeof(CallRegisterArguments));
  mArguments.CopyRvalFrom(&newArguments);

  size_t outputLength = aStream.ReadScalar();
  mOutput.appendN(0, outputLength);
  aStream.ReadBytes(mOutput.begin(), outputLength);
}

}  // namespace recordreplay
}  // namespace mozilla