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
|
/* 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 https://mozilla.org/MPL/2.0/. */
#include <time.h>
#include <unistd.h>
#include <cstring>
#include "SandboxInfo.h"
#include "SandboxProfilerChild.h"
#include "SandboxProfiler.h"
#include "mozilla/Atomics.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/PodOperations.h"
namespace mozilla {
#if defined(DEBUG)
thread_local Atomic<bool> sInSignalContext = Atomic<bool>(false);
AutoForbidSignalContext::AutoForbidSignalContext() { sInSignalContext = true; }
AutoForbidSignalContext::~AutoForbidSignalContext() {
sInSignalContext = false;
}
#endif // defined(DEBUG)
static StaticAutoPtr<SandboxProfiler> gProfiler;
static StaticAutoPtr<SandboxProfilerQueue> gSyscallsQueue;
static StaticAutoPtr<SandboxProfilerQueue> gLogsQueue;
static Atomic<bool> isShutdown = Atomic<bool>(false);
// Those function pointers are set by the main thread, and subsequently read by
// all other thread, in particular in SIGSYS handlers.
struct UprofilerFuncPtrs uprofiler;
bool uprofiler_initted = false;
static bool const SANDBOX_PROFILER_DEBUG = false;
// Semaphores that we use to signal the SandboxProfilerEmitter thread when data
// has been pushed to the SandboxProfilerQueue
static sem_t gSyscallRequest;
static sem_t gLogsRequest;
// This is only be called on main thread, and not within SIGSYS context
//
// We might be called either from the profiler-started notification observer in
// which case the !Active() call is not useful, but also directly from Sandbox'
// SandboxLateInit where we want to verify if we are not already active: that
// can happen if the user started the profiler via MOZ_PROFILER_STARTUP=1
/* static */
void SandboxProfiler::Create() {
MOZ_ASSERT(!sInSignalContext,
"SandboxProfiler::Create called in SIGSYS handler");
if (!Init()) {
return;
}
if (!Active()) {
return;
}
if (!gSyscallsQueue) {
gSyscallsQueue = new SandboxProfilerQueue(15);
}
if (!gLogsQueue) {
gLogsQueue = new SandboxProfilerQueue(15);
}
if (!gProfiler) {
gProfiler = new SandboxProfiler();
}
}
SandboxProfiler::SandboxProfiler() {
mThreadLogs = std::thread(&SandboxProfiler::ThreadMain, this,
"SandboxProfilerEmitterLogs", gLogsQueue.get(),
&gLogsRequest);
mThreadSyscalls = std::thread(&SandboxProfiler::ThreadMain, this,
"SandboxProfilerEmitterSyscalls",
gSyscallsQueue.get(), &gSyscallRequest);
}
/* static */
void SandboxProfiler::Shutdown() {
isShutdown = true;
if (gProfiler) {
// Unblock remaining
SandboxProfiler::Signal(&gSyscallRequest);
SandboxProfiler::Signal(&gLogsRequest);
}
gProfiler = nullptr;
gSyscallsQueue = nullptr;
gLogsQueue = nullptr;
}
SandboxProfiler::~SandboxProfiler() {
if (mThreadLogs.joinable()) {
mThreadLogs.join();
}
if (mThreadSyscalls.joinable()) {
mThreadSyscalls.join();
}
}
/* static */
bool SandboxProfiler::ActiveWithQueue(SandboxProfilerQueue* aQueue) {
return !isShutdown && gProfiler && Active() && aQueue;
}
/* static */
void SandboxProfiler::Signal(sem_t* aSem) {
if (sem_post(aSem) < 0) {
if constexpr (SANDBOX_PROFILER_DEBUG) {
fprintf(stderr, "[%d] %s SEM_POST errno=%d\n", getpid(),
__PRETTY_FUNCTION__, errno);
}
}
}
/* static */
int SandboxProfiler::Wait(sem_t* aSem) { return sem_wait(aSem); }
/* static */
void SandboxProfiler::ReportInit(const void* top) {
if (!ActiveWithQueue(gSyscallsQueue)) {
return;
}
SandboxProfilerPayload payload = {
.mStack = NativeStack{.mCount = 0},
.mType = SandboxProfilerPayloadType::Init,
};
uprofiler.native_backtrace(top, &payload.mStack);
MOZ_ASSERT(gSyscallsQueue, "Queue is valid for Send() from ReportInit()");
if (!gSyscallsQueue) {
if constexpr (SANDBOX_PROFILER_DEBUG) {
fprintf(stderr,
"[%d] WARNING: Hello PRODUCER: gSyscallsQueue disappeared\n",
getpid());
}
return;
}
int rv = gSyscallsQueue->Send(payload);
if (rv == 0) {
if constexpr (SANDBOX_PROFILER_DEBUG) {
fprintf(stderr,
"[%d] WARNING: Hello PRODUCER: one stack mCount=%zu DROPPED\n",
getpid(), payload.mStack.mCount);
}
}
SandboxProfiler::Signal(&gSyscallRequest);
}
void SandboxProfiler::ReportInitImpl(SandboxProfilerPayload& payload,
ProfileChunkedBuffer& buffer) {
const char buf[] = "uprofiler init";
std::array arg_names = {"init"};
std::array arg_types = {
TRACE_VALUE_TYPE_STRING,
};
std::array arg_values = {reinterpret_cast<unsigned long long>(buf)};
Report("SandboxBroker::InitWithStack", arg_names, arg_types, arg_values,
&buffer);
}
/* static */
void SandboxProfiler::ReportLog(const char* aBuf) {
if (!ActiveWithQueue(gLogsQueue)) {
return;
}
if (!SandboxInfo::Get().Test(SandboxInfo::kVerbose) &&
!SandboxInfo::Get().Test(SandboxInfo::kVerboseTests)) {
return;
}
SandboxProfilerPayload payload = {
.mStack = NativeStack{.mCount = 0},
.mType = SandboxProfilerPayloadType::Log,
};
const size_t bufLen = strnlen(aBuf, PATH_MAX);
PodCopy(payload.mPath, aBuf, bufLen);
MOZ_ASSERT(gLogsQueue, "Queue is valid for Send() from ReportLog()");
if (!gLogsQueue) {
if constexpr (SANDBOX_PROFILER_DEBUG) {
fprintf(stderr, "[%d] WARNING: Hello PRODUCER: gLogsQueue disappeared\n",
getpid());
}
return;
}
int rv = gLogsQueue->Send(payload);
if (rv == 0) {
if constexpr (SANDBOX_PROFILER_DEBUG) {
fprintf(stderr, "[%d] WARNING: Hello PRODUCER: one log stack DROPPED\n",
getpid());
}
}
SandboxProfiler::Signal(&gLogsRequest);
}
void SandboxProfiler::ReportLogImpl(SandboxProfilerPayload& payload) {
std::array arg_names = {"log"};
std::array arg_types = {
TRACE_VALUE_TYPE_STRING,
};
std::array arg_values = {
reinterpret_cast<unsigned long long>(payload.mPath),
};
Report("SandboxBroker::Log", arg_names, arg_types, arg_values, nullptr);
}
/* static */
void SandboxProfiler::ReportRequest(const void* top, uint64_t aId,
const char* aOp, int aFlags,
const char* aPath, const char* aPath2,
pid_t aPid) {
if (!ActiveWithQueue(gSyscallsQueue)) {
return;
}
// Take a stack, this should be safe to do in the context of SIGSYS
SandboxProfilerPayload payload = {
.mStack = NativeStack{.mCount = 0},
.mId = aId,
.mOp = aOp,
.mFlags = aFlags,
.mPid = aPid,
.mType = SandboxProfilerPayloadType::Request,
};
if (aPath) {
const size_t pathLen = strnlen(aPath, PATH_MAX);
PodCopy(payload.mPath, aPath, pathLen);
} else {
payload.mPath[0] = '\0';
}
if (aPath2) {
const size_t path2Len = strnlen(aPath2, PATH_MAX);
PodCopy(payload.mPath2, aPath2, path2Len);
} else {
payload.mPath2[0] = '\0';
}
uprofiler.native_backtrace(top, &payload.mStack);
MOZ_ASSERT(gSyscallsQueue, "Queue is valid for Send() from ReportRequest()");
if (!gSyscallsQueue) {
if constexpr (SANDBOX_PROFILER_DEBUG) {
fprintf(stderr,
"[%d] WARNING: Hello PRODUCER: gSyscallsQueue disappeared\n",
getpid());
}
return;
}
int rv = gSyscallsQueue->Send(payload);
if (rv == 0) {
if constexpr (SANDBOX_PROFILER_DEBUG) {
fprintf(stderr,
"[%d] WARNING: Hello PRODUCER: one stack mCount=%zu DROPPED\n",
getpid(), payload.mStack.mCount);
}
}
SandboxProfiler::Signal(&gSyscallRequest);
}
void SandboxProfiler::ReportRequestImpl(SandboxProfilerPayload& payload,
ProfileChunkedBuffer& buffer) {
std::array arg_names = {"id", "op", "rflags", "path", "path2", "pid"};
std::array arg_types = {
TRACE_VALUE_TYPE_UINT, // id
TRACE_VALUE_TYPE_STRING, // op
TRACE_VALUE_TYPE_UINT, // rflags
TRACE_VALUE_TYPE_STRING, // path
TRACE_VALUE_TYPE_STRING, // path2
TRACE_VALUE_TYPE_UINT // pid
};
std::array arg_values = {static_cast<unsigned long long>(payload.mId),
reinterpret_cast<unsigned long long>(payload.mOp),
static_cast<unsigned long long>(payload.mFlags),
reinterpret_cast<unsigned long long>(payload.mPath),
reinterpret_cast<unsigned long long>(payload.mPath2),
static_cast<unsigned long long>(payload.mPid)};
Report("SandboxBrokerClient", arg_names, arg_types, arg_values, &buffer);
}
void SandboxProfiler::ThreadMain(const char* aThreadName,
SandboxProfilerQueue* aQueue,
sem_t* aRequest) {
uprofiler.register_thread(aThreadName, CallerPC());
SandboxProfilerPayload p;
DebugOnly<int> sem_init_rv =
sem_init(aRequest, /* pshared */ 0, /* value */ 0);
MOZ_ASSERT(sem_init_rv == 0, "Failure to initialize semaphore");
while (!isShutdown) {
errno = 0;
if (SandboxProfiler::Wait(aRequest) < 0) {
int _errno = errno;
MOZ_ASSERT(_errno != EINVAL, "sem_wait() returned EINVAL");
if (_errno == EAGAIN || _errno == EINTR) {
continue;
}
}
MOZ_ASSERT(aQueue, "Syscalls queue is valid for Recv()");
if (!aQueue) {
if constexpr (SANDBOX_PROFILER_DEBUG) {
fprintf(stderr,
"[%d] WARNING: Hello CONSUMER [%s]: aQueue disappeared\n",
getpid(), aThreadName);
}
continue;
}
int deq = aQueue->Recv(&p);
if (deq > 0) {
switch (p.mType) {
case SandboxProfilerPayloadType::Init:
case SandboxProfilerPayloadType::Request: {
ProfileBufferChunkManagerSingle chunkManager{
mozilla::ProfileBufferChunkManager::scExpectedMaximumStackSize};
ProfileChunkedBuffer chunkedBuffer{
ProfileChunkedBuffer::ThreadSafety::WithoutMutex, chunkManager};
uprofiler.backtrace_into_buffer(&p.mStack, &chunkedBuffer);
switch (p.mType) {
case SandboxProfilerPayloadType::Init:
ReportInitImpl(p, chunkedBuffer);
break;
case SandboxProfilerPayloadType::Request:
ReportRequestImpl(p, chunkedBuffer);
break;
default:
// impossible?
MOZ_ASSERT_UNREACHABLE("Should have been Init/Request");
break;
}
} break;
case SandboxProfilerPayloadType::Log:
ReportLogImpl(p);
break;
default:
fprintf(stderr, "[%d] mType=%hhu\n", getpid(),
static_cast<uint8_t>(p.mType));
MOZ_CRASH("Unsupported type");
break;
}
}
}
sem_destroy(aRequest);
uprofiler.unregister_thread();
}
} // namespace mozilla
|