diff options
Diffstat (limited to 'mozglue')
244 files changed, 82341 insertions, 0 deletions
diff --git a/mozglue/android/APKOpen.cpp b/mozglue/android/APKOpen.cpp new file mode 100644 index 0000000000..679ea694d7 --- /dev/null +++ b/mozglue/android/APKOpen.cpp @@ -0,0 +1,551 @@ +/* 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/. */ + +/* + * This custom library loading code is only meant to be called + * during initialization. As a result, it takes no special + * precautions to be threadsafe. Any of the library loading functions + * like mozload should not be available to other code. + */ + +#include <jni.h> +#include <android/log.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <sys/mman.h> +#include <sys/limits.h> +#include <errno.h> +#include <string.h> +#include <stdio.h> +#include <stdlib.h> +#include <fcntl.h> +#include <unistd.h> +#include <zlib.h> +#include "dlfcn.h" +#include "APKOpen.h" +#include <sys/time.h> +#include <sys/syscall.h> +#include <sys/resource.h> +#include <sys/prctl.h> +#include "sqlite3.h" +#include "Linker.h" +#include "BaseProfiler.h" +#include "application.ini.h" + +#include "mozilla/arm.h" +#include "mozilla/Bootstrap.h" +#include "mozilla/Printf.h" +#include "mozilla/Sprintf.h" +#include "mozilla/TimeStamp.h" +#include "mozilla/UniquePtr.h" +#include "XREChildData.h" + +/* Android headers don't define RUSAGE_THREAD */ +#ifndef RUSAGE_THREAD +# define RUSAGE_THREAD 1 +#endif + +#ifndef RELEASE_OR_BETA +/* Official builds have the debuggable flag set to false, which disables + * the backtrace dumper from bionic. However, as it is useful for native + * crashes happening before the crash reporter is registered, re-enable + * it on non release builds (i.e. nightly and aurora). + * Using a constructor so that it is re-enabled as soon as libmozglue.so + * is loaded. + */ +__attribute__((constructor)) void make_dumpable() { prctl(PR_SET_DUMPABLE, 1); } +#endif + +typedef int mozglueresult; + +using LoadGeckoLibsResult = + mozilla::Result<mozilla::Ok, mozilla::BootstrapError>; + +enum StartupEvent { +#define mozilla_StartupTimeline_Event(ev, z) ev, +#include "StartupTimeline.h" +#undef mozilla_StartupTimeline_Event + MAX_STARTUP_EVENT_ID +}; + +using namespace mozilla; + +void JNI_Throw(JNIEnv* jenv, const char* classname, const char* msg) { + __android_log_print(ANDROID_LOG_ERROR, "GeckoLibLoad", "Throw\n"); + jclass cls = jenv->FindClass(classname); + if (cls == nullptr) { + __android_log_print( + ANDROID_LOG_ERROR, "GeckoLibLoad", + "Couldn't find exception class (or exception pending) %s\n", classname); + exit(FAILURE); + } + int rc = jenv->ThrowNew(cls, msg); + if (rc < 0) { + __android_log_print(ANDROID_LOG_ERROR, "GeckoLibLoad", + "Error throwing exception %s\n", msg); + exit(FAILURE); + } + jenv->DeleteLocalRef(cls); +} + +namespace { +JavaVM* sJavaVM; +} + +void abortThroughJava(const char* msg) { + struct sigaction sigact = {}; + if (__wrap_sigaction(SIGSEGV, nullptr, &sigact)) { + return; // sigaction call failed. + } + + Dl_info info = {}; + if ((sigact.sa_flags & SA_SIGINFO) && + __wrap_dladdr(reinterpret_cast<void*>(sigact.sa_sigaction), &info) && + info.dli_fname && strstr(info.dli_fname, "libxul.so")) { + return; // Existing signal handler is in libxul (i.e. we have crash + // reporter). + } + + JNIEnv* env = nullptr; + if (!sJavaVM || + sJavaVM->AttachCurrentThreadAsDaemon(&env, nullptr) != JNI_OK) { + return; + } + + if (!env || env->PushLocalFrame(2) != JNI_OK) { + return; + } + + jclass loader = env->FindClass("org/mozilla/gecko/mozglue/GeckoLoader"); + if (!loader) { + return; + } + + jmethodID method = + env->GetStaticMethodID(loader, "abort", "(Ljava/lang/String;)V"); + jstring str = env->NewStringUTF(msg); + + if (method && str) { + env->CallStaticVoidMethod(loader, method, str); + } + + env->PopLocalFrame(nullptr); +} + +Bootstrap::UniquePtr gBootstrap; +#ifndef MOZ_FOLD_LIBS +static void* sqlite_handle = nullptr; +static void* nspr_handle = nullptr; +static void* plc_handle = nullptr; +#else +# define sqlite_handle nss_handle +# define nspr_handle nss_handle +# define plc_handle nss_handle +#endif +static void* nss_handle = nullptr; + +static UniquePtr<char[]> getUnpackedLibraryName(const char* libraryName) { + static const char* libdir = getenv("MOZ_ANDROID_LIBDIR"); + + size_t len = strlen(libdir) + 1 /* path separator */ + strlen(libraryName) + + 1; /* null terminator */ + auto file = MakeUnique<char[]>(len); + snprintf(file.get(), len, "%s/%s", libdir, libraryName); + return file; +} + +static void* dlopenLibrary(const char* libraryName) { + return __wrap_dlopen(getUnpackedLibraryName(libraryName).get(), + RTLD_GLOBAL | RTLD_LAZY); +} + +static void EnsureBaseProfilerInitialized() { + // There is no single entry-point into C++ code on Android. + // Instead, GeckoThread and GeckoLibLoader call various functions to load + // libraries one-by-one. + // We want to capture all that library loading in the profiler, so we need to + // kick off the base profiler at the beginning of whichever function is called + // first. + // We currently assume that all these functions are called on the same thread. + static bool sInitialized = false; + if (sInitialized) { + return; + } + + // The stack depth we observe here will be determined by the stack of + // whichever caller enters this code first. In practice this means that we may + // miss some root-most frames, which hopefully shouldn't ruin profiling. + int stackBase = 5; + mozilla::baseprofiler::profiler_init(&stackBase); + sInitialized = true; +} + +static LoadGeckoLibsResult loadGeckoLibs() { + TimeStamp t0 = TimeStamp::Now(); + struct rusage usage1_thread, usage1; + getrusage(RUSAGE_THREAD, &usage1_thread); + getrusage(RUSAGE_SELF, &usage1); + + static const char* libxul = getenv("MOZ_ANDROID_LIBDIR_OVERRIDE"); + MOZ_TRY_VAR( + gBootstrap, + GetBootstrap(libxul ? libxul : getUnpackedLibraryName("libxul.so").get(), + LibLoadingStrategy::ReadAhead)); + + TimeStamp t1 = TimeStamp::Now(); + struct rusage usage2_thread, usage2; + getrusage(RUSAGE_THREAD, &usage2_thread); + getrusage(RUSAGE_SELF, &usage2); + +#define RUSAGE_TIMEDIFF(u1, u2, field) \ + ((u2.ru_##field.tv_sec - u1.ru_##field.tv_sec) * 1000 + \ + (u2.ru_##field.tv_usec - u1.ru_##field.tv_usec) / 1000) + + __android_log_print(ANDROID_LOG_ERROR, "GeckoLibLoad", + "Loaded libs in %fms total, %ldms(%ldms) user, " + "%ldms(%ldms) system, %ld(%ld) faults", + (t1 - t0).ToMilliseconds(), + RUSAGE_TIMEDIFF(usage1_thread, usage2_thread, utime), + RUSAGE_TIMEDIFF(usage1, usage2, utime), + RUSAGE_TIMEDIFF(usage1_thread, usage2_thread, stime), + RUSAGE_TIMEDIFF(usage1, usage2, stime), + usage2_thread.ru_majflt - usage1_thread.ru_majflt, + usage2.ru_majflt - usage1.ru_majflt); + + gBootstrap->XRE_StartupTimelineRecord(LINKER_INITIALIZED, t0); + gBootstrap->XRE_StartupTimelineRecord(LIBRARIES_LOADED, t1); + return Ok(); +} + +static mozglueresult loadNSSLibs(); + +static mozglueresult loadSQLiteLibs() { + if (sqlite_handle) return SUCCESS; + +#ifdef MOZ_FOLD_LIBS + if (loadNSSLibs() != SUCCESS) return FAILURE; +#else + + sqlite_handle = dlopenLibrary("libmozsqlite3.so"); + if (!sqlite_handle) { + __android_log_print(ANDROID_LOG_ERROR, "GeckoLibLoad", + "Couldn't get a handle to libmozsqlite3!"); + return FAILURE; + } +#endif + + return SUCCESS; +} + +static mozglueresult loadNSSLibs() { + if (nss_handle && nspr_handle && plc_handle) return SUCCESS; + + nss_handle = dlopenLibrary("libnss3.so"); + +#ifndef MOZ_FOLD_LIBS + nspr_handle = dlopenLibrary("libnspr4.so"); + + plc_handle = dlopenLibrary("libplc4.so"); +#endif + + if (!nss_handle) { + __android_log_print(ANDROID_LOG_ERROR, "GeckoLibLoad", + "Couldn't get a handle to libnss3!"); + return FAILURE; + } + +#ifndef MOZ_FOLD_LIBS + if (!nspr_handle) { + __android_log_print(ANDROID_LOG_ERROR, "GeckoLibLoad", + "Couldn't get a handle to libnspr4!"); + return FAILURE; + } + + if (!plc_handle) { + __android_log_print(ANDROID_LOG_ERROR, "GeckoLibLoad", + "Couldn't get a handle to libplc4!"); + return FAILURE; + } +#endif + + return SUCCESS; +} + +extern "C" APKOPEN_EXPORT void MOZ_JNICALL +Java_org_mozilla_gecko_mozglue_GeckoLoader_loadGeckoLibsNative( + JNIEnv* jenv, jclass jGeckoAppShellClass) { + EnsureBaseProfilerInitialized(); + + jenv->GetJavaVM(&sJavaVM); + + LoadGeckoLibsResult res = loadGeckoLibs(); + if (res.isOk()) { + return; + } + + const BootstrapError& errorInfo = res.inspectErr(); + + auto msg = errorInfo.match( + [](const nsresult& aRv) { + return Smprintf("Error loading Gecko libraries: nsresult 0x%08X", aRv); + }, + [](const DLErrorType& aErr) { + return Smprintf("Error loading Gecko libraries: %s", aErr.get()); + }); + + JNI_Throw(jenv, "java/lang/Exception", msg.get()); +} + +extern "C" APKOPEN_EXPORT void MOZ_JNICALL +Java_org_mozilla_gecko_mozglue_GeckoLoader_loadSQLiteLibsNative( + JNIEnv* jenv, jclass jGeckoAppShellClass) { + EnsureBaseProfilerInitialized(); + + __android_log_print(ANDROID_LOG_ERROR, "GeckoLibLoad", "Load sqlite start\n"); + mozglueresult rv = loadSQLiteLibs(); + if (rv != SUCCESS) { + JNI_Throw(jenv, "java/lang/Exception", "Error loading sqlite libraries"); + } + __android_log_print(ANDROID_LOG_ERROR, "GeckoLibLoad", "Load sqlite done\n"); +} + +extern "C" APKOPEN_EXPORT void MOZ_JNICALL +Java_org_mozilla_gecko_mozglue_GeckoLoader_loadNSSLibsNative( + JNIEnv* jenv, jclass jGeckoAppShellClass) { + EnsureBaseProfilerInitialized(); + + __android_log_print(ANDROID_LOG_ERROR, "GeckoLibLoad", "Load nss start\n"); + mozglueresult rv = loadNSSLibs(); + if (rv != SUCCESS) { + JNI_Throw(jenv, "java/lang/Exception", "Error loading nss libraries"); + } + __android_log_print(ANDROID_LOG_ERROR, "GeckoLibLoad", "Load nss done\n"); +} + +static char** CreateArgvFromObjectArray(JNIEnv* jenv, jobjectArray jargs, + int* length) { + size_t stringCount = jenv->GetArrayLength(jargs); + + if (length) { + *length = stringCount; + } + + if (!stringCount) { + return nullptr; + } + + char** argv = new char*[stringCount + 1]; + + argv[stringCount] = nullptr; + + for (size_t ix = 0; ix < stringCount; ix++) { + jstring string = (jstring)(jenv->GetObjectArrayElement(jargs, ix)); + const char* rawString = jenv->GetStringUTFChars(string, nullptr); + const int strLength = jenv->GetStringUTFLength(string); + argv[ix] = strndup(rawString, strLength); + jenv->ReleaseStringUTFChars(string, rawString); + jenv->DeleteLocalRef(string); + } + + return argv; +} + +static void FreeArgv(char** argv, int argc) { + for (int ix = 0; ix < argc; ix++) { + // String was allocated with strndup, so need to use free to deallocate. + free(argv[ix]); + } + delete[] (argv); +} + +extern "C" APKOPEN_EXPORT void MOZ_JNICALL +Java_org_mozilla_gecko_mozglue_GeckoLoader_nativeRun( + JNIEnv* jenv, jclass jc, jobjectArray jargs, int prefsFd, int prefMapFd, + int ipcFd, int crashFd, int crashAnnotationFd, bool xpcshell, + jstring outFilePath) { + EnsureBaseProfilerInitialized(); + + int argc = 0; + char** argv = CreateArgvFromObjectArray(jenv, jargs, &argc); + + if (ipcFd < 0) { + if (gBootstrap == nullptr) { + FreeArgv(argv, argc); + return; + } + +#ifdef MOZ_LINKER + ElfLoader::Singleton.ExpectShutdown(false); +#endif + const char* outFilePathRaw = nullptr; + if (xpcshell) { + MOZ_ASSERT(outFilePath); + outFilePathRaw = jenv->GetStringUTFChars(outFilePath, nullptr); + } + gBootstrap->GeckoStart(jenv, argv, argc, sAppData, xpcshell, + outFilePathRaw); + if (outFilePathRaw) { + jenv->ReleaseStringUTFChars(outFilePath, outFilePathRaw); + } +#ifdef MOZ_LINKER + ElfLoader::Singleton.ExpectShutdown(true); +#endif + } else { + gBootstrap->XRE_SetAndroidChildFds( + jenv, {prefsFd, prefMapFd, ipcFd, crashFd, crashAnnotationFd}); + gBootstrap->XRE_SetProcessType(argv[argc - 1]); + + XREChildData childData; + gBootstrap->XRE_InitChildProcess(argc - 1, argv, &childData); + } + +#ifdef MOZ_WIDGET_ANDROID +# ifdef MOZ_PROFILE_GENERATE + gBootstrap->XRE_WriteLLVMProfData(); +# endif +#endif + gBootstrap.reset(); + FreeArgv(argv, argc); +} + +extern "C" APKOPEN_EXPORT mozglueresult ChildProcessInit(int argc, + char* argv[]) { + EnsureBaseProfilerInitialized(); + + if (loadNSSLibs() != SUCCESS) { + return FAILURE; + } + if (loadSQLiteLibs() != SUCCESS) { + return FAILURE; + } + if (loadGeckoLibs().isErr()) { + return FAILURE; + } + + gBootstrap->XRE_SetProcessType(argv[--argc]); + + XREChildData childData; + return NS_FAILED(gBootstrap->XRE_InitChildProcess(argc, argv, &childData)); +} + +// Does current process name end with ':media'? +static bool IsMediaProcess() { + pid_t pid = getpid(); + char str[256]; + SprintfLiteral(str, "/proc/%d/cmdline", pid); + FILE* f = fopen(str, "r"); + if (f) { + fgets(str, sizeof(str), f); + fclose(f); + const size_t strLen = strlen(str); + const char suffix[] = ":media"; + const size_t suffixLen = sizeof(suffix) - 1; + if (strLen >= suffixLen && + !strncmp(str + strLen - suffixLen, suffix, suffixLen)) { + return true; + } + } + return false; +} + +#ifndef SYS_rt_tgsigqueueinfo +# define SYS_rt_tgsigqueueinfo __NR_rt_tgsigqueueinfo +#endif +/* Copy of http://androidxref.com/7.1.1_r6/xref/bionic/linker/debugger.cpp#262, + * with debuggerd related code stripped. + * + * Copyright (C) 2008 The Android Open Source Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +static void CatchFatalSignals(int num, siginfo_t* info, void* context) { + // It's possible somebody cleared the SA_SIGINFO flag, which would mean + // our "info" arg holds an undefined value. + struct sigaction action = {}; + if ((sigaction(num, nullptr, &action) < 0) || + !(action.sa_flags & SA_SIGINFO)) { + info = nullptr; + } + + // We need to return from the signal handler so that debuggerd can dump the + // thread that crashed, but returning here does not guarantee that the signal + // will be thrown again, even for SIGSEGV and friends, since the signal could + // have been sent manually. Resend the signal with rt_tgsigqueueinfo(2) to + // preserve the SA_SIGINFO contents. + signal(num, SIG_DFL); + + struct siginfo si; + if (!info) { + memset(&si, 0, sizeof(si)); + si.si_code = SI_USER; + si.si_pid = getpid(); + si.si_uid = getuid(); + info = &si; + } else if (info->si_code >= 0 || info->si_code == SI_TKILL) { + // rt_tgsigqueueinfo(2)'s documentation appears to be incorrect on kernels + // that contain commit 66dd34a (3.9+). The manpage claims to only allow + // negative si_code values that are not SI_TKILL, but 66dd34a changed the + // check to allow all si_code values in calls coming from inside the house. + } + + int rc = syscall(SYS_rt_tgsigqueueinfo, getpid(), gettid(), num, info); + if (rc != 0) { + __android_log_print(ANDROID_LOG_FATAL, "mozglue", + "failed to resend signal during crash: %s", + strerror(errno)); + _exit(0); + } +} + +extern "C" APKOPEN_EXPORT void MOZ_JNICALL +Java_org_mozilla_gecko_mozglue_GeckoLoader_suppressCrashDialog(JNIEnv* jenv, + jclass jc) { + MOZ_RELEASE_ASSERT(IsMediaProcess(), + "Suppress crash dialog only for media process"); + // Restoring to SIG_DFL will crash on x86/Android M devices (see bug 1374556) + // so copy Android code + // (http://androidxref.com/7.1.1_r6/xref/bionic/linker/debugger.cpp#302). See + // comments above CatchFatalSignals() for copyright notice. + struct sigaction action; + memset(&action, 0, sizeof(action)); + sigemptyset(&action.sa_mask); + action.sa_sigaction = &CatchFatalSignals; + action.sa_flags = SA_RESTART | SA_SIGINFO; + + // Use the alternate signal stack if available so we can catch stack + // overflows. + action.sa_flags |= SA_ONSTACK; + + sigaction(SIGABRT, &action, nullptr); + sigaction(SIGBUS, &action, nullptr); + sigaction(SIGFPE, &action, nullptr); + sigaction(SIGILL, &action, nullptr); + sigaction(SIGSEGV, &action, nullptr); +#if defined(SIGSTKFLT) + sigaction(SIGSTKFLT, &action, nullptr); +#endif + sigaction(SIGTRAP, &action, nullptr); +} diff --git a/mozglue/android/APKOpen.h b/mozglue/android/APKOpen.h new file mode 100644 index 0000000000..f3666bf028 --- /dev/null +++ b/mozglue/android/APKOpen.h @@ -0,0 +1,29 @@ +/* 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/. */ + +#ifndef APKOpen_h +#define APKOpen_h + +#include <jni.h> + +#ifndef APKOPEN_EXPORT +# define APKOPEN_EXPORT __attribute__((visibility("default"))) +#endif + +APKOPEN_EXPORT void abortThroughJava(const char* msg); + +static const int SUCCESS = 0; +static const int FAILURE = 1; +void JNI_Throw(JNIEnv* jenv, const char* classname, const char* msg); + +// Bug 1207642 - Work around Dalvik bug by realigning stack on JNI entry +#ifndef MOZ_JNICALL +# ifdef __i386__ +# define MOZ_JNICALL JNICALL __attribute__((force_align_arg_pointer)) +# else +# define MOZ_JNICALL JNICALL +# endif +#endif + +#endif /* APKOpen_h */ diff --git a/mozglue/android/Ashmem.cpp b/mozglue/android/Ashmem.cpp new file mode 100644 index 0000000000..edfeb495dd --- /dev/null +++ b/mozglue/android/Ashmem.cpp @@ -0,0 +1,73 @@ +/* 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 <cstring> +#include <dlfcn.h> +#include <fcntl.h> +#include <linux/ashmem.h> +#include <stdio.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <sys/ioctl.h> +#include <unistd.h> + +#include "Ashmem.h" + +namespace mozilla { +namespace android { + +static void* libhandle() { + static void* handle = dlopen("libandroid.so", RTLD_LAZY | RTLD_LOCAL); + return handle; +} + +int ashmem_create(const char* name, size_t size) { + static auto fCreate = + (int (*)(const char*, size_t))dlsym(libhandle(), "ASharedMemory_create"); + + if (fCreate) { + return fCreate(name, size); + } + + int fd = open("/" ASHMEM_NAME_DEF, O_RDWR); + if (fd < 0) { + return fd; + } + + if (name) { + char str[ASHMEM_NAME_LEN]; + strlcpy(str, name, sizeof(str)); + ioctl(fd, ASHMEM_SET_NAME, str); + } + + if (ioctl(fd, ASHMEM_SET_SIZE, size) != 0) { + close(fd); + return -1; + } + + return fd; +} + +size_t ashmem_getSize(int fd) { + static auto fGetSize = + (size_t(*)(int))dlsym(libhandle(), "ASharedMemory_getSize"); + if (fGetSize) { + return fGetSize(fd); + } + + return (size_t)ioctl(fd, ASHMEM_GET_SIZE, nullptr); +} + +int ashmem_setProt(int fd, int prot) { + static auto fSetProt = + (int (*)(int, int))dlsym(libhandle(), "ASharedMemory_setProt"); + if (fSetProt) { + return fSetProt(fd, prot); + } + + return ioctl(fd, ASHMEM_SET_PROT_MASK, prot); +} + +} // namespace android +} // namespace mozilla diff --git a/mozglue/android/Ashmem.h b/mozglue/android/Ashmem.h new file mode 100644 index 0000000000..3307761e32 --- /dev/null +++ b/mozglue/android/Ashmem.h @@ -0,0 +1,23 @@ +/* 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/. */ + +#ifndef Ashmem_h__ +#define Ashmem_h__ + +#include <linux/ashmem.h> +#include "mozilla/Types.h" + +namespace mozilla { +namespace android { + +// Wrappers for the ASharedMemory function in the NDK +// https://developer.android.com/ndk/reference/group/memory +MFBT_API int ashmem_create(const char* name, size_t size); +MFBT_API size_t ashmem_getSize(int fd); +MFBT_API int ashmem_setProt(int fd, int prot); + +} // namespace android +} // namespace mozilla + +#endif // Ashmem_h__ diff --git a/mozglue/android/SharedMemNatives.cpp b/mozglue/android/SharedMemNatives.cpp new file mode 100644 index 0000000000..f38a2274b3 --- /dev/null +++ b/mozglue/android/SharedMemNatives.cpp @@ -0,0 +1,80 @@ +/* -*- Mode: c++; c-basic-offset: 2; tab-width: 20; indent-tabs-mode: nil; -*- + * 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 <errno.h> +#include <jni.h> +#include <stdio.h> +#include <string.h> +#include <sys/mman.h> + +#include "mozilla/Sprintf.h" + +extern "C" { + +JNIEXPORT +void JNICALL +Java_org_mozilla_gecko_media_SampleBuffer_nativeReadFromDirectBuffer( + JNIEnv* jenv, jclass, jobject src, jlong dest, jint offset, jint size) { + uint8_t* from = static_cast<uint8_t*>(jenv->GetDirectBufferAddress(src)); + if (from == nullptr) { + jenv->ThrowNew(jenv->FindClass("java/lang/NullPointerException"), + "Null direct buffer"); + return; + } + + void* to = reinterpret_cast<void*>(dest); + if (to == nullptr) { + jenv->ThrowNew(jenv->FindClass("java/lang/NullPointerException"), + "Null shared memory buffer"); + return; + } + + memcpy(to, from + offset, size); +} + +JNIEXPORT +void JNICALL +Java_org_mozilla_gecko_media_SampleBuffer_nativeWriteToDirectBuffer( + JNIEnv* jenv, jclass, jlong src, jobject dest, jint offset, jint size) { + uint8_t* from = reinterpret_cast<uint8_t*>(src); + if (from == nullptr) { + jenv->ThrowNew(jenv->FindClass("java/lang/NullPointerException"), + "Null shared memory buffer"); + return; + } + + void* to = jenv->GetDirectBufferAddress(dest); + if (to == nullptr) { + jenv->ThrowNew(jenv->FindClass("java/lang/NullPointerException"), + "Null direct buffer"); + return; + } + + memcpy(to, from + offset, size); +} + +JNIEXPORT +jlong JNICALL Java_org_mozilla_gecko_mozglue_SharedMemory_map(JNIEnv* env, + jobject jobj, + jint fd, + jint length) { + void* address = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (address == MAP_FAILED) { + char msg[128]; + SprintfLiteral(msg, "mmap failed. errno=%d", errno); + env->ThrowNew(env->FindClass("java/lang/NullPointerException"), msg); + return 0; + } + return jlong(address); +} + +JNIEXPORT +void JNICALL Java_org_mozilla_gecko_mozglue_SharedMemory_unmap(JNIEnv* env, + jobject jobj, + jlong address, + jint size) { + munmap((void*)address, (size_t)size); +} +} diff --git a/mozglue/android/moz.build b/mozglue/android/moz.build new file mode 100644 index 0000000000..bf5813d963 --- /dev/null +++ b/mozglue/android/moz.build @@ -0,0 +1,44 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# 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/. + +EXPORTS += [ + "APKOpen.h", +] + +EXPORTS.mozilla += ["Ashmem.h"] + +SOURCES += [ + "APKOpen.cpp", + "Ashmem.cpp", + "nsGeckoUtils.cpp", + "SharedMemNatives.cpp", +] + +FINAL_LIBRARY = "mozglue" + +for var in ("ANDROID_PACKAGE_NAME", "ANDROID_CPU_ARCH"): + DEFINES[var] = '"%s"' % CONFIG[var] + +if CONFIG["MOZ_FOLD_LIBS"]: + DEFINES["MOZ_FOLD_LIBS"] = True + +LOCAL_INCLUDES += [ + "!/build", + "../linker", + "/ipc/chromium/src", + "/nsprpub/lib/ds", + "/nsprpub/lib/libc/include", + "/nsprpub/pr/include", + "/third_party/sqlite3/src", + "/toolkit/components/startup", + "/xpcom/build", +] + +DEFINES["XPCOM_GLUE"] = True + +USE_LIBS += [ + "xpcomglue", +] diff --git a/mozglue/android/nsGeckoUtils.cpp b/mozglue/android/nsGeckoUtils.cpp new file mode 100644 index 0000000000..017353e548 --- /dev/null +++ b/mozglue/android/nsGeckoUtils.cpp @@ -0,0 +1,24 @@ +/* -*- Mode: c++; c-basic-offset: 2; tab-width: 20; indent-tabs-mode: nil; -*- + * 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 <jni.h> + +#include <stdlib.h> +#include <fcntl.h> +#include "APKOpen.h" +#include "Zip.h" +#include "mozilla/RefPtr.h" + +extern "C" __attribute__((visibility("default"))) void MOZ_JNICALL +Java_org_mozilla_gecko_mozglue_GeckoLoader_putenv(JNIEnv* jenv, jclass, + jstring map) { + const char* str; + // XXX: java doesn't give us true UTF8, we should figure out something + // better to do here + str = jenv->GetStringUTFChars(map, nullptr); + if (str == nullptr) return; + putenv(strdup(str)); + jenv->ReleaseStringUTFChars(map, str); +} diff --git a/mozglue/baseprofiler/build/generate_profiling_categories.py b/mozglue/baseprofiler/build/generate_profiling_categories.py new file mode 100644 index 0000000000..64da056bbd --- /dev/null +++ b/mozglue/baseprofiler/build/generate_profiling_categories.py @@ -0,0 +1,350 @@ +# 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/. + +# This script generates ProfilingCategoryList.h and profiling_categories.rs +# files from profiling_categories.yaml. + +from collections import OrderedDict + +import yaml + +CPP_HEADER_TEMPLATE = """\ +/* 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/. */ + +#ifndef {includeguard} +#define {includeguard} + +/* This file is generated by generate_profiling_categories.py from + profiling_categories.yaml. DO NOT EDIT! */ + +// Profiler sub-categories are applied to each sampled stack to describe the +// type of workload that the CPU is busy with. Only one sub-category can be +// assigned so be mindful that these are non-overlapping. The active category is +// set by pushing a label to the profiling stack, or by the unwinder in cases +// such as JITs. A profile sample in arbitrary C++/Rust will typically be +// categorized based on the top of the label stack. +// +// The list of available color names for categories is: +// transparent +// blue +// green +// grey +// lightblue +// magenta +// orange +// purple +// yellow + +// clang-format off + +{contents} + +// clang-format on + +#endif // {includeguard} +""" + +CPP_MACRO_DEFINITION = """\ +#define MOZ_PROFILING_CATEGORY_LIST(BEGIN_CATEGORY, SUBCATEGORY, END_CATEGORY) \\ +""" + +RUST_TEMPLATE = """\ +/* 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/. */ + +/* This file is generated by generate_profiling_categories.py from + profiling_categories.yaml. DO NOT EDIT! */ + +{contents}\ +""" + +RUST_ENUM_TEMPLATE = """\ +#[repr(u32)] +#[derive(Debug, Copy, Clone)] +pub enum {name} {{ +{fields} +}} +""" + +RUST_CONVERSION_IMPL_TEMPLATE = """\ +impl {name} {{ + pub fn to_cpp_enum_value(&self) -> u32 {{ +{content} + }} +}} +""" + +RUST_DEFAULT_IMPL_TEMPLATE = """\ +impl Default for {name} {{ + fn default() -> Self {{ +{content} + }} +}} +""" + +RUST_MATCH_SELF = """\ + match *self {{ +{fields} + }} +""" + + +def generate_header(c_out, includeguard, contents): + c_out.write( + CPP_HEADER_TEMPLATE.format(includeguard=includeguard, contents=contents) + ) + + +def generate_rust_file(c_out, contents): + c_out.write(RUST_TEMPLATE.format(contents=contents)) + + +def load_yaml(yaml_path): + # Load into an OrderedDict to ensure order is preserved. Note: Python 3.7+ + # also preserves ordering for normal dictionaries. + # Code based on https://stackoverflow.com/a/21912744. + class OrderedLoader(yaml.Loader): + pass + + def construct_mapping(loader, node): + loader.flatten_mapping(node) + return OrderedDict(loader.construct_pairs(node)) + + tag = yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG + OrderedLoader.add_constructor(tag, construct_mapping) + + file_handler = open(yaml_path) + return yaml.load(file_handler, OrderedLoader) + + +def generate_category_macro(name, label, color, subcategories): + contents = ' BEGIN_CATEGORY({name}, "{label}", "{color}") \\\n'.format( + name=name, label=label, color=color + ) + + subcategory_items = [] + + for subcategory in subcategories: + subcat_name = subcategory["name"] + assert isinstance(subcat_name, str) + subcat_label = subcategory["label"] + assert isinstance(subcat_label, str) + + subcategory_items.append( + ' SUBCATEGORY({parent_cat}, {name}, "{label}") \\\n'.format( + parent_cat=name, name=subcat_name, label=subcat_label + ) + ) + + contents += "".join(subcategory_items) + contents += " END_CATEGORY" + + return contents + + +def generate_macro_header(c_out, yaml_path): + """Generate ProfilingCategoryList.h from profiling_categories.yaml. + The generated file has a macro to generate the profiling category enums. + """ + + data = load_yaml(yaml_path) + + # Stores the macro definition of each categories. + category_items = [] + + for category in data: + name = category["name"] + assert isinstance(name, str) + label = category["label"] + assert isinstance(label, str) + color = category["color"] + assert isinstance(color, str) + subcategories = category.get("subcategories", None) + assert ( + isinstance(subcategories, list) and len(subcategories) > 0 + ), "At least one subcategory expected as default in {}.".format(name) + + category_items.append( + generate_category_macro(name, label, color, subcategories) + ) + + contents = CPP_MACRO_DEFINITION + contents += " \\\n".join(category_items) + + generate_header(c_out, "baseprofiler_ProfilingCategoryList_h", contents) + + +class RustEnum: + """Class that keeps the rust enum fields and impls. + This is used for generating the Rust ProfilingCategoryPair and ProfilingCategory + enums as well as ProfilingCategoryPair's sub category enums. + For example, this can either generate an enum with discrimant fields for sub + category enums and ProfilingCategory: + ``` + #[repr(u32)] + #[derive(Debug, Copy, Clone)] + pub enum Graphics { + LayerBuilding = 0, + ... + } + ``` + or can generate an enum with optional tuple values for ProfilingCategoryPair + to explicitly mention their sub categories: + ``` + #[repr(u32)] + #[derive(Debug, Copy, Clone)] + pub enum ProfilingCategoryPair { + Network(Option<Network>), + ... + } + ``` + + And in addition to enums, it will generate impls for each enum. See one + example below: + ``` + impl Default for Network { + fn default() -> Self { + Network::Other + } + } + ``` + """ + + def __init__(self, name): + # Name of the Rust enum. + self.name = name + # Fields of the Rust enum. This list contains elements of + # (field_name, field_string) tuple for convenience. + self.fields = [] + # Impls of the Rust enum. Each element is a string. + self.impls = [] + # Default category of the Rust enum. Main enums won't have it, but all + # sub category enums must have one. This is being checked later. + self.default_category = None + + def append_optional_tuple_field(self, field_name): + """Append the enum fields list with an optional tuple field.""" + field = (field_name, " {name}(Option<{name}>),".format(name=field_name)) + self.fields.append(field) + + def append_discriminant_field(self, field_name, field_value): + """Append the enum fields list with a discriminant field.""" + field = ( + field_name, + " {name} = {value},".format(name=field_name, value=field_value), + ) + self.fields.append(field) + + def append_default_impl(self, default_category): + """Append the enum impls list with a default implementation.""" + self.default_category = default_category + + self.impls.append( + RUST_DEFAULT_IMPL_TEMPLATE.format( + name=self.name, + content=" {category}::{subcategory}".format( + category=self.name, subcategory=self.default_category + ), + ) + ) + + def append_conversion_impl(self, content): + """Append the enum impls list with a conversion implementation for cpp values.""" + self.impls.append( + RUST_CONVERSION_IMPL_TEMPLATE.format(name=self.name, content=content) + ) + + def to_rust_string(self): + """Serialize the enum with its impls as a string""" + joined_fields = "\n".join(map(lambda field: field[1], self.fields)) + result = RUST_ENUM_TEMPLATE.format(name=self.name, fields=joined_fields) + result += "\n" + result += "\n".join(self.impls) + return result + + +def generate_rust_enums(c_out, yaml_path): + """Generate profiling_categories.rs from profiling_categories.yaml. + The generated file has a profiling category enums and their impls. + """ + + data = load_yaml(yaml_path) + + # Each category has its own enum for keeping its subcategories. We are + # keeping all of them here. + enums = [] + # Parent enums for prifiling category and profiling category pair. They will + # be appended to the end of the `enums`. + profiling_category_pair_enum = RustEnum("ProfilingCategoryPair") + profiling_category_enum = RustEnum("ProfilingCategory") + profiling_category_pair_value = 0 + + for cat_index, category in enumerate(data): + cat_name = category["name"] + assert isinstance(cat_name, str) + cat_label = category["label"] + assert isinstance(cat_label, str) + # This will be used as our main enum field and sub category enum. + cat_label = "".join(filter(str.isalnum, cat_label)) + cat_subcategories = category.get("subcategories", None) + assert ( + isinstance(cat_subcategories, list) and len(cat_subcategories) > 0 + ), "At least one subcategory expected as default in {}.".format(cat_name) + + # Create a new enum for this sub category and append it to the enums list. + category_enum = RustEnum(cat_label) + enums.append(category_enum) + + for subcategory in cat_subcategories: + subcat_name = subcategory["name"] + assert isinstance(subcat_name, str) + subcat_label = subcategory["label"] + assert isinstance(subcat_label, str) + friendly_subcat_name = None + + if cat_name == subcat_name: + # This is the default sub-category. It should use the label as name. + friendly_subcat_name = subcat_label + category_enum.append_default_impl(subcat_label) + else: + # This is a non-default sub-category. + underscore_pos = subcat_name.find("_") + friendly_subcat_name = subcat_name[underscore_pos + 1 :] + + friendly_subcat_name = "".join(filter(str.isalnum, friendly_subcat_name)) + category_enum.append_discriminant_field( + friendly_subcat_name, profiling_category_pair_value + ) + profiling_category_pair_value += 1 + + assert ( + category_enum.default_category is not None + ), "There must be a default subcategory with the same name." + + # Append the main enums. + profiling_category_pair_enum.append_optional_tuple_field(cat_label) + profiling_category_enum.append_discriminant_field(cat_label, cat_index) + + # Add the main enums impls for conversion into cpp values. + profiling_category_pair_impl_fields = "\n".join( + " {enum_name}::{field_name}(val) => val.unwrap_or_default() as u32,".format( + enum_name="ProfilingCategoryPair", field_name=field + ) + for field, _ in profiling_category_pair_enum.fields + ) + profiling_category_pair_enum.append_conversion_impl( + RUST_MATCH_SELF.format(fields=profiling_category_pair_impl_fields) + ) + profiling_category_enum.append_conversion_impl(" *self as u32") + + # After adding all the sub category enums, we can add the main enums to the list. + enums.append(profiling_category_pair_enum) + enums.append(profiling_category_enum) + + # Print all the enums and their impls. + contents = "\n".join(map(lambda enum: enum.to_rust_string(), enums)) + generate_rust_file(c_out, contents) diff --git a/mozglue/baseprofiler/build/profiling_categories.yaml b/mozglue/baseprofiler/build/profiling_categories.yaml new file mode 100644 index 0000000000..62fd5d0617 --- /dev/null +++ b/mozglue/baseprofiler/build/profiling_categories.yaml @@ -0,0 +1,301 @@ +# 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/. + +# Profiling categories +# ==================== +# This file defines all profiling categories with their sub-categories. It is +# parsed by generate_profiling_categories.py at build time to create +# ProfilingCategoryList.h and profiling_categories.rs files. +# +# Profiling sub-categories are applied to each sampled stack to describe the +# type of workload that the CPU is busy with. Only one sub-category can be +# assigned so be mindful that these are non-overlapping. The active category is +# set by pushing a label to the profiling stack, or by the unwinder in cases +# such as JITs. A profile sample in arbitrary C++/Rust will typically be +# categorized based on the top of the label stack. +# +# Each category consists of a name and a set of attributes that are described below: +# +# name [required] +# ==== +# Name of the profiling category. This will be used in the C++ enum fields (not +# by Rust). +# +# label [required] +# ===== +# Label of the profiling category. This a more human readable string for the +# category. Label will be displayed in the Firefox Profiler front-end. But also +# this will be used as a Rust enum field (with non-alphanumeric characters +# removed) because it's more idiomatic for Rust enums than name fields (which +# are snake cased fields with all caps, which is not idiomatic to rust enum +# field). +# +# color [required] +# ===== +# Color that this category will show up as in the Firefox Profiler front-end. +# The list of available color names for categories is: +# - transparent +# - blue +# - green +# - grey +# - lightblue +# - magenta +# - orange +# - purple +# - yellow +# +# subcategories [required] +# ============= +# A list of sub-categories that belong to this category. +# There must be at least one sub-category for each category and there must be at +# least one category with the same name as the category to indicate the default +# sub-category. Each sub-category must have name and label attributes. +# +# name attribute should either be the same as the category (for default +# sub-category) or should start with parent category name + underscore +# (e.g. JS_Parsing). +# +# label attribute has the same purpose as parent category label attribute. +# +# For example: +# - name: JS +# subcategories: +# - name: JS +# label: Other +# - name: JS_Parsing +# label: Parsing +# +# Note that the first sub-category has the same name with the category. This is +# the default sub-category. Also note the other sub-categories starting with the +# category name + underscore. +# + +- name: IDLE + label: Idle + color: transparent + subcategories: + - name: IDLE + label: Other + +- name: OTHER + label: Other + color: grey + subcategories: + - name: OTHER + label: Other + - name: OTHER_PreferenceRead + label: Preference Read + - name: OTHER_Profiling + label: Profiling + +- name: TEST + label: Test + color: darkgray + subcategories: + - name: TEST + label: Test + +- name: LAYOUT + label: Layout + color: purple + subcategories: + - name: LAYOUT + label: Other + - name: LAYOUT_FrameConstruction + label: Frame construction + - name: LAYOUT_Reflow + label: Reflow + - name: LAYOUT_CSSParsing + label: CSS parsing + - name: LAYOUT_SelectorQuery + label: Selector query + - name: LAYOUT_StyleComputation + label: Style computation + - name: LAYOUT_Destroy + label: Layout cleanup + +- name: JS + label: JavaScript + color: yellow + subcategories: + - name: JS + label: Other + - name: JS_Parsing + label: Parsing + - name: JS_BaselineCompilation + label: JIT Compile (baseline) + - name: JS_IonCompilation + label: JIT Compile (ion) + - name: JS_Interpreter + label: Interpreter + - name: JS_BaselineInterpret + label: JIT (baseline-interpreter) + - name: JS_Baseline + label: JIT (baseline) + - name: JS_IonMonkey + label: JIT (ion) + - name: JS_Builtin + label: Builtin API + - name: JS_Wasm + label: Wasm + +- name: GCCC + label: GC / CC + color: orange + subcategories: + - name: GCCC + label: Other + - name: GCCC_MinorGC + label: Minor GC + - name: GCCC_MajorGC + label: Major GC (Other) + - name: GCCC_MajorGC_Mark + label: Major GC (Mark) + - name: GCCC_MajorGC_Sweep + label: Major GC (Sweep) + - name: GCCC_MajorGC_Compact + label: Major GC (Compact) + - name: GCCC_UnmarkGray + label: Unmark Gray + - name: GCCC_Barrier + label: Barrier + - name: GCCC_FreeSnowWhite + label: CC (Free Snow White) + - name: GCCC_BuildGraph + label: CC (Build Graph) + - name: GCCC_ScanRoots + label: CC (Scan Roots) + - name: GCCC_CollectWhite + label: CC (Collect White) + - name: GCCC_Finalize + label: CC (Finalize) + +- name: NETWORK + label: Network + color: lightblue + subcategories: + - name: NETWORK + label: Other + +- name: GRAPHICS + label: Graphics + color: green + subcategories: + - name: GRAPHICS + label: Other + - name: GRAPHICS_DisplayListBuilding + label: DisplayList building + - name: GRAPHICS_DisplayListMerging + label: DisplayList merging + - name: GRAPHICS_LayerBuilding + label: Layer building + - name: GRAPHICS_TileAllocation + label: Tile allocation + - name: GRAPHICS_WRDisplayList + label: WebRender display list + - name: GRAPHICS_Rasterization + label: Rasterization + - name: GRAPHICS_FlushingAsyncPaints + label: Flushing async paints + - name: GRAPHICS_ImageDecoding + label: Image decoding + +- name: DOM + label: DOM + color: blue + subcategories: + - name: DOM + label: Other + +- name: JAVA_ANDROID + label: Android + color: yellow + subcategories: + - name: JAVA_ANDROID + label: Other + +- name: JAVA_ANDROIDX + label: AndroidX + color: orange + subcategories: + - name: JAVA_ANDROIDX + label: Other + +- name: JAVA_LANGUAGE + label: Java + color: blue + subcategories: + - name: JAVA_LANGUAGE + label: Other + +- name: JAVA_MOZILLA + label: Mozilla + color: green + subcategories: + - name: JAVA_MOZILLA + label: Other + +- name: JAVA_KOTLIN + label: Kotlin + color: purple + subcategories: + - name: JAVA_KOTLIN + label: Other + +- name: JAVA_BLOCKED + label: Blocked + color: lightblue + subcategories: + - name: JAVA_BLOCKED + label: Other + +- name: IPC + label: IPC + color: lightgreen + subcategories: + - name: IPC + label: Other + +- name: MEDIA + label: Media + color: orange + subcategories: + - name: MEDIA + label: Other + - name: MEDIA_CUBEB + label: Cubeb + - name: MEDIA_PLAYBACK + label: Playback + - name: MEDIA_RT + label: Real-time rendering + +# We don't name this category ACCESSIBILITY +# because it's already defined as a macro. +- name: A11Y + label: Accessibility + color: brown + subcategories: + - name: A11Y + label: Other + +- name: PROFILER + label: Profiler + color: lightred + subcategories: + - name: PROFILER + label: Other + +- name: TIMER + label: Timer + color: grey + subcategories: + - name: TIMER + label: Other + +- name: REMOTE_PROTOCOL + label: Remote-Protocol + color: grey + subcategories: + - name: REMOTE_PROTOCOL + label: Other diff --git a/mozglue/baseprofiler/core/BaseAndGeckoProfilerDetail.cpp b/mozglue/baseprofiler/core/BaseAndGeckoProfilerDetail.cpp new file mode 100644 index 0000000000..213f25cf16 --- /dev/null +++ b/mozglue/baseprofiler/core/BaseAndGeckoProfilerDetail.cpp @@ -0,0 +1,92 @@ +/* -*- Mode: C++; tab-width: 2; 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 "mozilla/BaseAndGeckoProfilerDetail.h" + +#include <limits> +#include <string_view> + +namespace mozilla::profiler::detail { + +constexpr std::string_view scPidPrefix = "pid:"; + +// Convert a C string to a BaseProfilerProcessId. Return unspecified +// BaseProfilerProcessId if the string is not exactly a valid pid. +static baseprofiler::BaseProfilerProcessId StringToPid(const char* aString) { + if (!aString || aString[0] == '\0') { + // Null or empty. + return baseprofiler::BaseProfilerProcessId{}; + } + + if (aString[0] == '0') { + if (aString[1] != '\0') { + // Don't accept leading zeroes. + return baseprofiler::BaseProfilerProcessId{}; + } + return baseprofiler::BaseProfilerProcessId::FromNumber(0); + } + + using PidNumber = baseprofiler::BaseProfilerProcessId::NumberType; + PidNumber pid = 0; + for (;;) { + const char c = *aString; + if (c == '\0') { + break; + } + if (c < '0' || c > '9') { + // Only accept decimal digits. + return baseprofiler::BaseProfilerProcessId{}; + } + static_assert(!std::numeric_limits<PidNumber>::is_signed, + "The following relies on unsigned arithmetic"); + PidNumber newPid = pid * 10u + PidNumber(c - '0'); + if (newPid < pid) { + // Unsigned overflow. + return baseprofiler::BaseProfilerProcessId{}; + } + pid = newPid; + ++aString; + } + return baseprofiler::BaseProfilerProcessId::FromNumber(pid); +} + +[[nodiscard]] MFBT_API bool FilterHasPid( + const char* aFilter, baseprofiler::BaseProfilerProcessId aPid) { + if (strncmp(aFilter, scPidPrefix.data(), scPidPrefix.length()) != 0) { + // The filter is not starting with "pid:". + return false; + } + + return StringToPid(aFilter + scPidPrefix.length()) == aPid; +} + +[[nodiscard]] MFBT_API bool FiltersExcludePid( + Span<const char* const> aFilters, + baseprofiler::BaseProfilerProcessId aPid) { + if (aFilters.empty()) { + return false; + } + + // First, check if the list only contains "pid:..." strings. + for (const char* const filter : aFilters) { + if (strncmp(filter, scPidPrefix.data(), scPidPrefix.length()) != 0) { + // At least one filter is *not* a "pid:...", our pid is not excluded. + return false; + } + } + + // Here, all filters start with "pid:". Check if the given pid is included. + for (const char* const filter : aFilters) { + if (StringToPid(filter + scPidPrefix.length()) == aPid) { + // Our pid is present, so it's not excluded. + return false; + } + } + // Our pid was not in a list of only pids, so it's excluded. + return true; +} + +} // namespace mozilla::profiler::detail diff --git a/mozglue/baseprofiler/core/EHABIStackWalk.cpp b/mozglue/baseprofiler/core/EHABIStackWalk.cpp new file mode 100644 index 0000000000..0c2c855c9b --- /dev/null +++ b/mozglue/baseprofiler/core/EHABIStackWalk.cpp @@ -0,0 +1,592 @@ +/* -*- 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/. */ + +/* + * This is an implementation of stack unwinding according to a subset + * of the ARM Exception Handling ABI, as described in: + * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0038a/IHI0038A_ehabi.pdf + * + * This handles only the ARM-defined "personality routines" (chapter + * 9), and don't track the value of FP registers, because profiling + * needs only chain of PC/SP values. + * + * Because the exception handling info may not be accurate for all + * possible places where an async signal could occur (e.g., in a + * prologue or epilogue), this bounds-checks all stack accesses. + * + * This file uses "struct" for structures in the exception tables and + * "class" otherwise. We should avoid violating the C++11 + * standard-layout rules in the former. + */ + +#include "BaseProfiler.h" + +#include "EHABIStackWalk.h" + +#include "BaseProfilerSharedLibraries.h" +#include "platform.h" + +#include "mozilla/Atomics.h" +#include "mozilla/DebugOnly.h" +#include "mozilla/EndianUtils.h" + +#include <algorithm> +#include <elf.h> +#include <stdint.h> +#include <vector> +#include <string> + +#ifndef PT_ARM_EXIDX +# define PT_ARM_EXIDX 0x70000001 +#endif + +namespace mozilla { +namespace baseprofiler { + +struct PRel31 { + uint32_t mBits; + bool topBit() const { return mBits & 0x80000000; } + uint32_t value() const { return mBits & 0x7fffffff; } + int32_t offset() const { return (static_cast<int32_t>(mBits) << 1) >> 1; } + const void* compute() const { + return reinterpret_cast<const char*>(this) + offset(); + } + + private: + PRel31(const PRel31& copied) = delete; + PRel31() = delete; +}; + +struct EHEntry { + PRel31 startPC; + PRel31 exidx; + + private: + EHEntry(const EHEntry& copied) = delete; + EHEntry() = delete; +}; + +class EHState { + // Note that any core register can be used as a "frame pointer" to + // influence the unwinding process, so this must track all of them. + uint32_t mRegs[16]; + + public: + bool unwind(const EHEntry* aEntry, const void* stackBase); + uint32_t& operator[](int i) { return mRegs[i]; } + const uint32_t& operator[](int i) const { return mRegs[i]; } + explicit EHState(const mcontext_t&); +}; + +enum { R_SP = 13, R_LR = 14, R_PC = 15 }; + +class EHTable { + uint32_t mStartPC; + uint32_t mEndPC; + uint32_t mBaseAddress; + const EHEntry* mEntriesBegin; + const EHEntry* mEntriesEnd; + std::string mName; + + public: + EHTable(const void* aELF, size_t aSize, const std::string& aName); + const EHEntry* lookup(uint32_t aPC) const; + bool isValid() const { return mEntriesEnd != mEntriesBegin; } + const std::string& name() const { return mName; } + uint32_t startPC() const { return mStartPC; } + uint32_t endPC() const { return mEndPC; } + uint32_t baseAddress() const { return mBaseAddress; } +}; + +class EHAddrSpace { + std::vector<uint32_t> mStarts; + std::vector<EHTable> mTables; + static Atomic<const EHAddrSpace*> sCurrent; + + public: + explicit EHAddrSpace(const std::vector<EHTable>& aTables); + const EHTable* lookup(uint32_t aPC) const; + static void Update(); + static const EHAddrSpace* Get(); +}; + +void EHABIStackWalkInit() { EHAddrSpace::Update(); } + +size_t EHABIStackWalk(const mcontext_t& aContext, void* stackBase, void** aSPs, + void** aPCs, const size_t aNumFrames) { + const EHAddrSpace* space = EHAddrSpace::Get(); + EHState state(aContext); + size_t count = 0; + + while (count < aNumFrames) { + uint32_t pc = state[R_PC], sp = state[R_SP]; + aPCs[count] = reinterpret_cast<void*>(pc); + aSPs[count] = reinterpret_cast<void*>(sp); + count++; + + if (!space) break; + // TODO: cache these lookups. Binary-searching libxul is + // expensive (possibly more expensive than doing the actual + // unwind), and even a small cache should help. + const EHTable* table = space->lookup(pc); + if (!table) break; + const EHEntry* entry = table->lookup(pc); + if (!entry) break; + if (!state.unwind(entry, stackBase)) break; + } + + return count; +} + +class EHInterp { + public: + // Note that stackLimit is exclusive and stackBase is inclusive + // (i.e, stackLimit < SP <= stackBase), following the convention + // set by the AAPCS spec. + EHInterp(EHState& aState, const EHEntry* aEntry, uint32_t aStackLimit, + uint32_t aStackBase) + : mState(aState), + mStackLimit(aStackLimit), + mStackBase(aStackBase), + mNextWord(0), + mWordsLeft(0), + mFailed(false) { + const PRel31& exidx = aEntry->exidx; + uint32_t firstWord; + + if (exidx.mBits == 1) { // EXIDX_CANTUNWIND + mFailed = true; + return; + } + if (exidx.topBit()) { + firstWord = exidx.mBits; + } else { + mNextWord = reinterpret_cast<const uint32_t*>(exidx.compute()); + firstWord = *mNextWord++; + } + + switch (firstWord >> 24) { + case 0x80: // short + mWord = firstWord << 8; + mBytesLeft = 3; + break; + case 0x81: + case 0x82: // long; catch descriptor size ignored + mWord = firstWord << 16; + mBytesLeft = 2; + mWordsLeft = (firstWord >> 16) & 0xff; + break; + default: + // unknown personality + mFailed = true; + } + } + + bool unwind(); + + private: + // TODO: GCC has been observed not CSEing repeated reads of + // mState[R_SP] with writes to mFailed between them, suggesting that + // it hasn't determined that they can't alias and is thus missing + // optimization opportunities. So, we may want to flatten EHState + // into this class; this may also make the code simpler. + EHState& mState; + uint32_t mStackLimit; + uint32_t mStackBase; + const uint32_t* mNextWord; + uint32_t mWord; + uint8_t mWordsLeft; + uint8_t mBytesLeft; + bool mFailed; + + enum { + I_ADDSP = 0x00, // 0sxxxxxx (subtract if s) + M_ADDSP = 0x80, + I_POPMASK = 0x80, // 1000iiii iiiiiiii (if any i set) + M_POPMASK = 0xf0, + I_MOVSP = 0x90, // 1001nnnn + M_MOVSP = 0xf0, + I_POPN = 0xa0, // 1010lnnn + M_POPN = 0xf0, + I_FINISH = 0xb0, // 10110000 + I_POPLO = 0xb1, // 10110001 0000iiii (if any i set) + I_ADDSPBIG = 0xb2, // 10110010 uleb128 + I_POPFDX = 0xb3, // 10110011 sssscccc + I_POPFDX8 = 0xb8, // 10111nnn + M_POPFDX8 = 0xf8, + // "Intel Wireless MMX" extensions omitted. + I_POPFDD = 0xc8, // 1100100h sssscccc + M_POPFDD = 0xfe, + I_POPFDD8 = 0xd0, // 11010nnn + M_POPFDD8 = 0xf8 + }; + + uint8_t next() { + if (mBytesLeft == 0) { + if (mWordsLeft == 0) { + return I_FINISH; + } + mWordsLeft--; + mWord = *mNextWord++; + mBytesLeft = 4; + } + mBytesLeft--; + mWord = (mWord << 8) | (mWord >> 24); // rotate + return mWord; + } + + uint32_t& vSP() { return mState[R_SP]; } + uint32_t* ptrSP() { return reinterpret_cast<uint32_t*>(vSP()); } + + void checkStackBase() { + if (vSP() > mStackBase) mFailed = true; + } + void checkStackLimit() { + if (vSP() <= mStackLimit) mFailed = true; + } + void checkStackAlign() { + if ((vSP() & 3) != 0) mFailed = true; + } + void checkStack() { + checkStackBase(); + checkStackLimit(); + checkStackAlign(); + } + + void popRange(uint8_t first, uint8_t last, uint16_t mask) { + bool hasSP = false; + uint32_t tmpSP; + if (mask == 0) mFailed = true; + for (uint8_t r = first; r <= last; ++r) { + if (mask & 1) { + if (r == R_SP) { + hasSP = true; + tmpSP = *ptrSP(); + } else + mState[r] = *ptrSP(); + vSP() += 4; + checkStackBase(); + if (mFailed) return; + } + mask >>= 1; + } + if (hasSP) { + vSP() = tmpSP; + checkStack(); + } + } +}; + +bool EHState::unwind(const EHEntry* aEntry, const void* stackBasePtr) { + // The unwinding program cannot set SP to less than the initial value. + uint32_t stackLimit = mRegs[R_SP] - 4; + uint32_t stackBase = reinterpret_cast<uint32_t>(stackBasePtr); + EHInterp interp(*this, aEntry, stackLimit, stackBase); + return interp.unwind(); +} + +bool EHInterp::unwind() { + mState[R_PC] = 0; + checkStack(); + while (!mFailed) { + uint8_t insn = next(); +#if DEBUG_EHABI_UNWIND + LOG("unwind insn = %02x", (unsigned)insn); +#endif + // Try to put the common cases first. + + // 00xxxxxx: vsp = vsp + (xxxxxx << 2) + 4 + // 01xxxxxx: vsp = vsp - (xxxxxx << 2) - 4 + if ((insn & M_ADDSP) == I_ADDSP) { + uint32_t offset = ((insn & 0x3f) << 2) + 4; + if (insn & 0x40) { + vSP() -= offset; + checkStackLimit(); + } else { + vSP() += offset; + checkStackBase(); + } + continue; + } + + // 10100nnn: Pop r4-r[4+nnn] + // 10101nnn: Pop r4-r[4+nnn], r14 + if ((insn & M_POPN) == I_POPN) { + uint8_t n = (insn & 0x07) + 1; + bool lr = insn & 0x08; + uint32_t* ptr = ptrSP(); + vSP() += (n + (lr ? 1 : 0)) * 4; + checkStackBase(); + for (uint8_t r = 4; r < 4 + n; ++r) mState[r] = *ptr++; + if (lr) mState[R_LR] = *ptr++; + continue; + } + + // 1011000: Finish + if (insn == I_FINISH) { + if (mState[R_PC] == 0) { + mState[R_PC] = mState[R_LR]; + // Non-standard change (bug 916106): Prevent the caller from + // re-using LR. Since the caller is by definition not a leaf + // routine, it will have to restore LR from somewhere to + // return to its own caller, so we can safely zero it here. + // This makes a difference only if an error in unwinding + // (e.g., caused by starting from within a prologue/epilogue) + // causes us to load a pointer to a leaf routine as LR; if we + // don't do something, we'll go into an infinite loop of + // "returning" to that same function. + mState[R_LR] = 0; + } + return true; + } + + // 1001nnnn: Set vsp = r[nnnn] + if ((insn & M_MOVSP) == I_MOVSP) { + vSP() = mState[insn & 0x0f]; + checkStack(); + continue; + } + + // 11001000 sssscccc: Pop VFP regs D[16+ssss]-D[16+ssss+cccc] (as FLDMFDD) + // 11001001 sssscccc: Pop VFP regs D[ssss]-D[ssss+cccc] (as FLDMFDD) + if ((insn & M_POPFDD) == I_POPFDD) { + uint8_t n = (next() & 0x0f) + 1; + // Note: if the 16+ssss+cccc > 31, the encoding is reserved. + // As the space is currently unused, we don't try to check. + vSP() += 8 * n; + checkStackBase(); + continue; + } + + // 11010nnn: Pop VFP regs D[8]-D[8+nnn] (as FLDMFDD) + if ((insn & M_POPFDD8) == I_POPFDD8) { + uint8_t n = (insn & 0x07) + 1; + vSP() += 8 * n; + checkStackBase(); + continue; + } + + // 10110010 uleb128: vsp = vsp + 0x204 + (uleb128 << 2) + if (insn == I_ADDSPBIG) { + uint32_t acc = 0; + uint8_t shift = 0; + uint8_t byte; + do { + if (shift >= 32) return false; + byte = next(); + acc |= (byte & 0x7f) << shift; + shift += 7; + } while (byte & 0x80); + uint32_t offset = 0x204 + (acc << 2); + // The calculations above could have overflowed. + // But the one we care about is this: + if (vSP() + offset < vSP()) mFailed = true; + vSP() += offset; + // ...so that this is the only other check needed: + checkStackBase(); + continue; + } + + // 1000iiii iiiiiiii (i not all 0): Pop under masks {r15-r12}, {r11-r4} + if ((insn & M_POPMASK) == I_POPMASK) { + popRange(4, 15, ((insn & 0x0f) << 8) | next()); + continue; + } + + // 1011001 0000iiii (i not all 0): Pop under mask {r3-r0} + if (insn == I_POPLO) { + popRange(0, 3, next() & 0x0f); + continue; + } + + // 10110011 sssscccc: Pop VFP regs D[ssss]-D[ssss+cccc] (as FLDMFDX) + if (insn == I_POPFDX) { + uint8_t n = (next() & 0x0f) + 1; + vSP() += 8 * n + 4; + checkStackBase(); + continue; + } + + // 10111nnn: Pop VFP regs D[8]-D[8+nnn] (as FLDMFDX) + if ((insn & M_POPFDX8) == I_POPFDX8) { + uint8_t n = (insn & 0x07) + 1; + vSP() += 8 * n + 4; + checkStackBase(); + continue; + } + + // unhandled instruction +#ifdef DEBUG_EHABI_UNWIND + LOG("Unhandled EHABI instruction 0x%02x", insn); +#endif + mFailed = true; + } + return false; +} + +bool operator<(const EHTable& lhs, const EHTable& rhs) { + return lhs.startPC() < rhs.startPC(); +} + +// Async signal unsafe. +EHAddrSpace::EHAddrSpace(const std::vector<EHTable>& aTables) + : mTables(aTables) { + std::sort(mTables.begin(), mTables.end()); + DebugOnly<uint32_t> lastEnd = 0; + for (std::vector<EHTable>::iterator i = mTables.begin(); i != mTables.end(); + ++i) { + MOZ_ASSERT(i->startPC() >= lastEnd); + mStarts.push_back(i->startPC()); + lastEnd = i->endPC(); + } +} + +const EHTable* EHAddrSpace::lookup(uint32_t aPC) const { + ptrdiff_t i = (std::upper_bound(mStarts.begin(), mStarts.end(), aPC) - + mStarts.begin()) - + 1; + + if (i < 0 || aPC >= mTables[i].endPC()) return 0; + return &mTables[i]; +} + +const EHEntry* EHTable::lookup(uint32_t aPC) const { + MOZ_ASSERT(aPC >= mStartPC); + if (aPC >= mEndPC) return nullptr; + + const EHEntry* begin = mEntriesBegin; + const EHEntry* end = mEntriesEnd; + MOZ_ASSERT(begin < end); + if (aPC < reinterpret_cast<uint32_t>(begin->startPC.compute())) + return nullptr; + + while (end - begin > 1) { +#ifdef EHABI_UNWIND_MORE_ASSERTS + if ((end - 1)->startPC.compute() < begin->startPC.compute()) { + MOZ_CRASH("unsorted exidx"); + } +#endif + const EHEntry* mid = begin + (end - begin) / 2; + if (aPC < reinterpret_cast<uint32_t>(mid->startPC.compute())) + end = mid; + else + begin = mid; + } + return begin; +} + +#if MOZ_LITTLE_ENDIAN() +static const unsigned char hostEndian = ELFDATA2LSB; +#elif MOZ_BIG_ENDIAN() +static const unsigned char hostEndian = ELFDATA2MSB; +#else +# error "No endian?" +#endif + +// Async signal unsafe: std::vector::reserve, std::string copy ctor. +EHTable::EHTable(const void* aELF, size_t aSize, const std::string& aName) + : mStartPC(~0), // largest uint32_t + mEndPC(0), + mEntriesBegin(nullptr), + mEntriesEnd(nullptr), + mName(aName) { + const uint32_t fileHeaderAddr = reinterpret_cast<uint32_t>(aELF); + + if (aSize < sizeof(Elf32_Ehdr)) return; + + const Elf32_Ehdr& file = *(reinterpret_cast<Elf32_Ehdr*>(fileHeaderAddr)); + if (memcmp(&file.e_ident[EI_MAG0], ELFMAG, SELFMAG) != 0 || + file.e_ident[EI_CLASS] != ELFCLASS32 || + file.e_ident[EI_DATA] != hostEndian || + file.e_ident[EI_VERSION] != EV_CURRENT || file.e_machine != EM_ARM || + file.e_version != EV_CURRENT) + // e_flags? + return; + + MOZ_ASSERT(file.e_phoff + file.e_phnum * file.e_phentsize <= aSize); + const Elf32_Phdr *exidxHdr = 0, *zeroHdr = 0; + for (unsigned i = 0; i < file.e_phnum; ++i) { + const Elf32_Phdr& phdr = *(reinterpret_cast<Elf32_Phdr*>( + fileHeaderAddr + file.e_phoff + i * file.e_phentsize)); + if (phdr.p_type == PT_ARM_EXIDX) { + exidxHdr = &phdr; + } else if (phdr.p_type == PT_LOAD) { + if (phdr.p_offset == 0) { + zeroHdr = &phdr; + } + if (phdr.p_flags & PF_X) { + mStartPC = std::min(mStartPC, phdr.p_vaddr); + mEndPC = std::max(mEndPC, phdr.p_vaddr + phdr.p_memsz); + } + } + } + if (!exidxHdr) return; + if (!zeroHdr) return; + mBaseAddress = fileHeaderAddr - zeroHdr->p_vaddr; + mStartPC += mBaseAddress; + mEndPC += mBaseAddress; + mEntriesBegin = + reinterpret_cast<const EHEntry*>(mBaseAddress + exidxHdr->p_vaddr); + mEntriesEnd = reinterpret_cast<const EHEntry*>( + mBaseAddress + exidxHdr->p_vaddr + exidxHdr->p_memsz); +} + +Atomic<const EHAddrSpace*> EHAddrSpace::sCurrent(nullptr); + +// Async signal safe; can fail if Update() hasn't returned yet. +const EHAddrSpace* EHAddrSpace::Get() { return sCurrent; } + +// Collect unwinding information from loaded objects. Calls after the +// first have no effect. Async signal unsafe. +void EHAddrSpace::Update() { + const EHAddrSpace* space = sCurrent; + if (space) return; + + SharedLibraryInfo info = SharedLibraryInfo::GetInfoForSelf(); + std::vector<EHTable> tables; + + for (size_t i = 0; i < info.GetSize(); ++i) { + const SharedLibrary& lib = info.GetEntry(i); + // FIXME: This isn't correct if the start address isn't p_offset 0, because + // the start address will not point at the file header. But this is worked + // around by magic number checks in the EHTable constructor. + EHTable tab(reinterpret_cast<const void*>(lib.GetStart()), + lib.GetEnd() - lib.GetStart(), lib.GetDebugPath()); + if (tab.isValid()) tables.push_back(tab); + } + space = new EHAddrSpace(tables); + + if (!sCurrent.compareExchange(nullptr, space)) { + delete space; + space = sCurrent; + } +} + +EHState::EHState(const mcontext_t& context) { +#ifdef linux + mRegs[0] = context.arm_r0; + mRegs[1] = context.arm_r1; + mRegs[2] = context.arm_r2; + mRegs[3] = context.arm_r3; + mRegs[4] = context.arm_r4; + mRegs[5] = context.arm_r5; + mRegs[6] = context.arm_r6; + mRegs[7] = context.arm_r7; + mRegs[8] = context.arm_r8; + mRegs[9] = context.arm_r9; + mRegs[10] = context.arm_r10; + mRegs[11] = context.arm_fp; + mRegs[12] = context.arm_ip; + mRegs[13] = context.arm_sp; + mRegs[14] = context.arm_lr; + mRegs[15] = context.arm_pc; +#else +# error "Unhandled OS for ARM EHABI unwinding" +#endif +} + +} // namespace baseprofiler +} // namespace mozilla diff --git a/mozglue/baseprofiler/core/EHABIStackWalk.h b/mozglue/baseprofiler/core/EHABIStackWalk.h new file mode 100644 index 0000000000..d5f4edc0d7 --- /dev/null +++ b/mozglue/baseprofiler/core/EHABIStackWalk.h @@ -0,0 +1,30 @@ +/* -*- 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/. */ + +/* + * This is an implementation of stack unwinding according to a subset + * of the ARM Exception Handling ABI; see the comment at the top of + * the .cpp file for details. + */ + +#ifndef mozilla_EHABIStackWalk_h__ +#define mozilla_EHABIStackWalk_h__ + +#include <stddef.h> +#include <ucontext.h> + +namespace mozilla { +namespace baseprofiler { + +void EHABIStackWalkInit(); + +size_t EHABIStackWalk(const mcontext_t& aContext, void* stackBase, void** aSPs, + void** aPCs, size_t aNumFrames); + +} // namespace baseprofiler +} // namespace mozilla + +#endif diff --git a/mozglue/baseprofiler/core/PageInformation.cpp b/mozglue/baseprofiler/core/PageInformation.cpp new file mode 100644 index 0000000000..bb7c7872aa --- /dev/null +++ b/mozglue/baseprofiler/core/PageInformation.cpp @@ -0,0 +1,49 @@ +/* -*- Mode: C++; tab-width: 2; 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 "PageInformation.h" + +#include "BaseProfiler.h" + +#include "mozilla/BaseProfileJSONWriter.h" + +namespace mozilla { +namespace baseprofiler { + +PageInformation::PageInformation(uint64_t aTabID, uint64_t aInnerWindowID, + const std::string& aUrl, + uint64_t aEmbedderInnerWindowID) + : mTabID(aTabID), + mInnerWindowID(aInnerWindowID), + mUrl(aUrl), + mEmbedderInnerWindowID(aEmbedderInnerWindowID), + mRefCnt(0) {} + +bool PageInformation::Equals(PageInformation* aOtherPageInfo) const { + // It's enough to check inner window IDs because they are unique for each + // page. Therefore, we don't have to check the tab ID or url. + return InnerWindowID() == aOtherPageInfo->InnerWindowID(); +} + +void PageInformation::StreamJSON(SpliceableJSONWriter& aWriter) const { + aWriter.StartObjectElement(); + // Here, we are converting uint64_t to double. Both tab and Inner + // Window IDs are created using `nsContentUtils::GenerateProcessSpecificId`, + // which is specifically designed to only use 53 of the 64 bits to be lossless + // when passed into and out of JS as a double. + aWriter.DoubleProperty("tabID", TabID()); + aWriter.DoubleProperty("innerWindowID", InnerWindowID()); + aWriter.StringProperty("url", Url()); + aWriter.DoubleProperty("embedderInnerWindowID", EmbedderInnerWindowID()); + aWriter.EndObject(); +} + +size_t PageInformation::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const { + return aMallocSizeOf(this); +} + +} // namespace baseprofiler +} // namespace mozilla diff --git a/mozglue/baseprofiler/core/PageInformation.h b/mozglue/baseprofiler/core/PageInformation.h new file mode 100644 index 0000000000..41808877b3 --- /dev/null +++ b/mozglue/baseprofiler/core/PageInformation.h @@ -0,0 +1,77 @@ +/* -*- Mode: C++; tab-width: 2; 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/. */ + +#ifndef PageInformation_h +#define PageInformation_h + +#include "mozilla/Atomics.h" +#include "mozilla/Maybe.h" +#include "mozilla/MemoryReporting.h" + +#include <string> + +namespace mozilla { +namespace baseprofiler { + +class SpliceableJSONWriter; + +// This class contains information that's relevant to a single page only +// while the page information is important and registered with the profiler, +// but regardless of whether the profiler is running. All accesses to it are +// protected by the profiler state lock. +// When the page gets unregistered, we keep the profiler buffer position +// to determine if we are still using this page. If not, we unregister +// it in the next page registration. +class PageInformation final { + public: + PageInformation(uint64_t aTabID, uint64_t aInnerWindowID, + const std::string& aUrl, uint64_t aEmbedderInnerWindowID); + + // Using hand-rolled ref-counting, because RefCounted.h macros don't produce + // the same code between mozglue and libxul, see bug 1536656. + MFBT_API void AddRef() const { ++mRefCnt; } + MFBT_API void Release() const { + MOZ_ASSERT(int32_t(mRefCnt) > 0); + if (--mRefCnt) { + delete this; + } + } + + size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const; + bool Equals(PageInformation* aOtherPageInfo) const; + void StreamJSON(SpliceableJSONWriter& aWriter) const; + + uint64_t InnerWindowID() const { return mInnerWindowID; } + uint64_t TabID() const { return mTabID; } + const std::string& Url() const { return mUrl; } + uint64_t EmbedderInnerWindowID() const { return mEmbedderInnerWindowID; } + + Maybe<uint64_t> BufferPositionWhenUnregistered() const { + return mBufferPositionWhenUnregistered; + } + + void NotifyUnregistered(uint64_t aBufferPosition) { + mBufferPositionWhenUnregistered = Some(aBufferPosition); + } + + private: + const uint64_t mTabID; + const uint64_t mInnerWindowID; + const std::string mUrl; + const uint64_t mEmbedderInnerWindowID; + + // Holds the buffer position when page is unregistered. + // It's used to determine if we still use this page in the profiler or + // not. + Maybe<uint64_t> mBufferPositionWhenUnregistered; + + mutable Atomic<int32_t, MemoryOrdering::ReleaseAcquire> mRefCnt; +}; + +} // namespace baseprofiler +} // namespace mozilla + +#endif // PageInformation_h diff --git a/mozglue/baseprofiler/core/PlatformMacros.h b/mozglue/baseprofiler/core/PlatformMacros.h new file mode 100644 index 0000000000..c72e94c128 --- /dev/null +++ b/mozglue/baseprofiler/core/PlatformMacros.h @@ -0,0 +1,130 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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/. */ + +#ifndef PLATFORM_MACROS_H +#define PLATFORM_MACROS_H + +// Define platform selection macros in a consistent way. Don't add anything +// else to this file, so it can remain freestanding. The primary factorisation +// is on (ARCH,OS) pairs ("PLATforms") but ARCH_ and OS_ macros are defined +// too, since they are sometimes convenient. +// +// Note: "GP" is short for "Gecko Profiler". + +#undef GP_PLAT_x86_android +#undef GP_PLAT_amd64_android +#undef GP_PLAT_arm_android +#undef GP_PLAT_arm64_android +#undef GP_PLAT_x86_linux +#undef GP_PLAT_amd64_linux +#undef GP_PLAT_arm_linux +#undef GP_PLAT_mips64_linux +#undef GP_PLAT_amd64_darwin +#undef GP_PLAT_arm64_darwin +#undef GP_PLAT_x86_windows +#undef GP_PLAT_amd64_windows +#undef GP_PLAT_arm64_windows + +#undef GP_ARCH_x86 +#undef GP_ARCH_amd64 +#undef GP_ARCH_arm +#undef GP_ARCH_arm64 +#undef GP_ARCH_mips64 + +#undef GP_OS_android +#undef GP_OS_linux +#undef GP_OS_darwin +#undef GP_OS_windows + +// We test __ANDROID__ before __linux__ because __linux__ is defined on both +// Android and Linux, whereas GP_OS_android is not defined on vanilla Linux. + +#if defined(__ANDROID__) && defined(__i386__) +# define GP_PLAT_x86_android 1 +# define GP_ARCH_x86 1 +# define GP_OS_android 1 + +#elif defined(__ANDROID__) && defined(__x86_64__) +# define GP_PLAT_amd64_android 1 +# define GP_ARCH_amd64 1 +# define GP_OS_android 1 + +#elif defined(__ANDROID__) && defined(__arm__) +# define GP_PLAT_arm_android 1 +# define GP_ARCH_arm 1 +# define GP_OS_android 1 + +#elif defined(__ANDROID__) && defined(__aarch64__) +# define GP_PLAT_arm64_android 1 +# define GP_ARCH_arm64 1 +# define GP_OS_android 1 + +#elif defined(__linux__) && defined(__i386__) +# define GP_PLAT_x86_linux 1 +# define GP_ARCH_x86 1 +# define GP_OS_linux 1 + +#elif defined(__linux__) && defined(__x86_64__) +# define GP_PLAT_amd64_linux 1 +# define GP_ARCH_amd64 1 +# define GP_OS_linux 1 + +#elif defined(__linux__) && defined(__arm__) +# define GP_PLAT_arm_linux 1 +# define GP_ARCH_arm 1 +# define GP_OS_linux 1 + +#elif defined(__linux__) && defined(__aarch64__) +# define GP_PLAT_arm64_linux 1 +# define GP_ARCH_arm64 1 +# define GP_OS_linux 1 + +#elif defined(__linux__) && defined(__mips64) +# define GP_PLAT_mips64_linux 1 +# define GP_ARCH_mips64 1 +# define GP_OS_linux 1 + +#elif defined(__APPLE__) && defined(__aarch64__) +# define GP_PLAT_arm64_darwin 1 +# define GP_ARCH_arm64 1 +# define GP_OS_darwin 1 + +#elif defined(__APPLE__) && defined(__x86_64__) +# define GP_PLAT_amd64_darwin 1 +# define GP_ARCH_amd64 1 +# define GP_OS_darwin 1 + +#elif defined(__FreeBSD__) && defined(__x86_64__) +# define GP_PLAT_amd64_freebsd 1 +# define GP_ARCH_amd64 1 +# define GP_OS_freebsd 1 + +#elif defined(__FreeBSD__) && defined(__aarch64__) +# define GP_PLAT_arm64_freebsd 1 +# define GP_ARCH_arm64 1 +# define GP_OS_freebsd 1 + +#elif (defined(_MSC_VER) || defined(__MINGW32__)) && \ + (defined(_M_IX86) || defined(__i386__)) +# define GP_PLAT_x86_windows 1 +# define GP_ARCH_x86 1 +# define GP_OS_windows 1 + +#elif (defined(_MSC_VER) || defined(__MINGW32__)) && \ + (defined(_M_X64) || defined(__x86_64__)) +# define GP_PLAT_amd64_windows 1 +# define GP_ARCH_amd64 1 +# define GP_OS_windows 1 + +#elif defined(_MSC_VER) && defined(_M_ARM64) +# define GP_PLAT_arm64_windows 1 +# define GP_ARCH_arm64 1 +# define GP_OS_windows 1 + +#else +# error "Unsupported platform" +#endif + +#endif /* ndef PLATFORM_MACROS_H */ diff --git a/mozglue/baseprofiler/core/ProfileBuffer.cpp b/mozglue/baseprofiler/core/ProfileBuffer.cpp new file mode 100644 index 0000000000..dd5504274b --- /dev/null +++ b/mozglue/baseprofiler/core/ProfileBuffer.cpp @@ -0,0 +1,217 @@ +/* -*- Mode: C++; tab-width: 2; 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 "ProfileBuffer.h" + +#include "mozilla/MathAlgorithms.h" + +#include "BaseProfiler.h" + +namespace mozilla { +namespace baseprofiler { + +ProfileBuffer::ProfileBuffer(ProfileChunkedBuffer& aBuffer) + : mEntries(aBuffer) { + // Assume the given buffer is in-session. + MOZ_ASSERT(mEntries.IsInSession()); +} + +/* static */ +ProfileBufferBlockIndex ProfileBuffer::AddEntry( + ProfileChunkedBuffer& aProfileChunkedBuffer, + const ProfileBufferEntry& aEntry) { + switch (aEntry.GetKind()) { +#define SWITCH_KIND(KIND, TYPE, SIZE) \ + case ProfileBufferEntry::Kind::KIND: { \ + return aProfileChunkedBuffer.PutFrom(&aEntry, 1 + (SIZE)); \ + break; \ + } + + FOR_EACH_PROFILE_BUFFER_ENTRY_KIND(SWITCH_KIND) + +#undef SWITCH_KIND + default: + MOZ_ASSERT(false, "Unhandled baseprofiler::ProfilerBuffer entry KIND"); + return ProfileBufferBlockIndex{}; + } +} + +// Called from signal, call only reentrant functions +uint64_t ProfileBuffer::AddEntry(const ProfileBufferEntry& aEntry) { + return AddEntry(mEntries, aEntry).ConvertToProfileBufferIndex(); +} + +/* static */ +ProfileBufferBlockIndex ProfileBuffer::AddThreadIdEntry( + ProfileChunkedBuffer& aProfileChunkedBuffer, + BaseProfilerThreadId aThreadId) { + return AddEntry(aProfileChunkedBuffer, + ProfileBufferEntry::ThreadId(aThreadId)); +} + +uint64_t ProfileBuffer::AddThreadIdEntry(BaseProfilerThreadId aThreadId) { + return AddThreadIdEntry(mEntries, aThreadId).ConvertToProfileBufferIndex(); +} + +void ProfileBuffer::CollectCodeLocation( + const char* aLabel, const char* aStr, uint32_t aFrameFlags, + uint64_t aInnerWindowID, const Maybe<uint32_t>& aLineNumber, + const Maybe<uint32_t>& aColumnNumber, + const Maybe<ProfilingCategoryPair>& aCategoryPair) { + AddEntry(ProfileBufferEntry::Label(aLabel)); + AddEntry(ProfileBufferEntry::FrameFlags(uint64_t(aFrameFlags))); + + if (aStr) { + // Store the string using one or more DynamicStringFragment entries. + size_t strLen = strlen(aStr) + 1; // +1 for the null terminator + // If larger than the prescribed limit, we will cut the string and end it + // with an ellipsis. + const bool tooBig = strLen > kMaxFrameKeyLength; + if (tooBig) { + strLen = kMaxFrameKeyLength; + } + char chars[ProfileBufferEntry::kNumChars]; + for (size_t j = 0;; j += ProfileBufferEntry::kNumChars) { + // Store up to kNumChars characters in the entry. + size_t len = ProfileBufferEntry::kNumChars; + const bool last = j + len >= strLen; + if (last) { + // Only the last entry may be smaller than kNumChars. + len = strLen - j; + if (tooBig) { + // That last entry is part of a too-big string, replace the end + // characters with an ellipsis "...". + len = std::max(len, size_t(4)); + chars[len - 4] = '.'; + chars[len - 3] = '.'; + chars[len - 2] = '.'; + chars[len - 1] = '\0'; + // Make sure the memcpy will not overwrite our ellipsis! + len -= 4; + } + } + memcpy(chars, &aStr[j], len); + AddEntry(ProfileBufferEntry::DynamicStringFragment(chars)); + if (last) { + break; + } + } + } + + if (aInnerWindowID) { + AddEntry(ProfileBufferEntry::InnerWindowID(aInnerWindowID)); + } + + if (aLineNumber) { + AddEntry(ProfileBufferEntry::LineNumber(*aLineNumber)); + } + + if (aColumnNumber) { + AddEntry(ProfileBufferEntry::ColumnNumber(*aColumnNumber)); + } + + if (aCategoryPair.isSome()) { + AddEntry(ProfileBufferEntry::CategoryPair(int(*aCategoryPair))); + } +} + +size_t ProfileBuffer::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const { + // Measurement of the following members may be added later if DMD finds it + // is worthwhile: + // - memory pointed to by the elements within mEntries + return mEntries.SizeOfExcludingThis(aMallocSizeOf); +} + +size_t ProfileBuffer::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const { + return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf); +} + +void ProfileBuffer::CollectOverheadStats(TimeDuration aSamplingTime, + TimeDuration aLocking, + TimeDuration aCleaning, + TimeDuration aCounters, + TimeDuration aThreads) { + double timeUs = aSamplingTime.ToMilliseconds() * 1000.0; + if (mFirstSamplingTimeUs == 0.0) { + mFirstSamplingTimeUs = timeUs; + } else { + // Note that we'll have 1 fewer interval than other numbers (because + // we need both ends of an interval to know its duration). The final + // difference should be insignificant over the expected many thousands + // of iterations. + mIntervalsUs.Count(timeUs - mLastSamplingTimeUs); + } + mLastSamplingTimeUs = timeUs; + // Time to take the lock before sampling. + double lockingUs = aLocking.ToMilliseconds() * 1000.0; + // Time to discard expired data. + double cleaningUs = aCleaning.ToMilliseconds() * 1000.0; + // Time to gather all counters. + double countersUs = aCounters.ToMilliseconds() * 1000.0; + // Time to sample all threads. + double threadsUs = aThreads.ToMilliseconds() * 1000.0; + + // Add to our gathered stats. + mOverheadsUs.Count(lockingUs + cleaningUs + countersUs + threadsUs); + mLockingsUs.Count(lockingUs); + mCleaningsUs.Count(cleaningUs); + mCountersUs.Count(countersUs); + mThreadsUs.Count(threadsUs); + + // Record details in buffer, if requested. + static const bool sRecordSamplingOverhead = []() { + const char* recordOverheads = getenv("MOZ_PROFILER_RECORD_OVERHEADS"); + return recordOverheads && recordOverheads[0] != '\0'; + }(); + if (sRecordSamplingOverhead) { + AddEntry(ProfileBufferEntry::ProfilerOverheadTime(timeUs)); + AddEntry(ProfileBufferEntry::ProfilerOverheadDuration(lockingUs)); + AddEntry(ProfileBufferEntry::ProfilerOverheadDuration(cleaningUs)); + AddEntry(ProfileBufferEntry::ProfilerOverheadDuration(countersUs)); + AddEntry(ProfileBufferEntry::ProfilerOverheadDuration(threadsUs)); + } +} + +ProfilerBufferInfo ProfileBuffer::GetProfilerBufferInfo() const { + return {BufferRangeStart(), + BufferRangeEnd(), + static_cast<uint32_t>(*mEntries.BufferLength() / + 8), // 8 bytes per entry. + mIntervalsUs, + mOverheadsUs, + mLockingsUs, + mCleaningsUs, + mCountersUs, + mThreadsUs}; +} + +/* ProfileBufferCollector */ + +void ProfileBufferCollector::CollectNativeLeafAddr(void* aAddr) { + mBuf.AddEntry(ProfileBufferEntry::NativeLeafAddr(aAddr)); +} + +void ProfileBufferCollector::CollectProfilingStackFrame( + const ProfilingStackFrame& aFrame) { + // WARNING: this function runs within the profiler's "critical section". + + MOZ_ASSERT(aFrame.isLabelFrame() || + (aFrame.isJsFrame() && !aFrame.isOSRFrame())); + + const char* label = aFrame.label(); + const char* dynamicString = aFrame.dynamicString(); + Maybe<uint32_t> line; + Maybe<uint32_t> column; + + MOZ_ASSERT(aFrame.isLabelFrame()); + + mBuf.CollectCodeLocation(label, dynamicString, aFrame.flags(), + aFrame.realmID(), line, column, + Some(aFrame.categoryPair())); +} + +} // namespace baseprofiler +} // namespace mozilla diff --git a/mozglue/baseprofiler/core/ProfileBuffer.h b/mozglue/baseprofiler/core/ProfileBuffer.h new file mode 100644 index 0000000000..f77b429df8 --- /dev/null +++ b/mozglue/baseprofiler/core/ProfileBuffer.h @@ -0,0 +1,211 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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/. */ + +#ifndef MOZ_PROFILE_BUFFER_H +#define MOZ_PROFILE_BUFFER_H + +#include "ProfileBufferEntry.h" + +#include "BaseProfiler.h" +#include "mozilla/Maybe.h" +#include "mozilla/PowerOfTwo.h" +#include "mozilla/ProfileBufferChunkManagerSingle.h" +#include "mozilla/ProfileChunkedBuffer.h" + +namespace mozilla { +namespace baseprofiler { + +// Class storing most profiling data in a ProfileChunkedBuffer. +// +// This class is used as a queue of entries which, after construction, never +// allocates. This makes it safe to use in the profiler's "critical section". +class ProfileBuffer final { + public: + // ProfileBuffer constructor + // @param aBuffer The in-session ProfileChunkedBuffer to use as buffer + // manager. + explicit ProfileBuffer(ProfileChunkedBuffer& aBuffer); + + ProfileChunkedBuffer& UnderlyingChunkedBuffer() const { return mEntries; } + + bool IsThreadSafe() const { return mEntries.IsThreadSafe(); } + + // Add |aEntry| to the buffer, ignoring what kind of entry it is. + // Returns the position of the entry. + uint64_t AddEntry(const ProfileBufferEntry& aEntry); + + // Add to the buffer a sample start (ThreadId) entry for aThreadId. + // Returns the position of the entry. + uint64_t AddThreadIdEntry(BaseProfilerThreadId aThreadId); + + void CollectCodeLocation(const char* aLabel, const char* aStr, + uint32_t aFrameFlags, uint64_t aInnerWindowID, + const Maybe<uint32_t>& aLineNumber, + const Maybe<uint32_t>& aColumnNumber, + const Maybe<ProfilingCategoryPair>& aCategoryPair); + + // Maximum size of a frameKey string that we'll handle. + static const size_t kMaxFrameKeyLength = 512; + + // Stream JSON for samples in the buffer to aWriter, using the supplied + // UniqueStacks object. + // Only streams samples for the given thread ID and which were taken at or + // after aSinceTime. If ID is 0, ignore the stored thread ID; this should only + // be used when the buffer contains only one sample. + // Return the thread ID of the streamed sample(s), or 0. + BaseProfilerThreadId StreamSamplesToJSON(SpliceableJSONWriter& aWriter, + BaseProfilerThreadId aThreadId, + double aSinceTime, + UniqueStacks& aUniqueStacks) const; + + void StreamMarkersToJSON(SpliceableJSONWriter& aWriter, + BaseProfilerThreadId aThreadId, + const TimeStamp& aProcessStartTime, + double aSinceTime, + UniqueStacks& aUniqueStacks) const; + void StreamPausedRangesToJSON(SpliceableJSONWriter& aWriter, + double aSinceTime) const; + void StreamProfilerOverheadToJSON(SpliceableJSONWriter& aWriter, + const TimeStamp& aProcessStartTime, + double aSinceTime) const; + void StreamCountersToJSON(SpliceableJSONWriter& aWriter, + const TimeStamp& aProcessStartTime, + double aSinceTime) const; + + // Find (via |aLastSample|) the most recent sample for the thread denoted by + // |aThreadId| and clone it, patching in the current time as appropriate. + // Mutate |aLastSample| to point to the newly inserted sample. + // Returns whether duplication was successful. + bool DuplicateLastSample(BaseProfilerThreadId aThreadId, + const TimeStamp& aProcessStartTime, + Maybe<uint64_t>& aLastSample); + + void DiscardSamplesBeforeTime(double aTime); + + size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const; + size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const; + + void CollectOverheadStats(TimeDuration aSamplingTime, TimeDuration aLocking, + TimeDuration aCleaning, TimeDuration aCounters, + TimeDuration aThreads); + + ProfilerBufferInfo GetProfilerBufferInfo() const; + + private: + // Add |aEntry| to the provider ProfileChunkedBuffer. + // `static` because it may be used to add an entry to a `ProfileChunkedBuffer` + // that is not attached to a `ProfileBuffer`. + static ProfileBufferBlockIndex AddEntry( + ProfileChunkedBuffer& aProfileChunkedBuffer, + const ProfileBufferEntry& aEntry); + + // Add a sample start (ThreadId) entry for aThreadId to the provided + // ProfileChunkedBuffer. Returns the position of the entry. + // `static` because it may be used to add an entry to a `ProfileChunkedBuffer` + // that is not attached to a `ProfileBuffer`. + static ProfileBufferBlockIndex AddThreadIdEntry( + ProfileChunkedBuffer& aProfileChunkedBuffer, + BaseProfilerThreadId aThreadId); + + // The storage in which this ProfileBuffer stores its entries. + ProfileChunkedBuffer& mEntries; + + public: + // `BufferRangeStart()` and `BufferRangeEnd()` return `uint64_t` values + // corresponding to the first entry and past the last entry stored in + // `mEntries`. + // + // The returned values are not guaranteed to be stable, because other threads + // may also be accessing the buffer concurrently. But they will always + // increase, and can therefore give an indication of how far these values have + // *at least* reached. In particular: + // - Entries whose index is strictly less that `BufferRangeStart()` have been + // discarded by now, so any related data may also be safely discarded. + // - It is safe to try and read entries at any index strictly less than + // `BufferRangeEnd()` -- but note that these reads may fail by the time you + // request them, as old entries get overwritten by new ones. + uint64_t BufferRangeStart() const { return mEntries.GetState().mRangeStart; } + uint64_t BufferRangeEnd() const { return mEntries.GetState().mRangeEnd; } + + private: + // Single pre-allocated chunk (to avoid spurious mallocs), used when: + // - Duplicating sleeping stacks (hence scExpectedMaximumStackSize). + // - Adding JIT info. + // - Streaming stacks to JSON. + // Mutable because it's accessed from non-multithreaded const methods. + mutable Maybe<ProfileBufferChunkManagerSingle> mMaybeWorkerChunkManager; + ProfileBufferChunkManagerSingle& WorkerChunkManager() const { + if (mMaybeWorkerChunkManager.isNothing()) { + // Only actually allocate it on first use. (Some ProfileBuffers are + // temporary and don't actually need this.) + mMaybeWorkerChunkManager.emplace( + ProfileBufferChunk::SizeofChunkMetadata() + + ProfileBufferChunkManager::scExpectedMaximumStackSize); + } + return *mMaybeWorkerChunkManager; + } + + // Time from launch (us) when first sampling was recorded. + double mFirstSamplingTimeUs = 0.0; + // Time from launch (us) when last sampling was recorded. + double mLastSamplingTimeUs = 0.0; + // Sampling stats: Interval (us) between successive samplings. + ProfilerStats mIntervalsUs; + // Sampling stats: Total duration (us) of each sampling. (Split detail below.) + ProfilerStats mOverheadsUs; + // Sampling stats: Time (us) to acquire the lock before sampling. + ProfilerStats mLockingsUs; + // Sampling stats: Time (us) to discard expired data. + ProfilerStats mCleaningsUs; + // Sampling stats: Time (us) to collect counter data. + ProfilerStats mCountersUs; + // Sampling stats: Time (us) to sample thread stacks. + ProfilerStats mThreadsUs; +}; + +/** + * Helper type used to implement ProfilerStackCollector. This type is used as + * the collector for MergeStacks by ProfileBuffer. It holds a reference to the + * buffer, as well as additional feature flags which are needed to control the + * data collection strategy + */ +class ProfileBufferCollector final : public ProfilerStackCollector { + public: + ProfileBufferCollector(ProfileBuffer& aBuf, uint64_t aSamplePos, + uint64_t aBufferRangeStart) + : mBuf(aBuf), + mSamplePositionInBuffer(aSamplePos), + mBufferRangeStart(aBufferRangeStart) { + MOZ_ASSERT( + mSamplePositionInBuffer >= mBufferRangeStart, + "The sample position should always be after the buffer range start"); + } + + // Position at which the sample starts in the profiler buffer (which may be + // different from the buffer in which the sample data is collected here). + Maybe<uint64_t> SamplePositionInBuffer() override { + return Some(mSamplePositionInBuffer); + } + + // Profiler buffer's range start (which may be different from the buffer in + // which the sample data is collected here). + Maybe<uint64_t> BufferRangeStart() override { + return Some(mBufferRangeStart); + } + + virtual void CollectNativeLeafAddr(void* aAddr) override; + virtual void CollectProfilingStackFrame( + const ProfilingStackFrame& aFrame) override; + + private: + ProfileBuffer& mBuf; + uint64_t mSamplePositionInBuffer; + uint64_t mBufferRangeStart; +}; + +} // namespace baseprofiler +} // namespace mozilla + +#endif diff --git a/mozglue/baseprofiler/core/ProfileBufferEntry.cpp b/mozglue/baseprofiler/core/ProfileBufferEntry.cpp new file mode 100644 index 0000000000..faa3826171 --- /dev/null +++ b/mozglue/baseprofiler/core/ProfileBufferEntry.cpp @@ -0,0 +1,1367 @@ +/* -*- 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 "ProfileBufferEntry.h" + +#include <ostream> +#include <type_traits> + +#include "mozilla/Logging.h" +#include "mozilla/ScopeExit.h" +#include "mozilla/Sprintf.h" +#include "mozilla/StackWalk.h" + +#include "BaseProfiler.h" +#include "mozilla/BaseProfilerMarkers.h" +#include "platform.h" +#include "ProfileBuffer.h" +#include "ProfilerBacktrace.h" + +namespace mozilla { +namespace baseprofiler { + +//////////////////////////////////////////////////////////////////////// +// BEGIN ProfileBufferEntry + +ProfileBufferEntry::ProfileBufferEntry() + : mKind(Kind::INVALID), mStorage{0, 0, 0, 0, 0, 0, 0, 0} {} + +// aString must be a static string. +ProfileBufferEntry::ProfileBufferEntry(Kind aKind, const char* aString) + : mKind(aKind) { + memcpy(mStorage, &aString, sizeof(aString)); +} + +ProfileBufferEntry::ProfileBufferEntry(Kind aKind, char aChars[kNumChars]) + : mKind(aKind) { + memcpy(mStorage, aChars, kNumChars); +} + +ProfileBufferEntry::ProfileBufferEntry(Kind aKind, void* aPtr) : mKind(aKind) { + memcpy(mStorage, &aPtr, sizeof(aPtr)); +} + +ProfileBufferEntry::ProfileBufferEntry(Kind aKind, double aDouble) + : mKind(aKind) { + memcpy(mStorage, &aDouble, sizeof(aDouble)); +} + +ProfileBufferEntry::ProfileBufferEntry(Kind aKind, int aInt) : mKind(aKind) { + memcpy(mStorage, &aInt, sizeof(aInt)); +} + +ProfileBufferEntry::ProfileBufferEntry(Kind aKind, int64_t aInt64) + : mKind(aKind) { + memcpy(mStorage, &aInt64, sizeof(aInt64)); +} + +ProfileBufferEntry::ProfileBufferEntry(Kind aKind, uint64_t aUint64) + : mKind(aKind) { + memcpy(mStorage, &aUint64, sizeof(aUint64)); +} + +ProfileBufferEntry::ProfileBufferEntry(Kind aKind, + BaseProfilerThreadId aThreadId) + : mKind(aKind) { + static_assert(std::is_trivially_copyable_v<BaseProfilerThreadId>); + static_assert(sizeof(aThreadId) <= sizeof(mStorage)); + memcpy(mStorage, &aThreadId, sizeof(aThreadId)); +} + +const char* ProfileBufferEntry::GetString() const { + const char* result; + memcpy(&result, mStorage, sizeof(result)); + return result; +} + +void* ProfileBufferEntry::GetPtr() const { + void* result; + memcpy(&result, mStorage, sizeof(result)); + return result; +} + +double ProfileBufferEntry::GetDouble() const { + double result; + memcpy(&result, mStorage, sizeof(result)); + return result; +} + +int ProfileBufferEntry::GetInt() const { + int result; + memcpy(&result, mStorage, sizeof(result)); + return result; +} + +int64_t ProfileBufferEntry::GetInt64() const { + int64_t result; + memcpy(&result, mStorage, sizeof(result)); + return result; +} + +uint64_t ProfileBufferEntry::GetUint64() const { + uint64_t result; + memcpy(&result, mStorage, sizeof(result)); + return result; +} + +BaseProfilerThreadId ProfileBufferEntry::GetThreadId() const { + BaseProfilerThreadId result; + static_assert(std::is_trivially_copyable_v<BaseProfilerThreadId>); + memcpy(&result, mStorage, sizeof(result)); + return result; +} + +void ProfileBufferEntry::CopyCharsInto(char (&aOutArray)[kNumChars]) const { + memcpy(aOutArray, mStorage, kNumChars); +} + +// END ProfileBufferEntry +//////////////////////////////////////////////////////////////////////// + +// As mentioned in ProfileBufferEntry.h, the JSON format contains many +// arrays whose elements are laid out according to various schemas to help +// de-duplication. This RAII class helps write these arrays by keeping track of +// the last non-null element written and adding the appropriate number of null +// elements when writing new non-null elements. It also automatically opens and +// closes an array element on the given JSON writer. +// +// You grant the AutoArraySchemaWriter exclusive access to the JSONWriter and +// the UniqueJSONStrings objects for the lifetime of AutoArraySchemaWriter. Do +// not access them independently while the AutoArraySchemaWriter is alive. +// If you need to add complex objects, call FreeFormElement(), which will give +// you temporary access to the writer. +// +// Example usage: +// +// // Define the schema of elements in this type of array: [FOO, BAR, BAZ] +// enum Schema : uint32_t { +// FOO = 0, +// BAR = 1, +// BAZ = 2 +// }; +// +// AutoArraySchemaWriter writer(someJsonWriter, someUniqueStrings); +// if (shouldWriteFoo) { +// writer.IntElement(FOO, getFoo()); +// } +// ... etc ... +// +// The elements need to be added in-order. +class MOZ_RAII AutoArraySchemaWriter { + public: + explicit AutoArraySchemaWriter(SpliceableJSONWriter& aWriter) + : mJSONWriter(aWriter), mNextFreeIndex(0) { + mJSONWriter.StartArrayElement(); + } + + ~AutoArraySchemaWriter() { mJSONWriter.EndArray(); } + + template <typename T> + void IntElement(uint32_t aIndex, T aValue) { + static_assert(!std::is_same_v<T, uint64_t>, + "Narrowing uint64 -> int64 conversion not allowed"); + FillUpTo(aIndex); + mJSONWriter.IntElement(static_cast<int64_t>(aValue)); + } + + void DoubleElement(uint32_t aIndex, double aValue) { + FillUpTo(aIndex); + mJSONWriter.DoubleElement(aValue); + } + + void TimeMsElement(uint32_t aIndex, double aTime_ms) { + FillUpTo(aIndex); + mJSONWriter.TimeDoubleMsElement(aTime_ms); + } + + void BoolElement(uint32_t aIndex, bool aValue) { + FillUpTo(aIndex); + mJSONWriter.BoolElement(aValue); + } + + protected: + SpliceableJSONWriter& Writer() { return mJSONWriter; } + + void FillUpTo(uint32_t aIndex) { + MOZ_ASSERT(aIndex >= mNextFreeIndex); + mJSONWriter.NullElements(aIndex - mNextFreeIndex); + mNextFreeIndex = aIndex + 1; + } + + private: + SpliceableJSONWriter& mJSONWriter; + uint32_t mNextFreeIndex; +}; + +// Same as AutoArraySchemaWriter, but this can also write strings (output as +// indexes into the table of unique strings). +class MOZ_RAII AutoArraySchemaWithStringsWriter : public AutoArraySchemaWriter { + public: + AutoArraySchemaWithStringsWriter(SpliceableJSONWriter& aWriter, + UniqueJSONStrings& aStrings) + : AutoArraySchemaWriter(aWriter), mStrings(aStrings) {} + + void StringElement(uint32_t aIndex, const Span<const char>& aValue) { + FillUpTo(aIndex); + mStrings.WriteElement(Writer(), aValue); + } + + private: + UniqueJSONStrings& mStrings; +}; + +UniqueStacks::StackKey UniqueStacks::BeginStack(const FrameKey& aFrame) { + return StackKey(GetOrAddFrameIndex(aFrame)); +} + +UniqueStacks::StackKey UniqueStacks::AppendFrame(const StackKey& aStack, + const FrameKey& aFrame) { + return StackKey(aStack, GetOrAddStackIndex(aStack), + GetOrAddFrameIndex(aFrame)); +} + +bool UniqueStacks::FrameKey::NormalFrameData::operator==( + const NormalFrameData& aOther) const { + return mLocation == aOther.mLocation && + mRelevantForJS == aOther.mRelevantForJS && + mInnerWindowID == aOther.mInnerWindowID && mLine == aOther.mLine && + mColumn == aOther.mColumn && mCategoryPair == aOther.mCategoryPair; +} + +UniqueStacks::UniqueStacks() + : mUniqueStrings(MakeUnique<UniqueJSONStrings>( + FailureLatchInfallibleSource::Singleton())), + mFrameTableWriter(FailureLatchInfallibleSource::Singleton()), + mStackTableWriter(FailureLatchInfallibleSource::Singleton()) { + mFrameTableWriter.StartBareList(); + mStackTableWriter.StartBareList(); +} + +uint32_t UniqueStacks::GetOrAddStackIndex(const StackKey& aStack) { + uint32_t count = mStackToIndexMap.count(); + auto entry = mStackToIndexMap.lookupForAdd(aStack); + if (entry) { + MOZ_ASSERT(entry->value() < count); + return entry->value(); + } + + MOZ_RELEASE_ASSERT(mStackToIndexMap.add(entry, aStack, count)); + StreamStack(aStack); + return count; +} + +uint32_t UniqueStacks::GetOrAddFrameIndex(const FrameKey& aFrame) { + uint32_t count = mFrameToIndexMap.count(); + auto entry = mFrameToIndexMap.lookupForAdd(aFrame); + if (entry) { + MOZ_ASSERT(entry->value() < count); + return entry->value(); + } + + MOZ_RELEASE_ASSERT(mFrameToIndexMap.add(entry, aFrame, count)); + StreamNonJITFrame(aFrame); + return count; +} + +void UniqueStacks::SpliceFrameTableElements(SpliceableJSONWriter& aWriter) { + mFrameTableWriter.EndBareList(); + aWriter.TakeAndSplice(mFrameTableWriter.TakeChunkedWriteFunc()); +} + +void UniqueStacks::SpliceStackTableElements(SpliceableJSONWriter& aWriter) { + mStackTableWriter.EndBareList(); + aWriter.TakeAndSplice(mStackTableWriter.TakeChunkedWriteFunc()); +} + +void UniqueStacks::StreamStack(const StackKey& aStack) { + enum Schema : uint32_t { PREFIX = 0, FRAME = 1 }; + + AutoArraySchemaWriter writer(mStackTableWriter); + if (aStack.mPrefixStackIndex.isSome()) { + writer.IntElement(PREFIX, *aStack.mPrefixStackIndex); + } + writer.IntElement(FRAME, aStack.mFrameIndex); +} + +void UniqueStacks::StreamNonJITFrame(const FrameKey& aFrame) { + using NormalFrameData = FrameKey::NormalFrameData; + + enum Schema : uint32_t { + LOCATION = 0, + RELEVANT_FOR_JS = 1, + INNER_WINDOW_ID = 2, + IMPLEMENTATION = 3, + LINE = 4, + COLUMN = 5, + CATEGORY = 6, + SUBCATEGORY = 7 + }; + + AutoArraySchemaWithStringsWriter writer(mFrameTableWriter, *mUniqueStrings); + + const NormalFrameData& data = aFrame.mData.as<NormalFrameData>(); + writer.StringElement(LOCATION, data.mLocation); + writer.BoolElement(RELEVANT_FOR_JS, data.mRelevantForJS); + + // It's okay to convert uint64_t to double here because DOM always creates IDs + // that are convertible to double. + writer.DoubleElement(INNER_WINDOW_ID, data.mInnerWindowID); + + if (data.mLine.isSome()) { + writer.IntElement(LINE, *data.mLine); + } + if (data.mColumn.isSome()) { + writer.IntElement(COLUMN, *data.mColumn); + } + if (data.mCategoryPair.isSome()) { + const ProfilingCategoryPairInfo& info = + GetProfilingCategoryPairInfo(*data.mCategoryPair); + writer.IntElement(CATEGORY, uint32_t(info.mCategory)); + writer.IntElement(SUBCATEGORY, info.mSubcategoryIndex); + } +} + +struct ProfileSample { + uint32_t mStack; + double mTime; + Maybe<double> mResponsiveness; +}; + +static void WriteSample(SpliceableJSONWriter& aWriter, + const ProfileSample& aSample) { + enum Schema : uint32_t { + STACK = 0, + TIME = 1, + EVENT_DELAY = 2, + }; + + AutoArraySchemaWriter writer(aWriter); + + writer.IntElement(STACK, aSample.mStack); + + writer.TimeMsElement(TIME, aSample.mTime); + + if (aSample.mResponsiveness.isSome()) { + writer.DoubleElement(EVENT_DELAY, *aSample.mResponsiveness); + } +} + +class EntryGetter { + public: + explicit EntryGetter(ProfileChunkedBuffer::Reader& aReader, + uint64_t aInitialReadPos = 0) + : mBlockIt( + aReader.At(ProfileBufferBlockIndex::CreateFromProfileBufferIndex( + aInitialReadPos))), + mBlockItEnd(aReader.end()) { + if (!ReadLegacyOrEnd()) { + // Find and read the next non-legacy entry. + Next(); + } + } + + bool Has() const { return mBlockIt != mBlockItEnd; } + + const ProfileBufferEntry& Get() const { + MOZ_ASSERT(Has(), "Caller should have checked `Has()` before `Get()`"); + return mEntry; + } + + void Next() { + MOZ_ASSERT(Has(), "Caller should have checked `Has()` before `Next()`"); + for (;;) { + ++mBlockIt; + if (ReadLegacyOrEnd()) { + // Either we're at the end, or we could read a legacy entry -> Done. + break; + } + // Otherwise loop around until we hit the end or a legacy entry. + } + } + + ProfileBufferBlockIndex CurBlockIndex() const { + return mBlockIt.CurrentBlockIndex(); + } + + uint64_t CurPos() const { + return CurBlockIndex().ConvertToProfileBufferIndex(); + } + + private: + // Try to read the entry at the current `mBlockIt` position. + // * If we're at the end of the buffer, just return `true`. + // * If there is a "legacy" entry (containing a real `ProfileBufferEntry`), + // read it into `mEntry`, and return `true` as well. + // * Otherwise the entry contains a "modern" type that cannot be read into + // `mEntry`, return `false` (so `EntryGetter` can skip to another entry). + bool ReadLegacyOrEnd() { + if (!Has()) { + return true; + } + // Read the entry "kind", which is always at the start of all entries. + ProfileBufferEntryReader aER = *mBlockIt; + auto type = static_cast<ProfileBufferEntry::Kind>( + aER.ReadObject<ProfileBufferEntry::KindUnderlyingType>()); + MOZ_ASSERT(static_cast<ProfileBufferEntry::KindUnderlyingType>(type) < + static_cast<ProfileBufferEntry::KindUnderlyingType>( + ProfileBufferEntry::Kind::MODERN_LIMIT)); + if (type >= ProfileBufferEntry::Kind::LEGACY_LIMIT) { + aER.SetRemainingBytes(0); + return false; + } + // Here, we have a legacy item, we need to read it from the start. + // Because the above `ReadObject` moved the reader, we ned to reset it to + // the start of the entry before reading the whole entry. + aER = *mBlockIt; + aER.ReadBytes(&mEntry, aER.RemainingBytes()); + return true; + } + + ProfileBufferEntry mEntry; + ProfileChunkedBuffer::BlockIterator mBlockIt; + const ProfileChunkedBuffer::BlockIterator mBlockItEnd; +}; + +// The following grammar shows legal sequences of profile buffer entries. +// The sequences beginning with a ThreadId entry are known as "samples". +// +// ( +// ( /* Samples */ +// ThreadId +// Time +// ( NativeLeafAddr +// | Label FrameFlags? DynamicStringFragment* LineNumber? CategoryPair? +// | JitReturnAddr +// )+ +// Responsiveness? +// ) +// | MarkerData +// | ( /* Counters */ +// CounterId +// Time +// ( +// CounterKey +// Count +// Number? +// )* +// ) +// | CollectionStart +// | CollectionEnd +// | Pause +// | Resume +// | ( ProfilerOverheadTime /* Sampling start timestamp */ +// ProfilerOverheadDuration /* Lock acquisition */ +// ProfilerOverheadDuration /* Expired data cleaning */ +// ProfilerOverheadDuration /* Counters */ +// ProfilerOverheadDuration /* Threads */ +// ) +// )* +// +// The most complicated part is the stack entry sequence that begins with +// Label. Here are some examples. +// +// - ProfilingStack frames without a dynamic string: +// +// Label("js::RunScript") +// CategoryPair(ProfilingCategoryPair::JS) +// +// Label("XREMain::XRE_main") +// LineNumber(4660) +// CategoryPair(ProfilingCategoryPair::OTHER) +// +// Label("ElementRestyler::ComputeStyleChangeFor") +// LineNumber(3003) +// CategoryPair(ProfilingCategoryPair::CSS) +// +// - ProfilingStack frames with a dynamic string: +// +// Label("nsObserverService::NotifyObservers") +// FrameFlags(uint64_t(ProfilingStackFrame::Flags::IS_LABEL_FRAME)) +// DynamicStringFragment("domwindo") +// DynamicStringFragment("wopened") +// LineNumber(291) +// CategoryPair(ProfilingCategoryPair::OTHER) +// +// Label("") +// FrameFlags(uint64_t(ProfilingStackFrame::Flags::IS_JS_FRAME)) +// DynamicStringFragment("closeWin") +// DynamicStringFragment("dow (chr") +// DynamicStringFragment("ome://gl") +// DynamicStringFragment("obal/con") +// DynamicStringFragment("tent/glo") +// DynamicStringFragment("balOverl") +// DynamicStringFragment("ay.js:5)") +// DynamicStringFragment("") # this string holds the closing '\0' +// LineNumber(25) +// CategoryPair(ProfilingCategoryPair::JS) +// +// Label("") +// FrameFlags(uint64_t(ProfilingStackFrame::Flags::IS_JS_FRAME)) +// DynamicStringFragment("bound (s") +// DynamicStringFragment("elf-host") +// DynamicStringFragment("ed:914)") +// LineNumber(945) +// CategoryPair(ProfilingCategoryPair::JS) +// +// - A profiling stack frame with an overly long dynamic string: +// +// Label("") +// FrameFlags(uint64_t(ProfilingStackFrame::Flags::IS_LABEL_FRAME)) +// DynamicStringFragment("(too lon") +// DynamicStringFragment("g)") +// LineNumber(100) +// CategoryPair(ProfilingCategoryPair::NETWORK) +// +// - A wasm JIT frame: +// +// Label("") +// FrameFlags(uint64_t(0)) +// DynamicStringFragment("wasm-fun") +// DynamicStringFragment("ction[87") +// DynamicStringFragment("36] (blo") +// DynamicStringFragment("b:http:/") +// DynamicStringFragment("/webasse") +// DynamicStringFragment("mbly.org") +// DynamicStringFragment("/3dc5759") +// DynamicStringFragment("4-ce58-4") +// DynamicStringFragment("626-975b") +// DynamicStringFragment("-08ad116") +// DynamicStringFragment("30bc1:38") +// DynamicStringFragment("29856)") +// +// - A JS frame in a synchronous sample: +// +// Label("") +// FrameFlags(uint64_t(ProfilingStackFrame::Flags::IS_LABEL_FRAME)) +// DynamicStringFragment("u (https") +// DynamicStringFragment("://perf-") +// DynamicStringFragment("html.io/") +// DynamicStringFragment("ac0da204") +// DynamicStringFragment("aaa44d75") +// DynamicStringFragment("a800.bun") +// DynamicStringFragment("dle.js:2") +// DynamicStringFragment("5)") + +// Because this is a format entirely internal to the Profiler, any parsing +// error indicates a bug in the ProfileBuffer writing or the parser itself, +// or possibly flaky hardware. +#define ERROR_AND_CONTINUE(msg) \ + { \ + fprintf(stderr, "ProfileBuffer parse error: %s", msg); \ + MOZ_ASSERT(false, msg); \ + continue; \ + } + +BaseProfilerThreadId ProfileBuffer::StreamSamplesToJSON( + SpliceableJSONWriter& aWriter, BaseProfilerThreadId aThreadId, + double aSinceTime, UniqueStacks& aUniqueStacks) const { + UniquePtr<char[]> dynStrBuf = MakeUnique<char[]>(kMaxFrameKeyLength); + + return mEntries.Read([&](ProfileChunkedBuffer::Reader* aReader) { + MOZ_ASSERT(aReader, + "ProfileChunkedBuffer cannot be out-of-session when sampler is " + "running"); + + BaseProfilerThreadId processedThreadId; + + EntryGetter e(*aReader); + + for (;;) { + // This block skips entries until we find the start of the next sample. + // This is useful in three situations. + // + // - The circular buffer overwrites old entries, so when we start parsing + // we might be in the middle of a sample, and we must skip forward to + // the start of the next sample. + // + // - We skip samples that don't have an appropriate ThreadId or Time. + // + // - We skip range Pause, Resume, CollectionStart, Counter and + // CollectionEnd entries between samples. + while (e.Has()) { + if (e.Get().IsThreadId()) { + break; + } + e.Next(); + } + + if (!e.Has()) { + break; + } + + // Due to the skip_to_next_sample block above, if we have an entry here it + // must be a ThreadId entry. + MOZ_ASSERT(e.Get().IsThreadId()); + + BaseProfilerThreadId threadId = e.Get().GetThreadId(); + e.Next(); + + // Ignore samples that are for the wrong thread. + if (threadId != aThreadId && aThreadId.IsSpecified()) { + continue; + } + + MOZ_ASSERT( + aThreadId.IsSpecified() || !processedThreadId.IsSpecified(), + "Unspecified aThreadId should only be used with 1-sample buffer"); + + ProfileSample sample; + + if (e.Has() && e.Get().IsTime()) { + sample.mTime = e.Get().GetDouble(); + e.Next(); + + // Ignore samples that are too old. + if (sample.mTime < aSinceTime) { + continue; + } + } else { + ERROR_AND_CONTINUE("expected a Time entry"); + } + + UniqueStacks::StackKey stack = + aUniqueStacks.BeginStack(UniqueStacks::FrameKey("(root)")); + + int numFrames = 0; + while (e.Has()) { + if (e.Get().IsNativeLeafAddr()) { + numFrames++; + + void* pc = e.Get().GetPtr(); + e.Next(); + + static const uint32_t BUF_SIZE = 256; + char buf[BUF_SIZE]; + + // Bug 753041: We need a double cast here to tell GCC that we don't + // want to sign extend 32-bit addresses starting with 0xFXXXXXX. + unsigned long long pcULL = (unsigned long long)(uintptr_t)pc; + SprintfLiteral(buf, "0x%llx", pcULL); + + // If the "MOZ_PROFILER_SYMBOLICATE" env-var is set, we add a local + // symbolication description to the PC address. This is off by + // default, and mainly intended for local development. + static const bool preSymbolicate = []() { + const char* symbolicate = getenv("MOZ_PROFILER_SYMBOLICATE"); + return symbolicate && symbolicate[0] != '\0'; + }(); + if (preSymbolicate) { + MozCodeAddressDetails details; + if (MozDescribeCodeAddress(pc, &details)) { + // Replace \0 terminator with space. + const uint32_t pcLen = strlen(buf); + buf[pcLen] = ' '; + // Add description after space. Note: Using a frame number of 0, + // as using `numFrames` wouldn't help here, and would prevent + // combining same function calls that happen at different depths. + // TODO: Remove unsightly "#00: " if too annoying. :-) + MozFormatCodeAddressDetails( + buf + pcLen + 1, BUF_SIZE - (pcLen + 1), 0, pc, &details); + } + } + + stack = aUniqueStacks.AppendFrame(stack, UniqueStacks::FrameKey(buf)); + + } else if (e.Get().IsLabel()) { + numFrames++; + + const char* label = e.Get().GetString(); + e.Next(); + + using FrameFlags = ProfilingStackFrame::Flags; + uint32_t frameFlags = 0; + if (e.Has() && e.Get().IsFrameFlags()) { + frameFlags = uint32_t(e.Get().GetUint64()); + e.Next(); + } + + bool relevantForJS = + frameFlags & uint32_t(FrameFlags::RELEVANT_FOR_JS); + + // Copy potential dynamic string fragments into dynStrBuf, so that + // dynStrBuf will then contain the entire dynamic string. + size_t i = 0; + dynStrBuf[0] = '\0'; + while (e.Has()) { + if (e.Get().IsDynamicStringFragment()) { + char chars[ProfileBufferEntry::kNumChars]; + e.Get().CopyCharsInto(chars); + for (char c : chars) { + if (i < kMaxFrameKeyLength) { + dynStrBuf[i] = c; + i++; + } + } + e.Next(); + } else { + break; + } + } + dynStrBuf[kMaxFrameKeyLength - 1] = '\0'; + bool hasDynamicString = (i != 0); + + std::string frameLabel; + if (label[0] != '\0' && hasDynamicString) { + if (frameFlags & uint32_t(FrameFlags::STRING_TEMPLATE_METHOD)) { + frameLabel += label; + frameLabel += '.'; + frameLabel += dynStrBuf.get(); + } else if (frameFlags & + uint32_t(FrameFlags::STRING_TEMPLATE_GETTER)) { + frameLabel += "get "; + frameLabel += label; + frameLabel += '.'; + frameLabel += dynStrBuf.get(); + } else if (frameFlags & + uint32_t(FrameFlags::STRING_TEMPLATE_SETTER)) { + frameLabel += "set "; + frameLabel += label; + frameLabel += '.'; + frameLabel += dynStrBuf.get(); + } else { + frameLabel += label; + frameLabel += ' '; + frameLabel += dynStrBuf.get(); + } + } else if (hasDynamicString) { + frameLabel += dynStrBuf.get(); + } else { + frameLabel += label; + } + + uint64_t innerWindowID = 0; + if (e.Has() && e.Get().IsInnerWindowID()) { + innerWindowID = uint64_t(e.Get().GetUint64()); + e.Next(); + } + + Maybe<unsigned> line; + if (e.Has() && e.Get().IsLineNumber()) { + line = Some(unsigned(e.Get().GetInt())); + e.Next(); + } + + Maybe<unsigned> column; + if (e.Has() && e.Get().IsColumnNumber()) { + column = Some(unsigned(e.Get().GetInt())); + e.Next(); + } + + Maybe<ProfilingCategoryPair> categoryPair; + if (e.Has() && e.Get().IsCategoryPair()) { + categoryPair = + Some(ProfilingCategoryPair(uint32_t(e.Get().GetInt()))); + e.Next(); + } + + stack = aUniqueStacks.AppendFrame( + stack, UniqueStacks::FrameKey(std::move(frameLabel), + relevantForJS, innerWindowID, line, + column, categoryPair)); + + } else { + break; + } + } + + if (numFrames == 0) { + // It is possible to have empty stacks if native stackwalking is + // disabled. Skip samples with empty stacks. (See Bug 1497985). + // Thus, don't use ERROR_AND_CONTINUE, but just continue. + continue; + } + + sample.mStack = aUniqueStacks.GetOrAddStackIndex(stack); + + if (e.Has() && e.Get().IsResponsiveness()) { + sample.mResponsiveness = Some(e.Get().GetDouble()); + e.Next(); + } + + WriteSample(aWriter, sample); + + processedThreadId = threadId; + } + + return processedThreadId; + }); +} + +void ProfileBuffer::StreamMarkersToJSON(SpliceableJSONWriter& aWriter, + BaseProfilerThreadId aThreadId, + const TimeStamp& aProcessStartTime, + double aSinceTime, + UniqueStacks& aUniqueStacks) const { + mEntries.ReadEach([&](ProfileBufferEntryReader& aER) { + auto type = static_cast<ProfileBufferEntry::Kind>( + aER.ReadObject<ProfileBufferEntry::KindUnderlyingType>()); + MOZ_ASSERT(static_cast<ProfileBufferEntry::KindUnderlyingType>(type) < + static_cast<ProfileBufferEntry::KindUnderlyingType>( + ProfileBufferEntry::Kind::MODERN_LIMIT)); + if (type == ProfileBufferEntry::Kind::Marker) { + ::mozilla::base_profiler_markers_detail::DeserializeAfterKindAndStream( + aER, + [&](const BaseProfilerThreadId& aMarkerThreadId) { + return (aMarkerThreadId == aThreadId) ? &aWriter : nullptr; + }, + [&](ProfileChunkedBuffer& aChunkedBuffer) { + ProfilerBacktrace backtrace("", &aChunkedBuffer); + backtrace.StreamJSON(aWriter, TimeStamp::ProcessCreation(), + aUniqueStacks); + }, + // We don't have Rust markers in the mozglue. + [&](mozilla::base_profiler_markers_detail::Streaming:: + DeserializerTag) { + MOZ_ASSERT_UNREACHABLE("No Rust markers in mozglue."); + }); + } else { + // The entry was not a marker, we need to skip to the end. + aER.SetRemainingBytes(0); + } + }); +} + +void ProfileBuffer::StreamProfilerOverheadToJSON( + SpliceableJSONWriter& aWriter, const TimeStamp& aProcessStartTime, + double aSinceTime) const { + mEntries.Read([&](ProfileChunkedBuffer::Reader* aReader) { + MOZ_ASSERT(aReader, + "ProfileChunkedBuffer cannot be out-of-session when sampler is " + "running"); + + EntryGetter e(*aReader); + + enum Schema : uint32_t { + TIME = 0, + LOCKING = 1, + MARKER_CLEANING = 2, + COUNTERS = 3, + THREADS = 4 + }; + + aWriter.StartObjectProperty("profilerOverhead"); + aWriter.StartObjectProperty("samples"); + // Stream all sampling overhead data. We skip other entries, because we + // process them in StreamSamplesToJSON()/etc. + { + JSONSchemaWriter schema(aWriter); + schema.WriteField("time"); + schema.WriteField("locking"); + schema.WriteField("expiredMarkerCleaning"); + schema.WriteField("counters"); + schema.WriteField("threads"); + } + + aWriter.StartArrayProperty("data"); + double firstTime = 0.0; + double lastTime = 0.0; + ProfilerStats intervals, overheads, lockings, cleanings, counters, threads; + while (e.Has()) { + // valid sequence: ProfilerOverheadTime, ProfilerOverheadDuration * 4 + if (e.Get().IsProfilerOverheadTime()) { + double time = e.Get().GetDouble(); + if (time >= aSinceTime) { + e.Next(); + if (!e.Has() || !e.Get().IsProfilerOverheadDuration()) { + ERROR_AND_CONTINUE( + "expected a ProfilerOverheadDuration entry after " + "ProfilerOverheadTime"); + } + double locking = e.Get().GetDouble(); + e.Next(); + if (!e.Has() || !e.Get().IsProfilerOverheadDuration()) { + ERROR_AND_CONTINUE( + "expected a ProfilerOverheadDuration entry after " + "ProfilerOverheadTime,ProfilerOverheadDuration"); + } + double cleaning = e.Get().GetDouble(); + e.Next(); + if (!e.Has() || !e.Get().IsProfilerOverheadDuration()) { + ERROR_AND_CONTINUE( + "expected a ProfilerOverheadDuration entry after " + "ProfilerOverheadTime,ProfilerOverheadDuration*2"); + } + double counter = e.Get().GetDouble(); + e.Next(); + if (!e.Has() || !e.Get().IsProfilerOverheadDuration()) { + ERROR_AND_CONTINUE( + "expected a ProfilerOverheadDuration entry after " + "ProfilerOverheadTime,ProfilerOverheadDuration*3"); + } + double thread = e.Get().GetDouble(); + + if (firstTime == 0.0) { + firstTime = time; + } else { + // Note that we'll have 1 fewer interval than other numbers (because + // we need both ends of an interval to know its duration). The final + // difference should be insignificant over the expected many + // thousands of iterations. + intervals.Count(time - lastTime); + } + lastTime = time; + overheads.Count(locking + cleaning + counter + thread); + lockings.Count(locking); + cleanings.Count(cleaning); + counters.Count(counter); + threads.Count(thread); + + AutoArraySchemaWriter writer(aWriter); + writer.TimeMsElement(TIME, time); + writer.DoubleElement(LOCKING, locking); + writer.DoubleElement(MARKER_CLEANING, cleaning); + writer.DoubleElement(COUNTERS, counter); + writer.DoubleElement(THREADS, thread); + } + } + e.Next(); + } + aWriter.EndArray(); // data + aWriter.EndObject(); // samples + + // Only output statistics if there is at least one full interval (and + // therefore at least two samplings.) + if (intervals.n > 0) { + aWriter.StartObjectProperty("statistics"); + aWriter.DoubleProperty("profiledDuration", lastTime - firstTime); + aWriter.IntProperty("samplingCount", overheads.n); + aWriter.DoubleProperty("overheadDurations", overheads.sum); + aWriter.DoubleProperty("overheadPercentage", + overheads.sum / (lastTime - firstTime)); +#define PROFILER_STATS(name, var) \ + aWriter.DoubleProperty("mean" name, (var).sum / (var).n); \ + aWriter.DoubleProperty("min" name, (var).min); \ + aWriter.DoubleProperty("max" name, (var).max); + PROFILER_STATS("Interval", intervals); + PROFILER_STATS("Overhead", overheads); + PROFILER_STATS("Lockings", lockings); + PROFILER_STATS("Cleaning", cleanings); + PROFILER_STATS("Counter", counters); + PROFILER_STATS("Thread", threads); +#undef PROFILER_STATS + aWriter.EndObject(); // statistics + } + aWriter.EndObject(); // profilerOverhead + }); +} + +struct CounterKeyedSample { + double mTime; + uint64_t mNumber; + int64_t mCount; +}; + +using CounterKeyedSamples = Vector<CounterKeyedSample>; + +using CounterMap = HashMap<uint64_t, CounterKeyedSamples>; + +// HashMap lookup, if not found, a default value is inserted. +// Returns reference to (existing or new) value inside the HashMap. +template <typename HashM, typename Key> +static auto& LookupOrAdd(HashM& aMap, Key&& aKey) { + auto addPtr = aMap.lookupForAdd(aKey); + if (!addPtr) { + MOZ_RELEASE_ASSERT(aMap.add(addPtr, std::forward<Key>(aKey), + typename HashM::Entry::ValueType{})); + MOZ_ASSERT(!!addPtr); + } + return addPtr->value(); +} + +void ProfileBuffer::StreamCountersToJSON(SpliceableJSONWriter& aWriter, + const TimeStamp& aProcessStartTime, + double aSinceTime) const { + // Because this is a format entirely internal to the Profiler, any parsing + // error indicates a bug in the ProfileBuffer writing or the parser itself, + // or possibly flaky hardware. + + mEntries.Read([&](ProfileChunkedBuffer::Reader* aReader) { + MOZ_ASSERT(aReader, + "ProfileChunkedBuffer cannot be out-of-session when sampler is " + "running"); + + EntryGetter e(*aReader); + + enum Schema : uint32_t { TIME = 0, COUNT = 1, NUMBER = 2 }; + + // Stream all counters. We skip other entries, because we process them in + // StreamSamplesToJSON()/etc. + // + // Valid sequence in the buffer: + // CounterID + // Time + // ( CounterKey Count Number? )* + // + // And the JSON (example): + // "counters": { + // "name": "malloc", + // "category": "Memory", + // "description": "Amount of allocated memory", + // "sample_groups": { + // "id": 0, + // "samples": { + // "schema": {"time": 0, "number": 1, "count": 2}, + // "data": [ + // [ + // 16117.033968000002, + // 2446216, + // 6801320 + // ], + // [ + // 16118.037638, + // 2446216, + // 6801320 + // ], + // ], + // } + // } + // }, + + // Build the map of counters and populate it + HashMap<void*, CounterMap> counters; + + while (e.Has()) { + // skip all non-Counters, including if we start in the middle of a counter + if (e.Get().IsCounterId()) { + void* id = e.Get().GetPtr(); + CounterMap& counter = LookupOrAdd(counters, id); + e.Next(); + if (!e.Has() || !e.Get().IsTime()) { + ERROR_AND_CONTINUE("expected a Time entry"); + } + double time = e.Get().GetDouble(); + e.Next(); + if (time >= aSinceTime) { + while (e.Has() && e.Get().IsCounterKey()) { + uint64_t key = e.Get().GetUint64(); + CounterKeyedSamples& data = LookupOrAdd(counter, key); + e.Next(); + if (!e.Has() || !e.Get().IsCount()) { + ERROR_AND_CONTINUE("expected a Count entry"); + } + int64_t count = e.Get().GetUint64(); + e.Next(); + uint64_t number; + if (!e.Has() || !e.Get().IsNumber()) { + number = 0; + } else { + number = e.Get().GetInt64(); + e.Next(); + } + CounterKeyedSample sample = {time, number, count}; + MOZ_RELEASE_ASSERT(data.append(sample)); + } + } else { + // skip counter sample - only need to skip the initial counter + // id, then let the loop at the top skip the rest + } + } else { + e.Next(); + } + } + // we have a map of a map of counter entries; dump them to JSON + if (counters.count() == 0) { + return; + } + + aWriter.StartArrayProperty("counters"); + for (auto iter = counters.iter(); !iter.done(); iter.next()) { + CounterMap& counter = iter.get().value(); + const BaseProfilerCount* base_counter = + static_cast<const BaseProfilerCount*>(iter.get().key()); + + aWriter.Start(); + aWriter.StringProperty("name", MakeStringSpan(base_counter->mLabel)); + aWriter.StringProperty("category", + MakeStringSpan(base_counter->mCategory)); + aWriter.StringProperty("description", + MakeStringSpan(base_counter->mDescription)); + + aWriter.StartArrayProperty("sample_groups"); + for (auto counter_iter = counter.iter(); !counter_iter.done(); + counter_iter.next()) { + CounterKeyedSamples& samples = counter_iter.get().value(); + uint64_t key = counter_iter.get().key(); + + size_t size = samples.length(); + if (size == 0) { + continue; + } + + bool hasNumber = false; + for (size_t i = 0; i < size; i++) { + if (samples[i].mNumber != 0) { + hasNumber = true; + break; + } + } + + aWriter.StartObjectElement(); + { + aWriter.IntProperty("id", static_cast<int64_t>(key)); + aWriter.StartObjectProperty("samples"); + { + JSONSchemaWriter schema(aWriter); + schema.WriteField("time"); + schema.WriteField("count"); + if (hasNumber) { + schema.WriteField("number"); + } + } + + aWriter.StartArrayProperty("data"); + uint64_t previousNumber = 0; + int64_t previousCount = 0; + for (size_t i = 0; i < size; i++) { + // Encode as deltas, and only encode if different than the last + // sample + if (i == 0 || samples[i].mNumber != previousNumber || + samples[i].mCount != previousCount) { + MOZ_ASSERT(i == 0 || samples[i].mTime >= samples[i - 1].mTime); + MOZ_ASSERT(samples[i].mNumber >= previousNumber); + MOZ_ASSERT(samples[i].mNumber - previousNumber <= + uint64_t(std::numeric_limits<int64_t>::max())); + + AutoArraySchemaWriter writer(aWriter); + writer.TimeMsElement(TIME, samples[i].mTime); + writer.IntElement(COUNT, samples[i].mCount - previousCount); + if (hasNumber) { + writer.IntElement( + NUMBER, + static_cast<int64_t>(samples[i].mNumber - previousNumber)); + } + previousNumber = samples[i].mNumber; + previousCount = samples[i].mCount; + } + } + aWriter.EndArray(); // data + aWriter.EndObject(); // samples + } + aWriter.EndObject(); // sample_groups item + } + aWriter.EndArray(); // sample groups + aWriter.End(); // for each counter + } + aWriter.EndArray(); // counters + }); +} + +#undef ERROR_AND_CONTINUE + +static void AddPausedRange(SpliceableJSONWriter& aWriter, const char* aReason, + const Maybe<double>& aStartTime, + const Maybe<double>& aEndTime) { + aWriter.Start(); + if (aStartTime) { + aWriter.TimeDoubleMsProperty("startTime", *aStartTime); + } else { + aWriter.NullProperty("startTime"); + } + if (aEndTime) { + aWriter.TimeDoubleMsProperty("endTime", *aEndTime); + } else { + aWriter.NullProperty("endTime"); + } + aWriter.StringProperty("reason", MakeStringSpan(aReason)); + aWriter.End(); +} + +void ProfileBuffer::StreamPausedRangesToJSON(SpliceableJSONWriter& aWriter, + double aSinceTime) const { + mEntries.Read([&](ProfileChunkedBuffer::Reader* aReader) { + MOZ_ASSERT(aReader, + "ProfileChunkedBuffer cannot be out-of-session when sampler is " + "running"); + + EntryGetter e(*aReader); + + Maybe<double> currentPauseStartTime; + Maybe<double> currentCollectionStartTime; + + while (e.Has()) { + if (e.Get().IsPause()) { + currentPauseStartTime = Some(e.Get().GetDouble()); + } else if (e.Get().IsResume()) { + AddPausedRange(aWriter, "profiler-paused", currentPauseStartTime, + Some(e.Get().GetDouble())); + currentPauseStartTime = Nothing(); + } else if (e.Get().IsCollectionStart()) { + currentCollectionStartTime = Some(e.Get().GetDouble()); + } else if (e.Get().IsCollectionEnd()) { + AddPausedRange(aWriter, "collecting", currentCollectionStartTime, + Some(e.Get().GetDouble())); + currentCollectionStartTime = Nothing(); + } + e.Next(); + } + + if (currentPauseStartTime) { + AddPausedRange(aWriter, "profiler-paused", currentPauseStartTime, + Nothing()); + } + if (currentCollectionStartTime) { + AddPausedRange(aWriter, "collecting", currentCollectionStartTime, + Nothing()); + } + }); +} + +bool ProfileBuffer::DuplicateLastSample(BaseProfilerThreadId aThreadId, + const TimeStamp& aProcessStartTime, + Maybe<uint64_t>& aLastSample) { + if (!aLastSample) { + return false; + } + + ProfileChunkedBuffer tempBuffer( + ProfileChunkedBuffer::ThreadSafety::WithoutMutex, WorkerChunkManager()); + + auto retrieveWorkerChunk = MakeScopeExit( + [&]() { WorkerChunkManager().Reset(tempBuffer.GetAllChunks()); }); + + const bool ok = mEntries.Read([&](ProfileChunkedBuffer::Reader* aReader) { + MOZ_ASSERT(aReader, + "ProfileChunkedBuffer cannot be out-of-session when sampler is " + "running"); + + EntryGetter e(*aReader, *aLastSample); + + if (e.CurPos() != *aLastSample) { + // The last sample is no longer within the buffer range, so we cannot + // use it. Reset the stored buffer position to Nothing(). + aLastSample.reset(); + return false; + } + + MOZ_RELEASE_ASSERT(e.Has() && e.Get().IsThreadId() && + e.Get().GetThreadId() == aThreadId); + + e.Next(); + + // Go through the whole entry and duplicate it, until we find the next + // one. + while (e.Has()) { + switch (e.Get().GetKind()) { + case ProfileBufferEntry::Kind::Pause: + case ProfileBufferEntry::Kind::Resume: + case ProfileBufferEntry::Kind::PauseSampling: + case ProfileBufferEntry::Kind::ResumeSampling: + case ProfileBufferEntry::Kind::CollectionStart: + case ProfileBufferEntry::Kind::CollectionEnd: + case ProfileBufferEntry::Kind::ThreadId: + // We're done. + return true; + case ProfileBufferEntry::Kind::Time: + // Copy with new time + AddEntry( + tempBuffer, + ProfileBufferEntry::Time( + (TimeStamp::Now() - aProcessStartTime).ToMilliseconds())); + break; + case ProfileBufferEntry::Kind::CounterKey: + case ProfileBufferEntry::Kind::Number: + case ProfileBufferEntry::Kind::Count: + case ProfileBufferEntry::Kind::Responsiveness: + // Don't copy anything not part of a thread's stack sample + break; + case ProfileBufferEntry::Kind::CounterId: + // CounterId is normally followed by Time - if so, we'd like + // to skip it. If we duplicate Time, it won't hurt anything, just + // waste buffer space (and this can happen if the CounterId has + // fallen off the end of the buffer, but Time (and Number/Count) + // are still in the buffer). + e.Next(); + if (e.Has() && e.Get().GetKind() != ProfileBufferEntry::Kind::Time) { + // this would only happen if there was an invalid sequence + // in the buffer. Don't skip it. + continue; + } + // we've skipped Time + break; + case ProfileBufferEntry::Kind::ProfilerOverheadTime: + // ProfilerOverheadTime is normally followed by + // ProfilerOverheadDuration*4 - if so, we'd like to skip it. Don't + // duplicate, as we are in the middle of a sampling and will soon + // capture its own overhead. + e.Next(); + // A missing Time would only happen if there was an invalid + // sequence in the buffer. Don't skip unexpected entry. + if (e.Has() && + e.Get().GetKind() != + ProfileBufferEntry::Kind::ProfilerOverheadDuration) { + continue; + } + e.Next(); + if (e.Has() && + e.Get().GetKind() != + ProfileBufferEntry::Kind::ProfilerOverheadDuration) { + continue; + } + e.Next(); + if (e.Has() && + e.Get().GetKind() != + ProfileBufferEntry::Kind::ProfilerOverheadDuration) { + continue; + } + e.Next(); + if (e.Has() && + e.Get().GetKind() != + ProfileBufferEntry::Kind::ProfilerOverheadDuration) { + continue; + } + // we've skipped ProfilerOverheadTime and + // ProfilerOverheadDuration*4. + break; + default: { + // Copy anything else we don't know about. + AddEntry(tempBuffer, e.Get()); + break; + } + } + e.Next(); + } + return true; + }); + + if (!ok) { + return false; + } + + // If the buffer was big enough, there won't be any cleared blocks. + if (tempBuffer.GetState().mClearedBlockCount != 0) { + // No need to try to read stack again as it won't fit. Reset the stored + // buffer position to Nothing(). + aLastSample.reset(); + return false; + } + + aLastSample = Some(AddThreadIdEntry(aThreadId)); + + tempBuffer.Read([&](ProfileChunkedBuffer::Reader* aReader) { + MOZ_ASSERT(aReader, "tempBuffer cannot be out-of-session"); + + EntryGetter e(*aReader); + + while (e.Has()) { + AddEntry(e.Get()); + e.Next(); + } + }); + + return true; +} + +void ProfileBuffer::DiscardSamplesBeforeTime(double aTime) { + // This function does nothing! + // The duration limit will be removed from Firefox, see bug 1632365. + Unused << aTime; +} + +// END ProfileBuffer +//////////////////////////////////////////////////////////////////////// + +} // namespace baseprofiler +} // namespace mozilla diff --git a/mozglue/baseprofiler/core/ProfileBufferEntry.h b/mozglue/baseprofiler/core/ProfileBufferEntry.h new file mode 100644 index 0000000000..ee6e401bd8 --- /dev/null +++ b/mozglue/baseprofiler/core/ProfileBufferEntry.h @@ -0,0 +1,364 @@ +/* -*- Mode: C++; tab-width: 2; 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/. */ + +#ifndef ProfileBufferEntry_h +#define ProfileBufferEntry_h + +#include "BaseProfilingCategory.h" +#include "gtest/MozGtestFriend.h" +#include "mozilla/BaseProfileJSONWriter.h" +#include "mozilla/HashFunctions.h" +#include "mozilla/HashTable.h" +#include "mozilla/Maybe.h" +#include "mozilla/ProfileBufferEntryKinds.h" +#include "mozilla/UniquePtr.h" +#include "mozilla/Variant.h" +#include "mozilla/Vector.h" + +#include <string> +#include <type_traits> + +namespace mozilla { +namespace baseprofiler { + +class ProfileBufferEntry { + public: + using KindUnderlyingType = + std::underlying_type_t<::mozilla::ProfileBufferEntryKind>; + using Kind = ::mozilla::ProfileBufferEntryKind; + + ProfileBufferEntry(); + + static constexpr size_t kNumChars = ::mozilla::ProfileBufferEntryNumChars; + + private: + // aString must be a static string. + ProfileBufferEntry(Kind aKind, const char* aString); + ProfileBufferEntry(Kind aKind, char aChars[kNumChars]); + ProfileBufferEntry(Kind aKind, void* aPtr); + ProfileBufferEntry(Kind aKind, double aDouble); + ProfileBufferEntry(Kind aKind, int64_t aInt64); + ProfileBufferEntry(Kind aKind, uint64_t aUint64); + ProfileBufferEntry(Kind aKind, int aInt); + ProfileBufferEntry(Kind aKind, BaseProfilerThreadId aThreadId); + + public: +#define CTOR(KIND, TYPE, SIZE) \ + static ProfileBufferEntry KIND(TYPE aVal) { \ + return ProfileBufferEntry(Kind::KIND, aVal); \ + } + FOR_EACH_PROFILE_BUFFER_ENTRY_KIND(CTOR) +#undef CTOR + + Kind GetKind() const { return mKind; } + +#define IS_KIND(KIND, TYPE, SIZE) \ + bool Is##KIND() const { return mKind == Kind::KIND; } + FOR_EACH_PROFILE_BUFFER_ENTRY_KIND(IS_KIND) +#undef IS_KIND + + private: + FRIEND_TEST(ThreadProfile, InsertOneEntry); + FRIEND_TEST(ThreadProfile, InsertOneEntryWithTinyBuffer); + FRIEND_TEST(ThreadProfile, InsertEntriesNoWrap); + FRIEND_TEST(ThreadProfile, InsertEntriesWrap); + FRIEND_TEST(ThreadProfile, MemoryMeasure); + friend class ProfileBuffer; + + Kind mKind; + uint8_t mStorage[kNumChars]; + + const char* GetString() const; + void* GetPtr() const; + double GetDouble() const; + int GetInt() const; + int64_t GetInt64() const; + uint64_t GetUint64() const; + BaseProfilerThreadId GetThreadId() const; + void CopyCharsInto(char (&aOutArray)[kNumChars]) const; +}; + +// Packed layout: 1 byte for the tag + 8 bytes for the value. +static_assert(sizeof(ProfileBufferEntry) == 9, "bad ProfileBufferEntry size"); + +class UniqueStacks { + public: + struct FrameKey { + explicit FrameKey(const char* aLocation) + : mData(NormalFrameData{std::string(aLocation), false, 0, Nothing(), + Nothing()}) {} + + FrameKey(std::string&& aLocation, bool aRelevantForJS, + uint64_t aInnerWindowID, const Maybe<unsigned>& aLine, + const Maybe<unsigned>& aColumn, + const Maybe<ProfilingCategoryPair>& aCategoryPair) + : mData(NormalFrameData{aLocation, aRelevantForJS, aInnerWindowID, + aLine, aColumn, aCategoryPair}) {} + + FrameKey(const FrameKey& aToCopy) = default; + + uint32_t Hash() const; + bool operator==(const FrameKey& aOther) const { + return mData == aOther.mData; + } + + struct NormalFrameData { + bool operator==(const NormalFrameData& aOther) const; + + std::string mLocation; + bool mRelevantForJS; + uint64_t mInnerWindowID; + Maybe<unsigned> mLine; + Maybe<unsigned> mColumn; + Maybe<ProfilingCategoryPair> mCategoryPair; + }; + Variant<NormalFrameData> mData; + }; + + struct FrameKeyHasher { + using Lookup = FrameKey; + + static HashNumber hash(const FrameKey& aLookup) { + HashNumber hash = 0; + if (aLookup.mData.is<FrameKey::NormalFrameData>()) { + const FrameKey::NormalFrameData& data = + aLookup.mData.as<FrameKey::NormalFrameData>(); + if (!data.mLocation.empty()) { + hash = AddToHash(hash, HashString(data.mLocation.c_str())); + } + hash = AddToHash(hash, data.mRelevantForJS); + hash = mozilla::AddToHash(hash, data.mInnerWindowID); + if (data.mLine.isSome()) { + hash = AddToHash(hash, *data.mLine); + } + if (data.mColumn.isSome()) { + hash = AddToHash(hash, *data.mColumn); + } + if (data.mCategoryPair.isSome()) { + hash = AddToHash(hash, static_cast<uint32_t>(*data.mCategoryPair)); + } + } + return hash; + } + + static bool match(const FrameKey& aKey, const FrameKey& aLookup) { + return aKey == aLookup; + } + + static void rekey(FrameKey& aKey, const FrameKey& aNewKey) { + aKey = aNewKey; + } + }; + + struct StackKey { + Maybe<uint32_t> mPrefixStackIndex; + uint32_t mFrameIndex; + + explicit StackKey(uint32_t aFrame) + : mFrameIndex(aFrame), mHash(HashGeneric(aFrame)) {} + + StackKey(const StackKey& aPrefix, uint32_t aPrefixStackIndex, + uint32_t aFrame) + : mPrefixStackIndex(Some(aPrefixStackIndex)), + mFrameIndex(aFrame), + mHash(AddToHash(aPrefix.mHash, aFrame)) {} + + HashNumber Hash() const { return mHash; } + + bool operator==(const StackKey& aOther) const { + return mPrefixStackIndex == aOther.mPrefixStackIndex && + mFrameIndex == aOther.mFrameIndex; + } + + private: + HashNumber mHash; + }; + + struct StackKeyHasher { + using Lookup = StackKey; + + static HashNumber hash(const StackKey& aLookup) { return aLookup.Hash(); } + + static bool match(const StackKey& aKey, const StackKey& aLookup) { + return aKey == aLookup; + } + + static void rekey(StackKey& aKey, const StackKey& aNewKey) { + aKey = aNewKey; + } + }; + + UniqueStacks(); + + // Return a StackKey for aFrame as the stack's root frame (no prefix). + [[nodiscard]] StackKey BeginStack(const FrameKey& aFrame); + + // Return a new StackKey that is obtained by appending aFrame to aStack. + [[nodiscard]] StackKey AppendFrame(const StackKey& aStack, + const FrameKey& aFrame); + + [[nodiscard]] uint32_t GetOrAddFrameIndex(const FrameKey& aFrame); + [[nodiscard]] uint32_t GetOrAddStackIndex(const StackKey& aStack); + + void SpliceFrameTableElements(SpliceableJSONWriter& aWriter); + void SpliceStackTableElements(SpliceableJSONWriter& aWriter); + + UniqueJSONStrings& UniqueStrings() { + MOZ_RELEASE_ASSERT(mUniqueStrings.get()); + return *mUniqueStrings; + } + + private: + void StreamNonJITFrame(const FrameKey& aFrame); + void StreamStack(const StackKey& aStack); + + UniquePtr<UniqueJSONStrings> mUniqueStrings; + + SpliceableChunkedJSONWriter mFrameTableWriter; + HashMap<FrameKey, uint32_t, FrameKeyHasher> mFrameToIndexMap; + + SpliceableChunkedJSONWriter mStackTableWriter; + HashMap<StackKey, uint32_t, StackKeyHasher> mStackToIndexMap; +}; + +// +// Thread profile JSON Format +// -------------------------- +// +// The profile contains much duplicate information. The output JSON of the +// profile attempts to deduplicate strings, frames, and stack prefixes, to cut +// down on size and to increase JSON streaming speed. Deduplicated values are +// streamed as indices into their respective tables. +// +// Further, arrays of objects with the same set of properties (e.g., samples, +// frames) are output as arrays according to a schema instead of an object +// with property names. A property that is not present is represented in the +// array as null or undefined. +// +// The format of the thread profile JSON is shown by the following example +// with 1 sample and 1 marker: +// +// { +// "name": "Foo", +// "tid": 42, +// "samples": +// { +// "schema": +// { +// "stack": 0, /* index into stackTable */ +// "time": 1, /* number */ +// "eventDelay": 2, /* number */ +// }, +// "data": +// [ +// [ 1, 0.0, 0.0 ] /* { stack: 1, time: 0.0, eventDelay: 0.0 } */ +// ] +// }, +// +// "markers": +// { +// "schema": +// { +// "name": 0, /* index into stringTable */ +// "time": 1, /* number */ +// "data": 2 /* arbitrary JSON */ +// }, +// "data": +// [ +// [ 3, 0.1 ] /* { name: 'example marker', time: 0.1 } */ +// ] +// }, +// +// "stackTable": +// { +// "schema": +// { +// "prefix": 0, /* index into stackTable */ +// "frame": 1 /* index into frameTable */ +// }, +// "data": +// [ +// [ null, 0 ], /* (root) */ +// [ 0, 1 ] /* (root) > foo.js */ +// ] +// }, +// +// "frameTable": +// { +// "schema": +// { +// "location": 0, /* index into stringTable */ +// "relevantForJS": 1, /* bool */ +// "innerWindowID": 2, /* inner window ID of global JS `window` object */ +// "implementation": 3, /* index into stringTable */ +// "line": 4, /* number */ +// "column": 5, /* number */ +// "category": 6, /* index into profile.meta.categories */ +// "subcategory": 7 /* index into +// profile.meta.categories[category].subcategories */ +// }, +// "data": +// [ +// [ 0 ], /* { location: '(root)' } */ +// [ 1, 2 ] /* { location: 'foo.js', +// implementation: 'baseline' } */ +// ] +// }, +// +// "stringTable": +// [ +// "(root)", +// "foo.js", +// "baseline", +// "example marker" +// ] +// } +// +// Process: +// { +// "name": "Bar", +// "pid": 24, +// "threads": +// [ +// <0-N threads from above> +// ], +// "counters": /* includes the memory counter */ +// [ +// { +// "name": "qwerty", +// "category": "uiop", +// "description": "this is qwerty uiop", +// "sample_groups: +// [ +// { +// "id": 42, /* number (thread id, or object identifier (tab), etc) */ +// "samples: +// { +// "schema": +// { +// "time": 1, /* number */ +// "number": 2, /* number (of times the counter was touched) */ +// "count": 3 /* number (total for the counter) */ +// }, +// "data": +// [ +// [ 0.1, 1824, +// 454622 ] /* { time: 0.1, number: 1824, count: 454622 } */ +// ] +// }, +// }, +// /* more sample-group objects with different id's */ +// ] +// }, +// /* more counters */ +// ], +// } +// + +} // namespace baseprofiler +} // namespace mozilla + +#endif /* ndef ProfileBufferEntry_h */ diff --git a/mozglue/baseprofiler/core/ProfileJSONWriter.cpp b/mozglue/baseprofiler/core/ProfileJSONWriter.cpp new file mode 100644 index 0000000000..dc51f6958e --- /dev/null +++ b/mozglue/baseprofiler/core/ProfileJSONWriter.cpp @@ -0,0 +1,101 @@ +/* -*- Mode: C++; tab-width: 2; 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 "mozilla/BaseProfileJSONWriter.h" + +namespace mozilla::baseprofiler { + +UniqueJSONStrings::UniqueJSONStrings(FailureLatch& aFailureLatch) + : mStringTableWriter(aFailureLatch) { + mStringTableWriter.StartBareList(); + if (const char* failure = mStringTableWriter.GetFailure(); failure) { + ClearAndSetFailure(failure); + return; + } +} + +UniqueJSONStrings::UniqueJSONStrings(FailureLatch& aFailureLatch, + const UniqueJSONStrings& aOther, + ProgressLogger aProgressLogger) + : mStringTableWriter(aFailureLatch) { + using namespace mozilla::literals::ProportionValue_literals; // For `10_pc`. + + if (mStringTableWriter.Failed()) { + return; + } + + if (const char* failure = aOther.GetFailure(); failure) { + ClearAndSetFailure(failure); + return; + } + + mStringTableWriter.StartBareList(); + uint32_t count = aOther.mStringHashToIndexMap.count(); + if (count != 0) { + if (!mStringHashToIndexMap.reserve(count)) { + ClearAndSetFailure("Cannot reserve UniqueJSONStrings map storage"); + return; + } + auto iter = aOther.mStringHashToIndexMap.iter(); + for (auto&& [unusedIndex, progressLogger] : + aProgressLogger.CreateLoopSubLoggersFromTo( + 10_pc, 90_pc, count, "Copying unique strings...")) { + (void)unusedIndex; + if (iter.done()) { + break; + } + mStringHashToIndexMap.putNewInfallible(iter.get().key(), + iter.get().value()); + iter.next(); + } + aProgressLogger.SetLocalProgress(90_pc, "Copied unique strings"); + mStringTableWriter.CopyAndSplice( + aOther.mStringTableWriter.ChunkedWriteFunc()); + if (const char* failure = aOther.GetFailure(); failure) { + ClearAndSetFailure(failure); + } + aProgressLogger.SetLocalProgress(100_pc, "Spliced unique strings"); + } +} + +UniqueJSONStrings::~UniqueJSONStrings() = default; + +void UniqueJSONStrings::SpliceStringTableElements( + SpliceableJSONWriter& aWriter) { + aWriter.TakeAndSplice(mStringTableWriter.TakeChunkedWriteFunc()); +} + +void UniqueJSONStrings::ClearAndSetFailure(std::string aFailure) { + mStringTableWriter.SetFailure(std::move(aFailure)); + mStringHashToIndexMap.clear(); +} + +Maybe<uint32_t> UniqueJSONStrings::GetOrAddIndex(const Span<const char>& aStr) { + if (Failed()) { + return Nothing{}; + } + + uint32_t count = mStringHashToIndexMap.count(); + HashNumber hash = HashString(aStr.data(), aStr.size()); + auto entry = mStringHashToIndexMap.lookupForAdd(hash); + if (entry) { + MOZ_ASSERT(entry->value() < count); + return Some(entry->value()); + } + + if (!mStringHashToIndexMap.add(entry, hash, count)) { + ClearAndSetFailure("OOM in UniqueJSONStrings::GetOrAddIndex adding a map"); + return Nothing{}; + } + mStringTableWriter.StringElement(aStr); + if (const char* failure = mStringTableWriter.GetFailure(); failure) { + ClearAndSetFailure(failure); + return Nothing{}; + } + return Some(count); +} + +} // namespace mozilla::baseprofiler diff --git a/mozglue/baseprofiler/core/ProfiledThreadData.cpp b/mozglue/baseprofiler/core/ProfiledThreadData.cpp new file mode 100644 index 0000000000..62cb994ae2 --- /dev/null +++ b/mozglue/baseprofiler/core/ProfiledThreadData.cpp @@ -0,0 +1,191 @@ +/* -*- Mode: C++; tab-width: 2; 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 "ProfiledThreadData.h" + +#include "BaseProfiler.h" +#include "ProfileBuffer.h" + +#include "mozilla/BaseProfileJSONWriter.h" + +#if defined(GP_OS_darwin) +# include <pthread.h> +#endif + +namespace mozilla { +namespace baseprofiler { + +ProfiledThreadData::ProfiledThreadData(ThreadInfo* aThreadInfo) + : mThreadInfo(aThreadInfo) {} + +ProfiledThreadData::~ProfiledThreadData() {} + +void ProfiledThreadData::StreamJSON(const ProfileBuffer& aBuffer, + SpliceableJSONWriter& aWriter, + const std::string& aProcessName, + const std::string& aETLDplus1, + const TimeStamp& aProcessStartTime, + double aSinceTime) { + UniqueStacks uniqueStacks; + + aWriter.SetUniqueStrings(uniqueStacks.UniqueStrings()); + + aWriter.Start(); + { + StreamSamplesAndMarkers(mThreadInfo->Name(), mThreadInfo->ThreadId(), + aBuffer, aWriter, aProcessName, aETLDplus1, + aProcessStartTime, mThreadInfo->RegisterTime(), + mUnregisterTime, aSinceTime, uniqueStacks); + + aWriter.StartObjectProperty("stackTable"); + { + { + JSONSchemaWriter schema(aWriter); + schema.WriteField("prefix"); + schema.WriteField("frame"); + } + + aWriter.StartArrayProperty("data"); + { uniqueStacks.SpliceStackTableElements(aWriter); } + aWriter.EndArray(); + } + aWriter.EndObject(); + + aWriter.StartObjectProperty("frameTable"); + { + { + JSONSchemaWriter schema(aWriter); + schema.WriteField("location"); + schema.WriteField("relevantForJS"); + schema.WriteField("innerWindowID"); + schema.WriteField("implementation"); + schema.WriteField("line"); + schema.WriteField("column"); + schema.WriteField("category"); + schema.WriteField("subcategory"); + } + + aWriter.StartArrayProperty("data"); + { uniqueStacks.SpliceFrameTableElements(aWriter); } + aWriter.EndArray(); + } + aWriter.EndObject(); + + aWriter.StartArrayProperty("stringTable"); + { + std::move(uniqueStacks.UniqueStrings()) + .SpliceStringTableElements(aWriter); + } + aWriter.EndArray(); + } + aWriter.End(); + + aWriter.ResetUniqueStrings(); +} + +BaseProfilerThreadId StreamSamplesAndMarkers( + const char* aName, BaseProfilerThreadId aThreadId, + const ProfileBuffer& aBuffer, SpliceableJSONWriter& aWriter, + const std::string& aProcessName, const std::string& aETLDplus1, + const TimeStamp& aProcessStartTime, const TimeStamp& aRegisterTime, + const TimeStamp& aUnregisterTime, double aSinceTime, + UniqueStacks& aUniqueStacks) { + BaseProfilerThreadId processedThreadId; + + aWriter.StringProperty( + "processType", + "(unknown)" /* XRE_GeckoProcessTypeToString(XRE_GetProcessType()) */); + + { + std::string name = aName; + // We currently need to distinguish threads output by Base Profiler from + // those in Gecko Profiler, as the frontend could get confused and lose + // tracks with the same name. + // TODO: As part of the profilers de-duplication, thread data from both + // profilers should end up in the same track, at which point this won't be + // necessary anymore. See meta bug 1557566. + name += " (pre-xul)"; + aWriter.StringProperty("name", name); + } + + // Use given process name (if any). + if (!aProcessName.empty()) { + aWriter.StringProperty("processName", aProcessName); + } + if (!aETLDplus1.empty()) { + aWriter.StringProperty("eTLD+1", aETLDplus1); + } + + if (aRegisterTime) { + aWriter.DoubleProperty( + "registerTime", (aRegisterTime - aProcessStartTime).ToMilliseconds()); + } else { + aWriter.NullProperty("registerTime"); + } + + if (aUnregisterTime) { + aWriter.DoubleProperty( + "unregisterTime", + (aUnregisterTime - aProcessStartTime).ToMilliseconds()); + } else { + aWriter.NullProperty("unregisterTime"); + } + + aWriter.StartObjectProperty("samples"); + { + { + JSONSchemaWriter schema(aWriter); + schema.WriteField("stack"); + schema.WriteField("time"); + schema.WriteField("eventDelay"); + } + + aWriter.StartArrayProperty("data"); + { + processedThreadId = aBuffer.StreamSamplesToJSON( + aWriter, aThreadId, aSinceTime, aUniqueStacks); + } + aWriter.EndArray(); + } + aWriter.EndObject(); + + aWriter.StartObjectProperty("markers"); + { + { + JSONSchemaWriter schema(aWriter); + schema.WriteField("name"); + schema.WriteField("startTime"); + schema.WriteField("endTime"); + schema.WriteField("phase"); + schema.WriteField("category"); + schema.WriteField("data"); + } + + aWriter.StartArrayProperty("data"); + { + aBuffer.StreamMarkersToJSON(aWriter, aThreadId, aProcessStartTime, + aSinceTime, aUniqueStacks); + } + aWriter.EndArray(); + } + aWriter.EndObject(); + + // Tech note: If `ToNumber()` returns a uint64_t, the conversion to int64_t is + // "implementation-defined" before C++20. This is acceptable here, because + // this is a one-way conversion to a unique identifier that's used to visually + // separate data by thread on the front-end. + aWriter.IntProperty( + "pid", static_cast<int64_t>(profiler_current_process_id().ToNumber())); + aWriter.IntProperty("tid", + static_cast<int64_t>(aThreadId.IsSpecified() + ? aThreadId.ToNumber() + : processedThreadId.ToNumber())); + + return processedThreadId; +} + +} // namespace baseprofiler +} // namespace mozilla diff --git a/mozglue/baseprofiler/core/ProfiledThreadData.h b/mozglue/baseprofiler/core/ProfiledThreadData.h new file mode 100644 index 0000000000..0590b03326 --- /dev/null +++ b/mozglue/baseprofiler/core/ProfiledThreadData.h @@ -0,0 +1,120 @@ +/* -*- Mode: C++; tab-width: 2; 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/. */ + +#ifndef ProfiledThreadData_h +#define ProfiledThreadData_h + +#include "BaseProfilingStack.h" +#include "platform.h" +#include "ProfileBufferEntry.h" +#include "ThreadInfo.h" + +#include "mozilla/RefPtr.h" +#include "mozilla/TimeStamp.h" +#include "mozilla/UniquePtr.h" + +#include <string> + +namespace mozilla { +namespace baseprofiler { + +class ProfileBuffer; + +// This class contains information about a thread that is only relevant while +// the profiler is running, for any threads (both alive and dead) whose thread +// name matches the "thread filter" in the current profiler run. +// ProfiledThreadData objects may be kept alive even after the thread is +// unregistered, as long as there is still data for that thread in the profiler +// buffer. +// +// Accesses to this class are protected by the profiler state lock. +// +// Created as soon as the following are true for the thread: +// - The profiler is running, and +// - the thread matches the profiler's thread filter, and +// - the thread is registered with the profiler. +// So it gets created in response to either (1) the profiler being started (for +// an existing registered thread) or (2) the thread being registered (if the +// profiler is already running). +// +// The thread may be unregistered during the lifetime of ProfiledThreadData. +// If that happens, NotifyUnregistered() is called. +// +// This class is the right place to store buffer positions. Profiler buffer +// positions become invalid if the profiler buffer is destroyed, which happens +// when the profiler is stopped. +class ProfiledThreadData final { + public: + explicit ProfiledThreadData(ThreadInfo* aThreadInfo); + ~ProfiledThreadData(); + + void NotifyUnregistered(uint64_t aBufferPosition) { + mLastSample = Nothing(); + MOZ_ASSERT(!mBufferPositionWhenReceivedJSContext, + "JSContext should have been cleared before the thread was " + "unregistered"); + mUnregisterTime = TimeStamp::Now(); + mBufferPositionWhenUnregistered = Some(aBufferPosition); + } + Maybe<uint64_t> BufferPositionWhenUnregistered() { + return mBufferPositionWhenUnregistered; + } + + Maybe<uint64_t>& LastSample() { return mLastSample; } + + void StreamJSON(const ProfileBuffer& aBuffer, SpliceableJSONWriter& aWriter, + const std::string& aProcessName, + const std::string& aETLDplus1, + const TimeStamp& aProcessStartTime, double aSinceTime); + + const RefPtr<ThreadInfo> Info() const { return mThreadInfo; } + + void NotifyReceivedJSContext(uint64_t aCurrentBufferPosition) { + mBufferPositionWhenReceivedJSContext = Some(aCurrentBufferPosition); + } + + private: + // Group A: + // The following fields are interesting for the entire lifetime of a + // ProfiledThreadData object. + + // This thread's thread info. + const RefPtr<ThreadInfo> mThreadInfo; + + // Group B: + // The following fields are only used while this thread is alive and + // registered. They become Nothing() once the thread is unregistered. + + // When sampling, this holds the position in ActivePS::mBuffer of the most + // recent sample for this thread, or Nothing() if there is no sample for this + // thread in the buffer. + Maybe<uint64_t> mLastSample; + + // Only non-Nothing() if the thread currently has a JSContext. + Maybe<uint64_t> mBufferPositionWhenReceivedJSContext; + + // Group C: + // The following fields are only used once this thread has been unregistered. + + Maybe<uint64_t> mBufferPositionWhenUnregistered; + TimeStamp mUnregisterTime; +}; + +// Stream all samples and markers from aBuffer with the given aThreadId (or 0 +// for everything, which is assumed to be a single backtrace sample.) +// Returns the thread id of the output sample(s), or 0 if none was present. +BaseProfilerThreadId StreamSamplesAndMarkers( + const char* aName, BaseProfilerThreadId aThreadId, + const ProfileBuffer& aBuffer, SpliceableJSONWriter& aWriter, + const std::string& aProcessName, const std::string& aETLDplus1, + const TimeStamp& aProcessStartTime, const TimeStamp& aRegisterTime, + const TimeStamp& aUnregisterTime, double aSinceTime, + UniqueStacks& aUniqueStacks); + +} // namespace baseprofiler +} // namespace mozilla + +#endif // ProfiledThreadData_h diff --git a/mozglue/baseprofiler/core/ProfilerBacktrace.cpp b/mozglue/baseprofiler/core/ProfilerBacktrace.cpp new file mode 100644 index 0000000000..8343888351 --- /dev/null +++ b/mozglue/baseprofiler/core/ProfilerBacktrace.cpp @@ -0,0 +1,125 @@ +/* -*- 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 "ProfilerBacktrace.h" + +#include "BaseProfiler.h" +#include "ProfileBuffer.h" +#include "ProfiledThreadData.h" +#include "ThreadInfo.h" + +#include "mozilla/BaseProfileJSONWriter.h" + +namespace mozilla { +namespace baseprofiler { + +ProfilerBacktrace::ProfilerBacktrace( + const char* aName, + UniquePtr<ProfileChunkedBuffer> aProfileChunkedBufferStorage, + UniquePtr<ProfileBuffer> aProfileBufferStorageOrNull /* = nullptr */) + : mName(aName), + mOptionalProfileChunkedBufferStorage( + std::move(aProfileChunkedBufferStorage)), + mProfileChunkedBuffer(mOptionalProfileChunkedBufferStorage.get()), + mOptionalProfileBufferStorage(std::move(aProfileBufferStorageOrNull)), + mProfileBuffer(mOptionalProfileBufferStorage.get()) { + if (mProfileBuffer) { + MOZ_RELEASE_ASSERT(mProfileChunkedBuffer, + "If we take ownership of a ProfileBuffer, we must also " + "receive ownership of a ProfileChunkedBuffer"); + MOZ_RELEASE_ASSERT( + mProfileChunkedBuffer == &mProfileBuffer->UnderlyingChunkedBuffer(), + "If we take ownership of a ProfileBuffer, we must also receive " + "ownership of its ProfileChunkedBuffer"); + } + MOZ_ASSERT( + !mProfileChunkedBuffer || !mProfileChunkedBuffer->IsThreadSafe(), + "ProfilerBacktrace only takes a non-thread-safe ProfileChunkedBuffer"); +} + +ProfilerBacktrace::ProfilerBacktrace( + const char* aName, + ProfileChunkedBuffer* aExternalProfileChunkedBufferOrNull /* = nullptr */, + ProfileBuffer* aExternalProfileBufferOrNull /* = nullptr */) + : mName(aName), + mProfileChunkedBuffer(aExternalProfileChunkedBufferOrNull), + mProfileBuffer(aExternalProfileBufferOrNull) { + if (!mProfileChunkedBuffer) { + if (mProfileBuffer) { + // We don't have a ProfileChunkedBuffer but we have a ProfileBuffer, use + // the latter's ProfileChunkedBuffer. + mProfileChunkedBuffer = &mProfileBuffer->UnderlyingChunkedBuffer(); + MOZ_ASSERT(!mProfileChunkedBuffer->IsThreadSafe(), + "ProfilerBacktrace only takes a non-thread-safe " + "ProfileChunkedBuffer"); + } + } else { + if (mProfileBuffer) { + MOZ_RELEASE_ASSERT( + mProfileChunkedBuffer == &mProfileBuffer->UnderlyingChunkedBuffer(), + "If we reference both ProfileChunkedBuffer and ProfileBuffer, they " + "must already be connected"); + } + MOZ_ASSERT(!mProfileChunkedBuffer->IsThreadSafe(), + "ProfilerBacktrace only takes a non-thread-safe " + "ProfileChunkedBuffer"); + } +} + +ProfilerBacktrace::~ProfilerBacktrace() {} + +BaseProfilerThreadId ProfilerBacktrace::StreamJSON( + SpliceableJSONWriter& aWriter, const TimeStamp& aProcessStartTime, + UniqueStacks& aUniqueStacks) { + BaseProfilerThreadId processedThreadId; + + // Unlike ProfiledThreadData::StreamJSON, we don't need to call + // ProfileBuffer::AddJITInfoForRange because ProfileBuffer does not contain + // any JitReturnAddr entries. For synchronous samples, JIT frames get expanded + // at sample time. + if (mProfileBuffer) { + processedThreadId = StreamSamplesAndMarkers( + mName.c_str(), BaseProfilerThreadId{}, *mProfileBuffer, aWriter, "", "", + aProcessStartTime, + /* aRegisterTime */ TimeStamp(), + /* aUnregisterTime */ TimeStamp(), + /* aSinceTime */ 0, aUniqueStacks); + } else if (mProfileChunkedBuffer) { + ProfileBuffer profileBuffer(*mProfileChunkedBuffer); + processedThreadId = StreamSamplesAndMarkers( + mName.c_str(), BaseProfilerThreadId{}, profileBuffer, aWriter, "", "", + aProcessStartTime, + /* aRegisterTime */ TimeStamp(), + /* aUnregisterTime */ TimeStamp(), + /* aSinceTime */ 0, aUniqueStacks); + } + // If there are no buffers, the backtrace is empty and nothing is streamed. + + return processedThreadId; +} + +} // namespace baseprofiler + +// static +template <typename Destructor> +UniquePtr<baseprofiler::ProfilerBacktrace, Destructor> +ProfileBufferEntryReader:: + Deserializer<UniquePtr<baseprofiler::ProfilerBacktrace, Destructor>>::Read( + ProfileBufferEntryReader& aER) { + auto profileChunkedBuffer = aER.ReadObject<UniquePtr<ProfileChunkedBuffer>>(); + if (!profileChunkedBuffer) { + return nullptr; + } + MOZ_ASSERT( + !profileChunkedBuffer->IsThreadSafe(), + "ProfilerBacktrace only stores non-thread-safe ProfileChunkedBuffers"); + std::string name = aER.ReadObject<std::string>(); + return UniquePtr<baseprofiler::ProfilerBacktrace, Destructor>{ + new baseprofiler::ProfilerBacktrace(name.c_str(), + std::move(profileChunkedBuffer))}; +}; + +} // namespace mozilla diff --git a/mozglue/baseprofiler/core/ProfilerBacktrace.h b/mozglue/baseprofiler/core/ProfilerBacktrace.h new file mode 100644 index 0000000000..0b5b69e738 --- /dev/null +++ b/mozglue/baseprofiler/core/ProfilerBacktrace.h @@ -0,0 +1,162 @@ +/* -*- Mode: C++; tab-width: 2; 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/. */ + +#ifndef __PROFILER_BACKTRACE_H +#define __PROFILER_BACKTRACE_H + +#include "mozilla/ProfileChunkedBuffer.h" +#include "mozilla/UniquePtr.h" + +#include <string> + +namespace mozilla { + +class TimeStamp; + +namespace baseprofiler { + +class ProfileBuffer; +class SpliceableJSONWriter; +class ThreadInfo; +class UniqueStacks; + +// ProfilerBacktrace encapsulates a synchronous sample. +// It can work with a ProfileBuffer and/or a ProfileChunkedBuffer (if both, they +// must already be linked together). The ProfileChunkedBuffer contains all the +// data; the ProfileBuffer is not strictly needed, only provide it if it is +// already available at the call site. +// And these buffers can either be: +// - owned here, so that the ProfilerBacktrace object can be kept for later +// use), OR +// - referenced through pointers (in cases where the backtrace is immediately +// streamed out, so we only need temporary references to external buffers); +// these pointers may be null for empty backtraces. +class ProfilerBacktrace { + public: + // Take ownership of external buffers and use them to keep, and to stream a + // backtrace. If a ProfileBuffer is given, its underlying chunked buffer must + // be provided as well. + explicit ProfilerBacktrace( + const char* aName, + UniquePtr<ProfileChunkedBuffer> aProfileChunkedBufferStorage, + UniquePtr<ProfileBuffer> aProfileBufferStorageOrNull = nullptr); + + // Take pointers to external buffers and use them to stream a backtrace. + // If null, the backtrace is effectively empty. + // If both are provided, they must already be connected. + explicit ProfilerBacktrace( + const char* aName, + ProfileChunkedBuffer* aExternalProfileChunkedBufferOrNull = nullptr, + ProfileBuffer* aExternalProfileBufferOrNull = nullptr); + + ~ProfilerBacktrace(); + + [[nodiscard]] bool IsEmpty() const { + return !mProfileChunkedBuffer || + ProfileBufferEntryWriter::Serializer<ProfileChunkedBuffer>::Bytes( + *mProfileChunkedBuffer) <= ULEB128Size(0u); + } + + // ProfilerBacktraces' stacks are deduplicated in the context of the + // profile that contains the backtrace as a marker payload. + // + // That is, markers that contain backtraces should not need their own stack, + // frame, and string tables. They should instead reuse their parent + // profile's tables. + BaseProfilerThreadId StreamJSON(SpliceableJSONWriter& aWriter, + const TimeStamp& aProcessStartTime, + UniqueStacks& aUniqueStacks); + + private: + // Used to de/serialize a ProfilerBacktrace. + friend ProfileBufferEntryWriter::Serializer<ProfilerBacktrace>; + friend ProfileBufferEntryReader::Deserializer<ProfilerBacktrace>; + + std::string mName; + + // `ProfileChunkedBuffer` in which `mProfileBuffer` stores its data; must be + // located before `mProfileBuffer` so that it's destroyed after. + UniquePtr<ProfileChunkedBuffer> mOptionalProfileChunkedBufferStorage; + // If null, there is no need to check mProfileBuffer's (if present) underlying + // buffer because this is done when constructed. + ProfileChunkedBuffer* mProfileChunkedBuffer; + + UniquePtr<ProfileBuffer> mOptionalProfileBufferStorage; + ProfileBuffer* mProfileBuffer; +}; + +} // namespace baseprofiler + +// Format: [ UniquePtr<BlockRingsBuffer> | name ] +// Initial len==0 marks a nullptr or empty backtrace. +template <> +struct ProfileBufferEntryWriter::Serializer<baseprofiler::ProfilerBacktrace> { + static Length Bytes(const baseprofiler::ProfilerBacktrace& aBacktrace) { + if (!aBacktrace.mProfileChunkedBuffer) { + // No buffer. + return ULEB128Size(0u); + } + auto bufferBytes = SumBytes(*aBacktrace.mProfileChunkedBuffer); + if (bufferBytes <= ULEB128Size(0u)) { + // Empty buffer. + return ULEB128Size(0u); + } + return bufferBytes + SumBytes(aBacktrace.mName); + } + + static void Write(ProfileBufferEntryWriter& aEW, + const baseprofiler::ProfilerBacktrace& aBacktrace) { + if (!aBacktrace.mProfileChunkedBuffer || + SumBytes(*aBacktrace.mProfileChunkedBuffer) <= ULEB128Size(0u)) { + // No buffer, or empty buffer. + aEW.WriteULEB128(0u); + return; + } + aEW.WriteObject(*aBacktrace.mProfileChunkedBuffer); + aEW.WriteObject(aBacktrace.mName); + } +}; + +template <typename Destructor> +struct ProfileBufferEntryWriter::Serializer< + UniquePtr<baseprofiler::ProfilerBacktrace, Destructor>> { + static Length Bytes(const UniquePtr<baseprofiler::ProfilerBacktrace, + Destructor>& aBacktrace) { + if (!aBacktrace) { + // Null backtrace pointer (treated like an empty backtrace). + return ULEB128Size(0u); + } + return SumBytes(*aBacktrace); + } + + static void Write(ProfileBufferEntryWriter& aEW, + const UniquePtr<baseprofiler::ProfilerBacktrace, + Destructor>& aBacktrace) { + if (!aBacktrace) { + // Null backtrace pointer (treated like an empty backtrace). + aEW.WriteULEB128(0u); + return; + } + aEW.WriteObject(*aBacktrace); + } +}; + +template <typename Destructor> +struct ProfileBufferEntryReader::Deserializer< + UniquePtr<baseprofiler::ProfilerBacktrace, Destructor>> { + static void ReadInto( + ProfileBufferEntryReader& aER, + UniquePtr<baseprofiler::ProfilerBacktrace, Destructor>& aBacktrace) { + aBacktrace = Read(aER); + } + + static UniquePtr<baseprofiler::ProfilerBacktrace, Destructor> Read( + ProfileBufferEntryReader& aER); +}; + +} // namespace mozilla + +#endif // __PROFILER_BACKTRACE_H diff --git a/mozglue/baseprofiler/core/ProfilerMarkers.cpp b/mozglue/baseprofiler/core/ProfilerMarkers.cpp new file mode 100644 index 0000000000..eff6c7e364 --- /dev/null +++ b/mozglue/baseprofiler/core/ProfilerMarkers.cpp @@ -0,0 +1,348 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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 "mozilla/BaseProfilerMarkers.h" + +#include "mozilla/BaseProfilerUtils.h" + +#include <limits> + +namespace mozilla { +namespace base_profiler_markers_detail { + +// We need an atomic type that can hold a `DeserializerTag`. (Atomic doesn't +// work with too-small types.) +using DeserializerTagAtomic = unsigned; + +// The atomic sDeserializerCount still also include bits that act as a "RWLock": +// Whoever can set this bit gets exclusive access to the count and the whole +// sMarkerTypeFunctions1Based array, guaranteeing that it cannot be modified. +static constexpr DeserializerTagAtomic scExclusiveLock = 0x80'00'00'00u; +// Code that wants shared access can add this value, then ensure there is no +// exclusive lock, after which it's guaranteed that no exclusive lock can be +// taken until the shared lock count goes back to zero. +static constexpr DeserializerTagAtomic scSharedLockUnit = 0x00'01'00'00u; +// This mask isolates the actual count value from the lock bits. +static constexpr DeserializerTagAtomic scTagMask = 0x00'00'FF'FFu; + +// Number of currently-registered deserializers and other marker type functions. +// The high bits contain lock bits, see above. +static Atomic<DeserializerTagAtomic, MemoryOrdering::ReleaseAcquire> + sDeserializerCount{0}; + +// This needs to be big enough to handle all possible marker types. If one day +// this needs to be higher, the underlying DeserializerTag type will have to be +// changed. +static constexpr DeserializerTagAtomic DeserializerMax = 250; +static_assert(DeserializerMax <= scTagMask, + "DeserializerMax doesn't fit in scTagMask"); + +static_assert( + DeserializerMax <= std::numeric_limits<Streaming::DeserializerTag>::max(), + "The maximum number of deserializers must fit in the DeserializerTag type"); + +// Array of marker type functions. +// 1-based, i.e.: [0] -> tag 1, [DeserializerMax - 1] -> tag DeserializerMax. +// Elements are added at the next available atomically-incremented +// `sDeserializerCount` (minus 1) whenever a new marker type is used in a +// Firefox session; the content is kept between profiler runs in that session. +// There is theoretically a race between the increment and the time the entry is +// fully written, but in practice all new elements are written (during +// profiling, using a marker type for the first time) long before they are read +// (after profiling is paused). +static Streaming::MarkerTypeFunctions + sMarkerTypeFunctions1Based[DeserializerMax]; + +/* static */ Streaming::DeserializerTag Streaming::TagForMarkerTypeFunctions( + Streaming::MarkerDataDeserializer aDeserializer, + Streaming::MarkerTypeNameFunction aMarkerTypeNameFunction, + Streaming::MarkerSchemaFunction aMarkerSchemaFunction) { + MOZ_RELEASE_ASSERT(!!aDeserializer); + MOZ_RELEASE_ASSERT(!!aMarkerTypeNameFunction); + MOZ_RELEASE_ASSERT(!!aMarkerSchemaFunction); + + // Add a shared lock request, which will prevent future exclusive locking. + DeserializerTagAtomic tagWithLock = (sDeserializerCount += scSharedLockUnit); + + // An exclusive locker may have arrived before us, just wait for it to finish. + while ((tagWithLock & scExclusiveLock) != 0u) { + tagWithLock = sDeserializerCount; + } + + MOZ_ASSERT( + // This is equivalent to shifting right to only keep the lock counts. + tagWithLock / scSharedLockUnit < + // This is effectively half of the permissible shared lock range, + // that would mean way too many threads doing this work here! + scExclusiveLock / scSharedLockUnit / 2, + "The shared lock count is getting unexpectedly high, verify the " + "algorithm, and tweak constants if needed"); + + // Reserve a tag. Even if there are multiple shared-lock holders here, each + // one will get a different value, and therefore will access a different part + // of the sMarkerTypeFunctions1Based array. + const DeserializerTagAtomic tag = ++sDeserializerCount & scTagMask; + + MOZ_RELEASE_ASSERT( + tag <= DeserializerMax, + "Too many deserializers, consider increasing DeserializerMax. " + "Or is a deserializer stored again and again?"); + sMarkerTypeFunctions1Based[tag - 1] = {aDeserializer, aMarkerTypeNameFunction, + aMarkerSchemaFunction}; + + // And release our shared lock, to allow exclusive readers. + sDeserializerCount -= scSharedLockUnit; + + return static_cast<DeserializerTag>(tag); +} + +/* static */ Streaming::MarkerDataDeserializer Streaming::DeserializerForTag( + Streaming::DeserializerTag aTag) { + MOZ_RELEASE_ASSERT( + aTag > 0 && static_cast<DeserializerTagAtomic>(aTag) <= + static_cast<DeserializerTagAtomic>(sDeserializerCount), + "Out-of-range tag value"); + return sMarkerTypeFunctions1Based[aTag - 1].mMarkerDataDeserializer; +} + +Streaming::LockedMarkerTypeFunctionsList::LockedMarkerTypeFunctionsList() { + for (;;) { + const DeserializerTagAtomic count = sDeserializerCount; + if ((count & scTagMask) != count) { + // Someone already has a lock, loop around. + continue; + } + + // There are currently no locks, try to add our exclusive lock. + if (!sDeserializerCount.compareExchange(count, count | scExclusiveLock)) { + // Someone else modified sDeserializerCount since our read, loop around. + continue; + } + + // We applied our exclusive lock, we can now read the list of functions, + // without interference until ~LockedMarkerTypeFunctionsList(). + // (Note that sDeserializerCount may receive shared lock requests, but the + // count won't change.) + mMarkerTypeFunctionsSpan = {sMarkerTypeFunctions1Based, count}; + break; + } +} + +Streaming::LockedMarkerTypeFunctionsList::~LockedMarkerTypeFunctionsList() { + MOZ_ASSERT( + (sDeserializerCount & scExclusiveLock) == scExclusiveLock, + "sDeserializerCount should still have the the exclusive lock bit set"); + MOZ_ASSERT( + (sDeserializerCount & scTagMask) == + DeserializerTagAtomic(mMarkerTypeFunctionsSpan.size()), + "sDeserializerCount should have the same count since construction"); + sDeserializerCount &= ~scExclusiveLock; +} + +// Only accessed on the main thread. +// Both profilers (Base and Gecko) could be active at the same time, so keep a +// ref-count to only allocate at most one buffer at any time. +static int sBufferForMainThreadAddMarkerRefCount = 0; +static ProfileChunkedBuffer* sBufferForMainThreadAddMarker = nullptr; + +ProfileChunkedBuffer* GetClearedBufferForMainThreadAddMarker() { + if (!mozilla::baseprofiler::profiler_is_main_thread()) { + return nullptr; + } + + if (sBufferForMainThreadAddMarker) { + MOZ_ASSERT(sBufferForMainThreadAddMarker->IsInSession(), + "sBufferForMainThreadAddMarker should always be in-session"); + sBufferForMainThreadAddMarker->Clear(); + MOZ_ASSERT( + sBufferForMainThreadAddMarker->IsInSession(), + "Cleared sBufferForMainThreadAddMarker should still be in-session"); + } + + return sBufferForMainThreadAddMarker; +} + +MFBT_API void EnsureBufferForMainThreadAddMarker() { + if (!mozilla::baseprofiler::profiler_is_main_thread()) { + return; + } + + if (sBufferForMainThreadAddMarkerRefCount++ == 0) { + // First `Ensure`, allocate the buffer. + MOZ_ASSERT(!sBufferForMainThreadAddMarker); + sBufferForMainThreadAddMarker = new ProfileChunkedBuffer( + ProfileChunkedBuffer::ThreadSafety::WithoutMutex, + MakeUnique<ProfileBufferChunkManagerSingle>( + ProfileBufferChunkManager::scExpectedMaximumStackSize)); + MOZ_ASSERT(sBufferForMainThreadAddMarker); + MOZ_ASSERT(sBufferForMainThreadAddMarker->IsInSession()); + } +} + +MFBT_API void ReleaseBufferForMainThreadAddMarker() { + if (!mozilla::baseprofiler::profiler_is_main_thread()) { + return; + } + + if (sBufferForMainThreadAddMarkerRefCount == 0) { + // Unexpected Release! This should not normally happen, but it's harmless in + // practice, it means the buffer is not alive anyway. + return; + } + + MOZ_ASSERT(sBufferForMainThreadAddMarker); + MOZ_ASSERT(sBufferForMainThreadAddMarker->IsInSession()); + if (--sBufferForMainThreadAddMarkerRefCount == 0) { + // Last `Release`, destroy the buffer. + delete sBufferForMainThreadAddMarker; + sBufferForMainThreadAddMarker = nullptr; + } +} + +} // namespace base_profiler_markers_detail + +void MarkerSchema::Stream(JSONWriter& aWriter, + const Span<const char>& aName) && { + // The caller should have started a JSON array, in which we can add an object + // that defines a marker schema. + + if (mLocations.empty()) { + // SpecialFrontendLocation case, don't output anything for this type. + return; + } + + aWriter.StartObjectElement(); + { + aWriter.StringProperty("name", aName); + + if (!mChartLabel.empty()) { + aWriter.StringProperty("chartLabel", mChartLabel); + } + + if (!mTooltipLabel.empty()) { + aWriter.StringProperty("tooltipLabel", mTooltipLabel); + } + + if (!mTableLabel.empty()) { + aWriter.StringProperty("tableLabel", mTableLabel); + } + + aWriter.StartArrayProperty("display"); + { + for (Location location : mLocations) { + aWriter.StringElement(LocationToStringSpan(location)); + } + } + aWriter.EndArray(); + + aWriter.StartArrayProperty("data"); + { + for (const DataRow& row : mData) { + aWriter.StartObjectElement(); + { + row.match( + [&aWriter](const DynamicData& aData) { + aWriter.StringProperty("key", aData.mKey); + if (aData.mLabel) { + aWriter.StringProperty("label", *aData.mLabel); + } + aWriter.StringProperty("format", + FormatToStringSpan(aData.mFormat)); + if (aData.mSearchable) { + aWriter.BoolProperty( + "searchable", + *aData.mSearchable == Searchable::Searchable); + } + }, + [&aWriter](const StaticData& aStaticData) { + aWriter.StringProperty("label", aStaticData.mLabel); + aWriter.StringProperty("value", aStaticData.mValue); + }); + } + aWriter.EndObject(); + } + } + aWriter.EndArray(); + } + aWriter.EndObject(); +} + +/* static */ +Span<const char> MarkerSchema::LocationToStringSpan( + MarkerSchema::Location aLocation) { + switch (aLocation) { + case Location::MarkerChart: + return mozilla::MakeStringSpan("marker-chart"); + case Location::MarkerTable: + return mozilla::MakeStringSpan("marker-table"); + case Location::TimelineOverview: + return mozilla::MakeStringSpan("timeline-overview"); + case Location::TimelineMemory: + return mozilla::MakeStringSpan("timeline-memory"); + case Location::TimelineIPC: + return mozilla::MakeStringSpan("timeline-ipc"); + case Location::TimelineFileIO: + return mozilla::MakeStringSpan("timeline-fileio"); + case Location::StackChart: + return mozilla::MakeStringSpan("stack-chart"); + default: + MOZ_CRASH("Unexpected Location enum"); + return {}; + } +} + +/* static */ +Span<const char> MarkerSchema::FormatToStringSpan( + MarkerSchema::Format aFormat) { + switch (aFormat) { + case Format::Url: + return mozilla::MakeStringSpan("url"); + case Format::FilePath: + return mozilla::MakeStringSpan("file-path"); + case Format::String: + return mozilla::MakeStringSpan("string"); + case Format::Duration: + return mozilla::MakeStringSpan("duration"); + case Format::Time: + return mozilla::MakeStringSpan("time"); + case Format::Seconds: + return mozilla::MakeStringSpan("seconds"); + case Format::Milliseconds: + return mozilla::MakeStringSpan("milliseconds"); + case Format::Microseconds: + return mozilla::MakeStringSpan("microseconds"); + case Format::Nanoseconds: + return mozilla::MakeStringSpan("nanoseconds"); + case Format::Bytes: + return mozilla::MakeStringSpan("bytes"); + case Format::Percentage: + return mozilla::MakeStringSpan("percentage"); + case Format::Integer: + return mozilla::MakeStringSpan("integer"); + case Format::Decimal: + return mozilla::MakeStringSpan("decimal"); + default: + MOZ_CRASH("Unexpected Format enum"); + return {}; + } +} + +} // namespace mozilla + +namespace mozilla::baseprofiler { +template MFBT_API ProfileBufferBlockIndex AddMarker(const ProfilerString8View&, + const MarkerCategory&, + MarkerOptions&&, + markers::TextMarker, + const std::string&); + +template MFBT_API ProfileBufferBlockIndex +AddMarkerToBuffer(ProfileChunkedBuffer&, const ProfilerString8View&, + const MarkerCategory&, MarkerOptions&&, markers::NoPayload); + +template MFBT_API ProfileBufferBlockIndex AddMarkerToBuffer( + ProfileChunkedBuffer&, const ProfilerString8View&, const MarkerCategory&, + MarkerOptions&&, markers::TextMarker, const std::string&); +} // namespace mozilla::baseprofiler diff --git a/mozglue/baseprofiler/core/ProfilerUtils.cpp b/mozglue/baseprofiler/core/ProfilerUtils.cpp new file mode 100644 index 0000000000..5f53910774 --- /dev/null +++ b/mozglue/baseprofiler/core/ProfilerUtils.cpp @@ -0,0 +1,162 @@ +/* -*- Mode: C++; tab-width: 2; 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/. */ + +// This file implements functions from BaseProfilerUtils.h on all platforms. +// Functions with platform-specific implementations are separated in #if blocks +// below, with each block being self-contained with all the #includes and +// definitions it needs, to keep platform code easier to maintain in isolation. + +#include "mozilla/BaseProfilerUtils.h" + +// --------------------------------------------- WASI process & thread ids +#if defined(__wasi__) + +namespace mozilla::baseprofiler { + +// WASI is single-process and single-thread for now. + +BaseProfilerProcessId profiler_current_process_id() { + return BaseProfilerProcessId::FromNativeId(1u); +} + +BaseProfilerThreadId profiler_current_thread_id() { + return BaseProfilerThreadId::FromNativeId(1u); +} + +} // namespace mozilla::baseprofiler + +// --------------------------------------------- Windows process & thread ids +#elif defined(XP_WIN) + +# include <process.h> +# include <processthreadsapi.h> + +namespace mozilla::baseprofiler { + +BaseProfilerProcessId profiler_current_process_id() { + return BaseProfilerProcessId::FromNativeId(_getpid()); +} + +BaseProfilerThreadId profiler_current_thread_id() { + static_assert(std::is_same_v<BaseProfilerThreadId::NativeType, + decltype(GetCurrentThreadId())>, + "BaseProfilerThreadId::NativeType must be exactly the type " + "returned by GetCurrentThreadId()"); + return BaseProfilerThreadId::FromNativeId(GetCurrentThreadId()); +} + +} // namespace mozilla::baseprofiler + +// --------------------------------------------- Non-Windows process id +#else +// All non-Windows platforms are assumed to be POSIX, which has getpid(). + +# include <unistd.h> + +namespace mozilla::baseprofiler { + +BaseProfilerProcessId profiler_current_process_id() { + return BaseProfilerProcessId::FromNativeId(getpid()); +} + +} // namespace mozilla::baseprofiler + +// --------------------------------------------- Non-Windows thread id +// ------------------------------------------------------- macOS +# if defined(XP_MACOSX) + +# include <pthread.h> + +namespace mozilla::baseprofiler { + +BaseProfilerThreadId profiler_current_thread_id() { + uint64_t tid; + if (pthread_threadid_np(nullptr, &tid) != 0) { + return BaseProfilerThreadId{}; + } + return BaseProfilerThreadId::FromNativeId(tid); +} + +} // namespace mozilla::baseprofiler + +// ------------------------------------------------------- Android +// Test Android before Linux, because Linux includes Android. +# elif defined(__ANDROID__) || defined(ANDROID) + +namespace mozilla::baseprofiler { + +BaseProfilerThreadId profiler_current_thread_id() { + return BaseProfilerThreadId::FromNativeId(gettid()); +} + +} // namespace mozilla::baseprofiler + +// ------------------------------------------------------- Linux +# elif defined(XP_LINUX) + +# include <sys/syscall.h> + +namespace mozilla::baseprofiler { + +BaseProfilerThreadId profiler_current_thread_id() { + // glibc doesn't provide a wrapper for gettid() until 2.30 + return BaseProfilerThreadId::FromNativeId(syscall(SYS_gettid)); +} + +} // namespace mozilla::baseprofiler + +// ------------------------------------------------------- FreeBSD +# elif defined(XP_FREEBSD) + +# include <sys/thr.h> + +namespace mozilla::baseprofiler { + +BaseProfilerThreadId profiler_current_thread_id() { + long id; + if (thr_self(&id) != 0) { + return BaseProfilerThreadId{}; + } + return BaseProfilerThreadId::FromNativeId(id); +} + +} // namespace mozilla::baseprofiler + +// ------------------------------------------------------- Others +# else + +namespace mozilla::baseprofiler { + +BaseProfilerThreadId profiler_current_thread_id() { + return BaseProfilerThreadId::FromNativeId(std::this_thread::get_id()); +} + +} // namespace mozilla::baseprofiler + +# endif +#endif // End of non-XP_WIN. + +// --------------------------------------------- Platform-agnostic definitions + +namespace mozilla::baseprofiler { + +static BaseProfilerThreadId scBaseProfilerMainThreadId{}; + +void profiler_init_main_thread_id() { + if (!scBaseProfilerMainThreadId.IsSpecified()) { + scBaseProfilerMainThreadId = profiler_current_thread_id(); + } +} + +BaseProfilerThreadId profiler_main_thread_id() { + return scBaseProfilerMainThreadId; +} + +bool profiler_is_main_thread() { + return profiler_current_thread_id() == scBaseProfilerMainThreadId; +} + +} // namespace mozilla::baseprofiler diff --git a/mozglue/baseprofiler/core/ProfilingCategory.cpp b/mozglue/baseprofiler/core/ProfilingCategory.cpp new file mode 100644 index 0000000000..8ff2b15555 --- /dev/null +++ b/mozglue/baseprofiler/core/ProfilingCategory.cpp @@ -0,0 +1,71 @@ +/* -*- 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 "BaseProfilingCategory.h" + +#include "mozilla/ArrayUtils.h" +#include "mozilla/Assertions.h" + +#include "BaseProfiler.h" + +namespace mozilla { +namespace baseprofiler { + +// clang-format off + +// ProfilingSubcategory_X: +// One enum for each category X, listing that category's subcategories. This +// allows the sProfilingCategoryInfo macro construction below to look up a +// per-category index for a subcategory. +#define SUBCATEGORY_ENUMS_BEGIN_CATEGORY(name, labelAsString, color) \ + enum class ProfilingSubcategory_##name : uint32_t { +#define SUBCATEGORY_ENUMS_SUBCATEGORY(category, name, labelAsString) \ + name, +#define SUBCATEGORY_ENUMS_END_CATEGORY \ + }; +MOZ_PROFILING_CATEGORY_LIST(SUBCATEGORY_ENUMS_BEGIN_CATEGORY, + SUBCATEGORY_ENUMS_SUBCATEGORY, + SUBCATEGORY_ENUMS_END_CATEGORY) +#undef SUBCATEGORY_ENUMS_BEGIN_CATEGORY +#undef SUBCATEGORY_ENUMS_SUBCATEGORY +#undef SUBCATEGORY_ENUMS_END_CATEGORY + +// sProfilingCategoryPairInfo: +// A list of ProfilingCategoryPairInfos with the same order as +// ProfilingCategoryPair, which can be used to map a ProfilingCategoryPair to +// its information. +#define CATEGORY_INFO_BEGIN_CATEGORY(name, labelAsString, color) +#define CATEGORY_INFO_SUBCATEGORY(category, name, labelAsString) \ + {ProfilingCategory::category, \ + uint32_t(ProfilingSubcategory_##category::name), labelAsString}, +#define CATEGORY_INFO_END_CATEGORY +const ProfilingCategoryPairInfo sProfilingCategoryPairInfo[] = { + MOZ_PROFILING_CATEGORY_LIST(CATEGORY_INFO_BEGIN_CATEGORY, + CATEGORY_INFO_SUBCATEGORY, + CATEGORY_INFO_END_CATEGORY) +}; +#undef CATEGORY_INFO_BEGIN_CATEGORY +#undef CATEGORY_INFO_SUBCATEGORY +#undef CATEGORY_INFO_END_CATEGORY + +// clang-format on + +const ProfilingCategoryPairInfo& GetProfilingCategoryPairInfo( + ProfilingCategoryPair aCategoryPair) { + static_assert( + MOZ_ARRAY_LENGTH(sProfilingCategoryPairInfo) == + uint32_t(ProfilingCategoryPair::COUNT), + "sProfilingCategoryPairInfo and ProfilingCategory need to have the " + "same order and the same length"); + + uint32_t categoryPairIndex = uint32_t(aCategoryPair); + MOZ_RELEASE_ASSERT(categoryPairIndex <= + uint32_t(ProfilingCategoryPair::LAST)); + return sProfilingCategoryPairInfo[categoryPairIndex]; +} + +} // namespace baseprofiler +} // namespace mozilla diff --git a/mozglue/baseprofiler/core/ProfilingStack.cpp b/mozglue/baseprofiler/core/ProfilingStack.cpp new file mode 100644 index 0000000000..f5cd2ddd04 --- /dev/null +++ b/mozglue/baseprofiler/core/ProfilingStack.cpp @@ -0,0 +1,52 @@ +/* -*- 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 "BaseProfilingStack.h" + +#include <algorithm> + +#include "mozilla/IntegerRange.h" +#include "mozilla/UniquePtr.h" +#include "mozilla/UniquePtrExtensions.h" + +#include "BaseProfiler.h" + +namespace mozilla { +namespace baseprofiler { + +ProfilingStack::~ProfilingStack() { + // The label macros keep a reference to the ProfilingStack to avoid a TLS + // access. If these are somehow not all cleared we will get a + // use-after-free so better to crash now. + MOZ_RELEASE_ASSERT(stackPointer == 0); + + delete[] frames; +} + +void ProfilingStack::ensureCapacitySlow() { + MOZ_ASSERT(stackPointer >= capacity); + const uint32_t kInitialCapacity = 128; + + uint32_t sp = stackPointer; + auto newCapacity = + std::max(sp + 1, capacity ? capacity * 2 : kInitialCapacity); + + auto* newFrames = new ProfilingStackFrame[newCapacity]; + + // It's important that `frames` / `capacity` / `stackPointer` remain + // consistent here at all times. + for (auto i : IntegerRange(capacity)) { + newFrames[i] = frames[i]; + } + + ProfilingStackFrame* oldFrames = frames; + frames = newFrames; + capacity = newCapacity; + delete[] oldFrames; +} + +} // namespace baseprofiler +} // namespace mozilla diff --git a/mozglue/baseprofiler/core/RegisteredThread.cpp b/mozglue/baseprofiler/core/RegisteredThread.cpp new file mode 100644 index 0000000000..85a7fc2c6d --- /dev/null +++ b/mozglue/baseprofiler/core/RegisteredThread.cpp @@ -0,0 +1,42 @@ +/* -*- Mode: C++; tab-width: 2; 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 "RegisteredThread.h" + +#include "BaseProfiler.h" + +namespace mozilla { +namespace baseprofiler { + +RegisteredThread::RegisteredThread(ThreadInfo* aInfo, void* aStackTop) + : mRacyRegisteredThread(aInfo->ThreadId()), + mPlatformData(AllocPlatformData(aInfo->ThreadId())), + mStackTop(aStackTop), + mThreadInfo(aInfo) { + // We don't have to guess on mac +#if defined(GP_OS_darwin) + pthread_t self = pthread_self(); + mStackTop = pthread_get_stackaddr_np(self); +#endif +} + +RegisteredThread::~RegisteredThread() {} + +size_t RegisteredThread::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const { + size_t n = aMallocSizeOf(this); + + // Measurement of the following members may be added later if DMD finds it + // is worthwhile: + // - mPlatformData + // + // The following members are not measured: + // - mThreadInfo: because it is non-owning + + return n; +} + +} // namespace baseprofiler +} // namespace mozilla diff --git a/mozglue/baseprofiler/core/RegisteredThread.h b/mozglue/baseprofiler/core/RegisteredThread.h new file mode 100644 index 0000000000..baaf7dced6 --- /dev/null +++ b/mozglue/baseprofiler/core/RegisteredThread.h @@ -0,0 +1,164 @@ +/* -*- Mode: C++; tab-width: 2; 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/. */ + +#ifndef RegisteredThread_h +#define RegisteredThread_h + +#include "platform.h" +#include "ThreadInfo.h" + +#include "mozilla/UniquePtr.h" + +namespace mozilla { +namespace baseprofiler { + +// This class contains the state for a single thread that is accessible without +// protection from gPSMutex in platform.cpp. Because there is no external +// protection against data races, it must provide internal protection. Hence +// the "Racy" prefix. +// +class RacyRegisteredThread final { + public: + explicit RacyRegisteredThread(BaseProfilerThreadId aThreadId) + : mThreadId(aThreadId), mSleep(AWAKE), mIsBeingProfiled(false) {} + + ~RacyRegisteredThread() {} + + void SetIsBeingProfiled(bool aIsBeingProfiled) { + mIsBeingProfiled = aIsBeingProfiled; + } + + bool IsBeingProfiled() const { return mIsBeingProfiled; } + + // This is called on every profiler restart. Put things that should happen at + // that time here. + void ReinitializeOnResume() { + // This is needed to cause an initial sample to be taken from sleeping + // threads that had been observed prior to the profiler stopping and + // restarting. Otherwise sleeping threads would not have any samples to + // copy forward while sleeping. + (void)mSleep.compareExchange(SLEEPING_OBSERVED, SLEEPING_NOT_OBSERVED); + } + + // This returns true for the second and subsequent calls in each sleep cycle. + bool CanDuplicateLastSampleDueToSleep() { + if (mSleep == AWAKE) { + return false; + } + + if (mSleep.compareExchange(SLEEPING_NOT_OBSERVED, SLEEPING_OBSERVED)) { + return false; + } + + return true; + } + + // Call this whenever the current thread sleeps. Calling it twice in a row + // without an intervening setAwake() call is an error. + void SetSleeping() { + MOZ_ASSERT(mSleep == AWAKE); + mSleep = SLEEPING_NOT_OBSERVED; + } + + // Call this whenever the current thread wakes. Calling it twice in a row + // without an intervening setSleeping() call is an error. + void SetAwake() { + MOZ_ASSERT(mSleep != AWAKE); + mSleep = AWAKE; + } + + bool IsSleeping() { return mSleep != AWAKE; } + + BaseProfilerThreadId ThreadId() const { return mThreadId; } + + class ProfilingStack& ProfilingStack() { return mProfilingStack; } + const class ProfilingStack& ProfilingStack() const { return mProfilingStack; } + + private: + class ProfilingStack mProfilingStack; + + // mThreadId contains the thread ID of the current thread. It is safe to read + // this from multiple threads concurrently, as it will never be mutated. + const BaseProfilerThreadId mThreadId; + + // mSleep tracks whether the thread is sleeping, and if so, whether it has + // been previously observed. This is used for an optimization: in some cases, + // when a thread is asleep, we duplicate the previous sample, which is + // cheaper than taking a new sample. + // + // mSleep is atomic because it is accessed from multiple threads. + // + // - It is written only by this thread, via setSleeping() and setAwake(). + // + // - It is read by SamplerThread::Run(). + // + // There are two cases where racing between threads can cause an issue. + // + // - If CanDuplicateLastSampleDueToSleep() returns false but that result is + // invalidated before being acted upon, we will take a full sample + // unnecessarily. This is additional work but won't cause any correctness + // issues. (In actual fact, this case is impossible. In order to go from + // CanDuplicateLastSampleDueToSleep() returning false to it returning true + // requires an intermediate call to it in order for mSleep to go from + // SLEEPING_NOT_OBSERVED to SLEEPING_OBSERVED.) + // + // - If CanDuplicateLastSampleDueToSleep() returns true but that result is + // invalidated before being acted upon -- i.e. the thread wakes up before + // DuplicateLastSample() is called -- we will duplicate the previous + // sample. This is inaccurate, but only slightly... we will effectively + // treat the thread as having slept a tiny bit longer than it really did. + // + // This latter inaccuracy could be avoided by moving the + // CanDuplicateLastSampleDueToSleep() check within the thread-freezing code, + // e.g. the section where Tick() is called. But that would reduce the + // effectiveness of the optimization because more code would have to be run + // before we can tell that duplication is allowed. + // + static const int AWAKE = 0; + static const int SLEEPING_NOT_OBSERVED = 1; + static const int SLEEPING_OBSERVED = 2; + Atomic<int> mSleep; + + // Is this thread being profiled? (e.g., should markers be recorded?) + Atomic<bool, MemoryOrdering::Relaxed> mIsBeingProfiled; +}; + +// This class contains information that's relevant to a single thread only +// while that thread is running and registered with the profiler, but +// regardless of whether the profiler is running. All accesses to it are +// protected by the profiler state lock. +class RegisteredThread final { + public: + RegisteredThread(ThreadInfo* aInfo, void* aStackTop); + ~RegisteredThread(); + + class RacyRegisteredThread& RacyRegisteredThread() { + return mRacyRegisteredThread; + } + const class RacyRegisteredThread& RacyRegisteredThread() const { + return mRacyRegisteredThread; + } + + PlatformData* GetPlatformData() const { return mPlatformData.get(); } + const void* StackTop() const { return mStackTop; } + + size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const; + + const RefPtr<ThreadInfo> Info() const { return mThreadInfo; } + + private: + class RacyRegisteredThread mRacyRegisteredThread; + + const UniquePlatformData mPlatformData; + const void* mStackTop; + + const RefPtr<ThreadInfo> mThreadInfo; +}; + +} // namespace baseprofiler +} // namespace mozilla + +#endif // RegisteredThread_h diff --git a/mozglue/baseprofiler/core/ThreadInfo.h b/mozglue/baseprofiler/core/ThreadInfo.h new file mode 100644 index 0000000000..80211396ac --- /dev/null +++ b/mozglue/baseprofiler/core/ThreadInfo.h @@ -0,0 +1,62 @@ +/* -*- Mode: C++; tab-width: 2; 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/. */ + +#ifndef ThreadInfo_h +#define ThreadInfo_h + +#include "mozilla/Atomics.h" +#include "mozilla/BaseProfilerUtils.h" +#include "mozilla/TimeStamp.h" + +namespace mozilla { +namespace baseprofiler { + +// This class contains information about a thread which needs to be stored +// across restarts of the profiler and which can be useful even after the +// thread has stopped running. +// It uses threadsafe refcounting and only contains immutable data. +class ThreadInfo final { + public: + ThreadInfo(const char* aName, BaseProfilerThreadId aThreadId, + bool aIsMainThread, + const TimeStamp& aRegisterTime = TimeStamp::Now()) + : mName(aName), + mRegisterTime(aRegisterTime), + mThreadId(aThreadId), + mIsMainThread(aIsMainThread), + mRefCnt(0) { + MOZ_ASSERT(aThreadId.IsSpecified(), + "Given aThreadId should not be unspecified"); + } + + // Using hand-rolled ref-counting, because RefCounted.h macros don't produce + // the same code between mozglue and libxul, see bug 1536656. + MFBT_API void AddRef() const { ++mRefCnt; } + MFBT_API void Release() const { + MOZ_ASSERT(int32_t(mRefCnt) > 0); + if (--mRefCnt == 0) { + delete this; + } + } + + const char* Name() const { return mName.c_str(); } + TimeStamp RegisterTime() const { return mRegisterTime; } + BaseProfilerThreadId ThreadId() const { return mThreadId; } + bool IsMainThread() const { return mIsMainThread; } + + private: + const std::string mName; + const TimeStamp mRegisterTime; + const BaseProfilerThreadId mThreadId; + const bool mIsMainThread; + + mutable Atomic<int32_t, MemoryOrdering::ReleaseAcquire> mRefCnt; +}; + +} // namespace baseprofiler +} // namespace mozilla + +#endif // ThreadInfo_h diff --git a/mozglue/baseprofiler/core/VTuneProfiler.cpp b/mozglue/baseprofiler/core/VTuneProfiler.cpp new file mode 100644 index 0000000000..2911c39f08 --- /dev/null +++ b/mozglue/baseprofiler/core/VTuneProfiler.cpp @@ -0,0 +1,92 @@ +/* -*- Mode: C++; tab-width: 2; 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/. */ + +#ifdef XP_WIN +# undef UNICODE +# undef _UNICODE +#endif + +#include "VTuneProfiler.h" + +#include <memory> + +#include "BaseProfiler.h" + +namespace mozilla { +namespace baseprofiler { + +VTuneProfiler* VTuneProfiler::mInstance = nullptr; + +void VTuneProfiler::Initialize() { + // This is just a 'dirty trick' to find out if the ittnotify DLL was found. + // If it wasn't this function always returns 0, otherwise it returns + // incrementing numbers, if the library was found this wastes 2 events but + // that should be okay. + // TODO re-implement here if vtune is needed + // __itt_event testEvent = + // __itt_event_create("Test event", strlen("Test event")); + // testEvent = __itt_event_create("Test event 2", strlen("Test event 2")); + + // if (testEvent) { + // mInstance = new VTuneProfiler(); + // } +} + +void VTuneProfiler::Shutdown() {} + +void VTuneProfiler::TraceInternal(const char* aName, TracingKind aKind) { + // TODO re-implement here if vtune is needed + // std::string str(aName); + + // auto iter = mStrings.find(str); + + // __itt_event event; + // if (iter != mStrings.end()) { + // event = iter->second; + // } else { + // event = __itt_event_create(aName, str.length()); + // mStrings.insert({str, event}); + // } + + // if (aKind == TRACING_INTERVAL_START || aKind == TRACING_EVENT) { + // // VTune will consider starts not matched with an end to be single point + // in + // // time events. + // __itt_event_start(event); + // } else { + // __itt_event_end(event); + // } +} + +void VTuneProfiler::RegisterThreadInternal(const char* aName) { + // TODO re-implement here if vtune is needed + // std::string str(aName); + + // if (!str.compare("Main Thread (Base Profiler)")) { + // // Process main thread. + // switch (XRE_GetProcessType()) { + // case GeckoProcessType::GeckoProcessType_Default: + // __itt_thread_set_name("Main Process"); + // break; + // case GeckoProcessType::GeckoProcessType_Content: + // __itt_thread_set_name("Content Process"); + // break; + // case GeckoProcessType::GeckoProcessType_GMPlugin: + // __itt_thread_set_name("Plugin Process"); + // break; + // case GeckoProcessType::GeckoProcessType_GPU: + // __itt_thread_set_name("GPU Process"); + // break; + // default: + // __itt_thread_set_name("Unknown Process"); + // } + // return; + // } + // __itt_thread_set_name(aName); +} + +} // namespace baseprofiler +} // namespace mozilla diff --git a/mozglue/baseprofiler/core/VTuneProfiler.h b/mozglue/baseprofiler/core/VTuneProfiler.h new file mode 100644 index 0000000000..cf94ab7242 --- /dev/null +++ b/mozglue/baseprofiler/core/VTuneProfiler.h @@ -0,0 +1,84 @@ +/* -*- Mode: C++; tab-width: 2; 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/. */ + +#ifndef VTuneProfiler_h +#define VTuneProfiler_h + +// The intent here is to add 0 overhead for regular users. In order to build +// the VTune profiler code at all --enable-vtune-instrumentation needs to be +// set as a build option. Even then, when none of the environment variables +// is specified that allow us to find the ittnotify DLL, these functions +// should be minimal overhead. When starting Firefox under VTune, these +// env vars will be automatically defined, otherwise INTEL_LIBITTNOTIFY32/64 +// should be set to point at the ittnotify DLL. +#ifndef MOZ_VTUNE_INSTRUMENTATION + +# define VTUNE_INIT() +# define VTUNE_SHUTDOWN() + +# define VTUNE_TRACING(name, kind) +# define VTUNE_REGISTER_THREAD(name) + +#else + +# include "BaseProfiler.h" + +// This is the regular Intel header, these functions are actually defined for +// us inside js/src/vtune by an intel C file which actually dynamically resolves +// them to the correct DLL. Through libxul these will 'magically' resolve. +# include "vtune/ittnotify.h" + +# include <stddef.h> +# include <unordered_map> +# include <string> + +namespace mozilla { +namespace baseprofiler { + +class VTuneProfiler { + public: + static void Initialize(); + static void Shutdown(); + + enum TracingKind { + TRACING_EVENT, + TRACING_INTERVAL_START, + TRACING_INTERVAL_END, + }; + + static void Trace(const char* aName, TracingKind aKind) { + if (mInstance) { + mInstance->TraceInternal(aName, aKind); + } + } + static void RegisterThread(const char* aName) { + if (mInstance) { + mInstance->RegisterThreadInternal(aName); + } + } + + private: + void TraceInternal(const char* aName, TracingKind aKind); + void RegisterThreadInternal(const char* aName); + + // This is null when the ittnotify DLL could not be found. + static VTuneProfiler* mInstance; + + std::unordered_map<std::string, __itt_event> mStrings; +}; + +# define VTUNE_INIT() VTuneProfiler::Initialize() +# define VTUNE_SHUTDOWN() VTuneProfiler::Shutdown() + +# define VTUNE_TRACING(name, kind) VTuneProfiler::Trace(name, kind) +# define VTUNE_REGISTER_THREAD(name) VTuneProfiler::RegisterThread(name) + +} // namespace baseprofiler +} // namespace mozilla + +#endif + +#endif /* VTuneProfiler_h */ diff --git a/mozglue/baseprofiler/core/platform-linux-android.cpp b/mozglue/baseprofiler/core/platform-linux-android.cpp new file mode 100644 index 0000000000..7921d9f4fd --- /dev/null +++ b/mozglue/baseprofiler/core/platform-linux-android.cpp @@ -0,0 +1,507 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +// Copyright (c) 2006-2011 The Chromium Authors. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions +// are met: +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in +// the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google, Inc. nor the names of its contributors +// may be used to endorse or promote products derived from this +// software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +// OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +// AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT +// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +// SUCH DAMAGE. + +// This file is used for both Linux and Android. + +#include <stdio.h> +#include <math.h> + +#include <pthread.h> +#if defined(GP_OS_freebsd) +# include <sys/thr.h> +#endif +#include <semaphore.h> +#include <signal.h> +#include <sys/time.h> +#include <sys/resource.h> +#include <sys/syscall.h> +#include <sys/types.h> +#include <stdlib.h> +#include <sched.h> +#include <ucontext.h> +// Ubuntu Dapper requires memory pages to be marked as +// executable. Otherwise, OS raises an exception when executing code +// in that page. +#include <sys/types.h> // mmap & munmap +#include <sys/mman.h> // mmap & munmap +#include <sys/stat.h> // open +#include <fcntl.h> // open +#include <unistd.h> // sysconf +#include <semaphore.h> +#ifdef __GLIBC__ +# include <execinfo.h> // backtrace, backtrace_symbols +#endif // def __GLIBC__ +#include <strings.h> // index +#include <errno.h> +#include <stdarg.h> + +#include "prenv.h" +#include "mozilla/PodOperations.h" +#include "mozilla/DebugOnly.h" + +#include <string.h> +#include <list> + +using namespace mozilla; + +namespace mozilla { +namespace baseprofiler { + +static int64_t MicrosecondsSince1970() { + struct timeval tv; + gettimeofday(&tv, NULL); + return int64_t(tv.tv_sec) * 1000000 + int64_t(tv.tv_usec); +} + +void* GetStackTop(void* aGuess) { return aGuess; } + +static void PopulateRegsFromContext(Registers& aRegs, ucontext_t* aContext) { + aRegs.mContext = aContext; + mcontext_t& mcontext = aContext->uc_mcontext; + + // Extracting the sample from the context is extremely machine dependent. +#if defined(GP_PLAT_x86_linux) || defined(GP_PLAT_x86_android) + aRegs.mPC = reinterpret_cast<Address>(mcontext.gregs[REG_EIP]); + aRegs.mSP = reinterpret_cast<Address>(mcontext.gregs[REG_ESP]); + aRegs.mFP = reinterpret_cast<Address>(mcontext.gregs[REG_EBP]); + aRegs.mLR = 0; +#elif defined(GP_PLAT_amd64_linux) || defined(GP_PLAT_amd64_android) + aRegs.mPC = reinterpret_cast<Address>(mcontext.gregs[REG_RIP]); + aRegs.mSP = reinterpret_cast<Address>(mcontext.gregs[REG_RSP]); + aRegs.mFP = reinterpret_cast<Address>(mcontext.gregs[REG_RBP]); + aRegs.mLR = 0; +#elif defined(GP_PLAT_amd64_freebsd) + aRegs.mPC = reinterpret_cast<Address>(mcontext.mc_rip); + aRegs.mSP = reinterpret_cast<Address>(mcontext.mc_rsp); + aRegs.mFP = reinterpret_cast<Address>(mcontext.mc_rbp); + aRegs.mLR = 0; +#elif defined(GP_PLAT_arm_linux) || defined(GP_PLAT_arm_android) + aRegs.mPC = reinterpret_cast<Address>(mcontext.arm_pc); + aRegs.mSP = reinterpret_cast<Address>(mcontext.arm_sp); + aRegs.mFP = reinterpret_cast<Address>(mcontext.arm_fp); + aRegs.mLR = reinterpret_cast<Address>(mcontext.arm_lr); +#elif defined(GP_PLAT_arm64_linux) || defined(GP_PLAT_arm64_android) + aRegs.mPC = reinterpret_cast<Address>(mcontext.pc); + aRegs.mSP = reinterpret_cast<Address>(mcontext.sp); + aRegs.mFP = reinterpret_cast<Address>(mcontext.regs[29]); + aRegs.mLR = reinterpret_cast<Address>(mcontext.regs[30]); +#elif defined(GP_PLAT_arm64_freebsd) + aRegs.mPC = reinterpret_cast<Address>(mcontext.mc_gpregs.gp_elr); + aRegs.mSP = reinterpret_cast<Address>(mcontext.mc_gpregs.gp_sp); + aRegs.mFP = reinterpret_cast<Address>(mcontext.mc_gpregs.gp_x[29]); + aRegs.mLR = reinterpret_cast<Address>(mcontext.mc_gpregs.gp_lr); +#elif defined(GP_PLAT_mips64_linux) || defined(GP_PLAT_mips64_android) + aRegs.mPC = reinterpret_cast<Address>(mcontext.pc); + aRegs.mSP = reinterpret_cast<Address>(mcontext.gregs[29]); + aRegs.mFP = reinterpret_cast<Address>(mcontext.gregs[30]); + +#else +# error "bad platform" +#endif +} + +#if defined(GP_OS_android) +# define SYS_tgkill __NR_tgkill +#endif + +#if defined(GP_OS_linux) || defined(GP_OS_android) +int tgkill(pid_t tgid, pid_t tid, int signalno) { + return syscall(SYS_tgkill, tgid, tid, signalno); +} +#endif + +#if defined(GP_OS_freebsd) +# define tgkill thr_kill2 +#endif + +class PlatformData { + public: + explicit PlatformData(BaseProfilerThreadId aThreadId) {} + + ~PlatformData() {} +}; + +//////////////////////////////////////////////////////////////////////// +// BEGIN Sampler target specifics + +// The only way to reliably interrupt a Linux thread and inspect its register +// and stack state is by sending a signal to it, and doing the work inside the +// signal handler. But we don't want to run much code inside the signal +// handler, since POSIX severely restricts what we can do in signal handlers. +// So we use a system of semaphores to suspend the thread and allow the +// sampler thread to do all the work of unwinding and copying out whatever +// data it wants. +// +// A four-message protocol is used to reliably suspend and later resume the +// thread to be sampled (the samplee): +// +// Sampler (signal sender) thread Samplee (thread to be sampled) +// +// Prepare the SigHandlerCoordinator +// and point sSigHandlerCoordinator at it +// +// send SIGPROF to samplee ------- MSG 1 ----> (enter signal handler) +// wait(mMessage2) Copy register state +// into sSigHandlerCoordinator +// <------ MSG 2 ----- post(mMessage2) +// Samplee is now suspended. wait(mMessage3) +// Examine its stack/register +// state at leisure +// +// Release samplee: +// post(mMessage3) ------- MSG 3 -----> +// wait(mMessage4) Samplee now resumes. Tell +// the sampler that we are done. +// <------ MSG 4 ------ post(mMessage4) +// Now we know the samplee's signal (leave signal handler) +// handler has finished using +// sSigHandlerCoordinator. We can +// safely reuse it for some other thread. +// + +// A type used to coordinate between the sampler (signal sending) thread and +// the thread currently being sampled (the samplee, which receives the +// signals). +// +// The first message is sent using a SIGPROF signal delivery. The subsequent +// three are sent using sem_wait/sem_post pairs. They are named accordingly +// in the following struct. +struct SigHandlerCoordinator { + SigHandlerCoordinator() { + PodZero(&mUContext); + int r = sem_init(&mMessage2, /* pshared */ 0, 0); + r |= sem_init(&mMessage3, /* pshared */ 0, 0); + r |= sem_init(&mMessage4, /* pshared */ 0, 0); + MOZ_ASSERT(r == 0); + (void)r; + } + + ~SigHandlerCoordinator() { + int r = sem_destroy(&mMessage2); + r |= sem_destroy(&mMessage3); + r |= sem_destroy(&mMessage4); + MOZ_ASSERT(r == 0); + (void)r; + } + + sem_t mMessage2; // To sampler: "context is in sSigHandlerCoordinator" + sem_t mMessage3; // To samplee: "resume" + sem_t mMessage4; // To sampler: "finished with sSigHandlerCoordinator" + ucontext_t mUContext; // Context at signal +}; + +struct SigHandlerCoordinator* Sampler::sSigHandlerCoordinator = nullptr; + +static void SigprofHandler(int aSignal, siginfo_t* aInfo, void* aContext) { + // Avoid TSan warning about clobbering errno. + int savedErrno = errno; + + MOZ_ASSERT(aSignal == SIGPROF); + MOZ_ASSERT(Sampler::sSigHandlerCoordinator); + + // By sending us this signal, the sampler thread has sent us message 1 in + // the comment above, with the meaning "|sSigHandlerCoordinator| is ready + // for use, please copy your register context into it." + Sampler::sSigHandlerCoordinator->mUContext = + *static_cast<ucontext_t*>(aContext); + + // Send message 2: tell the sampler thread that the context has been copied + // into |sSigHandlerCoordinator->mUContext|. sem_post can never fail by + // being interrupted by a signal, so there's no loop around this call. + int r = sem_post(&Sampler::sSigHandlerCoordinator->mMessage2); + MOZ_ASSERT(r == 0); + + // At this point, the sampler thread assumes we are suspended, so we must + // not touch any global state here. + + // Wait for message 3: the sampler thread tells us to resume. + while (true) { + r = sem_wait(&Sampler::sSigHandlerCoordinator->mMessage3); + if (r == -1 && errno == EINTR) { + // Interrupted by a signal. Try again. + continue; + } + // We don't expect any other kind of failure + MOZ_ASSERT(r == 0); + break; + } + + // Send message 4: tell the sampler thread that we are finished accessing + // |sSigHandlerCoordinator|. After this point it is not safe to touch + // |sSigHandlerCoordinator|. + r = sem_post(&Sampler::sSigHandlerCoordinator->mMessage4); + MOZ_ASSERT(r == 0); + + errno = savedErrno; +} + +Sampler::Sampler(PSLockRef aLock) : mMyPid(profiler_current_process_id()) { +#if defined(USE_EHABI_STACKWALK) + EHABIStackWalkInit(); +#endif + + // NOTE: We don't initialize LUL here, instead initializing it in + // SamplerThread's constructor. This is because with the + // profiler_suspend_and_sample_thread entry point, we want to be able to + // sample without waiting for LUL to be initialized. + + // Request profiling signals. + struct sigaction sa; + sa.sa_sigaction = SigprofHandler; + sigemptyset(&sa.sa_mask); + sa.sa_flags = SA_RESTART | SA_SIGINFO; + if (sigaction(SIGPROF, &sa, &mOldSigprofHandler) != 0) { + MOZ_CRASH("Error installing SIGPROF handler in the profiler"); + } +} + +void Sampler::Disable(PSLockRef aLock) { + // Restore old signal handler. This is global state so it's important that + // we do it now, while gPSMutex is locked. + sigaction(SIGPROF, &mOldSigprofHandler, 0); +} + +template <typename Func> +void Sampler::SuspendAndSampleAndResumeThread( + PSLockRef aLock, const RegisteredThread& aRegisteredThread, + const TimeStamp& aNow, const Func& aProcessRegs) { + // Only one sampler thread can be sampling at once. So we expect to have + // complete control over |sSigHandlerCoordinator|. + MOZ_ASSERT(!sSigHandlerCoordinator); + + if (!mSamplerTid.IsSpecified()) { + mSamplerTid = profiler_current_thread_id(); + } + BaseProfilerThreadId sampleeTid = aRegisteredThread.Info()->ThreadId(); + MOZ_RELEASE_ASSERT(sampleeTid != mSamplerTid); + + //----------------------------------------------------------------// + // Suspend the samplee thread and get its context. + + SigHandlerCoordinator coord; // on sampler thread's stack + sSigHandlerCoordinator = &coord; + + // Send message 1 to the samplee (the thread to be sampled), by + // signalling at it. + // This could fail if the thread doesn't exist anymore. + int r = tgkill(mMyPid.ToNumber(), sampleeTid.ToNumber(), SIGPROF); + if (r == 0) { + // Wait for message 2 from the samplee, indicating that the context + // is available and that the thread is suspended. + while (true) { + r = sem_wait(&sSigHandlerCoordinator->mMessage2); + if (r == -1 && errno == EINTR) { + // Interrupted by a signal. Try again. + continue; + } + // We don't expect any other kind of failure. + MOZ_ASSERT(r == 0); + break; + } + + //----------------------------------------------------------------// + // Sample the target thread. + + // WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING + // + // The profiler's "critical section" begins here. In the critical section, + // we must not do any dynamic memory allocation, nor try to acquire any lock + // or any other unshareable resource. This is because the thread to be + // sampled has been suspended at some entirely arbitrary point, and we have + // no idea which unsharable resources (locks, essentially) it holds. So any + // attempt to acquire any lock, including the implied locks used by the + // malloc implementation, risks deadlock. This includes TimeStamp::Now(), + // which gets a lock on Windows. + + // The samplee thread is now frozen and sSigHandlerCoordinator->mUContext is + // valid. We can poke around in it and unwind its stack as we like. + + // Extract the current register values. + Registers regs; + PopulateRegsFromContext(regs, &sSigHandlerCoordinator->mUContext); + aProcessRegs(regs, aNow); + + //----------------------------------------------------------------// + // Resume the target thread. + + // Send message 3 to the samplee, which tells it to resume. + r = sem_post(&sSigHandlerCoordinator->mMessage3); + MOZ_ASSERT(r == 0); + + // Wait for message 4 from the samplee, which tells us that it has + // finished with |sSigHandlerCoordinator|. + while (true) { + r = sem_wait(&sSigHandlerCoordinator->mMessage4); + if (r == -1 && errno == EINTR) { + continue; + } + MOZ_ASSERT(r == 0); + break; + } + + // The profiler's critical section ends here. After this point, none of the + // critical section limitations documented above apply. + // + // WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING + } + + // This isn't strictly necessary, but doing so does help pick up anomalies + // in which the signal handler is running when it shouldn't be. + sSigHandlerCoordinator = nullptr; +} + +// END Sampler target specifics +//////////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////////// +// BEGIN SamplerThread target specifics + +static void* ThreadEntry(void* aArg) { + auto thread = static_cast<SamplerThread*>(aArg); + thread->Run(); + return nullptr; +} + +SamplerThread::SamplerThread(PSLockRef aLock, uint32_t aActivityGeneration, + double aIntervalMilliseconds, uint32_t aFeatures) + : mSampler(aLock), + mActivityGeneration(aActivityGeneration), + mIntervalMicroseconds( + std::max(1, int(floor(aIntervalMilliseconds * 1000 + 0.5)))) { +#if defined(USE_LUL_STACKWALK) + lul::LUL* lul = CorePS::Lul(aLock); + if (!lul && ProfilerFeature::HasStackWalkEnabled(aFeatures)) { + CorePS::SetLul(aLock, MakeUnique<lul::LUL>(logging_sink_for_LUL)); + // Read all the unwind info currently available. + lul = CorePS::Lul(aLock); + read_procmaps(lul); + + // Switch into unwind mode. After this point, we can't add or remove any + // unwind info to/from this LUL instance. The only thing we can do with + // it is Unwind() calls. + lul->EnableUnwinding(); + + // Has a test been requested? + if (getenv("MOZ_PROFILER_LUL_TEST")) { + int nTests = 0, nTestsPassed = 0; + RunLulUnitTests(&nTests, &nTestsPassed, lul); + } + } +#endif + + // Start the sampling thread. It repeatedly sends a SIGPROF signal. Sending + // the signal ourselves instead of relying on itimer provides much better + // accuracy. + if (pthread_create(&mThread, nullptr, ThreadEntry, this) != 0) { + MOZ_CRASH("pthread_create failed"); + } +} + +SamplerThread::~SamplerThread() { pthread_join(mThread, nullptr); } + +void SamplerThread::SleepMicro(uint32_t aMicroseconds) { + if (aMicroseconds >= 1000000) { + // Use usleep for larger intervals, because the nanosleep + // code below only supports intervals < 1 second. + MOZ_ALWAYS_TRUE(!::usleep(aMicroseconds)); + return; + } + + struct timespec ts; + ts.tv_sec = 0; + ts.tv_nsec = aMicroseconds * 1000UL; + + int rv = ::nanosleep(&ts, &ts); + + while (rv != 0 && errno == EINTR) { + // Keep waiting in case of interrupt. + // nanosleep puts the remaining time back into ts. + rv = ::nanosleep(&ts, &ts); + } + + MOZ_ASSERT(!rv, "nanosleep call failed"); +} + +void SamplerThread::Stop(PSLockRef aLock) { + // Restore old signal handler. This is global state so it's important that + // we do it now, while gPSMutex is locked. It's safe to do this now even + // though this SamplerThread is still alive, because the next time the main + // loop of Run() iterates it won't get past the mActivityGeneration check, + // and so won't send any signals. + mSampler.Disable(aLock); +} + +// END SamplerThread target specifics +//////////////////////////////////////////////////////////////////////// + +#if defined(GP_OS_linux) || defined(GP_OS_freebsd) + +// We use pthread_atfork() to temporarily disable signal delivery during any +// fork() call. Without that, fork() can be repeatedly interrupted by signal +// delivery, requiring it to be repeatedly restarted, which can lead to *long* +// delays. See bug 837390. +// +// We provide no paf_child() function to run in the child after forking. This +// is fine because we always immediately exec() after fork(), and exec() +// clobbers all process state. Also, we don't want the sampler to resume in the +// child process between fork() and exec(), it would be wasteful. +// +// Unfortunately all this is only doable on non-Android because Bionic doesn't +// have pthread_atfork. + +// In the parent, before the fork, increase gSkipSampling to ensure that +// profiler sampling loops will be skipped. There could be one in progress now, +// causing a small delay, but further sampling will be skipped, allowing `fork` +// to complete. +static void paf_prepare() { ++gSkipSampling; } + +// In the parent, after the fork, decrease gSkipSampling to let the sampler +// resume sampling (unless other places have made it non-zero as well). +static void paf_parent() { --gSkipSampling; } + +static void PlatformInit(PSLockRef aLock) { + // Set up the fork handlers. + pthread_atfork(paf_prepare, paf_parent, nullptr); +} + +#else + +static void PlatformInit(PSLockRef aLock) {} + +#endif + +#if defined(HAVE_NATIVE_UNWIND) +// TODO port getcontext from breakpad, if profiler_get_backtrace is needed. +# define REGISTERS_SYNC_POPULATE(regs) \ + MOZ_CRASH("profiler_get_backtrace() unsupported"); +#endif + +} // namespace baseprofiler +} // namespace mozilla diff --git a/mozglue/baseprofiler/core/platform-macos.cpp b/mozglue/baseprofiler/core/platform-macos.cpp new file mode 100644 index 0000000000..6e6e801f1f --- /dev/null +++ b/mozglue/baseprofiler/core/platform-macos.cpp @@ -0,0 +1,220 @@ +/* -*- 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 <unistd.h> +#include <sys/mman.h> +#include <mach/mach_init.h> +#include <mach-o/getsect.h> + +#include <AvailabilityMacros.h> + +#include <pthread.h> +#include <semaphore.h> +#include <signal.h> +#include <libkern/OSAtomic.h> +#include <mach/mach.h> +#include <mach/semaphore.h> +#include <mach/task.h> +#include <mach/thread_act.h> +#include <mach/vm_statistics.h> +#include <sys/time.h> +#include <sys/resource.h> +#include <sys/syscall.h> +#include <sys/types.h> +#include <sys/sysctl.h> +#include <stdarg.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> +#include <math.h> + +// this port is based off of v8 svn revision 9837 + +namespace mozilla { +namespace baseprofiler { + +static int64_t MicrosecondsSince1970() { + struct timeval tv; + gettimeofday(&tv, NULL); + return int64_t(tv.tv_sec) * 1000000 + int64_t(tv.tv_usec); +} + +void* GetStackTop(void* aGuess) { + pthread_t thread = pthread_self(); + return pthread_get_stackaddr_np(thread); +} + +class PlatformData { + public: + explicit PlatformData(BaseProfilerThreadId aThreadId) + : mProfiledThread(mach_thread_self()) {} + + ~PlatformData() { + // Deallocate Mach port for thread. + mach_port_deallocate(mach_task_self(), mProfiledThread); + } + + thread_act_t ProfiledThread() { return mProfiledThread; } + + private: + // Note: for mProfiledThread Mach primitives are used instead of pthread's + // because the latter doesn't provide thread manipulation primitives required. + // For details, consult "Mac OS X Internals" book, Section 7.3. + thread_act_t mProfiledThread; +}; + +//////////////////////////////////////////////////////////////////////// +// BEGIN Sampler target specifics + +Sampler::Sampler(PSLockRef aLock) {} + +void Sampler::Disable(PSLockRef aLock) {} + +template <typename Func> +void Sampler::SuspendAndSampleAndResumeThread( + PSLockRef aLock, const RegisteredThread& aRegisteredThread, + const TimeStamp& aNow, const Func& aProcessRegs) { + thread_act_t samplee_thread = + aRegisteredThread.GetPlatformData()->ProfiledThread(); + + //----------------------------------------------------------------// + // Suspend the samplee thread and get its context. + + // We're using thread_suspend on OS X because pthread_kill (which is what we + // at one time used on Linux) has less consistent performance and causes + // strange crashes, see bug 1166778 and bug 1166808. thread_suspend + // is also just a lot simpler to use. + + if (KERN_SUCCESS != thread_suspend(samplee_thread)) { + return; + } + + //----------------------------------------------------------------// + // Sample the target thread. + + // WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING + // + // The profiler's "critical section" begins here. We must be very careful + // what we do here, or risk deadlock. See the corresponding comment in + // platform-linux-android.cpp for details. + +#if defined(__x86_64__) + thread_state_flavor_t flavor = x86_THREAD_STATE64; + x86_thread_state64_t state; + mach_msg_type_number_t count = x86_THREAD_STATE64_COUNT; +# if __DARWIN_UNIX03 +# define REGISTER_FIELD(name) __r##name +# else +# define REGISTER_FIELD(name) r##name +# endif // __DARWIN_UNIX03 +#elif defined(__aarch64__) + thread_state_flavor_t flavor = ARM_THREAD_STATE64; + arm_thread_state64_t state; + mach_msg_type_number_t count = ARM_THREAD_STATE64_COUNT; +# if __DARWIN_UNIX03 +# define REGISTER_FIELD(name) __##name +# else +# define REGISTER_FIELD(name) name +# endif // __DARWIN_UNIX03 +#else +# error "unknown architecture" +#endif + + if (thread_get_state(samplee_thread, flavor, + reinterpret_cast<natural_t*>(&state), + &count) == KERN_SUCCESS) { + Registers regs; +#if defined(__x86_64__) + regs.mPC = reinterpret_cast<Address>(state.REGISTER_FIELD(ip)); + regs.mSP = reinterpret_cast<Address>(state.REGISTER_FIELD(sp)); + regs.mFP = reinterpret_cast<Address>(state.REGISTER_FIELD(bp)); + regs.mLR = 0; +#elif defined(__aarch64__) + regs.mPC = reinterpret_cast<Address>(state.REGISTER_FIELD(pc)); + regs.mSP = reinterpret_cast<Address>(state.REGISTER_FIELD(sp)); + regs.mFP = reinterpret_cast<Address>(state.REGISTER_FIELD(fp)); + regs.mLR = reinterpret_cast<Address>(state.REGISTER_FIELD(lr)); +#else +# error "unknown architecture" +#endif + + aProcessRegs(regs, aNow); + } + +#undef REGISTER_FIELD + + //----------------------------------------------------------------// + // Resume the target thread. + + thread_resume(samplee_thread); + + // The profiler's critical section ends here. + // + // WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING +} + +// END Sampler target specifics +//////////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////////// +// BEGIN SamplerThread target specifics + +static void* ThreadEntry(void* aArg) { + auto thread = static_cast<SamplerThread*>(aArg); + thread->Run(); + return nullptr; +} + +SamplerThread::SamplerThread(PSLockRef aLock, uint32_t aActivityGeneration, + double aIntervalMilliseconds, uint32_t aFeatures) + : mSampler(aLock), + mActivityGeneration(aActivityGeneration), + mIntervalMicroseconds( + std::max(1, int(floor(aIntervalMilliseconds * 1000 + 0.5)))), + mThread{nullptr} { + pthread_attr_t* attr_ptr = nullptr; + if (pthread_create(&mThread, attr_ptr, ThreadEntry, this) != 0) { + MOZ_CRASH("pthread_create failed"); + } +} + +SamplerThread::~SamplerThread() { pthread_join(mThread, nullptr); } + +void SamplerThread::SleepMicro(uint32_t aMicroseconds) { + usleep(aMicroseconds); + // FIXME: the OSX 10.12 page for usleep says "The usleep() function is + // obsolescent. Use nanosleep(2) instead." This implementation could be + // merged with the linux-android version. Also, this doesn't handle the + // case where the usleep call is interrupted by a signal. +} + +void SamplerThread::Stop(PSLockRef aLock) { mSampler.Disable(aLock); } + +// END SamplerThread target specifics +//////////////////////////////////////////////////////////////////////// + +static void PlatformInit(PSLockRef aLock) {} + +// clang-format off +#if defined(HAVE_NATIVE_UNWIND) +// Derive the stack pointer from the frame pointer. The 0x10 offset is +// 8 bytes for the previous frame pointer and 8 bytes for the return +// address both stored on the stack after at the beginning of the current +// frame. +# define REGISTERS_SYNC_POPULATE(regs) \ + regs.mSP = reinterpret_cast<Address>(__builtin_frame_address(0)) + 0x10; \ + _Pragma("GCC diagnostic push") \ + _Pragma("GCC diagnostic ignored \"-Wframe-address\"") \ + regs.mFP = reinterpret_cast<Address>(__builtin_frame_address(1)); \ + _Pragma("GCC diagnostic pop") \ + regs.mPC = reinterpret_cast<Address>( \ + __builtin_extract_return_addr(__builtin_return_address(0))); \ + regs.mLR = 0; +#endif +// clang-format on + +} // namespace baseprofiler +} // namespace mozilla diff --git a/mozglue/baseprofiler/core/platform-win32.cpp b/mozglue/baseprofiler/core/platform-win32.cpp new file mode 100644 index 0000000000..808bd9d618 --- /dev/null +++ b/mozglue/baseprofiler/core/platform-win32.cpp @@ -0,0 +1,348 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +// Copyright (c) 2006-2011 The Chromium Authors. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions +// are met: +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in +// the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google, Inc. nor the names of its contributors +// may be used to endorse or promote products derived from this +// software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +// OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +// AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT +// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +// SUCH DAMAGE. + +#include <windows.h> +#include <mmsystem.h> +#include <process.h> + +#include "nsWindowsDllInterceptor.h" +#include "mozilla/StackWalk_windows.h" +#include "mozilla/WindowsVersion.h" + +namespace mozilla { +namespace baseprofiler { + +static int64_t MicrosecondsSince1970() { + int64_t prt; + FILETIME ft; + SYSTEMTIME st; + + GetSystemTime(&st); + SystemTimeToFileTime(&st, &ft); + static_assert(sizeof(ft) == sizeof(prt), "Expect FILETIME to be 64 bits"); + memcpy(&prt, &ft, sizeof(prt)); + const int64_t epochBias = 116444736000000000LL; + prt = (prt - epochBias) / 10; + + return prt; +} + +void* GetStackTop(void* aGuess) { + PNT_TIB pTib = reinterpret_cast<PNT_TIB>(NtCurrentTeb()); + return reinterpret_cast<void*>(pTib->StackBase); +} + +static void PopulateRegsFromContext(Registers& aRegs, CONTEXT* aContext) { +#if defined(GP_ARCH_amd64) + aRegs.mPC = reinterpret_cast<Address>(aContext->Rip); + aRegs.mSP = reinterpret_cast<Address>(aContext->Rsp); + aRegs.mFP = reinterpret_cast<Address>(aContext->Rbp); + aRegs.mLR = 0; +#elif defined(GP_ARCH_x86) + aRegs.mPC = reinterpret_cast<Address>(aContext->Eip); + aRegs.mSP = reinterpret_cast<Address>(aContext->Esp); + aRegs.mFP = reinterpret_cast<Address>(aContext->Ebp); + aRegs.mLR = 0; +#elif defined(GP_ARCH_arm64) + aRegs.mPC = reinterpret_cast<Address>(aContext->Pc); + aRegs.mSP = reinterpret_cast<Address>(aContext->Sp); + aRegs.mFP = reinterpret_cast<Address>(aContext->Fp); + aRegs.mLR = reinterpret_cast<Address>(aContext->Lr); +#else +# error "bad arch" +#endif +} + +// Gets a real (i.e. not pseudo) handle for the current thread, with the +// permissions needed for profiling. +// @return a real HANDLE for the current thread. +static HANDLE GetRealCurrentThreadHandleForProfiling() { + HANDLE realCurrentThreadHandle; + if (!::DuplicateHandle( + ::GetCurrentProcess(), ::GetCurrentThread(), ::GetCurrentProcess(), + &realCurrentThreadHandle, + THREAD_GET_CONTEXT | THREAD_SUSPEND_RESUME | THREAD_QUERY_INFORMATION, + FALSE, 0)) { + return nullptr; + } + + return realCurrentThreadHandle; +} + +class PlatformData { + public: + // Get a handle to the calling thread. This is the thread that we are + // going to profile. We need a real handle because we are going to use it in + // the sampler thread. + explicit PlatformData(BaseProfilerThreadId aThreadId) + : mProfiledThread(GetRealCurrentThreadHandleForProfiling()) { + MOZ_ASSERT(DWORD(aThreadId.ToNumber()) == ::GetCurrentThreadId()); + } + + ~PlatformData() { + if (mProfiledThread != nullptr) { + CloseHandle(mProfiledThread); + mProfiledThread = nullptr; + } + } + + HANDLE ProfiledThread() { return mProfiledThread; } + + private: + HANDLE mProfiledThread; +}; + +#if defined(USE_MOZ_STACK_WALK) +HANDLE +GetThreadHandle(PlatformData* aData) { return aData->ProfiledThread(); } +#endif + +static const HANDLE kNoThread = INVALID_HANDLE_VALUE; + +//////////////////////////////////////////////////////////////////////// +// BEGIN Sampler target specifics + +Sampler::Sampler(PSLockRef aLock) {} + +void Sampler::Disable(PSLockRef aLock) {} + +template <typename Func> +void Sampler::SuspendAndSampleAndResumeThread( + PSLockRef aLock, const RegisteredThread& aRegisteredThread, + const TimeStamp& aNow, const Func& aProcessRegs) { + HANDLE profiled_thread = + aRegisteredThread.GetPlatformData()->ProfiledThread(); + if (profiled_thread == nullptr) { + return; + } + + // Context used for sampling the register state of the profiled thread. + CONTEXT context; + memset(&context, 0, sizeof(context)); + + //----------------------------------------------------------------// + // Suspend the samplee thread and get its context. + + static const DWORD kSuspendFailed = static_cast<DWORD>(-1); + if (SuspendThread(profiled_thread) == kSuspendFailed) { + return; + } + + // SuspendThread is asynchronous, so the thread may still be running. + // Call GetThreadContext first to ensure the thread is really suspended. + // See https://blogs.msdn.microsoft.com/oldnewthing/20150205-00/?p=44743. + + // Using only CONTEXT_CONTROL is faster but on 64-bit it causes crashes in + // RtlVirtualUnwind (see bug 1120126) so we set all the flags. +#if defined(GP_ARCH_amd64) + context.ContextFlags = CONTEXT_FULL; +#else + context.ContextFlags = CONTEXT_CONTROL; +#endif + if (!GetThreadContext(profiled_thread, &context)) { + ResumeThread(profiled_thread); + return; + } + + //----------------------------------------------------------------// + // Sample the target thread. + + // WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING + // + // The profiler's "critical section" begins here. We must be very careful + // what we do here, or risk deadlock. See the corresponding comment in + // platform-linux-android.cpp for details. + + Registers regs; + PopulateRegsFromContext(regs, &context); + aProcessRegs(regs, aNow); + + //----------------------------------------------------------------// + // Resume the target thread. + + ResumeThread(profiled_thread); + + // The profiler's critical section ends here. + // + // WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING +} + +// END Sampler target specifics +//////////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////////// +// BEGIN SamplerThread target specifics + +static unsigned int __stdcall ThreadEntry(void* aArg) { + auto thread = static_cast<SamplerThread*>(aArg); + thread->Run(); + return 0; +} + +SamplerThread::SamplerThread(PSLockRef aLock, uint32_t aActivityGeneration, + double aIntervalMilliseconds, uint32_t aFeatures) + : mSampler(aLock), + mActivityGeneration(aActivityGeneration), + mIntervalMicroseconds( + std::max(1, int(floor(aIntervalMilliseconds * 1000 + 0.5)))), + mNoTimerResolutionChange( + ProfilerFeature::HasNoTimerResolutionChange(aFeatures)) { + if ((!mNoTimerResolutionChange) && (mIntervalMicroseconds < 10 * 1000)) { + // By default the timer resolution (which tends to be 1/64Hz, around 16ms) + // is not changed. However, if the requested interval is sufficiently low, + // the resolution will be adjusted to match. Note that this affects all + // timers in Firefox, and could therefore hide issues while profiling. This + // change may be prevented with the "notimerresolutionchange" feature. + ::timeBeginPeriod(mIntervalMicroseconds / 1000); + } + + // Create a new thread. It is important to use _beginthreadex() instead of + // the Win32 function CreateThread(), because the CreateThread() does not + // initialize thread-specific structures in the C runtime library. + mThread = reinterpret_cast<HANDLE>(_beginthreadex(nullptr, + /* stack_size */ 0, + ThreadEntry, this, + /* initflag */ 0, nullptr)); + if (mThread == 0) { + MOZ_CRASH("_beginthreadex failed"); + } +} + +SamplerThread::~SamplerThread() { + WaitForSingleObject(mThread, INFINITE); + + // Close our own handle for the thread. + if (mThread != kNoThread) { + CloseHandle(mThread); + } +} + +void SamplerThread::SleepMicro(uint32_t aMicroseconds) { + // For now, keep the old behaviour of minimum Sleep(1), even for + // smaller-than-usual sleeps after an overshoot, unless the user has + // explicitly opted into a sub-millisecond profiler interval. + if (mIntervalMicroseconds >= 1000) { + ::Sleep(std::max(1u, aMicroseconds / 1000)); + } else { + TimeStamp start = TimeStamp::Now(); + TimeStamp end = start + TimeDuration::FromMicroseconds(aMicroseconds); + + // First, sleep for as many whole milliseconds as possible. + if (aMicroseconds >= 1000) { + ::Sleep(aMicroseconds / 1000); + } + + // Then, spin until enough time has passed. + while (TimeStamp::Now() < end) { + YieldProcessor(); + } + } +} + +void SamplerThread::Stop(PSLockRef aLock) { + if ((!mNoTimerResolutionChange) && (mIntervalMicroseconds < 10 * 1000)) { + // Disable any timer resolution changes we've made. Do it now while + // gPSMutex is locked, i.e. before any other SamplerThread can be created + // and call ::timeBeginPeriod(). + // + // It's safe to do this now even though this SamplerThread is still alive, + // because the next time the main loop of Run() iterates it won't get past + // the mActivityGeneration check, and so it won't make any more ::Sleep() + // calls. + ::timeEndPeriod(mIntervalMicroseconds / 1000); + } + + mSampler.Disable(aLock); +} + +// END SamplerThread target specifics +//////////////////////////////////////////////////////////////////////// + +static void PlatformInit(PSLockRef aLock) {} + +#if defined(HAVE_NATIVE_UNWIND) +# define REGISTERS_SYNC_POPULATE(regs) \ + CONTEXT context; \ + RtlCaptureContext(&context); \ + PopulateRegsFromContext(regs, &context); +#endif + +#if defined(GP_PLAT_amd64_windows) +static WindowsDllInterceptor NtDllIntercept; + +typedef NTSTATUS(NTAPI* LdrUnloadDll_func)(HMODULE module); +static WindowsDllInterceptor::FuncHookType<LdrUnloadDll_func> stub_LdrUnloadDll; + +static NTSTATUS NTAPI patched_LdrUnloadDll(HMODULE module) { + // Prevent the stack walker from suspending this thread when LdrUnloadDll + // holds the RtlLookupFunctionEntry lock. + AutoSuppressStackWalking suppress; + return stub_LdrUnloadDll(module); +} + +// These pointers are disguised as PVOID to avoid pulling in obscure headers +typedef PVOID(WINAPI* LdrResolveDelayLoadedAPI_func)( + PVOID ParentModuleBase, PVOID DelayloadDescriptor, PVOID FailureDllHook, + PVOID FailureSystemHook, PVOID ThunkAddress, ULONG Flags); +static WindowsDllInterceptor::FuncHookType<LdrResolveDelayLoadedAPI_func> + stub_LdrResolveDelayLoadedAPI; + +static PVOID WINAPI patched_LdrResolveDelayLoadedAPI( + PVOID ParentModuleBase, PVOID DelayloadDescriptor, PVOID FailureDllHook, + PVOID FailureSystemHook, PVOID ThunkAddress, ULONG Flags) { + // Prevent the stack walker from suspending this thread when + // LdrResolveDelayLoadAPI holds the RtlLookupFunctionEntry lock. + AutoSuppressStackWalking suppress; + return stub_LdrResolveDelayLoadedAPI(ParentModuleBase, DelayloadDescriptor, + FailureDllHook, FailureSystemHook, + ThunkAddress, Flags); +} + +MFBT_API void InitializeWin64ProfilerHooks() { + // This function could be called by both profilers, but we only want to run + // it once. + static bool ran = false; + if (ran) { + return; + } + ran = true; + + NtDllIntercept.Init("ntdll.dll"); + stub_LdrUnloadDll.Set(NtDllIntercept, "LdrUnloadDll", &patched_LdrUnloadDll); + if (IsWin8OrLater()) { // LdrResolveDelayLoadedAPI was introduced in Win8 + stub_LdrResolveDelayLoadedAPI.Set(NtDllIntercept, + "LdrResolveDelayLoadedAPI", + &patched_LdrResolveDelayLoadedAPI); + } +} +#endif // defined(GP_PLAT_amd64_windows) + +} // namespace baseprofiler +} // namespace mozilla diff --git a/mozglue/baseprofiler/core/platform.cpp b/mozglue/baseprofiler/core/platform.cpp new file mode 100644 index 0000000000..dd3bfe6674 --- /dev/null +++ b/mozglue/baseprofiler/core/platform.cpp @@ -0,0 +1,3824 @@ +/* -*- 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/. */ + +// There are three kinds of samples done by the profiler. +// +// - A "periodic" sample is the most complex kind. It is done in response to a +// timer while the profiler is active. It involves writing a stack trace plus +// a variety of other values (memory measurements, responsiveness +// measurements, etc.) into the main ProfileBuffer. The sampling is done from +// off-thread, and so SuspendAndSampleAndResumeThread() is used to get the +// register values. +// +// - A "synchronous" sample is a simpler kind. It is done in response to an API +// call (profiler_get_backtrace()). It involves writing a stack trace and +// little else into a temporary ProfileBuffer, and wrapping that up in a +// ProfilerBacktrace that can be subsequently used in a marker. The sampling +// is done on-thread, and so REGISTERS_SYNC_POPULATE() is used to get the +// register values. +// +// - A "backtrace" sample is the simplest kind. It is done in response to an +// API call (profiler_suspend_and_sample_thread()). It involves getting a +// stack trace via a ProfilerStackCollector; it does not write to a +// ProfileBuffer. The sampling is done from off-thread, and so uses +// SuspendAndSampleAndResumeThread() to get the register values. + +#include "platform.h" + +#include <algorithm> +#include <errno.h> +#include <fstream> +#include <ostream> +#include <set> +#include <sstream> +#include <string_view> + +// #include "memory_hooks.h" +#include "mozilla/ArrayUtils.h" +#include "mozilla/AutoProfilerLabel.h" +#include "mozilla/BaseAndGeckoProfilerDetail.h" +#include "mozilla/BaseProfilerDetail.h" +#include "mozilla/DoubleConversion.h" +#include "mozilla/Printf.h" +#include "mozilla/ProfileBufferChunkManagerSingle.h" +#include "mozilla/ProfileBufferChunkManagerWithLocalLimit.h" +#include "mozilla/ProfileChunkedBuffer.h" +#include "mozilla/Services.h" +#include "mozilla/Span.h" +#include "mozilla/StackWalk.h" +#ifdef XP_WIN +# include "mozilla/StackWalkThread.h" +#endif +#include "mozilla/StaticPtr.h" +#include "mozilla/ThreadLocal.h" +#include "mozilla/TimeStamp.h" + +#include "mozilla/UniquePtr.h" +#include "mozilla/Vector.h" +#include "prdtoa.h" +#include "prtime.h" + +#include "BaseProfiler.h" +#include "BaseProfilingCategory.h" +#include "PageInformation.h" +#include "ProfiledThreadData.h" +#include "ProfilerBacktrace.h" +#include "ProfileBuffer.h" +#include "RegisteredThread.h" +#include "BaseProfilerSharedLibraries.h" +#include "ThreadInfo.h" +#include "VTuneProfiler.h" + +// Win32 builds always have frame pointers, so FramePointerStackWalk() always +// works. +#if defined(GP_PLAT_x86_windows) +# define HAVE_NATIVE_UNWIND +# define USE_FRAME_POINTER_STACK_WALK +#endif + +// Win64 builds always omit frame pointers, so we use the slower +// MozStackWalk(), which works in that case. +#if defined(GP_PLAT_amd64_windows) +# define HAVE_NATIVE_UNWIND +# define USE_MOZ_STACK_WALK +#endif + +// AArch64 Win64 doesn't seem to use frame pointers, so we use the slower +// MozStackWalk(). +#if defined(GP_PLAT_arm64_windows) +# define HAVE_NATIVE_UNWIND +# define USE_MOZ_STACK_WALK +#endif + +// Mac builds use FramePointerStackWalk(). Even if we build without +// frame pointers, we'll still get useful stacks in system libraries +// because those always have frame pointers. +// We don't use MozStackWalk() on Mac. +#if defined(GP_OS_darwin) +# define HAVE_NATIVE_UNWIND +# define USE_FRAME_POINTER_STACK_WALK +#endif + +// No stack-walking in baseprofiler on linux, android, bsd. +// APIs now make it easier to capture backtraces from the Base Profiler, which +// is currently not supported on these platform, and would lead to a MOZ_CRASH +// in REGISTERS_SYNC_POPULATE(). `#if 0` added in bug 1658232, follow-up bugs +// should be referenced in meta bug 1557568. +#if 0 +// Android builds use the ARM Exception Handling ABI to unwind. +# if defined(GP_PLAT_arm_linux) || defined(GP_PLAT_arm_android) +# define HAVE_NATIVE_UNWIND +# define USE_EHABI_STACKWALK +# include "EHABIStackWalk.h" +# endif + +// Linux/BSD builds use LUL, which uses DWARF info to unwind stacks. +# if defined(GP_PLAT_amd64_linux) || defined(GP_PLAT_x86_linux) || \ + defined(GP_PLAT_amd64_android) || defined(GP_PLAT_x86_android) || \ + defined(GP_PLAT_mips64_linux) || defined(GP_PLAT_arm64_linux) || \ + defined(GP_PLAT_arm64_android) || defined(GP_PLAT_amd64_freebsd) || \ + defined(GP_PLAT_arm64_freebsd) +# define HAVE_NATIVE_UNWIND +# define USE_LUL_STACKWALK +# include "lul/LulMain.h" +# include "lul/platform-linux-lul.h" + +// On linux we use LUL for periodic samples and synchronous samples, but we use +// FramePointerStackWalk for backtrace samples when MOZ_PROFILING is enabled. +// (See the comment at the top of the file for a definition of +// periodic/synchronous/backtrace.). +// +// FramePointerStackWalk can produce incomplete stacks when the current entry is +// in a shared library without framepointers, however LUL can take a long time +// to initialize, which is undesirable for consumers of +// profiler_suspend_and_sample_thread like the Background Hang Reporter. +# if defined(MOZ_PROFILING) +# define USE_FRAME_POINTER_STACK_WALK +# endif +# endif +#endif + +// We can only stackwalk without expensive initialization on platforms which +// support FramePointerStackWalk or MozStackWalk. LUL Stackwalking requires +// initializing LUL, and EHABIStackWalk requires initializing EHABI, both of +// which can be expensive. +#if defined(USE_FRAME_POINTER_STACK_WALK) || defined(USE_MOZ_STACK_WALK) +# define HAVE_FASTINIT_NATIVE_UNWIND +#endif + +#ifdef MOZ_VALGRIND +# include <valgrind/memcheck.h> +#else +# define VALGRIND_MAKE_MEM_DEFINED(_addr, _len) ((void)0) +#endif + +#if defined(GP_OS_linux) || defined(GP_OS_android) || defined(GP_OS_freebsd) +# include <ucontext.h> +#endif + +namespace mozilla { +namespace baseprofiler { + +using detail::RacyFeatures; + +bool LogTest(int aLevelToTest) { + static const int maxLevel = getenv("MOZ_BASE_PROFILER_VERBOSE_LOGGING") ? 5 + : getenv("MOZ_BASE_PROFILER_DEBUG_LOGGING") ? 4 + : getenv("MOZ_BASE_PROFILER_LOGGING") ? 3 + : 0; + return aLevelToTest <= maxLevel; +} + +void PrintToConsole(const char* aFmt, ...) { + va_list args; + va_start(args, aFmt); +#if defined(ANDROID) + __android_log_vprint(ANDROID_LOG_INFO, "Gecko", aFmt, args); +#else + vfprintf(stderr, aFmt, args); +#endif + va_end(args); +} + +ProfileChunkedBuffer& profiler_get_core_buffer() { + // This needs its own mutex, because it is used concurrently from functions + // guarded by gPSMutex as well as others without safety (e.g., + // profiler_add_marker). It is *not* used inside the critical section of the + // sampler, because mutexes cannot be used there. + static ProfileChunkedBuffer sProfileChunkedBuffer{ + ProfileChunkedBuffer::ThreadSafety::WithMutex}; + return sProfileChunkedBuffer; +} + +Atomic<int, MemoryOrdering::Relaxed> gSkipSampling; + +constexpr static bool ValidateFeatures() { + int expectedFeatureNumber = 0; + + // Feature numbers should start at 0 and increase by 1 each. +#define CHECK_FEATURE(n_, str_, Name_, desc_) \ + if ((n_) != expectedFeatureNumber) { \ + return false; \ + } \ + ++expectedFeatureNumber; + + BASE_PROFILER_FOR_EACH_FEATURE(CHECK_FEATURE) + +#undef CHECK_FEATURE + + return true; +} + +static_assert(ValidateFeatures(), "Feature list is invalid"); + +// Return all features that are available on this platform. +static uint32_t AvailableFeatures() { + uint32_t features = 0; + +#define ADD_FEATURE(n_, str_, Name_, desc_) \ + ProfilerFeature::Set##Name_(features); + + // Add all the possible features. + BASE_PROFILER_FOR_EACH_FEATURE(ADD_FEATURE) + +#undef ADD_FEATURE + + // Now remove features not supported on this platform/configuration. + ProfilerFeature::ClearJava(features); + ProfilerFeature::ClearJS(features); + ProfilerFeature::ClearScreenshots(features); +#if !defined(HAVE_NATIVE_UNWIND) + ProfilerFeature::ClearStackWalk(features); +#endif +#if !defined(GP_OS_windows) + ProfilerFeature::ClearNoTimerResolutionChange(features); +#endif + + return features; +} + +// Default features common to all contexts (even if not available). +static constexpr uint32_t DefaultFeatures() { + return ProfilerFeature::Java | ProfilerFeature::JS | + ProfilerFeature::StackWalk | ProfilerFeature::CPUUtilization | + ProfilerFeature::ProcessCPU; +} + +// Extra default features when MOZ_PROFILER_STARTUP is set (even if not +// available). +static constexpr uint32_t StartupExtraDefaultFeatures() { + // Enable mainthreadio by default for startup profiles as startup is heavy on + // I/O operations, and main thread I/O is really important to see there. + return ProfilerFeature::MainThreadIO | ProfilerFeature::IPCMessages; +} + +// The auto-lock/unlock mutex that guards accesses to CorePS and ActivePS. +// Use `PSAutoLock lock;` to take the lock until the end of the enclosing block. +// External profilers may use this same lock for their own data, but as the lock +// is non-recursive, *only* `f(PSLockRef, ...)` functions below should be +// called, to avoid double-locking. +class MOZ_RAII PSAutoLock { + public: + PSAutoLock() : mLock(gPSMutex) {} + + PSAutoLock(const PSAutoLock&) = delete; + void operator=(const PSAutoLock&) = delete; + + [[nodiscard]] static bool IsLockedOnCurrentThread() { + return gPSMutex.IsLockedOnCurrentThread(); + } + + private: + static detail::BaseProfilerMutex gPSMutex; + detail::BaseProfilerAutoLock mLock; +}; + +detail::BaseProfilerMutex PSAutoLock::gPSMutex{"Base Profiler mutex"}; + +// Only functions that take a PSLockRef arg can access CorePS's and ActivePS's +// fields. +typedef const PSAutoLock& PSLockRef; + +#define PS_GET(type_, name_) \ + static type_ name_(PSLockRef) { \ + MOZ_ASSERT(sInstance); \ + return sInstance->m##name_; \ + } + +#define PS_GET_LOCKLESS(type_, name_) \ + static type_ name_() { \ + MOZ_ASSERT(sInstance); \ + return sInstance->m##name_; \ + } + +#define PS_GET_AND_SET(type_, name_) \ + PS_GET(type_, name_) \ + static void Set##name_(PSLockRef, type_ a##name_) { \ + MOZ_ASSERT(sInstance); \ + sInstance->m##name_ = a##name_; \ + } + +// All functions in this file can run on multiple threads unless they have an +// NS_IsMainThread() assertion. + +// This class contains the profiler's core global state, i.e. that which is +// valid even when the profiler is not active. Most profile operations can't do +// anything useful when this class is not instantiated, so we release-assert +// its non-nullness in all such operations. +// +// Accesses to CorePS are guarded by gPSMutex. Getters and setters take a +// PSAutoLock reference as an argument as proof that the gPSMutex is currently +// locked. This makes it clear when gPSMutex is locked and helps avoid +// accidental unlocked accesses to global state. There are ways to circumvent +// this mechanism, but please don't do so without *very* good reason and a +// detailed explanation. +// +// The exceptions to this rule: +// +// - mProcessStartTime, because it's immutable; +// +// - each thread's RacyRegisteredThread object is accessible without locking via +// TLSRegisteredThread::RacyRegisteredThread(). +class CorePS { + private: + CorePS() + : mProcessStartTime(TimeStamp::ProcessCreation()) +#ifdef USE_LUL_STACKWALK + , + mLul(nullptr) +#endif + { + } + + ~CorePS() {} + + public: + static void Create(PSLockRef aLock) { + MOZ_ASSERT(!sInstance); + sInstance = new CorePS(); + } + + static void Destroy(PSLockRef aLock) { + MOZ_ASSERT(sInstance); + delete sInstance; + sInstance = nullptr; + } + + // Unlike ActivePS::Exists(), CorePS::Exists() can be called without gPSMutex + // being locked. This is because CorePS is instantiated so early on the main + // thread that we don't have to worry about it being racy. + static bool Exists() { return !!sInstance; } + + static void AddSizeOf(PSLockRef, MallocSizeOf aMallocSizeOf, + size_t& aProfSize, size_t& aLulSize) { + MOZ_ASSERT(sInstance); + + aProfSize += aMallocSizeOf(sInstance); + + for (auto& registeredThread : sInstance->mRegisteredThreads) { + aProfSize += registeredThread->SizeOfIncludingThis(aMallocSizeOf); + } + + for (auto& registeredPage : sInstance->mRegisteredPages) { + aProfSize += registeredPage->SizeOfIncludingThis(aMallocSizeOf); + } + + // Measurement of the following things may be added later if DMD finds it + // is worthwhile: + // - CorePS::mRegisteredThreads itself (its elements' children are + // measured above) + // - CorePS::mRegisteredPages itself (its elements' children are + // measured above) + // - CorePS::mInterposeObserver + +#if defined(USE_LUL_STACKWALK) + if (sInstance->mLul) { + aLulSize += sInstance->mLul->SizeOfIncludingThis(aMallocSizeOf); + } +#endif + } + + // No PSLockRef is needed for this field because it's immutable. + PS_GET_LOCKLESS(const TimeStamp&, ProcessStartTime) + + PS_GET(const Vector<UniquePtr<RegisteredThread>>&, RegisteredThreads) + + static void AppendRegisteredThread( + PSLockRef, UniquePtr<RegisteredThread>&& aRegisteredThread) { + MOZ_ASSERT(sInstance); + MOZ_RELEASE_ASSERT( + sInstance->mRegisteredThreads.append(std::move(aRegisteredThread))); + } + + static void RemoveRegisteredThread(PSLockRef, + RegisteredThread* aRegisteredThread) { + MOZ_ASSERT(sInstance); + // Remove aRegisteredThread from mRegisteredThreads. + for (UniquePtr<RegisteredThread>& rt : sInstance->mRegisteredThreads) { + if (rt.get() == aRegisteredThread) { + sInstance->mRegisteredThreads.erase(&rt); + return; + } + } + } + + PS_GET(Vector<RefPtr<PageInformation>>&, RegisteredPages) + + static void AppendRegisteredPage(PSLockRef, + RefPtr<PageInformation>&& aRegisteredPage) { + MOZ_ASSERT(sInstance); + struct RegisteredPageComparator { + PageInformation* aA; + bool operator()(PageInformation* aB) const { return aA->Equals(aB); } + }; + + auto foundPageIter = std::find_if( + sInstance->mRegisteredPages.begin(), sInstance->mRegisteredPages.end(), + RegisteredPageComparator{aRegisteredPage.get()}); + + if (foundPageIter != sInstance->mRegisteredPages.end()) { + if ((*foundPageIter)->Url() == "about:blank") { + // When a BrowsingContext is loaded, the first url loaded in it will be + // about:blank, and if the principal matches, the first document loaded + // in it will share an inner window. That's why we should delete the + // intermittent about:blank if they share the inner window. + sInstance->mRegisteredPages.erase(foundPageIter); + } else { + // Do not register the same page again. + return; + } + } + MOZ_RELEASE_ASSERT( + sInstance->mRegisteredPages.append(std::move(aRegisteredPage))); + } + + static void RemoveRegisteredPage(PSLockRef, + uint64_t aRegisteredInnerWindowID) { + MOZ_ASSERT(sInstance); + // Remove RegisteredPage from mRegisteredPages by given inner window ID. + sInstance->mRegisteredPages.eraseIf([&](const RefPtr<PageInformation>& rd) { + return rd->InnerWindowID() == aRegisteredInnerWindowID; + }); + } + + static void ClearRegisteredPages(PSLockRef) { + MOZ_ASSERT(sInstance); + sInstance->mRegisteredPages.clear(); + } + + PS_GET(const Vector<BaseProfilerCount*>&, Counters) + + static void AppendCounter(PSLockRef, BaseProfilerCount* aCounter) { + MOZ_ASSERT(sInstance); + // we don't own the counter; they may be stored in static objects + MOZ_RELEASE_ASSERT(sInstance->mCounters.append(aCounter)); + } + + static void RemoveCounter(PSLockRef, BaseProfilerCount* aCounter) { + // we may be called to remove a counter after the profiler is stopped or + // late in shutdown. + if (sInstance) { + auto* counter = std::find(sInstance->mCounters.begin(), + sInstance->mCounters.end(), aCounter); + MOZ_RELEASE_ASSERT(counter != sInstance->mCounters.end()); + sInstance->mCounters.erase(counter); + } + } + +#ifdef USE_LUL_STACKWALK + static lul::LUL* Lul(PSLockRef) { + MOZ_ASSERT(sInstance); + return sInstance->mLul.get(); + } + static void SetLul(PSLockRef, UniquePtr<lul::LUL> aLul) { + MOZ_ASSERT(sInstance); + sInstance->mLul = std::move(aLul); + } +#endif + + PS_GET_AND_SET(const std::string&, ProcessName) + PS_GET_AND_SET(const std::string&, ETLDplus1) + + private: + // The singleton instance + static CorePS* sInstance; + + // The time that the process started. + const TimeStamp mProcessStartTime; + + // Info on all the registered threads. + // ThreadIds in mRegisteredThreads are unique. + Vector<UniquePtr<RegisteredThread>> mRegisteredThreads; + + // Info on all the registered pages. + // InnerWindowIDs in mRegisteredPages are unique. + Vector<RefPtr<PageInformation>> mRegisteredPages; + + // Non-owning pointers to all active counters + Vector<BaseProfilerCount*> mCounters; + +#ifdef USE_LUL_STACKWALK + // LUL's state. Null prior to the first activation, non-null thereafter. + UniquePtr<lul::LUL> mLul; +#endif + + // Process name, provided by child process initialization code. + std::string mProcessName; + // Private name, provided by child process initialization code (eTLD+1 in + // fission) + std::string mETLDplus1; +}; + +CorePS* CorePS::sInstance = nullptr; + +class SamplerThread; + +static SamplerThread* NewSamplerThread(PSLockRef aLock, uint32_t aGeneration, + double aInterval, uint32_t aFeatures); + +struct LiveProfiledThreadData { + RegisteredThread* mRegisteredThread; + UniquePtr<ProfiledThreadData> mProfiledThreadData; +}; + +// The buffer size is provided as a number of "entries", this is their size in +// bytes. +constexpr static uint32_t scBytesPerEntry = 8; + +// This class contains the profiler's global state that is valid only when the +// profiler is active. When not instantiated, the profiler is inactive. +// +// Accesses to ActivePS are guarded by gPSMutex, in much the same fashion as +// CorePS. +// +class ActivePS { + private: + // We need to decide how many chunks of what size we want to fit in the given + // total maximum capacity for this process, in the (likely) context of + // multiple processes doing the same choice and having an inter-process + // mechanism to control the overal memory limit. + + // Minimum chunk size allowed, enough for at least one stack. + constexpr static uint32_t scMinimumChunkSize = + 2 * ProfileBufferChunkManager::scExpectedMaximumStackSize; + + // Ideally we want at least 2 unreleased chunks to work with (1 current and 1 + // next), and 2 released chunks (so that one can be recycled when old, leaving + // one with some data). + constexpr static uint32_t scMinimumNumberOfChunks = 4; + + // And we want to limit chunks to a maximum size, which is a compromise + // between: + // - A big size, which helps with reducing the rate of allocations and IPCs. + // - A small size, which helps with equalizing the duration of recorded data + // (as the inter-process controller will discard the oldest chunks in all + // Firefox processes). + constexpr static uint32_t scMaximumChunkSize = 1024 * 1024; + + public: + // We should be able to store at least the minimum number of the smallest- + // possible chunks. + constexpr static uint32_t scMinimumBufferSize = + scMinimumNumberOfChunks * scMinimumChunkSize; + constexpr static uint32_t scMinimumBufferEntries = + scMinimumBufferSize / scBytesPerEntry; + + // Limit to 2GiB. + constexpr static uint32_t scMaximumBufferSize = 2u * 1024u * 1024u * 1024u; + constexpr static uint32_t scMaximumBufferEntries = + scMaximumBufferSize / scBytesPerEntry; + + constexpr static uint32_t ClampToAllowedEntries(uint32_t aEntries) { + if (aEntries <= scMinimumBufferEntries) { + return scMinimumBufferEntries; + } + if (aEntries >= scMaximumBufferEntries) { + return scMaximumBufferEntries; + } + return aEntries; + } + + private: + constexpr static uint32_t ChunkSizeForEntries(uint32_t aEntries) { + return uint32_t(std::min(size_t(ClampToAllowedEntries(aEntries)) * + scBytesPerEntry / scMinimumNumberOfChunks, + size_t(scMaximumChunkSize))); + } + + static uint32_t AdjustFeatures(uint32_t aFeatures, uint32_t aFilterCount) { + // Filter out any features unavailable in this platform/configuration. + aFeatures &= AvailableFeatures(); + + // Some features imply others. + if (aFeatures & ProfilerFeature::FileIOAll) { + aFeatures |= ProfilerFeature::MainThreadIO | ProfilerFeature::FileIO; + } else if (aFeatures & ProfilerFeature::FileIO) { + aFeatures |= ProfilerFeature::MainThreadIO; + } + + return aFeatures; + } + + ActivePS(PSLockRef aLock, const TimeStamp& aProfilingStartTime, + PowerOfTwo32 aCapacity, double aInterval, uint32_t aFeatures, + const char** aFilters, uint32_t aFilterCount, + const Maybe<double>& aDuration) + : mProfilingStartTime(aProfilingStartTime), + mGeneration(sNextGeneration++), + mCapacity(aCapacity), + mDuration(aDuration), + mInterval(aInterval), + mFeatures(AdjustFeatures(aFeatures, aFilterCount)), + mProfileBufferChunkManager( + MakeUnique<ProfileBufferChunkManagerWithLocalLimit>( + size_t(ClampToAllowedEntries(aCapacity.Value())) * + scBytesPerEntry, + ChunkSizeForEntries(aCapacity.Value()))), + mProfileBuffer([this]() -> ProfileChunkedBuffer& { + ProfileChunkedBuffer& buffer = profiler_get_core_buffer(); + buffer.SetChunkManager(*mProfileBufferChunkManager); + return buffer; + }()), + // The new sampler thread doesn't start sampling immediately because the + // main loop within Run() is blocked until this function's caller + // unlocks gPSMutex. + mSamplerThread( + NewSamplerThread(aLock, mGeneration, aInterval, aFeatures)), + mIsPaused(false), + mIsSamplingPaused(false) { + // Deep copy and lower-case aFilters. + MOZ_ALWAYS_TRUE(mFilters.resize(aFilterCount)); + MOZ_ALWAYS_TRUE(mFiltersLowered.resize(aFilterCount)); + for (uint32_t i = 0; i < aFilterCount; ++i) { + mFilters[i] = aFilters[i]; + mFiltersLowered[i].reserve(mFilters[i].size()); + std::transform(mFilters[i].cbegin(), mFilters[i].cend(), + std::back_inserter(mFiltersLowered[i]), ::tolower); + } + } + + ~ActivePS() { + if (mProfileBufferChunkManager) { + // We still control the chunk manager, remove it from the core buffer. + profiler_get_core_buffer().ResetChunkManager(); + } + } + + bool ThreadSelected(const char* aThreadName) { + if (mFiltersLowered.empty()) { + return true; + } + + std::string name = aThreadName; + std::transform(name.begin(), name.end(), name.begin(), ::tolower); + + for (const auto& filter : mFiltersLowered) { + if (filter == "*") { + return true; + } + + // Crude, non UTF-8 compatible, case insensitive substring search + if (name.find(filter) != std::string::npos) { + return true; + } + + // If the filter is "pid:<my pid>", profile all threads. + if (mozilla::profiler::detail::FilterHasPid(filter.c_str())) { + return true; + } + } + + return false; + } + + public: + static void Create(PSLockRef aLock, const TimeStamp& aProfilingStartTime, + PowerOfTwo32 aCapacity, double aInterval, + uint32_t aFeatures, const char** aFilters, + uint32_t aFilterCount, const Maybe<double>& aDuration) { + MOZ_ASSERT(!sInstance); + sInstance = new ActivePS(aLock, aProfilingStartTime, aCapacity, aInterval, + aFeatures, aFilters, aFilterCount, aDuration); + } + + [[nodiscard]] static SamplerThread* Destroy(PSLockRef aLock) { + MOZ_ASSERT(sInstance); + auto samplerThread = sInstance->mSamplerThread; + delete sInstance; + sInstance = nullptr; + + return samplerThread; + } + + static bool Exists(PSLockRef) { return !!sInstance; } + + static bool Equals(PSLockRef, PowerOfTwo32 aCapacity, + const Maybe<double>& aDuration, double aInterval, + uint32_t aFeatures, const char** aFilters, + uint32_t aFilterCount) { + MOZ_ASSERT(sInstance); + if (sInstance->mCapacity != aCapacity || + sInstance->mDuration != aDuration || + sInstance->mInterval != aInterval || + sInstance->mFeatures != aFeatures || + sInstance->mFilters.length() != aFilterCount) { + return false; + } + + for (uint32_t i = 0; i < sInstance->mFilters.length(); ++i) { + if (strcmp(sInstance->mFilters[i].c_str(), aFilters[i]) != 0) { + return false; + } + } + return true; + } + + static size_t SizeOf(PSLockRef, MallocSizeOf aMallocSizeOf) { + MOZ_ASSERT(sInstance); + + size_t n = aMallocSizeOf(sInstance); + + n += sInstance->mProfileBuffer.SizeOfExcludingThis(aMallocSizeOf); + + // Measurement of the following members may be added later if DMD finds it + // is worthwhile: + // - mLiveProfiledThreads (both the array itself, and the contents) + // - mDeadProfiledThreads (both the array itself, and the contents) + // + + return n; + } + + static UniquePtr<ProfileBufferChunkManagerWithLocalLimit> + ExtractBaseProfilerChunkManager(PSLockRef) { + MOZ_ASSERT(sInstance); + return std::move(sInstance->mProfileBufferChunkManager); + } + + static bool ShouldProfileThread(PSLockRef aLock, ThreadInfo* aInfo) { + MOZ_ASSERT(sInstance); + return sInstance->ThreadSelected(aInfo->Name()); + } + + PS_GET_LOCKLESS(TimeStamp, ProfilingStartTime) + + PS_GET(uint32_t, Generation) + + PS_GET(PowerOfTwo32, Capacity) + + PS_GET(Maybe<double>, Duration) + + PS_GET(double, Interval) + + PS_GET(uint32_t, Features) + +#define PS_GET_FEATURE(n_, str_, Name_, desc_) \ + static bool Feature##Name_(PSLockRef) { \ + MOZ_ASSERT(sInstance); \ + return ProfilerFeature::Has##Name_(sInstance->mFeatures); \ + } + + BASE_PROFILER_FOR_EACH_FEATURE(PS_GET_FEATURE) + +#undef PS_GET_FEATURE + + PS_GET(const Vector<std::string>&, Filters) + PS_GET(const Vector<std::string>&, FiltersLowered) + + static void FulfillChunkRequests(PSLockRef) { + MOZ_ASSERT(sInstance); + if (sInstance->mProfileBufferChunkManager) { + sInstance->mProfileBufferChunkManager->FulfillChunkRequests(); + } + } + + static ProfileBuffer& Buffer(PSLockRef) { + MOZ_ASSERT(sInstance); + return sInstance->mProfileBuffer; + } + + static const Vector<LiveProfiledThreadData>& LiveProfiledThreads(PSLockRef) { + MOZ_ASSERT(sInstance); + return sInstance->mLiveProfiledThreads; + } + + // Returns an array containing (RegisteredThread*, ProfiledThreadData*) pairs + // for all threads that should be included in a profile, both for threads + // that are still registered, and for threads that have been unregistered but + // still have data in the buffer. + // For threads that have already been unregistered, the RegisteredThread + // pointer will be null. + // The returned array is sorted by thread register time. + // Do not hold on to the return value across thread registration or profiler + // restarts. + static Vector<std::pair<RegisteredThread*, ProfiledThreadData*>> + ProfiledThreads(PSLockRef) { + MOZ_ASSERT(sInstance); + Vector<std::pair<RegisteredThread*, ProfiledThreadData*>> array; + MOZ_RELEASE_ASSERT( + array.initCapacity(sInstance->mLiveProfiledThreads.length() + + sInstance->mDeadProfiledThreads.length())); + for (auto& t : sInstance->mLiveProfiledThreads) { + MOZ_RELEASE_ASSERT(array.append( + std::make_pair(t.mRegisteredThread, t.mProfiledThreadData.get()))); + } + for (auto& t : sInstance->mDeadProfiledThreads) { + MOZ_RELEASE_ASSERT( + array.append(std::make_pair((RegisteredThread*)nullptr, t.get()))); + } + + std::sort(array.begin(), array.end(), + [](const std::pair<RegisteredThread*, ProfiledThreadData*>& a, + const std::pair<RegisteredThread*, ProfiledThreadData*>& b) { + return a.second->Info()->RegisterTime() < + b.second->Info()->RegisterTime(); + }); + return array; + } + + static Vector<RefPtr<PageInformation>> ProfiledPages(PSLockRef aLock) { + MOZ_ASSERT(sInstance); + Vector<RefPtr<PageInformation>> array; + for (auto& d : CorePS::RegisteredPages(aLock)) { + MOZ_RELEASE_ASSERT(array.append(d)); + } + for (auto& d : sInstance->mDeadProfiledPages) { + MOZ_RELEASE_ASSERT(array.append(d)); + } + // We don't need to sort the pages like threads since we won't show them + // as a list. + return array; + } + + // Do a linear search through mLiveProfiledThreads to find the + // ProfiledThreadData object for a RegisteredThread. + static ProfiledThreadData* GetProfiledThreadData( + PSLockRef, RegisteredThread* aRegisteredThread) { + MOZ_ASSERT(sInstance); + for (const LiveProfiledThreadData& thread : + sInstance->mLiveProfiledThreads) { + if (thread.mRegisteredThread == aRegisteredThread) { + return thread.mProfiledThreadData.get(); + } + } + return nullptr; + } + + static ProfiledThreadData* AddLiveProfiledThread( + PSLockRef, RegisteredThread* aRegisteredThread, + UniquePtr<ProfiledThreadData>&& aProfiledThreadData) { + MOZ_ASSERT(sInstance); + MOZ_RELEASE_ASSERT( + sInstance->mLiveProfiledThreads.append(LiveProfiledThreadData{ + aRegisteredThread, std::move(aProfiledThreadData)})); + + // Return a weak pointer to the ProfiledThreadData object. + return sInstance->mLiveProfiledThreads.back().mProfiledThreadData.get(); + } + + static void UnregisterThread(PSLockRef aLockRef, + RegisteredThread* aRegisteredThread) { + MOZ_ASSERT(sInstance); + + DiscardExpiredDeadProfiledThreads(aLockRef); + + // Find the right entry in the mLiveProfiledThreads array and remove the + // element, moving the ProfiledThreadData object for the thread into the + // mDeadProfiledThreads array. + // The thread's RegisteredThread object gets destroyed here. + for (size_t i = 0; i < sInstance->mLiveProfiledThreads.length(); i++) { + LiveProfiledThreadData& thread = sInstance->mLiveProfiledThreads[i]; + if (thread.mRegisteredThread == aRegisteredThread) { + thread.mProfiledThreadData->NotifyUnregistered( + sInstance->mProfileBuffer.BufferRangeEnd()); + MOZ_RELEASE_ASSERT(sInstance->mDeadProfiledThreads.append( + std::move(thread.mProfiledThreadData))); + sInstance->mLiveProfiledThreads.erase( + &sInstance->mLiveProfiledThreads[i]); + return; + } + } + } + + PS_GET_AND_SET(bool, IsPaused) + + // True if sampling is paused (though generic `SetIsPaused()` or specific + // `SetIsSamplingPaused()`). + static bool IsSamplingPaused(PSLockRef lock) { + MOZ_ASSERT(sInstance); + return IsPaused(lock) || sInstance->mIsSamplingPaused; + } + + static void SetIsSamplingPaused(PSLockRef, bool aIsSamplingPaused) { + MOZ_ASSERT(sInstance); + sInstance->mIsSamplingPaused = aIsSamplingPaused; + } + + static void DiscardExpiredDeadProfiledThreads(PSLockRef) { + MOZ_ASSERT(sInstance); + uint64_t bufferRangeStart = sInstance->mProfileBuffer.BufferRangeStart(); + // Discard any dead threads that were unregistered before bufferRangeStart. + sInstance->mDeadProfiledThreads.eraseIf( + [bufferRangeStart]( + const UniquePtr<ProfiledThreadData>& aProfiledThreadData) { + Maybe<uint64_t> bufferPosition = + aProfiledThreadData->BufferPositionWhenUnregistered(); + MOZ_RELEASE_ASSERT(bufferPosition, + "should have unregistered this thread"); + return *bufferPosition < bufferRangeStart; + }); + } + + static void UnregisterPage(PSLockRef aLock, + uint64_t aRegisteredInnerWindowID) { + MOZ_ASSERT(sInstance); + auto& registeredPages = CorePS::RegisteredPages(aLock); + for (size_t i = 0; i < registeredPages.length(); i++) { + RefPtr<PageInformation>& page = registeredPages[i]; + if (page->InnerWindowID() == aRegisteredInnerWindowID) { + page->NotifyUnregistered(sInstance->mProfileBuffer.BufferRangeEnd()); + MOZ_RELEASE_ASSERT( + sInstance->mDeadProfiledPages.append(std::move(page))); + registeredPages.erase(®isteredPages[i--]); + } + } + } + + static void DiscardExpiredPages(PSLockRef) { + MOZ_ASSERT(sInstance); + uint64_t bufferRangeStart = sInstance->mProfileBuffer.BufferRangeStart(); + // Discard any dead pages that were unregistered before + // bufferRangeStart. + sInstance->mDeadProfiledPages.eraseIf( + [bufferRangeStart](const RefPtr<PageInformation>& aProfiledPage) { + Maybe<uint64_t> bufferPosition = + aProfiledPage->BufferPositionWhenUnregistered(); + MOZ_RELEASE_ASSERT(bufferPosition, + "should have unregistered this page"); + return *bufferPosition < bufferRangeStart; + }); + } + + static void ClearUnregisteredPages(PSLockRef) { + MOZ_ASSERT(sInstance); + sInstance->mDeadProfiledPages.clear(); + } + + static void ClearExpiredExitProfiles(PSLockRef) { + MOZ_ASSERT(sInstance); + uint64_t bufferRangeStart = sInstance->mProfileBuffer.BufferRangeStart(); + // Discard exit profiles that were gathered before our buffer RangeStart. + sInstance->mExitProfiles.eraseIf( + [bufferRangeStart](const ExitProfile& aExitProfile) { + return aExitProfile.mBufferPositionAtGatherTime < bufferRangeStart; + }); + } + + static void AddExitProfile(PSLockRef aLock, const std::string& aExitProfile) { + MOZ_ASSERT(sInstance); + + ClearExpiredExitProfiles(aLock); + + MOZ_RELEASE_ASSERT(sInstance->mExitProfiles.append( + ExitProfile{aExitProfile, sInstance->mProfileBuffer.BufferRangeEnd()})); + } + + static Vector<std::string> MoveExitProfiles(PSLockRef aLock) { + MOZ_ASSERT(sInstance); + + ClearExpiredExitProfiles(aLock); + + Vector<std::string> profiles; + MOZ_RELEASE_ASSERT( + profiles.initCapacity(sInstance->mExitProfiles.length())); + for (auto& profile : sInstance->mExitProfiles) { + MOZ_RELEASE_ASSERT(profiles.append(std::move(profile.mJSON))); + } + sInstance->mExitProfiles.clear(); + return profiles; + } + + private: + // The singleton instance. + static ActivePS* sInstance; + + const TimeStamp mProfilingStartTime; + + // We need to track activity generations. If we didn't we could have the + // following scenario. + // + // - profiler_stop() locks gPSMutex, de-instantiates ActivePS, unlocks + // gPSMutex, deletes the SamplerThread (which does a join). + // + // - profiler_start() runs on a different thread, locks gPSMutex, + // re-instantiates ActivePS, unlocks gPSMutex -- all before the join + // completes. + // + // - SamplerThread::Run() locks gPSMutex, sees that ActivePS is instantiated, + // and continues as if the start/stop pair didn't occur. Also + // profiler_stop() is stuck, unable to finish. + // + // By checking ActivePS *and* the generation, we can avoid this scenario. + // sNextGeneration is used to track the next generation number; it is static + // because it must persist across different ActivePS instantiations. + const uint32_t mGeneration; + static uint32_t sNextGeneration; + + // The maximum number of 8-byte entries in mProfileBuffer. + const PowerOfTwo32 mCapacity; + + // The maximum duration of entries in mProfileBuffer, in seconds. + const Maybe<double> mDuration; + + // The interval between samples, measured in milliseconds. + const double mInterval; + + // The profile features that are enabled. + const uint32_t mFeatures; + + // Substrings of names of threads we want to profile. + Vector<std::string> mFilters; + Vector<std::string> mFiltersLowered; + + // The chunk manager used by `mProfileBuffer` below. + // May become null if it gets transferred to the Gecko Profiler. + UniquePtr<ProfileBufferChunkManagerWithLocalLimit> mProfileBufferChunkManager; + + // The buffer into which all samples are recorded. + ProfileBuffer mProfileBuffer; + + // ProfiledThreadData objects for any threads that were profiled at any point + // during this run of the profiler: + // - mLiveProfiledThreads contains all threads that are still registered, and + // - mDeadProfiledThreads contains all threads that have already been + // unregistered but for which there is still data in the profile buffer. + Vector<LiveProfiledThreadData> mLiveProfiledThreads; + Vector<UniquePtr<ProfiledThreadData>> mDeadProfiledThreads; + + // Info on all the dead pages. + // Registered pages are being moved to this array after unregistration. + // We are keeping them in case we need them in the profile data. + // We are removing them when we ensure that we won't need them anymore. + Vector<RefPtr<PageInformation>> mDeadProfiledPages; + + // The current sampler thread. This class is not responsible for destroying + // the SamplerThread object; the Destroy() method returns it so the caller + // can destroy it. + SamplerThread* const mSamplerThread; + + // Is the profiler fully paused? + bool mIsPaused; + + // Is the profiler periodic sampling paused? + bool mIsSamplingPaused; + + struct ExitProfile { + std::string mJSON; + uint64_t mBufferPositionAtGatherTime; + }; + Vector<ExitProfile> mExitProfiles; +}; + +ActivePS* ActivePS::sInstance = nullptr; +uint32_t ActivePS::sNextGeneration = 0; + +#undef PS_GET +#undef PS_GET_LOCKLESS +#undef PS_GET_AND_SET + +namespace detail { + +TimeStamp GetProfilingStartTime() { + if (!CorePS::Exists()) { + return {}; + } + PSAutoLock lock; + if (!ActivePS::Exists(lock)) { + return {}; + } + return ActivePS::ProfilingStartTime(); +} + +[[nodiscard]] MFBT_API UniquePtr<ProfileBufferChunkManagerWithLocalLimit> +ExtractBaseProfilerChunkManager() { + PSAutoLock lock; + if (MOZ_UNLIKELY(!ActivePS::Exists(lock))) { + return nullptr; + } + return ActivePS::ExtractBaseProfilerChunkManager(lock); +} + +} // namespace detail + +Atomic<uint32_t, MemoryOrdering::Relaxed> RacyFeatures::sActiveAndFeatures(0); + +/* static */ +void RacyFeatures::SetActive(uint32_t aFeatures) { + sActiveAndFeatures = Active | aFeatures; +} + +/* static */ +void RacyFeatures::SetInactive() { sActiveAndFeatures = 0; } + +/* static */ +bool RacyFeatures::IsActive() { return uint32_t(sActiveAndFeatures) & Active; } + +/* static */ +void RacyFeatures::SetPaused() { sActiveAndFeatures |= Paused; } + +/* static */ +void RacyFeatures::SetUnpaused() { sActiveAndFeatures &= ~Paused; } + +/* static */ +void RacyFeatures::SetSamplingPaused() { sActiveAndFeatures |= SamplingPaused; } + +/* static */ +void RacyFeatures::SetSamplingUnpaused() { + sActiveAndFeatures &= ~SamplingPaused; +} + +/* static */ +bool RacyFeatures::IsActiveWithFeature(uint32_t aFeature) { + uint32_t af = sActiveAndFeatures; // copy it first + return (af & Active) && (af & aFeature); +} + +/* static */ +bool RacyFeatures::IsActiveWithoutFeature(uint32_t aFeature) { + uint32_t af = sActiveAndFeatures; // copy it first + return (af & Active) && !(af & aFeature); +} + +/* static */ +bool RacyFeatures::IsActiveAndUnpaused() { + uint32_t af = sActiveAndFeatures; // copy it first + return (af & Active) && !(af & Paused); +} + +/* static */ +bool RacyFeatures::IsActiveAndSamplingUnpaused() { + uint32_t af = sActiveAndFeatures; // copy it first + return (af & Active) && !(af & (Paused | SamplingPaused)); +} + +// Each live thread has a RegisteredThread, and we store a reference to it in +// TLS. This class encapsulates that TLS. +class TLSRegisteredThread { + public: + static bool Init(PSLockRef) { + bool ok1 = sRegisteredThread.init(); + bool ok2 = AutoProfilerLabel::sProfilingStack.init(); + return ok1 && ok2; + } + + // Get the entire RegisteredThread. Accesses are guarded by gPSMutex. + static class RegisteredThread* RegisteredThread(PSLockRef) { + return sRegisteredThread.get(); + } + + // Get only the RacyRegisteredThread. Accesses are not guarded by gPSMutex. + static class RacyRegisteredThread* RacyRegisteredThread() { + class RegisteredThread* registeredThread = sRegisteredThread.get(); + return registeredThread ? ®isteredThread->RacyRegisteredThread() + : nullptr; + } + + // Get only the ProfilingStack. Accesses are not guarded by gPSMutex. + // RacyRegisteredThread() can also be used to get the ProfilingStack, but that + // is marginally slower because it requires an extra pointer indirection. + static ProfilingStack* Stack() { + return AutoProfilerLabel::sProfilingStack.get(); + } + + static void SetRegisteredThread(PSLockRef, + class RegisteredThread* aRegisteredThread) { + sRegisteredThread.set(aRegisteredThread); + AutoProfilerLabel::sProfilingStack.set( + aRegisteredThread + ? &aRegisteredThread->RacyRegisteredThread().ProfilingStack() + : nullptr); + } + + private: + // This is a non-owning reference to the RegisteredThread; + // CorePS::mRegisteredThreads is the owning reference. On thread + // deregistration, this reference is cleared and the RegisteredThread is + // destroyed. + static MOZ_THREAD_LOCAL(class RegisteredThread*) sRegisteredThread; +}; + +MOZ_THREAD_LOCAL(RegisteredThread*) TLSRegisteredThread::sRegisteredThread; + +/* static */ +ProfilingStack* AutoProfilerLabel::GetProfilingStack() { + return sProfilingStack.get(); +} + +// Although you can access a thread's ProfilingStack via +// TLSRegisteredThread::sRegisteredThread, we also have a second TLS pointer +// directly to the ProfilingStack. Here's why. +// +// - We need to be able to push to and pop from the ProfilingStack in +// AutoProfilerLabel. +// +// - The class functions are hot and must be defined in BaseProfiler.h so they +// can be inlined. +// +// - We don't want to expose TLSRegisteredThread (and RegisteredThread) in +// BaseProfiler.h. +// +// This second pointer isn't ideal, but does provide a way to satisfy those +// constraints. TLSRegisteredThread is responsible for updating it. +MOZ_THREAD_LOCAL(ProfilingStack*) AutoProfilerLabel::sProfilingStack; + +namespace detail { + +[[nodiscard]] MFBT_API TimeStamp GetThreadRegistrationTime() { + if (!CorePS::Exists()) { + return {}; + } + + PSAutoLock lock; + + RegisteredThread* registeredThread = + TLSRegisteredThread::RegisteredThread(lock); + if (!registeredThread) { + return {}; + } + + return registeredThread->Info()->RegisterTime(); +} + +} // namespace detail + +// The name of the main thread. +static const char* const kMainThreadName = "GeckoMain"; + +//////////////////////////////////////////////////////////////////////// +// BEGIN sampling/unwinding code + +// The registers used for stack unwinding and a few other sampling purposes. +// The ctor does nothing; users are responsible for filling in the fields. +class Registers { + public: + Registers() : mPC{nullptr}, mSP{nullptr}, mFP{nullptr}, mLR{nullptr} {} + + void Clear() { memset(this, 0, sizeof(*this)); } + + // These fields are filled in by + // Sampler::SuspendAndSampleAndResumeThread() for periodic and backtrace + // samples, and by REGISTERS_SYNC_POPULATE for synchronous samples. + Address mPC; // Instruction pointer. + Address mSP; // Stack pointer. + Address mFP; // Frame pointer. + Address mLR; // ARM link register. +#if defined(GP_OS_linux) || defined(GP_OS_android) || defined(GP_OS_freebsd) + // This contains all the registers, which means it duplicates the four fields + // above. This is ok. + ucontext_t* mContext; // The context from the signal handler. +#endif +}; + +// Setting MAX_NATIVE_FRAMES too high risks the unwinder wasting a lot of time +// looping on corrupted stacks. +static const size_t MAX_NATIVE_FRAMES = 1024; + +struct NativeStack { + void* mPCs[MAX_NATIVE_FRAMES]; + void* mSPs[MAX_NATIVE_FRAMES]; + size_t mCount; // Number of frames filled. + + NativeStack() : mPCs(), mSPs(), mCount(0) {} +}; + +// Merges the profiling stack and native stack, outputting the details to +// aCollector. +static void MergeStacks(uint32_t aFeatures, bool aIsSynchronous, + const RegisteredThread& aRegisteredThread, + const Registers& aRegs, const NativeStack& aNativeStack, + ProfilerStackCollector& aCollector) { + // WARNING: this function runs within the profiler's "critical section". + // WARNING: this function might be called while the profiler is inactive, and + // cannot rely on ActivePS. + + const ProfilingStack& profilingStack = + aRegisteredThread.RacyRegisteredThread().ProfilingStack(); + const ProfilingStackFrame* profilingStackFrames = profilingStack.frames; + uint32_t profilingStackFrameCount = profilingStack.stackSize(); + + Maybe<uint64_t> samplePosInBuffer; + if (!aIsSynchronous) { + // aCollector.SamplePositionInBuffer() will return Nothing() when + // profiler_suspend_and_sample_thread is called from the background hang + // reporter. + samplePosInBuffer = aCollector.SamplePositionInBuffer(); + } + // While the profiling stack array is ordered oldest-to-youngest, the JS and + // native arrays are ordered youngest-to-oldest. We must add frames to aInfo + // oldest-to-youngest. Thus, iterate over the profiling stack forwards and JS + // and native arrays backwards. Note: this means the terminating condition + // jsIndex and nativeIndex is being < 0. + uint32_t profilingStackIndex = 0; + int32_t nativeIndex = aNativeStack.mCount - 1; + + uint8_t* lastLabelFrameStackAddr = nullptr; + + // Iterate as long as there is at least one frame remaining. + while (profilingStackIndex != profilingStackFrameCount || nativeIndex >= 0) { + // There are 1 to 3 frames available. Find and add the oldest. + uint8_t* profilingStackAddr = nullptr; + uint8_t* nativeStackAddr = nullptr; + + if (profilingStackIndex != profilingStackFrameCount) { + const ProfilingStackFrame& profilingStackFrame = + profilingStackFrames[profilingStackIndex]; + + if (profilingStackFrame.isLabelFrame() || + profilingStackFrame.isSpMarkerFrame()) { + lastLabelFrameStackAddr = (uint8_t*)profilingStackFrame.stackAddress(); + } + + // Skip any JS_OSR frames. Such frames are used when the JS interpreter + // enters a jit frame on a loop edge (via on-stack-replacement, or OSR). + // To avoid both the profiling stack frame and jit frame being recorded + // (and showing up twice), the interpreter marks the interpreter + // profiling stack frame as JS_OSR to ensure that it doesn't get counted. + if (profilingStackFrame.isOSRFrame()) { + profilingStackIndex++; + continue; + } + + MOZ_ASSERT(lastLabelFrameStackAddr); + profilingStackAddr = lastLabelFrameStackAddr; + } + + if (nativeIndex >= 0) { + nativeStackAddr = (uint8_t*)aNativeStack.mSPs[nativeIndex]; + } + + // If there's a native stack frame which has the same SP as a profiling + // stack frame, pretend we didn't see the native stack frame. Ditto for a + // native stack frame which has the same SP as a JS stack frame. In effect + // this means profiling stack frames or JS frames trump conflicting native + // frames. + if (nativeStackAddr && (profilingStackAddr == nativeStackAddr)) { + nativeStackAddr = nullptr; + nativeIndex--; + MOZ_ASSERT(profilingStackAddr); + } + + // Sanity checks. + MOZ_ASSERT_IF(profilingStackAddr, profilingStackAddr != nativeStackAddr); + MOZ_ASSERT_IF(nativeStackAddr, nativeStackAddr != profilingStackAddr); + + // Check to see if profiling stack frame is top-most. + if (profilingStackAddr > nativeStackAddr) { + MOZ_ASSERT(profilingStackIndex < profilingStackFrameCount); + const ProfilingStackFrame& profilingStackFrame = + profilingStackFrames[profilingStackIndex]; + + // Sp marker frames are just annotations and should not be recorded in + // the profile. + if (!profilingStackFrame.isSpMarkerFrame()) { + if (aIsSynchronous && profilingStackFrame.categoryPair() == + ProfilingCategoryPair::PROFILER) { + // For stacks captured synchronously (ie. marker stacks), stop + // walking the stack as soon as we enter the profiler category, + // to avoid showing profiler internal code in marker stacks. + return; + } + aCollector.CollectProfilingStackFrame(profilingStackFrame); + } + profilingStackIndex++; + continue; + } + + // If we reach here, there must be a native stack frame and it must be the + // greatest frame. + if (nativeStackAddr) { + MOZ_ASSERT(nativeIndex >= 0); + void* addr = (void*)aNativeStack.mPCs[nativeIndex]; + aCollector.CollectNativeLeafAddr(addr); + } + if (nativeIndex >= 0) { + nativeIndex--; + } + } +} + +#if defined(GP_OS_windows) && defined(USE_MOZ_STACK_WALK) +static HANDLE GetThreadHandle(PlatformData* aData); +#endif + +#if defined(USE_FRAME_POINTER_STACK_WALK) || defined(USE_MOZ_STACK_WALK) +static void StackWalkCallback(uint32_t aFrameNumber, void* aPC, void* aSP, + void* aClosure) { + NativeStack* nativeStack = static_cast<NativeStack*>(aClosure); + MOZ_ASSERT(nativeStack->mCount < MAX_NATIVE_FRAMES); + nativeStack->mSPs[nativeStack->mCount] = aSP; + nativeStack->mPCs[nativeStack->mCount] = aPC; + nativeStack->mCount++; +} +#endif + +#if defined(USE_FRAME_POINTER_STACK_WALK) +static void DoFramePointerBacktrace(PSLockRef aLock, + const RegisteredThread& aRegisteredThread, + const Registers& aRegs, + NativeStack& aNativeStack) { + // WARNING: this function runs within the profiler's "critical section". + // WARNING: this function might be called while the profiler is inactive, and + // cannot rely on ActivePS. + + // Start with the current function. We use 0 as the frame number here because + // the FramePointerStackWalk() call below will use 1..N. This is a bit weird + // but it doesn't matter because StackWalkCallback() doesn't use the frame + // number argument. + StackWalkCallback(/* frameNum */ 0, aRegs.mPC, aRegs.mSP, &aNativeStack); + + uint32_t maxFrames = uint32_t(MAX_NATIVE_FRAMES - aNativeStack.mCount); + + const void* stackEnd = aRegisteredThread.StackTop(); + if (aRegs.mFP >= aRegs.mSP && aRegs.mFP <= stackEnd) { + FramePointerStackWalk(StackWalkCallback, maxFrames, &aNativeStack, + reinterpret_cast<void**>(aRegs.mFP), + const_cast<void*>(stackEnd)); + } +} +#endif + +#if defined(USE_MOZ_STACK_WALK) +static void DoMozStackWalkBacktrace(PSLockRef aLock, + const RegisteredThread& aRegisteredThread, + const Registers& aRegs, + NativeStack& aNativeStack) { + // WARNING: this function runs within the profiler's "critical section". + // WARNING: this function might be called while the profiler is inactive, and + // cannot rely on ActivePS. + + // Start with the current function. We use 0 as the frame number here because + // the MozStackWalkThread() call below will use 1..N. This is a bit weird but + // it doesn't matter because StackWalkCallback() doesn't use the frame number + // argument. + StackWalkCallback(/* frameNum */ 0, aRegs.mPC, aRegs.mSP, &aNativeStack); + + uint32_t maxFrames = uint32_t(MAX_NATIVE_FRAMES - aNativeStack.mCount); + + HANDLE thread = GetThreadHandle(aRegisteredThread.GetPlatformData()); + MOZ_ASSERT(thread); + MozStackWalkThread(StackWalkCallback, maxFrames, &aNativeStack, thread, + /* context */ nullptr); +} +#endif + +#ifdef USE_EHABI_STACKWALK +static void DoEHABIBacktrace(PSLockRef aLock, + const RegisteredThread& aRegisteredThread, + const Registers& aRegs, + NativeStack& aNativeStack) { + // WARNING: this function runs within the profiler's "critical section". + // WARNING: this function might be called while the profiler is inactive, and + // cannot rely on ActivePS. + + aNativeStack.mCount = + EHABIStackWalk(aRegs.mContext->uc_mcontext, + const_cast<void*>(aRegisteredThread.StackTop()), + aNativeStack.mSPs, aNativeStack.mPCs, MAX_NATIVE_FRAMES); +} +#endif + +#ifdef USE_LUL_STACKWALK + +// See the comment at the callsite for why this function is necessary. +# if defined(MOZ_HAVE_ASAN_IGNORE) +MOZ_ASAN_IGNORE static void ASAN_memcpy(void* aDst, const void* aSrc, + size_t aLen) { + // The obvious thing to do here is call memcpy(). However, although + // ASAN_memcpy() is not instrumented by ASAN, memcpy() still is, and the + // false positive still manifests! So we must implement memcpy() ourselves + // within this function. + char* dst = static_cast<char*>(aDst); + const char* src = static_cast<const char*>(aSrc); + + for (size_t i = 0; i < aLen; i++) { + dst[i] = src[i]; + } +} +# endif + +static void DoLULBacktrace(PSLockRef aLock, + const RegisteredThread& aRegisteredThread, + const Registers& aRegs, NativeStack& aNativeStack) { + // WARNING: this function runs within the profiler's "critical section". + // WARNING: this function might be called while the profiler is inactive, and + // cannot rely on ActivePS. + + const mcontext_t* mc = &aRegs.mContext->uc_mcontext; + + lul::UnwindRegs startRegs; + memset(&startRegs, 0, sizeof(startRegs)); + +# if defined(GP_PLAT_amd64_linux) || defined(GP_PLAT_amd64_android) + startRegs.xip = lul::TaggedUWord(mc->gregs[REG_RIP]); + startRegs.xsp = lul::TaggedUWord(mc->gregs[REG_RSP]); + startRegs.xbp = lul::TaggedUWord(mc->gregs[REG_RBP]); +# elif defined(GP_PLAT_amd64_freebsd) + startRegs.xip = lul::TaggedUWord(mc->mc_rip); + startRegs.xsp = lul::TaggedUWord(mc->mc_rsp); + startRegs.xbp = lul::TaggedUWord(mc->mc_rbp); +# elif defined(GP_PLAT_arm_linux) || defined(GP_PLAT_arm_android) + startRegs.r15 = lul::TaggedUWord(mc->arm_pc); + startRegs.r14 = lul::TaggedUWord(mc->arm_lr); + startRegs.r13 = lul::TaggedUWord(mc->arm_sp); + startRegs.r12 = lul::TaggedUWord(mc->arm_ip); + startRegs.r11 = lul::TaggedUWord(mc->arm_fp); + startRegs.r7 = lul::TaggedUWord(mc->arm_r7); +# elif defined(GP_PLAT_arm64_linux) || defined(GP_PLAT_arm64_android) + startRegs.pc = lul::TaggedUWord(mc->pc); + startRegs.x29 = lul::TaggedUWord(mc->regs[29]); + startRegs.x30 = lul::TaggedUWord(mc->regs[30]); + startRegs.sp = lul::TaggedUWord(mc->sp); +# elif defined(GP_PLAT_arm64_freebsd) + startRegs.pc = lul::TaggedUWord(mc->mc_gpregs.gp_elr); + startRegs.x29 = lul::TaggedUWord(mc->mc_gpregs.gp_x[29]); + startRegs.x30 = lul::TaggedUWord(mc->mc_gpregs.gp_lr); + startRegs.sp = lul::TaggedUWord(mc->mc_gpregs.gp_sp); +# elif defined(GP_PLAT_x86_linux) || defined(GP_PLAT_x86_android) + startRegs.xip = lul::TaggedUWord(mc->gregs[REG_EIP]); + startRegs.xsp = lul::TaggedUWord(mc->gregs[REG_ESP]); + startRegs.xbp = lul::TaggedUWord(mc->gregs[REG_EBP]); +# elif defined(GP_PLAT_mips64_linux) + startRegs.pc = lul::TaggedUWord(mc->pc); + startRegs.sp = lul::TaggedUWord(mc->gregs[29]); + startRegs.fp = lul::TaggedUWord(mc->gregs[30]); +# else +# error "Unknown plat" +# endif + + // Copy up to N_STACK_BYTES from rsp-REDZONE upwards, but not going past the + // stack's registered top point. Do some basic sanity checks too. This + // assumes that the TaggedUWord holding the stack pointer value is valid, but + // it should be, since it was constructed that way in the code just above. + + // We could construct |stackImg| so that LUL reads directly from the stack in + // question, rather than from a copy of it. That would reduce overhead and + // space use a bit. However, it gives a problem with dynamic analysis tools + // (ASan, TSan, Valgrind) which is that such tools will report invalid or + // racing memory accesses, and such accesses will be reported deep inside LUL. + // By taking a copy here, we can either sanitise the copy (for Valgrind) or + // copy it using an unchecked memcpy (for ASan, TSan). That way we don't have + // to try and suppress errors inside LUL. + // + // N_STACK_BYTES is set to 160KB. This is big enough to hold all stacks + // observed in some minutes of testing, whilst keeping the size of this + // function (DoNativeBacktrace)'s frame reasonable. Most stacks observed in + // practice are small, 4KB or less, and so the copy costs are insignificant + // compared to other profiler overhead. + // + // |stackImg| is allocated on this (the sampling thread's) stack. That + // implies that the frame for this function is at least N_STACK_BYTES large. + // In general it would be considered unacceptable to have such a large frame + // on a stack, but it only exists for the unwinder thread, and so is not + // expected to be a problem. Allocating it on the heap is troublesome because + // this function runs whilst the sampled thread is suspended, so any heap + // allocation risks deadlock. Allocating it as a global variable is not + // thread safe, which would be a problem if we ever allow multiple sampler + // threads. Hence allocating it on the stack seems to be the least-worst + // option. + + lul::StackImage stackImg; + + { +# if defined(GP_PLAT_amd64_linux) || defined(GP_PLAT_amd64_android) || \ + defined(GP_PLAT_amd64_freebsd) + uintptr_t rEDZONE_SIZE = 128; + uintptr_t start = startRegs.xsp.Value() - rEDZONE_SIZE; +# elif defined(GP_PLAT_arm_linux) || defined(GP_PLAT_arm_android) + uintptr_t rEDZONE_SIZE = 0; + uintptr_t start = startRegs.r13.Value() - rEDZONE_SIZE; +# elif defined(GP_PLAT_arm64_linux) || defined(GP_PLAT_arm64_android) || \ + defined(GP_PLAT_arm64_freebsd) + uintptr_t rEDZONE_SIZE = 0; + uintptr_t start = startRegs.sp.Value() - rEDZONE_SIZE; +# elif defined(GP_PLAT_x86_linux) || defined(GP_PLAT_x86_android) + uintptr_t rEDZONE_SIZE = 0; + uintptr_t start = startRegs.xsp.Value() - rEDZONE_SIZE; +# elif defined(GP_PLAT_mips64_linux) + uintptr_t rEDZONE_SIZE = 0; + uintptr_t start = startRegs.sp.Value() - rEDZONE_SIZE; +# else +# error "Unknown plat" +# endif + uintptr_t end = reinterpret_cast<uintptr_t>(aRegisteredThread.StackTop()); + uintptr_t ws = sizeof(void*); + start &= ~(ws - 1); + end &= ~(ws - 1); + uintptr_t nToCopy = 0; + if (start < end) { + nToCopy = end - start; + if (nToCopy > lul::N_STACK_BYTES) nToCopy = lul::N_STACK_BYTES; + } + MOZ_ASSERT(nToCopy <= lul::N_STACK_BYTES); + stackImg.mLen = nToCopy; + stackImg.mStartAvma = start; + if (nToCopy > 0) { + // If this is a vanilla memcpy(), ASAN makes the following complaint: + // + // ERROR: AddressSanitizer: stack-buffer-underflow ... + // ... + // HINT: this may be a false positive if your program uses some custom + // stack unwind mechanism or swapcontext + // + // This code is very much a custom stack unwind mechanism! So we use an + // alternative memcpy() implementation that is ignored by ASAN. +# if defined(MOZ_HAVE_ASAN_IGNORE) + ASAN_memcpy(&stackImg.mContents[0], (void*)start, nToCopy); +# else + memcpy(&stackImg.mContents[0], (void*)start, nToCopy); +# endif + (void)VALGRIND_MAKE_MEM_DEFINED(&stackImg.mContents[0], nToCopy); + } + } + + size_t framePointerFramesAcquired = 0; + lul::LUL* lul = CorePS::Lul(aLock); + lul->Unwind(reinterpret_cast<uintptr_t*>(aNativeStack.mPCs), + reinterpret_cast<uintptr_t*>(aNativeStack.mSPs), + &aNativeStack.mCount, &framePointerFramesAcquired, + MAX_NATIVE_FRAMES, &startRegs, &stackImg); + + // Update stats in the LUL stats object. Unfortunately this requires + // three global memory operations. + lul->mStats.mContext += 1; + lul->mStats.mCFI += aNativeStack.mCount - 1 - framePointerFramesAcquired; + lul->mStats.mFP += framePointerFramesAcquired; +} + +#endif + +#ifdef HAVE_NATIVE_UNWIND +static void DoNativeBacktrace(PSLockRef aLock, + const RegisteredThread& aRegisteredThread, + const Registers& aRegs, + NativeStack& aNativeStack) { + // This method determines which stackwalker is used for periodic and + // synchronous samples. (Backtrace samples are treated differently, see + // profiler_suspend_and_sample_thread() for details). The only part of the + // ordering that matters is that LUL must precede FRAME_POINTER, because on + // Linux they can both be present. +# if defined(USE_LUL_STACKWALK) + DoLULBacktrace(aLock, aRegisteredThread, aRegs, aNativeStack); +# elif defined(USE_EHABI_STACKWALK) + DoEHABIBacktrace(aLock, aRegisteredThread, aRegs, aNativeStack); +# elif defined(USE_FRAME_POINTER_STACK_WALK) + DoFramePointerBacktrace(aLock, aRegisteredThread, aRegs, aNativeStack); +# elif defined(USE_MOZ_STACK_WALK) + DoMozStackWalkBacktrace(aLock, aRegisteredThread, aRegs, aNativeStack); +# else +# error "Invalid configuration" +# endif +} +#endif + +// Writes some components shared by periodic and synchronous profiles to +// ActivePS's ProfileBuffer. (This should only be called from DoSyncSample() +// and DoPeriodicSample().) +// +// The grammar for entry sequences is in a comment above +// ProfileBuffer::StreamSamplesToJSON. +static inline void DoSharedSample( + PSLockRef aLock, bool aIsSynchronous, RegisteredThread& aRegisteredThread, + const Registers& aRegs, uint64_t aSamplePos, uint64_t aBufferRangeStart, + ProfileBuffer& aBuffer, + StackCaptureOptions aCaptureOptions = StackCaptureOptions::Full) { + // WARNING: this function runs within the profiler's "critical section". + + MOZ_ASSERT(!aBuffer.IsThreadSafe(), + "Mutexes cannot be used inside this critical section"); + + MOZ_RELEASE_ASSERT(ActivePS::Exists(aLock)); + + ProfileBufferCollector collector(aBuffer, aSamplePos, aBufferRangeStart); + NativeStack nativeStack; +#if defined(HAVE_NATIVE_UNWIND) + if (ActivePS::FeatureStackWalk(aLock) && + aCaptureOptions == StackCaptureOptions::Full) { + DoNativeBacktrace(aLock, aRegisteredThread, aRegs, nativeStack); + + MergeStacks(ActivePS::Features(aLock), aIsSynchronous, aRegisteredThread, + aRegs, nativeStack, collector); + } else +#endif + { + MergeStacks(ActivePS::Features(aLock), aIsSynchronous, aRegisteredThread, + aRegs, nativeStack, collector); + + // We can't walk the whole native stack, but we can record the top frame. + if (aCaptureOptions == StackCaptureOptions::Full) { + aBuffer.AddEntry(ProfileBufferEntry::NativeLeafAddr((void*)aRegs.mPC)); + } + } +} + +// Writes the components of a synchronous sample to the given ProfileBuffer. +static void DoSyncSample(PSLockRef aLock, RegisteredThread& aRegisteredThread, + const TimeStamp& aNow, const Registers& aRegs, + ProfileBuffer& aBuffer, + StackCaptureOptions aCaptureOptions) { + // WARNING: this function runs within the profiler's "critical section". + + MOZ_ASSERT(aCaptureOptions != StackCaptureOptions::NoStack, + "DoSyncSample should not be called when no capture is needed"); + + const uint64_t bufferRangeStart = aBuffer.BufferRangeStart(); + + const uint64_t samplePos = + aBuffer.AddThreadIdEntry(aRegisteredThread.Info()->ThreadId()); + + TimeDuration delta = aNow - CorePS::ProcessStartTime(); + aBuffer.AddEntry(ProfileBufferEntry::Time(delta.ToMilliseconds())); + + DoSharedSample(aLock, /* aIsSynchronous = */ true, aRegisteredThread, aRegs, + samplePos, bufferRangeStart, aBuffer, aCaptureOptions); +} + +// Writes the components of a periodic sample to ActivePS's ProfileBuffer. +// The ThreadId entry is already written in the main ProfileBuffer, its location +// is `aSamplePos`, we can write the rest to `aBuffer` (which may be different). +static void DoPeriodicSample(PSLockRef aLock, + RegisteredThread& aRegisteredThread, + ProfiledThreadData& aProfiledThreadData, + const Registers& aRegs, uint64_t aSamplePos, + uint64_t aBufferRangeStart, + ProfileBuffer& aBuffer) { + // WARNING: this function runs within the profiler's "critical section". + + DoSharedSample(aLock, /* aIsSynchronous = */ false, aRegisteredThread, aRegs, + aSamplePos, aBufferRangeStart, aBuffer); +} + +// END sampling/unwinding code +//////////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////////// +// BEGIN saving/streaming code + +const static uint64_t kJS_MAX_SAFE_UINTEGER = +9007199254740991ULL; + +static int64_t SafeJSInteger(uint64_t aValue) { + return aValue <= kJS_MAX_SAFE_UINTEGER ? int64_t(aValue) : -1; +} + +static void AddSharedLibraryInfoToStream(JSONWriter& aWriter, + const SharedLibrary& aLib) { + aWriter.StartObjectElement(); + aWriter.IntProperty("start", SafeJSInteger(aLib.GetStart())); + aWriter.IntProperty("end", SafeJSInteger(aLib.GetEnd())); + aWriter.IntProperty("offset", SafeJSInteger(aLib.GetOffset())); + aWriter.StringProperty("name", aLib.GetModuleName()); + aWriter.StringProperty("path", aLib.GetModulePath()); + aWriter.StringProperty("debugName", aLib.GetDebugName()); + aWriter.StringProperty("debugPath", aLib.GetDebugPath()); + aWriter.StringProperty("breakpadId", aLib.GetBreakpadId()); + aWriter.StringProperty("codeId", aLib.GetCodeId()); + aWriter.StringProperty("arch", aLib.GetArch()); + aWriter.EndObject(); +} + +void AppendSharedLibraries(JSONWriter& aWriter) { + SharedLibraryInfo info = SharedLibraryInfo::GetInfoForSelf(); + info.SortByAddress(); + for (size_t i = 0; i < info.GetSize(); i++) { + AddSharedLibraryInfoToStream(aWriter, info.GetEntry(i)); + } +} + +static void StreamCategories(SpliceableJSONWriter& aWriter) { + // Same order as ProfilingCategory. Format: + // [ + // { + // name: "Idle", + // color: "transparent", + // subcategories: ["Other"], + // }, + // { + // name: "Other", + // color: "grey", + // subcategories: [ + // "JSM loading", + // "Subprocess launching", + // "DLL loading" + // ] + // }, + // ... + // ] + +#define CATEGORY_JSON_BEGIN_CATEGORY(name, labelAsString, color) \ + aWriter.Start(); \ + aWriter.StringProperty("name", labelAsString); \ + aWriter.StringProperty("color", color); \ + aWriter.StartArrayProperty("subcategories"); +#define CATEGORY_JSON_SUBCATEGORY(supercategory, name, labelAsString) \ + aWriter.StringElement(labelAsString); +#define CATEGORY_JSON_END_CATEGORY \ + aWriter.EndArray(); \ + aWriter.EndObject(); + + MOZ_PROFILING_CATEGORY_LIST(CATEGORY_JSON_BEGIN_CATEGORY, + CATEGORY_JSON_SUBCATEGORY, + CATEGORY_JSON_END_CATEGORY) + +#undef CATEGORY_JSON_BEGIN_CATEGORY +#undef CATEGORY_JSON_SUBCATEGORY +#undef CATEGORY_JSON_END_CATEGORY +} + +static void StreamMarkerSchema(SpliceableJSONWriter& aWriter) { + // Get an array view with all registered marker-type-specific functions. + base_profiler_markers_detail::Streaming::LockedMarkerTypeFunctionsList + markerTypeFunctionsArray; + // List of streamed marker names, this is used to spot duplicates. + std::set<std::string> names; + // Stream the display schema for each different one. (Duplications may come + // from the same code potentially living in different libraries.) + for (const auto& markerTypeFunctions : markerTypeFunctionsArray) { + auto name = markerTypeFunctions.mMarkerTypeNameFunction(); + // std::set.insert(T&&) returns a pair, its `second` is true if the element + // was actually inserted (i.e., it was not there yet.) + const bool didInsert = + names.insert(std::string(name.data(), name.size())).second; + if (didInsert) { + markerTypeFunctions.mMarkerSchemaFunction().Stream(aWriter, name); + } + } +} + +static int64_t MicrosecondsSince1970(); + +static void StreamMetaJSCustomObject(PSLockRef aLock, + SpliceableJSONWriter& aWriter, + bool aIsShuttingDown) { + MOZ_RELEASE_ASSERT(CorePS::Exists() && ActivePS::Exists(aLock)); + + aWriter.IntProperty("version", 27); + + // The "startTime" field holds the number of milliseconds since midnight + // January 1, 1970 GMT. This grotty code computes (Now - (Now - + // ProcessStartTime)) to convert CorePS::ProcessStartTime() into that form. + // Note: This is the only absolute time in the profile! All other timestamps + // are relative to this startTime. + TimeDuration delta = TimeStamp::Now() - CorePS::ProcessStartTime(); + aWriter.DoubleProperty( + "startTime", MicrosecondsSince1970() / 1000.0 - delta.ToMilliseconds()); + + aWriter.DoubleProperty("profilingStartTime", (ActivePS::ProfilingStartTime() - + CorePS::ProcessStartTime()) + .ToMilliseconds()); + + if (const TimeStamp contentEarliestTime = + ActivePS::Buffer(aLock) + .UnderlyingChunkedBuffer() + .GetEarliestChunkStartTimeStamp(); + !contentEarliestTime.IsNull()) { + aWriter.DoubleProperty( + "contentEarliestTime", + (contentEarliestTime - CorePS::ProcessStartTime()).ToMilliseconds()); + } else { + aWriter.NullProperty("contentEarliestTime"); + } + + const double profilingEndTime = profiler_time(); + aWriter.DoubleProperty("profilingEndTime", profilingEndTime); + + if (aIsShuttingDown) { + aWriter.DoubleProperty("shutdownTime", profilingEndTime); + } else { + aWriter.NullProperty("shutdownTime"); + } + + aWriter.StartArrayProperty("categories"); + StreamCategories(aWriter); + aWriter.EndArray(); + + aWriter.StartArrayProperty("markerSchema"); + StreamMarkerSchema(aWriter); + aWriter.EndArray(); + + if (!profiler_is_main_thread()) { + // Leave the rest of the properties out if we're not on the main thread. + // At the moment, the only case in which this function is called on a + // background thread is if we're in a content process and are going to + // send this profile to the parent process. In that case, the parent + // process profile's "meta" object already has the rest of the properties, + // and the parent process profile is dumped on that process's main thread. + return; + } + + aWriter.DoubleProperty("interval", ActivePS::Interval(aLock)); + aWriter.IntProperty("stackwalk", ActivePS::FeatureStackWalk(aLock)); + +#ifdef DEBUG + aWriter.IntProperty("debug", 1); +#else + aWriter.IntProperty("debug", 0); +#endif + + aWriter.IntProperty("gcpoison", 0); + + aWriter.IntProperty("asyncstack", 0); + + aWriter.IntProperty("processType", 0); +} + +static void StreamPages(PSLockRef aLock, SpliceableJSONWriter& aWriter) { + MOZ_RELEASE_ASSERT(CorePS::Exists()); + ActivePS::DiscardExpiredPages(aLock); + for (const auto& page : ActivePS::ProfiledPages(aLock)) { + page->StreamJSON(aWriter); + } +} + +static void locked_profiler_stream_json_for_this_process( + PSLockRef aLock, SpliceableJSONWriter& aWriter, double aSinceTime, + bool aIsShuttingDown, bool aOnlyThreads = false) { + LOG("locked_profiler_stream_json_for_this_process"); + + MOZ_RELEASE_ASSERT(CorePS::Exists() && ActivePS::Exists(aLock)); + + AUTO_PROFILER_STATS(base_locked_profiler_stream_json_for_this_process); + + const double collectionStartMs = profiler_time(); + + ProfileBuffer& buffer = ActivePS::Buffer(aLock); + + // If there is a set "Window length", discard older data. + Maybe<double> durationS = ActivePS::Duration(aLock); + if (durationS.isSome()) { + const double durationStartMs = collectionStartMs - *durationS * 1000; + buffer.DiscardSamplesBeforeTime(durationStartMs); + } + + if (!aOnlyThreads) { + // Put shared library info + aWriter.StartArrayProperty("libs"); + AppendSharedLibraries(aWriter); + aWriter.EndArray(); + + // Put meta data + aWriter.StartObjectProperty("meta"); + { StreamMetaJSCustomObject(aLock, aWriter, aIsShuttingDown); } + aWriter.EndObject(); + + // Put page data + aWriter.StartArrayProperty("pages"); + { StreamPages(aLock, aWriter); } + aWriter.EndArray(); + + buffer.StreamProfilerOverheadToJSON(aWriter, CorePS::ProcessStartTime(), + aSinceTime); + buffer.StreamCountersToJSON(aWriter, CorePS::ProcessStartTime(), + aSinceTime); + + // Lists the samples for each thread profile + aWriter.StartArrayProperty("threads"); + } + + // if aOnlyThreads is true, the only output will be the threads array items. + { + ActivePS::DiscardExpiredDeadProfiledThreads(aLock); + Vector<std::pair<RegisteredThread*, ProfiledThreadData*>> threads = + ActivePS::ProfiledThreads(aLock); + for (auto& thread : threads) { + ProfiledThreadData* profiledThreadData = thread.second; + profiledThreadData->StreamJSON( + buffer, aWriter, CorePS::ProcessName(aLock), CorePS::ETLDplus1(aLock), + CorePS::ProcessStartTime(), aSinceTime); + } + } + + if (!aOnlyThreads) { + aWriter.EndArray(); + + aWriter.StartArrayProperty("pausedRanges"); + { buffer.StreamPausedRangesToJSON(aWriter, aSinceTime); } + aWriter.EndArray(); + } + + const double collectionEndMs = profiler_time(); + + // Record timestamps for the collection into the buffer, so that consumers + // know why we didn't collect any samples for its duration. + // We put these entries into the buffer after we've collected the profile, + // so they'll be visible for the *next* profile collection (if they haven't + // been overwritten due to buffer wraparound by then). + buffer.AddEntry(ProfileBufferEntry::CollectionStart(collectionStartMs)); + buffer.AddEntry(ProfileBufferEntry::CollectionEnd(collectionEndMs)); +} + +bool profiler_stream_json_for_this_process(SpliceableJSONWriter& aWriter, + double aSinceTime, + bool aIsShuttingDown, + bool aOnlyThreads) { + LOG("profiler_stream_json_for_this_process"); + + MOZ_RELEASE_ASSERT(CorePS::Exists()); + + PSAutoLock lock; + + if (!ActivePS::Exists(lock)) { + return false; + } + + locked_profiler_stream_json_for_this_process(lock, aWriter, aSinceTime, + aIsShuttingDown, aOnlyThreads); + return true; +} + +// END saving/streaming code +//////////////////////////////////////////////////////////////////////// + +static char FeatureCategory(uint32_t aFeature) { + if (aFeature & DefaultFeatures()) { + if (aFeature & AvailableFeatures()) { + return 'D'; + } + return 'd'; + } + + if (aFeature & StartupExtraDefaultFeatures()) { + if (aFeature & AvailableFeatures()) { + return 'S'; + } + return 's'; + } + + if (aFeature & AvailableFeatures()) { + return '-'; + } + return 'x'; +} + +static void PrintUsage() { + PrintToConsole( + "\n" + "Profiler environment variable usage:\n" + "\n" + " MOZ_BASE_PROFILER_HELP\n" + " If set to any value, prints this message.\n" + " (Only BaseProfiler features are known here; Use MOZ_PROFILER_HELP\n" + " for Gecko Profiler help, with more features).\n" + "\n" + " MOZ_BASE_PROFILER_{,DEBUG_,VERBOSE}LOGGING\n" + " Enables BaseProfiler logging to stdout. The levels of logging\n" + " available are MOZ_BASE_PROFILER_LOGGING' (least verbose),\n" + " '..._DEBUG_LOGGING', '..._VERBOSE_LOGGING' (most verbose)\n" + "\n" + " MOZ_PROFILER_STARTUP\n" + " If set to any value other than '' or '0'/'N'/'n', starts the\n" + " profiler immediately on start-up.\n" + " Useful if you want profile code that runs very early.\n" + "\n" + " MOZ_PROFILER_STARTUP_ENTRIES=<%u..%u>\n" + " If MOZ_PROFILER_STARTUP is set, specifies the number of entries\n" + " per process in the profiler's circular buffer when the profiler is\n" + " first started.\n" + " If unset, the platform default is used:\n" + " %u entries per process, or %u when MOZ_PROFILER_STARTUP is set.\n" + " (%u bytes per entry -> %u or %u total bytes per process)\n" + " Optional units in bytes: KB, KiB, MB, MiB, GB, GiB\n" + "\n" + " MOZ_PROFILER_STARTUP_DURATION=<1..>\n" + " If MOZ_PROFILER_STARTUP is set, specifies the maximum life time\n" + " of entries in the the profiler's circular buffer when the profiler\n" + " is first started, in seconds.\n" + " If unset, the life time of the entries will only be restricted by\n" + " MOZ_PROFILER_STARTUP_ENTRIES (or its default value), and no\n" + " additional time duration restriction will be applied.\n" + "\n" + " MOZ_PROFILER_STARTUP_INTERVAL=<1..1000>\n" + " If MOZ_PROFILER_STARTUP is set, specifies the sample interval,\n" + " measured in milliseconds, when the profiler is first started.\n" + " If unset, the platform default is used.\n" + "\n" + " MOZ_PROFILER_STARTUP_FEATURES_BITFIELD=<Number>\n" + " If MOZ_PROFILER_STARTUP is set, specifies the profiling\n" + " features, as the integer value of the features bitfield.\n" + " If unset, the value from MOZ_PROFILER_STARTUP_FEATURES is used.\n" + "\n" + " MOZ_PROFILER_STARTUP_FEATURES=<Features>\n" + " If MOZ_PROFILER_STARTUP is set, specifies the profiling\n" + " features, as a comma-separated list of strings.\n" + " Ignored if MOZ_PROFILER_STARTUP_FEATURES_BITFIELD is set.\n" + " If unset, the platform default is used.\n" + "\n" + " Features: (x=unavailable, D/d=default/unavailable,\n" + " S/s=MOZ_PROFILER_STARTUP extra " + "default/unavailable)\n", + unsigned(ActivePS::scMinimumBufferEntries), + unsigned(ActivePS::scMaximumBufferEntries), + unsigned(BASE_PROFILER_DEFAULT_ENTRIES.Value()), + unsigned(BASE_PROFILER_DEFAULT_STARTUP_ENTRIES.Value()), + unsigned(scBytesPerEntry), + unsigned(BASE_PROFILER_DEFAULT_ENTRIES.Value() * scBytesPerEntry), + unsigned(BASE_PROFILER_DEFAULT_STARTUP_ENTRIES.Value() * + scBytesPerEntry)); + +#define PRINT_FEATURE(n_, str_, Name_, desc_) \ + PrintToConsole(" %c %7u: \"%s\" (%s)\n", \ + FeatureCategory(ProfilerFeature::Name_), \ + ProfilerFeature::Name_, str_, desc_); + + BASE_PROFILER_FOR_EACH_FEATURE(PRINT_FEATURE) + +#undef PRINT_FEATURE + + PrintToConsole( + " - \"default\" (All above D+S defaults)\n" + "\n" + " MOZ_PROFILER_STARTUP_FILTERS=<Filters>\n" + " If MOZ_PROFILER_STARTUP is set, specifies the thread filters, as " + "a\n" + " comma-separated list of strings. A given thread will be sampled if\n" + " any of the filters is a case-insensitive substring of the thread\n" + " name. If unset, a default is used.\n" + "\n" + " MOZ_PROFILER_SHUTDOWN\n" + " If set, the profiler saves a profile to the named file on shutdown.\n" + "\n" + " MOZ_PROFILER_SYMBOLICATE\n" + " If set, the profiler will pre-symbolicate profiles.\n" + " *Note* This will add a significant pause when gathering data, and\n" + " is intended mainly for local development.\n" + "\n" + " MOZ_PROFILER_LUL_TEST\n" + " If set to any value, runs LUL unit tests at startup.\n" + "\n" + " This platform %s native unwinding.\n" + "\n", +#if defined(HAVE_NATIVE_UNWIND) + "supports" +#else + "does not support" +#endif + ); +} + +//////////////////////////////////////////////////////////////////////// +// BEGIN Sampler + +#if defined(GP_OS_linux) || defined(GP_OS_android) +struct SigHandlerCoordinator; +#endif + +// Sampler performs setup and teardown of the state required to sample with the +// profiler. Sampler may exist when ActivePS is not present. +// +// SuspendAndSampleAndResumeThread must only be called from a single thread, +// and must not sample the thread it is being called from. A separate Sampler +// instance must be used for each thread which wants to capture samples. + +// WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING +// +// With the exception of SamplerThread, all Sampler objects must be Disable-d +// before releasing the lock which was used to create them. This avoids races +// on linux with the SIGPROF signal handler. + +class Sampler { + public: + // Sets up the profiler such that it can begin sampling. + explicit Sampler(PSLockRef aLock); + + // Disable the sampler, restoring it to its previous state. This must be + // called once, and only once, before the Sampler is destroyed. + void Disable(PSLockRef aLock); + + // This method suspends and resumes the samplee thread. It calls the passed-in + // function-like object aProcessRegs (passing it a populated |const + // Registers&| arg) while the samplee thread is suspended. + // + // Func must be a function-like object of type `void()`. + template <typename Func> + void SuspendAndSampleAndResumeThread( + PSLockRef aLock, const RegisteredThread& aRegisteredThread, + const TimeStamp& aNow, const Func& aProcessRegs); + + private: +#if defined(GP_OS_linux) || defined(GP_OS_android) || defined(GP_OS_freebsd) + // Used to restore the SIGPROF handler when ours is removed. + struct sigaction mOldSigprofHandler; + + // This process' ID. Needed as an argument for tgkill in + // SuspendAndSampleAndResumeThread. + BaseProfilerProcessId mMyPid; + + // The sampler thread's ID. Used to assert that it is not sampling itself, + // which would lead to deadlock. + BaseProfilerThreadId mSamplerTid; + + public: + // This is the one-and-only variable used to communicate between the sampler + // thread and the samplee thread's signal handler. It's static because the + // samplee thread's signal handler is static. + static struct SigHandlerCoordinator* sSigHandlerCoordinator; +#endif +}; + +// END Sampler +//////////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////////// +// BEGIN SamplerThread + +// The sampler thread controls sampling and runs whenever the profiler is +// active. It periodically runs through all registered threads, finds those +// that should be sampled, then pauses and samples them. + +class SamplerThread { + public: + // Creates a sampler thread, but doesn't start it. + SamplerThread(PSLockRef aLock, uint32_t aActivityGeneration, + double aIntervalMilliseconds, uint32_t aFeatures); + ~SamplerThread(); + + // This runs on (is!) the sampler thread. + void Run(); + + // This runs on the main thread. + void Stop(PSLockRef aLock); + + private: + // This suspends the calling thread for the given number of microseconds. + // Best effort timing. + void SleepMicro(uint32_t aMicroseconds); + + // The sampler used to suspend and sample threads. + Sampler mSampler; + + // The activity generation, for detecting when the sampler thread must stop. + const uint32_t mActivityGeneration; + + // The interval between samples, measured in microseconds. + const int mIntervalMicroseconds; + + // The OS-specific handle for the sampler thread. +#if defined(GP_OS_windows) + HANDLE mThread; +#elif defined(GP_OS_darwin) || defined(GP_OS_linux) || \ + defined(GP_OS_android) || defined(GP_OS_freebsd) + pthread_t mThread; +#endif + +#if defined(GP_OS_windows) + bool mNoTimerResolutionChange = true; +#endif + + SamplerThread(const SamplerThread&) = delete; + void operator=(const SamplerThread&) = delete; +}; + +// This function is required because we need to create a SamplerThread within +// ActivePS's constructor, but SamplerThread is defined after ActivePS. It +// could probably be removed by moving some code around. +static SamplerThread* NewSamplerThread(PSLockRef aLock, uint32_t aGeneration, + double aInterval, uint32_t aFeatures) { + return new SamplerThread(aLock, aGeneration, aInterval, aFeatures); +} + +// This function is the sampler thread. This implementation is used for all +// targets. +void SamplerThread::Run() { + // TODO: If possible, name this thread later on, after NSPR becomes available. + // PR_SetCurrentThreadName("SamplerThread"); + + // Features won't change during this SamplerThread's lifetime, so we can read + // them once and store them locally. + const uint32_t features = []() -> uint32_t { + PSAutoLock lock; + if (!ActivePS::Exists(lock)) { + // If there is no active profiler, it doesn't matter what we return, + // because this thread will exit before any feature is used. + return 0; + } + return ActivePS::Features(lock); + }(); + + // Not *no*-stack-sampling means we do want stack sampling. + const bool stackSampling = !ProfilerFeature::HasNoStackSampling(features); + + // Use local BlocksRingBuffer&ProfileBuffer to capture the stack. + // (This is to avoid touching the CorePS::CoreBuffer lock while + // a thread is suspended, because that thread could be working with + // the CorePS::CoreBuffer as well.) + ProfileBufferChunkManagerSingle localChunkManager( + ProfileBufferChunkManager::scExpectedMaximumStackSize); + ProfileChunkedBuffer localBuffer( + ProfileChunkedBuffer::ThreadSafety::WithoutMutex, localChunkManager); + ProfileBuffer localProfileBuffer(localBuffer); + + // Will be kept between collections, to know what each collection does. + auto previousState = localBuffer.GetState(); + + // This will be positive if we are running behind schedule (sampling less + // frequently than desired) and negative if we are ahead of schedule. + TimeDuration lastSleepOvershoot = 0; + TimeStamp sampleStart = TimeStamp::Now(); + + while (true) { + // This scope is for |lock|. It ends before we sleep below. + { + PSAutoLock lock; + TimeStamp lockAcquired = TimeStamp::Now(); + + if (!ActivePS::Exists(lock)) { + return; + } + + // At this point profiler_stop() might have been called, and + // profiler_start() might have been called on another thread. If this + // happens the generation won't match. + if (ActivePS::Generation(lock) != mActivityGeneration) { + return; + } + + ActivePS::ClearExpiredExitProfiles(lock); + + TimeStamp expiredMarkersCleaned = TimeStamp::Now(); + + if (int(gSkipSampling) <= 0 && !ActivePS::IsSamplingPaused(lock)) { + TimeDuration delta = sampleStart - CorePS::ProcessStartTime(); + ProfileBuffer& buffer = ActivePS::Buffer(lock); + + // handle per-process generic counters + const Vector<BaseProfilerCount*>& counters = CorePS::Counters(lock); + for (auto& counter : counters) { + // create Buffer entries for each counter + buffer.AddEntry(ProfileBufferEntry::CounterId(counter)); + buffer.AddEntry(ProfileBufferEntry::Time(delta.ToMilliseconds())); + // XXX support keyed maps of counts + // In the future, we'll support keyed counters - for example, counters + // with a key which is a thread ID. For "simple" counters we'll just + // use a key of 0. + int64_t count; + uint64_t number; + counter->Sample(count, number); + buffer.AddEntry(ProfileBufferEntry::CounterKey(0)); + buffer.AddEntry(ProfileBufferEntry::Count(count)); + if (number) { + buffer.AddEntry(ProfileBufferEntry::Number(number)); + } + } + TimeStamp countersSampled = TimeStamp::Now(); + + if (stackSampling) { + const Vector<LiveProfiledThreadData>& liveThreads = + ActivePS::LiveProfiledThreads(lock); + + for (auto& thread : liveThreads) { + RegisteredThread* registeredThread = thread.mRegisteredThread; + ProfiledThreadData* profiledThreadData = + thread.mProfiledThreadData.get(); + RefPtr<ThreadInfo> info = registeredThread->Info(); + + // If the thread is asleep and has been sampled before in the same + // sleep episode, find and copy the previous sample, as that's + // cheaper than taking a new sample. + if (registeredThread->RacyRegisteredThread() + .CanDuplicateLastSampleDueToSleep()) { + bool dup_ok = ActivePS::Buffer(lock).DuplicateLastSample( + info->ThreadId(), CorePS::ProcessStartTime(), + profiledThreadData->LastSample()); + if (dup_ok) { + continue; + } + } + + AUTO_PROFILER_STATS(base_SamplerThread_Run_DoPeriodicSample); + + TimeStamp now = TimeStamp::Now(); + + // Record the global profiler buffer's range start now, before + // adding the first entry for this thread's sample. + const uint64_t bufferRangeStart = buffer.BufferRangeStart(); + + // Add the thread ID now, so we know its position in the main + // buffer, which is used by some JS data. (DoPeriodicSample only + // knows about the temporary local buffer.) + const uint64_t samplePos = + buffer.AddThreadIdEntry(registeredThread->Info()->ThreadId()); + profiledThreadData->LastSample() = Some(samplePos); + + // Also add the time, so it's always there after the thread ID, as + // expected by the parser. (Other stack data is optional.) + TimeDuration delta = now - CorePS::ProcessStartTime(); + buffer.AddEntry(ProfileBufferEntry::Time(delta.ToMilliseconds())); + + mSampler.SuspendAndSampleAndResumeThread( + lock, *registeredThread, now, + [&](const Registers& aRegs, const TimeStamp& aNow) { + DoPeriodicSample(lock, *registeredThread, *profiledThreadData, + aRegs, samplePos, bufferRangeStart, + localProfileBuffer); + }); + + // If data is complete, copy it into the global buffer. + auto state = localBuffer.GetState(); + if (state.mClearedBlockCount != previousState.mClearedBlockCount) { + LOG("Stack sample too big for local storage, needed %u bytes", + unsigned(state.mRangeEnd - previousState.mRangeEnd)); + } else if (state.mRangeEnd - previousState.mRangeEnd >= + *profiler_get_core_buffer().BufferLength()) { + LOG("Stack sample too big for profiler storage, needed %u bytes", + unsigned(state.mRangeEnd - previousState.mRangeEnd)); + } else { + profiler_get_core_buffer().AppendContents(localBuffer); + } + + // Clean up for the next run. + localBuffer.Clear(); + previousState = localBuffer.GetState(); + } + } + +#if defined(USE_LUL_STACKWALK) + // The LUL unwind object accumulates frame statistics. Periodically we + // should poke it to give it a chance to print those statistics. This + // involves doing I/O (fprintf, __android_log_print, etc.) and so + // can't safely be done from the critical section inside + // SuspendAndSampleAndResumeThread, which is why it is done here. + lul::LUL* lul = CorePS::Lul(lock); + if (lul) { + lul->MaybeShowStats(); + } +#endif + TimeStamp threadsSampled = TimeStamp::Now(); + + { + AUTO_PROFILER_STATS(Sampler_FulfillChunkRequests); + ActivePS::FulfillChunkRequests(lock); + } + + buffer.CollectOverheadStats(delta, lockAcquired - sampleStart, + expiredMarkersCleaned - lockAcquired, + countersSampled - expiredMarkersCleaned, + threadsSampled - countersSampled); + } + } + // gPSMutex is not held after this point. + + // Calculate how long a sleep to request. After the sleep, measure how + // long we actually slept and take the difference into account when + // calculating the sleep interval for the next iteration. This is an + // attempt to keep "to schedule" in the presence of inaccuracy of the + // actual sleep intervals. + TimeStamp targetSleepEndTime = + sampleStart + TimeDuration::FromMicroseconds(mIntervalMicroseconds); + TimeStamp beforeSleep = TimeStamp::Now(); + TimeDuration targetSleepDuration = targetSleepEndTime - beforeSleep; + double sleepTime = std::max( + 0.0, (targetSleepDuration - lastSleepOvershoot).ToMicroseconds()); + SleepMicro(static_cast<uint32_t>(sleepTime)); + sampleStart = TimeStamp::Now(); + lastSleepOvershoot = + sampleStart - (beforeSleep + TimeDuration::FromMicroseconds(sleepTime)); + } +} + +// Temporary closing namespaces from enclosing platform.cpp. +} // namespace baseprofiler +} // namespace mozilla + +// We #include these files directly because it means those files can use +// declarations from this file trivially. These provide target-specific +// implementations of all SamplerThread methods except Run(). +#if defined(GP_OS_windows) +# include "platform-win32.cpp" +#elif defined(GP_OS_darwin) +# include "platform-macos.cpp" +#elif defined(GP_OS_linux) || defined(GP_OS_android) || defined(GP_OS_freebsd) +# include "platform-linux-android.cpp" +#else +# error "bad platform" +#endif + +namespace mozilla { +namespace baseprofiler { + +UniquePlatformData AllocPlatformData(BaseProfilerThreadId aThreadId) { + return UniquePlatformData(new PlatformData(aThreadId)); +} + +void PlatformDataDestructor::operator()(PlatformData* aData) { delete aData; } + +// END SamplerThread +//////////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////////// +// BEGIN externally visible functions + +static uint32_t ParseFeature(const char* aFeature, bool aIsStartup) { + if (strcmp(aFeature, "default") == 0) { + return (aIsStartup ? (DefaultFeatures() | StartupExtraDefaultFeatures()) + : DefaultFeatures()) & + AvailableFeatures(); + } + +#define PARSE_FEATURE_BIT(n_, str_, Name_, desc_) \ + if (strcmp(aFeature, str_) == 0) { \ + return ProfilerFeature::Name_; \ + } + + BASE_PROFILER_FOR_EACH_FEATURE(PARSE_FEATURE_BIT) + +#undef PARSE_FEATURE_BIT + + PrintToConsole("\nUnrecognized feature \"%s\".\n\n", aFeature); + // Since we may have an old feature we don't implement anymore, don't exit. + PrintUsage(); + return 0; +} + +uint32_t ParseFeaturesFromStringArray(const char** aFeatures, + uint32_t aFeatureCount, + bool aIsStartup /* = false */) { + uint32_t features = 0; + for (size_t i = 0; i < aFeatureCount; i++) { + features |= ParseFeature(aFeatures[i], aIsStartup); + } + return features; +} + +// Find the RegisteredThread for the current thread. This should only be called +// in places where TLSRegisteredThread can't be used. +static RegisteredThread* FindCurrentThreadRegisteredThread(PSLockRef aLock) { + BaseProfilerThreadId id = profiler_current_thread_id(); + const Vector<UniquePtr<RegisteredThread>>& registeredThreads = + CorePS::RegisteredThreads(aLock); + for (auto& registeredThread : registeredThreads) { + if (registeredThread->Info()->ThreadId() == id) { + return registeredThread.get(); + } + } + + return nullptr; +} + +static ProfilingStack* locked_register_thread(PSLockRef aLock, + const char* aName, + void* aStackTop) { + MOZ_RELEASE_ASSERT(CorePS::Exists()); + + MOZ_ASSERT(!FindCurrentThreadRegisteredThread(aLock)); + + VTUNE_REGISTER_THREAD(aName); + + if (!TLSRegisteredThread::Init(aLock)) { + return nullptr; + } + + RefPtr<ThreadInfo> info = new ThreadInfo(aName, profiler_current_thread_id(), + profiler_is_main_thread()); + UniquePtr<RegisteredThread> registeredThread = + MakeUnique<RegisteredThread>(info, aStackTop); + + TLSRegisteredThread::SetRegisteredThread(aLock, registeredThread.get()); + + if (ActivePS::Exists(aLock) && ActivePS::ShouldProfileThread(aLock, info)) { + registeredThread->RacyRegisteredThread().SetIsBeingProfiled(true); + ActivePS::AddLiveProfiledThread(aLock, registeredThread.get(), + MakeUnique<ProfiledThreadData>(info)); + } + + ProfilingStack* profilingStack = + ®isteredThread->RacyRegisteredThread().ProfilingStack(); + + CorePS::AppendRegisteredThread(aLock, std::move(registeredThread)); + + return profilingStack; +} + +static void locked_profiler_start(PSLockRef aLock, PowerOfTwo32 aCapacity, + double aInterval, uint32_t aFeatures, + const char** aFilters, uint32_t aFilterCount, + const Maybe<double>& aDuration); + +static Vector<const char*> SplitAtCommas(const char* aString, + UniquePtr<char[]>& aStorage) { + size_t len = strlen(aString); + aStorage = MakeUnique<char[]>(len + 1); + PodCopy(aStorage.get(), aString, len + 1); + + // Iterate over all characters in aStorage and split at commas, by + // overwriting commas with the null char. + Vector<const char*> array; + size_t currentElementStart = 0; + for (size_t i = 0; i <= len; i++) { + if (aStorage[i] == ',') { + aStorage[i] = '\0'; + } + if (aStorage[i] == '\0') { + // Only add non-empty elements, otherwise ParseFeatures would later + // complain about unrecognized features. + if (currentElementStart != i) { + MOZ_RELEASE_ASSERT(array.append(&aStorage[currentElementStart])); + } + currentElementStart = i + 1; + } + } + return array; +} + +static const char* get_size_suffix(const char* str) { + const char* ptr = str; + + while (isdigit(*ptr)) { + ptr++; + } + + return ptr; +} + +void profiler_init(void* aStackTop) { + LOG("profiler_init"); + + profiler_init_main_thread_id(); + + VTUNE_INIT(); + + MOZ_RELEASE_ASSERT(!CorePS::Exists()); + + if (getenv("MOZ_BASE_PROFILER_HELP")) { + PrintUsage(); + exit(0); + } + + SharedLibraryInfo::Initialize(); + + uint32_t features = DefaultFeatures() & AvailableFeatures(); + + UniquePtr<char[]> filterStorage; + + Vector<const char*> filters; + MOZ_RELEASE_ASSERT(filters.append(kMainThreadName)); + + PowerOfTwo32 capacity = BASE_PROFILER_DEFAULT_ENTRIES; + Maybe<double> duration = Nothing(); + double interval = BASE_PROFILER_DEFAULT_INTERVAL; + + { + PSAutoLock lock; + + // We've passed the possible failure point. Instantiate CorePS, which + // indicates that the profiler has initialized successfully. + CorePS::Create(lock); + + Unused << locked_register_thread(lock, kMainThreadName, aStackTop); + + // Platform-specific initialization. + PlatformInit(lock); + + // (Linux-only) We could create CorePS::mLul and read unwind info into it + // at this point. That would match the lifetime implied by destruction of + // it in profiler_shutdown() just below. However, that gives a big delay on + // startup, even if no profiling is actually to be done. So, instead, it is + // created on demand at the first call to PlatformStart(). + + const char* startupEnv = getenv("MOZ_PROFILER_STARTUP"); + if (!startupEnv || startupEnv[0] == '\0' || + ((startupEnv[0] == '0' || startupEnv[0] == 'N' || + startupEnv[0] == 'n') && + startupEnv[1] == '\0')) { + return; + } + + // Hidden option to stop Base Profiler, mostly due to Talos intermittents, + // see https://bugzilla.mozilla.org/show_bug.cgi?id=1638851#c3 + // TODO: Investigate root cause and remove this in bugs 1648324 and 1648325. + if (getenv("MOZ_PROFILER_STARTUP_NO_BASE")) { + return; + } + + LOG("- MOZ_PROFILER_STARTUP is set"); + + // Startup default capacity may be different. + capacity = BASE_PROFILER_DEFAULT_STARTUP_ENTRIES; + + const char* startupCapacity = getenv("MOZ_PROFILER_STARTUP_ENTRIES"); + if (startupCapacity && startupCapacity[0] != '\0') { + errno = 0; + long capacityLong = strtol(startupCapacity, nullptr, 10); + std::string_view sizeSuffix = get_size_suffix(startupCapacity); + + if (sizeSuffix == "KB") { + capacityLong *= 1000 / scBytesPerEntry; + } else if (sizeSuffix == "KiB") { + capacityLong *= 1024 / scBytesPerEntry; + } else if (sizeSuffix == "MB") { + capacityLong *= (1000 * 1000) / scBytesPerEntry; + } else if (sizeSuffix == "MiB") { + capacityLong *= (1024 * 1024) / scBytesPerEntry; + } else if (sizeSuffix == "GB") { + capacityLong *= (1000 * 1000 * 1000) / scBytesPerEntry; + } else if (sizeSuffix == "GiB") { + capacityLong *= (1024 * 1024 * 1024) / scBytesPerEntry; + } else if (!sizeSuffix.empty()) { + PrintToConsole( + "- MOZ_PROFILER_STARTUP_ENTRIES unit must be one of the " + "following: KB, KiB, MB, MiB, GB, GiB"); + PrintUsage(); + exit(1); + } + + // `long` could be 32 or 64 bits, so we force a 64-bit comparison with + // the maximum 32-bit signed number (as more than that is clamped down to + // 2^31 anyway). + if (errno == 0 && capacityLong > 0 && + static_cast<uint64_t>(capacityLong) <= + static_cast<uint64_t>(INT32_MAX)) { + capacity = PowerOfTwo32(ActivePS::ClampToAllowedEntries( + static_cast<uint32_t>(capacityLong))); + LOG("- MOZ_PROFILER_STARTUP_ENTRIES = %u", unsigned(capacity.Value())); + } else { + PrintToConsole("- MOZ_PROFILER_STARTUP_ENTRIES not a valid integer: %s", + startupCapacity); + PrintUsage(); + exit(1); + } + } + + const char* startupDuration = getenv("MOZ_PROFILER_STARTUP_DURATION"); + if (startupDuration && startupDuration[0] != '\0') { + // The duration is a floating point number. Use StringToDouble rather than + // strtod, so that "." is used as the decimal separator regardless of OS + // locale. + auto durationVal = StringToDouble(std::string(startupDuration)); + if (durationVal && *durationVal >= 0.0) { + if (*durationVal > 0.0) { + duration = Some(*durationVal); + } + LOG("- MOZ_PROFILER_STARTUP_DURATION = %f", *durationVal); + } else { + PrintToConsole("- MOZ_PROFILER_STARTUP_DURATION not a valid float: %s", + startupDuration); + PrintUsage(); + exit(1); + } + } + + const char* startupInterval = getenv("MOZ_PROFILER_STARTUP_INTERVAL"); + if (startupInterval && startupInterval[0] != '\0') { + // The interval is a floating point number. Use StringToDouble rather than + // strtod, so that "." is used as the decimal separator regardless of OS + // locale. + auto intervalValue = StringToDouble(MakeStringSpan(startupInterval)); + if (intervalValue && *intervalValue > 0.0 && *intervalValue <= 1000.0) { + interval = *intervalValue; + LOG("- MOZ_PROFILER_STARTUP_INTERVAL = %f", interval); + } else { + PrintToConsole("- MOZ_PROFILER_STARTUP_INTERVAL not a valid float: %s", + startupInterval); + PrintUsage(); + exit(1); + } + } + + features |= StartupExtraDefaultFeatures() & AvailableFeatures(); + + const char* startupFeaturesBitfield = + getenv("MOZ_PROFILER_STARTUP_FEATURES_BITFIELD"); + if (startupFeaturesBitfield && startupFeaturesBitfield[0] != '\0') { + errno = 0; + features = strtol(startupFeaturesBitfield, nullptr, 10); + if (errno == 0) { + LOG("- MOZ_PROFILER_STARTUP_FEATURES_BITFIELD = %d", features); + } else { + PrintToConsole( + "- MOZ_PROFILER_STARTUP_FEATURES_BITFIELD not a valid integer: %s", + startupFeaturesBitfield); + PrintUsage(); + exit(1); + } + } else { + const char* startupFeatures = getenv("MOZ_PROFILER_STARTUP_FEATURES"); + if (startupFeatures) { + // Interpret startupFeatures as a list of feature strings, separated by + // commas. + UniquePtr<char[]> featureStringStorage; + Vector<const char*> featureStringArray = + SplitAtCommas(startupFeatures, featureStringStorage); + features = ParseFeaturesFromStringArray(featureStringArray.begin(), + featureStringArray.length(), + /* aIsStartup */ true); + LOG("- MOZ_PROFILER_STARTUP_FEATURES = %d", features); + } + } + + const char* startupFilters = getenv("MOZ_PROFILER_STARTUP_FILTERS"); + if (startupFilters && startupFilters[0] != '\0') { + filters = SplitAtCommas(startupFilters, filterStorage); + LOG("- MOZ_PROFILER_STARTUP_FILTERS = %s", startupFilters); + + if (mozilla::profiler::detail::FiltersExcludePid(filters)) { + LOG(" -> This process is excluded and won't be profiled"); + return; + } + } + + locked_profiler_start(lock, capacity, interval, features, filters.begin(), + filters.length(), duration); + } + + // TODO: Install memory counter if it is possible from mozglue. + // #if defined(MOZ_REPLACE_MALLOC) && defined(MOZ_PROFILER_MEMORY) + // // start counting memory allocations (outside of lock because this may + // call + // // profiler_add_sampled_counter which would attempt to take the lock.) + // mozilla::profiler::install_memory_counter(true); + // #endif +} + +static void locked_profiler_save_profile_to_file(PSLockRef aLock, + const char* aFilename, + bool aIsShuttingDown); + +static SamplerThread* locked_profiler_stop(PSLockRef aLock); + +void profiler_shutdown() { + LOG("profiler_shutdown"); + + VTUNE_SHUTDOWN(); + + MOZ_RELEASE_ASSERT(profiler_is_main_thread()); + MOZ_RELEASE_ASSERT(CorePS::Exists()); + + // If the profiler is active we must get a handle to the SamplerThread before + // ActivePS is destroyed, in order to delete it. + SamplerThread* samplerThread = nullptr; + { + PSAutoLock lock; + + // Save the profile on shutdown if requested. + if (ActivePS::Exists(lock)) { + const char* filename = getenv("MOZ_PROFILER_SHUTDOWN"); + if (filename && filename[0] != '\0') { + locked_profiler_save_profile_to_file(lock, filename, + /* aIsShuttingDown */ true); + } + + samplerThread = locked_profiler_stop(lock); + } + + CorePS::Destroy(lock); + + // We just destroyed CorePS and the ThreadInfos it contains, so we can + // clear this thread's TLSRegisteredThread. + TLSRegisteredThread::SetRegisteredThread(lock, nullptr); + } + + // We do these operations with gPSMutex unlocked. The comments in + // profiler_stop() explain why. + if (samplerThread) { + delete samplerThread; + } +} + +static bool WriteProfileToJSONWriter(SpliceableChunkedJSONWriter& aWriter, + double aSinceTime, bool aIsShuttingDown, + bool aOnlyThreads = false) { + LOG("WriteProfileToJSONWriter"); + + MOZ_RELEASE_ASSERT(CorePS::Exists()); + + if (!aOnlyThreads) { + aWriter.Start(); + { + if (!profiler_stream_json_for_this_process( + aWriter, aSinceTime, aIsShuttingDown, aOnlyThreads)) { + return false; + } + + // Don't include profiles from other processes because this is a + // synchronous function. + aWriter.StartArrayProperty("processes"); + aWriter.EndArray(); + } + aWriter.End(); + } else { + aWriter.StartBareList(); + if (!profiler_stream_json_for_this_process(aWriter, aSinceTime, + aIsShuttingDown, aOnlyThreads)) { + return false; + } + aWriter.EndBareList(); + } + return true; +} + +void profiler_set_process_name(const std::string& aProcessName, + const std::string* aETLDplus1) { + LOG("profiler_set_process_name(\"%s\", \"%s\")", aProcessName.c_str(), + aETLDplus1 ? aETLDplus1->c_str() : "<none>"); + PSAutoLock lock; + CorePS::SetProcessName(lock, aProcessName); + if (aETLDplus1) { + CorePS::SetETLDplus1(lock, *aETLDplus1); + } +} + +UniquePtr<char[]> profiler_get_profile(double aSinceTime, bool aIsShuttingDown, + bool aOnlyThreads) { + LOG("profiler_get_profile"); + + SpliceableChunkedJSONWriter b{FailureLatchInfallibleSource::Singleton()}; + if (!WriteProfileToJSONWriter(b, aSinceTime, aIsShuttingDown, aOnlyThreads)) { + return nullptr; + } + return b.ChunkedWriteFunc().CopyData(); +} + +void profiler_get_start_params(int* aCapacity, Maybe<double>* aDuration, + double* aInterval, uint32_t* aFeatures, + Vector<const char*>* aFilters) { + MOZ_RELEASE_ASSERT(CorePS::Exists()); + + if (!aCapacity || !aDuration || !aInterval || !aFeatures || !aFilters) { + return; + } + + PSAutoLock lock; + + if (!ActivePS::Exists(lock)) { + *aCapacity = 0; + *aDuration = Nothing(); + *aInterval = 0; + *aFeatures = 0; + aFilters->clear(); + return; + } + + *aCapacity = ActivePS::Capacity(lock).Value(); + *aDuration = ActivePS::Duration(lock); + *aInterval = ActivePS::Interval(lock); + *aFeatures = ActivePS::Features(lock); + + const Vector<std::string>& filters = ActivePS::Filters(lock); + MOZ_ALWAYS_TRUE(aFilters->resize(filters.length())); + for (uint32_t i = 0; i < filters.length(); ++i) { + (*aFilters)[i] = filters[i].c_str(); + } +} + +void GetProfilerEnvVarsForChildProcess( + std::function<void(const char* key, const char* value)>&& aSetEnv) { + MOZ_RELEASE_ASSERT(CorePS::Exists()); + + PSAutoLock lock; + + if (!ActivePS::Exists(lock)) { + aSetEnv("MOZ_PROFILER_STARTUP", ""); + return; + } + + aSetEnv("MOZ_PROFILER_STARTUP", "1"); + auto capacityString = + Smprintf("%u", unsigned(ActivePS::Capacity(lock).Value())); + aSetEnv("MOZ_PROFILER_STARTUP_ENTRIES", capacityString.get()); + + // Use AppendFloat instead of Smprintf with %f because the decimal + // separator used by %f is locale-dependent. But the string we produce needs + // to be parseable by strtod, which only accepts the period character as a + // decimal separator. AppendFloat always uses the period character. + std::string intervalString = std::to_string(ActivePS::Interval(lock)); + aSetEnv("MOZ_PROFILER_STARTUP_INTERVAL", intervalString.c_str()); + + auto featuresString = Smprintf("%d", ActivePS::Features(lock)); + aSetEnv("MOZ_PROFILER_STARTUP_FEATURES_BITFIELD", featuresString.get()); + + std::string filtersString; + const Vector<std::string>& filters = ActivePS::Filters(lock); + for (uint32_t i = 0; i < filters.length(); ++i) { + filtersString += filters[i]; + if (i != filters.length() - 1) { + filtersString += ","; + } + } + aSetEnv("MOZ_PROFILER_STARTUP_FILTERS", filtersString.c_str()); +} + +void profiler_received_exit_profile(const std::string& aExitProfile) { + MOZ_RELEASE_ASSERT(CorePS::Exists()); + PSAutoLock lock; + if (!ActivePS::Exists(lock)) { + return; + } + ActivePS::AddExitProfile(lock, aExitProfile); +} + +Vector<std::string> profiler_move_exit_profiles() { + MOZ_RELEASE_ASSERT(CorePS::Exists()); + PSAutoLock lock; + Vector<std::string> profiles; + if (ActivePS::Exists(lock)) { + profiles = ActivePS::MoveExitProfiles(lock); + } + return profiles; +} + +static void locked_profiler_save_profile_to_file(PSLockRef aLock, + const char* aFilename, + bool aIsShuttingDown = false) { + LOG("locked_profiler_save_profile_to_file(%s)", aFilename); + + MOZ_RELEASE_ASSERT(CorePS::Exists() && ActivePS::Exists(aLock)); + + std::ofstream stream; + stream.open(aFilename); + if (stream.is_open()) { + OStreamJSONWriteFunc jw(stream); + SpliceableJSONWriter w(jw, FailureLatchInfallibleSource::Singleton()); + w.Start(); + { + locked_profiler_stream_json_for_this_process(aLock, w, /* sinceTime */ 0, + aIsShuttingDown); + + w.StartArrayProperty("processes"); + Vector<std::string> exitProfiles = ActivePS::MoveExitProfiles(aLock); + for (auto& exitProfile : exitProfiles) { + if (!exitProfile.empty()) { + w.Splice(exitProfile); + } + } + w.EndArray(); + } + w.End(); + + stream.close(); + } +} + +void baseprofiler_save_profile_to_file(const char* aFilename) { + LOG("baseprofiler_save_profile_to_file(%s)", aFilename); + + MOZ_RELEASE_ASSERT(CorePS::Exists()); + + PSAutoLock lock; + + if (!ActivePS::Exists(lock)) { + return; + } + + locked_profiler_save_profile_to_file(lock, aFilename); +} + +uint32_t profiler_get_available_features() { + MOZ_RELEASE_ASSERT(CorePS::Exists()); + return AvailableFeatures(); +} + +Maybe<ProfilerBufferInfo> profiler_get_buffer_info() { + MOZ_RELEASE_ASSERT(CorePS::Exists()); + + PSAutoLock lock; + + if (!ActivePS::Exists(lock)) { + return Nothing(); + } + + return Some(ActivePS::Buffer(lock).GetProfilerBufferInfo()); +} + +// This basically duplicates AutoProfilerLabel's constructor. +static void* MozGlueBaseLabelEnter(const char* aLabel, + const char* aDynamicString, void* aSp) { + ProfilingStack* profilingStack = AutoProfilerLabel::sProfilingStack.get(); + if (profilingStack) { + profilingStack->pushLabelFrame(aLabel, aDynamicString, aSp, + ProfilingCategoryPair::OTHER); + } + return profilingStack; +} + +// This basically duplicates AutoProfilerLabel's destructor. +static void MozGlueBaseLabelExit(void* sProfilingStack) { + if (sProfilingStack) { + reinterpret_cast<ProfilingStack*>(sProfilingStack)->pop(); + } +} + +static void locked_profiler_start(PSLockRef aLock, PowerOfTwo32 aCapacity, + double aInterval, uint32_t aFeatures, + const char** aFilters, uint32_t aFilterCount, + const Maybe<double>& aDuration) { + const TimeStamp profilingStartTime = TimeStamp::Now(); + + if (LOG_TEST) { + LOG("locked_profiler_start"); + LOG("- capacity = %d", int(aCapacity.Value())); + LOG("- duration = %.2f", aDuration ? *aDuration : -1); + LOG("- interval = %.2f", aInterval); + +#define LOG_FEATURE(n_, str_, Name_, desc_) \ + if (ProfilerFeature::Has##Name_(aFeatures)) { \ + LOG("- feature = %s", str_); \ + } + + BASE_PROFILER_FOR_EACH_FEATURE(LOG_FEATURE) + +#undef LOG_FEATURE + + for (uint32_t i = 0; i < aFilterCount; i++) { + LOG("- threads = %s", aFilters[i]); + } + } + + MOZ_RELEASE_ASSERT(CorePS::Exists() && !ActivePS::Exists(aLock)); + + mozilla::base_profiler_markers_detail::EnsureBufferForMainThreadAddMarker(); + +#if defined(GP_PLAT_amd64_windows) + InitializeWin64ProfilerHooks(); +#endif + + // Fall back to the default values if the passed-in values are unreasonable. + // We want to be able to store at least one full stack. + // TODO: Review magic numbers. + PowerOfTwo32 capacity = + (aCapacity.Value() >= + ProfileBufferChunkManager::scExpectedMaximumStackSize / scBytesPerEntry) + ? aCapacity + : BASE_PROFILER_DEFAULT_ENTRIES; + Maybe<double> duration = aDuration; + + if (aDuration && *aDuration <= 0) { + duration = Nothing(); + } + double interval = aInterval > 0 ? aInterval : BASE_PROFILER_DEFAULT_INTERVAL; + + ActivePS::Create(aLock, profilingStartTime, capacity, interval, aFeatures, + aFilters, aFilterCount, duration); + + // Set up profiling for each registered thread, if appropriate. + const Vector<UniquePtr<RegisteredThread>>& registeredThreads = + CorePS::RegisteredThreads(aLock); + for (auto& registeredThread : registeredThreads) { + RefPtr<ThreadInfo> info = registeredThread->Info(); + + if (ActivePS::ShouldProfileThread(aLock, info)) { + registeredThread->RacyRegisteredThread().SetIsBeingProfiled(true); + ActivePS::AddLiveProfiledThread(aLock, registeredThread.get(), + MakeUnique<ProfiledThreadData>(info)); + registeredThread->RacyRegisteredThread().ReinitializeOnResume(); + } + } + + // Setup support for pushing/popping labels in mozglue. + RegisterProfilerLabelEnterExit(MozGlueBaseLabelEnter, MozGlueBaseLabelExit); + + // At the very end, set up RacyFeatures. + RacyFeatures::SetActive(ActivePS::Features(aLock)); +} + +void profiler_start(PowerOfTwo32 aCapacity, double aInterval, + uint32_t aFeatures, const char** aFilters, + uint32_t aFilterCount, const Maybe<double>& aDuration) { + LOG("profiler_start"); + + SamplerThread* samplerThread = nullptr; + { + PSAutoLock lock; + + // Initialize if necessary. + if (!CorePS::Exists()) { + profiler_init(nullptr); + } + + // Reset the current state if the profiler is running. + if (ActivePS::Exists(lock)) { + samplerThread = locked_profiler_stop(lock); + } + + locked_profiler_start(lock, aCapacity, aInterval, aFeatures, aFilters, + aFilterCount, aDuration); + } + + // TODO: Install memory counter if it is possible from mozglue. + // #if defined(MOZ_REPLACE_MALLOC) && defined(MOZ_PROFILER_MEMORY) + // // start counting memory allocations (outside of lock because this may + // call + // // profiler_add_sampled_counter which would attempt to take the lock.) + // mozilla::profiler::install_memory_counter(true); + // #endif + + // We do these operations with gPSMutex unlocked. The comments in + // profiler_stop() explain why. + if (samplerThread) { + delete samplerThread; + } +} + +void profiler_ensure_started(PowerOfTwo32 aCapacity, double aInterval, + uint32_t aFeatures, const char** aFilters, + uint32_t aFilterCount, + const Maybe<double>& aDuration) { + LOG("profiler_ensure_started"); + + // bool startedProfiler = false; (See TODO below) + SamplerThread* samplerThread = nullptr; + { + PSAutoLock lock; + + // Initialize if necessary. + if (!CorePS::Exists()) { + profiler_init(nullptr); + } + + if (ActivePS::Exists(lock)) { + // The profiler is active. + if (!ActivePS::Equals(lock, aCapacity, aDuration, aInterval, aFeatures, + aFilters, aFilterCount)) { + // Stop and restart with different settings. + samplerThread = locked_profiler_stop(lock); + locked_profiler_start(lock, aCapacity, aInterval, aFeatures, aFilters, + aFilterCount, aDuration); + // startedProfiler = true; (See TODO below) + } + } else { + // The profiler is stopped. + locked_profiler_start(lock, aCapacity, aInterval, aFeatures, aFilters, + aFilterCount, aDuration); + // startedProfiler = true; (See TODO below) + } + } + + // TODO: Install memory counter if it is possible from mozglue. + // #if defined(MOZ_REPLACE_MALLOC) && defined(MOZ_PROFILER_MEMORY) + // // start counting memory allocations (outside of lock because this may + // // call profiler_add_sampled_counter which would attempt to take the + // // lock.) + // mozilla::profiler::install_memory_counter(true); + // #endif + + // We do these operations with gPSMutex unlocked. The comments in + // profiler_stop() explain why. + if (samplerThread) { + delete samplerThread; + } +} + +[[nodiscard]] static SamplerThread* locked_profiler_stop(PSLockRef aLock) { + LOG("locked_profiler_stop"); + + MOZ_RELEASE_ASSERT(CorePS::Exists() && ActivePS::Exists(aLock)); + + // At the very start, clear RacyFeatures. + RacyFeatures::SetInactive(); + + // TODO: Uninstall memory counter if it is possible from mozglue. + // #if defined(MOZ_REPLACE_MALLOC) && defined(MOZ_PROFILER_MEMORY) + // mozilla::profiler::install_memory_counter(false); + // #endif + + // Remove support for pushing/popping labels in mozglue. + RegisterProfilerLabelEnterExit(nullptr, nullptr); + + // Stop sampling live threads. + const Vector<LiveProfiledThreadData>& liveProfiledThreads = + ActivePS::LiveProfiledThreads(aLock); + for (auto& thread : liveProfiledThreads) { + RegisteredThread* registeredThread = thread.mRegisteredThread; + registeredThread->RacyRegisteredThread().SetIsBeingProfiled(false); + } + + // The Stop() call doesn't actually stop Run(); that happens in this + // function's caller when the sampler thread is destroyed. Stop() just gives + // the SamplerThread a chance to do some cleanup with gPSMutex locked. + SamplerThread* samplerThread = ActivePS::Destroy(aLock); + samplerThread->Stop(aLock); + + mozilla::base_profiler_markers_detail::ReleaseBufferForMainThreadAddMarker(); + + return samplerThread; +} + +void profiler_stop() { + LOG("profiler_stop"); + + MOZ_RELEASE_ASSERT(CorePS::Exists()); + + SamplerThread* samplerThread; + { + PSAutoLock lock; + + if (!ActivePS::Exists(lock)) { + return; + } + + samplerThread = locked_profiler_stop(lock); + } + + // We delete with gPSMutex unlocked. Otherwise we would get a deadlock: we + // would be waiting here with gPSMutex locked for SamplerThread::Run() to + // return so the join operation within the destructor can complete, but Run() + // needs to lock gPSMutex to return. + // + // Because this call occurs with gPSMutex unlocked, it -- including the final + // iteration of Run()'s loop -- must be able detect deactivation and return + // in a way that's safe with respect to other gPSMutex-locking operations + // that may have occurred in the meantime. + delete samplerThread; +} + +bool profiler_is_paused() { + MOZ_RELEASE_ASSERT(CorePS::Exists()); + + PSAutoLock lock; + + if (!ActivePS::Exists(lock)) { + return false; + } + + return ActivePS::IsPaused(lock); +} + +void profiler_pause() { + LOG("profiler_pause"); + + MOZ_RELEASE_ASSERT(CorePS::Exists()); + + { + PSAutoLock lock; + + if (!ActivePS::Exists(lock)) { + return; + } + + RacyFeatures::SetPaused(); + ActivePS::SetIsPaused(lock, true); + ActivePS::Buffer(lock).AddEntry(ProfileBufferEntry::Pause(profiler_time())); + } +} + +void profiler_resume() { + LOG("profiler_resume"); + + MOZ_RELEASE_ASSERT(CorePS::Exists()); + + { + PSAutoLock lock; + + if (!ActivePS::Exists(lock)) { + return; + } + + ActivePS::Buffer(lock).AddEntry( + ProfileBufferEntry::Resume(profiler_time())); + ActivePS::SetIsPaused(lock, false); + RacyFeatures::SetUnpaused(); + } +} + +bool profiler_is_sampling_paused() { + MOZ_RELEASE_ASSERT(CorePS::Exists()); + + PSAutoLock lock; + + if (!ActivePS::Exists(lock)) { + return false; + } + + return ActivePS::IsSamplingPaused(lock); +} + +void profiler_pause_sampling() { + LOG("profiler_pause_sampling"); + + MOZ_RELEASE_ASSERT(CorePS::Exists()); + + { + PSAutoLock lock; + + if (!ActivePS::Exists(lock)) { + return; + } + + RacyFeatures::SetSamplingPaused(); + ActivePS::SetIsSamplingPaused(lock, true); + ActivePS::Buffer(lock).AddEntry( + ProfileBufferEntry::PauseSampling(profiler_time())); + } +} + +void profiler_resume_sampling() { + LOG("profiler_resume_sampling"); + + MOZ_RELEASE_ASSERT(CorePS::Exists()); + + { + PSAutoLock lock; + + if (!ActivePS::Exists(lock)) { + return; + } + + ActivePS::Buffer(lock).AddEntry( + ProfileBufferEntry::ResumeSampling(profiler_time())); + ActivePS::SetIsSamplingPaused(lock, false); + RacyFeatures::SetSamplingUnpaused(); + } +} + +bool profiler_feature_active(uint32_t aFeature) { + // This function runs both on and off the main thread. + + MOZ_RELEASE_ASSERT(CorePS::Exists()); + + // This function is hot enough that we use RacyFeatures, not ActivePS. + return RacyFeatures::IsActiveWithFeature(aFeature); +} + +bool profiler_active_without_feature(uint32_t aFeature) { + // This function runs both on and off the main thread. + + // This function is hot enough that we use RacyFeatures, not ActivePS. + return RacyFeatures::IsActiveWithoutFeature(aFeature); +} + +void profiler_add_sampled_counter(BaseProfilerCount* aCounter) { + DEBUG_LOG("profiler_add_sampled_counter(%s)", aCounter->mLabel); + PSAutoLock lock; + CorePS::AppendCounter(lock, aCounter); +} + +void profiler_remove_sampled_counter(BaseProfilerCount* aCounter) { + DEBUG_LOG("profiler_remove_sampled_counter(%s)", aCounter->mLabel); + PSAutoLock lock; + // Note: we don't enforce a final sample, though we could do so if the + // profiler was active + CorePS::RemoveCounter(lock, aCounter); +} + +ProfilingStack* profiler_register_thread(const char* aName, + void* aGuessStackTop) { + DEBUG_LOG("profiler_register_thread(%s)", aName); + + MOZ_RELEASE_ASSERT(CorePS::Exists()); + + PSAutoLock lock; + + if (RegisteredThread* thread = FindCurrentThreadRegisteredThread(lock); + thread) { + LOG("profiler_register_thread(%s) - thread %" PRIu64 + " already registered as %s", + aName, uint64_t(profiler_current_thread_id().ToNumber()), + thread->Info()->Name()); + // TODO: Use new name. This is currently not possible because the + // RegisteredThread's ThreadInfo cannot be changed. + // In the meantime, we record a marker that could be used in the frontend. + std::string text("Thread "); + text += std::to_string(profiler_current_thread_id().ToNumber()); + text += " \""; + text += thread->Info()->Name(); + text += "\" attempted to re-register as \""; + text += aName; + text += "\""; + BASE_PROFILER_MARKER_TEXT("profiler_register_thread again", OTHER_Profiling, + MarkerThreadId::MainThread(), text); + + return &thread->RacyRegisteredThread().ProfilingStack(); + } + + void* stackTop = GetStackTop(aGuessStackTop); + return locked_register_thread(lock, aName, stackTop); +} + +void profiler_unregister_thread() { + if (!CorePS::Exists()) { + // This function can be called after the main thread has already shut down. + return; + } + + PSAutoLock lock; + + RegisteredThread* registeredThread = FindCurrentThreadRegisteredThread(lock); + MOZ_RELEASE_ASSERT(registeredThread == + TLSRegisteredThread::RegisteredThread(lock)); + if (registeredThread) { + RefPtr<ThreadInfo> info = registeredThread->Info(); + + DEBUG_LOG("profiler_unregister_thread: %s", info->Name()); + + if (ActivePS::Exists(lock)) { + ActivePS::UnregisterThread(lock, registeredThread); + } + + // Clear the pointer to the RegisteredThread object that we're about to + // destroy. + TLSRegisteredThread::SetRegisteredThread(lock, nullptr); + + // Remove the thread from the list of registered threads. This deletes the + // registeredThread object. + CorePS::RemoveRegisteredThread(lock, registeredThread); + } else { + LOG("profiler_unregister_thread() - thread %" PRIu64 + " already unregistered", + uint64_t(profiler_current_thread_id().ToNumber())); + // We cannot record a marker on this thread because it was already + // unregistered. Send it to the main thread (unless this *is* already the + // main thread, which has been unregistered); this may be useful to catch + // mismatched register/unregister pairs in Firefox. + if (BaseProfilerThreadId tid = profiler_current_thread_id(); + tid != profiler_main_thread_id()) { + BASE_PROFILER_MARKER_TEXT( + "profiler_unregister_thread again", OTHER_Profiling, + MarkerThreadId::MainThread(), + std::to_string(profiler_current_thread_id().ToNumber())); + } + // There are two ways FindCurrentThreadRegisteredThread() might have failed. + // + // - TLSRegisteredThread::Init() failed in locked_register_thread(). + // + // - We've already called profiler_unregister_thread() for this thread. + // (Whether or not it should, this does happen in practice.) + // + // Either way, TLSRegisteredThread should be empty. + MOZ_RELEASE_ASSERT(!TLSRegisteredThread::RegisteredThread(lock)); + } +} + +void profiler_register_page(uint64_t aTabID, uint64_t aInnerWindowID, + const std::string& aUrl, + uint64_t aEmbedderInnerWindowID) { + DEBUG_LOG("profiler_register_page(%" PRIu64 ", %" PRIu64 ", %s, %" PRIu64 ")", + aTabID, aInnerWindowID, aUrl.c_str(), aEmbedderInnerWindowID); + + MOZ_RELEASE_ASSERT(CorePS::Exists()); + + PSAutoLock lock; + + // When a Browsing context is first loaded, the first url loaded in it will be + // about:blank. Because of that, this call keeps the first non-about:blank + // registration of window and discards the previous one. + RefPtr<PageInformation> pageInfo = + new PageInformation(aTabID, aInnerWindowID, aUrl, aEmbedderInnerWindowID); + CorePS::AppendRegisteredPage(lock, std::move(pageInfo)); + + // After appending the given page to CorePS, look for the expired + // pages and remove them if there are any. + if (ActivePS::Exists(lock)) { + ActivePS::DiscardExpiredPages(lock); + } +} + +void profiler_unregister_page(uint64_t aRegisteredInnerWindowID) { + if (!CorePS::Exists()) { + // This function can be called after the main thread has already shut down. + return; + } + + PSAutoLock lock; + + // During unregistration, if the profiler is active, we have to keep the + // page information since there may be some markers associated with the given + // page. But if profiler is not active. we have no reason to keep the + // page information here because there can't be any marker associated with it. + if (ActivePS::Exists(lock)) { + ActivePS::UnregisterPage(lock, aRegisteredInnerWindowID); + } else { + CorePS::RemoveRegisteredPage(lock, aRegisteredInnerWindowID); + } +} + +void profiler_clear_all_pages() { + if (!CorePS::Exists()) { + // This function can be called after the main thread has already shut down. + return; + } + + { + PSAutoLock lock; + CorePS::ClearRegisteredPages(lock); + if (ActivePS::Exists(lock)) { + ActivePS::ClearUnregisteredPages(lock); + } + } +} + +void profiler_thread_sleep() { + // This function runs both on and off the main thread. + + MOZ_RELEASE_ASSERT(CorePS::Exists()); + + RacyRegisteredThread* racyRegisteredThread = + TLSRegisteredThread::RacyRegisteredThread(); + if (!racyRegisteredThread) { + return; + } + + racyRegisteredThread->SetSleeping(); +} + +void profiler_thread_wake() { + // This function runs both on and off the main thread. + + MOZ_RELEASE_ASSERT(CorePS::Exists()); + + RacyRegisteredThread* racyRegisteredThread = + TLSRegisteredThread::RacyRegisteredThread(); + if (!racyRegisteredThread) { + return; + } + + racyRegisteredThread->SetAwake(); +} + +bool detail::IsThreadBeingProfiled() { + MOZ_RELEASE_ASSERT(CorePS::Exists()); + + const RacyRegisteredThread* racyRegisteredThread = + TLSRegisteredThread::RacyRegisteredThread(); + return racyRegisteredThread && racyRegisteredThread->IsBeingProfiled(); +} + +bool profiler_thread_is_sleeping() { + MOZ_RELEASE_ASSERT(profiler_is_main_thread()); + MOZ_RELEASE_ASSERT(CorePS::Exists()); + + RacyRegisteredThread* racyRegisteredThread = + TLSRegisteredThread::RacyRegisteredThread(); + if (!racyRegisteredThread) { + return false; + } + return racyRegisteredThread->IsSleeping(); +} + +double profiler_time() { + MOZ_RELEASE_ASSERT(CorePS::Exists()); + + TimeDuration delta = TimeStamp::Now() - CorePS::ProcessStartTime(); + return delta.ToMilliseconds(); +} + +bool profiler_capture_backtrace_into(ProfileChunkedBuffer& aChunkedBuffer, + StackCaptureOptions aCaptureOptions) { + MOZ_RELEASE_ASSERT(CorePS::Exists()); + + PSAutoLock lock; + + if (!ActivePS::Exists(lock) || + aCaptureOptions == StackCaptureOptions::NoStack) { + return false; + } + + RegisteredThread* registeredThread = + TLSRegisteredThread::RegisteredThread(lock); + if (!registeredThread) { + MOZ_ASSERT(registeredThread); + return false; + } + + ProfileBuffer profileBuffer(aChunkedBuffer); + + Registers regs; +#if defined(HAVE_NATIVE_UNWIND) + REGISTERS_SYNC_POPULATE(regs); +#else + regs.Clear(); +#endif + + DoSyncSample(lock, *registeredThread, TimeStamp::Now(), regs, profileBuffer, + aCaptureOptions); + + return true; +} + +UniquePtr<ProfileChunkedBuffer> profiler_capture_backtrace() { + MOZ_RELEASE_ASSERT(CorePS::Exists()); + AUTO_BASE_PROFILER_LABEL("baseprofiler::profiler_capture_backtrace", + PROFILER); + + // Quick is-active check before allocating a buffer. + // If NoMarkerStacks is set, we don't want to capture a backtrace. + if (!profiler_active_without_feature(ProfilerFeature::NoMarkerStacks)) { + return nullptr; + } + + auto buffer = MakeUnique<ProfileChunkedBuffer>( + ProfileChunkedBuffer::ThreadSafety::WithoutMutex, + MakeUnique<ProfileBufferChunkManagerSingle>( + ProfileBufferChunkManager::scExpectedMaximumStackSize)); + + if (!profiler_capture_backtrace_into(*buffer, StackCaptureOptions::Full)) { + return nullptr; + } + + return buffer; +} + +UniqueProfilerBacktrace profiler_get_backtrace() { + UniquePtr<ProfileChunkedBuffer> buffer = profiler_capture_backtrace(); + + if (!buffer) { + return nullptr; + } + + return UniqueProfilerBacktrace( + new ProfilerBacktrace("SyncProfile", std::move(buffer))); +} + +void ProfilerBacktraceDestructor::operator()(ProfilerBacktrace* aBacktrace) { + delete aBacktrace; +} + +bool profiler_is_locked_on_current_thread() { + // This function is used to help users avoid calling `profiler_...` functions + // when the profiler may already have a lock in place, which would prevent a + // 2nd recursive lock (resulting in a crash or a never-ending wait). + // So we must return `true` for any of: + // - The main profiler mutex, used by most functions, and/or + // - The buffer mutex, used directly in some functions without locking the + // main mutex, e.g., marker-related functions. + return PSAutoLock::IsLockedOnCurrentThread() || + profiler_get_core_buffer().IsThreadSafeAndLockedOnCurrentThread(); +} + +// This is a simplified version of profiler_add_marker that can be easily passed +// into the JS engine. +void profiler_add_js_marker(const char* aMarkerName, const char* aMarkerText) { + BASE_PROFILER_MARKER_TEXT( + ProfilerString8View::WrapNullTerminatedString(aMarkerName), JS, {}, + ProfilerString8View::WrapNullTerminatedString(aMarkerText)); +} + +// NOTE: aCollector's methods will be called while the target thread is paused. +// Doing things in those methods like allocating -- which may try to claim +// locks -- is a surefire way to deadlock. +void profiler_suspend_and_sample_thread(BaseProfilerThreadId aThreadId, + uint32_t aFeatures, + ProfilerStackCollector& aCollector, + bool aSampleNative /* = true */) { + const bool isSynchronous = [&aThreadId]() { + const BaseProfilerThreadId currentThreadId = profiler_current_thread_id(); + if (!aThreadId.IsSpecified()) { + aThreadId = currentThreadId; + return true; + } + return aThreadId == currentThreadId; + }(); + + // Lock the profiler mutex + PSAutoLock lock; + + const Vector<UniquePtr<RegisteredThread>>& registeredThreads = + CorePS::RegisteredThreads(lock); + for (auto& thread : registeredThreads) { + RefPtr<ThreadInfo> info = thread->Info(); + RegisteredThread& registeredThread = *thread.get(); + + if (info->ThreadId() == aThreadId) { + if (info->IsMainThread()) { + aCollector.SetIsMainThread(); + } + + // Allocate the space for the native stack + NativeStack nativeStack; + + auto collectStack = [&](const Registers& aRegs, const TimeStamp& aNow) { + // The target thread is now suspended. Collect a native + // backtrace, and call the callback. +#if defined(HAVE_FASTINIT_NATIVE_UNWIND) + if (aSampleNative) { + // We can only use FramePointerStackWalk or MozStackWalk from + // suspend_and_sample_thread as other stackwalking methods may not be + // initialized. +# if defined(USE_FRAME_POINTER_STACK_WALK) + DoFramePointerBacktrace(lock, registeredThread, aRegs, nativeStack); +# elif defined(USE_MOZ_STACK_WALK) + DoMozStackWalkBacktrace(lock, registeredThread, aRegs, nativeStack); +# else +# error "Invalid configuration" +# endif + + MergeStacks(aFeatures, isSynchronous, registeredThread, aRegs, + nativeStack, aCollector); + } else +#endif + { + MergeStacks(aFeatures, isSynchronous, registeredThread, aRegs, + nativeStack, aCollector); + + aCollector.CollectNativeLeafAddr((void*)aRegs.mPC); + } + }; + + if (isSynchronous) { + // Sampling the current thread, do NOT suspend it! + Registers regs; +#if defined(HAVE_NATIVE_UNWIND) + REGISTERS_SYNC_POPULATE(regs); +#else + regs.Clear(); +#endif + collectStack(regs, TimeStamp::Now()); + } else { + // Suspend, sample, and then resume the target thread. + Sampler sampler(lock); + TimeStamp now = TimeStamp::Now(); + sampler.SuspendAndSampleAndResumeThread(lock, registeredThread, now, + collectStack); + + // NOTE: Make sure to disable the sampler before it is destroyed, in + // case the profiler is running at the same time. + sampler.Disable(lock); + } + break; + } + } +} + +// END externally visible functions +//////////////////////////////////////////////////////////////////////// + +} // namespace baseprofiler +} // namespace mozilla diff --git a/mozglue/baseprofiler/core/platform.h b/mozglue/baseprofiler/core/platform.h new file mode 100644 index 0000000000..f77fc12726 --- /dev/null +++ b/mozglue/baseprofiler/core/platform.h @@ -0,0 +1,149 @@ +// Copyright (c) 2006-2011 The Chromium Authors. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions +// are met: +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in +// the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google, Inc. nor the names of its contributors +// may be used to endorse or promote products derived from this +// software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +// OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +// AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT +// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +// SUCH DAMAGE. + +#ifndef TOOLS_PLATFORM_H_ +#define TOOLS_PLATFORM_H_ + +#include "PlatformMacros.h" + +#include "BaseProfiler.h" + +#include "mozilla/Atomics.h" +#include "mozilla/Logging.h" +#include "mozilla/UniquePtr.h" +#include "mozilla/Vector.h" + +#include <functional> +#include <stdint.h> +#include <string> + +namespace mozilla { +namespace baseprofiler { +bool LogTest(int aLevelToTest); +void PrintToConsole(const char* aFmt, ...) MOZ_FORMAT_PRINTF(1, 2); +} // namespace baseprofiler +} // namespace mozilla + +// These are for MOZ_BASE_PROFILER_LOGGING and above. It's the default logging +// level for the profiler, and should be used sparingly. +#define LOG_TEST ::mozilla::baseprofiler::LogTest(3) +#define LOG(arg, ...) \ + do { \ + if (LOG_TEST) { \ + ::mozilla::baseprofiler::PrintToConsole( \ + "[I %d/%d] " arg "\n", \ + int(::mozilla::baseprofiler::profiler_current_process_id() \ + .ToNumber()), \ + int(::mozilla::baseprofiler::profiler_current_thread_id() \ + .ToNumber()), \ + ##__VA_ARGS__); \ + } \ + } while (0) + +// These are for MOZ_BASE_PROFILER_DEBUG_LOGGING. It should be used for logging +// that is somewhat more verbose than LOG. +#define DEBUG_LOG_TEST ::mozilla::baseprofiler::LogTest(4) +#define DEBUG_LOG(arg, ...) \ + do { \ + if (DEBUG_LOG_TEST) { \ + ::mozilla::baseprofiler::PrintToConsole( \ + "[D %d/%d] " arg "\n", \ + int(::mozilla::baseprofiler::profiler_current_process_id() \ + .ToNumber()), \ + int(::mozilla::baseprofiler::profiler_current_thread_id() \ + .ToNumber()), \ + ##__VA_ARGS__); \ + } \ + } while (0) + +// These are for MOZ_BASE_PROFILER_VERBOSE_LOGGING. It should be used for +// logging that is somewhat more verbose than DEBUG_LOG. +#define VERBOSE_LOG_TEST ::mozilla::baseprofiler::LogTest(5) +#define VERBOSE_LOG(arg, ...) \ + do { \ + if (VERBOSE_LOG_TEST) { \ + ::mozilla::baseprofiler::PrintToConsole( \ + "[V %d/%d] " arg "\n", \ + int(::mozilla::baseprofiler::profiler_current_process_id() \ + .ToNumber()), \ + int(::mozilla::baseprofiler::profiler_current_thread_id() \ + .ToNumber()), \ + ##__VA_ARGS__); \ + } \ + } while (0) + +namespace mozilla { + +class JSONWriter; + +namespace baseprofiler { + +// If positive, skip stack-sampling in the sampler thread loop. +// Users should increment it atomically when samplings should be avoided, and +// later decrement it back. Multiple uses can overlap. +// There could be a sampling in progress when this is first incremented, so if +// it is critical to prevent any sampling, lock the profiler mutex instead. +// Relaxed ordering, because it's used to request that the profiler pause +// future sampling; this is not time critical, nor dependent on anything else. +extern mozilla::Atomic<int, mozilla::MemoryOrdering::Relaxed> gSkipSampling; + +typedef uint8_t* Address; + +class PlatformData; + +// We can't new/delete the type safely without defining it +// (-Wdelete-incomplete). Use these to hide the details from clients. +struct PlatformDataDestructor { + void operator()(PlatformData*); +}; + +typedef UniquePtr<PlatformData, PlatformDataDestructor> UniquePlatformData; +UniquePlatformData AllocPlatformData(BaseProfilerThreadId aThreadId); + +// Convert the array of strings to a bitfield. +uint32_t ParseFeaturesFromStringArray(const char** aFeatures, + uint32_t aFeatureCount, + bool aIsStartup = false); + +// Flags to conveniently track various JS instrumentations. +enum class JSInstrumentationFlags { + StackSampling = 0x1, + Allocations = 0x2, +}; + +// Record an exit profile from a child process. +void profiler_received_exit_profile(const std::string& aExitProfile); + +// Extract all received exit profiles that have not yet expired (i.e., they +// still intersect with this process' buffer range). +Vector<std::string> profiler_move_exit_profiles(); + +} // namespace baseprofiler +} // namespace mozilla + +#endif /* ndef TOOLS_PLATFORM_H_ */ diff --git a/mozglue/baseprofiler/core/shared-libraries-linux.cc b/mozglue/baseprofiler/core/shared-libraries-linux.cc new file mode 100644 index 0000000000..a12d60cf73 --- /dev/null +++ b/mozglue/baseprofiler/core/shared-libraries-linux.cc @@ -0,0 +1,854 @@ +/* -*- 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 "BaseProfilerSharedLibraries.h" + +#define PATH_MAX_TOSTRING(x) #x +#define PATH_MAX_STRING(x) PATH_MAX_TOSTRING(x) +#include <stdio.h> +#include <string.h> +#include <limits.h> +#include <unistd.h> +#include <fstream> +#include "platform.h" +#include "mozilla/Sprintf.h" + +#include <algorithm> +#include <arpa/inet.h> +#include <elf.h> +#include <fcntl.h> +#if defined(GP_OS_linux) || defined(GP_OS_android) +# include <features.h> +#endif +#include <sys/mman.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <vector> + +#if defined(GP_OS_linux) || defined(GP_OS_android) || defined(GP_OS_freebsd) +# include <link.h> // dl_phdr_info, ElfW() +#else +# error "Unexpected configuration" +#endif + +#if defined(GP_OS_android) +extern "C" MOZ_EXPORT __attribute__((weak)) int dl_iterate_phdr( + int (*callback)(struct dl_phdr_info* info, size_t size, void* data), + void* data); +#endif + +#if defined(GP_OS_freebsd) && !defined(ElfW) +# define ElfW(type) Elf_##type +#endif + +// ---------------------------------------------------------------------------- +// Starting imports from toolkit/crashreporter/google-breakpad/, as needed by +// this file when moved to mozglue. + +// Imported from +// toolkit/crashreporter/google-breakpad/src/common/memory_range.h. +// A lightweight wrapper with a pointer and a length to encapsulate a contiguous +// range of memory. It provides helper methods for checked access of a subrange +// of the memory. Its implemementation does not allocate memory or call into +// libc functions, and is thus safer to use in a crashed environment. +class MemoryRange { + public: + MemoryRange() : data_(NULL), length_(0) {} + + MemoryRange(const void* data, size_t length) { Set(data, length); } + + // Returns true if this memory range contains no data. + bool IsEmpty() const { + // Set() guarantees that |length_| is zero if |data_| is NULL. + return length_ == 0; + } + + // Resets to an empty range. + void Reset() { + data_ = NULL; + length_ = 0; + } + + // Sets this memory range to point to |data| and its length to |length|. + void Set(const void* data, size_t length) { + data_ = reinterpret_cast<const uint8_t*>(data); + // Always set |length_| to zero if |data_| is NULL. + length_ = data ? length : 0; + } + + // Returns true if this range covers a subrange of |sub_length| bytes + // at |sub_offset| bytes of this memory range, or false otherwise. + bool Covers(size_t sub_offset, size_t sub_length) const { + // The following checks verify that: + // 1. sub_offset is within [ 0 .. length_ - 1 ] + // 2. sub_offset + sub_length is within + // [ sub_offset .. length_ ] + return sub_offset < length_ && sub_offset + sub_length >= sub_offset && + sub_offset + sub_length <= length_; + } + + // Returns a raw data pointer to a subrange of |sub_length| bytes at + // |sub_offset| bytes of this memory range, or NULL if the subrange + // is out of bounds. + const void* GetData(size_t sub_offset, size_t sub_length) const { + return Covers(sub_offset, sub_length) ? (data_ + sub_offset) : NULL; + } + + // Same as the two-argument version of GetData() but uses sizeof(DataType) + // as the subrange length and returns an |DataType| pointer for convenience. + template <typename DataType> + const DataType* GetData(size_t sub_offset) const { + return reinterpret_cast<const DataType*>( + GetData(sub_offset, sizeof(DataType))); + } + + // Returns a raw pointer to the |element_index|-th element of an array + // of elements of length |element_size| starting at |sub_offset| bytes + // of this memory range, or NULL if the element is out of bounds. + const void* GetArrayElement(size_t element_offset, size_t element_size, + unsigned element_index) const { + size_t sub_offset = element_offset + element_index * element_size; + return GetData(sub_offset, element_size); + } + + // Same as the three-argument version of GetArrayElement() but deduces + // the element size using sizeof(ElementType) and returns an |ElementType| + // pointer for convenience. + template <typename ElementType> + const ElementType* GetArrayElement(size_t element_offset, + unsigned element_index) const { + return reinterpret_cast<const ElementType*>( + GetArrayElement(element_offset, sizeof(ElementType), element_index)); + } + + // Returns a subrange of |sub_length| bytes at |sub_offset| bytes of + // this memory range, or an empty range if the subrange is out of bounds. + MemoryRange Subrange(size_t sub_offset, size_t sub_length) const { + return Covers(sub_offset, sub_length) + ? MemoryRange(data_ + sub_offset, sub_length) + : MemoryRange(); + } + + // Returns a pointer to the beginning of this memory range. + const uint8_t* data() const { return data_; } + + // Returns the length, in bytes, of this memory range. + size_t length() const { return length_; } + + private: + // Pointer to the beginning of this memory range. + const uint8_t* data_; + + // Length, in bytes, of this memory range. + size_t length_; +}; + +// Imported from +// toolkit/crashreporter/google-breakpad/src/common/linux/memory_mapped_file.h +// and inlined .cc. +// A utility class for mapping a file into memory for read-only access of the +// file content. Its implementation avoids calling into libc functions by +// directly making system calls for open, close, mmap, and munmap. +class MemoryMappedFile { + public: + MemoryMappedFile() {} + + // Constructor that calls Map() to map a file at |path| into memory. + // If Map() fails, the object behaves as if it is default constructed. + MemoryMappedFile(const char* path, size_t offset) { Map(path, offset); } + + MemoryMappedFile(const MemoryMappedFile&) = delete; + MemoryMappedFile& operator=(const MemoryMappedFile&) = delete; + + ~MemoryMappedFile() {} + + // Maps a file at |path| into memory, which can then be accessed via + // content() as a MemoryRange object or via data(), and returns true on + // success. Mapping an empty file will succeed but with data() and size() + // returning NULL and 0, respectively. An existing mapping is unmapped + // before a new mapping is created. + bool Map(const char* path, size_t offset) { + Unmap(); + + int fd = open(path, O_RDONLY, 0); + if (fd == -1) { + return false; + } + +#if defined(__x86_64__) || defined(__aarch64__) || \ + (defined(__mips__) && _MIPS_SIM == _ABI64) || \ + !(defined(GP_OS_linux) || defined(GP_OS_android)) + + struct stat st; + if (fstat(fd, &st) == -1 || st.st_size < 0) { +#else + struct stat64 st; + if (fstat64(fd, &st) == -1 || st.st_size < 0) { +#endif + close(fd); + return false; + } + + // Strangely file size can be negative, but we check above that it is not. + size_t file_len = static_cast<size_t>(st.st_size); + // If the file does not extend beyond the offset, simply use an empty + // MemoryRange and return true. Don't bother to call mmap() + // even though mmap() can handle an empty file on some platforms. + if (offset >= file_len) { + close(fd); + return true; + } + + void* data = mmap(NULL, file_len, PROT_READ, MAP_PRIVATE, fd, offset); + close(fd); + if (data == MAP_FAILED) { + return false; + } + + content_.Set(data, file_len - offset); + return true; + } + + // Unmaps the memory for the mapped file. It's a no-op if no file is + // mapped. + void Unmap() { + if (content_.data()) { + munmap(const_cast<uint8_t*>(content_.data()), content_.length()); + content_.Set(NULL, 0); + } + } + + // Returns a MemoryRange object that covers the memory for the mapped + // file. The MemoryRange object is empty if no file is mapped. + const MemoryRange& content() const { return content_; } + + // Returns a pointer to the beginning of the memory for the mapped file. + // or NULL if no file is mapped or the mapped file is empty. + const void* data() const { return content_.data(); } + + // Returns the size in bytes of the mapped file, or zero if no file + // is mapped. + size_t size() const { return content_.length(); } + + private: + // Mapped file content as a MemoryRange object. + MemoryRange content_; +}; + +// Imported from +// toolkit/crashreporter/google-breakpad/src/common/linux/file_id.h and inlined +// .cc. +// GNU binutils' ld defaults to 'sha1', which is 160 bits == 20 bytes, +// so this is enough to fit that, which most binaries will use. +// This is just a sensible default for vectors so most callers can get away with +// stack allocation. +static const size_t kDefaultBuildIdSize = 20; + +// Used in a few places for backwards-compatibility. +typedef struct { + uint32_t data1; + uint16_t data2; + uint16_t data3; + uint8_t data4[8]; +} MDGUID; /* GUID */ + +const size_t kMDGUIDSize = sizeof(MDGUID); + +class FileID { + public: + explicit FileID(const char* path) : path_(path) {} + ~FileID() {} + + // Load the identifier for the elf file path specified in the constructor into + // |identifier|. + // + // The current implementation will look for a .note.gnu.build-id + // section and use that as the file id, otherwise it falls back to + // XORing the first 4096 bytes of the .text section to generate an identifier. + bool ElfFileIdentifier(std::vector<uint8_t>& identifier) { + MemoryMappedFile mapped_file(path_.c_str(), 0); + if (!mapped_file.data()) // Should probably check if size >= ElfW(Ehdr)? + return false; + + return ElfFileIdentifierFromMappedFile(mapped_file.data(), identifier); + } + + // Traits classes so consumers can write templatized code to deal + // with specific ELF bits. + struct ElfClass32 { + typedef Elf32_Addr Addr; + typedef Elf32_Ehdr Ehdr; + typedef Elf32_Nhdr Nhdr; + typedef Elf32_Phdr Phdr; + typedef Elf32_Shdr Shdr; + typedef Elf32_Half Half; + typedef Elf32_Off Off; + typedef Elf32_Sym Sym; + typedef Elf32_Word Word; + + static const int kClass = ELFCLASS32; + static const uint16_t kMachine = EM_386; + static const size_t kAddrSize = sizeof(Elf32_Addr); + static constexpr const char* kMachineName = "x86"; + }; + + struct ElfClass64 { + typedef Elf64_Addr Addr; + typedef Elf64_Ehdr Ehdr; + typedef Elf64_Nhdr Nhdr; + typedef Elf64_Phdr Phdr; + typedef Elf64_Shdr Shdr; + typedef Elf64_Half Half; + typedef Elf64_Off Off; + typedef Elf64_Sym Sym; + typedef Elf64_Word Word; + + static const int kClass = ELFCLASS64; + static const uint16_t kMachine = EM_X86_64; + static const size_t kAddrSize = sizeof(Elf64_Addr); + static constexpr const char* kMachineName = "x86_64"; + }; + + // Internal helper method, exposed for convenience for callers + // that already have more info. + template <typename ElfClass> + static const typename ElfClass::Shdr* FindElfSectionByName( + const char* name, typename ElfClass::Word section_type, + const typename ElfClass::Shdr* sections, const char* section_names, + const char* names_end, int nsection) { + if (!name || !sections || nsection == 0) { + return NULL; + } + + int name_len = strlen(name); + if (name_len == 0) return NULL; + + for (int i = 0; i < nsection; ++i) { + const char* section_name = section_names + sections[i].sh_name; + if (sections[i].sh_type == section_type && + names_end - section_name >= name_len + 1 && + strcmp(name, section_name) == 0) { + return sections + i; + } + } + return NULL; + } + + struct ElfSegment { + const void* start; + size_t size; + }; + + // Convert an offset from an Elf header into a pointer to the mapped + // address in the current process. Takes an extra template parameter + // to specify the return type to avoid having to dynamic_cast the + // result. + template <typename ElfClass, typename T> + static const T* GetOffset(const typename ElfClass::Ehdr* elf_header, + typename ElfClass::Off offset) { + return reinterpret_cast<const T*>(reinterpret_cast<uintptr_t>(elf_header) + + offset); + } + +// ELF note name and desc are 32-bits word padded. +#define NOTE_PADDING(a) ((a + 3) & ~3) + + static bool ElfClassBuildIDNoteIdentifier(const void* section, size_t length, + std::vector<uint8_t>& identifier) { + static_assert(sizeof(ElfClass32::Nhdr) == sizeof(ElfClass64::Nhdr), + "Elf32_Nhdr and Elf64_Nhdr should be the same"); + typedef typename ElfClass32::Nhdr Nhdr; + + const void* section_end = reinterpret_cast<const char*>(section) + length; + const Nhdr* note_header = reinterpret_cast<const Nhdr*>(section); + while (reinterpret_cast<const void*>(note_header) < section_end) { + if (note_header->n_type == NT_GNU_BUILD_ID) break; + note_header = reinterpret_cast<const Nhdr*>( + reinterpret_cast<const char*>(note_header) + sizeof(Nhdr) + + NOTE_PADDING(note_header->n_namesz) + + NOTE_PADDING(note_header->n_descsz)); + } + if (reinterpret_cast<const void*>(note_header) >= section_end || + note_header->n_descsz == 0) { + return false; + } + + const uint8_t* build_id = reinterpret_cast<const uint8_t*>(note_header) + + sizeof(Nhdr) + + NOTE_PADDING(note_header->n_namesz); + identifier.insert(identifier.end(), build_id, + build_id + note_header->n_descsz); + + return true; + } + + template <typename ElfClass> + static bool FindElfClassSection(const char* elf_base, + const char* section_name, + typename ElfClass::Word section_type, + const void** section_start, + size_t* section_size) { + typedef typename ElfClass::Ehdr Ehdr; + typedef typename ElfClass::Shdr Shdr; + + if (!elf_base || !section_start || !section_size) { + return false; + } + + if (strncmp(elf_base, ELFMAG, SELFMAG) != 0) { + return false; + } + + const Ehdr* elf_header = reinterpret_cast<const Ehdr*>(elf_base); + if (elf_header->e_ident[EI_CLASS] != ElfClass::kClass) { + return false; + } + + const Shdr* sections = + GetOffset<ElfClass, Shdr>(elf_header, elf_header->e_shoff); + const Shdr* section_names = sections + elf_header->e_shstrndx; + const char* names = + GetOffset<ElfClass, char>(elf_header, section_names->sh_offset); + const char* names_end = names + section_names->sh_size; + + const Shdr* section = + FindElfSectionByName<ElfClass>(section_name, section_type, sections, + names, names_end, elf_header->e_shnum); + + if (section != NULL && section->sh_size > 0) { + *section_start = elf_base + section->sh_offset; + *section_size = section->sh_size; + } + + return true; + } + + template <typename ElfClass> + static bool FindElfClassSegment(const char* elf_base, + typename ElfClass::Word segment_type, + std::vector<ElfSegment>* segments) { + typedef typename ElfClass::Ehdr Ehdr; + typedef typename ElfClass::Phdr Phdr; + + if (!elf_base || !segments) { + return false; + } + + if (strncmp(elf_base, ELFMAG, SELFMAG) != 0) { + return false; + } + + const Ehdr* elf_header = reinterpret_cast<const Ehdr*>(elf_base); + if (elf_header->e_ident[EI_CLASS] != ElfClass::kClass) { + return false; + } + + const Phdr* phdrs = + GetOffset<ElfClass, Phdr>(elf_header, elf_header->e_phoff); + + for (int i = 0; i < elf_header->e_phnum; ++i) { + if (phdrs[i].p_type == segment_type) { + ElfSegment seg = {}; + seg.start = elf_base + phdrs[i].p_offset; + seg.size = phdrs[i].p_filesz; + segments->push_back(seg); + } + } + + return true; + } + + static bool IsValidElf(const void* elf_base) { + return strncmp(reinterpret_cast<const char*>(elf_base), ELFMAG, SELFMAG) == + 0; + } + + static int ElfClass(const void* elf_base) { + const ElfW(Ehdr)* elf_header = + reinterpret_cast<const ElfW(Ehdr)*>(elf_base); + + return elf_header->e_ident[EI_CLASS]; + } + + static bool FindElfSection(const void* elf_mapped_base, + const char* section_name, uint32_t section_type, + const void** section_start, size_t* section_size) { + if (!elf_mapped_base || !section_start || !section_size) { + return false; + } + + *section_start = NULL; + *section_size = 0; + + if (!IsValidElf(elf_mapped_base)) return false; + + int cls = ElfClass(elf_mapped_base); + const char* elf_base = static_cast<const char*>(elf_mapped_base); + + if (cls == ELFCLASS32) { + return FindElfClassSection<ElfClass32>(elf_base, section_name, + section_type, section_start, + section_size) && + *section_start != NULL; + } else if (cls == ELFCLASS64) { + return FindElfClassSection<ElfClass64>(elf_base, section_name, + section_type, section_start, + section_size) && + *section_start != NULL; + } + + return false; + } + + static bool FindElfSegments(const void* elf_mapped_base, + uint32_t segment_type, + std::vector<ElfSegment>* segments) { + if (!elf_mapped_base || !segments) { + return false; + } + + if (!IsValidElf(elf_mapped_base)) return false; + + int cls = ElfClass(elf_mapped_base); + const char* elf_base = static_cast<const char*>(elf_mapped_base); + + if (cls == ELFCLASS32) { + return FindElfClassSegment<ElfClass32>(elf_base, segment_type, segments); + } else if (cls == ELFCLASS64) { + return FindElfClassSegment<ElfClass64>(elf_base, segment_type, segments); + } + + return false; + } + + // Attempt to locate a .note.gnu.build-id section in an ELF binary + // and copy it into |identifier|. + static bool FindElfBuildIDNote(const void* elf_mapped_base, + std::vector<uint8_t>& identifier) { + // lld normally creates 2 PT_NOTEs, gold normally creates 1. + std::vector<ElfSegment> segs; + if (FindElfSegments(elf_mapped_base, PT_NOTE, &segs)) { + for (ElfSegment& seg : segs) { + if (ElfClassBuildIDNoteIdentifier(seg.start, seg.size, identifier)) { + return true; + } + } + } + + void* note_section; + size_t note_size; + if (FindElfSection(elf_mapped_base, ".note.gnu.build-id", SHT_NOTE, + (const void**)¬e_section, ¬e_size)) { + return ElfClassBuildIDNoteIdentifier(note_section, note_size, identifier); + } + + return false; + } + + // Attempt to locate the .text section of an ELF binary and generate + // a simple hash by XORing the first page worth of bytes into |identifier|. + static bool HashElfTextSection(const void* elf_mapped_base, + std::vector<uint8_t>& identifier) { + identifier.resize(kMDGUIDSize); + + void* text_section; + size_t text_size; + if (!FindElfSection(elf_mapped_base, ".text", SHT_PROGBITS, + (const void**)&text_section, &text_size) || + text_size == 0) { + return false; + } + + // Only provide |kMDGUIDSize| bytes to keep identifiers produced by this + // function backwards-compatible. + memset(&identifier[0], 0, kMDGUIDSize); + const uint8_t* ptr = reinterpret_cast<const uint8_t*>(text_section); + const uint8_t* ptr_end = + ptr + std::min(text_size, static_cast<size_t>(4096)); + while (ptr < ptr_end) { + for (unsigned i = 0; i < kMDGUIDSize; i++) identifier[i] ^= ptr[i]; + ptr += kMDGUIDSize; + } + return true; + } + + // Load the identifier for the elf file mapped into memory at |base| into + // |identifier|. Return false if the identifier could not be created for this + // file. + static bool ElfFileIdentifierFromMappedFile( + const void* base, std::vector<uint8_t>& identifier) { + // Look for a build id note first. + if (FindElfBuildIDNote(base, identifier)) return true; + + // Fall back on hashing the first page of the text section. + return HashElfTextSection(base, identifier); + } + + // These three functions are not ever called in an unsafe context, so it's OK + // to allocate memory and use libc. + static std::string bytes_to_hex_string(const uint8_t* bytes, size_t count, + bool lowercase = false) { + std::string result; + for (unsigned int idx = 0; idx < count; ++idx) { + char buf[3]; + SprintfLiteral(buf, lowercase ? "%02x" : "%02X", bytes[idx]); + result.append(buf); + } + return result; + } + + // Convert the |identifier| data to a string. The string will + // be formatted as a UUID in all uppercase without dashes. + // (e.g., 22F065BBFC9C49F780FE26A7CEBD7BCE). + static std::string ConvertIdentifierToUUIDString( + const std::vector<uint8_t>& identifier) { + uint8_t identifier_swapped[kMDGUIDSize] = {0}; + + // Endian-ness swap to match dump processor expectation. + memcpy(identifier_swapped, &identifier[0], + std::min(kMDGUIDSize, identifier.size())); + uint32_t* data1 = reinterpret_cast<uint32_t*>(identifier_swapped); + *data1 = htonl(*data1); + uint16_t* data2 = reinterpret_cast<uint16_t*>(identifier_swapped + 4); + *data2 = htons(*data2); + uint16_t* data3 = reinterpret_cast<uint16_t*>(identifier_swapped + 6); + *data3 = htons(*data3); + + return bytes_to_hex_string(identifier_swapped, kMDGUIDSize); + } + + // Convert the entire |identifier| data to a lowercase hex string. + static std::string ConvertIdentifierToString( + const std::vector<uint8_t>& identifier) { + return bytes_to_hex_string(&identifier[0], identifier.size(), + /* lowercase */ true); + } + + private: + // Storage for the path specified + std::string path_; +}; + +// End of imports from toolkit/crashreporter/google-breakpad/. +// ---------------------------------------------------------------------------- + +struct LoadedLibraryInfo { + LoadedLibraryInfo(const char* aName, unsigned long aBaseAddress, + unsigned long aFirstMappingStart, + unsigned long aLastMappingEnd) + : mName(aName), + mBaseAddress(aBaseAddress), + mFirstMappingStart(aFirstMappingStart), + mLastMappingEnd(aLastMappingEnd) {} + + std::string mName; + unsigned long mBaseAddress; + unsigned long mFirstMappingStart; + unsigned long mLastMappingEnd; +}; + +static std::string IDtoUUIDString(const std::vector<uint8_t>& aIdentifier) { + std::string uuid = FileID::ConvertIdentifierToUUIDString(aIdentifier); + // This is '0', not '\0', since it represents the breakpad id age. + uuid += '0'; + return uuid; +} + +// Return raw Build ID in hex. +static std::string IDtoString(const std::vector<uint8_t>& aIdentifier) { + std::string uuid = FileID::ConvertIdentifierToString(aIdentifier); + return uuid; +} + +// Get the breakpad Id for the binary file pointed by bin_name +static std::string getBreakpadId(const char* bin_name) { + std::vector<uint8_t> identifier; + identifier.reserve(kDefaultBuildIdSize); + + FileID file_id(bin_name); + if (file_id.ElfFileIdentifier(identifier)) { + return IDtoUUIDString(identifier); + } + + return {}; +} + +// Get the code Id for the binary file pointed by bin_name +static std::string getCodeId(const char* bin_name) { + std::vector<uint8_t> identifier; + identifier.reserve(kDefaultBuildIdSize); + + FileID file_id(bin_name); + if (file_id.ElfFileIdentifier(identifier)) { + return IDtoString(identifier); + } + + return {}; +} + +static SharedLibrary SharedLibraryAtPath(const char* path, + unsigned long libStart, + unsigned long libEnd, + unsigned long offset = 0) { + std::string pathStr = path; + + size_t pos = pathStr.rfind('\\'); + std::string nameStr = + (pos != std::string::npos) ? pathStr.substr(pos + 1) : pathStr; + + return SharedLibrary(libStart, libEnd, offset, getBreakpadId(path), + getCodeId(path), nameStr, pathStr, nameStr, pathStr, + std::string{}, ""); +} + +static int dl_iterate_callback(struct dl_phdr_info* dl_info, size_t size, + void* data) { + auto libInfoList = reinterpret_cast<std::vector<LoadedLibraryInfo>*>(data); + + if (dl_info->dlpi_phnum <= 0) return 0; + + unsigned long baseAddress = dl_info->dlpi_addr; + unsigned long firstMappingStart = -1; + unsigned long lastMappingEnd = 0; + + for (size_t i = 0; i < dl_info->dlpi_phnum; i++) { + if (dl_info->dlpi_phdr[i].p_type != PT_LOAD) { + continue; + } + unsigned long start = dl_info->dlpi_addr + dl_info->dlpi_phdr[i].p_vaddr; + unsigned long end = start + dl_info->dlpi_phdr[i].p_memsz; + if (start < firstMappingStart) { + firstMappingStart = start; + } + if (end > lastMappingEnd) { + lastMappingEnd = end; + } + } + + libInfoList->push_back(LoadedLibraryInfo(dl_info->dlpi_name, baseAddress, + firstMappingStart, lastMappingEnd)); + + return 0; +} + +SharedLibraryInfo SharedLibraryInfo::GetInfoForSelf() { + SharedLibraryInfo info; + +#if defined(GP_OS_linux) + // We need to find the name of the executable (exeName, exeNameLen) and the + // address of its executable section (exeExeAddr) in the running image. + char exeName[PATH_MAX]; + memset(exeName, 0, sizeof(exeName)); + + ssize_t exeNameLen = readlink("/proc/self/exe", exeName, sizeof(exeName) - 1); + if (exeNameLen == -1) { + // readlink failed for whatever reason. Note this, but keep going. + exeName[0] = '\0'; + exeNameLen = 0; + // LOG("SharedLibraryInfo::GetInfoForSelf(): readlink failed"); + } else { + // Assert no buffer overflow. + MOZ_RELEASE_ASSERT(exeNameLen >= 0 && + exeNameLen < static_cast<ssize_t>(sizeof(exeName))); + } + + unsigned long exeExeAddr = 0; +#endif + +#if defined(GP_OS_android) + // If dl_iterate_phdr doesn't exist, we give up immediately. + if (!dl_iterate_phdr) { + // On ARM Android, dl_iterate_phdr is provided by the custom linker. + // So if libxul was loaded by the system linker (e.g. as part of + // xpcshell when running tests), it won't be available and we should + // not call it. + return info; + } +#endif + +#if defined(GP_OS_linux) || defined(GP_OS_android) + // Read info from /proc/self/maps. We ignore most of it. + pid_t pid = mozilla::baseprofiler::profiler_current_process_id().ToNumber(); + char path[PATH_MAX]; + SprintfLiteral(path, "/proc/%d/maps", pid); + std::ifstream maps(path); + std::string line; + while (std::getline(maps, line)) { + int ret; + unsigned long start; + unsigned long end; + char perm[6 + 1] = ""; + unsigned long offset; + char modulePath[PATH_MAX + 1] = ""; + ret = sscanf(line.c_str(), + "%lx-%lx %6s %lx %*s %*x %" PATH_MAX_STRING(PATH_MAX) "s\n", + &start, &end, perm, &offset, modulePath); + if (!strchr(perm, 'x')) { + // Ignore non executable entries + continue; + } + if (ret != 5 && ret != 4) { + // LOG("SharedLibraryInfo::GetInfoForSelf(): " + // "reading /proc/self/maps failed"); + continue; + } + +# if defined(GP_OS_linux) + // Try to establish the main executable's load address. + if (exeNameLen > 0 && strcmp(modulePath, exeName) == 0) { + exeExeAddr = start; + } +# elif defined(GP_OS_android) + // Use /proc/pid/maps to get the dalvik-jit section since it has no + // associated phdrs. + if (0 == strcmp(modulePath, "/dev/ashmem/dalvik-jit-code-cache")) { + info.AddSharedLibrary( + SharedLibraryAtPath(modulePath, start, end, offset)); + if (info.GetSize() > 10000) { + // LOG("SharedLibraryInfo::GetInfoForSelf(): " + // "implausibly large number of mappings acquired"); + break; + } + } +# endif + } +#endif + + std::vector<LoadedLibraryInfo> libInfoList; + + // We collect the bulk of the library info using dl_iterate_phdr. + dl_iterate_phdr(dl_iterate_callback, &libInfoList); + + for (const auto& libInfo : libInfoList) { + info.AddSharedLibrary( + SharedLibraryAtPath(libInfo.mName.c_str(), libInfo.mFirstMappingStart, + libInfo.mLastMappingEnd, + libInfo.mFirstMappingStart - libInfo.mBaseAddress)); + } + +#if defined(GP_OS_linux) + // Make another pass over the information we just harvested from + // dl_iterate_phdr. If we see a nameless object mapped at what we earlier + // established to be the main executable's load address, attach the + // executable's name to that entry. + for (size_t i = 0; i < info.GetSize(); i++) { + SharedLibrary& lib = info.GetMutableEntry(i); + if (lib.GetStart() <= exeExeAddr && exeExeAddr <= lib.GetEnd() && + lib.GetDebugPath().empty()) { + lib = SharedLibraryAtPath(exeName, lib.GetStart(), lib.GetEnd(), + lib.GetOffset()); + + // We only expect to see one such entry. + break; + } + } +#endif + + return info; +} + +void SharedLibraryInfo::Initialize() { /* do nothing */ +} diff --git a/mozglue/baseprofiler/core/shared-libraries-macos.cc b/mozglue/baseprofiler/core/shared-libraries-macos.cc new file mode 100644 index 0000000000..f995581001 --- /dev/null +++ b/mozglue/baseprofiler/core/shared-libraries-macos.cc @@ -0,0 +1,206 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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 "BaseProfilerSharedLibraries.h" + +#include "platform.h" + +#include "mozilla/Unused.h" +#include <AvailabilityMacros.h> + +#include <dlfcn.h> +#include <mach-o/arch.h> +#include <mach-o/dyld_images.h> +#include <mach-o/dyld.h> +#include <mach-o/loader.h> +#include <mach/mach_init.h> +#include <mach/mach_traps.h> +#include <mach/task_info.h> +#include <mach/task.h> +#include <sstream> +#include <stdlib.h> +#include <string.h> +#include <vector> + +// Architecture specific abstraction. +#if defined(GP_ARCH_x86) +typedef mach_header platform_mach_header; +typedef segment_command mach_segment_command_type; +# define MACHO_MAGIC_NUMBER MH_MAGIC +# define CMD_SEGMENT LC_SEGMENT +# define seg_size uint32_t +#else +typedef mach_header_64 platform_mach_header; +typedef segment_command_64 mach_segment_command_type; +# define MACHO_MAGIC_NUMBER MH_MAGIC_64 +# define CMD_SEGMENT LC_SEGMENT_64 +# define seg_size uint64_t +#endif + +struct NativeSharedLibrary { + const platform_mach_header* header; + std::string path; +}; +static std::vector<NativeSharedLibrary>* sSharedLibrariesList = nullptr; + +class MOZ_RAII SharedLibrariesLock { + public: + SharedLibrariesLock() { sSharedLibrariesMutex.Lock(); } + + ~SharedLibrariesLock() { sSharedLibrariesMutex.Unlock(); } + + SharedLibrariesLock(const SharedLibrariesLock&) = delete; + void operator=(const SharedLibrariesLock&) = delete; + + private: + static mozilla::baseprofiler::detail::BaseProfilerMutex sSharedLibrariesMutex; +}; + +mozilla::baseprofiler::detail::BaseProfilerMutex + SharedLibrariesLock::sSharedLibrariesMutex; + +static void SharedLibraryAddImage(const struct mach_header* mh, + intptr_t vmaddr_slide) { + // NOTE: Presumably for backwards-compatibility reasons, this function accepts + // a mach_header even on 64-bit where it ought to be a mach_header_64. We cast + // it to the right type here. + auto header = reinterpret_cast<const platform_mach_header*>(mh); + + Dl_info info; + if (!dladdr(header, &info)) { + return; + } + + SharedLibrariesLock lock; + if (!sSharedLibrariesList) { + return; + } + + NativeSharedLibrary lib = {header, info.dli_fname}; + sSharedLibrariesList->push_back(lib); +} + +static void SharedLibraryRemoveImage(const struct mach_header* mh, + intptr_t vmaddr_slide) { + // NOTE: Presumably for backwards-compatibility reasons, this function accepts + // a mach_header even on 64-bit where it ought to be a mach_header_64. We cast + // it to the right type here. + auto header = reinterpret_cast<const platform_mach_header*>(mh); + + SharedLibrariesLock lock; + if (!sSharedLibrariesList) { + return; + } + + uint32_t count = sSharedLibrariesList->size(); + for (uint32_t i = 0; i < count; ++i) { + if ((*sSharedLibrariesList)[i].header == header) { + sSharedLibrariesList->erase(sSharedLibrariesList->begin() + i); + return; + } + } +} + +void SharedLibraryInfo::Initialize() { + // NOTE: We intentionally leak this memory here. We're allocating dynamically + // in order to avoid static initializers. + sSharedLibrariesList = new std::vector<NativeSharedLibrary>(); + + _dyld_register_func_for_add_image(SharedLibraryAddImage); + _dyld_register_func_for_remove_image(SharedLibraryRemoveImage); +} + +static void addSharedLibrary(const platform_mach_header* header, + const char* path, SharedLibraryInfo& info) { + const struct load_command* cmd = + reinterpret_cast<const struct load_command*>(header + 1); + + seg_size size = 0; + unsigned long long start = reinterpret_cast<unsigned long long>(header); + // Find the cmd segment in the macho image. It will contain the offset we care + // about. + const uint8_t* uuid_bytes = nullptr; + for (unsigned int i = 0; + cmd && (i < header->ncmds) && (uuid_bytes == nullptr || size == 0); + ++i) { + if (cmd->cmd == CMD_SEGMENT) { + const mach_segment_command_type* seg = + reinterpret_cast<const mach_segment_command_type*>(cmd); + + if (!strcmp(seg->segname, "__TEXT")) { + size = seg->vmsize; + } + } else if (cmd->cmd == LC_UUID) { + const uuid_command* ucmd = reinterpret_cast<const uuid_command*>(cmd); + uuid_bytes = ucmd->uuid; + } + + cmd = reinterpret_cast<const struct load_command*>( + reinterpret_cast<const char*>(cmd) + cmd->cmdsize); + } + + std::string uuid; + std::string breakpadId; + if (uuid_bytes != nullptr) { + static constexpr char digits[16] = {'0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; + for (int i = 0; i < 16; ++i) { + uint8_t byte = uuid_bytes[i]; + uuid += digits[byte >> 4]; + uuid += digits[byte & 0xFu]; + } + + // Breakpad id is the same as the uuid but with the additional trailing 0 + // for the breakpad id age. + breakpadId = uuid; + // breakpad id age. + breakpadId += '0'; + } + + std::string pathStr = path; + + size_t pos = pathStr.rfind('\\'); + std::string nameStr = + (pos != std::string::npos) ? pathStr.substr(pos + 1) : pathStr; + + const NXArchInfo* archInfo = + NXGetArchInfoFromCpuType(header->cputype, header->cpusubtype); + + info.AddSharedLibrary(SharedLibrary( + start, start + size, 0, breakpadId, uuid, nameStr, pathStr, nameStr, + pathStr, std::string{}, archInfo ? archInfo->name : "")); +} + +// Translate the statically stored sSharedLibrariesList information into a +// SharedLibraryInfo object. +SharedLibraryInfo SharedLibraryInfo::GetInfoForSelf() { + SharedLibrariesLock lock; + SharedLibraryInfo sharedLibraryInfo; + + for (auto& info : *sSharedLibrariesList) { + addSharedLibrary(info.header, info.path.c_str(), sharedLibraryInfo); + } + + // Add the entry for dyld itself. + // We only support macOS 10.12+, which corresponds to dyld version 15+. + // dyld version 15 added the dyldPath property. + task_dyld_info_data_t task_dyld_info; + mach_msg_type_number_t count = TASK_DYLD_INFO_COUNT; + if (task_info(mach_task_self(), TASK_DYLD_INFO, (task_info_t)&task_dyld_info, + &count) != KERN_SUCCESS) { + return sharedLibraryInfo; + } + + struct dyld_all_image_infos* aii = + (struct dyld_all_image_infos*)task_dyld_info.all_image_info_addr; + if (aii->version >= 15) { + const platform_mach_header* header = + reinterpret_cast<const platform_mach_header*>( + aii->dyldImageLoadAddress); + addSharedLibrary(header, aii->dyldPath, sharedLibraryInfo); + } + + return sharedLibraryInfo; +} diff --git a/mozglue/baseprofiler/core/shared-libraries-win32.cc b/mozglue/baseprofiler/core/shared-libraries-win32.cc new file mode 100644 index 0000000000..eb6ca3572a --- /dev/null +++ b/mozglue/baseprofiler/core/shared-libraries-win32.cc @@ -0,0 +1,222 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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 <windows.h> + +#include "BaseProfilerSharedLibraries.h" + +#include "mozilla/glue/WindowsUnicode.h" +#include "mozilla/NativeNt.h" +#include "mozilla/WindowsEnumProcessModules.h" +#include "mozilla/WindowsVersion.h" + +#include <cctype> +#include <string> + +static constexpr char uppercaseDigits[16] = {'0', '1', '2', '3', '4', '5', + '6', '7', '8', '9', 'A', 'B', + 'C', 'D', 'E', 'F'}; +static constexpr char lowercaseDigits[16] = {'0', '1', '2', '3', '4', '5', + '6', '7', '8', '9', 'a', 'b', + 'c', 'd', 'e', 'f'}; + +static void AppendHex(const unsigned char* aBegin, const unsigned char* aEnd, + std::string& aOut) { + for (const unsigned char* p = aBegin; p < aEnd; ++p) { + unsigned char c = *p; + aOut += uppercaseDigits[c >> 4]; + aOut += uppercaseDigits[c & 0xFu]; + } +} + +static constexpr bool WITH_PADDING = true; +static constexpr bool WITHOUT_PADDING = false; +static constexpr bool LOWERCASE = true; +static constexpr bool UPPERCASE = false; +template <typename T> +static void AppendHex(T aValue, std::string& aOut, bool aWithPadding, + bool aLowercase = UPPERCASE) { + for (int i = sizeof(T) * 2 - 1; i >= 0; --i) { + unsigned nibble = (aValue >> (i * 4)) & 0xFu; + // If no-padding requested, skip starting zeroes -- unless we're on the very + // last nibble (so we don't output a blank). + if (!aWithPadding && i != 0) { + if (nibble == 0) { + // Requested no padding, skip zeroes. + continue; + } + // Requested no padding, got first non-zero, pretend we now want padding + // so we don't skip zeroes anymore. + aWithPadding = true; + } + aOut += aLowercase ? lowercaseDigits[nibble] : uppercaseDigits[nibble]; + } +} + +static bool IsModuleUnsafeToLoad(const std::string& aModuleName) { + auto LowerCaseEqualsLiteral = [](char aModuleChar, char aDetouredChar) { + return std::tolower(aModuleChar) == aDetouredChar; + }; + +#if defined(_M_AMD64) || defined(_M_IX86) + // Hackaround for Bug 1607574. Nvidia's shim driver nvd3d9wrap[x].dll detours + // LoadLibraryExW and it causes AV when the following conditions are met. + // 1. LoadLibraryExW was called for "detoured.dll" + // 2. nvinit[x].dll was unloaded + // 3. OS version is older than 6.2 +# if defined(_M_AMD64) + LPCWSTR kNvidiaShimDriver = L"nvd3d9wrapx.dll"; + LPCWSTR kNvidiaInitDriver = L"nvinitx.dll"; +# elif defined(_M_IX86) + LPCWSTR kNvidiaShimDriver = L"nvd3d9wrap.dll"; + LPCWSTR kNvidiaInitDriver = L"nvinit.dll"; +# endif + constexpr std::string_view detoured_dll = "detoured.dll"; + if (std::equal(aModuleName.cbegin(), aModuleName.cend(), + detoured_dll.cbegin(), detoured_dll.cend(), + LowerCaseEqualsLiteral) && + !mozilla::IsWin8OrLater() && ::GetModuleHandleW(kNvidiaShimDriver) && + !::GetModuleHandleW(kNvidiaInitDriver)) { + return true; + } +#endif // defined(_M_AMD64) || defined(_M_IX86) + + // Hackaround for Bug 1723868. There is no safe way to prevent the module + // Microsoft's VP9 Video Decoder from being unloaded because mfplat.dll may + // have posted more than one task to unload the module in the work queue + // without calling LoadLibrary. + constexpr std::string_view vp9_decoder_dll = "msvp9dec_store.dll"; + if (std::equal(aModuleName.cbegin(), aModuleName.cend(), + vp9_decoder_dll.cbegin(), vp9_decoder_dll.cend(), + LowerCaseEqualsLiteral)) { + return true; + } + + return false; +} + +void SharedLibraryInfo::AddSharedLibraryFromModuleInfo( + const wchar_t* aModulePath, mozilla::Maybe<HMODULE> aModule) { + mozilla::UniquePtr<char[]> utf8ModulePath( + mozilla::glue::WideToUTF8(aModulePath)); + if (!utf8ModulePath) { + return; + } + + std::string modulePathStr(utf8ModulePath.get()); + size_t pos = modulePathStr.find_last_of("\\/"); + std::string moduleNameStr = (pos != std::string::npos) + ? modulePathStr.substr(pos + 1) + : modulePathStr; + + // If the module is unsafe to call LoadLibraryEx for, we skip. + if (IsModuleUnsafeToLoad(moduleNameStr)) { + return; + } + + // Load the module again - to make sure that its handle will remain valid as + // we attempt to read the PDB information from it - or for the first time if + // we only have a path. We want to load the DLL without running the newly + // loaded module's DllMain function, but not as a data file because we want + // to be able to do RVA computations easily. Hence, we use the flag + // LOAD_LIBRARY_AS_IMAGE_RESOURCE which ensures that the sections (not PE + // headers) will be relocated by the loader. Otherwise GetPdbInfo() and/or + // GetVersionInfo() can cause a crash. If the original handle |aModule| is + // valid, LoadLibraryEx just increments its refcount. + nsModuleHandle handleLock( + ::LoadLibraryExW(aModulePath, NULL, LOAD_LIBRARY_AS_IMAGE_RESOURCE)); + if (!handleLock) { + return; + } + + mozilla::nt::PEHeaders headers(handleLock.get()); + if (!headers) { + return; + } + + mozilla::Maybe<mozilla::Range<const uint8_t>> bounds = headers.GetBounds(); + if (!bounds) { + return; + } + + // Put the original |aModule| into SharedLibrary, but we get debug info + // from |handleLock| as |aModule| might be inaccessible. + const uintptr_t modStart = + aModule.isSome() ? reinterpret_cast<uintptr_t>(*aModule) + : reinterpret_cast<uintptr_t>(handleLock.get()); + const uintptr_t modEnd = modStart + bounds->length(); + + std::string breakpadId; + std::string pdbPathStr; + std::string pdbNameStr; + if (const auto* debugInfo = headers.GetPdbInfo()) { + MOZ_ASSERT(breakpadId.empty()); + const GUID& pdbSig = debugInfo->pdbSignature; + AppendHex(pdbSig.Data1, breakpadId, WITH_PADDING); + AppendHex(pdbSig.Data2, breakpadId, WITH_PADDING); + AppendHex(pdbSig.Data3, breakpadId, WITH_PADDING); + AppendHex(reinterpret_cast<const unsigned char*>(&pdbSig.Data4), + reinterpret_cast<const unsigned char*>(&pdbSig.Data4) + + sizeof(pdbSig.Data4), + breakpadId); + AppendHex(debugInfo->pdbAge, breakpadId, WITHOUT_PADDING); + + // The PDB file name could be different from module filename, + // so report both + // e.g. The PDB for C:\Windows\SysWOW64\ntdll.dll is wntdll.pdb + pdbPathStr = debugInfo->pdbFileName; + size_t pos = pdbPathStr.find_last_of("\\/"); + pdbNameStr = + (pos != std::string::npos) ? pdbPathStr.substr(pos + 1) : pdbPathStr; + } + + std::string codeId; + DWORD timestamp; + DWORD imageSize; + if (headers.GetTimeStamp(timestamp) && headers.GetImageSize(imageSize)) { + AppendHex(timestamp, codeId, WITH_PADDING); + AppendHex(imageSize, codeId, WITHOUT_PADDING, LOWERCASE); + } + + std::string versionStr; + uint64_t version; + if (headers.GetVersionInfo(version)) { + versionStr += std::to_string((version >> 48) & 0xFFFF); + versionStr += '.'; + versionStr += std::to_string((version >> 32) & 0xFFFF); + versionStr += '.'; + versionStr += std::to_string((version >> 16) & 0xFFFF); + versionStr += '.'; + versionStr += std::to_string(version & 0xFFFF); + } + + SharedLibrary shlib(modStart, modEnd, + 0, // DLLs are always mapped at offset 0 on Windows + breakpadId, codeId, moduleNameStr, modulePathStr, + pdbNameStr, pdbPathStr, versionStr, ""); + AddSharedLibrary(shlib); +} + +SharedLibraryInfo SharedLibraryInfo::GetInfoForSelf() { + SharedLibraryInfo sharedLibraryInfo; + + auto addSharedLibraryFromModuleInfo = + [&sharedLibraryInfo](const wchar_t* aModulePath, HMODULE aModule) { + sharedLibraryInfo.AddSharedLibraryFromModuleInfo( + aModulePath, mozilla::Some(aModule)); + }; + + mozilla::EnumerateProcessModules(addSharedLibraryFromModuleInfo); + return sharedLibraryInfo; +} + +SharedLibraryInfo SharedLibraryInfo::GetInfoFromPath(const wchar_t* aPath) { + SharedLibraryInfo sharedLibraryInfo; + sharedLibraryInfo.AddSharedLibraryFromModuleInfo(aPath, mozilla::Nothing()); + return sharedLibraryInfo; +} + +void SharedLibraryInfo::Initialize() { /* do nothing */ +} diff --git a/mozglue/baseprofiler/core/vtune/ittnotify.h b/mozglue/baseprofiler/core/vtune/ittnotify.h new file mode 100644 index 0000000000..04adf9eb5e --- /dev/null +++ b/mozglue/baseprofiler/core/vtune/ittnotify.h @@ -0,0 +1,4127 @@ +// clang-format off + +/* <copyright> + This file is provided under a dual BSD/GPLv2 license. When using or + redistributing this file, you may do so under either license. + + GPL LICENSE SUMMARY + + Copyright (c) 2005-2014 Intel Corporation. All rights reserved. + + This program is free software; you can redistribute it and/or modify + it under the terms of version 2 of the GNU General Public License as + published by the Free Software Foundation. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. + The full GNU General Public License is included in this distribution + in the file called LICENSE.GPL. + + Contact Information: + http://software.intel.com/en-us/articles/intel-vtune-amplifier-xe/ + + BSD LICENSE + + Copyright (c) 2005-2014 Intel Corporation. All rights reserved. + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + * Neither the name of Intel Corporation nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +</copyright> */ +#ifndef _ITTNOTIFY_H_ +# define _ITTNOTIFY_H_ + +/** +@file +@brief Public User API functions and types +@mainpage + +The ITT API is used to annotate a user's program with additional information +that can be used by correctness and performance tools. The user inserts +calls in their program. Those calls generate information that is collected +at runtime, and used by Intel(R) Threading Tools. + +@section API Concepts +The following general concepts are used throughout the API. + +@subsection Unicode Support +Many API functions take character string arguments. On Windows, there +are two versions of each such function. The function name is suffixed +by W if Unicode support is enabled, and by A otherwise. Any API function +that takes a character string argument adheres to this convention. + +@subsection Conditional Compilation +Many users prefer having an option to modify ITT API code when linking it +inside their runtimes. ITT API header file provides a mechanism to replace +ITT API function names inside your code with empty strings. To do this, +define the macros INTEL_NO_ITTNOTIFY_API during compilation and remove the +static library from the linker script. + +@subsection Domains +[see domains] +Domains provide a way to separate notification for different modules or +libraries in a program. Domains are specified by dotted character strings, +e.g. TBB.Internal.Control. + +A mechanism (to be specified) is provided to enable and disable +domains. By default, all domains are enabled. +@subsection Named Entities and Instances +Named entities (frames, regions, tasks, and markers) communicate +information about the program to the analysis tools. A named entity often +refers to a section of program code, or to some set of logical concepts +that the programmer wants to group together. + +Named entities relate to the programmer's static view of the program. When +the program actually executes, many instances of a given named entity +may be created. + +The API annotations denote instances of named entities. The actual +named entities are displayed using the analysis tools. In other words, +the named entities come into existence when instances are created. + +Instances of named entities may have instance identifiers (IDs). Some +API calls use instance identifiers to create relationships between +different instances of named entities. Other API calls associate data +with instances of named entities. + +Some named entities must always have instance IDs. In particular, regions +and frames always have IDs. Task and markers need IDs only if the ID is +needed in another API call (such as adding a relation or metadata). + +The lifetime of instance IDs is distinct from the lifetime of +instances. This allows various relationships to be specified separate +from the actual execution of instances. This flexibility comes at the +expense of extra API calls. + +The same ID may not be reused for different instances, unless a previous +[ref] __itt_id_destroy call for that ID has been issued. +*/ + +/** @cond exclude_from_documentation */ +#ifndef ITT_OS_WIN +# define ITT_OS_WIN 1 +#endif /* ITT_OS_WIN */ + +#ifndef ITT_OS_LINUX +# define ITT_OS_LINUX 2 +#endif /* ITT_OS_LINUX */ + +#ifndef ITT_OS_MAC +# define ITT_OS_MAC 3 +#endif /* ITT_OS_MAC */ + +#ifndef ITT_OS_FREEBSD +# define ITT_OS_FREEBSD 4 +#endif /* ITT_OS_FREEBSD */ + +#ifndef ITT_OS +# if defined WIN32 || defined _WIN32 +# define ITT_OS ITT_OS_WIN +# elif defined( __APPLE__ ) && defined( __MACH__ ) +# define ITT_OS ITT_OS_MAC +# elif defined( __FreeBSD__ ) +# define ITT_OS ITT_OS_FREEBSD +# else +# define ITT_OS ITT_OS_LINUX +# endif +#endif /* ITT_OS */ + +#ifndef ITT_PLATFORM_WIN +# define ITT_PLATFORM_WIN 1 +#endif /* ITT_PLATFORM_WIN */ + +#ifndef ITT_PLATFORM_POSIX +# define ITT_PLATFORM_POSIX 2 +#endif /* ITT_PLATFORM_POSIX */ + +#ifndef ITT_PLATFORM_MAC +# define ITT_PLATFORM_MAC 3 +#endif /* ITT_PLATFORM_MAC */ + +#ifndef ITT_PLATFORM_FREEBSD +# define ITT_PLATFORM_FREEBSD 4 +#endif /* ITT_PLATFORM_FREEBSD */ + +#ifndef ITT_PLATFORM +# if ITT_OS==ITT_OS_WIN +# define ITT_PLATFORM ITT_PLATFORM_WIN +# elif ITT_OS==ITT_OS_MAC +# define ITT_PLATFORM ITT_PLATFORM_MAC +# elif ITT_OS==ITT_OS_FREEBSD +# define ITT_PLATFORM ITT_PLATFORM_FREEBSD +# else +# define ITT_PLATFORM ITT_PLATFORM_POSIX +# endif +#endif /* ITT_PLATFORM */ + +#if defined(_UNICODE) && !defined(UNICODE) +#define UNICODE +#endif + +#include <stddef.h> +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#include <tchar.h> +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#include <stdint.h> +#if defined(UNICODE) || defined(_UNICODE) +#include <wchar.h> +#endif /* UNICODE || _UNICODE */ +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ + +#ifndef ITTAPI_CDECL +# if ITT_PLATFORM==ITT_PLATFORM_WIN +# define ITTAPI_CDECL __cdecl +# else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +# if defined _M_IX86 || defined __i386__ +# define ITTAPI_CDECL __attribute__ ((cdecl)) +# else /* _M_IX86 || __i386__ */ +# define ITTAPI_CDECL /* actual only on x86 platform */ +# endif /* _M_IX86 || __i386__ */ +# endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* ITTAPI_CDECL */ + +#ifndef STDCALL +# if ITT_PLATFORM==ITT_PLATFORM_WIN +# define STDCALL __stdcall +# else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +# if defined _M_IX86 || defined __i386__ +# define STDCALL __attribute__ ((stdcall)) +# else /* _M_IX86 || __i386__ */ +# define STDCALL /* supported only on x86 platform */ +# endif /* _M_IX86 || __i386__ */ +# endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* STDCALL */ + +#define ITTAPI ITTAPI_CDECL +#define LIBITTAPI ITTAPI_CDECL + +/* TODO: Temporary for compatibility! */ +#define ITTAPI_CALL ITTAPI_CDECL +#define LIBITTAPI_CALL ITTAPI_CDECL + +#if ITT_PLATFORM==ITT_PLATFORM_WIN +/* use __forceinline (VC++ specific) */ +#define ITT_INLINE __forceinline +#define ITT_INLINE_ATTRIBUTE /* nothing */ +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +/* + * Generally, functions are not inlined unless optimization is specified. + * For functions declared inline, this attribute inlines the function even + * if no optimization level was specified. + */ +#ifdef __STRICT_ANSI__ +#define ITT_INLINE static +#define ITT_INLINE_ATTRIBUTE __attribute__((unused)) +#else /* __STRICT_ANSI__ */ +#define ITT_INLINE static inline +#define ITT_INLINE_ATTRIBUTE __attribute__((always_inline, unused)) +#endif /* __STRICT_ANSI__ */ +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +/** @endcond */ + +#ifdef INTEL_ITTNOTIFY_ENABLE_LEGACY +# if ITT_PLATFORM==ITT_PLATFORM_WIN +# pragma message("WARNING!!! Deprecated API is used. Please undefine INTEL_ITTNOTIFY_ENABLE_LEGACY macro") +# else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +# warning "Deprecated API is used. Please undefine INTEL_ITTNOTIFY_ENABLE_LEGACY macro" +# endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +# include "vtune/legacy/ittnotify.h" +#endif /* INTEL_ITTNOTIFY_ENABLE_LEGACY */ + +/** @cond exclude_from_documentation */ +/* Helper macro for joining tokens */ +#define ITT_JOIN_AUX(p,n) p##n +#define ITT_JOIN(p,n) ITT_JOIN_AUX(p,n) + +#ifdef ITT_MAJOR +#undef ITT_MAJOR +#endif +#ifdef ITT_MINOR +#undef ITT_MINOR +#endif +#define ITT_MAJOR 3 +#define ITT_MINOR 0 + +/* Standard versioning of a token with major and minor version numbers */ +#define ITT_VERSIONIZE(x) \ + ITT_JOIN(x, \ + ITT_JOIN(_, \ + ITT_JOIN(ITT_MAJOR, \ + ITT_JOIN(_, ITT_MINOR)))) + +#ifndef INTEL_ITTNOTIFY_PREFIX +# define INTEL_ITTNOTIFY_PREFIX __itt_ +#endif /* INTEL_ITTNOTIFY_PREFIX */ +#ifndef INTEL_ITTNOTIFY_POSTFIX +# define INTEL_ITTNOTIFY_POSTFIX _ptr_ +#endif /* INTEL_ITTNOTIFY_POSTFIX */ + +#define ITTNOTIFY_NAME_AUX(n) ITT_JOIN(INTEL_ITTNOTIFY_PREFIX,n) +#define ITTNOTIFY_NAME(n) ITT_VERSIONIZE(ITTNOTIFY_NAME_AUX(ITT_JOIN(n,INTEL_ITTNOTIFY_POSTFIX))) + +#define ITTNOTIFY_VOID(n) (!ITTNOTIFY_NAME(n)) ? (void)0 : ITTNOTIFY_NAME(n) +#define ITTNOTIFY_DATA(n) (!ITTNOTIFY_NAME(n)) ? 0 : ITTNOTIFY_NAME(n) + +#define ITTNOTIFY_VOID_D0(n,d) (!(d)->flags) ? (void)0 : (!ITTNOTIFY_NAME(n)) ? (void)0 : ITTNOTIFY_NAME(n)(d) +#define ITTNOTIFY_VOID_D1(n,d,x) (!(d)->flags) ? (void)0 : (!ITTNOTIFY_NAME(n)) ? (void)0 : ITTNOTIFY_NAME(n)(d,x) +#define ITTNOTIFY_VOID_D2(n,d,x,y) (!(d)->flags) ? (void)0 : (!ITTNOTIFY_NAME(n)) ? (void)0 : ITTNOTIFY_NAME(n)(d,x,y) +#define ITTNOTIFY_VOID_D3(n,d,x,y,z) (!(d)->flags) ? (void)0 : (!ITTNOTIFY_NAME(n)) ? (void)0 : ITTNOTIFY_NAME(n)(d,x,y,z) +#define ITTNOTIFY_VOID_D4(n,d,x,y,z,a) (!(d)->flags) ? (void)0 : (!ITTNOTIFY_NAME(n)) ? (void)0 : ITTNOTIFY_NAME(n)(d,x,y,z,a) +#define ITTNOTIFY_VOID_D5(n,d,x,y,z,a,b) (!(d)->flags) ? (void)0 : (!ITTNOTIFY_NAME(n)) ? (void)0 : ITTNOTIFY_NAME(n)(d,x,y,z,a,b) +#define ITTNOTIFY_VOID_D6(n,d,x,y,z,a,b,c) (!(d)->flags) ? (void)0 : (!ITTNOTIFY_NAME(n)) ? (void)0 : ITTNOTIFY_NAME(n)(d,x,y,z,a,b,c) +#define ITTNOTIFY_DATA_D0(n,d) (!(d)->flags) ? 0 : (!ITTNOTIFY_NAME(n)) ? 0 : ITTNOTIFY_NAME(n)(d) +#define ITTNOTIFY_DATA_D1(n,d,x) (!(d)->flags) ? 0 : (!ITTNOTIFY_NAME(n)) ? 0 : ITTNOTIFY_NAME(n)(d,x) +#define ITTNOTIFY_DATA_D2(n,d,x,y) (!(d)->flags) ? 0 : (!ITTNOTIFY_NAME(n)) ? 0 : ITTNOTIFY_NAME(n)(d,x,y) +#define ITTNOTIFY_DATA_D3(n,d,x,y,z) (!(d)->flags) ? 0 : (!ITTNOTIFY_NAME(n)) ? 0 : ITTNOTIFY_NAME(n)(d,x,y,z) +#define ITTNOTIFY_DATA_D4(n,d,x,y,z,a) (!(d)->flags) ? 0 : (!ITTNOTIFY_NAME(n)) ? 0 : ITTNOTIFY_NAME(n)(d,x,y,z,a) +#define ITTNOTIFY_DATA_D5(n,d,x,y,z,a,b) (!(d)->flags) ? 0 : (!ITTNOTIFY_NAME(n)) ? 0 : ITTNOTIFY_NAME(n)(d,x,y,z,a,b) +#define ITTNOTIFY_DATA_D6(n,d,x,y,z,a,b,c) (!(d)->flags) ? 0 : (!ITTNOTIFY_NAME(n)) ? 0 : ITTNOTIFY_NAME(n)(d,x,y,z,a,b,c) + +#ifdef ITT_STUB +#undef ITT_STUB +#endif +#ifdef ITT_STUBV +#undef ITT_STUBV +#endif +#define ITT_STUBV(api,type,name,args) \ + typedef type (api* ITT_JOIN(ITTNOTIFY_NAME(name),_t)) args; \ + extern ITT_JOIN(ITTNOTIFY_NAME(name),_t) ITTNOTIFY_NAME(name); +#define ITT_STUB ITT_STUBV +/** @endcond */ + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/** @cond exclude_from_gpa_documentation */ +/** + * @defgroup public Public API + * @{ + * @} + */ + +/** + * @defgroup control Collection Control + * @ingroup public + * General behavior: application continues to run, but no profiling information is being collected + * + * Pausing occurs not only for the current thread but for all process as well as spawned processes + * - Intel(R) Parallel Inspector and Intel(R) Inspector XE: + * - Does not analyze or report errors that involve memory access. + * - Other errors are reported as usual. Pausing data collection in + * Intel(R) Parallel Inspector and Intel(R) Inspector XE + * only pauses tracing and analyzing memory access. + * It does not pause tracing or analyzing threading APIs. + * . + * - Intel(R) Parallel Amplifier and Intel(R) VTune(TM) Amplifier XE: + * - Does continue to record when new threads are started. + * . + * - Other effects: + * - Possible reduction of runtime overhead. + * . + * @{ + */ +/** @brief Pause collection */ +void ITTAPI __itt_pause(void); +/** @brief Resume collection */ +void ITTAPI __itt_resume(void); +/** @brief Detach collection */ +void ITTAPI __itt_detach(void); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, pause, (void)) +ITT_STUBV(ITTAPI, void, resume, (void)) +ITT_STUBV(ITTAPI, void, detach, (void)) +#define __itt_pause ITTNOTIFY_VOID(pause) +#define __itt_pause_ptr ITTNOTIFY_NAME(pause) +#define __itt_resume ITTNOTIFY_VOID(resume) +#define __itt_resume_ptr ITTNOTIFY_NAME(resume) +#define __itt_detach ITTNOTIFY_VOID(detach) +#define __itt_detach_ptr ITTNOTIFY_NAME(detach) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_pause() +#define __itt_pause_ptr 0 +#define __itt_resume() +#define __itt_resume_ptr 0 +#define __itt_detach() +#define __itt_detach_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_pause_ptr 0 +#define __itt_resume_ptr 0 +#define __itt_detach_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ +/** @} control group */ +/** @endcond */ + +/** + * @defgroup threads Threads + * @ingroup public + * Give names to threads + * @{ + */ +/** + * @brief Sets thread name of calling thread + * @param[in] name - name of thread + */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +void ITTAPI __itt_thread_set_nameA(const char *name); +void ITTAPI __itt_thread_set_nameW(const wchar_t *name); +#if defined(UNICODE) || defined(_UNICODE) +# define __itt_thread_set_name __itt_thread_set_nameW +# define __itt_thread_set_name_ptr __itt_thread_set_nameW_ptr +#else /* UNICODE */ +# define __itt_thread_set_name __itt_thread_set_nameA +# define __itt_thread_set_name_ptr __itt_thread_set_nameA_ptr +#endif /* UNICODE */ +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +void ITTAPI __itt_thread_set_name(const char *name); +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +#if ITT_PLATFORM==ITT_PLATFORM_WIN +ITT_STUBV(ITTAPI, void, thread_set_nameA, (const char *name)) +ITT_STUBV(ITTAPI, void, thread_set_nameW, (const wchar_t *name)) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +ITT_STUBV(ITTAPI, void, thread_set_name, (const char *name)) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_thread_set_nameA ITTNOTIFY_VOID(thread_set_nameA) +#define __itt_thread_set_nameA_ptr ITTNOTIFY_NAME(thread_set_nameA) +#define __itt_thread_set_nameW ITTNOTIFY_VOID(thread_set_nameW) +#define __itt_thread_set_nameW_ptr ITTNOTIFY_NAME(thread_set_nameW) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_thread_set_name ITTNOTIFY_VOID(thread_set_name) +#define __itt_thread_set_name_ptr ITTNOTIFY_NAME(thread_set_name) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#else /* INTEL_NO_ITTNOTIFY_API */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_thread_set_nameA(name) +#define __itt_thread_set_nameA_ptr 0 +#define __itt_thread_set_nameW(name) +#define __itt_thread_set_nameW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_thread_set_name(name) +#define __itt_thread_set_name_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_thread_set_nameA_ptr 0 +#define __itt_thread_set_nameW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_thread_set_name_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** @cond exclude_from_gpa_documentation */ + +/** + * @brief Mark current thread as ignored from this point on, for the duration of its existence. + */ +void ITTAPI __itt_thread_ignore(void); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, thread_ignore, (void)) +#define __itt_thread_ignore ITTNOTIFY_VOID(thread_ignore) +#define __itt_thread_ignore_ptr ITTNOTIFY_NAME(thread_ignore) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_thread_ignore() +#define __itt_thread_ignore_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_thread_ignore_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ +/** @} threads group */ + +/** + * @defgroup suppress Error suppression + * @ingroup public + * General behavior: application continues to run, but errors are suppressed + * + * @{ + */ + +/*****************************************************************//** + * @name group of functions used for error suppression in correctness tools + *********************************************************************/ +/** @{ */ +/** + * @hideinitializer + * @brief possible value for suppression mask + */ +#define __itt_suppress_all_errors 0x7fffffff + +/** + * @hideinitializer + * @brief possible value for suppression mask (suppresses errors from threading analysis) + */ +#define __itt_suppress_threading_errors 0x000000ff + +/** + * @hideinitializer + * @brief possible value for suppression mask (suppresses errors from memory analysis) + */ +#define __itt_suppress_memory_errors 0x0000ff00 + +/** + * @brief Start suppressing errors identified in mask on this thread + */ +void ITTAPI __itt_suppress_push(unsigned int mask); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, suppress_push, (unsigned int mask)) +#define __itt_suppress_push ITTNOTIFY_VOID(suppress_push) +#define __itt_suppress_push_ptr ITTNOTIFY_NAME(suppress_push) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_suppress_push(mask) +#define __itt_suppress_push_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_suppress_push_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Undo the effects of the matching call to __itt_suppress_push + */ +void ITTAPI __itt_suppress_pop(void); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, suppress_pop, (void)) +#define __itt_suppress_pop ITTNOTIFY_VOID(suppress_pop) +#define __itt_suppress_pop_ptr ITTNOTIFY_NAME(suppress_pop) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_suppress_pop() +#define __itt_suppress_pop_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_suppress_pop_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @enum __itt_model_disable + * @brief Enumerator for the disable methods + */ +typedef enum __itt_suppress_mode { + __itt_unsuppress_range, + __itt_suppress_range +} __itt_suppress_mode_t; + +/** + * @brief Mark a range of memory for error suppression or unsuppression for error types included in mask + */ +void ITTAPI __itt_suppress_mark_range(__itt_suppress_mode_t mode, unsigned int mask, void * address, size_t size); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, suppress_mark_range, (__itt_suppress_mode_t mode, unsigned int mask, void * address, size_t size)) +#define __itt_suppress_mark_range ITTNOTIFY_VOID(suppress_mark_range) +#define __itt_suppress_mark_range_ptr ITTNOTIFY_NAME(suppress_mark_range) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_suppress_mark_range(mask) +#define __itt_suppress_mark_range_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_suppress_mark_range_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Undo the effect of a matching call to __itt_suppress_mark_range. If not matching + * call is found, nothing is changed. + */ +void ITTAPI __itt_suppress_clear_range(__itt_suppress_mode_t mode, unsigned int mask, void * address, size_t size); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, suppress_clear_range, (__itt_suppress_mode_t mode, unsigned int mask, void * address, size_t size)) +#define __itt_suppress_clear_range ITTNOTIFY_VOID(suppress_clear_range) +#define __itt_suppress_clear_range_ptr ITTNOTIFY_NAME(suppress_clear_range) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_suppress_clear_range(mask) +#define __itt_suppress_clear_range_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_suppress_clear_range_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ +/** @} */ +/** @} suppress group */ + +/** + * @defgroup sync Synchronization + * @ingroup public + * Indicate user-written synchronization code + * @{ + */ +/** + * @hideinitializer + * @brief possible value of attribute argument for sync object type + */ +#define __itt_attr_barrier 1 + +/** + * @hideinitializer + * @brief possible value of attribute argument for sync object type + */ +#define __itt_attr_mutex 2 + +/** +@brief Name a synchronization object +@param[in] addr Handle for the synchronization object. You should +use a real address to uniquely identify the synchronization object. +@param[in] objtype null-terminated object type string. If NULL is +passed, the name will be "User Synchronization". +@param[in] objname null-terminated object name string. If NULL, +no name will be assigned to the object. +@param[in] attribute one of [#__itt_attr_barrier, #__itt_attr_mutex] + */ + +#if ITT_PLATFORM==ITT_PLATFORM_WIN +void ITTAPI __itt_sync_createA(void *addr, const char *objtype, const char *objname, int attribute); +void ITTAPI __itt_sync_createW(void *addr, const wchar_t *objtype, const wchar_t *objname, int attribute); +#if defined(UNICODE) || defined(_UNICODE) +# define __itt_sync_create __itt_sync_createW +# define __itt_sync_create_ptr __itt_sync_createW_ptr +#else /* UNICODE */ +# define __itt_sync_create __itt_sync_createA +# define __itt_sync_create_ptr __itt_sync_createA_ptr +#endif /* UNICODE */ +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +void ITTAPI __itt_sync_create (void *addr, const char *objtype, const char *objname, int attribute); +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +#if ITT_PLATFORM==ITT_PLATFORM_WIN +ITT_STUBV(ITTAPI, void, sync_createA, (void *addr, const char *objtype, const char *objname, int attribute)) +ITT_STUBV(ITTAPI, void, sync_createW, (void *addr, const wchar_t *objtype, const wchar_t *objname, int attribute)) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +ITT_STUBV(ITTAPI, void, sync_create, (void *addr, const char* objtype, const char* objname, int attribute)) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_sync_createA ITTNOTIFY_VOID(sync_createA) +#define __itt_sync_createA_ptr ITTNOTIFY_NAME(sync_createA) +#define __itt_sync_createW ITTNOTIFY_VOID(sync_createW) +#define __itt_sync_createW_ptr ITTNOTIFY_NAME(sync_createW) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_sync_create ITTNOTIFY_VOID(sync_create) +#define __itt_sync_create_ptr ITTNOTIFY_NAME(sync_create) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#else /* INTEL_NO_ITTNOTIFY_API */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_sync_createA(addr, objtype, objname, attribute) +#define __itt_sync_createA_ptr 0 +#define __itt_sync_createW(addr, objtype, objname, attribute) +#define __itt_sync_createW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_sync_create(addr, objtype, objname, attribute) +#define __itt_sync_create_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_sync_createA_ptr 0 +#define __itt_sync_createW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_sync_create_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** +@brief Rename a synchronization object + +You can use the rename call to assign or reassign a name to a given +synchronization object. +@param[in] addr handle for the synchronization object. +@param[in] name null-terminated object name string. +*/ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +void ITTAPI __itt_sync_renameA(void *addr, const char *name); +void ITTAPI __itt_sync_renameW(void *addr, const wchar_t *name); +#if defined(UNICODE) || defined(_UNICODE) +# define __itt_sync_rename __itt_sync_renameW +# define __itt_sync_rename_ptr __itt_sync_renameW_ptr +#else /* UNICODE */ +# define __itt_sync_rename __itt_sync_renameA +# define __itt_sync_rename_ptr __itt_sync_renameA_ptr +#endif /* UNICODE */ +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +void ITTAPI __itt_sync_rename(void *addr, const char *name); +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +#if ITT_PLATFORM==ITT_PLATFORM_WIN +ITT_STUBV(ITTAPI, void, sync_renameA, (void *addr, const char *name)) +ITT_STUBV(ITTAPI, void, sync_renameW, (void *addr, const wchar_t *name)) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +ITT_STUBV(ITTAPI, void, sync_rename, (void *addr, const char *name)) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_sync_renameA ITTNOTIFY_VOID(sync_renameA) +#define __itt_sync_renameA_ptr ITTNOTIFY_NAME(sync_renameA) +#define __itt_sync_renameW ITTNOTIFY_VOID(sync_renameW) +#define __itt_sync_renameW_ptr ITTNOTIFY_NAME(sync_renameW) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_sync_rename ITTNOTIFY_VOID(sync_rename) +#define __itt_sync_rename_ptr ITTNOTIFY_NAME(sync_rename) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#else /* INTEL_NO_ITTNOTIFY_API */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_sync_renameA(addr, name) +#define __itt_sync_renameA_ptr 0 +#define __itt_sync_renameW(addr, name) +#define __itt_sync_renameW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_sync_rename(addr, name) +#define __itt_sync_rename_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_sync_renameA_ptr 0 +#define __itt_sync_renameW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_sync_rename_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + @brief Destroy a synchronization object. + @param addr Handle for the synchronization object. + */ +void ITTAPI __itt_sync_destroy(void *addr); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, sync_destroy, (void *addr)) +#define __itt_sync_destroy ITTNOTIFY_VOID(sync_destroy) +#define __itt_sync_destroy_ptr ITTNOTIFY_NAME(sync_destroy) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_sync_destroy(addr) +#define __itt_sync_destroy_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_sync_destroy_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/*****************************************************************//** + * @name group of functions is used for performance measurement tools + *********************************************************************/ +/** @{ */ +/** + * @brief Enter spin loop on user-defined sync object + */ +void ITTAPI __itt_sync_prepare(void* addr); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, sync_prepare, (void *addr)) +#define __itt_sync_prepare ITTNOTIFY_VOID(sync_prepare) +#define __itt_sync_prepare_ptr ITTNOTIFY_NAME(sync_prepare) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_sync_prepare(addr) +#define __itt_sync_prepare_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_sync_prepare_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Quit spin loop without acquiring spin object + */ +void ITTAPI __itt_sync_cancel(void *addr); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, sync_cancel, (void *addr)) +#define __itt_sync_cancel ITTNOTIFY_VOID(sync_cancel) +#define __itt_sync_cancel_ptr ITTNOTIFY_NAME(sync_cancel) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_sync_cancel(addr) +#define __itt_sync_cancel_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_sync_cancel_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Successful spin loop completion (sync object acquired) + */ +void ITTAPI __itt_sync_acquired(void *addr); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, sync_acquired, (void *addr)) +#define __itt_sync_acquired ITTNOTIFY_VOID(sync_acquired) +#define __itt_sync_acquired_ptr ITTNOTIFY_NAME(sync_acquired) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_sync_acquired(addr) +#define __itt_sync_acquired_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_sync_acquired_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Start sync object releasing code. Is called before the lock release call. + */ +void ITTAPI __itt_sync_releasing(void* addr); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, sync_releasing, (void *addr)) +#define __itt_sync_releasing ITTNOTIFY_VOID(sync_releasing) +#define __itt_sync_releasing_ptr ITTNOTIFY_NAME(sync_releasing) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_sync_releasing(addr) +#define __itt_sync_releasing_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_sync_releasing_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ +/** @} */ + +/** @} sync group */ + +/**************************************************************//** + * @name group of functions is used for correctness checking tools + ******************************************************************/ +/** @{ */ +/** + * @ingroup legacy + * @deprecated Legacy API + * @brief Fast synchronization which does no require spinning. + * - This special function is to be used by TBB and OpenMP libraries only when they know + * there is no spin but they need to suppress TC warnings about shared variable modifications. + * - It only has corresponding pointers in static library and does not have corresponding function + * in dynamic library. + * @see void __itt_sync_prepare(void* addr); + */ +void ITTAPI __itt_fsync_prepare(void* addr); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, fsync_prepare, (void *addr)) +#define __itt_fsync_prepare ITTNOTIFY_VOID(fsync_prepare) +#define __itt_fsync_prepare_ptr ITTNOTIFY_NAME(fsync_prepare) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_fsync_prepare(addr) +#define __itt_fsync_prepare_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_fsync_prepare_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @ingroup legacy + * @deprecated Legacy API + * @brief Fast synchronization which does no require spinning. + * - This special function is to be used by TBB and OpenMP libraries only when they know + * there is no spin but they need to suppress TC warnings about shared variable modifications. + * - It only has corresponding pointers in static library and does not have corresponding function + * in dynamic library. + * @see void __itt_sync_cancel(void *addr); + */ +void ITTAPI __itt_fsync_cancel(void *addr); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, fsync_cancel, (void *addr)) +#define __itt_fsync_cancel ITTNOTIFY_VOID(fsync_cancel) +#define __itt_fsync_cancel_ptr ITTNOTIFY_NAME(fsync_cancel) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_fsync_cancel(addr) +#define __itt_fsync_cancel_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_fsync_cancel_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @ingroup legacy + * @deprecated Legacy API + * @brief Fast synchronization which does no require spinning. + * - This special function is to be used by TBB and OpenMP libraries only when they know + * there is no spin but they need to suppress TC warnings about shared variable modifications. + * - It only has corresponding pointers in static library and does not have corresponding function + * in dynamic library. + * @see void __itt_sync_acquired(void *addr); + */ +void ITTAPI __itt_fsync_acquired(void *addr); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, fsync_acquired, (void *addr)) +#define __itt_fsync_acquired ITTNOTIFY_VOID(fsync_acquired) +#define __itt_fsync_acquired_ptr ITTNOTIFY_NAME(fsync_acquired) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_fsync_acquired(addr) +#define __itt_fsync_acquired_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_fsync_acquired_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @ingroup legacy + * @deprecated Legacy API + * @brief Fast synchronization which does no require spinning. + * - This special function is to be used by TBB and OpenMP libraries only when they know + * there is no spin but they need to suppress TC warnings about shared variable modifications. + * - It only has corresponding pointers in static library and does not have corresponding function + * in dynamic library. + * @see void __itt_sync_releasing(void* addr); + */ +void ITTAPI __itt_fsync_releasing(void* addr); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, fsync_releasing, (void *addr)) +#define __itt_fsync_releasing ITTNOTIFY_VOID(fsync_releasing) +#define __itt_fsync_releasing_ptr ITTNOTIFY_NAME(fsync_releasing) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_fsync_releasing(addr) +#define __itt_fsync_releasing_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_fsync_releasing_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ +/** @} */ + +/** + * @defgroup model Modeling by Intel(R) Parallel Advisor + * @ingroup public + * This is the subset of itt used for modeling by Intel(R) Parallel Advisor. + * This API is called ONLY using annotate.h, by "Annotation" macros + * the user places in their sources during the parallelism modeling steps. + * + * site_begin/end and task_begin/end take the address of handle variables, + * which are writeable by the API. Handles must be 0 initialized prior + * to the first call to begin, or may cause a run-time failure. + * The handles are initialized in a multi-thread safe way by the API if + * the handle is 0. The commonly expected idiom is one static handle to + * identify a site or task. If a site or task of the same name has already + * been started during this collection, the same handle MAY be returned, + * but is not required to be - it is unspecified if data merging is done + * based on name. These routines also take an instance variable. Like + * the lexical instance, these must be 0 initialized. Unlike the lexical + * instance, this is used to track a single dynamic instance. + * + * API used by the Intel(R) Parallel Advisor to describe potential concurrency + * and related activities. User-added source annotations expand to calls + * to these procedures to enable modeling of a hypothetical concurrent + * execution serially. + * @{ + */ +#if !defined(_ADVISOR_ANNOTATE_H_) || defined(ANNOTATE_EXPAND_NULL) + +typedef void* __itt_model_site; /*!< @brief handle for lexical site */ +typedef void* __itt_model_site_instance; /*!< @brief handle for dynamic instance */ +typedef void* __itt_model_task; /*!< @brief handle for lexical site */ +typedef void* __itt_model_task_instance; /*!< @brief handle for dynamic instance */ + +/** + * @enum __itt_model_disable + * @brief Enumerator for the disable methods + */ +typedef enum { + __itt_model_disable_observation, + __itt_model_disable_collection +} __itt_model_disable; + +#endif /* !_ADVISOR_ANNOTATE_H_ || ANNOTATE_EXPAND_NULL */ + +/** + * @brief ANNOTATE_SITE_BEGIN/ANNOTATE_SITE_END support. + * + * site_begin/end model a potential concurrency site. + * site instances may be recursively nested with themselves. + * site_end exits the most recently started but unended site for the current + * thread. The handle passed to end may be used to validate structure. + * Instances of a site encountered on different threads concurrently + * are considered completely distinct. If the site name for two different + * lexical sites match, it is unspecified whether they are treated as the + * same or different for data presentation. + */ +void ITTAPI __itt_model_site_begin(__itt_model_site *site, __itt_model_site_instance *instance, const char *name); +#if ITT_PLATFORM==ITT_PLATFORM_WIN +void ITTAPI __itt_model_site_beginW(const wchar_t *name); +#endif +void ITTAPI __itt_model_site_beginA(const char *name); +void ITTAPI __itt_model_site_beginAL(const char *name, size_t siteNameLen); +void ITTAPI __itt_model_site_end (__itt_model_site *site, __itt_model_site_instance *instance); +void ITTAPI __itt_model_site_end_2(void); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, model_site_begin, (__itt_model_site *site, __itt_model_site_instance *instance, const char *name)) +#if ITT_PLATFORM==ITT_PLATFORM_WIN +ITT_STUBV(ITTAPI, void, model_site_beginW, (const wchar_t *name)) +#endif +ITT_STUBV(ITTAPI, void, model_site_beginA, (const char *name)) +ITT_STUBV(ITTAPI, void, model_site_beginAL, (const char *name, size_t siteNameLen)) +ITT_STUBV(ITTAPI, void, model_site_end, (__itt_model_site *site, __itt_model_site_instance *instance)) +ITT_STUBV(ITTAPI, void, model_site_end_2, (void)) +#define __itt_model_site_begin ITTNOTIFY_VOID(model_site_begin) +#define __itt_model_site_begin_ptr ITTNOTIFY_NAME(model_site_begin) +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_model_site_beginW ITTNOTIFY_VOID(model_site_beginW) +#define __itt_model_site_beginW_ptr ITTNOTIFY_NAME(model_site_beginW) +#endif +#define __itt_model_site_beginA ITTNOTIFY_VOID(model_site_beginA) +#define __itt_model_site_beginA_ptr ITTNOTIFY_NAME(model_site_beginA) +#define __itt_model_site_beginAL ITTNOTIFY_VOID(model_site_beginAL) +#define __itt_model_site_beginAL_ptr ITTNOTIFY_NAME(model_site_beginAL) +#define __itt_model_site_end ITTNOTIFY_VOID(model_site_end) +#define __itt_model_site_end_ptr ITTNOTIFY_NAME(model_site_end) +#define __itt_model_site_end_2 ITTNOTIFY_VOID(model_site_end_2) +#define __itt_model_site_end_2_ptr ITTNOTIFY_NAME(model_site_end_2) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_model_site_begin(site, instance, name) +#define __itt_model_site_begin_ptr 0 +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_model_site_beginW(name) +#define __itt_model_site_beginW_ptr 0 +#endif +#define __itt_model_site_beginA(name) +#define __itt_model_site_beginA_ptr 0 +#define __itt_model_site_beginAL(name, siteNameLen) +#define __itt_model_site_beginAL_ptr 0 +#define __itt_model_site_end(site, instance) +#define __itt_model_site_end_ptr 0 +#define __itt_model_site_end_2() +#define __itt_model_site_end_2_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_model_site_begin_ptr 0 +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_model_site_beginW_ptr 0 +#endif +#define __itt_model_site_beginA_ptr 0 +#define __itt_model_site_beginAL_ptr 0 +#define __itt_model_site_end_ptr 0 +#define __itt_model_site_end_2_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief ANNOTATE_TASK_BEGIN/ANNOTATE_TASK_END support + * + * task_begin/end model a potential task, which is contained within the most + * closely enclosing dynamic site. task_end exits the most recently started + * but unended task. The handle passed to end may be used to validate + * structure. It is unspecified if bad dynamic nesting is detected. If it + * is, it should be encoded in the resulting data collection. The collector + * should not fail due to construct nesting issues, nor attempt to directly + * indicate the problem. + */ +void ITTAPI __itt_model_task_begin(__itt_model_task *task, __itt_model_task_instance *instance, const char *name); +#if ITT_PLATFORM==ITT_PLATFORM_WIN +void ITTAPI __itt_model_task_beginW(const wchar_t *name); +void ITTAPI __itt_model_iteration_taskW(const wchar_t *name); +#endif +void ITTAPI __itt_model_task_beginA(const char *name); +void ITTAPI __itt_model_task_beginAL(const char *name, size_t taskNameLen); +void ITTAPI __itt_model_iteration_taskA(const char *name); +void ITTAPI __itt_model_iteration_taskAL(const char *name, size_t taskNameLen); +void ITTAPI __itt_model_task_end (__itt_model_task *task, __itt_model_task_instance *instance); +void ITTAPI __itt_model_task_end_2(void); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, model_task_begin, (__itt_model_task *task, __itt_model_task_instance *instance, const char *name)) +#if ITT_PLATFORM==ITT_PLATFORM_WIN +ITT_STUBV(ITTAPI, void, model_task_beginW, (const wchar_t *name)) +ITT_STUBV(ITTAPI, void, model_iteration_taskW, (const wchar_t *name)) +#endif +ITT_STUBV(ITTAPI, void, model_task_beginA, (const char *name)) +ITT_STUBV(ITTAPI, void, model_task_beginAL, (const char *name, size_t taskNameLen)) +ITT_STUBV(ITTAPI, void, model_iteration_taskA, (const char *name)) +ITT_STUBV(ITTAPI, void, model_iteration_taskAL, (const char *name, size_t taskNameLen)) +ITT_STUBV(ITTAPI, void, model_task_end, (__itt_model_task *task, __itt_model_task_instance *instance)) +ITT_STUBV(ITTAPI, void, model_task_end_2, (void)) +#define __itt_model_task_begin ITTNOTIFY_VOID(model_task_begin) +#define __itt_model_task_begin_ptr ITTNOTIFY_NAME(model_task_begin) +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_model_task_beginW ITTNOTIFY_VOID(model_task_beginW) +#define __itt_model_task_beginW_ptr ITTNOTIFY_NAME(model_task_beginW) +#define __itt_model_iteration_taskW ITTNOTIFY_VOID(model_iteration_taskW) +#define __itt_model_iteration_taskW_ptr ITTNOTIFY_NAME(model_iteration_taskW) +#endif +#define __itt_model_task_beginA ITTNOTIFY_VOID(model_task_beginA) +#define __itt_model_task_beginA_ptr ITTNOTIFY_NAME(model_task_beginA) +#define __itt_model_task_beginAL ITTNOTIFY_VOID(model_task_beginAL) +#define __itt_model_task_beginAL_ptr ITTNOTIFY_NAME(model_task_beginAL) +#define __itt_model_iteration_taskA ITTNOTIFY_VOID(model_iteration_taskA) +#define __itt_model_iteration_taskA_ptr ITTNOTIFY_NAME(model_iteration_taskA) +#define __itt_model_iteration_taskAL ITTNOTIFY_VOID(model_iteration_taskAL) +#define __itt_model_iteration_taskAL_ptr ITTNOTIFY_NAME(model_iteration_taskAL) +#define __itt_model_task_end ITTNOTIFY_VOID(model_task_end) +#define __itt_model_task_end_ptr ITTNOTIFY_NAME(model_task_end) +#define __itt_model_task_end_2 ITTNOTIFY_VOID(model_task_end_2) +#define __itt_model_task_end_2_ptr ITTNOTIFY_NAME(model_task_end_2) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_model_task_begin(task, instance, name) +#define __itt_model_task_begin_ptr 0 +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_model_task_beginW(name) +#define __itt_model_task_beginW_ptr 0 +#endif +#define __itt_model_task_beginA(name) +#define __itt_model_task_beginA_ptr 0 +#define __itt_model_task_beginAL(name, siteNameLen) +#define __itt_model_task_beginAL_ptr 0 +#define __itt_model_iteration_taskA(name) +#define __itt_model_iteration_taskA_ptr 0 +#define __itt_model_iteration_taskAL(name, siteNameLen) +#define __itt_model_iteration_taskAL_ptr 0 +#define __itt_model_task_end(task, instance) +#define __itt_model_task_end_ptr 0 +#define __itt_model_task_end_2() +#define __itt_model_task_end_2_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_model_task_begin_ptr 0 +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_model_task_beginW_ptr 0 +#endif +#define __itt_model_task_beginA_ptr 0 +#define __itt_model_task_beginAL_ptr 0 +#define __itt_model_iteration_taskA_ptr 0 +#define __itt_model_iteration_taskAL_ptr 0 +#define __itt_model_task_end_ptr 0 +#define __itt_model_task_end_2_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief ANNOTATE_LOCK_ACQUIRE/ANNOTATE_LOCK_RELEASE support + * + * lock_acquire/release model a potential lock for both lockset and + * performance modeling. Each unique address is modeled as a separate + * lock, with invalid addresses being valid lock IDs. Specifically: + * no storage is accessed by the API at the specified address - it is only + * used for lock identification. Lock acquires may be self-nested and are + * unlocked by a corresponding number of releases. + * (These closely correspond to __itt_sync_acquired/__itt_sync_releasing, + * but may not have identical semantics.) + */ +void ITTAPI __itt_model_lock_acquire(void *lock); +void ITTAPI __itt_model_lock_acquire_2(void *lock); +void ITTAPI __itt_model_lock_release(void *lock); +void ITTAPI __itt_model_lock_release_2(void *lock); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, model_lock_acquire, (void *lock)) +ITT_STUBV(ITTAPI, void, model_lock_acquire_2, (void *lock)) +ITT_STUBV(ITTAPI, void, model_lock_release, (void *lock)) +ITT_STUBV(ITTAPI, void, model_lock_release_2, (void *lock)) +#define __itt_model_lock_acquire ITTNOTIFY_VOID(model_lock_acquire) +#define __itt_model_lock_acquire_ptr ITTNOTIFY_NAME(model_lock_acquire) +#define __itt_model_lock_acquire_2 ITTNOTIFY_VOID(model_lock_acquire_2) +#define __itt_model_lock_acquire_2_ptr ITTNOTIFY_NAME(model_lock_acquire_2) +#define __itt_model_lock_release ITTNOTIFY_VOID(model_lock_release) +#define __itt_model_lock_release_ptr ITTNOTIFY_NAME(model_lock_release) +#define __itt_model_lock_release_2 ITTNOTIFY_VOID(model_lock_release_2) +#define __itt_model_lock_release_2_ptr ITTNOTIFY_NAME(model_lock_release_2) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_model_lock_acquire(lock) +#define __itt_model_lock_acquire_ptr 0 +#define __itt_model_lock_acquire_2(lock) +#define __itt_model_lock_acquire_2_ptr 0 +#define __itt_model_lock_release(lock) +#define __itt_model_lock_release_ptr 0 +#define __itt_model_lock_release_2(lock) +#define __itt_model_lock_release_2_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_model_lock_acquire_ptr 0 +#define __itt_model_lock_acquire_2_ptr 0 +#define __itt_model_lock_release_ptr 0 +#define __itt_model_lock_release_2_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief ANNOTATE_RECORD_ALLOCATION/ANNOTATE_RECORD_DEALLOCATION support + * + * record_allocation/deallocation describe user-defined memory allocator + * behavior, which may be required for correctness modeling to understand + * when storage is not expected to be actually reused across threads. + */ +void ITTAPI __itt_model_record_allocation (void *addr, size_t size); +void ITTAPI __itt_model_record_deallocation(void *addr); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, model_record_allocation, (void *addr, size_t size)) +ITT_STUBV(ITTAPI, void, model_record_deallocation, (void *addr)) +#define __itt_model_record_allocation ITTNOTIFY_VOID(model_record_allocation) +#define __itt_model_record_allocation_ptr ITTNOTIFY_NAME(model_record_allocation) +#define __itt_model_record_deallocation ITTNOTIFY_VOID(model_record_deallocation) +#define __itt_model_record_deallocation_ptr ITTNOTIFY_NAME(model_record_deallocation) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_model_record_allocation(addr, size) +#define __itt_model_record_allocation_ptr 0 +#define __itt_model_record_deallocation(addr) +#define __itt_model_record_deallocation_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_model_record_allocation_ptr 0 +#define __itt_model_record_deallocation_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief ANNOTATE_INDUCTION_USES support + * + * Note particular storage is inductive through the end of the current site + */ +void ITTAPI __itt_model_induction_uses(void* addr, size_t size); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, model_induction_uses, (void *addr, size_t size)) +#define __itt_model_induction_uses ITTNOTIFY_VOID(model_induction_uses) +#define __itt_model_induction_uses_ptr ITTNOTIFY_NAME(model_induction_uses) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_model_induction_uses(addr, size) +#define __itt_model_induction_uses_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_model_induction_uses_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief ANNOTATE_REDUCTION_USES support + * + * Note particular storage is used for reduction through the end + * of the current site + */ +void ITTAPI __itt_model_reduction_uses(void* addr, size_t size); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, model_reduction_uses, (void *addr, size_t size)) +#define __itt_model_reduction_uses ITTNOTIFY_VOID(model_reduction_uses) +#define __itt_model_reduction_uses_ptr ITTNOTIFY_NAME(model_reduction_uses) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_model_reduction_uses(addr, size) +#define __itt_model_reduction_uses_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_model_reduction_uses_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief ANNOTATE_OBSERVE_USES support + * + * Have correctness modeling record observations about uses of storage + * through the end of the current site + */ +void ITTAPI __itt_model_observe_uses(void* addr, size_t size); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, model_observe_uses, (void *addr, size_t size)) +#define __itt_model_observe_uses ITTNOTIFY_VOID(model_observe_uses) +#define __itt_model_observe_uses_ptr ITTNOTIFY_NAME(model_observe_uses) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_model_observe_uses(addr, size) +#define __itt_model_observe_uses_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_model_observe_uses_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief ANNOTATE_CLEAR_USES support + * + * Clear the special handling of a piece of storage related to induction, + * reduction or observe_uses + */ +void ITTAPI __itt_model_clear_uses(void* addr); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, model_clear_uses, (void *addr)) +#define __itt_model_clear_uses ITTNOTIFY_VOID(model_clear_uses) +#define __itt_model_clear_uses_ptr ITTNOTIFY_NAME(model_clear_uses) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_model_clear_uses(addr) +#define __itt_model_clear_uses_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_model_clear_uses_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief ANNOTATE_DISABLE_*_PUSH/ANNOTATE_DISABLE_*_POP support + * + * disable_push/disable_pop push and pop disabling based on a parameter. + * Disabling observations stops processing of memory references during + * correctness modeling, and all annotations that occur in the disabled + * region. This allows description of code that is expected to be handled + * specially during conversion to parallelism or that is not recognized + * by tools (e.g. some kinds of synchronization operations.) + * This mechanism causes all annotations in the disabled region, other + * than disable_push and disable_pop, to be ignored. (For example, this + * might validly be used to disable an entire parallel site and the contained + * tasks and locking in it for data collection purposes.) + * The disable for collection is a more expensive operation, but reduces + * collector overhead significantly. This applies to BOTH correctness data + * collection and performance data collection. For example, a site + * containing a task might only enable data collection for the first 10 + * iterations. Both performance and correctness data should reflect this, + * and the program should run as close to full speed as possible when + * collection is disabled. + */ +void ITTAPI __itt_model_disable_push(__itt_model_disable x); +void ITTAPI __itt_model_disable_pop(void); +void ITTAPI __itt_model_aggregate_task(size_t x); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, model_disable_push, (__itt_model_disable x)) +ITT_STUBV(ITTAPI, void, model_disable_pop, (void)) +ITT_STUBV(ITTAPI, void, model_aggregate_task, (size_t x)) +#define __itt_model_disable_push ITTNOTIFY_VOID(model_disable_push) +#define __itt_model_disable_push_ptr ITTNOTIFY_NAME(model_disable_push) +#define __itt_model_disable_pop ITTNOTIFY_VOID(model_disable_pop) +#define __itt_model_disable_pop_ptr ITTNOTIFY_NAME(model_disable_pop) +#define __itt_model_aggregate_task ITTNOTIFY_VOID(model_aggregate_task) +#define __itt_model_aggregate_task_ptr ITTNOTIFY_NAME(model_aggregate_task) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_model_disable_push(x) +#define __itt_model_disable_push_ptr 0 +#define __itt_model_disable_pop() +#define __itt_model_disable_pop_ptr 0 +#define __itt_model_aggregate_task(x) +#define __itt_model_aggregate_task_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_model_disable_push_ptr 0 +#define __itt_model_disable_pop_ptr 0 +#define __itt_model_aggregate_task_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ +/** @} model group */ + +/** + * @defgroup heap Heap + * @ingroup public + * Heap group + * @{ + */ + +typedef void* __itt_heap_function; + +/** + * @brief Create an identification for heap function + * @return non-zero identifier or NULL + */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +__itt_heap_function ITTAPI __itt_heap_function_createA(const char* name, const char* domain); +__itt_heap_function ITTAPI __itt_heap_function_createW(const wchar_t* name, const wchar_t* domain); +#if defined(UNICODE) || defined(_UNICODE) +# define __itt_heap_function_create __itt_heap_function_createW +# define __itt_heap_function_create_ptr __itt_heap_function_createW_ptr +#else +# define __itt_heap_function_create __itt_heap_function_createA +# define __itt_heap_function_create_ptr __itt_heap_function_createA_ptr +#endif /* UNICODE */ +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +__itt_heap_function ITTAPI __itt_heap_function_create(const char* name, const char* domain); +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +#if ITT_PLATFORM==ITT_PLATFORM_WIN +ITT_STUB(ITTAPI, __itt_heap_function, heap_function_createA, (const char* name, const char* domain)) +ITT_STUB(ITTAPI, __itt_heap_function, heap_function_createW, (const wchar_t* name, const wchar_t* domain)) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +ITT_STUB(ITTAPI, __itt_heap_function, heap_function_create, (const char* name, const char* domain)) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_heap_function_createA ITTNOTIFY_DATA(heap_function_createA) +#define __itt_heap_function_createA_ptr ITTNOTIFY_NAME(heap_function_createA) +#define __itt_heap_function_createW ITTNOTIFY_DATA(heap_function_createW) +#define __itt_heap_function_createW_ptr ITTNOTIFY_NAME(heap_function_createW) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_heap_function_create ITTNOTIFY_DATA(heap_function_create) +#define __itt_heap_function_create_ptr ITTNOTIFY_NAME(heap_function_create) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#else /* INTEL_NO_ITTNOTIFY_API */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_heap_function_createA(name, domain) (__itt_heap_function)0 +#define __itt_heap_function_createA_ptr 0 +#define __itt_heap_function_createW(name, domain) (__itt_heap_function)0 +#define __itt_heap_function_createW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_heap_function_create(name, domain) (__itt_heap_function)0 +#define __itt_heap_function_create_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_heap_function_createA_ptr 0 +#define __itt_heap_function_createW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_heap_function_create_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Record an allocation begin occurrence. + */ +void ITTAPI __itt_heap_allocate_begin(__itt_heap_function h, size_t size, int initialized); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, heap_allocate_begin, (__itt_heap_function h, size_t size, int initialized)) +#define __itt_heap_allocate_begin ITTNOTIFY_VOID(heap_allocate_begin) +#define __itt_heap_allocate_begin_ptr ITTNOTIFY_NAME(heap_allocate_begin) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_heap_allocate_begin(h, size, initialized) +#define __itt_heap_allocate_begin_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_heap_allocate_begin_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Record an allocation end occurrence. + */ +void ITTAPI __itt_heap_allocate_end(__itt_heap_function h, void** addr, size_t size, int initialized); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, heap_allocate_end, (__itt_heap_function h, void** addr, size_t size, int initialized)) +#define __itt_heap_allocate_end ITTNOTIFY_VOID(heap_allocate_end) +#define __itt_heap_allocate_end_ptr ITTNOTIFY_NAME(heap_allocate_end) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_heap_allocate_end(h, addr, size, initialized) +#define __itt_heap_allocate_end_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_heap_allocate_end_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Record an free begin occurrence. + */ +void ITTAPI __itt_heap_free_begin(__itt_heap_function h, void* addr); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, heap_free_begin, (__itt_heap_function h, void* addr)) +#define __itt_heap_free_begin ITTNOTIFY_VOID(heap_free_begin) +#define __itt_heap_free_begin_ptr ITTNOTIFY_NAME(heap_free_begin) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_heap_free_begin(h, addr) +#define __itt_heap_free_begin_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_heap_free_begin_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Record an free end occurrence. + */ +void ITTAPI __itt_heap_free_end(__itt_heap_function h, void* addr); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, heap_free_end, (__itt_heap_function h, void* addr)) +#define __itt_heap_free_end ITTNOTIFY_VOID(heap_free_end) +#define __itt_heap_free_end_ptr ITTNOTIFY_NAME(heap_free_end) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_heap_free_end(h, addr) +#define __itt_heap_free_end_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_heap_free_end_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Record an reallocation begin occurrence. + */ +void ITTAPI __itt_heap_reallocate_begin(__itt_heap_function h, void* addr, size_t new_size, int initialized); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, heap_reallocate_begin, (__itt_heap_function h, void* addr, size_t new_size, int initialized)) +#define __itt_heap_reallocate_begin ITTNOTIFY_VOID(heap_reallocate_begin) +#define __itt_heap_reallocate_begin_ptr ITTNOTIFY_NAME(heap_reallocate_begin) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_heap_reallocate_begin(h, addr, new_size, initialized) +#define __itt_heap_reallocate_begin_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_heap_reallocate_begin_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Record an reallocation end occurrence. + */ +void ITTAPI __itt_heap_reallocate_end(__itt_heap_function h, void* addr, void** new_addr, size_t new_size, int initialized); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, heap_reallocate_end, (__itt_heap_function h, void* addr, void** new_addr, size_t new_size, int initialized)) +#define __itt_heap_reallocate_end ITTNOTIFY_VOID(heap_reallocate_end) +#define __itt_heap_reallocate_end_ptr ITTNOTIFY_NAME(heap_reallocate_end) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_heap_reallocate_end(h, addr, new_addr, new_size, initialized) +#define __itt_heap_reallocate_end_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_heap_reallocate_end_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** @brief internal access begin */ +void ITTAPI __itt_heap_internal_access_begin(void); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, heap_internal_access_begin, (void)) +#define __itt_heap_internal_access_begin ITTNOTIFY_VOID(heap_internal_access_begin) +#define __itt_heap_internal_access_begin_ptr ITTNOTIFY_NAME(heap_internal_access_begin) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_heap_internal_access_begin() +#define __itt_heap_internal_access_begin_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_heap_internal_access_begin_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** @brief internal access end */ +void ITTAPI __itt_heap_internal_access_end(void); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, heap_internal_access_end, (void)) +#define __itt_heap_internal_access_end ITTNOTIFY_VOID(heap_internal_access_end) +#define __itt_heap_internal_access_end_ptr ITTNOTIFY_NAME(heap_internal_access_end) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_heap_internal_access_end() +#define __itt_heap_internal_access_end_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_heap_internal_access_end_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** @brief record memory growth begin */ +void ITTAPI __itt_heap_record_memory_growth_begin(void); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, heap_record_memory_growth_begin, (void)) +#define __itt_heap_record_memory_growth_begin ITTNOTIFY_VOID(heap_record_memory_growth_begin) +#define __itt_heap_record_memory_growth_begin_ptr ITTNOTIFY_NAME(heap_record_memory_growth_begin) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_heap_record_memory_growth_begin() +#define __itt_heap_record_memory_growth_begin_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_heap_record_memory_growth_begin_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** @brief record memory growth end */ +void ITTAPI __itt_heap_record_memory_growth_end(void); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, heap_record_memory_growth_end, (void)) +#define __itt_heap_record_memory_growth_end ITTNOTIFY_VOID(heap_record_memory_growth_end) +#define __itt_heap_record_memory_growth_end_ptr ITTNOTIFY_NAME(heap_record_memory_growth_end) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_heap_record_memory_growth_end() +#define __itt_heap_record_memory_growth_end_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_heap_record_memory_growth_end_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Specify the type of heap detection/reporting to modify. + */ +/** + * @hideinitializer + * @brief Report on memory leaks. + */ +#define __itt_heap_leaks 0x00000001 + +/** + * @hideinitializer + * @brief Report on memory growth. + */ +#define __itt_heap_growth 0x00000002 + + +/** @brief heap reset detection */ +void ITTAPI __itt_heap_reset_detection(unsigned int reset_mask); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, heap_reset_detection, (unsigned int reset_mask)) +#define __itt_heap_reset_detection ITTNOTIFY_VOID(heap_reset_detection) +#define __itt_heap_reset_detection_ptr ITTNOTIFY_NAME(heap_reset_detection) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_heap_reset_detection() +#define __itt_heap_reset_detection_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_heap_reset_detection_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** @brief report */ +void ITTAPI __itt_heap_record(unsigned int record_mask); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, heap_record, (unsigned int record_mask)) +#define __itt_heap_record ITTNOTIFY_VOID(heap_record) +#define __itt_heap_record_ptr ITTNOTIFY_NAME(heap_record) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_heap_record() +#define __itt_heap_record_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_heap_record_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** @} heap group */ +/** @endcond */ +/* ========================================================================== */ + +/** + * @defgroup domains Domains + * @ingroup public + * Domains group + * @{ + */ + +/** @cond exclude_from_documentation */ +#pragma pack(push, 8) + +typedef struct ___itt_domain +{ + volatile int flags; /*!< Zero if disabled, non-zero if enabled. The meaning of different non-zero values is reserved to the runtime */ + const char* nameA; /*!< Copy of original name in ASCII. */ +#if defined(UNICODE) || defined(_UNICODE) + const wchar_t* nameW; /*!< Copy of original name in UNICODE. */ +#else /* UNICODE || _UNICODE */ + void* nameW; +#endif /* UNICODE || _UNICODE */ + int extra1; /*!< Reserved to the runtime */ + void* extra2; /*!< Reserved to the runtime */ + struct ___itt_domain* next; +} __itt_domain; + +#pragma pack(pop) +/** @endcond */ + +/** + * @ingroup domains + * @brief Create a domain. + * Create domain using some domain name: the URI naming style is recommended. + * Because the set of domains is expected to be static over the application's + * execution time, there is no mechanism to destroy a domain. + * Any domain can be accessed by any thread in the process, regardless of + * which thread created the domain. This call is thread-safe. + * @param[in] name name of domain + */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +__itt_domain* ITTAPI __itt_domain_createA(const char *name); +__itt_domain* ITTAPI __itt_domain_createW(const wchar_t *name); +#if defined(UNICODE) || defined(_UNICODE) +# define __itt_domain_create __itt_domain_createW +# define __itt_domain_create_ptr __itt_domain_createW_ptr +#else /* UNICODE */ +# define __itt_domain_create __itt_domain_createA +# define __itt_domain_create_ptr __itt_domain_createA_ptr +#endif /* UNICODE */ +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +__itt_domain* ITTAPI __itt_domain_create(const char *name); +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +#if ITT_PLATFORM==ITT_PLATFORM_WIN +ITT_STUB(ITTAPI, __itt_domain*, domain_createA, (const char *name)) +ITT_STUB(ITTAPI, __itt_domain*, domain_createW, (const wchar_t *name)) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +ITT_STUB(ITTAPI, __itt_domain*, domain_create, (const char *name)) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_domain_createA ITTNOTIFY_DATA(domain_createA) +#define __itt_domain_createA_ptr ITTNOTIFY_NAME(domain_createA) +#define __itt_domain_createW ITTNOTIFY_DATA(domain_createW) +#define __itt_domain_createW_ptr ITTNOTIFY_NAME(domain_createW) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_domain_create ITTNOTIFY_DATA(domain_create) +#define __itt_domain_create_ptr ITTNOTIFY_NAME(domain_create) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#else /* INTEL_NO_ITTNOTIFY_API */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_domain_createA(name) (__itt_domain*)0 +#define __itt_domain_createA_ptr 0 +#define __itt_domain_createW(name) (__itt_domain*)0 +#define __itt_domain_createW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_domain_create(name) (__itt_domain*)0 +#define __itt_domain_create_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_domain_createA_ptr 0 +#define __itt_domain_createW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_domain_create_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ +/** @} domains group */ + +/** + * @defgroup ids IDs + * @ingroup public + * IDs group + * @{ + */ + +/** @cond exclude_from_documentation */ +#pragma pack(push, 8) + +typedef struct ___itt_id +{ + unsigned long long d1, d2, d3; +} __itt_id; + +#pragma pack(pop) +/** @endcond */ + +const __itt_id __itt_null = { 0, 0, 0 }; + +/** + * @ingroup ids + * @brief A convenience function is provided to create an ID without domain control. + * @brief This is a convenience function to initialize an __itt_id structure. This function + * does not affect the collector runtime in any way. After you make the ID with this + * function, you still must create it with the __itt_id_create function before using the ID + * to identify a named entity. + * @param[in] addr The address of object; high QWORD of the ID value. + * @param[in] extra The extra data to unique identify object; low QWORD of the ID value. + */ + +ITT_INLINE __itt_id ITTAPI __itt_id_make(void* addr, unsigned long long extra) ITT_INLINE_ATTRIBUTE; +ITT_INLINE __itt_id ITTAPI __itt_id_make(void* addr, unsigned long long extra) +{ + __itt_id id = __itt_null; + id.d1 = (unsigned long long)((uintptr_t)addr); + id.d2 = (unsigned long long)extra; + id.d3 = (unsigned long long)0; /* Reserved. Must be zero */ + return id; +} + +/** + * @ingroup ids + * @brief Create an instance of identifier. + * This establishes the beginning of the lifetime of an instance of + * the given ID in the trace. Once this lifetime starts, the ID + * can be used to tag named entity instances in calls such as + * __itt_task_begin, and to specify relationships among + * identified named entity instances, using the \ref relations APIs. + * Instance IDs are not domain specific! + * @param[in] domain The domain controlling the execution of this call. + * @param[in] id The ID to create. + */ +void ITTAPI __itt_id_create(const __itt_domain *domain, __itt_id id); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, id_create, (const __itt_domain *domain, __itt_id id)) +#define __itt_id_create(d,x) ITTNOTIFY_VOID_D1(id_create,d,x) +#define __itt_id_create_ptr ITTNOTIFY_NAME(id_create) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_id_create(domain,id) +#define __itt_id_create_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_id_create_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @ingroup ids + * @brief Destroy an instance of identifier. + * This ends the lifetime of the current instance of the given ID value in the trace. + * Any relationships that are established after this lifetime ends are invalid. + * This call must be performed before the given ID value can be reused for a different + * named entity instance. + * @param[in] domain The domain controlling the execution of this call. + * @param[in] id The ID to destroy. + */ +void ITTAPI __itt_id_destroy(const __itt_domain *domain, __itt_id id); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, id_destroy, (const __itt_domain *domain, __itt_id id)) +#define __itt_id_destroy(d,x) ITTNOTIFY_VOID_D1(id_destroy,d,x) +#define __itt_id_destroy_ptr ITTNOTIFY_NAME(id_destroy) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_id_destroy(domain,id) +#define __itt_id_destroy_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_id_destroy_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ +/** @} ids group */ + +/** + * @defgroup handless String Handles + * @ingroup public + * String Handles group + * @{ + */ + +/** @cond exclude_from_documentation */ +#pragma pack(push, 8) + +typedef struct ___itt_string_handle +{ + const char* strA; /*!< Copy of original string in ASCII. */ +#if defined(UNICODE) || defined(_UNICODE) + const wchar_t* strW; /*!< Copy of original string in UNICODE. */ +#else /* UNICODE || _UNICODE */ + void* strW; +#endif /* UNICODE || _UNICODE */ + int extra1; /*!< Reserved. Must be zero */ + void* extra2; /*!< Reserved. Must be zero */ + struct ___itt_string_handle* next; +} __itt_string_handle; + +#pragma pack(pop) +/** @endcond */ + +/** + * @ingroup handles + * @brief Create a string handle. + * Create and return handle value that can be associated with a string. + * Consecutive calls to __itt_string_handle_create with the same name + * return the same value. Because the set of string handles is expected to remain + * static during the application's execution time, there is no mechanism to destroy a string handle. + * Any string handle can be accessed by any thread in the process, regardless of which thread created + * the string handle. This call is thread-safe. + * @param[in] name The input string + */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +__itt_string_handle* ITTAPI __itt_string_handle_createA(const char *name); +__itt_string_handle* ITTAPI __itt_string_handle_createW(const wchar_t *name); +#if defined(UNICODE) || defined(_UNICODE) +# define __itt_string_handle_create __itt_string_handle_createW +# define __itt_string_handle_create_ptr __itt_string_handle_createW_ptr +#else /* UNICODE */ +# define __itt_string_handle_create __itt_string_handle_createA +# define __itt_string_handle_create_ptr __itt_string_handle_createA_ptr +#endif /* UNICODE */ +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +__itt_string_handle* ITTAPI __itt_string_handle_create(const char *name); +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +#if ITT_PLATFORM==ITT_PLATFORM_WIN +ITT_STUB(ITTAPI, __itt_string_handle*, string_handle_createA, (const char *name)) +ITT_STUB(ITTAPI, __itt_string_handle*, string_handle_createW, (const wchar_t *name)) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +ITT_STUB(ITTAPI, __itt_string_handle*, string_handle_create, (const char *name)) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_string_handle_createA ITTNOTIFY_DATA(string_handle_createA) +#define __itt_string_handle_createA_ptr ITTNOTIFY_NAME(string_handle_createA) +#define __itt_string_handle_createW ITTNOTIFY_DATA(string_handle_createW) +#define __itt_string_handle_createW_ptr ITTNOTIFY_NAME(string_handle_createW) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_string_handle_create ITTNOTIFY_DATA(string_handle_create) +#define __itt_string_handle_create_ptr ITTNOTIFY_NAME(string_handle_create) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#else /* INTEL_NO_ITTNOTIFY_API */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_string_handle_createA(name) (__itt_string_handle*)0 +#define __itt_string_handle_createA_ptr 0 +#define __itt_string_handle_createW(name) (__itt_string_handle*)0 +#define __itt_string_handle_createW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_string_handle_create(name) (__itt_string_handle*)0 +#define __itt_string_handle_create_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_string_handle_createA_ptr 0 +#define __itt_string_handle_createW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_string_handle_create_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ +/** @} handles group */ + +/** @cond exclude_from_documentation */ +typedef unsigned long long __itt_timestamp; +/** @endcond */ + +#define __itt_timestamp_none ((__itt_timestamp)-1LL) + +/** @cond exclude_from_gpa_documentation */ + +/** + * @ingroup timestamps + * @brief Return timestamp corresponding to the current moment. + * This returns the timestamp in the format that is the most relevant for the current + * host or platform (RDTSC, QPC, and others). You can use the "<" operator to + * compare __itt_timestamp values. + */ +__itt_timestamp ITTAPI __itt_get_timestamp(void); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUB(ITTAPI, __itt_timestamp, get_timestamp, (void)) +#define __itt_get_timestamp ITTNOTIFY_DATA(get_timestamp) +#define __itt_get_timestamp_ptr ITTNOTIFY_NAME(get_timestamp) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_get_timestamp() +#define __itt_get_timestamp_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_get_timestamp_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ +/** @} timestamps */ +/** @endcond */ + +/** @cond exclude_from_gpa_documentation */ + +/** + * @defgroup regions Regions + * @ingroup public + * Regions group + * @{ + */ +/** + * @ingroup regions + * @brief Begin of region instance. + * Successive calls to __itt_region_begin with the same ID are ignored + * until a call to __itt_region_end with the same ID + * @param[in] domain The domain for this region instance + * @param[in] id The instance ID for this region instance. Must not be __itt_null + * @param[in] parentid The instance ID for the parent of this region instance, or __itt_null + * @param[in] name The name of this region + */ +void ITTAPI __itt_region_begin(const __itt_domain *domain, __itt_id id, __itt_id parentid, __itt_string_handle *name); + +/** + * @ingroup regions + * @brief End of region instance. + * The first call to __itt_region_end with a given ID ends the + * region. Successive calls with the same ID are ignored, as are + * calls that do not have a matching __itt_region_begin call. + * @param[in] domain The domain for this region instance + * @param[in] id The instance ID for this region instance + */ +void ITTAPI __itt_region_end(const __itt_domain *domain, __itt_id id); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, region_begin, (const __itt_domain *domain, __itt_id id, __itt_id parentid, __itt_string_handle *name)) +ITT_STUBV(ITTAPI, void, region_end, (const __itt_domain *domain, __itt_id id)) +#define __itt_region_begin(d,x,y,z) ITTNOTIFY_VOID_D3(region_begin,d,x,y,z) +#define __itt_region_begin_ptr ITTNOTIFY_NAME(region_begin) +#define __itt_region_end(d,x) ITTNOTIFY_VOID_D1(region_end,d,x) +#define __itt_region_end_ptr ITTNOTIFY_NAME(region_end) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_region_begin(d,x,y,z) +#define __itt_region_begin_ptr 0 +#define __itt_region_end(d,x) +#define __itt_region_end_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_region_begin_ptr 0 +#define __itt_region_end_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ +/** @} regions group */ + +/** + * @defgroup frames Frames + * @ingroup public + * Frames are similar to regions, but are intended to be easier to use and to implement. + * In particular: + * - Frames always represent periods of elapsed time + * - By default, frames have no nesting relationships + * @{ + */ + +/** + * @ingroup frames + * @brief Begin a frame instance. + * Successive calls to __itt_frame_begin with the + * same ID are ignored until a call to __itt_frame_end with the same ID. + * @param[in] domain The domain for this frame instance + * @param[in] id The instance ID for this frame instance or NULL + */ +void ITTAPI __itt_frame_begin_v3(const __itt_domain *domain, __itt_id *id); + +/** + * @ingroup frames + * @brief End a frame instance. + * The first call to __itt_frame_end with a given ID + * ends the frame. Successive calls with the same ID are ignored, as are + * calls that do not have a matching __itt_frame_begin call. + * @param[in] domain The domain for this frame instance + * @param[in] id The instance ID for this frame instance or NULL for current + */ +void ITTAPI __itt_frame_end_v3(const __itt_domain *domain, __itt_id *id); + +/** + * @ingroup frames + * @brief Submits a frame instance. + * Successive calls to __itt_frame_begin or __itt_frame_submit with the + * same ID are ignored until a call to __itt_frame_end or __itt_frame_submit + * with the same ID. + * Passing special __itt_timestamp_none value as "end" argument means + * take the current timestamp as the end timestamp. + * @param[in] domain The domain for this frame instance + * @param[in] id The instance ID for this frame instance or NULL + * @param[in] begin Timestamp of the beginning of the frame + * @param[in] end Timestamp of the end of the frame + */ +void ITTAPI __itt_frame_submit_v3(const __itt_domain *domain, __itt_id *id, + __itt_timestamp begin, __itt_timestamp end); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, frame_begin_v3, (const __itt_domain *domain, __itt_id *id)) +ITT_STUBV(ITTAPI, void, frame_end_v3, (const __itt_domain *domain, __itt_id *id)) +ITT_STUBV(ITTAPI, void, frame_submit_v3, (const __itt_domain *domain, __itt_id *id, __itt_timestamp begin, __itt_timestamp end)) +#define __itt_frame_begin_v3(d,x) ITTNOTIFY_VOID_D1(frame_begin_v3,d,x) +#define __itt_frame_begin_v3_ptr ITTNOTIFY_NAME(frame_begin_v3) +#define __itt_frame_end_v3(d,x) ITTNOTIFY_VOID_D1(frame_end_v3,d,x) +#define __itt_frame_end_v3_ptr ITTNOTIFY_NAME(frame_end_v3) +#define __itt_frame_submit_v3(d,x,b,e) ITTNOTIFY_VOID_D3(frame_submit_v3,d,x,b,e) +#define __itt_frame_submit_v3_ptr ITTNOTIFY_NAME(frame_submit_v3) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_frame_begin_v3(domain,id) +#define __itt_frame_begin_v3_ptr 0 +#define __itt_frame_end_v3(domain,id) +#define __itt_frame_end_v3_ptr 0 +#define __itt_frame_submit_v3(domain,id,begin,end) +#define __itt_frame_submit_v3_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_frame_begin_v3_ptr 0 +#define __itt_frame_end_v3_ptr 0 +#define __itt_frame_submit_v3_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ +/** @} frames group */ +/** @endcond */ + +/** + * @defgroup taskgroup Task Group + * @ingroup public + * Task Group + * @{ + */ +/** + * @ingroup task_groups + * @brief Denotes a task_group instance. + * Successive calls to __itt_task_group with the same ID are ignored. + * @param[in] domain The domain for this task_group instance + * @param[in] id The instance ID for this task_group instance. Must not be __itt_null. + * @param[in] parentid The instance ID for the parent of this task_group instance, or __itt_null. + * @param[in] name The name of this task_group + */ +void ITTAPI __itt_task_group(const __itt_domain *domain, __itt_id id, __itt_id parentid, __itt_string_handle *name); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, task_group, (const __itt_domain *domain, __itt_id id, __itt_id parentid, __itt_string_handle *name)) +#define __itt_task_group(d,x,y,z) ITTNOTIFY_VOID_D3(task_group,d,x,y,z) +#define __itt_task_group_ptr ITTNOTIFY_NAME(task_group) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_task_group(d,x,y,z) +#define __itt_task_group_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_task_group_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ +/** @} taskgroup group */ + +/** + * @defgroup tasks Tasks + * @ingroup public + * A task instance represents a piece of work performed by a particular + * thread for a period of time. A call to __itt_task_begin creates a + * task instance. This becomes the current instance for that task on that + * thread. A following call to __itt_task_end on the same thread ends the + * instance. There may be multiple simultaneous instances of tasks with the + * same name on different threads. If an ID is specified, the task instance + * receives that ID. Nested tasks are allowed. + * + * Note: The task is defined by the bracketing of __itt_task_begin and + * __itt_task_end on the same thread. If some scheduling mechanism causes + * task switching (the thread executes a different user task) or task + * switching (the user task switches to a different thread) then this breaks + * the notion of current instance. Additional API calls are required to + * deal with that possibility. + * @{ + */ + +/** + * @ingroup tasks + * @brief Begin a task instance. + * @param[in] domain The domain for this task + * @param[in] taskid The instance ID for this task instance, or __itt_null + * @param[in] parentid The parent instance to which this task instance belongs, or __itt_null + * @param[in] name The name of this task + */ +void ITTAPI __itt_task_begin(const __itt_domain *domain, __itt_id taskid, __itt_id parentid, __itt_string_handle *name); + +/** + * @ingroup tasks + * @brief Begin a task instance. + * @param[in] domain The domain for this task + * @param[in] taskid The identifier for this task instance (may be 0) + * @param[in] parentid The parent of this task (may be 0) + * @param[in] fn The pointer to the function you are tracing + */ +void ITTAPI __itt_task_begin_fn(const __itt_domain *domain, __itt_id taskid, __itt_id parentid, void* fn); + +/** + * @ingroup tasks + * @brief End the current task instance. + * @param[in] domain The domain for this task + */ +void ITTAPI __itt_task_end(const __itt_domain *domain); + +/** + * @ingroup tasks + * @brief Begin an overlapped task instance. + * @param[in] domain The domain for this task. + * @param[in] taskid The identifier for this task instance, *cannot* be __itt_null. + * @param[in] parentid The parent of this task, or __itt_null. + * @param[in] name The name of this task. + */ +void ITTAPI __itt_task_begin_overlapped(const __itt_domain* domain, __itt_id taskid, __itt_id parentid, __itt_string_handle* name); + +/** + * @ingroup tasks + * @brief End an overlapped task instance. + * @param[in] domain The domain for this task + * @param[in] taskid Explicit ID of finished task + */ +void ITTAPI __itt_task_end_overlapped(const __itt_domain *domain, __itt_id taskid); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, task_begin, (const __itt_domain *domain, __itt_id id, __itt_id parentid, __itt_string_handle *name)) +ITT_STUBV(ITTAPI, void, task_begin_fn, (const __itt_domain *domain, __itt_id id, __itt_id parentid, void* fn)) +ITT_STUBV(ITTAPI, void, task_end, (const __itt_domain *domain)) +ITT_STUBV(ITTAPI, void, task_begin_overlapped, (const __itt_domain *domain, __itt_id taskid, __itt_id parentid, __itt_string_handle *name)) +ITT_STUBV(ITTAPI, void, task_end_overlapped, (const __itt_domain *domain, __itt_id taskid)) +#define __itt_task_begin(d,x,y,z) ITTNOTIFY_VOID_D3(task_begin,d,x,y,z) +#define __itt_task_begin_ptr ITTNOTIFY_NAME(task_begin) +#define __itt_task_begin_fn(d,x,y,z) ITTNOTIFY_VOID_D3(task_begin_fn,d,x,y,z) +#define __itt_task_begin_fn_ptr ITTNOTIFY_NAME(task_begin_fn) +#define __itt_task_end(d) ITTNOTIFY_VOID_D0(task_end,d) +#define __itt_task_end_ptr ITTNOTIFY_NAME(task_end) +#define __itt_task_begin_overlapped(d,x,y,z) ITTNOTIFY_VOID_D3(task_begin_overlapped,d,x,y,z) +#define __itt_task_begin_overlapped_ptr ITTNOTIFY_NAME(task_begin_overlapped) +#define __itt_task_end_overlapped(d,x) ITTNOTIFY_VOID_D1(task_end_overlapped,d,x) +#define __itt_task_end_overlapped_ptr ITTNOTIFY_NAME(task_end_overlapped) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_task_begin(domain,id,parentid,name) +#define __itt_task_begin_ptr 0 +#define __itt_task_begin_fn(domain,id,parentid,fn) +#define __itt_task_begin_fn_ptr 0 +#define __itt_task_end(domain) +#define __itt_task_end_ptr 0 +#define __itt_task_begin_overlapped(domain,taskid,parentid,name) +#define __itt_task_begin_overlapped_ptr 0 +#define __itt_task_end_overlapped(domain,taskid) +#define __itt_task_end_overlapped_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_task_begin_ptr 0 +#define __itt_task_begin_fn_ptr 0 +#define __itt_task_end_ptr 0 +#define __itt_task_begin_overlapped_ptr 0 +#define __itt_task_end_overlapped_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ +/** @} tasks group */ + + +/** + * @defgroup markers Markers + * Markers represent a single discreet event in time. Markers have a scope, + * described by an enumerated type __itt_scope. Markers are created by + * the API call __itt_marker. A marker instance can be given an ID for use in + * adding metadata. + * @{ + */ + +/** + * @brief Describes the scope of an event object in the trace. + */ +typedef enum +{ + __itt_scope_unknown = 0, + __itt_scope_global, + __itt_scope_track_group, + __itt_scope_track, + __itt_scope_task, + __itt_scope_marker +} __itt_scope; + +/** @cond exclude_from_documentation */ +#define __itt_marker_scope_unknown __itt_scope_unknown +#define __itt_marker_scope_global __itt_scope_global +#define __itt_marker_scope_process __itt_scope_track_group +#define __itt_marker_scope_thread __itt_scope_track +#define __itt_marker_scope_task __itt_scope_task +/** @endcond */ + +/** + * @ingroup markers + * @brief Create a marker instance + * @param[in] domain The domain for this marker + * @param[in] id The instance ID for this marker or __itt_null + * @param[in] name The name for this marker + * @param[in] scope The scope for this marker + */ +void ITTAPI __itt_marker(const __itt_domain *domain, __itt_id id, __itt_string_handle *name, __itt_scope scope); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, marker, (const __itt_domain *domain, __itt_id id, __itt_string_handle *name, __itt_scope scope)) +#define __itt_marker(d,x,y,z) ITTNOTIFY_VOID_D3(marker,d,x,y,z) +#define __itt_marker_ptr ITTNOTIFY_NAME(marker) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_marker(domain,id,name,scope) +#define __itt_marker_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_marker_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ +/** @} markers group */ + +/** + * @defgroup metadata Metadata + * The metadata API is used to attach extra information to named + * entities. Metadata can be attached to an identified named entity by ID, + * or to the current entity (which is always a task). + * + * Conceptually metadata has a type (what kind of metadata), a key (the + * name of the metadata), and a value (the actual data). The encoding of + * the value depends on the type of the metadata. + * + * The type of metadata is specified by an enumerated type __itt_metdata_type. + * @{ + */ + +/** + * @ingroup parameters + * @brief describes the type of metadata + */ +typedef enum { + __itt_metadata_unknown = 0, + __itt_metadata_u64, /**< Unsigned 64-bit integer */ + __itt_metadata_s64, /**< Signed 64-bit integer */ + __itt_metadata_u32, /**< Unsigned 32-bit integer */ + __itt_metadata_s32, /**< Signed 32-bit integer */ + __itt_metadata_u16, /**< Unsigned 16-bit integer */ + __itt_metadata_s16, /**< Signed 16-bit integer */ + __itt_metadata_float, /**< Signed 32-bit floating-point */ + __itt_metadata_double /**< SIgned 64-bit floating-point */ +} __itt_metadata_type; + +/** + * @ingroup parameters + * @brief Add metadata to an instance of a named entity. + * @param[in] domain The domain controlling the call + * @param[in] id The identifier of the instance to which the metadata is to be added, or __itt_null to add to the current task + * @param[in] key The name of the metadata + * @param[in] type The type of the metadata + * @param[in] count The number of elements of the given type. If count == 0, no metadata will be added. + * @param[in] data The metadata itself +*/ +void ITTAPI __itt_metadata_add(const __itt_domain *domain, __itt_id id, __itt_string_handle *key, __itt_metadata_type type, size_t count, void *data); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, metadata_add, (const __itt_domain *domain, __itt_id id, __itt_string_handle *key, __itt_metadata_type type, size_t count, void *data)) +#define __itt_metadata_add(d,x,y,z,a,b) ITTNOTIFY_VOID_D5(metadata_add,d,x,y,z,a,b) +#define __itt_metadata_add_ptr ITTNOTIFY_NAME(metadata_add) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_metadata_add(d,x,y,z,a,b) +#define __itt_metadata_add_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_metadata_add_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @ingroup parameters + * @brief Add string metadata to an instance of a named entity. + * @param[in] domain The domain controlling the call + * @param[in] id The identifier of the instance to which the metadata is to be added, or __itt_null to add to the current task + * @param[in] key The name of the metadata + * @param[in] data The metadata itself + * @param[in] length The number of characters in the string, or -1 if the length is unknown but the string is null-terminated +*/ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +void ITTAPI __itt_metadata_str_addA(const __itt_domain *domain, __itt_id id, __itt_string_handle *key, const char *data, size_t length); +void ITTAPI __itt_metadata_str_addW(const __itt_domain *domain, __itt_id id, __itt_string_handle *key, const wchar_t *data, size_t length); +#if defined(UNICODE) || defined(_UNICODE) +# define __itt_metadata_str_add __itt_metadata_str_addW +# define __itt_metadata_str_add_ptr __itt_metadata_str_addW_ptr +#else /* UNICODE */ +# define __itt_metadata_str_add __itt_metadata_str_addA +# define __itt_metadata_str_add_ptr __itt_metadata_str_addA_ptr +#endif /* UNICODE */ +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +void ITTAPI __itt_metadata_str_add(const __itt_domain *domain, __itt_id id, __itt_string_handle *key, const char *data, size_t length); +#endif + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +#if ITT_PLATFORM==ITT_PLATFORM_WIN +ITT_STUBV(ITTAPI, void, metadata_str_addA, (const __itt_domain *domain, __itt_id id, __itt_string_handle *key, const char *data, size_t length)) +ITT_STUBV(ITTAPI, void, metadata_str_addW, (const __itt_domain *domain, __itt_id id, __itt_string_handle *key, const wchar_t *data, size_t length)) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +ITT_STUBV(ITTAPI, void, metadata_str_add, (const __itt_domain *domain, __itt_id id, __itt_string_handle *key, const char *data, size_t length)) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_metadata_str_addA(d,x,y,z,a) ITTNOTIFY_VOID_D4(metadata_str_addA,d,x,y,z,a) +#define __itt_metadata_str_addA_ptr ITTNOTIFY_NAME(metadata_str_addA) +#define __itt_metadata_str_addW(d,x,y,z,a) ITTNOTIFY_VOID_D4(metadata_str_addW,d,x,y,z,a) +#define __itt_metadata_str_addW_ptr ITTNOTIFY_NAME(metadata_str_addW) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_metadata_str_add(d,x,y,z,a) ITTNOTIFY_VOID_D4(metadata_str_add,d,x,y,z,a) +#define __itt_metadata_str_add_ptr ITTNOTIFY_NAME(metadata_str_add) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#else /* INTEL_NO_ITTNOTIFY_API */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_metadata_str_addA(d,x,y,z,a) +#define __itt_metadata_str_addA_ptr 0 +#define __itt_metadata_str_addW(d,x,y,z,a) +#define __itt_metadata_str_addW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_metadata_str_add(d,x,y,z,a) +#define __itt_metadata_str_add_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_metadata_str_addA_ptr 0 +#define __itt_metadata_str_addW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_metadata_str_add_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @ingroup parameters + * @brief Add metadata to an instance of a named entity. + * @param[in] domain The domain controlling the call + * @param[in] scope The scope of the instance to which the metadata is to be added + + * @param[in] id The identifier of the instance to which the metadata is to be added, or __itt_null to add to the current task + + * @param[in] key The name of the metadata + * @param[in] type The type of the metadata + * @param[in] count The number of elements of the given type. If count == 0, no metadata will be added. + * @param[in] data The metadata itself +*/ +void ITTAPI __itt_metadata_add_with_scope(const __itt_domain *domain, __itt_scope scope, __itt_string_handle *key, __itt_metadata_type type, size_t count, void *data); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, metadata_add_with_scope, (const __itt_domain *domain, __itt_scope scope, __itt_string_handle *key, __itt_metadata_type type, size_t count, void *data)) +#define __itt_metadata_add_with_scope(d,x,y,z,a,b) ITTNOTIFY_VOID_D5(metadata_add_with_scope,d,x,y,z,a,b) +#define __itt_metadata_add_with_scope_ptr ITTNOTIFY_NAME(metadata_add_with_scope) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_metadata_add_with_scope(d,x,y,z,a,b) +#define __itt_metadata_add_with_scope_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_metadata_add_with_scope_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @ingroup parameters + * @brief Add string metadata to an instance of a named entity. + * @param[in] domain The domain controlling the call + * @param[in] scope The scope of the instance to which the metadata is to be added + + * @param[in] id The identifier of the instance to which the metadata is to be added, or __itt_null to add to the current task + + * @param[in] key The name of the metadata + * @param[in] data The metadata itself + * @param[in] length The number of characters in the string, or -1 if the length is unknown but the string is null-terminated +*/ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +void ITTAPI __itt_metadata_str_add_with_scopeA(const __itt_domain *domain, __itt_scope scope, __itt_string_handle *key, const char *data, size_t length); +void ITTAPI __itt_metadata_str_add_with_scopeW(const __itt_domain *domain, __itt_scope scope, __itt_string_handle *key, const wchar_t *data, size_t length); +#if defined(UNICODE) || defined(_UNICODE) +# define __itt_metadata_str_add_with_scope __itt_metadata_str_add_with_scopeW +# define __itt_metadata_str_add_with_scope_ptr __itt_metadata_str_add_with_scopeW_ptr +#else /* UNICODE */ +# define __itt_metadata_str_add_with_scope __itt_metadata_str_add_with_scopeA +# define __itt_metadata_str_add_with_scope_ptr __itt_metadata_str_add_with_scopeA_ptr +#endif /* UNICODE */ +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +void ITTAPI __itt_metadata_str_add_with_scope(const __itt_domain *domain, __itt_scope scope, __itt_string_handle *key, const char *data, size_t length); +#endif + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +#if ITT_PLATFORM==ITT_PLATFORM_WIN +ITT_STUBV(ITTAPI, void, metadata_str_add_with_scopeA, (const __itt_domain *domain, __itt_scope scope, __itt_string_handle *key, const char *data, size_t length)) +ITT_STUBV(ITTAPI, void, metadata_str_add_with_scopeW, (const __itt_domain *domain, __itt_scope scope, __itt_string_handle *key, const wchar_t *data, size_t length)) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +ITT_STUBV(ITTAPI, void, metadata_str_add_with_scope, (const __itt_domain *domain, __itt_scope scope, __itt_string_handle *key, const char *data, size_t length)) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_metadata_str_add_with_scopeA(d,x,y,z,a) ITTNOTIFY_VOID_D4(metadata_str_add_with_scopeA,d,x,y,z,a) +#define __itt_metadata_str_add_with_scopeA_ptr ITTNOTIFY_NAME(metadata_str_add_with_scopeA) +#define __itt_metadata_str_add_with_scopeW(d,x,y,z,a) ITTNOTIFY_VOID_D4(metadata_str_add_with_scopeW,d,x,y,z,a) +#define __itt_metadata_str_add_with_scopeW_ptr ITTNOTIFY_NAME(metadata_str_add_with_scopeW) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_metadata_str_add_with_scope(d,x,y,z,a) ITTNOTIFY_VOID_D4(metadata_str_add_with_scope,d,x,y,z,a) +#define __itt_metadata_str_add_with_scope_ptr ITTNOTIFY_NAME(metadata_str_add_with_scope) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#else /* INTEL_NO_ITTNOTIFY_API */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_metadata_str_add_with_scopeA(d,x,y,z,a) +#define __itt_metadata_str_add_with_scopeA_ptr 0 +#define __itt_metadata_str_add_with_scopeW(d,x,y,z,a) +#define __itt_metadata_str_add_with_scopeW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_metadata_str_add_with_scope(d,x,y,z,a) +#define __itt_metadata_str_add_with_scope_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_metadata_str_add_with_scopeA_ptr 0 +#define __itt_metadata_str_add_with_scopeW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_metadata_str_add_with_scope_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** @} metadata group */ + +/** + * @defgroup relations Relations + * Instances of named entities can be explicitly associated with other + * instances using instance IDs and the relationship API calls. + * + * @{ + */ + +/** + * @ingroup relations + * @brief The kind of relation between two instances is specified by the enumerated type __itt_relation. + * Relations between instances can be added with an API call. The relation + * API uses instance IDs. Relations can be added before or after the actual + * instances are created and persist independently of the instances. This + * is the motivation for having different lifetimes for instance IDs and + * the actual instances. + */ +typedef enum +{ + __itt_relation_is_unknown = 0, + __itt_relation_is_dependent_on, /**< "A is dependent on B" means that A cannot start until B completes */ + __itt_relation_is_sibling_of, /**< "A is sibling of B" means that A and B were created as a group */ + __itt_relation_is_parent_of, /**< "A is parent of B" means that A created B */ + __itt_relation_is_continuation_of, /**< "A is continuation of B" means that A assumes the dependencies of B */ + __itt_relation_is_child_of, /**< "A is child of B" means that A was created by B (inverse of is_parent_of) */ + __itt_relation_is_continued_by, /**< "A is continued by B" means that B assumes the dependencies of A (inverse of is_continuation_of) */ + __itt_relation_is_predecessor_to /**< "A is predecessor to B" means that B cannot start until A completes (inverse of is_dependent_on) */ +} __itt_relation; + +/** + * @ingroup relations + * @brief Add a relation to the current task instance. + * The current task instance is the head of the relation. + * @param[in] domain The domain controlling this call + * @param[in] relation The kind of relation + * @param[in] tail The ID for the tail of the relation + */ +void ITTAPI __itt_relation_add_to_current(const __itt_domain *domain, __itt_relation relation, __itt_id tail); + +/** + * @ingroup relations + * @brief Add a relation between two instance identifiers. + * @param[in] domain The domain controlling this call + * @param[in] head The ID for the head of the relation + * @param[in] relation The kind of relation + * @param[in] tail The ID for the tail of the relation + */ +void ITTAPI __itt_relation_add(const __itt_domain *domain, __itt_id head, __itt_relation relation, __itt_id tail); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, relation_add_to_current, (const __itt_domain *domain, __itt_relation relation, __itt_id tail)) +ITT_STUBV(ITTAPI, void, relation_add, (const __itt_domain *domain, __itt_id head, __itt_relation relation, __itt_id tail)) +#define __itt_relation_add_to_current(d,x,y) ITTNOTIFY_VOID_D2(relation_add_to_current,d,x,y) +#define __itt_relation_add_to_current_ptr ITTNOTIFY_NAME(relation_add_to_current) +#define __itt_relation_add(d,x,y,z) ITTNOTIFY_VOID_D3(relation_add,d,x,y,z) +#define __itt_relation_add_ptr ITTNOTIFY_NAME(relation_add) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_relation_add_to_current(d,x,y) +#define __itt_relation_add_to_current_ptr 0 +#define __itt_relation_add(d,x,y,z) +#define __itt_relation_add_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_relation_add_to_current_ptr 0 +#define __itt_relation_add_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ +/** @} relations group */ + +/** @cond exclude_from_documentation */ +#pragma pack(push, 8) + +typedef struct ___itt_clock_info +{ + unsigned long long clock_freq; /*!< Clock domain frequency */ + unsigned long long clock_base; /*!< Clock domain base timestamp */ +} __itt_clock_info; + +#pragma pack(pop) +/** @endcond */ + +/** @cond exclude_from_documentation */ +typedef void (ITTAPI *__itt_get_clock_info_fn)(__itt_clock_info* clock_info, void* data); +/** @endcond */ + +/** @cond exclude_from_documentation */ +#pragma pack(push, 8) + +typedef struct ___itt_clock_domain +{ + __itt_clock_info info; /*!< Most recent clock domain info */ + __itt_get_clock_info_fn fn; /*!< Callback function pointer */ + void* fn_data; /*!< Input argument for the callback function */ + int extra1; /*!< Reserved. Must be zero */ + void* extra2; /*!< Reserved. Must be zero */ + struct ___itt_clock_domain* next; +} __itt_clock_domain; + +#pragma pack(pop) +/** @endcond */ + +/** + * @ingroup clockdomains + * @brief Create a clock domain. + * Certain applications require the capability to trace their application using + * a clock domain different than the CPU, for instance the instrumentation of events + * that occur on a GPU. + * Because the set of domains is expected to be static over the application's execution time, + * there is no mechanism to destroy a domain. + * Any domain can be accessed by any thread in the process, regardless of which thread created + * the domain. This call is thread-safe. + * @param[in] fn A pointer to a callback function which retrieves alternative CPU timestamps + * @param[in] fn_data Argument for a callback function; may be NULL + */ +__itt_clock_domain* ITTAPI __itt_clock_domain_create(__itt_get_clock_info_fn fn, void* fn_data); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUB(ITTAPI, __itt_clock_domain*, clock_domain_create, (__itt_get_clock_info_fn fn, void* fn_data)) +#define __itt_clock_domain_create ITTNOTIFY_DATA(clock_domain_create) +#define __itt_clock_domain_create_ptr ITTNOTIFY_NAME(clock_domain_create) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_clock_domain_create(fn,fn_data) (__itt_clock_domain*)0 +#define __itt_clock_domain_create_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_clock_domain_create_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @ingroup clockdomains + * @brief Recalculate clock domains frequences and clock base timestamps. + */ +void ITTAPI __itt_clock_domain_reset(void); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, clock_domain_reset, (void)) +#define __itt_clock_domain_reset ITTNOTIFY_VOID(clock_domain_reset) +#define __itt_clock_domain_reset_ptr ITTNOTIFY_NAME(clock_domain_reset) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_clock_domain_reset() +#define __itt_clock_domain_reset_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_clock_domain_reset_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @ingroup clockdomain + * @brief Create an instance of identifier. This establishes the beginning of the lifetime of + * an instance of the given ID in the trace. Once this lifetime starts, the ID can be used to + * tag named entity instances in calls such as __itt_task_begin, and to specify relationships among + * identified named entity instances, using the \ref relations APIs. + * @param[in] domain The domain controlling the execution of this call. + * @param[in] clock_domain The clock domain controlling the execution of this call. + * @param[in] timestamp The user defined timestamp. + * @param[in] id The ID to create. + */ +void ITTAPI __itt_id_create_ex(const __itt_domain* domain, __itt_clock_domain* clock_domain, unsigned long long timestamp, __itt_id id); + +/** + * @ingroup clockdomain + * @brief Destroy an instance of identifier. This ends the lifetime of the current instance of the + * given ID value in the trace. Any relationships that are established after this lifetime ends are + * invalid. This call must be performed before the given ID value can be reused for a different + * named entity instance. + * @param[in] domain The domain controlling the execution of this call. + * @param[in] clock_domain The clock domain controlling the execution of this call. + * @param[in] timestamp The user defined timestamp. + * @param[in] id The ID to destroy. + */ +void ITTAPI __itt_id_destroy_ex(const __itt_domain* domain, __itt_clock_domain* clock_domain, unsigned long long timestamp, __itt_id id); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, id_create_ex, (const __itt_domain *domain, __itt_clock_domain* clock_domain, unsigned long long timestamp, __itt_id id)) +ITT_STUBV(ITTAPI, void, id_destroy_ex, (const __itt_domain *domain, __itt_clock_domain* clock_domain, unsigned long long timestamp, __itt_id id)) +#define __itt_id_create_ex(d,x,y,z) ITTNOTIFY_VOID_D3(id_create_ex,d,x,y,z) +#define __itt_id_create_ex_ptr ITTNOTIFY_NAME(id_create_ex) +#define __itt_id_destroy_ex(d,x,y,z) ITTNOTIFY_VOID_D3(id_destroy_ex,d,x,y,z) +#define __itt_id_destroy_ex_ptr ITTNOTIFY_NAME(id_destroy_ex) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_id_create_ex(domain,clock_domain,timestamp,id) +#define __itt_id_create_ex_ptr 0 +#define __itt_id_destroy_ex(domain,clock_domain,timestamp,id) +#define __itt_id_destroy_ex_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_id_create_ex_ptr 0 +#define __itt_id_destroy_ex_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @ingroup clockdomain + * @brief Begin a task instance. + * @param[in] domain The domain for this task + * @param[in] clock_domain The clock domain controlling the execution of this call. + * @param[in] timestamp The user defined timestamp. + * @param[in] taskid The instance ID for this task instance, or __itt_null + * @param[in] parentid The parent instance to which this task instance belongs, or __itt_null + * @param[in] name The name of this task + */ +void ITTAPI __itt_task_begin_ex(const __itt_domain* domain, __itt_clock_domain* clock_domain, unsigned long long timestamp, __itt_id taskid, __itt_id parentid, __itt_string_handle* name); + +/** + * @ingroup clockdomain + * @brief Begin a task instance. + * @param[in] domain The domain for this task + * @param[in] clock_domain The clock domain controlling the execution of this call. + * @param[in] timestamp The user defined timestamp. + * @param[in] taskid The identifier for this task instance, or __itt_null + * @param[in] parentid The parent of this task, or __itt_null + * @param[in] fn The pointer to the function you are tracing + */ +void ITTAPI __itt_task_begin_fn_ex(const __itt_domain* domain, __itt_clock_domain* clock_domain, unsigned long long timestamp, __itt_id taskid, __itt_id parentid, void* fn); + +/** + * @ingroup clockdomain + * @brief End the current task instance. + * @param[in] domain The domain for this task + * @param[in] clock_domain The clock domain controlling the execution of this call. + * @param[in] timestamp The user defined timestamp. + */ +void ITTAPI __itt_task_end_ex(const __itt_domain* domain, __itt_clock_domain* clock_domain, unsigned long long timestamp); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, task_begin_ex, (const __itt_domain *domain, __itt_clock_domain* clock_domain, unsigned long long timestamp, __itt_id id, __itt_id parentid, __itt_string_handle *name)) +ITT_STUBV(ITTAPI, void, task_begin_fn_ex, (const __itt_domain *domain, __itt_clock_domain* clock_domain, unsigned long long timestamp, __itt_id id, __itt_id parentid, void* fn)) +ITT_STUBV(ITTAPI, void, task_end_ex, (const __itt_domain *domain, __itt_clock_domain* clock_domain, unsigned long long timestamp)) +#define __itt_task_begin_ex(d,x,y,z,a,b) ITTNOTIFY_VOID_D5(task_begin_ex,d,x,y,z,a,b) +#define __itt_task_begin_ex_ptr ITTNOTIFY_NAME(task_begin_ex) +#define __itt_task_begin_fn_ex(d,x,y,z,a,b) ITTNOTIFY_VOID_D5(task_begin_fn_ex,d,x,y,z,a,b) +#define __itt_task_begin_fn_ex_ptr ITTNOTIFY_NAME(task_begin_fn_ex) +#define __itt_task_end_ex(d,x,y) ITTNOTIFY_VOID_D2(task_end_ex,d,x,y) +#define __itt_task_end_ex_ptr ITTNOTIFY_NAME(task_end_ex) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_task_begin_ex(domain,clock_domain,timestamp,id,parentid,name) +#define __itt_task_begin_ex_ptr 0 +#define __itt_task_begin_fn_ex(domain,clock_domain,timestamp,id,parentid,fn) +#define __itt_task_begin_fn_ex_ptr 0 +#define __itt_task_end_ex(domain,clock_domain,timestamp) +#define __itt_task_end_ex_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_task_begin_ex_ptr 0 +#define __itt_task_begin_fn_ex_ptr 0 +#define __itt_task_end_ex_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @defgroup counters Counters + * @ingroup public + * Counters are user-defined objects with a monotonically increasing + * value. Counter values are 64-bit unsigned integers. + * Counters have names that can be displayed in + * the tools. + * @{ + */ + +/** + * @brief opaque structure for counter identification + */ +/** @cond exclude_from_documentation */ + +typedef struct ___itt_counter* __itt_counter; + +/** + * @brief Create an unsigned 64 bits integer counter with given name/domain + * + * After __itt_counter_create() is called, __itt_counter_inc(id), __itt_counter_inc_delta(id, delta), + * __itt_counter_set_value(id, value_ptr) or __itt_counter_set_value_ex(id, clock_domain, timestamp, value_ptr) + * can be used to change the value of the counter, where value_ptr is a pointer to an unsigned 64 bits integer + * + * The call is equal to __itt_counter_create_typed(name, domain, __itt_metadata_u64) + */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +__itt_counter ITTAPI __itt_counter_createA(const char *name, const char *domain); +__itt_counter ITTAPI __itt_counter_createW(const wchar_t *name, const wchar_t *domain); +#if defined(UNICODE) || defined(_UNICODE) +# define __itt_counter_create __itt_counter_createW +# define __itt_counter_create_ptr __itt_counter_createW_ptr +#else /* UNICODE */ +# define __itt_counter_create __itt_counter_createA +# define __itt_counter_create_ptr __itt_counter_createA_ptr +#endif /* UNICODE */ +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +__itt_counter ITTAPI __itt_counter_create(const char *name, const char *domain); +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ + +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +#if ITT_PLATFORM==ITT_PLATFORM_WIN +ITT_STUB(ITTAPI, __itt_counter, counter_createA, (const char *name, const char *domain)) +ITT_STUB(ITTAPI, __itt_counter, counter_createW, (const wchar_t *name, const wchar_t *domain)) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +ITT_STUB(ITTAPI, __itt_counter, counter_create, (const char *name, const char *domain)) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_counter_createA ITTNOTIFY_DATA(counter_createA) +#define __itt_counter_createA_ptr ITTNOTIFY_NAME(counter_createA) +#define __itt_counter_createW ITTNOTIFY_DATA(counter_createW) +#define __itt_counter_createW_ptr ITTNOTIFY_NAME(counter_createW) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_counter_create ITTNOTIFY_DATA(counter_create) +#define __itt_counter_create_ptr ITTNOTIFY_NAME(counter_create) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#else /* INTEL_NO_ITTNOTIFY_API */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_counter_createA(name, domain) +#define __itt_counter_createA_ptr 0 +#define __itt_counter_createW(name, domain) +#define __itt_counter_createW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_counter_create(name, domain) +#define __itt_counter_create_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_counter_createA_ptr 0 +#define __itt_counter_createW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_counter_create_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Increment the unsigned 64 bits integer counter value + * + * Calling this function to non-unsigned 64 bits integer counters has no effect + */ +void ITTAPI __itt_counter_inc(__itt_counter id); + +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, counter_inc, (__itt_counter id)) +#define __itt_counter_inc ITTNOTIFY_VOID(counter_inc) +#define __itt_counter_inc_ptr ITTNOTIFY_NAME(counter_inc) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_counter_inc(id) +#define __itt_counter_inc_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_counter_inc_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ +/** + * @brief Increment the unsigned 64 bits integer counter value with x + * + * Calling this function to non-unsigned 64 bits integer counters has no effect + */ +void ITTAPI __itt_counter_inc_delta(__itt_counter id, unsigned long long value); + +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, counter_inc_delta, (__itt_counter id, unsigned long long value)) +#define __itt_counter_inc_delta ITTNOTIFY_VOID(counter_inc_delta) +#define __itt_counter_inc_delta_ptr ITTNOTIFY_NAME(counter_inc_delta) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_counter_inc_delta(id, value) +#define __itt_counter_inc_delta_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_counter_inc_delta_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Decrement the unsigned 64 bits integer counter value + * + * Calling this function to non-unsigned 64 bits integer counters has no effect + */ +void ITTAPI __itt_counter_dec(__itt_counter id); + +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, counter_dec, (__itt_counter id)) +#define __itt_counter_dec ITTNOTIFY_VOID(counter_dec) +#define __itt_counter_dec_ptr ITTNOTIFY_NAME(counter_dec) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_counter_dec(id) +#define __itt_counter_dec_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_counter_dec_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ +/** + * @brief Decrement the unsigned 64 bits integer counter value with x + * + * Calling this function to non-unsigned 64 bits integer counters has no effect + */ +void ITTAPI __itt_counter_dec_delta(__itt_counter id, unsigned long long value); + +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, counter_dec_delta, (__itt_counter id, unsigned long long value)) +#define __itt_counter_dec_delta ITTNOTIFY_VOID(counter_dec_delta) +#define __itt_counter_dec_delta_ptr ITTNOTIFY_NAME(counter_dec_delta) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_counter_dec_delta(id, value) +#define __itt_counter_dec_delta_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_counter_dec_delta_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @ingroup counters + * @brief Increment a counter by one. + * The first call with a given name creates a counter by that name and sets its + * value to zero. Successive calls increment the counter value. + * @param[in] domain The domain controlling the call. Counter names are not domain specific. + * The domain argument is used only to enable or disable the API calls. + * @param[in] name The name of the counter + */ +void ITTAPI __itt_counter_inc_v3(const __itt_domain *domain, __itt_string_handle *name); + +/** + * @ingroup counters + * @brief Increment a counter by the value specified in delta. + * @param[in] domain The domain controlling the call. Counter names are not domain specific. + * The domain argument is used only to enable or disable the API calls. + * @param[in] name The name of the counter + * @param[in] delta The amount by which to increment the counter + */ +void ITTAPI __itt_counter_inc_delta_v3(const __itt_domain *domain, __itt_string_handle *name, unsigned long long delta); + +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, counter_inc_v3, (const __itt_domain *domain, __itt_string_handle *name)) +ITT_STUBV(ITTAPI, void, counter_inc_delta_v3, (const __itt_domain *domain, __itt_string_handle *name, unsigned long long delta)) +#define __itt_counter_inc_v3(d,x) ITTNOTIFY_VOID_D1(counter_inc_v3,d,x) +#define __itt_counter_inc_v3_ptr ITTNOTIFY_NAME(counter_inc_v3) +#define __itt_counter_inc_delta_v3(d,x,y) ITTNOTIFY_VOID_D2(counter_inc_delta_v3,d,x,y) +#define __itt_counter_inc_delta_v3_ptr ITTNOTIFY_NAME(counter_inc_delta_v3) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_counter_inc_v3(domain,name) +#define __itt_counter_inc_v3_ptr 0 +#define __itt_counter_inc_delta_v3(domain,name,delta) +#define __itt_counter_inc_delta_v3_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_counter_inc_v3_ptr 0 +#define __itt_counter_inc_delta_v3_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + + +/** + * @ingroup counters + * @brief Decrement a counter by one. + * The first call with a given name creates a counter by that name and sets its + * value to zero. Successive calls decrement the counter value. + * @param[in] domain The domain controlling the call. Counter names are not domain specific. + * The domain argument is used only to enable or disable the API calls. + * @param[in] name The name of the counter + */ +void ITTAPI __itt_counter_dec_v3(const __itt_domain *domain, __itt_string_handle *name); + +/** + * @ingroup counters + * @brief Decrement a counter by the value specified in delta. + * @param[in] domain The domain controlling the call. Counter names are not domain specific. + * The domain argument is used only to enable or disable the API calls. + * @param[in] name The name of the counter + * @param[in] delta The amount by which to decrement the counter + */ +void ITTAPI __itt_counter_dec_delta_v3(const __itt_domain *domain, __itt_string_handle *name, unsigned long long delta); + +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, counter_dec_v3, (const __itt_domain *domain, __itt_string_handle *name)) +ITT_STUBV(ITTAPI, void, counter_dec_delta_v3, (const __itt_domain *domain, __itt_string_handle *name, unsigned long long delta)) +#define __itt_counter_dec_v3(d,x) ITTNOTIFY_VOID_D1(counter_dec_v3,d,x) +#define __itt_counter_dec_v3_ptr ITTNOTIFY_NAME(counter_dec_v3) +#define __itt_counter_dec_delta_v3(d,x,y) ITTNOTIFY_VOID_D2(counter_dec_delta_v3,d,x,y) +#define __itt_counter_dec_delta_v3_ptr ITTNOTIFY_NAME(counter_dec_delta_v3) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_counter_dec_v3(domain,name) +#define __itt_counter_dec_v3_ptr 0 +#define __itt_counter_dec_delta_v3(domain,name,delta) +#define __itt_counter_dec_delta_v3_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_counter_dec_v3_ptr 0 +#define __itt_counter_dec_delta_v3_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** @} counters group */ + + +/** + * @brief Set the counter value + */ +void ITTAPI __itt_counter_set_value(__itt_counter id, void *value_ptr); + +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, counter_set_value, (__itt_counter id, void *value_ptr)) +#define __itt_counter_set_value ITTNOTIFY_VOID(counter_set_value) +#define __itt_counter_set_value_ptr ITTNOTIFY_NAME(counter_set_value) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_counter_set_value(id, value_ptr) +#define __itt_counter_set_value_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_counter_set_value_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Set the counter value + */ +void ITTAPI __itt_counter_set_value_ex(__itt_counter id, __itt_clock_domain *clock_domain, unsigned long long timestamp, void *value_ptr); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, counter_set_value_ex, (__itt_counter id, __itt_clock_domain *clock_domain, unsigned long long timestamp, void *value_ptr)) +#define __itt_counter_set_value_ex ITTNOTIFY_VOID(counter_set_value_ex) +#define __itt_counter_set_value_ex_ptr ITTNOTIFY_NAME(counter_set_value_ex) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_counter_set_value_ex(id, clock_domain, timestamp, value_ptr) +#define __itt_counter_set_value_ex_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_counter_set_value_ex_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Create a typed counter with given name/domain + * + * After __itt_counter_create_typed() is called, __itt_counter_inc(id), __itt_counter_inc_delta(id, delta), + * __itt_counter_set_value(id, value_ptr) or __itt_counter_set_value_ex(id, clock_domain, timestamp, value_ptr) + * can be used to change the value of the counter + */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +__itt_counter ITTAPI __itt_counter_create_typedA(const char *name, const char *domain, __itt_metadata_type type); +__itt_counter ITTAPI __itt_counter_create_typedW(const wchar_t *name, const wchar_t *domain, __itt_metadata_type type); +#if defined(UNICODE) || defined(_UNICODE) +# define __itt_counter_create_typed __itt_counter_create_typedW +# define __itt_counter_create_typed_ptr __itt_counter_create_typedW_ptr +#else /* UNICODE */ +# define __itt_counter_create_typed __itt_counter_create_typedA +# define __itt_counter_create_typed_ptr __itt_counter_create_typedA_ptr +#endif /* UNICODE */ +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +__itt_counter ITTAPI __itt_counter_create_typed(const char *name, const char *domain, __itt_metadata_type type); +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ + +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +#if ITT_PLATFORM==ITT_PLATFORM_WIN +ITT_STUB(ITTAPI, __itt_counter, counter_create_typedA, (const char *name, const char *domain, __itt_metadata_type type)) +ITT_STUB(ITTAPI, __itt_counter, counter_create_typedW, (const wchar_t *name, const wchar_t *domain, __itt_metadata_type type)) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +ITT_STUB(ITTAPI, __itt_counter, counter_create_typed, (const char *name, const char *domain, __itt_metadata_type type)) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_counter_create_typedA ITTNOTIFY_DATA(counter_create_typedA) +#define __itt_counter_create_typedA_ptr ITTNOTIFY_NAME(counter_create_typedA) +#define __itt_counter_create_typedW ITTNOTIFY_DATA(counter_create_typedW) +#define __itt_counter_create_typedW_ptr ITTNOTIFY_NAME(counter_create_typedW) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_counter_create_typed ITTNOTIFY_DATA(counter_create_typed) +#define __itt_counter_create_typed_ptr ITTNOTIFY_NAME(counter_create_typed) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#else /* INTEL_NO_ITTNOTIFY_API */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_counter_create_typedA(name, domain, type) +#define __itt_counter_create_typedA_ptr 0 +#define __itt_counter_create_typedW(name, domain, type) +#define __itt_counter_create_typedW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_counter_create_typed(name, domain, type) +#define __itt_counter_create_typed_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_counter_create_typedA_ptr 0 +#define __itt_counter_create_typedW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_counter_create_typed_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Destroy the counter identified by the pointer previously returned by __itt_counter_create() or + * __itt_counter_create_typed() + */ +void ITTAPI __itt_counter_destroy(__itt_counter id); + +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, counter_destroy, (__itt_counter id)) +#define __itt_counter_destroy ITTNOTIFY_VOID(counter_destroy) +#define __itt_counter_destroy_ptr ITTNOTIFY_NAME(counter_destroy) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_counter_destroy(id) +#define __itt_counter_destroy_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_counter_destroy_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ +/** @} counters group */ + +/** + * @ingroup markers + * @brief Create a marker instance. + * @param[in] domain The domain for this marker + * @param[in] clock_domain The clock domain controlling the execution of this call. + * @param[in] timestamp The user defined timestamp. + * @param[in] id The instance ID for this marker, or __itt_null + * @param[in] name The name for this marker + * @param[in] scope The scope for this marker + */ +void ITTAPI __itt_marker_ex(const __itt_domain *domain, __itt_clock_domain* clock_domain, unsigned long long timestamp, __itt_id id, __itt_string_handle *name, __itt_scope scope); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, marker_ex, (const __itt_domain *domain, __itt_clock_domain* clock_domain, unsigned long long timestamp, __itt_id id, __itt_string_handle *name, __itt_scope scope)) +#define __itt_marker_ex(d,x,y,z,a,b) ITTNOTIFY_VOID_D5(marker_ex,d,x,y,z,a,b) +#define __itt_marker_ex_ptr ITTNOTIFY_NAME(marker_ex) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_marker_ex(domain,clock_domain,timestamp,id,name,scope) +#define __itt_marker_ex_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_marker_ex_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @ingroup clockdomain + * @brief Add a relation to the current task instance. + * The current task instance is the head of the relation. + * @param[in] domain The domain controlling this call + * @param[in] clock_domain The clock domain controlling the execution of this call. + * @param[in] timestamp The user defined timestamp. + * @param[in] relation The kind of relation + * @param[in] tail The ID for the tail of the relation + */ +void ITTAPI __itt_relation_add_to_current_ex(const __itt_domain *domain, __itt_clock_domain* clock_domain, unsigned long long timestamp, __itt_relation relation, __itt_id tail); + +/** + * @ingroup clockdomain + * @brief Add a relation between two instance identifiers. + * @param[in] domain The domain controlling this call + * @param[in] clock_domain The clock domain controlling the execution of this call. + * @param[in] timestamp The user defined timestamp. + * @param[in] head The ID for the head of the relation + * @param[in] relation The kind of relation + * @param[in] tail The ID for the tail of the relation + */ +void ITTAPI __itt_relation_add_ex(const __itt_domain *domain, __itt_clock_domain* clock_domain, unsigned long long timestamp, __itt_id head, __itt_relation relation, __itt_id tail); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, relation_add_to_current_ex, (const __itt_domain *domain, __itt_clock_domain* clock_domain, unsigned long long timestamp, __itt_relation relation, __itt_id tail)) +ITT_STUBV(ITTAPI, void, relation_add_ex, (const __itt_domain *domain, __itt_clock_domain* clock_domain, unsigned long long timestamp, __itt_id head, __itt_relation relation, __itt_id tail)) +#define __itt_relation_add_to_current_ex(d,x,y,z,a) ITTNOTIFY_VOID_D4(relation_add_to_current_ex,d,x,y,z,a) +#define __itt_relation_add_to_current_ex_ptr ITTNOTIFY_NAME(relation_add_to_current_ex) +#define __itt_relation_add_ex(d,x,y,z,a,b) ITTNOTIFY_VOID_D5(relation_add_ex,d,x,y,z,a,b) +#define __itt_relation_add_ex_ptr ITTNOTIFY_NAME(relation_add_ex) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_relation_add_to_current_ex(domain,clock_domain,timestame,relation,tail) +#define __itt_relation_add_to_current_ex_ptr 0 +#define __itt_relation_add_ex(domain,clock_domain,timestamp,head,relation,tail) +#define __itt_relation_add_ex_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_relation_add_to_current_ex_ptr 0 +#define __itt_relation_add_ex_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** @cond exclude_from_documentation */ +typedef enum ___itt_track_group_type +{ + __itt_track_group_type_normal = 0 +} __itt_track_group_type; +/** @endcond */ + +/** @cond exclude_from_documentation */ +#pragma pack(push, 8) + +typedef struct ___itt_track_group +{ + __itt_string_handle* name; /*!< Name of the track group */ + struct ___itt_track* track; /*!< List of child tracks */ + __itt_track_group_type tgtype; /*!< Type of the track group */ + int extra1; /*!< Reserved. Must be zero */ + void* extra2; /*!< Reserved. Must be zero */ + struct ___itt_track_group* next; +} __itt_track_group; + +#pragma pack(pop) +/** @endcond */ + +/** + * @brief Placeholder for custom track types. Currently, "normal" custom track + * is the only available track type. + */ +typedef enum ___itt_track_type +{ + __itt_track_type_normal = 0 +#ifdef INTEL_ITTNOTIFY_API_PRIVATE + , __itt_track_type_queue +#endif /* INTEL_ITTNOTIFY_API_PRIVATE */ +} __itt_track_type; + +/** @cond exclude_from_documentation */ +#pragma pack(push, 8) + +typedef struct ___itt_track +{ + __itt_string_handle* name; /*!< Name of the track group */ + __itt_track_group* group; /*!< Parent group to a track */ + __itt_track_type ttype; /*!< Type of the track */ + int extra1; /*!< Reserved. Must be zero */ + void* extra2; /*!< Reserved. Must be zero */ + struct ___itt_track* next; +} __itt_track; + +#pragma pack(pop) +/** @endcond */ + +/** + * @brief Create logical track group. + */ +__itt_track_group* ITTAPI __itt_track_group_create(__itt_string_handle* name, __itt_track_group_type track_group_type); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUB(ITTAPI, __itt_track_group*, track_group_create, (__itt_string_handle* name, __itt_track_group_type track_group_type)) +#define __itt_track_group_create ITTNOTIFY_DATA(track_group_create) +#define __itt_track_group_create_ptr ITTNOTIFY_NAME(track_group_create) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_track_group_create(name) (__itt_track_group*)0 +#define __itt_track_group_create_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_track_group_create_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Create logical track. + */ +__itt_track* ITTAPI __itt_track_create(__itt_track_group* track_group, __itt_string_handle* name, __itt_track_type track_type); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUB(ITTAPI, __itt_track*, track_create, (__itt_track_group* track_group,__itt_string_handle* name, __itt_track_type track_type)) +#define __itt_track_create ITTNOTIFY_DATA(track_create) +#define __itt_track_create_ptr ITTNOTIFY_NAME(track_create) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_track_create(track_group,name,track_type) (__itt_track*)0 +#define __itt_track_create_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_track_create_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Set the logical track. + */ +void ITTAPI __itt_set_track(__itt_track* track); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, set_track, (__itt_track *track)) +#define __itt_set_track ITTNOTIFY_VOID(set_track) +#define __itt_set_track_ptr ITTNOTIFY_NAME(set_track) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_set_track(track) +#define __itt_set_track_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_set_track_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/* ========================================================================== */ +/** @cond exclude_from_gpa_documentation */ +/** + * @defgroup events Events + * @ingroup public + * Events group + * @{ + */ +/** @brief user event type */ +typedef int __itt_event; + +/** + * @brief Create an event notification + * @note name or namelen being null/name and namelen not matching, user event feature not enabled + * @return non-zero event identifier upon success and __itt_err otherwise + */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +__itt_event LIBITTAPI __itt_event_createA(const char *name, int namelen); +__itt_event LIBITTAPI __itt_event_createW(const wchar_t *name, int namelen); +#if defined(UNICODE) || defined(_UNICODE) +# define __itt_event_create __itt_event_createW +# define __itt_event_create_ptr __itt_event_createW_ptr +#else +# define __itt_event_create __itt_event_createA +# define __itt_event_create_ptr __itt_event_createA_ptr +#endif /* UNICODE */ +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +__itt_event LIBITTAPI __itt_event_create(const char *name, int namelen); +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +#if ITT_PLATFORM==ITT_PLATFORM_WIN +ITT_STUB(LIBITTAPI, __itt_event, event_createA, (const char *name, int namelen)) +ITT_STUB(LIBITTAPI, __itt_event, event_createW, (const wchar_t *name, int namelen)) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +ITT_STUB(LIBITTAPI, __itt_event, event_create, (const char *name, int namelen)) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_event_createA ITTNOTIFY_DATA(event_createA) +#define __itt_event_createA_ptr ITTNOTIFY_NAME(event_createA) +#define __itt_event_createW ITTNOTIFY_DATA(event_createW) +#define __itt_event_createW_ptr ITTNOTIFY_NAME(event_createW) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_event_create ITTNOTIFY_DATA(event_create) +#define __itt_event_create_ptr ITTNOTIFY_NAME(event_create) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#else /* INTEL_NO_ITTNOTIFY_API */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_event_createA(name, namelen) (__itt_event)0 +#define __itt_event_createA_ptr 0 +#define __itt_event_createW(name, namelen) (__itt_event)0 +#define __itt_event_createW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_event_create(name, namelen) (__itt_event)0 +#define __itt_event_create_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_event_createA_ptr 0 +#define __itt_event_createW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_event_create_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Record an event occurrence. + * @return __itt_err upon failure (invalid event id/user event feature not enabled) + */ +int LIBITTAPI __itt_event_start(__itt_event event); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUB(LIBITTAPI, int, event_start, (__itt_event event)) +#define __itt_event_start ITTNOTIFY_DATA(event_start) +#define __itt_event_start_ptr ITTNOTIFY_NAME(event_start) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_event_start(event) (int)0 +#define __itt_event_start_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_event_start_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Record an event end occurrence. + * @note It is optional if events do not have durations. + * @return __itt_err upon failure (invalid event id/user event feature not enabled) + */ +int LIBITTAPI __itt_event_end(__itt_event event); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUB(LIBITTAPI, int, event_end, (__itt_event event)) +#define __itt_event_end ITTNOTIFY_DATA(event_end) +#define __itt_event_end_ptr ITTNOTIFY_NAME(event_end) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_event_end(event) (int)0 +#define __itt_event_end_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_event_end_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ +/** @} events group */ + + +/** + * @defgroup arrays Arrays Visualizer + * @ingroup public + * Visualize arrays + * @{ + */ + +/** + * @enum __itt_av_data_type + * @brief Defines types of arrays data (for C/C++ intrinsic types) + */ +typedef enum +{ + __itt_e_first = 0, + __itt_e_char = 0, /* 1-byte integer */ + __itt_e_uchar, /* 1-byte unsigned integer */ + __itt_e_int16, /* 2-byte integer */ + __itt_e_uint16, /* 2-byte unsigned integer */ + __itt_e_int32, /* 4-byte integer */ + __itt_e_uint32, /* 4-byte unsigned integer */ + __itt_e_int64, /* 8-byte integer */ + __itt_e_uint64, /* 8-byte unsigned integer */ + __itt_e_float, /* 4-byte floating */ + __itt_e_double, /* 8-byte floating */ + __itt_e_last = __itt_e_double +} __itt_av_data_type; + +/** + * @brief Save an array data to a file. + * Output format is defined by the file extension. The csv and bmp formats are supported (bmp - for 2-dimensional array only). + * @param[in] data - pointer to the array data + * @param[in] rank - the rank of the array + * @param[in] dimensions - pointer to an array of integers, which specifies the array dimensions. + * The size of dimensions must be equal to the rank + * @param[in] type - the type of the array, specified as one of the __itt_av_data_type values (for intrinsic types) + * @param[in] filePath - the file path; the output format is defined by the file extension + * @param[in] columnOrder - defines how the array is stored in the linear memory. + * It should be 1 for column-major order (e.g. in FORTRAN) or 0 - for row-major order (e.g. in C). + */ + +#if ITT_PLATFORM==ITT_PLATFORM_WIN +int ITTAPI __itt_av_saveA(void *data, int rank, const int *dimensions, int type, const char *filePath, int columnOrder); +int ITTAPI __itt_av_saveW(void *data, int rank, const int *dimensions, int type, const wchar_t *filePath, int columnOrder); +#if defined(UNICODE) || defined(_UNICODE) +# define __itt_av_save __itt_av_saveW +# define __itt_av_save_ptr __itt_av_saveW_ptr +#else /* UNICODE */ +# define __itt_av_save __itt_av_saveA +# define __itt_av_save_ptr __itt_av_saveA_ptr +#endif /* UNICODE */ +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +int ITTAPI __itt_av_save(void *data, int rank, const int *dimensions, int type, const char *filePath, int columnOrder); +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +#if ITT_PLATFORM==ITT_PLATFORM_WIN +ITT_STUB(ITTAPI, int, av_saveA, (void *data, int rank, const int *dimensions, int type, const char *filePath, int columnOrder)) +ITT_STUB(ITTAPI, int, av_saveW, (void *data, int rank, const int *dimensions, int type, const wchar_t *filePath, int columnOrder)) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +ITT_STUB(ITTAPI, int, av_save, (void *data, int rank, const int *dimensions, int type, const char *filePath, int columnOrder)) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_av_saveA ITTNOTIFY_DATA(av_saveA) +#define __itt_av_saveA_ptr ITTNOTIFY_NAME(av_saveA) +#define __itt_av_saveW ITTNOTIFY_DATA(av_saveW) +#define __itt_av_saveW_ptr ITTNOTIFY_NAME(av_saveW) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_av_save ITTNOTIFY_DATA(av_save) +#define __itt_av_save_ptr ITTNOTIFY_NAME(av_save) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#else /* INTEL_NO_ITTNOTIFY_API */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_av_saveA(name) +#define __itt_av_saveA_ptr 0 +#define __itt_av_saveW(name) +#define __itt_av_saveW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_av_save(name) +#define __itt_av_save_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_av_saveA_ptr 0 +#define __itt_av_saveW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_av_save_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +void ITTAPI __itt_enable_attach(void); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, enable_attach, (void)) +#define __itt_enable_attach ITTNOTIFY_VOID(enable_attach) +#define __itt_enable_attach_ptr ITTNOTIFY_NAME(enable_attach) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_enable_attach() +#define __itt_enable_attach_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_enable_attach_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** @cond exclude_from_gpa_documentation */ + +/** @} arrays group */ + +/** @endcond */ + +/** + * @brief Module load info + * This API is used to report necessary information in case of module relocation + * @param[in] start_addr - relocated module start address + * @param[in] end_addr - relocated module end address + * @param[in] path - file system path to the module + */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +void ITTAPI __itt_module_loadA(void *start_addr, void *end_addr, const char *path); +void ITTAPI __itt_module_loadW(void *start_addr, void *end_addr, const wchar_t *path); +#if defined(UNICODE) || defined(_UNICODE) +# define __itt_module_load __itt_module_loadW +# define __itt_module_load_ptr __itt_module_loadW_ptr +#else /* UNICODE */ +# define __itt_module_load __itt_module_loadA +# define __itt_module_load_ptr __itt_module_loadA_ptr +#endif /* UNICODE */ +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +void ITTAPI __itt_module_load(void *start_addr, void *end_addr, const char *path); +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +#if ITT_PLATFORM==ITT_PLATFORM_WIN +ITT_STUB(ITTAPI, void, module_loadA, (void *start_addr, void *end_addr, const char *path)) +ITT_STUB(ITTAPI, void, module_loadW, (void *start_addr, void *end_addr, const wchar_t *path)) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +ITT_STUB(ITTAPI, void, module_load, (void *start_addr, void *end_addr, const char *path)) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_module_loadA ITTNOTIFY_VOID(module_loadA) +#define __itt_module_loadA_ptr ITTNOTIFY_NAME(module_loadA) +#define __itt_module_loadW ITTNOTIFY_VOID(module_loadW) +#define __itt_module_loadW_ptr ITTNOTIFY_NAME(module_loadW) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_module_load ITTNOTIFY_VOID(module_load) +#define __itt_module_load_ptr ITTNOTIFY_NAME(module_load) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#else /* INTEL_NO_ITTNOTIFY_API */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_module_loadA(start_addr, end_addr, path) +#define __itt_module_loadA_ptr 0 +#define __itt_module_loadW(start_addr, end_addr, path) +#define __itt_module_loadW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_module_load(start_addr, end_addr, path) +#define __itt_module_load_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_module_loadA_ptr 0 +#define __itt_module_loadW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_module_load_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + + + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* _ITTNOTIFY_H_ */ + +#ifdef INTEL_ITTNOTIFY_API_PRIVATE + +#ifndef _ITTNOTIFY_PRIVATE_ +#define _ITTNOTIFY_PRIVATE_ + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/** + * @ingroup clockdomain + * @brief Begin an overlapped task instance. + * @param[in] domain The domain for this task + * @param[in] clock_domain The clock domain controlling the execution of this call. + * @param[in] timestamp The user defined timestamp. + * @param[in] taskid The identifier for this task instance, *cannot* be __itt_null. + * @param[in] parentid The parent of this task, or __itt_null. + * @param[in] name The name of this task. + */ +void ITTAPI __itt_task_begin_overlapped_ex(const __itt_domain* domain, __itt_clock_domain* clock_domain, unsigned long long timestamp, __itt_id taskid, __itt_id parentid, __itt_string_handle* name); + +/** + * @ingroup clockdomain + * @brief End an overlapped task instance. + * @param[in] domain The domain for this task + * @param[in] clock_domain The clock domain controlling the execution of this call. + * @param[in] timestamp The user defined timestamp. + * @param[in] taskid Explicit ID of finished task + */ +void ITTAPI __itt_task_end_overlapped_ex(const __itt_domain* domain, __itt_clock_domain* clock_domain, unsigned long long timestamp, __itt_id taskid); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, task_begin_overlapped_ex, (const __itt_domain* domain, __itt_clock_domain* clock_domain, unsigned long long timestamp, __itt_id taskid, __itt_id parentid, __itt_string_handle* name)) +ITT_STUBV(ITTAPI, void, task_end_overlapped_ex, (const __itt_domain* domain, __itt_clock_domain* clock_domain, unsigned long long timestamp, __itt_id taskid)) +#define __itt_task_begin_overlapped_ex(d,x,y,z,a,b) ITTNOTIFY_VOID_D5(task_begin_overlapped_ex,d,x,y,z,a,b) +#define __itt_task_begin_overlapped_ex_ptr ITTNOTIFY_NAME(task_begin_overlapped_ex) +#define __itt_task_end_overlapped_ex(d,x,y,z) ITTNOTIFY_VOID_D3(task_end_overlapped_ex,d,x,y,z) +#define __itt_task_end_overlapped_ex_ptr ITTNOTIFY_NAME(task_end_overlapped_ex) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_task_begin_overlapped_ex(domain,clock_domain,timestamp,taskid,parentid,name) +#define __itt_task_begin_overlapped_ex_ptr 0 +#define __itt_task_end_overlapped_ex(domain,clock_domain,timestamp,taskid) +#define __itt_task_end_overlapped_ex_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_task_begin_overlapped_ex_ptr 0 +#define __itt_task_end_overlapped_ptr 0 +#define __itt_task_end_overlapped_ex_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @defgroup makrs_internal Marks + * @ingroup internal + * Marks group + * @warning Internal API: + * - It is not shipped to outside of Intel + * - It is delivered to internal Intel teams using e-mail or SVN access only + * @{ + */ +/** @brief user mark type */ +typedef int __itt_mark_type; + +/** + * @brief Creates a user mark type with the specified name using char or Unicode string. + * @param[in] name - name of mark to create + * @return Returns a handle to the mark type + */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +__itt_mark_type ITTAPI __itt_mark_createA(const char *name); +__itt_mark_type ITTAPI __itt_mark_createW(const wchar_t *name); +#if defined(UNICODE) || defined(_UNICODE) +# define __itt_mark_create __itt_mark_createW +# define __itt_mark_create_ptr __itt_mark_createW_ptr +#else /* UNICODE */ +# define __itt_mark_create __itt_mark_createA +# define __itt_mark_create_ptr __itt_mark_createA_ptr +#endif /* UNICODE */ +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +__itt_mark_type ITTAPI __itt_mark_create(const char *name); +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +#if ITT_PLATFORM==ITT_PLATFORM_WIN +ITT_STUB(ITTAPI, __itt_mark_type, mark_createA, (const char *name)) +ITT_STUB(ITTAPI, __itt_mark_type, mark_createW, (const wchar_t *name)) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +ITT_STUB(ITTAPI, __itt_mark_type, mark_create, (const char *name)) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_mark_createA ITTNOTIFY_DATA(mark_createA) +#define __itt_mark_createA_ptr ITTNOTIFY_NAME(mark_createA) +#define __itt_mark_createW ITTNOTIFY_DATA(mark_createW) +#define __itt_mark_createW_ptr ITTNOTIFY_NAME(mark_createW) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_mark_create ITTNOTIFY_DATA(mark_create) +#define __itt_mark_create_ptr ITTNOTIFY_NAME(mark_create) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#else /* INTEL_NO_ITTNOTIFY_API */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_mark_createA(name) (__itt_mark_type)0 +#define __itt_mark_createA_ptr 0 +#define __itt_mark_createW(name) (__itt_mark_type)0 +#define __itt_mark_createW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_mark_create(name) (__itt_mark_type)0 +#define __itt_mark_create_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_mark_createA_ptr 0 +#define __itt_mark_createW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_mark_create_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Creates a "discrete" user mark type of the specified type and an optional parameter using char or Unicode string. + * + * - The mark of "discrete" type is placed to collection results in case of success. It appears in overtime view(s) as a special tick sign. + * - The call is "synchronous" - function returns after mark is actually added to results. + * - This function is useful, for example, to mark different phases of application + * (beginning of the next mark automatically meand end of current region). + * - Can be used together with "continuous" marks (see below) at the same collection session + * @param[in] mt - mark, created by __itt_mark_create(const char* name) function + * @param[in] parameter - string parameter of mark + * @return Returns zero value in case of success, non-zero value otherwise. + */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +int ITTAPI __itt_markA(__itt_mark_type mt, const char *parameter); +int ITTAPI __itt_markW(__itt_mark_type mt, const wchar_t *parameter); +#if defined(UNICODE) || defined(_UNICODE) +# define __itt_mark __itt_markW +# define __itt_mark_ptr __itt_markW_ptr +#else /* UNICODE */ +# define __itt_mark __itt_markA +# define __itt_mark_ptr __itt_markA_ptr +#endif /* UNICODE */ +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +int ITTAPI __itt_mark(__itt_mark_type mt, const char *parameter); +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +#if ITT_PLATFORM==ITT_PLATFORM_WIN +ITT_STUB(ITTAPI, int, markA, (__itt_mark_type mt, const char *parameter)) +ITT_STUB(ITTAPI, int, markW, (__itt_mark_type mt, const wchar_t *parameter)) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +ITT_STUB(ITTAPI, int, mark, (__itt_mark_type mt, const char *parameter)) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_markA ITTNOTIFY_DATA(markA) +#define __itt_markA_ptr ITTNOTIFY_NAME(markA) +#define __itt_markW ITTNOTIFY_DATA(markW) +#define __itt_markW_ptr ITTNOTIFY_NAME(markW) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_mark ITTNOTIFY_DATA(mark) +#define __itt_mark_ptr ITTNOTIFY_NAME(mark) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#else /* INTEL_NO_ITTNOTIFY_API */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_markA(mt, parameter) (int)0 +#define __itt_markA_ptr 0 +#define __itt_markW(mt, parameter) (int)0 +#define __itt_markW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_mark(mt, parameter) (int)0 +#define __itt_mark_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_markA_ptr 0 +#define __itt_markW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_mark_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Use this if necessary to create a "discrete" user event type (mark) for process + * rather then for one thread + * @see int __itt_mark(__itt_mark_type mt, const char* parameter); + */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +int ITTAPI __itt_mark_globalA(__itt_mark_type mt, const char *parameter); +int ITTAPI __itt_mark_globalW(__itt_mark_type mt, const wchar_t *parameter); +#if defined(UNICODE) || defined(_UNICODE) +# define __itt_mark_global __itt_mark_globalW +# define __itt_mark_global_ptr __itt_mark_globalW_ptr +#else /* UNICODE */ +# define __itt_mark_global __itt_mark_globalA +# define __itt_mark_global_ptr __itt_mark_globalA_ptr +#endif /* UNICODE */ +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +int ITTAPI __itt_mark_global(__itt_mark_type mt, const char *parameter); +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +#if ITT_PLATFORM==ITT_PLATFORM_WIN +ITT_STUB(ITTAPI, int, mark_globalA, (__itt_mark_type mt, const char *parameter)) +ITT_STUB(ITTAPI, int, mark_globalW, (__itt_mark_type mt, const wchar_t *parameter)) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +ITT_STUB(ITTAPI, int, mark_global, (__itt_mark_type mt, const char *parameter)) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_mark_globalA ITTNOTIFY_DATA(mark_globalA) +#define __itt_mark_globalA_ptr ITTNOTIFY_NAME(mark_globalA) +#define __itt_mark_globalW ITTNOTIFY_DATA(mark_globalW) +#define __itt_mark_globalW_ptr ITTNOTIFY_NAME(mark_globalW) +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_mark_global ITTNOTIFY_DATA(mark_global) +#define __itt_mark_global_ptr ITTNOTIFY_NAME(mark_global) +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#else /* INTEL_NO_ITTNOTIFY_API */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_mark_globalA(mt, parameter) (int)0 +#define __itt_mark_globalA_ptr 0 +#define __itt_mark_globalW(mt, parameter) (int)0 +#define __itt_mark_globalW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_mark_global(mt, parameter) (int)0 +#define __itt_mark_global_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#if ITT_PLATFORM==ITT_PLATFORM_WIN +#define __itt_mark_globalA_ptr 0 +#define __itt_mark_globalW_ptr 0 +#else /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#define __itt_mark_global_ptr 0 +#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */ +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Creates an "end" point for "continuous" mark with specified name. + * + * - Returns zero value in case of success, non-zero value otherwise. + * Also returns non-zero value when preceding "begin" point for the + * mark with the same name failed to be created or not created. + * - The mark of "continuous" type is placed to collection results in + * case of success. It appears in overtime view(s) as a special tick + * sign (different from "discrete" mark) together with line from + * corresponding "begin" mark to "end" mark. + * @note Continuous marks can overlap and be nested inside each other. + * Discrete mark can be nested inside marked region + * @param[in] mt - mark, created by __itt_mark_create(const char* name) function + * @return Returns zero value in case of success, non-zero value otherwise. + */ +int ITTAPI __itt_mark_off(__itt_mark_type mt); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUB(ITTAPI, int, mark_off, (__itt_mark_type mt)) +#define __itt_mark_off ITTNOTIFY_DATA(mark_off) +#define __itt_mark_off_ptr ITTNOTIFY_NAME(mark_off) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_mark_off(mt) (int)0 +#define __itt_mark_off_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_mark_off_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Use this if necessary to create an "end" point for mark of process + * @see int __itt_mark_off(__itt_mark_type mt); + */ +int ITTAPI __itt_mark_global_off(__itt_mark_type mt); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUB(ITTAPI, int, mark_global_off, (__itt_mark_type mt)) +#define __itt_mark_global_off ITTNOTIFY_DATA(mark_global_off) +#define __itt_mark_global_off_ptr ITTNOTIFY_NAME(mark_global_off) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_mark_global_off(mt) (int)0 +#define __itt_mark_global_off_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_mark_global_off_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ +/** @} marks group */ + +/** + * @defgroup counters_internal Counters + * @ingroup internal + * Counters group + * @{ + */ + + +/** + * @defgroup stitch Stack Stitching + * @ingroup internal + * Stack Stitching group + * @{ + */ +/** + * @brief opaque structure for counter identification + */ +typedef struct ___itt_caller *__itt_caller; + +/** + * @brief Create the stitch point e.g. a point in call stack where other stacks should be stitched to. + * The function returns a unique identifier which is used to match the cut points with corresponding stitch points. + */ +__itt_caller ITTAPI __itt_stack_caller_create(void); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUB(ITTAPI, __itt_caller, stack_caller_create, (void)) +#define __itt_stack_caller_create ITTNOTIFY_DATA(stack_caller_create) +#define __itt_stack_caller_create_ptr ITTNOTIFY_NAME(stack_caller_create) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_stack_caller_create() (__itt_caller)0 +#define __itt_stack_caller_create_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_stack_caller_create_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Destroy the inforamtion about stitch point identified by the pointer previously returned by __itt_stack_caller_create() + */ +void ITTAPI __itt_stack_caller_destroy(__itt_caller id); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, stack_caller_destroy, (__itt_caller id)) +#define __itt_stack_caller_destroy ITTNOTIFY_VOID(stack_caller_destroy) +#define __itt_stack_caller_destroy_ptr ITTNOTIFY_NAME(stack_caller_destroy) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_stack_caller_destroy(id) +#define __itt_stack_caller_destroy_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_stack_caller_destroy_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief Sets the cut point. Stack from each event which occurs after this call will be cut + * at the same stack level the function was called and stitched to the corresponding stitch point. + */ +void ITTAPI __itt_stack_callee_enter(__itt_caller id); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, stack_callee_enter, (__itt_caller id)) +#define __itt_stack_callee_enter ITTNOTIFY_VOID(stack_callee_enter) +#define __itt_stack_callee_enter_ptr ITTNOTIFY_NAME(stack_callee_enter) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_stack_callee_enter(id) +#define __itt_stack_callee_enter_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_stack_callee_enter_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** + * @brief This function eliminates the cut point which was set by latest __itt_stack_callee_enter(). + */ +void ITTAPI __itt_stack_callee_leave(__itt_caller id); + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +ITT_STUBV(ITTAPI, void, stack_callee_leave, (__itt_caller id)) +#define __itt_stack_callee_leave ITTNOTIFY_VOID(stack_callee_leave) +#define __itt_stack_callee_leave_ptr ITTNOTIFY_NAME(stack_callee_leave) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_stack_callee_leave(id) +#define __itt_stack_callee_leave_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_stack_callee_leave_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +/** @} stitch group */ + +/* ***************************************************************************************************************************** */ + +#include <stdarg.h> + +/** @cond exclude_from_documentation */ +typedef enum __itt_error_code +{ + __itt_error_success = 0, /*!< no error */ + __itt_error_no_module = 1, /*!< module can't be loaded */ + /* %1$s -- library name; win: %2$d -- system error code; unx: %2$s -- system error message. */ + __itt_error_no_symbol = 2, /*!< symbol not found */ + /* %1$s -- library name, %2$s -- symbol name. */ + __itt_error_unknown_group = 3, /*!< unknown group specified */ + /* %1$s -- env var name, %2$s -- group name. */ + __itt_error_cant_read_env = 4, /*!< GetEnvironmentVariable() failed */ + /* %1$s -- env var name, %2$d -- system error. */ + __itt_error_env_too_long = 5, /*!< variable value too long */ + /* %1$s -- env var name, %2$d -- actual length of the var, %3$d -- max allowed length. */ + __itt_error_system = 6 /*!< pthread_mutexattr_init or pthread_mutex_init failed */ + /* %1$s -- function name, %2$d -- errno. */ +} __itt_error_code; + +typedef void (__itt_error_handler_t)(__itt_error_code code, va_list); +__itt_error_handler_t* __itt_set_error_handler(__itt_error_handler_t*); + +const char* ITTAPI __itt_api_version(void); +/** @endcond */ + +/** @cond exclude_from_documentation */ +#ifndef INTEL_NO_MACRO_BODY +#ifndef INTEL_NO_ITTNOTIFY_API +#define __itt_error_handler ITT_JOIN(INTEL_ITTNOTIFY_PREFIX, error_handler) +void __itt_error_handler(__itt_error_code code, va_list args); +extern const int ITTNOTIFY_NAME(err); +#define __itt_err ITTNOTIFY_NAME(err) +ITT_STUB(ITTAPI, const char*, api_version, (void)) +#define __itt_api_version ITTNOTIFY_DATA(api_version) +#define __itt_api_version_ptr ITTNOTIFY_NAME(api_version) +#else /* INTEL_NO_ITTNOTIFY_API */ +#define __itt_api_version() (const char*)0 +#define __itt_api_version_ptr 0 +#endif /* INTEL_NO_ITTNOTIFY_API */ +#else /* INTEL_NO_MACRO_BODY */ +#define __itt_api_version_ptr 0 +#endif /* INTEL_NO_MACRO_BODY */ +/** @endcond */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* _ITTNOTIFY_PRIVATE_ */ + +#endif /* INTEL_ITTNOTIFY_API_PRIVATE */ + +// clang-format on diff --git a/mozglue/baseprofiler/lul/AutoObjectMapper.cpp b/mozglue/baseprofiler/lul/AutoObjectMapper.cpp new file mode 100644 index 0000000000..0037c943aa --- /dev/null +++ b/mozglue/baseprofiler/lul/AutoObjectMapper.cpp @@ -0,0 +1,80 @@ +/* -*- 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 <sys/mman.h> +#include <unistd.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> + +#include "mozilla/Assertions.h" +#include "mozilla/Sprintf.h" + +#include "BaseProfiler.h" +#include "PlatformMacros.h" +#include "AutoObjectMapper.h" + +// A helper function for creating failure error messages in +// AutoObjectMapper*::Map. +static void failedToMessage(void (*aLog)(const char*), const char* aHowFailed, + std::string aFileName) { + char buf[300]; + SprintfLiteral(buf, "AutoObjectMapper::Map: Failed to %s \'%s\'", aHowFailed, + aFileName.c_str()); + buf[sizeof(buf) - 1] = 0; + aLog(buf); +} + +AutoObjectMapperPOSIX::AutoObjectMapperPOSIX(void (*aLog)(const char*)) + : mImage(nullptr), mSize(0), mLog(aLog), mIsMapped(false) {} + +AutoObjectMapperPOSIX::~AutoObjectMapperPOSIX() { + if (!mIsMapped) { + // There's nothing to do. + MOZ_ASSERT(!mImage); + MOZ_ASSERT(mSize == 0); + return; + } + MOZ_ASSERT(mSize > 0); + // The following assertion doesn't necessarily have to be true, + // but we assume (reasonably enough) that no mmap facility would + // be crazy enough to map anything at page zero. + MOZ_ASSERT(mImage); + munmap(mImage, mSize); +} + +bool AutoObjectMapperPOSIX::Map(/*OUT*/ void** start, /*OUT*/ size_t* length, + std::string fileName) { + MOZ_ASSERT(!mIsMapped); + + int fd = open(fileName.c_str(), O_RDONLY); + if (fd == -1) { + failedToMessage(mLog, "open", fileName); + return false; + } + + struct stat st; + int err = fstat(fd, &st); + size_t sz = (err == 0) ? st.st_size : 0; + if (err != 0 || sz == 0) { + failedToMessage(mLog, "fstat", fileName); + close(fd); + return false; + } + + void* image = mmap(nullptr, sz, PROT_READ, MAP_SHARED, fd, 0); + if (image == MAP_FAILED) { + failedToMessage(mLog, "mmap", fileName); + close(fd); + return false; + } + + close(fd); + mIsMapped = true; + mImage = *start = image; + mSize = *length = sz; + return true; +} diff --git a/mozglue/baseprofiler/lul/AutoObjectMapper.h b/mozglue/baseprofiler/lul/AutoObjectMapper.h new file mode 100644 index 0000000000..f63aa43e0e --- /dev/null +++ b/mozglue/baseprofiler/lul/AutoObjectMapper.h @@ -0,0 +1,64 @@ +/* -*- 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/. */ + +#ifndef AutoObjectMapper_h +#define AutoObjectMapper_h + +#include <string> + +#include "mozilla/Attributes.h" +#include "PlatformMacros.h" + +// A (nearly-) RAII class that maps an object in and then unmaps it on +// destruction. This base class version uses the "normal" POSIX +// functions: open, fstat, close, mmap, munmap. + +class MOZ_STACK_CLASS AutoObjectMapperPOSIX { + public: + // The constructor does not attempt to map the file, because that + // might fail. Instead, once the object has been constructed, + // call Map() to attempt the mapping. There is no corresponding + // Unmap() since the unmapping is done in the destructor. Failure + // messages are sent to |aLog|. + explicit AutoObjectMapperPOSIX(void (*aLog)(const char*)); + + // Unmap the file on destruction of this object. + ~AutoObjectMapperPOSIX(); + + // Map |fileName| into the address space and return the mapping + // extents. If the file is zero sized this will fail. The file is + // mapped read-only and private. Returns true iff the mapping + // succeeded, in which case *start and *length hold its extent. + // Once a call to Map succeeds, all subsequent calls to it will + // fail. + bool Map(/*OUT*/ void** start, /*OUT*/ size_t* length, std::string fileName); + + protected: + // If we are currently holding a mapped object, these record the + // mapped address range. + void* mImage; + size_t mSize; + + // A logging sink, for complaining about mapping failures. + void (*mLog)(const char*); + + private: + // Are we currently holding a mapped object? This is private to + // the base class. Derived classes need to have their own way to + // track whether they are holding a mapped object. + bool mIsMapped; + + // Disable copying and assignment. + AutoObjectMapperPOSIX(const AutoObjectMapperPOSIX&); + AutoObjectMapperPOSIX& operator=(const AutoObjectMapperPOSIX&); + // Disable heap allocation of this class. + void* operator new(size_t); + void* operator new[](size_t); + void operator delete(void*); + void operator delete[](void*); +}; + +#endif // AutoObjectMapper_h diff --git a/mozglue/baseprofiler/lul/LulCommon.cpp b/mozglue/baseprofiler/lul/LulCommon.cpp new file mode 100644 index 0000000000..f014892a57 --- /dev/null +++ b/mozglue/baseprofiler/lul/LulCommon.cpp @@ -0,0 +1,102 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ + +// Copyright (c) 2011, 2013 Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// Original author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com> + +// This file is derived from the following files in +// toolkit/crashreporter/google-breakpad: +// src/common/module.cc +// src/common/unique_string.cc + +// There's no internal-only interface for LulCommon. Hence include +// the external interface directly. +#include "LulCommonExt.h" + +#include <stdlib.h> +#include <string.h> + +#include <string> +#include <map> + +#include "BaseProfiler.h" + +namespace lul { + +using std::string; + +//////////////////////////////////////////////////////////////// +// Module +// +Module::Module(const string& name, const string& os, const string& architecture, + const string& id) + : name_(name), os_(os), architecture_(architecture), id_(id) {} + +Module::~Module() {} + +//////////////////////////////////////////////////////////////// +// UniqueString +// +class UniqueString { + public: + explicit UniqueString(string str) { str_ = strdup(str.c_str()); } + ~UniqueString() { free(reinterpret_cast<void*>(const_cast<char*>(str_))); } + const char* str_; +}; + +const char* FromUniqueString(const UniqueString* ustr) { return ustr->str_; } + +bool IsEmptyUniqueString(const UniqueString* ustr) { + return (ustr->str_)[0] == '\0'; +} + +//////////////////////////////////////////////////////////////// +// UniqueStringUniverse +// +UniqueStringUniverse::~UniqueStringUniverse() { + for (std::map<string, UniqueString*>::iterator it = map_.begin(); + it != map_.end(); it++) { + delete it->second; + } +} + +const UniqueString* UniqueStringUniverse::ToUniqueString(string str) { + std::map<string, UniqueString*>::iterator it = map_.find(str); + if (it == map_.end()) { + UniqueString* ustr = new UniqueString(str); + map_[str] = ustr; + return ustr; + } else { + return it->second; + } +} + +} // namespace lul diff --git a/mozglue/baseprofiler/lul/LulCommonExt.h b/mozglue/baseprofiler/lul/LulCommonExt.h new file mode 100644 index 0000000000..b20a7321ff --- /dev/null +++ b/mozglue/baseprofiler/lul/LulCommonExt.h @@ -0,0 +1,509 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ + +// Copyright (c) 2006, 2010, 2012, 2013 Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// Original author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com> + +// module.h: Define google_breakpad::Module. A Module holds debugging +// information, and can write that information out as a Breakpad +// symbol file. + +// (C) Copyright Greg Colvin and Beman Dawes 1998, 1999. +// Copyright (c) 2001, 2002 Peter Dimov +// +// Permission to copy, use, modify, sell and distribute this software +// is granted provided this copyright notice appears in all copies. +// This software is provided "as is" without express or implied +// warranty, and with no claim as to its suitability for any purpose. +// +// See http://www.boost.org/libs/smart_ptr/scoped_ptr.htm for documentation. +// + +// This file is derived from the following files in +// toolkit/crashreporter/google-breakpad: +// src/common/unique_string.h +// src/common/scoped_ptr.h +// src/common/module.h + +// External interface for the "Common" component of LUL. + +#ifndef LulCommonExt_h +#define LulCommonExt_h + +#include <stdlib.h> +#include <stdio.h> +#include <stdint.h> + +#include <string> +#include <map> +#include <vector> +#include <cstddef> // for std::ptrdiff_t + +#include "mozilla/Assertions.h" + +namespace lul { + +using std::map; +using std::string; + +//////////////////////////////////////////////////////////////// +// UniqueString +// + +// Abstract type +class UniqueString; + +// Get the contained C string (debugging only) +const char* FromUniqueString(const UniqueString*); + +// Is the given string empty (that is, "") ? +bool IsEmptyUniqueString(const UniqueString*); + +//////////////////////////////////////////////////////////////// +// UniqueStringUniverse +// + +// All UniqueStrings live in some specific UniqueStringUniverse. +class UniqueStringUniverse { + public: + UniqueStringUniverse() {} + ~UniqueStringUniverse(); + // Convert a |string| to a UniqueString, that lives in this universe. + const UniqueString* ToUniqueString(string str); + + private: + map<string, UniqueString*> map_; +}; + +//////////////////////////////////////////////////////////////// +// GUID +// + +typedef struct { + uint32_t data1; + uint16_t data2; + uint16_t data3; + uint8_t data4[8]; +} MDGUID; // GUID + +typedef MDGUID GUID; + +//////////////////////////////////////////////////////////////// +// scoped_ptr +// + +// scoped_ptr mimics a built-in pointer except that it guarantees deletion +// of the object pointed to, either on destruction of the scoped_ptr or via +// an explicit reset(). scoped_ptr is a simple solution for simple needs; +// use shared_ptr or std::auto_ptr if your needs are more complex. + +// *** NOTE *** +// If your scoped_ptr is a class member of class FOO pointing to a +// forward declared type BAR (as shown below), then you MUST use a non-inlined +// version of the destructor. The destructor of a scoped_ptr (called from +// FOO's destructor) must have a complete definition of BAR in order to +// destroy it. Example: +// +// -- foo.h -- +// class BAR; +// +// class FOO { +// public: +// FOO(); +// ~FOO(); // Required for sources that instantiate class FOO to compile! +// +// private: +// scoped_ptr<BAR> bar_; +// }; +// +// -- foo.cc -- +// #include "foo.h" +// FOO::~FOO() {} // Empty, but must be non-inlined to FOO's class definition. + +// scoped_ptr_malloc added by Google +// When one of these goes out of scope, instead of doing a delete or +// delete[], it calls free(). scoped_ptr_malloc<char> is likely to see +// much more use than any other specializations. + +// release() added by Google +// Use this to conditionally transfer ownership of a heap-allocated object +// to the caller, usually on method success. + +template <typename T> +class scoped_ptr { + private: + T* ptr; + + scoped_ptr(scoped_ptr const&); + scoped_ptr& operator=(scoped_ptr const&); + + public: + typedef T element_type; + + explicit scoped_ptr(T* p = 0) : ptr(p) {} + + ~scoped_ptr() { delete ptr; } + + void reset(T* p = 0) { + if (ptr != p) { + delete ptr; + ptr = p; + } + } + + T& operator*() const { + MOZ_ASSERT(ptr != 0); + return *ptr; + } + + T* operator->() const { + MOZ_ASSERT(ptr != 0); + return ptr; + } + + bool operator==(T* p) const { return ptr == p; } + + bool operator!=(T* p) const { return ptr != p; } + + T* get() const { return ptr; } + + void swap(scoped_ptr& b) { + T* tmp = b.ptr; + b.ptr = ptr; + ptr = tmp; + } + + T* release() { + T* tmp = ptr; + ptr = 0; + return tmp; + } + + private: + // no reason to use these: each scoped_ptr should have its own object + template <typename U> + bool operator==(scoped_ptr<U> const& p) const; + template <typename U> + bool operator!=(scoped_ptr<U> const& p) const; +}; + +template <typename T> +inline void swap(scoped_ptr<T>& a, scoped_ptr<T>& b) { + a.swap(b); +} + +template <typename T> +inline bool operator==(T* p, const scoped_ptr<T>& b) { + return p == b.get(); +} + +template <typename T> +inline bool operator!=(T* p, const scoped_ptr<T>& b) { + return p != b.get(); +} + +// scoped_array extends scoped_ptr to arrays. Deletion of the array pointed to +// is guaranteed, either on destruction of the scoped_array or via an explicit +// reset(). Use shared_array or std::vector if your needs are more complex. + +template <typename T> +class scoped_array { + private: + T* ptr; + + scoped_array(scoped_array const&); + scoped_array& operator=(scoped_array const&); + + public: + typedef T element_type; + + explicit scoped_array(T* p = 0) : ptr(p) {} + + ~scoped_array() { delete[] ptr; } + + void reset(T* p = 0) { + if (ptr != p) { + delete[] ptr; + ptr = p; + } + } + + T& operator[](std::ptrdiff_t i) const { + MOZ_ASSERT(ptr != 0); + MOZ_ASSERT(i >= 0); + return ptr[i]; + } + + bool operator==(T* p) const { return ptr == p; } + + bool operator!=(T* p) const { return ptr != p; } + + T* get() const { return ptr; } + + void swap(scoped_array& b) { + T* tmp = b.ptr; + b.ptr = ptr; + ptr = tmp; + } + + T* release() { + T* tmp = ptr; + ptr = 0; + return tmp; + } + + private: + // no reason to use these: each scoped_array should have its own object + template <typename U> + bool operator==(scoped_array<U> const& p) const; + template <typename U> + bool operator!=(scoped_array<U> const& p) const; +}; + +template <class T> +inline void swap(scoped_array<T>& a, scoped_array<T>& b) { + a.swap(b); +} + +template <typename T> +inline bool operator==(T* p, const scoped_array<T>& b) { + return p == b.get(); +} + +template <typename T> +inline bool operator!=(T* p, const scoped_array<T>& b) { + return p != b.get(); +} + +// This class wraps the c library function free() in a class that can be +// passed as a template argument to scoped_ptr_malloc below. +class ScopedPtrMallocFree { + public: + inline void operator()(void* x) const { free(x); } +}; + +// scoped_ptr_malloc<> is similar to scoped_ptr<>, but it accepts a +// second template argument, the functor used to free the object. + +template <typename T, typename FreeProc = ScopedPtrMallocFree> +class scoped_ptr_malloc { + private: + T* ptr; + + scoped_ptr_malloc(scoped_ptr_malloc const&); + scoped_ptr_malloc& operator=(scoped_ptr_malloc const&); + + public: + typedef T element_type; + + explicit scoped_ptr_malloc(T* p = 0) : ptr(p) {} + + ~scoped_ptr_malloc() { free_((void*)ptr); } + + void reset(T* p = 0) { + if (ptr != p) { + free_((void*)ptr); + ptr = p; + } + } + + T& operator*() const { + MOZ_ASSERT(ptr != 0); + return *ptr; + } + + T* operator->() const { + MOZ_ASSERT(ptr != 0); + return ptr; + } + + bool operator==(T* p) const { return ptr == p; } + + bool operator!=(T* p) const { return ptr != p; } + + T* get() const { return ptr; } + + void swap(scoped_ptr_malloc& b) { + T* tmp = b.ptr; + b.ptr = ptr; + ptr = tmp; + } + + T* release() { + T* tmp = ptr; + ptr = 0; + return tmp; + } + + private: + // no reason to use these: each scoped_ptr_malloc should have its own object + template <typename U, typename GP> + bool operator==(scoped_ptr_malloc<U, GP> const& p) const; + template <typename U, typename GP> + bool operator!=(scoped_ptr_malloc<U, GP> const& p) const; + + static FreeProc const free_; +}; + +template <typename T, typename FP> +FP const scoped_ptr_malloc<T, FP>::free_ = FP(); + +template <typename T, typename FP> +inline void swap(scoped_ptr_malloc<T, FP>& a, scoped_ptr_malloc<T, FP>& b) { + a.swap(b); +} + +template <typename T, typename FP> +inline bool operator==(T* p, const scoped_ptr_malloc<T, FP>& b) { + return p == b.get(); +} + +template <typename T, typename FP> +inline bool operator!=(T* p, const scoped_ptr_malloc<T, FP>& b) { + return p != b.get(); +} + +//////////////////////////////////////////////////////////////// +// Module +// + +// A Module represents the contents of a module, and supports methods +// for adding information produced by parsing STABS or DWARF data +// --- possibly both from the same file --- and then writing out the +// unified contents as a Breakpad-format symbol file. +class Module { + public: + // The type of addresses and sizes in a symbol table. + typedef uint64_t Address; + + // Representation of an expression. This can either be a postfix + // expression, in which case it is stored as a string, or a simple + // expression of the form (identifier + imm) or *(identifier + imm). + // It can also be invalid (denoting "no value"). + enum ExprHow { kExprInvalid = 1, kExprPostfix, kExprSimple, kExprSimpleMem }; + + struct Expr { + // Construct a simple-form expression + Expr(const UniqueString* ident, long offset, bool deref) { + if (IsEmptyUniqueString(ident)) { + Expr(); + } else { + postfix_ = ""; + ident_ = ident; + offset_ = offset; + how_ = deref ? kExprSimpleMem : kExprSimple; + } + } + + // Construct an invalid expression + Expr() { + postfix_ = ""; + ident_ = nullptr; + offset_ = 0; + how_ = kExprInvalid; + } + + // Return the postfix expression string, either directly, + // if this is a postfix expression, or by synthesising it + // for a simple expression. + std::string getExprPostfix() const { + switch (how_) { + case kExprPostfix: + return postfix_; + case kExprSimple: + case kExprSimpleMem: { + char buf[40]; + sprintf(buf, " %ld %c%s", labs(offset_), offset_ < 0 ? '-' : '+', + how_ == kExprSimple ? "" : " ^"); + return std::string(FromUniqueString(ident_)) + std::string(buf); + } + case kExprInvalid: + default: + MOZ_ASSERT(0 && "getExprPostfix: invalid Module::Expr type"); + return "Expr::genExprPostfix: kExprInvalid"; + } + } + + // The identifier that gives the starting value for simple expressions. + const UniqueString* ident_; + // The offset to add for simple expressions. + long offset_; + // The Postfix expression string to evaluate for non-simple expressions. + std::string postfix_; + // The operation expressed by this expression. + ExprHow how_; + }; + + // A map from register names to expressions that recover + // their values. This can represent a complete set of rules to + // follow at some address, or a set of changes to be applied to an + // extant set of rules. + // NOTE! there are two completely different types called RuleMap. This + // is one of them. + typedef std::map<const UniqueString*, Expr> RuleMap; + + // A map from addresses to RuleMaps, representing changes that take + // effect at given addresses. + typedef std::map<Address, RuleMap> RuleChangeMap; + + // A range of 'STACK CFI' stack walking information. An instance of + // this structure corresponds to a 'STACK CFI INIT' record and the + // subsequent 'STACK CFI' records that fall within its range. + struct StackFrameEntry { + // The starting address and number of bytes of machine code this + // entry covers. + Address address, size; + + // The initial register recovery rules, in force at the starting + // address. + RuleMap initial_rules; + + // A map from addresses to rule changes. To find the rules in + // force at a given address, start with initial_rules, and then + // apply the changes given in this map for all addresses up to and + // including the address you're interested in. + RuleChangeMap rule_changes; + }; + + // Create a new module with the given name, operating system, + // architecture, and ID string. + Module(const std::string& name, const std::string& os, + const std::string& architecture, const std::string& id); + ~Module(); + + private: + // Module header entries. + std::string name_, os_, architecture_, id_; +}; + +} // namespace lul + +#endif // LulCommonExt_h diff --git a/mozglue/baseprofiler/lul/LulDwarf.cpp b/mozglue/baseprofiler/lul/LulDwarf.cpp new file mode 100644 index 0000000000..c83296fc62 --- /dev/null +++ b/mozglue/baseprofiler/lul/LulDwarf.cpp @@ -0,0 +1,2252 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ + +// Copyright (c) 2010 Google Inc. All Rights Reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// CFI reader author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com> +// Original author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com> + +// Implementation of dwarf2reader::LineInfo, dwarf2reader::CompilationUnit, +// and dwarf2reader::CallFrameInfo. See dwarf2reader.h for details. + +// This file is derived from the following files in +// toolkit/crashreporter/google-breakpad: +// src/common/dwarf/bytereader.cc +// src/common/dwarf/dwarf2reader.cc +// src/common/dwarf_cfi_to_module.cc + +#include <stdint.h> +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +#include <map> +#include <stack> +#include <string> + +#include "mozilla/Assertions.h" +#include "mozilla/Sprintf.h" + +#include "LulCommonExt.h" +#include "LulDwarfInt.h" + +// Set this to 1 for verbose logging +#define DEBUG_DWARF 0 + +namespace lul { + +using std::string; + +ByteReader::ByteReader(enum Endianness endian) + : offset_reader_(NULL), + address_reader_(NULL), + endian_(endian), + address_size_(0), + offset_size_(0), + have_section_base_(), + have_text_base_(), + have_data_base_(), + have_function_base_() {} + +ByteReader::~ByteReader() {} + +void ByteReader::SetOffsetSize(uint8 size) { + offset_size_ = size; + MOZ_ASSERT(size == 4 || size == 8); + if (size == 4) { + this->offset_reader_ = &ByteReader::ReadFourBytes; + } else { + this->offset_reader_ = &ByteReader::ReadEightBytes; + } +} + +void ByteReader::SetAddressSize(uint8 size) { + address_size_ = size; + MOZ_ASSERT(size == 4 || size == 8); + if (size == 4) { + this->address_reader_ = &ByteReader::ReadFourBytes; + } else { + this->address_reader_ = &ByteReader::ReadEightBytes; + } +} + +uint64 ByteReader::ReadInitialLength(const char* start, size_t* len) { + const uint64 initial_length = ReadFourBytes(start); + start += 4; + + // In DWARF2/3, if the initial length is all 1 bits, then the offset + // size is 8 and we need to read the next 8 bytes for the real length. + if (initial_length == 0xffffffff) { + SetOffsetSize(8); + *len = 12; + return ReadOffset(start); + } else { + SetOffsetSize(4); + *len = 4; + } + return initial_length; +} + +bool ByteReader::ValidEncoding(DwarfPointerEncoding encoding) const { + if (encoding == DW_EH_PE_omit) return true; + if (encoding == DW_EH_PE_aligned) return true; + if ((encoding & 0x7) > DW_EH_PE_udata8) return false; + if ((encoding & 0x70) > DW_EH_PE_funcrel) return false; + return true; +} + +bool ByteReader::UsableEncoding(DwarfPointerEncoding encoding) const { + switch (encoding & 0x70) { + case DW_EH_PE_absptr: + return true; + case DW_EH_PE_pcrel: + return have_section_base_; + case DW_EH_PE_textrel: + return have_text_base_; + case DW_EH_PE_datarel: + return have_data_base_; + case DW_EH_PE_funcrel: + return have_function_base_; + default: + return false; + } +} + +uint64 ByteReader::ReadEncodedPointer(const char* buffer, + DwarfPointerEncoding encoding, + size_t* len) const { + // UsableEncoding doesn't approve of DW_EH_PE_omit, so we shouldn't + // see it here. + MOZ_ASSERT(encoding != DW_EH_PE_omit); + + // The Linux Standards Base 4.0 does not make this clear, but the + // GNU tools (gcc/unwind-pe.h; readelf/dwarf.c; gdb/dwarf2-frame.c) + // agree that aligned pointers are always absolute, machine-sized, + // machine-signed pointers. + if (encoding == DW_EH_PE_aligned) { + MOZ_ASSERT(have_section_base_); + + // We don't need to align BUFFER in *our* address space. Rather, we + // need to find the next position in our buffer that would be aligned + // when the .eh_frame section the buffer contains is loaded into the + // program's memory. So align assuming that buffer_base_ gets loaded at + // address section_base_, where section_base_ itself may or may not be + // aligned. + + // First, find the offset to START from the closest prior aligned + // address. + uint64 skew = section_base_ & (AddressSize() - 1); + // Now find the offset from that aligned address to buffer. + uint64 offset = skew + (buffer - buffer_base_); + // Round up to the next boundary. + uint64 aligned = (offset + AddressSize() - 1) & -AddressSize(); + // Convert back to a pointer. + const char* aligned_buffer = buffer_base_ + (aligned - skew); + // Finally, store the length and actually fetch the pointer. + *len = aligned_buffer - buffer + AddressSize(); + return ReadAddress(aligned_buffer); + } + + // Extract the value first, ignoring whether it's a pointer or an + // offset relative to some base. + uint64 offset; + switch (encoding & 0x0f) { + case DW_EH_PE_absptr: + // DW_EH_PE_absptr is weird, as it is used as a meaningful value for + // both the high and low nybble of encoding bytes. When it appears in + // the high nybble, it means that the pointer is absolute, not an + // offset from some base address. When it appears in the low nybble, + // as here, it means that the pointer is stored as a normal + // machine-sized and machine-signed address. A low nybble of + // DW_EH_PE_absptr does not imply that the pointer is absolute; it is + // correct for us to treat the value as an offset from a base address + // if the upper nybble is not DW_EH_PE_absptr. + offset = ReadAddress(buffer); + *len = AddressSize(); + break; + + case DW_EH_PE_uleb128: + offset = ReadUnsignedLEB128(buffer, len); + break; + + case DW_EH_PE_udata2: + offset = ReadTwoBytes(buffer); + *len = 2; + break; + + case DW_EH_PE_udata4: + offset = ReadFourBytes(buffer); + *len = 4; + break; + + case DW_EH_PE_udata8: + offset = ReadEightBytes(buffer); + *len = 8; + break; + + case DW_EH_PE_sleb128: + offset = ReadSignedLEB128(buffer, len); + break; + + case DW_EH_PE_sdata2: + offset = ReadTwoBytes(buffer); + // Sign-extend from 16 bits. + offset = (offset ^ 0x8000) - 0x8000; + *len = 2; + break; + + case DW_EH_PE_sdata4: + offset = ReadFourBytes(buffer); + // Sign-extend from 32 bits. + offset = (offset ^ 0x80000000ULL) - 0x80000000ULL; + *len = 4; + break; + + case DW_EH_PE_sdata8: + // No need to sign-extend; this is the full width of our type. + offset = ReadEightBytes(buffer); + *len = 8; + break; + + default: + abort(); + } + + // Find the appropriate base address. + uint64 base; + switch (encoding & 0x70) { + case DW_EH_PE_absptr: + base = 0; + break; + + case DW_EH_PE_pcrel: + MOZ_ASSERT(have_section_base_); + base = section_base_ + (buffer - buffer_base_); + break; + + case DW_EH_PE_textrel: + MOZ_ASSERT(have_text_base_); + base = text_base_; + break; + + case DW_EH_PE_datarel: + MOZ_ASSERT(have_data_base_); + base = data_base_; + break; + + case DW_EH_PE_funcrel: + MOZ_ASSERT(have_function_base_); + base = function_base_; + break; + + default: + abort(); + } + + uint64 pointer = base + offset; + + // Remove inappropriate upper bits. + if (AddressSize() == 4) + pointer = pointer & 0xffffffff; + else + MOZ_ASSERT(AddressSize() == sizeof(uint64)); + + return pointer; +} + +// A DWARF rule for recovering the address or value of a register, or +// computing the canonical frame address. There is one subclass of this for +// each '*Rule' member function in CallFrameInfo::Handler. +// +// It's annoying that we have to handle Rules using pointers (because +// the concrete instances can have an arbitrary size). They're small, +// so it would be much nicer if we could just handle them by value +// instead of fretting about ownership and destruction. +// +// It seems like all these could simply be instances of std::tr1::bind, +// except that we need instances to be EqualityComparable, too. +// +// This could logically be nested within State, but then the qualified names +// get horrendous. +class CallFrameInfo::Rule { + public: + virtual ~Rule() {} + + // Tell HANDLER that, at ADDRESS in the program, REG can be + // recovered using this rule. If REG is kCFARegister, then this rule + // describes how to compute the canonical frame address. Return what the + // HANDLER member function returned. + virtual bool Handle(Handler* handler, uint64 address, int reg) const = 0; + + // Equality on rules. We use these to decide which rules we need + // to report after a DW_CFA_restore_state instruction. + virtual bool operator==(const Rule& rhs) const = 0; + + bool operator!=(const Rule& rhs) const { return !(*this == rhs); } + + // Return a pointer to a copy of this rule. + virtual Rule* Copy() const = 0; + + // If this is a base+offset rule, change its base register to REG. + // Otherwise, do nothing. (Ugly, but required for DW_CFA_def_cfa_register.) + virtual void SetBaseRegister(unsigned reg) {} + + // If this is a base+offset rule, change its offset to OFFSET. Otherwise, + // do nothing. (Ugly, but required for DW_CFA_def_cfa_offset.) + virtual void SetOffset(long long offset) {} + + // A RTTI workaround, to make it possible to implement equality + // comparisons on classes derived from this one. + enum CFIRTag { + CFIR_UNDEFINED_RULE, + CFIR_SAME_VALUE_RULE, + CFIR_OFFSET_RULE, + CFIR_VAL_OFFSET_RULE, + CFIR_REGISTER_RULE, + CFIR_EXPRESSION_RULE, + CFIR_VAL_EXPRESSION_RULE + }; + + // Produce the tag that identifies the child class of this object. + virtual CFIRTag getTag() const = 0; +}; + +// Rule: the value the register had in the caller cannot be recovered. +class CallFrameInfo::UndefinedRule : public CallFrameInfo::Rule { + public: + UndefinedRule() {} + ~UndefinedRule() {} + CFIRTag getTag() const override { return CFIR_UNDEFINED_RULE; } + bool Handle(Handler* handler, uint64 address, int reg) const override { + return handler->UndefinedRule(address, reg); + } + bool operator==(const Rule& rhs) const override { + if (rhs.getTag() != CFIR_UNDEFINED_RULE) return false; + return true; + } + Rule* Copy() const override { return new UndefinedRule(*this); } +}; + +// Rule: the register's value is the same as that it had in the caller. +class CallFrameInfo::SameValueRule : public CallFrameInfo::Rule { + public: + SameValueRule() {} + ~SameValueRule() {} + CFIRTag getTag() const override { return CFIR_SAME_VALUE_RULE; } + bool Handle(Handler* handler, uint64 address, int reg) const override { + return handler->SameValueRule(address, reg); + } + bool operator==(const Rule& rhs) const override { + if (rhs.getTag() != CFIR_SAME_VALUE_RULE) return false; + return true; + } + Rule* Copy() const override { return new SameValueRule(*this); } +}; + +// Rule: the register is saved at OFFSET from BASE_REGISTER. BASE_REGISTER +// may be CallFrameInfo::Handler::kCFARegister. +class CallFrameInfo::OffsetRule : public CallFrameInfo::Rule { + public: + OffsetRule(int base_register, long offset) + : base_register_(base_register), offset_(offset) {} + ~OffsetRule() {} + CFIRTag getTag() const override { return CFIR_OFFSET_RULE; } + bool Handle(Handler* handler, uint64 address, int reg) const override { + return handler->OffsetRule(address, reg, base_register_, offset_); + } + bool operator==(const Rule& rhs) const override { + if (rhs.getTag() != CFIR_OFFSET_RULE) return false; + const OffsetRule* our_rhs = static_cast<const OffsetRule*>(&rhs); + return (base_register_ == our_rhs->base_register_ && + offset_ == our_rhs->offset_); + } + Rule* Copy() const override { return new OffsetRule(*this); } + // We don't actually need SetBaseRegister or SetOffset here, since they + // are only ever applied to CFA rules, for DW_CFA_def_cfa_offset, and it + // doesn't make sense to use OffsetRule for computing the CFA: it + // computes the address at which a register is saved, not a value. + private: + int base_register_; + long offset_; +}; + +// Rule: the value the register had in the caller is the value of +// BASE_REGISTER plus offset. BASE_REGISTER may be +// CallFrameInfo::Handler::kCFARegister. +class CallFrameInfo::ValOffsetRule : public CallFrameInfo::Rule { + public: + ValOffsetRule(int base_register, long offset) + : base_register_(base_register), offset_(offset) {} + ~ValOffsetRule() {} + CFIRTag getTag() const override { return CFIR_VAL_OFFSET_RULE; } + bool Handle(Handler* handler, uint64 address, int reg) const override { + return handler->ValOffsetRule(address, reg, base_register_, offset_); + } + bool operator==(const Rule& rhs) const override { + if (rhs.getTag() != CFIR_VAL_OFFSET_RULE) return false; + const ValOffsetRule* our_rhs = static_cast<const ValOffsetRule*>(&rhs); + return (base_register_ == our_rhs->base_register_ && + offset_ == our_rhs->offset_); + } + Rule* Copy() const override { return new ValOffsetRule(*this); } + void SetBaseRegister(unsigned reg) override { base_register_ = reg; } + void SetOffset(long long offset) override { offset_ = offset; } + + private: + int base_register_; + long offset_; +}; + +// Rule: the register has been saved in another register REGISTER_NUMBER_. +class CallFrameInfo::RegisterRule : public CallFrameInfo::Rule { + public: + explicit RegisterRule(int register_number) + : register_number_(register_number) {} + ~RegisterRule() {} + CFIRTag getTag() const override { return CFIR_REGISTER_RULE; } + bool Handle(Handler* handler, uint64 address, int reg) const override { + return handler->RegisterRule(address, reg, register_number_); + } + bool operator==(const Rule& rhs) const override { + if (rhs.getTag() != CFIR_REGISTER_RULE) return false; + const RegisterRule* our_rhs = static_cast<const RegisterRule*>(&rhs); + return (register_number_ == our_rhs->register_number_); + } + Rule* Copy() const override { return new RegisterRule(*this); } + + private: + int register_number_; +}; + +// Rule: EXPRESSION evaluates to the address at which the register is saved. +class CallFrameInfo::ExpressionRule : public CallFrameInfo::Rule { + public: + explicit ExpressionRule(const string& expression) : expression_(expression) {} + ~ExpressionRule() {} + CFIRTag getTag() const override { return CFIR_EXPRESSION_RULE; } + bool Handle(Handler* handler, uint64 address, int reg) const override { + return handler->ExpressionRule(address, reg, expression_); + } + bool operator==(const Rule& rhs) const override { + if (rhs.getTag() != CFIR_EXPRESSION_RULE) return false; + const ExpressionRule* our_rhs = static_cast<const ExpressionRule*>(&rhs); + return (expression_ == our_rhs->expression_); + } + Rule* Copy() const override { return new ExpressionRule(*this); } + + private: + string expression_; +}; + +// Rule: EXPRESSION evaluates to the previous value of the register. +class CallFrameInfo::ValExpressionRule : public CallFrameInfo::Rule { + public: + explicit ValExpressionRule(const string& expression) + : expression_(expression) {} + ~ValExpressionRule() {} + CFIRTag getTag() const override { return CFIR_VAL_EXPRESSION_RULE; } + bool Handle(Handler* handler, uint64 address, int reg) const override { + return handler->ValExpressionRule(address, reg, expression_); + } + bool operator==(const Rule& rhs) const override { + if (rhs.getTag() != CFIR_VAL_EXPRESSION_RULE) return false; + const ValExpressionRule* our_rhs = + static_cast<const ValExpressionRule*>(&rhs); + return (expression_ == our_rhs->expression_); + } + Rule* Copy() const override { return new ValExpressionRule(*this); } + + private: + string expression_; +}; + +// A map from register numbers to rules. +class CallFrameInfo::RuleMap { + public: + RuleMap() : cfa_rule_(NULL) {} + RuleMap(const RuleMap& rhs) : cfa_rule_(NULL) { *this = rhs; } + ~RuleMap() { Clear(); } + + RuleMap& operator=(const RuleMap& rhs); + + // Set the rule for computing the CFA to RULE. Take ownership of RULE. + void SetCFARule(Rule* rule) { + delete cfa_rule_; + cfa_rule_ = rule; + } + + // Return the current CFA rule. Unlike RegisterRule, this RuleMap retains + // ownership of the rule. We use this for DW_CFA_def_cfa_offset and + // DW_CFA_def_cfa_register, and for detecting references to the CFA before + // a rule for it has been established. + Rule* CFARule() const { return cfa_rule_; } + + // Return the rule for REG, or NULL if there is none. The caller takes + // ownership of the result. + Rule* RegisterRule(int reg) const; + + // Set the rule for computing REG to RULE. Take ownership of RULE. + void SetRegisterRule(int reg, Rule* rule); + + // Make all the appropriate calls to HANDLER as if we were changing from + // this RuleMap to NEW_RULES at ADDRESS. We use this to implement + // DW_CFA_restore_state, where lots of rules can change simultaneously. + // Return true if all handlers returned true; otherwise, return false. + bool HandleTransitionTo(Handler* handler, uint64 address, + const RuleMap& new_rules) const; + + private: + // A map from register numbers to Rules. + typedef std::map<int, Rule*> RuleByNumber; + + // Remove all register rules and clear cfa_rule_. + void Clear(); + + // The rule for computing the canonical frame address. This RuleMap owns + // this rule. + Rule* cfa_rule_; + + // A map from register numbers to postfix expressions to recover + // their values. This RuleMap owns the Rules the map refers to. + RuleByNumber registers_; +}; + +CallFrameInfo::RuleMap& CallFrameInfo::RuleMap::operator=(const RuleMap& rhs) { + Clear(); + // Since each map owns the rules it refers to, assignment must copy them. + if (rhs.cfa_rule_) cfa_rule_ = rhs.cfa_rule_->Copy(); + for (RuleByNumber::const_iterator it = rhs.registers_.begin(); + it != rhs.registers_.end(); it++) + registers_[it->first] = it->second->Copy(); + return *this; +} + +CallFrameInfo::Rule* CallFrameInfo::RuleMap::RegisterRule(int reg) const { + MOZ_ASSERT(reg != Handler::kCFARegister); + RuleByNumber::const_iterator it = registers_.find(reg); + if (it != registers_.end()) + return it->second->Copy(); + else + return NULL; +} + +void CallFrameInfo::RuleMap::SetRegisterRule(int reg, Rule* rule) { + MOZ_ASSERT(reg != Handler::kCFARegister); + MOZ_ASSERT(rule); + Rule** slot = ®isters_[reg]; + delete *slot; + *slot = rule; +} + +bool CallFrameInfo::RuleMap::HandleTransitionTo( + Handler* handler, uint64 address, const RuleMap& new_rules) const { + // Transition from cfa_rule_ to new_rules.cfa_rule_. + if (cfa_rule_ && new_rules.cfa_rule_) { + if (*cfa_rule_ != *new_rules.cfa_rule_ && + !new_rules.cfa_rule_->Handle(handler, address, Handler::kCFARegister)) + return false; + } else if (cfa_rule_) { + // this RuleMap has a CFA rule but new_rules doesn't. + // CallFrameInfo::Handler has no way to handle this --- and shouldn't; + // it's garbage input. The instruction interpreter should have + // detected this and warned, so take no action here. + } else if (new_rules.cfa_rule_) { + // This shouldn't be possible: NEW_RULES is some prior state, and + // there's no way to remove entries. + MOZ_ASSERT(0); + } else { + // Both CFA rules are empty. No action needed. + } + + // Traverse the two maps in order by register number, and report + // whatever differences we find. + RuleByNumber::const_iterator old_it = registers_.begin(); + RuleByNumber::const_iterator new_it = new_rules.registers_.begin(); + while (old_it != registers_.end() && new_it != new_rules.registers_.end()) { + if (old_it->first < new_it->first) { + // This RuleMap has an entry for old_it->first, but NEW_RULES + // doesn't. + // + // This isn't really the right thing to do, but since CFI generally + // only mentions callee-saves registers, and GCC's convention for + // callee-saves registers is that they are unchanged, it's a good + // approximation. + if (!handler->SameValueRule(address, old_it->first)) return false; + old_it++; + } else if (old_it->first > new_it->first) { + // NEW_RULES has entry for new_it->first, but this RuleMap + // doesn't. This shouldn't be possible: NEW_RULES is some prior + // state, and there's no way to remove entries. + MOZ_ASSERT(0); + } else { + // Both maps have an entry for this register. Report the new + // rule if it is different. + if (*old_it->second != *new_it->second && + !new_it->second->Handle(handler, address, new_it->first)) + return false; + new_it++; + old_it++; + } + } + // Finish off entries from this RuleMap with no counterparts in new_rules. + while (old_it != registers_.end()) { + if (!handler->SameValueRule(address, old_it->first)) return false; + old_it++; + } + // Since we only make transitions from a rule set to some previously + // saved rule set, and we can only add rules to the map, NEW_RULES + // must have fewer rules than *this. + MOZ_ASSERT(new_it == new_rules.registers_.end()); + + return true; +} + +// Remove all register rules and clear cfa_rule_. +void CallFrameInfo::RuleMap::Clear() { + delete cfa_rule_; + cfa_rule_ = NULL; + for (RuleByNumber::iterator it = registers_.begin(); it != registers_.end(); + it++) + delete it->second; + registers_.clear(); +} + +// The state of the call frame information interpreter as it processes +// instructions from a CIE and FDE. +class CallFrameInfo::State { + public: + // Create a call frame information interpreter state with the given + // reporter, reader, handler, and initial call frame info address. + State(ByteReader* reader, Handler* handler, Reporter* reporter, + uint64 address) + : reader_(reader), + handler_(handler), + reporter_(reporter), + address_(address), + entry_(NULL), + cursor_(NULL), + saved_rules_(NULL) {} + + ~State() { + if (saved_rules_) delete saved_rules_; + } + + // Interpret instructions from CIE, save the resulting rule set for + // DW_CFA_restore instructions, and return true. On error, report + // the problem to reporter_ and return false. + bool InterpretCIE(const CIE& cie); + + // Interpret instructions from FDE, and return true. On error, + // report the problem to reporter_ and return false. + bool InterpretFDE(const FDE& fde); + + private: + // The operands of a CFI instruction, for ParseOperands. + struct Operands { + unsigned register_number; // A register number. + uint64 offset; // An offset or address. + long signed_offset; // A signed offset. + string expression; // A DWARF expression. + }; + + // Parse CFI instruction operands from STATE's instruction stream as + // described by FORMAT. On success, populate OPERANDS with the + // results, and return true. On failure, report the problem and + // return false. + // + // Each character of FORMAT should be one of the following: + // + // 'r' unsigned LEB128 register number (OPERANDS->register_number) + // 'o' unsigned LEB128 offset (OPERANDS->offset) + // 's' signed LEB128 offset (OPERANDS->signed_offset) + // 'a' machine-size address (OPERANDS->offset) + // (If the CIE has a 'z' augmentation string, 'a' uses the + // encoding specified by the 'R' argument.) + // '1' a one-byte offset (OPERANDS->offset) + // '2' a two-byte offset (OPERANDS->offset) + // '4' a four-byte offset (OPERANDS->offset) + // '8' an eight-byte offset (OPERANDS->offset) + // 'e' a DW_FORM_block holding a (OPERANDS->expression) + // DWARF expression + bool ParseOperands(const char* format, Operands* operands); + + // Interpret one CFI instruction from STATE's instruction stream, update + // STATE, report any rule changes to handler_, and return true. On + // failure, report the problem and return false. + bool DoInstruction(); + + // The following Do* member functions are subroutines of DoInstruction, + // factoring out the actual work of operations that have several + // different encodings. + + // Set the CFA rule to be the value of BASE_REGISTER plus OFFSET, and + // return true. On failure, report and return false. (Used for + // DW_CFA_def_cfa and DW_CFA_def_cfa_sf.) + bool DoDefCFA(unsigned base_register, long offset); + + // Change the offset of the CFA rule to OFFSET, and return true. On + // failure, report and return false. (Subroutine for + // DW_CFA_def_cfa_offset and DW_CFA_def_cfa_offset_sf.) + bool DoDefCFAOffset(long offset); + + // Specify that REG can be recovered using RULE, and return true. On + // failure, report and return false. + bool DoRule(unsigned reg, Rule* rule); + + // Specify that REG can be found at OFFSET from the CFA, and return true. + // On failure, report and return false. (Subroutine for DW_CFA_offset, + // DW_CFA_offset_extended, and DW_CFA_offset_extended_sf.) + bool DoOffset(unsigned reg, long offset); + + // Specify that the caller's value for REG is the CFA plus OFFSET, + // and return true. On failure, report and return false. (Subroutine + // for DW_CFA_val_offset and DW_CFA_val_offset_sf.) + bool DoValOffset(unsigned reg, long offset); + + // Restore REG to the rule established in the CIE, and return true. On + // failure, report and return false. (Subroutine for DW_CFA_restore and + // DW_CFA_restore_extended.) + bool DoRestore(unsigned reg); + + // Return the section offset of the instruction at cursor. For use + // in error messages. + uint64 CursorOffset() { return entry_->offset + (cursor_ - entry_->start); } + + // Report that entry_ is incomplete, and return false. For brevity. + bool ReportIncomplete() { + reporter_->Incomplete(entry_->offset, entry_->kind); + return false; + } + + // For reading multi-byte values with the appropriate endianness. + ByteReader* reader_; + + // The handler to which we should report the data we find. + Handler* handler_; + + // For reporting problems in the info we're parsing. + Reporter* reporter_; + + // The code address to which the next instruction in the stream applies. + uint64 address_; + + // The entry whose instructions we are currently processing. This is + // first a CIE, and then an FDE. + const Entry* entry_; + + // The next instruction to process. + const char* cursor_; + + // The current set of rules. + RuleMap rules_; + + // The set of rules established by the CIE, used by DW_CFA_restore + // and DW_CFA_restore_extended. We set this after interpreting the + // CIE's instructions. + RuleMap cie_rules_; + + // A stack of saved states, for DW_CFA_remember_state and + // DW_CFA_restore_state. + std::stack<RuleMap>* saved_rules_; +}; + +bool CallFrameInfo::State::InterpretCIE(const CIE& cie) { + entry_ = &cie; + cursor_ = entry_->instructions; + while (cursor_ < entry_->end) + if (!DoInstruction()) return false; + // Note the rules established by the CIE, for use by DW_CFA_restore + // and DW_CFA_restore_extended. + cie_rules_ = rules_; + return true; +} + +bool CallFrameInfo::State::InterpretFDE(const FDE& fde) { + entry_ = &fde; + cursor_ = entry_->instructions; + while (cursor_ < entry_->end) + if (!DoInstruction()) return false; + return true; +} + +bool CallFrameInfo::State::ParseOperands(const char* format, + Operands* operands) { + size_t len; + const char* operand; + + for (operand = format; *operand; operand++) { + size_t bytes_left = entry_->end - cursor_; + switch (*operand) { + case 'r': + operands->register_number = reader_->ReadUnsignedLEB128(cursor_, &len); + if (len > bytes_left) return ReportIncomplete(); + cursor_ += len; + break; + + case 'o': + operands->offset = reader_->ReadUnsignedLEB128(cursor_, &len); + if (len > bytes_left) return ReportIncomplete(); + cursor_ += len; + break; + + case 's': + operands->signed_offset = reader_->ReadSignedLEB128(cursor_, &len); + if (len > bytes_left) return ReportIncomplete(); + cursor_ += len; + break; + + case 'a': + operands->offset = reader_->ReadEncodedPointer( + cursor_, entry_->cie->pointer_encoding, &len); + if (len > bytes_left) return ReportIncomplete(); + cursor_ += len; + break; + + case '1': + if (1 > bytes_left) return ReportIncomplete(); + operands->offset = static_cast<unsigned char>(*cursor_++); + break; + + case '2': + if (2 > bytes_left) return ReportIncomplete(); + operands->offset = reader_->ReadTwoBytes(cursor_); + cursor_ += 2; + break; + + case '4': + if (4 > bytes_left) return ReportIncomplete(); + operands->offset = reader_->ReadFourBytes(cursor_); + cursor_ += 4; + break; + + case '8': + if (8 > bytes_left) return ReportIncomplete(); + operands->offset = reader_->ReadEightBytes(cursor_); + cursor_ += 8; + break; + + case 'e': { + size_t expression_length = reader_->ReadUnsignedLEB128(cursor_, &len); + if (len > bytes_left || expression_length > bytes_left - len) + return ReportIncomplete(); + cursor_ += len; + operands->expression = string(cursor_, expression_length); + cursor_ += expression_length; + break; + } + + default: + MOZ_ASSERT(0); + } + } + + return true; +} + +bool CallFrameInfo::State::DoInstruction() { + CIE* cie = entry_->cie; + Operands ops; + + // Our entry's kind should have been set by now. + MOZ_ASSERT(entry_->kind != kUnknown); + + // We shouldn't have been invoked unless there were more + // instructions to parse. + MOZ_ASSERT(cursor_ < entry_->end); + + unsigned opcode = *cursor_++; + if ((opcode & 0xc0) != 0) { + switch (opcode & 0xc0) { + // Advance the address. + case DW_CFA_advance_loc: { + size_t code_offset = opcode & 0x3f; + address_ += code_offset * cie->code_alignment_factor; + break; + } + + // Find a register at an offset from the CFA. + case DW_CFA_offset: + if (!ParseOperands("o", &ops) || + !DoOffset(opcode & 0x3f, ops.offset * cie->data_alignment_factor)) + return false; + break; + + // Restore the rule established for a register by the CIE. + case DW_CFA_restore: + if (!DoRestore(opcode & 0x3f)) return false; + break; + + // The 'if' above should have excluded this possibility. + default: + MOZ_ASSERT(0); + } + + // Return here, so the big switch below won't be indented. + return true; + } + + switch (opcode) { + // Set the address. + case DW_CFA_set_loc: + if (!ParseOperands("a", &ops)) return false; + address_ = ops.offset; + break; + + // Advance the address. + case DW_CFA_advance_loc1: + if (!ParseOperands("1", &ops)) return false; + address_ += ops.offset * cie->code_alignment_factor; + break; + + // Advance the address. + case DW_CFA_advance_loc2: + if (!ParseOperands("2", &ops)) return false; + address_ += ops.offset * cie->code_alignment_factor; + break; + + // Advance the address. + case DW_CFA_advance_loc4: + if (!ParseOperands("4", &ops)) return false; + address_ += ops.offset * cie->code_alignment_factor; + break; + + // Advance the address. + case DW_CFA_MIPS_advance_loc8: + if (!ParseOperands("8", &ops)) return false; + address_ += ops.offset * cie->code_alignment_factor; + break; + + // Compute the CFA by adding an offset to a register. + case DW_CFA_def_cfa: + if (!ParseOperands("ro", &ops) || + !DoDefCFA(ops.register_number, ops.offset)) + return false; + break; + + // Compute the CFA by adding an offset to a register. + case DW_CFA_def_cfa_sf: + if (!ParseOperands("rs", &ops) || + !DoDefCFA(ops.register_number, + ops.signed_offset * cie->data_alignment_factor)) + return false; + break; + + // Change the base register used to compute the CFA. + case DW_CFA_def_cfa_register: { + Rule* cfa_rule = rules_.CFARule(); + if (!cfa_rule) { + reporter_->NoCFARule(entry_->offset, entry_->kind, CursorOffset()); + return false; + } + if (!ParseOperands("r", &ops)) return false; + cfa_rule->SetBaseRegister(ops.register_number); + if (!cfa_rule->Handle(handler_, address_, Handler::kCFARegister)) + return false; + break; + } + + // Change the offset used to compute the CFA. + case DW_CFA_def_cfa_offset: + if (!ParseOperands("o", &ops) || !DoDefCFAOffset(ops.offset)) + return false; + break; + + // Change the offset used to compute the CFA. + case DW_CFA_def_cfa_offset_sf: + if (!ParseOperands("s", &ops) || + !DoDefCFAOffset(ops.signed_offset * cie->data_alignment_factor)) + return false; + break; + + // Specify an expression whose value is the CFA. + case DW_CFA_def_cfa_expression: { + if (!ParseOperands("e", &ops)) return false; + Rule* rule = new ValExpressionRule(ops.expression); + rules_.SetCFARule(rule); + if (!rule->Handle(handler_, address_, Handler::kCFARegister)) + return false; + break; + } + + // The register's value cannot be recovered. + case DW_CFA_undefined: { + if (!ParseOperands("r", &ops) || + !DoRule(ops.register_number, new UndefinedRule())) + return false; + break; + } + + // The register's value is unchanged from its value in the caller. + case DW_CFA_same_value: { + if (!ParseOperands("r", &ops) || + !DoRule(ops.register_number, new SameValueRule())) + return false; + break; + } + + // Find a register at an offset from the CFA. + case DW_CFA_offset_extended: + if (!ParseOperands("ro", &ops) || + !DoOffset(ops.register_number, + ops.offset * cie->data_alignment_factor)) + return false; + break; + + // The register is saved at an offset from the CFA. + case DW_CFA_offset_extended_sf: + if (!ParseOperands("rs", &ops) || + !DoOffset(ops.register_number, + ops.signed_offset * cie->data_alignment_factor)) + return false; + break; + + // The register is saved at an offset from the CFA. + case DW_CFA_GNU_negative_offset_extended: + if (!ParseOperands("ro", &ops) || + !DoOffset(ops.register_number, + -ops.offset * cie->data_alignment_factor)) + return false; + break; + + // The register's value is the sum of the CFA plus an offset. + case DW_CFA_val_offset: + if (!ParseOperands("ro", &ops) || + !DoValOffset(ops.register_number, + ops.offset * cie->data_alignment_factor)) + return false; + break; + + // The register's value is the sum of the CFA plus an offset. + case DW_CFA_val_offset_sf: + if (!ParseOperands("rs", &ops) || + !DoValOffset(ops.register_number, + ops.signed_offset * cie->data_alignment_factor)) + return false; + break; + + // The register has been saved in another register. + case DW_CFA_register: { + if (!ParseOperands("ro", &ops) || + !DoRule(ops.register_number, new RegisterRule(ops.offset))) + return false; + break; + } + + // An expression yields the address at which the register is saved. + case DW_CFA_expression: { + if (!ParseOperands("re", &ops) || + !DoRule(ops.register_number, new ExpressionRule(ops.expression))) + return false; + break; + } + + // An expression yields the caller's value for the register. + case DW_CFA_val_expression: { + if (!ParseOperands("re", &ops) || + !DoRule(ops.register_number, new ValExpressionRule(ops.expression))) + return false; + break; + } + + // Restore the rule established for a register by the CIE. + case DW_CFA_restore_extended: + if (!ParseOperands("r", &ops) || !DoRestore(ops.register_number)) + return false; + break; + + // Save the current set of rules on a stack. + case DW_CFA_remember_state: + if (!saved_rules_) { + saved_rules_ = new std::stack<RuleMap>(); + } + saved_rules_->push(rules_); + break; + + // Pop the current set of rules off the stack. + case DW_CFA_restore_state: { + if (!saved_rules_ || saved_rules_->empty()) { + reporter_->EmptyStateStack(entry_->offset, entry_->kind, + CursorOffset()); + return false; + } + const RuleMap& new_rules = saved_rules_->top(); + if (rules_.CFARule() && !new_rules.CFARule()) { + reporter_->ClearingCFARule(entry_->offset, entry_->kind, + CursorOffset()); + return false; + } + rules_.HandleTransitionTo(handler_, address_, new_rules); + rules_ = new_rules; + saved_rules_->pop(); + break; + } + + // No operation. (Padding instruction.) + case DW_CFA_nop: + break; + + // A SPARC register window save: Registers 8 through 15 (%o0-%o7) + // are saved in registers 24 through 31 (%i0-%i7), and registers + // 16 through 31 (%l0-%l7 and %i0-%i7) are saved at CFA offsets + // (0-15 * the register size). The register numbers must be + // hard-coded. A GNU extension, and not a pretty one. + case DW_CFA_GNU_window_save: { + // Save %o0-%o7 in %i0-%i7. + for (int i = 8; i < 16; i++) + if (!DoRule(i, new RegisterRule(i + 16))) return false; + // Save %l0-%l7 and %i0-%i7 at the CFA. + for (int i = 16; i < 32; i++) + // Assume that the byte reader's address size is the same as + // the architecture's register size. !@#%*^ hilarious. + if (!DoRule(i, new OffsetRule(Handler::kCFARegister, + (i - 16) * reader_->AddressSize()))) + return false; + break; + } + + // I'm not sure what this is. GDB doesn't use it for unwinding. + case DW_CFA_GNU_args_size: + if (!ParseOperands("o", &ops)) return false; + break; + + // An opcode we don't recognize. + default: { + reporter_->BadInstruction(entry_->offset, entry_->kind, CursorOffset()); + return false; + } + } + + return true; +} + +bool CallFrameInfo::State::DoDefCFA(unsigned base_register, long offset) { + Rule* rule = new ValOffsetRule(base_register, offset); + rules_.SetCFARule(rule); + return rule->Handle(handler_, address_, Handler::kCFARegister); +} + +bool CallFrameInfo::State::DoDefCFAOffset(long offset) { + Rule* cfa_rule = rules_.CFARule(); + if (!cfa_rule) { + reporter_->NoCFARule(entry_->offset, entry_->kind, CursorOffset()); + return false; + } + cfa_rule->SetOffset(offset); + return cfa_rule->Handle(handler_, address_, Handler::kCFARegister); +} + +bool CallFrameInfo::State::DoRule(unsigned reg, Rule* rule) { + rules_.SetRegisterRule(reg, rule); + return rule->Handle(handler_, address_, reg); +} + +bool CallFrameInfo::State::DoOffset(unsigned reg, long offset) { + if (!rules_.CFARule()) { + reporter_->NoCFARule(entry_->offset, entry_->kind, CursorOffset()); + return false; + } + return DoRule(reg, new OffsetRule(Handler::kCFARegister, offset)); +} + +bool CallFrameInfo::State::DoValOffset(unsigned reg, long offset) { + if (!rules_.CFARule()) { + reporter_->NoCFARule(entry_->offset, entry_->kind, CursorOffset()); + return false; + } + return DoRule(reg, new ValOffsetRule(Handler::kCFARegister, offset)); +} + +bool CallFrameInfo::State::DoRestore(unsigned reg) { + // DW_CFA_restore and DW_CFA_restore_extended don't make sense in a CIE. + if (entry_->kind == kCIE) { + reporter_->RestoreInCIE(entry_->offset, CursorOffset()); + return false; + } + Rule* rule = cie_rules_.RegisterRule(reg); + if (!rule) { + // This isn't really the right thing to do, but since CFI generally + // only mentions callee-saves registers, and GCC's convention for + // callee-saves registers is that they are unchanged, it's a good + // approximation. + rule = new SameValueRule(); + } + return DoRule(reg, rule); +} + +bool CallFrameInfo::ReadEntryPrologue(const char* cursor, Entry* entry) { + const char* buffer_end = buffer_ + buffer_length_; + + // Initialize enough of ENTRY for use in error reporting. + entry->offset = cursor - buffer_; + entry->start = cursor; + entry->kind = kUnknown; + entry->end = NULL; + + // Read the initial length. This sets reader_'s offset size. + size_t length_size; + uint64 length = reader_->ReadInitialLength(cursor, &length_size); + if (length_size > size_t(buffer_end - cursor)) return ReportIncomplete(entry); + cursor += length_size; + + // In a .eh_frame section, a length of zero marks the end of the series + // of entries. + if (length == 0 && eh_frame_) { + entry->kind = kTerminator; + entry->end = cursor; + return true; + } + + // Validate the length. + if (length > size_t(buffer_end - cursor)) return ReportIncomplete(entry); + + // The length is the number of bytes after the initial length field; + // we have that position handy at this point, so compute the end + // now. (If we're parsing 64-bit-offset DWARF on a 32-bit machine, + // and the length didn't fit in a size_t, we would have rejected it + // above.) + entry->end = cursor + length; + + // Parse the next field: either the offset of a CIE or a CIE id. + size_t offset_size = reader_->OffsetSize(); + if (offset_size > size_t(entry->end - cursor)) return ReportIncomplete(entry); + entry->id = reader_->ReadOffset(cursor); + + // Don't advance cursor past id field yet; in .eh_frame data we need + // the id's position to compute the section offset of an FDE's CIE. + + // Now we can decide what kind of entry this is. + if (eh_frame_) { + // In .eh_frame data, an ID of zero marks the entry as a CIE, and + // anything else is an offset from the id field of the FDE to the start + // of the CIE. + if (entry->id == 0) { + entry->kind = kCIE; + } else { + entry->kind = kFDE; + // Turn the offset from the id into an offset from the buffer's start. + entry->id = (cursor - buffer_) - entry->id; + } + } else { + // In DWARF CFI data, an ID of ~0 (of the appropriate width, given the + // offset size for the entry) marks the entry as a CIE, and anything + // else is the offset of the CIE from the beginning of the section. + if (offset_size == 4) + entry->kind = (entry->id == 0xffffffff) ? kCIE : kFDE; + else { + MOZ_ASSERT(offset_size == 8); + entry->kind = (entry->id == 0xffffffffffffffffULL) ? kCIE : kFDE; + } + } + + // Now advance cursor past the id. + cursor += offset_size; + + // The fields specific to this kind of entry start here. + entry->fields = cursor; + + entry->cie = NULL; + + return true; +} + +bool CallFrameInfo::ReadCIEFields(CIE* cie) { + const char* cursor = cie->fields; + size_t len; + + MOZ_ASSERT(cie->kind == kCIE); + + // Prepare for early exit. + cie->version = 0; + cie->augmentation.clear(); + cie->code_alignment_factor = 0; + cie->data_alignment_factor = 0; + cie->return_address_register = 0; + cie->has_z_augmentation = false; + cie->pointer_encoding = DW_EH_PE_absptr; + cie->instructions = 0; + + // Parse the version number. + if (cie->end - cursor < 1) return ReportIncomplete(cie); + cie->version = reader_->ReadOneByte(cursor); + cursor++; + + // If we don't recognize the version, we can't parse any more fields of the + // CIE. For DWARF CFI, we handle versions 1 through 4 (there was never a + // version 2 of CFI data). For .eh_frame, we handle versions 1 and 4 as well; + // the difference between those versions seems to be the same as for + // .debug_frame. + if (cie->version < 1 || cie->version > 4) { + reporter_->UnrecognizedVersion(cie->offset, cie->version); + return false; + } + + const char* augmentation_start = cursor; + const void* augmentation_end = + memchr(augmentation_start, '\0', cie->end - augmentation_start); + if (!augmentation_end) return ReportIncomplete(cie); + cursor = static_cast<const char*>(augmentation_end); + cie->augmentation = string(augmentation_start, cursor - augmentation_start); + // Skip the terminating '\0'. + cursor++; + + // Is this CFI augmented? + if (!cie->augmentation.empty()) { + // Is it an augmentation we recognize? + if (cie->augmentation[0] == DW_Z_augmentation_start) { + // Linux C++ ABI 'z' augmentation, used for exception handling data. + cie->has_z_augmentation = true; + } else { + // Not an augmentation we recognize. Augmentations can have arbitrary + // effects on the form of rest of the content, so we have to give up. + reporter_->UnrecognizedAugmentation(cie->offset, cie->augmentation); + return false; + } + } + + if (cie->version >= 4) { + // Check that the address_size and segment_size fields are plausible. + if (cie->end - cursor < 2) { + return ReportIncomplete(cie); + } + uint8_t address_size = reader_->ReadOneByte(cursor); + cursor++; + if (address_size != sizeof(void*)) { + // This is not per-se invalid CFI. But we can reasonably expect to + // be running on a target of the same word size as the CFI is for, + // so we reject this case. + reporter_->InvalidDwarf4Artefact(cie->offset, "Invalid address_size"); + return false; + } + uint8_t segment_size = reader_->ReadOneByte(cursor); + cursor++; + if (segment_size != 0) { + // This is also not per-se invalid CFI, but we don't currently handle + // the case of non-zero |segment_size|. + reporter_->InvalidDwarf4Artefact(cie->offset, "Invalid segment_size"); + return false; + } + // We only continue parsing if |segment_size| is zero. If this routine + // is ever changed to allow non-zero |segment_size|, then + // ReadFDEFields() below will have to be changed to match, per comments + // there. + } + + // Parse the code alignment factor. + cie->code_alignment_factor = reader_->ReadUnsignedLEB128(cursor, &len); + if (size_t(cie->end - cursor) < len) return ReportIncomplete(cie); + cursor += len; + + // Parse the data alignment factor. + cie->data_alignment_factor = reader_->ReadSignedLEB128(cursor, &len); + if (size_t(cie->end - cursor) < len) return ReportIncomplete(cie); + cursor += len; + + // Parse the return address register. This is a ubyte in version 1, and + // a ULEB128 in version 3. + if (cie->version == 1) { + if (cursor >= cie->end) return ReportIncomplete(cie); + cie->return_address_register = uint8(*cursor++); + } else { + cie->return_address_register = reader_->ReadUnsignedLEB128(cursor, &len); + if (size_t(cie->end - cursor) < len) return ReportIncomplete(cie); + cursor += len; + } + + // If we have a 'z' augmentation string, find the augmentation data and + // use the augmentation string to parse it. + if (cie->has_z_augmentation) { + uint64_t data_size = reader_->ReadUnsignedLEB128(cursor, &len); + if (size_t(cie->end - cursor) < len + data_size) + return ReportIncomplete(cie); + cursor += len; + const char* data = cursor; + cursor += data_size; + const char* data_end = cursor; + + cie->has_z_lsda = false; + cie->has_z_personality = false; + cie->has_z_signal_frame = false; + + // Walk the augmentation string, and extract values from the + // augmentation data as the string directs. + for (size_t i = 1; i < cie->augmentation.size(); i++) { + switch (cie->augmentation[i]) { + case DW_Z_has_LSDA: + // The CIE's augmentation data holds the language-specific data + // area pointer's encoding, and the FDE's augmentation data holds + // the pointer itself. + cie->has_z_lsda = true; + // Fetch the LSDA encoding from the augmentation data. + if (data >= data_end) return ReportIncomplete(cie); + cie->lsda_encoding = DwarfPointerEncoding(*data++); + if (!reader_->ValidEncoding(cie->lsda_encoding)) { + reporter_->InvalidPointerEncoding(cie->offset, cie->lsda_encoding); + return false; + } + // Don't check if the encoding is usable here --- we haven't + // read the FDE's fields yet, so we're not prepared for + // DW_EH_PE_funcrel, although that's a fine encoding for the + // LSDA to use, since it appears in the FDE. + break; + + case DW_Z_has_personality_routine: + // The CIE's augmentation data holds the personality routine + // pointer's encoding, followed by the pointer itself. + cie->has_z_personality = true; + // Fetch the personality routine pointer's encoding from the + // augmentation data. + if (data >= data_end) return ReportIncomplete(cie); + cie->personality_encoding = DwarfPointerEncoding(*data++); + if (!reader_->ValidEncoding(cie->personality_encoding)) { + reporter_->InvalidPointerEncoding(cie->offset, + cie->personality_encoding); + return false; + } + if (!reader_->UsableEncoding(cie->personality_encoding)) { + reporter_->UnusablePointerEncoding(cie->offset, + cie->personality_encoding); + return false; + } + // Fetch the personality routine's pointer itself from the data. + cie->personality_address = reader_->ReadEncodedPointer( + data, cie->personality_encoding, &len); + if (len > size_t(data_end - data)) return ReportIncomplete(cie); + data += len; + break; + + case DW_Z_has_FDE_address_encoding: + // The CIE's augmentation data holds the pointer encoding to use + // for addresses in the FDE. + if (data >= data_end) return ReportIncomplete(cie); + cie->pointer_encoding = DwarfPointerEncoding(*data++); + if (!reader_->ValidEncoding(cie->pointer_encoding)) { + reporter_->InvalidPointerEncoding(cie->offset, + cie->pointer_encoding); + return false; + } + if (!reader_->UsableEncoding(cie->pointer_encoding)) { + reporter_->UnusablePointerEncoding(cie->offset, + cie->pointer_encoding); + return false; + } + break; + + case DW_Z_is_signal_trampoline: + // Frames using this CIE are signal delivery frames. + cie->has_z_signal_frame = true; + break; + + default: + // An augmentation we don't recognize. + reporter_->UnrecognizedAugmentation(cie->offset, cie->augmentation); + return false; + } + } + } + + // The CIE's instructions start here. + cie->instructions = cursor; + + return true; +} + +bool CallFrameInfo::ReadFDEFields(FDE* fde) { + const char* cursor = fde->fields; + size_t size; + + // At this point, for Dwarf 4 and above, we are assuming that the + // associated CIE has its |segment_size| field equal to zero. This is + // checked for in ReadCIEFields() above. If ReadCIEFields() is ever + // changed to allow non-zero |segment_size| CIEs then we will have to read + // the segment_selector value at this point. + + fde->address = + reader_->ReadEncodedPointer(cursor, fde->cie->pointer_encoding, &size); + if (size > size_t(fde->end - cursor)) return ReportIncomplete(fde); + cursor += size; + reader_->SetFunctionBase(fde->address); + + // For the length, we strip off the upper nybble of the encoding used for + // the starting address. + DwarfPointerEncoding length_encoding = + DwarfPointerEncoding(fde->cie->pointer_encoding & 0x0f); + fde->size = reader_->ReadEncodedPointer(cursor, length_encoding, &size); + if (size > size_t(fde->end - cursor)) return ReportIncomplete(fde); + cursor += size; + + // If the CIE has a 'z' augmentation string, then augmentation data + // appears here. + if (fde->cie->has_z_augmentation) { + uint64_t data_size = reader_->ReadUnsignedLEB128(cursor, &size); + if (size_t(fde->end - cursor) < size + data_size) + return ReportIncomplete(fde); + cursor += size; + + // In the abstract, we should walk the augmentation string, and extract + // items from the FDE's augmentation data as we encounter augmentation + // string characters that specify their presence: the ordering of items + // in the augmentation string determines the arrangement of values in + // the augmentation data. + // + // In practice, there's only ever one value in FDE augmentation data + // that we support --- the LSDA pointer --- and we have to bail if we + // see any unrecognized augmentation string characters. So if there is + // anything here at all, we know what it is, and where it starts. + if (fde->cie->has_z_lsda) { + // Check whether the LSDA's pointer encoding is usable now: only once + // we've parsed the FDE's starting address do we call reader_-> + // SetFunctionBase, so that the DW_EH_PE_funcrel encoding becomes + // usable. + if (!reader_->UsableEncoding(fde->cie->lsda_encoding)) { + reporter_->UnusablePointerEncoding(fde->cie->offset, + fde->cie->lsda_encoding); + return false; + } + + fde->lsda_address = + reader_->ReadEncodedPointer(cursor, fde->cie->lsda_encoding, &size); + if (size > data_size) return ReportIncomplete(fde); + // Ideally, we would also complain here if there were unconsumed + // augmentation data. + } + + cursor += data_size; + } + + // The FDE's instructions start after those. + fde->instructions = cursor; + + return true; +} + +bool CallFrameInfo::Start() { + const char* buffer_end = buffer_ + buffer_length_; + const char* cursor; + bool all_ok = true; + const char* entry_end; + bool ok; + + // Traverse all the entries in buffer_, skipping CIEs and offering + // FDEs to the handler. + for (cursor = buffer_; cursor < buffer_end; + cursor = entry_end, all_ok = all_ok && ok) { + FDE fde; + + // Make it easy to skip this entry with 'continue': assume that + // things are not okay until we've checked all the data, and + // prepare the address of the next entry. + ok = false; + + // Read the entry's prologue. + if (!ReadEntryPrologue(cursor, &fde)) { + if (!fde.end) { + // If we couldn't even figure out this entry's extent, then we + // must stop processing entries altogether. + all_ok = false; + break; + } + entry_end = fde.end; + continue; + } + + // The next iteration picks up after this entry. + entry_end = fde.end; + + // Did we see an .eh_frame terminating mark? + if (fde.kind == kTerminator) { + // If there appears to be more data left in the section after the + // terminating mark, warn the user. But this is just a warning; + // we leave all_ok true. + if (fde.end < buffer_end) reporter_->EarlyEHTerminator(fde.offset); + break; + } + + // In this loop, we skip CIEs. We only parse them fully when we + // parse an FDE that refers to them. This limits our memory + // consumption (beyond the buffer itself) to that needed to + // process the largest single entry. + if (fde.kind != kFDE) { + ok = true; + continue; + } + + // Validate the CIE pointer. + if (fde.id > buffer_length_) { + reporter_->CIEPointerOutOfRange(fde.offset, fde.id); + continue; + } + + CIE cie; + + // Parse this FDE's CIE header. + if (!ReadEntryPrologue(buffer_ + fde.id, &cie)) continue; + // This had better be an actual CIE. + if (cie.kind != kCIE) { + reporter_->BadCIEId(fde.offset, fde.id); + continue; + } + if (!ReadCIEFields(&cie)) continue; + + // We now have the values that govern both the CIE and the FDE. + cie.cie = &cie; + fde.cie = &cie; + + // Parse the FDE's header. + if (!ReadFDEFields(&fde)) continue; + + // Call Entry to ask the consumer if they're interested. + if (!handler_->Entry(fde.offset, fde.address, fde.size, cie.version, + cie.augmentation, cie.return_address_register)) { + // The handler isn't interested in this entry. That's not an error. + ok = true; + continue; + } + + if (cie.has_z_augmentation) { + // Report the personality routine address, if we have one. + if (cie.has_z_personality) { + if (!handler_->PersonalityRoutine( + cie.personality_address, + IsIndirectEncoding(cie.personality_encoding))) + continue; + } + + // Report the language-specific data area address, if we have one. + if (cie.has_z_lsda) { + if (!handler_->LanguageSpecificDataArea( + fde.lsda_address, IsIndirectEncoding(cie.lsda_encoding))) + continue; + } + + // If this is a signal-handling frame, report that. + if (cie.has_z_signal_frame) { + if (!handler_->SignalHandler()) continue; + } + } + + // Interpret the CIE's instructions, and then the FDE's instructions. + State state(reader_, handler_, reporter_, fde.address); + ok = state.InterpretCIE(cie) && state.InterpretFDE(fde); + + // Tell the ByteReader that the function start address from the + // FDE header is no longer valid. + reader_->ClearFunctionBase(); + + // Report the end of the entry. + handler_->End(); + } + + return all_ok; +} + +const char* CallFrameInfo::KindName(EntryKind kind) { + if (kind == CallFrameInfo::kUnknown) + return "entry"; + else if (kind == CallFrameInfo::kCIE) + return "common information entry"; + else if (kind == CallFrameInfo::kFDE) + return "frame description entry"; + else { + MOZ_ASSERT(kind == CallFrameInfo::kTerminator); + return ".eh_frame sequence terminator"; + } +} + +bool CallFrameInfo::ReportIncomplete(Entry* entry) { + reporter_->Incomplete(entry->offset, entry->kind); + return false; +} + +void CallFrameInfo::Reporter::Incomplete(uint64 offset, + CallFrameInfo::EntryKind kind) { + char buf[300]; + SprintfLiteral(buf, "%s: CFI %s at offset 0x%llx in '%s': entry ends early\n", + filename_.c_str(), CallFrameInfo::KindName(kind), offset, + section_.c_str()); + log_(buf); +} + +void CallFrameInfo::Reporter::EarlyEHTerminator(uint64 offset) { + char buf[300]; + SprintfLiteral(buf, + "%s: CFI at offset 0x%llx in '%s': saw end-of-data marker" + " before end of section contents\n", + filename_.c_str(), offset, section_.c_str()); + log_(buf); +} + +void CallFrameInfo::Reporter::CIEPointerOutOfRange(uint64 offset, + uint64 cie_offset) { + char buf[300]; + SprintfLiteral(buf, + "%s: CFI frame description entry at offset 0x%llx in '%s':" + " CIE pointer is out of range: 0x%llx\n", + filename_.c_str(), offset, section_.c_str(), cie_offset); + log_(buf); +} + +void CallFrameInfo::Reporter::BadCIEId(uint64 offset, uint64 cie_offset) { + char buf[300]; + SprintfLiteral(buf, + "%s: CFI frame description entry at offset 0x%llx in '%s':" + " CIE pointer does not point to a CIE: 0x%llx\n", + filename_.c_str(), offset, section_.c_str(), cie_offset); + log_(buf); +} + +void CallFrameInfo::Reporter::UnrecognizedVersion(uint64 offset, int version) { + char buf[300]; + SprintfLiteral(buf, + "%s: CFI frame description entry at offset 0x%llx in '%s':" + " CIE specifies unrecognized version: %d\n", + filename_.c_str(), offset, section_.c_str(), version); + log_(buf); +} + +void CallFrameInfo::Reporter::UnrecognizedAugmentation(uint64 offset, + const string& aug) { + char buf[300]; + SprintfLiteral(buf, + "%s: CFI frame description entry at offset 0x%llx in '%s':" + " CIE specifies unrecognized augmentation: '%s'\n", + filename_.c_str(), offset, section_.c_str(), aug.c_str()); + log_(buf); +} + +void CallFrameInfo::Reporter::InvalidDwarf4Artefact(uint64 offset, + const char* what) { + char* what_safe = strndup(what, 100); + char buf[300]; + SprintfLiteral(buf, + "%s: CFI frame description entry at offset 0x%llx in '%s':" + " CIE specifies invalid Dwarf4 artefact: %s\n", + filename_.c_str(), offset, section_.c_str(), what_safe); + log_(buf); + free(what_safe); +} + +void CallFrameInfo::Reporter::InvalidPointerEncoding(uint64 offset, + uint8 encoding) { + char buf[300]; + SprintfLiteral(buf, + "%s: CFI common information entry at offset 0x%llx in '%s':" + " 'z' augmentation specifies invalid pointer encoding: " + "0x%02x\n", + filename_.c_str(), offset, section_.c_str(), encoding); + log_(buf); +} + +void CallFrameInfo::Reporter::UnusablePointerEncoding(uint64 offset, + uint8 encoding) { + char buf[300]; + SprintfLiteral(buf, + "%s: CFI common information entry at offset 0x%llx in '%s':" + " 'z' augmentation specifies a pointer encoding for which" + " we have no base address: 0x%02x\n", + filename_.c_str(), offset, section_.c_str(), encoding); + log_(buf); +} + +void CallFrameInfo::Reporter::RestoreInCIE(uint64 offset, uint64 insn_offset) { + char buf[300]; + SprintfLiteral(buf, + "%s: CFI common information entry at offset 0x%llx in '%s':" + " the DW_CFA_restore instruction at offset 0x%llx" + " cannot be used in a common information entry\n", + filename_.c_str(), offset, section_.c_str(), insn_offset); + log_(buf); +} + +void CallFrameInfo::Reporter::BadInstruction(uint64 offset, + CallFrameInfo::EntryKind kind, + uint64 insn_offset) { + char buf[300]; + SprintfLiteral(buf, + "%s: CFI %s at offset 0x%llx in section '%s':" + " the instruction at offset 0x%llx is unrecognized\n", + filename_.c_str(), CallFrameInfo::KindName(kind), offset, + section_.c_str(), insn_offset); + log_(buf); +} + +void CallFrameInfo::Reporter::NoCFARule(uint64 offset, + CallFrameInfo::EntryKind kind, + uint64 insn_offset) { + char buf[300]; + SprintfLiteral(buf, + "%s: CFI %s at offset 0x%llx in section '%s':" + " the instruction at offset 0x%llx assumes that a CFA rule " + "has been set, but none has been set\n", + filename_.c_str(), CallFrameInfo::KindName(kind), offset, + section_.c_str(), insn_offset); + log_(buf); +} + +void CallFrameInfo::Reporter::EmptyStateStack(uint64 offset, + CallFrameInfo::EntryKind kind, + uint64 insn_offset) { + char buf[300]; + SprintfLiteral(buf, + "%s: CFI %s at offset 0x%llx in section '%s':" + " the DW_CFA_restore_state instruction at offset 0x%llx" + " should pop a saved state from the stack, but the stack " + "is empty\n", + filename_.c_str(), CallFrameInfo::KindName(kind), offset, + section_.c_str(), insn_offset); + log_(buf); +} + +void CallFrameInfo::Reporter::ClearingCFARule(uint64 offset, + CallFrameInfo::EntryKind kind, + uint64 insn_offset) { + char buf[300]; + SprintfLiteral(buf, + "%s: CFI %s at offset 0x%llx in section '%s':" + " the DW_CFA_restore_state instruction at offset 0x%llx" + " would clear the CFA rule in effect\n", + filename_.c_str(), CallFrameInfo::KindName(kind), offset, + section_.c_str(), insn_offset); + log_(buf); +} + +unsigned int DwarfCFIToModule::RegisterNames::I386() { + /* + 8 "$eax", "$ecx", "$edx", "$ebx", "$esp", "$ebp", "$esi", "$edi", + 3 "$eip", "$eflags", "$unused1", + 8 "$st0", "$st1", "$st2", "$st3", "$st4", "$st5", "$st6", "$st7", + 2 "$unused2", "$unused3", + 8 "$xmm0", "$xmm1", "$xmm2", "$xmm3", "$xmm4", "$xmm5", "$xmm6", "$xmm7", + 8 "$mm0", "$mm1", "$mm2", "$mm3", "$mm4", "$mm5", "$mm6", "$mm7", + 3 "$fcw", "$fsw", "$mxcsr", + 8 "$es", "$cs", "$ss", "$ds", "$fs", "$gs", "$unused4", "$unused5", + 2 "$tr", "$ldtr" + */ + return 8 + 3 + 8 + 2 + 8 + 8 + 3 + 8 + 2; +} + +unsigned int DwarfCFIToModule::RegisterNames::X86_64() { + /* + 8 "$rax", "$rdx", "$rcx", "$rbx", "$rsi", "$rdi", "$rbp", "$rsp", + 8 "$r8", "$r9", "$r10", "$r11", "$r12", "$r13", "$r14", "$r15", + 1 "$rip", + 8 "$xmm0","$xmm1","$xmm2", "$xmm3", "$xmm4", "$xmm5", "$xmm6", "$xmm7", + 8 "$xmm8","$xmm9","$xmm10","$xmm11","$xmm12","$xmm13","$xmm14","$xmm15", + 8 "$st0", "$st1", "$st2", "$st3", "$st4", "$st5", "$st6", "$st7", + 8 "$mm0", "$mm1", "$mm2", "$mm3", "$mm4", "$mm5", "$mm6", "$mm7", + 1 "$rflags", + 8 "$es", "$cs", "$ss", "$ds", "$fs", "$gs", "$unused1", "$unused2", + 4 "$fs.base", "$gs.base", "$unused3", "$unused4", + 2 "$tr", "$ldtr", + 3 "$mxcsr", "$fcw", "$fsw" + */ + return 8 + 8 + 1 + 8 + 8 + 8 + 8 + 1 + 8 + 4 + 2 + 3; +} + +// Per ARM IHI 0040A, section 3.1 +unsigned int DwarfCFIToModule::RegisterNames::ARM() { + /* + 8 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", + 8 "r8", "r9", "r10", "r11", "r12", "sp", "lr", "pc", + 8 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7", + 8 "fps", "cpsr", "", "", "", "", "", "", + 8 "", "", "", "", "", "", "", "", + 8 "", "", "", "", "", "", "", "", + 8 "", "", "", "", "", "", "", "", + 8 "", "", "", "", "", "", "", "", + 8 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7", + 8 "s8", "s9", "s10", "s11", "s12", "s13", "s14", "s15", + 8 "s16", "s17", "s18", "s19", "s20", "s21", "s22", "s23", + 8 "s24", "s25", "s26", "s27", "s28", "s29", "s30", "s31", + 8 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7" + */ + return 13 * 8; +} + +// Per ARM IHI 0057A, section 3.1 +unsigned int DwarfCFIToModule::RegisterNames::ARM64() { + /* + 8 "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7", + 8 "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15", + 8 "x16" "x17", "x18", "x19", "x20", "x21", "x22", "x23", + 8 "x24", "x25", "x26", "x27", "x28", "x29", "x30","sp", + 8 "", "", "", "", "", "", "", "", + 8 "", "", "", "", "", "", "", "", + 8 "", "", "", "", "", "", "", "", + 8 "", "", "", "", "", "", "", "", + 8 "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7", + 8 "v8", "v9", "v10", "v11", "v12", "v13", "v14", "v15", + 8 "v16", "v17", "v18", "v19", "v20", "v21", "v22, "v23", + 8 "v24", "x25", "x26, "x27", "v28", "v29", "v30", "v31", + */ + return 12 * 8; +} + +unsigned int DwarfCFIToModule::RegisterNames::MIPS() { + /* + 8 "$zero", "$at", "$v0", "$v1", "$a0", "$a1", "$a2", "$a3", + 8 "$t0", "$t1", "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", + 8 "$s0", "$s1", "$s2", "$s3", "$s4", "$s5", "$s6", "$s7", + 8 "$t8", "$t9", "$k0", "$k1", "$gp", "$sp", "$fp", "$ra", + 9 "$lo", "$hi", "$pc", "$f0", "$f1", "$f2", "$f3", "$f4", "$f5", + 8 "$f6", "$f7", "$f8", "$f9", "$f10", "$f11", "$f12", "$f13", + 7 "$f14", "$f15", "$f16", "$f17", "$f18", "$f19", "$f20", + 7 "$f21", "$f22", "$f23", "$f24", "$f25", "$f26", "$f27", + 6 "$f28", "$f29", "$f30", "$f31", "$fcsr", "$fir" + */ + return 8 + 8 + 8 + 8 + 9 + 8 + 7 + 7 + 6; +} + +// See prototype for comments. +int32_t parseDwarfExpr(Summariser* summ, const ByteReader* reader, string expr, + bool debug, bool pushCfaAtStart, bool derefAtEnd) { + const char* cursor = expr.c_str(); + const char* end1 = cursor + expr.length(); + + char buf[100]; + if (debug) { + SprintfLiteral(buf, "LUL.DW << DwarfExpr, len is %d\n", + (int)(end1 - cursor)); + summ->Log(buf); + } + + // Add a marker for the start of this expression. In it, indicate + // whether or not the CFA should be pushed onto the stack prior to + // evaluation. + int32_t start_ix = + summ->AddPfxInstr(PfxInstr(PX_Start, pushCfaAtStart ? 1 : 0)); + MOZ_ASSERT(start_ix >= 0); + + while (cursor < end1) { + uint8 opc = reader->ReadOneByte(cursor); + cursor++; + + const char* nm = nullptr; + PfxExprOp pxop = PX_End; + + switch (opc) { + case DW_OP_lit0 ... DW_OP_lit31: { + int32_t simm32 = (int32_t)(opc - DW_OP_lit0); + if (debug) { + SprintfLiteral(buf, "LUL.DW DW_OP_lit%d\n", (int)simm32); + summ->Log(buf); + } + (void)summ->AddPfxInstr(PfxInstr(PX_SImm32, simm32)); + break; + } + + case DW_OP_breg0 ... DW_OP_breg31: { + size_t len; + int64_t n = reader->ReadSignedLEB128(cursor, &len); + cursor += len; + DW_REG_NUMBER reg = (DW_REG_NUMBER)(opc - DW_OP_breg0); + if (debug) { + SprintfLiteral(buf, "LUL.DW DW_OP_breg%d %lld\n", (int)reg, + (long long int)n); + summ->Log(buf); + } + // PfxInstr only allows a 32 bit signed offset. So we + // must fail if the immediate is out of range. + if (n < INT32_MIN || INT32_MAX < n) goto fail; + (void)summ->AddPfxInstr(PfxInstr(PX_DwReg, reg)); + (void)summ->AddPfxInstr(PfxInstr(PX_SImm32, (int32_t)n)); + (void)summ->AddPfxInstr(PfxInstr(PX_Add)); + break; + } + + case DW_OP_const4s: { + uint64_t u64 = reader->ReadFourBytes(cursor); + cursor += 4; + // u64 is guaranteed by |ReadFourBytes| to be in the + // range 0 .. FFFFFFFF inclusive. But to be safe: + uint32_t u32 = (uint32_t)(u64 & 0xFFFFFFFF); + int32_t s32 = (int32_t)u32; + if (debug) { + SprintfLiteral(buf, "LUL.DW DW_OP_const4s %d\n", (int)s32); + summ->Log(buf); + } + (void)summ->AddPfxInstr(PfxInstr(PX_SImm32, s32)); + break; + } + + case DW_OP_deref: + nm = "deref"; + pxop = PX_Deref; + goto no_operands; + case DW_OP_and: + nm = "and"; + pxop = PX_And; + goto no_operands; + case DW_OP_plus: + nm = "plus"; + pxop = PX_Add; + goto no_operands; + case DW_OP_minus: + nm = "minus"; + pxop = PX_Sub; + goto no_operands; + case DW_OP_shl: + nm = "shl"; + pxop = PX_Shl; + goto no_operands; + case DW_OP_ge: + nm = "ge"; + pxop = PX_CmpGES; + goto no_operands; + no_operands: + MOZ_ASSERT(nm && pxop != PX_End); + if (debug) { + SprintfLiteral(buf, "LUL.DW DW_OP_%s\n", nm); + summ->Log(buf); + } + (void)summ->AddPfxInstr(PfxInstr(pxop)); + break; + + default: + if (debug) { + SprintfLiteral(buf, "LUL.DW unknown opc %d\n", (int)opc); + summ->Log(buf); + } + goto fail; + + } // switch (opc) + + } // while (cursor < end1) + + MOZ_ASSERT(cursor >= end1); + + if (cursor > end1) { + // We overran the Dwarf expression. Give up. + goto fail; + } + + // For DW_CFA_expression, what the expression denotes is the address + // of where the previous value is located. The caller of this routine + // may therefore request one last dereference before the end marker is + // inserted. + if (derefAtEnd) { + (void)summ->AddPfxInstr(PfxInstr(PX_Deref)); + } + + // Insert an end marker, and declare success. + (void)summ->AddPfxInstr(PfxInstr(PX_End)); + if (debug) { + SprintfLiteral(buf, + "LUL.DW conversion of dwarf expression succeeded, " + "ix = %d\n", + (int)start_ix); + summ->Log(buf); + summ->Log("LUL.DW >>\n"); + } + return start_ix; + +fail: + if (debug) { + summ->Log("LUL.DW conversion of dwarf expression failed\n"); + summ->Log("LUL.DW >>\n"); + } + return -1; +} + +bool DwarfCFIToModule::Entry(size_t offset, uint64 address, uint64 length, + uint8 version, const string& augmentation, + unsigned return_address) { + if (DEBUG_DWARF) { + char buf[100]; + SprintfLiteral(buf, "LUL.DW DwarfCFIToModule::Entry 0x%llx,+%lld\n", + address, length); + summ_->Log(buf); + } + + summ_->Entry(address, length); + + // If dwarf2reader::CallFrameInfo can handle this version and + // augmentation, then we should be okay with that, so there's no + // need to check them here. + + // Get ready to collect entries. + return_address_ = return_address; + + // Breakpad STACK CFI records must provide a .ra rule, but DWARF CFI + // may not establish any rule for .ra if the return address column + // is an ordinary register, and that register holds the return + // address on entry to the function. So establish an initial .ra + // rule citing the return address register. + if (return_address_ < num_dw_regs_) { + summ_->Rule(address, return_address_, NODEREF, return_address, 0); + } + + return true; +} + +const UniqueString* DwarfCFIToModule::RegisterName(int i) { + if (i < 0) { + MOZ_ASSERT(i == kCFARegister); + return usu_->ToUniqueString(".cfa"); + } + unsigned reg = i; + if (reg == return_address_) return usu_->ToUniqueString(".ra"); + + char buf[30]; + SprintfLiteral(buf, "dwarf_reg_%u", reg); + return usu_->ToUniqueString(buf); +} + +bool DwarfCFIToModule::UndefinedRule(uint64 address, int reg) { + reporter_->UndefinedNotSupported(entry_offset_, RegisterName(reg)); + // Treat this as a non-fatal error. + return true; +} + +bool DwarfCFIToModule::SameValueRule(uint64 address, int reg) { + if (DEBUG_DWARF) { + char buf[100]; + SprintfLiteral(buf, "LUL.DW 0x%llx: old r%d = Same\n", address, reg); + summ_->Log(buf); + } + // reg + 0 + summ_->Rule(address, reg, NODEREF, reg, 0); + return true; +} + +bool DwarfCFIToModule::OffsetRule(uint64 address, int reg, int base_register, + long offset) { + if (DEBUG_DWARF) { + char buf[100]; + SprintfLiteral(buf, "LUL.DW 0x%llx: old r%d = *(r%d + %ld)\n", address, + reg, base_register, offset); + summ_->Log(buf); + } + // *(base_register + offset) + summ_->Rule(address, reg, DEREF, base_register, offset); + return true; +} + +bool DwarfCFIToModule::ValOffsetRule(uint64 address, int reg, int base_register, + long offset) { + if (DEBUG_DWARF) { + char buf[100]; + SprintfLiteral(buf, "LUL.DW 0x%llx: old r%d = r%d + %ld\n", address, reg, + base_register, offset); + summ_->Log(buf); + } + // base_register + offset + summ_->Rule(address, reg, NODEREF, base_register, offset); + return true; +} + +bool DwarfCFIToModule::RegisterRule(uint64 address, int reg, + int base_register) { + if (DEBUG_DWARF) { + char buf[100]; + SprintfLiteral(buf, "LUL.DW 0x%llx: old r%d = r%d\n", address, reg, + base_register); + summ_->Log(buf); + } + // base_register + 0 + summ_->Rule(address, reg, NODEREF, base_register, 0); + return true; +} + +bool DwarfCFIToModule::ExpressionRule(uint64 address, int reg, + const string& expression) { + bool debug = !!DEBUG_DWARF; + int32_t start_ix = + parseDwarfExpr(summ_, reader_, expression, debug, true /*pushCfaAtStart*/, + true /*derefAtEnd*/); + if (start_ix >= 0) { + summ_->Rule(address, reg, PFXEXPR, 0, start_ix); + } else { + // Parsing of the Dwarf expression failed. Treat this as a + // non-fatal error, hence return |true| even on this path. + reporter_->ExpressionCouldNotBeSummarised(entry_offset_, RegisterName(reg)); + } + return true; +} + +bool DwarfCFIToModule::ValExpressionRule(uint64 address, int reg, + const string& expression) { + bool debug = !!DEBUG_DWARF; + int32_t start_ix = + parseDwarfExpr(summ_, reader_, expression, debug, true /*pushCfaAtStart*/, + false /*!derefAtEnd*/); + if (start_ix >= 0) { + summ_->Rule(address, reg, PFXEXPR, 0, start_ix); + } else { + // Parsing of the Dwarf expression failed. Treat this as a + // non-fatal error, hence return |true| even on this path. + reporter_->ExpressionCouldNotBeSummarised(entry_offset_, RegisterName(reg)); + } + return true; +} + +bool DwarfCFIToModule::End() { + // module_->AddStackFrameEntry(entry_); + if (DEBUG_DWARF) { + summ_->Log("LUL.DW DwarfCFIToModule::End()\n"); + } + summ_->End(); + return true; +} + +void DwarfCFIToModule::Reporter::UndefinedNotSupported( + size_t offset, const UniqueString* reg) { + char buf[300]; + SprintfLiteral(buf, "DwarfCFIToModule::Reporter::UndefinedNotSupported()\n"); + log_(buf); + // BPLOG(INFO) << file_ << ", section '" << section_ + // << "': the call frame entry at offset 0x" + // << std::setbase(16) << offset << std::setbase(10) + // << " sets the rule for register '" << FromUniqueString(reg) + // << "' to 'undefined', but the Breakpad symbol file format cannot " + // << " express this"; +} + +// FIXME: move this somewhere sensible +static bool is_power_of_2(uint64_t n) { + int i, nSetBits = 0; + for (i = 0; i < 8 * (int)sizeof(n); i++) { + if ((n & ((uint64_t)1) << i) != 0) nSetBits++; + } + return nSetBits <= 1; +} + +void DwarfCFIToModule::Reporter::ExpressionCouldNotBeSummarised( + size_t offset, const UniqueString* reg) { + static uint64_t n_complaints = 0; // This isn't threadsafe + n_complaints++; + if (!is_power_of_2(n_complaints)) return; + char buf[300]; + SprintfLiteral(buf, + "DwarfCFIToModule::Reporter::" + "ExpressionCouldNotBeSummarised(shown %llu times)\n", + (unsigned long long int)n_complaints); + log_(buf); +} + +} // namespace lul diff --git a/mozglue/baseprofiler/lul/LulDwarfExt.h b/mozglue/baseprofiler/lul/LulDwarfExt.h new file mode 100644 index 0000000000..dcd2500e5a --- /dev/null +++ b/mozglue/baseprofiler/lul/LulDwarfExt.h @@ -0,0 +1,1289 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ + +// Copyright 2006, 2010 Google Inc. All Rights Reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// Original author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com> + +// This file is derived from the following files in +// toolkit/crashreporter/google-breakpad: +// src/common/dwarf/types.h +// src/common/dwarf/dwarf2enums.h +// src/common/dwarf/bytereader.h +// src/common/dwarf_cfi_to_module.h +// src/common/dwarf/dwarf2reader.h + +#ifndef LulDwarfExt_h +#define LulDwarfExt_h + +#include "LulDwarfSummariser.h" + +#include "mozilla/Assertions.h" + +#include <stdint.h> +#include <string> + +typedef signed char int8; +typedef short int16; +typedef int int32; +typedef long long int64; + +typedef unsigned char uint8; +typedef unsigned short uint16; +typedef unsigned int uint32; +typedef unsigned long long uint64; + +#ifdef __PTRDIFF_TYPE__ +typedef __PTRDIFF_TYPE__ intptr; +typedef unsigned __PTRDIFF_TYPE__ uintptr; +#else +# error "Can't find pointer-sized integral types." +#endif + +namespace lul { + +class UniqueString; + +// Exception handling frame description pointer formats, as described +// by the Linux Standard Base Core Specification 4.0, section 11.5, +// DWARF Extensions. +enum DwarfPointerEncoding { + DW_EH_PE_absptr = 0x00, + DW_EH_PE_omit = 0xff, + DW_EH_PE_uleb128 = 0x01, + DW_EH_PE_udata2 = 0x02, + DW_EH_PE_udata4 = 0x03, + DW_EH_PE_udata8 = 0x04, + DW_EH_PE_sleb128 = 0x09, + DW_EH_PE_sdata2 = 0x0A, + DW_EH_PE_sdata4 = 0x0B, + DW_EH_PE_sdata8 = 0x0C, + DW_EH_PE_pcrel = 0x10, + DW_EH_PE_textrel = 0x20, + DW_EH_PE_datarel = 0x30, + DW_EH_PE_funcrel = 0x40, + DW_EH_PE_aligned = 0x50, + + // The GNU toolchain sources define this enum value as well, + // simply to help classify the lower nybble values into signed and + // unsigned groups. + DW_EH_PE_signed = 0x08, + + // This is not documented in LSB 4.0, but it is used in both the + // Linux and OS X toolchains. It can be added to any other + // encoding (except DW_EH_PE_aligned), and indicates that the + // encoded value represents the address at which the true address + // is stored, not the true address itself. + DW_EH_PE_indirect = 0x80 +}; + +// We can't use the obvious name of LITTLE_ENDIAN and BIG_ENDIAN +// because it conflicts with a macro +enum Endianness { ENDIANNESS_BIG, ENDIANNESS_LITTLE }; + +// A ByteReader knows how to read single- and multi-byte values of +// various endiannesses, sizes, and encodings, as used in DWARF +// debugging information and Linux C++ exception handling data. +class ByteReader { + public: + // Construct a ByteReader capable of reading one-, two-, four-, and + // eight-byte values according to ENDIANNESS, absolute machine-sized + // addresses, DWARF-style "initial length" values, signed and + // unsigned LEB128 numbers, and Linux C++ exception handling data's + // encoded pointers. + explicit ByteReader(enum Endianness endianness); + virtual ~ByteReader(); + + // Read a single byte from BUFFER and return it as an unsigned 8 bit + // number. + uint8 ReadOneByte(const char* buffer) const; + + // Read two bytes from BUFFER and return them as an unsigned 16 bit + // number, using this ByteReader's endianness. + uint16 ReadTwoBytes(const char* buffer) const; + + // Read four bytes from BUFFER and return them as an unsigned 32 bit + // number, using this ByteReader's endianness. This function returns + // a uint64 so that it is compatible with ReadAddress and + // ReadOffset. The number it returns will never be outside the range + // of an unsigned 32 bit integer. + uint64 ReadFourBytes(const char* buffer) const; + + // Read eight bytes from BUFFER and return them as an unsigned 64 + // bit number, using this ByteReader's endianness. + uint64 ReadEightBytes(const char* buffer) const; + + // Read an unsigned LEB128 (Little Endian Base 128) number from + // BUFFER and return it as an unsigned 64 bit integer. Set LEN to + // the number of bytes read. + // + // The unsigned LEB128 representation of an integer N is a variable + // number of bytes: + // + // - If N is between 0 and 0x7f, then its unsigned LEB128 + // representation is a single byte whose value is N. + // + // - Otherwise, its unsigned LEB128 representation is (N & 0x7f) | + // 0x80, followed by the unsigned LEB128 representation of N / + // 128, rounded towards negative infinity. + // + // In other words, we break VALUE into groups of seven bits, put + // them in little-endian order, and then write them as eight-bit + // bytes with the high bit on all but the last. + uint64 ReadUnsignedLEB128(const char* buffer, size_t* len) const; + + // Read a signed LEB128 number from BUFFER and return it as an + // signed 64 bit integer. Set LEN to the number of bytes read. + // + // The signed LEB128 representation of an integer N is a variable + // number of bytes: + // + // - If N is between -0x40 and 0x3f, then its signed LEB128 + // representation is a single byte whose value is N in two's + // complement. + // + // - Otherwise, its signed LEB128 representation is (N & 0x7f) | + // 0x80, followed by the signed LEB128 representation of N / 128, + // rounded towards negative infinity. + // + // In other words, we break VALUE into groups of seven bits, put + // them in little-endian order, and then write them as eight-bit + // bytes with the high bit on all but the last. + int64 ReadSignedLEB128(const char* buffer, size_t* len) const; + + // Indicate that addresses on this architecture are SIZE bytes long. SIZE + // must be either 4 or 8. (DWARF allows addresses to be any number of + // bytes in length from 1 to 255, but we only support 32- and 64-bit + // addresses at the moment.) You must call this before using the + // ReadAddress member function. + // + // For data in a .debug_info section, or something that .debug_info + // refers to like line number or macro data, the compilation unit + // header's address_size field indicates the address size to use. Call + // frame information doesn't indicate its address size (a shortcoming of + // the spec); you must supply the appropriate size based on the + // architecture of the target machine. + void SetAddressSize(uint8 size); + + // Return the current address size, in bytes. This is either 4, + // indicating 32-bit addresses, or 8, indicating 64-bit addresses. + uint8 AddressSize() const { return address_size_; } + + // Read an address from BUFFER and return it as an unsigned 64 bit + // integer, respecting this ByteReader's endianness and address size. You + // must call SetAddressSize before calling this function. + uint64 ReadAddress(const char* buffer) const; + + // DWARF actually defines two slightly different formats: 32-bit DWARF + // and 64-bit DWARF. This is *not* related to the size of registers or + // addresses on the target machine; it refers only to the size of section + // offsets and data lengths appearing in the DWARF data. One only needs + // 64-bit DWARF when the debugging data itself is larger than 4GiB. + // 32-bit DWARF can handle x86_64 or PPC64 code just fine, unless the + // debugging data itself is very large. + // + // DWARF information identifies itself as 32-bit or 64-bit DWARF: each + // compilation unit and call frame information entry begins with an + // "initial length" field, which, in addition to giving the length of the + // data, also indicates the size of section offsets and lengths appearing + // in that data. The ReadInitialLength member function, below, reads an + // initial length and sets the ByteReader's offset size as a side effect. + // Thus, in the normal process of reading DWARF data, the appropriate + // offset size is set automatically. So, you should only need to call + // SetOffsetSize if you are using the same ByteReader to jump from the + // midst of one block of DWARF data into another. + + // Read a DWARF "initial length" field from START, and return it as + // an unsigned 64 bit integer, respecting this ByteReader's + // endianness. Set *LEN to the length of the initial length in + // bytes, either four or twelve. As a side effect, set this + // ByteReader's offset size to either 4 (if we see a 32-bit DWARF + // initial length) or 8 (if we see a 64-bit DWARF initial length). + // + // A DWARF initial length is either: + // + // - a byte count stored as an unsigned 32-bit value less than + // 0xffffff00, indicating that the data whose length is being + // measured uses the 32-bit DWARF format, or + // + // - The 32-bit value 0xffffffff, followed by a 64-bit byte count, + // indicating that the data whose length is being measured uses + // the 64-bit DWARF format. + uint64 ReadInitialLength(const char* start, size_t* len); + + // Read an offset from BUFFER and return it as an unsigned 64 bit + // integer, respecting the ByteReader's endianness. In 32-bit DWARF, the + // offset is 4 bytes long; in 64-bit DWARF, the offset is eight bytes + // long. You must call ReadInitialLength or SetOffsetSize before calling + // this function; see the comments above for details. + uint64 ReadOffset(const char* buffer) const; + + // Return the current offset size, in bytes. + // A return value of 4 indicates that we are reading 32-bit DWARF. + // A return value of 8 indicates that we are reading 64-bit DWARF. + uint8 OffsetSize() const { return offset_size_; } + + // Indicate that section offsets and lengths are SIZE bytes long. SIZE + // must be either 4 (meaning 32-bit DWARF) or 8 (meaning 64-bit DWARF). + // Usually, you should not call this function yourself; instead, let a + // call to ReadInitialLength establish the data's offset size + // automatically. + void SetOffsetSize(uint8 size); + + // The Linux C++ ABI uses a variant of DWARF call frame information + // for exception handling. This data is included in the program's + // address space as the ".eh_frame" section, and intepreted at + // runtime to walk the stack, find exception handlers, and run + // cleanup code. The format is mostly the same as DWARF CFI, with + // some adjustments made to provide the additional + // exception-handling data, and to make the data easier to work with + // in memory --- for example, to allow it to be placed in read-only + // memory even when describing position-independent code. + // + // In particular, exception handling data can select a number of + // different encodings for pointers that appear in the data, as + // described by the DwarfPointerEncoding enum. There are actually + // four axes(!) to the encoding: + // + // - The pointer size: pointers can be 2, 4, or 8 bytes long, or use + // the DWARF LEB128 encoding. + // + // - The pointer's signedness: pointers can be signed or unsigned. + // + // - The pointer's base address: the data stored in the exception + // handling data can be the actual address (that is, an absolute + // pointer), or relative to one of a number of different base + // addreses --- including that of the encoded pointer itself, for + // a form of "pc-relative" addressing. + // + // - The pointer may be indirect: it may be the address where the + // true pointer is stored. (This is used to refer to things via + // global offset table entries, program linkage table entries, or + // other tricks used in position-independent code.) + // + // There are also two options that fall outside that matrix + // altogether: the pointer may be omitted, or it may have padding to + // align it on an appropriate address boundary. (That last option + // may seem like it should be just another axis, but it is not.) + + // Indicate that the exception handling data is loaded starting at + // SECTION_BASE, and that the start of its buffer in our own memory + // is BUFFER_BASE. This allows us to find the address that a given + // byte in our buffer would have when loaded into the program the + // data describes. We need this to resolve DW_EH_PE_pcrel pointers. + void SetCFIDataBase(uint64 section_base, const char* buffer_base); + + // Indicate that the base address of the program's ".text" section + // is TEXT_BASE. We need this to resolve DW_EH_PE_textrel pointers. + void SetTextBase(uint64 text_base); + + // Indicate that the base address for DW_EH_PE_datarel pointers is + // DATA_BASE. The proper value depends on the ABI; it is usually the + // address of the global offset table, held in a designated register in + // position-independent code. You will need to look at the startup code + // for the target system to be sure. I tried; my eyes bled. + void SetDataBase(uint64 data_base); + + // Indicate that the base address for the FDE we are processing is + // FUNCTION_BASE. This is the start address of DW_EH_PE_funcrel + // pointers. (This encoding does not seem to be used by the GNU + // toolchain.) + void SetFunctionBase(uint64 function_base); + + // Indicate that we are no longer processing any FDE, so any use of + // a DW_EH_PE_funcrel encoding is an error. + void ClearFunctionBase(); + + // Return true if ENCODING is a valid pointer encoding. + bool ValidEncoding(DwarfPointerEncoding encoding) const; + + // Return true if we have all the information we need to read a + // pointer that uses ENCODING. This checks that the appropriate + // SetFooBase function for ENCODING has been called. + bool UsableEncoding(DwarfPointerEncoding encoding) const; + + // Read an encoded pointer from BUFFER using ENCODING; return the + // absolute address it represents, and set *LEN to the pointer's + // length in bytes, including any padding for aligned pointers. + // + // This function calls 'abort' if ENCODING is invalid or refers to a + // base address this reader hasn't been given, so you should check + // with ValidEncoding and UsableEncoding first if you would rather + // die in a more helpful way. + uint64 ReadEncodedPointer(const char* buffer, DwarfPointerEncoding encoding, + size_t* len) const; + + private: + // Function pointer type for our address and offset readers. + typedef uint64 (ByteReader::*AddressReader)(const char*) const; + + // Read an offset from BUFFER and return it as an unsigned 64 bit + // integer. DWARF2/3 define offsets as either 4 or 8 bytes, + // generally depending on the amount of DWARF2/3 info present. + // This function pointer gets set by SetOffsetSize. + AddressReader offset_reader_; + + // Read an address from BUFFER and return it as an unsigned 64 bit + // integer. DWARF2/3 allow addresses to be any size from 0-255 + // bytes currently. Internally we support 4 and 8 byte addresses, + // and will CHECK on anything else. + // This function pointer gets set by SetAddressSize. + AddressReader address_reader_; + + Endianness endian_; + uint8 address_size_; + uint8 offset_size_; + + // Base addresses for Linux C++ exception handling data's encoded pointers. + bool have_section_base_, have_text_base_, have_data_base_; + bool have_function_base_; + uint64 section_base_; + uint64 text_base_, data_base_, function_base_; + const char* buffer_base_; +}; + +inline uint8 ByteReader::ReadOneByte(const char* buffer) const { + return buffer[0]; +} + +inline uint16 ByteReader::ReadTwoBytes(const char* signed_buffer) const { + const unsigned char* buffer = + reinterpret_cast<const unsigned char*>(signed_buffer); + const uint16 buffer0 = buffer[0]; + const uint16 buffer1 = buffer[1]; + if (endian_ == ENDIANNESS_LITTLE) { + return buffer0 | buffer1 << 8; + } else { + return buffer1 | buffer0 << 8; + } +} + +inline uint64 ByteReader::ReadFourBytes(const char* signed_buffer) const { + const unsigned char* buffer = + reinterpret_cast<const unsigned char*>(signed_buffer); + const uint32 buffer0 = buffer[0]; + const uint32 buffer1 = buffer[1]; + const uint32 buffer2 = buffer[2]; + const uint32 buffer3 = buffer[3]; + if (endian_ == ENDIANNESS_LITTLE) { + return buffer0 | buffer1 << 8 | buffer2 << 16 | buffer3 << 24; + } else { + return buffer3 | buffer2 << 8 | buffer1 << 16 | buffer0 << 24; + } +} + +inline uint64 ByteReader::ReadEightBytes(const char* signed_buffer) const { + const unsigned char* buffer = + reinterpret_cast<const unsigned char*>(signed_buffer); + const uint64 buffer0 = buffer[0]; + const uint64 buffer1 = buffer[1]; + const uint64 buffer2 = buffer[2]; + const uint64 buffer3 = buffer[3]; + const uint64 buffer4 = buffer[4]; + const uint64 buffer5 = buffer[5]; + const uint64 buffer6 = buffer[6]; + const uint64 buffer7 = buffer[7]; + if (endian_ == ENDIANNESS_LITTLE) { + return buffer0 | buffer1 << 8 | buffer2 << 16 | buffer3 << 24 | + buffer4 << 32 | buffer5 << 40 | buffer6 << 48 | buffer7 << 56; + } else { + return buffer7 | buffer6 << 8 | buffer5 << 16 | buffer4 << 24 | + buffer3 << 32 | buffer2 << 40 | buffer1 << 48 | buffer0 << 56; + } +} + +// Read an unsigned LEB128 number. Each byte contains 7 bits of +// information, plus one bit saying whether the number continues or +// not. + +inline uint64 ByteReader::ReadUnsignedLEB128(const char* buffer, + size_t* len) const { + uint64 result = 0; + size_t num_read = 0; + unsigned int shift = 0; + unsigned char byte; + + do { + byte = *buffer++; + num_read++; + + result |= (static_cast<uint64>(byte & 0x7f)) << shift; + + shift += 7; + + } while (byte & 0x80); + + *len = num_read; + + return result; +} + +// Read a signed LEB128 number. These are like regular LEB128 +// numbers, except the last byte may have a sign bit set. + +inline int64 ByteReader::ReadSignedLEB128(const char* buffer, + size_t* len) const { + int64 result = 0; + unsigned int shift = 0; + size_t num_read = 0; + unsigned char byte; + + do { + byte = *buffer++; + num_read++; + result |= (static_cast<uint64>(byte & 0x7f) << shift); + shift += 7; + } while (byte & 0x80); + + if ((shift < 8 * sizeof(result)) && (byte & 0x40)) + result |= -((static_cast<int64>(1)) << shift); + *len = num_read; + return result; +} + +inline uint64 ByteReader::ReadOffset(const char* buffer) const { + MOZ_ASSERT(this->offset_reader_); + return (this->*offset_reader_)(buffer); +} + +inline uint64 ByteReader::ReadAddress(const char* buffer) const { + MOZ_ASSERT(this->address_reader_); + return (this->*address_reader_)(buffer); +} + +inline void ByteReader::SetCFIDataBase(uint64 section_base, + const char* buffer_base) { + section_base_ = section_base; + buffer_base_ = buffer_base; + have_section_base_ = true; +} + +inline void ByteReader::SetTextBase(uint64 text_base) { + text_base_ = text_base; + have_text_base_ = true; +} + +inline void ByteReader::SetDataBase(uint64 data_base) { + data_base_ = data_base; + have_data_base_ = true; +} + +inline void ByteReader::SetFunctionBase(uint64 function_base) { + function_base_ = function_base; + have_function_base_ = true; +} + +inline void ByteReader::ClearFunctionBase() { have_function_base_ = false; } + +// (derived from) +// dwarf_cfi_to_module.h: Define the DwarfCFIToModule class, which +// accepts parsed DWARF call frame info and adds it to a Summariser object. + +// This class is a reader for DWARF's Call Frame Information. CFI +// describes how to unwind stack frames --- even for functions that do +// not follow fixed conventions for saving registers, whose frame size +// varies as they execute, etc. +// +// CFI describes, at each machine instruction, how to compute the +// stack frame's base address, how to find the return address, and +// where to find the saved values of the caller's registers (if the +// callee has stashed them somewhere to free up the registers for its +// own use). +// +// For example, suppose we have a function whose machine code looks +// like this (imagine an assembly language that looks like C, for a +// machine with 32-bit registers, and a stack that grows towards lower +// addresses): +// +// func: ; entry point; return address at sp +// func+0: sp = sp - 16 ; allocate space for stack frame +// func+1: sp[12] = r0 ; save r0 at sp+12 +// ... ; other code, not frame-related +// func+10: sp -= 4; *sp = x ; push some x on the stack +// ... ; other code, not frame-related +// func+20: r0 = sp[16] ; restore saved r0 +// func+21: sp += 20 ; pop whole stack frame +// func+22: pc = *sp; sp += 4 ; pop return address and jump to it +// +// DWARF CFI is (a very compressed representation of) a table with a +// row for each machine instruction address and a column for each +// register showing how to restore it, if possible. +// +// A special column named "CFA", for "Canonical Frame Address", tells how +// to compute the base address of the frame; registers' entries may +// refer to the CFA in describing where the registers are saved. +// +// Another special column, named "RA", represents the return address. +// +// For example, here is a complete (uncompressed) table describing the +// function above: +// +// insn cfa r0 r1 ... ra +// ======================================= +// func+0: sp cfa[0] +// func+1: sp+16 cfa[0] +// func+2: sp+16 cfa[-4] cfa[0] +// func+11: sp+20 cfa[-4] cfa[0] +// func+21: sp+20 cfa[0] +// func+22: sp cfa[0] +// +// Some things to note here: +// +// - Each row describes the state of affairs *before* executing the +// instruction at the given address. Thus, the row for func+0 +// describes the state before we allocate the stack frame. In the +// next row, the formula for computing the CFA has changed, +// reflecting that allocation. +// +// - The other entries are written in terms of the CFA; this allows +// them to remain unchanged as the stack pointer gets bumped around. +// For example, the rule for recovering the return address (the "ra" +// column) remains unchanged throughout the function, even as the +// stack pointer takes on three different offsets from the return +// address. +// +// - Although we haven't shown it, most calling conventions designate +// "callee-saves" and "caller-saves" registers. The callee must +// preserve the values of callee-saves registers; if it uses them, +// it must save their original values somewhere, and restore them +// before it returns. In contrast, the callee is free to trash +// caller-saves registers; if the callee uses these, it will +// probably not bother to save them anywhere, and the CFI will +// probably mark their values as "unrecoverable". +// +// (However, since the caller cannot assume the callee was going to +// save them, caller-saves registers are probably dead in the caller +// anyway, so compilers usually don't generate CFA for caller-saves +// registers.) +// +// - Exactly where the CFA points is a matter of convention that +// depends on the architecture and ABI in use. In the example, the +// CFA is the value the stack pointer had upon entry to the +// function, pointing at the saved return address. But on the x86, +// the call frame information generated by GCC follows the +// convention that the CFA is the address *after* the saved return +// address. +// +// But by definition, the CFA remains constant throughout the +// lifetime of the frame. This makes it a useful value for other +// columns to refer to. It is also gives debuggers a useful handle +// for identifying a frame. +// +// If you look at the table above, you'll notice that a given entry is +// often the same as the one immediately above it: most instructions +// change only one or two aspects of the stack frame, if they affect +// it at all. The DWARF format takes advantage of this fact, and +// reduces the size of the data by mentioning only the addresses and +// columns at which changes take place. So for the above, DWARF CFI +// data would only actually mention the following: +// +// insn cfa r0 r1 ... ra +// ======================================= +// func+0: sp cfa[0] +// func+1: sp+16 +// func+2: cfa[-4] +// func+11: sp+20 +// func+21: r0 +// func+22: sp +// +// In fact, this is the way the parser reports CFI to the consumer: as +// a series of statements of the form, "At address X, column Y changed +// to Z," and related conventions for describing the initial state. +// +// Naturally, it would be impractical to have to scan the entire +// program's CFI, noting changes as we go, just to recover the +// unwinding rules in effect at one particular instruction. To avoid +// this, CFI data is grouped into "entries", each of which covers a +// specified range of addresses and begins with a complete statement +// of the rules for all recoverable registers at that starting +// address. Each entry typically covers a single function. +// +// Thus, to compute the contents of a given row of the table --- that +// is, rules for recovering the CFA, RA, and registers at a given +// instruction --- the consumer should find the entry that covers that +// instruction's address, start with the initial state supplied at the +// beginning of the entry, and work forward until it has processed all +// the changes up to and including those for the present instruction. +// +// There are seven kinds of rules that can appear in an entry of the +// table: +// +// - "undefined": The given register is not preserved by the callee; +// its value cannot be recovered. +// +// - "same value": This register has the same value it did in the callee. +// +// - offset(N): The register is saved at offset N from the CFA. +// +// - val_offset(N): The value the register had in the caller is the +// CFA plus offset N. (This is usually only useful for describing +// the stack pointer.) +// +// - register(R): The register's value was saved in another register R. +// +// - expression(E): Evaluating the DWARF expression E using the +// current frame's registers' values yields the address at which the +// register was saved. +// +// - val_expression(E): Evaluating the DWARF expression E using the +// current frame's registers' values yields the value the register +// had in the caller. + +class CallFrameInfo { + public: + // The different kinds of entries one finds in CFI. Used internally, + // and for error reporting. + enum EntryKind { kUnknown, kCIE, kFDE, kTerminator }; + + // The handler class to which the parser hands the parsed call frame + // information. Defined below. + class Handler; + + // A reporter class, which CallFrameInfo uses to report errors + // encountered while parsing call frame information. Defined below. + class Reporter; + + // Create a DWARF CFI parser. BUFFER points to the contents of the + // .debug_frame section to parse; BUFFER_LENGTH is its length in bytes. + // REPORTER is an error reporter the parser should use to report + // problems. READER is a ByteReader instance that has the endianness and + // address size set properly. Report the data we find to HANDLER. + // + // This class can also parse Linux C++ exception handling data, as found + // in '.eh_frame' sections. This data is a variant of DWARF CFI that is + // placed in loadable segments so that it is present in the program's + // address space, and is interpreted by the C++ runtime to search the + // call stack for a handler interested in the exception being thrown, + // actually pop the frames, and find cleanup code to run. + // + // There are two differences between the call frame information described + // in the DWARF standard and the exception handling data Linux places in + // the .eh_frame section: + // + // - Exception handling data uses uses a different format for call frame + // information entry headers. The distinguished CIE id, the way FDEs + // refer to their CIEs, and the way the end of the series of entries is + // determined are all slightly different. + // + // If the constructor's EH_FRAME argument is true, then the + // CallFrameInfo parses the entry headers as Linux C++ exception + // handling data. If EH_FRAME is false or omitted, the CallFrameInfo + // parses standard DWARF call frame information. + // + // - Linux C++ exception handling data uses CIE augmentation strings + // beginning with 'z' to specify the presence of additional data after + // the CIE and FDE headers and special encodings used for addresses in + // frame description entries. + // + // CallFrameInfo can handle 'z' augmentations in either DWARF CFI or + // exception handling data if you have supplied READER with the base + // addresses needed to interpret the pointer encodings that 'z' + // augmentations can specify. See the ByteReader interface for details + // about the base addresses. See the CallFrameInfo::Handler interface + // for details about the additional information one might find in + // 'z'-augmented data. + // + // Thus: + // + // - If you are parsing standard DWARF CFI, as found in a .debug_frame + // section, you should pass false for the EH_FRAME argument, or omit + // it, and you need not worry about providing READER with the + // additional base addresses. + // + // - If you want to parse Linux C++ exception handling data from a + // .eh_frame section, you should pass EH_FRAME as true, and call + // READER's Set*Base member functions before calling our Start method. + // + // - If you want to parse DWARF CFI that uses the 'z' augmentations + // (although I don't think any toolchain ever emits such data), you + // could pass false for EH_FRAME, but call READER's Set*Base members. + // + // The extensions the Linux C++ ABI makes to DWARF for exception + // handling are described here, rather poorly: + // http://refspecs.linux-foundation.org/LSB_4.0.0/LSB-Core-generic/LSB-Core-generic/dwarfext.html + // http://refspecs.linux-foundation.org/LSB_4.0.0/LSB-Core-generic/LSB-Core-generic/ehframechpt.html + // + // The mechanics of C++ exception handling, personality routines, + // and language-specific data areas are described here, rather nicely: + // http://www.codesourcery.com/public/cxx-abi/abi-eh.html + + CallFrameInfo(const char* buffer, size_t buffer_length, ByteReader* reader, + Handler* handler, Reporter* reporter, bool eh_frame = false) + : buffer_(buffer), + buffer_length_(buffer_length), + reader_(reader), + handler_(handler), + reporter_(reporter), + eh_frame_(eh_frame) {} + + ~CallFrameInfo() {} + + // Parse the entries in BUFFER, reporting what we find to HANDLER. + // Return true if we reach the end of the section successfully, or + // false if we encounter an error. + bool Start(); + + // Return the textual name of KIND. For error reporting. + static const char* KindName(EntryKind kind); + + private: + struct CIE; + + // A CFI entry, either an FDE or a CIE. + struct Entry { + // The starting offset of the entry in the section, for error + // reporting. + size_t offset; + + // The start of this entry in the buffer. + const char* start; + + // Which kind of entry this is. + // + // We want to be able to use this for error reporting even while we're + // in the midst of parsing. Error reporting code may assume that kind, + // offset, and start fields are valid, although kind may be kUnknown. + EntryKind kind; + + // The end of this entry's common prologue (initial length and id), and + // the start of this entry's kind-specific fields. + const char* fields; + + // The start of this entry's instructions. + const char* instructions; + + // The address past the entry's last byte in the buffer. (Note that + // since offset points to the entry's initial length field, and the + // length field is the number of bytes after that field, this is not + // simply buffer_ + offset + length.) + const char* end; + + // For both DWARF CFI and .eh_frame sections, this is the CIE id in a + // CIE, and the offset of the associated CIE in an FDE. + uint64 id; + + // The CIE that applies to this entry, if we've parsed it. If this is a + // CIE, then this field points to this structure. + CIE* cie; + }; + + // A common information entry (CIE). + struct CIE : public Entry { + uint8 version; // CFI data version number + std::string augmentation; // vendor format extension markers + uint64 code_alignment_factor; // scale for code address adjustments + int data_alignment_factor; // scale for stack pointer adjustments + unsigned return_address_register; // which register holds the return addr + + // True if this CIE includes Linux C++ ABI 'z' augmentation data. + bool has_z_augmentation; + + // Parsed 'z' augmentation data. These are meaningful only if + // has_z_augmentation is true. + bool has_z_lsda; // The 'z' augmentation included 'L'. + bool has_z_personality; // The 'z' augmentation included 'P'. + bool has_z_signal_frame; // The 'z' augmentation included 'S'. + + // If has_z_lsda is true, this is the encoding to be used for language- + // specific data area pointers in FDEs. + DwarfPointerEncoding lsda_encoding; + + // If has_z_personality is true, this is the encoding used for the + // personality routine pointer in the augmentation data. + DwarfPointerEncoding personality_encoding; + + // If has_z_personality is true, this is the address of the personality + // routine --- or, if personality_encoding & DW_EH_PE_indirect, the + // address where the personality routine's address is stored. + uint64 personality_address; + + // This is the encoding used for addresses in the FDE header and + // in DW_CFA_set_loc instructions. This is always valid, whether + // or not we saw a 'z' augmentation string; its default value is + // DW_EH_PE_absptr, which is what normal DWARF CFI uses. + DwarfPointerEncoding pointer_encoding; + }; + + // A frame description entry (FDE). + struct FDE : public Entry { + uint64 address; // start address of described code + uint64 size; // size of described code, in bytes + + // If cie->has_z_lsda is true, then this is the language-specific data + // area's address --- or its address's address, if cie->lsda_encoding + // has the DW_EH_PE_indirect bit set. + uint64 lsda_address; + }; + + // Internal use. + class Rule; + class UndefinedRule; + class SameValueRule; + class OffsetRule; + class ValOffsetRule; + class RegisterRule; + class ExpressionRule; + class ValExpressionRule; + class RuleMap; + class State; + + // Parse the initial length and id of a CFI entry, either a CIE, an FDE, + // or a .eh_frame end-of-data mark. CURSOR points to the beginning of the + // data to parse. On success, populate ENTRY as appropriate, and return + // true. On failure, report the problem, and return false. Even if we + // return false, set ENTRY->end to the first byte after the entry if we + // were able to figure that out, or NULL if we weren't. + bool ReadEntryPrologue(const char* cursor, Entry* entry); + + // Parse the fields of a CIE after the entry prologue, including any 'z' + // augmentation data. Assume that the 'Entry' fields of CIE are + // populated; use CIE->fields and CIE->end as the start and limit for + // parsing. On success, populate the rest of *CIE, and return true; on + // failure, report the problem and return false. + bool ReadCIEFields(CIE* cie); + + // Parse the fields of an FDE after the entry prologue, including any 'z' + // augmentation data. Assume that the 'Entry' fields of *FDE are + // initialized; use FDE->fields and FDE->end as the start and limit for + // parsing. Assume that FDE->cie is fully initialized. On success, + // populate the rest of *FDE, and return true; on failure, report the + // problem and return false. + bool ReadFDEFields(FDE* fde); + + // Report that ENTRY is incomplete, and return false. This is just a + // trivial wrapper for invoking reporter_->Incomplete; it provides a + // little brevity. + bool ReportIncomplete(Entry* entry); + + // Return true if ENCODING has the DW_EH_PE_indirect bit set. + static bool IsIndirectEncoding(DwarfPointerEncoding encoding) { + return encoding & DW_EH_PE_indirect; + } + + // The contents of the DWARF .debug_info section we're parsing. + const char* buffer_; + size_t buffer_length_; + + // For reading multi-byte values with the appropriate endianness. + ByteReader* reader_; + + // The handler to which we should report the data we find. + Handler* handler_; + + // For reporting problems in the info we're parsing. + Reporter* reporter_; + + // True if we are processing .eh_frame-format data. + bool eh_frame_; +}; + +// The handler class for CallFrameInfo. The a CFI parser calls the +// member functions of a handler object to report the data it finds. +class CallFrameInfo::Handler { + public: + // The pseudo-register number for the canonical frame address. + enum { kCFARegister = DW_REG_CFA }; + + Handler() {} + virtual ~Handler() {} + + // The parser has found CFI for the machine code at ADDRESS, + // extending for LENGTH bytes. OFFSET is the offset of the frame + // description entry in the section, for use in error messages. + // VERSION is the version number of the CFI format. AUGMENTATION is + // a string describing any producer-specific extensions present in + // the data. RETURN_ADDRESS is the number of the register that holds + // the address to which the function should return. + // + // Entry should return true to process this CFI, or false to skip to + // the next entry. + // + // The parser invokes Entry for each Frame Description Entry (FDE) + // it finds. The parser doesn't report Common Information Entries + // to the handler explicitly; instead, if the handler elects to + // process a given FDE, the parser reiterates the appropriate CIE's + // contents at the beginning of the FDE's rules. + virtual bool Entry(size_t offset, uint64 address, uint64 length, + uint8 version, const std::string& augmentation, + unsigned return_address) = 0; + + // When the Entry function returns true, the parser calls these + // handler functions repeatedly to describe the rules for recovering + // registers at each instruction in the given range of machine code. + // Immediately after a call to Entry, the handler should assume that + // the rule for each callee-saves register is "unchanged" --- that + // is, that the register still has the value it had in the caller. + // + // If a *Rule function returns true, we continue processing this entry's + // instructions. If a *Rule function returns false, we stop evaluating + // instructions, and skip to the next entry. Either way, we call End + // before going on to the next entry. + // + // In all of these functions, if the REG parameter is kCFARegister, then + // the rule describes how to find the canonical frame address. + // kCFARegister may be passed as a BASE_REGISTER argument, meaning that + // the canonical frame address should be used as the base address for the + // computation. All other REG values will be positive. + + // At ADDRESS, register REG's value is not recoverable. + virtual bool UndefinedRule(uint64 address, int reg) = 0; + + // At ADDRESS, register REG's value is the same as that it had in + // the caller. + virtual bool SameValueRule(uint64 address, int reg) = 0; + + // At ADDRESS, register REG has been saved at offset OFFSET from + // BASE_REGISTER. + virtual bool OffsetRule(uint64 address, int reg, int base_register, + long offset) = 0; + + // At ADDRESS, the caller's value of register REG is the current + // value of BASE_REGISTER plus OFFSET. (This rule doesn't provide an + // address at which the register's value is saved.) + virtual bool ValOffsetRule(uint64 address, int reg, int base_register, + long offset) = 0; + + // At ADDRESS, register REG has been saved in BASE_REGISTER. This differs + // from ValOffsetRule(ADDRESS, REG, BASE_REGISTER, 0), in that + // BASE_REGISTER is the "home" for REG's saved value: if you want to + // assign to a variable whose home is REG in the calling frame, you + // should put the value in BASE_REGISTER. + virtual bool RegisterRule(uint64 address, int reg, int base_register) = 0; + + // At ADDRESS, the DWARF expression EXPRESSION yields the address at + // which REG was saved. + virtual bool ExpressionRule(uint64 address, int reg, + const std::string& expression) = 0; + + // At ADDRESS, the DWARF expression EXPRESSION yields the caller's + // value for REG. (This rule doesn't provide an address at which the + // register's value is saved.) + virtual bool ValExpressionRule(uint64 address, int reg, + const std::string& expression) = 0; + + // Indicate that the rules for the address range reported by the + // last call to Entry are complete. End should return true if + // everything is okay, or false if an error has occurred and parsing + // should stop. + virtual bool End() = 0; + + // Handler functions for Linux C++ exception handling data. These are + // only called if the data includes 'z' augmentation strings. + + // The Linux C++ ABI uses an extension of the DWARF CFI format to + // walk the stack to propagate exceptions from the throw to the + // appropriate catch, and do the appropriate cleanups along the way. + // CFI entries used for exception handling have two additional data + // associated with them: + // + // - The "language-specific data area" describes which exception + // types the function has 'catch' clauses for, and indicates how + // to go about re-entering the function at the appropriate catch + // clause. If the exception is not caught, it describes the + // destructors that must run before the frame is popped. + // + // - The "personality routine" is responsible for interpreting the + // language-specific data area's contents, and deciding whether + // the exception should continue to propagate down the stack, + // perhaps after doing some cleanup for this frame, or whether the + // exception will be caught here. + // + // In principle, the language-specific data area is opaque to + // everybody but the personality routine. In practice, these values + // may be useful or interesting to readers with extra context, and + // we have to at least skip them anyway, so we might as well report + // them to the handler. + + // This entry's exception handling personality routine's address is + // ADDRESS. If INDIRECT is true, then ADDRESS is the address at + // which the routine's address is stored. The default definition for + // this handler function simply returns true, allowing parsing of + // the entry to continue. + virtual bool PersonalityRoutine(uint64 address, bool indirect) { + return true; + } + + // This entry's language-specific data area (LSDA) is located at + // ADDRESS. If INDIRECT is true, then ADDRESS is the address at + // which the area's address is stored. The default definition for + // this handler function simply returns true, allowing parsing of + // the entry to continue. + virtual bool LanguageSpecificDataArea(uint64 address, bool indirect) { + return true; + } + + // This entry describes a signal trampoline --- this frame is the + // caller of a signal handler. The default definition for this + // handler function simply returns true, allowing parsing of the + // entry to continue. + // + // The best description of the rationale for and meaning of signal + // trampoline CFI entries seems to be in the GCC bug database: + // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=26208 + virtual bool SignalHandler() { return true; } +}; + +// The CallFrameInfo class makes calls on an instance of this class to +// report errors or warn about problems in the data it is parsing. +// These messages are sent to the message sink |aLog| provided to the +// constructor. +class CallFrameInfo::Reporter { + public: + // Create an error reporter which attributes troubles to the section + // named SECTION in FILENAME. + // + // Normally SECTION would be .debug_frame, but the Mac puts CFI data + // in a Mach-O section named __debug_frame. If we support + // Linux-style exception handling data, we could be reading an + // .eh_frame section. + Reporter(void (*aLog)(const char*), const std::string& filename, + const std::string& section = ".debug_frame") + : log_(aLog), filename_(filename), section_(section) {} + virtual ~Reporter() {} + + // The CFI entry at OFFSET ends too early to be well-formed. KIND + // indicates what kind of entry it is; KIND can be kUnknown if we + // haven't parsed enough of the entry to tell yet. + virtual void Incomplete(uint64 offset, CallFrameInfo::EntryKind kind); + + // The .eh_frame data has a four-byte zero at OFFSET where the next + // entry's length would be; this is a terminator. However, the buffer + // length as given to the CallFrameInfo constructor says there should be + // more data. + virtual void EarlyEHTerminator(uint64 offset); + + // The FDE at OFFSET refers to the CIE at CIE_OFFSET, but the + // section is not that large. + virtual void CIEPointerOutOfRange(uint64 offset, uint64 cie_offset); + + // The FDE at OFFSET refers to the CIE at CIE_OFFSET, but the entry + // there is not a CIE. + virtual void BadCIEId(uint64 offset, uint64 cie_offset); + + // The FDE at OFFSET refers to a CIE with version number VERSION, + // which we don't recognize. We cannot parse DWARF CFI if it uses + // a version number we don't recognize. + virtual void UnrecognizedVersion(uint64 offset, int version); + + // The FDE at OFFSET refers to a CIE with augmentation AUGMENTATION, + // which we don't recognize. We cannot parse DWARF CFI if it uses + // augmentations we don't recognize. + virtual void UnrecognizedAugmentation(uint64 offset, + const std::string& augmentation); + + // The FDE at OFFSET contains an invalid or otherwise unusable Dwarf4 + // specific field (currently, only "address_size" or "segment_size"). + // Parsing DWARF CFI with unexpected values here seems dubious at best, + // so we stop. WHAT gives a little more information about what is wrong. + virtual void InvalidDwarf4Artefact(uint64 offset, const char* what); + + // The pointer encoding ENCODING, specified by the CIE at OFFSET, is not + // a valid encoding. + virtual void InvalidPointerEncoding(uint64 offset, uint8 encoding); + + // The pointer encoding ENCODING, specified by the CIE at OFFSET, depends + // on a base address which has not been supplied. + virtual void UnusablePointerEncoding(uint64 offset, uint8 encoding); + + // The CIE at OFFSET contains a DW_CFA_restore instruction at + // INSN_OFFSET, which may not appear in a CIE. + virtual void RestoreInCIE(uint64 offset, uint64 insn_offset); + + // The entry at OFFSET, of kind KIND, has an unrecognized + // instruction at INSN_OFFSET. + virtual void BadInstruction(uint64 offset, CallFrameInfo::EntryKind kind, + uint64 insn_offset); + + // The instruction at INSN_OFFSET in the entry at OFFSET, of kind + // KIND, establishes a rule that cites the CFA, but we have not + // established a CFA rule yet. + virtual void NoCFARule(uint64 offset, CallFrameInfo::EntryKind kind, + uint64 insn_offset); + + // The instruction at INSN_OFFSET in the entry at OFFSET, of kind + // KIND, is a DW_CFA_restore_state instruction, but the stack of + // saved states is empty. + virtual void EmptyStateStack(uint64 offset, CallFrameInfo::EntryKind kind, + uint64 insn_offset); + + // The DW_CFA_remember_state instruction at INSN_OFFSET in the entry + // at OFFSET, of kind KIND, would restore a state that has no CFA + // rule, whereas the current state does have a CFA rule. This is + // bogus input, which the CallFrameInfo::Handler interface doesn't + // (and shouldn't) have any way to report. + virtual void ClearingCFARule(uint64 offset, CallFrameInfo::EntryKind kind, + uint64 insn_offset); + + private: + // A logging sink function, as supplied by LUL's user. + void (*log_)(const char*); + + protected: + // The name of the file whose CFI we're reading. + std::string filename_; + + // The name of the CFI section in that file. + std::string section_; +}; + +using lul::CallFrameInfo; +using lul::Summariser; + +// A class that accepts parsed call frame information from the DWARF +// CFI parser and populates a google_breakpad::Module object with the +// contents. +class DwarfCFIToModule : public CallFrameInfo::Handler { + public: + // DwarfCFIToModule uses an instance of this class to report errors + // detected while converting DWARF CFI to Breakpad STACK CFI records. + class Reporter { + public: + // Create a reporter that writes messages to the message sink + // |aLog|. FILE is the name of the file we're processing, and + // SECTION is the name of the section within that file that we're + // looking at (.debug_frame, .eh_frame, etc.). + Reporter(void (*aLog)(const char*), const std::string& file, + const std::string& section) + : log_(aLog), file_(file), section_(section) {} + virtual ~Reporter() {} + + // The DWARF CFI entry at OFFSET says that REG is undefined, but the + // Breakpad symbol file format cannot express this. + virtual void UndefinedNotSupported(size_t offset, const UniqueString* reg); + + // The DWARF CFI entry at OFFSET says that REG uses a DWARF + // expression to find its value, but parseDwarfExpr could not + // convert it to a sequence of PfxInstrs. + virtual void ExpressionCouldNotBeSummarised(size_t offset, + const UniqueString* reg); + + private: + // A logging sink function, as supplied by LUL's user. + void (*log_)(const char*); + + protected: + std::string file_, section_; + }; + + // Register name tables. If TABLE is a vector returned by one of these + // functions, then TABLE[R] is the name of the register numbered R in + // DWARF call frame information. + class RegisterNames { + public: + // Intel's "x86" or IA-32. + static unsigned int I386(); + + // AMD x86_64, AMD64, Intel EM64T, or Intel 64 + static unsigned int X86_64(); + + // ARM. + static unsigned int ARM(); + + // AARCH64. + static unsigned int ARM64(); + + // MIPS. + static unsigned int MIPS(); + }; + + // Create a handler for the dwarf2reader::CallFrameInfo parser that + // records the stack unwinding information it receives in SUMM. + // + // Use REGISTER_NAMES[I] as the name of register number I; *this + // keeps a reference to the vector, so the vector should remain + // alive for as long as the DwarfCFIToModule does. + // + // Use REPORTER for reporting problems encountered in the conversion + // process. + DwarfCFIToModule(const unsigned int num_dw_regs, Reporter* reporter, + ByteReader* reader, + /*MOD*/ UniqueStringUniverse* usu, + /*OUT*/ Summariser* summ) + : summ_(summ), + usu_(usu), + num_dw_regs_(num_dw_regs), + reporter_(reporter), + reader_(reader), + return_address_(-1) {} + virtual ~DwarfCFIToModule() {} + + virtual bool Entry(size_t offset, uint64 address, uint64 length, + uint8 version, const std::string& augmentation, + unsigned return_address) override; + virtual bool UndefinedRule(uint64 address, int reg) override; + virtual bool SameValueRule(uint64 address, int reg) override; + virtual bool OffsetRule(uint64 address, int reg, int base_register, + long offset) override; + virtual bool ValOffsetRule(uint64 address, int reg, int base_register, + long offset) override; + virtual bool RegisterRule(uint64 address, int reg, + int base_register) override; + virtual bool ExpressionRule(uint64 address, int reg, + const std::string& expression) override; + virtual bool ValExpressionRule(uint64 address, int reg, + const std::string& expression) override; + virtual bool End() override; + + private: + // Return the name to use for register I. + const UniqueString* RegisterName(int i); + + // The Summariser to which we should give entries + Summariser* summ_; + + // Universe for creating UniqueStrings in, should that be necessary. + UniqueStringUniverse* usu_; + + // The number of Dwarf-defined register names for this architecture. + const unsigned int num_dw_regs_; + + // The reporter to use to report problems. + Reporter* reporter_; + + // The ByteReader to use for parsing Dwarf expressions. + ByteReader* reader_; + + // The section offset of the current frame description entry, for + // use in error messages. + size_t entry_offset_; + + // The return address column for that entry. + unsigned return_address_; +}; + +// Convert the Dwarf expression in |expr| into PfxInstrs stored in the +// SecMap referred to by |summ|, and return the index of the starting +// PfxInstr added, which must be >= 0. In case of failure return -1. +int32_t parseDwarfExpr(Summariser* summ, const ByteReader* reader, + std::string expr, bool debug, bool pushCfaAtStart, + bool derefAtEnd); + +} // namespace lul + +#endif // LulDwarfExt_h diff --git a/mozglue/baseprofiler/lul/LulDwarfInt.h b/mozglue/baseprofiler/lul/LulDwarfInt.h new file mode 100644 index 0000000000..b72c6e08e3 --- /dev/null +++ b/mozglue/baseprofiler/lul/LulDwarfInt.h @@ -0,0 +1,193 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ + +// Copyright (c) 2008, 2010 Google Inc. All Rights Reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// CFI reader author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com> + +// This file is derived from the following file in +// toolkit/crashreporter/google-breakpad: +// src/common/dwarf/dwarf2enums.h + +#ifndef LulDwarfInt_h +#define LulDwarfInt_h + +#include "LulCommonExt.h" +#include "LulDwarfExt.h" + +namespace lul { + +// These enums do not follow the google3 style only because they are +// known universally (specs, other implementations) by the names in +// exactly this capitalization. +// Tag names and codes. + +// Call Frame Info instructions. +enum DwarfCFI { + DW_CFA_advance_loc = 0x40, + DW_CFA_offset = 0x80, + DW_CFA_restore = 0xc0, + DW_CFA_nop = 0x00, + DW_CFA_set_loc = 0x01, + DW_CFA_advance_loc1 = 0x02, + DW_CFA_advance_loc2 = 0x03, + DW_CFA_advance_loc4 = 0x04, + DW_CFA_offset_extended = 0x05, + DW_CFA_restore_extended = 0x06, + DW_CFA_undefined = 0x07, + DW_CFA_same_value = 0x08, + DW_CFA_register = 0x09, + DW_CFA_remember_state = 0x0a, + DW_CFA_restore_state = 0x0b, + DW_CFA_def_cfa = 0x0c, + DW_CFA_def_cfa_register = 0x0d, + DW_CFA_def_cfa_offset = 0x0e, + DW_CFA_def_cfa_expression = 0x0f, + DW_CFA_expression = 0x10, + DW_CFA_offset_extended_sf = 0x11, + DW_CFA_def_cfa_sf = 0x12, + DW_CFA_def_cfa_offset_sf = 0x13, + DW_CFA_val_offset = 0x14, + DW_CFA_val_offset_sf = 0x15, + DW_CFA_val_expression = 0x16, + + // Opcodes in this range are reserved for user extensions. + DW_CFA_lo_user = 0x1c, + DW_CFA_hi_user = 0x3f, + + // SGI/MIPS specific. + DW_CFA_MIPS_advance_loc8 = 0x1d, + + // GNU extensions. + DW_CFA_GNU_window_save = 0x2d, + DW_CFA_GNU_args_size = 0x2e, + DW_CFA_GNU_negative_offset_extended = 0x2f +}; + +// Exception handling 'z' augmentation letters. +enum DwarfZAugmentationCodes { + // If the CFI augmentation string begins with 'z', then the CIE and FDE + // have an augmentation data area just before the instructions, whose + // contents are determined by the subsequent augmentation letters. + DW_Z_augmentation_start = 'z', + + // If this letter is present in a 'z' augmentation string, the CIE + // augmentation data includes a pointer encoding, and the FDE + // augmentation data includes a language-specific data area pointer, + // represented using that encoding. + DW_Z_has_LSDA = 'L', + + // If this letter is present in a 'z' augmentation string, the CIE + // augmentation data includes a pointer encoding, followed by a pointer + // to a personality routine, represented using that encoding. + DW_Z_has_personality_routine = 'P', + + // If this letter is present in a 'z' augmentation string, the CIE + // augmentation data includes a pointer encoding describing how the FDE's + // initial location, address range, and DW_CFA_set_loc operands are + // encoded. + DW_Z_has_FDE_address_encoding = 'R', + + // If this letter is present in a 'z' augmentation string, then code + // addresses covered by FDEs that cite this CIE are signal delivery + // trampolines. Return addresses of frames in trampolines should not be + // adjusted as described in section 6.4.4 of the DWARF 3 spec. + DW_Z_is_signal_trampoline = 'S' +}; + +// Expression opcodes +enum DwarfExpressionOpcodes { + DW_OP_addr = 0x03, + DW_OP_deref = 0x06, + DW_OP_const1s = 0x09, + DW_OP_const2u = 0x0a, + DW_OP_const2s = 0x0b, + DW_OP_const4u = 0x0c, + DW_OP_const4s = 0x0d, + DW_OP_const8u = 0x0e, + DW_OP_const8s = 0x0f, + DW_OP_constu = 0x10, + DW_OP_consts = 0x11, + DW_OP_dup = 0x12, + DW_OP_drop = 0x13, + DW_OP_over = 0x14, + DW_OP_pick = 0x15, + DW_OP_swap = 0x16, + DW_OP_rot = 0x17, + DW_OP_xderef = 0x18, + DW_OP_abs = 0x19, + DW_OP_and = 0x1a, + DW_OP_div = 0x1b, + DW_OP_minus = 0x1c, + DW_OP_mod = 0x1d, + DW_OP_mul = 0x1e, + DW_OP_neg = 0x1f, + DW_OP_not = 0x20, + DW_OP_or = 0x21, + DW_OP_plus = 0x22, + DW_OP_plus_uconst = 0x23, + DW_OP_shl = 0x24, + DW_OP_shr = 0x25, + DW_OP_shra = 0x26, + DW_OP_xor = 0x27, + DW_OP_skip = 0x2f, + DW_OP_bra = 0x28, + DW_OP_eq = 0x29, + DW_OP_ge = 0x2a, + DW_OP_gt = 0x2b, + DW_OP_le = 0x2c, + DW_OP_lt = 0x2d, + DW_OP_ne = 0x2e, + DW_OP_lit0 = 0x30, + DW_OP_lit31 = 0x4f, + DW_OP_reg0 = 0x50, + DW_OP_reg31 = 0x6f, + DW_OP_breg0 = 0x70, + DW_OP_breg31 = 0x8f, + DW_OP_regx = 0x90, + DW_OP_fbreg = 0x91, + DW_OP_bregx = 0x92, + DW_OP_piece = 0x93, + DW_OP_deref_size = 0x94, + DW_OP_xderef_size = 0x95, + DW_OP_nop = 0x96, + DW_OP_push_object_address = 0x97, + DW_OP_call2 = 0x98, + DW_OP_call4 = 0x99, + DW_OP_call_ref = 0x9a, + DW_OP_form_tls_address = 0x9b, + DW_OP_call_frame_cfa = 0x9c, + DW_OP_bit_piece = 0x9d, + DW_OP_lo_user = 0xe0, + DW_OP_hi_user = 0xff +}; + +} // namespace lul + +#endif // LulDwarfInt_h diff --git a/mozglue/baseprofiler/lul/LulDwarfSummariser.cpp b/mozglue/baseprofiler/lul/LulDwarfSummariser.cpp new file mode 100644 index 0000000000..ff0f212f6c --- /dev/null +++ b/mozglue/baseprofiler/lul/LulDwarfSummariser.cpp @@ -0,0 +1,553 @@ +/* -*- 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 "LulDwarfSummariser.h" + +#include "mozilla/Assertions.h" +#include "mozilla/Sprintf.h" + +#include "LulDwarfExt.h" + +// Set this to 1 for verbose logging +#define DEBUG_SUMMARISER 0 + +namespace lul { + +// Do |s64|'s lowest 32 bits sign extend back to |s64| itself? +static inline bool fitsIn32Bits(int64 s64) { + return s64 == ((s64 & 0xffffffff) ^ 0x80000000) - 0x80000000; +} + +// Check a LExpr prefix expression, starting at pfxInstrs[start] up to +// the next PX_End instruction, to ensure that: +// * It only mentions registers that are tracked on this target +// * The start point is sane +// If the expression is ok, return NULL. Else return a pointer +// a const char* holding a bit of text describing the problem. +static const char* checkPfxExpr(const vector<PfxInstr>* pfxInstrs, + int64_t start) { + size_t nInstrs = pfxInstrs->size(); + if (start < 0 || start >= (ssize_t)nInstrs) { + return "bogus start point"; + } + size_t i; + for (i = start; i < nInstrs; i++) { + PfxInstr pxi = (*pfxInstrs)[i]; + if (pxi.mOpcode == PX_End) break; + if (pxi.mOpcode == PX_DwReg && + !registerIsTracked((DW_REG_NUMBER)pxi.mOperand)) { + return "uses untracked reg"; + } + } + return nullptr; // success +} + +Summariser::Summariser(SecMap* aSecMap, uintptr_t aTextBias, + void (*aLog)(const char*)) + : mSecMap(aSecMap), mTextBias(aTextBias), mLog(aLog) { + mCurrAddr = 0; + mMax1Addr = 0; // Gives an empty range. + + // Initialise the running RuleSet to "haven't got a clue" status. + new (&mCurrRules) RuleSet(); +} + +void Summariser::Entry(uintptr_t aAddress, uintptr_t aLength) { + aAddress += mTextBias; + if (DEBUG_SUMMARISER) { + char buf[100]; + SprintfLiteral(buf, "LUL Entry(%llx, %llu)\n", + (unsigned long long int)aAddress, + (unsigned long long int)aLength); + mLog(buf); + } + // This throws away any previous summary, that is, assumes + // that the previous summary, if any, has been properly finished + // by a call to End(). + mCurrAddr = aAddress; + mMax1Addr = aAddress + aLength; + new (&mCurrRules) RuleSet(); +} + +void Summariser::Rule(uintptr_t aAddress, int aNewReg, LExprHow how, + int16_t oldReg, int64_t offset) { + aAddress += mTextBias; + if (DEBUG_SUMMARISER) { + char buf[100]; + if (how == NODEREF || how == DEREF) { + bool deref = how == DEREF; + SprintfLiteral(buf, "LUL 0x%llx old-r%d = %sr%d + %lld%s\n", + (unsigned long long int)aAddress, aNewReg, + deref ? "*(" : "", (int)oldReg, (long long int)offset, + deref ? ")" : ""); + } else if (how == PFXEXPR) { + SprintfLiteral(buf, "LUL 0x%llx old-r%d = pfx-expr-at %lld\n", + (unsigned long long int)aAddress, aNewReg, + (long long int)offset); + } else { + SprintfLiteral(buf, "LUL 0x%llx old-r%d = (invalid LExpr!)\n", + (unsigned long long int)aAddress, aNewReg); + } + mLog(buf); + } + + if (mCurrAddr < aAddress) { + // Flush the existing summary first. + mCurrRules.mAddr = mCurrAddr; + mCurrRules.mLen = aAddress - mCurrAddr; + mSecMap->AddRuleSet(&mCurrRules); + if (DEBUG_SUMMARISER) { + mLog("LUL "); + mCurrRules.Print(mLog); + mLog("\n"); + } + mCurrAddr = aAddress; + } + + // If for some reason summarisation fails, either or both of these + // become non-null and point at constant text describing the + // problem. Using two rather than just one avoids complications of + // having to concatenate two strings to produce a complete error message. + const char* reason1 = nullptr; + const char* reason2 = nullptr; + + // |offset| needs to be a 32 bit value that sign extends to 64 bits + // on a 64 bit target. We will need to incorporate |offset| into + // any LExpr made here. So we may as well check it right now. + if (!fitsIn32Bits(offset)) { + reason1 = "offset not in signed 32-bit range"; + goto cant_summarise; + } + + // FIXME: factor out common parts of the arch-dependent summarisers. + +#if defined(GP_ARCH_arm) + + // ----------------- arm ----------------- // + + // Now, can we add the rule to our summary? This depends on whether + // the registers and the overall expression are representable. This + // is the heart of the summarisation process. + switch (aNewReg) { + case DW_REG_CFA: + // This is a rule that defines the CFA. The only forms we + // choose to represent are: r7/11/12/13 + offset. The offset + // must fit into 32 bits since 'uintptr_t' is 32 bit on ARM, + // hence there is no need to check it for overflow. + if (how != NODEREF) { + reason1 = "rule for DW_REG_CFA: invalid |how|"; + goto cant_summarise; + } + switch (oldReg) { + case DW_REG_ARM_R7: + case DW_REG_ARM_R11: + case DW_REG_ARM_R12: + case DW_REG_ARM_R13: + break; + default: + reason1 = "rule for DW_REG_CFA: invalid |oldReg|"; + goto cant_summarise; + } + mCurrRules.mCfaExpr = LExpr(how, oldReg, offset); + break; + + case DW_REG_ARM_R7: + case DW_REG_ARM_R11: + case DW_REG_ARM_R12: + case DW_REG_ARM_R13: + case DW_REG_ARM_R14: + case DW_REG_ARM_R15: { + // This is a new rule for R7, R11, R12, R13 (SP), R14 (LR) or + // R15 (the return address). + switch (how) { + case NODEREF: + case DEREF: + // Check the old register is one we're tracking. + if (!registerIsTracked((DW_REG_NUMBER)oldReg) && + oldReg != DW_REG_CFA) { + reason1 = "rule for R7/11/12/13/14/15: uses untracked reg"; + goto cant_summarise; + } + break; + case PFXEXPR: { + // Check that the prefix expression only mentions tracked registers. + const vector<PfxInstr>* pfxInstrs = mSecMap->GetPfxInstrs(); + reason2 = checkPfxExpr(pfxInstrs, offset); + if (reason2) { + reason1 = "rule for R7/11/12/13/14/15: "; + goto cant_summarise; + } + break; + } + default: + goto cant_summarise; + } + LExpr expr = LExpr(how, oldReg, offset); + switch (aNewReg) { + case DW_REG_ARM_R7: + mCurrRules.mR7expr = expr; + break; + case DW_REG_ARM_R11: + mCurrRules.mR11expr = expr; + break; + case DW_REG_ARM_R12: + mCurrRules.mR12expr = expr; + break; + case DW_REG_ARM_R13: + mCurrRules.mR13expr = expr; + break; + case DW_REG_ARM_R14: + mCurrRules.mR14expr = expr; + break; + case DW_REG_ARM_R15: + mCurrRules.mR15expr = expr; + break; + default: + MOZ_ASSERT(0); + } + break; + } + + default: + // Leave |reason1| and |reason2| unset here. This program point + // is reached so often that it causes a flood of "Can't + // summarise" messages. In any case, we don't really care about + // the fact that this summary would produce a new value for a + // register that we're not tracking. We do on the other hand + // care if the summary's expression *uses* a register that we're + // not tracking. But in that case one of the above failures + // should tell us which. + goto cant_summarise; + } + + // Mark callee-saved registers (r4 .. r11) as unchanged, if there is + // no other information about them. FIXME: do this just once, at + // the point where the ruleset is committed. + if (mCurrRules.mR7expr.mHow == UNKNOWN) { + mCurrRules.mR7expr = LExpr(NODEREF, DW_REG_ARM_R7, 0); + } + if (mCurrRules.mR11expr.mHow == UNKNOWN) { + mCurrRules.mR11expr = LExpr(NODEREF, DW_REG_ARM_R11, 0); + } + if (mCurrRules.mR12expr.mHow == UNKNOWN) { + mCurrRules.mR12expr = LExpr(NODEREF, DW_REG_ARM_R12, 0); + } + + // The old r13 (SP) value before the call is always the same as the + // CFA. + mCurrRules.mR13expr = LExpr(NODEREF, DW_REG_CFA, 0); + + // If there's no information about R15 (the return address), say + // it's a copy of R14 (the link register). + if (mCurrRules.mR15expr.mHow == UNKNOWN) { + mCurrRules.mR15expr = LExpr(NODEREF, DW_REG_ARM_R14, 0); + } + +#elif defined(GP_ARCH_arm64) + + // ----------------- arm64 ----------------- // + + switch (aNewReg) { + case DW_REG_CFA: + if (how != NODEREF) { + reason1 = "rule for DW_REG_CFA: invalid |how|"; + goto cant_summarise; + } + switch (oldReg) { + case DW_REG_AARCH64_X29: + case DW_REG_AARCH64_SP: + break; + default: + reason1 = "rule for DW_REG_CFA: invalid |oldReg|"; + goto cant_summarise; + } + mCurrRules.mCfaExpr = LExpr(how, oldReg, offset); + break; + + case DW_REG_AARCH64_X29: + case DW_REG_AARCH64_X30: + case DW_REG_AARCH64_SP: { + switch (how) { + case NODEREF: + case DEREF: + // Check the old register is one we're tracking. + if (!registerIsTracked((DW_REG_NUMBER)oldReg) && + oldReg != DW_REG_CFA) { + reason1 = "rule for X29/X30/SP: uses untracked reg"; + goto cant_summarise; + } + break; + case PFXEXPR: { + // Check that the prefix expression only mentions tracked registers. + const vector<PfxInstr>* pfxInstrs = mSecMap->GetPfxInstrs(); + reason2 = checkPfxExpr(pfxInstrs, offset); + if (reason2) { + reason1 = "rule for X29/X30/SP: "; + goto cant_summarise; + } + break; + } + default: + goto cant_summarise; + } + LExpr expr = LExpr(how, oldReg, offset); + switch (aNewReg) { + case DW_REG_AARCH64_X29: + mCurrRules.mX29expr = expr; + break; + case DW_REG_AARCH64_X30: + mCurrRules.mX30expr = expr; + break; + case DW_REG_AARCH64_SP: + mCurrRules.mSPexpr = expr; + break; + default: + MOZ_ASSERT(0); + } + break; + } + default: + // Leave |reason1| and |reason2| unset here, for the reasons explained + // in the analogous point + goto cant_summarise; + } + + if (mCurrRules.mX29expr.mHow == UNKNOWN) { + mCurrRules.mX29expr = LExpr(NODEREF, DW_REG_AARCH64_X29, 0); + } + if (mCurrRules.mX30expr.mHow == UNKNOWN) { + mCurrRules.mX30expr = LExpr(NODEREF, DW_REG_AARCH64_X30, 0); + } + // On aarch64, it seems the old SP value before the call is always the + // same as the CFA. Therefore, in the absence of any other way to + // recover the SP, specify that the CFA should be copied. + if (mCurrRules.mSPexpr.mHow == UNKNOWN) { + mCurrRules.mSPexpr = LExpr(NODEREF, DW_REG_CFA, 0); + } +#elif defined(GP_ARCH_amd64) || defined(GP_ARCH_x86) + + // ---------------- x64/x86 ---------------- // + + // Now, can we add the rule to our summary? This depends on whether + // the registers and the overall expression are representable. This + // is the heart of the summarisation process. + switch (aNewReg) { + case DW_REG_CFA: { + // This is a rule that defines the CFA. The only forms we choose to + // represent are: = SP+offset, = FP+offset, or =prefix-expr. + switch (how) { + case NODEREF: + if (oldReg != DW_REG_INTEL_XSP && oldReg != DW_REG_INTEL_XBP) { + reason1 = "rule for DW_REG_CFA: invalid |oldReg|"; + goto cant_summarise; + } + break; + case DEREF: + reason1 = "rule for DW_REG_CFA: invalid |how|"; + goto cant_summarise; + case PFXEXPR: { + // Check that the prefix expression only mentions tracked registers. + const vector<PfxInstr>* pfxInstrs = mSecMap->GetPfxInstrs(); + reason2 = checkPfxExpr(pfxInstrs, offset); + if (reason2) { + reason1 = "rule for CFA: "; + goto cant_summarise; + } + break; + } + default: + goto cant_summarise; + } + mCurrRules.mCfaExpr = LExpr(how, oldReg, offset); + break; + } + + case DW_REG_INTEL_XSP: + case DW_REG_INTEL_XBP: + case DW_REG_INTEL_XIP: { + // This is a new rule for XSP, XBP or XIP (the return address). + switch (how) { + case NODEREF: + case DEREF: + // Check the old register is one we're tracking. + if (!registerIsTracked((DW_REG_NUMBER)oldReg) && + oldReg != DW_REG_CFA) { + reason1 = "rule for XSP/XBP/XIP: uses untracked reg"; + goto cant_summarise; + } + break; + case PFXEXPR: { + // Check that the prefix expression only mentions tracked registers. + const vector<PfxInstr>* pfxInstrs = mSecMap->GetPfxInstrs(); + reason2 = checkPfxExpr(pfxInstrs, offset); + if (reason2) { + reason1 = "rule for XSP/XBP/XIP: "; + goto cant_summarise; + } + break; + } + default: + goto cant_summarise; + } + LExpr expr = LExpr(how, oldReg, offset); + switch (aNewReg) { + case DW_REG_INTEL_XBP: + mCurrRules.mXbpExpr = expr; + break; + case DW_REG_INTEL_XSP: + mCurrRules.mXspExpr = expr; + break; + case DW_REG_INTEL_XIP: + mCurrRules.mXipExpr = expr; + break; + default: + MOZ_CRASH("impossible value for aNewReg"); + } + break; + } + + default: + // Leave |reason1| and |reason2| unset here, for the reasons + // explained in the analogous point in the ARM case just above. + goto cant_summarise; + } + + // On Intel, it seems the old SP value before the call is always the + // same as the CFA. Therefore, in the absence of any other way to + // recover the SP, specify that the CFA should be copied. + if (mCurrRules.mXspExpr.mHow == UNKNOWN) { + mCurrRules.mXspExpr = LExpr(NODEREF, DW_REG_CFA, 0); + } + + // Also, gcc says "Undef" for BP when it is unchanged. + if (mCurrRules.mXbpExpr.mHow == UNKNOWN) { + mCurrRules.mXbpExpr = LExpr(NODEREF, DW_REG_INTEL_XBP, 0); + } + +#elif defined(GP_ARCH_mips64) + // ---------------- mips ---------------- // + // + // Now, can we add the rule to our summary? This depends on whether + // the registers and the overall expression are representable. This + // is the heart of the summarisation process. + switch (aNewReg) { + case DW_REG_CFA: + // This is a rule that defines the CFA. The only forms we can + // represent are: = SP+offset or = FP+offset. + if (how != NODEREF) { + reason1 = "rule for DW_REG_CFA: invalid |how|"; + goto cant_summarise; + } + if (oldReg != DW_REG_MIPS_SP && oldReg != DW_REG_MIPS_FP) { + reason1 = "rule for DW_REG_CFA: invalid |oldReg|"; + goto cant_summarise; + } + mCurrRules.mCfaExpr = LExpr(how, oldReg, offset); + break; + + case DW_REG_MIPS_SP: + case DW_REG_MIPS_FP: + case DW_REG_MIPS_PC: { + // This is a new rule for SP, FP or PC (the return address). + switch (how) { + case NODEREF: + case DEREF: + // Check the old register is one we're tracking. + if (!registerIsTracked((DW_REG_NUMBER)oldReg) && + oldReg != DW_REG_CFA) { + reason1 = "rule for SP/FP/PC: uses untracked reg"; + goto cant_summarise; + } + break; + case PFXEXPR: { + // Check that the prefix expression only mentions tracked registers. + const vector<PfxInstr>* pfxInstrs = mSecMap->GetPfxInstrs(); + reason2 = checkPfxExpr(pfxInstrs, offset); + if (reason2) { + reason1 = "rule for SP/FP/PC: "; + goto cant_summarise; + } + break; + } + default: + goto cant_summarise; + } + LExpr expr = LExpr(how, oldReg, offset); + switch (aNewReg) { + case DW_REG_MIPS_FP: + mCurrRules.mFPexpr = expr; + break; + case DW_REG_MIPS_SP: + mCurrRules.mSPexpr = expr; + break; + case DW_REG_MIPS_PC: + mCurrRules.mPCexpr = expr; + break; + default: + MOZ_CRASH("impossible value for aNewReg"); + } + break; + } + default: + // Leave |reason1| and |reason2| unset here, for the reasons + // explained in the analogous point in the ARM case just above. + goto cant_summarise; + } + + // On MIPS, it seems the old SP value before the call is always the + // same as the CFA. Therefore, in the absence of any other way to + // recover the SP, specify that the CFA should be copied. + if (mCurrRules.mSPexpr.mHow == UNKNOWN) { + mCurrRules.mSPexpr = LExpr(NODEREF, DW_REG_CFA, 0); + } + + // Also, gcc says "Undef" for FP when it is unchanged. + if (mCurrRules.mFPexpr.mHow == UNKNOWN) { + mCurrRules.mFPexpr = LExpr(NODEREF, DW_REG_MIPS_FP, 0); + } + +#else + +# error "Unsupported arch" +#endif + + return; + +cant_summarise: + if (reason1 || reason2) { + char buf[200]; + SprintfLiteral(buf, + "LUL can't summarise: " + "SVMA=0x%llx: %s%s, expr=LExpr(%s,%u,%lld)\n", + (unsigned long long int)(aAddress - mTextBias), + reason1 ? reason1 : "", reason2 ? reason2 : "", + NameOf_LExprHow(how), (unsigned int)oldReg, + (long long int)offset); + mLog(buf); + } +} + +uint32_t Summariser::AddPfxInstr(PfxInstr pfxi) { + return mSecMap->AddPfxInstr(pfxi); +} + +void Summariser::End() { + if (DEBUG_SUMMARISER) { + mLog("LUL End\n"); + } + if (mCurrAddr < mMax1Addr) { + mCurrRules.mAddr = mCurrAddr; + mCurrRules.mLen = mMax1Addr - mCurrAddr; + mSecMap->AddRuleSet(&mCurrRules); + if (DEBUG_SUMMARISER) { + mLog("LUL "); + mCurrRules.Print(mLog); + mLog("\n"); + } + } +} + +} // namespace lul diff --git a/mozglue/baseprofiler/lul/LulDwarfSummariser.h b/mozglue/baseprofiler/lul/LulDwarfSummariser.h new file mode 100644 index 0000000000..30f1ba23c1 --- /dev/null +++ b/mozglue/baseprofiler/lul/LulDwarfSummariser.h @@ -0,0 +1,64 @@ +/* -*- 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/. */ + +#ifndef LulDwarfSummariser_h +#define LulDwarfSummariser_h + +#include "LulMainInt.h" + +namespace lul { + +class Summariser { + public: + Summariser(SecMap* aSecMap, uintptr_t aTextBias, void (*aLog)(const char*)); + + virtual void Entry(uintptr_t aAddress, uintptr_t aLength); + virtual void End(); + + // Tell the summariser that the value for |aNewReg| at |aAddress| is + // recovered using the LExpr that can be constructed using the + // components |how|, |oldReg| and |offset|. The summariser will + // inspect the components and may reject them for various reasons, + // but the hope is that it will find them acceptable and record this + // rule permanently. + virtual void Rule(uintptr_t aAddress, int aNewReg, LExprHow how, + int16_t oldReg, int64_t offset); + + virtual uint32_t AddPfxInstr(PfxInstr pfxi); + + // Send output to the logging sink, for debugging. + virtual void Log(const char* str) { mLog(str); } + + private: + // The SecMap in which we park the finished summaries (RuleSets) and + // also any PfxInstrs derived from Dwarf expressions. + SecMap* mSecMap; + + // Running state for the current summary (RuleSet) under construction. + RuleSet mCurrRules; + + // The start of the address range to which the RuleSet under + // construction applies. + uintptr_t mCurrAddr; + + // The highest address, plus one, for which the RuleSet under + // construction could possibly apply. If there are no further + // incoming events then mCurrRules will eventually be emitted + // as-is, for the range mCurrAddr.. mMax1Addr - 1, if that is + // nonempty. + uintptr_t mMax1Addr; + + // The bias value (to add to the SVMAs, to get AVMAs) to be used + // when adding entries into mSecMap. + uintptr_t mTextBias; + + // A logging sink, for debugging. + void (*mLog)(const char* aFmt); +}; + +} // namespace lul + +#endif // LulDwarfSummariser_h diff --git a/mozglue/baseprofiler/lul/LulElf.cpp b/mozglue/baseprofiler/lul/LulElf.cpp new file mode 100644 index 0000000000..b5d5e772be --- /dev/null +++ b/mozglue/baseprofiler/lul/LulElf.cpp @@ -0,0 +1,873 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ + +// Copyright (c) 2006, 2011, 2012 Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// Restructured in 2009 by: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com> + +// (derived from) +// dump_symbols.cc: implement google_breakpad::WriteSymbolFile: +// Find all the debugging info in a file and dump it as a Breakpad symbol file. +// +// dump_symbols.h: Read debugging information from an ELF file, and write +// it out as a Breakpad symbol file. + +// This file is derived from the following files in +// toolkit/crashreporter/google-breakpad: +// src/common/linux/dump_symbols.cc +// src/common/linux/elfutils.cc +// src/common/linux/file_id.cc + +#include <errno.h> +#include <fcntl.h> +#include <libgen.h> +#include <stdio.h> +#include <string.h> +#include <sys/mman.h> +#include <sys/stat.h> +#include <unistd.h> +#include <arpa/inet.h> + +#include <cstdlib> +#include <set> +#include <string> +#include <vector> + +#include "mozilla/Assertions.h" +#include "mozilla/Sprintf.h" + +#include "PlatformMacros.h" +#include "LulCommonExt.h" +#include "LulDwarfExt.h" +#include "LulElfInt.h" +#include "LulMainInt.h" + +#if defined(GP_PLAT_arm_android) && !defined(SHT_ARM_EXIDX) +// bionic and older glibsc don't define it +# define SHT_ARM_EXIDX (SHT_LOPROC + 1) +#endif + +// Old Linux header doesn't define EM_AARCH64 +#ifndef EM_AARCH64 +# define EM_AARCH64 183 +#endif + +// This namespace contains helper functions. +namespace { + +using lul::DwarfCFIToModule; +using lul::FindElfSectionByName; +using lul::GetOffset; +using lul::IsValidElf; +using lul::Module; +using lul::scoped_ptr; +using lul::Summariser; +using lul::UniqueStringUniverse; +using std::set; +using std::string; +using std::vector; + +// +// FDWrapper +// +// Wrapper class to make sure opened file is closed. +// +class FDWrapper { + public: + explicit FDWrapper(int fd) : fd_(fd) {} + ~FDWrapper() { + if (fd_ != -1) close(fd_); + } + int get() { return fd_; } + int release() { + int fd = fd_; + fd_ = -1; + return fd; + } + + private: + int fd_; +}; + +// +// MmapWrapper +// +// Wrapper class to make sure mapped regions are unmapped. +// +class MmapWrapper { + public: + MmapWrapper() : is_set_(false), base_(NULL), size_(0) {} + ~MmapWrapper() { + if (is_set_ && base_ != NULL) { + MOZ_ASSERT(size_ > 0); + munmap(base_, size_); + } + } + void set(void* mapped_address, size_t mapped_size) { + is_set_ = true; + base_ = mapped_address; + size_ = mapped_size; + } + void release() { + MOZ_ASSERT(is_set_); + is_set_ = false; + base_ = NULL; + size_ = 0; + } + + private: + bool is_set_; + void* base_; + size_t size_; +}; + +// Set NUM_DW_REGNAMES to be the number of Dwarf register names +// appropriate to the machine architecture given in HEADER. Return +// true on success, or false if HEADER's machine architecture is not +// supported. +template <typename ElfClass> +bool DwarfCFIRegisterNames(const typename ElfClass::Ehdr* elf_header, + unsigned int* num_dw_regnames) { + switch (elf_header->e_machine) { + case EM_386: + *num_dw_regnames = DwarfCFIToModule::RegisterNames::I386(); + return true; + case EM_ARM: + *num_dw_regnames = DwarfCFIToModule::RegisterNames::ARM(); + return true; + case EM_X86_64: + *num_dw_regnames = DwarfCFIToModule::RegisterNames::X86_64(); + return true; + case EM_MIPS: + *num_dw_regnames = DwarfCFIToModule::RegisterNames::MIPS(); + return true; + case EM_AARCH64: + *num_dw_regnames = DwarfCFIToModule::RegisterNames::ARM64(); + return true; + default: + MOZ_ASSERT(0); + return false; + } +} + +template <typename ElfClass> +bool LoadDwarfCFI(const string& dwarf_filename, + const typename ElfClass::Ehdr* elf_header, + const char* section_name, + const typename ElfClass::Shdr* section, const bool eh_frame, + const typename ElfClass::Shdr* got_section, + const typename ElfClass::Shdr* text_section, + const bool big_endian, SecMap* smap, uintptr_t text_bias, + UniqueStringUniverse* usu, void (*log)(const char*)) { + // Find the appropriate set of register names for this file's + // architecture. + unsigned int num_dw_regs = 0; + if (!DwarfCFIRegisterNames<ElfClass>(elf_header, &num_dw_regs)) { + fprintf(stderr, + "%s: unrecognized ELF machine architecture '%d';" + " cannot convert DWARF call frame information\n", + dwarf_filename.c_str(), elf_header->e_machine); + return false; + } + + const lul::Endianness endianness = + big_endian ? lul::ENDIANNESS_BIG : lul::ENDIANNESS_LITTLE; + + // Find the call frame information and its size. + const char* cfi = GetOffset<ElfClass, char>(elf_header, section->sh_offset); + size_t cfi_size = section->sh_size; + + // Plug together the parser, handler, and their entourages. + + // Here's a summariser, which will receive the output of the + // parser, create summaries, and add them to |smap|. + Summariser summ(smap, text_bias, log); + + lul::ByteReader reader(endianness); + reader.SetAddressSize(ElfClass::kAddrSize); + + DwarfCFIToModule::Reporter module_reporter(log, dwarf_filename, section_name); + DwarfCFIToModule handler(num_dw_regs, &module_reporter, &reader, usu, &summ); + + // Provide the base addresses for .eh_frame encoded pointers, if + // possible. + reader.SetCFIDataBase(section->sh_addr, cfi); + if (got_section) reader.SetDataBase(got_section->sh_addr); + if (text_section) reader.SetTextBase(text_section->sh_addr); + + lul::CallFrameInfo::Reporter dwarf_reporter(log, dwarf_filename, + section_name); + lul::CallFrameInfo parser(cfi, cfi_size, &reader, &handler, &dwarf_reporter, + eh_frame); + parser.Start(); + + return true; +} + +bool LoadELF(const string& obj_file, MmapWrapper* map_wrapper, + void** elf_header) { + int obj_fd = open(obj_file.c_str(), O_RDONLY); + if (obj_fd < 0) { + fprintf(stderr, "Failed to open ELF file '%s': %s\n", obj_file.c_str(), + strerror(errno)); + return false; + } + FDWrapper obj_fd_wrapper(obj_fd); + struct stat st; + if (fstat(obj_fd, &st) != 0 && st.st_size <= 0) { + fprintf(stderr, "Unable to fstat ELF file '%s': %s\n", obj_file.c_str(), + strerror(errno)); + return false; + } + // Mapping it read-only is good enough. In any case, mapping it + // read-write confuses Valgrind's debuginfo acquire/discard + // heuristics, making it hard to profile the profiler. + void* obj_base = mmap(nullptr, st.st_size, PROT_READ, MAP_PRIVATE, obj_fd, 0); + if (obj_base == MAP_FAILED) { + fprintf(stderr, "Failed to mmap ELF file '%s': %s\n", obj_file.c_str(), + strerror(errno)); + return false; + } + map_wrapper->set(obj_base, st.st_size); + *elf_header = obj_base; + if (!IsValidElf(*elf_header)) { + fprintf(stderr, "Not a valid ELF file: %s\n", obj_file.c_str()); + return false; + } + return true; +} + +// Get the endianness of ELF_HEADER. If it's invalid, return false. +template <typename ElfClass> +bool ElfEndianness(const typename ElfClass::Ehdr* elf_header, + bool* big_endian) { + if (elf_header->e_ident[EI_DATA] == ELFDATA2LSB) { + *big_endian = false; + return true; + } + if (elf_header->e_ident[EI_DATA] == ELFDATA2MSB) { + *big_endian = true; + return true; + } + + fprintf(stderr, "bad data encoding in ELF header: %d\n", + elf_header->e_ident[EI_DATA]); + return false; +} + +// +// LoadSymbolsInfo +// +// Holds the state between the two calls to LoadSymbols() in case it's necessary +// to follow the .gnu_debuglink section and load debug information from a +// different file. +// +template <typename ElfClass> +class LoadSymbolsInfo { + public: + typedef typename ElfClass::Addr Addr; + + explicit LoadSymbolsInfo(const vector<string>& dbg_dirs) + : debug_dirs_(dbg_dirs), has_loading_addr_(false) {} + + // Keeps track of which sections have been loaded so sections don't + // accidentally get loaded twice from two different files. + void LoadedSection(const string& section) { + if (loaded_sections_.count(section) == 0) { + loaded_sections_.insert(section); + } else { + fprintf(stderr, "Section %s has already been loaded.\n", section.c_str()); + } + } + + string debuglink_file() const { return debuglink_file_; } + + private: + const vector<string>& debug_dirs_; // Directories in which to + // search for the debug ELF file. + + string debuglink_file_; // Full path to the debug ELF file. + + bool has_loading_addr_; // Indicate if LOADING_ADDR_ is valid. + + set<string> loaded_sections_; // Tracks the Loaded ELF sections + // between calls to LoadSymbols(). +}; + +// Find the preferred loading address of the binary. +template <typename ElfClass> +typename ElfClass::Addr GetLoadingAddress( + const typename ElfClass::Phdr* program_headers, int nheader) { + typedef typename ElfClass::Phdr Phdr; + + // For non-PIC executables (e_type == ET_EXEC), the load address is + // the start address of the first PT_LOAD segment. (ELF requires + // the segments to be sorted by load address.) For PIC executables + // and dynamic libraries (e_type == ET_DYN), this address will + // normally be zero. + for (int i = 0; i < nheader; ++i) { + const Phdr& header = program_headers[i]; + if (header.p_type == PT_LOAD) return header.p_vaddr; + } + return 0; +} + +template <typename ElfClass> +bool LoadSymbols(const string& obj_file, const bool big_endian, + const typename ElfClass::Ehdr* elf_header, + const bool read_gnu_debug_link, + LoadSymbolsInfo<ElfClass>* info, SecMap* smap, void* rx_avma, + size_t rx_size, UniqueStringUniverse* usu, + void (*log)(const char*)) { + typedef typename ElfClass::Phdr Phdr; + typedef typename ElfClass::Shdr Shdr; + + char buf[500]; + SprintfLiteral(buf, "LoadSymbols: BEGIN %s\n", obj_file.c_str()); + buf[sizeof(buf) - 1] = 0; + log(buf); + + // This is how the text bias is calculated. + // BEGIN CALCULATE BIAS + uintptr_t loading_addr = GetLoadingAddress<ElfClass>( + GetOffset<ElfClass, Phdr>(elf_header, elf_header->e_phoff), + elf_header->e_phnum); + uintptr_t text_bias = ((uintptr_t)rx_avma) - loading_addr; + SprintfLiteral(buf, "LoadSymbols: rx_avma=%llx, text_bias=%llx", + (unsigned long long int)(uintptr_t)rx_avma, + (unsigned long long int)text_bias); + buf[sizeof(buf) - 1] = 0; + log(buf); + // END CALCULATE BIAS + + const Shdr* sections = + GetOffset<ElfClass, Shdr>(elf_header, elf_header->e_shoff); + const Shdr* section_names = sections + elf_header->e_shstrndx; + const char* names = + GetOffset<ElfClass, char>(elf_header, section_names->sh_offset); + const char* names_end = names + section_names->sh_size; + bool found_usable_info = false; + + // Dwarf Call Frame Information (CFI) is actually independent from + // the other DWARF debugging information, and can be used alone. + const Shdr* dwarf_cfi_section = + FindElfSectionByName<ElfClass>(".debug_frame", SHT_PROGBITS, sections, + names, names_end, elf_header->e_shnum); + if (dwarf_cfi_section) { + // Ignore the return value of this function; even without call frame + // information, the other debugging information could be perfectly + // useful. + info->LoadedSection(".debug_frame"); + bool result = LoadDwarfCFI<ElfClass>(obj_file, elf_header, ".debug_frame", + dwarf_cfi_section, false, 0, 0, + big_endian, smap, text_bias, usu, log); + found_usable_info = found_usable_info || result; + if (result) log("LoadSymbols: read CFI from .debug_frame"); + } + + // Linux C++ exception handling information can also provide + // unwinding data. + const Shdr* eh_frame_section = + FindElfSectionByName<ElfClass>(".eh_frame", SHT_PROGBITS, sections, names, + names_end, elf_header->e_shnum); + if (eh_frame_section) { + // Pointers in .eh_frame data may be relative to the base addresses of + // certain sections. Provide those sections if present. + const Shdr* got_section = FindElfSectionByName<ElfClass>( + ".got", SHT_PROGBITS, sections, names, names_end, elf_header->e_shnum); + const Shdr* text_section = FindElfSectionByName<ElfClass>( + ".text", SHT_PROGBITS, sections, names, names_end, elf_header->e_shnum); + info->LoadedSection(".eh_frame"); + // As above, ignore the return value of this function. + bool result = LoadDwarfCFI<ElfClass>( + obj_file, elf_header, ".eh_frame", eh_frame_section, true, got_section, + text_section, big_endian, smap, text_bias, usu, log); + found_usable_info = found_usable_info || result; + if (result) log("LoadSymbols: read CFI from .eh_frame"); + } + + SprintfLiteral(buf, "LoadSymbols: END %s\n", obj_file.c_str()); + buf[sizeof(buf) - 1] = 0; + log(buf); + + return found_usable_info; +} + +// Return the breakpad symbol file identifier for the architecture of +// ELF_HEADER. +template <typename ElfClass> +const char* ElfArchitecture(const typename ElfClass::Ehdr* elf_header) { + typedef typename ElfClass::Half Half; + Half arch = elf_header->e_machine; + switch (arch) { + case EM_386: + return "x86"; + case EM_ARM: + return "arm"; + case EM_AARCH64: + return "arm64"; + case EM_MIPS: + return "mips"; + case EM_PPC64: + return "ppc64"; + case EM_PPC: + return "ppc"; + case EM_S390: + return "s390"; + case EM_SPARC: + return "sparc"; + case EM_SPARCV9: + return "sparcv9"; + case EM_X86_64: + return "x86_64"; + default: + return NULL; + } +} + +// Format the Elf file identifier in IDENTIFIER as a UUID with the +// dashes removed. +string FormatIdentifier(unsigned char identifier[16]) { + char identifier_str[40]; + lul::FileID::ConvertIdentifierToString(identifier, identifier_str, + sizeof(identifier_str)); + string id_no_dash; + for (int i = 0; identifier_str[i] != '\0'; ++i) + if (identifier_str[i] != '-') id_no_dash += identifier_str[i]; + // Add an extra "0" by the end. PDB files on Windows have an 'age' + // number appended to the end of the file identifier; this isn't + // really used or necessary on other platforms, but be consistent. + id_no_dash += '0'; + return id_no_dash; +} + +// Return the non-directory portion of FILENAME: the portion after the +// last slash, or the whole filename if there are no slashes. +string BaseFileName(const string& filename) { + // Lots of copies! basename's behavior is less than ideal. + char* c_filename = strdup(filename.c_str()); + string base = basename(c_filename); + free(c_filename); + return base; +} + +template <typename ElfClass> +bool ReadSymbolDataElfClass(const typename ElfClass::Ehdr* elf_header, + const string& obj_filename, + const vector<string>& debug_dirs, SecMap* smap, + void* rx_avma, size_t rx_size, + UniqueStringUniverse* usu, + void (*log)(const char*)) { + typedef typename ElfClass::Ehdr Ehdr; + + unsigned char identifier[16]; + if (!lul ::FileID::ElfFileIdentifierFromMappedFile(elf_header, identifier)) { + fprintf(stderr, "%s: unable to generate file identifier\n", + obj_filename.c_str()); + return false; + } + + const char* architecture = ElfArchitecture<ElfClass>(elf_header); + if (!architecture) { + fprintf(stderr, "%s: unrecognized ELF machine architecture: %d\n", + obj_filename.c_str(), elf_header->e_machine); + return false; + } + + // Figure out what endianness this file is. + bool big_endian; + if (!ElfEndianness<ElfClass>(elf_header, &big_endian)) return false; + + string name = BaseFileName(obj_filename); + string os = "Linux"; + string id = FormatIdentifier(identifier); + + LoadSymbolsInfo<ElfClass> info(debug_dirs); + if (!LoadSymbols<ElfClass>(obj_filename, big_endian, elf_header, + !debug_dirs.empty(), &info, smap, rx_avma, rx_size, + usu, log)) { + const string debuglink_file = info.debuglink_file(); + if (debuglink_file.empty()) return false; + + // Load debuglink ELF file. + fprintf(stderr, "Found debugging info in %s\n", debuglink_file.c_str()); + MmapWrapper debug_map_wrapper; + Ehdr* debug_elf_header = NULL; + if (!LoadELF(debuglink_file, &debug_map_wrapper, + reinterpret_cast<void**>(&debug_elf_header))) + return false; + // Sanity checks to make sure everything matches up. + const char* debug_architecture = + ElfArchitecture<ElfClass>(debug_elf_header); + if (!debug_architecture) { + fprintf(stderr, "%s: unrecognized ELF machine architecture: %d\n", + debuglink_file.c_str(), debug_elf_header->e_machine); + return false; + } + if (strcmp(architecture, debug_architecture)) { + fprintf(stderr, + "%s with ELF machine architecture %s does not match " + "%s with ELF architecture %s\n", + debuglink_file.c_str(), debug_architecture, obj_filename.c_str(), + architecture); + return false; + } + + bool debug_big_endian; + if (!ElfEndianness<ElfClass>(debug_elf_header, &debug_big_endian)) + return false; + if (debug_big_endian != big_endian) { + fprintf(stderr, "%s and %s does not match in endianness\n", + obj_filename.c_str(), debuglink_file.c_str()); + return false; + } + + if (!LoadSymbols<ElfClass>(debuglink_file, debug_big_endian, + debug_elf_header, false, &info, smap, rx_avma, + rx_size, usu, log)) { + return false; + } + } + + return true; +} + +} // namespace + +namespace lul { + +bool ReadSymbolDataInternal(const uint8_t* obj_file, const string& obj_filename, + const vector<string>& debug_dirs, SecMap* smap, + void* rx_avma, size_t rx_size, + UniqueStringUniverse* usu, + void (*log)(const char*)) { + if (!IsValidElf(obj_file)) { + fprintf(stderr, "Not a valid ELF file: %s\n", obj_filename.c_str()); + return false; + } + + int elfclass = ElfClass(obj_file); + if (elfclass == ELFCLASS32) { + return ReadSymbolDataElfClass<ElfClass32>( + reinterpret_cast<const Elf32_Ehdr*>(obj_file), obj_filename, debug_dirs, + smap, rx_avma, rx_size, usu, log); + } + if (elfclass == ELFCLASS64) { + return ReadSymbolDataElfClass<ElfClass64>( + reinterpret_cast<const Elf64_Ehdr*>(obj_file), obj_filename, debug_dirs, + smap, rx_avma, rx_size, usu, log); + } + + return false; +} + +bool ReadSymbolData(const string& obj_file, const vector<string>& debug_dirs, + SecMap* smap, void* rx_avma, size_t rx_size, + UniqueStringUniverse* usu, void (*log)(const char*)) { + MmapWrapper map_wrapper; + void* elf_header = NULL; + if (!LoadELF(obj_file, &map_wrapper, &elf_header)) return false; + + return ReadSymbolDataInternal(reinterpret_cast<uint8_t*>(elf_header), + obj_file, debug_dirs, smap, rx_avma, rx_size, + usu, log); +} + +namespace { + +template <typename ElfClass> +void FindElfClassSection(const char* elf_base, const char* section_name, + typename ElfClass::Word section_type, + const void** section_start, int* section_size) { + typedef typename ElfClass::Ehdr Ehdr; + typedef typename ElfClass::Shdr Shdr; + + MOZ_ASSERT(elf_base); + MOZ_ASSERT(section_start); + MOZ_ASSERT(section_size); + + MOZ_ASSERT(strncmp(elf_base, ELFMAG, SELFMAG) == 0); + + const Ehdr* elf_header = reinterpret_cast<const Ehdr*>(elf_base); + MOZ_ASSERT(elf_header->e_ident[EI_CLASS] == ElfClass::kClass); + + const Shdr* sections = + GetOffset<ElfClass, Shdr>(elf_header, elf_header->e_shoff); + const Shdr* section_names = sections + elf_header->e_shstrndx; + const char* names = + GetOffset<ElfClass, char>(elf_header, section_names->sh_offset); + const char* names_end = names + section_names->sh_size; + + const Shdr* section = + FindElfSectionByName<ElfClass>(section_name, section_type, sections, + names, names_end, elf_header->e_shnum); + + if (section != NULL && section->sh_size > 0) { + *section_start = elf_base + section->sh_offset; + *section_size = section->sh_size; + } +} + +template <typename ElfClass> +void FindElfClassSegment(const char* elf_base, + typename ElfClass::Word segment_type, + const void** segment_start, int* segment_size) { + typedef typename ElfClass::Ehdr Ehdr; + typedef typename ElfClass::Phdr Phdr; + + MOZ_ASSERT(elf_base); + MOZ_ASSERT(segment_start); + MOZ_ASSERT(segment_size); + + MOZ_ASSERT(strncmp(elf_base, ELFMAG, SELFMAG) == 0); + + const Ehdr* elf_header = reinterpret_cast<const Ehdr*>(elf_base); + MOZ_ASSERT(elf_header->e_ident[EI_CLASS] == ElfClass::kClass); + + const Phdr* phdrs = + GetOffset<ElfClass, Phdr>(elf_header, elf_header->e_phoff); + + for (int i = 0; i < elf_header->e_phnum; ++i) { + if (phdrs[i].p_type == segment_type) { + *segment_start = elf_base + phdrs[i].p_offset; + *segment_size = phdrs[i].p_filesz; + return; + } + } +} + +} // namespace + +bool IsValidElf(const void* elf_base) { + return strncmp(reinterpret_cast<const char*>(elf_base), ELFMAG, SELFMAG) == 0; +} + +int ElfClass(const void* elf_base) { + const ElfW(Ehdr)* elf_header = reinterpret_cast<const ElfW(Ehdr)*>(elf_base); + + return elf_header->e_ident[EI_CLASS]; +} + +bool FindElfSection(const void* elf_mapped_base, const char* section_name, + uint32_t section_type, const void** section_start, + int* section_size, int* elfclass) { + MOZ_ASSERT(elf_mapped_base); + MOZ_ASSERT(section_start); + MOZ_ASSERT(section_size); + + *section_start = NULL; + *section_size = 0; + + if (!IsValidElf(elf_mapped_base)) return false; + + int cls = ElfClass(elf_mapped_base); + if (elfclass) { + *elfclass = cls; + } + + const char* elf_base = static_cast<const char*>(elf_mapped_base); + + if (cls == ELFCLASS32) { + FindElfClassSection<ElfClass32>(elf_base, section_name, section_type, + section_start, section_size); + return *section_start != NULL; + } else if (cls == ELFCLASS64) { + FindElfClassSection<ElfClass64>(elf_base, section_name, section_type, + section_start, section_size); + return *section_start != NULL; + } + + return false; +} + +bool FindElfSegment(const void* elf_mapped_base, uint32_t segment_type, + const void** segment_start, int* segment_size, + int* elfclass) { + MOZ_ASSERT(elf_mapped_base); + MOZ_ASSERT(segment_start); + MOZ_ASSERT(segment_size); + + *segment_start = NULL; + *segment_size = 0; + + if (!IsValidElf(elf_mapped_base)) return false; + + int cls = ElfClass(elf_mapped_base); + if (elfclass) { + *elfclass = cls; + } + + const char* elf_base = static_cast<const char*>(elf_mapped_base); + + if (cls == ELFCLASS32) { + FindElfClassSegment<ElfClass32>(elf_base, segment_type, segment_start, + segment_size); + return *segment_start != NULL; + } else if (cls == ELFCLASS64) { + FindElfClassSegment<ElfClass64>(elf_base, segment_type, segment_start, + segment_size); + return *segment_start != NULL; + } + + return false; +} + +// (derived from) +// file_id.cc: Return a unique identifier for a file +// +// See file_id.h for documentation +// + +// ELF note name and desc are 32-bits word padded. +#define NOTE_PADDING(a) ((a + 3) & ~3) + +// These functions are also used inside the crashed process, so be safe +// and use the syscall/libc wrappers instead of direct syscalls or libc. + +template <typename ElfClass> +static bool ElfClassBuildIDNoteIdentifier(const void* section, int length, + uint8_t identifier[kMDGUIDSize]) { + typedef typename ElfClass::Nhdr Nhdr; + + const void* section_end = reinterpret_cast<const char*>(section) + length; + const Nhdr* note_header = reinterpret_cast<const Nhdr*>(section); + while (reinterpret_cast<const void*>(note_header) < section_end) { + if (note_header->n_type == NT_GNU_BUILD_ID) break; + note_header = reinterpret_cast<const Nhdr*>( + reinterpret_cast<const char*>(note_header) + sizeof(Nhdr) + + NOTE_PADDING(note_header->n_namesz) + + NOTE_PADDING(note_header->n_descsz)); + } + if (reinterpret_cast<const void*>(note_header) >= section_end || + note_header->n_descsz == 0) { + return false; + } + + const char* build_id = reinterpret_cast<const char*>(note_header) + + sizeof(Nhdr) + NOTE_PADDING(note_header->n_namesz); + // Copy as many bits of the build ID as will fit + // into the GUID space. + memset(identifier, 0, kMDGUIDSize); + memcpy(identifier, build_id, + std::min(kMDGUIDSize, (size_t)note_header->n_descsz)); + + return true; +} + +// Attempt to locate a .note.gnu.build-id section in an ELF binary +// and copy as many bytes of it as will fit into |identifier|. +static bool FindElfBuildIDNote(const void* elf_mapped_base, + uint8_t identifier[kMDGUIDSize]) { + void* note_section; + int note_size, elfclass; + if ((!FindElfSegment(elf_mapped_base, PT_NOTE, (const void**)¬e_section, + ¬e_size, &elfclass) || + note_size == 0) && + (!FindElfSection(elf_mapped_base, ".note.gnu.build-id", SHT_NOTE, + (const void**)¬e_section, ¬e_size, &elfclass) || + note_size == 0)) { + return false; + } + + if (elfclass == ELFCLASS32) { + return ElfClassBuildIDNoteIdentifier<ElfClass32>(note_section, note_size, + identifier); + } else if (elfclass == ELFCLASS64) { + return ElfClassBuildIDNoteIdentifier<ElfClass64>(note_section, note_size, + identifier); + } + + return false; +} + +// Attempt to locate the .text section of an ELF binary and generate +// a simple hash by XORing the first page worth of bytes into |identifier|. +static bool HashElfTextSection(const void* elf_mapped_base, + uint8_t identifier[kMDGUIDSize]) { + void* text_section; + int text_size; + if (!FindElfSection(elf_mapped_base, ".text", SHT_PROGBITS, + (const void**)&text_section, &text_size, NULL) || + text_size == 0) { + return false; + } + + memset(identifier, 0, kMDGUIDSize); + const uint8_t* ptr = reinterpret_cast<const uint8_t*>(text_section); + const uint8_t* ptr_end = ptr + std::min(text_size, 4096); + while (ptr < ptr_end) { + for (unsigned i = 0; i < kMDGUIDSize; i++) identifier[i] ^= ptr[i]; + ptr += kMDGUIDSize; + } + return true; +} + +// static +bool FileID::ElfFileIdentifierFromMappedFile(const void* base, + uint8_t identifier[kMDGUIDSize]) { + // Look for a build id note first. + if (FindElfBuildIDNote(base, identifier)) return true; + + // Fall back on hashing the first page of the text section. + return HashElfTextSection(base, identifier); +} + +// static +void FileID::ConvertIdentifierToString(const uint8_t identifier[kMDGUIDSize], + char* buffer, int buffer_length) { + uint8_t identifier_swapped[kMDGUIDSize]; + + // Endian-ness swap to match dump processor expectation. + memcpy(identifier_swapped, identifier, kMDGUIDSize); + uint32_t* data1 = reinterpret_cast<uint32_t*>(identifier_swapped); + *data1 = htonl(*data1); + uint16_t* data2 = reinterpret_cast<uint16_t*>(identifier_swapped + 4); + *data2 = htons(*data2); + uint16_t* data3 = reinterpret_cast<uint16_t*>(identifier_swapped + 6); + *data3 = htons(*data3); + + int buffer_idx = 0; + for (unsigned int idx = 0; + (buffer_idx < buffer_length) && (idx < kMDGUIDSize); ++idx) { + int hi = (identifier_swapped[idx] >> 4) & 0x0F; + int lo = (identifier_swapped[idx]) & 0x0F; + + if (idx == 4 || idx == 6 || idx == 8 || idx == 10) + buffer[buffer_idx++] = '-'; + + buffer[buffer_idx++] = (hi >= 10) ? 'A' + hi - 10 : '0' + hi; + buffer[buffer_idx++] = (lo >= 10) ? 'A' + lo - 10 : '0' + lo; + } + + // NULL terminate + buffer[(buffer_idx < buffer_length) ? buffer_idx : buffer_idx - 1] = 0; +} + +} // namespace lul diff --git a/mozglue/baseprofiler/lul/LulElfExt.h b/mozglue/baseprofiler/lul/LulElfExt.h new file mode 100644 index 0000000000..73d9ff7f15 --- /dev/null +++ b/mozglue/baseprofiler/lul/LulElfExt.h @@ -0,0 +1,69 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ + +// Copyright (c) 2006, 2011, 2012 Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// This file is derived from the following files in +// toolkit/crashreporter/google-breakpad: +// src/common/linux/dump_symbols.h + +#ifndef LulElfExt_h +#define LulElfExt_h + +// These two functions are the external interface to the +// ELF/Dwarf/EXIDX reader. + +#include "LulMainInt.h" + +using lul::SecMap; + +namespace lul { + +class UniqueStringUniverse; + +// Find all the unwind information in OBJ_FILE, an ELF executable +// or shared library, and add it to SMAP. +bool ReadSymbolData(const std::string& obj_file, + const std::vector<std::string>& debug_dirs, SecMap* smap, + void* rx_avma, size_t rx_size, UniqueStringUniverse* usu, + void (*log)(const char*)); + +// The same as ReadSymbolData, except that OBJ_FILE is assumed to +// point to a mapped-in image of OBJ_FILENAME. +bool ReadSymbolDataInternal(const uint8_t* obj_file, + const std::string& obj_filename, + const std::vector<std::string>& debug_dirs, + SecMap* smap, void* rx_avma, size_t rx_size, + UniqueStringUniverse* usu, + void (*log)(const char*)); + +} // namespace lul + +#endif // LulElfExt_h diff --git a/mozglue/baseprofiler/lul/LulElfInt.h b/mozglue/baseprofiler/lul/LulElfInt.h new file mode 100644 index 0000000000..31ffba8ff0 --- /dev/null +++ b/mozglue/baseprofiler/lul/LulElfInt.h @@ -0,0 +1,218 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ + +// Copyright (c) 2006, 2012, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// This file is derived from the following files in +// toolkit/crashreporter/google-breakpad: +// src/common/android/include/elf.h +// src/common/linux/elfutils.h +// src/common/linux/file_id.h +// src/common/linux/elfutils-inl.h + +#ifndef LulElfInt_h +#define LulElfInt_h + +// This header defines functions etc internal to the ELF reader. It +// should not be included outside of LulElf.cpp. + +#include <elf.h> +#include <stdlib.h> + +#include "mozilla/Assertions.h" + +#include "PlatformMacros.h" + +// (derived from) +// elfutils.h: Utilities for dealing with ELF files. +// +#include <link.h> + +#if defined(GP_OS_android) + +// From toolkit/crashreporter/google-breakpad/src/common/android/include/elf.h +// The Android headers don't always define this constant. +# ifndef EM_X86_64 +# define EM_X86_64 62 +# endif + +# ifndef EM_PPC64 +# define EM_PPC64 21 +# endif + +# ifndef EM_S390 +# define EM_S390 22 +# endif + +# ifndef NT_GNU_BUILD_ID +# define NT_GNU_BUILD_ID 3 +# endif + +# ifndef ElfW +# define ElfW(type) _ElfW(Elf, ELFSIZE, type) +# define _ElfW(e, w, t) _ElfW_1(e, w, _##t) +# define _ElfW_1(e, w, t) e##w##t +# endif + +#endif + +#if defined(GP_OS_freebsd) + +# ifndef ElfW +# define ElfW(type) Elf_##type +# endif + +#endif + +namespace lul { + +// Traits classes so consumers can write templatized code to deal +// with specific ELF bits. +struct ElfClass32 { + typedef Elf32_Addr Addr; + typedef Elf32_Ehdr Ehdr; + typedef Elf32_Nhdr Nhdr; + typedef Elf32_Phdr Phdr; + typedef Elf32_Shdr Shdr; + typedef Elf32_Half Half; + typedef Elf32_Off Off; + typedef Elf32_Word Word; + static const int kClass = ELFCLASS32; + static const size_t kAddrSize = sizeof(Elf32_Addr); +}; + +struct ElfClass64 { + typedef Elf64_Addr Addr; + typedef Elf64_Ehdr Ehdr; + typedef Elf64_Nhdr Nhdr; + typedef Elf64_Phdr Phdr; + typedef Elf64_Shdr Shdr; + typedef Elf64_Half Half; + typedef Elf64_Off Off; + typedef Elf64_Word Word; + static const int kClass = ELFCLASS64; + static const size_t kAddrSize = sizeof(Elf64_Addr); +}; + +bool IsValidElf(const void* elf_header); +int ElfClass(const void* elf_base); + +// Attempt to find a section named |section_name| of type |section_type| +// in the ELF binary data at |elf_mapped_base|. On success, returns true +// and sets |*section_start| to point to the start of the section data, +// and |*section_size| to the size of the section's data. If |elfclass| +// is not NULL, set |*elfclass| to the ELF file class. +bool FindElfSection(const void* elf_mapped_base, const char* section_name, + uint32_t section_type, const void** section_start, + int* section_size, int* elfclass); + +// Internal helper method, exposed for convenience for callers +// that already have more info. +template <typename ElfClass> +const typename ElfClass::Shdr* FindElfSectionByName( + const char* name, typename ElfClass::Word section_type, + const typename ElfClass::Shdr* sections, const char* section_names, + const char* names_end, int nsection); + +// Attempt to find the first segment of type |segment_type| in the ELF +// binary data at |elf_mapped_base|. On success, returns true and sets +// |*segment_start| to point to the start of the segment data, and +// and |*segment_size| to the size of the segment's data. If |elfclass| +// is not NULL, set |*elfclass| to the ELF file class. +bool FindElfSegment(const void* elf_mapped_base, uint32_t segment_type, + const void** segment_start, int* segment_size, + int* elfclass); + +// Convert an offset from an Elf header into a pointer to the mapped +// address in the current process. Takes an extra template parameter +// to specify the return type to avoid having to dynamic_cast the +// result. +template <typename ElfClass, typename T> +const T* GetOffset(const typename ElfClass::Ehdr* elf_header, + typename ElfClass::Off offset); + +// (derived from) +// file_id.h: Return a unique identifier for a file +// + +static const size_t kMDGUIDSize = sizeof(MDGUID); + +class FileID { + public: + // Load the identifier for the elf file mapped into memory at |base| into + // |identifier|. Return false if the identifier could not be created for the + // file. + static bool ElfFileIdentifierFromMappedFile(const void* base, + uint8_t identifier[kMDGUIDSize]); + + // Convert the |identifier| data to a NULL terminated string. The string will + // be formatted as a UUID (e.g., 22F065BB-FC9C-49F7-80FE-26A7CEBD7BCE). + // The |buffer| should be at least 37 bytes long to receive all of the data + // and termination. Shorter buffers will contain truncated data. + static void ConvertIdentifierToString(const uint8_t identifier[kMDGUIDSize], + char* buffer, int buffer_length); +}; + +template <typename ElfClass, typename T> +const T* GetOffset(const typename ElfClass::Ehdr* elf_header, + typename ElfClass::Off offset) { + return reinterpret_cast<const T*>(reinterpret_cast<uintptr_t>(elf_header) + + offset); +} + +template <typename ElfClass> +const typename ElfClass::Shdr* FindElfSectionByName( + const char* name, typename ElfClass::Word section_type, + const typename ElfClass::Shdr* sections, const char* section_names, + const char* names_end, int nsection) { + MOZ_ASSERT(name != NULL); + MOZ_ASSERT(sections != NULL); + MOZ_ASSERT(nsection > 0); + + int name_len = strlen(name); + if (name_len == 0) return NULL; + + for (int i = 0; i < nsection; ++i) { + const char* section_name = section_names + sections[i].sh_name; + if (sections[i].sh_type == section_type && + names_end - section_name >= name_len + 1 && + strcmp(name, section_name) == 0) { + return sections + i; + } + } + return NULL; +} + +} // namespace lul + +// And finally, the external interface, offered to LulMain.cpp +#include "LulElfExt.h" + +#endif // LulElfInt_h diff --git a/mozglue/baseprofiler/lul/LulMain.cpp b/mozglue/baseprofiler/lul/LulMain.cpp new file mode 100644 index 0000000000..75f205ac3a --- /dev/null +++ b/mozglue/baseprofiler/lul/LulMain.cpp @@ -0,0 +1,1958 @@ +/* -*- 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 "LulMain.h" + +#include <string.h> +#include <stdlib.h> +#include <stdio.h> +#include <unistd.h> // write(), only for testing LUL + +#include <algorithm> // std::sort +#include <string> +#include <utility> + +#include "mozilla/Assertions.h" +#include "mozilla/ArrayUtils.h" +#include "mozilla/CheckedInt.h" +#include "mozilla/DebugOnly.h" +#include "mozilla/MemoryChecking.h" +#include "mozilla/Sprintf.h" +#include "mozilla/UniquePtr.h" +#include "mozilla/Unused.h" + +#include "BaseProfiler.h" +#include "LulCommonExt.h" +#include "LulElfExt.h" +#include "LulMainInt.h" + +using mozilla::baseprofiler::profiler_current_process_id; +using mozilla::baseprofiler::profiler_current_thread_id; + +// Set this to 1 for verbose logging +#define DEBUG_MAIN 0 + +namespace lul { + +using mozilla::CheckedInt; +using mozilla::DebugOnly; +using mozilla::MallocSizeOf; +using mozilla::Unused; +using std::pair; +using std::string; +using std::vector; + +// WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING +// +// Some functions in this file are marked RUNS IN NO-MALLOC CONTEXT. +// Any such function -- and, hence, the transitive closure of those +// reachable from it -- must not do any dynamic memory allocation. +// Doing so risks deadlock. There is exactly one root function for +// the transitive closure: Lul::Unwind. +// +// WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING + +//////////////////////////////////////////////////////////////// +// RuleSet // +//////////////////////////////////////////////////////////////// + +static const char* NameOf_DW_REG(int16_t aReg) { + switch (aReg) { + case DW_REG_CFA: + return "cfa"; +#if defined(GP_ARCH_amd64) || defined(GP_ARCH_x86) + case DW_REG_INTEL_XBP: + return "xbp"; + case DW_REG_INTEL_XSP: + return "xsp"; + case DW_REG_INTEL_XIP: + return "xip"; +#elif defined(GP_ARCH_arm) + case DW_REG_ARM_R7: + return "r7"; + case DW_REG_ARM_R11: + return "r11"; + case DW_REG_ARM_R12: + return "r12"; + case DW_REG_ARM_R13: + return "r13"; + case DW_REG_ARM_R14: + return "r14"; + case DW_REG_ARM_R15: + return "r15"; +#elif defined(GP_ARCH_arm64) + case DW_REG_AARCH64_X29: + return "x29"; + case DW_REG_AARCH64_X30: + return "x30"; + case DW_REG_AARCH64_SP: + return "sp"; +#elif defined(GP_ARCH_mips64) + case DW_REG_MIPS_SP: + return "sp"; + case DW_REG_MIPS_FP: + return "fp"; + case DW_REG_MIPS_PC: + return "pc"; +#else +# error "Unsupported arch" +#endif + default: + return "???"; + } +} + +string LExpr::ShowRule(const char* aNewReg) const { + char buf[64]; + string res = string(aNewReg) + "="; + switch (mHow) { + case UNKNOWN: + res += "Unknown"; + break; + case NODEREF: + SprintfLiteral(buf, "%s+%d", NameOf_DW_REG(mReg), (int)mOffset); + res += buf; + break; + case DEREF: + SprintfLiteral(buf, "*(%s+%d)", NameOf_DW_REG(mReg), (int)mOffset); + res += buf; + break; + case PFXEXPR: + SprintfLiteral(buf, "PfxExpr-at-%d", (int)mOffset); + res += buf; + break; + default: + res += "???"; + break; + } + return res; +} + +void RuleSet::Print(void (*aLog)(const char*)) const { + char buf[96]; + SprintfLiteral(buf, "[%llx .. %llx]: let ", (unsigned long long int)mAddr, + (unsigned long long int)(mAddr + mLen - 1)); + string res = string(buf); + res += mCfaExpr.ShowRule("cfa"); + res += " in"; + // For each reg we care about, print the recovery expression. +#if defined(GP_ARCH_amd64) || defined(GP_ARCH_x86) + res += mXipExpr.ShowRule(" RA"); + res += mXspExpr.ShowRule(" SP"); + res += mXbpExpr.ShowRule(" BP"); +#elif defined(GP_ARCH_arm) + res += mR15expr.ShowRule(" R15"); + res += mR7expr.ShowRule(" R7"); + res += mR11expr.ShowRule(" R11"); + res += mR12expr.ShowRule(" R12"); + res += mR13expr.ShowRule(" R13"); + res += mR14expr.ShowRule(" R14"); +#elif defined(GP_ARCH_arm64) + res += mX29expr.ShowRule(" X29"); + res += mX30expr.ShowRule(" X30"); + res += mSPexpr.ShowRule(" SP"); +#elif defined(GP_ARCH_mips64) + res += mPCexpr.ShowRule(" PC"); + res += mSPexpr.ShowRule(" SP"); + res += mFPexpr.ShowRule(" FP"); +#else +# error "Unsupported arch" +#endif + aLog(res.c_str()); +} + +LExpr* RuleSet::ExprForRegno(DW_REG_NUMBER aRegno) { + switch (aRegno) { + case DW_REG_CFA: + return &mCfaExpr; +#if defined(GP_ARCH_amd64) || defined(GP_ARCH_x86) + case DW_REG_INTEL_XIP: + return &mXipExpr; + case DW_REG_INTEL_XSP: + return &mXspExpr; + case DW_REG_INTEL_XBP: + return &mXbpExpr; +#elif defined(GP_ARCH_arm) + case DW_REG_ARM_R15: + return &mR15expr; + case DW_REG_ARM_R14: + return &mR14expr; + case DW_REG_ARM_R13: + return &mR13expr; + case DW_REG_ARM_R12: + return &mR12expr; + case DW_REG_ARM_R11: + return &mR11expr; + case DW_REG_ARM_R7: + return &mR7expr; +#elif defined(GP_ARCH_arm64) + case DW_REG_AARCH64_X29: + return &mX29expr; + case DW_REG_AARCH64_X30: + return &mX30expr; + case DW_REG_AARCH64_SP: + return &mSPexpr; +#elif defined(GP_ARCH_mips64) + case DW_REG_MIPS_SP: + return &mSPexpr; + case DW_REG_MIPS_FP: + return &mFPexpr; + case DW_REG_MIPS_PC: + return &mPCexpr; +#else +# error "Unknown arch" +#endif + default: + return nullptr; + } +} + +RuleSet::RuleSet() { + mAddr = 0; + mLen = 0; + // The only other fields are of type LExpr and those are initialised + // by LExpr::LExpr(). +} + +//////////////////////////////////////////////////////////////// +// SecMap // +//////////////////////////////////////////////////////////////// + +// See header file LulMainInt.h for comments about invariants. + +SecMap::SecMap(void (*aLog)(const char*)) + : mSummaryMinAddr(1), mSummaryMaxAddr(0), mUsable(true), mLog(aLog) {} + +SecMap::~SecMap() { mRuleSets.clear(); } + +// RUNS IN NO-MALLOC CONTEXT +RuleSet* SecMap::FindRuleSet(uintptr_t ia) { + // Binary search mRuleSets to find one that brackets |ia|. + // lo and hi need to be signed, else the loop termination tests + // don't work properly. Note that this works correctly even when + // mRuleSets.size() == 0. + + // Can't do this until the array has been sorted and preened. + MOZ_ASSERT(mUsable); + + long int lo = 0; + long int hi = (long int)mRuleSets.size() - 1; + while (true) { + // current unsearched space is from lo to hi, inclusive. + if (lo > hi) { + // not found + return nullptr; + } + long int mid = lo + ((hi - lo) / 2); + RuleSet* mid_ruleSet = &mRuleSets[mid]; + uintptr_t mid_minAddr = mid_ruleSet->mAddr; + uintptr_t mid_maxAddr = mid_minAddr + mid_ruleSet->mLen - 1; + if (ia < mid_minAddr) { + hi = mid - 1; + continue; + } + if (ia > mid_maxAddr) { + lo = mid + 1; + continue; + } + MOZ_ASSERT(mid_minAddr <= ia && ia <= mid_maxAddr); + return mid_ruleSet; + } + // NOTREACHED +} + +// Add a RuleSet to the collection. The rule is copied in. Calling +// this makes the map non-searchable. +void SecMap::AddRuleSet(const RuleSet* rs) { + mUsable = false; + mRuleSets.push_back(*rs); +} + +// Add a PfxInstr to the vector of such instrs, and return the index +// in the vector. Calling this makes the map non-searchable. +uint32_t SecMap::AddPfxInstr(PfxInstr pfxi) { + mUsable = false; + mPfxInstrs.push_back(pfxi); + return mPfxInstrs.size() - 1; +} + +static bool CmpRuleSetsByAddrLE(const RuleSet& rs1, const RuleSet& rs2) { + return rs1.mAddr < rs2.mAddr; +} + +// Prepare the map for searching. Completely remove any which don't +// fall inside the specified range [start, +len). +void SecMap::PrepareRuleSets(uintptr_t aStart, size_t aLen) { + if (mRuleSets.empty()) { + return; + } + + MOZ_ASSERT(aLen > 0); + if (aLen == 0) { + // This should never happen. + mRuleSets.clear(); + return; + } + + // Sort by start addresses. + std::sort(mRuleSets.begin(), mRuleSets.end(), CmpRuleSetsByAddrLE); + + // Detect any entry not completely contained within [start, +len). + // Set its length to zero, so that the next pass will remove it. + for (size_t i = 0; i < mRuleSets.size(); ++i) { + RuleSet* rs = &mRuleSets[i]; + if (rs->mLen > 0 && + (rs->mAddr < aStart || rs->mAddr + rs->mLen > aStart + aLen)) { + rs->mLen = 0; + } + } + + // Iteratively truncate any overlaps and remove any zero length + // entries that might result, or that may have been present + // initially. Unless the input is seriously screwy, this is + // expected to iterate only once. + while (true) { + size_t i; + size_t n = mRuleSets.size(); + size_t nZeroLen = 0; + + if (n == 0) { + break; + } + + for (i = 1; i < n; ++i) { + RuleSet* prev = &mRuleSets[i - 1]; + RuleSet* here = &mRuleSets[i]; + MOZ_ASSERT(prev->mAddr <= here->mAddr); + if (prev->mAddr + prev->mLen > here->mAddr) { + prev->mLen = here->mAddr - prev->mAddr; + } + if (prev->mLen == 0) nZeroLen++; + } + + if (mRuleSets[n - 1].mLen == 0) { + nZeroLen++; + } + + // At this point, the entries are in-order and non-overlapping. + // If none of them are zero-length, we are done. + if (nZeroLen == 0) { + break; + } + + // Slide back the entries to remove the zero length ones. + size_t j = 0; // The write-point. + for (i = 0; i < n; ++i) { + if (mRuleSets[i].mLen == 0) { + continue; + } + if (j != i) mRuleSets[j] = mRuleSets[i]; + ++j; + } + MOZ_ASSERT(i == n); + MOZ_ASSERT(nZeroLen <= n); + MOZ_ASSERT(j == n - nZeroLen); + while (nZeroLen > 0) { + mRuleSets.pop_back(); + nZeroLen--; + } + + MOZ_ASSERT(mRuleSets.size() == j); + } + + size_t n = mRuleSets.size(); + +#ifdef DEBUG + // Do a final check on the rules: their address ranges must be + // ascending, non overlapping, non zero sized. + if (n > 0) { + MOZ_ASSERT(mRuleSets[0].mLen > 0); + for (size_t i = 1; i < n; ++i) { + RuleSet* prev = &mRuleSets[i - 1]; + RuleSet* here = &mRuleSets[i]; + MOZ_ASSERT(prev->mAddr < here->mAddr); + MOZ_ASSERT(here->mLen > 0); + MOZ_ASSERT(prev->mAddr + prev->mLen <= here->mAddr); + } + } +#endif + + // Set the summary min and max address values. + if (n == 0) { + // Use the values defined in comments in the class declaration. + mSummaryMinAddr = 1; + mSummaryMaxAddr = 0; + } else { + mSummaryMinAddr = mRuleSets[0].mAddr; + mSummaryMaxAddr = mRuleSets[n - 1].mAddr + mRuleSets[n - 1].mLen - 1; + } + char buf[150]; + SprintfLiteral(buf, "PrepareRuleSets: %d entries, smin/smax 0x%llx, 0x%llx\n", + (int)n, (unsigned long long int)mSummaryMinAddr, + (unsigned long long int)mSummaryMaxAddr); + buf[sizeof(buf) - 1] = 0; + mLog(buf); + + // Is now usable for binary search. + mUsable = true; + +#if 0 + mLog("\nRulesets after preening\n"); + for (size_t i = 0; i < mRuleSets.size(); ++i) { + mRuleSets[i].Print(mLog); + mLog("\n"); + } + mLog("\n"); +#endif +} + +bool SecMap::IsEmpty() { return mRuleSets.empty(); } + +size_t SecMap::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const { + size_t n = aMallocSizeOf(this); + + // It's conceivable that these calls would be unsafe with some + // implementations of std::vector, but it seems to be working for now... + n += aMallocSizeOf(mRuleSets.data()); + n += aMallocSizeOf(mPfxInstrs.data()); + + return n; +} + +//////////////////////////////////////////////////////////////// +// SegArray // +//////////////////////////////////////////////////////////////// + +// A SegArray holds a set of address ranges that together exactly +// cover an address range, with no overlaps or holes. Each range has +// an associated value, which in this case has been specialised to be +// a simple boolean. The representation is kept to minimal canonical +// form in which adjacent ranges with the same associated value are +// merged together. Each range is represented by a |struct Seg|. +// +// SegArrays are used to keep track of which parts of the address +// space are known to contain instructions. +class SegArray { + public: + void add(uintptr_t lo, uintptr_t hi, bool val) { + if (lo > hi) { + return; + } + split_at(lo); + if (hi < UINTPTR_MAX) { + split_at(hi + 1); + } + std::vector<Seg>::size_type iLo, iHi, i; + iLo = find(lo); + iHi = find(hi); + for (i = iLo; i <= iHi; ++i) { + mSegs[i].val = val; + } + preen(); + } + + // RUNS IN NO-MALLOC CONTEXT + bool getBoundingCodeSegment(/*OUT*/ uintptr_t* rx_min, + /*OUT*/ uintptr_t* rx_max, uintptr_t addr) { + std::vector<Seg>::size_type i = find(addr); + if (!mSegs[i].val) { + return false; + } + *rx_min = mSegs[i].lo; + *rx_max = mSegs[i].hi; + return true; + } + + SegArray() { + Seg s(0, UINTPTR_MAX, false); + mSegs.push_back(s); + } + + private: + struct Seg { + Seg(uintptr_t lo, uintptr_t hi, bool val) : lo(lo), hi(hi), val(val) {} + uintptr_t lo; + uintptr_t hi; + bool val; + }; + + void preen() { + for (std::vector<Seg>::iterator iter = mSegs.begin(); + iter < mSegs.end() - 1; ++iter) { + if (iter[0].val != iter[1].val) { + continue; + } + iter[0].hi = iter[1].hi; + mSegs.erase(iter + 1); + // Back up one, so as not to miss an opportunity to merge + // with the entry after this one. + --iter; + } + } + + // RUNS IN NO-MALLOC CONTEXT + std::vector<Seg>::size_type find(uintptr_t a) { + long int lo = 0; + long int hi = (long int)mSegs.size(); + while (true) { + // The unsearched space is lo .. hi inclusive. + if (lo > hi) { + // Not found. This can't happen. + return (std::vector<Seg>::size_type)(-1); + } + long int mid = lo + ((hi - lo) / 2); + uintptr_t mid_lo = mSegs[mid].lo; + uintptr_t mid_hi = mSegs[mid].hi; + if (a < mid_lo) { + hi = mid - 1; + continue; + } + if (a > mid_hi) { + lo = mid + 1; + continue; + } + return (std::vector<Seg>::size_type)mid; + } + } + + void split_at(uintptr_t a) { + std::vector<Seg>::size_type i = find(a); + if (mSegs[i].lo == a) { + return; + } + mSegs.insert(mSegs.begin() + i + 1, mSegs[i]); + mSegs[i].hi = a - 1; + mSegs[i + 1].lo = a; + } + + void show() { + printf("<< %d entries:\n", (int)mSegs.size()); + for (std::vector<Seg>::iterator iter = mSegs.begin(); iter < mSegs.end(); + ++iter) { + printf(" %016llx %016llx %s\n", (unsigned long long int)(*iter).lo, + (unsigned long long int)(*iter).hi, + (*iter).val ? "true" : "false"); + } + printf(">>\n"); + } + + std::vector<Seg> mSegs; +}; + +//////////////////////////////////////////////////////////////// +// PriMap // +//////////////////////////////////////////////////////////////// + +class PriMap { + public: + explicit PriMap(void (*aLog)(const char*)) : mLog(aLog) {} + + // RUNS IN NO-MALLOC CONTEXT + pair<const RuleSet*, const vector<PfxInstr>*> Lookup(uintptr_t ia) { + SecMap* sm = FindSecMap(ia); + return pair<const RuleSet*, const vector<PfxInstr>*>( + sm ? sm->FindRuleSet(ia) : nullptr, sm ? sm->GetPfxInstrs() : nullptr); + } + + // Add a secondary map. No overlaps allowed w.r.t. existing + // secondary maps. + void AddSecMap(mozilla::UniquePtr<SecMap>&& aSecMap) { + // We can't add an empty SecMap to the PriMap. But that's OK + // since we'd never be able to find anything in it anyway. + if (aSecMap->IsEmpty()) { + return; + } + + // Iterate through the SecMaps and find the right place for this + // one. At the same time, ensure that the in-order + // non-overlapping invariant is preserved (and, generally, holds). + // FIXME: this gives a cost that is O(N^2) in the total number of + // shared objects in the system. ToDo: better. + MOZ_ASSERT(aSecMap->mSummaryMinAddr <= aSecMap->mSummaryMaxAddr); + + size_t num_secMaps = mSecMaps.size(); + uintptr_t i; + for (i = 0; i < num_secMaps; ++i) { + mozilla::UniquePtr<SecMap>& sm_i = mSecMaps[i]; + MOZ_ASSERT(sm_i->mSummaryMinAddr <= sm_i->mSummaryMaxAddr); + if (aSecMap->mSummaryMinAddr < sm_i->mSummaryMaxAddr) { + // |aSecMap| needs to be inserted immediately before mSecMaps[i]. + break; + } + } + MOZ_ASSERT(i <= num_secMaps); + if (i == num_secMaps) { + // It goes at the end. + mSecMaps.push_back(std::move(aSecMap)); + } else { + std::vector<mozilla::UniquePtr<SecMap>>::iterator iter = + mSecMaps.begin() + i; + mSecMaps.insert(iter, std::move(aSecMap)); + } + char buf[100]; + SprintfLiteral(buf, "AddSecMap: now have %d SecMaps\n", + (int)mSecMaps.size()); + buf[sizeof(buf) - 1] = 0; + mLog(buf); + } + + // Remove and delete any SecMaps in the mapping, that intersect + // with the specified address range. + void RemoveSecMapsInRange(uintptr_t avma_min, uintptr_t avma_max) { + MOZ_ASSERT(avma_min <= avma_max); + size_t num_secMaps = mSecMaps.size(); + if (num_secMaps > 0) { + intptr_t i; + // Iterate from end to start over the vector, so as to ensure + // that the special case where |avma_min| and |avma_max| denote + // the entire address space, can be completed in time proportional + // to the number of elements in the map. + for (i = (intptr_t)num_secMaps - 1; i >= 0; i--) { + mozilla::UniquePtr<SecMap>& sm_i = mSecMaps[i]; + if (sm_i->mSummaryMaxAddr < avma_min || + avma_max < sm_i->mSummaryMinAddr) { + // There's no overlap. Move on. + continue; + } + // We need to remove mSecMaps[i] and slide all those above it + // downwards to cover the hole. + mSecMaps.erase(mSecMaps.begin() + i); + } + } + } + + // Return the number of currently contained SecMaps. + size_t CountSecMaps() { return mSecMaps.size(); } + + size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const { + size_t n = aMallocSizeOf(this); + + // It's conceivable that this call would be unsafe with some + // implementations of std::vector, but it seems to be working for now... + n += aMallocSizeOf(mSecMaps.data()); + + for (size_t i = 0; i < mSecMaps.size(); i++) { + n += mSecMaps[i]->SizeOfIncludingThis(aMallocSizeOf); + } + + return n; + } + + private: + // RUNS IN NO-MALLOC CONTEXT + SecMap* FindSecMap(uintptr_t ia) { + // Binary search mSecMaps to find one that brackets |ia|. + // lo and hi need to be signed, else the loop termination tests + // don't work properly. + long int lo = 0; + long int hi = (long int)mSecMaps.size() - 1; + while (true) { + // current unsearched space is from lo to hi, inclusive. + if (lo > hi) { + // not found + return nullptr; + } + long int mid = lo + ((hi - lo) / 2); + mozilla::UniquePtr<SecMap>& mid_secMap = mSecMaps[mid]; + uintptr_t mid_minAddr = mid_secMap->mSummaryMinAddr; + uintptr_t mid_maxAddr = mid_secMap->mSummaryMaxAddr; + if (ia < mid_minAddr) { + hi = mid - 1; + continue; + } + if (ia > mid_maxAddr) { + lo = mid + 1; + continue; + } + MOZ_ASSERT(mid_minAddr <= ia && ia <= mid_maxAddr); + return mid_secMap.get(); + } + // NOTREACHED + } + + private: + // sorted array of per-object ranges, non overlapping, non empty + std::vector<mozilla::UniquePtr<SecMap>> mSecMaps; + + // a logging sink, for debugging. + void (*mLog)(const char*); +}; + +//////////////////////////////////////////////////////////////// +// LUL // +//////////////////////////////////////////////////////////////// + +#define LUL_LOG(_str) \ + do { \ + char buf[200]; \ + SprintfLiteral(buf, "LUL: pid %" PRIu64 " tid %" PRIu64 " lul-obj %p: %s", \ + uint64_t(profiler_current_process_id().ToNumber()), \ + uint64_t(profiler_current_thread_id().ToNumber()), this, \ + (_str)); \ + buf[sizeof(buf) - 1] = 0; \ + mLog(buf); \ + } while (0) + +LUL::LUL(void (*aLog)(const char*)) + : mLog(aLog), + mAdminMode(true), + mAdminThreadId(profiler_current_thread_id()), + mPriMap(new PriMap(aLog)), + mSegArray(new SegArray()), + mUSU(new UniqueStringUniverse()) { + LUL_LOG("LUL::LUL: Created object"); +} + +LUL::~LUL() { + LUL_LOG("LUL::~LUL: Destroyed object"); + delete mPriMap; + delete mSegArray; + mLog = nullptr; + delete mUSU; +} + +void LUL::MaybeShowStats() { + // This is racey in the sense that it can't guarantee that + // n_new == n_new_Context + n_new_CFI + n_new_Scanned + // if it should happen that mStats is updated by some other thread + // in between computation of n_new and n_new_{Context,CFI,FP}. + // But it's just stats printing, so we don't really care. + uint32_t n_new = mStats - mStatsPrevious; + if (n_new >= 5000) { + uint32_t n_new_Context = mStats.mContext - mStatsPrevious.mContext; + uint32_t n_new_CFI = mStats.mCFI - mStatsPrevious.mCFI; + uint32_t n_new_FP = mStats.mFP - mStatsPrevious.mFP; + mStatsPrevious = mStats; + char buf[200]; + SprintfLiteral(buf, + "LUL frame stats: TOTAL %5u" + " CTX %4u CFI %4u FP %4u", + n_new, n_new_Context, n_new_CFI, n_new_FP); + buf[sizeof(buf) - 1] = 0; + mLog(buf); + } +} + +size_t LUL::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const { + size_t n = aMallocSizeOf(this); + n += mPriMap->SizeOfIncludingThis(aMallocSizeOf); + + // Measurement of the following members may be added later if DMD finds it + // is worthwhile: + // - mSegArray + // - mUSU + + return n; +} + +void LUL::EnableUnwinding() { + LUL_LOG("LUL::EnableUnwinding"); + // Don't assert for Admin mode here. That is, tolerate a call here + // if we are already in Unwinding mode. + MOZ_RELEASE_ASSERT(profiler_current_thread_id() == mAdminThreadId); + + mAdminMode = false; +} + +void LUL::NotifyAfterMap(uintptr_t aRXavma, size_t aSize, const char* aFileName, + const void* aMappedImage) { + MOZ_RELEASE_ASSERT(mAdminMode); + MOZ_RELEASE_ASSERT(profiler_current_thread_id() == mAdminThreadId); + + mLog(":\n"); + char buf[200]; + SprintfLiteral(buf, "NotifyMap %llx %llu %s\n", + (unsigned long long int)aRXavma, (unsigned long long int)aSize, + aFileName); + buf[sizeof(buf) - 1] = 0; + mLog(buf); + + // Ignore obviously-stupid notifications. + if (aSize > 0) { + // Here's a new mapping, for this object. + mozilla::UniquePtr<SecMap> smap = mozilla::MakeUnique<SecMap>(mLog); + + // Read CFI or EXIDX unwind data into |smap|. + if (!aMappedImage) { + (void)lul::ReadSymbolData(string(aFileName), std::vector<string>(), + smap.get(), (void*)aRXavma, aSize, mUSU, mLog); + } else { + (void)lul::ReadSymbolDataInternal( + (const uint8_t*)aMappedImage, string(aFileName), + std::vector<string>(), smap.get(), (void*)aRXavma, aSize, mUSU, mLog); + } + + mLog("NotifyMap .. preparing entries\n"); + + smap->PrepareRuleSets(aRXavma, aSize); + + SprintfLiteral(buf, "NotifyMap got %lld entries\n", + (long long int)smap->Size()); + buf[sizeof(buf) - 1] = 0; + mLog(buf); + + // Add it to the primary map (the top level set of mapped objects). + mPriMap->AddSecMap(std::move(smap)); + + // Tell the segment array about the mapping, so that the stack + // scan and __kernel_syscall mechanisms know where valid code is. + mSegArray->add(aRXavma, aRXavma + aSize - 1, true); + } +} + +void LUL::NotifyExecutableArea(uintptr_t aRXavma, size_t aSize) { + MOZ_RELEASE_ASSERT(mAdminMode); + MOZ_RELEASE_ASSERT(profiler_current_thread_id() == mAdminThreadId); + + mLog(":\n"); + char buf[200]; + SprintfLiteral(buf, "NotifyExecutableArea %llx %llu\n", + (unsigned long long int)aRXavma, + (unsigned long long int)aSize); + buf[sizeof(buf) - 1] = 0; + mLog(buf); + + // Ignore obviously-stupid notifications. + if (aSize > 0) { + // Tell the segment array about the mapping, so that the stack + // scan and __kernel_syscall mechanisms know where valid code is. + mSegArray->add(aRXavma, aRXavma + aSize - 1, true); + } +} + +void LUL::NotifyBeforeUnmap(uintptr_t aRXavmaMin, uintptr_t aRXavmaMax) { + MOZ_RELEASE_ASSERT(mAdminMode); + MOZ_RELEASE_ASSERT(profiler_current_thread_id() == mAdminThreadId); + + mLog(":\n"); + char buf[100]; + SprintfLiteral(buf, "NotifyUnmap %016llx-%016llx\n", + (unsigned long long int)aRXavmaMin, + (unsigned long long int)aRXavmaMax); + buf[sizeof(buf) - 1] = 0; + mLog(buf); + + MOZ_ASSERT(aRXavmaMin <= aRXavmaMax); + + // Remove from the primary map, any secondary maps that intersect + // with the address range. Also delete the secondary maps. + mPriMap->RemoveSecMapsInRange(aRXavmaMin, aRXavmaMax); + + // Tell the segment array that the address range no longer + // contains valid code. + mSegArray->add(aRXavmaMin, aRXavmaMax, false); + + SprintfLiteral(buf, "NotifyUnmap: now have %d SecMaps\n", + (int)mPriMap->CountSecMaps()); + buf[sizeof(buf) - 1] = 0; + mLog(buf); +} + +size_t LUL::CountMappings() { + MOZ_RELEASE_ASSERT(mAdminMode); + MOZ_RELEASE_ASSERT(profiler_current_thread_id() == mAdminThreadId); + + return mPriMap->CountSecMaps(); +} + +// RUNS IN NO-MALLOC CONTEXT +static TaggedUWord DerefTUW(TaggedUWord aAddr, const StackImage* aStackImg) { + if (!aAddr.Valid()) { + return TaggedUWord(); + } + + // Lower limit check. |aAddr.Value()| is the lowest requested address + // and |aStackImg->mStartAvma| is the lowest address we actually have, + // so the comparison is straightforward. + if (aAddr.Value() < aStackImg->mStartAvma) { + return TaggedUWord(); + } + + // Upper limit check. We must compute the highest requested address + // and the highest address we actually have, but being careful to + // avoid overflow. In particular if |aAddr| is 0xFFF...FFF or the + // 3/7 values below that, then we will get overflow. See bug #1245477. + typedef CheckedInt<uintptr_t> CheckedUWord; + CheckedUWord highest_requested_plus_one = + CheckedUWord(aAddr.Value()) + CheckedUWord(sizeof(uintptr_t)); + CheckedUWord highest_available_plus_one = + CheckedUWord(aStackImg->mStartAvma) + CheckedUWord(aStackImg->mLen); + if (!highest_requested_plus_one.isValid() // overflow? + || !highest_available_plus_one.isValid() // overflow? + || (highest_requested_plus_one.value() > + highest_available_plus_one.value())) { // in range? + return TaggedUWord(); + } + + return TaggedUWord( + *(uintptr_t*)(&aStackImg + ->mContents[aAddr.Value() - aStackImg->mStartAvma])); +} + +// RUNS IN NO-MALLOC CONTEXT +static TaggedUWord EvaluateReg(int16_t aReg, const UnwindRegs* aOldRegs, + TaggedUWord aCFA) { + switch (aReg) { + case DW_REG_CFA: + return aCFA; +#if defined(GP_ARCH_amd64) || defined(GP_ARCH_x86) + case DW_REG_INTEL_XBP: + return aOldRegs->xbp; + case DW_REG_INTEL_XSP: + return aOldRegs->xsp; + case DW_REG_INTEL_XIP: + return aOldRegs->xip; +#elif defined(GP_ARCH_arm) + case DW_REG_ARM_R7: + return aOldRegs->r7; + case DW_REG_ARM_R11: + return aOldRegs->r11; + case DW_REG_ARM_R12: + return aOldRegs->r12; + case DW_REG_ARM_R13: + return aOldRegs->r13; + case DW_REG_ARM_R14: + return aOldRegs->r14; + case DW_REG_ARM_R15: + return aOldRegs->r15; +#elif defined(GP_ARCH_arm64) + case DW_REG_AARCH64_X29: + return aOldRegs->x29; + case DW_REG_AARCH64_X30: + return aOldRegs->x30; + case DW_REG_AARCH64_SP: + return aOldRegs->sp; +#elif defined(GP_ARCH_mips64) + case DW_REG_MIPS_SP: + return aOldRegs->sp; + case DW_REG_MIPS_FP: + return aOldRegs->fp; + case DW_REG_MIPS_PC: + return aOldRegs->pc; +#else +# error "Unsupported arch" +#endif + default: + MOZ_ASSERT(0); + return TaggedUWord(); + } +} + +// RUNS IN NO-MALLOC CONTEXT +// See prototype for comment. +TaggedUWord EvaluatePfxExpr(int32_t start, const UnwindRegs* aOldRegs, + TaggedUWord aCFA, const StackImage* aStackImg, + const vector<PfxInstr>& aPfxInstrs) { + // A small evaluation stack, and a stack pointer, which points to + // the highest numbered in-use element. + const int N_STACK = 10; + TaggedUWord stack[N_STACK]; + int stackPointer = -1; + for (int i = 0; i < N_STACK; i++) stack[i] = TaggedUWord(); + +#define PUSH(_tuw) \ + do { \ + if (stackPointer >= N_STACK - 1) goto fail; /* overflow */ \ + stack[++stackPointer] = (_tuw); \ + } while (0) + +#define POP(_lval) \ + do { \ + if (stackPointer < 0) goto fail; /* underflow */ \ + _lval = stack[stackPointer--]; \ + } while (0) + + // Cursor in the instruction sequence. + size_t curr = start + 1; + + // Check the start point is sane. + size_t nInstrs = aPfxInstrs.size(); + if (start < 0 || (size_t)start >= nInstrs) goto fail; + + { + // The instruction sequence must start with PX_Start. If not, + // something is seriously wrong. + PfxInstr first = aPfxInstrs[start]; + if (first.mOpcode != PX_Start) goto fail; + + // Push the CFA on the stack to start with (or not), as required by + // the original DW_OP_*expression* CFI. + if (first.mOperand != 0) PUSH(aCFA); + } + + while (true) { + if (curr >= nInstrs) goto fail; // ran off the end of the sequence + + PfxInstr pfxi = aPfxInstrs[curr++]; + if (pfxi.mOpcode == PX_End) break; // we're done + + switch (pfxi.mOpcode) { + case PX_Start: + // This should appear only at the start of the sequence. + goto fail; + case PX_End: + // We just took care of that, so we shouldn't see it again. + MOZ_ASSERT(0); + goto fail; + case PX_SImm32: + PUSH(TaggedUWord((intptr_t)pfxi.mOperand)); + break; + case PX_DwReg: { + DW_REG_NUMBER reg = (DW_REG_NUMBER)pfxi.mOperand; + MOZ_ASSERT(reg != DW_REG_CFA); + PUSH(EvaluateReg(reg, aOldRegs, aCFA)); + break; + } + case PX_Deref: { + TaggedUWord addr; + POP(addr); + PUSH(DerefTUW(addr, aStackImg)); + break; + } + case PX_Add: { + TaggedUWord x, y; + POP(x); + POP(y); + PUSH(y + x); + break; + } + case PX_Sub: { + TaggedUWord x, y; + POP(x); + POP(y); + PUSH(y - x); + break; + } + case PX_And: { + TaggedUWord x, y; + POP(x); + POP(y); + PUSH(y & x); + break; + } + case PX_Or: { + TaggedUWord x, y; + POP(x); + POP(y); + PUSH(y | x); + break; + } + case PX_CmpGES: { + TaggedUWord x, y; + POP(x); + POP(y); + PUSH(y.CmpGEs(x)); + break; + } + case PX_Shl: { + TaggedUWord x, y; + POP(x); + POP(y); + PUSH(y << x); + break; + } + default: + MOZ_ASSERT(0); + goto fail; + } + } // while (true) + + // Evaluation finished. The top value on the stack is the result. + if (stackPointer >= 0) { + return stack[stackPointer]; + } + // Else fall through + +fail: + return TaggedUWord(); + +#undef PUSH +#undef POP +} + +// RUNS IN NO-MALLOC CONTEXT +TaggedUWord LExpr::EvaluateExpr(const UnwindRegs* aOldRegs, TaggedUWord aCFA, + const StackImage* aStackImg, + const vector<PfxInstr>* aPfxInstrs) const { + switch (mHow) { + case UNKNOWN: + return TaggedUWord(); + case NODEREF: { + TaggedUWord tuw = EvaluateReg(mReg, aOldRegs, aCFA); + tuw = tuw + TaggedUWord((intptr_t)mOffset); + return tuw; + } + case DEREF: { + TaggedUWord tuw = EvaluateReg(mReg, aOldRegs, aCFA); + tuw = tuw + TaggedUWord((intptr_t)mOffset); + return DerefTUW(tuw, aStackImg); + } + case PFXEXPR: { + MOZ_ASSERT(aPfxInstrs); + if (!aPfxInstrs) { + return TaggedUWord(); + } + return EvaluatePfxExpr(mOffset, aOldRegs, aCFA, aStackImg, *aPfxInstrs); + } + default: + MOZ_ASSERT(0); + return TaggedUWord(); + } +} + +// RUNS IN NO-MALLOC CONTEXT +static void UseRuleSet(/*MOD*/ UnwindRegs* aRegs, const StackImage* aStackImg, + const RuleSet* aRS, const vector<PfxInstr>* aPfxInstrs) { + // Take a copy of regs, since we'll need to refer to the old values + // whilst computing the new ones. + UnwindRegs old_regs = *aRegs; + + // Mark all the current register values as invalid, so that the + // caller can see, on our return, which ones have been computed + // anew. If we don't even manage to compute a new PC value, then + // the caller will have to abandon the unwind. + // FIXME: Create and use instead: aRegs->SetAllInvalid(); +#if defined(GP_ARCH_amd64) || defined(GP_ARCH_x86) + aRegs->xbp = TaggedUWord(); + aRegs->xsp = TaggedUWord(); + aRegs->xip = TaggedUWord(); +#elif defined(GP_ARCH_arm) + aRegs->r7 = TaggedUWord(); + aRegs->r11 = TaggedUWord(); + aRegs->r12 = TaggedUWord(); + aRegs->r13 = TaggedUWord(); + aRegs->r14 = TaggedUWord(); + aRegs->r15 = TaggedUWord(); +#elif defined(GP_ARCH_arm64) + aRegs->x29 = TaggedUWord(); + aRegs->x30 = TaggedUWord(); + aRegs->sp = TaggedUWord(); + aRegs->pc = TaggedUWord(); +#elif defined(GP_ARCH_mips64) + aRegs->sp = TaggedUWord(); + aRegs->fp = TaggedUWord(); + aRegs->pc = TaggedUWord(); +#else +# error "Unsupported arch" +#endif + + // This is generally useful. + const TaggedUWord inval = TaggedUWord(); + + // First, compute the CFA. + TaggedUWord cfa = aRS->mCfaExpr.EvaluateExpr(&old_regs, inval /*old cfa*/, + aStackImg, aPfxInstrs); + + // If we didn't manage to compute the CFA, well .. that's ungood, + // but keep going anyway. It'll be OK provided none of the register + // value rules mention the CFA. In any case, compute the new values + // for each register that we're tracking. + +#if defined(GP_ARCH_amd64) || defined(GP_ARCH_x86) + aRegs->xbp = + aRS->mXbpExpr.EvaluateExpr(&old_regs, cfa, aStackImg, aPfxInstrs); + aRegs->xsp = + aRS->mXspExpr.EvaluateExpr(&old_regs, cfa, aStackImg, aPfxInstrs); + aRegs->xip = + aRS->mXipExpr.EvaluateExpr(&old_regs, cfa, aStackImg, aPfxInstrs); +#elif defined(GP_ARCH_arm) + aRegs->r7 = aRS->mR7expr.EvaluateExpr(&old_regs, cfa, aStackImg, aPfxInstrs); + aRegs->r11 = + aRS->mR11expr.EvaluateExpr(&old_regs, cfa, aStackImg, aPfxInstrs); + aRegs->r12 = + aRS->mR12expr.EvaluateExpr(&old_regs, cfa, aStackImg, aPfxInstrs); + aRegs->r13 = + aRS->mR13expr.EvaluateExpr(&old_regs, cfa, aStackImg, aPfxInstrs); + aRegs->r14 = + aRS->mR14expr.EvaluateExpr(&old_regs, cfa, aStackImg, aPfxInstrs); + aRegs->r15 = + aRS->mR15expr.EvaluateExpr(&old_regs, cfa, aStackImg, aPfxInstrs); +#elif defined(GP_ARCH_arm64) + aRegs->x29 = + aRS->mX29expr.EvaluateExpr(&old_regs, cfa, aStackImg, aPfxInstrs); + aRegs->x30 = + aRS->mX30expr.EvaluateExpr(&old_regs, cfa, aStackImg, aPfxInstrs); + aRegs->sp = aRS->mSPexpr.EvaluateExpr(&old_regs, cfa, aStackImg, aPfxInstrs); +#elif defined(GP_ARCH_mips64) + aRegs->sp = aRS->mSPexpr.EvaluateExpr(&old_regs, cfa, aStackImg, aPfxInstrs); + aRegs->fp = aRS->mFPexpr.EvaluateExpr(&old_regs, cfa, aStackImg, aPfxInstrs); + aRegs->pc = aRS->mPCexpr.EvaluateExpr(&old_regs, cfa, aStackImg, aPfxInstrs); +#else +# error "Unsupported arch" +#endif + + // We're done. Any regs for which we didn't manage to compute a + // new value will now be marked as invalid. +} + +// RUNS IN NO-MALLOC CONTEXT +void LUL::Unwind(/*OUT*/ uintptr_t* aFramePCs, + /*OUT*/ uintptr_t* aFrameSPs, + /*OUT*/ size_t* aFramesUsed, + /*OUT*/ size_t* aFramePointerFramesAcquired, + size_t aFramesAvail, UnwindRegs* aStartRegs, + StackImage* aStackImg) { + MOZ_RELEASE_ASSERT(!mAdminMode); + + ///////////////////////////////////////////////////////// + // BEGIN UNWIND + + *aFramesUsed = 0; + + UnwindRegs regs = *aStartRegs; + TaggedUWord last_valid_sp = TaggedUWord(); + + while (true) { + if (DEBUG_MAIN) { + char buf[300]; + mLog("\n"); +#if defined(GP_ARCH_amd64) || defined(GP_ARCH_x86) + SprintfLiteral( + buf, "LoopTop: rip %d/%llx rsp %d/%llx rbp %d/%llx\n", + (int)regs.xip.Valid(), (unsigned long long int)regs.xip.Value(), + (int)regs.xsp.Valid(), (unsigned long long int)regs.xsp.Value(), + (int)regs.xbp.Valid(), (unsigned long long int)regs.xbp.Value()); + buf[sizeof(buf) - 1] = 0; + mLog(buf); +#elif defined(GP_ARCH_arm) + SprintfLiteral( + buf, + "LoopTop: r15 %d/%llx r7 %d/%llx r11 %d/%llx" + " r12 %d/%llx r13 %d/%llx r14 %d/%llx\n", + (int)regs.r15.Valid(), (unsigned long long int)regs.r15.Value(), + (int)regs.r7.Valid(), (unsigned long long int)regs.r7.Value(), + (int)regs.r11.Valid(), (unsigned long long int)regs.r11.Value(), + (int)regs.r12.Valid(), (unsigned long long int)regs.r12.Value(), + (int)regs.r13.Valid(), (unsigned long long int)regs.r13.Value(), + (int)regs.r14.Valid(), (unsigned long long int)regs.r14.Value()); + buf[sizeof(buf) - 1] = 0; + mLog(buf); +#elif defined(GP_ARCH_arm64) + SprintfLiteral( + buf, + "LoopTop: pc %d/%llx x29 %d/%llx x30 %d/%llx" + " sp %d/%llx\n", + (int)regs.pc.Valid(), (unsigned long long int)regs.pc.Value(), + (int)regs.x29.Valid(), (unsigned long long int)regs.x29.Value(), + (int)regs.x30.Valid(), (unsigned long long int)regs.x30.Value(), + (int)regs.sp.Valid(), (unsigned long long int)regs.sp.Value()); + buf[sizeof(buf) - 1] = 0; + mLog(buf); +#elif defined(GP_ARCH_mips64) + SprintfLiteral( + buf, "LoopTop: pc %d/%llx sp %d/%llx fp %d/%llx\n", + (int)regs.pc.Valid(), (unsigned long long int)regs.pc.Value(), + (int)regs.sp.Valid(), (unsigned long long int)regs.sp.Value(), + (int)regs.fp.Valid(), (unsigned long long int)regs.fp.Value()); + buf[sizeof(buf) - 1] = 0; + mLog(buf); +#else +# error "Unsupported arch" +#endif + } + +#if defined(GP_ARCH_amd64) || defined(GP_ARCH_x86) + TaggedUWord ia = regs.xip; + TaggedUWord sp = regs.xsp; +#elif defined(GP_ARCH_arm) + TaggedUWord ia = (*aFramesUsed == 0 ? regs.r15 : regs.r14); + TaggedUWord sp = regs.r13; +#elif defined(GP_ARCH_arm64) + TaggedUWord ia = (*aFramesUsed == 0 ? regs.pc : regs.x30); + TaggedUWord sp = regs.sp; +#elif defined(GP_ARCH_mips64) + TaggedUWord ia = regs.pc; + TaggedUWord sp = regs.sp; +#else +# error "Unsupported arch" +#endif + + if (*aFramesUsed >= aFramesAvail) { + break; + } + + // If we don't have a valid value for the PC, give up. + if (!ia.Valid()) { + break; + } + + // If this is the innermost frame, record the SP value, which + // presumably is valid. If this isn't the innermost frame, and we + // have a valid SP value, check that its SP value isn't less that + // the one we've seen so far, so as to catch potential SP value + // cycles. + if (*aFramesUsed == 0) { + last_valid_sp = sp; + } else { + MOZ_ASSERT(last_valid_sp.Valid()); + if (sp.Valid()) { + if (sp.Value() < last_valid_sp.Value()) { + // Hmm, SP going in the wrong direction. Let's stop. + break; + } + // Remember where we got to. + last_valid_sp = sp; + } + } + + // For the innermost frame, the IA value is what we need. For all + // other frames, it's actually the return address, so back up one + // byte so as to get it into the calling instruction. + aFramePCs[*aFramesUsed] = ia.Value() - (*aFramesUsed == 0 ? 0 : 1); + aFrameSPs[*aFramesUsed] = sp.Valid() ? sp.Value() : 0; + (*aFramesUsed)++; + + // Find the RuleSet for the current IA, if any. This will also + // query the backing (secondary) maps if it isn't found in the + // thread-local cache. + + // If this isn't the innermost frame, back up into the calling insn. + if (*aFramesUsed > 1) { + ia = ia + TaggedUWord((uintptr_t)(-1)); + } + + pair<const RuleSet*, const vector<PfxInstr>*> ruleset_and_pfxinstrs = + mPriMap->Lookup(ia.Value()); + const RuleSet* ruleset = ruleset_and_pfxinstrs.first; + const vector<PfxInstr>* pfxinstrs = ruleset_and_pfxinstrs.second; + + if (DEBUG_MAIN) { + char buf[100]; + SprintfLiteral(buf, "ruleset for 0x%llx = %p\n", + (unsigned long long int)ia.Value(), ruleset); + buf[sizeof(buf) - 1] = 0; + mLog(buf); + } + +#if defined(GP_PLAT_x86_android) || defined(GP_PLAT_x86_linux) + ///////////////////////////////////////////// + //// + // On 32 bit x86-linux, syscalls are often done via the VDSO + // function __kernel_vsyscall, which doesn't have a corresponding + // object that we can read debuginfo from. That effectively kills + // off all stack traces for threads blocked in syscalls. Hence + // special-case by looking at the code surrounding the program + // counter. + // + // 0xf7757420 <__kernel_vsyscall+0>: push %ecx + // 0xf7757421 <__kernel_vsyscall+1>: push %edx + // 0xf7757422 <__kernel_vsyscall+2>: push %ebp + // 0xf7757423 <__kernel_vsyscall+3>: mov %esp,%ebp + // 0xf7757425 <__kernel_vsyscall+5>: sysenter + // 0xf7757427 <__kernel_vsyscall+7>: nop + // 0xf7757428 <__kernel_vsyscall+8>: nop + // 0xf7757429 <__kernel_vsyscall+9>: nop + // 0xf775742a <__kernel_vsyscall+10>: nop + // 0xf775742b <__kernel_vsyscall+11>: nop + // 0xf775742c <__kernel_vsyscall+12>: nop + // 0xf775742d <__kernel_vsyscall+13>: nop + // 0xf775742e <__kernel_vsyscall+14>: int $0x80 + // 0xf7757430 <__kernel_vsyscall+16>: pop %ebp + // 0xf7757431 <__kernel_vsyscall+17>: pop %edx + // 0xf7757432 <__kernel_vsyscall+18>: pop %ecx + // 0xf7757433 <__kernel_vsyscall+19>: ret + // + // In cases where the sampled thread is blocked in a syscall, its + // program counter will point at "pop %ebp". Hence we look for + // the sequence "int $0x80; pop %ebp; pop %edx; pop %ecx; ret", and + // the corresponding register-recovery actions are: + // new_ebp = *(old_esp + 0) + // new eip = *(old_esp + 12) + // new_esp = old_esp + 16 + // + // It may also be the case that the program counter points two + // nops before the "int $0x80", viz, is __kernel_vsyscall+12, in + // the case where the syscall has been restarted but the thread + // hasn't been rescheduled. The code below doesn't handle that; + // it could easily be made to. + // + if (!ruleset && *aFramesUsed == 1 && ia.Valid() && sp.Valid()) { + uintptr_t insns_min, insns_max; + uintptr_t eip = ia.Value(); + bool b = mSegArray->getBoundingCodeSegment(&insns_min, &insns_max, eip); + if (b && eip - 2 >= insns_min && eip + 3 <= insns_max) { + uint8_t* eipC = (uint8_t*)eip; + if (eipC[-2] == 0xCD && eipC[-1] == 0x80 && eipC[0] == 0x5D && + eipC[1] == 0x5A && eipC[2] == 0x59 && eipC[3] == 0xC3) { + TaggedUWord sp_plus_0 = sp; + TaggedUWord sp_plus_12 = sp; + TaggedUWord sp_plus_16 = sp; + sp_plus_12 = sp_plus_12 + TaggedUWord(12); + sp_plus_16 = sp_plus_16 + TaggedUWord(16); + TaggedUWord new_ebp = DerefTUW(sp_plus_0, aStackImg); + TaggedUWord new_eip = DerefTUW(sp_plus_12, aStackImg); + TaggedUWord new_esp = sp_plus_16; + if (new_ebp.Valid() && new_eip.Valid() && new_esp.Valid()) { + regs.xbp = new_ebp; + regs.xip = new_eip; + regs.xsp = new_esp; + continue; + } + } + } + } + //// + ///////////////////////////////////////////// +#endif // defined(GP_PLAT_x86_android) || defined(GP_PLAT_x86_linux) + + // So, do we have a ruleset for this address? If so, use it now. + if (ruleset) { + if (DEBUG_MAIN) { + ruleset->Print(mLog); + mLog("\n"); + } + // Use the RuleSet to compute the registers for the previous + // frame. |regs| is modified in-place. + UseRuleSet(®s, aStackImg, ruleset, pfxinstrs); + continue; + } + +#if defined(GP_PLAT_amd64_linux) || defined(GP_PLAT_x86_linux) || \ + defined(GP_PLAT_amd64_android) || defined(GP_PLAT_x86_android) || \ + defined(GP_PLAT_amd64_freebsd) + // There's no RuleSet for the specified address. On amd64/x86_linux, see if + // it's possible to recover the caller's frame by using the frame pointer. + + // We seek to compute (new_IP, new_SP, new_BP) from (old_BP, stack image), + // and assume the following layout: + // + // <--- new_SP + // +----------+ + // | new_IP | (return address) + // +----------+ + // | new_BP | <--- old_BP + // +----------+ + // | .... | + // | .... | + // | .... | + // +----------+ <---- old_SP (arbitrary, but must be <= old_BP) + + const size_t wordSzB = sizeof(uintptr_t); + TaggedUWord old_xsp = regs.xsp; + + // points at new_BP ? + TaggedUWord old_xbp = regs.xbp; + // points at new_IP ? + TaggedUWord old_xbp_plus1 = regs.xbp + TaggedUWord(1 * wordSzB); + // is the new_SP ? + TaggedUWord old_xbp_plus2 = regs.xbp + TaggedUWord(2 * wordSzB); + + if (old_xbp.Valid() && old_xbp.IsAligned() && old_xsp.Valid() && + old_xsp.IsAligned() && old_xsp.Value() <= old_xbp.Value()) { + // We don't need to do any range, alignment or validity checks for + // addresses passed to DerefTUW, since that performs them itself, and + // returns an invalid value on failure. Any such value will poison + // subsequent uses, and we do a final check for validity before putting + // the computed values into |regs|. + TaggedUWord new_xbp = DerefTUW(old_xbp, aStackImg); + if (new_xbp.Valid() && new_xbp.IsAligned() && + old_xbp.Value() < new_xbp.Value()) { + TaggedUWord new_xip = DerefTUW(old_xbp_plus1, aStackImg); + TaggedUWord new_xsp = old_xbp_plus2; + if (new_xbp.Valid() && new_xip.Valid() && new_xsp.Valid()) { + regs.xbp = new_xbp; + regs.xip = new_xip; + regs.xsp = new_xsp; + (*aFramePointerFramesAcquired)++; + continue; + } + } + } +#elif defined(GP_ARCH_arm64) + // Here is an example of generated code for prologue and epilogue.. + // + // stp x29, x30, [sp, #-16]! + // mov x29, sp + // ... + // ldp x29, x30, [sp], #16 + // ret + // + // Next is another example of generated code. + // + // stp x20, x19, [sp, #-32]! + // stp x29, x30, [sp, #16] + // add x29, sp, #0x10 + // ... + // ldp x29, x30, [sp, #16] + // ldp x20, x19, [sp], #32 + // ret + // + // Previous x29 and x30 register are stored in the address of x29 register. + // But since sp register value depends on local variables, we cannot compute + // previous sp register from current sp/fp/lr register and there is no + // regular rule for sp register in prologue. But since return address is lr + // register, if x29 is valid, we will get return address without sp + // register. + // + // So we assume the following layout that if no rule set. x29 is frame + // pointer, so we will be able to compute x29 and x30 . + // + // +----------+ <--- new_sp (cannot compute) + // | .... | + // +----------+ + // | new_lr | (return address) + // +----------+ + // | new_fp | <--- old_fp + // +----------+ + // | .... | + // | .... | + // +----------+ <---- old_sp (arbitrary, but unused) + + TaggedUWord old_fp = regs.x29; + if (old_fp.Valid() && old_fp.IsAligned() && last_valid_sp.Valid() && + last_valid_sp.Value() <= old_fp.Value()) { + TaggedUWord new_fp = DerefTUW(old_fp, aStackImg); + if (new_fp.Valid() && new_fp.IsAligned() && + old_fp.Value() < new_fp.Value()) { + TaggedUWord old_fp_plus1 = old_fp + TaggedUWord(8); + TaggedUWord new_lr = DerefTUW(old_fp_plus1, aStackImg); + if (new_lr.Valid()) { + regs.x29 = new_fp; + regs.x30 = new_lr; + // When using frame pointer to walk stack, we cannot compute sp + // register since we cannot compute sp register from fp/lr/sp + // register, and there is no regular rule to compute previous sp + // register. So mark as invalid. + regs.sp = TaggedUWord(); + (*aFramePointerFramesAcquired)++; + continue; + } + } + } +#endif // defined(GP_PLAT_amd64_linux) || defined(GP_PLAT_x86_linux) || + // defined(GP_PLAT_amd64_android) || defined(GP_PLAT_x86_android) + + // We failed to recover a frame either using CFI or FP chasing, and we + // have no other ways to recover the frame. So we have to give up. + break; + + } // top level unwind loop + + // END UNWIND + ///////////////////////////////////////////////////////// +} + +//////////////////////////////////////////////////////////////// +// LUL Unit Testing // +//////////////////////////////////////////////////////////////// + +static const int LUL_UNIT_TEST_STACK_SIZE = 32768; + +#if defined(GP_ARCH_mips64) +static __attribute__((noinline)) unsigned long __getpc(void) { + unsigned long rtaddr; + __asm__ volatile("move %0, $31" : "=r"(rtaddr)); + return rtaddr; +} +#endif + +// This function is innermost in the test call sequence. It uses LUL +// to unwind, and compares the result with the sequence specified in +// the director string. These need to agree in order for the test to +// pass. In order not to screw up the results, this function needs +// to have a not-very big stack frame, since we're only presenting +// the innermost LUL_UNIT_TEST_STACK_SIZE bytes of stack to LUL, and +// that chunk unavoidably includes the frame for this function. +// +// This function must not be inlined into its callers. Doing so will +// cause the expected-vs-actual backtrace consistency checking to +// fail. Prints summary results to |aLUL|'s logging sink and also +// returns a boolean indicating whether or not the test failed. +static __attribute__((noinline)) bool GetAndCheckStackTrace( + LUL* aLUL, const char* dstring) { + // Get hold of the current unwind-start registers. + UnwindRegs startRegs; + memset(&startRegs, 0, sizeof(startRegs)); +#if defined(GP_ARCH_amd64) + volatile uintptr_t block[3]; + MOZ_ASSERT(sizeof(block) == 24); + __asm__ __volatile__( + "leaq 0(%%rip), %%r15" + "\n\t" + "movq %%r15, 0(%0)" + "\n\t" + "movq %%rsp, 8(%0)" + "\n\t" + "movq %%rbp, 16(%0)" + "\n" + : + : "r"(&block[0]) + : "memory", "r15"); + startRegs.xip = TaggedUWord(block[0]); + startRegs.xsp = TaggedUWord(block[1]); + startRegs.xbp = TaggedUWord(block[2]); + const uintptr_t REDZONE_SIZE = 128; + uintptr_t start = block[1] - REDZONE_SIZE; +#elif defined(GP_PLAT_x86_linux) || defined(GP_PLAT_x86_android) + volatile uintptr_t block[3]; + MOZ_ASSERT(sizeof(block) == 12); + __asm__ __volatile__( + ".byte 0xE8,0x00,0x00,0x00,0x00" /*call next insn*/ + "\n\t" + "popl %%edi" + "\n\t" + "movl %%edi, 0(%0)" + "\n\t" + "movl %%esp, 4(%0)" + "\n\t" + "movl %%ebp, 8(%0)" + "\n" + : + : "r"(&block[0]) + : "memory", "edi"); + startRegs.xip = TaggedUWord(block[0]); + startRegs.xsp = TaggedUWord(block[1]); + startRegs.xbp = TaggedUWord(block[2]); + const uintptr_t REDZONE_SIZE = 0; + uintptr_t start = block[1] - REDZONE_SIZE; +#elif defined(GP_PLAT_arm_linux) || defined(GP_PLAT_arm_android) + volatile uintptr_t block[6]; + MOZ_ASSERT(sizeof(block) == 24); + __asm__ __volatile__( + "mov r0, r15" + "\n\t" + "str r0, [%0, #0]" + "\n\t" + "str r14, [%0, #4]" + "\n\t" + "str r13, [%0, #8]" + "\n\t" + "str r12, [%0, #12]" + "\n\t" + "str r11, [%0, #16]" + "\n\t" + "str r7, [%0, #20]" + "\n" + : + : "r"(&block[0]) + : "memory", "r0"); + startRegs.r15 = TaggedUWord(block[0]); + startRegs.r14 = TaggedUWord(block[1]); + startRegs.r13 = TaggedUWord(block[2]); + startRegs.r12 = TaggedUWord(block[3]); + startRegs.r11 = TaggedUWord(block[4]); + startRegs.r7 = TaggedUWord(block[5]); + const uintptr_t REDZONE_SIZE = 0; + uintptr_t start = block[1] - REDZONE_SIZE; +#elif defined(GP_ARCH_arm64) + volatile uintptr_t block[4]; + MOZ_ASSERT(sizeof(block) == 32); + __asm__ __volatile__( + "adr x0, . \n\t" + "str x0, [%0, #0] \n\t" + "str x29, [%0, #8] \n\t" + "str x30, [%0, #16] \n\t" + "mov x0, sp \n\t" + "str x0, [%0, #24] \n\t" + : + : "r"(&block[0]) + : "memory", "x0"); + startRegs.pc = TaggedUWord(block[0]); + startRegs.x29 = TaggedUWord(block[1]); + startRegs.x30 = TaggedUWord(block[2]); + startRegs.sp = TaggedUWord(block[3]); + const uintptr_t REDZONE_SIZE = 0; + uintptr_t start = block[1] - REDZONE_SIZE; +#elif defined(GP_ARCH_mips64) + volatile uintptr_t block[3]; + MOZ_ASSERT(sizeof(block) == 24); + __asm__ __volatile__( + "sd $29, 8(%0) \n" + "sd $30, 16(%0) \n" + : + : "r"(block) + : "memory"); + block[0] = __getpc(); + startRegs.pc = TaggedUWord(block[0]); + startRegs.sp = TaggedUWord(block[1]); + startRegs.fp = TaggedUWord(block[2]); + const uintptr_t REDZONE_SIZE = 0; + uintptr_t start = block[1] - REDZONE_SIZE; +#else +# error "Unsupported platform" +#endif + + // Get hold of the innermost LUL_UNIT_TEST_STACK_SIZE bytes of the + // stack. + uintptr_t end = start + LUL_UNIT_TEST_STACK_SIZE; + uintptr_t ws = sizeof(void*); + start &= ~(ws - 1); + end &= ~(ws - 1); + uintptr_t nToCopy = end - start; + if (nToCopy > lul::N_STACK_BYTES) { + nToCopy = lul::N_STACK_BYTES; + } + MOZ_ASSERT(nToCopy <= lul::N_STACK_BYTES); + StackImage* stackImg = new StackImage(); + stackImg->mLen = nToCopy; + stackImg->mStartAvma = start; + if (nToCopy > 0) { + MOZ_MAKE_MEM_DEFINED((void*)start, nToCopy); + memcpy(&stackImg->mContents[0], (void*)start, nToCopy); + } + + // Unwind it. + const int MAX_TEST_FRAMES = 64; + uintptr_t framePCs[MAX_TEST_FRAMES]; + uintptr_t frameSPs[MAX_TEST_FRAMES]; + size_t framesAvail = mozilla::ArrayLength(framePCs); + size_t framesUsed = 0; + size_t framePointerFramesAcquired = 0; + aLUL->Unwind(&framePCs[0], &frameSPs[0], &framesUsed, + &framePointerFramesAcquired, framesAvail, &startRegs, stackImg); + + delete stackImg; + + // if (0) { + // // Show what we have. + // fprintf(stderr, "Got %d frames:\n", (int)framesUsed); + // for (size_t i = 0; i < framesUsed; i++) { + // fprintf(stderr, " [%2d] SP %p PC %p\n", + // (int)i, (void*)frameSPs[i], (void*)framePCs[i]); + // } + // fprintf(stderr, "\n"); + //} + + // Check to see if there's a consistent binding between digits in + // the director string ('1' .. '8') and the PC values acquired by + // the unwind. If there isn't, the unwinding has failed somehow. + uintptr_t binding[8]; // binding for '1' .. binding for '8' + memset((void*)binding, 0, sizeof(binding)); + + // The general plan is to work backwards along the director string + // and forwards along the framePCs array. Doing so corresponds to + // working outwards from the innermost frame of the recursive test set. + const char* cursor = dstring; + + // Find the end. This leaves |cursor| two bytes past the first + // character we want to look at -- see comment below. + while (*cursor) cursor++; + + // Counts the number of consistent frames. + size_t nConsistent = 0; + + // Iterate back to the start of the director string. The starting + // points are a bit complex. We can't use framePCs[0] because that + // contains the PC in this frame (above). We can't use framePCs[1] + // because that will contain the PC at return point in the recursive + // test group (TestFn[1-8]) for their call "out" to this function, + // GetAndCheckStackTrace. Although LUL will compute a correct + // return address, that will not be the same return address as for a + // recursive call out of the the function to another function in the + // group. Hence we can only start consistency checking at + // framePCs[2]. + // + // To be consistent, then, we must ignore the last element in the + // director string as that corresponds to framePCs[1]. Hence the + // start points are: framePCs[2] and the director string 2 bytes + // before the terminating zero. + // + // Also as a result of this, the number of consistent frames counted + // will always be one less than the length of the director string + // (not including its terminating zero). + size_t frameIx; + for (cursor = cursor - 2, frameIx = 2; + cursor >= dstring && frameIx < framesUsed; cursor--, frameIx++) { + char c = *cursor; + uintptr_t pc = framePCs[frameIx]; + // If this doesn't hold, the director string is ill-formed. + MOZ_ASSERT(c >= '1' && c <= '8'); + int n = ((int)c) - ((int)'1'); + if (binding[n] == 0) { + // There's no binding for |c| yet, so install |pc| and carry on. + binding[n] = pc; + nConsistent++; + continue; + } + // There's a pre-existing binding for |c|. Check it's consistent. + if (binding[n] != pc) { + // Not consistent. Give up now. + break; + } + // Consistent. Keep going. + nConsistent++; + } + + // So, did we succeed? + bool passed = nConsistent + 1 == strlen(dstring); + + // Show the results. + char buf[200]; + SprintfLiteral(buf, "LULUnitTest: dstring = %s\n", dstring); + buf[sizeof(buf) - 1] = 0; + aLUL->mLog(buf); + SprintfLiteral(buf, "LULUnitTest: %d consistent, %d in dstring: %s\n", + (int)nConsistent, (int)strlen(dstring), + passed ? "PASS" : "FAIL"); + buf[sizeof(buf) - 1] = 0; + aLUL->mLog(buf); + + return !passed; +} + +// Macro magic to create a set of 8 mutually recursive functions with +// varying frame sizes. These will recurse amongst themselves as +// specified by |strP|, the directory string, and call +// GetAndCheckStackTrace when the string becomes empty, passing it the +// original value of the string. This checks the result, printing +// results on |aLUL|'s logging sink, and also returns a boolean +// indicating whether or not the results are acceptable (correct). + +#define DECL_TEST_FN(NAME) \ + bool NAME(LUL* aLUL, const char* strPorig, const char* strP); + +#define GEN_TEST_FN(NAME, FRAMESIZE) \ + bool NAME(LUL* aLUL, const char* strPorig, const char* strP) { \ + /* Create a frame of size (at least) FRAMESIZE, so that the */ \ + /* 8 functions created by this macro offer some variation in frame */ \ + /* sizes. This isn't as simple as it might seem, since a clever */ \ + /* optimizing compiler (eg, clang-5) detects that the array is unused */ \ + /* and removes it. We try to defeat this by passing it to a function */ \ + /* in a different compilation unit, and hoping that clang does not */ \ + /* notice that the call is a no-op. */ \ + char space[FRAMESIZE]; \ + Unused << write(1, space, 0); /* write zero bytes of |space| to stdout */ \ + \ + if (*strP == '\0') { \ + /* We've come to the end of the director string. */ \ + /* Take a stack snapshot. */ \ + /* We purposefully use a negation to avoid tail-call optimization */ \ + return !GetAndCheckStackTrace(aLUL, strPorig); \ + } else { \ + /* Recurse onwards. This is a bit subtle. The obvious */ \ + /* thing to do here is call onwards directly, from within the */ \ + /* arms of the case statement. That gives a problem in that */ \ + /* there will be multiple return points inside each function when */ \ + /* unwinding, so it will be difficult to check for consistency */ \ + /* against the director string. Instead, we make an indirect */ \ + /* call, so as to guarantee that there is only one call site */ \ + /* within each function. This does assume that the compiler */ \ + /* won't transform it back to the simple direct-call form. */ \ + /* To discourage it from doing so, the call is bracketed with */ \ + /* __asm__ __volatile__ sections so as to make it not-movable. */ \ + bool (*nextFn)(LUL*, const char*, const char*) = NULL; \ + switch (*strP) { \ + case '1': \ + nextFn = TestFn1; \ + break; \ + case '2': \ + nextFn = TestFn2; \ + break; \ + case '3': \ + nextFn = TestFn3; \ + break; \ + case '4': \ + nextFn = TestFn4; \ + break; \ + case '5': \ + nextFn = TestFn5; \ + break; \ + case '6': \ + nextFn = TestFn6; \ + break; \ + case '7': \ + nextFn = TestFn7; \ + break; \ + case '8': \ + nextFn = TestFn8; \ + break; \ + default: \ + nextFn = TestFn8; \ + break; \ + } \ + /* "use" |space| immediately after the recursive call, */ \ + /* so as to dissuade clang from deallocating the space while */ \ + /* the call is active, or otherwise messing with the stack frame. */ \ + __asm__ __volatile__("" ::: "cc", "memory"); \ + bool passed = nextFn(aLUL, strPorig, strP + 1); \ + Unused << write(1, space, 0); \ + __asm__ __volatile__("" ::: "cc", "memory"); \ + return passed; \ + } \ + } + +// The test functions are mutually recursive, so it is necessary to +// declare them before defining them. +DECL_TEST_FN(TestFn1) +DECL_TEST_FN(TestFn2) +DECL_TEST_FN(TestFn3) +DECL_TEST_FN(TestFn4) +DECL_TEST_FN(TestFn5) +DECL_TEST_FN(TestFn6) +DECL_TEST_FN(TestFn7) +DECL_TEST_FN(TestFn8) + +GEN_TEST_FN(TestFn1, 123) +GEN_TEST_FN(TestFn2, 456) +GEN_TEST_FN(TestFn3, 789) +GEN_TEST_FN(TestFn4, 23) +GEN_TEST_FN(TestFn5, 47) +GEN_TEST_FN(TestFn6, 117) +GEN_TEST_FN(TestFn7, 1) +GEN_TEST_FN(TestFn8, 99) + +// This starts the test sequence going. Call here to generate a +// sequence of calls as directed by the string |dstring|. The call +// sequence will, from its innermost frame, finish by calling +// GetAndCheckStackTrace() and passing it |dstring|. +// GetAndCheckStackTrace() will unwind the stack, check consistency +// of those results against |dstring|, and print a pass/fail message +// to aLUL's logging sink. It also updates the counters in *aNTests +// and aNTestsPassed. +__attribute__((noinline)) void TestUnw(/*OUT*/ int* aNTests, + /*OUT*/ int* aNTestsPassed, LUL* aLUL, + const char* dstring) { + // Ensure that the stack has at least this much space on it. This + // makes it safe to saw off the top LUL_UNIT_TEST_STACK_SIZE bytes + // and hand it to LUL. Safe in the sense that no segfault can + // happen because the stack is at least this big. This is all + // somewhat dubious in the sense that a sufficiently clever compiler + // (clang, for one) can figure out that space[] is unused and delete + // it from the frame. Hence the somewhat elaborate hoop jumping to + // fill it up before the call and to at least appear to use the + // value afterwards. + int i; + volatile char space[LUL_UNIT_TEST_STACK_SIZE]; + for (i = 0; i < LUL_UNIT_TEST_STACK_SIZE; i++) { + space[i] = (char)(i & 0x7F); + } + + // Really run the test. + bool passed = TestFn1(aLUL, dstring, dstring); + + // Appear to use space[], by visiting the value to compute some kind + // of checksum, and then (apparently) using the checksum. + int sum = 0; + for (i = 0; i < LUL_UNIT_TEST_STACK_SIZE; i++) { + // If this doesn't fool LLVM, I don't know what will. + sum += space[i] - 3 * i; + } + __asm__ __volatile__("" : : "r"(sum)); + + // Update the counters. + (*aNTests)++; + if (passed) { + (*aNTestsPassed)++; + } +} + +void RunLulUnitTests(/*OUT*/ int* aNTests, /*OUT*/ int* aNTestsPassed, + LUL* aLUL) { + aLUL->mLog(":\n"); + aLUL->mLog("LULUnitTest: BEGIN\n"); + *aNTests = *aNTestsPassed = 0; + TestUnw(aNTests, aNTestsPassed, aLUL, "11111111"); + TestUnw(aNTests, aNTestsPassed, aLUL, "11222211"); + TestUnw(aNTests, aNTestsPassed, aLUL, "111222333"); + TestUnw(aNTests, aNTestsPassed, aLUL, "1212121231212331212121212121212"); + TestUnw(aNTests, aNTestsPassed, aLUL, "31415827271828325332173258"); + TestUnw(aNTests, aNTestsPassed, aLUL, + "123456781122334455667788777777777777777777777"); + aLUL->mLog("LULUnitTest: END\n"); + aLUL->mLog(":\n"); +} + +} // namespace lul diff --git a/mozglue/baseprofiler/lul/LulMain.h b/mozglue/baseprofiler/lul/LulMain.h new file mode 100644 index 0000000000..987a878c90 --- /dev/null +++ b/mozglue/baseprofiler/lul/LulMain.h @@ -0,0 +1,378 @@ +/* -*- 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/. */ + +#ifndef LulMain_h +#define LulMain_h + +#include "PlatformMacros.h" +#include "mozilla/Atomics.h" +#include "mozilla/MemoryReporting.h" +#include "mozilla/BaseProfilerUtils.h" + +// LUL: A Lightweight Unwind Library. +// This file provides the end-user (external) interface for LUL. + +// Some comments about naming in the implementation. These are safe +// to ignore if you are merely using LUL, but are important if you +// hack on its internals. +// +// Debuginfo readers in general have tended to use the word "address" +// to mean several different things. This sometimes makes them +// difficult to understand and maintain. LUL tries hard to avoid +// using the word "address" and instead uses the following more +// precise terms: +// +// * SVMA ("Stated Virtual Memory Address"): this is an address of a +// symbol (etc) as it is stated in the symbol table, or other +// metadata, of an object. Such values are typically small and +// start from zero or thereabouts, unless the object has been +// prelinked. +// +// * AVMA ("Actual Virtual Memory Address"): this is the address of a +// symbol (etc) in a running process, that is, once the associated +// object has been mapped into a process. Such values are typically +// much larger than SVMAs, since objects can get mapped arbitrarily +// far along the address space. +// +// * "Bias": the difference between AVMA and SVMA for a given symbol +// (specifically, AVMA - SVMA). The bias is always an integral +// number of pages. Once we know the bias for a given object's +// text section (for example), we can compute the AVMAs of all of +// its text symbols by adding the bias to their SVMAs. +// +// * "Image address": typically, to read debuginfo from an object we +// will temporarily mmap in the file so as to read symbol tables +// etc. Addresses in this temporary mapping are called "Image +// addresses". Note that the temporary mapping is entirely +// unrelated to the mappings of the file that the dynamic linker +// must perform merely in order to get the program to run. Hence +// image addresses are unrelated to either SVMAs or AVMAs. + +namespace lul { + +// A machine word plus validity tag. +class TaggedUWord { + public: + // RUNS IN NO-MALLOC CONTEXT + // Construct a valid one. + explicit TaggedUWord(uintptr_t w) : mValue(w), mValid(true) {} + + // RUNS IN NO-MALLOC CONTEXT + // Construct an invalid one. + TaggedUWord() : mValue(0), mValid(false) {} + + // RUNS IN NO-MALLOC CONTEXT + TaggedUWord operator+(TaggedUWord rhs) const { + return (Valid() && rhs.Valid()) ? TaggedUWord(Value() + rhs.Value()) + : TaggedUWord(); + } + + // RUNS IN NO-MALLOC CONTEXT + TaggedUWord operator-(TaggedUWord rhs) const { + return (Valid() && rhs.Valid()) ? TaggedUWord(Value() - rhs.Value()) + : TaggedUWord(); + } + + // RUNS IN NO-MALLOC CONTEXT + TaggedUWord operator&(TaggedUWord rhs) const { + return (Valid() && rhs.Valid()) ? TaggedUWord(Value() & rhs.Value()) + : TaggedUWord(); + } + + // RUNS IN NO-MALLOC CONTEXT + TaggedUWord operator|(TaggedUWord rhs) const { + return (Valid() && rhs.Valid()) ? TaggedUWord(Value() | rhs.Value()) + : TaggedUWord(); + } + + // RUNS IN NO-MALLOC CONTEXT + TaggedUWord CmpGEs(TaggedUWord rhs) const { + if (Valid() && rhs.Valid()) { + intptr_t s1 = (intptr_t)Value(); + intptr_t s2 = (intptr_t)rhs.Value(); + return TaggedUWord(s1 >= s2 ? 1 : 0); + } + return TaggedUWord(); + } + + // RUNS IN NO-MALLOC CONTEXT + TaggedUWord operator<<(TaggedUWord rhs) const { + if (Valid() && rhs.Valid()) { + uintptr_t shift = rhs.Value(); + if (shift < 8 * sizeof(uintptr_t)) return TaggedUWord(Value() << shift); + } + return TaggedUWord(); + } + + // RUNS IN NO-MALLOC CONTEXT + // Is equal? Note: non-validity on either side gives non-equality. + bool operator==(TaggedUWord other) const { + return (mValid && other.Valid()) ? (mValue == other.Value()) : false; + } + + // RUNS IN NO-MALLOC CONTEXT + // Is it word-aligned? + bool IsAligned() const { + return mValid && (mValue & (sizeof(uintptr_t) - 1)) == 0; + } + + // RUNS IN NO-MALLOC CONTEXT + uintptr_t Value() const { return mValue; } + + // RUNS IN NO-MALLOC CONTEXT + bool Valid() const { return mValid; } + + private: + uintptr_t mValue; + bool mValid; +}; + +// The registers, with validity tags, that will be unwound. + +struct UnwindRegs { +#if defined(GP_ARCH_arm) + TaggedUWord r7; + TaggedUWord r11; + TaggedUWord r12; + TaggedUWord r13; + TaggedUWord r14; + TaggedUWord r15; +#elif defined(GP_ARCH_arm64) + TaggedUWord x29; + TaggedUWord x30; + TaggedUWord sp; + TaggedUWord pc; +#elif defined(GP_ARCH_amd64) || defined(GP_ARCH_x86) + TaggedUWord xbp; + TaggedUWord xsp; + TaggedUWord xip; +#elif defined(GP_ARCH_mips64) + TaggedUWord sp; + TaggedUWord fp; + TaggedUWord pc; +#else +# error "Unknown plat" +#endif +}; + +// The maximum number of bytes in a stack snapshot. This value can be increased +// if necessary, but testing showed that 160k is enough to obtain good +// backtraces on x86_64 Linux. Most backtraces fit comfortably into 4-8k of +// stack space, but we do have some very deep stacks occasionally. Please see +// the comments in DoNativeBacktrace as to why it's OK to have this value be so +// large. +static const size_t N_STACK_BYTES = 160 * 1024; + +// The stack chunk image that will be unwound. +struct StackImage { + // [start_avma, +len) specify the address range in the buffer. + // Obviously we require 0 <= len <= N_STACK_BYTES. + uintptr_t mStartAvma; + size_t mLen; + uint8_t mContents[N_STACK_BYTES]; +}; + +// Statistics collection for the unwinder. +template <typename T> +class LULStats { + public: + LULStats() : mContext(0), mCFI(0), mFP(0) {} + + template <typename S> + explicit LULStats(const LULStats<S>& aOther) + : mContext(aOther.mContext), mCFI(aOther.mCFI), mFP(aOther.mFP) {} + + template <typename S> + LULStats<T>& operator=(const LULStats<S>& aOther) { + mContext = aOther.mContext; + mCFI = aOther.mCFI; + mFP = aOther.mFP; + return *this; + } + + template <typename S> + uint32_t operator-(const LULStats<S>& aOther) { + return (mContext - aOther.mContext) + (mCFI - aOther.mCFI) + + (mFP - aOther.mFP); + } + + T mContext; // Number of context frames + T mCFI; // Number of CFI/EXIDX frames + T mFP; // Number of frame-pointer recovered frames +}; + +// The core unwinder library class. Just one of these is needed, and +// it can be shared by multiple unwinder threads. +// +// The library operates in one of two modes. +// +// * Admin mode. The library is this state after creation. In Admin +// mode, no unwinding may be performed. It is however allowable to +// perform administrative tasks -- primarily, loading of unwind info +// -- in this mode. In particular, it is safe for the library to +// perform dynamic memory allocation in this mode. Safe in the +// sense that there is no risk of deadlock against unwinding threads +// that might -- because of where they have been sampled -- hold the +// system's malloc lock. +// +// * Unwind mode. In this mode, calls to ::Unwind may be made, but +// nothing else. ::Unwind guarantees not to make any dynamic memory +// requests, so as to guarantee that the calling thread won't +// deadlock in the case where it already holds the system's malloc lock. +// +// The library is created in Admin mode. After debuginfo is loaded, +// the caller must switch it into Unwind mode by calling +// ::EnableUnwinding. There is no way to switch it back to Admin mode +// after that. To safely switch back to Admin mode would require the +// caller (or other external agent) to guarantee that there are no +// pending ::Unwind calls. + +class PriMap; +class SegArray; +class UniqueStringUniverse; + +class LUL { + public: + // Create; supply a logging sink. Sets the object in Admin mode. + explicit LUL(void (*aLog)(const char*)); + + // Destroy. Caller is responsible for ensuring that no other + // threads are in Unwind calls. All resources are freed and all + // registered unwinder threads are deregistered. Can be called + // either in Admin or Unwind mode. + ~LUL(); + + // Notify the library that unwinding is now allowed and so + // admin-mode calls are no longer allowed. The object is initially + // created in admin mode. The only possible transition is + // admin->unwinding, therefore. + void EnableUnwinding(); + + // Notify of a new r-x mapping, and load the associated unwind info. + // The filename is strdup'd and used for debug printing. If + // aMappedImage is NULL, this function will mmap/munmap the file + // itself, so as to be able to read the unwind info. If + // aMappedImage is non-NULL then it is assumed to point to a + // called-supplied and caller-managed mapped image of the file. + // May only be called in Admin mode. + void NotifyAfterMap(uintptr_t aRXavma, size_t aSize, const char* aFileName, + const void* aMappedImage); + + // In rare cases we know an executable area exists but don't know + // what the associated file is. This call notifies LUL of such + // areas. This is important for correct functioning of stack + // scanning and of the x86-{linux,android} special-case + // __kernel_syscall function handling. + // This must be called only after the code area in + // question really has been mapped. + // May only be called in Admin mode. + void NotifyExecutableArea(uintptr_t aRXavma, size_t aSize); + + // Notify that a mapped area has been unmapped; discard any + // associated unwind info. Acquires mRWlock for writing. Note that + // to avoid segfaulting the stack-scan unwinder, which inspects code + // areas, this must be called before the code area in question is + // really unmapped. Note that, unlike NotifyAfterMap(), this + // function takes the start and end addresses of the range to be + // unmapped, rather than a start and a length parameter. This is so + // as to make it possible to notify an unmap for the entire address + // space using a single call. + // May only be called in Admin mode. + void NotifyBeforeUnmap(uintptr_t aAvmaMin, uintptr_t aAvmaMax); + + // Apply NotifyBeforeUnmap to the entire address space. This causes + // LUL to discard all unwind and executable-area information for the + // entire address space. + // May only be called in Admin mode. + void NotifyBeforeUnmapAll() { NotifyBeforeUnmap(0, UINTPTR_MAX); } + + // Returns the number of mappings currently registered. + // May only be called in Admin mode. + size_t CountMappings(); + + // Unwind |aStackImg| starting with the context in |aStartRegs|. + // Write the number of frames recovered in *aFramesUsed. Put + // the PC values in aFramePCs[0 .. *aFramesUsed-1] and + // the SP values in aFrameSPs[0 .. *aFramesUsed-1]. + // |aFramesAvail| is the size of the two output arrays and hence the + // largest possible value of *aFramesUsed. PC values are always + // valid, and the unwind will stop when the PC becomes invalid, but + // the SP values might be invalid, in which case the value zero will + // be written in the relevant frameSPs[] slot. + // + // This function assumes that the SP values increase as it unwinds + // away from the innermost frame -- that is, that the stack grows + // down. It monitors SP values as it unwinds to check they + // decrease, so as to avoid looping on corrupted stacks. + // + // May only be called in Unwind mode. Multiple threads may unwind + // at once. LUL user is responsible for ensuring that no thread makes + // any Admin calls whilst in Unwind mode. + // MOZ_CRASHes if the calling thread is not registered for unwinding. + // + // The calling thread must previously have been registered via a call to + // RegisterSampledThread. + void Unwind(/*OUT*/ uintptr_t* aFramePCs, + /*OUT*/ uintptr_t* aFrameSPs, + /*OUT*/ size_t* aFramesUsed, + /*OUT*/ size_t* aFramePointerFramesAcquired, size_t aFramesAvail, + UnwindRegs* aStartRegs, StackImage* aStackImg); + + // The logging sink. Call to send debug strings to the caller- + // specified destination. Can only be called by the Admin thread. + void (*mLog)(const char*); + + // Statistics relating to unwinding. These have to be atomic since + // unwinding can occur on different threads simultaneously. + LULStats<mozilla::Atomic<uint32_t>> mStats; + + // Possibly show the statistics. This may not be called from any + // registered sampling thread, since it involves I/O. + void MaybeShowStats(); + + size_t SizeOfIncludingThis(mozilla::MallocSizeOf) const; + + private: + // The statistics counters at the point where they were last printed. + LULStats<uint32_t> mStatsPrevious; + + // Are we in admin mode? Initially |true| but changes to |false| + // once unwinding begins. + bool mAdminMode; + + // The thread ID associated with admin mode. This is the only thread + // that is allowed do perform non-Unwind calls on this object. Conversely, + // no registered Unwinding thread may be the admin thread. This is so + // as to clearly partition the one thread that may do dynamic memory + // allocation from the threads that are being sampled, since the latter + // absolutely may not do dynamic memory allocation. + mozilla::baseprofiler::BaseProfilerThreadId mAdminThreadId; + + // The top level mapping from code address ranges to postprocessed + // unwind info. Basically a sorted array of (addr, len, info) + // records. This field is updated by NotifyAfterMap and NotifyBeforeUnmap. + PriMap* mPriMap; + + // An auxiliary structure that records which address ranges are + // mapped r-x, for the benefit of the stack scanner. + SegArray* mSegArray; + + // A UniqueStringUniverse that holds all the strdup'd strings created + // whilst reading unwind information. This is included so as to make + // it possible to free them in ~LUL. + UniqueStringUniverse* mUSU; +}; + +// Run unit tests on an initialised, loaded-up LUL instance, and print +// summary results on |aLUL|'s logging sink. Also return the number +// of tests run in *aNTests and the number that passed in +// *aNTestsPassed. +void RunLulUnitTests(/*OUT*/ int* aNTests, /*OUT*/ int* aNTestsPassed, + LUL* aLUL); + +} // namespace lul + +#endif // LulMain_h diff --git a/mozglue/baseprofiler/lul/LulMainInt.h b/mozglue/baseprofiler/lul/LulMainInt.h new file mode 100644 index 0000000000..c2ee45d73d --- /dev/null +++ b/mozglue/baseprofiler/lul/LulMainInt.h @@ -0,0 +1,420 @@ +/* -*- 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/. */ + +#ifndef LulMainInt_h +#define LulMainInt_h + +#include "PlatformMacros.h" +#include "LulMain.h" // for TaggedUWord + +#include "mozilla/Assertions.h" + +#include <string> +#include <vector> + +// This file is provides internal interface inside LUL. If you are an +// end-user of LUL, do not include it in your code. The end-user +// interface is in LulMain.h. + +namespace lul { + +using std::vector; + +//////////////////////////////////////////////////////////////// +// DW_REG_ constants // +//////////////////////////////////////////////////////////////// + +// These are the Dwarf CFI register numbers, as (presumably) defined +// in the ELF ABI supplements for each architecture. + +enum DW_REG_NUMBER { + // No real register has this number. It's convenient to be able to + // treat the CFA (Canonical Frame Address) as "just another + // register", though. + DW_REG_CFA = -1, +#if defined(GP_ARCH_arm) + // ARM registers + DW_REG_ARM_R7 = 7, + DW_REG_ARM_R11 = 11, + DW_REG_ARM_R12 = 12, + DW_REG_ARM_R13 = 13, + DW_REG_ARM_R14 = 14, + DW_REG_ARM_R15 = 15, +#elif defined(GP_ARCH_arm64) + // aarch64 registers + DW_REG_AARCH64_X29 = 29, + DW_REG_AARCH64_X30 = 30, + DW_REG_AARCH64_SP = 31, +#elif defined(GP_ARCH_amd64) + // Because the X86 (32 bit) and AMD64 (64 bit) summarisers are + // combined, a merged set of register constants is needed. + DW_REG_INTEL_XBP = 6, + DW_REG_INTEL_XSP = 7, + DW_REG_INTEL_XIP = 16, +#elif defined(GP_ARCH_x86) + DW_REG_INTEL_XBP = 5, + DW_REG_INTEL_XSP = 4, + DW_REG_INTEL_XIP = 8, +#elif defined(GP_ARCH_mips64) + DW_REG_MIPS_SP = 29, + DW_REG_MIPS_FP = 30, + DW_REG_MIPS_PC = 34, +#else +# error "Unknown arch" +#endif +}; + +//////////////////////////////////////////////////////////////// +// PfxExpr // +//////////////////////////////////////////////////////////////// + +enum PfxExprOp { + // meaning of mOperand effect on stack + PX_Start, // bool start-with-CFA? start, with CFA on stack, or not + PX_End, // none stop; result is at top of stack + PX_SImm32, // int32 push signed int32 + PX_DwReg, // DW_REG_NUMBER push value of the specified reg + PX_Deref, // none pop X ; push *X + PX_Add, // none pop X ; pop Y ; push Y + X + PX_Sub, // none pop X ; pop Y ; push Y - X + PX_And, // none pop X ; pop Y ; push Y & X + PX_Or, // none pop X ; pop Y ; push Y | X + PX_CmpGES, // none pop X ; pop Y ; push (Y >=s X) ? 1 : 0 + PX_Shl // none pop X ; pop Y ; push Y << X +}; + +struct PfxInstr { + PfxInstr(PfxExprOp opcode, int32_t operand) + : mOpcode(opcode), mOperand(operand) {} + explicit PfxInstr(PfxExprOp opcode) : mOpcode(opcode), mOperand(0) {} + bool operator==(const PfxInstr& other) const { + return mOpcode == other.mOpcode && mOperand == other.mOperand; + } + PfxExprOp mOpcode; + int32_t mOperand; +}; + +static_assert(sizeof(PfxInstr) <= 8, "PfxInstr size changed unexpectedly"); + +// Evaluate the prefix expression whose PfxInstrs start at aPfxInstrs[start]. +// In the case of any mishap (stack over/underflow, running off the end of +// the instruction vector, obviously malformed sequences), +// return an invalid TaggedUWord. +// RUNS IN NO-MALLOC CONTEXT +TaggedUWord EvaluatePfxExpr(int32_t start, const UnwindRegs* aOldRegs, + TaggedUWord aCFA, const StackImage* aStackImg, + const vector<PfxInstr>& aPfxInstrs); + +//////////////////////////////////////////////////////////////// +// LExpr // +//////////////////////////////////////////////////////////////// + +// An expression -- very primitive. Denotes either "register + +// offset", a dereferenced version of the same, or a reference to a +// prefix expression stored elsewhere. So as to allow convenient +// handling of Dwarf-derived unwind info, the register may also denote +// the CFA. A large number of these need to be stored, so we ensure +// it fits into 8 bytes. See comment below on RuleSet to see how +// expressions fit into the bigger picture. + +enum LExprHow { + UNKNOWN = 0, // This LExpr denotes no value. + NODEREF, // Value is (mReg + mOffset). + DEREF, // Value is *(mReg + mOffset). + PFXEXPR // Value is EvaluatePfxExpr(secMap->mPfxInstrs[mOffset]) +}; + +inline static const char* NameOf_LExprHow(LExprHow how) { + switch (how) { + case UNKNOWN: + return "UNKNOWN"; + case NODEREF: + return "NODEREF"; + case DEREF: + return "DEREF"; + case PFXEXPR: + return "PFXEXPR"; + default: + return "LExpr-??"; + } +} + +struct LExpr { + // Denotes an expression with no value. + LExpr() : mHow(UNKNOWN), mReg(0), mOffset(0) {} + + // Denotes any expressible expression. + LExpr(LExprHow how, int16_t reg, int32_t offset) + : mHow(how), mReg(reg), mOffset(offset) { + switch (how) { + case UNKNOWN: + MOZ_ASSERT(reg == 0 && offset == 0); + break; + case NODEREF: + break; + case DEREF: + break; + case PFXEXPR: + MOZ_ASSERT(reg == 0 && offset >= 0); + break; + default: + MOZ_ASSERT(0, "LExpr::LExpr: invalid how"); + } + } + + // Change the offset for an expression that references memory. + LExpr add_delta(long delta) { + MOZ_ASSERT(mHow == NODEREF); + // If this is a non-debug build and the above assertion would have + // failed, at least return LExpr() so that the machinery that uses + // the resulting expression fails in a repeatable way. + return (mHow == NODEREF) ? LExpr(mHow, mReg, mOffset + delta) + : LExpr(); // Gone bad + } + + // Dereference an expression that denotes a memory address. + LExpr deref() { + MOZ_ASSERT(mHow == NODEREF); + // Same rationale as for add_delta(). + return (mHow == NODEREF) ? LExpr(DEREF, mReg, mOffset) + : LExpr(); // Gone bad + } + + // Print a rule for recovery of |aNewReg| whose recovered value + // is this LExpr. + std::string ShowRule(const char* aNewReg) const; + + // Evaluate this expression, producing a TaggedUWord. |aOldRegs| + // holds register values that may be referred to by the expression. + // |aCFA| holds the CFA value, if any, that applies. |aStackImg| + // contains a chuck of stack that will be consulted if the expression + // references memory. |aPfxInstrs| holds the vector of PfxInstrs + // that will be consulted if this is a PFXEXPR. + // RUNS IN NO-MALLOC CONTEXT + TaggedUWord EvaluateExpr(const UnwindRegs* aOldRegs, TaggedUWord aCFA, + const StackImage* aStackImg, + const vector<PfxInstr>* aPfxInstrs) const; + + // Representation of expressions. If |mReg| is DW_REG_CFA (-1) then + // it denotes the CFA. All other allowed values for |mReg| are + // nonnegative and are DW_REG_ values. + LExprHow mHow : 8; + int16_t mReg; // A DW_REG_ value + int32_t mOffset; // 32-bit signed offset should be more than enough. +}; + +static_assert(sizeof(LExpr) <= 8, "LExpr size changed unexpectedly"); + +//////////////////////////////////////////////////////////////// +// RuleSet // +//////////////////////////////////////////////////////////////// + +// This is platform-dependent. For some address range, describes how +// to recover the CFA and then how to recover the registers for the +// previous frame. +// +// The set of LExprs contained in a given RuleSet describe a DAG which +// says how to compute the caller's registers ("new registers") from +// the callee's registers ("old registers"). The DAG can contain a +// single internal node, which is the value of the CFA for the callee. +// It would be possible to construct a DAG that omits the CFA, but +// including it makes the summarisers simpler, and the Dwarf CFI spec +// has the CFA as a central concept. +// +// For this to make sense, |mCfaExpr| can't have +// |mReg| == DW_REG_CFA since we have no previous value for the CFA. +// All of the other |Expr| fields can -- and usually do -- specify +// |mReg| == DW_REG_CFA. +// +// With that in place, the unwind algorithm proceeds as follows. +// +// (0) Initially: we have values for the old registers, and a memory +// image. +// +// (1) Compute the CFA by evaluating |mCfaExpr|. Add the computed +// value to the set of "old registers". +// +// (2) Compute values for the registers by evaluating all of the other +// |Expr| fields in the RuleSet. These can depend on both the old +// register values and the just-computed CFA. +// +// If we are unwinding without computing a CFA, perhaps because the +// RuleSets are derived from EXIDX instead of Dwarf, then +// |mCfaExpr.mHow| will be LExpr::UNKNOWN, so the computed value will +// be invalid -- that is, TaggedUWord() -- and so any attempt to use +// that will result in the same value. But that's OK because the +// RuleSet would make no sense if depended on the CFA but specified no +// way to compute it. +// +// A RuleSet is not allowed to cover zero address range. Having zero +// length would break binary searching in SecMaps and PriMaps. + +class RuleSet { + public: + RuleSet(); + void Print(void (*aLog)(const char*)) const; + + // Find the LExpr* for a given DW_REG_ value in this class. + LExpr* ExprForRegno(DW_REG_NUMBER aRegno); + + uintptr_t mAddr; + uintptr_t mLen; + // How to compute the CFA. + LExpr mCfaExpr; + // How to compute caller register values. These may reference the + // value defined by |mCfaExpr|. +#if defined(GP_ARCH_amd64) || defined(GP_ARCH_x86) + LExpr mXipExpr; // return address + LExpr mXspExpr; + LExpr mXbpExpr; +#elif defined(GP_ARCH_arm) + LExpr mR15expr; // return address + LExpr mR14expr; + LExpr mR13expr; + LExpr mR12expr; + LExpr mR11expr; + LExpr mR7expr; +#elif defined(GP_ARCH_arm64) + LExpr mX29expr; // frame pointer register + LExpr mX30expr; // link register + LExpr mSPexpr; +#elif defined(GP_ARCH_mips64) + LExpr mPCexpr; + LExpr mFPexpr; + LExpr mSPexpr; +#else +# error "Unknown arch" +#endif +}; + +// Returns |true| for Dwarf register numbers which are members +// of the set of registers that LUL unwinds on this target. +static inline bool registerIsTracked(DW_REG_NUMBER reg) { + switch (reg) { +#if defined(GP_ARCH_amd64) || defined(GP_ARCH_x86) + case DW_REG_INTEL_XBP: + case DW_REG_INTEL_XSP: + case DW_REG_INTEL_XIP: + return true; +#elif defined(GP_ARCH_arm) + case DW_REG_ARM_R7: + case DW_REG_ARM_R11: + case DW_REG_ARM_R12: + case DW_REG_ARM_R13: + case DW_REG_ARM_R14: + case DW_REG_ARM_R15: + return true; +#elif defined(GP_ARCH_arm64) + case DW_REG_AARCH64_X29: + case DW_REG_AARCH64_X30: + case DW_REG_AARCH64_SP: + return true; +#elif defined(GP_ARCH_mips64) + case DW_REG_MIPS_FP: + case DW_REG_MIPS_SP: + case DW_REG_MIPS_PC: + return true; +#else +# error "Unknown arch" +#endif + default: + return false; + } +} + +//////////////////////////////////////////////////////////////// +// SecMap // +//////////////////////////////////////////////////////////////// + +// A SecMap may have zero address range, temporarily, whilst RuleSets +// are being added to it. But adding a zero-range SecMap to a PriMap +// will make it impossible to maintain the total order of the PriMap +// entries, and so that can't be allowed to happen. + +class SecMap { + public: + // These summarise the contained mRuleSets, in that they give + // exactly the lowest and highest addresses that any of the entries + // in this SecMap cover. Hence invariants: + // + // mRuleSets is nonempty + // <=> mSummaryMinAddr <= mSummaryMaxAddr + // && mSummaryMinAddr == mRuleSets[0].mAddr + // && mSummaryMaxAddr == mRuleSets[#rulesets-1].mAddr + // + mRuleSets[#rulesets-1].mLen - 1; + // + // This requires that no RuleSet has zero length. + // + // mRuleSets is empty + // <=> mSummaryMinAddr > mSummaryMaxAddr + // + // This doesn't constrain mSummaryMinAddr and mSummaryMaxAddr uniquely, + // so let's use mSummaryMinAddr == 1 and mSummaryMaxAddr == 0 to denote + // this case. + + explicit SecMap(void (*aLog)(const char*)); + ~SecMap(); + + // Binary search mRuleSets to find one that brackets |ia|, or nullptr + // if none is found. It's not allowable to do this until PrepareRuleSets + // has been called first. + RuleSet* FindRuleSet(uintptr_t ia); + + // Add a RuleSet to the collection. The rule is copied in. Calling + // this makes the map non-searchable. + void AddRuleSet(const RuleSet* rs); + + // Add a PfxInstr to the vector of such instrs, and return the index + // in the vector. Calling this makes the map non-searchable. + uint32_t AddPfxInstr(PfxInstr pfxi); + + // Returns the entire vector of PfxInstrs. + const vector<PfxInstr>* GetPfxInstrs() { return &mPfxInstrs; } + + // Prepare the map for searching. Also, remove any rules for code + // address ranges which don't fall inside [start, +len). |len| may + // not be zero. + void PrepareRuleSets(uintptr_t start, size_t len); + + bool IsEmpty(); + + size_t Size() { return mRuleSets.size(); } + + size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const; + + // The min and max addresses of the addresses in the contained + // RuleSets. See comment above for invariants. + uintptr_t mSummaryMinAddr; + uintptr_t mSummaryMaxAddr; + + private: + // False whilst adding entries; true once it is safe to call FindRuleSet. + // Transition (false->true) is caused by calling PrepareRuleSets(). + bool mUsable; + + // A vector of RuleSets, sorted, nonoverlapping (post Prepare()). + vector<RuleSet> mRuleSets; + + // A vector of PfxInstrs, which are referred to by the RuleSets. + // These are provided as a representation of Dwarf expressions + // (DW_CFA_val_expression, DW_CFA_expression, DW_CFA_def_cfa_expression), + // are relatively expensive to evaluate, and and are therefore + // expected to be used only occasionally. + // + // The vector holds a bunch of separate PfxInstr programs, each one + // starting with a PX_Start and terminated by a PX_End, all + // concatenated together. When a RuleSet can't recover a value + // using a self-contained LExpr, it uses a PFXEXPR whose mOffset is + // the index in this vector of start of the necessary PfxInstr program. + vector<PfxInstr> mPfxInstrs; + + // A logging sink, for debugging. + void (*mLog)(const char*); +}; + +} // namespace lul + +#endif // ndef LulMainInt_h diff --git a/mozglue/baseprofiler/lul/platform-linux-lul.cpp b/mozglue/baseprofiler/lul/platform-linux-lul.cpp new file mode 100644 index 0000000000..a9ee65858d --- /dev/null +++ b/mozglue/baseprofiler/lul/platform-linux-lul.cpp @@ -0,0 +1,76 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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 <stdio.h> +#include <signal.h> +#include <string.h> +#include <stdlib.h> +#include <time.h> + +#include "AutoObjectMapper.h" +#include "BaseProfiler.h" +#include "BaseProfilerSharedLibraries.h" +#include "platform.h" +#include "PlatformMacros.h" +#include "LulMain.h" + +// Contains miscellaneous helpers that are used to connect the Gecko Profiler +// and LUL. + +// Find out, in a platform-dependent way, where the code modules got +// mapped in the process' virtual address space, and get |aLUL| to +// load unwind info for them. +void read_procmaps(lul::LUL* aLUL) { + MOZ_ASSERT(aLUL->CountMappings() == 0); + +#if defined(GP_OS_linux) || defined(GP_OS_android) || defined(GP_OS_freebsd) + SharedLibraryInfo info = SharedLibraryInfo::GetInfoForSelf(); + + for (size_t i = 0; i < info.GetSize(); i++) { + const SharedLibrary& lib = info.GetEntry(i); + + std::string nativePath = lib.GetDebugPath(); + + // We can use the standard POSIX-based mapper. + AutoObjectMapperPOSIX mapper(aLUL->mLog); + + // Ask |mapper| to map the object. Then hand its mapped address + // to NotifyAfterMap(). + void* image = nullptr; + size_t size = 0; + bool ok = mapper.Map(&image, &size, nativePath); + if (ok && image && size > 0) { + aLUL->NotifyAfterMap(lib.GetStart(), lib.GetEnd() - lib.GetStart(), + nativePath.c_str(), image); + } else if (!ok && lib.GetDebugName().empty()) { + // The object has no name and (as a consequence) the mapper failed to map + // it. This happens on Linux, where GetInfoForSelf() produces such a + // mapping for the VDSO. This is a problem on x86-{linux,android} because + // lack of knowledge about the mapped area inhibits LUL's special + // __kernel_syscall handling. Hence notify |aLUL| at least of the + // mapping, even though it can't read any unwind information for the area. + aLUL->NotifyExecutableArea(lib.GetStart(), lib.GetEnd() - lib.GetStart()); + } + + // |mapper| goes out of scope at this point and so its destructor + // unmaps the object. + } + +#else +# error "Unknown platform" +#endif +} + +// LUL needs a callback for its logging sink. +void logging_sink_for_LUL(const char* str) { + // These are only printed when Verbose logging is enabled (e.g. with + // MOZ_BASE_PROFILER_VERBOSE_LOGGING=1). This is because LUL's logging is much + // more verbose than the rest of the profiler's logging, which occurs at the + // Info (3) and Debug (4) levels. + // FIXME: This causes a build failure in memory/replace/dmd/test/SmokeDMD (!) + // and other places, because it doesn't link the implementation in + // platform.cpp. + // VERBOSE_LOG("[%d] %s", profiler_current_process_id(), str); +} diff --git a/mozglue/baseprofiler/lul/platform-linux-lul.h b/mozglue/baseprofiler/lul/platform-linux-lul.h new file mode 100644 index 0000000000..b54e80edcf --- /dev/null +++ b/mozglue/baseprofiler/lul/platform-linux-lul.h @@ -0,0 +1,21 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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/. */ + +#ifndef MOZ_PLATFORM_LINUX_LUL_H +#define MOZ_PLATFORM_LINUX_LUL_H + +#include "platform.h" + +#include "BaseProfiler.h" + +// Find out, in a platform-dependent way, where the code modules got +// mapped in the process' virtual address space, and get |aLUL| to +// load unwind info for them. +void read_procmaps(lul::LUL* aLUL); + +// LUL needs a callback for its logging sink. +void logging_sink_for_LUL(const char* str); + +#endif /* ndef MOZ_PLATFORM_LINUX_LUL_H */ diff --git a/mozglue/baseprofiler/moz.build b/mozglue/baseprofiler/moz.build new file mode 100644 index 0000000000..bbefe9582e --- /dev/null +++ b/mozglue/baseprofiler/moz.build @@ -0,0 +1,133 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# 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/. + +# This is pretty much a copy from tools/profiler, cut down to exclude anything +# that cannot work in mozglue (because they are totally dependent on libxul- +# specific code). +# All exported headers now prefixed with "Base" to avoid #include name clashes. + +if CONFIG["MOZ_GECKO_PROFILER"]: + DEFINES["IMPL_MFBT"] = True + EXPORTS += [ + "public/BaseProfilerSharedLibraries.h", + "public/BaseProfilingStack.h", + ] + UNIFIED_SOURCES += [ + "core/PageInformation.cpp", + "core/platform.cpp", + "core/ProfileBuffer.cpp", + "core/ProfileBufferEntry.cpp", + "core/ProfiledThreadData.cpp", + "core/ProfileJSONWriter.cpp", + "core/ProfilerBacktrace.cpp", + "core/ProfilerMarkers.cpp", + "core/ProfilingCategory.cpp", + "core/ProfilingStack.cpp", + "core/RegisteredThread.cpp", + ] + + if CONFIG["OS_TARGET"] in ("Android", "Linux", "FreeBSD"): + if CONFIG["CPU_ARCH"] in ("arm", "aarch64", "x86", "x86_64", "mips64"): + UNIFIED_SOURCES += [ + "lul/AutoObjectMapper.cpp", + "lul/LulCommon.cpp", + "lul/LulDwarf.cpp", + "lul/LulDwarfSummariser.cpp", + "lul/LulElf.cpp", + "lul/LulMain.cpp", + "lul/platform-linux-lul.cpp", + ] + # These files cannot be built in unified mode because of name clashes with mozglue headers on Android. + SOURCES += [ + "core/shared-libraries-linux.cc", + ] + if CONFIG["CPU_ARCH"] == "arm" and CONFIG["OS_TARGET"] != "FreeBSD": + SOURCES += [ + "core/EHABIStackWalk.cpp", + ] + elif CONFIG["OS_TARGET"] == "Darwin": + UNIFIED_SOURCES += [ + "core/shared-libraries-macos.cc", + ] + elif CONFIG["OS_TARGET"] == "WINNT": + SOURCES += [ + "core/shared-libraries-win32.cc", + ] + + LOCAL_INCLUDES += [ + "/mozglue/baseprofiler/core/", + "/mozglue/linker", + ] + + if CONFIG["OS_TARGET"] == "Android": + DEFINES["ANDROID_NDK_MAJOR_VERSION"] = CONFIG["ANDROID_NDK_MAJOR_VERSION"] + DEFINES["ANDROID_NDK_MINOR_VERSION"] = CONFIG["ANDROID_NDK_MINOR_VERSION"] + +GeneratedFile( + "public/ProfilingCategoryList.h", + script="build/generate_profiling_categories.py", + entry_point="generate_macro_header", + inputs=["build/profiling_categories.yaml"], +) + +EXPORTS += [ + "!public/ProfilingCategoryList.h", + "public/BaseProfiler.h", + "public/BaseProfilingCategory.h", +] + +EXPORTS.mozilla += [ + "public/BaseAndGeckoProfilerDetail.h", + "public/BaseProfileJSONWriter.h", + "public/BaseProfilerCounts.h", + "public/BaseProfilerDetail.h", + "public/BaseProfilerLabels.h", + "public/BaseProfilerMarkers.h", + "public/BaseProfilerMarkersDetail.h", + "public/BaseProfilerMarkersPrerequisites.h", + "public/BaseProfilerMarkerTypes.h", + "public/BaseProfilerRAIIMacro.h", + "public/BaseProfilerState.h", + "public/BaseProfilerUtils.h", + "public/BlocksRingBuffer.h", + "public/FailureLatch.h", + "public/leb128iterator.h", + "public/ModuloBuffer.h", + "public/PowerOfTwo.h", + "public/ProfileBufferChunk.h", + "public/ProfileBufferChunkManager.h", + "public/ProfileBufferChunkManagerSingle.h", + "public/ProfileBufferChunkManagerWithLocalLimit.h", + "public/ProfileBufferControlledChunkManager.h", + "public/ProfileBufferEntryKinds.h", + "public/ProfileBufferEntrySerialization.h", + "public/ProfileBufferIndex.h", + "public/ProfileChunkedBuffer.h", + "public/ProfileChunkedBufferDetail.h", + "public/ProgressLogger.h", + "public/ProportionValue.h", +] + +UNIFIED_SOURCES += [ + "core/BaseAndGeckoProfilerDetail.cpp", + "core/ProfilerUtils.cpp", +] + +if CONFIG["MOZ_VTUNE"]: + DEFINES["MOZ_VTUNE_INSTRUMENTATION"] = True + UNIFIED_SOURCES += [ + "core/VTuneProfiler.cpp", + ] + +FINAL_LIBRARY = "mozglue" + +if CONFIG["CC_TYPE"] in ("clang", "gcc"): + CXXFLAGS += [ + "-Wno-ignored-qualifiers", # due to use of breakpad headers + ] + +with Files("**"): + BUG_COMPONENT = ("Core", "Gecko Profiler") diff --git a/mozglue/baseprofiler/public/BaseAndGeckoProfilerDetail.h b/mozglue/baseprofiler/public/BaseAndGeckoProfilerDetail.h new file mode 100644 index 0000000000..f45170c83f --- /dev/null +++ b/mozglue/baseprofiler/public/BaseAndGeckoProfilerDetail.h @@ -0,0 +1,64 @@ +/* -*- Mode: C++; tab-width: 2; 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/. */ + +// Internal Base and Gecko Profiler utilities. +// It should declare or define things that are used in both profilers, but not +// needed outside of the profilers. +// In particular, it is *not* included in popular headers like BaseProfiler.h +// and GeckoProfiler.h, to avoid rebuilding the world when this is modified. + +#ifndef BaseAndGeckoProfilerDetail_h +#define BaseAndGeckoProfilerDetail_h + +#include "mozilla/BaseProfilerUtils.h" +#include "mozilla/Span.h" +#include "mozilla/TimeStamp.h" +#include "mozilla/Types.h" +#include "mozilla/UniquePtr.h" + +namespace mozilla { + +class ProfileBufferChunkManagerWithLocalLimit; + +namespace baseprofiler::detail { + +[[nodiscard]] MFBT_API TimeStamp GetProfilingStartTime(); + +[[nodiscard]] MFBT_API UniquePtr<ProfileBufferChunkManagerWithLocalLimit> +ExtractBaseProfilerChunkManager(); + +// If the current thread is registered, returns its registration time, otherwise +// a null timestamp. +[[nodiscard]] MFBT_API TimeStamp GetThreadRegistrationTime(); + +} // namespace baseprofiler::detail + +namespace profiler::detail { + +// True if the filter is exactly "pid:<aPid>". +[[nodiscard]] MFBT_API bool FilterHasPid( + const char* aFilter, baseprofiler::BaseProfilerProcessId aPid = + baseprofiler::profiler_current_process_id()); + +// Only true if the filters only contain "pid:..." strings, and *none* of them +// is exactly "pid:<aPid>". E.g.: +// - [], 123 -> false (no pids) +// - ["main"], 123 -> false (not all pids) +// - ["main", "pid:123"], 123 -> false (not all pids) +// - ["pid:123"], 123 -> false (all pids, including "pid:123") +// - ["pid:123", "pid:456"], 123 -> false (all pids, including "pid:123") +// - ["pid:456"], 123 -> true (all pids, but no "pid:123") +// - ["pid:456", "pid:789"], 123 -> true (all pids, but no "pid:123") +[[nodiscard]] MFBT_API bool FiltersExcludePid( + Span<const char* const> aFilters, + baseprofiler::BaseProfilerProcessId aPid = + baseprofiler::profiler_current_process_id()); + +} // namespace profiler::detail + +} // namespace mozilla + +#endif // BaseAndGeckoProfilerDetail_h diff --git a/mozglue/baseprofiler/public/BaseProfileJSONWriter.h b/mozglue/baseprofiler/public/BaseProfileJSONWriter.h new file mode 100644 index 0000000000..00a2926366 --- /dev/null +++ b/mozglue/baseprofiler/public/BaseProfileJSONWriter.h @@ -0,0 +1,600 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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/. */ + +#ifndef BASEPROFILEJSONWRITER_H +#define BASEPROFILEJSONWRITER_H + +#include "mozilla/FailureLatch.h" +#include "mozilla/HashFunctions.h" +#include "mozilla/HashTable.h" +#include "mozilla/JSONWriter.h" +#include "mozilla/Maybe.h" +#include "mozilla/NotNull.h" +#include "mozilla/ProgressLogger.h" +#include "mozilla/TimeStamp.h" +#include "mozilla/UniquePtrExtensions.h" + +#include <functional> +#include <ostream> +#include <string_view> + +namespace mozilla { +namespace baseprofiler { + +class SpliceableJSONWriter; + +// On average, profile JSONs are large enough such that we want to avoid +// reallocating its buffer when expanding. Additionally, the contents of the +// profile are not accessed until the profile is entirely written. For these +// reasons we use a chunked writer that keeps an array of chunks, which is +// concatenated together after writing is finished. +class ChunkedJSONWriteFunc final : public JSONWriteFunc, public FailureLatch { + public: + friend class SpliceableJSONWriter; + + explicit ChunkedJSONWriteFunc(FailureLatch& aFailureLatch) + : mFailureLatch(WrapNotNullUnchecked(&aFailureLatch)) { + (void)AllocChunk(kChunkSize); + } + + [[nodiscard]] bool IsEmpty() const { + MOZ_ASSERT_IF(!mChunkPtr, !mChunkEnd && mChunkList.length() == 0 && + mChunkLengths.length() == 0); + return !mChunkPtr; + } + + // Length of data written so far, excluding null terminator. + [[nodiscard]] size_t Length() const { + MOZ_ASSERT(mChunkLengths.length() == mChunkList.length()); + size_t totalLen = 0; + for (size_t i = 0; i < mChunkLengths.length(); i++) { + MOZ_ASSERT(strlen(mChunkList[i].get()) == mChunkLengths[i]); + totalLen += mChunkLengths[i]; + } + return totalLen; + } + + void Write(const Span<const char>& aStr) final { + if (Failed()) { + return; + } + + MOZ_ASSERT(mChunkPtr >= mChunkList.back().get() && mChunkPtr <= mChunkEnd); + MOZ_ASSERT(mChunkEnd >= mChunkList.back().get() + mChunkLengths.back()); + MOZ_ASSERT(*mChunkPtr == '\0'); + + // Most strings to be written are small, but subprocess profiles (e.g., + // from the content process in e10s) may be huge. If the string is larger + // than a chunk, allocate its own chunk. + char* newPtr; + if (aStr.size() >= kChunkSize) { + if (!AllocChunk(aStr.size() + 1)) { + return; + } + newPtr = mChunkPtr + aStr.size(); + } else { + newPtr = mChunkPtr + aStr.size(); + if (newPtr >= mChunkEnd) { + if (!AllocChunk(kChunkSize)) { + return; + } + newPtr = mChunkPtr + aStr.size(); + } + } + + memcpy(mChunkPtr, aStr.data(), aStr.size()); + *newPtr = '\0'; + mChunkPtr = newPtr; + mChunkLengths.back() += aStr.size(); + } + + [[nodiscard]] bool CopyDataIntoLazilyAllocatedBuffer( + const std::function<char*(size_t)>& aAllocator) const { + // Request a buffer for the full content plus a null terminator. + if (Failed()) { + return false; + } + + char* ptr = aAllocator(Length() + 1); + + if (!ptr) { + // Failed to allocate memory. + return false; + } + + for (size_t i = 0; i < mChunkList.length(); i++) { + size_t len = mChunkLengths[i]; + memcpy(ptr, mChunkList[i].get(), len); + ptr += len; + } + *ptr = '\0'; + return true; + } + + [[nodiscard]] UniquePtr<char[]> CopyData() const { + UniquePtr<char[]> c; + if (!CopyDataIntoLazilyAllocatedBuffer([&](size_t allocationSize) { + c = MakeUnique<char[]>(allocationSize); + return c.get(); + })) { + // Something went wrong, make sure the returned pointer is null even if + // the allocation happened. + c = nullptr; + } + return c; + } + + void Take(ChunkedJSONWriteFunc&& aOther) { + SetFailureFrom(aOther); + if (Failed()) { + return; + } + + for (size_t i = 0; i < aOther.mChunkList.length(); i++) { + MOZ_ALWAYS_TRUE(mChunkLengths.append(aOther.mChunkLengths[i])); + MOZ_ALWAYS_TRUE(mChunkList.append(std::move(aOther.mChunkList[i]))); + } + mChunkPtr = mChunkList.back().get() + mChunkLengths.back(); + mChunkEnd = mChunkPtr; + aOther.Clear(); + } + + FAILURELATCH_IMPL_PROXY(*mFailureLatch) + + // Change the failure latch to be used here, and if the previous latch was + // already in failure state, set that failure in the new latch. + // This allows using this WriteFunc in isolation, before attempting to bring + // it into another operation group with its own FailureLatch. + void ChangeFailureLatchAndForwardState(FailureLatch& aFailureLatch) { + aFailureLatch.SetFailureFrom(*this); + mFailureLatch = WrapNotNullUnchecked(&aFailureLatch); + } + + private: + void Clear() { + mChunkPtr = nullptr; + mChunkEnd = nullptr; + mChunkList.clear(); + mChunkLengths.clear(); + } + + void ClearAndSetFailure(std::string aFailure) { + Clear(); + SetFailure(std::move(aFailure)); + } + + [[nodiscard]] bool ClearAndSetFailureAndFalse(std::string aFailure) { + ClearAndSetFailure(std::move(aFailure)); + return false; + } + + [[nodiscard]] bool AllocChunk(size_t aChunkSize) { + if (Failed()) { + if (mChunkPtr) { + // FailureLatch is in failed state, but chunks have not been cleared yet + // (error must have happened elsewhere). + Clear(); + } + return false; + } + + MOZ_ASSERT(mChunkLengths.length() == mChunkList.length()); + UniquePtr<char[]> newChunk = MakeUniqueFallible<char[]>(aChunkSize); + if (!newChunk) { + return ClearAndSetFailureAndFalse( + "OOM in ChunkedJSONWriteFunc::AllocChunk allocating new chunk"); + } + mChunkPtr = newChunk.get(); + mChunkEnd = mChunkPtr + aChunkSize; + *mChunkPtr = '\0'; + if (!mChunkLengths.append(0)) { + return ClearAndSetFailureAndFalse( + "OOM in ChunkedJSONWriteFunc::AllocChunk appending length"); + } + if (!mChunkList.append(std::move(newChunk))) { + return ClearAndSetFailureAndFalse( + "OOM in ChunkedJSONWriteFunc::AllocChunk appending new chunk"); + } + return true; + } + + static const size_t kChunkSize = 4096 * 512; + + // Pointer for writing inside the current chunk. + // + // The current chunk is always at the back of mChunkList, i.e., + // mChunkList.back() <= mChunkPtr <= mChunkEnd. + char* mChunkPtr = nullptr; + + // Pointer to the end of the current chunk. + // + // The current chunk is always at the back of mChunkList, i.e., + // mChunkEnd >= mChunkList.back() + mChunkLengths.back(). + char* mChunkEnd = nullptr; + + // List of chunks and their lengths. + // + // For all i, the length of the string in mChunkList[i] is + // mChunkLengths[i]. + Vector<UniquePtr<char[]>> mChunkList; + Vector<size_t> mChunkLengths; + + NotNull<FailureLatch*> mFailureLatch; +}; + +struct OStreamJSONWriteFunc final : public JSONWriteFunc { + explicit OStreamJSONWriteFunc(std::ostream& aStream) : mStream(aStream) {} + + void Write(const Span<const char>& aStr) final { + std::string_view sv(aStr.data(), aStr.size()); + mStream << sv; + } + + std::ostream& mStream; +}; + +class UniqueJSONStrings; + +class SpliceableJSONWriter : public JSONWriter, public FailureLatch { + public: + SpliceableJSONWriter(JSONWriteFunc& aWriter, FailureLatch& aFailureLatch) + : JSONWriter(aWriter, JSONWriter::SingleLineStyle), + mFailureLatch(WrapNotNullUnchecked(&aFailureLatch)) {} + + SpliceableJSONWriter(UniquePtr<JSONWriteFunc> aWriter, + FailureLatch& aFailureLatch) + : JSONWriter(std::move(aWriter), JSONWriter::SingleLineStyle), + mFailureLatch(WrapNotNullUnchecked(&aFailureLatch)) {} + + void StartBareList() { StartCollection(scEmptyString, scEmptyString); } + + void EndBareList() { EndCollection(scEmptyString); } + + // Output a time (int64_t given in nanoseconds) in milliseconds. trim zeroes. + // E.g.: 1'234'567'890 -> "1234.56789" + void TimeI64NsProperty(const Span<const char>& aMaybePropertyName, + int64_t aTime_ns) { + if (aTime_ns == 0) { + Scalar(aMaybePropertyName, MakeStringSpan("0")); + return; + } + + static constexpr int64_t million = 1'000'000; + const int64_t absNanos = std::abs(aTime_ns); + const int64_t integerMilliseconds = absNanos / million; + auto remainderNanoseconds = static_cast<uint32_t>(absNanos % million); + + // Plenty enough to fit INT64_MIN (-9223372036854775808). + static constexpr size_t DIGITS_MAX = 23; + char buf[DIGITS_MAX + 1]; + int len = + snprintf(buf, DIGITS_MAX, (aTime_ns >= 0) ? "%" PRIu64 : "-%" PRIu64, + integerMilliseconds); + if (remainderNanoseconds != 0) { + buf[len++] = '.'; + // Output up to 6 fractional digits. Exit early if the rest would + // be trailing zeros. + uint32_t powerOfTen = static_cast<uint32_t>(million / 10); + for (;;) { + auto digit = remainderNanoseconds / powerOfTen; + buf[len++] = '0' + static_cast<char>(digit); + remainderNanoseconds %= powerOfTen; + if (remainderNanoseconds == 0) { + break; + } + powerOfTen /= 10; + if (powerOfTen == 0) { + break; + } + } + } + + Scalar(aMaybePropertyName, Span<const char>(buf, len)); + } + + // Output a (double) time in milliseconds, with at best nanosecond precision. + void TimeDoubleMsProperty(const Span<const char>& aMaybePropertyName, + double aTime_ms) { + const double dTime_ns = aTime_ms * 1'000'000.0; + // Make sure it's well within int64_t range. + // 2^63 nanoseconds is almost 300 years; these times are relative to + // firefox startup, this should be enough for most uses. + if (dTime_ns >= 0.0) { + MOZ_RELEASE_ASSERT(dTime_ns < double(INT64_MAX - 1)); + } else { + MOZ_RELEASE_ASSERT(dTime_ns > double(INT64_MIN + 2)); + } + // Round to nearest integer nanosecond. The conversion to integer truncates + // the fractional part, so first we need to push it 0.5 away from zero. + const int64_t iTime_ns = + (dTime_ns >= 0.0) ? int64_t(dTime_ns + 0.5) : int64_t(dTime_ns - 0.5); + TimeI64NsProperty(aMaybePropertyName, iTime_ns); + } + + // Output a (double) time in milliseconds, with at best nanosecond precision. + void TimeDoubleMsElement(double aTime_ms) { + TimeDoubleMsProperty(nullptr, aTime_ms); + } + + // This function must be used to correctly stream timestamps in profiles. + // Null timestamps don't output anything. + void TimeProperty(const Span<const char>& aMaybePropertyName, + const TimeStamp& aTime) { + if (!aTime.IsNull()) { + TimeDoubleMsProperty( + aMaybePropertyName, + (aTime - TimeStamp::ProcessCreation()).ToMilliseconds()); + } + } + + void NullElements(uint32_t aCount) { + for (uint32_t i = 0; i < aCount; i++) { + NullElement(); + } + } + + void Splice(const Span<const char>& aStr) { + Separator(); + WriteFunc().Write(aStr); + mNeedComma[mDepth] = true; + } + + void Splice(const char* aStr, size_t aLen) { + Separator(); + WriteFunc().Write(Span<const char>(aStr, aLen)); + mNeedComma[mDepth] = true; + } + + // Splice the given JSON directly in, without quoting. + void SplicedJSONProperty(const Span<const char>& aMaybePropertyName, + const Span<const char>& aJsonValue) { + Scalar(aMaybePropertyName, aJsonValue); + } + + void CopyAndSplice(const ChunkedJSONWriteFunc& aFunc) { + Separator(); + for (size_t i = 0; i < aFunc.mChunkList.length(); i++) { + WriteFunc().Write( + Span<const char>(aFunc.mChunkList[i].get(), aFunc.mChunkLengths[i])); + } + mNeedComma[mDepth] = true; + } + + // Takes the chunks from aFunc and write them. If move is not possible + // (e.g., using OStreamJSONWriteFunc), aFunc's chunks are copied and its + // storage cleared. + virtual void TakeAndSplice(ChunkedJSONWriteFunc&& aFunc) { + Separator(); + for (size_t i = 0; i < aFunc.mChunkList.length(); i++) { + WriteFunc().Write( + Span<const char>(aFunc.mChunkList[i].get(), aFunc.mChunkLengths[i])); + } + aFunc.mChunkPtr = nullptr; + aFunc.mChunkEnd = nullptr; + aFunc.mChunkList.clear(); + aFunc.mChunkLengths.clear(); + mNeedComma[mDepth] = true; + } + + // Set (or reset) the pointer to a UniqueJSONStrings. + void SetUniqueStrings(UniqueJSONStrings& aUniqueStrings) { + MOZ_RELEASE_ASSERT(!mUniqueStrings); + mUniqueStrings = &aUniqueStrings; + } + + // Set (or reset) the pointer to a UniqueJSONStrings. + void ResetUniqueStrings() { + MOZ_RELEASE_ASSERT(mUniqueStrings); + mUniqueStrings = nullptr; + } + + // Add `aStr` to the unique-strings list (if not already there), and write its + // index as a named object property. + inline void UniqueStringProperty(const Span<const char>& aName, + const Span<const char>& aStr); + + // Add `aStr` to the unique-strings list (if not already there), and write its + // index as an array element. + inline void UniqueStringElement(const Span<const char>& aStr); + + // THe following functions override JSONWriter functions non-virtually. The + // goal is to try and prevent calls that specify a style, which would be + // ignored anyway because the whole thing is single-lined. It's fine if some + // calls still make it through a `JSONWriter&`, no big deal. + void Start() { JSONWriter::Start(); } + void StartArrayProperty(const Span<const char>& aName) { + JSONWriter::StartArrayProperty(aName); + } + template <size_t N> + void StartArrayProperty(const char (&aName)[N]) { + JSONWriter::StartArrayProperty(Span<const char>(aName, N)); + } + void StartArrayElement() { JSONWriter::StartArrayElement(); } + void StartObjectProperty(const Span<const char>& aName) { + JSONWriter::StartObjectProperty(aName); + } + template <size_t N> + void StartObjectProperty(const char (&aName)[N]) { + JSONWriter::StartObjectProperty(Span<const char>(aName, N)); + } + void StartObjectElement() { JSONWriter::StartObjectElement(); } + + FAILURELATCH_IMPL_PROXY(*mFailureLatch) + + protected: + NotNull<FailureLatch*> mFailureLatch; + + private: + UniqueJSONStrings* mUniqueStrings = nullptr; +}; + +class SpliceableChunkedJSONWriter final : public SpliceableJSONWriter { + public: + explicit SpliceableChunkedJSONWriter(FailureLatch& aFailureLatch) + : SpliceableJSONWriter(MakeUnique<ChunkedJSONWriteFunc>(aFailureLatch), + aFailureLatch) {} + + // Access the ChunkedJSONWriteFunc as reference-to-const, usually to copy data + // out. + const ChunkedJSONWriteFunc& ChunkedWriteFunc() const { + return ChunkedWriteFuncRef(); + } + + // Access the ChunkedJSONWriteFunc as rvalue-reference, usually to take its + // data out. This writer shouldn't be used anymore after this. + ChunkedJSONWriteFunc&& TakeChunkedWriteFunc() { + ChunkedJSONWriteFunc& ref = ChunkedWriteFuncRef(); +#ifdef DEBUG + mTaken = true; +#endif // + return std::move(ref); + } + + // Adopts the chunks from aFunc without copying. + void TakeAndSplice(ChunkedJSONWriteFunc&& aFunc) override { + MOZ_ASSERT(!mTaken); + Separator(); + ChunkedWriteFuncRef().Take(std::move(aFunc)); + mNeedComma[mDepth] = true; + } + + void ChangeFailureLatchAndForwardState(FailureLatch& aFailureLatch) { + mFailureLatch = WrapNotNullUnchecked(&aFailureLatch); + return ChunkedWriteFuncRef().ChangeFailureLatchAndForwardState( + aFailureLatch); + } + + private: + const ChunkedJSONWriteFunc& ChunkedWriteFuncRef() const { + MOZ_ASSERT(!mTaken); + // The WriteFunc was non-fallibly allocated as a ChunkedJSONWriteFunc in the + // only constructor above, so it's safe to cast to ChunkedJSONWriteFunc&. + return static_cast<const ChunkedJSONWriteFunc&>(WriteFunc()); + } + + ChunkedJSONWriteFunc& ChunkedWriteFuncRef() { + MOZ_ASSERT(!mTaken); + // The WriteFunc was non-fallibly allocated as a ChunkedJSONWriteFunc in the + // only constructor above, so it's safe to cast to ChunkedJSONWriteFunc&. + return static_cast<ChunkedJSONWriteFunc&>(WriteFunc()); + } + +#ifdef DEBUG + bool mTaken = false; +#endif +}; + +class JSONSchemaWriter { + JSONWriter& mWriter; + uint32_t mIndex; + + public: + explicit JSONSchemaWriter(JSONWriter& aWriter) : mWriter(aWriter), mIndex(0) { + aWriter.StartObjectProperty("schema", + SpliceableJSONWriter::SingleLineStyle); + } + + void WriteField(const Span<const char>& aName) { + mWriter.IntProperty(aName, mIndex++); + } + + template <size_t Np1> + void WriteField(const char (&aName)[Np1]) { + WriteField(Span<const char>(aName, Np1 - 1)); + } + + ~JSONSchemaWriter() { mWriter.EndObject(); } +}; + +// This class helps create an indexed list of unique strings, and inserts the +// index as a JSON value. The collected list of unique strings can later be +// inserted as a JSON array. +// This can be useful for elements/properties with many repeated strings. +// +// With only JSONWriter w, +// `w.WriteElement("a"); w.WriteElement("b"); w.WriteElement("a");` +// when done inside a JSON array, will generate: +// `["a", "b", "c"]` +// +// With UniqueStrings u, +// `u.WriteElement(w, "a"); u.WriteElement(w, "b"); u.WriteElement(w, "a");` +// when done inside a JSON array, will generate: +// `[0, 1, 0]` +// and later, `u.SpliceStringTableElements(w)` (inside a JSON array), will +// output the corresponding indexed list of unique strings: +// `["a", "b"]` +class UniqueJSONStrings final : public FailureLatch { + public: + // Start an empty list of unique strings. + MFBT_API explicit UniqueJSONStrings(FailureLatch& aFailureLatch); + + // Start with a copy of the strings from another list. + MFBT_API UniqueJSONStrings(FailureLatch& aFailureLatch, + const UniqueJSONStrings& aOther, + ProgressLogger aProgressLogger); + + MFBT_API ~UniqueJSONStrings(); + + // Add `aStr` to the list (if not already there), and write its index as a + // named object property. + void WriteProperty(SpliceableJSONWriter& aWriter, + const Span<const char>& aName, + const Span<const char>& aStr) { + if (const Maybe<uint32_t> maybeIndex = GetOrAddIndex(aStr); maybeIndex) { + aWriter.IntProperty(aName, *maybeIndex); + } else { + aWriter.SetFailureFrom(*this); + } + } + + // Add `aStr` to the list (if not already there), and write its index as an + // array element. + void WriteElement(SpliceableJSONWriter& aWriter, + const Span<const char>& aStr) { + if (const Maybe<uint32_t> maybeIndex = GetOrAddIndex(aStr); maybeIndex) { + aWriter.IntElement(*maybeIndex); + } else if (!aWriter.Failed()) { + aWriter.SetFailureFrom(*this); + } + } + + // Splice all collected unique strings into an array. This should only be done + // once, and then this UniqueStrings shouldn't be used anymore. + MFBT_API void SpliceStringTableElements(SpliceableJSONWriter& aWriter); + + FAILURELATCH_IMPL_PROXY(mStringTableWriter) + + void ChangeFailureLatchAndForwardState(FailureLatch& aFailureLatch) { + mStringTableWriter.ChangeFailureLatchAndForwardState(aFailureLatch); + } + + private: + MFBT_API void ClearAndSetFailure(std::string aFailure); + + // If `aStr` is already listed, return its index. + // Otherwise add it to the list and return the new index. + MFBT_API Maybe<uint32_t> GetOrAddIndex(const Span<const char>& aStr); + + SpliceableChunkedJSONWriter mStringTableWriter; + HashMap<HashNumber, uint32_t> mStringHashToIndexMap; +}; + +void SpliceableJSONWriter::UniqueStringProperty(const Span<const char>& aName, + const Span<const char>& aStr) { + MOZ_RELEASE_ASSERT(mUniqueStrings); + mUniqueStrings->WriteProperty(*this, aName, aStr); +} + +// Add `aStr` to the list (if not already there), and write its index as an +// array element. +void SpliceableJSONWriter::UniqueStringElement(const Span<const char>& aStr) { + MOZ_RELEASE_ASSERT(mUniqueStrings); + mUniqueStrings->WriteElement(*this, aStr); +} + +} // namespace baseprofiler +} // namespace mozilla + +#endif // BASEPROFILEJSONWRITER_H diff --git a/mozglue/baseprofiler/public/BaseProfiler.h b/mozglue/baseprofiler/public/BaseProfiler.h new file mode 100644 index 0000000000..a085eb9774 --- /dev/null +++ b/mozglue/baseprofiler/public/BaseProfiler.h @@ -0,0 +1,506 @@ +/* -*- Mode: C++; tab-width: 2; 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/. */ + +// The Gecko Profiler is an always-on profiler that takes fast and low overhead +// samples of the program execution using only userspace functionality for +// portability. The goal of this module is to provide performance data in a +// generic cross-platform way without requiring custom tools or kernel support. +// +// Samples are collected to form a timeline with optional timeline event +// (markers) used for filtering. The samples include both native stacks and +// platform-independent "label stack" frames. + +#ifndef BaseProfiler_h +#define BaseProfiler_h + +// This file is safe to include unconditionally, and only defines +// empty macros if MOZ_GECKO_PROFILER is not set. + +// These headers are also safe to include unconditionally, with empty macros if +// MOZ_GECKO_PROFILER is not set. +// If your file only uses particular APIs (e.g., only markers), please consider +// including only the needed headers instead of this one, to reduce compilation +// dependencies. +#include "mozilla/BaseProfilerCounts.h" +#include "mozilla/BaseProfilerLabels.h" +#include "mozilla/BaseProfilerMarkers.h" +#include "mozilla/BaseProfilerState.h" + +#ifndef MOZ_GECKO_PROFILER + +# include "mozilla/UniquePtr.h" + +// This file can be #included unconditionally. However, everything within this +// file must be guarded by a #ifdef MOZ_GECKO_PROFILER, *except* for the +// following macros and functions, which encapsulate the most common operations +// and thus avoid the need for many #ifdefs. + +# define AUTO_BASE_PROFILER_INIT \ + ::mozilla::baseprofiler::profiler_init_main_thread_id() + +# define BASE_PROFILER_REGISTER_THREAD(name) +# define BASE_PROFILER_UNREGISTER_THREAD() +# define AUTO_BASE_PROFILER_REGISTER_THREAD(name) + +# define AUTO_BASE_PROFILER_THREAD_SLEEP +# define AUTO_BASE_PROFILER_THREAD_WAKE + +// Function stubs for when MOZ_GECKO_PROFILER is not defined. + +namespace mozilla { + +namespace baseprofiler { +// This won't be used, it's just there to allow the empty definition of +// `profiler_get_backtrace`. +struct ProfilerBacktrace {}; +using UniqueProfilerBacktrace = UniquePtr<ProfilerBacktrace>; + +// Get/Capture-backtrace functions can return nullptr or false, the result +// should be fed to another empty macro or stub anyway. + +static inline UniqueProfilerBacktrace profiler_get_backtrace() { + return nullptr; +} + +static inline bool profiler_capture_backtrace_into( + ProfileChunkedBuffer& aChunkedBuffer, StackCaptureOptions aCaptureOptions) { + return false; +} + +static inline UniquePtr<ProfileChunkedBuffer> profiler_capture_backtrace() { + return nullptr; +} + +static inline void profiler_init(void* stackTop) {} + +static inline void profiler_shutdown() {} + +} // namespace baseprofiler +} // namespace mozilla + +#else // !MOZ_GECKO_PROFILER + +# include "BaseProfilingStack.h" + +# include "mozilla/Assertions.h" +# include "mozilla/Atomics.h" +# include "mozilla/Attributes.h" +# include "mozilla/BaseProfilerRAIIMacro.h" +# include "mozilla/Maybe.h" +# include "mozilla/PowerOfTwo.h" +# include "mozilla/TimeStamp.h" +# include "mozilla/UniquePtr.h" + +# include <functional> +# include <stdint.h> +# include <string> + +namespace mozilla { + +class MallocAllocPolicy; +class ProfileChunkedBuffer; +enum class StackCaptureOptions; +template <class T, size_t MinInlineCapacity, class AllocPolicy> +class Vector; + +namespace baseprofiler { + +class ProfilerBacktrace; +class SpliceableJSONWriter; + +//--------------------------------------------------------------------------- +// Start and stop the profiler +//--------------------------------------------------------------------------- + +static constexpr PowerOfTwo32 BASE_PROFILER_DEFAULT_ENTRIES = +# if !defined(GP_PLAT_arm_android) + MakePowerOfTwo32<8 * 1024 * 1024>(); // 8M entries = 64MB +# else + MakePowerOfTwo32<2 * 1024 * 1024>(); // 2M entries = 16MB +# endif + +// Startup profiling usually need to capture more data, especially on slow +// systems. +// Note: Keep in sync with GeckoThread.maybeStartGeckoProfiler: +// https://searchfox.org/mozilla-central/source/mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoThread.java +static constexpr PowerOfTwo32 BASE_PROFILER_DEFAULT_STARTUP_ENTRIES = +# if !defined(GP_PLAT_arm_android) + mozilla::MakePowerOfTwo32<64 * 1024 * 1024>(); // 64M entries = 512MB +# else + mozilla::MakePowerOfTwo32<8 * 1024 * 1024>(); // 8M entries = 64MB +# endif + +// Note: Keep in sync with GeckoThread.maybeStartGeckoProfiler: +// https://searchfox.org/mozilla-central/source/mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoThread.java +# define BASE_PROFILER_DEFAULT_INTERVAL 1 /* millisecond */ +# define BASE_PROFILER_MAX_INTERVAL 5000 /* milliseconds */ + +// Initialize the profiler. If MOZ_PROFILER_STARTUP is set the profiler will +// also be started. This call must happen before any other profiler calls +// (except profiler_start(), which will call profiler_init() if it hasn't +// already run). +MFBT_API void profiler_init(void* stackTop); + +# define AUTO_BASE_PROFILER_INIT \ + ::mozilla::baseprofiler::AutoProfilerInit PROFILER_RAII + +// Clean up the profiler module, stopping it if required. This function may +// also save a shutdown profile if requested. No profiler calls should happen +// after this point and all profiling stack labels should have been popped. +MFBT_API void profiler_shutdown(); + +// Start the profiler -- initializing it first if necessary -- with the +// selected options. Stops and restarts the profiler if it is already active. +// After starting the profiler is "active". The samples will be recorded in a +// circular buffer. +// "aCapacity" is the maximum number of 8-byte entries in the profiler's +// circular buffer. +// "aInterval" the sampling interval, measured in millseconds. +// "aFeatures" is the feature set. Features unsupported by this +// platform/configuration are ignored. +// "aFilters" is the list of thread filters. Threads that do not match any +// of the filters are not profiled. A filter matches a thread if +// (a) the thread name contains the filter as a case-insensitive +// substring, or +// (b) the filter is of the form "pid:<n>" where n is the process +// id of the process that the thread is running in. +// "aDuration" is the duration of entries in the profiler's circular buffer. +MFBT_API void profiler_start(PowerOfTwo32 aCapacity, double aInterval, + uint32_t aFeatures, const char** aFilters, + uint32_t aFilterCount, + const Maybe<double>& aDuration = Nothing()); + +// Stop the profiler and discard the profile without saving it. A no-op if the +// profiler is inactive. After stopping the profiler is "inactive". +MFBT_API void profiler_stop(); + +// If the profiler is inactive, start it. If it's already active, restart it if +// the requested settings differ from the current settings. Both the check and +// the state change are performed while the profiler state is locked. +// The only difference to profiler_start is that the current buffer contents are +// not discarded if the profiler is already running with the requested settings. +MFBT_API void profiler_ensure_started( + PowerOfTwo32 aCapacity, double aInterval, uint32_t aFeatures, + const char** aFilters, uint32_t aFilterCount, + const Maybe<double>& aDuration = Nothing()); + +//--------------------------------------------------------------------------- +// Control the profiler +//--------------------------------------------------------------------------- + +// Register/unregister threads with the profiler. Both functions operate the +// same whether the profiler is active or inactive. +# define BASE_PROFILER_REGISTER_THREAD(name) \ + do { \ + char stackTop; \ + ::mozilla::baseprofiler::profiler_register_thread(name, &stackTop); \ + } while (0) +# define BASE_PROFILER_UNREGISTER_THREAD() \ + ::mozilla::baseprofiler::profiler_unregister_thread() +MFBT_API ProfilingStack* profiler_register_thread(const char* name, + void* guessStackTop); +MFBT_API void profiler_unregister_thread(); + +// Registers a DOM Window (the JS global `window`) with the profiler. Each +// Window _roughly_ corresponds to a single document loaded within a +// browsing context. Both the Window Id and Browser Id are recorded to allow +// correlating different Windows loaded within the same tab or frame element. +// +// We register pages for each navigations but we do not register +// history.pushState or history.replaceState since they correspond to the same +// Inner Window ID. When a browsing context is first loaded, the first url +// loaded in it will be about:blank. Because of that, this call keeps the first +// non-about:blank registration of window and discards the previous one. +// +// "aTabID" is the BrowserId of that document belongs to. +// That's used to determine the tab of that page. +// "aInnerWindowID" is the ID of the `window` global object of that +// document. +// "aUrl" is the URL of the page. +// "aEmbedderInnerWindowID" is the inner window id of embedder. It's used to +// determine sub documents of a page. +MFBT_API void profiler_register_page(uint64_t aTabD, uint64_t aInnerWindowID, + const std::string& aUrl, + uint64_t aEmbedderInnerWindowID); + +// Unregister page with the profiler. +// +// Take a Inner Window ID and unregister the page entry that has the same ID. +MFBT_API void profiler_unregister_page(uint64_t aRegisteredInnerWindowID); + +// Remove all registered and unregistered pages in the profiler. +void profiler_clear_all_pages(); + +class BaseProfilerCount; +MFBT_API void profiler_add_sampled_counter(BaseProfilerCount* aCounter); +MFBT_API void profiler_remove_sampled_counter(BaseProfilerCount* aCounter); + +// Register and unregister a thread within a scope. +# define AUTO_BASE_PROFILER_REGISTER_THREAD(name) \ + ::mozilla::baseprofiler::AutoProfilerRegisterThread PROFILER_RAII(name) + +// Pause and resume the profiler. No-ops if the profiler is inactive. While +// paused the profile will not take any samples and will not record any data +// into its buffers. The profiler remains fully initialized in this state. +// This feature will keep JavaScript profiling enabled, thus allowing toggling +// the profiler without invalidating the JIT. +MFBT_API void profiler_pause(); +MFBT_API void profiler_resume(); + +// Only pause and resume the periodic sampling loop, including stack sampling, +// counters, and profiling overheads. +MFBT_API void profiler_pause_sampling(); +MFBT_API void profiler_resume_sampling(); + +// These functions tell the profiler that a thread went to sleep so that we can +// avoid sampling it while it's sleeping. Calling profiler_thread_sleep() +// twice without an intervening profiler_thread_wake() is an error. All three +// functions operate the same whether the profiler is active or inactive. +MFBT_API void profiler_thread_sleep(); +MFBT_API void profiler_thread_wake(); + +// Mark a thread as asleep/awake within a scope. +# define AUTO_BASE_PROFILER_THREAD_SLEEP \ + ::mozilla::baseprofiler::AutoProfilerThreadSleep PROFILER_RAII +# define AUTO_BASE_PROFILER_THREAD_WAKE \ + ::mozilla::baseprofiler::AutoProfilerThreadWake PROFILER_RAII + +//--------------------------------------------------------------------------- +// Get information from the profiler +//--------------------------------------------------------------------------- + +// Get the params used to start the profiler. Returns 0 and an empty vector +// (via outparams) if the profile is inactive. It's possible that the features +// returned may be slightly different to those requested due to required +// adjustments. +MFBT_API void profiler_get_start_params( + int* aEntrySize, Maybe<double>* aDuration, double* aInterval, + uint32_t* aFeatures, Vector<const char*, 0, MallocAllocPolicy>* aFilters); + +// The number of milliseconds since the process started. Operates the same +// whether the profiler is active or inactive. +MFBT_API double profiler_time(); + +// An object of this class is passed to profiler_suspend_and_sample_thread(). +// For each stack frame, one of the Collect methods will be called. +class ProfilerStackCollector { + public: + // Some collectors need to worry about possibly overwriting previous + // generations of data. If that's not an issue, this can return Nothing, + // which is the default behaviour. + virtual Maybe<uint64_t> SamplePositionInBuffer() { return Nothing(); } + virtual Maybe<uint64_t> BufferRangeStart() { return Nothing(); } + + // This method will be called once if the thread being suspended is the main + // thread. Default behaviour is to do nothing. + virtual void SetIsMainThread() {} + + // WARNING: The target thread is suspended when the Collect methods are + // called. Do not try to allocate or acquire any locks, or you could + // deadlock. The target thread will have resumed by the time this function + // returns. + + virtual void CollectNativeLeafAddr(void* aAddr) = 0; + + virtual void CollectProfilingStackFrame( + const ProfilingStackFrame& aFrame) = 0; +}; + +// This method suspends the thread identified by aThreadId, samples its +// profiling stack, JS stack, and (optionally) native stack, passing the +// collected frames into aCollector. aFeatures dictates which compiler features +// are used. |Leaf| is the only relevant one. +// Use `aThreadId`=0 to sample the current thread. +MFBT_API void profiler_suspend_and_sample_thread( + int aThreadId, uint32_t aFeatures, ProfilerStackCollector& aCollector, + bool aSampleNative = true); + +struct ProfilerBacktraceDestructor { + MFBT_API void operator()(ProfilerBacktrace*); +}; + +using UniqueProfilerBacktrace = + UniquePtr<ProfilerBacktrace, ProfilerBacktraceDestructor>; + +// Immediately capture the current thread's call stack, store it in the provided +// buffer (usually to avoid allocations if you can construct the buffer on the +// stack). Returns false if unsuccessful, if the profiler is inactive, or if +// aCaptureOptions is NoStack. +MFBT_API bool profiler_capture_backtrace_into( + ProfileChunkedBuffer& aChunkedBuffer, StackCaptureOptions aCaptureOptions); + +// Immediately capture the current thread's call stack, and return it in a +// ProfileChunkedBuffer (usually for later use in MarkerStack::TakeBacktrace()). +// May be null if unsuccessful, or if the profiler is inactive. +MFBT_API UniquePtr<ProfileChunkedBuffer> profiler_capture_backtrace(); + +// Immediately capture the current thread's call stack, and return it in a +// ProfilerBacktrace (usually for later use in marker function that take a +// ProfilerBacktrace). May be null if unsuccessful, or if the profiler is +// inactive. +MFBT_API UniqueProfilerBacktrace profiler_get_backtrace(); + +struct ProfilerStats { + unsigned n = 0; + double sum = 0; + double min = std::numeric_limits<double>::max(); + double max = 0; + void Count(double v) { + ++n; + sum += v; + if (v < min) { + min = v; + } + if (v > max) { + max = v; + } + } +}; + +struct ProfilerBufferInfo { + // Index of the oldest entry. + uint64_t mRangeStart; + // Index of the newest entry. + uint64_t mRangeEnd; + // Buffer capacity in number of 8-byte entries. + uint32_t mEntryCount; + // Sampling stats: Interval (us) between successive samplings. + ProfilerStats mIntervalsUs; + // Sampling stats: Total duration (us) of each sampling. (Split detail below.) + ProfilerStats mOverheadsUs; + // Sampling stats: Time (us) to acquire the lock before sampling. + ProfilerStats mLockingsUs; + // Sampling stats: Time (us) to discard expired data. + ProfilerStats mCleaningsUs; + // Sampling stats: Time (us) to collect counter data. + ProfilerStats mCountersUs; + // Sampling stats: Time (us) to sample thread stacks. + ProfilerStats mThreadsUs; +}; + +// Get information about the current buffer status. +// Returns Nothing() if the profiler is inactive. +// +// This information may be useful to a user-interface displaying the current +// status of the profiler, allowing the user to get a sense for how fast the +// buffer is being written to, and how much data is visible. +MFBT_API Maybe<ProfilerBufferInfo> profiler_get_buffer_info(); + +} // namespace baseprofiler +} // namespace mozilla + +namespace mozilla { +namespace baseprofiler { + +//--------------------------------------------------------------------------- +// Put profiling data into the profiler (markers) +//--------------------------------------------------------------------------- + +MFBT_API void profiler_add_js_marker(const char* aMarkerName, + const char* aMarkerText); + +//--------------------------------------------------------------------------- +// Output profiles +//--------------------------------------------------------------------------- + +// Set a user-friendly process name, used in JSON stream. +MFBT_API void profiler_set_process_name(const std::string& aProcessName, + const std::string* aETLDplus1); + +// Get the profile encoded as a JSON string. A no-op (returning nullptr) if the +// profiler is inactive. +// If aIsShuttingDown is true, the current time is included as the process +// shutdown time in the JSON's "meta" object. +MFBT_API UniquePtr<char[]> profiler_get_profile(double aSinceTime = 0, + bool aIsShuttingDown = false, + bool aOnlyThreads = false); + +// Write the profile for this process (excluding subprocesses) into aWriter. +// Returns false if the profiler is inactive. +MFBT_API bool profiler_stream_json_for_this_process( + SpliceableJSONWriter& aWriter, double aSinceTime = 0, + bool aIsShuttingDown = false, bool aOnlyThreads = false); + +// Get the profile and write it into a file. A no-op if the profile is +// inactive. +// Prefixed with "base" to avoid clashing with Gecko Profiler's extern "C" +// profiler_save_profile_to_file when called from debugger. +MFBT_API void baseprofiler_save_profile_to_file(const char* aFilename); + +//--------------------------------------------------------------------------- +// RAII classes +//--------------------------------------------------------------------------- + +class MOZ_RAII AutoProfilerInit { + public: + explicit AutoProfilerInit() { profiler_init(this); } + + ~AutoProfilerInit() { profiler_shutdown(); } + + private: +}; + +// Convenience class to register and unregister a thread with the profiler. +// Needs to be the first object on the stack of the thread. +class MOZ_RAII AutoProfilerRegisterThread final { + public: + explicit AutoProfilerRegisterThread(const char* aName) { + profiler_register_thread(aName, this); + } + + ~AutoProfilerRegisterThread() { profiler_unregister_thread(); } + + private: + AutoProfilerRegisterThread(const AutoProfilerRegisterThread&) = delete; + AutoProfilerRegisterThread& operator=(const AutoProfilerRegisterThread&) = + delete; +}; + +class MOZ_RAII AutoProfilerThreadSleep { + public: + explicit AutoProfilerThreadSleep() { profiler_thread_sleep(); } + + ~AutoProfilerThreadSleep() { profiler_thread_wake(); } + + private: +}; + +// Temporarily wake up the profiling of a thread while servicing events such as +// Asynchronous Procedure Calls (APCs). +class MOZ_RAII AutoProfilerThreadWake { + public: + explicit AutoProfilerThreadWake() + : mIssuedWake(profiler_thread_is_sleeping()) { + if (mIssuedWake) { + profiler_thread_wake(); + } + } + + ~AutoProfilerThreadWake() { + if (mIssuedWake) { + MOZ_ASSERT(!profiler_thread_is_sleeping()); + profiler_thread_sleep(); + } + } + + private: + bool mIssuedWake; +}; + +// Get the MOZ_PROFILER_STARTUP* environment variables that should be +// supplied to a child process that is about to be launched, in order +// to make that child process start with the same profiler settings as +// in the current process. The given function is invoked once for +// each variable to be set. +MFBT_API void GetProfilerEnvVarsForChildProcess( + std::function<void(const char* key, const char* value)>&& aSetEnv); + +} // namespace baseprofiler +} // namespace mozilla + +#endif // !MOZ_GECKO_PROFILER + +#endif // BaseProfiler_h diff --git a/mozglue/baseprofiler/public/BaseProfilerCounts.h b/mozglue/baseprofiler/public/BaseProfilerCounts.h new file mode 100644 index 0000000000..fbcc713744 --- /dev/null +++ b/mozglue/baseprofiler/public/BaseProfilerCounts.h @@ -0,0 +1,280 @@ +/* -*- Mode: C++; tab-width: 2; 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/. */ + +#ifndef BaseProfilerCounts_h +#define BaseProfilerCounts_h + +#ifndef MOZ_GECKO_PROFILER + +# define BASE_PROFILER_DEFINE_COUNT_TOTAL(label, category, description) +# define BASE_PROFILER_DEFINE_COUNT(label, category, description) +# define BASE_PROFILER_DEFINE_STATIC_COUNT_TOTAL(label, category, description) +# define AUTO_BASE_PROFILER_COUNT_TOTAL(label, count) +# define AUTO_BASE_PROFILER_COUNT(label) +# define AUTO_BASE_PROFILER_STATIC_COUNT(label, count) +# define AUTO_BASE_PROFILER_FORCE_ALLOCATION(label) + +#else + +# include "mozilla/Atomics.h" + +namespace mozilla { +namespace baseprofiler { + +class BaseProfilerCount; +MFBT_API void profiler_add_sampled_counter(BaseProfilerCount* aCounter); +MFBT_API void profiler_remove_sampled_counter(BaseProfilerCount* aCounter); + +typedef Atomic<int64_t, MemoryOrdering::Relaxed> ProfilerAtomicSigned; +typedef Atomic<uint64_t, MemoryOrdering::Relaxed> ProfilerAtomicUnsigned; + +// Counter support +// There are two types of counters: +// 1) a simple counter which can be added to or subtracted from. This could +// track the number of objects of a type, the number of calls to something +// (reflow, JIT, etc). +// 2) a combined counter which has the above, plus a number-of-calls counter +// that is incremented by 1 for each call to modify the count. This provides +// an optional source for a 'heatmap' of access. This can be used (for +// example) to track the amount of memory allocated, and provide a heatmap of +// memory operations (allocs/frees). +// +// Counters are sampled by the profiler once per sample-period. At this time, +// all counters are global to the process. In the future, there might be more +// versions with per-thread or other discriminators. +// +// Typical usage: +// There are two ways to use counters: With heap-created counter objects, +// or using macros. Note: the macros use statics, and will be slightly +// faster/smaller, and you need to care about creating them before using +// them. They're similar to the use-pattern for the other AUTO_PROFILER* +// macros, but they do need the PROFILER_DEFINE* to be use to instantiate +// the statics. +// +// PROFILER_DEFINE_COUNT(mything, "JIT", "Some JIT byte count") +// ... +// void foo() { ... AUTO_PROFILER_COUNT(mything, number_of_bytes_used); ... } +// +// or (to also get a heatmap) +// +// PROFILER_DEFINE_COUNT_TOTAL(mything, "JIT", "Some JIT byte count") +// ... +// void foo() { +// ... +// AUTO_PROFILER_COUNT_TOTAL(mything, number_of_bytes_generated); +// ... +// } +// +// To use without statics/macros: +// +// UniquePtr<ProfilerCounter> myCounter; +// ... +// myCounter = +// MakeUnique<ProfilerCounter>("mything", "JIT", "Some JIT byte count")); +// ... +// void foo() { ... myCounter->Add(number_of_bytes_generated0; ... } + +class BaseProfilerCount { + public: + BaseProfilerCount(const char* aLabel, ProfilerAtomicSigned* aCounter, + ProfilerAtomicUnsigned* aNumber, const char* aCategory, + const char* aDescription) + : mLabel(aLabel), + mCategory(aCategory), + mDescription(aDescription), + mCounter(aCounter), + mNumber(aNumber) { +# define COUNTER_CANARY 0xDEADBEEF +# ifdef DEBUG + mCanary = COUNTER_CANARY; + mPrevNumber = 0; +# endif + // Can't call profiler_* here since this may be non-xul-library + } +# ifdef DEBUG + ~BaseProfilerCount() { mCanary = 0; } +# endif + + void Sample(int64_t& aCounter, uint64_t& aNumber) { + MOZ_ASSERT(mCanary == COUNTER_CANARY); + + aCounter = *mCounter; + aNumber = mNumber ? *mNumber : 0; +# ifdef DEBUG + MOZ_ASSERT(aNumber >= mPrevNumber); + mPrevNumber = aNumber; +# endif + } + + // We don't define ++ and Add() here, since the static defines directly + // increment the atomic counters, and the subclasses implement ++ and + // Add() directly. + + // These typically are static strings (for example if you use the macros + // below) + const char* mLabel; + const char* mCategory; + const char* mDescription; + // We're ok with these being un-ordered in race conditions. These are + // pointers because we want to be able to use statics and increment them + // directly. Otherwise we could just have them inline, and not need the + // constructor args. + // These can be static globals (using the macros below), though they + // don't have to be - their lifetime must be longer than the use of them + // by the profiler (see profiler_add/remove_sampled_counter()). If you're + // using a lot of these, they probably should be allocated at runtime (see + // class ProfilerCountOnly below). + ProfilerAtomicSigned* mCounter; + ProfilerAtomicUnsigned* mNumber; // may be null + +# ifdef DEBUG + uint32_t mCanary; + uint64_t mPrevNumber; // value of number from the last Sample() +# endif +}; + +// Designed to be allocated dynamically, and simply incremented with obj++ +// or obj->Add(n) +class ProfilerCounter final : public BaseProfilerCount { + public: + ProfilerCounter(const char* aLabel, const char* aCategory, + const char* aDescription) + : BaseProfilerCount(aLabel, &mCounter, nullptr, aCategory, aDescription) { + // Assume we're in libxul + profiler_add_sampled_counter(this); + } + + virtual ~ProfilerCounter() { profiler_remove_sampled_counter(this); } + + BaseProfilerCount& operator++() { + Add(1); + return *this; + } + + void Add(int64_t aNumber) { mCounter += aNumber; } + + ProfilerAtomicSigned mCounter; +}; + +// Also keeps a heatmap (number of calls to ++/Add()) +class ProfilerCounterTotal final : public BaseProfilerCount { + public: + ProfilerCounterTotal(const char* aLabel, const char* aCategory, + const char* aDescription) + : BaseProfilerCount(aLabel, &mCounter, &mNumber, aCategory, + aDescription) { + // Assume we're in libxul + profiler_add_sampled_counter(this); + } + + virtual ~ProfilerCounterTotal() { profiler_remove_sampled_counter(this); } + + BaseProfilerCount& operator++() { + Add(1); + return *this; + } + + void Add(int64_t aNumber) { + mCounter += aNumber; + mNumber++; + } + + ProfilerAtomicSigned mCounter; + ProfilerAtomicUnsigned mNumber; +}; + +// Defines a counter that is sampled on each profiler tick, with a running +// count (signed), and number-of-instances. Note that because these are two +// independent Atomics, there is a possiblity that count will not include +// the last call, but number of uses will. I think this is not worth +// worrying about +# define BASE_PROFILER_DEFINE_COUNT_TOTAL(label, category, description) \ + ProfilerAtomicSigned profiler_count_##label(0); \ + ProfilerAtomicUnsigned profiler_number_##label(0); \ + const char profiler_category_##label[] = category; \ + const char profiler_description_##label[] = description; \ + UniquePtr<::mozilla::baseprofiler::BaseProfilerCount> AutoCount_##label; + +// This counts, but doesn't keep track of the number of calls to +// AUTO_PROFILER_COUNT() +# define BASE_PROFILER_DEFINE_COUNT(label, category, description) \ + ProfilerAtomicSigned profiler_count_##label(0); \ + const char profiler_category_##label[] = category; \ + const char profiler_description_##label[] = description; \ + UniquePtr<::mozilla::baseprofiler::BaseProfilerCount> AutoCount_##label; + +// This will create a static initializer if used, but avoids a possible +// allocation. +# define BASE_PROFILER_DEFINE_STATIC_COUNT_TOTAL(label, category, \ + description) \ + ProfilerAtomicSigned profiler_count_##label(0); \ + ProfilerAtomicUnsigned profiler_number_##label(0); \ + ::mozilla::baseprofiler::BaseProfilerCount AutoCount_##label( \ + #label, &profiler_count_##label, &profiler_number_##label, category, \ + description); + +// If we didn't care about static initializers, we could avoid the need for +// a ptr to the BaseProfilerCount object. + +// XXX It would be better to do this without the if() and without the +// theoretical race to set the UniquePtr (i.e. possible leak). +# define AUTO_BASE_PROFILER_COUNT_TOTAL(label, count) \ + do { \ + profiler_number_##label++; /* do this first*/ \ + profiler_count_##label += count; \ + if (!AutoCount_##label) { \ + /* Ignore that we could call this twice in theory, and that we leak \ + * them \ + */ \ + AutoCount_##label.reset(new BaseProfilerCount( \ + #label, &profiler_count_##label, &profiler_number_##label, \ + profiler_category_##label, profiler_description_##label)); \ + ::mozilla::baseprofiler::profiler_add_sampled_counter( \ + AutoCount_##label.get()); \ + } \ + } while (0) + +# define AUTO_BASE_PROFILER_COUNT(label, count) \ + do { \ + profiler_count_##label += count; /* do this first*/ \ + if (!AutoCount_##label) { \ + /* Ignore that we could call this twice in theory, and that we leak \ + * them \ + */ \ + AutoCount_##label.reset(new BaseProfilerCount( \ + #label, nullptr, &profiler_number_##label, \ + profiler_category_##label, profiler_description_##label)); \ + ::mozilla::baseprofiler::profiler_add_sampled_counter( \ + AutoCount_##label.get()); \ + } \ + } while (0) + +# define AUTO_BASE_PROFILER_STATIC_COUNT(label, count) \ + do { \ + profiler_number_##label++; /* do this first*/ \ + profiler_count_##label += count; \ + } while (0) + +// if we need to force the allocation +# define AUTO_BASE_PROFILER_FORCE_ALLOCATION(label) \ + do { \ + if (!AutoCount_##label) { \ + /* Ignore that we could call this twice in theory, and that we leak \ + * them \ + */ \ + AutoCount_##label.reset( \ + new ::mozilla::baseprofiler::BaseProfilerCount( \ + #label, &profiler_count_##label, &profiler_number_##label, \ + profiler_category_##label, profiler_description_##label)); \ + } \ + } while (0) + +} // namespace baseprofiler +} // namespace mozilla + +#endif // !MOZ_GECKO_PROFILER + +#endif // BaseProfilerCounts_h diff --git a/mozglue/baseprofiler/public/BaseProfilerDetail.h b/mozglue/baseprofiler/public/BaseProfilerDetail.h new file mode 100644 index 0000000000..6ecf6e117b --- /dev/null +++ b/mozglue/baseprofiler/public/BaseProfilerDetail.h @@ -0,0 +1,285 @@ +/* -*- Mode: C++; tab-width: 2; 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/. */ + +// Internal Base Profiler utilities. + +#ifndef BaseProfilerDetail_h +#define BaseProfilerDetail_h + +#include "mozilla/Atomics.h" +#include "mozilla/Attributes.h" +#include "mozilla/Maybe.h" +#include "mozilla/PlatformMutex.h" +#include "mozilla/PlatformRWLock.h" +#include "mozilla/BaseProfilerUtils.h" + +namespace mozilla { +namespace baseprofiler { + +namespace detail { + +// Thin shell around mozglue PlatformMutex, for Base Profiler internal use. +class MOZ_CAPABILITY("mutex") BaseProfilerMutex + : private ::mozilla::detail::MutexImpl { + public: + BaseProfilerMutex() : ::mozilla::detail::MutexImpl() {} + explicit BaseProfilerMutex(const char* aName) + : ::mozilla::detail::MutexImpl(), mName(aName) {} + + BaseProfilerMutex(const BaseProfilerMutex&) = delete; + BaseProfilerMutex& operator=(const BaseProfilerMutex&) = delete; + BaseProfilerMutex(BaseProfilerMutex&&) = delete; + BaseProfilerMutex& operator=(BaseProfilerMutex&&) = delete; + +#ifdef DEBUG + ~BaseProfilerMutex() { + MOZ_ASSERT(!BaseProfilerThreadId::FromNumber(mOwningThreadId).IsSpecified(), + "BaseProfilerMutex should have been unlocked when destroyed"); + } +#endif // DEBUG + + [[nodiscard]] bool IsLockedOnCurrentThread() const { + return BaseProfilerThreadId::FromNumber(mOwningThreadId) == + baseprofiler::profiler_current_thread_id(); + } + + void AssertCurrentThreadOwns() const MOZ_ASSERT_CAPABILITY(this) { + MOZ_ASSERT(IsLockedOnCurrentThread()); + } + + void Lock() MOZ_CAPABILITY_ACQUIRE() { + const BaseProfilerThreadId tid = baseprofiler::profiler_current_thread_id(); + MOZ_ASSERT(tid.IsSpecified()); + MOZ_ASSERT(!IsLockedOnCurrentThread(), "Recursive locking"); + ::mozilla::detail::MutexImpl::lock(); + MOZ_ASSERT(!BaseProfilerThreadId::FromNumber(mOwningThreadId).IsSpecified(), + "Not unlocked properly"); + mOwningThreadId = tid.ToNumber(); + } + + [[nodiscard]] bool TryLock() MOZ_TRY_ACQUIRE(true) { + const BaseProfilerThreadId tid = baseprofiler::profiler_current_thread_id(); + MOZ_ASSERT(tid.IsSpecified()); + MOZ_ASSERT(!IsLockedOnCurrentThread(), "Recursive locking"); + if (!::mozilla::detail::MutexImpl::tryLock()) { + // Failed to lock, nothing more to do. + return false; + } + MOZ_ASSERT(!BaseProfilerThreadId::FromNumber(mOwningThreadId).IsSpecified(), + "Not unlocked properly"); + mOwningThreadId = tid.ToNumber(); + return true; + } + + void Unlock() MOZ_CAPABILITY_RELEASE() { + MOZ_ASSERT(IsLockedOnCurrentThread(), "Unlocking when not locked here"); + // We're still holding the mutex here, so it's safe to just reset + // `mOwningThreadId`. + mOwningThreadId = BaseProfilerThreadId{}.ToNumber(); + ::mozilla::detail::MutexImpl::unlock(); + } + + const char* GetName() const { return mName; } + + private: + // Thread currently owning the lock, or 0. + // Atomic because it may be read at any time independent of the mutex. + // Relaxed because threads only need to know if they own it already, so: + // - If it's their id, only *they* wrote that value with a locked mutex. + // - If it's different from their thread id it doesn't matter what other + // number it is (0 or another id) and that it can change again at any time. + Atomic<typename BaseProfilerThreadId::NumberType, MemoryOrdering::Relaxed> + mOwningThreadId; + + const char* mName = nullptr; +}; + +// RAII class to lock a mutex. +class MOZ_RAII BaseProfilerAutoLock { + public: + explicit BaseProfilerAutoLock(BaseProfilerMutex& aMutex) : mMutex(aMutex) { + mMutex.Lock(); + } + + BaseProfilerAutoLock(const BaseProfilerAutoLock&) = delete; + BaseProfilerAutoLock& operator=(const BaseProfilerAutoLock&) = delete; + BaseProfilerAutoLock(BaseProfilerAutoLock&&) = delete; + BaseProfilerAutoLock& operator=(BaseProfilerAutoLock&&) = delete; + + ~BaseProfilerAutoLock() { mMutex.Unlock(); } + + private: + BaseProfilerMutex& mMutex; +}; + +// Thin shell around mozglue PlatformMutex, for Base Profiler internal use. +// Actual mutex may be disabled at construction time. +class BaseProfilerMaybeMutex : private ::mozilla::detail::MutexImpl { + public: + explicit BaseProfilerMaybeMutex(bool aActivate) { + if (aActivate) { + mMaybeMutex.emplace(); + } + } + + BaseProfilerMaybeMutex(const BaseProfilerMaybeMutex&) = delete; + BaseProfilerMaybeMutex& operator=(const BaseProfilerMaybeMutex&) = delete; + BaseProfilerMaybeMutex(BaseProfilerMaybeMutex&&) = delete; + BaseProfilerMaybeMutex& operator=(BaseProfilerMaybeMutex&&) = delete; + + ~BaseProfilerMaybeMutex() = default; + + bool IsActivated() const { return mMaybeMutex.isSome(); } + + [[nodiscard]] bool IsActivatedAndLockedOnCurrentThread() const { + if (!IsActivated()) { + // Not activated, so we can never be locked. + return false; + } + return mMaybeMutex->IsLockedOnCurrentThread(); + } + + void AssertCurrentThreadOwns() const { +#ifdef DEBUG + if (IsActivated()) { + mMaybeMutex->AssertCurrentThreadOwns(); + } +#endif // DEBUG + } + + MOZ_PUSH_IGNORE_THREAD_SAFETY + void Lock() { + if (IsActivated()) { + mMaybeMutex->Lock(); + } + } + + void Unlock() { + if (IsActivated()) { + mMaybeMutex->Unlock(); + } + } + MOZ_POP_THREAD_SAFETY + + private: + Maybe<BaseProfilerMutex> mMaybeMutex; +}; + +// RAII class to lock a mutex. +class MOZ_RAII BaseProfilerMaybeAutoLock { + public: + explicit BaseProfilerMaybeAutoLock(BaseProfilerMaybeMutex& aMaybeMutex) + : mMaybeMutex(aMaybeMutex) { + mMaybeMutex.Lock(); + } + + BaseProfilerMaybeAutoLock(const BaseProfilerMaybeAutoLock&) = delete; + BaseProfilerMaybeAutoLock& operator=(const BaseProfilerMaybeAutoLock&) = + delete; + BaseProfilerMaybeAutoLock(BaseProfilerMaybeAutoLock&&) = delete; + BaseProfilerMaybeAutoLock& operator=(BaseProfilerMaybeAutoLock&&) = delete; + + ~BaseProfilerMaybeAutoLock() { mMaybeMutex.Unlock(); } + + private: + BaseProfilerMaybeMutex& mMaybeMutex; +}; + +class BaseProfilerSharedMutex : public ::mozilla::detail::RWLockImpl { + public: +#ifdef DEBUG + ~BaseProfilerSharedMutex() { + MOZ_ASSERT(!BaseProfilerThreadId::FromNumber(mOwningThreadId).IsSpecified(), + "BaseProfilerMutex should have been unlocked when destroyed"); + } +#endif // DEBUG + + [[nodiscard]] bool IsLockedExclusiveOnCurrentThread() const { + return BaseProfilerThreadId::FromNumber(mOwningThreadId) == + baseprofiler::profiler_current_thread_id(); + } + + void LockExclusive() { + const BaseProfilerThreadId tid = baseprofiler::profiler_current_thread_id(); + MOZ_ASSERT(tid.IsSpecified()); + MOZ_ASSERT(!IsLockedExclusiveOnCurrentThread(), "Recursive locking"); + ::mozilla::detail::RWLockImpl::writeLock(); + MOZ_ASSERT(!BaseProfilerThreadId::FromNumber(mOwningThreadId).IsSpecified(), + "Not unlocked properly"); + mOwningThreadId = tid.ToNumber(); + } + + void UnlockExclusive() { + MOZ_ASSERT(IsLockedExclusiveOnCurrentThread(), + "Unlocking when not locked here"); + // We're still holding the mutex here, so it's safe to just reset + // `mOwningThreadId`. + mOwningThreadId = BaseProfilerThreadId{}.ToNumber(); + writeUnlock(); + } + + void LockShared() { readLock(); } + + void UnlockShared() { readUnlock(); } + + private: + // Thread currently owning the exclusive lock, or 0. + // Atomic because it may be read at any time independent of the mutex. + // Relaxed because threads only need to know if they own it already, so: + // - If it's their id, only *they* wrote that value with a locked mutex. + // - If it's different from their thread id it doesn't matter what other + // number it is (0 or another id) and that it can change again at any time. + Atomic<typename BaseProfilerThreadId::NumberType, MemoryOrdering::Relaxed> + mOwningThreadId; +}; + +// RAII class to lock a shared mutex exclusively. +class MOZ_RAII BaseProfilerAutoLockExclusive { + public: + explicit BaseProfilerAutoLockExclusive(BaseProfilerSharedMutex& aSharedMutex) + : mSharedMutex(aSharedMutex) { + mSharedMutex.LockExclusive(); + } + + BaseProfilerAutoLockExclusive(const BaseProfilerAutoLockExclusive&) = delete; + BaseProfilerAutoLockExclusive& operator=( + const BaseProfilerAutoLockExclusive&) = delete; + BaseProfilerAutoLockExclusive(BaseProfilerAutoLockExclusive&&) = delete; + BaseProfilerAutoLockExclusive& operator=(BaseProfilerAutoLockExclusive&&) = + delete; + + ~BaseProfilerAutoLockExclusive() { mSharedMutex.UnlockExclusive(); } + + private: + BaseProfilerSharedMutex& mSharedMutex; +}; + +// RAII class to lock a shared mutex non-exclusively, other +// BaseProfilerAutoLockShared's may happen in other threads. +class MOZ_RAII BaseProfilerAutoLockShared { + public: + explicit BaseProfilerAutoLockShared(BaseProfilerSharedMutex& aSharedMutex) + : mSharedMutex(aSharedMutex) { + mSharedMutex.LockShared(); + } + + BaseProfilerAutoLockShared(const BaseProfilerAutoLockShared&) = delete; + BaseProfilerAutoLockShared& operator=(const BaseProfilerAutoLockShared&) = + delete; + BaseProfilerAutoLockShared(BaseProfilerAutoLockShared&&) = delete; + BaseProfilerAutoLockShared& operator=(BaseProfilerAutoLockShared&&) = delete; + + ~BaseProfilerAutoLockShared() { mSharedMutex.UnlockShared(); } + + private: + BaseProfilerSharedMutex& mSharedMutex; +}; + +} // namespace detail +} // namespace baseprofiler +} // namespace mozilla + +#endif // BaseProfilerDetail_h diff --git a/mozglue/baseprofiler/public/BaseProfilerLabels.h b/mozglue/baseprofiler/public/BaseProfilerLabels.h new file mode 100644 index 0000000000..8da596e3ab --- /dev/null +++ b/mozglue/baseprofiler/public/BaseProfilerLabels.h @@ -0,0 +1,178 @@ +/* -*- Mode: C++; tab-width: 2; 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/. */ + +// This header contains all definitions related to Base Profiler labels (outside +// of XUL). +// It is safe to include unconditionally, and only defines empty macros if +// MOZ_GECKO_PROFILER is not set. + +#ifndef BaseProfilerLabels_h +#define BaseProfilerLabels_h + +#ifndef MOZ_GECKO_PROFILER + +# define AUTO_BASE_PROFILER_LABEL(label, categoryPair) +# define AUTO_BASE_PROFILER_LABEL_CATEGORY_PAIR(categoryPair) +# define AUTO_BASE_PROFILER_LABEL_DYNAMIC_CSTR(label, categoryPair, cStr) +# define AUTO_BASE_PROFILER_LABEL_DYNAMIC_STRING(label, categoryPair, str) +# define AUTO_BASE_PROFILER_LABEL_FAST(label, categoryPair, ctx) +# define AUTO_BASE_PROFILER_LABEL_DYNAMIC_FAST(label, dynamicString, \ + categoryPair, ctx, flags) + +#else // !MOZ_GECKO_PROFILER + +# include "BaseProfilingStack.h" + +# include "mozilla/Attributes.h" +# include "mozilla/Maybe.h" +# include "mozilla/BaseProfilerRAIIMacro.h" +# include "mozilla/BaseProfilerState.h" +# include "mozilla/ThreadLocal.h" + +# include <stdint.h> +# include <string> + +namespace mozilla::baseprofiler { + +// Insert an RAII object in this scope to enter a label stack frame. Any +// samples collected in this scope will contain this label in their stack. +// The label argument must be a static C string. It is usually of the +// form "ClassName::FunctionName". (Ideally we'd use the compiler to provide +// that for us, but __func__ gives us the function name without the class +// name.) If the label applies to only part of a function, you can qualify it +// like this: "ClassName::FunctionName:PartName". +// +// Use AUTO_BASE_PROFILER_LABEL_DYNAMIC_* if you want to add additional / +// dynamic information to the label stack frame. +# define AUTO_BASE_PROFILER_LABEL(label, categoryPair) \ + ::mozilla::baseprofiler::AutoProfilerLabel PROFILER_RAII( \ + label, nullptr, \ + ::mozilla::baseprofiler::ProfilingCategoryPair::categoryPair) + +// Similar to AUTO_BASE_PROFILER_LABEL, but with only one argument: the category +// pair. The label string is taken from the category pair. This is convenient +// for labels like +// AUTO_BASE_PROFILER_LABEL_CATEGORY_PAIR(GRAPHICS_LayerBuilding) which would +// otherwise just repeat the string. +# define AUTO_BASE_PROFILER_LABEL_CATEGORY_PAIR(categoryPair) \ + ::mozilla::baseprofiler::AutoProfilerLabel PROFILER_RAII( \ + "", nullptr, \ + ::mozilla::baseprofiler::ProfilingCategoryPair::categoryPair, \ + uint32_t(::mozilla::baseprofiler::ProfilingStackFrame::Flags:: \ + LABEL_DETERMINED_BY_CATEGORY_PAIR)) + +// Similar to AUTO_BASE_PROFILER_LABEL, but with an additional string. The +// inserted RAII object stores the cStr pointer in a field; it does not copy the +// string. +// +// WARNING: This means that the string you pass to this macro needs to live at +// least until the end of the current scope. Be careful using this macro with +// ns[C]String; the other AUTO_BASE_PROFILER_LABEL_DYNAMIC_* macros below are +// preferred because they avoid this problem. +// +// If the profiler samples the current thread and walks the label stack while +// this RAII object is on the stack, it will copy the supplied string into the +// profile buffer. So there's one string copy operation, and it happens at +// sample time. +// +// Compare this to the plain AUTO_BASE_PROFILER_LABEL macro, which only accepts +// literal strings: When the label stack frames generated by +// AUTO_BASE_PROFILER_LABEL are sampled, no string copy needs to be made because +// the profile buffer can just store the raw pointers to the literal strings. +// Consequently, AUTO_BASE_PROFILER_LABEL frames take up considerably less space +// in the profile buffer than AUTO_BASE_PROFILER_LABEL_DYNAMIC_* frames. +# define AUTO_BASE_PROFILER_LABEL_DYNAMIC_CSTR(label, categoryPair, cStr) \ + ::mozilla::baseprofiler::AutoProfilerLabel PROFILER_RAII( \ + label, cStr, \ + ::mozilla::baseprofiler::ProfilingCategoryPair::categoryPair) + +// Similar to AUTO_BASE_PROFILER_LABEL_DYNAMIC_CSTR, but takes an std::string. +// +// Note: The use of the Maybe<>s ensures the scopes for the dynamic string and +// the AutoProfilerLabel are appropriate, while also not incurring the runtime +// cost of the string assignment unless the profiler is active. Therefore, +// unlike AUTO_BASE_PROFILER_LABEL and AUTO_BASE_PROFILER_LABEL_DYNAMIC_CSTR, +// this macro doesn't push/pop a label when the profiler is inactive. +# define AUTO_BASE_PROFILER_LABEL_DYNAMIC_STRING(label, categoryPair, str) \ + Maybe<std::string> autoStr; \ + Maybe<::mozilla::baseprofiler::AutoProfilerLabel> raiiObjectString; \ + if (::mozilla::baseprofiler::profiler_is_active()) { \ + autoStr.emplace(str); \ + raiiObjectString.emplace( \ + label, autoStr->c_str(), \ + ::mozilla::baseprofiler::ProfilingCategoryPair::categoryPair); \ + } + +// Similar to AUTO_BASE_PROFILER_LABEL, but accepting a JSContext* parameter, +// and a no-op if the profiler is disabled. Used to annotate functions for which +// overhead in the range of nanoseconds is noticeable. It avoids overhead from +// the TLS lookup because it can get the ProfilingStack from the JS context, and +// avoids almost all overhead in the case where the profiler is disabled. +# define AUTO_BASE_PROFILER_LABEL_FAST(label, categoryPair, ctx) \ + ::mozilla::baseprofiler::AutoProfilerLabel PROFILER_RAII( \ + ctx, label, nullptr, \ + ::mozilla::baseprofiler::ProfilingCategoryPair::categoryPair) + +// Similar to AUTO_BASE_PROFILER_LABEL_FAST, but also takes an extra string and +// an additional set of flags. The flags parameter should carry values from the +// ProfilingStackFrame::Flags enum. +# define AUTO_BASE_PROFILER_LABEL_DYNAMIC_FAST(label, dynamicString, \ + categoryPair, ctx, flags) \ + ::mozilla::baseprofiler::AutoProfilerLabel PROFILER_RAII( \ + ctx, label, dynamicString, \ + ::mozilla::baseprofiler::ProfilingCategoryPair::categoryPair, flags) + +// This class creates a non-owning ProfilingStack reference. Objects of this +// class are stack-allocated, and so exist within a thread, and are thus bounded +// by the lifetime of the thread, which ensures that the references held can't +// be used after the ProfilingStack is destroyed. +class MOZ_RAII AutoProfilerLabel { + public: + // This is the AUTO_BASE_PROFILER_LABEL and AUTO_BASE_PROFILER_LABEL_DYNAMIC + // variant. + AutoProfilerLabel(const char* aLabel, const char* aDynamicString, + ProfilingCategoryPair aCategoryPair, uint32_t aFlags = 0) { + // Get the ProfilingStack from TLS. + Push(GetProfilingStack(), aLabel, aDynamicString, aCategoryPair, aFlags); + } + + void Push(ProfilingStack* aProfilingStack, const char* aLabel, + const char* aDynamicString, ProfilingCategoryPair aCategoryPair, + uint32_t aFlags = 0) { + // This function runs both on and off the main thread. + + mProfilingStack = aProfilingStack; + if (mProfilingStack) { + mProfilingStack->pushLabelFrame(aLabel, aDynamicString, this, + aCategoryPair, aFlags); + } + } + + ~AutoProfilerLabel() { + // This function runs both on and off the main thread. + + if (mProfilingStack) { + mProfilingStack->pop(); + } + } + + MFBT_API static ProfilingStack* GetProfilingStack(); + + private: + // We save a ProfilingStack pointer in the ctor so we don't have to redo the + // TLS lookup in the dtor. + ProfilingStack* mProfilingStack; + + public: + // See the comment on the definition in platform.cpp for details about this. + static MOZ_THREAD_LOCAL(ProfilingStack*) sProfilingStack; +}; + +} // namespace mozilla::baseprofiler + +#endif // !MOZ_GECKO_PROFILER + +#endif // BaseProfilerLabels_h diff --git a/mozglue/baseprofiler/public/BaseProfilerMarkerTypes.h b/mozglue/baseprofiler/public/BaseProfilerMarkerTypes.h new file mode 100644 index 0000000000..404e15c5f6 --- /dev/null +++ b/mozglue/baseprofiler/public/BaseProfilerMarkerTypes.h @@ -0,0 +1,125 @@ +/* -*- Mode: C++; tab-width: 2; 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/. */ + +#ifndef BaseProfilerMarkerTypes_h +#define BaseProfilerMarkerTypes_h + +// This header contains common marker type definitions. +// +// It #include's "mozilla/BaseProfilerMarkers.h", see that file for how to +// define other marker types, and how to add markers to the profiler buffers. +// +// If you don't need to use these common types, #include +// "mozilla/BaseProfilerMarkers.h" instead. +// +// Types in this files can be defined without relying on xpcom. +// Others are defined in "ProfilerMarkerTypes.h". + +// !!! /!\ WORK IN PROGRESS /!\ !!! +// This file contains draft marker definitions, but most are not used yet. +// Further work is needed to complete these definitions, and use them to convert +// existing PROFILER_ADD_MARKER calls. See meta bug 1661394. + +#include "mozilla/BaseProfilerMarkers.h" + +namespace mozilla::baseprofiler::markers { + +struct MediaSampleMarker { + static constexpr Span<const char> MarkerTypeName() { + return MakeStringSpan("MediaSample"); + } + static void StreamJSONMarkerData(SpliceableJSONWriter& aWriter, + int64_t aSampleStartTimeUs, + int64_t aSampleEndTimeUs, + int64_t aQueueLength) { + aWriter.IntProperty("sampleStartTimeUs", aSampleStartTimeUs); + aWriter.IntProperty("sampleEndTimeUs", aSampleEndTimeUs); + aWriter.IntProperty("queueLength", aQueueLength); + } + static MarkerSchema MarkerTypeDisplay() { + using MS = MarkerSchema; + MS schema{MS::Location::MarkerChart, MS::Location::MarkerTable}; + schema.AddKeyLabelFormat("sampleStartTimeUs", "Sample start time", + MS::Format::Microseconds); + schema.AddKeyLabelFormat("sampleEndTimeUs", "Sample end time", + MS::Format::Microseconds); + schema.AddKeyLabelFormat("queueLength", "Queue length", + MS::Format::Integer); + return schema; + } +}; + +struct VideoFallingBehindMarker { + static constexpr Span<const char> MarkerTypeName() { + return MakeStringSpan("VideoFallingBehind"); + } + static void StreamJSONMarkerData(SpliceableJSONWriter& aWriter, + int64_t aVideoFrameStartTimeUs, + int64_t aMediaCurrentTimeUs) { + aWriter.IntProperty("videoFrameStartTimeUs", aVideoFrameStartTimeUs); + aWriter.IntProperty("mediaCurrentTimeUs", aMediaCurrentTimeUs); + } + static MarkerSchema MarkerTypeDisplay() { + using MS = MarkerSchema; + MS schema{MS::Location::MarkerChart, MS::Location::MarkerTable}; + schema.AddKeyLabelFormat("videoFrameStartTimeUs", "Video frame start time", + MS::Format::Microseconds); + schema.AddKeyLabelFormat("mediaCurrentTimeUs", "Media current time", + MS::Format::Microseconds); + return schema; + } +}; + +struct ContentBuildMarker { + static constexpr Span<const char> MarkerTypeName() { + return MakeStringSpan("CONTENT_FULL_PAINT_TIME"); + } + static void StreamJSONMarkerData(SpliceableJSONWriter& aWriter) {} + static MarkerSchema MarkerTypeDisplay() { + using MS = MarkerSchema; + MS schema{MS::Location::MarkerChart, MS::Location::MarkerTable}; + return schema; + } +}; + +struct MediaEngineMarker { + static constexpr Span<const char> MarkerTypeName() { + return MakeStringSpan("MediaEngine"); + } + static void StreamJSONMarkerData(baseprofiler::SpliceableJSONWriter& aWriter, + uint64_t aMediaEngineId) { + aWriter.IntProperty("id", aMediaEngineId); + } + static MarkerSchema MarkerTypeDisplay() { + using MS = MarkerSchema; + MS schema{MS::Location::MarkerChart, MS::Location::MarkerTable}; + schema.AddKeyLabelFormat("id", "Id", MS::Format::Integer); + return schema; + } +}; + +struct MediaEngineTextMarker { + static constexpr Span<const char> MarkerTypeName() { + return MakeStringSpan("MediaEngineText"); + } + static void StreamJSONMarkerData(baseprofiler::SpliceableJSONWriter& aWriter, + uint64_t aMediaEngineId, + const ProfilerString8View& aText) { + aWriter.IntProperty("id", aMediaEngineId); + aWriter.StringProperty("text", aText); + } + static MarkerSchema MarkerTypeDisplay() { + using MS = MarkerSchema; + MS schema{MS::Location::MarkerChart, MS::Location::MarkerTable}; + schema.AddKeyLabelFormat("id", "Id", MS::Format::Integer); + schema.AddKeyLabelFormat("text", "Details", MS::Format::String); + return schema; + } +}; + +} // namespace mozilla::baseprofiler::markers + +#endif // BaseProfilerMarkerTypes_h diff --git a/mozglue/baseprofiler/public/BaseProfilerMarkers.h b/mozglue/baseprofiler/public/BaseProfilerMarkers.h new file mode 100644 index 0000000000..d706cefd4a --- /dev/null +++ b/mozglue/baseprofiler/public/BaseProfilerMarkers.h @@ -0,0 +1,255 @@ +/* -*- Mode: C++; tab-width: 2; 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/. */ + +// Markers are useful to delimit something important happening such as the first +// paint. Unlike labels, which are only recorded in the profile buffer if a +// sample is collected while the label is on the label stack, markers will +// always be recorded in the profile buffer. +// +// This header contains basic definitions necessary to create marker types, and +// to add markers to the profiler buffers. +// +// If basic marker types are needed, #include +// "mozilla/BaseProfilerMarkerTypes.h" instead. +// +// But if you want to create your own marker type locally, you can #include this +// header only; look at mozilla/BaseProfilerMarkerTypes.h for examples of how to +// define types, and mozilla/BaseProfilerMarkerPrerequisites.h for some +// supporting types. +// +// To then record markers: +// - Use `baseprofiler::AddMarker(...)` from mozglue or other libraries that +// are outside of xul, especially if they may happen outside of xpcom's +// lifetime (typically startup, shutdown, or tests). +// - Otherwise #include "ProfilerMarkers.h" instead, and use +// `profiler_add_marker(...)`. +// See these functions for more details. + +#ifndef BaseProfilerMarkers_h +#define BaseProfilerMarkers_h + +#include "mozilla/BaseProfilerMarkersDetail.h" +#include "mozilla/BaseProfilerLabels.h" +#include "mozilla/TimeStamp.h" +#include "mozilla/Unused.h" + +#include <functional> +#include <string> +#include <utility> + +namespace mozilla::baseprofiler { + +#ifdef MOZ_GECKO_PROFILER +// Forward-declaration. TODO: Move to more common header, see bug 1681416. +MFBT_API bool profiler_capture_backtrace_into( + ProfileChunkedBuffer& aChunkedBuffer, StackCaptureOptions aCaptureOptions); + +// Add a marker to a given buffer. `AddMarker()` and related macros should be +// used in most cases, see below for more information about them and the +// parameters; This function may be useful when markers need to be recorded in a +// local buffer outside of the main profiler buffer. +template <typename MarkerType, typename... PayloadArguments> +ProfileBufferBlockIndex AddMarkerToBuffer( + ProfileChunkedBuffer& aBuffer, const ProfilerString8View& aName, + const MarkerCategory& aCategory, MarkerOptions&& aOptions, + MarkerType aMarkerType, const PayloadArguments&... aPayloadArguments) { + Unused << aMarkerType; // Only the empty object type is useful. + AUTO_BASE_PROFILER_LABEL("baseprofiler::AddMarkerToBuffer", PROFILER); + return base_profiler_markers_detail::AddMarkerToBuffer<MarkerType>( + aBuffer, aName, aCategory, std::move(aOptions), + // Do not capture a stack if the NoMarkerStacks feature is set. + profiler_active_without_feature(ProfilerFeature::NoMarkerStacks) + ? ::mozilla::baseprofiler::profiler_capture_backtrace_into + : nullptr, + aPayloadArguments...); +} + +// Add a marker (without payload) to a given buffer. +inline ProfileBufferBlockIndex AddMarkerToBuffer( + ProfileChunkedBuffer& aBuffer, const ProfilerString8View& aName, + const MarkerCategory& aCategory, MarkerOptions&& aOptions = {}) { + return AddMarkerToBuffer(aBuffer, aName, aCategory, std::move(aOptions), + markers::NoPayload{}); +} +#endif // MOZ_GECKO_PROFILER + +// Add a marker to the Base Profiler buffer. +// - aName: Main name of this marker. +// - aCategory: Category for this marker. +// - aOptions: Optional settings (such as timing, inner window id, +// backtrace...), see `MarkerOptions` for details. +// - aMarkerType: Empty object that specifies the type of marker. +// - aPayloadArguments: Arguments expected by this marker type's +// ` StreamJSONMarkerData` function. +template <typename MarkerType, typename... PayloadArguments> +ProfileBufferBlockIndex AddMarker( + const ProfilerString8View& aName, const MarkerCategory& aCategory, + MarkerOptions&& aOptions, MarkerType aMarkerType, + const PayloadArguments&... aPayloadArguments) { +#ifndef MOZ_GECKO_PROFILER + return {}; +#else + // Record base markers whenever the core buffer is in session. + // TODO: When profiler_thread_is_being_profiled becomes available from + // mozglue, use it instead. + ProfileChunkedBuffer& coreBuffer = + ::mozilla::baseprofiler::profiler_get_core_buffer(); + if (!coreBuffer.IsInSession()) { + return {}; + } + return ::mozilla::baseprofiler::AddMarkerToBuffer( + coreBuffer, aName, aCategory, std::move(aOptions), aMarkerType, + aPayloadArguments...); +#endif +} + +// Add a marker (without payload) to the Base Profiler buffer. +inline ProfileBufferBlockIndex AddMarker(const ProfilerString8View& aName, + const MarkerCategory& aCategory, + MarkerOptions&& aOptions = {}) { + return AddMarker(aName, aCategory, std::move(aOptions), markers::NoPayload{}); +} + +} // namespace mozilla::baseprofiler + +// Same as `AddMarker()` (without payload). This macro is safe to use even if +// MOZ_GECKO_PROFILER is not #defined. +#define BASE_PROFILER_MARKER_UNTYPED(markerName, categoryName, ...) \ + do { \ + AUTO_PROFILER_STATS(BASE_PROFILER_MARKER_UNTYPED); \ + ::mozilla::baseprofiler::AddMarker( \ + markerName, ::mozilla::baseprofiler::category::categoryName, \ + ##__VA_ARGS__); \ + } while (false) + +// Same as `AddMarker()` (with payload). This macro is safe to use even if +// MOZ_GECKO_PROFILER is not #defined. +#define BASE_PROFILER_MARKER(markerName, categoryName, options, MarkerType, \ + ...) \ + do { \ + AUTO_PROFILER_STATS(BASE_PROFILER_MARKER_with_##MarkerType); \ + ::mozilla::baseprofiler::AddMarker( \ + markerName, ::mozilla::baseprofiler::category::categoryName, options, \ + ::mozilla::baseprofiler::markers::MarkerType{}, ##__VA_ARGS__); \ + } while (false) + +namespace mozilla::baseprofiler::markers { +// Most common marker type. Others are in BaseProfilerMarkerTypes.h. +struct TextMarker { + static constexpr Span<const char> MarkerTypeName() { + return MakeStringSpan("Text"); + } + static void StreamJSONMarkerData(baseprofiler::SpliceableJSONWriter& aWriter, + const ProfilerString8View& aText) { + aWriter.StringProperty("name", aText); + } + static MarkerSchema MarkerTypeDisplay() { + using MS = MarkerSchema; + MS schema{MS::Location::MarkerChart, MS::Location::MarkerTable}; + schema.SetChartLabel("{marker.data.name}"); + schema.SetTableLabel("{marker.name} - {marker.data.name}"); + schema.AddKeyLabelFormatSearchable("name", "Details", MS::Format::String, + MS::Searchable::Searchable); + return schema; + } +}; + +// Keep this struct in sync with the `gecko_profiler::marker::Tracing` Rust +// counterpart. +struct Tracing { + static constexpr Span<const char> MarkerTypeName() { + return MakeStringSpan("tracing"); + } + static void StreamJSONMarkerData(SpliceableJSONWriter& aWriter, + const ProfilerString8View& aCategory) { + if (aCategory.Length() != 0) { + aWriter.StringProperty("category", aCategory); + } + } + static MarkerSchema MarkerTypeDisplay() { + using MS = MarkerSchema; + MS schema{MS::Location::MarkerChart, MS::Location::MarkerTable, + MS::Location::TimelineOverview}; + schema.AddKeyLabelFormatSearchable("category", "Type", MS::Format::String, + MS::Searchable::Searchable); + return schema; + } +}; +} // namespace mozilla::baseprofiler::markers + +// Add a text marker. This macro is safe to use even if MOZ_GECKO_PROFILER is +// not #defined. +#define BASE_PROFILER_MARKER_TEXT(markerName, categoryName, options, text) \ + do { \ + AUTO_PROFILER_STATS(BASE_PROFILER_MARKER_TEXT); \ + ::mozilla::baseprofiler::AddMarker( \ + markerName, ::mozilla::baseprofiler::category::categoryName, options, \ + ::mozilla::baseprofiler::markers::TextMarker{}, text); \ + } while (false) + +namespace mozilla::baseprofiler { + +// RAII object that adds a BASE_PROFILER_MARKER_TEXT when destroyed; the +// marker's timing will be the interval from construction (unless an instant or +// start time is already specified in the provided options) until destruction. +class MOZ_RAII AutoProfilerTextMarker { + public: + AutoProfilerTextMarker(const char* aMarkerName, + const MarkerCategory& aCategory, + MarkerOptions&& aOptions, const std::string& aText) + : mMarkerName(aMarkerName), + mCategory(aCategory), + mOptions(std::move(aOptions)), + mText(aText) { + MOZ_ASSERT(mOptions.Timing().EndTime().IsNull(), + "AutoProfilerTextMarker options shouldn't have an end time"); + if (profiler_is_active_and_unpaused() && + mOptions.Timing().StartTime().IsNull()) { + mOptions.Set(MarkerTiming::InstantNow()); + } + } + + ~AutoProfilerTextMarker() { + if (profiler_is_active_and_unpaused()) { + mOptions.TimingRef().SetIntervalEnd(); + AUTO_PROFILER_STATS(AUTO_BASE_PROFILER_MARKER_TEXT); + AddMarker(ProfilerString8View::WrapNullTerminatedString(mMarkerName), + mCategory, std::move(mOptions), markers::TextMarker{}, mText); + } + } + + protected: + const char* mMarkerName; + MarkerCategory mCategory; + MarkerOptions mOptions; + std::string mText; +}; + +#ifdef MOZ_GECKO_PROFILER +extern template MFBT_API ProfileBufferBlockIndex +AddMarker(const ProfilerString8View&, const MarkerCategory&, MarkerOptions&&, + markers::TextMarker, const std::string&); + +extern template MFBT_API ProfileBufferBlockIndex +AddMarkerToBuffer(ProfileChunkedBuffer&, const ProfilerString8View&, + const MarkerCategory&, MarkerOptions&&, markers::NoPayload); + +extern template MFBT_API ProfileBufferBlockIndex AddMarkerToBuffer( + ProfileChunkedBuffer&, const ProfilerString8View&, const MarkerCategory&, + MarkerOptions&&, markers::TextMarker, const std::string&); +#endif // MOZ_GECKO_PROFILER + +} // namespace mozilla::baseprofiler + +// Creates an AutoProfilerTextMarker RAII object. This macro is safe to use +// even if MOZ_GECKO_PROFILER is not #defined. +#define AUTO_BASE_PROFILER_MARKER_TEXT(markerName, categoryName, options, \ + text) \ + ::mozilla::baseprofiler::AutoProfilerTextMarker PROFILER_RAII( \ + markerName, ::mozilla::baseprofiler::category::categoryName, options, \ + text) + +#endif // BaseProfilerMarkers_h diff --git a/mozglue/baseprofiler/public/BaseProfilerMarkersDetail.h b/mozglue/baseprofiler/public/BaseProfilerMarkersDetail.h new file mode 100644 index 0000000000..1102499100 --- /dev/null +++ b/mozglue/baseprofiler/public/BaseProfilerMarkersDetail.h @@ -0,0 +1,741 @@ +/* -*- Mode: C++; tab-width: 2; 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/. */ + +#ifndef BaseProfilerMarkersDetail_h +#define BaseProfilerMarkersDetail_h + +#ifndef BaseProfilerMarkers_h +# error "This header should only be #included by BaseProfilerMarkers.h" +#endif + +#include "mozilla/BaseProfilerMarkersPrerequisites.h" + +// ~~ HERE BE DRAGONS ~~ +// +// Everything below is internal implementation detail, you shouldn't need to +// look at it unless working on the profiler code. + +#include "mozilla/BaseProfileJSONWriter.h" +#include "mozilla/ProfileBufferEntryKinds.h" + +#include <limits> +#include <tuple> +#include <type_traits> + +namespace mozilla::baseprofiler { +// Implemented in platform.cpp +MFBT_API ProfileChunkedBuffer& profiler_get_core_buffer(); +} // namespace mozilla::baseprofiler + +namespace mozilla::base_profiler_markers_detail { + +struct Streaming { + // A `MarkerDataDeserializer` is a free function that can read a serialized + // payload from an `EntryReader` and streams it as JSON object properties. + using MarkerDataDeserializer = void (*)(ProfileBufferEntryReader&, + baseprofiler::SpliceableJSONWriter&); + + // A `MarkerTypeNameFunction` is a free function that returns the name of the + // marker type. + using MarkerTypeNameFunction = Span<const char> (*)(); + + // A `MarkerSchemaFunction` is a free function that returns a + // `MarkerSchema`, which contains all the information needed to stream + // the display schema associated with a marker type. + using MarkerSchemaFunction = MarkerSchema (*)(); + + struct MarkerTypeFunctions { + MarkerDataDeserializer mMarkerDataDeserializer = nullptr; + MarkerTypeNameFunction mMarkerTypeNameFunction = nullptr; + MarkerSchemaFunction mMarkerSchemaFunction = nullptr; + }; + + // A `DeserializerTag` will be added before the payload, to help select the + // correct deserializer when reading back the payload. + using DeserializerTag = uint8_t; + + // Store a deserializer (and other marker-type-specific functions) and get its + // `DeserializerTag`. + // This is intended to be only used once per deserializer when a new marker + // type is used for the first time, so it should be called to initialize a + // `static const` tag that will be re-used by all markers of the corresponding + // payload type -- see use below. + MFBT_API static DeserializerTag TagForMarkerTypeFunctions( + MarkerDataDeserializer aDeserializer, + MarkerTypeNameFunction aMarkerTypeNameFunction, + MarkerSchemaFunction aMarkerSchemaFunction); + + // Get the `MarkerDataDeserializer` for a given `DeserializerTag`. + MFBT_API static MarkerDataDeserializer DeserializerForTag( + DeserializerTag aTag); + + // Retrieve all MarkerTypeFunctions's. + // While this object lives, no other operations can happen on this list. + class LockedMarkerTypeFunctionsList { + public: + MFBT_API LockedMarkerTypeFunctionsList(); + MFBT_API ~LockedMarkerTypeFunctionsList(); + + LockedMarkerTypeFunctionsList(const LockedMarkerTypeFunctionsList&) = + delete; + LockedMarkerTypeFunctionsList& operator=( + const LockedMarkerTypeFunctionsList&) = delete; + + auto begin() const { return mMarkerTypeFunctionsSpan.begin(); } + auto end() const { return mMarkerTypeFunctionsSpan.end(); } + + private: + Span<const MarkerTypeFunctions> mMarkerTypeFunctionsSpan; + }; +}; + +// This helper will examine a marker type's `StreamJSONMarkerData` function, see +// specialization below. +template <typename T> +struct StreamFunctionTypeHelper; + +// Helper specialization that takes the expected +// `StreamJSONMarkerData(baseprofiler::SpliceableJSONWriter&, ...)` function and +// provide information about the `...` parameters. +template <typename R, typename... As> +struct StreamFunctionTypeHelper<R(baseprofiler::SpliceableJSONWriter&, As...)> { + constexpr static size_t scArity = sizeof...(As); + using TupleType = + std::tuple<std::remove_cv_t<std::remove_reference_t<As>>...>; + + // Serialization function that takes the exact same parameter types + // (const-ref'd) as `StreamJSONMarkerData`. This has to be inside the helper + // because only here can we access the raw parameter pack `As...`. + // And because we're using the same argument types through + // references-to-const, permitted implicit conversions can happen. + static ProfileBufferBlockIndex Serialize( + ProfileChunkedBuffer& aBuffer, const ProfilerString8View& aName, + const MarkerCategory& aCategory, MarkerOptions&& aOptions, + Streaming::DeserializerTag aDeserializerTag, const As&... aAs) { + // Note that options are first after the entry kind, because they contain + // the thread id, which is handled first to filter markers by threads. + return aBuffer.PutObjects(ProfileBufferEntryKind::Marker, aOptions, aName, + aCategory, aDeserializerTag, + MarkerPayloadType::Cpp, aAs...); + } +}; + +// Helper for a marker type. +// A marker type is defined in a `struct` with some expected static member +// functions. See example in BaseProfilerMarkers.h. +template <typename MarkerType> +struct MarkerTypeSerialization { + // Definitions to access the expected + // `StreamJSONMarkerData(baseprofiler::SpliceableJSONWriter&, ...)` function + // and its parameters. + using StreamFunctionType = + StreamFunctionTypeHelper<decltype(MarkerType::StreamJSONMarkerData)>; + constexpr static size_t scStreamFunctionParameterCount = + StreamFunctionType::scArity; + using StreamFunctionUserParametersTuple = + typename StreamFunctionType::TupleType; + template <size_t i> + using StreamFunctionParameter = + std::tuple_element_t<i, StreamFunctionUserParametersTuple>; + + template <typename... Ts> + static ProfileBufferBlockIndex Serialize(ProfileChunkedBuffer& aBuffer, + const ProfilerString8View& aName, + const MarkerCategory& aCategory, + MarkerOptions&& aOptions, + const Ts&... aTs) { + static_assert(!std::is_same_v<MarkerType, + ::mozilla::baseprofiler::markers::NoPayload>, + "NoPayload should have been handled in the caller."); + // Register marker type functions, and get the tag for this deserializer. + // Note that the tag is stored in a function-static object, and this + // function is static in a templated struct, so there should only be one tag + // per MarkerType. + // Making the tag class-static may have been more efficient (to avoid a + // thread-safe init check at every call), but random global static + // initialization order would make it more complex to coordinate with + // `Streaming::TagForMarkerTypeFunctions()`, and also would add a (small) + // cost for everybody, even the majority of users not using the profiler. + static const Streaming::DeserializerTag tag = + Streaming::TagForMarkerTypeFunctions(Deserialize, + MarkerType::MarkerTypeName, + MarkerType::MarkerTypeDisplay); + return StreamFunctionType::Serialize(aBuffer, aName, aCategory, + std::move(aOptions), tag, aTs...); + } + + private: + // This templated function will recursively deserialize each argument expected + // by `MarkerType::StreamJSONMarkerData()` on the stack, and call it at the + // end. E.g., for `StreamJSONMarkerData(int, char)`: + // - DeserializeArguments<0>(aER, aWriter) reads an int and calls: + // - DeserializeArguments<1>(aER, aWriter, const int&) reads a char and calls: + // - MarkerType::StreamJSONMarkerData(aWriter, const int&, const char&). + // Prototyping on godbolt showed that clang and gcc can flatten these + // recursive calls into one function with successive reads followed by the one + // stream call; tested up to 40 arguments: https://godbolt.org/z/5KeeM4 + template <size_t i = 0, typename... Args> + static void DeserializeArguments(ProfileBufferEntryReader& aEntryReader, + baseprofiler::SpliceableJSONWriter& aWriter, + const Args&... aArgs) { + static_assert(sizeof...(Args) == i, + "We should have collected `i` arguments so far"); + if constexpr (i < scStreamFunctionParameterCount) { + // Deserialize the i-th argument on this stack. + auto argument = aEntryReader.ReadObject<StreamFunctionParameter<i>>(); + // Add our local argument to the next recursive call. + DeserializeArguments<i + 1>(aEntryReader, aWriter, aArgs..., argument); + } else { + // We've read all the arguments, finally call the `StreamJSONMarkerData` + // function, which should write the appropriate JSON elements for this + // marker type. Note that the MarkerType-specific "type" element is + // already written. + MarkerType::StreamJSONMarkerData(aWriter, aArgs...); + } + } + + public: + static void Deserialize(ProfileBufferEntryReader& aEntryReader, + baseprofiler::SpliceableJSONWriter& aWriter) { + aWriter.StringProperty("type", MarkerType::MarkerTypeName()); + DeserializeArguments(aEntryReader, aWriter); + } +}; + +template <> +struct MarkerTypeSerialization<::mozilla::baseprofiler::markers::NoPayload> { + // Nothing! NoPayload has special handling avoiding payload work. +}; + +template <typename MarkerType, typename... Ts> +static ProfileBufferBlockIndex AddMarkerWithOptionalStackToBuffer( + ProfileChunkedBuffer& aBuffer, const ProfilerString8View& aName, + const MarkerCategory& aCategory, MarkerOptions&& aOptions, + const Ts&... aTs) { + if constexpr (std::is_same_v<MarkerType, + ::mozilla::baseprofiler::markers::NoPayload>) { + static_assert(sizeof...(Ts) == 0, + "NoPayload does not accept any payload arguments."); + // Special case for NoPayload where there is a stack or inner window id: + // Because these options would be stored in the payload 'data' object, but + // there is no such object for NoPayload, we convert the marker to another + // type (without user fields in the 'data' object), so that the stack and/or + // inner window id are not lost. + // TODO: Remove this when bug 1646714 lands. + if (aOptions.Stack().GetChunkedBuffer() || + !aOptions.InnerWindowId().IsUnspecified()) { + struct NoPayloadUserData { + static constexpr Span<const char> MarkerTypeName() { + return MakeStringSpan("NoPayloadUserData"); + } + static void StreamJSONMarkerData( + baseprofiler::SpliceableJSONWriter& aWriter) { + // No user payload. + } + static mozilla::MarkerSchema MarkerTypeDisplay() { + using MS = mozilla::MarkerSchema; + MS schema{MS::Location::MarkerChart, MS::Location::MarkerTable}; + // No user data to display. + return schema; + } + }; + return MarkerTypeSerialization<NoPayloadUserData>::Serialize( + aBuffer, aName, aCategory, std::move(aOptions)); + } + + // Note that options are first after the entry kind, because they contain + // the thread id, which is handled first to filter markers by threads. + return aBuffer.PutObjects( + ProfileBufferEntryKind::Marker, std::move(aOptions), aName, aCategory, + base_profiler_markers_detail::Streaming::DeserializerTag(0)); + } else { + return MarkerTypeSerialization<MarkerType>::Serialize( + aBuffer, aName, aCategory, std::move(aOptions), aTs...); + } +} + +// Pointer to a function that can capture a backtrace into the provided +// `ProfileChunkedBuffer`, and returns true when successful. +using OptionalBacktraceCaptureFunction = bool (*)(ProfileChunkedBuffer&, + StackCaptureOptions); + +// Use a pre-allocated and cleared chunked buffer in the main thread's +// `AddMarkerToBuffer()`. +// Null if not the main thread, or if profilers are not active. +MFBT_API ProfileChunkedBuffer* GetClearedBufferForMainThreadAddMarker(); +// Called by the profiler(s) when starting/stopping. Safe to nest. +MFBT_API void EnsureBufferForMainThreadAddMarker(); +MFBT_API void ReleaseBufferForMainThreadAddMarker(); + +// Add a marker with the given name, options, and arguments to the given buffer. +// Because this may be called from either Base or Gecko Profiler functions, the +// appropriate backtrace-capturing function must also be provided. +template <typename MarkerType, typename... Ts> +ProfileBufferBlockIndex AddMarkerToBuffer( + ProfileChunkedBuffer& aBuffer, const ProfilerString8View& aName, + const MarkerCategory& aCategory, MarkerOptions&& aOptions, + OptionalBacktraceCaptureFunction aOptionalBacktraceCaptureFunction, + const Ts&... aTs) { + if (aOptions.ThreadId().IsUnspecified()) { + // If yet unspecified, set thread to this thread where the marker is added. + aOptions.Set(MarkerThreadId::CurrentThread()); + } + + if (aOptions.IsTimingUnspecified()) { + // If yet unspecified, set timing to this instant of adding the marker. + aOptions.Set(MarkerTiming::InstantNow()); + } + + StackCaptureOptions captureOptions = aOptions.Stack().CaptureOptions(); + if (captureOptions != StackCaptureOptions::NoStack && + // Backtrace capture function will be nullptr if the profiler + // NoMarkerStacks feature is set. + aOptionalBacktraceCaptureFunction != nullptr) { + // A capture was requested, let's attempt to do it here&now. This avoids a + // lot of allocations that would be necessary if capturing a backtrace + // separately. + // TODO reduce internal profiler stack levels, see bug 1659872. + auto CaptureStackAndAddMarker = [&](ProfileChunkedBuffer& aChunkedBuffer) { + aOptions.StackRef().UseRequestedBacktrace( + aOptionalBacktraceCaptureFunction(aChunkedBuffer, captureOptions) + ? &aChunkedBuffer + : nullptr); + // This call must be made from here, while chunkedBuffer is in scope. + return AddMarkerWithOptionalStackToBuffer<MarkerType>( + aBuffer, aName, aCategory, std::move(aOptions), aTs...); + }; + + if (ProfileChunkedBuffer* buffer = GetClearedBufferForMainThreadAddMarker(); + buffer) { + // Use a pre-allocated buffer for the main thread (because it's the most + // used thread, and most sensitive to overhead), so it's only allocated + // once. It could be null if this is not the main thread, or no profilers + // are currently active. + return CaptureStackAndAddMarker(*buffer); + } + // TODO use a local on-stack byte buffer to remove last allocation. + ProfileBufferChunkManagerSingle chunkManager( + ProfileBufferChunkManager::scExpectedMaximumStackSize); + ProfileChunkedBuffer chunkedBuffer( + ProfileChunkedBuffer::ThreadSafety::WithoutMutex, chunkManager); + return CaptureStackAndAddMarker(chunkedBuffer); + } + + return AddMarkerWithOptionalStackToBuffer<MarkerType>( + aBuffer, aName, aCategory, std::move(aOptions), aTs...); +} + +// Assuming aEntryReader points right after the entry type (being Marker), this +// reads the remainder of the marker and outputs it. +// - GetWriterForThreadCallback, called first, after the thread id is read: +// (ThreadId) -> SpliceableJSONWriter* or null +// If null, nothing will be output, but aEntryReader will still be read fully. +// - StackCallback, only called if GetWriterForThreadCallback didn't return +// null, and if the marker contains a stack: +// (ProfileChunkedBuffer&) -> void +// - RustMarkerCallback, only called if GetWriterForThreadCallback didn't return +// null, and if the marker contains a Rust payload: +// (DeserializerTag) -> void +template <typename GetWriterForThreadCallback, typename StackCallback, + typename RustMarkerCallback> +void DeserializeAfterKindAndStream( + ProfileBufferEntryReader& aEntryReader, + GetWriterForThreadCallback&& aGetWriterForThreadCallback, + StackCallback&& aStackCallback, RustMarkerCallback&& aRustMarkerCallback) { + // Each entry is made up of the following: + // ProfileBufferEntry::Kind::Marker, <- already read by caller + // options, <- next location in entries + // name, + // payload + const MarkerOptions options = aEntryReader.ReadObject<MarkerOptions>(); + + baseprofiler::SpliceableJSONWriter* writer = + std::forward<GetWriterForThreadCallback>(aGetWriterForThreadCallback)( + options.ThreadId().ThreadId()); + if (!writer) { + // No writer associated with this thread id, drop it. + aEntryReader.SetRemainingBytes(0); + return; + } + + // Write the information to JSON with the following schema: + // [name, startTime, endTime, phase, category, data] + writer->StartArrayElement(); + { + writer->UniqueStringElement(aEntryReader.ReadObject<ProfilerString8View>()); + + const double startTime = options.Timing().GetStartTime(); + writer->TimeDoubleMsElement(startTime); + + const double endTime = options.Timing().GetEndTime(); + writer->TimeDoubleMsElement(endTime); + + writer->IntElement(static_cast<int64_t>(options.Timing().MarkerPhase())); + + MarkerCategory category = aEntryReader.ReadObject<MarkerCategory>(); + writer->IntElement(static_cast<int64_t>(category.GetCategory())); + + if (const auto tag = + aEntryReader.ReadObject<mozilla::base_profiler_markers_detail:: + Streaming::DeserializerTag>(); + tag != 0) { + writer->StartObjectElement(); + { + // Stream "common props". + + // TODO: Move this to top-level tuple, when frontend supports it. + if (!options.InnerWindowId().IsUnspecified()) { + // Here, we are converting uint64_t to double. Both Browsing Context + // and Inner Window IDs are created using + // `nsContentUtils::GenerateProcessSpecificId`, which is specifically + // designed to only use 53 of the 64 bits to be lossless when passed + // into and out of JS as a double. + writer->DoubleProperty( + "innerWindowID", + static_cast<double>(options.InnerWindowId().Id())); + } + + // TODO: Move this to top-level tuple, when frontend supports it. + if (ProfileChunkedBuffer* chunkedBuffer = + options.Stack().GetChunkedBuffer(); + chunkedBuffer) { + writer->StartObjectProperty("stack"); + { std::forward<StackCallback>(aStackCallback)(*chunkedBuffer); } + writer->EndObject(); + } + + auto payloadType = static_cast<mozilla::MarkerPayloadType>( + aEntryReader.ReadObject< + std::underlying_type_t<mozilla::MarkerPayloadType>>()); + + // Stream the payload, including the type. + switch (payloadType) { + case mozilla::MarkerPayloadType::Cpp: { + mozilla::base_profiler_markers_detail::Streaming:: + MarkerDataDeserializer deserializer = + mozilla::base_profiler_markers_detail::Streaming:: + DeserializerForTag(tag); + MOZ_RELEASE_ASSERT(deserializer); + deserializer(aEntryReader, *writer); + MOZ_ASSERT(aEntryReader.RemainingBytes() == 0u); + break; + } + case mozilla::MarkerPayloadType::Rust: + std::forward<RustMarkerCallback>(aRustMarkerCallback)(tag); + MOZ_ASSERT(aEntryReader.RemainingBytes() == 0u); + break; + default: + MOZ_ASSERT_UNREACHABLE("Unknown payload type."); + break; + } + } + writer->EndObject(); + } + } + writer->EndArray(); + MOZ_ASSERT(aEntryReader.RemainingBytes() == 0u); +} + +} // namespace mozilla::base_profiler_markers_detail + +namespace mozilla { + +// ---------------------------------------------------------------------------- +// Serializer, Deserializer: ProfilerStringView<CHAR> + +// The serialization starts with a ULEB128 number that encodes both whether the +// ProfilerStringView is literal (Least Significant Bit = 0) or not (LSB = 1), +// plus the string length (excluding null terminator) in bytes, shifted left by +// 1 bit. Following that number: +// - If literal, the string pointer value. +// - If non-literal, the contents as bytes (excluding null terminator if any). +template <typename CHAR> +struct ProfileBufferEntryWriter::Serializer<ProfilerStringView<CHAR>> { + static Length Bytes(const ProfilerStringView<CHAR>& aString) { + MOZ_RELEASE_ASSERT( + aString.Length() < std::numeric_limits<Length>::max() / 2, + "Double the string length doesn't fit in Length type"); + const Length stringLength = static_cast<Length>(aString.Length()); + if (aString.IsLiteral()) { + // Literal -> Length shifted left and LSB=0, then pointer. + return ULEB128Size(stringLength << 1 | 0u) + + static_cast<ProfileChunkedBuffer::Length>(sizeof(const CHAR*)); + } + // Non-literal -> Length shifted left and LSB=1, then string size in bytes. + return ULEB128Size((stringLength << 1) | 1u) + stringLength * sizeof(CHAR); + } + + static void Write(ProfileBufferEntryWriter& aEW, + const ProfilerStringView<CHAR>& aString) { + MOZ_RELEASE_ASSERT( + aString.Length() < std::numeric_limits<Length>::max() / 2, + "Double the string length doesn't fit in Length type"); + const Span<const CHAR> span = aString; + if (aString.IsLiteral()) { + // Literal -> Length shifted left and LSB=0, then pointer. + aEW.WriteULEB128(span.Length() << 1 | 0u); + aEW.WriteObject(WrapProfileBufferRawPointer(span.Elements())); + return; + } + // Non-literal -> Length shifted left and LSB=1, then string size in bytes. + aEW.WriteULEB128(span.Length() << 1 | 1u); + aEW.WriteBytes(span.Elements(), span.LengthBytes()); + } +}; + +template <typename CHAR> +struct ProfileBufferEntryReader::Deserializer<ProfilerStringView<CHAR>> { + static void ReadInto(ProfileBufferEntryReader& aER, + ProfilerStringView<CHAR>& aString) { + aString = Read(aER); + } + + static ProfilerStringView<CHAR> Read(ProfileBufferEntryReader& aER) { + const Length lengthAndIsLiteral = aER.ReadULEB128<Length>(); + const Length stringLength = lengthAndIsLiteral >> 1; + if ((lengthAndIsLiteral & 1u) == 0u) { + // LSB==0 -> Literal string, read the string pointer. + return ProfilerStringView<CHAR>( + aER.ReadObject<const CHAR*>(), stringLength, + ProfilerStringView<CHAR>::Ownership::Literal); + } + // LSB==1 -> Not a literal string. + ProfileBufferEntryReader::DoubleSpanOfConstBytes spans = + aER.ReadSpans(stringLength * sizeof(CHAR)); + if (MOZ_LIKELY(spans.IsSingleSpan()) && + reinterpret_cast<uintptr_t>(spans.mFirstOrOnly.Elements()) % + alignof(CHAR) == + 0u) { + // Only a single span, correctly aligned for the CHAR type, we can just + // refer to it directly, assuming that this ProfilerStringView will not + // outlive the chunk. + return ProfilerStringView<CHAR>( + reinterpret_cast<const CHAR*>(spans.mFirstOrOnly.Elements()), + stringLength, ProfilerStringView<CHAR>::Ownership::Reference); + } else { + // Two spans, we need to concatenate them; or one span, but misaligned. + // Allocate a buffer to store the string (plus terminal, for safety), and + // give it to the ProfilerStringView; Note that this is a secret use of + // ProfilerStringView, which is intended to only be used between + // deserialization and JSON streaming. + CHAR* buffer = new CHAR[stringLength + 1]; + spans.CopyBytesTo(buffer); + buffer[stringLength] = CHAR(0); + return ProfilerStringView<CHAR>( + buffer, stringLength, + ProfilerStringView<CHAR>::Ownership::OwnedThroughStringView); + } + } +}; + +// Serializer, Deserializer: MarkerCategory + +// The serialization contains both category numbers encoded as ULEB128. +template <> +struct ProfileBufferEntryWriter::Serializer<MarkerCategory> { + static Length Bytes(const MarkerCategory& aCategory) { + return ULEB128Size(static_cast<uint32_t>(aCategory.CategoryPair())); + } + + static void Write(ProfileBufferEntryWriter& aEW, + const MarkerCategory& aCategory) { + aEW.WriteULEB128(static_cast<uint32_t>(aCategory.CategoryPair())); + } +}; + +template <> +struct ProfileBufferEntryReader::Deserializer<MarkerCategory> { + static void ReadInto(ProfileBufferEntryReader& aER, + MarkerCategory& aCategory) { + aCategory = Read(aER); + } + + static MarkerCategory Read(ProfileBufferEntryReader& aER) { + return MarkerCategory(static_cast<baseprofiler::ProfilingCategoryPair>( + aER.ReadULEB128<uint32_t>())); + } +}; + +// ---------------------------------------------------------------------------- +// Serializer, Deserializer: MarkerTiming + +// The serialization starts with the marker phase, followed by one or two +// timestamps as needed. +template <> +struct ProfileBufferEntryWriter::Serializer<MarkerTiming> { + static Length Bytes(const MarkerTiming& aTiming) { + MOZ_ASSERT(!aTiming.IsUnspecified()); + const auto phase = aTiming.MarkerPhase(); + switch (phase) { + case MarkerTiming::Phase::Instant: + return SumBytes(phase, aTiming.StartTime()); + case MarkerTiming::Phase::Interval: + return SumBytes(phase, aTiming.StartTime(), aTiming.EndTime()); + case MarkerTiming::Phase::IntervalStart: + return SumBytes(phase, aTiming.StartTime()); + case MarkerTiming::Phase::IntervalEnd: + return SumBytes(phase, aTiming.EndTime()); + default: + MOZ_RELEASE_ASSERT(phase == MarkerTiming::Phase::Instant || + phase == MarkerTiming::Phase::Interval || + phase == MarkerTiming::Phase::IntervalStart || + phase == MarkerTiming::Phase::IntervalEnd); + return 0; // Only to avoid build errors. + } + } + + static void Write(ProfileBufferEntryWriter& aEW, + const MarkerTiming& aTiming) { + MOZ_ASSERT(!aTiming.IsUnspecified()); + const auto phase = aTiming.MarkerPhase(); + switch (phase) { + case MarkerTiming::Phase::Instant: + aEW.WriteObjects(phase, aTiming.StartTime()); + return; + case MarkerTiming::Phase::Interval: + aEW.WriteObjects(phase, aTiming.StartTime(), aTiming.EndTime()); + return; + case MarkerTiming::Phase::IntervalStart: + aEW.WriteObjects(phase, aTiming.StartTime()); + return; + case MarkerTiming::Phase::IntervalEnd: + aEW.WriteObjects(phase, aTiming.EndTime()); + return; + default: + MOZ_RELEASE_ASSERT(phase == MarkerTiming::Phase::Instant || + phase == MarkerTiming::Phase::Interval || + phase == MarkerTiming::Phase::IntervalStart || + phase == MarkerTiming::Phase::IntervalEnd); + return; + } + } +}; + +template <> +struct ProfileBufferEntryReader::Deserializer<MarkerTiming> { + static void ReadInto(ProfileBufferEntryReader& aER, MarkerTiming& aTiming) { + aTiming.mPhase = aER.ReadObject<MarkerTiming::Phase>(); + switch (aTiming.mPhase) { + case MarkerTiming::Phase::Instant: + aTiming.mStartTime = aER.ReadObject<TimeStamp>(); + aTiming.mEndTime = TimeStamp{}; + break; + case MarkerTiming::Phase::Interval: + aTiming.mStartTime = aER.ReadObject<TimeStamp>(); + aTiming.mEndTime = aER.ReadObject<TimeStamp>(); + break; + case MarkerTiming::Phase::IntervalStart: + aTiming.mStartTime = aER.ReadObject<TimeStamp>(); + aTiming.mEndTime = TimeStamp{}; + break; + case MarkerTiming::Phase::IntervalEnd: + aTiming.mStartTime = TimeStamp{}; + aTiming.mEndTime = aER.ReadObject<TimeStamp>(); + break; + default: + MOZ_RELEASE_ASSERT(aTiming.mPhase == MarkerTiming::Phase::Instant || + aTiming.mPhase == MarkerTiming::Phase::Interval || + aTiming.mPhase == + MarkerTiming::Phase::IntervalStart || + aTiming.mPhase == MarkerTiming::Phase::IntervalEnd); + break; + } + } + + static MarkerTiming Read(ProfileBufferEntryReader& aER) { + TimeStamp start; + TimeStamp end; + auto phase = aER.ReadObject<MarkerTiming::Phase>(); + switch (phase) { + case MarkerTiming::Phase::Instant: + start = aER.ReadObject<TimeStamp>(); + break; + case MarkerTiming::Phase::Interval: + start = aER.ReadObject<TimeStamp>(); + end = aER.ReadObject<TimeStamp>(); + break; + case MarkerTiming::Phase::IntervalStart: + start = aER.ReadObject<TimeStamp>(); + break; + case MarkerTiming::Phase::IntervalEnd: + end = aER.ReadObject<TimeStamp>(); + break; + default: + MOZ_RELEASE_ASSERT(phase == MarkerTiming::Phase::Instant || + phase == MarkerTiming::Phase::Interval || + phase == MarkerTiming::Phase::IntervalStart || + phase == MarkerTiming::Phase::IntervalEnd); + break; + } + return MarkerTiming(start, end, phase); + } +}; + +// ---------------------------------------------------------------------------- +// Serializer, Deserializer: MarkerStack + +// The serialization only contains the `ProfileChunkedBuffer` from the +// backtrace; if there is no backtrace or if it's empty, this will implicitly +// store a nullptr (see +// `ProfileBufferEntryWriter::Serializer<ProfilerChunkedBuffer*>`). +template <> +struct ProfileBufferEntryWriter::Serializer<MarkerStack> { + static Length Bytes(const MarkerStack& aStack) { + return SumBytes(aStack.GetChunkedBuffer()); + } + + static void Write(ProfileBufferEntryWriter& aEW, const MarkerStack& aStack) { + aEW.WriteObject(aStack.GetChunkedBuffer()); + } +}; + +template <> +struct ProfileBufferEntryReader::Deserializer<MarkerStack> { + static void ReadInto(ProfileBufferEntryReader& aER, MarkerStack& aStack) { + aStack = Read(aER); + } + + static MarkerStack Read(ProfileBufferEntryReader& aER) { + return MarkerStack(aER.ReadObject<UniquePtr<ProfileChunkedBuffer>>()); + } +}; + +// ---------------------------------------------------------------------------- +// Serializer, Deserializer: MarkerOptions + +// The serialization contains all members (either trivially-copyable, or they +// provide their specialization above). +template <> +struct ProfileBufferEntryWriter::Serializer<MarkerOptions> { + static Length Bytes(const MarkerOptions& aOptions) { + return SumBytes(aOptions.ThreadId(), aOptions.Timing(), aOptions.Stack(), + aOptions.InnerWindowId()); + } + + static void Write(ProfileBufferEntryWriter& aEW, + const MarkerOptions& aOptions) { + aEW.WriteObjects(aOptions.ThreadId(), aOptions.Timing(), aOptions.Stack(), + aOptions.InnerWindowId()); + } +}; + +template <> +struct ProfileBufferEntryReader::Deserializer<MarkerOptions> { + static void ReadInto(ProfileBufferEntryReader& aER, MarkerOptions& aOptions) { + aER.ReadIntoObjects(aOptions.mThreadId, aOptions.mTiming, aOptions.mStack, + aOptions.mInnerWindowId); + } + + static MarkerOptions Read(ProfileBufferEntryReader& aER) { + MarkerOptions options; + ReadInto(aER, options); + return options; + } +}; + +} // namespace mozilla + +#endif // BaseProfilerMarkersDetail_h diff --git a/mozglue/baseprofiler/public/BaseProfilerMarkersPrerequisites.h b/mozglue/baseprofiler/public/BaseProfilerMarkersPrerequisites.h new file mode 100644 index 0000000000..15fd729d0c --- /dev/null +++ b/mozglue/baseprofiler/public/BaseProfilerMarkersPrerequisites.h @@ -0,0 +1,886 @@ +/* -*- Mode: C++; tab-width: 2; 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/. */ + +// This header contains basic definitions required to create marker types, and +// to add markers to the profiler buffers. +// +// In most cases, #include "mozilla/BaseProfilerMarkers.h" instead, or +// #include "mozilla/BaseProfilerMarkerTypes.h" for common marker types. + +#ifndef BaseProfilerMarkersPrerequisites_h +#define BaseProfilerMarkersPrerequisites_h + +namespace mozilla { + +enum class StackCaptureOptions { + NoStack, // No stack captured. + Full, // Capture a full stack, including label frames, JS frames and + // native frames. + NonNative, // Capture a stack without native frames for reduced overhead. +}; + +} + +#include "BaseProfilingCategory.h" +#include "mozilla/Maybe.h" +#include "mozilla/ProfileChunkedBuffer.h" +#include "mozilla/BaseProfilerState.h" +#include "mozilla/TimeStamp.h" +#include "mozilla/UniquePtr.h" +#include "mozilla/Variant.h" + +#include <initializer_list> +#include <string_view> +#include <string> +#include <type_traits> +#include <utility> +#include <vector> + +namespace mozilla { + +// Return a NotNull<const CHAR*> pointing at the literal empty string `""`. +template <typename CHAR> +constexpr const CHAR* LiteralEmptyStringPointer() { + static_assert(std::is_same_v<CHAR, char> || std::is_same_v<CHAR, char16_t>, + "Only char and char16_t are supported in Firefox"); + if constexpr (std::is_same_v<CHAR, char>) { + return ""; + } + if constexpr (std::is_same_v<CHAR, char16_t>) { + return u""; + } +} + +// Return a string_view<CHAR> pointing at the literal empty string. +template <typename CHAR> +constexpr std::basic_string_view<CHAR> LiteralEmptyStringView() { + static_assert(std::is_same_v<CHAR, char> || std::is_same_v<CHAR, char16_t>, + "Only char and char16_t are supported in Firefox"); + // Use `operator""sv()` from <string_view>. + using namespace std::literals::string_view_literals; + if constexpr (std::is_same_v<CHAR, char>) { + return ""sv; + } + if constexpr (std::is_same_v<CHAR, char16_t>) { + return u""sv; + } +} + +// General string view, optimized for short on-stack life before serialization, +// and between deserialization and JSON-streaming. +template <typename CHAR> +class MOZ_STACK_CLASS ProfilerStringView { + public: + // Default constructor points at "" (literal empty string). + constexpr ProfilerStringView() = default; + + // Don't allow copy. + ProfilerStringView(const ProfilerStringView&) = delete; + ProfilerStringView& operator=(const ProfilerStringView&) = delete; + + // Allow move. For consistency the moved-from string is always reset to "". + constexpr ProfilerStringView(ProfilerStringView&& aOther) + : mStringView(std::move(aOther.mStringView)), + mOwnership(aOther.mOwnership) { + if (mOwnership == Ownership::OwnedThroughStringView) { + // We now own the buffer, make the other point at the literal "". + aOther.mStringView = LiteralEmptyStringView<CHAR>(); + aOther.mOwnership = Ownership::Literal; + } + } + constexpr ProfilerStringView& operator=(ProfilerStringView&& aOther) { + mStringView = std::move(aOther.mStringView); + mOwnership = aOther.mOwnership; + if (mOwnership == Ownership::OwnedThroughStringView) { + // We now own the buffer, make the other point at the literal "". + aOther.mStringView = LiteralEmptyStringView<CHAR>(); + aOther.mOwnership = Ownership::Literal; + } + return *this; + } + + ~ProfilerStringView() { + if (MOZ_UNLIKELY(mOwnership == Ownership::OwnedThroughStringView)) { + // We own the buffer pointed at by mStringView, destroy it. + // This is only used between deserialization and streaming. + delete mStringView.data(); + } + } + + // Implicit construction from nullptr, points at "" (literal empty string). + constexpr MOZ_IMPLICIT ProfilerStringView(decltype(nullptr)) {} + + // Implicit constructor from a literal string. + template <size_t Np1> + constexpr MOZ_IMPLICIT ProfilerStringView(const CHAR (&aLiteralString)[Np1]) + : ProfilerStringView(aLiteralString, Np1 - 1, Ownership::Literal) {} + + // Constructor from a non-literal string. + constexpr ProfilerStringView(const CHAR* aString, size_t aLength) + : ProfilerStringView(aString, aLength, Ownership::Reference) {} + + // Implicit constructor from a string_view. + constexpr MOZ_IMPLICIT ProfilerStringView( + const std::basic_string_view<CHAR>& aStringView) + : ProfilerStringView(aStringView.data(), aStringView.length(), + Ownership::Reference) {} + + // Implicit constructor from an expiring string_view. We assume that the + // pointed-at string will outlive this ProfilerStringView. + constexpr MOZ_IMPLICIT ProfilerStringView( + std::basic_string_view<CHAR>&& aStringView) + : ProfilerStringView(aStringView.data(), aStringView.length(), + Ownership::Reference) {} + + // Implicit constructor from std::string. + constexpr MOZ_IMPLICIT ProfilerStringView( + const std::basic_string<CHAR>& aString) + : ProfilerStringView(aString.data(), aString.length(), + Ownership::Reference) {} + + // Construction from a raw pointer to a null-terminated string. + // This is a named class-static function to make it more obvious where work is + // being done (to determine the string length), and encourage users to instead + // provide a length, if already known. + // TODO: Find callers and convert them to constructor instead if possible. + static constexpr ProfilerStringView WrapNullTerminatedString( + const CHAR* aString) { + return ProfilerStringView( + aString, aString ? std::char_traits<CHAR>::length(aString) : 0, + Ownership::Reference); + } + + // Implicit constructor for an object with member functions `Data()` + // `Length()`, and `IsLiteral()`, common in xpcom strings. + template < + typename String, + typename DataReturnType = decltype(std::declval<const String>().Data()), + typename LengthReturnType = + decltype(std::declval<const String>().Length()), + typename IsLiteralReturnType = + decltype(std::declval<const String>().IsLiteral()), + typename = + std::enable_if_t<std::is_convertible_v<DataReturnType, const CHAR*> && + std::is_integral_v<LengthReturnType> && + std::is_same_v<IsLiteralReturnType, bool>>> + constexpr MOZ_IMPLICIT ProfilerStringView(const String& aString) + : ProfilerStringView( + static_cast<const CHAR*>(aString.Data()), aString.Length(), + aString.IsLiteral() ? Ownership::Literal : Ownership::Reference) {} + + [[nodiscard]] constexpr const std::basic_string_view<CHAR>& StringView() + const { + return mStringView; + } + + [[nodiscard]] constexpr size_t Length() const { return mStringView.length(); } + + [[nodiscard]] constexpr bool IsLiteral() const { + return mOwnership == Ownership::Literal; + } + [[nodiscard]] constexpr bool IsReference() const { + return mOwnership == Ownership::Reference; + } + // No `IsOwned...()` because it's a secret, only used internally! + + [[nodiscard]] Span<const CHAR> AsSpan() const { + return Span<const CHAR>(mStringView.data(), mStringView.length()); + } + [[nodiscard]] operator Span<const CHAR>() const { return AsSpan(); } + + private: + enum class Ownership { Literal, Reference, OwnedThroughStringView }; + + // Allow deserializer to store anything here. + friend ProfileBufferEntryReader::Deserializer<ProfilerStringView>; + + constexpr ProfilerStringView(const CHAR* aString, size_t aLength, + Ownership aOwnership) + : mStringView(aString ? std::basic_string_view<CHAR>(aString, aLength) + : LiteralEmptyStringView<CHAR>()), + mOwnership(aString ? aOwnership : Ownership::Literal) {} + + // String view to an outside string (literal or reference). + // We may actually own the pointed-at buffer, but it is only used internally + // between deserialization and JSON streaming. + std::basic_string_view<CHAR> mStringView = LiteralEmptyStringView<CHAR>(); + + Ownership mOwnership = Ownership::Literal; +}; + +using ProfilerString8View = ProfilerStringView<char>; +using ProfilerString16View = ProfilerStringView<char16_t>; + +// This compulsory marker parameter contains the required category information. +class MarkerCategory { + public: + // Constructor from category pair (includes both super- and sub-categories). + constexpr explicit MarkerCategory( + baseprofiler::ProfilingCategoryPair aCategoryPair) + : mCategoryPair(aCategoryPair) {} + + // Returns the stored category pair. + constexpr baseprofiler::ProfilingCategoryPair CategoryPair() const { + return mCategoryPair; + } + + // Returns the super-category from the stored category pair. + baseprofiler::ProfilingCategory GetCategory() const { + return GetProfilingCategoryPairInfo(mCategoryPair).mCategory; + } + + private: + baseprofiler::ProfilingCategoryPair mCategoryPair = + baseprofiler::ProfilingCategoryPair::OTHER; +}; + +namespace baseprofiler::category { + +// Each category pair name constructs a MarkerCategory. +// E.g.: mozilla::baseprofiler::category::OTHER_Profiling +// Profiler macros will take the category name alone without namespace. +// E.g.: `PROFILER_MARKER_UNTYPED("name", OTHER_Profiling)` +#define CATEGORY_ENUM_BEGIN_CATEGORY(name, labelAsString, color) +#define CATEGORY_ENUM_SUBCATEGORY(supercategory, name, labelAsString) \ + static constexpr MarkerCategory name{ProfilingCategoryPair::name}; +#define CATEGORY_ENUM_END_CATEGORY +MOZ_PROFILING_CATEGORY_LIST(CATEGORY_ENUM_BEGIN_CATEGORY, + CATEGORY_ENUM_SUBCATEGORY, + CATEGORY_ENUM_END_CATEGORY) +#undef CATEGORY_ENUM_BEGIN_CATEGORY +#undef CATEGORY_ENUM_SUBCATEGORY +#undef CATEGORY_ENUM_END_CATEGORY + +// Import `MarkerCategory` into this namespace. This will allow using this type +// dynamically in macros that prepend `::mozilla::baseprofiler::category::` to +// the given category, e.g.: +// `PROFILER_MARKER_UNTYPED("name", MarkerCategory(...))` +using MarkerCategory = ::mozilla::MarkerCategory; + +} // namespace baseprofiler::category + +// The classes below are all embedded in a `MarkerOptions` object. +class MarkerOptions; + +// This marker option captures a given thread id. +// If left unspecified (by default construction) during the add-marker call, the +// current thread id will be used then. +class MarkerThreadId { + public: + // Default constructor, keeps the thread id unspecified. + constexpr MarkerThreadId() = default; + + // Constructor from a given thread id. + constexpr explicit MarkerThreadId( + baseprofiler::BaseProfilerThreadId aThreadId) + : mThreadId(aThreadId) {} + + // Use the current thread's id. + static MarkerThreadId CurrentThread() { + return MarkerThreadId(baseprofiler::profiler_current_thread_id()); + } + + // Use the main thread's id. This can be useful to record a marker from a + // possibly-unregistered thread, and display it in the main thread track. + static MarkerThreadId MainThread() { + return MarkerThreadId(baseprofiler::profiler_main_thread_id()); + } + + [[nodiscard]] constexpr baseprofiler::BaseProfilerThreadId ThreadId() const { + return mThreadId; + } + + [[nodiscard]] constexpr bool IsUnspecified() const { + return !mThreadId.IsSpecified(); + } + + private: + baseprofiler::BaseProfilerThreadId mThreadId; +}; + +// This marker option contains marker timing information. +// This class encapsulates the logic for correctly storing a marker based on its +// Use the static methods to create the MarkerTiming. This is a transient object +// that is being used to enforce the constraints of the combinations of the +// data. +class MarkerTiming { + public: + // The following static methods are used to create the MarkerTiming based on + // the type that it is. + + static MarkerTiming InstantAt(const TimeStamp& aTime) { + MOZ_ASSERT(!aTime.IsNull(), "Time is null for an instant marker."); + return MarkerTiming{aTime, TimeStamp{}, MarkerTiming::Phase::Instant}; + } + + static MarkerTiming InstantNow() { return InstantAt(TimeStamp::Now()); } + + static MarkerTiming Interval(const TimeStamp& aStartTime, + const TimeStamp& aEndTime) { + MOZ_ASSERT(!aStartTime.IsNull(), + "Start time is null for an interval marker."); + MOZ_ASSERT(!aEndTime.IsNull(), "End time is null for an interval marker."); + return MarkerTiming{aStartTime, aEndTime, MarkerTiming::Phase::Interval}; + } + + static MarkerTiming IntervalUntilNowFrom(const TimeStamp& aStartTime) { + return Interval(aStartTime, TimeStamp::Now()); + } + + static MarkerTiming IntervalStart(const TimeStamp& aTime = TimeStamp::Now()) { + MOZ_ASSERT(!aTime.IsNull(), "Time is null for an interval start marker."); + return MarkerTiming{aTime, TimeStamp{}, MarkerTiming::Phase::IntervalStart}; + } + + static MarkerTiming IntervalEnd(const TimeStamp& aTime = TimeStamp::Now()) { + MOZ_ASSERT(!aTime.IsNull(), "Time is null for an interval end marker."); + return MarkerTiming{TimeStamp{}, aTime, MarkerTiming::Phase::IntervalEnd}; + } + + // Set the interval end in this timing. + // If there was already a start time, this makes it a full interval. + void SetIntervalEnd(const TimeStamp& aTime = TimeStamp::Now()) { + MOZ_ASSERT(!aTime.IsNull(), "Time is null for an interval end marker."); + mEndTime = aTime; + mPhase = mStartTime.IsNull() ? Phase::IntervalEnd : Phase::Interval; + } + + [[nodiscard]] const TimeStamp& StartTime() const { return mStartTime; } + [[nodiscard]] const TimeStamp& EndTime() const { return mEndTime; } + + enum class Phase : uint8_t { + Instant = 0, + Interval = 1, + IntervalStart = 2, + IntervalEnd = 3, + }; + + [[nodiscard]] Phase MarkerPhase() const { + MOZ_ASSERT(!IsUnspecified()); + return mPhase; + } + + // The following getter methods are used to put the value into the buffer for + // storage. + [[nodiscard]] double GetStartTime() const { + MOZ_ASSERT(!IsUnspecified()); + // If mStartTime is null (e.g., for IntervalEnd), this will output 0.0 as + // expected. + return MarkerTiming::timeStampToDouble(mStartTime); + } + + [[nodiscard]] double GetEndTime() const { + MOZ_ASSERT(!IsUnspecified()); + // If mEndTime is null (e.g., for Instant or IntervalStart), this will + // output 0.0 as expected. + return MarkerTiming::timeStampToDouble(mEndTime); + } + + [[nodiscard]] uint8_t GetPhase() const { + MOZ_ASSERT(!IsUnspecified()); + return static_cast<uint8_t>(mPhase); + } + + // This is a constructor for Rust FFI bindings. It must not be used outside of + // this! Please see the other static constructors above. + static void UnsafeConstruct(MarkerTiming* aMarkerTiming, + const TimeStamp& aStartTime, + const TimeStamp& aEndTime, Phase aPhase) { + new (aMarkerTiming) MarkerTiming{aStartTime, aEndTime, aPhase}; + } + + private: + friend ProfileBufferEntryWriter::Serializer<MarkerTiming>; + friend ProfileBufferEntryReader::Deserializer<MarkerTiming>; + friend MarkerOptions; + + // Default timing leaves it internally "unspecified", serialization getters + // and add-marker functions will default to `InstantNow()`. + constexpr MarkerTiming() = default; + + // This should only be used by internal profiler code. + [[nodiscard]] bool IsUnspecified() const { + return mStartTime.IsNull() && mEndTime.IsNull(); + } + + // Full constructor, used by static factory functions. + constexpr MarkerTiming(const TimeStamp& aStartTime, const TimeStamp& aEndTime, + Phase aPhase) + : mStartTime(aStartTime), mEndTime(aEndTime), mPhase(aPhase) {} + + static double timeStampToDouble(const TimeStamp& time) { + if (time.IsNull()) { + // The Phase lets us know not to use this value. + return 0; + } + return (time - TimeStamp::ProcessCreation()).ToMilliseconds(); + } + + TimeStamp mStartTime; + TimeStamp mEndTime; + Phase mPhase = Phase::Instant; +}; + +// This marker option allows three cases: +// - By default, no stacks are captured. +// - The caller can request a stack capture, and the add-marker code will take +// care of it in the most efficient way. +// - The caller can still provide an existing backtrace, for cases where a +// marker reports something that happened elsewhere. +class MarkerStack { + public: + // Default constructor, no capture. + constexpr MarkerStack() = default; + + // Disallow copy. + MarkerStack(const MarkerStack&) = delete; + MarkerStack& operator=(const MarkerStack&) = delete; + + // Allow move. + MarkerStack(MarkerStack&& aOther) + : mCaptureOptions(aOther.mCaptureOptions), + mOptionalChunkedBufferStorage( + std::move(aOther.mOptionalChunkedBufferStorage)), + mChunkedBuffer(aOther.mChunkedBuffer) { + AssertInvariants(); + aOther.Clear(); + } + MarkerStack& operator=(MarkerStack&& aOther) { + mCaptureOptions = aOther.mCaptureOptions; + mOptionalChunkedBufferStorage = + std::move(aOther.mOptionalChunkedBufferStorage); + mChunkedBuffer = aOther.mChunkedBuffer; + AssertInvariants(); + aOther.Clear(); + return *this; + } + + // Take ownership of a backtrace. If null or empty, equivalent to NoStack(). + explicit MarkerStack(UniquePtr<ProfileChunkedBuffer>&& aExternalChunkedBuffer) + : mOptionalChunkedBufferStorage( + (!aExternalChunkedBuffer || aExternalChunkedBuffer->IsEmpty()) + ? nullptr + : std::move(aExternalChunkedBuffer)), + mChunkedBuffer(mOptionalChunkedBufferStorage.get()) { + AssertInvariants(); + } + + // Use an existing backtrace stored elsewhere, which the user must guarantee + // is alive during the add-marker call. If empty, equivalent to NoStack(). + explicit MarkerStack(ProfileChunkedBuffer& aExternalChunkedBuffer) + : mChunkedBuffer(aExternalChunkedBuffer.IsEmpty() + ? nullptr + : &aExternalChunkedBuffer) { + AssertInvariants(); + } + + // Don't capture a stack in this marker. + static MarkerStack NoStack() { + return MarkerStack(StackCaptureOptions::NoStack); + } + + // Capture a stack when adding this marker. + static MarkerStack Capture( + StackCaptureOptions aCaptureOptions = StackCaptureOptions::Full) { + // Actual capture will be handled inside profiler_add_marker. + return MarkerStack(aCaptureOptions); + } + + // Optionally capture a stack, useful for avoiding long-winded ternaries. + static MarkerStack MaybeCapture(bool aDoCapture) { + return aDoCapture ? Capture() : NoStack(); + } + + // Use an existing backtrace stored elsewhere, which the user must guarantee + // is alive during the add-marker call. If empty, equivalent to NoStack(). + static MarkerStack UseBacktrace( + ProfileChunkedBuffer& aExternalChunkedBuffer) { + return MarkerStack(aExternalChunkedBuffer); + } + + // Take ownership of a backtrace previously captured with + // `profiler_capture_backtrace()`. If null, equivalent to NoStack(). + static MarkerStack TakeBacktrace( + UniquePtr<ProfileChunkedBuffer>&& aExternalChunkedBuffer) { + return MarkerStack(std::move(aExternalChunkedBuffer)); + } + + // Construct with the given capture options. + static MarkerStack WithCaptureOptions(StackCaptureOptions aCaptureOptions) { + return MarkerStack(aCaptureOptions); + } + + [[nodiscard]] StackCaptureOptions CaptureOptions() const { + return mCaptureOptions; + } + + ProfileChunkedBuffer* GetChunkedBuffer() const { return mChunkedBuffer; } + + // Use backtrace after a request. If null, equivalent to NoStack(). + void UseRequestedBacktrace(ProfileChunkedBuffer* aExternalChunkedBuffer) { + MOZ_RELEASE_ASSERT(mCaptureOptions != StackCaptureOptions::NoStack); + mCaptureOptions = StackCaptureOptions::NoStack; + if (aExternalChunkedBuffer && !aExternalChunkedBuffer->IsEmpty()) { + // We only need to use the provided buffer if it is not empty. + mChunkedBuffer = aExternalChunkedBuffer; + } + AssertInvariants(); + } + + void Clear() { + mCaptureOptions = StackCaptureOptions::NoStack; + mOptionalChunkedBufferStorage.reset(); + mChunkedBuffer = nullptr; + AssertInvariants(); + } + + private: + explicit MarkerStack(StackCaptureOptions aCaptureOptions) + : mCaptureOptions(aCaptureOptions) { + AssertInvariants(); + } + + // This should be called after every constructor and non-const function. + void AssertInvariants() const { +#ifdef DEBUG + if (mCaptureOptions != StackCaptureOptions::NoStack) { + MOZ_ASSERT(!mOptionalChunkedBufferStorage, + "We should not hold a buffer when capture is requested"); + MOZ_ASSERT(!mChunkedBuffer, + "We should not point at a buffer when capture is requested"); + } else { + if (mOptionalChunkedBufferStorage) { + MOZ_ASSERT(mChunkedBuffer == mOptionalChunkedBufferStorage.get(), + "Non-null mOptionalChunkedBufferStorage must be pointed-at " + "by mChunkedBuffer"); + } + if (mChunkedBuffer) { + MOZ_ASSERT(!mChunkedBuffer->IsEmpty(), + "Non-null mChunkedBuffer must not be empty"); + } + } +#endif // DEBUG + } + + StackCaptureOptions mCaptureOptions = StackCaptureOptions::NoStack; + + // Optional storage for the backtrace, in case it was captured before the + // add-marker call. + UniquePtr<ProfileChunkedBuffer> mOptionalChunkedBufferStorage; + + // If not null, this points to the backtrace. It may point to a backtrace + // temporarily stored on the stack, or to mOptionalChunkedBufferStorage. + ProfileChunkedBuffer* mChunkedBuffer = nullptr; +}; + +// This marker option captures a given inner window id. +class MarkerInnerWindowId { + public: + // Default constructor, it leaves the id unspecified. + constexpr MarkerInnerWindowId() = default; + + // Constructor with a specified inner window id. + constexpr explicit MarkerInnerWindowId(uint64_t i) : mInnerWindowId(i) {} + + // Constructor with either specified inner window id or Nothing. + constexpr explicit MarkerInnerWindowId(const Maybe<uint64_t>& i) + : mInnerWindowId(i.valueOr(scNoId)) {} + + // Explicit option with unspecified id. + constexpr static MarkerInnerWindowId NoId() { return MarkerInnerWindowId{}; } + + [[nodiscard]] bool IsUnspecified() const { return mInnerWindowId == scNoId; } + + [[nodiscard]] constexpr uint64_t Id() const { return mInnerWindowId; } + + private: + static constexpr uint64_t scNoId = 0; + uint64_t mInnerWindowId = scNoId; +}; + +// This class combines each of the possible marker options above. +class MarkerOptions { + public: + // Constructor from individual options (including none). + // Implicit to allow `{}` and one option type as-is. + // Options that are not provided here are defaulted. In particular, timing + // defaults to `MarkerTiming::InstantNow()` when the marker is recorded. + template <typename... Options> + MOZ_IMPLICIT MarkerOptions(Options&&... aOptions) { + (Set(std::forward<Options>(aOptions)), ...); + } + + // Disallow copy. + MarkerOptions(const MarkerOptions&) = delete; + MarkerOptions& operator=(const MarkerOptions&) = delete; + + // Allow move. + MarkerOptions(MarkerOptions&&) = default; + MarkerOptions& operator=(MarkerOptions&&) = default; + + // The embedded `MarkerTiming` hasn't been specified yet. + [[nodiscard]] bool IsTimingUnspecified() const { + return mTiming.IsUnspecified(); + } + + // Each option may be added in a chain by e.g.: + // `options.Set(MarkerThreadId(123)).Set(MarkerTiming::IntervalEnd())`. + // When passed to an add-marker function, it must be an rvalue, either created + // on the spot, or `std::move`d from storage, e.g.: + // `PROFILER_MARKER_UNTYPED("...", std::move(options).Set(...))`; + // + // Options can be read by their name (without "Marker"), e.g.: `o.ThreadId()`. + // Add "Ref" for a non-const reference, e.g.: `o.ThreadIdRef() = ...;` +#define FUNCTIONS_ON_MEMBER(NAME) \ + MarkerOptions& Set(Marker##NAME&& a##NAME)& { \ + m##NAME = std::move(a##NAME); \ + return *this; \ + } \ + \ + MarkerOptions&& Set(Marker##NAME&& a##NAME)&& { \ + m##NAME = std::move(a##NAME); \ + return std::move(*this); \ + } \ + \ + const Marker##NAME& NAME() const { return m##NAME; } \ + \ + Marker##NAME& NAME##Ref() { return m##NAME; } + + FUNCTIONS_ON_MEMBER(ThreadId); + FUNCTIONS_ON_MEMBER(Timing); + FUNCTIONS_ON_MEMBER(Stack); + FUNCTIONS_ON_MEMBER(InnerWindowId); +#undef FUNCTIONS_ON_MEMBER + + private: + friend ProfileBufferEntryReader::Deserializer<MarkerOptions>; + + MarkerThreadId mThreadId; + MarkerTiming mTiming; + MarkerStack mStack; + MarkerInnerWindowId mInnerWindowId; +}; + +} // namespace mozilla + +namespace mozilla::baseprofiler::markers { + +// Default marker payload types, with no extra information, not even a marker +// type and payload. This is intended for label-only markers. +struct NoPayload final {}; + +} // namespace mozilla::baseprofiler::markers + +namespace mozilla { + +class JSONWriter; + +// This class collects all the information necessary to stream the JSON schema +// that informs the front-end how to display a type of markers. +// It will be created and populated in `MarkerTypeDisplay()` functions in each +// marker type definition, see Add/Set functions. +class MarkerSchema { + public: + enum class Location : unsigned { + MarkerChart, + MarkerTable, + // This adds markers to the main marker timeline in the header. + TimelineOverview, + // In the timeline, this is a section that breaks out markers that are + // related to memory. When memory counters are enabled, this is its own + // track, otherwise it is displayed with the main thread. + TimelineMemory, + // This adds markers to the IPC timeline area in the header. + TimelineIPC, + // This adds markers to the FileIO timeline area in the header. + TimelineFileIO, + // TODO - This is not supported yet. + StackChart + }; + + // Used as constructor parameter, to explicitly specify that the location (and + // other display options) are handled as a special case in the front-end. + // In this case, *no* schema will be output for this type. + struct SpecialFrontendLocation {}; + + enum class Format { + // ---------------------------------------------------- + // String types. + + // Show the URL, and handle PII sanitization + Url, + // Show the file path, and handle PII sanitization. + FilePath, + // Important, do not put URL or file path information here, as it will not + // be sanitized. Please be careful with including other types of PII here as + // well. + // e.g. "Label: Some String" + String, + + // ---------------------------------------------------- + // Numeric types + + // For time data that represents a duration of time. + // e.g. "Label: 5s, 5ms, 5μs" + Duration, + // Data that happened at a specific time, relative to the start of the + // profile. e.g. "Label: 15.5s, 20.5ms, 30.5μs" + Time, + // The following are alternatives to display a time only in a specific unit + // of time. + Seconds, // "Label: 5s" + Milliseconds, // "Label: 5ms" + Microseconds, // "Label: 5μs" + Nanoseconds, // "Label: 5ns" + // e.g. "Label: 5.55mb, 5 bytes, 312.5kb" + Bytes, + // This should be a value between 0 and 1. + // "Label: 50%" + Percentage, + // The integer should be used for generic representations of numbers. + // Do not use it for time information. + // "Label: 52, 5,323, 1,234,567" + Integer, + // The decimal should be used for generic representations of numbers. + // Do not use it for time information. + // "Label: 52.23, 0.0054, 123,456.78" + Decimal + }; + + enum class Searchable { NotSearchable, Searchable }; + + // Marker schema, with a non-empty list of locations where markers should be + // shown. + // Tech note: Even though `aLocations` are templated arguments, they are + // assigned to an `enum class` object, so they can only be of that enum type. + template <typename... Locations> + explicit MarkerSchema(Location aLocation, Locations... aLocations) + : mLocations{aLocation, aLocations...} {} + + // Alternative constructor for MarkerSchema. + explicit MarkerSchema(const mozilla::MarkerSchema::Location* aLocations, + size_t aLength) + : mLocations(aLocations, aLocations + aLength) {} + + // Marker schema for types that have special frontend handling. + // Nothing else should be set in this case. + // Implicit to allow quick return from MarkerTypeDisplay functions. + MOZ_IMPLICIT MarkerSchema(SpecialFrontendLocation) {} + + // Caller must specify location(s) or SpecialFrontendLocation above. + MarkerSchema() = delete; + + // Optional labels in the marker chart, the chart tooltip, and the marker + // table. If not provided, the marker "name" will be used. The given string + // can contain element keys in braces to include data elements streamed by + // `StreamJSONMarkerData()`. E.g.: "This is {text}" + +#define LABEL_SETTER(name) \ + MarkerSchema& Set##name(std::string a##name) { \ + m##name = std::move(a##name); \ + return *this; \ + } + + LABEL_SETTER(ChartLabel) + LABEL_SETTER(TooltipLabel) + LABEL_SETTER(TableLabel) + +#undef LABEL_SETTER + + MarkerSchema& SetAllLabels(std::string aText) { + // Here we set the same text in each label. + // TODO: Move to a single "label" field once the front-end allows it. + SetChartLabel(aText); + SetTooltipLabel(aText); + SetTableLabel(std::move(aText)); + return *this; + } + + // Each data element that is streamed by `StreamJSONMarkerData()` can be + // displayed as indicated by using one of the `Add...` function below. + // Each `Add...` will add a line in the full marker description. Parameters: + // - `aKey`: Element property name as streamed by `StreamJSONMarkerData()`. + // - `aLabel`: Optional prefix. Defaults to the key name. + // - `aFormat`: How to format the data element value, see `Format` above. + // - `aSearchable`: Optional, indicates if the value is used in searches, + // defaults to false. + + MarkerSchema& AddKeyFormat(std::string aKey, Format aFormat) { + mData.emplace_back(mozilla::VariantType<DynamicData>{}, + DynamicData{std::move(aKey), mozilla::Nothing{}, aFormat, + mozilla::Nothing{}}); + return *this; + } + + MarkerSchema& AddKeyLabelFormat(std::string aKey, std::string aLabel, + Format aFormat) { + mData.emplace_back( + mozilla::VariantType<DynamicData>{}, + DynamicData{std::move(aKey), mozilla::Some(std::move(aLabel)), aFormat, + mozilla::Nothing{}}); + return *this; + } + + MarkerSchema& AddKeyFormatSearchable(std::string aKey, Format aFormat, + Searchable aSearchable) { + mData.emplace_back(mozilla::VariantType<DynamicData>{}, + DynamicData{std::move(aKey), mozilla::Nothing{}, aFormat, + mozilla::Some(aSearchable)}); + return *this; + } + + MarkerSchema& AddKeyLabelFormatSearchable(std::string aKey, + std::string aLabel, Format aFormat, + Searchable aSearchable) { + mData.emplace_back( + mozilla::VariantType<DynamicData>{}, + DynamicData{std::move(aKey), mozilla::Some(std::move(aLabel)), aFormat, + mozilla::Some(aSearchable)}); + return *this; + } + + // The display may also include static rows. + + MarkerSchema& AddStaticLabelValue(std::string aLabel, std::string aValue) { + mData.emplace_back(mozilla::VariantType<StaticData>{}, + StaticData{std::move(aLabel), std::move(aValue)}); + return *this; + } + + // Internal streaming function. + MFBT_API void Stream(JSONWriter& aWriter, const Span<const char>& aName) &&; + + private: + MFBT_API static Span<const char> LocationToStringSpan(Location aLocation); + MFBT_API static Span<const char> FormatToStringSpan(Format aFormat); + + // List of marker display locations. Empty for SpecialFrontendLocation. + std::vector<Location> mLocations; + // Labels for different places. + std::string mChartLabel; + std::string mTooltipLabel; + std::string mTableLabel; + // Main display, made of zero or more rows of key+label+format or label+value. + private: + struct DynamicData { + std::string mKey; + mozilla::Maybe<std::string> mLabel; + Format mFormat; + mozilla::Maybe<Searchable> mSearchable; + }; + struct StaticData { + std::string mLabel; + std::string mValue; + }; + using DataRow = mozilla::Variant<DynamicData, StaticData>; + using DataRowVector = std::vector<DataRow>; + + DataRowVector mData; +}; + +} // namespace mozilla + +#endif // BaseProfilerMarkersPrerequisites_h diff --git a/mozglue/baseprofiler/public/BaseProfilerRAIIMacro.h b/mozglue/baseprofiler/public/BaseProfilerRAIIMacro.h new file mode 100644 index 0000000000..b89f7e1f77 --- /dev/null +++ b/mozglue/baseprofiler/public/BaseProfilerRAIIMacro.h @@ -0,0 +1,15 @@ +/* -*- Mode: C++; tab-width: 2; 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/. */ + +#ifndef BaseProfilerRAIIMacro_h +#define BaseProfilerRAIIMacro_h + +// Macros used by the AUTO_PROFILER_* macros to generate unique variable names. +#define PROFILER_RAII_PASTE(id, line) id##line +#define PROFILER_RAII_EXPAND(id, line) PROFILER_RAII_PASTE(id, line) +#define PROFILER_RAII PROFILER_RAII_EXPAND(raiiObject, __LINE__) + +#endif // BaseProfilerRAIIMacro_h diff --git a/mozglue/baseprofiler/public/BaseProfilerSharedLibraries.h b/mozglue/baseprofiler/public/BaseProfilerSharedLibraries.h new file mode 100644 index 0000000000..705ca55f4b --- /dev/null +++ b/mozglue/baseprofiler/public/BaseProfilerSharedLibraries.h @@ -0,0 +1,170 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim:set ts=2 sw=2 sts=2 et cindent: */ +/* 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/. */ + +#ifndef BASE_PROFILER_SHARED_LIBRARIES_H_ +#define BASE_PROFILER_SHARED_LIBRARIES_H_ + +#include "BaseProfiler.h" + +#ifndef MOZ_GECKO_PROFILER +# error Do not #include this header when MOZ_GECKO_PROFILER is not #defined. +#endif + +#include <algorithm> +#include <stdint.h> +#include <stdlib.h> +#include <string> +#include <vector> + +class SharedLibrary { + public: + SharedLibrary(uintptr_t aStart, uintptr_t aEnd, uintptr_t aOffset, + const std::string& aBreakpadId, const std::string& aCodeId, + const std::string& aModuleName, const std::string& aModulePath, + const std::string& aDebugName, const std::string& aDebugPath, + const std::string& aVersion, const char* aArch) + : mStart(aStart), + mEnd(aEnd), + mOffset(aOffset), + mBreakpadId(aBreakpadId), + mCodeId(aCodeId), + mModuleName(aModuleName), + mModulePath(aModulePath), + mDebugName(aDebugName), + mDebugPath(aDebugPath), + mVersion(aVersion), + mArch(aArch) {} + + SharedLibrary(const SharedLibrary& aEntry) + : mStart(aEntry.mStart), + mEnd(aEntry.mEnd), + mOffset(aEntry.mOffset), + mBreakpadId(aEntry.mBreakpadId), + mCodeId(aEntry.mCodeId), + mModuleName(aEntry.mModuleName), + mModulePath(aEntry.mModulePath), + mDebugName(aEntry.mDebugName), + mDebugPath(aEntry.mDebugPath), + mVersion(aEntry.mVersion), + mArch(aEntry.mArch) {} + + SharedLibrary& operator=(const SharedLibrary& aEntry) { + // Gracefully handle self assignment + if (this == &aEntry) return *this; + + mStart = aEntry.mStart; + mEnd = aEntry.mEnd; + mOffset = aEntry.mOffset; + mBreakpadId = aEntry.mBreakpadId; + mCodeId = aEntry.mCodeId; + mModuleName = aEntry.mModuleName; + mModulePath = aEntry.mModulePath; + mDebugName = aEntry.mDebugName; + mDebugPath = aEntry.mDebugPath; + mVersion = aEntry.mVersion; + mArch = aEntry.mArch; + return *this; + } + + bool operator==(const SharedLibrary& other) const { + return (mStart == other.mStart) && (mEnd == other.mEnd) && + (mOffset == other.mOffset) && (mModuleName == other.mModuleName) && + (mModulePath == other.mModulePath) && + (mDebugName == other.mDebugName) && + (mDebugPath == other.mDebugPath) && + (mBreakpadId == other.mBreakpadId) && (mCodeId == other.mCodeId) && + (mVersion == other.mVersion) && (mArch == other.mArch); + } + + uintptr_t GetStart() const { return mStart; } + uintptr_t GetEnd() const { return mEnd; } + uintptr_t GetOffset() const { return mOffset; } + const std::string& GetBreakpadId() const { return mBreakpadId; } + const std::string& GetCodeId() const { return mCodeId; } + const std::string& GetModuleName() const { return mModuleName; } + const std::string& GetModulePath() const { return mModulePath; } + const std::string& GetDebugName() const { return mDebugName; } + const std::string& GetDebugPath() const { return mDebugPath; } + const std::string& GetVersion() const { return mVersion; } + const std::string& GetArch() const { return mArch; } + + private: + SharedLibrary() : mStart{0}, mEnd{0}, mOffset{0} {} + + uintptr_t mStart; + uintptr_t mEnd; + uintptr_t mOffset; + std::string mBreakpadId; + // A string carrying an identifier for a binary. + // + // All platforms have different formats: + // - Windows: The code ID for a Windows PE file. + // It's the PE timestamp and PE image size. + // - macOS: The code ID for a macOS / iOS binary (mach-O). + // It's the mach-O UUID without dashes and without the trailing 0 for the + // breakpad ID. + // - Linux/Android: The code ID for a Linux ELF file. + // It's the complete build ID, as hex string. + std::string mCodeId; + std::string mModuleName; + std::string mModulePath; + std::string mDebugName; + std::string mDebugPath; + std::string mVersion; + std::string mArch; +}; + +static bool CompareAddresses(const SharedLibrary& first, + const SharedLibrary& second) { + return first.GetStart() < second.GetStart(); +} + +class SharedLibraryInfo { + public: + static SharedLibraryInfo GetInfoForSelf(); +#ifdef XP_WIN + static SharedLibraryInfo GetInfoFromPath(const wchar_t* aPath); +#endif + + static void Initialize(); + + SharedLibraryInfo() {} + + void AddSharedLibrary(SharedLibrary entry) { mEntries.push_back(entry); } + + const SharedLibrary& GetEntry(size_t i) const { return mEntries[i]; } + + SharedLibrary& GetMutableEntry(size_t i) { return mEntries[i]; } + + // Removes items in the range [first, last) + // i.e. element at the "last" index is not removed + void RemoveEntries(size_t first, size_t last) { + mEntries.erase(mEntries.begin() + first, mEntries.begin() + last); + } + + bool Contains(const SharedLibrary& searchItem) const { + return (mEntries.end() != + std::find(mEntries.begin(), mEntries.end(), searchItem)); + } + + size_t GetSize() const { return mEntries.size(); } + + void SortByAddress() { + std::sort(mEntries.begin(), mEntries.end(), CompareAddresses); + } + + void Clear() { mEntries.clear(); } + + private: +#ifdef XP_WIN + void AddSharedLibraryFromModuleInfo(const wchar_t* aModulePath, + mozilla::Maybe<HMODULE> aModule); +#endif + + std::vector<SharedLibrary> mEntries; +}; + +#endif // BASE_PROFILER_SHARED_LIBRARIES_H_ diff --git a/mozglue/baseprofiler/public/BaseProfilerState.h b/mozglue/baseprofiler/public/BaseProfilerState.h new file mode 100644 index 0000000000..15be2743a4 --- /dev/null +++ b/mozglue/baseprofiler/public/BaseProfilerState.h @@ -0,0 +1,405 @@ +/* -*- Mode: C++; tab-width: 2; 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/. */ + +// The Gecko Profiler is an always-on profiler that takes fast and low overhead +// samples of the program execution using only userspace functionality for +// portability. The goal of this module is to provide performance data in a +// generic cross-platform way without requiring custom tools or kernel support. +// +// Samples are collected to form a timeline with optional timeline event +// (markers) used for filtering. The samples include both native stacks and +// platform-independent "label stack" frames. + +#ifndef BaseProfilerState_h +#define BaseProfilerState_h + +// This header contains most functions that give information about the Base +// Profiler: Whether it is active or not, paused, the selected features, and +// some generic process and thread information. +// It is safe to include unconditionally, but uses of structs and functions must +// be guarded by `#ifdef MOZ_GECKO_PROFILER`. + +#include "mozilla/BaseProfilerUtils.h" + +#ifndef MOZ_GECKO_PROFILER + +# define AUTO_PROFILER_STATS(name) + +namespace mozilla::baseprofiler { + +[[nodiscard]] inline bool profiler_is_active() { return false; } +[[nodiscard]] inline bool profiler_is_active_and_unpaused() { return false; } + +} // namespace mozilla::baseprofiler + +#else // !MOZ_GECKO_PROFILER + +# include "mozilla/Atomics.h" +# include "mozilla/Maybe.h" + +# include <stdint.h> +# include <string> + +// Uncomment the following line to display profiler runtime statistics at +// shutdown. +// # define PROFILER_RUNTIME_STATS + +# ifdef PROFILER_RUNTIME_STATS +# include "mozilla/TimeStamp.h" +# endif + +namespace mozilla::baseprofiler { + +# ifdef PROFILER_RUNTIME_STATS +// This class gathers durations and displays some basic stats when destroyed. +// It is intended to be used as a static variable (see `AUTO_PROFILER_STATS` +// below), to display stats at the end of the program. +class StaticBaseProfilerStats { + public: + explicit StaticBaseProfilerStats(const char* aName) : mName(aName) {} + + ~StaticBaseProfilerStats() { + // Using unsigned long long for computations and printfs. + using ULL = unsigned long long; + ULL n = static_cast<ULL>(mNumberDurations); + if (n != 0) { + ULL sumNs = static_cast<ULL>(mSumDurationsNs); + printf( + "[%d] Profiler stats `%s`: %llu ns / %llu = %llu ns, max %llu ns\n", + int(profiler_current_process_id().ToNumber()), mName, sumNs, n, + sumNs / n, static_cast<ULL>(mLongestDurationNs)); + } else { + printf("[%d] Profiler stats `%s`: (nothing)\n", + int(profiler_current_process_id().ToNumber()), mName); + } + } + + void AddDurationFrom(TimeStamp aStart) { + DurationNs duration = static_cast<DurationNs>( + (TimeStamp::Now() - aStart).ToMicroseconds() * 1000 + 0.5); + mSumDurationsNs += duration; + ++mNumberDurations; + // Update mLongestDurationNs if this one is longer. + for (;;) { + DurationNs longest = mLongestDurationNs; + if (MOZ_LIKELY(longest >= duration)) { + // This duration is not the longest, nothing to do. + break; + } + if (MOZ_LIKELY(mLongestDurationNs.compareExchange(longest, duration))) { + // Successfully updated `mLongestDurationNs` with the new value. + break; + } + // Otherwise someone else just updated `mLongestDurationNs`, we need to + // try again by looping. + } + } + + private: + using DurationNs = uint64_t; + using Count = uint32_t; + + Atomic<DurationNs> mSumDurationsNs{0}; + Atomic<DurationNs> mLongestDurationNs{0}; + Atomic<Count> mNumberDurations{0}; + const char* mName; +}; + +// RAII object that measure its scoped lifetime duration and reports it to a +// `StaticBaseProfilerStats`. +class MOZ_RAII AutoProfilerStats { + public: + explicit AutoProfilerStats(StaticBaseProfilerStats& aStats) + : mStats(aStats), mStart(TimeStamp::Now()) {} + + ~AutoProfilerStats() { mStats.AddDurationFrom(mStart); } + + private: + StaticBaseProfilerStats& mStats; + TimeStamp mStart; +}; + +// Macro that should be used to collect basic statistics from measurements of +// block durations, from where this macro is, until the end of its enclosing +// scope. The name is used in the static variable name and when displaying stats +// at the end of the program; Another location could use the same name but their +// stats will not be combined, so use different name if these locations should +// be distinguished. +# define AUTO_PROFILER_STATS(name) \ + static ::mozilla::baseprofiler::StaticBaseProfilerStats sStat##name( \ + #name); \ + ::mozilla::baseprofiler::AutoProfilerStats autoStat##name(sStat##name); + +# else // PROFILER_RUNTIME_STATS + +# define AUTO_PROFILER_STATS(name) + +# endif // PROFILER_RUNTIME_STATS else + +//--------------------------------------------------------------------------- +// Profiler features +//--------------------------------------------------------------------------- + +# if defined(__APPLE__) && defined(__aarch64__) +# define POWER_HELP "Sample per process power use" +# elif defined(__APPLE__) && defined(__x86_64__) +# define POWER_HELP \ + "Record the power used by the entire system with each sample." +# elif defined(__linux__) && defined(__x86_64__) +# define POWER_HELP \ + "Record the power used by the entire system with each sample. " \ + "Only available with Intel CPUs and requires setting " \ + "the sysctl kernel.perf_event_paranoid to 0." +# elif defined(_MSC_VER) +# define POWER_HELP \ + "Record the value of every energy meter available on the system with " \ + "each sample. Only available on Windows 11 with Intel CPUs." +# else +# define POWER_HELP "Not supported on this platform." +# endif + +// Higher-order macro containing all the feature info in one place. Define +// |MACRO| appropriately to extract the relevant parts. Note that the number +// values are used internally only and so can be changed without consequence. +// Any changes to this list should also be applied to the feature list in +// toolkit/components/extensions/schemas/geckoProfiler.json. +// *** Synchronize with lists in ProfilerState.h and geckoProfiler.json *** +# define BASE_PROFILER_FOR_EACH_FEATURE(MACRO) \ + MACRO(0, "java", Java, "Profile Java code, Android only") \ + \ + MACRO(1, "js", JS, \ + "Get the JS engine to expose the JS stack to the profiler") \ + \ + MACRO(2, "mainthreadio", MainThreadIO, "Add main thread file I/O") \ + \ + MACRO(3, "fileio", FileIO, \ + "Add file I/O from all profiled threads, implies mainthreadio") \ + \ + MACRO(4, "fileioall", FileIOAll, \ + "Add file I/O from all threads, implies fileio") \ + \ + MACRO(5, "nomarkerstacks", NoMarkerStacks, \ + "Markers do not capture stacks, to reduce overhead") \ + \ + MACRO(6, "screenshots", Screenshots, \ + "Take a snapshot of the window on every composition") \ + \ + MACRO(7, "seqstyle", SequentialStyle, \ + "Disable parallel traversal in styling") \ + \ + MACRO(8, "stackwalk", StackWalk, \ + "Walk the C++ stack, not available on all platforms") \ + \ + MACRO(9, "jsallocations", JSAllocations, \ + "Have the JavaScript engine track allocations") \ + \ + MACRO(10, "nostacksampling", NoStackSampling, \ + "Disable all stack sampling: Cancels \"js\", \"stackwalk\" and " \ + "labels") \ + \ + MACRO(11, "nativeallocations", NativeAllocations, \ + "Collect the stacks from a smaller subset of all native " \ + "allocations, biasing towards collecting larger allocations") \ + \ + MACRO(12, "ipcmessages", IPCMessages, \ + "Have the IPC layer track cross-process messages") \ + \ + MACRO(13, "audiocallbacktracing", AudioCallbackTracing, \ + "Audio callback tracing") \ + \ + MACRO(14, "cpu", CPUUtilization, "CPU utilization") \ + \ + MACRO(15, "notimerresolutionchange", NoTimerResolutionChange, \ + "Do not adjust the timer resolution for fast sampling, so that " \ + "other Firefox timers do not get affected") \ + \ + MACRO(16, "cpuallthreads", CPUAllThreads, \ + "Sample the CPU utilization of all registered threads") \ + \ + MACRO(17, "samplingallthreads", SamplingAllThreads, \ + "Sample the stacks of all registered threads") \ + \ + MACRO(18, "markersallthreads", MarkersAllThreads, \ + "Record markers from all registered threads") \ + \ + MACRO(19, "unregisteredthreads", UnregisteredThreads, \ + "Discover and profile unregistered threads -- beware: expensive!") \ + \ + MACRO(20, "processcpu", ProcessCPU, \ + "Sample the CPU utilization of each process") \ + \ + MACRO(21, "power", Power, POWER_HELP) +// *** Synchronize with lists in ProfilerState.h and geckoProfiler.json *** + +struct ProfilerFeature { +# define DECLARE(n_, str_, Name_, desc_) \ + static constexpr uint32_t Name_ = (1u << n_); \ + [[nodiscard]] static constexpr bool Has##Name_(uint32_t aFeatures) { \ + return aFeatures & Name_; \ + } \ + static constexpr void Set##Name_(uint32_t& aFeatures) { \ + aFeatures |= Name_; \ + } \ + static constexpr void Clear##Name_(uint32_t& aFeatures) { \ + aFeatures &= ~Name_; \ + } + + // Define a bitfield constant, a getter, and two setters for each feature. + BASE_PROFILER_FOR_EACH_FEATURE(DECLARE) + +# undef DECLARE +}; + +namespace detail { + +// RacyFeatures is only defined in this header file so that its methods can +// be inlined into profiler_is_active(). Please do not use anything from the +// detail namespace outside the profiler. + +// Within the profiler's code, the preferred way to check profiler activeness +// and features is via ActivePS(). However, that requires locking gPSMutex. +// There are some hot operations where absolute precision isn't required, so we +// duplicate the activeness/feature state in a lock-free manner in this class. +class RacyFeatures { + public: + MFBT_API static void SetActive(uint32_t aFeatures); + + MFBT_API static void SetInactive(); + + MFBT_API static void SetPaused(); + + MFBT_API static void SetUnpaused(); + + MFBT_API static void SetSamplingPaused(); + + MFBT_API static void SetSamplingUnpaused(); + + [[nodiscard]] MFBT_API static mozilla::Maybe<uint32_t> FeaturesIfActive() { + if (uint32_t af = sActiveAndFeatures; af & Active) { + // Active, remove the Active&Paused bits to get all features. + return Some(af & ~(Active | Paused | SamplingPaused)); + } + return Nothing(); + } + + [[nodiscard]] MFBT_API static bool IsActive(); + + [[nodiscard]] MFBT_API static bool IsActiveWithFeature(uint32_t aFeature); + + [[nodiscard]] MFBT_API static bool IsActiveWithoutFeature(uint32_t aFeature); + + // True if profiler is active, and not fully paused. + // Note that periodic sampling *could* be paused! + [[nodiscard]] MFBT_API static bool IsActiveAndUnpaused(); + + // True if profiler is active, and sampling is not paused (though generic + // `SetPaused()` or specific `SetSamplingPaused()`). + [[nodiscard]] MFBT_API static bool IsActiveAndSamplingUnpaused(); + + private: + static constexpr uint32_t Active = 1u << 31; + static constexpr uint32_t Paused = 1u << 30; + static constexpr uint32_t SamplingPaused = 1u << 29; + +// Ensure Active/Paused don't overlap with any of the feature bits. +# define NO_OVERLAP(n_, str_, Name_, desc_) \ + static_assert(ProfilerFeature::Name_ != SamplingPaused, \ + "bad feature value"); + + BASE_PROFILER_FOR_EACH_FEATURE(NO_OVERLAP); + +# undef NO_OVERLAP + + // We combine the active bit with the feature bits so they can be read or + // written in a single atomic operation. + // TODO: Could this be MFBT_DATA for better inlining optimization? + static Atomic<uint32_t, MemoryOrdering::Relaxed> sActiveAndFeatures; +}; + +MFBT_API bool IsThreadBeingProfiled(); + +} // namespace detail + +//--------------------------------------------------------------------------- +// Get information from the profiler +//--------------------------------------------------------------------------- + +// Is the profiler active? Note: the return value of this function can become +// immediately out-of-date. E.g. the profile might be active but then +// profiler_stop() is called immediately afterward. One common and reasonable +// pattern of usage is the following: +// +// if (profiler_is_active()) { +// ExpensiveData expensiveData = CreateExpensiveData(); +// PROFILER_OPERATION(expensiveData); +// } +// +// where PROFILER_OPERATION is a no-op if the profiler is inactive. In this +// case the profiler_is_active() check is just an optimization -- it prevents +// us calling CreateExpensiveData() unnecessarily in most cases, but the +// expensive data will end up being created but not used if another thread +// stops the profiler between the CreateExpensiveData() and PROFILER_OPERATION +// calls. +[[nodiscard]] inline bool profiler_is_active() { + return baseprofiler::detail::RacyFeatures::IsActive(); +} + +// Same as profiler_is_active(), but also checks if the profiler is not paused. +[[nodiscard]] inline bool profiler_is_active_and_unpaused() { + return baseprofiler::detail::RacyFeatures::IsActiveAndUnpaused(); +} + +// Is the profiler active and unpaused, and is the current thread being +// profiled? (Same caveats and recommented usage as profiler_is_active().) +[[nodiscard]] inline bool profiler_thread_is_being_profiled() { + return baseprofiler::detail::RacyFeatures::IsActiveAndUnpaused() && + baseprofiler::detail::IsThreadBeingProfiled(); +} + +// Is the profiler active and paused? Returns false if the profiler is inactive. +[[nodiscard]] MFBT_API bool profiler_is_paused(); + +// Is the profiler active and sampling is paused? Returns false if the profiler +// is inactive. +[[nodiscard]] MFBT_API bool profiler_is_sampling_paused(); + +// Is the current thread sleeping? +[[nodiscard]] MFBT_API bool profiler_thread_is_sleeping(); + +// Get all the features supported by the profiler that are accepted by +// profiler_start(). The result is the same whether the profiler is active or +// not. +[[nodiscard]] MFBT_API uint32_t profiler_get_available_features(); + +// Returns the full feature set if the profiler is active. +// Note: the return value can become immediately out-of-date, much like the +// return value of profiler_is_active(). +[[nodiscard]] inline mozilla::Maybe<uint32_t> profiler_features_if_active() { + return baseprofiler::detail::RacyFeatures::FeaturesIfActive(); +} + +// Check if a profiler feature (specified via the ProfilerFeature type) is +// active. Returns false if the profiler is inactive. Note: the return value +// can become immediately out-of-date, much like the return value of +// profiler_is_active(). +[[nodiscard]] MFBT_API bool profiler_feature_active(uint32_t aFeature); + +// Check if the profiler is active without a feature (specified via the +// ProfilerFeature type). Note: the return value can become immediately +// out-of-date, much like the return value of profiler_is_active(). +[[nodiscard]] MFBT_API bool profiler_active_without_feature(uint32_t aFeature); + +// Returns true if any of the profiler mutexes are currently locked *on the +// current thread*. This may be used by re-entrant code that may call profiler +// functions while the same of a different profiler mutex is locked, which could +// deadlock. +[[nodiscard]] bool profiler_is_locked_on_current_thread(); + +} // namespace mozilla::baseprofiler + +#endif // !MOZ_GECKO_PROFILER + +#endif // BaseProfilerState_h diff --git a/mozglue/baseprofiler/public/BaseProfilerUtils.h b/mozglue/baseprofiler/public/BaseProfilerUtils.h new file mode 100644 index 0000000000..ab02e03b95 --- /dev/null +++ b/mozglue/baseprofiler/public/BaseProfilerUtils.h @@ -0,0 +1,227 @@ +/* -*- Mode: C++; tab-width: 2; 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/. */ + +#ifndef BaseProfilerUtils_h +#define BaseProfilerUtils_h + +// This header contains most process- and thread-related functions. +// It is safe to include unconditionally. + +// --------------------------------------------- WASI process & thread ids +#if defined(__wasi__) + +namespace mozilla::baseprofiler::detail { +using ProcessIdType = unsigned; +using ThreadIdType = unsigned; +} // namespace mozilla::baseprofiler::detail + +// --------------------------------------------- Windows process & thread ids +#elif defined(XP_WIN) + +namespace mozilla::baseprofiler::detail { +using ProcessIdType = int; +using ThreadIdType = unsigned long; +} // namespace mozilla::baseprofiler::detail + +// --------------------------------------------- Non-Windows process id +#else +// All non-Windows platforms are assumed to be POSIX, which has getpid(). + +# include <unistd.h> +namespace mozilla::baseprofiler::detail { +using ProcessIdType = decltype(getpid()); +} // namespace mozilla::baseprofiler::detail + +// --------------------------------------------- Non-Windows thread id +// ------------------------------------------------------- macOS +# if defined(XP_MACOSX) + +namespace mozilla::baseprofiler::detail { +using ThreadIdType = uint64_t; +} // namespace mozilla::baseprofiler::detail + +// ------------------------------------------------------- Android +// Test Android before Linux, because Linux includes Android. +# elif defined(__ANDROID__) || defined(ANDROID) + +# include <sys/types.h> +namespace mozilla::baseprofiler::detail { +using ThreadIdType = decltype(gettid()); +} // namespace mozilla::baseprofiler::detail + +// ------------------------------------------------------- Linux +# elif defined(XP_LINUX) + +namespace mozilla::baseprofiler::detail { +using ThreadIdType = long; +} // namespace mozilla::baseprofiler::detail + +// ------------------------------------------------------- FreeBSD +# elif defined(XP_FREEBSD) + +namespace mozilla::baseprofiler::detail { +using ThreadIdType = long; +} // namespace mozilla::baseprofiler::detail + +// ------------------------------------------------------- Others +# else + +# include <thread> + +namespace mozilla::baseprofiler::detail { +using ThreadIdType = std::thread::id; +} // namespace mozilla::baseprofiler::detail + +# endif +#endif // End of non-XP_WIN. + +#include <stdint.h> +#include <string.h> +#include <type_traits> + +namespace mozilla::baseprofiler { + +// Trivially-copyable class containing a process id. It may be left unspecified. +class BaseProfilerProcessId { + public: + using NativeType = detail::ProcessIdType; + + using NumberType = + std::conditional_t<(sizeof(NativeType) <= 4), uint32_t, uint64_t>; + static_assert(sizeof(NativeType) <= sizeof(NumberType)); + + // Unspecified process id. + constexpr BaseProfilerProcessId() = default; + + [[nodiscard]] constexpr bool IsSpecified() const { + return mProcessId != scUnspecified; + } + + // Construct from a native type. + [[nodiscard]] static BaseProfilerProcessId FromNativeId( + const NativeType& aNativeProcessId) { + BaseProfilerProcessId id; + // Convert trivially-copyable native id by copying its bits. + static_assert(std::is_trivially_copyable_v<NativeType>); + memcpy(&id.mProcessId, &aNativeProcessId, sizeof(NativeType)); + return id; + } + + // Get the process id as a number, which may be unspecified. + // This should only be used for serialization or logging. + [[nodiscard]] constexpr NumberType ToNumber() const { return mProcessId; } + + // BaseProfilerProcessId from given number (which may be unspecified). + constexpr static BaseProfilerProcessId FromNumber( + const NumberType& aProcessId) { + BaseProfilerProcessId id; + id.mProcessId = aProcessId; + return id; + } + + [[nodiscard]] constexpr bool operator==( + const BaseProfilerProcessId& aOther) const { + return mProcessId == aOther.mProcessId; + } + [[nodiscard]] constexpr bool operator!=( + const BaseProfilerProcessId& aOther) const { + return mProcessId != aOther.mProcessId; + } + + private: + static constexpr NumberType scUnspecified = 0; + NumberType mProcessId = scUnspecified; +}; + +// Check traits. These should satisfy usage in std::atomic. +static_assert(std::is_trivially_copyable_v<BaseProfilerProcessId>); +static_assert(std::is_copy_constructible_v<BaseProfilerProcessId>); +static_assert(std::is_move_constructible_v<BaseProfilerProcessId>); +static_assert(std::is_copy_assignable_v<BaseProfilerProcessId>); +static_assert(std::is_move_assignable_v<BaseProfilerProcessId>); + +// Trivially-copyable class containing a thread id. It may be left unspecified. +class BaseProfilerThreadId { + public: + using NativeType = detail::ThreadIdType; + + using NumberType = + std::conditional_t<(sizeof(NativeType) <= 4), uint32_t, uint64_t>; + static_assert(sizeof(NativeType) <= sizeof(NumberType)); + + // Unspecified thread id. + constexpr BaseProfilerThreadId() = default; + + [[nodiscard]] constexpr bool IsSpecified() const { + return mThreadId != scUnspecified; + } + + // Construct from a native type. + [[nodiscard]] static BaseProfilerThreadId FromNativeId( + const NativeType& aNativeThreadId) { + BaseProfilerThreadId id; + // Convert trivially-copyable native id by copying its bits. + static_assert(std::is_trivially_copyable_v<NativeType>); + memcpy(&id.mThreadId, &aNativeThreadId, sizeof(NativeType)); + return id; + } + + // Get the thread id as a number, which may be unspecified. + // This should only be used for serialization or logging. + [[nodiscard]] constexpr NumberType ToNumber() const { return mThreadId; } + + // BaseProfilerThreadId from given number (which may be unspecified). + constexpr static BaseProfilerThreadId FromNumber( + const NumberType& aThreadId) { + BaseProfilerThreadId id; + id.mThreadId = aThreadId; + return id; + } + + [[nodiscard]] constexpr bool operator==( + const BaseProfilerThreadId& aOther) const { + return mThreadId == aOther.mThreadId; + } + [[nodiscard]] constexpr bool operator!=( + const BaseProfilerThreadId& aOther) const { + return mThreadId != aOther.mThreadId; + } + + private: + static constexpr NumberType scUnspecified = 0; + NumberType mThreadId = scUnspecified; +}; + +// Check traits. These should satisfy usage in std::atomic. +static_assert(std::is_trivially_copyable_v<BaseProfilerThreadId>); +static_assert(std::is_copy_constructible_v<BaseProfilerThreadId>); +static_assert(std::is_move_constructible_v<BaseProfilerThreadId>); +static_assert(std::is_copy_assignable_v<BaseProfilerThreadId>); +static_assert(std::is_move_assignable_v<BaseProfilerThreadId>); + +} // namespace mozilla::baseprofiler + +#include "mozilla/Types.h" + +namespace mozilla::baseprofiler { + +// Get the current process's ID. +[[nodiscard]] MFBT_API BaseProfilerProcessId profiler_current_process_id(); + +// Get the current thread's ID. +[[nodiscard]] MFBT_API BaseProfilerThreadId profiler_current_thread_id(); + +// Must be called at least once from the main thread, before any other main- +// thread id function. +MFBT_API void profiler_init_main_thread_id(); + +[[nodiscard]] MFBT_API BaseProfilerThreadId profiler_main_thread_id(); + +[[nodiscard]] MFBT_API bool profiler_is_main_thread(); + +} // namespace mozilla::baseprofiler + +#endif // BaseProfilerUtils_h diff --git a/mozglue/baseprofiler/public/BaseProfilingCategory.h b/mozglue/baseprofiler/public/BaseProfilingCategory.h new file mode 100644 index 0000000000..2e80950966 --- /dev/null +++ b/mozglue/baseprofiler/public/BaseProfilingCategory.h @@ -0,0 +1,68 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * vim: set ts=8 sts=4 et sw=4 tw=99: + * 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/. */ + +#ifndef BaseProfilingCategory_h +#define BaseProfilingCategory_h + +#include "mozilla/Types.h" + +#include <cstdint> + +#include "ProfilingCategoryList.h" + +namespace mozilla { +namespace baseprofiler { + +// clang-format off + +// An enum that lists all possible category pairs in one list. +// This is the enum that is used in profiler stack labels. Having one list that +// includes subcategories from all categories in one list allows assigning the +// category pair to a stack label with just one number. +#define CATEGORY_ENUM_BEGIN_CATEGORY(name, labelAsString, color) +#define CATEGORY_ENUM_SUBCATEGORY(supercategory, name, labelAsString) name, +#define CATEGORY_ENUM_END_CATEGORY +enum class ProfilingCategoryPair : uint32_t { + MOZ_PROFILING_CATEGORY_LIST(CATEGORY_ENUM_BEGIN_CATEGORY, + CATEGORY_ENUM_SUBCATEGORY, + CATEGORY_ENUM_END_CATEGORY) + COUNT, + LAST = COUNT - 1, +}; +#undef CATEGORY_ENUM_BEGIN_CATEGORY +#undef CATEGORY_ENUM_SUBCATEGORY +#undef CATEGORY_ENUM_END_CATEGORY + +// An enum that lists just the categories without their subcategories. +#define SUPERCATEGORY_ENUM_BEGIN_CATEGORY(name, labelAsString, color) name, +#define SUPERCATEGORY_ENUM_SUBCATEGORY(supercategory, name, labelAsString) +#define SUPERCATEGORY_ENUM_END_CATEGORY +enum class ProfilingCategory : uint32_t { + MOZ_PROFILING_CATEGORY_LIST(SUPERCATEGORY_ENUM_BEGIN_CATEGORY, + SUPERCATEGORY_ENUM_SUBCATEGORY, + SUPERCATEGORY_ENUM_END_CATEGORY) + COUNT, + LAST = COUNT - 1, +}; +#undef SUPERCATEGORY_ENUM_BEGIN_CATEGORY +#undef SUPERCATEGORY_ENUM_SUBCATEGORY +#undef SUPERCATEGORY_ENUM_END_CATEGORY + +// clang-format on + +struct ProfilingCategoryPairInfo { + ProfilingCategory mCategory; + uint32_t mSubcategoryIndex; + const char* mLabel; +}; + +MFBT_API const ProfilingCategoryPairInfo& GetProfilingCategoryPairInfo( + ProfilingCategoryPair aCategoryPair); + +} // namespace baseprofiler +} // namespace mozilla + +#endif /* BaseProfilingCategory_h */ diff --git a/mozglue/baseprofiler/public/BaseProfilingStack.h b/mozglue/baseprofiler/public/BaseProfilingStack.h new file mode 100644 index 0000000000..2e3781cf17 --- /dev/null +++ b/mozglue/baseprofiler/public/BaseProfilingStack.h @@ -0,0 +1,517 @@ +/* -*- 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/. */ + +#ifndef BaseProfilingStack_h +#define BaseProfilingStack_h + +#ifndef MOZ_GECKO_PROFILER +# error Do not #include this header when MOZ_GECKO_PROFILER is not #defined. +#endif + +#include "BaseProfilingCategory.h" + +#include "mozilla/Atomics.h" + +#include <stdint.h> + +// This file defines the classes ProfilingStack and ProfilingStackFrame. +// The ProfilingStack manages an array of ProfilingStackFrames. +// It keeps track of the "label stack" and the JS interpreter stack. +// The two stack types are interleaved. +// +// Usage: +// +// ProfilingStack* profilingStack = ...; +// +// // For label frames: +// profilingStack->pushLabelFrame(...); +// // Execute some code. When finished, pop the frame: +// profilingStack->pop(); +// +// // For JS stack frames: +// profilingStack->pushJSFrame(...); +// // Execute some code. When finished, pop the frame: +// profilingStack->pop(); +// +// +// Concurrency considerations +// +// A thread's profiling stack (and the frames inside it) is only modified by +// that thread. However, the profiling stack can be *read* by a different +// thread, the sampler thread: Whenever the profiler wants to sample a given +// thread A, the following happens: +// (1) Thread A is suspended. +// (2) The sampler thread (thread S) reads the ProfilingStack of thread A, +// including all ProfilingStackFrames that are currently in that stack +// (profilingStack->frames[0..profilingStack->stackSize()]). +// (3) Thread A is resumed. +// +// Thread suspension is achieved using platform-specific APIs; refer to each +// platform's Sampler::SuspendAndSampleAndResumeThread implementation in +// platform-*.cpp for details. +// +// When the thread is suspended, the values in profilingStack->stackPointer and +// in the stack frame range +// profilingStack->frames[0..profilingStack->stackPointer] need to be in a +// consistent state, so that thread S does not read partially- constructed stack +// frames. More specifically, we have two requirements: +// (1) When adding a new frame at the top of the stack, its ProfilingStackFrame +// data needs to be put in place *before* the stackPointer is incremented, +// and the compiler + CPU need to know that this order matters. +// (2) When popping an frame from the stack and then preparing the +// ProfilingStackFrame data for the next frame that is about to be pushed, +// the decrement of the stackPointer in pop() needs to happen *before* the +// ProfilingStackFrame for the new frame is being popuplated, and the +// compiler + CPU need to know that this order matters. +// +// We can express the relevance of these orderings in multiple ways. +// Option A is to make stackPointer an atomic with SequentiallyConsistent +// memory ordering. This would ensure that no writes in thread A would be +// reordered across any writes to stackPointer, which satisfies requirements +// (1) and (2) at the same time. Option A is the simplest. +// Option B is to use ReleaseAcquire memory ordering both for writes to +// stackPointer *and* for writes to ProfilingStackFrame fields. Release-stores +// ensure that all writes that happened *before this write in program order* are +// not reordered to happen after this write. ReleaseAcquire ordering places no +// requirements on the ordering of writes that happen *after* this write in +// program order. +// Using release-stores for writes to stackPointer expresses requirement (1), +// and using release-stores for writes to the ProfilingStackFrame fields +// expresses requirement (2). +// +// Option B is more complicated than option A, but has much better performance +// on x86/64: In a microbenchmark run on a Macbook Pro from 2017, switching +// from option A to option B reduced the overhead of pushing+popping a +// ProfilingStackFrame by 10 nanoseconds. +// On x86/64, release-stores require no explicit hardware barriers or lock +// instructions. +// On ARM/64, option B may be slower than option A, because the compiler will +// generate hardware barriers for every single release-store instead of just +// for the writes to stackPointer. However, the actual performance impact of +// this has not yet been measured on ARM, so we're currently using option B +// everywhere. This is something that we may want to change in the future once +// we've done measurements. + +namespace mozilla { +namespace baseprofiler { + +// A call stack can be specified to the JS engine such that all JS entry/exits +// to functions push/pop a stack frame to/from the specified stack. +// +// For more detailed information, see vm/GeckoProfiler.h. +// +class ProfilingStackFrame { + // A ProfilingStackFrame represents either a label frame or a JS frame. + + // WARNING WARNING WARNING + // + // All the fields below are Atomic<...,ReleaseAcquire>. This is needed so + // that writes to these fields are release-writes, which ensures that + // earlier writes in this thread don't get reordered after the writes to + // these fields. In particular, the decrement of the stack pointer in + // ProfilingStack::pop() is a write that *must* happen before the values in + // this ProfilingStackFrame are changed. Otherwise, the sampler thread might + // see an inconsistent state where the stack pointer still points to a + // ProfilingStackFrame which has already been popped off the stack and whose + // fields have now been partially repopulated with new values. + // See the "Concurrency considerations" paragraph at the top of this file + // for more details. + + // Descriptive label for this stack frame. Must be a static string! Can be + // an empty string, but not a null pointer. + Atomic<const char*, ReleaseAcquire> label_; + + // An additional descriptive string of this frame which is combined with + // |label_| in profiler output. Need not be (and usually isn't) static. Can + // be null. + Atomic<const char*, ReleaseAcquire> dynamicString_; + + // Stack pointer for non-JS stack frames, the script pointer otherwise. + Atomic<void*, ReleaseAcquire> spOrScript; + + // ID of the JS Realm for JS stack frames. + // Must not be used on non-JS frames; it'll contain either the default 0, + // or a leftover value from a previous JS stack frame that was using this + // ProfilingStackFrame object. + mozilla::Atomic<uint64_t, mozilla::ReleaseAcquire> realmID_; + + // The bytecode offset for JS stack frames. + // Must not be used on non-JS frames; it'll contain either the default 0, + // or a leftover value from a previous JS stack frame that was using this + // ProfilingStackFrame object. + Atomic<int32_t, ReleaseAcquire> pcOffsetIfJS_; + + // Bits 0...8 hold the Flags. Bits 9...31 hold the category pair. + Atomic<uint32_t, ReleaseAcquire> flagsAndCategoryPair_; + + public: + ProfilingStackFrame() = default; + ProfilingStackFrame& operator=(const ProfilingStackFrame& other) { + label_ = other.label(); + dynamicString_ = other.dynamicString(); + void* spScript = other.spOrScript; + spOrScript = spScript; + int32_t offsetIfJS = other.pcOffsetIfJS_; + pcOffsetIfJS_ = offsetIfJS; + int64_t realmID = other.realmID_; + realmID_ = realmID; + uint32_t flagsAndCategory = other.flagsAndCategoryPair_; + flagsAndCategoryPair_ = flagsAndCategory; + return *this; + } + + // Reserve up to 16 bits for flags, and 16 for category pair. + enum class Flags : uint32_t { + // The first three flags describe the kind of the frame and are + // mutually exclusive. (We still give them individual bits for + // simplicity.) + + // A regular label frame. These usually come from AutoProfilerLabel. + IS_LABEL_FRAME = 1 << 0, + + // A special frame indicating the start of a run of JS profiling stack + // frames. IS_SP_MARKER_FRAME frames are ignored, except for the sp + // field. These frames are needed to get correct ordering between JS + // and LABEL frames because JS frames don't carry sp information. + // SP is short for "stack pointer". + IS_SP_MARKER_FRAME = 1 << 1, + + // A JS frame. + IS_JS_FRAME = 1 << 2, + + // An interpreter JS frame that has OSR-ed into baseline. IS_JS_FRAME + // frames can have this flag set and unset during their lifetime. + // JS_OSR frames are ignored. + JS_OSR = 1 << 3, + + // The next three are mutually exclusive. + // By default, for profiling stack frames that have both a label and a + // dynamic string, the two strings are combined into one string of the + // form "<label> <dynamicString>" during JSON serialization. The + // following flags can be used to change this preset. + STRING_TEMPLATE_METHOD = 1 << 4, // "<label>.<dynamicString>" + STRING_TEMPLATE_GETTER = 1 << 5, // "get <label>.<dynamicString>" + STRING_TEMPLATE_SETTER = 1 << 6, // "set <label>.<dynamicString>" + + // If set, causes this stack frame to be marked as "relevantForJS" in + // the profile JSON, which will make it show up in the "JS only" call + // tree view. + RELEVANT_FOR_JS = 1 << 7, + + // If set, causes the label on this ProfilingStackFrame to be ignored + // and to be replaced by the subcategory's label. + LABEL_DETERMINED_BY_CATEGORY_PAIR = 1 << 8, + + // Frame dynamic string does not contain user data. + NONSENSITIVE = 1 << 9, + + // A JS Baseline Interpreter frame. + IS_BLINTERP_FRAME = 1 << 10, + + FLAGS_BITCOUNT = 16, + FLAGS_MASK = (1 << FLAGS_BITCOUNT) - 1 + }; + + static_assert( + uint32_t(ProfilingCategoryPair::LAST) <= + (UINT32_MAX >> uint32_t(Flags::FLAGS_BITCOUNT)), + "Too many category pairs to fit into u32 with together with the " + "reserved bits for the flags"); + + bool isLabelFrame() const { + return uint32_t(flagsAndCategoryPair_) & uint32_t(Flags::IS_LABEL_FRAME); + } + + bool isSpMarkerFrame() const { + return uint32_t(flagsAndCategoryPair_) & + uint32_t(Flags::IS_SP_MARKER_FRAME); + } + + bool isJsFrame() const { + return uint32_t(flagsAndCategoryPair_) & uint32_t(Flags::IS_JS_FRAME); + } + + bool isOSRFrame() const { + return uint32_t(flagsAndCategoryPair_) & uint32_t(Flags::JS_OSR); + } + + void setIsOSRFrame(bool isOSR) { + if (isOSR) { + flagsAndCategoryPair_ = + uint32_t(flagsAndCategoryPair_) | uint32_t(Flags::JS_OSR); + } else { + flagsAndCategoryPair_ = + uint32_t(flagsAndCategoryPair_) & ~uint32_t(Flags::JS_OSR); + } + } + + const char* label() const { + uint32_t flagsAndCategoryPair = flagsAndCategoryPair_; + if (flagsAndCategoryPair & + uint32_t(Flags::LABEL_DETERMINED_BY_CATEGORY_PAIR)) { + auto categoryPair = ProfilingCategoryPair( + flagsAndCategoryPair >> uint32_t(Flags::FLAGS_BITCOUNT)); + return GetProfilingCategoryPairInfo(categoryPair).mLabel; + } + return label_; + } + + const char* dynamicString() const { return dynamicString_; } + + void initLabelFrame(const char* aLabel, const char* aDynamicString, void* sp, + ProfilingCategoryPair aCategoryPair, uint32_t aFlags) { + label_ = aLabel; + dynamicString_ = aDynamicString; + spOrScript = sp; + // pcOffsetIfJS_ is not set and must not be used on label frames. + flagsAndCategoryPair_ = + uint32_t(Flags::IS_LABEL_FRAME) | + (uint32_t(aCategoryPair) << uint32_t(Flags::FLAGS_BITCOUNT)) | aFlags; + MOZ_ASSERT(isLabelFrame()); + } + + void initSpMarkerFrame(void* sp) { + label_ = ""; + dynamicString_ = nullptr; + spOrScript = sp; + // pcOffsetIfJS_ is not set and must not be used on sp marker frames. + flagsAndCategoryPair_ = uint32_t(Flags::IS_SP_MARKER_FRAME) | + (uint32_t(ProfilingCategoryPair::OTHER) + << uint32_t(Flags::FLAGS_BITCOUNT)); + MOZ_ASSERT(isSpMarkerFrame()); + } + + void initJsFrame(const char* aLabel, const char* aDynamicString, + void* /* JSScript* */ aScript, int32_t aOffset, + uint64_t aRealmID) { + label_ = aLabel; + dynamicString_ = aDynamicString; + spOrScript = aScript; + pcOffsetIfJS_ = aOffset; + realmID_ = aRealmID; + flagsAndCategoryPair_ = + uint32_t(Flags::IS_JS_FRAME) | (uint32_t(ProfilingCategoryPair::JS) + << uint32_t(Flags::FLAGS_BITCOUNT)); + MOZ_ASSERT(isJsFrame()); + } + + uint32_t flags() const { + return uint32_t(flagsAndCategoryPair_) & uint32_t(Flags::FLAGS_MASK); + } + + ProfilingCategoryPair categoryPair() const { + return ProfilingCategoryPair(flagsAndCategoryPair_ >> + uint32_t(Flags::FLAGS_BITCOUNT)); + } + + uint64_t realmID() const { return realmID_; } + + void* stackAddress() const { + MOZ_ASSERT(!isJsFrame()); + return spOrScript; + } + + // Note that the pointer returned might be invalid. + void* rawScript() const { + MOZ_ASSERT(isJsFrame()); + return spOrScript; + } + void setRawScript(void* aScript) { + MOZ_ASSERT(isJsFrame()); + spOrScript = aScript; + } + + int32_t pcOffset() const { + MOZ_ASSERT(isJsFrame()); + return pcOffsetIfJS_; + } + + void setPCOffset(int32_t aOffset) { + MOZ_ASSERT(isJsFrame()); + pcOffsetIfJS_ = aOffset; + } + + // The offset of a pc into a script's code can actually be 0, so to + // signify a nullptr pc, use a -1 index. This is checked against in + // pc() and setPC() to set/get the right pc. + static const int32_t NullPCOffset = -1; +}; + +// Each thread has its own ProfilingStack. That thread modifies the +// ProfilingStack, pushing and popping elements as necessary. +// +// The ProfilingStack is also read periodically by the profiler's sampler +// thread. This happens only when the thread that owns the ProfilingStack is +// suspended. So there are no genuine parallel accesses. +// +// However, it is possible for pushing/popping to be interrupted by a periodic +// sample. Because of this, we need pushing/popping to be effectively atomic. +// +// - When pushing a new frame, we increment the stack pointer -- making the new +// frame visible to the sampler thread -- only after the new frame has been +// fully written. The stack pointer is Atomic<uint32_t,ReleaseAcquire>, so +// the increment is a release-store, which ensures that this store is not +// reordered before the writes of the frame. +// +// - When popping an old frame, the only operation is the decrementing of the +// stack pointer, which is obviously atomic. +// +class ProfilingStack final { + public: + ProfilingStack() = default; + + MFBT_API ~ProfilingStack(); + + void pushLabelFrame(const char* label, const char* dynamicString, void* sp, + ProfilingCategoryPair categoryPair, uint32_t flags = 0) { + // This thread is the only one that ever changes the value of + // stackPointer. + // Store the value of the atomic in a non-atomic local variable so that + // the compiler won't generate two separate loads from the atomic for + // the size check and the frames[] array indexing operation. + uint32_t stackPointerVal = stackPointer; + + if (MOZ_UNLIKELY(stackPointerVal >= capacity)) { + ensureCapacitySlow(); + } + frames[stackPointerVal].initLabelFrame(label, dynamicString, sp, + categoryPair, flags); + + // This must happen at the end! The compiler will not reorder this + // update because stackPointer is Atomic<..., ReleaseAcquire>, so any + // the writes above will not be reordered below the stackPointer store. + // Do the read and the write as two separate statements, in order to + // make it clear that we don't need an atomic increment, which would be + // more expensive on x86 than the separate operations done here. + // However, don't use stackPointerVal here; instead, allow the compiler + // to turn this store into a non-atomic increment instruction which + // takes up less code size. + stackPointer = stackPointer + 1; + } + + void pushSpMarkerFrame(void* sp) { + uint32_t oldStackPointer = stackPointer; + + if (MOZ_UNLIKELY(oldStackPointer >= capacity)) { + ensureCapacitySlow(); + } + frames[oldStackPointer].initSpMarkerFrame(sp); + + // This must happen at the end, see the comment in pushLabelFrame. + stackPointer = oldStackPointer + 1; + } + + void pushJsOffsetFrame(const char* label, const char* dynamicString, + void* script, int32_t offset, uint64_t aRealmID) { + // This thread is the only one that ever changes the value of + // stackPointer. Only load the atomic once. + uint32_t oldStackPointer = stackPointer; + + if (MOZ_UNLIKELY(oldStackPointer >= capacity)) { + ensureCapacitySlow(); + } + frames[oldStackPointer].initJsFrame(label, dynamicString, script, offset, + aRealmID); + + // This must happen at the end, see the comment in pushLabelFrame. + stackPointer = stackPointer + 1; + } + + void pop() { + MOZ_ASSERT(stackPointer > 0); + // Do the read and the write as two separate statements, in order to + // make it clear that we don't need an atomic decrement, which would be + // more expensive on x86 than the separate operations done here. + // This thread is the only one that ever changes the value of + // stackPointer. + uint32_t oldStackPointer = stackPointer; + stackPointer = oldStackPointer - 1; + } + + uint32_t stackSize() const { return stackPointer; } + uint32_t stackCapacity() const { return capacity; } + + private: + // Out of line path for expanding the buffer, since otherwise this would get + // inlined in every DOM WebIDL call. + MFBT_API MOZ_COLD void ensureCapacitySlow(); + + // No copying. + ProfilingStack(const ProfilingStack&) = delete; + void operator=(const ProfilingStack&) = delete; + + // No moving either. + ProfilingStack(ProfilingStack&&) = delete; + void operator=(ProfilingStack&&) = delete; + + uint32_t capacity = 0; + + public: + // The pointer to the stack frames, this is read from the profiler thread and + // written from the current thread. + // + // This is effectively a unique pointer. + Atomic<ProfilingStackFrame*, SequentiallyConsistent> frames{nullptr}; + + // This may exceed the capacity, so instead use the stackSize() method to + // determine the number of valid frames in stackFrames. When this is less + // than stackCapacity(), it refers to the first free stackframe past the top + // of the in-use stack (i.e. frames[stackPointer - 1] is the top stack + // frame). + // + // WARNING WARNING WARNING + // + // This is an atomic variable that uses ReleaseAcquire memory ordering. + // See the "Concurrency considerations" paragraph at the top of this file + // for more details. + Atomic<uint32_t, ReleaseAcquire> stackPointer{0}; +}; + +class AutoGeckoProfilerEntry; +class GeckoProfilerEntryMarker; +class GeckoProfilerBaselineOSRMarker; + +class GeckoProfilerThread { + friend class AutoGeckoProfilerEntry; + friend class GeckoProfilerEntryMarker; + friend class GeckoProfilerBaselineOSRMarker; + + ProfilingStack* profilingStack_; + + // Same as profilingStack_ if the profiler is currently active, otherwise + // null. + ProfilingStack* profilingStackIfEnabled_; + + public: + MFBT_API GeckoProfilerThread(); + + uint32_t stackPointer() { + MOZ_ASSERT(infraInstalled()); + return profilingStack_->stackPointer; + } + ProfilingStackFrame* stack() { return profilingStack_->frames; } + ProfilingStack* getProfilingStack() { return profilingStack_; } + ProfilingStack* getProfilingStackIfEnabled() { + return profilingStackIfEnabled_; + } + + /* + * True if the profiler infrastructure is setup. Should be true in builds + * that include profiler support except during early startup or late + * shutdown. Unrelated to the presence of the Gecko Profiler addon. + */ + bool infraInstalled() { return profilingStack_ != nullptr; } + + MFBT_API void setProfilingStack(ProfilingStack* profilingStack, bool enabled); + void enable(bool enable) { + profilingStackIfEnabled_ = enable ? profilingStack_ : nullptr; + } +}; + +} // namespace baseprofiler +} // namespace mozilla + +#endif /* BaseProfilingStack_h */ diff --git a/mozglue/baseprofiler/public/BlocksRingBuffer.h b/mozglue/baseprofiler/public/BlocksRingBuffer.h new file mode 100644 index 0000000000..32cbe6b93c --- /dev/null +++ b/mozglue/baseprofiler/public/BlocksRingBuffer.h @@ -0,0 +1,999 @@ +/* -*- Mode: C++; tab-width: 2; 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/. */ + +#ifndef BlocksRingBuffer_h +#define BlocksRingBuffer_h + +#include "mozilla/BaseProfilerDetail.h" +#include "mozilla/ModuloBuffer.h" +#include "mozilla/ProfileBufferIndex.h" +#include "mozilla/ScopeExit.h" + +#include <functional> +#include <string> +#include <tuple> +#include <utility> + +namespace mozilla { + +// Thread-safe Ring buffer that can store blocks of different sizes during +// defined sessions. +// Each *block* contains an *entry* and the entry size: +// [ entry_size | entry ] [ entry_size | entry ] ... +// *In-session* is a period of time during which `BlocksRingBuffer` allows +// reading and writing. *Out-of-session*, the `BlocksRingBuffer` object is +// still valid, but contains no data, and gracefully denies accesses. +// +// To write an entry, the buffer reserves a block of sufficient size (to contain +// user data of predetermined size), writes the entry size, and lets the caller +// fill the entry contents using ModuloBuffer::Iterator APIs and a few entry- +// specific APIs. E.g.: +// ``` +// BlockRingsBuffer brb(PowerOfTwo<BlockRingsBuffer::Length>(1024)); +// brb.ReserveAndPut([]() { return sizeof(123); }, +// [&](ProfileBufferEntryWriter& aEW) { +// aEW.WriteObject(123); +// }); +// ``` +// Other `Put...` functions may be used as shortcuts for simple entries. +// The objects given to the caller's callbacks should only be used inside the +// callbacks and not stored elsewhere, because they keep their own references to +// the BlocksRingBuffer and therefore should not live longer. +// Different type of objects may be serialized into an entry, see `Serializer` +// for more information. +// +// When reading data, the buffer iterates over blocks (it knows how to read the +// entry size, and therefore move to the next block), and lets the caller read +// the entry inside of each block. E.g.: +// ``` +// brb.Read([](BlocksRingBuffer::Reader aR) {} +// for (ProfileBufferEntryReader aER : aR) { +// /* Use ProfileBufferEntryReader functions to read serialized objects. */ +// int n = aER.ReadObject<int>(); +// } +// }); +// ``` +// Different type of objects may be deserialized from an entry, see +// `Deserializer` for more information. +// +// The caller may retrieve the `ProfileBufferBlockIndex` corresponding to an +// entry (`ProfileBufferBlockIndex` is an opaque type preventing the user from +// modifying it). That index may later be used to get back to that particular +// entry if it still exists. +class BlocksRingBuffer { + public: + // Using ModuloBuffer as underlying circular byte buffer. + using Buffer = ModuloBuffer<uint32_t, ProfileBufferIndex>; + using Byte = Buffer::Byte; + + // Length type for total buffer (as PowerOfTwo<Length>) and each entry. + using Length = uint32_t; + + enum class ThreadSafety { WithoutMutex, WithMutex }; + + // Default constructor starts out-of-session (nothing to read or write). + explicit BlocksRingBuffer(ThreadSafety aThreadSafety) + : mMutex(aThreadSafety != ThreadSafety::WithoutMutex) {} + + // Create a buffer of the given length. + explicit BlocksRingBuffer(ThreadSafety aThreadSafety, + PowerOfTwo<Length> aLength) + : mMutex(aThreadSafety != ThreadSafety::WithoutMutex), + mMaybeUnderlyingBuffer(Some(UnderlyingBuffer(aLength))) {} + + // Take ownership of an existing buffer. + BlocksRingBuffer(ThreadSafety aThreadSafety, + UniquePtr<Buffer::Byte[]> aExistingBuffer, + PowerOfTwo<Length> aLength) + : mMutex(aThreadSafety != ThreadSafety::WithoutMutex), + mMaybeUnderlyingBuffer( + Some(UnderlyingBuffer(std::move(aExistingBuffer), aLength))) {} + + // Use an externally-owned buffer. + BlocksRingBuffer(ThreadSafety aThreadSafety, Buffer::Byte* aExternalBuffer, + PowerOfTwo<Length> aLength) + : mMutex(aThreadSafety != ThreadSafety::WithoutMutex), + mMaybeUnderlyingBuffer( + Some(UnderlyingBuffer(aExternalBuffer, aLength))) {} + + // Destructor doesn't need to do anything special. (Clearing entries would + // only update indices and stats, which won't be accessible after the object + // is destroyed anyway.) + ~BlocksRingBuffer() = default; + + // Remove underlying buffer, if any. + void Reset() { + baseprofiler::detail::BaseProfilerMaybeAutoLock lock(mMutex); + ResetUnderlyingBuffer(); + } + + // Create a buffer of the given length. + void Set(PowerOfTwo<Length> aLength) { + baseprofiler::detail::BaseProfilerMaybeAutoLock lock(mMutex); + ResetUnderlyingBuffer(); + mMaybeUnderlyingBuffer.emplace(aLength); + } + + // Take ownership of an existing buffer. + void Set(UniquePtr<Buffer::Byte[]> aExistingBuffer, + PowerOfTwo<Length> aLength) { + baseprofiler::detail::BaseProfilerMaybeAutoLock lock(mMutex); + ResetUnderlyingBuffer(); + mMaybeUnderlyingBuffer.emplace(std::move(aExistingBuffer), aLength); + } + + // Use an externally-owned buffer. + void Set(Buffer::Byte* aExternalBuffer, PowerOfTwo<Length> aLength) { + baseprofiler::detail::BaseProfilerMaybeAutoLock lock(mMutex); + ResetUnderlyingBuffer(); + mMaybeUnderlyingBuffer.emplace(aExternalBuffer, aLength); + } + + // This cannot change during the lifetime of this buffer, so there's no need + // to lock. + bool IsThreadSafe() const { return mMutex.IsActivated(); } + + [[nodiscard]] bool IsInSession() const { + baseprofiler::detail::BaseProfilerMaybeAutoLock lock(mMutex); + return !!mMaybeUnderlyingBuffer; + } + + // Lock the buffer mutex and run the provided callback. + // This can be useful when the caller needs to explicitly lock down this + // buffer, but not do anything else with it. + template <typename Callback> + auto LockAndRun(Callback&& aCallback) const { + baseprofiler::detail::BaseProfilerMaybeAutoLock lock(mMutex); + return std::forward<Callback>(aCallback)(); + } + + // Buffer length in bytes. + Maybe<PowerOfTwo<Length>> BufferLength() const { + baseprofiler::detail::BaseProfilerMaybeAutoLock lock(mMutex); + return mMaybeUnderlyingBuffer.map([](const UnderlyingBuffer& aBuffer) { + return aBuffer.mBuffer.BufferLength(); + }); + ; + } + + // Size of external resources. + size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const { + if (!mMaybeUnderlyingBuffer) { + return 0; + } + return mMaybeUnderlyingBuffer->mBuffer.SizeOfExcludingThis(aMallocSizeOf); + } + + size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const { + return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf); + } + + // Snapshot of the buffer state. + struct State { + // Index to the first block. + ProfileBufferBlockIndex mRangeStart; + + // Index past the last block. Equals mRangeStart if empty. + ProfileBufferBlockIndex mRangeEnd; + + // Number of blocks that have been pushed into this buffer. + uint64_t mPushedBlockCount = 0; + + // Number of blocks that have been removed from this buffer. + // Note: Live entries = pushed - cleared. + uint64_t mClearedBlockCount = 0; + }; + + // Get a snapshot of the current state. + // When out-of-session, mFirstReadIndex==mNextWriteIndex, and + // mPushedBlockCount==mClearedBlockCount==0. + // Note that these may change right after this thread-safe call, so they + // should only be used for statistical purposes. + State GetState() const { + baseprofiler::detail::BaseProfilerMaybeAutoLock lock(mMutex); + return { + mFirstReadIndex, mNextWriteIndex, + mMaybeUnderlyingBuffer ? mMaybeUnderlyingBuffer->mPushedBlockCount : 0, + mMaybeUnderlyingBuffer ? mMaybeUnderlyingBuffer->mClearedBlockCount + : 0}; + } + + class Reader; + + // Class that can iterate through blocks and provide + // `ProfileBufferEntryReader`s. + // Created through `Reader`, lives within a lock guard lifetime. + class BlockIterator { + public: +#ifdef DEBUG + ~BlockIterator() { + // No BlockIterator should live outside of a mutexed call. + mRing->mMutex.AssertCurrentThreadOwns(); + } +#endif // DEBUG + + // Comparison with other iterator, mostly used in range-for loops. + bool operator==(const BlockIterator aRhs) const { + MOZ_ASSERT(mRing == aRhs.mRing); + return mBlockIndex == aRhs.mBlockIndex; + } + bool operator!=(const BlockIterator aRhs) const { + MOZ_ASSERT(mRing == aRhs.mRing); + return mBlockIndex != aRhs.mBlockIndex; + } + + // Advance to next BlockIterator. + BlockIterator& operator++() { + mBlockIndex = NextBlockIndex(); + return *this; + } + + // Dereferencing creates a `ProfileBufferEntryReader` for the entry inside + // this block. + ProfileBufferEntryReader operator*() const { + return mRing->ReaderInBlockAt(mBlockIndex); + } + + // True if this iterator is just past the last entry. + bool IsAtEnd() const { + MOZ_ASSERT(mBlockIndex <= BufferRangeEnd()); + return mBlockIndex == BufferRangeEnd(); + } + + // Can be used as reference to come back to this entry with `ReadAt()`. + ProfileBufferBlockIndex CurrentBlockIndex() const { return mBlockIndex; } + + // Index past the end of this block, which is the start of the next block. + ProfileBufferBlockIndex NextBlockIndex() const { + MOZ_ASSERT(!IsAtEnd()); + const Length entrySize = + mRing->ReaderInBlockAt(mBlockIndex).RemainingBytes(); + return ProfileBufferBlockIndex::CreateFromProfileBufferIndex( + mBlockIndex.ConvertToProfileBufferIndex() + ULEB128Size(entrySize) + + entrySize); + } + + // Index of the first block in the whole buffer. + ProfileBufferBlockIndex BufferRangeStart() const { + return mRing->mFirstReadIndex; + } + + // Index past the last block in the whole buffer. + ProfileBufferBlockIndex BufferRangeEnd() const { + return mRing->mNextWriteIndex; + } + + private: + // Only a Reader can instantiate a BlockIterator. + friend class Reader; + + BlockIterator(const BlocksRingBuffer& aRing, + ProfileBufferBlockIndex aBlockIndex) + : mRing(WrapNotNull(&aRing)), mBlockIndex(aBlockIndex) { + // No BlockIterator should live outside of a mutexed call. + mRing->mMutex.AssertCurrentThreadOwns(); + } + + // Using a non-null pointer instead of a reference, to allow copying. + // This BlockIterator should only live inside one of the thread-safe + // BlocksRingBuffer functions, for this reference to stay valid. + NotNull<const BlocksRingBuffer*> mRing; + ProfileBufferBlockIndex mBlockIndex; + }; + + // Class that can create `BlockIterator`s (e.g., for range-for), or just + // iterate through entries; lives within a lock guard lifetime. + class MOZ_RAII Reader { + public: + Reader(const Reader&) = delete; + Reader& operator=(const Reader&) = delete; + Reader(Reader&&) = delete; + Reader& operator=(Reader&&) = delete; + +#ifdef DEBUG + ~Reader() { + // No Reader should live outside of a mutexed call. + mRing.mMutex.AssertCurrentThreadOwns(); + } +#endif // DEBUG + + // Index of the first block in the whole buffer. + ProfileBufferBlockIndex BufferRangeStart() const { + return mRing.mFirstReadIndex; + } + + // Index past the last block in the whole buffer. + ProfileBufferBlockIndex BufferRangeEnd() const { + return mRing.mNextWriteIndex; + } + + // Iterators to the first and past-the-last blocks. + // Compatible with range-for (see `ForEach` below as example). + BlockIterator begin() const { + return BlockIterator(mRing, BufferRangeStart()); + } + // Note that a `BlockIterator` at the `end()` should not be dereferenced, as + // there is no actual block there! + BlockIterator end() const { return BlockIterator(mRing, BufferRangeEnd()); } + + // Get a `BlockIterator` at the given `ProfileBufferBlockIndex`, clamped to + // the stored range. Note that a `BlockIterator` at the `end()` should not + // be dereferenced, as there is no actual block there! + BlockIterator At(ProfileBufferBlockIndex aBlockIndex) const { + if (aBlockIndex < BufferRangeStart()) { + // Anything before the range (including null ProfileBufferBlockIndex) is + // clamped at the beginning. + return begin(); + } + // Otherwise we at least expect the index to be valid (pointing exactly at + // a live block, or just past the end.) + mRing.AssertBlockIndexIsValidOrEnd(aBlockIndex); + return BlockIterator(mRing, aBlockIndex); + } + + // Run `aCallback(ProfileBufferEntryReader&)` on each entry from first to + // last. Callback should not store `ProfileBufferEntryReader`, as it may + // become invalid after this thread-safe call. + template <typename Callback> + void ForEach(Callback&& aCallback) const { + for (ProfileBufferEntryReader reader : *this) { + aCallback(reader); + } + } + + private: + friend class BlocksRingBuffer; + + explicit Reader(const BlocksRingBuffer& aRing) : mRing(aRing) { + // No Reader should live outside of a mutexed call. + mRing.mMutex.AssertCurrentThreadOwns(); + } + + // This Reader should only live inside one of the thread-safe + // BlocksRingBuffer functions, for this reference to stay valid. + const BlocksRingBuffer& mRing; + }; + + // Call `aCallback(BlocksRingBuffer::Reader*)` (nullptr when out-of-session), + // and return whatever `aCallback` returns. Callback should not store + // `Reader`, because it may become invalid after this call. + template <typename Callback> + auto Read(Callback&& aCallback) const { + { + baseprofiler::detail::BaseProfilerMaybeAutoLock lock(mMutex); + if (MOZ_LIKELY(mMaybeUnderlyingBuffer)) { + Reader reader(*this); + return std::forward<Callback>(aCallback)(&reader); + } + } + return std::forward<Callback>(aCallback)(nullptr); + } + + // Call `aCallback(ProfileBufferEntryReader&)` on each item. + // Callback should not store `ProfileBufferEntryReader`, because it may become + // invalid after this call. + template <typename Callback> + void ReadEach(Callback&& aCallback) const { + Read([&](Reader* aReader) { + if (MOZ_LIKELY(aReader)) { + aReader->ForEach(aCallback); + } + }); + } + + // Call `aCallback(Maybe<ProfileBufferEntryReader>&&)` on the entry at + // the given ProfileBufferBlockIndex; The `Maybe` will be `Nothing` if + // out-of-session, or if that entry doesn't exist anymore, or if we've reached + // just past the last entry. Return whatever `aCallback` returns. Callback + // should not store `ProfileBufferEntryReader`, because it may become invalid + // after this call. + template <typename Callback> + auto ReadAt(ProfileBufferBlockIndex aBlockIndex, Callback&& aCallback) const { + baseprofiler::detail::BaseProfilerMaybeAutoLock lock(mMutex); + MOZ_ASSERT(aBlockIndex <= mNextWriteIndex); + Maybe<ProfileBufferEntryReader> maybeEntryReader; + if (MOZ_LIKELY(mMaybeUnderlyingBuffer) && aBlockIndex >= mFirstReadIndex && + aBlockIndex < mNextWriteIndex) { + AssertBlockIndexIsValid(aBlockIndex); + maybeEntryReader.emplace(ReaderInBlockAt(aBlockIndex)); + } + return std::forward<Callback>(aCallback)(std::move(maybeEntryReader)); + } + + // Main function to write entries. + // Reserve `aCallbackBytes()` bytes, call `aCallback()` with a pointer to an + // on-stack temporary ProfileBufferEntryWriter (nullptr when out-of-session), + // and return whatever `aCallback` returns. Callback should not store + // `ProfileBufferEntryWriter`, because it may become invalid after this + // thread-safe call. Note: `aCallbackBytes` is a callback instead of a simple + // value, to delay this potentially-expensive computation until after we're + // checked that we're in-session; use `Put(Length, Callback)` below if you + // know the size already. + template <typename CallbackBytes, typename Callback> + auto ReserveAndPut(CallbackBytes aCallbackBytes, Callback&& aCallback) { + Maybe<ProfileBufferEntryWriter> maybeEntryWriter; + + baseprofiler::detail::BaseProfilerMaybeAutoLock lock(mMutex); + + if (MOZ_LIKELY(mMaybeUnderlyingBuffer)) { + const Length entryBytes = std::forward<CallbackBytes>(aCallbackBytes)(); + MOZ_RELEASE_ASSERT(entryBytes > 0); + const Length bufferBytes = + mMaybeUnderlyingBuffer->mBuffer.BufferLength().Value(); + MOZ_RELEASE_ASSERT(entryBytes <= bufferBytes - ULEB128Size(entryBytes), + "Entry would wrap and overwrite itself"); + // Compute block size from the requested entry size. + const Length blockBytes = ULEB128Size(entryBytes) + entryBytes; + // We will put this new block at the end of the current buffer. + const ProfileBufferIndex blockIndex = + mNextWriteIndex.ConvertToProfileBufferIndex(); + // Compute the end of this new block. + const ProfileBufferIndex blockEnd = blockIndex + blockBytes; + while (blockEnd > + mFirstReadIndex.ConvertToProfileBufferIndex() + bufferBytes) { + // About to trample on an old block. + ProfileBufferEntryReader reader = ReaderInBlockAt(mFirstReadIndex); + mMaybeUnderlyingBuffer->mClearedBlockCount += 1; + // Move the buffer reading start past this cleared block. + mFirstReadIndex = ProfileBufferBlockIndex::CreateFromProfileBufferIndex( + mFirstReadIndex.ConvertToProfileBufferIndex() + + ULEB128Size(reader.RemainingBytes()) + reader.RemainingBytes()); + } + // Store the new end of buffer. + mNextWriteIndex = + ProfileBufferBlockIndex::CreateFromProfileBufferIndex(blockEnd); + mMaybeUnderlyingBuffer->mPushedBlockCount += 1; + // Finally, let aCallback write into the entry. + mMaybeUnderlyingBuffer->mBuffer.EntryWriterFromTo(maybeEntryWriter, + blockIndex, blockEnd); + MOZ_ASSERT(maybeEntryWriter.isSome(), + "Non-empty entry should always create an EntryWriter"); + maybeEntryWriter->WriteULEB128(entryBytes); + MOZ_ASSERT(maybeEntryWriter->RemainingBytes() == entryBytes); + } + +#ifdef DEBUG + auto checkAllWritten = MakeScopeExit([&]() { + MOZ_ASSERT(!maybeEntryWriter || maybeEntryWriter->RemainingBytes() == 0); + }); +#endif // DEBUG + return std::forward<Callback>(aCallback)(maybeEntryWriter); + } + + // Add a new entry of known size, call `aCallback` with a pointer to a + // temporary ProfileBufferEntryWriter (can be null when out-of-session), and + // return whatever `aCallback` returns. Callback should not store the + // `ProfileBufferEntryWriter`, as it may become invalid after this thread-safe + // call. + template <typename Callback> + auto Put(Length aBytes, Callback&& aCallback) { + return ReserveAndPut([aBytes]() { return aBytes; }, + std::forward<Callback>(aCallback)); + } + + // Add a new entry copied from the given buffer, return block index. + ProfileBufferBlockIndex PutFrom(const void* aSrc, Length aBytes) { + return ReserveAndPut([aBytes]() { return aBytes; }, + [&](Maybe<ProfileBufferEntryWriter>& aEntryWriter) { + if (MOZ_UNLIKELY(aEntryWriter.isNothing())) { + // Out-of-session, return "empty" index. + return ProfileBufferBlockIndex{}; + } + aEntryWriter->WriteBytes(aSrc, aBytes); + return aEntryWriter->CurrentBlockIndex(); + }); + } + + // Add a new single entry with *all* given object (using a Serializer for + // each), return block index. + template <typename... Ts> + ProfileBufferBlockIndex PutObjects(const Ts&... aTs) { + static_assert(sizeof...(Ts) > 0, + "PutObjects must be given at least one object."); + return ReserveAndPut( + [&]() { return ProfileBufferEntryWriter::SumBytes(aTs...); }, + [&](Maybe<ProfileBufferEntryWriter>& aEntryWriter) { + if (MOZ_UNLIKELY(aEntryWriter.isNothing())) { + // Out-of-session, return "empty" index. + return ProfileBufferBlockIndex{}; + } + aEntryWriter->WriteObjects(aTs...); + return aEntryWriter->CurrentBlockIndex(); + }); + } + + // Add a new entry copied from the given object, return block index. + template <typename T> + ProfileBufferBlockIndex PutObject(const T& aOb) { + return PutObjects(aOb); + } + + // Append the contents of another BlocksRingBuffer to this one. + ProfileBufferBlockIndex AppendContents(const BlocksRingBuffer& aSrc) { + baseprofiler::detail::BaseProfilerMaybeAutoLock lock(mMutex); + + if (MOZ_UNLIKELY(!mMaybeUnderlyingBuffer)) { + // We are out-of-session, could not append contents. + return ProfileBufferBlockIndex{}; + } + + baseprofiler::detail::BaseProfilerMaybeAutoLock srcLock(aSrc.mMutex); + + if (MOZ_UNLIKELY(!aSrc.mMaybeUnderlyingBuffer)) { + // The other BRB is out-of-session, nothing to copy, we're done. + return ProfileBufferBlockIndex{}; + } + + const ProfileBufferIndex srcStartIndex = + aSrc.mFirstReadIndex.ConvertToProfileBufferIndex(); + const ProfileBufferIndex srcEndIndex = + aSrc.mNextWriteIndex.ConvertToProfileBufferIndex(); + const Length bytesToCopy = static_cast<Length>(srcEndIndex - srcStartIndex); + + if (MOZ_UNLIKELY(bytesToCopy == 0)) { + // The other BRB is empty, nothing to copy, we're done. + return ProfileBufferBlockIndex{}; + } + + const Length bufferBytes = + mMaybeUnderlyingBuffer->mBuffer.BufferLength().Value(); + + MOZ_RELEASE_ASSERT(bytesToCopy <= bufferBytes, + "Entry would wrap and overwrite itself"); + + // We will put all copied blocks at the end of the current buffer. + const ProfileBufferIndex dstStartIndex = + mNextWriteIndex.ConvertToProfileBufferIndex(); + // Compute where the copy will end... + const ProfileBufferIndex dstEndIndex = dstStartIndex + bytesToCopy; + + while (dstEndIndex > + mFirstReadIndex.ConvertToProfileBufferIndex() + bufferBytes) { + // About to trample on an old block. + ProfileBufferEntryReader reader = ReaderInBlockAt(mFirstReadIndex); + mMaybeUnderlyingBuffer->mClearedBlockCount += 1; + // Move the buffer reading start past this cleared block. + mFirstReadIndex = ProfileBufferBlockIndex::CreateFromProfileBufferIndex( + mFirstReadIndex.ConvertToProfileBufferIndex() + + ULEB128Size(reader.RemainingBytes()) + reader.RemainingBytes()); + } + + // Store the new end of buffer. + mNextWriteIndex = + ProfileBufferBlockIndex::CreateFromProfileBufferIndex(dstEndIndex); + // Update our pushed count with the number of live blocks we are copying. + mMaybeUnderlyingBuffer->mPushedBlockCount += + aSrc.mMaybeUnderlyingBuffer->mPushedBlockCount - + aSrc.mMaybeUnderlyingBuffer->mClearedBlockCount; + + auto reader = aSrc.mMaybeUnderlyingBuffer->mBuffer.EntryReaderFromTo( + srcStartIndex, srcEndIndex, nullptr, nullptr); + auto writer = mMaybeUnderlyingBuffer->mBuffer.EntryWriterFromTo( + dstStartIndex, dstEndIndex); + writer.WriteFromReader(reader, bytesToCopy); + + return ProfileBufferBlockIndex::CreateFromProfileBufferIndex(dstStartIndex); + } + + // Clear all entries: Move read index to the end so that these entries cannot + // be read anymore. + void Clear() { + baseprofiler::detail::BaseProfilerMaybeAutoLock lock(mMutex); + ClearAllEntries(); + } + + // Clear all entries strictly before aBlockIndex, and move read index to the + // end so that these entries cannot be read anymore. + void ClearBefore(ProfileBufferBlockIndex aBlockIndex) { + baseprofiler::detail::BaseProfilerMaybeAutoLock lock(mMutex); + if (!mMaybeUnderlyingBuffer) { + return; + } + // Don't accept a not-yet-written index. One-past-the-end is ok. + MOZ_ASSERT(aBlockIndex <= mNextWriteIndex); + if (aBlockIndex <= mFirstReadIndex) { + // Already cleared. + return; + } + if (aBlockIndex == mNextWriteIndex) { + // Right past the end, just clear everything. + ClearAllEntries(); + return; + } + // Otherwise we need to clear a subset of entries. + AssertBlockIndexIsValid(aBlockIndex); + // Just count skipped entries. + Reader reader(*this); + BlockIterator it = reader.begin(); + for (; it.CurrentBlockIndex() < aBlockIndex; ++it) { + MOZ_ASSERT(it.CurrentBlockIndex() < reader.end().CurrentBlockIndex()); + mMaybeUnderlyingBuffer->mClearedBlockCount += 1; + } + MOZ_ASSERT(it.CurrentBlockIndex() == aBlockIndex); + // Move read index to given index, so there's effectively no more entries + // before. + mFirstReadIndex = aBlockIndex; + } + +#ifdef DEBUG + void Dump() const { + baseprofiler::detail::BaseProfilerMaybeAutoLock lock(mMutex); + if (!mMaybeUnderlyingBuffer) { + printf("empty BlocksRingBuffer\n"); + return; + } + using ULL = unsigned long long; + printf("start=%llu (%llu) end=%llu (%llu) - ", + ULL(mFirstReadIndex.ConvertToProfileBufferIndex()), + ULL(mFirstReadIndex.ConvertToProfileBufferIndex() & + (mMaybeUnderlyingBuffer->mBuffer.BufferLength().Value() - 1)), + ULL(mNextWriteIndex.ConvertToProfileBufferIndex()), + ULL(mNextWriteIndex.ConvertToProfileBufferIndex() & + (mMaybeUnderlyingBuffer->mBuffer.BufferLength().Value() - 1))); + mMaybeUnderlyingBuffer->mBuffer.Dump(); + } +#endif // DEBUG + + private: + // In DEBUG mode, assert that `aBlockIndex` is a valid index for a live block. + // (Not just in range, but points exactly at the start of a block.) + // Slow, so avoid it for internal checks; this is more to check what callers + // provide us. + void AssertBlockIndexIsValid(ProfileBufferBlockIndex aBlockIndex) const { +#ifdef DEBUG + mMutex.AssertCurrentThreadOwns(); + MOZ_ASSERT(aBlockIndex >= mFirstReadIndex); + MOZ_ASSERT(aBlockIndex < mNextWriteIndex); + // Quick check (default), or slow check (change '1' to '0') below: +# if 1 + // Quick check that this looks like a valid block start. + // Read the entry size at the start of the block. + const Length entryBytes = ReaderInBlockAt(aBlockIndex).RemainingBytes(); + MOZ_ASSERT(entryBytes > 0, "Empty entries are not allowed"); + MOZ_ASSERT( + entryBytes < mMaybeUnderlyingBuffer->mBuffer.BufferLength().Value() - + ULEB128Size(entryBytes), + "Entry would wrap and overwrite itself"); + // The end of the block should be inside the live buffer range. + MOZ_ASSERT(aBlockIndex.ConvertToProfileBufferIndex() + + ULEB128Size(entryBytes) + entryBytes <= + mNextWriteIndex.ConvertToProfileBufferIndex()); +# else + // Slow check that the index is really the start of the block. + // This kills performances, as it reads from the first index until + // aBlockIndex. Only use to debug issues locally. + Reader reader(*this); + BlockIterator it = reader.begin(); + for (; it.CurrentBlockIndex() < aBlockIndex; ++it) { + MOZ_ASSERT(it.CurrentBlockIndex() < reader.end().CurrentBlockIndex()); + } + MOZ_ASSERT(it.CurrentBlockIndex() == aBlockIndex); +# endif +#endif // DEBUG + } + + // In DEBUG mode, assert that `aBlockIndex` is a valid index for a live block, + // or is just past-the-end. (Not just in range, but points exactly at the + // start of a block.) Slow, so avoid it for internal checks; this is more to + // check what callers provide us. + void AssertBlockIndexIsValidOrEnd(ProfileBufferBlockIndex aBlockIndex) const { +#ifdef DEBUG + mMutex.AssertCurrentThreadOwns(); + if (aBlockIndex == mNextWriteIndex) { + return; + } + AssertBlockIndexIsValid(aBlockIndex); +#endif // DEBUG + } + + // Create a reader for the block starting at aBlockIndex. + ProfileBufferEntryReader ReaderInBlockAt( + ProfileBufferBlockIndex aBlockIndex) const { + mMutex.AssertCurrentThreadOwns(); + MOZ_ASSERT(mMaybeUnderlyingBuffer.isSome()); + MOZ_ASSERT(aBlockIndex >= mFirstReadIndex); + MOZ_ASSERT(aBlockIndex < mNextWriteIndex); + // Create a reader from the given index until the end of the buffer. + ProfileBufferEntryReader reader = + mMaybeUnderlyingBuffer->mBuffer.EntryReaderFromTo( + aBlockIndex.ConvertToProfileBufferIndex(), + mNextWriteIndex.ConvertToProfileBufferIndex(), nullptr, nullptr); + // Read the block size at the beginning. + const Length entryBytes = reader.ReadULEB128<Length>(); + // Make sure we don't overshoot the buffer. + MOZ_RELEASE_ASSERT(entryBytes <= reader.RemainingBytes()); + ProfileBufferIndex nextBlockIndex = + aBlockIndex.ConvertToProfileBufferIndex() + ULEB128Size(entryBytes) + + entryBytes; + // And reduce the reader to the entry area. Only provide a next-block-index + // if it's not at the end of the buffer (i.e., there's an actual block + // there). + reader = mMaybeUnderlyingBuffer->mBuffer.EntryReaderFromTo( + aBlockIndex.ConvertToProfileBufferIndex() + ULEB128Size(entryBytes), + nextBlockIndex, aBlockIndex, + (nextBlockIndex < mNextWriteIndex.ConvertToProfileBufferIndex()) + ? ProfileBufferBlockIndex::CreateFromProfileBufferIndex( + nextBlockIndex) + : ProfileBufferBlockIndex{}); + return reader; + } + + ProfileBufferEntryReader FullBufferReader() const { + mMutex.AssertCurrentThreadOwns(); + if (!mMaybeUnderlyingBuffer) { + return {}; + } + return mMaybeUnderlyingBuffer->mBuffer.EntryReaderFromTo( + mFirstReadIndex.ConvertToProfileBufferIndex(), + mNextWriteIndex.ConvertToProfileBufferIndex(), nullptr, nullptr); + } + + // Clear all entries: Move read index to the end so that these entries cannot + // be read anymore. + void ClearAllEntries() { + mMutex.AssertCurrentThreadOwns(); + if (!mMaybeUnderlyingBuffer) { + return; + } + // Mark all entries pushed so far as cleared. + mMaybeUnderlyingBuffer->mClearedBlockCount = + mMaybeUnderlyingBuffer->mPushedBlockCount; + // Move read index to write index, so there's effectively no more entries + // that can be read. (Not setting both to 0, in case user is keeping + // `ProfileBufferBlockIndex`'es to old entries.) + mFirstReadIndex = mNextWriteIndex; + } + + // If there is an underlying buffer, clear all entries, and discard the + // buffer. This BlocksRingBuffer will now gracefully reject all API calls, and + // is in a state where a new underlying buffer may be set. + void ResetUnderlyingBuffer() { + mMutex.AssertCurrentThreadOwns(); + if (!mMaybeUnderlyingBuffer) { + return; + } + ClearAllEntries(); + mMaybeUnderlyingBuffer.reset(); + } + + // Used to de/serialize a BlocksRingBuffer (e.g., containing a backtrace). + friend ProfileBufferEntryWriter::Serializer<BlocksRingBuffer>; + friend ProfileBufferEntryReader::Deserializer<BlocksRingBuffer>; + friend ProfileBufferEntryWriter::Serializer<UniquePtr<BlocksRingBuffer>>; + friend ProfileBufferEntryReader::Deserializer<UniquePtr<BlocksRingBuffer>>; + + // Mutex guarding the following members. + mutable baseprofiler::detail::BaseProfilerMaybeMutex mMutex; + + struct UnderlyingBuffer { + // Create a buffer of the given length. + explicit UnderlyingBuffer(PowerOfTwo<Length> aLength) : mBuffer(aLength) { + MOZ_ASSERT(aLength.Value() > ULEB128MaxSize<Length>(), + "Buffer should be able to contain more than a block size"); + } + + // Take ownership of an existing buffer. + UnderlyingBuffer(UniquePtr<Buffer::Byte[]> aExistingBuffer, + PowerOfTwo<Length> aLength) + : mBuffer(std::move(aExistingBuffer), aLength) { + MOZ_ASSERT(aLength.Value() > ULEB128MaxSize<Length>(), + "Buffer should be able to contain more than a block size"); + } + + // Use an externally-owned buffer. + UnderlyingBuffer(Buffer::Byte* aExternalBuffer, PowerOfTwo<Length> aLength) + : mBuffer(aExternalBuffer, aLength) { + MOZ_ASSERT(aLength.Value() > ULEB128MaxSize<Length>(), + "Buffer should be able to contain more than a block size"); + } + + // Only allow move-construction. + UnderlyingBuffer(UnderlyingBuffer&&) = default; + + // Copies and move-assignment are explictly disallowed. + UnderlyingBuffer(const UnderlyingBuffer&) = delete; + UnderlyingBuffer& operator=(const UnderlyingBuffer&) = delete; + UnderlyingBuffer& operator=(UnderlyingBuffer&&) = delete; + + // Underlying circular byte buffer. + Buffer mBuffer; + + // Statistics. + uint64_t mPushedBlockCount = 0; + uint64_t mClearedBlockCount = 0; + }; + + // Underlying buffer, with stats. + // Only valid during in-session period. + Maybe<UnderlyingBuffer> mMaybeUnderlyingBuffer; + + // Index to the first block to be read (or cleared). Initialized to 1 because + // 0 is reserved for the "empty" ProfileBufferBlockIndex value. Kept between + // sessions, so that stored indices from one session will be gracefully denied + // in future sessions. + ProfileBufferBlockIndex mFirstReadIndex = + ProfileBufferBlockIndex::CreateFromProfileBufferIndex( + ProfileBufferIndex(1)); + // Index where the next new block should be allocated. Initialized to 1 + // because 0 is reserved for the "empty" ProfileBufferBlockIndex value. Kept + // between sessions, so that stored indices from one session will be + // gracefully denied in future sessions. + ProfileBufferBlockIndex mNextWriteIndex = + ProfileBufferBlockIndex::CreateFromProfileBufferIndex( + ProfileBufferIndex(1)); +}; + +// ---------------------------------------------------------------------------- +// BlocksRingBuffer serialization + +// A BlocksRingBuffer can hide another one! +// This will be used to store marker backtraces; They can be read back into a +// UniquePtr<BlocksRingBuffer>. +// Format: len (ULEB128) | start | end | buffer (len bytes) | pushed | cleared +// len==0 marks an out-of-session buffer, or empty buffer. +template <> +struct ProfileBufferEntryWriter::Serializer<BlocksRingBuffer> { + static Length Bytes(const BlocksRingBuffer& aBuffer) { + baseprofiler::detail::BaseProfilerMaybeAutoLock lock(aBuffer.mMutex); + if (aBuffer.mMaybeUnderlyingBuffer.isNothing()) { + // Out-of-session, we only need 1 byte to store a length of 0. + return ULEB128Size<Length>(0); + } + const auto start = aBuffer.mFirstReadIndex.ConvertToProfileBufferIndex(); + const auto end = aBuffer.mNextWriteIndex.ConvertToProfileBufferIndex(); + const auto len = end - start; + if (len == 0) { + // In-session but empty, also store a length of 0. + return ULEB128Size<Length>(0); + } + return ULEB128Size(len) + sizeof(start) + sizeof(end) + len + + sizeof(aBuffer.mMaybeUnderlyingBuffer->mPushedBlockCount) + + sizeof(aBuffer.mMaybeUnderlyingBuffer->mClearedBlockCount); + } + + static void Write(ProfileBufferEntryWriter& aEW, + const BlocksRingBuffer& aBuffer) { + baseprofiler::detail::BaseProfilerMaybeAutoLock lock(aBuffer.mMutex); + if (aBuffer.mMaybeUnderlyingBuffer.isNothing()) { + // Out-of-session, only store a length of 0. + aEW.WriteULEB128<Length>(0); + return; + } + const auto start = aBuffer.mFirstReadIndex.ConvertToProfileBufferIndex(); + const auto end = aBuffer.mNextWriteIndex.ConvertToProfileBufferIndex(); + MOZ_ASSERT(end - start <= std::numeric_limits<Length>::max()); + const auto len = static_cast<Length>(end - start); + if (len == 0) { + // In-session but empty, only store a length of 0. + aEW.WriteULEB128<Length>(0); + return; + } + // In-session. + // Store buffer length, start and end indices. + aEW.WriteULEB128<Length>(len); + aEW.WriteObject(start); + aEW.WriteObject(end); + // Write all the bytes. + auto reader = aBuffer.FullBufferReader(); + aEW.WriteFromReader(reader, reader.RemainingBytes()); + // And write stats. + aEW.WriteObject(aBuffer.mMaybeUnderlyingBuffer->mPushedBlockCount); + aEW.WriteObject(aBuffer.mMaybeUnderlyingBuffer->mClearedBlockCount); + } +}; + +// A serialized BlocksRingBuffer can be read into an empty buffer (either +// out-of-session, or in-session with enough room). +template <> +struct ProfileBufferEntryReader::Deserializer<BlocksRingBuffer> { + static void ReadInto(ProfileBufferEntryReader& aER, + BlocksRingBuffer& aBuffer) { + // Expect an empty buffer, as we're going to overwrite it. + MOZ_ASSERT(aBuffer.GetState().mRangeStart == aBuffer.GetState().mRangeEnd); + // Read the stored buffer length. + const auto len = aER.ReadULEB128<Length>(); + if (len == 0) { + // 0-length means an "uninteresting" buffer, just return now. + return; + } + // We have a non-empty buffer to read. + if (aBuffer.BufferLength().isSome()) { + // Output buffer is in-session (i.e., it already has a memory buffer + // attached). Make sure the caller allocated enough space. + MOZ_RELEASE_ASSERT(aBuffer.BufferLength()->Value() >= len); + } else { + // Output buffer is out-of-session, attach a new memory buffer. + aBuffer.Set(PowerOfTwo<Length>(len)); + MOZ_ASSERT(aBuffer.BufferLength()->Value() >= len); + } + // Read start and end indices. + const auto start = aER.ReadObject<ProfileBufferIndex>(); + aBuffer.mFirstReadIndex = + ProfileBufferBlockIndex::CreateFromProfileBufferIndex(start); + const auto end = aER.ReadObject<ProfileBufferIndex>(); + aBuffer.mNextWriteIndex = + ProfileBufferBlockIndex::CreateFromProfileBufferIndex(end); + MOZ_ASSERT(end - start == len); + // Copy bytes into the buffer. + auto writer = + aBuffer.mMaybeUnderlyingBuffer->mBuffer.EntryWriterFromTo(start, end); + writer.WriteFromReader(aER, end - start); + MOZ_ASSERT(writer.RemainingBytes() == 0); + // Finally copy stats. + aBuffer.mMaybeUnderlyingBuffer->mPushedBlockCount = aER.ReadObject< + decltype(aBuffer.mMaybeUnderlyingBuffer->mPushedBlockCount)>(); + aBuffer.mMaybeUnderlyingBuffer->mClearedBlockCount = aER.ReadObject< + decltype(aBuffer.mMaybeUnderlyingBuffer->mClearedBlockCount)>(); + } + + // We cannot output a BlocksRingBuffer object (not copyable), use `ReadInto()` + // or `aER.ReadObject<UniquePtr<BlocksRinbBuffer>>()` instead. + static BlocksRingBuffer Read(ProfileBufferEntryReader& aER) = delete; +}; + +// A BlocksRingBuffer is usually refererenced through a UniquePtr, for +// convenience we support (de)serializing that UniquePtr directly. +// This is compatible with the non-UniquePtr serialization above, with a null +// pointer being treated like an out-of-session or empty buffer; and any of +// these would be deserialized into a null pointer. +template <> +struct ProfileBufferEntryWriter::Serializer<UniquePtr<BlocksRingBuffer>> { + static Length Bytes(const UniquePtr<BlocksRingBuffer>& aBufferUPtr) { + if (!aBufferUPtr) { + // Null pointer, treat it like an empty buffer, i.e., write length of 0. + return ULEB128Size<Length>(0); + } + // Otherwise write the pointed-at BlocksRingBuffer (which could be + // out-of-session or empty.) + return SumBytes(*aBufferUPtr); + } + + static void Write(ProfileBufferEntryWriter& aEW, + const UniquePtr<BlocksRingBuffer>& aBufferUPtr) { + if (!aBufferUPtr) { + // Null pointer, treat it like an empty buffer, i.e., write length of 0. + aEW.WriteULEB128<Length>(0); + return; + } + // Otherwise write the pointed-at BlocksRingBuffer (which could be + // out-of-session or empty.) + aEW.WriteObject(*aBufferUPtr); + } +}; + +template <> +struct ProfileBufferEntryReader::Deserializer<UniquePtr<BlocksRingBuffer>> { + static void ReadInto(ProfileBufferEntryReader& aER, + UniquePtr<BlocksRingBuffer>& aBuffer) { + aBuffer = Read(aER); + } + + static UniquePtr<BlocksRingBuffer> Read(ProfileBufferEntryReader& aER) { + UniquePtr<BlocksRingBuffer> bufferUPtr; + // Keep a copy of the reader before reading the length, so we can restart + // from here below. + ProfileBufferEntryReader readerBeforeLen = aER; + // Read the stored buffer length. + const auto len = aER.ReadULEB128<Length>(); + if (len == 0) { + // 0-length means an "uninteresting" buffer, just return nullptr. + return bufferUPtr; + } + // We have a non-empty buffer. + // allocate an empty BlocksRingBuffer without mutex. + bufferUPtr = MakeUnique<BlocksRingBuffer>( + BlocksRingBuffer::ThreadSafety::WithoutMutex); + // Rewind the reader before the length and deserialize the contents, using + // the non-UniquePtr Deserializer. + aER = readerBeforeLen; + aER.ReadIntoObject(*bufferUPtr); + return bufferUPtr; + } +}; + +} // namespace mozilla + +#endif // BlocksRingBuffer_h diff --git a/mozglue/baseprofiler/public/FailureLatch.h b/mozglue/baseprofiler/public/FailureLatch.h new file mode 100644 index 0000000000..10205e009b --- /dev/null +++ b/mozglue/baseprofiler/public/FailureLatch.h @@ -0,0 +1,217 @@ +/* -*- 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/. */ + +// This header contains an interface `FailureLatch`, and some implementation +// helpers that may be used across a range of classes and functions to handle +// failures at any point during a process, and share that failure state so that +// the process may gracefully stop quickly and report the first error. +// +// It could be thought as a replacement for C++ exceptions, but it's less strong +// (cancellations may be delayed). +// Now, if possible, mozilla::Result may be a better option as C++ exceptions +// replacement, as it is more visible in all affected functions. +// Consider FailureLatch if failures may happen in different places, but where +// `return`ing this potential failure from all functions would be too arduous. + +#ifndef mozilla_FailureLatch_h +#define mozilla_FailureLatch_h + +#include <mozilla/Assertions.h> + +#include <string> + +namespace mozilla { + +// ---------------------------------------------------------------------------- +// Main interface +// ---------------------------------------------------------------------------- + +// Interface handling a failure latch (starting in a successful state, the first +// failure gets recorded, subsequent failures are ignored.) +class FailureLatch { + public: + virtual ~FailureLatch() = default; + + // Can this ever fail? (This may influence how some code deals with + // failures, e.g., if infallible, OOMs should assert&crash.) + [[nodiscard]] virtual bool Fallible() const = 0; + + // Set latch in its failed state because of an external cause. + // The first call sets the reason, subsequent calls are ignored. + virtual void SetFailure(std::string aReason) = 0; + + // Has there been any failure so far? + [[nodiscard]] virtual bool Failed() const = 0; + + // Return first failure string, may be null if not failed yet. + [[nodiscard]] virtual const char* GetFailure() const = 0; + + // Retrieve the one source FailureLatch. It could reference `*this`! + // This may be used by dependent proxy FailureLatch'es to find where to + // redirect calls. + [[nodiscard]] virtual const FailureLatch& SourceFailureLatch() const = 0; + [[nodiscard]] virtual FailureLatch& SourceFailureLatch() = 0; + + // Non-virtual helpers. + + // Transfer any failure from another FailureLatch. + void SetFailureFrom(const FailureLatch& aOther) { + if (Failed()) { + return; + } + if (const char* otherFailure = aOther.GetFailure(); otherFailure) { + SetFailure(otherFailure); + } + } +}; + +// ---------------------------------------------------------------------------- +// Concrete implementations +// ---------------------------------------------------------------------------- + +// Concrete infallible FailureLatch class. +// Any `SetFailure` leads to an assert-crash, so the final runtime result can +// always be assumed to be succesful. +class FailureLatchInfallibleSource final : public FailureLatch { + public: + [[nodiscard]] bool Fallible() const final { return false; } + + void SetFailure(std::string aReason) final { + MOZ_RELEASE_ASSERT(false, + "SetFailure in infallible FailureLatchInfallibleSource"); + } + + [[nodiscard]] bool Failed() const final { return false; } + + [[nodiscard]] const char* GetFailure() const final { return nullptr; } + + [[nodiscard]] const ::mozilla::FailureLatch& SourceFailureLatch() + const final { + return *this; + } + + [[nodiscard]] ::mozilla::FailureLatch& SourceFailureLatch() final { + return *this; + } + + // Singleton FailureLatchInfallibleSource that may be used as default + // FailureLatch proxy. + static FailureLatchInfallibleSource& Singleton() { + static FailureLatchInfallibleSource singleton; + return singleton; + } +}; + +// Concrete FailureLatch class, intended to be intantiated as an object shared +// between classes and functions that are part of a long operation, so that +// failures can happen anywhere and be visible everywhere. +// Not thread-safe. +class FailureLatchSource final : public FailureLatch { + public: + [[nodiscard]] bool Fallible() const final { return true; } + + void SetFailure(std::string aReason) final { + if (!mFailed) { + mFailed = true; + mReason = std::move(aReason); + } + } + + [[nodiscard]] bool Failed() const final { return mFailed; } + + [[nodiscard]] const char* GetFailure() const final { + return mFailed ? mReason.c_str() : nullptr; + } + + [[nodiscard]] const FailureLatch& SourceFailureLatch() const final { + return *this; + } + + [[nodiscard]] FailureLatch& SourceFailureLatch() final { return *this; } + + private: + bool mFailed = false; + std::string mReason; +}; + +// ---------------------------------------------------------------------------- +// Helper macros, to be used in FailureLatch-derived classes +// ---------------------------------------------------------------------------- + +// Classes deriving from FailureLatch can use this to forward virtual calls to +// another FailureLatch. +#define FAILURELATCH_IMPL_PROXY(FAILURELATCH_REF) \ + [[nodiscard]] bool Fallible() const final { \ + return static_cast<const ::mozilla::FailureLatch&>(FAILURELATCH_REF) \ + .Fallible(); \ + } \ + void SetFailure(std::string aReason) final { \ + static_cast<::mozilla::FailureLatch&>(FAILURELATCH_REF) \ + .SetFailure(std::move(aReason)); \ + } \ + [[nodiscard]] bool Failed() const final { \ + return static_cast<const ::mozilla::FailureLatch&>(FAILURELATCH_REF) \ + .Failed(); \ + } \ + [[nodiscard]] const char* GetFailure() const final { \ + return static_cast<const ::mozilla::FailureLatch&>(FAILURELATCH_REF) \ + .GetFailure(); \ + } \ + [[nodiscard]] const FailureLatch& SourceFailureLatch() const final { \ + return static_cast<const ::mozilla::FailureLatch&>(FAILURELATCH_REF) \ + .SourceFailureLatch(); \ + } \ + [[nodiscard]] FailureLatch& SourceFailureLatch() final { \ + return static_cast<::mozilla::FailureLatch&>(FAILURELATCH_REF) \ + .SourceFailureLatch(); \ + } + +// Classes deriving from FailureLatch can use this to forward virtual calls to +// another FailureLatch through a pointer, unless it's null in which case act +// like an infallible FailureLatch. +#define FAILURELATCH_IMPL_PROXY_OR_INFALLIBLE(FAILURELATCH_PTR, CLASS_NAME) \ + [[nodiscard]] bool Fallible() const final { \ + return FAILURELATCH_PTR \ + ? static_cast<const ::mozilla::FailureLatch*>(FAILURELATCH_PTR) \ + ->Fallible() \ + : false; \ + } \ + void SetFailure(std::string aReason) final { \ + if (FAILURELATCH_PTR) { \ + static_cast<::mozilla::FailureLatch*>(FAILURELATCH_PTR) \ + ->SetFailure(std::move(aReason)); \ + } else { \ + MOZ_RELEASE_ASSERT(false, "SetFailure in infallible " #CLASS_NAME); \ + } \ + } \ + [[nodiscard]] bool Failed() const final { \ + return FAILURELATCH_PTR \ + ? static_cast<const ::mozilla::FailureLatch*>(FAILURELATCH_PTR) \ + ->Failed() \ + : false; \ + } \ + [[nodiscard]] const char* GetFailure() const final { \ + return FAILURELATCH_PTR \ + ? static_cast<const ::mozilla::FailureLatch*>(FAILURELATCH_PTR) \ + ->GetFailure() \ + : nullptr; \ + } \ + [[nodiscard]] const FailureLatch& SourceFailureLatch() const final { \ + return FAILURELATCH_PTR \ + ? static_cast<const ::mozilla::FailureLatch*>(FAILURELATCH_PTR) \ + ->SourceFailureLatch() \ + : ::mozilla::FailureLatchInfallibleSource::Singleton(); \ + } \ + [[nodiscard]] FailureLatch& SourceFailureLatch() final { \ + return FAILURELATCH_PTR \ + ? static_cast<::mozilla::FailureLatch*>(FAILURELATCH_PTR) \ + ->SourceFailureLatch() \ + : ::mozilla::FailureLatchInfallibleSource::Singleton(); \ + } + +} // namespace mozilla + +#endif /* mozilla_FailureLatch_h */ diff --git a/mozglue/baseprofiler/public/ModuloBuffer.h b/mozglue/baseprofiler/public/ModuloBuffer.h new file mode 100644 index 0000000000..80e765279e --- /dev/null +++ b/mozglue/baseprofiler/public/ModuloBuffer.h @@ -0,0 +1,618 @@ +/* -*- Mode: C++; tab-width: 2; 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/. */ + +#ifndef ModuloBuffer_h +#define ModuloBuffer_h + +#include "mozilla/leb128iterator.h" +#include "mozilla/Maybe.h" +#include "mozilla/MemoryReporting.h" +#include "mozilla/NotNull.h" +#include "mozilla/PowerOfTwo.h" +#include "mozilla/ProfileBufferEntrySerialization.h" +#include "mozilla/UniquePtr.h" + +#include <functional> +#include <iterator> +#include <limits> +#include <type_traits> + +namespace mozilla { + +// The ModuloBuffer class is a circular buffer that holds raw byte values, with +// data-read/write helpers. +// +// OffsetT: Type of the internal offset into the buffer of bytes, it should be +// large enough to access all bytes of the buffer. It will also be used as +// Length (in bytes) of the buffer and of any subset. Default uint32_t +// IndexT: Type of the external index, it should be large enough that overflows +// should not happen during the lifetime of the ModuloBuffer. +// +// The basic usage is to create an iterator-like object with `ReaderAt(Index)` +// or `WriterAt(Index)`, and use it to read/write data blobs. Iterators +// automatically manage the wrap-around (through "Modulo", which is effectively +// an AND-masking with the PowerOfTwo buffer size.) +// +// There is zero safety: No thread safety, no checks that iterators may be +// overwriting data that's still to be read, etc. It's up to the caller to add +// adequate checks. +// The intended use is as an underlying buffer for a safer container. +template <typename OffsetT = uint32_t, typename IndexT = uint64_t> +class ModuloBuffer { + public: + using Byte = uint8_t; + static_assert(sizeof(Byte) == 1, "ModuloBuffer::Byte must be 1 byte"); + using Offset = OffsetT; + static_assert(!std::numeric_limits<Offset>::is_signed, + "ModuloBuffer::Offset must be an unsigned integral type"); + using Length = Offset; + using Index = IndexT; + static_assert(!std::numeric_limits<Index>::is_signed, + "ModuloBuffer::Index must be an unsigned integral type"); + static_assert(sizeof(Index) >= sizeof(Offset), + "ModuloBuffer::Index size must >= Offset"); + + // Create a buffer of the given length. + explicit ModuloBuffer(PowerOfTwo<Length> aLength) + : mMask(aLength.Mask()), + mBuffer(WrapNotNull(new Byte[aLength.Value()])), + mBufferDeleter([](Byte* aBuffer) { delete[] aBuffer; }) {} + + // Take ownership of an existing buffer. Existing contents is ignored. + // Done by extracting the raw pointer from UniquePtr<Byte[]>, and adding + // an equivalent `delete[]` in `mBufferDeleter`. + ModuloBuffer(UniquePtr<Byte[]> aExistingBuffer, PowerOfTwo<Length> aLength) + : mMask(aLength.Mask()), + mBuffer(WrapNotNull(aExistingBuffer.release())), + mBufferDeleter([](Byte* aBuffer) { delete[] aBuffer; }) {} + + // Use an externally-owned buffer. Existing contents is ignored. + ModuloBuffer(Byte* aExternalBuffer, PowerOfTwo<Length> aLength) + : mMask(aLength.Mask()), mBuffer(WrapNotNull(aExternalBuffer)) {} + + // Disallow copying, as we may uniquely own the resource. + ModuloBuffer(const ModuloBuffer& aOther) = delete; + ModuloBuffer& operator=(const ModuloBuffer& aOther) = delete; + + // Allow move-construction. Stealing ownership if the original had it. + // This effectively prevents copy construction, and all assignments; needed so + // that a ModuloBuffer may be initialized from a separate construction. + // The moved-from ModuloBuffer still points at the resource but doesn't own + // it, so it won't try to free it; but accesses are not guaranteed, so it + // should not be used anymore. + ModuloBuffer(ModuloBuffer&& aOther) + : mMask(std::move(aOther.mMask)), + mBuffer(std::move(aOther.mBuffer)), + mBufferDeleter(std::move(aOther.mBufferDeleter)) { + // The above move leaves `aOther.mBufferDeleter` in a valid state but with + // an unspecified value, so it could theoretically still contain the + // original function, which would be bad because we don't want aOther to + // delete the resource that `this` now owns. + if (aOther.mBufferDeleter) { + // `aOther` still had a non-empty deleter, reset it. + aOther.mBufferDeleter = nullptr; + } + } + + // Disallow assignment, as we have some `const` members. + ModuloBuffer& operator=(ModuloBuffer&& aOther) = delete; + + // Destructor, deletes the resource if we uniquely own it. + ~ModuloBuffer() { + if (mBufferDeleter) { + mBufferDeleter(mBuffer); + } + } + + PowerOfTwo<Length> BufferLength() const { + return PowerOfTwo<Length>(mMask.MaskValue() + 1); + } + + // Size of external resources. + // Note: `mBufferDeleter`'s potential external data (for its captures) is not + // included, as it's hidden in the `std::function` implementation. + size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const { + if (!mBufferDeleter) { + // If we don't have a buffer deleter, assume we don't own the data, so + // it's probably on the stack, or should be reported by its owner. + return 0; + } + return aMallocSizeOf(mBuffer); + } + + size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const { + return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf); + } + + ProfileBufferEntryReader EntryReaderFromTo( + Index aStart, Index aEnd, ProfileBufferBlockIndex aBlockIndex, + ProfileBufferBlockIndex aNextBlockIndex) const { + using EntrySpan = Span<const ProfileBufferEntryReader::Byte>; + if (aStart == aEnd) { + return ProfileBufferEntryReader{}; + } + // Don't allow over-wrapping. + MOZ_ASSERT(aEnd - aStart <= mMask.MaskValue() + 1); + // Start offset in 0 .. (buffer size - 1) + Offset start = static_cast<Offset>(aStart) & mMask; + // End offset in 1 .. (buffer size) + Offset end = (static_cast<Offset>(aEnd - 1) & mMask) + 1; + if (start < end) { + // Segment doesn't cross buffer threshold, one span is enough. + return ProfileBufferEntryReader{EntrySpan(&mBuffer[start], end - start), + aBlockIndex, aNextBlockIndex}; + } + // Segment crosses buffer threshold, we need one span until the end and one + // span restarting at the beginning of the buffer. + return ProfileBufferEntryReader{ + EntrySpan(&mBuffer[start], mMask.MaskValue() + 1 - start), + EntrySpan(&mBuffer[0], end), aBlockIndex, aNextBlockIndex}; + } + + // Return an entry writer for the given range. + ProfileBufferEntryWriter EntryWriterFromTo(Index aStart, Index aEnd) const { + using EntrySpan = Span<ProfileBufferEntryReader::Byte>; + if (aStart == aEnd) { + return ProfileBufferEntryWriter{}; + } + MOZ_ASSERT(aEnd - aStart <= mMask.MaskValue() + 1); + // Start offset in 0 .. (buffer size - 1) + Offset start = static_cast<Offset>(aStart) & mMask; + // End offset in 1 .. (buffer size) + Offset end = (static_cast<Offset>(aEnd - 1) & mMask) + 1; + if (start < end) { + // Segment doesn't cross buffer threshold, one span is enough. + return ProfileBufferEntryWriter{ + EntrySpan(&mBuffer[start], end - start), + ProfileBufferBlockIndex::CreateFromProfileBufferIndex(aStart), + ProfileBufferBlockIndex::CreateFromProfileBufferIndex(aEnd)}; + } + // Segment crosses buffer threshold, we need one span until the end and one + // span restarting at the beginning of the buffer. + return ProfileBufferEntryWriter{ + EntrySpan(&mBuffer[start], mMask.MaskValue() + 1 - start), + EntrySpan(&mBuffer[0], end), + ProfileBufferBlockIndex::CreateFromProfileBufferIndex(aStart), + ProfileBufferBlockIndex::CreateFromProfileBufferIndex(aEnd)}; + } + + // Emplace an entry writer into `aMaybeEntryWriter` for the given range. + void EntryWriterFromTo(Maybe<ProfileBufferEntryWriter>& aMaybeEntryWriter, + Index aStart, Index aEnd) const { + MOZ_ASSERT(aMaybeEntryWriter.isNothing(), + "Reference entry writer should be Nothing."); + using EntrySpan = Span<ProfileBufferEntryReader::Byte>; + if (aStart == aEnd) { + return; + } + MOZ_ASSERT(aEnd - aStart <= mMask.MaskValue() + 1); + // Start offset in 0 .. (buffer size - 1) + Offset start = static_cast<Offset>(aStart) & mMask; + // End offset in 1 .. (buffer size) + Offset end = (static_cast<Offset>(aEnd - 1) & mMask) + 1; + if (start < end) { + // Segment doesn't cross buffer threshold, one span is enough. + aMaybeEntryWriter.emplace( + EntrySpan(&mBuffer[start], end - start), + ProfileBufferBlockIndex::CreateFromProfileBufferIndex(aStart), + ProfileBufferBlockIndex::CreateFromProfileBufferIndex(aEnd)); + } else { + // Segment crosses buffer threshold, we need one span until the end and + // one span restarting at the beginning of the buffer. + aMaybeEntryWriter.emplace( + EntrySpan(&mBuffer[start], mMask.MaskValue() + 1 - start), + EntrySpan(&mBuffer[0], end), + ProfileBufferBlockIndex::CreateFromProfileBufferIndex(aStart), + ProfileBufferBlockIndex::CreateFromProfileBufferIndex(aEnd)); + } + } + + // All ModuloBuffer operations should be done through this iterator, which has + // an effectively infinite range. The underlying wrapping-around is hidden. + // Use `ReaderAt(Index)` or `WriterAt(Index)` to create it. + // + // `const Iterator<...>` means the iterator itself cannot change, i.e., it + // cannot move, and only its const methods are available. Note that these + // const methods may still be used to modify the buffer contents (e.g.: + // `operator*()`, `Poke()`). + // + // `Iterator</*IsBufferConst=*/true>` means the buffer contents cannot be + // modified, i.e., write operations are forbidden, but the iterator may still + // move if non-const itself. + template <bool IsBufferConst> + class Iterator { + // Alias to const- or mutable-`ModuloBuffer` depending on `IsBufferConst`. + using ConstOrMutableBuffer = + std::conditional_t<IsBufferConst, const ModuloBuffer, ModuloBuffer>; + + // Implementation note about the strange enable-if's below: + // `template <bool NotIBC = !IsBufferConst> enable_if_t<NotIBC>` + // which intuitively could be simplified to: + // `enable_if_t<!IsBufferConst>` + // The former extra-templated syntax is in fact necessary to delay + // instantiation of these functions until they are actually needed. + // + // If we were just doing `enable_if_t<!IsBufferConst>`, this would only + // depend on the *class* (`ModuloBuffer<...>::Iterator`), which gets + // instantiated when a `ModuloBuffer` is created with some template + // arguments; at that point, all non-templated methods get instantiated, so + // there's no "SFINAE" happening, and `enable_if_t<...>` is actually doing + // `typename enable_if<...>::type` on the spot, but there is no `type` if + // `IsBufferConst` is true, so it just fails right away. E.g.: + // error: no type named 'type' in 'std::enable_if<false, void>'; + // 'enable_if' cannot be used to disable this declaration + // note: in instantiation of template type alias 'enable_if_t' + // > std::enable_if_t<!IsBufferConst> WriteObject(const T& aObject) { + // in instantiation of template class + // 'mozilla::ModuloBuffer<...>::Iterator<true>' + // > auto it = mb.ReaderAt(1); + // + // By adding another template level `template <bool NotIsBufferConst = + // !IsBufferConst>`, the instantiation is delayed until the function is + // actually invoked somewhere, e.g. `it.Poke(...);`. + // So at that invocation point, the compiler looks for a "Poke" name in it, + // and considers potential template instantiations that could work. The + // `enable_if_t` is *now* attempted, with `NotIsBufferConst` taking its + // value from `!IsBufferConst`: + // - If `IsBufferConst` is false, `NotIsBufferConst` is true, + // `enable_if<NotIsBufferConst>` does define a `type` (`void` by default), + // so `enable_if_t` happily becomes `void`, the function exists and may be + // called. + // - Otherwise if `IsBufferConst` is true, `NotIsBufferConst` is false, + // `enable_if<NotIsBufferConst>` does *not* define a `type`, therefore + // `enable_if_t` produces an error because there is no `type`. Now "SFINAE" + // happens: This "Substitution Failure Is Not An Error" (by itself)... But + // then, there are no other functions named "Poke" as requested in the + // `it.Poke(...);` call, so we are now getting an error (can't find + // function), as expected because `it` had `IsBufferConst`==true. (But at + // least the compiler waited until this invocation attempt before outputting + // an error.) + // + // C++ is fun! + + public: + // These definitions are expected by std functions, to recognize this as an + // iterator. See https://en.cppreference.com/w/cpp/iterator/iterator_traits + using difference_type = Index; + using value_type = Byte; + using pointer = std::conditional_t<IsBufferConst, const Byte*, Byte*>; + using reference = std::conditional_t<IsBufferConst, const Byte&, Byte&>; + using iterator_category = std::random_access_iterator_tag; + + // Can always copy/assign from the same kind of iterator. + Iterator(const Iterator& aRhs) = default; + Iterator& operator=(const Iterator& aRhs) = default; + + // Can implicitly copy an Iterator-to-mutable (reader+writer) to + // Iterator-to-const (reader-only), but not the reverse. + template <bool IsRhsBufferConst, + typename = std::enable_if_t<(!IsRhsBufferConst) && IsBufferConst>> + MOZ_IMPLICIT Iterator(const Iterator<IsRhsBufferConst>& aRhs) + : mModuloBuffer(aRhs.mModuloBuffer), mIndex(aRhs.mIndex) {} + + // Can implicitly assign from an Iterator-to-mutable (reader+writer) to + // Iterator-to-const (reader-only), but not the reverse. + template <bool IsRhsBufferConst, + typename = std::enable_if_t<(!IsRhsBufferConst) && IsBufferConst>> + Iterator& operator=(const Iterator<IsRhsBufferConst>& aRhs) { + mModuloBuffer = aRhs.mModuloBuffer; + mIndex = aRhs.mIndex; + return *this; + } + + // Current location of the iterator in the `Index` range. + // Note that due to wrapping, multiple indices may effectively point at the + // same byte in the buffer. + Index CurrentIndex() const { return mIndex; } + + // Location comparison in the `Index` range. I.e., two `Iterator`s may look + // unequal, but refer to the same buffer location. + // Must be on the same buffer. + bool operator==(const Iterator& aRhs) const { + MOZ_ASSERT(mModuloBuffer == aRhs.mModuloBuffer); + return mIndex == aRhs.mIndex; + } + bool operator!=(const Iterator& aRhs) const { + MOZ_ASSERT(mModuloBuffer == aRhs.mModuloBuffer); + return mIndex != aRhs.mIndex; + } + bool operator<(const Iterator& aRhs) const { + MOZ_ASSERT(mModuloBuffer == aRhs.mModuloBuffer); + return mIndex < aRhs.mIndex; + } + bool operator<=(const Iterator& aRhs) const { + MOZ_ASSERT(mModuloBuffer == aRhs.mModuloBuffer); + return mIndex <= aRhs.mIndex; + } + bool operator>(const Iterator& aRhs) const { + MOZ_ASSERT(mModuloBuffer == aRhs.mModuloBuffer); + return mIndex > aRhs.mIndex; + } + bool operator>=(const Iterator& aRhs) const { + MOZ_ASSERT(mModuloBuffer == aRhs.mModuloBuffer); + return mIndex >= aRhs.mIndex; + } + + // Movement in the `Index` range. + Iterator& operator++() { + ++mIndex; + return *this; + } + Iterator operator++(int) { + Iterator here(*mModuloBuffer, mIndex); + ++mIndex; + return here; + } + Iterator& operator--() { + --mIndex; + return *this; + } + Iterator operator--(int) { + Iterator here(*mModuloBuffer, mIndex); + --mIndex; + return here; + } + Iterator& operator+=(Length aLength) { + mIndex += aLength; + return *this; + } + Iterator operator+(Length aLength) const { + return Iterator(*mModuloBuffer, mIndex + aLength); + } + friend Iterator operator+(Length aLength, const Iterator& aIt) { + return aIt + aLength; + } + Iterator& operator-=(Length aLength) { + mIndex -= aLength; + return *this; + } + Iterator operator-(Length aLength) const { + return Iterator(*mModuloBuffer, mIndex - aLength); + } + + // Distance from `aRef` to here in the `Index` range. + // May be negative (as 2's complement) if `aRef > *this`. + Index operator-(const Iterator& aRef) const { + MOZ_ASSERT(mModuloBuffer == aRef.mModuloBuffer); + return mIndex - aRef.mIndex; + } + + // Dereference a single byte (read-only if `IsBufferConst` is true). + reference operator*() const { + return mModuloBuffer->mBuffer[OffsetInBuffer()]; + } + + // Random-access dereference. + reference operator[](Length aLength) const { return *(*this + aLength); } + + // Write data (if `IsBufferConst` is false) but don't move iterator. + template <bool NotIsBufferConst = !IsBufferConst> + std::enable_if_t<NotIsBufferConst> Poke(const void* aSrc, + Length aLength) const { + // Don't allow data larger than the buffer. + MOZ_ASSERT(aLength <= mModuloBuffer->BufferLength().Value()); + // Offset inside the buffer (corresponding to our Index). + Offset offset = OffsetInBuffer(); + // Compute remaining bytes between this offset and the end of the buffer. + Length remaining = mModuloBuffer->BufferLength().Value() - offset; + if (MOZ_LIKELY(remaining >= aLength)) { + // Enough space to write everything before the end. + memcpy(&mModuloBuffer->mBuffer[offset], aSrc, aLength); + } else { + // Not enough space. Write as much as possible before the end. + memcpy(&mModuloBuffer->mBuffer[offset], aSrc, remaining); + // And then continue from the beginning of the buffer. + memcpy(&mModuloBuffer->mBuffer[0], + static_cast<const Byte*>(aSrc) + remaining, + (aLength - remaining)); + } + } + + // Write object data (if `IsBufferConst` is false) but don't move iterator. + // Note that this copies bytes from the object, with the intent to read them + // back later. Restricted to trivially-copyable types, which support this + // without Undefined Behavior! + template <typename T, bool NotIsBufferConst = !IsBufferConst> + std::enable_if_t<NotIsBufferConst> PokeObject(const T& aObject) const { + static_assert(std::is_trivially_copyable<T>::value, + "PokeObject<T> - T must be trivially copyable"); + return Poke(&aObject, sizeof(T)); + } + + // Write data (if `IsBufferConst` is false) and move iterator ahead. + template <bool NotIsBufferConst = !IsBufferConst> + std::enable_if_t<NotIsBufferConst> Write(const void* aSrc, Length aLength) { + Poke(aSrc, aLength); + mIndex += aLength; + } + + // Write object data (if `IsBufferConst` is false) and move iterator ahead. + // Note that this copies bytes from the object, with the intent to read them + // back later. Restricted to trivially-copyable types, which support this + // without Undefined Behavior! + template <typename T, bool NotIsBufferConst = !IsBufferConst> + std::enable_if_t<NotIsBufferConst> WriteObject(const T& aObject) { + static_assert(std::is_trivially_copyable<T>::value, + "WriteObject<T> - T must be trivially copyable"); + return Write(&aObject, sizeof(T)); + } + + // Number of bytes needed to represent `aValue` in unsigned LEB128. + template <typename T> + static unsigned ULEB128Size(T aValue) { + return ::mozilla::ULEB128Size(aValue); + } + + // Write number as unsigned LEB128 (if `IsBufferConst` is false) and move + // iterator ahead. + template <typename T, bool NotIsBufferConst = !IsBufferConst> + std::enable_if_t<NotIsBufferConst> WriteULEB128(T aValue) { + ::mozilla::WriteULEB128(aValue, *this); + } + + // Read data but don't move iterator. + void Peek(void* aDst, Length aLength) const { + // Don't allow data larger than the buffer. + MOZ_ASSERT(aLength <= mModuloBuffer->BufferLength().Value()); + // Offset inside the buffer (corresponding to our Index). + Offset offset = OffsetInBuffer(); + // Compute remaining bytes between this offset and the end of the buffer. + Length remaining = mModuloBuffer->BufferLength().Value() - offset; + if (MOZ_LIKELY(remaining >= aLength)) { + // Can read everything we need before the end of the buffer. + memcpy(aDst, &mModuloBuffer->mBuffer[offset], aLength); + } else { + // Read as much as possible before the end of the buffer. + memcpy(aDst, &mModuloBuffer->mBuffer[offset], remaining); + // And then continue from the beginning of the buffer. + memcpy(static_cast<Byte*>(aDst) + remaining, &mModuloBuffer->mBuffer[0], + (aLength - remaining)); + } + } + + // Read data into an object but don't move iterator. + // Note that this overwrites `aObject` with bytes from the buffer. + // Restricted to trivially-copyable types, which support this without + // Undefined Behavior! + template <typename T> + void PeekIntoObject(T& aObject) const { + static_assert(std::is_trivially_copyable<T>::value, + "PeekIntoObject<T> - T must be trivially copyable"); + Peek(&aObject, sizeof(T)); + } + + // Read data as an object but don't move iterator. + // Note that this creates an default `T` first, and then overwrites it with + // bytes from the buffer. Restricted to trivially-copyable types, which + // support this without Undefined Behavior! + template <typename T> + T PeekObject() const { + static_assert(std::is_trivially_copyable<T>::value, + "PeekObject<T> - T must be trivially copyable"); + T object; + PeekIntoObject(object); + return object; + } + + // Read data and move iterator ahead. + void Read(void* aDst, Length aLength) { + Peek(aDst, aLength); + mIndex += aLength; + } + + // Read data into a mutable iterator and move both iterators ahead. + void ReadInto(Iterator</* IsBufferConst */ false>& aDst, Length aLength) { + // Don't allow data larger than the buffer. + MOZ_ASSERT(aLength <= mModuloBuffer->BufferLength().Value()); + MOZ_ASSERT(aLength <= aDst.mModuloBuffer->BufferLength().Value()); + // Offset inside the buffer (corresponding to our Index). + Offset offset = OffsetInBuffer(); + // Compute remaining bytes between this offset and the end of the buffer. + Length remaining = mModuloBuffer->BufferLength().Value() - offset; + if (MOZ_LIKELY(remaining >= aLength)) { + // Can read everything we need before the end of the buffer. + aDst.Write(&mModuloBuffer->mBuffer[offset], aLength); + } else { + // Read as much as possible before the end of the buffer. + aDst.Write(&mModuloBuffer->mBuffer[offset], remaining); + // And then continue from the beginning of the buffer. + aDst.Write(&mModuloBuffer->mBuffer[0], (aLength - remaining)); + } + mIndex += aLength; + } + + // Read data into an object and move iterator ahead. + // Note that this overwrites `aObject` with bytes from the buffer. + // Restricted to trivially-copyable types, which support this without + // Undefined Behavior! + template <typename T> + void ReadIntoObject(T& aObject) { + static_assert(std::is_trivially_copyable<T>::value, + "ReadIntoObject<T> - T must be trivially copyable"); + Read(&aObject, sizeof(T)); + } + + // Read data as an object and move iterator ahead. + // Note that this creates an default `T` first, and then overwrites it with + // bytes from the buffer. Restricted to trivially-copyable types, which + // support this without Undefined Behavior! + template <typename T> + T ReadObject() { + static_assert(std::is_trivially_copyable<T>::value, + "ReadObject<T> - T must be trivially copyable"); + T object; + ReadIntoObject(object); + return object; + } + + // Read an unsigned LEB128 number and move iterator ahead. + template <typename T> + T ReadULEB128() { + return ::mozilla::ReadULEB128<T>(*this); + } + + private: + // Only a ModuloBuffer can instantiate its iterator. + friend class ModuloBuffer; + + Iterator(ConstOrMutableBuffer& aBuffer, Index aIndex) + : mModuloBuffer(WrapNotNull(&aBuffer)), mIndex(aIndex) {} + + // Convert the Iterator's mIndex into an offset inside the byte buffer. + Offset OffsetInBuffer() const { + return static_cast<Offset>(mIndex) & mModuloBuffer->mMask; + } + + // ModuloBuffer that this Iterator operates on. + // Using a non-null pointer instead of a reference, to allow re-assignment + // of an Iterator variable. + NotNull<ConstOrMutableBuffer*> mModuloBuffer; + + // Position of this iterator in the wider `Index` range. (Will be wrapped + // around as needed when actually accessing bytes from the buffer.) + Index mIndex; + }; + + // Shortcut to iterator to const (read-only) data. + using Reader = Iterator<true>; + // Shortcut to iterator to non-const (read/write) data. + using Writer = Iterator<false>; + + // Create an iterator to const data at the given index. + Reader ReaderAt(Index aIndex) const { return Reader(*this, aIndex); } + + // Create an iterator to non-const data at the given index. + Writer WriterAt(Index aIndex) { return Writer(*this, aIndex); } + +#ifdef DEBUG + void Dump() const { + Length len = BufferLength().Value(); + if (len > 128) { + len = 128; + } + for (Length i = 0; i < len; ++i) { + printf("%02x ", mBuffer[i]); + } + printf("\n"); + } +#endif // DEBUG + + private: + // Mask used to convert an index to an offset in `mBuffer` + const PowerOfTwoMask<Offset> mMask; + + // Buffer data. `const NotNull<...>` shows that `mBuffer is `const`, and + // `Byte* const` shows that the pointer cannot be changed to point at + // something else, but the pointed-at `Byte`s are writable. + const NotNull<Byte* const> mBuffer; + + // Function used to release the buffer resource (if needed). + std::function<void(Byte*)> mBufferDeleter; +}; + +} // namespace mozilla + +#endif // ModuloBuffer_h diff --git a/mozglue/baseprofiler/public/PowerOfTwo.h b/mozglue/baseprofiler/public/PowerOfTwo.h new file mode 100644 index 0000000000..7d396c15e6 --- /dev/null +++ b/mozglue/baseprofiler/public/PowerOfTwo.h @@ -0,0 +1,322 @@ +/* -*- Mode: C++; tab-width: 2; 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/. */ + +// PowerOfTwo is a value type that always hold a power of 2. +// It has the same size as their underlying unsigned type, but offer the +// guarantee of being a power of 2, which permits some optimizations when +// involved in modulo operations (using masking instead of actual modulo). +// +// PowerOfTwoMask contains a mask corresponding to a power of 2. +// E.g., 2^8 is 256 or 0x100, the corresponding mask is 2^8-1 or 255 or 0xFF. +// It should be used instead of PowerOfTwo in situations where most operations +// would be modulo, this saves having to recompute the mask from the stored +// power of 2. +// +// One common use would be for ring-buffer containers with a power-of-2 size, +// where an index is usually converted to an in-buffer offset by `i % size`. +// Instead, the container could store a PowerOfTwo or PowerOfTwoMask, and do +// `i % p2` or `i & p2m`, which is more efficient than for arbitrary sizes. +// +// Shortcuts for common 32- and 64-bit values: PowerOfTwo32, etc. +// +// To create constexpr constants, use MakePowerOfTwo<Type, Value>(), etc. + +#ifndef PowerOfTwo_h +#define PowerOfTwo_h + +#include "mozilla/MathAlgorithms.h" + +#include <limits> + +namespace mozilla { + +// Compute the smallest power of 2 greater than or equal to aInput, except if +// that would overflow in which case the highest possible power of 2 if chosen. +// 0->1, 1->1, 2->2, 3->4, ... 2^31->2^31, 2^31+1->2^31 (for uint32_t), etc. +template <typename T> +T FriendlyRoundUpPow2(T aInput) { + // This is the same code as `RoundUpPow2()`, except we handle any type (that + // CeilingLog2 supports) and allow the greater-than-max-power case. + constexpr T max = T(1) << (sizeof(T) * CHAR_BIT - 1); + if (aInput >= max) { + return max; + } + return T(1) << CeilingLog2(aInput); +} + +namespace detail { +// Same function name `CountLeadingZeroes` with uint32_t and uint64_t overloads. +inline uint_fast8_t CountLeadingZeroes(uint32_t aValue) { + MOZ_ASSERT(aValue != 0); + return detail::CountLeadingZeroes32(aValue); +} +inline uint_fast8_t CountLeadingZeroes(uint64_t aValue) { + MOZ_ASSERT(aValue != 0); + return detail::CountLeadingZeroes64(aValue); +} +// Refuse anything else. +template <typename T> +inline uint_fast8_t CountLeadingZeroes(T aValue) = delete; +} // namespace detail + +// Compute the smallest 2^N-1 mask where aInput can fit. +// I.e., `x & mask == x`, but `x & (mask >> 1) != x`. +// Or looking at binary, we want a mask with as many leading zeroes as the +// input, by right-shifting a full mask: (8-bit examples) +// input: 00000000 00000001 00000010 00010110 01111111 10000000 +// N leading 0s: ^^^^^^^^ 8 ^^^^^^^ 7 ^^^^^^ 6 ^^^ 3 ^ 1 0 +// full mask: 11111111 11111111 11111111 11111111 11111111 11111111 +// full mask >> N: 00000000 00000001 00000011 00011111 01111111 11111111 +template <typename T> +T RoundUpPow2Mask(T aInput) { + // Special case, as CountLeadingZeroes(0) is undefined. (And even if that was + // defined, shifting by the full type size is also undefined!) + if (aInput == 0) { + return 0; + } + return T(-1) >> detail::CountLeadingZeroes(aInput); +} + +template <typename T> +class PowerOfTwoMask; + +template <typename T, T Mask> +constexpr PowerOfTwoMask<T> MakePowerOfTwoMask(); + +template <typename T> +class PowerOfTwo; + +template <typename T, T Value> +constexpr PowerOfTwo<T> MakePowerOfTwo(); + +// PowerOfTwoMask will always contain a mask for a power of 2, which is useful +// for power-of-2 modulo operations (e.g., to keep an index inside a power-of-2 +// container). +// Use this instead of PowerOfTwo if masking is the primary use of the value. +// +// Note that this class can store a "full" mask where all bits are set, so it +// works for mask corresponding to the power of 2 that would overflow `T` +// (e.g., 2^32 for uint32_t gives a mask of 2^32-1, which fits in a uint32_t). +// For this reason there is no API that computes the power of 2 corresponding to +// the mask; But this can be done explicitly with `MaskValue() + 1`, which may +// be useful for computing things like distance-to-the-end by doing +// `MaskValue() + 1 - offset`, which works fine with unsigned number types. +template <typename T> +class PowerOfTwoMask { + static_assert(!std::numeric_limits<T>::is_signed, + "PowerOfTwoMask must use an unsigned type"); + + public: + // Construct a power of 2 mask where the given value can fit. + // Cannot be constexpr because of `RoundUpPow2Mask()`. + explicit PowerOfTwoMask(T aInput) : mMask(RoundUpPow2Mask(aInput)) {} + + // Compute the mask corresponding to a PowerOfTwo. + // This saves having to compute the nearest 2^N-1. + // Not a conversion constructor, as that could be ambiguous whether we'd want + // the mask corresponding to the power of 2 (2^N -> 2^N-1), or the mask that + // can *contain* the PowerOfTwo value (2^N -> 2^(N+1)-1). + // Note: Not offering reverse PowerOfTwoMark-to-PowerOfTwo conversion, because + // that could result in an unexpected 0 result for the largest possible mask. + template <typename U> + static constexpr PowerOfTwoMask<U> MaskForPowerOfTwo( + const PowerOfTwo<U>& aP2) { + return PowerOfTwoMask(aP2); + } + + // Allow smaller unsigned types as input. + // Bigger or signed types must be explicitly converted by the caller. + template <typename U> + explicit constexpr PowerOfTwoMask(U aInput) + : mMask(RoundUpPow2Mask(static_cast<T>(aInput))) { + static_assert(!std::numeric_limits<T>::is_signed, + "PowerOfTwoMask does not accept signed types"); + static_assert(sizeof(U) <= sizeof(T), + "PowerOfTwoMask does not accept bigger types"); + } + + constexpr T MaskValue() const { return mMask; } + + // `x & aPowerOfTwoMask` just works. + template <typename U> + friend U operator&(U aNumber, PowerOfTwoMask aP2M) { + return static_cast<U>(aNumber & aP2M.MaskValue()); + } + + // `aPowerOfTwoMask & x` just works. + template <typename U> + friend constexpr U operator&(PowerOfTwoMask aP2M, U aNumber) { + return static_cast<U>(aP2M.MaskValue() & aNumber); + } + + // `x % aPowerOfTwoMask(2^N-1)` is equivalent to `x % 2^N` but is more + // optimal by doing `x & (2^N-1)`. + // Useful for templated code doing modulo with a template argument type. + template <typename U> + friend constexpr U operator%(U aNumerator, PowerOfTwoMask aDenominator) { + return aNumerator & aDenominator.MaskValue(); + } + + constexpr bool operator==(const PowerOfTwoMask& aRhs) const { + return mMask == aRhs.mMask; + } + constexpr bool operator!=(const PowerOfTwoMask& aRhs) const { + return mMask != aRhs.mMask; + } + + private: + // Trust `PowerOfTwo` to call the private Trusted constructor below. + friend class PowerOfTwo<T>; + + // Trust `MakePowerOfTwoMask()` to call the private Trusted constructor below. + template <typename U, U Mask> + friend constexpr PowerOfTwoMask<U> MakePowerOfTwoMask(); + + struct Trusted { + T mMask; + }; + // Construct the mask corresponding to a PowerOfTwo. + // This saves having to compute the nearest 2^N-1. + // Note: Not a public PowerOfTwo->PowerOfTwoMask conversion constructor, as + // that could be ambiguous whether we'd want the mask corresponding to the + // power of 2 (2^N -> 2^N-1), or the mask that can *contain* the PowerOfTwo + // value (2^N -> 2^(N+1)-1). + explicit constexpr PowerOfTwoMask(const Trusted& aP2) : mMask(aP2.mMask) {} + + T mMask = 0; +}; + +// Make a PowerOfTwoMask constant, statically-checked. +template <typename T, T Mask> +constexpr PowerOfTwoMask<T> MakePowerOfTwoMask() { + static_assert(Mask == T(-1) || IsPowerOfTwo(Mask + 1), + "MakePowerOfTwoMask<T, Mask>: Mask must be 2^N-1"); + using Trusted = typename PowerOfTwoMask<T>::Trusted; + return PowerOfTwoMask<T>(Trusted{Mask}); +} + +// PowerOfTwo will always contain a power of 2. +template <typename T> +class PowerOfTwo { + static_assert(!std::numeric_limits<T>::is_signed, + "PowerOfTwo must use an unsigned type"); + + public: + // Construct a power of 2 that can fit the given value, or the highest power + // of 2 possible. + // Caller should explicitly check/assert `Value() <= aInput` if they want to. + // Cannot be constexpr because of `FriendlyRoundUpPow2()`. + explicit PowerOfTwo(T aInput) : mValue(FriendlyRoundUpPow2(aInput)) {} + + // Allow smaller unsigned types as input. + // Bigger or signed types must be explicitly converted by the caller. + template <typename U> + explicit PowerOfTwo(U aInput) + : mValue(FriendlyRoundUpPow2(static_cast<T>(aInput))) { + static_assert(!std::numeric_limits<T>::is_signed, + "PowerOfTwo does not accept signed types"); + static_assert(sizeof(U) <= sizeof(T), + "PowerOfTwo does not accept bigger types"); + } + + constexpr T Value() const { return mValue; } + + // Binary mask corresponding to the power of 2, useful for modulo. + // E.g., `x & powerOfTwo(y).Mask()` == `x % powerOfTwo(y)`. + // Consider PowerOfTwoMask class instead of PowerOfTwo if masking is the + // primary use case. + constexpr T MaskValue() const { return mValue - 1; } + + // PowerOfTwoMask corresponding to this power of 2, useful for modulo. + constexpr PowerOfTwoMask<T> Mask() const { + using Trusted = typename PowerOfTwoMask<T>::Trusted; + return PowerOfTwoMask<T>(Trusted{MaskValue()}); + } + + // `x % aPowerOfTwo` works optimally. + // Useful for templated code doing modulo with a template argument type. + // Use PowerOfTwoMask class instead if masking is the primary use case. + template <typename U> + friend constexpr U operator%(U aNumerator, PowerOfTwo aDenominator) { + return aNumerator & aDenominator.MaskValue(); + } + + constexpr bool operator==(const PowerOfTwo& aRhs) const { + return mValue == aRhs.mValue; + } + constexpr bool operator!=(const PowerOfTwo& aRhs) const { + return mValue != aRhs.mValue; + } + constexpr bool operator<(const PowerOfTwo& aRhs) const { + return mValue < aRhs.mValue; + } + constexpr bool operator<=(const PowerOfTwo& aRhs) const { + return mValue <= aRhs.mValue; + } + constexpr bool operator>(const PowerOfTwo& aRhs) const { + return mValue > aRhs.mValue; + } + constexpr bool operator>=(const PowerOfTwo& aRhs) const { + return mValue >= aRhs.mValue; + } + + private: + // Trust `MakePowerOfTwo()` to call the private Trusted constructor below. + template <typename U, U Value> + friend constexpr PowerOfTwo<U> MakePowerOfTwo(); + + struct Trusted { + T mValue; + }; + // Construct a PowerOfTwo with the given trusted value. + // This saves having to compute the nearest 2^N. + // Note: Not offering PowerOfTwoMark-to-PowerOfTwo conversion, because that + // could result in an unexpected 0 result for the largest possible mask. + explicit constexpr PowerOfTwo(const Trusted& aP2) : mValue(aP2.mValue) {} + + // The smallest power of 2 is 2^0 == 1. + T mValue = 1; +}; + +// Make a PowerOfTwo constant, statically-checked. +template <typename T, T Value> +constexpr PowerOfTwo<T> MakePowerOfTwo() { + static_assert(IsPowerOfTwo(Value), + "MakePowerOfTwo<T, Value>: Value must be 2^N"); + using Trusted = typename PowerOfTwo<T>::Trusted; + return PowerOfTwo<T>(Trusted{Value}); +} + +// Shortcuts for the most common types and functions. + +using PowerOfTwoMask32 = PowerOfTwoMask<uint32_t>; +using PowerOfTwo32 = PowerOfTwo<uint32_t>; +using PowerOfTwoMask64 = PowerOfTwoMask<uint64_t>; +using PowerOfTwo64 = PowerOfTwo<uint64_t>; + +template <uint32_t Mask> +constexpr PowerOfTwoMask32 MakePowerOfTwoMask32() { + return MakePowerOfTwoMask<uint32_t, Mask>(); +} + +template <uint32_t Value> +constexpr PowerOfTwo32 MakePowerOfTwo32() { + return MakePowerOfTwo<uint32_t, Value>(); +} + +template <uint64_t Mask> +constexpr PowerOfTwoMask64 MakePowerOfTwoMask64() { + return MakePowerOfTwoMask<uint64_t, Mask>(); +} + +template <uint64_t Value> +constexpr PowerOfTwo64 MakePowerOfTwo64() { + return MakePowerOfTwo<uint64_t, Value>(); +} + +} // namespace mozilla + +#endif // PowerOfTwo_h diff --git a/mozglue/baseprofiler/public/ProfileBufferChunk.h b/mozglue/baseprofiler/public/ProfileBufferChunk.h new file mode 100644 index 0000000000..9ba2483372 --- /dev/null +++ b/mozglue/baseprofiler/public/ProfileBufferChunk.h @@ -0,0 +1,547 @@ +/* -*- Mode: C++; tab-width: 2; 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/. */ + +#ifndef ProfileBufferChunk_h +#define ProfileBufferChunk_h + +#include "mozilla/MemoryReporting.h" +#include "mozilla/ProfileBufferIndex.h" +#include "mozilla/Span.h" +#include "mozilla/TimeStamp.h" +#include "mozilla/UniquePtr.h" + +#if defined(MOZ_MEMORY) +# include "mozmemory.h" +#endif + +#include <algorithm> +#include <limits> +#include <type_traits> + +#ifdef DEBUG +# include <cstdio> +#endif + +namespace mozilla { + +// Represents a single chunk of memory, with a link to the next chunk (or null). +// +// A chunk is made of an internal header (which contains a public part) followed +// by user-accessible bytes. +// +// +---------------+---------+----------------------------------------------+ +// | public Header | private | memory containing user blocks | +// +---------------+---------+----------------------------------------------+ +// <---------------BufferBytes()------------------> +// <------------------------------ChunkBytes()------------------------------> +// +// The chunk can reserve "blocks", but doesn't know the internal contents of +// each block, it only knows where the first one starts, and where the last one +// ends (which is where the next one will begin, if not already out of range). +// It is up to the user to add structure to each block so that they can be +// distinguished when later read. +// +// +---------------+---------+----------------------------------------------+ +// | public Header | private | [1st block]...[last full block] | +// +---------------+---------+----------------------------------------------+ +// ChunkHeader().mOffsetFirstBlock ^ ^ +// ChunkHeader().mOffsetPastLastBlock --' +// +// It is possible to attempt to reserve more than the remaining space, in which +// case only what is available is returned. The caller is responsible for using +// another chunk, reserving a block "tail" in it, and using both parts to +// constitute a full block. (This initial tail may be empty in some chunks.) +// +// +---------------+---------+----------------------------------------------+ +// | public Header | private | tail][1st block]...[last full block][head... | +// +---------------+---------+----------------------------------------------+ +// ChunkHeader().mOffsetFirstBlock ^ ^ +// ChunkHeader().mOffsetPastLastBlock --' +// +// Each Chunk has an internal state (checked in DEBUG builds) that directs how +// to use it during creation, initialization, use, end of life, recycling, and +// destruction. See `State` below for details. +// In particular: +// - `ReserveInitialBlockAsTail()` must be called before the first `Reserve()` +// after construction or recycling, even with a size of 0 (no actual tail), +// - `MarkDone()` and `MarkRecycled()` must be called as appropriate. +class ProfileBufferChunk { + public: + using Byte = uint8_t; + using Length = uint32_t; + + using SpanOfBytes = Span<Byte>; + + // Hint about the size of the metadata (public and private headers). + // `Create()` below takes the minimum *buffer* size, so the minimum total + // Chunk size is at least `SizeofChunkMetadata() + aMinBufferBytes`. + [[nodiscard]] static constexpr Length SizeofChunkMetadata() { + return static_cast<Length>(sizeof(InternalHeader)); + } + + // Allocate space for a chunk with a given minimum size, and construct it. + // The actual size may be higher, to match the actual space taken in the + // memory pool. + [[nodiscard]] static UniquePtr<ProfileBufferChunk> Create( + Length aMinBufferBytes) { + // We need at least one byte, to cover the always-present `mBuffer` byte. + aMinBufferBytes = std::max(aMinBufferBytes, Length(1)); + // Trivial struct with the same alignment as `ProfileBufferChunk`, and size + // equal to that alignment, because typically the sizeof of an object is + // a multiple of its alignment. + struct alignas(alignof(InternalHeader)) ChunkStruct { + Byte c[alignof(InternalHeader)]; + }; + static_assert(std::is_trivial_v<ChunkStruct>, + "ChunkStruct must be trivial to avoid any construction"); + // Allocate an array of that struct, enough to contain the expected + // `ProfileBufferChunk` (with its header+buffer). + size_t count = (sizeof(InternalHeader) + aMinBufferBytes + + (alignof(InternalHeader) - 1)) / + alignof(InternalHeader); +#if defined(MOZ_MEMORY) + // Potentially expand the array to use more of the effective allocation. + count = (malloc_good_size(count * sizeof(ChunkStruct)) + + (sizeof(ChunkStruct) - 1)) / + sizeof(ChunkStruct); +#endif + auto chunkStorage = MakeUnique<ChunkStruct[]>(count); + MOZ_ASSERT(reinterpret_cast<uintptr_t>(chunkStorage.get()) % + alignof(InternalHeader) == + 0); + // After the allocation, compute the actual chunk size (including header). + const size_t chunkBytes = count * sizeof(ChunkStruct); + MOZ_ASSERT(chunkBytes >= sizeof(ProfileBufferChunk), + "Not enough space to construct a ProfileBufferChunk"); + MOZ_ASSERT(chunkBytes <= + static_cast<size_t>(std::numeric_limits<Length>::max())); + // Compute the size of the user-accessible buffer inside the chunk. + const Length bufferBytes = + static_cast<Length>(chunkBytes - sizeof(InternalHeader)); + MOZ_ASSERT(bufferBytes >= aMinBufferBytes, + "Not enough space for minimum buffer size"); + // Construct the header at the beginning of the allocated array, with the + // known buffer size. + new (chunkStorage.get()) ProfileBufferChunk(bufferBytes); + // We now have a proper `ProfileBufferChunk` object, create the appropriate + // UniquePtr for it. + UniquePtr<ProfileBufferChunk> chunk{ + reinterpret_cast<ProfileBufferChunk*>(chunkStorage.release())}; + MOZ_ASSERT( + size_t(reinterpret_cast<const char*>( + &chunk.get()->BufferSpan()[bufferBytes - 1]) - + reinterpret_cast<const char*>(chunk.get())) == chunkBytes - 1, + "Buffer span spills out of chunk allocation"); + return chunk; + } + +#ifdef DEBUG + ~ProfileBufferChunk() { + MOZ_ASSERT(mInternalHeader.mState != InternalHeader::State::InUse); + MOZ_ASSERT(mInternalHeader.mState != InternalHeader::State::Full); + MOZ_ASSERT(mInternalHeader.mState == InternalHeader::State::Created || + mInternalHeader.mState == InternalHeader::State::Done || + mInternalHeader.mState == InternalHeader::State::Recycled); + } +#endif + + // Must be called with the first block tail (may be empty), which will be + // skipped if the reader starts with this ProfileBufferChunk. + [[nodiscard]] SpanOfBytes ReserveInitialBlockAsTail(Length aTailSize) { +#ifdef DEBUG + MOZ_ASSERT(mInternalHeader.mState != InternalHeader::State::InUse); + MOZ_ASSERT(mInternalHeader.mState != InternalHeader::State::Full); + MOZ_ASSERT(mInternalHeader.mState != InternalHeader::State::Done); + MOZ_ASSERT(mInternalHeader.mState == InternalHeader::State::Created || + mInternalHeader.mState == InternalHeader::State::Recycled); + mInternalHeader.mState = InternalHeader::State::InUse; +#endif + mInternalHeader.mHeader.mOffsetFirstBlock = aTailSize; + mInternalHeader.mHeader.mOffsetPastLastBlock = aTailSize; + mInternalHeader.mHeader.mStartTimeStamp = TimeStamp::Now(); + return SpanOfBytes(&mBuffer, aTailSize); + } + + struct ReserveReturn { + SpanOfBytes mSpan; + ProfileBufferBlockIndex mBlockRangeIndex; + }; + + // Reserve a block of up to `aBlockSize` bytes, and return a Span to it, and + // its starting index. The actual size may be smaller, if the block cannot fit + // in the remaining space. + [[nodiscard]] ReserveReturn ReserveBlock(Length aBlockSize) { + MOZ_ASSERT(mInternalHeader.mState != InternalHeader::State::Created); + MOZ_ASSERT(mInternalHeader.mState != InternalHeader::State::Full); + MOZ_ASSERT(mInternalHeader.mState != InternalHeader::State::Done); + MOZ_ASSERT(mInternalHeader.mState != InternalHeader::State::Recycled); + MOZ_ASSERT(mInternalHeader.mState == InternalHeader::State::InUse); + MOZ_ASSERT(RangeStart() != 0, + "Expected valid range start before first Reserve()"); + const Length blockOffset = mInternalHeader.mHeader.mOffsetPastLastBlock; + Length reservedSize = aBlockSize; + if (MOZ_UNLIKELY(aBlockSize >= RemainingBytes())) { + reservedSize = RemainingBytes(); +#ifdef DEBUG + mInternalHeader.mState = InternalHeader::State::Full; +#endif + } + mInternalHeader.mHeader.mOffsetPastLastBlock += reservedSize; + mInternalHeader.mHeader.mBlockCount += 1; + return {SpanOfBytes(&mBuffer + blockOffset, reservedSize), + ProfileBufferBlockIndex::CreateFromProfileBufferIndex( + mInternalHeader.mHeader.mRangeStart + blockOffset)}; + } + + // When a chunk will not be used to store more blocks (because it is full, or + // because the profiler will not add more data), it should be marked "done". + // Access to its content is still allowed. + void MarkDone() { +#ifdef DEBUG + MOZ_ASSERT(mInternalHeader.mState != InternalHeader::State::Created); + MOZ_ASSERT(mInternalHeader.mState != InternalHeader::State::Done); + MOZ_ASSERT(mInternalHeader.mState != InternalHeader::State::Recycled); + MOZ_ASSERT(mInternalHeader.mState == InternalHeader::State::InUse || + mInternalHeader.mState == InternalHeader::State::Full); + mInternalHeader.mState = InternalHeader::State::Done; +#endif + mInternalHeader.mHeader.mDoneTimeStamp = TimeStamp::Now(); + } + + // A "Done" chunk may be recycled, to avoid allocating a new one. + void MarkRecycled() { +#ifdef DEBUG + // We also allow Created and already-Recycled chunks to be recycled, this + // way it's easier to recycle chunks when their state is not easily + // trackable. + MOZ_ASSERT(mInternalHeader.mState != InternalHeader::State::InUse); + MOZ_ASSERT(mInternalHeader.mState != InternalHeader::State::Full); + MOZ_ASSERT(mInternalHeader.mState == InternalHeader::State::Created || + mInternalHeader.mState == InternalHeader::State::Done || + mInternalHeader.mState == InternalHeader::State::Recycled); + mInternalHeader.mState = InternalHeader::State::Recycled; +#endif + // Reset all header fields, in case this recycled chunk gets read. + mInternalHeader.mHeader.Reset(); + } + + // Public header, meant to uniquely identify a chunk, it may be shared with + // other processes to coordinate global memory handling. + struct Header { + explicit Header(Length aBufferBytes) : mBufferBytes(aBufferBytes) {} + + // Reset all members to their as-new values (apart from the buffer size, + // which cannot change), ready for re-use. + void Reset() { + mOffsetFirstBlock = 0; + mOffsetPastLastBlock = 0; + mStartTimeStamp = TimeStamp{}; + mDoneTimeStamp = TimeStamp{}; + mBlockCount = 0; + mRangeStart = 0; + mProcessId = 0; + } + + // Note: Part of the ordering of members below is to avoid unnecessary + // padding. + + // Members managed by the ProfileBufferChunk. + + // Offset of the first block (past the initial tail block, which may be 0). + Length mOffsetFirstBlock = 0; + // Offset past the last byte of the last reserved block + // It may be past mBufferBytes when last block continues in the next + // ProfileBufferChunk. It may be before mBufferBytes if ProfileBufferChunk + // is marked "Done" before the end is reached. + Length mOffsetPastLastBlock = 0; + // Timestamp when the buffer becomes in-use, ready to record data. + TimeStamp mStartTimeStamp; + // Timestamp when the buffer is "Done" (which happens when the last block is + // written). This will be used to find and discard the oldest + // ProfileBufferChunk. + TimeStamp mDoneTimeStamp; + // Number of bytes in the buffer, set once at construction time. + const Length mBufferBytes; + // Number of reserved blocks (including final one even if partial, but + // excluding initial tail). + Length mBlockCount = 0; + + // Meta-data set by the user. + + // Index of the first byte of this ProfileBufferChunk, relative to all + // Chunks for this process. Index 0 is reserved as nullptr-like index, + // mRangeStart should be set to a non-0 value before the first `Reserve()`. + ProfileBufferIndex mRangeStart = 0; + // Process writing to this ProfileBufferChunk. + int mProcessId = 0; + + // A bit of spare space (necessary here because of the alignment due to + // other members), may be later repurposed for extra data. + const int mPADDING = 0; + }; + + [[nodiscard]] const Header& ChunkHeader() const { + return mInternalHeader.mHeader; + } + + [[nodiscard]] Length BufferBytes() const { + return ChunkHeader().mBufferBytes; + } + + // Total size of the chunk (buffer + header). + [[nodiscard]] Length ChunkBytes() const { + return static_cast<Length>(sizeof(InternalHeader)) + BufferBytes(); + } + + // Size of external resources, in this case all the following chunks. + [[nodiscard]] size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const { + const ProfileBufferChunk* const next = GetNext(); + return next ? next->SizeOfIncludingThis(aMallocSizeOf) : 0; + } + + // Size of this chunk and all following ones. + [[nodiscard]] size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const { + // Just in case `aMallocSizeOf` falls back on just `sizeof`, make sure we + // account for at least the actual Chunk requested allocation size. + return std::max<size_t>(aMallocSizeOf(this), ChunkBytes()) + + SizeOfExcludingThis(aMallocSizeOf); + } + + [[nodiscard]] Length RemainingBytes() const { + return BufferBytes() - OffsetPastLastBlock(); + } + + [[nodiscard]] Length OffsetFirstBlock() const { + return ChunkHeader().mOffsetFirstBlock; + } + + [[nodiscard]] Length OffsetPastLastBlock() const { + return ChunkHeader().mOffsetPastLastBlock; + } + + [[nodiscard]] Length BlockCount() const { return ChunkHeader().mBlockCount; } + + [[nodiscard]] int ProcessId() const { return ChunkHeader().mProcessId; } + + void SetProcessId(int aProcessId) { + mInternalHeader.mHeader.mProcessId = aProcessId; + } + + // Global range index at the start of this Chunk. + [[nodiscard]] ProfileBufferIndex RangeStart() const { + return ChunkHeader().mRangeStart; + } + + void SetRangeStart(ProfileBufferIndex aRangeStart) { + mInternalHeader.mHeader.mRangeStart = aRangeStart; + } + + // Get a read-only Span to the buffer. It is up to the caller to decypher the + // contents, based on known offsets and the internal block structure. + [[nodiscard]] Span<const Byte> BufferSpan() const { + return Span<const Byte>(&mBuffer, BufferBytes()); + } + + [[nodiscard]] Byte ByteAt(Length aOffset) const { + MOZ_ASSERT(aOffset < OffsetPastLastBlock()); + return *(&mBuffer + aOffset); + } + + [[nodiscard]] ProfileBufferChunk* GetNext() { + return mInternalHeader.mNext.get(); + } + [[nodiscard]] const ProfileBufferChunk* GetNext() const { + return mInternalHeader.mNext.get(); + } + + [[nodiscard]] UniquePtr<ProfileBufferChunk> ReleaseNext() { + return std::move(mInternalHeader.mNext); + } + + void InsertNext(UniquePtr<ProfileBufferChunk>&& aChunk) { + if (!aChunk) { + return; + } + aChunk->SetLast(ReleaseNext()); + mInternalHeader.mNext = std::move(aChunk); + } + + // Find the last chunk in this chain (it may be `this`). + [[nodiscard]] ProfileBufferChunk* Last() { + ProfileBufferChunk* chunk = this; + for (;;) { + ProfileBufferChunk* next = chunk->GetNext(); + if (!next) { + return chunk; + } + chunk = next; + } + } + [[nodiscard]] const ProfileBufferChunk* Last() const { + const ProfileBufferChunk* chunk = this; + for (;;) { + const ProfileBufferChunk* next = chunk->GetNext(); + if (!next) { + return chunk; + } + chunk = next; + } + } + + void SetLast(UniquePtr<ProfileBufferChunk>&& aChunk) { + if (!aChunk) { + return; + } + Last()->mInternalHeader.mNext = std::move(aChunk); + } + + // Join two possibly-null chunk lists. + [[nodiscard]] static UniquePtr<ProfileBufferChunk> Join( + UniquePtr<ProfileBufferChunk>&& aFirst, + UniquePtr<ProfileBufferChunk>&& aLast) { + if (aFirst) { + aFirst->SetLast(std::move(aLast)); + return std::move(aFirst); + } + return std::move(aLast); + } + +#ifdef DEBUG + void Dump(std::FILE* aFile = stdout) const { + fprintf(aFile, + "Chunk[%p] chunkSize=%u bufferSize=%u state=%s rangeStart=%u " + "firstBlockOffset=%u offsetPastLastBlock=%u blockCount=%u", + this, unsigned(ChunkBytes()), unsigned(BufferBytes()), + mInternalHeader.StateString(), unsigned(RangeStart()), + unsigned(OffsetFirstBlock()), unsigned(OffsetPastLastBlock()), + unsigned(BlockCount())); + const auto len = OffsetPastLastBlock(); + constexpr unsigned columns = 16; + unsigned char ascii[columns + 1]; + ascii[columns] = '\0'; + for (Length i = 0; i < len; ++i) { + if (i % columns == 0) { + fprintf(aFile, "\n %4u=0x%03x:", unsigned(i), unsigned(i)); + for (unsigned a = 0; a < columns; ++a) { + ascii[a] = ' '; + } + } + unsigned char sep = ' '; + if (i == OffsetFirstBlock()) { + if (i == OffsetPastLastBlock()) { + sep = '#'; + } else { + sep = '['; + } + } else if (i == OffsetPastLastBlock()) { + sep = ']'; + } + unsigned char c = *(&mBuffer + i); + fprintf(aFile, "%c%02x", sep, c); + + if (i == len - 1) { + if (i + 1 == OffsetPastLastBlock()) { + // Special case when last block ends right at the end. + fprintf(aFile, "]"); + } else { + fprintf(aFile, " "); + } + } else if (i % columns == columns - 1) { + fprintf(aFile, " "); + } + + ascii[i % columns] = (c >= ' ' && c <= '~') ? c : '.'; + + if (i % columns == columns - 1) { + fprintf(aFile, " %s", ascii); + } + } + + if (len % columns < columns - 1) { + for (Length i = len % columns; i < columns; ++i) { + fprintf(aFile, " "); + } + fprintf(aFile, " %s", ascii); + } + + fprintf(aFile, "\n"); + } +#endif // DEBUG + + private: + // ProfileBufferChunk constructor. Use static `Create()` to allocate and + // construct a ProfileBufferChunk. + explicit ProfileBufferChunk(Length aBufferBytes) + : mInternalHeader(aBufferBytes) {} + + // This internal header starts with the public `Header`, and adds some data + // only necessary for local handling. + // This encapsulation is also necessary to perform placement-new in + // `Create()`. + struct InternalHeader { + explicit InternalHeader(Length aBufferBytes) : mHeader(aBufferBytes) {} + + Header mHeader; + UniquePtr<ProfileBufferChunk> mNext; + +#ifdef DEBUG + enum class State { + Created, // Self-set. Just constructed, waiting for initial block tail. + InUse, // Ready to accept blocks. + Full, // Self-set. Blocks reach the end (or further). + Done, // Blocks won't be added anymore. + Recycled // Still full of data, but expecting an initial block tail. + }; + + State mState = State::Created; + // Transition table: (X=unexpected) + // Method \ State Created InUse Full Done Recycled + // ReserveInitialBlockAsTail InUse X X X InUse + // Reserve X InUse/Full X X X + // MarkDone X Done Done X X + // MarkRecycled X X X Recycled X + // destructor ok X X ok ok + + const char* StateString() const { + switch (mState) { + case State::Created: + return "Created"; + case State::InUse: + return "InUse"; + case State::Full: + return "Full"; + case State::Done: + return "Done"; + case State::Recycled: + return "Recycled"; + default: + return "?"; + } + } +#else // DEBUG + const char* StateString() const { return "(non-DEBUG)"; } +#endif + }; + + InternalHeader mInternalHeader; + + // KEEP THIS LAST! + // First byte of the buffer. Note that ProfileBufferChunk::Create allocates a + // bigger block, such that `mBuffer` is the first of `mBufferBytes` available + // bytes. + // The initialization is not strictly needed, because bytes should only be + // read after they have been written and `mOffsetPastLastBlock` has been + // updated. However: + // - Reviewbot complains that it's not initialized. + // - It's cheap to initialize one byte. + // - In the worst case (reading does happen), zero is not a valid entry size + // and should get caught in entry readers. + Byte mBuffer = '\0'; +}; + +} // namespace mozilla + +#endif // ProfileBufferChunk_h diff --git a/mozglue/baseprofiler/public/ProfileBufferChunkManager.h b/mozglue/baseprofiler/public/ProfileBufferChunkManager.h new file mode 100644 index 0000000000..e7f12bf21f --- /dev/null +++ b/mozglue/baseprofiler/public/ProfileBufferChunkManager.h @@ -0,0 +1,134 @@ +/* -*- Mode: C++; tab-width: 2; 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/. */ + +#ifndef ProfileBufferChunkManager_h +#define ProfileBufferChunkManager_h + +#include "mozilla/ProfileBufferChunk.h" +#include "mozilla/ScopeExit.h" + +#include <functional> + +namespace mozilla { + +// Manages the ProfileBufferChunks for this process. +// The main user of this class is the buffer that needs chunks to store its +// data. +// The main ProfileBufferChunks responsibilities are: +// - It can create new chunks, they are called "unreleased". +// - Later these chunks are returned here, and become "released". +// - The manager is free to destroy or recycle the oldest released chunks +// (usually to reclaim memory), and will inform the user through a provided +// callback. +// - The user may access still-alive released chunks. +class ProfileBufferChunkManager { + public: + virtual ~ProfileBufferChunkManager() +#ifdef DEBUG + { + MOZ_ASSERT(!mUser, "Still registered when being destroyed"); + } +#else + = default; +#endif + + // Expected maximum size needed to store one stack sample. + // Most ChunkManager sub-classes will require chunk sizes, this can serve as + // a minimum recommendation to hold most backtraces. + constexpr static ProfileBufferChunk::Length scExpectedMaximumStackSize = + 128 * 1024; + + // Estimated maximum buffer size. + [[nodiscard]] virtual size_t MaxTotalSize() const = 0; + + // Create or recycle a chunk right now. May return null in case of allocation + // failure. + // Note that the chunk-destroyed callback may be invoked during this call; + // user should be careful with reentrancy issues. + [[nodiscard]] virtual UniquePtr<ProfileBufferChunk> GetChunk() = 0; + + // `aChunkReceiver` may be called with a new or recycled chunk, or nullptr. + // (See `FulfillChunkRequests()` regarding when the callback may happen.) + virtual void RequestChunk( + std::function<void(UniquePtr<ProfileBufferChunk>)>&& aChunkReceiver) = 0; + + // This method may be invoked at any time on any thread (and not necessarily + // by the main user of this class), to do the work necessary to respond to a + // previous `RequestChunk()`. + // It is optional: If it is never called, or called too late, the user is + // responsible for directly calling `GetChunk()` when a chunk is really + // needed (or it should at least fail gracefully). + // The idea is to fulfill chunk request on a separate thread, and most + // importantly outside of profiler calls, to avoid doing expensive memory + // allocations during these calls. + virtual void FulfillChunkRequests() = 0; + + // One chunk is released by the user, the ProfileBufferChunkManager should + // keep it as long as possible (depending on local or global memory/time + // limits). Note that the chunk-destroyed callback may be invoked during this + // call; user should be careful with reentrancy issues. + virtual void ReleaseChunk(UniquePtr<ProfileBufferChunk> aChunk) = 0; + + // `aChunkDestroyedCallback` will be called whenever the contents of a + // previously-released chunk is about to be destroyed or recycled. + // Note that it may be called during other functions above, or at other times + // from the same or other threads; user should be careful with reentrancy + // issues. + virtual void SetChunkDestroyedCallback( + std::function<void(const ProfileBufferChunk&)>&& + aChunkDestroyedCallback) = 0; + + // Give away all released chunks that have not yet been destroyed. + [[nodiscard]] virtual UniquePtr<ProfileBufferChunk> + GetExtantReleasedChunks() = 0; + + // Let a callback see all released chunks that have not yet been destroyed, if + // any. Return whatever the callback returns. + template <typename Callback> + [[nodiscard]] auto PeekExtantReleasedChunks(Callback&& aCallback) { + const ProfileBufferChunk* chunks = PeekExtantReleasedChunksAndLock(); + auto unlock = + MakeScopeExit([&]() { UnlockAfterPeekExtantReleasedChunks(); }); + return std::forward<Callback>(aCallback)(chunks); + } + + // Chunks that were still unreleased will never be released. + virtual void ForgetUnreleasedChunks() = 0; + + [[nodiscard]] virtual size_t SizeOfExcludingThis( + MallocSizeOf aMallocSizeOf) const = 0; + [[nodiscard]] virtual size_t SizeOfIncludingThis( + MallocSizeOf aMallocSizeOf) const = 0; + + protected: + // Derived classes to implement `PeekExtantReleasedChunks` through these: + virtual const ProfileBufferChunk* PeekExtantReleasedChunksAndLock() = 0; + virtual void UnlockAfterPeekExtantReleasedChunks() = 0; + +#ifdef DEBUG + public: + // DEBUG checks ensuring that this manager and its users avoid UAFs. + // Derived classes should assert that mUser is not null in their functions. + + void RegisteredWith(const void* aUser) { + MOZ_ASSERT(!mUser); + MOZ_ASSERT(aUser); + mUser = aUser; + } + + void DeregisteredFrom(const void* aUser) { + MOZ_ASSERT(mUser == aUser); + mUser = nullptr; + } + + protected: + const void* mUser = nullptr; +#endif // DEBUG +}; + +} // namespace mozilla + +#endif // ProfileBufferChunkManager_h diff --git a/mozglue/baseprofiler/public/ProfileBufferChunkManagerSingle.h b/mozglue/baseprofiler/public/ProfileBufferChunkManagerSingle.h new file mode 100644 index 0000000000..c91b38cbdb --- /dev/null +++ b/mozglue/baseprofiler/public/ProfileBufferChunkManagerSingle.h @@ -0,0 +1,172 @@ +/* -*- Mode: C++; tab-width: 2; 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/. */ + +#ifndef ProfileBufferChunkManagerSingle_h +#define ProfileBufferChunkManagerSingle_h + +#include "mozilla/ProfileBufferChunkManager.h" + +#ifdef DEBUG +# include "mozilla/Atomics.h" +#endif // DEBUG + +namespace mozilla { + +// Manages only one Chunk. +// The first call to `Get`/`RequestChunk()` will retrieve the one chunk, and all +// subsequent calls will return nullptr. That chunk may still be released, but +// it will never be destroyed or recycled. +// Unlike others, this manager may be `Reset()`, to allow another round of +// small-data gathering. +// The main use is with short-lived ProfileChunkedBuffers that collect little +// data that can fit in one chunk, e.g., capturing one stack. +// It is not thread-safe. +class ProfileBufferChunkManagerSingle final : public ProfileBufferChunkManager { + public: + using Length = ProfileBufferChunk::Length; + + // Use a preallocated chunk. (Accepting null to gracefully handle OOM.) + explicit ProfileBufferChunkManagerSingle(UniquePtr<ProfileBufferChunk> aChunk) + : mInitialChunk(std::move(aChunk)), + mBufferBytes(mInitialChunk ? mInitialChunk->BufferBytes() : 0) { + MOZ_ASSERT(!mInitialChunk || !mInitialChunk->GetNext(), + "Expected at most one chunk"); + } + + // ChunkMinBufferBytes: Minimum number of user-available bytes in the Chunk. + // Note that Chunks use a bit more memory for their header. + explicit ProfileBufferChunkManagerSingle(Length aChunkMinBufferBytes) + : mInitialChunk(ProfileBufferChunk::Create(aChunkMinBufferBytes)), + mBufferBytes(mInitialChunk ? mInitialChunk->BufferBytes() : 0) {} + +#ifdef DEBUG + ~ProfileBufferChunkManagerSingle() { MOZ_ASSERT(mVirtuallyLocked == false); } +#endif // DEBUG + + // Reset this manager, using the provided chunk (probably coming from the + // ProfileChunkedBuffer that just used it); if null, fallback on current or + // released chunk. + void Reset(UniquePtr<ProfileBufferChunk> aPossibleChunk) { + if (aPossibleChunk) { + mInitialChunk = std::move(aPossibleChunk); + mReleasedChunk = nullptr; + } else if (!mInitialChunk) { + MOZ_ASSERT(!!mReleasedChunk, "Can't reset properly!"); + mInitialChunk = std::move(mReleasedChunk); + } + + if (mInitialChunk) { + mInitialChunk->MarkRecycled(); + mBufferBytes = mInitialChunk->BufferBytes(); + } else { + mBufferBytes = 0; + } + } + + [[nodiscard]] size_t MaxTotalSize() const final { return mBufferBytes; } + + // One of `GetChunk` and `RequestChunk` will only work the very first time (if + // there's even a chunk). + [[nodiscard]] UniquePtr<ProfileBufferChunk> GetChunk() final { + MOZ_ASSERT(mUser, "Not registered yet"); + return std::move(mInitialChunk); + } + + void RequestChunk(std::function<void(UniquePtr<ProfileBufferChunk>)>&& + aChunkReceiver) final { + MOZ_ASSERT(mUser, "Not registered yet"); + // Simple retrieval. + std::move(aChunkReceiver)(GetChunk()); + } + + void FulfillChunkRequests() final { + // Nothing to do here. + } + + void ReleaseChunk(UniquePtr<ProfileBufferChunk> aChunk) final { + MOZ_ASSERT(mUser, "Not registered yet"); + if (!aChunk) { + return; + } + MOZ_ASSERT(!mReleasedChunk, "Unexpected 2nd released chunk"); + MOZ_ASSERT(!aChunk->GetNext(), "Only expected one released chunk"); + mReleasedChunk = std::move(aChunk); + } + + void SetChunkDestroyedCallback( + std::function<void(const ProfileBufferChunk&)>&& aChunkDestroyedCallback) + final { + MOZ_ASSERT(mUser, "Not registered yet"); + // The chunk-destroyed callback will never actually be called, but we keep + // the callback here in case the caller expects it to live as long as this + // manager. + mChunkDestroyedCallback = std::move(aChunkDestroyedCallback); + } + + [[nodiscard]] UniquePtr<ProfileBufferChunk> GetExtantReleasedChunks() final { + MOZ_ASSERT(mUser, "Not registered yet"); + return std::move(mReleasedChunk); + } + + void ForgetUnreleasedChunks() final { + MOZ_ASSERT(mUser, "Not registered yet"); + } + + [[nodiscard]] size_t SizeOfExcludingThis( + MallocSizeOf aMallocSizeOf) const final { + MOZ_ASSERT(mUser, "Not registered yet"); + size_t size = 0; + if (mInitialChunk) { + size += mInitialChunk->SizeOfIncludingThis(aMallocSizeOf); + } + if (mReleasedChunk) { + size += mReleasedChunk->SizeOfIncludingThis(aMallocSizeOf); + } + // Note: Missing size of std::function external resources (if any). + return size; + } + + [[nodiscard]] size_t SizeOfIncludingThis( + MallocSizeOf aMallocSizeOf) const final { + MOZ_ASSERT(mUser, "Not registered yet"); + return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf); + } + + protected: + // This manager is not thread-safe, so there's not actual locking needed. + const ProfileBufferChunk* PeekExtantReleasedChunksAndLock() final { + MOZ_ASSERT(mVirtuallyLocked.compareExchange(false, true)); + MOZ_ASSERT(mUser, "Not registered yet"); + return mReleasedChunk.get(); + } + void UnlockAfterPeekExtantReleasedChunks() final { + MOZ_ASSERT(mVirtuallyLocked.compareExchange(true, false)); + } + + private: + // Initial chunk created with this manager, given away at first Get/Request. + UniquePtr<ProfileBufferChunk> mInitialChunk; + + // Storage for the released chunk (which should probably not happen, as it + // means the chunk is full). + UniquePtr<ProfileBufferChunk> mReleasedChunk; + + // Size of the one chunk we're managing. Stored here, because the chunk may + // be moved out and inaccessible from here. + Length mBufferBytes; + + // The chunk-destroyed callback will never actually be called, but we keep it + // here in case the caller expects it to live as long as this manager. + std::function<void(const ProfileBufferChunk&)> mChunkDestroyedCallback; + +#ifdef DEBUG + mutable Atomic<bool> mVirtuallyLocked{false}; +#endif // DEBUG +}; + +} // namespace mozilla + +#endif // ProfileBufferChunkManagerSingle_h diff --git a/mozglue/baseprofiler/public/ProfileBufferChunkManagerWithLocalLimit.h b/mozglue/baseprofiler/public/ProfileBufferChunkManagerWithLocalLimit.h new file mode 100644 index 0000000000..034279809d --- /dev/null +++ b/mozglue/baseprofiler/public/ProfileBufferChunkManagerWithLocalLimit.h @@ -0,0 +1,444 @@ +/* -*- Mode: C++; tab-width: 2; 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/. */ + +#ifndef ProfileBufferChunkManagerWithLocalLimit_h +#define ProfileBufferChunkManagerWithLocalLimit_h + +#include "BaseProfiler.h" +#include "mozilla/BaseProfilerDetail.h" +#include "mozilla/ProfileBufferChunkManager.h" +#include "mozilla/ProfileBufferControlledChunkManager.h" +#include "mozilla/mozalloc.h" + +#include <utility> + +namespace mozilla { + +// Manages the Chunks for this process in a thread-safe manner, with a maximum +// size per process. +// +// "Unreleased" chunks are not owned here, only "released" chunks can be +// destroyed or recycled when reaching the memory limit, so it is theoretically +// possible to break that limit, if: +// - The user of this class doesn't release their chunks, AND/OR +// - The limit is too small (e.g., smaller than 2 or 3 chunks, which should be +// the usual number of unreleased chunks in flight). +// In this case, it just means that we will use more memory than allowed, +// potentially risking OOMs. Hopefully this shouldn't happen in real code, +// assuming that the user is doing the right thing and releasing chunks ASAP, +// and that the memory limit is reasonably large. +class ProfileBufferChunkManagerWithLocalLimit final + : public ProfileBufferChunkManager, + public ProfileBufferControlledChunkManager { + public: + using Length = ProfileBufferChunk::Length; + + // MaxTotalBytes: Maximum number of bytes allocated in all local Chunks. + // ChunkMinBufferBytes: Minimum number of user-available bytes in each Chunk. + // Note that Chunks use a bit more memory for their header. + explicit ProfileBufferChunkManagerWithLocalLimit(size_t aMaxTotalBytes, + Length aChunkMinBufferBytes) + : mMaxTotalBytes(aMaxTotalBytes), + mChunkMinBufferBytes(aChunkMinBufferBytes) {} + + ~ProfileBufferChunkManagerWithLocalLimit() { + if (mUpdateCallback) { + // Signal the end of this callback. + std::move(mUpdateCallback)(Update(nullptr)); + } + } + + [[nodiscard]] size_t MaxTotalSize() const final { + // `mMaxTotalBytes` is `const` so there is no need to lock the mutex. + return mMaxTotalBytes; + } + + [[nodiscard]] size_t TotalSize() const { return mTotalBytes; } + + [[nodiscard]] UniquePtr<ProfileBufferChunk> GetChunk() final { + AUTO_PROFILER_STATS(Local_GetChunk); + + ChunkAndUpdate chunkAndUpdate = [&]() { + baseprofiler::detail::BaseProfilerAutoLock lock(mMutex); + return GetChunk(lock); + }(); + + baseprofiler::detail::BaseProfilerAutoLock lock(mUpdateCallbackMutex); + if (mUpdateCallback && !chunkAndUpdate.second.IsNotUpdate()) { + mUpdateCallback(std::move(chunkAndUpdate.second)); + } + + return std::move(chunkAndUpdate.first); + } + + void RequestChunk(std::function<void(UniquePtr<ProfileBufferChunk>)>&& + aChunkReceiver) final { + AUTO_PROFILER_STATS(Local_RequestChunk); + baseprofiler::detail::BaseProfilerAutoLock lock(mMutex); + if (mChunkReceiver) { + // We already have a chunk receiver, meaning a request is pending. + return; + } + // Store the chunk receiver. This indicates that a request is pending, and + // it will be handled in the next `FulfillChunkRequests()` call. + mChunkReceiver = std::move(aChunkReceiver); + } + + void FulfillChunkRequests() final { + AUTO_PROFILER_STATS(Local_FulfillChunkRequests); + std::function<void(UniquePtr<ProfileBufferChunk>)> chunkReceiver; + ChunkAndUpdate chunkAndUpdate = [&]() -> ChunkAndUpdate { + baseprofiler::detail::BaseProfilerAutoLock lock(mMutex); + if (!mChunkReceiver) { + // No receiver means no pending request, we're done. + return {}; + } + // Otherwise there is a request, extract the receiver to call below. + std::swap(chunkReceiver, mChunkReceiver); + MOZ_ASSERT(!mChunkReceiver, "mChunkReceiver should have been emptied"); + // And allocate the requested chunk. This may fail, it's fine, we're + // letting the receiver know about it. + AUTO_PROFILER_STATS(Local_FulfillChunkRequests_GetChunk); + return GetChunk(lock); + }(); + + if (chunkReceiver) { + { + baseprofiler::detail::BaseProfilerAutoLock lock(mUpdateCallbackMutex); + if (mUpdateCallback && !chunkAndUpdate.second.IsNotUpdate()) { + mUpdateCallback(std::move(chunkAndUpdate.second)); + } + } + + // Invoke callback outside of lock, so that it can use other chunk manager + // functions if needed. + // Note that this means there could be a race, where another request + // happens now and even gets fulfilled before this one is! It should be + // rare, and shouldn't be a problem anyway, the user will still get their + // requested chunks, new/recycled chunks look the same so their order + // doesn't matter. + std::move(chunkReceiver)(std::move(chunkAndUpdate.first)); + } + } + + void ReleaseChunk(UniquePtr<ProfileBufferChunk> aChunk) final { + if (!aChunk) { + return; + } + + MOZ_RELEASE_ASSERT(!aChunk->GetNext(), "ReleaseChunk only accepts 1 chunk"); + MOZ_RELEASE_ASSERT(!aChunk->ChunkHeader().mDoneTimeStamp.IsNull(), + "Released chunk should have a 'Done' timestamp"); + + Update update = [&]() { + baseprofiler::detail::BaseProfilerAutoLock lock(mMutex); + MOZ_ASSERT(mUser, "Not registered yet"); + // Keep a pointer to the first newly-released chunk, so we can use it to + // prepare an update (after `aChunk` is moved-from). + const ProfileBufferChunk* const newlyReleasedChunk = aChunk.get(); + // Transfer the chunk size from the unreleased bucket to the released one. + mUnreleasedBufferBytes -= aChunk->BufferBytes(); + mReleasedBufferBytes += aChunk->BufferBytes(); + if (!mReleasedChunks) { + // No other released chunks at the moment, we're starting the list. + MOZ_ASSERT(mReleasedBufferBytes == aChunk->BufferBytes()); + mReleasedChunks = std::move(aChunk); + } else { + // Insert aChunk in mReleasedChunks to keep done-timestamp order. + const TimeStamp& releasedChunkDoneTimeStamp = + aChunk->ChunkHeader().mDoneTimeStamp; + if (releasedChunkDoneTimeStamp < + mReleasedChunks->ChunkHeader().mDoneTimeStamp) { + // aChunk is the oldest -> Insert at the beginning. + aChunk->SetLast(std::move(mReleasedChunks)); + mReleasedChunks = std::move(aChunk); + } else { + // Go through the already-released chunk list, and insert aChunk + // before the first younger released chunk, or at the end. + ProfileBufferChunk* chunk = mReleasedChunks.get(); + for (;;) { + ProfileBufferChunk* const nextChunk = chunk->GetNext(); + if (!nextChunk || releasedChunkDoneTimeStamp < + nextChunk->ChunkHeader().mDoneTimeStamp) { + // Either we're at the last released chunk, or the next released + // chunk is younger -> Insert right after this released chunk. + chunk->InsertNext(std::move(aChunk)); + break; + } + chunk = nextChunk; + } + } + } + + return Update(mUnreleasedBufferBytes, mReleasedBufferBytes, + mReleasedChunks.get(), newlyReleasedChunk); + }(); + + baseprofiler::detail::BaseProfilerAutoLock lock(mUpdateCallbackMutex); + if (mUpdateCallback && !update.IsNotUpdate()) { + mUpdateCallback(std::move(update)); + } + } + + void SetChunkDestroyedCallback( + std::function<void(const ProfileBufferChunk&)>&& aChunkDestroyedCallback) + final { + baseprofiler::detail::BaseProfilerAutoLock lock(mMutex); + MOZ_ASSERT(mUser, "Not registered yet"); + mChunkDestroyedCallback = std::move(aChunkDestroyedCallback); + } + + [[nodiscard]] UniquePtr<ProfileBufferChunk> GetExtantReleasedChunks() final { + UniquePtr<ProfileBufferChunk> chunks; + size_t unreleasedBufferBytes = [&]() { + baseprofiler::detail::BaseProfilerAutoLock lock(mMutex); + MOZ_ASSERT(mUser, "Not registered yet"); + mReleasedBufferBytes = 0; + chunks = std::move(mReleasedChunks); + return mUnreleasedBufferBytes; + }(); + + baseprofiler::detail::BaseProfilerAutoLock lock(mUpdateCallbackMutex); + if (mUpdateCallback) { + mUpdateCallback(Update(unreleasedBufferBytes, 0, nullptr, nullptr)); + } + + return chunks; + } + + void ForgetUnreleasedChunks() final { + Update update = [&]() { + baseprofiler::detail::BaseProfilerAutoLock lock(mMutex); + MOZ_ASSERT(mUser, "Not registered yet"); + mUnreleasedBufferBytes = 0; + return Update(0, mReleasedBufferBytes, mReleasedChunks.get(), nullptr); + }(); + baseprofiler::detail::BaseProfilerAutoLock lock(mUpdateCallbackMutex); + if (mUpdateCallback) { + mUpdateCallback(std::move(update)); + } + } + + [[nodiscard]] size_t SizeOfExcludingThis( + MallocSizeOf aMallocSizeOf) const final { + baseprofiler::detail::BaseProfilerAutoLock lock(mMutex); + return SizeOfExcludingThis(aMallocSizeOf, lock); + } + + [[nodiscard]] size_t SizeOfIncludingThis( + MallocSizeOf aMallocSizeOf) const final { + baseprofiler::detail::BaseProfilerAutoLock lock(mMutex); + MOZ_ASSERT(mUser, "Not registered yet"); + return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf, lock); + } + + void SetUpdateCallback(UpdateCallback&& aUpdateCallback) final { + { + baseprofiler::detail::BaseProfilerAutoLock lock(mUpdateCallbackMutex); + if (mUpdateCallback) { + // Signal the end of the previous callback. + std::move(mUpdateCallback)(Update(nullptr)); + mUpdateCallback = nullptr; + } + } + + if (aUpdateCallback) { + Update initialUpdate = [&]() { + baseprofiler::detail::BaseProfilerAutoLock lock(mMutex); + return Update(mUnreleasedBufferBytes, mReleasedBufferBytes, + mReleasedChunks.get(), nullptr); + }(); + + baseprofiler::detail::BaseProfilerAutoLock lock(mUpdateCallbackMutex); + MOZ_ASSERT(!mUpdateCallback, "Only one update callback allowed"); + mUpdateCallback = std::move(aUpdateCallback); + mUpdateCallback(std::move(initialUpdate)); + } + } + + void DestroyChunksAtOrBefore(TimeStamp aDoneTimeStamp) final { + MOZ_ASSERT(!aDoneTimeStamp.IsNull()); + baseprofiler::detail::BaseProfilerAutoLock lock(mMutex); + for (;;) { + if (!mReleasedChunks) { + // We don't own any released chunks (anymore), we're done. + break; + } + if (mReleasedChunks->ChunkHeader().mDoneTimeStamp > aDoneTimeStamp) { + // The current chunk is strictly after the given timestamp, we're done. + break; + } + // We've found a chunk at or before the timestamp, discard it. + DiscardOldestReleasedChunk(lock); + } + } + + protected: + const ProfileBufferChunk* PeekExtantReleasedChunksAndLock() final + MOZ_CAPABILITY_ACQUIRE(mMutex) { + mMutex.Lock(); + MOZ_ASSERT(mUser, "Not registered yet"); + return mReleasedChunks.get(); + } + void UnlockAfterPeekExtantReleasedChunks() final + MOZ_CAPABILITY_RELEASE(mMutex) { + mMutex.Unlock(); + } + + private: + size_t MaybeRecycleChunkAndGetDeallocatedSize( + UniquePtr<ProfileBufferChunk>&& chunk, + const baseprofiler::detail::BaseProfilerAutoLock& aLock) { + // Try to recycle big-enough chunks. (All chunks should have the same size, + // but it's a cheap test and may allow future adjustments based on actual + // data rate.) + if (chunk->BufferBytes() >= mChunkMinBufferBytes) { + // We keep up to two recycled chunks at any time. + if (!mRecycledChunks) { + mRecycledChunks = std::move(chunk); + return 0; + } else if (!mRecycledChunks->GetNext()) { + mRecycledChunks->InsertNext(std::move(chunk)); + return 0; + } + } + return moz_malloc_usable_size(chunk.get()); + } + + UniquePtr<ProfileBufferChunk> TakeRecycledChunk( + const baseprofiler::detail::BaseProfilerAutoLock& aLock) { + UniquePtr<ProfileBufferChunk> recycled; + if (mRecycledChunks) { + recycled = std::exchange(mRecycledChunks, mRecycledChunks->ReleaseNext()); + recycled->MarkRecycled(); + } + return recycled; + } + + void DiscardOldestReleasedChunk( + const baseprofiler::detail::BaseProfilerAutoLock& aLock) { + MOZ_ASSERT(!!mReleasedChunks); + UniquePtr<ProfileBufferChunk> oldest = + std::exchange(mReleasedChunks, mReleasedChunks->ReleaseNext()); + mReleasedBufferBytes -= oldest->BufferBytes(); + if (mChunkDestroyedCallback) { + // Inform the user that we're going to destroy this chunk. + mChunkDestroyedCallback(*oldest); + } + + mTotalBytes -= + MaybeRecycleChunkAndGetDeallocatedSize(std::move(oldest), aLock); + } + + using ChunkAndUpdate = std::pair<UniquePtr<ProfileBufferChunk>, Update>; + [[nodiscard]] ChunkAndUpdate GetChunk( + const baseprofiler::detail::BaseProfilerAutoLock& aLock) { + MOZ_ASSERT(mUser, "Not registered yet"); + // After this function, the total memory consumption will be the sum of: + // - Bytes from released (i.e., full) chunks, + // - Bytes from unreleased (still in use) chunks, + // - Bytes from the chunk we want to create/recycle. (Note that we don't + // count the extra bytes of chunk header, and of extra allocation ability, + // for the new chunk, as it's assumed to be negligible compared to the + // total memory limit.) + // If this total is higher than the local limit, we'll want to destroy + // the oldest released chunks until we're under the limit; if any, we may + // recycle one of them to avoid a deallocation followed by an allocation. + while (mReleasedBufferBytes + mUnreleasedBufferBytes + + mChunkMinBufferBytes >= + mMaxTotalBytes && + !!mReleasedChunks) { + // We have reached the local limit, discard the oldest released chunk. + DiscardOldestReleasedChunk(aLock); + } + + // Extract the recycled chunk, if any. + ChunkAndUpdate chunkAndUpdate{TakeRecycledChunk(aLock), Update()}; + UniquePtr<ProfileBufferChunk>& chunk = chunkAndUpdate.first; + + if (!chunk) { + // No recycled chunk -> Create a chunk now. (This could still fail.) + chunk = ProfileBufferChunk::Create(mChunkMinBufferBytes); + mTotalBytes += moz_malloc_usable_size(chunk.get()); + } + + if (chunk) { + // We do have a chunk (recycled or new), record its size as "unreleased". + mUnreleasedBufferBytes += chunk->BufferBytes(); + + chunkAndUpdate.second = + Update(mUnreleasedBufferBytes, mReleasedBufferBytes, + mReleasedChunks.get(), nullptr); + } + + return chunkAndUpdate; + } + + [[nodiscard]] size_t SizeOfExcludingThis( + MallocSizeOf aMallocSizeOf, + const baseprofiler::detail::BaseProfilerAutoLock&) const { + MOZ_ASSERT(mUser, "Not registered yet"); + size_t size = 0; + if (mReleasedChunks) { + size += mReleasedChunks->SizeOfIncludingThis(aMallocSizeOf); + } + if (mRecycledChunks) { + size += mRecycledChunks->SizeOfIncludingThis(aMallocSizeOf); + } + // Note: Missing size of std::function external resources (if any). + return size; + } + + // Maxumum number of bytes that should be used by all unreleased and released + // chunks. Note that only released chunks can be destroyed here, so it is the + // responsibility of the user to properly release their chunks when possible. + const size_t mMaxTotalBytes; + + // Minimum number of bytes that new chunks should be able to store. + // Used when calling `ProfileBufferChunk::Create()`. + const Length mChunkMinBufferBytes; + + // Mutex guarding the following members. + mutable baseprofiler::detail::BaseProfilerMutex mMutex; + + // Number of bytes currently held in chunks that have been given away (through + // `GetChunk` or `RequestChunk`) and not released yet. + size_t mUnreleasedBufferBytes = 0; + + // Number of bytes currently held in chunks that have been released and stored + // in `mReleasedChunks` below. + size_t mReleasedBufferBytes = 0; + + // Total allocated size (used to substract it from memory counters). + size_t mTotalBytes = 0; + + // List of all released chunks. The oldest one should be at the start of the + // list, and may be destroyed or recycled when the memory limit is reached. + UniquePtr<ProfileBufferChunk> mReleasedChunks; + + // This may hold chunks that were released then slated for destruction, they + // will be reused next time an allocation would have been needed. + UniquePtr<ProfileBufferChunk> mRecycledChunks; + + // Optional callback used to notify the user when a chunk is about to be + // destroyed or recycled. (The data content is always destroyed, but the chunk + // container may be reused.) + std::function<void(const ProfileBufferChunk&)> mChunkDestroyedCallback; + + // Callback set from `RequestChunk()`, until it is serviced in + // `FulfillChunkRequests()`. There can only be one request in flight. + std::function<void(UniquePtr<ProfileBufferChunk>)> mChunkReceiver; + + // Separate mutex guarding mUpdateCallback, so that it may be invoked outside + // of the main buffer `mMutex`. + mutable baseprofiler::detail::BaseProfilerMutex mUpdateCallbackMutex; + + UpdateCallback mUpdateCallback; +}; + +} // namespace mozilla + +#endif // ProfileBufferChunkManagerWithLocalLimit_h diff --git a/mozglue/baseprofiler/public/ProfileBufferControlledChunkManager.h b/mozglue/baseprofiler/public/ProfileBufferControlledChunkManager.h new file mode 100644 index 0000000000..45b39b163c --- /dev/null +++ b/mozglue/baseprofiler/public/ProfileBufferControlledChunkManager.h @@ -0,0 +1,203 @@ +/* -*- Mode: C++; tab-width: 2; 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/. */ + +#ifndef ProfileBufferControlledChunkManager_h +#define ProfileBufferControlledChunkManager_h + +#include "mozilla/ProfileBufferChunk.h" + +#include <functional> +#include <vector> + +namespace mozilla { + +// A "Controlled" chunk manager will provide updates about chunks that it +// creates, releases, and destroys; and it can destroy released chunks as +// requested. +class ProfileBufferControlledChunkManager { + public: + using Length = ProfileBufferChunk::Length; + + virtual ~ProfileBufferControlledChunkManager() = default; + + // Minimum amount of chunk metadata to be transferred between processes. + struct ChunkMetadata { + // Timestamp when chunk was marked "done", which is used to: + // - determine its age, so the oldest one will be destroyed first, + // - uniquely identify this chunk in this process. (The parent process is + // responsible for associating this timestamp to its process id.) + TimeStamp mDoneTimeStamp; + // Size of this chunk's buffer. + Length mBufferBytes; + + ChunkMetadata(TimeStamp aDoneTimeStamp, Length aBufferBytes) + : mDoneTimeStamp(aDoneTimeStamp), mBufferBytes(aBufferBytes) {} + }; + + // Class collecting all information necessary to describe updates that + // happened in a chunk manager. + // An update can be folded into a previous update. + class Update { + public: + // Construct a "not-an-Update" object, which should only be used after a + // real update is folded into it. + Update() = default; + + // Construct a "final" Update, which marks the end of all updates from a + // chunk manager. + explicit Update(decltype(nullptr)) : mUnreleasedBytes(FINAL) {} + + // Construct an Update from the given data and released chunks. + // The chunk pointers may be null, and it doesn't matter if + // `aNewlyReleasedChunks` is already linked to `aExistingReleasedChunks` or + // not. + Update(size_t aUnreleasedBytes, size_t aReleasedBytes, + const ProfileBufferChunk* aExistingReleasedChunks, + const ProfileBufferChunk* aNewlyReleasedChunks) + : mUnreleasedBytes(aUnreleasedBytes), + mReleasedBytes(aReleasedBytes), + mOldestDoneTimeStamp( + aExistingReleasedChunks + ? aExistingReleasedChunks->ChunkHeader().mDoneTimeStamp + : TimeStamp{}) { + MOZ_RELEASE_ASSERT( + !IsNotUpdate(), + "Empty update should only be constructed with default constructor"); + MOZ_RELEASE_ASSERT( + !IsFinal(), + "Final update should only be constructed with nullptr constructor"); + for (const ProfileBufferChunk* chunk = aNewlyReleasedChunks; chunk; + chunk = chunk->GetNext()) { + mNewlyReleasedChunks.emplace_back(ChunkMetadata{ + chunk->ChunkHeader().mDoneTimeStamp, chunk->BufferBytes()}); + } + } + + // Construct an Update from raw data. + // This may be used to re-construct an Update that was previously + // serialized. + Update(size_t aUnreleasedBytes, size_t aReleasedBytes, + TimeStamp aOldestDoneTimeStamp, + std::vector<ChunkMetadata>&& aNewlyReleasedChunks) + : mUnreleasedBytes(aUnreleasedBytes), + mReleasedBytes(aReleasedBytes), + mOldestDoneTimeStamp(aOldestDoneTimeStamp), + mNewlyReleasedChunks(std::move(aNewlyReleasedChunks)) {} + + // Clear the Update completely and return it to a "not-an-Update" state. + void Clear() { + mUnreleasedBytes = NO_UPDATE; + mReleasedBytes = 0; + mOldestDoneTimeStamp = TimeStamp{}; + mNewlyReleasedChunks.clear(); + } + + bool IsNotUpdate() const { return mUnreleasedBytes == NO_UPDATE; } + + bool IsFinal() const { return mUnreleasedBytes == FINAL; } + + size_t UnreleasedBytes() const { + MOZ_RELEASE_ASSERT(!IsNotUpdate(), + "Cannot access UnreleasedBytes from empty update"); + MOZ_RELEASE_ASSERT(!IsFinal(), + "Cannot access UnreleasedBytes from final update"); + return mUnreleasedBytes; + } + + size_t ReleasedBytes() const { + MOZ_RELEASE_ASSERT(!IsNotUpdate(), + "Cannot access ReleasedBytes from empty update"); + MOZ_RELEASE_ASSERT(!IsFinal(), + "Cannot access ReleasedBytes from final update"); + return mReleasedBytes; + } + + TimeStamp OldestDoneTimeStamp() const { + MOZ_RELEASE_ASSERT(!IsNotUpdate(), + "Cannot access OldestDoneTimeStamp from empty update"); + MOZ_RELEASE_ASSERT(!IsFinal(), + "Cannot access OldestDoneTimeStamp from final update"); + return mOldestDoneTimeStamp; + } + + const std::vector<ChunkMetadata>& NewlyReleasedChunksRef() const { + MOZ_RELEASE_ASSERT( + !IsNotUpdate(), + "Cannot access NewlyReleasedChunksRef from empty update"); + MOZ_RELEASE_ASSERT( + !IsFinal(), "Cannot access NewlyReleasedChunksRef from final update"); + return mNewlyReleasedChunks; + } + + // Fold a later update into this one. + void Fold(Update&& aNewUpdate) { + MOZ_ASSERT( + !IsFinal() || aNewUpdate.IsFinal(), + "There shouldn't be another non-final update after the final update"); + + if (IsNotUpdate() || aNewUpdate.IsFinal()) { + // We were empty, or the new update is the final update, we just switch + // to that new update. + *this = std::move(aNewUpdate); + return; + } + + mUnreleasedBytes = aNewUpdate.mUnreleasedBytes; + mReleasedBytes = aNewUpdate.mReleasedBytes; + if (!aNewUpdate.mOldestDoneTimeStamp.IsNull()) { + MOZ_ASSERT(mOldestDoneTimeStamp.IsNull() || + mOldestDoneTimeStamp <= aNewUpdate.mOldestDoneTimeStamp); + mOldestDoneTimeStamp = aNewUpdate.mOldestDoneTimeStamp; + auto it = mNewlyReleasedChunks.begin(); + while (it != mNewlyReleasedChunks.end() && + it->mDoneTimeStamp < mOldestDoneTimeStamp) { + it = mNewlyReleasedChunks.erase(it); + } + } + if (!aNewUpdate.mNewlyReleasedChunks.empty()) { + mNewlyReleasedChunks.reserve(mNewlyReleasedChunks.size() + + aNewUpdate.mNewlyReleasedChunks.size()); + mNewlyReleasedChunks.insert(mNewlyReleasedChunks.end(), + aNewUpdate.mNewlyReleasedChunks.begin(), + aNewUpdate.mNewlyReleasedChunks.end()); + } + } + + private: + static const size_t NO_UPDATE = size_t(-1); + static const size_t FINAL = size_t(-2); + + size_t mUnreleasedBytes = NO_UPDATE; + size_t mReleasedBytes = 0; + TimeStamp mOldestDoneTimeStamp; + std::vector<ChunkMetadata> mNewlyReleasedChunks; + }; + + using UpdateCallback = std::function<void(Update&&)>; + + // This *may* be set (or reset) by an object that needs to know about all + // chunk updates that happen in this manager. The main use will be to + // coordinate the global memory usage of Firefox. + // If a non-empty callback is given, it will be immediately invoked with the + // current state. + // When the callback is about to be destroyed (by overwriting it here, or in + // the class destructor), it will be invoked one last time with an empty + // update. + // Note that the callback (even the first current-state callback) will be + // invoked from inside a locked scope, so it should *not* call other functions + // of the chunk manager. A side benefit of this locking is that it guarantees + // that no two invocations can overlap. + virtual void SetUpdateCallback(UpdateCallback&& aUpdateCallback) = 0; + + // This is a request to destroy all chunks before the given timestamp. + // This timestamp should be one that was given in a previous UpdateCallback + // call. Obviously, only released chunks can be destroyed. + virtual void DestroyChunksAtOrBefore(TimeStamp aDoneTimeStamp) = 0; +}; + +} // namespace mozilla + +#endif // ProfileBufferControlledChunkManager_h diff --git a/mozglue/baseprofiler/public/ProfileBufferEntryKinds.h b/mozglue/baseprofiler/public/ProfileBufferEntryKinds.h new file mode 100644 index 0000000000..cd5912c49b --- /dev/null +++ b/mozglue/baseprofiler/public/ProfileBufferEntryKinds.h @@ -0,0 +1,105 @@ +/* -*- Mode: C++; tab-width: 2; 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/. */ + +#ifndef ProfileBufferEntryKinds_h +#define ProfileBufferEntryKinds_h + +#include "mozilla/BaseProfilerUtils.h" + +#include <cstdint> + +namespace mozilla { + +// This is equal to sizeof(double), which is the largest non-char variant in +// |u|. +static constexpr size_t ProfileBufferEntryNumChars = 8; + +// NOTE! If you add entries, you need to verify if they need to be added to the +// switch statement in DuplicateLastSample! +// This will evaluate the MACRO with (KIND, TYPE, SIZE) +#define FOR_EACH_PROFILE_BUFFER_ENTRY_KIND(MACRO) \ + MACRO(CategoryPair, int, sizeof(int)) \ + MACRO(CollectionStart, double, sizeof(double)) \ + MACRO(CollectionEnd, double, sizeof(double)) \ + MACRO(Label, const char*, sizeof(const char*)) \ + MACRO(FrameFlags, uint64_t, sizeof(uint64_t)) \ + MACRO(DynamicStringFragment, char*, ProfileBufferEntryNumChars) \ + MACRO(JitReturnAddr, void*, sizeof(void*)) \ + MACRO(InnerWindowID, uint64_t, sizeof(uint64_t)) \ + MACRO(LineNumber, int, sizeof(int)) \ + MACRO(ColumnNumber, int, sizeof(int)) \ + MACRO(NativeLeafAddr, void*, sizeof(void*)) \ + MACRO(Pause, double, sizeof(double)) \ + MACRO(Resume, double, sizeof(double)) \ + MACRO(PauseSampling, double, sizeof(double)) \ + MACRO(ResumeSampling, double, sizeof(double)) \ + MACRO(Responsiveness, double, sizeof(double)) \ + MACRO(ThreadId, ::mozilla::baseprofiler::BaseProfilerThreadId, \ + sizeof(::mozilla::baseprofiler::BaseProfilerThreadId)) \ + MACRO(Time, double, sizeof(double)) \ + MACRO(TimeBeforeCompactStack, double, sizeof(double)) \ + MACRO(TimeBeforeSameSample, double, sizeof(double)) \ + MACRO(CounterId, void*, sizeof(void*)) \ + MACRO(CounterKey, uint64_t, sizeof(uint64_t)) \ + MACRO(Number, uint64_t, sizeof(uint64_t)) \ + MACRO(Count, int64_t, sizeof(int64_t)) \ + MACRO(ProfilerOverheadTime, double, sizeof(double)) \ + MACRO(ProfilerOverheadDuration, double, sizeof(double)) + +// The `Kind` is a single byte identifying the type of data that is actually +// stored in a `ProfileBufferEntry`, as per the list in +// `FOR_EACH_PROFILE_BUFFER_ENTRY_KIND`. +// +// This byte is also used to identify entries in ProfileChunkedBuffer blocks, +// for both "legacy" entries that do contain a `ProfileBufferEntry`, and for +// new types of entries that may carry more data of different types. +// TODO: Eventually each type of "legacy" entry should be replaced with newer, +// more efficient kinds of entries (e.g., stack frames could be stored in one +// bigger entry, instead of multiple `ProfileBufferEntry`s); then we could +// discard `ProfileBufferEntry` and move this enum to a more appropriate spot. +enum class ProfileBufferEntryKind : uint8_t { + INVALID = 0, +#define KIND(KIND, TYPE, SIZE) KIND, + FOR_EACH_PROFILE_BUFFER_ENTRY_KIND(KIND) +#undef KIND + + // Any value under `LEGACY_LIMIT` represents a `ProfileBufferEntry`. + LEGACY_LIMIT, + + // Any value starting here does *not* represent a `ProfileBufferEntry` and + // requires separate decoding and handling. + + // Markers and their data. + Marker = LEGACY_LIMIT, + + // Entry with "running times", such as CPU usage measurements. + // Optional between TimeBeforeX and X. + RunningTimes, + + // Optional between TimeBeforeX and X. + UnresponsiveDurationMs, + + // Collection of legacy stack entries, must follow a ThreadId and + // TimeBeforeCompactStack (which are not included in the CompactStack; + // TimeBeforeCompactStack is equivalent to Time, but indicates that a + // CompactStack follows shortly afterwards). + CompactStack, + + // Indicates that this sample is identical to the previous one, must follow a + // ThreadId and TimeBeforeSameSample. + SameSample, + + MODERN_LIMIT +}; + +enum class MarkerPayloadType : uint8_t { + Cpp, + Rust, +}; + +} // namespace mozilla + +#endif // ProfileBufferEntryKinds_h diff --git a/mozglue/baseprofiler/public/ProfileBufferEntrySerialization.h b/mozglue/baseprofiler/public/ProfileBufferEntrySerialization.h new file mode 100644 index 0000000000..bdb3b54d41 --- /dev/null +++ b/mozglue/baseprofiler/public/ProfileBufferEntrySerialization.h @@ -0,0 +1,1184 @@ +/* -*- Mode: C++; tab-width: 2; 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/. */ + +#ifndef ProfileBufferEntrySerialization_h +#define ProfileBufferEntrySerialization_h + +#include "mozilla/Assertions.h" +#include "mozilla/leb128iterator.h" +#include "mozilla/Likely.h" +#include "mozilla/Maybe.h" +#include "mozilla/ProfileBufferIndex.h" +#include "mozilla/Span.h" +#include "mozilla/UniquePtrExtensions.h" +#include "mozilla/Unused.h" +#include "mozilla/Variant.h" + +#include <string> +#include <tuple> + +namespace mozilla { + +class ProfileBufferEntryWriter; + +// Iterator-like class used to read from an entry. +// An entry may be split in two memory segments (e.g., the ends of a ring +// buffer, or two chunks of a chunked buffer); it doesn't deal with this +// underlying buffer, but only with one or two spans pointing at the space +// where the entry lives. +class ProfileBufferEntryReader { + public: + using Byte = uint8_t; + using Length = uint32_t; + + using SpanOfConstBytes = Span<const Byte>; + + // Class to be specialized for types to be read from a profile buffer entry. + // See common specializations at the bottom of this header. + // The following static functions must be provided: + // static void ReadInto(EntryReader aER&, T& aT) + // { + // /* Call `aER.ReadX(...)` function to deserialize into aT, be sure to + // read exactly `Bytes(aT)`! */ + // } + // static T Read(EntryReader& aER) { + // /* Call `aER.ReadX(...)` function to deserialize and return a `T`, be + // sure to read exactly `Bytes(returned value)`! */ + // } + template <typename T> + struct Deserializer; + + ProfileBufferEntryReader() = default; + + // Reader over one Span. + ProfileBufferEntryReader(SpanOfConstBytes aSpan, + ProfileBufferBlockIndex aCurrentBlockIndex, + ProfileBufferBlockIndex aNextBlockIndex) + : mCurrentSpan(aSpan), + mNextSpanOrEmpty(aSpan.Last(0)), + mCurrentBlockIndex(aCurrentBlockIndex), + mNextBlockIndex(aNextBlockIndex) { + // 2nd internal Span points at the end of the 1st internal Span, to enforce + // invariants. + CheckInvariants(); + } + + // Reader over two Spans, the second one must not be empty. + ProfileBufferEntryReader(SpanOfConstBytes aSpanHead, + SpanOfConstBytes aSpanTail, + ProfileBufferBlockIndex aCurrentBlockIndex, + ProfileBufferBlockIndex aNextBlockIndex) + : mCurrentSpan(aSpanHead), + mNextSpanOrEmpty(aSpanTail), + mCurrentBlockIndex(aCurrentBlockIndex), + mNextBlockIndex(aNextBlockIndex) { + MOZ_RELEASE_ASSERT(!mNextSpanOrEmpty.IsEmpty()); + if (MOZ_UNLIKELY(mCurrentSpan.IsEmpty())) { + // First span is already empty, skip it. + mCurrentSpan = mNextSpanOrEmpty; + mNextSpanOrEmpty = mNextSpanOrEmpty.Last(0); + } + CheckInvariants(); + } + + // Allow copying, which is needed when used as an iterator in some std + // functions (e.g., string assignment), and to occasionally backtrack. + // Be aware that the main profile buffer APIs give a reference to an entry + // reader, and expect that reader to advance to the end of the entry, so don't + // just advance copies! + ProfileBufferEntryReader(const ProfileBufferEntryReader&) = default; + ProfileBufferEntryReader& operator=(const ProfileBufferEntryReader&) = + default; + + // Don't =default moving, as it doesn't bring any benefit in this class. + + [[nodiscard]] Length RemainingBytes() const { + return mCurrentSpan.LengthBytes() + mNextSpanOrEmpty.LengthBytes(); + } + + void SetRemainingBytes(Length aBytes) { + MOZ_RELEASE_ASSERT(aBytes <= RemainingBytes()); + if (aBytes <= mCurrentSpan.LengthBytes()) { + mCurrentSpan = mCurrentSpan.First(aBytes); + mNextSpanOrEmpty = mCurrentSpan.Last(0); + } else { + mNextSpanOrEmpty = + mNextSpanOrEmpty.First(aBytes - mCurrentSpan.LengthBytes()); + } + } + + [[nodiscard]] ProfileBufferBlockIndex CurrentBlockIndex() const { + return mCurrentBlockIndex; + } + + [[nodiscard]] ProfileBufferBlockIndex NextBlockIndex() const { + return mNextBlockIndex; + } + + // Create a reader of size zero, pointing at aOffset past the current position + // of this Reader, so it can be used as end iterator. + [[nodiscard]] ProfileBufferEntryReader EmptyIteratorAtOffset( + Length aOffset) const { + MOZ_RELEASE_ASSERT(aOffset <= RemainingBytes()); + if (MOZ_LIKELY(aOffset < mCurrentSpan.LengthBytes())) { + // aOffset is before the end of mCurrentSpan. + return ProfileBufferEntryReader(mCurrentSpan.Subspan(aOffset, 0), + mCurrentBlockIndex, mNextBlockIndex); + } + // aOffset is right at the end of mCurrentSpan, or inside mNextSpanOrEmpty. + return ProfileBufferEntryReader( + mNextSpanOrEmpty.Subspan(aOffset - mCurrentSpan.LengthBytes(), 0), + mCurrentBlockIndex, mNextBlockIndex); + } + + // Be like a limited input iterator, with only `*`, prefix-`++`, `==`, `!=`. + // These definitions are expected by std functions, to recognize this as an + // iterator. See https://en.cppreference.com/w/cpp/iterator/iterator_traits + using difference_type = std::make_signed_t<Length>; + using value_type = Byte; + using pointer = const Byte*; + using reference = const Byte&; + using iterator_category = std::input_iterator_tag; + + [[nodiscard]] const Byte& operator*() { + // Assume the caller will read from the returned reference (and not just + // take the address). + MOZ_RELEASE_ASSERT(mCurrentSpan.LengthBytes() >= 1); + return *(mCurrentSpan.Elements()); + } + + ProfileBufferEntryReader& operator++() { + MOZ_RELEASE_ASSERT(mCurrentSpan.LengthBytes() >= 1); + if (MOZ_LIKELY(mCurrentSpan.LengthBytes() > 1)) { + // More than 1 byte left in mCurrentSpan, just eat it. + mCurrentSpan = mCurrentSpan.From(1); + } else { + // mCurrentSpan will be empty, move mNextSpanOrEmpty to mCurrentSpan. + mCurrentSpan = mNextSpanOrEmpty; + mNextSpanOrEmpty = mNextSpanOrEmpty.Last(0); + } + CheckInvariants(); + return *this; + } + + ProfileBufferEntryReader& operator+=(Length aBytes) { + MOZ_RELEASE_ASSERT(aBytes <= RemainingBytes()); + if (MOZ_LIKELY(aBytes <= mCurrentSpan.LengthBytes())) { + // All bytes are in mCurrentSpan. + // Update mCurrentSpan past the read bytes. + mCurrentSpan = mCurrentSpan.From(aBytes); + if (mCurrentSpan.IsEmpty() && !mNextSpanOrEmpty.IsEmpty()) { + // Don't leave mCurrentSpan empty, move non-empty mNextSpanOrEmpty into + // mCurrentSpan. + mCurrentSpan = mNextSpanOrEmpty; + mNextSpanOrEmpty = mNextSpanOrEmpty.Last(0); + } + } else { + // mCurrentSpan does not hold enough bytes. + // This should only happen at most once: Only for double spans, and when + // data crosses the gap. + const Length tail = + aBytes - static_cast<Length>(mCurrentSpan.LengthBytes()); + // Move mNextSpanOrEmpty to mCurrentSpan, past the data. So the next call + // will go back to the true case above. + mCurrentSpan = mNextSpanOrEmpty.From(tail); + mNextSpanOrEmpty = mNextSpanOrEmpty.Last(0); + } + CheckInvariants(); + return *this; + } + + [[nodiscard]] bool operator==(const ProfileBufferEntryReader& aOther) const { + return mCurrentSpan.Elements() == aOther.mCurrentSpan.Elements(); + } + [[nodiscard]] bool operator!=(const ProfileBufferEntryReader& aOther) const { + return mCurrentSpan.Elements() != aOther.mCurrentSpan.Elements(); + } + + // Read an unsigned LEB128 number and move iterator ahead. + template <typename T> + [[nodiscard]] T ReadULEB128() { + return ::mozilla::ReadULEB128<T>(*this); + } + + // This struct points at a number of bytes through either one span, or two + // separate spans (in the rare cases when it is split between two chunks). + // So the possibilities are: + // - Totally empty: { [] [] } + // - First span is not empty: { [content] [] } (Most common case.) + // - Both spans are not empty: { [cont] [ent] } + // But something like { [] [content] } is not possible. + // + // Recommended usage patterns: + // - Call a utility function like `CopyBytesTo` if you always need to copy the + // data to an outside buffer, e.g., to deserialize an aligned object. + // - Access both spans one after the other; Note that the second one may be + // empty; and the fist could be empty as well if there is no data at all. + // - Check is the second span is empty, in which case you only need to read + // the first one; and since its part of a chunk, it may be directly passed + // as an unaligned pointer or reference, thereby saving one copy. But + // remember to always handle the double-span case as well. + // + // Reminder: An empty span still has a non-null pointer, so it's safe to use + // with functions like memcpy. + struct DoubleSpanOfConstBytes { + SpanOfConstBytes mFirstOrOnly; + SpanOfConstBytes mSecondOrEmpty; + + void CheckInvariants() const { + MOZ_ASSERT(mFirstOrOnly.IsEmpty() ? mSecondOrEmpty.IsEmpty() : true, + "mSecondOrEmpty should not be the only span to contain data"); + } + + DoubleSpanOfConstBytes() : mFirstOrOnly(), mSecondOrEmpty() { + CheckInvariants(); + } + + DoubleSpanOfConstBytes(const Byte* aOnlyPointer, size_t aOnlyLength) + : mFirstOrOnly(aOnlyPointer, aOnlyLength), mSecondOrEmpty() { + CheckInvariants(); + } + + DoubleSpanOfConstBytes(const Byte* aFirstPointer, size_t aFirstLength, + const Byte* aSecondPointer, size_t aSecondLength) + : mFirstOrOnly(aFirstPointer, aFirstLength), + mSecondOrEmpty(aSecondPointer, aSecondLength) { + CheckInvariants(); + } + + // Is there no data at all? + [[nodiscard]] bool IsEmpty() const { + // We only need to check the first span, because if it's empty, the second + // one must be empty as well. + return mFirstOrOnly.IsEmpty(); + } + + // Total length (in bytes) pointed at by both spans. + [[nodiscard]] size_t LengthBytes() const { + return mFirstOrOnly.LengthBytes() + mSecondOrEmpty.LengthBytes(); + } + + // Utility functions to copy all `LengthBytes()` to a given buffer. + void CopyBytesTo(void* aDest) const { + memcpy(aDest, mFirstOrOnly.Elements(), mFirstOrOnly.LengthBytes()); + if (MOZ_UNLIKELY(!mSecondOrEmpty.IsEmpty())) { + memcpy(static_cast<Byte*>(aDest) + mFirstOrOnly.LengthBytes(), + mSecondOrEmpty.Elements(), mSecondOrEmpty.LengthBytes()); + } + } + + // If the second span is empty, only the first span may point at data. + [[nodiscard]] bool IsSingleSpan() const { return mSecondOrEmpty.IsEmpty(); } + }; + + // Get Span(s) to a sequence of bytes, see `DoubleSpanOfConstBytes` for usage. + // Note that the reader location is *not* updated, do `+=` on it afterwards. + [[nodiscard]] DoubleSpanOfConstBytes PeekSpans(Length aBytes) const { + MOZ_RELEASE_ASSERT(aBytes <= RemainingBytes()); + if (MOZ_LIKELY(aBytes <= mCurrentSpan.LengthBytes())) { + // All `aBytes` are in the current chunk, only one span is needed. + return DoubleSpanOfConstBytes{mCurrentSpan.Elements(), aBytes}; + } + // Otherwise the first span covers then end of the current chunk, and the + // second span starts in the next chunk. + return DoubleSpanOfConstBytes{ + mCurrentSpan.Elements(), mCurrentSpan.LengthBytes(), + mNextSpanOrEmpty.Elements(), aBytes - mCurrentSpan.LengthBytes()}; + } + + // Get Span(s) to a sequence of bytes, see `DoubleSpanOfConstBytes` for usage, + // and move the reader forward. + [[nodiscard]] DoubleSpanOfConstBytes ReadSpans(Length aBytes) { + DoubleSpanOfConstBytes spans = PeekSpans(aBytes); + (*this) += aBytes; + return spans; + } + + // Read a sequence of bytes, like memcpy. + void ReadBytes(void* aDest, Length aBytes) { + DoubleSpanOfConstBytes spans = ReadSpans(aBytes); + MOZ_ASSERT(spans.LengthBytes() == aBytes); + spans.CopyBytesTo(aDest); + } + + template <typename T> + void ReadIntoObject(T& aObject) { + Deserializer<T>::ReadInto(*this, aObject); + } + + // Read into one or more objects, sequentially. + // `EntryReader::ReadIntoObjects()` with nothing is implicitly allowed, this + // could be useful for generic programming. + template <typename... Ts> + void ReadIntoObjects(Ts&... aTs) { + (ReadIntoObject(aTs), ...); + } + + // Read data as an object and move iterator ahead. + template <typename T> + [[nodiscard]] T ReadObject() { + T ob = Deserializer<T>::Read(*this); + return ob; + } + + private: + friend class ProfileBufferEntryWriter; + + // Invariants: + // - mCurrentSpan cannot be empty unless mNextSpanOrEmpty is also empty. So + // mCurrentSpan always points at the next byte to read or the end. + // - If mNextSpanOrEmpty is empty, it points at the end of mCurrentSpan. So + // when reaching the end of mCurrentSpan, we can blindly move + // mNextSpanOrEmpty to mCurrentSpan and keep the invariants. + SpanOfConstBytes mCurrentSpan; + SpanOfConstBytes mNextSpanOrEmpty; + ProfileBufferBlockIndex mCurrentBlockIndex; + ProfileBufferBlockIndex mNextBlockIndex; + + void CheckInvariants() const { + MOZ_ASSERT(!mCurrentSpan.IsEmpty() || mNextSpanOrEmpty.IsEmpty()); + MOZ_ASSERT(!mNextSpanOrEmpty.IsEmpty() || + (mNextSpanOrEmpty == mCurrentSpan.Last(0))); + } +}; + +// Iterator-like class used to write into an entry. +// An entry may be split in two memory segments (e.g., the ends of a ring +// buffer, or two chunks of a chunked buffer); it doesn't deal with this +// underlying buffer, but only with one or two spans pointing at the space +// reserved for the entry. +class ProfileBufferEntryWriter { + public: + using Byte = uint8_t; + using Length = uint32_t; + + using SpanOfBytes = Span<Byte>; + + // Class to be specialized for types to be written in an entry. + // See common specializations at the bottom of this header. + // The following static functions must be provided: + // static Length Bytes(const T& aT) { + // /* Return number of bytes that will be written. */ + // } + // static void Write(ProfileBufferEntryWriter& aEW, + // const T& aT) { + // /* Call `aEW.WriteX(...)` functions to serialize aT, be sure to write + // exactly `Bytes(aT)` bytes! */ + // } + template <typename T> + struct Serializer; + + ProfileBufferEntryWriter() = default; + + ProfileBufferEntryWriter(SpanOfBytes aSpan, + ProfileBufferBlockIndex aCurrentBlockIndex, + ProfileBufferBlockIndex aNextBlockIndex) + : mCurrentSpan(aSpan), + mCurrentBlockIndex(aCurrentBlockIndex), + mNextBlockIndex(aNextBlockIndex) {} + + ProfileBufferEntryWriter(SpanOfBytes aSpanHead, SpanOfBytes aSpanTail, + ProfileBufferBlockIndex aCurrentBlockIndex, + ProfileBufferBlockIndex aNextBlockIndex) + : mCurrentSpan(aSpanHead), + mNextSpanOrEmpty(aSpanTail), + mCurrentBlockIndex(aCurrentBlockIndex), + mNextBlockIndex(aNextBlockIndex) { + // Either: + // - mCurrentSpan is not empty, OR + // - mNextSpanOrEmpty is empty if mNextSpanOrEmpty is empty as well. + MOZ_RELEASE_ASSERT(!mCurrentSpan.IsEmpty() || mNextSpanOrEmpty.IsEmpty()); + } + + // Disable copying and moving, so we can't have multiple writing heads. + ProfileBufferEntryWriter(const ProfileBufferEntryWriter&) = delete; + ProfileBufferEntryWriter& operator=(const ProfileBufferEntryWriter&) = delete; + ProfileBufferEntryWriter(ProfileBufferEntryWriter&&) = delete; + ProfileBufferEntryWriter& operator=(ProfileBufferEntryWriter&&) = delete; + + void Set() { + mCurrentSpan = SpanOfBytes{}; + mNextSpanOrEmpty = SpanOfBytes{}; + mCurrentBlockIndex = nullptr; + mNextBlockIndex = nullptr; + } + + void Set(SpanOfBytes aSpan, ProfileBufferBlockIndex aCurrentBlockIndex, + ProfileBufferBlockIndex aNextBlockIndex) { + mCurrentSpan = aSpan; + mNextSpanOrEmpty = SpanOfBytes{}; + mCurrentBlockIndex = aCurrentBlockIndex; + mNextBlockIndex = aNextBlockIndex; + } + + void Set(SpanOfBytes aSpan0, SpanOfBytes aSpan1, + ProfileBufferBlockIndex aCurrentBlockIndex, + ProfileBufferBlockIndex aNextBlockIndex) { + mCurrentSpan = aSpan0; + mNextSpanOrEmpty = aSpan1; + mCurrentBlockIndex = aCurrentBlockIndex; + mNextBlockIndex = aNextBlockIndex; + // Either: + // - mCurrentSpan is not empty, OR + // - mNextSpanOrEmpty is empty if mNextSpanOrEmpty is empty as well. + MOZ_RELEASE_ASSERT(!mCurrentSpan.IsEmpty() || mNextSpanOrEmpty.IsEmpty()); + } + + [[nodiscard]] Length RemainingBytes() const { + return mCurrentSpan.LengthBytes() + mNextSpanOrEmpty.LengthBytes(); + } + + [[nodiscard]] ProfileBufferBlockIndex CurrentBlockIndex() const { + return mCurrentBlockIndex; + } + + [[nodiscard]] ProfileBufferBlockIndex NextBlockIndex() const { + return mNextBlockIndex; + } + + // Be like a limited output iterator, with only `*` and prefix-`++`. + // These definitions are expected by std functions, to recognize this as an + // iterator. See https://en.cppreference.com/w/cpp/iterator/iterator_traits + using value_type = Byte; + using pointer = Byte*; + using reference = Byte&; + using iterator_category = std::output_iterator_tag; + + [[nodiscard]] Byte& operator*() { + MOZ_RELEASE_ASSERT(RemainingBytes() >= 1); + return *( + (MOZ_LIKELY(!mCurrentSpan.IsEmpty()) ? mCurrentSpan : mNextSpanOrEmpty) + .Elements()); + } + + ProfileBufferEntryWriter& operator++() { + if (MOZ_LIKELY(mCurrentSpan.LengthBytes() >= 1)) { + // There is at least 1 byte in mCurrentSpan, eat it. + mCurrentSpan = mCurrentSpan.From(1); + } else { + // mCurrentSpan is empty, move mNextSpanOrEmpty (past the first byte) to + // mCurrentSpan. + MOZ_RELEASE_ASSERT(mNextSpanOrEmpty.LengthBytes() >= 1); + mCurrentSpan = mNextSpanOrEmpty.From(1); + mNextSpanOrEmpty = mNextSpanOrEmpty.First(0); + } + return *this; + } + + ProfileBufferEntryWriter& operator+=(Length aBytes) { + // Note: This is a rare operation. The code below is a copy of `WriteBytes` + // but without the `memcpy`s. + MOZ_RELEASE_ASSERT(aBytes <= RemainingBytes()); + if (MOZ_LIKELY(aBytes <= mCurrentSpan.LengthBytes())) { + // Data fits in mCurrentSpan. + // Update mCurrentSpan. It may become empty, so in case of a double span, + // the next call will go to the false case below. + mCurrentSpan = mCurrentSpan.From(aBytes); + } else { + // Data does not fully fit in mCurrentSpan. + // This should only happen at most once: Only for double spans, and when + // data crosses the gap or starts there. + const Length tail = + aBytes - static_cast<Length>(mCurrentSpan.LengthBytes()); + // Move mNextSpanOrEmpty to mCurrentSpan, past the data. So the next call + // will go back to the true case above. + mCurrentSpan = mNextSpanOrEmpty.From(tail); + mNextSpanOrEmpty = mNextSpanOrEmpty.First(0); + } + return *this; + } + + // Number of bytes needed to represent `aValue` in unsigned LEB128. + template <typename T> + [[nodiscard]] static unsigned ULEB128Size(T aValue) { + return ::mozilla::ULEB128Size(aValue); + } + + // Write number as unsigned LEB128 and move iterator ahead. + template <typename T> + void WriteULEB128(T aValue) { + ::mozilla::WriteULEB128(aValue, *this); + } + + // Number of bytes needed to serialize objects. + template <typename... Ts> + [[nodiscard]] static Length SumBytes(const Ts&... aTs) { + return (0 + ... + Serializer<Ts>::Bytes(aTs)); + } + + // Write a sequence of bytes, like memcpy. + void WriteBytes(const void* aSrc, Length aBytes) { + MOZ_RELEASE_ASSERT(aBytes <= RemainingBytes()); + if (MOZ_LIKELY(aBytes <= mCurrentSpan.LengthBytes())) { + // Data fits in mCurrentSpan. + memcpy(mCurrentSpan.Elements(), aSrc, aBytes); + // Update mCurrentSpan. It may become empty, so in case of a double span, + // the next call will go to the false case below. + mCurrentSpan = mCurrentSpan.From(aBytes); + } else { + // Data does not fully fit in mCurrentSpan. + // This should only happen at most once: Only for double spans, and when + // data crosses the gap or starts there. + // Split data between the end of mCurrentSpan and the beginning of + // mNextSpanOrEmpty. (mCurrentSpan could be empty, it's ok to do a memcpy + // because Span::Elements() is never null.) + memcpy(mCurrentSpan.Elements(), aSrc, mCurrentSpan.LengthBytes()); + const Length tail = + aBytes - static_cast<Length>(mCurrentSpan.LengthBytes()); + memcpy(mNextSpanOrEmpty.Elements(), + reinterpret_cast<const Byte*>(aSrc) + mCurrentSpan.LengthBytes(), + tail); + // Move mNextSpanOrEmpty to mCurrentSpan, past the data. So the next call + // will go back to the true case above. + mCurrentSpan = mNextSpanOrEmpty.From(tail); + mNextSpanOrEmpty = mNextSpanOrEmpty.First(0); + } + } + + void WriteFromReader(ProfileBufferEntryReader& aReader, Length aBytes) { + MOZ_RELEASE_ASSERT(aBytes <= RemainingBytes()); + MOZ_RELEASE_ASSERT(aBytes <= aReader.RemainingBytes()); + Length read0 = std::min( + aBytes, static_cast<Length>(aReader.mCurrentSpan.LengthBytes())); + if (read0 != 0) { + WriteBytes(aReader.mCurrentSpan.Elements(), read0); + } + Length read1 = aBytes - read0; + if (read1 != 0) { + WriteBytes(aReader.mNextSpanOrEmpty.Elements(), read1); + } + aReader += aBytes; + } + + // Write a single object by using the appropriate Serializer. + template <typename T> + void WriteObject(const T& aObject) { + Serializer<T>::Write(*this, aObject); + } + + // Write one or more objects, sequentially. + // Allow `EntryWrite::WriteObjects()` with nothing, this could be useful + // for generic programming. + template <typename... Ts> + void WriteObjects(const Ts&... aTs) { + (WriteObject(aTs), ...); + } + + private: + // The two spans covering the memory still to be written. + SpanOfBytes mCurrentSpan; + SpanOfBytes mNextSpanOrEmpty; + ProfileBufferBlockIndex mCurrentBlockIndex; + ProfileBufferBlockIndex mNextBlockIndex; +}; + +// ============================================================================ +// Serializer and Deserializer ready-to-use specializations. + +// ---------------------------------------------------------------------------- +// Trivially-copyable types (default) + +// The default implementation works for all trivially-copyable types (e.g., +// PODs). +// +// Usage: `aEW.WriteObject(123);`. +// +// Raw pointers, though trivially-copyable, are explictly forbidden when writing +// (to avoid unexpected leaks/UAFs), instead use one of +// `WrapProfileBufferLiteralCStringPointer`, `WrapProfileBufferUnownedCString`, +// or `WrapProfileBufferRawPointer` as needed. +template <typename T> +struct ProfileBufferEntryWriter::Serializer { + static_assert(std::is_trivially_copyable<T>::value, + "Serializer only works with trivially-copyable types by " + "default, use/add specialization for other types."); + + static constexpr Length Bytes(const T&) { return sizeof(T); } + + static void Write(ProfileBufferEntryWriter& aEW, const T& aT) { + static_assert(!std::is_pointer<T>::value, + "Serializer won't write raw pointers by default, use " + "WrapProfileBufferRawPointer or other."); + aEW.WriteBytes(&aT, sizeof(T)); + } +}; + +// Usage: `aER.ReadObject<int>();` or `int x; aER.ReadIntoObject(x);`. +template <typename T> +struct ProfileBufferEntryReader::Deserializer { + static_assert(std::is_trivially_copyable<T>::value, + "Deserializer only works with trivially-copyable types by " + "default, use/add specialization for other types."); + + static void ReadInto(ProfileBufferEntryReader& aER, T& aT) { + aER.ReadBytes(&aT, sizeof(T)); + } + + static T Read(ProfileBufferEntryReader& aER) { + // Note that this creates a default `T` first, and then overwrites it with + // bytes from the buffer. Trivially-copyable types support this without UB. + T ob; + ReadInto(aER, ob); + return ob; + } +}; + +// ---------------------------------------------------------------------------- +// Strip const/volatile/reference from types. + +// Automatically strip `const`. +template <typename T> +struct ProfileBufferEntryWriter::Serializer<const T> + : public ProfileBufferEntryWriter::Serializer<T> {}; + +template <typename T> +struct ProfileBufferEntryReader::Deserializer<const T> + : public ProfileBufferEntryReader::Deserializer<T> {}; + +// Automatically strip `volatile`. +template <typename T> +struct ProfileBufferEntryWriter::Serializer<volatile T> + : public ProfileBufferEntryWriter::Serializer<T> {}; + +template <typename T> +struct ProfileBufferEntryReader::Deserializer<volatile T> + : public ProfileBufferEntryReader::Deserializer<T> {}; + +// Automatically strip `lvalue-reference`. +template <typename T> +struct ProfileBufferEntryWriter::Serializer<T&> + : public ProfileBufferEntryWriter::Serializer<T> {}; + +template <typename T> +struct ProfileBufferEntryReader::Deserializer<T&> + : public ProfileBufferEntryReader::Deserializer<T> {}; + +// Automatically strip `rvalue-reference`. +template <typename T> +struct ProfileBufferEntryWriter::Serializer<T&&> + : public ProfileBufferEntryWriter::Serializer<T> {}; + +template <typename T> +struct ProfileBufferEntryReader::Deserializer<T&&> + : public ProfileBufferEntryReader::Deserializer<T> {}; + +// ---------------------------------------------------------------------------- +// ProfileBufferBlockIndex + +// ProfileBufferBlockIndex, serialized as the underlying value. +template <> +struct ProfileBufferEntryWriter::Serializer<ProfileBufferBlockIndex> { + static constexpr Length Bytes(const ProfileBufferBlockIndex& aBlockIndex) { + return sizeof(ProfileBufferBlockIndex); + } + + static void Write(ProfileBufferEntryWriter& aEW, + const ProfileBufferBlockIndex& aBlockIndex) { + aEW.WriteBytes(&aBlockIndex, sizeof(aBlockIndex)); + } +}; + +template <> +struct ProfileBufferEntryReader::Deserializer<ProfileBufferBlockIndex> { + static void ReadInto(ProfileBufferEntryReader& aER, + ProfileBufferBlockIndex& aBlockIndex) { + aER.ReadBytes(&aBlockIndex, sizeof(aBlockIndex)); + } + + static ProfileBufferBlockIndex Read(ProfileBufferEntryReader& aER) { + ProfileBufferBlockIndex blockIndex; + ReadInto(aER, blockIndex); + return blockIndex; + } +}; + +// ---------------------------------------------------------------------------- +// Literal C string pointer + +// Wrapper around a pointer to a literal C string. +template <size_t NonTerminalCharacters> +struct ProfileBufferLiteralCStringPointer { + const char* mCString; +}; + +// Wrap a pointer to a literal C string. +template <size_t CharactersIncludingTerminal> +ProfileBufferLiteralCStringPointer<CharactersIncludingTerminal - 1> +WrapProfileBufferLiteralCStringPointer( + const char (&aCString)[CharactersIncludingTerminal]) { + return {aCString}; +} + +// Literal C strings, serialized as the raw pointer because it is unique and +// valid for the whole program lifetime. +// +// Usage: `aEW.WriteObject(WrapProfileBufferLiteralCStringPointer("hi"));`. +// +// No deserializer is provided for this type, instead it must be deserialized as +// a raw pointer: `aER.ReadObject<const char*>();` +template <size_t CharactersIncludingTerminal> +struct ProfileBufferEntryReader::Deserializer< + ProfileBufferLiteralCStringPointer<CharactersIncludingTerminal>> { + static constexpr Length Bytes( + const ProfileBufferLiteralCStringPointer<CharactersIncludingTerminal>&) { + // We're only storing a pointer, its size is independent from the pointer + // value. + return sizeof(const char*); + } + + static void Write( + ProfileBufferEntryWriter& aEW, + const ProfileBufferLiteralCStringPointer<CharactersIncludingTerminal>& + aWrapper) { + // Write the pointer *value*, not the string contents. + aEW.WriteBytes(aWrapper.mCString, sizeof(aWrapper.mCString)); + } +}; + +// ---------------------------------------------------------------------------- +// C string contents + +// Wrapper around a pointer to a C string whose contents will be serialized. +struct ProfileBufferUnownedCString { + const char* mCString; +}; + +// Wrap a pointer to a C string whose contents will be serialized. +inline ProfileBufferUnownedCString WrapProfileBufferUnownedCString( + const char* aCString) { + return {aCString}; +} + +// The contents of a (probably) unowned C string are serialized as the number of +// characters (encoded as ULEB128) and all the characters in the string. The +// terminal '\0' is omitted. +// +// Usage: `aEW.WriteObject(WrapProfileBufferUnownedCString(str.c_str()))`. +// +// No deserializer is provided for this pointer type, instead it must be +// deserialized as one of the other string types that manages its contents, +// e.g.: `aER.ReadObject<std::string>();` +template <> +struct ProfileBufferEntryWriter::Serializer<ProfileBufferUnownedCString> { + static Length Bytes(const ProfileBufferUnownedCString& aS) { + const auto len = strlen(aS.mCString); + return ULEB128Size(len) + len; + } + + static void Write(ProfileBufferEntryWriter& aEW, + const ProfileBufferUnownedCString& aS) { + const auto len = strlen(aS.mCString); + aEW.WriteULEB128(len); + aEW.WriteBytes(aS.mCString, len); + } +}; + +// ---------------------------------------------------------------------------- +// Raw pointers + +// Wrapper around a pointer to be serialized as the raw pointer value. +template <typename T> +struct ProfileBufferRawPointer { + T* mRawPointer; +}; + +// Wrap a pointer to be serialized as the raw pointer value. +template <typename T> +ProfileBufferRawPointer<T> WrapProfileBufferRawPointer(T* aRawPointer) { + return {aRawPointer}; +} + +// Raw pointers are serialized as the raw pointer value. +// +// Usage: `aEW.WriteObject(WrapProfileBufferRawPointer(ptr));` +// +// The wrapper is compulsory when writing pointers (to avoid unexpected +// leaks/UAFs), but reading can be done straight into a raw pointer object, +// e.g.: `aER.ReadObject<Foo*>;`. +template <typename T> +struct ProfileBufferEntryWriter::Serializer<ProfileBufferRawPointer<T>> { + template <typename U> + static constexpr Length Bytes(const U&) { + return sizeof(T*); + } + + static void Write(ProfileBufferEntryWriter& aEW, + const ProfileBufferRawPointer<T>& aWrapper) { + aEW.WriteBytes(&aWrapper.mRawPointer, sizeof(aWrapper.mRawPointer)); + } +}; + +// Usage: `aER.ReadObject<Foo*>;` or `Foo* p; aER.ReadIntoObject(p);`, no +// wrapper necessary. +template <typename T> +struct ProfileBufferEntryReader::Deserializer<ProfileBufferRawPointer<T>> { + static void ReadInto(ProfileBufferEntryReader& aER, + ProfileBufferRawPointer<T>& aPtr) { + aER.ReadBytes(&aPtr.mRawPointer, sizeof(aPtr)); + } + + static ProfileBufferRawPointer<T> Read(ProfileBufferEntryReader& aER) { + ProfileBufferRawPointer<T> rawPointer; + ReadInto(aER, rawPointer); + return rawPointer; + } +}; + +// ---------------------------------------------------------------------------- +// std::string contents + +// std::string contents are serialized as the number of characters (encoded as +// ULEB128) and all the characters in the string. The terminal '\0' is omitted. +// +// Usage: `std::string s = ...; aEW.WriteObject(s);` +template <typename CHAR> +struct ProfileBufferEntryWriter::Serializer<std::basic_string<CHAR>> { + static Length Bytes(const std::basic_string<CHAR>& aS) { + const Length len = static_cast<Length>(aS.length()); + return ULEB128Size(len) + len; + } + + static void Write(ProfileBufferEntryWriter& aEW, + const std::basic_string<CHAR>& aS) { + const Length len = static_cast<Length>(aS.length()); + aEW.WriteULEB128(len); + aEW.WriteBytes(aS.c_str(), len * sizeof(CHAR)); + } +}; + +// Usage: `std::string s = aEW.ReadObject<std::string>(s);` or +// `std::string s; aER.ReadIntoObject(s);` +template <typename CHAR> +struct ProfileBufferEntryReader::Deserializer<std::basic_string<CHAR>> { + static void ReadCharsInto(ProfileBufferEntryReader& aER, + std::basic_string<CHAR>& aS, size_t aLength) { + // Assign to `aS` by using iterators. + // (`aER+0` so we get the same iterator type as `aER+len`.) + aS.assign(aER, aER.EmptyIteratorAtOffset(aLength)); + aER += aLength; + } + + static void ReadInto(ProfileBufferEntryReader& aER, + std::basic_string<CHAR>& aS) { + ReadCharsInto( + aER, aS, + aER.ReadULEB128<typename std::basic_string<CHAR>::size_type>()); + } + + static std::basic_string<CHAR> ReadChars(ProfileBufferEntryReader& aER, + size_t aLength) { + // Construct a string by using iterators. + // (`aER+0` so we get the same iterator type as `aER+len`.) + std::basic_string<CHAR> s(aER, aER.EmptyIteratorAtOffset(aLength)); + aER += aLength; + return s; + } + + static std::basic_string<CHAR> Read(ProfileBufferEntryReader& aER) { + return ReadChars( + aER, aER.ReadULEB128<typename std::basic_string<CHAR>::size_type>()); + } +}; + +// ---------------------------------------------------------------------------- +// mozilla::UniqueFreePtr<CHAR> + +// UniqueFreePtr<CHAR>, which points at a string allocated with `malloc` +// (typically generated by `strdup()`), is serialized as the number of +// *bytes* (encoded as ULEB128) and all the characters in the string. The +// null terminator is omitted. +// `CHAR` can be any type that has a specialization for +// `std::char_traits<CHAR>::length(const CHAR*)`. +// +// Note: A nullptr pointer will be serialized like an empty string, so when +// deserializing it will result in an allocated buffer only containing a +// single null terminator. +template <typename CHAR> +struct ProfileBufferEntryWriter::Serializer<UniqueFreePtr<CHAR>> { + static Length Bytes(const UniqueFreePtr<CHAR>& aS) { + if (!aS) { + // Null pointer, store it as if it was an empty string (so: 0 bytes). + return ULEB128Size(0u); + } + // Note that we store the size in *bytes*, not in number of characters. + const auto bytes = std::char_traits<CHAR>::length(aS.get()) * sizeof(CHAR); + return ULEB128Size(bytes) + bytes; + } + + static void Write(ProfileBufferEntryWriter& aEW, + const UniqueFreePtr<CHAR>& aS) { + if (!aS) { + // Null pointer, store it as if it was an empty string (so we write a + // length of 0 bytes). + aEW.WriteULEB128(0u); + return; + } + // Note that we store the size in *bytes*, not in number of characters. + const auto bytes = std::char_traits<CHAR>::length(aS.get()) * sizeof(CHAR); + aEW.WriteULEB128(bytes); + aEW.WriteBytes(aS.get(), bytes); + } +}; + +template <typename CHAR> +struct ProfileBufferEntryReader::Deserializer<UniqueFreePtr<CHAR>> { + static void ReadInto(ProfileBufferEntryReader& aER, UniqueFreePtr<CHAR>& aS) { + aS = Read(aER); + } + + static UniqueFreePtr<CHAR> Read(ProfileBufferEntryReader& aER) { + // Read the number of *bytes* that follow. + const auto bytes = aER.ReadULEB128<size_t>(); + // We need a buffer of the non-const character type. + using NC_CHAR = std::remove_const_t<CHAR>; + // We allocate the required number of bytes, plus one extra character for + // the null terminator. + NC_CHAR* buffer = static_cast<NC_CHAR*>(malloc(bytes + sizeof(NC_CHAR))); + // Copy the characters into the buffer. + aER.ReadBytes(buffer, bytes); + // And append a null terminator. + buffer[bytes / sizeof(NC_CHAR)] = NC_CHAR(0); + return UniqueFreePtr<CHAR>(buffer); + } +}; + +// ---------------------------------------------------------------------------- +// std::tuple + +// std::tuple is serialized as a sequence of each recursively-serialized item. +// +// This is equivalent to manually serializing each item, so reading/writing +// tuples is equivalent to reading/writing their elements in order, e.g.: +// ``` +// std::tuple<int, std::string> is = ...; +// aEW.WriteObject(is); // Write the tuple, equivalent to: +// aEW.WriteObject(/* int */ std::get<0>(is), /* string */ std::get<1>(is)); +// ... +// // Reading back can be done directly into a tuple: +// auto is = aER.ReadObject<std::tuple<int, std::string>>(); +// // Or each item could be read separately: +// auto i = aER.ReadObject<int>(); auto s = aER.ReadObject<std::string>(); +// ``` +template <typename... Ts> +struct ProfileBufferEntryWriter::Serializer<std::tuple<Ts...>> { + private: + template <size_t... Is> + static Length TupleBytes(const std::tuple<Ts...>& aTuple, + std::index_sequence<Is...>) { + return (0 + ... + SumBytes(std::get<Is>(aTuple))); + } + + template <size_t... Is> + static void TupleWrite(ProfileBufferEntryWriter& aEW, + const std::tuple<Ts...>& aTuple, + std::index_sequence<Is...>) { + (aEW.WriteObject(std::get<Is>(aTuple)), ...); + } + + public: + static Length Bytes(const std::tuple<Ts...>& aTuple) { + // Generate a 0..N-1 index pack, we'll add the sizes of each item. + return TupleBytes(aTuple, std::index_sequence_for<Ts...>()); + } + + static void Write(ProfileBufferEntryWriter& aEW, + const std::tuple<Ts...>& aTuple) { + // Generate a 0..N-1 index pack, we'll write each item. + TupleWrite(aEW, aTuple, std::index_sequence_for<Ts...>()); + } +}; + +template <typename... Ts> +struct ProfileBufferEntryReader::Deserializer<std::tuple<Ts...>> { + template <size_t I> + static void TupleIReadInto(ProfileBufferEntryReader& aER, + std::tuple<Ts...>& aTuple) { + aER.ReadIntoObject(std::get<I>(aTuple)); + } + + template <size_t... Is> + static void TupleReadInto(ProfileBufferEntryReader& aER, + std::tuple<Ts...>& aTuple, + std::index_sequence<Is...>) { + (TupleIReadInto<Is>(aER, aTuple), ...); + } + + static void ReadInto(ProfileBufferEntryReader& aER, + std::tuple<Ts...>& aTuple) { + TupleReadInto(aER, aTuple, std::index_sequence_for<Ts...>()); + } + + static std::tuple<Ts...> Read(ProfileBufferEntryReader& aER) { + // Note that this creates default `Ts` first, and then overwrites them. + std::tuple<Ts...> ob; + ReadInto(aER, ob); + return ob; + } +}; +// ---------------------------------------------------------------------------- +// mozilla::Span + +// Span. All elements are serialized in sequence. +// The caller is assumed to know the number of elements (they may manually +// write&read it before the span if needed). +// Similar to tuples, reading/writing spans is equivalent to reading/writing +// their elements in order. +template <class T, size_t N> +struct ProfileBufferEntryWriter::Serializer<Span<T, N>> { + static Length Bytes(const Span<T, N>& aSpan) { + Length bytes = 0; + for (const T& element : aSpan) { + bytes += SumBytes(element); + } + return bytes; + } + + static void Write(ProfileBufferEntryWriter& aEW, const Span<T, N>& aSpan) { + for (const T& element : aSpan) { + aEW.WriteObject(element); + } + } +}; + +template <class T, size_t N> +struct ProfileBufferEntryReader::Deserializer<Span<T, N>> { + // Read elements back into span pointing at a pre-allocated buffer. + static void ReadInto(ProfileBufferEntryReader& aER, Span<T, N>& aSpan) { + for (T& element : aSpan) { + aER.ReadIntoObject(element); + } + } + + // A Span does not own its data, this would probably leak so we forbid this. + static Span<T, N> Read(ProfileBufferEntryReader& aER) = delete; +}; + +// ---------------------------------------------------------------------------- +// mozilla::Maybe + +// Maybe<T> is serialized as one byte containing either 'm' (Nothing), +// or 'M' followed by the recursively-serialized `T` object. +template <typename T> +struct ProfileBufferEntryWriter::Serializer<Maybe<T>> { + static Length Bytes(const Maybe<T>& aMaybe) { + // 1 byte to store nothing/something flag, then object size if present. + return aMaybe.isNothing() ? 1 : (1 + SumBytes(aMaybe.ref())); + } + + static void Write(ProfileBufferEntryWriter& aEW, const Maybe<T>& aMaybe) { + // 'm'/'M' is just an arbitrary 1-byte value to distinguish states. + if (aMaybe.isNothing()) { + aEW.WriteObject<char>('m'); + } else { + aEW.WriteObject<char>('M'); + // Use the Serializer for the contained type. + aEW.WriteObject(aMaybe.ref()); + } + } +}; + +template <typename T> +struct ProfileBufferEntryReader::Deserializer<Maybe<T>> { + static void ReadInto(ProfileBufferEntryReader& aER, Maybe<T>& aMaybe) { + char c = aER.ReadObject<char>(); + if (c == 'm') { + aMaybe.reset(); + } else { + MOZ_ASSERT(c == 'M'); + // If aMaybe is empty, create a default `T` first, to be overwritten. + // Otherwise we'll just overwrite whatever was already there. + if (aMaybe.isNothing()) { + aMaybe.emplace(); + } + // Use the Deserializer for the contained type. + aER.ReadIntoObject(aMaybe.ref()); + } + } + + static Maybe<T> Read(ProfileBufferEntryReader& aER) { + Maybe<T> maybe; + char c = aER.ReadObject<char>(); + MOZ_ASSERT(c == 'M' || c == 'm'); + if (c == 'M') { + // Note that this creates a default `T` inside the Maybe first, and then + // overwrites it. + maybe = Some(T{}); + // Use the Deserializer for the contained type. + aER.ReadIntoObject(maybe.ref()); + } + return maybe; + } +}; + +// ---------------------------------------------------------------------------- +// mozilla::Variant + +// Variant is serialized as the tag (0-based index of the stored type, encoded +// as ULEB128), and the recursively-serialized object. +template <typename... Ts> +struct ProfileBufferEntryWriter::Serializer<Variant<Ts...>> { + public: + static Length Bytes(const Variant<Ts...>& aVariantTs) { + return aVariantTs.match([](auto aIndex, const auto& aAlternative) { + return ULEB128Size(aIndex) + SumBytes(aAlternative); + }); + } + + static void Write(ProfileBufferEntryWriter& aEW, + const Variant<Ts...>& aVariantTs) { + aVariantTs.match([&aEW](auto aIndex, const auto& aAlternative) { + aEW.WriteULEB128(aIndex); + aEW.WriteObject(aAlternative); + }); + } +}; + +template <typename... Ts> +struct ProfileBufferEntryReader::Deserializer<Variant<Ts...>> { + private: + // Called from the fold expression in `VariantReadInto()`, only the selected + // variant will deserialize the object. + template <size_t I> + static void VariantIReadInto(ProfileBufferEntryReader& aER, + Variant<Ts...>& aVariantTs, unsigned aTag) { + if (I == aTag) { + // Ensure the variant contains the target type. Note that this may create + // a default object. + if (!aVariantTs.template is<I>()) { + aVariantTs = Variant<Ts...>(VariantIndex<I>{}); + } + aER.ReadIntoObject(aVariantTs.template as<I>()); + } + } + + template <size_t... Is> + static void VariantReadInto(ProfileBufferEntryReader& aER, + Variant<Ts...>& aVariantTs, + std::index_sequence<Is...>) { + unsigned tag = aER.ReadULEB128<unsigned>(); + (VariantIReadInto<Is>(aER, aVariantTs, tag), ...); + } + + public: + static void ReadInto(ProfileBufferEntryReader& aER, + Variant<Ts...>& aVariantTs) { + // Generate a 0..N-1 index pack, the selected variant will deserialize + // itself. + VariantReadInto(aER, aVariantTs, std::index_sequence_for<Ts...>()); + } + + static Variant<Ts...> Read(ProfileBufferEntryReader& aER) { + // Note that this creates a default `Variant` of the first type, and then + // overwrites it. Consider using `ReadInto` for more control if needed. + Variant<Ts...> variant(VariantIndex<0>{}); + ReadInto(aER, variant); + return variant; + } +}; + +} // namespace mozilla + +#endif // ProfileBufferEntrySerialization_h diff --git a/mozglue/baseprofiler/public/ProfileBufferIndex.h b/mozglue/baseprofiler/public/ProfileBufferIndex.h new file mode 100644 index 0000000000..5cda6bd89e --- /dev/null +++ b/mozglue/baseprofiler/public/ProfileBufferIndex.h @@ -0,0 +1,97 @@ +/* -*- Mode: C++; tab-width: 2; 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/. */ + +#ifndef ProfileBufferIndex_h +#define ProfileBufferIndex_h + +#include "mozilla/Attributes.h" + +#include <cstddef> +#include <cstdint> + +namespace mozilla { + +// Generic index into a Profiler buffer, mostly for internal usage. +// Intended to appear infinite (it should effectively never wrap). +// 0 (zero) is reserved as nullptr-like value; it may indicate failure result, +// or it may point at the earliest available block. +using ProfileBufferIndex = uint64_t; + +// Externally-opaque class encapsulating a block index, i.e. a +// ProfileBufferIndex that is guaranteed to point at the start of a Profile +// buffer block (until it is destroyed, but then that index cannot be reused and +// functions should gracefully handle expired blocks). +// Users may get these from Profile buffer functions, to later access previous +// blocks; they should avoid converting and operating on their value. +class ProfileBufferBlockIndex { + public: + // Default constructor with internal 0 value, for which Profile buffers must + // guarantee that it is before any valid entries; All public APIs should + // fail gracefully, doing and/or returning Nothing. + ProfileBufferBlockIndex() : mBlockIndex(0) {} + + // Implicit conversion from literal `nullptr` to internal 0 value, to allow + // convenient init/reset/comparison with 0 index. + MOZ_IMPLICIT ProfileBufferBlockIndex(std::nullptr_t) : mBlockIndex(0) {} + + // Explicit conversion to bool, works in `if` and other tests. + // Only returns false for default `ProfileBufferBlockIndex{}` value. + explicit operator bool() const { return mBlockIndex != 0; } + + // Comparison operators. Default `ProfileBufferBlockIndex{}` value is always + // the lowest. + [[nodiscard]] bool operator==(const ProfileBufferBlockIndex& aRhs) const { + return mBlockIndex == aRhs.mBlockIndex; + } + [[nodiscard]] bool operator!=(const ProfileBufferBlockIndex& aRhs) const { + return mBlockIndex != aRhs.mBlockIndex; + } + [[nodiscard]] bool operator<(const ProfileBufferBlockIndex& aRhs) const { + return mBlockIndex < aRhs.mBlockIndex; + } + [[nodiscard]] bool operator<=(const ProfileBufferBlockIndex& aRhs) const { + return mBlockIndex <= aRhs.mBlockIndex; + } + [[nodiscard]] bool operator>(const ProfileBufferBlockIndex& aRhs) const { + return mBlockIndex > aRhs.mBlockIndex; + } + [[nodiscard]] bool operator>=(const ProfileBufferBlockIndex& aRhs) const { + return mBlockIndex >= aRhs.mBlockIndex; + } + + // Explicit conversion to ProfileBufferIndex, mostly used by internal Profile + // buffer code. + [[nodiscard]] ProfileBufferIndex ConvertToProfileBufferIndex() const { + return mBlockIndex; + } + + // Explicit creation from ProfileBufferIndex, mostly used by internal + // Profile buffer code. + [[nodiscard]] static ProfileBufferBlockIndex CreateFromProfileBufferIndex( + ProfileBufferIndex aIndex) { + return ProfileBufferBlockIndex(aIndex); + } + + private: + // Private to prevent easy construction from any value. Use + // `CreateFromProfileBufferIndex()` instead. + // The main reason for this indirection is to make it harder to create these + // objects, because only the profiler code should need to do it. Ideally, this + // class should be used wherever a block index should be stored, but there is + // so much code that uses `uint64_t` that it would be a big task to change + // them all. So for now we allow conversions to/from numbers, but it's as ugly + // as possible to make sure it doesn't get too common; and if one day we want + // to tackle a global change, it should be easy to find all these locations + // thanks to the explicit conversion functions. + explicit ProfileBufferBlockIndex(ProfileBufferIndex aBlockIndex) + : mBlockIndex(aBlockIndex) {} + + ProfileBufferIndex mBlockIndex; +}; + +} // namespace mozilla + +#endif // ProfileBufferIndex_h diff --git a/mozglue/baseprofiler/public/ProfileChunkedBuffer.h b/mozglue/baseprofiler/public/ProfileChunkedBuffer.h new file mode 100644 index 0000000000..ded7fbd08b --- /dev/null +++ b/mozglue/baseprofiler/public/ProfileChunkedBuffer.h @@ -0,0 +1,1560 @@ +/* -*- Mode: C++; tab-width: 2; 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/. */ + +#ifndef ProfileChunkedBuffer_h +#define ProfileChunkedBuffer_h + +#include "mozilla/Attributes.h" +#include "mozilla/BaseProfilerDetail.h" +#include "mozilla/NotNull.h" +#include "mozilla/ProfileBufferChunkManager.h" +#include "mozilla/ProfileBufferChunkManagerSingle.h" +#include "mozilla/ProfileBufferEntrySerialization.h" +#include "mozilla/ProfileChunkedBufferDetail.h" +#include "mozilla/RefPtr.h" +#include "mozilla/ScopeExit.h" +#include "mozilla/Unused.h" + +#include <utility> + +#ifdef DEBUG +# include <cstdio> +#endif + +namespace mozilla { + +// Thread-safe buffer that can store blocks of different sizes during defined +// sessions, using Chunks (from a ChunkManager) as storage. +// +// Each *block* contains an *entry* and the entry size: +// [ entry_size | entry ] [ entry_size | entry ] ... +// +// *In-session* is a period of time during which `ProfileChunkedBuffer` allows +// reading and writing. +// *Out-of-session*, the `ProfileChunkedBuffer` object is still valid, but +// contains no data, and gracefully denies accesses. +// +// To write an entry, the buffer reserves a block of sufficient size (to contain +// user data of predetermined size), writes the entry size, and lets the caller +// fill the entry contents using a ProfileBufferEntryWriter. E.g.: +// ``` +// ProfileChunkedBuffer cb(...); +// cb.ReserveAndPut([]() { return sizeof(123); }, +// [&](Maybe<ProfileBufferEntryWriter>& aEW) { +// if (aEW) { aEW->WriteObject(123); } +// }); +// ``` +// Other `Put...` functions may be used as shortcuts for simple entries. +// The objects given to the caller's callbacks should only be used inside the +// callbacks and not stored elsewhere, because they keep their own references to +// chunk memory and therefore should not live longer. +// Different type of objects may be serialized into an entry, see +// `ProfileBufferEntryWriter::Serializer` for more information. +// +// When reading data, the buffer iterates over blocks (it knows how to read the +// entry size, and therefore move to the next block), and lets the caller read +// the entry inside of each block. E.g.: +// ``` +// cb.ReadEach([](ProfileBufferEntryReader& aER) { +// /* Use ProfileBufferEntryReader functions to read serialized objects. */ +// int n = aER.ReadObject<int>(); +// }); +// ``` +// Different type of objects may be deserialized from an entry, see +// `ProfileBufferEntryReader::Deserializer` for more information. +// +// Writers may retrieve the block index corresponding to an entry +// (`ProfileBufferBlockIndex` is an opaque type preventing the user from easily +// modifying it). That index may later be used with `ReadAt` to get back to the +// entry in that particular block -- if it still exists. +class ProfileChunkedBuffer { + public: + using Byte = ProfileBufferChunk::Byte; + using Length = ProfileBufferChunk::Length; + + enum class ThreadSafety { WithoutMutex, WithMutex }; + + // Default constructor starts out-of-session (nothing to read or write). + explicit ProfileChunkedBuffer(ThreadSafety aThreadSafety) + : mMutex(aThreadSafety != ThreadSafety::WithoutMutex) {} + + // Start in-session with external chunk manager. + ProfileChunkedBuffer(ThreadSafety aThreadSafety, + ProfileBufferChunkManager& aChunkManager) + : mMutex(aThreadSafety != ThreadSafety::WithoutMutex) { + SetChunkManager(aChunkManager); + } + + // Start in-session with owned chunk manager. + ProfileChunkedBuffer(ThreadSafety aThreadSafety, + UniquePtr<ProfileBufferChunkManager>&& aChunkManager) + : mMutex(aThreadSafety != ThreadSafety::WithoutMutex) { + SetChunkManager(std::move(aChunkManager)); + } + + ~ProfileChunkedBuffer() { + // Do proper clean-up by resetting the chunk manager. + ResetChunkManager(); + } + + // This cannot change during the lifetime of this buffer, so there's no need + // to lock. + [[nodiscard]] bool IsThreadSafe() const { return mMutex.IsActivated(); } + + [[nodiscard]] bool IsInSession() const { + baseprofiler::detail::BaseProfilerMaybeAutoLock lock(mMutex); + return !!mChunkManager; + } + + // Stop using the current chunk manager. + // If we own the current chunk manager, it will be destroyed. + // This will always clear currently-held chunks, if any. + void ResetChunkManager() { + baseprofiler::detail::BaseProfilerMaybeAutoLock lock(mMutex); + Unused << ResetChunkManager(lock); + } + + // Set the current chunk manager. + // The caller is responsible for keeping the chunk manager alive as along as + // it's used here (until the next (Re)SetChunkManager, or + // ~ProfileChunkedBuffer). + void SetChunkManager(ProfileBufferChunkManager& aChunkManager) { + baseprofiler::detail::BaseProfilerMaybeAutoLock lock(mMutex); + Unused << ResetChunkManager(lock); + SetChunkManager(aChunkManager, lock); + } + + // Set the current chunk manager, and keep ownership of it. + void SetChunkManager(UniquePtr<ProfileBufferChunkManager>&& aChunkManager) { + baseprofiler::detail::BaseProfilerMaybeAutoLock lock(mMutex); + Unused << ResetChunkManager(lock); + mOwnedChunkManager = std::move(aChunkManager); + if (mOwnedChunkManager) { + SetChunkManager(*mOwnedChunkManager, lock); + } + } + + // Set the current chunk manager, except if it's already the one provided. + // The caller is responsible for keeping the chunk manager alive as along as + // it's used here (until the next (Re)SetChunkManager, or + // ~ProfileChunkedBuffer). + void SetChunkManagerIfDifferent(ProfileBufferChunkManager& aChunkManager) { + baseprofiler::detail::BaseProfilerMaybeAutoLock lock(mMutex); + if (!mChunkManager || mChunkManager != &aChunkManager) { + Unused << ResetChunkManager(lock); + SetChunkManager(aChunkManager, lock); + } + } + + // Clear the contents of this buffer, ready to receive new chunks. + // Note that memory is not freed: No chunks are destroyed, they are all + // receycled. + // Also the range doesn't reset, instead it continues at some point after the + // previous range. This may be useful if the caller may be keeping indexes + // into old chunks that have now been cleared, using these indexes will fail + // gracefully (instead of potentially pointing into new data). + void Clear() { + baseprofiler::detail::BaseProfilerMaybeAutoLock lock(mMutex); + if (MOZ_UNLIKELY(!mChunkManager)) { + // Out-of-session. + return; + } + + mRangeStart = mRangeEnd = mNextChunkRangeStart; + mPushedBlockCount = 0; + mClearedBlockCount = 0; + mFailedPutBytes = 0; + + // Recycle all released chunks as "next" chunks. This will reduce the number + // of future allocations. Also, when using ProfileBufferChunkManagerSingle, + // this retrieves the one chunk if it was released. + UniquePtr<ProfileBufferChunk> releasedChunks = + mChunkManager->GetExtantReleasedChunks(); + if (releasedChunks) { + // Released chunks should be in the "Done" state, they need to be marked + // "recycled" before they can be reused. + for (ProfileBufferChunk* chunk = releasedChunks.get(); chunk; + chunk = chunk->GetNext()) { + chunk->MarkRecycled(); + } + mNextChunks = ProfileBufferChunk::Join(std::move(mNextChunks), + std::move(releasedChunks)); + } + + if (mCurrentChunk) { + // We already have a current chunk (empty or in-use), mark it "done" and + // then "recycled", ready to be reused. + mCurrentChunk->MarkDone(); + mCurrentChunk->MarkRecycled(); + } else { + if (!mNextChunks) { + // No current chunk, and no next chunks to recycle, nothing more to do. + // The next "Put" operation will try to allocate a chunk as needed. + return; + } + + // No current chunk, take a next chunk. + mCurrentChunk = std::exchange(mNextChunks, mNextChunks->ReleaseNext()); + } + + // Here, there was already a current chunk, or one has just been taken. + // Make sure it's ready to receive new entries. + InitializeCurrentChunk(lock); + } + + // Buffer maximum length in bytes. + Maybe<size_t> BufferLength() const { + baseprofiler::detail::BaseProfilerMaybeAutoLock lock(mMutex); + if (!mChunkManager) { + return Nothing{}; + } + return Some(mChunkManager->MaxTotalSize()); + } + + [[nodiscard]] size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const { + baseprofiler::detail::BaseProfilerMaybeAutoLock lock(mMutex); + return SizeOfExcludingThis(aMallocSizeOf, lock); + } + + [[nodiscard]] size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const { + baseprofiler::detail::BaseProfilerMaybeAutoLock lock(mMutex); + return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf, lock); + } + + // Snapshot of the buffer state. + struct State { + // Index to/before the first block. + ProfileBufferIndex mRangeStart = 1; + + // Index past the last block. Equals mRangeStart if empty. + ProfileBufferIndex mRangeEnd = 1; + + // Number of blocks that have been pushed into this buffer. + uint64_t mPushedBlockCount = 0; + + // Number of blocks that have been removed from this buffer. + // Note: Live entries = pushed - cleared. + uint64_t mClearedBlockCount = 0; + + // Number of bytes that could not be put into this buffer. + uint64_t mFailedPutBytes = 0; + }; + + // Get a snapshot of the current state. + // When out-of-session, mFirstReadIndex==mNextWriteIndex, and + // mPushedBlockCount==mClearedBlockCount==0. + // Note that these may change right after this thread-safe call, so they + // should only be used for statistical purposes. + [[nodiscard]] State GetState() const { + baseprofiler::detail::BaseProfilerMaybeAutoLock lock(mMutex); + return {mRangeStart, mRangeEnd, mPushedBlockCount, mClearedBlockCount, + mFailedPutBytes}; + } + + // In in-session, return the start TimeStamp of the earliest chunk. + // If out-of-session, return a null TimeStamp. + [[nodiscard]] TimeStamp GetEarliestChunkStartTimeStamp() const { + baseprofiler::detail::BaseProfilerMaybeAutoLock lock(mMutex); + if (MOZ_UNLIKELY(!mChunkManager)) { + // Out-of-session. + return {}; + } + return mChunkManager->PeekExtantReleasedChunks( + [&](const ProfileBufferChunk* aOldestChunk) -> TimeStamp { + if (aOldestChunk) { + return aOldestChunk->ChunkHeader().mStartTimeStamp; + } + if (mCurrentChunk) { + return mCurrentChunk->ChunkHeader().mStartTimeStamp; + } + return {}; + }); + } + + [[nodiscard]] bool IsEmpty() const { + baseprofiler::detail::BaseProfilerMaybeAutoLock lock(mMutex); + return mRangeStart == mRangeEnd; + } + + // True if this buffer is already locked on this thread. + // This should be used if some functions may call an already-locked buffer, + // e.g.: Put -> memory hook -> profiler_add_native_allocation_marker -> Put. + [[nodiscard]] bool IsThreadSafeAndLockedOnCurrentThread() const { + return mMutex.IsActivatedAndLockedOnCurrentThread(); + } + + // Lock the buffer mutex and run the provided callback. + // This can be useful when the caller needs to explicitly lock down this + // buffer, but not do anything else with it. + template <typename Callback> + auto LockAndRun(Callback&& aCallback) const { + baseprofiler::detail::BaseProfilerMaybeAutoLock lock(mMutex); + return std::forward<Callback>(aCallback)(); + } + + // Reserve a block that can hold an entry of the given `aCallbackEntryBytes()` + // size, write the entry size (ULEB128-encoded), and invoke and return + // `aCallback(Maybe<ProfileBufferEntryWriter>&)`. + // Note: `aCallbackEntryBytes` is a callback instead of a simple value, to + // delay this potentially-expensive computation until after we're checked that + // we're in-session; use `Put(Length, Callback)` below if you know the size + // already. + template <typename CallbackEntryBytes, typename Callback> + auto ReserveAndPut(CallbackEntryBytes&& aCallbackEntryBytes, + Callback&& aCallback) + -> decltype(std::forward<Callback>(aCallback)( + std::declval<Maybe<ProfileBufferEntryWriter>&>())) { + baseprofiler::detail::BaseProfilerMaybeAutoLock lock(mMutex); + + // This can only be read in the 2nd lambda below after it has been written + // by the first lambda. + Length entryBytes; + + return ReserveAndPutRaw( + [&]() { + entryBytes = std::forward<CallbackEntryBytes>(aCallbackEntryBytes)(); + MOZ_ASSERT(entryBytes != 0, "Empty entries are not allowed"); + return ULEB128Size(entryBytes) + entryBytes; + }, + [&](Maybe<ProfileBufferEntryWriter>& aMaybeEntryWriter) { + if (aMaybeEntryWriter.isSome()) { + aMaybeEntryWriter->WriteULEB128(entryBytes); + MOZ_ASSERT(aMaybeEntryWriter->RemainingBytes() == entryBytes); + } + return std::forward<Callback>(aCallback)(aMaybeEntryWriter); + }, + lock); + } + + template <typename Callback> + auto Put(Length aEntryBytes, Callback&& aCallback) { + return ReserveAndPut([aEntryBytes]() { return aEntryBytes; }, + std::forward<Callback>(aCallback)); + } + + // Add a new entry copied from the given buffer, return block index. + ProfileBufferBlockIndex PutFrom(const void* aSrc, Length aBytes) { + return ReserveAndPut( + [aBytes]() { return aBytes; }, + [aSrc, aBytes](Maybe<ProfileBufferEntryWriter>& aMaybeEntryWriter) { + if (aMaybeEntryWriter.isNothing()) { + return ProfileBufferBlockIndex{}; + } + aMaybeEntryWriter->WriteBytes(aSrc, aBytes); + return aMaybeEntryWriter->CurrentBlockIndex(); + }); + } + + // Add a new single entry with *all* given object (using a Serializer for + // each), return block index. + template <typename... Ts> + ProfileBufferBlockIndex PutObjects(const Ts&... aTs) { + static_assert(sizeof...(Ts) > 0, + "PutObjects must be given at least one object."); + return ReserveAndPut( + [&]() { return ProfileBufferEntryWriter::SumBytes(aTs...); }, + [&](Maybe<ProfileBufferEntryWriter>& aMaybeEntryWriter) { + if (aMaybeEntryWriter.isNothing()) { + return ProfileBufferBlockIndex{}; + } + aMaybeEntryWriter->WriteObjects(aTs...); + return aMaybeEntryWriter->CurrentBlockIndex(); + }); + } + + // Add a new entry copied from the given object, return block index. + template <typename T> + ProfileBufferBlockIndex PutObject(const T& aOb) { + return PutObjects(aOb); + } + + // Get *all* chunks related to this buffer, including extant chunks in its + // ChunkManager, and yet-unused new/recycled chunks. + // We don't expect this buffer to be used again, though it's still possible + // and will allocate the first buffer when needed. + [[nodiscard]] UniquePtr<ProfileBufferChunk> GetAllChunks() { + baseprofiler::detail::BaseProfilerMaybeAutoLock lock(mMutex); + if (MOZ_UNLIKELY(!mChunkManager)) { + // Out-of-session. + return nullptr; + } + UniquePtr<ProfileBufferChunk> chunks = + mChunkManager->GetExtantReleasedChunks(); + Unused << HandleRequestedChunk_IsPending(lock); + if (MOZ_LIKELY(!!mCurrentChunk)) { + mCurrentChunk->MarkDone(); + chunks = + ProfileBufferChunk::Join(std::move(chunks), std::move(mCurrentChunk)); + } + chunks = + ProfileBufferChunk::Join(std::move(chunks), std::move(mNextChunks)); + mChunkManager->ForgetUnreleasedChunks(); + mRangeStart = mRangeEnd = mNextChunkRangeStart; + return chunks; + } + + // True if the given index points inside the current chunk (up to the last + // written byte). + // This could be used to check if an index written now would have a good + // chance of referring to a previous block that has not been destroyed yet. + // But use with extreme care: This information may become incorrect right + // after this function returns, because new writes could start a new chunk. + [[nodiscard]] bool IsIndexInCurrentChunk(ProfileBufferIndex aIndex) const { + baseprofiler::detail::BaseProfilerMaybeAutoLock lock(mMutex); + if (MOZ_UNLIKELY(!mChunkManager || !mCurrentChunk)) { + // Out-of-session, or no current chunk. + return false; + } + return (mCurrentChunk->RangeStart() <= aIndex) && + (aIndex < (mCurrentChunk->RangeStart() + + mCurrentChunk->OffsetPastLastBlock())); + } + + class Reader; + + // Class that can iterate through blocks and provide + // `ProfileBufferEntryReader`s. + // Created through `Reader`, lives within a lock guard lifetime. + class BlockIterator { + public: +#ifdef DEBUG + ~BlockIterator() { + // No BlockIterator should live outside of a mutexed call. + mBuffer->mMutex.AssertCurrentThreadOwns(); + } +#endif // DEBUG + + // Comparison with other iterator, mostly used in range-for loops. + [[nodiscard]] bool operator==(const BlockIterator& aRhs) const { + MOZ_ASSERT(mBuffer == aRhs.mBuffer); + return mCurrentBlockIndex == aRhs.mCurrentBlockIndex; + } + [[nodiscard]] bool operator!=(const BlockIterator& aRhs) const { + MOZ_ASSERT(mBuffer == aRhs.mBuffer); + return mCurrentBlockIndex != aRhs.mCurrentBlockIndex; + } + + // Advance to next BlockIterator. + BlockIterator& operator++() { + mBuffer->mMutex.AssertCurrentThreadOwns(); + mCurrentBlockIndex = + ProfileBufferBlockIndex::CreateFromProfileBufferIndex( + mNextBlockPointer.GlobalRangePosition()); + mCurrentEntry = + mNextBlockPointer.EntryReader(mNextBlockPointer.ReadEntrySize()); + return *this; + } + + // Dereferencing creates a `ProfileBufferEntryReader` object for the entry + // inside this block. + // (Note: It would be possible to return a `const + // ProfileBufferEntryReader&`, but not useful in practice, because in most + // case the user will want to read, which is non-const.) + [[nodiscard]] ProfileBufferEntryReader operator*() const { + return mCurrentEntry; + } + + // True if this iterator is just past the last entry. + [[nodiscard]] bool IsAtEnd() const { + return mCurrentEntry.RemainingBytes() == 0; + } + + // Can be used as reference to come back to this entry with `GetEntryAt()`. + [[nodiscard]] ProfileBufferBlockIndex CurrentBlockIndex() const { + return mCurrentBlockIndex; + } + + // Index past the end of this block, which is the start of the next block. + [[nodiscard]] ProfileBufferBlockIndex NextBlockIndex() const { + MOZ_ASSERT(!IsAtEnd()); + return ProfileBufferBlockIndex::CreateFromProfileBufferIndex( + mNextBlockPointer.GlobalRangePosition()); + } + + // Index of the first block in the whole buffer. + [[nodiscard]] ProfileBufferBlockIndex BufferRangeStart() const { + mBuffer->mMutex.AssertCurrentThreadOwns(); + return ProfileBufferBlockIndex::CreateFromProfileBufferIndex( + mBuffer->mRangeStart); + } + + // Index past the last block in the whole buffer. + [[nodiscard]] ProfileBufferBlockIndex BufferRangeEnd() const { + mBuffer->mMutex.AssertCurrentThreadOwns(); + return ProfileBufferBlockIndex::CreateFromProfileBufferIndex( + mBuffer->mRangeEnd); + } + + private: + // Only a Reader can instantiate a BlockIterator. + friend class Reader; + + BlockIterator(const ProfileChunkedBuffer& aBuffer, + const ProfileBufferChunk* aChunks0, + const ProfileBufferChunk* aChunks1, + ProfileBufferBlockIndex aBlockIndex) + : mNextBlockPointer(aChunks0, aChunks1, aBlockIndex), + mCurrentBlockIndex( + ProfileBufferBlockIndex::CreateFromProfileBufferIndex( + mNextBlockPointer.GlobalRangePosition())), + mCurrentEntry( + mNextBlockPointer.EntryReader(mNextBlockPointer.ReadEntrySize())), + mBuffer(WrapNotNull(&aBuffer)) { + // No BlockIterator should live outside of a mutexed call. + mBuffer->mMutex.AssertCurrentThreadOwns(); + } + + profiler::detail::InChunkPointer mNextBlockPointer; + + ProfileBufferBlockIndex mCurrentBlockIndex; + + ProfileBufferEntryReader mCurrentEntry; + + // Using a non-null pointer instead of a reference, to allow copying. + // This BlockIterator should only live inside one of the thread-safe + // ProfileChunkedBuffer functions, for this reference to stay valid. + NotNull<const ProfileChunkedBuffer*> mBuffer; + }; + + // Class that can create `BlockIterator`s (e.g., for range-for), or just + // iterate through entries; lives within a lock guard lifetime. + class MOZ_RAII Reader { + public: + Reader(const Reader&) = delete; + Reader& operator=(const Reader&) = delete; + Reader(Reader&&) = delete; + Reader& operator=(Reader&&) = delete; + +#ifdef DEBUG + ~Reader() { + // No Reader should live outside of a mutexed call. + mBuffer.mMutex.AssertCurrentThreadOwns(); + } +#endif // DEBUG + + // Index of the first block in the whole buffer. + [[nodiscard]] ProfileBufferBlockIndex BufferRangeStart() const { + mBuffer.mMutex.AssertCurrentThreadOwns(); + return ProfileBufferBlockIndex::CreateFromProfileBufferIndex( + mBuffer.mRangeStart); + } + + // Index past the last block in the whole buffer. + [[nodiscard]] ProfileBufferBlockIndex BufferRangeEnd() const { + mBuffer.mMutex.AssertCurrentThreadOwns(); + return ProfileBufferBlockIndex::CreateFromProfileBufferIndex( + mBuffer.mRangeEnd); + } + + // Iterators to the first and past-the-last blocks. + // Compatible with range-for (see `ForEach` below as example). + [[nodiscard]] BlockIterator begin() const { + return BlockIterator(mBuffer, mChunks0, mChunks1, nullptr); + } + // Note that a `BlockIterator` at the `end()` should not be dereferenced, as + // there is no actual block there! + [[nodiscard]] BlockIterator end() const { + return BlockIterator(mBuffer, nullptr, nullptr, nullptr); + } + + // Get a `BlockIterator` at the given `ProfileBufferBlockIndex`, clamped to + // the stored range. Note that a `BlockIterator` at the `end()` should not + // be dereferenced, as there is no actual block there! + [[nodiscard]] BlockIterator At(ProfileBufferBlockIndex aBlockIndex) const { + if (aBlockIndex < BufferRangeStart()) { + // Anything before the range (including null ProfileBufferBlockIndex) is + // clamped at the beginning. + return begin(); + } + // Otherwise we at least expect the index to be valid (pointing exactly at + // a live block, or just past the end.) + return BlockIterator(mBuffer, mChunks0, mChunks1, aBlockIndex); + } + + // Run `aCallback(ProfileBufferEntryReader&)` on each entry from first to + // last. Callback should not store `ProfileBufferEntryReader`, as it may + // become invalid after this thread-safe call. + template <typename Callback> + void ForEach(Callback&& aCallback) const { + for (ProfileBufferEntryReader reader : *this) { + aCallback(reader); + } + } + + // If this reader only points at one chunk with some data, this data will be + // exposed as a single entry. + [[nodiscard]] ProfileBufferEntryReader SingleChunkDataAsEntry() { + const ProfileBufferChunk* onlyNonEmptyChunk = nullptr; + for (const ProfileBufferChunk* chunkList : {mChunks0, mChunks1}) { + for (const ProfileBufferChunk* chunk = chunkList; chunk; + chunk = chunk->GetNext()) { + if (chunk->OffsetFirstBlock() != chunk->OffsetPastLastBlock()) { + if (onlyNonEmptyChunk) { + // More than one non-empty chunk. + return ProfileBufferEntryReader(); + } + onlyNonEmptyChunk = chunk; + } + } + } + if (!onlyNonEmptyChunk) { + // No non-empty chunks. + return ProfileBufferEntryReader(); + } + // Here, we have found one chunk that had some data. + return ProfileBufferEntryReader( + onlyNonEmptyChunk->BufferSpan().FromTo( + onlyNonEmptyChunk->OffsetFirstBlock(), + onlyNonEmptyChunk->OffsetPastLastBlock()), + ProfileBufferBlockIndex::CreateFromProfileBufferIndex( + onlyNonEmptyChunk->RangeStart()), + ProfileBufferBlockIndex::CreateFromProfileBufferIndex( + onlyNonEmptyChunk->RangeStart() + + (onlyNonEmptyChunk->OffsetPastLastBlock() - + onlyNonEmptyChunk->OffsetFirstBlock()))); + } + + private: + friend class ProfileChunkedBuffer; + + explicit Reader(const ProfileChunkedBuffer& aBuffer, + const ProfileBufferChunk* aChunks0, + const ProfileBufferChunk* aChunks1) + : mBuffer(aBuffer), mChunks0(aChunks0), mChunks1(aChunks1) { + // No Reader should live outside of a mutexed call. + mBuffer.mMutex.AssertCurrentThreadOwns(); + } + + // This Reader should only live inside one of the thread-safe + // ProfileChunkedBuffer functions, for this reference to stay valid. + const ProfileChunkedBuffer& mBuffer; + const ProfileBufferChunk* mChunks0; + const ProfileBufferChunk* mChunks1; + }; + + // In in-session, call `aCallback(ProfileChunkedBuffer::Reader&)` and return + // true. Callback should not store `Reader`, because it may become invalid + // after this call. + // If out-of-session, return false (callback is not invoked). + template <typename Callback> + [[nodiscard]] auto Read(Callback&& aCallback) const { + baseprofiler::detail::BaseProfilerMaybeAutoLock lock(mMutex); + if (MOZ_UNLIKELY(!mChunkManager)) { + // Out-of-session. + return std::forward<Callback>(aCallback)(static_cast<Reader*>(nullptr)); + } + return mChunkManager->PeekExtantReleasedChunks( + [&](const ProfileBufferChunk* aOldestChunk) { + Reader reader(*this, aOldestChunk, mCurrentChunk.get()); + return std::forward<Callback>(aCallback)(&reader); + }); + } + + // Invoke `aCallback(ProfileBufferEntryReader& [, ProfileBufferBlockIndex])` + // on each entry, it must read or at least skip everything. Either/both chunk + // pointers may be null. + template <typename Callback> + static void ReadEach(const ProfileBufferChunk* aChunks0, + const ProfileBufferChunk* aChunks1, + Callback&& aCallback) { + static_assert(std::is_invocable_v<Callback, ProfileBufferEntryReader&> || + std::is_invocable_v<Callback, ProfileBufferEntryReader&, + ProfileBufferBlockIndex>, + "ReadEach callback must take ProfileBufferEntryReader& and " + "optionally a ProfileBufferBlockIndex"); + profiler::detail::InChunkPointer p{aChunks0, aChunks1}; + while (!p.IsNull()) { + // The position right before an entry size *is* a block index. + const ProfileBufferBlockIndex blockIndex = + ProfileBufferBlockIndex::CreateFromProfileBufferIndex( + p.GlobalRangePosition()); + Length entrySize = p.ReadEntrySize(); + if (entrySize == 0) { + return; + } + ProfileBufferEntryReader entryReader = p.EntryReader(entrySize); + if (entryReader.RemainingBytes() == 0) { + return; + } + MOZ_ASSERT(entryReader.RemainingBytes() == entrySize); + if constexpr (std::is_invocable_v<Callback, ProfileBufferEntryReader&, + ProfileBufferBlockIndex>) { + aCallback(entryReader, blockIndex); + } else { + Unused << blockIndex; + aCallback(entryReader); + } + MOZ_ASSERT(entryReader.RemainingBytes() == 0); + } + } + + // Invoke `aCallback(ProfileBufferEntryReader& [, ProfileBufferBlockIndex])` + // on each entry, it must read or at least skip everything. + template <typename Callback> + void ReadEach(Callback&& aCallback) const { + baseprofiler::detail::BaseProfilerMaybeAutoLock lock(mMutex); + if (MOZ_UNLIKELY(!mChunkManager)) { + // Out-of-session. + return; + } + mChunkManager->PeekExtantReleasedChunks( + [&](const ProfileBufferChunk* aOldestChunk) { + ReadEach(aOldestChunk, mCurrentChunk.get(), + std::forward<Callback>(aCallback)); + }); + } + + // Call `aCallback(Maybe<ProfileBufferEntryReader>&&)` on the entry at + // the given ProfileBufferBlockIndex; The `Maybe` will be `Nothing` if + // out-of-session, or if that entry doesn't exist anymore, or if we've reached + // just past the last entry. Return whatever `aCallback` returns. Callback + // should not store `ProfileBufferEntryReader`, because it may become invalid + // after this call. + // Either/both chunk pointers may be null. + template <typename Callback> + [[nodiscard]] static auto ReadAt(ProfileBufferBlockIndex aMinimumBlockIndex, + const ProfileBufferChunk* aChunks0, + const ProfileBufferChunk* aChunks1, + Callback&& aCallback) { + static_assert( + std::is_invocable_v<Callback, Maybe<ProfileBufferEntryReader>&&>, + "ReadAt callback must take a Maybe<ProfileBufferEntryReader>&&"); + Maybe<ProfileBufferEntryReader> maybeEntryReader; + if (profiler::detail::InChunkPointer p{aChunks0, aChunks1}; !p.IsNull()) { + // If the pointer position is before the given position, try to advance. + if (p.GlobalRangePosition() >= + aMinimumBlockIndex.ConvertToProfileBufferIndex() || + p.AdvanceToGlobalRangePosition( + aMinimumBlockIndex.ConvertToProfileBufferIndex())) { + MOZ_ASSERT(p.GlobalRangePosition() >= + aMinimumBlockIndex.ConvertToProfileBufferIndex()); + // Here we're pointing at the start of a block, try to read the entry + // size. (Entries cannot be empty, so 0 means failure.) + if (Length entrySize = p.ReadEntrySize(); entrySize != 0) { + maybeEntryReader.emplace(p.EntryReader(entrySize)); + if (maybeEntryReader->RemainingBytes() == 0) { + // An empty entry reader means there was no complete block at the + // given index. + maybeEntryReader.reset(); + } else { + MOZ_ASSERT(maybeEntryReader->RemainingBytes() == entrySize); + } + } + } + } +#ifdef DEBUG + auto assertAllRead = MakeScopeExit([&]() { + MOZ_ASSERT(!maybeEntryReader || maybeEntryReader->RemainingBytes() == 0); + }); +#endif // DEBUG + return std::forward<Callback>(aCallback)(std::move(maybeEntryReader)); + } + + // Call `aCallback(Maybe<ProfileBufferEntryReader>&&)` on the entry at + // the given ProfileBufferBlockIndex; The `Maybe` will be `Nothing` if + // out-of-session, or if that entry doesn't exist anymore, or if we've reached + // just past the last entry. Return whatever `aCallback` returns. Callback + // should not store `ProfileBufferEntryReader`, because it may become invalid + // after this call. + template <typename Callback> + [[nodiscard]] auto ReadAt(ProfileBufferBlockIndex aBlockIndex, + Callback&& aCallback) const { + baseprofiler::detail::BaseProfilerMaybeAutoLock lock(mMutex); + if (MOZ_UNLIKELY(!mChunkManager)) { + // Out-of-session. + return std::forward<Callback>(aCallback)(Nothing{}); + } + return mChunkManager->PeekExtantReleasedChunks( + [&](const ProfileBufferChunk* aOldestChunk) { + return ReadAt(aBlockIndex, aOldestChunk, mCurrentChunk.get(), + std::forward<Callback>(aCallback)); + }); + } + + // Append the contents of another ProfileChunkedBuffer to this one. + ProfileBufferBlockIndex AppendContents(const ProfileChunkedBuffer& aSrc) { + ProfileBufferBlockIndex firstBlockIndex; + // If we start failing, we'll stop writing. + bool failed = false; + aSrc.ReadEach([&](ProfileBufferEntryReader& aER) { + if (failed) { + return; + } + failed = + !Put(aER.RemainingBytes(), [&](Maybe<ProfileBufferEntryWriter>& aEW) { + if (aEW.isNothing()) { + return false; + } + if (!firstBlockIndex) { + firstBlockIndex = aEW->CurrentBlockIndex(); + } + aEW->WriteFromReader(aER, aER.RemainingBytes()); + return true; + }); + }); + return failed ? nullptr : firstBlockIndex; + } + +#ifdef DEBUG + void Dump(std::FILE* aFile = stdout) const { + baseprofiler::detail::BaseProfilerMaybeAutoLock lock(mMutex); + fprintf(aFile, + "ProfileChunkedBuffer[%p] State: range %u-%u pushed=%u cleared=%u " + "(live=%u) failed-puts=%u bytes", + this, unsigned(mRangeStart), unsigned(mRangeEnd), + unsigned(mPushedBlockCount), unsigned(mClearedBlockCount), + unsigned(mPushedBlockCount) - unsigned(mClearedBlockCount), + unsigned(mFailedPutBytes)); + if (MOZ_UNLIKELY(!mChunkManager)) { + fprintf(aFile, " - Out-of-session\n"); + return; + } + fprintf(aFile, " - chunks:\n"); + bool hasChunks = false; + mChunkManager->PeekExtantReleasedChunks( + [&](const ProfileBufferChunk* aOldestChunk) { + for (const ProfileBufferChunk* chunk = aOldestChunk; chunk; + chunk = chunk->GetNext()) { + fprintf(aFile, "R "); + chunk->Dump(aFile); + hasChunks = true; + } + }); + if (mCurrentChunk) { + fprintf(aFile, "C "); + mCurrentChunk->Dump(aFile); + hasChunks = true; + } + for (const ProfileBufferChunk* chunk = mNextChunks.get(); chunk; + chunk = chunk->GetNext()) { + fprintf(aFile, "N "); + chunk->Dump(aFile); + hasChunks = true; + } + switch (mRequestedChunkHolder->GetState()) { + case RequestedChunkRefCountedHolder::State::Unused: + fprintf(aFile, " - No request pending.\n"); + break; + case RequestedChunkRefCountedHolder::State::Requested: + fprintf(aFile, " - Request pending.\n"); + break; + case RequestedChunkRefCountedHolder::State::Fulfilled: + fprintf(aFile, " - Request fulfilled.\n"); + break; + } + if (!hasChunks) { + fprintf(aFile, " No chunks.\n"); + } + } +#endif // DEBUG + + private: + // Used to de/serialize a ProfileChunkedBuffer (e.g., containing a backtrace). + friend ProfileBufferEntryWriter::Serializer<ProfileChunkedBuffer>; + friend ProfileBufferEntryReader::Deserializer<ProfileChunkedBuffer>; + friend ProfileBufferEntryWriter::Serializer<UniquePtr<ProfileChunkedBuffer>>; + friend ProfileBufferEntryReader::Deserializer< + UniquePtr<ProfileChunkedBuffer>>; + + [[nodiscard]] UniquePtr<ProfileBufferChunkManager> ResetChunkManager( + const baseprofiler::detail::BaseProfilerMaybeAutoLock&) { + UniquePtr<ProfileBufferChunkManager> chunkManager; + if (mChunkManager) { + mRequestedChunkHolder = nullptr; + mChunkManager->ForgetUnreleasedChunks(); +#ifdef DEBUG + mChunkManager->DeregisteredFrom(this); +#endif + mChunkManager = nullptr; + chunkManager = std::move(mOwnedChunkManager); + if (mCurrentChunk) { + mCurrentChunk->MarkDone(); + mCurrentChunk = nullptr; + } + mNextChunks = nullptr; + mNextChunkRangeStart = mRangeEnd; + mRangeStart = mRangeEnd; + mPushedBlockCount = 0; + mClearedBlockCount = 0; + mFailedPutBytes = 0; + } + return chunkManager; + } + + void SetChunkManager( + ProfileBufferChunkManager& aChunkManager, + const baseprofiler::detail::BaseProfilerMaybeAutoLock& aLock) { + MOZ_ASSERT(!mChunkManager); + mChunkManager = &aChunkManager; +#ifdef DEBUG + mChunkManager->RegisteredWith(this); +#endif + + mChunkManager->SetChunkDestroyedCallback( + [this](const ProfileBufferChunk& aChunk) { + for (;;) { + ProfileBufferIndex rangeStart = mRangeStart; + if (MOZ_LIKELY(rangeStart <= aChunk.RangeStart())) { + if (MOZ_LIKELY(mRangeStart.compareExchange( + rangeStart, + aChunk.RangeStart() + aChunk.BufferBytes()))) { + break; + } + } + } + mClearedBlockCount += aChunk.BlockCount(); + }); + + // We start with one chunk right away, and request a following one now + // so it should be available before the current chunk is full. + SetAndInitializeCurrentChunk(mChunkManager->GetChunk(), aLock); + mRequestedChunkHolder = MakeRefPtr<RequestedChunkRefCountedHolder>(); + RequestChunk(aLock); + } + + [[nodiscard]] size_t SizeOfExcludingThis( + MallocSizeOf aMallocSizeOf, + const baseprofiler::detail::BaseProfilerMaybeAutoLock&) const { + if (MOZ_UNLIKELY(!mChunkManager)) { + // Out-of-session. + return 0; + } + size_t size = mChunkManager->SizeOfIncludingThis(aMallocSizeOf); + if (mCurrentChunk) { + size += mCurrentChunk->SizeOfIncludingThis(aMallocSizeOf); + } + if (mNextChunks) { + size += mNextChunks->SizeOfIncludingThis(aMallocSizeOf); + } + return size; + } + + void InitializeCurrentChunk( + const baseprofiler::detail::BaseProfilerMaybeAutoLock&) { + MOZ_ASSERT(!!mCurrentChunk); + mCurrentChunk->SetRangeStart(mNextChunkRangeStart); + mNextChunkRangeStart += mCurrentChunk->BufferBytes(); + Unused << mCurrentChunk->ReserveInitialBlockAsTail(0); + } + + void SetAndInitializeCurrentChunk( + UniquePtr<ProfileBufferChunk>&& aChunk, + const baseprofiler::detail::BaseProfilerMaybeAutoLock& aLock) { + mCurrentChunk = std::move(aChunk); + if (mCurrentChunk) { + InitializeCurrentChunk(aLock); + } + } + + void RequestChunk( + const baseprofiler::detail::BaseProfilerMaybeAutoLock& aLock) { + if (HandleRequestedChunk_IsPending(aLock)) { + // There is already a pending request, don't start a new one. + return; + } + + // Ensure the `RequestedChunkHolder` knows we're starting a request. + mRequestedChunkHolder->StartRequest(); + + // Request a chunk, the callback carries a `RefPtr` of the + // `RequestedChunkHolder`, so it's guaranteed to live until it's invoked, + // even if this `ProfileChunkedBuffer` changes its `ChunkManager` or is + // destroyed. + mChunkManager->RequestChunk( + [requestedChunkHolder = RefPtr<RequestedChunkRefCountedHolder>( + mRequestedChunkHolder)](UniquePtr<ProfileBufferChunk> aChunk) { + requestedChunkHolder->AddRequestedChunk(std::move(aChunk)); + }); + } + + [[nodiscard]] bool HandleRequestedChunk_IsPending( + const baseprofiler::detail::BaseProfilerMaybeAutoLock& aLock) { + MOZ_ASSERT(!!mChunkManager); + MOZ_ASSERT(!!mRequestedChunkHolder); + + if (mRequestedChunkHolder->GetState() == + RequestedChunkRefCountedHolder::State::Unused) { + return false; + } + + // A request is either in-flight or fulfilled. + Maybe<UniquePtr<ProfileBufferChunk>> maybeChunk = + mRequestedChunkHolder->GetChunkIfFulfilled(); + if (maybeChunk.isNothing()) { + // Request is still pending. + return true; + } + + // Since we extracted the provided chunk, the holder should now be unused. + MOZ_ASSERT(mRequestedChunkHolder->GetState() == + RequestedChunkRefCountedHolder::State::Unused); + + // Request has been fulfilled. + UniquePtr<ProfileBufferChunk>& chunk = *maybeChunk; + if (chunk) { + // Try to use as current chunk if needed. + if (!mCurrentChunk) { + SetAndInitializeCurrentChunk(std::move(chunk), aLock); + // We've just received a chunk and made it current, request a next chunk + // for later. + MOZ_ASSERT(!mNextChunks); + RequestChunk(aLock); + return true; + } + + if (!mNextChunks) { + mNextChunks = std::move(chunk); + } else { + mNextChunks->InsertNext(std::move(chunk)); + } + } + + return false; + } + + // Get a pointer to the next chunk available + [[nodiscard]] ProfileBufferChunk* GetOrCreateCurrentChunk( + const baseprofiler::detail::BaseProfilerMaybeAutoLock& aLock) { + ProfileBufferChunk* current = mCurrentChunk.get(); + if (MOZ_UNLIKELY(!current)) { + // No current chunk ready. + MOZ_ASSERT(!mNextChunks, + "There shouldn't be next chunks when there is no current one"); + // See if a request has recently been fulfilled, ignore pending status. + Unused << HandleRequestedChunk_IsPending(aLock); + current = mCurrentChunk.get(); + if (MOZ_UNLIKELY(!current)) { + // There was no pending chunk, try to get one right now. + // This may still fail, but we can't do anything else about it, the + // caller must handle the nullptr case. + // Attempt a request for later. + SetAndInitializeCurrentChunk(mChunkManager->GetChunk(), aLock); + current = mCurrentChunk.get(); + } + } + return current; + } + + // Get a pointer to the next chunk available + [[nodiscard]] ProfileBufferChunk* GetOrCreateNextChunk( + const baseprofiler::detail::BaseProfilerMaybeAutoLock& aLock) { + MOZ_ASSERT(!!mCurrentChunk, + "Why ask for a next chunk when there isn't even a current one?"); + ProfileBufferChunk* next = mNextChunks.get(); + if (MOZ_UNLIKELY(!next)) { + // No next chunk ready, see if a request has recently been fulfilled, + // ignore pending status. + Unused << HandleRequestedChunk_IsPending(aLock); + next = mNextChunks.get(); + if (MOZ_UNLIKELY(!next)) { + // There was no pending chunk, try to get one right now. + mNextChunks = mChunkManager->GetChunk(); + next = mNextChunks.get(); + // This may still fail, but we can't do anything else about it, the + // caller must handle the nullptr case. + if (MOZ_UNLIKELY(!next)) { + // Attempt a request for later. + RequestChunk(aLock); + } + } + } + return next; + } + + // Reserve a block of `aCallbackBlockBytes()` size, and invoke and return + // `aCallback(Maybe<ProfileBufferEntryWriter>&)`. Note that this is the "raw" + // version that doesn't write the entry size at the beginning of the block. + // Note: `aCallbackBlockBytes` is a callback instead of a simple value, to + // delay this potentially-expensive computation until after we're checked that + // we're in-session; use `Put(Length, Callback)` below if you know the size + // already. + template <typename CallbackBlockBytes, typename Callback> + auto ReserveAndPutRaw(CallbackBlockBytes&& aCallbackBlockBytes, + Callback&& aCallback, + baseprofiler::detail::BaseProfilerMaybeAutoLock& aLock, + uint64_t aBlockCount = 1) { + // The entry writer that will point into one or two chunks to write + // into, empty by default (failure). + Maybe<ProfileBufferEntryWriter> maybeEntryWriter; + + // The current chunk will be filled if we need to write more than its + // remaining space. + bool currentChunkFilled = false; + + // If the current chunk gets filled, we may or may not initialize the next + // chunk! + bool nextChunkInitialized = false; + + if (MOZ_LIKELY(mChunkManager)) { + // In-session. + + const Length blockBytes = + std::forward<CallbackBlockBytes>(aCallbackBlockBytes)(); + + if (ProfileBufferChunk* current = GetOrCreateCurrentChunk(aLock); + MOZ_LIKELY(current)) { + if (blockBytes <= current->RemainingBytes()) { + // Block fits in current chunk with only one span. + currentChunkFilled = blockBytes == current->RemainingBytes(); + const auto [mem0, blockIndex] = current->ReserveBlock(blockBytes); + MOZ_ASSERT(mem0.LengthBytes() == blockBytes); + maybeEntryWriter.emplace( + mem0, blockIndex, + ProfileBufferBlockIndex::CreateFromProfileBufferIndex( + blockIndex.ConvertToProfileBufferIndex() + blockBytes)); + MOZ_ASSERT(maybeEntryWriter->RemainingBytes() == blockBytes); + mRangeEnd += blockBytes; + mPushedBlockCount += aBlockCount; + } else if (blockBytes >= current->BufferBytes()) { + // Currently only two buffer chunks are held at a time and it is not + // possible to write an object that takes up more space than this. In + // this scenario, silently discard this block of data if it is unable + // to fit into the two reserved profiler chunks. + mFailedPutBytes += blockBytes; + } else { + // Block doesn't fit fully in current chunk, it needs to overflow into + // the next one. + // Whether or not we can write this entry, the current chunk is now + // considered full, so it will be released. (Otherwise we could refuse + // this entry, but later accept a smaller entry into this chunk, which + // would be somewhat inconsistent.) + currentChunkFilled = true; + // Make sure the next chunk is available (from a previous request), + // otherwise create one on the spot. + if (ProfileBufferChunk* next = GetOrCreateNextChunk(aLock); + MOZ_LIKELY(next)) { + // Here, we know we have a current and a next chunk. + // Reserve head of block at the end of the current chunk. + const auto [mem0, blockIndex] = + current->ReserveBlock(current->RemainingBytes()); + MOZ_ASSERT(mem0.LengthBytes() < blockBytes); + MOZ_ASSERT(current->RemainingBytes() == 0); + // Set the next chunk range, and reserve the needed space for the + // tail of the block. + next->SetRangeStart(mNextChunkRangeStart); + mNextChunkRangeStart += next->BufferBytes(); + const auto mem1 = next->ReserveInitialBlockAsTail( + blockBytes - mem0.LengthBytes()); + MOZ_ASSERT(next->RemainingBytes() != 0); + nextChunkInitialized = true; + // Block is split in two spans. + maybeEntryWriter.emplace( + mem0, mem1, blockIndex, + ProfileBufferBlockIndex::CreateFromProfileBufferIndex( + blockIndex.ConvertToProfileBufferIndex() + blockBytes)); + MOZ_ASSERT(maybeEntryWriter->RemainingBytes() == blockBytes); + mRangeEnd += blockBytes; + mPushedBlockCount += aBlockCount; + } else { + // Cannot get a new chunk. Record put failure. + mFailedPutBytes += blockBytes; + } + } + } else { + // Cannot get a current chunk. Record put failure. + mFailedPutBytes += blockBytes; + } + } // end of `if (MOZ_LIKELY(mChunkManager))` + + // Here, we either have a `Nothing` (failure), or a non-empty entry writer + // pointing at the start of the block. + + // After we invoke the callback and return, we may need to handle the + // current chunk being filled. + auto handleFilledChunk = MakeScopeExit([&]() { + // If the entry writer was not already empty, the callback *must* have + // filled the full entry. + MOZ_ASSERT(!maybeEntryWriter || maybeEntryWriter->RemainingBytes() == 0); + + if (currentChunkFilled) { + // Extract current (now filled) chunk. + UniquePtr<ProfileBufferChunk> filled = std::move(mCurrentChunk); + + if (mNextChunks) { + // Cycle to the next chunk. + mCurrentChunk = + std::exchange(mNextChunks, mNextChunks->ReleaseNext()); + + // Make sure it is initialized (it is now the current chunk). + if (!nextChunkInitialized) { + InitializeCurrentChunk(aLock); + } + } + + // And finally mark filled chunk done and release it. + filled->MarkDone(); + mChunkManager->ReleaseChunk(std::move(filled)); + + // Request another chunk if needed. + // In most cases, here we should have one current chunk and no next + // chunk, so we want to do a request so there hopefully will be a next + // chunk available when the current one gets filled. + // But we also for a request if we don't even have a current chunk (if + // it's too late, it's ok because the next `ReserveAndPutRaw` wil just + // allocate one on the spot.) + // And if we already have a next chunk, there's no need for more now. + if (!mCurrentChunk || !mNextChunks) { + RequestChunk(aLock); + } + } + }); + + return std::forward<Callback>(aCallback)(maybeEntryWriter); + } + + // Reserve a block of `aBlockBytes` size, and invoke and return + // `aCallback(Maybe<ProfileBufferEntryWriter>&)`. Note that this is the "raw" + // version that doesn't write the entry size at the beginning of the block. + template <typename Callback> + auto ReserveAndPutRaw(Length aBlockBytes, Callback&& aCallback, + uint64_t aBlockCount) { + baseprofiler::detail::BaseProfilerMaybeAutoLock lock(mMutex); + return ReserveAndPutRaw([aBlockBytes]() { return aBlockBytes; }, + std::forward<Callback>(aCallback), lock, + aBlockCount); + } + + // Mutex guarding the following members. + mutable baseprofiler::detail::BaseProfilerMaybeMutex mMutex; + + // Pointer to the current Chunk Manager (or null when out-of-session.) + // It may be owned locally (see below) or externally. + ProfileBufferChunkManager* mChunkManager = nullptr; + + // Only non-null when we own the current Chunk Manager. + UniquePtr<ProfileBufferChunkManager> mOwnedChunkManager; + + UniquePtr<ProfileBufferChunk> mCurrentChunk; + + UniquePtr<ProfileBufferChunk> mNextChunks; + + // Class used to transfer requested chunks from a `ChunkManager` to a + // `ProfileChunkedBuffer`. + // It needs to be ref-counted because the request may be fulfilled + // asynchronously, and either side may be destroyed during the request. + // It cannot use the `ProfileChunkedBuffer` mutex, because that buffer and its + // mutex could be destroyed during the request. + class RequestedChunkRefCountedHolder { + public: + enum class State { Unused, Requested, Fulfilled }; + + // Get the current state. Note that it may change after the function + // returns, so it should be used carefully, e.g., `ProfileChunkedBuffer` can + // see if a request is pending or fulfilled, to avoid starting another + // request. + [[nodiscard]] State GetState() const { + baseprofiler::detail::BaseProfilerAutoLock lock(mRequestMutex); + return mState; + } + + // Must be called by `ProfileChunkedBuffer` when it requests a chunk. + // There cannot be more than one request in-flight. + void StartRequest() { + baseprofiler::detail::BaseProfilerAutoLock lock(mRequestMutex); + MOZ_ASSERT(mState == State::Unused, "Already requested or fulfilled"); + mState = State::Requested; + } + + // Must be called by the `ChunkManager` with a chunk. + // If the `ChunkManager` cannot provide a chunk (because of memory limits, + // or it gets destroyed), it must call this anyway with a nullptr. + void AddRequestedChunk(UniquePtr<ProfileBufferChunk>&& aChunk) { + baseprofiler::detail::BaseProfilerAutoLock lock(mRequestMutex); + MOZ_ASSERT(mState == State::Requested); + mState = State::Fulfilled; + mRequestedChunk = std::move(aChunk); + } + + // The `ProfileChunkedBuffer` can try to extract the provided chunk after a + // request: + // - Nothing -> Request is not fulfilled yet. + // - Some(nullptr) -> The `ChunkManager` was not able to provide a chunk. + // - Some(chunk) -> Requested chunk. + [[nodiscard]] Maybe<UniquePtr<ProfileBufferChunk>> GetChunkIfFulfilled() { + Maybe<UniquePtr<ProfileBufferChunk>> maybeChunk; + baseprofiler::detail::BaseProfilerAutoLock lock(mRequestMutex); + MOZ_ASSERT(mState == State::Requested || mState == State::Fulfilled); + if (mState == State::Fulfilled) { + mState = State::Unused; + maybeChunk.emplace(std::move(mRequestedChunk)); + } + return maybeChunk; + } + + // Ref-counting implementation. Hand-rolled, because mozilla::RefCounted + // logs AddRefs and Releases in xpcom, but this object could be AddRef'd + // by the Base Profiler before xpcom starts, then Release'd by the Gecko + // Profiler in xpcom, leading to apparent negative leaks. + + void AddRef() { + baseprofiler::detail::BaseProfilerAutoLock lock(mRequestMutex); + ++mRefCount; + } + + void Release() { + { + baseprofiler::detail::BaseProfilerAutoLock lock(mRequestMutex); + if (--mRefCount > 0) { + return; + } + } + delete this; + } + + private: + ~RequestedChunkRefCountedHolder() = default; + + // Mutex guarding the following members. + mutable baseprofiler::detail::BaseProfilerMutex mRequestMutex; + int mRefCount = 0; + State mState = State::Unused; + UniquePtr<ProfileBufferChunk> mRequestedChunk; + }; + + // Requested-chunk holder, kept alive when in-session, but may also live + // longer if a request is in-flight. + RefPtr<RequestedChunkRefCountedHolder> mRequestedChunkHolder; + + // Range start of the next chunk to become current. Starting at 1 because + // 0 is a reserved index similar to nullptr. + ProfileBufferIndex mNextChunkRangeStart = 1; + + // Index to the first block. + // Atomic because it may be increased when a Chunk is destroyed, and the + // callback may be invoked from anywhere, including from inside one of our + // locked section, so we cannot protect it with a mutex. + Atomic<ProfileBufferIndex, MemoryOrdering::ReleaseAcquire> mRangeStart{1}; + + // Index past the last block. Equals mRangeStart if empty. + ProfileBufferIndex mRangeEnd = 1; + + // Number of blocks that have been pushed into this buffer. + uint64_t mPushedBlockCount = 0; + + // Number of blocks that have been removed from this buffer. + // Note: Live entries = pushed - cleared. + // Atomic because it may be updated when a Chunk is destroyed, and the + // callback may be invoked from anywhere, including from inside one of our + // locked section, so we cannot protect it with a mutex. + Atomic<uint64_t, MemoryOrdering::ReleaseAcquire> mClearedBlockCount{0}; + + // Number of bytes that could not be put into this buffer. + uint64_t mFailedPutBytes = 0; +}; + +// ---------------------------------------------------------------------------- +// ProfileChunkedBuffer serialization + +// A ProfileChunkedBuffer can hide another one! +// This will be used to store marker backtraces; They can be read back into a +// UniquePtr<ProfileChunkedBuffer>. +// Format: len (ULEB128) | start | end | buffer (len bytes) | pushed | cleared +// len==0 marks an out-of-session buffer, or empty buffer. +template <> +struct ProfileBufferEntryWriter::Serializer<ProfileChunkedBuffer> { + static Length Bytes(const ProfileChunkedBuffer& aBuffer) { + return aBuffer.Read([&](ProfileChunkedBuffer::Reader* aReader) { + if (!aReader) { + // Out-of-session, we only need 1 byte to store a length of 0. + return ULEB128Size<Length>(0); + } + ProfileBufferEntryReader reader = aReader->SingleChunkDataAsEntry(); + const ProfileBufferIndex start = + reader.CurrentBlockIndex().ConvertToProfileBufferIndex(); + const ProfileBufferIndex end = + reader.NextBlockIndex().ConvertToProfileBufferIndex(); + MOZ_ASSERT(end - start <= std::numeric_limits<Length>::max()); + const Length len = static_cast<Length>(end - start); + if (len == 0) { + // In-session but empty, also store a length of 0. + return ULEB128Size<Length>(0); + } + // In-session. + return static_cast<Length>(ULEB128Size(len) + sizeof(start) + len + + sizeof(aBuffer.mPushedBlockCount) + + sizeof(aBuffer.mClearedBlockCount)); + }); + } + + static void Write(ProfileBufferEntryWriter& aEW, + const ProfileChunkedBuffer& aBuffer) { + aBuffer.Read([&](ProfileChunkedBuffer::Reader* aReader) { + if (!aReader) { + // Out-of-session, only store a length of 0. + aEW.WriteULEB128<Length>(0); + return; + } + ProfileBufferEntryReader reader = aReader->SingleChunkDataAsEntry(); + const ProfileBufferIndex start = + reader.CurrentBlockIndex().ConvertToProfileBufferIndex(); + const ProfileBufferIndex end = + reader.NextBlockIndex().ConvertToProfileBufferIndex(); + MOZ_ASSERT(end - start <= std::numeric_limits<Length>::max()); + const Length len = static_cast<Length>(end - start); + MOZ_ASSERT(len <= aEW.RemainingBytes()); + if (len == 0) { + // In-session but empty, only store a length of 0. + aEW.WriteULEB128<Length>(0); + return; + } + // In-session. + // Store buffer length, and start index. + aEW.WriteULEB128(len); + aEW.WriteObject(start); + // Write all the bytes. + aEW.WriteFromReader(reader, reader.RemainingBytes()); + // And write stats. + aEW.WriteObject(static_cast<uint64_t>(aBuffer.mPushedBlockCount)); + aEW.WriteObject(static_cast<uint64_t>(aBuffer.mClearedBlockCount)); + // Note: Failed pushes are not important to serialize. + }); + } +}; + +// A serialized ProfileChunkedBuffer can be read into an empty buffer (either +// out-of-session, or in-session with enough room). +template <> +struct ProfileBufferEntryReader::Deserializer<ProfileChunkedBuffer> { + static void ReadInto(ProfileBufferEntryReader& aER, + ProfileChunkedBuffer& aBuffer) { + // Expect an empty buffer, as we're going to overwrite it. + MOZ_ASSERT(aBuffer.GetState().mRangeStart == aBuffer.GetState().mRangeEnd); + // Read the stored buffer length. + const auto len = aER.ReadULEB128<ProfileChunkedBuffer::Length>(); + if (len == 0) { + // 0-length means an "uninteresting" buffer, just return now. + return; + } + // We have a non-empty buffer to read. + + // Read start and end indices. + const auto start = aER.ReadObject<ProfileBufferIndex>(); + aBuffer.mRangeStart = start; + // For now, set the end to be the start (the buffer is still empty). It will + // be updated in `ReserveAndPutRaw()` below. + aBuffer.mRangeEnd = start; + + if (aBuffer.IsInSession()) { + // Output buffer is in-session (i.e., it already has a memory buffer + // attached). Make sure the caller allocated enough space. + MOZ_RELEASE_ASSERT(aBuffer.BufferLength().value() >= len); + } else { + // Output buffer is out-of-session, set a new chunk manager that will + // provide a single chunk of just the right size. + aBuffer.SetChunkManager(MakeUnique<ProfileBufferChunkManagerSingle>(len)); + MOZ_ASSERT(aBuffer.BufferLength().value() >= len); + } + + // Copy bytes into the buffer. + aBuffer.ReserveAndPutRaw( + len, + [&](Maybe<ProfileBufferEntryWriter>& aEW) { + MOZ_RELEASE_ASSERT(aEW.isSome()); + aEW->WriteFromReader(aER, len); + }, + 0); + // Finally copy stats. + aBuffer.mPushedBlockCount = aER.ReadObject<uint64_t>(); + aBuffer.mClearedBlockCount = aER.ReadObject<uint64_t>(); + // Failed puts are not important to keep. + aBuffer.mFailedPutBytes = 0; + } + + // We cannot output a ProfileChunkedBuffer object (not copyable), use + // `ReadInto()` or `aER.ReadObject<UniquePtr<BlocksRinbBuffer>>()` instead. + static ProfileChunkedBuffer Read(ProfileBufferEntryReader& aER) = delete; +}; + +// A ProfileChunkedBuffer is usually refererenced through a UniquePtr, for +// convenience we support (de)serializing that UniquePtr directly. +// This is compatible with the non-UniquePtr serialization above, with a null +// pointer being treated like an out-of-session or empty buffer; and any of +// these would be deserialized into a null pointer. +template <> +struct ProfileBufferEntryWriter::Serializer<UniquePtr<ProfileChunkedBuffer>> { + static Length Bytes(const UniquePtr<ProfileChunkedBuffer>& aBufferUPtr) { + if (!aBufferUPtr) { + // Null pointer, treat it like an empty buffer, i.e., write length of 0. + return ULEB128Size<Length>(0); + } + // Otherwise write the pointed-at ProfileChunkedBuffer (which could be + // out-of-session or empty.) + return SumBytes(*aBufferUPtr); + } + + static void Write(ProfileBufferEntryWriter& aEW, + const UniquePtr<ProfileChunkedBuffer>& aBufferUPtr) { + if (!aBufferUPtr) { + // Null pointer, treat it like an empty buffer, i.e., write length of 0. + aEW.WriteULEB128<Length>(0); + return; + } + // Otherwise write the pointed-at ProfileChunkedBuffer (which could be + // out-of-session or empty.) + aEW.WriteObject(*aBufferUPtr); + } +}; + +// Serialization of a raw pointer to ProfileChunkedBuffer. +// Use Deserializer<UniquePtr<ProfileChunkedBuffer>> to read it back. +template <> +struct ProfileBufferEntryWriter::Serializer<ProfileChunkedBuffer*> { + static Length Bytes(ProfileChunkedBuffer* aBufferUPtr) { + if (!aBufferUPtr) { + // Null pointer, treat it like an empty buffer, i.e., write length of 0. + return ULEB128Size<Length>(0); + } + // Otherwise write the pointed-at ProfileChunkedBuffer (which could be + // out-of-session or empty.) + return SumBytes(*aBufferUPtr); + } + + static void Write(ProfileBufferEntryWriter& aEW, + ProfileChunkedBuffer* aBufferUPtr) { + if (!aBufferUPtr) { + // Null pointer, treat it like an empty buffer, i.e., write length of 0. + aEW.WriteULEB128<Length>(0); + return; + } + // Otherwise write the pointed-at ProfileChunkedBuffer (which could be + // out-of-session or empty.) + aEW.WriteObject(*aBufferUPtr); + } +}; + +template <> +struct ProfileBufferEntryReader::Deserializer<UniquePtr<ProfileChunkedBuffer>> { + static void ReadInto(ProfileBufferEntryReader& aER, + UniquePtr<ProfileChunkedBuffer>& aBuffer) { + aBuffer = Read(aER); + } + + static UniquePtr<ProfileChunkedBuffer> Read(ProfileBufferEntryReader& aER) { + UniquePtr<ProfileChunkedBuffer> bufferUPtr; + // Keep a copy of the reader before reading the length, so we can restart + // from here below. + ProfileBufferEntryReader readerBeforeLen = aER; + // Read the stored buffer length. + const auto len = aER.ReadULEB128<ProfileChunkedBuffer::Length>(); + if (len == 0) { + // 0-length means an "uninteresting" buffer, just return nullptr. + return bufferUPtr; + } + // We have a non-empty buffer. + // allocate an empty ProfileChunkedBuffer without mutex. + bufferUPtr = MakeUnique<ProfileChunkedBuffer>( + ProfileChunkedBuffer::ThreadSafety::WithoutMutex); + // Rewind the reader before the length and deserialize the contents, using + // the non-UniquePtr Deserializer. + aER = readerBeforeLen; + aER.ReadIntoObject(*bufferUPtr); + return bufferUPtr; + } +}; + +} // namespace mozilla + +#endif // ProfileChunkedBuffer_h diff --git a/mozglue/baseprofiler/public/ProfileChunkedBufferDetail.h b/mozglue/baseprofiler/public/ProfileChunkedBufferDetail.h new file mode 100644 index 0000000000..9828822aaf --- /dev/null +++ b/mozglue/baseprofiler/public/ProfileChunkedBufferDetail.h @@ -0,0 +1,400 @@ +/* -*- Mode: C++; tab-width: 2; 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/. */ + +#ifndef ProfileChunkedBufferDetail_h +#define ProfileChunkedBufferDetail_h + +#include "mozilla/Assertions.h" +#include "mozilla/Likely.h" +#include "mozilla/ProfileBufferChunk.h" +#include "mozilla/ProfileBufferEntrySerialization.h" + +namespace mozilla::profiler::detail { + +// Internal accessor pointing at a position inside a chunk. +// It can handle two groups of chunks (typically the extant chunks stored in +// the store manager, and the current chunk). +// The main operations are: +// - ReadEntrySize() to read an entry size, 0 means failure. +// - operator+=(Length) to skip a number of bytes. +// - EntryReader() creates an entry reader at the current position for a given +// size (it may fail with an empty reader), and skips the entry. +// Note that there is no "past-the-end" position -- as soon as InChunkPointer +// reaches the end, it becomes effectively null. +class InChunkPointer { + public: + using Byte = ProfileBufferChunk::Byte; + using Length = ProfileBufferChunk::Length; + + // Nullptr-like InChunkPointer, may be used as end iterator. + InChunkPointer() + : mChunk(nullptr), mNextChunkGroup(nullptr), mOffsetInChunk(0) {} + + // InChunkPointer over one or two chunk groups, pointing at the given + // block index (if still in range). + // This constructor should only be used with *trusted* block index values! + InChunkPointer(const ProfileBufferChunk* aChunk, + const ProfileBufferChunk* aNextChunkGroup, + ProfileBufferBlockIndex aBlockIndex) + : mChunk(aChunk), mNextChunkGroup(aNextChunkGroup) { + if (mChunk) { + mOffsetInChunk = mChunk->OffsetFirstBlock(); + Adjust(); + } else if (mNextChunkGroup) { + mChunk = mNextChunkGroup; + mNextChunkGroup = nullptr; + mOffsetInChunk = mChunk->OffsetFirstBlock(); + Adjust(); + } else { + mOffsetInChunk = 0; + } + + // Try to advance to given position. + if (!AdvanceToGlobalRangePosition(aBlockIndex)) { + // Block does not exist anymore (or block doesn't look valid), reset the + // in-chunk pointer. + mChunk = nullptr; + mNextChunkGroup = nullptr; + } + } + + // InChunkPointer over one or two chunk groups, will start at the first + // block (if any). This may be slow, so avoid using it too much. + InChunkPointer(const ProfileBufferChunk* aChunk, + const ProfileBufferChunk* aNextChunkGroup, + ProfileBufferIndex aIndex = ProfileBufferIndex(0)) + : mChunk(aChunk), mNextChunkGroup(aNextChunkGroup) { + if (mChunk) { + mOffsetInChunk = mChunk->OffsetFirstBlock(); + Adjust(); + } else if (mNextChunkGroup) { + mChunk = mNextChunkGroup; + mNextChunkGroup = nullptr; + mOffsetInChunk = mChunk->OffsetFirstBlock(); + Adjust(); + } else { + mOffsetInChunk = 0; + } + + // Try to advance to given position. + if (!AdvanceToGlobalRangePosition(aIndex)) { + // Block does not exist anymore, reset the in-chunk pointer. + mChunk = nullptr; + mNextChunkGroup = nullptr; + } + } + + // Compute the current position in the global range. + // 0 if null (including if we're reached the end). + [[nodiscard]] ProfileBufferIndex GlobalRangePosition() const { + if (IsNull()) { + return 0; + } + return mChunk->RangeStart() + mOffsetInChunk; + } + + // Move InChunkPointer forward to the block at the given global block + // position, which is assumed to be valid exactly -- but it may be obsolete. + // 0 stays where it is (if valid already). + // MOZ_ASSERTs if the index is invalid. + [[nodiscard]] bool AdvanceToGlobalRangePosition( + ProfileBufferBlockIndex aBlockIndex) { + if (IsNull()) { + // Pointer is null already. (Not asserting because it's acceptable.) + return false; + } + if (!aBlockIndex) { + // Special null position, just stay where we are. + return ShouldPointAtValidBlock(); + } + if (aBlockIndex.ConvertToProfileBufferIndex() < GlobalRangePosition()) { + // Past the requested position, stay where we are (assuming the current + // position was valid). + return ShouldPointAtValidBlock(); + } + for (;;) { + if (aBlockIndex.ConvertToProfileBufferIndex() < + mChunk->RangeStart() + mChunk->OffsetPastLastBlock()) { + // Target position is in this chunk's written space, move to it. + mOffsetInChunk = + aBlockIndex.ConvertToProfileBufferIndex() - mChunk->RangeStart(); + return ShouldPointAtValidBlock(); + } + // Position is after this chunk, try next chunk. + GoToNextChunk(); + if (IsNull()) { + return false; + } + // Skip whatever block tail there is, we don't allow pointing in the + // middle of a block. + mOffsetInChunk = mChunk->OffsetFirstBlock(); + if (aBlockIndex.ConvertToProfileBufferIndex() < GlobalRangePosition()) { + // Past the requested position, meaning that the given position was in- + // between blocks -> Failure. + MOZ_ASSERT(false, "AdvanceToGlobalRangePosition - In-between blocks"); + return false; + } + } + } + + // Move InChunkPointer forward to the block at or after the given global + // range position. + // 0 stays where it is (if valid already). + [[nodiscard]] bool AdvanceToGlobalRangePosition( + ProfileBufferIndex aPosition) { + if (aPosition == 0) { + // Special position '0', just stay where we are. + // Success if this position is already valid. + return !IsNull(); + } + for (;;) { + ProfileBufferIndex currentPosition = GlobalRangePosition(); + if (currentPosition == 0) { + // Pointer is null. + return false; + } + if (aPosition <= currentPosition) { + // At or past the requested position, stay where we are. + return true; + } + if (aPosition < mChunk->RangeStart() + mChunk->OffsetPastLastBlock()) { + // Target position is in this chunk's written space, move to it. + for (;;) { + // Skip the current block. + mOffsetInChunk += ReadEntrySize(); + if (mOffsetInChunk >= mChunk->OffsetPastLastBlock()) { + // Reached the end of the chunk, this can happen for the last + // block, let's just continue to the next chunk. + break; + } + if (aPosition <= mChunk->RangeStart() + mOffsetInChunk) { + // We're at or after the position, return at this block position. + return true; + } + } + } + // Position is after this chunk, try next chunk. + GoToNextChunk(); + if (IsNull()) { + return false; + } + // Skip whatever block tail there is, we don't allow pointing in the + // middle of a block. + mOffsetInChunk = mChunk->OffsetFirstBlock(); + } + } + + [[nodiscard]] Byte ReadByte() { + MOZ_ASSERT(!IsNull()); + MOZ_ASSERT(mOffsetInChunk < mChunk->OffsetPastLastBlock()); + Byte byte = mChunk->ByteAt(mOffsetInChunk); + if (MOZ_UNLIKELY(++mOffsetInChunk == mChunk->OffsetPastLastBlock())) { + Adjust(); + } + return byte; + } + + // Read and skip a ULEB128-encoded size. + // 0 means failure (0-byte entries are not allowed.) + // Note that this doesn't guarantee that there are actually that many bytes + // available to read! (EntryReader() below may gracefully fail.) + [[nodiscard]] Length ReadEntrySize() { + ULEB128Reader<Length> reader; + if (IsNull()) { + return 0; + } + for (;;) { + const bool isComplete = reader.FeedByteIsComplete(ReadByte()); + if (MOZ_UNLIKELY(IsNull())) { + // End of chunks, so there's no actual entry after this anyway. + return 0; + } + if (MOZ_LIKELY(isComplete)) { + if (MOZ_UNLIKELY(reader.Value() > mChunk->BufferBytes())) { + // Don't allow entries larger than a chunk. + return 0; + } + return reader.Value(); + } + } + } + + InChunkPointer& operator+=(Length aLength) { + MOZ_ASSERT(!IsNull()); + mOffsetInChunk += aLength; + Adjust(); + return *this; + } + + [[nodiscard]] ProfileBufferEntryReader EntryReader(Length aLength) { + if (IsNull() || aLength == 0) { + return ProfileBufferEntryReader(); + } + + MOZ_ASSERT(mOffsetInChunk < mChunk->OffsetPastLastBlock()); + + // We should be pointing at the entry, past the entry size. + const ProfileBufferIndex entryIndex = GlobalRangePosition(); + // Verify that there's enough space before for the size (starting at index + // 1 at least). + MOZ_ASSERT(entryIndex >= 1u + ULEB128Size(aLength)); + + const Length remaining = mChunk->OffsetPastLastBlock() - mOffsetInChunk; + Span<const Byte> mem0 = mChunk->BufferSpan(); + mem0 = mem0.From(mOffsetInChunk); + if (aLength <= remaining) { + // Move to the end of this block, which could make this null if we have + // reached the end of all buffers. + *this += aLength; + return ProfileBufferEntryReader( + mem0.To(aLength), + // Block starts before the entry size. + ProfileBufferBlockIndex::CreateFromProfileBufferIndex( + entryIndex - ULEB128Size(aLength)), + // Block ends right after the entry (could be null for last entry). + ProfileBufferBlockIndex::CreateFromProfileBufferIndex( + GlobalRangePosition())); + } + + // We need to go to the next chunk for the 2nd part of this block. + GoToNextChunk(); + if (IsNull()) { + return ProfileBufferEntryReader(); + } + + Span<const Byte> mem1 = mChunk->BufferSpan(); + const Length tail = aLength - remaining; + MOZ_ASSERT(tail <= mChunk->BufferBytes()); + MOZ_ASSERT(tail == mChunk->OffsetFirstBlock()); + // We are in the correct chunk, move the offset to the end of the block. + mOffsetInChunk = tail; + // And adjust as needed, which could make this null if we have reached the + // end of all buffers. + Adjust(); + return ProfileBufferEntryReader( + mem0, mem1.To(tail), + // Block starts before the entry size. + ProfileBufferBlockIndex::CreateFromProfileBufferIndex( + entryIndex - ULEB128Size(aLength)), + // Block ends right after the entry (could be null for last entry). + ProfileBufferBlockIndex::CreateFromProfileBufferIndex( + GlobalRangePosition())); + } + + [[nodiscard]] bool IsNull() const { return !mChunk; } + + [[nodiscard]] bool operator==(const InChunkPointer& aOther) const { + if (IsNull() || aOther.IsNull()) { + return IsNull() && aOther.IsNull(); + } + return mChunk == aOther.mChunk && mOffsetInChunk == aOther.mOffsetInChunk; + } + + [[nodiscard]] bool operator!=(const InChunkPointer& aOther) const { + return !(*this == aOther); + } + + [[nodiscard]] Byte operator*() const { + MOZ_ASSERT(!IsNull()); + MOZ_ASSERT(mOffsetInChunk < mChunk->OffsetPastLastBlock()); + return mChunk->ByteAt(mOffsetInChunk); + } + + InChunkPointer& operator++() { + MOZ_ASSERT(!IsNull()); + MOZ_ASSERT(mOffsetInChunk < mChunk->OffsetPastLastBlock()); + if (MOZ_UNLIKELY(++mOffsetInChunk == mChunk->OffsetPastLastBlock())) { + mOffsetInChunk = 0; + GoToNextChunk(); + Adjust(); + } + return *this; + } + + private: + void GoToNextChunk() { + MOZ_ASSERT(!IsNull()); + const ProfileBufferIndex expectedNextRangeStart = + mChunk->RangeStart() + mChunk->BufferBytes(); + + mChunk = mChunk->GetNext(); + if (!mChunk) { + // Reached the end of the current chunk group, try the next one (which + // may be null too, especially on the 2nd try). + mChunk = mNextChunkGroup; + mNextChunkGroup = nullptr; + } + + if (mChunk && mChunk->RangeStart() == 0) { + // Reached a chunk without a valid (non-null) range start, assume there + // are only unused chunks from here on. + mChunk = nullptr; + } + + MOZ_ASSERT(!mChunk || mChunk->RangeStart() == expectedNextRangeStart, + "We don't handle discontinuous buffers (yet)"); + // Non-DEBUG fallback: Stop reading past discontinuities. + // (They should be rare, only happening on temporary OOMs.) + // TODO: Handle discontinuities (by skipping over incomplete blocks). + if (mChunk && mChunk->RangeStart() != expectedNextRangeStart) { + mChunk = nullptr; + } + } + + // We want `InChunkPointer` to always point at a valid byte (or be null). + // After some operations, `mOffsetInChunk` may point past the end of the + // current `mChunk`, in which case we need to adjust our position to be inside + // the appropriate chunk. E.g., if we're 10 bytes after the end of the current + // chunk, we should end up at offset 10 in the next chunk. + // Note that we may "fall off" the last chunk and make this `InChunkPointer` + // effectively null. + void Adjust() { + while (mChunk && mOffsetInChunk >= mChunk->OffsetPastLastBlock()) { + // TODO: Try to adjust offset between chunks relative to mRangeStart + // differences. But we don't handle discontinuities yet. + if (mOffsetInChunk < mChunk->BufferBytes()) { + mOffsetInChunk -= mChunk->BufferBytes(); + } else { + mOffsetInChunk -= mChunk->OffsetPastLastBlock(); + } + GoToNextChunk(); + } + } + + // Check if the current position is likely to point at a valid block. + // (Size should be reasonable, and block should fully fit inside buffer.) + // MOZ_ASSERTs on failure, to catch incorrect uses of block indices (which + // should only point at valid blocks if still in range). Non-asserting build + // fallback should still be handled. + [[nodiscard]] bool ShouldPointAtValidBlock() const { + if (IsNull()) { + // Pointer is null, no blocks here. + MOZ_ASSERT(false, "ShouldPointAtValidBlock - null pointer"); + return false; + } + // Use a copy, so we don't modify `*this`. + InChunkPointer pointer = *this; + // Try to read the entry size. + Length entrySize = pointer.ReadEntrySize(); + if (entrySize == 0) { + // Entry size of zero means we read 0 or a way-too-big value. + MOZ_ASSERT(false, "ShouldPointAtValidBlock - invalid size"); + return false; + } + // See if the last byte of the entry is still inside the buffer. + pointer += entrySize - 1; + MOZ_ASSERT(!IsNull(), "ShouldPointAtValidBlock - past end of buffer"); + return !IsNull(); + } + + const ProfileBufferChunk* mChunk; + const ProfileBufferChunk* mNextChunkGroup; + Length mOffsetInChunk; +}; + +} // namespace mozilla::profiler::detail + +#endif // ProfileChunkedBufferDetail_h diff --git a/mozglue/baseprofiler/public/ProgressLogger.h b/mozglue/baseprofiler/public/ProgressLogger.h new file mode 100644 index 0000000000..e15095faaa --- /dev/null +++ b/mozglue/baseprofiler/public/ProgressLogger.h @@ -0,0 +1,500 @@ +/* -*- 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/. */ + +#ifndef ProgressLogger_h +#define ProgressLogger_h + +#include "mozilla/Assertions.h" +#include "mozilla/ProportionValue.h" +#include "mozilla/RefCounted.h" +#include "mozilla/RefPtr.h" + +#include <atomic> + +// Uncomment to printf ProcessLogger updates. +// #define DEBUG_PROCESSLOGGER + +#ifdef DEBUG_PROCESSLOGGER +# include "mozilla/BaseProfilerUtils.h" +# include <cstdio> +#endif // DEBUG_PROCESSLOGGER + +namespace mozilla { + +// A `ProgressLogger` is used to update a referenced atomic `ProportionValue`, +// and can recursively create a sub-logger corresponding to a subset of their +// own range, but that sub-logger's updates are done in its local 0%-100% range. +// The typical usage is for multi-level tasks, where each level can estimate its +// own work and the work delegated to a next-level function, without knowing how +// this local work relates to the higher-level total work. See +// `CreateSubLoggerFromTo` for details. +// Note that this implementation is single-threaded, it does not support logging +// progress from multiple threads at the same time. +class ProgressLogger { + public: + // An RefPtr'd object of this class is used as the target of all + // ProgressLogger updates, and it may be shared to make these updates visible + // from other code in any thread. + class SharedProgress : public external::AtomicRefCounted<SharedProgress> { + public: + MOZ_DECLARE_REFCOUNTED_TYPENAME(SharedProgress) + + SharedProgress() = default; + + SharedProgress(const SharedProgress&) = delete; + SharedProgress& operator=(const SharedProgress&) = delete; + + // This constant is used to indicate that an update may change the progress + // value, but should not modify the previously-recorded location. + static constexpr const char* NO_LOCATION_UPDATE = nullptr; + + // Set the current progress and location, but the previous location is not + // overwritten if the new one is null or empty. + // The location and then the progress are atomically "released", so that all + // preceding writes on this thread will be visible to other threads reading + // these values; most importantly when reaching 100% progress, the reader + // can be confident that the location is final and the operation being + // watched has completed. + void SetProgress( + ProportionValue aProgress, + const char* aLocationOrNullEmptyToIgnore = NO_LOCATION_UPDATE) { + if (aLocationOrNullEmptyToIgnore && + *aLocationOrNullEmptyToIgnore != '\0') { + mLastLocation.store(aLocationOrNullEmptyToIgnore, + std::memory_order_release); + } + mProgress.store(aProgress, std::memory_order_release); + } + + // Read the current progress value. Atomically "acquired", so that writes + // from the thread that stored this value are all visible to the reader + // here; most importantly when reaching 100%, we can be confident that the + // location is final and the operation being watched has completed. + [[nodiscard]] ProportionValue Progress() const { + return mProgress.load(std::memory_order_acquire); + } + + // Read the current progress value. Atomically "acquired". + [[nodiscard]] const char* LastLocation() const { + return mLastLocation.load(std::memory_order_acquire); + } + + private: + friend mozilla::detail::RefCounted<SharedProgress, + mozilla::detail::AtomicRefCount>; + ~SharedProgress() = default; + + // Progress and last-known location. + // Beware that these two values are not strongly tied: Reading one then the + // other may give mismatched information; but it should be fine for + // informational usage. + // They are stored using atomic acquire-release ordering, to guarantee that + // when read, all writes preceding these values are visible. + std::atomic<ProportionValue> mProgress = ProportionValue{0.0}; + std::atomic<const char*> mLastLocation = nullptr; + }; + + static constexpr const char* NO_LOCATION_UPDATE = + SharedProgress::NO_LOCATION_UPDATE; + + ProgressLogger() = default; + + // Construct a top-level logger, starting at 0% and expected to end at 100%. + explicit ProgressLogger( + RefPtr<SharedProgress> aGlobalProgressOrNull, + const char* aLocationOrNullEmptyToIgnoreAtStart = NO_LOCATION_UPDATE, + const char* aLocationOrNullEmptyToIgnoreAtEnd = NO_LOCATION_UPDATE) + : ProgressLogger{std::move(aGlobalProgressOrNull), + /* Start */ ProportionValue{0.0}, + /* Multiplier */ ProportionValue{1.0}, + aLocationOrNullEmptyToIgnoreAtStart, + aLocationOrNullEmptyToIgnoreAtEnd} {} + + // Don't make copies, it would be confusing! + // TODO: Copies could one day be allowed to track multi-threaded work, but it + // is outside the scope of this implementation; Please update if needed. + ProgressLogger(const ProgressLogger&) = delete; + ProgressLogger& operator&(const ProgressLogger&) = delete; + + // Move-construct is allowed, to return from CreateSubLoggerFromTo, and + // forward straight into a function. Note that moved-from ProgressLoggers must + // not be used anymore! Use `CreateSubLoggerFromTo` to pass a sub-logger to + // functions. + ProgressLogger(ProgressLogger&& aOther) + : mGlobalProgressOrNull(std::move(aOther.mGlobalProgressOrNull)), + mLocalStartInGlobalSpace(aOther.mLocalStartInGlobalSpace), + mLocalToGlobalMultiplier(aOther.mLocalToGlobalMultiplier), + mLocationAtDestruction(aOther.mLocationAtDestruction) { + aOther.MarkMovedFrom(); +#ifdef DEBUG_PROCESSLOGGER + if (mGlobalProgressOrNull) { + printf("[%d] Moved (staying globally at %.2f in [%.2f, %.2f])\n", + int(baseprofiler::profiler_current_process_id().ToNumber()), + GetGlobalProgress().ToDouble() * 100.0, + mLocalStartInGlobalSpace.ToDouble() * 100.0, + (mLocalStartInGlobalSpace + mLocalToGlobalMultiplier).ToDouble() * + 100.0); + } +#endif // DEBUG_PROCESSLOGGER + } + + // Move-assign. This may be useful when starting with a default (empty) logger + // and later assigning it a progress value to start updating. + ProgressLogger& operator=(ProgressLogger&& aOther) { + mGlobalProgressOrNull = std::move(aOther.mGlobalProgressOrNull); + mLocalStartInGlobalSpace = aOther.mLocalStartInGlobalSpace; + mLocalToGlobalMultiplier = aOther.mLocalToGlobalMultiplier; + mLocationAtDestruction = aOther.mLocationAtDestruction; + aOther.MarkMovedFrom(); +#ifdef DEBUG_PROCESSLOGGER + if (mGlobalProgressOrNull) { + printf("[%d] Re-assigned (globally at %.2f in [%.2f, %.2f])\n", + int(baseprofiler::profiler_current_process_id().ToNumber()), + GetGlobalProgress().ToDouble() * 100.0, + mLocalStartInGlobalSpace.ToDouble() * 100.0, + (mLocalStartInGlobalSpace + mLocalToGlobalMultiplier).ToDouble() * + 100.0); + } +#endif // DEBUG_PROCESSLOGGER + return *this; + } + + // Destruction sets the local update value to 100% unless empty or moved-from. + ~ProgressLogger() { + if (!IsMovedFrom()) { +#ifdef DEBUG_PROCESSLOGGER + if (mGlobalProgressOrNull) { + printf("[%d] Destruction:\n", + int(baseprofiler::profiler_current_process_id().ToNumber())); + } +#endif // DEBUG_PROCESSLOGGER + SetLocalProgress(ProportionValue{1.0}, mLocationAtDestruction); + } + } + + // Retrieve the current progress in the global space. May be invalid. + [[nodiscard]] ProportionValue GetGlobalProgress() const { + return mGlobalProgressOrNull ? mGlobalProgressOrNull->Progress() + : ProportionValue::MakeInvalid(); + } + + // Retrieve the last known global location. May be null. + [[nodiscard]] const char* GetLastGlobalLocation() const { + return mGlobalProgressOrNull ? mGlobalProgressOrNull->LastLocation() + : nullptr; + } + + // Set the current progress in the local space. + void SetLocalProgress(ProportionValue aLocalProgress, + const char* aLocationOrNullEmptyToIgnore) { + MOZ_ASSERT(!IsMovedFrom()); + if (mGlobalProgressOrNull && !mLocalToGlobalMultiplier.IsExactlyZero()) { + mGlobalProgressOrNull->SetProgress(LocalToGlobal(aLocalProgress), + aLocationOrNullEmptyToIgnore); +#ifdef DEBUG_PROCESSLOGGER + printf("[%d] - local %.0f%% ~ global %.2f%% \"%s\"\n", + int(baseprofiler::profiler_current_process_id().ToNumber()), + aLocalProgress.ToDouble() * 100.0, + LocalToGlobal(aLocalProgress).ToDouble() * 100.0, + aLocationOrNullEmptyToIgnore ? aLocationOrNullEmptyToIgnore + : "<null>"); +#endif // DEBUG_PROCESSLOGGER + } + } + + // Create a sub-logger that will record progress in the given local range. + // E.g.: `f(pl.CreateSubLoggerFromTo(0.2, "f...", 0.4, "f done"));` expects + // that `f` will produce work in the local range 0.2 (when starting) to 0.4 + // (when returning); `f` itself will update this provided logger from 0.0 + // to 1.0 (local to that `f` function), which will effectively be converted to + // 0.2-0.4 (local to the calling function). + // This can cascade multiple levels, each deeper level affecting a smaller and + // smaller range in the global output. + [[nodiscard]] ProgressLogger CreateSubLoggerFromTo( + ProportionValue aSubStartInLocalSpace, + const char* aLocationOrNullEmptyToIgnoreAtStart, + ProportionValue aSubEndInLocalSpace, + const char* aLocationOrNullEmptyToIgnoreAtEnd = NO_LOCATION_UPDATE) { + MOZ_ASSERT(!IsMovedFrom()); + if (!mGlobalProgressOrNull) { + return ProgressLogger{}; + } + const ProportionValue subStartInGlobalSpace = + LocalToGlobal(aSubStartInLocalSpace); + const ProportionValue subEndInGlobalSpace = + LocalToGlobal(aSubEndInLocalSpace); + if (subStartInGlobalSpace.IsInvalid() || subEndInGlobalSpace.IsInvalid()) { + return ProgressLogger{mGlobalProgressOrNull, + /* Start */ ProportionValue::MakeInvalid(), + /* Multiplier */ ProportionValue{0.0}, + aLocationOrNullEmptyToIgnoreAtStart, + aLocationOrNullEmptyToIgnoreAtEnd}; + } +#ifdef DEBUG_PROCESSLOGGER + if (mGlobalProgressOrNull) { + printf("[%d] * Sub: local [%.0f%%, %.0f%%] ~ global [%.2f%%, %.2f%%]\n", + int(baseprofiler::profiler_current_process_id().ToNumber()), + aSubStartInLocalSpace.ToDouble() * 100.0, + aSubEndInLocalSpace.ToDouble() * 100.0, + subStartInGlobalSpace.ToDouble() * 100.0, + subEndInGlobalSpace.ToDouble() * 100.0); + } +#endif // DEBUG_PROCESSLOGGER + return ProgressLogger{ + mGlobalProgressOrNull, + /* Start */ subStartInGlobalSpace, + /* Multipler */ subEndInGlobalSpace - subStartInGlobalSpace, + aLocationOrNullEmptyToIgnoreAtStart, aLocationOrNullEmptyToIgnoreAtEnd}; + } + + // Helper with no start location. + [[nodiscard]] ProgressLogger CreateSubLoggerFromTo( + ProportionValue aSubStartInLocalSpace, + ProportionValue aSubEndInLocalSpace, + const char* aLocationOrNullEmptyToIgnoreAtEnd = NO_LOCATION_UPDATE) { + return CreateSubLoggerFromTo(aSubStartInLocalSpace, NO_LOCATION_UPDATE, + aSubEndInLocalSpace, + aLocationOrNullEmptyToIgnoreAtEnd); + } + + // Helper using the current progress as start. + [[nodiscard]] ProgressLogger CreateSubLoggerTo( + const char* aLocationOrNullEmptyToIgnoreAtStart, + ProportionValue aSubEndInLocalSpace, + const char* aLocationOrNullEmptyToIgnoreAtEnd = NO_LOCATION_UPDATE) { + MOZ_ASSERT(!IsMovedFrom()); + if (!mGlobalProgressOrNull) { + return ProgressLogger{}; + } + const ProportionValue subStartInGlobalSpace = GetGlobalProgress(); + const ProportionValue subEndInGlobalSpace = + LocalToGlobal(aSubEndInLocalSpace); + if (subStartInGlobalSpace.IsInvalid() || subEndInGlobalSpace.IsInvalid()) { + return ProgressLogger{mGlobalProgressOrNull, + /* Start */ ProportionValue::MakeInvalid(), + /* Multiplier */ ProportionValue{0.0}, + aLocationOrNullEmptyToIgnoreAtStart, + aLocationOrNullEmptyToIgnoreAtEnd}; + } +#ifdef DEBUG_PROCESSLOGGER + if (mGlobalProgressOrNull) { + printf("[%d] * Sub: local [(here), %.0f%%] ~ global [%.2f%%, %.2f%%]\n", + int(baseprofiler::profiler_current_process_id().ToNumber()), + aSubEndInLocalSpace.ToDouble() * 100.0, + subStartInGlobalSpace.ToDouble() * 100.0, + subEndInGlobalSpace.ToDouble() * 100.0); + } +#endif // DEBUG_PROCESSLOGGER + return ProgressLogger{ + mGlobalProgressOrNull, + /* Start */ subStartInGlobalSpace, + /* Multiplier */ subEndInGlobalSpace - subStartInGlobalSpace, + aLocationOrNullEmptyToIgnoreAtStart, aLocationOrNullEmptyToIgnoreAtEnd}; + } + + // Helper using the current progress as start, no start location. + [[nodiscard]] ProgressLogger CreateSubLoggerTo( + ProportionValue aSubEndInLocalSpace, + const char* aLocationOrNullEmptyToIgnoreAtEnd = NO_LOCATION_UPDATE) { + return CreateSubLoggerTo(NO_LOCATION_UPDATE, aSubEndInLocalSpace, + aLocationOrNullEmptyToIgnoreAtEnd); + } + + class IndexAndProgressLoggerRange; + + [[nodiscard]] inline IndexAndProgressLoggerRange CreateLoopSubLoggersFromTo( + ProportionValue aLoopStartInLocalSpace, + ProportionValue aLoopEndInLocalSpace, uint32_t aLoopCount, + const char* aLocationOrNullEmptyToIgnoreAtEdges = + ProgressLogger::NO_LOCATION_UPDATE); + [[nodiscard]] inline IndexAndProgressLoggerRange CreateLoopSubLoggersTo( + ProportionValue aLoopEndInLocalSpace, uint32_t aLoopCount, + const char* aLocationOrNullEmptyToIgnoreAtEdges = + ProgressLogger::NO_LOCATION_UPDATE); + + private: + // All constructions start at the local 0%. + ProgressLogger(RefPtr<SharedProgress> aGlobalProgressOrNull, + ProportionValue aLocalStartInGlobalSpace, + ProportionValue aLocalToGlobalMultiplier, + const char* aLocationOrNullEmptyToIgnoreAtConstruction, + const char* aLocationOrNullEmptyToIgnoreAtDestruction) + : mGlobalProgressOrNull(std::move(aGlobalProgressOrNull)), + mLocalStartInGlobalSpace(aLocalStartInGlobalSpace), + mLocalToGlobalMultiplier(aLocalToGlobalMultiplier), + mLocationAtDestruction(aLocationOrNullEmptyToIgnoreAtDestruction) { + MOZ_ASSERT(!IsMovedFrom(), "Don't construct a moved-from object!"); + SetLocalProgress(ProportionValue{0.0}, + aLocationOrNullEmptyToIgnoreAtConstruction); + } + + void MarkMovedFrom() { + mLocalToGlobalMultiplier = ProportionValue::MakeInvalid(); + } + [[nodiscard]] bool IsMovedFrom() const { + return mLocalToGlobalMultiplier.IsInvalid(); + } + + [[nodiscard]] ProportionValue LocalToGlobal( + ProportionValue aLocalProgress) const { + return aLocalProgress * mLocalToGlobalMultiplier + mLocalStartInGlobalSpace; + } + + // Global progress value to update from local changes. + RefPtr<SharedProgress> mGlobalProgressOrNull; + + // How much to multiply and add to a local [0, 100%] value, to get the + // corresponding value in the global space. + // If mLocalToGlobalMultiplier is invalid, this ProgressLogger is moved-from, + // functions should not be used, and destructor won't update progress. + ProportionValue mLocalStartInGlobalSpace; + ProportionValue mLocalToGlobalMultiplier; + + const char* mLocationAtDestruction = nullptr; +}; + +// Helper class for range-for loop, e.g., with `aProgressLogger`: +// for (auto [index, loopProgressLogger] : +// IndexAndProgressLoggerRange{aProgressLogger, 30_pc, 50_pc, 10, +// "looping..."}) { +// // This will loop 10 times. +// // `index` is the loop index, from 0 to 9. +// // The overall loop will start at 30% and end at 50% of aProgressLogger. +// // `loopProgressLogger` is the progress logger for each iteration, +// // covering 1/10th of the range, therefore: [30%,32%], then [32%,34%], +// // etc. until [48%,50%]. +// // Progress is automatically updated before/after each loop. +// } +// Note that this implementation is single-threaded, it does not support logging +// progress from parallel loops. +class ProgressLogger::IndexAndProgressLoggerRange { + public: + struct IndexAndProgressLogger { + uint32_t index; + ProgressLogger progressLogger; + }; + + class IndexAndProgressLoggerEndIterator { + public: + explicit IndexAndProgressLoggerEndIterator(uint32_t aIndex) + : mIndex(aIndex) {} + + [[nodiscard]] uint32_t Index() const { return mIndex; } + + private: + uint32_t mIndex; + }; + + class IndexAndProgressLoggerIterator { + public: + IndexAndProgressLoggerIterator( + RefPtr<ProgressLogger::SharedProgress> aGlobalProgressOrNull, + ProportionValue aLoopStartInGlobalSpace, + ProportionValue aLoopIncrementInGlobalSpace, + const char* aLocationOrNullEmptyToIgnoreAtEdges) + : mGlobalProgressOrNull(aGlobalProgressOrNull), + mLoopStartInGlobalSpace(aLoopStartInGlobalSpace), + mLoopIncrementInGlobalSpace(aLoopIncrementInGlobalSpace), + mIndex(0u), + mLocationOrNullEmptyToIgnoreAtEdges( + aLocationOrNullEmptyToIgnoreAtEdges) { + if (mGlobalProgressOrNull) { + mGlobalProgressOrNull->SetProgress(mLoopStartInGlobalSpace, + mLocationOrNullEmptyToIgnoreAtEdges); + } + } + + [[nodiscard]] IndexAndProgressLogger operator*() { + return IndexAndProgressLogger{ + mIndex, + mGlobalProgressOrNull + ? ProgressLogger{mGlobalProgressOrNull, mLoopStartInGlobalSpace, + mLoopIncrementInGlobalSpace, + ProgressLogger::NO_LOCATION_UPDATE, + ProgressLogger::NO_LOCATION_UPDATE} + : ProgressLogger{}}; + } + + [[nodiscard]] bool operator!=( + const IndexAndProgressLoggerEndIterator& aEnd) const { + return mIndex != aEnd.Index(); + } + + IndexAndProgressLoggerIterator& operator++() { + ++mIndex; + mLoopStartInGlobalSpace = + mLoopStartInGlobalSpace + mLoopIncrementInGlobalSpace; + if (mGlobalProgressOrNull) { + mGlobalProgressOrNull->SetProgress(mLoopStartInGlobalSpace, + mLocationOrNullEmptyToIgnoreAtEdges); + } + return *this; + } + + private: + RefPtr<ProgressLogger::SharedProgress> mGlobalProgressOrNull; + ProportionValue mLoopStartInGlobalSpace; + ProportionValue mLoopIncrementInGlobalSpace; + uint32_t mIndex; + const char* mLocationOrNullEmptyToIgnoreAtEdges; + }; + + [[nodiscard]] IndexAndProgressLoggerIterator begin() { + return IndexAndProgressLoggerIterator{ + mGlobalProgressOrNull, mLoopStartInGlobalSpace, + mLoopIncrementInGlobalSpace, mLocationOrNullEmptyToIgnoreAtEdges}; + } + + [[nodiscard]] IndexAndProgressLoggerEndIterator end() { + return IndexAndProgressLoggerEndIterator{mLoopCount}; + } + + private: + friend class ProgressLogger; + IndexAndProgressLoggerRange(ProgressLogger& aProgressLogger, + ProportionValue aLoopStartInGlobalSpace, + ProportionValue aLoopEndInGlobalSpace, + uint32_t aLoopCount, + const char* aLocationOrNullEmptyToIgnoreAtEdges = + ProgressLogger::NO_LOCATION_UPDATE) + : mGlobalProgressOrNull(aProgressLogger.mGlobalProgressOrNull), + mLoopStartInGlobalSpace(aLoopStartInGlobalSpace), + mLoopIncrementInGlobalSpace( + (aLoopEndInGlobalSpace - aLoopStartInGlobalSpace) / aLoopCount), + mLoopCount(aLoopCount), + mLocationOrNullEmptyToIgnoreAtEdges( + aLocationOrNullEmptyToIgnoreAtEdges) {} + + RefPtr<ProgressLogger::SharedProgress> mGlobalProgressOrNull; + ProportionValue mLoopStartInGlobalSpace; + ProportionValue mLoopIncrementInGlobalSpace; + uint32_t mLoopCount; + const char* mLocationOrNullEmptyToIgnoreAtEdges; +}; + +[[nodiscard]] ProgressLogger::IndexAndProgressLoggerRange +ProgressLogger::CreateLoopSubLoggersFromTo( + ProportionValue aLoopStartInLocalSpace, + ProportionValue aLoopEndInLocalSpace, uint32_t aLoopCount, + const char* aLocationOrNullEmptyToIgnoreAtEdges) { + return IndexAndProgressLoggerRange{ + *this, LocalToGlobal(aLoopStartInLocalSpace), + LocalToGlobal(aLoopEndInLocalSpace), aLoopCount, + aLocationOrNullEmptyToIgnoreAtEdges}; +} + +[[nodiscard]] ProgressLogger::IndexAndProgressLoggerRange +ProgressLogger::CreateLoopSubLoggersTo( + ProportionValue aLoopEndInLocalSpace, uint32_t aLoopCount, + const char* aLocationOrNullEmptyToIgnoreAtEdges) { + return IndexAndProgressLoggerRange{ + *this, GetGlobalProgress(), LocalToGlobal(aLoopEndInLocalSpace), + aLoopCount, aLocationOrNullEmptyToIgnoreAtEdges}; +} + +} // namespace mozilla + +#endif // ProgressLogger_h diff --git a/mozglue/baseprofiler/public/ProportionValue.h b/mozglue/baseprofiler/public/ProportionValue.h new file mode 100644 index 0000000000..61eb2766ec --- /dev/null +++ b/mozglue/baseprofiler/public/ProportionValue.h @@ -0,0 +1,235 @@ +/* -*- 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/. */ + +#ifndef ProportionValue_h +#define ProportionValue_h + +#include "mozilla/Attributes.h" + +#include <algorithm> +#include <limits> + +namespace mozilla { + +// Class storing a proportion value between 0 and 1, effectively 0% to 100%. +// The public interface deals with doubles, but internally the value is encoded +// in an integral type, so arithmetic operations are fast. +// It also supports an invalid value: Use MakeInvalid() to construct, it infects +// any operation, and gets converted to a signaling NaN. +class ProportionValue { + public: + using UnderlyingType = uint32_t; + + // Default-construct at 0%. + constexpr ProportionValue() + // This `noexcept` is necessary to avoid a build error when encapsulating + // `ProportionValue` in `std::Atomic`: + // "use of deleted function + // 'constexpr std::atomic<mozilla::ProportionValue>::atomic()" + // because the default `std::atomic<T>::atomic()` constructor is marked: + // `noexcept(std::is_nothrow_default_constructible_v<T>)` + // and therefore this default constructor here must be explicitly marked + // `noexcept` as well. + noexcept + : mIntegralValue(0u) {} + + // Construct a ProportionValue with the given value, clamped to 0..1. + // Note that it's constexpr, so construction from literal numbers should incur + // no runtime costs. + // If `aValue` is NaN, behavior is undefined! Use `MakeInvalid()` instead. + constexpr explicit ProportionValue(double aValue) + : mIntegralValue(UnderlyingType(std::clamp(aValue, 0.0, 1.0) * scMaxD)) {} + + [[nodiscard]] static constexpr ProportionValue MakeInvalid() { + return ProportionValue(scInvalidU, Internal{}); + } + + [[nodiscard]] constexpr double ToDouble() const { + return IsInvalid() ? std::numeric_limits<double>::signaling_NaN() + : (double(mIntegralValue) * scInvMaxD); + } + + // Retrieve the underlying integral value, for storage or testing purposes. + [[nodiscard]] constexpr UnderlyingType ToUnderlyingType() const { + return mIntegralValue; + }; + + // Re-construct a ProportionValue from an underlying integral value. + [[nodiscard]] static constexpr ProportionValue FromUnderlyingType( + UnderlyingType aUnderlyingType) { + return ProportionValue( + (aUnderlyingType <= scMaxU) ? aUnderlyingType : scInvalidU, Internal{}); + } + + [[nodiscard]] constexpr bool IsExactlyZero() const { + return mIntegralValue == 0u; + } + + [[nodiscard]] constexpr bool IsExactlyOne() const { + return mIntegralValue == scMaxU; + } + + [[nodiscard]] constexpr bool IsValid() const { + // Compare to the maximum value, not just exactly scInvalidU, to catch any + // kind of invalid state. + return mIntegralValue <= scMaxU; + } + [[nodiscard]] constexpr bool IsInvalid() const { + // Compare to the maximum value, not just exactly scInvalidU, to catch any + // kind of invalid state. + return mIntegralValue > scMaxU; + } + + // Strict comparisons based on the underlying integral value. Use + // `CompareWithin` instead to make fuzzy comparisons. + // `ProportionValue::MakeInvalid()`s are equal, and greater than anything + // else; Best to avoid comparisons, and first use IsInvalid() instead. +#define OPERATOR_COMPARISON(CMP) \ + [[nodiscard]] constexpr friend bool operator CMP( \ + const ProportionValue& aLHS, const ProportionValue& aRHS) { \ + return aLHS.mIntegralValue CMP aRHS.mIntegralValue; \ + } + OPERATOR_COMPARISON(==) + OPERATOR_COMPARISON(!=) + OPERATOR_COMPARISON(<) + OPERATOR_COMPARISON(<=) + OPERATOR_COMPARISON(>) + OPERATOR_COMPARISON(>=) +#undef OPERATOR_COMPARISON + + // Arithmetic operations + - *, all working on the underlying integral values + // (i.e, no expensive floating-point operations are used), and always clamping + // to 0..1 range. Invalid values are poisonous. + + [[nodiscard]] constexpr ProportionValue operator+( + ProportionValue aRHS) const { + return ProportionValue( + (IsInvalid() || aRHS.IsInvalid()) + ? scInvalidU + // Adding fixed-point values keep the same scale, so there is no + // adjustment needed for that. [0,1]+[0,1]=[0,2], so we only need to + // ensure that the result is capped at max 1, aka scMaxU: + // a+b<=max <=> b<=max-a, so b is at maximum max-a. + : (mIntegralValue + + std::min(aRHS.mIntegralValue, scMaxU - mIntegralValue)), + Internal{}); + } + + [[nodiscard]] constexpr ProportionValue operator-( + ProportionValue aRHS) const { + return ProportionValue( + (IsInvalid() || aRHS.IsInvalid()) + ? scInvalidU + // Subtracting fixed-point values keep the same scale, so there is + // no adjustment needed for that. [0,1]-[0,1]=[-1,1], so we only + // need to ensure that the value is positive: + // a-b>=0 <=> b<=a, so b is at maximum a. + : (mIntegralValue - std::min(aRHS.mIntegralValue, mIntegralValue)), + Internal{}); + } + + [[nodiscard]] constexpr ProportionValue operator*( + ProportionValue aRHS) const { + // Type to hold the full result of multiplying two maximum numbers. + using DoublePrecisionType = uint64_t; + static_assert(sizeof(DoublePrecisionType) >= 2 * sizeof(UnderlyingType)); + return ProportionValue( + (IsInvalid() || aRHS.IsInvalid()) + ? scInvalidU + // Multiplying fixed-point values doubles the scale (2^31 -> 2^62), + // so we need to adjust the result by dividing it by one scale + // (which is optimized into a binary right-shift). + : (UnderlyingType((DoublePrecisionType(mIntegralValue) * + DoublePrecisionType(aRHS.mIntegralValue)) / + DoublePrecisionType(scMaxU))), + Internal{}); + } + + // Explicitly forbid divisions, they make little sense, and would almost + // always return a clamped 100% (E.g.: 50% / 10% = 0.5 / 0.1 = 5 = 500%). + [[nodiscard]] constexpr ProportionValue operator/( + ProportionValue aRHS) const = delete; + + // Division by a positive integer value, useful to split an interval in equal + // parts (with maybe some spare space at the end, because it is rounded down). + // Division by 0 produces an invalid value. + [[nodiscard]] constexpr ProportionValue operator/(uint32_t aDivisor) const { + return ProportionValue((IsInvalid() || aDivisor == 0u) + ? scInvalidU + : (mIntegralValue / aDivisor), + Internal{}); + } + + // Multiplication by a positive integer value, useful as inverse of the + // integer division above. But it may be lossy because the division is rounded + // down, therefore: PV - u < (PV / u) * u <= PV. + // Clamped to 100% max. + [[nodiscard]] constexpr ProportionValue operator*( + uint32_t aMultiplier) const { + return ProportionValue(IsInvalid() + ? scInvalidU + : ((aMultiplier > scMaxU / mIntegralValue) + ? scMaxU + : (mIntegralValue * aMultiplier)), + Internal{}); + } + + private: + // Tagged constructor for internal construction from the UnderlyingType, so + // that it is never ambiguously considered in constructions from one number. + struct Internal {}; + constexpr ProportionValue(UnderlyingType aIntegralValue, Internal) + : mIntegralValue(aIntegralValue) {} + + // Use all but 1 bit for the fractional part. + // Valid values can go from 0b0 (0%) up to 0b1000...00 (scMaxU aka 100%). + static constexpr unsigned scFractionalBits = sizeof(UnderlyingType) * 8 - 1; + // Maximum value corresponding to 1.0 or 100%. + static constexpr UnderlyingType scMaxU = UnderlyingType(1u) + << scFractionalBits; + // This maximum value corresponding to 1.0 can also be seen as the scaling + // factor from any [0,1] `double` value to the internal integral value. + static constexpr double scMaxD = double(scMaxU); + // The inverse can be used to convert the internal value back to [0,1]. + static constexpr double scInvMaxD = 1.0 / scMaxD; + + // Special value outside [0,max], used to construct invalid values. + static constexpr UnderlyingType scInvalidU = ~UnderlyingType(0u); + + // Internal integral value, guaranteed to always be <= scMaxU, or scInvalidU. + // This is effectively a fixed-point value using 1 bit for the integer part + // and 31 bits for the fractional part. + // It is roughly equal to the `double` value [0,1] multiplied by scMaxD. + UnderlyingType mIntegralValue; +}; + +namespace literals { +inline namespace ProportionValue_literals { + +// User-defined literal for integer percentages, e.g.: `10_pc`, `100_pc` +// (equivalent to `ProportionValue{0.1}` and `ProportionValue{1.0}`). +// Clamped to [0, 100]_pc. +[[nodiscard]] constexpr ProportionValue operator""_pc( + unsigned long long int aPercentage) { + return ProportionValue{ + double(std::clamp<unsigned long long int>(aPercentage, 0u, 100u)) / + 100.0}; +} + +// User-defined literal for non-integer percentages, e.g.: `12.3_pc`, `100.0_pc` +// (equivalent to `ProportionValue{0.123}` and `ProportionValue{1.0}`). +// Clamped to [0.0, 100.0]_pc. +[[nodiscard]] constexpr ProportionValue operator""_pc(long double aPercentage) { + return ProportionValue{ + double(std::clamp<long double>(aPercentage, 0.0, 100.0)) / 100.0}; +} + +} // namespace ProportionValue_literals +} // namespace literals + +} // namespace mozilla + +#endif // ProportionValue_h diff --git a/mozglue/baseprofiler/public/leb128iterator.h b/mozglue/baseprofiler/public/leb128iterator.h new file mode 100644 index 0000000000..636baf916f --- /dev/null +++ b/mozglue/baseprofiler/public/leb128iterator.h @@ -0,0 +1,207 @@ +/* -*- Mode: C++; tab-width: 2; 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/. */ + +// LEB128 utilities that can read/write unsigned LEB128 numbers from/to +// iterators. +// +// LEB128 = Little Endian Base 128, where small numbers take few bytes, but +// large numbers are still allowed, which is ideal when serializing numbers that +// are likely to be small. +// Each byte contains 7 bits from the number, starting at the "little end", the +// top bit is 0 for the last byte, 1 otherwise. +// Numbers 0-127 only take 1 byte. 128-16383 take 2 bytes. Etc. +// +// Iterators only need to provide: +// - `*it` to return a reference to the next byte to be read from or written to. +// - `++it` to advance the iterator after a byte is written. +// +// The caller must always provide sufficient space to write any number, by: +// - pre-allocating a large enough buffer, or +// - allocating more space when `++it` reaches the end and/or `*it` is invoked +// after the end, or +// - moving the underlying pointer to an appropriate location (e.g., wrapping +// around a circular buffer). +// The caller must also provide enough bytes to read a full value (i.e., at +// least one byte should have its top bit unset), and a type large enough to +// hold the stored value. +// +// Note: There are insufficient checks for validity! These functions are +// intended to be used together, i.e., the user should only `ReadULEB128()` from +// a sufficiently-large buffer that the same user filled with `WriteULEB128()`. +// Using with externally-sourced data (e.g., DWARF) is *not* recommended. +// +// https://en.wikipedia.org/wiki/LEB128 + +#ifndef leb128iterator_h +#define leb128iterator_h + +#include "mozilla/Assertions.h" +#include "mozilla/Likely.h" + +#include <climits> +#include <cstdint> +#include <limits> +#include <type_traits> + +namespace mozilla { + +// Number of bytes needed to represent `aValue`. +template <typename T> +constexpr uint_fast8_t ULEB128Size(T aValue) { + static_assert(!std::numeric_limits<T>::is_signed, + "ULEB128Size only takes unsigned types"); + // We need one output byte per 7 bits of non-zero value. So we just remove + // 7 least significant bits at a time until the value becomes zero. + // Note the special case of 0, which still needs 1 output byte; this is done + // by starting the first loop before we check for 0. + uint_fast8_t size = 0; + for (;;) { + size += 1; + aValue >>= 7; + // Expecting small values, so it should be more likely that `aValue == 0`. + if (MOZ_LIKELY(aValue == 0)) { + return size; + } + } +} + +// Maximum number of bytes needed to represent any value of type `T`. +template <typename T> +constexpr uint_fast8_t ULEB128MaxSize() { + return ULEB128Size<T>(std::numeric_limits<T>::max()); +} + +// Write `aValue` in LEB128 to `aIterator`. +// The iterator will be moved past the last byte. +template <typename T, typename It> +void WriteULEB128(T aValue, It& aIterator) { + static_assert(!std::numeric_limits<T>::is_signed, + "WriteULEB128 only takes unsigned types"); + using IteratorValue = std::remove_reference_t<decltype(*aIterator)>; + static_assert(sizeof(IteratorValue) == 1, + "WriteULEB128 expects an iterator to single bytes"); + // 0. Don't test for 0 yet, as we want to output one byte for it. + for (;;) { + // 1. Extract the 7 least significant bits. + const uint_fast8_t byte = aValue & 0x7Fu; + // 2. Remove them from `aValue`. + aValue >>= 7; + // 3. Write the 7 bits, and set the 8th bit if `aValue` is not 0 yet + // (meaning there will be more bytes after this one.) + // Expecting small values, so it should be more likely that `aValue == 0`. + // Note: No absolute need to force-cast to IteratorValue, because we have + // only changed the bottom 8 bits above. However the compiler could warn + // about a narrowing conversion from potentially-multibyte uint_fast8_t down + // to whatever single-byte type `*iterator* expects, so we make it explicit. + *aIterator = static_cast<IteratorValue>( + MOZ_LIKELY(aValue == 0) ? byte : (byte | 0x80u)); + // 4. Always advance the iterator to the next byte. + ++aIterator; + // 5. We're done if `aValue` is 0. + // Expecting small values, so it should be more likely that `aValue == 0`. + if (MOZ_LIKELY(aValue == 0)) { + return; + } + } +} + +// Read an LEB128 value from `aIterator`. +// The iterator will be moved past the last byte. +template <typename T, typename It> +T ReadULEB128(It& aIterator) { + static_assert(!std::numeric_limits<T>::is_signed, + "ReadULEB128 must return an unsigned type"); + using IteratorValue = std::remove_reference_t<decltype(*aIterator)>; + static_assert(sizeof(IteratorValue) == 1, + "ReadULEB128 expects an iterator to single bytes"); + // Incoming bits will be added to `result`... + T result = 0; + // ... starting with the least significant bits. + uint_fast8_t shift = 0; + for (;;) { + // 1. Read one byte from the iterator. + // `static_cast` just in case IteratorValue is not implicitly convertible to + // uint_fast8_t. It wouldn't matter if the sign was extended, we're only + // dealing with the bottom 8 bits below. + const uint_fast8_t byte = static_cast<uint_fast8_t>(*aIterator); + // 2. Always advance the iterator. + ++aIterator; + // 3. Extract the 7 bits of value, and shift them in place into `result`. + result |= static_cast<T>(byte & 0x7fu) << shift; + // 4. If the 8th bit is *not* set, this was the last byte. + // Expecting small values, so it should be more likely that the bit is off. + if (MOZ_LIKELY((byte & 0x80u) == 0)) { + return result; + } + // There are more bytes to read. + // 5. Next byte will contain more significant bits above the past 7. + shift += 7; + // Safety check that we're not going to shift by >= than the type size, + // which is Undefined Behavior in C++. + MOZ_ASSERT(shift < CHAR_BIT * sizeof(T)); + } +} + +// constexpr ULEB128 reader class. +// Mostly useful when dealing with non-trivial byte feeds. +template <typename T> +class ULEB128Reader { + static_assert(!std::numeric_limits<T>::is_signed, + "ULEB128Reader must handle an unsigned type"); + + public: + constexpr ULEB128Reader() = default; + + // Don't allow copy/assignment, it doesn't make sense for a stateful parser. + constexpr ULEB128Reader(const ULEB128Reader&) = delete; + constexpr ULEB128Reader& operator=(const ULEB128Reader&) = delete; + + // Feed a byte into the parser. + // Returns true if this was the last byte. + [[nodiscard]] constexpr bool FeedByteIsComplete(unsigned aByte) { + MOZ_ASSERT(!IsComplete()); + // Extract the 7 bits of value, and shift them in place into the value. + mValue |= static_cast<T>(aByte & 0x7fu) << mShift; + // If the 8th bit is *not* set, this was the last byte. + // Expecting small values, so it should be more likely that the bit is off. + if (MOZ_LIKELY((aByte & 0x80u) == 0)) { + mShift = mCompleteShift; + return true; + } + // There are more bytes to read. + // Next byte will contain more significant bits above the past 7. + mShift += 7; + // Safety check that we're not going to shift by >= than the type size, + // which is Undefined Behavior in C++. + MOZ_ASSERT(mShift < CHAR_BIT * sizeof(T)); + return false; + } + + constexpr void Reset() { + mValue = 0; + mShift = 0; + } + + [[nodiscard]] constexpr bool IsComplete() const { + return mShift == mCompleteShift; + } + + [[nodiscard]] constexpr T Value() const { + MOZ_ASSERT(IsComplete()); + return mValue; + } + + private: + // Special value of `mShift` indicating that parsing is complete. + constexpr static unsigned mCompleteShift = 0x10000u; + + T mValue = 0; + unsigned mShift = 0; +}; + +} // namespace mozilla + +#endif // leb128iterator_h diff --git a/mozglue/build/AsanOptions.cpp b/mozglue/build/AsanOptions.cpp new file mode 100644 index 0000000000..a96e6bd840 --- /dev/null +++ b/mozglue/build/AsanOptions.cpp @@ -0,0 +1,197 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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 "mozilla/Attributes.h" + +#ifndef _MSC_VER // Not supported by clang-cl yet + +// When running with AddressSanitizer, we need to explicitly set some +// options specific to our codebase to prevent errors during runtime. +// To override these, set the ASAN_OPTIONS environment variable. +// +// Currently, these are: +// +// allow_user_segv_handler=1 - Tell ASan to allow our code to use its +// own SIGSEGV handlers. This is required by ASM.js internally. +// +// alloc_dealloc_mismatch=0 - Disable alloc-dealloc mismatch checking +// in ASan. This is required because we define our own new/delete +// operators that are backed by malloc/free. If one of them gets inlined +// while the other doesn't, ASan will report false positives. +// +// detect_leaks=0 - Disable LeakSanitizer. This is required because +// otherwise leak checking will be enabled for various building and +// testing executables where we don't care much about leaks. +// +// allocator_may_return_null=1 - Tell ASan to return NULL when an allocation +// fails instead of aborting the program. This allows us to handle failing +// allocations the same way we would handle them with a regular allocator and +// also uncovers potential bugs that might occur in these situations. +// +// max_malloc_fill_size - Tell ASan to initialize memory to a certain value +// when it is allocated. This option specifies the maximum allocation size +// for which ASan should still initialize the memory. The value we specify +// here is exactly 256MiB. +// +// max_free_fill_size - Similar to max_malloc_fill_size, tell ASan to +// overwrite memory with a certain value when it is freed. Again, the value +// here specifies the maximum allocation size, larger allocations will +// skipped. +// +// malloc_fill_byte / free_fill_byte - These values specify the byte values +// used to initialize/overwrite memory in conjunction with the previous +// options max_malloc_fill_size and max_free_fill_size. The values used here +// are 0xe4 and 0xe5 to match the kAllocPoison and kAllocJunk constants used +// by mozjemalloc. +// +// malloc_context_size - This value specifies how many stack frames are +// stored for each malloc and free call. Since Firefox can have lots of deep +// stacks with allocations, we limit the default size here further to save +// some memory. +// +// fast_unwind_on_check - Use the fast (frame-pointer-based) stack unwinder +// for internal CHECK failures. The slow unwinder doesn't work on Android. +// +// fast_unwind_on_fatal - Use the fast (frame-pointer-based) stack unwinder +// to print fatal error reports. The slow unwinder doesn't work on Android. +// +// detect_stack_use_after_return=0 - Work around bug 1768099. +// +// intercept_tls_get_addr=0 - Work around +// https://github.com/google/sanitizers/issues/1322 (bug 1635327). +// +// !! Note: __asan_default_options is not used on Android! (bug 1576213) +// These should be updated in: +// mobile/android/geckoview/src/asan/resources/lib/*/wrap.sh +// +extern "C" MOZ_ASAN_IGNORE const char* __asan_default_options() { + return "allow_user_segv_handler=1:alloc_dealloc_mismatch=0:detect_leaks=0" +# ifdef MOZ_ASAN_REPORTER + ":malloc_context_size=20" +# endif +# ifdef __ANDROID__ + ":fast_unwind_on_check=1:fast_unwind_on_fatal=1" +# endif + ":max_free_fill_size=268435456:max_malloc_fill_size=268435456" + ":malloc_fill_byte=228:free_fill_byte=229" + ":handle_sigill=1" + ":allocator_may_return_null=1" + ":detect_stack_use_after_return=0" + ":intercept_tls_get_addr=0"; +} + +// !!! Please do not add suppressions for new leaks in Gecko code, unless they +// are intentional !!! +extern "C" const char* __lsan_default_suppressions() { + return "# Add your suppressions below\n" + + // LSan runs with a shallow stack depth and no debug symbols, so some + // small intentional leaks in system libraries show up with this. You + // do not want this enabled when running locally with a deep stack, as + // it can catch too much. + "leak:libc.so\n" + + // nsComponentManagerImpl intentionally leaks factory entries, and + // probably some other stuff. + "leak:nsComponentManagerImpl\n" + + // Bug 981220 - Pixman fails to free TLS memory. + "leak:pixman_implementation_lookup_composite\n" + + // Bug 987918 - Font shutdown leaks when CLEANUP_MEMORY is not enabled. + "leak:libfontconfig.so\n" + "leak:libfreetype.so\n" + "leak:GI___strdup\n" + // The symbol is really __GI___strdup, but if you have the leading _, + // it doesn't suppress it. + + // xdg_mime_init() is leaked by Gtk3 library + "leak:xdg_mime_init\n" + + // Bug 1078015 - If the process terminates during a PR_Sleep, LSAN + // detects a leak + "leak:PR_Sleep\n" + + // Bug 1363976 - Stylo holds some global data alive forever. + "leak:style::global_style_data\n" + "leak:style::sharing::SHARING_CACHE_KEY\n" + "leak:style::bloom::BLOOM_KEY\n" + + // + // Many leaks only affect some test suites. The suite annotations are + // not checked. + // + + // Bug 979928 - WebRTC leaks in different mochitest suites. + "leak:NR_reg_init\n" + // nr_reg_local_init should be redundant with NR_reg_init, but on + // Aurora we get fewer stack frames for some reason. + "leak:nr_reg_local_init\n" + "leak:r_log_register\n" + "leak:nr_reg_set\n" + + // This is a one-time leak in mochitest-bc, so it is probably okay to + // ignore. + "leak:GlobalPrinters::InitializeGlobalPrinters\n" + "leak:nsPSPrinterList::GetPrinterList\n" + + // Bug 1028456 - Various NSPR fd-related leaks in different mochitest + // suites. + "leak:_PR_Getfd\n" + + // Bug 1028483 - The XML parser sometimes leaks an object. Mostly + // happens in toolkit/components/thumbnails. + "leak:processInternalEntity\n" + + // Bug 1187421 - NSS does not always free the error stack in different + // mochitest suites. + "leak:nss_ClearErrorStack\n" + + // Bug 1602689 - leak at mozilla::NotNull, RacyRegisteredThread, + // RegisteredThread::RegisteredThread, mozilla::detail::UniqueSelector + "leak:RegisteredThread::RegisteredThread\n" + + // + // Leaks with system libraries in their stacks. These show up across a + // number of tests. Better symbols and disabling fast stackwalking may + // help diagnose these. + // + "leak:libcairo.so\n" + // https://github.com/OpenPrinting/cups/pull/317 + "leak:libcups.so\n" + "leak:libdl.so\n" + "leak:libdricore.so\n" + "leak:libdricore9.2.1.so\n" + "leak:libGL.so\n" + "leak:libEGL_mesa.so\n" + "leak:libglib-2.0.so\n" + "leak:libglsl.so\n" + "leak:libp11-kit.so\n" + "leak:libpixman-1.so\n" + "leak:libpulse.so\n" + // lubpulsecommon 1.1 is Ubuntu 12.04 + "leak:libpulsecommon-1.1.so\n" + // lubpulsecommon 1.1 is Ubuntu 16.04 + "leak:libpulsecommon-8.0.so\n" + "leak:libresolv.so\n" + "leak:libstdc++.so\n" + "leak:libXrandr.so\n" + "leak:libX11.so\n" + "leak:pthread_setspecific_internal\n" + "leak:swrast_dri.so\n" + + "leak:js::frontend::BytecodeEmitter:\n" + "leak:js::frontend::GeneralParser\n" + "leak:js::frontend::Parse\n" + "leak:xpc::CIGSHelper\n" + "leak:mozJSModuleLoader\n" + "leak:mozilla::xpcom::ConstructJSMComponent\n" + "leak:XPCWrappedNativeJSOps\n" + + // End of suppressions. + ; // Please keep this semicolon. +} + +#endif // _MSC_VER diff --git a/mozglue/build/BionicGlue.cpp b/mozglue/build/BionicGlue.cpp new file mode 100644 index 0000000000..8cc21b54e5 --- /dev/null +++ b/mozglue/build/BionicGlue.cpp @@ -0,0 +1,37 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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 <sys/types.h> +#include <unistd.h> +#include <sys/syscall.h> + +#define NS_EXPORT __attribute__((visibility("default"))) + +extern "C" NS_EXPORT int raise(int sig) { + // Bug 741272: Bionic incorrectly uses kill(), which signals the + // process, and thus could signal another thread (and let this one + // return "successfully" from raising a fatal signal). + // + // Bug 943170: POSIX specifies pthread_kill(pthread_self(), sig) as + // equivalent to raise(sig), but Bionic also has a bug with these + // functions, where a forked child will kill its parent instead. + + extern pid_t gettid(void); + return syscall(__NR_tgkill, getpid(), gettid(), sig); +} + +/* Flash plugin uses symbols that are not present in Android >= 4.4 */ +namespace android { +namespace VectorImpl { +NS_EXPORT void reservedVectorImpl1(void) {} +NS_EXPORT void reservedVectorImpl2(void) {} +NS_EXPORT void reservedVectorImpl3(void) {} +NS_EXPORT void reservedVectorImpl4(void) {} +NS_EXPORT void reservedVectorImpl5(void) {} +NS_EXPORT void reservedVectorImpl6(void) {} +NS_EXPORT void reservedVectorImpl7(void) {} +NS_EXPORT void reservedVectorImpl8(void) {} +} // namespace VectorImpl +} // namespace android diff --git a/mozglue/build/Makefile.in b/mozglue/build/Makefile.in new file mode 100644 index 0000000000..558ceba066 --- /dev/null +++ b/mozglue/build/Makefile.in @@ -0,0 +1,15 @@ +# +# 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/. + +# For FORCE_SHARED_LIB +include $(topsrcdir)/config/config.mk + +ifeq (WINNT,$(OS_TARGET)) +# Rebuild mozglue.dll if the manifest changes - it's included by mozglue.rc. +# (this dependency should really be just for mozglue.dll, not other targets) +# Note the manifest file exists in the tree, so we use the explicit filename +# here. +EXTRA_DEPS += $(srcdir)/mozglue.dll.manifest +endif diff --git a/mozglue/build/TsanOptions.cpp b/mozglue/build/TsanOptions.cpp new file mode 100644 index 0000000000..eab90d4351 --- /dev/null +++ b/mozglue/build/TsanOptions.cpp @@ -0,0 +1,314 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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 "mozilla/Attributes.h" +#include "mozilla/TsanOptions.h" + +#ifndef _MSC_VER // Not supported by clang-cl yet + +// +// When running with ThreadSanitizer, we sometimes need to suppress existing +// races. However, in any case, it should be either because +// +// 1) a bug is on file. In this case, the bug number should always be +// included with the suppression. +// +// or 2) this is an intentional race. Please be very careful with judging +// races as intentional and benign. Races in C++ are undefined behavior +// and compilers increasingly rely on exploiting this for optimizations. +// Hence, many seemingly benign races cause harmful or unexpected +// side-effects. +// +// See also: +// https://software.intel.com/en-us/blogs/2013/01/06/benign-data-races-what-could-possibly-go-wrong +// +// +// Also, when adding any race suppressions here, make sure to always add +// a signature for each of the two race stacks. Sometimes, TSan fails to +// symbolize one of the two traces and this can cause suppressed races to +// show up intermittently. +// +// clang-format off +extern "C" const char* __tsan_default_suppressions() { + return "# Add your suppressions below\n" + + // External uninstrumented libraries + MOZ_TSAN_DEFAULT_EXTLIB_SUPPRESSIONS + + // TSan internals + "race:__tsan::ProcessPendingSignals\n" + "race:__tsan::CallUserSignalHandler\n" + + + + + + // Uninstrumented code causing false positives + + // These libraries are uninstrumented and cause mutex false positives. + // However, they can be unloaded by GTK early which we cannot avoid. + "mutex:libGL.so\n" + "mutex:libGLdispatch\n" + "mutex:libGLX\n" + // Bug 1637707 - permanent + "mutex:libEGL_mesa.so\n" + // ~GLContextGLX unlocks a libGL mutex. + "mutex:GLContextGLX::~GLContextGLX\n" + // Bug 1825171 + "mutex:libffi.so\n" + "mutex:wl_registry_destroy\n" + // Bug 1651446 - permanent (ffmpeg) + "race:libavcodec.so*\n" + "race:libavutil.so*\n" + // For some reason, the suppressions on libpulse.so + // through `called_from_lib` only work partially. + "race:libpulse.so\n" + "race:pa_context_suspend_source_by_index\n" + "race:pa_context_unref\n" + "race:pa_format_info_set_prop_string_array\n" + "race:pa_stream_get_index\n" + "race:pa_stream_update_timing_info\n" + // This is a callback from libglib-2 that is apparently + // not fully suppressed through `called_from_lib`. + "race:g_main_context_dispatch\n" + // This is likely a false positive involving a mutex from GTK. + // See also bug 1642653 - permanent. + "mutex:GetMaiAtkType\n" + // Bug 1688716 - Failure due to fire_glxtest_process + // calling into uninstrumented external graphics driver code. + // For example: iris_dri.so and swrast_dri.so. + "race:fire_glxtest_process\n" + // Bug 1722721 - WebRender using uninstrumented Mesa drivers + "race:swrast_dri.so\n" + // Bug 1825171 + "race:libffi.so\n" + "race:mozilla::widget::WaylandBuffer::BufferReleaseCallbackHandler\n" + + + + + // Deadlock reports on single-threaded runtime. + // + // This is a known false positive from TSan where it reports + // a potential deadlock even though all mutexes are only + // taken by a single thread. For applications/tasks where we + // are absolutely sure that no second thread will be involved + // we should suppress these issues. + // + // See also https://github.com/google/sanitizers/issues/488 + + // Bug 1614605 - permanent + "deadlock:SanctionsTestServer\n" + "deadlock:OCSPStaplingServer\n" + // Bug 1643087 - permanent + "deadlock:BadCertAndPinningServer\n" + // Bug 1606804 - permanent + "deadlock:cert_storage::SecurityState::open_db\n" + "deadlock:cert_storage::SecurityState::add_certs\n" + // Bug 1651770 - permanent + "deadlock:mozilla::camera::LockAndDispatch\n" + // Bug 1606804 - permanent + "deadlock:third_party/rust/rkv/src/env.rs\n" + // Bug 1680655 - permanent + "deadlock:EncryptedClientHelloServer\n" + // Bug 1682861 - permanent + "deadlock:nsDOMWindowUtils::CompareCanvases\n" + + + + + + // Benign races in third-party code (should be fixed upstream) + + // No Bug - permanent + // No Upstream Bug Filed! + // + // SIMD Initialization in libjpeg, potentially runs + // initialization twice, but otherwise benign. Init + // routine itself is in native assembler. + "race:init_simd\n" + "race:simd_support\n" + "race:jsimd_can_ycc_rgb\n" + // Bug 1615228 - permanent + // No Upstream Bug Filed! + // + // Likely benign race in ipc/chromium/ where we set + // `message_loop_` to `NULL` on two threads when stopping + // a thread at the same time it is already finishing. + "race:base::Thread::Stop\n" + // Bug 1615569 - permanent + // No Upstream Bug Filed! + // + // NSS is using freebl from two different threads but freebl isn't + // that threadsafe. + "race:mp_exptmod.max_window_bits\n" + // Bug 1652499 - permanent + // No Upstream Bug Filed! + // + // Likely benign race in webrtc.org code - race while updating the + // minimum log severity. + "race:Loggable\n" + "race:UpdateMinLogSeverity\n" + // Bug 1652174 - permanent + // Upstream Bug: https://github.com/libevent/libevent/issues/777 + // + // Likely benign write-write race in libevent to set a sticky boolean + // flag to true. + "race:event_debug_mode_too_late\n" + + // Bug 1653618 - permanent + // Upstream Bug: https://github.com/sctplab/usrsctp/issues/507 + // + // Might lead to scheduled timers in libusrsctp getting dropped? + "race:sctp_handle_tick\n" + "race:sctp_handle_sack\n" + // Bug 1648604 - permanent + // Upstream Bug: https://github.com/sctplab/usrsctp/issues/482 + // + // Likely benign race in libusrsctp allocator during a free. + "race:system_base_info\n" + // Benign lock-order-inversion in libusrsctp + // No upstream bug filed! + "deadlock:sctp_add_to_readq\n" + + // Bug 1153409 - permanent + // No Upstream Bug Filed! + // + // Probably benign - sqlite has a few optimizations where it does + // racy reads and then does properly synchronized integrity checks + // afterwards. Some concern of compiler optimizations messing this + // up due to "volatile" being too weak for this. + "race:third_party/sqlite3/*\n" + "deadlock:third_party/sqlite3/*\n" + // Bug 1674770 - permanent + // Upstream Bug: https://github.com/Amanieu/parking_lot/issues/257 + // + // parking_lot using incorrect atomic orderings in RwLock, upstream + // fix already up for review. + "race:StrongRuleNode::ensure_child\n" + // No Bug - permanent + // Upstream Bugs: + // + // * https://github.com/rayon-rs/rayon/issues/812 + // * https://github.com/crossbeam-rs/crossbeam/issues/589 + // + // Probably a false-positive from crossbeam's deque not being + // understood by tsan. + "race:crossbeam_deque*::resize\n" + "race:crossbeam_deque*::push\n" + "race:crossbeam_deque*::write\n" + "race:crossbeam_deque*::read\n" + "race:crossbeam_deque*::steal\n" + // Bug 1805819 - permanent + // No Upstream Bug Filed! + // + // False positive in libc's tzset_internal + // See https://crbug.com/379738 also + "race:tzset_internal\n" + + + + + + // The rest of these suppressions are miscellaneous issues in gecko + // that should be investigated and ideally fixed. + + // Bug 1671574 - Permanent + // The StartupCache thread intentionally races with the main thread to + // trigger OS-level paging. It is never joined with the main thread. + "thread:StartupCache\n" + + // Bug 1734262 - Permanent + // When spawning async processes, we create a helper thread to wait for + // the process to terminate in order to asynchronously report the exit + // code to Gecko. This thread waits on a syscall for the process to end, + // which means there's no easy way to cancel and join it during Gecko + // shutdown. Suppress thread leak reports for this thread. + "thread:CreateMonitorThread\n" + + // Bug 1601600 + "race:SkARGB32_Blitter\n" + "race:SkARGB32_Shader_Blitter\n" + "race:SkARGB32_Opaque_Blitter\n" + "race:SkRasterPipelineBlitter\n" + "race:Clamp_S32_D32_nofilter_trans_shaderproc\n" + "race:SkSpriteBlitter_Memcpy\n" + + // Bug 1606800 + "race:CallInitFunc\n" + + // Bug 1606803 + "race:ipv6_is_present\n" + + // Bug 1615123 + "race:_dl_deallocate_tls\n" + "race:__libc_memalign\n" + + // Bug 1664803 + "race:Sampler::sSigHandlerCoordinator\n" + + // Bug 1656068 + "race:WebRtcAec_Create\n" + + // No Bug - Logging bug in Mochitests + "race:mochitest/ssltunnel/ssltunnel.cpp\n" + + // This thread does not seem to be stopped/joined. + // ImageBridgeChild should be turned back into a background + // task queue in bug 1647628, in which case these suppressions + // can be removed. + "race:mozilla::layers::ImageBridgeChild::ShutDown\n" + + // Bug 1652530 + "mutex:XErrorTrap\n" + + // Bug 1671601 + "race:CamerasParent::ActorDestroy\n" + "race:CamerasParent::DispatchToVideoCaptureThread\n" + + // Bug 1623541 + "race:VRShMem::PullSystemState\n" + "race:VRShMem::PushSystemState\n" + "race:VRShMem::PullBrowserState\n" + "race:VRShMem::PushBrowserState\n" + + // Bug 1682951 + "race:storage::Connection::Release\n" + + // Bug 1683357 + "race:image::ImageSurfaceCache::SuggestedSizeInternal\n" + "race:image::RasterImage::SetMetadata\n" + "race:image::RasterImage::GetWidth\n" + + // Bug 1722721 - This is a benign race creating worker/SW compositor threads. + "race:webrender::profiler::register_thread\n" + + // Bug 1722721 - This is a false positive during SW-WR rendering. + "race:scale_blit\n" + + "race:mozilla::gl::MesaMemoryLeakWorkaround\n" + + // Bug 1733908 + "race:js::wasm::Code::bestTier\n" + "race:js::wasm::Code::commitTier2\n" + "race:js::wasm::Code::setTier2\n" + "race:js::wasm::Code::setAndBorrowTier2\n" + + // Bug 1755449 + // The Glean init thread is used to perform I/O and other blocking operations. + // It is never joined with the main thread, but this is being re-evaluated. + "thread:glean::initialize\n" + + // Bug 1822605 - permanent + // A race exists in libvulkan_lvp.so. This was previously addressed in bug + // 1816713. However, libvulkan_lvp.so is unloaded so a called_from_lib + // suppression cannot be used. + "race:libvulkan_lvp.so\n" + + // End of suppressions. + ; // Please keep this semicolon. +} +// clang-format on +#endif // _MSC_VER diff --git a/mozglue/build/UbsanOptions.cpp b/mozglue/build/UbsanOptions.cpp new file mode 100644 index 0000000000..547baa790c --- /dev/null +++ b/mozglue/build/UbsanOptions.cpp @@ -0,0 +1,16 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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 "mozilla/Attributes.h" + +#ifndef _MSC_VER // Not supported by clang-cl yet + +extern "C" const char* __ubsan_default_options() { + return "print_stacktrace=1"; +} + +extern "C" const char* __ubsan_default_suppressions() { return ""; } + +#endif diff --git a/mozglue/build/arm-eabi-filter b/mozglue/build/arm-eabi-filter new file mode 100644 index 0000000000..401454ee88 --- /dev/null +++ b/mozglue/build/arm-eabi-filter @@ -0,0 +1,4 @@ +{ + local: + __aeabi*; +}; diff --git a/mozglue/build/arm.cpp b/mozglue/build/arm.cpp new file mode 100644 index 0000000000..a74a41f6c0 --- /dev/null +++ b/mozglue/build/arm.cpp @@ -0,0 +1,131 @@ +/* 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/. */ + +/* compile-time and runtime tests for whether to use various ARM extensions */ + +#include "arm.h" + +#if defined(MOZILLA_ARM_HAVE_CPUID_DETECTION) + +// arm.h has parallel #ifs which declare MOZILLA_ARM_HAVE_CPUID_DETECTION. +// We don't check it here so that we get compile errors if it's defined, but +// we don't compile one of these detection methods. The detection code here is +// based on the CPU detection in libtheora. + +# if defined(__linux__) || defined(ANDROID) +# include <stdio.h> +# include <stdlib.h> +# include <string.h> + +enum { + MOZILLA_HAS_EDSP_FLAG = 1, + MOZILLA_HAS_ARMV6_FLAG = 2, + MOZILLA_HAS_ARMV7_FLAG = 4, + MOZILLA_HAS_NEON_FLAG = 8 +}; + +static unsigned get_arm_cpu_flags(void) { + unsigned flags; + FILE* fin; + bool armv6_processor = false; + flags = 0; + /*Reading /proc/self/auxv would be easier, but that doesn't work reliably on + Android. This also means that detection will fail in Scratchbox, which is + desirable, as NEON does not work in the qemu shipped with the Maemo 5 SDK. + I don't know if /proc/self/auxv would do any better in that case, anyway, + or if it would return random flags from the host CPU.*/ + fin = fopen("/proc/cpuinfo", "r"); + if (fin != nullptr) { + /*512 should be enough for anybody (it's even enough for all the flags that + x86 has accumulated... so far).*/ + char buf[512]; + while (fgets(buf, 511, fin) != nullptr) { + if (memcmp(buf, "Features", 8) == 0) { + char* p; + p = strstr(buf, " edsp"); + if (p != nullptr && (p[5] == ' ' || p[5] == '\n')) + flags |= MOZILLA_HAS_EDSP_FLAG; + p = strstr(buf, " neon"); + if (p != nullptr && (p[5] == ' ' || p[5] == '\n')) + flags |= MOZILLA_HAS_NEON_FLAG; + } + if (memcmp(buf, "CPU architecture:", 17) == 0) { + int version; + version = atoi(buf + 17); + if (version >= 6) flags |= MOZILLA_HAS_ARMV6_FLAG; + if (version >= 7) flags |= MOZILLA_HAS_ARMV7_FLAG; + } + /* media/webrtc/trunk/src/system_wrappers/source/cpu_features_arm.c + * Unfortunately, it seems that certain ARMv6-based CPUs + * report an incorrect architecture number of 7! + * + * We try to correct this by looking at the 'elf_format' + * field reported by the 'Processor' field, which is of the + * form of "(v7l)" for an ARMv7-based CPU, and "(v6l)" for + * an ARMv6-one. + */ + if (memcmp(buf, "Processor\t:", 11) == 0) { + if (strstr(buf, "(v6l)") != 0) { + armv6_processor = true; + } + } + } + fclose(fin); + } + if (armv6_processor) { + // ARMv6 pretending to be ARMv7? clear flag + if (flags & MOZILLA_HAS_ARMV7_FLAG) { + flags &= ~MOZILLA_HAS_ARMV7_FLAG; + } + } + return flags; +} + +// Cache a local copy so we only have to read /proc/cpuinfo once. +static unsigned arm_cpu_flags = get_arm_cpu_flags(); + +# if !defined(MOZILLA_PRESUME_EDSP) +static bool check_edsp(void) { + return (arm_cpu_flags & MOZILLA_HAS_EDSP_FLAG) != 0; +} +# endif + +# if !defined(MOZILLA_PRESUME_ARMV6) +static bool check_armv6(void) { + return (arm_cpu_flags & MOZILLA_HAS_ARMV6_FLAG) != 0; +} +# endif + +# if !defined(MOZILLA_PRESUME_ARMV7) +static bool check_armv7(void) { + return (arm_cpu_flags & MOZILLA_HAS_ARMV7_FLAG) != 0; +} +# endif + +# if !defined(MOZILLA_PRESUME_NEON) +static bool check_neon(void) { + return (arm_cpu_flags & MOZILLA_HAS_NEON_FLAG) != 0; +} +# endif + +# endif // defined(__linux__) || defined(ANDROID) + +namespace mozilla { +namespace arm_private { +# if !defined(MOZILLA_PRESUME_EDSP) +bool edsp_enabled = check_edsp(); +# endif +# if !defined(MOZILLA_PRESUME_ARMV6) +bool armv6_enabled = check_armv6(); +# endif +# if !defined(MOZILLA_PRESUME_ARMV7) +bool armv7_enabled = check_armv7(); +# endif +# if !defined(MOZILLA_PRESUME_NEON) +bool neon_enabled = check_neon(); +# endif +} // namespace arm_private +} // namespace mozilla + +#endif // MOZILLA_ARM_HAVE_CPUID_DETECTION diff --git a/mozglue/build/arm.h b/mozglue/build/arm.h new file mode 100644 index 0000000000..8600329931 --- /dev/null +++ b/mozglue/build/arm.h @@ -0,0 +1,145 @@ +/* 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/. */ + +/* compile-time and runtime tests for whether to use SSE instructions */ + +#ifndef mozilla_arm_h_ +#define mozilla_arm_h_ + +// for definition of MFBT_DATA +#include "mozilla/Types.h" + +/* This is patterned after SSE.h, but provides ARMv5E, ARMv6, and NEON + detection. For reasons similar to the SSE code, code using NEON (even just + in inline asm) needs to be in a separate compilation unit from the regular + code, because it requires an ".fpu neon" directive which can't be undone. + ARMv5E and ARMv6 code may also require an .arch directive, since by default + the assembler refuses to generate code for opcodes outside of its current + .arch setting. + + TODO: Add Thumb, Thumb2, VFP, iwMMX, etc. detection, if we need it. */ + +#if defined(__GNUC__) && defined(__arm__) + +# define MOZILLA_ARM_ARCH 3 + +# if defined(__ARM_ARCH_4__) || defined(__ARM_ARCH_4T__) || \ + defined(_ARM_ARCH_4) +# undef MOZILLA_ARM_ARCH +# define MOZILLA_ARM_ARCH 4 +# endif + +# if defined(__ARM_ARCH_5__) || defined(__ARM_ARCH_5T__) || \ + defined(__ARM_ARCH_5E__) || defined(__ARM_ARCH_5TE__) || \ + defined(__ARM_ARCH_5TEJ__) || defined(_ARM_ARCH_5) +# undef MOZILLA_ARM_ARCH +# define MOZILLA_ARM_ARCH 5 +# endif + +# if defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || \ + defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6Z__) || \ + defined(__ARM_ARCH_6ZK__) || defined(__ARM_ARCH_6T2__) || \ + defined(__ARM_ARCH_6M__) || defined(_ARM_ARCH_6) +# undef MOZILLA_ARM_ARCH +# define MOZILLA_ARM_ARCH 6 +# endif + +# if defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) || \ + defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__) || \ + defined(__ARM_ARCH_7EM__) || defined(_ARM_ARCH_7) +# undef MOZILLA_ARM_ARCH +# define MOZILLA_ARM_ARCH 7 +# endif + +# ifdef __GNUC__ +# define MOZILLA_MAY_SUPPORT_EDSP 1 + +# if defined(HAVE_ARM_SIMD) +# define MOZILLA_MAY_SUPPORT_ARMV6 1 +# endif + +# if defined(HAVE_ARM_NEON) +# define MOZILLA_MAY_SUPPORT_NEON 1 +# endif + +# if defined(HAVE_ARM_SIMD) +# define MOZILLA_MAY_SUPPORT_ARMV7 1 +# endif +# endif + +// Currently we only have CPU detection for Linux via /proc/cpuinfo +# if defined(__linux__) || defined(ANDROID) +# define MOZILLA_ARM_HAVE_CPUID_DETECTION 1 +# endif + +#endif + +// When using -mfpu=neon on arm gcc, or using default on aarch64, +// the compiler generates neon instructions. +#if defined(__ARM_NEON) +# define MOZILLA_PRESUME_NEON 1 +#endif + +namespace mozilla { + +namespace arm_private { +#if defined(MOZILLA_ARM_HAVE_CPUID_DETECTION) +# if !defined(MOZILLA_PRESUME_EDSP) +extern bool MFBT_DATA edsp_enabled; +# endif +# if !defined(MOZILLA_PRESUME_ARMV6) +extern bool MFBT_DATA armv6_enabled; +# endif +# if !defined(MOZILLA_PRESUME_ARMV7) +extern bool MFBT_DATA armv7_enabled; +# endif +# if !defined(MOZILLA_PRESUME_NEON) +extern bool MFBT_DATA neon_enabled; +# endif +#endif +} // namespace arm_private + +#if defined(MOZILLA_PRESUME_EDSP) +# define MOZILLA_MAY_SUPPORT_EDSP 1 +inline bool supports_edsp() { return true; } +#elif defined(MOZILLA_MAY_SUPPORT_EDSP) && \ + defined(MOZILLA_ARM_HAVE_CPUID_DETECTION) +inline bool supports_edsp() { return arm_private::edsp_enabled; } +#else +inline bool supports_edsp() { return false; } +#endif + +#if defined(MOZILLA_PRESUME_ARMV6) +# define MOZILLA_MAY_SUPPORT_ARMV6 1 +inline bool supports_armv6() { return true; } +#elif defined(MOZILLA_MAY_SUPPORT_ARMV6) && \ + defined(MOZILLA_ARM_HAVE_CPUID_DETECTION) +inline bool supports_armv6() { return arm_private::armv6_enabled; } +#else +inline bool supports_armv6() { return false; } +#endif + +#if defined(MOZILLA_PRESUME_ARMV7) +# define MOZILLA_MAY_SUPPORT_ARMV7 1 +inline bool supports_armv7() { return true; } +#elif defined(MOZILLA_MAY_SUPPORT_ARMV7) && \ + defined(MOZILLA_ARM_HAVE_CPUID_DETECTION) +inline bool supports_armv7() { return arm_private::armv7_enabled; } +#else +inline bool supports_armv7() { return false; } +#endif + +#if defined(MOZILLA_PRESUME_NEON) +# define MOZILLA_MAY_SUPPORT_NEON 1 +inline bool supports_neon() { return true; } +#elif defined(MOZILLA_MAY_SUPPORT_NEON) && \ + defined(MOZILLA_ARM_HAVE_CPUID_DETECTION) +inline bool supports_neon() { return arm_private::neon_enabled; } +#else +inline bool supports_neon() { return false; } +#endif + +} // namespace mozilla + +#endif /* !defined(mozilla_arm_h_) */ diff --git a/mozglue/build/dummy.cpp b/mozglue/build/dummy.cpp new file mode 100644 index 0000000000..c6b1ccd808 --- /dev/null +++ b/mozglue/build/dummy.cpp @@ -0,0 +1,5 @@ +/* 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/. */ + +void _dummy(void) {} diff --git a/mozglue/build/mips.cpp b/mozglue/build/mips.cpp new file mode 100644 index 0000000000..7166080f5e --- /dev/null +++ b/mozglue/build/mips.cpp @@ -0,0 +1,42 @@ +/* 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/. */ + +/* compile-time and runtime tests for whether to use MIPS-specific extensions */ + +#include "mips.h" + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +enum { + MIPS_FLAG_LOONGSON3 = 1, +}; + +static unsigned get_mips_cpu_flags(void) { + unsigned flags = 0; + FILE* fin; + + fin = fopen("/proc/cpuinfo", "r"); + if (fin != nullptr) { + char buf[1024]; + memset(buf, 0, sizeof(buf)); + fread(buf, sizeof(char), sizeof(buf) - 1, fin); + fclose(fin); + if (strstr(buf, "Loongson-3")) flags |= MIPS_FLAG_LOONGSON3; + } + return flags; +} + +static bool check_loongson3(void) { + // Cache a local copy so we only have to read /proc/cpuinfo once. + static unsigned mips_cpu_flags = get_mips_cpu_flags(); + return (mips_cpu_flags & MIPS_FLAG_LOONGSON3) != 0; +} + +namespace mozilla { +namespace mips_private { +bool isLoongson3 = check_loongson3(); +} // namespace mips_private +} // namespace mozilla diff --git a/mozglue/build/mips.h b/mozglue/build/mips.h new file mode 100644 index 0000000000..a7c3ed5416 --- /dev/null +++ b/mozglue/build/mips.h @@ -0,0 +1,29 @@ +/* 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/. */ + +/* compile-time and runtime tests for whether to use MIPS-specific extensions */ + +#ifndef mozilla_mips_h_ +#define mozilla_mips_h_ + +// for definition of MFBT_DATA +#include "mozilla/Types.h" + +namespace mozilla { + +namespace mips_private { +extern bool MFBT_DATA isLoongson3; +} // namespace mips_private + +inline bool supports_mmi() { +#ifdef __mips__ + return mips_private::isLoongson3; +#else + return false; +#endif +} + +} // namespace mozilla + +#endif /* !defined(mozilla_mips_h_) */ diff --git a/mozglue/build/moz.build b/mozglue/build/moz.build new file mode 100644 index 0000000000..fe195739e2 --- /dev/null +++ b/mozglue/build/moz.build @@ -0,0 +1,115 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# 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/. + +# Build mozglue as a shared lib on Windows, OSX and Android. But not for +# embedders! +# If this is ever changed, update MOZ_SHARED_MOZGLUE in browser/installer/Makefile.in +if CONFIG["JS_STANDALONE"] and not CONFIG["MOZ_MEMORY"]: + Library("mozglue") +elif CONFIG["OS_TARGET"] in ("WINNT", "Darwin", "Android"): + SharedLibrary("mozglue") +else: + Library("mozglue") + +if CONFIG["OS_TARGET"] == "Android": + LDFLAGS += ["-Wl,--version-script,%s/mozglue.ver" % SRCDIR] + SOURCES += [ + "BionicGlue.cpp", + ] + +if CONFIG["MOZ_ASAN"]: + SOURCES += [ + "AsanOptions.cpp", + ] + +if CONFIG["MOZ_UBSAN"]: + SOURCES += [ + "UbsanOptions.cpp", + ] + +if CONFIG["MOZ_TSAN"]: + SOURCES += [ + "TsanOptions.cpp", + ] + +if CONFIG["OS_TARGET"] == "WINNT": + if CONFIG["MOZ_MEMORY"]: + DEFFILE = "mozglue.def" + OS_LIBS += [ + "advapi32", + "user32", + "winmm", + "uuid", + ] + # We'll break the DLL blocklist if we immediately load user32.dll. + # For the same reason, we delayload these other DLLs to avoid eager + # dependencies on user32.dll. + DELAYLOAD_DLLS += [ + "advapi32.dll", + "dbghelp.dll", + "oleaut32.dll", + "ole32.dll", + "user32.dll", + "version.dll", + "winmm.dll", + ] + +if CONFIG["MOZ_WIDGET_TOOLKIT"]: + + if CONFIG["MOZ_MEMORY"] and FORCE_SHARED_LIB: + pass + # TODO: SHARED_LIBRARY_LIBS go here + else: + # Temporary, until bug 662814 lands + NoVisibilityFlags() + SOURCES += [ + "dummy.cpp", + ] + + if CONFIG["OS_TARGET"] == "WINNT": + LOCAL_INCLUDES += [ + "/memory/build", + ] + + EXPORTS.mozilla += [ + "arm.h", + "mips.h", + "ppc.h", + ] + + if CONFIG["CPU_ARCH"] == "arm": + SOURCES += [ + "arm.cpp", + ] + + if CONFIG["CPU_ARCH"].startswith("mips"): + SOURCES += [ + "mips.cpp", + ] + + if CONFIG["CPU_ARCH"].startswith("ppc"): + SOURCES += [ + "ppc.cpp", + ] + + if CONFIG["MOZ_LINKER"]: + USE_LIBS += [ + "zlib", + ] + +USE_LIBS += [ + "mfbt", +] + +LIBRARY_DEFINES["IMPL_MFBT"] = True +LIBRARY_DEFINES["MOZ_HAS_MOZGLUE"] = True + +if CONFIG["MOZ_LINKER"] and CONFIG["CPU_ARCH"] == "arm": + LDFLAGS += ["-Wl,-version-script,%s/arm-eabi-filter" % SRCDIR] + +DIST_INSTALL = True + +include("replace_malloc.mozbuild") diff --git a/mozglue/build/mozglue.def b/mozglue/build/mozglue.def new file mode 100644 index 0000000000..188037b9f1 --- /dev/null +++ b/mozglue/build/mozglue.def @@ -0,0 +1,22 @@ +; 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/. + +LIBRARY mozglue.dll + +EXPORTS + ; symbols that are actually useful + malloc=je_malloc + calloc=je_calloc + realloc=je_realloc + free=je_free + posix_memalign=je_posix_memalign + malloc_usable_size=je_malloc_usable_size + malloc_good_size=je_malloc_good_size + _aligned_free=je_free + _aligned_malloc=wrap__aligned_malloc + strndup=wrap_strndup + strdup=wrap_strdup + _strdup=wrap_strdup + wcsdup=wrap_wcsdup + _wcsdup=wrap_wcsdup diff --git a/mozglue/build/mozglue.dll.manifest b/mozglue/build/mozglue.dll.manifest new file mode 100644 index 0000000000..037eae4f77 --- /dev/null +++ b/mozglue/build/mozglue.dll.manifest @@ -0,0 +1,9 @@ +<?xml version="1.0" encoding="UTF-8" standalone="yes"?> +<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> +<assemblyIdentity + version="1.0.0.0" + name="mozglue" + type="win32" +/> +<file name="mozglue.dll"/> +</assembly> diff --git a/mozglue/build/mozglue.ver b/mozglue/build/mozglue.ver new file mode 100644 index 0000000000..433d820b36 --- /dev/null +++ b/mozglue/build/mozglue.ver @@ -0,0 +1,4 @@ +libmozglue.so { +global: + *; +}; diff --git a/mozglue/build/ppc.cpp b/mozglue/build/ppc.cpp new file mode 100644 index 0000000000..20ef121386 --- /dev/null +++ b/mozglue/build/ppc.cpp @@ -0,0 +1,64 @@ +/* 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/. */ + +/* compile-time and runtime tests for whether to use Power ISA-specific + * extensions */ + +#include "ppc.h" +#include "mozilla/Unused.h" + +#include <stdio.h> +#include <stdlib.h> + +#if defined(XP_LINUX) +// Use the getauxval() function if available. +// ARCH_3_00 wasn't defined until glibc 2.23, so include just in case. +# include <sys/auxv.h> +# ifndef PPC_FEATURE2_ARCH_3_00 +# define PPC_FEATURE2_ARCH_3_00 0x00800000 +# endif +#endif + +const unsigned PPC_FLAG_VMX = 1; +const unsigned PPC_FLAG_VSX = 2; +const unsigned PPC_FLAG_VSX3 = 4; + +static signed get_ppc_cpu_flags(void) { + // This could be expensive, so cache the result. + static signed cpu_flags = -1; + + if (cpu_flags > -1) { // already checked + return cpu_flags; + } + cpu_flags = 0; + +#if defined(XP_LINUX) + // Try getauxval(). + unsigned long int cap = getauxval(AT_HWCAP); + unsigned long int cap2 = getauxval(AT_HWCAP2); + + if (cap & PPC_FEATURE_HAS_ALTIVEC) { + cpu_flags |= PPC_FLAG_VMX; + } + if (cap & PPC_FEATURE_HAS_VSX) { + cpu_flags |= PPC_FLAG_VSX; + } + if (cap2 & PPC_FEATURE2_ARCH_3_00) { + cpu_flags |= PPC_FLAG_VSX3; + } +#else + // Non-Linux detection here. Currently, on systems other than Linux, + // no CPU SIMD features will be detected. +#endif + + return cpu_flags; +} + +namespace mozilla { +namespace ppc_private { +bool vmx_enabled = !!(get_ppc_cpu_flags() & PPC_FLAG_VMX); +bool vsx_enabled = !!(get_ppc_cpu_flags() & PPC_FLAG_VSX); +bool vsx3_enabled = !!(get_ppc_cpu_flags() & PPC_FLAG_VSX3); +} // namespace ppc_private +} // namespace mozilla diff --git a/mozglue/build/ppc.h b/mozglue/build/ppc.h new file mode 100644 index 0000000000..9527d8dadd --- /dev/null +++ b/mozglue/build/ppc.h @@ -0,0 +1,47 @@ +/* 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/. */ + +/* compile-time and runtime tests for whether to use Power ISA-specific + extensions */ + +#ifndef mozilla_ppc_h_ +#define mozilla_ppc_h_ + +// for definition of MFBT_DATA +#include "mozilla/Types.h" + +namespace mozilla { +namespace ppc_private { +extern bool MFBT_DATA vmx_enabled; +extern bool MFBT_DATA vsx_enabled; +extern bool MFBT_DATA vsx3_enabled; +} // namespace ppc_private + +inline bool supports_vmx() { +#ifdef __powerpc__ + return ppc_private::vmx_enabled; +#else + return false; +#endif +} + +inline bool supports_vsx() { +#ifdef __powerpc__ + return ppc_private::vsx_enabled; +#else + return false; +#endif +} + +inline bool supports_vsx3() { +#ifdef __powerpc__ + return ppc_private::vsx3_enabled; +#else + return false; +#endif +} + +} // namespace mozilla + +#endif /* !defined(mozilla_ppc_h_) */ diff --git a/mozglue/build/replace_malloc.mozbuild b/mozglue/build/replace_malloc.mozbuild new file mode 100644 index 0000000000..16c8b7271f --- /dev/null +++ b/mozglue/build/replace_malloc.mozbuild @@ -0,0 +1,6 @@ +# 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/. + +if CONFIG['OS_TARGET'] == 'Darwin' and CONFIG['MOZ_REPLACE_MALLOC']: + LDFLAGS += ['-Wl,-U,_replace_init'] diff --git a/mozglue/interposers/InterposerHelper.h b/mozglue/interposers/InterposerHelper.h new file mode 100644 index 0000000000..fddc05a163 --- /dev/null +++ b/mozglue/interposers/InterposerHelper.h @@ -0,0 +1,79 @@ +/* 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/. */ + +#ifndef InterposerHelper_h +#define InterposerHelper_h + +#include <type_traits> + +#ifdef MOZ_LINKER +# include "Linker.h" +#else +# include <dlfcn.h> +#endif + +#include "mozilla/Assertions.h" + +template <typename T> +static inline T dlsym_wrapper(void* aHandle, const char* aName) { +#ifdef MOZ_LINKER + return reinterpret_cast<T>(__wrap_dlsym(aHandle, aName)); +#else + return reinterpret_cast<T>(dlsym(aHandle, aName)); +#endif // MOZ_LINKER +} + +static inline void* dlopen_wrapper(const char* aPath, int flags) { +#ifdef MOZ_LINKER + return __wrap_dlopen(aPath, flags); +#else + return dlopen(aPath, flags); +#endif // MOZ_LINKER +} + +template <typename T> +static T get_real_symbol(const char* aName, T aReplacementSymbol) { + // T can only be a function pointer + static_assert(std::is_function<typename std::remove_pointer<T>::type>::value); + + // Find the corresponding function in the linked libraries + T real_symbol = dlsym_wrapper<T>(RTLD_NEXT, aName); + +#if defined(ANDROID) + if (real_symbol == nullptr) { + // On old versions of Android the application runtime links in libc before + // we get a chance to link libmozglue, so its symbols don't appear when + // resolving them with RTLD_NEXT. This behavior differ between the + // different versions of Android so we'll just look for them directly into + // libc.so. Note that this won't work if we're trying to interpose + // functions that are in other libraries, but hopefully we'll never have + // to do that. + void* handle = dlopen_wrapper("libc.so", RTLD_LAZY); + + if (handle) { + real_symbol = dlsym_wrapper<T>(handle, aName); + } + } +#endif + + if (real_symbol == nullptr) { + MOZ_CRASH_UNSAFE_PRINTF( + "%s() interposition failed but the interposer function is " + "still being called, this won't work!", + aName); + } + + if (real_symbol == aReplacementSymbol) { + MOZ_CRASH_UNSAFE_PRINTF( + "We could not obtain the real %s(). Calling the symbol we " + "got would make us enter an infinite loop so stop here instead.", + aName); + } + + return real_symbol; +} + +#define GET_REAL_SYMBOL(name) get_real_symbol(#name, name) + +#endif // InterposerHelper_h diff --git a/mozglue/interposers/env_interposer.cpp b/mozglue/interposers/env_interposer.cpp new file mode 100644 index 0000000000..d8c11b5d35 --- /dev/null +++ b/mozglue/interposers/env_interposer.cpp @@ -0,0 +1,79 @@ +/* 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 <pthread.h> +#include <string.h> +#include <unistd.h> + +#include "InterposerHelper.h" + +// The interposers in this file cover all the functions used to access the +// environment (getenv(), putenv(), setenv(), unsetenv() and clearenv()). They +// all use the mutex below for synchronization to prevent races that caused +// startup crashes, see bug 1752703. +static pthread_mutex_t gEnvLock = PTHREAD_MUTEX_INITIALIZER; + +static char* internal_getenv(const char* aName) { + if (environ == nullptr || aName[0] == '\0') { + return nullptr; + } + + size_t len = strlen(aName); + for (char** env_ptr = environ; *env_ptr != nullptr; ++env_ptr) { + if ((aName[0] == (*env_ptr)[0]) && (strncmp(aName, *env_ptr, len) == 0) && + ((*env_ptr)[len] == '=')) { + return *env_ptr + len + 1; + } + } + + return nullptr; +} + +extern "C" { + +MFBT_API char* getenv(const char* name) { + pthread_mutex_lock(&gEnvLock); + char* result = internal_getenv(name); + pthread_mutex_unlock(&gEnvLock); + + return result; +} + +MFBT_API int putenv(char* string) { + static const auto real_putenv = GET_REAL_SYMBOL(putenv); + + pthread_mutex_lock(&gEnvLock); + int result = real_putenv(string); + pthread_mutex_unlock(&gEnvLock); + return result; +} + +MFBT_API int setenv(const char* name, const char* value, int replace) { + static const auto real_setenv = GET_REAL_SYMBOL(setenv); + + pthread_mutex_lock(&gEnvLock); + int result = real_setenv(name, value, replace); + pthread_mutex_unlock(&gEnvLock); + return result; +} + +MFBT_API int unsetenv(const char* name) { + static const auto real_unsetenv = GET_REAL_SYMBOL(unsetenv); + + pthread_mutex_lock(&gEnvLock); + int result = real_unsetenv(name); + pthread_mutex_unlock(&gEnvLock); + return result; +} + +MFBT_API int clearenv(void) { + static const auto real_clearenv = GET_REAL_SYMBOL(clearenv); + + pthread_mutex_lock(&gEnvLock); + int result = real_clearenv(); + pthread_mutex_unlock(&gEnvLock); + return result; +} + +} // extern "C" diff --git a/mozglue/interposers/moz.build b/mozglue/interposers/moz.build new file mode 100644 index 0000000000..f191deff8f --- /dev/null +++ b/mozglue/interposers/moz.build @@ -0,0 +1,24 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# 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/. +Library("interposers") + +DEFINES["IMPL_MFBT"] = True + +UNIFIED_SOURCES += [ + "env_interposer.cpp", +] + +if CONFIG["MOZ_CRASHREPORTER"]: + UNIFIED_SOURCES += [ + "pthread_create_interposer.cpp", + ] + +if CONFIG["MOZ_LINKER"] and CONFIG["MOZ_WIDGET_TOOLKIT"] == "android": + LOCAL_INCLUDES += [ + "/mozglue/linker", + ] + +FINAL_LIBRARY = "mozglue" diff --git a/mozglue/interposers/pthread_create_interposer.cpp b/mozglue/interposers/pthread_create_interposer.cpp new file mode 100644 index 0000000000..65f60c2d1b --- /dev/null +++ b/mozglue/interposers/pthread_create_interposer.cpp @@ -0,0 +1,108 @@ +/* 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 <algorithm> + +#include <pthread.h> +#include <signal.h> +#include <stdlib.h> +#include <sys/mman.h> + +#include "mozilla/Assertions.h" +#include "mozilla/DebugOnly.h" + +#include "InterposerHelper.h" + +using mozilla::DebugOnly; + +struct SigAltStack { + void* mem; + size_t size; +}; + +struct PthreadCreateParams { + void* (*start_routine)(void*); + void* arg; +}; + +// Install the alternate signal stack, returns a pointer to the memory area we +// mapped to store the stack only if it was installed successfully, otherwise +// returns NULL. +static void* install_sig_alt_stack(size_t size) { + void* alt_stack_mem = mmap(nullptr, size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (alt_stack_mem) { + stack_t alt_stack = { + .ss_sp = alt_stack_mem, + .ss_flags = 0, + .ss_size = size, + }; + + int rv = sigaltstack(&alt_stack, nullptr); + if (rv == 0) { + return alt_stack_mem; + } + + rv = munmap(alt_stack_mem, size); + MOZ_ASSERT(rv == 0); + } + + return nullptr; +} + +// Uninstall the alternate signal handler and unmaps it. Does nothing if +// alt_stack_mem is NULL. +static void uninstall_sig_alt_stack(void* alt_stack_ptr) { + SigAltStack* alt_stack = static_cast<SigAltStack*>(alt_stack_ptr); + if (alt_stack->mem) { + stack_t disable_alt_stack = {}; + disable_alt_stack.ss_flags = SS_DISABLE; + DebugOnly<int> rv = sigaltstack(&disable_alt_stack, nullptr); + MOZ_ASSERT(rv == 0); + rv = munmap(alt_stack->mem, alt_stack->size); + MOZ_ASSERT(rv == 0); + } +} + +// This replaces the routine passed to pthread_create() when a thread is +// started, it handles the alternate signal stack and calls the thread's +// actual routine. +void* set_alt_signal_stack_and_start(PthreadCreateParams* params) { + void* (*start_routine)(void*) = params->start_routine; + void* arg = params->arg; + free(params); + + void* thread_rv = nullptr; + static const size_t kSigStackSize = std::max(size_t(16384), size_t(SIGSTKSZ)); + void* alt_stack_mem = install_sig_alt_stack(kSigStackSize); + SigAltStack alt_stack{alt_stack_mem, kSigStackSize}; + pthread_cleanup_push(uninstall_sig_alt_stack, &alt_stack); + thread_rv = start_routine(arg); + pthread_cleanup_pop(1); + + return thread_rv; +} + +extern "C" { +// This interposer replaces libpthread's pthread_create() so that we can +// inject an alternate signal stack in every new thread. +MFBT_API int pthread_create(pthread_t* thread, const pthread_attr_t* attr, + void* (*start_routine)(void*), void* arg) { + static const auto real_pthread_create = GET_REAL_SYMBOL(pthread_create); + + PthreadCreateParams* params = + (PthreadCreateParams*)malloc(sizeof(PthreadCreateParams)); + params->start_routine = start_routine; + params->arg = arg; + + int result = real_pthread_create( + thread, attr, (void* (*)(void*))set_alt_signal_stack_and_start, params); + + if (result != 0) { + free(params); + } + + return result; +} +} diff --git a/mozglue/linker/BaseElf.cpp b/mozglue/linker/BaseElf.cpp new file mode 100644 index 0000000000..78542b3875 --- /dev/null +++ b/mozglue/linker/BaseElf.cpp @@ -0,0 +1,190 @@ +/* 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 "BaseElf.h" +#include "Elfxx.h" +#include "Logging.h" +#include "mozilla/IntegerPrintfMacros.h" +#include "mozilla/RefPtr.h" + +using namespace Elf; + +unsigned long BaseElf::Hash(const char* symbol) { + const unsigned char* sym = reinterpret_cast<const unsigned char*>(symbol); + unsigned long h = 0, g; + while (*sym) { + h = (h << 4) + *sym++; + g = h & 0xf0000000; + h ^= g; + h ^= g >> 24; + } + return h; +} + +void* BaseElf::GetSymbolPtr(const char* symbol) const { + return GetSymbolPtr(symbol, Hash(symbol)); +} + +void* BaseElf::GetSymbolPtr(const char* symbol, unsigned long hash) const { + const Sym* sym = GetSymbol(symbol, hash); + void* ptr = nullptr; + if (sym && sym->st_shndx != SHN_UNDEF) ptr = GetPtr(sym->st_value); + DEBUG_LOG("BaseElf::GetSymbolPtr(%p [\"%s\"], \"%s\") = %p", + reinterpret_cast<const void*>(this), GetPath(), symbol, ptr); + return ptr; +} + +const Sym* BaseElf::GetSymbol(const char* symbol, unsigned long hash) const { + /* Search symbol with the buckets and chains tables. + * The hash computed from the symbol name gives an index in the buckets + * table. The corresponding value in the bucket table is an index in the + * symbols table and in the chains table. + * If the corresponding symbol in the symbols table matches, we're done. + * Otherwise, the corresponding value in the chains table is a new index + * in both tables, which corresponding symbol is tested and so on and so + * forth */ + size_t bucket = hash % buckets.numElements(); + for (size_t y = buckets[bucket]; y != STN_UNDEF; y = chains[y]) { + if (strcmp(symbol, strtab.GetStringAt(symtab[y].st_name))) continue; + return &symtab[y]; + } + return nullptr; +} + +bool BaseElf::Contains(void* addr) const { return base.Contains(addr); } + +#ifdef __ARM_EABI__ +const void* BaseElf::FindExidx(int* pcount) const { + if (arm_exidx) { + *pcount = arm_exidx.numElements(); + return arm_exidx; + } + *pcount = 0; + return nullptr; +} +#endif + +already_AddRefed<LibHandle> LoadedElf::Create(const char* path, + void* base_addr) { + DEBUG_LOG("LoadedElf::Create(\"%s\", %p) = ...", path, base_addr); + + uint8_t mapped; + /* If the page is not mapped, mincore returns an error. If base_addr is + * nullptr, as would happen if the corresponding binary is prelinked with + * the prelink look (but not with the android apriori tool), no page being + * mapped there (right?), mincore returns an error, too, which makes + * prelinked libraries on glibc unsupported. This is not an interesting + * use case for now, so don't try supporting that case. + */ + if (mincore(const_cast<void*>(base_addr), PageSize(), &mapped)) + return nullptr; + + RefPtr<LoadedElf> elf = new LoadedElf(path); + + const Ehdr* ehdr = Ehdr::validate(base_addr); + if (!ehdr) return nullptr; + + Addr min_vaddr = (Addr)-1; // We want to find the lowest and biggest + Addr max_vaddr = 0; // virtual address used by this Elf. + const Phdr* dyn = nullptr; +#ifdef __ARM_EABI__ + const Phdr* arm_exidx_phdr = nullptr; +#endif + + Array<Phdr> phdrs(reinterpret_cast<const char*>(ehdr) + ehdr->e_phoff, + ehdr->e_phnum); + for (auto phdr = phdrs.begin(); phdr < phdrs.end(); ++phdr) { + switch (phdr->p_type) { + case PT_LOAD: + if (phdr->p_vaddr < min_vaddr) min_vaddr = phdr->p_vaddr; + if (max_vaddr < phdr->p_vaddr + phdr->p_memsz) + max_vaddr = phdr->p_vaddr + phdr->p_memsz; + break; + case PT_DYNAMIC: + dyn = &*phdr; + break; +#ifdef __ARM_EABI__ + case PT_ARM_EXIDX: + /* We cannot initialize arm_exidx here + because we don't have a base yet */ + arm_exidx_phdr = &*phdr; + break; +#endif + } + } + + /* If the lowest PT_LOAD virtual address in headers is not 0, then the ELF + * is either prelinked or a non-PIE executable. The former case is not + * possible, because base_addr would be nullptr and the mincore test above + * would already have made us return. + * For a non-PIE executable, PT_LOADs contain absolute addresses, so in + * practice, this means min_vaddr should be equal to base_addr. max_vaddr + * can thus be adjusted accordingly. + */ + if (min_vaddr != 0) { + void* min_vaddr_ptr = + reinterpret_cast<void*>(static_cast<uintptr_t>(min_vaddr)); + if (min_vaddr_ptr != base_addr) { + LOG("%s: %p != %p", elf->GetPath(), min_vaddr_ptr, base_addr); + return nullptr; + } + max_vaddr -= min_vaddr; + } + if (!dyn) { + LOG("%s: No PT_DYNAMIC segment found", elf->GetPath()); + return nullptr; + } + + elf->base.Assign(base_addr, max_vaddr); + + if (!elf->InitDyn(dyn)) return nullptr; + +#ifdef __ARM_EABI__ + if (arm_exidx_phdr) + elf->arm_exidx.InitSize(elf->GetPtr(arm_exidx_phdr->p_vaddr), + arm_exidx_phdr->p_memsz); +#endif + + DEBUG_LOG("LoadedElf::Create(\"%s\", %p) = %p", path, base_addr, + static_cast<void*>(elf)); + + ElfLoader::Singleton.Register(elf); + return elf.forget(); +} + +bool LoadedElf::InitDyn(const Phdr* pt_dyn) { + Array<Dyn> dyns; + dyns.InitSize(GetPtr<Dyn>(pt_dyn->p_vaddr), pt_dyn->p_filesz); + + size_t symnum = 0; + for (auto dyn = dyns.begin(); dyn < dyns.end() && dyn->d_tag; ++dyn) { + switch (dyn->d_tag) { + case DT_HASH: { + DEBUG_LOG("%s 0x%08" PRIxPTR, "DT_HASH", uintptr_t(dyn->d_un.d_val)); + const Elf::Word* hash_table_header = GetPtr<Elf::Word>(dyn->d_un.d_ptr); + symnum = hash_table_header[1]; + buckets.Init(&hash_table_header[2], hash_table_header[0]); + chains.Init(&*buckets.end()); + } break; + case DT_STRTAB: + DEBUG_LOG("%s 0x%08" PRIxPTR, "DT_STRTAB", uintptr_t(dyn->d_un.d_val)); + strtab.Init(GetPtr(dyn->d_un.d_ptr)); + break; + case DT_SYMTAB: + DEBUG_LOG("%s 0x%08" PRIxPTR, "DT_SYMTAB", uintptr_t(dyn->d_un.d_val)); + symtab.Init(GetPtr(dyn->d_un.d_ptr)); + break; + } + } + if (!buckets || !symnum) { + ERROR("%s: Missing or broken DT_HASH", GetPath()); + } else if (!strtab) { + ERROR("%s: Missing DT_STRTAB", GetPath()); + } else if (!symtab) { + ERROR("%s: Missing DT_SYMTAB", GetPath()); + } else { + return true; + } + return false; +} diff --git a/mozglue/linker/BaseElf.h b/mozglue/linker/BaseElf.h new file mode 100644 index 0000000000..9569dbc579 --- /dev/null +++ b/mozglue/linker/BaseElf.h @@ -0,0 +1,128 @@ +/* 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/. */ + +#ifndef BaseElf_h +#define BaseElf_h + +#include "ElfLoader.h" +#include "Elfxx.h" + +/** + * Base class for ELF libraries. This class includes things that will be + * common between SystemElfs and CustomElfs. + */ +class BaseElf : public LibHandle { + public: + /** + * Hash function for symbol lookup, as defined in ELF standard for System V. + */ + static unsigned long Hash(const char* symbol); + + /** + * Returns the address corresponding to the given symbol name (with a + * pre-computed hash). + */ + void* GetSymbolPtr(const char* symbol, unsigned long hash) const; + + /** + * Returns a pointer to the Elf Symbol in the Dynamic Symbol table + * corresponding to the given symbol name (with a pre-computed hash). + */ + const Elf::Sym* GetSymbol(const char* symbol, unsigned long hash) const; + + explicit BaseElf(const char* path, Mappable* mappable = nullptr) + : LibHandle(path), mappable(mappable) {} + + protected: + /** + * Inherited from LibHandle. Those are temporary and are not supposed to + * be used. + */ + virtual void* GetSymbolPtr(const char* symbol) const; + virtual bool Contains(void* addr) const; + virtual void* GetBase() const { return GetPtr(0); } + +#ifdef __ARM_EABI__ + virtual const void* FindExidx(int* pcount) const; +#endif + + virtual Mappable* GetMappable() const { return NULL; }; + + public: + /* private: */ + /** + * Returns a pointer relative to the base address where the library is + * loaded. + */ + void* GetPtr(const Elf::Addr offset) const { + if (reinterpret_cast<void*>(offset) > base) + return reinterpret_cast<void*>(offset); + return base + offset; + } + + /** + * Like the above, but returns a typed (const) pointer + */ + template <typename T> + const T* GetPtr(const Elf::Addr offset) const { + if (reinterpret_cast<void*>(offset) > base) + return reinterpret_cast<const T*>(offset); + return reinterpret_cast<const T*>(base + offset); + } + + /* Appropriated Mappable */ + /* /!\ we rely on this being nullptr for BaseElf instances, but not + * CustomElf instances. */ + RefPtr<Mappable> mappable; + + /* Base address where the library is loaded */ + MappedPtr base; + + /* Buckets and chains for the System V symbol hash table */ + Array<Elf::Word> buckets; + UnsizedArray<Elf::Word> chains; + + /* protected: */ + /* String table */ + Elf::Strtab strtab; + + /* Symbol table */ + UnsizedArray<Elf::Sym> symtab; + +#ifdef __ARM_EABI__ + /* ARM.exidx information used by FindExidx */ + Array<uint32_t[2]> arm_exidx; +#endif +}; + +/** + * Class for ELF libraries that already loaded in memory. + */ +class LoadedElf : public BaseElf { + public: + /** + * Returns a LoadedElf corresponding to the already loaded ELF + * at the given base address. + */ + static already_AddRefed<LibHandle> Create(const char* path, void* base_addr); + + private: + explicit LoadedElf(const char* path) : BaseElf(path) {} + + ~LoadedElf() { + /* Avoid base's destructor unmapping something that doesn't actually + * belong to it. */ + base.release(); + ElfLoader::Singleton.Forget(this); + } + + /** + * Initializes the library according to information found in the given + * PT_DYNAMIC header. + * Returns whether this succeeded or failed. + */ + bool InitDyn(const Elf::Phdr* pt_dyn); +}; + +#endif /* BaseElf_h */ diff --git a/mozglue/linker/CustomElf.cpp b/mozglue/linker/CustomElf.cpp new file mode 100644 index 0000000000..5d44b34d22 --- /dev/null +++ b/mozglue/linker/CustomElf.cpp @@ -0,0 +1,680 @@ +/* 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 <cstring> +#include <sys/mman.h> +#include <vector> +#include <dlfcn.h> +#include <signal.h> +#include <string.h> +#include "CustomElf.h" +#include "BaseElf.h" +#include "Mappable.h" +#include "Logging.h" +#include "mozilla/IntegerPrintfMacros.h" + +using namespace Elf; + +/* TODO: Fill ElfLoader::Singleton.lastError on errors. */ + +const Ehdr* Ehdr::validate(const void* buf) { + if (!buf || buf == MAP_FAILED) return nullptr; + + const Ehdr* ehdr = reinterpret_cast<const Ehdr*>(buf); + + /* Only support ELF executables or libraries for the host system */ + if (memcmp(ELFMAG, &ehdr->e_ident, SELFMAG) || + ehdr->e_ident[EI_CLASS] != ELFCLASS || + ehdr->e_ident[EI_DATA] != ELFDATA || ehdr->e_ident[EI_VERSION] != 1 || + (ehdr->e_ident[EI_OSABI] != ELFOSABI && + ehdr->e_ident[EI_OSABI] != ELFOSABI_NONE) || +#ifdef EI_ABIVERSION + ehdr->e_ident[EI_ABIVERSION] != ELFABIVERSION || +#endif + (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) || + ehdr->e_machine != ELFMACHINE || ehdr->e_version != 1 || + ehdr->e_phentsize != sizeof(Phdr)) + return nullptr; + + return ehdr; +} + +namespace { + +void debug_phdr(const char* type, const Phdr* phdr) { + DEBUG_LOG("%s @0x%08" PRIxPTR + " (" + "filesz: 0x%08" PRIxPTR + ", " + "memsz: 0x%08" PRIxPTR + ", " + "offset: 0x%08" PRIxPTR + ", " + "flags: %c%c%c)", + type, uintptr_t(phdr->p_vaddr), uintptr_t(phdr->p_filesz), + uintptr_t(phdr->p_memsz), uintptr_t(phdr->p_offset), + phdr->p_flags & PF_R ? 'r' : '-', phdr->p_flags & PF_W ? 'w' : '-', + phdr->p_flags & PF_X ? 'x' : '-'); +} + +static int p_flags_to_mprot(Word flags) { + return ((flags & PF_X) ? PROT_EXEC : 0) | ((flags & PF_W) ? PROT_WRITE : 0) | + ((flags & PF_R) ? PROT_READ : 0); +} + +} /* anonymous namespace */ + +/** + * RAII wrapper for a mapping of the first page off a Mappable object. + * This calls Mappable::munmap instead of system munmap. + */ +class Mappable1stPagePtr : public GenericMappedPtr<Mappable1stPagePtr> { + public: + explicit Mappable1stPagePtr(Mappable* mappable) + : GenericMappedPtr<Mappable1stPagePtr>( + mappable->mmap(nullptr, PageSize(), PROT_READ, MAP_PRIVATE, 0)), + mappable(mappable) {} + + private: + friend class GenericMappedPtr<Mappable1stPagePtr>; + void munmap(void* buf, size_t length) { mappable->munmap(buf, length); } + + RefPtr<Mappable> mappable; +}; + +already_AddRefed<LibHandle> CustomElf::Load(Mappable* mappable, + const char* path, int flags) { + DEBUG_LOG("CustomElf::Load(\"%s\", 0x%x) = ...", path, flags); + if (!mappable) return nullptr; + /* Keeping a RefPtr of the CustomElf is going to free the appropriate + * resources when returning nullptr */ + RefPtr<CustomElf> elf = new CustomElf(mappable, path); + /* Map the first page of the Elf object to access Elf and program headers */ + Mappable1stPagePtr ehdr_raw(mappable); + if (ehdr_raw == MAP_FAILED) return nullptr; + + const Ehdr* ehdr = Ehdr::validate(ehdr_raw); + if (!ehdr) return nullptr; + + /* Scan Elf Program Headers and gather some information about them */ + std::vector<const Phdr*> pt_loads; + Addr min_vaddr = (Addr)-1; // We want to find the lowest and biggest + Addr max_vaddr = 0; // virtual address used by this Elf. + const Phdr* dyn = nullptr; + + const Phdr* first_phdr = reinterpret_cast<const Phdr*>( + reinterpret_cast<const char*>(ehdr) + ehdr->e_phoff); + const Phdr* end_phdr = &first_phdr[ehdr->e_phnum]; +#ifdef __ARM_EABI__ + const Phdr* arm_exidx_phdr = nullptr; +#endif + + for (const Phdr* phdr = first_phdr; phdr < end_phdr; phdr++) { + switch (phdr->p_type) { + case PT_LOAD: + debug_phdr("PT_LOAD", phdr); + pt_loads.push_back(phdr); + if (phdr->p_vaddr < min_vaddr) min_vaddr = phdr->p_vaddr; + if (max_vaddr < phdr->p_vaddr + phdr->p_memsz) + max_vaddr = phdr->p_vaddr + phdr->p_memsz; + break; + case PT_DYNAMIC: + debug_phdr("PT_DYNAMIC", phdr); + if (!dyn) { + dyn = phdr; + } else { + ERROR("%s: Multiple PT_DYNAMIC segments detected", elf->GetPath()); + return nullptr; + } + break; + case PT_TLS: + debug_phdr("PT_TLS", phdr); + if (phdr->p_memsz) { + ERROR("%s: TLS is not supported", elf->GetPath()); + return nullptr; + } + break; + case PT_GNU_STACK: + debug_phdr("PT_GNU_STACK", phdr); +// Skip on Android until bug 706116 is fixed +#ifndef ANDROID + if (phdr->p_flags & PF_X) { + ERROR("%s: Executable stack is not supported", elf->GetPath()); + return nullptr; + } +#endif + break; +#ifdef __ARM_EABI__ + case PT_ARM_EXIDX: + /* We cannot initialize arm_exidx here + because we don't have a base yet */ + arm_exidx_phdr = phdr; + break; +#endif + default: + DEBUG_LOG("%s: Program header type #%d not handled", elf->GetPath(), + phdr->p_type); + } + } + + if (min_vaddr != 0) { + ERROR("%s: Unsupported minimal virtual address: 0x%08" PRIxPTR, + elf->GetPath(), uintptr_t(min_vaddr)); + return nullptr; + } + if (!dyn) { + ERROR("%s: No PT_DYNAMIC segment found", elf->GetPath()); + return nullptr; + } + + /* Reserve enough memory to map the complete virtual address space for this + * library. + * As we are using the base address from here to mmap something else with + * MAP_FIXED | MAP_SHARED, we need to make sure these mmaps will work. For + * instance, on armv6, MAP_SHARED mappings require a 16k alignment, but mmap + * MAP_PRIVATE only returns a 4k aligned address. So we first get a base + * address with MAP_SHARED, which guarantees the kernel returns an address + * that we'll be able to use with MAP_FIXED, and then remap MAP_PRIVATE at + * the same address, because of some bad side effects of keeping it as + * MAP_SHARED. */ + elf->base.Assign(MemoryRange::mmap(nullptr, max_vaddr, PROT_NONE, + MAP_SHARED | MAP_ANONYMOUS, -1, 0)); + if ((elf->base == MAP_FAILED) || + (mmap(elf->base, max_vaddr, PROT_NONE, + MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0) != elf->base)) { + ERROR("%s: Failed to mmap", elf->GetPath()); + return nullptr; + } + + /* Load and initialize library */ + for (std::vector<const Phdr*>::iterator it = pt_loads.begin(); + it < pt_loads.end(); ++it) + if (!elf->LoadSegment(*it)) return nullptr; + + /* We're not going to mmap anymore */ + mappable->finalize(); + + elf->l_addr = elf->base; + elf->l_name = elf->GetPath(); + elf->l_ld = elf->GetPtr<Dyn>(dyn->p_vaddr); + ElfLoader::Singleton.Register(elf); + + if (!elf->InitDyn(dyn)) return nullptr; + + if (elf->has_text_relocs) { + for (std::vector<const Phdr*>::iterator it = pt_loads.begin(); + it < pt_loads.end(); ++it) + mprotect(PageAlignedPtr(elf->GetPtr((*it)->p_vaddr)), + PageAlignedEndPtr((*it)->p_memsz), + p_flags_to_mprot((*it)->p_flags) | PROT_WRITE); + } + + if (!elf->Relocate() || !elf->RelocateJumps()) return nullptr; + + if (elf->has_text_relocs) { + for (std::vector<const Phdr*>::iterator it = pt_loads.begin(); + it < pt_loads.end(); ++it) + mprotect(PageAlignedPtr(elf->GetPtr((*it)->p_vaddr)), + PageAlignedEndPtr((*it)->p_memsz), + p_flags_to_mprot((*it)->p_flags)); + } + + if (!elf->CallInit()) return nullptr; + +#ifdef __ARM_EABI__ + if (arm_exidx_phdr) + elf->arm_exidx.InitSize(elf->GetPtr(arm_exidx_phdr->p_vaddr), + arm_exidx_phdr->p_memsz); +#endif + + DEBUG_LOG("CustomElf::Load(\"%s\", 0x%x) = %p", path, flags, + static_cast<void*>(elf)); + return elf.forget(); +} + +CustomElf::~CustomElf() { + DEBUG_LOG("CustomElf::~CustomElf(%p [\"%s\"])", reinterpret_cast<void*>(this), + GetPath()); + CallFini(); + /* Normally, __cxa_finalize is called by the .fini function. However, + * Android NDK before r6b doesn't do that. Our wrapped cxa_finalize only + * calls destructors once, so call it in all cases. */ + ElfLoader::__wrap_cxa_finalize(this); + ElfLoader::Singleton.Forget(this); +} + +void* CustomElf::GetSymbolPtrInDeps(const char* symbol) const { + /* Resolve dlopen and related functions to point to ours */ + if (symbol[0] == 'd' && symbol[1] == 'l') { + if (strcmp(symbol + 2, "open") == 0) return FunctionPtr(__wrap_dlopen); + if (strcmp(symbol + 2, "error") == 0) return FunctionPtr(__wrap_dlerror); + if (strcmp(symbol + 2, "close") == 0) return FunctionPtr(__wrap_dlclose); + if (strcmp(symbol + 2, "sym") == 0) return FunctionPtr(__wrap_dlsym); + if (strcmp(symbol + 2, "addr") == 0) return FunctionPtr(__wrap_dladdr); + if (strcmp(symbol + 2, "_iterate_phdr") == 0) + return FunctionPtr(__wrap_dl_iterate_phdr); + } else if (symbol[0] == '_' && symbol[1] == '_') { + /* Resolve a few C++ ABI specific functions to point to ours */ +#ifdef __ARM_EABI__ + if (strcmp(symbol + 2, "aeabi_atexit") == 0) + return FunctionPtr(&ElfLoader::__wrap_aeabi_atexit); +#else + if (strcmp(symbol + 2, "cxa_atexit") == 0) + return FunctionPtr(&ElfLoader::__wrap_cxa_atexit); +#endif + if (strcmp(symbol + 2, "cxa_finalize") == 0) + return FunctionPtr(&ElfLoader::__wrap_cxa_finalize); + if (strcmp(symbol + 2, "dso_handle") == 0) + return const_cast<CustomElf*>(this); +#ifdef __ARM_EABI__ + if (strcmp(symbol + 2, "gnu_Unwind_Find_exidx") == 0) + return FunctionPtr(__wrap___gnu_Unwind_Find_exidx); +#endif + } else if (symbol[0] == 's' && symbol[1] == 'i') { + if (strcmp(symbol + 2, "gnal") == 0) return FunctionPtr(signal); + if (strcmp(symbol + 2, "gaction") == 0) return FunctionPtr(sigaction); + } + + void* sym; + + unsigned long hash = Hash(symbol); + + /* self_elf should never be NULL, but better safe than sorry. */ + if (ElfLoader::Singleton.self_elf) { + /* We consider the library containing this code a permanent LD_PRELOAD, + * so, check if the symbol exists here first. */ + sym = static_cast<BaseElf*>(ElfLoader::Singleton.self_elf.get()) + ->GetSymbolPtr(symbol, hash); + if (sym) return sym; + } + + /* Then search the symbol in our dependencies. Since we already searched in + * libraries the system linker loaded, skip those (on glibc systems). We + * also assume the symbol is to be found in one of the dependent libraries + * directly, not in their own dependent libraries. Building libraries with + * --no-allow-shlib-undefined ensures such indirect symbol dependency don't + * happen. */ + for (std::vector<RefPtr<LibHandle> >::const_iterator it = + dependencies.begin(); + it < dependencies.end(); ++it) { + /* Skip if it's the library containing this code, since we've already + * looked at it above. */ + if (*it == ElfLoader::Singleton.self_elf) continue; + if (BaseElf* be = (*it)->AsBaseElf()) { + sym = be->GetSymbolPtr(symbol, hash); + } else { + sym = (*it)->GetSymbolPtr(symbol); + } + if (sym) return sym; + } + return nullptr; +} + +bool CustomElf::LoadSegment(const Phdr* pt_load) const { + if (pt_load->p_type != PT_LOAD) { + DEBUG_LOG("%s: Elf::LoadSegment only takes PT_LOAD program headers", + GetPath()); + return false; + ; + } + + int prot = p_flags_to_mprot(pt_load->p_flags); + + /* Mmap at page boundary */ + Addr align = PageSize(); + Addr align_offset; + void *mapped, *where; + do { + align_offset = pt_load->p_vaddr - AlignedPtr(pt_load->p_vaddr, align); + where = GetPtr(pt_load->p_vaddr - align_offset); + DEBUG_LOG("%s: Loading segment @%p %c%c%c", GetPath(), where, + prot & PROT_READ ? 'r' : '-', prot & PROT_WRITE ? 'w' : '-', + prot & PROT_EXEC ? 'x' : '-'); + mapped = mappable->mmap(where, pt_load->p_filesz + align_offset, prot, + MAP_PRIVATE | MAP_FIXED, + pt_load->p_offset - align_offset); + if ((mapped != MAP_FAILED) || (pt_load->p_vaddr == 0) || + (pt_load->p_align == align)) + break; + /* The virtual address space for the library is properly aligned at + * 16k on ARMv6 (see CustomElf::Load), and so is the first segment + * (p_vaddr == 0). But subsequent segments may not be 16k aligned + * and fail to mmap. In such case, try to mmap again at the p_align + * boundary instead of page boundary. */ + DEBUG_LOG("%s: Failed to mmap, retrying", GetPath()); + align = pt_load->p_align; + } while (1); + + if (mapped != where) { + if (mapped == MAP_FAILED) { + ERROR("%s: Failed to mmap", GetPath()); + } else { + ERROR("%s: Didn't map at the expected location (wanted: %p, got: %p)", + GetPath(), where, mapped); + } + return false; + } + + /* When p_memsz is greater than p_filesz, we need to have nulled out memory + * after p_filesz and before p_memsz. + * Above the end of the last page, and up to p_memsz, we already have nulled + * out memory because we mapped anonymous memory on the whole library virtual + * address space. We just need to adjust this anonymous memory protection + * flags. */ + if (pt_load->p_memsz > pt_load->p_filesz) { + Addr file_end = pt_load->p_vaddr + pt_load->p_filesz; + Addr mem_end = pt_load->p_vaddr + pt_load->p_memsz; + Addr next_page = PageAlignedEndPtr(file_end); + if (next_page > file_end) { + void* ptr = GetPtr(file_end); + memset(ptr, 0, next_page - file_end); + } + if (mem_end > next_page) { + if (mprotect(GetPtr(next_page), mem_end - next_page, prot) < 0) { + ERROR("%s: Failed to mprotect", GetPath()); + return false; + } + } + } + return true; +} + +namespace { + +void debug_dyn(const char* type, const Dyn* dyn) { + DEBUG_LOG("%s 0x%08" PRIxPTR, type, uintptr_t(dyn->d_un.d_val)); +} + +} /* anonymous namespace */ + +bool CustomElf::InitDyn(const Phdr* pt_dyn) { + /* Scan PT_DYNAMIC segment and gather some information */ + const Dyn* first_dyn = GetPtr<Dyn>(pt_dyn->p_vaddr); + const Dyn* end_dyn = GetPtr<Dyn>(pt_dyn->p_vaddr + pt_dyn->p_filesz); + std::vector<Word> dt_needed; + size_t symnum = 0; + for (const Dyn* dyn = first_dyn; dyn < end_dyn && dyn->d_tag; dyn++) { + switch (dyn->d_tag) { + case DT_NEEDED: + debug_dyn("DT_NEEDED", dyn); + dt_needed.push_back(dyn->d_un.d_val); + break; + case DT_HASH: { + debug_dyn("DT_HASH", dyn); + const Word* hash_table_header = GetPtr<Word>(dyn->d_un.d_ptr); + symnum = hash_table_header[1]; + buckets.Init(&hash_table_header[2], hash_table_header[0]); + chains.Init(&*buckets.end()); + } break; + case DT_STRTAB: + debug_dyn("DT_STRTAB", dyn); + strtab.Init(GetPtr(dyn->d_un.d_ptr)); + break; + case DT_SYMTAB: + debug_dyn("DT_SYMTAB", dyn); + symtab.Init(GetPtr(dyn->d_un.d_ptr)); + break; + case DT_SYMENT: + debug_dyn("DT_SYMENT", dyn); + if (dyn->d_un.d_val != sizeof(Sym)) { + ERROR("%s: Unsupported DT_SYMENT", GetPath()); + return false; + } + break; + case DT_TEXTREL: + if (strcmp("libflashplayer.so", GetName()) == 0) { + has_text_relocs = true; + } else { + ERROR("%s: Text relocations are not supported", GetPath()); + return false; + } + break; + case DT_STRSZ: /* Ignored */ + debug_dyn("DT_STRSZ", dyn); + break; + case UNSUPPORTED_RELOC(): + case UNSUPPORTED_RELOC(SZ): + case UNSUPPORTED_RELOC(ENT): + ERROR("%s: Unsupported relocations", GetPath()); + return false; + case RELOC(): + debug_dyn(STR_RELOC(), dyn); + relocations.Init(GetPtr(dyn->d_un.d_ptr)); + break; + case RELOC(SZ): + debug_dyn(STR_RELOC(SZ), dyn); + relocations.InitSize(dyn->d_un.d_val); + break; + case RELOC(ENT): + debug_dyn(STR_RELOC(ENT), dyn); + if (dyn->d_un.d_val != sizeof(Reloc)) { + ERROR("%s: Unsupported DT_RELENT", GetPath()); + return false; + } + break; + case DT_JMPREL: + debug_dyn("DT_JMPREL", dyn); + jumprels.Init(GetPtr(dyn->d_un.d_ptr)); + break; + case DT_PLTRELSZ: + debug_dyn("DT_PLTRELSZ", dyn); + jumprels.InitSize(dyn->d_un.d_val); + break; + case DT_PLTGOT: + debug_dyn("DT_PLTGOT", dyn); + break; + case DT_INIT: + debug_dyn("DT_INIT", dyn); + init = dyn->d_un.d_ptr; + break; + case DT_INIT_ARRAY: + debug_dyn("DT_INIT_ARRAY", dyn); + init_array.Init(GetPtr(dyn->d_un.d_ptr)); + break; + case DT_INIT_ARRAYSZ: + debug_dyn("DT_INIT_ARRAYSZ", dyn); + init_array.InitSize(dyn->d_un.d_val); + break; + case DT_FINI: + debug_dyn("DT_FINI", dyn); + fini = dyn->d_un.d_ptr; + break; + case DT_FINI_ARRAY: + debug_dyn("DT_FINI_ARRAY", dyn); + fini_array.Init(GetPtr(dyn->d_un.d_ptr)); + break; + case DT_FINI_ARRAYSZ: + debug_dyn("DT_FINI_ARRAYSZ", dyn); + fini_array.InitSize(dyn->d_un.d_val); + break; + case DT_PLTREL: + if (dyn->d_un.d_val != RELOC()) { + ERROR("%s: Error: DT_PLTREL is not " STR_RELOC(), GetPath()); + return false; + } + break; + case DT_FLAGS: { + Addr flags = dyn->d_un.d_val; + /* Treat as a DT_TEXTREL tag */ + if (flags & DF_TEXTREL) { + if (strcmp("libflashplayer.so", GetName()) == 0) { + has_text_relocs = true; + } else { + ERROR("%s: Text relocations are not supported", GetPath()); + return false; + } + } + /* we can treat this like having a DT_SYMBOLIC tag */ + flags &= ~DF_SYMBOLIC; + if (flags) + WARN("%s: unhandled flags #%" PRIxPTR " not handled", GetPath(), + uintptr_t(flags)); + } break; + case DT_SONAME: /* Should match GetName(), but doesn't matter */ + case DT_SYMBOLIC: /* Indicates internal symbols should be looked up in + * the library itself first instead of the executable, + * which is actually what this linker does by default */ + case RELOC(COUNT): /* Indicates how many relocations are relative, which + * is usually used to skip relocations on prelinked + * libraries. They are not supported anyways. */ + case UNSUPPORTED_RELOC(COUNT): /* This should error out, but it doesn't + * really matter. */ + case DT_FLAGS_1: /* Additional linker-internal flags that we don't care + * about. See DF_1_* values in src/include/elf/common.h + * in binutils. */ + case DT_VERSYM: /* DT_VER* entries are used for symbol versioning, which + */ + case DT_VERDEF: /* this linker doesn't support yet. */ + case DT_VERDEFNUM: + case DT_VERNEED: + case DT_VERNEEDNUM: + /* Ignored */ + break; + default: + WARN("%s: dynamic header type #%" PRIxPTR " not handled", GetPath(), + uintptr_t(dyn->d_tag)); + } + } + + if (!buckets || !symnum) { + ERROR("%s: Missing or broken DT_HASH", GetPath()); + return false; + } + if (!strtab) { + ERROR("%s: Missing DT_STRTAB", GetPath()); + return false; + } + if (!symtab) { + ERROR("%s: Missing DT_SYMTAB", GetPath()); + return false; + } + + /* Load dependent libraries */ + for (size_t i = 0; i < dt_needed.size(); i++) { + const char* name = strtab.GetStringAt(dt_needed[i]); + RefPtr<LibHandle> handle = + ElfLoader::Singleton.Load(name, RTLD_GLOBAL | RTLD_LAZY, this); + if (!handle) return false; + dependencies.push_back(handle); + } + + return true; +} + +bool CustomElf::Relocate() { + DEBUG_LOG("Relocate %s @%p", GetPath(), static_cast<void*>(base)); + uint32_t symtab_index = (uint32_t)-1; + void* symptr = nullptr; + for (Array<Reloc>::iterator rel = relocations.begin(); + rel < relocations.end(); ++rel) { + /* Location of the relocation */ + void* ptr = GetPtr(rel->r_offset); + + /* R_*_RELATIVE relocations apply directly at the given location */ + if (ELF_R_TYPE(rel->r_info) == R_RELATIVE) { + *(void**)ptr = GetPtr(rel->GetAddend(base)); + continue; + } + /* Other relocation types need a symbol resolution */ + /* Avoid symbol resolution when it's the same symbol as last iteration */ + if (symtab_index != ELF_R_SYM(rel->r_info)) { + symtab_index = ELF_R_SYM(rel->r_info); + const Sym sym = symtab[symtab_index]; + if (sym.st_shndx != SHN_UNDEF) { + symptr = GetPtr(sym.st_value); + } else { + /* TODO: handle symbol resolving to nullptr vs. being undefined. */ + symptr = GetSymbolPtrInDeps(strtab.GetStringAt(sym.st_name)); + } + } + + if (symptr == nullptr) + WARN("%s: Relocation to NULL @0x%08" PRIxPTR, GetPath(), + uintptr_t(rel->r_offset)); + + /* Apply relocation */ + switch (ELF_R_TYPE(rel->r_info)) { + case R_GLOB_DAT: + /* R_*_GLOB_DAT relocations simply use the symbol value */ + *(void**)ptr = symptr; + break; + case R_ABS: + /* R_*_ABS* relocations add the relocation added to the symbol value */ + *(const char**)ptr = (const char*)symptr + rel->GetAddend(base); + break; + default: + ERROR("%s: Unsupported relocation type: 0x%" PRIxPTR, GetPath(), + uintptr_t(ELF_R_TYPE(rel->r_info))); + return false; + } + } + return true; +} + +bool CustomElf::RelocateJumps() { + /* TODO: Dynamic symbol resolution */ + for (Array<Reloc>::iterator rel = jumprels.begin(); rel < jumprels.end(); + ++rel) { + /* Location of the relocation */ + void* ptr = GetPtr(rel->r_offset); + + /* Only R_*_JMP_SLOT relocations are expected */ + if (ELF_R_TYPE(rel->r_info) != R_JMP_SLOT) { + ERROR("%s: Jump relocation type mismatch", GetPath()); + return false; + } + + /* TODO: Avoid code duplication with the relocations above */ + const Sym sym = symtab[ELF_R_SYM(rel->r_info)]; + void* symptr; + if (sym.st_shndx != SHN_UNDEF) + symptr = GetPtr(sym.st_value); + else + symptr = GetSymbolPtrInDeps(strtab.GetStringAt(sym.st_name)); + + if (symptr == nullptr) { + if (ELF_ST_BIND(sym.st_info) == STB_WEAK) { + WARN("%s: Relocation to NULL @0x%08" PRIxPTR " for symbol \"%s\"", + GetPath(), uintptr_t(rel->r_offset), + strtab.GetStringAt(sym.st_name)); + } else { + ERROR("%s: Relocation to NULL @0x%08" PRIxPTR " for symbol \"%s\"", + GetPath(), uintptr_t(rel->r_offset), + strtab.GetStringAt(sym.st_name)); + return false; + } + } + /* Apply relocation */ + *(void**)ptr = symptr; + } + return true; +} + +bool CustomElf::CallInit() { + if (init) CallFunction(init); + + for (Array<void*>::iterator it = init_array.begin(); it < init_array.end(); + ++it) { + /* Android x86 NDK wrongly puts 0xffffffff in INIT_ARRAY */ + if (*it && *it != reinterpret_cast<void*>(-1)) CallFunction(*it); + } + initialized = true; + return true; +} + +void CustomElf::CallFini() { + if (!initialized) return; + for (Array<void*>::reverse_iterator it = fini_array.rbegin(); + it < fini_array.rend(); ++it) { + /* Android x86 NDK wrongly puts 0xffffffff in FINI_ARRAY */ + if (*it && *it != reinterpret_cast<void*>(-1)) CallFunction(*it); + } + if (fini) CallFunction(fini); +} + +Mappable* CustomElf::GetMappable() const { + if (!mappable) return nullptr; + if (mappable->GetKind() == Mappable::MAPPABLE_EXTRACT_FILE) return mappable; + return ElfLoader::GetMappableFromPath(GetPath()); +} diff --git a/mozglue/linker/CustomElf.h b/mozglue/linker/CustomElf.h new file mode 100644 index 0000000000..f7b116e9d3 --- /dev/null +++ b/mozglue/linker/CustomElf.h @@ -0,0 +1,147 @@ +/* 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/. */ + +#ifndef CustomElf_h +#define CustomElf_h + +#include "ElfLoader.h" +#include "BaseElf.h" +#include "Logging.h" +#include "Elfxx.h" + +/** + * Library Handle class for ELF libraries we don't let the system linker + * handle. + */ +class CustomElf : public BaseElf, private ElfLoader::link_map { + friend class ElfLoader; + friend class SEGVHandler; + + public: + /** + * Returns a new CustomElf using the given file descriptor to map ELF + * content. The file descriptor ownership is stolen, and it will be closed + * in CustomElf's destructor if an instance is created, or by the Load + * method otherwise. The path corresponds to the file descriptor, and flags + * are the same kind of flags that would be given to dlopen(), though + * currently, none are supported and the behaviour is more or less that of + * RTLD_GLOBAL | RTLD_BIND_NOW. + */ + static already_AddRefed<LibHandle> Load(Mappable* mappable, const char* path, + int flags); + + /** + * Inherited from LibHandle/BaseElf + */ + virtual ~CustomElf(); + + protected: + virtual Mappable* GetMappable() const; + + public: + /** + * Returns the instance, casted as BaseElf. (short of a better way to do + * this without RTTI) + */ + virtual BaseElf* AsBaseElf() { return this; } + + private: + /** + * Scan dependent libraries to find the address corresponding to the + * given symbol name. This is used to find symbols that are undefined + * in the Elf object. + */ + void* GetSymbolPtrInDeps(const char* symbol) const; + + /** + * Private constructor + */ + CustomElf(Mappable* mappable, const char* path) + : BaseElf(path, mappable), + link_map(), + init(0), + fini(0), + initialized(false), + has_text_relocs(false) {} + + /** + * Loads an Elf segment defined by the given PT_LOAD header. + * Returns whether this succeeded or failed. + */ + bool LoadSegment(const Elf::Phdr* pt_load) const; + + /** + * Initializes the library according to information found in the given + * PT_DYNAMIC header. + * Returns whether this succeeded or failed. + */ + bool InitDyn(const Elf::Phdr* pt_dyn); + + /** + * Apply .rel.dyn/.rela.dyn relocations. + * Returns whether this succeeded or failed. + */ + bool Relocate(); + + /** + * Apply .rel.plt/.rela.plt relocations. + * Returns whether this succeeded or failed. + */ + bool RelocateJumps(); + + /** + * Call initialization functions (.init/.init_array) + * Returns true; + */ + bool CallInit(); + + /** + * Call destructor functions (.fini_array/.fini) + * Returns whether this succeeded or failed. + */ + void CallFini(); + + /** + * Call a function given a pointer to its location. + */ + void CallFunction(void* ptr) const { + /* C++ doesn't allow direct conversion between pointer-to-object + * and pointer-to-function. */ + union { + void* ptr; + void (*func)(void); + } f; + f.ptr = ptr; + DEBUG_LOG("%s: Calling function @%p", GetPath(), ptr); + f.func(); + } + + /** + * Call a function given a an address relative to the library base + */ + void CallFunction(Elf::Addr addr) const { return CallFunction(GetPtr(addr)); } + + /* List of dependent libraries */ + std::vector<RefPtr<LibHandle> > dependencies; + + /* List of .rel.dyn/.rela.dyn relocations */ + Array<Elf::Reloc> relocations; + + /* List of .rel.plt/.rela.plt relocation */ + Array<Elf::Reloc> jumprels; + + /* Relative address of the initialization and destruction functions + * (.init/.fini) */ + Elf::Addr init, fini; + + /* List of initialization and destruction functions + * (.init_array/.fini_array) */ + Array<void*> init_array, fini_array; + + bool initialized; + + bool has_text_relocs; +}; + +#endif /* CustomElf_h */ diff --git a/mozglue/linker/ElfLoader.cpp b/mozglue/linker/ElfLoader.cpp new file mode 100644 index 0000000000..55b113467a --- /dev/null +++ b/mozglue/linker/ElfLoader.cpp @@ -0,0 +1,1360 @@ +/* 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 <string> +#include <cstring> +#include <cstdlib> +#include <cstdio> +#include <dlfcn.h> +#include <unistd.h> +#include <errno.h> +#include <algorithm> +#include <fcntl.h> +#include "ElfLoader.h" +#include "BaseElf.h" +#include "CustomElf.h" +#include "Mappable.h" +#include "Logging.h" +#include "Utils.h" +#include <inttypes.h> + +// From Utils.h +mozilla::Atomic<size_t, mozilla::ReleaseAcquire> gPageSize; + +#if defined(ANDROID) +# include <sys/syscall.h> +# include <sys/system_properties.h> +# include <math.h> + +# include <android/api-level.h> + +/** + * Return the current Android version, or 0 on failure. + */ +static int GetAndroidSDKVersion() { + static int version = 0; + if (version) { + return version; + } + + char version_string[PROP_VALUE_MAX] = {'\0'}; + int len = __system_property_get("ro.build.version.sdk", version_string); + if (len) { + version = static_cast<int>(strtol(version_string, nullptr, 10)); + } + return version; +} + +# if __ANDROID_API__ < 8 +/* Android API < 8 doesn't provide sigaltstack */ + +extern "C" { + +inline int sigaltstack(const stack_t* ss, stack_t* oss) { + return syscall(__NR_sigaltstack, ss, oss); +} + +} /* extern "C" */ +# endif /* __ANDROID_API__ */ +#endif /* ANDROID */ + +#ifdef __ARM_EABI__ +extern "C" MOZ_EXPORT const void* __gnu_Unwind_Find_exidx(void* pc, int* pcount) + __attribute__((weak)); +#endif + +/* Ideally we'd #include <link.h>, but that's a world of pain + * Moreover, not all versions of android support it, so we need a weak + * reference. */ +extern "C" MOZ_EXPORT int dl_iterate_phdr(dl_phdr_cb callback, void* data) + __attribute__((weak)); + +/* Pointer to the PT_DYNAMIC section of the executable or library + * containing this code. */ +extern "C" Elf::Dyn _DYNAMIC[]; + +/** + * dlfcn.h replacements functions + */ + +void* __wrap_dlopen(const char* path, int flags) { +#if defined(ANDROID) + if (GetAndroidSDKVersion() >= 23) { + return dlopen(path, flags); + } +#endif + + RefPtr<LibHandle> handle = ElfLoader::Singleton.Load(path, flags); + if (handle) handle->AddDirectRef(); + return handle; +} + +const char* __wrap_dlerror(void) { +#if defined(ANDROID) + if (GetAndroidSDKVersion() >= 23) { + return dlerror(); + } +#endif + + const char* error = ElfLoader::Singleton.lastError.exchange(nullptr); + if (error) { + // Return a custom error if available. + return error; + } + // Or fallback to the system error. + return dlerror(); +} + +void* __wrap_dlsym(void* handle, const char* symbol) { +#if defined(ANDROID) + if (GetAndroidSDKVersion() >= 23) { + return dlsym(handle, symbol); + } +#endif + + if (!handle) { + ElfLoader::Singleton.lastError = "dlsym(NULL, sym) unsupported"; + return nullptr; + } + if (handle != RTLD_DEFAULT && handle != RTLD_NEXT) { + LibHandle* h = reinterpret_cast<LibHandle*>(handle); + return h->GetSymbolPtr(symbol); + } + + ElfLoader::Singleton.lastError = nullptr; // Use system dlerror. + return dlsym(handle, symbol); +} + +int __wrap_dlclose(void* handle) { +#if defined(ANDROID) + if (GetAndroidSDKVersion() >= 23) { + return dlclose(handle); + } +#endif + + if (!handle) { + ElfLoader::Singleton.lastError = "No handle given to dlclose()"; + return -1; + } + reinterpret_cast<LibHandle*>(handle)->ReleaseDirectRef(); + return 0; +} + +int __wrap_dladdr(const void* addr, Dl_info* info) { +#if defined(ANDROID) + if (GetAndroidSDKVersion() >= 23) { + return dladdr(addr, info); + } +#endif + + RefPtr<LibHandle> handle = + ElfLoader::Singleton.GetHandleByPtr(const_cast<void*>(addr)); + if (!handle) { + return dladdr(addr, info); + } + info->dli_fname = handle->GetPath(); + info->dli_fbase = handle->GetBase(); + return 1; +} + +class DlIteratePhdrHelper { + public: + DlIteratePhdrHelper() { + int pipefd[2]; + valid_pipe = (pipe(pipefd) == 0); + read_fd.reset(pipefd[0]); + write_fd.reset(pipefd[1]); + } + + int fill_and_call(dl_phdr_cb callback, const void* l_addr, const char* l_name, + void* data); + + private: + bool valid_pipe; + AutoCloseFD read_fd; + AutoCloseFD write_fd; +}; + +// This function is called for each shared library iterated over by +// dl_iterate_phdr, and is used to fill a dl_phdr_info which is then +// sent through to the dl_iterate_phdr callback. +int DlIteratePhdrHelper::fill_and_call(dl_phdr_cb callback, const void* l_addr, + const char* l_name, void* data) { + dl_phdr_info info; + info.dlpi_addr = reinterpret_cast<Elf::Addr>(l_addr); + info.dlpi_name = l_name; + info.dlpi_phdr = nullptr; + info.dlpi_phnum = 0; + + // Assuming l_addr points to Elf headers (in most cases, this is true), + // get the Phdr location from there. + // Unfortunately, when l_addr doesn't point to Elf headers, it may point + // to unmapped memory, or worse, unreadable memory. The only way to detect + // the latter without causing a SIGSEGV is to use the pointer in a system + // call that will try to read from there, and return an EFAULT error if + // it can't. One such system call is write(). It used to be possible to + // use a file descriptor on /dev/null for these kind of things, but recent + // Linux kernels never return an EFAULT error when using /dev/null. + // So instead, we use a self pipe. We do however need to read() from the + // read end of the pipe as well so as to not fill up the pipe buffer and + // block on subsequent writes. + // In the unlikely event reads from or write to the pipe fail for some + // other reason than EFAULT, we don't try any further and just skip setting + // the Phdr location for all subsequent libraries, rather than trying to + // start over with a new pipe. + int can_read = true; + if (valid_pipe) { + int ret; + char raw_ehdr[sizeof(Elf::Ehdr)]; + static_assert(sizeof(raw_ehdr) < PIPE_BUF, "PIPE_BUF is too small"); + do { + // writes are atomic when smaller than PIPE_BUF, per POSIX.1-2008. + ret = write(write_fd, l_addr, sizeof(raw_ehdr)); + } while (ret == -1 && errno == EINTR); + if (ret != sizeof(raw_ehdr)) { + if (ret == -1 && errno == EFAULT) { + can_read = false; + } else { + valid_pipe = false; + } + } else { + size_t nbytes = 0; + do { + // Per POSIX.1-2008, interrupted reads can return a length smaller + // than the given one instead of failing with errno EINTR. + ret = read(read_fd, raw_ehdr + nbytes, sizeof(raw_ehdr) - nbytes); + if (ret > 0) nbytes += ret; + } while ((nbytes != sizeof(raw_ehdr) && ret > 0) || + (ret == -1 && errno == EINTR)); + if (nbytes != sizeof(raw_ehdr)) { + valid_pipe = false; + } + } + } + + if (valid_pipe && can_read) { + const Elf::Ehdr* ehdr = Elf::Ehdr::validate(l_addr); + if (ehdr) { + info.dlpi_phdr = reinterpret_cast<const Elf::Phdr*>( + reinterpret_cast<const char*>(ehdr) + ehdr->e_phoff); + info.dlpi_phnum = ehdr->e_phnum; + } + } + + return callback(&info, sizeof(dl_phdr_info), data); +} + +int __wrap_dl_iterate_phdr(dl_phdr_cb callback, void* data) { +#if defined(ANDROID) + if (GetAndroidSDKVersion() >= 23) { + return dl_iterate_phdr(callback, data); + } +#endif + + DlIteratePhdrHelper helper; + AutoLock lock(&ElfLoader::Singleton.handlesMutex); + + if (dl_iterate_phdr) { + for (ElfLoader::LibHandleList::reverse_iterator it = + ElfLoader::Singleton.handles.rbegin(); + it < ElfLoader::Singleton.handles.rend(); ++it) { + BaseElf* elf = (*it)->AsBaseElf(); + if (!elf) { + continue; + } + int ret = helper.fill_and_call(callback, (*it)->GetBase(), + (*it)->GetPath(), data); + if (ret) return ret; + } + return dl_iterate_phdr(callback, data); + } + + /* For versions of Android that don't support dl_iterate_phdr (< 5.0), + * we go through the debugger helper data, which is known to be racy, but + * there's not much we can do about this :( . */ + if (!ElfLoader::Singleton.dbg) return -1; + + for (ElfLoader::DebuggerHelper::iterator it = + ElfLoader::Singleton.dbg.begin(); + it < ElfLoader::Singleton.dbg.end(); ++it) { + int ret = helper.fill_and_call(callback, it->l_addr, it->l_name, data); + if (ret) return ret; + } + return 0; +} + +#ifdef __ARM_EABI__ +const void* __wrap___gnu_Unwind_Find_exidx(void* pc, int* pcount) { + RefPtr<LibHandle> handle = ElfLoader::Singleton.GetHandleByPtr(pc); + if (handle) return handle->FindExidx(pcount); + if (__gnu_Unwind_Find_exidx) return __gnu_Unwind_Find_exidx(pc, pcount); + *pcount = 0; + return nullptr; +} +#endif + +/** + * faulty.lib public API + */ + +MFBT_API size_t __dl_get_mappable_length(void* handle) { + if (!handle) return 0; + return reinterpret_cast<LibHandle*>(handle)->GetMappableLength(); +} + +MFBT_API void* __dl_mmap(void* handle, void* addr, size_t length, + off_t offset) { + if (!handle) return nullptr; + return reinterpret_cast<LibHandle*>(handle)->MappableMMap(addr, length, + offset); +} + +MFBT_API void __dl_munmap(void* handle, void* addr, size_t length) { + if (!handle) return; + return reinterpret_cast<LibHandle*>(handle)->MappableMUnmap(addr, length); +} + +MFBT_API bool IsSignalHandlingBroken() { + return ElfLoader::Singleton.isSignalHandlingBroken(); +} + +namespace { + +/** + * Returns the part after the last '/' for the given path + */ +const char* LeafName(const char* path) { + const char* lastSlash = strrchr(path, '/'); + if (lastSlash) return lastSlash + 1; + return path; +} + +/** + * Run the given lambda while holding the internal lock of the system linker. + * To take the lock, we call the system dl_iterate_phdr and invoke the lambda + * from the callback, which is called while the lock is held. Return true on + * success. + */ +template <class Lambda> +static bool RunWithSystemLinkerLock(Lambda&& aLambda) { + if (!dl_iterate_phdr) { + // No dl_iterate_phdr support. + return false; + } + +#if defined(ANDROID) + if (GetAndroidSDKVersion() < 23) { + // dl_iterate_phdr is _not_ protected by a lock on Android < 23. + // Also return false here if we failed to get the version. + return false; + } +#endif + + dl_iterate_phdr( + [](dl_phdr_info*, size_t, void* lambda) -> int { + (*static_cast<Lambda*>(lambda))(); + // Return 1 to stop iterating. + return 1; + }, + &aLambda); + return true; +} + +} /* Anonymous namespace */ + +/** + * LibHandle + */ +LibHandle::~LibHandle() { free(path); } + +const char* LibHandle::GetName() const { + return path ? LeafName(path) : nullptr; +} + +size_t LibHandle::GetMappableLength() const { + if (!mappable) mappable = GetMappable(); + if (!mappable) return 0; + return mappable->GetLength(); +} + +void* LibHandle::MappableMMap(void* addr, size_t length, off_t offset) const { + if (!mappable) mappable = GetMappable(); + if (!mappable) return MAP_FAILED; + void* mapped = mappable->mmap(addr, length, PROT_READ, MAP_PRIVATE, offset); + return mapped; +} + +void LibHandle::MappableMUnmap(void* addr, size_t length) const { + if (mappable) mappable->munmap(addr, length); +} + +/** + * SystemElf + */ +already_AddRefed<LibHandle> SystemElf::Load(const char* path, int flags) { + /* The Android linker returns a handle when the file name matches an + * already loaded library, even when the full path doesn't exist */ + if (path && path[0] == '/' && (access(path, F_OK) == -1)) { + DEBUG_LOG("dlopen(\"%s\", 0x%x) = %p", path, flags, (void*)nullptr); + ElfLoader::Singleton.lastError = "Specified file does not exist"; + return nullptr; + } + + ElfLoader::Singleton.lastError = nullptr; // Use system dlerror. + void* handle = dlopen(path, flags); + DEBUG_LOG("dlopen(\"%s\", 0x%x) = %p", path, flags, handle); + if (handle) { + SystemElf* elf = new SystemElf(path, handle); + ElfLoader::Singleton.Register(elf); + RefPtr<LibHandle> lib(elf); + return lib.forget(); + } + return nullptr; +} + +SystemElf::~SystemElf() { + if (!dlhandle) return; + DEBUG_LOG("dlclose(%p [\"%s\"])", dlhandle, GetPath()); + ElfLoader::Singleton.lastError = nullptr; // Use system dlerror. + dlclose(dlhandle); + ElfLoader::Singleton.Forget(this); +} + +void* SystemElf::GetSymbolPtr(const char* symbol) const { + ElfLoader::Singleton.lastError = nullptr; // Use system dlerror. + void* sym = dlsym(dlhandle, symbol); + DEBUG_LOG("dlsym(%p [\"%s\"], \"%s\") = %p", dlhandle, GetPath(), symbol, + sym); + return sym; +} + +Mappable* SystemElf::GetMappable() const { + const char* path = GetPath(); + if (!path) return nullptr; +#ifdef ANDROID + /* On Android, if we don't have the full path, try in /system/lib */ + const char* name = LeafName(path); + std::string systemPath; + if (name == path) { + systemPath = "/system/lib/"; + systemPath += path; + path = systemPath.c_str(); + } +#endif + + return MappableFile::Create(path); +} + +#ifdef __ARM_EABI__ +const void* SystemElf::FindExidx(int* pcount) const { + /* TODO: properly implement when ElfLoader::GetHandleByPtr + does return SystemElf handles */ + *pcount = 0; + return nullptr; +} +#endif + +/** + * ElfLoader + */ + +/* Unique ElfLoader instance */ +ElfLoader ElfLoader::Singleton; + +already_AddRefed<LibHandle> ElfLoader::Load(const char* path, int flags, + LibHandle* parent) { + /* Ensure logging is initialized or refresh if environment changed. */ + Logging::Init(); + + /* Ensure self_elf initialization. */ + if (!self_elf) Init(); + + RefPtr<LibHandle> handle; + + /* Handle dlopen(nullptr) directly. */ + if (!path) { + handle = SystemElf::Load(nullptr, flags); + return handle.forget(); + } + + /* TODO: Handle relative paths correctly */ + const char* name = LeafName(path); + + /* Search the list of handles we already have for a match. When the given + * path is not absolute, compare file names, otherwise compare full paths. */ + if (name == path) { + AutoLock lock(&handlesMutex); + for (LibHandleList::iterator it = handles.begin(); it < handles.end(); ++it) + if ((*it)->GetName() && (strcmp((*it)->GetName(), name) == 0)) { + handle = *it; + return handle.forget(); + } + } else { + AutoLock lock(&handlesMutex); + for (LibHandleList::iterator it = handles.begin(); it < handles.end(); ++it) + if ((*it)->GetPath() && (strcmp((*it)->GetPath(), path) == 0)) { + handle = *it; + return handle.forget(); + } + } + + char* abs_path = nullptr; + const char* requested_path = path; + + /* When the path is not absolute and the library is being loaded for + * another, first try to load the library from the directory containing + * that parent library. */ + if ((name == path) && parent) { + const char* parentPath = parent->GetPath(); + abs_path = new char[strlen(parentPath) + strlen(path)]; + strcpy(abs_path, parentPath); + char* slash = strrchr(abs_path, '/'); + strcpy(slash + 1, path); + path = abs_path; + } + + Mappable* mappable = GetMappableFromPath(path); + + /* Try loading with the custom linker if we have a Mappable */ + if (mappable) handle = CustomElf::Load(mappable, path, flags); + + /* Try loading with the system linker if everything above failed */ + if (!handle) handle = SystemElf::Load(path, flags); + + /* If we didn't have an absolute path and haven't been able to load + * a library yet, try in the system search path */ + if (!handle && abs_path) handle = SystemElf::Load(name, flags); + + delete[] abs_path; + DEBUG_LOG("ElfLoader::Load(\"%s\", 0x%x, %p [\"%s\"]) = %p", requested_path, + flags, reinterpret_cast<void*>(parent), + parent ? parent->GetPath() : "", static_cast<void*>(handle)); + + return handle.forget(); +} + +already_AddRefed<LibHandle> ElfLoader::GetHandleByPtr(void* addr) { + AutoLock lock(&handlesMutex); + /* Scan the list of handles we already have for a match */ + for (LibHandleList::iterator it = handles.begin(); it < handles.end(); ++it) { + if ((*it)->Contains(addr)) { + RefPtr<LibHandle> lib = *it; + return lib.forget(); + } + } + return nullptr; +} + +Mappable* ElfLoader::GetMappableFromPath(const char* path) { + const char* name = LeafName(path); + Mappable* mappable = nullptr; + RefPtr<Zip> zip; + const char* subpath; + if ((subpath = strchr(path, '!'))) { + char* zip_path = strndup(path, subpath - path); + while (*(++subpath) == '/') { + } + zip = ZipCollection::GetZip(zip_path); + free(zip_path); + Zip::Stream s; + if (zip && zip->GetStream(subpath, &s)) { + /* When the MOZ_LINKER_EXTRACT environment variable is set to "1", + * compressed libraries are going to be (temporarily) extracted as + * files, in the directory pointed by the MOZ_LINKER_CACHE + * environment variable. */ + const char* extract = getenv("MOZ_LINKER_EXTRACT"); + if (extract && !strncmp(extract, "1", 2 /* Including '\0' */)) + mappable = MappableExtractFile::Create(name, zip, &s); + if (!mappable) { + if (s.GetType() == Zip::Stream::DEFLATE) { + mappable = MappableDeflate::Create(name, zip, &s); + } + } + } + } + /* If we couldn't load above, try with a MappableFile */ + if (!mappable && !zip) mappable = MappableFile::Create(path); + + return mappable; +} + +void ElfLoader::Register(LibHandle* handle) { + AutoLock lock(&handlesMutex); + handles.push_back(handle); +} + +void ElfLoader::Register(CustomElf* handle) { + Register(static_cast<LibHandle*>(handle)); + if (dbg) { + // We could race with the system linker when modifying the debug map, so + // only do so while holding the system linker's internal lock. + RunWithSystemLinkerLock([this, handle] { dbg.Add(handle); }); + } +} + +void ElfLoader::Forget(LibHandle* handle) { + /* Ensure logging is initialized or refresh if environment changed. */ + Logging::Init(); + + AutoLock lock(&handlesMutex); + LibHandleList::iterator it = + std::find(handles.begin(), handles.end(), handle); + if (it != handles.end()) { + DEBUG_LOG("ElfLoader::Forget(%p [\"%s\"])", reinterpret_cast<void*>(handle), + handle->GetPath()); + handles.erase(it); + } else { + DEBUG_LOG("ElfLoader::Forget(%p [\"%s\"]): Handle not found", + reinterpret_cast<void*>(handle), handle->GetPath()); + } +} + +void ElfLoader::Forget(CustomElf* handle) { + Forget(static_cast<LibHandle*>(handle)); + if (dbg) { + // We could race with the system linker when modifying the debug map, so + // only do so while holding the system linker's internal lock. + RunWithSystemLinkerLock([this, handle] { dbg.Remove(handle); }); + } +} + +void ElfLoader::Init() { + Dl_info info; + /* On Android < 4.1 can't reenter dl* functions. So when the library + * containing this code is dlopen()ed, it can't call dladdr from a + * static initializer. */ + if (dladdr(_DYNAMIC, &info) != 0) { + self_elf = LoadedElf::Create(info.dli_fname, info.dli_fbase); + } +#if defined(ANDROID) + // On Android < 5.0, resolving weak symbols via dlsym doesn't work. + // The weak symbols Gecko uses are in either libc or libm, so we + // wrap those such that this linker does symbol resolution for them. + if (GetAndroidSDKVersion() < 21) { + if (dladdr(FunctionPtr(syscall), &info) != 0) { + libc = LoadedElf::Create(info.dli_fname, info.dli_fbase); + } + if (dladdr(FunctionPtr<int (*)(double)>(isnan), &info) != 0) { + libm = LoadedElf::Create(info.dli_fname, info.dli_fbase); + } + } +#endif +} + +ElfLoader::~ElfLoader() { + LibHandleList list; + + if (!Singleton.IsShutdownExpected()) { + MOZ_CRASH("Unexpected shutdown"); + } + + /* Release self_elf and libc */ + self_elf = nullptr; +#if defined(ANDROID) + libc = nullptr; + libm = nullptr; +#endif + + AutoLock lock(&handlesMutex); + /* Build up a list of all library handles with direct (external) references. + * We actually skip system library handles because we want to keep at least + * some of these open. Most notably, Mozilla codebase keeps a few libgnome + * libraries deliberately open because of the mess that libORBit destruction + * is. dlclose()ing these libraries actually leads to problems. */ + for (LibHandleList::reverse_iterator it = handles.rbegin(); + it < handles.rend(); ++it) { + if ((*it)->DirectRefCount()) { + if (SystemElf* se = (*it)->AsSystemElf()) { + se->Forget(); + } else { + list.push_back(*it); + } + } + } + /* Force release all external references to the handles collected above */ + for (LibHandleList::iterator it = list.begin(); it < list.end(); ++it) { + while ((*it)->ReleaseDirectRef()) { + } + } + /* Remove the remaining system handles. */ + if (handles.size()) { + list = handles; + for (LibHandleList::reverse_iterator it = list.rbegin(); it < list.rend(); + ++it) { + if ((*it)->AsSystemElf()) { + DEBUG_LOG( + "ElfLoader::~ElfLoader(): Remaining handle for \"%s\" " + "[%" PRIdPTR " direct refs, %" PRIdPTR " refs total]", + (*it)->GetPath(), (*it)->DirectRefCount(), (*it)->refCount()); + } else { + DEBUG_LOG( + "ElfLoader::~ElfLoader(): Unexpected remaining handle for \"%s\" " + "[%" PRIdPTR " direct refs, %" PRIdPTR " refs total]", + (*it)->GetPath(), (*it)->DirectRefCount(), (*it)->refCount()); + /* Not removing, since it could have references to other libraries, + * destroying them as a side effect, and possibly leaving dangling + * pointers in the handle list we're scanning */ + } + } + } + pthread_mutex_destroy(&handlesMutex); +} + +#ifdef __ARM_EABI__ +int ElfLoader::__wrap_aeabi_atexit(void* that, ElfLoader::Destructor destructor, + void* dso_handle) { + Singleton.destructors.push_back( + DestructorCaller(destructor, that, dso_handle)); + return 0; +} +#else +int ElfLoader::__wrap_cxa_atexit(ElfLoader::Destructor destructor, void* that, + void* dso_handle) { + Singleton.destructors.push_back( + DestructorCaller(destructor, that, dso_handle)); + return 0; +} +#endif + +void ElfLoader::__wrap_cxa_finalize(void* dso_handle) { + /* Call all destructors for the given DSO handle in reverse order they were + * registered. */ + std::vector<DestructorCaller>::reverse_iterator it; + for (it = Singleton.destructors.rbegin(); it < Singleton.destructors.rend(); + ++it) { + if (it->IsForHandle(dso_handle)) { + it->Call(); + } + } +} + +void ElfLoader::DestructorCaller::Call() { + if (destructor) { + DEBUG_LOG("ElfLoader::DestructorCaller::Call(%p, %p, %p)", + FunctionPtr(destructor), object, dso_handle); + destructor(object); + destructor = nullptr; + } +} + +ElfLoader::DebuggerHelper::DebuggerHelper() + : dbg(nullptr), firstAdded(nullptr) { + /* Find ELF auxiliary vectors. + * + * The kernel stores the following data on the stack when starting a + * program: + * argc + * argv[0] (pointer into argv strings defined below) + * argv[1] (likewise) + * ... + * argv[argc - 1] (likewise) + * nullptr + * envp[0] (pointer into environment strings defined below) + * envp[1] (likewise) + * ... + * envp[n] (likewise) + * nullptr + * ... (more NULLs on some platforms such as Android 4.3) + * auxv[0] (first ELF auxiliary vector) + * auxv[1] (second ELF auxiliary vector) + * ... + * auxv[p] (last ELF auxiliary vector) + * (AT_NULL, nullptr) + * padding + * argv strings, separated with '\0' + * environment strings, separated with '\0' + * nullptr + * + * What we are after are the auxv values defined by the following struct. + */ + struct AuxVector { + Elf::Addr type; + Elf::Addr value; + }; + + /* Pointer to the environment variables list */ + extern char** environ; + + /* The environment may have changed since the program started, in which + * case the environ variables list isn't the list the kernel put on stack + * anymore. But in this new list, variables that didn't change still point + * to the strings the kernel put on stack. It is quite unlikely that two + * modified environment variables point to two consecutive strings in memory, + * so we assume that if two consecutive environment variables point to two + * consecutive strings, we found strings the kernel put on stack. */ + char** env; + for (env = environ; *env; env++) + if (*env + strlen(*env) + 1 == env[1]) break; + if (!*env) return; + + /* Next, we scan the stack backwards to find a pointer to one of those + * strings we found above, which will give us the location of the original + * envp list. As we are looking for pointers, we need to look at 32-bits or + * 64-bits aligned values, depening on the architecture. */ + char** scan = reinterpret_cast<char**>(reinterpret_cast<uintptr_t>(*env) & + ~(sizeof(void*) - 1)); + while (*env != *scan) scan--; + + /* Finally, scan forward to find the last environment variable pointer and + * thus the first auxiliary vector. */ + while (*scan++) + ; + + /* Some platforms have more NULLs here, so skip them if we encounter them */ + while (!*scan) scan++; + + AuxVector* auxv = reinterpret_cast<AuxVector*>(scan); + + /* The two values of interest in the auxiliary vectors are AT_PHDR and + * AT_PHNUM, which gives us the the location and size of the ELF program + * headers. */ + Array<Elf::Phdr> phdrs; + char* base = nullptr; + while (auxv->type) { + if (auxv->type == AT_PHDR) { + phdrs.Init(reinterpret_cast<Elf::Phdr*>(auxv->value)); + /* Assume the base address is the first byte of the same page */ + base = reinterpret_cast<char*>(PageAlignedPtr(auxv->value)); + } + if (auxv->type == AT_PHNUM) phdrs.Init(auxv->value); + auxv++; + } + + if (!phdrs) { + DEBUG_LOG("Couldn't find program headers"); + return; + } + + /* In some cases, the address for the program headers we get from the + * auxiliary vectors is not mapped, because of the PT_LOAD segments + * definitions in the program executable. Trying to map anonymous memory + * with a hint giving the base address will return a different address + * if something is mapped there, and the base address otherwise. */ + MappedPtr mem(MemoryRange::mmap(base, PageSize(), PROT_NONE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)); + if (mem == base) { + /* If program headers aren't mapped, try to map them */ + int fd = open("/proc/self/exe", O_RDONLY); + if (fd == -1) { + DEBUG_LOG("Failed to open /proc/self/exe"); + return; + } + mem.Assign( + MemoryRange::mmap(base, PageSize(), PROT_READ, MAP_PRIVATE, fd, 0)); + /* If we don't manage to map at the right address, just give up. */ + if (mem != base) { + DEBUG_LOG("Couldn't read program headers"); + return; + } + } + /* Sanity check: the first bytes at the base address should be an ELF + * header. */ + if (!Elf::Ehdr::validate(base)) { + DEBUG_LOG("Couldn't find program base"); + return; + } + + /* Search for the program PT_DYNAMIC segment */ + Array<Elf::Dyn> dyns; + for (Array<Elf::Phdr>::iterator phdr = phdrs.begin(); phdr < phdrs.end(); + ++phdr) { + /* While the program headers are expected within the first mapped page of + * the program executable, the executable PT_LOADs may actually make them + * loaded at an address that is not the wanted base address of the + * library. We thus need to adjust the base address, compensating for the + * virtual address of the PT_LOAD segment corresponding to offset 0. */ + if (phdr->p_type == PT_LOAD && phdr->p_offset == 0) base -= phdr->p_vaddr; + if (phdr->p_type == PT_DYNAMIC) + dyns.Init(base + phdr->p_vaddr, phdr->p_filesz); + } + if (!dyns) { + DEBUG_LOG("Failed to find PT_DYNAMIC section in program"); + return; + } + + /* Search for the DT_DEBUG information */ + for (Array<Elf::Dyn>::iterator dyn = dyns.begin(); dyn < dyns.end(); ++dyn) { + if (dyn->d_tag == DT_DEBUG) { + dbg = reinterpret_cast<r_debug*>(dyn->d_un.d_ptr); + break; + } + } + DEBUG_LOG("DT_DEBUG points at %p", static_cast<void*>(dbg)); +} + +/** + * Helper class to ensure the given pointer is writable within the scope of + * an instance. Permissions to the memory page where the pointer lies are + * restored to their original value when the instance is destroyed. + */ +class EnsureWritable { + public: + template <typename T> + explicit EnsureWritable(T* ptr, size_t length_ = sizeof(T)) { + MOZ_ASSERT(length_ < PageSize()); + prot = -1; + page = MAP_FAILED; + + char* firstPage = PageAlignedPtr(reinterpret_cast<char*>(ptr)); + char* lastPageEnd = + PageAlignedEndPtr(reinterpret_cast<char*>(ptr) + length_); + length = lastPageEnd - firstPage; + uintptr_t start = reinterpret_cast<uintptr_t>(firstPage); + uintptr_t end; + + prot = getProt(start, &end); + if (prot == -1 || (start + length) > end) MOZ_CRASH(); + + if (prot & PROT_WRITE) { + success = true; + return; + } + + page = firstPage; + int ret = mprotect(page, length, prot | PROT_WRITE); + success = ret == 0; + if (!success) { + ERROR("mprotect(%p, %zu, %d) = %d (errno=%d; %s)", page, length, + prot | PROT_WRITE, ret, errno, strerror(errno)); + } + } + + bool IsWritable() const { return success; } + + ~EnsureWritable() { + if (success && page != MAP_FAILED) { + mprotect(page, length, prot); + } + } + + private: + int getProt(uintptr_t addr, uintptr_t* end) { + /* The interesting part of the /proc/self/maps format looks like: + * startAddr-endAddr rwxp */ + int result = 0; + AutoCloseFILE f(fopen("/proc/self/maps", "r")); + while (f) { + unsigned long long startAddr, endAddr; + char perms[5]; + if (fscanf(f, "%llx-%llx %4s %*1024[^\n] ", &startAddr, &endAddr, + perms) != 3) + return -1; + if (addr < startAddr || addr >= endAddr) continue; + if (perms[0] == 'r') + result |= PROT_READ; + else if (perms[0] != '-') + return -1; + if (perms[1] == 'w') + result |= PROT_WRITE; + else if (perms[1] != '-') + return -1; + if (perms[2] == 'x') + result |= PROT_EXEC; + else if (perms[2] != '-') + return -1; + *end = endAddr; + return result; + } + return -1; + } + + int prot; + void* page; + size_t length; + bool success; +}; + +/** + * The system linker maintains a doubly linked list of library it loads + * for use by the debugger. Unfortunately, it also uses the list pointers + * in a lot of operations and adding our data in the list is likely to + * trigger crashes when the linker tries to use data we don't provide or + * that fall off the amount data we allocated. Fortunately, the linker only + * traverses the list forward and accesses the head of the list from a + * private pointer instead of using the value in the r_debug structure. + * This means we can safely add members at the beginning of the list. + * Unfortunately, gdb checks the coherency of l_prev values, so we have + * to adjust the l_prev value for the first element the system linker + * knows about. Fortunately, it doesn't use l_prev, and the first element + * is not ever going to be released before our elements, since it is the + * program executable, so the system linker should not be changing + * r_debug::r_map. + */ +void ElfLoader::DebuggerHelper::Add(ElfLoader::link_map* map) { + if (!dbg->r_brk) return; + + dbg->r_state = r_debug::RT_ADD; + dbg->r_brk(); + + if (!firstAdded) { + /* When adding a library for the first time, r_map points to data + * handled by the system linker, and that data may be read-only */ + EnsureWritable w(&dbg->r_map->l_prev); + if (!w.IsWritable()) { + dbg->r_state = r_debug::RT_CONSISTENT; + dbg->r_brk(); + return; + } + + firstAdded = map; + dbg->r_map->l_prev = map; + } else + dbg->r_map->l_prev = map; + + map->l_prev = nullptr; + map->l_next = dbg->r_map; + + dbg->r_map = map; + dbg->r_state = r_debug::RT_CONSISTENT; + dbg->r_brk(); +} + +void ElfLoader::DebuggerHelper::Remove(ElfLoader::link_map* map) { + if (!dbg->r_brk) return; + + dbg->r_state = r_debug::RT_DELETE; + dbg->r_brk(); + + if (map == firstAdded) { + /* When removing the first added library, its l_next is going to be + * data handled by the system linker, and that data may be read-only */ + EnsureWritable w(&map->l_next->l_prev); + if (!w.IsWritable()) { + dbg->r_state = r_debug::RT_CONSISTENT; + dbg->r_brk(); + return; + } + + firstAdded = map->l_prev; + map->l_next->l_prev = map->l_prev; + } else if (map->l_next) { + map->l_next->l_prev = map->l_prev; + } + + if (dbg->r_map == map) + dbg->r_map = map->l_next; + else if (map->l_prev) { + map->l_prev->l_next = map->l_next; + } + dbg->r_state = r_debug::RT_CONSISTENT; + dbg->r_brk(); +} + +#if defined(ANDROID) && defined(__NR_sigaction) +/* As some system libraries may be calling signal() or sigaction() to + * set a SIGSEGV handler, effectively breaking MappableSeekableZStream, + * or worse, restore our SIGSEGV handler with wrong flags (which using + * signal() will do), we want to hook into the system's sigaction() to + * replace it with our own wrapper instead, so that our handler is never + * replaced. We used to only do that with libraries this linker loads, + * but it turns out at least one system library does call signal() and + * breaks us (libsc-a3xx.so on the Samsung Galaxy S4). + * As libc's signal (bsd_signal/sysv_signal, really) calls sigaction + * under the hood, instead of calling the signal system call directly, + * we only need to hook sigaction. This is true for both bionic and + * glibc. + */ + +/* libc's sigaction */ +extern "C" int sigaction(int signum, const struct sigaction* act, + struct sigaction* oldact); + +/* Simple reimplementation of sigaction. This is roughly equivalent + * to the assembly that comes in bionic, but not quite equivalent to + * glibc's implementation, so we only use this on Android. */ +int sys_sigaction(int signum, const struct sigaction* act, + struct sigaction* oldact) { + return syscall(__NR_sigaction, signum, act, oldact); +} + +/* Replace the first instructions of the given function with a jump + * to the given new function. */ +template <typename T> +static bool Divert(T func, T new_func) { + void* ptr = FunctionPtr(func); + uintptr_t addr = reinterpret_cast<uintptr_t>(ptr); + +# if defined(__i386__) + // A 32-bit jump is a 5 bytes instruction. + EnsureWritable w(ptr, 5); + *reinterpret_cast<unsigned char*>(addr) = 0xe9; // jmp + *reinterpret_cast<intptr_t*>(addr + 1) = + reinterpret_cast<uintptr_t>(new_func) - addr - 5; // target displacement + return true; +# elif defined(__arm__) || defined(__aarch64__) + const unsigned char trampoline[] = { +# ifdef __arm__ + // .thumb + 0x46, 0x04, // nop + 0x78, 0x47, // bx pc + 0x46, 0x04, // nop + // .arm + 0x04, 0xf0, 0x1f, 0xe5, // ldr pc, [pc, #-4] + // .word <new_func> +# else // __aarch64__ + 0x50, 0x00, + 0x00, 0x58, // ldr x16, [pc, #8] ; x16 (aka ip0) is the first + 0x00, 0x02, + 0x1f, 0xd6, // br x16 ; intra-procedure-call + // .word <new_func.lo> ; scratch register. + // .word <new_func.hi> +# endif + }; + const unsigned char* start; +# ifdef __arm__ + if (addr & 0x01) { + /* Function is thumb, the actual address of the code is without the + * least significant bit. */ + addr--; + /* The arm part of the trampoline needs to be 32-bit aligned */ + if (addr & 0x02) + start = trampoline; + else + start = trampoline + 2; + } else { + /* Function is arm, we only need the arm part of the trampoline */ + start = trampoline + 6; + } +# else // __aarch64__ + start = trampoline; +# endif + + size_t len = sizeof(trampoline) - (start - trampoline); + EnsureWritable w(reinterpret_cast<void*>(addr), len + sizeof(void*)); + memcpy(reinterpret_cast<void*>(addr), start, len); + *reinterpret_cast<void**>(addr + len) = FunctionPtr(new_func); + __builtin___clear_cache(reinterpret_cast<char*>(addr), + reinterpret_cast<char*>(addr + len + sizeof(void*))); + return true; +# else + return false; +# endif +} +#else +# define sys_sigaction sigaction +template <typename T> +static bool Divert(T func, T new_func) { + return false; +} +#endif + +namespace { + +/* Clock that only accounts for time spent in the current process. */ +static uint64_t ProcessTimeStamp_Now() { + struct timespec ts; + int rv = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts); + + if (rv != 0) { + return 0; + } + + uint64_t baseNs = (uint64_t)ts.tv_sec * 1000000000; + return baseNs + (uint64_t)ts.tv_nsec; +} + +} // namespace + +/* Data structure used to pass data to the temporary signal handler, + * as well as triggering a test crash. */ +struct TmpData { + volatile int crash_int; + volatile uint64_t crash_timestamp; +}; + +SEGVHandler::SEGVHandler() + : initialized(false), + registeredHandler(false), + signalHandlingBroken(true), + signalHandlingSlow(true) { + /* Ensure logging is initialized before the DEBUG_LOG in the test_handler. + * As this constructor runs before the ElfLoader constructor (by effect + * of ElfLoader inheriting from this class), this also initializes on behalf + * of ElfLoader and DebuggerHelper. */ + Logging::Init(); + + /* Initialize oldStack.ss_flags to an invalid value when used to set + * an alternative stack, meaning we haven't got information about the + * original alternative stack and thus don't mean to restore it in + * the destructor. */ + oldStack.ss_flags = SS_ONSTACK; + + /* Get the current segfault signal handler. */ + struct sigaction old_action; + sys_sigaction(SIGSEGV, nullptr, &old_action); + + /* Some devices don't provide useful information to their SIGSEGV handlers, + * making it impossible for on-demand decompression to work. To check if + * we're on such a device, setup a temporary handler and deliberately + * trigger a segfault. The handler will set signalHandlingBroken if the + * provided information is bogus. + * Some other devices have a kernel option enabled that makes SIGSEGV handler + * have an overhead so high that it affects how on-demand decompression + * performs. The handler will also set signalHandlingSlow if the triggered + * SIGSEGV took too much time. */ + struct sigaction action; + action.sa_sigaction = &SEGVHandler::test_handler; + sigemptyset(&action.sa_mask); + action.sa_flags = SA_SIGINFO | SA_NODEFER; + action.sa_restorer = nullptr; + stackPtr.Assign(MemoryRange::mmap(nullptr, PageSize(), PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)); + if (stackPtr.get() == MAP_FAILED) return; + if (sys_sigaction(SIGSEGV, &action, nullptr)) return; + + TmpData* data = reinterpret_cast<TmpData*>(stackPtr.get()); + data->crash_timestamp = ProcessTimeStamp_Now(); + mprotect(stackPtr, stackPtr.GetLength(), PROT_NONE); + data->crash_int = 123; + /* Restore the original segfault signal handler. */ + sys_sigaction(SIGSEGV, &old_action, nullptr); + stackPtr.Assign(MAP_FAILED, 0); +} + +void SEGVHandler::FinishInitialization() { + /* Ideally, we'd need some locking here, but in practice, we're not + * going to race with another thread. */ + initialized = true; + + if (signalHandlingBroken || signalHandlingSlow) return; + + typedef int (*sigaction_func)(int, const struct sigaction*, + struct sigaction*); + + sigaction_func libc_sigaction; + +#if defined(ANDROID) + /* Android > 4.4 comes with a sigaction wrapper in a LD_PRELOADed library + * (libsigchain) for ART. That wrapper kind of does the same trick as we + * do, so we need extra care in handling it. + * - Divert the libc's sigaction, assuming the LD_PRELOADed library uses + * it under the hood (which is more or less true according to the source + * of that library, since it's doing a lookup in RTLD_NEXT) + * - With the LD_PRELOADed library in place, all calls to sigaction from + * from system libraries will go to the LD_PRELOADed library. + * - The LD_PRELOADed library calls to sigaction go to our __wrap_sigaction. + * - The calls to sigaction from libraries faulty.lib loads are sent to + * the LD_PRELOADed library. + * In practice, for signal handling, this means: + * - The signal handler registered to the kernel is ours. + * - Our handler redispatches to the LD_PRELOADed library's if there's a + * segfault we don't handle. + * - The LD_PRELOADed library redispatches according to whatever system + * library or faulty.lib-loaded library set with sigaction. + * + * When there is no sigaction wrapper in place: + * - Divert the libc's sigaction. + * - Calls to sigaction from system library and faulty.lib-loaded libraries + * all go to the libc's sigaction, which end up in our __wrap_sigaction. + * - The signal handler registered to the kernel is ours. + * - Our handler redispatches according to whatever system library or + * faulty.lib-loaded library set with sigaction. + */ + void* libc = dlopen("libc.so", RTLD_GLOBAL | RTLD_LAZY); + if (libc) { + /* + * Lollipop bionic only has a small trampoline in sigaction, with the real + * work happening in __sigaction. Divert there instead of sigaction if it + * exists. Bug 1154803 + */ + libc_sigaction = + reinterpret_cast<sigaction_func>(dlsym(libc, "__sigaction")); + + if (!libc_sigaction) { + libc_sigaction = + reinterpret_cast<sigaction_func>(dlsym(libc, "sigaction")); + } + } else +#endif + { + libc_sigaction = sigaction; + } + + if (!Divert(libc_sigaction, __wrap_sigaction)) return; + + /* Setup an alternative stack if the already existing one is not big + * enough, or if there is none. */ + if (sigaltstack(nullptr, &oldStack) == 0) { + if (oldStack.ss_flags == SS_ONSTACK) oldStack.ss_flags = 0; + if (!oldStack.ss_sp || oldStack.ss_size < stackSize) { + stackPtr.Assign(MemoryRange::mmap(nullptr, stackSize, + PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)); + if (stackPtr.get() == MAP_FAILED) return; + stack_t stack; + stack.ss_sp = stackPtr; + stack.ss_size = stackSize; + stack.ss_flags = 0; + if (sigaltstack(&stack, nullptr) != 0) return; + } + } + /* Register our own handler, and store the already registered one in + * SEGVHandler's struct sigaction member */ + action.sa_sigaction = &SEGVHandler::handler; + action.sa_flags = SA_SIGINFO | SA_NODEFER | SA_ONSTACK; + registeredHandler = !sys_sigaction(SIGSEGV, &action, &this->action); +} + +SEGVHandler::~SEGVHandler() { + /* Restore alternative stack for signals */ + if (oldStack.ss_flags != SS_ONSTACK) sigaltstack(&oldStack, nullptr); + /* Restore original signal handler */ + if (registeredHandler) sys_sigaction(SIGSEGV, &this->action, nullptr); +} + +/* Test handler for a deliberately triggered SIGSEGV that determines whether + * useful information is provided to signal handlers, particularly whether + * si_addr is filled in properly, and whether the segfault handler is called + * quickly enough. */ +void SEGVHandler::test_handler(int signum, siginfo_t* info, void* context) { + SEGVHandler& that = ElfLoader::Singleton; + if (signum == SIGSEGV && info && info->si_addr == that.stackPtr.get()) + that.signalHandlingBroken = false; + mprotect(that.stackPtr, that.stackPtr.GetLength(), PROT_READ | PROT_WRITE); + TmpData* data = reinterpret_cast<TmpData*>(that.stackPtr.get()); + uint64_t latency = ProcessTimeStamp_Now() - data->crash_timestamp; + DEBUG_LOG("SEGVHandler latency: %" PRIu64, latency); + /* See bug 886736 for timings on different devices, 150 µs is reasonably above + * the latency on "working" devices and seems to be short enough to not incur + * a huge overhead to on-demand decompression. */ + if (latency <= 150000) that.signalHandlingSlow = false; +} + +/* TODO: "properly" handle signal masks and flags */ +void SEGVHandler::handler(int signum, siginfo_t* info, void* context) { + // ASSERT(signum == SIGSEGV); + DEBUG_LOG("Caught segmentation fault @%p", info->si_addr); + + /* Redispatch to the registered handler */ + SEGVHandler& that = ElfLoader::Singleton; + if (that.action.sa_flags & SA_SIGINFO) { + DEBUG_LOG("Redispatching to registered handler @%p", + FunctionPtr(that.action.sa_sigaction)); + that.action.sa_sigaction(signum, info, context); + } else if (that.action.sa_handler == SIG_DFL) { + DEBUG_LOG("Redispatching to default handler"); + /* Reset the handler to the default one, and trigger it. */ + sys_sigaction(signum, &that.action, nullptr); + raise(signum); + } else if (that.action.sa_handler != SIG_IGN) { + DEBUG_LOG("Redispatching to registered handler @%p", + FunctionPtr(that.action.sa_handler)); + that.action.sa_handler(signum); + } else { + DEBUG_LOG("Ignoring"); + } +} + +int SEGVHandler::__wrap_sigaction(int signum, const struct sigaction* act, + struct sigaction* oldact) { + SEGVHandler& that = ElfLoader::Singleton; + + /* Use system sigaction() function for all but SIGSEGV signals. */ + if (!that.registeredHandler || (signum != SIGSEGV)) + return sys_sigaction(signum, act, oldact); + + if (oldact) *oldact = that.action; + if (act) that.action = *act; + return 0; +} diff --git a/mozglue/linker/ElfLoader.h b/mozglue/linker/ElfLoader.h new file mode 100644 index 0000000000..059c092f6d --- /dev/null +++ b/mozglue/linker/ElfLoader.h @@ -0,0 +1,634 @@ +/* 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/. */ + +#ifndef ElfLoader_h +#define ElfLoader_h + +#include <vector> +#include <dlfcn.h> +#include <signal.h> +#include "mozilla/Atomics.h" +#include "mozilla/RefCounted.h" +#include "mozilla/RefPtr.h" +#include "mozilla/UniquePtr.h" +#include "Zip.h" +#include "Elfxx.h" +#include "Mappable.h" + +/** + * dlfcn.h replacement functions + */ +extern "C" { +void* __wrap_dlopen(const char* path, int flags); +const char* __wrap_dlerror(void); +void* __wrap_dlsym(void* handle, const char* symbol); +int __wrap_dlclose(void* handle); + +#ifndef HAVE_DLADDR +typedef struct { + const char* dli_fname; + void* dli_fbase; + const char* dli_sname; + void* dli_saddr; +} Dl_info; +#endif +int __wrap_dladdr(const void* addr, Dl_info* info); + +struct dl_phdr_info { + Elf::Addr dlpi_addr; + const char* dlpi_name; + const Elf::Phdr* dlpi_phdr; + Elf::Half dlpi_phnum; +}; + +typedef int (*dl_phdr_cb)(struct dl_phdr_info*, size_t, void*); +int __wrap_dl_iterate_phdr(dl_phdr_cb callback, void* data); + +#ifdef __ARM_EABI__ +const void* __wrap___gnu_Unwind_Find_exidx(void* pc, int* pcount); +#endif + +/** + * faulty.lib public API + */ +MFBT_API size_t __dl_get_mappable_length(void* handle); + +MFBT_API void* __dl_mmap(void* handle, void* addr, size_t length, off_t offset); + +MFBT_API void __dl_munmap(void* handle, void* addr, size_t length); + +MFBT_API bool IsSignalHandlingBroken(); +} + +/* Forward declarations for use in LibHandle */ +class BaseElf; +class CustomElf; +class SystemElf; + +/** + * Specialize RefCounted template for LibHandle. We may get references to + * LibHandles during the execution of their destructor, so we need + * RefCounted<LibHandle>::Release to support some reentrancy. See further + * below. + */ +class LibHandle; + +namespace mozilla { +namespace detail { + +template <> +inline void RefCounted<LibHandle, AtomicRefCount>::Release() const; + +#ifdef DEBUG +template <> +inline RefCounted<LibHandle, AtomicRefCount>::~RefCounted() { + MOZ_ASSERT(mRefCnt == 0x7fffdead); +} +#endif + +} /* namespace detail */ +} /* namespace mozilla */ + +/** + * Abstract class for loaded libraries. Libraries may be loaded through the + * system linker or this linker, both cases will be derived from this class. + */ +class LibHandle : public mozilla::external::AtomicRefCounted<LibHandle> { + public: + MOZ_DECLARE_REFCOUNTED_TYPENAME(LibHandle) + /** + * Constructor. Takes the path of the loaded library and will store a copy + * of the leaf name. + */ + LibHandle(const char* path) + : directRefCnt(0), + path(path ? strdup(path) : nullptr), + mappable(nullptr) {} + + /** + * Destructor. + */ + virtual ~LibHandle(); + + /** + * Returns the pointer to the address to which the given symbol resolves + * inside the library. It is not supposed to resolve the symbol in other + * libraries, although in practice, it will for system libraries. + */ + virtual void* GetSymbolPtr(const char* symbol) const = 0; + + /** + * Returns whether the given address is part of the virtual address space + * covered by the loaded library. + */ + virtual bool Contains(void* addr) const = 0; + + /** + * Returns the base address of the loaded library. + */ + virtual void* GetBase() const = 0; + + /** + * Returns the file name of the library without the containing directory. + */ + const char* GetName() const; + + /** + * Returns the full path of the library, when available. Otherwise, returns + * the file name. + */ + const char* GetPath() const { return path; } + + /** + * Library handles can be referenced from other library handles or + * externally (when dlopen()ing using this linker). We need to be + * able to distinguish between the two kind of referencing for better + * bookkeeping. + */ + void AddDirectRef() { + mozilla::external::AtomicRefCounted<LibHandle>::AddRef(); + ++directRefCnt; + } + + /** + * Releases a direct reference, and returns whether there are any direct + * references left. + */ + bool ReleaseDirectRef() { + const MozRefCountType count = --directRefCnt; + MOZ_ASSERT(count + 1 > 0); + MOZ_ASSERT(count + 1 <= + mozilla::external::AtomicRefCounted<LibHandle>::refCount()); + mozilla::external::AtomicRefCounted<LibHandle>::Release(); + return !!count; + } + + /** + * Returns the number of direct references + */ + MozRefCountType DirectRefCount() { return directRefCnt; } + + /** + * Returns the complete size of the file or stream behind the library + * handle. + */ + size_t GetMappableLength() const; + + /** + * Returns a memory mapping of the file or stream behind the library + * handle. + */ + void* MappableMMap(void* addr, size_t length, off_t offset) const; + + /** + * Unmaps a memory mapping of the file or stream behind the library + * handle. + */ + void MappableMUnmap(void* addr, size_t length) const; + +#ifdef __ARM_EABI__ + /** + * Find the address and entry count of the ARM.exidx section + * associated with the library + */ + virtual const void* FindExidx(int* pcount) const = 0; +#endif + + protected: + /** + * Returns a mappable object for use by MappableMMap and related functions. + */ + virtual Mappable* GetMappable() const = 0; + + /** + * Returns the instance, casted as the wanted type. Returns nullptr if + * that's not the actual type. (short of a better way to do this without + * RTTI) + */ + friend class ElfLoader; + friend class CustomElf; + friend class SEGVHandler; + friend int __wrap_dl_iterate_phdr(dl_phdr_cb callback, void* data); + virtual BaseElf* AsBaseElf() { return nullptr; } + virtual SystemElf* AsSystemElf() { return nullptr; } + + private: + mozilla::Atomic<MozRefCountType> directRefCnt; + char* path; + + /* Mappable object keeping the result of GetMappable() */ + mutable RefPtr<Mappable> mappable; +}; + +/** + * Specialized RefCounted<LibHandle>::Release. Under normal operation, when + * mRefCnt reaches 0, the LibHandle is deleted. Its mRefCnt is however + * increased to 1 on normal builds, and 0x7fffdead on debug builds so that the + * LibHandle can still be referenced while the destructor is executing. The + * mRefCnt is allowed to grow > 0x7fffdead, but not to decrease under that + * value, which would mean too many Releases from within the destructor. + */ +namespace mozilla { +namespace detail { + +template <> +inline void RefCounted<LibHandle, AtomicRefCount>::Release() const { +#ifdef DEBUG + if (mRefCnt > 0x7fff0000) MOZ_ASSERT(mRefCnt > 0x7fffdead); +#endif + MOZ_ASSERT(mRefCnt > 0); + if (mRefCnt > 0) { + if (0 == --mRefCnt) { +#ifdef DEBUG + mRefCnt = 0x7fffdead; +#else + ++mRefCnt; +#endif + delete static_cast<const LibHandle*>(this); + } + } +} + +} /* namespace detail */ +} /* namespace mozilla */ + +/** + * Class handling libraries loaded by the system linker + */ +class SystemElf : public LibHandle { + public: + /** + * Returns a new SystemElf for the given path. The given flags are passed + * to dlopen(). + */ + static already_AddRefed<LibHandle> Load(const char* path, int flags); + + /** + * Inherited from LibHandle + */ + virtual ~SystemElf(); + virtual void* GetSymbolPtr(const char* symbol) const; + virtual bool Contains(void* addr) const { return false; /* UNIMPLEMENTED */ } + virtual void* GetBase() const { return nullptr; /* UNIMPLEMENTED */ } + +#ifdef __ARM_EABI__ + virtual const void* FindExidx(int* pcount) const; +#endif + + protected: + virtual Mappable* GetMappable() const; + + /** + * Returns the instance, casted as SystemElf. (short of a better way to do + * this without RTTI) + */ + friend class ElfLoader; + virtual SystemElf* AsSystemElf() { return this; } + + /** + * Remove the reference to the system linker handle. This avoids dlclose() + * being called when the instance is destroyed. + */ + void Forget() { dlhandle = nullptr; } + + private: + /** + * Private constructor + */ + SystemElf(const char* path, void* handle) + : LibHandle(path), dlhandle(handle) {} + + /* Handle as returned by system dlopen() */ + void* dlhandle; +}; + +/** + * The ElfLoader registers its own SIGSEGV handler to handle segmentation + * faults within the address space of the loaded libraries. It however + * allows a handler to be set for faults in other places, and redispatches + * to the handler set through signal() or sigaction(). + */ +class SEGVHandler { + public: + bool hasRegisteredHandler() { + if (!initialized) FinishInitialization(); + return registeredHandler; + } + + bool isSignalHandlingBroken() { return signalHandlingBroken; } + + static int __wrap_sigaction(int signum, const struct sigaction* act, + struct sigaction* oldact); + + protected: + SEGVHandler(); + ~SEGVHandler(); + + private: + /** + * The constructor doesn't do all initialization, and the tail is done + * at a later time. + */ + void FinishInitialization(); + + /** + * SIGSEGV handler registered with __wrap_signal or __wrap_sigaction. + */ + struct sigaction action; + + /** + * ElfLoader SIGSEGV handler. + */ + static void handler(int signum, siginfo_t* info, void* context); + + /** + * Temporary test handler. + */ + static void test_handler(int signum, siginfo_t* info, void* context); + + /** + * Size of the alternative stack. The printf family requires more than 8KB + * of stack, and our signal handler may print a few things. + */ + static const size_t stackSize = 12 * 1024; + + /** + * Alternative stack information used before initialization. + */ + stack_t oldStack; + + /** + * Pointer to an alternative stack for signals. Only set if oldStack is + * not set or not big enough. + */ + MappedPtr stackPtr; + + bool initialized; + bool registeredHandler; + bool signalHandlingBroken; + bool signalHandlingSlow; +}; + +/** + * Elf Loader class in charge of loading and bookkeeping libraries. + */ +class ElfLoader : public SEGVHandler { + public: + /** + * The Elf Loader instance + */ + static ElfLoader Singleton; + + /** + * Loads the given library with the given flags. Equivalent to dlopen() + * The extra "parent" argument optionally gives the handle of the library + * requesting the given library to be loaded. The loader may look in the + * directory containing that parent library for the library to load. + */ + already_AddRefed<LibHandle> Load(const char* path, int flags, + LibHandle* parent = nullptr); + + /** + * Returns the handle of the library containing the given address in + * its virtual address space, i.e. the library handle for which + * LibHandle::Contains returns true. Its purpose is to allow to + * implement dladdr(). + */ + already_AddRefed<LibHandle> GetHandleByPtr(void* addr); + + /** + * Returns a Mappable object for the path. Paths in the form + * /foo/bar/baz/archive!/directory/lib.so + * try to load the directory/lib.so in /foo/bar/baz/archive, provided + * that file is a Zip archive. + */ + static Mappable* GetMappableFromPath(const char* path); + + void ExpectShutdown(bool val) { expect_shutdown = val; } + bool IsShutdownExpected() { return expect_shutdown; } + + private: + bool expect_shutdown; + + protected: + /** + * Registers the given handle. This method is meant to be called by + * LibHandle subclass creators. + */ + void Register(LibHandle* handle); + void Register(CustomElf* handle); + + /** + * Forget about the given handle. This method is meant to be called by + * LibHandle subclass destructors. + */ + void Forget(LibHandle* handle); + void Forget(CustomElf* handle); + + friend class SystemElf; + friend const char* __wrap_dlerror(void); + friend void* __wrap_dlsym(void* handle, const char* symbol); + friend int __wrap_dlclose(void* handle); + /* __wrap_dlerror() returns this custom last error if non-null or the system + * dlerror() value if this is null. Must refer to a string constant. */ + mozilla::Atomic<const char*, mozilla::Relaxed> lastError; + + private: + ElfLoader() : expect_shutdown(true), lastError(nullptr) { + pthread_mutex_init(&handlesMutex, nullptr); + } + + ~ElfLoader(); + + /* Initialization code that can't run during static initialization. */ + void Init(); + + /* System loader handle for the library/program containing our code. This + * is used to resolve wrapped functions. */ + RefPtr<LibHandle> self_elf; + +#if defined(ANDROID) + /* System loader handle for the libc. This is used to resolve weak symbols + * that some libcs contain that the Android linker won't dlsym(). Normally, + * we wouldn't treat non-Android differently, but glibc uses versioned + * symbols which this linker doesn't support. */ + RefPtr<LibHandle> libc; + + /* And for libm. */ + RefPtr<LibHandle> libm; +#endif + + /* Bookkeeping */ + typedef std::vector<LibHandle*> LibHandleList; + LibHandleList handles; + + pthread_mutex_t handlesMutex; + + protected: + friend class CustomElf; + friend class LoadedElf; + + /* Definition of static destructors as to be used for C++ ABI compatibility */ + typedef void (*Destructor)(void* object); + + /** + * C++ ABI makes static initializers register destructors through a specific + * atexit interface. On glibc/linux systems, the dso_handle is a pointer + * within a given library. On bionic/android systems, it is an undefined + * symbol. Making sense of the value is not really important, and all that + * is really important is that it is different for each loaded library, so + * that they can be discriminated when shutting down. For convenience, on + * systems where the dso handle is a symbol, that symbol is resolved to + * point at corresponding CustomElf. + * + * Destructors are registered with __*_atexit with an associated object to + * be passed as argument when it is called. + * + * When __cxa_finalize is called, destructors registered for the given + * DSO handle are called in the reverse order they were registered. + */ +#ifdef __ARM_EABI__ + static int __wrap_aeabi_atexit(void* that, Destructor destructor, + void* dso_handle); +#else + static int __wrap_cxa_atexit(Destructor destructor, void* that, + void* dso_handle); +#endif + + static void __wrap_cxa_finalize(void* dso_handle); + + /** + * Registered destructor. Keeps track of the destructor function pointer, + * associated object to call it with, and DSO handle. + */ + class DestructorCaller { + public: + DestructorCaller(Destructor destructor, void* object, void* dso_handle) + : destructor(destructor), object(object), dso_handle(dso_handle) {} + + /** + * Call the destructor function with the associated object. + * Call only once, see CustomElf::~CustomElf. + */ + void Call(); + + /** + * Returns whether the destructor is associated to the given DSO handle + */ + bool IsForHandle(void* handle) const { return handle == dso_handle; } + + private: + Destructor destructor; + void* object; + void* dso_handle; + }; + + private: + /* Keep track of all registered destructors */ + std::vector<DestructorCaller> destructors; + + /* Forward declaration, see further below */ + class DebuggerHelper; + + public: + /* Loaded object descriptor for the debugger interface below*/ + struct link_map { + /* Base address of the loaded object. */ + const void* l_addr; + /* File name */ + const char* l_name; + /* Address of the PT_DYNAMIC segment. */ + const void* l_ld; + + private: + friend class ElfLoader::DebuggerHelper; + /* Double linked list of loaded objects. */ + link_map *l_next, *l_prev; + }; + + private: + /* Data structure used by the linker to give details about shared objects it + * loaded to debuggers. This is normally defined in link.h, but Android + * headers lack this file. */ + struct r_debug { + /* Version number of the protocol. */ + int r_version; + + /* Head of the linked list of loaded objects. */ + link_map* r_map; + + /* Function to be called when updates to the linked list of loaded objects + * are going to occur. The function is to be called before and after + * changes. */ + void (*r_brk)(void); + + /* Indicates to the debugger what state the linked list of loaded objects + * is in when the function above is called. */ + enum { + RT_CONSISTENT, /* Changes are complete */ + RT_ADD, /* Beginning to add a new object */ + RT_DELETE /* Beginning to remove an object */ + } r_state; + }; + + /* Memory representation of ELF Auxiliary Vectors */ + struct AuxVector { + Elf::Addr type; + Elf::Addr value; + }; + + /* Helper class used to integrate libraries loaded by this linker in + * r_debug */ + class DebuggerHelper { + public: + DebuggerHelper(); + + void Init(AuxVector* auvx); + + explicit operator bool() { return dbg; } + + /* Make the debugger aware of a new loaded object */ + void Add(link_map* map); + + /* Make the debugger aware of the unloading of an object */ + void Remove(link_map* map); + + /* Iterates over all link_maps */ + class iterator { + public: + const link_map* operator->() const { return item; } + + const link_map& operator++() { + item = item->l_next; + return *item; + } + + bool operator<(const iterator& other) const { + if (other.item == nullptr) return item ? true : false; + MOZ_CRASH( + "DebuggerHelper::iterator::operator< called with something else " + "than DebuggerHelper::end()"); + } + + protected: + friend class DebuggerHelper; + explicit iterator(const link_map* item) : item(item) {} + + private: + const link_map* item; + }; + + iterator begin() const { return iterator(dbg ? dbg->r_map : nullptr); } + + iterator end() const { return iterator(nullptr); } + + private: + r_debug* dbg; + link_map* firstAdded; + }; + friend int __wrap_dl_iterate_phdr(dl_phdr_cb callback, void* data); + DebuggerHelper dbg; +}; + +#endif /* ElfLoader_h */ diff --git a/mozglue/linker/Elfxx.h b/mozglue/linker/Elfxx.h new file mode 100644 index 0000000000..4baf923b55 --- /dev/null +++ b/mozglue/linker/Elfxx.h @@ -0,0 +1,246 @@ +/* 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/. */ + +#ifndef Elfxx_h +#define Elfxx_h + +#include "Utils.h" + +/** + * Android system headers have two different elf.h file. The one under linux/ + * is the most complete on older Android API versions without unified headers. + */ +#if defined(ANDROID) && __ANDROID_API__ < 21 && !defined(__ANDROID_API_L__) +# include <linux/elf.h> +#else +# include <elf.h> +#endif +#include <endian.h> + +#if defined(__ARM_EABI__) && !defined(PT_ARM_EXIDX) +# define PT_ARM_EXIDX 0x70000001 +#endif + +/** + * Generic ELF macros for the target system + */ +#ifdef __LP64__ +# define Elf_(type) Elf64_##type +# define ELFCLASS ELFCLASS64 +# define ELF_R_TYPE ELF64_R_TYPE +# define ELF_R_SYM ELF64_R_SYM +# ifndef ELF_ST_BIND +# define ELF_ST_BIND ELF64_ST_BIND +# endif +#else +# define Elf_(type) Elf32_##type +# define ELFCLASS ELFCLASS32 +# define ELF_R_TYPE ELF32_R_TYPE +# define ELF_R_SYM ELF32_R_SYM +# ifndef ELF_ST_BIND +# define ELF_ST_BIND ELF32_ST_BIND +# endif +#endif + +#ifndef __BYTE_ORDER +# error Cannot find endianness +#endif + +#if __BYTE_ORDER == __LITTLE_ENDIAN +# define ELFDATA ELFDATA2LSB +#elif __BYTE_ORDER == __BIG_ENDIAN +# define ELFDATA ELFDATA2MSB +#endif + +#ifdef __linux__ +# define ELFOSABI ELFOSABI_LINUX +# ifdef EI_ABIVERSION +# define ELFABIVERSION 0 +# endif +#else +# error Unknown ELF OSABI +#endif + +#if defined(__i386__) +# define ELFMACHINE EM_386 + +// Doing this way probably doesn't scale to other architectures +# define R_ABS R_386_32 +# define R_GLOB_DAT R_386_GLOB_DAT +# define R_JMP_SLOT R_386_JMP_SLOT +# define R_RELATIVE R_386_RELATIVE +# define RELOC(n) DT_REL##n +# define UNSUPPORTED_RELOC(n) DT_RELA##n +# define STR_RELOC(n) "DT_REL" #n +# define Reloc Rel + +#elif defined(__x86_64__) +# define ELFMACHINE EM_X86_64 + +# define R_ABS R_X86_64_64 +# define R_GLOB_DAT R_X86_64_GLOB_DAT +# define R_JMP_SLOT R_X86_64_JUMP_SLOT +# define R_RELATIVE R_X86_64_RELATIVE +# define RELOC(n) DT_RELA##n +# define UNSUPPORTED_RELOC(n) DT_REL##n +# define STR_RELOC(n) "DT_RELA" #n +# define Reloc Rela + +#elif defined(__arm__) +# define ELFMACHINE EM_ARM + +# ifndef R_ARM_ABS32 +# define R_ARM_ABS32 2 +# endif +# ifndef R_ARM_GLOB_DAT +# define R_ARM_GLOB_DAT 21 +# endif +# ifndef R_ARM_JUMP_SLOT +# define R_ARM_JUMP_SLOT 22 +# endif +# ifndef R_ARM_RELATIVE +# define R_ARM_RELATIVE 23 +# endif + +# define R_ABS R_ARM_ABS32 +# define R_GLOB_DAT R_ARM_GLOB_DAT +# define R_JMP_SLOT R_ARM_JUMP_SLOT +# define R_RELATIVE R_ARM_RELATIVE +# define RELOC(n) DT_REL##n +# define UNSUPPORTED_RELOC(n) DT_RELA##n +# define STR_RELOC(n) "DT_REL" #n +# define Reloc Rel + +#elif defined(__aarch64__) +# define ELFMACHINE EM_AARCH64 + +# define R_ABS R_AARCH64_ABS64 +# define R_GLOB_DAT R_AARCH64_GLOB_DAT +# define R_JMP_SLOT R_AARCH64_JUMP_SLOT +# define R_RELATIVE R_AARCH64_RELATIVE +# define RELOC(n) DT_RELA##n +# define UNSUPPORTED_RELOC(n) DT_REL##n +# define STR_RELOC(n) "DT_RELA" #n +# define Reloc Rela + +#else +# error Unknown ELF machine type +#endif + +/** + * Android system headers don't have all definitions + */ +#ifndef STN_UNDEF +# define STN_UNDEF 0 +#endif +#ifndef DT_INIT_ARRAY +# define DT_INIT_ARRAY 25 +#endif +#ifndef DT_FINI_ARRAY +# define DT_FINI_ARRAY 26 +#endif +#ifndef DT_INIT_ARRAYSZ +# define DT_INIT_ARRAYSZ 27 +#endif +#ifndef DT_FINI_ARRAYSZ +# define DT_FINI_ARRAYSZ 28 +#endif +#ifndef DT_RELACOUNT +# define DT_RELACOUNT 0x6ffffff9 +#endif +#ifndef DT_RELCOUNT +# define DT_RELCOUNT 0x6ffffffa +#endif +#ifndef DT_VERSYM +# define DT_VERSYM 0x6ffffff0 +#endif +#ifndef DT_VERDEF +# define DT_VERDEF 0x6ffffffc +#endif +#ifndef DT_VERDEFNUM +# define DT_VERDEFNUM 0x6ffffffd +#endif +#ifndef DT_VERNEED +# define DT_VERNEED 0x6ffffffe +#endif +#ifndef DT_VERNEEDNUM +# define DT_VERNEEDNUM 0x6fffffff +#endif +#ifndef DT_FLAGS_1 +# define DT_FLAGS_1 0x6ffffffb +#endif +#ifndef DT_FLAGS +# define DT_FLAGS 30 +#endif +#ifndef DF_SYMBOLIC +# define DF_SYMBOLIC 0x00000002 +#endif +#ifndef DF_TEXTREL +# define DF_TEXTREL 0x00000004 +#endif + +namespace Elf { + +/** + * Define a few basic Elf Types + */ +typedef Elf_(Phdr) Phdr; +typedef Elf_(Dyn) Dyn; +typedef Elf_(Sym) Sym; +typedef Elf_(Addr) Addr; +typedef Elf_(Word) Word; +typedef Elf_(Half) Half; + +/** + * Helper class around the standard Elf header struct + */ +struct Ehdr : public Elf_(Ehdr) { + /** + * Equivalent to reinterpret_cast<const Ehdr *>(buf), but additionally + * checking that this is indeed an Elf header and that the Elf type + * corresponds to that of the system + */ + static const Ehdr* validate(const void* buf); +}; + +/** + * Elf String table + */ +class Strtab : public UnsizedArray<const char> { + public: + /** + * Returns the string at the given index in the table + */ + const char* GetStringAt(off_t index) const { + return &UnsizedArray<const char>::operator[](index); + } +}; + +/** + * Helper class around Elf relocation. + */ +struct Rel : public Elf_(Rel) { + /** + * Returns the addend for the relocation, which is the value stored + * at r_offset. + */ + Addr GetAddend(void* base) const { + return *(reinterpret_cast<const Addr*>(reinterpret_cast<const char*>(base) + + r_offset)); + } +}; + +/** + * Helper class around Elf relocation with addend. + */ +struct Rela : public Elf_(Rela) { + /** + * Returns the addend for the relocation. + */ + Addr GetAddend(void* base) const { return r_addend; } +}; + +} /* namespace Elf */ + +#endif /* Elfxx_h */ diff --git a/mozglue/linker/Linker.h b/mozglue/linker/Linker.h new file mode 100644 index 0000000000..77ddb06ecc --- /dev/null +++ b/mozglue/linker/Linker.h @@ -0,0 +1,24 @@ +/* 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/. */ + +#ifndef Linker_h +#define Linker_h + +#ifdef MOZ_LINKER +# include "ElfLoader.h" +# define __wrap_sigaction SEGVHandler::__wrap_sigaction +#else +# include <dlfcn.h> +# include <link.h> +# include <signal.h> +# define __wrap_sigaction sigaction +# define __wrap_dlopen dlopen +# define __wrap_dlerror dlerror +# define __wrap_dlsym dlsym +# define __wrap_dlclose dlclose +# define __wrap_dladdr dladdr +# define __wrap_dl_iterate_phdr dl_iterate_phdr +#endif + +#endif diff --git a/mozglue/linker/Logging.cpp b/mozglue/linker/Logging.cpp new file mode 100644 index 0000000000..e61c7835d2 --- /dev/null +++ b/mozglue/linker/Logging.cpp @@ -0,0 +1,7 @@ +/* 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 "Logging.h" + +Logging Logging::Singleton; diff --git a/mozglue/linker/Logging.h b/mozglue/linker/Logging.h new file mode 100644 index 0000000000..1e66ea41de --- /dev/null +++ b/mozglue/linker/Logging.h @@ -0,0 +1,72 @@ +/* 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/. */ + +#ifndef Logging_h +#define Logging_h + +#include <cstdlib> +#include "mozilla/Likely.h" +#include "mozilla/MacroArgs.h" + +#ifdef ANDROID +# include <android/log.h> +# define LOG(...) \ + __android_log_print(ANDROID_LOG_INFO, "GeckoLinker", __VA_ARGS__) +# define WARN(...) \ + __android_log_print(ANDROID_LOG_WARN, "GeckoLinker", __VA_ARGS__) +# define ERROR(...) \ + __android_log_print(ANDROID_LOG_ERROR, "GeckoLinker", __VA_ARGS__) +#else +# include <cstdio> + +/* Expand to 1 or m depending on whether there is one argument or more + * given. */ +# define MOZ_ONE_OR_MORE_ARGS_IMPL2(_1, _2, _3, _4, _5, _6, _7, _8, _9, N, \ + ...) \ + N +# define MOZ_ONE_OR_MORE_ARGS_IMPL(args) MOZ_ONE_OR_MORE_ARGS_IMPL2 args +# define MOZ_ONE_OR_MORE_ARGS(...) \ + MOZ_ONE_OR_MORE_ARGS_IMPL((__VA_ARGS__, m, m, m, m, m, m, m, m, 1, 0)) + +# define MOZ_MACRO_GLUE(a, b) a b + +/* Some magic to choose between LOG1 and LOGm depending on the number of + * arguments */ +# define MOZ_CHOOSE_LOG(...) \ + MOZ_MACRO_GLUE(MOZ_CONCAT(LOG, MOZ_ONE_OR_MORE_ARGS(__VA_ARGS__)), \ + (__VA_ARGS__)) + +# define LOG1(format) fprintf(stderr, format "\n") +# define LOGm(format, ...) fprintf(stderr, format "\n", __VA_ARGS__) +# define LOG(...) MOZ_CHOOSE_LOG(__VA_ARGS__) +# define WARN(...) MOZ_CHOOSE_LOG("Warning: " __VA_ARGS__) +# define ERROR(...) MOZ_CHOOSE_LOG("Error: " __VA_ARGS__) + +#endif + +class Logging { + public: + static bool isVerbose() { return Singleton.verbose; } + + private: + bool verbose; + + public: + static void Init() { + const char* env = getenv("MOZ_DEBUG_LINKER"); + if (env && *env == '1') Singleton.verbose = true; + } + + private: + static Logging Singleton; +}; + +#define DEBUG_LOG(...) \ + do { \ + if (MOZ_UNLIKELY(Logging::isVerbose())) { \ + LOG(__VA_ARGS__); \ + } \ + } while (0) + +#endif /* Logging_h */ diff --git a/mozglue/linker/Mappable.cpp b/mozglue/linker/Mappable.cpp new file mode 100644 index 0000000000..cacd6a46f6 --- /dev/null +++ b/mozglue/linker/Mappable.cpp @@ -0,0 +1,376 @@ +/* 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 <fcntl.h> +#include <unistd.h> +#include <sys/mman.h> +#include <sys/stat.h> +#include <cstring> +#include <cstdlib> +#include <cstdio> +#include <string> + +#include "Mappable.h" + +#include "mozilla/IntegerPrintfMacros.h" +#include "mozilla/UniquePtr.h" + +#ifdef ANDROID +# include "mozilla/Ashmem.h" +#endif +#include <sys/stat.h> +#include <errno.h> +#include "ElfLoader.h" +#include "XZStream.h" +#include "Logging.h" + +using mozilla::MakeUnique; +using mozilla::UniquePtr; + +class CacheValidator { + public: + CacheValidator(const char* aCachedLibPath, Zip* aZip, Zip::Stream* aStream) + : mCachedLibPath(aCachedLibPath) { + static const char kChecksumSuffix[] = ".crc"; + + mCachedChecksumPath = + MakeUnique<char[]>(strlen(aCachedLibPath) + sizeof(kChecksumSuffix)); + sprintf(mCachedChecksumPath.get(), "%s%s", aCachedLibPath, kChecksumSuffix); + DEBUG_LOG("mCachedChecksumPath: %s", mCachedChecksumPath.get()); + + mChecksum = aStream->GetCRC32(); + DEBUG_LOG("mChecksum: %x", mChecksum); + } + + // Returns whether the cache is valid and up-to-date. + bool IsValid() const { + // Validate based on checksum. + RefPtr<Mappable> checksumMap = + MappableFile::Create(mCachedChecksumPath.get()); + if (!checksumMap) { + // Force caching if checksum is missing in cache. + return false; + } + + DEBUG_LOG("Comparing %x with %s", mChecksum, mCachedChecksumPath.get()); + MappedPtr checksumBuf = checksumMap->mmap(nullptr, checksumMap->GetLength(), + PROT_READ, MAP_PRIVATE, 0); + if (checksumBuf == MAP_FAILED) { + WARN("Couldn't map %s to validate checksum", mCachedChecksumPath.get()); + return false; + } + if (memcmp(checksumBuf, &mChecksum, sizeof(mChecksum))) { + return false; + } + return !access(mCachedLibPath.c_str(), R_OK); + } + + // Caches the APK-provided checksum used in future cache validations. + void CacheChecksum() const { + AutoCloseFD fd(open(mCachedChecksumPath.get(), + O_TRUNC | O_RDWR | O_CREAT | O_NOATIME, + S_IRUSR | S_IWUSR)); + if (fd == -1) { + WARN("Couldn't open %s to update checksum", mCachedChecksumPath.get()); + return; + } + + DEBUG_LOG("Updating checksum %s", mCachedChecksumPath.get()); + + const size_t size = sizeof(mChecksum); + size_t written = 0; + while (written < size) { + ssize_t ret = + write(fd, reinterpret_cast<const uint8_t*>(&mChecksum) + written, + size - written); + if (ret >= 0) { + written += ret; + } else if (errno != EINTR) { + WARN("Writing checksum %s failed with errno %d", + mCachedChecksumPath.get(), errno); + break; + } + } + } + + private: + const std::string mCachedLibPath; + UniquePtr<char[]> mCachedChecksumPath; + uint32_t mChecksum; +}; + +Mappable* MappableFile::Create(const char* path) { + int fd = open(path, O_RDONLY); + if (fd != -1) return new MappableFile(fd); + return nullptr; +} + +MemoryRange MappableFile::mmap(const void* addr, size_t length, int prot, + int flags, off_t offset) { + MOZ_ASSERT(fd != -1); + MOZ_ASSERT(!(flags & MAP_SHARED)); + flags |= MAP_PRIVATE; + + return MemoryRange::mmap(const_cast<void*>(addr), length, prot, flags, fd, + offset); +} + +void MappableFile::finalize() { + /* Close file ; equivalent to close(fd.forget()) */ + fd = -1; +} + +size_t MappableFile::GetLength() const { + struct stat st; + return fstat(fd, &st) ? 0 : st.st_size; +} + +Mappable* MappableExtractFile::Create(const char* name, Zip* zip, + Zip::Stream* stream) { + MOZ_ASSERT(zip && stream); + + const char* cachePath = getenv("MOZ_LINKER_CACHE"); + if (!cachePath || !*cachePath) { + WARN( + "MOZ_LINKER_EXTRACT is set, but not MOZ_LINKER_CACHE; " + "not extracting"); + return nullptr; + } + + // Ensure that the cache dir is private. + chmod(cachePath, 0770); + + UniquePtr<char[]> path = + MakeUnique<char[]>(strlen(cachePath) + strlen(name) + 2); + sprintf(path.get(), "%s/%s", cachePath, name); + + CacheValidator validator(path.get(), zip, stream); + if (validator.IsValid()) { + DEBUG_LOG("Reusing %s", static_cast<char*>(path.get())); + return MappableFile::Create(path.get()); + } + DEBUG_LOG("Extracting to %s", static_cast<char*>(path.get())); + AutoCloseFD fd; + fd = open(path.get(), O_TRUNC | O_RDWR | O_CREAT | O_NOATIME, + S_IRUSR | S_IWUSR); + if (fd == -1) { + ERROR("Couldn't open %s to decompress library", path.get()); + return nullptr; + } + AutoUnlinkFile file(path.release()); + if (stream->GetType() == Zip::Stream::DEFLATE) { + if (ftruncate(fd, stream->GetUncompressedSize()) == -1) { + ERROR("Couldn't ftruncate %s to decompress library", file.get()); + return nullptr; + } + /* Map the temporary file for use as inflate buffer */ + MappedPtr buffer(MemoryRange::mmap(nullptr, stream->GetUncompressedSize(), + PROT_WRITE, MAP_SHARED, fd, 0)); + if (buffer == MAP_FAILED) { + ERROR("Couldn't map %s to decompress library", file.get()); + return nullptr; + } + + z_stream zStream = stream->GetZStream(buffer); + + /* Decompress */ + if (inflateInit2(&zStream, -MAX_WBITS) != Z_OK) { + ERROR("inflateInit failed: %s", zStream.msg); + return nullptr; + } + if (inflate(&zStream, Z_FINISH) != Z_STREAM_END) { + ERROR("inflate failed: %s", zStream.msg); + return nullptr; + } + if (inflateEnd(&zStream) != Z_OK) { + ERROR("inflateEnd failed: %s", zStream.msg); + return nullptr; + } + if (zStream.total_out != stream->GetUncompressedSize()) { + ERROR("File not fully uncompressed! %ld / %d", zStream.total_out, + static_cast<unsigned int>(stream->GetUncompressedSize())); + return nullptr; + } + } else if (XZStream::IsXZ(stream->GetBuffer(), stream->GetSize())) { + XZStream xzStream(stream->GetBuffer(), stream->GetSize()); + + if (!xzStream.Init()) { + ERROR("Couldn't initialize XZ decoder"); + return nullptr; + } + DEBUG_LOG("XZStream created, compressed=%" PRIuPTR + ", uncompressed=%" PRIuPTR, + xzStream.Size(), xzStream.UncompressedSize()); + + if (ftruncate(fd, xzStream.UncompressedSize()) == -1) { + ERROR("Couldn't ftruncate %s to decompress library", file.get()); + return nullptr; + } + MappedPtr buffer(MemoryRange::mmap(nullptr, xzStream.UncompressedSize(), + PROT_WRITE, MAP_SHARED, fd, 0)); + if (buffer == MAP_FAILED) { + ERROR("Couldn't map %s to decompress library", file.get()); + return nullptr; + } + const size_t written = xzStream.Decode(buffer, buffer.GetLength()); + DEBUG_LOG("XZStream decoded %" PRIuPTR, written); + if (written != buffer.GetLength()) { + ERROR("Error decoding XZ file %s", file.get()); + return nullptr; + } + } else { + return nullptr; + } + + validator.CacheChecksum(); + return new MappableExtractFile(fd.forget(), file.release()); +} + +/** + * _MappableBuffer is a buffer which content can be mapped at different + * locations in the virtual address space. + * On Linux, uses a (deleted) temporary file on a tmpfs for sharable content. + * On Android, uses ashmem. + */ +class _MappableBuffer : public MappedPtr { + public: + /** + * Returns a _MappableBuffer instance with the given name and the given + * length. + */ + static _MappableBuffer* Create(const char* name, size_t length) { + AutoCloseFD fd; + const char* ident; +#ifdef ANDROID + /* On Android, initialize an ashmem region with the given length */ + fd = mozilla::android::ashmem_create(name, length); + ident = name; +#else + /* On Linux, use /dev/shm as base directory for temporary files, assuming + * it's on tmpfs */ + /* TODO: check that /dev/shm is tmpfs */ + char path[256]; + sprintf(path, "/dev/shm/%s.XXXXXX", name); + fd = mkstemp(path); + if (fd == -1) return nullptr; + unlink(path); + ftruncate(fd, length); + ident = path; +#endif + + void* buf = + ::mmap(nullptr, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (buf != MAP_FAILED) { + DEBUG_LOG("Decompression buffer of size 0x%" PRIxPTR + " in " +#ifdef ANDROID + "ashmem " +#endif + "\"%s\", mapped @%p", + length, ident, buf); + return new _MappableBuffer(fd.forget(), buf, length); + } + return nullptr; + } + + void* mmap(const void* addr, size_t length, int prot, int flags, + off_t offset) { + MOZ_ASSERT(fd != -1); +#ifdef ANDROID + /* Mapping ashmem MAP_PRIVATE is like mapping anonymous memory, even when + * there is content in the ashmem */ + if (flags & MAP_PRIVATE) { + flags &= ~MAP_PRIVATE; + flags |= MAP_SHARED; + } +#endif + return ::mmap(const_cast<void*>(addr), length, prot, flags, fd, offset); + } + + private: + _MappableBuffer(int fd, void* buf, size_t length) + : MappedPtr(buf, length), fd(fd) {} + + /* File descriptor for the temporary file or ashmem */ + AutoCloseFD fd; +}; + +Mappable* MappableDeflate::Create(const char* name, Zip* zip, + Zip::Stream* stream) { + MOZ_ASSERT(stream->GetType() == Zip::Stream::DEFLATE); + _MappableBuffer* buf = + _MappableBuffer::Create(name, stream->GetUncompressedSize()); + if (buf) return new MappableDeflate(buf, zip, stream); + return nullptr; +} + +MappableDeflate::MappableDeflate(_MappableBuffer* buf, Zip* zip, + Zip::Stream* stream) + : zip(zip), buffer(buf), zStream(stream->GetZStream(*buf)) {} + +MappableDeflate::~MappableDeflate() {} + +MemoryRange MappableDeflate::mmap(const void* addr, size_t length, int prot, + int flags, off_t offset) { + MOZ_ASSERT(buffer); + MOZ_ASSERT(!(flags & MAP_SHARED)); + flags |= MAP_PRIVATE; + + /* The deflate stream is uncompressed up to the required offset + length, if + * it hasn't previously been uncompressed */ + ssize_t missing = offset + length + zStream.avail_out - buffer->GetLength(); + if (missing > 0) { + uInt avail_out = zStream.avail_out; + zStream.avail_out = missing; + if ((*buffer == zStream.next_out) && + (inflateInit2(&zStream, -MAX_WBITS) != Z_OK)) { + ERROR("inflateInit failed: %s", zStream.msg); + return MemoryRange(MAP_FAILED, 0); + } + int ret = inflate(&zStream, Z_SYNC_FLUSH); + if (ret < 0) { + ERROR("inflate failed: %s", zStream.msg); + return MemoryRange(MAP_FAILED, 0); + } + if (ret == Z_NEED_DICT) { + ERROR("zstream requires a dictionary. %s", zStream.msg); + return MemoryRange(MAP_FAILED, 0); + } + zStream.avail_out = avail_out - missing + zStream.avail_out; + if (ret == Z_STREAM_END) { + if (inflateEnd(&zStream) != Z_OK) { + ERROR("inflateEnd failed: %s", zStream.msg); + return MemoryRange(MAP_FAILED, 0); + } + if (zStream.total_out != buffer->GetLength()) { + ERROR("File not fully uncompressed! %ld / %d", zStream.total_out, + static_cast<unsigned int>(buffer->GetLength())); + return MemoryRange(MAP_FAILED, 0); + } + } + } +#if defined(ANDROID) && defined(__arm__) + if (prot & PROT_EXEC) { + /* We just extracted data that may be executed in the future. + * We thus need to ensure Instruction and Data cache coherency. */ + DEBUG_LOG("cacheflush(%p, %p)", *buffer + offset, + *buffer + (offset + length)); + cacheflush(reinterpret_cast<uintptr_t>(*buffer + offset), + reinterpret_cast<uintptr_t>(*buffer + (offset + length)), 0); + } +#endif + + return MemoryRange(buffer->mmap(addr, length, prot, flags, offset), length); +} + +void MappableDeflate::finalize() { + /* Free zlib internal buffers */ + inflateEnd(&zStream); + /* Free decompression buffer */ + buffer = nullptr; + /* Remove reference to Zip archive */ + zip = nullptr; +} + +size_t MappableDeflate::GetLength() const { return buffer->GetLength(); } diff --git a/mozglue/linker/Mappable.h b/mozglue/linker/Mappable.h new file mode 100644 index 0000000000..8468aaaccb --- /dev/null +++ b/mozglue/linker/Mappable.h @@ -0,0 +1,161 @@ +/* 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/. */ + +#ifndef Mappable_h +#define Mappable_h + +#include "Zip.h" +#include "mozilla/RefPtr.h" +#include "mozilla/UniquePtr.h" +#include "zlib.h" + +/** + * Abstract class to handle mmap()ing from various kind of entities, such as + * plain files or Zip entries. The virtual members are meant to act as the + * equivalent system functions, except mapped memory is always MAP_PRIVATE, + * even though a given implementation may use something different internally. + */ +class Mappable : public mozilla::RefCounted<Mappable> { + public: + MOZ_DECLARE_REFCOUNTED_TYPENAME(Mappable) + virtual ~Mappable() {} + + virtual MemoryRange mmap(const void* addr, size_t length, int prot, int flags, + off_t offset) = 0; + + enum Kind { + MAPPABLE_FILE, + MAPPABLE_EXTRACT_FILE, + MAPPABLE_DEFLATE, + MAPPABLE_SEEKABLE_ZSTREAM + }; + + virtual Kind GetKind() const = 0; + + private: + virtual void munmap(void* addr, size_t length) { ::munmap(addr, length); } + /* Limit use of Mappable::munmap to classes that keep track of the address + * and size of the mapping. This allows to ignore ::munmap return value. */ + friend class Mappable1stPagePtr; + friend class LibHandle; + + public: + /** + * Indicate to a Mappable instance that no further mmap is going to happen. + */ + virtual void finalize() = 0; + + /** + * Returns the maximum length that can be mapped from this Mappable for + * offset = 0. + */ + virtual size_t GetLength() const = 0; +}; + +/** + * Mappable implementation for plain files + */ +class MappableFile : public Mappable { + public: + ~MappableFile() {} + + /** + * Create a MappableFile instance for the given file path. + */ + static Mappable* Create(const char* path); + + /* Inherited from Mappable */ + virtual MemoryRange mmap(const void* addr, size_t length, int prot, int flags, + off_t offset); + virtual void finalize(); + virtual size_t GetLength() const; + + virtual Kind GetKind() const { return MAPPABLE_FILE; }; + + protected: + explicit MappableFile(int fd) : fd(fd) {} + + private: + /* File descriptor */ + AutoCloseFD fd; +}; + +/** + * Mappable implementation for deflated stream in a Zip archive + * Inflates the complete stream into a cache file. + */ +class MappableExtractFile : public MappableFile { + public: + ~MappableExtractFile() = default; + + /** + * Create a MappableExtractFile instance for the given Zip stream. The name + * argument is used to create the cache file in the cache directory. + */ + static Mappable* Create(const char* name, Zip* zip, Zip::Stream* stream); + + /* Override finalize from MappableFile */ + virtual void finalize() {} + + virtual Kind GetKind() const { return MAPPABLE_EXTRACT_FILE; }; + + private: + /** + * AutoUnlinkFile keeps track of a file name and removes (unlinks) the file + * when the instance is destroyed. + */ + struct UnlinkFile { + void operator()(char* value) { + unlink(value); + delete[] value; + } + }; + typedef mozilla::UniquePtr<char[], UnlinkFile> AutoUnlinkFile; + + MappableExtractFile(int fd, const char* path) + : MappableFile(fd), path(path) {} + + /* Extracted file path */ + mozilla::UniquePtr<const char[]> path; +}; + +class _MappableBuffer; + +/** + * Mappable implementation for deflated stream in a Zip archive. + * Inflates the mapped bits in a temporary buffer. + */ +class MappableDeflate : public Mappable { + public: + ~MappableDeflate(); + + /** + * Create a MappableDeflate instance for the given Zip stream. The name + * argument is used for an appropriately named temporary file, and the Zip + * instance is given for the MappableDeflate to keep a reference of it. + */ + static Mappable* Create(const char* name, Zip* zip, Zip::Stream* stream); + + /* Inherited from Mappable */ + virtual MemoryRange mmap(const void* addr, size_t length, int prot, int flags, + off_t offset); + virtual void finalize(); + virtual size_t GetLength() const; + + virtual Kind GetKind() const { return MAPPABLE_DEFLATE; }; + + private: + MappableDeflate(_MappableBuffer* buf, Zip* zip, Zip::Stream* stream); + + /* Zip reference */ + RefPtr<Zip> zip; + + /* Decompression buffer */ + mozilla::UniquePtr<_MappableBuffer> buffer; + + /* Zlib data */ + z_stream zStream; +}; + +#endif /* Mappable_h */ diff --git a/mozglue/linker/Utils.h b/mozglue/linker/Utils.h new file mode 100644 index 0000000000..d3827f1f41 --- /dev/null +++ b/mozglue/linker/Utils.h @@ -0,0 +1,532 @@ +/* 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/. */ + +#ifndef Utils_h +#define Utils_h + +#include <pthread.h> +#include <stdint.h> +#include <stddef.h> +#include <sys/mman.h> +#include <unistd.h> +#include "mozilla/Assertions.h" +#include "mozilla/Atomics.h" +#include "mozilla/Scoped.h" + +/** + * On architectures that are little endian and that support unaligned reads, + * we can use direct type, but on others, we want to have a special class + * to handle conversion and alignment issues. + */ +#if !defined(DEBUG) && (defined(__i386__) || defined(__x86_64__)) +typedef uint16_t le_uint16; +typedef uint32_t le_uint32; +#else + +/** + * Template that allows to find an unsigned int type from a (computed) bit size + */ +template <int s> +struct UInt {}; +template <> +struct UInt<16> { + typedef uint16_t Type; +}; +template <> +struct UInt<32> { + typedef uint32_t Type; +}; + +/** + * Template to access 2 n-bit sized words as a 2*n-bit sized word, doing + * conversion from little endian and avoiding alignment issues. + */ +template <typename T> +class le_to_cpu { + public: + typedef typename UInt<16 * sizeof(T)>::Type Type; + + operator Type() const { return (b << (sizeof(T) * 8)) | a; } + + const le_to_cpu& operator=(const Type& v) { + a = v & ((1 << (sizeof(T) * 8)) - 1); + b = v >> (sizeof(T) * 8); + return *this; + } + + le_to_cpu() {} + explicit le_to_cpu(const Type& v) { operator=(v); } + + const le_to_cpu& operator+=(const Type& v) { + return operator=(operator Type() + v); + } + + const le_to_cpu& operator++(int) { return operator=(operator Type() + 1); } + + private: + T a, b; +}; + +/** + * Type definitions + */ +typedef le_to_cpu<unsigned char> le_uint16; +typedef le_to_cpu<le_uint16> le_uint32; +#endif + +/** + * AutoCloseFD is a RAII wrapper for POSIX file descriptors + */ +struct AutoCloseFDTraits { + typedef int type; + static int empty() { return -1; } + static void release(int fd) { + if (fd != -1) close(fd); + } +}; +typedef mozilla::Scoped<AutoCloseFDTraits> AutoCloseFD; + +/** + * AutoCloseFILE is a RAII wrapper for POSIX streams + */ +struct AutoCloseFILETraits { + typedef FILE* type; + static FILE* empty() { return nullptr; } + static void release(FILE* f) { + if (f) fclose(f); + } +}; +typedef mozilla::Scoped<AutoCloseFILETraits> AutoCloseFILE; + +extern mozilla::Atomic<size_t, mozilla::ReleaseAcquire> gPageSize; + +/** + * Page alignment helpers + */ +static size_t PageSize() { + if (!gPageSize) { + gPageSize = sysconf(_SC_PAGESIZE); + } + + return gPageSize; +} + +static inline uintptr_t AlignedPtr(uintptr_t ptr, size_t alignment) { + return ptr & ~(alignment - 1); +} + +template <typename T> +static inline T* AlignedPtr(T* ptr, size_t alignment) { + return reinterpret_cast<T*>( + AlignedPtr(reinterpret_cast<uintptr_t>(ptr), alignment)); +} + +template <typename T> +static inline T PageAlignedPtr(T ptr) { + return AlignedPtr(ptr, PageSize()); +} + +static inline uintptr_t AlignedEndPtr(uintptr_t ptr, size_t alignment) { + return AlignedPtr(ptr + alignment - 1, alignment); +} + +template <typename T> +static inline T* AlignedEndPtr(T* ptr, size_t alignment) { + return reinterpret_cast<T*>( + AlignedEndPtr(reinterpret_cast<uintptr_t>(ptr), alignment)); +} + +template <typename T> +static inline T PageAlignedEndPtr(T ptr) { + return AlignedEndPtr(ptr, PageSize()); +} + +static inline size_t AlignedSize(size_t size, size_t alignment) { + return (size + alignment - 1) & ~(alignment - 1); +} + +static inline size_t PageAlignedSize(size_t size) { + return AlignedSize(size, PageSize()); +} + +static inline bool IsAlignedPtr(uintptr_t ptr, size_t alignment) { + return ptr % alignment == 0; +} + +template <typename T> +static inline bool IsAlignedPtr(T* ptr, size_t alignment) { + return IsAlignedPtr(reinterpret_cast<uintptr_t>(ptr), alignment); +} + +template <typename T> +static inline bool IsPageAlignedPtr(T ptr) { + return IsAlignedPtr(ptr, PageSize()); +} + +static inline bool IsAlignedSize(size_t size, size_t alignment) { + return size % alignment == 0; +} + +static inline bool IsPageAlignedSize(size_t size) { + return IsAlignedSize(size, PageSize()); +} + +static inline size_t PageNumber(size_t size) { + return (size + PageSize() - 1) / PageSize(); +} + +/** + * MemoryRange stores a pointer, size pair. + */ +class MemoryRange { + public: + MemoryRange(void* buf, size_t length) : buf(buf), length(length) {} + + void Assign(void* b, size_t len) { + buf = b; + length = len; + } + + void Assign(const MemoryRange& other) { + buf = other.buf; + length = other.length; + } + + void* get() const { return buf; } + + operator void*() const { return buf; } + + operator unsigned char*() const { + return reinterpret_cast<unsigned char*>(buf); + } + + bool operator==(void* ptr) const { return buf == ptr; } + + bool operator==(unsigned char* ptr) const { return buf == ptr; } + + void* operator+(off_t offset) const { + return reinterpret_cast<char*>(buf) + offset; + } + + /** + * Returns whether the given address is within the mapped range + */ + bool Contains(void* ptr) const { + return (ptr >= buf) && (ptr < reinterpret_cast<char*>(buf) + length); + } + + /** + * Returns the length of the mapped range + */ + size_t GetLength() const { return length; } + + static MemoryRange mmap(void* addr, size_t length, int prot, int flags, + int fd, off_t offset) { + return MemoryRange(::mmap(addr, length, prot, flags, fd, offset), length); + } + + private: + void* buf; + size_t length; +}; + +/** + * MappedPtr is a RAII wrapper for mmap()ed memory. It can be used as + * a simple void * or unsigned char *. + * + * It is defined as a derivative of a template that allows to use a + * different unmapping strategy. + */ +template <typename T> +class GenericMappedPtr : public MemoryRange { + public: + GenericMappedPtr(void* buf, size_t length) : MemoryRange(buf, length) {} + explicit GenericMappedPtr(const MemoryRange& other) : MemoryRange(other) {} + GenericMappedPtr() : MemoryRange(MAP_FAILED, 0) {} + + void Assign(void* b, size_t len) { + if (get() != MAP_FAILED) static_cast<T*>(this)->munmap(get(), GetLength()); + MemoryRange::Assign(b, len); + } + + void Assign(const MemoryRange& other) { + Assign(other.get(), other.GetLength()); + } + + ~GenericMappedPtr() { + if (get() != MAP_FAILED) static_cast<T*>(this)->munmap(get(), GetLength()); + } + + void release() { MemoryRange::Assign(MAP_FAILED, 0); } +}; + +struct MappedPtr : public GenericMappedPtr<MappedPtr> { + MappedPtr(void* buf, size_t length) + : GenericMappedPtr<MappedPtr>(buf, length) {} + MOZ_IMPLICIT MappedPtr(const MemoryRange& other) + : GenericMappedPtr<MappedPtr>(other) {} + MappedPtr() : GenericMappedPtr<MappedPtr>() {} + + private: + friend class GenericMappedPtr<MappedPtr>; + void munmap(void* buf, size_t length) { ::munmap(buf, length); } +}; + +/** + * UnsizedArray is a way to access raw arrays of data in memory. + * + * struct S { ... }; + * UnsizedArray<S> a(buf); + * UnsizedArray<S> b; b.Init(buf); + * + * This is roughly equivalent to + * const S *a = reinterpret_cast<const S *>(buf); + * const S *b = nullptr; b = reinterpret_cast<const S *>(buf); + * + * An UnsizedArray has no known length, and it's up to the caller to make + * sure the accessed memory is mapped and makes sense. + */ +template <typename T> +class UnsizedArray { + public: + typedef size_t idx_t; + + /** + * Constructors and Initializers + */ + UnsizedArray() : contents(nullptr) {} + explicit UnsizedArray(const void* buf) + : contents(reinterpret_cast<const T*>(buf)) {} + + void Init(const void* buf) { + MOZ_ASSERT(contents == nullptr); + contents = reinterpret_cast<const T*>(buf); + } + + /** + * Returns the nth element of the array + */ + const T& operator[](const idx_t index) const { + MOZ_ASSERT(contents); + return contents[index]; + } + + operator const T*() const { return contents; } + /** + * Returns whether the array points somewhere + */ + explicit operator bool() const { return contents != nullptr; } + + private: + const T* contents; +}; + +/** + * Array, like UnsizedArray, is a way to access raw arrays of data in memory. + * Unlike UnsizedArray, it has a known length, and is enumerable with an + * iterator. + * + * struct S { ... }; + * Array<S> a(buf, len); + * UnsizedArray<S> b; b.Init(buf, len); + * + * In the above examples, len is the number of elements in the array. It is + * also possible to initialize an Array with the buffer size: + * + * Array<S> c; c.InitSize(buf, size); + * + * It is also possible to initialize an Array in two steps, only providing + * one data at a time: + * + * Array<S> d; + * d.Init(buf); + * d.Init(len); // or d.InitSize(size); + * + */ +template <typename T> +class Array : public UnsizedArray<T> { + public: + typedef typename UnsizedArray<T>::idx_t idx_t; + + /** + * Constructors and Initializers + */ + Array() : UnsizedArray<T>(), length(0) {} + Array(const void* buf, const idx_t length) + : UnsizedArray<T>(buf), length(length) {} + + void Init(const void* buf) { UnsizedArray<T>::Init(buf); } + + void Init(const idx_t len) { + MOZ_ASSERT(length == 0); + length = len; + } + + void InitSize(const idx_t size) { Init(size / sizeof(T)); } + + void Init(const void* buf, const idx_t len) { + UnsizedArray<T>::Init(buf); + Init(len); + } + + void InitSize(const void* buf, const idx_t size) { + UnsizedArray<T>::Init(buf); + InitSize(size); + } + + /** + * Returns the nth element of the array + */ + const T& operator[](const idx_t index) const { + MOZ_ASSERT(index < length); + MOZ_ASSERT(operator bool()); + return UnsizedArray<T>::operator[](index); + } + + /** + * Returns the number of elements in the array + */ + idx_t numElements() const { return length; } + + /** + * Returns whether the array points somewhere and has at least one element. + */ + explicit operator bool() const { + return (length > 0) && UnsizedArray<T>::operator bool(); + } + + /** + * Iterator for an Array. Use is similar to that of STL const_iterators: + * + * struct S { ... }; + * Array<S> a(buf, len); + * for (Array<S>::iterator it = a.begin(); it < a.end(); ++it) { + * // Do something with *it. + * } + */ + class iterator { + public: + iterator() : item(nullptr) {} + + const T& operator*() const { return *item; } + + const T* operator->() const { return item; } + + iterator& operator++() { + ++item; + return *this; + } + + bool operator<(const iterator& other) const { return item < other.item; } + + protected: + friend class Array<T>; + explicit iterator(const T& item) : item(&item) {} + + private: + const T* item; + }; + + /** + * Returns an iterator pointing at the beginning of the Array + */ + iterator begin() const { + if (length) return iterator(UnsizedArray<T>::operator[](0)); + return iterator(); + } + + /** + * Returns an iterator pointing past the end of the Array + */ + iterator end() const { + if (length) return iterator(UnsizedArray<T>::operator[](length)); + return iterator(); + } + + /** + * Reverse iterator for an Array. Use is similar to that of STL + * const_reverse_iterators: + * + * struct S { ... }; + * Array<S> a(buf, len); + * for (Array<S>::reverse_iterator it = a.rbegin(); it < a.rend(); ++it) { + * // Do something with *it. + * } + */ + class reverse_iterator { + public: + reverse_iterator() : item(nullptr) {} + + const T& operator*() const { + const T* tmp = item; + return *--tmp; + } + + const T* operator->() const { return &operator*(); } + + reverse_iterator& operator++() { + --item; + return *this; + } + + bool operator<(const reverse_iterator& other) const { + return item > other.item; + } + + protected: + friend class Array<T>; + explicit reverse_iterator(const T& item) : item(&item) {} + + private: + const T* item; + }; + + /** + * Returns a reverse iterator pointing at the end of the Array + */ + reverse_iterator rbegin() const { + if (length) return reverse_iterator(UnsizedArray<T>::operator[](length)); + return reverse_iterator(); + } + + /** + * Returns a reverse iterator pointing past the beginning of the Array + */ + reverse_iterator rend() const { + if (length) return reverse_iterator(UnsizedArray<T>::operator[](0)); + return reverse_iterator(); + } + + private: + idx_t length; +}; + +/** + * Transforms a pointer-to-function to a pointer-to-object pointing at the + * same address. + */ +template <typename T> +void* FunctionPtr(T func) { + union { + void* ptr; + T func; + } f; + f.func = func; + return f.ptr; +} + +class AutoLock { + public: + explicit AutoLock(pthread_mutex_t* mutex) : mutex(mutex) { + if (pthread_mutex_lock(mutex)) MOZ_CRASH("pthread_mutex_lock failed"); + } + ~AutoLock() { + if (pthread_mutex_unlock(mutex)) MOZ_CRASH("pthread_mutex_unlock failed"); + } + + private: + pthread_mutex_t* mutex; +}; + +#endif /* Utils_h */ diff --git a/mozglue/linker/XZStream.cpp b/mozglue/linker/XZStream.cpp new file mode 100644 index 0000000000..db154d12aa --- /dev/null +++ b/mozglue/linker/XZStream.cpp @@ -0,0 +1,221 @@ +/* -*- 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 "XZStream.h" + +#include <algorithm> +#include <cstring> +#include "mozilla/Assertions.h" +#include "mozilla/CheckedInt.h" +#include "Logging.h" + +// LZMA dictionary size, should have a minimum size for the given compression +// rate, see XZ Utils docs for details. +static const uint32_t kDictSize = 1 << 24; + +static const size_t kFooterSize = 12; + +// Parses a variable-length integer (VLI), +// see http://tukaani.org/xz/xz-file-format.txt for details. +static size_t ParseVarLenInt(const uint8_t* aBuf, size_t aBufSize, + uint64_t* aValue) { + if (!aBufSize) { + return 0; + } + aBufSize = std::min(size_t(9), aBufSize); + + *aValue = aBuf[0] & 0x7F; + size_t i = 0; + + while (aBuf[i++] & 0x80) { + if (i >= aBufSize || aBuf[i] == 0x0) { + return 0; + } + *aValue |= static_cast<uint64_t>(aBuf[i] & 0x7F) << (i * 7); + } + return i; +} + +/* static */ +bool XZStream::IsXZ(const void* aBuf, size_t aBufSize) { + static const uint8_t kXzMagic[] = {0xfd, '7', 'z', 'X', 'Z', 0x0}; + MOZ_ASSERT(aBuf); + return aBufSize > sizeof(kXzMagic) && + !memcmp(reinterpret_cast<const void*>(kXzMagic), aBuf, + sizeof(kXzMagic)); +} + +XZStream::XZStream(const void* aInBuf, size_t aInSize) + : mInBuf(static_cast<const uint8_t*>(aInBuf)), + mUncompSize(0), + mDec(nullptr) { + mBuffers.in = mInBuf; + mBuffers.in_pos = 0; + mBuffers.in_size = aInSize; +} + +XZStream::~XZStream() { xz_dec_end(mDec); } + +bool XZStream::Init() { +#ifdef XZ_USE_CRC64 + xz_crc64_init(); +#endif + xz_crc32_init(); + + mDec = xz_dec_init(XZ_DYNALLOC, kDictSize); + + if (!mDec) { + return false; + } + + mUncompSize = ParseUncompressedSize(); + if (!mUncompSize) { + return false; + } + + return true; +} + +size_t XZStream::Decode(void* aOutBuf, size_t aOutSize) { + if (!mDec) { + return 0; + } + + mBuffers.out = static_cast<uint8_t*>(aOutBuf); + mBuffers.out_pos = 0; + mBuffers.out_size = aOutSize; + + while (mBuffers.in_pos < mBuffers.in_size && + mBuffers.out_pos < mBuffers.out_size) { + const xz_ret ret = xz_dec_run(mDec, &mBuffers); + + switch (ret) { + case XZ_STREAM_END: + // Stream ended, the next loop iteration should terminate. + MOZ_ASSERT(mBuffers.in_pos == mBuffers.in_size); + [[fallthrough]]; +#ifdef XZ_DEC_ANY_CHECK + case XZ_UNSUPPORTED_CHECK: + // Ignore unsupported check. + [[fallthrough]]; +#endif + case XZ_OK: + // Chunk decoded, proceed. + break; + + case XZ_MEM_ERROR: + ERROR("XZ decoding: memory allocation failed"); + return 0; + + case XZ_MEMLIMIT_ERROR: + ERROR("XZ decoding: memory usage limit reached"); + return 0; + + case XZ_FORMAT_ERROR: + ERROR("XZ decoding: invalid stream format"); + return 0; + + case XZ_OPTIONS_ERROR: + ERROR("XZ decoding: unsupported header options"); + return 0; + + case XZ_DATA_ERROR: + [[fallthrough]]; + case XZ_BUF_ERROR: + ERROR("XZ decoding: corrupt input stream"); + return 0; + + default: + MOZ_ASSERT_UNREACHABLE("XZ decoding: unknown error condition"); + return 0; + } + } + return mBuffers.out_pos; +} + +size_t XZStream::RemainingInput() const { + return mBuffers.in_size - mBuffers.in_pos; +} + +size_t XZStream::Size() const { return mBuffers.in_size; } + +size_t XZStream::UncompressedSize() const { return mUncompSize; } + +size_t XZStream::ParseIndexSize() const { + static const uint8_t kFooterMagic[] = {'Y', 'Z'}; + + const uint8_t* footer = mInBuf + mBuffers.in_size - kFooterSize; + // The magic bytes are at the end of the footer. + if (memcmp(reinterpret_cast<const void*>(kFooterMagic), + footer + kFooterSize - sizeof(kFooterMagic), + sizeof(kFooterMagic))) { + // Not a valid footer at stream end. + ERROR("XZ parsing: Invalid footer at end of stream"); + return 0; + } + // Backward size is a 32 bit LE integer field positioned after the 32 bit + // CRC32 code. It encodes the index size as a multiple of 4 bytes with a + // minimum size of 4 bytes. + const uint32_t backwardSizeRaw = *(footer + 4); + // Check for overflow. + mozilla::CheckedInt<size_t> backwardSizeBytes(backwardSizeRaw); + backwardSizeBytes = (backwardSizeBytes + 1) * 4; + if (!backwardSizeBytes.isValid()) { + ERROR("XZ parsing: Cannot parse index size"); + return 0; + } + return backwardSizeBytes.value(); +} + +size_t XZStream::ParseUncompressedSize() const { + static const uint8_t kIndexIndicator[] = {0x0}; + + const size_t indexSize = ParseIndexSize(); + if (!indexSize) { + return 0; + } + // The footer follows directly the index, so we can use it as a reference. + const uint8_t* end = mInBuf + mBuffers.in_size; + const uint8_t* index = end - kFooterSize - indexSize; + + // The xz stream index consists of three concatenated elements: + // (1) 1 byte indicator (always OxOO) + // (2) a Variable Length Integer (VLI) field for the number of records + // (3) a list of records + // See https://tukaani.org/xz/xz-file-format-1.0.4.txt + // Each record contains a VLI field for unpadded size followed by a var field + // for uncompressed size. We only support xz streams with a single record. + + if (memcmp(reinterpret_cast<const void*>(kIndexIndicator), index, + sizeof(kIndexIndicator))) { + ERROR("XZ parsing: Invalid stream index"); + return 0; + } + + index += sizeof(kIndexIndicator); + uint64_t numRecords = 0; + index += ParseVarLenInt(index, end - index, &numRecords); + // Only streams with a single record are supported. + if (numRecords != 1) { + ERROR("XZ parsing: Multiple records not supported"); + return 0; + } + uint64_t unpaddedSize = 0; + index += ParseVarLenInt(index, end - index, &unpaddedSize); + if (!unpaddedSize) { + ERROR("XZ parsing: Unpadded size is 0"); + return 0; + } + uint64_t uncompressedSize = 0; + index += ParseVarLenInt(index, end - index, &uncompressedSize); + mozilla::CheckedInt<size_t> checkedSize(uncompressedSize); + if (!checkedSize.isValid()) { + ERROR("XZ parsing: Uncompressed stream size is too large"); + return 0; + } + + return checkedSize.value(); +} diff --git a/mozglue/linker/XZStream.h b/mozglue/linker/XZStream.h new file mode 100644 index 0000000000..bab5520e37 --- /dev/null +++ b/mozglue/linker/XZStream.h @@ -0,0 +1,49 @@ +/* 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/. */ + +#ifndef XZSTREAM_h +#define XZSTREAM_h + +#include <cstdlib> +#include <stdint.h> + +#define XZ_DEC_DYNALLOC +#include "xz.h" + +// Used to decode XZ stream buffers. +class XZStream { + public: + // Returns whether the provided buffer is likely a XZ stream. + static bool IsXZ(const void* aBuf, size_t aBufSize); + + // Creates a XZ stream object for the given input buffer. + XZStream(const void* aInBuf, size_t aInSize); + ~XZStream(); + + // Initializes the decoder and returns whether decoding may commence. + bool Init(); + // Decodes the next chunk of input into the given output buffer. + size_t Decode(void* aOutBuf, size_t aOutSize); + // Returns the number of yet undecoded bytes in the input buffer. + size_t RemainingInput() const; + // Returns the total number of bytes in the input buffer (compressed size). + size_t Size() const; + // Returns the expected final number of bytes in the output buffer. + // Note: will return 0 before successful Init(). + size_t UncompressedSize() const; + + private: + // Parses the stream footer and returns the size of the index in bytes. + size_t ParseIndexSize() const; + // Parses the stream index and returns the expected uncompressed size in + // bytes. + size_t ParseUncompressedSize() const; + + const uint8_t* mInBuf; + size_t mUncompSize; + xz_buf mBuffers; + xz_dec* mDec; +}; + +#endif // XZSTREAM_h diff --git a/mozglue/linker/Zip.cpp b/mozglue/linker/Zip.cpp new file mode 100644 index 0000000000..7ecc6b9a74 --- /dev/null +++ b/mozglue/linker/Zip.cpp @@ -0,0 +1,277 @@ +/* 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 <sys/mman.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <errno.h> +#include <unistd.h> +#include <cstdlib> +#include <algorithm> +#include "Logging.h" +#include "Zip.h" + +already_AddRefed<Zip> Zip::Create(const char* filename) { + /* Open and map the file in memory */ + AutoCloseFD fd(open(filename, O_RDONLY)); + if (fd == -1) { + ERROR("Error opening %s: %s", filename, strerror(errno)); + return nullptr; + } + struct stat st; + if (fstat(fd, &st) == -1) { + ERROR("Error stating %s: %s", filename, strerror(errno)); + return nullptr; + } + size_t size = st.st_size; + if (size <= sizeof(CentralDirectoryEnd)) { + ERROR("Error reading %s: too short", filename); + return nullptr; + } + void* mapped = mmap(nullptr, size, PROT_READ, MAP_SHARED, fd, 0); + if (mapped == MAP_FAILED) { + ERROR("Error mmapping %s: %s", filename, strerror(errno)); + return nullptr; + } + DEBUG_LOG("Mapped %s @%p", filename, mapped); + + return Create(filename, mapped, size); +} + +already_AddRefed<Zip> Zip::Create(const char* filename, void* mapped, + size_t size) { + RefPtr<Zip> zip = new Zip(filename, mapped, size); + + // If neither the first Local File entry nor central directory entries + // have been found, the zip was invalid. + if (!zip->nextFile && !zip->entries) { + ERROR("%s - Invalid zip", filename); + return nullptr; + } + + ZipCollection::Singleton.Register(zip); + return zip.forget(); +} + +Zip::Zip(const char* filename, void* mapped, size_t size) + : name(filename ? strdup(filename) : nullptr), + mapped(mapped), + size(size), + nextFile(LocalFile::validate(mapped)) // first Local File entry + , + nextDir(nullptr), + entries(nullptr) { + pthread_mutex_init(&mutex, nullptr); + // If the first local file entry couldn't be found (which can happen + // with optimized jars), check the first central directory entry. + if (!nextFile) GetFirstEntry(); +} + +Zip::~Zip() { + if (name) { + munmap(mapped, size); + DEBUG_LOG("Unmapped %s @%p", name, mapped); + free(name); + } + pthread_mutex_destroy(&mutex); +} + +bool Zip::GetStream(const char* path, Zip::Stream* out) const { + AutoLock lock(&mutex); + + DEBUG_LOG("%s - GetFile %s", name, path); + /* Fast path: if the Local File header on store matches, we can return the + * corresponding stream right away. + * However, the Local File header may not contain enough information, in + * which case the 3rd bit on the generalFlag is set. Unfortunately, this + * bit is also set in some archives even when we do have the data (most + * notably the android packages as built by the Mozilla build system). + * So instead of testing the generalFlag bit, only use the fast path when + * we haven't read the central directory entries yet, and when the + * compressed size as defined in the header is not filled (which is a + * normal condition for the bit to be set). */ + if (nextFile && nextFile->GetName().Equals(path) && !entries && + (nextFile->compressedSize != 0)) { + DEBUG_LOG("%s - %s was next file: fast path", name, path); + /* Fill Stream info from Local File header content */ + const char* data = reinterpret_cast<const char*>(nextFile->GetData()); + out->compressedBuf = data; + out->compressedSize = nextFile->compressedSize; + out->uncompressedSize = nextFile->uncompressedSize; + out->CRC32 = nextFile->CRC32; + out->type = static_cast<Stream::Type>(uint16_t(nextFile->compression)); + + /* Find the next Local File header. It is usually simply following the + * compressed stream, but in cases where the 3rd bit of the generalFlag + * is set, there is a Data Descriptor header before. */ + data += nextFile->compressedSize; + if ((nextFile->generalFlag & 0x8) && DataDescriptor::validate(data)) { + data += sizeof(DataDescriptor); + } + nextFile = LocalFile::validate(data); + return true; + } + + /* If the directory entry we have in store doesn't match, scan the Central + * Directory for the entry corresponding to the given path */ + if (!nextDir || !nextDir->GetName().Equals(path)) { + const DirectoryEntry* entry = GetFirstEntry(); + DEBUG_LOG("%s - Scan directory entries in search for %s", name, path); + while (entry && !entry->GetName().Equals(path)) { + entry = entry->GetNext(); + } + nextDir = entry; + } + if (!nextDir) { + DEBUG_LOG("%s - Couldn't find %s", name, path); + return false; + } + + /* Find the Local File header corresponding to the Directory entry that + * was found. */ + nextFile = + LocalFile::validate(static_cast<const char*>(mapped) + nextDir->offset); + if (!nextFile) { + ERROR("%s - Couldn't find the Local File header for %s", name, path); + return false; + } + + /* Fill Stream info from Directory entry content */ + const char* data = reinterpret_cast<const char*>(nextFile->GetData()); + out->compressedBuf = data; + out->compressedSize = nextDir->compressedSize; + out->uncompressedSize = nextDir->uncompressedSize; + out->CRC32 = nextDir->CRC32; + out->type = static_cast<Stream::Type>(uint16_t(nextDir->compression)); + + /* Store the next directory entry */ + nextDir = nextDir->GetNext(); + nextFile = nullptr; + return true; +} + +const Zip::DirectoryEntry* Zip::GetFirstEntry() const { + if (entries) return entries; + + const CentralDirectoryEnd* end = nullptr; + const char* _end = + static_cast<const char*>(mapped) + size - sizeof(CentralDirectoryEnd); + + /* Scan for the Central Directory End */ + for (; _end > mapped && !end; _end--) + end = CentralDirectoryEnd::validate(_end); + if (!end) { + ERROR("%s - Couldn't find end of central directory record", name); + return nullptr; + } + + entries = + DirectoryEntry::validate(static_cast<const char*>(mapped) + end->offset); + if (!entries) { + ERROR("%s - Couldn't find central directory record", name); + } + return entries; +} + +bool Zip::VerifyCRCs() const { + AutoLock lock(&mutex); + + for (const DirectoryEntry* entry = GetFirstEntry(); entry; + entry = entry->GetNext()) { + const LocalFile* file = + LocalFile::validate(static_cast<const char*>(mapped) + entry->offset); + uint32_t crc = crc32(0, nullptr, 0); + + DEBUG_LOG("%.*s: crc=%08x", int(entry->filenameSize), + reinterpret_cast<const char*>(entry) + sizeof(*entry), + uint32_t(entry->CRC32)); + + if (entry->compression == Stream::Type::STORE) { + crc = crc32(crc, static_cast<const uint8_t*>(file->GetData()), + entry->compressedSize); + DEBUG_LOG(" STORE size=%d crc=%08x", int(entry->compressedSize), crc); + + } else if (entry->compression == Stream::Type::DEFLATE) { + z_stream zstream; + Bytef buffer[1024]; + zstream.avail_in = entry->compressedSize; + zstream.next_in = + reinterpret_cast<Bytef*>(const_cast<void*>(file->GetData())); + zstream.zalloc = nullptr; + zstream.zfree = nullptr; + zstream.opaque = nullptr; + + if (inflateInit2(&zstream, -MAX_WBITS) != Z_OK) { + return false; + } + + for (;;) { + zstream.avail_out = sizeof(buffer); + zstream.next_out = buffer; + + int ret = inflate(&zstream, Z_SYNC_FLUSH); + crc = crc32(crc, buffer, sizeof(buffer) - zstream.avail_out); + + if (ret == Z_STREAM_END) { + break; + } else if (ret != Z_OK) { + return false; + } + } + + inflateEnd(&zstream); + DEBUG_LOG(" DEFLATE size=%d crc=%08x", int(zstream.total_out), crc); + + } else { + MOZ_ASSERT_UNREACHABLE("Unexpected stream type"); + continue; + } + + if (entry->CRC32 != crc) { + return false; + } + } + + return true; +} + +ZipCollection ZipCollection::Singleton; + +static pthread_mutex_t sZipCollectionMutex = PTHREAD_MUTEX_INITIALIZER; + +already_AddRefed<Zip> ZipCollection::GetZip(const char* path) { + { + AutoLock lock(&sZipCollectionMutex); + /* Search the list of Zips we already have for a match */ + for (const auto& zip : Singleton.zips) { + if (zip->GetName() && (strcmp(zip->GetName(), path) == 0)) { + return RefPtr<Zip>(zip).forget(); + } + } + } + return Zip::Create(path); +} + +void ZipCollection::Register(Zip* zip) { + AutoLock lock(&sZipCollectionMutex); + DEBUG_LOG("ZipCollection::Register(\"%s\")", zip->GetName()); + Singleton.zips.push_back(zip); +} + +void ZipCollection::Forget(const Zip* zip) { + AutoLock lock(&sZipCollectionMutex); + if (zip->refCount() > 1) { + // Someone has acquired a reference before we had acquired the lock, + // ignore this request. + return; + } + DEBUG_LOG("ZipCollection::Forget(\"%s\")", zip->GetName()); + const auto it = std::find(Singleton.zips.begin(), Singleton.zips.end(), zip); + if (*it == zip) { + Singleton.zips.erase(it); + } else { + DEBUG_LOG("ZipCollection::Forget: didn't find \"%s\" in bookkeeping", + zip->GetName()); + } +} diff --git a/mozglue/linker/Zip.h b/mozglue/linker/Zip.h new file mode 100644 index 0000000000..3e596c3c4c --- /dev/null +++ b/mozglue/linker/Zip.h @@ -0,0 +1,388 @@ +/* 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/. */ + +#ifndef Zip_h +#define Zip_h + +#include <cstring> +#include <stdint.h> +#include <vector> +#include <zlib.h> +#include <pthread.h> +#include "Utils.h" +#include "mozilla/Assertions.h" +#include "mozilla/RefCounted.h" +#include "mozilla/RefPtr.h" + +/** + * Forward declaration + */ +class ZipCollection; + +/** + * Class to handle access to Zip archive streams. The Zip archive is mapped + * in memory, and streams are direct references to that mapped memory. + * Zip files are assumed to be correctly formed. No boundary checks are + * performed, which means hand-crafted malicious Zip archives can make the + * code fail in bad ways. However, since the only intended use is to load + * libraries from Zip archives, there is no interest in making this code + * safe, since the libraries could contain malicious code anyways. + */ +class Zip : public mozilla::external::AtomicRefCounted<Zip> { + public: + MOZ_DECLARE_REFCOUNTED_TYPENAME(Zip) + /** + * Create a Zip instance for the given file name. Returns nullptr in case + * of failure. + */ + static already_AddRefed<Zip> Create(const char* filename); + + /** + * Create a Zip instance using the given buffer. + */ + static already_AddRefed<Zip> Create(void* buffer, size_t size) { + return Create(nullptr, buffer, size); + } + + private: + static already_AddRefed<Zip> Create(const char* filename, void* buffer, + size_t size); + + /** + * Private constructor + */ + Zip(const char* filename, void* buffer, size_t size); + + public: + /** + * Destructor + */ + ~Zip(); + + /** + * Class used to access Zip archive item streams + */ + class Stream { + public: + /** + * Stream types + */ + enum Type { STORE = 0, DEFLATE = 8 }; + + /** + * Constructor + */ + Stream() + : compressedBuf(nullptr), + compressedSize(0), + uncompressedSize(0), + CRC32(0), + type(STORE) {} + + /** + * Getters + */ + const void* GetBuffer() { return compressedBuf; } + size_t GetSize() { return compressedSize; } + size_t GetUncompressedSize() { return uncompressedSize; } + size_t GetCRC32() { return CRC32; } + Type GetType() { return type; } + + /** + * Returns a z_stream for use with inflate functions using the given + * buffer as inflate output. The caller is expected to allocate enough + * memory for the Stream uncompressed size. + */ + z_stream GetZStream(void* buf) { + z_stream zStream; + zStream.avail_in = compressedSize; + zStream.next_in = + reinterpret_cast<Bytef*>(const_cast<void*>(compressedBuf)); + zStream.avail_out = uncompressedSize; + zStream.next_out = static_cast<Bytef*>(buf); + zStream.zalloc = nullptr; + zStream.zfree = nullptr; + zStream.opaque = nullptr; + return zStream; + } + + protected: + friend class Zip; + const void* compressedBuf; + size_t compressedSize; + size_t uncompressedSize; + size_t CRC32; + Type type; + }; + + /** + * Returns a stream from the Zip archive. + */ + bool GetStream(const char* path, Stream* out) const; + + /** + * Returns the file name of the archive + */ + const char* GetName() const { return name; } + + /** + * Returns whether all files have correct CRC checksum. + */ + bool VerifyCRCs() const; + + private: + /* File name of the archive */ + char* name; + /* Address where the Zip archive is mapped */ + void* mapped; + /* Size of the archive */ + size_t size; + + /** + * Strings (file names, comments, etc.) in the Zip headers are NOT zero + * terminated. This class is a helper around them. + */ + class StringBuf { + public: + /** + * Constructor + */ + StringBuf(const char* buf, size_t length) : buf(buf), length(length) {} + + /** + * Returns whether the string has the same content as the given zero + * terminated string. + */ + bool Equals(const char* str) const { + return (strncmp(str, buf, length) == 0 && str[length] == '\0'); + } + + private: + const char* buf; + size_t length; + }; + +/* All the following types need to be packed */ +#pragma pack(1) + public: + /** + * A Zip archive is an aggregate of entities which all start with a + * signature giving their type. This template is to be used as a base + * class for these entities. + */ + template <typename T> + class SignedEntity { + public: + /** + * Equivalent to reinterpret_cast<const T *>(buf), with an additional + * check of the signature. + */ + static const T* validate(const void* buf) { + const T* ret = static_cast<const T*>(buf); + if (ret->signature == T::magic) return ret; + return nullptr; + } + + explicit SignedEntity(uint32_t magic) : signature(magic) {} + + private: + le_uint32 signature; + }; + + private: + /** + * Header used to describe a Local File entry. The header is followed by + * the file name and an extra field, then by the data stream. + */ + struct LocalFile : public SignedEntity<LocalFile> { + /* Signature for a Local File header */ + static const uint32_t magic = 0x04034b50; + + /** + * Returns the file name + */ + StringBuf GetName() const { + return StringBuf(reinterpret_cast<const char*>(this) + sizeof(*this), + filenameSize); + } + + /** + * Returns a pointer to the data associated with this header + */ + const void* GetData() const { + return reinterpret_cast<const char*>(this) + sizeof(*this) + + filenameSize + extraFieldSize; + } + + le_uint16 minVersion; + le_uint16 generalFlag; + le_uint16 compression; + le_uint16 lastModifiedTime; + le_uint16 lastModifiedDate; + le_uint32 CRC32; + le_uint32 compressedSize; + le_uint32 uncompressedSize; + le_uint16 filenameSize; + le_uint16 extraFieldSize; + }; + + /** + * In some cases, when a zip archive is created, compressed size and CRC + * are not known when writing the Local File header. In these cases, the + * 3rd bit of the general flag in the Local File header is set, and there + * is an additional header following the compressed data. + */ + struct DataDescriptor : public SignedEntity<DataDescriptor> { + /* Signature for a Data Descriptor header */ + static const uint32_t magic = 0x08074b50; + + le_uint32 CRC32; + le_uint32 compressedSize; + le_uint32 uncompressedSize; + }; + + /** + * Header used to describe a Central Directory Entry. The header is + * followed by the file name, an extra field, and a comment. + */ + struct DirectoryEntry : public SignedEntity<DirectoryEntry> { + /* Signature for a Central Directory Entry header */ + static const uint32_t magic = 0x02014b50; + + /** + * Returns the file name + */ + StringBuf GetName() const { + return StringBuf(reinterpret_cast<const char*>(this) + sizeof(*this), + filenameSize); + } + + /** + * Returns the Central Directory Entry following this one. + */ + const DirectoryEntry* GetNext() const { + return validate(reinterpret_cast<const char*>(this) + sizeof(*this) + + filenameSize + extraFieldSize + fileCommentSize); + } + + le_uint16 creatorVersion; + le_uint16 minVersion; + le_uint16 generalFlag; + le_uint16 compression; + le_uint16 lastModifiedTime; + le_uint16 lastModifiedDate; + le_uint32 CRC32; + le_uint32 compressedSize; + le_uint32 uncompressedSize; + le_uint16 filenameSize; + le_uint16 extraFieldSize; + le_uint16 fileCommentSize; + le_uint16 diskNum; + le_uint16 internalAttributes; + le_uint32 externalAttributes; + le_uint32 offset; + }; + + /** + * Header used to describe the End of Central Directory Record. + */ + struct CentralDirectoryEnd : public SignedEntity<CentralDirectoryEnd> { + /* Signature for the End of Central Directory Record */ + static const uint32_t magic = 0x06054b50; + + le_uint16 diskNum; + le_uint16 startDisk; + le_uint16 recordsOnDisk; + le_uint16 records; + le_uint32 size; + le_uint32 offset; + le_uint16 commentSize; + }; +#pragma pack() + + /** + * Returns the first Directory entry + */ + const DirectoryEntry* GetFirstEntry() const; + + /* Pointer to the Local File Entry following the last one GetStream() used. + * This is used by GetStream to avoid scanning the Directory Entries when the + * requested entry is that one. */ + mutable const LocalFile* nextFile; + + /* Likewise for the next Directory entry */ + mutable const DirectoryEntry* nextDir; + + /* Pointer to the Directory entries */ + mutable const DirectoryEntry* entries; + + mutable pthread_mutex_t mutex; +}; + +/** + * Class for bookkeeping Zip instances + */ +class ZipCollection { + public: + static ZipCollection Singleton; + + /** + * Get a Zip instance for the given path. If there is an existing one + * already, return that one, otherwise create a new one. + */ + static already_AddRefed<Zip> GetZip(const char* path); + + protected: + friend class Zip; + friend class mozilla::detail::RefCounted<Zip, + mozilla::detail::AtomicRefCount>; + + /** + * Register the given Zip instance. This method is meant to be called + * by Zip::Create. + */ + static void Register(Zip* zip); + + /** + * Forget about the given Zip instance. This method is meant to be called + * by the Zip destructor. + */ + static void Forget(const Zip* zip); + + private: + /* Zip instances bookkept in this collection */ + std::vector<RefPtr<Zip>> zips; +}; + +namespace mozilla { +namespace detail { + +template <> +inline void RefCounted<Zip, AtomicRefCount>::Release() const { + MOZ_ASSERT(static_cast<int32_t>(mRefCnt) > 0); + const auto count = --mRefCnt; + if (count == 1) { + // No external references are left, attempt to remove it from the + // collection. If it's successfully removed from the collection, Release() + // will be called with mRefCnt = 1, which will finally delete this zip. + ZipCollection::Forget(static_cast<const Zip*>(this)); + } else if (count == 0) { +#ifdef DEBUG + mRefCnt = detail::DEAD; +#endif + delete static_cast<const Zip*>(this); + } +} + +#ifdef DEBUG +template <> +inline RefCounted<Zip, AtomicRefCount>::~RefCounted() { + MOZ_ASSERT(mRefCnt == detail::DEAD); +} +#endif + +} // namespace detail +} // namespace mozilla + +#endif /* Zip_h */ diff --git a/mozglue/linker/moz.build b/mozglue/linker/moz.build new file mode 100644 index 0000000000..2eb2be9e35 --- /dev/null +++ b/mozglue/linker/moz.build @@ -0,0 +1,33 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# 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/. + +if CONFIG["MOZ_LINKER"]: + SOURCES += [ + "BaseElf.cpp", + "CustomElf.cpp", + "ElfLoader.cpp", + "Mappable.cpp", + "XZStream.cpp", + ] + +# When the linker is disabled, we still need Zip for mozglue/android. +# Logging is a required dependency. +SOURCES += [ + "Logging.cpp", + "Zip.cpp", +] + +Library("linker") + +FINAL_LIBRARY = "mozglue" + +TEST_DIRS += ["tests"] + +DEFINES["XZ_USE_CRC64"] = 1 + +USE_LIBS += [ + "xz-embedded", +] diff --git a/mozglue/linker/tests/TestZip.cpp b/mozglue/linker/tests/TestZip.cpp new file mode 100644 index 0000000000..a2d2b10bdd --- /dev/null +++ b/mozglue/linker/tests/TestZip.cpp @@ -0,0 +1,61 @@ +/* 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 <cstdio> +#include <unistd.h> +#include "Zip.h" +#include "mozilla/RefPtr.h" + +#include "gtest/gtest.h" + +Logging Logging::Singleton; + +/** + * test.zip is a basic test zip file with a central directory. It contains + * four entries, in the following order: + * "foo", "bar", "baz", "qux". + * The entries are going to be read out of order. + */ +extern const unsigned char TEST_ZIP[]; +extern const unsigned int TEST_ZIP_SIZE; +const char* test_entries[] = {"baz", "foo", "bar", "qux"}; + +/** + * no_central_dir.zip is a hand crafted test zip with no central directory + * entries. The Zip reader is expected to be able to traverse these entries + * if requested in order, without reading a central directory + * - First entry is a file "a", STOREd. + * - Second entry is a file "b", STOREd, using a data descriptor. CRC is + * unknown, but compressed and uncompressed sizes are known in the local + * file header. + * - Third entry is a file "c", DEFLATEd, using a data descriptor. CRC, + * compressed and uncompressed sizes are known in the local file header. + * This is the kind of entry that can be found in a zip that went through + * zipalign if it had a data descriptor originally. + * - Fourth entry is a file "d", STOREd. + */ +extern const unsigned char NO_CENTRAL_DIR_ZIP[]; +extern const unsigned int NO_CENTRAL_DIR_ZIP_SIZE; +const char* no_central_dir_entries[] = {"a", "b", "c", "d"}; + +TEST(Zip, TestZip) +{ + Zip::Stream s; + RefPtr<Zip> z = Zip::Create((void*)TEST_ZIP, TEST_ZIP_SIZE); + for (auto& entry : test_entries) { + ASSERT_TRUE(z->GetStream(entry, &s)) + << "Could not get entry \"" << entry << "\""; + } +} + +TEST(Zip, NoCentralDir) +{ + Zip::Stream s; + RefPtr<Zip> z = + Zip::Create((void*)NO_CENTRAL_DIR_ZIP, NO_CENTRAL_DIR_ZIP_SIZE); + for (auto& entry : no_central_dir_entries) { + ASSERT_TRUE(z->GetStream(entry, &s)) + << "Could not get entry \"" << entry << "\""; + } +} diff --git a/mozglue/linker/tests/TestZipData.S b/mozglue/linker/tests/TestZipData.S new file mode 100644 index 0000000000..5fbb825451 --- /dev/null +++ b/mozglue/linker/tests/TestZipData.S @@ -0,0 +1,17 @@ +.macro zip_data name, path + .global \name + .data + .balign 16 + \name: + .incbin "\path" + .L\name\()_END: + .size \name, .L\name\()_END-\name + .global \name\()_SIZE + .data + .balign 4 + \name\()_SIZE: + .int .L\name\()_END-\name +.endm + +zip_data TEST_ZIP, "test.zip" +zip_data NO_CENTRAL_DIR_ZIP, "no_central_dir.zip" diff --git a/mozglue/linker/tests/moz.build b/mozglue/linker/tests/moz.build new file mode 100644 index 0000000000..4ecc93b190 --- /dev/null +++ b/mozglue/linker/tests/moz.build @@ -0,0 +1,20 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# 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/. + +FINAL_LIBRARY = "xul-gtest" + +UNIFIED_SOURCES += [ + "../Zip.cpp", + "TestZip.cpp", +] + +SOURCES += [ + "TestZipData.S", +] + +LOCAL_INCLUDES += [".."] + +ASFLAGS += ["-I", SRCDIR] diff --git a/mozglue/linker/tests/no_central_dir.zip b/mozglue/linker/tests/no_central_dir.zip Binary files differnew file mode 100644 index 0000000000..df882220d1 --- /dev/null +++ b/mozglue/linker/tests/no_central_dir.zip diff --git a/mozglue/linker/tests/test.zip b/mozglue/linker/tests/test.zip Binary files differnew file mode 100644 index 0000000000..657835b0ca --- /dev/null +++ b/mozglue/linker/tests/test.zip diff --git a/mozglue/misc/AutoProfilerLabel.cpp b/mozglue/misc/AutoProfilerLabel.cpp new file mode 100644 index 0000000000..eaa70fa335 --- /dev/null +++ b/mozglue/misc/AutoProfilerLabel.cpp @@ -0,0 +1,109 @@ +/* -*- Mode: C++; tab-width: 2; 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 "mozilla/AutoProfilerLabel.h" + +#include "mozilla/Assertions.h" +#include "mozilla/PlatformMutex.h" + +namespace mozilla { + +// RAII class that encapsulates all shared static data, and enforces locking +// when accessing this data. +class MOZ_RAII AutoProfilerLabelData { + public: + AutoProfilerLabelData() { sAPLMutex.Lock(); } + + ~AutoProfilerLabelData() { sAPLMutex.Unlock(); } + + AutoProfilerLabelData(const AutoProfilerLabelData&) = delete; + void operator=(const AutoProfilerLabelData&) = delete; + + const ProfilerLabelEnter& EnterCRef() const { return sEnter; } + ProfilerLabelEnter& EnterRef() { return sEnter; } + + const ProfilerLabelExit& ExitCRef() const { return sExit; } + ProfilerLabelExit& ExitRef() { return sExit; } + + const uint32_t& GenerationCRef() const { return sGeneration; } + uint32_t& GenerationRef() { return sGeneration; } + + static bool RacyIsProfilerPresent() { return !!sGeneration; } + + private: + // Thin shell around mozglue PlatformMutex, for local internal use. + // Does not preserve behavior in JS record/replay. + class Mutex : private mozilla::detail::MutexImpl { + public: + Mutex() : mozilla::detail::MutexImpl() {} + void Lock() { mozilla::detail::MutexImpl::lock(); } + void Unlock() { mozilla::detail::MutexImpl::unlock(); } + }; + + // Mutex protecting access to the following static members. + static Mutex sAPLMutex MOZ_UNANNOTATED; + + static ProfilerLabelEnter sEnter; + static ProfilerLabelExit sExit; + + // Current "generation" of RegisterProfilerLabelEnterExit calls. + static uint32_t sGeneration; +}; + +/* static */ AutoProfilerLabelData::Mutex AutoProfilerLabelData::sAPLMutex; +/* static */ ProfilerLabelEnter AutoProfilerLabelData::sEnter = nullptr; +/* static */ ProfilerLabelExit AutoProfilerLabelData::sExit = nullptr; +/* static */ uint32_t AutoProfilerLabelData::sGeneration = 0; + +void RegisterProfilerLabelEnterExit(ProfilerLabelEnter aEnter, + ProfilerLabelExit aExit) { + MOZ_ASSERT(!aEnter == !aExit, "Must provide both null or both non-null"); + + AutoProfilerLabelData data; + MOZ_ASSERT(!aEnter != !data.EnterRef(), + "Must go from null to non-null, or from non-null to null"); + data.EnterRef() = aEnter; + data.ExitRef() = aExit; + ++data.GenerationRef(); +} + +bool IsProfilerPresent() { + return AutoProfilerLabelData::RacyIsProfilerPresent(); +} + +ProfilerLabel ProfilerLabelBegin(const char* aLabelName, + const char* aDynamicString, void* aSp) { + const AutoProfilerLabelData data; + void* entryContext = (data.EnterCRef()) + ? data.EnterCRef()(aLabelName, aDynamicString, aSp) + : nullptr; + uint32_t generation = data.GenerationCRef(); + + return std::make_tuple(entryContext, generation); +} + +void ProfilerLabelEnd(const ProfilerLabel& aLabel) { + if (!IsValidProfilerLabel(aLabel)) { + return; + } + + const AutoProfilerLabelData data; + if (data.ExitCRef() && (std::get<1>(aLabel) == data.GenerationCRef())) { + data.ExitCRef()(std::get<0>(aLabel)); + } +} + +AutoProfilerLabel::AutoProfilerLabel(const char* aLabel, + const char* aDynamicString) { + std::tie(mEntryContext, mGeneration) = + ProfilerLabelBegin(aLabel, aDynamicString, this); +} + +AutoProfilerLabel::~AutoProfilerLabel() { + ProfilerLabelEnd(std::make_tuple(mEntryContext, mGeneration)); +} + +} // namespace mozilla diff --git a/mozglue/misc/AutoProfilerLabel.h b/mozglue/misc/AutoProfilerLabel.h new file mode 100644 index 0000000000..82299c7b15 --- /dev/null +++ b/mozglue/misc/AutoProfilerLabel.h @@ -0,0 +1,71 @@ +/* -*- Mode: C++; tab-width: 2; 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/. */ + +#ifndef mozilla_AutoProfilerLabel_h +#define mozilla_AutoProfilerLabel_h + +#include "mozilla/Attributes.h" + +#include "mozilla/Types.h" +#include <tuple> + +// The Gecko Profiler defines AutoProfilerLabel, an RAII class for +// pushing/popping frames to/from the ProfilingStack. +// +// This file defines a class of the same name that does much the same thing, +// but which can be used in (and only in) mozglue. A different class is +// necessary because mozglue cannot directly access sProfilingStack. +// +// Note that this class is slightly slower than the other AutoProfilerLabel, +// and it lacks the macro wrappers. It also is effectively hardwired to use +// JS::ProfilingCategory::OTHER as the category pair, because that's what +// the callbacks provided by the profiler use. (Specifying the categories in +// this file would require #including ProfilingCategory.h in mozglue, which we +// don't want to do.) + +namespace mozilla { + +// Enter should return a pointer that will be given to Exit. +typedef void* (*ProfilerLabelEnter)(const char* aLabel, + const char* aDynamicString, void* aSp); +typedef void (*ProfilerLabelExit)(void* EntryContext); + +// Register callbacks that do the entry/exit work involving sProfilingStack. +MFBT_API void RegisterProfilerLabelEnterExit(ProfilerLabelEnter aEnter, + ProfilerLabelExit aExit); + +// This #ifdef prevents this AutoProfilerLabel from being defined in libxul, +// which would conflict with the one in the profiler. +#ifdef IMPL_MFBT + +class MOZ_RAII AutoProfilerLabel { + public: + AutoProfilerLabel(const char* aLabel, const char* aDynamicString); + ~AutoProfilerLabel(); + + private: + void* mEntryContext; + // Number of RegisterProfilerLabelEnterExit calls, to avoid giving an entry + // context from one generation to the next. + uint32_t mGeneration; +}; + +using ProfilerLabel = std::tuple<void*, uint32_t>; + +bool IsProfilerPresent(); +ProfilerLabel ProfilerLabelBegin(const char* aLabelName, + const char* aDynamicString, void* aSp); +void ProfilerLabelEnd(const ProfilerLabel& aLabel); + +inline bool IsValidProfilerLabel(const ProfilerLabel& aLabel) { + return !!std::get<0>(aLabel); +} + +#endif + +} // namespace mozilla + +#endif // mozilla_AutoProfilerLabel_h diff --git a/mozglue/misc/AwakeTimeStamp.cpp b/mozglue/misc/AwakeTimeStamp.cpp new file mode 100644 index 0000000000..a5c8ed4abd --- /dev/null +++ b/mozglue/misc/AwakeTimeStamp.cpp @@ -0,0 +1,93 @@ +/* -*- 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 "AwakeTimeStamp.h" + +#ifdef XP_WIN +# include <windows.h> +#endif + +#include "mozilla/Assertions.h" +#include "mozilla/DebugOnly.h" + +namespace mozilla { + +static constexpr uint64_t kUSperS = 1000000; +static constexpr uint64_t kUSperMS = 1000; +#ifndef XP_WIN +static constexpr uint64_t kNSperUS = 1000; +#endif + +double AwakeTimeDuration::ToSeconds() const { + return static_cast<double>(mValueUs) / kUSperS; +} +double AwakeTimeDuration::ToMilliseconds() const { + return static_cast<double>(mValueUs) / kUSperMS; +} +double AwakeTimeDuration::ToMicroseconds() const { + return static_cast<double>(mValueUs); +} + +AwakeTimeDuration AwakeTimeStamp::operator-( + AwakeTimeStamp const& aOther) const { + return AwakeTimeDuration(mValueUs - aOther.mValueUs); +} + +AwakeTimeStamp AwakeTimeStamp::operator+( + const AwakeTimeDuration& aDuration) const { + return AwakeTimeStamp(mValueUs + aDuration.mValueUs); +} + +void AwakeTimeStamp::operator+=(const AwakeTimeDuration& aOther) { + mValueUs += aOther.mValueUs; +} + +void AwakeTimeStamp::operator-=(const AwakeTimeDuration& aOther) { + MOZ_ASSERT(mValueUs >= aOther.mValueUs); + mValueUs -= aOther.mValueUs; +} + +// Apple things +#if defined(__APPLE__) && defined(__MACH__) +# include <time.h> +# include <sys/time.h> +# include <sys/types.h> +# include <mach/mach_time.h> + +AwakeTimeStamp AwakeTimeStamp::NowLoRes() { + return AwakeTimeStamp(clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / kNSperUS); +} + +#elif defined(XP_WIN) + +// Number of hundreds of nanoseconds in a microsecond +static constexpr uint64_t kHNSperUS = 10; + +AwakeTimeStamp AwakeTimeStamp::NowLoRes() { + ULONGLONG interrupt_time; + DebugOnly<bool> rv = QueryUnbiasedInterruptTime(&interrupt_time); + MOZ_ASSERT(rv); + + return AwakeTimeStamp(interrupt_time / kHNSperUS); +} + +#else // Linux and other POSIX but not macOS +# include <time.h> + +uint64_t TimespecToMicroseconds(struct timespec aTs) { + return aTs.tv_sec * kUSperS + aTs.tv_nsec / kNSperUS; +} + +AwakeTimeStamp AwakeTimeStamp::NowLoRes() { + struct timespec ts = {0}; + DebugOnly<int> rv = clock_gettime(CLOCK_MONOTONIC, &ts); + MOZ_ASSERT(!rv); + return AwakeTimeStamp(TimespecToMicroseconds(ts)); +} + +#endif + +}; // namespace mozilla diff --git a/mozglue/misc/AwakeTimeStamp.h b/mozglue/misc/AwakeTimeStamp.h new file mode 100644 index 0000000000..b95ef9e77e --- /dev/null +++ b/mozglue/misc/AwakeTimeStamp.h @@ -0,0 +1,118 @@ +/* -*- 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/. */ + +#ifndef mozilla_AwakeTimeStamp_h +#define mozilla_AwakeTimeStamp_h + +#include <stdint.h> +#include <mozilla/Types.h> +#include "mozilla/Assertions.h" + +namespace mozilla { + +class AwakeTimeDuration; + +// Conceptually like mozilla::TimeStamp, but only increments when the device is +// awake, on all platforms, and with a restricted API. +// +// It is always valid, there is no way to acquire an AwakeTimeStamp that is not +// valid, unlike TimeStamp that can be null. +// +// Some arithmetic and ordering operations are supported, when they make sense. +// +// This timestamp shouldn't be considered to be high-resolution, and is suitable +// to measure time from a hundred of milliseconds (because of Windows +// limitations). +class AwakeTimeStamp { + public: + MFBT_API static AwakeTimeStamp NowLoRes(); + MFBT_API void operator+=(const AwakeTimeDuration& aOther); + MFBT_API void operator-=(const AwakeTimeDuration& aOther); + MFBT_API bool operator<(const AwakeTimeStamp& aOther) const { + return mValueUs < aOther.mValueUs; + } + MFBT_API bool operator<=(const AwakeTimeStamp& aOther) const { + return mValueUs <= aOther.mValueUs; + } + MFBT_API bool operator>=(const AwakeTimeStamp& aOther) const { + return mValueUs >= aOther.mValueUs; + } + MFBT_API bool operator>(const AwakeTimeStamp& aOther) const { + return mValueUs > aOther.mValueUs; + } + MFBT_API bool operator==(const AwakeTimeStamp& aOther) const { + return mValueUs == aOther.mValueUs; + } + MFBT_API bool operator!=(const AwakeTimeStamp& aOther) const { + return !(*this == aOther); + } + MFBT_API AwakeTimeDuration operator-(AwakeTimeStamp const& aOther) const; + MFBT_API AwakeTimeStamp operator+(const AwakeTimeDuration& aDuration) const; + + private: + explicit AwakeTimeStamp(uint64_t aValueUs) : mValueUs(aValueUs) {} + + uint64_t mValueUs; +}; + +// A duration, only counting the time the computer was awake. +// +// Can be obtained via subtracting two AwakeTimeStamp, or default-contructed to +// mean a empty duration. +// +// Arithmetic and ordering operations are defined when they make sense. +class AwakeTimeDuration { + public: + MFBT_API AwakeTimeDuration() : mValueUs(0) {} + + MFBT_API double ToSeconds() const; + MFBT_API double ToMilliseconds() const; + MFBT_API double ToMicroseconds() const; + MFBT_API void operator+=(const AwakeTimeDuration& aDuration) { + mValueUs += aDuration.mValueUs; + } + MFBT_API AwakeTimeDuration operator+(const AwakeTimeDuration& aOther) const { + return AwakeTimeDuration(mValueUs + aOther.mValueUs); + } + MFBT_API AwakeTimeDuration operator-(const AwakeTimeDuration& aOther) const { + MOZ_ASSERT(mValueUs >= aOther.mValueUs); + return AwakeTimeDuration(mValueUs - aOther.mValueUs); + } + MFBT_API void operator-=(const AwakeTimeDuration& aOther) { + MOZ_ASSERT(mValueUs >= aOther.mValueUs); + mValueUs -= aOther.mValueUs; + } + MFBT_API bool operator<(const AwakeTimeDuration& aOther) const { + return mValueUs < aOther.mValueUs; + } + MFBT_API bool operator<=(const AwakeTimeDuration& aOther) const { + return mValueUs <= aOther.mValueUs; + } + MFBT_API bool operator>=(const AwakeTimeDuration& aOther) const { + return mValueUs >= aOther.mValueUs; + } + MFBT_API bool operator>(const AwakeTimeDuration& aOther) const { + return mValueUs > aOther.mValueUs; + } + MFBT_API bool operator==(const AwakeTimeDuration& aOther) const { + return mValueUs == aOther.mValueUs; + } + MFBT_API bool operator!=(const AwakeTimeDuration& aOther) const { + return !(*this == aOther); + } + + private: + friend AwakeTimeStamp; + // Not using a default value because we want this private, but allow creating + // duration that are empty. + explicit AwakeTimeDuration(uint64_t aValueUs) : mValueUs(aValueUs) {} + + uint64_t mValueUs; +}; + +}; // namespace mozilla + +#endif // mozilla_AwakeTimeStamp_h diff --git a/mozglue/misc/ConditionVariable_noop.cpp b/mozglue/misc/ConditionVariable_noop.cpp new file mode 100644 index 0000000000..4b28861650 --- /dev/null +++ b/mozglue/misc/ConditionVariable_noop.cpp @@ -0,0 +1,40 @@ +/* -*- 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 "mozilla/Assertions.h" + +#include "mozilla/PlatformConditionVariable.h" +#include "mozilla/PlatformMutex.h" +#include "MutexPlatformData_noop.h" + +using mozilla::TimeDuration; + +struct mozilla::detail::ConditionVariableImpl::PlatformData {}; + +mozilla::detail::ConditionVariableImpl::ConditionVariableImpl() {} + +mozilla::detail::ConditionVariableImpl::~ConditionVariableImpl() {} + +void mozilla::detail::ConditionVariableImpl::notify_one() {} + +void mozilla::detail::ConditionVariableImpl::notify_all() {} + +void mozilla::detail::ConditionVariableImpl::wait(MutexImpl&) { + // On WASI, there are no threads, so we never wait (either the condvar must + // be ready or there is a deadlock). +} + +mozilla::CVStatus mozilla::detail::ConditionVariableImpl::wait_for( + MutexImpl&, const TimeDuration&) { + return CVStatus::NoTimeout; +} + +mozilla::detail::ConditionVariableImpl::PlatformData* +mozilla::detail::ConditionVariableImpl::platformData() { + static_assert(sizeof platformData_ >= sizeof(PlatformData), + "platformData_ is too small"); + return reinterpret_cast<PlatformData*>(platformData_); +} diff --git a/mozglue/misc/ConditionVariable_posix.cpp b/mozglue/misc/ConditionVariable_posix.cpp new file mode 100644 index 0000000000..5c98560551 --- /dev/null +++ b/mozglue/misc/ConditionVariable_posix.cpp @@ -0,0 +1,159 @@ +/* -*- 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 "mozilla/Assertions.h" +#include "mozilla/CheckedInt.h" + +#include <errno.h> +#include <pthread.h> +#include <time.h> + +#include "mozilla/PlatformConditionVariable.h" +#include "mozilla/PlatformMutex.h" +#include "MutexPlatformData_posix.h" + +using mozilla::CheckedInt; +using mozilla::TimeDuration; + +static const long NanoSecPerSec = 1000000000; + +// Android 4.4 or earlier & macOS 10.12 has the clock functions, but not +// pthread_condattr_setclock. +#if defined(HAVE_CLOCK_MONOTONIC) && \ + !(defined(__ANDROID__) && __ANDROID_API__ < 21) && !defined(__APPLE__) +# define CV_USE_CLOCK_API +#endif + +#ifdef CV_USE_CLOCK_API +// The C++ specification defines std::condition_variable::wait_for in terms of +// std::chrono::steady_clock, which is closest to CLOCK_MONOTONIC. +static const clockid_t WhichClock = CLOCK_MONOTONIC; + +// While timevaladd is widely available to work with timevals, the newer +// timespec structure is largely lacking such conveniences. Thankfully, the +// utilities available in MFBT make implementing our own quite easy. +static void moz_timespecadd(struct timespec* lhs, struct timespec* rhs, + struct timespec* result) { + // Add nanoseconds. This may wrap, but not above 2 billion. + MOZ_RELEASE_ASSERT(lhs->tv_nsec < NanoSecPerSec); + MOZ_RELEASE_ASSERT(rhs->tv_nsec < NanoSecPerSec); + result->tv_nsec = lhs->tv_nsec + rhs->tv_nsec; + + // Add seconds, checking for overflow in the platform specific time_t type. + CheckedInt<time_t> sec = CheckedInt<time_t>(lhs->tv_sec) + rhs->tv_sec; + + // If nanoseconds overflowed, carry the result over into seconds. + if (result->tv_nsec >= NanoSecPerSec) { + MOZ_RELEASE_ASSERT(result->tv_nsec < 2 * NanoSecPerSec); + result->tv_nsec -= NanoSecPerSec; + sec += 1; + } + + // Extracting the value asserts that there was no overflow. + MOZ_RELEASE_ASSERT(sec.isValid()); + result->tv_sec = sec.value(); +} +#endif + +struct mozilla::detail::ConditionVariableImpl::PlatformData { + pthread_cond_t ptCond; +}; + +mozilla::detail::ConditionVariableImpl::ConditionVariableImpl() { + pthread_cond_t* ptCond = &platformData()->ptCond; + +#ifdef CV_USE_CLOCK_API + pthread_condattr_t attr; + int r0 = pthread_condattr_init(&attr); + MOZ_RELEASE_ASSERT(!r0); + + int r1 = pthread_condattr_setclock(&attr, WhichClock); + MOZ_RELEASE_ASSERT(!r1); + + int r2 = pthread_cond_init(ptCond, &attr); + MOZ_RELEASE_ASSERT(!r2); + + int r3 = pthread_condattr_destroy(&attr); + MOZ_RELEASE_ASSERT(!r3); +#else + int r = pthread_cond_init(ptCond, NULL); + MOZ_RELEASE_ASSERT(!r); +#endif +} + +mozilla::detail::ConditionVariableImpl::~ConditionVariableImpl() { + int r = pthread_cond_destroy(&platformData()->ptCond); + MOZ_RELEASE_ASSERT(r == 0); +} + +void mozilla::detail::ConditionVariableImpl::notify_one() { + int r = pthread_cond_signal(&platformData()->ptCond); + MOZ_RELEASE_ASSERT(r == 0); +} + +void mozilla::detail::ConditionVariableImpl::notify_all() { + int r = pthread_cond_broadcast(&platformData()->ptCond); + MOZ_RELEASE_ASSERT(r == 0); +} + +void mozilla::detail::ConditionVariableImpl::wait(MutexImpl& lock) { + pthread_cond_t* ptCond = &platformData()->ptCond; + pthread_mutex_t* ptMutex = &lock.platformData()->ptMutex; + + int r = pthread_cond_wait(ptCond, ptMutex); + MOZ_RELEASE_ASSERT(r == 0); +} + +mozilla::CVStatus mozilla::detail::ConditionVariableImpl::wait_for( + MutexImpl& lock, const TimeDuration& a_rel_time) { + if (a_rel_time == TimeDuration::Forever()) { + wait(lock); + return CVStatus::NoTimeout; + } + + pthread_cond_t* ptCond = &platformData()->ptCond; + pthread_mutex_t* ptMutex = &lock.platformData()->ptMutex; + int r; + + // Clamp to 0, as time_t is unsigned. + TimeDuration rel_time = a_rel_time < TimeDuration::FromSeconds(0) + ? TimeDuration::FromSeconds(0) + : a_rel_time; + + // Convert the duration to a timespec. + struct timespec rel_ts; + rel_ts.tv_sec = static_cast<time_t>(rel_time.ToSeconds()); + rel_ts.tv_nsec = + static_cast<uint64_t>(rel_time.ToMicroseconds() * 1000.0) % NanoSecPerSec; + +#ifdef CV_USE_CLOCK_API + struct timespec now_ts; + r = clock_gettime(WhichClock, &now_ts); + MOZ_RELEASE_ASSERT(!r); + + struct timespec abs_ts; + moz_timespecadd(&now_ts, &rel_ts, &abs_ts); + + r = pthread_cond_timedwait(ptCond, ptMutex, &abs_ts); +#else + // Our non-clock-supporting platforms, OS X and Android, do support waiting + // on a condition variable with a relative timeout. + r = pthread_cond_timedwait_relative_np(ptCond, ptMutex, &rel_ts); +#endif + + if (r == 0) { + return CVStatus::NoTimeout; + } + MOZ_RELEASE_ASSERT(r == ETIMEDOUT); + return CVStatus::Timeout; +} + +mozilla::detail::ConditionVariableImpl::PlatformData* +mozilla::detail::ConditionVariableImpl::platformData() { + static_assert(sizeof platformData_ >= sizeof(PlatformData), + "platformData_ is too small"); + return reinterpret_cast<PlatformData*>(platformData_); +} diff --git a/mozglue/misc/ConditionVariable_windows.cpp b/mozglue/misc/ConditionVariable_windows.cpp new file mode 100644 index 0000000000..0c0151f1d3 --- /dev/null +++ b/mozglue/misc/ConditionVariable_windows.cpp @@ -0,0 +1,98 @@ +/* -*- 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 "mozilla/Assertions.h" + +#include <float.h> +#include <intrin.h> +#include <stdlib.h> +#include <windows.h> + +#include "mozilla/PlatformConditionVariable.h" +#include "mozilla/PlatformMutex.h" +#include "MutexPlatformData_windows.h" + +// Some versions of the Windows SDK have a bug where some interlocked functions +// are not redefined as compiler intrinsics. Fix that for the interlocked +// functions that are used in this file. +#if defined(_MSC_VER) && !defined(InterlockedExchangeAdd) +# define InterlockedExchangeAdd(addend, value) \ + _InterlockedExchangeAdd((volatile long*)(addend), (long)(value)) +#endif + +#if defined(_MSC_VER) && !defined(InterlockedIncrement) +# define InterlockedIncrement(addend) \ + _InterlockedIncrement((volatile long*)(addend)) +#endif + +// Wrapper for native condition variable APIs. +struct mozilla::detail::ConditionVariableImpl::PlatformData { + CONDITION_VARIABLE cv_; +}; + +mozilla::detail::ConditionVariableImpl::ConditionVariableImpl() { + InitializeConditionVariable(&platformData()->cv_); +} + +void mozilla::detail::ConditionVariableImpl::notify_one() { + WakeConditionVariable(&platformData()->cv_); +} + +void mozilla::detail::ConditionVariableImpl::notify_all() { + WakeAllConditionVariable(&platformData()->cv_); +} + +void mozilla::detail::ConditionVariableImpl::wait(MutexImpl& lock) { + SRWLOCK* srwlock = &lock.platformData()->lock; + bool r = + SleepConditionVariableSRW(&platformData()->cv_, srwlock, INFINITE, 0); + MOZ_RELEASE_ASSERT(r); +} + +mozilla::CVStatus mozilla::detail::ConditionVariableImpl::wait_for( + MutexImpl& lock, const mozilla::TimeDuration& rel_time) { + if (rel_time == mozilla::TimeDuration::Forever()) { + wait(lock); + return CVStatus::NoTimeout; + } + + SRWLOCK* srwlock = &lock.platformData()->lock; + + // Note that DWORD is unsigned, so we have to be careful to clamp at 0. If + // rel_time is Forever, then ToMilliseconds is +inf, which evaluates as + // greater than UINT32_MAX, resulting in the correct INFINITE wait. We also + // don't want to round sub-millisecond waits to 0, as that wastes energy (see + // bug 1437167 comment 6), so we instead round submillisecond waits to 1ms. + double msecd = rel_time.ToMilliseconds(); + DWORD msec; + if (msecd < 0.0) { + msec = 0; + } else if (msecd > UINT32_MAX) { + msec = INFINITE; + } else { + msec = static_cast<DWORD>(msecd); + // Round submillisecond waits to 1ms. + if (msec == 0 && !rel_time.IsZero()) { + msec = 1; + } + } + + BOOL r = SleepConditionVariableSRW(&platformData()->cv_, srwlock, msec, 0); + if (r) return CVStatus::NoTimeout; + MOZ_RELEASE_ASSERT(GetLastError() == ERROR_TIMEOUT); + return CVStatus::Timeout; +} + +mozilla::detail::ConditionVariableImpl::~ConditionVariableImpl() { + // Native condition variables don't require cleanup. +} + +inline mozilla::detail::ConditionVariableImpl::PlatformData* +mozilla::detail::ConditionVariableImpl::platformData() { + static_assert(sizeof platformData_ >= sizeof(PlatformData), + "platformData_ is too small"); + return reinterpret_cast<PlatformData*>(platformData_); +} diff --git a/mozglue/misc/Debug.h b/mozglue/misc/Debug.h new file mode 100644 index 0000000000..f8f05c6299 --- /dev/null +++ b/mozglue/misc/Debug.h @@ -0,0 +1,69 @@ +/* -*- 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 https://mozilla.org/MPL/2.0/. */ + +#ifndef mozilla_glue_Debug_h +#define mozilla_glue_Debug_h + +/* This header file intends to supply debugging utilities for use in code + * that cannot use XPCOM debugging facilities like nsDebug.h. + * e.g. mozglue, browser/app + * + * NB: printf_stderr() is in the global namespace, so include this file with + * care; avoid including from header files. + */ + +#include <io.h> +#if defined(XP_WIN) +# include <windows.h> +#endif // defined(XP_WIN) +#include "mozilla/Attributes.h" +#include "mozilla/Sprintf.h" + +#if defined(MOZILLA_INTERNAL_API) +# error Do not include this file from XUL sources. +#endif + +// Though this is a separate implementation than nsDebug's, we want to make the +// declarations compatible to avoid confusing the linker if both headers are +// included. +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +void printf_stderr(const char* fmt, ...) MOZ_FORMAT_PRINTF(1, 2); +inline void printf_stderr(const char* fmt, ...) { +#if defined(XP_WIN) + if (IsDebuggerPresent()) { + char buf[2048]; + va_list args; + va_start(args, fmt); + VsprintfLiteral(buf, fmt, args); + va_end(args); + OutputDebugStringA(buf); + } +#endif // defined(XP_WIN) + + // stderr is unbuffered by default so we open a new FILE (which is buffered) + // so that calls to printf_stderr are not as likely to get mixed together. + int fd = _fileno(stderr); + if (fd == -2) return; + + FILE* fp = _fdopen(_dup(fd), "a"); + if (!fp) return; + + va_list args; + va_start(args, fmt); + vfprintf(fp, fmt, args); + va_end(args); + + fclose(fp); +} + +#ifdef __cplusplus +} +#endif // __cplusplus + +#endif // mozilla_glue_Debug_h diff --git a/mozglue/misc/DynamicallyLinkedFunctionPtr.h b/mozglue/misc/DynamicallyLinkedFunctionPtr.h new file mode 100644 index 0000000000..4313974ec5 --- /dev/null +++ b/mozglue/misc/DynamicallyLinkedFunctionPtr.h @@ -0,0 +1,137 @@ +/* -*- 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/. */ + +#ifndef mozilla_DynamicallyLinkedFunctionPtr_h +#define mozilla_DynamicallyLinkedFunctionPtr_h + +#include <windows.h> + +#include <utility> + +#include "mozilla/Attributes.h" + +namespace mozilla { +namespace detail { + +template <typename T> +struct FunctionPtrCracker; + +template <typename R, typename... Args> +struct FunctionPtrCracker<R (*)(Args...)> { + using ReturnT = R; + using FunctionPtrT = R (*)(Args...); +}; + +#if defined(_M_IX86) +template <typename R, typename... Args> +struct FunctionPtrCracker<R(__stdcall*)(Args...)> { + using ReturnT = R; + using FunctionPtrT = R(__stdcall*)(Args...); +}; + +template <typename R, typename... Args> +struct FunctionPtrCracker<R(__fastcall*)(Args...)> { + using ReturnT = R; + using FunctionPtrT = R(__fastcall*)(Args...); +}; +#endif // defined(_M_IX86) + +template <typename T> +class DynamicallyLinkedFunctionPtrBase { + public: + using ReturnT = typename FunctionPtrCracker<T>::ReturnT; + using FunctionPtrT = typename FunctionPtrCracker<T>::FunctionPtrT; + + DynamicallyLinkedFunctionPtrBase(const wchar_t* aLibName, + const char* aFuncName) + : mModule(::LoadLibraryW(aLibName)), mFunction(nullptr) { + if (!mModule) { + return; + } + + mFunction = + reinterpret_cast<FunctionPtrT>(::GetProcAddress(mModule, aFuncName)); + + if (!mFunction) { + // Since the function doesn't exist, there is no point in holding a + // reference to mModule anymore. + ::FreeLibrary(mModule); + mModule = nullptr; + } + } + + DynamicallyLinkedFunctionPtrBase(const DynamicallyLinkedFunctionPtrBase&) = + delete; + DynamicallyLinkedFunctionPtrBase& operator=( + const DynamicallyLinkedFunctionPtrBase&) = delete; + + DynamicallyLinkedFunctionPtrBase(DynamicallyLinkedFunctionPtrBase&&) = delete; + DynamicallyLinkedFunctionPtrBase& operator=( + DynamicallyLinkedFunctionPtrBase&&) = delete; + + template <typename... Args> + ReturnT operator()(Args&&... args) const { + return mFunction(std::forward<Args>(args)...); + } + + explicit operator bool() const { return !!mFunction; } + + protected: + HMODULE mModule; + FunctionPtrT mFunction; +}; + +} // namespace detail + +/** + * In most cases, this class is the one that you want to use for resolving a + * dynamically-linked function pointer. It should be instantiated as a static + * local variable. + * + * NB: It has a trivial destructor, so the DLL that is loaded is never freed. + * Assuming that this function is called fairly often, this is the most + * sensible option. OTOH, if the function you are calling is a one-off, or the + * static local requirement is too restrictive, use DynamicallyLinkedFunctionPtr + * instead. + */ +template <typename T> +class MOZ_STATIC_LOCAL_CLASS StaticDynamicallyLinkedFunctionPtr final + : public detail::DynamicallyLinkedFunctionPtrBase<T> { + public: + StaticDynamicallyLinkedFunctionPtr(const wchar_t* aLibName, + const char* aFuncName) + : detail::DynamicallyLinkedFunctionPtrBase<T>(aLibName, aFuncName) {} + + /** + * We only offer this operator for the static local case, as it is not + * possible for this object to be destroyed while the returned pointer is + * being held. + */ + operator typename detail::DynamicallyLinkedFunctionPtrBase<T>::FunctionPtrT() + const { + return this->mFunction; + } +}; + +template <typename T> +class MOZ_NON_PARAM MOZ_NON_TEMPORARY_CLASS DynamicallyLinkedFunctionPtr final + : public detail::DynamicallyLinkedFunctionPtrBase<T> { + public: + DynamicallyLinkedFunctionPtr(const wchar_t* aLibName, const char* aFuncName) + : detail::DynamicallyLinkedFunctionPtrBase<T>(aLibName, aFuncName) {} + + ~DynamicallyLinkedFunctionPtr() { + if (!this->mModule) { + return; + } + + ::FreeLibrary(this->mModule); + } +}; + +} // namespace mozilla + +#endif // mozilla_DynamicallyLinkedFunctionPtr_h diff --git a/mozglue/misc/GetKnownFolderPath.cpp b/mozglue/misc/GetKnownFolderPath.cpp new file mode 100644 index 0000000000..676a1c8056 --- /dev/null +++ b/mozglue/misc/GetKnownFolderPath.cpp @@ -0,0 +1,33 @@ +/* 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 "GetKnownFolderPath.h" + +namespace mozilla { + +UniquePtr<wchar_t, LoadedCoTaskMemFreeDeleter> GetKnownFolderPath( + REFKNOWNFOLDERID folderId) { + static decltype(SHGetKnownFolderPath)* shGetKnownFolderPath = nullptr; + if (!shGetKnownFolderPath) { + // We could go out of our way to `FreeLibrary` on this, decrementing its + // ref count and potentially unloading it. However doing so would be either + // effectively a no-op, or counterproductive. Just let it get cleaned up + // when the process is terminated, because we're going to load it anyway + // elsewhere. + HMODULE shell32Dll = ::LoadLibraryW(L"shell32"); + if (!shell32Dll) { + return nullptr; + } + shGetKnownFolderPath = reinterpret_cast<decltype(shGetKnownFolderPath)>( + ::GetProcAddress(shell32Dll, "SHGetKnownFolderPath")); + if (!shGetKnownFolderPath) { + return nullptr; + } + } + PWSTR path = nullptr; + shGetKnownFolderPath(folderId, 0, nullptr, &path); + return UniquePtr<wchar_t, LoadedCoTaskMemFreeDeleter>(path); +} + +} // namespace mozilla diff --git a/mozglue/misc/GetKnownFolderPath.h b/mozglue/misc/GetKnownFolderPath.h new file mode 100644 index 0000000000..3fd80440ab --- /dev/null +++ b/mozglue/misc/GetKnownFolderPath.h @@ -0,0 +1,45 @@ +/* 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/. */ + +#ifndef mozilla_GetKnownFolderPath_h +#define mozilla_GetKnownFolderPath_h + +#include <windows.h> +#include <objbase.h> +#include <shlobj.h> + +#include "mozilla/glue/Debug.h" +#include "mozilla/UniquePtr.h" + +namespace mozilla { + +struct LoadedCoTaskMemFreeDeleter { + void operator()(void* ptr) { + static decltype(CoTaskMemFree)* coTaskMemFree = nullptr; + if (!coTaskMemFree) { + // Just let this get cleaned up when the process is terminated, because + // we're going to load it anyway elsewhere. + HMODULE ole32Dll = ::LoadLibraryW(L"ole32"); + if (!ole32Dll) { + printf_stderr( + "Could not load ole32 - will not free with CoTaskMemFree"); + return; + } + coTaskMemFree = reinterpret_cast<decltype(coTaskMemFree)>( + ::GetProcAddress(ole32Dll, "CoTaskMemFree")); + if (!coTaskMemFree) { + printf_stderr("Could not find CoTaskMemFree"); + return; + } + } + coTaskMemFree(ptr); + } +}; + +UniquePtr<wchar_t, LoadedCoTaskMemFreeDeleter> GetKnownFolderPath( + REFKNOWNFOLDERID folderId); + +} // namespace mozilla + +#endif diff --git a/mozglue/misc/ImportDir.h b/mozglue/misc/ImportDir.h new file mode 100644 index 0000000000..6f7721d966 --- /dev/null +++ b/mozglue/misc/ImportDir.h @@ -0,0 +1,94 @@ +/* -*- 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 https://mozilla.org/MPL/2.0/. */ + +#include "mozilla/NativeNt.h" +#include "mozilla/WinHeaderOnlyUtils.h" + +namespace mozilla { +namespace detail { + +inline LauncherResult<nt::DataDirectoryEntry> GetImageDirectoryViaFileIo( + const nsAutoHandle& aImageFile, const uint32_t aOurImportDirectoryRva) { + OVERLAPPED ov = {}; + ov.Offset = aOurImportDirectoryRva; + + DWORD bytesRead; + nt::DataDirectoryEntry result; + if (!::ReadFile(aImageFile, &result, sizeof(result), &bytesRead, &ov) || + bytesRead != sizeof(result)) { + return LAUNCHER_ERROR_FROM_LAST(); + } + + return result; +} + +} // namespace detail + +/** + * This function ensures that the import directory of a loaded binary image + * matches the version that is found in the original file on disk. We do this + * to prevent tampering by third-party code. + * + * Yes, this function may perform file I/O on the critical path during + * startup. A mitigating factor here is that this function must be called + * immediately after creating a process using the image specified by + * |aFullImagePath|; by this point, the system has already paid the price of + * pulling the image file's contents into the page cache. + * + * @param aFullImagePath Wide-character string containing the absolute path + * to the binary whose import directory we are touching. + * @param aTransferMgr Encapsulating the transfer from the current process to + * the child process whose import table we are touching. + */ +inline LauncherVoidResult RestoreImportDirectory( + const wchar_t* aFullImagePath, nt::CrossExecTransferManager& aTransferMgr) { + uint32_t importDirEntryRva; + PIMAGE_DATA_DIRECTORY importDirEntry = + aTransferMgr.LocalPEHeaders().GetImageDirectoryEntryPtr( + IMAGE_DIRECTORY_ENTRY_IMPORT, &importDirEntryRva); + if (!importDirEntry) { + return LAUNCHER_ERROR_FROM_WIN32(ERROR_BAD_EXE_FORMAT); + } + + nsAutoHandle file(::CreateFileW(aFullImagePath, GENERIC_READ, FILE_SHARE_READ, + nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, + nullptr)); + if (file.get() == INVALID_HANDLE_VALUE) { + return LAUNCHER_ERROR_FROM_LAST(); + } + + // Why do we use file I/O here instead of a memory mapping? The simple reason + // is that we do not want any kernel-mode drivers to start tampering with file + // contents under the belief that the file is being mapped for execution. + // Windows 8 supports creation of file mappings using the SEC_IMAGE_NO_EXECUTE + // flag, which may help to mitigate this, but we might as well just support + // a single implementation that works everywhere. + LauncherResult<nt::DataDirectoryEntry> realImportDirectory = + detail::GetImageDirectoryViaFileIo(file, importDirEntryRva); + if (realImportDirectory.isErr()) { + return realImportDirectory.propagateErr(); + } + + nt::DataDirectoryEntry toWrite = realImportDirectory.unwrap(); + + { // Scope for prot + AutoVirtualProtect prot = aTransferMgr.Protect( + importDirEntry, sizeof(IMAGE_DATA_DIRECTORY), PAGE_READWRITE); + if (!prot) { + return LAUNCHER_ERROR_FROM_MOZ_WINDOWS_ERROR(prot.GetError()); + } + + LauncherVoidResult writeResult = aTransferMgr.Transfer( + importDirEntry, &toWrite, sizeof(IMAGE_DATA_DIRECTORY)); + if (writeResult.isErr()) { + return writeResult.propagateErr(); + } + } + + return Ok(); +} + +} // namespace mozilla diff --git a/mozglue/misc/IntegerPrintfMacros.h b/mozglue/misc/IntegerPrintfMacros.h new file mode 100644 index 0000000000..acf33eb85c --- /dev/null +++ b/mozglue/misc/IntegerPrintfMacros.h @@ -0,0 +1,84 @@ +/* -*- 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/. */ + +/* Implements the C99 <inttypes.h> interface. */ + +#ifndef mozilla_IntegerPrintfMacros_h_ +#define mozilla_IntegerPrintfMacros_h_ + +/* + * These macros should not be used with the NSPR printf-like functions or their + * users. If you need to use NSPR's facilities, see the comment on + * supported formats at the top of nsprpub/pr/include/prprf.h. + */ + +/* + * scanf is a footgun: if the input number exceeds the bounds of the target + * type, behavior is undefined (in the compiler sense: that is, this code + * could overwrite your hard drive with zeroes): + * + * uint8_t u; + * sscanf("256", "%" SCNu8, &u); // BAD + * + * For this reason, *never* use the SCN* macros provided by this header! + */ + +#include <inttypes.h> + +/* + * Fix up Android's broken [u]intptr_t inttype macros. Android's PRI*PTR + * macros are defined as "ld", but sizeof(long) is 8 and sizeof(intptr_t) + * is 4 on 32-bit Android. TestTypeTraits.cpp asserts that these new macro + * definitions match the actual type sizes seen at compile time. + */ +#if defined(ANDROID) && !defined(__LP64__) +# undef PRIdPTR /* intptr_t */ +# define PRIdPTR "d" /* intptr_t */ +# undef PRIiPTR /* intptr_t */ +# define PRIiPTR "i" /* intptr_t */ +# undef PRIoPTR /* uintptr_t */ +# define PRIoPTR "o" /* uintptr_t */ +# undef PRIuPTR /* uintptr_t */ +# define PRIuPTR "u" /* uintptr_t */ +# undef PRIxPTR /* uintptr_t */ +# define PRIxPTR "x" /* uintptr_t */ +# undef PRIXPTR /* uintptr_t */ +# define PRIXPTR "X" /* uintptr_t */ +#endif + +/* + * Fix up Android's broken macros for [u]int_fastN_t. On ARM64, Android's + * PRI*FAST16/32 macros are defined as "d", but the types themselves are defined + * as long and unsigned long. + */ +#if defined(ANDROID) && defined(__LP64__) +# undef PRIdFAST16 /* int_fast16_t */ +# define PRIdFAST16 PRId64 /* int_fast16_t */ +# undef PRIiFAST16 /* int_fast16_t */ +# define PRIiFAST16 PRIi64 /* int_fast16_t */ +# undef PRIoFAST16 /* uint_fast16_t */ +# define PRIoFAST16 PRIo64 /* uint_fast16_t */ +# undef PRIuFAST16 /* uint_fast16_t */ +# define PRIuFAST16 PRIu64 /* uint_fast16_t */ +# undef PRIxFAST16 /* uint_fast16_t */ +# define PRIxFAST16 PRIx64 /* uint_fast16_t */ +# undef PRIXFAST16 /* uint_fast16_t */ +# define PRIXFAST16 PRIX64 /* uint_fast16_t */ +# undef PRIdFAST32 /* int_fast32_t */ +# define PRIdFAST32 PRId64 /* int_fast32_t */ +# undef PRIiFAST32 /* int_fast32_t */ +# define PRIiFAST32 PRIi64 /* int_fast32_t */ +# undef PRIoFAST32 /* uint_fast32_t */ +# define PRIoFAST32 PRIo64 /* uint_fast32_t */ +# undef PRIuFAST32 /* uint_fast32_t */ +# define PRIuFAST32 PRIu64 /* uint_fast32_t */ +# undef PRIxFAST32 /* uint_fast32_t */ +# define PRIxFAST32 PRIx64 /* uint_fast32_t */ +# undef PRIXFAST32 /* uint_fast32_t */ +# define PRIXFAST32 PRIX64 /* uint_fast32_t */ +#endif + +#endif /* mozilla_IntegerPrintfMacros_h_ */ diff --git a/mozglue/misc/MmapFaultHandler.cpp b/mozglue/misc/MmapFaultHandler.cpp new file mode 100644 index 0000000000..a73b9bd24b --- /dev/null +++ b/mozglue/misc/MmapFaultHandler.cpp @@ -0,0 +1,130 @@ +/* -*- 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 "MmapFaultHandler.h" + +#if defined(XP_UNIX) && !defined(XP_DARWIN) && !defined(__wasi__) + +# include "mozilla/Assertions.h" +# include "mozilla/Atomics.h" +# include "mozilla/ThreadLocal.h" +# include <signal.h> +# include <cstring> + +static MOZ_THREAD_LOCAL(MmapAccessScope*) sMmapAccessScope; + +static struct sigaction sPrevSIGBUSHandler; + +static void MmapSIGBUSHandler(int signum, siginfo_t* info, void* context) { + MOZ_RELEASE_ASSERT(signum == SIGBUS); + + MmapAccessScope* mas = sMmapAccessScope.get(); + + if (mas && mas->IsInsideBuffer(info->si_addr)) { + // Temporarily instead of handling the signal, we crash intentionally and + // send some diagnostic information to find out why the signal is received. + mas->CrashWithInfo(info->si_addr); + + // The address is inside the buffer, handle the failure. + siglongjmp(mas->mJmpBuf, signum); + } + + // This signal is not caused by accessing region protected by MmapAccessScope. + // Forward the signal to the next handler. + if (sPrevSIGBUSHandler.sa_flags & SA_SIGINFO) { + sPrevSIGBUSHandler.sa_sigaction(signum, info, context); + } else if (sPrevSIGBUSHandler.sa_handler == SIG_DFL || + sPrevSIGBUSHandler.sa_handler == SIG_IGN) { + // There is no next handler. Uninstalling our handler and returning will + // cause a crash. + sigaction(signum, &sPrevSIGBUSHandler, nullptr); + } else { + sPrevSIGBUSHandler.sa_handler(signum); + } +} + +mozilla::Atomic<bool> gSIGBUSHandlerInstalled(false); +mozilla::Atomic<bool> gSIGBUSHandlerInstalling(false); + +void InstallMmapFaultHandler() { + // This function is called from MmapAccessScope's constructor because there is + // no single point where we could install the handler during startup. This + // means that it's called quite often, so to minimize using of the mutex we + // first check the atomic variable outside the lock. + if (gSIGBUSHandlerInstalled) { + return; + } + + if (gSIGBUSHandlerInstalling.compareExchange(false, true)) { + sMmapAccessScope.infallibleInit(); + + struct sigaction busHandler; + busHandler.sa_flags = SA_SIGINFO | SA_NODEFER | SA_ONSTACK; + busHandler.sa_sigaction = MmapSIGBUSHandler; + sigemptyset(&busHandler.sa_mask); + if (sigaction(SIGBUS, &busHandler, &sPrevSIGBUSHandler)) { + MOZ_CRASH("Unable to install SIGBUS handler"); + } + + MOZ_ASSERT(!gSIGBUSHandlerInstalled); + gSIGBUSHandlerInstalled = true; + } else { + // Just spin lock here. It should not take a substantial amount + // of time, so a mutex would likely be a spin lock anyway, and + // this avoids the need to new up a static mutex from within + // mozglue/misc, which complicates things with + // check_vanilla_allocations.py + while (!gSIGBUSHandlerInstalled) { + } + } +} + +MmapAccessScope::MmapAccessScope(void* aBuf, uint32_t aBufLen, + const char* aFilename) { + // Install signal handler if it wasn't installed yet. + InstallMmapFaultHandler(); + + // We'll handle the signal only if the crashing address is inside this buffer. + mBuf = aBuf; + mBufLen = aBufLen; + mFilename = aFilename; + + SetThreadLocalScope(); +} + +MmapAccessScope::~MmapAccessScope() { + MOZ_RELEASE_ASSERT(sMmapAccessScope.get() == this); + sMmapAccessScope.set(mPreviousScope); +} + +void MmapAccessScope::SetThreadLocalScope() { + // mJmpBuf is set outside of this classs for reasons mentioned in the header + // file, but we need to initialize the member here too to make Coverity happy. + memset(mJmpBuf, 0, sizeof(sigjmp_buf)); + + // If MmapAccessScopes are nested, save the previous one and restore it in + // the destructor. + mPreviousScope = sMmapAccessScope.get(); + + // MmapAccessScope is now set up (except mJmpBuf for reasons mentioned in the + // header file). Store the pointer in a thread-local variable sMmapAccessScope + // so we can use it in the handler if the signal is triggered. + sMmapAccessScope.set(this); +} + +bool MmapAccessScope::IsInsideBuffer(void* aPtr) { + return aPtr >= mBuf && aPtr < (void*)((char*)mBuf + mBufLen); +} + +void MmapAccessScope::CrashWithInfo(void* aPtr) { + // All we have is the buffer and the crashing address. + MOZ_CRASH_UNSAFE_PRINTF( + "SIGBUS received when accessing mmaped file [buffer=%p, " + "buflen=%u, address=%p, filename=%s]", + mBuf, mBufLen, aPtr, mFilename); +} + +#endif diff --git a/mozglue/misc/MmapFaultHandler.h b/mozglue/misc/MmapFaultHandler.h new file mode 100644 index 0000000000..fca356921c --- /dev/null +++ b/mozglue/misc/MmapFaultHandler.h @@ -0,0 +1,113 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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/. */ + +#ifndef MmapFaultHandler_h_ +#define MmapFaultHandler_h_ + +#if defined(XP_WIN) +// Windows + +# ifdef HAVE_SEH_EXCEPTIONS +# define MMAP_FAULT_HANDLER_BEGIN_HANDLE(fd) __try { +# define MMAP_FAULT_HANDLER_BEGIN_BUFFER(buf, bufLen) __try { +# define MMAP_FAULT_HANDLER_CATCH(retval) \ + } \ + __except (GetExceptionCode() == EXCEPTION_IN_PAGE_ERROR \ + ? EXCEPTION_EXECUTE_HANDLER \ + : EXCEPTION_CONTINUE_SEARCH) { \ + NS_WARNING("unexpected EXCEPTION_IN_PAGE_ERROR"); \ + return retval; \ + } +# else +# define MMAP_FAULT_HANDLER_BEGIN_HANDLE(fd) { +# define MMAP_FAULT_HANDLER_BEGIN_BUFFER(buf, bufLen) { +# define MMAP_FAULT_HANDLER_CATCH(retval) } +# endif + +#elif defined(XP_DARWIN) +// MacOS + +# define MMAP_FAULT_HANDLER_BEGIN_HANDLE(fd) { +# define MMAP_FAULT_HANDLER_BEGIN_BUFFER(buf, bufLen) { +# define MMAP_FAULT_HANDLER_CATCH(retval) } + +#elif defined(__wasi__) + +# define MMAP_FAULT_HANDLER_BEGIN_HANDLE(fd) { +# define MMAP_FAULT_HANDLER_BEGIN_BUFFER(buf, bufLen) { +# define MMAP_FAULT_HANDLER_CATCH(retval) } + +#else +// Linux + +# include "mozilla/Attributes.h" +# include "mozilla/Types.h" +# include <stdint.h> +# include <setjmp.h> + +class MOZ_RAII MmapAccessScope { + public: + MFBT_API MmapAccessScope(void* aBuf, uint32_t aBufLen, + const char* aFilename = nullptr); + MFBT_API ~MmapAccessScope(); + + MmapAccessScope(const MmapAccessScope&) = delete; + MmapAccessScope& operator=(const MmapAccessScope&) = delete; + + void SetThreadLocalScope(); + bool IsInsideBuffer(void* aPtr); + void CrashWithInfo(void* aPtr); + + // sigsetjmp cannot be called from a method that returns before calling + // siglongjmp, so the macro must call sigsetjmp directly and mJmpBuf must be + // public. + sigjmp_buf mJmpBuf; + + private: + void* mBuf; + const char* mFilename; + uint32_t mBufLen; + MmapAccessScope* mPreviousScope; +}; + +// Gets around warnings for null-checking in a macro. +template <typename T> +inline bool ValidFD(T fd) { + return !!fd; +} + +# define MMAP_FAULT_HANDLER_BEGIN_HANDLE(fd) \ + { \ + void* mmapScopeBuf = nullptr; \ + nsCString mmapScopeFilename; \ + uint32_t mmapScopeBufLen = 0; \ + if (ValidFD(fd) && fd->mMap) { \ + mmapScopeBuf = (void*)fd->mFileStart; \ + mmapScopeBufLen = fd->mTotalLen; \ + } \ + if (ValidFD(fd) && fd->mFile) { \ + nsCOMPtr<nsIFile> file = fd->mFile.GetBaseFile(); \ + if (file) { \ + file->GetNativeLeafName(mmapScopeFilename); \ + } \ + } \ + MmapAccessScope mmapScope(mmapScopeBuf, mmapScopeBufLen, \ + mmapScopeFilename.get()); \ + if (sigsetjmp(mmapScope.mJmpBuf, 0) == 0) { +# define MMAP_FAULT_HANDLER_BEGIN_BUFFER(buf, bufLen) \ + { \ + MmapAccessScope mmapScope((void*)(buf), (bufLen)); \ + if (sigsetjmp(mmapScope.mJmpBuf, 0) == 0) { +# define MMAP_FAULT_HANDLER_CATCH(retval) \ + } \ + else { \ + NS_WARNING("SIGBUS received when accessing mmapped file"); \ + return retval; \ + } \ + } + +#endif + +#endif diff --git a/mozglue/misc/MutexPlatformData_noop.h b/mozglue/misc/MutexPlatformData_noop.h new file mode 100644 index 0000000000..a2c8d060be --- /dev/null +++ b/mozglue/misc/MutexPlatformData_noop.h @@ -0,0 +1,18 @@ +/* -*- 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/. */ + +#ifndef MutexPlatformData_noop_h +#define MutexPlatformData_noop_h + +#if !defined(__wasi__) +# error This code is for WASI only. +#endif + +#include "mozilla/PlatformMutex.h" + +struct mozilla::detail::MutexImpl::PlatformData {}; + +#endif // MutexPlatformData_noop_h diff --git a/mozglue/misc/MutexPlatformData_posix.h b/mozglue/misc/MutexPlatformData_posix.h new file mode 100644 index 0000000000..d1659d8d7b --- /dev/null +++ b/mozglue/misc/MutexPlatformData_posix.h @@ -0,0 +1,18 @@ +/* -*- 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/. */ + +#ifndef MutexPlatformData_posix_h +#define MutexPlatformData_posix_h + +#include <pthread.h> + +#include "mozilla/PlatformMutex.h" + +struct mozilla::detail::MutexImpl::PlatformData { + pthread_mutex_t ptMutex; +}; + +#endif // MutexPlatformData_posix_h diff --git a/mozglue/misc/MutexPlatformData_windows.h b/mozglue/misc/MutexPlatformData_windows.h new file mode 100644 index 0000000000..489f921115 --- /dev/null +++ b/mozglue/misc/MutexPlatformData_windows.h @@ -0,0 +1,18 @@ +/* -*- 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/. */ + +#ifndef MutexPlatformData_windows_h +#define MutexPlatformData_windows_h + +#include <windows.h> + +#include "mozilla/PlatformMutex.h" + +struct mozilla::detail::MutexImpl::PlatformData { + SRWLOCK lock; +}; + +#endif // MutexPlatformData_windows_h diff --git a/mozglue/misc/Mutex_noop.cpp b/mozglue/misc/Mutex_noop.cpp new file mode 100644 index 0000000000..eaa7959cc0 --- /dev/null +++ b/mozglue/misc/Mutex_noop.cpp @@ -0,0 +1,34 @@ +/* -*- 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 "mozilla/Assertions.h" + +#include <errno.h> +#include <stdio.h> + +#include "mozilla/PlatformMutex.h" +#include "MutexPlatformData_noop.h" + +mozilla::detail::MutexImpl::MutexImpl() {} + +mozilla::detail::MutexImpl::~MutexImpl() {} + +inline void mozilla::detail::MutexImpl::mutexLock() {} + +bool mozilla::detail::MutexImpl::tryLock() { return mutexTryLock(); } + +bool mozilla::detail::MutexImpl::mutexTryLock() { return true; } + +void mozilla::detail::MutexImpl::lock() { mutexLock(); } + +void mozilla::detail::MutexImpl::unlock() {} + +mozilla::detail::MutexImpl::PlatformData* +mozilla::detail::MutexImpl::platformData() { + static_assert(sizeof(platformData_) >= sizeof(PlatformData), + "platformData_ is too small"); + return reinterpret_cast<PlatformData*>(platformData_); +} diff --git a/mozglue/misc/Mutex_posix.cpp b/mozglue/misc/Mutex_posix.cpp new file mode 100644 index 0000000000..7378a544f2 --- /dev/null +++ b/mozglue/misc/Mutex_posix.cpp @@ -0,0 +1,133 @@ +/* -*- 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 "mozilla/Assertions.h" + +#include <errno.h> +#include <pthread.h> +#include <stdio.h> + +#if defined(XP_DARWIN) +# include <pthread_spis.h> +#endif + +#include "mozilla/PlatformMutex.h" +#include "MutexPlatformData_posix.h" + +#define REPORT_PTHREADS_ERROR(result, msg) \ + { \ + errno = result; \ + perror(msg); \ + MOZ_CRASH(msg); \ + } + +#define TRY_CALL_PTHREADS(call, msg) \ + { \ + int result = (call); \ + if (result != 0) { \ + REPORT_PTHREADS_ERROR(result, msg); \ + } \ + } + +mozilla::detail::MutexImpl::MutexImpl() { + pthread_mutexattr_t* attrp = nullptr; + +#if defined(DEBUG) +# define MUTEX_KIND PTHREAD_MUTEX_ERRORCHECK +// Linux with glibc, FreeBSD and macOS 10.14+ support adaptive mutexes that +// spin for a short number of tries before sleeping. NSPR's locks did this, +// too, and it seems like a reasonable thing to do. +#elif (defined(__linux__) && defined(__GLIBC__)) || defined(__FreeBSD__) +# define MUTEX_KIND PTHREAD_MUTEX_ADAPTIVE_NP +#elif defined(XP_DARWIN) +# if defined(PTHREAD_MUTEX_POLICY_FIRSTFIT_NP) +# define POLICY_KIND PTHREAD_MUTEX_POLICY_FIRSTFIT_NP +# else +# define POLICY_KIND (3) // The definition is missing in old SDKs +# endif +#endif + +#if defined(MUTEX_KIND) || defined(POLICY_KIND) +# define ATTR_REQUIRED +#endif + +#if defined(ATTR_REQUIRED) + pthread_mutexattr_t attr; + + TRY_CALL_PTHREADS( + pthread_mutexattr_init(&attr), + "mozilla::detail::MutexImpl::MutexImpl: pthread_mutexattr_init failed"); + +# if defined(MUTEX_KIND) + TRY_CALL_PTHREADS(pthread_mutexattr_settype(&attr, MUTEX_KIND), + "mozilla::detail::MutexImpl::MutexImpl: " + "pthread_mutexattr_settype failed"); +# elif defined(POLICY_KIND) + if (__builtin_available(macOS 10.14, *)) { + TRY_CALL_PTHREADS(pthread_mutexattr_setpolicy_np(&attr, POLICY_KIND), + "mozilla::detail::MutexImpl::MutexImpl: " + "pthread_mutexattr_setpolicy_np failed"); + } +# endif + attrp = &attr; +#endif + + TRY_CALL_PTHREADS( + pthread_mutex_init(&platformData()->ptMutex, attrp), + "mozilla::detail::MutexImpl::MutexImpl: pthread_mutex_init failed"); + +#if defined(ATTR_REQUIRED) + TRY_CALL_PTHREADS(pthread_mutexattr_destroy(&attr), + "mozilla::detail::MutexImpl::MutexImpl: " + "pthread_mutexattr_destroy failed"); +#endif +} + +mozilla::detail::MutexImpl::~MutexImpl() { + TRY_CALL_PTHREADS( + pthread_mutex_destroy(&platformData()->ptMutex), + "mozilla::detail::MutexImpl::~MutexImpl: pthread_mutex_destroy failed"); +} + +inline void mozilla::detail::MutexImpl::mutexLock() { + TRY_CALL_PTHREADS( + pthread_mutex_lock(&platformData()->ptMutex), + "mozilla::detail::MutexImpl::mutexLock: pthread_mutex_lock failed"); +} + +bool mozilla::detail::MutexImpl::tryLock() { return mutexTryLock(); } + +bool mozilla::detail::MutexImpl::mutexTryLock() { + int result = pthread_mutex_trylock(&platformData()->ptMutex); + if (result == 0) { + return true; + } + + if (result == EBUSY) { + return false; + } + + REPORT_PTHREADS_ERROR( + result, + "mozilla::detail::MutexImpl::mutexTryLock: pthread_mutex_trylock failed"); +} + +void mozilla::detail::MutexImpl::lock() { mutexLock(); } + +void mozilla::detail::MutexImpl::unlock() { + TRY_CALL_PTHREADS( + pthread_mutex_unlock(&platformData()->ptMutex), + "mozilla::detail::MutexImpl::unlock: pthread_mutex_unlock failed"); +} + +#undef TRY_CALL_PTHREADS + +mozilla::detail::MutexImpl::PlatformData* +mozilla::detail::MutexImpl::platformData() { + static_assert(sizeof(platformData_) >= sizeof(PlatformData), + "platformData_ is too small"); + return reinterpret_cast<PlatformData*>(platformData_); +} diff --git a/mozglue/misc/Mutex_windows.cpp b/mozglue/misc/Mutex_windows.cpp new file mode 100644 index 0000000000..c4c78fd4f6 --- /dev/null +++ b/mozglue/misc/Mutex_windows.cpp @@ -0,0 +1,40 @@ +/* -*- 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 "mozilla/Assertions.h" +#include "mozilla/DebugOnly.h" +#include "mozilla/PlatformMutex.h" + +#include <windows.h> + +#include "MutexPlatformData_windows.h" + +mozilla::detail::MutexImpl::MutexImpl() { + InitializeSRWLock(&platformData()->lock); +} + +mozilla::detail::MutexImpl::~MutexImpl() {} + +void mozilla::detail::MutexImpl::lock() { + AcquireSRWLockExclusive(&platformData()->lock); +} + +bool mozilla::detail::MutexImpl::tryLock() { return mutexTryLock(); } + +bool mozilla::detail::MutexImpl::mutexTryLock() { + return !!TryAcquireSRWLockExclusive(&platformData()->lock); +} + +void mozilla::detail::MutexImpl::unlock() { + ReleaseSRWLockExclusive(&platformData()->lock); +} + +mozilla::detail::MutexImpl::PlatformData* +mozilla::detail::MutexImpl::platformData() { + static_assert(sizeof(platformData_) >= sizeof(PlatformData), + "platformData_ is too small"); + return reinterpret_cast<PlatformData*>(platformData_); +} diff --git a/mozglue/misc/NativeNt.h b/mozglue/misc/NativeNt.h new file mode 100644 index 0000000000..439ef3d12a --- /dev/null +++ b/mozglue/misc/NativeNt.h @@ -0,0 +1,1729 @@ +/* -*- 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 https://mozilla.org/MPL/2.0/. */ + +#ifndef mozilla_NativeNt_h +#define mozilla_NativeNt_h + +#include <stdint.h> +#include <windows.h> +#include <winnt.h> +#include <winternl.h> + +#include <algorithm> +#include <utility> + +#include "mozilla/ArrayUtils.h" +#include "mozilla/Attributes.h" +#include "mozilla/DebugOnly.h" +#include "mozilla/Maybe.h" +#include "mozilla/Range.h" +#include "mozilla/Span.h" +#include "mozilla/WinHeaderOnlyUtils.h" +#include "mozilla/interceptor/MMPolicies.h" +#include "mozilla/interceptor/TargetFunction.h" + +#if defined(MOZILLA_INTERNAL_API) +# include "nsString.h" +#endif // defined(MOZILLA_INTERNAL_API) + +// The declarations within this #if block are intended to be used for initial +// process initialization ONLY. You probably don't want to be using these in +// normal Gecko code! +#if !defined(MOZILLA_INTERNAL_API) + +extern "C" { + +# if !defined(STATUS_ACCESS_DENIED) +# define STATUS_ACCESS_DENIED ((NTSTATUS)0xC0000022L) +# endif // !defined(STATUS_ACCESS_DENIED) + +# if !defined(STATUS_DLL_NOT_FOUND) +# define STATUS_DLL_NOT_FOUND ((NTSTATUS)0xC0000135L) +# endif // !defined(STATUS_DLL_NOT_FOUND) + +# if !defined(STATUS_UNSUCCESSFUL) +# define STATUS_UNSUCCESSFUL ((NTSTATUS)0xC0000001L) +# endif // !defined(STATUS_UNSUCCESSFUL) + +# if !defined(STATUS_INFO_LENGTH_MISMATCH) +# define STATUS_INFO_LENGTH_MISMATCH ((NTSTATUS)0xC0000004L) +# endif + +enum SECTION_INHERIT { ViewShare = 1, ViewUnmap = 2 }; + +NTSTATUS NTAPI NtMapViewOfSection( + HANDLE aSection, HANDLE aProcess, PVOID* aBaseAddress, ULONG_PTR aZeroBits, + SIZE_T aCommitSize, PLARGE_INTEGER aSectionOffset, PSIZE_T aViewSize, + SECTION_INHERIT aInheritDisposition, ULONG aAllocationType, + ULONG aProtectionFlags); + +NTSTATUS NTAPI NtUnmapViewOfSection(HANDLE aProcess, PVOID aBaseAddress); + +enum MEMORY_INFORMATION_CLASS { + MemoryBasicInformation = 0, + MemorySectionName = 2 +}; + +// NB: When allocating, space for the buffer must also be included +typedef struct _MEMORY_SECTION_NAME { + UNICODE_STRING mSectionFileName; +} MEMORY_SECTION_NAME, *PMEMORY_SECTION_NAME; + +NTSTATUS NTAPI NtQueryVirtualMemory(HANDLE aProcess, PVOID aBaseAddress, + MEMORY_INFORMATION_CLASS aMemInfoClass, + PVOID aMemInfo, SIZE_T aMemInfoLen, + PSIZE_T aReturnLen); + +LONG NTAPI RtlCompareUnicodeString(PCUNICODE_STRING aStr1, + PCUNICODE_STRING aStr2, + BOOLEAN aCaseInsensitive); + +BOOLEAN NTAPI RtlEqualUnicodeString(PCUNICODE_STRING aStr1, + PCUNICODE_STRING aStr2, + BOOLEAN aCaseInsensitive); + +NTSTATUS NTAPI RtlGetVersion(PRTL_OSVERSIONINFOW aOutVersionInformation); + +VOID NTAPI RtlAcquireSRWLockExclusive(PSRWLOCK aLock); +VOID NTAPI RtlAcquireSRWLockShared(PSRWLOCK aLock); + +VOID NTAPI RtlReleaseSRWLockExclusive(PSRWLOCK aLock); +VOID NTAPI RtlReleaseSRWLockShared(PSRWLOCK aLock); + +ULONG NTAPI RtlNtStatusToDosError(NTSTATUS aStatus); +VOID NTAPI RtlSetLastWin32Error(DWORD aError); +DWORD NTAPI RtlGetLastWin32Error(); + +VOID NTAPI RtlRunOnceInitialize(PRTL_RUN_ONCE aRunOnce); + +NTSTATUS NTAPI NtReadVirtualMemory(HANDLE aProcessHandle, PVOID aBaseAddress, + PVOID aBuffer, SIZE_T aNumBytesToRead, + PSIZE_T aNumBytesRead); + +NTSTATUS NTAPI LdrLoadDll(PWCHAR aDllPath, PULONG aFlags, + PUNICODE_STRING aDllName, PHANDLE aOutHandle); + +typedef ULONG(NTAPI* PRTL_RUN_ONCE_INIT_FN)(PRTL_RUN_ONCE, PVOID, PVOID*); +NTSTATUS NTAPI RtlRunOnceExecuteOnce(PRTL_RUN_ONCE aRunOnce, + PRTL_RUN_ONCE_INIT_FN aInitFn, + PVOID aContext, PVOID* aParameter); + +} // extern "C" + +#endif // !defined(MOZILLA_INTERNAL_API) + +extern "C" { +PVOID NTAPI RtlAllocateHeap(PVOID aHeapHandle, ULONG aFlags, SIZE_T aSize); + +PVOID NTAPI RtlReAllocateHeap(PVOID aHeapHandle, ULONG aFlags, LPVOID aMem, + SIZE_T aNewSize); + +BOOLEAN NTAPI RtlFreeHeap(PVOID aHeapHandle, ULONG aFlags, PVOID aHeapBase); + +BOOLEAN NTAPI RtlQueryPerformanceCounter(LARGE_INTEGER* aPerfCount); + +#define RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE 1 +#define RTL_DUPLICATE_UNICODE_STRING_ALLOCATE_NULL_STRING 2 +NTSTATUS NTAPI RtlDuplicateUnicodeString(ULONG aFlags, PCUNICODE_STRING aSrc, + PUNICODE_STRING aDest); + +VOID NTAPI RtlFreeUnicodeString(PUNICODE_STRING aUnicodeString); +} // extern "C" + +namespace mozilla { +namespace nt { + +/** + * This class encapsulates a UNICODE_STRING that owns its own buffer. The + * buffer is always NULL terminated, thus allowing us to cast to a wide C-string + * without requiring any mutation. + * + * We only allow creation of this owned buffer from outside XUL. + */ +class AllocatedUnicodeString final { + public: + AllocatedUnicodeString() : mUnicodeString() {} + +#if defined(MOZILLA_INTERNAL_API) + AllocatedUnicodeString(const AllocatedUnicodeString& aOther) = delete; + + AllocatedUnicodeString& operator=(const AllocatedUnicodeString& aOther) = + delete; +#else + explicit AllocatedUnicodeString(PCUNICODE_STRING aSrc) { + if (!aSrc) { + mUnicodeString = {}; + return; + } + + Duplicate(aSrc); + } + + explicit AllocatedUnicodeString(const char* aSrc) { + if (!aSrc) { + mUnicodeString = {}; + return; + } + + Duplicate(aSrc); + } + + AllocatedUnicodeString(const AllocatedUnicodeString& aOther) { + Duplicate(&aOther.mUnicodeString); + } + + AllocatedUnicodeString& operator=(const AllocatedUnicodeString& aOther) { + Clear(); + Duplicate(&aOther.mUnicodeString); + return *this; + } + + AllocatedUnicodeString& operator=(PCUNICODE_STRING aSrc) { + Clear(); + Duplicate(aSrc); + return *this; + } +#endif // defined(MOZILLA_INTERNAL_API) + + AllocatedUnicodeString(AllocatedUnicodeString&& aOther) + : mUnicodeString(aOther.mUnicodeString) { + aOther.mUnicodeString = {}; + } + + AllocatedUnicodeString& operator=(AllocatedUnicodeString&& aOther) { + Clear(); + mUnicodeString = aOther.mUnicodeString; + aOther.mUnicodeString = {}; + return *this; + } + + ~AllocatedUnicodeString() { Clear(); } + + bool IsEmpty() const { + return !mUnicodeString.Buffer || !mUnicodeString.Length; + } + + operator PCUNICODE_STRING() const { return &mUnicodeString; } + + operator const WCHAR*() const { return mUnicodeString.Buffer; } + + USHORT CharLen() const { return mUnicodeString.Length / sizeof(WCHAR); } + +#if defined(MOZILLA_INTERNAL_API) + nsDependentString AsString() const { + if (!mUnicodeString.Buffer) { + return nsDependentString(); + } + + // We can use nsDependentString here as we guaranteed null termination + // when we allocated the string. + return nsDependentString(mUnicodeString.Buffer, CharLen()); + } +#endif // defined(MOZILLA_INTERNAL_API) + + private: +#if !defined(MOZILLA_INTERNAL_API) + void Duplicate(PCUNICODE_STRING aSrc) { + MOZ_ASSERT(aSrc); + + // We duplicate with null termination so that this string may be used + // as a wide C-string without any further manipulation. + NTSTATUS ntStatus = ::RtlDuplicateUnicodeString( + RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE, aSrc, &mUnicodeString); + MOZ_ASSERT(NT_SUCCESS(ntStatus)); + if (!NT_SUCCESS(ntStatus)) { + // Make sure that mUnicodeString does not contain bogus data + // (since not all callers zero it out before invoking) + mUnicodeString = {}; + } + } + + void Duplicate(const char* aSrc) { + MOZ_ASSERT(aSrc); + + ANSI_STRING ansiStr; + RtlInitAnsiString(&ansiStr, aSrc); + NTSTATUS ntStatus = + ::RtlAnsiStringToUnicodeString(&mUnicodeString, &ansiStr, TRUE); + MOZ_ASSERT(NT_SUCCESS(ntStatus)); + if (!NT_SUCCESS(ntStatus)) { + mUnicodeString = {}; + } + } +#endif // !defined(MOZILLA_INTERNAL_API) + + void Clear() { + if (!mUnicodeString.Buffer) { + return; + } + + ::RtlFreeUnicodeString(&mUnicodeString); + mUnicodeString = {}; + } + + UNICODE_STRING mUnicodeString; +}; + +#if !defined(MOZILLA_INTERNAL_API) + +struct MemorySectionNameBuf : public _MEMORY_SECTION_NAME { + MemorySectionNameBuf() { + mSectionFileName.Length = 0; + mSectionFileName.MaximumLength = sizeof(mBuf); + mSectionFileName.Buffer = mBuf; + } + + MemorySectionNameBuf(const MemorySectionNameBuf& aOther) { *this = aOther; } + + MemorySectionNameBuf(MemorySectionNameBuf&& aOther) { + *this = std::move(aOther); + } + + // We cannot use default copy here because mSectionFileName.Buffer needs to + // be updated to point to |this->mBuf|, not |aOther.mBuf|. + MemorySectionNameBuf& operator=(const MemorySectionNameBuf& aOther) { + mSectionFileName.Length = aOther.mSectionFileName.Length; + mSectionFileName.MaximumLength = sizeof(mBuf); + MOZ_ASSERT(mSectionFileName.Length <= mSectionFileName.MaximumLength); + mSectionFileName.Buffer = mBuf; + memcpy(mBuf, aOther.mBuf, aOther.mSectionFileName.Length); + return *this; + } + + MemorySectionNameBuf& operator=(MemorySectionNameBuf&& aOther) { + mSectionFileName.Length = aOther.mSectionFileName.Length; + aOther.mSectionFileName.Length = 0; + mSectionFileName.MaximumLength = sizeof(mBuf); + MOZ_ASSERT(mSectionFileName.Length <= mSectionFileName.MaximumLength); + aOther.mSectionFileName.MaximumLength = sizeof(aOther.mBuf); + mSectionFileName.Buffer = mBuf; + memmove(mBuf, aOther.mBuf, mSectionFileName.Length); + return *this; + } + + // Native NT paths, so we can't assume MAX_PATH. Use a larger buffer. + WCHAR mBuf[2 * MAX_PATH]; + + bool IsEmpty() const { + return !mSectionFileName.Buffer || !mSectionFileName.Length; + } + + operator PCUNICODE_STRING() const { return &mSectionFileName; } +}; + +class MemorySectionNameOnHeap { + UniquePtr<uint8_t[]> mBuffer; + + MemorySectionNameOnHeap() = default; + explicit MemorySectionNameOnHeap(size_t aBufferLen) + : mBuffer(MakeUnique<uint8_t[]>(aBufferLen)) {} + + public: + static MemorySectionNameOnHeap GetBackingFilePath(HANDLE aProcess, + void* aSectionAddr) { + SIZE_T bufferLen = MAX_PATH * 2; + do { + MemorySectionNameOnHeap sectionName(bufferLen); + + SIZE_T requiredBytes; + NTSTATUS ntStatus = ::NtQueryVirtualMemory( + aProcess, aSectionAddr, MemorySectionName, sectionName.mBuffer.get(), + bufferLen, &requiredBytes); + if (NT_SUCCESS(ntStatus)) { + return sectionName; + } + + if (ntStatus != STATUS_INFO_LENGTH_MISMATCH || + bufferLen >= requiredBytes) { + break; + } + + bufferLen = requiredBytes; + } while (1); + + return MemorySectionNameOnHeap(); + } + + // Allow move & Disallow copy + MemorySectionNameOnHeap(MemorySectionNameOnHeap&&) = default; + MemorySectionNameOnHeap& operator=(MemorySectionNameOnHeap&&) = default; + MemorySectionNameOnHeap(const MemorySectionNameOnHeap&) = delete; + MemorySectionNameOnHeap& operator=(const MemorySectionNameOnHeap&) = delete; + + PCUNICODE_STRING AsUnicodeString() const { + return reinterpret_cast<PCUNICODE_STRING>(mBuffer.get()); + } +}; + +inline bool FindCharInUnicodeString(const UNICODE_STRING& aStr, WCHAR aChar, + uint16_t& aPos, uint16_t aStartIndex = 0) { + const uint16_t aMaxIndex = aStr.Length / sizeof(WCHAR); + + for (uint16_t curIndex = aStartIndex; curIndex < aMaxIndex; ++curIndex) { + if (aStr.Buffer[curIndex] == aChar) { + aPos = curIndex; + return true; + } + } + + return false; +} + +inline bool IsHexDigit(WCHAR aChar) { + return (aChar >= L'0' && aChar <= L'9') || (aChar >= L'A' && aChar <= L'F') || + (aChar >= L'a' && aChar <= L'f'); +} + +inline bool MatchUnicodeString(const UNICODE_STRING& aStr, + bool (*aPredicate)(WCHAR)) { + WCHAR* cur = aStr.Buffer; + WCHAR* end = &aStr.Buffer[aStr.Length / sizeof(WCHAR)]; + while (cur < end) { + if (!aPredicate(*cur)) { + return false; + } + + ++cur; + } + + return true; +} + +inline bool Contains12DigitHexString(const UNICODE_STRING& aLeafName) { + // Quick check: If the string is too short, don't bother + // (We need at least 12 hex digits, one char for '.', and 3 for extension) + const USHORT kMinLen = (12 + 1 + 3) * sizeof(wchar_t); + if (aLeafName.Length < kMinLen) { + return false; + } + + uint16_t start, end; + if (!FindCharInUnicodeString(aLeafName, L'.', start)) { + return false; + } + + ++start; + if (!FindCharInUnicodeString(aLeafName, L'.', end, start)) { + return false; + } + + if (end - start != 12) { + return false; + } + + UNICODE_STRING test; + test.Buffer = &aLeafName.Buffer[start]; + test.Length = (end - start) * sizeof(WCHAR); + test.MaximumLength = test.Length; + + return MatchUnicodeString(test, &IsHexDigit); +} + +inline bool IsFileNameAtLeast16HexDigits(const UNICODE_STRING& aLeafName) { + // Quick check: If the string is too short, don't bother + // (We need 16 hex digits, one char for '.', and 3 for extension) + const USHORT kMinLen = (16 + 1 + 3) * sizeof(wchar_t); + if (aLeafName.Length < kMinLen) { + return false; + } + + uint16_t dotIndex; + if (!FindCharInUnicodeString(aLeafName, L'.', dotIndex)) { + return false; + } + + if (dotIndex < 16) { + return false; + } + + UNICODE_STRING test; + test.Buffer = aLeafName.Buffer; + test.Length = dotIndex * sizeof(WCHAR); + test.MaximumLength = aLeafName.MaximumLength; + + return MatchUnicodeString(test, &IsHexDigit); +} + +inline void GetLeafName(PUNICODE_STRING aDestString, + PCUNICODE_STRING aSrcString) { + WCHAR* buf = aSrcString->Buffer; + WCHAR* end = &aSrcString->Buffer[(aSrcString->Length / sizeof(WCHAR)) - 1]; + WCHAR* cur = end; + while (cur >= buf) { + if (*cur == L'\\') { + break; + } + + --cur; + } + + // At this point, either cur points to the final backslash, or it points to + // buf - 1. Either way, we're interested in cur + 1 as the desired buffer. + aDestString->Buffer = cur + 1; + aDestString->Length = (end - aDestString->Buffer + 1) * sizeof(WCHAR); + aDestString->MaximumLength = aDestString->Length; +} + +#endif // !defined(MOZILLA_INTERNAL_API) + +#if defined(MOZILLA_INTERNAL_API) + +inline const nsDependentSubstring GetLeafName(const nsAString& aString) { + auto it = aString.EndReading(); + size_t pos = aString.Length(); + while (it > aString.BeginReading()) { + if (*(it - 1) == u'\\') { + return Substring(aString, pos); + } + + MOZ_ASSERT(pos > 0); + --pos; + --it; + } + + return Substring(aString, 0); // No backslash in the string +} + +#endif // defined(MOZILLA_INTERNAL_API) + +inline char EnsureLowerCaseASCII(char aChar) { + if (aChar >= 'A' && aChar <= 'Z') { + aChar -= 'A' - 'a'; + } + + return aChar; +} + +inline int StricmpASCII(const char* aLeft, const char* aRight) { + char curLeft, curRight; + + do { + curLeft = EnsureLowerCaseASCII(*(aLeft++)); + curRight = EnsureLowerCaseASCII(*(aRight++)); + } while (curLeft && curLeft == curRight); + + return curLeft - curRight; +} + +inline int StrcmpASCII(const char* aLeft, const char* aRight) { + char curLeft, curRight; + + do { + curLeft = *(aLeft++); + curRight = *(aRight++); + } while (curLeft && curLeft == curRight); + + return curLeft - curRight; +} + +inline size_t StrlenASCII(const char* aStr) { + size_t len = 0; + + while (*(aStr++)) { + ++len; + } + + return len; +} + +struct CodeViewRecord70 { + uint32_t signature; + GUID pdbSignature; + uint32_t pdbAge; + // A UTF-8 string, according to + // https://github.com/Microsoft/microsoft-pdb/blob/082c5290e5aff028ae84e43affa8be717aa7af73/PDB/dbi/locator.cpp#L785 + char pdbFileName[1]; +}; + +class MOZ_RAII PEHeaders final { + /** + * This structure is documented on MSDN as VS_VERSIONINFO, but is not present + * in SDK headers because it cannot be specified as a C struct. The following + * structure contains the fixed-length fields at the beginning of + * VS_VERSIONINFO. + */ + struct VS_VERSIONINFO_HEADER { + WORD wLength; + WORD wValueLength; + WORD wType; + WCHAR szKey[16]; // ArrayLength(L"VS_VERSION_INFO") + // Additional data goes here, aligned on a 4-byte boundary + }; + + public: + // The lowest two bits of an HMODULE are used as flags. Stripping those bits + // from the HMODULE yields the base address of the binary's memory mapping. + // (See LoadLibraryEx docs on MSDN) + template <typename T> + static T HModuleToBaseAddr(HMODULE aModule) { + return reinterpret_cast<T>(reinterpret_cast<uintptr_t>(aModule) & + ~uintptr_t(3)); + } + + explicit PEHeaders(void* aBaseAddress) + : PEHeaders(reinterpret_cast<PIMAGE_DOS_HEADER>(aBaseAddress)) {} + + explicit PEHeaders(HMODULE aModule) + : PEHeaders(HModuleToBaseAddr<PIMAGE_DOS_HEADER>(aModule)) {} + + explicit PEHeaders(PIMAGE_DOS_HEADER aMzHeader) + : mMzHeader(aMzHeader), + mPeHeader(nullptr), + mImageLimit(nullptr), + mIsImportDirectoryTampered(false) { + if (!mMzHeader || mMzHeader->e_magic != IMAGE_DOS_SIGNATURE) { + return; + } + + mPeHeader = RVAToPtrUnchecked<PIMAGE_NT_HEADERS>(mMzHeader->e_lfanew); + if (!mPeHeader || mPeHeader->Signature != IMAGE_NT_SIGNATURE) { + return; + } + + if (mPeHeader->OptionalHeader.Magic != IMAGE_NT_OPTIONAL_HDR_MAGIC) { + return; + } + + DWORD imageSize = mPeHeader->OptionalHeader.SizeOfImage; + // This is a coarse-grained check to ensure that the image size is + // reasonable. It we aren't big enough to contain headers, we have a + // problem! + if (imageSize < sizeof(IMAGE_DOS_HEADER) + sizeof(IMAGE_NT_HEADERS)) { + return; + } + + mImageLimit = RVAToPtrUnchecked<void*>(imageSize - 1UL); + + PIMAGE_DATA_DIRECTORY importDirEntry = + GetImageDirectoryEntryPtr(IMAGE_DIRECTORY_ENTRY_IMPORT); + if (!importDirEntry) { + return; + } + + mIsImportDirectoryTampered = (importDirEntry->VirtualAddress >= imageSize); + } + + explicit operator bool() const { return !!mImageLimit; } + + /** + * This overload computes absolute virtual addresses relative to the base + * address of the binary. + */ + template <typename T, typename R> + T RVAToPtr(R aRva) const { + return RVAToPtr<T>(mMzHeader, aRva); + } + + /** + * This overload computes a result by adding aRva to aBase, but also ensures + * that the resulting pointer falls within the bounds of this binary's memory + * mapping. + */ + template <typename T, typename R> + T RVAToPtr(void* aBase, R aRva) const { + if (!mImageLimit) { + return nullptr; + } + + char* absAddress = reinterpret_cast<char*>(aBase) + aRva; + if (absAddress < reinterpret_cast<char*>(mMzHeader) || + absAddress > reinterpret_cast<char*>(mImageLimit)) { + return nullptr; + } + + return reinterpret_cast<T>(absAddress); + } + + Maybe<Range<const uint8_t>> GetBounds() const { + if (!mImageLimit) { + return Nothing(); + } + + auto base = reinterpret_cast<const uint8_t*>(mMzHeader); + DWORD imageSize = mPeHeader->OptionalHeader.SizeOfImage; + return Some(Range(base, imageSize)); + } + + DWORD GetFileCharacteristics() const { + return mPeHeader ? mPeHeader->FileHeader.Characteristics : 0; + } + + bool IsWithinImage(const void* aAddress) const { + uintptr_t addr = reinterpret_cast<uintptr_t>(aAddress); + uintptr_t imageBase = reinterpret_cast<uintptr_t>(mMzHeader); + uintptr_t imageLimit = reinterpret_cast<uintptr_t>(mImageLimit); + return addr >= imageBase && addr <= imageLimit; + } + + PIMAGE_IMPORT_DESCRIPTOR GetImportDirectory() const { + // If the import directory is already tampered, we skip bounds check + // because it could be located outside the mapped image. + return mIsImportDirectoryTampered + ? GetImageDirectoryEntry<PIMAGE_IMPORT_DESCRIPTOR, + BoundsCheckPolicy::Skip>( + IMAGE_DIRECTORY_ENTRY_IMPORT) + : GetImageDirectoryEntry<PIMAGE_IMPORT_DESCRIPTOR>( + IMAGE_DIRECTORY_ENTRY_IMPORT); + } + + PIMAGE_RESOURCE_DIRECTORY GetResourceTable() const { + return GetImageDirectoryEntry<PIMAGE_RESOURCE_DIRECTORY>( + IMAGE_DIRECTORY_ENTRY_RESOURCE); + } + + PIMAGE_DATA_DIRECTORY GetImageDirectoryEntryPtr( + const uint32_t aDirectoryIndex, uint32_t* aOutRva = nullptr) const { + if (aOutRva) { + *aOutRva = 0; + } + + IMAGE_OPTIONAL_HEADER& optionalHeader = mPeHeader->OptionalHeader; + + const uint32_t maxIndex = std::min(optionalHeader.NumberOfRvaAndSizes, + DWORD(IMAGE_NUMBEROF_DIRECTORY_ENTRIES)); + if (aDirectoryIndex >= maxIndex) { + return nullptr; + } + + PIMAGE_DATA_DIRECTORY dirEntry = + &optionalHeader.DataDirectory[aDirectoryIndex]; + if (aOutRva) { + *aOutRva = reinterpret_cast<char*>(dirEntry) - + reinterpret_cast<char*>(mMzHeader); + MOZ_ASSERT(*aOutRva); + } + + return dirEntry; + } + + bool GetVersionInfo(uint64_t& aOutVersion) const { + // RT_VERSION == 16 + // Version resources require an id of 1 + auto root = FindResourceLeaf<VS_VERSIONINFO_HEADER*>(16, 1); + if (!root) { + return false; + } + + VS_FIXEDFILEINFO* fixedInfo = GetFixedFileInfo(root); + if (!fixedInfo) { + return false; + } + + aOutVersion = ((static_cast<uint64_t>(fixedInfo->dwFileVersionMS) << 32) | + static_cast<uint64_t>(fixedInfo->dwFileVersionLS)); + return true; + } + + bool GetTimeStamp(DWORD& aResult) const { + if (!(*this)) { + return false; + } + + aResult = mPeHeader->FileHeader.TimeDateStamp; + return true; + } + + bool GetImageSize(DWORD& aResult) const { + if (!(*this)) { + return false; + } + + aResult = mPeHeader->OptionalHeader.SizeOfImage; + return true; + } + + bool GetCheckSum(DWORD& aResult) const { + if (!(*this)) { + return false; + } + + aResult = mPeHeader->OptionalHeader.CheckSum; + return true; + } + + PIMAGE_IMPORT_DESCRIPTOR + GetImportDescriptor(const char* aModuleNameASCII) const { + for (PIMAGE_IMPORT_DESCRIPTOR curImpDesc = GetImportDirectory(); + IsValid(curImpDesc); ++curImpDesc) { + auto curName = mIsImportDirectoryTampered + ? RVAToPtrUnchecked<const char*>(curImpDesc->Name) + : RVAToPtr<const char*>(curImpDesc->Name); + if (!curName) { + return nullptr; + } + + if (StricmpASCII(aModuleNameASCII, curName)) { + continue; + } + + // curImpDesc now points to the IAT for the module we're interested in + return curImpDesc; + } + + return nullptr; + } + + template <typename CallbackT> + void EnumImportChunks(const CallbackT& aCallback) const { + for (PIMAGE_IMPORT_DESCRIPTOR curImpDesc = GetImportDirectory(); + IsValid(curImpDesc); ++curImpDesc) { + auto curName = mIsImportDirectoryTampered + ? RVAToPtrUnchecked<const char*>(curImpDesc->Name) + : RVAToPtr<const char*>(curImpDesc->Name); + if (!curName) { + continue; + } + + aCallback(curName); + } + } + + /** + * If |aBoundaries| is given, this method checks whether each IAT entry is + * within the given range, and if any entry is out of the range, we return + * Nothing(). + */ + Maybe<Span<IMAGE_THUNK_DATA>> GetIATThunksForModule( + const char* aModuleNameASCII, + const Range<const uint8_t>* aBoundaries = nullptr) const { + PIMAGE_IMPORT_DESCRIPTOR impDesc = GetImportDescriptor(aModuleNameASCII); + if (!impDesc) { + return Nothing(); + } + + auto firstIatThunk = + this->template RVAToPtr<PIMAGE_THUNK_DATA>(impDesc->FirstThunk); + if (!firstIatThunk) { + return Nothing(); + } + + // Find the length by iterating through the table until we find a null entry + PIMAGE_THUNK_DATA curIatThunk = firstIatThunk; + while (IsValid(curIatThunk)) { + if (aBoundaries) { + auto iatEntry = + reinterpret_cast<const uint8_t*>(curIatThunk->u1.Function); + if (iatEntry < aBoundaries->begin().get() || + iatEntry >= aBoundaries->end().get()) { + return Nothing(); + } + } + + ++curIatThunk; + } + + return Some(Span(firstIatThunk, curIatThunk)); + } + + /** + * Resources are stored in a three-level tree. To locate a particular entry, + * you must supply a resource type, the resource id, and then the language id. + * If aLangId == 0, we just resolve the first entry regardless of language. + */ + template <typename T> + T FindResourceLeaf(WORD aType, WORD aResId, WORD aLangId = 0) const { + PIMAGE_RESOURCE_DIRECTORY topLevel = GetResourceTable(); + if (!topLevel) { + return nullptr; + } + + PIMAGE_RESOURCE_DIRECTORY_ENTRY typeEntry = + FindResourceEntry(topLevel, aType); + if (!typeEntry || !typeEntry->DataIsDirectory) { + return nullptr; + } + + auto idDir = RVAToPtr<PIMAGE_RESOURCE_DIRECTORY>( + topLevel, typeEntry->OffsetToDirectory); + PIMAGE_RESOURCE_DIRECTORY_ENTRY idEntry = FindResourceEntry(idDir, aResId); + if (!idEntry || !idEntry->DataIsDirectory) { + return nullptr; + } + + auto langDir = RVAToPtr<PIMAGE_RESOURCE_DIRECTORY>( + topLevel, idEntry->OffsetToDirectory); + PIMAGE_RESOURCE_DIRECTORY_ENTRY langEntry; + if (aLangId) { + langEntry = FindResourceEntry(langDir, aLangId); + } else { + langEntry = FindFirstResourceEntry(langDir); + } + + if (!langEntry || langEntry->DataIsDirectory) { + return nullptr; + } + + auto dataEntry = + RVAToPtr<PIMAGE_RESOURCE_DATA_ENTRY>(topLevel, langEntry->OffsetToData); + return dataEntry ? RVAToPtr<T>(dataEntry->OffsetToData) : nullptr; + } + + template <size_t N> + Maybe<Span<const uint8_t>> FindSection(const char (&aSecName)[N], + DWORD aCharacteristicsMask) const { + static_assert((N - 1) <= IMAGE_SIZEOF_SHORT_NAME, + "Section names must be at most 8 characters excluding null " + "terminator"); + + if (!(*this)) { + return Nothing(); + } + + Span<IMAGE_SECTION_HEADER> sectionTable = GetSectionTable(); + for (auto&& sectionHeader : sectionTable) { + if (strncmp(reinterpret_cast<const char*>(sectionHeader.Name), aSecName, + IMAGE_SIZEOF_SHORT_NAME)) { + continue; + } + + if (!(sectionHeader.Characteristics & aCharacteristicsMask)) { + // We found the section but it does not have the expected + // characteristics + return Nothing(); + } + + DWORD rva = sectionHeader.VirtualAddress; + if (!rva) { + return Nothing(); + } + + DWORD size = sectionHeader.Misc.VirtualSize; + if (!size) { + return Nothing(); + } + + auto base = RVAToPtr<const uint8_t*>(rva); + return Some(Span(base, size)); + } + + return Nothing(); + } + + // There may be other code sections in the binary besides .text + Maybe<Span<const uint8_t>> GetTextSectionInfo() const { + return FindSection(".text", IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE | + IMAGE_SCN_MEM_READ); + } + + static bool IsValid(PIMAGE_IMPORT_DESCRIPTOR aImpDesc) { + return aImpDesc && aImpDesc->OriginalFirstThunk != 0; + } + + static bool IsValid(PIMAGE_THUNK_DATA aImgThunk) { + return aImgThunk && aImgThunk->u1.Ordinal != 0; + } + + bool IsImportDirectoryTampered() const { return mIsImportDirectoryTampered; } + + FARPROC GetEntryPoint() const { + // Use the unchecked version because the entrypoint may be tampered. + return RVAToPtrUnchecked<FARPROC>( + mPeHeader->OptionalHeader.AddressOfEntryPoint); + } + + const CodeViewRecord70* GetPdbInfo() const { + PIMAGE_DEBUG_DIRECTORY debugDirectory = + GetImageDirectoryEntry<PIMAGE_DEBUG_DIRECTORY>( + IMAGE_DIRECTORY_ENTRY_DEBUG); + if (!debugDirectory) { + return nullptr; + } + + const CodeViewRecord70* debugInfo = + RVAToPtr<CodeViewRecord70*>(debugDirectory->AddressOfRawData); + return (debugInfo && debugInfo->signature == 'SDSR') ? debugInfo : nullptr; + } + + private: + enum class BoundsCheckPolicy { Default, Skip }; + + template <typename T, BoundsCheckPolicy Policy = BoundsCheckPolicy::Default> + T GetImageDirectoryEntry(const uint32_t aDirectoryIndex) const { + PIMAGE_DATA_DIRECTORY dirEntry = GetImageDirectoryEntryPtr(aDirectoryIndex); + if (!dirEntry) { + return nullptr; + } + + return Policy == BoundsCheckPolicy::Skip + ? RVAToPtrUnchecked<T>(dirEntry->VirtualAddress) + : RVAToPtr<T>(dirEntry->VirtualAddress); + } + + // This private variant does not have bounds checks, because we need to be + // able to resolve the bounds themselves. + template <typename T, typename R> + T RVAToPtrUnchecked(R aRva) const { + return reinterpret_cast<T>(reinterpret_cast<char*>(mMzHeader) + aRva); + } + + Span<IMAGE_SECTION_HEADER> GetSectionTable() const { + MOZ_ASSERT(*this); + auto base = RVAToPtr<PIMAGE_SECTION_HEADER>( + &mPeHeader->OptionalHeader, mPeHeader->FileHeader.SizeOfOptionalHeader); + // The Windows loader has an internal limit of 96 sections (per PE spec) + auto numSections = + std::min(mPeHeader->FileHeader.NumberOfSections, WORD(96)); + return Span{base, numSections}; + } + + PIMAGE_RESOURCE_DIRECTORY_ENTRY + FindResourceEntry(PIMAGE_RESOURCE_DIRECTORY aCurLevel, WORD aId) const { + if (!aCurLevel) { + return nullptr; + } + + // Immediately after the IMAGE_RESOURCE_DIRECTORY structure is an array + // of IMAGE_RESOURCE_DIRECTORY_ENTRY structures. Since this function + // searches by ID, we need to skip past any named entries before iterating. + auto dirEnt = + reinterpret_cast<PIMAGE_RESOURCE_DIRECTORY_ENTRY>(aCurLevel + 1) + + aCurLevel->NumberOfNamedEntries; + if (!(IsWithinImage(dirEnt) && + IsWithinImage(&dirEnt[aCurLevel->NumberOfIdEntries - 1].Id))) { + return nullptr; + } + + for (WORD i = 0; i < aCurLevel->NumberOfIdEntries; ++i) { + if (dirEnt[i].Id == aId) { + return &dirEnt[i]; + } + } + + return nullptr; + } + + PIMAGE_RESOURCE_DIRECTORY_ENTRY + FindFirstResourceEntry(PIMAGE_RESOURCE_DIRECTORY aCurLevel) const { + // Immediately after the IMAGE_RESOURCE_DIRECTORY structure is an array + // of IMAGE_RESOURCE_DIRECTORY_ENTRY structures. We just return the first + // entry, regardless of whether it is indexed by name or by id. + auto dirEnt = + reinterpret_cast<PIMAGE_RESOURCE_DIRECTORY_ENTRY>(aCurLevel + 1); + WORD numEntries = + aCurLevel->NumberOfNamedEntries + aCurLevel->NumberOfIdEntries; + if (!numEntries) { + return nullptr; + } + + return dirEnt; + } + + VS_FIXEDFILEINFO* GetFixedFileInfo(VS_VERSIONINFO_HEADER* aVerInfo) const { + WORD length = aVerInfo->wLength; + if (length < sizeof(VS_VERSIONINFO_HEADER)) { + return nullptr; + } + + const wchar_t kVersionInfoKey[] = L"VS_VERSION_INFO"; + if (::RtlCompareMemory(aVerInfo->szKey, kVersionInfoKey, + ArrayLength(kVersionInfoKey)) != + ArrayLength(kVersionInfoKey)) { + return nullptr; + } + + if (aVerInfo->wValueLength != sizeof(VS_FIXEDFILEINFO)) { + // Fixed file info does not exist + return nullptr; + } + + WORD offset = sizeof(VS_VERSIONINFO_HEADER); + + uintptr_t base = reinterpret_cast<uintptr_t>(aVerInfo); + // Align up to 4-byte boundary +#pragma warning(suppress : 4146) + offset += (-(base + offset) & 3); + + if (offset >= length) { + return nullptr; + } + + auto result = reinterpret_cast<VS_FIXEDFILEINFO*>(base + offset); + if (result->dwSignature != 0xFEEF04BD) { + return nullptr; + } + + return result; + } + + private: + PIMAGE_DOS_HEADER mMzHeader; + PIMAGE_NT_HEADERS mPeHeader; + void* mImageLimit; + bool mIsImportDirectoryTampered; +}; + +// This class represents an export section of a local/remote process. +template <typename MMPolicy> +class MOZ_RAII PEExportSection { + const MMPolicy& mMMPolicy; + uintptr_t mImageBase; + DWORD mOrdinalBase; + DWORD mRvaDirStart; + DWORD mRvaDirEnd; + mozilla::interceptor::TargetObjectArray<MMPolicy, DWORD> mExportAddressTable; + mozilla::interceptor::TargetObjectArray<MMPolicy, DWORD> mExportNameTable; + mozilla::interceptor::TargetObjectArray<MMPolicy, WORD> mExportOrdinalTable; + + explicit PEExportSection(const MMPolicy& aMMPolicy) + : mMMPolicy(aMMPolicy), + mImageBase(0), + mOrdinalBase(0), + mRvaDirStart(0), + mRvaDirEnd(0), + mExportAddressTable(mMMPolicy), + mExportNameTable(mMMPolicy), + mExportOrdinalTable(mMMPolicy) {} + + PEExportSection(const MMPolicy& aMMPolicy, uintptr_t aImageBase, + DWORD aRvaDirStart, DWORD aRvaDirEnd, + const IMAGE_EXPORT_DIRECTORY& exportDir) + : mMMPolicy(aMMPolicy), + mImageBase(aImageBase), + mOrdinalBase(exportDir.Base), + mRvaDirStart(aRvaDirStart), + mRvaDirEnd(aRvaDirEnd), + mExportAddressTable(mMMPolicy, + mImageBase + exportDir.AddressOfFunctions, + exportDir.NumberOfFunctions), + mExportNameTable(mMMPolicy, mImageBase + exportDir.AddressOfNames, + exportDir.NumberOfNames), + mExportOrdinalTable(mMMPolicy, + mImageBase + exportDir.AddressOfNameOrdinals, + exportDir.NumberOfNames) {} + + static const PEExportSection Get(uintptr_t aImageBase, + const MMPolicy& aMMPolicy) { + mozilla::interceptor::TargetObject<MMPolicy, IMAGE_DOS_HEADER> mzHeader( + aMMPolicy, aImageBase); + if (!mzHeader || mzHeader->e_magic != IMAGE_DOS_SIGNATURE) { + return PEExportSection(aMMPolicy); + } + + mozilla::interceptor::TargetObject<MMPolicy, IMAGE_NT_HEADERS> peHeader( + aMMPolicy, aImageBase + mzHeader->e_lfanew); + if (!peHeader || peHeader->Signature != IMAGE_NT_SIGNATURE) { + return PEExportSection(aMMPolicy); + } + + if (peHeader->OptionalHeader.Magic != IMAGE_NT_OPTIONAL_HDR_MAGIC) { + return PEExportSection(aMMPolicy); + } + + const IMAGE_OPTIONAL_HEADER& optionalHeader = peHeader->OptionalHeader; + + DWORD imageSize = optionalHeader.SizeOfImage; + // This is a coarse-grained check to ensure that the image size is + // reasonable. It we aren't big enough to contain headers, we have a + // problem! + if (imageSize < sizeof(IMAGE_DOS_HEADER) + sizeof(IMAGE_NT_HEADERS)) { + return PEExportSection(aMMPolicy); + } + + if (optionalHeader.NumberOfRvaAndSizes <= IMAGE_DIRECTORY_ENTRY_EXPORT) { + return PEExportSection(aMMPolicy); + } + + const IMAGE_DATA_DIRECTORY& exportDirectoryEntry = + optionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT]; + if (!exportDirectoryEntry.VirtualAddress || !exportDirectoryEntry.Size) { + return PEExportSection(aMMPolicy); + } + + mozilla::interceptor::TargetObject<MMPolicy, IMAGE_EXPORT_DIRECTORY> + exportDirectory(aMMPolicy, + aImageBase + exportDirectoryEntry.VirtualAddress); + if (!exportDirectory || !exportDirectory->NumberOfFunctions) { + return PEExportSection(aMMPolicy); + } + + return PEExportSection( + aMMPolicy, aImageBase, exportDirectoryEntry.VirtualAddress, + exportDirectoryEntry.VirtualAddress + exportDirectoryEntry.Size, + *exportDirectory.GetLocalBase()); + } + + FARPROC GetProcAddressByOrdinal(WORD aOrdinal) const { + if (aOrdinal < mOrdinalBase) { + return nullptr; + } + + auto rvaToFunction = mExportAddressTable[aOrdinal - mOrdinalBase]; + if (!rvaToFunction) { + return nullptr; + } + return reinterpret_cast<FARPROC>(mImageBase + *rvaToFunction); + } + + public: + static const PEExportSection Get(HMODULE aModule, const MMPolicy& aMMPolicy) { + return Get(PEHeaders::HModuleToBaseAddr<uintptr_t>(aModule), aMMPolicy); + } + + explicit operator bool() const { + // Because PEExportSection doesn't use MMPolicy::Reserve(), a boolified + // mMMPolicy is expected to be false. We don't check mMMPolicy here. + return mImageBase && mRvaDirStart && mRvaDirEnd && mExportAddressTable && + mExportNameTable && mExportOrdinalTable; + } + + template <typename T> + T RVAToPtr(uint32_t aRva) const { + return reinterpret_cast<T>(mImageBase + aRva); + } + + PIMAGE_EXPORT_DIRECTORY GetExportDirectory() const { + if (!*this) { + return nullptr; + } + + return RVAToPtr<PIMAGE_EXPORT_DIRECTORY>(mRvaDirStart); + } + + /** + * This functions searches the export table for a given string as + * GetProcAddress does, but this returns a matched entry of the Export + * Address Table i.e. a pointer to an RVA of a matched function instead + * of a function address. If the entry is forwarded, this function + * returns nullptr. + */ + const DWORD* FindExportAddressTableEntry( + const char* aFunctionNameASCII) const { + if (!*this || !aFunctionNameASCII) { + return nullptr; + } + + struct NameTableComparator { + NameTableComparator(const PEExportSection<MMPolicy>& aExportSection, + const char* aTarget) + : mExportSection(aExportSection), + mTargetName(aTarget), + mTargetNamelength(StrlenASCII(aTarget)) {} + + int operator()(DWORD aRVAToString) const { + mozilla::interceptor::TargetObjectArray<MMPolicy, char> itemString( + mExportSection.mMMPolicy, mExportSection.mImageBase + aRVAToString, + mTargetNamelength + 1); + return StrcmpASCII(mTargetName, itemString[0]); + } + + const PEExportSection<MMPolicy>& mExportSection; + const char* mTargetName; + size_t mTargetNamelength; + }; + + const NameTableComparator comp(*this, aFunctionNameASCII); + + size_t match; + if (!mExportNameTable.BinarySearchIf(comp, &match)) { + return nullptr; + } + + const WORD* index = mExportOrdinalTable[match]; + if (!index) { + return nullptr; + } + + const DWORD* rvaToFunction = mExportAddressTable[*index]; + if (!rvaToFunction) { + return nullptr; + } + + if (*rvaToFunction >= mRvaDirStart && *rvaToFunction < mRvaDirEnd) { + // If an entry points to an address within the export section, the + // field is a forwarder RVA. We return nullptr because the entry is + // not a function address but a null-terminated string used for export + // forwarding. + return nullptr; + } + + return rvaToFunction; + } + + /** + * This functions behaves the same as the native ::GetProcAddress except + * the following cases: + * - Returns nullptr if a target entry is forwarded to another dll. + */ + FARPROC GetProcAddress(const char* aFunctionNameASCII) const { + uintptr_t maybeOdrinal = reinterpret_cast<uintptr_t>(aFunctionNameASCII); + // When the high-order word of |aFunctionNameASCII| is zero, it's not + // a string but an ordinal value. + if (maybeOdrinal < 0x10000) { + return GetProcAddressByOrdinal(static_cast<WORD>(maybeOdrinal)); + } + + auto rvaToFunction = FindExportAddressTableEntry(aFunctionNameASCII); + if (!rvaToFunction) { + return nullptr; + } + return reinterpret_cast<FARPROC>(mImageBase + *rvaToFunction); + } +}; + +inline HANDLE RtlGetProcessHeap() { + PTEB teb = ::NtCurrentTeb(); + PPEB peb = teb->ProcessEnvironmentBlock; + return peb->Reserved4[1]; +} + +inline PVOID RtlGetThreadLocalStoragePointer() { + return ::NtCurrentTeb()->Reserved1[11]; +} + +inline void RtlSetThreadLocalStoragePointerForTestingOnly(PVOID aNewValue) { + ::NtCurrentTeb()->Reserved1[11] = aNewValue; +} + +inline DWORD RtlGetCurrentThreadId() { + PTEB teb = ::NtCurrentTeb(); + CLIENT_ID* cid = reinterpret_cast<CLIENT_ID*>(&teb->Reserved1[8]); + return static_cast<DWORD>(reinterpret_cast<uintptr_t>(cid->UniqueThread) & + 0xFFFFFFFFUL); +} + +const HANDLE kCurrentProcess = reinterpret_cast<HANDLE>(-1); + +inline LauncherResult<DWORD> GetParentProcessId() { + struct PROCESS_BASIC_INFORMATION { + NTSTATUS ExitStatus; + PPEB PebBaseAddress; + ULONG_PTR AffinityMask; + LONG BasePriority; + ULONG_PTR UniqueProcessId; + ULONG_PTR InheritedFromUniqueProcessId; + }; + + ULONG returnLength; + PROCESS_BASIC_INFORMATION pbi = {}; + NTSTATUS status = + ::NtQueryInformationProcess(kCurrentProcess, ProcessBasicInformation, + &pbi, sizeof(pbi), &returnLength); + if (!NT_SUCCESS(status)) { + return LAUNCHER_ERROR_FROM_NTSTATUS(status); + } + + return static_cast<DWORD>(pbi.InheritedFromUniqueProcessId & 0xFFFFFFFF); +} + +inline SIZE_T WINAPI VirtualQueryEx(HANDLE aProcess, LPCVOID aAddress, + PMEMORY_BASIC_INFORMATION aMemInfo, + SIZE_T aMemInfoLen) { +#if defined(MOZILLA_INTERNAL_API) + return ::VirtualQueryEx(aProcess, aAddress, aMemInfo, aMemInfoLen); +#else + SIZE_T returnedLength; + NTSTATUS status = ::NtQueryVirtualMemory( + aProcess, const_cast<PVOID>(aAddress), MemoryBasicInformation, aMemInfo, + aMemInfoLen, &returnedLength); + if (!NT_SUCCESS(status)) { + ::RtlSetLastWin32Error(::RtlNtStatusToDosError(status)); + returnedLength = 0; + } + return returnedLength; +#endif // defined(MOZILLA_INTERNAL_API) +} + +inline SIZE_T WINAPI VirtualQuery(LPCVOID aAddress, + PMEMORY_BASIC_INFORMATION aMemInfo, + SIZE_T aMemInfoLen) { + return nt::VirtualQueryEx(kCurrentProcess, aAddress, aMemInfo, aMemInfoLen); +} + +struct DataDirectoryEntry : public _IMAGE_DATA_DIRECTORY { + DataDirectoryEntry() : _IMAGE_DATA_DIRECTORY() {} + + MOZ_IMPLICIT DataDirectoryEntry(const _IMAGE_DATA_DIRECTORY& aOther) + : _IMAGE_DATA_DIRECTORY(aOther) {} + + DataDirectoryEntry(const DataDirectoryEntry& aOther) = default; + + bool operator==(const DataDirectoryEntry& aOther) const { + return VirtualAddress == aOther.VirtualAddress && Size == aOther.Size; + } + + bool operator!=(const DataDirectoryEntry& aOther) const { + return !(*this == aOther); + } +}; + +inline LauncherResult<void*> GetProcessPebPtr(HANDLE aProcess) { + ULONG returnLength; + PROCESS_BASIC_INFORMATION pbi; + NTSTATUS status = ::NtQueryInformationProcess( + aProcess, ProcessBasicInformation, &pbi, sizeof(pbi), &returnLength); + if (!NT_SUCCESS(status)) { + return LAUNCHER_ERROR_FROM_NTSTATUS(status); + } + + return pbi.PebBaseAddress; +} + +/** + * This function relies on a specific offset into the mostly-undocumented PEB + * structure. The risk is reduced thanks to the fact that the Chromium sandbox + * relies on the location of this field. It is unlikely to change at this point. + * To further reduce the risk, we also check for the magic 'MZ' signature that + * should indicate the beginning of a PE image. + */ +inline LauncherResult<HMODULE> GetProcessExeModule(HANDLE aProcess) { + LauncherResult<void*> ppeb = GetProcessPebPtr(aProcess); + if (ppeb.isErr()) { + return ppeb.propagateErr(); + } + + PEB peb; + SIZE_T bytesRead; + +#if defined(MOZILLA_INTERNAL_API) + if (!::ReadProcessMemory(aProcess, ppeb.unwrap(), &peb, sizeof(peb), + &bytesRead) || + bytesRead != sizeof(peb)) { + return LAUNCHER_ERROR_FROM_LAST(); + } +#else + NTSTATUS ntStatus = ::NtReadVirtualMemory(aProcess, ppeb.unwrap(), &peb, + sizeof(peb), &bytesRead); + if (!NT_SUCCESS(ntStatus) || bytesRead != sizeof(peb)) { + return LAUNCHER_ERROR_FROM_NTSTATUS(ntStatus); + } +#endif + + // peb.ImageBaseAddress + void* baseAddress = peb.Reserved3[1]; + + char mzMagic[2]; +#if defined(MOZILLA_INTERNAL_API) + if (!::ReadProcessMemory(aProcess, baseAddress, mzMagic, sizeof(mzMagic), + &bytesRead) || + bytesRead != sizeof(mzMagic)) { + return LAUNCHER_ERROR_FROM_LAST(); + } +#else + ntStatus = ::NtReadVirtualMemory(aProcess, baseAddress, mzMagic, + sizeof(mzMagic), &bytesRead); + if (!NT_SUCCESS(ntStatus) || bytesRead != sizeof(mzMagic)) { + return LAUNCHER_ERROR_FROM_NTSTATUS(ntStatus); + } +#endif + + MOZ_ASSERT(mzMagic[0] == 'M' && mzMagic[1] == 'Z'); + if (mzMagic[0] != 'M' || mzMagic[1] != 'Z') { + return LAUNCHER_ERROR_FROM_WIN32(ERROR_BAD_EXE_FORMAT); + } + + return static_cast<HMODULE>(baseAddress); +} + +#if defined(_MSC_VER) +extern "C" IMAGE_DOS_HEADER __ImageBase; +#endif + +// This class manages data transfer from the local process's executable +// to another process's executable via WriteProcessMemory. +// Bug 1662560 told us the same executable may be mapped onto a different +// address in a different process. This means when we transfer data within +// the mapped executable such as a global variable or IAT from the current +// process to another process, we need to shift its address by the difference +// between two executable's mapped imagebase. +class CrossExecTransferManager final { + HANDLE mRemoteProcess; + uint8_t* mLocalImagebase; + PEHeaders mLocalExec; + uint8_t* mRemoteImagebase; + + static HMODULE GetLocalExecModule() { +#if defined(_MSC_VER) + return reinterpret_cast<HMODULE>(&__ImageBase); +#else + return ::GetModuleHandleW(nullptr); +#endif + } + + LauncherVoidResult EnsureRemoteImagebase() { + if (!mRemoteImagebase) { + LauncherResult<HMODULE> remoteImageBaseResult = + GetProcessExeModule(mRemoteProcess); + if (remoteImageBaseResult.isErr()) { + return remoteImageBaseResult.propagateErr(); + } + + mRemoteImagebase = + reinterpret_cast<uint8_t*>(remoteImageBaseResult.unwrap()); + } + return Ok(); + } + + template <typename T> + T* LocalExecToRemoteExec(T* aLocalAddress) const { + MOZ_ASSERT(mRemoteImagebase); + MOZ_ASSERT(mLocalExec.IsWithinImage(aLocalAddress)); + + if (!mRemoteImagebase || !mLocalExec.IsWithinImage(aLocalAddress)) { + return aLocalAddress; + } + + uintptr_t offset = reinterpret_cast<uintptr_t>(aLocalAddress) - + reinterpret_cast<uintptr_t>(mLocalImagebase); + return reinterpret_cast<T*>(mRemoteImagebase + offset); + } + + public: + explicit CrossExecTransferManager(HANDLE aRemoteProcess) + : mRemoteProcess(aRemoteProcess), + mLocalImagebase( + PEHeaders::HModuleToBaseAddr<uint8_t*>(GetLocalExecModule())), + mLocalExec(mLocalImagebase), + mRemoteImagebase(nullptr) {} + + CrossExecTransferManager(HANDLE aRemoteProcess, HMODULE aLocalImagebase) + : mRemoteProcess(aRemoteProcess), + mLocalImagebase( + PEHeaders::HModuleToBaseAddr<uint8_t*>(aLocalImagebase)), + mLocalExec(mLocalImagebase), + mRemoteImagebase(nullptr) {} + + explicit operator bool() const { return !!mLocalExec; } + HANDLE RemoteProcess() const { return mRemoteProcess; } + const PEHeaders& LocalPEHeaders() const { return mLocalExec; } + + AutoVirtualProtect Protect(void* aLocalAddress, size_t aLength, + DWORD aProtFlags) { + // If EnsureRemoteImagebase() fails, a subsequent operaion will fail. + Unused << EnsureRemoteImagebase(); + return AutoVirtualProtect(LocalExecToRemoteExec(aLocalAddress), aLength, + aProtFlags, mRemoteProcess); + } + + LauncherVoidResult Transfer(LPVOID aDestinationAddress, + LPCVOID aBufferToWrite, SIZE_T aBufferSize) { + LauncherVoidResult result = EnsureRemoteImagebase(); + if (result.isErr()) { + return result.propagateErr(); + } + + if (!::WriteProcessMemory(mRemoteProcess, + LocalExecToRemoteExec(aDestinationAddress), + aBufferToWrite, aBufferSize, nullptr)) { + return LAUNCHER_ERROR_FROM_LAST(); + } + + return Ok(); + } +}; + +#if !defined(MOZILLA_INTERNAL_API) + +inline LauncherResult<HMODULE> GetModuleHandleFromLeafName( + const UNICODE_STRING& aTarget) { + auto maybePeb = nt::GetProcessPebPtr(kCurrentProcess); + if (maybePeb.isErr()) { + return maybePeb.propagateErr(); + } + + const PPEB peb = reinterpret_cast<PPEB>(maybePeb.unwrap()); + if (!peb->Ldr) { + return LAUNCHER_ERROR_FROM_WIN32(ERROR_BAD_EXE_FORMAT); + } + + auto firstItem = &peb->Ldr->InMemoryOrderModuleList; + for (auto p = firstItem->Flink; p != firstItem; p = p->Flink) { + const auto currentTableEntry = + CONTAINING_RECORD(p, LDR_DATA_TABLE_ENTRY, InMemoryOrderLinks); + + UNICODE_STRING leafName; + nt::GetLeafName(&leafName, ¤tTableEntry->FullDllName); + + if (::RtlCompareUnicodeString(&leafName, &aTarget, TRUE) == 0) { + return reinterpret_cast<HMODULE>(currentTableEntry->DllBase); + } + } + + return LAUNCHER_ERROR_FROM_WIN32(ERROR_MOD_NOT_FOUND); +} + +class MOZ_ONLY_USED_TO_AVOID_STATIC_CONSTRUCTORS SRWLock final { + public: + constexpr SRWLock() : mLock(SRWLOCK_INIT) {} + + void LockShared() { ::RtlAcquireSRWLockShared(&mLock); } + + void LockExclusive() { ::RtlAcquireSRWLockExclusive(&mLock); } + + void UnlockShared() { ::RtlReleaseSRWLockShared(&mLock); } + + void UnlockExclusive() { ::RtlReleaseSRWLockExclusive(&mLock); } + + SRWLock(const SRWLock&) = delete; + SRWLock(SRWLock&&) = delete; + SRWLock& operator=(const SRWLock&) = delete; + SRWLock& operator=(SRWLock&&) = delete; + + SRWLOCK* operator&() { return &mLock; } + + private: + SRWLOCK mLock; +}; + +class MOZ_RAII AutoExclusiveLock final { + public: + explicit AutoExclusiveLock(SRWLock& aLock) : mLock(aLock) { + aLock.LockExclusive(); + } + + ~AutoExclusiveLock() { mLock.UnlockExclusive(); } + + AutoExclusiveLock(const AutoExclusiveLock&) = delete; + AutoExclusiveLock(AutoExclusiveLock&&) = delete; + AutoExclusiveLock& operator=(const AutoExclusiveLock&) = delete; + AutoExclusiveLock& operator=(AutoExclusiveLock&&) = delete; + + private: + SRWLock& mLock; +}; + +class MOZ_RAII AutoSharedLock final { + public: + explicit AutoSharedLock(SRWLock& aLock) : mLock(aLock) { aLock.LockShared(); } + + ~AutoSharedLock() { mLock.UnlockShared(); } + + AutoSharedLock(const AutoSharedLock&) = delete; + AutoSharedLock(AutoSharedLock&&) = delete; + AutoSharedLock& operator=(const AutoSharedLock&) = delete; + AutoSharedLock& operator=(AutoSharedLock&&) = delete; + + private: + SRWLock& mLock; +}; + +#endif // !defined(MOZILLA_INTERNAL_API) + +class RtlAllocPolicy { + public: + template <typename T> + T* maybe_pod_malloc(size_t aNumElems) { + if (aNumElems & mozilla::tl::MulOverflowMask<sizeof(T)>::value) { + return nullptr; + } + + return static_cast<T*>( + ::RtlAllocateHeap(RtlGetProcessHeap(), 0, aNumElems * sizeof(T))); + } + + template <typename T> + T* maybe_pod_calloc(size_t aNumElems) { + if (aNumElems & mozilla::tl::MulOverflowMask<sizeof(T)>::value) { + return nullptr; + } + + return static_cast<T*>(::RtlAllocateHeap( + RtlGetProcessHeap(), HEAP_ZERO_MEMORY, aNumElems * sizeof(T))); + } + + template <typename T> + T* maybe_pod_realloc(T* aPtr, size_t aOldSize, size_t aNewSize) { + if (aNewSize & mozilla::tl::MulOverflowMask<sizeof(T)>::value) { + return nullptr; + } + + return static_cast<T*>(::RtlReAllocateHeap(RtlGetProcessHeap(), 0, aPtr, + aNewSize * sizeof(T))); + } + + template <typename T> + T* pod_malloc(size_t aNumElems) { + return maybe_pod_malloc<T>(aNumElems); + } + + template <typename T> + T* pod_calloc(size_t aNumElems) { + return maybe_pod_calloc<T>(aNumElems); + } + + template <typename T> + T* pod_realloc(T* aPtr, size_t aOldSize, size_t aNewSize) { + return maybe_pod_realloc<T>(aPtr, aOldSize, aNewSize); + } + + template <typename T> + void free_(T* aPtr, size_t aNumElems = 0) { + ::RtlFreeHeap(RtlGetProcessHeap(), 0, aPtr); + } + + void reportAllocOverflow() const {} + + [[nodiscard]] bool checkSimulatedOOM() const { return true; } +}; + +class AutoMappedView final { + void* mView; + + void Unmap() { + if (!mView) { + return; + } + +#if defined(MOZILLA_INTERNAL_API) + ::UnmapViewOfFile(mView); +#else + NTSTATUS status = ::NtUnmapViewOfSection(nt::kCurrentProcess, mView); + if (!NT_SUCCESS(status)) { + ::RtlSetLastWin32Error(::RtlNtStatusToDosError(status)); + } +#endif + mView = nullptr; + } + + public: + explicit AutoMappedView(void* aView) : mView(aView) {} + + AutoMappedView(HANDLE aSection, ULONG aProtectionFlags) : mView(nullptr) { +#if defined(MOZILLA_INTERNAL_API) + mView = ::MapViewOfFile(aSection, aProtectionFlags, 0, 0, 0); +#else + SIZE_T viewSize = 0; + NTSTATUS status = ::NtMapViewOfSection(aSection, nt::kCurrentProcess, + &mView, 0, 0, nullptr, &viewSize, + ViewUnmap, 0, aProtectionFlags); + if (!NT_SUCCESS(status)) { + ::RtlSetLastWin32Error(::RtlNtStatusToDosError(status)); + } +#endif + } + ~AutoMappedView() { Unmap(); } + + // Allow move & Disallow copy + AutoMappedView(AutoMappedView&& aOther) : mView(aOther.mView) { + aOther.mView = nullptr; + } + AutoMappedView& operator=(AutoMappedView&& aOther) { + if (this != &aOther) { + Unmap(); + mView = aOther.mView; + aOther.mView = nullptr; + } + return *this; + } + AutoMappedView(const AutoMappedView&) = delete; + AutoMappedView& operator=(const AutoMappedView&) = delete; + + explicit operator bool() const { return !!mView; } + template <typename T> + T* as() { + return reinterpret_cast<T*>(mView); + } + + void* release() { + void* p = mView; + mView = nullptr; + return p; + } +}; + +} // namespace nt +} // namespace mozilla + +#endif // mozilla_NativeNt_h diff --git a/mozglue/misc/PlatformConditionVariable.h b/mozglue/misc/PlatformConditionVariable.h new file mode 100644 index 0000000000..61fb06ade1 --- /dev/null +++ b/mozglue/misc/PlatformConditionVariable.h @@ -0,0 +1,71 @@ +/* -*- 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/. */ + +#ifndef mozilla_ConditionVariable_h +#define mozilla_ConditionVariable_h + +#include <stdint.h> + +#include <utility> + +#include "mozilla/Attributes.h" +#include "mozilla/PlatformMutex.h" +#include "mozilla/TimeStamp.h" +#if !defined(XP_WIN) && !defined(__wasi__) +# include <pthread.h> +#endif + +namespace mozilla { + +enum class CVStatus { NoTimeout, Timeout }; + +namespace detail { + +class ConditionVariableImpl { + public: + struct PlatformData; + + MFBT_API ConditionVariableImpl(); + MFBT_API ~ConditionVariableImpl(); + + // Wake one thread that is waiting on this condition. + MFBT_API void notify_one(); + + // Wake all threads that are waiting on this condition. + MFBT_API void notify_all(); + + // Atomically release |lock| and sleep the current thread of execution on + // this condition variable. + // |lock| will be re-acquired before this function returns. + // The thread may be woken from sleep from another thread via notify_one() + // or notify_all(), but may also wake spuriously. The caller should recheck + // its predicate after this function returns, typically in a while loop. + MFBT_API void wait(MutexImpl& lock); + + MFBT_API CVStatus wait_for(MutexImpl& lock, + const mozilla::TimeDuration& rel_time); + + private: + ConditionVariableImpl(const ConditionVariableImpl&) = delete; + ConditionVariableImpl& operator=(const ConditionVariableImpl&) = delete; + + PlatformData* platformData(); + +#if !defined(XP_WIN) && !defined(__wasi__) + void* platformData_[sizeof(pthread_cond_t) / sizeof(void*)]; + static_assert(sizeof(pthread_cond_t) / sizeof(void*) != 0 && + sizeof(pthread_cond_t) % sizeof(void*) == 0, + "pthread_cond_t must have pointer alignment"); +#else + void* platformData_[4]; +#endif +}; + +} // namespace detail + +} // namespace mozilla + +#endif // mozilla_ConditionVariable_h diff --git a/mozglue/misc/PlatformMutex.h b/mozglue/misc/PlatformMutex.h new file mode 100644 index 0000000000..ac5459cf10 --- /dev/null +++ b/mozglue/misc/PlatformMutex.h @@ -0,0 +1,66 @@ +/* -*- 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/. */ + +#ifndef mozilla_PlatformMutex_h +#define mozilla_PlatformMutex_h + +#include <utility> + +#include "mozilla/Attributes.h" +#include "mozilla/Types.h" + +#if !defined(XP_WIN) && !defined(__wasi__) +# include <pthread.h> +#endif + +namespace mozilla { + +namespace detail { + +class ConditionVariableImpl; + +class MutexImpl { + public: + struct PlatformData; + + explicit MFBT_API MutexImpl(); + MFBT_API ~MutexImpl(); + + protected: + MFBT_API void lock(); + MFBT_API void unlock(); + // We have a separate, forwarding API so internal uses don't have to go + // through the PLT. + MFBT_API bool tryLock(); + + private: + MutexImpl(const MutexImpl&) = delete; + void operator=(const MutexImpl&) = delete; + MutexImpl(MutexImpl&&) = delete; + void operator=(MutexImpl&&) = delete; + bool operator==(const MutexImpl& rhs) = delete; + + void mutexLock(); + bool mutexTryLock(); + + PlatformData* platformData(); + +#if !defined(XP_WIN) && !defined(__wasi__) + void* platformData_[sizeof(pthread_mutex_t) / sizeof(void*)]; + static_assert(sizeof(pthread_mutex_t) / sizeof(void*) != 0 && + sizeof(pthread_mutex_t) % sizeof(void*) == 0, + "pthread_mutex_t must have pointer alignment"); +#else + void* platformData_[6]; +#endif + + friend class mozilla::detail::ConditionVariableImpl; +}; + +} // namespace detail + +} // namespace mozilla +#endif // mozilla_PlatformMutex_h diff --git a/mozglue/misc/PlatformRWLock.h b/mozglue/misc/PlatformRWLock.h new file mode 100644 index 0000000000..0acd09b25b --- /dev/null +++ b/mozglue/misc/PlatformRWLock.h @@ -0,0 +1,50 @@ +/* -*- 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/. */ + +#ifndef mozilla_PlatformRWLock_h +#define mozilla_PlatformRWLock_h + +#include "mozilla/Types.h" + +#ifndef XP_WIN +# include <pthread.h> +#endif + +namespace mozilla::detail { + +class RWLockImpl { + public: + explicit MFBT_API RWLockImpl(); + MFBT_API ~RWLockImpl(); + + protected: + [[nodiscard]] MFBT_API bool tryReadLock(); + MFBT_API void readLock(); + MFBT_API void readUnlock(); + + [[nodiscard]] MFBT_API bool tryWriteLock(); + MFBT_API void writeLock(); + MFBT_API void writeUnlock(); + + private: + RWLockImpl(const RWLockImpl&) = delete; + void operator=(const RWLockImpl&) = delete; + RWLockImpl(RWLockImpl&&) = delete; + void operator=(RWLockImpl&&) = delete; + bool operator==(const RWLockImpl& rhs) = delete; + +#ifndef XP_WIN + pthread_rwlock_t mRWLock; +#else + // SRWLock is pointer-sized. We declare it in such a fashion here to avoid + // pulling in windows.h wherever this header is used. + void* mRWLock; +#endif +}; + +} // namespace mozilla::detail + +#endif // mozilla_PlatformRWLock_h diff --git a/mozglue/misc/PreXULSkeletonUI.cpp b/mozglue/misc/PreXULSkeletonUI.cpp new file mode 100644 index 0000000000..145a525f85 --- /dev/null +++ b/mozglue/misc/PreXULSkeletonUI.cpp @@ -0,0 +1,2238 @@ +/* -*- 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 "PreXULSkeletonUI.h" + +#include <algorithm> +#include <math.h> +#include <limits.h> +#include <cmath> +#include <locale> +#include <string> +#include <objbase.h> +#include <shlobj.h> + +#include "mozilla/Assertions.h" +#include "mozilla/Attributes.h" +#include "mozilla/BaseProfilerMarkers.h" +#include "mozilla/CacheNtDllThunk.h" +#include "mozilla/FStream.h" +#include "mozilla/GetKnownFolderPath.h" +#include "mozilla/HashFunctions.h" +#include "mozilla/HelperMacros.h" +#include "mozilla/glue/Debug.h" +#include "mozilla/Maybe.h" +#include "mozilla/mscom/ProcessRuntime.h" +#include "mozilla/ResultVariant.h" +#include "mozilla/ScopeExit.h" +#include "mozilla/UniquePtr.h" +#include "mozilla/UniquePtrExtensions.h" +#include "mozilla/Unused.h" +#include "mozilla/WindowsDpiAwareness.h" +#include "mozilla/WindowsVersion.h" +#include "mozilla/WindowsProcessMitigations.h" + +namespace mozilla { + +// ColorRect defines an optionally-rounded, optionally-bordered rectangle of a +// particular color that we will draw. +struct ColorRect { + uint32_t color; + uint32_t borderColor; + int x; + int y; + int width; + int height; + int borderWidth; + int borderRadius; + bool flipIfRTL; +}; + +// DrawRect is mostly the same as ColorRect, but exists as an implementation +// detail to simplify drawing borders. We draw borders as a strokeOnly rect +// underneath an inner rect of a particular color. We also need to keep +// track of the backgroundColor for rounding rects, in order to correctly +// anti-alias. +struct DrawRect { + uint32_t color; + uint32_t backgroundColor; + int x; + int y; + int width; + int height; + int borderRadius; + int borderWidth; + bool strokeOnly; +}; + +struct NormalizedRGB { + double r; + double g; + double b; +}; + +NormalizedRGB UintToRGB(uint32_t color) { + double r = static_cast<double>(color >> 16 & 0xff) / 255.0; + double g = static_cast<double>(color >> 8 & 0xff) / 255.0; + double b = static_cast<double>(color >> 0 & 0xff) / 255.0; + return NormalizedRGB{r, g, b}; +} + +uint32_t RGBToUint(const NormalizedRGB& rgb) { + return (static_cast<uint32_t>(rgb.r * 255.0) << 16) | + (static_cast<uint32_t>(rgb.g * 255.0) << 8) | + (static_cast<uint32_t>(rgb.b * 255.0) << 0); +} + +double Lerp(double a, double b, double x) { return a + x * (b - a); } + +NormalizedRGB Lerp(const NormalizedRGB& a, const NormalizedRGB& b, double x) { + return NormalizedRGB{Lerp(a.r, b.r, x), Lerp(a.g, b.g, x), Lerp(a.b, b.b, x)}; +} + +// Produces a smooth curve in [0,1] based on a linear input in [0,1] +double SmoothStep3(double x) { return x * x * (3.0 - 2.0 * x); } + +static const wchar_t kPreXULSkeletonUIKeyPath[] = + L"SOFTWARE" + L"\\" MOZ_APP_VENDOR L"\\" MOZ_APP_BASENAME L"\\PreXULSkeletonUISettings"; + +static bool sPreXULSkeletonUIShown = false; +static bool sPreXULSkeletonUIEnabled = false; +static HWND sPreXULSkeletonUIWindow; +static LPWSTR const gStockApplicationIcon = MAKEINTRESOURCEW(32512); +static LPWSTR const gIDCWait = MAKEINTRESOURCEW(32514); +static HANDLE sPreXULSKeletonUIAnimationThread; +static HANDLE sPreXULSKeletonUILockFile = INVALID_HANDLE_VALUE; + +static mozilla::mscom::ProcessRuntime* sProcessRuntime; +static uint32_t* sPixelBuffer = nullptr; +static Vector<ColorRect>* sAnimatedRects = nullptr; +static int sTotalChromeHeight = 0; +static volatile LONG sAnimationControlFlag = 0; +static bool sMaximized = false; +static int sNonClientVerticalMargins = 0; +static int sNonClientHorizontalMargins = 0; +static uint32_t sDpi = 0; + +// Color values needed by the animation loop +static uint32_t sAnimationColor; +static uint32_t sToolbarForegroundColor; + +static ThemeMode sTheme = ThemeMode::Invalid; + +typedef BOOL(WINAPI* EnableNonClientDpiScalingProc)(HWND); +static EnableNonClientDpiScalingProc sEnableNonClientDpiScaling = NULL; +typedef int(WINAPI* GetSystemMetricsForDpiProc)(int, UINT); +GetSystemMetricsForDpiProc sGetSystemMetricsForDpi = NULL; +typedef UINT(WINAPI* GetDpiForWindowProc)(HWND); +GetDpiForWindowProc sGetDpiForWindow = NULL; +typedef ATOM(WINAPI* RegisterClassWProc)(const WNDCLASSW*); +RegisterClassWProc sRegisterClassW = NULL; +typedef HICON(WINAPI* LoadIconWProc)(HINSTANCE, LPCWSTR); +LoadIconWProc sLoadIconW = NULL; +typedef HICON(WINAPI* LoadCursorWProc)(HINSTANCE, LPCWSTR); +LoadCursorWProc sLoadCursorW = NULL; +typedef HWND(WINAPI* CreateWindowExWProc)(DWORD, LPCWSTR, LPCWSTR, DWORD, int, + int, int, int, HWND, HMENU, HINSTANCE, + LPVOID); +CreateWindowExWProc sCreateWindowExW = NULL; +typedef BOOL(WINAPI* ShowWindowProc)(HWND, int); +ShowWindowProc sShowWindow = NULL; +typedef BOOL(WINAPI* SetWindowPosProc)(HWND, HWND, int, int, int, int, UINT); +SetWindowPosProc sSetWindowPos = NULL; +typedef HDC(WINAPI* GetWindowDCProc)(HWND); +GetWindowDCProc sGetWindowDC = NULL; +typedef int(WINAPI* FillRectProc)(HDC, const RECT*, HBRUSH); +FillRectProc sFillRect = NULL; +typedef BOOL(WINAPI* DeleteObjectProc)(HGDIOBJ); +DeleteObjectProc sDeleteObject = NULL; +typedef int(WINAPI* ReleaseDCProc)(HWND, HDC); +ReleaseDCProc sReleaseDC = NULL; +typedef HMONITOR(WINAPI* MonitorFromWindowProc)(HWND, DWORD); +MonitorFromWindowProc sMonitorFromWindow = NULL; +typedef BOOL(WINAPI* GetMonitorInfoWProc)(HMONITOR, LPMONITORINFO); +GetMonitorInfoWProc sGetMonitorInfoW = NULL; +typedef LONG_PTR(WINAPI* SetWindowLongPtrWProc)(HWND, int, LONG_PTR); +SetWindowLongPtrWProc sSetWindowLongPtrW = NULL; +typedef int(WINAPI* StretchDIBitsProc)(HDC, int, int, int, int, int, int, int, + int, const VOID*, const BITMAPINFO*, + UINT, DWORD); +StretchDIBitsProc sStretchDIBits = NULL; +typedef HBRUSH(WINAPI* CreateSolidBrushProc)(COLORREF); +CreateSolidBrushProc sCreateSolidBrush = NULL; + +static int sWindowWidth; +static int sWindowHeight; +static double sCSSToDevPixelScaling; + +static Maybe<PreXULSkeletonUIError> sErrorReason; + +static const int kAnimationCSSPixelsPerFrame = 11; +static const int kAnimationCSSExtraWindowSize = 300; + +// NOTE: these values were pulled out of thin air as round numbers that are +// likely to be too big to be seen in practice. If we legitimately see windows +// this big, we probably don't want to be drawing them on the CPU anyway. +static const uint32_t kMaxWindowWidth = 1 << 16; +static const uint32_t kMaxWindowHeight = 1 << 16; + +static const wchar_t* sEnabledRegSuffix = L"|Enabled"; +static const wchar_t* sScreenXRegSuffix = L"|ScreenX"; +static const wchar_t* sScreenYRegSuffix = L"|ScreenY"; +static const wchar_t* sWidthRegSuffix = L"|Width"; +static const wchar_t* sHeightRegSuffix = L"|Height"; +static const wchar_t* sMaximizedRegSuffix = L"|Maximized"; +static const wchar_t* sUrlbarCSSRegSuffix = L"|UrlbarCSSSpan"; +static const wchar_t* sCssToDevPixelScalingRegSuffix = L"|CssToDevPixelScaling"; +static const wchar_t* sSearchbarRegSuffix = L"|SearchbarCSSSpan"; +static const wchar_t* sSpringsCSSRegSuffix = L"|SpringsCSSSpan"; +static const wchar_t* sThemeRegSuffix = L"|Theme"; +static const wchar_t* sFlagsRegSuffix = L"|Flags"; +static const wchar_t* sProgressSuffix = L"|Progress"; + +std::wstring GetRegValueName(const wchar_t* prefix, const wchar_t* suffix) { + std::wstring result(prefix); + result.append(suffix); + return result; +} + +// This is paraphrased from WinHeaderOnlyUtils.h. The fact that this file is +// included in standalone SpiderMonkey builds prohibits us from including that +// file directly, and it hardly warrants its own header. Bug 1674920 tracks +// only including this file for gecko-related builds. +Result<UniquePtr<wchar_t[]>, PreXULSkeletonUIError> GetBinaryPath() { + DWORD bufLen = MAX_PATH; + UniquePtr<wchar_t[]> buf; + while (true) { + buf = MakeUnique<wchar_t[]>(bufLen); + DWORD retLen = ::GetModuleFileNameW(nullptr, buf.get(), bufLen); + if (!retLen) { + return Err(PreXULSkeletonUIError::FilesystemFailure); + } + + if (retLen == bufLen && ::GetLastError() == ERROR_INSUFFICIENT_BUFFER) { + bufLen *= 2; + continue; + } + + break; + } + + return buf; +} + +// PreXULSkeletonUIDisallowed means that we don't even have the capacity to +// enable the skeleton UI, whether because we're on a platform that doesn't +// support it or because we launched with command line arguments that we don't +// support. Some of these situations are transient, so we want to make sure we +// don't mess with registry values in these scenarios that we may use in +// other scenarios in which the skeleton UI is actually enabled. +static bool PreXULSkeletonUIDisallowed() { + return sErrorReason.isSome() && + (*sErrorReason == PreXULSkeletonUIError::Cmdline || + *sErrorReason == PreXULSkeletonUIError::EnvVars); +} + +// Note: this is specifically *not* a robust, multi-locale lowercasing +// operation. It is not intended to be such. It is simply intended to match the +// way in which we look for other instances of firefox to remote into. +// See +// https://searchfox.org/mozilla-central/rev/71621bfa47a371f2b1ccfd33c704913124afb933/toolkit/components/remote/nsRemoteService.cpp#56 +static void MutateStringToLowercase(wchar_t* ptr) { + while (*ptr) { + wchar_t ch = *ptr; + if (ch >= L'A' && ch <= L'Z') { + *ptr = ch + (L'a' - L'A'); + } + ++ptr; + } +} + +static Result<Ok, PreXULSkeletonUIError> GetSkeletonUILock() { + auto localAppDataPath = GetKnownFolderPath(FOLDERID_LocalAppData); + if (!localAppDataPath) { + return Err(PreXULSkeletonUIError::FilesystemFailure); + } + + if (sPreXULSKeletonUILockFile != INVALID_HANDLE_VALUE) { + return Ok(); + } + + // Note: because we're in mozglue, we cannot easily access things from + // toolkit, like `GetInstallHash`. We could move `GetInstallHash` into + // mozglue, and rip out all of its usage of types defined in toolkit headers. + // However, it seems cleaner to just hash the bin path ourselves. We don't + // get quite the same robustness that `GetInstallHash` might provide, but + // we already don't have that with how we key our registry values, so it + // probably makes sense to just match those. + UniquePtr<wchar_t[]> binPath; + MOZ_TRY_VAR(binPath, GetBinaryPath()); + + // Lowercase the binpath to match how we look for remote instances. + MutateStringToLowercase(binPath.get()); + + // The number of bytes * 2 characters per byte + 1 for the null terminator + uint32_t hexHashSize = sizeof(uint32_t) * 2 + 1; + UniquePtr<wchar_t[]> installHash = MakeUnique<wchar_t[]>(hexHashSize); + // This isn't perfect - it's a 32-bit hash of the path to our executable. It + // could reasonably collide, or casing could potentially affect things, but + // the theory is that that should be uncommon enough and the failure case + // mild enough that this is fine. + uint32_t binPathHash = HashString(binPath.get()); + swprintf(installHash.get(), hexHashSize, L"%08x", binPathHash); + + std::wstring lockFilePath; + lockFilePath.append(localAppDataPath.get()); + lockFilePath.append( + L"\\" MOZ_APP_VENDOR L"\\" MOZ_APP_BASENAME L"\\SkeletonUILock-"); + lockFilePath.append(installHash.get()); + + // We intentionally leak this file - that is okay, and (kind of) the point. + // We want to hold onto this handle until the application exits, and hold + // onto it with exclusive rights. If this check fails, then we assume that + // another instance of the executable is holding it, and thus return false. + sPreXULSKeletonUILockFile = + ::CreateFileW(lockFilePath.c_str(), GENERIC_READ | GENERIC_WRITE, + 0, // No sharing - this is how the lock works + nullptr, CREATE_ALWAYS, + FILE_FLAG_DELETE_ON_CLOSE, // Don't leave this lying around + nullptr); + if (sPreXULSKeletonUILockFile == INVALID_HANDLE_VALUE) { + return Err(PreXULSkeletonUIError::FailedGettingLock); + } + + return Ok(); +} + +const char kGeneralSection[] = "[General]"; +const char kStartWithLastProfile[] = "StartWithLastProfile="; + +static bool ProfileDbHasStartWithLastProfile(IFStream& iniContents) { + bool inGeneral = false; + std::string line; + while (std::getline(iniContents, line)) { + size_t whitespace = 0; + while (line.length() > whitespace && + (line[whitespace] == ' ' || line[whitespace] == '\t')) { + whitespace++; + } + line.erase(0, whitespace); + + if (line.compare(kGeneralSection) == 0) { + inGeneral = true; + } else if (inGeneral) { + if (line[0] == '[') { + inGeneral = false; + } else { + if (line.find(kStartWithLastProfile) == 0) { + char val = line.c_str()[sizeof(kStartWithLastProfile) - 1]; + if (val == '0') { + return false; + } else if (val == '1') { + return true; + } + } + } + } + } + + // If we don't find it in the .ini file, we interpret that as true + return true; +} + +static Result<Ok, PreXULSkeletonUIError> CheckForStartWithLastProfile() { + auto roamingAppData = GetKnownFolderPath(FOLDERID_RoamingAppData); + if (!roamingAppData) { + return Err(PreXULSkeletonUIError::FilesystemFailure); + } + std::wstring profileDbPath(roamingAppData.get()); + profileDbPath.append( + L"\\" MOZ_APP_VENDOR L"\\" MOZ_APP_BASENAME L"\\profiles.ini"); + IFStream profileDb(profileDbPath.c_str()); + if (profileDb.fail()) { + return Err(PreXULSkeletonUIError::FilesystemFailure); + } + + if (!ProfileDbHasStartWithLastProfile(profileDb)) { + return Err(PreXULSkeletonUIError::NoStartWithLastProfile); + } + + return Ok(); +} + +// We could use nsAutoRegKey, but including nsWindowsHelpers.h causes build +// failures in random places because we're in mozglue. Overall it should be +// simpler and cleaner to just step around that issue with this class: +class MOZ_RAII AutoCloseRegKey { + public: + explicit AutoCloseRegKey(HKEY key) : mKey(key) {} + ~AutoCloseRegKey() { ::RegCloseKey(mKey); } + + private: + HKEY mKey; +}; + +int CSSToDevPixels(double cssPixels, double scaling) { + return floor(cssPixels * scaling + 0.5); +} + +int CSSToDevPixels(int cssPixels, double scaling) { + return CSSToDevPixels((double)cssPixels, scaling); +} + +int CSSToDevPixelsFloor(double cssPixels, double scaling) { + return floor(cssPixels * scaling); +} + +// Some things appear to floor to device pixels rather than rounding. A good +// example of this is border widths. +int CSSToDevPixelsFloor(int cssPixels, double scaling) { + return CSSToDevPixelsFloor((double)cssPixels, scaling); +} + +double SignedDistanceToCircle(double x, double y, double radius) { + return sqrt(x * x + y * y) - radius; +} + +// For more details, see +// https://searchfox.org/mozilla-central/rev/a5d9abfda1e26b1207db9549549ab0bdd73f735d/gfx/wr/webrender/res/shared.glsl#141-187 +// which was a reference for this function. +double DistanceAntiAlias(double signedDistance) { + // Distance assumed to be in device pixels. We use an aa range of 0.5 for + // reasons detailed in the linked code above. + const double aaRange = 0.5; + double dist = 0.5 * signedDistance / aaRange; + if (dist <= -0.5 + std::numeric_limits<double>::epsilon()) return 1.0; + if (dist >= 0.5 - std::numeric_limits<double>::epsilon()) return 0.0; + return 0.5 + dist * (0.8431027 * dist * dist - 1.14453603); +} + +void RasterizeRoundedRectTopAndBottom(const DrawRect& rect) { + if (rect.height <= 2 * rect.borderRadius) { + MOZ_ASSERT(false, "Skeleton UI rect height too small for border radius."); + return; + } + if (rect.width <= 2 * rect.borderRadius) { + MOZ_ASSERT(false, "Skeleton UI rect width too small for border radius."); + return; + } + + NormalizedRGB rgbBase = UintToRGB(rect.backgroundColor); + NormalizedRGB rgbBlend = UintToRGB(rect.color); + + for (int rowIndex = 0; rowIndex < rect.borderRadius; ++rowIndex) { + int yTop = rect.y + rect.borderRadius - 1 - rowIndex; + int yBottom = rect.y + rect.height - rect.borderRadius + rowIndex; + + uint32_t* lineStartTop = &sPixelBuffer[yTop * sWindowWidth]; + uint32_t* innermostPixelTopLeft = + lineStartTop + rect.x + rect.borderRadius - 1; + uint32_t* innermostPixelTopRight = + lineStartTop + rect.x + rect.width - rect.borderRadius; + uint32_t* lineStartBottom = &sPixelBuffer[yBottom * sWindowWidth]; + uint32_t* innermostPixelBottomLeft = + lineStartBottom + rect.x + rect.borderRadius - 1; + uint32_t* innermostPixelBottomRight = + lineStartBottom + rect.x + rect.width - rect.borderRadius; + + // Add 0.5 to x and y to get the pixel center. + double pixelY = (double)rowIndex + 0.5; + for (int columnIndex = 0; columnIndex < rect.borderRadius; ++columnIndex) { + double pixelX = (double)columnIndex + 0.5; + double distance = + SignedDistanceToCircle(pixelX, pixelY, (double)rect.borderRadius); + double alpha = DistanceAntiAlias(distance); + NormalizedRGB rgb = Lerp(rgbBase, rgbBlend, alpha); + uint32_t color = RGBToUint(rgb); + + innermostPixelTopLeft[-columnIndex] = color; + innermostPixelTopRight[columnIndex] = color; + innermostPixelBottomLeft[-columnIndex] = color; + innermostPixelBottomRight[columnIndex] = color; + } + + std::fill(innermostPixelTopLeft + 1, innermostPixelTopRight, rect.color); + std::fill(innermostPixelBottomLeft + 1, innermostPixelBottomRight, + rect.color); + } +} + +void RasterizeAnimatedRoundedRectTopAndBottom( + const ColorRect& colorRect, const uint32_t* animationLookup, + int priorUpdateAreaMin, int priorUpdateAreaMax, int currentUpdateAreaMin, + int currentUpdateAreaMax, int animationMin) { + // We iterate through logical pixel rows here, from inside to outside, which + // for the top of the rounded rect means from bottom to top, and for the + // bottom of the rect means top to bottom. We paint pixels from left to + // right on the top and bottom rows at the same time for the entire animation + // window. (If the animation window does not overlap any rounded corners, + // however, we won't be called at all) + for (int rowIndex = 0; rowIndex < colorRect.borderRadius; ++rowIndex) { + int yTop = colorRect.y + colorRect.borderRadius - 1 - rowIndex; + int yBottom = + colorRect.y + colorRect.height - colorRect.borderRadius + rowIndex; + + uint32_t* lineStartTop = &sPixelBuffer[yTop * sWindowWidth]; + uint32_t* lineStartBottom = &sPixelBuffer[yBottom * sWindowWidth]; + + // Add 0.5 to x and y to get the pixel center. + double pixelY = (double)rowIndex + 0.5; + for (int x = priorUpdateAreaMin; x < currentUpdateAreaMax; ++x) { + // The column index is the distance from the innermost pixel, which + // is different depending on whether we're on the left or right + // side of the rect. It will always be the max here, and if it's + // negative that just means we're outside the rounded area. + int columnIndex = + std::max((int)colorRect.x + (int)colorRect.borderRadius - x - 1, + x - ((int)colorRect.x + (int)colorRect.width - + (int)colorRect.borderRadius)); + + double alpha = 1.0; + if (columnIndex >= 0) { + double pixelX = (double)columnIndex + 0.5; + double distance = SignedDistanceToCircle( + pixelX, pixelY, (double)colorRect.borderRadius); + alpha = DistanceAntiAlias(distance); + } + // We don't do alpha blending for the antialiased pixels at the + // shape's border. It is not noticeable in the animation. + if (alpha > 1.0 - std::numeric_limits<double>::epsilon()) { + // Overwrite the tail end of last frame's animation with the + // rect's normal, unanimated color. + uint32_t color = x < priorUpdateAreaMax + ? colorRect.color + : animationLookup[x - animationMin]; + lineStartTop[x] = color; + lineStartBottom[x] = color; + } + } + } +} + +void RasterizeColorRect(const ColorRect& colorRect) { + // We sometimes split our rect into two, to simplify drawing borders. If we + // have a border, we draw a stroke-only rect first, and then draw the smaller + // inner rect on top of it. + Vector<DrawRect, 2> drawRects; + Unused << drawRects.reserve(2); + if (colorRect.borderWidth == 0) { + DrawRect rect = {}; + rect.color = colorRect.color; + rect.backgroundColor = + sPixelBuffer[colorRect.y * sWindowWidth + colorRect.x]; + rect.x = colorRect.x; + rect.y = colorRect.y; + rect.width = colorRect.width; + rect.height = colorRect.height; + rect.borderRadius = colorRect.borderRadius; + rect.strokeOnly = false; + drawRects.infallibleAppend(rect); + } else { + DrawRect borderRect = {}; + borderRect.color = colorRect.borderColor; + borderRect.backgroundColor = + sPixelBuffer[colorRect.y * sWindowWidth + colorRect.x]; + borderRect.x = colorRect.x; + borderRect.y = colorRect.y; + borderRect.width = colorRect.width; + borderRect.height = colorRect.height; + borderRect.borderRadius = colorRect.borderRadius; + borderRect.borderWidth = colorRect.borderWidth; + borderRect.strokeOnly = true; + drawRects.infallibleAppend(borderRect); + + DrawRect baseRect = {}; + baseRect.color = colorRect.color; + baseRect.backgroundColor = borderRect.color; + baseRect.x = colorRect.x + colorRect.borderWidth; + baseRect.y = colorRect.y + colorRect.borderWidth; + baseRect.width = colorRect.width - 2 * colorRect.borderWidth; + baseRect.height = colorRect.height - 2 * colorRect.borderWidth; + baseRect.borderRadius = + std::max(0, (int)colorRect.borderRadius - (int)colorRect.borderWidth); + baseRect.borderWidth = 0; + baseRect.strokeOnly = false; + drawRects.infallibleAppend(baseRect); + } + + for (const DrawRect& rect : drawRects) { + if (rect.height <= 0 || rect.width <= 0) { + continue; + } + + // For rounded rectangles, the first thing we do is draw the top and + // bottom of the rectangle, with the more complicated logic below. After + // that we can just draw the vertically centered part of the rect like + // normal. + RasterizeRoundedRectTopAndBottom(rect); + + // We then draw the flat, central portion of the rect (which in the case of + // non-rounded rects, is just the entire thing.) + int solidRectStartY = + std::clamp(rect.y + rect.borderRadius, 0, sTotalChromeHeight); + int solidRectEndY = std::clamp(rect.y + rect.height - rect.borderRadius, 0, + sTotalChromeHeight); + for (int y = solidRectStartY; y < solidRectEndY; ++y) { + // For strokeOnly rects (used to draw borders), we just draw the left + // and right side here. Looping down a column of pixels is not the most + // cache-friendly thing, but it shouldn't be a big deal given the height + // of the urlbar. + // Also, if borderRadius is less than borderWidth, we need to ensure + // that we fully draw the top and bottom lines, so we make sure to check + // that we're inside the middle range range before excluding pixels. + if (rect.strokeOnly && y - rect.y > rect.borderWidth && + rect.y + rect.height - y > rect.borderWidth) { + int startXLeft = std::clamp(rect.x, 0, sWindowWidth); + int endXLeft = std::clamp(rect.x + rect.borderWidth, 0, sWindowWidth); + int startXRight = + std::clamp(rect.x + rect.width - rect.borderWidth, 0, sWindowWidth); + int endXRight = std::clamp(rect.x + rect.width, 0, sWindowWidth); + + uint32_t* lineStart = &sPixelBuffer[y * sWindowWidth]; + uint32_t* dataStartLeft = lineStart + startXLeft; + uint32_t* dataEndLeft = lineStart + endXLeft; + uint32_t* dataStartRight = lineStart + startXRight; + uint32_t* dataEndRight = lineStart + endXRight; + std::fill(dataStartLeft, dataEndLeft, rect.color); + std::fill(dataStartRight, dataEndRight, rect.color); + } else { + int startX = std::clamp(rect.x, 0, sWindowWidth); + int endX = std::clamp(rect.x + rect.width, 0, sWindowWidth); + uint32_t* lineStart = &sPixelBuffer[y * sWindowWidth]; + uint32_t* dataStart = lineStart + startX; + uint32_t* dataEnd = lineStart + endX; + std::fill(dataStart, dataEnd, rect.color); + } + } + } +} + +// Paints the pixels to sPixelBuffer for the skeleton UI animation (a light +// gradient which moves from left to right across the grey placeholder rects). +// Takes in the rect to draw, together with a lookup table for the gradient, +// and the bounds of the previous and current frame of the animation. +bool RasterizeAnimatedRect(const ColorRect& colorRect, + const uint32_t* animationLookup, + int priorAnimationMin, int animationMin, + int animationMax) { + int rectMin = colorRect.x; + int rectMax = colorRect.x + colorRect.width; + bool animationWindowOverlaps = + rectMax >= priorAnimationMin && rectMin < animationMax; + + int priorUpdateAreaMin = std::max(rectMin, priorAnimationMin); + int priorUpdateAreaMax = std::min(rectMax, animationMin); + int currentUpdateAreaMin = std::max(rectMin, animationMin); + int currentUpdateAreaMax = std::min(rectMax, animationMax); + + if (!animationWindowOverlaps) { + return false; + } + + bool animationWindowOverlapsBorderRadius = + rectMin + colorRect.borderRadius > priorAnimationMin || + rectMax - colorRect.borderRadius <= animationMax; + + // If we don't overlap the left or right side of the rounded rectangle, + // just pretend it's not rounded. This is a small optimization but + // there's no point in doing all of this rounded rectangle checking if + // we aren't even overlapping + int borderRadius = + animationWindowOverlapsBorderRadius ? colorRect.borderRadius : 0; + + if (borderRadius > 0) { + // Similarly to how we draw the rounded rects in DrawSkeletonUI, we + // first draw the rounded top and bottom, and then we draw the center + // rect. + RasterizeAnimatedRoundedRectTopAndBottom( + colorRect, animationLookup, priorUpdateAreaMin, priorUpdateAreaMax, + currentUpdateAreaMin, currentUpdateAreaMax, animationMin); + } + + for (int y = colorRect.y + borderRadius; + y < colorRect.y + colorRect.height - borderRadius; ++y) { + uint32_t* lineStart = &sPixelBuffer[y * sWindowWidth]; + // Overwrite the tail end of last frame's animation with the rect's + // normal, unanimated color. + for (int x = priorUpdateAreaMin; x < priorUpdateAreaMax; ++x) { + lineStart[x] = colorRect.color; + } + // Then apply the animated color + for (int x = currentUpdateAreaMin; x < currentUpdateAreaMax; ++x) { + lineStart[x] = animationLookup[x - animationMin]; + } + } + + return true; +} + +Result<Ok, PreXULSkeletonUIError> DrawSkeletonUI( + HWND hWnd, CSSPixelSpan urlbarCSSSpan, CSSPixelSpan searchbarCSSSpan, + Vector<CSSPixelSpan>& springs, const ThemeColors& currentTheme, + const EnumSet<SkeletonUIFlag, uint32_t>& flags) { + // NOTE: we opt here to paint a pixel buffer for the application chrome by + // hand, without using native UI library methods. Why do we do this? + // + // 1) It gives us a little bit more control, especially if we want to animate + // any of this. + // 2) It's actually more portable. We can do this on any platform where we + // can blit a pixel buffer to the screen, and it only has to change + // insofar as the UI is different on those platforms (and thus would have + // to change anyway.) + // + // The performance impact of this ought to be negligible. As far as has been + // observed, on slow reference hardware this might take up to a millisecond, + // for a startup which otherwise takes 30 seconds. + // + // The readability and maintainability are a greater concern. When the + // silhouette of Firefox's core UI changes, this code will likely need to + // change. However, for the foreseeable future, our skeleton UI will be mostly + // axis-aligned geometric shapes, and the thought is that any code which is + // manipulating raw pixels should not be *too* hard to maintain and + // understand so long as it is only painting such simple shapes. + + sAnimationColor = currentTheme.animationColor; + sToolbarForegroundColor = currentTheme.toolbarForegroundColor; + + bool menubarShown = flags.contains(SkeletonUIFlag::MenubarShown); + bool bookmarksToolbarShown = + flags.contains(SkeletonUIFlag::BookmarksToolbarShown); + bool rtlEnabled = flags.contains(SkeletonUIFlag::RtlEnabled); + + int chromeHorMargin = CSSToDevPixels(2, sCSSToDevPixelScaling); + int verticalOffset = sMaximized ? sNonClientVerticalMargins : 0; + int horizontalOffset = + sNonClientHorizontalMargins - (sMaximized ? 0 : chromeHorMargin); + + // found in tabs.inc.css, "--tab-min-height" + 2 * "--tab-block-margin" + int tabBarHeight = CSSToDevPixels(44, sCSSToDevPixelScaling); + int selectedTabBorderWidth = CSSToDevPixels(2, sCSSToDevPixelScaling); + // found in tabs.inc.css, "--tab-block-margin" + int titlebarSpacerWidth = horizontalOffset + + CSSToDevPixels(2, sCSSToDevPixelScaling) - + selectedTabBorderWidth; + if (!sMaximized && !menubarShown) { + // found in tabs.inc.css, ".titlebar-spacer" + titlebarSpacerWidth += CSSToDevPixels(40, sCSSToDevPixelScaling); + } + // found in tabs.inc.css, "--tab-block-margin" + int selectedTabMarginTop = + CSSToDevPixels(4, sCSSToDevPixelScaling) - selectedTabBorderWidth; + int selectedTabMarginBottom = + CSSToDevPixels(4, sCSSToDevPixelScaling) - selectedTabBorderWidth; + int selectedTabBorderRadius = CSSToDevPixels(4, sCSSToDevPixelScaling); + int selectedTabWidth = + CSSToDevPixels(221, sCSSToDevPixelScaling) + 2 * selectedTabBorderWidth; + int toolbarHeight = CSSToDevPixels(40, sCSSToDevPixelScaling); + // found in browser.css, "#PersonalToolbar" + int bookmarkToolbarHeight = CSSToDevPixels(28, sCSSToDevPixelScaling); + if (bookmarksToolbarShown) { + toolbarHeight += bookmarkToolbarHeight; + } + // found in urlbar-searchbar.inc.css, "#urlbar[breakout]" + int urlbarTopOffset = CSSToDevPixels(4, sCSSToDevPixelScaling); + int urlbarHeight = CSSToDevPixels(32, sCSSToDevPixelScaling); + // found in browser-aero.css, "#navigator-toolbox::after" border-bottom + int chromeContentDividerHeight = CSSToDevPixels(1, sCSSToDevPixelScaling); + + int tabPlaceholderBarMarginTop = CSSToDevPixels(14, sCSSToDevPixelScaling); + int tabPlaceholderBarMarginLeft = CSSToDevPixels(10, sCSSToDevPixelScaling); + int tabPlaceholderBarHeight = CSSToDevPixels(10, sCSSToDevPixelScaling); + int tabPlaceholderBarWidth = CSSToDevPixels(120, sCSSToDevPixelScaling); + + int toolbarPlaceholderHeight = CSSToDevPixels(10, sCSSToDevPixelScaling); + int toolbarPlaceholderMarginRight = + rtlEnabled ? CSSToDevPixels(11, sCSSToDevPixelScaling) + : CSSToDevPixels(9, sCSSToDevPixelScaling); + int toolbarPlaceholderMarginLeft = + rtlEnabled ? CSSToDevPixels(9, sCSSToDevPixelScaling) + : CSSToDevPixels(11, sCSSToDevPixelScaling); + int placeholderMargin = CSSToDevPixels(8, sCSSToDevPixelScaling); + + int menubarHeightDevPixels = + menubarShown ? CSSToDevPixels(28, sCSSToDevPixelScaling) : 0; + + // defined in urlbar-searchbar.inc.css as --urlbar-margin-inline: 5px + int urlbarMargin = + CSSToDevPixels(5, sCSSToDevPixelScaling) + horizontalOffset; + + int urlbarTextPlaceholderMarginTop = + CSSToDevPixels(12, sCSSToDevPixelScaling); + int urlbarTextPlaceholderMarginLeft = + CSSToDevPixels(12, sCSSToDevPixelScaling); + int urlbarTextPlaceHolderWidth = CSSToDevPixels( + std::clamp(urlbarCSSSpan.end - urlbarCSSSpan.start - 10.0, 0.0, 260.0), + sCSSToDevPixelScaling); + int urlbarTextPlaceholderHeight = CSSToDevPixels(10, sCSSToDevPixelScaling); + + int searchbarTextPlaceholderWidth = CSSToDevPixels(62, sCSSToDevPixelScaling); + + auto scopeExit = MakeScopeExit([&] { + delete sAnimatedRects; + sAnimatedRects = nullptr; + }); + + Vector<ColorRect> rects; + + ColorRect menubar = {}; + menubar.color = currentTheme.tabBarColor; + menubar.x = 0; + menubar.y = verticalOffset; + menubar.width = sWindowWidth; + menubar.height = menubarHeightDevPixels; + menubar.flipIfRTL = false; + if (!rects.append(menubar)) { + return Err(PreXULSkeletonUIError::OOM); + } + + int placeholderBorderRadius = CSSToDevPixels(4, sCSSToDevPixelScaling); + // found in browser.css "--toolbarbutton-border-radius" + int urlbarBorderRadius = CSSToDevPixels(4, sCSSToDevPixelScaling); + + // The (traditionally dark blue on Windows) background of the tab bar. + ColorRect tabBar = {}; + tabBar.color = currentTheme.tabBarColor; + tabBar.x = 0; + tabBar.y = menubar.y + menubar.height; + tabBar.width = sWindowWidth; + tabBar.height = tabBarHeight; + tabBar.flipIfRTL = false; + if (!rects.append(tabBar)) { + return Err(PreXULSkeletonUIError::OOM); + } + + // The initial selected tab + ColorRect selectedTab = {}; + selectedTab.color = currentTheme.tabColor; + selectedTab.x = titlebarSpacerWidth; + selectedTab.y = menubar.y + menubar.height + selectedTabMarginTop; + selectedTab.width = selectedTabWidth; + selectedTab.height = + tabBar.y + tabBar.height - selectedTab.y - selectedTabMarginBottom; + selectedTab.borderColor = currentTheme.tabOutlineColor; + selectedTab.borderWidth = selectedTabBorderWidth; + selectedTab.borderRadius = selectedTabBorderRadius; + selectedTab.flipIfRTL = true; + if (!rects.append(selectedTab)) { + return Err(PreXULSkeletonUIError::OOM); + } + + // A placeholder rect representing text that will fill the selected tab title + ColorRect tabTextPlaceholder = {}; + tabTextPlaceholder.color = currentTheme.toolbarForegroundColor; + tabTextPlaceholder.x = selectedTab.x + tabPlaceholderBarMarginLeft; + tabTextPlaceholder.y = selectedTab.y + tabPlaceholderBarMarginTop; + tabTextPlaceholder.width = tabPlaceholderBarWidth; + tabTextPlaceholder.height = tabPlaceholderBarHeight; + tabTextPlaceholder.borderRadius = placeholderBorderRadius; + tabTextPlaceholder.flipIfRTL = true; + if (!rects.append(tabTextPlaceholder)) { + return Err(PreXULSkeletonUIError::OOM); + } + + // The toolbar background + ColorRect toolbar = {}; + toolbar.color = currentTheme.backgroundColor; + toolbar.x = 0; + toolbar.y = tabBar.y + tabBarHeight; + toolbar.width = sWindowWidth; + toolbar.height = toolbarHeight; + toolbar.flipIfRTL = false; + if (!rects.append(toolbar)) { + return Err(PreXULSkeletonUIError::OOM); + } + + // The single-pixel divider line below the toolbar + ColorRect chromeContentDivider = {}; + chromeContentDivider.color = currentTheme.chromeContentDividerColor; + chromeContentDivider.x = 0; + chromeContentDivider.y = toolbar.y + toolbar.height; + chromeContentDivider.width = sWindowWidth; + chromeContentDivider.height = chromeContentDividerHeight; + chromeContentDivider.flipIfRTL = false; + if (!rects.append(chromeContentDivider)) { + return Err(PreXULSkeletonUIError::OOM); + } + + // The urlbar + ColorRect urlbar = {}; + urlbar.color = currentTheme.urlbarColor; + urlbar.x = CSSToDevPixels(urlbarCSSSpan.start, sCSSToDevPixelScaling) + + horizontalOffset; + urlbar.y = tabBar.y + tabBarHeight + urlbarTopOffset; + urlbar.width = CSSToDevPixels((urlbarCSSSpan.end - urlbarCSSSpan.start), + sCSSToDevPixelScaling); + urlbar.height = urlbarHeight; + urlbar.borderColor = currentTheme.urlbarBorderColor; + urlbar.borderWidth = CSSToDevPixels(1, sCSSToDevPixelScaling); + urlbar.borderRadius = urlbarBorderRadius; + urlbar.flipIfRTL = false; + if (!rects.append(urlbar)) { + return Err(PreXULSkeletonUIError::OOM); + } + + // The urlbar placeholder rect representating text that will fill the urlbar + // If rtl is enabled, it is flipped relative to the the urlbar rectangle, not + // sWindowWidth. + ColorRect urlbarTextPlaceholder = {}; + urlbarTextPlaceholder.color = currentTheme.toolbarForegroundColor; + urlbarTextPlaceholder.x = + rtlEnabled + ? ((urlbar.x + urlbar.width) - urlbarTextPlaceholderMarginLeft - + urlbarTextPlaceHolderWidth) + : (urlbar.x + urlbarTextPlaceholderMarginLeft); + urlbarTextPlaceholder.y = urlbar.y + urlbarTextPlaceholderMarginTop; + urlbarTextPlaceholder.width = urlbarTextPlaceHolderWidth; + urlbarTextPlaceholder.height = urlbarTextPlaceholderHeight; + urlbarTextPlaceholder.borderRadius = placeholderBorderRadius; + urlbarTextPlaceholder.flipIfRTL = false; + if (!rects.append(urlbarTextPlaceholder)) { + return Err(PreXULSkeletonUIError::OOM); + } + + // The searchbar and placeholder text, if present + // This is y-aligned with the urlbar + bool hasSearchbar = searchbarCSSSpan.start != 0 && searchbarCSSSpan.end != 0; + ColorRect searchbarRect = {}; + if (hasSearchbar == true) { + searchbarRect.color = currentTheme.urlbarColor; + searchbarRect.x = + CSSToDevPixels(searchbarCSSSpan.start, sCSSToDevPixelScaling) + + horizontalOffset; + searchbarRect.y = urlbar.y; + searchbarRect.width = CSSToDevPixels( + searchbarCSSSpan.end - searchbarCSSSpan.start, sCSSToDevPixelScaling); + searchbarRect.height = urlbarHeight; + searchbarRect.borderRadius = urlbarBorderRadius; + searchbarRect.borderColor = currentTheme.urlbarBorderColor; + searchbarRect.borderWidth = CSSToDevPixels(1, sCSSToDevPixelScaling); + searchbarRect.flipIfRTL = false; + if (!rects.append(searchbarRect)) { + return Err(PreXULSkeletonUIError::OOM); + } + + // The placeholder rect representating text that will fill the searchbar + // This uses the same margins as the urlbarTextPlaceholder + // If rtl is enabled, it is flipped relative to the the searchbar rectangle, + // not sWindowWidth. + ColorRect searchbarTextPlaceholder = {}; + searchbarTextPlaceholder.color = currentTheme.toolbarForegroundColor; + searchbarTextPlaceholder.x = + rtlEnabled + ? ((searchbarRect.x + searchbarRect.width) - + urlbarTextPlaceholderMarginLeft - searchbarTextPlaceholderWidth) + : (searchbarRect.x + urlbarTextPlaceholderMarginLeft); + searchbarTextPlaceholder.y = + searchbarRect.y + urlbarTextPlaceholderMarginTop; + searchbarTextPlaceholder.width = searchbarTextPlaceholderWidth; + searchbarTextPlaceholder.height = urlbarTextPlaceholderHeight; + searchbarTextPlaceholder.flipIfRTL = false; + if (!rects.append(searchbarTextPlaceholder) || + !sAnimatedRects->append(searchbarTextPlaceholder)) { + return Err(PreXULSkeletonUIError::OOM); + } + } + + // Determine where the placeholder rectangles should not go. This is + // anywhere occupied by a spring, urlbar, or searchbar + Vector<DevPixelSpan> noPlaceholderSpans; + + DevPixelSpan urlbarSpan; + urlbarSpan.start = urlbar.x - urlbarMargin; + urlbarSpan.end = urlbar.width + urlbar.x + urlbarMargin; + + DevPixelSpan searchbarSpan; + if (hasSearchbar) { + searchbarSpan.start = searchbarRect.x - urlbarMargin; + searchbarSpan.end = searchbarRect.width + searchbarRect.x + urlbarMargin; + } + + DevPixelSpan marginLeftPlaceholder; + marginLeftPlaceholder.start = toolbarPlaceholderMarginLeft; + marginLeftPlaceholder.end = toolbarPlaceholderMarginLeft; + if (!noPlaceholderSpans.append(marginLeftPlaceholder)) { + return Err(PreXULSkeletonUIError::OOM); + } + + if (rtlEnabled) { + // If we're RTL, then the springs as ordered in the DOM will be from right + // to left, which will break our comparison logic below + springs.reverse(); + } + + for (auto spring : springs) { + DevPixelSpan springDevPixels; + springDevPixels.start = + CSSToDevPixels(spring.start, sCSSToDevPixelScaling) + horizontalOffset; + springDevPixels.end = + CSSToDevPixels(spring.end, sCSSToDevPixelScaling) + horizontalOffset; + if (!noPlaceholderSpans.append(springDevPixels)) { + return Err(PreXULSkeletonUIError::OOM); + } + } + + DevPixelSpan marginRightPlaceholder; + marginRightPlaceholder.start = sWindowWidth - toolbarPlaceholderMarginRight; + marginRightPlaceholder.end = sWindowWidth - toolbarPlaceholderMarginRight; + if (!noPlaceholderSpans.append(marginRightPlaceholder)) { + return Err(PreXULSkeletonUIError::OOM); + } + + Vector<DevPixelSpan, 2> spansToAdd; + Unused << spansToAdd.reserve(2); + spansToAdd.infallibleAppend(urlbarSpan); + if (hasSearchbar) { + spansToAdd.infallibleAppend(searchbarSpan); + } + + for (auto& toAdd : spansToAdd) { + for (auto& span : noPlaceholderSpans) { + if (span.start > toAdd.start) { + if (!noPlaceholderSpans.insert(&span, toAdd)) { + return Err(PreXULSkeletonUIError::OOM); + } + break; + } + } + } + + for (size_t i = 1; i < noPlaceholderSpans.length(); i++) { + int start = noPlaceholderSpans[i - 1].end + placeholderMargin; + int end = noPlaceholderSpans[i].start - placeholderMargin; + if (start + 2 * placeholderBorderRadius >= end) { + continue; + } + + // The placeholder rects should all be y-aligned. + ColorRect placeholderRect = {}; + placeholderRect.color = currentTheme.toolbarForegroundColor; + placeholderRect.x = start; + placeholderRect.y = urlbarTextPlaceholder.y; + placeholderRect.width = end - start; + placeholderRect.height = toolbarPlaceholderHeight; + placeholderRect.borderRadius = placeholderBorderRadius; + placeholderRect.flipIfRTL = false; + if (!rects.append(placeholderRect) || + !sAnimatedRects->append(placeholderRect)) { + return Err(PreXULSkeletonUIError::OOM); + } + } + + sTotalChromeHeight = chromeContentDivider.y + chromeContentDivider.height; + if (sTotalChromeHeight > sWindowHeight) { + return Err(PreXULSkeletonUIError::BadWindowDimensions); + } + + if (!sAnimatedRects->append(tabTextPlaceholder) || + !sAnimatedRects->append(urlbarTextPlaceholder)) { + return Err(PreXULSkeletonUIError::OOM); + } + + sPixelBuffer = + (uint32_t*)calloc(sWindowWidth * sTotalChromeHeight, sizeof(uint32_t)); + + for (auto& rect : *sAnimatedRects) { + if (rtlEnabled && rect.flipIfRTL) { + rect.x = sWindowWidth - rect.x - rect.width; + } + rect.x = std::clamp(rect.x, 0, sWindowWidth); + rect.width = std::clamp(rect.width, 0, sWindowWidth - rect.x); + rect.y = std::clamp(rect.y, 0, sTotalChromeHeight); + rect.height = std::clamp(rect.height, 0, sTotalChromeHeight - rect.y); + } + + for (auto& rect : rects) { + if (rtlEnabled && rect.flipIfRTL) { + rect.x = sWindowWidth - rect.x - rect.width; + } + rect.x = std::clamp(rect.x, 0, sWindowWidth); + rect.width = std::clamp(rect.width, 0, sWindowWidth - rect.x); + rect.y = std::clamp(rect.y, 0, sTotalChromeHeight); + rect.height = std::clamp(rect.height, 0, sTotalChromeHeight - rect.y); + RasterizeColorRect(rect); + } + + HDC hdc = sGetWindowDC(hWnd); + if (!hdc) { + return Err(PreXULSkeletonUIError::FailedGettingDC); + } + auto cleanupDC = MakeScopeExit([=] { sReleaseDC(hWnd, hdc); }); + + BITMAPINFO chromeBMI = {}; + chromeBMI.bmiHeader.biSize = sizeof(chromeBMI.bmiHeader); + chromeBMI.bmiHeader.biWidth = sWindowWidth; + chromeBMI.bmiHeader.biHeight = -sTotalChromeHeight; + chromeBMI.bmiHeader.biPlanes = 1; + chromeBMI.bmiHeader.biBitCount = 32; + chromeBMI.bmiHeader.biCompression = BI_RGB; + + // First, we just paint the chrome area with our pixel buffer + int scanLinesCopied = sStretchDIBits( + hdc, 0, 0, sWindowWidth, sTotalChromeHeight, 0, 0, sWindowWidth, + sTotalChromeHeight, sPixelBuffer, &chromeBMI, DIB_RGB_COLORS, SRCCOPY); + if (scanLinesCopied == 0) { + return Err(PreXULSkeletonUIError::FailedBlitting); + } + + // Then, we just fill the rest with FillRect + RECT rect = {0, sTotalChromeHeight, sWindowWidth, sWindowHeight}; + HBRUSH brush = + sCreateSolidBrush(RGB((currentTheme.backgroundColor & 0xff0000) >> 16, + (currentTheme.backgroundColor & 0x00ff00) >> 8, + (currentTheme.backgroundColor & 0x0000ff) >> 0)); + int fillRectResult = sFillRect(hdc, &rect, brush); + + sDeleteObject(brush); + + if (fillRectResult == 0) { + return Err(PreXULSkeletonUIError::FailedFillingBottomRect); + } + + scopeExit.release(); + return Ok(); +} + +DWORD WINAPI AnimateSkeletonUI(void* aUnused) { + if (!sPixelBuffer || sAnimatedRects->empty()) { + return 0; + } + + // See the comments above the InterlockedIncrement calls below here - we + // atomically flip this up and down around sleep so the main thread doesn't + // have to wait for us if we're just sleeping. + if (InterlockedIncrement(&sAnimationControlFlag) != 1) { + return 0; + } + // Sleep for two seconds - startups faster than this don't really benefit + // from an animation, and we don't want to take away cycles from them. + // Startups longer than this, however, are more likely to be blocked on IO, + // and thus animating does not substantially impact startup times for them. + ::Sleep(2000); + if (InterlockedDecrement(&sAnimationControlFlag) != 0) { + return 0; + } + + // On each of the animated rects (which happen to all be placeholder UI + // rects sharing the same color), we want to animate a gradient moving across + // the screen from left to right. The gradient starts as the rect's color on, + // the left side, changes to the background color of the window by the middle + // of the gradient, and then goes back down to the rect's color. To make this + // faster than interpolating between the two colors for each pixel for each + // frame, we simply create a lookup buffer in which we can look up the color + // for a particular offset into the gradient. + // + // To do this we just interpolate between the two values, and to give the + // gradient a smoother transition between colors, we transform the linear + // blend amount via the cubic smooth step function (SmoothStep3) to produce + // a smooth start and stop for the gradient. We do this for the first half + // of the gradient, and then simply copy that backwards for the second half. + // + // The CSS width of 80 chosen here is effectively is just to match the size + // of the animation provided in the design mockup. We define it in CSS pixels + // simply because the rest of our UI is based off of CSS scalings. + int animationWidth = CSSToDevPixels(80, sCSSToDevPixelScaling); + UniquePtr<uint32_t[]> animationLookup = + MakeUnique<uint32_t[]>(animationWidth); + uint32_t animationColor = sAnimationColor; + NormalizedRGB rgbBlend = UintToRGB(animationColor); + + // Build the first half of the lookup table + for (int i = 0; i < animationWidth / 2; ++i) { + uint32_t baseColor = sToolbarForegroundColor; + double blendAmountLinear = + static_cast<double>(i) / (static_cast<double>(animationWidth / 2)); + double blendAmount = SmoothStep3(blendAmountLinear); + + NormalizedRGB rgbBase = UintToRGB(baseColor); + NormalizedRGB rgb = Lerp(rgbBase, rgbBlend, blendAmount); + animationLookup[i] = RGBToUint(rgb); + } + + // Copy the first half of the lookup table into the second half backwards + for (int i = animationWidth / 2; i < animationWidth; ++i) { + int j = animationWidth - 1 - i; + if (j == animationWidth / 2) { + // If animationWidth is odd, we'll be left with one pixel at the center. + // Just color that as the animation color. + animationLookup[i] = animationColor; + } else { + animationLookup[i] = animationLookup[j]; + } + } + + // The bitmap info remains unchanged throughout the animation - this just + // effectively describes the contents of sPixelBuffer + BITMAPINFO chromeBMI = {}; + chromeBMI.bmiHeader.biSize = sizeof(chromeBMI.bmiHeader); + chromeBMI.bmiHeader.biWidth = sWindowWidth; + chromeBMI.bmiHeader.biHeight = -sTotalChromeHeight; + chromeBMI.bmiHeader.biPlanes = 1; + chromeBMI.bmiHeader.biBitCount = 32; + chromeBMI.bmiHeader.biCompression = BI_RGB; + + uint32_t animationIteration = 0; + + int devPixelsPerFrame = + CSSToDevPixels(kAnimationCSSPixelsPerFrame, sCSSToDevPixelScaling); + int devPixelsExtraWindowSize = + CSSToDevPixels(kAnimationCSSExtraWindowSize, sCSSToDevPixelScaling); + + if (::InterlockedCompareExchange(&sAnimationControlFlag, 0, 0)) { + // The window got consumed before we were able to draw anything. + return 0; + } + + while (true) { + // The gradient will move across the screen at devPixelsPerFrame at + // 60fps, and then loop back to the beginning. However, we add a buffer of + // devPixelsExtraWindowSize around the edges so it doesn't immediately + // jump back, giving it a more pulsing feel. + int animationMin = ((animationIteration * devPixelsPerFrame) % + (sWindowWidth + devPixelsExtraWindowSize)) - + devPixelsExtraWindowSize / 2; + int animationMax = animationMin + animationWidth; + // The priorAnimationMin is the beginning of the previous frame's animation. + // Since we only want to draw the bits of the image that we updated, we need + // to overwrite the left bit of the animation we drew last frame with the + // default color. + int priorAnimationMin = animationMin - devPixelsPerFrame; + animationMin = std::max(0, animationMin); + priorAnimationMin = std::max(0, priorAnimationMin); + animationMax = std::min((int)sWindowWidth, animationMax); + + // The gradient only affects the specific rects that we put into + // sAnimatedRects. So we simply update those rects, and maintain a flag + // to avoid drawing when we don't need to. + bool updatedAnything = false; + for (ColorRect rect : *sAnimatedRects) { + bool hadUpdates = + RasterizeAnimatedRect(rect, animationLookup.get(), priorAnimationMin, + animationMin, animationMax); + updatedAnything = updatedAnything || hadUpdates; + } + + if (updatedAnything) { + HDC hdc = sGetWindowDC(sPreXULSkeletonUIWindow); + if (!hdc) { + return 0; + } + + sStretchDIBits(hdc, priorAnimationMin, 0, + animationMax - priorAnimationMin, sTotalChromeHeight, + priorAnimationMin, 0, animationMax - priorAnimationMin, + sTotalChromeHeight, sPixelBuffer, &chromeBMI, + DIB_RGB_COLORS, SRCCOPY); + + sReleaseDC(sPreXULSkeletonUIWindow, hdc); + } + + animationIteration++; + + // We coordinate around our sleep here to ensure that the main thread does + // not wait on us if we're sleeping. If we don't get 1 here, it means the + // window has been consumed and we don't need to sleep. If in + // ConsumePreXULSkeletonUIHandle we get a value other than 1 after + // incrementing, it means we're sleeping, and that function can assume that + // we will safely exit after the sleep because of the observed value of + // sAnimationControlFlag. + if (InterlockedIncrement(&sAnimationControlFlag) != 1) { + return 0; + } + + // Note: Sleep does not guarantee an exact time interval. If the system is + // busy, for instance, we could easily end up taking several frames longer, + // and really we could be left unscheduled for an arbitrarily long time. + // This is fine, and we don't really care. We could track how much time this + // actually took and jump the animation forward the appropriate amount, but + // its not even clear that that's a better user experience. So we leave this + // as simple as we can. + ::Sleep(16); + + // Here we bring sAnimationControlFlag back down - again, if we don't get a + // 0 here it means we consumed the skeleton UI window in the mean time, so + // we can simply exit. + if (InterlockedDecrement(&sAnimationControlFlag) != 0) { + return 0; + } + } +} + +LRESULT WINAPI PreXULSkeletonUIProc(HWND hWnd, UINT msg, WPARAM wParam, + LPARAM lParam) { + // Exposing a generic oleacc proxy for the skeleton isn't useful and may cause + // screen readers to report spurious information when the skeleton appears. + if (msg == WM_GETOBJECT && sPreXULSkeletonUIWindow) { + return E_FAIL; + } + + // NOTE: this block was copied from WinUtils.cpp, and needs to be kept in + // sync. + if (msg == WM_NCCREATE && sEnableNonClientDpiScaling) { + sEnableNonClientDpiScaling(hWnd); + } + + // NOTE: this block was paraphrased from the WM_NCCALCSIZE handler in + // nsWindow.cpp, and will need to be kept in sync. + if (msg == WM_NCCALCSIZE) { + RECT* clientRect = + wParam ? &(reinterpret_cast<NCCALCSIZE_PARAMS*>(lParam))->rgrc[0] + : (reinterpret_cast<RECT*>(lParam)); + + // These match the margins set in browser-tabsintitlebar.js with + // default prefs on Windows. Bug 1673092 tracks lining this up with + // that more correctly instead of hard-coding it. + int horizontalOffset = + sNonClientHorizontalMargins - + (sMaximized ? 0 : CSSToDevPixels(2, sCSSToDevPixelScaling)); + int verticalOffset = + sNonClientHorizontalMargins - + (sMaximized ? 0 : CSSToDevPixels(2, sCSSToDevPixelScaling)); + clientRect->top = clientRect->top; + clientRect->left += horizontalOffset; + clientRect->right -= horizontalOffset; + clientRect->bottom -= verticalOffset; + return 0; + } + + return ::DefWindowProcW(hWnd, msg, wParam, lParam); +} + +bool IsSystemDarkThemeEnabled() { + DWORD result; + HKEY themeKey; + DWORD dataLen = sizeof(uint32_t); + LPCWSTR keyName = + L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize"; + + result = ::RegOpenKeyExW(HKEY_CURRENT_USER, keyName, 0, KEY_READ, &themeKey); + if (result != ERROR_SUCCESS) { + return false; + } + AutoCloseRegKey closeKey(themeKey); + + uint32_t lightThemeEnabled; + result = ::RegGetValueW( + themeKey, nullptr, L"AppsUseLightTheme", RRF_RT_REG_DWORD, nullptr, + reinterpret_cast<PBYTE>(&lightThemeEnabled), &dataLen); + if (result != ERROR_SUCCESS) { + return false; + } + return !lightThemeEnabled; +} + +ThemeColors GetTheme(ThemeMode themeId) { + ThemeColors theme = {}; + switch (themeId) { + case ThemeMode::Dark: + // Dark theme or default theme when in dark mode + + // controlled by css variable --toolbar-bgcolor + theme.backgroundColor = 0x2b2a33; + theme.tabColor = 0x42414d; + theme.toolbarForegroundColor = 0x6a6a6d; + theme.tabOutlineColor = 0x1c1b22; + // controlled by css variable --lwt-accent-color + theme.tabBarColor = 0x1c1b22; + // controlled by --toolbar-non-lwt-textcolor in browser.css + theme.chromeContentDividerColor = 0x0c0c0d; + // controlled by css variable --toolbar-field-background-color + theme.urlbarColor = 0x42414d; + theme.urlbarBorderColor = 0x42414d; + theme.animationColor = theme.urlbarColor; + return theme; + case ThemeMode::Light: + case ThemeMode::Default: + default: + // --toolbar-non-lwt-bgcolor in browser.css + theme.backgroundColor = 0xf9f9fb; + theme.tabColor = 0xf9f9fb; + theme.toolbarForegroundColor = 0xdddde1; + theme.tabOutlineColor = 0xdddde1; + // found in browser-aero.css ":root[tabsintitlebar]:not(:-moz-lwtheme)" + // (set to "hsl(235,33%,19%)") + theme.tabBarColor = 0xf0f0f4; + // --chrome-content-separator-color in browser.css + theme.chromeContentDividerColor = 0xe1e1e2; + // controlled by css variable --toolbar-color + theme.urlbarColor = 0xffffff; + theme.urlbarBorderColor = 0xdddde1; + theme.animationColor = theme.backgroundColor; + return theme; + } +} + +Result<HKEY, PreXULSkeletonUIError> OpenPreXULSkeletonUIRegKey() { + HKEY key; + DWORD disposition; + LSTATUS result = + ::RegCreateKeyExW(HKEY_CURRENT_USER, kPreXULSkeletonUIKeyPath, 0, nullptr, + 0, KEY_ALL_ACCESS, nullptr, &key, &disposition); + + if (result != ERROR_SUCCESS) { + return Err(PreXULSkeletonUIError::FailedToOpenRegistryKey); + } + + if (disposition == REG_CREATED_NEW_KEY || + disposition == REG_OPENED_EXISTING_KEY) { + return key; + } + + ::RegCloseKey(key); + return Err(PreXULSkeletonUIError::FailedToOpenRegistryKey); +} + +Result<Ok, PreXULSkeletonUIError> LoadGdi32AndUser32Procedures() { + HMODULE user32Dll = ::LoadLibraryW(L"user32"); + HMODULE gdi32Dll = ::LoadLibraryW(L"gdi32"); + + if (!user32Dll || !gdi32Dll) { + return Err(PreXULSkeletonUIError::FailedLoadingDynamicProcs); + } + + auto getThreadDpiAwarenessContext = + (decltype(GetThreadDpiAwarenessContext)*)::GetProcAddress( + user32Dll, "GetThreadDpiAwarenessContext"); + auto areDpiAwarenessContextsEqual = + (decltype(AreDpiAwarenessContextsEqual)*)::GetProcAddress( + user32Dll, "AreDpiAwarenessContextsEqual"); + if (getThreadDpiAwarenessContext && areDpiAwarenessContextsEqual && + areDpiAwarenessContextsEqual(getThreadDpiAwarenessContext(), + DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE)) { + // EnableNonClientDpiScaling is optional - we can handle not having it. + sEnableNonClientDpiScaling = + (EnableNonClientDpiScalingProc)::GetProcAddress( + user32Dll, "EnableNonClientDpiScaling"); + } + + sGetSystemMetricsForDpi = (GetSystemMetricsForDpiProc)::GetProcAddress( + user32Dll, "GetSystemMetricsForDpi"); + if (!sGetSystemMetricsForDpi) { + return Err(PreXULSkeletonUIError::FailedLoadingDynamicProcs); + } + sGetDpiForWindow = + (GetDpiForWindowProc)::GetProcAddress(user32Dll, "GetDpiForWindow"); + if (!sGetDpiForWindow) { + return Err(PreXULSkeletonUIError::FailedLoadingDynamicProcs); + } + sRegisterClassW = + (RegisterClassWProc)::GetProcAddress(user32Dll, "RegisterClassW"); + if (!sRegisterClassW) { + return Err(PreXULSkeletonUIError::FailedLoadingDynamicProcs); + } + sCreateWindowExW = + (CreateWindowExWProc)::GetProcAddress(user32Dll, "CreateWindowExW"); + if (!sCreateWindowExW) { + return Err(PreXULSkeletonUIError::FailedLoadingDynamicProcs); + } + sShowWindow = (ShowWindowProc)::GetProcAddress(user32Dll, "ShowWindow"); + if (!sShowWindow) { + return Err(PreXULSkeletonUIError::FailedLoadingDynamicProcs); + } + sSetWindowPos = (SetWindowPosProc)::GetProcAddress(user32Dll, "SetWindowPos"); + if (!sSetWindowPos) { + return Err(PreXULSkeletonUIError::FailedLoadingDynamicProcs); + } + sGetWindowDC = (GetWindowDCProc)::GetProcAddress(user32Dll, "GetWindowDC"); + if (!sGetWindowDC) { + return Err(PreXULSkeletonUIError::FailedLoadingDynamicProcs); + } + sFillRect = (FillRectProc)::GetProcAddress(user32Dll, "FillRect"); + if (!sFillRect) { + return Err(PreXULSkeletonUIError::FailedLoadingDynamicProcs); + } + sReleaseDC = (ReleaseDCProc)::GetProcAddress(user32Dll, "ReleaseDC"); + if (!sReleaseDC) { + return Err(PreXULSkeletonUIError::FailedLoadingDynamicProcs); + } + sLoadIconW = (LoadIconWProc)::GetProcAddress(user32Dll, "LoadIconW"); + if (!sLoadIconW) { + return Err(PreXULSkeletonUIError::FailedLoadingDynamicProcs); + } + sLoadCursorW = (LoadCursorWProc)::GetProcAddress(user32Dll, "LoadCursorW"); + if (!sLoadCursorW) { + return Err(PreXULSkeletonUIError::FailedLoadingDynamicProcs); + } + sMonitorFromWindow = + (MonitorFromWindowProc)::GetProcAddress(user32Dll, "MonitorFromWindow"); + if (!sMonitorFromWindow) { + return Err(PreXULSkeletonUIError::FailedLoadingDynamicProcs); + } + sGetMonitorInfoW = + (GetMonitorInfoWProc)::GetProcAddress(user32Dll, "GetMonitorInfoW"); + if (!sGetMonitorInfoW) { + return Err(PreXULSkeletonUIError::FailedLoadingDynamicProcs); + } + sSetWindowLongPtrW = + (SetWindowLongPtrWProc)::GetProcAddress(user32Dll, "SetWindowLongPtrW"); + if (!sSetWindowLongPtrW) { + return Err(PreXULSkeletonUIError::FailedLoadingDynamicProcs); + } + sStretchDIBits = + (StretchDIBitsProc)::GetProcAddress(gdi32Dll, "StretchDIBits"); + if (!sStretchDIBits) { + return Err(PreXULSkeletonUIError::FailedLoadingDynamicProcs); + } + sCreateSolidBrush = + (CreateSolidBrushProc)::GetProcAddress(gdi32Dll, "CreateSolidBrush"); + if (!sCreateSolidBrush) { + return Err(PreXULSkeletonUIError::FailedLoadingDynamicProcs); + } + sDeleteObject = (DeleteObjectProc)::GetProcAddress(gdi32Dll, "DeleteObject"); + if (!sDeleteObject) { + return Err(PreXULSkeletonUIError::FailedLoadingDynamicProcs); + } + + return Ok(); +} + +// Strips "--", "-", and "/" from the front of the arg if one of those exists, +// returning `arg + 2`, `arg + 1`, and `arg + 1` respectively. If none of these +// prefixes are found, the argument is not a flag, and nullptr is returned. +const char* NormalizeFlag(const char* arg) { + if (strstr(arg, "--") == arg) { + return arg + 2; + } + + if (arg[0] == '-') { + return arg + 1; + } + + if (arg[0] == '/') { + return arg + 1; + } + + return nullptr; +} + +static bool EnvHasValue(const char* name) { + const char* val = getenv(name); + return (val && *val); +} + +// Ensures that we only see arguments in the command line which are acceptable. +// This is based on manual inspection of the list of arguments listed in the MDN +// page for Gecko/Firefox commandline options: +// https://developer.mozilla.org/en-US/docs/Mozilla/Command_Line_Options +// Broadly speaking, we want to reject any argument which causes us to show +// something other than the default window at its normal size. Here is a non- +// exhaustive list of command line options we want to *exclude*: +// +// -ProfileManager : This will display the profile manager window, which does +// not match the skeleton UI at all. +// +// -CreateProfile : This will display a firefox window with the default +// screen position and size, and not the position and size +// which we have recorded in the registry. +// +// -P <profile> : This could cause us to display firefox with a position +// and size of a different profile than that in which we +// were previously running. +// +// -width, -height : This will cause the width and height values in the +// registry to be incorrect. +// +// -kiosk : See above. +// +// -headless : This one should be rather obvious. +// +// -migration : This will start with the import wizard, which of course +// does not match the skeleton UI. +// +// -private-window : This is tricky, but the colors of the main content area +// make this not feel great with the white content of the +// default skeleton UI. +// +// NOTE: we generally want to skew towards erroneous rejections of the command +// line rather than erroneous approvals. The consequence of a bad rejection +// is that we don't show the skeleton UI, which is business as usual. The +// consequence of a bad approval is that we show it when we're not supposed to, +// which is visually jarring and can also be unpredictable - there's no +// guarantee that the code which handles the non-default window is set up to +// properly handle the transition from the skeleton UI window. +static Result<Ok, PreXULSkeletonUIError> ValidateCmdlineArguments( + int argc, char** argv, bool* explicitProfile) { + const char* approvedArgumentsArray[] = { + // These won't cause the browser to be visualy different in any way + "new-instance", "no-remote", "browser", "foreground", "setDefaultBrowser", + "attach-console", "wait-for-browser", "osint", + + // These will cause the chrome to be a bit different or extra windows to + // be created, but overall the skeleton UI should still be broadly + // correct enough. + "new-tab", "new-window", + + // To the extent possible, we want to ensure that existing tests cover + // the skeleton UI, so we need to allow marionette + "marionette", + + // These will cause the content area to appear different, but won't + // meaningfully affect the chrome + "preferences", "search", "url", + +#ifndef MOZILLA_OFFICIAL + // On local builds, we want to allow -profile, because it's how `mach run` + // operates, and excluding that would create an unnecessary blind spot for + // Firefox devs. + "profile" +#endif + + // There are other arguments which are likely okay. However, they are + // not included here because this list is not intended to be + // exhaustive - it only intends to green-light some somewhat commonly + // used arguments. We want to err on the side of an unnecessary + // rejection of the command line. + }; + + int approvedArgumentsArraySize = + sizeof(approvedArgumentsArray) / sizeof(approvedArgumentsArray[0]); + Vector<const char*> approvedArguments; + if (!approvedArguments.reserve(approvedArgumentsArraySize)) { + return Err(PreXULSkeletonUIError::OOM); + } + + for (int i = 0; i < approvedArgumentsArraySize; ++i) { + approvedArguments.infallibleAppend(approvedArgumentsArray[i]); + } + +#ifdef MOZILLA_OFFICIAL + int profileArgIndex = -1; + // If we're running mochitests or direct marionette tests, those specify a + // temporary profile, and we want to ensure that we get the added coverage + // from those. + for (int i = 1; i < argc; ++i) { + const char* flag = NormalizeFlag(argv[i]); + if (flag && !strcmp(flag, "marionette")) { + if (!approvedArguments.append("profile")) { + return Err(PreXULSkeletonUIError::OOM); + } + profileArgIndex = approvedArguments.length() - 1; + + break; + } + } +#else + int profileArgIndex = approvedArguments.length() - 1; +#endif + + for (int i = 1; i < argc; ++i) { + const char* flag = NormalizeFlag(argv[i]); + if (!flag) { + // If this is not a flag, then we interpret it as a URL, similar to + // BrowserContentHandler.sys.mjs. Some command line options take + // additional arguments, which may or may not be URLs. We don't need to + // know this, because we don't need to parse them out; we just rely on the + // assumption that if arg X is actually a parameter for the preceding + // arg Y, then X must not look like a flag (starting with "--", "-", + // or "/"). + // + // The most important thing here is the assumption that if something is + // going to meaningfully alter the appearance of the window itself, it + // must be a flag. + continue; + } + + bool approved = false; + for (const char* approvedArg : approvedArguments) { + // We do a case-insensitive compare here with _stricmp. Even though some + // of these arguments are *not* read as case-insensitive, others *are*. + // Similar to the flag logic above, we don't really care about this + // distinction, because we don't need to parse the arguments - we just + // rely on the assumption that none of the listed flags in our + // approvedArguments are overloaded in such a way that a different + // casing would visually alter the firefox window. + if (!_stricmp(flag, approvedArg)) { + approved = true; + + if (i == profileArgIndex) { + *explicitProfile = true; + } + break; + } + } + + if (!approved) { + return Err(PreXULSkeletonUIError::Cmdline); + } + } + + return Ok(); +} + +static Result<Ok, PreXULSkeletonUIError> ValidateEnvVars() { + if (EnvHasValue("MOZ_SAFE_MODE_RESTART") || + EnvHasValue("MOZ_APP_SILENT_START") || + EnvHasValue("MOZ_RESET_PROFILE_RESTART") || EnvHasValue("MOZ_HEADLESS") || + (EnvHasValue("XRE_PROFILE_PATH") && + !EnvHasValue("MOZ_SKELETON_UI_RESTARTING"))) { + return Err(PreXULSkeletonUIError::EnvVars); + } + + return Ok(); +} + +static bool VerifyWindowDimensions(uint32_t windowWidth, + uint32_t windowHeight) { + return windowWidth <= kMaxWindowWidth && windowHeight <= kMaxWindowHeight; +} + +static Result<Vector<CSSPixelSpan>, PreXULSkeletonUIError> ReadRegCSSPixelSpans( + HKEY regKey, const std::wstring& valueName) { + DWORD dataLen = 0; + LSTATUS result = ::RegQueryValueExW(regKey, valueName.c_str(), nullptr, + nullptr, nullptr, &dataLen); + if (result != ERROR_SUCCESS) { + return Err(PreXULSkeletonUIError::RegistryError); + } + + if (dataLen % (2 * sizeof(double)) != 0) { + return Err(PreXULSkeletonUIError::CorruptData); + } + + auto buffer = MakeUniqueFallible<wchar_t[]>(dataLen); + if (!buffer) { + return Err(PreXULSkeletonUIError::OOM); + } + result = + ::RegGetValueW(regKey, nullptr, valueName.c_str(), RRF_RT_REG_BINARY, + nullptr, reinterpret_cast<PBYTE>(buffer.get()), &dataLen); + if (result != ERROR_SUCCESS) { + return Err(PreXULSkeletonUIError::RegistryError); + } + + Vector<CSSPixelSpan> resultVector; + double* asDoubles = reinterpret_cast<double*>(buffer.get()); + for (size_t i = 0; i < dataLen / (2 * sizeof(double)); i++) { + CSSPixelSpan span = {}; + span.start = *(asDoubles++); + span.end = *(asDoubles++); + if (!resultVector.append(span)) { + return Err(PreXULSkeletonUIError::OOM); + } + } + + return resultVector; +} + +static Result<double, PreXULSkeletonUIError> ReadRegDouble( + HKEY regKey, const std::wstring& valueName) { + double value = 0; + DWORD dataLen = sizeof(double); + LSTATUS result = + ::RegGetValueW(regKey, nullptr, valueName.c_str(), RRF_RT_REG_BINARY, + nullptr, reinterpret_cast<PBYTE>(&value), &dataLen); + if (result != ERROR_SUCCESS || dataLen != sizeof(double)) { + return Err(PreXULSkeletonUIError::RegistryError); + } + + return value; +} + +static Result<uint32_t, PreXULSkeletonUIError> ReadRegUint( + HKEY regKey, const std::wstring& valueName) { + DWORD value = 0; + DWORD dataLen = sizeof(uint32_t); + LSTATUS result = + ::RegGetValueW(regKey, nullptr, valueName.c_str(), RRF_RT_REG_DWORD, + nullptr, reinterpret_cast<PBYTE>(&value), &dataLen); + if (result != ERROR_SUCCESS) { + return Err(PreXULSkeletonUIError::RegistryError); + } + + return value; +} + +static Result<bool, PreXULSkeletonUIError> ReadRegBool( + HKEY regKey, const std::wstring& valueName) { + uint32_t value; + MOZ_TRY_VAR(value, ReadRegUint(regKey, valueName)); + return !!value; +} + +static Result<Ok, PreXULSkeletonUIError> WriteRegCSSPixelSpans( + HKEY regKey, const std::wstring& valueName, const CSSPixelSpan* spans, + int spansLength) { + // No guarantee on the packing of CSSPixelSpan. We could #pragma it, but it's + // also trivial to just copy them into a buffer of doubles. + auto doubles = MakeUnique<double[]>(spansLength * 2); + for (int i = 0; i < spansLength; ++i) { + doubles[i * 2] = spans[i].start; + doubles[i * 2 + 1] = spans[i].end; + } + + LSTATUS result = + ::RegSetValueExW(regKey, valueName.c_str(), 0, REG_BINARY, + reinterpret_cast<const BYTE*>(doubles.get()), + spansLength * sizeof(double) * 2); + if (result != ERROR_SUCCESS) { + return Err(PreXULSkeletonUIError::RegistryError); + } + return Ok(); +} + +static Result<Ok, PreXULSkeletonUIError> WriteRegDouble( + HKEY regKey, const std::wstring& valueName, double value) { + LSTATUS result = + ::RegSetValueExW(regKey, valueName.c_str(), 0, REG_BINARY, + reinterpret_cast<const BYTE*>(&value), sizeof(value)); + if (result != ERROR_SUCCESS) { + return Err(PreXULSkeletonUIError::RegistryError); + } + + return Ok(); +} + +static Result<Ok, PreXULSkeletonUIError> WriteRegUint( + HKEY regKey, const std::wstring& valueName, uint32_t value) { + LSTATUS result = + ::RegSetValueExW(regKey, valueName.c_str(), 0, REG_DWORD, + reinterpret_cast<PBYTE>(&value), sizeof(value)); + if (result != ERROR_SUCCESS) { + return Err(PreXULSkeletonUIError::RegistryError); + } + + return Ok(); +} + +static Result<Ok, PreXULSkeletonUIError> WriteRegBool( + HKEY regKey, const std::wstring& valueName, bool value) { + return WriteRegUint(regKey, valueName, value ? 1 : 0); +} + +static Result<Ok, PreXULSkeletonUIError> CreateAndStorePreXULSkeletonUIImpl( + HINSTANCE hInstance, int argc, char** argv) { + // Initializing COM below may load modules via SetWindowHookEx, some of + // which may modify the executable's IAT for ntdll.dll. If that happens, + // this browser process fails to launch sandbox processes because we cannot + // copy a modified IAT into a remote process (See SandboxBroker::LaunchApp). + // To prevent that, we cache the intact IAT before COM initialization. + // If EAF+ is enabled, CacheNtDllThunk() causes a crash, but EAF+ will + // also prevent an injected module from parsing the PE headers and modifying + // the IAT. Therefore, we can skip CacheNtDllThunk(). + if (!mozilla::IsEafPlusEnabled()) { + CacheNtDllThunk(); + } + + // NOTE: it's important that we initialize sProcessRuntime before showing a + // window. Historically we ran into issues where showing the window would + // cause an accessibility win event to fire, which could cause in-process + // system or third party components to initialize COM and prevent us from + // initializing it with important settings we need. + + // Some COM settings are global to the process and must be set before any non- + // trivial COM is run in the application. Since these settings may affect + // stability, we should instantiate COM ASAP so that we can ensure that these + // global settings are configured before anything can interfere. + sProcessRuntime = new mscom::ProcessRuntime( + mscom::ProcessRuntime::ProcessCategory::GeckoBrowserParent); + + const TimeStamp skeletonStart = TimeStamp::Now(); + + if (!IsWin10OrLater()) { + return Err(PreXULSkeletonUIError::Ineligible); + } + + HKEY regKey; + MOZ_TRY_VAR(regKey, OpenPreXULSkeletonUIRegKey()); + AutoCloseRegKey closeKey(regKey); + + UniquePtr<wchar_t[]> binPath; + MOZ_TRY_VAR(binPath, GetBinaryPath()); + + std::wstring regProgressName = + GetRegValueName(binPath.get(), sProgressSuffix); + auto progressResult = ReadRegUint(regKey, regProgressName); + if (!progressResult.isErr() && + progressResult.unwrap() != + static_cast<uint32_t>(PreXULSkeletonUIProgress::Completed)) { + return Err(PreXULSkeletonUIError::CrashedOnce); + } + + MOZ_TRY( + WriteRegUint(regKey, regProgressName, + static_cast<uint32_t>(PreXULSkeletonUIProgress::Started))); + auto writeCompletion = MakeScopeExit([&] { + Unused << WriteRegUint( + regKey, regProgressName, + static_cast<uint32_t>(PreXULSkeletonUIProgress::Completed)); + }); + + MOZ_TRY(GetSkeletonUILock()); + + bool explicitProfile = false; + MOZ_TRY(ValidateCmdlineArguments(argc, argv, &explicitProfile)); + MOZ_TRY(ValidateEnvVars()); + + auto enabledResult = + ReadRegBool(regKey, GetRegValueName(binPath.get(), sEnabledRegSuffix)); + if (enabledResult.isErr()) { + return Err(PreXULSkeletonUIError::EnabledKeyDoesNotExist); + } + if (!enabledResult.unwrap()) { + return Err(PreXULSkeletonUIError::Disabled); + } + sPreXULSkeletonUIEnabled = true; + + MOZ_ASSERT(!sAnimatedRects); + sAnimatedRects = new Vector<ColorRect>(); + + MOZ_TRY(LoadGdi32AndUser32Procedures()); + + if (!explicitProfile) { + MOZ_TRY(CheckForStartWithLastProfile()); + } + + WNDCLASSW wc; + wc.style = CS_DBLCLKS; + wc.lpfnWndProc = PreXULSkeletonUIProc; + wc.cbClsExtra = 0; + wc.cbWndExtra = 0; + wc.hInstance = hInstance; + wc.hIcon = sLoadIconW(::GetModuleHandleW(nullptr), gStockApplicationIcon); + wc.hCursor = sLoadCursorW(hInstance, gIDCWait); + wc.hbrBackground = nullptr; + wc.lpszMenuName = nullptr; + + // TODO: just ensure we disable this if we've overridden the window class + wc.lpszClassName = L"MozillaWindowClass"; + + if (!sRegisterClassW(&wc)) { + return Err(PreXULSkeletonUIError::FailedRegisteringWindowClass); + } + + uint32_t screenX; + MOZ_TRY_VAR(screenX, ReadRegUint(regKey, GetRegValueName(binPath.get(), + sScreenXRegSuffix))); + uint32_t screenY; + MOZ_TRY_VAR(screenY, ReadRegUint(regKey, GetRegValueName(binPath.get(), + sScreenYRegSuffix))); + uint32_t windowWidth; + MOZ_TRY_VAR( + windowWidth, + ReadRegUint(regKey, GetRegValueName(binPath.get(), sWidthRegSuffix))); + uint32_t windowHeight; + MOZ_TRY_VAR( + windowHeight, + ReadRegUint(regKey, GetRegValueName(binPath.get(), sHeightRegSuffix))); + MOZ_TRY_VAR( + sMaximized, + ReadRegBool(regKey, GetRegValueName(binPath.get(), sMaximizedRegSuffix))); + MOZ_TRY_VAR( + sCSSToDevPixelScaling, + ReadRegDouble(regKey, GetRegValueName(binPath.get(), + sCssToDevPixelScalingRegSuffix))); + Vector<CSSPixelSpan> urlbar; + MOZ_TRY_VAR(urlbar, + ReadRegCSSPixelSpans( + regKey, GetRegValueName(binPath.get(), sUrlbarCSSRegSuffix))); + Vector<CSSPixelSpan> searchbar; + MOZ_TRY_VAR(searchbar, + ReadRegCSSPixelSpans( + regKey, GetRegValueName(binPath.get(), sSearchbarRegSuffix))); + Vector<CSSPixelSpan> springs; + MOZ_TRY_VAR(springs, ReadRegCSSPixelSpans( + regKey, GetRegValueName(binPath.get(), + sSpringsCSSRegSuffix))); + + if (urlbar.empty() || searchbar.empty()) { + return Err(PreXULSkeletonUIError::CorruptData); + } + + EnumSet<SkeletonUIFlag, uint32_t> flags; + uint32_t flagsUint; + MOZ_TRY_VAR(flagsUint, ReadRegUint(regKey, GetRegValueName(binPath.get(), + sFlagsRegSuffix))); + flags.deserialize(flagsUint); + + if (flags.contains(SkeletonUIFlag::TouchDensity) || + flags.contains(SkeletonUIFlag::CompactDensity)) { + return Err(PreXULSkeletonUIError::BadUIDensity); + } + + uint32_t theme; + MOZ_TRY_VAR(theme, ReadRegUint(regKey, GetRegValueName(binPath.get(), + sThemeRegSuffix))); + ThemeMode themeMode = static_cast<ThemeMode>(theme); + if (themeMode == ThemeMode::Default) { + if (IsSystemDarkThemeEnabled() == true) { + themeMode = ThemeMode::Dark; + } + } + ThemeColors currentTheme = GetTheme(themeMode); + + if (!VerifyWindowDimensions(windowWidth, windowHeight)) { + return Err(PreXULSkeletonUIError::BadWindowDimensions); + } + + int showCmd = SW_SHOWNORMAL; + DWORD windowStyle = kPreXULSkeletonUIWindowStyle; + if (sMaximized) { + showCmd = SW_SHOWMAXIMIZED; + windowStyle |= WS_MAXIMIZE; + } + + sPreXULSkeletonUIWindow = + sCreateWindowExW(kPreXULSkeletonUIWindowStyleEx, L"MozillaWindowClass", + L"", windowStyle, screenX, screenY, windowWidth, + windowHeight, nullptr, nullptr, hInstance, nullptr); + if (!sPreXULSkeletonUIWindow) { + return Err(PreXULSkeletonUIError::CreateWindowFailed); + } + + sShowWindow(sPreXULSkeletonUIWindow, showCmd); + + sDpi = sGetDpiForWindow(sPreXULSkeletonUIWindow); + sNonClientHorizontalMargins = + sGetSystemMetricsForDpi(SM_CXFRAME, sDpi) + + sGetSystemMetricsForDpi(SM_CXPADDEDBORDER, sDpi); + sNonClientVerticalMargins = sGetSystemMetricsForDpi(SM_CYFRAME, sDpi) + + sGetSystemMetricsForDpi(SM_CXPADDEDBORDER, sDpi); + + if (sMaximized) { + HMONITOR monitor = + sMonitorFromWindow(sPreXULSkeletonUIWindow, MONITOR_DEFAULTTONULL); + if (!monitor) { + // NOTE: we specifically don't clean up the window here. If we're unable + // to finish setting up the window how we want it, we still need to keep + // it around and consume it with the first real toplevel window we + // create, to avoid flickering. + return Err(PreXULSkeletonUIError::FailedGettingMonitorInfo); + } + MONITORINFO mi = {sizeof(MONITORINFO)}; + if (!sGetMonitorInfoW(monitor, &mi)) { + return Err(PreXULSkeletonUIError::FailedGettingMonitorInfo); + } + + sWindowWidth = + mi.rcWork.right - mi.rcWork.left + sNonClientHorizontalMargins * 2; + sWindowHeight = + mi.rcWork.bottom - mi.rcWork.top + sNonClientVerticalMargins * 2; + } else { + sWindowWidth = static_cast<int>(windowWidth); + sWindowHeight = static_cast<int>(windowHeight); + } + + sSetWindowPos(sPreXULSkeletonUIWindow, 0, 0, 0, 0, 0, + SWP_FRAMECHANGED | SWP_NOACTIVATE | SWP_NOMOVE | + SWP_NOOWNERZORDER | SWP_NOSIZE | SWP_NOZORDER); + MOZ_TRY(DrawSkeletonUI(sPreXULSkeletonUIWindow, urlbar[0], searchbar[0], + springs, currentTheme, flags)); + if (sAnimatedRects) { + sPreXULSKeletonUIAnimationThread = ::CreateThread( + nullptr, 256 * 1024, AnimateSkeletonUI, nullptr, 0, nullptr); + } + + BASE_PROFILER_MARKER_UNTYPED( + "CreatePreXULSkeletonUI", OTHER, + MarkerTiming::IntervalUntilNowFrom(skeletonStart)); + + return Ok(); +} + +void CreateAndStorePreXULSkeletonUI(HINSTANCE hInstance, int argc, + char** argv) { + auto result = CreateAndStorePreXULSkeletonUIImpl(hInstance, argc, argv); + + if (result.isErr()) { + sErrorReason.emplace(result.unwrapErr()); + } +} + +void CleanupProcessRuntime() { + delete sProcessRuntime; + sProcessRuntime = nullptr; +} + +bool WasPreXULSkeletonUIMaximized() { return sMaximized; } + +bool GetPreXULSkeletonUIWasShown() { + return sPreXULSkeletonUIShown || !!sPreXULSkeletonUIWindow; +} + +HWND ConsumePreXULSkeletonUIHandle() { + // NOTE: we need to make sure that everything that runs here is a no-op if + // it failed to be set, which is a possibility. If anything fails to be set + // we don't want to clean everything up right away, because if we have a + // blank window up, we want that to stick around and get consumed by nsWindow + // as normal, otherwise the window will flicker in and out, which we imagine + // is unpleasant. + + // If we don't get 1 here, it means the thread is actually just sleeping, so + // we don't need to worry about giving out ownership of the window, because + // the thread will simply exit after its sleep. However, if it is 1, we need + // to wait for the thread to exit to be safe, as it could be doing anything. + if (InterlockedIncrement(&sAnimationControlFlag) == 1) { + ::WaitForSingleObject(sPreXULSKeletonUIAnimationThread, INFINITE); + } + ::CloseHandle(sPreXULSKeletonUIAnimationThread); + sPreXULSKeletonUIAnimationThread = nullptr; + HWND result = sPreXULSkeletonUIWindow; + sPreXULSkeletonUIWindow = nullptr; + free(sPixelBuffer); + sPixelBuffer = nullptr; + delete sAnimatedRects; + sAnimatedRects = nullptr; + + return result; +} + +Maybe<PreXULSkeletonUIError> GetPreXULSkeletonUIErrorReason() { + return sErrorReason; +} + +Result<Ok, PreXULSkeletonUIError> PersistPreXULSkeletonUIValues( + const SkeletonUISettings& settings) { + if (!sPreXULSkeletonUIEnabled) { + return Err(PreXULSkeletonUIError::Disabled); + } + + HKEY regKey; + MOZ_TRY_VAR(regKey, OpenPreXULSkeletonUIRegKey()); + AutoCloseRegKey closeKey(regKey); + + UniquePtr<wchar_t[]> binPath; + MOZ_TRY_VAR(binPath, GetBinaryPath()); + + MOZ_TRY(WriteRegUint(regKey, + GetRegValueName(binPath.get(), sScreenXRegSuffix), + settings.screenX)); + MOZ_TRY(WriteRegUint(regKey, + GetRegValueName(binPath.get(), sScreenYRegSuffix), + settings.screenY)); + MOZ_TRY(WriteRegUint(regKey, GetRegValueName(binPath.get(), sWidthRegSuffix), + settings.width)); + MOZ_TRY(WriteRegUint(regKey, GetRegValueName(binPath.get(), sHeightRegSuffix), + settings.height)); + + MOZ_TRY(WriteRegBool(regKey, + GetRegValueName(binPath.get(), sMaximizedRegSuffix), + settings.maximized)); + + EnumSet<SkeletonUIFlag, uint32_t> flags; + if (settings.menubarShown) { + flags += SkeletonUIFlag::MenubarShown; + } + if (settings.bookmarksToolbarShown) { + flags += SkeletonUIFlag::BookmarksToolbarShown; + } + if (settings.rtlEnabled) { + flags += SkeletonUIFlag::RtlEnabled; + } + if (settings.uiDensity == SkeletonUIDensity::Touch) { + flags += SkeletonUIFlag::TouchDensity; + } + if (settings.uiDensity == SkeletonUIDensity::Compact) { + flags += SkeletonUIFlag::CompactDensity; + } + + uint32_t flagsUint = flags.serialize(); + MOZ_TRY(WriteRegUint(regKey, GetRegValueName(binPath.get(), sFlagsRegSuffix), + flagsUint)); + + MOZ_TRY(WriteRegDouble( + regKey, GetRegValueName(binPath.get(), sCssToDevPixelScalingRegSuffix), + settings.cssToDevPixelScaling)); + MOZ_TRY(WriteRegCSSPixelSpans( + regKey, GetRegValueName(binPath.get(), sUrlbarCSSRegSuffix), + &settings.urlbarSpan, 1)); + MOZ_TRY(WriteRegCSSPixelSpans( + regKey, GetRegValueName(binPath.get(), sSearchbarRegSuffix), + &settings.searchbarSpan, 1)); + MOZ_TRY(WriteRegCSSPixelSpans( + regKey, GetRegValueName(binPath.get(), sSpringsCSSRegSuffix), + settings.springs.begin(), settings.springs.length())); + + return Ok(); +} + +MFBT_API bool GetPreXULSkeletonUIEnabled() { return sPreXULSkeletonUIEnabled; } + +MFBT_API Result<Ok, PreXULSkeletonUIError> SetPreXULSkeletonUIEnabledIfAllowed( + bool value) { + // If the pre-XUL skeleton UI was disallowed for some reason, we just want to + // ignore changes to the registry. An example of how things could be bad if + // we didn't: someone running firefox with the -profile argument could + // turn the skeleton UI on or off for the default profile. Turning it off + // maybe isn't so bad (though it's likely still incorrect), but turning it + // on could be bad if the user had specifically disabled it for a profile for + // some reason. Ultimately there's no correct decision here, and the + // messiness of this is just a consequence of sharing the registry values + // across profiles. However, whatever ill effects we observe should be + // correct themselves after one session. + if (PreXULSkeletonUIDisallowed()) { + return Err(PreXULSkeletonUIError::Disabled); + } + + HKEY regKey; + MOZ_TRY_VAR(regKey, OpenPreXULSkeletonUIRegKey()); + AutoCloseRegKey closeKey(regKey); + + UniquePtr<wchar_t[]> binPath; + MOZ_TRY_VAR(binPath, GetBinaryPath()); + MOZ_TRY(WriteRegBool( + regKey, GetRegValueName(binPath.get(), sEnabledRegSuffix), value)); + + if (!sPreXULSkeletonUIEnabled && value) { + // We specifically don't care if we fail to get this lock. We just want to + // do our best effort to lock it so that future instances don't create + // skeleton UIs while we're still running, since they will immediately exit + // and tell us to open a new window. + Unused << GetSkeletonUILock(); + } + + sPreXULSkeletonUIEnabled = value; + + return Ok(); +} + +MFBT_API Result<Ok, PreXULSkeletonUIError> SetPreXULSkeletonUIThemeId( + ThemeMode theme) { + if (theme == sTheme) { + return Ok(); + } + sTheme = theme; + + // If we fail below, invalidate sTheme + auto invalidateTheme = MakeScopeExit([] { sTheme = ThemeMode::Invalid; }); + + HKEY regKey; + MOZ_TRY_VAR(regKey, OpenPreXULSkeletonUIRegKey()); + AutoCloseRegKey closeKey(regKey); + + UniquePtr<wchar_t[]> binPath; + MOZ_TRY_VAR(binPath, GetBinaryPath()); + MOZ_TRY(WriteRegUint(regKey, GetRegValueName(binPath.get(), sThemeRegSuffix), + static_cast<uint32_t>(theme))); + + invalidateTheme.release(); + return Ok(); +} + +MFBT_API void PollPreXULSkeletonUIEvents() { + if (sPreXULSkeletonUIEnabled && sPreXULSkeletonUIWindow) { + MSG outMsg = {}; + PeekMessageW(&outMsg, sPreXULSkeletonUIWindow, 0, 0, 0); + } +} + +Result<Ok, PreXULSkeletonUIError> NotePreXULSkeletonUIRestarting() { + if (!sPreXULSkeletonUIEnabled) { + return Err(PreXULSkeletonUIError::Disabled); + } + + ::SetEnvironmentVariableW(L"MOZ_SKELETON_UI_RESTARTING", L"1"); + + // We assume that we are going to exit the application very shortly after + // this. It should thus be fine to release this lock, and we'll need to, + // since during a restart we launch the new instance before closing this + // one. + if (sPreXULSKeletonUILockFile != INVALID_HANDLE_VALUE) { + ::CloseHandle(sPreXULSKeletonUILockFile); + } + return Ok(); +} + +} // namespace mozilla diff --git a/mozglue/misc/PreXULSkeletonUI.h b/mozglue/misc/PreXULSkeletonUI.h new file mode 100644 index 0000000000..ee193b1dc8 --- /dev/null +++ b/mozglue/misc/PreXULSkeletonUI.h @@ -0,0 +1,186 @@ +/* -*- 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 https://mozilla.org/MPL/2.0/. */ + +#ifndef PreXULSkeletonUI_h_ +#define PreXULSkeletonUI_h_ + +#include <windows.h> +#include "mozilla/EnumSet.h" +#include "mozilla/Maybe.h" +#include "mozilla/Result.h" +#include "mozilla/Types.h" +#include "mozilla/Vector.h" + +namespace mozilla { + +// These unfortunately need to be kept in sync with the window style and +// extended window style computations in nsWindow. Luckily those styles seem +// to not vary based off of any user settings for the initial toplevel window, +// so we're safe here for now. +static const DWORD kPreXULSkeletonUIWindowStyle = + WS_CLIPCHILDREN | WS_DLGFRAME | WS_BORDER | WS_MAXIMIZEBOX | + WS_MINIMIZEBOX | WS_SIZEBOX | WS_SYSMENU; +static const DWORD kPreXULSkeletonUIWindowStyleEx = WS_EX_WINDOWEDGE; + +struct CSSPixelSpan { + double start; + double end; +}; + +struct DevPixelSpan { + int start; + int end; +}; + +enum class SkeletonUIDensity { Default, Touch, Compact }; + +struct SkeletonUISettings { + uint32_t screenX; + uint32_t screenY; + uint32_t width; + uint32_t height; + CSSPixelSpan urlbarSpan; + CSSPixelSpan searchbarSpan; + double cssToDevPixelScaling; + Vector<CSSPixelSpan> springs; + bool maximized; + bool menubarShown; + bool bookmarksToolbarShown; + bool rtlEnabled; + SkeletonUIDensity uiDensity; +}; + +enum class ThemeMode : uint32_t { Invalid, Default, Dark, Light }; + +enum class SkeletonUIFlag : uint8_t { + MenubarShown, + BookmarksToolbarShown, + RtlEnabled, + TouchDensity, + CompactDensity, +}; + +struct ThemeColors { + uint32_t backgroundColor; + uint32_t toolbarForegroundColor; + uint32_t tabBarColor; + uint32_t tabColor; + uint32_t tabOutlineColor; + uint32_t chromeContentDividerColor; + uint32_t urlbarColor; + uint32_t urlbarBorderColor; + uint32_t animationColor; +}; + +enum class PreXULSkeletonUIError : uint32_t { + None, + Ineligible, + Disabled, + EnabledKeyDoesNotExist, + OOM, + Cmdline, + EnvVars, + FailedToOpenRegistryKey, + RegistryError, + FailedLoadingDynamicProcs, + FailedGettingLock, + FilesystemFailure, + NoStartWithLastProfile, + FailedRegisteringWindowClass, + CorruptData, + BadWindowDimensions, + FailedGettingMonitorInfo, + CreateWindowFailed, + FailedGettingDC, + FailedBlitting, + FailedFillingBottomRect, + CrashedOnce, + BadUIDensity, + Unknown, +}; + +inline const wchar_t* GetPreXULSkeletonUIErrorString( + PreXULSkeletonUIError error) { + switch (error) { + case PreXULSkeletonUIError::None: + return L"None"; + case PreXULSkeletonUIError::Ineligible: + return L"Ineligible"; + case PreXULSkeletonUIError::Disabled: + return L"Disabled"; + case PreXULSkeletonUIError::OOM: + return L"OOM"; + case PreXULSkeletonUIError::Cmdline: + return L"Cmdline"; + case PreXULSkeletonUIError::EnvVars: + return L"EnvVars"; + case PreXULSkeletonUIError::FailedToOpenRegistryKey: + return L"FailedToOpenRegistryKey"; + case PreXULSkeletonUIError::RegistryError: + return L"RegistryError"; + case PreXULSkeletonUIError::FailedLoadingDynamicProcs: + return L"FailedLoadingDynamicProcs"; + case PreXULSkeletonUIError::FailedGettingLock: + return L"FailedGettingLock"; + case PreXULSkeletonUIError::FilesystemFailure: + return L"FilesystemFailure"; + case PreXULSkeletonUIError::NoStartWithLastProfile: + return L"NoStartWithLastProfile"; + case PreXULSkeletonUIError::FailedRegisteringWindowClass: + return L"FailedRegisteringWindowClass"; + case PreXULSkeletonUIError::CorruptData: + return L"CorruptData"; + case PreXULSkeletonUIError::BadWindowDimensions: + return L"BadWindowDimensions"; + case PreXULSkeletonUIError::FailedGettingMonitorInfo: + return L"FailedGettingMonitorInfo"; + case PreXULSkeletonUIError::EnabledKeyDoesNotExist: + return L"EnabledKeyDoesNotExist"; + case PreXULSkeletonUIError::CreateWindowFailed: + return L"CreateWindowFailed"; + case PreXULSkeletonUIError::FailedGettingDC: + return L"FailedGettingDC"; + case PreXULSkeletonUIError::FailedBlitting: + return L"FailedBlitting"; + case PreXULSkeletonUIError::FailedFillingBottomRect: + return L"FailedFillingBottomRect"; + case PreXULSkeletonUIError::CrashedOnce: + return L"CrashedOnce"; + case PreXULSkeletonUIError::BadUIDensity: + return L"BadUIDensity"; + case PreXULSkeletonUIError::Unknown: + return L"Unknown"; + } + + MOZ_ASSERT_UNREACHABLE(); + return L"Unknown"; +} + +enum class PreXULSkeletonUIProgress : uint32_t { + Started, + Completed, +}; + +MFBT_API void CreateAndStorePreXULSkeletonUI(HINSTANCE hInstance, int argc, + char** argv); +MFBT_API void CleanupProcessRuntime(); +MFBT_API bool GetPreXULSkeletonUIWasShown(); +MFBT_API HWND ConsumePreXULSkeletonUIHandle(); +MFBT_API Maybe<PreXULSkeletonUIError> GetPreXULSkeletonUIErrorReason(); +MFBT_API bool WasPreXULSkeletonUIMaximized(); +MFBT_API Result<Ok, PreXULSkeletonUIError> PersistPreXULSkeletonUIValues( + const SkeletonUISettings& settings); +MFBT_API bool GetPreXULSkeletonUIEnabled(); +MFBT_API Result<Ok, PreXULSkeletonUIError> SetPreXULSkeletonUIEnabledIfAllowed( + bool value); +MFBT_API void PollPreXULSkeletonUIEvents(); +MFBT_API Result<Ok, PreXULSkeletonUIError> SetPreXULSkeletonUIThemeId( + ThemeMode theme); +MFBT_API Result<Ok, PreXULSkeletonUIError> NotePreXULSkeletonUIRestarting(); + +} // namespace mozilla + +#endif diff --git a/mozglue/misc/Printf.cpp b/mozglue/misc/Printf.cpp new file mode 100644 index 0000000000..10e06ff51c --- /dev/null +++ b/mozglue/misc/Printf.cpp @@ -0,0 +1,1105 @@ +/* -*- 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/. */ + +/* + * Portable safe sprintf code. + * + * Author: Kipp E.B. Hickman + */ + +#include "double-conversion/double-to-string.h" +#include "mozilla/AllocPolicy.h" +#include "mozilla/Printf.h" +#include "mozilla/UniquePtrExtensions.h" +#include "mozilla/Vector.h" + +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#if defined(XP_WIN) +# include <windows.h> +#endif + +using double_conversion::DoubleToStringConverter; +using DTSC = DoubleToStringConverter; + +/* + * Numbered Argument State + */ +struct NumArgState { + int type; // type of the current ap + va_list ap; // point to the corresponding position on ap +}; + +using NumArgStateVector = + mozilla::Vector<NumArgState, 20, mozilla::MallocAllocPolicy>; + +// For values up to and including TYPE_DOUBLE, the lowest bit indicates +// whether the type is signed (0) or unsigned (1). +#define TYPE_SHORT 0 +#define TYPE_USHORT 1 +#define TYPE_INTN 2 +#define TYPE_UINTN 3 +#define TYPE_LONG 4 +#define TYPE_ULONG 5 +#define TYPE_LONGLONG 6 +#define TYPE_ULONGLONG 7 +#define TYPE_DOUBLE 8 +#define TYPE_STRING 9 +#define TYPE_INTSTR 10 +#define TYPE_POINTER 11 +#if defined(XP_WIN) +# define TYPE_WSTRING 12 +#endif +#define TYPE_SCHAR 14 +#define TYPE_UCHAR 15 +#define TYPE_UNKNOWN 20 + +#define FLAG_LEFT 0x1 +#define FLAG_SIGNED 0x2 +#define FLAG_SPACED 0x4 +#define FLAG_ZEROS 0x8 +#define FLAG_NEG 0x10 + +static const char hex[] = "0123456789abcdef"; +static const char HEX[] = "0123456789ABCDEF"; + +// Fill into the buffer using the data in src +bool mozilla::PrintfTarget::fill2(const char* src, int srclen, int width, + int flags) { + char space = ' '; + + width -= srclen; + if (width > 0 && (flags & FLAG_LEFT) == 0) { // Right adjusting + if (flags & FLAG_ZEROS) { + space = '0'; + } + while (--width >= 0) { + if (!emit(&space, 1)) { + return false; + } + } + } + + // Copy out the source data + if (!emit(src, srclen)) { + return false; + } + + if (width > 0 && (flags & FLAG_LEFT) != 0) { // Left adjusting + while (--width >= 0) { + if (!emit(&space, 1)) { + return false; + } + } + } + return true; +} + +/* + * Fill a number. The order is: optional-sign zero-filling conversion-digits + */ +bool mozilla::PrintfTarget::fill_n(const char* src, int srclen, int width, + int prec, int type, int flags) { + int zerowidth = 0; + int precwidth = 0; + int signwidth = 0; + int leftspaces = 0; + int rightspaces = 0; + int cvtwidth; + char sign; + + if ((type & 1) == 0) { + if (flags & FLAG_NEG) { + sign = '-'; + signwidth = 1; + } else if (flags & FLAG_SIGNED) { + sign = '+'; + signwidth = 1; + } else if (flags & FLAG_SPACED) { + sign = ' '; + signwidth = 1; + } + } + cvtwidth = signwidth + srclen; + + if (prec > 0 && (type != TYPE_DOUBLE)) { + if (prec > srclen) { + precwidth = prec - srclen; // Need zero filling + cvtwidth += precwidth; + } + } + + if ((flags & FLAG_ZEROS) && ((type == TYPE_DOUBLE) || (prec < 0))) { + if (width > cvtwidth) { + zerowidth = width - cvtwidth; // Zero filling + cvtwidth += zerowidth; + } + } + + if (flags & FLAG_LEFT) { + if (width > cvtwidth) { + // Space filling on the right (i.e. left adjusting) + rightspaces = width - cvtwidth; + } + } else { + if (width > cvtwidth) { + // Space filling on the left (i.e. right adjusting) + leftspaces = width - cvtwidth; + } + } + while (--leftspaces >= 0) { + if (!emit(" ", 1)) { + return false; + } + } + if (signwidth) { + if (!emit(&sign, 1)) { + return false; + } + } + while (--precwidth >= 0) { + if (!emit("0", 1)) { + return false; + } + } + while (--zerowidth >= 0) { + if (!emit("0", 1)) { + return false; + } + } + if (!emit(src, uint32_t(srclen))) { + return false; + } + while (--rightspaces >= 0) { + if (!emit(" ", 1)) { + return false; + } + } + return true; +} + +// All that the cvt_* functions care about as far as the TYPE_* constants is +// that the low bit is set to indicate unsigned, or unset to indicate signed. +// So we don't try to hard to ensure that the passed TYPE_* constant lines +// up with the actual size of the number being printed here. The main printf +// code, below, does have to care so that the correct bits are extracted from +// the varargs list. +bool mozilla::PrintfTarget::appendIntDec(int32_t num) { + int flags = 0; + long n = num; + if (n < 0) { + n = -n; + flags |= FLAG_NEG; + } + return cvt_l(n, -1, -1, 10, TYPE_INTN, flags, hex); +} + +bool mozilla::PrintfTarget::appendIntDec(uint32_t num) { + return cvt_l(num, -1, -1, 10, TYPE_UINTN, 0, hex); +} + +bool mozilla::PrintfTarget::appendIntOct(uint32_t num) { + return cvt_l(num, -1, -1, 8, TYPE_UINTN, 0, hex); +} + +bool mozilla::PrintfTarget::appendIntHex(uint32_t num) { + return cvt_l(num, -1, -1, 16, TYPE_UINTN, 0, hex); +} + +bool mozilla::PrintfTarget::appendIntDec(int64_t num) { + int flags = 0; + if (num < 0) { + num = -num; + flags |= FLAG_NEG; + } + return cvt_ll(num, -1, -1, 10, TYPE_INTN, flags, hex); +} + +bool mozilla::PrintfTarget::appendIntDec(uint64_t num) { + return cvt_ll(num, -1, -1, 10, TYPE_UINTN, 0, hex); +} + +bool mozilla::PrintfTarget::appendIntOct(uint64_t num) { + return cvt_ll(num, -1, -1, 8, TYPE_UINTN, 0, hex); +} + +bool mozilla::PrintfTarget::appendIntHex(uint64_t num) { + return cvt_ll(num, -1, -1, 16, TYPE_UINTN, 0, hex); +} + +/* Convert a long into its printable form. */ +bool mozilla::PrintfTarget::cvt_l(long num, int width, int prec, int radix, + int type, int flags, const char* hexp) { + char cvtbuf[100]; + char* cvt; + int digits; + + // according to the man page this needs to happen + if ((prec == 0) && (num == 0)) { + return fill_n("", 0, width, prec, type, flags); + } + + // Converting decimal is a little tricky. In the unsigned case we + // need to stop when we hit 10 digits. In the signed case, we can + // stop when the number is zero. + cvt = cvtbuf + sizeof(cvtbuf); + digits = 0; + while (num) { + int digit = (((unsigned long)num) % radix) & 0xF; + *--cvt = hexp[digit]; + digits++; + num = (long)(((unsigned long)num) / radix); + } + if (digits == 0) { + *--cvt = '0'; + digits++; + } + + // Now that we have the number converted without its sign, deal with + // the sign and zero padding. + return fill_n(cvt, digits, width, prec, type, flags); +} + +/* Convert a 64-bit integer into its printable form. */ +bool mozilla::PrintfTarget::cvt_ll(int64_t num, int width, int prec, int radix, + int type, int flags, const char* hexp) { + // According to the man page, this needs to happen. + if (prec == 0 && num == 0) { + return fill_n("", 0, width, prec, type, flags); + } + + // Converting decimal is a little tricky. In the unsigned case we + // need to stop when we hit 10 digits. In the signed case, we can + // stop when the number is zero. + int64_t rad = int64_t(radix); + char cvtbuf[100]; + char* cvt = cvtbuf + sizeof(cvtbuf); + int digits = 0; + while (num != 0) { + int64_t quot = uint64_t(num) / rad; + int64_t rem = uint64_t(num) % rad; + int32_t digit = int32_t(rem); + *--cvt = hexp[digit & 0xf]; + digits++; + num = quot; + } + if (digits == 0) { + *--cvt = '0'; + digits++; + } + + // Now that we have the number converted without its sign, deal with + // the sign and zero padding. + return fill_n(cvt, digits, width, prec, type, flags); +} + +template <size_t N> +constexpr static size_t lengthof(const char (&)[N]) { + return N - 1; +} + +// Longest possible output from ToFixed for positive numbers: +// [0-9]{kMaxFixedDigitsBeforePoint}\.[0-9]{kMaxFixedDigitsAfterPoint}? +constexpr int FIXED_MAX_CHARS = + DTSC::kMaxFixedDigitsBeforePoint + 1 + DTSC::kMaxFixedDigitsAfterPoint; + +// Longest possible output from ToExponential: +// [1-9]\.[0-9]{kMaxExponentialDigits}e[+-][0-9]{1,3} +// (std::numeric_limits<double>::max() has exponent 308). +constexpr int EXPONENTIAL_MAX_CHARS = + lengthof("1.") + DTSC::kMaxExponentialDigits + lengthof("e+999"); + +// Longest possible output from ToPrecise: +// [0-9\.]{kMaxPrecisionDigits+1} or +// [1-9]\.[0-9]{kMaxPrecisionDigits-1}e[+-][0-9]{1,3} +constexpr int PRECISE_MAX_CHARS = + lengthof("1.") + DTSC::kMaxPrecisionDigits - 1 + lengthof("e+999"); + +constexpr int DTSC_MAX_CHARS = + std::max({FIXED_MAX_CHARS, EXPONENTIAL_MAX_CHARS, PRECISE_MAX_CHARS}); + +/* + * Convert a double precision floating point number into its printable + * form. + */ +bool mozilla::PrintfTarget::cvt_f(double d, char c, int width, int prec, + int flags) { + bool lower = islower(c); + const char* inf = lower ? "inf" : "INF"; + const char* nan = lower ? "nan" : "NAN"; + char e = lower ? 'e' : 'E'; + DoubleToStringConverter converter(DTSC::UNIQUE_ZERO | DTSC::NO_TRAILING_ZERO | + DTSC::EMIT_POSITIVE_EXPONENT_SIGN, + inf, nan, e, 0, 0, 4, 0, 2); + // Longest of the above cases, plus space for a terminal nul character. + char buf[DTSC_MAX_CHARS + 1]; + double_conversion::StringBuilder builder(buf, sizeof(buf)); + bool success = false; + if (std::signbit(d)) { + d = std::abs(d); + flags |= FLAG_NEG; + } + if (!std::isfinite(d)) { + flags &= ~FLAG_ZEROS; + } + // "If the precision is missing, it shall be taken as 6." + if (prec < 0) { + prec = 6; + } + switch (c) { + case 'e': + case 'E': + success = converter.ToExponential(d, prec, &builder); + break; + case 'f': + case 'F': + success = converter.ToFixed(d, prec, &builder); + break; + case 'g': + case 'G': + // "If an explicit precision is zero, it shall be taken as 1." + success = converter.ToPrecision(d, prec ? prec : 1, &builder); + break; + } + if (!success) { + return false; + } + int len = builder.position(); + char* cvt = builder.Finalize(); + return fill_n(cvt, len, width, prec, TYPE_DOUBLE, flags); +} + +/* + * Convert a string into its printable form. "width" is the output + * width. "prec" is the maximum number of characters of "s" to output, + * where -1 means until NUL. + */ +bool mozilla::PrintfTarget::cvt_s(const char* s, int width, int prec, + int flags) { + if (prec == 0) { + return true; + } + if (!s) { + s = "(null)"; + } + + // Limit string length by precision value + int slen = int(strlen(s)); + if (0 < prec && prec < slen) { + slen = prec; + } + + // and away we go + return fill2(s, slen, width, flags); +} + +/* + * BuildArgArray stands for Numbered Argument list Sprintf + * for example, + * fmp = "%4$i, %2$d, %3s, %1d"; + * the number must start from 1, and no gap among them + */ +static bool BuildArgArray(const char* fmt, va_list ap, NumArgStateVector& nas) { + size_t number = 0, cn = 0, i; + const char* p; + char c; + + // First pass: + // Detemine how many legal % I have got, then allocate space. + + p = fmt; + i = 0; + while ((c = *p++) != 0) { + if (c != '%') { + continue; + } + if ((c = *p++) == '%') { // skip %% case + continue; + } + + while (c != 0) { + if (c > '9' || c < '0') { + if (c == '$') { // numbered argument case + if (i > 0) { + MOZ_CRASH("Bad format string"); + } + number++; + } else { // non-numbered argument case + if (number > 0) { + MOZ_CRASH("Bad format string"); + } + i = 1; + } + break; + } + + c = *p++; + } + } + + if (number == 0) { + return true; + } + + // Only allow a limited number of arguments. + MOZ_RELEASE_ASSERT(number <= 20); + + if (!nas.growByUninitialized(number)) { + return false; + } + + for (i = 0; i < number; i++) { + nas[i].type = TYPE_UNKNOWN; + } + + // Second pass: + // Set nas[].type. + + p = fmt; + while ((c = *p++) != 0) { + if (c != '%') { + continue; + } + c = *p++; + if (c == '%') { + continue; + } + + cn = 0; + while (c && c != '$') { // should improve error check later + cn = cn * 10 + c - '0'; + c = *p++; + } + + if (!c || cn < 1 || cn > number) { + MOZ_CRASH("Bad format string"); + } + + // nas[cn] starts from 0, and make sure nas[cn].type is not assigned. + cn--; + if (nas[cn].type != TYPE_UNKNOWN) { + continue; + } + + c = *p++; + + // flags + while ((c == '-') || (c == '+') || (c == ' ') || (c == '0')) { + c = *p++; + } + + // width + if (c == '*') { + // not supported feature, for the argument is not numbered + MOZ_CRASH("Bad format string"); + } + + while ((c >= '0') && (c <= '9')) { + c = *p++; + } + + // precision + if (c == '.') { + c = *p++; + if (c == '*') { + // not supported feature, for the argument is not numbered + MOZ_CRASH("Bad format string"); + } + + while ((c >= '0') && (c <= '9')) { + c = *p++; + } + } + + // size + nas[cn].type = TYPE_INTN; + if (c == 'h') { + nas[cn].type = TYPE_SHORT; + c = *p++; + if (c == 'h') { + nas[cn].type = TYPE_SCHAR; + c = *p++; + } + } else if (c == 'L') { + nas[cn].type = TYPE_LONGLONG; + c = *p++; + } else if (c == 'l') { + nas[cn].type = TYPE_LONG; + c = *p++; + if (c == 'l') { + nas[cn].type = TYPE_LONGLONG; + c = *p++; + } + } else if (c == 'z' || c == 'I') { + static_assert(sizeof(size_t) == sizeof(int) || + sizeof(size_t) == sizeof(long) || + sizeof(size_t) == sizeof(long long), + "size_t is not one of the expected sizes"); + nas[cn].type = sizeof(size_t) == sizeof(int) ? TYPE_INTN + : sizeof(size_t) == sizeof(long) ? TYPE_LONG + : TYPE_LONGLONG; + c = *p++; + } else if (c == 't') { + static_assert(sizeof(ptrdiff_t) == sizeof(int) || + sizeof(ptrdiff_t) == sizeof(long) || + sizeof(ptrdiff_t) == sizeof(long long), + "ptrdiff_t is not one of the expected sizes"); + nas[cn].type = sizeof(ptrdiff_t) == sizeof(int) ? TYPE_INTN + : sizeof(ptrdiff_t) == sizeof(long) ? TYPE_LONG + : TYPE_LONGLONG; + c = *p++; + } else if (c == 'j') { + static_assert(sizeof(intmax_t) == sizeof(int) || + sizeof(intmax_t) == sizeof(long) || + sizeof(intmax_t) == sizeof(long long), + "intmax_t is not one of the expected sizes"); + nas[cn].type = sizeof(intmax_t) == sizeof(int) ? TYPE_INTN + : sizeof(intmax_t) == sizeof(long) ? TYPE_LONG + : TYPE_LONGLONG; + c = *p++; + } + + // format + switch (c) { + case 'd': + case 'c': + case 'i': + break; + + case 'o': + case 'u': + case 'x': + case 'X': + // Mark as unsigned type. + nas[cn].type |= 1; + break; + + case 'e': + case 'E': + case 'f': + case 'F': + case 'g': + case 'G': + nas[cn].type = TYPE_DOUBLE; + break; + + case 'p': + nas[cn].type = TYPE_POINTER; + break; + + case 'S': +#if defined(XP_WIN) + nas[cn].type = TYPE_WSTRING; +#else + MOZ_ASSERT(0); + nas[cn].type = TYPE_UNKNOWN; +#endif + break; + + case 's': +#if defined(XP_WIN) + if (nas[cn].type == TYPE_LONG) { + nas[cn].type = TYPE_WSTRING; + break; + } +#endif + // Other type sizes are not supported here. + MOZ_ASSERT(nas[cn].type == TYPE_INTN); + nas[cn].type = TYPE_STRING; + break; + + case 'n': + nas[cn].type = TYPE_INTSTR; + break; + + default: + MOZ_ASSERT(0); + nas[cn].type = TYPE_UNKNOWN; + break; + } + + // get a legal para. + if (nas[cn].type == TYPE_UNKNOWN) { + MOZ_CRASH("Bad format string"); + } + } + + // Third pass: + // Fill nas[].ap. + + cn = 0; + while (cn < number) { + // A TYPE_UNKNOWN here means that the format asked for a + // positional argument without specifying the meaning of some + // earlier argument. + MOZ_ASSERT(nas[cn].type != TYPE_UNKNOWN); + + va_copy(nas[cn].ap, ap); + + switch (nas[cn].type) { + case TYPE_SCHAR: + case TYPE_UCHAR: + case TYPE_SHORT: + case TYPE_USHORT: + case TYPE_INTN: + case TYPE_UINTN: + (void)va_arg(ap, int); + break; + case TYPE_LONG: + (void)va_arg(ap, long); + break; + case TYPE_ULONG: + (void)va_arg(ap, unsigned long); + break; + case TYPE_LONGLONG: + (void)va_arg(ap, long long); + break; + case TYPE_ULONGLONG: + (void)va_arg(ap, unsigned long long); + break; + case TYPE_STRING: + (void)va_arg(ap, char*); + break; + case TYPE_INTSTR: + (void)va_arg(ap, int*); + break; + case TYPE_DOUBLE: + (void)va_arg(ap, double); + break; + case TYPE_POINTER: + (void)va_arg(ap, void*); + break; +#if defined(XP_WIN) + case TYPE_WSTRING: + (void)va_arg(ap, wchar_t*); + break; +#endif + + default: + MOZ_CRASH(); + } + + cn++; + } + + return true; +} + +mozilla::PrintfTarget::PrintfTarget() : mEmitted(0) {} + +bool mozilla::PrintfTarget::vprint(const char* fmt, va_list ap) { + char c; + int flags, width, prec, radix, type; + union { + char ch; + int i; + long l; + long long ll; + double d; + const char* s; + int* ip; + void* p; +#if defined(XP_WIN) + const wchar_t* ws; +#endif + } u{}; + const char* hexp; + int i; + + // Build an argument array, IF the fmt is numbered argument + // list style, to contain the Numbered Argument list pointers. + + NumArgStateVector nas; + if (!BuildArgArray(fmt, ap, nas)) { + // the fmt contains error Numbered Argument format, jliu@netscape.com + MOZ_CRASH("Bad format string"); + } + + while ((c = *fmt++) != 0) { + if (c != '%') { + if (!emit(fmt - 1, 1)) { + return false; + } + + continue; + } + + // Gobble up the % format string. Hopefully we have handled all + // of the strange cases! + flags = 0; + c = *fmt++; + if (c == '%') { + // quoting a % with %% + if (!emit(fmt - 1, 1)) { + return false; + } + + continue; + } + + if (!nas.empty()) { + // the fmt contains the Numbered Arguments feature + i = 0; + while (c && c != '$') { // should improve error check later + i = (i * 10) + (c - '0'); + c = *fmt++; + } + + if (nas[i - 1].type == TYPE_UNKNOWN) { + MOZ_CRASH("Bad format string"); + } + + ap = nas[i - 1].ap; + c = *fmt++; + } + + // Examine optional flags. Note that we do not implement the + // '#' flag of sprintf(). The ANSI C spec. of the '#' flag is + // somewhat ambiguous and not ideal, which is perhaps why + // the various sprintf() implementations are inconsistent + // on this feature. + while ((c == '-') || (c == '+') || (c == ' ') || (c == '0')) { + if (c == '-') { + flags |= FLAG_LEFT; + } + if (c == '+') { + flags |= FLAG_SIGNED; + } + if (c == ' ') { + flags |= FLAG_SPACED; + } + if (c == '0') { + flags |= FLAG_ZEROS; + } + c = *fmt++; + } + if (flags & FLAG_SIGNED) { + flags &= ~FLAG_SPACED; + } + if (flags & FLAG_LEFT) { + flags &= ~FLAG_ZEROS; + } + + // width + if (c == '*') { + c = *fmt++; + width = va_arg(ap, int); + if (width < 0) { + width = -width; + flags |= FLAG_LEFT; + flags &= ~FLAG_ZEROS; + } + } else { + width = 0; + while ((c >= '0') && (c <= '9')) { + width = (width * 10) + (c - '0'); + c = *fmt++; + } + } + + // precision + prec = -1; + if (c == '.') { + c = *fmt++; + if (c == '*') { + c = *fmt++; + prec = va_arg(ap, int); + } else { + prec = 0; + while ((c >= '0') && (c <= '9')) { + prec = (prec * 10) + (c - '0'); + c = *fmt++; + } + } + } + + // size + type = TYPE_INTN; + if (c == 'h') { + type = TYPE_SHORT; + c = *fmt++; + if (c == 'h') { + type = TYPE_SCHAR; + c = *fmt++; + } + } else if (c == 'L') { + type = TYPE_LONGLONG; + c = *fmt++; + } else if (c == 'l') { + type = TYPE_LONG; + c = *fmt++; + if (c == 'l') { + type = TYPE_LONGLONG; + c = *fmt++; + } + } else if (c == 'z' || c == 'I') { + static_assert(sizeof(size_t) == sizeof(int) || + sizeof(size_t) == sizeof(long) || + sizeof(size_t) == sizeof(long long), + "size_t is not one of the expected sizes"); + type = sizeof(size_t) == sizeof(int) ? TYPE_INTN + : sizeof(size_t) == sizeof(long) ? TYPE_LONG + : TYPE_LONGLONG; + c = *fmt++; + } else if (c == 't') { + static_assert(sizeof(ptrdiff_t) == sizeof(int) || + sizeof(ptrdiff_t) == sizeof(long) || + sizeof(ptrdiff_t) == sizeof(long long), + "ptrdiff_t is not one of the expected sizes"); + type = sizeof(ptrdiff_t) == sizeof(int) ? TYPE_INTN + : sizeof(ptrdiff_t) == sizeof(long) ? TYPE_LONG + : TYPE_LONGLONG; + c = *fmt++; + } else if (c == 'j') { + static_assert(sizeof(intmax_t) == sizeof(int) || + sizeof(intmax_t) == sizeof(long) || + sizeof(intmax_t) == sizeof(long long), + "intmax_t is not one of the expected sizes"); + type = sizeof(intmax_t) == sizeof(int) ? TYPE_INTN + : sizeof(intmax_t) == sizeof(long) ? TYPE_LONG + : TYPE_LONGLONG; + c = *fmt++; + } + + // format + hexp = hex; + switch (c) { + case 'd': + case 'i': // decimal/integer + radix = 10; + goto fetch_and_convert; + + case 'o': // octal + radix = 8; + type |= 1; + goto fetch_and_convert; + + case 'u': // unsigned decimal + radix = 10; + type |= 1; + goto fetch_and_convert; + + case 'x': // unsigned hex + radix = 16; + type |= 1; + goto fetch_and_convert; + + case 'X': // unsigned HEX + radix = 16; + hexp = HEX; + type |= 1; + goto fetch_and_convert; + + fetch_and_convert: + switch (type) { + case TYPE_SCHAR: + u.l = (signed char)va_arg(ap, int); + if (u.l < 0) { + u.l = -u.l; + flags |= FLAG_NEG; + } + goto do_long; + case TYPE_UCHAR: + u.l = (unsigned char)va_arg(ap, unsigned int); + goto do_long; + case TYPE_SHORT: + u.l = (short)va_arg(ap, int); + if (u.l < 0) { + u.l = -u.l; + flags |= FLAG_NEG; + } + goto do_long; + case TYPE_USHORT: + u.l = (unsigned short)va_arg(ap, unsigned int); + goto do_long; + case TYPE_INTN: + u.l = va_arg(ap, int); + if (u.l < 0) { + u.l = -u.l; + flags |= FLAG_NEG; + } + goto do_long; + case TYPE_UINTN: + u.l = (long)va_arg(ap, unsigned int); + goto do_long; + + case TYPE_LONG: + u.l = va_arg(ap, long); + if (u.l < 0) { + u.l = -u.l; + flags |= FLAG_NEG; + } + goto do_long; + case TYPE_ULONG: + u.l = (long)va_arg(ap, unsigned long); + do_long: + if (!cvt_l(u.l, width, prec, radix, type, flags, hexp)) { + return false; + } + + break; + + case TYPE_LONGLONG: + u.ll = va_arg(ap, long long); + if (u.ll < 0) { + u.ll = -u.ll; + flags |= FLAG_NEG; + } + goto do_longlong; + case TYPE_POINTER: + u.ll = (uintptr_t)va_arg(ap, void*); + goto do_longlong; + case TYPE_ULONGLONG: + u.ll = va_arg(ap, unsigned long long); + do_longlong: + if (!cvt_ll(u.ll, width, prec, radix, type, flags, hexp)) { + return false; + } + + break; + } + break; + + case 'e': + case 'E': + case 'f': + case 'F': + case 'g': + case 'G': + u.d = va_arg(ap, double); + if (!cvt_f(u.d, c, width, prec, flags)) { + return false; + } + + break; + + case 'c': + if ((flags & FLAG_LEFT) == 0) { + while (width-- > 1) { + if (!emit(" ", 1)) { + return false; + } + } + } + switch (type) { + case TYPE_SHORT: + case TYPE_INTN: + u.ch = va_arg(ap, int); + if (!emit(&u.ch, 1)) { + return false; + } + break; + } + if (flags & FLAG_LEFT) { + while (width-- > 1) { + if (!emit(" ", 1)) { + return false; + } + } + } + break; + + case 'p': + type = TYPE_POINTER; + radix = 16; + goto fetch_and_convert; + + case 's': + if (type == TYPE_INTN) { + u.s = va_arg(ap, const char*); + if (!cvt_s(u.s, width, prec, flags)) { + return false; + } + break; + } + MOZ_ASSERT(type == TYPE_LONG); + [[fallthrough]]; + case 'S': +#if defined(XP_WIN) + { + u.ws = va_arg(ap, const wchar_t*); + + int rv = WideCharToMultiByte(CP_ACP, 0, u.ws, -1, NULL, 0, NULL, NULL); + if (rv == 0 && GetLastError() == ERROR_NO_UNICODE_TRANSLATION) { + if (!cvt_s("<unicode errors in string>", width, prec, flags)) { + return false; + } + } else { + if (rv == 0) { + rv = 1; + } + UniqueFreePtr<char[]> buf((char*)malloc(rv)); + WideCharToMultiByte(CP_ACP, 0, u.ws, -1, buf.get(), rv, NULL, NULL); + buf[rv - 1] = '\0'; + + if (!cvt_s(buf.get(), width, prec, flags)) { + return false; + } + } + } +#else + // Not supported here. + MOZ_ASSERT(0); +#endif + break; + + case 'n': + u.ip = va_arg(ap, int*); + if (u.ip) { + *u.ip = mEmitted; + } + break; + + default: + // Not a % token after all... skip it + if (!emit("%", 1)) { + return false; + } + if (!emit(fmt - 1, 1)) { + return false; + } + } + } + + return true; +} + +/************************************************************************/ + +bool mozilla::PrintfTarget::print(const char* format, ...) { + va_list ap; + + va_start(ap, format); + bool result = vprint(format, ap); + va_end(ap); + return result; +} + +#undef TYPE_SHORT +#undef TYPE_USHORT +#undef TYPE_INTN +#undef TYPE_UINTN +#undef TYPE_LONG +#undef TYPE_ULONG +#undef TYPE_LONGLONG +#undef TYPE_ULONGLONG +#undef TYPE_STRING +#undef TYPE_DOUBLE +#undef TYPE_INTSTR +#undef TYPE_POINTER +#undef TYPE_WSTRING +#undef TYPE_UNKNOWN +#undef TYPE_SCHAR +#undef TYPE_UCHAR + +#undef FLAG_LEFT +#undef FLAG_SIGNED +#undef FLAG_SPACED +#undef FLAG_ZEROS +#undef FLAG_NEG diff --git a/mozglue/misc/Printf.h b/mozglue/misc/Printf.h new file mode 100644 index 0000000000..3f4fa2c1bb --- /dev/null +++ b/mozglue/misc/Printf.h @@ -0,0 +1,266 @@ +/* -*- 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/. */ + +/* Printf-like functions, with canned variants that malloc their result. */ + +#ifndef mozilla_Printf_h +#define mozilla_Printf_h + +/* +** API for PR printf like routines. +** +** These exist partly for historical reasons -- initially they were in +** NSPR, then forked in tree and modified in js/ -- but now the prime +** motivation is both closer control over the exact formatting (with +** one exception, see below) and also the ability to control where +** exactly the generated results are sent. +** +** It might seem that this could all be dispensed with in favor of a +** wrapper around |vsnprintf| -- except that this implementation +** guarantees that the %s format will accept a NULL pointer, whereas +** with standard functions this is undefined. +** +** This supports the following formats. It implements a subset of the +** standard formats; due to the use of MOZ_FORMAT_PRINTF, it is not +** permissible to extend the standard, aside from relaxing undefined +** behavior. +** +** %d - decimal +** %u - unsigned decimal +** %x - unsigned hex +** %X - unsigned uppercase hex +** %o - unsigned octal +** %hd, %hu, %hx, %hX, %ho - "short" versions of above +** %ld, %lu, %lx, %lX, %lo - "long" versions of above +** %lld, %llu, %llx, %llX, %llo - "long long" versions of above +** %zd, %zo, %zu, %zx, %zX - size_t versions of above +** %Id, %Io, %Iu, %Ix, %IX - size_t versions of above (for Windows compat). +** Note that MSVC 2015 and newer supports the z length modifier so +** users should prefer using %z instead of %I. We are supporting %I in +** addition to %z in case third-party code that uses %I gets routed to +** use this printf implementation. +** %s - string +** %S, %ls - wide string, that is wchar_t* +** %c - character +** %p - pointer (deals with machine dependent pointer size) +** %f - float; note that this is actually formatted using the +** system's native printf, and so the results may vary +** %g - float; note that this is actually formatted using the +** system's native printf, and so the results may vary +*/ + +#include "mozilla/AllocPolicy.h" +#include "mozilla/Assertions.h" +#include "mozilla/Attributes.h" +#include "mozilla/IntegerPrintfMacros.h" +#include "mozilla/Types.h" +#include "mozilla/UniquePtr.h" + +#include <stdarg.h> +#include <string.h> + +namespace mozilla { + +/* + * This class may be subclassed to provide a way to get the output of + * a printf-like call, as the output is generated. + */ +class PrintfTarget { + public: + /* The Printf-like interface. */ + bool MFBT_API print(const char* format, ...) MOZ_FORMAT_PRINTF(2, 3); + + /* The Vprintf-like interface. */ + bool MFBT_API vprint(const char* fmt, va_list) MOZ_FORMAT_PRINTF(2, 0); + + /* Fast paths for formatting integers as though by %d, %o, %u, or %x. + Since octal and hex formatting always treat numbers as unsigned, there + are no signed overloads for AppendInt{Oct,Hex}. */ + bool MFBT_API appendIntDec(int32_t); + bool MFBT_API appendIntDec(uint32_t); + bool MFBT_API appendIntOct(uint32_t); + bool MFBT_API appendIntHex(uint32_t); + bool MFBT_API appendIntDec(int64_t); + bool MFBT_API appendIntDec(uint64_t); + bool MFBT_API appendIntOct(uint64_t); + bool MFBT_API appendIntHex(uint64_t); + + inline size_t emitted() { return mEmitted; } + + protected: + MFBT_API PrintfTarget(); + virtual ~PrintfTarget() = default; + + /* Subclasses override this. It is called when more output is + available. It may be called with len==0. This should return + true on success, or false on failure. */ + virtual bool append(const char* sp, size_t len) = 0; + + private: + /* Number of bytes emitted so far. */ + size_t mEmitted; + + /* The implementation calls this to emit bytes and update + mEmitted. */ + bool emit(const char* sp, size_t len) { + mEmitted += len; + return append(sp, len); + } + + bool fill2(const char* src, int srclen, int width, int flags); + bool fill_n(const char* src, int srclen, int width, int prec, int type, + int flags); + bool cvt_l(long num, int width, int prec, int radix, int type, int flags, + const char* hexp); + bool cvt_ll(int64_t num, int width, int prec, int radix, int type, int flags, + const char* hexp); + bool cvt_f(double d, char c, int width, int prec, int flags); + bool cvt_s(const char* s, int width, int prec, int flags); +}; + +namespace detail { + +template <typename AllocPolicy = mozilla::MallocAllocPolicy> +struct AllocPolicyBasedFreePolicy { + void operator()(const void* ptr) { + AllocPolicy policy; + policy.free_(const_cast<void*>(ptr)); + } +}; + +} // namespace detail + +// The type returned by Smprintf and friends. +template <typename AllocPolicy> +using SmprintfPolicyPointer = + mozilla::UniquePtr<char, detail::AllocPolicyBasedFreePolicy<AllocPolicy>>; + +// The default type if no alloc policy is specified. +typedef SmprintfPolicyPointer<mozilla::MallocAllocPolicy> SmprintfPointer; + +// Used in the implementation of Smprintf et al. +template <typename AllocPolicy> +class MOZ_STACK_CLASS SprintfState final : private mozilla::PrintfTarget, + private AllocPolicy { + public: + explicit SprintfState(char* base) + : mMaxlen(base ? strlen(base) : 0), + mBase(base), + mCur(base ? base + mMaxlen : 0) {} + + ~SprintfState() { this->free_(mBase); } + + bool vprint(const char* format, va_list ap_list) MOZ_FORMAT_PRINTF(2, 0) { + // The "" here has a single \0 character, which is what we're + // trying to append. + return mozilla::PrintfTarget::vprint(format, ap_list) && append("", 1); + } + + SmprintfPolicyPointer<AllocPolicy> release() { + SmprintfPolicyPointer<AllocPolicy> result(mBase); + mBase = nullptr; + return result; + } + + protected: + bool append(const char* sp, size_t len) override { + ptrdiff_t off; + char* newbase; + size_t newlen; + + off = mCur - mBase; + if (off + len >= mMaxlen) { + /* Grow the buffer */ + newlen = mMaxlen + ((len > 32) ? len : 32); + newbase = this->template maybe_pod_malloc<char>(newlen); + if (!newbase) { + /* Ran out of memory */ + return false; + } + memcpy(newbase, mBase, mMaxlen); + this->free_(mBase); + mBase = newbase; + mMaxlen = newlen; + mCur = mBase + off; + } + + /* Copy data */ + memcpy(mCur, sp, len); + mCur += len; + MOZ_ASSERT(size_t(mCur - mBase) <= mMaxlen); + return true; + } + + private: + size_t mMaxlen; + char* mBase; + char* mCur; +}; + +/* +** sprintf into a malloc'd buffer. Return a pointer to the malloc'd +** buffer on success, nullptr on failure. Call AllocPolicy::free_ to release +** the memory returned. +*/ +template <typename AllocPolicy = mozilla::MallocAllocPolicy> +MOZ_FORMAT_PRINTF(1, 2) +SmprintfPolicyPointer<AllocPolicy> Smprintf(const char* fmt, ...) { + SprintfState<AllocPolicy> ss(nullptr); + va_list ap; + va_start(ap, fmt); + bool r = ss.vprint(fmt, ap); + va_end(ap); + if (!r) { + return nullptr; + } + return ss.release(); +} + +/* +** "append" sprintf into a malloc'd buffer. "last" is the last value of +** the malloc'd buffer. sprintf will append data to the end of last, +** growing it as necessary using realloc. If last is nullptr, SmprintfAppend +** will allocate the initial string. The return value is the new value of +** last for subsequent calls, or nullptr if there is a malloc failure. +*/ +template <typename AllocPolicy = mozilla::MallocAllocPolicy> +MOZ_FORMAT_PRINTF(2, 3) +SmprintfPolicyPointer<AllocPolicy> SmprintfAppend( + SmprintfPolicyPointer<AllocPolicy>&& last, const char* fmt, ...) { + SprintfState<AllocPolicy> ss(last.release()); + va_list ap; + va_start(ap, fmt); + bool r = ss.vprint(fmt, ap); + va_end(ap); + if (!r) { + return nullptr; + } + return ss.release(); +} + +/* +** va_list forms of the above. +*/ +template <typename AllocPolicy = mozilla::MallocAllocPolicy> +MOZ_FORMAT_PRINTF(1, 0) +SmprintfPolicyPointer<AllocPolicy> Vsmprintf(const char* fmt, va_list ap) { + SprintfState<AllocPolicy> ss(nullptr); + if (!ss.vprint(fmt, ap)) return nullptr; + return ss.release(); +} + +template <typename AllocPolicy = mozilla::MallocAllocPolicy> +MOZ_FORMAT_PRINTF(2, 0) +SmprintfPolicyPointer<AllocPolicy> VsmprintfAppend( + SmprintfPolicyPointer<AllocPolicy>&& last, const char* fmt, va_list ap) { + SprintfState<AllocPolicy> ss(last.release()); + if (!ss.vprint(fmt, ap)) return nullptr; + return ss.release(); +} + +} // namespace mozilla + +#endif /* mozilla_Printf_h */ diff --git a/mozglue/misc/ProcessType.cpp b/mozglue/misc/ProcessType.cpp new file mode 100644 index 0000000000..031d242328 --- /dev/null +++ b/mozglue/misc/ProcessType.cpp @@ -0,0 +1,58 @@ +/* -*- 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 "ProcessType.h" + +#include <cstring> + +#include "mozilla/Assertions.h" + +using namespace mozilla::startup; + +namespace mozilla { +namespace startup { +GeckoProcessType sChildProcessType = GeckoProcessType_Default; +} // namespace startup + +void SetGeckoProcessType(const char* aProcessTypeString) { +#if !defined(DEBUG) + // If not a DEBUG build then just return if sChildProcessType has already been + // set and is not fork server. In DEBUG builds we will check that process type + // matches the one already set if it is not fork server. + if (sChildProcessType != GeckoProcessType_Default && + sChildProcessType != GeckoProcessType_ForkServer) { + return; + } +#endif + +#define GECKO_PROCESS_TYPE(enum_value, enum_name, string_name, proc_typename, \ + process_bin_type, procinfo_typename, \ + webidl_typename, allcaps_name) \ + if (std::strcmp(aProcessTypeString, string_name) == 0) { \ + MOZ_ASSERT_IF( \ + sChildProcessType != GeckoProcessType_Default && \ + sChildProcessType != GeckoProcessType_ForkServer, \ + sChildProcessType == GeckoProcessType::GeckoProcessType_##enum_name); \ + sChildProcessType = GeckoProcessType::GeckoProcessType_##enum_name; \ + return; \ + } +#define SKIP_PROCESS_TYPE_DEFAULT +#if !defined(MOZ_ENABLE_FORKSERVER) +# define SKIP_PROCESS_TYPE_FORKSERVER +#endif +#if !defined(ENABLE_TESTS) +# define SKIP_PROCESS_TYPE_IPDLUNITTEST +#endif +#include "mozilla/GeckoProcessTypes.h" +#undef SKIP_PROCESS_TYPE_IPDLUNITTEST +#undef SKIP_PROCESS_TYPE_FORKSERVER +#undef SKIP_PROCESS_TYPE_DEFAULT +#undef GECKO_PROCESS_TYPE + + MOZ_CRASH("aProcessTypeString is not valid."); +} + +} // namespace mozilla diff --git a/mozglue/misc/ProcessType.h b/mozglue/misc/ProcessType.h new file mode 100644 index 0000000000..890ed4faac --- /dev/null +++ b/mozglue/misc/ProcessType.h @@ -0,0 +1,44 @@ +/* -*- 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/. */ + +#ifndef IPC_PROCESSTYPE_H_ +#define IPC_PROCESSTYPE_H_ + +#include "mozilla/Attributes.h" +#include "mozilla/Types.h" + +// This enum is not dense. See GeckoProcessTypes.h for details. +enum GeckoProcessType { +#define GECKO_PROCESS_TYPE(enum_value, enum_name, string_name, proc_typename, \ + process_bin_type, procinfo_typename, \ + webidl_typename, allcaps_name) \ + GeckoProcessType_##enum_name = (enum_value), +#include "mozilla/GeckoProcessTypes.h" +#undef GECKO_PROCESS_TYPE + GeckoProcessType_End, + GeckoProcessType_Invalid = GeckoProcessType_End +}; + +namespace mozilla { +namespace startup { +extern MFBT_DATA GeckoProcessType sChildProcessType; +} // namespace startup + +/** + * @return the GeckoProcessType of the current process. + */ +MOZ_ALWAYS_INLINE GeckoProcessType GetGeckoProcessType() { + return startup::sChildProcessType; +} + +/** + * Set the gecko process type based on a null-terminated byte string. + */ +MFBT_API void SetGeckoProcessType(const char* aProcessTypeString); + +} // namespace mozilla + +#endif // IPC_PROCESSTYPE_H_ diff --git a/mozglue/misc/RWLock_posix.cpp b/mozglue/misc/RWLock_posix.cpp new file mode 100644 index 0000000000..970bddd1aa --- /dev/null +++ b/mozglue/misc/RWLock_posix.cpp @@ -0,0 +1,64 @@ +/* -*- 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/. */ + +#ifdef XP_WIN +# error This file should only be compiled on non-Windows platforms. +#endif + +#include "mozilla/PlatformRWLock.h" + +#include "mozilla/Assertions.h" + +#include <errno.h> + +// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init) +mozilla::detail::RWLockImpl::RWLockImpl() { + MOZ_RELEASE_ASSERT(pthread_rwlock_init(&mRWLock, nullptr) == 0, + "pthread_rwlock_init failed"); +} + +mozilla::detail::RWLockImpl::~RWLockImpl() { + MOZ_RELEASE_ASSERT(pthread_rwlock_destroy(&mRWLock) == 0, + "pthread_rwlock_destroy failed"); +} + +bool mozilla::detail::RWLockImpl::tryReadLock() { + int rv = pthread_rwlock_tryrdlock(&mRWLock); + // We allow EDEADLK here because it has been observed returned on macos when + // the write lock is held by the current thread. + MOZ_RELEASE_ASSERT(rv == 0 || rv == EBUSY || rv == EDEADLK, + "pthread_rwlock_tryrdlock failed"); + return rv == 0; +} + +void mozilla::detail::RWLockImpl::readLock() { + MOZ_RELEASE_ASSERT(pthread_rwlock_rdlock(&mRWLock) == 0, + "pthread_rwlock_rdlock failed"); +} + +void mozilla::detail::RWLockImpl::readUnlock() { + MOZ_RELEASE_ASSERT(pthread_rwlock_unlock(&mRWLock) == 0, + "pthread_rwlock_unlock failed"); +} + +bool mozilla::detail::RWLockImpl::tryWriteLock() { + int rv = pthread_rwlock_trywrlock(&mRWLock); + // We allow EDEADLK here because it has been observed returned on macos when + // the write lock is held by the current thread. + MOZ_RELEASE_ASSERT(rv == 0 || rv == EBUSY || rv == EDEADLK, + "pthread_rwlock_trywrlock failed"); + return rv == 0; +} + +void mozilla::detail::RWLockImpl::writeLock() { + MOZ_RELEASE_ASSERT(pthread_rwlock_wrlock(&mRWLock) == 0, + "pthread_rwlock_wrlock failed"); +} + +void mozilla::detail::RWLockImpl::writeUnlock() { + MOZ_RELEASE_ASSERT(pthread_rwlock_unlock(&mRWLock) == 0, + "pthread_rwlock_unlock failed"); +} diff --git a/mozglue/misc/RWLock_windows.cpp b/mozglue/misc/RWLock_windows.cpp new file mode 100644 index 0000000000..2c347f218c --- /dev/null +++ b/mozglue/misc/RWLock_windows.cpp @@ -0,0 +1,48 @@ +/* -*- 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/. */ + +#ifndef XP_WIN +# error This file should only be compiled on Windows. +#endif + +#include "mozilla/PlatformRWLock.h" + +#include <windows.h> + +#define NativeHandle(m) (reinterpret_cast<SRWLOCK*>(&m)) + +mozilla::detail::RWLockImpl::RWLockImpl() { + static_assert(sizeof(SRWLOCK) <= sizeof(mRWLock), "SRWLOCK is too big!"); + InitializeSRWLock(NativeHandle(mRWLock)); +} + +mozilla::detail::RWLockImpl::~RWLockImpl() {} + +bool mozilla::detail::RWLockImpl::tryReadLock() { + return TryAcquireSRWLockShared(NativeHandle(mRWLock)); +} + +void mozilla::detail::RWLockImpl::readLock() { + AcquireSRWLockShared(NativeHandle(mRWLock)); +} + +void mozilla::detail::RWLockImpl::readUnlock() { + ReleaseSRWLockShared(NativeHandle(mRWLock)); +} + +bool mozilla::detail::RWLockImpl::tryWriteLock() { + return TryAcquireSRWLockExclusive(NativeHandle(mRWLock)); +} + +void mozilla::detail::RWLockImpl::writeLock() { + AcquireSRWLockExclusive(NativeHandle(mRWLock)); +} + +void mozilla::detail::RWLockImpl::writeUnlock() { + ReleaseSRWLockExclusive(NativeHandle(mRWLock)); +} + +#undef NativeHandle diff --git a/mozglue/misc/RuntimeExceptionModule.cpp b/mozglue/misc/RuntimeExceptionModule.cpp new file mode 100644 index 0000000000..6b7e55c5e9 --- /dev/null +++ b/mozglue/misc/RuntimeExceptionModule.cpp @@ -0,0 +1,111 @@ +/* -*- 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 "RuntimeExceptionModule.h" + +#include <cstdint> + +#include "mozilla/ProcessType.h" + +#if defined(XP_WIN) +# include <windows.h> +# if defined(__MINGW32__) || defined(__MINGW64__) +// Add missing constants and types for mingw builds +# define HREPORT HANDLE +# define PWER_SUBMIT_RESULT WER_SUBMIT_RESULT* +# define WER_MAX_PREFERRED_MODULES_BUFFER (256) +# endif // defined(__MINGW32__) || defined(__MINGW64__) +# include <werapi.h> // For WerRegisterRuntimeExceptionModule() +# include <stdlib.h> + +# include "mozilla/mozalloc_oom.h" +# include "mozilla/Unused.h" + +using mozilla::Unused; +#endif + +namespace CrashReporter { + +#ifdef XP_WIN + +struct InProcessWindowsErrorReportingData { + uint32_t mProcessType; + size_t* mOOMAllocationSizePtr; +}; + +static InProcessWindowsErrorReportingData gInProcessWerData; +const static size_t kModulePathLength = MAX_PATH + 1; +static wchar_t sModulePath[kModulePathLength]; + +bool GetRuntimeExceptionModulePath(wchar_t* aPath, const size_t aLength) { + const wchar_t* kModuleName = L"mozwer.dll"; + DWORD res = ::GetModuleFileNameW(nullptr, aPath, aLength); + if ((res > 0) && (res != aLength)) { + wchar_t* last_backslash = wcsrchr(aPath, L'\\'); + if (last_backslash) { + *(last_backslash + 1) = L'\0'; + if (wcscat_s(aPath, aLength, kModuleName) == 0) { + return true; + } + } + } + + return false; +} + +#endif // XP_WIN + +void RegisterRuntimeExceptionModule() { +#ifdef XP_WIN +# if defined(DEBUG) + // In debug builds, disable the crash reporter by default, and allow to + // enable it with the MOZ_CRASHREPORTER environment variable. + const char* envvar = getenv("MOZ_CRASHREPORTER"); + if (!envvar || !*envvar) { + return; + } +# else + // In other builds, enable the crash reporter by default, and allow + // disabling it with the MOZ_CRASHREPORTER_DISABLE environment variable. + const char* envvar = getenv("MOZ_CRASHREPORTER_DISABLE"); + if (envvar && *envvar) { + return; + } +# endif + + // If sModulePath is set we have already registerd the module. + if (*sModulePath) { + return; + } + + // If we fail to get the path just return. + if (!GetRuntimeExceptionModulePath(sModulePath, kModulePathLength)) { + return; + } + + gInProcessWerData.mProcessType = mozilla::GetGeckoProcessType(); + gInProcessWerData.mOOMAllocationSizePtr = &gOOMAllocationSize; + if (FAILED(::WerRegisterRuntimeExceptionModule(sModulePath, + &gInProcessWerData))) { + // The registration failed null out sModulePath to record this. + *sModulePath = L'\0'; + return; + } +#endif // XP_WIN +} + +void UnregisterRuntimeExceptionModule() { +#ifdef XP_WIN + // If sModulePath is set then we have registered the module. + if (*sModulePath) { + Unused << ::WerUnregisterRuntimeExceptionModule(sModulePath, + &gInProcessWerData); + *sModulePath = L'\0'; + } +#endif // XP_WIN +} + +} // namespace CrashReporter diff --git a/mozglue/misc/RuntimeExceptionModule.h b/mozglue/misc/RuntimeExceptionModule.h new file mode 100644 index 0000000000..47468e4f36 --- /dev/null +++ b/mozglue/misc/RuntimeExceptionModule.h @@ -0,0 +1,20 @@ +/* -*- 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/. */ + +#ifndef RUNTIMEEXCEPTIONMODULE_H_ +#define RUNTIMEEXCEPTIONMODULE_H_ + +#include "mozilla/Types.h" + +namespace CrashReporter { + +MFBT_API void RegisterRuntimeExceptionModule(); + +MFBT_API void UnregisterRuntimeExceptionModule(); + +} // namespace CrashReporter + +#endif // RUNTIMEEXCEPTIONMODULE_H_ diff --git a/mozglue/misc/SIMD.cpp b/mozglue/misc/SIMD.cpp new file mode 100644 index 0000000000..3893de57b3 --- /dev/null +++ b/mozglue/misc/SIMD.cpp @@ -0,0 +1,565 @@ +/* -*- 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 "mozilla/SIMD.h" + +#include <cstring> +#include <stdint.h> +#include <type_traits> + +#include "mozilla/EndianUtils.h" +#include "mozilla/SSE.h" + +#ifdef MOZILLA_PRESUME_SSE2 + +# include <immintrin.h> + +#endif + +namespace mozilla { + +template <typename TValue> +const TValue* FindInBufferNaive(const TValue* ptr, TValue value, + size_t length) { + const TValue* end = ptr + length; + while (ptr < end) { + if (*ptr == value) { + return ptr; + } + ptr++; + } + return nullptr; +} + +#ifdef MOZILLA_PRESUME_SSE2 + +const __m128i* Cast128(uintptr_t ptr) { + return reinterpret_cast<const __m128i*>(ptr); +} + +template <typename T> +T GetAs(uintptr_t ptr) { + return *reinterpret_cast<const T*>(ptr); +} + +// Akin to ceil/floor, AlignDown/AlignUp will return the original pointer if it +// is already aligned. +uintptr_t AlignDown16(uintptr_t ptr) { return ptr & ~0xf; } + +uintptr_t AlignUp16(uintptr_t ptr) { return AlignDown16(ptr + 0xf); } + +template <typename TValue> +__m128i CmpEq128(__m128i a, __m128i b) { + static_assert(sizeof(TValue) == 1 || sizeof(TValue) == 2); + if (sizeof(TValue) == 1) { + return _mm_cmpeq_epi8(a, b); + } + return _mm_cmpeq_epi16(a, b); +} + +# ifdef __GNUC__ + +// Earlier versions of GCC are missing the _mm_loadu_si32 instruction. This +// workaround from Peter Cordes (https://stackoverflow.com/a/72837992) compiles +// down to the same instructions. We could just replace _mm_loadu_si32 +__m128i Load32BitsIntoXMM(uintptr_t ptr) { + int tmp; + memcpy(&tmp, reinterpret_cast<const void*>(ptr), + sizeof(tmp)); // unaligned aliasing-safe load + return _mm_cvtsi32_si128(tmp); // efficient on GCC/clang/MSVC +} + +# else + +__m128i Load32BitsIntoXMM(uintptr_t ptr) { + return _mm_loadu_si32(Cast128(ptr)); +} + +# endif + +const char* Check4x4Chars(__m128i needle, uintptr_t a, uintptr_t b, uintptr_t c, + uintptr_t d) { + __m128i haystackA = Load32BitsIntoXMM(a); + __m128i cmpA = CmpEq128<char>(needle, haystackA); + __m128i haystackB = Load32BitsIntoXMM(b); + __m128i cmpB = CmpEq128<char>(needle, haystackB); + __m128i haystackC = Load32BitsIntoXMM(c); + __m128i cmpC = CmpEq128<char>(needle, haystackC); + __m128i haystackD = Load32BitsIntoXMM(d); + __m128i cmpD = CmpEq128<char>(needle, haystackD); + __m128i or_ab = _mm_or_si128(cmpA, cmpB); + __m128i or_cd = _mm_or_si128(cmpC, cmpD); + __m128i or_abcd = _mm_or_si128(or_ab, or_cd); + int orMask = _mm_movemask_epi8(or_abcd); + if (orMask & 0xf) { + int cmpMask; + cmpMask = _mm_movemask_epi8(cmpA); + if (cmpMask & 0xf) { + return reinterpret_cast<const char*>(a + __builtin_ctz(cmpMask)); + } + cmpMask = _mm_movemask_epi8(cmpB); + if (cmpMask & 0xf) { + return reinterpret_cast<const char*>(b + __builtin_ctz(cmpMask)); + } + cmpMask = _mm_movemask_epi8(cmpC); + if (cmpMask & 0xf) { + return reinterpret_cast<const char*>(c + __builtin_ctz(cmpMask)); + } + cmpMask = _mm_movemask_epi8(cmpD); + if (cmpMask & 0xf) { + return reinterpret_cast<const char*>(d + __builtin_ctz(cmpMask)); + } + } + + return nullptr; +} + +template <typename TValue> +const TValue* Check4x16Bytes(__m128i needle, uintptr_t a, uintptr_t b, + uintptr_t c, uintptr_t d) { + __m128i haystackA = _mm_loadu_si128(Cast128(a)); + __m128i cmpA = CmpEq128<TValue>(needle, haystackA); + __m128i haystackB = _mm_loadu_si128(Cast128(b)); + __m128i cmpB = CmpEq128<TValue>(needle, haystackB); + __m128i haystackC = _mm_loadu_si128(Cast128(c)); + __m128i cmpC = CmpEq128<TValue>(needle, haystackC); + __m128i haystackD = _mm_loadu_si128(Cast128(d)); + __m128i cmpD = CmpEq128<TValue>(needle, haystackD); + __m128i or_ab = _mm_or_si128(cmpA, cmpB); + __m128i or_cd = _mm_or_si128(cmpC, cmpD); + __m128i or_abcd = _mm_or_si128(or_ab, or_cd); + int orMask = _mm_movemask_epi8(or_abcd); + if (orMask) { + int cmpMask; + cmpMask = _mm_movemask_epi8(cmpA); + if (cmpMask) { + return reinterpret_cast<const TValue*>(a + __builtin_ctz(cmpMask)); + } + cmpMask = _mm_movemask_epi8(cmpB); + if (cmpMask) { + return reinterpret_cast<const TValue*>(b + __builtin_ctz(cmpMask)); + } + cmpMask = _mm_movemask_epi8(cmpC); + if (cmpMask) { + return reinterpret_cast<const TValue*>(c + __builtin_ctz(cmpMask)); + } + cmpMask = _mm_movemask_epi8(cmpD); + if (cmpMask) { + return reinterpret_cast<const TValue*>(d + __builtin_ctz(cmpMask)); + } + } + + return nullptr; +} + +enum class HaystackOverlap { + Overlapping, + Sequential, +}; + +// Check two 16-byte chunks for the two-byte sequence loaded into needle1 +// followed by needle1. `carryOut` is an optional pointer which we will +// populate based on whether the last character of b matches needle1. This +// should be provided on subsequent calls via `carryIn` so we can detect cases +// where the last byte of b's 16-byte chunk is needle1 and the first byte of +// the next a's 16-byte chunk is needle2. `overlap` and whether +// `carryIn`/`carryOut` are NULL should be knowable at compile time to avoid +// branching. +template <typename TValue> +const TValue* Check2x2x16Bytes(__m128i needle1, __m128i needle2, uintptr_t a, + uintptr_t b, __m128i* carryIn, __m128i* carryOut, + HaystackOverlap overlap) { + const int shiftRightAmount = 16 - sizeof(TValue); + const int shiftLeftAmount = sizeof(TValue); + __m128i haystackA = _mm_loadu_si128(Cast128(a)); + __m128i cmpA1 = CmpEq128<TValue>(needle1, haystackA); + __m128i cmpA2 = CmpEq128<TValue>(needle2, haystackA); + __m128i cmpA; + if (carryIn) { + cmpA = _mm_and_si128( + _mm_or_si128(_mm_bslli_si128(cmpA1, shiftLeftAmount), *carryIn), cmpA2); + } else { + cmpA = _mm_and_si128(_mm_bslli_si128(cmpA1, shiftLeftAmount), cmpA2); + } + __m128i haystackB = _mm_loadu_si128(Cast128(b)); + __m128i cmpB1 = CmpEq128<TValue>(needle1, haystackB); + __m128i cmpB2 = CmpEq128<TValue>(needle2, haystackB); + __m128i cmpB; + if (overlap == HaystackOverlap::Overlapping) { + cmpB = _mm_and_si128(_mm_bslli_si128(cmpB1, shiftLeftAmount), cmpB2); + } else { + MOZ_ASSERT(overlap == HaystackOverlap::Sequential); + __m128i carryAB = _mm_bsrli_si128(cmpA1, shiftRightAmount); + cmpB = _mm_and_si128( + _mm_or_si128(_mm_bslli_si128(cmpB1, shiftLeftAmount), carryAB), cmpB2); + } + __m128i or_ab = _mm_or_si128(cmpA, cmpB); + int orMask = _mm_movemask_epi8(or_ab); + if (orMask) { + int cmpMask; + cmpMask = _mm_movemask_epi8(cmpA); + if (cmpMask) { + return reinterpret_cast<const TValue*>(a + __builtin_ctz(cmpMask) - + shiftLeftAmount); + } + cmpMask = _mm_movemask_epi8(cmpB); + if (cmpMask) { + return reinterpret_cast<const TValue*>(b + __builtin_ctz(cmpMask) - + shiftLeftAmount); + } + } + + if (carryOut) { + _mm_store_si128(carryOut, _mm_bsrli_si128(cmpB1, shiftRightAmount)); + } + + return nullptr; +} + +template <typename TValue> +const TValue* FindInBuffer(const TValue* ptr, TValue value, size_t length) { + static_assert(sizeof(TValue) == 1 || sizeof(TValue) == 2); + static_assert(std::is_unsigned<TValue>::value); + uint64_t splat64; + if (sizeof(TValue) == 1) { + splat64 = 0x0101010101010101llu; + } else { + splat64 = 0x0001000100010001llu; + } + + // Load our needle into a 16-byte register + uint64_t u64_value = static_cast<uint64_t>(value) * splat64; + int64_t i64_value = *reinterpret_cast<int64_t*>(&u64_value); + __m128i needle = _mm_set_epi64x(i64_value, i64_value); + + size_t numBytes = length * sizeof(TValue); + uintptr_t cur = reinterpret_cast<uintptr_t>(ptr); + uintptr_t end = cur + numBytes; + + if ((sizeof(TValue) > 1 && numBytes < 16) || numBytes < 4) { + while (cur < end) { + if (GetAs<TValue>(cur) == value) { + return reinterpret_cast<const TValue*>(cur); + } + cur += sizeof(TValue); + } + return nullptr; + } + + if (numBytes < 16) { + // NOTE: here and below, we have some bit fiddling which could look a + // little weird. The important thing to note though is it's just a trick + // for getting the number 4 if numBytes is greater than or equal to 8, + // and 0 otherwise. This lets us fully cover the range without any + // branching for the case where numBytes is in [4,8), and [8,16). We get + // four ranges from this - if numbytes > 8, we get: + // [0,4), [4,8], [end - 8), [end - 4) + // and if numbytes < 8, we get + // [0,4), [0,4), [end - 4), [end - 4) + uintptr_t a = cur; + uintptr_t b = cur + ((numBytes & 8) >> 1); + uintptr_t c = end - 4 - ((numBytes & 8) >> 1); + uintptr_t d = end - 4; + const char* charResult = Check4x4Chars(needle, a, b, c, d); + // Note: we ensure above that sizeof(TValue) == 1 here, so this is + // either char to char or char to something like a uint8_t. + return reinterpret_cast<const TValue*>(charResult); + } + + if (numBytes < 64) { + // NOTE: see the above explanation of the similar chunk of code, but in + // this case, replace 8 with 32 and 4 with 16. + uintptr_t a = cur; + uintptr_t b = cur + ((numBytes & 32) >> 1); + uintptr_t c = end - 16 - ((numBytes & 32) >> 1); + uintptr_t d = end - 16; + return Check4x16Bytes<TValue>(needle, a, b, c, d); + } + + // Get the initial unaligned load out of the way. This will overlap with the + // aligned stuff below, but the overlapped part should effectively be free + // (relative to a mispredict from doing a byte-by-byte loop). + __m128i haystack = _mm_loadu_si128(Cast128(cur)); + __m128i cmp = CmpEq128<TValue>(needle, haystack); + int cmpMask = _mm_movemask_epi8(cmp); + if (cmpMask) { + return reinterpret_cast<const TValue*>(cur + __builtin_ctz(cmpMask)); + } + + // Now we're working with aligned memory. Hooray! \o/ + cur = AlignUp16(cur); + + // The address of the final 48-63 bytes. We overlap this with what we check in + // our hot loop below to avoid branching. Again, the overlap should be + // negligible compared with a branch mispredict. + uintptr_t tailStartPtr = AlignDown16(end - 48); + uintptr_t tailEndPtr = end - 16; + + while (cur < tailStartPtr) { + uintptr_t a = cur; + uintptr_t b = cur + 16; + uintptr_t c = cur + 32; + uintptr_t d = cur + 48; + const TValue* result = Check4x16Bytes<TValue>(needle, a, b, c, d); + if (result) { + return result; + } + cur += 64; + } + + uintptr_t a = tailStartPtr; + uintptr_t b = tailStartPtr + 16; + uintptr_t c = tailStartPtr + 32; + uintptr_t d = tailEndPtr; + return Check4x16Bytes<TValue>(needle, a, b, c, d); +} + +template <typename TValue> +const TValue* TwoElementLoop(uintptr_t start, uintptr_t end, TValue v1, + TValue v2) { + static_assert(sizeof(TValue) == 1 || sizeof(TValue) == 2); + + const TValue* cur = reinterpret_cast<const TValue*>(start); + const TValue* preEnd = reinterpret_cast<const TValue*>(end - sizeof(TValue)); + + uint32_t expected = static_cast<uint32_t>(v1) | + (static_cast<uint32_t>(v2) << (sizeof(TValue) * 8)); + while (cur < preEnd) { + // NOTE: this should only ever be called on little endian architectures. + static_assert(MOZ_LITTLE_ENDIAN()); + // We or cur[0] and cur[1] together explicitly and compare to expected, + // in order to avoid UB from just loading them as a uint16_t/uint32_t. + // However, it will compile down the same code after optimizations on + // little endian systems which support unaligned loads. Comparing them + // value-by-value, however, will not, and seems to perform worse in local + // microbenchmarking. Even after bitwise or'ing the comparison values + // together to avoid the short circuit, the compiler doesn't seem to get + // the hint and creates two branches, the first of which might be + // frequently mispredicted. + uint32_t actual = static_cast<uint32_t>(cur[0]) | + (static_cast<uint32_t>(cur[1]) << (sizeof(TValue) * 8)); + if (actual == expected) { + return cur; + } + cur++; + } + return nullptr; +} + +template <typename TValue> +const TValue* FindTwoInBuffer(const TValue* ptr, TValue v1, TValue v2, + size_t length) { + static_assert(sizeof(TValue) == 1 || sizeof(TValue) == 2); + static_assert(std::is_unsigned<TValue>::value); + uint64_t splat64; + if (sizeof(TValue) == 1) { + splat64 = 0x0101010101010101llu; + } else { + splat64 = 0x0001000100010001llu; + } + + // Load our needle into a 16-byte register + uint64_t u64_v1 = static_cast<uint64_t>(v1) * splat64; + int64_t i64_v1 = *reinterpret_cast<int64_t*>(&u64_v1); + __m128i needle1 = _mm_set_epi64x(i64_v1, i64_v1); + uint64_t u64_v2 = static_cast<uint64_t>(v2) * splat64; + int64_t i64_v2 = *reinterpret_cast<int64_t*>(&u64_v2); + __m128i needle2 = _mm_set_epi64x(i64_v2, i64_v2); + + size_t numBytes = length * sizeof(TValue); + uintptr_t cur = reinterpret_cast<uintptr_t>(ptr); + uintptr_t end = cur + numBytes; + + if (numBytes < 16) { + return TwoElementLoop<TValue>(cur, end, v1, v2); + } + + if (numBytes < 32) { + uintptr_t a = cur; + uintptr_t b = end - 16; + return Check2x2x16Bytes<TValue>(needle1, needle2, a, b, nullptr, nullptr, + HaystackOverlap::Overlapping); + } + + // Get the initial unaligned load out of the way. This will likely overlap + // with the aligned stuff below, but the overlapped part should effectively + // be free. + __m128i haystack = _mm_loadu_si128(Cast128(cur)); + __m128i cmp1 = CmpEq128<TValue>(needle1, haystack); + __m128i cmp2 = CmpEq128<TValue>(needle2, haystack); + int cmpMask1 = _mm_movemask_epi8(cmp1); + int cmpMask2 = _mm_movemask_epi8(cmp2); + int cmpMask = (cmpMask1 << sizeof(TValue)) & cmpMask2; + if (cmpMask) { + return reinterpret_cast<const TValue*>(cur + __builtin_ctz(cmpMask) - + sizeof(TValue)); + } + + // Now we're working with aligned memory. Hooray! \o/ + cur = AlignUp16(cur); + + // The address of the final 48-63 bytes. We overlap this with what we check in + // our hot loop below to avoid branching. Again, the overlap should be + // negligible compared with a branch mispredict. + uintptr_t tailEndPtr = end - 16; + uintptr_t tailStartPtr = AlignDown16(tailEndPtr); + + __m128i cmpMaskCarry = _mm_set1_epi32(0); + while (cur < tailStartPtr) { + uintptr_t a = cur; + uintptr_t b = cur + 16; + const TValue* result = + Check2x2x16Bytes<TValue>(needle1, needle2, a, b, &cmpMaskCarry, + &cmpMaskCarry, HaystackOverlap::Sequential); + if (result) { + return result; + } + cur += 32; + } + + uint32_t carry = (cur == tailStartPtr) ? 0xffffffff : 0; + __m128i wideCarry = Load32BitsIntoXMM(reinterpret_cast<uintptr_t>(&carry)); + cmpMaskCarry = _mm_and_si128(cmpMaskCarry, wideCarry); + uintptr_t a = tailStartPtr; + uintptr_t b = tailEndPtr; + return Check2x2x16Bytes<TValue>(needle1, needle2, a, b, &cmpMaskCarry, + nullptr, HaystackOverlap::Overlapping); +} + +const char* SIMD::memchr8SSE2(const char* ptr, char value, size_t length) { + // Signed chars are just really annoying to do bit logic with. Convert to + // unsigned at the outermost scope so we don't have to worry about it. + const unsigned char* uptr = reinterpret_cast<const unsigned char*>(ptr); + unsigned char uvalue = static_cast<unsigned char>(value); + const unsigned char* uresult = + FindInBuffer<unsigned char>(uptr, uvalue, length); + return reinterpret_cast<const char*>(uresult); +} + +// So, this is a bit awkward. It generally simplifies things if we can just +// assume all the AVX2 code is 64-bit, so we have this preprocessor guard +// in SIMD_avx2 over all of its actual code, and it also defines versions +// of its endpoints that just assert false if the guard is not satisfied. +// A 32 bit processor could implement the AVX2 instruction set though, which +// would result in it passing the supports_avx2() check and landing in an +// assertion failure. Accordingly, we just don't allow that to happen. We +// are not particularly concerned about ensuring that newer 32 bit processors +// get access to the AVX2 functions exposed here. +# if defined(MOZILLA_MAY_SUPPORT_AVX2) && defined(__x86_64__) + +bool SupportsAVX2() { return supports_avx2(); } + +# else + +bool SupportsAVX2() { return false; } + +# endif + +const char* SIMD::memchr8(const char* ptr, char value, size_t length) { + if (SupportsAVX2()) { + return memchr8AVX2(ptr, value, length); + } + return memchr8SSE2(ptr, value, length); +} + +const char16_t* SIMD::memchr16SSE2(const char16_t* ptr, char16_t value, + size_t length) { + return FindInBuffer<char16_t>(ptr, value, length); +} + +const char16_t* SIMD::memchr16(const char16_t* ptr, char16_t value, + size_t length) { + if (SupportsAVX2()) { + return memchr16AVX2(ptr, value, length); + } + return memchr16SSE2(ptr, value, length); +} + +const uint64_t* SIMD::memchr64(const uint64_t* ptr, uint64_t value, + size_t length) { + if (SupportsAVX2()) { + return memchr64AVX2(ptr, value, length); + } + return FindInBufferNaive<uint64_t>(ptr, value, length); +} + +const char* SIMD::memchr2x8(const char* ptr, char v1, char v2, size_t length) { + // Signed chars are just really annoying to do bit logic with. Convert to + // unsigned at the outermost scope so we don't have to worry about it. + const unsigned char* uptr = reinterpret_cast<const unsigned char*>(ptr); + unsigned char uv1 = static_cast<unsigned char>(v1); + unsigned char uv2 = static_cast<unsigned char>(v2); + const unsigned char* uresult = + FindTwoInBuffer<unsigned char>(uptr, uv1, uv2, length); + return reinterpret_cast<const char*>(uresult); +} + +const char16_t* SIMD::memchr2x16(const char16_t* ptr, char16_t v1, char16_t v2, + size_t length) { + return FindTwoInBuffer<char16_t>(ptr, v1, v2, length); +} + +#else + +const char* SIMD::memchr8(const char* ptr, char value, size_t length) { + const void* result = ::memchr(reinterpret_cast<const void*>(ptr), + static_cast<int>(value), length); + return reinterpret_cast<const char*>(result); +} + +const char* SIMD::memchr8SSE2(const char* ptr, char value, size_t length) { + return memchr8(ptr, value, length); +} + +const char16_t* SIMD::memchr16(const char16_t* ptr, char16_t value, + size_t length) { + return FindInBufferNaive<char16_t>(ptr, value, length); +} + +const char16_t* SIMD::memchr16SSE2(const char16_t* ptr, char16_t value, + size_t length) { + return memchr16(ptr, value, length); +} + +const uint64_t* SIMD::memchr64(const uint64_t* ptr, uint64_t value, + size_t length) { + return FindInBufferNaive<uint64_t>(ptr, value, length); +} + +const char* SIMD::memchr2x8(const char* ptr, char v1, char v2, size_t length) { + const char* end = ptr + length - 1; + while (ptr < end) { + ptr = memchr8(ptr, v1, end - ptr); + if (!ptr) { + return nullptr; + } + if (ptr[1] == v2) { + return ptr; + } + ptr++; + } + return nullptr; +} + +const char16_t* SIMD::memchr2x16(const char16_t* ptr, char16_t v1, char16_t v2, + size_t length) { + const char16_t* end = ptr + length - 1; + while (ptr < end) { + ptr = memchr16(ptr, v1, end - ptr); + if (!ptr) { + return nullptr; + } + if (ptr[1] == v2) { + return ptr; + } + ptr++; + } + return nullptr; +} + +#endif + +} // namespace mozilla diff --git a/mozglue/misc/SIMD.h b/mozglue/misc/SIMD.h new file mode 100644 index 0000000000..3d17185656 --- /dev/null +++ b/mozglue/misc/SIMD.h @@ -0,0 +1,81 @@ +/* -*- 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/. */ + +#ifndef mozilla_SIMD_h +#define mozilla_SIMD_h + +#include "mozilla/Types.h" + +namespace mozilla { +// A collection of SIMD-implemented algorithms. Some of these exist in the CRT. +// However, the quality of the C runtime implementation varies wildly across +// platforms, so these should at least ensure consistency. +// +// NOTE: these are currently only implemented with hand-written SIMD for x86 +// and AMD64 platforms, and fallback to the the C runtime or naive loops on +// other architectures. Please consider this before switching an already +// optimized loop to these helpers. +class SIMD { + public: + // NOTE: for memchr we have a goofy void* signature just to be an easy drop + // in replacement for the CRT version. We also give memchr8 which is just a + // typed version of memchr. + static const void* memchr(const void* ptr, int value, size_t num) { + return memchr8(reinterpret_cast<const char*>(ptr), static_cast<char>(value), + num); + } + + // Search through `ptr[0..length]` for the first occurrence of `value` and + // return the pointer to it, or nullptr if it cannot be found. + static MFBT_API const char* memchr8(const char* ptr, char value, + size_t length); + + // This function just restricts our execution to the SSE2 path + static MFBT_API const char* memchr8SSE2(const char* ptr, char value, + size_t length); + + // This function just restricts our execution to the AVX2 path + static MFBT_API const char* memchr8AVX2(const char* ptr, char value, + size_t length); + + // Search through `ptr[0..length]` for the first occurrence of `value` and + // return the pointer to it, or nullptr if it cannot be found. + static MFBT_API const char16_t* memchr16(const char16_t* ptr, char16_t value, + size_t length); + + // This function just restricts our execution to the SSE2 path + static MFBT_API const char16_t* memchr16SSE2(const char16_t* ptr, + char16_t value, size_t length); + + // This function just restricts our execution to the AVX2 path + static MFBT_API const char16_t* memchr16AVX2(const char16_t* ptr, + char16_t value, size_t length); + + // Search through `ptr[0..length]` for the first occurrence of `value` and + // return the pointer to it, or nullptr if it cannot be found. + static MFBT_API const uint64_t* memchr64(const uint64_t* ptr, uint64_t value, + size_t length); + + // This function just restricts our execution to the AVX2 path + static MFBT_API const uint64_t* memchr64AVX2(const uint64_t* ptr, + uint64_t value, size_t length); + + // Search through `ptr[0..length]` for the first occurrence of `v1` which is + // immediately followed by `v2` and return the pointer to the occurrence of + // `v1`. + static MFBT_API const char* memchr2x8(const char* ptr, char v1, char v2, + size_t length); + + // Search through `ptr[0..length]` for the first occurrence of `v1` which is + // immediately followed by `v2` and return the pointer to the occurrence of + // `v1`. + static MFBT_API const char16_t* memchr2x16(const char16_t* ptr, char16_t v1, + char16_t v2, size_t length); +}; + +} // namespace mozilla + +#endif // mozilla_SIMD_h diff --git a/mozglue/misc/SIMD_avx2.cpp b/mozglue/misc/SIMD_avx2.cpp new file mode 100644 index 0000000000..a1467c7a55 --- /dev/null +++ b/mozglue/misc/SIMD_avx2.cpp @@ -0,0 +1,294 @@ +/* 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 "mozilla/SIMD.h" + +#include "mozilla/SSE.h" +#include "mozilla/Assertions.h" + +// Restricting to x86_64 simplifies things, and we're not particularly +// worried about slightly degraded performance on 32 bit processors which +// support AVX2, as this should be quite a minority. +#if defined(MOZILLA_MAY_SUPPORT_AVX2) && defined(__x86_64__) + +# include <cstring> +# include <immintrin.h> +# include <stdint.h> +# include <type_traits> + +# include "mozilla/EndianUtils.h" + +namespace mozilla { + +const __m256i* Cast256(uintptr_t ptr) { + return reinterpret_cast<const __m256i*>(ptr); +} + +template <typename T> +T GetAs(uintptr_t ptr) { + return *reinterpret_cast<const T*>(ptr); +} + +uintptr_t AlignDown32(uintptr_t ptr) { return ptr & ~0x1f; } + +uintptr_t AlignUp32(uintptr_t ptr) { return AlignDown32(ptr + 0x1f); } + +template <typename TValue> +__m128i CmpEq128(__m128i a, __m128i b) { + static_assert(sizeof(TValue) == 1 || sizeof(TValue) == 2); + if (sizeof(TValue) == 1) { + return _mm_cmpeq_epi8(a, b); + } + return _mm_cmpeq_epi16(a, b); +} + +template <typename TValue> +__m256i CmpEq256(__m256i a, __m256i b) { + static_assert(sizeof(TValue) == 1 || sizeof(TValue) == 2 || + sizeof(TValue) == 8); + if (sizeof(TValue) == 1) { + return _mm256_cmpeq_epi8(a, b); + } + if (sizeof(TValue) == 2) { + return _mm256_cmpeq_epi16(a, b); + } + + return _mm256_cmpeq_epi64(a, b); +} + +# if defined(__GNUC__) && !defined(__clang__) + +// See the comment in SIMD.cpp over Load32BitsIntoXMM. This is just adapted +// from that workaround. Testing this, it also yields the correct instructions +// across all tested compilers. +__m128i Load64BitsIntoXMM(uintptr_t ptr) { + int64_t tmp; + memcpy(&tmp, reinterpret_cast<const void*>(ptr), sizeof(tmp)); + return _mm_cvtsi64_si128(tmp); +} + +# else + +__m128i Load64BitsIntoXMM(uintptr_t ptr) { + return _mm_loadu_si64(reinterpret_cast<const __m128i*>(ptr)); +} + +# endif + +template <typename TValue> +const TValue* Check4x8Bytes(__m128i needle, uintptr_t a, uintptr_t b, + uintptr_t c, uintptr_t d) { + __m128i haystackA = Load64BitsIntoXMM(a); + __m128i cmpA = CmpEq128<TValue>(needle, haystackA); + __m128i haystackB = Load64BitsIntoXMM(b); + __m128i cmpB = CmpEq128<TValue>(needle, haystackB); + __m128i haystackC = Load64BitsIntoXMM(c); + __m128i cmpC = CmpEq128<TValue>(needle, haystackC); + __m128i haystackD = Load64BitsIntoXMM(d); + __m128i cmpD = CmpEq128<TValue>(needle, haystackD); + __m128i or_ab = _mm_or_si128(cmpA, cmpB); + __m128i or_cd = _mm_or_si128(cmpC, cmpD); + __m128i or_abcd = _mm_or_si128(or_ab, or_cd); + int orMask = _mm_movemask_epi8(or_abcd); + if (orMask & 0xff) { + int cmpMask; + cmpMask = _mm_movemask_epi8(cmpA); + if (cmpMask & 0xff) { + return reinterpret_cast<const TValue*>(a + __builtin_ctz(cmpMask)); + } + cmpMask = _mm_movemask_epi8(cmpB); + if (cmpMask & 0xff) { + return reinterpret_cast<const TValue*>(b + __builtin_ctz(cmpMask)); + } + cmpMask = _mm_movemask_epi8(cmpC); + if (cmpMask & 0xff) { + return reinterpret_cast<const TValue*>(c + __builtin_ctz(cmpMask)); + } + cmpMask = _mm_movemask_epi8(cmpD); + if (cmpMask & 0xff) { + return reinterpret_cast<const TValue*>(d + __builtin_ctz(cmpMask)); + } + } + + return nullptr; +} + +template <typename TValue> +const TValue* Check4x32Bytes(__m256i needle, uintptr_t a, uintptr_t b, + uintptr_t c, uintptr_t d) { + __m256i haystackA = _mm256_loadu_si256(Cast256(a)); + __m256i cmpA = CmpEq256<TValue>(needle, haystackA); + __m256i haystackB = _mm256_loadu_si256(Cast256(b)); + __m256i cmpB = CmpEq256<TValue>(needle, haystackB); + __m256i haystackC = _mm256_loadu_si256(Cast256(c)); + __m256i cmpC = CmpEq256<TValue>(needle, haystackC); + __m256i haystackD = _mm256_loadu_si256(Cast256(d)); + __m256i cmpD = CmpEq256<TValue>(needle, haystackD); + __m256i or_ab = _mm256_or_si256(cmpA, cmpB); + __m256i or_cd = _mm256_or_si256(cmpC, cmpD); + __m256i or_abcd = _mm256_or_si256(or_ab, or_cd); + int orMask = _mm256_movemask_epi8(or_abcd); + if (orMask) { + int cmpMask; + cmpMask = _mm256_movemask_epi8(cmpA); + if (cmpMask) { + return reinterpret_cast<const TValue*>(a + __builtin_ctz(cmpMask)); + } + cmpMask = _mm256_movemask_epi8(cmpB); + if (cmpMask) { + return reinterpret_cast<const TValue*>(b + __builtin_ctz(cmpMask)); + } + cmpMask = _mm256_movemask_epi8(cmpC); + if (cmpMask) { + return reinterpret_cast<const TValue*>(c + __builtin_ctz(cmpMask)); + } + cmpMask = _mm256_movemask_epi8(cmpD); + if (cmpMask) { + return reinterpret_cast<const TValue*>(d + __builtin_ctz(cmpMask)); + } + } + + return nullptr; +} + +template <typename TValue> +const TValue* FindInBufferAVX2(const TValue* ptr, TValue value, size_t length) { + static_assert(sizeof(TValue) == 1 || sizeof(TValue) == 2 || + sizeof(TValue) == 8); + static_assert(std::is_unsigned<TValue>::value); + + // Load our needle into a 32-byte register + __m256i needle; + if (sizeof(TValue) == 1) { + needle = _mm256_set1_epi8(value); + } else if (sizeof(TValue) == 2) { + needle = _mm256_set1_epi16(value); + } else { + needle = _mm256_set1_epi64x(value); + } + + size_t numBytes = length * sizeof(TValue); + uintptr_t cur = reinterpret_cast<uintptr_t>(ptr); + uintptr_t end = cur + numBytes; + + if (numBytes < 8 || (sizeof(TValue) == 8 && numBytes < 32)) { + while (cur < end) { + if (GetAs<TValue>(cur) == value) { + return reinterpret_cast<const TValue*>(cur); + } + cur += sizeof(TValue); + } + return nullptr; + } + + if constexpr (sizeof(TValue) != 8) { + if (numBytes < 32) { + __m128i needle_narrow; + if (sizeof(TValue) == 1) { + needle_narrow = _mm_set1_epi8(value); + } else { + needle_narrow = _mm_set1_epi16(value); + } + uintptr_t a = cur; + uintptr_t b = cur + ((numBytes & 16) >> 1); + uintptr_t c = end - 8 - ((numBytes & 16) >> 1); + uintptr_t d = end - 8; + return Check4x8Bytes<TValue>(needle_narrow, a, b, c, d); + } + } + + if (numBytes < 128) { + // NOTE: here and below, we have some bit fiddling which could look a + // little weird. The important thing to note though is it's just a trick + // for getting the number 32 if numBytes is greater than or equal to 64, + // and 0 otherwise. This lets us fully cover the range without any + // branching for the case where numBytes is in [32,64), and [64,128). We get + // four ranges from this - if numbytes > 64, we get: + // [0,32), [32,64], [end - 64), [end - 32) + // and if numbytes < 64, we get + // [0,32), [0,32), [end - 32), [end - 32) + uintptr_t a = cur; + uintptr_t b = cur + ((numBytes & 64) >> 1); + uintptr_t c = end - 32 - ((numBytes & 64) >> 1); + uintptr_t d = end - 32; + return Check4x32Bytes<TValue>(needle, a, b, c, d); + } + + // Get the initial unaligned load out of the way. This will overlap with the + // aligned stuff below, but the overlapped part should effectively be free + // (relative to a mispredict from doing a byte-by-byte loop). + __m256i haystack = _mm256_loadu_si256(Cast256(cur)); + __m256i cmp = CmpEq256<TValue>(needle, haystack); + int cmpMask = _mm256_movemask_epi8(cmp); + if (cmpMask) { + return reinterpret_cast<const TValue*>(cur + __builtin_ctz(cmpMask)); + } + + // Now we're working with aligned memory. Hooray! \o/ + cur = AlignUp32(cur); + + uintptr_t tailStartPtr = AlignDown32(end - 96); + uintptr_t tailEndPtr = end - 32; + + while (cur < tailStartPtr) { + uintptr_t a = cur; + uintptr_t b = cur + 32; + uintptr_t c = cur + 64; + uintptr_t d = cur + 96; + const TValue* result = Check4x32Bytes<TValue>(needle, a, b, c, d); + if (result) { + return result; + } + cur += 128; + } + + uintptr_t a = tailStartPtr; + uintptr_t b = tailStartPtr + 32; + uintptr_t c = tailStartPtr + 64; + uintptr_t d = tailEndPtr; + return Check4x32Bytes<TValue>(needle, a, b, c, d); +} + +const char* SIMD::memchr8AVX2(const char* ptr, char value, size_t length) { + const unsigned char* uptr = reinterpret_cast<const unsigned char*>(ptr); + unsigned char uvalue = static_cast<unsigned char>(value); + const unsigned char* uresult = + FindInBufferAVX2<unsigned char>(uptr, uvalue, length); + return reinterpret_cast<const char*>(uresult); +} + +const char16_t* SIMD::memchr16AVX2(const char16_t* ptr, char16_t value, + size_t length) { + return FindInBufferAVX2<char16_t>(ptr, value, length); +} + +const uint64_t* SIMD::memchr64AVX2(const uint64_t* ptr, uint64_t value, + size_t length) { + return FindInBufferAVX2<uint64_t>(ptr, value, length); +} + +} // namespace mozilla + +#else + +namespace mozilla { + +const char* SIMD::memchr8AVX2(const char* ptr, char value, size_t length) { + MOZ_RELEASE_ASSERT(false, "AVX2 not supported in this binary."); +} + +const char16_t* SIMD::memchr16AVX2(const char16_t* ptr, char16_t value, + size_t length) { + MOZ_RELEASE_ASSERT(false, "AVX2 not supported in this binary."); +} + +const uint64_t* SIMD::memchr64AVX2(const uint64_t* ptr, uint64_t value, + size_t length) { + MOZ_RELEASE_ASSERT(false, "AVX2 not supported in this binary."); +} + +} // namespace mozilla + +#endif diff --git a/mozglue/misc/SSE.cpp b/mozglue/misc/SSE.cpp new file mode 100644 index 0000000000..ca0b4c3c86 --- /dev/null +++ b/mozglue/misc/SSE.cpp @@ -0,0 +1,202 @@ +/* vim: set shiftwidth=4 tabstop=8 autoindent cindent expandtab: */ +/* 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/. */ + +/* compile-time and runtime tests for whether to use SSE instructions */ + +#include "SSE.h" + +#ifdef HAVE_CPUID_H +// cpuid.h is available on gcc 4.3 and higher on i386 and x86_64 +# include <cpuid.h> +#elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_AMD64)) +// MSVC 2005 or newer on x86-32 or x86-64 +# include <intrin.h> +#endif + +namespace { + +// SSE.h has parallel #ifs which declare MOZILLA_SSE_HAVE_CPUID_DETECTION. +// We can't declare these functions in the header file, however, because +// <intrin.h> conflicts with <windows.h> on MSVC 2005, and some files want to +// include both SSE.h and <windows.h>. + +#ifdef HAVE_CPUID_H + +enum CPUIDRegister { eax = 0, ebx = 1, ecx = 2, edx = 3 }; + +static bool has_cpuid_bits(unsigned int level, CPUIDRegister reg, + unsigned int bits) { + unsigned int regs[4]; + unsigned int eax, ebx, ecx, edx; + unsigned max = __get_cpuid_max(level & 0x80000000u, nullptr); + if (level > max) return false; + __cpuid_count(level, 0, eax, ebx, ecx, edx); + regs[0] = eax; + regs[1] = ebx; + regs[2] = ecx; + regs[3] = edx; + return (regs[reg] & bits) == bits; +} + +#elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_AMD64)) + +enum CPUIDRegister { eax = 0, ebx = 1, ecx = 2, edx = 3 }; + +static bool has_cpuid_bits(unsigned int level, CPUIDRegister reg, + unsigned int bits) { + // Check that the level in question is supported. + int regs[4]; + __cpuid(regs, level & 0x80000000u); + if (unsigned(regs[0]) < level) return false; + + // "The __cpuid intrinsic clears the ECX register before calling the cpuid + // instruction." + __cpuid(regs, level); + return (unsigned(regs[reg]) & bits) == bits; +} + +#elif (defined(__GNUC__) || defined(__SUNPRO_CC)) && \ + (defined(__i386) || defined(__x86_64__)) + +enum CPUIDRegister { eax = 0, ebx = 1, ecx = 2, edx = 3 }; + +# ifdef __i386 +static void moz_cpuid(int CPUInfo[4], int InfoType) { + asm("xchg %esi, %ebx\n" + "xor %ecx, %ecx\n" // ecx is the sub-leaf (we only ever need 0) + "cpuid\n" + "movl %eax, (%edi)\n" + "movl %ebx, 4(%edi)\n" + "movl %ecx, 8(%edi)\n" + "movl %edx, 12(%edi)\n" + "xchg %esi, %ebx\n" + : + : "a"(InfoType), // %eax + "D"(CPUInfo) // %edi + : "%ecx", "%edx", "%esi"); +} +# else +static void moz_cpuid(int CPUInfo[4], int InfoType) { + asm("xchg %rsi, %rbx\n" + "xor %ecx, %ecx\n" // ecx is the sub-leaf (we only ever need 0) + "cpuid\n" + "movl %eax, (%rdi)\n" + "movl %ebx, 4(%rdi)\n" + "movl %ecx, 8(%rdi)\n" + "movl %edx, 12(%rdi)\n" + "xchg %rsi, %rbx\n" + : + : "a"(InfoType), // %eax + "D"(CPUInfo) // %rdi + : "%ecx", "%edx", "%rsi"); +} +# endif + +static bool has_cpuid_bits(unsigned int level, CPUIDRegister reg, + unsigned int bits) { + // Check that the level in question is supported. + volatile int regs[4]; + moz_cpuid((int*)regs, level & 0x80000000u); + if (unsigned(regs[0]) < level) return false; + + moz_cpuid((int*)regs, level); + return (unsigned(regs[reg]) & bits) == bits; +} + +#endif // end CPUID declarations + +} // namespace + +namespace mozilla { + +namespace sse_private { + +#if defined(MOZILLA_SSE_HAVE_CPUID_DETECTION) + +# if !defined(MOZILLA_PRESUME_MMX) +bool mmx_enabled = has_cpuid_bits(1u, edx, (1u << 23)); +# endif + +# if !defined(MOZILLA_PRESUME_SSE) +bool sse_enabled = has_cpuid_bits(1u, edx, (1u << 25)); +# endif + +# if !defined(MOZILLA_PRESUME_SSE2) +bool sse2_enabled = has_cpuid_bits(1u, edx, (1u << 26)); +# endif + +# if !defined(MOZILLA_PRESUME_SSE3) +bool sse3_enabled = has_cpuid_bits(1u, ecx, (1u << 0)); +# endif + +# if !defined(MOZILLA_PRESUME_SSSE3) +bool ssse3_enabled = has_cpuid_bits(1u, ecx, (1u << 9)); +# endif + +# if !defined(MOZILLA_PRESUME_SSE4A) +bool sse4a_enabled = has_cpuid_bits(0x80000001u, ecx, (1u << 6)); +# endif + +# if !defined(MOZILLA_PRESUME_SSE4_1) +bool sse4_1_enabled = has_cpuid_bits(1u, ecx, (1u << 19)); +# endif + +# if !defined(MOZILLA_PRESUME_SSE4_2) +bool sse4_2_enabled = has_cpuid_bits(1u, ecx, (1u << 20)); +# endif + +# if !defined(MOZILLA_PRESUME_FMA3) +bool fma3_enabled = has_cpuid_bits(1u, ecx, (1u << 12)); +# endif + +# if !defined(MOZILLA_PRESUME_AVX) || !defined(MOZILLA_PRESUME_AVX2) +static bool has_avx() { +# if defined(MOZILLA_PRESUME_AVX) + return true; +# else + const unsigned AVX = 1u << 28; + const unsigned OSXSAVE = 1u << 27; + const unsigned XSAVE = 1u << 26; + + const unsigned XMM_STATE = 1u << 1; + const unsigned YMM_STATE = 1u << 2; + const unsigned AVX_STATE = XMM_STATE | YMM_STATE; + + return has_cpuid_bits(1u, ecx, AVX | OSXSAVE | XSAVE) && + // ensure the OS supports XSAVE of YMM registers + (xgetbv(0) & AVX_STATE) == AVX_STATE; +# endif // MOZILLA_PRESUME_AVX +} +# endif // !MOZILLA_PRESUME_AVX || !MOZILLA_PRESUME_AVX2 + +# if !defined(MOZILLA_PRESUME_AVX) +bool avx_enabled = has_avx(); +# endif + +# if !defined(MOZILLA_PRESUME_AVX2) +bool avx2_enabled = has_avx() && has_cpuid_bits(7u, ebx, (1u << 5)); +# endif + +# if !defined(MOZILLA_PRESUME_AES) +bool aes_enabled = has_cpuid_bits(1u, ecx, (1u << 25)); +# endif + +bool has_constant_tsc = has_cpuid_bits(0x80000007u, edx, (1u << 8)); + +#endif + +} // namespace sse_private + +#ifdef HAVE_CPUID_H + +uint64_t xgetbv(uint32_t xcr) { + uint32_t eax, edx; + __asm__(".byte 0x0f, 0x01, 0xd0" : "=a"(eax), "=d"(edx) : "c"(xcr)); + return (uint64_t)(edx) << 32 | eax; +} + +#endif + +} // namespace mozilla diff --git a/mozglue/misc/SSE.h b/mozglue/misc/SSE.h new file mode 100644 index 0000000000..0b87366a80 --- /dev/null +++ b/mozglue/misc/SSE.h @@ -0,0 +1,371 @@ +/* vim: set shiftwidth=2 tabstop=8 autoindent cindent expandtab: */ +/* 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/. */ + +/* compile-time and runtime tests for whether to use SSE instructions */ + +#ifndef mozilla_SSE_h_ +#define mozilla_SSE_h_ + +// for definition of MFBT_DATA +#include "mozilla/Types.h" + +/** + * The public interface of this header consists of a set of macros and + * functions for Intel CPU features. + * + * DETECTING ISA EXTENSIONS + * ======================== + * + * This header provides the following functions for determining whether the + * current CPU supports a particular instruction set extension: + * + * mozilla::supports_mmx + * mozilla::supports_sse + * mozilla::supports_sse2 + * mozilla::supports_sse3 + * mozilla::supports_ssse3 + * mozilla::supports_sse4a + * mozilla::supports_sse4_1 + * mozilla::supports_sse4_2 + * mozilla::supports_avx + * mozilla::supports_avx2 + * mozilla::supports_aes + * mozilla::has_constant_tsc + * + * If you're writing code using inline assembly, you should guard it with a + * call to one of these functions. For instance: + * + * if (mozilla::supports_sse2()) { + * asm(" ... "); + * } + * else { + * ... + * } + * + * Note that these functions depend on cpuid intrinsics only available in gcc + * 4.3 or later and MSVC 8.0 (Visual C++ 2005) or later, so they return false + * in older compilers. (This could be fixed by replacing the code with inline + * assembly.) + * + * + * USING INTRINSICS + * ================ + * + * This header also provides support for coding using CPU intrinsics. + * + * For each mozilla::supports_abc function, we define a MOZILLA_MAY_SUPPORT_ABC + * macro which indicates that the target/compiler combination we're using is + * compatible with the ABC extension. For instance, x86_64 with MSVC 2003 is + * compatible with SSE2 but not SSE3, since although there exist x86_64 CPUs + * with SSE3 support, MSVC 2003 only supports through SSE2. + * + * Until gcc fixes #pragma target [1] [2] or our x86 builds require SSE2, + * you'll need to separate code using intrinsics into a file separate from your + * regular code. Here's the recommended pattern: + * + * #ifdef MOZILLA_MAY_SUPPORT_ABC + * namespace mozilla { + * namespace ABC { + * void foo(); + * } + * } + * #endif + * + * void foo() { + * #ifdef MOZILLA_MAY_SUPPORT_ABC + * if (mozilla::supports_abc()) { + * mozilla::ABC::foo(); // in a separate file + * return; + * } + * #endif + * + * foo_unvectorized(); + * } + * + * You'll need to define mozilla::ABC::foo() in a separate file and add the + * -mabc flag when using gcc. + * + * [1] http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39787 and + * [2] http://gcc.gnu.org/bugzilla/show_bug.cgi?id=41201 being fixed. + * + */ + +#if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) + +# ifdef __MMX__ +// It's ok to use MMX instructions based on the -march option (or +// the default for x86_64 or for Intel Mac). +# define MOZILLA_PRESUME_MMX 1 +# endif +# ifdef __SSE__ +// It's ok to use SSE instructions based on the -march option (or +// the default for x86_64 or for Intel Mac). +# define MOZILLA_PRESUME_SSE 1 +# endif +# ifdef __SSE2__ +// It's ok to use SSE2 instructions based on the -march option (or +// the default for x86_64 or for Intel Mac). +# define MOZILLA_PRESUME_SSE2 1 +# endif +# ifdef __SSE3__ +// It's ok to use SSE3 instructions based on the -march option (or the +// default for Intel Mac). +# define MOZILLA_PRESUME_SSE3 1 +# endif +# ifdef __SSSE3__ +// It's ok to use SSSE3 instructions based on the -march option. +# define MOZILLA_PRESUME_SSSE3 1 +# endif +# ifdef __SSE4A__ +// It's ok to use SSE4A instructions based on the -march option. +# define MOZILLA_PRESUME_SSE4A 1 +# endif +# ifdef __SSE4_1__ +// It's ok to use SSE4.1 instructions based on the -march option. +# define MOZILLA_PRESUME_SSE4_1 1 +# endif +# ifdef __SSE4_2__ +// It's ok to use SSE4.2 instructions based on the -march option. +# define MOZILLA_PRESUME_SSE4_2 1 +# endif +# ifdef __AVX__ +// It's ok to use AVX instructions based on the -march option. +# define MOZILLA_PRESUME_AVX 1 +# endif +# ifdef __AVX2__ +// It's ok to use AVX instructions based on the -march option. +# define MOZILLA_PRESUME_AVX2 1 +# endif +# ifdef __AES__ +// It's ok to use AES instructions based on the -march option. +# define MOZILLA_PRESUME_AES 1 +# endif + +# ifdef HAVE_CPUID_H +# define MOZILLA_SSE_HAVE_CPUID_DETECTION +# endif + +#elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_AMD64)) + +# define MOZILLA_SSE_HAVE_CPUID_DETECTION + +# if defined(_M_IX86_FP) + +# if _M_IX86_FP >= 1 +// It's ok to use SSE instructions based on the /arch option +# define MOZILLA_PRESUME_SSE +# endif +# if _M_IX86_FP >= 2 +// It's ok to use SSE2 instructions based on the /arch option +# define MOZILLA_PRESUME_SSE2 +# endif + +# elif defined(_M_AMD64) +// MSVC for AMD64 doesn't support MMX, so don't presume it here. + +// SSE is always available on AMD64. +# define MOZILLA_PRESUME_SSE +// SSE2 is always available on AMD64. +# define MOZILLA_PRESUME_SSE2 +# endif + +#elif defined(__SUNPRO_CC) && (defined(__i386) || defined(__x86_64__)) +// Sun Studio on x86 or amd64 + +# define MOZILLA_SSE_HAVE_CPUID_DETECTION + +# if defined(__x86_64__) +// MMX is always available on AMD64. +# define MOZILLA_PRESUME_MMX +// SSE is always available on AMD64. +# define MOZILLA_PRESUME_SSE +// SSE2 is always available on AMD64. +# define MOZILLA_PRESUME_SSE2 +# endif + +#endif + +namespace mozilla { + +namespace sse_private { +#if defined(MOZILLA_SSE_HAVE_CPUID_DETECTION) +# if !defined(MOZILLA_PRESUME_MMX) +extern bool MFBT_DATA mmx_enabled; +# endif +# if !defined(MOZILLA_PRESUME_SSE) +extern bool MFBT_DATA sse_enabled; +# endif +# if !defined(MOZILLA_PRESUME_SSE2) +extern bool MFBT_DATA sse2_enabled; +# endif +# if !defined(MOZILLA_PRESUME_SSE3) +extern bool MFBT_DATA sse3_enabled; +# endif +# if !defined(MOZILLA_PRESUME_SSSE3) +extern bool MFBT_DATA ssse3_enabled; +# endif +# if !defined(MOZILLA_PRESUME_SSE4A) +extern bool MFBT_DATA sse4a_enabled; +# endif +# if !defined(MOZILLA_PRESUME_SSE4_1) +extern bool MFBT_DATA sse4_1_enabled; +# endif +# if !defined(MOZILLA_PRESUME_SSE4_2) +extern bool MFBT_DATA sse4_2_enabled; +# endif +# if !defined(MOZILLA_PRESUME_FMA3) +extern bool MFBT_DATA fma3_enabled; +# endif +# if !defined(MOZILLA_PRESUME_AVX) +extern bool MFBT_DATA avx_enabled; +# endif +# if !defined(MOZILLA_PRESUME_AVX2) +extern bool MFBT_DATA avx2_enabled; +# endif +# if !defined(MOZILLA_PRESUME_AES) +extern bool MFBT_DATA aes_enabled; +# endif +extern bool MFBT_DATA has_constant_tsc; + +#endif +} // namespace sse_private + +#ifdef HAVE_CPUID_H +MOZ_EXPORT uint64_t xgetbv(uint32_t xcr); +#endif + +#if defined(MOZILLA_PRESUME_MMX) +# define MOZILLA_MAY_SUPPORT_MMX 1 +inline bool supports_mmx() { return true; } +#elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION) +# if !(defined(_MSC_VER) && defined(_M_AMD64)) +// Define MOZILLA_MAY_SUPPORT_MMX only if we're not on MSVC for +// AMD64, since that compiler doesn't support MMX. +# define MOZILLA_MAY_SUPPORT_MMX 1 +# endif +inline bool supports_mmx() { return sse_private::mmx_enabled; } +#else +inline bool supports_mmx() { return false; } +#endif + +#if defined(MOZILLA_PRESUME_SSE) +# define MOZILLA_MAY_SUPPORT_SSE 1 +inline bool supports_sse() { return true; } +#elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION) +# define MOZILLA_MAY_SUPPORT_SSE 1 +inline bool supports_sse() { return sse_private::sse_enabled; } +#else +inline bool supports_sse() { return false; } +#endif + +#if defined(MOZILLA_PRESUME_SSE2) +# define MOZILLA_MAY_SUPPORT_SSE2 1 +inline bool supports_sse2() { return true; } +#elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION) +# define MOZILLA_MAY_SUPPORT_SSE2 1 +inline bool supports_sse2() { return sse_private::sse2_enabled; } +#else +inline bool supports_sse2() { return false; } +#endif + +#if defined(MOZILLA_PRESUME_SSE3) +# define MOZILLA_MAY_SUPPORT_SSE3 1 +inline bool supports_sse3() { return true; } +#elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION) +# define MOZILLA_MAY_SUPPORT_SSE3 1 +inline bool supports_sse3() { return sse_private::sse3_enabled; } +#else +inline bool supports_sse3() { return false; } +#endif + +#if defined(MOZILLA_PRESUME_SSSE3) +# define MOZILLA_MAY_SUPPORT_SSSE3 1 +inline bool supports_ssse3() { return true; } +#elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION) +# define MOZILLA_MAY_SUPPORT_SSSE3 1 +inline bool supports_ssse3() { return sse_private::ssse3_enabled; } +#else +inline bool supports_ssse3() { return false; } +#endif + +#if defined(MOZILLA_PRESUME_SSE4A) +# define MOZILLA_MAY_SUPPORT_SSE4A 1 +inline bool supports_sse4a() { return true; } +#elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION) +# define MOZILLA_MAY_SUPPORT_SSE4A 1 +inline bool supports_sse4a() { return sse_private::sse4a_enabled; } +#else +inline bool supports_sse4a() { return false; } +#endif + +#if defined(MOZILLA_PRESUME_SSE4_1) +# define MOZILLA_MAY_SUPPORT_SSE4_1 1 +inline bool supports_sse4_1() { return true; } +#elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION) +# define MOZILLA_MAY_SUPPORT_SSE4_1 1 +inline bool supports_sse4_1() { return sse_private::sse4_1_enabled; } +#else +inline bool supports_sse4_1() { return false; } +#endif + +#if defined(MOZILLA_PRESUME_SSE4_2) +# define MOZILLA_MAY_SUPPORT_SSE4_2 1 +inline bool supports_sse4_2() { return true; } +#elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION) +# define MOZILLA_MAY_SUPPORT_SSE4_2 1 +inline bool supports_sse4_2() { return sse_private::sse4_2_enabled; } +#else +inline bool supports_sse4_2() { return false; } +#endif + +#if defined(MOZILLA_PRESUME_FMA3) +# define MOZILLA_MAY_SUPPORT_FMA3 1 +inline bool supports_fma3() { return true; } +#elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION) +# define MOZILLA_MAY_SUPPORT_FMA3 1 +inline bool supports_fma3() { return sse_private::fma3_enabled; } +#else +inline bool supports_fma3() { return false; } +#endif + +#if defined(MOZILLA_PRESUME_AVX) +# define MOZILLA_MAY_SUPPORT_AVX 1 +inline bool supports_avx() { return true; } +#elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION) +# define MOZILLA_MAY_SUPPORT_AVX 1 +inline bool supports_avx() { return sse_private::avx_enabled; } +#else +inline bool supports_avx() { return false; } +#endif + +#if defined(MOZILLA_PRESUME_AVX2) +# define MOZILLA_MAY_SUPPORT_AVX2 1 +inline bool supports_avx2() { return true; } +#elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION) +# define MOZILLA_MAY_SUPPORT_AVX2 1 +inline bool supports_avx2() { return sse_private::avx2_enabled; } +#else +inline bool supports_avx2() { return false; } +#endif + +#if defined(MOZILLA_PRESUME_AES) +# define MOZILLA_MAY_SUPPORT_AES 1 +inline bool supports_aes() { return true; } +#elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION) +# define MOZILLA_MAY_SUPPORT_AES 1 +inline bool supports_aes() { return sse_private::aes_enabled; } +#else +inline bool supports_aes() { return false; } +#endif + +#ifdef MOZILLA_SSE_HAVE_CPUID_DETECTION +inline bool has_constant_tsc() { return sse_private::has_constant_tsc; } +#else +inline bool has_constant_tsc() { return false; } +#endif + +} // namespace mozilla + +#endif /* !defined(mozilla_SSE_h_) */ diff --git a/mozglue/misc/Sprintf.h b/mozglue/misc/Sprintf.h new file mode 100644 index 0000000000..4b459de82d --- /dev/null +++ b/mozglue/misc/Sprintf.h @@ -0,0 +1,98 @@ +/* -*- 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/. */ + +/* Provides a safer sprintf for printing to fixed-size character arrays. */ + +#ifndef mozilla_Sprintf_h_ +#define mozilla_Sprintf_h_ + +#include <stdio.h> +#include <stdarg.h> +#include <algorithm> + +#include "mozilla/Assertions.h" +#include "mozilla/Attributes.h" +#include "mozilla/Printf.h" + +#ifdef __cplusplus + +# ifndef SPRINTF_H_USES_VSNPRINTF +namespace mozilla { +namespace detail { + +struct MOZ_STACK_CLASS SprintfAppend final : public mozilla::PrintfTarget { + explicit SprintfAppend(char* aBuf, size_t aBufLen) + : mBuf(aBuf), mBufLen(aBufLen) {} + + bool append(const char* aStr, size_t aLen) override { + if (aLen == 0) { + return true; + } + // Don't copy more than what's left to use. + size_t copy = std::min(mBufLen, aLen); + if (copy > 0) { + memcpy(mBuf, aStr, copy); + mBuf += copy; + mBufLen -= copy; + } + return true; + } + + private: + char* mBuf; + size_t mBufLen; +}; + +} // namespace detail +} // namespace mozilla +# endif // SPRINTF_H_USES_VSNPRINTF + +MOZ_FORMAT_PRINTF(3, 0) +MOZ_MAYBE_UNUSED +static int VsprintfBuf(char* buffer, size_t bufsize, const char* format, + va_list args) { + MOZ_ASSERT(format != buffer); +# ifdef SPRINTF_H_USES_VSNPRINTF + int result = vsnprintf(buffer, bufsize, format, args); + buffer[bufsize - 1] = '\0'; + return result; +# else + mozilla::detail::SprintfAppend ss(buffer, bufsize); + ss.vprint(format, args); + size_t len = ss.emitted(); + buffer[std::min(len, bufsize - 1)] = '\0'; + return len; +# endif +} + +MOZ_FORMAT_PRINTF(3, 4) +MOZ_MAYBE_UNUSED +static int SprintfBuf(char* buffer, size_t bufsize, const char* format, ...) { + va_list args; + va_start(args, format); + int result = VsprintfBuf(buffer, bufsize, format, args); + va_end(args); + return result; +} + +template <size_t N> +MOZ_FORMAT_PRINTF(2, 0) +int VsprintfLiteral(char (&buffer)[N], const char* format, va_list args) { + return VsprintfBuf(buffer, N, format, args); +} + +template <size_t N> +MOZ_FORMAT_PRINTF(2, 3) +int SprintfLiteral(char (&buffer)[N], const char* format, ...) { + va_list args; + va_start(args, format); + int result = VsprintfLiteral(buffer, format, args); + va_end(args); + return result; +} + +#endif +#endif /* mozilla_Sprintf_h_ */ diff --git a/mozglue/misc/StackWalk.cpp b/mozglue/misc/StackWalk.cpp new file mode 100644 index 0000000000..307715b170 --- /dev/null +++ b/mozglue/misc/StackWalk.cpp @@ -0,0 +1,1063 @@ +/* -*- 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/. */ + +/* API for getting a stack trace of the C/C++ stack on the current thread */ + +#include "mozilla/ArrayUtils.h" +#include "mozilla/Attributes.h" +#include "mozilla/StackWalk.h" +#ifdef XP_WIN +# include "mozilla/StackWalkThread.h" +# include <io.h> +#else +# include <unistd.h> +#endif +#include "mozilla/Sprintf.h" + +#include <string.h> + +#if defined(ANDROID) && defined(MOZ_LINKER) +# include "Linker.h" +# include <android/log.h> +#endif + +using namespace mozilla; + +// for _Unwind_Backtrace from libcxxrt or libunwind +// cxxabi.h from libcxxrt implicitly includes unwind.h first +#if defined(HAVE__UNWIND_BACKTRACE) && !defined(_GNU_SOURCE) +# define _GNU_SOURCE +#endif + +#if defined(HAVE_DLOPEN) || defined(XP_DARWIN) +# include <dlfcn.h> +#endif + +#if (defined(XP_DARWIN) && \ + (defined(__i386) || defined(__ppc__) || defined(HAVE__UNWIND_BACKTRACE))) +# define MOZ_STACKWALK_SUPPORTS_MACOSX 1 +#else +# define MOZ_STACKWALK_SUPPORTS_MACOSX 0 +#endif + +#if (defined(linux) && \ + ((defined(__GNUC__) && (defined(__i386) || defined(PPC))) || \ + defined(HAVE__UNWIND_BACKTRACE))) +# define MOZ_STACKWALK_SUPPORTS_LINUX 1 +#else +# define MOZ_STACKWALK_SUPPORTS_LINUX 0 +#endif + +#if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 1) +# define HAVE___LIBC_STACK_END 1 +#else +# define HAVE___LIBC_STACK_END 0 +#endif + +#if HAVE___LIBC_STACK_END +extern MOZ_EXPORT void* __libc_stack_end; // from ld-linux.so +#endif + +#ifdef ANDROID +# include <algorithm> +# include <unistd.h> +# include <pthread.h> +#endif + +class FrameSkipper { + public: + constexpr FrameSkipper() : mPc(0) {} + bool ShouldSkipPC(void* aPC) { + // Skip frames until we encounter the one we were initialized with, + // and then never skip again. + if (mPc != 0) { + if (mPc != uintptr_t(aPC)) { + return true; + } + mPc = 0; + } + return false; + } + explicit FrameSkipper(const void* aPc) : mPc(uintptr_t(aPc)) {} + + private: + uintptr_t mPc; +}; + +#ifdef XP_WIN + +# include <windows.h> +# include <process.h> +# include <stdio.h> +# include <malloc.h> +# include "mozilla/ArrayUtils.h" +# include "mozilla/Atomics.h" +# include "mozilla/StackWalk_windows.h" +# include "mozilla/WindowsVersion.h" + +# include <imagehlp.h> +// We need a way to know if we are building for WXP (or later), as if we are, we +// need to use the newer 64-bit APIs. API_VERSION_NUMBER seems to fit the bill. +// A value of 9 indicates we want to use the new APIs. +# if API_VERSION_NUMBER < 9 +# error Too old imagehlp.h +# endif + +CRITICAL_SECTION gDbgHelpCS; + +# if defined(_M_AMD64) || defined(_M_ARM64) +// Because various Win64 APIs acquire function-table locks, we need a way of +// preventing stack walking while those APIs are being called. Otherwise, the +// stack walker may suspend a thread holding such a lock, and deadlock when the +// stack unwind code attempts to wait for that lock. +// +// We're using an atomic counter rather than a critical section because we +// don't require mutual exclusion with the stack walker. If the stack walker +// determines that it's safe to start unwinding the suspended thread (i.e. +// there are no suppressions when the unwind begins), then it's safe to +// continue unwinding that thread even if other threads request suppressions +// in the meantime, because we can't deadlock with those other threads. +// +// XXX: This global variable is a larger-than-necessary hammer. A more scoped +// solution would be to maintain a counter per thread, but then it would be +// more difficult for WalkStackMain64 to read the suspended thread's counter. +static Atomic<size_t> sStackWalkSuppressions; + +void SuppressStackWalking() { ++sStackWalkSuppressions; } + +void DesuppressStackWalking() { --sStackWalkSuppressions; } + +MFBT_API +AutoSuppressStackWalking::AutoSuppressStackWalking() { SuppressStackWalking(); } + +MFBT_API +AutoSuppressStackWalking::~AutoSuppressStackWalking() { + DesuppressStackWalking(); +} + +static uint8_t* sJitCodeRegionStart; +static size_t sJitCodeRegionSize; +uint8_t* sMsMpegJitCodeRegionStart; +size_t sMsMpegJitCodeRegionSize; + +MFBT_API void RegisterJitCodeRegion(uint8_t* aStart, size_t aSize) { + // Currently we can only handle one JIT code region at a time + MOZ_RELEASE_ASSERT(!sJitCodeRegionStart); + + sJitCodeRegionStart = aStart; + sJitCodeRegionSize = aSize; +} + +MFBT_API void UnregisterJitCodeRegion(uint8_t* aStart, size_t aSize) { + // Currently we can only handle one JIT code region at a time + MOZ_RELEASE_ASSERT(sJitCodeRegionStart && sJitCodeRegionStart == aStart && + sJitCodeRegionSize == aSize); + + sJitCodeRegionStart = nullptr; + sJitCodeRegionSize = 0; +} + +# endif // _M_AMD64 || _M_ARM64 + +// Routine to print an error message to standard error. +static void PrintError(const char* aPrefix) { + LPSTR lpMsgBuf; + DWORD lastErr = GetLastError(); + FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + nullptr, lastErr, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language + (LPSTR)&lpMsgBuf, 0, nullptr); + fprintf(stderr, "### ERROR: %s: %s", aPrefix, + lpMsgBuf ? lpMsgBuf : "(null)\n"); + fflush(stderr); + LocalFree(lpMsgBuf); +} + +static void InitializeDbgHelpCriticalSection() { + static bool initialized = false; + if (initialized) { + return; + } + ::InitializeCriticalSection(&gDbgHelpCS); + initialized = true; +} + +// Wrapper around a reference to a CONTEXT, to simplify access to main +// platform-specific execution registers. +// It also avoids using CONTEXT* nullable pointers. +class CONTEXTGenericAccessors { + public: + explicit CONTEXTGenericAccessors(CONTEXT& aCONTEXT) : mCONTEXT(aCONTEXT) {} + + CONTEXT* CONTEXTPtr() { return &mCONTEXT; } + + inline auto& PC() { +# if defined(_M_AMD64) + return mCONTEXT.Rip; +# elif defined(_M_ARM64) + return mCONTEXT.Pc; +# elif defined(_M_IX86) + return mCONTEXT.Eip; +# else +# error "unknown platform" +# endif + } + + inline auto& SP() { +# if defined(_M_AMD64) + return mCONTEXT.Rsp; +# elif defined(_M_ARM64) + return mCONTEXT.Sp; +# elif defined(_M_IX86) + return mCONTEXT.Esp; +# else +# error "unknown platform" +# endif + } + + inline auto& BP() { +# if defined(_M_AMD64) + return mCONTEXT.Rbp; +# elif defined(_M_ARM64) + return mCONTEXT.Fp; +# elif defined(_M_IX86) + return mCONTEXT.Ebp; +# else +# error "unknown platform" +# endif + } + + private: + CONTEXT& mCONTEXT; +}; + +/** + * Walk the stack, translating PC's found into strings and recording the + * chain in aBuffer. For this to work properly, the DLLs must be rebased + * so that the address in the file agrees with the address in memory. + * Otherwise StackWalk will return FALSE when it hits a frame in a DLL + * whose in memory address doesn't match its in-file address. + */ + +static void DoMozStackWalkThread(MozWalkStackCallback aCallback, + const void* aFirstFramePC, uint32_t aMaxFrames, + void* aClosure, HANDLE aThread, + CONTEXT* aContext) { + InitializeDbgHelpCriticalSection(); + + HANDLE targetThread = aThread; + bool walkCallingThread; + if (!targetThread) { + targetThread = ::GetCurrentThread(); + walkCallingThread = true; + } else { + DWORD targetThreadId = ::GetThreadId(targetThread); + DWORD currentThreadId = ::GetCurrentThreadId(); + walkCallingThread = (targetThreadId == currentThreadId); + } + + // If not already provided, get a context for the specified thread. + CONTEXT context_buf; + if (!aContext) { + memset(&context_buf, 0, sizeof(CONTEXT)); + context_buf.ContextFlags = CONTEXT_FULL; + if (walkCallingThread) { + ::RtlCaptureContext(&context_buf); + } else if (!GetThreadContext(targetThread, &context_buf)) { + return; + } + } + CONTEXTGenericAccessors context{aContext ? *aContext : context_buf}; + +# if defined(_M_IX86) + // Setup initial stack frame to walk from. + STACKFRAME64 frame64; + memset(&frame64, 0, sizeof(frame64)); + frame64.AddrPC.Offset = context.PC(); + frame64.AddrStack.Offset = context.SP(); + frame64.AddrFrame.Offset = context.BP(); + frame64.AddrPC.Mode = AddrModeFlat; + frame64.AddrStack.Mode = AddrModeFlat; + frame64.AddrFrame.Mode = AddrModeFlat; + frame64.AddrReturn.Mode = AddrModeFlat; +# endif + +# if defined(_M_AMD64) || defined(_M_ARM64) + // If there are any active suppressions, then at least one thread (we don't + // know which) is holding a lock that can deadlock RtlVirtualUnwind. Since + // that thread may be the one that we're trying to unwind, we can't proceed. + // + // But if there are no suppressions, then our target thread can't be holding + // a lock, and it's safe to proceed. By virtue of being suspended, the target + // thread can't acquire any new locks during the unwind process, so we only + // need to do this check once. After that, sStackWalkSuppressions can be + // changed by other threads while we're unwinding, and that's fine because + // we can't deadlock with those threads. + if (sStackWalkSuppressions) { + return; + } +# endif + +# if defined(_M_AMD64) || defined(_M_ARM64) + bool firstFrame = true; +# endif + + FrameSkipper skipper(aFirstFramePC); + + uint32_t frames = 0; + + // Now walk the stack. + while (true) { + DWORD64 addr; + DWORD64 spaddr; + +# if defined(_M_IX86) + // 32-bit frame unwinding. + // Debug routines are not threadsafe, so grab the lock. + EnterCriticalSection(&gDbgHelpCS); + BOOL ok = StackWalk64( +# if defined _M_IX86 + IMAGE_FILE_MACHINE_I386, +# endif + ::GetCurrentProcess(), targetThread, &frame64, context.CONTEXTPtr(), + nullptr, + SymFunctionTableAccess64, // function table access routine + SymGetModuleBase64, // module base routine + 0); + LeaveCriticalSection(&gDbgHelpCS); + + if (ok) { + addr = frame64.AddrPC.Offset; + spaddr = frame64.AddrStack.Offset; + } else { + addr = 0; + spaddr = 0; + if (walkCallingThread) { + PrintError("WalkStack64"); + } + } + + if (!ok) { + break; + } + +# elif defined(_M_AMD64) || defined(_M_ARM64) + + auto currentInstr = context.PC(); + + // If we reach a frame in JIT code, we don't have enough information to + // unwind, so we have to give up. + if (sJitCodeRegionStart && (uint8_t*)currentInstr >= sJitCodeRegionStart && + (uint8_t*)currentInstr < sJitCodeRegionStart + sJitCodeRegionSize) { + break; + } + + // We must also avoid msmpeg2vdec.dll's JIT region: they don't generate + // unwind data, so their JIT unwind callback just throws up its hands and + // terminates the process. + if (sMsMpegJitCodeRegionStart && + (uint8_t*)currentInstr >= sMsMpegJitCodeRegionStart && + (uint8_t*)currentInstr < + sMsMpegJitCodeRegionStart + sMsMpegJitCodeRegionSize) { + break; + } + + // 64-bit frame unwinding. + // Try to look up unwind metadata for the current function. + ULONG64 imageBase; + PRUNTIME_FUNCTION runtimeFunction = + RtlLookupFunctionEntry(currentInstr, &imageBase, NULL); + + if (runtimeFunction) { + PVOID dummyHandlerData; + ULONG64 dummyEstablisherFrame; + RtlVirtualUnwind(UNW_FLAG_NHANDLER, imageBase, currentInstr, + runtimeFunction, context.CONTEXTPtr(), &dummyHandlerData, + &dummyEstablisherFrame, nullptr); + } else if (firstFrame) { + // Leaf functions can be unwound by hand. + context.PC() = *reinterpret_cast<DWORD64*>(context.SP()); + context.SP() += sizeof(void*); + } else { + // Something went wrong. + break; + } + + addr = context.PC(); + spaddr = context.SP(); + firstFrame = false; +# else +# error "unknown platform" +# endif + + if (addr == 0) { + break; + } + + if (skipper.ShouldSkipPC((void*)addr)) { + continue; + } + + aCallback(++frames, (void*)addr, (void*)spaddr, aClosure); + + if (aMaxFrames != 0 && frames == aMaxFrames) { + break; + } + +# if defined(_M_IX86) + if (frame64.AddrReturn.Offset == 0) { + break; + } +# endif + } +} + +MFBT_API void MozStackWalkThread(MozWalkStackCallback aCallback, + uint32_t aMaxFrames, void* aClosure, + HANDLE aThread, CONTEXT* aContext) { + // We don't pass a aFirstFramePC because we walk the stack for another + // thread. + DoMozStackWalkThread(aCallback, nullptr, aMaxFrames, aClosure, aThread, + aContext); +} + +MFBT_API void MozStackWalk(MozWalkStackCallback aCallback, + const void* aFirstFramePC, uint32_t aMaxFrames, + void* aClosure) { + DoMozStackWalkThread(aCallback, aFirstFramePC ? aFirstFramePC : CallerPC(), + aMaxFrames, aClosure, nullptr, nullptr); +} + +static BOOL CALLBACK callbackEspecial64(PCSTR aModuleName, DWORD64 aModuleBase, + ULONG aModuleSize, PVOID aUserContext) { + BOOL retval = TRUE; + DWORD64 addr = *(DWORD64*)aUserContext; + + /* + * You'll want to control this if we are running on an + * architecture where the addresses go the other direction. + * Not sure this is even a realistic consideration. + */ + const BOOL addressIncreases = TRUE; + + /* + * If it falls in side the known range, load the symbols. + */ + if (addressIncreases + ? (addr >= aModuleBase && addr <= (aModuleBase + aModuleSize)) + : (addr <= aModuleBase && addr >= (aModuleBase - aModuleSize))) { + retval = !!SymLoadModule64(GetCurrentProcess(), nullptr, (PSTR)aModuleName, + nullptr, aModuleBase, aModuleSize); + if (!retval) { + PrintError("SymLoadModule64"); + } + } + + return retval; +} + +/* + * SymGetModuleInfoEspecial + * + * Attempt to determine the module information. + * Bug 112196 says this DLL may not have been loaded at the time + * SymInitialize was called, and thus the module information + * and symbol information is not available. + * This code rectifies that problem. + */ + +// New members were added to IMAGEHLP_MODULE64 (that show up in the +// Platform SDK that ships with VC8, but not the Platform SDK that ships +// with VC7.1, i.e., between DbgHelp 6.0 and 6.1), but we don't need to +// use them, and it's useful to be able to function correctly with the +// older library. (Stock Windows XP SP2 seems to ship with dbghelp.dll +// version 5.1.) Since Platform SDK version need not correspond to +// compiler version, and the version number in debughlp.h was NOT bumped +// when these changes were made, ifdef based on a constant that was +// added between these versions. +# ifdef SSRVOPT_SETCONTEXT +# define NS_IMAGEHLP_MODULE64_SIZE \ + (((offsetof(IMAGEHLP_MODULE64, LoadedPdbName) + sizeof(DWORD64) - 1) / \ + sizeof(DWORD64)) * \ + sizeof(DWORD64)) +# else +# define NS_IMAGEHLP_MODULE64_SIZE sizeof(IMAGEHLP_MODULE64) +# endif + +BOOL SymGetModuleInfoEspecial64(HANDLE aProcess, DWORD64 aAddr, + PIMAGEHLP_MODULE64 aModuleInfo, + PIMAGEHLP_LINE64 aLineInfo) { + BOOL retval = FALSE; + + /* + * Init the vars if we have em. + */ + aModuleInfo->SizeOfStruct = NS_IMAGEHLP_MODULE64_SIZE; + if (aLineInfo) { + aLineInfo->SizeOfStruct = sizeof(IMAGEHLP_LINE64); + } + + /* + * Give it a go. + * It may already be loaded. + */ + retval = SymGetModuleInfo64(aProcess, aAddr, aModuleInfo); + if (retval == FALSE) { + /* + * Not loaded, here's the magic. + * Go through all the modules. + */ + // Need to cast to PENUMLOADED_MODULES_CALLBACK64 because the + // constness of the first parameter of + // PENUMLOADED_MODULES_CALLBACK64 varies over SDK versions (from + // non-const to const over time). See bug 391848 and bug + // 415426. + BOOL enumRes = EnumerateLoadedModules64( + aProcess, (PENUMLOADED_MODULES_CALLBACK64)callbackEspecial64, + (PVOID)&aAddr); + if (enumRes != FALSE) { + /* + * One final go. + * If it fails, then well, we have other problems. + */ + retval = SymGetModuleInfo64(aProcess, aAddr, aModuleInfo); + } + } + + /* + * If we got module info, we may attempt line info as well. + * We will not report failure if this does not work. + */ + if (retval != FALSE && aLineInfo) { + DWORD displacement = 0; + BOOL lineRes = FALSE; + lineRes = SymGetLineFromAddr64(aProcess, aAddr, &displacement, aLineInfo); + if (!lineRes) { + // Clear out aLineInfo to indicate that it's not valid + memset(aLineInfo, 0, sizeof(*aLineInfo)); + } + } + + return retval; +} + +static bool EnsureSymInitialized() { + static bool gInitialized = false; + bool retStat; + + if (gInitialized) { + return gInitialized; + } + + InitializeDbgHelpCriticalSection(); + + SymSetOptions(SYMOPT_LOAD_LINES | SYMOPT_UNDNAME); + retStat = SymInitialize(GetCurrentProcess(), nullptr, TRUE); + if (!retStat) { + PrintError("SymInitialize"); + } + + gInitialized = retStat; + /* XXX At some point we need to arrange to call SymCleanup */ + + return retStat; +} + +MFBT_API bool MozDescribeCodeAddress(void* aPC, + MozCodeAddressDetails* aDetails) { + aDetails->library[0] = '\0'; + aDetails->loffset = 0; + aDetails->filename[0] = '\0'; + aDetails->lineno = 0; + aDetails->function[0] = '\0'; + aDetails->foffset = 0; + + if (!EnsureSymInitialized()) { + return false; + } + + HANDLE myProcess = ::GetCurrentProcess(); + BOOL ok; + + // debug routines are not threadsafe, so grab the lock. + EnterCriticalSection(&gDbgHelpCS); + + // + // Attempt to load module info before we attempt to resolve the symbol. + // This just makes sure we get good info if available. + // + + DWORD64 addr = (DWORD64)aPC; + IMAGEHLP_MODULE64 modInfo; + IMAGEHLP_LINE64 lineInfo; + BOOL modInfoRes; + modInfoRes = SymGetModuleInfoEspecial64(myProcess, addr, &modInfo, &lineInfo); + + if (modInfoRes) { + strncpy(aDetails->library, modInfo.LoadedImageName, + sizeof(aDetails->library)); + aDetails->library[mozilla::ArrayLength(aDetails->library) - 1] = '\0'; + aDetails->loffset = (char*)aPC - (char*)modInfo.BaseOfImage; + + if (lineInfo.FileName) { + strncpy(aDetails->filename, lineInfo.FileName, + sizeof(aDetails->filename)); + aDetails->filename[mozilla::ArrayLength(aDetails->filename) - 1] = '\0'; + aDetails->lineno = lineInfo.LineNumber; + } + } + + ULONG64 buffer[(sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(TCHAR) + + sizeof(ULONG64) - 1) / + sizeof(ULONG64)]; + PSYMBOL_INFO pSymbol = (PSYMBOL_INFO)buffer; + pSymbol->SizeOfStruct = sizeof(SYMBOL_INFO); + pSymbol->MaxNameLen = MAX_SYM_NAME; + + DWORD64 displacement; + ok = SymFromAddr(myProcess, addr, &displacement, pSymbol); + + if (ok) { + strncpy(aDetails->function, pSymbol->Name, sizeof(aDetails->function)); + aDetails->function[mozilla::ArrayLength(aDetails->function) - 1] = '\0'; + aDetails->foffset = static_cast<ptrdiff_t>(displacement); + } + + LeaveCriticalSection(&gDbgHelpCS); // release our lock + return true; +} + +// i386 or PPC Linux stackwalking code +#elif HAVE_DLADDR && \ + (HAVE__UNWIND_BACKTRACE || MOZ_STACKWALK_SUPPORTS_LINUX || \ + MOZ_STACKWALK_SUPPORTS_MACOSX) + +# include <stdlib.h> +# include <stdio.h> + +// On glibc 2.1, the Dl_info api defined in <dlfcn.h> is only exposed +// if __USE_GNU is defined. I suppose its some kind of standards +// adherence thing. +// +# if (__GLIBC_MINOR__ >= 1) && !defined(__USE_GNU) +# define __USE_GNU +# endif + +// This thing is exported by libstdc++ +// Yes, this is a gcc only hack +# if defined(MOZ_DEMANGLE_SYMBOLS) +# include <cxxabi.h> +# endif // MOZ_DEMANGLE_SYMBOLS + +namespace mozilla { + +void DemangleSymbol(const char* aSymbol, char* aBuffer, int aBufLen) { + aBuffer[0] = '\0'; + +# if defined(MOZ_DEMANGLE_SYMBOLS) + /* See demangle.h in the gcc source for the voodoo */ + char* demangled = abi::__cxa_demangle(aSymbol, 0, 0, 0); + + if (demangled) { + strncpy(aBuffer, demangled, aBufLen); + aBuffer[aBufLen - 1] = '\0'; + free(demangled); + } +# endif // MOZ_DEMANGLE_SYMBOLS +} + +} // namespace mozilla + +// {x86, ppc} x {Linux, Mac} stackwalking code. +# if ((defined(__i386) || defined(PPC) || defined(__ppc__)) && \ + (MOZ_STACKWALK_SUPPORTS_MACOSX || MOZ_STACKWALK_SUPPORTS_LINUX)) + +static void DoFramePointerStackWalk(MozWalkStackCallback aCallback, + const void* aFirstFramePC, + uint32_t aMaxFrames, void* aClosure, + void** aBp, void* aStackEnd); + +MFBT_API void MozStackWalk(MozWalkStackCallback aCallback, + const void* aFirstFramePC, uint32_t aMaxFrames, + void* aClosure) { + // Get the frame pointer + void** bp = (void**)__builtin_frame_address(0); + + void* stackEnd; +# if HAVE___LIBC_STACK_END + stackEnd = __libc_stack_end; +# elif defined(XP_DARWIN) + stackEnd = pthread_get_stackaddr_np(pthread_self()); +# elif defined(ANDROID) + pthread_attr_t sattr; + pthread_attr_init(&sattr); + pthread_getattr_np(pthread_self(), &sattr); + void* stackBase = stackEnd = nullptr; + size_t stackSize = 0; + if (gettid() != getpid()) { + // bionic's pthread_attr_getstack doesn't tell the truth for the main + // thread (see bug 846670). So don't use it for the main thread. + if (!pthread_attr_getstack(&sattr, &stackBase, &stackSize)) { + stackEnd = static_cast<char*>(stackBase) + stackSize; + } else { + stackEnd = nullptr; + } + } + if (!stackEnd) { + // So consider the current frame pointer + an arbitrary size of 8MB + // (modulo overflow ; not really arbitrary as it's the default stack + // size for the main thread) if pthread_attr_getstack failed for + // some reason (or was skipped). + static const uintptr_t kMaxStackSize = 8 * 1024 * 1024; + uintptr_t maxStackStart = uintptr_t(-1) - kMaxStackSize; + uintptr_t stackStart = std::max(maxStackStart, uintptr_t(bp)); + stackEnd = reinterpret_cast<void*>(stackStart + kMaxStackSize); + } +# else +# error Unsupported configuration +# endif + DoFramePointerStackWalk(aCallback, aFirstFramePC, aMaxFrames, aClosure, bp, + stackEnd); +} + +# elif defined(HAVE__UNWIND_BACKTRACE) + +// libgcc_s.so symbols _Unwind_Backtrace@@GCC_3.3 and _Unwind_GetIP@@GCC_3.0 +# include <unwind.h> + +struct unwind_info { + MozWalkStackCallback callback; + FrameSkipper skipper; + int maxFrames; + int numFrames; + void* closure; +}; + +static _Unwind_Reason_Code unwind_callback(struct _Unwind_Context* context, + void* closure) { + unwind_info* info = static_cast<unwind_info*>(closure); + void* pc = reinterpret_cast<void*>(_Unwind_GetIP(context)); + // TODO Use something like '_Unwind_GetGR()' to get the stack pointer. + if (!info->skipper.ShouldSkipPC(pc)) { + info->numFrames++; + (*info->callback)(info->numFrames, pc, nullptr, info->closure); + if (info->maxFrames != 0 && info->numFrames == info->maxFrames) { + // Again, any error code that stops the walk will do. + return _URC_FOREIGN_EXCEPTION_CAUGHT; + } + } + return _URC_NO_REASON; +} + +MFBT_API void MozStackWalk(MozWalkStackCallback aCallback, + const void* aFirstFramePC, uint32_t aMaxFrames, + void* aClosure) { + unwind_info info; + info.callback = aCallback; + info.skipper = FrameSkipper(aFirstFramePC ? aFirstFramePC : CallerPC()); + info.maxFrames = aMaxFrames; + info.numFrames = 0; + info.closure = aClosure; + + // We ignore the return value from _Unwind_Backtrace. There are three main + // reasons for this. + // - On ARM/Android bionic's _Unwind_Backtrace usually (always?) returns + // _URC_FAILURE. See + // https://bugzilla.mozilla.org/show_bug.cgi?id=717853#c110. + // - If aMaxFrames != 0, we want to stop early, and the only way to do that + // is to make unwind_callback return something other than _URC_NO_REASON, + // which causes _Unwind_Backtrace to return a non-success code. + // - MozStackWalk doesn't have a return value anyway. + (void)_Unwind_Backtrace(unwind_callback, &info); +} + +# endif + +bool MFBT_API MozDescribeCodeAddress(void* aPC, + MozCodeAddressDetails* aDetails) { + aDetails->library[0] = '\0'; + aDetails->loffset = 0; + aDetails->filename[0] = '\0'; + aDetails->lineno = 0; + aDetails->function[0] = '\0'; + aDetails->foffset = 0; + + Dl_info info; + +# if defined(ANDROID) && defined(MOZ_LINKER) + int ok = __wrap_dladdr(aPC, &info); +# else + int ok = dladdr(aPC, &info); +# endif + + if (!ok) { + return true; + } + + strncpy(aDetails->library, info.dli_fname, sizeof(aDetails->library)); + aDetails->library[mozilla::ArrayLength(aDetails->library) - 1] = '\0'; + aDetails->loffset = (char*)aPC - (char*)info.dli_fbase; + +# if !defined(XP_FREEBSD) + // On FreeBSD, dli_sname is unusably bad, it often returns things like + // 'gtk_xtbin_new' or 'XRE_GetBootstrap' instead of long C++ symbols. Just let + // GetFunction do the lookup directly in the ELF image. + + const char* symbol = info.dli_sname; + if (!symbol || symbol[0] == '\0') { + return true; + } + + DemangleSymbol(symbol, aDetails->function, sizeof(aDetails->function)); + + if (aDetails->function[0] == '\0') { + // Just use the mangled symbol if demangling failed. + strncpy(aDetails->function, symbol, sizeof(aDetails->function)); + aDetails->function[mozilla::ArrayLength(aDetails->function) - 1] = '\0'; + } + + aDetails->foffset = (char*)aPC - (char*)info.dli_saddr; +# endif + + return true; +} + +#else // unsupported platform. + +MFBT_API void MozStackWalk(MozWalkStackCallback aCallback, + const void* aFirstFramePC, uint32_t aMaxFrames, + void* aClosure) {} + +MFBT_API bool MozDescribeCodeAddress(void* aPC, + MozCodeAddressDetails* aDetails) { + aDetails->library[0] = '\0'; + aDetails->loffset = 0; + aDetails->filename[0] = '\0'; + aDetails->lineno = 0; + aDetails->function[0] = '\0'; + aDetails->foffset = 0; + return false; +} + +#endif + +#if defined(XP_WIN) || defined(XP_MACOSX) || defined(XP_LINUX) + +# if defined(XP_MACOSX) && defined(__aarch64__) +// On macOS arm64, system libraries are arm64e binaries, and arm64e can do +// pointer authentication: The low bits of the pointer are the actual pointer +// value, and the high bits are an encrypted hash. During stackwalking, we need +// to strip off this hash. In theory, ptrauth_strip would be the right function +// to call for this. However, that function is a no-op unless it's called from +// code which also builds as arm64e - which we do not. So we cannot use it. So +// for now, we hardcode a mask that seems to work today: 40 bits for the pointer +// and 24 bits for the hash seems to do the trick. We can worry about +// dynamically computing the correct mask if this ever stops working. +const uintptr_t kPointerMask = + (uintptr_t(1) << 40) - 1; // 40 bits pointer, 24 bit PAC +# else +const uintptr_t kPointerMask = ~uintptr_t(0); +# endif + +MOZ_ASAN_IGNORE +static void DoFramePointerStackWalk(MozWalkStackCallback aCallback, + const void* aFirstFramePC, + uint32_t aMaxFrames, void* aClosure, + void** aBp, void* aStackEnd) { + // Stack walking code courtesy Kipp's "leaky". + + FrameSkipper skipper(aFirstFramePC); + uint32_t numFrames = 0; + + // Sanitize the given aBp. Assume that something reasonably close to + // but before the stack end is going be a valid frame pointer. Also + // check that it is an aligned address. This increases the chances + // that if the pointer is not valid (which might happen if the caller + // called __builtin_frame_address(1) and its frame is busted for some + // reason), we won't read it, leading to a crash. Because the calling + // code is not using frame pointers when returning, it might actually + // recover just fine. + static const uintptr_t kMaxStackSize = 8 * 1024 * 1024; + if (uintptr_t(aBp) < uintptr_t(aStackEnd) - + std::min(kMaxStackSize, uintptr_t(aStackEnd)) || + aBp >= aStackEnd || (uintptr_t(aBp) & 3)) { + return; + } + + while (aBp) { + void** next = (void**)*aBp; + // aBp may not be a frame pointer on i386 if code was compiled with + // -fomit-frame-pointer, so do some sanity checks. + // (aBp should be a frame pointer on ppc(64) but checking anyway may help + // a little if the stack has been corrupted.) + // We don't need to check against the begining of the stack because + // we can assume that aBp > sp + if (next <= aBp || next >= aStackEnd || (uintptr_t(next) & 3)) { + break; + } +# if (defined(__ppc__) && defined(XP_MACOSX)) || defined(__powerpc64__) + // ppc mac or powerpc64 linux + void* pc = *(aBp + 2); + aBp += 3; +# else // i386 or powerpc32 linux + void* pc = *(aBp + 1); + aBp += 2; +# endif + + // Strip off pointer authentication hash, if present. For now, it looks + // like only return addresses require stripping, and stack pointers do + // not. This might change in the future. + pc = (void*)((uintptr_t)pc & kPointerMask); + + if (!skipper.ShouldSkipPC(pc)) { + // Assume that the SP points to the BP of the function + // it called. We can't know the exact location of the SP + // but this should be sufficient for our use the SP + // to order elements on the stack. + numFrames++; + (*aCallback)(numFrames, pc, aBp, aClosure); + if (aMaxFrames != 0 && numFrames == aMaxFrames) { + break; + } + } + aBp = next; + } +} + +namespace mozilla { + +MFBT_API void FramePointerStackWalk(MozWalkStackCallback aCallback, + uint32_t aMaxFrames, void* aClosure, + void** aBp, void* aStackEnd) { + // We don't pass a aFirstFramePC because we start walking the stack from the + // frame at aBp. + DoFramePointerStackWalk(aCallback, nullptr, aMaxFrames, aClosure, aBp, + aStackEnd); +} + +} // namespace mozilla + +#else + +namespace mozilla { +MFBT_API void FramePointerStackWalk(MozWalkStackCallback aCallback, + uint32_t aMaxFrames, void* aClosure, + void** aBp, void* aStackEnd) {} +} // namespace mozilla + +#endif + +MFBT_API int MozFormatCodeAddressDetails( + char* aBuffer, uint32_t aBufferSize, uint32_t aFrameNumber, void* aPC, + const MozCodeAddressDetails* aDetails) { + return MozFormatCodeAddress(aBuffer, aBufferSize, aFrameNumber, aPC, + aDetails->function, aDetails->library, + aDetails->loffset, aDetails->filename, + aDetails->lineno); +} + +MFBT_API int MozFormatCodeAddress(char* aBuffer, uint32_t aBufferSize, + uint32_t aFrameNumber, const void* aPC, + const char* aFunction, const char* aLibrary, + ptrdiff_t aLOffset, const char* aFileName, + uint32_t aLineNo) { + const char* function = aFunction && aFunction[0] ? aFunction : "???"; + if (aFileName && aFileName[0]) { + // We have a filename and (presumably) a line number. Use them. + return SprintfBuf(aBuffer, aBufferSize, "#%02u: %s (%s:%u)", aFrameNumber, + function, aFileName, aLineNo); + } else if (aLibrary && aLibrary[0]) { + // We have no filename, but we do have a library name. Use it and the + // library offset, and print them in a way that `fix_stacks.py` can + // post-process. + return SprintfBuf(aBuffer, aBufferSize, "#%02u: %s[%s +0x%" PRIxPTR "]", + aFrameNumber, function, aLibrary, + static_cast<uintptr_t>(aLOffset)); + } else { + // We have nothing useful to go on. (The format string is split because + // '??)' is a trigraph and causes a warning, sigh.) + return SprintfBuf(aBuffer, aBufferSize, + "#%02u: ??? (???:???" + ")", + aFrameNumber); + } +} + +static void EnsureWrite(FILE* aStream, const char* aBuf, size_t aLen) { +#ifdef XP_WIN + int fd = _fileno(aStream); +#else + int fd = fileno(aStream); +#endif + while (aLen > 0) { +#ifdef XP_WIN + auto written = _write(fd, aBuf, aLen); +#else + auto written = write(fd, aBuf, aLen); +#endif + if (written <= 0 || size_t(written) > aLen) { + break; + } + aBuf += written; + aLen -= written; + } +} + +template <int N> +static int PrintStackFrameBuf(char (&aBuf)[N], uint32_t aFrameNumber, void* aPC, + void* aSP) { + MozCodeAddressDetails details; + MozDescribeCodeAddress(aPC, &details); + int len = + MozFormatCodeAddressDetails(aBuf, N - 1, aFrameNumber, aPC, &details); + len = std::min(len, N - 2); + aBuf[len++] = '\n'; + aBuf[len] = '\0'; + return len; +} + +static void PrintStackFrame(uint32_t aFrameNumber, void* aPC, void* aSP, + void* aClosure) { + FILE* stream = (FILE*)aClosure; + char buf[1025]; // 1024 + 1 for trailing '\n' + int len = PrintStackFrameBuf(buf, aFrameNumber, aPC, aSP); + fflush(stream); + EnsureWrite(stream, buf, len); +} + +static bool WalkTheStackEnabled() { + static bool result = [] { + char* value = getenv("MOZ_DISABLE_WALKTHESTACK"); + return !(value && value[0]); + }(); + return result; +} + +MFBT_API void MozWalkTheStack(FILE* aStream, const void* aFirstFramePC, + uint32_t aMaxFrames) { + if (WalkTheStackEnabled()) { + MozStackWalk(PrintStackFrame, aFirstFramePC ? aFirstFramePC : CallerPC(), + aMaxFrames, aStream); + } +} + +static void WriteStackFrame(uint32_t aFrameNumber, void* aPC, void* aSP, + void* aClosure) { + auto writer = (void (*)(const char*))aClosure; + char buf[1024]; + PrintStackFrameBuf(buf, aFrameNumber, aPC, aSP); + writer(buf); +} + +MFBT_API void MozWalkTheStackWithWriter(void (*aWriter)(const char*), + const void* aFirstFramePC, + uint32_t aMaxFrames) { + if (WalkTheStackEnabled()) { + MozStackWalk(WriteStackFrame, aFirstFramePC ? aFirstFramePC : CallerPC(), + aMaxFrames, (void*)aWriter); + } +} diff --git a/mozglue/misc/StackWalk.h b/mozglue/misc/StackWalk.h new file mode 100644 index 0000000000..250160934f --- /dev/null +++ b/mozglue/misc/StackWalk.h @@ -0,0 +1,215 @@ +/* -*- 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/. */ + +/* APIs for getting a stack trace of the current thread */ + +#ifndef mozilla_StackWalk_h +#define mozilla_StackWalk_h + +#include "mozilla/Types.h" +#include <stdint.h> +#include <stdio.h> + +MOZ_BEGIN_EXTERN_C + +/** + * Returns the position of the Program Counter for the caller of the current + * function. This is meant to be used to feed the aFirstFramePC argument to + * MozStackWalk or MozWalkTheStack*, and should be used in the last function + * that should be skipped in the trace, and passed down to MozStackWalk or + * MozWalkTheStack*, through any intermediaries. + * + * THIS DOES NOT 100% RELIABLY GIVE THE CALLER PC, but marking functions + * calling this macro with MOZ_NEVER_INLINE gets us close. In cases it doesn't + * give the caller's PC, it may give the caller of the caller, or its caller, + * etc. depending on tail call optimization. + * + * Past versions of stackwalking relied on passing a constant number of frames + * to skip to MozStackWalk or MozWalkTheStack, which fell short in more cases + * (inlining of intermediaries, tail call optimization). + */ +#define CallerPC() __builtin_extract_return_addr(__builtin_return_address(0)) + +/** + * The callback for MozStackWalk and MozStackWalkThread. + * + * @param aFrameNumber The frame number (starts at 1, not 0). + * @param aPC The program counter value. + * @param aSP The best approximation possible of what the stack + * pointer will be pointing to when the execution returns + * to executing that at aPC. If no approximation can + * be made it will be nullptr. + * @param aClosure Extra data passed in from MozStackWalk() or + * MozStackWalkThread(). + */ +typedef void (*MozWalkStackCallback)(uint32_t aFrameNumber, void* aPC, + void* aSP, void* aClosure); + +/** + * Call aCallback for each stack frame on the current thread, from + * the caller of MozStackWalk to main (or above). + * + * @param aCallback Callback function, called once per frame. + * @param aFirstFramePC Position of the Program Counter where the trace + * starts from. All frames seen before reaching that + * address are skipped. Nullptr means that the first + * callback will be for the caller of MozStackWalk. + * @param aMaxFrames Maximum number of frames to trace. 0 means no limit. + * @param aClosure Caller-supplied data passed through to aCallback. + * + * May skip some stack frames due to compiler optimizations or code + * generation. + */ +MFBT_API void MozStackWalk(MozWalkStackCallback aCallback, + const void* aFirstFramePC, uint32_t aMaxFrames, + void* aClosure); + +typedef struct { + /* + * The name of the shared library or executable containing an + * address and the address's offset within that library, or empty + * string and zero if unknown. + */ + char library[256]; + ptrdiff_t loffset; + /* + * The name of the file name and line number of the code + * corresponding to the address, or empty string and zero if + * unknown. + */ + char filename[256]; + unsigned long lineno; + /* + * The name of the function containing an address and the address's + * offset within that function, or empty string and zero if unknown. + */ + char function[256]; + ptrdiff_t foffset; +} MozCodeAddressDetails; + +/** + * For a given pointer to code, fill in the pieces of information used + * when printing a stack trace. + * + * @param aPC The code address. + * @param aDetails A structure to be filled in with the result. + */ +MFBT_API bool MozDescribeCodeAddress(void* aPC, + MozCodeAddressDetails* aDetails); + +/** + * Format the information about a code address in a format suitable for + * stack traces on the current platform. When available, this string + * should contain the function name, source file, and line number. When + * these are not available, library and offset should be reported, if + * possible. + * + * Note that this output is parsed by several scripts including the fix*.py and + * make-tree.pl scripts in tools/rb/. It should only be change with care, and + * in conjunction with those scripts. + * + * @param aBuffer A string to be filled in with the description. + * The string will always be null-terminated. + * @param aBufferSize The size, in bytes, of aBuffer, including + * room for the terminating null. If the information + * to be printed would be larger than aBuffer, it + * will be truncated so that aBuffer[aBufferSize-1] + * is the terminating null. + * @param aFrameNumber The frame number. + * @param aPC The code address. + * @param aFunction The function name. Possibly null or the empty string. + * @param aLibrary The library name. Possibly null or the empty string. + * @param aLOffset The library offset. + * @param aFileName The filename. Possibly null or the empty string. + * @param aLineNo The line number. Possibly zero. + * @return The minimum number of characters necessary to format + * the frame information, without the terminating null. + * The buffer will have been truncated if the returned + * value is greater than aBufferSize-1. + */ +MFBT_API int MozFormatCodeAddress(char* aBuffer, uint32_t aBufferSize, + uint32_t aFrameNumber, const void* aPC, + const char* aFunction, const char* aLibrary, + ptrdiff_t aLOffset, const char* aFileName, + uint32_t aLineNo); + +/** + * Format the information about a code address in the same fashion as + * MozFormatCodeAddress. + * + * @param aBuffer A string to be filled in with the description. + * The string will always be null-terminated. + * @param aBufferSize The size, in bytes, of aBuffer, including + * room for the terminating null. If the information + * to be printed would be larger than aBuffer, it + * will be truncated so that aBuffer[aBufferSize-1] + * is the terminating null. + * @param aFrameNumber The frame number. + * @param aPC The code address. + * @param aDetails The value filled in by MozDescribeCodeAddress(aPC). + * @return The minimum number of characters necessary to format + * the frame information, without the terminating null. + * The buffer will have been truncated if the returned + * value is greater than aBufferSize-1. + */ +MFBT_API int MozFormatCodeAddressDetails(char* aBuffer, uint32_t aBufferSize, + uint32_t aFrameNumber, void* aPC, + const MozCodeAddressDetails* aDetails); + +#ifdef __cplusplus +# define FRAMES_DEFAULT = 0 +#else +# define FRAMES_DEFAULT +#endif +/** + * Walk the stack and print the stack trace to the given stream. + * + * @param aStream A stdio stream. + * @param aFirstFramePC Position of the Program Counter where the trace + * starts from. All frames seen before reaching that + * address are skipped. Nullptr means that the first + * callback will be for the caller of MozWalkTheStack. + * @param aMaxFrames Maximum number of frames to trace. 0 means no limit. + */ +MFBT_API void MozWalkTheStack(FILE* aStream, + const void* aFirstFramePC FRAMES_DEFAULT, + uint32_t aMaxFrames FRAMES_DEFAULT); + +/** + * Walk the stack and send each stack trace line to a callback writer. + * Each line string is null terminated but doesn't contain a '\n' character. + * + * @param aWriter The callback. + * @param aFirstFramePC Position of the Program Counter where the trace + * starts from. All frames seen before reaching that + * address are skipped. Nullptr means that the first + * callback will be for the caller of + * MozWalkTheStackWithWriter. + * @param aMaxFrames Maximum number of frames to trace. 0 means no limit. + */ +MFBT_API void MozWalkTheStackWithWriter( + void (*aWriter)(const char*), const void* aFirstFramePC FRAMES_DEFAULT, + uint32_t aMaxFrames FRAMES_DEFAULT); + +#undef FRAMES_DEFAULT + +MOZ_END_EXTERN_C + +#ifdef __cplusplus +namespace mozilla { + +MFBT_API void FramePointerStackWalk(MozWalkStackCallback aCallback, + uint32_t aMaxFrames, void* aClosure, + void** aBp, void* aStackEnd); + +# if defined(XP_LINUX) || defined(XP_FREEBSD) +MFBT_API void DemangleSymbol(const char* aSymbol, char* aBuffer, int aBufLen); +# endif + +} // namespace mozilla +#endif + +#endif diff --git a/mozglue/misc/StackWalkThread.h b/mozglue/misc/StackWalkThread.h new file mode 100644 index 0000000000..1db789de35 --- /dev/null +++ b/mozglue/misc/StackWalkThread.h @@ -0,0 +1,34 @@ +/* -*- 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/. */ + +/* APIs for getting a stack trace of an arbitrary thread */ + +#ifndef mozilla_StackWalkThread_h +#define mozilla_StackWalkThread_h + +#include "mozilla/StackWalk.h" + +#include <windows.h> + +/** + * Like MozStackWalk, but walks the stack for another thread. + * Call aCallback for each stack frame on the current thread, from + * the caller of MozStackWalk to main (or above). + * + * @param aCallback Same as for MozStackWalk(). + * @param aMaxFrames Same as for MozStackWalk(). + * @param aClosure Same as for MozStackWalk(). + * @param aThread The handle of the thread whose stack is to be walked. + * If 0, walks the current thread. + * @param aContext A CONTEXT, presumably obtained with GetThreadContext() + * after suspending the thread with SuspendThread(). If + * null, the CONTEXT will be re-obtained. + */ +MFBT_API void MozStackWalkThread(MozWalkStackCallback aCallback, + uint32_t aMaxFrames, void* aClosure, + HANDLE aThread, CONTEXT* aContext); + +#endif diff --git a/mozglue/misc/StackWalk_windows.h b/mozglue/misc/StackWalk_windows.h new file mode 100644 index 0000000000..dca4d41064 --- /dev/null +++ b/mozglue/misc/StackWalk_windows.h @@ -0,0 +1,34 @@ +/* -*- 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/. */ + +#ifndef mozilla_StackWalk_windows_h +#define mozilla_StackWalk_windows_h + +#include "mozilla/Types.h" + +#if defined(_M_AMD64) || defined(_M_ARM64) +/** + * Allow stack walkers to work around the egregious win64 dynamic lookup table + * list API by locking around SuspendThread to avoid deadlock. + * + * See comment in StackWalk.cpp + */ +struct MOZ_RAII AutoSuppressStackWalking { + MFBT_API AutoSuppressStackWalking(); + MFBT_API ~AutoSuppressStackWalking(); +}; + +# if defined(IMPL_MFBT) +void SuppressStackWalking(); +void DesuppressStackWalking(); +# endif // defined(IMPL_MFBT) + +MFBT_API void RegisterJitCodeRegion(uint8_t* aStart, size_t size); + +MFBT_API void UnregisterJitCodeRegion(uint8_t* aStart, size_t size); +#endif // _M_AMD64 + +#endif // mozilla_StackWalk_windows_h diff --git a/mozglue/misc/TimeStamp.cpp b/mozglue/misc/TimeStamp.cpp new file mode 100644 index 0000000000..4133234617 --- /dev/null +++ b/mozglue/misc/TimeStamp.cpp @@ -0,0 +1,85 @@ +/* -*- 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/. */ + +/* + * Implementation of the OS-independent methods of the TimeStamp class + */ + +#include "mozilla/TimeStamp.h" +#include "mozilla/Uptime.h" +#include <string.h> +#include <stdlib.h> + +namespace mozilla { + +/** + * Wrapper class used to initialize static data used by the TimeStamp class + */ +struct TimeStampInitialization { + /** + * First timestamp taken when the class static initializers are run. This + * timestamp is used to sanitize timestamps coming from different sources. + */ + TimeStamp mFirstTimeStamp; + + /** + * Timestamp representing the time when the process was created. This field + * is populated lazily the first time this information is required and is + * replaced every time the process is restarted. + */ + TimeStamp mProcessCreation; + + TimeStampInitialization() { + TimeStamp::Startup(); + mFirstTimeStamp = TimeStamp::Now(); + // On Windows < 10, initializing the uptime requires `mFirstTimeStamp` to be + // valid. + mozilla::InitializeUptime(); + }; + + ~TimeStampInitialization() { TimeStamp::Shutdown(); }; +}; + +static TimeStampInitialization sInitOnce; + +MFBT_API TimeStamp TimeStamp::ProcessCreation() { + if (sInitOnce.mProcessCreation.IsNull()) { + char* mozAppRestart = getenv("MOZ_APP_RESTART"); + TimeStamp ts; + + /* When calling PR_SetEnv() with an empty value the existing variable may + * be unset or set to the empty string depending on the underlying platform + * thus we have to check if the variable is present and not empty. */ + if (mozAppRestart && (strcmp(mozAppRestart, "") != 0)) { + /* Firefox was restarted, use the first time-stamp we've taken as the new + * process startup time. */ + ts = sInitOnce.mFirstTimeStamp; + } else { + TimeStamp now = Now(); + uint64_t uptime = ComputeProcessUptime(); + + ts = now - TimeDuration::FromMicroseconds(static_cast<double>(uptime)); + + if ((ts > sInitOnce.mFirstTimeStamp) || (uptime == 0)) { + ts = sInitOnce.mFirstTimeStamp; + } + } + + sInitOnce.mProcessCreation = ts; + } + + return sInitOnce.mProcessCreation; +} + +void TimeStamp::RecordProcessRestart() { + sInitOnce.mProcessCreation = TimeStamp(); +} + +MFBT_API TimeStamp TimeStamp::FirstTimeStamp() { + return sInitOnce.mFirstTimeStamp; +} + +} // namespace mozilla diff --git a/mozglue/misc/TimeStamp.h b/mozglue/misc/TimeStamp.h new file mode 100644 index 0000000000..102116815d --- /dev/null +++ b/mozglue/misc/TimeStamp.h @@ -0,0 +1,603 @@ +/* -*- 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/. */ + +#ifndef mozilla_TimeStamp_h +#define mozilla_TimeStamp_h + +#include "mozilla/Assertions.h" +#include "mozilla/Attributes.h" +#include "mozilla/FloatingPoint.h" +#include "mozilla/Types.h" +#include <algorithm> // for std::min, std::max +#include <ostream> +#include <stdint.h> +#include <type_traits> + +namespace IPC { +template <typename T> +struct ParamTraits; +} // namespace IPC + +#ifdef XP_WIN +// defines TimeStampValue as a complex value keeping both +// GetTickCount and QueryPerformanceCounter values +# include "TimeStamp_windows.h" + +# include "mozilla/Maybe.h" // For TimeStamp::RawQueryPerformanceCounterValue +#endif + +namespace mozilla { + +#ifndef XP_WIN +typedef uint64_t TimeStampValue; +#endif + +class TimeStamp; + +/** + * Platform-specific implementation details of BaseTimeDuration. + */ +class BaseTimeDurationPlatformUtils { + public: + static MFBT_API double ToSeconds(int64_t aTicks); + static MFBT_API double ToSecondsSigDigits(int64_t aTicks); + static MFBT_API int64_t TicksFromMilliseconds(double aMilliseconds); + static MFBT_API int64_t ResolutionInTicks(); +}; + +/** + * Instances of this class represent the length of an interval of time. + * Negative durations are allowed, meaning the end is before the start. + * + * Internally the duration is stored as a int64_t in units of + * PR_TicksPerSecond() when building with NSPR interval timers, or a + * system-dependent unit when building with system clocks. The + * system-dependent unit must be constant, otherwise the semantics of + * this class would be broken. + * + * The ValueCalculator template parameter determines how arithmetic + * operations are performed on the integer count of ticks (mValue). + */ +template <typename ValueCalculator> +class BaseTimeDuration { + public: + // The default duration is 0. + constexpr BaseTimeDuration() : mValue(0) {} + // Allow construction using '0' as the initial value, for readability, + // but no other numbers (so we don't have any implicit unit conversions). + struct _SomethingVeryRandomHere; + MOZ_IMPLICIT BaseTimeDuration(_SomethingVeryRandomHere* aZero) : mValue(0) { + MOZ_ASSERT(!aZero, "Who's playing funny games here?"); + } + // Default copy-constructor and assignment are OK + + // Converting copy-constructor and assignment operator + template <typename E> + explicit BaseTimeDuration(const BaseTimeDuration<E>& aOther) + : mValue(aOther.mValue) {} + + template <typename E> + BaseTimeDuration& operator=(const BaseTimeDuration<E>& aOther) { + mValue = aOther.mValue; + return *this; + } + + double ToSeconds() const { + if (mValue == INT64_MAX) { + return PositiveInfinity<double>(); + } + if (mValue == INT64_MIN) { + return NegativeInfinity<double>(); + } + return BaseTimeDurationPlatformUtils::ToSeconds(mValue); + } + // Return a duration value that includes digits of time we think to + // be significant. This method should be used when displaying a + // time to humans. + double ToSecondsSigDigits() const { + if (mValue == INT64_MAX) { + return PositiveInfinity<double>(); + } + if (mValue == INT64_MIN) { + return NegativeInfinity<double>(); + } + return BaseTimeDurationPlatformUtils::ToSecondsSigDigits(mValue); + } + double ToMilliseconds() const { return ToSeconds() * 1000.0; } + double ToMicroseconds() const { return ToMilliseconds() * 1000.0; } + + // Using a double here is safe enough; with 53 bits we can represent + // durations up to over 280,000 years exactly. If the units of + // mValue do not allow us to represent durations of that length, + // long durations are clamped to the max/min representable value + // instead of overflowing. + static inline BaseTimeDuration FromSeconds(double aSeconds) { + return FromMilliseconds(aSeconds * 1000.0); + } + static BaseTimeDuration FromMilliseconds(double aMilliseconds) { + if (aMilliseconds == PositiveInfinity<double>()) { + return Forever(); + } + if (aMilliseconds == NegativeInfinity<double>()) { + return FromTicks(INT64_MIN); + } + return FromTicks( + BaseTimeDurationPlatformUtils::TicksFromMilliseconds(aMilliseconds)); + } + static inline BaseTimeDuration FromMicroseconds(double aMicroseconds) { + return FromMilliseconds(aMicroseconds / 1000.0); + } + + static constexpr BaseTimeDuration Forever() { return FromTicks(INT64_MAX); } + + BaseTimeDuration operator+(const BaseTimeDuration& aOther) const { + return FromTicks(ValueCalculator::Add(mValue, aOther.mValue)); + } + BaseTimeDuration operator-(const BaseTimeDuration& aOther) const { + return FromTicks(ValueCalculator::Subtract(mValue, aOther.mValue)); + } + BaseTimeDuration& operator+=(const BaseTimeDuration& aOther) { + mValue = ValueCalculator::Add(mValue, aOther.mValue); + return *this; + } + BaseTimeDuration& operator-=(const BaseTimeDuration& aOther) { + mValue = ValueCalculator::Subtract(mValue, aOther.mValue); + return *this; + } + BaseTimeDuration operator-() const { + // We don't just use FromTicks(ValueCalculator::Subtract(0, mValue)) + // since that won't give the correct result for -TimeDuration::Forever(). + int64_t ticks; + if (mValue == INT64_MAX) { + ticks = INT64_MIN; + } else if (mValue == INT64_MIN) { + ticks = INT64_MAX; + } else { + ticks = -mValue; + } + + return FromTicks(ticks); + } + + static BaseTimeDuration Max(const BaseTimeDuration& aA, + const BaseTimeDuration& aB) { + return FromTicks(std::max(aA.mValue, aB.mValue)); + } + static BaseTimeDuration Min(const BaseTimeDuration& aA, + const BaseTimeDuration& aB) { + return FromTicks(std::min(aA.mValue, aB.mValue)); + } + +#if defined(DEBUG) + int64_t GetValue() const { return mValue; } +#endif + + private: + // Block double multiplier (slower, imprecise if long duration) - Bug 853398. + // If required, use MultDouble explicitly and with care. + BaseTimeDuration operator*(const double aMultiplier) const = delete; + + // Block double divisor (for the same reason, and because dividing by + // fractional values would otherwise invoke the int64_t variant, and rounding + // the passed argument can then cause divide-by-zero) - Bug 1147491. + BaseTimeDuration operator/(const double aDivisor) const = delete; + + public: + BaseTimeDuration MultDouble(double aMultiplier) const { + return FromTicks(ValueCalculator::Multiply(mValue, aMultiplier)); + } + BaseTimeDuration operator*(const int32_t aMultiplier) const { + return FromTicks(ValueCalculator::Multiply(mValue, aMultiplier)); + } + BaseTimeDuration operator*(const uint32_t aMultiplier) const { + return FromTicks(ValueCalculator::Multiply(mValue, aMultiplier)); + } + BaseTimeDuration operator*(const int64_t aMultiplier) const { + return FromTicks(ValueCalculator::Multiply(mValue, aMultiplier)); + } + BaseTimeDuration operator*(const uint64_t aMultiplier) const { + if (aMultiplier > INT64_MAX) { + return Forever(); + } + return FromTicks(ValueCalculator::Multiply(mValue, aMultiplier)); + } + BaseTimeDuration operator/(const int64_t aDivisor) const { + MOZ_ASSERT(aDivisor != 0, "Division by zero"); + return FromTicks(ValueCalculator::Divide(mValue, aDivisor)); + } + double operator/(const BaseTimeDuration& aOther) const { + MOZ_ASSERT(aOther.mValue != 0, "Division by zero"); + return ValueCalculator::DivideDouble(mValue, aOther.mValue); + } + BaseTimeDuration operator%(const BaseTimeDuration& aOther) const { + MOZ_ASSERT(aOther.mValue != 0, "Division by zero"); + return FromTicks(ValueCalculator::Modulo(mValue, aOther.mValue)); + } + + template <typename E> + bool operator<(const BaseTimeDuration<E>& aOther) const { + return mValue < aOther.mValue; + } + template <typename E> + bool operator<=(const BaseTimeDuration<E>& aOther) const { + return mValue <= aOther.mValue; + } + template <typename E> + bool operator>=(const BaseTimeDuration<E>& aOther) const { + return mValue >= aOther.mValue; + } + template <typename E> + bool operator>(const BaseTimeDuration<E>& aOther) const { + return mValue > aOther.mValue; + } + template <typename E> + bool operator==(const BaseTimeDuration<E>& aOther) const { + return mValue == aOther.mValue; + } + template <typename E> + bool operator!=(const BaseTimeDuration<E>& aOther) const { + return mValue != aOther.mValue; + } + bool IsZero() const { return mValue == 0; } + explicit operator bool() const { return mValue != 0; } + + friend std::ostream& operator<<(std::ostream& aStream, + const BaseTimeDuration& aDuration) { + return aStream << aDuration.ToMilliseconds() << " ms"; + } + + // Return a best guess at the system's current timing resolution, + // which might be variable. BaseTimeDurations below this order of + // magnitude are meaningless, and those at the same order of + // magnitude or just above are suspect. + static BaseTimeDuration Resolution() { + return FromTicks(BaseTimeDurationPlatformUtils::ResolutionInTicks()); + } + + // We could define additional operators here: + // -- convert to/from other time units + // -- scale duration by a float + // but let's do that on demand. + // Comparing durations for equality will only lead to bugs on + // platforms with high-resolution timers. + + private: + friend class TimeStamp; + friend struct IPC::ParamTraits<mozilla::BaseTimeDuration<ValueCalculator>>; + template <typename> + friend class BaseTimeDuration; + + static BaseTimeDuration FromTicks(int64_t aTicks) { + BaseTimeDuration t; + t.mValue = aTicks; + return t; + } + + static BaseTimeDuration FromTicks(double aTicks) { + // NOTE: this MUST be a >= test, because int64_t(double(INT64_MAX)) + // overflows and gives INT64_MIN. + if (aTicks >= double(INT64_MAX)) { + return FromTicks(INT64_MAX); + } + + // This MUST be a <= test. + if (aTicks <= double(INT64_MIN)) { + return FromTicks(INT64_MIN); + } + + return FromTicks(int64_t(aTicks)); + } + + // Duration, result is implementation-specific difference of two TimeStamps + int64_t mValue; +}; + +/** + * Perform arithmetic operations on the value of a BaseTimeDuration without + * doing strict checks on the range of values. + */ +class TimeDurationValueCalculator { + public: + static int64_t Add(int64_t aA, int64_t aB) { return aA + aB; } + static int64_t Subtract(int64_t aA, int64_t aB) { return aA - aB; } + + template <typename T> + static int64_t Multiply(int64_t aA, T aB) { + static_assert(std::is_integral_v<T>, + "Using integer multiplication routine with non-integer type." + " Further specialization required"); + return aA * static_cast<int64_t>(aB); + } + + static int64_t Divide(int64_t aA, int64_t aB) { return aA / aB; } + static double DivideDouble(int64_t aA, int64_t aB) { + return static_cast<double>(aA) / aB; + } + static int64_t Modulo(int64_t aA, int64_t aB) { return aA % aB; } +}; + +template <> +inline int64_t TimeDurationValueCalculator::Multiply<double>(int64_t aA, + double aB) { + return static_cast<int64_t>(aA * aB); +} + +/** + * Specialization of BaseTimeDuration that uses TimeDurationValueCalculator for + * arithmetic on the mValue member. + * + * Use this class for time durations that are *not* expected to hold values of + * Forever (or the negative equivalent) or when such time duration are *not* + * expected to be used in arithmetic operations. + */ +typedef BaseTimeDuration<TimeDurationValueCalculator> TimeDuration; + +/** + * Instances of this class represent moments in time, or a special + * "null" moment. We do not use the non-monotonic system clock or + * local time, since they can be reset, causing apparent backward + * travel in time, which can confuse algorithms. Instead we measure + * elapsed time according to the system. This time can never go + * backwards (i.e. it never wraps around, at least not in less than + * five million years of system elapsed time). It might not advance + * while the system is sleeping. If TimeStamp::SetNow() is not called + * at all for hours or days, we might not notice the passage of some + * of that time. + * + * We deliberately do not expose a way to convert TimeStamps to some + * particular unit. All you can do is compute a difference between two + * TimeStamps to get a TimeDuration. You can also add a TimeDuration + * to a TimeStamp to get a new TimeStamp. You can't do something + * meaningless like add two TimeStamps. + * + * Internally this is implemented as either a wrapper around + * - high-resolution, monotonic, system clocks if they exist on this + * platform + * - PRIntervalTime otherwise. We detect wraparounds of + * PRIntervalTime and work around them. + * + * This class is similar to C++11's time_point, however it is + * explicitly nullable and provides an IsNull() method. time_point + * is initialized to the clock's epoch and provides a + * time_since_epoch() method that functions similiarly. i.e. + * t.IsNull() is equivalent to t.time_since_epoch() == + * decltype(t)::duration::zero(); + * + * Note that, since TimeStamp objects are small, prefer to pass them by value + * unless there is a specific reason not to do so. + */ +class TimeStamp { + public: + /** + * Initialize to the "null" moment + */ + constexpr TimeStamp() : mValue(0) {} + // Default copy-constructor and assignment are OK + + /** + * The system timestamps are the same as the TimeStamp + * retrieved by mozilla::TimeStamp. Since we need this for + * vsync timestamps, we enable the creation of mozilla::TimeStamps + * on platforms that support vsync aligned refresh drivers / compositors + * Verified true as of Jan 31, 2015: B2G and OS X + * False on Windows 7 + * Android's event time uses CLOCK_MONOTONIC via SystemClock.uptimeMilles. + * So it is same value of TimeStamp posix implementation. + * Wayland/GTK event time also uses CLOCK_MONOTONIC on Weston/Mutter + * compositors. + * UNTESTED ON OTHER PLATFORMS + */ +#if defined(XP_DARWIN) || defined(MOZ_WIDGET_ANDROID) || defined(MOZ_WIDGET_GTK) + static TimeStamp FromSystemTime(int64_t aSystemTime) { + static_assert(sizeof(aSystemTime) == sizeof(TimeStampValue), + "System timestamp should be same units as TimeStampValue"); + return TimeStamp(aSystemTime); + } +#endif + + /** + * Return true if this is the "null" moment + */ + bool IsNull() const { return mValue == 0; } + + /** + * Return true if this is not the "null" moment, may be used in tests, e.g.: + * |if (timestamp) { ... }| + */ + explicit operator bool() const { return mValue != 0; } + + /** + * Return a timestamp reflecting the current elapsed system time. This + * is monotonically increasing (i.e., does not decrease) over the + * lifetime of this process' XPCOM session. + * + * Now() is trying to ensure the best possible precision on each platform, + * at least one millisecond. + * + * NowLoRes() has been introduced to workaround performance problems of + * QueryPerformanceCounter on the Windows platform. NowLoRes() is giving + * lower precision, usually 15.6 ms, but with very good performance benefit. + * Use it for measurements of longer times, like >200ms timeouts. + */ + static TimeStamp Now() { return Now(true); } + static TimeStamp NowLoRes() { return Now(false); } + + /** + * Return a timestamp representing the time when the current process was + * created which will be comparable with other timestamps taken with this + * class. + * + * @returns A timestamp representing the time when the process was created + */ + static MFBT_API TimeStamp ProcessCreation(); + + /** + * Return the very first timestamp that was taken. This can be used instead + * of TimeStamp::ProcessCreation() by code that might not allow running the + * complex logic required to compute the real process creation. This will + * necessarily have been recorded sometimes after TimeStamp::ProcessCreation() + * or at best should be equal to it. + * + * @returns The first tiemstamp that was taken by this process + */ + static MFBT_API TimeStamp FirstTimeStamp(); + + /** + * Records a process restart. After this call ProcessCreation() will return + * the time when the browser was restarted instead of the actual time when + * the process was created. + */ + static MFBT_API void RecordProcessRestart(); + +#ifdef XP_LINUX + uint64_t RawClockMonotonicNanosecondsSinceBoot() { + return static_cast<uint64_t>(mValue); + } +#endif + +#ifdef XP_MACOSX + uint64_t RawMachAbsoluteTimeValue() { return static_cast<uint64_t>(mValue); } +#endif + +#ifdef XP_WIN + Maybe<uint64_t> RawQueryPerformanceCounterValue() { + // mQPC is stored in `mt` i.e. QueryPerformanceCounter * 1000 + // so divide out the 1000 + return mValue.mHasQPC ? Some(mValue.mQPC / 1000ULL) : Nothing(); + } +#endif + + /** + * Compute the difference between two timestamps. Both must be non-null. + */ + TimeDuration operator-(const TimeStamp& aOther) const { + MOZ_ASSERT(!IsNull(), "Cannot compute with a null value"); + MOZ_ASSERT(!aOther.IsNull(), "Cannot compute with aOther null value"); + static_assert(-INT64_MAX > INT64_MIN, "int64_t sanity check"); + int64_t ticks = int64_t(mValue - aOther.mValue); + // Check for overflow. + if (mValue > aOther.mValue) { + if (ticks < 0) { + ticks = INT64_MAX; + } + } else { + if (ticks > 0) { + ticks = INT64_MIN; + } + } + return TimeDuration::FromTicks(ticks); + } + + TimeStamp operator+(const TimeDuration& aOther) const { + TimeStamp result = *this; + result += aOther; + return result; + } + TimeStamp operator-(const TimeDuration& aOther) const { + TimeStamp result = *this; + result -= aOther; + return result; + } + TimeStamp& operator+=(const TimeDuration& aOther) { + MOZ_ASSERT(!IsNull(), "Cannot compute with a null value"); + TimeStampValue value = mValue + aOther.mValue; + // Check for underflow. + // (We don't check for overflow because it's not obvious what the error + // behavior should be in that case.) + if (aOther.mValue < 0 && value > mValue) { + value = 0; + } + mValue = value; + return *this; + } + TimeStamp& operator-=(const TimeDuration& aOther) { + MOZ_ASSERT(!IsNull(), "Cannot compute with a null value"); + TimeStampValue value = mValue - aOther.mValue; + // Check for underflow. + // (We don't check for overflow because it's not obvious what the error + // behavior should be in that case.) + if (aOther.mValue > 0 && value > mValue) { + value = 0; + } + mValue = value; + return *this; + } + + bool operator<(const TimeStamp& aOther) const { + MOZ_ASSERT(!IsNull(), "Cannot compute with a null value"); + MOZ_ASSERT(!aOther.IsNull(), "Cannot compute with aOther null value"); + return mValue < aOther.mValue; + } + bool operator<=(const TimeStamp& aOther) const { + MOZ_ASSERT(!IsNull(), "Cannot compute with a null value"); + MOZ_ASSERT(!aOther.IsNull(), "Cannot compute with aOther null value"); + return mValue <= aOther.mValue; + } + bool operator>=(const TimeStamp& aOther) const { + MOZ_ASSERT(!IsNull(), "Cannot compute with a null value"); + MOZ_ASSERT(!aOther.IsNull(), "Cannot compute with aOther null value"); + return mValue >= aOther.mValue; + } + bool operator>(const TimeStamp& aOther) const { + MOZ_ASSERT(!IsNull(), "Cannot compute with a null value"); + MOZ_ASSERT(!aOther.IsNull(), "Cannot compute with aOther null value"); + return mValue > aOther.mValue; + } + bool operator==(const TimeStamp& aOther) const { + return IsNull() ? aOther.IsNull() + : !aOther.IsNull() && mValue == aOther.mValue; + } + bool operator!=(const TimeStamp& aOther) const { return !(*this == aOther); } + + // Comparing TimeStamps for equality should be discouraged. Adding + // two TimeStamps, or scaling TimeStamps, is nonsense and must never + // be allowed. + + static MFBT_API void Startup(); + static MFBT_API void Shutdown(); + +#if defined(DEBUG) + TimeStampValue GetValue() const { return mValue; } +#endif + + private: + friend struct IPC::ParamTraits<mozilla::TimeStamp>; + friend struct TimeStampInitialization; + + MOZ_IMPLICIT + TimeStamp(TimeStampValue aValue) : mValue(aValue) {} + + static MFBT_API TimeStamp Now(bool aHighResolution); + + /** + * Computes the uptime of the current process in microseconds. The result + * is platform-dependent and needs to be checked against existing timestamps + * for consistency. + * + * @returns The number of microseconds since the calling process was started + * or 0 if an error was encountered while computing the uptime + */ + static MFBT_API uint64_t ComputeProcessUptime(); + + /** + * When built with PRIntervalTime, a value of 0 means this instance + * is "null". Otherwise, the low 32 bits represent a PRIntervalTime, + * and the high 32 bits represent a counter of the number of + * rollovers of PRIntervalTime that we've seen. This counter starts + * at 1 to avoid a real time colliding with the "null" value. + * + * PR_INTERVAL_MAX is set at 100,000 ticks per second. So the minimum + * time to wrap around is about 2^64/100000 seconds, i.e. about + * 5,849,424 years. + * + * When using a system clock, a value is system dependent. + */ + TimeStampValue mValue; +}; + +} // namespace mozilla + +#endif /* mozilla_TimeStamp_h */ diff --git a/mozglue/misc/TimeStamp_darwin.cpp b/mozglue/misc/TimeStamp_darwin.cpp new file mode 100644 index 0000000000..896ac8ae92 --- /dev/null +++ b/mozglue/misc/TimeStamp_darwin.cpp @@ -0,0 +1,187 @@ +/* -*- 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/. */ + +// +// Implement TimeStamp::Now() with mach_absolute_time +// +// The "tick" unit for mach_absolute_time is defined using mach_timebase_info() +// which gives a conversion ratio to nanoseconds. For more information see +// Apple's QA1398. +// +// This code is inspired by Chromium's time_mac.cc. The biggest +// differences are that we explicitly initialize using +// TimeStamp::Initialize() instead of lazily in Now() and that +// we store the time value in ticks and convert when needed instead +// of storing the time value in nanoseconds. + +#include <mach/mach_time.h> +#include <sys/time.h> +#include <sys/sysctl.h> +#include <time.h> +#include <unistd.h> + +#include "mozilla/TimeStamp.h" +#include "mozilla/Uptime.h" + +// Estimate of the smallest duration of time we can measure. +static uint64_t sResolution; +static uint64_t sResolutionSigDigs; + +static const uint64_t kNsPerMs = 1000000; +static const uint64_t kUsPerSec = 1000000; +static const double kNsPerMsd = 1000000.0; +static const double kNsPerSecd = 1000000000.0; + +static bool gInitialized = false; +static double sNsPerTick; + +static uint64_t ClockTime() { + // mach_absolute_time is it when it comes to ticks on the Mac. Other calls + // with less precision (such as TickCount) just call through to + // mach_absolute_time. + // + // At the time of writing mach_absolute_time returns the number of nanoseconds + // since boot. This won't overflow 64bits for 500+ years so we aren't going + // to worry about that possiblity + return mach_absolute_time(); +} + +static uint64_t ClockResolutionNs() { + uint64_t start = ClockTime(); + uint64_t end = ClockTime(); + uint64_t minres = (end - start); + + // 10 total trials is arbitrary: what we're trying to avoid by + // looping is getting unlucky and being interrupted by a context + // switch or signal, or being bitten by paging/cache effects + for (int i = 0; i < 9; ++i) { + start = ClockTime(); + end = ClockTime(); + + uint64_t candidate = (start - end); + if (candidate < minres) { + minres = candidate; + } + } + + if (0 == minres) { + // measurable resolution is either incredibly low, ~1ns, or very + // high. fall back on NSPR's resolution assumption + minres = 1 * kNsPerMs; + } + + return minres; +} + +namespace mozilla { + +double BaseTimeDurationPlatformUtils::ToSeconds(int64_t aTicks) { + MOZ_ASSERT(gInitialized, "calling TimeDuration too early"); + return (aTicks * sNsPerTick) / kNsPerSecd; +} + +double BaseTimeDurationPlatformUtils::ToSecondsSigDigits(int64_t aTicks) { + MOZ_ASSERT(gInitialized, "calling TimeDuration too early"); + // don't report a value < mResolution ... + int64_t valueSigDigs = sResolution * (aTicks / sResolution); + // and chop off insignificant digits + valueSigDigs = sResolutionSigDigs * (valueSigDigs / sResolutionSigDigs); + return (valueSigDigs * sNsPerTick) / kNsPerSecd; +} + +int64_t BaseTimeDurationPlatformUtils::TicksFromMilliseconds( + double aMilliseconds) { + MOZ_ASSERT(gInitialized, "calling TimeDuration too early"); + double result = (aMilliseconds * kNsPerMsd) / sNsPerTick; + if (result > double(INT64_MAX)) { + return INT64_MAX; + } else if (result < double(INT64_MIN)) { + return INT64_MIN; + } + + return result; +} + +int64_t BaseTimeDurationPlatformUtils::ResolutionInTicks() { + MOZ_ASSERT(gInitialized, "calling TimeDuration too early"); + return static_cast<int64_t>(sResolution); +} + +void TimeStamp::Startup() { + if (gInitialized) { + return; + } + + mach_timebase_info_data_t timebaseInfo; + // Apple's QA1398 suggests that the output from mach_timebase_info + // will not change while a program is running, so it should be safe + // to cache the result. + kern_return_t kr = mach_timebase_info(&timebaseInfo); + if (kr != KERN_SUCCESS) { + MOZ_RELEASE_ASSERT(false, "mach_timebase_info failed"); + } + + sNsPerTick = double(timebaseInfo.numer) / timebaseInfo.denom; + + sResolution = ClockResolutionNs(); + + // find the number of significant digits in sResolution, for the + // sake of ToSecondsSigDigits() + for (sResolutionSigDigs = 1; !(sResolutionSigDigs == sResolution || + 10 * sResolutionSigDigs > sResolution); + sResolutionSigDigs *= 10) + ; + + gInitialized = true; + + return; +} + +void TimeStamp::Shutdown() {} + +TimeStamp TimeStamp::Now(bool aHighResolution) { + return TimeStamp(ClockTime()); +} + +// Computes and returns the process uptime in microseconds. +// Returns 0 if an error was encountered. +uint64_t TimeStamp::ComputeProcessUptime() { + struct timeval tv; + int rv = gettimeofday(&tv, nullptr); + + if (rv == -1) { + return 0; + } + + int mib[] = { + CTL_KERN, + KERN_PROC, + KERN_PROC_PID, + getpid(), + }; + u_int mibLen = sizeof(mib) / sizeof(mib[0]); + + struct kinfo_proc proc; + size_t bufferSize = sizeof(proc); + rv = sysctl(mib, mibLen, &proc, &bufferSize, nullptr, 0); + + if (rv == -1) { + return 0; + } + + uint64_t startTime = + ((uint64_t)proc.kp_proc.p_un.__p_starttime.tv_sec * kUsPerSec) + + proc.kp_proc.p_un.__p_starttime.tv_usec; + uint64_t now = (tv.tv_sec * kUsPerSec) + tv.tv_usec; + + if (startTime > now) { + return 0; + } + + return now - startTime; +} + +} // namespace mozilla diff --git a/mozglue/misc/TimeStamp_posix.cpp b/mozglue/misc/TimeStamp_posix.cpp new file mode 100644 index 0000000000..10c046d046 --- /dev/null +++ b/mozglue/misc/TimeStamp_posix.cpp @@ -0,0 +1,337 @@ +/* -*- 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/. */ + +// +// Implement TimeStamp::Now() with POSIX clocks. +// +// The "tick" unit for POSIX clocks is simply a nanosecond, as this is +// the smallest unit of time representable by struct timespec. That +// doesn't mean that a nanosecond is the resolution of TimeDurations +// obtained with this API; see TimeDuration::Resolution; +// + +#include <sys/syscall.h> +#include <time.h> +#include <unistd.h> +#include <string.h> + +#if defined(__DragonFly__) || defined(__FreeBSD__) || defined(__NetBSD__) || \ + defined(__OpenBSD__) +# include <sys/param.h> +# include <sys/sysctl.h> +#endif + +#if defined(__DragonFly__) || defined(__FreeBSD__) +# include <sys/user.h> +#endif + +#if defined(__NetBSD__) +# undef KERN_PROC +# define KERN_PROC KERN_PROC2 +# define KINFO_PROC struct kinfo_proc2 +#else +# define KINFO_PROC struct kinfo_proc +#endif + +#if defined(__DragonFly__) +# define KP_START_SEC kp_start.tv_sec +# define KP_START_USEC kp_start.tv_usec +#elif defined(__FreeBSD__) +# define KP_START_SEC ki_start.tv_sec +# define KP_START_USEC ki_start.tv_usec +#else +# define KP_START_SEC p_ustart_sec +# define KP_START_USEC p_ustart_usec +#endif + +#include "mozilla/Sprintf.h" +#include "mozilla/TimeStamp.h" + +#if !defined(__wasi__) +# include <pthread.h> +#endif + +// Estimate of the smallest duration of time we can measure. +static uint64_t sResolution; +static uint64_t sResolutionSigDigs; + +#if !defined(__wasi__) +static const uint16_t kNsPerUs = 1000; +#endif + +static const uint64_t kNsPerMs = 1000000; +static const uint64_t kNsPerSec = 1000000000; +static const double kNsPerMsd = 1000000.0; +static const double kNsPerSecd = 1000000000.0; + +static uint64_t TimespecToNs(const struct timespec& aTs) { + uint64_t baseNs = uint64_t(aTs.tv_sec) * kNsPerSec; + return baseNs + uint64_t(aTs.tv_nsec); +} + +static uint64_t ClockTimeNs() { + struct timespec ts; + // this can't fail: we know &ts is valid, and TimeStamp::Startup() + // checks that CLOCK_MONOTONIC is supported (and aborts if not) + clock_gettime(CLOCK_MONOTONIC, &ts); + + // tv_sec is defined to be relative to an arbitrary point in time, + // but it would be madness for that point in time to be earlier than + // the Epoch. So we can safely assume that even if time_t is 32 + // bits, tv_sec won't overflow while the browser is open. Revisit + // this argument if we're still building with 32-bit time_t around + // the year 2037. + return TimespecToNs(ts); +} + +static uint64_t ClockResolutionNs() { + // NB: why not rely on clock_getres()? Two reasons: (i) it might + // lie, and (ii) it might return an "ideal" resolution that while + // theoretically true, could never be measured in practice. Since + // clock_gettime() likely involves a system call on your platform, + // the "actual" timing resolution shouldn't be lower than syscall + // overhead. + + uint64_t start = ClockTimeNs(); + uint64_t end = ClockTimeNs(); + uint64_t minres = (end - start); + + // 10 total trials is arbitrary: what we're trying to avoid by + // looping is getting unlucky and being interrupted by a context + // switch or signal, or being bitten by paging/cache effects + for (int i = 0; i < 9; ++i) { + start = ClockTimeNs(); + end = ClockTimeNs(); + + uint64_t candidate = (start - end); + if (candidate < minres) { + minres = candidate; + } + } + + if (0 == minres) { + // measurable resolution is either incredibly low, ~1ns, or very + // high. fall back on clock_getres() + struct timespec ts; + if (0 == clock_getres(CLOCK_MONOTONIC, &ts)) { + minres = TimespecToNs(ts); + } + } + + if (0 == minres) { + // clock_getres probably failed. fall back on NSPR's resolution + // assumption + minres = 1 * kNsPerMs; + } + + return minres; +} + +namespace mozilla { + +double BaseTimeDurationPlatformUtils::ToSeconds(int64_t aTicks) { + return double(aTicks) / kNsPerSecd; +} + +double BaseTimeDurationPlatformUtils::ToSecondsSigDigits(int64_t aTicks) { + // don't report a value < mResolution ... + int64_t valueSigDigs = sResolution * (aTicks / sResolution); + // and chop off insignificant digits + valueSigDigs = sResolutionSigDigs * (valueSigDigs / sResolutionSigDigs); + return double(valueSigDigs) / kNsPerSecd; +} + +int64_t BaseTimeDurationPlatformUtils::TicksFromMilliseconds( + double aMilliseconds) { + double result = aMilliseconds * kNsPerMsd; + if (result > double(INT64_MAX)) { + return INT64_MAX; + } + if (result < INT64_MIN) { + return INT64_MIN; + } + + return result; +} + +int64_t BaseTimeDurationPlatformUtils::ResolutionInTicks() { + return static_cast<int64_t>(sResolution); +} + +static bool gInitialized = false; + +void TimeStamp::Startup() { + if (gInitialized) { + return; + } + + struct timespec dummy; + if (clock_gettime(CLOCK_MONOTONIC, &dummy) != 0) { + MOZ_CRASH("CLOCK_MONOTONIC is absent!"); + } + + sResolution = ClockResolutionNs(); + + // find the number of significant digits in sResolution, for the + // sake of ToSecondsSigDigits() + for (sResolutionSigDigs = 1; !(sResolutionSigDigs == sResolution || + 10 * sResolutionSigDigs > sResolution); + sResolutionSigDigs *= 10) + ; + + gInitialized = true; +} + +void TimeStamp::Shutdown() {} + +TimeStamp TimeStamp::Now(bool aHighResolution) { + return TimeStamp(ClockTimeNs()); +} + +#if defined(XP_LINUX) || defined(ANDROID) + +// Calculates the amount of jiffies that have elapsed since boot and up to the +// starttime value of a specific process as found in its /proc/*/stat file. +// Returns 0 if an error occurred. + +static uint64_t JiffiesSinceBoot(const char* aFile) { + char stat[512]; + + FILE* f = fopen(aFile, "r"); + if (!f) { + return 0; + } + + int n = fread(&stat, 1, sizeof(stat) - 1, f); + + fclose(f); + + if (n <= 0) { + return 0; + } + + stat[n] = 0; + + long long unsigned startTime = 0; // instead of uint64_t to keep GCC quiet + char* s = strrchr(stat, ')'); + + if (!s) { + return 0; + } + + int rv = sscanf(s + 2, + "%*c %*d %*d %*d %*d %*d %*u %*u %*u %*u " + "%*u %*u %*u %*d %*d %*d %*d %*d %*d %llu", + &startTime); + + if (rv != 1 || !startTime) { + return 0; + } + + return startTime; +} + +// Computes the interval that has elapsed between the thread creation and the +// process creation by comparing the starttime fields in the respective +// /proc/*/stat files. The resulting value will be a good approximation of the +// process uptime. This value will be stored at the address pointed by aTime; +// if an error occurred 0 will be stored instead. + +static void* ComputeProcessUptimeThread(void* aTime) { + uint64_t* uptime = static_cast<uint64_t*>(aTime); + long hz = sysconf(_SC_CLK_TCK); + + *uptime = 0; + + if (!hz) { + return nullptr; + } + + char threadStat[40]; + SprintfLiteral(threadStat, "/proc/self/task/%d/stat", + (pid_t)syscall(__NR_gettid)); + + uint64_t threadJiffies = JiffiesSinceBoot(threadStat); + uint64_t selfJiffies = JiffiesSinceBoot("/proc/self/stat"); + + if (!threadJiffies || !selfJiffies) { + return nullptr; + } + + *uptime = ((threadJiffies - selfJiffies) * kNsPerSec) / hz; + return nullptr; +} + +// Computes and returns the process uptime in us on Linux & its derivatives. +// Returns 0 if an error was encountered. + +uint64_t TimeStamp::ComputeProcessUptime() { + uint64_t uptime = 0; + pthread_t uptime_pthread; + + if (pthread_create(&uptime_pthread, nullptr, ComputeProcessUptimeThread, + &uptime)) { + MOZ_CRASH("Failed to create process uptime thread."); + return 0; + } + + pthread_join(uptime_pthread, NULL); + + return uptime / kNsPerUs; +} + +#elif defined(__DragonFly__) || defined(__FreeBSD__) || defined(__NetBSD__) || \ + defined(__OpenBSD__) + +// Computes and returns the process uptime in us on various BSD flavors. +// Returns 0 if an error was encountered. + +uint64_t TimeStamp::ComputeProcessUptime() { + struct timespec ts; + int rv = clock_gettime(CLOCK_REALTIME, &ts); + + if (rv == -1) { + return 0; + } + + int mib[] = { + CTL_KERN, + KERN_PROC, + KERN_PROC_PID, + getpid(), +# if defined(__NetBSD__) || defined(__OpenBSD__) + sizeof(KINFO_PROC), + 1, +# endif + }; + u_int mibLen = sizeof(mib) / sizeof(mib[0]); + + KINFO_PROC proc; + size_t bufferSize = sizeof(proc); + rv = sysctl(mib, mibLen, &proc, &bufferSize, nullptr, 0); + + if (rv == -1) { + return 0; + } + + uint64_t startTime = ((uint64_t)proc.KP_START_SEC * kNsPerSec) + + (proc.KP_START_USEC * kNsPerUs); + uint64_t now = ((uint64_t)ts.tv_sec * kNsPerSec) + ts.tv_nsec; + + if (startTime > now) { + return 0; + } + + return (now - startTime) / kNsPerUs; +} + +#else + +uint64_t TimeStamp::ComputeProcessUptime() { return 0; } + +#endif + +} // namespace mozilla diff --git a/mozglue/misc/TimeStamp_windows.cpp b/mozglue/misc/TimeStamp_windows.cpp new file mode 100644 index 0000000000..89ebb48b79 --- /dev/null +++ b/mozglue/misc/TimeStamp_windows.cpp @@ -0,0 +1,527 @@ +/* -*- 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/. */ + +// Implement TimeStamp::Now() with QueryPerformanceCounter() controlled with +// values of GetTickCount64(). + +#include "mozilla/DynamicallyLinkedFunctionPtr.h" +#include "mozilla/MathAlgorithms.h" +#include "mozilla/TimeStamp.h" +#include "mozilla/Uptime.h" + +#include <stdio.h> +#include <stdlib.h> +#include <intrin.h> +#include <windows.h> + +// To enable logging define to your favorite logging API +#define LOG(x) + +class AutoCriticalSection { + public: + explicit AutoCriticalSection(LPCRITICAL_SECTION aSection) + : mSection(aSection) { + ::EnterCriticalSection(mSection); + } + ~AutoCriticalSection() { ::LeaveCriticalSection(mSection); } + + private: + LPCRITICAL_SECTION mSection; +}; + +// Estimate of the smallest duration of time we can measure. +static volatile ULONGLONG sResolution; +static volatile ULONGLONG sResolutionSigDigs; +static const double kNsPerSecd = 1000000000.0; +static const LONGLONG kNsPerMillisec = 1000000; + +// ---------------------------------------------------------------------------- +// Global constants +// ---------------------------------------------------------------------------- + +// Tolerance to failures settings. +// +// What is the interval we want to have failure free. +// in [ms] +static const uint32_t kFailureFreeInterval = 5000; +// How many failures we are willing to tolerate in the interval. +static const uint32_t kMaxFailuresPerInterval = 4; +// What is the threshold to treat fluctuations as actual failures. +// in [ms] +static const uint32_t kFailureThreshold = 50; + +// If we are not able to get the value of GTC time increment, use this value +// which is the most usual increment. +static const DWORD kDefaultTimeIncrement = 156001; + +// ---------------------------------------------------------------------------- +// Global variables, not changing at runtime +// ---------------------------------------------------------------------------- + +// Result of QueryPerformanceFrequency +// We use default of 1 for the case we can't use QueryPerformanceCounter +// to make mt/ms conversions work despite that. +static uint64_t sFrequencyPerSec = 1; + +namespace mozilla { + +MFBT_API uint64_t GetQueryPerformanceFrequencyPerSec() { + return sFrequencyPerSec; +} + +} // namespace mozilla + +// How much we are tolerant to GTC occasional loose of resoltion. +// This number says how many multiples of the minimal GTC resolution +// detected on the system are acceptable. This number is empirical. +static const LONGLONG kGTCTickLeapTolerance = 4; + +// Base tolerance (more: "inability of detection" range) threshold is calculated +// dynamically, and kept in sGTCResolutionThreshold. +// +// Schematically, QPC worked "100%" correctly if ((GTC_now - GTC_epoch) - +// (QPC_now - QPC_epoch)) was in [-sGTCResolutionThreshold, +// sGTCResolutionThreshold] interval every time we'd compared two time stamps. +// If not, then we check the overflow behind this basic threshold +// is in kFailureThreshold. If not, we condider it as a QPC failure. If too +// many failures in short time are detected, QPC is considered faulty and +// disabled. +// +// Kept in [mt] +static LONGLONG sGTCResolutionThreshold; + +// If QPC is found faulty for two stamps in this interval, we engage +// the fault detection algorithm. For duration larger then this limit +// we bypass using durations calculated from QPC when jitter is detected, +// but don't touch the sUseQPC flag. +// +// Value is in [ms]. +static const uint32_t kHardFailureLimit = 2000; +// Conversion to [mt] +static LONGLONG sHardFailureLimit; + +// Conversion of kFailureFreeInterval and kFailureThreshold to [mt] +static LONGLONG sFailureFreeInterval; +static LONGLONG sFailureThreshold; + +// ---------------------------------------------------------------------------- +// Systemm status flags +// ---------------------------------------------------------------------------- + +// Flag for stable TSC that indicates platform where QPC is stable. +static bool sHasStableTSC = false; + +// ---------------------------------------------------------------------------- +// Global state variables, changing at runtime +// ---------------------------------------------------------------------------- + +// Initially true, set to false when QPC is found unstable and never +// returns back to true since that time. +static bool volatile sUseQPC = true; + +// ---------------------------------------------------------------------------- +// Global lock +// ---------------------------------------------------------------------------- + +// Thread spin count before entering the full wait state for sTimeStampLock. +// Inspired by Rob Arnold's work on PRMJ_Now(). +static const DWORD kLockSpinCount = 4096; + +// Common mutex (thanks the relative complexity of the logic, this is better +// then using CMPXCHG8B.) +// It is protecting the globals bellow. +static CRITICAL_SECTION sTimeStampLock; + +// ---------------------------------------------------------------------------- +// Global lock protected variables +// ---------------------------------------------------------------------------- + +// Timestamp in future until QPC must behave correctly. +// Set to now + kFailureFreeInterval on first QPC failure detection. +// Set to now + E * kFailureFreeInterval on following errors, +// where E is number of errors detected during last kFailureFreeInterval +// milliseconds, calculated simply as: +// E = (sFaultIntoleranceCheckpoint - now) / kFailureFreeInterval + 1. +// When E > kMaxFailuresPerInterval -> disable QPC. +// +// Kept in [mt] +static ULONGLONG sFaultIntoleranceCheckpoint = 0; + +namespace mozilla { + +// Result is in [mt] +static inline ULONGLONG PerformanceCounter() { + LARGE_INTEGER pc; + ::QueryPerformanceCounter(&pc); + + // QueryPerformanceCounter may slightly jitter (not be 100% monotonic.) + // This is a simple go-backward protection for such a faulty hardware. + AutoCriticalSection lock(&sTimeStampLock); + + static decltype(LARGE_INTEGER::QuadPart) last; + if (last > pc.QuadPart) { + return last * 1000ULL; + } + last = pc.QuadPart; + return pc.QuadPart * 1000ULL; +} + +static void InitThresholds() { + DWORD timeAdjustment = 0, timeIncrement = 0; + BOOL timeAdjustmentDisabled; + GetSystemTimeAdjustment(&timeAdjustment, &timeIncrement, + &timeAdjustmentDisabled); + + LOG(("TimeStamp: timeIncrement=%d [100ns]", timeIncrement)); + + if (!timeIncrement) { + timeIncrement = kDefaultTimeIncrement; + } + + // Ceiling to a millisecond + // Example values: 156001, 210000 + DWORD timeIncrementCeil = timeIncrement; + // Don't want to round up if already rounded, values will be: 156000, 209999 + timeIncrementCeil -= 1; + // Convert to ms, values will be: 15, 20 + timeIncrementCeil /= 10000; + // Round up, values will be: 16, 21 + timeIncrementCeil += 1; + // Convert back to 100ns, values will be: 160000, 210000 + timeIncrementCeil *= 10000; + + // How many milli-ticks has the interval rounded up + LONGLONG ticksPerGetTickCountResolutionCeiling = + (int64_t(timeIncrementCeil) * sFrequencyPerSec) / 10000LL; + + // GTC may jump by 32 (2*16) ms in two steps, therefor use the ceiling value. + sGTCResolutionThreshold = + LONGLONG(kGTCTickLeapTolerance * ticksPerGetTickCountResolutionCeiling); + + sHardFailureLimit = ms2mt(kHardFailureLimit); + sFailureFreeInterval = ms2mt(kFailureFreeInterval); + sFailureThreshold = ms2mt(kFailureThreshold); +} + +static void InitResolution() { + // 10 total trials is arbitrary: what we're trying to avoid by + // looping is getting unlucky and being interrupted by a context + // switch or signal, or being bitten by paging/cache effects + + ULONGLONG minres = ~0ULL; + if (sUseQPC) { + int loops = 10; + do { + ULONGLONG start = PerformanceCounter(); + ULONGLONG end = PerformanceCounter(); + + ULONGLONG candidate = (end - start); + if (candidate < minres) { + minres = candidate; + } + } while (--loops && minres); + + if (0 == minres) { + minres = 1; + } + } else { + // GetTickCount has only ~16ms known resolution + minres = ms2mt(16); + } + + // Converting minres that is in [mt] to nanosecods, multiplicating + // the argument to preserve resolution. + ULONGLONG result = mt2ms(minres * kNsPerMillisec); + if (0 == result) { + result = 1; + } + + sResolution = result; + + // find the number of significant digits in mResolution, for the + // sake of ToSecondsSigDigits() + ULONGLONG sigDigs; + for (sigDigs = 1; !(sigDigs == result || 10 * sigDigs > result); + sigDigs *= 10) + ; + + sResolutionSigDigs = sigDigs; +} + +// ---------------------------------------------------------------------------- +// TimeStampValue implementation +// ---------------------------------------------------------------------------- +MFBT_API +TimeStampValue::TimeStampValue(ULONGLONG aGTC, ULONGLONG aQPC, bool aHasQPC) + : mGTC(aGTC), mQPC(aQPC), mHasQPC(aHasQPC) { + mIsNull = aGTC == 0 && aQPC == 0; +} + +MFBT_API TimeStampValue& TimeStampValue::operator+=(const int64_t aOther) { + mGTC += aOther; + mQPC += aOther; + return *this; +} + +MFBT_API TimeStampValue& TimeStampValue::operator-=(const int64_t aOther) { + mGTC -= aOther; + mQPC -= aOther; + return *this; +} + +// If the duration is less then two seconds, perform check of QPC stability +// by comparing both GTC and QPC calculated durations of this and aOther. +MFBT_API uint64_t TimeStampValue::CheckQPC(const TimeStampValue& aOther) const { + uint64_t deltaGTC = mGTC - aOther.mGTC; + + if (!mHasQPC || !aOther.mHasQPC) { // Both not holding QPC + return deltaGTC; + } + + uint64_t deltaQPC = mQPC - aOther.mQPC; + + if (sHasStableTSC) { // For stable TSC there is no need to check + return deltaQPC; + } + + // Check QPC is sane before using it. + int64_t diff = DeprecatedAbs(int64_t(deltaQPC) - int64_t(deltaGTC)); + if (diff <= sGTCResolutionThreshold) { + return deltaQPC; + } + + // Treat absolutely for calibration purposes + int64_t duration = DeprecatedAbs(int64_t(deltaGTC)); + int64_t overflow = diff - sGTCResolutionThreshold; + + LOG(("TimeStamp: QPC check after %llums with overflow %1.4fms", + mt2ms(duration), mt2ms_f(overflow))); + + if (overflow <= sFailureThreshold) { // We are in the limit, let go. + return deltaQPC; + } + + // QPC deviates, don't use it, since now this method may only return deltaGTC. + + if (!sUseQPC) { // QPC already disabled, no need to run the fault tolerance + // algorithm. + return deltaGTC; + } + + LOG(("TimeStamp: QPC jittered over failure threshold")); + + if (duration < sHardFailureLimit) { + // Interval between the two time stamps is very short, consider + // QPC as unstable and record a failure. + uint64_t now = ms2mt(GetTickCount64()); + + AutoCriticalSection lock(&sTimeStampLock); + + if (sFaultIntoleranceCheckpoint && sFaultIntoleranceCheckpoint > now) { + // There's already been an error in the last fault intollerant interval. + // Time since now to the checkpoint actually holds information on how many + // failures there were in the failure free interval we have defined. + uint64_t failureCount = + (sFaultIntoleranceCheckpoint - now + sFailureFreeInterval - 1) / + sFailureFreeInterval; + if (failureCount > kMaxFailuresPerInterval) { + sUseQPC = false; + LOG(("TimeStamp: QPC disabled")); + } else { + // Move the fault intolerance checkpoint more to the future, prolong it + // to reflect the number of detected failures. + ++failureCount; + sFaultIntoleranceCheckpoint = now + failureCount * sFailureFreeInterval; + LOG(("TimeStamp: recording %dth QPC failure", failureCount)); + } + } else { + // Setup fault intolerance checkpoint in the future for first detected + // error. + sFaultIntoleranceCheckpoint = now + sFailureFreeInterval; + LOG(("TimeStamp: recording 1st QPC failure")); + } + } + + return deltaGTC; +} + +MFBT_API uint64_t +TimeStampValue::operator-(const TimeStampValue& aOther) const { + if (IsNull() && aOther.IsNull()) { + return uint64_t(0); + } + + return CheckQPC(aOther); +} + +// ---------------------------------------------------------------------------- +// TimeDuration and TimeStamp implementation +// ---------------------------------------------------------------------------- + +MFBT_API double BaseTimeDurationPlatformUtils::ToSeconds(int64_t aTicks) { + // Converting before arithmetic avoids blocked store forward + return double(aTicks) / (double(sFrequencyPerSec) * 1000.0); +} + +MFBT_API double BaseTimeDurationPlatformUtils::ToSecondsSigDigits( + int64_t aTicks) { + // don't report a value < mResolution ... + LONGLONG resolution = sResolution; + LONGLONG resolutionSigDigs = sResolutionSigDigs; + LONGLONG valueSigDigs = resolution * (aTicks / resolution); + // and chop off insignificant digits + valueSigDigs = resolutionSigDigs * (valueSigDigs / resolutionSigDigs); + return double(valueSigDigs) / kNsPerSecd; +} + +MFBT_API int64_t +BaseTimeDurationPlatformUtils::TicksFromMilliseconds(double aMilliseconds) { + double result = ms2mt(aMilliseconds); + if (result > double(INT64_MAX)) { + return INT64_MAX; + } else if (result < double(INT64_MIN)) { + return INT64_MIN; + } + + return result; +} + +MFBT_API int64_t BaseTimeDurationPlatformUtils::ResolutionInTicks() { + return static_cast<int64_t>(sResolution); +} + +static bool HasStableTSC() { +#if defined(_M_ARM64) + // AArch64 defines that its system counter run at a constant rate + // regardless of the current clock frequency of the system. See "The + // Generic Timer", section D7, in the ARMARM for ARMv8. + return true; +#else + union { + int regs[4]; + struct { + int nIds; + char cpuString[12]; + }; + } cpuInfo; + + __cpuid(cpuInfo.regs, 0); + // Only allow Intel or AMD CPUs for now. + // The order of the registers is reg[1], reg[3], reg[2]. We just adjust the + // string so that we can compare in one go. + if (_strnicmp(cpuInfo.cpuString, "GenuntelineI", sizeof(cpuInfo.cpuString)) && + _strnicmp(cpuInfo.cpuString, "AuthcAMDenti", sizeof(cpuInfo.cpuString))) { + return false; + } + + int regs[4]; + + // detect if the Advanced Power Management feature is supported + __cpuid(regs, 0x80000000); + if ((unsigned int)regs[0] < 0x80000007) { + // XXX should we return true here? If there is no APM there may be + // no way how TSC can run out of sync among cores. + return false; + } + + __cpuid(regs, 0x80000007); + // if bit 8 is set than TSC will run at a constant rate + // in all ACPI P-states, C-states and T-states + return regs[3] & (1 << 8); +#endif +} + +static bool gInitialized = false; + +MFBT_API void TimeStamp::Startup() { + if (gInitialized) { + return; + } + + gInitialized = true; + + // Decide which implementation to use for the high-performance timer. + + InitializeCriticalSectionAndSpinCount(&sTimeStampLock, kLockSpinCount); + + bool forceGTC = false; + bool forceQPC = false; + + char* modevar = getenv("MOZ_TIMESTAMP_MODE"); + if (modevar) { + if (!strcmp(modevar, "QPC")) { + forceQPC = true; + } else if (!strcmp(modevar, "GTC")) { + forceGTC = true; + } + } + + LARGE_INTEGER freq; + sUseQPC = !forceGTC && ::QueryPerformanceFrequency(&freq); + if (!sUseQPC) { + // No Performance Counter. Fall back to use GetTickCount64. + InitResolution(); + + LOG(("TimeStamp: using GetTickCount64")); + return; + } + + sHasStableTSC = forceQPC || HasStableTSC(); + LOG(("TimeStamp: HasStableTSC=%d", sHasStableTSC)); + + sFrequencyPerSec = freq.QuadPart; + LOG(("TimeStamp: QPC frequency=%llu", sFrequencyPerSec)); + + InitThresholds(); + InitResolution(); + + return; +} + +MFBT_API void TimeStamp::Shutdown() { DeleteCriticalSection(&sTimeStampLock); } + +TimeStampValue NowInternal(bool aHighResolution) { + // sUseQPC is volatile + bool useQPC = (aHighResolution && sUseQPC); + + // Both values are in [mt] units. + ULONGLONG QPC = useQPC ? PerformanceCounter() : uint64_t(0); + ULONGLONG GTC = ms2mt(GetTickCount64()); + return TimeStampValue(GTC, QPC, useQPC); +} + +MFBT_API TimeStamp TimeStamp::Now(bool aHighResolution) { + return TimeStamp(NowInternal(aHighResolution)); +} + +// Computes and returns the process uptime in microseconds. +// Returns 0 if an error was encountered. + +MFBT_API uint64_t TimeStamp::ComputeProcessUptime() { + FILETIME start, foo, bar, baz; + bool success = GetProcessTimes(GetCurrentProcess(), &start, &foo, &bar, &baz); + if (!success) { + return 0; + } + + static const StaticDynamicallyLinkedFunctionPtr<void(WINAPI*)(LPFILETIME)> + pGetSystemTimePreciseAsFileTime(L"kernel32.dll", + "GetSystemTimePreciseAsFileTime"); + + FILETIME now; + if (pGetSystemTimePreciseAsFileTime) { + pGetSystemTimePreciseAsFileTime(&now); + } else { + GetSystemTimeAsFileTime(&now); + } + + ULARGE_INTEGER startUsec = {{start.dwLowDateTime, start.dwHighDateTime}}; + ULARGE_INTEGER nowUsec = {{now.dwLowDateTime, now.dwHighDateTime}}; + + return (nowUsec.QuadPart - startUsec.QuadPart) / 10ULL; +} + +} // namespace mozilla diff --git a/mozglue/misc/TimeStamp_windows.h b/mozglue/misc/TimeStamp_windows.h new file mode 100644 index 0000000000..9deabe52de --- /dev/null +++ b/mozglue/misc/TimeStamp_windows.h @@ -0,0 +1,100 @@ +/* -*- 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/. */ + +#ifndef mozilla_TimeStamp_windows_h +#define mozilla_TimeStamp_windows_h + +#include "mozilla/Types.h" + +namespace mozilla { + +/** + * The [mt] unit: + * + * Many values are kept in ticks of the Performance Counter x 1000, + * further just referred as [mt], meaning milli-ticks. + * + * This is needed to preserve maximum precision of the performance frequency + * representation. GetTickCount64 values in milliseconds are multiplied with + * frequency per second. Therefore we need to multiply QPC value by 1000 to + * have the same units to allow simple arithmentic with both QPC and GTC. + */ +#define ms2mt(x) ((x)*mozilla::GetQueryPerformanceFrequencyPerSec()) +#define mt2ms(x) ((x) / mozilla::GetQueryPerformanceFrequencyPerSec()) +#define mt2ms_f(x) (double(x) / mozilla::GetQueryPerformanceFrequencyPerSec()) + +MFBT_API uint64_t GetQueryPerformanceFrequencyPerSec(); + +class TimeStamp; +class TimeStampValue; + +TimeStampValue NowInternal(bool aHighResolution); + +class TimeStampValue { + friend TimeStampValue NowInternal(bool); + friend bool IsCanonicalTimeStamp(TimeStampValue); + friend struct IPC::ParamTraits<mozilla::TimeStampValue>; + friend class TimeStamp; + + // Both QPC and GTC are kept in [mt] units. + uint64_t mGTC; + uint64_t mQPC; + + bool mIsNull; + bool mHasQPC; + + MFBT_API TimeStampValue(uint64_t aGTC, uint64_t aQPC, bool aHasQPC); + + MFBT_API uint64_t CheckQPC(const TimeStampValue& aOther) const; + + // This struct is used to allow doing TimeStampValue a = 0 and similar + struct _SomethingVeryRandomHere; + constexpr MOZ_IMPLICIT TimeStampValue(_SomethingVeryRandomHere* aNullValue) + : mGTC(0), mQPC(0), mIsNull(true), mHasQPC(false) {} + + public: + MFBT_API uint64_t operator-(const TimeStampValue& aOther) const; + + TimeStampValue operator+(const int64_t aOther) const { + return TimeStampValue(mGTC + aOther, mQPC + aOther, mHasQPC); + } + TimeStampValue operator-(const int64_t aOther) const { + return TimeStampValue(mGTC - aOther, mQPC - aOther, mHasQPC); + } + MFBT_API TimeStampValue& operator+=(const int64_t aOther); + MFBT_API TimeStampValue& operator-=(const int64_t aOther); + + bool operator<(const TimeStampValue& aOther) const { + return int64_t(*this - aOther) < 0; + } + bool operator>(const TimeStampValue& aOther) const { + return int64_t(*this - aOther) > 0; + } + bool operator<=(const TimeStampValue& aOther) const { + return int64_t(*this - aOther) <= 0; + } + bool operator>=(const TimeStampValue& aOther) const { + return int64_t(*this - aOther) >= 0; + } + bool operator==(const TimeStampValue& aOther) const { + return int64_t(*this - aOther) == 0; + } + bool operator!=(const TimeStampValue& aOther) const { + return int64_t(*this - aOther) != 0; + } + bool IsNull() const { return mIsNull; } + +#if defined(DEBUG) + uint64_t GTC() const { return mGTC; } + uint64_t QPC() const { return mQPC; } + + bool HasQPC() const { return mHasQPC; } +#endif +}; + +} // namespace mozilla + +#endif /* mozilla_TimeStamp_h */ diff --git a/mozglue/misc/Uptime.cpp b/mozglue/misc/Uptime.cpp new file mode 100644 index 0000000000..924b154359 --- /dev/null +++ b/mozglue/misc/Uptime.cpp @@ -0,0 +1,155 @@ +/* -*- 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 "Uptime.h" + +#ifdef XP_WIN +# include "mozilla/DynamicallyLinkedFunctionPtr.h" +#endif // XP_WIN + +#include <stdint.h> + +#include "mozilla/TimeStamp.h" +#include "mozilla/Maybe.h" +#include "mozilla/Assertions.h" + +using namespace mozilla; + +namespace { + +Maybe<uint64_t> NowIncludingSuspendMs(); +Maybe<uint64_t> NowExcludingSuspendMs(); +static Maybe<uint64_t> mStartExcludingSuspendMs; +static Maybe<uint64_t> mStartIncludingSuspendMs; + +// Apple things +#if defined(__APPLE__) && defined(__MACH__) +# include <time.h> +# include <sys/time.h> +# include <sys/types.h> +# include <mach/mach_time.h> + +const uint64_t kNSperMS = 1000000; + +Maybe<uint64_t> NowExcludingSuspendMs() { + return Some(clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / kNSperMS); +} + +Maybe<uint64_t> NowIncludingSuspendMs() { + return Some(clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW) / kNSperMS); +} + +#elif defined(XP_WIN) + +// Number of hundreds of nanoseconds in a millisecond +static constexpr uint64_t kHNSperMS = 10000; + +Maybe<uint64_t> NowExcludingSuspendMs() { + ULONGLONG interrupt_time; + if (!QueryUnbiasedInterruptTime(&interrupt_time)) { + return Nothing(); + } + return Some(interrupt_time / kHNSperMS); +} + +Maybe<uint64_t> NowIncludingSuspendMs() { + static const mozilla::StaticDynamicallyLinkedFunctionPtr<void(WINAPI*)( + PULONGLONG)> + pQueryInterruptTime(L"KernelBase.dll", "QueryInterruptTime"); + if (!pQueryInterruptTime) { + // On Windows, this does include the time the computer was suspended so it's + // an adequate fallback. + TimeStamp processCreation = TimeStamp::ProcessCreation(); + TimeStamp now = TimeStamp::Now(); + if (!processCreation.IsNull() && !now.IsNull()) { + return Some(uint64_t((now - processCreation).ToMilliseconds())); + } else { + return Nothing(); + } + } + ULONGLONG interrupt_time; + pQueryInterruptTime(&interrupt_time); + return Some(interrupt_time / kHNSperMS); +} + +#elif defined(XP_UNIX) // including BSDs and Android +# include <time.h> + +// Number of nanoseconds in a millisecond. +static constexpr uint64_t kNSperMS = 1000000; + +uint64_t TimespecToMilliseconds(struct timespec aTs) { + return aTs.tv_sec * 1000 + aTs.tv_nsec / kNSperMS; +} + +Maybe<uint64_t> NowExcludingSuspendMs() { + struct timespec ts = {0}; + +# ifdef XP_OPENBSD + if (clock_gettime(CLOCK_UPTIME, &ts)) { +# else + if (clock_gettime(CLOCK_MONOTONIC, &ts)) { +# endif + return Nothing(); + } + return Some(TimespecToMilliseconds(ts)); +} + +Maybe<uint64_t> NowIncludingSuspendMs() { +# ifndef CLOCK_BOOTTIME + return Nothing(); +# else + struct timespec ts = {0}; + + if (clock_gettime(CLOCK_BOOTTIME, &ts)) { + return Nothing(); + } + return Some(TimespecToMilliseconds(ts)); +# endif +} + +#else // catch all + +Maybe<uint64_t> NowExcludingSuspendMs() { return Nothing(); } +Maybe<uint64_t> NowIncludingSuspendMs() { return Nothing(); } + +#endif + +}; // anonymous namespace + +namespace mozilla { + +void InitializeUptime() { + MOZ_RELEASE_ASSERT(mStartIncludingSuspendMs.isNothing() && + mStartExcludingSuspendMs.isNothing(), + "Must not be called more than once"); + mStartIncludingSuspendMs = NowIncludingSuspendMs(); + mStartExcludingSuspendMs = NowExcludingSuspendMs(); +} + +Maybe<uint64_t> ProcessUptimeMs() { + if (!mStartIncludingSuspendMs) { + return Nothing(); + } + Maybe<uint64_t> maybeNow = NowIncludingSuspendMs(); + if (!maybeNow) { + return Nothing(); + } + return Some(maybeNow.value() - mStartIncludingSuspendMs.value()); +} + +Maybe<uint64_t> ProcessUptimeExcludingSuspendMs() { + if (!mStartExcludingSuspendMs) { + return Nothing(); + } + Maybe<uint64_t> maybeNow = NowExcludingSuspendMs(); + if (!maybeNow) { + return Nothing(); + } + return Some(maybeNow.value() - mStartExcludingSuspendMs.value()); +} + +}; // namespace mozilla diff --git a/mozglue/misc/Uptime.h b/mozglue/misc/Uptime.h new file mode 100644 index 0000000000..4438e0d6d1 --- /dev/null +++ b/mozglue/misc/Uptime.h @@ -0,0 +1,26 @@ +/* -*- 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/. */ + +#ifndef mozilla_Uptime_h +#define mozilla_Uptime_h + +#include <stdint.h> + +#include "mozilla/Maybe.h" + +namespace mozilla { + +// Called at the beginning of the process from TimeStamp::Startup. +MFBT_API void InitializeUptime(); +// Returns the number of milliseconds the calling process has lived for. +MFBT_API Maybe<uint64_t> ProcessUptimeMs(); +// Returns the number of milliseconds the calling process has lived for, +// excluding the time period the system was suspended. +MFBT_API Maybe<uint64_t> ProcessUptimeExcludingSuspendMs(); + +}; // namespace mozilla + +#endif // mozilla_Uptime_h diff --git a/mozglue/misc/WinUtils.h b/mozglue/misc/WinUtils.h new file mode 100644 index 0000000000..2291a352a5 --- /dev/null +++ b/mozglue/misc/WinUtils.h @@ -0,0 +1,140 @@ +/* -*- 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/. */ + +#ifndef mozilla_glue_MozglueUtils_h +#define mozilla_glue_MozglueUtils_h + +#include <windows.h> + +#include "mozilla/Atomics.h" +#include "mozilla/Attributes.h" + +namespace mozilla { +namespace glue { + +#ifdef DEBUG + +class MOZ_STATIC_CLASS Win32SRWLock final { + public: + // Microsoft guarantees that '0' is never a valid thread id + // https://docs.microsoft.com/en-ca/windows/desktop/ProcThread/thread-handles-and-identifiers + static const DWORD kInvalidThreadId = 0; + + constexpr Win32SRWLock() + : mExclusiveThreadId(kInvalidThreadId), mLock(SRWLOCK_INIT) {} + + ~Win32SRWLock() { MOZ_ASSERT(mExclusiveThreadId == kInvalidThreadId); } + + void LockShared() { + MOZ_ASSERT( + mExclusiveThreadId != GetCurrentThreadId(), + "Deadlock detected - A thread attempted to acquire a shared lock on " + "a SRWLOCK when it already owns the exclusive lock on it."); + + ::AcquireSRWLockShared(&mLock); + } + + void UnlockShared() { ::ReleaseSRWLockShared(&mLock); } + + void LockExclusive() { + MOZ_ASSERT( + mExclusiveThreadId != GetCurrentThreadId(), + "Deadlock detected - A thread attempted to acquire an exclusive lock " + "on a SRWLOCK when it already owns the exclusive lock on it."); + + ::AcquireSRWLockExclusive(&mLock); + mExclusiveThreadId = GetCurrentThreadId(); + } + + void UnlockExclusive() { + MOZ_ASSERT(mExclusiveThreadId == GetCurrentThreadId()); + + mExclusiveThreadId = kInvalidThreadId; + ::ReleaseSRWLockExclusive(&mLock); + } + + Win32SRWLock(const Win32SRWLock&) = delete; + Win32SRWLock(Win32SRWLock&&) = delete; + Win32SRWLock& operator=(const Win32SRWLock&) = delete; + Win32SRWLock& operator=(Win32SRWLock&&) = delete; + + private: + // "Relaxed" memory ordering is fine. Threads will see other thread IDs + // appear here in some non-deterministic ordering (or not at all) and simply + // ignore them. + // + // But a thread will only read its own ID if it previously wrote it, and a + // single thread doesn't need a memory barrier to read its own write. + + Atomic<DWORD, Relaxed> mExclusiveThreadId; + SRWLOCK mLock; +}; + +#else // DEBUG + +class MOZ_STATIC_CLASS Win32SRWLock final { + public: + constexpr Win32SRWLock() : mLock(SRWLOCK_INIT) {} + + void LockShared() { ::AcquireSRWLockShared(&mLock); } + + void UnlockShared() { ::ReleaseSRWLockShared(&mLock); } + + void LockExclusive() { ::AcquireSRWLockExclusive(&mLock); } + + void UnlockExclusive() { ::ReleaseSRWLockExclusive(&mLock); } + + ~Win32SRWLock() = default; + + Win32SRWLock(const Win32SRWLock&) = delete; + Win32SRWLock(Win32SRWLock&&) = delete; + Win32SRWLock& operator=(const Win32SRWLock&) = delete; + Win32SRWLock& operator=(Win32SRWLock&&) = delete; + + private: + SRWLOCK mLock; +}; + +#endif + +class MOZ_RAII AutoSharedLock final { + public: + explicit AutoSharedLock(Win32SRWLock& aLock) : mLock(aLock) { + mLock.LockShared(); + } + + ~AutoSharedLock() { mLock.UnlockShared(); } + + AutoSharedLock(const AutoSharedLock&) = delete; + AutoSharedLock(AutoSharedLock&&) = delete; + AutoSharedLock& operator=(const AutoSharedLock&) = delete; + AutoSharedLock& operator=(AutoSharedLock&&) = delete; + + private: + Win32SRWLock& mLock; +}; + +class MOZ_RAII AutoExclusiveLock final { + public: + explicit AutoExclusiveLock(Win32SRWLock& aLock) : mLock(aLock) { + mLock.LockExclusive(); + } + + ~AutoExclusiveLock() { mLock.UnlockExclusive(); } + + AutoExclusiveLock(const AutoExclusiveLock&) = delete; + AutoExclusiveLock(AutoExclusiveLock&&) = delete; + AutoExclusiveLock& operator=(const AutoExclusiveLock&) = delete; + AutoExclusiveLock& operator=(AutoExclusiveLock&&) = delete; + + private: + Win32SRWLock& mLock; +}; + +} // namespace glue +} // namespace mozilla + +#endif // mozilla_glue_MozglueUtils_h diff --git a/mozglue/misc/WindowsDllMain.cpp b/mozglue/misc/WindowsDllMain.cpp new file mode 100644 index 0000000000..1baec678db --- /dev/null +++ b/mozglue/misc/WindowsDllMain.cpp @@ -0,0 +1,20 @@ +/* -*- 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 <libloaderapi.h> + +BOOL WINAPI DllMain(HINSTANCE aInstDll, DWORD aReason, LPVOID) { + if (aReason == DLL_PROCESS_ATTACH) { + ::DisableThreadLibraryCalls(aInstDll); + + // mozglue.dll imports RtlGenRandom from advapi32.dll as SystemFunction036, + // but the actual function is implemented in cryptbase.dll. To avoid + // loading a fake cryptbase.dll from the installation directory, we preload + // cryptbase.dll from the system directory. + ::LoadLibraryExW(L"cryptbase.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32); + } + return TRUE; +} diff --git a/mozglue/misc/WindowsDpiAwareness.h b/mozglue/misc/WindowsDpiAwareness.h new file mode 100644 index 0000000000..104b135536 --- /dev/null +++ b/mozglue/misc/WindowsDpiAwareness.h @@ -0,0 +1,41 @@ +/* -*- 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 https://mozilla.org/MPL/2.0/. */ + +#ifndef WindowsDpiAwareness_h_ +#define WindowsDpiAwareness_h_ + +#include <windows.h> + +#if !defined(DPI_AWARENESS_CONTEXT_DECLARED) && \ + !defined(DPI_AWARENESS_CONTEXT_UNAWARE) + +DECLARE_HANDLE(DPI_AWARENESS_CONTEXT); + +typedef enum DPI_AWARENESS { + DPI_AWARENESS_INVALID = -1, + DPI_AWARENESS_UNAWARE = 0, + DPI_AWARENESS_SYSTEM_AWARE = 1, + DPI_AWARENESS_PER_MONITOR_AWARE = 2 +} DPI_AWARENESS; + +# define DPI_AWARENESS_CONTEXT_UNAWARE ((DPI_AWARENESS_CONTEXT)-1) +# define DPI_AWARENESS_CONTEXT_SYSTEM_AWARE ((DPI_AWARENESS_CONTEXT)-2) +# define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE ((DPI_AWARENESS_CONTEXT)-3) + +# define DPI_AWARENESS_CONTEXT_DECLARED +#endif // (DPI_AWARENESS_CONTEXT_DECLARED) + +#if WINVER < 0x0605 +WINUSERAPI DPI_AWARENESS_CONTEXT WINAPI GetThreadDpiAwarenessContext(); +WINUSERAPI BOOL WINAPI AreDpiAwarenessContextsEqual(DPI_AWARENESS_CONTEXT, + DPI_AWARENESS_CONTEXT); +#endif /* WINVER < 0x0605 */ +typedef DPI_AWARENESS_CONTEXT(WINAPI* SetThreadDpiAwarenessContextProc)( + DPI_AWARENESS_CONTEXT); +typedef BOOL(WINAPI* EnableNonClientDpiScalingProc)(HWND); +typedef int(WINAPI* GetSystemMetricsForDpiProc)(int, UINT); + +#endif diff --git a/mozglue/misc/WindowsDpiInitialization.cpp b/mozglue/misc/WindowsDpiInitialization.cpp new file mode 100644 index 0000000000..2e957b5830 --- /dev/null +++ b/mozglue/misc/WindowsDpiInitialization.cpp @@ -0,0 +1,73 @@ +/* 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 "mozilla/WindowsDpiInitialization.h" + +#include "mozilla/DynamicallyLinkedFunctionPtr.h" +#include "mozilla/WindowsProcessMitigations.h" +#include "mozilla/WindowsVersion.h" + +#include <shellscalingapi.h> +#include <windows.h> + +namespace mozilla { + +typedef HRESULT(WINAPI* SetProcessDpiAwarenessType)(PROCESS_DPI_AWARENESS); +typedef BOOL(WINAPI* SetProcessDpiAwarenessContextType)(DPI_AWARENESS_CONTEXT); + +WindowsDpiInitializationResult WindowsDpiInitialization() { + // DPI Awareness can't be used in a Win32k Lockdown process, so there's + // nothing to do + if (IsWin32kLockedDown()) { + return WindowsDpiInitializationResult::Success; + } + + // From MSDN: + // SetProcessDpiAwarenessContext() was added in the Win10 Anniversary Update + // SetProcessDpiAwareness() was added in Windows 8.1 + // SetProcessDpiAware() was added in Windows Vista + // + // DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 wasn't added later until + // the Creators Update, so if it fails we just fall back to + // DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE + if (IsWin10AnniversaryUpdateOrLater()) { + DynamicallyLinkedFunctionPtr<SetProcessDpiAwarenessContextType> + setProcessDpiAwarenessContext(L"user32.dll", + "SetProcessDpiAwarenessContext"); + if (!setProcessDpiAwarenessContext) { + return WindowsDpiInitializationResult:: + FindSetProcessDpiAwarenessContextFailed; + } + + if (!setProcessDpiAwarenessContext( + DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2) && + !setProcessDpiAwarenessContext( + DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE)) { + return WindowsDpiInitializationResult:: + SetProcessDpiAwarenessContextFailed; + } + + return WindowsDpiInitializationResult::Success; + } else if (IsWin8Point1OrLater()) { + DynamicallyLinkedFunctionPtr<SetProcessDpiAwarenessType> + setProcessDpiAwareness(L"Shcore.dll", "SetProcessDpiAwareness"); + if (!setProcessDpiAwareness) { + return WindowsDpiInitializationResult::FindSetProcessDpiAwarenessFailed; + } + + if (FAILED(setProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE))) { + return WindowsDpiInitializationResult::SetProcessDpiAwarenessFailed; + } + + return WindowsDpiInitializationResult::Success; + } else { + if (!SetProcessDPIAware()) { + return WindowsDpiInitializationResult::SetProcessDPIAwareFailed; + } + + return WindowsDpiInitializationResult::Success; + } +} + +} // namespace mozilla diff --git a/mozglue/misc/WindowsDpiInitialization.h b/mozglue/misc/WindowsDpiInitialization.h new file mode 100644 index 0000000000..5943d50526 --- /dev/null +++ b/mozglue/misc/WindowsDpiInitialization.h @@ -0,0 +1,54 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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/. */ + +#ifndef MOZILLA_MOZGLUE_MISC_WINDOWSDPIINITIALIZATION_H_ +#define MOZILLA_MOZGLUE_MISC_WINDOWSDPIINITIALIZATION_H_ +#include "mozilla/Types.h" + +namespace mozilla { + +// The result codes that may be returned from WindowsDpiInitialization() +enum class WindowsDpiInitializationResult : uint32_t { + Success, + FindSetProcessDpiAwarenessContextFailed, + SetProcessDpiAwarenessContextFailed, + FindSetProcessDpiAwarenessFailed, + SetProcessDpiAwarenessFailed, + SetProcessDPIAwareFailed, +}; + +// Get a string representation of any WindowsDpiInitializationResult value +inline const char* WindowsDpiInitializationResultString( + WindowsDpiInitializationResult result) { + switch (result) { + case WindowsDpiInitializationResult::Success: + return "Success"; + case WindowsDpiInitializationResult:: + FindSetProcessDpiAwarenessContextFailed: + return "Failed to find SetProcessDpiAwarenessContext"; + case WindowsDpiInitializationResult::SetProcessDpiAwarenessContextFailed: + return "SetProcessDpiAwarenessContext failed"; + case WindowsDpiInitializationResult::FindSetProcessDpiAwarenessFailed: + return "Failed to find SetProcessDpiAwareness"; + case WindowsDpiInitializationResult::SetProcessDpiAwarenessFailed: + return "SetProcessDpiAwareness failed"; + case WindowsDpiInitializationResult::SetProcessDPIAwareFailed: + return "SetProcessDPIAware failed"; + default: + return "Unknown result"; + } +} + +// Initialize DPI awareness to the best available for the current OS +// According to MSDN, this will be: +// Per-Monitor V2 for Windows 10 Creators Update (1703) and later +// Per-Monitor V1 for Windows 8.1 and later +// System DPI for Vista and later (we don't support anything older) +// https://docs.microsoft.com/en-us/windows/win32/hidpi/high-dpi-desktop-application-development-on-windows +MFBT_API WindowsDpiInitializationResult WindowsDpiInitialization(); + +} // namespace mozilla + +#endif // MOZILLA_MOZGLUE_MISC_WINDOWSDPIINITIALIZATION_H_ diff --git a/mozglue/misc/WindowsEnumProcessModules.h b/mozglue/misc/WindowsEnumProcessModules.h new file mode 100644 index 0000000000..573b0dbdfa --- /dev/null +++ b/mozglue/misc/WindowsEnumProcessModules.h @@ -0,0 +1,61 @@ +/* -*- 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/. */ + +#ifndef mozilla_WindowsEnumProcessModules_h +#define mozilla_WindowsEnumProcessModules_h + +#include <windows.h> +#include <psapi.h> + +#include "mozilla/FunctionRef.h" +#include "mozilla/NativeNt.h" +#include "mozilla/UniquePtr.h" +#include "mozilla/WinHeaderOnlyUtils.h" + +namespace mozilla { + +// Why don't we use CreateToolhelp32Snapshot instead of EnumProcessModules? +// CreateToolhelp32Snapshot gets the ANSI versions of module path strings +// via ntdll!RtlQueryProcessDebugInformation and stores them into a snapshot. +// Module32FirstW/Module32NextW re-converts ANSI into Unicode, but it cannot +// restore lost information. This means we still need GetModuleFileNameEx +// even when we use CreateToolhelp32Snapshot, but EnumProcessModules is faster. +inline bool EnumerateProcessModules( + const FunctionRef<void(const wchar_t*, HMODULE)>& aCallback) { + DWORD modulesSize; + if (!::EnumProcessModules(nt::kCurrentProcess, nullptr, 0, &modulesSize)) { + return false; + } + + DWORD modulesNum = modulesSize / sizeof(HMODULE); + UniquePtr<HMODULE[]> modules = MakeUnique<HMODULE[]>(modulesNum); + if (!::EnumProcessModules(nt::kCurrentProcess, modules.get(), + modulesNum * sizeof(HMODULE), &modulesSize)) { + return false; + } + + // The list may have shrunk between calls + if (modulesSize / sizeof(HMODULE) < modulesNum) { + modulesNum = modulesSize / sizeof(HMODULE); + } + + for (DWORD i = 0; i < modulesNum; ++i) { + UniquePtr<wchar_t[]> modulePath = GetFullModulePath(modules[i]); + if (!modulePath) { + continue; + } + + // Please note that modules[i] could be invalid if the module + // was unloaded after GetFullModulePath succeeded. + aCallback(modulePath.get(), modules[i]); + } + + return true; +} + +} // namespace mozilla + +#endif // mozilla_WindowsEnumProcessModules_h diff --git a/mozglue/misc/WindowsMapRemoteView.cpp b/mozglue/misc/WindowsMapRemoteView.cpp new file mode 100644 index 0000000000..aca9979ab6 --- /dev/null +++ b/mozglue/misc/WindowsMapRemoteView.cpp @@ -0,0 +1,124 @@ +/* -*- 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 https://mozilla.org/MPL/2.0/. */ + +#include "mozilla/WindowsMapRemoteView.h" + +#include "mozilla/Assertions.h" +#include "mozilla/DynamicallyLinkedFunctionPtr.h" + +#include <winternl.h> + +#if (NTDDI_VERSION < NTDDI_WIN10_RS2) + +// MapViewOfFile2 is just an inline function that calls MapViewOfFileNuma2 with +// its preferred node set to NUMA_NO_PREFERRED_NODE +WINBASEAPI PVOID WINAPI MapViewOfFileNuma2(HANDLE aFileMapping, HANDLE aProcess, + ULONG64 aOffset, PVOID aBaseAddress, + SIZE_T aViewSize, + ULONG aAllocationType, + ULONG aPageProtection, + ULONG aPreferredNode); + +WINBASEAPI BOOL WINAPI UnmapViewOfFile2(HANDLE aProcess, PVOID aBaseAddress, + ULONG aUnmapFlags); + +#endif // (NTDDI_VERSION < NTDDI_WIN10_RS2) + +enum SECTION_INHERIT { ViewShare = 1, ViewUnmap = 2 }; + +NTSTATUS NTAPI NtMapViewOfSection( + HANDLE aSection, HANDLE aProcess, PVOID* aBaseAddress, ULONG_PTR aZeroBits, + SIZE_T aCommitSize, PLARGE_INTEGER aSectionOffset, PSIZE_T aViewSize, + SECTION_INHERIT aInheritDisposition, ULONG aAllocationType, + ULONG aProtectionFlags); + +NTSTATUS NTAPI NtUnmapViewOfSection(HANDLE aProcess, PVOID aBaseAddress); + +static DWORD GetWin32ErrorCode(NTSTATUS aNtStatus) { + static const mozilla::StaticDynamicallyLinkedFunctionPtr< + decltype(&RtlNtStatusToDosError)> + pRtlNtStatusToDosError(L"ntdll.dll", "RtlNtStatusToDosError"); + + MOZ_ASSERT(!!pRtlNtStatusToDosError); + if (!pRtlNtStatusToDosError) { + return ERROR_GEN_FAILURE; + } + + return pRtlNtStatusToDosError(aNtStatus); +} + +namespace mozilla { + +MFBT_API void* MapRemoteViewOfFile(HANDLE aFileMapping, HANDLE aProcess, + ULONG64 aOffset, PVOID aBaseAddress, + SIZE_T aViewSize, ULONG aAllocationType, + ULONG aProtectionFlags) { + static const StaticDynamicallyLinkedFunctionPtr<decltype(&MapViewOfFileNuma2)> + pMapViewOfFileNuma2(L"Api-ms-win-core-memory-l1-1-5.dll", + "MapViewOfFileNuma2"); + + if (!!pMapViewOfFileNuma2) { + return pMapViewOfFileNuma2(aFileMapping, aProcess, aOffset, aBaseAddress, + aViewSize, aAllocationType, aProtectionFlags, + NUMA_NO_PREFERRED_NODE); + } + + static const StaticDynamicallyLinkedFunctionPtr<decltype(&NtMapViewOfSection)> + pNtMapViewOfSection(L"ntdll.dll", "NtMapViewOfSection"); + + MOZ_ASSERT(!!pNtMapViewOfSection); + if (!pNtMapViewOfSection) { + return nullptr; + } + + // For the sake of consistency, we only permit the same flags that + // MapViewOfFileNuma2 allows + if (aAllocationType != 0 && aAllocationType != MEM_RESERVE && + aAllocationType != MEM_LARGE_PAGES) { + ::SetLastError(ERROR_INVALID_PARAMETER); + return nullptr; + } + + NTSTATUS ntStatus; + + LARGE_INTEGER offset; + offset.QuadPart = aOffset; + + ntStatus = pNtMapViewOfSection(aFileMapping, aProcess, &aBaseAddress, 0, 0, + &offset, &aViewSize, ViewUnmap, + aAllocationType, aProtectionFlags); + if (NT_SUCCESS(ntStatus)) { + ::SetLastError(ERROR_SUCCESS); + return aBaseAddress; + } + + ::SetLastError(GetWin32ErrorCode(ntStatus)); + return nullptr; +} + +MFBT_API bool UnmapRemoteViewOfFile(HANDLE aProcess, PVOID aBaseAddress) { + static const StaticDynamicallyLinkedFunctionPtr<decltype(&UnmapViewOfFile2)> + pUnmapViewOfFile2(L"kernel32.dll", "UnmapViewOfFile2"); + + if (!!pUnmapViewOfFile2) { + return !!pUnmapViewOfFile2(aProcess, aBaseAddress, 0); + } + + static const StaticDynamicallyLinkedFunctionPtr< + decltype(&NtUnmapViewOfSection)> + pNtUnmapViewOfSection(L"ntdll.dll", "NtUnmapViewOfSection"); + + MOZ_ASSERT(!!pNtUnmapViewOfSection); + if (!pNtUnmapViewOfSection) { + return false; + } + + NTSTATUS ntStatus = pNtUnmapViewOfSection(aProcess, aBaseAddress); + ::SetLastError(GetWin32ErrorCode(ntStatus)); + return NT_SUCCESS(ntStatus); +} + +} // namespace mozilla diff --git a/mozglue/misc/WindowsMapRemoteView.h b/mozglue/misc/WindowsMapRemoteView.h new file mode 100644 index 0000000000..6ab88074b5 --- /dev/null +++ b/mozglue/misc/WindowsMapRemoteView.h @@ -0,0 +1,25 @@ +/* -*- 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 https://mozilla.org/MPL/2.0/. */ + +#ifndef mozilla_WindowsMapRemoteView_h +#define mozilla_WindowsMapRemoteView_h + +#include "mozilla/Types.h" + +#include <windows.h> + +namespace mozilla { + +MFBT_API PVOID MapRemoteViewOfFile(HANDLE aFileMapping, HANDLE aProcess, + ULONG64 aOffset, PVOID aBaseAddress, + SIZE_T aViewSize, ULONG aAllocationType, + ULONG aProtectionFlags); + +MFBT_API bool UnmapRemoteViewOfFile(HANDLE aProcess, PVOID aBaseAddress); + +} // namespace mozilla + +#endif // mozilla_WindowsMapRemoteView_h diff --git a/mozglue/misc/WindowsProcessMitigations.cpp b/mozglue/misc/WindowsProcessMitigations.cpp new file mode 100644 index 0000000000..f528738b77 --- /dev/null +++ b/mozglue/misc/WindowsProcessMitigations.cpp @@ -0,0 +1,91 @@ +/* -*- 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 "mozilla/WindowsProcessMitigations.h" + +#include <processthreadsapi.h> + +#include "mozilla/Assertions.h" +#include "mozilla/DynamicallyLinkedFunctionPtr.h" + +static_assert(sizeof(PROCESS_MITIGATION_DYNAMIC_CODE_POLICY) == 4); + +#if (_WIN32_WINNT < 0x0602) +BOOL WINAPI GetProcessMitigationPolicy( + HANDLE hProcess, PROCESS_MITIGATION_POLICY MitigationPolicy, PVOID lpBuffer, + SIZE_T dwLength); +#endif // (_WIN32_WINNT < 0x0602) + +namespace mozilla { + +static decltype(&::GetProcessMitigationPolicy) +FetchGetProcessMitigationPolicyFunc() { + static const StaticDynamicallyLinkedFunctionPtr< + decltype(&::GetProcessMitigationPolicy)> + pGetProcessMitigationPolicy(L"kernel32.dll", + "GetProcessMitigationPolicy"); + return pGetProcessMitigationPolicy; +} + +static bool sWin32kLockedDownInPolicy = false; + +MFBT_API bool IsWin32kLockedDown() { + static bool sWin32kLockedDown = []() { + auto pGetProcessMitigationPolicy = FetchGetProcessMitigationPolicyFunc(); + + PROCESS_MITIGATION_SYSTEM_CALL_DISABLE_POLICY polInfo; + if (!pGetProcessMitigationPolicy || + !pGetProcessMitigationPolicy(::GetCurrentProcess(), + ProcessSystemCallDisablePolicy, &polInfo, + sizeof(polInfo))) { + // We failed to get pointer to GetProcessMitigationPolicy or the call + // to it failed, so just return what the sandbox policy says. + return sWin32kLockedDownInPolicy; + } + + return !!polInfo.DisallowWin32kSystemCalls; + }(); + + return sWin32kLockedDown; +} + +MFBT_API void SetWin32kLockedDownInPolicy() { + sWin32kLockedDownInPolicy = true; +} + +MFBT_API bool IsDynamicCodeDisabled() { + auto pGetProcessMitigationPolicy = FetchGetProcessMitigationPolicyFunc(); + if (!pGetProcessMitigationPolicy) { + return false; + } + + PROCESS_MITIGATION_DYNAMIC_CODE_POLICY polInfo; + if (!pGetProcessMitigationPolicy(::GetCurrentProcess(), + ProcessDynamicCodePolicy, &polInfo, + sizeof(polInfo))) { + return false; + } + + return polInfo.ProhibitDynamicCode; +} + +MFBT_API bool IsEafPlusEnabled() { + auto pGetProcessMitigationPolicy = FetchGetProcessMitigationPolicyFunc(); + if (!pGetProcessMitigationPolicy) { + return false; + } + + PROCESS_MITIGATION_PAYLOAD_RESTRICTION_POLICY polInfo; + if (!pGetProcessMitigationPolicy(::GetCurrentProcess(), + ProcessPayloadRestrictionPolicy, &polInfo, + sizeof(polInfo))) { + return false; + } + + return polInfo.EnableExportAddressFilterPlus; +} + +} // namespace mozilla diff --git a/mozglue/misc/WindowsProcessMitigations.h b/mozglue/misc/WindowsProcessMitigations.h new file mode 100644 index 0000000000..2f02553617 --- /dev/null +++ b/mozglue/misc/WindowsProcessMitigations.h @@ -0,0 +1,21 @@ +/* -*- 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/. */ + +#ifndef mozilla_WindowsProcessMitigations_h +#define mozilla_WindowsProcessMitigations_h + +#include "mozilla/Types.h" + +namespace mozilla { + +MFBT_API bool IsWin32kLockedDown(); +MFBT_API void SetWin32kLockedDownInPolicy(); +MFBT_API bool IsDynamicCodeDisabled(); +MFBT_API bool IsEafPlusEnabled(); + +} // namespace mozilla + +#endif // mozilla_WindowsProcessMitigations_h diff --git a/mozglue/misc/WindowsStackCookie.h b/mozglue/misc/WindowsStackCookie.h new file mode 100644 index 0000000000..c4196b3853 --- /dev/null +++ b/mozglue/misc/WindowsStackCookie.h @@ -0,0 +1,63 @@ +/* -*- 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 https://mozilla.org/MPL/2.0/. */ + +#ifndef mozilla_WindowsStackCookie_h +#define mozilla_WindowsStackCookie_h + +#if defined(DEBUG) && defined(_M_X64) && !defined(__MINGW64__) + +# include <windows.h> +# include <winnt.h> + +# include <cstdint> + +# include "mozilla/Types.h" + +namespace mozilla { + +// This function does pattern matching on the instructions generated for a +// given function, to detect whether it uses stack buffers. More specifically, +// it looks for instructions that characterize the presence of stack cookie +// checks. When this function returns true, it can be a false positive, but we +// use a rather long pattern to make false positives very unlikely. +// Note: Do not use this function inside the function that lives at +// aFunctionAddress, as that could introduce stack buffers. +// Note: The pattern we use does not work for MinGW builds. +inline bool HasStackCookieCheck(uintptr_t aFunctionAddress) { + DWORD64 imageBase{}; + auto entry = ::RtlLookupFunctionEntry( + reinterpret_cast<DWORD64>(aFunctionAddress), &imageBase, nullptr); + if (entry && entry->EndAddress > entry->BeginAddress + 14) { + auto begin = reinterpret_cast<uint8_t*>(imageBase + entry->BeginAddress); + auto end = reinterpret_cast<uint8_t*>(imageBase + entry->EndAddress - 14); + for (auto pc = begin; pc != end; ++pc) { + // 48 8b 05 XX XX XX XX: mov rax, qword ptr [rip + XXXXXXXX] + if ((pc[0] == 0x48 && pc[1] == 0x8b && pc[2] == 0x05) && + // 48 31 e0: xor rax, rsp + (pc[7] == 0x48 && pc[8] == 0x31 && pc[9] == 0xe0) && + // 48 89 (8|4)4 24 ...: mov qword ptr [rsp + ...], rax + (pc[10] == 0x48 && pc[11] == 0x89 && + (pc[12] == 0x44 || pc[12] == 0x84) && pc[13] == 0x24)) { + return true; + } + } + } + // In x64, if there is no entry, then there is no stack allocation, hence + // there is no stack cookie check: "Table-based exception handling requires a + // table entry for all functions that allocate stack space or call another + // function (for example, nonleaf functions)." + // https://learn.microsoft.com/en-us/cpp/build/exception-handling-x64 + // Similarly, if the gap between begin and end is less than 14 bytes, then + // the function cannot contain the pattern we are looking for, therefore it + // has no cookie check either. + return false; +} + +} // namespace mozilla + +#endif // defined(DEBUG) && defined(_M_X64) && !defined(__MINGW64__) + +#endif // mozilla_WindowsStackCookie_h diff --git a/mozglue/misc/WindowsUnicode.cpp b/mozglue/misc/WindowsUnicode.cpp new file mode 100644 index 0000000000..464380b6da --- /dev/null +++ b/mozglue/misc/WindowsUnicode.cpp @@ -0,0 +1,59 @@ +/* -*- 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 https://mozilla.org/MPL/2.0/. */ + +#include "WindowsUnicode.h" + +#include <windows.h> +// For UNICODE_STRING +#include <winternl.h> + +#include <string.h> + +namespace mozilla { +namespace glue { + +mozilla::UniquePtr<char[]> WideToUTF8(const wchar_t* aStr, + const size_t aStrLenExclNul) { + int numConv = ::WideCharToMultiByte(CP_UTF8, 0, aStr, aStrLenExclNul, nullptr, + 0, nullptr, nullptr); + if (!numConv) { + return nullptr; + } + + // Include room for the null terminator by adding one + auto buf = mozilla::MakeUnique<char[]>(numConv + 1); + + numConv = ::WideCharToMultiByte(CP_UTF8, 0, aStr, aStrLenExclNul, buf.get(), + numConv, nullptr, nullptr); + if (!numConv) { + return nullptr; + } + + // Add null termination. numConv does not include the terminator, so we don't + // subtract 1 when indexing into buf. + buf[numConv] = 0; + + return buf; +} + +mozilla::UniquePtr<char[]> WideToUTF8(const wchar_t* aStr) { + return WideToUTF8(aStr, wcslen(aStr)); +} + +mozilla::UniquePtr<char[]> WideToUTF8(const std::wstring& aStr) { + return WideToUTF8(aStr.data(), aStr.length()); +} + +mozilla::UniquePtr<char[]> WideToUTF8(PCUNICODE_STRING aStr) { + if (!aStr) { + return nullptr; + } + + return WideToUTF8(aStr->Buffer, aStr->Length / sizeof(WCHAR)); +} + +} // namespace glue +} // namespace mozilla diff --git a/mozglue/misc/WindowsUnicode.h b/mozglue/misc/WindowsUnicode.h new file mode 100644 index 0000000000..77fc376b92 --- /dev/null +++ b/mozglue/misc/WindowsUnicode.h @@ -0,0 +1,35 @@ +/* -*- 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 https://mozilla.org/MPL/2.0/. */ + +#ifndef mozilla_glue_WindowsUnicode_h +#define mozilla_glue_WindowsUnicode_h + +#include "mozilla/UniquePtr.h" + +#include <string> + +struct _UNICODE_STRING; + +namespace mozilla { +namespace glue { + +mozilla::UniquePtr<char[]> WideToUTF8(const wchar_t* aStr, + const size_t aStrLenExclNul); + +mozilla::UniquePtr<char[]> WideToUTF8(const wchar_t* aStr); +mozilla::UniquePtr<char[]> WideToUTF8(const std::wstring& aStr); +mozilla::UniquePtr<char[]> WideToUTF8(const _UNICODE_STRING* aStr); + +#if defined(bstr_t) +inline mozilla::UniquePtr<char[]> WideToUTF8(const _bstr_t& aStr) { + return WideToUTF8(static_cast<const wchar_t*>(aStr), aStr.length()); +} +#endif // defined(bstr_t) + +} // namespace glue +} // namespace mozilla + +#endif // mozilla_glue_WindowsUnicode_h diff --git a/mozglue/misc/WindowsUnwindInfo.h b/mozglue/misc/WindowsUnwindInfo.h new file mode 100644 index 0000000000..3667b5f2bc --- /dev/null +++ b/mozglue/misc/WindowsUnwindInfo.h @@ -0,0 +1,329 @@ +/* -*- 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 https://mozilla.org/MPL/2.0/. */ + +#ifndef mozilla_WindowsUnwindInfo_h +#define mozilla_WindowsUnwindInfo_h + +#ifdef _M_X64 + +# include <cstdint> + +# include "mozilla/Assertions.h" +# include "mozilla/UniquePtr.h" + +namespace mozilla { + +// On Windows x64, there is no standard function prologue, hence extra +// information that describes the prologue must be added for each non-leaf +// function in order to properly unwind the stack. This extra information is +// grouped into so-called function tables. +// +// A function table is a contiguous array of one or more RUNTIME_FUNCTION +// entries. Each RUNTIME_FUNCTION entry associates a start and end offset in +// code with specific unwind information. The function table is present in the +// .pdata section of binaries for static code, and added dynamically with +// RtlAddFunctionTable or RtlInstallFunctionTableCallback for dynamic code. +// RUNTIME_FUNCTION entries point to the unwind information, which can thus +// live at a different location in memory, for example it lives in the .xdata +// section for static code. +// +// Contrary to RUNTIME_FUNCTION, Microsoft provides no standard structure +// definition to map the unwind information. This file thus provides some +// helpers to read this data, originally based on breakpad code. The unwind +// information is partially documented at: +// https://learn.microsoft.com/en-us/cpp/build/exception-handling-x64. + +// The unwind information stores a bytecode in UnwindInfo.unwind_code[] that +// describes how the instructions in the function prologue interact with the +// stack. An instruction in this bytecode is called an unwind code. +// UnwindCodeOperationCodes enumerates all opcodes used by this bytecode. +// Unwind codes are stored in contiguous slots of 16 bits, where each unwind +// code can span either 1, 2, or 3 slots depending on the opcode it uses. +enum UnwindOperationCodes : uint8_t { + // UnwindCode.operation_info == register number + UWOP_PUSH_NONVOL = 0, + // UnwindCode.operation_info == 0 or 1, + // alloc size in next slot (if 0) or next 2 slots (if 1) + UWOP_ALLOC_LARGE = 1, + // UnwindCode.operation_info == size of allocation / 8 - 1 + UWOP_ALLOC_SMALL = 2, + // no UnwindCode.operation_info; register number UnwindInfo.frame_register + // receives (rsp + UnwindInfo.frame_offset*16) + UWOP_SET_FPREG = 3, + // UnwindCode.operation_info == register number, offset in next slot + UWOP_SAVE_NONVOL = 4, + // UnwindCode.operation_info == register number, offset in next 2 slots + UWOP_SAVE_NONVOL_FAR = 5, + // Version 1; undocumented; not meant for x64 + UWOP_SAVE_XMM = 6, + // Version 2; undocumented + UWOP_EPILOG = 6, + // Version 1; undocumented; not meant for x64 + UWOP_SAVE_XMM_FAR = 7, + // Version 2; undocumented + UWOP_SPARE = 7, + // UnwindCode.operation_info == XMM reg number, offset in next slot + UWOP_SAVE_XMM128 = 8, + // UnwindCode.operation_info == XMM reg number, offset in next 2 slots + UWOP_SAVE_XMM128_FAR = 9, + // UnwindCode.operation_info == 0: no error-code, 1: error-code + UWOP_PUSH_MACHFRAME = 10 +}; + +// Strictly speaking, UnwindCode represents a slot -- not a full unwind code. +union UnwindCode { + struct { + uint8_t offset_in_prolog; + UnwindOperationCodes unwind_operation_code : 4; + uint8_t operation_info : 4; + }; + uint16_t frame_offset; +}; + +// UnwindInfo is a variable-sized struct meant for C-style direct access to the +// unwind information. Be careful: +// - prefer using the size() helper method to using sizeof; +// - don't construct objects of this type, cast pointers instead; +// - consider using the IterableUnwindInfo helpers to iterate over unwind +// codes. +struct UnwindInfo { + uint8_t version : 3; + uint8_t flags : 5; // either 0, UNW_FLAG_CHAININFO, or a combination of + // UNW_FLAG_EHANDLER and UNW_FLAG_UHANDLER + uint8_t size_of_prolog; + uint8_t count_of_codes; // contains the length of the unwind_code[] array + uint8_t frame_register : 4; + uint8_t frame_offset : 4; + UnwindCode unwind_code[1]; // variable length + // Note: There is extra data after the variable length array if using flags + // UNW_FLAG_EHANDLER, UNW_FLAG_UHANDLER, or UNW_FLAG_CHAININFO. We + // ignore the extra data at the moment. For more details, see: + // https://learn.microsoft.com/en-us/cpp/build/exception-handling-x64. + // + // When using UNW_FLAG_EHANDLER or UNW_FLAG_UHANDLER, the extra data + // includes handler data of unspecificied size: only the handler knows + // the correct size for this data. This makes it difficult to know the + // size of the full unwind information or to copy it in this particular + // case. + + UnwindInfo(const UnwindInfo&) = delete; + UnwindInfo& operator=(const UnwindInfo&) = delete; + UnwindInfo(UnwindInfo&&) = delete; + UnwindInfo& operator=(UnwindInfo&&) = delete; + ~UnwindInfo() = delete; + + // Size of this structure, including the variable length unwind_code array + // but NOT including the extra data related to flags UNW_FLAG_EHANDLER, + // UNW_FLAG_UHANDLER, and UNW_FLAG_CHAININFO. + // + // The places where we currently use these helpers read unwind information at + // function entry points; as such we expect that they may encounter + // UNW_FLAG_EHANDLER and/or UNW_FLAG_UHANDLER but won't need to use the + // associated extra data, and it is expected that they should not encounter + // UNW_FLAG_CHAININFO. UNW_FLAG_CHAININFO is typically used for code that + // lives separately from the entry point of the function to which it belongs, + // this code then has chained unwind info pointing to the entry point. + inline size_t Size() const { + return offsetof(UnwindInfo, unwind_code) + + count_of_codes * sizeof(UnwindCode); + } + + // Note: We currently do not copy the extra data related to flags + // UNW_FLAG_EHANDLER, UNW_FLAG_UHANDLER, and UNW_FLAG_CHAININFO. + UniquePtr<uint8_t[]> Copy() const { + auto s = Size(); + auto result = MakeUnique<uint8_t[]>(s); + std::memcpy(result.get(), reinterpret_cast<const void*>(this), s); + return result; + } + + // An unwind code spans a number of slots in the unwind_code array that can + // vary from 1 to 3. This method assumes that the index parameter points to + // a slot that is the start of an unwind code. If the unwind code is + // well-formed, it returns true and sets its second parameter to the number + // of slots that the unwind code occupies. + // + // This function returns false if the unwind code is ill-formed, i.e.: + // - either the index points out of bounds; + // - or the opcode is invalid, or unexpected (e.g. UWOP_SAVE_XMM and + // UWOP_SAVE_XMM_FAR in version 1); + // - or using the correct slots count for the opcode would go out of bounds. + bool GetSlotsCountForCodeAt(uint8_t aIndex, uint8_t* aSlotsCount) const { + if (aIndex >= count_of_codes) { + MOZ_ASSERT_UNREACHABLE("The index is out of bounds"); + return false; + } + + const UnwindCode& unwindCode = unwind_code[aIndex]; + uint8_t slotsCount = 0; + + // See https://learn.microsoft.com/en-us/cpp/build/exception-handling-x64 + switch (unwindCode.unwind_operation_code) { + // Start with fixed-size opcodes common to versions 1 and 2 + case UWOP_SAVE_NONVOL_FAR: + case UWOP_SAVE_XMM128_FAR: + slotsCount = 3; + break; + + case UWOP_SAVE_NONVOL: + case UWOP_SAVE_XMM128: + slotsCount = 2; + break; + + case UWOP_PUSH_NONVOL: + case UWOP_ALLOC_SMALL: + case UWOP_SET_FPREG: + case UWOP_PUSH_MACHFRAME: + slotsCount = 1; + break; + + // UWOP_ALLOC_LARGE is the only variable-sized opcode. It is common to + // versions 1 and 2. It is ill-formed if the info is not 0 or 1. + case UWOP_ALLOC_LARGE: + if (unwindCode.operation_info > 1) { + MOZ_ASSERT_UNREACHABLE( + "Operation UWOP_ALLOC_LARGE is used, but operation_info " + "is not 0 or 1"); + return false; + } + slotsCount = 2 + unwindCode.operation_info; + break; + + case UWOP_SPARE: + if (version != 2) { + MOZ_ASSERT_UNREACHABLE( + "Operation code UWOP_SPARE is used, but version is not 2"); + return false; + } + slotsCount = 3; + break; + + case UWOP_EPILOG: + if (version != 2) { + MOZ_ASSERT_UNREACHABLE( + "Operation code UWOP_EPILOG is used, but version is not 2"); + return false; + } + slotsCount = 2; + break; + + default: + MOZ_ASSERT_UNREACHABLE("An unknown operation code is used"); + return false; + } + + // The unwind code is ill-formed if using the correct number of slots for + // the opcode would go out of bounds. + if (count_of_codes - aIndex < slotsCount) { + MOZ_ASSERT_UNREACHABLE( + "A valid operation code is used, but it spans too many slots"); + return false; + } + + *aSlotsCount = slotsCount; + return true; + } +}; + +class IterableUnwindInfo { + class Iterator { + public: + UnwindInfo& Info() { return mInfo; } + + uint8_t Index() const { + MOZ_ASSERT(IsValid()); + return mIndex; + } + + uint8_t SlotsCount() const { + MOZ_ASSERT(IsValid()); + return mSlotsCount; + } + + // An iterator is valid if it points to a well-formed unwind code. + // The end iterator is invalid as it does not point to any unwind code. + // All invalid iterators compare equal, which allows comparison with the + // end iterator to exit loops as soon as an ill-formed unwind code is met. + bool IsValid() const { return mIsValid; } + + bool IsAtEnd() const { return mIndex >= mInfo.count_of_codes; } + + bool operator==(const Iterator& aOther) const { + if (mIsValid != aOther.mIsValid) { + return false; + } + // Comparing two invalid iterators. + if (!mIsValid) { + return true; + } + // Comparing two valid iterators. + return mIndex == aOther.mIndex; + } + + bool operator!=(const Iterator& aOther) const { return !(*this == aOther); } + + Iterator& operator++() { + MOZ_ASSERT(IsValid()); + mIndex += mSlotsCount; + if (mIndex < mInfo.count_of_codes) { + mIsValid = mInfo.GetSlotsCountForCodeAt(mIndex, &mSlotsCount); + MOZ_ASSERT(IsValid()); + } else { + mIsValid = false; + } + return *this; + } + + const UnwindCode& operator*() { + MOZ_ASSERT(IsValid()); + return mInfo.unwind_code[mIndex]; + } + + private: + friend class IterableUnwindInfo; + + Iterator(UnwindInfo& aInfo, uint8_t aIndex, uint8_t aSlotsCount, + bool aIsValid) + : mInfo(aInfo), + mIndex(aIndex), + mSlotsCount(aSlotsCount), + mIsValid(aIsValid){}; + + UnwindInfo& mInfo; + uint8_t mIndex; + uint8_t mSlotsCount; + bool mIsValid; + }; + + public: + explicit IterableUnwindInfo(UnwindInfo& aInfo) + : mBegin(aInfo, 0, 0, false), + mEnd(aInfo, aInfo.count_of_codes, 0, false) { + if (aInfo.count_of_codes) { + mBegin.mIsValid = aInfo.GetSlotsCountForCodeAt(0, &mBegin.mSlotsCount); + MOZ_ASSERT(mBegin.mIsValid); + } + } + + explicit IterableUnwindInfo(uint8_t* aInfo) + : IterableUnwindInfo(*reinterpret_cast<UnwindInfo*>(aInfo)) {} + + UnwindInfo& Info() { return mBegin.Info(); } + + const Iterator& begin() { return mBegin; } + + const Iterator& end() { return mEnd; } + + private: + Iterator mBegin; + Iterator mEnd; +}; + +} // namespace mozilla + +#endif // _M_X64 + +#endif // mozilla_WindowsUnwindInfo_h diff --git a/mozglue/misc/decimal/Decimal.cpp b/mozglue/misc/decimal/Decimal.cpp new file mode 100644 index 0000000000..cc828e2843 --- /dev/null +++ b/mozglue/misc/decimal/Decimal.cpp @@ -0,0 +1,1063 @@ +/* + * Copyright (C) 2012 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "Decimal.h" +#include "moz-decimal-utils.h" +#include "DoubleConversion.h" + +using namespace moz_decimal_utils; + +#include <algorithm> +#include <float.h> + +namespace blink { + +namespace DecimalPrivate { + +static int const ExponentMax = 1023; +static int const ExponentMin = -1023; +static int const Precision = 18; + +static const uint64_t MaxCoefficient = UINT64_C(0xDE0B6B3A763FFFF); // 999999999999999999 == 18 9's + +// This class handles Decimal special values. +class SpecialValueHandler { + STACK_ALLOCATED(); + WTF_MAKE_NONCOPYABLE(SpecialValueHandler); +public: + enum HandleResult { + BothFinite, + BothInfinity, + EitherNaN, + LHSIsInfinity, + RHSIsInfinity, + }; + + SpecialValueHandler(const Decimal& lhs, const Decimal& rhs); + HandleResult handle(); + Decimal value() const; + +private: + enum Result { + ResultIsLHS, + ResultIsRHS, + ResultIsUnknown, + }; + + const Decimal& m_lhs; + const Decimal& m_rhs; + Result m_result; +}; + +SpecialValueHandler::SpecialValueHandler(const Decimal& lhs, const Decimal& rhs) + : m_lhs(lhs), m_rhs(rhs), m_result(ResultIsUnknown) +{ +} + +SpecialValueHandler::HandleResult SpecialValueHandler::handle() +{ + if (m_lhs.isFinite() && m_rhs.isFinite()) + return BothFinite; + + const Decimal::EncodedData::FormatClass lhsClass = m_lhs.value().formatClass(); + const Decimal::EncodedData::FormatClass rhsClass = m_rhs.value().formatClass(); + if (lhsClass == Decimal::EncodedData::ClassNaN) { + m_result = ResultIsLHS; + return EitherNaN; + } + + if (rhsClass == Decimal::EncodedData::ClassNaN) { + m_result = ResultIsRHS; + return EitherNaN; + } + + if (lhsClass == Decimal::EncodedData::ClassInfinity) + return rhsClass == Decimal::EncodedData::ClassInfinity ? BothInfinity : LHSIsInfinity; + + if (rhsClass == Decimal::EncodedData::ClassInfinity) + return RHSIsInfinity; + + ASSERT_NOT_REACHED(); + return BothFinite; +} + +Decimal SpecialValueHandler::value() const +{ + switch (m_result) { + case ResultIsLHS: + return m_lhs; + case ResultIsRHS: + return m_rhs; + case ResultIsUnknown: + default: + ASSERT_NOT_REACHED(); + return m_lhs; + } +} + +// This class is used for 128 bit unsigned integer arithmetic. +class UInt128 { +public: + UInt128(uint64_t low, uint64_t high) + : m_high(high), m_low(low) + { + } + + UInt128& operator/=(uint32_t); + + uint64_t high() const { return m_high; } + uint64_t low() const { return m_low; } + + static UInt128 multiply(uint64_t u, uint64_t v) { return UInt128(u * v, multiplyHigh(u, v)); } + +private: + static uint32_t highUInt32(uint64_t x) { return static_cast<uint32_t>(x >> 32); } + static uint32_t lowUInt32(uint64_t x) { return static_cast<uint32_t>(x & ((static_cast<uint64_t>(1) << 32) - 1)); } + static uint64_t makeUInt64(uint32_t low, uint32_t high) { return low | (static_cast<uint64_t>(high) << 32); } + + static uint64_t multiplyHigh(uint64_t, uint64_t); + + uint64_t m_high; + uint64_t m_low; +}; + +UInt128& UInt128::operator/=(const uint32_t divisor) +{ + ASSERT(divisor); + + if (!m_high) { + m_low /= divisor; + return *this; + } + + uint32_t dividend[4]; + dividend[0] = lowUInt32(m_low); + dividend[1] = highUInt32(m_low); + dividend[2] = lowUInt32(m_high); + dividend[3] = highUInt32(m_high); + + uint32_t quotient[4]; + uint32_t remainder = 0; + for (int i = 3; i >= 0; --i) { + const uint64_t work = makeUInt64(dividend[i], remainder); + remainder = static_cast<uint32_t>(work % divisor); + quotient[i] = static_cast<uint32_t>(work / divisor); + } + m_low = makeUInt64(quotient[0], quotient[1]); + m_high = makeUInt64(quotient[2], quotient[3]); + return *this; +} + +// Returns high 64bit of 128bit product. +uint64_t UInt128::multiplyHigh(uint64_t u, uint64_t v) +{ + const uint64_t uLow = lowUInt32(u); + const uint64_t uHigh = highUInt32(u); + const uint64_t vLow = lowUInt32(v); + const uint64_t vHigh = highUInt32(v); + const uint64_t partialProduct = uHigh * vLow + highUInt32(uLow * vLow); + return uHigh * vHigh + highUInt32(partialProduct) + highUInt32(uLow * vHigh + lowUInt32(partialProduct)); +} + +static int countDigits(uint64_t x) +{ + int numberOfDigits = 0; + for (uint64_t powerOfTen = 1; x >= powerOfTen; powerOfTen *= 10) { + ++numberOfDigits; + if (powerOfTen >= std::numeric_limits<uint64_t>::max() / 10) + break; + } + return numberOfDigits; +} + +static uint64_t scaleDown(uint64_t x, int n) +{ + ASSERT(n >= 0); + while (n > 0 && x) { + x /= 10; + --n; + } + return x; +} + +static uint64_t scaleUp(uint64_t x, int n) +{ + ASSERT(n >= 0); + ASSERT(n <= Precision); + + uint64_t y = 1; + uint64_t z = 10; + for (;;) { + if (n & 1) + y = y * z; + + n >>= 1; + if (!n) + return x * y; + + z = z * z; + } +} + +} // namespace DecimalPrivate + +using namespace DecimalPrivate; + +Decimal::EncodedData::EncodedData(Sign sign, FormatClass formatClass) + : m_coefficient(0) + , m_exponent(0) + , m_formatClass(formatClass) + , m_sign(sign) +{ +} + +Decimal::EncodedData::EncodedData(Sign sign, int exponent, uint64_t coefficient) + : m_formatClass(coefficient ? ClassNormal : ClassZero) + , m_sign(sign) +{ + if (exponent >= ExponentMin && exponent <= ExponentMax) { + while (coefficient > MaxCoefficient) { + coefficient /= 10; + ++exponent; + } + } + + if (exponent > ExponentMax) { + m_coefficient = 0; + m_exponent = 0; + m_formatClass = ClassInfinity; + return; + } + + if (exponent < ExponentMin) { + m_coefficient = 0; + m_exponent = 0; + m_formatClass = ClassZero; + return; + } + + m_coefficient = coefficient; + m_exponent = static_cast<int16_t>(exponent); +} + +bool Decimal::EncodedData::operator==(const EncodedData& another) const +{ + return m_sign == another.m_sign + && m_formatClass == another.m_formatClass + && m_exponent == another.m_exponent + && m_coefficient == another.m_coefficient; +} + +Decimal::Decimal(int32_t i32) + : m_data(i32 < 0 ? Negative : Positive, 0, i32 < 0 ? static_cast<uint64_t>(-static_cast<int64_t>(i32)) : static_cast<uint64_t>(i32)) +{ +} + +Decimal::Decimal(Sign sign, int exponent, uint64_t coefficient) + : m_data(sign, coefficient ? exponent : 0, coefficient) +{ +} + +Decimal::Decimal(const EncodedData& data) + : m_data(data) +{ +} + +Decimal::Decimal(const Decimal& other) + : m_data(other.m_data) +{ +} + +Decimal& Decimal::operator=(const Decimal& other) +{ + m_data = other.m_data; + return *this; +} + +Decimal& Decimal::operator+=(const Decimal& other) +{ + m_data = (*this + other).m_data; + return *this; +} + +Decimal& Decimal::operator-=(const Decimal& other) +{ + m_data = (*this - other).m_data; + return *this; +} + +Decimal& Decimal::operator*=(const Decimal& other) +{ + m_data = (*this * other).m_data; + return *this; +} + +Decimal& Decimal::operator/=(const Decimal& other) +{ + m_data = (*this / other).m_data; + return *this; +} + +Decimal Decimal::operator-() const +{ + if (isNaN()) + return *this; + + Decimal result(*this); + result.m_data.setSign(invertSign(m_data.sign())); + return result; +} + +Decimal Decimal::operator+(const Decimal& rhs) const +{ + const Decimal& lhs = *this; + const Sign lhsSign = lhs.sign(); + const Sign rhsSign = rhs.sign(); + + SpecialValueHandler handler(lhs, rhs); + switch (handler.handle()) { + case SpecialValueHandler::BothFinite: + break; + + case SpecialValueHandler::BothInfinity: + return lhsSign == rhsSign ? lhs : nan(); + + case SpecialValueHandler::EitherNaN: + return handler.value(); + + case SpecialValueHandler::LHSIsInfinity: + return lhs; + + case SpecialValueHandler::RHSIsInfinity: + return rhs; + } + + const AlignedOperands alignedOperands = alignOperands(lhs, rhs); + + const uint64_t result = lhsSign == rhsSign + ? alignedOperands.lhsCoefficient + alignedOperands.rhsCoefficient + : alignedOperands.lhsCoefficient - alignedOperands.rhsCoefficient; + + if (lhsSign == Negative && rhsSign == Positive && !result) + return Decimal(Positive, alignedOperands.exponent, 0); + + return static_cast<int64_t>(result) >= 0 + ? Decimal(lhsSign, alignedOperands.exponent, result) + : Decimal(invertSign(lhsSign), alignedOperands.exponent, -static_cast<int64_t>(result)); +} + +Decimal Decimal::operator-(const Decimal& rhs) const +{ + const Decimal& lhs = *this; + const Sign lhsSign = lhs.sign(); + const Sign rhsSign = rhs.sign(); + + SpecialValueHandler handler(lhs, rhs); + switch (handler.handle()) { + case SpecialValueHandler::BothFinite: + break; + + case SpecialValueHandler::BothInfinity: + return lhsSign == rhsSign ? nan() : lhs; + + case SpecialValueHandler::EitherNaN: + return handler.value(); + + case SpecialValueHandler::LHSIsInfinity: + return lhs; + + case SpecialValueHandler::RHSIsInfinity: + return infinity(invertSign(rhsSign)); + } + + const AlignedOperands alignedOperands = alignOperands(lhs, rhs); + + const uint64_t result = lhsSign == rhsSign + ? alignedOperands.lhsCoefficient - alignedOperands.rhsCoefficient + : alignedOperands.lhsCoefficient + alignedOperands.rhsCoefficient; + + if (lhsSign == Negative && rhsSign == Negative && !result) + return Decimal(Positive, alignedOperands.exponent, 0); + + return static_cast<int64_t>(result) >= 0 + ? Decimal(lhsSign, alignedOperands.exponent, result) + : Decimal(invertSign(lhsSign), alignedOperands.exponent, -static_cast<int64_t>(result)); +} + +Decimal Decimal::operator*(const Decimal& rhs) const +{ + const Decimal& lhs = *this; + const Sign lhsSign = lhs.sign(); + const Sign rhsSign = rhs.sign(); + const Sign resultSign = lhsSign == rhsSign ? Positive : Negative; + + SpecialValueHandler handler(lhs, rhs); + switch (handler.handle()) { + case SpecialValueHandler::BothFinite: { + const uint64_t lhsCoefficient = lhs.m_data.coefficient(); + const uint64_t rhsCoefficient = rhs.m_data.coefficient(); + int resultExponent = lhs.exponent() + rhs.exponent(); + UInt128 work(UInt128::multiply(lhsCoefficient, rhsCoefficient)); + while (work.high()) { + work /= 10; + ++resultExponent; + } + return Decimal(resultSign, resultExponent, work.low()); + } + + case SpecialValueHandler::BothInfinity: + return infinity(resultSign); + + case SpecialValueHandler::EitherNaN: + return handler.value(); + + case SpecialValueHandler::LHSIsInfinity: + return rhs.isZero() ? nan() : infinity(resultSign); + + case SpecialValueHandler::RHSIsInfinity: + return lhs.isZero() ? nan() : infinity(resultSign); + } + + ASSERT_NOT_REACHED(); + return nan(); +} + +Decimal Decimal::operator/(const Decimal& rhs) const +{ + const Decimal& lhs = *this; + const Sign lhsSign = lhs.sign(); + const Sign rhsSign = rhs.sign(); + const Sign resultSign = lhsSign == rhsSign ? Positive : Negative; + + SpecialValueHandler handler(lhs, rhs); + switch (handler.handle()) { + case SpecialValueHandler::BothFinite: + break; + + case SpecialValueHandler::BothInfinity: + return nan(); + + case SpecialValueHandler::EitherNaN: + return handler.value(); + + case SpecialValueHandler::LHSIsInfinity: + return infinity(resultSign); + + case SpecialValueHandler::RHSIsInfinity: + return zero(resultSign); + } + + ASSERT(lhs.isFinite()); + ASSERT(rhs.isFinite()); + + if (rhs.isZero()) + return lhs.isZero() ? nan() : infinity(resultSign); + + int resultExponent = lhs.exponent() - rhs.exponent(); + + if (lhs.isZero()) + return Decimal(resultSign, resultExponent, 0); + + uint64_t remainder = lhs.m_data.coefficient(); + const uint64_t divisor = rhs.m_data.coefficient(); + uint64_t result = 0; + for (;;) { + while (remainder < divisor && result < MaxCoefficient / 10) { + remainder *= 10; + result *= 10; + --resultExponent; + } + if (remainder < divisor) + break; + uint64_t quotient = remainder / divisor; + if (result > MaxCoefficient - quotient) + break; + result += quotient; + remainder %= divisor; + if (!remainder) + break; + } + + if (remainder > divisor / 2) + ++result; + + return Decimal(resultSign, resultExponent, result); +} + +bool Decimal::operator==(const Decimal& rhs) const +{ + if (isNaN() || rhs.isNaN()) + return false; + return m_data == rhs.m_data || compareTo(rhs).isZero(); +} + +bool Decimal::operator!=(const Decimal& rhs) const +{ + if (isNaN() || rhs.isNaN()) + return true; + if (m_data == rhs.m_data) + return false; + const Decimal result = compareTo(rhs); + if (result.isNaN()) + return false; + return !result.isZero(); +} + +bool Decimal::operator<(const Decimal& rhs) const +{ + const Decimal result = compareTo(rhs); + if (result.isNaN()) + return false; + return !result.isZero() && result.isNegative(); +} + +bool Decimal::operator<=(const Decimal& rhs) const +{ + if (isNaN() || rhs.isNaN()) + return false; + if (m_data == rhs.m_data) + return true; + const Decimal result = compareTo(rhs); + if (result.isNaN()) + return false; + return result.isZero() || result.isNegative(); +} + +bool Decimal::operator>(const Decimal& rhs) const +{ + const Decimal result = compareTo(rhs); + if (result.isNaN()) + return false; + return !result.isZero() && result.isPositive(); +} + +bool Decimal::operator>=(const Decimal& rhs) const +{ + if (isNaN() || rhs.isNaN()) + return false; + if (m_data == rhs.m_data) + return true; + const Decimal result = compareTo(rhs); + if (result.isNaN()) + return false; + return result.isZero() || !result.isNegative(); +} + +Decimal Decimal::abs() const +{ + Decimal result(*this); + result.m_data.setSign(Positive); + return result; +} + +Decimal::AlignedOperands Decimal::alignOperands(const Decimal& lhs, const Decimal& rhs) +{ + ASSERT(lhs.isFinite()); + ASSERT(rhs.isFinite()); + + const int lhsExponent = lhs.exponent(); + const int rhsExponent = rhs.exponent(); + int exponent = std::min(lhsExponent, rhsExponent); + uint64_t lhsCoefficient = lhs.m_data.coefficient(); + uint64_t rhsCoefficient = rhs.m_data.coefficient(); + + if (lhsExponent > rhsExponent) { + const int numberOfLHSDigits = countDigits(lhsCoefficient); + if (numberOfLHSDigits) { + const int lhsShiftAmount = lhsExponent - rhsExponent; + const int overflow = numberOfLHSDigits + lhsShiftAmount - Precision; + if (overflow <= 0) { + lhsCoefficient = scaleUp(lhsCoefficient, lhsShiftAmount); + } else { + lhsCoefficient = scaleUp(lhsCoefficient, lhsShiftAmount - overflow); + rhsCoefficient = scaleDown(rhsCoefficient, overflow); + exponent += overflow; + } + } + + } else if (lhsExponent < rhsExponent) { + const int numberOfRHSDigits = countDigits(rhsCoefficient); + if (numberOfRHSDigits) { + const int rhsShiftAmount = rhsExponent - lhsExponent; + const int overflow = numberOfRHSDigits + rhsShiftAmount - Precision; + if (overflow <= 0) { + rhsCoefficient = scaleUp(rhsCoefficient, rhsShiftAmount); + } else { + rhsCoefficient = scaleUp(rhsCoefficient, rhsShiftAmount - overflow); + lhsCoefficient = scaleDown(lhsCoefficient, overflow); + exponent += overflow; + } + } + } + + AlignedOperands alignedOperands; + alignedOperands.exponent = exponent; + alignedOperands.lhsCoefficient = lhsCoefficient; + alignedOperands.rhsCoefficient = rhsCoefficient; + return alignedOperands; +} + +static bool isMultiplePowersOfTen(uint64_t coefficient, int n) +{ + return !coefficient || !(coefficient % scaleUp(1, n)); +} + +// Round toward positive infinity. +Decimal Decimal::ceil() const +{ + if (isSpecial()) + return *this; + + if (exponent() >= 0) + return *this; + + uint64_t result = m_data.coefficient(); + const int numberOfDigits = countDigits(result); + const int numberOfDropDigits = -exponent(); + if (numberOfDigits <= numberOfDropDigits) + return isPositive() ? Decimal(1) : zero(Positive); + + result = scaleDown(result, numberOfDropDigits); + if (isPositive() && !isMultiplePowersOfTen(m_data.coefficient(), numberOfDropDigits)) + ++result; + return Decimal(sign(), 0, result); +} + +Decimal Decimal::compareTo(const Decimal& rhs) const +{ + const Decimal result(*this - rhs); + switch (result.m_data.formatClass()) { + case EncodedData::ClassInfinity: + return result.isNegative() ? Decimal(-1) : Decimal(1); + + case EncodedData::ClassNaN: + case EncodedData::ClassNormal: + return result; + + case EncodedData::ClassZero: + return zero(Positive); + + default: + ASSERT_NOT_REACHED(); + return nan(); + } +} + +// Round toward negative infinity. +Decimal Decimal::floor() const +{ + if (isSpecial()) + return *this; + + if (exponent() >= 0) + return *this; + + uint64_t result = m_data.coefficient(); + const int numberOfDigits = countDigits(result); + const int numberOfDropDigits = -exponent(); + if (numberOfDigits < numberOfDropDigits) + return isPositive() ? zero(Positive) : Decimal(-1); + + result = scaleDown(result, numberOfDropDigits); + if (isNegative() && !isMultiplePowersOfTen(m_data.coefficient(), numberOfDropDigits)) + ++result; + return Decimal(sign(), 0, result); +} + +Decimal Decimal::fromDouble(double doubleValue) +{ + if (std::isfinite(doubleValue)) + return fromString(mozToString(doubleValue)); + + if (std::isinf(doubleValue)) + return infinity(doubleValue < 0 ? Negative : Positive); + + return nan(); +} + +Decimal Decimal::fromString(const String& str) +{ + int exponent = 0; + Sign exponentSign = Positive; + int numberOfDigits = 0; + int numberOfDigitsAfterDot = 0; + int numberOfExtraDigits = 0; + Sign sign = Positive; + + enum { + StateDigit, + StateDot, + StateDotDigit, + StateE, + StateEDigit, + StateESign, + StateSign, + StateStart, + StateZero, + } state = StateStart; + +#define HandleCharAndBreak(expected, nextState) \ + if (ch == expected) { \ + state = nextState; \ + break; \ + } + +#define HandleTwoCharsAndBreak(expected1, expected2, nextState) \ + if (ch == expected1 || ch == expected2) { \ + state = nextState; \ + break; \ + } + + uint64_t accumulator = 0; + for (unsigned index = 0; index < str.length(); ++index) { + const int ch = str[index]; + switch (state) { + case StateDigit: + if (ch >= '0' && ch <= '9') { + if (numberOfDigits < Precision) { + ++numberOfDigits; + accumulator *= 10; + accumulator += ch - '0'; + } else { + ++numberOfExtraDigits; + } + break; + } + + HandleCharAndBreak('.', StateDot); + HandleTwoCharsAndBreak('E', 'e', StateE); + return nan(); + + case StateDot: + case StateDotDigit: + if (ch >= '0' && ch <= '9') { + if (numberOfDigits < Precision) { + ++numberOfDigits; + ++numberOfDigitsAfterDot; + accumulator *= 10; + accumulator += ch - '0'; + } + state = StateDotDigit; + break; + } + + HandleTwoCharsAndBreak('E', 'e', StateE); + return nan(); + + case StateE: + if (ch == '+') { + exponentSign = Positive; + state = StateESign; + break; + } + + if (ch == '-') { + exponentSign = Negative; + state = StateESign; + break; + } + + if (ch >= '0' && ch <= '9') { + exponent = ch - '0'; + state = StateEDigit; + break; + } + + return nan(); + + case StateEDigit: + if (ch >= '0' && ch <= '9') { + exponent *= 10; + exponent += ch - '0'; + if (exponent > ExponentMax + Precision) { + if (accumulator) + return exponentSign == Negative ? zero(Positive) : infinity(sign); + return zero(sign); + } + state = StateEDigit; + break; + } + + return nan(); + + case StateESign: + if (ch >= '0' && ch <= '9') { + exponent = ch - '0'; + state = StateEDigit; + break; + } + + return nan(); + + case StateSign: + if (ch >= '1' && ch <= '9') { + accumulator = ch - '0'; + numberOfDigits = 1; + state = StateDigit; + break; + } + + HandleCharAndBreak('0', StateZero); + return nan(); + + case StateStart: + if (ch >= '1' && ch <= '9') { + accumulator = ch - '0'; + numberOfDigits = 1; + state = StateDigit; + break; + } + + if (ch == '-') { + sign = Negative; + state = StateSign; + break; + } + + if (ch == '+') { + sign = Positive; + state = StateSign; + break; + } + + HandleCharAndBreak('0', StateZero); + HandleCharAndBreak('.', StateDot); + return nan(); + + case StateZero: + if (ch == '0') + break; + + if (ch >= '1' && ch <= '9') { + accumulator = ch - '0'; + numberOfDigits = 1; + state = StateDigit; + break; + } + + HandleCharAndBreak('.', StateDot); + HandleTwoCharsAndBreak('E', 'e', StateE); + return nan(); + + default: + ASSERT_NOT_REACHED(); + return nan(); + } + } + + if (state == StateZero) + return zero(sign); + + if (state == StateDigit || state == StateEDigit || state == StateDotDigit) { + int resultExponent = exponent * (exponentSign == Negative ? -1 : 1) - numberOfDigitsAfterDot + numberOfExtraDigits; + if (resultExponent < ExponentMin) + return zero(Positive); + + const int overflow = resultExponent - ExponentMax + 1; + if (overflow > 0) { + if (overflow + numberOfDigits - numberOfDigitsAfterDot > Precision) + return infinity(sign); + accumulator = scaleUp(accumulator, overflow); + resultExponent -= overflow; + } + + return Decimal(sign, resultExponent, accumulator); + } + + return nan(); +} + +Decimal Decimal::infinity(const Sign sign) +{ + return Decimal(EncodedData(sign, EncodedData::ClassInfinity)); +} + +Decimal Decimal::nan() +{ + return Decimal(EncodedData(Positive, EncodedData::ClassNaN)); +} + +Decimal Decimal::remainder(const Decimal& rhs) const +{ + const Decimal quotient = *this / rhs; + return quotient.isSpecial() ? quotient : *this - (quotient.isNegative() ? quotient.ceil() : quotient.floor()) * rhs; +} + +Decimal Decimal::round() const +{ + if (isSpecial()) + return *this; + + if (exponent() >= 0) + return *this; + + uint64_t result = m_data.coefficient(); + const int numberOfDigits = countDigits(result); + const int numberOfDropDigits = -exponent(); + if (numberOfDigits < numberOfDropDigits) + return zero(Positive); + + result = scaleDown(result, numberOfDropDigits - 1); + if (result % 10 >= 5) + result += 10; + result /= 10; + return Decimal(sign(), 0, result); +} + +double Decimal::toDouble() const +{ + if (isFinite()) { + bool valid; + const double doubleValue = mozToDouble(toString(), &valid); + return valid ? doubleValue : std::numeric_limits<double>::quiet_NaN(); + } + + if (isInfinity()) + return isNegative() ? -std::numeric_limits<double>::infinity() : std::numeric_limits<double>::infinity(); + + return std::numeric_limits<double>::quiet_NaN(); +} + +String Decimal::toString() const +{ + switch (m_data.formatClass()) { + case EncodedData::ClassInfinity: + return sign() ? "-Infinity" : "Infinity"; + + case EncodedData::ClassNaN: + return "NaN"; + + case EncodedData::ClassNormal: + case EncodedData::ClassZero: + break; + + default: + ASSERT_NOT_REACHED(); + return ""; + } + + StringBuilder builder; + if (sign()) + builder.append('-'); + + int originalExponent = exponent(); + uint64_t coefficient = m_data.coefficient(); + + if (originalExponent < 0) { + const int maxDigits = DBL_DIG; + uint64_t lastDigit = 0; + while (countDigits(coefficient) > maxDigits) { + lastDigit = coefficient % 10; + coefficient /= 10; + ++originalExponent; + } + + if (lastDigit >= 5) + ++coefficient; + + while (originalExponent < 0 && coefficient && !(coefficient % 10)) { + coefficient /= 10; + ++originalExponent; + } + } + + const String digits = mozToString(coefficient); + int coefficientLength = static_cast<int>(digits.length()); + const int adjustedExponent = originalExponent + coefficientLength - 1; + if (originalExponent <= 0 && adjustedExponent >= -6) { + if (!originalExponent) { + builder.append(digits); + return builder.toString(); + } + + if (adjustedExponent >= 0) { + for (int i = 0; i < coefficientLength; ++i) { + builder.append(digits[i]); + if (i == adjustedExponent) + builder.append('.'); + } + return builder.toString(); + } + + builder.appendLiteral("0."); + for (int i = adjustedExponent + 1; i < 0; ++i) + builder.append('0'); + + builder.append(digits); + + } else { + builder.append(digits[0]); + while (coefficientLength >= 2 && digits[coefficientLength - 1] == '0') + --coefficientLength; + if (coefficientLength >= 2) { + builder.append('.'); + for (int i = 1; i < coefficientLength; ++i) + builder.append(digits[i]); + } + + if (adjustedExponent) { + builder.append(adjustedExponent < 0 ? "e" : "e+"); + builder.appendNumber(adjustedExponent); + } + } + return builder.toString(); +} + +bool Decimal::toString(char* strBuf, size_t bufLength) const +{ + ASSERT(bufLength > 0); + String str = toString(); + size_t length = str.copy(strBuf, bufLength); + if (length < bufLength) { + strBuf[length] = '\0'; + return true; + } + strBuf[bufLength - 1] = '\0'; + return false; +} + +Decimal Decimal::zero(Sign sign) +{ + return Decimal(EncodedData(sign, EncodedData::ClassZero)); +} + +} // namespace blink + +// Implementation of DoubleConversion.h: + +namespace mozilla { + +Maybe<double> StringToDouble(Span<const char> aStringSpan) { + bool valid = false; + double result = mozToDouble(aStringSpan, &valid); + return valid ? Some(result) : Nothing(); +} + +} diff --git a/mozglue/misc/decimal/Decimal.h b/mozglue/misc/decimal/Decimal.h new file mode 100644 index 0000000000..10d0e2c7ce --- /dev/null +++ b/mozglue/misc/decimal/Decimal.h @@ -0,0 +1,221 @@ +/* + * Copyright (C) 2012 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * Imported from: + * https://chromium.googlesource.com/chromium/src.git/+/master/third_party/WebKit/Source/platform/Decimal.h + * Check UPSTREAM-GIT-SHA for the commit ID of the last update from Blink core. + */ + +#ifndef Decimal_h +#define Decimal_h + +#include "mozilla/Assertions.h" +#include <stdint.h> +#include "mozilla/Types.h" + +#include <string> + +#ifndef ASSERT +#define DEFINED_ASSERT_FOR_DECIMAL_H 1 +#define ASSERT MOZ_ASSERT +#endif + +#define PLATFORM_EXPORT + +// To use USING_FAST_MALLOC we'd need: +// https://chromium.googlesource.com/chromium/src.git/+/master/third_party/WebKit/Source/wtf/Allocator.h +// Since we don't allocate Decimal objects, no need. +#define USING_FAST_MALLOC(type) \ + void ignore_this_dummy_method() = delete + +#define DISALLOW_NEW() \ + private: \ + void* operator new(size_t) = delete; \ + void* operator new(size_t, void*) = delete; \ + public: + +namespace blink { + +namespace DecimalPrivate { +class SpecialValueHandler; +} + +// This class represents decimal base floating point number. +// +// FIXME: Once all C++ compiler support decimal type, we should replace this +// class to compiler supported one. See below URI for current status of decimal +// type for C++: // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1977.html +class PLATFORM_EXPORT Decimal { + USING_FAST_MALLOC(Decimal); +public: + enum Sign { + Positive, + Negative, + }; + + // You should not use EncodedData other than unit testing. + class EncodedData { + DISALLOW_NEW(); + // For accessing FormatClass. + friend class Decimal; + friend class DecimalPrivate::SpecialValueHandler; + public: + EncodedData(Sign, int exponent, uint64_t coefficient); + + bool operator==(const EncodedData&) const; + bool operator!=(const EncodedData& another) const { return !operator==(another); } + + uint64_t coefficient() const { return m_coefficient; } + int countDigits() const; + int exponent() const { return m_exponent; } + bool isFinite() const { return !isSpecial(); } + bool isInfinity() const { return m_formatClass == ClassInfinity; } + bool isNaN() const { return m_formatClass == ClassNaN; } + bool isSpecial() const { return m_formatClass == ClassInfinity || m_formatClass == ClassNaN; } + bool isZero() const { return m_formatClass == ClassZero; } + Sign sign() const { return m_sign; } + void setSign(Sign sign) { m_sign = sign; } + + private: + enum FormatClass { + ClassInfinity, + ClassNormal, + ClassNaN, + ClassZero, + }; + + EncodedData(Sign, FormatClass); + FormatClass formatClass() const { return m_formatClass; } + + uint64_t m_coefficient; + int16_t m_exponent; + FormatClass m_formatClass; + Sign m_sign; + }; + + MFBT_API explicit Decimal(int32_t = 0); + MFBT_API Decimal(Sign, int exponent, uint64_t coefficient); + MFBT_API Decimal(const Decimal&); + + MFBT_API Decimal& operator=(const Decimal&); + MFBT_API Decimal& operator+=(const Decimal&); + MFBT_API Decimal& operator-=(const Decimal&); + MFBT_API Decimal& operator*=(const Decimal&); + MFBT_API Decimal& operator/=(const Decimal&); + + MFBT_API Decimal operator-() const; + + MFBT_API bool operator==(const Decimal&) const; + MFBT_API bool operator!=(const Decimal&) const; + MFBT_API bool operator<(const Decimal&) const; + MFBT_API bool operator<=(const Decimal&) const; + MFBT_API bool operator>(const Decimal&) const; + MFBT_API bool operator>=(const Decimal&) const; + + MFBT_API Decimal operator+(const Decimal&) const; + MFBT_API Decimal operator-(const Decimal&) const; + MFBT_API Decimal operator*(const Decimal&) const; + MFBT_API Decimal operator/(const Decimal&) const; + + int exponent() const + { + ASSERT(isFinite()); + return m_data.exponent(); + } + + bool isFinite() const { return m_data.isFinite(); } + bool isInfinity() const { return m_data.isInfinity(); } + bool isNaN() const { return m_data.isNaN(); } + bool isNegative() const { return sign() == Negative; } + bool isPositive() const { return sign() == Positive; } + bool isSpecial() const { return m_data.isSpecial(); } + bool isZero() const { return m_data.isZero(); } + + MFBT_API Decimal abs() const; + MFBT_API Decimal ceil() const; + MFBT_API Decimal floor() const; + MFBT_API Decimal remainder(const Decimal&) const; + MFBT_API Decimal round() const; + + MFBT_API double toDouble() const; + // Note: toString method supports infinity and nan but fromString not. + MFBT_API std::string toString() const; + MFBT_API bool toString(char* strBuf, size_t bufLength) const; + + static MFBT_API Decimal fromDouble(double); + // fromString supports following syntax EBNF: + // number ::= sign? digit+ ('.' digit*) (exponent-marker sign? digit+)? + // | sign? '.' digit+ (exponent-marker sign? digit+)? + // sign ::= '+' | '-' + // exponent-marker ::= 'e' | 'E' + // digit ::= '0' | '1' | ... | '9' + // Note: fromString doesn't support "infinity" and "nan". + static MFBT_API Decimal fromString(const std::string& aValue); + static MFBT_API Decimal infinity(Sign); + static MFBT_API Decimal nan(); + static MFBT_API Decimal zero(Sign); + + // You should not use below methods. We expose them for unit testing. + MFBT_API explicit Decimal(const EncodedData&); + const EncodedData& value() const { return m_data; } + +private: + struct AlignedOperands { + uint64_t lhsCoefficient; + uint64_t rhsCoefficient; + int exponent; + }; + + MFBT_API explicit Decimal(double); + MFBT_API Decimal compareTo(const Decimal&) const; + + static MFBT_API AlignedOperands alignOperands(const Decimal& lhs, const Decimal& rhs); + static inline Sign invertSign(Sign sign) { return sign == Negative ? Positive : Negative; } + + Sign sign() const { return m_data.sign(); } + + EncodedData m_data; +}; + +} // namespace blink + +namespace mozilla { +typedef blink::Decimal Decimal; +} // namespace mozilla + +#undef USING_FAST_MALLOC + +#ifdef DEFINED_ASSERT_FOR_DECIMAL_H +#undef DEFINED_ASSERT_FOR_DECIMAL_H +#undef ASSERT +#endif + +#endif // Decimal_h diff --git a/mozglue/misc/decimal/DoubleConversion.h b/mozglue/misc/decimal/DoubleConversion.h new file mode 100644 index 0000000000..14c19e2540 --- /dev/null +++ b/mozglue/misc/decimal/DoubleConversion.h @@ -0,0 +1,27 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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/. */ + +/* A utility function that converts a string to a double independent of OS locale. */ + +#ifndef MOZILLA_DOUBLECONVERSION_H +#define MOZILLA_DOUBLECONVERSION_H + +#include "mozilla/Maybe.h" +#include "mozilla/Span.h" + +#include <string> + +namespace mozilla { + +// Parses aStringSpan into a double floating point value. Always treats . as the +// decimal separator, regardless of OS locale. Consumes the entire string; +// trailing garbage is invalid. Returns Nothing() for invalid input. +// The implementation uses double_conversion::StringToDoubleConverter with +// NO_FLAGS, see double-conversion/string-to-double.h for more documentation. +Maybe<double> StringToDouble(Span<const char> aStringSpan); + +} + +#endif // MOZILLA_DOUBLECONVERSION_H diff --git a/mozglue/misc/decimal/UPSTREAM-GIT-SHA b/mozglue/misc/decimal/UPSTREAM-GIT-SHA new file mode 100644 index 0000000000..ed86150b28 --- /dev/null +++ b/mozglue/misc/decimal/UPSTREAM-GIT-SHA @@ -0,0 +1 @@ +cad4c9e3b3c9e80bb189059373db528272bca96f diff --git a/mozglue/misc/decimal/add-doubleconversion-impl.patch b/mozglue/misc/decimal/add-doubleconversion-impl.patch new file mode 100644 index 0000000000..1cf0fb6ff1 --- /dev/null +++ b/mozglue/misc/decimal/add-doubleconversion-impl.patch @@ -0,0 +1,42 @@ +diff --git a/mozglue/misc/decimal/Decimal.cpp b/mozglue/misc/decimal/Decimal.cpp +--- a/mozglue/misc/decimal/Decimal.cpp ++++ b/mozglue/misc/decimal/Decimal.cpp +@@ -25,16 +25,17 @@ + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + #include "Decimal.h" + #include "moz-decimal-utils.h" ++#include "DoubleConversion.h" + + using namespace moz_decimal_utils; + + #include <algorithm> + #include <float.h> + + namespace blink { + +@@ -1043,8 +1044,20 @@ bool Decimal::toString(char* strBuf, siz + } + + Decimal Decimal::zero(Sign sign) + { + return Decimal(EncodedData(sign, EncodedData::ClassZero)); + } + + } // namespace blink ++ ++// Implementation of DoubleConversion.h: ++ ++namespace mozilla { ++ ++Maybe<double> StringToDouble(Span<const char> aStringSpan) { ++ bool valid = false; ++ double result = mozToDouble(aStringSpan, &valid); ++ return valid ? Some(result) : Nothing(); ++} ++ ++} diff --git a/mozglue/misc/decimal/comparison-with-nan.patch b/mozglue/misc/decimal/comparison-with-nan.patch new file mode 100644 index 0000000000..0e274ce033 --- /dev/null +++ b/mozglue/misc/decimal/comparison-with-nan.patch @@ -0,0 +1,67 @@ +diff --git a/mozglue/misc/decimal/Decimal.cpp b/mozglue/misc/decimal/Decimal.cpp +--- a/mozglue/misc/decimal/Decimal.cpp ++++ b/mozglue/misc/decimal/Decimal.cpp +@@ -509,21 +509,25 @@ Decimal Decimal::operator/(const Decimal + if (remainder > divisor / 2) + ++result; + + return Decimal(resultSign, resultExponent, result); + } + + bool Decimal::operator==(const Decimal& rhs) const + { ++ if (isNaN() || rhs.isNaN()) ++ return false; + return m_data == rhs.m_data || compareTo(rhs).isZero(); + } + + bool Decimal::operator!=(const Decimal& rhs) const + { ++ if (isNaN() || rhs.isNaN()) ++ return true; + if (m_data == rhs.m_data) + return false; + const Decimal result = compareTo(rhs); + if (result.isNaN()) + return false; + return !result.isZero(); + } + +@@ -532,16 +536,18 @@ bool Decimal::operator<(const Decimal& r + const Decimal result = compareTo(rhs); + if (result.isNaN()) + return false; + return !result.isZero() && result.isNegative(); + } + + bool Decimal::operator<=(const Decimal& rhs) const + { ++ if (isNaN() || rhs.isNaN()) ++ return false; + if (m_data == rhs.m_data) + return true; + const Decimal result = compareTo(rhs); + if (result.isNaN()) + return false; + return result.isZero() || result.isNegative(); + } + +@@ -550,16 +556,18 @@ bool Decimal::operator>(const Decimal& r + const Decimal result = compareTo(rhs); + if (result.isNaN()) + return false; + return !result.isZero() && result.isPositive(); + } + + bool Decimal::operator>=(const Decimal& rhs) const + { ++ if (isNaN() || rhs.isNaN()) ++ return false; + if (m_data == rhs.m_data) + return true; + const Decimal result = compareTo(rhs); + if (result.isNaN()) + return false; + return result.isZero() || !result.isNegative(); + } + diff --git a/mozglue/misc/decimal/fix-wshadow-warnings.patch b/mozglue/misc/decimal/fix-wshadow-warnings.patch new file mode 100644 index 0000000000..465c61a223 --- /dev/null +++ b/mozglue/misc/decimal/fix-wshadow-warnings.patch @@ -0,0 +1,171 @@ +diff --git a/mozglue/misc/decimal/Decimal.cpp b/mozglue/misc/decimal/Decimal.cpp +--- a/mozglue/misc/decimal/Decimal.cpp ++++ b/mozglue/misc/decimal/Decimal.cpp +@@ -118,18 +118,18 @@ Decimal SpecialValueHandler::value() con + ASSERT_NOT_REACHED(); + return m_lhs; + } + } + + // This class is used for 128 bit unsigned integer arithmetic. + class UInt128 { + public: +- UInt128(uint64_t low, uint64_t high) +- : m_high(high), m_low(low) ++ UInt128(uint64_t aLow, uint64_t aHigh) ++ : m_high(aHigh), m_low(aLow) + { + } + + UInt128& operator/=(uint32_t); + + uint64_t high() const { return m_high; } + uint64_t low() const { return m_low; } + +@@ -224,68 +224,68 @@ static uint64_t scaleUp(uint64_t x, int + z = z * z; + } + } + + } // namespace DecimalPrivate + + using namespace DecimalPrivate; + +-Decimal::EncodedData::EncodedData(Sign sign, FormatClass formatClass) ++Decimal::EncodedData::EncodedData(Sign aSign, FormatClass aFormatClass) + : m_coefficient(0) + , m_exponent(0) +- , m_formatClass(formatClass) +- , m_sign(sign) ++ , m_formatClass(aFormatClass) ++ , m_sign(aSign) + { + } + +-Decimal::EncodedData::EncodedData(Sign sign, int exponent, uint64_t coefficient) +- : m_formatClass(coefficient ? ClassNormal : ClassZero) +- , m_sign(sign) ++Decimal::EncodedData::EncodedData(Sign aSign, int aExponent, uint64_t aCoefficient) ++ : m_formatClass(aCoefficient ? ClassNormal : ClassZero) ++ , m_sign(aSign) + { +- if (exponent >= ExponentMin && exponent <= ExponentMax) { +- while (coefficient > MaxCoefficient) { +- coefficient /= 10; +- ++exponent; ++ if (aExponent >= ExponentMin && aExponent <= ExponentMax) { ++ while (aCoefficient > MaxCoefficient) { ++ aCoefficient /= 10; ++ ++aExponent; + } + } + +- if (exponent > ExponentMax) { ++ if (aExponent > ExponentMax) { + m_coefficient = 0; + m_exponent = 0; + m_formatClass = ClassInfinity; + return; + } + +- if (exponent < ExponentMin) { ++ if (aExponent < ExponentMin) { + m_coefficient = 0; + m_exponent = 0; + m_formatClass = ClassZero; + return; + } + +- m_coefficient = coefficient; +- m_exponent = static_cast<int16_t>(exponent); ++ m_coefficient = aCoefficient; ++ m_exponent = static_cast<int16_t>(aExponent); + } + + bool Decimal::EncodedData::operator==(const EncodedData& another) const + { + return m_sign == another.m_sign + && m_formatClass == another.m_formatClass + && m_exponent == another.m_exponent + && m_coefficient == another.m_coefficient; + } + + Decimal::Decimal(int32_t i32) + : m_data(i32 < 0 ? Negative : Positive, 0, i32 < 0 ? static_cast<uint64_t>(-static_cast<int64_t>(i32)) : static_cast<uint64_t>(i32)) + { + } + +-Decimal::Decimal(Sign sign, int exponent, uint64_t coefficient) +- : m_data(sign, coefficient ? exponent : 0, coefficient) ++Decimal::Decimal(Sign aSign, int aExponent, uint64_t aCoefficient) ++ : m_data(aSign, aCoefficient ? aExponent : 0, aCoefficient) + { + } + + Decimal::Decimal(const EncodedData& data) + : m_data(data) + { + } + +@@ -479,32 +479,32 @@ Decimal Decimal::operator/(const Decimal + if (rhs.isZero()) + return lhs.isZero() ? nan() : infinity(resultSign); + + int resultExponent = lhs.exponent() - rhs.exponent(); + + if (lhs.isZero()) + return Decimal(resultSign, resultExponent, 0); + +- uint64_t remainder = lhs.m_data.coefficient(); ++ uint64_t lhsRemainder = lhs.m_data.coefficient(); + const uint64_t divisor = rhs.m_data.coefficient(); + uint64_t result = 0; + while (result < MaxCoefficient / 100) { +- while (remainder < divisor) { +- remainder *= 10; ++ while (lhsRemainder < divisor) { ++ lhsRemainder *= 10; + result *= 10; + --resultExponent; + } +- result += remainder / divisor; +- remainder %= divisor; +- if (!remainder) ++ result += lhsRemainder / divisor; ++ lhsRemainder %= divisor; ++ if (!lhsRemainder) + break; + } + +- if (remainder > divisor / 2) ++ if (lhsRemainder > divisor / 2) + ++result; + + return Decimal(resultSign, resultExponent, result); + } + + bool Decimal::operator==(const Decimal& rhs) const + { + if (isNaN() || rhs.isNaN()) +diff --git a/mozglue/misc/decimal/Decimal.h b/mozglue/misc/decimal/Decimal.h +--- a/mozglue/misc/decimal/Decimal.h ++++ b/mozglue/misc/decimal/Decimal.h +@@ -88,17 +88,17 @@ public: + int countDigits() const; + int exponent() const { return m_exponent; } + bool isFinite() const { return !isSpecial(); } + bool isInfinity() const { return m_formatClass == ClassInfinity; } + bool isNaN() const { return m_formatClass == ClassNaN; } + bool isSpecial() const { return m_formatClass == ClassInfinity || m_formatClass == ClassNaN; } + bool isZero() const { return m_formatClass == ClassZero; } + Sign sign() const { return m_sign; } +- void setSign(Sign sign) { m_sign = sign; } ++ void setSign(Sign aSign) { m_sign = aSign; } + + private: + enum FormatClass { + ClassInfinity, + ClassNormal, + ClassNaN, + ClassZero, + }; diff --git a/mozglue/misc/decimal/mfbt-abi-markers.patch b/mozglue/misc/decimal/mfbt-abi-markers.patch new file mode 100644 index 0000000000..1d50d3d643 --- /dev/null +++ b/mozglue/misc/decimal/mfbt-abi-markers.patch @@ -0,0 +1,150 @@ +diff --git a/mozglue/misc/decimal/Decimal.h b/mozglue/misc/decimal/Decimal.h +--- a/mozglue/misc/decimal/Decimal.h ++++ b/mozglue/misc/decimal/Decimal.h +@@ -26,16 +26,18 @@ + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + #ifndef Decimal_h + #define Decimal_h + ++#include "mozilla/Types.h" ++ + #include "platform/PlatformExport.h" + #include "wtf/Allocator.h" + #include "wtf/Assertions.h" + #include "wtf/text/WTFString.h" + #include <stdint.h> + + namespace blink { + +@@ -91,92 +93,92 @@ public: + FormatClass formatClass() const { return m_formatClass; } + + uint64_t m_coefficient; + int16_t m_exponent; + FormatClass m_formatClass; + Sign m_sign; + }; + +- Decimal(int32_t = 0); +- Decimal(Sign, int exponent, uint64_t coefficient); +- Decimal(const Decimal&); ++ MFBT_API explicit Decimal(int32_t = 0); ++ MFBT_API Decimal(Sign, int exponent, uint64_t coefficient); ++ MFBT_API Decimal(const Decimal&); + +- Decimal& operator=(const Decimal&); +- Decimal& operator+=(const Decimal&); +- Decimal& operator-=(const Decimal&); +- Decimal& operator*=(const Decimal&); +- Decimal& operator/=(const Decimal&); ++ MFBT_API Decimal& operator=(const Decimal&); ++ MFBT_API Decimal& operator+=(const Decimal&); ++ MFBT_API Decimal& operator-=(const Decimal&); ++ MFBT_API Decimal& operator*=(const Decimal&); ++ MFBT_API Decimal& operator/=(const Decimal&); + +- Decimal operator-() const; ++ MFBT_API Decimal operator-() const; + +- bool operator==(const Decimal&) const; +- bool operator!=(const Decimal&) const; +- bool operator<(const Decimal&) const; +- bool operator<=(const Decimal&) const; +- bool operator>(const Decimal&) const; +- bool operator>=(const Decimal&) const; ++ MFBT_API bool operator==(const Decimal&) const; ++ MFBT_API bool operator!=(const Decimal&) const; ++ MFBT_API bool operator<(const Decimal&) const; ++ MFBT_API bool operator<=(const Decimal&) const; ++ MFBT_API bool operator>(const Decimal&) const; ++ MFBT_API bool operator>=(const Decimal&) const; + +- Decimal operator+(const Decimal&) const; +- Decimal operator-(const Decimal&) const; +- Decimal operator*(const Decimal&) const; +- Decimal operator/(const Decimal&) const; ++ MFBT_API Decimal operator+(const Decimal&) const; ++ MFBT_API Decimal operator-(const Decimal&) const; ++ MFBT_API Decimal operator*(const Decimal&) const; ++ MFBT_API Decimal operator/(const Decimal&) const; + + int exponent() const + { + ASSERT(isFinite()); + return m_data.exponent(); + } + + bool isFinite() const { return m_data.isFinite(); } + bool isInfinity() const { return m_data.isInfinity(); } + bool isNaN() const { return m_data.isNaN(); } + bool isNegative() const { return sign() == Negative; } + bool isPositive() const { return sign() == Positive; } + bool isSpecial() const { return m_data.isSpecial(); } + bool isZero() const { return m_data.isZero(); } + +- Decimal abs() const; +- Decimal ceil() const; +- Decimal floor() const; +- Decimal remainder(const Decimal&) const; +- Decimal round() const; ++ MFBT_API Decimal abs() const; ++ MFBT_API Decimal ceil() const; ++ MFBT_API Decimal floor() const; ++ MFBT_API Decimal remainder(const Decimal&) const; ++ MFBT_API Decimal round() const; + +- double toDouble() const; ++ MFBT_API double toDouble() const; + // Note: toString method supports infinity and nan but fromString not. +- String toString() const; ++ MFBT_API String toString() const; + +- static Decimal fromDouble(double); ++ static MFBT_API Decimal fromDouble(double); + // fromString supports following syntax EBNF: + // number ::= sign? digit+ ('.' digit*) (exponent-marker sign? digit+)? + // | sign? '.' digit+ (exponent-marker sign? digit+)? + // sign ::= '+' | '-' + // exponent-marker ::= 'e' | 'E' + // digit ::= '0' | '1' | ... | '9' + // Note: fromString doesn't support "infinity" and "nan". +- static Decimal fromString(const String&); +- static Decimal infinity(Sign); +- static Decimal nan(); +- static Decimal zero(Sign); ++ static MFBT_API Decimal fromString(const String&); ++ static MFBT_API Decimal infinity(Sign); ++ static MFBT_API Decimal nan(); ++ static MFBT_API Decimal zero(Sign); + + // You should not use below methods. We expose them for unit testing. +- explicit Decimal(const EncodedData&); ++ MFBT_API explicit Decimal(const EncodedData&); + const EncodedData& value() const { return m_data; } + + private: + struct AlignedOperands { + uint64_t lhsCoefficient; + uint64_t rhsCoefficient; + int exponent; + }; + +- Decimal(double); +- Decimal compareTo(const Decimal&) const; ++ MFBT_API explicit Decimal(double); ++ MFBT_API Decimal compareTo(const Decimal&) const; + +- static AlignedOperands alignOperands(const Decimal& lhs, const Decimal& rhs); ++ static MFBT_API AlignedOperands alignOperands(const Decimal& lhs, const Decimal& rhs); + static inline Sign invertSign(Sign sign) { return sign == Negative ? Positive : Negative; } + + Sign sign() const { return m_data.sign(); } + + EncodedData m_data; + }; + + } // namespace blink diff --git a/mozglue/misc/decimal/moz-decimal-utils.h b/mozglue/misc/decimal/moz-decimal-utils.h new file mode 100644 index 0000000000..05ea61e4bd --- /dev/null +++ b/mozglue/misc/decimal/moz-decimal-utils.h @@ -0,0 +1,110 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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/. */ + +#ifndef MOZ_DECIMAL_UTILS_H +#define MOZ_DECIMAL_UTILS_H + +// This file contains extra includes, defines and typedefs to allow compilation +// of Decimal.cpp under the Mozilla source without blink core dependencies. Do +// not include it into any file other than Decimal.cpp. + +#include "double-conversion/double-conversion.h" +#include "mozilla/ArrayUtils.h" +#include "mozilla/Casting.h" +#include "mozilla/FloatingPoint.h" +#include "mozilla/Span.h" + +#include <cmath> +#include <cstring> +#include <iomanip> +#include <limits> +#include <sstream> + +#ifndef UINT64_C +// For Android toolchain +#define UINT64_C(c) (c ## ULL) +#endif + +#ifdef ASSERT +#undef ASSERT +#endif +#define ASSERT MOZ_ASSERT + +#define ASSERT_NOT_REACHED() MOZ_ASSERT_UNREACHABLE("moz-decimal-utils.h") + +#define STACK_ALLOCATED() DISALLOW_NEW() + +#define WTF_MAKE_NONCOPYABLE(ClassName) \ + private: \ + ClassName(const ClassName&) = delete; \ + void operator=(const ClassName&) = delete; + +typedef std::string String; + +double mozToDouble(mozilla::Span<const char> aStr, bool *valid) { + double_conversion::StringToDoubleConverter converter( + double_conversion::StringToDoubleConverter::NO_FLAGS, + mozilla::UnspecifiedNaN<double>(), mozilla::UnspecifiedNaN<double>(), nullptr, nullptr); + const char* str = aStr.Elements(); + int length = mozilla::AssertedCast<int>(aStr.Length()); + int processed_char_count; // unused - NO_FLAGS requires the whole string to parse + double result = converter.StringToDouble(str, length, &processed_char_count); + *valid = std::isfinite(result); + return result; +} + +double mozToDouble(const String &aStr, bool *valid) { + return mozToDouble(mozilla::MakeStringSpan(aStr.c_str()), valid); +} + +String mozToString(double aNum) { + char buffer[64]; + int buffer_length = mozilla::ArrayLength(buffer); + const double_conversion::DoubleToStringConverter& converter = + double_conversion::DoubleToStringConverter::EcmaScriptConverter(); + double_conversion::StringBuilder builder(buffer, buffer_length); + converter.ToShortest(aNum, &builder); + return String(builder.Finalize()); +} + +String mozToString(int64_t aNum) { + std::ostringstream o; + o << std::setprecision(std::numeric_limits<int64_t>::digits10) << aNum; + return o.str(); +} + +String mozToString(uint64_t aNum) { + std::ostringstream o; + o << std::setprecision(std::numeric_limits<uint64_t>::digits10) << aNum; + return o.str(); +} + +namespace moz_decimal_utils { + +class StringBuilder +{ +public: + void append(char c) { + mStr += c; + } + void appendLiteral(const char *aStr) { + mStr += aStr; + } + void appendNumber(int aNum) { + mStr += mozToString(int64_t(aNum)); + } + void append(const String& aStr) { + mStr += aStr; + } + std::string toString() const { + return mStr; + } +private: + std::string mStr; +}; + +} // namespace moz_decimal_utils + +#endif diff --git a/mozglue/misc/decimal/to-moz-dependencies.patch b/mozglue/misc/decimal/to-moz-dependencies.patch new file mode 100644 index 0000000000..bf19a6da96 --- /dev/null +++ b/mozglue/misc/decimal/to-moz-dependencies.patch @@ -0,0 +1,224 @@ +diff --git a/mozglue/misc/decimal/Decimal.cpp b/mozglue/misc/decimal/Decimal.cpp +--- a/mozglue/misc/decimal/Decimal.cpp ++++ b/mozglue/misc/decimal/Decimal.cpp +@@ -23,22 +23,20 @@ + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +-#include "platform/Decimal.h" ++#include "Decimal.h" ++#include "moz-decimal-utils.h" + +-#include "wtf/Allocator.h" +-#include "wtf/MathExtras.h" +-#include "wtf/Noncopyable.h" +-#include "wtf/text/StringBuilder.h" ++using namespace moz_decimal_utils; + + #include <algorithm> + #include <float.h> + + namespace blink { + + namespace DecimalPrivate { + +@@ -690,17 +688,17 @@ Decimal Decimal::floor() const + if (isNegative() && !isMultiplePowersOfTen(m_data.coefficient(), numberOfDropDigits)) + ++result; + return Decimal(sign(), 0, result); + } + + Decimal Decimal::fromDouble(double doubleValue) + { + if (std::isfinite(doubleValue)) +- return fromString(String::numberToStringECMAScript(doubleValue)); ++ return fromString(mozToString(doubleValue)); + + if (std::isinf(doubleValue)) + return infinity(doubleValue < 0 ? Negative : Positive); + + return nan(); + } + + Decimal Decimal::fromString(const String& str) +@@ -931,17 +929,17 @@ Decimal Decimal::round() const + result /= 10; + return Decimal(sign(), 0, result); + } + + double Decimal::toDouble() const + { + if (isFinite()) { + bool valid; +- const double doubleValue = toString().toDouble(&valid); ++ const double doubleValue = mozToDouble(toString(), &valid); + return valid ? doubleValue : std::numeric_limits<double>::quiet_NaN(); + } + + if (isInfinity()) + return isNegative() ? -std::numeric_limits<double>::infinity() : std::numeric_limits<double>::infinity(); + + return std::numeric_limits<double>::quiet_NaN(); + } +@@ -984,17 +982,17 @@ String Decimal::toString() const + ++coefficient; + + while (originalExponent < 0 && coefficient && !(coefficient % 10)) { + coefficient /= 10; + ++originalExponent; + } + } + +- const String digits = String::number(coefficient); ++ const String digits = mozToString(coefficient); + int coefficientLength = static_cast<int>(digits.length()); + const int adjustedExponent = originalExponent + coefficientLength - 1; + if (originalExponent <= 0 && adjustedExponent >= -6) { + if (!originalExponent) { + builder.append(digits); + return builder.toString(); + } + +@@ -1026,14 +1024,27 @@ String Decimal::toString() const + if (adjustedExponent) { + builder.append(adjustedExponent < 0 ? "e" : "e+"); + builder.appendNumber(adjustedExponent); + } + } + return builder.toString(); + } + ++bool Decimal::toString(char* strBuf, size_t bufLength) const ++{ ++ ASSERT(bufLength > 0); ++ String str = toString(); ++ size_t length = str.copy(strBuf, bufLength); ++ if (length < bufLength) { ++ strBuf[length] = '\0'; ++ return true; ++ } ++ strBuf[bufLength - 1] = '\0'; ++ return false; ++} ++ + Decimal Decimal::zero(Sign sign) + { + return Decimal(EncodedData(sign, EncodedData::ClassZero)); + } + + } // namespace blink +diff --git a/mozglue/misc/decimal/Decimal.h b/mozglue/misc/decimal/Decimal.h +--- a/mozglue/misc/decimal/Decimal.h ++++ b/mozglue/misc/decimal/Decimal.h +@@ -23,26 +23,49 @@ + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + ++/** ++ * Imported from: ++ * https://chromium.googlesource.com/chromium/src.git/+/master/third_party/WebKit/Source/platform/Decimal.h ++ * Check UPSTREAM-GIT-SHA for the commit ID of the last update from Blink core. ++ */ ++ + #ifndef Decimal_h + #define Decimal_h + ++#include "mozilla/Assertions.h" ++#include <stdint.h> + #include "mozilla/Types.h" + +-#include "platform/PlatformExport.h" +-#include "wtf/Allocator.h" +-#include "wtf/Assertions.h" +-#include "wtf/text/WTFString.h" +-#include <stdint.h> ++#include <string> ++ ++#ifndef ASSERT ++#define DEFINED_ASSERT_FOR_DECIMAL_H 1 ++#define ASSERT MOZ_ASSERT ++#endif ++ ++#define PLATFORM_EXPORT ++ ++// To use USING_FAST_MALLOC we'd need: ++// https://chromium.googlesource.com/chromium/src.git/+/master/third_party/WebKit/Source/wtf/Allocator.h ++// Since we don't allocate Decimal objects, no need. ++#define USING_FAST_MALLOC(type) \ ++ void ignore_this_dummy_method() = delete ++ ++#define DISALLOW_NEW() \ ++ private: \ ++ void* operator new(size_t) = delete; \ ++ void* operator new(size_t, void*) = delete; \ ++ public: + + namespace blink { + + namespace DecimalPrivate { + class SpecialValueHandler; + } + + // This class represents decimal base floating point number. +@@ -139,27 +162,28 @@ public: + MFBT_API Decimal abs() const; + MFBT_API Decimal ceil() const; + MFBT_API Decimal floor() const; + MFBT_API Decimal remainder(const Decimal&) const; + MFBT_API Decimal round() const; + + MFBT_API double toDouble() const; + // Note: toString method supports infinity and nan but fromString not. +- MFBT_API String toString() const; ++ MFBT_API std::string toString() const; ++ MFBT_API bool toString(char* strBuf, size_t bufLength) const; + + static MFBT_API Decimal fromDouble(double); + // fromString supports following syntax EBNF: + // number ::= sign? digit+ ('.' digit*) (exponent-marker sign? digit+)? + // | sign? '.' digit+ (exponent-marker sign? digit+)? + // sign ::= '+' | '-' + // exponent-marker ::= 'e' | 'E' + // digit ::= '0' | '1' | ... | '9' + // Note: fromString doesn't support "infinity" and "nan". +- static MFBT_API Decimal fromString(const String&); ++ static MFBT_API Decimal fromString(const std::string& aValue); + static MFBT_API Decimal infinity(Sign); + static MFBT_API Decimal nan(); + static MFBT_API Decimal zero(Sign); + + // You should not use below methods. We expose them for unit testing. + MFBT_API explicit Decimal(const EncodedData&); + const EncodedData& value() const { return m_data; } + +@@ -178,9 +202,20 @@ private: + + Sign sign() const { return m_data.sign(); } + + EncodedData m_data; + }; + + } // namespace blink + ++namespace mozilla { ++typedef blink::Decimal Decimal; ++} // namespace mozilla ++ ++#undef USING_FAST_MALLOC ++ ++#ifdef DEFINED_ASSERT_FOR_DECIMAL_H ++#undef DEFINED_ASSERT_FOR_DECIMAL_H ++#undef ASSERT ++#endif ++ + #endif // Decimal_h diff --git a/mozglue/misc/decimal/update.sh b/mozglue/misc/decimal/update.sh new file mode 100755 index 0000000000..23748ebe2c --- /dev/null +++ b/mozglue/misc/decimal/update.sh @@ -0,0 +1,60 @@ +# Usage: ./update.sh [blink-core-source-directory] +# +# Copies the needed files from a directory containing the original +# Decimal.h and Decimal.cpp source that we need. +# If [blink-core-source-directory] is not specified, this script will +# attempt to download the latest versions using git. + +set -e + +FILES=( + "Decimal.h" + "Decimal.cpp" +) + +OWN_NAME=`basename $0` + +if [ $# -gt 1 ]; then + echo "$OWN_NAME: Too many arguments">&2 + exit 1 +fi + +if [ $# -eq 1 ]; then + BLINK_CORE_DIR="$1" + for F in "${FILES[@]}" + do + P="$BLINK_CORE_DIR/$F" + if [ ! -f "$P" ]; then + echo "$OWN_NAME: Couldn't find file: $P">&2 + exit 1 + fi + done + for F in "${FILES[@]}" + do + P="$BLINK_CORE_DIR/$F" + cp "$P" . + done +else + #LATEST_SHA=$(cat UPSTREAM-GIT-SHA) + LATEST_SHA=$(git ls-remote https://chromium.googlesource.com/chromium/src.git/ | awk "/refs\/heads\/master/ {print \$1}") + REPO_PATH="https://chromium.googlesource.com/chromium/src.git/+/$LATEST_SHA/third_party/WebKit/Source/platform" + #REPO_PATH="https://github.com/WebKit/webkit/tree/master/Source/WebCore/platform" + for F in "${FILES[@]}" + do + printf "Downloading `basename $F`..." + curl "$REPO_PATH/${F}?format=TEXT" | base64 -D > "$F" + echo done. + done + echo $LATEST_SHA > UPSTREAM-GIT-SHA +fi + +# Apply patches: + +patch -p4 < zero-serialization.patch +patch -p4 < comparison-with-nan.patch +patch -p4 < mfbt-abi-markers.patch +patch -p4 < to-moz-dependencies.patch +patch -p4 < add-doubleconversion-impl.patch +# The following is disabled. See +# https://bugzilla.mozilla.org/show_bug.cgi?id=1208357#c7 +#patch -p4 < fix-wshadow-warnings.patch diff --git a/mozglue/misc/decimal/zero-serialization.patch b/mozglue/misc/decimal/zero-serialization.patch new file mode 100644 index 0000000000..b8de9241bd --- /dev/null +++ b/mozglue/misc/decimal/zero-serialization.patch @@ -0,0 +1,22 @@ +diff --git a/mozglue/misc/decimal/Decimal.cpp b/mozglue/misc/decimal/Decimal.cpp +--- a/mozglue/misc/decimal/Decimal.cpp ++++ b/mozglue/misc/decimal/Decimal.cpp +@@ -277,17 +277,17 @@ bool Decimal::EncodedData::operator==(co + } + + Decimal::Decimal(int32_t i32) + : m_data(i32 < 0 ? Negative : Positive, 0, i32 < 0 ? static_cast<uint64_t>(-static_cast<int64_t>(i32)) : static_cast<uint64_t>(i32)) + { + } + + Decimal::Decimal(Sign sign, int exponent, uint64_t coefficient) +- : m_data(sign, exponent, coefficient) ++ : m_data(sign, coefficient ? exponent : 0, coefficient) + { + } + + Decimal::Decimal(const EncodedData& data) + : m_data(data) + { + } + diff --git a/mozglue/misc/moz.build b/mozglue/misc/moz.build new file mode 100644 index 0000000000..2e6669c2bf --- /dev/null +++ b/mozglue/misc/moz.build @@ -0,0 +1,156 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# 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/. + +FINAL_LIBRARY = "mozglue" + +EXPORTS.mozilla += [ + "AutoProfilerLabel.h", + "AwakeTimeStamp.h", + "decimal/Decimal.h", + "decimal/DoubleConversion.h", + "IntegerPrintfMacros.h", + "MmapFaultHandler.h", + "PlatformConditionVariable.h", + "PlatformMutex.h", + "PlatformRWLock.h", + "Printf.h", + "SIMD.h", + "Sprintf.h", + "SSE.h", + "StackWalk.h", + "TimeStamp.h", + "Uptime.h", +] + +EXPORTS.mozilla.glue += [ + "Debug.h", + "WinUtils.h", +] + +if CONFIG["OS_ARCH"] == "WINNT": + EXPORTS.mozilla += [ + "GetKnownFolderPath.h", + "PreXULSkeletonUI.h", + "StackWalk_windows.h", + "StackWalkThread.h", + "TimeStamp_windows.h", + "WindowsDpiAwareness.h", + ] + +SOURCES += [ + "AutoProfilerLabel.cpp", + "AwakeTimeStamp.cpp", + "MmapFaultHandler.cpp", + "Printf.cpp", + "SIMD.cpp", + "StackWalk.cpp", + "TimeStamp.cpp", + "Uptime.cpp", +] + +if CONFIG["CPU_ARCH"].startswith("x86"): + SOURCES += [ + "SIMD_avx2.cpp", + "SSE.cpp", + ] + SOURCES["SIMD_avx2.cpp"].flags += ["-mavx2"] + +if not CONFIG["JS_STANDALONE"]: + EXPORTS.mozilla += [ + "ProcessType.h", + "RuntimeExceptionModule.h", + ] + + SOURCES += [ + "ProcessType.cpp", + "RuntimeExceptionModule.cpp", + ] + +OS_LIBS += CONFIG["REALTIME_LIBS"] + +if CONFIG["OS_ARCH"] == "WINNT": + EXPORTS.mozilla += [ + "DynamicallyLinkedFunctionPtr.h", + "ImportDir.h", + "NativeNt.h", + "WindowsDpiInitialization.h", + "WindowsEnumProcessModules.h", + "WindowsMapRemoteView.h", + "WindowsProcessMitigations.h", + "WindowsStackCookie.h", + "WindowsUnwindInfo.h", + ] + EXPORTS.mozilla.glue += [ + "WindowsUnicode.h", + ] + SOURCES += [ + "GetKnownFolderPath.cpp", + "TimeStamp_windows.cpp", + "WindowsDllMain.cpp", + "WindowsDpiInitialization.cpp", + "WindowsMapRemoteView.cpp", + "WindowsProcessMitigations.cpp", + "WindowsUnicode.cpp", + ] + + OS_LIBS += [ + "dbghelp", + "oleaut32", + "ole32", + ] + + if not CONFIG["JS_STANDALONE"]: + SOURCES += [ + "/ipc/mscom/COMWrappers.cpp", + "/ipc/mscom/ProcessRuntime.cpp", + "PreXULSkeletonUI.cpp", + ] + +elif CONFIG["HAVE_CLOCK_MONOTONIC"]: + SOURCES += [ + "TimeStamp_posix.cpp", + ] +elif CONFIG["OS_ARCH"] == "Darwin": + SOURCES += [ + "TimeStamp_darwin.cpp", + ] +elif CONFIG["COMPILE_ENVIRONMENT"]: + error("No TimeStamp implementation on this platform. Build will not succeed") + +if CONFIG["OS_ARCH"] == "WINNT": + SOURCES += [ + "ConditionVariable_windows.cpp", + "Mutex_windows.cpp", + "RWLock_windows.cpp", + ] +# WASI hasn't supported cond vars and mutexes yet so noop implementation is used. +elif CONFIG["OS_ARCH"] == "WASI": + SOURCES += [ + "ConditionVariable_noop.cpp", + "Mutex_noop.cpp", + ] +else: + SOURCES += [ + "ConditionVariable_posix.cpp", + "Mutex_posix.cpp", + "RWLock_posix.cpp", + ] + +if CONFIG["MOZ_LINKER"] and CONFIG["MOZ_WIDGET_TOOLKIT"] == "android": + LOCAL_INCLUDES += [ + "/mozglue/linker", + ] + +SOURCES += [ + "decimal/Decimal.cpp", +] + +if CONFIG["CC_TYPE"] in ("clang", "clang-cl"): + # Suppress warnings from third-party V8 Decimal code. + SOURCES["decimal/Decimal.cpp"].flags += ["-Wno-implicit-fallthrough"] + +for var in ("MOZ_APP_BASENAME", "MOZ_APP_VENDOR"): + DEFINES[var] = '"%s"' % CONFIG[var] diff --git a/mozglue/moz.build b/mozglue/moz.build new file mode 100644 index 0000000000..7a85aa922f --- /dev/null +++ b/mozglue/moz.build @@ -0,0 +1,26 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# 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/. + +with Files("**"): + BUG_COMPONENT = ("Core", "mozglue") + +if CONFIG["MOZ_LINKER"] or CONFIG["MOZ_WIDGET_TOOLKIT"] == "android": + DIRS += ["linker"] + +if CONFIG["MOZ_WIDGET_TOOLKIT"] == "android": + DIRS += ["android"] + +if CONFIG["OS_ARCH"] == "Linux" and not CONFIG["FUZZING_SNAPSHOT"]: + DIRS += ["interposers"] + +DIRS += [ + "baseprofiler", + "build", + "misc", +] + +if CONFIG["MOZ_WIDGET_TOOLKIT"]: + TEST_DIRS += ["tests"] diff --git a/mozglue/static/README b/mozglue/static/README new file mode 100644 index 0000000000..e52c716166 --- /dev/null +++ b/mozglue/static/README @@ -0,0 +1,2 @@ +mozglue/static contains parts of the mozglue library that can/should be +statically linked to e.g. js/Gecko. diff --git a/mozglue/static/rust/Cargo.toml b/mozglue/static/rust/Cargo.toml new file mode 100644 index 0000000000..d0307a5bda --- /dev/null +++ b/mozglue/static/rust/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "mozglue-static" +version = "0.1.0" +edition = "2018" +license = "MPL-2.0" + +[lib] +path = "lib.rs" + +[dependencies] +arrayvec = "0.7" + +[build-dependencies] +mozbuild = "0.1" +cc = "1" +rustc_version = "0.4" + +[features] +moz_memory = [] diff --git a/mozglue/static/rust/build.rs b/mozglue/static/rust/build.rs new file mode 100644 index 0000000000..4df483da0a --- /dev/null +++ b/mozglue/static/rust/build.rs @@ -0,0 +1,32 @@ +/* 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/. */ + +use rustc_version::{version, Version}; + +fn main() { + let mut build = cc::Build::new(); + build.cpp(true); + // For js-confdefs.h, see wrappers.cpp. + build.include(mozbuild::TOPOBJDIR.join("js").join("src")); + build.include(mozbuild::TOPOBJDIR.join("dist").join("include")); + build.define("MOZ_HAS_MOZGLUE", None); + build.file("wrappers.cpp"); + build.compile("wrappers"); + println!("cargo:rerun-if-changed=wrappers.cpp"); + + let ver = version().unwrap(); + let max_oom_hook_version = Version::parse("1.71.0-alpha").unwrap(); + // The new alloc error panic feature was temporarily reverted. We kept the + // code in tree, but the version here is such that it's effectively never used. + let max_alloc_error_panic_version = Version::parse("1.71.0-alpha").unwrap(); + + if ver < max_oom_hook_version { + println!("cargo:rustc-cfg=feature=\"oom_with_hook\""); + } else if ver < max_alloc_error_panic_version { + println!("cargo:rustc-cfg=feature=\"oom_with_alloc_error_panic\""); + } else if std::env::var("MOZ_AUTOMATION").is_ok() { + panic!("Builds on automation must use a version of rust for which we know how to hook OOM: want < {}, have {}", + max_alloc_error_panic_version, ver); + } +} diff --git a/mozglue/static/rust/lib.rs b/mozglue/static/rust/lib.rs new file mode 100644 index 0000000000..001b920766 --- /dev/null +++ b/mozglue/static/rust/lib.rs @@ -0,0 +1,221 @@ +/* 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/. */ + +#![cfg_attr(feature = "oom_with_hook", feature(alloc_error_hook))] +#![cfg_attr(feature = "oom_with_alloc_error_panic", feature(panic_oom_payload))] + +use arrayvec::ArrayString; +use std::cmp; +use std::ops::Deref; +use std::os::raw::c_char; +use std::os::raw::c_int; +use std::panic; + +#[link(name = "wrappers")] +extern "C" { + // We can't use MOZ_Crash directly because it may be weakly linked + // and rust can't handle that. + fn RustMozCrash(filename: *const c_char, line: c_int, reason: *const c_char) -> !; +} + +/// Truncate a string at the closest unicode character boundary +/// ``` +/// assert_eq!(str_truncate_valid("éà ", 3), "é"); +/// assert_eq!(str_truncate_valid("éà ", 4), "éè"); +/// ``` +fn str_truncate_valid(s: &str, mut mid: usize) -> &str { + loop { + if let Some(res) = s.get(..mid) { + return res; + } + mid -= 1; + } +} + +/// Similar to ArrayString, but with terminating nul character. +#[derive(Debug, PartialEq)] +struct ArrayCString<const CAP: usize> { + inner: ArrayString<CAP>, +} + +impl<S: AsRef<str>, const CAP: usize> From<S> for ArrayCString<CAP> { + /// Contrary to ArrayString::from, truncates at the closest unicode + /// character boundary. + /// ``` + /// assert_eq!(ArrayCString::<4>::from("éà "), + /// ArrayCString::<4>::from("é")); + /// assert_eq!(&*ArrayCString::<4>::from("éà "), "é\0"); + /// ``` + fn from(s: S) -> Self { + let s = s.as_ref(); + let len = cmp::min(s.len(), CAP - 1); + let mut result = Self { + inner: ArrayString::from(str_truncate_valid(s, len)).unwrap(), + }; + result.inner.push('\0'); + result + } +} + +impl<const CAP: usize> Deref for ArrayCString<CAP> { + type Target = str; + + fn deref(&self) -> &str { + self.inner.as_str() + } +} + +fn panic_hook(info: &panic::PanicInfo) { + // Try to handle &str/String payloads, which should handle 99% of cases. + let payload = info.payload(); + let message = if let Some(layout) = oom_hook::oom_layout(payload) { + unsafe { + oom_hook::RustHandleOOM(layout.size()); + } + } else if let Some(s) = payload.downcast_ref::<&str>() { + s + } else if let Some(s) = payload.downcast_ref::<String>() { + s.as_str() + } else { + // Not the most helpful thing, but seems unlikely to happen + // in practice. + "Unhandled rust panic payload!" + }; + let (filename, line) = if let Some(loc) = info.location() { + (loc.file(), loc.line()) + } else { + ("unknown.rs", 0) + }; + // Copy the message and filename to the stack in order to safely add + // a terminating nul character (since rust strings don't come with one + // and RustMozCrash wants one). + let message = ArrayCString::<512>::from(message); + let filename = ArrayCString::<512>::from(filename); + unsafe { + RustMozCrash( + filename.as_ptr() as *const c_char, + line as c_int, + message.as_ptr() as *const c_char, + ); + } +} + +/// Configure a panic hook to redirect rust panics to MFBT's MOZ_Crash. +#[no_mangle] +pub extern "C" fn install_rust_hooks() { + panic::set_hook(Box::new(panic_hook)); + #[cfg(feature = "oom_with_hook")] + use std::alloc::set_alloc_error_hook; + #[cfg(feature = "oom_with_hook")] + set_alloc_error_hook(oom_hook::hook); +} + +mod oom_hook { + #[cfg(feature = "oom_with_alloc_error_panic")] + use std::alloc::AllocErrorPanicPayload; + use std::alloc::Layout; + use std::any::Any; + + #[inline(always)] + pub fn oom_layout(_payload: &dyn Any) -> Option<Layout> { + #[cfg(feature = "oom_with_alloc_error_panic")] + return _payload + .downcast_ref::<AllocErrorPanicPayload>() + .map(|p| p.layout()); + #[cfg(not(feature = "oom_with_alloc_error_panic"))] + return None; + } + + extern "C" { + pub fn RustHandleOOM(size: usize) -> !; + } + + #[cfg(feature = "oom_with_hook")] + pub fn hook(layout: Layout) { + unsafe { + RustHandleOOM(layout.size()); + } + } +} + +#[cfg(feature = "moz_memory")] +mod moz_memory { + use std::alloc::{GlobalAlloc, Layout}; + use std::os::raw::c_void; + + extern "C" { + fn malloc(size: usize) -> *mut c_void; + + fn free(ptr: *mut c_void); + + fn calloc(nmemb: usize, size: usize) -> *mut c_void; + + fn realloc(ptr: *mut c_void, size: usize) -> *mut c_void; + + #[cfg(windows)] + fn _aligned_malloc(size: usize, align: usize) -> *mut c_void; + + #[cfg(not(windows))] + fn memalign(align: usize, size: usize) -> *mut c_void; + } + + #[cfg(windows)] + unsafe fn memalign(align: usize, size: usize) -> *mut c_void { + _aligned_malloc(size, align) + } + + pub struct GeckoAlloc; + + #[inline(always)] + fn need_memalign(layout: Layout) -> bool { + // mozjemalloc guarantees a minimum alignment of 16 for all sizes, except + // for size classes below 16 (4 and 8). + layout.align() > layout.size() || layout.align() > 16 + } + + unsafe impl GlobalAlloc for GeckoAlloc { + unsafe fn alloc(&self, layout: Layout) -> *mut u8 { + if need_memalign(layout) { + memalign(layout.align(), layout.size()) as *mut u8 + } else { + malloc(layout.size()) as *mut u8 + } + } + + unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) { + free(ptr as *mut c_void) + } + + unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 { + if need_memalign(layout) { + let ptr = self.alloc(layout); + if !ptr.is_null() { + std::ptr::write_bytes(ptr, 0, layout.size()); + } + ptr + } else { + calloc(1, layout.size()) as *mut u8 + } + } + + unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 { + let new_layout = Layout::from_size_align_unchecked(new_size, layout.align()); + if need_memalign(new_layout) { + let new_ptr = self.alloc(new_layout); + if !new_ptr.is_null() { + let size = std::cmp::min(layout.size(), new_size); + std::ptr::copy_nonoverlapping(ptr, new_ptr, size); + self.dealloc(ptr, layout); + } + new_ptr + } else { + realloc(ptr as *mut c_void, new_size) as *mut u8 + } + } + } +} + +#[cfg(feature = "moz_memory")] +#[global_allocator] +static A: moz_memory::GeckoAlloc = moz_memory::GeckoAlloc; diff --git a/mozglue/static/rust/moz_asserts/Cargo.toml b/mozglue/static/rust/moz_asserts/Cargo.toml new file mode 100644 index 0000000000..27e336f7c7 --- /dev/null +++ b/mozglue/static/rust/moz_asserts/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "moz_asserts" +version = "0.1.0" +edition = "2018" +license = "MPL-2.0" + +[lib] +path = "lib.rs" + +[dependencies] +mozbuild = "0.1" diff --git a/mozglue/static/rust/moz_asserts/lib.rs b/mozglue/static/rust/moz_asserts/lib.rs new file mode 100644 index 0000000000..f4b00a5ad2 --- /dev/null +++ b/mozglue/static/rust/moz_asserts/lib.rs @@ -0,0 +1,103 @@ +/* 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/. */ + +// Implementation note: It seems tempting to use if cfg!(feature = ...) directly +// in the macro, but that doesn't work, as the feature would be evaluated +// against the dependent crate. +// +// It'd also seem tempting to export different macros depending on the features +// enabled, but that's also not great because it causes warnings when the +// features are enabled, e.g.: +// +// nightly_panic!("foo"); +// return SAFE_VALUE; +// +// would cause an unreachable code warning for Nightly builds. So instead we +// choose an exported constant to guard the condition. (For reference, this +// is also how rust's `debug_assert!` is implemented) + +/// Whether Nightly-only assertions are enabled. +pub use mozbuild::config::NIGHTLY_BUILD; + +/// Whether diagnostic assertions are enabled. +pub use mozbuild::config::MOZ_DIAGNOSTIC_ASSERT_ENABLED; + +/// assert! on Nightly, gets compiled out otherwise. +#[macro_export] +macro_rules! nightly_assert { + ($($arg:tt)*) => (if $crate::NIGHTLY_BUILD { assert!($($arg)*); }) +} + +/// assert_eq! on Nightly, gets compiled out otherwise. +#[macro_export] +macro_rules! nightly_assert_eq { + ($($arg:tt)*) => (if $crate::NIGHTLY_BUILD { assert_eq!($($arg)*); }) +} + +/// assert_ne! on Nightly, gets compiled out otherwise. +#[macro_export] +macro_rules! nightly_assert_ne { + ($($arg:tt)*) => (if $crate::NIGHTLY_BUILD { assert_ne!($($arg)*); }) +} + +/// panic! on Nightly, gets compiled out otherwise. +#[macro_export] +macro_rules! nightly_panic { + ($($arg:tt)*) => (if $crate::NIGHTLY_BUILD { panic!($($arg)*); }) +} + +/// unreachable! on Nightly, `std::hint::unreachable_unchecked()` otherwise. +/// +/// Use carefully! Consider using nightly_panic! and handling the failure +/// gracefully if you can't prove it's really unreachable. +/// +/// See https://doc.rust-lang.org/std/hint/fn.unreachable_unchecked.html#safety +/// for safety details. +#[macro_export] +macro_rules! nightly_unreachable { + ($($arg:tt)*) => { + if $crate::NIGHTLY_BUILD { + unreachable!($($arg)*); + } else { + std::hint::unreachable_unchecked() + } + } +} + +/// assert! when diagnostic asserts are enabled, gets compiled out otherwise. +#[macro_export] +macro_rules! diagnostic_assert { + ($($arg:tt)*) => (if $crate::MOZ_DIAGNOSTIC_ASSERT_ENABLED { assert!($($arg)*); }) +} + +/// assert_eq! when diagnostic asserts are enabled, gets compiled out otherwise. +#[macro_export] +macro_rules! diagnostic_assert_eq { + ($($arg:tt)*) => (if $crate::MOZ_DIAGNOSTIC_ASSERT_ENABLED { assert_eq!($($arg)*); }) +} + +/// assert_ne! when diagnostic asserts are enabled, gets compiled out otherwise. +#[macro_export] +macro_rules! diagnostic_assert_ne { + ($($arg:tt)*) => (if $crate::MOZ_DIAGNOSTIC_ASSERT_ENABLED { assert_ne!($($arg)*); }) +} + +/// unreachable! when diagnostic asserts are enabled, +/// `std::hint::unreachable_unchecked()` otherwise. +/// +/// Use carefully! Consider using diagnostic_panic! and handling the failure +/// gracefully if you can't prove it's really unreachable. +/// +/// See https://doc.rust-lang.org/std/hint/fn.unreachable_unchecked.html#safety +/// for safety details. +#[macro_export] +macro_rules! diagnostic_unreachable { + ($($arg:tt)*) => { + if $crate::MOZ_DIAGNOSTIC_ASSERT_ENABLED { + unreachable!($($arg)*); + } else { + std::hint::unreachable_unchecked() + } + } +} diff --git a/mozglue/static/rust/wrappers.cpp b/mozglue/static/rust/wrappers.cpp new file mode 100644 index 0000000000..926593f871 --- /dev/null +++ b/mozglue/static/rust/wrappers.cpp @@ -0,0 +1,23 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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/. */ + +// This ensures the right configuration for e.g. MOZ_GLUE_IN_PROGRAM, +// used in the MFBT headers included further below. We use js-confdefs.h +// instead of mozilla-config.h because the latter is not present in +// spidermonkey standalone builds while the former is always present. +#include "js-confdefs.h" +#include "mozilla/Assertions.h" +#include "mozilla/Types.h" +#include "mozilla/mozalloc_oom.h" + +// MOZ_Crash wrapper for use by rust, since MOZ_Crash is an inline function. +extern "C" void RustMozCrash(const char* aFilename, int aLine, + const char* aReason) { + MOZ_Crash(aFilename, aLine, aReason); +} + +// mozalloc_handle_oom wrapper for use by rust, because mozalloc_handle_oom is +// MFBT_API, that rust can't respect. +extern "C" void RustHandleOOM(size_t size) { mozalloc_handle_oom(size); } diff --git a/mozglue/tests/ShowSSEConfig.cpp b/mozglue/tests/ShowSSEConfig.cpp new file mode 100644 index 0000000000..a19b30198c --- /dev/null +++ b/mozglue/tests/ShowSSEConfig.cpp @@ -0,0 +1,125 @@ +/* vim: set shiftwidth=2 tabstop=8 autoindent cindent expandtab: */ +/* 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 "mozilla/SSE.h" +#include <stdio.h> + +#if defined(XP_WIN) +int wmain() +#else +int main() +#endif // defined(XP_WIN) +{ + printf("CPUID detection present: %s\n", +#ifdef MOZILLA_SSE_HAVE_CPUID_DETECTION + "yes" +#else + "no" +#endif + ); + +#ifdef MOZILLA_COMPILE_WITH_MMX +# define COMPILE_MMX_STRING "Y" +#else +# define COMPILE_MMX_STRING "-" +#endif +#ifdef MOZILLA_PRESUME_MMX +# define PRESUME_MMX_STRING "Y" +#else +# define PRESUME_MMX_STRING "-" +#endif + +#ifdef MOZILLA_COMPILE_WITH_SSE +# define COMPILE_SSE_STRING "Y" +#else +# define COMPILE_SSE_STRING "-" +#endif +#ifdef MOZILLA_PRESUME_SSE +# define PRESUME_SSE_STRING "Y" +#else +# define PRESUME_SSE_STRING "-" +#endif + +#ifdef MOZILLA_COMPILE_WITH_SSE2 +# define COMPILE_SSE2_STRING "Y" +#else +# define COMPILE_SSE2_STRING "-" +#endif +#ifdef MOZILLA_PRESUME_SSE2 +# define PRESUME_SSE2_STRING "Y" +#else +# define PRESUME_SSE2_STRING "-" +#endif + +#ifdef MOZILLA_COMPILE_WITH_SSE3 +# define COMPILE_SSE3_STRING "Y" +#else +# define COMPILE_SSE3_STRING "-" +#endif +#ifdef MOZILLA_PRESUME_SSE3 +# define PRESUME_SSE3_STRING "Y" +#else +# define PRESUME_SSE3_STRING "-" +#endif + +#ifdef MOZILLA_COMPILE_WITH_SSSE3 +# define COMPILE_SSSE3_STRING "Y" +#else +# define COMPILE_SSSE3_STRING "-" +#endif +#ifdef MOZILLA_PRESUME_SSSE3 +# define PRESUME_SSSE3_STRING "Y" +#else +# define PRESUME_SSSE3_STRING "-" +#endif + +#ifdef MOZILLA_COMPILE_WITH_SSE4A +# define COMPILE_SSE4A_STRING "Y" +#else +# define COMPILE_SSE4A_STRING "-" +#endif +#ifdef MOZILLA_PRESUME_SSE4A +# define PRESUME_SSE4A_STRING "Y" +#else +# define PRESUME_SSE4A_STRING "-" +#endif + +#ifdef MOZILLA_COMPILE_WITH_SSE4_1 +# define COMPILE_SSE4_1_STRING "Y" +#else +# define COMPILE_SSE4_1_STRING "-" +#endif +#ifdef MOZILLA_PRESUME_SSE4_1 +# define PRESUME_SSE4_1_STRING "Y" +#else +# define PRESUME_SSE4_1_STRING "-" +#endif + +#ifdef MOZILLA_COMPILE_WITH_SSE4_2 +# define COMPILE_SSE4_2_STRING "Y" +#else +# define COMPILE_SSE4_2_STRING "-" +#endif +#ifdef MOZILLA_PRESUME_SSE4_2 +# define PRESUME_SSE4_2_STRING "Y" +#else +# define PRESUME_SSE4_2_STRING "-" +#endif + + printf("Feature Presume Compile Support Use\n"); +#define SHOW_INFO(featurelc_, featureuc_) \ + printf("%7s %1s %1s %1s\n", #featurelc_, \ + PRESUME_##featureuc_##_STRING, COMPILE_##featureuc_##_STRING, \ + (mozilla::supports_##featurelc_() ? "Y" : "-")); + SHOW_INFO(mmx, MMX) + SHOW_INFO(sse, SSE) + SHOW_INFO(sse2, SSE2) + SHOW_INFO(sse3, SSE3) + SHOW_INFO(ssse3, SSSE3) + SHOW_INFO(sse4a, SSE4A) + SHOW_INFO(sse4_1, SSE4_1) + SHOW_INFO(sse4_2, SSE4_2) + return 0; +} diff --git a/mozglue/tests/TestBaseProfiler.cpp b/mozglue/tests/TestBaseProfiler.cpp new file mode 100644 index 0000000000..03b2076ebc --- /dev/null +++ b/mozglue/tests/TestBaseProfiler.cpp @@ -0,0 +1,5732 @@ +/* -*- 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 "BaseProfiler.h" + +#include "mozilla/Attributes.h" +#include "mozilla/BaseAndGeckoProfilerDetail.h" +#include "mozilla/BaseProfileJSONWriter.h" +#include "mozilla/BaseProfilerDetail.h" +#include "mozilla/FailureLatch.h" +#include "mozilla/FloatingPoint.h" +#include "mozilla/NotNull.h" +#include "mozilla/ProgressLogger.h" +#include "mozilla/ProportionValue.h" + +#ifdef MOZ_GECKO_PROFILER +# include "mozilla/BaseProfilerMarkerTypes.h" +# include "mozilla/BlocksRingBuffer.h" +# include "mozilla/leb128iterator.h" +# include "mozilla/ModuloBuffer.h" +# include "mozilla/mozalloc.h" +# include "mozilla/PowerOfTwo.h" +# include "mozilla/ProfileBufferChunk.h" +# include "mozilla/ProfileBufferChunkManagerSingle.h" +# include "mozilla/ProfileBufferChunkManagerWithLocalLimit.h" +# include "mozilla/ProfileBufferControlledChunkManager.h" +# include "mozilla/ProfileChunkedBuffer.h" +# include "mozilla/Vector.h" +#endif // MOZ_GECKO_PROFILER + +#if defined(_MSC_VER) || defined(__MINGW32__) +# include <windows.h> +# include <mmsystem.h> +# include <process.h> +#else +# include <errno.h> +# include <time.h> +#endif + +#include <algorithm> +#include <atomic> +#include <iostream> +#include <random> +#include <thread> +#include <type_traits> +#include <utility> + +void TestFailureLatch() { + printf("TestFailureLatch...\n"); + + // Test infallible latch. + { + mozilla::FailureLatchInfallibleSource& infallibleLatch = + mozilla::FailureLatchInfallibleSource::Singleton(); + + MOZ_RELEASE_ASSERT(!infallibleLatch.Fallible()); + MOZ_RELEASE_ASSERT(!infallibleLatch.Failed()); + MOZ_RELEASE_ASSERT(!infallibleLatch.GetFailure()); + MOZ_RELEASE_ASSERT(&infallibleLatch.SourceFailureLatch() == + &mozilla::FailureLatchInfallibleSource::Singleton()); + MOZ_RELEASE_ASSERT(&std::as_const(infallibleLatch).SourceFailureLatch() == + &mozilla::FailureLatchInfallibleSource::Singleton()); + } + + // Test failure latch basic functions. + { + mozilla::FailureLatchSource failureLatch; + + MOZ_RELEASE_ASSERT(failureLatch.Fallible()); + MOZ_RELEASE_ASSERT(!failureLatch.Failed()); + MOZ_RELEASE_ASSERT(!failureLatch.GetFailure()); + MOZ_RELEASE_ASSERT(&failureLatch.SourceFailureLatch() == &failureLatch); + MOZ_RELEASE_ASSERT(&std::as_const(failureLatch).SourceFailureLatch() == + &failureLatch); + + failureLatch.SetFailure("error"); + + MOZ_RELEASE_ASSERT(failureLatch.Fallible()); + MOZ_RELEASE_ASSERT(failureLatch.Failed()); + MOZ_RELEASE_ASSERT(failureLatch.GetFailure()); + MOZ_RELEASE_ASSERT(strcmp(failureLatch.GetFailure(), "error") == 0); + + failureLatch.SetFailure("later error"); + + MOZ_RELEASE_ASSERT(failureLatch.Fallible()); + MOZ_RELEASE_ASSERT(failureLatch.Failed()); + MOZ_RELEASE_ASSERT(failureLatch.GetFailure()); + MOZ_RELEASE_ASSERT(strcmp(failureLatch.GetFailure(), "error") == 0); + } + + // Test SetFailureFrom. + { + mozilla::FailureLatchSource failureLatch; + + MOZ_RELEASE_ASSERT(!failureLatch.Failed()); + failureLatch.SetFailureFrom(failureLatch); + MOZ_RELEASE_ASSERT(!failureLatch.Failed()); + MOZ_RELEASE_ASSERT(!failureLatch.GetFailure()); + + // SetFailureFrom with no error. + { + mozilla::FailureLatchSource failureLatchInnerOk; + MOZ_RELEASE_ASSERT(!failureLatchInnerOk.Failed()); + MOZ_RELEASE_ASSERT(!failureLatchInnerOk.GetFailure()); + + MOZ_RELEASE_ASSERT(!failureLatch.Failed()); + failureLatch.SetFailureFrom(failureLatchInnerOk); + MOZ_RELEASE_ASSERT(!failureLatch.Failed()); + + MOZ_RELEASE_ASSERT(!failureLatchInnerOk.Failed()); + MOZ_RELEASE_ASSERT(!failureLatchInnerOk.GetFailure()); + } + MOZ_RELEASE_ASSERT(!failureLatch.Failed()); + MOZ_RELEASE_ASSERT(!failureLatch.GetFailure()); + + // SetFailureFrom with error. + { + mozilla::FailureLatchSource failureLatchInnerError; + MOZ_RELEASE_ASSERT(!failureLatchInnerError.Failed()); + MOZ_RELEASE_ASSERT(!failureLatchInnerError.GetFailure()); + + failureLatchInnerError.SetFailure("inner error"); + MOZ_RELEASE_ASSERT(failureLatchInnerError.Failed()); + MOZ_RELEASE_ASSERT( + strcmp(failureLatchInnerError.GetFailure(), "inner error") == 0); + + MOZ_RELEASE_ASSERT(!failureLatch.Failed()); + failureLatch.SetFailureFrom(failureLatchInnerError); + MOZ_RELEASE_ASSERT(failureLatch.Failed()); + + MOZ_RELEASE_ASSERT(failureLatchInnerError.Failed()); + MOZ_RELEASE_ASSERT( + strcmp(failureLatchInnerError.GetFailure(), "inner error") == 0); + } + MOZ_RELEASE_ASSERT(failureLatch.Failed()); + MOZ_RELEASE_ASSERT(strcmp(failureLatch.GetFailure(), "inner error") == 0); + + failureLatch.SetFailureFrom(failureLatch); + MOZ_RELEASE_ASSERT(failureLatch.Failed()); + MOZ_RELEASE_ASSERT(strcmp(failureLatch.GetFailure(), "inner error") == 0); + + // SetFailureFrom with error again, ignored. + { + mozilla::FailureLatchSource failureLatchInnerError; + failureLatchInnerError.SetFailure("later inner error"); + MOZ_RELEASE_ASSERT(failureLatchInnerError.Failed()); + MOZ_RELEASE_ASSERT(strcmp(failureLatchInnerError.GetFailure(), + "later inner error") == 0); + + MOZ_RELEASE_ASSERT(failureLatch.Failed()); + failureLatch.SetFailureFrom(failureLatchInnerError); + MOZ_RELEASE_ASSERT(failureLatch.Failed()); + + MOZ_RELEASE_ASSERT(failureLatchInnerError.Failed()); + MOZ_RELEASE_ASSERT(strcmp(failureLatchInnerError.GetFailure(), + "later inner error") == 0); + } + MOZ_RELEASE_ASSERT(failureLatch.Failed()); + MOZ_RELEASE_ASSERT(strcmp(failureLatch.GetFailure(), "inner error") == 0); + } + + // Test FAILURELATCH_IMPL_PROXY + { + class Proxy final : public mozilla::FailureLatch { + public: + explicit Proxy(mozilla::FailureLatch& aFailureLatch) + : mFailureLatch(WrapNotNull(&aFailureLatch)) {} + + void Set(mozilla::FailureLatch& aFailureLatch) { + mFailureLatch = WrapNotNull(&aFailureLatch); + } + + FAILURELATCH_IMPL_PROXY(*mFailureLatch) + + private: + mozilla::NotNull<mozilla::FailureLatch*> mFailureLatch; + }; + + Proxy proxy{mozilla::FailureLatchInfallibleSource::Singleton()}; + + MOZ_RELEASE_ASSERT(!proxy.Fallible()); + MOZ_RELEASE_ASSERT(!proxy.Failed()); + MOZ_RELEASE_ASSERT(!proxy.GetFailure()); + MOZ_RELEASE_ASSERT(&proxy.SourceFailureLatch() == + &mozilla::FailureLatchInfallibleSource::Singleton()); + MOZ_RELEASE_ASSERT(&std::as_const(proxy).SourceFailureLatch() == + &mozilla::FailureLatchInfallibleSource::Singleton()); + + // Error from proxy. + { + mozilla::FailureLatchSource failureLatch; + proxy.Set(failureLatch); + MOZ_RELEASE_ASSERT(proxy.Fallible()); + MOZ_RELEASE_ASSERT(!proxy.Failed()); + MOZ_RELEASE_ASSERT(!proxy.GetFailure()); + MOZ_RELEASE_ASSERT(&proxy.SourceFailureLatch() == &failureLatch); + MOZ_RELEASE_ASSERT(&std::as_const(proxy).SourceFailureLatch() == + &failureLatch); + + proxy.SetFailure("error"); + MOZ_RELEASE_ASSERT(proxy.Failed()); + MOZ_RELEASE_ASSERT(strcmp(proxy.GetFailure(), "error") == 0); + MOZ_RELEASE_ASSERT(failureLatch.Failed()); + MOZ_RELEASE_ASSERT(strcmp(failureLatch.GetFailure(), "error") == 0); + + // Don't forget to stop pointing at soon-to-be-destroyed object. + proxy.Set(mozilla::FailureLatchInfallibleSource::Singleton()); + } + + // Error from proxy's origin. + { + mozilla::FailureLatchSource failureLatch; + proxy.Set(failureLatch); + MOZ_RELEASE_ASSERT(proxy.Fallible()); + MOZ_RELEASE_ASSERT(!proxy.Failed()); + MOZ_RELEASE_ASSERT(!proxy.GetFailure()); + MOZ_RELEASE_ASSERT(&proxy.SourceFailureLatch() == &failureLatch); + MOZ_RELEASE_ASSERT(&std::as_const(proxy).SourceFailureLatch() == + &failureLatch); + + failureLatch.SetFailure("error"); + MOZ_RELEASE_ASSERT(proxy.Failed()); + MOZ_RELEASE_ASSERT(strcmp(proxy.GetFailure(), "error") == 0); + MOZ_RELEASE_ASSERT(failureLatch.Failed()); + MOZ_RELEASE_ASSERT(strcmp(failureLatch.GetFailure(), "error") == 0); + + // Don't forget to stop pointing at soon-to-be-destroyed object. + proxy.Set(mozilla::FailureLatchInfallibleSource::Singleton()); + } + + MOZ_RELEASE_ASSERT(!proxy.Fallible()); + MOZ_RELEASE_ASSERT(!proxy.Failed()); + MOZ_RELEASE_ASSERT(!proxy.GetFailure()); + MOZ_RELEASE_ASSERT(&proxy.SourceFailureLatch() == + &mozilla::FailureLatchInfallibleSource::Singleton()); + MOZ_RELEASE_ASSERT(&std::as_const(proxy).SourceFailureLatch() == + &mozilla::FailureLatchInfallibleSource::Singleton()); + } + + // Test FAILURELATCH_IMPL_PROXY_OR_INFALLIBLE + { + class ProxyOrNull final : public mozilla::FailureLatch { + public: + ProxyOrNull() = default; + + void Set(mozilla::FailureLatch* aFailureLatchOrNull) { + mFailureLatchOrNull = aFailureLatchOrNull; + } + + FAILURELATCH_IMPL_PROXY_OR_INFALLIBLE(mFailureLatchOrNull, ProxyOrNull) + + private: + mozilla::FailureLatch* mFailureLatchOrNull = nullptr; + }; + + ProxyOrNull proxy; + + MOZ_RELEASE_ASSERT(!proxy.Fallible()); + MOZ_RELEASE_ASSERT(!proxy.Failed()); + MOZ_RELEASE_ASSERT(!proxy.GetFailure()); + MOZ_RELEASE_ASSERT(&proxy.SourceFailureLatch() == + &mozilla::FailureLatchInfallibleSource::Singleton()); + MOZ_RELEASE_ASSERT(&std::as_const(proxy).SourceFailureLatch() == + &mozilla::FailureLatchInfallibleSource::Singleton()); + + // Error from proxy. + { + mozilla::FailureLatchSource failureLatch; + proxy.Set(&failureLatch); + MOZ_RELEASE_ASSERT(proxy.Fallible()); + MOZ_RELEASE_ASSERT(!proxy.Failed()); + MOZ_RELEASE_ASSERT(!proxy.GetFailure()); + MOZ_RELEASE_ASSERT(&proxy.SourceFailureLatch() == &failureLatch); + MOZ_RELEASE_ASSERT(&std::as_const(proxy).SourceFailureLatch() == + &failureLatch); + + proxy.SetFailure("error"); + MOZ_RELEASE_ASSERT(proxy.Failed()); + MOZ_RELEASE_ASSERT(strcmp(proxy.GetFailure(), "error") == 0); + MOZ_RELEASE_ASSERT(failureLatch.Failed()); + MOZ_RELEASE_ASSERT(strcmp(failureLatch.GetFailure(), "error") == 0); + + // Don't forget to stop pointing at soon-to-be-destroyed object. + proxy.Set(nullptr); + } + + // Error from proxy's origin. + { + mozilla::FailureLatchSource failureLatch; + proxy.Set(&failureLatch); + MOZ_RELEASE_ASSERT(proxy.Fallible()); + MOZ_RELEASE_ASSERT(!proxy.Failed()); + MOZ_RELEASE_ASSERT(!proxy.GetFailure()); + MOZ_RELEASE_ASSERT(&proxy.SourceFailureLatch() == &failureLatch); + MOZ_RELEASE_ASSERT(&std::as_const(proxy).SourceFailureLatch() == + &failureLatch); + + failureLatch.SetFailure("error"); + MOZ_RELEASE_ASSERT(proxy.Failed()); + MOZ_RELEASE_ASSERT(strcmp(proxy.GetFailure(), "error") == 0); + MOZ_RELEASE_ASSERT(failureLatch.Failed()); + MOZ_RELEASE_ASSERT(strcmp(failureLatch.GetFailure(), "error") == 0); + + // Don't forget to stop pointing at soon-to-be-destroyed object. + proxy.Set(nullptr); + } + + MOZ_RELEASE_ASSERT(!proxy.Fallible()); + MOZ_RELEASE_ASSERT(!proxy.Failed()); + MOZ_RELEASE_ASSERT(!proxy.GetFailure()); + MOZ_RELEASE_ASSERT(&proxy.SourceFailureLatch() == + &mozilla::FailureLatchInfallibleSource::Singleton()); + MOZ_RELEASE_ASSERT(&std::as_const(proxy).SourceFailureLatch() == + &mozilla::FailureLatchInfallibleSource::Singleton()); + } + + printf("TestFailureLatch done\n"); +} + +void TestProfilerUtils() { + printf("TestProfilerUtils...\n"); + + { + using mozilla::baseprofiler::BaseProfilerProcessId; + using Number = BaseProfilerProcessId::NumberType; + static constexpr Number scMaxNumber = std::numeric_limits<Number>::max(); + + static_assert( + BaseProfilerProcessId{}.ToNumber() == 0, + "These tests assume that the unspecified process id number is 0; " + "if this fails, please update these tests accordingly"); + + static_assert(!BaseProfilerProcessId{}.IsSpecified()); + static_assert(!BaseProfilerProcessId::FromNumber(0).IsSpecified()); + static_assert(BaseProfilerProcessId::FromNumber(1).IsSpecified()); + static_assert(BaseProfilerProcessId::FromNumber(123).IsSpecified()); + static_assert(BaseProfilerProcessId::FromNumber(scMaxNumber).IsSpecified()); + + static_assert(BaseProfilerProcessId::FromNumber(Number(1)).ToNumber() == + Number(1)); + static_assert(BaseProfilerProcessId::FromNumber(Number(123)).ToNumber() == + Number(123)); + static_assert(BaseProfilerProcessId::FromNumber(scMaxNumber).ToNumber() == + scMaxNumber); + + static_assert(BaseProfilerProcessId{} == BaseProfilerProcessId{}); + static_assert(BaseProfilerProcessId::FromNumber(Number(123)) == + BaseProfilerProcessId::FromNumber(Number(123))); + static_assert(BaseProfilerProcessId{} != + BaseProfilerProcessId::FromNumber(Number(123))); + static_assert(BaseProfilerProcessId::FromNumber(Number(123)) != + BaseProfilerProcessId{}); + static_assert(BaseProfilerProcessId::FromNumber(Number(123)) != + BaseProfilerProcessId::FromNumber(scMaxNumber)); + static_assert(BaseProfilerProcessId::FromNumber(scMaxNumber) != + BaseProfilerProcessId::FromNumber(Number(123))); + + // Verify trivial-copyability by memcpy'ing to&from same-size storage. + static_assert(std::is_trivially_copyable_v<BaseProfilerProcessId>); + BaseProfilerProcessId pid; + MOZ_RELEASE_ASSERT(!pid.IsSpecified()); + Number pidStorage; + static_assert(sizeof(pidStorage) == sizeof(pid)); + // Copy from BaseProfilerProcessId to storage. Note: We cannot assume that + // this is equal to what ToNumber() gives us. All we can do is verify that + // copying from storage back to BaseProfilerProcessId works as expected. + std::memcpy(&pidStorage, &pid, sizeof(pidStorage)); + BaseProfilerProcessId pid2 = BaseProfilerProcessId::FromNumber(2); + MOZ_RELEASE_ASSERT(pid2.IsSpecified()); + std::memcpy(&pid2, &pidStorage, sizeof(pid)); + MOZ_RELEASE_ASSERT(!pid2.IsSpecified()); + + pid = BaseProfilerProcessId::FromNumber(123); + std::memcpy(&pidStorage, &pid, sizeof(pidStorage)); + pid2 = BaseProfilerProcessId{}; + MOZ_RELEASE_ASSERT(!pid2.IsSpecified()); + std::memcpy(&pid2, &pidStorage, sizeof(pid)); + MOZ_RELEASE_ASSERT(pid2.IsSpecified()); + MOZ_RELEASE_ASSERT(pid2.ToNumber() == 123); + + // No conversions to/from numbers. + static_assert(!std::is_constructible_v<BaseProfilerProcessId, Number>); + static_assert(!std::is_assignable_v<BaseProfilerProcessId, Number>); + static_assert(!std::is_constructible_v<Number, BaseProfilerProcessId>); + static_assert(!std::is_assignable_v<Number, BaseProfilerProcessId>); + + static_assert( + std::is_same_v< + decltype(mozilla::baseprofiler::profiler_current_process_id()), + BaseProfilerProcessId>); + MOZ_RELEASE_ASSERT( + mozilla::baseprofiler::profiler_current_process_id().IsSpecified()); + } + + { + mozilla::baseprofiler::profiler_init_main_thread_id(); + + using mozilla::baseprofiler::BaseProfilerThreadId; + using Number = BaseProfilerThreadId::NumberType; + static constexpr Number scMaxNumber = std::numeric_limits<Number>::max(); + + static_assert( + BaseProfilerThreadId{}.ToNumber() == 0, + "These tests assume that the unspecified thread id number is 0; " + "if this fails, please update these tests accordingly"); + + static_assert(!BaseProfilerThreadId{}.IsSpecified()); + static_assert(!BaseProfilerThreadId::FromNumber(0).IsSpecified()); + static_assert(BaseProfilerThreadId::FromNumber(1).IsSpecified()); + static_assert(BaseProfilerThreadId::FromNumber(123).IsSpecified()); + static_assert(BaseProfilerThreadId::FromNumber(scMaxNumber).IsSpecified()); + + static_assert(BaseProfilerThreadId::FromNumber(Number(1)).ToNumber() == + Number(1)); + static_assert(BaseProfilerThreadId::FromNumber(Number(123)).ToNumber() == + Number(123)); + static_assert(BaseProfilerThreadId::FromNumber(scMaxNumber).ToNumber() == + scMaxNumber); + + static_assert(BaseProfilerThreadId{} == BaseProfilerThreadId{}); + static_assert(BaseProfilerThreadId::FromNumber(Number(123)) == + BaseProfilerThreadId::FromNumber(Number(123))); + static_assert(BaseProfilerThreadId{} != + BaseProfilerThreadId::FromNumber(Number(123))); + static_assert(BaseProfilerThreadId::FromNumber(Number(123)) != + BaseProfilerThreadId{}); + static_assert(BaseProfilerThreadId::FromNumber(Number(123)) != + BaseProfilerThreadId::FromNumber(scMaxNumber)); + static_assert(BaseProfilerThreadId::FromNumber(scMaxNumber) != + BaseProfilerThreadId::FromNumber(Number(123))); + + // Verify trivial-copyability by memcpy'ing to&from same-size storage. + static_assert(std::is_trivially_copyable_v<BaseProfilerThreadId>); + BaseProfilerThreadId tid; + MOZ_RELEASE_ASSERT(!tid.IsSpecified()); + Number tidStorage; + static_assert(sizeof(tidStorage) == sizeof(tid)); + // Copy from BaseProfilerThreadId to storage. Note: We cannot assume that + // this is equal to what ToNumber() gives us. All we can do is verify that + // copying from storage back to BaseProfilerThreadId works as expected. + std::memcpy(&tidStorage, &tid, sizeof(tidStorage)); + BaseProfilerThreadId tid2 = BaseProfilerThreadId::FromNumber(2); + MOZ_RELEASE_ASSERT(tid2.IsSpecified()); + std::memcpy(&tid2, &tidStorage, sizeof(tid)); + MOZ_RELEASE_ASSERT(!tid2.IsSpecified()); + + tid = BaseProfilerThreadId::FromNumber(Number(123)); + std::memcpy(&tidStorage, &tid, sizeof(tidStorage)); + tid2 = BaseProfilerThreadId{}; + MOZ_RELEASE_ASSERT(!tid2.IsSpecified()); + std::memcpy(&tid2, &tidStorage, sizeof(tid)); + MOZ_RELEASE_ASSERT(tid2.IsSpecified()); + MOZ_RELEASE_ASSERT(tid2.ToNumber() == Number(123)); + + // No conversions to/from numbers. + static_assert(!std::is_constructible_v<BaseProfilerThreadId, Number>); + static_assert(!std::is_assignable_v<BaseProfilerThreadId, Number>); + static_assert(!std::is_constructible_v<Number, BaseProfilerThreadId>); + static_assert(!std::is_assignable_v<Number, BaseProfilerThreadId>); + + static_assert(std::is_same_v< + decltype(mozilla::baseprofiler::profiler_current_thread_id()), + BaseProfilerThreadId>); + BaseProfilerThreadId mainTestThreadId = + mozilla::baseprofiler::profiler_current_thread_id(); + MOZ_RELEASE_ASSERT(mainTestThreadId.IsSpecified()); + + BaseProfilerThreadId mainThreadId = + mozilla::baseprofiler::profiler_main_thread_id(); + MOZ_RELEASE_ASSERT(mainThreadId.IsSpecified()); + + MOZ_RELEASE_ASSERT(mainThreadId == mainTestThreadId, + "Test should run on the main thread"); + MOZ_RELEASE_ASSERT(mozilla::baseprofiler::profiler_is_main_thread()); + + std::thread testThread([&]() { + const BaseProfilerThreadId testThreadId = + mozilla::baseprofiler::profiler_current_thread_id(); + MOZ_RELEASE_ASSERT(testThreadId.IsSpecified()); + MOZ_RELEASE_ASSERT(testThreadId != mainThreadId); + MOZ_RELEASE_ASSERT(!mozilla::baseprofiler::profiler_is_main_thread()); + }); + testThread.join(); + } + + // No conversions between processes and threads. + static_assert( + !std::is_constructible_v<mozilla::baseprofiler::BaseProfilerThreadId, + mozilla::baseprofiler::BaseProfilerProcessId>); + static_assert( + !std::is_assignable_v<mozilla::baseprofiler::BaseProfilerThreadId, + mozilla::baseprofiler::BaseProfilerProcessId>); + static_assert( + !std::is_constructible_v<mozilla::baseprofiler::BaseProfilerProcessId, + mozilla::baseprofiler::BaseProfilerThreadId>); + static_assert( + !std::is_assignable_v<mozilla::baseprofiler::BaseProfilerProcessId, + mozilla::baseprofiler::BaseProfilerThreadId>); + + printf("TestProfilerUtils done\n"); +} + +void TestBaseAndProfilerDetail() { + printf("TestBaseAndProfilerDetail...\n"); + + { + using mozilla::profiler::detail::FilterHasPid; + + const auto pid123 = + mozilla::baseprofiler::BaseProfilerProcessId::FromNumber(123); + MOZ_RELEASE_ASSERT(FilterHasPid("pid:123", pid123)); + MOZ_RELEASE_ASSERT(!FilterHasPid("", pid123)); + MOZ_RELEASE_ASSERT(!FilterHasPid(" ", pid123)); + MOZ_RELEASE_ASSERT(!FilterHasPid("123", pid123)); + MOZ_RELEASE_ASSERT(!FilterHasPid("pid", pid123)); + MOZ_RELEASE_ASSERT(!FilterHasPid("pid:", pid123)); + MOZ_RELEASE_ASSERT(!FilterHasPid("pid=123", pid123)); + MOZ_RELEASE_ASSERT(!FilterHasPid("pid:123 ", pid123)); + MOZ_RELEASE_ASSERT(!FilterHasPid("pid: 123", pid123)); + MOZ_RELEASE_ASSERT(!FilterHasPid("pid:0123", pid123)); + MOZ_RELEASE_ASSERT(!FilterHasPid("pid:0000000000000000000000123", pid123)); + MOZ_RELEASE_ASSERT(!FilterHasPid("pid:12", pid123)); + MOZ_RELEASE_ASSERT(!FilterHasPid("pid:1234", pid123)); + MOZ_RELEASE_ASSERT(!FilterHasPid("pid:0", pid123)); + + using PidNumber = mozilla::baseprofiler::BaseProfilerProcessId::NumberType; + const PidNumber maxNumber = std::numeric_limits<PidNumber>::max(); + const auto maxPid = + mozilla::baseprofiler::BaseProfilerProcessId::FromNumber(maxNumber); + const std::string maxPidString = "pid:" + std::to_string(maxNumber); + MOZ_RELEASE_ASSERT(FilterHasPid(maxPidString.c_str(), maxPid)); + + const std::string tooBigPidString = maxPidString + "0"; + MOZ_RELEASE_ASSERT(!FilterHasPid(tooBigPidString.c_str(), maxPid)); + } + + { + using mozilla::profiler::detail::FiltersExcludePid; + const auto pid123 = + mozilla::baseprofiler::BaseProfilerProcessId::FromNumber(123); + + MOZ_RELEASE_ASSERT( + !FiltersExcludePid(mozilla::Span<const char*>{}, pid123)); + + { + const char* const filters[] = {"main"}; + MOZ_RELEASE_ASSERT(!FiltersExcludePid(filters, pid123)); + } + + { + const char* const filters[] = {"main", "pid:123"}; + MOZ_RELEASE_ASSERT(!FiltersExcludePid(filters, pid123)); + } + + { + const char* const filters[] = {"main", "pid:456"}; + MOZ_RELEASE_ASSERT(!FiltersExcludePid(filters, pid123)); + } + + { + const char* const filters[] = {"pid:123"}; + MOZ_RELEASE_ASSERT(!FiltersExcludePid(filters, pid123)); + } + + { + const char* const filters[] = {"pid:123", "pid:456"}; + MOZ_RELEASE_ASSERT(!FiltersExcludePid(filters, pid123)); + } + + { + const char* const filters[] = {"pid:456", "pid:123"}; + MOZ_RELEASE_ASSERT(!FiltersExcludePid(filters, pid123)); + } + + { + const char* const filters[] = {"pid:456"}; + MOZ_RELEASE_ASSERT(FiltersExcludePid(filters, pid123)); + } + + { + const char* const filters[] = {"pid:456", "pid:789"}; + MOZ_RELEASE_ASSERT(FiltersExcludePid(filters, pid123)); + } + } + + printf("TestBaseAndProfilerDetail done\n"); +} + +void TestSharedMutex() { + printf("TestSharedMutex...\n"); + + mozilla::baseprofiler::detail::BaseProfilerSharedMutex sm; + + // First round of minimal tests in this thread. + + MOZ_RELEASE_ASSERT(!sm.IsLockedExclusiveOnCurrentThread()); + + sm.LockExclusive(); + MOZ_RELEASE_ASSERT(sm.IsLockedExclusiveOnCurrentThread()); + sm.UnlockExclusive(); + MOZ_RELEASE_ASSERT(!sm.IsLockedExclusiveOnCurrentThread()); + + sm.LockShared(); + MOZ_RELEASE_ASSERT(!sm.IsLockedExclusiveOnCurrentThread()); + sm.UnlockShared(); + MOZ_RELEASE_ASSERT(!sm.IsLockedExclusiveOnCurrentThread()); + + { + mozilla::baseprofiler::detail::BaseProfilerAutoLockExclusive exclusiveLock{ + sm}; + MOZ_RELEASE_ASSERT(sm.IsLockedExclusiveOnCurrentThread()); + } + MOZ_RELEASE_ASSERT(!sm.IsLockedExclusiveOnCurrentThread()); + + { + mozilla::baseprofiler::detail::BaseProfilerAutoLockShared sharedLock{sm}; + MOZ_RELEASE_ASSERT(!sm.IsLockedExclusiveOnCurrentThread()); + } + MOZ_RELEASE_ASSERT(!sm.IsLockedExclusiveOnCurrentThread()); + + // The following will run actions between two threads, to verify that + // exclusive and shared locks work as expected. + + // These actions will happen from top to bottom. + // This will test all possible lock interactions. + enum NextAction { // State of the lock: + t1Starting, // (x=exclusive, s=shared, ?=blocked) + t2Starting, // t1 t2 + t1LockExclusive, // x + t2LockExclusiveAndBlock, // x x? - Can't have two exclusives. + t1UnlockExclusive, // x + t2UnblockedAfterT1Unlock, // x + t1LockSharedAndBlock, // s? x - Can't have shared during excl + t2UnlockExclusive, // s + t1UnblockedAfterT2Unlock, // s + t2LockShared, // s s - Can have multiple shared locks + t1UnlockShared, // s + t2StillLockedShared, // s + t1LockExclusiveAndBlock, // x? s - Can't have excl during shared + t2UnlockShared, // x + t1UnblockedAfterT2UnlockShared, // x + t2CheckAfterT1Lock, // x + t1LastUnlockExclusive, // (unlocked) + done + }; + + // Each thread will repeatedly read this `nextAction`, and run actions that + // target it... + std::atomic<NextAction> nextAction{static_cast<NextAction>(0)}; + // ... and advance to the next available action (which should usually be for + // the other thread). + auto AdvanceAction = [&nextAction]() { + MOZ_RELEASE_ASSERT(nextAction <= done); + nextAction = static_cast<NextAction>(static_cast<int>(nextAction) + 1); + }; + + std::thread t1{[&]() { + for (;;) { + switch (nextAction) { + case t1Starting: + AdvanceAction(); + break; + case t1LockExclusive: + MOZ_RELEASE_ASSERT(!sm.IsLockedExclusiveOnCurrentThread()); + sm.LockExclusive(); + MOZ_RELEASE_ASSERT(sm.IsLockedExclusiveOnCurrentThread()); + AdvanceAction(); + break; + case t1UnlockExclusive: + MOZ_RELEASE_ASSERT(sm.IsLockedExclusiveOnCurrentThread()); + // Advance first, before unlocking, so that t2 sees the new state. + AdvanceAction(); + sm.UnlockExclusive(); + MOZ_RELEASE_ASSERT(!sm.IsLockedExclusiveOnCurrentThread()); + break; + case t1LockSharedAndBlock: + // Advance action before attempting to lock after t2's exclusive lock. + AdvanceAction(); + sm.LockShared(); + // We will only acquire the lock after t1 unlocks. + MOZ_RELEASE_ASSERT(nextAction == t1UnblockedAfterT2Unlock); + MOZ_RELEASE_ASSERT(!sm.IsLockedExclusiveOnCurrentThread()); + AdvanceAction(); + break; + case t1UnlockShared: + MOZ_RELEASE_ASSERT(!sm.IsLockedExclusiveOnCurrentThread()); + // Advance first, before unlocking, so that t2 sees the new state. + AdvanceAction(); + sm.UnlockShared(); + MOZ_RELEASE_ASSERT(!sm.IsLockedExclusiveOnCurrentThread()); + break; + case t1LockExclusiveAndBlock: + MOZ_RELEASE_ASSERT(!sm.IsLockedExclusiveOnCurrentThread()); + // Advance action before attempting to lock after t2's shared lock. + AdvanceAction(); + sm.LockExclusive(); + // We will only acquire the lock after t2 unlocks. + MOZ_RELEASE_ASSERT(nextAction == t1UnblockedAfterT2UnlockShared); + MOZ_RELEASE_ASSERT(sm.IsLockedExclusiveOnCurrentThread()); + AdvanceAction(); + break; + case t1LastUnlockExclusive: + MOZ_RELEASE_ASSERT(sm.IsLockedExclusiveOnCurrentThread()); + // Advance first, before unlocking, so that t2 sees the new state. + AdvanceAction(); + sm.UnlockExclusive(); + MOZ_RELEASE_ASSERT(!sm.IsLockedExclusiveOnCurrentThread()); + break; + case done: + return; + default: + // Ignore other actions intended for t2. + break; + } + } + }}; + + std::thread t2{[&]() { + for (;;) { + switch (nextAction) { + case t2Starting: + AdvanceAction(); + break; + case t2LockExclusiveAndBlock: + MOZ_RELEASE_ASSERT(!sm.IsLockedExclusiveOnCurrentThread()); + // Advance action before attempting to lock after t1's exclusive lock. + AdvanceAction(); + sm.LockExclusive(); + // We will only acquire the lock after t1 unlocks. + MOZ_RELEASE_ASSERT(nextAction == t2UnblockedAfterT1Unlock); + MOZ_RELEASE_ASSERT(sm.IsLockedExclusiveOnCurrentThread()); + AdvanceAction(); + break; + case t2UnlockExclusive: + MOZ_RELEASE_ASSERT(sm.IsLockedExclusiveOnCurrentThread()); + // Advance first, before unlocking, so that t1 sees the new state. + AdvanceAction(); + sm.UnlockExclusive(); + MOZ_RELEASE_ASSERT(!sm.IsLockedExclusiveOnCurrentThread()); + break; + case t2LockShared: + sm.LockShared(); + MOZ_RELEASE_ASSERT(!sm.IsLockedExclusiveOnCurrentThread()); + AdvanceAction(); + break; + case t2StillLockedShared: + AdvanceAction(); + break; + case t2UnlockShared: + MOZ_RELEASE_ASSERT(!sm.IsLockedExclusiveOnCurrentThread()); + // Advance first, before unlocking, so that t1 sees the new state. + AdvanceAction(); + sm.UnlockShared(); + MOZ_RELEASE_ASSERT(!sm.IsLockedExclusiveOnCurrentThread()); + break; + case t2CheckAfterT1Lock: + MOZ_RELEASE_ASSERT(!sm.IsLockedExclusiveOnCurrentThread()); + AdvanceAction(); + break; + case done: + return; + default: + // Ignore other actions intended for t1. + break; + } + } + }}; + + t1.join(); + t2.join(); + + printf("TestSharedMutex done\n"); +} + +void TestProportionValue() { + printf("TestProportionValue...\n"); + + using mozilla::ProportionValue; + +#define STATIC_ASSERT_EQ(a, b) \ + static_assert((a) == (b)); \ + MOZ_RELEASE_ASSERT((a) == (b)); + +#define STATIC_ASSERT(e) STATIC_ASSERT_EQ(e, true) + + // Conversion from&to double. + STATIC_ASSERT_EQ(ProportionValue().ToDouble(), 0.0); + STATIC_ASSERT_EQ(ProportionValue(0.0).ToDouble(), 0.0); + STATIC_ASSERT_EQ(ProportionValue(0.5).ToDouble(), 0.5); + STATIC_ASSERT_EQ(ProportionValue(1.0).ToDouble(), 1.0); + + // Clamping. + STATIC_ASSERT_EQ( + ProportionValue(std::numeric_limits<double>::min()).ToDouble(), 0.0); + STATIC_ASSERT_EQ( + ProportionValue(std::numeric_limits<long double>::min()).ToDouble(), 0.0); + STATIC_ASSERT_EQ(ProportionValue(-1.0).ToDouble(), 0.0); + STATIC_ASSERT_EQ(ProportionValue(-0.01).ToDouble(), 0.0); + STATIC_ASSERT_EQ(ProportionValue(-0.0).ToDouble(), 0.0); + STATIC_ASSERT_EQ(ProportionValue(1.01).ToDouble(), 1.0); + STATIC_ASSERT_EQ( + ProportionValue(std::numeric_limits<double>::max()).ToDouble(), 1.0); + + // User-defined literal. + { + using namespace mozilla::literals::ProportionValue_literals; + STATIC_ASSERT_EQ(0_pc, ProportionValue(0.0)); + STATIC_ASSERT_EQ(0._pc, ProportionValue(0.0)); + STATIC_ASSERT_EQ(50_pc, ProportionValue(0.5)); + STATIC_ASSERT_EQ(50._pc, ProportionValue(0.5)); + STATIC_ASSERT_EQ(100_pc, ProportionValue(1.0)); + STATIC_ASSERT_EQ(100._pc, ProportionValue(1.0)); + STATIC_ASSERT_EQ(101_pc, ProportionValue(1.0)); + STATIC_ASSERT_EQ(100.01_pc, ProportionValue(1.0)); + STATIC_ASSERT_EQ(1000_pc, ProportionValue(1.0)); + STATIC_ASSERT_EQ(1000._pc, ProportionValue(1.0)); + } + { + // ProportionValue_literals is an inline namespace of mozilla::literals, so + // it's optional. + using namespace mozilla::literals; + STATIC_ASSERT_EQ(0_pc, ProportionValue(0.0)); + STATIC_ASSERT_EQ(0._pc, ProportionValue(0.0)); + STATIC_ASSERT_EQ(50_pc, ProportionValue(0.5)); + STATIC_ASSERT_EQ(50._pc, ProportionValue(0.5)); + STATIC_ASSERT_EQ(100_pc, ProportionValue(1.0)); + STATIC_ASSERT_EQ(100._pc, ProportionValue(1.0)); + STATIC_ASSERT_EQ(101_pc, ProportionValue(1.0)); + STATIC_ASSERT_EQ(100.01_pc, ProportionValue(1.0)); + STATIC_ASSERT_EQ(1000_pc, ProportionValue(1.0)); + STATIC_ASSERT_EQ(1000._pc, ProportionValue(1.0)); + } + + // Invalid construction, conversion to double NaN. + MOZ_RELEASE_ASSERT(std::isnan(ProportionValue::MakeInvalid().ToDouble())); + + using namespace mozilla::literals::ProportionValue_literals; + + // Conversion to&from underlying integral number. + STATIC_ASSERT_EQ( + ProportionValue::FromUnderlyingType((0_pc).ToUnderlyingType()).ToDouble(), + 0.0); + STATIC_ASSERT_EQ( + ProportionValue::FromUnderlyingType((50_pc).ToUnderlyingType()) + .ToDouble(), + 0.5); + STATIC_ASSERT_EQ( + ProportionValue::FromUnderlyingType((100_pc).ToUnderlyingType()) + .ToDouble(), + 1.0); + STATIC_ASSERT(ProportionValue::FromUnderlyingType( + ProportionValue::MakeInvalid().ToUnderlyingType()) + .IsInvalid()); + + // IsExactlyZero. + STATIC_ASSERT(ProportionValue().IsExactlyZero()); + STATIC_ASSERT((0_pc).IsExactlyZero()); + STATIC_ASSERT(!(50_pc).IsExactlyZero()); + STATIC_ASSERT(!(100_pc).IsExactlyZero()); + STATIC_ASSERT(!ProportionValue::MakeInvalid().IsExactlyZero()); + + // IsExactlyOne. + STATIC_ASSERT(!ProportionValue().IsExactlyOne()); + STATIC_ASSERT(!(0_pc).IsExactlyOne()); + STATIC_ASSERT(!(50_pc).IsExactlyOne()); + STATIC_ASSERT((100_pc).IsExactlyOne()); + STATIC_ASSERT(!ProportionValue::MakeInvalid().IsExactlyOne()); + + // IsValid. + STATIC_ASSERT(ProportionValue().IsValid()); + STATIC_ASSERT((0_pc).IsValid()); + STATIC_ASSERT((50_pc).IsValid()); + STATIC_ASSERT((100_pc).IsValid()); + STATIC_ASSERT(!ProportionValue::MakeInvalid().IsValid()); + + // IsInvalid. + STATIC_ASSERT(!ProportionValue().IsInvalid()); + STATIC_ASSERT(!(0_pc).IsInvalid()); + STATIC_ASSERT(!(50_pc).IsInvalid()); + STATIC_ASSERT(!(100_pc).IsInvalid()); + STATIC_ASSERT(ProportionValue::MakeInvalid().IsInvalid()); + + // Addition. + STATIC_ASSERT_EQ((0_pc + 0_pc).ToDouble(), 0.0); + STATIC_ASSERT_EQ((0_pc + 100_pc).ToDouble(), 1.0); + STATIC_ASSERT_EQ((100_pc + 0_pc).ToDouble(), 1.0); + STATIC_ASSERT_EQ((100_pc + 100_pc).ToDouble(), 1.0); + STATIC_ASSERT((ProportionValue::MakeInvalid() + 50_pc).IsInvalid()); + STATIC_ASSERT((50_pc + ProportionValue::MakeInvalid()).IsInvalid()); + + // Subtraction. + STATIC_ASSERT_EQ((0_pc - 0_pc).ToDouble(), 0.0); + STATIC_ASSERT_EQ((0_pc - 100_pc).ToDouble(), 0.0); + STATIC_ASSERT_EQ((100_pc - 0_pc).ToDouble(), 1.0); + STATIC_ASSERT_EQ((100_pc - 100_pc).ToDouble(), 0.0); + STATIC_ASSERT((ProportionValue::MakeInvalid() - 50_pc).IsInvalid()); + STATIC_ASSERT((50_pc - ProportionValue::MakeInvalid()).IsInvalid()); + + // Multiplication. + STATIC_ASSERT_EQ((0_pc * 0_pc).ToDouble(), 0.0); + STATIC_ASSERT_EQ((0_pc * 100_pc).ToDouble(), 0.0); + STATIC_ASSERT_EQ((50_pc * 50_pc).ToDouble(), 0.25); + STATIC_ASSERT_EQ((50_pc * 100_pc).ToDouble(), 0.5); + STATIC_ASSERT_EQ((100_pc * 50_pc).ToDouble(), 0.5); + STATIC_ASSERT_EQ((100_pc * 0_pc).ToDouble(), 0.0); + STATIC_ASSERT_EQ((100_pc * 100_pc).ToDouble(), 1.0); + STATIC_ASSERT((ProportionValue::MakeInvalid() * 50_pc).IsInvalid()); + STATIC_ASSERT((50_pc * ProportionValue::MakeInvalid()).IsInvalid()); + + // Division by a positive integer value. + STATIC_ASSERT_EQ((100_pc / 1u).ToDouble(), 1.0); + STATIC_ASSERT_EQ((100_pc / 2u).ToDouble(), 0.5); + STATIC_ASSERT_EQ( + (ProportionValue::FromUnderlyingType(6u) / 2u).ToUnderlyingType(), 3u); + STATIC_ASSERT_EQ( + (ProportionValue::FromUnderlyingType(5u) / 2u).ToUnderlyingType(), 2u); + STATIC_ASSERT_EQ( + (ProportionValue::FromUnderlyingType(1u) / 2u).ToUnderlyingType(), 0u); + STATIC_ASSERT_EQ( + (ProportionValue::FromUnderlyingType(0u) / 2u).ToUnderlyingType(), 0u); + STATIC_ASSERT((100_pc / 0u).IsInvalid()); + STATIC_ASSERT((ProportionValue::MakeInvalid() / 2u).IsInvalid()); + + // Multiplication by a positive integer value. + STATIC_ASSERT_EQ((100_pc * 1u).ToDouble(), 1.0); + STATIC_ASSERT_EQ((50_pc * 1u).ToDouble(), 0.5); + STATIC_ASSERT_EQ((50_pc * 2u).ToDouble(), 1.0); + STATIC_ASSERT_EQ((50_pc * 3u).ToDouble(), 1.0); // Clamped. + STATIC_ASSERT_EQ( + (ProportionValue::FromUnderlyingType(1u) * 2u).ToUnderlyingType(), 2u); + STATIC_ASSERT((ProportionValue::MakeInvalid() * 2u).IsInvalid()); + + // Verifying PV - u < (PV / u) * u <= PV, with n=3, PV between 6 and 9 : + STATIC_ASSERT_EQ( + (ProportionValue::FromUnderlyingType(6u) / 3u).ToUnderlyingType(), 2u); + STATIC_ASSERT_EQ( + (ProportionValue::FromUnderlyingType(7u) / 3u).ToUnderlyingType(), 2u); + STATIC_ASSERT_EQ( + (ProportionValue::FromUnderlyingType(8u) / 3u).ToUnderlyingType(), 2u); + STATIC_ASSERT_EQ( + (ProportionValue::FromUnderlyingType(9u) / 3u).ToUnderlyingType(), 3u); + + // Direct comparisons. + STATIC_ASSERT_EQ(0_pc, 0_pc); + STATIC_ASSERT(0_pc == 0_pc); + STATIC_ASSERT(!(0_pc == 100_pc)); + STATIC_ASSERT(0_pc != 100_pc); + STATIC_ASSERT(!(0_pc != 0_pc)); + STATIC_ASSERT(0_pc < 100_pc); + STATIC_ASSERT(!(0_pc < 0_pc)); + STATIC_ASSERT(0_pc <= 0_pc); + STATIC_ASSERT(0_pc <= 100_pc); + STATIC_ASSERT(!(100_pc <= 0_pc)); + STATIC_ASSERT(100_pc > 0_pc); + STATIC_ASSERT(!(100_pc > 100_pc)); + STATIC_ASSERT(100_pc >= 0_pc); + STATIC_ASSERT(100_pc >= 100_pc); + STATIC_ASSERT(!(0_pc >= 100_pc)); + // 0.5 is binary-friendly, so we can double it and compare it exactly. + STATIC_ASSERT_EQ(50_pc + 50_pc, 100_pc); + +#undef STATIC_ASSERT_EQ + + printf("TestProportionValue done\n"); +} + +template <typename Arg0, typename... Args> +bool AreAllEqual(Arg0&& aArg0, Args&&... aArgs) { + return ((aArg0 == aArgs) && ...); +} + +void TestProgressLogger() { + printf("TestProgressLogger...\n"); + + using mozilla::ProgressLogger; + using mozilla::ProportionValue; + using namespace mozilla::literals::ProportionValue_literals; + + auto progressRefPtr = mozilla::MakeRefPtr<ProgressLogger::SharedProgress>(); + MOZ_RELEASE_ASSERT(progressRefPtr); + MOZ_RELEASE_ASSERT(progressRefPtr->Progress().IsExactlyZero()); + + { + ProgressLogger pl(progressRefPtr, "Started", "All done"); + MOZ_RELEASE_ASSERT(progressRefPtr->Progress().IsExactlyZero()); + MOZ_RELEASE_ASSERT(pl.GetGlobalProgress().IsExactlyZero()); + MOZ_RELEASE_ASSERT(AreAllEqual(progressRefPtr->LastLocation(), + pl.GetLastGlobalLocation(), "Started")); + + // At this top level, the scale is 1:1. + pl.SetLocalProgress(10_pc, "Top 10%"); + MOZ_RELEASE_ASSERT( + AreAllEqual(progressRefPtr->Progress(), pl.GetGlobalProgress(), 10_pc)); + MOZ_RELEASE_ASSERT(AreAllEqual(progressRefPtr->LastLocation(), + pl.GetLastGlobalLocation(), "Top 10%")); + + pl.SetLocalProgress(0_pc, "Restarted"); + MOZ_RELEASE_ASSERT( + AreAllEqual(progressRefPtr->Progress(), pl.GetGlobalProgress(), 0_pc)); + MOZ_RELEASE_ASSERT(AreAllEqual(progressRefPtr->LastLocation(), + pl.GetLastGlobalLocation(), "Restarted")); + + { + // Create a sub-logger for the whole global range. Notice that this is + // moving the current progress back to 0. + ProgressLogger plSub1 = + pl.CreateSubLoggerFromTo(0_pc, "Sub1 started", 100_pc, "Sub1 ended"); + MOZ_RELEASE_ASSERT(progressRefPtr->Progress().IsExactlyZero()); + MOZ_RELEASE_ASSERT(pl.GetGlobalProgress().IsExactlyZero()); + MOZ_RELEASE_ASSERT(plSub1.GetGlobalProgress().IsExactlyZero()); + MOZ_RELEASE_ASSERT(AreAllEqual( + progressRefPtr->LastLocation(), pl.GetLastGlobalLocation(), + plSub1.GetLastGlobalLocation(), "Sub1 started")); + + // At this level, the scale is still 1:1. + plSub1.SetLocalProgress(10_pc, "Sub1 10%"); + MOZ_RELEASE_ASSERT(AreAllEqual(progressRefPtr->Progress(), + pl.GetGlobalProgress(), + plSub1.GetGlobalProgress(), 10_pc)); + MOZ_RELEASE_ASSERT(AreAllEqual( + progressRefPtr->LastLocation(), pl.GetLastGlobalLocation(), + plSub1.GetLastGlobalLocation(), "Sub1 10%")); + + { + // Create a sub-logger half the global range. + // 0 0.25 0.375 0.5 0.625 0.75 1 + // |---------------|-------|-------|-------|-------|---------------| + // plSub2: 0 0.25 0.5 0.75 1 + ProgressLogger plSub2 = plSub1.CreateSubLoggerFromTo( + 25_pc, "Sub2 started", 75_pc, "Sub2 ended"); + MOZ_RELEASE_ASSERT(AreAllEqual( + progressRefPtr->Progress(), pl.GetGlobalProgress(), + plSub1.GetGlobalProgress(), plSub2.GetGlobalProgress(), 25_pc)); + MOZ_RELEASE_ASSERT(AreAllEqual( + progressRefPtr->LastLocation(), pl.GetLastGlobalLocation(), + plSub1.GetLastGlobalLocation(), plSub2.GetLastGlobalLocation(), + "Sub2 started")); + + plSub2.SetLocalProgress(25_pc, "Sub2 25%"); + MOZ_RELEASE_ASSERT(AreAllEqual( + progressRefPtr->Progress(), pl.GetGlobalProgress(), + plSub1.GetGlobalProgress(), plSub2.GetGlobalProgress(), 37.5_pc)); + MOZ_RELEASE_ASSERT(AreAllEqual( + progressRefPtr->LastLocation(), pl.GetLastGlobalLocation(), + plSub1.GetLastGlobalLocation(), plSub2.GetLastGlobalLocation(), + "Sub2 25%")); + + plSub2.SetLocalProgress(50_pc, "Sub2 50%"); + MOZ_RELEASE_ASSERT(AreAllEqual( + progressRefPtr->Progress(), pl.GetGlobalProgress(), + plSub1.GetGlobalProgress(), plSub2.GetGlobalProgress(), 50_pc)); + MOZ_RELEASE_ASSERT(AreAllEqual( + progressRefPtr->LastLocation(), pl.GetLastGlobalLocation(), + plSub1.GetLastGlobalLocation(), plSub2.GetLastGlobalLocation(), + "Sub2 50%")); + + { + // Create a sub-logger half the parent range. + // 0 0.25 0.375 0.5 0.625 0.75 1 + // |---------------|-------|-------|-------|-------|---------------| + // plSub2: 0 0.25 0.5 0.75 1 + // plSub3: 0 0.5 1 + ProgressLogger plSub3 = plSub2.CreateSubLoggerTo( + "Sub3 started", 100_pc, ProgressLogger::NO_LOCATION_UPDATE); + MOZ_RELEASE_ASSERT(AreAllEqual( + progressRefPtr->Progress(), pl.GetGlobalProgress(), + plSub1.GetGlobalProgress(), plSub2.GetGlobalProgress(), + plSub3.GetGlobalProgress(), 50_pc)); + MOZ_RELEASE_ASSERT(AreAllEqual( + progressRefPtr->LastLocation(), pl.GetLastGlobalLocation(), + plSub1.GetLastGlobalLocation(), plSub2.GetLastGlobalLocation(), + plSub3.GetLastGlobalLocation(), "Sub3 started")); + + plSub3.SetLocalProgress(50_pc, "Sub3 50%"); + MOZ_RELEASE_ASSERT(AreAllEqual( + progressRefPtr->Progress(), pl.GetGlobalProgress(), + plSub1.GetGlobalProgress(), plSub2.GetGlobalProgress(), + plSub3.GetGlobalProgress(), 62.5_pc)); + MOZ_RELEASE_ASSERT(AreAllEqual( + progressRefPtr->LastLocation(), pl.GetLastGlobalLocation(), + plSub1.GetLastGlobalLocation(), plSub2.GetLastGlobalLocation(), + plSub3.GetLastGlobalLocation(), "Sub3 50%")); + } // End of plSub3 + + // When plSub3 ends, progress moves to its 100%, which is also plSub2's + // 100%, which is plSub1's and the global progress of 75% + MOZ_RELEASE_ASSERT(AreAllEqual( + progressRefPtr->Progress(), pl.GetGlobalProgress(), + plSub1.GetGlobalProgress(), plSub2.GetGlobalProgress(), 75_pc)); + // But location is still at the last explicit update. + MOZ_RELEASE_ASSERT(AreAllEqual( + progressRefPtr->LastLocation(), pl.GetLastGlobalLocation(), + plSub1.GetLastGlobalLocation(), plSub2.GetLastGlobalLocation(), + "Sub3 50%")); + } // End of plSub2 + + MOZ_RELEASE_ASSERT(AreAllEqual(progressRefPtr->Progress(), + pl.GetGlobalProgress(), + plSub1.GetGlobalProgress(), 75_pc)); + MOZ_RELEASE_ASSERT(AreAllEqual( + progressRefPtr->LastLocation(), pl.GetLastGlobalLocation(), + plSub1.GetLastGlobalLocation(), "Sub2 ended")); + } // End of plSub1 + + MOZ_RELEASE_ASSERT(progressRefPtr->Progress().IsExactlyOne()); + MOZ_RELEASE_ASSERT(pl.GetGlobalProgress().IsExactlyOne()); + MOZ_RELEASE_ASSERT(AreAllEqual(progressRefPtr->LastLocation(), + pl.GetLastGlobalLocation(), "Sub1 ended")); + + const auto loopStart = 75_pc; + const auto loopEnd = 87.5_pc; + const uint32_t loopCount = 8; + uint32_t expectedIndex = 0u; + auto expectedIterationStart = loopStart; + const auto iterationIncrement = (loopEnd - loopStart) / loopCount; + for (auto&& [index, loopPL] : pl.CreateLoopSubLoggersFromTo( + loopStart, loopEnd, loopCount, "looping...")) { + MOZ_RELEASE_ASSERT(index == expectedIndex); + ++expectedIndex; + MOZ_RELEASE_ASSERT( + AreAllEqual(progressRefPtr->Progress(), pl.GetGlobalProgress(), + loopPL.GetGlobalProgress(), expectedIterationStart)); + MOZ_RELEASE_ASSERT(AreAllEqual( + progressRefPtr->LastLocation(), pl.GetLastGlobalLocation(), + loopPL.GetLastGlobalLocation(), "looping...")); + + loopPL.SetLocalProgress(50_pc, "half"); + MOZ_RELEASE_ASSERT(loopPL.GetGlobalProgress() == + expectedIterationStart + iterationIncrement / 2u); + MOZ_RELEASE_ASSERT( + AreAllEqual(progressRefPtr->Progress(), pl.GetGlobalProgress(), + loopPL.GetGlobalProgress(), + expectedIterationStart + iterationIncrement / 2u)); + MOZ_RELEASE_ASSERT(AreAllEqual(progressRefPtr->LastLocation(), + pl.GetLastGlobalLocation(), + loopPL.GetLastGlobalLocation(), "half")); + + expectedIterationStart = expectedIterationStart + iterationIncrement; + } + MOZ_RELEASE_ASSERT(AreAllEqual(progressRefPtr->Progress(), + pl.GetGlobalProgress(), + expectedIterationStart)); + MOZ_RELEASE_ASSERT(AreAllEqual(progressRefPtr->LastLocation(), + pl.GetLastGlobalLocation(), "looping...")); + } // End of pl + MOZ_RELEASE_ASSERT(progressRefPtr->Progress().IsExactlyOne()); + MOZ_RELEASE_ASSERT(AreAllEqual(progressRefPtr->LastLocation(), "All done")); + + printf("TestProgressLogger done\n"); +} + +#ifdef MOZ_GECKO_PROFILER + +MOZ_MAYBE_UNUSED static void SleepMilli(unsigned aMilliseconds) { +# if defined(_MSC_VER) || defined(__MINGW32__) + Sleep(aMilliseconds); +# else + struct timespec ts = {/* .tv_sec */ static_cast<time_t>(aMilliseconds / 1000), + /* ts.tv_nsec */ long(aMilliseconds % 1000) * 1000000}; + struct timespec tr = {0, 0}; + while (nanosleep(&ts, &tr)) { + if (errno == EINTR) { + ts = tr; + } else { + printf("nanosleep() -> %s\n", strerror(errno)); + exit(1); + } + } +# endif +} + +MOZ_MAYBE_UNUSED static void WaitUntilTimeStampChanges( + const mozilla::TimeStamp& aTimeStampToCompare = mozilla::TimeStamp::Now()) { + while (aTimeStampToCompare == mozilla::TimeStamp::Now()) { + SleepMilli(1); + } +} + +using namespace mozilla; + +void TestPowerOfTwoMask() { + printf("TestPowerOfTwoMask...\n"); + + static_assert(MakePowerOfTwoMask<uint32_t, 0>().MaskValue() == 0); + constexpr PowerOfTwoMask<uint32_t> c0 = MakePowerOfTwoMask<uint32_t, 0>(); + MOZ_RELEASE_ASSERT(c0.MaskValue() == 0); + + static_assert(MakePowerOfTwoMask<uint32_t, 0xFFu>().MaskValue() == 0xFFu); + constexpr PowerOfTwoMask<uint32_t> cFF = + MakePowerOfTwoMask<uint32_t, 0xFFu>(); + MOZ_RELEASE_ASSERT(cFF.MaskValue() == 0xFFu); + + static_assert(MakePowerOfTwoMask<uint32_t, 0xFFFFFFFFu>().MaskValue() == + 0xFFFFFFFFu); + constexpr PowerOfTwoMask<uint32_t> cFFFFFFFF = + MakePowerOfTwoMask<uint32_t, 0xFFFFFFFFu>(); + MOZ_RELEASE_ASSERT(cFFFFFFFF.MaskValue() == 0xFFFFFFFFu); + + struct TestDataU32 { + uint32_t mInput; + uint32_t mMask; + }; + // clang-format off + TestDataU32 tests[] = { + { 0, 0 }, + { 1, 1 }, + { 2, 3 }, + { 3, 3 }, + { 4, 7 }, + { 5, 7 }, + { (1u << 31) - 1, (1u << 31) - 1 }, + { (1u << 31), uint32_t(-1) }, + { (1u << 31) + 1, uint32_t(-1) }, + { uint32_t(-1), uint32_t(-1) } + }; + // clang-format on + for (const TestDataU32& test : tests) { + PowerOfTwoMask<uint32_t> p2m(test.mInput); + MOZ_RELEASE_ASSERT(p2m.MaskValue() == test.mMask); + for (const TestDataU32& inner : tests) { + if (p2m.MaskValue() != uint32_t(-1)) { + MOZ_RELEASE_ASSERT((inner.mInput % p2m) == + (inner.mInput % (p2m.MaskValue() + 1))); + } + MOZ_RELEASE_ASSERT((inner.mInput & p2m) == (inner.mInput % p2m)); + MOZ_RELEASE_ASSERT((p2m & inner.mInput) == (inner.mInput & p2m)); + } + } + + printf("TestPowerOfTwoMask done\n"); +} + +void TestPowerOfTwo() { + printf("TestPowerOfTwo...\n"); + + static_assert(MakePowerOfTwo<uint32_t, 1>().Value() == 1); + constexpr PowerOfTwo<uint32_t> c1 = MakePowerOfTwo<uint32_t, 1>(); + MOZ_RELEASE_ASSERT(c1.Value() == 1); + static_assert(MakePowerOfTwo<uint32_t, 1>().Mask().MaskValue() == 0); + + static_assert(MakePowerOfTwo<uint32_t, 128>().Value() == 128); + constexpr PowerOfTwo<uint32_t> c128 = MakePowerOfTwo<uint32_t, 128>(); + MOZ_RELEASE_ASSERT(c128.Value() == 128); + static_assert(MakePowerOfTwo<uint32_t, 128>().Mask().MaskValue() == 127); + + static_assert(MakePowerOfTwo<uint32_t, 0x80000000u>().Value() == 0x80000000u); + constexpr PowerOfTwo<uint32_t> cMax = MakePowerOfTwo<uint32_t, 0x80000000u>(); + MOZ_RELEASE_ASSERT(cMax.Value() == 0x80000000u); + static_assert(MakePowerOfTwo<uint32_t, 0x80000000u>().Mask().MaskValue() == + 0x7FFFFFFFu); + + struct TestDataU32 { + uint32_t mInput; + uint32_t mValue; + uint32_t mMask; + }; + // clang-format off + TestDataU32 tests[] = { + { 0, 1, 0 }, + { 1, 1, 0 }, + { 2, 2, 1 }, + { 3, 4, 3 }, + { 4, 4, 3 }, + { 5, 8, 7 }, + { (1u << 31) - 1, (1u << 31), (1u << 31) - 1 }, + { (1u << 31), (1u << 31), (1u << 31) - 1 }, + { (1u << 31) + 1, (1u << 31), (1u << 31) - 1 }, + { uint32_t(-1), (1u << 31), (1u << 31) - 1 } + }; + // clang-format on + for (const TestDataU32& test : tests) { + PowerOfTwo<uint32_t> p2(test.mInput); + MOZ_RELEASE_ASSERT(p2.Value() == test.mValue); + MOZ_RELEASE_ASSERT(p2.MaskValue() == test.mMask); + PowerOfTwoMask<uint32_t> p2m = p2.Mask(); + MOZ_RELEASE_ASSERT(p2m.MaskValue() == test.mMask); + for (const TestDataU32& inner : tests) { + MOZ_RELEASE_ASSERT((inner.mInput % p2) == (inner.mInput % p2.Value())); + } + } + + printf("TestPowerOfTwo done\n"); +} + +void TestLEB128() { + printf("TestLEB128...\n"); + + MOZ_RELEASE_ASSERT(ULEB128MaxSize<uint8_t>() == 2); + MOZ_RELEASE_ASSERT(ULEB128MaxSize<uint16_t>() == 3); + MOZ_RELEASE_ASSERT(ULEB128MaxSize<uint32_t>() == 5); + MOZ_RELEASE_ASSERT(ULEB128MaxSize<uint64_t>() == 10); + + struct TestDataU64 { + uint64_t mValue; + unsigned mSize; + const char* mBytes; + }; + // clang-format off + TestDataU64 tests[] = { + // Small numbers should keep their normal byte representation. + { 0u, 1, "\0" }, + { 1u, 1, "\x01" }, + + // 0111 1111 (127, or 0x7F) is the highest number that fits into a single + // LEB128 byte. It gets encoded as 0111 1111, note the most significant bit + // is off. + { 0x7Fu, 1, "\x7F" }, + + // Next number: 128, or 0x80. + // Original data representation: 1000 0000 + // Broken up into groups of 7: 1 0000000 + // Padded with 0 (msB) or 1 (lsB): 00000001 10000000 + // Byte representation: 0x01 0x80 + // Little endian order: -> 0x80 0x01 + { 0x80u, 2, "\x80\x01" }, + + // Next: 129, or 0x81 (showing that we don't lose low bits.) + // Original data representation: 1000 0001 + // Broken up into groups of 7: 1 0000001 + // Padded with 0 (msB) or 1 (lsB): 00000001 10000001 + // Byte representation: 0x01 0x81 + // Little endian order: -> 0x81 0x01 + { 0x81u, 2, "\x81\x01" }, + + // Highest 8-bit number: 255, or 0xFF. + // Original data representation: 1111 1111 + // Broken up into groups of 7: 1 1111111 + // Padded with 0 (msB) or 1 (lsB): 00000001 11111111 + // Byte representation: 0x01 0xFF + // Little endian order: -> 0xFF 0x01 + { 0xFFu, 2, "\xFF\x01" }, + + // Next: 256, or 0x100. + // Original data representation: 1 0000 0000 + // Broken up into groups of 7: 10 0000000 + // Padded with 0 (msB) or 1 (lsB): 00000010 10000000 + // Byte representation: 0x10 0x80 + // Little endian order: -> 0x80 0x02 + { 0x100u, 2, "\x80\x02" }, + + // Highest 32-bit number: 0xFFFFFFFF (8 bytes, all bits set). + // Original: 1111 1111 1111 1111 1111 1111 1111 1111 + // Groups: 1111 1111111 1111111 1111111 1111111 + // Padded: 00001111 11111111 11111111 11111111 11111111 + // Bytes: 0x0F 0xFF 0xFF 0xFF 0xFF + // Little Endian: -> 0xFF 0xFF 0xFF 0xFF 0x0F + { 0xFFFFFFFFu, 5, "\xFF\xFF\xFF\xFF\x0F" }, + + // Highest 64-bit number: 0xFFFFFFFFFFFFFFFF (16 bytes, all bits set). + // 64 bits, that's 9 groups of 7 bits, plus 1 (most significant) bit. + { 0xFFFFFFFFFFFFFFFFu, 10, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01" } + }; + // clang-format on + + for (const TestDataU64& test : tests) { + MOZ_RELEASE_ASSERT(ULEB128Size(test.mValue) == test.mSize); + // Prepare a buffer that can accomodate the largest-possible LEB128. + uint8_t buffer[ULEB128MaxSize<uint64_t>()]; + // Use a pointer into the buffer as iterator. + uint8_t* p = buffer; + // And write the LEB128. + WriteULEB128(test.mValue, p); + // Pointer (iterator) should have advanced just past the expected LEB128 + // size. + MOZ_RELEASE_ASSERT(p == buffer + test.mSize); + // Check expected bytes. + for (unsigned i = 0; i < test.mSize; ++i) { + MOZ_RELEASE_ASSERT(buffer[i] == uint8_t(test.mBytes[i])); + } + + // Move pointer (iterator) back to start of buffer. + p = buffer; + // And read the LEB128 we wrote above. + uint64_t read = ReadULEB128<uint64_t>(p); + // Pointer (iterator) should have also advanced just past the expected + // LEB128 size. + MOZ_RELEASE_ASSERT(p == buffer + test.mSize); + // And check the read value. + MOZ_RELEASE_ASSERT(read == test.mValue); + + // Testing ULEB128 reader. + ULEB128Reader<uint64_t> reader; + MOZ_RELEASE_ASSERT(!reader.IsComplete()); + // Move pointer back to start of buffer. + p = buffer; + for (;;) { + // Read a byte and feed it to the reader. + if (reader.FeedByteIsComplete(*p++)) { + break; + } + // Not complete yet, we shouldn't have reached the end pointer. + MOZ_RELEASE_ASSERT(!reader.IsComplete()); + MOZ_RELEASE_ASSERT(p < buffer + test.mSize); + } + MOZ_RELEASE_ASSERT(reader.IsComplete()); + // Pointer should have advanced just past the expected LEB128 size. + MOZ_RELEASE_ASSERT(p == buffer + test.mSize); + // And check the read value. + MOZ_RELEASE_ASSERT(reader.Value() == test.mValue); + + // And again after a Reset. + reader.Reset(); + MOZ_RELEASE_ASSERT(!reader.IsComplete()); + p = buffer; + for (;;) { + if (reader.FeedByteIsComplete(*p++)) { + break; + } + MOZ_RELEASE_ASSERT(!reader.IsComplete()); + MOZ_RELEASE_ASSERT(p < buffer + test.mSize); + } + MOZ_RELEASE_ASSERT(reader.IsComplete()); + MOZ_RELEASE_ASSERT(p == buffer + test.mSize); + MOZ_RELEASE_ASSERT(reader.Value() == test.mValue); + } + + printf("TestLEB128 done\n"); +} + +struct StringWriteFunc final : public JSONWriteFunc { + std::string mString; + + void Write(const mozilla::Span<const char>& aStr) final { + mString.append(aStr.data(), aStr.size()); + } +}; + +void CheckJSON(mozilla::baseprofiler::SpliceableJSONWriter& aWriter, + const char* aExpected, int aLine) { + const std::string& actual = + static_cast<StringWriteFunc&>(aWriter.WriteFunc()).mString; + if (strcmp(aExpected, actual.c_str()) != 0) { + fprintf(stderr, + "---- EXPECTED ---- (line %d)\n<<<%s>>>\n" + "---- ACTUAL ----\n<<<%s>>>\n", + aLine, aExpected, actual.c_str()); + MOZ_RELEASE_ASSERT(false, "expected and actual output don't match"); + } +} + +void TestJSONTimeOutput() { + printf("TestJSONTimeOutput...\n"); + +# define TEST(in, out) \ + do { \ + mozilla::baseprofiler::SpliceableJSONWriter writer( \ + mozilla::MakeUnique<StringWriteFunc>(), \ + FailureLatchInfallibleSource::Singleton()); \ + writer.Start(); \ + writer.TimeDoubleMsProperty("time_ms", (in)); \ + writer.End(); \ + CheckJSON(writer, "{\"time_ms\":" out "}", __LINE__); \ + } while (false); + + TEST(0, "0"); + + TEST(0.000'000'1, "0"); + TEST(0.000'000'4, "0"); + TEST(0.000'000'499, "0"); + TEST(0.000'000'5, "0.000001"); + TEST(0.000'001, "0.000001"); + TEST(0.000'01, "0.00001"); + TEST(0.000'1, "0.0001"); + TEST(0.001, "0.001"); + TEST(0.01, "0.01"); + TEST(0.1, "0.1"); + TEST(1, "1"); + TEST(2, "2"); + TEST(10, "10"); + TEST(100, "100"); + TEST(1'000, "1000"); + TEST(10'000, "10000"); + TEST(100'000, "100000"); + TEST(1'000'000, "1000000"); + // 2^53-2 ns in ms. 2^53-1 is the highest integer value representable in + // double, -1 again because we're adding 0.5 before truncating. + // That's 104 days, after which the nanosecond precision would decrease. + TEST(9'007'199'254.740'990, "9007199254.74099"); + + TEST(-0.000'000'1, "0"); + TEST(-0.000'000'4, "0"); + TEST(-0.000'000'499, "0"); + TEST(-0.000'000'5, "-0.000001"); + TEST(-0.000'001, "-0.000001"); + TEST(-0.000'01, "-0.00001"); + TEST(-0.000'1, "-0.0001"); + TEST(-0.001, "-0.001"); + TEST(-0.01, "-0.01"); + TEST(-0.1, "-0.1"); + TEST(-1, "-1"); + TEST(-2, "-2"); + TEST(-10, "-10"); + TEST(-100, "-100"); + TEST(-1'000, "-1000"); + TEST(-10'000, "-10000"); + TEST(-100'000, "-100000"); + TEST(-1'000'000, "-1000000"); + TEST(-9'007'199'254.740'990, "-9007199254.74099"); + +# undef TEST + + printf("TestJSONTimeOutput done\n"); +} + +template <uint8_t byte, uint8_t... tail> +constexpr bool TestConstexprULEB128Reader(ULEB128Reader<uint64_t>& aReader) { + if (aReader.IsComplete()) { + return false; + } + const bool isComplete = aReader.FeedByteIsComplete(byte); + if (aReader.IsComplete() != isComplete) { + return false; + } + if constexpr (sizeof...(tail) == 0) { + return isComplete; + } else { + if (isComplete) { + return false; + } + return TestConstexprULEB128Reader<tail...>(aReader); + } +} + +template <uint64_t expected, uint8_t... bytes> +constexpr bool TestConstexprULEB128Reader() { + ULEB128Reader<uint64_t> reader; + if (!TestConstexprULEB128Reader<bytes...>(reader)) { + return false; + } + if (!reader.IsComplete()) { + return false; + } + if (reader.Value() != expected) { + return false; + } + + reader.Reset(); + if (!TestConstexprULEB128Reader<bytes...>(reader)) { + return false; + } + if (!reader.IsComplete()) { + return false; + } + if (reader.Value() != expected) { + return false; + } + + return true; +} + +static_assert(TestConstexprULEB128Reader<0x0u, 0x0u>()); +static_assert(!TestConstexprULEB128Reader<0x0u, 0x0u, 0x0u>()); +static_assert(TestConstexprULEB128Reader<0x1u, 0x1u>()); +static_assert(TestConstexprULEB128Reader<0x7Fu, 0x7Fu>()); +static_assert(TestConstexprULEB128Reader<0x80u, 0x80u, 0x01u>()); +static_assert(!TestConstexprULEB128Reader<0x80u, 0x80u>()); +static_assert(!TestConstexprULEB128Reader<0x80u, 0x01u>()); +static_assert(TestConstexprULEB128Reader<0x81u, 0x81u, 0x01u>()); +static_assert(TestConstexprULEB128Reader<0xFFu, 0xFFu, 0x01u>()); +static_assert(TestConstexprULEB128Reader<0x100u, 0x80u, 0x02u>()); +static_assert(TestConstexprULEB128Reader<0xFFFFFFFFu, 0xFFu, 0xFFu, 0xFFu, + 0xFFu, 0x0Fu>()); +static_assert( + !TestConstexprULEB128Reader<0xFFFFFFFFu, 0xFFu, 0xFFu, 0xFFu, 0xFFu>()); +static_assert(!TestConstexprULEB128Reader<0xFFFFFFFFu, 0xFFu, 0xFFu, 0xFFu, + 0xFFu, 0xFFu, 0x0Fu>()); +static_assert( + TestConstexprULEB128Reader<0xFFFFFFFFFFFFFFFFu, 0xFFu, 0xFFu, 0xFFu, 0xFFu, + 0xFFu, 0xFFu, 0xFFu, 0xFFu, 0xFFu, 0x01u>()); +static_assert( + !TestConstexprULEB128Reader<0xFFFFFFFFFFFFFFFFu, 0xFFu, 0xFFu, 0xFFu, 0xFFu, + 0xFFu, 0xFFu, 0xFFu, 0xFFu, 0xFFu>()); + +static void TestChunk() { + printf("TestChunk...\n"); + + static_assert(!std::is_default_constructible_v<ProfileBufferChunk>, + "ProfileBufferChunk should not be default-constructible"); + static_assert( + !std::is_constructible_v<ProfileBufferChunk, ProfileBufferChunk::Length>, + "ProfileBufferChunk should not be constructible from Length"); + + static_assert( + sizeof(ProfileBufferChunk::Header) == + sizeof(ProfileBufferChunk::Header::mOffsetFirstBlock) + + sizeof(ProfileBufferChunk::Header::mOffsetPastLastBlock) + + sizeof(ProfileBufferChunk::Header::mStartTimeStamp) + + sizeof(ProfileBufferChunk::Header::mDoneTimeStamp) + + sizeof(ProfileBufferChunk::Header::mBufferBytes) + + sizeof(ProfileBufferChunk::Header::mBlockCount) + + sizeof(ProfileBufferChunk::Header::mRangeStart) + + sizeof(ProfileBufferChunk::Header::mProcessId) + + sizeof(ProfileBufferChunk::Header::mPADDING), + "ProfileBufferChunk::Header may have unwanted padding, please review"); + // Note: The above static_assert is an attempt at keeping + // ProfileBufferChunk::Header tightly packed, but some changes could make this + // impossible to achieve (most probably due to alignment) -- Just do your + // best! + + constexpr ProfileBufferChunk::Length TestLen = 1000; + + // Basic allocations of different sizes. + for (ProfileBufferChunk::Length len = 0; len <= TestLen; ++len) { + auto chunk = ProfileBufferChunk::Create(len); + static_assert( + std::is_same_v<decltype(chunk), UniquePtr<ProfileBufferChunk>>, + "ProfileBufferChunk::Create() should return a " + "UniquePtr<ProfileBufferChunk>"); + MOZ_RELEASE_ASSERT(!!chunk, "OOM!?"); + MOZ_RELEASE_ASSERT(chunk->BufferBytes() >= len); + MOZ_RELEASE_ASSERT(chunk->ChunkBytes() >= + len + ProfileBufferChunk::SizeofChunkMetadata()); + MOZ_RELEASE_ASSERT(chunk->RemainingBytes() == chunk->BufferBytes()); + MOZ_RELEASE_ASSERT(chunk->OffsetFirstBlock() == 0); + MOZ_RELEASE_ASSERT(chunk->OffsetPastLastBlock() == 0); + MOZ_RELEASE_ASSERT(chunk->BlockCount() == 0); + MOZ_RELEASE_ASSERT(chunk->ProcessId() == 0); + MOZ_RELEASE_ASSERT(chunk->RangeStart() == 0); + MOZ_RELEASE_ASSERT(chunk->BufferSpan().LengthBytes() == + chunk->BufferBytes()); + MOZ_RELEASE_ASSERT(!chunk->GetNext()); + MOZ_RELEASE_ASSERT(!chunk->ReleaseNext()); + MOZ_RELEASE_ASSERT(chunk->Last() == chunk.get()); + } + + // Allocate the main test Chunk. + auto chunkA = ProfileBufferChunk::Create(TestLen); + MOZ_RELEASE_ASSERT(!!chunkA, "OOM!?"); + MOZ_RELEASE_ASSERT(chunkA->BufferBytes() >= TestLen); + MOZ_RELEASE_ASSERT(chunkA->ChunkBytes() >= + TestLen + ProfileBufferChunk::SizeofChunkMetadata()); + MOZ_RELEASE_ASSERT(!chunkA->GetNext()); + MOZ_RELEASE_ASSERT(!chunkA->ReleaseNext()); + + constexpr ProfileBufferIndex chunkARangeStart = 12345; + chunkA->SetRangeStart(chunkARangeStart); + MOZ_RELEASE_ASSERT(chunkA->RangeStart() == chunkARangeStart); + + // Get a read-only span over its buffer. + auto bufferA = chunkA->BufferSpan(); + static_assert( + std::is_same_v<decltype(bufferA), Span<const ProfileBufferChunk::Byte>>, + "BufferSpan() should return a Span<const Byte>"); + MOZ_RELEASE_ASSERT(bufferA.LengthBytes() == chunkA->BufferBytes()); + + // Add the initial tail block. + constexpr ProfileBufferChunk::Length initTailLen = 10; + auto initTail = chunkA->ReserveInitialBlockAsTail(initTailLen); + static_assert( + std::is_same_v<decltype(initTail), Span<ProfileBufferChunk::Byte>>, + "ReserveInitialBlockAsTail() should return a Span<Byte>"); + MOZ_RELEASE_ASSERT(initTail.LengthBytes() == initTailLen); + MOZ_RELEASE_ASSERT(initTail.Elements() == bufferA.Elements()); + MOZ_RELEASE_ASSERT(chunkA->OffsetFirstBlock() == initTailLen); + MOZ_RELEASE_ASSERT(chunkA->OffsetPastLastBlock() == initTailLen); + + // Add the first complete block. + constexpr ProfileBufferChunk::Length block1Len = 20; + auto block1 = chunkA->ReserveBlock(block1Len); + static_assert( + std::is_same_v<decltype(block1), ProfileBufferChunk::ReserveReturn>, + "ReserveBlock() should return a ReserveReturn"); + MOZ_RELEASE_ASSERT(block1.mBlockRangeIndex.ConvertToProfileBufferIndex() == + chunkARangeStart + initTailLen); + MOZ_RELEASE_ASSERT(block1.mSpan.LengthBytes() == block1Len); + MOZ_RELEASE_ASSERT(block1.mSpan.Elements() == + bufferA.Elements() + initTailLen); + MOZ_RELEASE_ASSERT(chunkA->OffsetFirstBlock() == initTailLen); + MOZ_RELEASE_ASSERT(chunkA->OffsetPastLastBlock() == initTailLen + block1Len); + MOZ_RELEASE_ASSERT(chunkA->RemainingBytes() != 0); + + // Add another block to over-fill the ProfileBufferChunk. + const ProfileBufferChunk::Length remaining = + chunkA->BufferBytes() - (initTailLen + block1Len); + constexpr ProfileBufferChunk::Length overfill = 30; + const ProfileBufferChunk::Length block2Len = remaining + overfill; + ProfileBufferChunk::ReserveReturn block2 = chunkA->ReserveBlock(block2Len); + MOZ_RELEASE_ASSERT(block2.mBlockRangeIndex.ConvertToProfileBufferIndex() == + chunkARangeStart + initTailLen + block1Len); + MOZ_RELEASE_ASSERT(block2.mSpan.LengthBytes() == remaining); + MOZ_RELEASE_ASSERT(block2.mSpan.Elements() == + bufferA.Elements() + initTailLen + block1Len); + MOZ_RELEASE_ASSERT(chunkA->OffsetFirstBlock() == initTailLen); + MOZ_RELEASE_ASSERT(chunkA->OffsetPastLastBlock() == chunkA->BufferBytes()); + MOZ_RELEASE_ASSERT(chunkA->RemainingBytes() == 0); + + // Block must be marked "done" before it can be recycled. + chunkA->MarkDone(); + + // It must be marked "recycled" before data can be added to it again. + chunkA->MarkRecycled(); + + // Add an empty initial tail block. + Span<ProfileBufferChunk::Byte> initTail2 = + chunkA->ReserveInitialBlockAsTail(0); + MOZ_RELEASE_ASSERT(initTail2.LengthBytes() == 0); + MOZ_RELEASE_ASSERT(initTail2.Elements() == bufferA.Elements()); + MOZ_RELEASE_ASSERT(chunkA->OffsetFirstBlock() == 0); + MOZ_RELEASE_ASSERT(chunkA->OffsetPastLastBlock() == 0); + + // Block must be marked "done" before it can be destroyed. + chunkA->MarkDone(); + + chunkA->SetProcessId(123); + MOZ_RELEASE_ASSERT(chunkA->ProcessId() == 123); + + printf("TestChunk done\n"); +} + +static void TestChunkManagerSingle() { + printf("TestChunkManagerSingle...\n"); + + // Construct a ProfileBufferChunkManagerSingle for one chunk of size >=1000. + constexpr ProfileBufferChunk::Length ChunkMinBufferBytes = 1000; + ProfileBufferChunkManagerSingle cms{ChunkMinBufferBytes}; + + // Reference to base class, to exercize virtual methods. + ProfileBufferChunkManager& cm = cms; + +# ifdef DEBUG + const char* chunkManagerRegisterer = "TestChunkManagerSingle"; + cm.RegisteredWith(chunkManagerRegisterer); +# endif // DEBUG + + const auto maxTotalSize = cm.MaxTotalSize(); + MOZ_RELEASE_ASSERT(maxTotalSize >= ChunkMinBufferBytes); + + cm.SetChunkDestroyedCallback([](const ProfileBufferChunk&) { + MOZ_RELEASE_ASSERT( + false, + "ProfileBufferChunkManagerSingle should never destroy its one chunk"); + }); + + UniquePtr<ProfileBufferChunk> extantReleasedChunks = + cm.GetExtantReleasedChunks(); + MOZ_RELEASE_ASSERT(!extantReleasedChunks, "Unexpected released chunk(s)"); + + // First request. + UniquePtr<ProfileBufferChunk> chunk = cm.GetChunk(); + MOZ_RELEASE_ASSERT(!!chunk, "First chunk request should always work"); + MOZ_RELEASE_ASSERT(chunk->BufferBytes() >= ChunkMinBufferBytes, + "Unexpected chunk size"); + MOZ_RELEASE_ASSERT(!chunk->GetNext(), "There should only be one chunk"); + + // Keep address, for later checks. + const uintptr_t chunkAddress = reinterpret_cast<uintptr_t>(chunk.get()); + + extantReleasedChunks = cm.GetExtantReleasedChunks(); + MOZ_RELEASE_ASSERT(!extantReleasedChunks, "Unexpected released chunk(s)"); + + // Second request. + MOZ_RELEASE_ASSERT(!cm.GetChunk(), "Second chunk request should always fail"); + + extantReleasedChunks = cm.GetExtantReleasedChunks(); + MOZ_RELEASE_ASSERT(!extantReleasedChunks, "Unexpected released chunk(s)"); + + // Add some data to the chunk (to verify recycling later on). + MOZ_RELEASE_ASSERT(chunk->ChunkHeader().mOffsetFirstBlock == 0); + MOZ_RELEASE_ASSERT(chunk->ChunkHeader().mOffsetPastLastBlock == 0); + MOZ_RELEASE_ASSERT(chunk->RangeStart() == 0); + chunk->SetRangeStart(100); + MOZ_RELEASE_ASSERT(chunk->RangeStart() == 100); + Unused << chunk->ReserveInitialBlockAsTail(1); + Unused << chunk->ReserveBlock(2); + MOZ_RELEASE_ASSERT(chunk->ChunkHeader().mOffsetFirstBlock == 1); + MOZ_RELEASE_ASSERT(chunk->ChunkHeader().mOffsetPastLastBlock == 1 + 2); + + // Release the first chunk. + chunk->MarkDone(); + cm.ReleaseChunk(std::move(chunk)); + MOZ_RELEASE_ASSERT(!chunk, "chunk UniquePtr should have been moved-from"); + + // Request after release. + MOZ_RELEASE_ASSERT(!cm.GetChunk(), + "Chunk request after release should also fail"); + + // Check released chunk. + extantReleasedChunks = cm.GetExtantReleasedChunks(); + MOZ_RELEASE_ASSERT(!!extantReleasedChunks, + "Could not retrieve released chunk"); + MOZ_RELEASE_ASSERT(!extantReleasedChunks->GetNext(), + "There should only be one released chunk"); + MOZ_RELEASE_ASSERT( + reinterpret_cast<uintptr_t>(extantReleasedChunks.get()) == chunkAddress, + "Released chunk should be first requested one"); + + MOZ_RELEASE_ASSERT(!cm.GetExtantReleasedChunks(), + "Unexpected extra released chunk(s)"); + + // Another request after release. + MOZ_RELEASE_ASSERT(!cm.GetChunk(), + "Chunk request after release should also fail"); + + MOZ_RELEASE_ASSERT( + cm.MaxTotalSize() == maxTotalSize, + "MaxTotalSize() should not change after requests&releases"); + + // Reset the chunk manager. (Single-only non-virtual function.) + cms.Reset(std::move(extantReleasedChunks)); + MOZ_RELEASE_ASSERT(!extantReleasedChunks, + "Released chunk UniquePtr should have been moved-from"); + + MOZ_RELEASE_ASSERT( + cm.MaxTotalSize() == maxTotalSize, + "MaxTotalSize() should not change when resetting with the same chunk"); + + // 2nd round, first request. Theoretically async, but this implementation just + // immediately runs the callback. + bool ran = false; + cm.RequestChunk([&](UniquePtr<ProfileBufferChunk> aChunk) { + ran = true; + MOZ_RELEASE_ASSERT(!!aChunk); + chunk = std::move(aChunk); + }); + MOZ_RELEASE_ASSERT(ran, "RequestChunk callback not called immediately"); + ran = false; + cm.FulfillChunkRequests(); + MOZ_RELEASE_ASSERT(!ran, "FulfillChunkRequests should not have any effects"); + MOZ_RELEASE_ASSERT(!!chunk, "First chunk request should always work"); + MOZ_RELEASE_ASSERT(chunk->BufferBytes() >= ChunkMinBufferBytes, + "Unexpected chunk size"); + MOZ_RELEASE_ASSERT(!chunk->GetNext(), "There should only be one chunk"); + MOZ_RELEASE_ASSERT(reinterpret_cast<uintptr_t>(chunk.get()) == chunkAddress, + "Requested chunk should be first requested one"); + // Verify that chunk is empty and usable. + MOZ_RELEASE_ASSERT(chunk->ChunkHeader().mOffsetFirstBlock == 0); + MOZ_RELEASE_ASSERT(chunk->ChunkHeader().mOffsetPastLastBlock == 0); + MOZ_RELEASE_ASSERT(chunk->RangeStart() == 0); + chunk->SetRangeStart(200); + MOZ_RELEASE_ASSERT(chunk->RangeStart() == 200); + Unused << chunk->ReserveInitialBlockAsTail(3); + Unused << chunk->ReserveBlock(4); + MOZ_RELEASE_ASSERT(chunk->ChunkHeader().mOffsetFirstBlock == 3); + MOZ_RELEASE_ASSERT(chunk->ChunkHeader().mOffsetPastLastBlock == 3 + 4); + + // Second request. + ran = false; + cm.RequestChunk([&](UniquePtr<ProfileBufferChunk> aChunk) { + ran = true; + MOZ_RELEASE_ASSERT(!aChunk, "Second chunk request should always fail"); + }); + MOZ_RELEASE_ASSERT(ran, "RequestChunk callback not called"); + + // This one does nothing. + cm.ForgetUnreleasedChunks(); + + // Don't forget to mark chunk "Done" before letting it die. + chunk->MarkDone(); + chunk = nullptr; + + // Create a tiny chunk and reset the chunk manager with it. + chunk = ProfileBufferChunk::Create(1); + MOZ_RELEASE_ASSERT(!!chunk); + auto tinyChunkSize = chunk->BufferBytes(); + MOZ_RELEASE_ASSERT(tinyChunkSize >= 1); + MOZ_RELEASE_ASSERT(tinyChunkSize < ChunkMinBufferBytes); + MOZ_RELEASE_ASSERT(chunk->RangeStart() == 0); + chunk->SetRangeStart(300); + MOZ_RELEASE_ASSERT(chunk->RangeStart() == 300); + cms.Reset(std::move(chunk)); + MOZ_RELEASE_ASSERT(!chunk, "chunk UniquePtr should have been moved-from"); + MOZ_RELEASE_ASSERT(cm.MaxTotalSize() == tinyChunkSize, + "MaxTotalSize() should match the new chunk size"); + chunk = cm.GetChunk(); + MOZ_RELEASE_ASSERT(chunk->RangeStart() == 0, "Got non-recycled chunk"); + + // Enough testing! Clean-up. + Unused << chunk->ReserveInitialBlockAsTail(0); + chunk->MarkDone(); + cm.ForgetUnreleasedChunks(); + +# ifdef DEBUG + cm.DeregisteredFrom(chunkManagerRegisterer); +# endif // DEBUG + + printf("TestChunkManagerSingle done\n"); +} + +static void TestChunkManagerWithLocalLimit() { + printf("TestChunkManagerWithLocalLimit...\n"); + + // Construct a ProfileBufferChunkManagerWithLocalLimit with chunk of minimum + // size >=100, up to 1000 bytes. + constexpr ProfileBufferChunk::Length MaxTotalBytes = 1000; + constexpr ProfileBufferChunk::Length ChunkMinBufferBytes = 100; + ProfileBufferChunkManagerWithLocalLimit cmll{MaxTotalBytes, + ChunkMinBufferBytes}; + + // Reference to base class, to exercize virtual methods. + ProfileBufferChunkManager& cm = cmll; + +# ifdef DEBUG + const char* chunkManagerRegisterer = "TestChunkManagerWithLocalLimit"; + cm.RegisteredWith(chunkManagerRegisterer); +# endif // DEBUG + + MOZ_RELEASE_ASSERT(cm.MaxTotalSize() == MaxTotalBytes, + "Max total size should be exactly as given"); + + unsigned destroyedChunks = 0; + unsigned destroyedBytes = 0; + cm.SetChunkDestroyedCallback([&](const ProfileBufferChunk& aChunks) { + for (const ProfileBufferChunk* chunk = &aChunks; chunk; + chunk = chunk->GetNext()) { + destroyedChunks += 1; + destroyedBytes += chunk->BufferBytes(); + } + }); + + UniquePtr<ProfileBufferChunk> extantReleasedChunks = + cm.GetExtantReleasedChunks(); + MOZ_RELEASE_ASSERT(!extantReleasedChunks, "Unexpected released chunk(s)"); + + // First request. + UniquePtr<ProfileBufferChunk> chunk = cm.GetChunk(); + MOZ_RELEASE_ASSERT(!!chunk, + "First chunk immediate request should always work"); + const auto chunkActualBufferBytes = chunk->BufferBytes(); + MOZ_RELEASE_ASSERT(chunkActualBufferBytes >= ChunkMinBufferBytes, + "Unexpected chunk size"); + MOZ_RELEASE_ASSERT(!chunk->GetNext(), "There should only be one chunk"); + + // Keep address, for later checks. + const uintptr_t chunk1Address = reinterpret_cast<uintptr_t>(chunk.get()); + + extantReleasedChunks = cm.GetExtantReleasedChunks(); + MOZ_RELEASE_ASSERT(!extantReleasedChunks, "Unexpected released chunk(s)"); + + // Verify that ReleaseChunk accepts zero chunks. + cm.ReleaseChunk(nullptr); + MOZ_RELEASE_ASSERT(!extantReleasedChunks, "Unexpected released chunk(s)"); + + // For this test, we need to be able to get at least 2 chunks without hitting + // the limit. (If this failed, it wouldn't necessary be a problem with + // ProfileBufferChunkManagerWithLocalLimit, fiddle with constants at the top + // of this test.) + MOZ_RELEASE_ASSERT(chunkActualBufferBytes < 2 * MaxTotalBytes); + + unsigned chunk1ReuseCount = 0; + + // We will do enough loops to go through the maximum size a number of times. + const unsigned Rollovers = 3; + const unsigned Loops = Rollovers * MaxTotalBytes / chunkActualBufferBytes; + for (unsigned i = 0; i < Loops; ++i) { + // Add some data to the chunk. + MOZ_RELEASE_ASSERT(chunk->ChunkHeader().mOffsetFirstBlock == 0); + MOZ_RELEASE_ASSERT(chunk->ChunkHeader().mOffsetPastLastBlock == 0); + MOZ_RELEASE_ASSERT(chunk->RangeStart() == 0); + const ProfileBufferIndex index = 1 + i * chunkActualBufferBytes; + chunk->SetRangeStart(index); + MOZ_RELEASE_ASSERT(chunk->RangeStart() == index); + Unused << chunk->ReserveInitialBlockAsTail(1); + Unused << chunk->ReserveBlock(2); + MOZ_RELEASE_ASSERT(chunk->ChunkHeader().mOffsetFirstBlock == 1); + MOZ_RELEASE_ASSERT(chunk->ChunkHeader().mOffsetPastLastBlock == 1 + 2); + + // Request a new chunk. + bool ran = false; + UniquePtr<ProfileBufferChunk> newChunk; + cm.RequestChunk([&](UniquePtr<ProfileBufferChunk> aChunk) { + ran = true; + newChunk = std::move(aChunk); + }); + MOZ_RELEASE_ASSERT( + !ran, "RequestChunk should not immediately fulfill the request"); + cm.FulfillChunkRequests(); + MOZ_RELEASE_ASSERT(ran, "FulfillChunkRequests should invoke the callback"); + MOZ_RELEASE_ASSERT(!!newChunk, "Chunk request should always work"); + MOZ_RELEASE_ASSERT(newChunk->BufferBytes() == chunkActualBufferBytes, + "Unexpected chunk size"); + MOZ_RELEASE_ASSERT(!newChunk->GetNext(), "There should only be one chunk"); + + // Mark previous chunk done and release it. + WaitUntilTimeStampChanges(); // Force "done" timestamp to change. + chunk->MarkDone(); + cm.ReleaseChunk(std::move(chunk)); + + // And cycle to the new chunk. + chunk = std::move(newChunk); + + if (reinterpret_cast<uintptr_t>(chunk.get()) == chunk1Address) { + ++chunk1ReuseCount; + } + } + + // Expect all rollovers except 1 to destroy chunks. + MOZ_RELEASE_ASSERT(destroyedChunks >= (Rollovers - 1) * MaxTotalBytes / + chunkActualBufferBytes, + "Not enough destroyed chunks"); + MOZ_RELEASE_ASSERT(destroyedBytes == destroyedChunks * chunkActualBufferBytes, + "Mismatched destroyed chunks and bytes"); + MOZ_RELEASE_ASSERT(chunk1ReuseCount >= (Rollovers - 1), + "Not enough reuse of the first chunks"); + + // Check that chunk manager is reentrant from request callback. + bool ran = false; + bool ranInner = false; + UniquePtr<ProfileBufferChunk> newChunk; + cm.RequestChunk([&](UniquePtr<ProfileBufferChunk> aChunk) { + ran = true; + MOZ_RELEASE_ASSERT(!!aChunk, "Chunk request should always work"); + Unused << aChunk->ReserveInitialBlockAsTail(0); + WaitUntilTimeStampChanges(); // Force "done" timestamp to change. + aChunk->MarkDone(); + UniquePtr<ProfileBufferChunk> anotherChunk = cm.GetChunk(); + MOZ_RELEASE_ASSERT(!!anotherChunk); + Unused << anotherChunk->ReserveInitialBlockAsTail(0); + WaitUntilTimeStampChanges(); // Force "done" timestamp to change. + anotherChunk->MarkDone(); + cm.RequestChunk([&](UniquePtr<ProfileBufferChunk> aChunk) { + ranInner = true; + MOZ_RELEASE_ASSERT(!!aChunk, "Chunk request should always work"); + Unused << aChunk->ReserveInitialBlockAsTail(0); + WaitUntilTimeStampChanges(); // Force "done" timestamp to change. + aChunk->MarkDone(); + }); + MOZ_RELEASE_ASSERT( + !ranInner, "RequestChunk should not immediately fulfill the request"); + }); + MOZ_RELEASE_ASSERT(!ran, + "RequestChunk should not immediately fulfill the request"); + MOZ_RELEASE_ASSERT( + !ranInner, + "RequestChunk should not immediately fulfill the inner request"); + cm.FulfillChunkRequests(); + MOZ_RELEASE_ASSERT(ran, "FulfillChunkRequests should invoke the callback"); + MOZ_RELEASE_ASSERT(!ranInner, + "FulfillChunkRequests should not immediately fulfill " + "the inner request"); + cm.FulfillChunkRequests(); + MOZ_RELEASE_ASSERT( + ran, "2nd FulfillChunkRequests should invoke the inner request callback"); + + // Enough testing! Clean-up. + Unused << chunk->ReserveInitialBlockAsTail(0); + WaitUntilTimeStampChanges(); // Force "done" timestamp to change. + chunk->MarkDone(); + cm.ForgetUnreleasedChunks(); + + // Special testing of the release algorithm, to make sure released chunks get + // sorted. + constexpr unsigned RandomReleaseChunkLoop = 100; + // Build a vector of chunks, and mark them "done", ready to be released. + Vector<UniquePtr<ProfileBufferChunk>> chunksToRelease; + MOZ_RELEASE_ASSERT(chunksToRelease.reserve(RandomReleaseChunkLoop)); + Vector<TimeStamp> chunksTimeStamps; + MOZ_RELEASE_ASSERT(chunksTimeStamps.reserve(RandomReleaseChunkLoop)); + for (unsigned i = 0; i < RandomReleaseChunkLoop; ++i) { + UniquePtr<ProfileBufferChunk> chunk = cm.GetChunk(); + MOZ_RELEASE_ASSERT(chunk); + Unused << chunk->ReserveInitialBlockAsTail(0); + chunk->MarkDone(); + MOZ_RELEASE_ASSERT(!chunk->ChunkHeader().mDoneTimeStamp.IsNull()); + chunksTimeStamps.infallibleEmplaceBack(chunk->ChunkHeader().mDoneTimeStamp); + chunksToRelease.infallibleEmplaceBack(std::move(chunk)); + if (i % 10 == 0) { + // "Done" timestamps should *usually* increase, let's make extra sure some + // timestamps are actually different. + WaitUntilTimeStampChanges(); + } + } + // Shuffle the list. + std::random_device randomDevice; + std::mt19937 generator(randomDevice()); + std::shuffle(chunksToRelease.begin(), chunksToRelease.end(), generator); + // And release chunks one by one, checking that the list of released chunks + // is always sorted. + printf("TestChunkManagerWithLocalLimit - Shuffle test timestamps:"); + for (unsigned i = 0; i < RandomReleaseChunkLoop; ++i) { + printf(" %f", (chunksToRelease[i]->ChunkHeader().mDoneTimeStamp - + TimeStamp::ProcessCreation()) + .ToMicroseconds()); + cm.ReleaseChunk(std::move(chunksToRelease[i])); + cm.PeekExtantReleasedChunks([i](const ProfileBufferChunk* releasedChunks) { + MOZ_RELEASE_ASSERT(releasedChunks); + unsigned releasedChunkCount = 1; + for (;;) { + const ProfileBufferChunk* nextChunk = releasedChunks->GetNext(); + if (!nextChunk) { + break; + } + ++releasedChunkCount; + MOZ_RELEASE_ASSERT(releasedChunks->ChunkHeader().mDoneTimeStamp <= + nextChunk->ChunkHeader().mDoneTimeStamp); + releasedChunks = nextChunk; + } + MOZ_RELEASE_ASSERT(releasedChunkCount == i + 1); + }); + } + printf("\n"); + // Finally, the whole list of released chunks should have the exact same + // timestamps as the initial list of "done" chunks. + extantReleasedChunks = cm.GetExtantReleasedChunks(); + for (unsigned i = 0; i < RandomReleaseChunkLoop; ++i) { + MOZ_RELEASE_ASSERT(extantReleasedChunks, "Not enough released chunks"); + MOZ_RELEASE_ASSERT(extantReleasedChunks->ChunkHeader().mDoneTimeStamp == + chunksTimeStamps[i]); + Unused << std::exchange(extantReleasedChunks, + extantReleasedChunks->ReleaseNext()); + } + MOZ_RELEASE_ASSERT(!extantReleasedChunks, "Too many released chunks"); + +# ifdef DEBUG + cm.DeregisteredFrom(chunkManagerRegisterer); +# endif // DEBUG + + printf("TestChunkManagerWithLocalLimit done\n"); +} + +static bool IsSameMetadata( + const ProfileBufferControlledChunkManager::ChunkMetadata& a1, + const ProfileBufferControlledChunkManager::ChunkMetadata& a2) { + return a1.mDoneTimeStamp == a2.mDoneTimeStamp && + a1.mBufferBytes == a2.mBufferBytes; +}; + +static bool IsSameUpdate( + const ProfileBufferControlledChunkManager::Update& a1, + const ProfileBufferControlledChunkManager::Update& a2) { + // Final and not-an-update don't carry other data, so we can test these two + // states first. + if (a1.IsFinal() || a2.IsFinal()) { + return a1.IsFinal() && a2.IsFinal(); + } + if (a1.IsNotUpdate() || a2.IsNotUpdate()) { + return a1.IsNotUpdate() && a2.IsNotUpdate(); + } + + // Here, both are "normal" udpates, check member variables: + + if (a1.UnreleasedBytes() != a2.UnreleasedBytes()) { + return false; + } + if (a1.ReleasedBytes() != a2.ReleasedBytes()) { + return false; + } + if (a1.OldestDoneTimeStamp() != a2.OldestDoneTimeStamp()) { + return false; + } + if (a1.NewlyReleasedChunksRef().size() != + a2.NewlyReleasedChunksRef().size()) { + return false; + } + for (unsigned i = 0; i < a1.NewlyReleasedChunksRef().size(); ++i) { + if (!IsSameMetadata(a1.NewlyReleasedChunksRef()[i], + a2.NewlyReleasedChunksRef()[i])) { + return false; + } + } + return true; +} + +static void TestControlledChunkManagerUpdate() { + printf("TestControlledChunkManagerUpdate...\n"); + + using Update = ProfileBufferControlledChunkManager::Update; + + // Default construction. + Update update1; + MOZ_RELEASE_ASSERT(update1.IsNotUpdate()); + MOZ_RELEASE_ASSERT(!update1.IsFinal()); + + // Clear an already-cleared update. + update1.Clear(); + MOZ_RELEASE_ASSERT(update1.IsNotUpdate()); + MOZ_RELEASE_ASSERT(!update1.IsFinal()); + + // Final construction with nullptr. + const Update final(nullptr); + MOZ_RELEASE_ASSERT(final.IsFinal()); + MOZ_RELEASE_ASSERT(!final.IsNotUpdate()); + + // Copy final to cleared. + update1 = final; + MOZ_RELEASE_ASSERT(update1.IsFinal()); + MOZ_RELEASE_ASSERT(!update1.IsNotUpdate()); + + // Copy final to final. + update1 = final; + MOZ_RELEASE_ASSERT(update1.IsFinal()); + MOZ_RELEASE_ASSERT(!update1.IsNotUpdate()); + + // Clear a final update. + update1.Clear(); + MOZ_RELEASE_ASSERT(update1.IsNotUpdate()); + MOZ_RELEASE_ASSERT(!update1.IsFinal()); + + // Move final to cleared. + update1 = Update(nullptr); + MOZ_RELEASE_ASSERT(update1.IsFinal()); + MOZ_RELEASE_ASSERT(!update1.IsNotUpdate()); + + // Move final to final. + update1 = Update(nullptr); + MOZ_RELEASE_ASSERT(update1.IsFinal()); + MOZ_RELEASE_ASSERT(!update1.IsNotUpdate()); + + // Move from not-an-update (effectively same as Clear). + update1 = Update(); + MOZ_RELEASE_ASSERT(update1.IsNotUpdate()); + MOZ_RELEASE_ASSERT(!update1.IsFinal()); + + auto CreateBiggerChunkAfter = [](const ProfileBufferChunk& aChunkToBeat) { + while (TimeStamp::Now() <= aChunkToBeat.ChunkHeader().mDoneTimeStamp) { + ::SleepMilli(1); + } + auto chunk = ProfileBufferChunk::Create(aChunkToBeat.BufferBytes() * 2); + MOZ_RELEASE_ASSERT(!!chunk); + MOZ_RELEASE_ASSERT(chunk->BufferBytes() >= aChunkToBeat.BufferBytes() * 2); + Unused << chunk->ReserveInitialBlockAsTail(0); + chunk->MarkDone(); + MOZ_RELEASE_ASSERT(chunk->ChunkHeader().mDoneTimeStamp > + aChunkToBeat.ChunkHeader().mDoneTimeStamp); + return chunk; + }; + + update1 = Update(1, 2, nullptr, nullptr); + + // Create initial update with 2 released chunks and 1 unreleased chunk. + auto released = ProfileBufferChunk::Create(10); + ProfileBufferChunk* c1 = released.get(); + Unused << c1->ReserveInitialBlockAsTail(0); + c1->MarkDone(); + + released->SetLast(CreateBiggerChunkAfter(*c1)); + ProfileBufferChunk* c2 = c1->GetNext(); + + auto unreleased = CreateBiggerChunkAfter(*c2); + ProfileBufferChunk* c3 = unreleased.get(); + + Update update2(c3->BufferBytes(), c1->BufferBytes() + c2->BufferBytes(), c1, + c1); + MOZ_RELEASE_ASSERT(IsSameUpdate( + update2, + Update(c3->BufferBytes(), c1->BufferBytes() + c2->BufferBytes(), + c1->ChunkHeader().mDoneTimeStamp, + {{c1->ChunkHeader().mDoneTimeStamp, c1->BufferBytes()}, + {c2->ChunkHeader().mDoneTimeStamp, c2->BufferBytes()}}))); + // Check every field, this time only, after that we'll trust that the + // `SameUpdate` test will be enough. + MOZ_RELEASE_ASSERT(!update2.IsNotUpdate()); + MOZ_RELEASE_ASSERT(!update2.IsFinal()); + MOZ_RELEASE_ASSERT(update2.UnreleasedBytes() == c3->BufferBytes()); + MOZ_RELEASE_ASSERT(update2.ReleasedBytes() == + c1->BufferBytes() + c2->BufferBytes()); + MOZ_RELEASE_ASSERT(update2.OldestDoneTimeStamp() == + c1->ChunkHeader().mDoneTimeStamp); + MOZ_RELEASE_ASSERT(update2.NewlyReleasedChunksRef().size() == 2); + MOZ_RELEASE_ASSERT( + IsSameMetadata(update2.NewlyReleasedChunksRef()[0], + {c1->ChunkHeader().mDoneTimeStamp, c1->BufferBytes()})); + MOZ_RELEASE_ASSERT( + IsSameMetadata(update2.NewlyReleasedChunksRef()[1], + {c2->ChunkHeader().mDoneTimeStamp, c2->BufferBytes()})); + + // Fold into not-an-update. + update1.Fold(std::move(update2)); + MOZ_RELEASE_ASSERT(IsSameUpdate( + update1, + Update(c3->BufferBytes(), c1->BufferBytes() + c2->BufferBytes(), + c1->ChunkHeader().mDoneTimeStamp, + {{c1->ChunkHeader().mDoneTimeStamp, c1->BufferBytes()}, + {c2->ChunkHeader().mDoneTimeStamp, c2->BufferBytes()}}))); + + // Pretend nothing happened. + update2 = Update(c3->BufferBytes(), c1->BufferBytes() + c2->BufferBytes(), c1, + nullptr); + MOZ_RELEASE_ASSERT(IsSameUpdate( + update2, Update(c3->BufferBytes(), c1->BufferBytes() + c2->BufferBytes(), + c1->ChunkHeader().mDoneTimeStamp, {}))); + update1.Fold(std::move(update2)); + MOZ_RELEASE_ASSERT(IsSameUpdate( + update1, + Update(c3->BufferBytes(), c1->BufferBytes() + c2->BufferBytes(), + c1->ChunkHeader().mDoneTimeStamp, + {{c1->ChunkHeader().mDoneTimeStamp, c1->BufferBytes()}, + {c2->ChunkHeader().mDoneTimeStamp, c2->BufferBytes()}}))); + + // Pretend there's a new unreleased chunk. + c3->SetLast(CreateBiggerChunkAfter(*c3)); + ProfileBufferChunk* c4 = c3->GetNext(); + update2 = Update(c3->BufferBytes() + c4->BufferBytes(), + c1->BufferBytes() + c2->BufferBytes(), c1, nullptr); + MOZ_RELEASE_ASSERT( + IsSameUpdate(update2, Update(c3->BufferBytes() + c4->BufferBytes(), + c1->BufferBytes() + c2->BufferBytes(), + c1->ChunkHeader().mDoneTimeStamp, {}))); + update1.Fold(std::move(update2)); + MOZ_RELEASE_ASSERT(IsSameUpdate( + update1, + Update(c3->BufferBytes() + c4->BufferBytes(), + c1->BufferBytes() + c2->BufferBytes(), + c1->ChunkHeader().mDoneTimeStamp, + {{c1->ChunkHeader().mDoneTimeStamp, c1->BufferBytes()}, + {c2->ChunkHeader().mDoneTimeStamp, c2->BufferBytes()}}))); + + // Pretend the first unreleased chunk c3 has been released. + released->SetLast(std::exchange(unreleased, unreleased->ReleaseNext())); + update2 = + Update(c4->BufferBytes(), + c1->BufferBytes() + c2->BufferBytes() + c3->BufferBytes(), c1, c3); + MOZ_RELEASE_ASSERT(IsSameUpdate( + update2, + Update(c4->BufferBytes(), + c1->BufferBytes() + c2->BufferBytes() + c3->BufferBytes(), + c1->ChunkHeader().mDoneTimeStamp, + {{c3->ChunkHeader().mDoneTimeStamp, c3->BufferBytes()}}))); + update1.Fold(std::move(update2)); + MOZ_RELEASE_ASSERT(IsSameUpdate( + update1, + Update(c4->BufferBytes(), + c1->BufferBytes() + c2->BufferBytes() + c3->BufferBytes(), + c1->ChunkHeader().mDoneTimeStamp, + {{c1->ChunkHeader().mDoneTimeStamp, c1->BufferBytes()}, + {c2->ChunkHeader().mDoneTimeStamp, c2->BufferBytes()}, + {c3->ChunkHeader().mDoneTimeStamp, c3->BufferBytes()}}))); + + // Pretend c1 has been destroyed, so the oldest timestamp is now at c2. + released = released->ReleaseNext(); + c1 = nullptr; + update2 = Update(c4->BufferBytes(), c2->BufferBytes() + c3->BufferBytes(), c2, + nullptr); + MOZ_RELEASE_ASSERT(IsSameUpdate( + update2, Update(c4->BufferBytes(), c2->BufferBytes() + c3->BufferBytes(), + c2->ChunkHeader().mDoneTimeStamp, {}))); + update1.Fold(std::move(update2)); + MOZ_RELEASE_ASSERT(IsSameUpdate( + update1, + Update(c4->BufferBytes(), c2->BufferBytes() + c3->BufferBytes(), + c2->ChunkHeader().mDoneTimeStamp, + {{c2->ChunkHeader().mDoneTimeStamp, c2->BufferBytes()}, + {c3->ChunkHeader().mDoneTimeStamp, c3->BufferBytes()}}))); + + // Pretend c2 has been recycled to make unreleased c5, and c4 has been + // released. + auto recycled = std::exchange(released, released->ReleaseNext()); + recycled->MarkRecycled(); + Unused << recycled->ReserveInitialBlockAsTail(0); + recycled->MarkDone(); + released->SetLast(std::move(unreleased)); + unreleased = std::move(recycled); + ProfileBufferChunk* c5 = c2; + c2 = nullptr; + update2 = + Update(c5->BufferBytes(), c3->BufferBytes() + c4->BufferBytes(), c3, c4); + MOZ_RELEASE_ASSERT(IsSameUpdate( + update2, + Update(c5->BufferBytes(), c3->BufferBytes() + c4->BufferBytes(), + c3->ChunkHeader().mDoneTimeStamp, + {{c4->ChunkHeader().mDoneTimeStamp, c4->BufferBytes()}}))); + update1.Fold(std::move(update2)); + MOZ_RELEASE_ASSERT(IsSameUpdate( + update1, + Update(c5->BufferBytes(), c3->BufferBytes() + c4->BufferBytes(), + c3->ChunkHeader().mDoneTimeStamp, + {{c3->ChunkHeader().mDoneTimeStamp, c3->BufferBytes()}, + {c4->ChunkHeader().mDoneTimeStamp, c4->BufferBytes()}}))); + + // And send a final update. + update1.Fold(Update(nullptr)); + MOZ_RELEASE_ASSERT(update1.IsFinal()); + MOZ_RELEASE_ASSERT(!update1.IsNotUpdate()); + + printf("TestControlledChunkManagerUpdate done\n"); +} + +static void TestControlledChunkManagerWithLocalLimit() { + printf("TestControlledChunkManagerWithLocalLimit...\n"); + + // Construct a ProfileBufferChunkManagerWithLocalLimit with chunk of minimum + // size >=100, up to 1000 bytes. + constexpr ProfileBufferChunk::Length MaxTotalBytes = 1000; + constexpr ProfileBufferChunk::Length ChunkMinBufferBytes = 100; + ProfileBufferChunkManagerWithLocalLimit cmll{MaxTotalBytes, + ChunkMinBufferBytes}; + + // Reference to chunk manager base class. + ProfileBufferChunkManager& cm = cmll; + + // Reference to controlled chunk manager base class. + ProfileBufferControlledChunkManager& ccm = cmll; + +# ifdef DEBUG + const char* chunkManagerRegisterer = + "TestControlledChunkManagerWithLocalLimit"; + cm.RegisteredWith(chunkManagerRegisterer); +# endif // DEBUG + + MOZ_RELEASE_ASSERT(cm.MaxTotalSize() == MaxTotalBytes, + "Max total size should be exactly as given"); + + unsigned destroyedChunks = 0; + unsigned destroyedBytes = 0; + cm.SetChunkDestroyedCallback([&](const ProfileBufferChunk& aChunks) { + for (const ProfileBufferChunk* chunk = &aChunks; chunk; + chunk = chunk->GetNext()) { + destroyedChunks += 1; + destroyedBytes += chunk->BufferBytes(); + } + }); + + using Update = ProfileBufferControlledChunkManager::Update; + unsigned updateCount = 0; + ProfileBufferControlledChunkManager::Update update; + MOZ_RELEASE_ASSERT(update.IsNotUpdate()); + auto updateCallback = [&](Update&& aUpdate) { + ++updateCount; + update.Fold(std::move(aUpdate)); + }; + ccm.SetUpdateCallback(updateCallback); + MOZ_RELEASE_ASSERT(updateCount == 1, + "SetUpdateCallback should have triggered an update"); + MOZ_RELEASE_ASSERT(IsSameUpdate(update, Update(0, 0, TimeStamp{}, {}))); + updateCount = 0; + update.Clear(); + + UniquePtr<ProfileBufferChunk> extantReleasedChunks = + cm.GetExtantReleasedChunks(); + MOZ_RELEASE_ASSERT(!extantReleasedChunks, "Unexpected released chunk(s)"); + MOZ_RELEASE_ASSERT(updateCount == 1, + "GetExtantReleasedChunks should have triggered an update"); + MOZ_RELEASE_ASSERT(IsSameUpdate(update, Update(0, 0, TimeStamp{}, {}))); + updateCount = 0; + update.Clear(); + + // First request. + UniquePtr<ProfileBufferChunk> chunk = cm.GetChunk(); + MOZ_RELEASE_ASSERT(!!chunk, + "First chunk immediate request should always work"); + const auto chunkActualBufferBytes = chunk->BufferBytes(); + MOZ_RELEASE_ASSERT(updateCount == 1, + "GetChunk should have triggered an update"); + MOZ_RELEASE_ASSERT( + IsSameUpdate(update, Update(chunk->BufferBytes(), 0, TimeStamp{}, {}))); + updateCount = 0; + update.Clear(); + + extantReleasedChunks = cm.GetExtantReleasedChunks(); + MOZ_RELEASE_ASSERT(!extantReleasedChunks, "Unexpected released chunk(s)"); + MOZ_RELEASE_ASSERT(updateCount == 1, + "GetExtantReleasedChunks should have triggered an update"); + MOZ_RELEASE_ASSERT( + IsSameUpdate(update, Update(chunk->BufferBytes(), 0, TimeStamp{}, {}))); + updateCount = 0; + update.Clear(); + + // For this test, we need to be able to get at least 2 chunks without hitting + // the limit. (If this failed, it wouldn't necessary be a problem with + // ProfileBufferChunkManagerWithLocalLimit, fiddle with constants at the top + // of this test.) + MOZ_RELEASE_ASSERT(chunkActualBufferBytes < 2 * MaxTotalBytes); + + ProfileBufferChunk::Length previousUnreleasedBytes = chunk->BufferBytes(); + ProfileBufferChunk::Length previousReleasedBytes = 0; + TimeStamp previousOldestDoneTimeStamp; + + // We will do enough loops to go through the maximum size a number of times. + const unsigned Rollovers = 3; + const unsigned Loops = Rollovers * MaxTotalBytes / chunkActualBufferBytes; + for (unsigned i = 0; i < Loops; ++i) { + // Add some data to the chunk. + const ProfileBufferIndex index = + ProfileBufferIndex(chunkActualBufferBytes) * i + 1; + chunk->SetRangeStart(index); + Unused << chunk->ReserveInitialBlockAsTail(1); + Unused << chunk->ReserveBlock(2); + + // Request a new chunk. + UniquePtr<ProfileBufferChunk> newChunk; + cm.RequestChunk([&](UniquePtr<ProfileBufferChunk> aChunk) { + newChunk = std::move(aChunk); + }); + MOZ_RELEASE_ASSERT(updateCount == 0, + "RequestChunk() shouldn't have triggered an update"); + cm.FulfillChunkRequests(); + MOZ_RELEASE_ASSERT(!!newChunk, "Chunk request should always work"); + MOZ_RELEASE_ASSERT(newChunk->BufferBytes() == chunkActualBufferBytes, + "Unexpected chunk size"); + MOZ_RELEASE_ASSERT(!newChunk->GetNext(), "There should only be one chunk"); + + MOZ_RELEASE_ASSERT(updateCount == 1, + "FulfillChunkRequests() after a request should have " + "triggered an update"); + MOZ_RELEASE_ASSERT(!update.IsFinal()); + MOZ_RELEASE_ASSERT(!update.IsNotUpdate()); + MOZ_RELEASE_ASSERT(update.UnreleasedBytes() == + previousUnreleasedBytes + newChunk->BufferBytes()); + previousUnreleasedBytes = update.UnreleasedBytes(); + MOZ_RELEASE_ASSERT(update.ReleasedBytes() <= previousReleasedBytes); + previousReleasedBytes = update.ReleasedBytes(); + MOZ_RELEASE_ASSERT(previousOldestDoneTimeStamp.IsNull() || + update.OldestDoneTimeStamp() >= + previousOldestDoneTimeStamp); + previousOldestDoneTimeStamp = update.OldestDoneTimeStamp(); + MOZ_RELEASE_ASSERT(update.NewlyReleasedChunksRef().empty()); + updateCount = 0; + update.Clear(); + + // Make sure the "Done" timestamp below cannot be the same as from the + // previous loop. + const TimeStamp now = TimeStamp::Now(); + while (TimeStamp::Now() == now) { + ::SleepMilli(1); + } + + // Mark previous chunk done and release it. + WaitUntilTimeStampChanges(); // Force "done" timestamp to change. + chunk->MarkDone(); + const auto doneTimeStamp = chunk->ChunkHeader().mDoneTimeStamp; + const auto bufferBytes = chunk->BufferBytes(); + cm.ReleaseChunk(std::move(chunk)); + + MOZ_RELEASE_ASSERT(updateCount == 1, + "ReleaseChunk() should have triggered an update"); + MOZ_RELEASE_ASSERT(!update.IsFinal()); + MOZ_RELEASE_ASSERT(!update.IsNotUpdate()); + MOZ_RELEASE_ASSERT(update.UnreleasedBytes() == + previousUnreleasedBytes - bufferBytes); + previousUnreleasedBytes = update.UnreleasedBytes(); + MOZ_RELEASE_ASSERT(update.ReleasedBytes() == + previousReleasedBytes + bufferBytes); + previousReleasedBytes = update.ReleasedBytes(); + MOZ_RELEASE_ASSERT(previousOldestDoneTimeStamp.IsNull() || + update.OldestDoneTimeStamp() >= + previousOldestDoneTimeStamp); + previousOldestDoneTimeStamp = update.OldestDoneTimeStamp(); + MOZ_RELEASE_ASSERT(update.OldestDoneTimeStamp() <= doneTimeStamp); + MOZ_RELEASE_ASSERT(update.NewlyReleasedChunksRef().size() == 1); + MOZ_RELEASE_ASSERT(update.NewlyReleasedChunksRef()[0].mDoneTimeStamp == + doneTimeStamp); + MOZ_RELEASE_ASSERT(update.NewlyReleasedChunksRef()[0].mBufferBytes == + bufferBytes); + updateCount = 0; + update.Clear(); + + // And cycle to the new chunk. + chunk = std::move(newChunk); + } + + // Enough testing! Clean-up. + Unused << chunk->ReserveInitialBlockAsTail(0); + chunk->MarkDone(); + cm.ForgetUnreleasedChunks(); + MOZ_RELEASE_ASSERT( + updateCount == 1, + "ForgetUnreleasedChunks() should have triggered an update"); + MOZ_RELEASE_ASSERT(!update.IsFinal()); + MOZ_RELEASE_ASSERT(!update.IsNotUpdate()); + MOZ_RELEASE_ASSERT(update.UnreleasedBytes() == 0); + MOZ_RELEASE_ASSERT(update.ReleasedBytes() == previousReleasedBytes); + MOZ_RELEASE_ASSERT(update.NewlyReleasedChunksRef().empty() == 1); + updateCount = 0; + update.Clear(); + + ccm.SetUpdateCallback({}); + MOZ_RELEASE_ASSERT(updateCount == 1, + "SetUpdateCallback({}) should have triggered an update"); + MOZ_RELEASE_ASSERT(update.IsFinal()); + +# ifdef DEBUG + cm.DeregisteredFrom(chunkManagerRegisterer); +# endif // DEBUG + + printf("TestControlledChunkManagerWithLocalLimit done\n"); +} + +# define VERIFY_PCB_START_END_PUSHED_CLEARED_FAILED( \ + aProfileChunkedBuffer, aStart, aEnd, aPushed, aCleared, aFailed) \ + { \ + ProfileChunkedBuffer::State state = (aProfileChunkedBuffer).GetState(); \ + MOZ_RELEASE_ASSERT(state.mRangeStart == (aStart)); \ + MOZ_RELEASE_ASSERT(state.mRangeEnd == (aEnd)); \ + MOZ_RELEASE_ASSERT(state.mPushedBlockCount == (aPushed)); \ + MOZ_RELEASE_ASSERT(state.mClearedBlockCount == (aCleared)); \ + MOZ_RELEASE_ASSERT(state.mFailedPutBytes == (aFailed)); \ + } + +static void TestChunkedBuffer() { + printf("TestChunkedBuffer...\n"); + + ProfileBufferBlockIndex blockIndex; + MOZ_RELEASE_ASSERT(!blockIndex); + MOZ_RELEASE_ASSERT(blockIndex == nullptr); + + // Create an out-of-session ProfileChunkedBuffer. + ProfileChunkedBuffer cb(ProfileChunkedBuffer::ThreadSafety::WithMutex); + + MOZ_RELEASE_ASSERT(cb.BufferLength().isNothing()); + + VERIFY_PCB_START_END_PUSHED_CLEARED_FAILED(cb, 1, 1, 0, 0, 0); + + int result = 0; + result = cb.ReserveAndPut( + []() { + MOZ_RELEASE_ASSERT(false); + return 1; + }, + [](Maybe<ProfileBufferEntryWriter>& aEW) { return aEW ? 2 : 3; }); + MOZ_RELEASE_ASSERT(result == 3); + VERIFY_PCB_START_END_PUSHED_CLEARED_FAILED(cb, 1, 1, 0, 0, 0); + + result = 0; + result = cb.Put( + 1, [](Maybe<ProfileBufferEntryWriter>& aEW) { return aEW ? 1 : 2; }); + MOZ_RELEASE_ASSERT(result == 2); + VERIFY_PCB_START_END_PUSHED_CLEARED_FAILED(cb, 1, 1, 0, 0, 0); + + blockIndex = cb.PutFrom(&result, 1); + MOZ_RELEASE_ASSERT(!blockIndex); + VERIFY_PCB_START_END_PUSHED_CLEARED_FAILED(cb, 1, 1, 0, 0, 0); + + blockIndex = cb.PutObjects(123, result, "hello"); + MOZ_RELEASE_ASSERT(!blockIndex); + VERIFY_PCB_START_END_PUSHED_CLEARED_FAILED(cb, 1, 1, 0, 0, 0); + + blockIndex = cb.PutObject(123); + MOZ_RELEASE_ASSERT(!blockIndex); + VERIFY_PCB_START_END_PUSHED_CLEARED_FAILED(cb, 1, 1, 0, 0, 0); + + auto chunks = cb.GetAllChunks(); + static_assert(std::is_same_v<decltype(chunks), UniquePtr<ProfileBufferChunk>>, + "ProfileChunkedBuffer::GetAllChunks() should return a " + "UniquePtr<ProfileBufferChunk>"); + MOZ_RELEASE_ASSERT(!chunks, "Expected no chunks when out-of-session"); + + bool ran = false; + result = 0; + result = cb.Read([&](ProfileChunkedBuffer::Reader* aReader) { + ran = true; + MOZ_RELEASE_ASSERT(!aReader); + return 3; + }); + MOZ_RELEASE_ASSERT(ran); + MOZ_RELEASE_ASSERT(result == 3); + + cb.ReadEach([](ProfileBufferEntryReader&) { MOZ_RELEASE_ASSERT(false); }); + + result = 0; + result = cb.ReadAt(nullptr, [](Maybe<ProfileBufferEntryReader>&& er) { + MOZ_RELEASE_ASSERT(er.isNothing()); + return 4; + }); + MOZ_RELEASE_ASSERT(result == 4); + + // Use ProfileBufferChunkManagerWithLocalLimit, which will give away + // ProfileBufferChunks that can contain 128 bytes, using up to 1KB of memory + // (including usable 128 bytes and headers). + constexpr size_t bufferMaxSize = 1024; + constexpr ProfileChunkedBuffer::Length chunkMinSize = 128; + ProfileBufferChunkManagerWithLocalLimit cm(bufferMaxSize, chunkMinSize); + cb.SetChunkManager(cm); + VERIFY_PCB_START_END_PUSHED_CLEARED_FAILED(cb, 1, 1, 0, 0, 0); + + // Let the chunk manager fulfill the initial request for an extra chunk. + cm.FulfillChunkRequests(); + + MOZ_RELEASE_ASSERT(cm.MaxTotalSize() == bufferMaxSize); + MOZ_RELEASE_ASSERT(cb.BufferLength().isSome()); + MOZ_RELEASE_ASSERT(*cb.BufferLength() == bufferMaxSize); + VERIFY_PCB_START_END_PUSHED_CLEARED_FAILED(cb, 1, 1, 0, 0, 0); + + // Write an int with the main `ReserveAndPut` function. + const int test = 123; + ran = false; + blockIndex = nullptr; + bool success = cb.ReserveAndPut( + []() { return sizeof(test); }, + [&](Maybe<ProfileBufferEntryWriter>& aEW) { + ran = true; + if (!aEW) { + return false; + } + blockIndex = aEW->CurrentBlockIndex(); + MOZ_RELEASE_ASSERT(aEW->RemainingBytes() == sizeof(test)); + aEW->WriteObject(test); + MOZ_RELEASE_ASSERT(aEW->RemainingBytes() == 0); + return true; + }); + MOZ_RELEASE_ASSERT(ran); + MOZ_RELEASE_ASSERT(success); + MOZ_RELEASE_ASSERT(blockIndex.ConvertToProfileBufferIndex() == 1); + VERIFY_PCB_START_END_PUSHED_CLEARED_FAILED( + cb, 1, 1 + ULEB128Size(sizeof(test)) + sizeof(test), 1, 0, 0); + + ran = false; + result = 0; + result = cb.Read([&](ProfileChunkedBuffer::Reader* aReader) { + ran = true; + MOZ_RELEASE_ASSERT(!!aReader); + // begin() and end() should be at the range edges (verified above). + MOZ_RELEASE_ASSERT( + aReader->begin().CurrentBlockIndex().ConvertToProfileBufferIndex() == + 1); + MOZ_RELEASE_ASSERT( + aReader->end().CurrentBlockIndex().ConvertToProfileBufferIndex() == 0); + // Null ProfileBufferBlockIndex clamped to the beginning. + MOZ_RELEASE_ASSERT(aReader->At(nullptr) == aReader->begin()); + MOZ_RELEASE_ASSERT(aReader->At(blockIndex) == aReader->begin()); + // At(begin) same as begin(). + MOZ_RELEASE_ASSERT(aReader->At(aReader->begin().CurrentBlockIndex()) == + aReader->begin()); + // At(past block) same as end(). + MOZ_RELEASE_ASSERT( + aReader->At(ProfileBufferBlockIndex::CreateFromProfileBufferIndex( + 1 + 1 + sizeof(test))) == aReader->end()); + + size_t read = 0; + aReader->ForEach([&](ProfileBufferEntryReader& er) { + ++read; + MOZ_RELEASE_ASSERT(er.RemainingBytes() == sizeof(test)); + const auto value = er.ReadObject<decltype(test)>(); + MOZ_RELEASE_ASSERT(value == test); + MOZ_RELEASE_ASSERT(er.RemainingBytes() == 0); + }); + MOZ_RELEASE_ASSERT(read == 1); + + read = 0; + for (auto er : *aReader) { + static_assert(std::is_same_v<decltype(er), ProfileBufferEntryReader>, + "ProfileChunkedBuffer::Reader range-for should produce " + "ProfileBufferEntryReader objects"); + ++read; + MOZ_RELEASE_ASSERT(er.RemainingBytes() == sizeof(test)); + const auto value = er.ReadObject<decltype(test)>(); + MOZ_RELEASE_ASSERT(value == test); + MOZ_RELEASE_ASSERT(er.RemainingBytes() == 0); + }; + MOZ_RELEASE_ASSERT(read == 1); + return 5; + }); + MOZ_RELEASE_ASSERT(ran); + MOZ_RELEASE_ASSERT(result == 5); + + // Read the int directly from the ProfileChunkedBuffer, without block index. + size_t read = 0; + cb.ReadEach([&](ProfileBufferEntryReader& er) { + ++read; + MOZ_RELEASE_ASSERT(er.RemainingBytes() == sizeof(test)); + const auto value = er.ReadObject<decltype(test)>(); + MOZ_RELEASE_ASSERT(value == test); + MOZ_RELEASE_ASSERT(er.RemainingBytes() == 0); + }); + MOZ_RELEASE_ASSERT(read == 1); + + // Read the int directly from the ProfileChunkedBuffer, with block index. + read = 0; + blockIndex = nullptr; + cb.ReadEach( + [&](ProfileBufferEntryReader& er, ProfileBufferBlockIndex aBlockIndex) { + ++read; + MOZ_RELEASE_ASSERT(!!aBlockIndex); + MOZ_RELEASE_ASSERT(!blockIndex); + blockIndex = aBlockIndex; + MOZ_RELEASE_ASSERT(er.RemainingBytes() == sizeof(test)); + const auto value = er.ReadObject<decltype(test)>(); + MOZ_RELEASE_ASSERT(value == test); + MOZ_RELEASE_ASSERT(er.RemainingBytes() == 0); + }); + MOZ_RELEASE_ASSERT(read == 1); + MOZ_RELEASE_ASSERT(!!blockIndex); + MOZ_RELEASE_ASSERT(blockIndex != nullptr); + + // Read the int from its block index. + read = 0; + result = 0; + result = cb.ReadAt(blockIndex, [&](Maybe<ProfileBufferEntryReader>&& er) { + ++read; + MOZ_RELEASE_ASSERT(er.isSome()); + MOZ_RELEASE_ASSERT(er->CurrentBlockIndex() == blockIndex); + MOZ_RELEASE_ASSERT(!er->NextBlockIndex()); + MOZ_RELEASE_ASSERT(er->RemainingBytes() == sizeof(test)); + const auto value = er->ReadObject<decltype(test)>(); + MOZ_RELEASE_ASSERT(value == test); + MOZ_RELEASE_ASSERT(er->RemainingBytes() == 0); + return 6; + }); + MOZ_RELEASE_ASSERT(result == 6); + MOZ_RELEASE_ASSERT(read == 1); + + MOZ_RELEASE_ASSERT(!cb.IsIndexInCurrentChunk(ProfileBufferIndex{})); + MOZ_RELEASE_ASSERT( + cb.IsIndexInCurrentChunk(blockIndex.ConvertToProfileBufferIndex())); + MOZ_RELEASE_ASSERT(cb.IsIndexInCurrentChunk(cb.GetState().mRangeEnd - 1)); + MOZ_RELEASE_ASSERT(!cb.IsIndexInCurrentChunk(cb.GetState().mRangeEnd)); + + // No changes after reads. + VERIFY_PCB_START_END_PUSHED_CLEARED_FAILED( + cb, 1, 1 + ULEB128Size(sizeof(test)) + sizeof(test), 1, 0, 0); + + // Steal the underlying ProfileBufferChunks from the ProfileChunkedBuffer. + chunks = cb.GetAllChunks(); + MOZ_RELEASE_ASSERT(!!chunks, "Expected at least one chunk"); + MOZ_RELEASE_ASSERT(!!chunks->GetNext(), "Expected two chunks"); + MOZ_RELEASE_ASSERT(!chunks->GetNext()->GetNext(), "Expected only two chunks"); + const ProfileChunkedBuffer::Length chunkActualSize = chunks->BufferBytes(); + MOZ_RELEASE_ASSERT(chunkActualSize >= chunkMinSize); + MOZ_RELEASE_ASSERT(chunks->RangeStart() == 1); + MOZ_RELEASE_ASSERT(chunks->OffsetFirstBlock() == 0); + MOZ_RELEASE_ASSERT(chunks->OffsetPastLastBlock() == 1 + sizeof(test)); + + // GetAllChunks() should have advanced the index one full chunk forward. + VERIFY_PCB_START_END_PUSHED_CLEARED_FAILED(cb, 1 + chunkActualSize, + 1 + chunkActualSize, 1, 0, 0); + + // Nothing more to read from the now-empty ProfileChunkedBuffer. + cb.ReadEach([](ProfileBufferEntryReader&) { MOZ_RELEASE_ASSERT(false); }); + cb.ReadEach([](ProfileBufferEntryReader&, ProfileBufferBlockIndex) { + MOZ_RELEASE_ASSERT(false); + }); + result = 0; + result = cb.ReadAt(nullptr, [](Maybe<ProfileBufferEntryReader>&& er) { + MOZ_RELEASE_ASSERT(er.isNothing()); + return 7; + }); + MOZ_RELEASE_ASSERT(result == 7); + + // Read the int from the stolen chunks. + read = 0; + ProfileChunkedBuffer::ReadEach( + chunks.get(), nullptr, + [&](ProfileBufferEntryReader& er, ProfileBufferBlockIndex aBlockIndex) { + ++read; + MOZ_RELEASE_ASSERT(aBlockIndex == blockIndex); + MOZ_RELEASE_ASSERT(er.RemainingBytes() == sizeof(test)); + const auto value = er.ReadObject<decltype(test)>(); + MOZ_RELEASE_ASSERT(value == test); + MOZ_RELEASE_ASSERT(er.RemainingBytes() == 0); + }); + MOZ_RELEASE_ASSERT(read == 1); + + // No changes after reads. + VERIFY_PCB_START_END_PUSHED_CLEARED_FAILED(cb, 1 + chunkActualSize, + 1 + chunkActualSize, 1, 0, 0); + + // Write lots of numbers (by memcpy), which should trigger Chunk destructions. + ProfileBufferBlockIndex firstBlockIndex; + MOZ_RELEASE_ASSERT(!firstBlockIndex); + ProfileBufferBlockIndex lastBlockIndex; + MOZ_RELEASE_ASSERT(!lastBlockIndex); + const size_t lots = 2 * bufferMaxSize / (1 + sizeof(int)); + for (size_t i = 1; i < lots; ++i) { + ProfileBufferBlockIndex blockIndex = cb.PutFrom(&i, sizeof(i)); + MOZ_RELEASE_ASSERT(!!blockIndex); + MOZ_RELEASE_ASSERT(blockIndex > firstBlockIndex); + if (!firstBlockIndex) { + firstBlockIndex = blockIndex; + } + MOZ_RELEASE_ASSERT(blockIndex > lastBlockIndex); + lastBlockIndex = blockIndex; + } + + ProfileChunkedBuffer::State stateAfterPuts = cb.GetState(); + ProfileBufferIndex startAfterPuts = stateAfterPuts.mRangeStart; + MOZ_RELEASE_ASSERT(startAfterPuts > 1 + chunkActualSize); + ProfileBufferIndex endAfterPuts = stateAfterPuts.mRangeEnd; + MOZ_RELEASE_ASSERT(endAfterPuts > startAfterPuts); + uint64_t pushedAfterPuts = stateAfterPuts.mPushedBlockCount; + MOZ_RELEASE_ASSERT(pushedAfterPuts > 0); + uint64_t clearedAfterPuts = stateAfterPuts.mClearedBlockCount; + MOZ_RELEASE_ASSERT(clearedAfterPuts > 0); + MOZ_RELEASE_ASSERT(stateAfterPuts.mFailedPutBytes == 0); + MOZ_RELEASE_ASSERT(!cb.IsIndexInCurrentChunk(ProfileBufferIndex{})); + MOZ_RELEASE_ASSERT( + !cb.IsIndexInCurrentChunk(blockIndex.ConvertToProfileBufferIndex())); + MOZ_RELEASE_ASSERT( + !cb.IsIndexInCurrentChunk(firstBlockIndex.ConvertToProfileBufferIndex())); + + // Read extant numbers, which should at least follow each other. + read = 0; + size_t i = 0; + cb.ReadEach( + [&](ProfileBufferEntryReader& er, ProfileBufferBlockIndex aBlockIndex) { + ++read; + MOZ_RELEASE_ASSERT(!!aBlockIndex); + MOZ_RELEASE_ASSERT(aBlockIndex > firstBlockIndex); + MOZ_RELEASE_ASSERT(aBlockIndex <= lastBlockIndex); + MOZ_RELEASE_ASSERT(er.RemainingBytes() == sizeof(size_t)); + const auto value = er.ReadObject<size_t>(); + if (i == 0) { + i = value; + } else { + MOZ_RELEASE_ASSERT(value == ++i); + } + MOZ_RELEASE_ASSERT(er.RemainingBytes() == 0); + }); + MOZ_RELEASE_ASSERT(read != 0); + MOZ_RELEASE_ASSERT(read < lots); + + // Read first extant number. + read = 0; + i = 0; + blockIndex = nullptr; + success = + cb.ReadAt(firstBlockIndex, [&](Maybe<ProfileBufferEntryReader>&& er) { + MOZ_ASSERT(er.isSome()); + ++read; + MOZ_RELEASE_ASSERT(er->CurrentBlockIndex() > firstBlockIndex); + MOZ_RELEASE_ASSERT(!!er->NextBlockIndex()); + MOZ_RELEASE_ASSERT(er->NextBlockIndex() > firstBlockIndex); + MOZ_RELEASE_ASSERT(er->NextBlockIndex() < lastBlockIndex); + blockIndex = er->NextBlockIndex(); + MOZ_RELEASE_ASSERT(er->RemainingBytes() == sizeof(size_t)); + const auto value = er->ReadObject<size_t>(); + MOZ_RELEASE_ASSERT(i == 0); + i = value; + MOZ_RELEASE_ASSERT(er->RemainingBytes() == 0); + return 7; + }); + MOZ_RELEASE_ASSERT(success); + MOZ_RELEASE_ASSERT(read == 1); + // Read other extant numbers one by one. + do { + bool success = + cb.ReadAt(blockIndex, [&](Maybe<ProfileBufferEntryReader>&& er) { + MOZ_ASSERT(er.isSome()); + ++read; + MOZ_RELEASE_ASSERT(er->CurrentBlockIndex() == blockIndex); + MOZ_RELEASE_ASSERT(!er->NextBlockIndex() || + er->NextBlockIndex() > blockIndex); + MOZ_RELEASE_ASSERT(!er->NextBlockIndex() || + er->NextBlockIndex() > firstBlockIndex); + MOZ_RELEASE_ASSERT(!er->NextBlockIndex() || + er->NextBlockIndex() <= lastBlockIndex); + MOZ_RELEASE_ASSERT(er->NextBlockIndex() + ? blockIndex < lastBlockIndex + : blockIndex == lastBlockIndex, + "er->NextBlockIndex() should only be null when " + "blockIndex is at the last block"); + blockIndex = er->NextBlockIndex(); + MOZ_RELEASE_ASSERT(er->RemainingBytes() == sizeof(size_t)); + const auto value = er->ReadObject<size_t>(); + MOZ_RELEASE_ASSERT(value == ++i); + MOZ_RELEASE_ASSERT(er->RemainingBytes() == 0); + return true; + }); + MOZ_RELEASE_ASSERT(success); + } while (blockIndex); + MOZ_RELEASE_ASSERT(read > 1); + + // No changes after reads. + VERIFY_PCB_START_END_PUSHED_CLEARED_FAILED( + cb, startAfterPuts, endAfterPuts, pushedAfterPuts, clearedAfterPuts, 0); + +# ifdef DEBUG + // cb.Dump(); +# endif + + cb.Clear(); + +# ifdef DEBUG + // cb.Dump(); +# endif + + ProfileChunkedBuffer::State stateAfterClear = cb.GetState(); + ProfileBufferIndex startAfterClear = stateAfterClear.mRangeStart; + MOZ_RELEASE_ASSERT(startAfterClear > startAfterPuts); + ProfileBufferIndex endAfterClear = stateAfterClear.mRangeEnd; + MOZ_RELEASE_ASSERT(endAfterClear == startAfterClear); + MOZ_RELEASE_ASSERT(stateAfterClear.mPushedBlockCount == 0); + MOZ_RELEASE_ASSERT(stateAfterClear.mClearedBlockCount == 0); + MOZ_RELEASE_ASSERT(stateAfterClear.mFailedPutBytes == 0); + MOZ_RELEASE_ASSERT(!cb.IsIndexInCurrentChunk(ProfileBufferIndex{})); + MOZ_RELEASE_ASSERT( + !cb.IsIndexInCurrentChunk(blockIndex.ConvertToProfileBufferIndex())); + MOZ_RELEASE_ASSERT(!cb.IsIndexInCurrentChunk(stateAfterClear.mRangeEnd - 1)); + MOZ_RELEASE_ASSERT(!cb.IsIndexInCurrentChunk(stateAfterClear.mRangeEnd)); + + // Start writer threads. + constexpr int ThreadCount = 32; + std::thread threads[ThreadCount]; + for (int threadNo = 0; threadNo < ThreadCount; ++threadNo) { + threads[threadNo] = std::thread( + [&](int aThreadNo) { + ::SleepMilli(1); + constexpr int pushCount = 1024; + for (int push = 0; push < pushCount; ++push) { + // Reserve as many bytes as the thread number (but at least enough + // to store an int), and write an increasing int. + const bool success = + cb.Put(std::max(aThreadNo, int(sizeof(push))), + [&](Maybe<ProfileBufferEntryWriter>& aEW) { + if (!aEW) { + return false; + } + aEW->WriteObject(aThreadNo * 1000000 + push); + // Advance writer to the end. + for (size_t r = aEW->RemainingBytes(); r != 0; --r) { + aEW->WriteObject<char>('_'); + } + return true; + }); + MOZ_RELEASE_ASSERT(success); + } + }, + threadNo); + } + + // Wait for all writer threads to die. + for (auto&& thread : threads) { + thread.join(); + } + +# ifdef DEBUG + // cb.Dump(); +# endif + + ProfileChunkedBuffer::State stateAfterMTPuts = cb.GetState(); + ProfileBufferIndex startAfterMTPuts = stateAfterMTPuts.mRangeStart; + MOZ_RELEASE_ASSERT(startAfterMTPuts > startAfterClear); + ProfileBufferIndex endAfterMTPuts = stateAfterMTPuts.mRangeEnd; + MOZ_RELEASE_ASSERT(endAfterMTPuts > startAfterMTPuts); + MOZ_RELEASE_ASSERT(stateAfterMTPuts.mPushedBlockCount > 0); + MOZ_RELEASE_ASSERT(stateAfterMTPuts.mClearedBlockCount > 0); + MOZ_RELEASE_ASSERT(stateAfterMTPuts.mFailedPutBytes == 0); + + // Reset to out-of-session. + cb.ResetChunkManager(); + + ProfileChunkedBuffer::State stateAfterReset = cb.GetState(); + ProfileBufferIndex startAfterReset = stateAfterReset.mRangeStart; + MOZ_RELEASE_ASSERT(startAfterReset == endAfterMTPuts); + ProfileBufferIndex endAfterReset = stateAfterReset.mRangeEnd; + MOZ_RELEASE_ASSERT(endAfterReset == startAfterReset); + MOZ_RELEASE_ASSERT(stateAfterReset.mPushedBlockCount == 0); + MOZ_RELEASE_ASSERT(stateAfterReset.mClearedBlockCount == 0); + MOZ_RELEASE_ASSERT(stateAfterReset.mFailedPutBytes == 0); + + success = cb.ReserveAndPut( + []() { + MOZ_RELEASE_ASSERT(false); + return 1; + }, + [](Maybe<ProfileBufferEntryWriter>& aEW) { return !!aEW; }); + MOZ_RELEASE_ASSERT(!success); + VERIFY_PCB_START_END_PUSHED_CLEARED_FAILED(cb, startAfterReset, endAfterReset, + 0, 0, 0); + + success = + cb.Put(1, [](Maybe<ProfileBufferEntryWriter>& aEW) { return !!aEW; }); + MOZ_RELEASE_ASSERT(!success); + VERIFY_PCB_START_END_PUSHED_CLEARED_FAILED(cb, startAfterReset, endAfterReset, + 0, 0, 0); + + blockIndex = cb.PutFrom(&success, 1); + MOZ_RELEASE_ASSERT(!blockIndex); + VERIFY_PCB_START_END_PUSHED_CLEARED_FAILED(cb, startAfterReset, endAfterReset, + 0, 0, 0); + + blockIndex = cb.PutObjects(123, success, "hello"); + MOZ_RELEASE_ASSERT(!blockIndex); + VERIFY_PCB_START_END_PUSHED_CLEARED_FAILED(cb, startAfterReset, endAfterReset, + 0, 0, 0); + + blockIndex = cb.PutObject(123); + MOZ_RELEASE_ASSERT(!blockIndex); + VERIFY_PCB_START_END_PUSHED_CLEARED_FAILED(cb, startAfterReset, endAfterReset, + 0, 0, 0); + + chunks = cb.GetAllChunks(); + MOZ_RELEASE_ASSERT(!chunks, "Expected no chunks when out-of-session"); + VERIFY_PCB_START_END_PUSHED_CLEARED_FAILED(cb, startAfterReset, endAfterReset, + 0, 0, 0); + + cb.ReadEach([](ProfileBufferEntryReader&) { MOZ_RELEASE_ASSERT(false); }); + VERIFY_PCB_START_END_PUSHED_CLEARED_FAILED(cb, startAfterReset, endAfterReset, + 0, 0, 0); + + success = cb.ReadAt(nullptr, [](Maybe<ProfileBufferEntryReader>&& er) { + MOZ_RELEASE_ASSERT(er.isNothing()); + return true; + }); + MOZ_RELEASE_ASSERT(success); + VERIFY_PCB_START_END_PUSHED_CLEARED_FAILED(cb, startAfterReset, endAfterReset, + 0, 0, 0); + + printf("TestChunkedBuffer done\n"); +} + +static void TestChunkedBufferSingle() { + printf("TestChunkedBufferSingle...\n"); + + constexpr ProfileChunkedBuffer::Length chunkMinSize = 128; + + // Create a ProfileChunkedBuffer that will own&use a + // ProfileBufferChunkManagerSingle, which will give away one + // ProfileBufferChunk that can contain 128 bytes. + ProfileChunkedBuffer cbSingle( + ProfileChunkedBuffer::ThreadSafety::WithoutMutex, + MakeUnique<ProfileBufferChunkManagerSingle>(chunkMinSize)); + + MOZ_RELEASE_ASSERT(cbSingle.BufferLength().isSome()); + const ProfileChunkedBuffer::Length bufferBytes = *cbSingle.BufferLength(); + MOZ_RELEASE_ASSERT(bufferBytes >= chunkMinSize); + + VERIFY_PCB_START_END_PUSHED_CLEARED_FAILED(cbSingle, 1, 1, 0, 0, 0); + + // We will write this many blocks to fill the chunk. + constexpr size_t testBlocks = 4; + const ProfileChunkedBuffer::Length blockBytes = bufferBytes / testBlocks; + MOZ_RELEASE_ASSERT(ULEB128Size(blockBytes) == 1, + "This test assumes block sizes are small enough so that " + "their ULEB128-encoded size is 1 byte"); + const ProfileChunkedBuffer::Length entryBytes = + blockBytes - ULEB128Size(blockBytes); + + // First buffer-filling test: Try to write a too-big entry at the end of the + // chunk. + + // Write all but one block. + for (size_t i = 0; i < testBlocks - 1; ++i) { + cbSingle.Put(entryBytes, [&](Maybe<ProfileBufferEntryWriter>& aEW) { + MOZ_RELEASE_ASSERT(aEW.isSome()); + while (aEW->RemainingBytes() > 0) { + **aEW = '0' + i; + ++(*aEW); + } + }); + VERIFY_PCB_START_END_PUSHED_CLEARED_FAILED( + cbSingle, 1, 1 + blockBytes * (i + 1), i + 1, 0, 0); + } + + // Write the last block so that it's too big (by 1 byte) to fit in the chunk, + // this should fail. + const ProfileChunkedBuffer::Length remainingBytesForLastBlock = + bufferBytes - blockBytes * (testBlocks - 1); + MOZ_RELEASE_ASSERT(ULEB128Size(remainingBytesForLastBlock) == 1, + "This test assumes block sizes are small enough so that " + "their ULEB128-encoded size is 1 byte"); + const ProfileChunkedBuffer::Length entryToFitRemainingBytes = + remainingBytesForLastBlock - ULEB128Size(remainingBytesForLastBlock); + cbSingle.Put(entryToFitRemainingBytes + 1, + [&](Maybe<ProfileBufferEntryWriter>& aEW) { + MOZ_RELEASE_ASSERT(aEW.isNothing()); + }); + // The buffer state should not have changed, apart from the failed bytes. + VERIFY_PCB_START_END_PUSHED_CLEARED_FAILED( + cbSingle, 1, 1 + blockBytes * (testBlocks - 1), testBlocks - 1, 0, + remainingBytesForLastBlock + 1); + + size_t read = 0; + cbSingle.ReadEach([&](ProfileBufferEntryReader& aER) { + MOZ_RELEASE_ASSERT(aER.RemainingBytes() == entryBytes); + while (aER.RemainingBytes() > 0) { + MOZ_RELEASE_ASSERT(*aER == '0' + read); + ++aER; + } + ++read; + }); + MOZ_RELEASE_ASSERT(read == testBlocks - 1); + + // ~Interlude~ Test AppendContent: + // Create another ProfileChunkedBuffer that will use a + // ProfileBufferChunkManagerWithLocalLimit, which will give away + // ProfileBufferChunks that can contain 128 bytes, using up to 1KB of memory + // (including usable 128 bytes and headers). + constexpr size_t bufferMaxSize = 1024; + ProfileBufferChunkManagerWithLocalLimit cmTarget(bufferMaxSize, chunkMinSize); + ProfileChunkedBuffer cbTarget(ProfileChunkedBuffer::ThreadSafety::WithMutex, + cmTarget); + + // It should start empty. + cbTarget.ReadEach( + [](ProfileBufferEntryReader&) { MOZ_RELEASE_ASSERT(false); }); + VERIFY_PCB_START_END_PUSHED_CLEARED_FAILED(cbTarget, 1, 1, 0, 0, 0); + + // Copy the contents from cbSingle to cbTarget. + cbTarget.AppendContents(cbSingle); + + // And verify that we now have the same contents in cbTarget. + read = 0; + cbTarget.ReadEach([&](ProfileBufferEntryReader& aER) { + MOZ_RELEASE_ASSERT(aER.RemainingBytes() == entryBytes); + while (aER.RemainingBytes() > 0) { + MOZ_RELEASE_ASSERT(*aER == '0' + read); + ++aER; + } + ++read; + }); + MOZ_RELEASE_ASSERT(read == testBlocks - 1); + // The state should be the same as the source. + VERIFY_PCB_START_END_PUSHED_CLEARED_FAILED( + cbTarget, 1, 1 + blockBytes * (testBlocks - 1), testBlocks - 1, 0, 0); + +# ifdef DEBUG + // cbSingle.Dump(); + // cbTarget.Dump(); +# endif + + // Because we failed to write a too-big chunk above, the chunk was marked + // full, so that entries should be consistently rejected from now on. + cbSingle.Put(1, [&](Maybe<ProfileBufferEntryWriter>& aEW) { + MOZ_RELEASE_ASSERT(aEW.isNothing()); + }); + VERIFY_PCB_START_END_PUSHED_CLEARED_FAILED( + cbSingle, 1, 1 + blockBytes * ((testBlocks - 1)), testBlocks - 1, 0, + remainingBytesForLastBlock + 1 + ULEB128Size(1u) + 1); + + // Clear the buffer before the next test. + + cbSingle.Clear(); + // Clear() should move the index to the next chunk range -- even if it's + // really reusing the same chunk. + VERIFY_PCB_START_END_PUSHED_CLEARED_FAILED(cbSingle, 1 + bufferBytes, + 1 + bufferBytes, 0, 0, 0); + cbSingle.ReadEach( + [&](ProfileBufferEntryReader& aER) { MOZ_RELEASE_ASSERT(false); }); + + // Second buffer-filling test: Try to write a final entry that just fits at + // the end of the chunk. + + // Write all but one block. + for (size_t i = 0; i < testBlocks - 1; ++i) { + cbSingle.Put(entryBytes, [&](Maybe<ProfileBufferEntryWriter>& aEW) { + MOZ_RELEASE_ASSERT(aEW.isSome()); + while (aEW->RemainingBytes() > 0) { + **aEW = 'a' + i; + ++(*aEW); + } + }); + VERIFY_PCB_START_END_PUSHED_CLEARED_FAILED( + cbSingle, 1 + bufferBytes, 1 + bufferBytes + blockBytes * (i + 1), + i + 1, 0, 0); + } + + read = 0; + cbSingle.ReadEach([&](ProfileBufferEntryReader& aER) { + MOZ_RELEASE_ASSERT(aER.RemainingBytes() == entryBytes); + while (aER.RemainingBytes() > 0) { + MOZ_RELEASE_ASSERT(*aER == 'a' + read); + ++aER; + } + ++read; + }); + MOZ_RELEASE_ASSERT(read == testBlocks - 1); + + // Write the last block so that it fits exactly in the chunk. + cbSingle.Put(entryToFitRemainingBytes, + [&](Maybe<ProfileBufferEntryWriter>& aEW) { + MOZ_RELEASE_ASSERT(aEW.isSome()); + while (aEW->RemainingBytes() > 0) { + **aEW = 'a' + (testBlocks - 1); + ++(*aEW); + } + }); + VERIFY_PCB_START_END_PUSHED_CLEARED_FAILED( + cbSingle, 1 + bufferBytes, 1 + bufferBytes + blockBytes * testBlocks, + testBlocks, 0, 0); + + read = 0; + cbSingle.ReadEach([&](ProfileBufferEntryReader& aER) { + MOZ_RELEASE_ASSERT( + aER.RemainingBytes() == + ((read < testBlocks) ? entryBytes : entryToFitRemainingBytes)); + while (aER.RemainingBytes() > 0) { + MOZ_RELEASE_ASSERT(*aER == 'a' + read); + ++aER; + } + ++read; + }); + MOZ_RELEASE_ASSERT(read == testBlocks); + + // Because the single chunk has been filled, it shouldn't be possible to write + // more entries. + cbSingle.Put(1, [&](Maybe<ProfileBufferEntryWriter>& aEW) { + MOZ_RELEASE_ASSERT(aEW.isNothing()); + }); + VERIFY_PCB_START_END_PUSHED_CLEARED_FAILED( + cbSingle, 1 + bufferBytes, 1 + bufferBytes + blockBytes * testBlocks, + testBlocks, 0, ULEB128Size(1u) + 1); + + cbSingle.Clear(); + // Clear() should move the index to the next chunk range -- even if it's + // really reusing the same chunk. + VERIFY_PCB_START_END_PUSHED_CLEARED_FAILED(cbSingle, 1 + bufferBytes * 2, + 1 + bufferBytes * 2, 0, 0, 0); + cbSingle.ReadEach( + [&](ProfileBufferEntryReader& aER) { MOZ_RELEASE_ASSERT(false); }); + + // Clear() recycles the released chunk, so we should be able to record new + // entries. + cbSingle.Put(entryBytes, [&](Maybe<ProfileBufferEntryWriter>& aEW) { + MOZ_RELEASE_ASSERT(aEW.isSome()); + while (aEW->RemainingBytes() > 0) { + **aEW = 'x'; + ++(*aEW); + } + }); + VERIFY_PCB_START_END_PUSHED_CLEARED_FAILED( + cbSingle, 1 + bufferBytes * 2, + 1 + bufferBytes * 2 + ULEB128Size(entryBytes) + entryBytes, 1, 0, 0); + read = 0; + cbSingle.ReadEach([&](ProfileBufferEntryReader& aER) { + MOZ_RELEASE_ASSERT(read == 0); + MOZ_RELEASE_ASSERT(aER.RemainingBytes() == entryBytes); + while (aER.RemainingBytes() > 0) { + MOZ_RELEASE_ASSERT(*aER == 'x'); + ++aER; + } + ++read; + }); + MOZ_RELEASE_ASSERT(read == 1); + + printf("TestChunkedBufferSingle done\n"); +} + +static void TestModuloBuffer(ModuloBuffer<>& mb, uint32_t MBSize) { + using MB = ModuloBuffer<>; + + MOZ_RELEASE_ASSERT(mb.BufferLength().Value() == MBSize); + + // Iterator comparisons. + MOZ_RELEASE_ASSERT(mb.ReaderAt(2) == mb.ReaderAt(2)); + MOZ_RELEASE_ASSERT(mb.ReaderAt(2) != mb.ReaderAt(3)); + MOZ_RELEASE_ASSERT(mb.ReaderAt(2) < mb.ReaderAt(3)); + MOZ_RELEASE_ASSERT(mb.ReaderAt(2) <= mb.ReaderAt(2)); + MOZ_RELEASE_ASSERT(mb.ReaderAt(2) <= mb.ReaderAt(3)); + MOZ_RELEASE_ASSERT(mb.ReaderAt(3) > mb.ReaderAt(2)); + MOZ_RELEASE_ASSERT(mb.ReaderAt(2) >= mb.ReaderAt(2)); + MOZ_RELEASE_ASSERT(mb.ReaderAt(3) >= mb.ReaderAt(2)); + + // Iterators indices don't wrap around (even though they may be pointing at + // the same location). + MOZ_RELEASE_ASSERT(mb.ReaderAt(2) != mb.ReaderAt(MBSize + 2)); + MOZ_RELEASE_ASSERT(mb.ReaderAt(MBSize + 2) != mb.ReaderAt(2)); + + // Dereference. + static_assert(std::is_same<decltype(*mb.ReaderAt(0)), const MB::Byte&>::value, + "Dereferencing from a reader should return const Byte*"); + static_assert(std::is_same<decltype(*mb.WriterAt(0)), MB::Byte&>::value, + "Dereferencing from a writer should return Byte*"); + // Contiguous between 0 and MBSize-1. + MOZ_RELEASE_ASSERT(&*mb.ReaderAt(MBSize - 1) == + &*mb.ReaderAt(0) + (MBSize - 1)); + // Wraps around. + MOZ_RELEASE_ASSERT(&*mb.ReaderAt(MBSize) == &*mb.ReaderAt(0)); + MOZ_RELEASE_ASSERT(&*mb.ReaderAt(MBSize + MBSize - 1) == + &*mb.ReaderAt(MBSize - 1)); + MOZ_RELEASE_ASSERT(&*mb.ReaderAt(MBSize + MBSize) == &*mb.ReaderAt(0)); + // Power of 2 modulo wrapping. + MOZ_RELEASE_ASSERT(&*mb.ReaderAt(uint32_t(-1)) == &*mb.ReaderAt(MBSize - 1)); + MOZ_RELEASE_ASSERT(&*mb.ReaderAt(static_cast<MB::Index>(-1)) == + &*mb.ReaderAt(MBSize - 1)); + + // Arithmetic. + MB::Reader arit = mb.ReaderAt(0); + MOZ_RELEASE_ASSERT(++arit == mb.ReaderAt(1)); + MOZ_RELEASE_ASSERT(arit == mb.ReaderAt(1)); + + MOZ_RELEASE_ASSERT(--arit == mb.ReaderAt(0)); + MOZ_RELEASE_ASSERT(arit == mb.ReaderAt(0)); + + MOZ_RELEASE_ASSERT(arit++ == mb.ReaderAt(0)); + MOZ_RELEASE_ASSERT(arit == mb.ReaderAt(1)); + + MOZ_RELEASE_ASSERT(arit-- == mb.ReaderAt(1)); + MOZ_RELEASE_ASSERT(arit == mb.ReaderAt(0)); + + MOZ_RELEASE_ASSERT(arit + 3 == mb.ReaderAt(3)); + MOZ_RELEASE_ASSERT(arit == mb.ReaderAt(0)); + + MOZ_RELEASE_ASSERT(4 + arit == mb.ReaderAt(4)); + MOZ_RELEASE_ASSERT(arit == mb.ReaderAt(0)); + + // (Can't have assignments inside asserts, hence the split.) + const bool checkPlusEq = ((arit += 3) == mb.ReaderAt(3)); + MOZ_RELEASE_ASSERT(checkPlusEq); + MOZ_RELEASE_ASSERT(arit == mb.ReaderAt(3)); + + MOZ_RELEASE_ASSERT((arit - 2) == mb.ReaderAt(1)); + MOZ_RELEASE_ASSERT(arit == mb.ReaderAt(3)); + + const bool checkMinusEq = ((arit -= 2) == mb.ReaderAt(1)); + MOZ_RELEASE_ASSERT(checkMinusEq); + MOZ_RELEASE_ASSERT(arit == mb.ReaderAt(1)); + + // Random access. + MOZ_RELEASE_ASSERT(&arit[3] == &*(arit + 3)); + MOZ_RELEASE_ASSERT(arit == mb.ReaderAt(1)); + + // Iterator difference. + MOZ_RELEASE_ASSERT(mb.ReaderAt(3) - mb.ReaderAt(1) == 2); + MOZ_RELEASE_ASSERT(mb.ReaderAt(1) - mb.ReaderAt(3) == MB::Index(-2)); + + // Only testing Writer, as Reader is just a subset with no code differences. + MB::Writer it = mb.WriterAt(0); + MOZ_RELEASE_ASSERT(it.CurrentIndex() == 0); + + // Write two characters at the start. + it.WriteObject('x'); + it.WriteObject('y'); + + // Backtrack to read them. + it -= 2; + // PeekObject should read without moving. + MOZ_RELEASE_ASSERT(it.PeekObject<char>() == 'x'); + MOZ_RELEASE_ASSERT(it.CurrentIndex() == 0); + // ReadObject should read and move past the character. + MOZ_RELEASE_ASSERT(it.ReadObject<char>() == 'x'); + MOZ_RELEASE_ASSERT(it.CurrentIndex() == 1); + MOZ_RELEASE_ASSERT(it.PeekObject<char>() == 'y'); + MOZ_RELEASE_ASSERT(it.CurrentIndex() == 1); + MOZ_RELEASE_ASSERT(it.ReadObject<char>() == 'y'); + MOZ_RELEASE_ASSERT(it.CurrentIndex() == 2); + + // Checking that a reader can be created from a writer. + MB::Reader it2(it); + MOZ_RELEASE_ASSERT(it2.CurrentIndex() == 2); + // Or assigned. + it2 = it; + MOZ_RELEASE_ASSERT(it2.CurrentIndex() == 2); + + // Iterator traits. + static_assert(std::is_same<std::iterator_traits<MB::Reader>::difference_type, + MB::Index>::value, + "ModuloBuffer::Reader::difference_type should be Index"); + static_assert(std::is_same<std::iterator_traits<MB::Reader>::value_type, + MB::Byte>::value, + "ModuloBuffer::Reader::value_type should be Byte"); + static_assert(std::is_same<std::iterator_traits<MB::Reader>::pointer, + const MB::Byte*>::value, + "ModuloBuffer::Reader::pointer should be const Byte*"); + static_assert(std::is_same<std::iterator_traits<MB::Reader>::reference, + const MB::Byte&>::value, + "ModuloBuffer::Reader::reference should be const Byte&"); + static_assert(std::is_base_of< + std::input_iterator_tag, + std::iterator_traits<MB::Reader>::iterator_category>::value, + "ModuloBuffer::Reader::iterator_category should be derived " + "from input_iterator_tag"); + static_assert(std::is_base_of< + std::forward_iterator_tag, + std::iterator_traits<MB::Reader>::iterator_category>::value, + "ModuloBuffer::Reader::iterator_category should be derived " + "from forward_iterator_tag"); + static_assert(std::is_base_of< + std::bidirectional_iterator_tag, + std::iterator_traits<MB::Reader>::iterator_category>::value, + "ModuloBuffer::Reader::iterator_category should be derived " + "from bidirectional_iterator_tag"); + static_assert( + std::is_same<std::iterator_traits<MB::Reader>::iterator_category, + std::random_access_iterator_tag>::value, + "ModuloBuffer::Reader::iterator_category should be " + "random_access_iterator_tag"); + + // Use as input iterator by std::string constructor (which is only considered + // with proper input iterators.) + std::string s(mb.ReaderAt(0), mb.ReaderAt(2)); + MOZ_RELEASE_ASSERT(s == "xy"); + + // Write 4-byte number at index 2. + it.WriteObject(int32_t(123)); + MOZ_RELEASE_ASSERT(it.CurrentIndex() == 6); + // And another, which should now wrap around (but index continues on.) + it.WriteObject(int32_t(456)); + MOZ_RELEASE_ASSERT(it.CurrentIndex() == MBSize + 2); + // Even though index==MBSize+2, we can read the object we wrote at 2. + MOZ_RELEASE_ASSERT(it.ReadObject<int32_t>() == 123); + MOZ_RELEASE_ASSERT(it.CurrentIndex() == MBSize + 6); + // And similarly, index MBSize+6 points at the same location as index 6. + MOZ_RELEASE_ASSERT(it.ReadObject<int32_t>() == 456); + MOZ_RELEASE_ASSERT(it.CurrentIndex() == MBSize + MBSize + 2); +} + +void TestModuloBuffer() { + printf("TestModuloBuffer...\n"); + + // Testing ModuloBuffer with default template arguments. + using MB = ModuloBuffer<>; + + // Only 8-byte buffers, to easily test wrap-around. + constexpr uint32_t MBSize = 8; + + // MB with self-allocated heap buffer. + MB mbByLength(MakePowerOfTwo32<MBSize>()); + TestModuloBuffer(mbByLength, MBSize); + + // MB taking ownership of a provided UniquePtr to a buffer. + auto uniqueBuffer = MakeUnique<uint8_t[]>(MBSize); + MB mbByUniquePtr(MakeUnique<uint8_t[]>(MBSize), MakePowerOfTwo32<MBSize>()); + TestModuloBuffer(mbByUniquePtr, MBSize); + + // MB using part of a buffer on the stack. The buffer is three times the + // required size: The middle third is where ModuloBuffer will work, the first + // and last thirds are only used to later verify that ModuloBuffer didn't go + // out of its bounds. + uint8_t buffer[MBSize * 3]; + // Pre-fill the buffer with a known pattern, so we can later see what changed. + for (size_t i = 0; i < MBSize * 3; ++i) { + buffer[i] = uint8_t('A' + i); + } + MB mbByBuffer(&buffer[MBSize], MakePowerOfTwo32<MBSize>()); + TestModuloBuffer(mbByBuffer, MBSize); + + // Check that only the provided stack-based sub-buffer was modified. + uint32_t changed = 0; + for (size_t i = MBSize; i < MBSize * 2; ++i) { + changed += (buffer[i] == uint8_t('A' + i)) ? 0 : 1; + } + // Expect at least 75% changes. + MOZ_RELEASE_ASSERT(changed >= MBSize * 6 / 8); + + // Everything around the sub-buffer should be unchanged. + for (size_t i = 0; i < MBSize; ++i) { + MOZ_RELEASE_ASSERT(buffer[i] == uint8_t('A' + i)); + } + for (size_t i = MBSize * 2; i < MBSize * 3; ++i) { + MOZ_RELEASE_ASSERT(buffer[i] == uint8_t('A' + i)); + } + + // Check that move-construction is allowed. This verifies that we do not + // crash from a double free, when `mbByBuffer` and `mbByStolenBuffer` are both + // destroyed at the end of this function. + MB mbByStolenBuffer = std::move(mbByBuffer); + TestModuloBuffer(mbByStolenBuffer, MBSize); + + // Check that only the provided stack-based sub-buffer was modified. + changed = 0; + for (size_t i = MBSize; i < MBSize * 2; ++i) { + changed += (buffer[i] == uint8_t('A' + i)) ? 0 : 1; + } + // Expect at least 75% changes. + MOZ_RELEASE_ASSERT(changed >= MBSize * 6 / 8); + + // Everything around the sub-buffer should be unchanged. + for (size_t i = 0; i < MBSize; ++i) { + MOZ_RELEASE_ASSERT(buffer[i] == uint8_t('A' + i)); + } + for (size_t i = MBSize * 2; i < MBSize * 3; ++i) { + MOZ_RELEASE_ASSERT(buffer[i] == uint8_t('A' + i)); + } + + // This test function does a `ReadInto` as directed, and checks that the + // result is the same as if the copy had been done manually byte-by-byte. + // `TestReadInto(3, 7, 2)` copies from index 3 to index 7, 2 bytes long. + // Return the output string (from `ReadInto`) for external checks. + auto TestReadInto = [](MB::Index aReadFrom, MB::Index aWriteTo, + MB::Length aBytes) { + constexpr uint32_t TRISize = 16; + + // Prepare an input buffer, all different elements. + uint8_t input[TRISize + 1] = "ABCDEFGHIJKLMNOP"; + const MB mbInput(input, MakePowerOfTwo32<TRISize>()); + + // Prepare an output buffer, different from input. + uint8_t output[TRISize + 1] = "abcdefghijklmnop"; + MB mbOutput(output, MakePowerOfTwo32<TRISize>()); + + // Run ReadInto. + auto writer = mbOutput.WriterAt(aWriteTo); + mbInput.ReaderAt(aReadFrom).ReadInto(writer, aBytes); + + // Do the same operation manually. + uint8_t outputCheck[TRISize + 1] = "abcdefghijklmnop"; + MB mbOutputCheck(outputCheck, MakePowerOfTwo32<TRISize>()); + auto readerCheck = mbInput.ReaderAt(aReadFrom); + auto writerCheck = mbOutputCheck.WriterAt(aWriteTo); + for (MB::Length i = 0; i < aBytes; ++i) { + *writerCheck++ = *readerCheck++; + } + + // Compare the two outputs. + for (uint32_t i = 0; i < TRISize; ++i) { +# ifdef TEST_MODULOBUFFER_FAILURE_DEBUG + // Only used when debugging failures. + if (output[i] != outputCheck[i]) { + printf( + "*** from=%u to=%u bytes=%u i=%u\ninput: '%s'\noutput: " + "'%s'\ncheck: '%s'\n", + unsigned(aReadFrom), unsigned(aWriteTo), unsigned(aBytes), + unsigned(i), input, output, outputCheck); + } +# endif + MOZ_RELEASE_ASSERT(output[i] == outputCheck[i]); + } + +# ifdef TEST_MODULOBUFFER_HELPER + // Only used when adding more tests. + printf("*** from=%u to=%u bytes=%u output: %s\n", unsigned(aReadFrom), + unsigned(aWriteTo), unsigned(aBytes), output); +# endif + + return std::string(reinterpret_cast<const char*>(output)); + }; + + // A few manual checks: + constexpr uint32_t TRISize = 16; + MOZ_RELEASE_ASSERT(TestReadInto(0, 0, 0) == "abcdefghijklmnop"); + MOZ_RELEASE_ASSERT(TestReadInto(0, 0, TRISize) == "ABCDEFGHIJKLMNOP"); + MOZ_RELEASE_ASSERT(TestReadInto(0, 5, TRISize) == "LMNOPABCDEFGHIJK"); + MOZ_RELEASE_ASSERT(TestReadInto(5, 0, TRISize) == "FGHIJKLMNOPABCDE"); + + // Test everything! (16^3 = 4096, not too much.) + for (MB::Index r = 0; r < TRISize; ++r) { + for (MB::Index w = 0; w < TRISize; ++w) { + for (MB::Length len = 0; len < TRISize; ++len) { + TestReadInto(r, w, len); + } + } + } + + printf("TestModuloBuffer done\n"); +} + +void TestBlocksRingBufferAPI() { + printf("TestBlocksRingBufferAPI...\n"); + + // Create a 16-byte buffer, enough to store up to 3 entries (1 byte size + 4 + // bytes uint64_t). + constexpr uint32_t MBSize = 16; + uint8_t buffer[MBSize * 3]; + for (size_t i = 0; i < MBSize * 3; ++i) { + buffer[i] = uint8_t('A' + i); + } + + // Start a temporary block to constrain buffer lifetime. + { + BlocksRingBuffer rb(BlocksRingBuffer::ThreadSafety::WithMutex, + &buffer[MBSize], MakePowerOfTwo32<MBSize>()); + +# define VERIFY_START_END_PUSHED_CLEARED(aStart, aEnd, aPushed, aCleared) \ + { \ + BlocksRingBuffer::State state = rb.GetState(); \ + MOZ_RELEASE_ASSERT(state.mRangeStart.ConvertToProfileBufferIndex() == \ + (aStart)); \ + MOZ_RELEASE_ASSERT(state.mRangeEnd.ConvertToProfileBufferIndex() == \ + (aEnd)); \ + MOZ_RELEASE_ASSERT(state.mPushedBlockCount == (aPushed)); \ + MOZ_RELEASE_ASSERT(state.mClearedBlockCount == (aCleared)); \ + } + + // All entries will contain one 32-bit number. The resulting blocks will + // have the following structure: + // - 1 byte for the LEB128 size of 4 + // - 4 bytes for the number. + // E.g., if we have entries with `123` and `456`: + // .-- Index 0 reserved for empty ProfileBufferBlockIndex, nothing there. + // | .-- first readable block at index 1 + // | |.-- first block at index 1 + // | ||.-- 1 byte for the entry size, which is `4` (32 bits) + // | ||| .-- entry starts at index 2, contains 32-bit int + // | ||| | .-- entry and block finish *after* index 5 (so 6) + // | ||| | | .-- second block starts at index 6 + // | ||| | | | etc. + // | ||| | | | .-- End readable blocks: 11 + // v vvv v v V v + // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 + // - S[4 | int(123) ] [4 | int(456) ]E + + // Empty buffer to start with. + // Start&end indices still at 1 (0 is reserved for the default + // ProfileBufferBlockIndex{} that cannot point at a valid entry), nothing + // cleared. + VERIFY_START_END_PUSHED_CLEARED(1, 1, 0, 0); + + // Default ProfileBufferBlockIndex. + ProfileBufferBlockIndex bi0; + if (bi0) { + MOZ_RELEASE_ASSERT(false, + "if (ProfileBufferBlockIndex{}) should fail test"); + } + if (!bi0) { + } else { + MOZ_RELEASE_ASSERT(false, + "if (!ProfileBufferBlockIndex{}) should succeed test"); + } + MOZ_RELEASE_ASSERT(!bi0); + MOZ_RELEASE_ASSERT(bi0 == bi0); + MOZ_RELEASE_ASSERT(bi0 <= bi0); + MOZ_RELEASE_ASSERT(bi0 >= bi0); + MOZ_RELEASE_ASSERT(!(bi0 != bi0)); + MOZ_RELEASE_ASSERT(!(bi0 < bi0)); + MOZ_RELEASE_ASSERT(!(bi0 > bi0)); + + // Default ProfileBufferBlockIndex can be used, but returns no valid entry. + rb.ReadAt(bi0, [](Maybe<ProfileBufferEntryReader>&& aMaybeReader) { + MOZ_RELEASE_ASSERT(aMaybeReader.isNothing()); + }); + + // Push `1` directly. + MOZ_RELEASE_ASSERT( + rb.PutObject(uint32_t(1)).ConvertToProfileBufferIndex() == 1); + // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 + // - S[4 | int(1) ]E + VERIFY_START_END_PUSHED_CLEARED(1, 6, 1, 0); + + // Push `2` through ReserveAndPut, check output ProfileBufferBlockIndex. + auto bi2 = rb.ReserveAndPut([]() { return sizeof(uint32_t); }, + [](Maybe<ProfileBufferEntryWriter>& aEW) { + MOZ_RELEASE_ASSERT(aEW.isSome()); + aEW->WriteObject(uint32_t(2)); + return aEW->CurrentBlockIndex(); + }); + static_assert(std::is_same<decltype(bi2), ProfileBufferBlockIndex>::value, + "All index-returning functions should return a " + "ProfileBufferBlockIndex"); + MOZ_RELEASE_ASSERT(bi2.ConvertToProfileBufferIndex() == 6); + // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 + // - S[4 | int(1) ] [4 | int(2) ]E + VERIFY_START_END_PUSHED_CLEARED(1, 11, 2, 0); + + // Check single entry at bi2, store next block index. + auto i2Next = + rb.ReadAt(bi2, [bi2](Maybe<ProfileBufferEntryReader>&& aMaybeReader) { + MOZ_RELEASE_ASSERT(aMaybeReader.isSome()); + MOZ_RELEASE_ASSERT(aMaybeReader->CurrentBlockIndex() == bi2); + MOZ_RELEASE_ASSERT(aMaybeReader->NextBlockIndex() == nullptr); + size_t entrySize = aMaybeReader->RemainingBytes(); + MOZ_RELEASE_ASSERT(aMaybeReader->ReadObject<uint32_t>() == 2); + // The next block index is after this block, which is made of the + // entry size (coded as ULEB128) followed by the entry itself. + return bi2.ConvertToProfileBufferIndex() + ULEB128Size(entrySize) + + entrySize; + }); + auto bi2Next = rb.GetState().mRangeEnd; + MOZ_RELEASE_ASSERT(bi2Next.ConvertToProfileBufferIndex() == i2Next); + // bi2Next is at the end, nothing to read. + rb.ReadAt(bi2Next, [](Maybe<ProfileBufferEntryReader>&& aMaybeReader) { + MOZ_RELEASE_ASSERT(aMaybeReader.isNothing()); + }); + + // ProfileBufferBlockIndex tests. + if (bi2) { + } else { + MOZ_RELEASE_ASSERT( + false, + "if (non-default-ProfileBufferBlockIndex) should succeed test"); + } + if (!bi2) { + MOZ_RELEASE_ASSERT( + false, "if (!non-default-ProfileBufferBlockIndex) should fail test"); + } + + MOZ_RELEASE_ASSERT(!!bi2); + MOZ_RELEASE_ASSERT(bi2 == bi2); + MOZ_RELEASE_ASSERT(bi2 <= bi2); + MOZ_RELEASE_ASSERT(bi2 >= bi2); + MOZ_RELEASE_ASSERT(!(bi2 != bi2)); + MOZ_RELEASE_ASSERT(!(bi2 < bi2)); + MOZ_RELEASE_ASSERT(!(bi2 > bi2)); + + MOZ_RELEASE_ASSERT(bi0 != bi2); + MOZ_RELEASE_ASSERT(bi0 < bi2); + MOZ_RELEASE_ASSERT(bi0 <= bi2); + MOZ_RELEASE_ASSERT(!(bi0 == bi2)); + MOZ_RELEASE_ASSERT(!(bi0 > bi2)); + MOZ_RELEASE_ASSERT(!(bi0 >= bi2)); + + MOZ_RELEASE_ASSERT(bi2 != bi0); + MOZ_RELEASE_ASSERT(bi2 > bi0); + MOZ_RELEASE_ASSERT(bi2 >= bi0); + MOZ_RELEASE_ASSERT(!(bi2 == bi0)); + MOZ_RELEASE_ASSERT(!(bi2 < bi0)); + MOZ_RELEASE_ASSERT(!(bi2 <= bi0)); + + MOZ_RELEASE_ASSERT(bi2 != bi2Next); + MOZ_RELEASE_ASSERT(bi2 < bi2Next); + MOZ_RELEASE_ASSERT(bi2 <= bi2Next); + MOZ_RELEASE_ASSERT(!(bi2 == bi2Next)); + MOZ_RELEASE_ASSERT(!(bi2 > bi2Next)); + MOZ_RELEASE_ASSERT(!(bi2 >= bi2Next)); + + MOZ_RELEASE_ASSERT(bi2Next != bi2); + MOZ_RELEASE_ASSERT(bi2Next > bi2); + MOZ_RELEASE_ASSERT(bi2Next >= bi2); + MOZ_RELEASE_ASSERT(!(bi2Next == bi2)); + MOZ_RELEASE_ASSERT(!(bi2Next < bi2)); + MOZ_RELEASE_ASSERT(!(bi2Next <= bi2)); + + // Push `3` through Put, check writer output + // is returned to the initial caller. + auto put3 = + rb.Put(sizeof(uint32_t), [&](Maybe<ProfileBufferEntryWriter>& aEW) { + MOZ_RELEASE_ASSERT(aEW.isSome()); + aEW->WriteObject(uint32_t(3)); + MOZ_RELEASE_ASSERT(aEW->CurrentBlockIndex() == bi2Next); + return float(aEW->CurrentBlockIndex().ConvertToProfileBufferIndex()); + }); + static_assert(std::is_same<decltype(put3), float>::value, + "Expect float as returned by callback."); + MOZ_RELEASE_ASSERT(put3 == 11.0); + // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 (16) + // - S[4 | int(1) ] [4 | int(2) ] [4 | int(3) ]E + VERIFY_START_END_PUSHED_CLEARED(1, 16, 3, 0); + + // Re-Read single entry at bi2, it should now have a next entry. + rb.ReadAt(bi2, [&](Maybe<ProfileBufferEntryReader>&& aMaybeReader) { + MOZ_RELEASE_ASSERT(aMaybeReader.isSome()); + MOZ_RELEASE_ASSERT(aMaybeReader->CurrentBlockIndex() == bi2); + MOZ_RELEASE_ASSERT(aMaybeReader->ReadObject<uint32_t>() == 2); + MOZ_RELEASE_ASSERT(aMaybeReader->NextBlockIndex() == bi2Next); + }); + + // Check that we have `1` to `3`. + uint32_t count = 0; + rb.ReadEach([&](ProfileBufferEntryReader& aReader) { + MOZ_RELEASE_ASSERT(aReader.ReadObject<uint32_t>() == ++count); + }); + MOZ_RELEASE_ASSERT(count == 3); + + // Push `4`, store its ProfileBufferBlockIndex for later. + // This will wrap around, and clear the first entry. + ProfileBufferBlockIndex bi4 = rb.PutObject(uint32_t(4)); + // Before: + // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 (16) + // - S[4 | int(1) ] [4 | int(2) ] [4 | int(3) ]E + // 1. First entry cleared: + // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 (16) + // - ? ? ? ? ? S[4 | int(2) ] [4 | int(3) ]E + // 2. New entry starts at 15 and wraps around: (shown on separate line) + // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 (16) + // - ? ? ? ? ? S[4 | int(2) ] [4 | int(3) ] + // 16 17 18 19 20 21 ... + // [4 | int(4) ]E + // (collapsed) + // 16 17 18 19 20 21 6 7 8 9 10 11 12 13 14 15 (16) + // [4 | int(4) ]E ? S[4 | int(2) ] [4 | int(3) ] + VERIFY_START_END_PUSHED_CLEARED(6, 21, 4, 1); + + // Check that we have `2` to `4`. + count = 1; + rb.ReadEach([&](ProfileBufferEntryReader& aReader) { + MOZ_RELEASE_ASSERT(aReader.ReadObject<uint32_t>() == ++count); + }); + MOZ_RELEASE_ASSERT(count == 4); + + // Push 5 through Put, no returns. + // This will clear the second entry. + // Check that the EntryWriter can access bi4 but not bi2. + auto bi5 = + rb.Put(sizeof(uint32_t), [&](Maybe<ProfileBufferEntryWriter>& aEW) { + MOZ_RELEASE_ASSERT(aEW.isSome()); + aEW->WriteObject(uint32_t(5)); + return aEW->CurrentBlockIndex(); + }); + auto bi6 = rb.GetState().mRangeEnd; + // 16 17 18 19 20 21 22 23 24 25 26 11 12 13 14 15 (16) + // [4 | int(4) ] [4 | int(5) ]E ? S[4 | int(3) ] + VERIFY_START_END_PUSHED_CLEARED(11, 26, 5, 2); + + // Read single entry at bi2, should now gracefully fail. + rb.ReadAt(bi2, [](Maybe<ProfileBufferEntryReader>&& aMaybeReader) { + MOZ_RELEASE_ASSERT(aMaybeReader.isNothing()); + }); + + // Read single entry at bi5. + rb.ReadAt(bi5, [](Maybe<ProfileBufferEntryReader>&& aMaybeReader) { + MOZ_RELEASE_ASSERT(aMaybeReader.isSome()); + MOZ_RELEASE_ASSERT(aMaybeReader->ReadObject<uint32_t>() == 5); + }); + + rb.Read([&](BlocksRingBuffer::Reader* aReader) { + MOZ_RELEASE_ASSERT(!!aReader); + // begin() and end() should be at the range edges (verified above). + MOZ_RELEASE_ASSERT( + aReader->begin().CurrentBlockIndex().ConvertToProfileBufferIndex() == + 11); + MOZ_RELEASE_ASSERT( + aReader->end().CurrentBlockIndex().ConvertToProfileBufferIndex() == + 26); + // Null ProfileBufferBlockIndex clamped to the beginning. + MOZ_RELEASE_ASSERT(aReader->At(bi0) == aReader->begin()); + // Cleared block index clamped to the beginning. + MOZ_RELEASE_ASSERT(aReader->At(bi2) == aReader->begin()); + // At(begin) same as begin(). + MOZ_RELEASE_ASSERT(aReader->At(aReader->begin().CurrentBlockIndex()) == + aReader->begin()); + // bi5 at expected position. + MOZ_RELEASE_ASSERT( + aReader->At(bi5).CurrentBlockIndex().ConvertToProfileBufferIndex() == + 21); + // bi6 at expected position at the end. + MOZ_RELEASE_ASSERT(aReader->At(bi6) == aReader->end()); + // At(end) same as end(). + MOZ_RELEASE_ASSERT(aReader->At(aReader->end().CurrentBlockIndex()) == + aReader->end()); + }); + + // Check that we have `3` to `5`. + count = 2; + rb.ReadEach([&](ProfileBufferEntryReader& aReader) { + MOZ_RELEASE_ASSERT(aReader.ReadObject<uint32_t>() == ++count); + }); + MOZ_RELEASE_ASSERT(count == 5); + + // Clear everything before `4`, this should clear `3`. + rb.ClearBefore(bi4); + // 16 17 18 19 20 21 22 23 24 25 26 11 12 13 14 15 + // S[4 | int(4) ] [4 | int(5) ]E ? ? ? ? ? ? + VERIFY_START_END_PUSHED_CLEARED(16, 26, 5, 3); + + // Check that we have `4` to `5`. + count = 3; + rb.ReadEach([&](ProfileBufferEntryReader& aReader) { + MOZ_RELEASE_ASSERT(aReader.ReadObject<uint32_t>() == ++count); + }); + MOZ_RELEASE_ASSERT(count == 5); + + // Clear everything before `4` again, nothing to clear. + rb.ClearBefore(bi4); + VERIFY_START_END_PUSHED_CLEARED(16, 26, 5, 3); + + // Clear everything, this should clear `4` and `5`, and bring the start + // index where the end index currently is. + rb.ClearBefore(bi6); + // 16 17 18 19 20 21 22 23 24 25 26 11 12 13 14 15 + // ? ? ? ? ? ? ? ? ? ? SE? ? ? ? ? ? + VERIFY_START_END_PUSHED_CLEARED(26, 26, 5, 5); + + // Check that we have nothing to read. + rb.ReadEach([&](auto&&) { MOZ_RELEASE_ASSERT(false); }); + + // Read single entry at bi5, should now gracefully fail. + rb.ReadAt(bi5, [](Maybe<ProfileBufferEntryReader>&& aMaybeReader) { + MOZ_RELEASE_ASSERT(aMaybeReader.isNothing()); + }); + + // Clear everything before now-cleared `4`, nothing to clear. + rb.ClearBefore(bi4); + VERIFY_START_END_PUSHED_CLEARED(26, 26, 5, 5); + + // Push `6` directly. + MOZ_RELEASE_ASSERT(rb.PutObject(uint32_t(6)) == bi6); + // 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 + // ? ? ? ? ? ? ? ? ? ? S[4 | int(6) ]E ? + VERIFY_START_END_PUSHED_CLEARED(26, 31, 6, 5); + + { + // Create a 2nd buffer and fill it with `7` and `8`. + uint8_t buffer2[MBSize]; + BlocksRingBuffer rb2(BlocksRingBuffer::ThreadSafety::WithoutMutex, + buffer2, MakePowerOfTwo32<MBSize>()); + rb2.PutObject(uint32_t(7)); + rb2.PutObject(uint32_t(8)); + // Main buffer shouldn't have changed. + VERIFY_START_END_PUSHED_CLEARED(26, 31, 6, 5); + + // Append contents of rb2 to rb, this should end up being the same as + // pushing the two numbers. + rb.AppendContents(rb2); + // 32 33 34 35 36 37 38 39 40 41 26 27 28 29 30 31 + // int(7) ] [4 | int(8) ]E ? S[4 | int(6) ] [4 | + VERIFY_START_END_PUSHED_CLEARED(26, 41, 8, 5); + + // Append contents of rb2 to rb again, to verify that rb2 was not modified + // above. This should clear `6` and the first `7`. + rb.AppendContents(rb2); + // 48 49 50 51 36 37 38 39 40 41 42 43 44 45 46 47 + // int(8) ]E ? S[4 | int(8) ] [4 | int(7) ] [4 | + VERIFY_START_END_PUSHED_CLEARED(36, 51, 10, 7); + + // End of block where rb2 lives, to verify that it is not needed anymore + // for its copied values to survive in rb. + } + VERIFY_START_END_PUSHED_CLEARED(36, 51, 10, 7); + + // bi6 should now have been cleared. + rb.ReadAt(bi6, [](Maybe<ProfileBufferEntryReader>&& aMaybeReader) { + MOZ_RELEASE_ASSERT(aMaybeReader.isNothing()); + }); + + // Check that we have `8`, `7`, `8`. + count = 0; + uint32_t expected[3] = {8, 7, 8}; + rb.ReadEach([&](ProfileBufferEntryReader& aReader) { + MOZ_RELEASE_ASSERT(count < 3); + MOZ_RELEASE_ASSERT(aReader.ReadObject<uint32_t>() == expected[count++]); + }); + MOZ_RELEASE_ASSERT(count == 3); + + // End of block where rb lives, BlocksRingBuffer destructor should call + // entry destructor for remaining entries. + } + + // Check that only the provided stack-based sub-buffer was modified. + uint32_t changed = 0; + for (size_t i = MBSize; i < MBSize * 2; ++i) { + changed += (buffer[i] == uint8_t('A' + i)) ? 0 : 1; + } + // Expect at least 75% changes. + MOZ_RELEASE_ASSERT(changed >= MBSize * 6 / 8); + + // Everything around the sub-buffer should be unchanged. + for (size_t i = 0; i < MBSize; ++i) { + MOZ_RELEASE_ASSERT(buffer[i] == uint8_t('A' + i)); + } + for (size_t i = MBSize * 2; i < MBSize * 3; ++i) { + MOZ_RELEASE_ASSERT(buffer[i] == uint8_t('A' + i)); + } + + printf("TestBlocksRingBufferAPI done\n"); +} + +void TestBlocksRingBufferUnderlyingBufferChanges() { + printf("TestBlocksRingBufferUnderlyingBufferChanges...\n"); + + // Out-of-session BlocksRingBuffer to start with. + BlocksRingBuffer rb(BlocksRingBuffer::ThreadSafety::WithMutex); + + // Block index to read at. Initially "null", but may be changed below. + ProfileBufferBlockIndex bi; + + // Test all rb APIs when rb is out-of-session and therefore doesn't have an + // underlying buffer. + auto testOutOfSession = [&]() { + MOZ_RELEASE_ASSERT(rb.BufferLength().isNothing()); + BlocksRingBuffer::State state = rb.GetState(); + // When out-of-session, range start and ends are the same, and there are no + // pushed&cleared blocks. + MOZ_RELEASE_ASSERT(state.mRangeStart == state.mRangeEnd); + MOZ_RELEASE_ASSERT(state.mPushedBlockCount == 0); + MOZ_RELEASE_ASSERT(state.mClearedBlockCount == 0); + // `Put()` functions run the callback with `Nothing`. + int32_t ran = 0; + rb.Put(1, [&](Maybe<ProfileBufferEntryWriter>& aMaybeEntryWriter) { + MOZ_RELEASE_ASSERT(aMaybeEntryWriter.isNothing()); + ++ran; + }); + MOZ_RELEASE_ASSERT(ran == 1); + // `PutFrom` won't do anything, and returns the null + // ProfileBufferBlockIndex. + MOZ_RELEASE_ASSERT(rb.PutFrom(&ran, sizeof(ran)) == + ProfileBufferBlockIndex{}); + MOZ_RELEASE_ASSERT(rb.PutObject(ran) == ProfileBufferBlockIndex{}); + // `Read()` functions run the callback with `Nothing`. + ran = 0; + rb.Read([&](BlocksRingBuffer::Reader* aReader) { + MOZ_RELEASE_ASSERT(!aReader); + ++ran; + }); + MOZ_RELEASE_ASSERT(ran == 1); + ran = 0; + rb.ReadAt(ProfileBufferBlockIndex{}, + [&](Maybe<ProfileBufferEntryReader>&& aMaybeEntryReader) { + MOZ_RELEASE_ASSERT(aMaybeEntryReader.isNothing()); + ++ran; + }); + MOZ_RELEASE_ASSERT(ran == 1); + ran = 0; + rb.ReadAt(bi, [&](Maybe<ProfileBufferEntryReader>&& aMaybeEntryReader) { + MOZ_RELEASE_ASSERT(aMaybeEntryReader.isNothing()); + ++ran; + }); + MOZ_RELEASE_ASSERT(ran == 1); + // `ReadEach` shouldn't run the callback (nothing to read). + rb.ReadEach([](auto&&) { MOZ_RELEASE_ASSERT(false); }); + }; + + // As `testOutOfSession()` attempts to modify the buffer, we run it twice to + // make sure one run doesn't influence the next one. + testOutOfSession(); + testOutOfSession(); + + rb.ClearBefore(bi); + testOutOfSession(); + testOutOfSession(); + + rb.Clear(); + testOutOfSession(); + testOutOfSession(); + + rb.Reset(); + testOutOfSession(); + testOutOfSession(); + + constexpr uint32_t MBSize = 32; + + rb.Set(MakePowerOfTwo<BlocksRingBuffer::Length, MBSize>()); + + constexpr bool EMPTY = true; + constexpr bool NOT_EMPTY = false; + // Test all rb APIs when rb has an underlying buffer. + auto testInSession = [&](bool aExpectEmpty) { + MOZ_RELEASE_ASSERT(rb.BufferLength().isSome()); + BlocksRingBuffer::State state = rb.GetState(); + if (aExpectEmpty) { + MOZ_RELEASE_ASSERT(state.mRangeStart == state.mRangeEnd); + MOZ_RELEASE_ASSERT(state.mPushedBlockCount == 0); + MOZ_RELEASE_ASSERT(state.mClearedBlockCount == 0); + } else { + MOZ_RELEASE_ASSERT(state.mRangeStart < state.mRangeEnd); + MOZ_RELEASE_ASSERT(state.mPushedBlockCount > 0); + MOZ_RELEASE_ASSERT(state.mClearedBlockCount <= state.mPushedBlockCount); + } + int32_t ran = 0; + // The following three `Put...` will write three int32_t of value 1. + bi = rb.Put(sizeof(ran), + [&](Maybe<ProfileBufferEntryWriter>& aMaybeEntryWriter) { + MOZ_RELEASE_ASSERT(aMaybeEntryWriter.isSome()); + ++ran; + aMaybeEntryWriter->WriteObject(ran); + return aMaybeEntryWriter->CurrentBlockIndex(); + }); + MOZ_RELEASE_ASSERT(ran == 1); + MOZ_RELEASE_ASSERT(rb.PutFrom(&ran, sizeof(ran)) != + ProfileBufferBlockIndex{}); + MOZ_RELEASE_ASSERT(rb.PutObject(ran) != ProfileBufferBlockIndex{}); + ran = 0; + rb.Read([&](BlocksRingBuffer::Reader* aReader) { + MOZ_RELEASE_ASSERT(!!aReader); + ++ran; + }); + MOZ_RELEASE_ASSERT(ran == 1); + ran = 0; + rb.ReadEach([&](ProfileBufferEntryReader& aEntryReader) { + MOZ_RELEASE_ASSERT(aEntryReader.RemainingBytes() == sizeof(ran)); + MOZ_RELEASE_ASSERT(aEntryReader.ReadObject<decltype(ran)>() == 1); + ++ran; + }); + MOZ_RELEASE_ASSERT(ran >= 3); + ran = 0; + rb.ReadAt(ProfileBufferBlockIndex{}, + [&](Maybe<ProfileBufferEntryReader>&& aMaybeEntryReader) { + MOZ_RELEASE_ASSERT(aMaybeEntryReader.isNothing()); + ++ran; + }); + MOZ_RELEASE_ASSERT(ran == 1); + ran = 0; + rb.ReadAt(bi, [&](Maybe<ProfileBufferEntryReader>&& aMaybeEntryReader) { + MOZ_RELEASE_ASSERT(aMaybeEntryReader.isNothing() == !bi); + ++ran; + }); + MOZ_RELEASE_ASSERT(ran == 1); + }; + + testInSession(EMPTY); + testInSession(NOT_EMPTY); + + rb.Set(MakePowerOfTwo<BlocksRingBuffer::Length, 32>()); + MOZ_RELEASE_ASSERT(rb.BufferLength().isSome()); + rb.ReadEach([](auto&&) { MOZ_RELEASE_ASSERT(false); }); + + testInSession(EMPTY); + testInSession(NOT_EMPTY); + + rb.Reset(); + testOutOfSession(); + testOutOfSession(); + + uint8_t buffer[MBSize * 3]; + for (size_t i = 0; i < MBSize * 3; ++i) { + buffer[i] = uint8_t('A' + i); + } + + rb.Set(&buffer[MBSize], MakePowerOfTwo<BlocksRingBuffer::Length, MBSize>()); + MOZ_RELEASE_ASSERT(rb.BufferLength().isSome()); + rb.ReadEach([](auto&&) { MOZ_RELEASE_ASSERT(false); }); + + testInSession(EMPTY); + testInSession(NOT_EMPTY); + + rb.Reset(); + testOutOfSession(); + testOutOfSession(); + + rb.Set(&buffer[MBSize], MakePowerOfTwo<BlocksRingBuffer::Length, MBSize>()); + MOZ_RELEASE_ASSERT(rb.BufferLength().isSome()); + rb.ReadEach([](auto&&) { MOZ_RELEASE_ASSERT(false); }); + + testInSession(EMPTY); + testInSession(NOT_EMPTY); + + // Remove the current underlying buffer, this should clear all entries. + rb.Reset(); + + // Check that only the provided stack-based sub-buffer was modified. + uint32_t changed = 0; + for (size_t i = MBSize; i < MBSize * 2; ++i) { + changed += (buffer[i] == uint8_t('A' + i)) ? 0 : 1; + } + // Expect at least 75% changes. + MOZ_RELEASE_ASSERT(changed >= MBSize * 6 / 8); + + // Everything around the sub-buffer should be unchanged. + for (size_t i = 0; i < MBSize; ++i) { + MOZ_RELEASE_ASSERT(buffer[i] == uint8_t('A' + i)); + } + for (size_t i = MBSize * 2; i < MBSize * 3; ++i) { + MOZ_RELEASE_ASSERT(buffer[i] == uint8_t('A' + i)); + } + + testOutOfSession(); + testOutOfSession(); + + printf("TestBlocksRingBufferUnderlyingBufferChanges done\n"); +} + +void TestBlocksRingBufferThreading() { + printf("TestBlocksRingBufferThreading...\n"); + + constexpr uint32_t MBSize = 8192; + uint8_t buffer[MBSize * 3]; + for (size_t i = 0; i < MBSize * 3; ++i) { + buffer[i] = uint8_t('A' + i); + } + BlocksRingBuffer rb(BlocksRingBuffer::ThreadSafety::WithMutex, + &buffer[MBSize], MakePowerOfTwo32<MBSize>()); + + // Start reader thread. + std::atomic<bool> stopReader{false}; + std::thread reader([&]() { + for (;;) { + BlocksRingBuffer::State state = rb.GetState(); + printf( + "Reader: range=%llu..%llu (%llu bytes) pushed=%llu cleared=%llu " + "(alive=%llu)\n", + static_cast<unsigned long long>( + state.mRangeStart.ConvertToProfileBufferIndex()), + static_cast<unsigned long long>( + state.mRangeEnd.ConvertToProfileBufferIndex()), + static_cast<unsigned long long>( + state.mRangeEnd.ConvertToProfileBufferIndex()) - + static_cast<unsigned long long>( + state.mRangeStart.ConvertToProfileBufferIndex()), + static_cast<unsigned long long>(state.mPushedBlockCount), + static_cast<unsigned long long>(state.mClearedBlockCount), + static_cast<unsigned long long>(state.mPushedBlockCount - + state.mClearedBlockCount)); + if (stopReader) { + break; + } + ::SleepMilli(1); + } + }); + + // Start writer threads. + constexpr int ThreadCount = 32; + std::thread threads[ThreadCount]; + for (int threadNo = 0; threadNo < ThreadCount; ++threadNo) { + threads[threadNo] = std::thread( + [&](int aThreadNo) { + ::SleepMilli(1); + constexpr int pushCount = 1024; + for (int push = 0; push < pushCount; ++push) { + // Reserve as many bytes as the thread number (but at least enough + // to store an int), and write an increasing int. + rb.Put(std::max(aThreadNo, int(sizeof(push))), + [&](Maybe<ProfileBufferEntryWriter>& aEW) { + MOZ_RELEASE_ASSERT(aEW.isSome()); + aEW->WriteObject(aThreadNo * 1000000 + push); + *aEW += aEW->RemainingBytes(); + }); + } + }, + threadNo); + } + + // Wait for all writer threads to die. + for (auto&& thread : threads) { + thread.join(); + } + + // Stop reader thread. + stopReader = true; + reader.join(); + + // Check that only the provided stack-based sub-buffer was modified. + uint32_t changed = 0; + for (size_t i = MBSize; i < MBSize * 2; ++i) { + changed += (buffer[i] == uint8_t('A' + i)) ? 0 : 1; + } + // Expect at least 75% changes. + MOZ_RELEASE_ASSERT(changed >= MBSize * 6 / 8); + + // Everything around the sub-buffer should be unchanged. + for (size_t i = 0; i < MBSize; ++i) { + MOZ_RELEASE_ASSERT(buffer[i] == uint8_t('A' + i)); + } + for (size_t i = MBSize * 2; i < MBSize * 3; ++i) { + MOZ_RELEASE_ASSERT(buffer[i] == uint8_t('A' + i)); + } + + printf("TestBlocksRingBufferThreading done\n"); +} + +void TestBlocksRingBufferSerialization() { + printf("TestBlocksRingBufferSerialization...\n"); + + constexpr uint32_t MBSize = 64; + uint8_t buffer[MBSize * 3]; + for (size_t i = 0; i < MBSize * 3; ++i) { + buffer[i] = uint8_t('A' + i); + } + BlocksRingBuffer rb(BlocksRingBuffer::ThreadSafety::WithMutex, + &buffer[MBSize], MakePowerOfTwo32<MBSize>()); + + // Will expect literal string to always have the same address. +# define THE_ANSWER "The answer is " + const char* theAnswer = THE_ANSWER; + + rb.PutObjects('0', WrapProfileBufferLiteralCStringPointer(THE_ANSWER), 42, + std::string(" but pi="), 3.14); + rb.ReadEach([&](ProfileBufferEntryReader& aER) { + char c0; + const char* answer; + int integer; + std::string str; + double pi; + aER.ReadIntoObjects(c0, answer, integer, str, pi); + MOZ_RELEASE_ASSERT(c0 == '0'); + MOZ_RELEASE_ASSERT(answer == theAnswer); + MOZ_RELEASE_ASSERT(integer == 42); + MOZ_RELEASE_ASSERT(str == " but pi="); + MOZ_RELEASE_ASSERT(pi == 3.14); + }); + rb.ReadEach([&](ProfileBufferEntryReader& aER) { + char c0 = aER.ReadObject<char>(); + MOZ_RELEASE_ASSERT(c0 == '0'); + const char* answer = aER.ReadObject<const char*>(); + MOZ_RELEASE_ASSERT(answer == theAnswer); + int integer = aER.ReadObject<int>(); + MOZ_RELEASE_ASSERT(integer == 42); + std::string str = aER.ReadObject<std::string>(); + MOZ_RELEASE_ASSERT(str == " but pi="); + double pi = aER.ReadObject<double>(); + MOZ_RELEASE_ASSERT(pi == 3.14); + }); + + rb.Clear(); + // Write an int and store its ProfileBufferBlockIndex. + ProfileBufferBlockIndex blockIndex = rb.PutObject(123); + // It should be non-0. + MOZ_RELEASE_ASSERT(blockIndex != ProfileBufferBlockIndex{}); + // Write that ProfileBufferBlockIndex. + rb.PutObject(blockIndex); + rb.Read([&](BlocksRingBuffer::Reader* aR) { + BlocksRingBuffer::BlockIterator it = aR->begin(); + const BlocksRingBuffer::BlockIterator itEnd = aR->end(); + MOZ_RELEASE_ASSERT(it != itEnd); + MOZ_RELEASE_ASSERT((*it).ReadObject<int>() == 123); + ++it; + MOZ_RELEASE_ASSERT(it != itEnd); + MOZ_RELEASE_ASSERT((*it).ReadObject<ProfileBufferBlockIndex>() == + blockIndex); + ++it; + MOZ_RELEASE_ASSERT(it == itEnd); + }); + + rb.Clear(); + rb.PutObjects( + std::make_tuple('0', WrapProfileBufferLiteralCStringPointer(THE_ANSWER), + 42, std::string(" but pi="), 3.14)); + rb.ReadEach([&](ProfileBufferEntryReader& aER) { + MOZ_RELEASE_ASSERT(aER.ReadObject<char>() == '0'); + MOZ_RELEASE_ASSERT(aER.ReadObject<const char*>() == theAnswer); + MOZ_RELEASE_ASSERT(aER.ReadObject<int>() == 42); + MOZ_RELEASE_ASSERT(aER.ReadObject<std::string>() == " but pi="); + MOZ_RELEASE_ASSERT(aER.ReadObject<double>() == 3.14); + }); + + rb.Clear(); + rb.PutObjects( + std::make_tuple('0', WrapProfileBufferLiteralCStringPointer(THE_ANSWER), + 42, std::string(" but pi="), 3.14)); + rb.ReadEach([&](ProfileBufferEntryReader& aER) { + MOZ_RELEASE_ASSERT(aER.ReadObject<char>() == '0'); + MOZ_RELEASE_ASSERT(aER.ReadObject<const char*>() == theAnswer); + MOZ_RELEASE_ASSERT(aER.ReadObject<int>() == 42); + MOZ_RELEASE_ASSERT(aER.ReadObject<std::string>() == " but pi="); + MOZ_RELEASE_ASSERT(aER.ReadObject<double>() == 3.14); + }); + + rb.Clear(); + { + UniqueFreePtr<char> ufps(strdup(THE_ANSWER)); + rb.PutObjects(ufps); + } + rb.ReadEach([&](ProfileBufferEntryReader& aER) { + auto ufps = aER.ReadObject<UniqueFreePtr<char>>(); + MOZ_RELEASE_ASSERT(!!ufps); + MOZ_RELEASE_ASSERT(std::string(THE_ANSWER) == ufps.get()); + }); + + rb.Clear(); + int intArray[] = {1, 2, 3, 4, 5}; + rb.PutObjects(Span(intArray)); + rb.ReadEach([&](ProfileBufferEntryReader& aER) { + int intArrayOut[sizeof(intArray) / sizeof(intArray[0])] = {0}; + auto outSpan = Span(intArrayOut); + aER.ReadIntoObject(outSpan); + for (size_t i = 0; i < sizeof(intArray) / sizeof(intArray[0]); ++i) { + MOZ_RELEASE_ASSERT(intArrayOut[i] == intArray[i]); + } + }); + + rb.Clear(); + rb.PutObjects(Maybe<int>(Nothing{}), Maybe<int>(Some(123))); + rb.ReadEach([&](ProfileBufferEntryReader& aER) { + Maybe<int> mi0, mi1; + aER.ReadIntoObjects(mi0, mi1); + MOZ_RELEASE_ASSERT(mi0.isNothing()); + MOZ_RELEASE_ASSERT(mi1.isSome()); + MOZ_RELEASE_ASSERT(*mi1 == 123); + }); + + rb.Clear(); + using V = Variant<int, double, int>; + V v0(VariantIndex<0>{}, 123); + V v1(3.14); + V v2(VariantIndex<2>{}, 456); + rb.PutObjects(v0, v1, v2); + rb.ReadEach([&](ProfileBufferEntryReader& aER) { + MOZ_RELEASE_ASSERT(aER.ReadObject<V>() == v0); + MOZ_RELEASE_ASSERT(aER.ReadObject<V>() == v1); + MOZ_RELEASE_ASSERT(aER.ReadObject<V>() == v2); + }); + + // 2nd BlocksRingBuffer to contain the 1st one. It has be be more than twice + // the size. + constexpr uint32_t MBSize2 = MBSize * 4; + uint8_t buffer2[MBSize2 * 3]; + for (size_t i = 0; i < MBSize2 * 3; ++i) { + buffer2[i] = uint8_t('B' + i); + } + BlocksRingBuffer rb2(BlocksRingBuffer::ThreadSafety::WithoutMutex, + &buffer2[MBSize2], MakePowerOfTwo32<MBSize2>()); + rb2.PutObject(rb); + + // 3rd BlocksRingBuffer deserialized from the 2nd one. + uint8_t buffer3[MBSize * 3]; + for (size_t i = 0; i < MBSize * 3; ++i) { + buffer3[i] = uint8_t('C' + i); + } + BlocksRingBuffer rb3(BlocksRingBuffer::ThreadSafety::WithoutMutex, + &buffer3[MBSize], MakePowerOfTwo32<MBSize>()); + rb2.ReadEach([&](ProfileBufferEntryReader& aER) { aER.ReadIntoObject(rb3); }); + + // And a 4th heap-allocated one. + UniquePtr<BlocksRingBuffer> rb4up; + rb2.ReadEach([&](ProfileBufferEntryReader& aER) { + rb4up = aER.ReadObject<UniquePtr<BlocksRingBuffer>>(); + }); + MOZ_RELEASE_ASSERT(!!rb4up); + + // Clear 1st and 2nd BlocksRingBuffers, to ensure we have made a deep copy + // into the 3rd&4th ones. + rb.Clear(); + rb2.Clear(); + + // And now the 3rd one should have the same contents as the 1st one had. + rb3.ReadEach([&](ProfileBufferEntryReader& aER) { + MOZ_RELEASE_ASSERT(aER.ReadObject<V>() == v0); + MOZ_RELEASE_ASSERT(aER.ReadObject<V>() == v1); + MOZ_RELEASE_ASSERT(aER.ReadObject<V>() == v2); + }); + + // And 4th. + rb4up->ReadEach([&](ProfileBufferEntryReader& aER) { + MOZ_RELEASE_ASSERT(aER.ReadObject<V>() == v0); + MOZ_RELEASE_ASSERT(aER.ReadObject<V>() == v1); + MOZ_RELEASE_ASSERT(aER.ReadObject<V>() == v2); + }); + + // In fact, the 3rd and 4th ones should have the same state, because they were + // created the same way. + MOZ_RELEASE_ASSERT(rb3.GetState().mRangeStart == + rb4up->GetState().mRangeStart); + MOZ_RELEASE_ASSERT(rb3.GetState().mRangeEnd == rb4up->GetState().mRangeEnd); + MOZ_RELEASE_ASSERT(rb3.GetState().mPushedBlockCount == + rb4up->GetState().mPushedBlockCount); + MOZ_RELEASE_ASSERT(rb3.GetState().mClearedBlockCount == + rb4up->GetState().mClearedBlockCount); + + // Check that only the provided stack-based sub-buffer was modified. + uint32_t changed = 0; + for (size_t i = MBSize; i < MBSize * 2; ++i) { + changed += (buffer[i] == uint8_t('A' + i)) ? 0 : 1; + } + // Expect at least 75% changes. + MOZ_RELEASE_ASSERT(changed >= MBSize * 6 / 8); + + // Everything around the sub-buffers should be unchanged. + for (size_t i = 0; i < MBSize; ++i) { + MOZ_RELEASE_ASSERT(buffer[i] == uint8_t('A' + i)); + } + for (size_t i = MBSize * 2; i < MBSize * 3; ++i) { + MOZ_RELEASE_ASSERT(buffer[i] == uint8_t('A' + i)); + } + + for (size_t i = 0; i < MBSize2; ++i) { + MOZ_RELEASE_ASSERT(buffer2[i] == uint8_t('B' + i)); + } + for (size_t i = MBSize2 * 2; i < MBSize2 * 3; ++i) { + MOZ_RELEASE_ASSERT(buffer2[i] == uint8_t('B' + i)); + } + + for (size_t i = 0; i < MBSize; ++i) { + MOZ_RELEASE_ASSERT(buffer3[i] == uint8_t('C' + i)); + } + for (size_t i = MBSize * 2; i < MBSize * 3; ++i) { + MOZ_RELEASE_ASSERT(buffer3[i] == uint8_t('C' + i)); + } + + printf("TestBlocksRingBufferSerialization done\n"); +} + +void TestLiteralEmptyStringView() { + printf("TestLiteralEmptyStringView...\n"); + + static_assert(mozilla::LiteralEmptyStringView<char>() == + std::string_view("")); + static_assert(!!mozilla::LiteralEmptyStringView<char>().data()); + static_assert(mozilla::LiteralEmptyStringView<char>().length() == 0); + + static_assert(mozilla::LiteralEmptyStringView<char16_t>() == + std::basic_string_view<char16_t>(u"")); + static_assert(!!mozilla::LiteralEmptyStringView<char16_t>().data()); + static_assert(mozilla::LiteralEmptyStringView<char16_t>().length() == 0); + + printf("TestLiteralEmptyStringView done\n"); +} + +template <typename CHAR> +void TestProfilerStringView() { + if constexpr (std::is_same_v<CHAR, char>) { + printf("TestProfilerStringView<char>...\n"); + } else if constexpr (std::is_same_v<CHAR, char16_t>) { + printf("TestProfilerStringView<char16_t>...\n"); + } else { + MOZ_RELEASE_ASSERT(false, + "TestProfilerStringView only handles char and char16_t"); + } + + // Used to verify implicit constructions, as this will normally be used in + // function parameters. + auto BSV = [](mozilla::ProfilerStringView<CHAR>&& aBSV) { + return std::move(aBSV); + }; + + // These look like string literals, as expected by some string constructors. + const CHAR empty[0 + 1] = {CHAR('\0')}; + const CHAR hi[2 + 1] = { + CHAR('h'), + CHAR('i'), + CHAR('\0'), + }; + + // Literal empty string. + MOZ_RELEASE_ASSERT(BSV(empty).Length() == 0); + MOZ_RELEASE_ASSERT(BSV(empty).AsSpan().IsEmpty()); + MOZ_RELEASE_ASSERT(BSV(empty).IsLiteral()); + MOZ_RELEASE_ASSERT(!BSV(empty).IsReference()); + + // Literal non-empty string. + MOZ_RELEASE_ASSERT(BSV(hi).Length() == 2); + MOZ_RELEASE_ASSERT(BSV(hi).AsSpan().Elements()); + MOZ_RELEASE_ASSERT(BSV(hi).AsSpan().Elements()[0] == CHAR('h')); + MOZ_RELEASE_ASSERT(BSV(hi).AsSpan().Elements()[1] == CHAR('i')); + MOZ_RELEASE_ASSERT(BSV(hi).IsLiteral()); + MOZ_RELEASE_ASSERT(!BSV(hi).IsReference()); + + // std::string_view to a literal empty string. + MOZ_RELEASE_ASSERT(BSV(std::basic_string_view<CHAR>(empty)).Length() == 0); + MOZ_RELEASE_ASSERT( + BSV(std::basic_string_view<CHAR>(empty)).AsSpan().IsEmpty()); + MOZ_RELEASE_ASSERT(!BSV(std::basic_string_view<CHAR>(empty)).IsLiteral()); + MOZ_RELEASE_ASSERT(BSV(std::basic_string_view<CHAR>(empty)).IsReference()); + + // std::string_view to a literal non-empty string. + MOZ_RELEASE_ASSERT(BSV(std::basic_string_view<CHAR>(hi)).Length() == 2); + MOZ_RELEASE_ASSERT(BSV(std::basic_string_view<CHAR>(hi)).AsSpan().Elements()); + MOZ_RELEASE_ASSERT( + BSV(std::basic_string_view<CHAR>(hi)).AsSpan().Elements()[0] == + CHAR('h')); + MOZ_RELEASE_ASSERT( + BSV(std::basic_string_view<CHAR>(hi)).AsSpan().Elements()[1] == + CHAR('i')); + MOZ_RELEASE_ASSERT(!BSV(std::basic_string_view<CHAR>(hi)).IsLiteral()); + MOZ_RELEASE_ASSERT(BSV(std::basic_string_view<CHAR>(hi)).IsReference()); + + // Default std::string_view points at nullptr, ProfilerStringView converts it + // to the literal empty string. + MOZ_RELEASE_ASSERT(BSV(std::basic_string_view<CHAR>()).Length() == 0); + MOZ_RELEASE_ASSERT(!std::basic_string_view<CHAR>().data()); + MOZ_RELEASE_ASSERT(BSV(std::basic_string_view<CHAR>()).AsSpan().IsEmpty()); + MOZ_RELEASE_ASSERT(BSV(std::basic_string_view<CHAR>()).IsLiteral()); + MOZ_RELEASE_ASSERT(!BSV(std::basic_string_view<CHAR>()).IsReference()); + + // std::string to a literal empty string. + MOZ_RELEASE_ASSERT(BSV(std::basic_string<CHAR>(empty)).Length() == 0); + MOZ_RELEASE_ASSERT(BSV(std::basic_string<CHAR>(empty)).AsSpan().IsEmpty()); + MOZ_RELEASE_ASSERT(!BSV(std::basic_string<CHAR>(empty)).IsLiteral()); + MOZ_RELEASE_ASSERT(BSV(std::basic_string<CHAR>(empty)).IsReference()); + + // std::string to a literal non-empty string. + MOZ_RELEASE_ASSERT(BSV(std::basic_string<CHAR>(hi)).Length() == 2); + MOZ_RELEASE_ASSERT(BSV(std::basic_string<CHAR>(hi)).AsSpan().Elements()); + MOZ_RELEASE_ASSERT(BSV(std::basic_string<CHAR>(hi)).AsSpan().Elements()[0] == + CHAR('h')); + MOZ_RELEASE_ASSERT(BSV(std::basic_string<CHAR>(hi)).AsSpan().Elements()[1] == + CHAR('i')); + MOZ_RELEASE_ASSERT(!BSV(std::basic_string<CHAR>(hi)).IsLiteral()); + MOZ_RELEASE_ASSERT(BSV(std::basic_string<CHAR>(hi)).IsReference()); + + // Default std::string contains an empty null-terminated string. + MOZ_RELEASE_ASSERT(BSV(std::basic_string<CHAR>()).Length() == 0); + MOZ_RELEASE_ASSERT(std::basic_string<CHAR>().data()); + MOZ_RELEASE_ASSERT(BSV(std::basic_string<CHAR>()).AsSpan().IsEmpty()); + MOZ_RELEASE_ASSERT(!BSV(std::basic_string<CHAR>()).IsLiteral()); + MOZ_RELEASE_ASSERT(BSV(std::basic_string<CHAR>()).IsReference()); + + // Class that quacks like nsTString (with Data(), Length(), IsLiteral()), to + // check that ProfilerStringView can read from them. + class FakeNsTString { + public: + FakeNsTString(const CHAR* aData, size_t aLength, bool aIsLiteral) + : mData(aData), mLength(aLength), mIsLiteral(aIsLiteral) {} + + const CHAR* Data() const { return mData; } + size_t Length() const { return mLength; } + bool IsLiteral() const { return mIsLiteral; } + + private: + const CHAR* mData; + size_t mLength; + bool mIsLiteral; + }; + + // FakeNsTString to nullptr. + MOZ_RELEASE_ASSERT(BSV(FakeNsTString(nullptr, 0, true)).Length() == 0); + MOZ_RELEASE_ASSERT(BSV(FakeNsTString(nullptr, 0, true)).AsSpan().IsEmpty()); + MOZ_RELEASE_ASSERT(BSV(FakeNsTString(nullptr, 0, true)).IsLiteral()); + MOZ_RELEASE_ASSERT(!BSV(FakeNsTString(nullptr, 0, true)).IsReference()); + + // FakeNsTString to a literal empty string. + MOZ_RELEASE_ASSERT(BSV(FakeNsTString(empty, 0, true)).Length() == 0); + MOZ_RELEASE_ASSERT(BSV(FakeNsTString(empty, 0, true)).AsSpan().IsEmpty()); + MOZ_RELEASE_ASSERT(BSV(FakeNsTString(empty, 0, true)).IsLiteral()); + MOZ_RELEASE_ASSERT(!BSV(FakeNsTString(empty, 0, true)).IsReference()); + + // FakeNsTString to a literal non-empty string. + MOZ_RELEASE_ASSERT(BSV(FakeNsTString(hi, 2, true)).Length() == 2); + MOZ_RELEASE_ASSERT(BSV(FakeNsTString(hi, 2, true)).AsSpan().Elements()); + MOZ_RELEASE_ASSERT(BSV(FakeNsTString(hi, 2, true)).AsSpan().Elements()[0] == + CHAR('h')); + MOZ_RELEASE_ASSERT(BSV(FakeNsTString(hi, 2, true)).AsSpan().Elements()[1] == + CHAR('i')); + MOZ_RELEASE_ASSERT(BSV(FakeNsTString(hi, 2, true)).IsLiteral()); + MOZ_RELEASE_ASSERT(!BSV(FakeNsTString(hi, 2, true)).IsReference()); + + // FakeNsTString to a non-literal non-empty string. + MOZ_RELEASE_ASSERT(BSV(FakeNsTString(hi, 2, false)).Length() == 2); + MOZ_RELEASE_ASSERT(BSV(FakeNsTString(hi, 2, false)).AsSpan().Elements()); + MOZ_RELEASE_ASSERT(BSV(FakeNsTString(hi, 2, false)).AsSpan().Elements()[0] == + CHAR('h')); + MOZ_RELEASE_ASSERT(BSV(FakeNsTString(hi, 2, false)).AsSpan().Elements()[1] == + CHAR('i')); + MOZ_RELEASE_ASSERT(!BSV(FakeNsTString(hi, 2, false)).IsLiteral()); + MOZ_RELEASE_ASSERT(BSV(FakeNsTString(hi, 2, false)).IsReference()); + + // Serialization and deserialization (with ownership). + constexpr size_t bufferMaxSize = 1024; + constexpr ProfileChunkedBuffer::Length chunkMinSize = 128; + ProfileBufferChunkManagerWithLocalLimit cm(bufferMaxSize, chunkMinSize); + ProfileChunkedBuffer cb(ProfileChunkedBuffer::ThreadSafety::WithMutex, cm); + + // Literal string, serialized as raw pointer. + MOZ_RELEASE_ASSERT(cb.PutObject(BSV(hi))); + { + unsigned read = 0; + ProfilerStringView<CHAR> outerBSV; + cb.ReadEach([&](ProfileBufferEntryReader& aER) { + ++read; + auto bsv = aER.ReadObject<ProfilerStringView<CHAR>>(); + MOZ_RELEASE_ASSERT(bsv.Length() == 2); + MOZ_RELEASE_ASSERT(bsv.AsSpan().Elements()); + MOZ_RELEASE_ASSERT(bsv.AsSpan().Elements()[0] == CHAR('h')); + MOZ_RELEASE_ASSERT(bsv.AsSpan().Elements()[1] == CHAR('i')); + MOZ_RELEASE_ASSERT(bsv.IsLiteral()); + MOZ_RELEASE_ASSERT(!bsv.IsReference()); + outerBSV = std::move(bsv); + }); + MOZ_RELEASE_ASSERT(read == 1); + MOZ_RELEASE_ASSERT(outerBSV.Length() == 2); + MOZ_RELEASE_ASSERT(outerBSV.AsSpan().Elements()); + MOZ_RELEASE_ASSERT(outerBSV.AsSpan().Elements()[0] == CHAR('h')); + MOZ_RELEASE_ASSERT(outerBSV.AsSpan().Elements()[1] == CHAR('i')); + MOZ_RELEASE_ASSERT(outerBSV.IsLiteral()); + MOZ_RELEASE_ASSERT(!outerBSV.IsReference()); + } + + MOZ_RELEASE_ASSERT(cb.GetState().mRangeStart == 1u); + + cb.Clear(); + + // Non-literal string, content is serialized. + + // We'll try to write 4 strings, such that the 4th one will cross into the + // next chunk. + unsigned guessedChunkBytes = unsigned(cb.GetState().mRangeStart) - 1u; + static constexpr unsigned stringCount = 4u; + const unsigned stringSize = + guessedChunkBytes / stringCount / sizeof(CHAR) + 3u; + + std::basic_string<CHAR> longString; + longString.reserve(stringSize); + for (unsigned i = 0; i < stringSize; ++i) { + longString += CHAR('0' + i); + } + + for (unsigned i = 0; i < stringCount; ++i) { + MOZ_RELEASE_ASSERT(cb.PutObject(BSV(longString))); + } + + { + unsigned read = 0; + ProfilerStringView<CHAR> outerBSV; + cb.ReadEach([&](ProfileBufferEntryReader& aER) { + ++read; + { + auto bsv = aER.ReadObject<ProfilerStringView<CHAR>>(); + MOZ_RELEASE_ASSERT(bsv.Length() == stringSize); + MOZ_RELEASE_ASSERT(bsv.AsSpan().Elements()); + for (unsigned i = 0; i < stringSize; ++i) { + MOZ_RELEASE_ASSERT(bsv.AsSpan().Elements()[i] == CHAR('0' + i)); + longString += '0' + i; + } + MOZ_RELEASE_ASSERT(!bsv.IsLiteral()); + // The first 3 should be references (because they fit in one chunk, so + // they can be referenced directly), which the 4th one have to be copied + // out of two chunks and stitched back together. + MOZ_RELEASE_ASSERT(bsv.IsReference() == (read != 4)); + + // Test move of ownership. + outerBSV = std::move(bsv); + // After a move, references stay complete, while a non-reference had a + // buffer that has been moved out. + // NOLINTNEXTLINE(bugprone-use-after-move,clang-analyzer-cplusplus.Move) + MOZ_RELEASE_ASSERT(bsv.Length() == ((read != 4) ? stringSize : 0)); + } + + MOZ_RELEASE_ASSERT(outerBSV.Length() == stringSize); + MOZ_RELEASE_ASSERT(outerBSV.AsSpan().Elements()); + for (unsigned i = 0; i < stringSize; ++i) { + MOZ_RELEASE_ASSERT(outerBSV.AsSpan().Elements()[i] == CHAR('0' + i)); + longString += '0' + i; + } + MOZ_RELEASE_ASSERT(!outerBSV.IsLiteral()); + MOZ_RELEASE_ASSERT(outerBSV.IsReference() == (read != 4)); + }); + MOZ_RELEASE_ASSERT(read == 4); + } + + if constexpr (std::is_same_v<CHAR, char>) { + printf("TestProfilerStringView<char> done\n"); + } else if constexpr (std::is_same_v<CHAR, char16_t>) { + printf("TestProfilerStringView<char16_t> done\n"); + } +} + +void TestProfilerDependencies() { + TestPowerOfTwoMask(); + TestPowerOfTwo(); + TestLEB128(); + TestJSONTimeOutput(); + TestChunk(); + TestChunkManagerSingle(); + TestChunkManagerWithLocalLimit(); + TestControlledChunkManagerUpdate(); + TestControlledChunkManagerWithLocalLimit(); + TestChunkedBuffer(); + TestChunkedBufferSingle(); + TestModuloBuffer(); + TestBlocksRingBufferAPI(); + TestBlocksRingBufferUnderlyingBufferChanges(); + TestBlocksRingBufferThreading(); + TestBlocksRingBufferSerialization(); + TestLiteralEmptyStringView(); + TestProfilerStringView<char>(); + TestProfilerStringView<char16_t>(); +} + +// Increase the depth, to a maximum (to avoid too-deep recursion). +static constexpr size_t NextDepth(size_t aDepth) { + constexpr size_t MAX_DEPTH = 128; + return (aDepth < MAX_DEPTH) ? (aDepth + 1) : aDepth; +} + +Atomic<bool, Relaxed> sStopFibonacci; + +// Compute fibonacci the hard way (recursively: `f(n)=f(n-1)+f(n-2)`), and +// prevent inlining. +// The template parameter makes each depth be a separate function, to better +// distinguish them in the profiler output. +template <size_t DEPTH = 0> +MOZ_NEVER_INLINE unsigned long long Fibonacci(unsigned long long n) { + AUTO_BASE_PROFILER_LABEL_DYNAMIC_STRING("fib", OTHER, std::to_string(DEPTH)); + if (n == 0) { + return 0; + } + if (n == 1) { + return 1; + } + if (DEPTH < 5 && sStopFibonacci) { + return 1'000'000'000; + } + TimeStamp start = TimeStamp::Now(); + static constexpr size_t MAX_MARKER_DEPTH = 10; + unsigned long long f2 = Fibonacci<NextDepth(DEPTH)>(n - 2); + if (DEPTH == 0) { + BASE_PROFILER_MARKER_UNTYPED("Half-way through Fibonacci", OTHER); + } + unsigned long long f1 = Fibonacci<NextDepth(DEPTH)>(n - 1); + if (DEPTH < MAX_MARKER_DEPTH) { + BASE_PROFILER_MARKER_TEXT("fib", OTHER, + MarkerTiming::IntervalUntilNowFrom(start), + std::to_string(DEPTH)); + } + return f2 + f1; +} + +void TestProfiler() { + printf("TestProfiler starting -- pid: %" PRIu64 ", tid: %" PRIu64 "\n", + uint64_t(baseprofiler::profiler_current_process_id().ToNumber()), + uint64_t(baseprofiler::profiler_current_thread_id().ToNumber())); + // ::SleepMilli(10000); + + TestProfilerDependencies(); + + { + MOZ_RELEASE_ASSERT(!baseprofiler::profiler_is_active()); + MOZ_RELEASE_ASSERT(!baseprofiler::profiler_thread_is_being_profiled()); + MOZ_RELEASE_ASSERT(!baseprofiler::profiler_thread_is_sleeping()); + + const baseprofiler::BaseProfilerThreadId mainThreadId = + mozilla::baseprofiler::profiler_current_thread_id(); + + MOZ_RELEASE_ASSERT(mozilla::baseprofiler::profiler_main_thread_id() == + mainThreadId); + MOZ_RELEASE_ASSERT(mozilla::baseprofiler::profiler_is_main_thread()); + + std::thread testThread([&]() { + const baseprofiler::BaseProfilerThreadId testThreadId = + mozilla::baseprofiler::profiler_current_thread_id(); + MOZ_RELEASE_ASSERT(testThreadId != mainThreadId); + + MOZ_RELEASE_ASSERT(mozilla::baseprofiler::profiler_main_thread_id() != + testThreadId); + MOZ_RELEASE_ASSERT(!mozilla::baseprofiler::profiler_is_main_thread()); + }); + testThread.join(); + + printf("profiler_start()...\n"); + Vector<const char*> filters; + // Profile all registered threads. + MOZ_RELEASE_ASSERT(filters.append("")); + const uint32_t features = baseprofiler::ProfilerFeature::StackWalk; + baseprofiler::profiler_start(baseprofiler::BASE_PROFILER_DEFAULT_ENTRIES, + BASE_PROFILER_DEFAULT_INTERVAL, features, + filters.begin(), filters.length()); + + MOZ_RELEASE_ASSERT(baseprofiler::profiler_is_active()); + MOZ_RELEASE_ASSERT(baseprofiler::profiler_thread_is_being_profiled()); + MOZ_RELEASE_ASSERT(!baseprofiler::profiler_thread_is_sleeping()); + + sStopFibonacci = false; + + std::thread threadFib([]() { + AUTO_BASE_PROFILER_REGISTER_THREAD("fibonacci"); + SleepMilli(5); + auto cause = baseprofiler::profiler_capture_backtrace(); + AUTO_BASE_PROFILER_MARKER_TEXT( + "fibonacci", OTHER, MarkerStack::TakeBacktrace(std::move(cause)), + "First leaf call"); + static const unsigned long long fibStart = 37; + printf("Fibonacci(%llu)...\n", fibStart); + AUTO_BASE_PROFILER_LABEL("Label around Fibonacci", OTHER); + + unsigned long long f = Fibonacci(fibStart); + printf("Fibonacci(%llu) = %llu\n", fibStart, f); + }); + + std::thread threadCancelFib([]() { + AUTO_BASE_PROFILER_REGISTER_THREAD("fibonacci canceller"); + SleepMilli(5); + AUTO_BASE_PROFILER_MARKER_TEXT("fibonacci", OTHER, {}, "Canceller"); + static const int waitMaxSeconds = 10; + for (int i = 0; i < waitMaxSeconds; ++i) { + if (sStopFibonacci) { + AUTO_BASE_PROFILER_LABEL_DYNAMIC_STRING("fibCancel", OTHER, + std::to_string(i)); + return; + } + AUTO_BASE_PROFILER_THREAD_SLEEP; + SleepMilli(1000); + } + AUTO_BASE_PROFILER_LABEL_DYNAMIC_STRING("fibCancel", OTHER, + "Cancelling!"); + sStopFibonacci = true; + }); + + { + AUTO_BASE_PROFILER_MARKER_TEXT("main thread", OTHER, {}, + "joining fibonacci thread"); + AUTO_BASE_PROFILER_THREAD_SLEEP; + threadFib.join(); + } + + { + AUTO_BASE_PROFILER_MARKER_TEXT("main thread", OTHER, {}, + "joining fibonacci-canceller thread"); + sStopFibonacci = true; + AUTO_BASE_PROFILER_THREAD_SLEEP; + threadCancelFib.join(); + } + + // Just making sure all payloads know how to (de)serialize and stream. + + MOZ_RELEASE_ASSERT( + baseprofiler::AddMarker("markers 2.0 without options (omitted)", + mozilla::baseprofiler::category::OTHER)); + + MOZ_RELEASE_ASSERT(baseprofiler::AddMarker( + "markers 2.0 without options (implicit brace-init)", + mozilla::baseprofiler::category::OTHER, {})); + + MOZ_RELEASE_ASSERT(baseprofiler::AddMarker( + "markers 2.0 without options (explicit init)", + mozilla::baseprofiler::category::OTHER, MarkerOptions())); + + MOZ_RELEASE_ASSERT(baseprofiler::AddMarker( + "markers 2.0 without options (explicit brace-init)", + mozilla::baseprofiler::category::OTHER, MarkerOptions{})); + + MOZ_RELEASE_ASSERT(baseprofiler::AddMarker( + "markers 2.0 with one option (implicit)", + mozilla::baseprofiler::category::OTHER, MarkerInnerWindowId(123))); + + MOZ_RELEASE_ASSERT(baseprofiler::AddMarker( + "markers 2.0 with one option (implicit brace-init)", + mozilla::baseprofiler::category::OTHER, {MarkerInnerWindowId(123)})); + + MOZ_RELEASE_ASSERT( + baseprofiler::AddMarker("markers 2.0 with one option (explicit init)", + mozilla::baseprofiler::category::OTHER, + MarkerOptions(MarkerInnerWindowId(123)))); + + MOZ_RELEASE_ASSERT(baseprofiler::AddMarker( + "markers 2.0 with one option (explicit brace-init)", + mozilla::baseprofiler::category::OTHER, + MarkerOptions{MarkerInnerWindowId(123)})); + + MOZ_RELEASE_ASSERT(baseprofiler::AddMarker( + "markers 2.0 with two options (implicit brace-init)", + mozilla::baseprofiler::category::OTHER, + {MarkerInnerWindowId(123), MarkerStack::Capture()})); + + MOZ_RELEASE_ASSERT(baseprofiler::AddMarker( + "markers 2.0 with two options (explicit init)", + mozilla::baseprofiler::category::OTHER, + MarkerOptions(MarkerInnerWindowId(123), MarkerStack::Capture()))); + + MOZ_RELEASE_ASSERT(baseprofiler::AddMarker( + "markers 2.0 with two options (explicit brace-init)", + mozilla::baseprofiler::category::OTHER, + MarkerOptions{MarkerInnerWindowId(123), MarkerStack::Capture()})); + + MOZ_RELEASE_ASSERT( + baseprofiler::AddMarker("default-templated markers 2.0 without options", + mozilla::baseprofiler::category::OTHER)); + + MOZ_RELEASE_ASSERT(baseprofiler::AddMarker( + "default-templated markers 2.0 with option", + mozilla::baseprofiler::category::OTHER, MarkerInnerWindowId(123))); + + MOZ_RELEASE_ASSERT(baseprofiler::AddMarker( + "explicitly-default-templated markers 2.0 without options", + mozilla::baseprofiler::category::OTHER, {}, + ::mozilla::baseprofiler::markers::NoPayload{})); + + MOZ_RELEASE_ASSERT(baseprofiler::AddMarker( + "explicitly-default-templated markers 2.0 with option", + mozilla::baseprofiler::category::OTHER, MarkerInnerWindowId(123), + ::mozilla::baseprofiler::markers::NoPayload{})); + + MOZ_RELEASE_ASSERT(baseprofiler::AddMarker( + "tracing", mozilla::baseprofiler::category::OTHER, {}, + mozilla::baseprofiler::markers::Tracing{}, "category")); + + MOZ_RELEASE_ASSERT(baseprofiler::AddMarker( + "text", mozilla::baseprofiler::category::OTHER, {}, + mozilla::baseprofiler::markers::TextMarker{}, "text text")); + + MOZ_RELEASE_ASSERT(baseprofiler::AddMarker( + "media sample", mozilla::baseprofiler::category::OTHER, {}, + mozilla::baseprofiler::markers::MediaSampleMarker{}, 123, 456, 789)); + + MOZ_RELEASE_ASSERT(baseprofiler::AddMarker( + "video falling behind", mozilla::baseprofiler::category::OTHER, {}, + mozilla::baseprofiler::markers::VideoFallingBehindMarker{}, 123, 456)); + + printf("Sleep 1s...\n"); + { + AUTO_BASE_PROFILER_THREAD_SLEEP; + SleepMilli(1000); + } + + printf("baseprofiler_pause()...\n"); + baseprofiler::profiler_pause(); + + MOZ_RELEASE_ASSERT(!baseprofiler::profiler_thread_is_being_profiled()); + + Maybe<baseprofiler::ProfilerBufferInfo> info = + baseprofiler::profiler_get_buffer_info(); + MOZ_RELEASE_ASSERT(info.isSome()); + printf("Profiler buffer range: %llu .. %llu (%llu bytes)\n", + static_cast<unsigned long long>(info->mRangeStart), + static_cast<unsigned long long>(info->mRangeEnd), + // sizeof(ProfileBufferEntry) == 9 + (static_cast<unsigned long long>(info->mRangeEnd) - + static_cast<unsigned long long>(info->mRangeStart)) * + 9); + printf("Stats: min(us) .. mean(us) .. max(us) [count]\n"); + printf("- Intervals: %7.1f .. %7.1f .. %7.1f [%u]\n", + info->mIntervalsUs.min, + info->mIntervalsUs.sum / info->mIntervalsUs.n, + info->mIntervalsUs.max, info->mIntervalsUs.n); + printf("- Overheads: %7.1f .. %7.1f .. %7.1f [%u]\n", + info->mOverheadsUs.min, + info->mOverheadsUs.sum / info->mOverheadsUs.n, + info->mOverheadsUs.max, info->mOverheadsUs.n); + printf(" - Locking: %7.1f .. %7.1f .. %7.1f [%u]\n", + info->mLockingsUs.min, info->mLockingsUs.sum / info->mLockingsUs.n, + info->mLockingsUs.max, info->mLockingsUs.n); + printf(" - Clearning: %7.1f .. %7.1f .. %7.1f [%u]\n", + info->mCleaningsUs.min, + info->mCleaningsUs.sum / info->mCleaningsUs.n, + info->mCleaningsUs.max, info->mCleaningsUs.n); + printf(" - Counters: %7.1f .. %7.1f .. %7.1f [%u]\n", + info->mCountersUs.min, info->mCountersUs.sum / info->mCountersUs.n, + info->mCountersUs.max, info->mCountersUs.n); + printf(" - Threads: %7.1f .. %7.1f .. %7.1f [%u]\n", + info->mThreadsUs.min, info->mThreadsUs.sum / info->mThreadsUs.n, + info->mThreadsUs.max, info->mThreadsUs.n); + + printf("baseprofiler_get_profile()...\n"); + UniquePtr<char[]> profile = baseprofiler::profiler_get_profile(); + + // Use a string view over the profile contents, for easier testing. + std::string_view profileSV = profile.get(); + + constexpr const auto svnpos = std::string_view::npos; + // TODO: Properly parse profile and check fields. + // Check for some expected marker schema JSON output. + MOZ_RELEASE_ASSERT(profileSV.find("\"markerSchema\":[") != svnpos); + MOZ_RELEASE_ASSERT(profileSV.find("\"name\":\"Text\",") != svnpos); + MOZ_RELEASE_ASSERT(profileSV.find("\"name\":\"tracing\",") != svnpos); + MOZ_RELEASE_ASSERT(profileSV.find("\"name\":\"MediaSample\",") != svnpos); + MOZ_RELEASE_ASSERT(profileSV.find("\"display\":[") != svnpos); + MOZ_RELEASE_ASSERT(profileSV.find("\"marker-chart\"") != svnpos); + MOZ_RELEASE_ASSERT(profileSV.find("\"marker-table\"") != svnpos); + MOZ_RELEASE_ASSERT(profileSV.find("\"format\":\"string\"") != svnpos); + // TODO: Add more checks for what's expected in the profile. Some of them + // are done in gtest's. + + printf("baseprofiler_save_profile_to_file()...\n"); + baseprofiler::baseprofiler_save_profile_to_file( + "TestProfiler_profile.json"); + + printf("profiler_stop()...\n"); + baseprofiler::profiler_stop(); + + MOZ_RELEASE_ASSERT(!baseprofiler::profiler_is_active()); + MOZ_RELEASE_ASSERT(!baseprofiler::profiler_thread_is_being_profiled()); + MOZ_RELEASE_ASSERT(!baseprofiler::profiler_thread_is_sleeping()); + + printf("profiler_shutdown()...\n"); + } + + printf("TestProfiler done\n"); +} + +// Minimal string escaping, similar to how C++ stringliterals should be entered, +// to help update comparison strings in tests below. +void printEscaped(std::string_view aString) { + for (const char c : aString) { + switch (c) { + case '\n': + fprintf(stderr, "\\n\n"); + break; + case '"': + fprintf(stderr, "\\\""); + break; + case '\\': + fprintf(stderr, "\\\\"); + break; + default: + if (c >= ' ' && c <= '~') { + fprintf(stderr, "%c", c); + } else { + fprintf(stderr, "\\x%02x", unsigned(c)); + } + break; + } + } +} + +// Run aF(SpliceableChunkedJSONWriter&, UniqueJSONStrings&) from inside a JSON +// array, then output the string table, and compare the full output to +// aExpected. +template <typename F> +static void VerifyUniqueStringContents( + F&& aF, std::string_view aExpectedData, + std::string_view aExpectedUniqueStrings, + mozilla::baseprofiler::UniqueJSONStrings* aUniqueStringsOrNull = nullptr) { + mozilla::baseprofiler::SpliceableChunkedJSONWriter writer{ + FailureLatchInfallibleSource::Singleton()}; + + MOZ_RELEASE_ASSERT(!writer.ChunkedWriteFunc().Fallible()); + MOZ_RELEASE_ASSERT(!writer.ChunkedWriteFunc().Failed()); + MOZ_RELEASE_ASSERT(!writer.ChunkedWriteFunc().GetFailure()); + MOZ_RELEASE_ASSERT(&writer.ChunkedWriteFunc().SourceFailureLatch() == + &mozilla::FailureLatchInfallibleSource::Singleton()); + MOZ_RELEASE_ASSERT( + &std::as_const(writer.ChunkedWriteFunc()).SourceFailureLatch() == + &mozilla::FailureLatchInfallibleSource::Singleton()); + + MOZ_RELEASE_ASSERT(!writer.Fallible()); + MOZ_RELEASE_ASSERT(!writer.Failed()); + MOZ_RELEASE_ASSERT(!writer.GetFailure()); + MOZ_RELEASE_ASSERT(&writer.SourceFailureLatch() == + &mozilla::FailureLatchInfallibleSource::Singleton()); + MOZ_RELEASE_ASSERT(&std::as_const(writer).SourceFailureLatch() == + &mozilla::FailureLatchInfallibleSource::Singleton()); + + // By default use a local UniqueJSONStrings, otherwise use the one provided. + mozilla::baseprofiler::UniqueJSONStrings localUniqueStrings{ + FailureLatchInfallibleSource::Singleton()}; + MOZ_RELEASE_ASSERT(!localUniqueStrings.Fallible()); + MOZ_RELEASE_ASSERT(!localUniqueStrings.Failed()); + MOZ_RELEASE_ASSERT(!localUniqueStrings.GetFailure()); + MOZ_RELEASE_ASSERT(&localUniqueStrings.SourceFailureLatch() == + &mozilla::FailureLatchInfallibleSource::Singleton()); + MOZ_RELEASE_ASSERT(&std::as_const(localUniqueStrings).SourceFailureLatch() == + &mozilla::FailureLatchInfallibleSource::Singleton()); + + mozilla::baseprofiler::UniqueJSONStrings& uniqueStrings = + aUniqueStringsOrNull ? *aUniqueStringsOrNull : localUniqueStrings; + MOZ_RELEASE_ASSERT(!uniqueStrings.Failed()); + MOZ_RELEASE_ASSERT(!uniqueStrings.GetFailure()); + + writer.Start(); + { + writer.StartArrayProperty("data"); + { std::forward<F>(aF)(writer, uniqueStrings); } + writer.EndArray(); + + writer.StartArrayProperty("stringTable"); + { uniqueStrings.SpliceStringTableElements(writer); } + writer.EndArray(); + } + writer.End(); + + MOZ_RELEASE_ASSERT(!uniqueStrings.Failed()); + MOZ_RELEASE_ASSERT(!uniqueStrings.GetFailure()); + + MOZ_RELEASE_ASSERT(!writer.ChunkedWriteFunc().Failed()); + MOZ_RELEASE_ASSERT(!writer.ChunkedWriteFunc().GetFailure()); + + MOZ_RELEASE_ASSERT(!writer.Failed()); + MOZ_RELEASE_ASSERT(!writer.GetFailure()); + + UniquePtr<char[]> jsonString = writer.ChunkedWriteFunc().CopyData(); + MOZ_RELEASE_ASSERT(jsonString); + std::string_view jsonStringView(jsonString.get()); + const size_t length = writer.ChunkedWriteFunc().Length(); + MOZ_RELEASE_ASSERT(length == jsonStringView.length()); + std::string expected = "{\"data\":["; + expected += aExpectedData; + expected += "],\"stringTable\":["; + expected += aExpectedUniqueStrings; + expected += "]}"; + if (jsonStringView != expected) { + fprintf(stderr, + "Expected:\n" + "------\n"); + printEscaped(expected); + fprintf(stderr, + "\n" + "------\n" + "Actual:\n" + "------\n"); + printEscaped(jsonStringView); + fprintf(stderr, + "\n" + "------\n"); + } + MOZ_RELEASE_ASSERT(jsonStringView == expected); +} + +void TestUniqueJSONStrings() { + printf("TestUniqueJSONStrings...\n"); + + using SCJW = mozilla::baseprofiler::SpliceableChunkedJSONWriter; + using UJS = mozilla::baseprofiler::UniqueJSONStrings; + + // Empty everything. + VerifyUniqueStringContents([](SCJW& aWriter, UJS& aUniqueStrings) {}, "", ""); + + // Empty unique strings. + VerifyUniqueStringContents( + [](SCJW& aWriter, UJS& aUniqueStrings) { + aWriter.StringElement("string"); + }, + R"("string")", ""); + + // One unique string. + VerifyUniqueStringContents( + [](SCJW& aWriter, UJS& aUniqueStrings) { + aUniqueStrings.WriteElement(aWriter, "string"); + }, + "0", R"("string")"); + + // One unique string twice. + VerifyUniqueStringContents( + [](SCJW& aWriter, UJS& aUniqueStrings) { + aUniqueStrings.WriteElement(aWriter, "string"); + aUniqueStrings.WriteElement(aWriter, "string"); + }, + "0,0", R"("string")"); + + // Two single unique strings. + VerifyUniqueStringContents( + [](SCJW& aWriter, UJS& aUniqueStrings) { + aUniqueStrings.WriteElement(aWriter, "string0"); + aUniqueStrings.WriteElement(aWriter, "string1"); + }, + "0,1", R"("string0","string1")"); + + // Two unique strings with repetition. + VerifyUniqueStringContents( + [](SCJW& aWriter, UJS& aUniqueStrings) { + aUniqueStrings.WriteElement(aWriter, "string0"); + aUniqueStrings.WriteElement(aWriter, "string1"); + aUniqueStrings.WriteElement(aWriter, "string0"); + }, + "0,1,0", R"("string0","string1")"); + + // Mix some object properties, for coverage. + VerifyUniqueStringContents( + [](SCJW& aWriter, UJS& aUniqueStrings) { + aUniqueStrings.WriteElement(aWriter, "string0"); + aWriter.StartObjectElement(); + { + aUniqueStrings.WriteProperty(aWriter, "p0", "prop"); + aUniqueStrings.WriteProperty(aWriter, "p1", "string0"); + aUniqueStrings.WriteProperty(aWriter, "p2", "prop"); + } + aWriter.EndObject(); + aUniqueStrings.WriteElement(aWriter, "string1"); + aUniqueStrings.WriteElement(aWriter, "string0"); + aUniqueStrings.WriteElement(aWriter, "prop"); + }, + R"(0,{"p0":1,"p1":0,"p2":1},2,0,1)", R"("string0","prop","string1")"); + + // Unique string table with pre-existing data. + { + UJS ujs{FailureLatchInfallibleSource::Singleton()}; + { + SCJW writer{FailureLatchInfallibleSource::Singleton()}; + ujs.WriteElement(writer, "external0"); + ujs.WriteElement(writer, "external1"); + ujs.WriteElement(writer, "external0"); + } + VerifyUniqueStringContents( + [](SCJW& aWriter, UJS& aUniqueStrings) { + aUniqueStrings.WriteElement(aWriter, "string0"); + aUniqueStrings.WriteElement(aWriter, "string1"); + aUniqueStrings.WriteElement(aWriter, "string0"); + }, + "2,3,2", R"("external0","external1","string0","string1")", &ujs); + } + + // Unique string table with pre-existing data from another table. + { + UJS ujs{FailureLatchInfallibleSource::Singleton()}; + { + SCJW writer{FailureLatchInfallibleSource::Singleton()}; + ujs.WriteElement(writer, "external0"); + ujs.WriteElement(writer, "external1"); + ujs.WriteElement(writer, "external0"); + } + UJS ujsCopy(FailureLatchInfallibleSource::Singleton(), ujs, + mozilla::ProgressLogger{}); + VerifyUniqueStringContents( + [](SCJW& aWriter, UJS& aUniqueStrings) { + aUniqueStrings.WriteElement(aWriter, "string0"); + aUniqueStrings.WriteElement(aWriter, "string1"); + aUniqueStrings.WriteElement(aWriter, "string0"); + }, + "2,3,2", R"("external0","external1","string0","string1")", &ujs); + } + + // Unique string table through SpliceableJSONWriter. + VerifyUniqueStringContents( + [](SCJW& aWriter, UJS& aUniqueStrings) { + aWriter.SetUniqueStrings(aUniqueStrings); + aWriter.UniqueStringElement("string0"); + aWriter.StartObjectElement(); + { + aWriter.UniqueStringProperty("p0", "prop"); + aWriter.UniqueStringProperty("p1", "string0"); + aWriter.UniqueStringProperty("p2", "prop"); + } + aWriter.EndObject(); + aWriter.UniqueStringElement("string1"); + aWriter.UniqueStringElement("string0"); + aWriter.UniqueStringElement("prop"); + aWriter.ResetUniqueStrings(); + }, + R"(0,{"p0":1,"p1":0,"p2":1},2,0,1)", R"("string0","prop","string1")"); + + printf("TestUniqueJSONStrings done\n"); +} + +void StreamMarkers(const mozilla::ProfileChunkedBuffer& aBuffer, + mozilla::baseprofiler::SpliceableJSONWriter& aWriter) { + aWriter.StartArrayProperty("data"); + { + aBuffer.ReadEach([&](mozilla::ProfileBufferEntryReader& aEntryReader) { + mozilla::ProfileBufferEntryKind entryKind = + aEntryReader.ReadObject<mozilla::ProfileBufferEntryKind>(); + MOZ_RELEASE_ASSERT(entryKind == mozilla::ProfileBufferEntryKind::Marker); + + mozilla::base_profiler_markers_detail::DeserializeAfterKindAndStream( + aEntryReader, + [&](const mozilla::baseprofiler::BaseProfilerThreadId&) { + return &aWriter; + }, + [&](mozilla::ProfileChunkedBuffer&) { + aWriter.StringElement("Real backtrace would be here"); + }, + [&](mozilla::base_profiler_markers_detail::Streaming:: + DeserializerTag) {}); + }); + } + aWriter.EndArray(); +} + +void PrintMarkers(const mozilla::ProfileChunkedBuffer& aBuffer) { + mozilla::baseprofiler::SpliceableJSONWriter writer( + mozilla::MakeUnique<mozilla::baseprofiler::OStreamJSONWriteFunc>( + std::cout), + FailureLatchInfallibleSource::Singleton()); + mozilla::baseprofiler::UniqueJSONStrings uniqueStrings{ + FailureLatchInfallibleSource::Singleton()}; + writer.SetUniqueStrings(uniqueStrings); + writer.Start(); + { + StreamMarkers(aBuffer, writer); + + writer.StartArrayProperty("stringTable"); + { uniqueStrings.SpliceStringTableElements(writer); } + writer.EndArray(); + } + writer.End(); + writer.ResetUniqueStrings(); +} + +static void SubTestMarkerCategory( + const mozilla::MarkerCategory& aMarkerCategory, + const mozilla::baseprofiler::ProfilingCategoryPair& aProfilingCategoryPair, + const mozilla::baseprofiler::ProfilingCategory& aProfilingCategory) { + MOZ_RELEASE_ASSERT(aMarkerCategory.CategoryPair() == aProfilingCategoryPair, + "Unexpected MarkerCategory::CategoryPair()"); + + MOZ_RELEASE_ASSERT( + mozilla::MarkerCategory(aProfilingCategoryPair).CategoryPair() == + aProfilingCategoryPair, + "MarkerCategory(<name>).CategoryPair() should return <name>"); + + MOZ_RELEASE_ASSERT(aMarkerCategory.GetCategory() == aProfilingCategory, + "Unexpected MarkerCategory::GetCategory()"); + + mozilla::ProfileBufferChunkManagerSingle chunkManager(512); + mozilla::ProfileChunkedBuffer buffer( + mozilla::ProfileChunkedBuffer::ThreadSafety::WithoutMutex, chunkManager); + mozilla::ProfileBufferBlockIndex i = buffer.PutObject(aMarkerCategory); + MOZ_RELEASE_ASSERT(i != mozilla::ProfileBufferBlockIndex{}, + "Failed serialization"); + buffer.ReadEach([&](mozilla::ProfileBufferEntryReader& aER, + mozilla::ProfileBufferBlockIndex aIndex) { + MOZ_RELEASE_ASSERT(aIndex == i, "Unexpected deserialization index"); + const auto readCategory = aER.ReadObject<mozilla::MarkerCategory>(); + MOZ_RELEASE_ASSERT(aER.RemainingBytes() == 0, + "Unexpected extra serialized bytes"); + MOZ_RELEASE_ASSERT(readCategory.CategoryPair() == aProfilingCategoryPair, + "Incorrect deserialization value"); + }); +} + +void TestMarkerCategory() { + printf("TestMarkerCategory...\n"); + + mozilla::ProfileBufferChunkManagerSingle chunkManager(512); + mozilla::ProfileChunkedBuffer buffer( + mozilla::ProfileChunkedBuffer::ThreadSafety::WithoutMutex, chunkManager); + +# define CATEGORY_ENUM_BEGIN_CATEGORY(name, labelAsString, color) +# define CATEGORY_ENUM_SUBCATEGORY(supercategory, name, labelAsString) \ + static_assert( \ + std::is_same_v<decltype(mozilla::baseprofiler::category::name), \ + const mozilla::MarkerCategory>, \ + "baseprofiler::category::<name> should be a const MarkerCategory"); \ + \ + SubTestMarkerCategory( \ + mozilla::baseprofiler::category::name, \ + mozilla::baseprofiler::ProfilingCategoryPair::name, \ + mozilla::baseprofiler::ProfilingCategory::supercategory); +# define CATEGORY_ENUM_END_CATEGORY + MOZ_PROFILING_CATEGORY_LIST(CATEGORY_ENUM_BEGIN_CATEGORY, + CATEGORY_ENUM_SUBCATEGORY, + CATEGORY_ENUM_END_CATEGORY) +# undef CATEGORY_ENUM_BEGIN_CATEGORY +# undef CATEGORY_ENUM_SUBCATEGORY +# undef CATEGORY_ENUM_END_CATEGORY + + printf("TestMarkerCategory done\n"); +} + +void TestMarkerThreadId() { + printf("TestMarkerThreadId...\n"); + + MOZ_RELEASE_ASSERT(MarkerThreadId{}.IsUnspecified()); + MOZ_RELEASE_ASSERT(!MarkerThreadId::MainThread().IsUnspecified()); + MOZ_RELEASE_ASSERT(!MarkerThreadId::CurrentThread().IsUnspecified()); + + MOZ_RELEASE_ASSERT(!MarkerThreadId{ + mozilla::baseprofiler::BaseProfilerThreadId::FromNumber(42)} + .IsUnspecified()); + MOZ_RELEASE_ASSERT( + MarkerThreadId{ + mozilla::baseprofiler::BaseProfilerThreadId::FromNumber(42)} + .ThreadId() + .ToNumber() == 42); + + // We'll assume that this test runs in the main thread (which should be true + // when called from the `main` function). + MOZ_RELEASE_ASSERT(MarkerThreadId::MainThread().ThreadId() == + mozilla::baseprofiler::profiler_main_thread_id()); + + MOZ_RELEASE_ASSERT(MarkerThreadId::CurrentThread().ThreadId() == + mozilla::baseprofiler::profiler_current_thread_id()); + + MOZ_RELEASE_ASSERT(MarkerThreadId::CurrentThread().ThreadId() == + mozilla::baseprofiler::profiler_main_thread_id()); + + std::thread testThread([]() { + MOZ_RELEASE_ASSERT(!MarkerThreadId::MainThread().IsUnspecified()); + MOZ_RELEASE_ASSERT(!MarkerThreadId::CurrentThread().IsUnspecified()); + + MOZ_RELEASE_ASSERT(MarkerThreadId::MainThread().ThreadId() == + mozilla::baseprofiler::profiler_main_thread_id()); + + MOZ_RELEASE_ASSERT(MarkerThreadId::CurrentThread().ThreadId() == + mozilla::baseprofiler::profiler_current_thread_id()); + + MOZ_RELEASE_ASSERT(MarkerThreadId::CurrentThread().ThreadId() != + mozilla::baseprofiler::profiler_main_thread_id()); + }); + testThread.join(); + + printf("TestMarkerThreadId done\n"); +} + +void TestMarkerNoPayload() { + printf("TestMarkerNoPayload...\n"); + + mozilla::ProfileBufferChunkManagerSingle chunkManager(512); + mozilla::ProfileChunkedBuffer buffer( + mozilla::ProfileChunkedBuffer::ThreadSafety::WithoutMutex, chunkManager); + + mozilla::ProfileBufferBlockIndex i0 = + mozilla::baseprofiler::AddMarkerToBuffer( + buffer, "literal", mozilla::baseprofiler::category::OTHER_Profiling); + MOZ_RELEASE_ASSERT(i0); + + const std::string dynamic = "dynamic"; + mozilla::ProfileBufferBlockIndex i1 = + mozilla::baseprofiler::AddMarkerToBuffer( + buffer, dynamic, + mozilla::baseprofiler::category::GRAPHICS_FlushingAsyncPaints, {}); + MOZ_RELEASE_ASSERT(i1); + MOZ_RELEASE_ASSERT(i1 > i0); + + mozilla::ProfileBufferBlockIndex i2 = + mozilla::baseprofiler::AddMarkerToBuffer( + buffer, std::string_view("string_view"), + mozilla::baseprofiler::category::GRAPHICS_FlushingAsyncPaints, {}); + MOZ_RELEASE_ASSERT(i2); + MOZ_RELEASE_ASSERT(i2 > i1); + +# ifdef DEBUG + buffer.Dump(); +# endif + + PrintMarkers(buffer); + + printf("TestMarkerNoPayload done\n"); +} + +void TestUserMarker() { + printf("TestUserMarker...\n"); + + // User-defined marker type with text. + // It's fine to define it right in the function where it's used. + struct MarkerTypeTestMinimal { + static constexpr Span<const char> MarkerTypeName() { + return MakeStringSpan("test-minimal"); + } + static void StreamJSONMarkerData( + mozilla::baseprofiler::SpliceableJSONWriter& aWriter, + const std::string& aText) { + aWriter.StringProperty("text", aText); + } + static mozilla::MarkerSchema MarkerTypeDisplay() { + using MS = mozilla::MarkerSchema; + MS schema{MS::Location::MarkerChart, MS::Location::MarkerTable}; + schema.SetTooltipLabel("tooltip for test-minimal"); + schema.AddKeyLabelFormatSearchable("text", "Text", MS::Format::String, + MS::Searchable::Searchable); + return schema; + } + }; + + mozilla::ProfileBufferChunkManagerSingle chunkManager(1024); + mozilla::ProfileChunkedBuffer buffer( + mozilla::ProfileChunkedBuffer::ThreadSafety::WithoutMutex, chunkManager); + + MOZ_RELEASE_ASSERT(mozilla::baseprofiler::AddMarkerToBuffer( + buffer, "test2", mozilla::baseprofiler::category::OTHER_Profiling, {}, + MarkerTypeTestMinimal{}, std::string("payload text"))); + + MOZ_RELEASE_ASSERT(mozilla::baseprofiler::AddMarkerToBuffer( + buffer, "test2", mozilla::baseprofiler::category::OTHER_Profiling, + mozilla::MarkerThreadId( + mozilla::baseprofiler::BaseProfilerThreadId::FromNumber(123)), + MarkerTypeTestMinimal{}, std::string("ThreadId(123)"))); + + auto start = mozilla::TimeStamp::Now(); + + MOZ_RELEASE_ASSERT(mozilla::baseprofiler::AddMarkerToBuffer( + buffer, "test2", mozilla::baseprofiler::category::OTHER_Profiling, + mozilla::MarkerTiming::InstantAt(start), MarkerTypeTestMinimal{}, + std::string("InstantAt(start)"))); + + auto then = mozilla::TimeStamp::Now(); + + MOZ_RELEASE_ASSERT(mozilla::baseprofiler::AddMarkerToBuffer( + buffer, "test2", mozilla::baseprofiler::category::OTHER_Profiling, + mozilla::MarkerTiming::IntervalStart(start), MarkerTypeTestMinimal{}, + std::string("IntervalStart(start)"))); + + MOZ_RELEASE_ASSERT(mozilla::baseprofiler::AddMarkerToBuffer( + buffer, "test2", mozilla::baseprofiler::category::OTHER_Profiling, + mozilla::MarkerTiming::IntervalEnd(then), MarkerTypeTestMinimal{}, + std::string("IntervalEnd(then)"))); + + MOZ_RELEASE_ASSERT(mozilla::baseprofiler::AddMarkerToBuffer( + buffer, "test2", mozilla::baseprofiler::category::OTHER_Profiling, + mozilla::MarkerTiming::Interval(start, then), MarkerTypeTestMinimal{}, + std::string("Interval(start, then)"))); + + MOZ_RELEASE_ASSERT(mozilla::baseprofiler::AddMarkerToBuffer( + buffer, "test2", mozilla::baseprofiler::category::OTHER_Profiling, + mozilla::MarkerTiming::IntervalUntilNowFrom(start), + MarkerTypeTestMinimal{}, std::string("IntervalUntilNowFrom(start)"))); + + MOZ_RELEASE_ASSERT(mozilla::baseprofiler::AddMarkerToBuffer( + buffer, "test2", mozilla::baseprofiler::category::OTHER_Profiling, + mozilla::MarkerStack::NoStack(), MarkerTypeTestMinimal{}, + std::string("NoStack"))); + // Note: We cannot test stack-capture here, because the profiler is not + // initialized. + + MOZ_RELEASE_ASSERT(mozilla::baseprofiler::AddMarkerToBuffer( + buffer, "test2", mozilla::baseprofiler::category::OTHER_Profiling, + mozilla::MarkerInnerWindowId(123), MarkerTypeTestMinimal{}, + std::string("InnerWindowId(123)"))); + +# ifdef DEBUG + buffer.Dump(); +# endif + + PrintMarkers(buffer); + + printf("TestUserMarker done\n"); +} + +void TestPredefinedMarkers() { + printf("TestPredefinedMarkers...\n"); + + mozilla::ProfileBufferChunkManagerSingle chunkManager(1024); + mozilla::ProfileChunkedBuffer buffer( + mozilla::ProfileChunkedBuffer::ThreadSafety::WithoutMutex, chunkManager); + + MOZ_RELEASE_ASSERT(mozilla::baseprofiler::AddMarkerToBuffer( + buffer, std::string_view("tracing"), + mozilla::baseprofiler::category::OTHER, {}, + mozilla::baseprofiler::markers::Tracing{}, "category")); + + MOZ_RELEASE_ASSERT(mozilla::baseprofiler::AddMarkerToBuffer( + buffer, std::string_view("text"), mozilla::baseprofiler::category::OTHER, + {}, mozilla::baseprofiler::markers::TextMarker{}, "text text")); + + MOZ_RELEASE_ASSERT(mozilla::baseprofiler::AddMarkerToBuffer( + buffer, std::string_view("media"), mozilla::baseprofiler::category::OTHER, + {}, mozilla::baseprofiler::markers::MediaSampleMarker{}, 123, 456, 789)); + + MOZ_RELEASE_ASSERT(mozilla::baseprofiler::AddMarkerToBuffer( + buffer, std::string_view("media"), mozilla::baseprofiler::category::OTHER, + {}, mozilla::baseprofiler::markers::VideoFallingBehindMarker{}, 123, + 456)); + +# ifdef DEBUG + buffer.Dump(); +# endif + + PrintMarkers(buffer); + + printf("TestPredefinedMarkers done\n"); +} + +void TestProfilerMarkers() { + printf( + "TestProfilerMarkers -- pid: %" PRIu64 ", tid: %" PRIu64 "\n", + uint64_t(mozilla::baseprofiler::profiler_current_process_id().ToNumber()), + uint64_t(mozilla::baseprofiler::profiler_current_thread_id().ToNumber())); + // ::SleepMilli(10000); + + TestUniqueJSONStrings(); + TestMarkerCategory(); + TestMarkerThreadId(); + TestMarkerNoPayload(); + TestUserMarker(); + TestPredefinedMarkers(); + + printf("TestProfilerMarkers done\n"); +} + +#else // MOZ_GECKO_PROFILER + +// Testing that macros are still #defined (but do nothing) when +// MOZ_GECKO_PROFILER is disabled. +void TestProfiler() { + // These don't need to make sense, we just want to know that they're defined + // and don't do anything. + +# ifndef AUTO_BASE_PROFILER_INIT +# error AUTO_BASE_PROFILER_INIT not #defined +# endif // AUTO_BASE_PROFILER_INIT + AUTO_BASE_PROFILER_INIT; + +# ifndef AUTO_BASE_PROFILER_MARKER_TEXT +# error AUTO_BASE_PROFILER_MARKER_TEXT not #defined +# endif // AUTO_BASE_PROFILER_MARKER_TEXT + +# ifndef AUTO_BASE_PROFILER_LABEL +# error AUTO_BASE_PROFILER_LABEL not #defined +# endif // AUTO_BASE_PROFILER_LABEL + +# ifndef AUTO_BASE_PROFILER_THREAD_SLEEP +# error AUTO_BASE_PROFILER_THREAD_SLEEP not #defined +# endif // AUTO_BASE_PROFILER_THREAD_SLEEP + AUTO_BASE_PROFILER_THREAD_SLEEP; + +# ifndef BASE_PROFILER_MARKER_UNTYPED +# error BASE_PROFILER_MARKER_UNTYPED not #defined +# endif // BASE_PROFILER_MARKER_UNTYPED + +# ifndef BASE_PROFILER_MARKER +# error BASE_PROFILER_MARKER not #defined +# endif // BASE_PROFILER_MARKER + +# ifndef BASE_PROFILER_MARKER_TEXT +# error BASE_PROFILER_MARKER_TEXT not #defined +# endif // BASE_PROFILER_MARKER_TEXT + + MOZ_RELEASE_ASSERT(!mozilla::baseprofiler::profiler_get_backtrace(), + "profiler_get_backtrace should return nullptr"); + mozilla::ProfileChunkedBuffer buffer( + mozilla::ProfileChunkedBuffer::ThreadSafety::WithoutMutex); + MOZ_RELEASE_ASSERT(!mozilla::baseprofiler::profiler_capture_backtrace_into( + buffer, mozilla::StackCaptureOptions::Full), + "profiler_capture_backtrace_into should return false"); + MOZ_RELEASE_ASSERT(!mozilla::baseprofiler::profiler_capture_backtrace(), + "profiler_capture_backtrace should return nullptr"); +} + +// Testing that macros are still #defined (but do nothing) when +// MOZ_GECKO_PROFILER is disabled. +void TestProfilerMarkers() { + // These don't need to make sense, we just want to know that they're defined + // and don't do anything. +} + +#endif // MOZ_GECKO_PROFILER else + +#if defined(XP_WIN) +int wmain() +#else +int main() +#endif // defined(XP_WIN) +{ +#ifdef MOZ_GECKO_PROFILER + printf("BaseTestProfiler -- pid: %" PRIu64 ", tid: %" PRIu64 "\n", + uint64_t(baseprofiler::profiler_current_process_id().ToNumber()), + uint64_t(baseprofiler::profiler_current_thread_id().ToNumber())); + // ::SleepMilli(10000); +#endif // MOZ_GECKO_PROFILER + + TestFailureLatch(); + TestProfilerUtils(); + TestBaseAndProfilerDetail(); + TestSharedMutex(); + TestProportionValue(); + TestProgressLogger(); + // Note that there are two `TestProfiler{,Markers}` functions above, depending + // on whether MOZ_GECKO_PROFILER is #defined. + { + printf("profiler_init()...\n"); + AUTO_BASE_PROFILER_INIT; + + TestProfiler(); + TestProfilerMarkers(); + } + + return 0; +} diff --git a/mozglue/tests/TestIntegerPrintfMacros.cpp b/mozglue/tests/TestIntegerPrintfMacros.cpp new file mode 100644 index 0000000000..71fb26fc47 --- /dev/null +++ b/mozglue/tests/TestIntegerPrintfMacros.cpp @@ -0,0 +1,1128 @@ +/* -*- 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 "mozilla/Assertions.h" +#include "mozilla/Sprintf.h" + +#include <string.h> +#include <inttypes.h> + +/* Output array and poisoning method shared by all tests. */ +static char gOutput[32]; + +static void PoisonOutput() { memset(gOutput, 0xDA, sizeof(gOutput)); } + +/* + * The fprintf macros for signed integers are: + * + * PRIdN PRIdLEASTN PRIdFASTN PRIdMAX PRIdPTR + * PRIiN PRIiLEASTN PRIiFASTN PRIiMAX PRIiPTR + * + * In these names N is the width of the type as described in C99 7.18.1. + */ + +static void TestPrintSigned8() { + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRId8, int8_t(-17)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "-17")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIi8, int8_t(42)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "42")); +} + +static void TestPrintSigned16() { + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRId16, int16_t(-289)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "-289")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIi16, int16_t(728)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "728")); +} + +static void TestPrintSigned32() { + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRId32, int32_t(-342178)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "-342178")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIi32, int32_t(5719283)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "5719283")); +} + +static void TestPrintSigned64() { + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRId64, int64_t(-INT64_C(432157943248732))); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "-432157943248732")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIi64, int64_t(INT64_C(325719232983))); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "325719232983")); +} + +static void TestPrintSignedN() { + TestPrintSigned8(); + TestPrintSigned16(); + TestPrintSigned32(); + TestPrintSigned64(); +} + +static void TestPrintSignedLeast8() { + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIdLEAST8, int_least8_t(-17)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "-17")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIiLEAST8, int_least8_t(42)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "42")); +} + +static void TestPrintSignedLeast16() { + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIdLEAST16, int_least16_t(-289)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "-289")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIiLEAST16, int_least16_t(728)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "728")); +} + +static void TestPrintSignedLeast32() { + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIdLEAST32, int_least32_t(-342178)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "-342178")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIiLEAST32, int_least32_t(5719283)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "5719283")); +} + +static void TestPrintSignedLeast64() { + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIdLEAST64, + int_least64_t(-INT64_C(432157943248732))); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "-432157943248732")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIiLEAST64, + int_least64_t(INT64_C(325719232983))); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "325719232983")); +} + +static void TestPrintSignedLeastN() { + TestPrintSignedLeast8(); + TestPrintSignedLeast16(); + TestPrintSignedLeast32(); + TestPrintSignedLeast64(); +} + +static void TestPrintSignedFast8() { + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIdFAST8, int_fast8_t(-17)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "-17")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIiFAST8, int_fast8_t(42)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "42")); +} + +static void TestPrintSignedFast16() { + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIdFAST16, int_fast16_t(-289)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "-289")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIiFAST16, int_fast16_t(728)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "728")); +} + +static void TestPrintSignedFast32() { + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIdFAST32, int_fast32_t(-342178)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "-342178")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIiFAST32, int_fast32_t(5719283)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "5719283")); +} + +static void TestPrintSignedFast64() { + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIdFAST64, + int_fast64_t(-INT64_C(432157943248732))); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "-432157943248732")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIiFAST64, int_fast64_t(INT64_C(325719232983))); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "325719232983")); +} + +static void TestPrintSignedFastN() { + TestPrintSignedFast8(); + TestPrintSignedFast16(); + TestPrintSignedFast32(); + TestPrintSignedFast64(); +} + +static void TestPrintSignedMax() { + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIdMAX, intmax_t(-INTMAX_C(432157943248732))); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "-432157943248732")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIiMAX, intmax_t(INTMAX_C(325719232983))); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "325719232983")); +} + +static void TestPrintSignedPtr() { + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIdPTR, + intptr_t(reinterpret_cast<void*>(12345678))); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "12345678")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIiPTR, + intptr_t(reinterpret_cast<void*>(87654321))); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "87654321")); +} + +static void TestPrintSigned() { + TestPrintSignedN(); + TestPrintSignedLeastN(); + TestPrintSignedFastN(); + TestPrintSignedMax(); + TestPrintSignedPtr(); +} + +/* + * The fprintf macros for unsigned integers are: + * + * PRIoN PRIoLEASTN PRIoFASTN PRIoMAX PRIoPTR + * PRIuN PRIuLEASTN PRIuFASTN PRIuMAX PRIuPTR + * PRIxN PRIxLEASTN PRIxFASTN PRIxMAX PRIxPTR + * PRIXN PRIXLEASTN PRIXFASTN PRIXMAX PRIXPTR + * + * In these names N is the width of the type as described in C99 7.18.1. + */ + +static void TestPrintUnsigned8() { + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIo8, uint8_t(042)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "42")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIu8, uint8_t(17)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "17")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIx8, uint8_t(0x2a)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "2a")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIX8, uint8_t(0xCD)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "CD")); +} + +static void TestPrintUnsigned16() { + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIo16, uint16_t(04242)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "4242")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIu16, uint16_t(1717)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "1717")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIx16, uint16_t(0x2a2a)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "2a2a")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIX16, uint16_t(0xCDCD)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "CDCD")); +} + +static void TestPrintUnsigned32() { + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIo32, uint32_t(0424242)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "424242")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIu32, uint32_t(171717)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "171717")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIx32, uint32_t(0x2a2a2a)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "2a2a2a")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIX32, uint32_t(0xCDCDCD)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "CDCDCD")); +} + +static void TestPrintUnsigned64() { + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIo64, uint64_t(UINT64_C(0424242424242))); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "424242424242")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIu64, uint64_t(UINT64_C(17171717171717171717))); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "17171717171717171717")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIx64, uint64_t(UINT64_C(0x2a2a2a2a2a2a2a))); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "2a2a2a2a2a2a2a")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIX64, uint64_t(UINT64_C(0xCDCDCDCDCDCD))); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "CDCDCDCDCDCD")); +} + +static void TestPrintUnsignedN() { + TestPrintUnsigned8(); + TestPrintUnsigned16(); + TestPrintUnsigned32(); + TestPrintUnsigned64(); +} + +static void TestPrintUnsignedLeast8() { + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIoLEAST8, uint_least8_t(042)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "42")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIuLEAST8, uint_least8_t(17)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "17")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIxLEAST8, uint_least8_t(0x2a)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "2a")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIXLEAST8, uint_least8_t(0xCD)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "CD")); +} + +static void TestPrintUnsignedLeast16() { + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIoLEAST16, uint_least16_t(04242)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "4242")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIuLEAST16, uint_least16_t(1717)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "1717")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIxLEAST16, uint_least16_t(0x2a2a)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "2a2a")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIXLEAST16, uint_least16_t(0xCDCD)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "CDCD")); +} + +static void TestPrintUnsignedLeast32() { + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIoLEAST32, uint_least32_t(0424242)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "424242")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIuLEAST32, uint_least32_t(171717)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "171717")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIxLEAST32, uint_least32_t(0x2a2a2a)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "2a2a2a")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIXLEAST32, uint_least32_t(0xCDCDCD)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "CDCDCD")); +} + +static void TestPrintUnsignedLeast64() { + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIoLEAST64, + uint_least64_t(UINT64_C(0424242424242))); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "424242424242")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIuLEAST64, + uint_least64_t(UINT64_C(17171717171717171717))); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "17171717171717171717")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIxLEAST64, + uint_least64_t(UINT64_C(0x2a2a2a2a2a2a2a))); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "2a2a2a2a2a2a2a")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIXLEAST64, + uint_least64_t(UINT64_C(0xCDCDCDCDCDCD))); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "CDCDCDCDCDCD")); +} + +static void TestPrintUnsignedLeastN() { + TestPrintUnsignedLeast8(); + TestPrintUnsignedLeast16(); + TestPrintUnsignedLeast32(); + TestPrintUnsignedLeast64(); +} + +static void TestPrintUnsignedFast8() { + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIoFAST8, uint_fast8_t(042)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "42")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIuFAST8, uint_fast8_t(17)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "17")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIxFAST8, uint_fast8_t(0x2a)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "2a")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIXFAST8, uint_fast8_t(0xCD)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "CD")); +} + +static void TestPrintUnsignedFast16() { + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIoFAST16, uint_fast16_t(04242)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "4242")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIuFAST16, uint_fast16_t(1717)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "1717")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIxFAST16, uint_fast16_t(0x2a2a)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "2a2a")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIXFAST16, uint_fast16_t(0xCDCD)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "CDCD")); +} + +static void TestPrintUnsignedFast32() { + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIoFAST32, uint_fast32_t(0424242)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "424242")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIuFAST32, uint_fast32_t(171717)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "171717")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIxFAST32, uint_fast32_t(0x2a2a2a)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "2a2a2a")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIXFAST32, uint_fast32_t(0xCDCDCD)); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "CDCDCD")); +} + +static void TestPrintUnsignedFast64() { + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIoFAST64, + uint_fast64_t(UINT64_C(0424242424242))); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "424242424242")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIuFAST64, + uint_fast64_t(UINT64_C(17171717171717171717))); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "17171717171717171717")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIxFAST64, + uint_fast64_t(UINT64_C(0x2a2a2a2a2a2a2a))); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "2a2a2a2a2a2a2a")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIXFAST64, + uint_fast64_t(UINT64_C(0xCDCDCDCDCDCD))); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "CDCDCDCDCDCD")); +} + +static void TestPrintUnsignedFastN() { + TestPrintUnsignedFast8(); + TestPrintUnsignedFast16(); + TestPrintUnsignedFast32(); + TestPrintUnsignedFast64(); +} + +static void TestPrintUnsignedMax() { + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIoMAX, uintmax_t(UINTMAX_C(432157943248732))); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "14220563454333534")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIuMAX, uintmax_t(UINTMAX_C(325719232983))); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "325719232983")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIxMAX, uintmax_t(UINTMAX_C(327281321873))); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "4c337ca791")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIXMAX, uintmax_t(UINTMAX_C(912389523743523))); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "33DD03D75A323")); +} + +static void TestPrintUnsignedPtr() { + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIoPTR, + uintptr_t(reinterpret_cast<void*>(12345678))); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "57060516")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIuPTR, + uintptr_t(reinterpret_cast<void*>(87654321))); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "87654321")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIxPTR, + uintptr_t(reinterpret_cast<void*>(0x4c3a791))); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "4c3a791")); + + PoisonOutput(); + SprintfLiteral(gOutput, "%" PRIXPTR, + uintptr_t(reinterpret_cast<void*>(0xF328DB))); + MOZ_RELEASE_ASSERT(!strcmp(gOutput, "F328DB")); +} + +static void TestPrintUnsigned() { + TestPrintUnsignedN(); + TestPrintUnsignedLeastN(); + TestPrintUnsignedFastN(); + TestPrintUnsignedMax(); + TestPrintUnsignedPtr(); +} + +static void TestPrint() { + TestPrintSigned(); + TestPrintUnsigned(); +} + +/* + * The fscanf macros for signed integers are: + * + * SCNdN SCNdLEASTN SCNdFASTN SCNdMAX SCNdPTR + * SCNiN SCNiLEASTN SCNiFASTN SCNiMAX SCNiPTR + * + * In these names N is the width of the type as described in C99 7.18.1. + */ + +/* + * MSVC's scanf is insufficiently powerful to implement all the SCN* macros. + * Rather than support some subset of them, we instead support none of them. + * See the comment at the top of IntegerPrintfMacros.h. But in case we ever do + * support them, the following tests should adequately test implementation + * correctness. (Indeed, these tests *revealed* MSVC's limitations.) + * + * That said, even if MSVC ever picks up complete support, we still probably + * don't want to support these, because of the undefined-behavior issue noted + * further down in the comment atop IntegerPrintfMacros.h. + */ +#define SHOULD_TEST_SCANF_MACROS 0 + +#if SHOULD_TEST_SCANF_MACROS + +/* + * glibc header definitions for SCN{d,i,o,u,x}{,LEAST,FAST}8 use the "hh" length + * modifier, which is new in C99 (and C++11, by reference). We compile this + * file as C++11, so if "hh" is used in these macros, it's standard. But some + * versions of gcc wrongly think it isn't and warn about a "non-standard" + * modifier. And since these tests mostly exist to verify format-macro/type + * consistency (particularly through compiler warnings about incorrect formats), + * these warnings are unacceptable. So for now, compile tests for those macros + * only if we aren't compiling with gcc. + */ +# define SHOULD_TEST_8BIT_FORMAT_MACROS (!(MOZ_IS_GCC)) + +template <typename T> +union Input { + T mI; + unsigned char mPun[16]; +}; + +template <typename T> +static void PoisonInput(Input<T>& aInput) { + memset(aInput.mPun, 0xDA, sizeof(aInput.mPun)); +} + +template <typename T> +static bool ExtraBitsUntouched(const Input<T>& aInput) { + for (size_t i = sizeof(aInput.mI); i < sizeof(aInput); i++) { + if (aInput.mPun[i] != 0xDA) { + return false; + } + } + + return true; +} + +static void TestScanSigned8() { +# if SHOULD_TEST_8BIT_FORMAT_MACROS + Input<int8_t> u; + + PoisonInput(u); + sscanf("-17", "%" SCNd8, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == -17); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); + + PoisonInput(u); + sscanf("042", "%" SCNi8, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == 042); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); +# endif +} + +static void TestScanSigned16() { + Input<int16_t> u; + + PoisonInput(u); + sscanf("-1742", "%" SCNd16, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == -1742); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); + + PoisonInput(u); + sscanf("04217", "%" SCNi16, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == 04217); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); +} + +static void TestScanSigned32() { + Input<int32_t> u; + + PoisonInput(u); + sscanf("-174257", "%" SCNd32, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == -174257); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); + + PoisonInput(u); + sscanf("0423571", "%" SCNi32, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == 0423571); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); +} + +static void TestScanSigned64() { + Input<int64_t> u; + + PoisonInput(u); + sscanf("-17425238927232", "%" SCNd64, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == -INT64_C(17425238927232)); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); + + PoisonInput(u); + sscanf("042333576571", "%" SCNi64, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == INT64_C(042333576571)); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); +} + +static void TestScanSignedN() { + TestScanSigned8(); + TestScanSigned16(); + TestScanSigned32(); + TestScanSigned64(); +} + +static void TestScanSignedLeast8() { +# if SHOULD_TEST_8BIT_FORMAT_MACROS + Input<int_least8_t> u; + + PoisonInput(u); + sscanf("-17", "%" SCNdLEAST8, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == -17); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); + + PoisonInput(u); + sscanf("042", "%" SCNiLEAST8, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == 042); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); +# endif +} + +static void TestScanSignedLeast16() { + Input<int_least16_t> u; + + PoisonInput(u); + sscanf("-1742", "%" SCNdLEAST16, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == -1742); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); + + PoisonInput(u); + sscanf("04217", "%" SCNiLEAST16, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == 04217); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); +} + +static void TestScanSignedLeast32() { + Input<int_least32_t> u; + + PoisonInput(u); + sscanf("-174257", "%" SCNdLEAST32, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == -174257); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); + + PoisonInput(u); + sscanf("0423571", "%" SCNiLEAST32, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == 0423571); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); +} + +static void TestScanSignedLeast64() { + Input<int_least64_t> u; + + PoisonInput(u); + sscanf("-17425238927232", "%" SCNdLEAST64, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == -INT64_C(17425238927232)); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); + + PoisonInput(u); + sscanf("042333576571", "%" SCNiLEAST64, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == INT64_C(042333576571)); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); +} + +static void TestScanSignedLeastN() { + TestScanSignedLeast8(); + TestScanSignedLeast16(); + TestScanSignedLeast32(); + TestScanSignedLeast64(); +} + +static void TestScanSignedFast8() { +# if SHOULD_TEST_8BIT_FORMAT_MACROS + Input<int_fast8_t> u; + + PoisonInput(u); + sscanf("-17", "%" SCNdFAST8, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == -17); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); + + PoisonInput(u); + sscanf("042", "%" SCNiFAST8, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == 042); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); +# endif +} + +static void TestScanSignedFast16() { + Input<int_fast16_t> u; + + PoisonInput(u); + sscanf("-1742", "%" SCNdFAST16, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == -1742); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); + + PoisonInput(u); + sscanf("04217", "%" SCNiFAST16, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == 04217); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); +} + +static void TestScanSignedFast32() { + Input<int_fast32_t> u; + + PoisonInput(u); + sscanf("-174257", "%" SCNdFAST32, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == -174257); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); + + PoisonInput(u); + sscanf("0423571", "%" SCNiFAST32, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == 0423571); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); +} + +static void TestScanSignedFast64() { + Input<int_fast64_t> u; + + PoisonInput(u); + sscanf("-17425238927232", "%" SCNdFAST64, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == -INT64_C(17425238927232)); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); + + PoisonInput(u); + sscanf("042333576571", "%" SCNiFAST64, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == INT64_C(042333576571)); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); +} + +static void TestScanSignedFastN() { + TestScanSignedFast8(); + TestScanSignedFast16(); + TestScanSignedFast32(); + TestScanSignedFast64(); +} + +static void TestScanSignedMax() { + Input<intmax_t> u; + + PoisonInput(u); + sscanf("-432157943248732", "%" SCNdMAX, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == -INTMAX_C(432157943248732)); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); + + PoisonInput(u); + sscanf("04233357236571", "%" SCNiMAX, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == INTMAX_C(04233357236571)); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); +} + +static void TestScanSignedPtr() { + Input<intptr_t> u; + + PoisonInput(u); + sscanf("12345678", "%" SCNdPTR, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == intptr_t(reinterpret_cast<void*>(12345678))); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); + + PoisonInput(u); + sscanf("04233357236", "%" SCNiPTR, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == intptr_t(reinterpret_cast<void*>(04233357236))); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); +} + +static void TestScanSigned() { + TestScanSignedN(); + TestScanSignedLeastN(); + TestScanSignedFastN(); + TestScanSignedMax(); + TestScanSignedPtr(); +} + +/* + * The fscanf macros for unsigned integers are: + * + * SCNoN SCNoLEASTN SCNoFASTN SCNoMAX SCNoPTR + * SCNuN SCNuLEASTN SCNuFASTN SCNuMAX SCNuPTR + * SCNxN SCNxLEASTN SCNxFASTN SCNxMAX SCNxPTR + * + * In these names N is the width of the type as described in C99 7.18.1. + */ + +static void TestScanUnsigned8() { +# if SHOULD_TEST_8BIT_FORMAT_MACROS + Input<uint8_t> u; + + PoisonInput(u); + sscanf("17", "%" SCNo8, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == 017); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); + + PoisonInput(u); + sscanf("42", "%" SCNu8, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == 42); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); + + PoisonInput(u); + sscanf("2A", "%" SCNx8, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == 0x2A); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); +# endif +} + +static void TestScanUnsigned16() { + Input<uint16_t> u; + + PoisonInput(u); + sscanf("1742", "%" SCNo16, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == 01742); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); + + PoisonInput(u); + sscanf("4217", "%" SCNu16, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == 4217); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); + + PoisonInput(u); + sscanf("2ABC", "%" SCNx16, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == 0x2ABC); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); +} + +static void TestScanUnsigned32() { + Input<uint32_t> u; + + PoisonInput(u); + sscanf("17421742", "%" SCNo32, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == 017421742); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); + + PoisonInput(u); + sscanf("4217867", "%" SCNu32, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == 4217867); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); + + PoisonInput(u); + sscanf("2ABCBEEF", "%" SCNx32, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == 0x2ABCBEEF); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); +} + +static void TestScanUnsigned64() { + Input<uint64_t> u; + + PoisonInput(u); + sscanf("17421742173", "%" SCNo64, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == UINT64_C(017421742173)); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); + + PoisonInput(u); + sscanf("421786713579", "%" SCNu64, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == UINT64_C(421786713579)); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); + + PoisonInput(u); + sscanf("DEADBEEF7457E", "%" SCNx64, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == UINT64_C(0xDEADBEEF7457E)); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); +} + +static void TestScanUnsignedN() { + TestScanUnsigned8(); + TestScanUnsigned16(); + TestScanUnsigned32(); + TestScanUnsigned64(); +} + +static void TestScanUnsignedLeast8() { +# if SHOULD_TEST_8BIT_FORMAT_MACROS + Input<uint_least8_t> u; + + PoisonInput(u); + sscanf("17", "%" SCNoLEAST8, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == 017); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); + + PoisonInput(u); + sscanf("42", "%" SCNuLEAST8, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == 42); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); + + PoisonInput(u); + sscanf("2A", "%" SCNxLEAST8, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == 0x2A); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); +# endif +} + +static void TestScanUnsignedLeast16() { + Input<uint_least16_t> u; + + PoisonInput(u); + sscanf("1742", "%" SCNoLEAST16, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == 01742); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); + + PoisonInput(u); + sscanf("4217", "%" SCNuLEAST16, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == 4217); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); + + PoisonInput(u); + sscanf("2ABC", "%" SCNxLEAST16, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == 0x2ABC); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); +} + +static void TestScanUnsignedLeast32() { + Input<uint_least32_t> u; + + PoisonInput(u); + sscanf("17421742", "%" SCNoLEAST32, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == 017421742); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); + + PoisonInput(u); + sscanf("4217867", "%" SCNuLEAST32, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == 4217867); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); + + PoisonInput(u); + sscanf("2ABCBEEF", "%" SCNxLEAST32, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == 0x2ABCBEEF); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); +} + +static void TestScanUnsignedLeast64() { + Input<uint_least64_t> u; + + PoisonInput(u); + sscanf("17421742173", "%" SCNoLEAST64, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == UINT64_C(017421742173)); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); + + PoisonInput(u); + sscanf("421786713579", "%" SCNuLEAST64, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == UINT64_C(421786713579)); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); + + PoisonInput(u); + sscanf("DEADBEEF7457E", "%" SCNxLEAST64, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == UINT64_C(0xDEADBEEF7457E)); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); +} + +static void TestScanUnsignedLeastN() { + TestScanUnsignedLeast8(); + TestScanUnsignedLeast16(); + TestScanUnsignedLeast32(); + TestScanUnsignedLeast64(); +} + +static void TestScanUnsignedFast8() { +# if SHOULD_TEST_8BIT_FORMAT_MACROS + Input<uint_fast8_t> u; + + PoisonInput(u); + sscanf("17", "%" SCNoFAST8, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == 017); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); + + PoisonInput(u); + sscanf("42", "%" SCNuFAST8, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == 42); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); + + PoisonInput(u); + sscanf("2A", "%" SCNxFAST8, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == 0x2A); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); +# endif +} + +static void TestScanUnsignedFast16() { + Input<uint_fast16_t> u; + + PoisonInput(u); + sscanf("1742", "%" SCNoFAST16, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == 01742); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); + + PoisonInput(u); + sscanf("4217", "%" SCNuFAST16, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == 4217); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); + + PoisonInput(u); + sscanf("2ABC", "%" SCNxFAST16, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == 0x2ABC); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); +} + +static void TestScanUnsignedFast32() { + Input<uint_fast32_t> u; + + PoisonInput(u); + sscanf("17421742", "%" SCNoFAST32, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == 017421742); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); + + PoisonInput(u); + sscanf("4217867", "%" SCNuFAST32, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == 4217867); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); + + PoisonInput(u); + sscanf("2ABCBEEF", "%" SCNxFAST32, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == 0x2ABCBEEF); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); +} + +static void TestScanUnsignedFast64() { + Input<uint_fast64_t> u; + + PoisonInput(u); + sscanf("17421742173", "%" SCNoFAST64, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == UINT64_C(017421742173)); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); + + PoisonInput(u); + sscanf("421786713579", "%" SCNuFAST64, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == UINT64_C(421786713579)); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); + + PoisonInput(u); + sscanf("DEADBEEF7457E", "%" SCNxFAST64, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == UINT64_C(0xDEADBEEF7457E)); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); +} + +static void TestScanUnsignedFastN() { + TestScanUnsignedFast8(); + TestScanUnsignedFast16(); + TestScanUnsignedFast32(); + TestScanUnsignedFast64(); +} + +static void TestScanUnsignedMax() { + Input<uintmax_t> u; + + PoisonInput(u); + sscanf("14220563454333534", "%" SCNoMAX, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == UINTMAX_C(432157943248732)); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); + + PoisonInput(u); + sscanf("432157943248732", "%" SCNuMAX, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == UINTMAX_C(432157943248732)); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); + + PoisonInput(u); + sscanf("4c337ca791", "%" SCNxMAX, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == UINTMAX_C(327281321873)); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); +} + +static void TestScanUnsignedPtr() { + Input<uintptr_t> u; + + PoisonInput(u); + sscanf("57060516", "%" SCNoPTR, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == uintptr_t(reinterpret_cast<void*>(12345678))); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); + + PoisonInput(u); + sscanf("87654321", "%" SCNuPTR, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == uintptr_t(reinterpret_cast<void*>(87654321))); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); + + PoisonInput(u); + sscanf("4c3a791", "%" SCNxPTR, &u.mI); + MOZ_RELEASE_ASSERT(u.mI == uintptr_t(reinterpret_cast<void*>(0x4c3a791))); + MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); +} + +static void TestScanUnsigned() { + TestScanUnsignedN(); + TestScanUnsignedLeastN(); + TestScanUnsignedFastN(); + TestScanUnsignedMax(); + TestScanUnsignedPtr(); +} + +static void TestScan() { + TestScanSigned(); + TestScanUnsigned(); +} + +#endif /* SHOULD_TEST_SCANF_MACROS */ + +#if defined(XP_WIN) +int wmain() +#else +int main() +#endif // defined(XP_WIN) +{ + TestPrint(); +#if SHOULD_TEST_SCANF_MACROS + TestScan(); +#endif + return 0; +} diff --git a/mozglue/tests/TestNativeNt.cpp b/mozglue/tests/TestNativeNt.cpp new file mode 100644 index 0000000000..d4f671e33c --- /dev/null +++ b/mozglue/tests/TestNativeNt.cpp @@ -0,0 +1,606 @@ +/* -*- 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 https://mozilla.org/MPL/2.0/. */ + +#include "nscore.h" +#include "mozilla/NativeNt.h" +#include "mozilla/ThreadLocal.h" +#include "mozilla/UniquePtr.h" +#include "mozilla/WindowsEnumProcessModules.h" + +#include <stdio.h> +#include <windows.h> +#include <strsafe.h> + +const wchar_t kNormal[] = L"Foo.dll"; +const wchar_t kHex12[] = L"Foo.ABCDEF012345.dll"; +const wchar_t kHex15[] = L"ABCDEF012345678.dll"; +const wchar_t kHex16[] = L"ABCDEF0123456789.dll"; +const wchar_t kHex17[] = L"ABCDEF0123456789a.dll"; +const wchar_t kHex24[] = L"ABCDEF0123456789cdabef98.dll"; +const wchar_t kHex8[] = L"01234567.dll"; +const wchar_t kNonHex12[] = L"Foo.ABCDEFG12345.dll"; +const wchar_t kHex13[] = L"Foo.ABCDEF0123456.dll"; +const wchar_t kHex11[] = L"Foo.ABCDEF01234.dll"; +const wchar_t kPrefixedHex16[] = L"Pabcdef0123456789.dll"; +const uint32_t kTlsDataValue = 1234; +static MOZ_THREAD_LOCAL(uint32_t) sTlsData; + +// Need non-inline functions to bypass compiler optimization that the thread +// local storage pointer is cached in a register before accessing a thread-local +// variable. See bug 1803322 for a motivating example. +MOZ_NEVER_INLINE uint32_t getTlsData() { return sTlsData.get(); } +MOZ_NEVER_INLINE void setTlsData(uint32_t x) { sTlsData.set(x); } + +const char kFailFmt[] = + "TEST-FAILED | NativeNt | %s(%s) should have returned %s but did not\n"; + +#define RUN_TEST(fn, varName, expected) \ + if (fn(varName) == !expected) { \ + printf(kFailFmt, #fn, #varName, #expected); \ + return 1; \ + } + +#define EXPECT_FAIL(fn, varName) RUN_TEST(fn, varName, false) + +#define EXPECT_SUCCESS(fn, varName) RUN_TEST(fn, varName, true) + +using namespace mozilla; +using namespace mozilla::nt; + +bool TestVirtualQuery(HANDLE aProcess, LPCVOID aAddress) { + MEMORY_BASIC_INFORMATION info1 = {}, info2 = {}; + SIZE_T result1 = ::VirtualQueryEx(aProcess, aAddress, &info1, sizeof(info1)), + result2 = mozilla::nt::VirtualQueryEx(aProcess, aAddress, &info2, + sizeof(info2)); + if (result1 != result2) { + printf("TEST-FAILED | NativeNt | The returned values mismatch\n"); + return false; + } + + if (!result1) { + // Both APIs failed. + return true; + } + + if (memcmp(&info1, &info2, result1) != 0) { + printf("TEST-FAILED | NativeNt | The returned structures mismatch\n"); + return false; + } + + return true; +} + +// This class copies the self executable file to the %temp%\<outer>\<inner> +// folder. The length of its path is longer than MAX_PATH. +class LongNameModule { + wchar_t mOuterDirBuffer[MAX_PATH]; + wchar_t mInnerDirBuffer[MAX_PATH * 2]; + wchar_t mTargetFileBuffer[MAX_PATH * 2]; + + const wchar_t* mOuterDir; + const wchar_t* mInnerDir; + const wchar_t* mTargetFile; + + public: + explicit LongNameModule(const wchar_t* aNewLeafNameAfterCopy) + : mOuterDir(nullptr), mInnerDir(nullptr), mTargetFile(nullptr) { + const wchar_t kFolderName160Chars[] = + L"0123456789ABCDEF0123456789ABCDEF" + L"0123456789ABCDEF0123456789ABCDEF" + L"0123456789ABCDEF0123456789ABCDEF" + L"0123456789ABCDEF0123456789ABCDEF" + L"0123456789ABCDEF0123456789ABCDEF"; + UniquePtr<wchar_t[]> thisExe = GetFullBinaryPath(); + if (!thisExe) { + return; + } + + // If the buffer is too small, GetTempPathW returns the required + // length including a null character, while on a successful case + // it returns the number of copied characters which does not include + // a null character. This means len == MAX_PATH should never happen + // and len > MAX_PATH means GetTempPathW failed. + wchar_t tempDir[MAX_PATH]; + DWORD len = ::GetTempPathW(MAX_PATH, tempDir); + if (!len || len >= MAX_PATH) { + return; + } + + if (FAILED(::StringCbPrintfW(mOuterDirBuffer, sizeof(mOuterDirBuffer), + L"\\\\?\\%s%s", tempDir, + kFolderName160Chars)) || + !::CreateDirectoryW(mOuterDirBuffer, nullptr)) { + return; + } + mOuterDir = mOuterDirBuffer; + + if (FAILED(::StringCbPrintfW(mInnerDirBuffer, sizeof(mInnerDirBuffer), + L"\\\\?\\%s%s\\%s", tempDir, + kFolderName160Chars, kFolderName160Chars)) || + !::CreateDirectoryW(mInnerDirBuffer, nullptr)) { + return; + } + mInnerDir = mInnerDirBuffer; + + if (FAILED(::StringCbPrintfW(mTargetFileBuffer, sizeof(mTargetFileBuffer), + L"\\\\?\\%s%s\\%s\\%s", tempDir, + kFolderName160Chars, kFolderName160Chars, + aNewLeafNameAfterCopy)) || + !::CopyFileW(thisExe.get(), mTargetFileBuffer, + /*bFailIfExists*/ TRUE)) { + return; + } + mTargetFile = mTargetFileBuffer; + } + + ~LongNameModule() { + if (mTargetFile) { + ::DeleteFileW(mTargetFile); + } + if (mInnerDir) { + ::RemoveDirectoryW(mInnerDir); + } + if (mOuterDir) { + ::RemoveDirectoryW(mOuterDir); + } + } + + operator const wchar_t*() const { return mTargetFile; } +}; + +// Make sure module info retrieved from nt::PEHeaders is the same as one +// retrieved from GetModuleInformation API. +bool CompareModuleInfo(HMODULE aModuleForApi, HMODULE aModuleForPEHeader) { + MODULEINFO moduleInfo; + if (!::GetModuleInformation(::GetCurrentProcess(), aModuleForApi, &moduleInfo, + sizeof(moduleInfo))) { + printf("TEST-FAILED | NativeNt | GetModuleInformation failed - %08lx\n", + ::GetLastError()); + return false; + } + + PEHeaders headers(aModuleForPEHeader); + if (!headers) { + printf("TEST-FAILED | NativeNt | Failed to instantiate PEHeaders\n"); + return false; + } + + Maybe<Range<const uint8_t>> bounds = headers.GetBounds(); + if (!bounds) { + printf("TEST-FAILED | NativeNt | PEHeaders::GetBounds failed\n"); + return false; + } + + if (bounds->length() != moduleInfo.SizeOfImage) { + printf("TEST-FAILED | NativeNt | SizeOfImage does not match\n"); + return false; + } + + // GetModuleInformation sets EntryPoint to 0 for executables + // except the running self. + static const HMODULE sSelf = ::GetModuleHandleW(nullptr); + if (aModuleForApi != sSelf && + !(headers.GetFileCharacteristics() & IMAGE_FILE_DLL)) { + if (moduleInfo.EntryPoint) { + printf( + "TEST-FAIL | NativeNt | " + "GetModuleInformation returned a non-zero entrypoint " + "for an executable\n"); + return false; + } + + // Cannot verify PEHeaders::GetEntryPoint. + return true; + } + + // For a module whose entrypoint is 0 (e.g. ntdll.dll or win32u.dll), + // MODULEINFO::EntryPoint is set to 0, while PEHeaders::GetEntryPoint + // returns the imagebase (RVA=0). + intptr_t rvaEntryPoint = + moduleInfo.EntryPoint + ? reinterpret_cast<uintptr_t>(moduleInfo.EntryPoint) - + reinterpret_cast<uintptr_t>(moduleInfo.lpBaseOfDll) + : 0; + if (rvaEntryPoint < 0) { + printf("TEST-FAILED | NativeNt | MODULEINFO is invalid\n"); + return false; + } + + if (headers.RVAToPtr<FARPROC>(rvaEntryPoint) != headers.GetEntryPoint()) { + printf("TEST-FAILED | NativeNt | Entrypoint does not match\n"); + return false; + } + + return true; +} + +bool TestModuleInfo() { + UNICODE_STRING newLeafName; + ::RtlInitUnicodeString(&newLeafName, + L"\u672D\u5E4C\u5473\u564C.\u30E9\u30FC\u30E1\u30F3"); + + LongNameModule longNameModule(newLeafName.Buffer); + if (!longNameModule) { + printf( + "TEST-FAILED | NativeNt | " + "Failed to copy the executable to a long directory path\n"); + return 1; + } + + { + nsModuleHandle module(::LoadLibraryW(longNameModule)); + + bool detectedTarget = false; + bool passedAllModules = true; + auto moduleCallback = [&](const wchar_t* aModulePath, HMODULE aModule) { + UNICODE_STRING modulePath, moduleName; + ::RtlInitUnicodeString(&modulePath, aModulePath); + GetLeafName(&moduleName, &modulePath); + if (::RtlEqualUnicodeString(&moduleName, &newLeafName, + /*aCaseInsensitive*/ TRUE)) { + detectedTarget = true; + } + + if (!CompareModuleInfo(aModule, aModule)) { + passedAllModules = false; + } + }; + + if (!mozilla::EnumerateProcessModules(moduleCallback)) { + printf("TEST-FAILED | NativeNt | EnumerateProcessModules failed\n"); + return false; + } + + if (!detectedTarget) { + printf( + "TEST-FAILED | NativeNt | " + "EnumerateProcessModules missed the target file\n"); + return false; + } + + if (!passedAllModules) { + return false; + } + } + + return true; +} + +// Make sure PEHeaders works for a module loaded with LOAD_LIBRARY_AS_DATAFILE +// as well as a module loaded normally. +bool TestModuleLoadedAsData() { + const wchar_t kNewLeafName[] = L"\u03BC\u0061\u9EBA.txt"; + + LongNameModule longNameModule(kNewLeafName); + if (!longNameModule) { + printf( + "TEST-FAILED | NativeNt | " + "Failed to copy the executable to a long directory path\n"); + return 1; + } + + const wchar_t* kManualLoadModules[] = { + L"mshtml.dll", + L"shell32.dll", + longNameModule, + }; + + for (const auto moduleName : kManualLoadModules) { + // Must load a module as data first, + nsModuleHandle moduleAsData(::LoadLibraryExW( + moduleName, nullptr, + LOAD_LIBRARY_AS_DATAFILE | LOAD_LIBRARY_AS_IMAGE_RESOURCE)); + + // then load a module normally to map it on a different address. + nsModuleHandle module(::LoadLibraryW(moduleName)); + + if (!CompareModuleInfo(module.get(), moduleAsData.get())) { + return false; + } + + PEHeaders peAsData(moduleAsData.get()); + PEHeaders pe(module.get()); + if (!peAsData || !pe) { + printf("TEST-FAIL | NativeNt | Failed to load the module\n"); + return false; + } + + if (peAsData.RVAToPtr<HMODULE>(0) == pe.RVAToPtr<HMODULE>(0)) { + printf( + "TEST-FAIL | NativeNt | " + "The module should have been mapped onto two different places\n"); + return false; + } + + const auto* pdb1 = peAsData.GetPdbInfo(); + const auto* pdb2 = pe.GetPdbInfo(); + if (pdb1 && pdb2) { + if (pdb1->pdbSignature != pdb2->pdbSignature || + pdb1->pdbAge != pdb2->pdbAge || + strcmp(pdb1->pdbFileName, pdb2->pdbFileName)) { + printf( + "TEST-FAIL | NativeNt | " + "PDB info from the same module did not match.\n"); + return false; + } + } else if (pdb1 || pdb2) { + printf( + "TEST-FAIL | NativeNt | Failed to get PDB info from the module.\n"); + return false; + } + + uint64_t version1, version2; + bool result1 = peAsData.GetVersionInfo(version1); + bool result2 = pe.GetVersionInfo(version2); + if (result1 && result2) { + if (version1 != version2) { + printf("TEST-FAIL | NativeNt | Version mismatch\n"); + return false; + } + } else if (result1 || result2) { + printf( + "TEST-FAIL | NativeNt | Failed to get PDB info from the module.\n"); + return false; + } + } + + return true; +} + +LauncherResult<HMODULE> GetModuleHandleFromLeafName(const wchar_t* aName) { + UNICODE_STRING name; + ::RtlInitUnicodeString(&name, aName); + return nt::GetModuleHandleFromLeafName(name); +} + +// Need a non-inline function to bypass compiler optimization that the thread +// local storage pointer is cached in a register before accessing a thread-local +// variable. +MOZ_NEVER_INLINE PVOID SwapThreadLocalStoragePointer(PVOID aNewValue) { + auto oldValue = RtlGetThreadLocalStoragePointer(); + RtlSetThreadLocalStoragePointerForTestingOnly(aNewValue); + return oldValue; +} + +int wmain(int argc, wchar_t* argv[]) { + UNICODE_STRING normal; + ::RtlInitUnicodeString(&normal, kNormal); + + UNICODE_STRING hex12; + ::RtlInitUnicodeString(&hex12, kHex12); + + UNICODE_STRING hex16; + ::RtlInitUnicodeString(&hex16, kHex16); + + UNICODE_STRING hex24; + ::RtlInitUnicodeString(&hex24, kHex24); + + UNICODE_STRING hex8; + ::RtlInitUnicodeString(&hex8, kHex8); + + UNICODE_STRING nonHex12; + ::RtlInitUnicodeString(&nonHex12, kNonHex12); + + UNICODE_STRING hex13; + ::RtlInitUnicodeString(&hex13, kHex13); + + UNICODE_STRING hex11; + ::RtlInitUnicodeString(&hex11, kHex11); + + UNICODE_STRING hex15; + ::RtlInitUnicodeString(&hex15, kHex15); + + UNICODE_STRING hex17; + ::RtlInitUnicodeString(&hex17, kHex17); + + UNICODE_STRING prefixedHex16; + ::RtlInitUnicodeString(&prefixedHex16, kPrefixedHex16); + + EXPECT_FAIL(Contains12DigitHexString, normal); + EXPECT_SUCCESS(Contains12DigitHexString, hex12); + EXPECT_FAIL(Contains12DigitHexString, hex13); + EXPECT_FAIL(Contains12DigitHexString, hex11); + EXPECT_FAIL(Contains12DigitHexString, hex16); + EXPECT_FAIL(Contains12DigitHexString, nonHex12); + + EXPECT_FAIL(IsFileNameAtLeast16HexDigits, normal); + EXPECT_FAIL(IsFileNameAtLeast16HexDigits, hex12); + EXPECT_SUCCESS(IsFileNameAtLeast16HexDigits, hex24); + EXPECT_SUCCESS(IsFileNameAtLeast16HexDigits, hex16); + EXPECT_SUCCESS(IsFileNameAtLeast16HexDigits, hex17); + EXPECT_FAIL(IsFileNameAtLeast16HexDigits, hex8); + EXPECT_FAIL(IsFileNameAtLeast16HexDigits, hex15); + EXPECT_FAIL(IsFileNameAtLeast16HexDigits, prefixedHex16); + + if (RtlGetProcessHeap() != ::GetProcessHeap()) { + printf("TEST-FAILED | NativeNt | RtlGetProcessHeap() is broken\n"); + return 1; + } + +#ifdef HAVE_SEH_EXCEPTIONS + PVOID origTlsHead = nullptr; + bool isExceptionThrown = false; + // Touch sTlsData.get() several times to prevent the call to sTlsData.set() + // from being optimized out in PGO build. + printf("sTlsData#1 = %08x\n", getTlsData()); + MOZ_SEH_TRY { + // Need to call SwapThreadLocalStoragePointer inside __try to make sure + // accessing sTlsData is caught by SEH. This is due to clang's design. + // https://bugs.llvm.org/show_bug.cgi?id=44174. + origTlsHead = SwapThreadLocalStoragePointer(nullptr); + setTlsData(~kTlsDataValue); + } + MOZ_SEH_EXCEPT(GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION + ? EXCEPTION_EXECUTE_HANDLER + : EXCEPTION_CONTINUE_SEARCH) { + isExceptionThrown = true; + } + SwapThreadLocalStoragePointer(origTlsHead); + printf("sTlsData#2 = %08x\n", getTlsData()); + setTlsData(kTlsDataValue); + printf("sTlsData#3 = %08x\n", getTlsData()); + if (!isExceptionThrown || getTlsData() != kTlsDataValue) { + printf( + "TEST-FAILED | NativeNt | RtlGetThreadLocalStoragePointer() is " + "broken\n"); + return 1; + } +#endif + + if (RtlGetCurrentThreadId() != ::GetCurrentThreadId()) { + printf("TEST-FAILED | NativeNt | RtlGetCurrentThreadId() is broken\n"); + return 1; + } + + const wchar_t kKernel32[] = L"kernel32.dll"; + DWORD verInfoSize = ::GetFileVersionInfoSizeW(kKernel32, nullptr); + if (!verInfoSize) { + printf( + "TEST-FAILED | NativeNt | Call to GetFileVersionInfoSizeW failed with " + "code %lu\n", + ::GetLastError()); + return 1; + } + + auto verInfoBuf = MakeUnique<char[]>(verInfoSize); + + if (!::GetFileVersionInfoW(kKernel32, 0, verInfoSize, verInfoBuf.get())) { + printf( + "TEST-FAILED | NativeNt | Call to GetFileVersionInfoW failed with code " + "%lu\n", + ::GetLastError()); + return 1; + } + + UINT len; + VS_FIXEDFILEINFO* fixedFileInfo = nullptr; + if (!::VerQueryValueW(verInfoBuf.get(), L"\\", (LPVOID*)&fixedFileInfo, + &len)) { + printf( + "TEST-FAILED | NativeNt | Call to VerQueryValueW failed with code " + "%lu\n", + ::GetLastError()); + return 1; + } + + const uint64_t expectedVersion = + (static_cast<uint64_t>(fixedFileInfo->dwFileVersionMS) << 32) | + static_cast<uint64_t>(fixedFileInfo->dwFileVersionLS); + + PEHeaders k32headers(::GetModuleHandleW(kKernel32)); + if (!k32headers) { + printf( + "TEST-FAILED | NativeNt | Failed parsing kernel32.dll's PE headers\n"); + return 1; + } + + uint64_t version; + if (!k32headers.GetVersionInfo(version)) { + printf( + "TEST-FAILED | NativeNt | Unable to obtain version information from " + "kernel32.dll\n"); + return 1; + } + + if (version != expectedVersion) { + printf( + "TEST-FAILED | NativeNt | kernel32.dll's detected version " + "(0x%016llX) does not match expected version (0x%016llX)\n", + version, expectedVersion); + return 1; + } + + Maybe<Span<IMAGE_THUNK_DATA>> iatThunks = + k32headers.GetIATThunksForModule("kernel32.dll"); + if (iatThunks) { + printf( + "TEST-FAILED | NativeNt | Detected the IAT thunk for kernel32 " + "in kernel32.dll\n"); + return 1; + } + + const mozilla::nt::CodeViewRecord70* debugInfo = k32headers.GetPdbInfo(); + if (!debugInfo) { + printf( + "TEST-FAILED | NativeNt | Unable to obtain debug information from " + "kernel32.dll\n"); + return 1; + } + +#ifndef WIN32 // failure on windows10x32 + if (stricmp(debugInfo->pdbFileName, "kernel32.pdb")) { + printf( + "TEST-FAILED | NativeNt | Unexpected PDB filename " + "in kernel32.dll: %s\n", + debugInfo->pdbFileName); + return 1; + } +#endif + + PEHeaders ntdllheaders(::GetModuleHandleW(L"ntdll.dll")); + + auto ntdllBoundaries = ntdllheaders.GetBounds(); + if (!ntdllBoundaries) { + printf( + "TEST-FAILED | NativeNt | " + "Unable to obtain the boundaries of ntdll.dll\n"); + return 1; + } + + iatThunks = + k32headers.GetIATThunksForModule("ntdll.dll", ntdllBoundaries.ptr()); + if (!iatThunks) { + printf( + "TEST-FAILED | NativeNt | Unable to find the IAT thunk for " + "ntdll.dll in kernel32.dll\n"); + return 1; + } + + // To test the Ex version of API, we purposely get a real handle + // instead of a pseudo handle. + nsAutoHandle process( + ::OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, GetCurrentProcessId())); + if (!process) { + printf("TEST-FAILED | NativeNt | OpenProcess() failed - %08lx\n", + ::GetLastError()); + return 1; + } + + // Test Null page, Heap, Mapped image, and Invalid handle + if (!TestVirtualQuery(process, nullptr) || !TestVirtualQuery(process, argv) || + !TestVirtualQuery(process, kNormal) || + !TestVirtualQuery(nullptr, kNormal)) { + return 1; + } + + auto moduleResult = GetModuleHandleFromLeafName(kKernel32); + if (moduleResult.isErr() || + moduleResult.inspect() != k32headers.template RVAToPtr<HMODULE>(0)) { + printf( + "TEST-FAILED | NativeNt | " + "GetModuleHandleFromLeafName returns a wrong value.\n"); + return 1; + } + + moduleResult = GetModuleHandleFromLeafName(L"invalid"); + if (moduleResult.isOk()) { + printf( + "TEST-FAILED | NativeNt | " + "GetModuleHandleFromLeafName unexpectedly returns a value.\n"); + return 1; + } + + if (!TestModuleInfo()) { + return 1; + } + + if (!TestModuleLoadedAsData()) { + return 1; + } + + printf("TEST-PASS | NativeNt | All tests ran successfully\n"); + return 0; +} diff --git a/mozglue/tests/TestPEExportSection.cpp b/mozglue/tests/TestPEExportSection.cpp new file mode 100644 index 0000000000..cf160368d3 --- /dev/null +++ b/mozglue/tests/TestPEExportSection.cpp @@ -0,0 +1,706 @@ +/* -*- 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 https://mozilla.org/MPL/2.0/. */ + +// This test makes sure mozilla::nt::PEExportSection can parse the export +// section of a local process, and a remote process even though it's +// modified by an external code. + +#include "mozilla/CmdLineAndEnvUtils.h" +#include "mozilla/NativeNt.h" +#include "nsWindowsDllInterceptor.h" + +#include <stdio.h> +#include <windows.h> + +#define EXPORT_FUNCTION_EQ(name, func) \ + (GetProcAddress(imageBase, name) == reinterpret_cast<void*>(func)) + +#define VERIFY_EXPORT_FUNCTION(tables, name, expected, errorMessage) \ + do { \ + if (tables.GetProcAddress(name) != reinterpret_cast<void*>(expected)) { \ + printf("TEST-FAILED | TestPEExportSection | %s", errorMessage); \ + return kTestFail; \ + } \ + } while (0) + +using namespace mozilla::nt; +using mozilla::interceptor::MMPolicyInProcess; +using mozilla::interceptor::MMPolicyOutOfProcess; +using LocalPEExportSection = PEExportSection<MMPolicyInProcess>; +using RemotePEExportSection = PEExportSection<MMPolicyOutOfProcess>; + +constexpr DWORD kEventTimeoutinMs = 5000; +const wchar_t kProcessControlEventName[] = + L"TestPEExportSection.Process.Control.Event"; + +enum TestResult : int { + kTestSuccess = 0, + kTestFail, + kTestSkip, +}; + +// These strings start with the same keyword to make sure we don't do substring +// match. Moreover, kSecretFunctionInvalid is purposely longer than the +// combination of the other two strings and located in between the other two +// strings to effectively test binary search. +const char kSecretFunction[] = "Secret"; +const char kSecretFunctionInvalid[] = "Secret invalid long name"; +const char kSecretFunctionWithSuffix[] = "Secret2"; + +const wchar_t* kNoModification = L"--NoModification"; +const wchar_t* kNoExport = L"--NoExport"; +const wchar_t* kModifyTableEntry = L"--ModifyTableEntry"; +const wchar_t* kModifyTable = L"--ModifyTable"; +const wchar_t* kModifyDirectoryEntry = L"--ModifyDirectoryEntry"; +const wchar_t* kExportByOrdinal = L"--ExportByOrdinal"; + +// Use the global variable to pass the child process's error status to the +// parent process. We don't use a process's exit code to keep the test simple. +int gChildProcessStatus = 0; + +// These functions are exported by linker or export section tampering at +// runtime. Each of function bodies needs to be different to avoid ICF. +extern "C" __declspec(dllexport) int Export1() { return 0; } +extern "C" __declspec(dllexport) int Export2() { return 1; } +int SecretFunction1() { return 100; } +int SecretFunction2() { return 101; } + +// This class allocates a writable region downstream of the mapped image +// and prepares it as a valid export section. +class ExportDirectoryPatcher final { + static constexpr int kRegionAllocationTryLimit = 100; + static constexpr int kNumOfTableEntries = 2; + // VirtualAlloc sometimes fails if a desired base address is too small. + // Define a minimum desired base to reduce the number of allocation tries. + static constexpr uintptr_t kMinimumAllocationPoint = 0x8000000; + + struct ExportDirectory { + IMAGE_EXPORT_DIRECTORY mDirectoryHeader; + DWORD mExportAddressTable[kNumOfTableEntries]; + DWORD mExportNameTable[kNumOfTableEntries]; + WORD mExportOrdinalTable[kNumOfTableEntries]; + char mNameBuffer1[sizeof(kSecretFunction)]; + char mNameBuffer2[sizeof(kSecretFunctionWithSuffix)]; + + template <typename T> + static DWORD PtrToRVA(T aPtr, uintptr_t aBase) { + return reinterpret_cast<uintptr_t>(aPtr) - aBase; + } + + explicit ExportDirectory(uintptr_t aImageBase) : mDirectoryHeader{} { + mDirectoryHeader.Base = 1; + mExportAddressTable[0] = PtrToRVA(SecretFunction1, aImageBase); + mExportAddressTable[1] = PtrToRVA(SecretFunction2, aImageBase); + mExportNameTable[0] = PtrToRVA(mNameBuffer1, aImageBase); + mExportNameTable[1] = PtrToRVA(mNameBuffer2, aImageBase); + mExportOrdinalTable[0] = 0; + mExportOrdinalTable[1] = 1; + strcpy(mNameBuffer1, kSecretFunction); + strcpy(mNameBuffer2, kSecretFunctionWithSuffix); + } + }; + + uintptr_t mImageBase; + ExportDirectory* mNewExportDirectory; + + DWORD PtrToRVA(const void* aPtr) const { + return reinterpret_cast<uintptr_t>(aPtr) - mImageBase; + } + + public: + explicit ExportDirectoryPatcher(HMODULE aModule) + : mImageBase(PEHeaders::HModuleToBaseAddr<uintptr_t>(aModule)), + mNewExportDirectory(nullptr) { + SYSTEM_INFO si = {}; + ::GetSystemInfo(&si); + + int numPagesRequired = ((sizeof(ExportDirectory) - 1) / si.dwPageSize) + 1; + + uintptr_t desiredBase = mImageBase + si.dwAllocationGranularity; + desiredBase = std::max(desiredBase, kMinimumAllocationPoint); + + for (int i = 0; i < kRegionAllocationTryLimit; ++i) { + void* allocated = + ::VirtualAlloc(reinterpret_cast<void*>(desiredBase), + numPagesRequired * si.dwPageSize, + MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); + if (allocated) { + // Use the end of a allocated page as ExportDirectory in order to test + // the boundary between a commit page and a non-commited page. + allocated = reinterpret_cast<uint8_t*>(allocated) + + (numPagesRequired * si.dwPageSize) - + sizeof(ExportDirectory); + mNewExportDirectory = new (allocated) ExportDirectory(mImageBase); + return; + } + + desiredBase += si.dwAllocationGranularity; + } + + gChildProcessStatus = kTestSkip; + printf( + "TEST-SKIP | TestPEExportSection | " + "Giving up finding an allocatable space following the mapped image.\n"); + } + + ~ExportDirectoryPatcher() { + // Intentionally leave mNewExportDirectory leaked to keep a patched data + // available until the process is terminated. + } + + explicit operator bool() const { return !!mNewExportDirectory; } + + void PopulateDirectory(IMAGE_EXPORT_DIRECTORY& aOutput) const { + aOutput.NumberOfFunctions = aOutput.NumberOfNames = kNumOfTableEntries; + aOutput.AddressOfFunctions = + PtrToRVA(mNewExportDirectory->mExportAddressTable); + aOutput.AddressOfNames = PtrToRVA(mNewExportDirectory->mExportNameTable); + aOutput.AddressOfNameOrdinals = + PtrToRVA(mNewExportDirectory->mExportOrdinalTable); + } + + void PopulateDirectoryEntry(IMAGE_DATA_DIRECTORY& aOutput) const { + PopulateDirectory(mNewExportDirectory->mDirectoryHeader); + aOutput.VirtualAddress = PtrToRVA(&mNewExportDirectory->mDirectoryHeader); + aOutput.Size = sizeof(ExportDirectory); + } +}; + +// This exports SecretFunction1 as "Export1" by replacing an entry of the +// export address table. +void ModifyExportAddressTableEntry() { + MMPolicyInProcess policy; + HMODULE imageBase = ::GetModuleHandleW(nullptr); + auto ourExe = LocalPEExportSection::Get(imageBase, policy); + + auto addressTableEntry = + const_cast<DWORD*>(ourExe.FindExportAddressTableEntry("Export1")); + if (!addressTableEntry) { + gChildProcessStatus = kTestFail; + return; + } + + mozilla::AutoVirtualProtect protection( + addressTableEntry, sizeof(*addressTableEntry), PAGE_READWRITE); + if (!protection) { + gChildProcessStatus = kTestFail; + return; + } + + *addressTableEntry = reinterpret_cast<uintptr_t>(SecretFunction1) - + PEHeaders::HModuleToBaseAddr<uintptr_t>(imageBase); + + if (!EXPORT_FUNCTION_EQ("Export1", SecretFunction1) || + !EXPORT_FUNCTION_EQ("Export2", Export2)) { + gChildProcessStatus = kTestFail; + } +} + +// This switches the entire address table into one exporting SecretFunction1 +// and SecretFunction2. +void ModifyExportAddressTable() { + MMPolicyInProcess policy; + HMODULE imageBase = ::GetModuleHandleW(nullptr); + auto ourExe = LocalPEExportSection::Get(imageBase, policy); + + auto exportDirectory = ourExe.GetExportDirectory(); + if (!exportDirectory) { + gChildProcessStatus = kTestFail; + return; + } + + mozilla::AutoVirtualProtect protection( + exportDirectory, sizeof(*exportDirectory), PAGE_READWRITE); + if (!protection) { + gChildProcessStatus = kTestFail; + return; + } + + ExportDirectoryPatcher patcher(imageBase); + if (!patcher) { + return; + } + + patcher.PopulateDirectory(*exportDirectory); + + if (GetProcAddress(imageBase, "Export1") || + GetProcAddress(imageBase, "Export2") || + !EXPORT_FUNCTION_EQ(kSecretFunction, SecretFunction1) || + !EXPORT_FUNCTION_EQ(kSecretFunctionWithSuffix, SecretFunction2)) { + gChildProcessStatus = kTestFail; + } +} + +// This hides all export functions by setting the table size to 0. +void HideExportSection() { + HMODULE imageBase = ::GetModuleHandleW(nullptr); + PEHeaders ourExe(imageBase); + + auto sectionTable = + ourExe.GetImageDirectoryEntryPtr(IMAGE_DIRECTORY_ENTRY_EXPORT); + + mozilla::AutoVirtualProtect protection(sectionTable, sizeof(*sectionTable), + PAGE_READWRITE); + if (!protection) { + gChildProcessStatus = kTestFail; + return; + } + + sectionTable->VirtualAddress = sectionTable->Size = 0; + + if (GetProcAddress(imageBase, "Export1") || + GetProcAddress(imageBase, "Export2")) { + gChildProcessStatus = kTestFail; + } +} + +// This makes the export directory entry point to a new export section +// which exports SecretFunction1 and SecretFunction2. +void ModifyExportDirectoryEntry() { + HMODULE imageBase = ::GetModuleHandleW(nullptr); + PEHeaders ourExe(imageBase); + + auto sectionTable = + ourExe.GetImageDirectoryEntryPtr(IMAGE_DIRECTORY_ENTRY_EXPORT); + + mozilla::AutoVirtualProtect protection(sectionTable, sizeof(*sectionTable), + PAGE_READWRITE); + if (!protection) { + gChildProcessStatus = kTestFail; + return; + } + + ExportDirectoryPatcher patcher(imageBase); + if (!patcher) { + return; + } + + patcher.PopulateDirectoryEntry(*sectionTable); + + if (GetProcAddress(imageBase, "Export1") || + GetProcAddress(imageBase, "Export2") || + !EXPORT_FUNCTION_EQ(kSecretFunction, SecretFunction1) || + !EXPORT_FUNCTION_EQ(kSecretFunctionWithSuffix, SecretFunction2)) { + gChildProcessStatus = kTestFail; + } +} + +// This exports functions only by Ordinal by hiding the export name table. +void ExportByOrdinal() { + ModifyExportDirectoryEntry(); + if (gChildProcessStatus != kTestSuccess) { + return; + } + + MMPolicyInProcess policy; + HMODULE imageBase = ::GetModuleHandleW(nullptr); + auto ourExe = LocalPEExportSection::Get(imageBase, policy); + + auto exportDirectory = ourExe.GetExportDirectory(); + if (!exportDirectory) { + gChildProcessStatus = kTestFail; + return; + } + + exportDirectory->NumberOfNames = 0; + + if (GetProcAddress(imageBase, "Export1") || + GetProcAddress(imageBase, "Export2") || + GetProcAddress(imageBase, kSecretFunction) || + GetProcAddress(imageBase, kSecretFunctionWithSuffix) || + !EXPORT_FUNCTION_EQ(MAKEINTRESOURCE(1), SecretFunction1) || + !EXPORT_FUNCTION_EQ(MAKEINTRESOURCE(2), SecretFunction2)) { + gChildProcessStatus = kTestFail; + } +} + +class ChildProcess final { + nsAutoHandle mChildProcess; + nsAutoHandle mChildMainThread; + + public: + static int Main(const nsAutoHandle& aEvent, const wchar_t* aOption) { + if (wcscmp(aOption, kNoModification) == 0) { + ; + } else if (wcscmp(aOption, kNoExport) == 0) { + HideExportSection(); + } else if (wcscmp(aOption, kModifyTableEntry) == 0) { + ModifyExportAddressTableEntry(); + } else if (wcscmp(aOption, kModifyTable) == 0) { + ModifyExportAddressTable(); + } else if (wcscmp(aOption, kModifyDirectoryEntry) == 0) { + ModifyExportDirectoryEntry(); + } else if (wcscmp(aOption, kExportByOrdinal) == 0) { + ExportByOrdinal(); + } + + // Letting the parent process know the child process is ready. + ::SetEvent(aEvent); + + // The child process does not exit itself. It's force terminated by + // the parent process when all tests are done. + for (;;) { + ::Sleep(100); + } + } + + ChildProcess(const wchar_t* aExecutable, const wchar_t* aOption, + const nsAutoHandle& aEvent, const nsAutoHandle& aJob) { + const wchar_t* childArgv[] = {aExecutable, aOption}; + auto cmdLine( + mozilla::MakeCommandLine(mozilla::ArrayLength(childArgv), childArgv)); + + STARTUPINFOW si = {sizeof(si)}; + PROCESS_INFORMATION pi; + BOOL ok = ::CreateProcessW(aExecutable, cmdLine.get(), nullptr, nullptr, + FALSE, 0, nullptr, nullptr, &si, &pi); + if (!ok) { + printf( + "TEST-FAILED | TestPEExportSection | " + "CreateProcessW falied - %08lx.\n", + GetLastError()); + return; + } + + if (aJob && !::AssignProcessToJobObject(aJob, pi.hProcess)) { + printf( + "TEST-FAILED | TestPEExportSection | " + "AssignProcessToJobObject falied - %08lx.\n", + GetLastError()); + ::TerminateProcess(pi.hProcess, 1); + return; + } + + // Wait until requested modification is done in the child process. + if (::WaitForSingleObject(aEvent, kEventTimeoutinMs) != WAIT_OBJECT_0) { + printf( + "TEST-FAILED | TestPEExportSection | " + "Child process was not ready in time.\n"); + return; + } + + mChildProcess.own(pi.hProcess); + mChildMainThread.own(pi.hThread); + } + + ~ChildProcess() { ::TerminateProcess(mChildProcess, 0); } + + operator HANDLE() const { return mChildProcess; } + + TestResult GetStatus() const { + TestResult status = kTestSuccess; + if (!::ReadProcessMemory(mChildProcess, &gChildProcessStatus, &status, + sizeof(status), nullptr)) { + status = kTestFail; + printf( + "TEST-FAILED | TestPEExportSection | " + "ReadProcessMemory failed - %08lx\n", + GetLastError()); + } + return status; + } +}; + +template <typename MMPolicy> +TestResult BasicTest(const MMPolicy& aMMPolicy) { + const bool isAppHelpLoaded = ::GetModuleHandleW(L"apphelp.dll"); + + // Use ntdll.dll because it does not have any forwarder RVA. + HMODULE ntdllImageBase = ::GetModuleHandleW(L"ntdll.dll"); + auto ntdllExports = PEExportSection<MMPolicy>::Get(ntdllImageBase, aMMPolicy); + + auto exportDir = ntdllExports.GetExportDirectory(); + auto tableOfNames = + ntdllExports.template RVAToPtr<const PDWORD>(exportDir->AddressOfNames); + for (DWORD i = 0; i < exportDir->NumberOfNames; ++i) { + const auto name = + ntdllExports.template RVAToPtr<const char*>(tableOfNames[i]); + + if (isAppHelpLoaded && strcmp(name, "NtdllDefWindowProc_W") == 0) { + // In this case, GetProcAddress will return + // apphelp!DWM8AND16BitHook_DefWindowProcW. + continue; + } + + auto funcEntry = ntdllExports.FindExportAddressTableEntry(name); + if (ntdllExports.template RVAToPtr<const void*>(*funcEntry) != + ::GetProcAddress(ntdllImageBase, name)) { + printf( + "TEST-FAILED | TestPEExportSection | " + "FindExportAddressTableEntry did not resolve ntdll!%s.\n", + name); + return kTestFail; + } + } + + for (DWORD i = 0; i < 0x10000; i += 0x10) { + if (ntdllExports.GetProcAddress(MAKEINTRESOURCE(i)) != + ::GetProcAddress(ntdllImageBase, MAKEINTRESOURCE(i))) { + printf( + "TEST-FAILED | TestPEExportSection | " + "GetProcAddress did not resolve ntdll!Ordinal#%lu.\n", + i); + return kTestFail; + } + } + + // Test a known forwarder RVA. + auto k32Exports = PEExportSection<MMPolicy>::Get( + ::GetModuleHandleW(L"kernel32.dll"), aMMPolicy); + if (k32Exports.FindExportAddressTableEntry("HeapAlloc")) { + printf( + "TEST-FAILED | TestPEExportSection | " + "kernel32!HeapAlloc should be forwarded to ntdll!RtlAllocateHeap.\n"); + return kTestFail; + } + + // Test invalid names. + if (k32Exports.FindExportAddressTableEntry("Invalid name") || + k32Exports.FindExportAddressTableEntry("")) { + printf( + "TEST-FAILED | TestPEExportSection | " + "FindExportAddressTableEntry should return " + "nullptr for a non-existent name.\n"); + return kTestFail; + } + + return kTestSuccess; +} + +TestResult RunChildProcessTest( + const wchar_t* aExecutable, const wchar_t* aOption, + const nsAutoHandle& aEvent, const nsAutoHandle& aJob, + TestResult (*aTestCallback)(const RemotePEExportSection&)) { + ChildProcess childProcess(aExecutable, aOption, aEvent, aJob); + if (!childProcess) { + return kTestFail; + } + + auto result = childProcess.GetStatus(); + if (result != kTestSuccess) { + return result; + } + + MMPolicyOutOfProcess policy(childProcess); + + // One time is enough to run BasicTest in the child process. + static TestResult oneTimeResult = BasicTest<MMPolicyOutOfProcess>(policy); + if (oneTimeResult != kTestSuccess) { + return oneTimeResult; + } + + auto exportTableChild = + RemotePEExportSection::Get(::GetModuleHandleW(nullptr), policy); + return aTestCallback(exportTableChild); +} + +mozilla::LauncherResult<nsReturnRef<HANDLE>> CreateJobToLimitProcessLifetime() { + uint64_t version; + PEHeaders ntdllHeaders(::GetModuleHandleW(L"ntdll.dll")); + if (!ntdllHeaders.GetVersionInfo(version)) { + printf( + "TEST-FAILED | TestPEExportSection | " + "Unable to obtain version information from ntdll.dll\n"); + return LAUNCHER_ERROR_FROM_LAST(); + } + + constexpr uint64_t kWin8 = 0x60002ull << 32; + nsAutoHandle job; + + if (version < kWin8) { + // Since a process can be associated only with a single job in Win7 or + // older and this test program is already assigned with a job by + // infrastructure, we cannot use a job. + return job.out(); + } + + job.own(::CreateJobObject(nullptr, nullptr)); + if (!job) { + printf( + "TEST-FAILED | TestPEExportSection | " + "CreateJobObject falied - %08lx.\n", + GetLastError()); + return LAUNCHER_ERROR_FROM_LAST(); + } + + JOBOBJECT_EXTENDED_LIMIT_INFORMATION jobInfo = {}; + jobInfo.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE; + + if (!::SetInformationJobObject(job, JobObjectExtendedLimitInformation, + &jobInfo, sizeof(jobInfo))) { + printf( + "TEST-FAILED | TestPEExportSection | " + "SetInformationJobObject falied - %08lx.\n", + GetLastError()); + return LAUNCHER_ERROR_FROM_LAST(); + } + + return job.out(); +} + +extern "C" int wmain(int argc, wchar_t* argv[]) { + nsAutoHandle controlEvent( + ::CreateEventW(nullptr, FALSE, FALSE, kProcessControlEventName)); + + if (argc == 2) { + return ChildProcess::Main(controlEvent, argv[1]); + } + + if (argc != 1) { + printf( + "TEST-FAILED | TestPEExportSection | " + "Invalid arguments.\n"); + return kTestFail; + } + + MMPolicyInProcess policy; + if (BasicTest<MMPolicyInProcess>(policy)) { + return kTestFail; + } + + auto exportTableSelf = + LocalPEExportSection::Get(::GetModuleHandleW(nullptr), policy); + if (!exportTableSelf) { + printf( + "TEST-FAILED | TestPEExportSection | " + "LocalPEExportSection::Get failed.\n"); + return kTestFail; + } + + VERIFY_EXPORT_FUNCTION(exportTableSelf, "Export1", Export1, + "Local | Export1 was not exported.\n"); + VERIFY_EXPORT_FUNCTION(exportTableSelf, "Export2", Export2, + "Local | Export2 was not exported.\n"); + VERIFY_EXPORT_FUNCTION( + exportTableSelf, "Invalid name", 0, + "Local | GetProcAddress should return nullptr for an invalid name.\n"); + + // We'll add the child process to a job so that, in the event of a failure in + // this parent process, the child process will be automatically terminated. + auto probablyJob = CreateJobToLimitProcessLifetime(); + if (probablyJob.isErr()) { + return kTestFail; + } + + nsAutoHandle job(probablyJob.unwrap()); + + auto result = RunChildProcessTest( + argv[0], kNoModification, controlEvent, job, + [](const RemotePEExportSection& aTables) { + VERIFY_EXPORT_FUNCTION(aTables, "Export1", Export1, + "NoModification | Export1 was not exported.\n"); + VERIFY_EXPORT_FUNCTION(aTables, "Export2", Export2, + "NoModification | Export2 was not exported.\n"); + return kTestSuccess; + }); + if (result == kTestFail) { + return result; + } + + result = RunChildProcessTest( + argv[0], kNoExport, controlEvent, job, + [](const RemotePEExportSection& aTables) { + VERIFY_EXPORT_FUNCTION(aTables, "Export1", 0, + "NoExport | Export1 was exported.\n"); + VERIFY_EXPORT_FUNCTION(aTables, "Export2", 0, + "NoExport | Export2 was exported.\n"); + return kTestSuccess; + }); + if (result == kTestFail) { + return result; + } + + result = RunChildProcessTest( + argv[0], kModifyTableEntry, controlEvent, job, + [](const RemotePEExportSection& aTables) { + VERIFY_EXPORT_FUNCTION( + aTables, "Export1", SecretFunction1, + "ModifyTableEntry | SecretFunction1 was not exported.\n"); + VERIFY_EXPORT_FUNCTION( + aTables, "Export2", Export2, + "ModifyTableEntry | Export2 was not exported.\n"); + return kTestSuccess; + }); + if (result == kTestFail) { + return result; + } + + result = RunChildProcessTest( + argv[0], kModifyTable, controlEvent, job, + [](const RemotePEExportSection& aTables) { + VERIFY_EXPORT_FUNCTION(aTables, "Export1", 0, + "ModifyTable | Export1 was exported.\n"); + VERIFY_EXPORT_FUNCTION(aTables, "Export2", 0, + "ModifyTable | Export2 was exported.\n"); + VERIFY_EXPORT_FUNCTION( + aTables, kSecretFunction, SecretFunction1, + "ModifyTable | SecretFunction1 was not exported.\n"); + VERIFY_EXPORT_FUNCTION( + aTables, kSecretFunctionWithSuffix, SecretFunction2, + "ModifyTable | SecretFunction2 was not exported.\n"); + VERIFY_EXPORT_FUNCTION( + aTables, kSecretFunctionInvalid, 0, + "ModifyTable | kSecretFunctionInvalid was exported.\n"); + return kTestSuccess; + }); + if (result == kTestFail) { + return result; + } + + result = RunChildProcessTest( + argv[0], kModifyDirectoryEntry, controlEvent, job, + [](const RemotePEExportSection& aTables) { + VERIFY_EXPORT_FUNCTION( + aTables, "Export1", 0, + "ModifyDirectoryEntry | Export1 was exported.\n"); + VERIFY_EXPORT_FUNCTION( + aTables, "Export2", 0, + "ModifyDirectoryEntry | Export2 was exported.\n"); + VERIFY_EXPORT_FUNCTION( + aTables, kSecretFunction, SecretFunction1, + "ModifyDirectoryEntry | SecretFunction1 was not exported.\n"); + VERIFY_EXPORT_FUNCTION( + aTables, kSecretFunctionWithSuffix, SecretFunction2, + "ModifyDirectoryEntry | SecretFunction2 was not exported.\n"); + VERIFY_EXPORT_FUNCTION( + aTables, kSecretFunctionInvalid, 0, + "ModifyDirectoryEntry | kSecretFunctionInvalid was exported.\n"); + return kTestSuccess; + }); + if (result == kTestFail) { + return result; + } + + result = RunChildProcessTest( + argv[0], kExportByOrdinal, controlEvent, job, + [](const RemotePEExportSection& aTables) { + VERIFY_EXPORT_FUNCTION(aTables, "Export1", 0, + "ExportByOrdinal | Export1 was exported.\n"); + VERIFY_EXPORT_FUNCTION(aTables, "Export2", 0, + "ExportByOrdinal | Export2 was exported.\n"); + VERIFY_EXPORT_FUNCTION( + aTables, kSecretFunction, 0, + "ModifyDirectoryEntry | kSecretFunction was exported by name.\n"); + VERIFY_EXPORT_FUNCTION( + aTables, kSecretFunctionWithSuffix, 0, + "ModifyDirectoryEntry | " + "kSecretFunctionWithSuffix was exported by name.\n"); + VERIFY_EXPORT_FUNCTION( + aTables, MAKEINTRESOURCE(1), SecretFunction1, + "ModifyDirectoryEntry | " + "kSecretFunction was not exported by ordinal.\n"); + VERIFY_EXPORT_FUNCTION( + aTables, MAKEINTRESOURCE(2), SecretFunction2, + "ModifyDirectoryEntry | " + "kSecretFunctionWithSuffix was not exported by ordinal.\n"); + return kTestSuccess; + }); + if (result == kTestFail) { + return result; + } + + return kTestSuccess; +} diff --git a/mozglue/tests/TestPrintf.cpp b/mozglue/tests/TestPrintf.cpp new file mode 100644 index 0000000000..5f6f9e2005 --- /dev/null +++ b/mozglue/tests/TestPrintf.cpp @@ -0,0 +1,264 @@ +/* vim: set shiftwidth=2 tabstop=8 autoindent cindent expandtab: */ +/* 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 "mozilla/Printf.h" + +#include <cfloat> +#include <cmath> +#include <inttypes.h> +#include <stdarg.h> +#include <stddef.h> + +#if defined(__clang__) +# pragma clang diagnostic push +# pragma clang diagnostic ignored "-Wc++11-narrowing" +#elif defined(__GNUC__) +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wnarrowing" +#endif +namespace tiformat { +#include "glibc_printf_tests/tiformat.c" +} +namespace tllformat { +#include "glibc_printf_tests/tllformat.c" +} +#if defined(__clang__) +# pragma clang diagnostic pop +#elif defined(__GNUC__) +# pragma GCC diagnostic pop +#endif +namespace tfformat { +#include "glibc_printf_tests/tfformat.c" +} + +// A simple implementation of PrintfTarget, just for testing +// PrintfTarget::print. +class TestPrintfTarget : public mozilla::PrintfTarget { + public: + static const char* test_string; + + TestPrintfTarget() : mOut(0) { memset(mBuffer, '\0', sizeof(mBuffer)); } + + ~TestPrintfTarget() { + MOZ_RELEASE_ASSERT(mOut == strlen(test_string)); + MOZ_RELEASE_ASSERT(strncmp(mBuffer, test_string, strlen(test_string)) == 0); + } + + bool append(const char* sp, size_t len) override { + if (mOut + len < sizeof(mBuffer)) { + memcpy(&mBuffer[mOut], sp, len); + } + mOut += len; + return true; + } + + private: + char mBuffer[100]; + size_t mOut; +}; + +const char* TestPrintfTarget::test_string = "test string"; + +static void TestPrintfTargetPrint() { + TestPrintfTarget checker; + checker.print("test string"); +} + +// As of clang 14, __attribute__((printf)) doesn't allow %n on Android targets, +// which is used in this test. +static bool +#ifndef __ANDROID__ +MOZ_FORMAT_PRINTF(5, 6) +#endif + check_print(const char* file, int line, + bool (*cmp)(const char* a, const char* b), const char* expect, + const char* fmt, ...) { + va_list ap; + + va_start(ap, fmt); + mozilla::SmprintfPointer output = mozilla::Vsmprintf(fmt, ap); + va_end(ap); + + bool ret = output && cmp(output.get(), expect); + if (!ret && strcmp(expect, "ignore") != 0) { + fprintf(stderr, "(actual) \"%s\" != (expected) \"%s\" (%s:%d)\n", + output.get() ? output.get() : "null", expect, file, line); + } + return ret; +} + +bool str_match(const char* a, const char* b) { return !strcmp(a, b); } + +bool approx_match(const char* a, const char* b) { + return tfformat::matches(const_cast<char*>(a), b); +} + +#define print_one(...) check_print(__FILE__, __LINE__, str_match, __VA_ARGS__) + +static const char* zero() { return nullptr; } + +static void TestPrintfFormats() { + MOZ_RELEASE_ASSERT(print_one("0", "%d", 0)); + MOZ_RELEASE_ASSERT(print_one("23", "%d", 23)); + MOZ_RELEASE_ASSERT(print_one("+23", "%+d", 23)); + MOZ_RELEASE_ASSERT(print_one("-23", "%+d", -23)); + MOZ_RELEASE_ASSERT(print_one("0023", "%04d", 23)); + MOZ_RELEASE_ASSERT(print_one("777777", "%04d", 777777)); + MOZ_RELEASE_ASSERT(print_one(" 23", "% 4d", 23)); + MOZ_RELEASE_ASSERT(print_one("23 ", "%-4d", 23)); + MOZ_RELEASE_ASSERT(print_one(" 23", "%*d", 4, 23)); + MOZ_RELEASE_ASSERT(print_one("-23 ", "%*d", -7, -23)); + MOZ_RELEASE_ASSERT(print_one(" 077", "%5.3d", 77)); + MOZ_RELEASE_ASSERT(print_one(" 077", "%5.*d", 3, 77)); + MOZ_RELEASE_ASSERT(print_one(" 077", "%*.*d", 5, 3, 77)); + MOZ_RELEASE_ASSERT(print_one("077 ", "%*.*d", -5, 3, 77)); + MOZ_RELEASE_ASSERT(print_one("77 ", "%*.*d", -5, -3, 77)); + MOZ_RELEASE_ASSERT(print_one("-1", "%d", -1)); + MOZ_RELEASE_ASSERT(print_one("23", "%u", 23u)); + MOZ_RELEASE_ASSERT(print_one("0x17", "0x%x", 23u)); + MOZ_RELEASE_ASSERT(print_one("0xFF", "0x%X", 255u)); + MOZ_RELEASE_ASSERT(print_one("027", "0%o", 23u)); + MOZ_RELEASE_ASSERT(print_one("-1", "%hd", (short)-1)); + // A funny special case. + MOZ_RELEASE_ASSERT(print_one("", "%.*d", 0, 0)); + // This could be expanded if need be, it's just convenient to do + // it this way. + if (sizeof(short) == 2) { + MOZ_RELEASE_ASSERT(print_one("8000", "%hx", (unsigned short)0x8000)); + } + MOZ_RELEASE_ASSERT(print_one("2305", "%ld", 2305l)); + MOZ_RELEASE_ASSERT(print_one("-2305", "%ld", -2305l)); + MOZ_RELEASE_ASSERT(print_one("0xf0f0", "0x%lx", 0xf0f0ul)); + MOZ_RELEASE_ASSERT(print_one("0", "%lld", 0ll)); + MOZ_RELEASE_ASSERT(print_one("2305", "%lld", 2305ll)); + MOZ_RELEASE_ASSERT(print_one("-2305", "%lld", -2305ll)); + // A funny special case. + MOZ_RELEASE_ASSERT(print_one("", "%.*lld", 0, 0ll)); + MOZ_RELEASE_ASSERT(print_one("0xF0F0", "0x%llX", 0xf0f0ull)); + MOZ_RELEASE_ASSERT(print_one("27270", "%zu", (size_t)27270)); + MOZ_RELEASE_ASSERT(print_one("27270", "%tu", (ptrdiff_t)27270)); + MOZ_RELEASE_ASSERT(print_one("27270", "%ju", (intmax_t)27270)); + MOZ_RELEASE_ASSERT(print_one("hello", "he%so", "ll")); + MOZ_RELEASE_ASSERT(print_one("hello ", "%-8s", "hello")); + MOZ_RELEASE_ASSERT(print_one(" hello", "%8s", "hello")); + MOZ_RELEASE_ASSERT(print_one("hello ", "%*s", -8, "hello")); + MOZ_RELEASE_ASSERT(print_one("hello", "%.*s", 5, "hello there")); + MOZ_RELEASE_ASSERT(print_one("", "%.*s", 0, "hello there")); + MOZ_RELEASE_ASSERT(print_one("%%", "%%%%")); + MOZ_RELEASE_ASSERT(print_one("0", "%p", (char*)0)); + MOZ_RELEASE_ASSERT(print_one("h", "%c", 'h')); + MOZ_RELEASE_ASSERT(print_one("1.500000", "%f", 1.5f)); + MOZ_RELEASE_ASSERT(print_one("1.5", "%g", 1.5)); + MOZ_RELEASE_ASSERT(print_one("1.50000", "%.5f", 1.5)); + + MOZ_RELEASE_ASSERT(print_one("z ", "%-7s", "z")); + MOZ_RELEASE_ASSERT(print_one("z ", "%*s", -7, "z")); + MOZ_RELEASE_ASSERT(print_one("hello", "%*s", -3, "hello")); + + MOZ_RELEASE_ASSERT(print_one(" q", "%3c", 'q')); + MOZ_RELEASE_ASSERT(print_one("q ", "%-3c", 'q')); + MOZ_RELEASE_ASSERT(print_one(" q", "%*c", 3, 'q')); + MOZ_RELEASE_ASSERT(print_one("q ", "%*c", -3, 'q')); + + // Regression test for bug#1350097. The bug was an assertion + // failure caused by printing a very long floating point value. + print_one("ignore", "%lf", DBL_MAX); + + // Regression test for bug#1517433. The bug was an assertion + // failure caused by printing a floating point value with a large + // precision and/or width. + print_one("ignore", "%500.500lf", DBL_MAX); + + MOZ_RELEASE_ASSERT(print_one("2727", "%" PRIu32, (uint32_t)2727)); + MOZ_RELEASE_ASSERT(print_one("aa7", "%" PRIx32, (uint32_t)2727)); + MOZ_RELEASE_ASSERT(print_one("2727", "%" PRIu64, (uint64_t)2727)); + MOZ_RELEASE_ASSERT(print_one("aa7", "%" PRIx64, (uint64_t)2727)); + + int n1, n2; + MOZ_RELEASE_ASSERT(print_one(" hi ", "%n hi %n", &n1, &n2)); + MOZ_RELEASE_ASSERT(n1 == 0); + MOZ_RELEASE_ASSERT(n2 == 4); + + MOZ_RELEASE_ASSERT(print_one("23 % 24", "%2$ld %% %1$d", 24, 23l)); + MOZ_RELEASE_ASSERT( + print_one("7 8 9 10", "%4$lld %3$ld %2$d %1$hd", (short)10, 9, 8l, 7ll)); + + MOZ_RELEASE_ASSERT(print_one("0 ", "%2$p %1$n", &n1, zero())); + MOZ_RELEASE_ASSERT(n1 == 2); + + MOZ_RELEASE_ASSERT(print_one("23 % 024", "%2$-3ld%%%1$4.3d", 24, 23l)); + MOZ_RELEASE_ASSERT(print_one("23 1.5", "%2$d %1$g", 1.5, 23)); + MOZ_RELEASE_ASSERT( + print_one("ff number FF", "%3$llx %1$s %2$lX", "number", 255ul, 255ull)); + MOZ_RELEASE_ASSERT( + print_one("7799 9977", "%2$zu %1$zu", (size_t)9977, (size_t)7799)); +} + +template <typename T, size_t N> +static void TestGlibcPrintf(T (&test_cases)[N], const char* file, + bool (*cmp)(const char* a, const char* b)) { + bool ok = true; + char fmt2[40]; + for (auto& line : test_cases) { + // mozilla::PrintfTarget doesn't support the `#` flag character or the + // `a` conversion specifier. + if (!line.line || strchr(line.format_string, '#') || + strchr(line.format_string, 'a')) { + continue; + } + + // Derive the format string in the test case to add "2$" in the specifier + // (transforming e.g. "%f" into "%2$f"), and append "%1$.0d". + // The former will make the format string take the `line.value` as the + // second argument, and the latter will make the first argument formatted + // with no precision. We'll pass 0 as the first argument, such that the + // formatted value for it is "", which means the expected result string + // is still the same. + MOZ_RELEASE_ASSERT(sizeof(fmt2) > strlen(line.format_string) + 8); + const char* percent = strchr(line.format_string, '%'); + MOZ_RELEASE_ASSERT(percent); + size_t percent_off = percent - line.format_string; + memcpy(fmt2, line.format_string, percent_off + 1); + memcpy(fmt2 + percent_off + 1, "2$", 2); + strcpy(fmt2 + percent_off + 3, percent + 1); + strcat(fmt2, "%1$.0d"); + + int l = line.line; + const char* res = line.result; + const char* fmt = line.format_string; + if (strchr(line.format_string, 'I')) { + ok = check_print(file, l, cmp, res, fmt, (size_t)line.value) && ok; + ok = check_print(file, l, cmp, res, fmt2, 0, (size_t)line.value) && ok; + } else { + ok = check_print(file, l, cmp, res, fmt, line.value) && ok; + ok = check_print(file, l, cmp, res, fmt2, 0, line.value) && ok; + } + } + MOZ_RELEASE_ASSERT(ok); +} + +#if defined(XP_WIN) +int wmain() +#else +int main() +#endif // defined(XP_WIN) +{ + TestPrintfFormats(); + TestPrintfTargetPrint(); + TestGlibcPrintf(tiformat::sprint_ints, "tiformat.c", str_match); + TestGlibcPrintf(tllformat::sprint_ints, "tllformat.c", str_match); + TestGlibcPrintf(tfformat::sprint_doubles, "tfformat.c", approx_match); + + // %f is actually a not very useful formatting specifier, and if you give + // large numbers, it will print... large amounts of characters. Ensure + // that it does (which requires a patch to double-conversion). + mozilla::SmprintfPointer dbl_max = mozilla::Smprintf("%f", -DBL_MAX); + MOZ_RELEASE_ASSERT(dbl_max); + // Its length should be 309 digits before the dot, 6 after, plus the dot + // and the negative sign. + MOZ_RELEASE_ASSERT(strlen(dbl_max.get()) == 317); + + return 0; +} diff --git a/mozglue/tests/TestStackCookie.cpp b/mozglue/tests/TestStackCookie.cpp new file mode 100644 index 0000000000..6970fbc240 --- /dev/null +++ b/mozglue/tests/TestStackCookie.cpp @@ -0,0 +1,89 @@ +/* -*- 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 https://mozilla.org/MPL/2.0/. */ + +#include <stdio.h> +#include <windows.h> + +#include <array> +#include <utility> + +#include "mozilla/WindowsStackCookie.h" + +#if defined(DEBUG) && defined(_M_X64) && !defined(__MINGW64__) + +uint64_t NoStackCookie(const uint64_t* aArray, size_t aSize) { + uint64_t result = 0; + for (size_t i = 0; i < aSize; ++i) { + result += aArray[i]; + } + result /= aSize; + return result; +} + +// We expect the following instructions to be generated: +// +// 48 8b 05 XX XX XX XX mov rax,qword ptr [__security_cookie] +// 48 31 e0 xor rax,rsp 48 +// 89 44 24 38 mov qword ptr [rsp+38h],rax +uint64_t StackCookieWithSmallStackSpace(const uint64_t* aArray, size_t aSize) { + uint64_t array[0x2]{}; + for (size_t i = 0; i < aSize; ++i) { + array[aArray[i]] += aArray[i]; + } + return array[0] + array[1]; +} + +// We expect the following instructions to be generated: +// +// 48 8b 05 XX XX XX XX mov rax,qword ptr [__security_cookie] +// 48 31 e0 xor rax,rsp +// 48 89 84 24 28 40 00 00 mov qword ptr [rsp+4028h],rax +uint64_t StackCookieWithLargeStackSpace(const uint64_t* aArray, size_t aSize) { + uint64_t array[0x800]{}; + for (size_t i = 0; i < aSize; ++i) { + array[aArray[i]] += aArray[i]; + } + return array[0] + array[0x7FF]; +} + +bool TestStackCookieCheck() { + std::array<std::pair<uintptr_t, bool>, 3> testCases{ + std::make_pair<uintptr_t, bool>( + reinterpret_cast<uintptr_t>(NoStackCookie), false), + std::make_pair<uintptr_t, bool>( + reinterpret_cast<uintptr_t>(StackCookieWithSmallStackSpace), true), + std::make_pair<uintptr_t, bool>( + reinterpret_cast<uintptr_t>(StackCookieWithLargeStackSpace), true), + }; + for (auto [functionAddress, expectStackCookieCheck] : testCases) { + if (mozilla::HasStackCookieCheck(functionAddress) != + expectStackCookieCheck) { + printf( + "TEST-FAILED | StackCookie | Wrong output from HasStackCookieCheck " + "for function at %p (expected %d).\n", + reinterpret_cast<void*>(functionAddress), expectStackCookieCheck); + return false; + } + printf( + "TEST-PASS | StackCookie | Correct output from HasStackCookieCheck for " + "function at %p (expected %d).\n", + reinterpret_cast<void*>(functionAddress), expectStackCookieCheck); + } + return true; +} + +#endif // defined(DEBUG) && defined(_M_X64) && !defined(__MINGW64__) + +int wmain(int argc, wchar_t* argv[]) { +#if defined(DEBUG) && defined(_M_X64) && !defined(__MINGW64__) + if (!TestStackCookieCheck()) { + return 1; + } +#endif // defined(DEBUG) && defined(_M_X64) && !defined(__MINGW64__) + + printf("TEST-PASS | StackCookie | All tests ran successfully\n"); + return 0; +} diff --git a/mozglue/tests/TestTimeStampWin.cpp b/mozglue/tests/TestTimeStampWin.cpp new file mode 100644 index 0000000000..a69e2be59f --- /dev/null +++ b/mozglue/tests/TestTimeStampWin.cpp @@ -0,0 +1,96 @@ +/* -*- 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 https://mozilla.org/MPL/2.0/. */ + +#include "mozilla/CmdLineAndEnvUtils.h" +#include "mozilla/TimeStamp.h" + +#include "nsWindowsHelpers.h" + +#include <stdio.h> +#include <windows.h> + +static wchar_t kChildArg[] = L"--child"; + +static nsReturnRef<HANDLE> CreateProcessWrapper(const wchar_t* aPath) { + nsAutoHandle empty; + + const wchar_t* childArgv[] = {aPath, kChildArg}; + mozilla::UniquePtr<wchar_t[]> cmdLine( + mozilla::MakeCommandLine(mozilla::ArrayLength(childArgv), childArgv)); + + STARTUPINFOW si = {sizeof(si)}; + PROCESS_INFORMATION pi; + BOOL ok = ::CreateProcessW(aPath, cmdLine.get(), nullptr, nullptr, FALSE, 0, + nullptr, nullptr, &si, &pi); + if (!ok) { + printf( + "TEST-FAILED | TimeStampWin | " + "CreateProcess failed - %08lx\n", + GetLastError()); + return empty.out(); + } + + nsAutoHandle proc(pi.hProcess); + nsAutoHandle thd(pi.hThread); + + return proc.out(); +} + +int ChildMain() { + // Make sure a process creation timestamp is always not bigger than + // the current timestamp. + auto t0 = mozilla::TimeStamp::ProcessCreation(); + auto t1 = mozilla::TimeStamp::Now(); + if (t0 > t1) { + printf( + "TEST-FAILED | TimeStampWin | " + "Process creation timestamp is bigger than the current " + "timestamp!\n"); + return 1; + } + return 0; +} + +int wmain(int argc, wchar_t* argv[]) { + if (argc == 2 && wcscmp(argv[1], kChildArg) == 0) { + return ChildMain(); + } + + if (argc != 1) { + printf( + "TEST-FAILED | TimeStampWin | " + "Unexpected argc\n"); + return 1; + } + + // Start a child process successively, checking any of them terminates with + // a non-zero value which means an error. + for (int i = 0; i < 20; ++i) { + nsAutoHandle childProc(CreateProcessWrapper(argv[0])); + + if (::WaitForSingleObject(childProc, 60000) != WAIT_OBJECT_0) { + printf( + "TEST-FAILED | TimeStampWin | " + "Unexpected result from WaitForSingleObject\n"); + return 1; + } + + DWORD childExitCode; + if (!::GetExitCodeProcess(childProc.get(), &childExitCode)) { + printf( + "TEST-FAILED | TimeStampWin | " + "GetExitCodeProcess failed - %08lx\n", + GetLastError()); + return 1; + } + + if (childExitCode != 0) { + return childExitCode; + } + } + + return 0; +} diff --git a/mozglue/tests/glibc_printf_tests/COPYING b/mozglue/tests/glibc_printf_tests/COPYING new file mode 100644 index 0000000000..d159169d10 --- /dev/null +++ b/mozglue/tests/glibc_printf_tests/COPYING @@ -0,0 +1,339 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Lesser General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + <one line to give the program's name and a brief idea of what it does.> + Copyright (C) <year> <name of author> + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + <signature of Ty Coon>, 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. diff --git a/mozglue/tests/glibc_printf_tests/COPYING.LIB b/mozglue/tests/glibc_printf_tests/COPYING.LIB new file mode 100644 index 0000000000..4362b49151 --- /dev/null +++ b/mozglue/tests/glibc_printf_tests/COPYING.LIB @@ -0,0 +1,502 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations below. + + When we speak of free software, we are referring to freedom of use, +not price. Our General Public Licenses are designed to make sure that +you have the freedom to distribute copies of free software (and charge +for this service if you wish); that you receive source code or can get +it if you want it; that you can change the software and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it becomes +a de-facto standard. To achieve this, non-free programs must be +allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be distributed need not include anything that is +normally distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties with +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Libraries + + If you develop a new library, and you want it to be of the greatest +possible use to the public, we recommend making it free software that +everyone can redistribute and change. You can do so by permitting +redistribution under these terms (or, alternatively, under the terms of the +ordinary General Public License). + + To apply these terms, attach the following notices to the library. It is +safest to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least the +"copyright" line and a pointer to where the full notice is found. + + <one line to give the library's name and a brief idea of what it does.> + Copyright (C) <year> <name of author> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +Also add information on how to contact you by electronic and paper mail. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the library, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the + library `Frob' (a library for tweaking knobs) written by James Random Hacker. + + <signature of Ty Coon>, 1 April 1990 + Ty Coon, President of Vice + +That's all there is to it! diff --git a/mozglue/tests/glibc_printf_tests/README.MOZILLA b/mozglue/tests/glibc_printf_tests/README.MOZILLA new file mode 100644 index 0000000000..0016c66f70 --- /dev/null +++ b/mozglue/tests/glibc_printf_tests/README.MOZILLA @@ -0,0 +1,2 @@ +The files in this directory were copied from the stdio-common subdirectory +in glibc (https://sourceware.org/git/glibc.git). diff --git a/mozglue/tests/glibc_printf_tests/tfformat.c b/mozglue/tests/glibc_printf_tests/tfformat.c new file mode 100644 index 0000000000..97151cb876 --- /dev/null +++ b/mozglue/tests/glibc_printf_tests/tfformat.c @@ -0,0 +1,4164 @@ +#include <math.h> +#include <stdio.h> + +/* Tests taken from Cygnus C library. */ + +typedef struct +{ + int line; + double value; + const char *result; + const char *format_string; +} sprint_double_type; + +sprint_double_type sprint_doubles[] = +{ + {__LINE__, 30.3, "< +30.3>", "<%+15.10g>"}, + {__LINE__, 10.0, "<10.00>", "<%5.2f>"}, + + {__LINE__, 1.002121970718271e+05, "100212.19707 ", "%0-15.5f"}, + {__LINE__, -1.002121970718271e+05, "-100212.19707 ", "%0-15.5f"}, + {__LINE__, 1.002121970718271e+05, "000100212.19707", "%015.5f"}, + {__LINE__, -1.002121970718271e+05, "-00100212.19707", "%015.5f"}, + {__LINE__, 1.002121970718271e+05, "+00100212.19707", "%+015.5f"}, + {__LINE__, -1.002121970718271e+05, "-00100212.19707", "%+015.5f"}, + {__LINE__, 1.002121970718271e+05, " 00100212.19707", "% 015.5f"}, + {__LINE__, -1.002121970718271e+05, "-00100212.19707", "% 015.5f"}, + {__LINE__, 1.002121970718271e+05, "+100212.19707 ", "%+-15.5f"}, + {__LINE__, -1.002121970718271e+05, "-100212.19707 ", "%+-15.5f"}, + + {__LINE__, -1.002121970718271e+29, "-1.0E+29", "%.1E"}, + {__LINE__, -1.002126048612756e-02, "-1.002126E-02", "%+#E"}, + {__LINE__, -1.002653755271637e+00, "-1.00265", "%G"}, + {__LINE__, -1.003238744365917e-23, "-0.00", "%4.2f"}, + {__LINE__, -1.005084840877781e-29, " -0", "%4.f"}, + {__LINE__, -1.005362549674427e+01, "-10.0536", "%#g"}, + {__LINE__, -1.005915042991691e-17, "-1.00592E-17", "%G"}, + {__LINE__, -1.007829874228503e-18, "-1.00783e-18", "%.7g"}, + {__LINE__, -1.007829874228503e-18, "-1.007830e-18", "%#.7g"}, + {__LINE__, -1.009390937771849e+15, "-1009390937771848.628657", "%+f"}, + {__LINE__, -1.010679382726182e-29, "-0.0000000", "%.7f"}, + {__LINE__, -1.010691853346650e+13, "-10106918533466.497934", "%+f"}, + {__LINE__, -1.013412912122286e-22, "-1.01E-22", "%.2E"}, + {__LINE__, -1.019269582113858e-25, " -0", "%4.0f"}, + {__LINE__, -1.019886033368556e+24, "-1.019886E+24", "%+.7G"}, + {__LINE__, -1.021037413548719e+02, "-102.103741", "%f"}, + {__LINE__, -1.023833576089065e+26, "-1.023834E+26", "%+E"}, + {__LINE__, -1.024736652408627e+10, "-10247366524.086265", "%+f"}, + {__LINE__, -1.025439198495476e+09, "-1.02544e+09", "%+g"}, + {__LINE__, -1.027080247585776e-04, "-0.0001027", "%6.7f"}, + {__LINE__, -1.028096307262016e+18, "-1.0281E+18", "%3G"}, + {__LINE__, -1.029604290697901e-02, "-0.010296", "%g"}, + {__LINE__, -1.034347730570491e+16, "-10343477305704908.975059", "%+f"}, + {__LINE__, -1.034663325049286e+22, "-1.0E+22", "%#.1E"}, + {__LINE__, -1.034843152721857e-14, " -0", "%6.f"}, + {__LINE__, -1.036082122299529e-29, "-1.04e-29", "%4.3g"}, + {__LINE__, -1.037213662365954e-09, "-1e-09", "%3.e"}, + {__LINE__, -1.038563976775690e-12, "-0.000000", "%f"}, + {__LINE__, -1.040910158681323e-02, "-0.0104091", "%+G"}, + {__LINE__, -1.044680094714482e-20, "-1.04468E-20", "%G"}, + {__LINE__, -1.044990054091126e+24, "-1.044990E+24", "%+E"}, + {__LINE__, -1.045693871096982e+11, "-1.045694e+11", "%+e"}, + {__LINE__, -1.045714133591312e-04, "-0.000104571", "%+#3g"}, + {__LINE__, -1.046215079103016e-15, "-1.04622e-15", "%g"}, + {__LINE__, -1.046285293993789e-18, "-1.04629E-18", "%+4G"}, + {__LINE__, -1.046306092899333e-06, "-0.00", "%0.2f"}, + {__LINE__, -1.047308973649206e-22, "-1.05E-22", "%+1.3G"}, + {__LINE__, -1.047369032507755e+01, "-1.047369E+01", "%E"}, + {__LINE__, -1.048986365562919e-21, "-1.05E-21", "%+.2E"}, + {__LINE__, -1.049530193156793e-17, "-1.04953E-17", "%+G"}, + {__LINE__, -1.050073419263768e+25, "-1.05007e+25", "%g"}, + {__LINE__, -1.051739652002504e-28, "-0.000000", "%+f"}, + {__LINE__, -1.054493420082636e+21, "-1.0545e+21", "%#5.4e"}, + {__LINE__, -1.055867291029098e+18, "-1.05587e+18", "%g"}, + {__LINE__, -1.056514389757866e-16, "-1.05651E-16", "%5.6G"}, + {__LINE__, -1.057180924868704e+15, "-1057180924868704", "%4.f"}, + {__LINE__, -1.058455468395683e-23, "-1.05846e-23", "%g"}, + {__LINE__, -1.062560982393212e+08, "-1.06256e+08", "%g"}, + {__LINE__, -1.063365829241138e-10, "-1.063366e-10", "%+e"}, + {__LINE__, -1.063568908667280e-19, "-1.06357E-19", "%+G"}, + {__LINE__, -1.063734263253492e-13, "-0.000000", "%0f"}, + {__LINE__, -1.064472689765495e-13, "-1E-13", "%4.0G"}, + {__LINE__, -1.067192610000129e-25, "-1.06719E-25", "%G"}, + {__LINE__, -1.068401334996592e-12, "-0.0000000", "%+.7f"}, + {__LINE__, -1.069012628653724e-13, "-1.069013E-13", "%+.7G"}, + {__LINE__, -1.069451976810790e+16, "-10694519768107904.056365", "%f"}, + {__LINE__, -1.069568935323556e+17, "-1.06957e+17", "%g"}, + {__LINE__, -1.071351044854107e-29, "-1.07135e-29", "%g"}, + {__LINE__, -1.072274197526185e-21, "-1E-21", "%1.G"}, + {__LINE__, -1.073875921752995e+23, "-1E+23", "%5.E"}, + {__LINE__, -1.074835151152265e-12, "-1.0748e-12", "%1.5g"}, + {__LINE__, -1.075171047088241e-19, "-1.07517E-19", "%#G"}, + {__LINE__, -1.076258826412760e+22, "-1.076259e+22", "%+e"}, + {__LINE__, -1.076365103160401e+06, "-1.07637E+06", "%+4.6G"}, + {__LINE__, -1.076817750454633e+08, "-1e+08", "%4.g"}, + {__LINE__, -1.078615405755685e-30, "-1e-30", "%4.g"}, + {__LINE__, -1.078629622917468e-25, "-1.078630e-25", "%e"}, + {__LINE__, -1.079352432833170e+11, "-107935243283", "%+2.f"}, + {__LINE__, -1.081431147440215e+16, "-10814311474402147.439378", "%+f"}, + {__LINE__, -1.083042116905339e-16, "-1.083e-16", "%.5g"}, + {__LINE__, -1.085351710708553e-10, "-1.085e-10", "%1.4g"}, + {__LINE__, -1.085796045618276e+07, "-1.085796e+07", "%e"}, + {__LINE__, -1.087398259981007e+22, "-1.0874e+22", "%g"}, + {__LINE__, -1.087986044402224e-11, "-1.087986e-11", "%e"}, + {__LINE__, -1.090451848762709e-02, "-1.090452e-02", "%e"}, + {__LINE__, -1.091463236899737e+11, "-1.091463E+11", "%#E"}, + {__LINE__, -1.091617921737384e-29, "-1.09162e-29", "%3g"}, + {__LINE__, -1.092049328579047e-17, "-1.092049E-17", "%E"}, + {__LINE__, -1.093647615472090e+06, "-1093647.61547", "%6.5f"}, + {__LINE__, -1.094133175602384e-08, "-1.1E-08", "%0.1E"}, + {__LINE__, -1.095397916728214e-23, "-1e-23", "%6.e"}, + {__LINE__, -1.098958790437321e+02, "-109.895879", "%+#f"}, + {__LINE__, -1.100194638181594e-20, "-1.100195e-20", "%e"}, + {__LINE__, -1.102174253534260e+05, "-1.1e+05", "%2.3g"}, + {__LINE__, -1.102890180316350e-12, "-1.10289e-12", "%+g"}, + {__LINE__, -1.105582337418378e+00, "-1.11", "%3.3g"}, + {__LINE__, -1.110515122647056e+04, "-1.E+04", "%#0.G"}, + {__LINE__, -1.111365895262625e-18, "-1e-18", "%0.g"}, + {__LINE__, -1.112010622677495e+04, "-11120.1", "%g"}, + {__LINE__, -1.112580043156699e-23, "-1.11258e-23", "%1g"}, + {__LINE__, -1.112829057091303e+08, "-1.11283e+08", "%+2g"}, + {__LINE__, -1.115019046200472e+18, "-1.1150190e+18", "%+#.7e"}, + {__LINE__, -1.118078332268016e-13, "-1.12E-13", "%1.3G"}, + {__LINE__, -1.119113571963886e+08, "-1.119114e+08", "%e"}, + {__LINE__, -1.120102397563448e-15, "-1.12e-15", "%.4g"}, + {__LINE__, -1.120707480791434e-19, "-1.12071e-19", "%g"}, + {__LINE__, -1.124243676139007e-07, "-0.000000", "%f"}, + {__LINE__, -1.125025214608798e+15, "-1.12503E+15", "%0G"}, + {__LINE__, -1.126074395136447e+06, "-1E+06", "%+.0G"}, + {__LINE__, -1.127203019940870e-06, "-0.000001", "%+#7f"}, + {__LINE__, -1.130577648480677e+01, "-1.130578e+01", "%e"}, + {__LINE__, -1.131469694425240e-11, "-1.13147e-11", "%#0.6g"}, + {__LINE__, -1.132225023239752e+02, "-113.223", "%G"}, + {__LINE__, -1.133702113050128e+20, "-1E+20", "%+3.E"}, + {__LINE__, -1.135529466224404e-13, "-1.13553e-13", "%#g"}, + {__LINE__, -1.137587210063004e+21, "-1e+21", "%.1g"}, + {__LINE__, -1.140765637106361e-21, "-1.1e-21", "%+.1e"}, + {__LINE__, -1.141182595083699e-18, "-0.000000", "%f"}, + {__LINE__, -1.141695709120972e+00, "-1.1417", "%3G"}, + {__LINE__, -1.143199141708028e+18, "-1143199141708027833", "%2.f"}, + {__LINE__, -1.146712902056139e+21, "-1146712902056139071760.298975", "%f"}, + {__LINE__, -1.146837903839073e-02, " -0.0", "%+5.1f"}, + {__LINE__, -1.147363016107446e+10, "-1.14736e+10", "%+7g"}, + {__LINE__, -1.149575523465052e+20, "-114957552346505220697.28140", "%+.5f"}, + {__LINE__, -1.152748955392589e+13, "-1.15275E+13", "%G"}, + {__LINE__, -1.152751106015483e-17, "-1E-17", "%+3.E"}, + {__LINE__, -1.155539139258226e-18, "-1.2e-18", "%+.2g"}, + {__LINE__, -1.157089280563492e+20, "-1.15709e+20", "%g"}, + {__LINE__, -1.161217745859779e+19, "-1.161218E+19", "%E"}, + {__LINE__, -1.162293536734798e+10, "-11622935367.347980", "%f"}, + {__LINE__, -1.162629909468603e+20, "-1.16E+20", "%.3G"}, + {__LINE__, -1.164979155838631e+24, "-1.16498E+24", "%G"}, + {__LINE__, -1.165103052644816e-20, " -1e-20", "%+7.g"}, + {__LINE__, -1.166448459023264e-08, "-0.000000", "%f"}, + {__LINE__, -1.167694506705309e+28, "-1e+28", "%.1g"}, + {__LINE__, -1.169901754818745e-28, "-0.000000", "%+f"}, + {__LINE__, -1.170330336216446e+08, " -1e+08", "%7.g"}, + {__LINE__, -1.170946623214213e-17, "-1.17095E-17", "%G"}, + {__LINE__, -1.174581596799302e+27, "-1.174582e+27", "%+e"}, + {__LINE__, -1.174763473995155e-28, "-1E-28", "%.1G"}, + {__LINE__, -1.175885640508038e-23, "-1E-23", "%+.0E"}, + {__LINE__, -1.177268411775439e-05, "-0.000012", "%#2.6f"}, + {__LINE__, -1.177739669428001e-30, "-1.17774E-30", "%G"}, + {__LINE__, -1.178059639880544e-02, "-0.0117806", "%+.7G"}, + {__LINE__, -1.178793300854446e-13, "-1.2E-13", "%+#0.2G"}, + {__LINE__, -1.179629345138058e-04, "-0.000118", "%0.3g"}, + {__LINE__, -1.180730292213358e-25, "-1.18073e-25", "%g"}, + {__LINE__, -1.180766261654697e+02, "-118.077", "%3g"}, + {__LINE__, -1.181880261069391e-29, "-1.181880e-29", "%+#e"}, + {__LINE__, -1.183752810063514e-09, "-1.18375e-09", "%#g"}, + {__LINE__, -1.184191742443406e-21, "-1.184192e-21", "%e"}, + {__LINE__, -1.184859760488406e-26, "-1.18486E-26", "%G"}, + {__LINE__, -1.187276828720072e-29, "-1.18728E-29", "%G"}, + {__LINE__, -1.187992907205195e-04, "-1.187993E-04", "%E"}, + {__LINE__, -1.190586192763405e-28, "-1.E-28", "%#1.E"}, + {__LINE__, -1.192104053032086e+02, "-119.21", "%G"}, + {__LINE__, -1.192917884333569e+26, "-1.192918E+26", "%E"}, + {__LINE__, -1.195900753509801e+22, "-1.1959e+22", "%g"}, + {__LINE__, -1.196559413116537e-28, "-0.000000", "%6.6f"}, + {__LINE__, -1.197534588732952e+12, "-1.19753e+12", "%#g"}, + {__LINE__, -1.200279514790649e-25, "-0.0000", "%0.4f"}, + {__LINE__, -1.203806667944635e+10, "-1.2038067E+10", "%5.7E"}, + {__LINE__, -1.204344885974736e+07, "-1.204E+07", "%#2.4G"}, + {__LINE__, -1.205668007693083e+00, "-1.20567", "%g"}, + {__LINE__, -1.205898515218947e-11, "-1.2059e-11", "%+g"}, + {__LINE__, -1.206787414909373e-05, "-1.2e-05", "%2.2g"}, + {__LINE__, -1.207905830366447e+15, "-1.207906e+15", "%6.7g"}, + {__LINE__, -1.208067010597729e-17, "-1.208067e-17", "%5e"}, + {__LINE__, -1.210189135822574e+01, "-12", "%+2.0f"}, + {__LINE__, -1.211560695948122e+10, "-1.21156e+10", "%+g"}, + {__LINE__, -1.214003025273234e-18, "-1.214e-18", "%+g"}, + {__LINE__, -1.214096815259005e+17, "-121409681525900459.142520", "%f"}, + {__LINE__, -1.214699041716797e-14, "-1.2147E-14", "%G"}, + {__LINE__, -1.215061611919443e+14, "-121506161191944.306310", "%+#f"}, + {__LINE__, -1.217491221065185e+27, "-1.21749e+27", "%g"}, + {__LINE__, -1.218538401534388e-11, "-1.218538e-11", "%#e"}, + {__LINE__, -1.218810085883466e+16, "-1e+16", "%+.1g"}, + {__LINE__, -1.219422688169801e+08, "-1.219423e+08", "%e"}, + {__LINE__, -1.220473194407651e-14, "-1.2205E-14", "%1.5G"}, + {__LINE__, -1.220824440193375e-02, "-0.012208", "%4f"}, + {__LINE__, -1.221520240637007e+13, "-1.221520E+13", "%1E"}, + {__LINE__, -1.221894719843486e+29, "-1.22189E+29", "%G"}, + {__LINE__, -1.223020108610281e+25, "-1.22302E+25", "%G"}, + {__LINE__, -1.223203206227728e-02, "-0.012232", "%G"}, + {__LINE__, -1.225661737076919e-15, "-1.22566E-15", "%G"}, + {__LINE__, -1.228147221003795e-08, "-1.22815E-08", "%G"}, + {__LINE__, -1.228298534591771e+20, "-1.22830E+20", "%#G"}, + {__LINE__, -1.228469080229780e-02, "-0.012285", "%0f"}, + {__LINE__, -1.229161950699222e-09, "-1e-09", "%0.e"}, + {__LINE__, -1.231294820136559e-17, "-1.231295E-17", "%1.7G"}, + {__LINE__, -1.232588663406698e-21, "-1E-21", "%+0.G"}, + {__LINE__, -1.233381256982191e-26, "-0.000000", "%f"}, + {__LINE__, -1.233435864835578e+14, "-1.23344e+14", "%#g"}, + {__LINE__, -1.237409567806099e-25, "-1.23741E-25", "%#G"}, + {__LINE__, -1.238244697758558e-02, "-1.238245e-02", "%7e"}, + {__LINE__, -1.239393163123284e+26, "-1.23939e+26", "%+g"}, + {__LINE__, -1.240484781756132e-18, "-1E-18", "%4.E"}, + {__LINE__, -1.241420286838750e+23, "-1E+23", "%2.E"}, + {__LINE__, -1.243781122052343e-18, "-1.243781e-18", "%+e"}, + {__LINE__, -1.244421506844779e+07, "-1.244422E+07", "%+6E"}, + {__LINE__, -1.245754054764741e-21, "-1e-21", "%5.0g"}, + {__LINE__, -1.245992228426733e-24, "-0.000000", "%f"}, + {__LINE__, -1.246498277739883e-12, "-0.0000000", "%5.7f"}, + {__LINE__, -1.247130891343776e-18, "-1.24713E-18", "%G"}, + {__LINE__, -1.247309461703025e-03, "-0.0012473", "%#5.5G"}, + {__LINE__, -1.248139162966407e-29, "-1.e-29", "%+#6.g"}, + {__LINE__, -1.249756153623776e+08, "-1.249756e+08", "%e"}, + {__LINE__, -1.250899983565585e-10, "-1.2509e-10", "%5g"}, + {__LINE__, -1.250957368798327e+19, "-1.25096E+19", "%0G"}, + {__LINE__, -1.252630814464822e-02, "-0.012526", "%+f"}, + {__LINE__, -1.253076368257011e-28, "-0.000000", "%f"}, + {__LINE__, -1.254243507039370e+01, "-1.254244e+01", "%e"}, + {__LINE__, -1.257605614492298e-12, "-1.257606E-12", "%6.7G"}, + {__LINE__, -1.258041911573120e+06, "-1258041.911573", "%+f"}, + {__LINE__, -1.261670983426507e-25, "-0.00", "%.2f"}, + {__LINE__, -1.263216883336562e-14, "-1.26322e-14", "%g"}, + {__LINE__, -1.266846944498751e-15, "-1e-15", "%0.g"}, + {__LINE__, -1.266977908502326e+06, "-1266978", "%+1.f"}, + {__LINE__, -1.267006162870084e-23, "-1.267006e-23", "%e"}, + {__LINE__, -1.269144609375931e+01, "-1.269145e+01", "%+e"}, + {__LINE__, -1.269458714257904e+25, "-1.26946E+25", "%G"}, + {__LINE__, -1.276697325772662e-02, "-0.0128", "%7.3G"}, + {__LINE__, -1.278855081807602e+15, "-1278855081807601.87891", "%#0.5f"}, + {__LINE__, -1.278966821639612e+20, "-1.278967E+20", "%3E"}, + {__LINE__, -1.281942705258106e+03, "-1281.94", "%+G"}, + {__LINE__, -1.282331291499203e+14, "-128233129149920.266343", "%+f"}, + {__LINE__, -1.285125739341808e-28, "-1.28513e-28", "%g"}, + {__LINE__, -1.285700693704978e+11, "-1.285701E+11", "%E"}, + {__LINE__, -1.286574096459251e+28, "-1.287E+28", "%6.4G"}, + {__LINE__, -1.287967889247240e+03, "-1287.97", "%G"}, + {__LINE__, -1.288913808801319e-27, "-1.3e-27", "%7.1e"}, + {__LINE__, -1.289147517760377e-18, "-1.289148E-18", "%E"}, + {__LINE__, -1.289355006600107e+23, "-1.28936E+23", "%+G"}, + {__LINE__, -1.291232014623979e+01, "-1.29123E+01", "%+.5E"}, + {__LINE__, -1.293658562875966e-18, "-1.29366e-18", "%+g"}, + {__LINE__, -1.294982911983638e-19, "-1.29498E-19", "%#G"}, + {__LINE__, -1.296123363481695e-13, "-1.296123E-13", "%E"}, + {__LINE__, -1.296632862070602e-20, "-1.29663e-20", "%+g"}, + {__LINE__, -1.297801639022777e+23, "-129780163902277735132884.11777", "%#0.5f"}, + {__LINE__, -1.297886574534611e+16, "-1.297887e+16", "%+e"}, + {__LINE__, -1.300588216308401e+11, "-1.30e+11", "%.2e"}, + {__LINE__, -1.301296100909062e-10, "-1.3013e-10", "%+g"}, + {__LINE__, -1.303144244306468e-07, "-1.303144E-07", "%E"}, + {__LINE__, -1.304096315024042e-20, "-1.3041E-20", "%+G"}, + {__LINE__, -1.304817970675085e+16, "-1.304818E+16", "%+E"}, + {__LINE__, -1.304870304632683e-15, " -1e-15", "%7.g"}, + {__LINE__, -1.305213586757638e-18, "-1.30521e-18", "%g"}, + {__LINE__, -1.306880734910465e-22, "-1.306881E-22", "%E"}, + {__LINE__, -1.308964092631446e-23, "-1.30896e-23", "%g"}, + {__LINE__, -1.312070555198201e+26, "-131207055519820126043839537", "%+7.f"}, + {__LINE__, -1.312511609151056e-30, "-1.312512E-30", "%#E"}, + {__LINE__, -1.313087359008389e-23, "-0.000000", "%+f"}, + {__LINE__, -1.316948423042059e-02, "-1.316948E-02", "%+E"}, + {__LINE__, -1.317080882577385e+29, "-1.317E+29", "%+.4G"}, + {__LINE__, -1.317514598984346e+24, "-1.317515e+24", "%e"}, + {__LINE__, -1.317817551125923e-21, "-1.31782E-21", "%+G"}, + {__LINE__, -1.319603985988120e+29, "-131960398598811989856471882376.354188", "%f"}, + {__LINE__, -1.328850458671907e+06, "-1e+06", "%.0e"}, + {__LINE__, -1.328945346449817e-23, "-1.328945e-23", "%+e"}, + {__LINE__, -1.330146584094221e-08, "-1e-08", "%+5.g"}, + {__LINE__, -1.332609617892115e+00, "-1.33261", "%2.7G"}, +#ifndef __PCCNECV70__ + {__LINE__, -1.332751724965715e+22, "-13327517249657150344432.920974", "%f"}, + {__LINE__, -1.333194379353273e-19, "-0.000000", "%f"}, + {__LINE__, -1.334304387932777e-06, "-1.334304e-06", "%e"}, + {__LINE__, -1.334306581172082e-05, "-1.3e-05", "%+1.2g"}, + {__LINE__, -1.335283510893438e-05, " -1E-05", "%7.G"}, + {__LINE__, -1.337939692108373e+11, "-1.33794e+11", "%+4g"}, + {__LINE__, -1.339117288874809e-03, "-0.001", "%1.3f"}, + {__LINE__, -1.340100588141492e+27, "-1.340101E+27", "%E"}, + {__LINE__, -1.341953272572953e-19, "-0.000000", "%+f"}, + {__LINE__, -1.343058354301620e-02, "-1.343058e-02", "%e"}, + {__LINE__, -1.346662720871543e+22, "-1.34666E+22", "%#G"}, + {__LINE__, -1.354010578652210e-02, "-0.0135401", "%#g"}, + {__LINE__, -1.354066549307666e-12, " -0", "%+6.f"}, + {__LINE__, -1.355284719365947e+21, "-1.35528e+21", "%g"}, + {__LINE__, -1.356326532145087e+29, "-1E+29", "%4.G"}, + {__LINE__, -1.357278618897291e+19, "-13572786188972911780.581398", "%f"}, + {__LINE__, -1.357537331348202e-10, "-0.000", "%+.3f"}, + {__LINE__, -1.360011287595868e-10, "-0.000000", "%f"}, + {__LINE__, -1.360506902899232e+16, "-13605069028992320.", "%#5.f"}, + {__LINE__, -1.361102570277000e+10, "-1.3611E+10", "%G"}, + {__LINE__, -1.362601255900219e+07, "-1.363E+07", "%.4G"}, + {__LINE__, -1.366078182354771e+10, "-1.36608E+10", "%0G"}, + {__LINE__, -1.366667926615127e+08, "-136666792.661513", "%f"}, + {__LINE__, -1.367162823743231e-12, "-0.000000", "%#f"}, + {__LINE__, -1.371523037550709e+09, "-1.37152e+09", "%+g"}, + {__LINE__, -1.374328925986197e-17, "-1.37e-17", "%.2e"}, + {__LINE__, -1.374995603898421e-26, "-1.375e-26", "%+g"}, + {__LINE__, -1.376726539375688e+09, "-1.376727E+09", "%+E"}, + {__LINE__, -1.378351117694958e-13, "-0.000000", "%f"}, + {__LINE__, -1.378514699313619e-14, "-1.378515e-14", "%e"}, + {__LINE__, -1.379347588942324e-23, "-1.4e-23", "%+.1e"}, + {__LINE__, -1.380380583822272e-17, "-0.000000", "%+f"}, + {__LINE__, -1.381658659947406e+19, "-1.38166e+19", "%g"}, + {__LINE__, -1.382775316390237e+29, "-138277531639023653298181670834.3104", "%+#7.4f"}, + {__LINE__, -1.383541138922400e+00, "-1.383541E+00", "%E"}, + {__LINE__, -1.384625301445090e+01, "-13.8", "%+.3g"}, + {__LINE__, -1.386844086284027e-13, "-1.38684E-13", "%+G"}, + {__LINE__, -1.386930516448650e-28, "-1.386931E-28", "%E"}, + {__LINE__, -1.387444896054260e-07, "-1.38744E-07", "%G"}, + {__LINE__, -1.389685107980798e+22, "-1.38969E+22", "%G"}, + {__LINE__, -1.390880300369347e+03, "-1390.880300", "%f"}, + {__LINE__, -1.391423370198150e-17, "-1E-17", "%+2.G"}, + {__LINE__, -1.394441767471218e-09, "-1.394442e-09", "%e"}, + {__LINE__, -1.396275525062527e-20, "-1.39628E-20", "%G"}, + {__LINE__, -1.397045957455157e+24, "-1E+24", "%.0E"}, + {__LINE__, -1.397458546930799e+21, "-1397458546930798526375.383517", "%f"}, + {__LINE__, -1.397584578988941e-14, "-1.39758e-14", "%5g"}, + {__LINE__, -1.397789397300823e+09, "-1.398e+09", "%.4g"}, + {__LINE__, -1.398167472294874e+21, "-1.398167E+21", "%+7E"}, + {__LINE__, -1.398809636136688e-16, "-0.000000", "%+f"}, + {__LINE__, -1.399545335304119e+26, "-139954533530411872277454676.805064", "%#5f"}, + {__LINE__, -1.400102603335755e+20, "-140010260333575509150.705329", "%2f"}, + {__LINE__, -1.401212746235235e+04, "-1.401213e+04", "%e"}, + {__LINE__, -1.404409427681801e+07, "-1E+07", "%4.E"}, + {__LINE__, -1.406018114263948e-25, "-1e-25", "%3.e"}, + {__LINE__, -1.406245608096877e-06, "-1.4e-06", "%+.1e"}, + {__LINE__, -1.410697254683835e-10, "-1.410697e-10", "%e"}, + {__LINE__, -1.410739097553846e+25, "-1.410739E+25", "%+E"}, + {__LINE__, -1.410850631189577e+04, "-14108.5", "%.6g"}, + {__LINE__, -1.411680434455781e+28, "-14116804344557807304738616949.180141", "%f"}, + {__LINE__, -1.413309465660099e-27, "-1.413309E-27", "%+#E"}, + {__LINE__, -1.418468741386300e+09, "-1418468741.386300", "%f"}, + {__LINE__, -1.420277110892909e+06, "-1.42028e+06", "%1g"}, + {__LINE__, -1.420417889565590e-17, "-1.42E-17", "%.2E"}, + {__LINE__, -1.421098212056305e+08, "-1.421098e+08", "%4e"}, + {__LINE__, -1.421792812798986e-20, " -1E-20", "%7.G"}, + {__LINE__, -1.423675488122461e+18, "-1423675488122461363.360571", "%f"}, + {__LINE__, -1.423810545840653e+15, "-1E+15", "%3.E"}, + {__LINE__, -1.424242673476368e-15, "-1.424243e-15", "%e"}, + {__LINE__, -1.427847435688733e-01, "-1.427847E-01", "%E"}, + {__LINE__, -1.433897734612555e-04, "-0.0001", "%3.g"}, + {__LINE__, -1.434537754075696e-24, "-1.435E-24", "%+0.3E"}, + {__LINE__, -1.434774864809324e-24, "-1E-24", "%2.E"}, + {__LINE__, -1.435583851565142e-11, "-1E-11", "%.0E"}, + {__LINE__, -1.438791024010112e+11, "-1.43879e+11", "%#.6g"}, + {__LINE__, -1.439267565343777e+27, "-1.4e+27", "%2.1e"}, + {__LINE__, -1.439440602670449e+02, "-143.944", "%G"}, + {__LINE__, -1.439494412124925e+13, "-14394944121249.251268", "%f"}, + {__LINE__, -1.440032823245152e+10, "-14400328232.451516", "%f"}, + {__LINE__, -1.440174494009562e-08, "-0.000000", "%7f"}, + {__LINE__, -1.440827804010568e+07, "-1.E+07", "%#6.1G"}, + {__LINE__, -1.442760907195336e+09, "-1.44276E+09", "%+5G"}, + {__LINE__, -1.444655304181403e+10, "-14446553041.814035", "%7f"}, + {__LINE__, -1.444995766787036e+04, "-14450", "%5G"}, + {__LINE__, -1.445078682874535e-13, "-1E-13", "%3.E"}, + {__LINE__, -1.446781469662328e+01, "-14.4678", "%G"}, + {__LINE__, -1.447795251395321e-04, "-0.000145", "%+f"}, + {__LINE__, -1.448193079247108e-02, "-0.014", "%4.2G"}, + {__LINE__, -1.449552967961345e+14, "-1E+14", "%1.E"}, + {__LINE__, -1.451269763513571e+10, "-14512697635.135705", "%6f"}, + {__LINE__, -1.451843176990292e+12, "-1e+12", "%1.g"}, + {__LINE__, -1.452631405935931e+06, "-1.452631e+06", "%e"}, + {__LINE__, -1.452753650285897e+21, "-1452753650285897091265.496783", "%5f"}, + {__LINE__, -1.453811512433860e-05, "-1e-05", "%+6.g"}, + {__LINE__, -1.454071430557372e+13, "-1.4541E+13", "%.4E"}, + {__LINE__, -1.455956079562999e+03, "-1455.96", "%G"}, + {__LINE__, -1.459712196146602e-17, "-1.45971e-17", "%g"}, + {__LINE__, -1.461857733495947e-02, "-1.4619E-02", "%.4E"}, + {__LINE__, -1.465257069090911e-11, "-1.465257E-11", "%+E"}, + {__LINE__, -1.465306607257042e+05, "-146531", "%g"}, + {__LINE__, -1.465857076315921e+20, "-1.46586E+20", "%+G"}, + {__LINE__, -1.467919792051489e+16, "-1.46792e+16", "%2g"}, + {__LINE__, -1.469271519834567e+20, "-146927151983456720785", "%+.0f"}, + {__LINE__, -1.472961447442697e-28, "-1.47296e-28", "%#g"}, + {__LINE__, -1.473385695190456e-12, "-1.473386E-12", "%E"}, + {__LINE__, -1.485003089033267e-10, "-1.485E-10", "%2G"}, + {__LINE__, -1.490541379072390e-18, "-1.490541E-18", "%4E"}, + {__LINE__, -1.490683434793125e+22, "-14906834347931249951102.472551", "%#f"}, + {__LINE__, -1.498356837215425e+20, "-1.49836E+20", "%#G"}, + {__LINE__, -1.498677007818122e+14, "-1.49868E+14", "%+4G"}, + {__LINE__, -1.502143197677119e+23, "-150214319767711934616727.", "%#7.f"}, + {__LINE__, -1.503563770470753e-24, "-1.50356e-24", "%.6g"}, + {__LINE__, -1.504643092793197e-18, "-1.504643e-18", "%+4e"}, + {__LINE__, -1.507017706254022e-13, "-1.50702e-13", "%+g"}, + {__LINE__, -1.507054033750081e-22, "-0.000000", "%f"}, + {__LINE__, -1.508662346022339e+16, "-1.508662E+16", "%#E"}, + {__LINE__, -1.509366882438902e+08, "-2e+08", "%4.g"}, + {__LINE__, -1.511086567087967e+20, "-1.51109e+20", "%g"}, + {__LINE__, -1.514959134622707e-18, "-0.000000", "%+f"}, + {__LINE__, -1.515730120364204e+06, "-2E+06", "%0.G"}, + {__LINE__, -1.516372763118606e-09, "-1.51637e-09", "%g"}, + {__LINE__, -1.520610443546204e+27, "-1.52061e+27", "%g"}, + {__LINE__, -1.522040655642012e+11, "-1.52204E+11", "%+2G"}, + {__LINE__, -1.523772495014966e-24, "-2E-24", "%2.G"}, + {__LINE__, -1.523980825983892e-05, "-0.000", "%2.3f"}, + {__LINE__, -1.526696214603152e-03, "-0.0015267", "%+6.5G"}, + {__LINE__, -1.528680540769583e+00, "-1.5287", "%0.5G"}, + {__LINE__, -1.529951830525433e+08, "-1.529952e+08", "%+e"}, + {__LINE__, -1.533052896559935e+05, "-153305", "%4.0f"}, + {__LINE__, -1.533829640475152e-03, "-0.00153383", "%+#g"}, + {__LINE__, -1.535617804643105e-30, "-1.535618e-30", "%e"}, + {__LINE__, -1.537623698361821e+16, "-15376236983618207.7875719", "%4.7f"}, + {__LINE__, -1.544994895330616e+24, "-1.54499e+24", "%g"}, + {__LINE__, -1.545083356728403e+17, "-1.545083e+17", "%e"}, + {__LINE__, -1.545684535393879e+12, "-1.55E+12", "%3.3G"}, + {__LINE__, -1.545878334099427e+13, "-15458783340994.268762", "%1f"}, + {__LINE__, -1.547697185980564e-18, "-1.5477e-18", "%.6g"}, + {__LINE__, -1.548576527172579e-29, "-1.5E-29", "%2.2G"}, + {__LINE__, -1.549712227835891e+08, "-1.549712e+08", "%#6.7g"}, + {__LINE__, -1.550010819435335e-21, "-1.55001E-21", "%G"}, + {__LINE__, -1.551412476164257e+14, "-1.551412E+14", "%E"}, + {__LINE__, -1.553156638090030e+01, " -2E+01", "%7.G"}, + {__LINE__, -1.553733233064355e-01, "-0.2", "%3.g"}, + {__LINE__, -1.558391111609365e+00, "-1.558391", "%+f"}, + {__LINE__, -1.562004019284766e+15, "-1.562E+15", "%6.5G"}, + {__LINE__, -1.562195733239758e-16, "-0.000000", "%f"}, + {__LINE__, -1.563953339012525e-14, "-1.56395E-14", "%1G"}, + {__LINE__, -1.568317638534983e+18, "-1.56832E+18", "%1G"}, + {__LINE__, -1.568546851135348e+04, "-15685.5", "%+#G"}, + {__LINE__, -1.574297872228857e+18, "-1.574298e+18", "%e"}, + {__LINE__, -1.575696460196291e-21, "-1.5757E-21", "%G"}, + {__LINE__, -1.576324848379791e+25, "-1.57632E+25", "%+G"}, + {__LINE__, -1.577524395140843e+06, "-1.5775E+06", "%.5G"}, + {__LINE__, -1.585122856675091e+12, "-1585122856675.091452", "%f"}, + {__LINE__, -1.586734674395556e-02, "-1.586735E-02", "%E"}, + {__LINE__, -1.592602264315192e+22, "-1.5926E+22", "%G"}, + {__LINE__, -1.594121556700562e+05, "-2E+05", "%+.0G"}, + {__LINE__, -1.598538116193430e+22, "-1.59854e+22", "%+g"}, + {__LINE__, -1.605647463222382e+18, "-1.6056E+18", "%.5G"}, + {__LINE__, -1.610891449201191e+06, "-1.6109E+06", "%#6.4E"}, + {__LINE__, -1.614593168166818e-05, "-1.61459E-05", "%G"}, + {__LINE__, -1.615597722557325e-03, "-0.001616", "%f"}, + {__LINE__, -1.616038022182124e-29, "-1.61604E-29", "%1G"}, + {__LINE__, -1.618380771451625e-28, "-2e-28", "%+1.g"}, + {__LINE__, -1.620287456872150e+10, "-1.620e+10", "%#7.3e"}, + {__LINE__, -1.620469955611903e-29, "-0.000000", "%f"}, + {__LINE__, -1.622580720690927e+20, "-1.622581E+20", "%+#E"}, + {__LINE__, -1.622752500991079e-12, "-1.622753E-12", "%E"}, + {__LINE__, -1.622771933776459e+08, "-1.6228E+08", "%+4.4E"}, + {__LINE__, -1.623894567037391e+09, "-1.623895e+09", "%#e"}, + {__LINE__, -1.624021408185354e-25, "-1.624E-25", "%.5G"}, + {__LINE__, -1.624405320939366e-06, "-1.62441e-06", "%+#g"}, + {__LINE__, -1.631236208958857e-25, "-1.631236e-25", "%#e"}, + {__LINE__, -1.632165969536046e+20, "-1.63217E+20", "%G"}, + {__LINE__, -1.632859596256475e+06, "-1.63286E+06", "%G"}, + {__LINE__, -1.634371961703112e+06, "-1.634372E+06", "%+E"}, + {__LINE__, -1.637230383641845e+24, "-1.63723e+24", "%#g"}, + {__LINE__, -1.638081480475853e+17, "-1.63808e+17", "%+g"}, + {__LINE__, -1.638279690467673e+14, "-163827969046767.325523", "%#f"}, + {__LINE__, -1.640360249548215e-29, "-1.64036e-29", "%+6.6g"}, + {__LINE__, -1.645691728060833e-30, "-1.64569e-30", "%g"}, + {__LINE__, -1.648548965852592e-03, " -0", "%5.f"}, + {__LINE__, -1.650100957771182e+21, "-1650100957771182308420.065965", "%+f"}, + {__LINE__, -1.650957176011913e-02, "-0.016510", "%#4.5G"}, + {__LINE__, -1.651165108095301e+29, "-165116510809530137038336761179.380119", "%f"}, + {__LINE__, -1.657132845094847e-06, "-0.000002", "%f"}, + {__LINE__, -1.657152146172541e+12, "-1.657152E+12", "%.7G"}, + {__LINE__, -1.658377345516132e-06, "-0.00", "%.2f"}, + {__LINE__, -1.658850468902324e+20, "-1.659e+20", "%.4g"}, + {__LINE__, -1.661723557149830e+18, "-1.66172E+18", "%G"}, + {__LINE__, -1.663570885140273e+14, "-166357088514027", "%2.f"}, + {__LINE__, -1.665186944896906e+25, "-1.66519E+25", "%G"}, + {__LINE__, -1.675910137717036e+22, "-2e+22", "%5.g"}, + {__LINE__, -1.677447987675587e-15, "-1.67745E-15", "%G"}, + {__LINE__, -1.677510590697220e+06, "-1677510.590697", "%5f"}, + {__LINE__, -1.677658141289510e-18, "-2E-18", "%+5.G"}, + {__LINE__, -1.678316848066192e-03, "-1.678317E-03", "%E"}, + {__LINE__, -1.681506404024821e+24, "-1.68151E+24", "%G"}, + {__LINE__, -1.688584243722160e+12, "-1.68858E+12", "%+#G"}, + {__LINE__, -1.689468295411408e+28, "-1.68947E+28", "%+G"}, + {__LINE__, -1.690746767470207e-11, "-1.69075E-11", "%G"}, + {__LINE__, -1.692773960755248e-14, "-2E-14", "%4.E"}, + {__LINE__, -1.693349092740995e-18, "-1.69335e-18", "%g"}, + {__LINE__, -1.693642321808920e-26, "-1.69364E-26", "%G"}, + {__LINE__, -1.694579128700042e-12, "-0.000000", "%f"}, + {__LINE__, -1.695454897264717e-08, "-1.7E-08", "%+4.3G"}, + {__LINE__, -1.695733278397589e+19, "-1.695733e+19", "%4e"}, + {__LINE__, -1.699522534409388e-05, "-1.69952E-05", "%G"}, + {__LINE__, -1.701752039544919e+00, "-2", "%.0f"}, + {__LINE__, -1.702304998615036e+16, "-1.7023E+16", "%1G"}, + {__LINE__, -1.703113580904556e-25, "-1.703114e-25", "%+e"}, + {__LINE__, -1.705165515454546e+29, "-1.705166e+29", "%+e"}, + {__LINE__, -1.707392568277288e-05, "-1.70739E-05", "%G"}, + {__LINE__, -1.707477319256742e+09, "-1707477319.26", "%7.2f"}, + {__LINE__, -1.709669107513969e-03, " -0.002", "%7.g"}, + {__LINE__, -1.713284011412432e-24, "-1.713e-24", "%3.3e"}, + {__LINE__, -1.715512203951086e-28, "-1.715512E-28", "%E"}, + {__LINE__, -1.716880832248879e+13, "-1.71688e+13", "%+g"}, + {__LINE__, -1.720722835740680e-15, "-1.7E-15", "%.1E"}, + {__LINE__, -1.721855769574895e+07, "-17218557.695749", "%3f"}, + {__LINE__, -1.722449437415368e+01, "-1.722449e+01", "%e"}, + {__LINE__, -1.723218805116591e-29, "-2e-29", "%.0g"}, + {__LINE__, -1.726129004920195e-05, "-1.72613E-05", "%0G"}, + {__LINE__, -1.727400228037571e-28, "-0.0000000", "%2.7f"}, + {__LINE__, -1.730008745782339e-23, "-1.73e-23", "%.4g"}, + {__LINE__, -1.731875670201859e+09, "-1.73188E+09", "%G"}, + {__LINE__, -1.732142976428085e+10, "-2E+10", "%+6.0E"}, + {__LINE__, -1.732699651229194e-02, "-0.017327", "%5g"}, + {__LINE__, -1.734772870736446e-12, "-0.000000", "%+f"}, + {__LINE__, -1.738576887938331e+09, "-1.73858e+09", "%g"}, + {__LINE__, -1.738759937912438e-08, "-1.73876e-08", "%+2g"}, + {__LINE__, -1.739320154069143e-18, "-0.0000", "%1.4f"}, + {__LINE__, -1.739870415800800e+16, "-1.73987e+16", "%g"}, + {__LINE__, -1.741290506928618e+04, "-17412.9", "%G"}, + {__LINE__, -1.742520800031913e+29, "-1.742521e+29", "%7e"}, + {__LINE__, -1.747620095195231e+01, "-2e+01", "%+2.g"}, + {__LINE__, -1.749860675924882e-21, "-1.74986E-21", "%G"}, + {__LINE__, -1.752675363661431e-06, "-1.7527E-06", "%.5G"}, + {__LINE__, -1.752871508059699e-06, " -0", "%7.f"}, + {__LINE__, -1.755178530989839e+25, "-1.7551785e+25", "%1.7e"}, + {__LINE__, -1.755566213249512e-07, "-1.755566e-07", "%.6e"}, + {__LINE__, -1.756193485005071e-24, "-1.75619E-24", "%+2.6G"}, + {__LINE__, -1.758501410496218e+03, "-1758.501", "%.7G"}, + {__LINE__, -1.758795617219102e+20, "-1.75880E+20", "%#6G"}, + {__LINE__, -1.760538679276709e+03, "-2e+03", "%+1.e"}, + {__LINE__, -1.762660914833003e+27, "-1.76266E+27", "%G"}, + {__LINE__, -1.765122691141907e+19, "-17651226911419071186.1", "%5.1f"}, + {__LINE__, -1.765313277389086e-18, "-2E-18", "%+1.E"}, + {__LINE__, -1.765821717148734e+19, "-1.76582E+19", "%G"}, + {__LINE__, -1.767048687863149e-17, "-1.77E-17", "%+.3G"}, + {__LINE__, -1.768661645451962e+18, "-1.77E+18", "%+.3G"}, + {__LINE__, -1.769753257452233e+01, "-17.6975", "%G"}, + {__LINE__, -1.770945665065531e+28, "-1.770946E+28", "%0.7G"}, + {__LINE__, -1.776713865753894e-09, "-2e-09", "%1.g"}, + {__LINE__, -1.778424845787448e+03, "-1.7784E+03", "%.4E"}, + {__LINE__, -1.779060610701250e+06, "-1.779061E+06", "%E"}, + {__LINE__, -1.781447182110762e-27, "-1.781447E-27", "%E"}, + {__LINE__, -1.782655842123784e-13, "-2.E-13", "%#2.1G"}, + {__LINE__, -1.783071018169166e+16, "-17830710181691664.217851", "%+f"}, + {__LINE__, -1.784665985294415e+25, "-2E+25", "%3.G"}, + {__LINE__, -1.787297600658096e+25, "-2E+25", "%+5.E"}, + {__LINE__, -1.788200250255718e+12, "-1.7882E+12", "%2.4E"}, + {__LINE__, -1.792860730579114e-09, "-1.79286E-09", "%G"}, + {__LINE__, -1.793122797100936e+24, "-1.793123e+24", "%e"}, + {__LINE__, -1.793761706915029e-25, "-0.000000", "%f"}, + {__LINE__, -1.793947567431932e+22, "-1.79395e+22", "%g"}, + {__LINE__, -1.796428035404303e-18, "-1.79643e-18", "%g"}, + {__LINE__, -1.797113144273918e-16, "-1.79711E-16", "%.6G"}, + {__LINE__, -1.798796767828424e-29, "-1.7988E-29", "%G"}, + {__LINE__, -1.805004010633763e-11, "-1.805e-11", "%2.5g"}, + {__LINE__, -1.806936269752338e-26, "-1.80694e-26", "%g"}, + {__LINE__, -1.807122541760172e-10, "-1.80712e-10", "%g"}, + {__LINE__, -1.808295407815630e-06, "-1.8083E-06", "%+G"}, + {__LINE__, -1.813893236685959e+15, "-1813893236685959", "%0.f"}, + {__LINE__, -1.816201530145367e+06, "-1816202", "%+2.f"}, + {__LINE__, -1.822811219123512e+13, "-1.82281e+13", "%g"}, + {__LINE__, -1.826276499170243e-25, "-2E-25", "%+3.G"}, + {__LINE__, -1.832399287433839e-26, "-1.832399E-26", "%#4E"}, + {__LINE__, -1.833597815584463e-18, "-0.000000", "%+3f"}, + {__LINE__, -1.834165532712233e+28, "-1.834166E+28", "%.7G"}, + {__LINE__, -1.837633147831083e+28, "-1.8376331e+28", "%.7e"}, + {__LINE__, -1.839756716742518e-01, "-0.2", "%+0.G"}, + {__LINE__, -1.840101206950368e-14, "-1.840101E-14", "%+E"}, + {__LINE__, -1.842043645474877e+17, "-1.84204e+17", "%+g"}, + {__LINE__, -1.842155892969934e+17, "-1.84216e+17", "%+g"}, + {__LINE__, -1.843566073012842e-25, "-1.84357E-25", "%G"}, + {__LINE__, -1.843976321320497e-02, "-0.02", "%+4.2f"}, + {__LINE__, -1.845299931651554e+11, "-1.8453E+11", "%0.4E"}, + {__LINE__, -1.846199038659889e-06, "-1.8462e-06", "%0.5g"}, + {__LINE__, -1.847062180184169e-30, "-1.847062E-30", "%5E"}, + {__LINE__, -1.847962158722201e-16, "-0.000000", "%f"}, + {__LINE__, -1.849446376756582e+15, "-1.8E+15", "%2.2G"}, + {__LINE__, -1.853168465523878e-24, "-2E-24", "%1.E"}, + {__LINE__, -1.853601367230139e+10, "-1.853601e+10", "%e"}, + {__LINE__, -1.857367903775364e+18, "-1.86E+18", "%1.3G"}, + {__LINE__, -1.858332820633906e-05, "-0.000019", "%7f"}, + {__LINE__, -1.860243811657223e-23, "-1.860244e-23", "%e"}, + {__LINE__, -1.860660612539794e+08, "-1.86066e+08", "%+6.5e"}, + {__LINE__, -1.861160816251124e-09, "-2E-09", "%1.G"}, + {__LINE__, -1.862380636974688e-25, "-1.9E-25", "%.2G"}, + {__LINE__, -1.864168808453004e+15, "-1.86417E+15", "%G"}, + {__LINE__, -1.864273144411246e+07, "-1.86427e+07", "%g"}, + {__LINE__, -1.864929236615802e-02, "-2e-02", "%+3.e"}, + {__LINE__, -1.865010503480847e+19, "-2E+19", "%4.G"}, + {__LINE__, -1.866276374553144e+22, "-1.866276e+22", "%e"}, + {__LINE__, -1.870809567910649e+09, "-1.870810e+09", "%e"}, + {__LINE__, -1.872555495839008e-13, "-1.872555E-13", "%3E"}, + {__LINE__, -1.874465717110388e-22, "-1.874466E-22", "%E"}, + {__LINE__, -1.874916306627632e+21, "-1874916306627632422987.517202", "%f"}, + {__LINE__, -1.875804322194491e-23, "-0.000000", "%+f"}, + {__LINE__, -1.876662099198587e-04, "-0.000187666", "%G"}, + {__LINE__, -1.876775504795760e-25, "-1.87678e-25", "%0g"}, + {__LINE__, -1.879343051002554e-20, " -0.", "%#6.f"}, + {__LINE__, -1.881535445774717e-15, "-1.881535e-15", "%e"}, + {__LINE__, -1.887515901404300e+00, "-1.888", "%2.4G"}, + {__LINE__, -1.887730637149009e+17, "-1.887731E+17", "%E"}, + {__LINE__, -1.889920303480086e+17, "-1.889920e+17", "%+e"}, + {__LINE__, -1.891903478784091e+04, "-1.891903e+04", "%e"}, + {__LINE__, -1.893550084305833e+16, "-1.89355e+16", "%g"}, + {__LINE__, -1.894675230197676e+05, "-1.894675e+05", "%#e"}, + {__LINE__, -1.898389624953187e-27, "-1.898390e-27", "%+e"}, + {__LINE__, -1.899250044644046e+21, "-1899250044644046120367.875119", "%+4.6f"}, + {__LINE__, -1.904187609804679e-02, "-1.90419E-02", "%0.5E"}, + {__LINE__, -1.904452538421193e-04, "-2e-04", "%4.0e"}, + {__LINE__, -1.904615326969061e-16, "-1.904615e-16", "%1.7g"}, + {__LINE__, -1.907761255064750e+02, "-190.776126", "%f"}, + {__LINE__, -1.910727641826707e-10, " -2e-10", "%7.g"}, + {__LINE__, -1.913149279262051e+15, "-1.913149e+15", "%.7g"}, + {__LINE__, -1.913235739298009e+28, "-1.913236e+28", "%e"}, + {__LINE__, -1.913526822735271e+18, "-1.914e+18", "%.4g"}, + {__LINE__, -1.913780977515583e+17, "-191378097751558335.9150", "%5.4f"}, + {__LINE__, -1.917095456962182e-10, "-2E-10", "%4.G"}, + {__LINE__, -1.918803033972851e+23, "-191880303397285132405158.947222", "%f"}, + {__LINE__, -1.926420984801848e+16, "-19264209848018483.128840", "%+f"}, + {__LINE__, -1.931905465942639e-10, "-1.93191e-10", "%g"}, + {__LINE__, -1.932907105840252e+06, "-1.932907e+06", "%+#e"}, + {__LINE__, -1.933091601918916e+15, "-1.933092E+15", "%#E"}, + {__LINE__, -1.934296184983361e+09, "-1.9343e+09", "%1.6g"}, + {__LINE__, -1.934637311832448e+11, "-1.93464e+11", "%+g"}, + {__LINE__, -1.936201483262186e+12, "-2e+12", "%+6.g"}, + {__LINE__, -1.939114661603911e+19, "-1.93911E+19", "%G"}, + {__LINE__, -1.940478182124347e-13, "-0.000000", "%f"}, + {__LINE__, -1.943218220654923e+08, "-2E+08", "%+5.0G"}, + {__LINE__, -1.943526872455779e-30, "-1.9E-30", "%.1E"}, + {__LINE__, -1.949869164681357e+19, "-1.949869e+19", "%#3e"}, + {__LINE__, -1.954181060535402e+23, "-1.954181e+23", "%e"}, + {__LINE__, -1.961581555579142e+14, "-1.962E+14", "%.3E"}, + {__LINE__, -1.964535562036915e+07, "-1.96454E+07", "%G"}, + {__LINE__, -1.969749653575926e+04, "-19697.5", "%.6g"}, + {__LINE__, -1.973475369169053e+15, "-1.97348e+15", "%g"}, + {__LINE__, -1.975658532866600e-21, "-2e-21", "%+1.2g"}, + {__LINE__, -1.984050343831260e+09, "-1.984050E+09", "%E"}, + {__LINE__, -1.984422410018571e+12, "-1.984422e+12", "%+4.7g"}, + {__LINE__, -1.984878687667223e-22, "-0.000000", "%f"}, + {__LINE__, -1.986116930967811e-28, "-1.98612e-28", "%g"}, + {__LINE__, -1.986774457812683e+02, "-1.986774e+02", "%e"}, + {__LINE__, -1.987274814938726e-04, "-0.000198727", "%g"}, + {__LINE__, -1.987319200580415e+10, "-1.987319E+10", "%E"}, + {__LINE__, -1.987980768698114e-19, "-0.000000", "%3f"}, + {__LINE__, -1.994698965044602e-29, "-2E-29", "%+4.0G"}, + {__LINE__, -1.997833122667050e+26, "-1.99783E+26", "%G"}, + {__LINE__, -1.999453681184129e-10, "-0.00000", "%.5f"}, + {__LINE__, -1.999897345839745e-16, "-1.9999e-16", "%g"}, + {__LINE__, -2.003703825123989e-22, "-0.000000", "%f"}, + {__LINE__, -2.004569188274957e-23, "-2.00457E-23", "%G"}, + {__LINE__, -2.005911883189058e+07, "-2.00591E+07", "%G"}, + {__LINE__, -2.006438312005722e-25, "-2.006e-25", "%0.4g"}, + {__LINE__, -2.017322171210280e+18, "-2.01732E+18", "%+0G"}, + {__LINE__, -2.017718635819594e-06, "-2.01772e-06", "%5g"}, + {__LINE__, -2.019187445568160e-12, "-0.00", "%.2f"}, + {__LINE__, -2.021022665608503e+25, "-20210226656085028551501636.684", "%2.3f"}, + {__LINE__, -2.022720393474145e+07, "-2.02272E+07", "%#G"}, + {__LINE__, -2.024506694471065e+23, "-2.02451E+23", "%G"}, + {__LINE__, -2.024884686553821e-15, " -2e-15", "%7.g"}, + {__LINE__, -2.027484636128966e-23, "-2.0275E-23", "%.5G"}, + {__LINE__, -2.028185530467237e+21, "-2028185530467237374097.067", "%+4.3f"}, + {__LINE__, -2.028388905566873e-09, "-0.000000", "%f"}, + {__LINE__, -2.029856507431584e-17, "-2.02986E-17", "%G"}, + {__LINE__, -2.029891733449167e+27, "-2029891733449166846270372843.7190875", "%3.7f"}, + {__LINE__, -2.031933616694469e+14, "-2.031934E+14", "%#E"}, + {__LINE__, -2.034011738471413e-10, "-2.03401E-10", "%#G"}, + {__LINE__, -2.036087355975756e+29, "-203608735597575574161055556032.806635", "%+f"}, + {__LINE__, -2.036355025137273e+09, "-2.036355E+09", "%#.7G"}, + {__LINE__, -2.038423730536878e+12, "-2E+12", "%4.G"}, + {__LINE__, -2.045209202278810e+23, "-2.045209e+23", "%+#e"}, + {__LINE__, -2.046794083517423e-06, "-2.04679e-06", "%g"}, + {__LINE__, -2.048042459678599e-19, "-0.000000", "%f"}, + {__LINE__, -2.050526462096153e+18, "-2.05053e+18", "%+g"}, + {__LINE__, -2.053583900249402e+13, "-2.053584e+13", "%0.7g"}, + {__LINE__, -2.054342100957522e-23, "-2.05434E-23", "%#G"}, + {__LINE__, -2.054793400141025e+11, "-205479340014.1", "%+2.1f"}, + {__LINE__, -2.059200689196498e-11, "-0.000000", "%#f"}, + {__LINE__, -2.060960692728114e-16, "-0.000000", "%+f"}, + {__LINE__, -2.061417528654300e-30, "-2.06E-30", "%.3G"}, + {__LINE__, -2.061513401198340e-29, "-2.06151e-29", "%7g"}, + {__LINE__, -2.061846698845984e+27, "-2.061847E+27", "%E"}, + {__LINE__, -2.063922242876789e-19, "-2.06392e-19", "%#g"}, + {__LINE__, -2.065359431805529e+03, "-2065.359432", "%f"}, + {__LINE__, -2.065529069992156e-04, "-0.000206553", "%+G"}, + {__LINE__, -2.066082546490980e-25, "-2.06608e-25", "%5g"}, + {__LINE__, -2.068394312095200e+17, "-2.068394e+17", "%e"}, + {__LINE__, -2.071024178556425e-10, "-2.07102E-10", "%G"}, + {__LINE__, -2.074401605950382e-22, "-2.07e-22", "%1.3g"}, + {__LINE__, -2.074731895144874e-26, "-2.07473E-26", "%G"}, + {__LINE__, -2.074895800882730e+18, "-2074895800882730068.082083", "%+f"}, + {__LINE__, -2.077336220500764e-30, "-0", "%2.f"}, + {__LINE__, -2.078067878561077e+09, "-2.0781e+09", "%0.5g"}, + {__LINE__, -2.080497889634533e+15, "-2.080498E+15", "%4E"}, + {__LINE__, -2.084717279367594e-21, "-2.08472e-21", "%+g"}, + {__LINE__, -2.087827915301948e+29, "-2E+29", "%6.1G"}, + {__LINE__, -2.090268686389680e+05, "-2.090269e+05", "%1e"}, + {__LINE__, -2.092036176589720e+24, "-2.09204e+24", "%+g"}, + {__LINE__, -2.093546373938615e-21, "-2.093546e-21", "%#e"}, + {__LINE__, -2.101302069316682e+18, "-2.1013e+18", "%+7g"}, + {__LINE__, -2.103463622286002e-13, "-2.103464E-13", "%E"}, + {__LINE__, -2.105622845016732e+06, "-2105622.845017", "%1f"}, + {__LINE__, -2.109999123037767e+28, "-2.11E+28", "%.2E"}, + {__LINE__, -2.110317013992166e+04, "-21103.2", "%+g"}, + {__LINE__, -2.112226142154618e+21, "-2112226142154617569702.", "%#6.f"}, + {__LINE__, -2.112519484486528e+06, "-2.11252E+06", "%G"}, + {__LINE__, -2.113439260812000e+23, "-2E+23", "%1.G"}, + {__LINE__, -2.113465893194608e-15, "-2.1135E-15", "%.5G"}, + {__LINE__, -2.113547784669409e+20, "-2.1135e+20", "%2.5g"}, + {__LINE__, -2.114146001321822e+28, "-2.114146e+28", "%e"}, + {__LINE__, -2.114509425574444e-30, " -0", "%4.f"}, + {__LINE__, -2.114887421659561e-21, "-0.000000", "%+f"}, + {__LINE__, -2.121120215127601e+21, "-2121120215127601404958.5683097", "%.7f"}, + {__LINE__, -2.122555390665796e+06, "-2E+06", "%0.G"}, + {__LINE__, -2.123468430242052e-27, "-0.000000", "%+f"}, + {__LINE__, -2.123473598890635e+28, "-2.12347e+28", "%+g"}, + {__LINE__, -2.124328049748190e-04, "-2.124328E-04", "%E"}, + {__LINE__, -2.125633890999010e+28, "-2E+28", "%+5.G"}, + {__LINE__, -2.125697963566045e+21, "-2.1257E+21", "%0G"}, + {__LINE__, -2.127638109230109e+06, "-2.12764e+06", "%+6g"}, + {__LINE__, -2.128456114617786e+07, "-2.128456E+07", "%E"}, + {__LINE__, -2.128732742932824e-04, "-2.128733E-04", "%#E"}, + {__LINE__, -2.129654381588404e+27, "-2129654381588404365999167566.7697564", "%+.7f"}, + {__LINE__, -2.129792795009985e+20, "-2.12979e+20", "%g"}, + {__LINE__, -2.131026925664619e-20, "-2e-20", "%.0e"}, + {__LINE__, -2.132860730144188e-04, "-0.0002", "%5.g"}, + {__LINE__, -2.133620979309562e+06, "-2e+06", "%3.e"}, + {__LINE__, -2.133821423139120e-27, "-2.13382E-27", "%.6G"}, + {__LINE__, -2.134253401425940e-07, "-0.000000", "%f"}, + {__LINE__, -2.135469568156964e-29, "-2.135470E-29", "%+7E"}, + {__LINE__, -2.135723595288365e-25, "-0.0", "%.1f"}, + {__LINE__, -2.137642458920094e-09, "-2.13764e-09", "%g"}, + {__LINE__, -2.143412361116583e+12, "-2.14341e+12", "%+g"}, + {__LINE__, -2.144467087741129e-17, "-2.14447e-17", "%+g"}, + {__LINE__, -2.145726966596964e-11, "-0.000000", "%2f"}, + {__LINE__, -2.148318753042597e+08, "-2.14832E+08", "%5G"}, + {__LINE__, -2.149932310549644e-19, "-2.14993e-19", "%g"}, + {__LINE__, -2.150050917377646e-21, " -0", "%3.f"}, + {__LINE__, -2.150854313643430e-16, "-0.000000", "%.6f"}, + {__LINE__, -2.151071797121845e+00, "-2.15107", "%+g"}, + {__LINE__, -2.158728406865981e+14, "-215872840686598.088666", "%7f"}, + {__LINE__, -2.166062878011641e-23, "-2.166063e-23", "%e"}, + {__LINE__, -2.167045002060684e+12, "-2.167045E+12", "%E"}, + {__LINE__, -2.173752581051530e+20, "-217375258105153035259.604621", "%f"}, + {__LINE__, -2.175599129057555e+24, "-2.175599E+24", "%E"}, + {__LINE__, -2.176564454093042e-02, "-0.0217656", "%+G"}, + {__LINE__, -2.181270774327071e-12, "-0.000000", "%#1f"}, + {__LINE__, -2.184404769844158e-09, "-2.2E-09", "%+.2G"}, + {__LINE__, -2.184881476334310e-07, "-2.18488e-07", "%g"}, + {__LINE__, -2.189197211449684e-11, "-2E-11", "%.0G"}, + {__LINE__, -2.189451286805190e+20, "-218945128680519029984", "%3.f"}, + {__LINE__, -2.190642494146884e-27, " -0.", "%#4.f"}, + {__LINE__, -2.194727956725854e+06, "-2.19E+06", "%.3G"}, + {__LINE__, -2.196145868303877e+11, "-219614586830.387726", "%+5f"}, + {__LINE__, -2.196397972262690e-16, "-0.000000", "%3f"}, + {__LINE__, -2.202692843474668e-01, "-2.203e-01", "%.3e"}, + {__LINE__, -2.204528684782451e-21, "-2.20453E-21", "%G"}, + {__LINE__, -2.214926838821340e-16, "-2e-16", "%+2.g"}, + {__LINE__, -2.215558986352773e+18, "-2215558986352773374", "%0.f"}, + {__LINE__, -2.216976693056186e+27, "-2216976693056186498154147557.215765", "%f"}, + {__LINE__, -2.217348422793322e+13, "-2.21735E+13", "%6G"}, + {__LINE__, -2.217732695583920e-07, "-0", "%.0f"}, + {__LINE__, -2.221185494767834e-14, "-2.22119e-14", "%g"}, + {__LINE__, -2.224557193699609e-16, "-2.224557E-16", "%E"}, + {__LINE__, -2.227417758704028e-18, "-0.000000", "%0f"}, + {__LINE__, -2.231261655673006e-30, "-2.231262E-30", "%E"}, + {__LINE__, -2.233081938836160e+21, "-2233081938836160240668.960", "%+6.3f"}, + {__LINE__, -2.233158918439853e+05, "-223316", "%2G"}, + {__LINE__, -2.233802298007898e-30, "-2.2338E-30", "%2G"}, + {__LINE__, -2.233864858404732e+17, "-223386485840473181.241350", "%f"}, + {__LINE__, -2.241242805107749e+14, "-2.24124E+14", "%G"}, + {__LINE__, -2.244555498855249e+25, "-2.24456E+25", "%#G"}, + {__LINE__, -2.248170665127481e-07, "-2.248171E-07", "%E"}, + {__LINE__, -2.252558275567388e-13, "-2.25256e-13", "%g"}, + {__LINE__, -2.256190083496001e+13, "-22561900834960.0136398", "%7.7f"}, + {__LINE__, -2.259851596715945e+27, "-2.259852e+27", "%e"}, + {__LINE__, -2.260461480001174e-21, "-0.000000", "%f"}, + {__LINE__, -2.264128628428577e+22, "-2.26413e+22", "%1g"}, + {__LINE__, -2.265914518399595e-04, "-0.000226591", "%G"}, + {__LINE__, -2.266251557092826e+23, "-2.26625e+23", "%g"}, + {__LINE__, -2.268592252572450e+19, "-2.268592e+19", "%2.6e"}, + {__LINE__, -2.268597523847349e+11, "-2.2686e+11", "%g"}, + {__LINE__, -2.268963106935546e+05, "-226896", "%+g"}, + {__LINE__, -2.272527876808919e-24, "-0.000000", "%#f"}, + {__LINE__, -2.273366440479073e+02, "-227.337", "%g"}, + {__LINE__, -2.277858038556191e+25, "-2.28E+25", "%0.3G"}, + {__LINE__, -2.282019915623415e+25, "-2E+25", "%5.E"}, + {__LINE__, -2.283333247435650e-27, " -0", "%4.f"}, + {__LINE__, -2.285230610829355e+23, "-2E+23", "%2.G"}, + {__LINE__, -2.290900253402985e+23, "-2.2909E+23", "%G"}, + {__LINE__, -2.293266953268186e+08, "-2.29327e+08", "%g"}, + {__LINE__, -2.293489603164786e+23, "-229348960316478578391752.3076525", "%4.7f"}, + {__LINE__, -2.294434667605481e+15, "-2.2944E+15", "%.5G"}, + {__LINE__, -2.296607768466765e+21, "-2.29661E+21", "%G"}, + {__LINE__, -2.297449967994012e+14, "-229744996799401.23542", "%+.5f"}, + {__LINE__, -2.301162556466583e-01, "-0.230116", "%+#2g"}, + {__LINE__, -2.301676246374004e+05, "-230168.", "%#g"}, + {__LINE__, -2.301976724487835e-01, "-0.230198", "%5G"}, + {__LINE__, -2.303269770571222e-15, "-2.303270e-15", "%.6e"}, + {__LINE__, -2.306451480495636e+23, "-2.30645E+23", "%G"}, + {__LINE__, -2.307659351219690e-17, "-0.000000", "%+3f"}, + {__LINE__, -2.309009762339430e+16, "-2.30901E+16", "%G"}, + {__LINE__, -2.309261751862100e+06, "-2309261.751862", "%+f"}, + {__LINE__, -2.309504876918634e-26, " -2E-26", "%7.E"}, + {__LINE__, -2.310910348147804e-26, "-2e-26", "%5.g"}, + {__LINE__, -2.317173552252235e+05, "-231717.4", "%.7g"}, + {__LINE__, -2.319517289618469e-23, "-2.31952E-23", "%+G"}, + {__LINE__, -2.320103195142527e+08, "-2.320103E+08", "%+E"}, + {__LINE__, -2.323523032125633e+24, "-2323523032125633491773442.07419", "%.5f"}, + {__LINE__, -2.326279126614399e-16, "-0.000000", "%f"}, + {__LINE__, -2.331393688620925e-09, "-2.33139e-09", "%#g"}, + {__LINE__, -2.334233527512469e+01, " -23", "%6.0f"}, + {__LINE__, -2.337418834112767e+24, " -2E+24", "%7.E"}, + {__LINE__, -2.339455293612118e+12, "-2339455293612.1", "%+.1f"}, + {__LINE__, -2.341930318143367e-18, "-2.E-18", "%#4.E"}, + {__LINE__, -2.346107614950921e-17, "-0.00000", "%+2.5f"}, + {__LINE__, -2.348356578807972e+23, "-2.34836e+23", "%g"}, + {__LINE__, -2.351485855138244e+19, "-2.351486e+19", "%+e"}, + {__LINE__, -2.362101647639198e-15, "-2.36210e-15", "%#g"}, + {__LINE__, -2.369743873030115e+08, "-2e+08", "%.1g"}, + {__LINE__, -2.371658164739356e+25, "-23716581647393559784120498.079574", "%f"}, + {__LINE__, -2.372427847607163e+28, "-2.37243E+28", "%G"}, + {__LINE__, -2.375465465294647e+11, "-2.375465e+11", "%e"}, + {__LINE__, -2.377128971572195e-18, "-0.0", "%1.1f"}, + {__LINE__, -2.380322448568089e-13, "-2.38032E-13", "%+#0G"}, + {__LINE__, -2.385960728264882e+26, "-2E+26", "%.0E"}, + {__LINE__, -2.387345744004747e+00, "-2.38735", "%G"}, + {__LINE__, -2.390480023300201e+25, "-2.39048e+25", "%g"}, + {__LINE__, -2.395420431930886e+23, "-2E+23", "%2.E"}, + {__LINE__, -2.401144663870969e-10, "-2.e-10", "%#6.g"}, + {__LINE__, -2.402787441215039e-08, "-2.40279e-08", "%g"}, + {__LINE__, -2.402990830425278e+08, "-2.40299E+08", "%G"}, + {__LINE__, -2.404213682932215e+08, "-2.40421e+08", "%2g"}, + {__LINE__, -2.409385592298822e+21, "-2409385592298821876511.283335", "%7f"}, + {__LINE__, -2.410798267658614e+13, "-2.4108e+13", "%+g"}, + {__LINE__, -2.413180068271811e-24, "-0.000000", "%+f"}, + {__LINE__, -2.413268457113495e+26, "-2.41327E+26", "%3.6G"}, + {__LINE__, -2.415341832206007e-13, "-0.000000", "%+f"}, + {__LINE__, -2.422519577068670e-08, "-0.0000000", "%.7f"}, + {__LINE__, -2.422533651282808e+20, "-2.42253E+20", "%#G"}, + {__LINE__, -2.422795006880671e+17, "-2.4228e+17", "%g"}, + {__LINE__, -2.423483330202008e+20, "-2.42348e+20", "%g"}, + {__LINE__, -2.431756009640369e-04, "-0.000243176", "%+#3g"}, + {__LINE__, -2.431878622534173e-05, "-0.000024", "%f"}, + {__LINE__, -2.432012113077180e+24, "-2.43201e+24", "%g"}, + {__LINE__, -2.433843704470487e+07, "-24338437", "%4.f"}, + {__LINE__, -2.435475755031956e+11, "-2.435476E+11", "%1E"}, + {__LINE__, -2.438081888695437e-13, "-0.000000", "%f"}, + {__LINE__, -2.438334590462860e+10, "-2.4E+10", "%5.2G"}, + {__LINE__, -2.440223135334059e+17, "-2.44022e+17", "%g"}, + {__LINE__, -2.444107281115317e-21, "-2.44411e-21", "%+g"}, + {__LINE__, -2.444582273206171e-02, " -0.02", "%7.G"}, + {__LINE__, -2.445234676352794e+23, "-2.44523e+23", "%#g"}, + {__LINE__, -2.449185020984714e+08, "-2.E+08", "%#2.G"}, + {__LINE__, -2.450646640466020e+22, "-2.45065E+22", "%5G"}, + {__LINE__, -2.456840850262473e+13, "-24568408502625", "%0.f"}, + {__LINE__, -2.464466865714499e-10, "-2e-10", "%6.g"}, + {__LINE__, -2.466022088999151e+27, "-2.46602E+27", "%G"}, + {__LINE__, -2.484398113119779e-04, "-2.484398E-04", "%E"}, + {__LINE__, -2.484591850456328e+23, "-248459185045632846566832.085955", "%+f"}, + {__LINE__, -2.488166252162446e-11, "-2.5E-11", "%4.2G"}, + {__LINE__, -2.493507313049390e+24, "-2.49E+24", "%4.3G"}, + {__LINE__, -2.500280281988428e+28, "-2.5E+28", "%.4G"}, + {__LINE__, -2.500373956445372e-07, "-2.50037e-07", "%g"}, + {__LINE__, -2.505566434769299e-12, "-2.5E-12", "%+#0.2G"}, + {__LINE__, -2.508215917020758e+05, "-2.508216e+05", "%5e"}, + {__LINE__, -2.519368094680315e-29, "-2.51937e-29", "%#0g"}, + {__LINE__, -2.522266530587753e-11, "-2.52227e-11", "%+g"}, + {__LINE__, -2.534492886372514e-06, "-2.53449E-06", "%0G"}, + {__LINE__, -2.534617610955074e-02, "-0.03", "%0.2f"}, + {__LINE__, -2.540551642835634e+26, "-3E+26", "%4.G"}, + {__LINE__, -2.548210731830516e-26, "-0.000", "%2.3f"}, + {__LINE__, -2.558868082397931e-11, "-3E-11", "%2.E"}, + {__LINE__, -2.560062512991052e+11, "-2.56006E+11", "%+G"}, + {__LINE__, -2.561274682814428e-05, "-0.000026", "%f"}, + {__LINE__, -2.563424949397357e-02, " -0", "%6.f"}, + {__LINE__, -2.577010939931465e+17, "-2.57701E+17", "%G"}, + {__LINE__, -2.578224879294822e+10, "-3E+10", "%+4.1G"}, + {__LINE__, -2.585655636750244e+13, "-2.585656e+13", "%+0.7g"}, + {__LINE__, -2.585657986834408e+14, "-2.5857e+14", "%3.5g"}, + {__LINE__, -2.586728929368101e+02, "-258.673", "%+g"}, + {__LINE__, -2.591293023229468e+06, "-2591293.0232295", "%2.7f"}, + {__LINE__, -2.592454222603538e-06, "-2.59245E-06", "%G"}, + {__LINE__, -2.599399905869649e+02, "-259.939991", "%f"}, + {__LINE__, -2.605382860307596e+12, "-2.60538E+12", "%2G"}, + {__LINE__, -2.607507689402762e-01, "-0.260751", "%#.6g"}, + {__LINE__, -2.612933252582967e+15, "-2.61293e+15", "%g"}, + {__LINE__, -2.614337505491483e-14, "-2.61434E-14", "%.6G"}, + {__LINE__, -2.618302263333671e+14, "-2.62e+14", "%3.3g"}, + {__LINE__, -2.622998533972296e+00, "-3.", "%#0.g"}, + {__LINE__, -2.623348319869643e-15, "-2.62335E-15", "%#G"}, + {__LINE__, -2.626703132693163e+23, "-3E+23", "%+6.E"}, + {__LINE__, -2.629610853765779e+25, "-2.62961E+25", "%+#G"}, + {__LINE__, -2.632993880472784e+26, "-2.63299E+26", "%G"}, + {__LINE__, -2.635651112327873e-20, "-2.63565E-20", "%1G"}, + {__LINE__, -2.636003980473492e-23, "-2.636E-23", "%G"}, + {__LINE__, -2.639316453244009e+10, "-26393164532.440094", "%#f"}, + {__LINE__, -2.640517223417942e-28, "-2.641e-28", "%.3e"}, + {__LINE__, -2.640625797147664e-22, "-2.640626E-22", "%E"}, + {__LINE__, -2.644401262524378e-12, " -0.000", "%7.3f"}, + {__LINE__, -2.651033203243637e-14, "-2.651033e-14", "%+e"}, + {__LINE__, -2.652386470126594e-24, "-2.652386E-24", "%E"}, + {__LINE__, -2.654281018623631e-12, "-0.00000", "%6.5f"}, + {__LINE__, -2.655529742118827e+02, "-265.552974", "%f"}, + {__LINE__, -2.658550833241620e+22, "-26585508332416196708436.473273", "%+f"}, + {__LINE__, -2.660515890519100e+08, "-2.660516E+08", "%E"}, + {__LINE__, -2.665025749266086e-24, "-2.66503E-24", "%G"}, + {__LINE__, -2.666117105643095e-20, "-2.66612e-20", "%g"}, + {__LINE__, -2.667051347741259e+11, "-3e+11", "%6.g"}, + {__LINE__, -2.667207607243375e+04, "-26672.076072", "%+#f"}, + {__LINE__, -2.667631877167590e+26, "-2.667632E+26", "%E"}, + {__LINE__, -2.673817942962878e+13, "-2.673818e+13", "%+2.6e"}, + {__LINE__, -2.674972907853507e+20, "-2.674973E+20", "%E"}, + {__LINE__, -2.675830887404867e+14, "-2.675831E+14", "%E"}, + {__LINE__, -2.682749918168908e+26, "-2.682750E+26", "%E"}, + {__LINE__, -2.689222801942679e-03, "-0.0027", "%7.2G"}, + {__LINE__, -2.692245629411156e+25, "-2.69225e+25", "%g"}, + {__LINE__, -2.692606702464273e+11, "-2.69261e+11", "%g"}, + {__LINE__, -2.693070822061136e-27, "-2.69307e-27", "%+g"}, + {__LINE__, -2.694415515655336e-30, "-3E-30", "%+3.0E"}, + {__LINE__, -2.694770057384739e+15, "-2.69477e+15", "%g"}, + {__LINE__, -2.699998392329361e+01, "-27", "%+.2g"}, + {__LINE__, -2.700151952281511e+23, "-270015195228151050343968.", "%+#5.f"}, + {__LINE__, -2.707398527302841e-08, "-2.70740e-08", "%+#3g"}, + {__LINE__, -2.711404950850030e-21, "-2.71E-21", "%.2E"}, + {__LINE__, -2.714057739024281e-10, "-2.714e-10", "%.4g"}, + {__LINE__, -2.717657632815414e-10, "-2.71766e-10", "%g"}, + {__LINE__, -2.720875786430592e-09, "-2.7E-09", "%6.2G"}, + {__LINE__, -2.725886730997891e+19, "-2.72589e+19", "%g"}, + {__LINE__, -2.726089169748676e-09, "-2.72609E-09", "%+G"}, + {__LINE__, -2.732907572038661e-30, "-2.73291E-30", "%G"}, + {__LINE__, -2.737918050958789e+11, "-3e+11", "%1.g"}, + {__LINE__, -2.739024251198484e-18, "-2.73902e-18", "%+g"}, + {__LINE__, -2.742646144452305e+13, "-2.74265e+13", "%g"}, + {__LINE__, -2.742911865386719e+06, "-3E+06", "%+5.G"}, + {__LINE__, -2.759159275123811e-30, "-0.000000", "%+f"}, + {__LINE__, -2.759794813930001e-21, "-2.75979e-21", "%+g"}, + {__LINE__, -2.763243077558348e+20, "-2.7632E+20", "%+0.4E"}, + {__LINE__, -2.770348477810209e-14, "-0.000000", "%2f"}, + {__LINE__, -2.776074766292453e+05, "-277607", "%g"}, + {__LINE__, -2.776610811432007e-07, "-2.77661E-07", "%6G"}, + {__LINE__, -2.778673793270678e+23, "-3E+23", "%+4.E"}, + {__LINE__, -2.782405168708350e+08, "-278240516.87084", "%.5f"}, + {__LINE__, -2.783316149365198e-09, "-3E-09", "%5.0G"}, + {__LINE__, -2.785436703085409e-27, "-2.78544E-27", "%#G"}, + {__LINE__, -2.787479051660640e+21, "-2.78748e+21", "%g"}, + {__LINE__, -2.789445406042450e-03, "-0.002789", "%+f"}, + {__LINE__, -2.791104581836077e+05, "-279110.4581836", "%+7.7f"}, + {__LINE__, -2.802078617775784e+04, "-28020.786", "%0.3f"}, + {__LINE__, -2.804954315579055e+20, "-2.80495E+20", "%G"}, + {__LINE__, -2.806575341862696e-05, "-2.80658E-05", "%G"}, + {__LINE__, -2.807769556900402e-06, "-0.000003", "%+#f"}, + {__LINE__, -2.808882056357941e-12, "-0.0", "%.1f"}, + {__LINE__, -2.809386677339924e+10, "-2.8094E+10", "%6.5G"}, + {__LINE__, -2.818404311437694e+19, "-3E+19", "%6.G"}, + {__LINE__, -2.819463285551660e+01, "-28.1946", "%+G"}, + {__LINE__, -2.821428853207724e-16, "-3e-16", "%6.g"}, + {__LINE__, -2.824452750788444e+07, "-3e+07", "%+2.g"}, + {__LINE__, -2.825430381094971e+28, "-3e+28", "%+.0e"}, + {__LINE__, -2.830010928384944e-17, "-0", "%.0f"}, + {__LINE__, -2.832505114479680e-09, "-0.000000", "%f"}, + {__LINE__, -2.833246604950796e+13, "-2.83325E+13", "%G"}, + {__LINE__, -2.833274073265017e+13, "-28332740732650.174564", "%f"}, + {__LINE__, -2.835842581787797e+07, "-2.83584E+07", "%2G"}, + {__LINE__, -2.839758384681983e-07, "-2.83976E-07", "%.6G"}, + {__LINE__, -2.841077022753766e-09, "-3E-09", "%0.1G"}, + {__LINE__, -2.845307294930682e+00, "-2.845307e+00", "%+1e"}, + {__LINE__, -2.847420163874243e+19, "-2.8e+19", "%1.2g"}, + {__LINE__, -2.848133715109881e-25, "-2.848134E-25", "%E"}, + {__LINE__, -2.850208101288058e-06, "-2.8502E-06", "%+5.4E"}, + {__LINE__, -2.853666525870413e+04, "-28536.665259", "%f"}, + {__LINE__, -2.855661543202034e+21, "-2.85566e+21", "%g"}, + {__LINE__, -2.859528889324159e-08, "-2.859529e-08", "%3e"}, + {__LINE__, -2.860545310690251e+29, "-3e+29", "%.1g"}, + {__LINE__, -2.863576633666884e-14, "-2.863577e-14", "%+5e"}, + {__LINE__, -2.864115740396321e-14, "-2.86412e-14", "%g"}, + {__LINE__, -2.864481979037153e+20, "-2.86E+20", "%3.3G"}, + {__LINE__, -2.867582970177984e-18, "-0.000000", "%f"}, + {__LINE__, -2.868237707901564e+25, "-3E+25", "%6.E"}, + {__LINE__, -2.871741071402520e+13, "-3e+13", "%.1g"}, + {__LINE__, -2.873724610073364e+18, "-2873724610073364438.278531", "%+#f"}, + {__LINE__, -2.876433859770866e-25, "-2.87643e-25", "%g"}, + {__LINE__, -2.877458587075737e-08, "-2.877E-08", "%+.4G"}, + {__LINE__, -2.883349842927101e+24, "-2.883350E+24", "%2E"}, + {__LINE__, -2.883749925642885e+09, "-2.883750e+09", "%e"}, + {__LINE__, -2.883773835633003e-17, "-0.000000", "%+f"}, + {__LINE__, -2.890389090491409e+24, "-2890389090491409262995148.310438", "%#1f"}, + {__LINE__, -2.900848552225810e-11, "-0.000000", "%+f"}, + {__LINE__, -2.911942123176400e+25, "-29119421231764004431852300", "%0.f"}, + {__LINE__, -2.912904462180751e-18, "-2.9129E-18", "%6G"}, + {__LINE__, -2.917717875075651e+26, "-3e+26", "%0.e"}, + {__LINE__, -2.922293201084093e-26, "-2.922293E-26", "%E"}, + {__LINE__, -2.925070319932451e-30, "-0.000000", "%f"}, + {__LINE__, -2.926558572870874e+15, "-2.926559E+15", "%E"}, + {__LINE__, -2.928781435877896e+26, "-2.928781e+26", "%+e"}, + {__LINE__, -2.930178318110376e+28, "-2.930178E+28", "%E"}, + {__LINE__, -2.930984661925819e+05, "-293098.5", "%.7G"}, + {__LINE__, -2.932762424932762e-08, "-0.000000", "%#f"}, + {__LINE__, -2.933415597492494e+10, "-29334155974.924943", "%+#2f"}, + {__LINE__, -2.933564314850986e+29, "-2.93356E+29", "%7G"}, + {__LINE__, -2.943224907893795e+10, "-2.943225E+10", "%+7E"}, + {__LINE__, -2.945311540471221e+19, "-2.94531E+19", "%#G"}, + {__LINE__, -2.945812356522847e+15, "-2.94581e+15", "%+g"}, + {__LINE__, -2.945836999630957e+00, "-2.945837E+00", "%E"}, + {__LINE__, -2.947798782726622e-01, "-0.29478", "%+g"}, + {__LINE__, -2.948958405827917e+18, "-2.94896e+18", "%g"}, + {__LINE__, -2.949790871798059e-11, "-2.94979E-11", "%G"}, + {__LINE__, -2.950347806125225e-12, "-2.95035e-12", "%g"}, + {__LINE__, -2.952781884599368e-29, "-2.952782E-29", "%+E"}, + {__LINE__, -2.956801341442716e+06, "-2.9568E+06", "%G"}, + {__LINE__, -2.957469310356540e-07, "-0.000", "%1.3f"}, + {__LINE__, -2.960464869534870e-23, "-2.96046E-23", "%G"}, + {__LINE__, -2.962339381825446e-07, "-0", "%1.f"}, + {__LINE__, -2.971013180028710e+22, "-2.971e+22", "%3.5g"}, + {__LINE__, -2.975167862441254e+07, "-2.97517e+07", "%g"}, + {__LINE__, -2.976018424339993e+16, "-2.976018e+16", "%3e"}, + {__LINE__, -2.979173094835454e+29, "-2.97917E+29", "%G"}, + {__LINE__, -2.983135249987541e-03, "-0.00298314", "%+G"}, + {__LINE__, -2.985142444917919e-24, "-2.985142E-24", "%+#7E"}, + {__LINE__, -2.988680953635159e-14, "-0.0000000", "%2.7f"}, + {__LINE__, -2.989629778079379e+04, "-29896.297781", "%f"}, + {__LINE__, -2.991274275137276e+19, "-29912742751372762839.423558", "%+4f"}, + {__LINE__, -2.991286396006024e-06, "-2.99129e-06", "%4g"}, + {__LINE__, -2.993310397844811e+04, "-2.993310E+04", "%3.6E"}, + {__LINE__, -2.994669852410861e-29, "-2.99467E-29", "%G"}, + {__LINE__, -2.996082093034831e+27, "-2.996082e+27", "%1e"}, + {__LINE__, -2.999783904575110e+16, "-2.999784E+16", "%#E"}, + {__LINE__, -3.012019221956988e+25, "-3.012019E+25", "%E"}, + {__LINE__, -3.014211917706622e-25, "-0.000000", "%+f"}, + {__LINE__, -3.015149723683428e-19, "-3.01515E-19", "%G"}, + {__LINE__, -3.022158478004638e-19, "-3.02216e-19", "%6g"}, + {__LINE__, -3.022825518373900e-12, "-3.0228E-12", "%+.4E"}, + {__LINE__, -3.025108924057340e-19, "-0.000000", "%f"}, + {__LINE__, -3.026316824631967e+24, "-3026316824631966717618070.106255", "%1f"}, + {__LINE__, -3.033074643529623e-13, "-3.03307e-13", "%g"}, + {__LINE__, -3.035292960731141e+24, "-3035292960731141409524980.190326", "%f"}, + {__LINE__, -3.043291272956732e-13, "-3.04329e-13", "%#g"}, + {__LINE__, -3.045216723973715e-23, "-3.045e-23", "%.4g"}, + {__LINE__, -3.047140976048835e+09, "-3.04714E+09", "%3.5E"}, + {__LINE__, -3.047680278470886e+09, "-3.047680E+09", "%#E"}, + {__LINE__, -3.048465807963461e+05, "-304847", "%g"}, + {__LINE__, -3.050904753556756e+22, "-3.0509E+22", "%G"}, + {__LINE__, -3.052845748999047e-13, "-3.05285e-13", "%g"}, + {__LINE__, -3.053395231883620e-06, "-3E-06", "%2.G"}, + {__LINE__, -3.054894203375445e-28, "-3e-28", "%2.g"}, + {__LINE__, -3.055080347760755e-13, "-0", "%2.f"}, + {__LINE__, -3.055513037393624e-29, "-3e-29", "%3.g"}, + {__LINE__, -3.056198778208295e-06, "-3.0562e-06", "%g"}, + {__LINE__, -3.057813660266980e-21, "-3E-21", "%1.G"}, + {__LINE__, -3.059687036330998e-11, "-3.1E-11", "%#2.2G"}, + {__LINE__, -3.061450385559094e-10, "-3.06145E-10", "%G"}, + {__LINE__, -3.066605713361383e-27, "-0.000000", "%f"}, + {__LINE__, -3.071590110813156e+22, "-3.07159E+22", "%G"}, + {__LINE__, -3.073253864426931e+26, "-3.0733E+26", "%.5G"}, + {__LINE__, -3.078998328596940e+07, "-3.079e+07", "%5.4g"}, + {__LINE__, -3.082733887951920e+06, "-3.082734e+06", "%+6e"}, + {__LINE__, -3.084365358064710e+24, "-3.084365E+24", "%#E"}, + {__LINE__, -3.086948022123716e+25, "-30869480221237162176350921.072299", "%+f"}, + {__LINE__, -3.088200214218024e-10, "-0", "%0.f"}, + {__LINE__, -3.093442983942874e+08, "-309344298.394287", "%f"}, + {__LINE__, -3.103573455403534e-09, "-3.103573E-09", "%#E"}, + {__LINE__, -3.109178443120997e+07, "-31091784.431210", "%f"}, + {__LINE__, -3.111494549914917e+28, "-3e+28", "%0.g"}, + {__LINE__, -3.113384020517480e-17, " -0", "%+6.f"}, + {__LINE__, -3.121622779718055e+14, "-312162277971805.491", "%+.3f"}, + {__LINE__, -3.122780443843900e-01, "-0.312278", "%#g"}, + {__LINE__, -3.122952438335638e-25, "-3.122952E-25", "%E"}, + {__LINE__, -3.128970339463168e-18, "-3e-18", "%3.g"}, + {__LINE__, -3.130862507719335e+10, "-3.130863e+10", "%3e"}, + {__LINE__, -3.145960838955379e+03, "-3145.96", "%g"}, + {__LINE__, -3.149362645138929e+21, "-3.14936e+21", "%g"}, + {__LINE__, -3.150697168664913e+26, "-315069716866491322804222363.629378", "%+f"}, + {__LINE__, -3.157946785041287e+05, "-3.1579e+05", "%.4e"}, + {__LINE__, -3.158347006986809e-02, "-3.E-02", "%#.0E"}, + {__LINE__, -3.159542871923388e+07, "-31595429", "%2.f"}, + {__LINE__, -3.169997512351985e+15, "-3.169998E+15", "%6.7G"}, + {__LINE__, -3.170971776544746e+07, "-31709717.765447", "%f"}, + {__LINE__, -3.173246079104466e+19, "-3.173246E+19", "%1E"}, + {__LINE__, -3.173581228658553e-02, "-0.03", "%+0.G"}, + {__LINE__, -3.187598864929850e-07, "-3.18760E-07", "%#G"}, + {__LINE__, -3.190525302270244e+08, "-3.190525e+08", "%+0.7g"}, + {__LINE__, -3.191879884186422e+19, "-3.19E+19", "%.2E"}, + {__LINE__, -3.197292604744926e+02, "-3.197293e+02", "%+e"}, + {__LINE__, -3.203713337688838e-12, "-3.20371E-12", "%6G"}, + {__LINE__, -3.204416889544914e-18, "-0.000000", "%+f"}, + {__LINE__, -3.204494471917096e+09, "-3.20449e+09", "%g"}, + {__LINE__, -3.211933195516720e+11, "-3.21193E+11", "%G"}, + {__LINE__, -3.214544021431917e-24, "-0.000000", "%1f"}, + {__LINE__, -3.215501229487004e-07, "-3e-07", "%3.e"}, + {__LINE__, -3.232157492322707e-13, "-3.E-13", "%+#5.G"}, + {__LINE__, -3.239074974455177e-05, "-3.239e-05", "%2.4g"}, + {__LINE__, -3.243083730801156e-24, "-3.243084e-24", "%#e"}, + {__LINE__, -3.243128583394124e+07, "-32431285.8339", "%6.4f"}, + {__LINE__, -3.247997999770571e-08, "-3e-08", "%5.e"}, + {__LINE__, -3.258251054563991e-15, "-3.2583E-15", "%.5G"}, + {__LINE__, -3.259499053187446e+07, "-3.2595E+07", "%5.6G"}, + {__LINE__, -3.261907782031174e+20, "-3.3e+20", "%.2g"}, + {__LINE__, -3.263979380855122e+29, "-3.26398E+29", "%G"}, + {__LINE__, -3.264166546402073e+06, "-3.26417E+06", "%5G"}, + {__LINE__, -3.269357648926951e+01, "-3.E+01", "%#5.G"}, + {__LINE__, -3.273523287028019e-30, "-3.27352e-30", "%g"}, + {__LINE__, -3.274464331526264e-14, "-3e-14", "%.0g"}, + {__LINE__, -3.276647049958546e+05, "-327664.7", "%+.1f"}, + {__LINE__, -3.276853612008326e-04, "-3.276854e-04", "%e"}, + {__LINE__, -3.288077788905925e+17, "-3.28808e+17", "%g"}, + {__LINE__, -3.292054327509010e+21, "-3.29205e+21", "%+g"}, + {__LINE__, -3.292086868337041e-16, " -0", "%7.f"}, + {__LINE__, -3.299368070005327e-17, "-3E-17", "%5.G"}, + {__LINE__, -3.307165537474566e-30, "-3e-30", "%+2.g"}, + {__LINE__, -3.310556325973673e-17, "-3.31056E-17", "%G"}, + {__LINE__, -3.315407318453138e-09, "-3.315407e-09", "%+#.6e"}, + {__LINE__, -3.318402800998018e-09, "-3.3184E-09", "%+G"}, + {__LINE__, -3.324277622889107e-04, "-0.000332428", "%G"}, + {__LINE__, -3.326372457131185e+14, "-3.326372e+14", "%0e"}, + {__LINE__, -3.333300611287597e+18, "-3.3E+18", "%4.2G"}, + {__LINE__, -3.333608976277018e-25, "-3.33361e-25", "%g"}, + {__LINE__, -3.333613056182724e-24, "-3.33361e-24", "%.5e"}, + {__LINE__, -3.338505874378410e-26, "-0.00000", "%.5f"}, + {__LINE__, -3.339838772519661e+24, "-3.3398E+24", "%0.5G"}, + {__LINE__, -3.354802735583258e-27, "-3.3548E-27", "%G"}, + {__LINE__, -3.356542080644329e+15, "-3356542080644329.129058", "%#f"}, + {__LINE__, -3.360027692463026e-29, "-3.36003e-29", "%+g"}, + {__LINE__, -3.361845657814323e+06, "-3361845.657814", "%f"}, + {__LINE__, -3.363135493765816e-12, " -0", "%5.f"}, + {__LINE__, -3.367383112102258e+19, "-33673831121022579360.158034", "%f"}, + {__LINE__, -3.376798505037497e+03, "-3376.8", "%+3G"}, + {__LINE__, -3.388170639372559e+09, "-3.388171e+09", "%+e"}, + {__LINE__, -3.397304185715499e-12, "-3.397304e-12", "%+e"}, + {__LINE__, -3.401559374016378e-13, "-3.40156e-13", "%g"}, + {__LINE__, -3.402651870178825e+21, "-3.40265e+21", "%+g"}, + {__LINE__, -3.410491213537530e+12, "-3.41049E+12", "%G"}, + {__LINE__, -3.411695570460075e-01, "-3.411696e-01", "%e"}, + {__LINE__, -3.417311435719220e+29, "-3.4E+29", "%3.1E"}, + {__LINE__, -3.417581063208165e-23, " -0", "%+4.f"}, + {__LINE__, -3.424005615113663e-28, "-0.000000", "%f"}, + {__LINE__, -3.424793585094130e-12, "-0.000000", "%f"}, + {__LINE__, -3.434733077762304e-05, "-3.43473e-05", "%g"}, + {__LINE__, -3.436384988494743e+08, "-3.43638E+08", "%G"}, + {__LINE__, -3.441082995163884e-21, "-0.000000", "%f"}, + {__LINE__, -3.442868661576827e-23, "-3.442869e-23", "%#.7g"}, + {__LINE__, -3.444337521046186e+13, "-3.44434E+13", "%#G"}, + {__LINE__, -3.448230173418967e-28, "-3.44823E-28", "%#G"}, + {__LINE__, -3.448523828225326e-21, "-3.4485e-21", "%.4e"}, + {__LINE__, -3.449876796754720e-07, "-3.44988e-07", "%g"}, + {__LINE__, -3.450163790411588e+25, "-34501637904115876054333966.749255", "%2f"}, + {__LINE__, -3.450224454406567e-26, "-3.450224E-26", "%#1E"}, + {__LINE__, -3.459930768422874e-28, "-3.46E-28", "%2.3G"}, + {__LINE__, -3.460730505022601e-28, "-3.46073e-28", "%#g"}, + {__LINE__, -3.462894265593946e-10, "-3.46289e-10", "%+#g"}, + {__LINE__, -3.464808359229496e+05, "-346481", "%6.6G"}, + {__LINE__, -3.467272303652620e-03, "-0.00346727", "%g"}, + {__LINE__, -3.468252706910251e+10, "-3.5E+10", "%#7.2G"}, + {__LINE__, -3.473623965680253e+27, "-3.47362e+27", "%g"}, + {__LINE__, -3.474527926009729e+11, "-3.475E+11", "%.4G"}, + {__LINE__, -3.476489048299929e+15, "-3.47649e+15", "%g"}, + {__LINE__, -3.477809698895344e-09, "-0.000", "%+.3f"}, + {__LINE__, -3.493008398637667e-28, " -0", "%+4.f"}, + {__LINE__, -3.497441286691613e+07, "-3.49744e+07", "%g"}, + {__LINE__, -3.500904660533358e+00, "-3.500905E+00", "%+1E"}, + {__LINE__, -3.504996054364915e+25, "-3.505e+25", "%+g"}, + {__LINE__, -3.507738629125434e-24, "-0.00", "%+4.2f"}, + {__LINE__, -3.508557770253211e-04, "-3.508558e-04", "%#6e"}, + {__LINE__, -3.516056824437550e+20, "-3.516057e+20", "%#e"}, + {__LINE__, -3.521102917733367e+02, "-352.11", "%g"}, + {__LINE__, -3.523948985825251e+10, "-3.52395e+10", "%+g"}, + {__LINE__, -3.525281835697895e-15, "-3.525282e-15", "%e"}, + {__LINE__, -3.534088845494570e-26, "-0.000000", "%2f"}, + {__LINE__, -3.541002442741569e+01, "-35.410", "%3.3f"}, + {__LINE__, -3.541126630345017e-19, "-3.541127e-19", "%e"}, + {__LINE__, -3.545277845967514e-17, "-0.000000", "%f"}, + {__LINE__, -3.560935430637329e-12, "-3.56094E-12", "%G"}, + {__LINE__, -3.564234584447659e-23, "-3.56423E-23", "%G"}, + {__LINE__, -3.576040378379921e-11, "-0.000000", "%+6f"}, + {__LINE__, -3.578133580350674e-05, "-3.578134e-05", "%#e"}, + {__LINE__, -3.584484015191491e-28, "-0.0", "%+2.1f"}, + {__LINE__, -3.591961845146752e+27, "-3.59196E+27", "%G"}, + {__LINE__, -3.602508380305001e+26, "-3.60251e+26", "%g"}, + {__LINE__, -3.603113449808132e+04, "-36031.134498", "%+3.6f"}, + {__LINE__, -3.605569849504628e+25, "-4E+25", "%6.G"}, + {__LINE__, -3.611297754851416e+05, "-361129.775485", "%+f"}, + {__LINE__, -3.613177909959201e-28, "-0.000000", "%f"}, + {__LINE__, -3.616261225230595e-01, "-0.361626", "%3g"}, + {__LINE__, -3.619203521536307e+09, "-3.6192E+09", "%G"}, + {__LINE__, -3.619316709131723e-27, "-0.0000000", "%.7f"}, + {__LINE__, -3.623100583082346e+28, "-3.6231E+28", "%+G"}, + {__LINE__, -3.626582653589919e+21, "-3626582653589918603387.332041", "%f"}, + {__LINE__, -3.626667540343067e-30, "-4.E-30", "%#.0G"}, + {__LINE__, -3.633539220625861e-30, "-3.634E-30", "%+.3E"}, + {__LINE__, -3.641655782149502e-11, "-3.64166E-11", "%G"}, + {__LINE__, -3.644523594046571e+25, "-3.64452e+25", "%5g"}, + {__LINE__, -3.645977605394108e+25, "-3.646e+25", "%+0.3e"}, + {__LINE__, -3.647864582248812e+13, "-4E+13", "%4.G"}, + {__LINE__, -3.655241667633056e-25, "-3.655242E-25", "%E"}, + {__LINE__, -3.662603655433297e+23, "-3.662604e+23", "%e"}, + {__LINE__, -3.668995445134158e+22, " -4E+22", "%7.G"}, + {__LINE__, -3.670006666569412e-11, "-3.670007e-11", "%e"}, + {__LINE__, -3.670510381138509e-24, "-3.670510e-24", "%e"}, + {__LINE__, -3.671283003268254e-20, "-3.67128E-20", "%G"}, + {__LINE__, -3.687760201176777e-13, "-3.68776E-13", "%G"}, + {__LINE__, -3.687983982100676e-14, "-0.00", "%.2f"}, + {__LINE__, -3.694808382827435e-12, "-3.694808E-12", "%#E"}, + {__LINE__, -3.696158520838821e+28, "-3.69616E+28", "%3G"}, + {__LINE__, -3.700911860811323e+18, "-3.70091e+18", "%+g"}, + {__LINE__, -3.701328314738669e+24, "-3.70133e+24", "%g"}, + {__LINE__, -3.707068461822151e-19, "-3.707068E-19", "%E"}, + {__LINE__, -3.710679411506914e+25, "-3.71068e+25", "%g"}, + {__LINE__, -3.713003840923896e-23, "-3.713e-23", "%7g"}, + {__LINE__, -3.717028397747828e-28, "-3.71703e-28", "%g"}, + {__LINE__, -3.728159642236442e+14, "-3.72816E+14", "%G"}, + {__LINE__, -3.731956233637010e+21, "-3.73196E+21", "%G"}, + {__LINE__, -3.742336623322610e+07, "-3.74234e+07", "%g"}, + {__LINE__, -3.748328269630045e+04, "-37483.3", "%G"}, + {__LINE__, -3.750803081283569e-27, "-3.7508E-27", "%+7G"}, + {__LINE__, -3.752021500826652e-04, "-0.0004", "%.1G"}, + {__LINE__, -3.755942257004103e-12, "-3.75594E-12", "%G"}, + {__LINE__, -3.765921235889045e+03, " -4e+03", "%+7.g"}, + {__LINE__, -3.772453135667801e-28, "-3.77245E-28", "%G"}, + {__LINE__, -3.776384200230367e+27, "-3.776e+27", "%1.4g"}, + {__LINE__, -3.787035870684945e-25, "-4E-25", "%+3.G"}, + {__LINE__, -3.798616350818839e-29, "-3.7986164e-29", "%.7e"}, + {__LINE__, -3.807178315238428e-15, "-0.000000", "%+#6f"}, + {__LINE__, -3.822059837967635e+18, "-3822059837967634621.236357", "%6.6f"}, + {__LINE__, -3.823553096022006e+12, "-3.82355e+12", "%g"}, + {__LINE__, -3.839198115259428e-23, "-0.000000", "%f"}, + {__LINE__, -3.846953640321746e+02, "-3.846954E+02", "%E"}, + {__LINE__, -3.852230696542361e+08, "-3.8522e+08", "%#.5g"}, + {__LINE__, -3.861139362195314e-25, "-3.86114E-25", "%2G"}, + {__LINE__, -3.873388618099769e+09, "-3873388618", "%0.0f"}, + {__LINE__, -3.874161550543817e+15, "-3.874162e+15", "%+e"}, + {__LINE__, -3.874527935469425e+20, "-3.87E+20", "%3.3G"}, + {__LINE__, -3.878069577893697e-30, "-3.878070E-30", "%#E"}, + {__LINE__, -3.878852490397702e-20, "-3.87885e-20", "%g"}, + {__LINE__, -3.879688342272101e-10, "-0.00", "%#4.2f"}, + {__LINE__, -3.879797428399897e+03, "-3880", "%1.4g"}, + {__LINE__, -3.881588804766756e+00, "-3.9", "%+4.2G"}, + {__LINE__, -3.889377318111182e+01, "-38.8938", "%+.4f"}, + {__LINE__, -3.893444523430385e-03, "-0.004", "%+2.g"}, + {__LINE__, -3.894912141515856e+03, "-3894.91", "%+g"}, + {__LINE__, -3.905279115773667e+04, "-39052.791158", "%f"}, + {__LINE__, -3.905333955484919e-20, "-3.90533E-20", "%G"}, + {__LINE__, -3.906559442060730e+27, "-3906559442060730447110472596.250474", "%f"}, + {__LINE__, -3.909396469655334e-26, "-3.91E-26", "%.3G"}, + {__LINE__, -3.920132491092364e+11, "-3.92013e+11", "%g"}, + {__LINE__, -3.935651497471973e+27, "-3935651497471972604103029880", "%5.f"}, + {__LINE__, -3.939742577853889e-03, "-0.004", "%5.G"}, + {__LINE__, -3.943284017088198e+19, "-39432840170881981123.3", "%.1f"}, + {__LINE__, -3.947452186327660e+09, "-3947452186.33", "%0.2f"}, + {__LINE__, -3.951967237553488e+18, "-3951967237553488132.562052", "%f"}, + {__LINE__, -3.953673427359789e-28, "-3.954E-28", "%3.4G"}, + {__LINE__, -3.956044867371602e+23, "-4e+23", "%+6.1g"}, + {__LINE__, -3.965038994251913e-20, "-3.96504e-20", "%g"}, + {__LINE__, -3.970527677577937e+05, "-4.e+05", "%#0.1g"}, + {__LINE__, -3.970717822164395e-20, "-3.970718e-20", "%6e"}, + {__LINE__, -3.978063834438912e-19, "-3.97806E-19", "%G"}, + {__LINE__, -3.982705395472599e+27, "-3.9827e+27", "%#.5g"}, + {__LINE__, -3.983486821352571e-28, "-3.98349e-28", "%g"}, + {__LINE__, -3.985470630230926e-14, "-4e-14", "%.2g"}, + {__LINE__, -3.993095662863953e-16, "-3.993096e-16", "%e"}, + {__LINE__, -4.006984141314271e-22, " -4e-22", "%7.2g"}, + {__LINE__, -4.006984141314271e-22, "-4.0e-22", "%#7.2g"}, + {__LINE__, -4.013226099863981e+06, "-4.013226E+06", "%+E"}, + {__LINE__, -4.013702226581167e+00, "-4.0137022", "%+#3.7f"}, + {__LINE__, -4.017598274642537e+11, "-4E+11", "%5.G"}, + {__LINE__, -4.021385792825529e-09, " -4e-09", "%7.2g"}, + {__LINE__, -4.024099064819937e-20, "-4.0241E-20", "%+G"}, + {__LINE__, -4.026952473441366e+22, "-4.026952e+22", "%6.7g"}, + {__LINE__, -4.027636677087866e+12, "-4.02764E+12", "%G"}, + {__LINE__, -4.036506013049443e+17, "-4.03651e+17", "%0.6g"}, + {__LINE__, -4.063191953523012e-06, " -0.0", "%6.1f"}, + {__LINE__, -4.070953223475192e-23, "-4.07095E-23", "%G"}, + {__LINE__, -4.072425833070524e+09, "-4.072426E+09", "%E"}, + {__LINE__, -4.086025576759603e+12, "-4.086026e+12", "%e"}, + {__LINE__, -4.086627783296081e-24, "-4.087E-24", "%.3E"}, + {__LINE__, -4.086683676772144e+10, "-40866836767.721439", "%f"}, + {__LINE__, -4.087336043219081e-10, "-0.", "%+#0.f"}, + {__LINE__, -4.125162973336809e-26, "-4.125163E-26", "%.7G"}, + {__LINE__, -4.150753797717075e-10, "-4.2e-10", "%.2g"}, + {__LINE__, -4.152126152181912e+23, "-4.15213e+23", "%g"}, + {__LINE__, -4.164458223079432e-07, "-4.164458E-07", "%E"}, + {__LINE__, -4.170624866130099e+03, "-4170.624866", "%4.6f"}, + {__LINE__, -4.171137277374942e-01, " -4E-01", "%+7.E"}, + {__LINE__, -4.176067421901769e-25, "-4.17607E-25", "%G"}, + {__LINE__, -4.179514418604771e+24, "-4.1795e+24", "%3.5g"}, + {__LINE__, -4.196285183415621e+23, "-4.19629E+23", "%5G"}, + {__LINE__, -4.198600486581023e+23, "-4E+23", "%+4.G"}, + {__LINE__, -4.199020494598538e-16, "-0.000000", "%+f"}, + {__LINE__, -4.207824153501688e-01, "-0.420782", "%G"}, + {__LINE__, -4.219086441294006e-13, " -0", "%+4.f"}, + {__LINE__, -4.223689582765525e+12, "-4.22369e+12", "%g"}, + {__LINE__, -4.230213377391093e+04, "-42302.1", "%+2.6G"}, + {__LINE__, -4.232495358100605e-02, "-0.042325", "%+f"}, + {__LINE__, -4.235459662515940e-29, "-0.000000", "%f"}, + {__LINE__, -4.240807508057296e-07, "-4e-07", "%0.g"}, + {__LINE__, -4.248376819122346e-13, "-4.248377e-13", "%7e"}, + {__LINE__, -4.251361939638334e-23, "-4.251362E-23", "%E"}, + {__LINE__, -4.253044333568077e+24, "-4.253044e+24", "%4e"}, + {__LINE__, -4.255728039119772e-30, "-4.25573E-30", "%2G"}, + {__LINE__, -4.256502531648988e+02, "-425.65", "%G"}, + {__LINE__, -4.260731632612400e-04, "-0", "%2.f"}, + {__LINE__, -4.262037138254753e-25, "-4e-25", "%5.g"}, + {__LINE__, -4.270910126379393e+00, "-4.2709", "%#7.4f"}, + {__LINE__, -4.273844276097008e-09, " -0.", "%#4.f"}, + {__LINE__, -4.280424136111831e+21, "-4.280E+21", "%.3E"}, + {__LINE__, -4.284351940366486e-23, "-0.000000", "%+f"}, + {__LINE__, -4.291747881693517e-29, "-4.3e-29", "%+#.2g"}, + {__LINE__, -4.298346825232928e+04, "-42983.5", "%G"}, + {__LINE__, -4.298879924365432e+28, "-42988799243654316484253286034.800112", "%f"}, + {__LINE__, -4.302192665689887e-25, "-0.000000", "%f"}, + {__LINE__, -4.303899568112784e+06, "-4E+06", "%1.G"}, + {__LINE__, -4.307399960278190e-30, "-4.31e-30", "%.3g"}, + {__LINE__, -4.341161949359232e+01, "-43.411619", "%+f"}, + {__LINE__, -4.353334435604675e+17, " -4e+17", "%+7.g"}, + {__LINE__, -4.354760231068195e+08, "-4e+08", "%2.e"}, + {__LINE__, -4.363535913318519e+08, "-4.363536E+08", "%+7E"}, + {__LINE__, -4.365389605557404e-29, "-4.365390e-29", "%e"}, + {__LINE__, -4.369278570829168e-22, "-4.369279E-22", "%5E"}, + {__LINE__, -4.372187639240662e-11, "-4.37219e-11", "%+g"}, + {__LINE__, -4.374221079233858e-15, "-4.37422E-15", "%4.6G"}, + {__LINE__, -4.374840292001664e-13, "-0.000000", "%+f"}, + {__LINE__, -4.376554800103945e+16, "-43765548001039453.751559", "%f"}, + {__LINE__, -4.376659235899513e+07, "-4.4E+07", "%0.2G"}, + {__LINE__, -4.377316129868239e-06, "-4.37732E-06", "%+5G"}, + {__LINE__, -4.380028480226885e+21, "-4e+21", "%1.0e"}, + {__LINE__, -4.380434965450223e+16, "-43804349654502230.393405", "%f"}, + {__LINE__, -4.392477080355168e-15, "-4E-15", "%+2.G"}, + {__LINE__, -4.396120020536161e+17, "-439612002053616102.159594", "%#f"}, + {__LINE__, -4.400993154571784e+13, "-44009931545717.844159", "%f"}, + {__LINE__, -4.402873692051930e-26, "-4.4e-26", "%5.3g"}, + {__LINE__, -4.405330274903779e+24, "-4405330274903779042006135.568954", "%+5f"}, + {__LINE__, -4.406272291312090e+06, "-4.40627e+06", "%+g"}, + {__LINE__, -4.407065949816988e-13, "-0.000000", "%f"}, + {__LINE__, -4.408295597674563e-10, "-4.4083e-10", "%g"}, + {__LINE__, -4.417553815178214e-30, "-4.41755E-30", "%G"}, + {__LINE__, -4.428894120469459e+16, "-4.42889e+16", "%g"}, + {__LINE__, -4.433747825142046e+04, "-4.433748e+04", "%#e"}, + {__LINE__, -4.435051082856639e+11, "-4.43505E+11", "%G"}, + {__LINE__, -4.445045573007259e-27, "-4.445e-27", "%.5g"}, + {__LINE__, -4.447791368960968e-25, " -4e-25", "%+7.g"}, + {__LINE__, -4.449691386526521e-11, "-4.44969e-11", "%g"}, + {__LINE__, -4.451399093849402e+06, "-4.e+06", "%#0.e"}, + {__LINE__, -4.453753130320864e-22, "-4E-22", "%6.E"}, + {__LINE__, -4.455910780463539e+00, "-4", "%2.g"}, + {__LINE__, -4.464935674747308e+25, "-4.46494e+25", "%g"}, + {__LINE__, -4.469759565657550e-29, "-4.46976e-29", "%g"}, + {__LINE__, -4.473254139442931e+14, "-4E+14", "%3.1G"}, + {__LINE__, -4.480844704930373e-04, "-0.000448", "%5.6f"}, + {__LINE__, -4.484177356024563e+19, "-4.484177E+19", "%E"}, + {__LINE__, -4.488115678357666e-13, "-4.48812e-13", "%g"}, + {__LINE__, -4.491545112281561e+12, "-4.491545E+12", "%E"}, + {__LINE__, -4.492547433553077e-23, "-4.49255e-23", "%g"}, + {__LINE__, -4.492701755830813e-05, "-0.000045", "%f"}, + {__LINE__, -4.497405935096372e-02, "-4.497406e-02", "%e"}, + {__LINE__, -4.502764260212887e+21, "-4.50276E+21", "%G"}, + {__LINE__, -4.511984199938207e+29, "-451198419993820731832556703011.505306", "%f"}, + {__LINE__, -4.514517861504637e-13, "-4.51452e-13", "%g"}, + {__LINE__, -4.525577218881990e+24, "-4.5e+24", "%2.2g"}, + {__LINE__, -4.549244580903896e-26, "-5E-26", "%2.G"}, + {__LINE__, -4.569192488002113e-29, "-5E-29", "%3.G"}, + {__LINE__, -4.572011438847734e+17, "-4.57e+17", "%#.3g"}, + {__LINE__, -4.573010973514519e-08, "-4.57301e-08", "%+7g"}, + {__LINE__, -4.584534976177852e-04, "-0.000458453", "%7g"}, + {__LINE__, -4.584729895132228e-21, "-0.000000", "%4f"}, + {__LINE__, -4.585259328217483e-30, "-4.58526E-30", "%+#4.6G"}, + {__LINE__, -4.589603063610410e-04, "-4.589603E-04", "%+E"}, + {__LINE__, -4.592428795671033e+22, " -5e+22", "%7.g"}, + {__LINE__, -4.595683678223830e-20, " -0", "%5.f"}, + {__LINE__, -4.602585606100101e+05, "-4.602586E+05", "%E"}, + {__LINE__, -4.603375306660027e-08, "-5E-08", "%+6.G"}, + {__LINE__, -4.611341343728034e-18, "-4.611341e-18", "%5e"}, + {__LINE__, -4.613608487855863e+05, "-461361", "%6g"}, + {__LINE__, -4.615997775774194e-24, "-4.61600e-24", "%#g"}, + {__LINE__, -4.620310950564759e+23, "-462031095056475926696749", "%1.0f"}, + {__LINE__, -4.620645693595563e-10, "-4.6206457e-10", "%.7e"}, + {__LINE__, -4.621527706233292e-09, "-4.621528e-09", "%#e"}, + {__LINE__, -4.625215210297273e-13, "-4.62522E-13", "%G"}, + {__LINE__, -4.636755152220100e-29, "-0.000000", "%#f"}, + {__LINE__, -4.641203877508087e+23, "-4.6412e+23", "%0g"}, + {__LINE__, -4.648627249239175e+23, "-4.65E+23", "%+0.3G"}, + {__LINE__, -4.662546890623409e-10, "-4.663E-10", "%.3E"}, + {__LINE__, -4.664488650292317e-27, "-4.66449e-27", "%#g"}, + {__LINE__, -4.668715685060282e+08, "-466871568.50603", "%.5f"}, + {__LINE__, -4.673332851657081e-18, "-4.7e-18", "%#5.2g"}, + {__LINE__, -4.683361436174074e+24, "-4.68e+24", "%#.2e"}, + {__LINE__, -4.693748680461066e-01, "-0.469375", "%G"}, + {__LINE__, -4.697110010549124e+23, "-4.69711E+23", "%G"}, + {__LINE__, -4.710442739966989e+06, "-4.710443e+06", "%2e"}, + {__LINE__, -4.713427678642280e-09, "-4.71343e-09", "%g"}, + {__LINE__, -4.721999055940316e-04, "-4.721999e-04", "%#1e"}, + {__LINE__, -4.724262200578540e+17, "-472426220057853996.081576", "%+f"}, + {__LINE__, -4.733267644288093e+01, "-4.733268e+01", "%7e"}, + {__LINE__, -4.742854711396110e-11, "-0.000000", "%f"}, + {__LINE__, -4.757456106385936e+23, "-4.75746e+23", "%g"}, + {__LINE__, -4.770895495642298e+27, "-4770895495642298200777986494.358629", "%2f"}, + {__LINE__, -4.775073456318149e+29, "-4.775073E+29", "%#E"}, + {__LINE__, -4.779997387204258e+17, "-4.78e+17", "%g"}, + {__LINE__, -4.799264315770707e-23, "-4.799264E-23", "%+.7G"}, + {__LINE__, -4.814277930599660e+10, "-5e+10", "%2.1g"}, + {__LINE__, -4.818413387018612e+05, "-5e+05", "%.0g"}, + {__LINE__, -4.818827195107612e-16, "-4.818827e-16", "%e"}, + {__LINE__, -4.841524514024803e+21, "-4.84152E+21", "%G"}, + {__LINE__, -4.860585077313477e+10, "-4.86059E+10", "%+G"}, + {__LINE__, -4.870037451948589e+28, "-48700374519485887248038125107.996166", "%f"}, + {__LINE__, -4.888835952705153e-28, "-4.888836E-28", "%+E"}, + {__LINE__, -4.893523355754114e-19, "-4.893523e-19", "%.6e"}, + {__LINE__, -4.906070260482585e+17, "-490607026048258454", "%+.0f"}, + {__LINE__, -4.907734144101900e+25, "-5E+25", "%+.1G"}, + {__LINE__, -4.917899547741841e-25, "-4.917900e-25", "%e"}, + {__LINE__, -4.923348512538722e+25, "-49233485125387215219916470.232420", "%+.6f"}, + {__LINE__, -4.926052630078460e-26, "-4.926053e-26", "%e"}, + {__LINE__, -4.926751662051156e+14, "-4.927E+14", "%+5.3E"}, + {__LINE__, -4.928017952199046e-12, "-0.000000", "%f"}, + {__LINE__, -4.944296782981195e-21, "-4.9443E-21", "%1.5G"}, + {__LINE__, -4.947320427183599e-19, "-4.947320e-19", "%.6e"}, + {__LINE__, -4.968188938972135e+17, "-4.96819E+17", "%G"}, + {__LINE__, -4.968756725758674e+05, "-496876", "%G"}, + {__LINE__, -4.968788759793340e-26, "-4.97E-26", "%.3G"}, + {__LINE__, -4.983904999913364e-24, "-5E-24", "%0.G"}, + {__LINE__, -4.985209496522767e+23, "-4.985209e+23", "%e"}, + {__LINE__, -4.987363880732866e+08, "-4.98736E+08", "%G"}, + {__LINE__, -4.989754940144973e+05, "-498975.494014", "%f"}, + {__LINE__, -4.998203870733718e-25, "-4.9982e-25", "%g"}, + {__LINE__, -4.999153445016792e-27, "-5e-27", "%+1.e"}, + {__LINE__, -5.002693488258108e+06, " -5e+06", "%+7.g"}, + {__LINE__, -5.017958458603783e+05, "-5.0179585E+05", "%+.7E"}, + {__LINE__, -5.028902178573363e-12, "-5.0289e-12", "%0g"}, + {__LINE__, -5.032337536971444e+07, "-50323375.369714", "%f"}, + {__LINE__, -5.036525516370473e-24, "-5.0365e-24", "%4.5g"}, + {__LINE__, -5.038642321515756e+18, "-5038642321515755639.893541", "%f"}, + {__LINE__, -5.051003516976770e-14, "-5.051E-14", "%+G"}, + {__LINE__, -5.051196143816789e+27, "-5.0512e+27", "%g"}, + {__LINE__, -5.057372886713786e+28, "-5.057373e+28", "%#e"}, + {__LINE__, -5.057475029459727e+12, "-5.05748e+12", "%+g"}, + {__LINE__, -5.068286816124670e-17, "-5.068E-17", "%5.3E"}, + {__LINE__, -5.070475165759468e-12, "-0.000000", "%4.6f"}, + {__LINE__, -5.081352543220476e+20, "-5.081E+20", "%.3E"}, + {__LINE__, -5.084398319179363e+11, "-508439831917.936313", "%#f"}, + {__LINE__, -5.085781220648484e+05, "-5.E+05", "%+#7.G"}, + {__LINE__, -5.094941054632498e-24, "-5.09494E-24", "%+G"}, + {__LINE__, -5.100059596310871e-10, "-5.10006e-10", "%3.6g"}, + {__LINE__, -5.103072553594136e-01, "-0.5", "%+1.g"}, + {__LINE__, -5.106319890388412e+08, "-5.106320e+08", "%4e"}, + {__LINE__, -5.107120687977690e+09, "-5107120687.977690", "%f"}, + {__LINE__, -5.119910716893161e-13, "-5.11991E-13", "%6G"}, + {__LINE__, -5.127594569425709e-29, "-5.12759e-29", "%#g"}, + {__LINE__, -5.130365585632797e+29, "-5.13037e+29", "%#g"}, + {__LINE__, -5.131415638022112e+08, "-513141563.802211", "%0f"}, + {__LINE__, -5.144703216335916e-23, "-5.1447e-23", "%g"}, + {__LINE__, -5.152511923201882e-05, "-5.15251E-05", "%+G"}, + {__LINE__, -5.165484448280190e-12, " -0.0", "%5.1f"}, + {__LINE__, -5.179220760465737e-20, "-5e-20", "%+5.g"}, + {__LINE__, -5.183005119662180e-10, "-5.183005E-10", "%+E"}, + {__LINE__, -5.199587953258117e+13, "-5.19959e+13", "%6g"}, + {__LINE__, -5.202041951844580e-25, "-0.000000", "%f"}, + {__LINE__, -5.202836038621740e+00, "-5.20284", "%G"}, + {__LINE__, -5.212204099528552e-11, "-5.2122e-11", "%.6g"}, + {__LINE__, -5.225617726710534e+17, "-522561772671053414.995868", "%+f"}, + {__LINE__, -5.230162003281426e+25, "-52301620032814257754694153.84578", "%+6.5f"}, + {__LINE__, -5.251849429321286e-11, "-5.25185e-11", "%g"}, + {__LINE__, -5.265174939930368e+23, "-5.265175E+23", "%1.6E"}, + {__LINE__, -5.271218134351422e+21, "-5.27122e+21", "%+1g"}, + {__LINE__, -5.273823357545750e+17, "-527382335754575048.980541", "%f"}, + {__LINE__, -5.279848797214957e+11, "-527984879721.495659", "%0f"}, + {__LINE__, -5.281551888625290e+11, "-5e+11", "%4.g"}, + {__LINE__, -5.281780377142961e-24, "-5.28178e-24", "%g"}, + {__LINE__, -5.292125782379512e+03, "-5292.125782", "%f"}, + {__LINE__, -5.292401691410388e+15, "-5292401691410387.520875", "%f"}, + {__LINE__, -5.305120752102265e-25, "-5.30512e-25", "%+#.5e"}, + {__LINE__, -5.317975791704413e+16, "-5.31798e+16", "%+g"}, + {__LINE__, -5.336522843607233e+27, "-5.33652e+27", "%#6g"}, + {__LINE__, -5.342016438000917e-05, " -5e-05", "%+7.e"}, + {__LINE__, -5.351122291549103e+06, "-5351122.291549", "%+0f"}, + {__LINE__, -5.352043956303597e+00, "-5.35204", "%g"}, + {__LINE__, -5.353582488767747e+09, "-5.354E+09", "%7.3E"}, + {__LINE__, -5.383420177718380e+04, "-5e+04", "%1.e"}, + {__LINE__, -5.384320189791882e-10, "-5.384320e-10", "%e"}, + {__LINE__, -5.409487543257379e-12, "-5.40949E-12", "%G"}, + {__LINE__, -5.418395794776773e+23, "-5.4184e+23", "%g"}, + {__LINE__, -5.419918795921525e-09, "-0.000000", "%+f"}, + {__LINE__, -5.422374157832442e+11, "-5.42237e+11", "%3g"}, + {__LINE__, -5.426916951577001e-27, "-5.42692E-27", "%+G"}, + {__LINE__, -5.430058833461779e+28, "-5.43006E+28", "%G"}, + {__LINE__, -5.444433348653147e+23, "-5.444433E+23", "%E"}, + {__LINE__, -5.446560186537024e+09, "-5.446560E+09", "%E"}, + {__LINE__, -5.464243500152188e+27, "-5.46424E+27", "%G"}, + {__LINE__, -5.464427517531742e+20, "-546442751753174249529.365625", "%f"}, + {__LINE__, -5.465783705970954e+05, "-5E+05", "%1.E"}, + {__LINE__, -5.475138077475789e-11, "-5e-11", "%.0e"}, + {__LINE__, -5.475302250616576e+10, "-54753022506.1658", "%.4f"}, + {__LINE__, -5.488964905442472e+09, "-5.48896e+09", "%g"}, + {__LINE__, -5.492508526846316e-08, "-5.49251e-08", "%.6g"}, + {__LINE__, -5.520533408804846e-15, "-5.520533e-15", "%e"}, + {__LINE__, -5.532744688447266e+28, "-55327446884472664123677532188.594341", "%2.6f"}, + {__LINE__, -5.535834653450131e-14, "-5.5e-14", "%0.1e"}, + {__LINE__, -5.554344735392882e-02, "-5.554345E-02", "%+#E"}, + {__LINE__, -5.555497334515501e-17, "-5.5555e-17", "%g"}, + {__LINE__, -5.558304303221977e+14, "-555830430322197.699411", "%f"}, + {__LINE__, -5.562210815777694e-05, "-5.562211E-05", "%#1E"}, + {__LINE__, -5.564800722538402e-09, "-5.56480e-09", "%#.6g"}, + {__LINE__, -5.567143271590439e-01, "-5.567143e-01", "%#e"}, + {__LINE__, -5.568606044396186e+01, "-55.6861", "%4g"}, + {__LINE__, -5.592027856401721e+12, "-5.592028e+12", "%.7g"}, + {__LINE__, -5.592295342565443e+22, "-6.E+22", "%#3.G"}, + {__LINE__, -5.600007421926053e+07, "-56000074.219261", "%1f"}, + {__LINE__, -5.601444553155006e-12, "-5.60144E-12", "%G"}, + {__LINE__, -5.605128912835080e-03, "-0.005605", "%+f"}, + {__LINE__, -5.610950285679577e+19, "-5.61095E+19", "%G"}, + {__LINE__, -5.622074657679467e+11, "-562207465767.946690", "%f"}, + {__LINE__, -5.627358352391656e-01, "-0.6", "%1.g"}, + {__LINE__, -5.627974317622488e-02, "-5.627974e-02", "%+e"}, + {__LINE__, -5.629497696447229e+27, "-5.629E+27", "%+.4G"}, + {__LINE__, -5.631383567258576e-29, "-5.63138e-29", "%0g"}, + {__LINE__, -5.635241327177333e-18, "-5.63524E-18", "%G"}, + {__LINE__, -5.644819783138381e+06, "-5.644820E+06", "%#E"}, + {__LINE__, -5.648538459114833e+26, "-564853845911483265496144667.849876", "%+f"}, + {__LINE__, -5.649004865848537e+00, "-5.649", "%g"}, + {__LINE__, -5.653403316885170e+23, "-6E+23", "%2.0E"}, + {__LINE__, -5.656109962244926e-18, "-0.000000", "%f"}, + {__LINE__, -5.674988031695793e+22, "-5.674988E+22", "%+.7G"}, + {__LINE__, -5.686380226400881e-22, "-0.000000", "%f"}, + {__LINE__, -5.698840855829442e-14, "-5.69884E-14", "%G"}, + {__LINE__, -5.707036642649580e+03, "-5707.036643", "%f"}, + {__LINE__, -5.710344882278847e+11, "-5.71034E+11", "%+G"}, + {__LINE__, -5.714852196401017e+19, "-5.71485E+19", "%+G"}, + {__LINE__, -5.720153622156089e-22, "-5.72015E-22", "%G"}, + {__LINE__, -5.722366011976922e+26, "-5.72237e+26", "%+g"}, + {__LINE__, -5.722909928097404e+12, "-5.72291E+12", "%2G"}, + {__LINE__, -5.725325961787777e-25, "-5.72533e-25", "%g"}, + {__LINE__, -5.727138241052646e+19, "-5.72714e+19", "%+#g"}, + {__LINE__, -5.734025840446336e+14, "-5.73403e+14", "%+g"}, + {__LINE__, -5.736469106710259e-05, "-0.000057", "%.6f"}, + {__LINE__, -5.755099153733116e-14, "-5.7551e-14", "%+g"}, + {__LINE__, -5.758411844890947e-20, "-6E-20", "%+6.G"}, + {__LINE__, -5.760350214122813e+03, "-5760.35", "%+g"}, + {__LINE__, -5.768009192512392e-30, "-0", "%1.f"}, + {__LINE__, -5.772343286693103e+07, "-5.77234E+07", "%G"}, + {__LINE__, -5.776693265471852e-27, "-5.776693E-27", "%E"}, + {__LINE__, -5.779682861172754e-28, "-5.779683e-28", "%e"}, + {__LINE__, -5.789953490749936e+26, "-578995349074993637117358957.76656", "%5.5f"}, + {__LINE__, -5.796771264754334e+04, "-57967.7", "%g"}, + {__LINE__, -5.804788370597286e-02, "-5.80479E-02", "%+#1.5E"}, + {__LINE__, -5.805343475329215e-13, "-5.80534e-13", "%g"}, + {__LINE__, -5.809853913830122e+09, "-5809853914", "%+0.f"}, + {__LINE__, -5.817313814570609e-30, " -0", "%7.0f"}, + {__LINE__, -5.831708441750138e+00, "-5.83171", "%#g"}, + {__LINE__, -5.846914550593652e+29, "-5.846915e+29", "%e"}, + {__LINE__, -5.851455214762288e+04, "-58514.6", "%+G"}, + {__LINE__, -5.855803663871393e-23, "-6E-23", "%5.E"}, + {__LINE__, -5.858065129988470e+10, "-5.85807e+10", "%+g"}, + {__LINE__, -5.863147168075411e-14, "-5.86315e-14", "%+g"}, + {__LINE__, -5.878936740266680e-04, "-0.000587894", "%g"}, + {__LINE__, -5.887654893386630e+25, "-58876548933866299506689756.526612", "%#f"}, + {__LINE__, -5.889416409731902e-02, "-0.06", "%5.g"}, + {__LINE__, -5.897630195029892e-18, "-5.897630E-18", "%#0.7G"}, + {__LINE__, -5.898506808456422e-19, "-5.89851E-19", "%G"}, + {__LINE__, -5.901768645393515e+29, "-5.90177E+29", "%+G"}, + {__LINE__, -5.907529093570369e+10, "-59075290935.703695", "%f"}, + {__LINE__, -5.910069365260240e+13, "-59100693652602.39849", "%+#5.5f"}, + {__LINE__, -5.915678929676435e-07, "-0.000001", "%+f"}, + {__LINE__, -5.922232380131716e-01, "-0.592223", "%+3f"}, + {__LINE__, -5.923716473994306e+19, "-5.92372E+19", "%+#.6G"}, + {__LINE__, -5.929950428920404e-15, "-5.929950e-15", "%+e"}, + {__LINE__, -5.949552735665802e+26, "-5.94955e+26", "%#g"}, + {__LINE__, -5.949977081310732e-02, "-0.059500", "%f"}, + {__LINE__, -5.954520303889356e-07, "-5.954520E-07", "%5E"}, + {__LINE__, -5.978861337429145e-13, "-6E-13", "%1.G"}, + {__LINE__, -5.980539445081087e+15, "-5.98054E+15", "%.6G"}, + {__LINE__, -5.981724990853490e-03, " -0.006", "%#7.g"}, + {__LINE__, -5.989488907825821e+03, "-6E+03", "%.2G"}, + {__LINE__, -5.998272294890842e+06, "-6.00E+06", "%4.2E"}, + {__LINE__, -6.023775731455919e-29, "-6.023776e-29", "%e"}, + {__LINE__, -6.028153126666870e-05, "-6.e-05", "%+#6.e"}, + {__LINE__, -6.036200621484690e+25, "-6e+25", "%2.2g"}, + {__LINE__, -6.036400640928137e-14, "-6.036401e-14", "%e"}, + {__LINE__, -6.038198451098329e-20, "-6.038198e-20", "%1.7g"}, + {__LINE__, -6.045778975641123e-15, "-6.04578E-15", "%G"}, + {__LINE__, -6.046038904252073e+00, "-6.04604", "%g"}, + {__LINE__, -6.082158462782175e-06, "-0.000006", "%+#f"}, + {__LINE__, -6.106162726644904e+10, "-6.10616e+10", "%g"}, + {__LINE__, -6.126345215489182e+10, "-6e+10", "%+1.g"}, + {__LINE__, -6.131852309620571e+02, "-6e+02", "%4.e"}, + {__LINE__, -6.134589313243809e-01, "-0.613459", "%+#f"}, + {__LINE__, -6.142358153783462e-10, "-6.14236E-10", "%4G"}, + {__LINE__, -6.151524334856263e+27, "-6151524334856262932191839354.345575", "%+6f"}, + {__LINE__, -6.156818333724550e+04, "-6.e+04", "%#.1g"}, + {__LINE__, -6.166172551433993e+24, "-6166172551433993120895138.4178", "%5.4f"}, + {__LINE__, -6.169532428006014e-09, "-6.16953e-09", "%3g"}, + {__LINE__, -6.172019382601770e-28, "-0.000000", "%+f"}, + {__LINE__, -6.174496985807155e-09, "-6.1745E-09", "%G"}, + {__LINE__, -6.186771845105751e+04, "-61867.7", "%G"}, + {__LINE__, -6.200817973780066e+22, "-6.20082E+22", "%3G"}, + {__LINE__, -6.218004335456301e-22, "-0.000000", "%+f"}, + {__LINE__, -6.219009630546262e-18, "-6.22e-18", "%6.3g"}, + {__LINE__, -6.242532875230618e-06, "-0.000006", "%7f"}, + {__LINE__, -6.243612261280864e+14, "-6.24361e+14", "%.6g"}, + {__LINE__, -6.243689597320126e+10, "-6e+10", "%.0e"}, + {__LINE__, -6.244620667090527e+04, "-6e+04", "%6.e"}, + {__LINE__, -6.250789625572054e-09, "-0.000000", "%+f"}, + {__LINE__, -6.253537566219164e+01, "-63.", "%#1.f"}, + {__LINE__, -6.284420289484166e+21, "-6.2844203e+21", "%7.7e"}, + {__LINE__, -6.285939888202846e+27, "-6e+27", "%+.0g"}, + {__LINE__, -6.290025777678105e+13, "-6E+13", "%2.E"}, + {__LINE__, -6.291364254323924e+10, "-6.3E+10", "%3.2G"}, + {__LINE__, -6.292428812164449e+15, "-6292428812164448.714839", "%.6f"}, + {__LINE__, -6.292600606358002e+29, "-6.29260E+29", "%#G"}, + {__LINE__, -6.292739629144148e-15, "-6.292740E-15", "%E"}, + {__LINE__, -6.296471626465745e+28, "-6.29647E+28", "%G"}, + {__LINE__, -6.299575066640315e+27, "-6.300E+27", "%.3E"}, + {__LINE__, -6.315234679394876e-24, "-6E-24", "%2.G"}, + {__LINE__, -6.320883246383228e-01, "-0.632088", "%2g"}, + {__LINE__, -6.324938413548937e-11, "-6e-11", "%.1g"}, + {__LINE__, -6.336312983176250e+04, "-6.336313e+04", "%e"}, + {__LINE__, -6.341786787214541e+06, "-6.341787E+06", "%+7.6E"}, + {__LINE__, -6.360189291106185e-15, "-0.0000000", "%#.7f"}, + {__LINE__, -6.364818355457748e-15, "-0.0000000", "%1.7f"}, + {__LINE__, -6.366291103431814e+04, "-63662.9", "%#G"}, + {__LINE__, -6.366334042923153e-05, "-6.37e-05", "%5.3g"}, + {__LINE__, -6.370477455143194e-12, "-0.000000", "%f"}, + {__LINE__, -6.372829256546279e+23, "-6.37283E+23", "%G"}, + {__LINE__, -6.375208490193110e-23, "-6.37521e-23", "%4.5e"}, + {__LINE__, -6.382595100891736e-24, "-6.3826E-24", "%5G"}, + {__LINE__, -6.396882421146790e-22, "-6.39688e-22", "%+g"}, + {__LINE__, -6.407190354039938e+08, "-6.40719e+08", "%+g"}, + {__LINE__, -6.408581541430886e-10, "-6.E-10", "%#0.0G"}, + {__LINE__, -6.409206904981703e-03, "-0.006", "%+.0G"}, + {__LINE__, -6.416106714707772e+26, "-641610671470777216285341637.558304", "%f"}, + {__LINE__, -6.417436981761998e-04, "-0.000642", "%+#f"}, + {__LINE__, -6.429997249673124e+11, "-642999724967.312414", "%#f"}, + {__LINE__, -6.440827429825250e+16, "-6.44083E+16", "%G"}, + {__LINE__, -6.444869858444955e+02, "-6e+02", "%6.g"}, + {__LINE__, -6.471701890976228e-25, " -6e-25", "%7.1g"}, + {__LINE__, -6.485393161670371e-12, "-6.485393e-12", "%.6e"}, + {__LINE__, -6.487710907063584e+23, "-6.E+23", "%#3.E"}, + {__LINE__, -6.492942931343439e-08, "-0.", "%+#2.0f"}, + {__LINE__, -6.496010652113223e+18, "-6.49601e+18", "%g"}, + {__LINE__, -6.502856921840228e+22, "-6.50286e+22", "%4g"}, + {__LINE__, -6.523559906055000e-10, "-6.52356E-10", "%.6G"}, + {__LINE__, -6.525899074126662e+04, "-6.5e+04", "%+.1e"}, + {__LINE__, -6.532216801155521e-09, "-6.53222E-09", "%G"}, + {__LINE__, -6.544601787025684e-27, "-0", "%+.0f"}, + {__LINE__, -6.558968312132168e-01, " -1", "%+6.f"}, + {__LINE__, -6.559654261655786e+08, "-6.559654e+08", "%e"}, + {__LINE__, -6.566562622196495e-21, "-6.56656E-21", "%.6G"}, + {__LINE__, -6.573486832071960e+04, "-65734.9", "%G"}, + {__LINE__, -6.573806290918275e+13, "-6.57381E+13", "%G"}, + {__LINE__, -6.578113771674787e+17, "-6.57811E+17", "%+#G"}, + {__LINE__, -6.584106931007506e+15, "-6.584107E+15", "%E"}, + {__LINE__, -6.590538844308877e+07, "-65905388.443089", "%f"}, + {__LINE__, -6.593708834068371e-07, "-0.000001", "%f"}, + {__LINE__, -6.597116446195875e+08, "-6.5971164e+08", "%#1.7e"}, + {__LINE__, -6.605813542127091e-04, "-0.0007", "%+0.g"}, + {__LINE__, -6.607491403866429e+07, "-7e+07", "%3.g"}, + {__LINE__, -6.616499847522278e+09, "-6.61650E+09", "%+#G"}, + {__LINE__, -6.618619768691332e+20, "-7E+20", "%2.G"}, + {__LINE__, -6.626748168962331e-22, "-7.E-22", "%+#3.E"}, + {__LINE__, -6.639335450348280e+12, "-6.63934e+12", "%g"}, + {__LINE__, -6.643910144912576e-03, "-0.006644", "%f"}, + {__LINE__, -6.650293872031870e-18, "-6.65029e-18", "%+#g"}, + {__LINE__, -6.654903858656310e-21, "-0.000000", "%f"}, + {__LINE__, -6.665834666726511e-12, "-6.665835E-12", "%E"}, + {__LINE__, -6.666428729917570e-19, "-0.000000", "%+#f"}, + {__LINE__, -6.668415789681128e+27, "-6.66842E+27", "%+G"}, + {__LINE__, -6.675214676269601e-28, "-6.67521e-28", "%#g"}, + {__LINE__, -6.679560072732262e+01, "-66.7956", "%#g"}, + {__LINE__, -6.692325075457020e-19, "-0.000000", "%1f"}, + {__LINE__, -6.697096655164652e+00, "-6.70", "%#.3g"}, + {__LINE__, -6.712436135928394e-28, "-7e-28", "%0.g"}, + {__LINE__, -6.720769486122685e-16, "-7e-16", "%0.e"}, + {__LINE__, -6.727196898490600e+26, "-672719689849060031860972230.8", "%.1f"}, + {__LINE__, -6.744132627576416e+02, "-674.413", "%6G"}, + {__LINE__, -6.757705576425288e+28, "-6.7577056e+28", "%.7e"}, + {__LINE__, -6.760554929237173e-02, "-0.067606", "%+f"}, + {__LINE__, -6.763538810629361e+10, "-6.7635E+10", "%.4E"}, + {__LINE__, -6.764712008860796e-29, "-6.76471E-29", "%+G"}, + {__LINE__, -6.767299719678443e+04, "-67672.997197", "%f"}, + {__LINE__, -6.768326491352134e+20, "-676832649135213415547.70", "%+.2f"}, + {__LINE__, -6.771116968952891e+03, "-7E+03", "%3.1G"}, + {__LINE__, -6.775218099238350e-30, "-7.e-30", "%+#6.g"}, + {__LINE__, -6.782142689928918e-18, "-0.00", "%4.2f"}, + {__LINE__, -6.795409770512149e+08, "-7E+08", "%+5.0G"}, + {__LINE__, -6.820966157097271e+14, "-6.820966e+14", "%e"}, + {__LINE__, -6.824972990592273e-22, "-0.00", "%+#1.2f"}, + {__LINE__, -6.830452914741750e+29, "-6.8E+29", "%+7.2G"}, + {__LINE__, -6.835797187132348e+10, "-6.8358E+10", "%+5G"}, + {__LINE__, -6.837040829636343e-03, "-0.00683704", "%+g"}, + {__LINE__, -6.852956218658224e+23, "-6.85296e+23", "%3g"}, + {__LINE__, -6.854102607287217e-26, "-7e-26", "%5.e"}, + {__LINE__, -6.865065870249438e+16, "-6.86507E+16", "%#G"}, + {__LINE__, -6.884819522625523e-13, "-6.88482E-13", "%5G"}, + {__LINE__, -6.916316600148513e-12, "-0.000000", "%f"}, + {__LINE__, -6.925312418761560e-05, "-6.92531E-05", "%#G"}, + {__LINE__, -6.929518694178331e+09, "-6929518694.178", "%#.3f"}, + {__LINE__, -6.936008056682024e+02, "-6.936008E+02", "%E"}, + {__LINE__, -6.944911117352400e+26, "-6.944911E+26", "%1.6E"}, + {__LINE__, -6.945829492125162e+05, "-6.9e+05", "%+2.2g"}, + {__LINE__, -6.946603162471856e-13, "-6.94660E-13", "%#G"}, + {__LINE__, -6.947529440406653e+22, "-6.9475e+22", "%+6.5g"}, + {__LINE__, -6.950167604854856e-30, "-0.000000", "%7f"}, + {__LINE__, -6.958122319262799e-26, "-7E-26", "%+1.E"}, + {__LINE__, -6.958978258390961e+16, "-6.95898E+16", "%+5.6G"}, + {__LINE__, -6.963780466334008e-06, "-6.96378E-06", "%2G"}, + {__LINE__, -6.965570896221966e+13, "-6.965571E+13", "%5E"}, + {__LINE__, -6.967741871945064e-10, "-6.96774E-10", "%+#G"}, + {__LINE__, -6.973815739524307e+19, "-69738157395243067782.076", "%6.3f"}, + {__LINE__, -6.982714545008106e+13, "-6.98271e+13", "%g"}, + {__LINE__, -6.985955885054476e+03, "-6985.96", "%6G"}, + {__LINE__, -6.986415266835747e+18, "-6.98642e+18", "%#g"}, + {__LINE__, -6.996461529596512e-08, "-7.0E-08", "%#.1E"}, + {__LINE__, -7.000144914012241e-05, "-7.00014e-05", "%#g"}, + {__LINE__, -7.001017894492730e-03, "-0.00700102", "%+#.6G"}, + {__LINE__, -7.003690685875917e+20, "-7E+20", "%0.G"}, + {__LINE__, -7.010454163965384e+00, "-7.010454e+00", "%+e"}, + {__LINE__, -7.011493550018082e-26, " -0", "%7.f"}, + {__LINE__, -7.018018443394008e+21, "-7.0E+21", "%+#1.1E"}, + {__LINE__, -7.027253777387243e-05, "-7.02725E-05", "%+G"}, + {__LINE__, -7.043679213677740e-17, "-0", "%1.f"}, + {__LINE__, -7.051690489227881e-16, "-7.052e-16", "%0.4g"}, + {__LINE__, -7.063422475274161e-16, "-7.063422E-16", "%E"}, + {__LINE__, -7.065069592235558e-30, "-7.06507e-30", "%+#g"}, + {__LINE__, -7.068265283935834e-29, "-7e-29", "%+5.e"}, + {__LINE__, -7.068748224536295e-13, "-0.000000", "%f"}, + {__LINE__, -7.069621910708177e-04, "-0.000707", "%1.3g"}, + {__LINE__, -7.079725450964823e-12, "-0.000000", "%f"}, + {__LINE__, -7.087255464668218e+17, "-7.1e+17", "%.1e"}, + {__LINE__, -7.088715102009605e-09, "-7e-09", "%6.0g"}, + {__LINE__, -7.090414916270137e-15, "-0.00000", "%.5f"}, + {__LINE__, -7.091868311008428e-03, " -0.0", "%5.1f"}, + {__LINE__, -7.094242165237718e+21, "-7.09424e+21", "%+g"}, + {__LINE__, -7.094324305740325e-05, "-7E-05", "%+3.G"}, + {__LINE__, -7.104544752289144e+04, "-71045.4", "%#g"}, + {__LINE__, -7.105164644813694e-29, "-0.000000", "%+f"}, + {__LINE__, -7.114513164219487e-12, "-7.11451e-12", "%+g"}, + {__LINE__, -7.115778236604137e-12, "-7.11578E-12", "%G"}, + {__LINE__, -7.126217683410162e-27, "-7.126218e-27", "%e"}, + {__LINE__, -7.136214260967733e+28, "-71362142609677334803564008325.338364", "%f"}, + {__LINE__, -7.143452595522573e+19, "-7.14345e+19", "%g"}, + {__LINE__, -7.156042088843537e+27, "-7156042088843537207765166385.7977837", "%4.7f"}, + {__LINE__, -7.173687683710490e+24, "-7.17369e+24", "%6g"}, + {__LINE__, -7.190531162598206e+25, "-71905311625982060164119503", "%4.f"}, + {__LINE__, -7.201301831373747e-09, "-7.2013e-09", "%g"}, + {__LINE__, -7.209601436737306e+10, "-7.2096e+10", "%g"}, + {__LINE__, -7.211058997356655e-06, "-7.211059E-06", "%E"}, + {__LINE__, -7.226756177676272e-09, "-7.22676E-09", "%+G"}, + {__LINE__, -7.230870817566419e+19, "-72308708175664193618.1040364", "%#.7f"}, + {__LINE__, -7.231355992133345e-05, "-7.231356E-05", "%#E"}, + {__LINE__, -7.245999575278407e-12, "-7.246e-12", "%g"}, + {__LINE__, -7.247426403040390e-09, "-7.2474E-09", "%+4.5G"}, + {__LINE__, -7.257348680308567e-12, "-7.3E-12", "%1.2G"}, + {__LINE__, -7.258184256387366e-26, "-7.25818E-26", "%+2G"}, + {__LINE__, -7.262633672900126e+10, "-7.26263e+10", "%#g"}, + {__LINE__, -7.268887462426250e+03, "-7.268887e+03", "%+#e"}, + {__LINE__, -7.275284494682963e+08, "-7.27528e+08", "%g"}, + {__LINE__, -7.276335846256479e-07, " -0", "%4.f"}, + {__LINE__, -7.312556378294452e+15, "-7.31256e+15", "%+6g"}, + {__LINE__, -7.334763752442417e-15, "-0.0000", "%#.4f"}, + {__LINE__, -7.354627097325888e-18, "-7E-18", "%0.E"}, + {__LINE__, -7.360087573342401e-25, "-7.4E-25", "%6.2G"}, + {__LINE__, -7.368533563361098e-29, "-7.4E-29", "%3.1E"}, + {__LINE__, -7.379267723540596e-06, "-0.000007", "%f"}, + {__LINE__, -7.384525781709565e-19, "-0.000000", "%+#f"}, + {__LINE__, -7.388940868679677e-09, "-7.388941E-09", "%E"}, + {__LINE__, -7.391602149099698e+00, "-7.391602E+00", "%E"}, + {__LINE__, -7.393159340196723e+22, "-7.39316e+22", "%#2.6g"}, + {__LINE__, -7.399783260198277e-16, "-7.39978E-16", "%G"}, + {__LINE__, -7.405609590448331e-06, "-7.40561e-06", "%g"}, + {__LINE__, -7.407396977847794e-11, "-7.40740E-11", "%#G"}, + {__LINE__, -7.433716848698637e+20, "-7.43372e+20", "%+g"}, + {__LINE__, -7.444176062769411e-01, "-0.744418", "%#g"}, + {__LINE__, -7.448314697244012e+12, "-7448314697244.011739", "%f"}, + {__LINE__, -7.449533988369018e+08, "-7.449534e+08", "%+#7e"}, + {__LINE__, -7.465682839758801e-01, "-7E-01", "%4.E"}, + {__LINE__, -7.466517843126368e-26, "-7.5E-26", "%.1E"}, + {__LINE__, -7.471385785539900e+24, "-7471385785539899515583783.795591", "%f"}, + {__LINE__, -7.478345583780341e+00, "-7.478346e+00", "%e"}, + {__LINE__, -7.524759284626724e-06, "-7.52476e-06", "%2g"}, + {__LINE__, -7.534831049342539e-19, "-7.53483E-19", "%G"}, + {__LINE__, -7.540546673179273e+15, "-8e+15", "%.1g"}, + {__LINE__, -7.543440436184003e-08, "-7.54344E-08", "%6G"}, + {__LINE__, -7.558224240835564e-06, "-7.55822e-06", "%g"}, + {__LINE__, -7.564132915154469e+23, "-756413291515446850610260.298708", "%+f"}, + {__LINE__, -7.566590720355392e+15, "-7.566591e+15", "%1.6e"}, + {__LINE__, -7.568565927479741e-01, "-7.568566e-01", "%#6e"}, + {__LINE__, -7.615574141973305e-17, "-7.6156e-17", "%+.5g"}, + {__LINE__, -7.641297631509318e-28, "-8e-28", "%+.1g"}, + {__LINE__, -7.661227834163450e+09, "-7661227834.1634497", "%+#.7f"}, + {__LINE__, -7.665934364070126e+11, "-766593436407.0", "%.1f"}, + {__LINE__, -7.682500068463102e-27, "-0.000000", "%+#f"}, + {__LINE__, -7.709363160273798e+15, "-7.70936e+15", "%g"}, + {__LINE__, -7.712596019255238e+26, "-7.7e+26", "%.2g"}, + {__LINE__, -7.730213358437991e+25, "-7.7e+25", "%6.2g"}, + {__LINE__, -7.744420019068976e+05, "-7.74442E+05", "%.5E"}, + {__LINE__, -7.745813181942296e+11, "-7.7458132e+11", "%+.7e"}, + {__LINE__, -7.751004379716307e+16, "-7.751004e+16", "%e"}, + {__LINE__, -7.759862226564527e-28, "-7.75986e-28", "%g"}, + {__LINE__, -7.767838880221207e-27, "-7.76784e-27", "%g"}, + {__LINE__, -7.776301142157532e-19, "-0", "%2.f"}, + {__LINE__, -7.783944257077553e-14, "-8e-14", "%3.g"}, + {__LINE__, -7.801587579382377e+28, "-8e+28", "%+0.g"}, + {__LINE__, -7.806685979552780e+25, "-7.806686E+25", "%E"}, + {__LINE__, -7.825968891331719e-05, "-7.82597E-05", "%G"}, + {__LINE__, -7.826834158664018e+12, "-7.826834E+12", "%+#E"}, + {__LINE__, -7.832536798726886e+24, "-7.83254E+24", "%#1G"}, + {__LINE__, -7.840485340202362e-18, "-0.000000", "%.6f"}, + {__LINE__, -7.843236212099233e+21, "-7.84324e+21", "%g"}, + {__LINE__, -7.906644052019278e-13, "-7.906644E-13", "%E"}, + {__LINE__, -7.908090440678874e-20, "-7.90809E-20", "%#G"}, + {__LINE__, -7.917588593012283e+08, "-7.917589E+08", "%+E"}, + {__LINE__, -7.929100086742454e+29, "-792910008674245414539427656563.930213", "%f"}, + {__LINE__, -7.933235286034290e+26, "-7.933235E+26", "%E"}, + {__LINE__, -7.943431966567875e-03, "-0.0079434", "%+.5g"}, + {__LINE__, -7.950872643572970e-22, "-7.95087e-22", "%2g"}, + {__LINE__, -7.971225555313069e-01, "-0.797123", "%f"}, + {__LINE__, -7.978310272676090e-12, "-0.000000", "%#f"}, + {__LINE__, -8.006805393896414e-29, "-8.00681E-29", "%G"}, + {__LINE__, -8.006906303018830e+25, "-8.00691E+25", "%+G"}, + {__LINE__, -8.008484654476057e+18, "-8.008485e+18", "%5.6e"}, + {__LINE__, -8.008943512904249e+27, "-8008943512904249217854157905.0943", "%+.4f"}, + {__LINE__, -8.016539743121665e+27, "-8.01654E+27", "%G"}, + {__LINE__, -8.018247227006128e-05, " -8E-05", "%7.G"}, + {__LINE__, -8.044391842561705e+21, "-8.04439e+21", "%g"}, + {__LINE__, -8.081902305002036e-02, "-8.081902E-02", "%E"}, + {__LINE__, -8.084193011936438e-03, "-0.0080842", "%+5.5g"}, + {__LINE__, -8.105813534846529e-27, "-0.000000", "%f"}, + {__LINE__, -8.128116892989251e+00, "-8.12812", "%G"}, + {__LINE__, -8.136119035731877e+05, "-8e+05", "%+0.g"}, + {__LINE__, -8.140584224052606e+14, "-8E+14", "%5.G"}, + {__LINE__, -8.145928897003907e+13, "-8.15E+13", "%.3G"}, + {__LINE__, -8.164495905907508e-02, "-0.081645", "%G"}, + {__LINE__, -8.169602113313905e+20, "-8.16960e+20", "%#g"}, + {__LINE__, -8.179181640418333e+24, "-8.E+24", "%#.1G"}, + {__LINE__, -8.184324492264231e-18, "-8.18432E-18", "%G"}, + {__LINE__, -8.186480473850516e-28, "-0.000000", "%+f"}, + {__LINE__, -8.196858948158896e+07, "-8.1969E+07", "%.4E"}, + {__LINE__, -8.213138534699043e-30, "-0.000000", "%f"}, + {__LINE__, -8.221181368676532e-21, "-8.221181E-21", "%#1E"}, + {__LINE__, -8.222605734191438e+16, "-8.22261E+16", "%+G"}, + {__LINE__, -8.223925661091432e-14, "-8.223926e-14", "%0e"}, + {__LINE__, -8.232281541044777e-01, "-0.823228", "%f"}, + {__LINE__, -8.245536794508725e+22, "-8E+22", "%6.G"}, + {__LINE__, -8.270687365305925e+25, "-82706873653059247430974210.146815", "%f"}, + {__LINE__, -8.288978945738204e+11, "-8.28898e+11", "%5g"}, + {__LINE__, -8.290628698333139e+02, "-829.063", "%+G"}, + {__LINE__, -8.292416206056451e-16, "-8.29242e-16", "%+g"}, + {__LINE__, -8.296322623746486e-13, "-8.29632E-13", "%0.6G"}, + {__LINE__, -8.298321941376933e-16, "-0.000000", "%+f"}, + {__LINE__, -8.302331258878365e+29, "-8.30233E+29", "%0G"}, + {__LINE__, -8.309022429255061e+06, "-8309022.429255", "%+f"}, + {__LINE__, -8.312786829254646e-23, "-8.312787e-23", "%e"}, + {__LINE__, -8.313852965178063e-15, "-8.313853e-15", "%#7e"}, + {__LINE__, -8.317529094827869e+16, "-8.31753E+16", "%2G"}, + {__LINE__, -8.322847660882248e-17, "-0.000000", "%f"}, + {__LINE__, -8.324060602752638e-30, "-8.32406e-30", "%#g"}, + {__LINE__, -8.325043500735461e+27, "-8325043500735461066142306663.095951", "%#f"}, + {__LINE__, -8.332432016286935e-02, "-0.0833243", "%g"}, + {__LINE__, -8.334251670232117e-09, "-8.33425e-09", "%g"}, + {__LINE__, -8.338076225141778e+02, "-833.808", "%g"}, + {__LINE__, -8.338349587571534e-23, "-8.33835e-23", "%+2g"}, + {__LINE__, -8.342420812231939e+23, "-834242081223193920891390.00745", "%+0.5f"}, + {__LINE__, -8.347404637940894e+17, "-8.347405e+17", "%+e"}, + {__LINE__, -8.367002601133880e-12, "-0.000000", "%#2.6f"}, + {__LINE__, -8.368579538210858e-17, "-8.36858e-17", "%5g"}, + {__LINE__, -8.392398522472401e-10, "-0.0000", "%.4f"}, + {__LINE__, -8.393029677635258e-29, " -8E-29", "%7.G"}, + {__LINE__, -8.393843230819225e-11, "-8.393843E-11", "%E"}, + {__LINE__, -8.397239871789148e-11, "-8.39724e-11", "%g"}, + {__LINE__, -8.402316358199041e-29, "-0.000", "%3.3f"}, + {__LINE__, -8.402910159034304e-14, "-8.4e-14", "%+0.3g"}, + {__LINE__, -8.414960962391726e+09, "-8414960962.391726", "%#f"}, + {__LINE__, -8.417512471384889e-28, "-8.417512e-28", "%e"}, + {__LINE__, -8.418719240222911e+21, "-8418719240222911182058.851409", "%f"}, + {__LINE__, -8.421210956044195e-09, "-0.00000", "%.5f"}, + {__LINE__, -8.422001733529095e+24, "-8E+24", "%0.G"}, + {__LINE__, -8.443614453772469e+21, "-8.44361E+21", "%+G"}, + {__LINE__, -8.445883044175737e-19, "-0", "%+1.f"}, + {__LINE__, -8.460545304711022e-01, "-0.846055", "%G"}, + {__LINE__, -8.470462532754567e+12, "-8.47E+12", "%6.3G"}, + {__LINE__, -8.478488782426476e-07, "-8.478489E-07", "%E"}, + {__LINE__, -8.478657582923923e+09, "-8.5e+09", "%.2g"}, + {__LINE__, -8.482732098043366e+12, "-8482732098043", "%5.f"}, + {__LINE__, -8.489112373854447e+07, "-8.489e+07", "%#3.3e"}, + {__LINE__, -8.494153144629106e+02, "-849.415", "%+g"}, + {__LINE__, -8.496331191616493e+28, "-8E+28", "%+6.G"}, + {__LINE__, -8.503711894495339e+05, "-850371", "%g"}, + {__LINE__, -8.505271893964268e+14, "-8.505272E+14", "%E"}, + {__LINE__, -8.509873985358741e+15, "-9.e+15", "%#7.g"}, + {__LINE__, -8.518336897898081e-20, "-8.51834E-20", "%G"}, + {__LINE__, -8.529588044136128e+21, "-8.529588E+21", "%E"}, + {__LINE__, -8.539706586811079e-23, "-8.53971E-23", "%G"}, + {__LINE__, -8.545924619642008e+10, "-8.545925e+10", "%6e"}, + {__LINE__, -8.572893681637353e+09, "-8.57289E+09", "%G"}, + {__LINE__, -8.585483717692345e+29, "-8.58548E+29", "%+G"}, + {__LINE__, -8.586092014292309e+27, "-8.59e+27", "%.2e"}, + {__LINE__, -8.604716827086833e-26, "-8.60472e-26", "%g"}, + {__LINE__, -8.621349224186615e-04, "-0.000862", "%f"}, + {__LINE__, -8.626097065201103e-21, " -0", "%5.f"}, + {__LINE__, -8.631700514212522e-16, "-8.631701E-16", "%+E"}, + {__LINE__, -8.634703359975640e-09, "-0.000000", "%+5f"}, + {__LINE__, -8.639393025443855e-17, "-8.63939e-17", "%g"}, + {__LINE__, -8.640641942556812e-11, "-8.64064e-11", "%#g"}, + {__LINE__, -8.646260250525495e-09, "-0.000", "%.3f"}, + {__LINE__, -8.667322015806242e+09, "-8667322015.806242", "%+#f"}, + {__LINE__, -8.690705655554451e+10, "-8.690706E+10", "%#E"}, + {__LINE__, -8.714299893763934e-07, "-8.7143E-07", "%.5G"}, + {__LINE__, -8.715729756264988e+27, "-8.71573E+27", "%+.6G"}, + {__LINE__, -8.716518487180945e+16, "-9e+16", "%3.e"}, + {__LINE__, -8.744404488756237e+25, "-87444044887562366596966967.335902", "%4f"}, + {__LINE__, -8.754720498369242e+13, "-8.75472e+13", "%g"}, + {__LINE__, -8.763198097931010e-19, "-8.76320e-19", "%#g"}, + {__LINE__, -8.779007408852458e+28, "-8.77901E+28", "%G"}, + {__LINE__, -8.794576202907937e+11, "-8.79458E+11", "%#2.6G"}, + {__LINE__, -8.799091520612418e-11, "-8.799E-11", "%3.4G"}, + {__LINE__, -8.799246265524921e+11, "-8.79925E+11", "%G"}, + {__LINE__, -8.809010508550436e-04, "-8.8090E-04", "%.4E"}, + {__LINE__, -8.817389999872653e-10, "-8.81739e-10", "%g"}, + {__LINE__, -8.842022428985267e-28, "-8.842022E-28", "%E"}, + {__LINE__, -8.851265987586864e-07, "-0.000001", "%f"}, + {__LINE__, -8.861985131432196e+03, "-8861.985", "%+.7G"}, + {__LINE__, -8.874449013455113e-29, "-8.874449e-29", "%e"}, + {__LINE__, -8.878752609483453e+18, "-8.87875E+18", "%G"}, + {__LINE__, -8.882501665315313e-04, "-0.00088825", "%+G"}, + {__LINE__, -8.892056241505784e-14, "-8.89206E-14", "%G"}, + {__LINE__, -8.895327674961603e-30, "-8.89533E-30", "%G"}, + {__LINE__, -8.900059462213669e-30, "-8.900059E-30", "%+#E"}, + {__LINE__, -8.957100548759815e+18, "-9e+18", "%3.0e"}, + {__LINE__, -8.961352369869012e-30, "-8.961352e-30", "%+#2e"}, + {__LINE__, -8.962296474587800e+02, "-896", "%4.f"}, + {__LINE__, -8.962536469328530e-12, "-8.96254E-12", "%G"}, + {__LINE__, -8.963299420391932e+21, "-8.9633e+21", "%g"}, + {__LINE__, -8.967736672941522e-11, "-8.96774e-11", "%g"}, + {__LINE__, -8.994077362462679e+11, "-8.9940774e+11", "%+#.7e"}, + {__LINE__, -9.000452862345622e+15, "-9e+15", "%2.e"}, + {__LINE__, -9.007489135361462e+07, "-9.0075E+07", "%+2.4E"}, + {__LINE__, -9.036271805879910e+16, "-9.03627E+16", "%G"}, + {__LINE__, -9.036319073700248e+22, "-9.03632e+22", "%1g"}, + {__LINE__, -9.043207635989237e+11, "-9.043208e+11", "%#e"}, + {__LINE__, -9.043856598625815e-26, "-9e-26", "%+1.g"}, + {__LINE__, -9.052760561065698e+14, "-9.052761E+14", "%E"}, + {__LINE__, -9.066480297957160e-06, "-9E-06", "%+1.G"}, + {__LINE__, -9.070971376440752e-01, "-0.907097", "%g"}, + {__LINE__, -9.073301831888273e+09, "-9.07330E+09", "%#G"}, + {__LINE__, -9.091228662702147e+28, "-9E+28", "%4.G"}, + {__LINE__, -9.093652024983832e+04, "-9.093652E+04", "%7.6E"}, + {__LINE__, -9.098945831139295e-04, "-9.098946E-04", "%+1.6E"}, + {__LINE__, -9.124981449916591e+05, "-912498", "%G"}, + {__LINE__, -9.132038703003394e-02, "-0.0913204", "%#G"}, + {__LINE__, -9.138271437433964e-04, "-0.000913827", "%5g"}, + {__LINE__, -9.158186427463699e-04, "-0.000915819", "%g"}, + {__LINE__, -9.160846358172526e+16, "-9.160846E+16", "%#E"}, + {__LINE__, -9.160884330810163e+17, "-9E+17", "%+5.G"}, + {__LINE__, -9.190271387375542e+21, "-9.19027e+21", "%+g"}, + {__LINE__, -9.191651872412619e-04, "-9.191652E-04", "%E"}, + {__LINE__, -9.196426161984341e-10, "-9E-10", "%4.G"}, + {__LINE__, -9.201074139774962e+09, "-9E+09", "%4.G"}, + {__LINE__, -9.223294284802762e+09, "-9.22329E+09", "%G"}, + {__LINE__, -9.224740266754996e+02, "-9E+02", "%6.0G"}, + {__LINE__, -9.231583780512302e-01, " -0.9", "%6.g"}, + {__LINE__, -9.241998826543689e+07, "-92419988", "%1.f"}, + {__LINE__, -9.243603351372128e+27, "-9.243603e+27", "%#e"}, + {__LINE__, -9.255922697415071e+11, "-9.25592e+11", "%g"}, + {__LINE__, -9.265734968114124e-02, "-0.092657", "%f"}, + {__LINE__, -9.273132068521061e+11, "-9.27313E+11", "%G"}, + {__LINE__, -9.276017912826685e+27, "-9.E+27", "%+#5.G"}, + {__LINE__, -9.279781279788505e+17, "-9.27978E+17", "%7G"}, + {__LINE__, -9.305057549822465e-19, "-9.30506E-19", "%5G"}, + {__LINE__, -9.313182384508469e+18, "-9.31318E+18", "%G"}, + {__LINE__, -9.313350925182641e-12, "-9.31E-12", "%.3G"}, + {__LINE__, -9.362202657509130e+08, "-9e+08", "%.1g"}, + {__LINE__, -9.367344757598381e+19, "-9.36734e+19", "%g"}, + {__LINE__, -9.374222399774300e+26, "-9.37422e+26", "%4g"}, + {__LINE__, -9.385734023305105e+02, "-9.385734e+02", "%+0e"}, + {__LINE__, -9.392042570618909e+17, "-9.39204E+17", "%G"}, + {__LINE__, -9.395742939311926e+20, "-939574293931192627837.8451", "%.4f"}, + {__LINE__, -9.409066155962469e+09, "-9.40907E+09", "%#G"}, + {__LINE__, -9.412851077799790e+23, "-9.412851E+23", "%E"}, + {__LINE__, -9.431808019049818e+08, "-9.43181e+08", "%g"}, + {__LINE__, -9.465236900519924e-27, "-9.46524e-27", "%g"}, + {__LINE__, -9.478330386415852e+17, "-9.478330e+17", "%e"}, + {__LINE__, -9.510393719697428e+15, "-9.51039E+15", "%+G"}, + {__LINE__, -9.534229599909890e-09, "-0.000000", "%f"}, + {__LINE__, -9.541731189284823e-17, "-1.E-16", "%+#7.G"}, + {__LINE__, -9.548861908445902e+24, "-9548861908445902102471336.547751", "%f"}, + {__LINE__, -9.578447281451794e+14, "-957844728145179", "%5.f"}, + {__LINE__, -9.580512736933464e+11, "-9.58051E+11", "%G"}, + {__LINE__, -9.596838169770637e-28, "-1E-27", "%+6.G"}, + {__LINE__, -9.597528852877852e-22, "-1e-21", "%2.1g"}, + {__LINE__, -9.599943658427051e+02, "-9.599944E+02", "%E"}, + {__LINE__, -9.603227991185260e-14, "-1E-13", "%+0.E"}, + {__LINE__, -9.606818733892343e+08, "-960681873.389", "%1.3f"}, + {__LINE__, -9.617442692720937e+16, "-9.61744E+16", "%G"}, + {__LINE__, -9.628685516961109e+29, "-9.62869e+29", "%3g"}, + {__LINE__, -9.638354750455226e-11, "-0.000000", "%f"}, + {__LINE__, -9.644266379029137e+16, "-96442663790291369.707575", "%f"}, + {__LINE__, -9.645563533575482e+06, "-1E+07", "%1.G"}, + {__LINE__, -9.645993536742941e-29, "-9.64599e-29", "%+g"}, + {__LINE__, -9.647147223548563e+00, "-9.647147e+00", "%e"}, + {__LINE__, -9.671225879906396e-11, "-0", "%0.f"}, + {__LINE__, -9.675000471729906e-09, "-9.675000E-09", "%E"}, + {__LINE__, -9.683992878297159e-26, "-9.68e-26", "%2.2e"}, + {__LINE__, -9.695491325144765e+10, "-9.69549E+10", "%#G"}, + {__LINE__, -9.695661526907136e-24, " -0.", "%#6.f"}, + {__LINE__, -9.703124881970338e+07, "-9.703e+07", "%0.4g"}, + {__LINE__, -9.707701257031864e+26, "-970770125703186437249314622.022212", "%f"}, + {__LINE__, -9.709879792694702e+09, "-9.709880E+09", "%E"}, + {__LINE__, -9.736052226907940e+26, "-9.73605e+26", "%+#g"}, + {__LINE__, -9.745037157520795e+07, "-9.74504e+07", "%#5.6g"}, + {__LINE__, -9.757293950670832e-29, "-9.757E-29", "%0.4G"}, + {__LINE__, -9.762126832689880e+12, "-9.76213E+12", "%G"}, + {__LINE__, -9.801328150341989e-01, " -1", "%7.G"}, + {__LINE__, -9.837332964629669e-20, "-9.83733E-20", "%0G"}, + {__LINE__, -9.837790327283010e+23, "-1E+24", "%.1G"}, + {__LINE__, -9.839636176463729e+23, "-9.83964E+23", "%#6.5E"}, + {__LINE__, -9.841454250618272e+11, "-9.84E+11", "%+2.3G"}, + {__LINE__, -9.852920805249280e-12, "-9.852921e-12", "%.7g"}, + {__LINE__, -9.854092850182351e+12, "-9.85409e+12", "%0g"}, + {__LINE__, -9.866796026839712e+02, "-9.9E+02", "%#.1E"}, + {__LINE__, -9.867413260289803e-21, "-1e-20", "%.1g"}, + {__LINE__, -9.882136052131983e-08, "-9.88214e-08", "%g"}, + {__LINE__, -9.882755697941866e+20, "-9.882756E+20", "%E"}, + {__LINE__, -9.897275811087050e+20, "-9.9E+20", "%0.3G"}, + {__LINE__, -9.899860651812364e-04, "-9.899861e-04", "%e"}, + {__LINE__, -9.906754113555550e-11, "-9.90675e-11", "%3g"}, + {__LINE__, -9.926067557389940e+11, "-9.92607E+11", "%G"}, + {__LINE__, -9.929638609014063e+17, "-992963860901406292.214233", "%1f"}, + {__LINE__, -9.941214552193284e-24, "-0.000000", "%+#f"}, + {__LINE__, -9.942875240256573e+14, "-9.942875e+14", "%+1e"}, + {__LINE__, -9.944492909695798e-01, "-9.944E-01", "%.3E"}, + {__LINE__, -9.954710507359939e-27, "-1e-26", "%+0.2g"}, + {__LINE__, -9.955410507742851e+03, "-9955.41", "%+G"}, + {__LINE__, -9.974437672251590e+02, "-997.444", "%#G"}, + {__LINE__, -9.975683165288929e-26, "-9.97568E-26", "%6G"}, + {__LINE__, 1.003827370583415e+06, "1003827.370583", "%3f"}, + {__LINE__, 1.004189065268560e-27, "1.004189E-27", "%.7G"}, + {__LINE__, 1.005840059175462e-28, "+1.00584E-28", "%+G"}, + {__LINE__, 1.006586790090557e-12, "0.000000", "%#f"}, + {__LINE__, 1.007598594773359e+18, "1.0076E+18", "%G"}, + {__LINE__, 1.007902307001894e+29, "1.0079e+29", "%g"}, + {__LINE__, 1.008607405036580e-15, "1.00861E-15", "%6G"}, + {__LINE__, 1.008769102466771e+16, "+1.00877E+16", "%+G"}, + {__LINE__, 1.009545698372534e-08, "+1E-08", "%+0.0G"}, + {__LINE__, 1.010173673945099e-12, "+1.01017e-12", "%+#0g"}, + {__LINE__, 1.011375000606627e+09, "1011375000.606627", "%f"}, + {__LINE__, 1.011944652221650e+15, "1011944652221649.98", "%3.2f"}, + {__LINE__, 1.013057788090379e-21, "0.000000", "%#f"}, + {__LINE__, 1.014525729953198e+15, "1.01453e+15", "%3g"}, + {__LINE__, 1.016760538234615e-21, "+1.016761E-21", "%+3E"}, + {__LINE__, 1.017052611781237e-29, "0.000000", "%f"}, + {__LINE__, 1.018471223361229e+14, "1.01847E+14", "%G"}, + {__LINE__, 1.021646959220873e+15, "1021646959220872.6", "%4.1f"}, + {__LINE__, 1.022304833976590e+27, "1.0223e+27", "%g"}, + {__LINE__, 1.022500312067325e-12, "1.0225e-12", "%6g"}, + {__LINE__, 1.023019285462627e-20, "0.000000", "%f"}, + {__LINE__, 1.024192579400824e+06, "1.02419e+06", "%g"}, + {__LINE__, 1.024517953555766e-28, "1.02452E-28", "%G"}, + {__LINE__, 1.026673589339585e+21, "1.02667E+21", "%G"}, + {__LINE__, 1.027231090958880e-19, "1.E-19", "%#3.G"}, + {__LINE__, 1.028249904956487e-27, "1E-27", "%5.G"}, + {__LINE__, 1.028570144661448e-28, "+1.02857E-28", "%+G"}, + {__LINE__, 1.028934898454705e-13, "+1.028935E-13", "%+E"}, + {__LINE__, 1.029071053545906e-26, "1.029071E-26", "%E"}, + {__LINE__, 1.032672895881148e-30, "+1.032673E-30", "%+E"}, + {__LINE__, 1.033502358979511e+06, "1.0335E+06", "%.5G"}, + {__LINE__, 1.036790427717847e+19, "1e+19", "%4.g"}, + {__LINE__, 1.037022928821648e+03, "1037.02", "%5g"}, + {__LINE__, 1.040551683941284e+14, "1.04055E+14", "%G"}, + {__LINE__, 1.044499252101308e-17, "1.044499E-17", "%E"}, + {__LINE__, 1.045791899570889e+15, "+1.04579e+15", "%+g"}, + {__LINE__, 1.048730383198703e+28, "1.049e+28", "%.4g"}, + {__LINE__, 1.050654419882509e-18, "0.000000", "%f"}, + {__LINE__, 1.052600569262127e-27, "+0.000", "%+2.3f"}, + {__LINE__, 1.053213014784149e+15, "1E+15", "%4.G"}, + {__LINE__, 1.057553916225948e-15, "+0.000000", "%+4f"}, + {__LINE__, 1.058191244482453e+19, "1.058e+19", "%.3e"}, + {__LINE__, 1.058746269793972e-26, "+1.1E-26", "%+.2G"}, + {__LINE__, 1.058793271960762e-01, "+1.058793E-01", "%+4E"}, + {__LINE__, 1.058869592738370e-19, "1.05887e-19", "%3g"}, + {__LINE__, 1.060402312803008e+15, "1060402312803008.229025", "%f"}, + {__LINE__, 1.061106284763095e+00, "1.061106", "%0f"}, + {__LINE__, 1.061180160987467e+09, "1e+09", "%2.g"}, + {__LINE__, 1.061181537176067e-06, "+1.06118e-06", "%+g"}, + {__LINE__, 1.065021381771174e+20, "1.06502e+20", "%g"}, + {__LINE__, 1.065464040401130e-16, "1.065464e-16", "%e"}, + {__LINE__, 1.065573419819459e-05, "0.00001", "%.5f"}, + {__LINE__, 1.066107764306644e+13, "1.066108e+13", "%#e"}, + {__LINE__, 1.071318759598956e+12, "1.071319e+12", "%e"}, + {__LINE__, 1.071564727222321e+10, "1.072E+10", "%3.4G"}, + {__LINE__, 1.074652481218139e+14, "1.0747E+14", "%.4E"}, + {__LINE__, 1.078453684911309e-02, "0.0107845", "%g"}, + {__LINE__, 1.078550811446675e-05, "1.07855e-05", "%g"}, + {__LINE__, 1.078780494515273e-04, "0.000107878", "%4G"}, + {__LINE__, 1.079467488876157e-16, "+1.079467E-16", "%+E"}, + {__LINE__, 1.080529722917934e-20, "0.000000", "%f"}, + {__LINE__, 1.082169127709439e+01, "10.8217", "%#5g"}, + {__LINE__, 1.087241285590951e+22, "1E+22", "%4.G"}, + {__LINE__, 1.091049208406195e-14, "0.0000", "%.4f"}, + {__LINE__, 1.095316064213237e+16, "+1e+16", "%+0.g"}, + {__LINE__, 1.095349083237975e-04, "1.095349e-04", "%5e"}, + {__LINE__, 1.098002465452836e+12, "1098002465452.836205", "%f"}, + {__LINE__, 1.098445764138997e+13, "1.09845E+13", "%G"}, + {__LINE__, 1.099968047892474e-13, "1.1e-13", "%2.5g"}, + {__LINE__, 1.101017598311363e-04, "0.000110102", "%G"}, + {__LINE__, 1.102776332077817e+18, "1.103e+18", "%.3e"}, + {__LINE__, 1.103734053887830e-01, "0.110373", "%f"}, + {__LINE__, 1.105730106869006e-03, "0.00110573", "%g"}, + {__LINE__, 1.106366781854058e-10, "1.10637e-10", "%g"}, + {__LINE__, 1.107354763213839e+15, "1.107355e+15", "%6e"}, + {__LINE__, 1.108151709609825e-10, "+1.10815E-10", "%+G"}, + {__LINE__, 1.110054501993647e+21, "+1.11005E+21", "%+G"}, + {__LINE__, 1.111574814737873e-17, "+1.112E-17", "%+1.4G"}, + {__LINE__, 1.111865512360108e-15, "1E-15", "%.0G"}, + {__LINE__, 1.112281568330498e-03, "1.112282e-03", "%.6e"}, + {__LINE__, 1.112625415174238e+21, "+1.11263e+21", "%+g"}, + {__LINE__, 1.112744974910395e-02, "0.011127", "%f"}, + {__LINE__, 1.114726566979885e-10, "1.11473e-10", "%g"}, + {__LINE__, 1.115395843055731e-20, "+0.000000", "%+6f"}, + {__LINE__, 1.115983414693121e+15, "1.11598e+15", "%5.6g"}, + {__LINE__, 1.116118842074266e+27, "1.116119e+27", "%e"}, + {__LINE__, 1.118386983012328e+19, "11183869830123276400.968280", "%1f"}, + {__LINE__, 1.119344607892718e-22, "1.119345e-22", "%e"}, + {__LINE__, 1.120560837110279e-19, "0.000000", "%f"}, + {__LINE__, 1.123620157906291e-02, "0.0112362", "%G"}, + {__LINE__, 1.126804857639478e+29, "1.1268E+29", "%#0.5G"}, + {__LINE__, 1.128962199461581e+11, "1.12896e+11", "%g"}, + {__LINE__, 1.129878714246809e-16, "1E-16", "%0.0G"}, + {__LINE__, 1.130331982972407e-06, "+0.", "%+#1.f"}, + {__LINE__, 1.134167332070028e-16, "0", "%0.0f"}, + {__LINE__, 1.134248080053598e-28, "0.000000", "%f"}, + {__LINE__, 1.136023160708119e-10, "+0.000000", "%+1f"}, + {__LINE__, 1.136167141059036e+23, "113616714105903593434732.101741", "%f"}, + {__LINE__, 1.136439499920164e+10, "+1.13644E+10", "%+G"}, + {__LINE__, 1.136859734932182e+24, "1.13686e+24", "%g"}, + {__LINE__, 1.138985939448731e+09, "1.13899e+09", "%2g"}, + {__LINE__, 1.139287324790407e-25, "1e-25", "%.1g"}, + {__LINE__, 1.140559912153251e+06, "1.14056e+06", "%g"}, + {__LINE__, 1.141318036027086e-26, "1.14132E-26", "%G"}, + {__LINE__, 1.142586049368452e-20, "+1.14259E-20", "%+7.5E"}, + {__LINE__, 1.143283863993333e-17, "1.143284E-17", "%3E"}, + {__LINE__, 1.144637911632432e-28, "1.144638E-28", "%#3E"}, + {__LINE__, 1.145791416532065e-10, "1.14579e-10", "%g"}, + {__LINE__, 1.146958047315780e-17, "1.146958e-17", "%e"}, + {__LINE__, 1.147898346886569e-08, "1E-08", "%3.0E"}, + {__LINE__, 1.149724744965608e-14, "1.14972e-14", "%5g"}, + {__LINE__, 1.155383721940441e-10, "1e-10", "%1.g"}, + {__LINE__, 1.157066617729094e-02, "0", "%1.f"}, + {__LINE__, 1.157893614537215e+05, "1.157894E+05", "%E"}, + {__LINE__, 1.160476344451928e-19, "1.2e-19", "%.1e"}, + {__LINE__, 1.161610499315966e-26, "1.161610E-26", "%E"}, + {__LINE__, 1.162345042181490e+29, "116234504218148981813385489658.685401", "%5.6f"}, + {__LINE__, 1.162376053722862e+23, "1.16238e+23", "%g"}, + {__LINE__, 1.162996084782483e+18, "+1e+18", "%+0.g"}, + {__LINE__, 1.163544861478966e+26, "1.16354E+26", "%G"}, + {__LINE__, 1.167300137450931e+06, "1E+06", "%.0G"}, + {__LINE__, 1.168693829096401e-30, "+1.16869e-30", "%+g"}, + {__LINE__, 1.172791369381396e+15, "1.173E+15", "%.4G"}, + {__LINE__, 1.174427110782028e-29, " 1E-29", "%7.G"}, + {__LINE__, 1.179498322112450e+27, "1179498322112449759657692993.641831", "%f"}, + {__LINE__, 1.179555606293547e+27, "1.17956e+27", "%3g"}, + {__LINE__, 1.180815814881269e+06, "1.180816e+06", "%e"}, + {__LINE__, 1.181494870072805e+20, "1.181495e+20", "%5e"}, + {__LINE__, 1.181524119225619e+12, "+1181524119225.61917", "%+5.5f"}, + {__LINE__, 1.182111212289243e-20, "1e-20", "%4.g"}, + {__LINE__, 1.184503315019769e-27, "1.1845e-27", "%7g"}, + {__LINE__, 1.186413646767670e+29, "1.18641e+29", "%g"}, + {__LINE__, 1.186852938885004e-13, "1.18685e-13", "%4g"}, + {__LINE__, 1.188837612473914e+05, "1e+05", "%.0g"}, + {__LINE__, 1.190317482928293e-29, "1.190317e-29", "%e"}, + {__LINE__, 1.190527491143987e+02, "1.190527E+02", "%E"}, + {__LINE__, 1.191823062060233e-30, "1.2E-30", "%6.2G"}, + {__LINE__, 1.191926974812428e-23, "1.2e-23", "%.2g"}, + {__LINE__, 1.193549622366544e-25, "1E-25", "%4.1G"}, + {__LINE__, 1.195517368629765e-21, "1.195517e-21", "%1.7g"}, + {__LINE__, 1.195573061651289e-27, "1.19557e-27", "%#g"}, + {__LINE__, 1.195806681188325e-03, "+0.00120", "%+#.3G"}, + {__LINE__, 1.196842275192189e-28, "1.19684e-28", "%g"}, + {__LINE__, 1.197307550969576e+04, "1.1973E+04", "%.4E"}, + {__LINE__, 1.197608525847607e+15, "1197608525847606.8062805", "%4.7f"}, + {__LINE__, 1.197801338159407e+04, "11978", "%5G"}, + {__LINE__, 1.197813329735025e+27, "1.19781E+27", "%7G"}, + {__LINE__, 1.199483904123253e-12, "1.19948e-12", "%g"}, + {__LINE__, 1.199535292169766e-09, "1.199535e-09", "%e"}, + {__LINE__, 1.201478067515135e+05, "120147.806752", "%f"}, + {__LINE__, 1.202054662662158e+17, "1.20205E+17", "%#G"}, + {__LINE__, 1.202111922368321e+07, "+1.202112E+07", "%+#E"}, + {__LINE__, 1.202536892743791e-01, "0.120254", "%#g"}, + {__LINE__, 1.205698674119882e-29, "1.2057E-29", "%G"}, + {__LINE__, 1.208091664701942e-13, " 0.", "%#3.f"}, + {__LINE__, 1.208942413954872e-17, "1.20894E-17", "%G"}, + {__LINE__, 1.209704891496613e+10, "1.210e+10", "%2.3e"}, + {__LINE__, 1.211355639334831e+29, "1E+29", "%.0G"}, + {__LINE__, 1.211447553804203e+17, " 1E+17", "%7.E"}, + {__LINE__, 1.212481583429790e-16, "1.212482e-16", "%e"}, + {__LINE__, 1.212849001551862e+26, "121284900155186181613942153.4899157", "%6.7f"}, + {__LINE__, 1.213057538677801e+06, "1213057.538678", "%#.6f"}, + {__LINE__, 1.214609592911893e+25, "1.214610e+25", "%#e"}, + {__LINE__, 1.216086266251665e+01, "1e+01", "%5.e"}, + {__LINE__, 1.216468565931026e-22, "1.216469E-22", "%1E"}, + {__LINE__, 1.216503126598765e+14, "+1E+14", "%+0.E"}, + {__LINE__, 1.216641013695553e-14, "0.000000", "%f"}, + {__LINE__, 1.223142246779504e-23, "0.000000", "%f"}, + {__LINE__, 1.223884786443274e+08, "122388479", "%5.f"}, + {__LINE__, 1.224012562770076e+29, "1.22401e+29", "%0g"}, + {__LINE__, 1.225098157528297e+14, "1.22510e+14", "%#g"}, + {__LINE__, 1.226306848505242e-22, "1.22631E-22", "%G"}, + {__LINE__, 1.227487485547996e-08, "+1.22749e-08", "%+g"}, + {__LINE__, 1.237206486909689e+02, "+123.721", "%+#.6g"}, + {__LINE__, 1.240281126177077e-02, "+0.01", "%+3.g"}, + {__LINE__, 1.241197211306994e-06, "1.241197E-06", "%E"}, + {__LINE__, 1.241776311097410e+07, "1.241776E+07", "%E"}, + {__LINE__, 1.245995986024999e-03, "0.001246", "%g"}, + {__LINE__, 1.246082428199607e+03, "1246.082428", "%f"}, + {__LINE__, 1.247021567954220e+05, " 1e+05", "%7.g"}, + {__LINE__, 1.248140089618390e-30, "+1.24814E-30", "%+G"}, + {__LINE__, 1.248403361652064e-30, "+0.000000", "%+f"}, + {__LINE__, 1.251098538918842e-26, "+1.251099E-26", "%+E"}, + {__LINE__, 1.251952633022061e+25, "1.25195e+25", "%g"}, + {__LINE__, 1.253029705843461e-04, "+1.E-04", "%+#4.E"}, + {__LINE__, 1.259562132729488e-09, "1e-09", "%1.e"}, + {__LINE__, 1.261702205492260e-27, "0.000000", "%f"}, + {__LINE__, 1.265910361926660e-19, "+1.265910e-19", "%+e"}, + {__LINE__, 1.267411912651087e+12, "1.26741E+12", "%1G"}, + {__LINE__, 1.267830718285980e-10, "1.26783e-10", "%g"}, + {__LINE__, 1.268238378987517e-23, "1E-23", "%5.E"}, + {__LINE__, 1.275543253354167e+10, "1.27554E+10", "%G"}, + {__LINE__, 1.277255234454465e-10, "1.27726E-10", "%5G"}, + {__LINE__, 1.280590949834150e-21, "1.280591E-21", "%E"}, + {__LINE__, 1.285570453788242e-02, "1.285570E-02", "%E"}, + {__LINE__, 1.287712130719031e+09, "1287712130.719031", "%#f"}, + {__LINE__, 1.290142876187105e+29, "+1.290143E+29", "%+E"}, + {__LINE__, 1.293129213191961e+21, "1.29313E+21", "%G"}, + {__LINE__, 1.293317101334714e-23, "+0.000000", "%+f"}, + {__LINE__, 1.294506813069071e-13, "0.0", "%3.1f"}, + {__LINE__, 1.295576232685739e-22, "+0.000000", "%+f"}, + {__LINE__, 1.296010470431800e-19, "+0.000000", "%+6f"}, + {__LINE__, 1.298798231947674e+19, "1.2988E+19", "%2G"}, + {__LINE__, 1.299521114379381e+09, "1.29952E+09", "%G"}, + {__LINE__, 1.299847637892712e+24, "1.299848e+24", "%e"}, + {__LINE__, 1.301269777843698e+29, "130126977784369834978406288428.232074", "%f"}, + {__LINE__, 1.302911786437618e-15, " 0", "%6.f"}, + {__LINE__, 1.303863159906719e+27, "1303863159906719415559450681", "%0.f"}, + {__LINE__, 1.304396472326846e-12, "+1E-12", "%+1.0G"}, + {__LINE__, 1.306180443528427e-30, "+1e-30", "%+.1g"}, + {__LINE__, 1.308469138968514e+22, "1.30847E+22", "%0G"}, + {__LINE__, 1.312700156826057e-11, "1.3127e-11", "%g"}, + {__LINE__, 1.315364075120764e+21, "+1.31536E+21", "%+G"}, + {__LINE__, 1.320376485288444e-09, "+1.3204E-09", "%+6.5G"}, + {__LINE__, 1.321434154364635e-08, "1.32143e-08", "%1g"}, + {__LINE__, 1.322777272579176e+19, "+1e+19", "%+3.e"}, + {__LINE__, 1.322861663428564e+00, "1.322862E+00", "%#E"}, + {__LINE__, 1.323566013489230e+14, "+1.323566E+14", "%+1E"}, + {__LINE__, 1.327446904132973e-20, "+1.32745E-20", "%+4.6G"}, + {__LINE__, 1.328226362528158e+19, "1e+19", "%2.g"}, + {__LINE__, 1.329167460803610e+08, "1e+08", "%3.g"}, + {__LINE__, 1.335371420823160e+14, "1.33537E+14", "%7G"}, + {__LINE__, 1.336116185095935e-21, "0.000000", "%f"}, + {__LINE__, 1.338766774704671e-22, "+1.33877e-22", "%+g"}, + {__LINE__, 1.339199373014647e-12, "1E-12", "%1.E"}, + {__LINE__, 1.344293357481519e+26, "134429335748151945750805680.290796", "%#2f"}, + {__LINE__, 1.344328188122843e+29, "1.34433e+29", "%g"}, + {__LINE__, 1.344382581316972e-05, "1E-05", "%5.0E"}, + {__LINE__, 1.349835616116184e+11, "134983561611.6", "%2.1f"}, + {__LINE__, 1.349955669989707e+20, "134995566998970728533.890906", "%7f"}, + {__LINE__, 1.350230411093841e+20, "135023041109384145386.229648", "%f"}, + {__LINE__, 1.353391369565576e+10, " 1E+10", "%7.1G"}, + {__LINE__, 1.356889483699471e+22, "+1.35689E+22", "%+G"}, + {__LINE__, 1.358141775798243e+24, "+1358141775798242578687791.137781", "%+f"}, + {__LINE__, 1.358547541975523e-15, "1.35855e-15", "%g"}, + {__LINE__, 1.363402539800791e+11, "1.3634E+11", "%G"}, + {__LINE__, 1.363676837988798e+16, "+13636768379887982.4053", "%+2.4f"}, + {__LINE__, 1.364592026494839e-24, "1.36459e-24", "%g"}, + {__LINE__, 1.366664212399413e-09, "+1E-09", "%+6.G"}, + {__LINE__, 1.367446193117463e-17, "1.3674e-17", "%2.4e"}, + {__LINE__, 1.368681648785823e-18, "1.36868E-18", "%3G"}, + {__LINE__, 1.370635189583606e+05, "1.370635E+05", "%E"}, + {__LINE__, 1.371395540772186e-18, "+1.371396e-18", "%+e"}, + {__LINE__, 1.371945318043159e+10, "1E+10", "%0.E"}, + {__LINE__, 1.373780604772988e-21, "1e-21", "%3.g"}, + {__LINE__, 1.374244970441862e-28, "0.000000", "%7.6f"}, + {__LINE__, 1.375874695067149e-25, "1.37587E-25", "%#3G"}, + {__LINE__, 1.377165614832621e+20, "1.37717E+20", "%#G"}, + {__LINE__, 1.381541864946684e-09, "1.38154E-09", "%G"}, + {__LINE__, 1.382901694835415e+00, "1.382902E+00", "%E"}, + {__LINE__, 1.384250577530184e-21, "1.384e-21", "%.4g"}, + {__LINE__, 1.385322753374640e-03, " 1E-03", "%6.E"}, + {__LINE__, 1.387026156054724e+19, "1.387026E+19", "%E"}, + {__LINE__, 1.388726735790498e-27, "+1.388727E-27", "%+E"}, + {__LINE__, 1.388819105742044e+16, "1.38882e+16", "%g"}, + {__LINE__, 1.389941086024951e-19, "0.000000", "%f"}, + {__LINE__, 1.390101174785536e+25, "1.3901E+25", "%G"}, + {__LINE__, 1.392210785638597e+24, "+1.39221E+24", "%+G"}, + {__LINE__, 1.396148856283474e-26, "1.4e-26", "%7.3g"}, + {__LINE__, 1.398153728631994e-21, "1.39815E-21", "%G"}, + {__LINE__, 1.398332446762342e-14, "+1.39833E-14", "%+5G"}, + {__LINE__, 1.400250919607987e+10, "1.40025E+10", "%G"}, + {__LINE__, 1.400662469019739e+03, "1.E+03", "%#0.0E"}, + {__LINE__, 1.401720273747805e-12, "0.000000", "%#f"}, + {__LINE__, 1.402151358748719e+22, "1.402151e+22", "%e"}, + {__LINE__, 1.402351913752357e-28, "1.40235E-28", "%#1.6G"}, + {__LINE__, 1.403285877973810e+13, "+14032858779738.097255", "%+4f"}, + {__LINE__, 1.404540981735861e+04, "14045.409817", "%4f"}, + {__LINE__, 1.406420622355064e+19, "1.40642E+19", "%G"}, + {__LINE__, 1.412552725962301e-29, "+1e-29", "%+1.e"}, + {__LINE__, 1.413266982485044e+10, "1.41327e+10", "%g"}, + {__LINE__, 1.413627189411456e+21, "1.41363E+21", "%0G"}, + {__LINE__, 1.416647216002592e-25, "+0.000000", "%+#f"}, + {__LINE__, 1.417107155080584e-10, "1e-10", "%1.e"}, + {__LINE__, 1.418829895346648e+19, "14188298953466484952.912197", "%f"}, + {__LINE__, 1.419404559267523e-11, "1.4194e-11", "%g"}, + {__LINE__, 1.422421321425472e+15, "1.422421E+15", "%E"}, + {__LINE__, 1.424858902008998e+18, "1.42486E+18", "%2.5E"}, + {__LINE__, 1.428863051254205e+22, "1.428863e+22", "%e"}, + {__LINE__, 1.429533727936725e-02, "0.0142953", "%g"}, + {__LINE__, 1.430982219743369e-19, " 1E-19", "%6.G"}, + {__LINE__, 1.431974118434703e+17, "1E+17", "%3.G"}, + {__LINE__, 1.432418341970152e+16, "+1.432418e+16", "%+5e"}, + {__LINE__, 1.433470281650523e+04, "14334.7", "%g"}, + {__LINE__, 1.434446243424653e+24, " +1e+24", "%+7.g"}, + {__LINE__, 1.435118019241039e+24, "+1e+24", "%+4.g"}, + {__LINE__, 1.435691648420278e-01, "0.1435692", "%#4.7g"}, + {__LINE__, 1.437569598098744e+27, "1.437570E+27", "%#E"}, + {__LINE__, 1.437633484061726e-25, "+1.43763e-25", "%+7g"}, + {__LINE__, 1.437866591230707e-13, "1.43787e-13", "%g"}, + {__LINE__, 1.440593154445915e-01, "+0.144059", "%+1G"}, + {__LINE__, 1.441368205977342e+16, "1.441368E+16", "%#E"}, + {__LINE__, 1.441825266684367e+28, "1.44183e+28", "%g"}, + {__LINE__, 1.442374183199456e+20, "1.442374E+20", "%E"}, + {__LINE__, 1.445101893951061e-24, "0", "%0.f"}, + {__LINE__, 1.446037835160094e-20, "+1.44604E-20", "%+2G"}, + {__LINE__, 1.449245376093616e+04, "1.449245e+04", "%e"}, + {__LINE__, 1.454126845620100e+09, "1.45413E+09", "%G"}, + {__LINE__, 1.455764608801107e+09, "1.5e+09", "%6.1e"}, + {__LINE__, 1.457176183149955e-12, "1.45718e-12", "%#g"}, + {__LINE__, 1.458821535020046e-05, "1.45882e-05", "%#g"}, + {__LINE__, 1.460179141161202e-19, "1.460179E-19", "%#E"}, + {__LINE__, 1.461701055446198e-20, "1.461701e-20", "%#2.7g"}, + {__LINE__, 1.462408181482172e+10, "1.4624082E+10", "%3.7E"}, + {__LINE__, 1.463706296748154e+24, "1e+24", "%2.g"}, + {__LINE__, 1.464891255780761e-19, "+1.46489e-19", "%+g"}, + {__LINE__, 1.467903978945872e+25, "1.4679E+25", "%G"}, + {__LINE__, 1.468585992857062e+22, "14685859928570619281629.205943", "%f"}, + {__LINE__, 1.469803528668286e+29, "+1E+29", "%+6.G"}, + {__LINE__, 1.469883002609266e-23, "1.E-23", "%#2.E"}, + {__LINE__, 1.469941481858809e+08, "+146994148.185881", "%+f"}, + {__LINE__, 1.470744493304611e+13, "1.47074e+13", "%g"}, + {__LINE__, 1.471857261085004e+23, "1E+23", "%2.G"}, + {__LINE__, 1.474419778873037e+06, "1474419.7789", "%#.4f"}, + {__LINE__, 1.483134762223381e-03, "0", "%0.0f"}, + {__LINE__, 1.483575501240724e-13, "1.48358e-13", "%g"}, + {__LINE__, 1.486068112294452e+20, "1.486068e+20", "%e"}, + {__LINE__, 1.488665528787761e-03, " +0", "%+4.f"}, + {__LINE__, 1.492145267536713e+07, "14921452.675367", "%f"}, + {__LINE__, 1.492589762884761e-01, "0.149259", "%f"}, + {__LINE__, 1.494784286168215e+01, "1.494784E+01", "%E"}, + {__LINE__, 1.495945331501655e+04, "1.5e+04", "%.2g"}, + {__LINE__, 1.501383201904700e+14, "2e+14", "%1.g"}, + {__LINE__, 1.501387105011959e-02, "0.0150139", "%g"}, + {__LINE__, 1.505784488562271e+26, "1.50578e+26", "%5g"}, + {__LINE__, 1.506367986495534e+10, "1.50637e+10", "%2g"}, + {__LINE__, 1.507421710226296e+19, "1.507422E+19", "%#E"}, + {__LINE__, 1.507960881206134e+16, "2e+16", "%.0g"}, + {__LINE__, 1.508974301049424e+14, "+1.508974E+14", "%+E"}, + {__LINE__, 1.509204025111090e-18, "1.509204E-18", "%E"}, + {__LINE__, 1.511351799767759e+21, "1511351799767759462866.93397", "%.5f"}, + {__LINE__, 1.511712197184520e-06, "+1.51171e-06", "%+1g"}, + {__LINE__, 1.514703727491400e+02, "151.47", "%.5g"}, + {__LINE__, 1.514855355408287e+04, "15148.6", "%g"}, + {__LINE__, 1.516256896575301e+06, "1.516257e+06", "%.6e"}, + {__LINE__, 1.517712083681069e+14, "1.51771e+14", "%0g"}, + {__LINE__, 1.522102209167607e-18, "1.52210e-18", "%#g"}, + {__LINE__, 1.526264862895663e-12, "0.000000", "%f"}, + {__LINE__, 1.527165480858125e+10, "1.527e+10", "%.4g"}, + {__LINE__, 1.528363280308369e+00, "+1.52836", "%+0G"}, + {__LINE__, 1.529108490733420e-22, "1.529108E-22", "%E"}, + {__LINE__, 1.529783364474522e+19, "15297833644745216119.531506", "%7f"}, + {__LINE__, 1.530698363199346e-01, "0.1531", "%#.4f"}, + {__LINE__, 1.531520796537450e+22, "1.53152E+22", "%G"}, + {__LINE__, 1.533314559644813e+06, "+1533314.559645", "%+f"}, + {__LINE__, 1.534682791112854e+01, "+15.3468", "%+0G"}, + {__LINE__, 1.534714026386730e-11, "0.000000", "%f"}, + {__LINE__, 1.537145729498115e+00, "1.537146E+00", "%E"}, + {__LINE__, 1.538740040875751e-23, "1.538740E-23", "%E"}, + {__LINE__, 1.546248240596482e-03, "0.001546", "%f"}, + {__LINE__, 1.546808012239302e-09, "+1.54681E-09", "%+G"}, + {__LINE__, 1.546903877359107e+06, "1.5e+06", "%.2g"}, + {__LINE__, 1.550153973747718e-18, "2e-18", "%.0g"}, + {__LINE__, 1.551703460384378e+00, "+1.5517", "%+.4f"}, + {__LINE__, 1.553995673101369e+29, "1.554E+29", "%G"}, + {__LINE__, 1.555477017531899e+28, "1.55548e+28", "%g"}, + {__LINE__, 1.555548963448462e+12, "1555548963448.46227", "%6.5f"}, + {__LINE__, 1.558702451007712e+16, "2e+16", "%5.1g"}, + {__LINE__, 1.561641587723724e+29, "1.561642e+29", "%e"}, + {__LINE__, 1.561868589559509e-05, "1.56187E-05", "%G"}, + {__LINE__, 1.565229056438949e+13, "1.56523e+13", "%1.6g"}, + {__LINE__, 1.570074406600442e+14, "+1.57007E+14", "%+4G"}, + {__LINE__, 1.570951909913194e+13, "1.57095E+13", "%.6G"}, + {__LINE__, 1.572186421520727e-30, "+1.57219e-30", "%+g"}, + {__LINE__, 1.574374582066945e+12, "1.57e+12", "%5.2e"}, + {__LINE__, 1.577789404823585e+12, "1.577789E+12", "%E"}, + {__LINE__, 1.579641278389186e+04, "15796.413", "%7.3f"}, + {__LINE__, 1.581602330975388e+17, "1.5816E+17", "%#.5G"}, + {__LINE__, 1.587809692297490e-18, "+1.58781e-18", "%+2g"}, + {__LINE__, 1.588672954832388e-05, "1.58867E-05", "%G"}, + {__LINE__, 1.594548954259978e+25, "+1.595e+25", "%+.3e"}, + {__LINE__, 1.596653742160718e+10, "15966537421.6072", "%.4f"}, + {__LINE__, 1.597416186584641e+25, "1.59742e+25", "%g"}, + {__LINE__, 1.597810706039500e-04, "0.000159781", "%g"}, + {__LINE__, 1.601425691153542e+01, "16.0143", "%G"}, + {__LINE__, 1.601491150913706e-15, "+1.60149e-15", "%+g"}, + {__LINE__, 1.609357026469645e+02, "160.936", "%0g"}, + {__LINE__, 1.611064881461188e+06, "+1.611065E+06", "%+E"}, + {__LINE__, 1.611324319640770e-03, "0.00161132", "%G"}, + {__LINE__, 1.613202907940803e+19, "1.6E+19", "%#.1E"}, + {__LINE__, 1.613883500056636e-12, "0.000000", "%5f"}, + {__LINE__, 1.618254213010772e+07, "16182542.130108", "%6f"}, + {__LINE__, 1.620441271211248e-08, "1.62044E-08", "%0G"}, + {__LINE__, 1.622548435024803e-01, "+0.162255", "%+5G"}, + {__LINE__, 1.623740394555269e+19, "16237403945552689066.289531", "%3f"}, + {__LINE__, 1.625774250389937e+18, "1.625774e+18", "%1e"}, + {__LINE__, 1.626300864432426e-02, "0.016263", "%G"}, + {__LINE__, 1.626351146877694e-11, "1.6263511e-11", "%2.7e"}, + {__LINE__, 1.626568757587995e+25, "16265687575879949733348439.62220", "%6.5f"}, + {__LINE__, 1.626717482094360e-28, "+1.626717e-28", "%+3e"}, + {__LINE__, 1.630801585998995e-28, "1.6308e-28", "%0g"}, + {__LINE__, 1.631827173682962e+03, "1632", "%1.4g"}, + {__LINE__, 1.631907571243379e+18, "1631907571243378818.337533", "%f"}, + {__LINE__, 1.632058420447239e-14, " +2E-14", "%+7.G"}, + {__LINE__, 1.632847882621240e+18, "+1.63285E+18", "%+2G"}, + {__LINE__, 1.637329146233172e-09, "+1.63733e-09", "%+g"}, + {__LINE__, 1.638315287442427e-16, "+1.63832E-16", "%+G"}, + {__LINE__, 1.641284849351445e+15, "1641284849351445.096488", "%f"}, + {__LINE__, 1.644871927486929e+10, "2e+10", "%1.e"}, + {__LINE__, 1.645917293047431e-19, "1.64592e-19", "%.6g"}, + {__LINE__, 1.649201265647819e-07, "0.000000", "%f"}, + {__LINE__, 1.650169065733559e+23, "165016906573355903731226.135278", "%3f"}, + {__LINE__, 1.651217291298196e-30, "2E-30", "%4.G"}, + {__LINE__, 1.652680660160331e+08, "2.e+08", "%#3.g"}, + {__LINE__, 1.654171892948767e-15, "1.65417e-15", "%g"}, + {__LINE__, 1.655435863725412e+17, "+1.655436e+17", "%+#e"}, + {__LINE__, 1.656015606204101e+28, "+1.7E+28", "%+3.2G"}, + {__LINE__, 1.656814723110478e-21, "+0.000000", "%+f"}, + {__LINE__, 1.658632655199721e-19, "2.E-19", "%#4.G"}, + {__LINE__, 1.659915061311444e+27, "1.66E+27", "%.3G"}, + {__LINE__, 1.664258574522914e-29, "+1.664259E-29", "%+E"}, + {__LINE__, 1.667662840654469e-28, "+2e-28", "%+0.g"}, + {__LINE__, 1.673124993246863e-25, "+0.000000", "%+f"}, + {__LINE__, 1.677788498058833e-20, "1.678e-20", "%.4g"}, + {__LINE__, 1.681211731173386e-07, "1.68121e-07", "%0.6g"}, + {__LINE__, 1.686341560076196e+02, "169", "%.0f"}, + {__LINE__, 1.686507685287281e+08, "1.686508E+08", "%E"}, + {__LINE__, 1.689500448795801e+03, "+1689.500449", "%+0f"}, + {__LINE__, 1.690639426041009e-13, "2e-13", "%5.0g"}, + {__LINE__, 1.691554939595928e+16, "+1.6916e+16", "%+.5g"}, + {__LINE__, 1.698597126229626e-09, "1.699e-09", "%3.4g"}, + {__LINE__, 1.700093083173516e+16, "17000930831735159", "%5.f"}, + {__LINE__, 1.703653732982710e+13, "+1.70365e+13", "%+g"}, + {__LINE__, 1.705353380573352e-16, "0.000000", "%f"}, + {__LINE__, 1.705973328700179e-26, "1.70597e-26", "%5g"}, + {__LINE__, 1.717797289369145e+22, "+1.717797E+22", "%+7E"}, + {__LINE__, 1.722819196705361e-28, "1.722819E-28", "%1.7G"}, + {__LINE__, 1.723775772891202e+01, "17.237758", "%f"}, + {__LINE__, 1.723787102325353e-23, "0.000000", "%2f"}, + {__LINE__, 1.724551301171870e-13, "0.000000", "%f"}, + {__LINE__, 1.726098078655758e-20, "0.0000000", "%7.7f"}, + {__LINE__, 1.726302822039762e+18, "+2E+18", "%+1.G"}, + {__LINE__, 1.726659209108151e+18, "+1.72666E+18", "%+6G"}, + {__LINE__, 1.728324925097269e-25, "1.728325e-25", "%7e"}, + {__LINE__, 1.728357491215602e+02, "172.836", "%G"}, + {__LINE__, 1.729612227517587e+22, "1.729612e+22", "%e"}, + {__LINE__, 1.731753104740805e-06, "1.73175e-06", "%2g"}, + {__LINE__, 1.736066279733589e+18, "+1.736066E+18", "%+3E"}, + {__LINE__, 1.736626769480182e-19, "2e-19", "%.0g"}, + {__LINE__, 1.737445267713118e-13, " 2e-13", "%7.g"}, + {__LINE__, 1.739264683023077e+05, "173926.468302", "%f"}, + {__LINE__, 1.739423702667596e-16, "+1.73942e-16", "%+g"}, + {__LINE__, 1.741053616961658e-14, "1.74105E-14", "%G"}, + {__LINE__, 1.741085952255057e+29, "1.7e+29", "%#.1e"}, + {__LINE__, 1.743673705633426e+03, "2E+03", "%5.0G"}, + {__LINE__, 1.750940899205665e+27, "1.75094e+27", "%g"}, + {__LINE__, 1.753871003884151e-26, "0.", "%#.0f"}, + {__LINE__, 1.755237470854478e-14, "1.755237e-14", "%4e"}, + {__LINE__, 1.757491419837315e-03, "0.002", "%1.G"}, + {__LINE__, 1.758657797688126e+26, "+1.75866E+26", "%+#G"}, + {__LINE__, 1.760491452270889e+08, "1.76049E+08", "%G"}, + {__LINE__, 1.760816275862939e-10, "2e-10", "%.1g"}, + {__LINE__, 1.760905893493003e-22, "1.76e-22", "%.2e"}, + {__LINE__, 1.762101125986743e-12, "2e-12", "%.0g"}, + {__LINE__, 1.763855968659571e+27, "1.763856E+27", "%E"}, + {__LINE__, 1.764595217341348e-13, "1.764595E-13", "%E"}, + {__LINE__, 1.766737296048445e+08, "+1.77e+08", "%+.3g"}, + {__LINE__, 1.769916386969961e-05, "+1.76992e-05", "%+g"}, + {__LINE__, 1.773873506344176e-01, "1.773874e-01", "%e"}, + {__LINE__, 1.775392554371421e+03, "+1775.39", "%+3.2f"}, + {__LINE__, 1.776007623006870e+24, "1776007623006870161930237.372871", "%f"}, + {__LINE__, 1.777418921678653e-27, " 0", "%6.f"}, + {__LINE__, 1.780967607234234e-18, "1.780968E-18", "%E"}, + {__LINE__, 1.782510583486435e+24, "+2e+24", "%+3.g"}, + {__LINE__, 1.784002921603004e+14, "1.784E+14", "%G"}, + {__LINE__, 1.784231151778382e+11, "1.7842E+11", "%.5G"}, + {__LINE__, 1.785589091453195e+02, " 179.", "%#5.f"}, + {__LINE__, 1.786402639400039e+19, "2e+19", "%0.g"}, + {__LINE__, 1.786506485794647e-25, "2E-25", "%.1G"}, + {__LINE__, 1.787513971482493e-04, "+0.000178751", "%+G"}, + {__LINE__, 1.799765409320039e+23, "179976540932003885586710", "%6.0f"}, + {__LINE__, 1.800451864218989e+08, "+1.80045e+08", "%+g"}, + {__LINE__, 1.800743538258572e+04, " +18007", "%+7.f"}, + {__LINE__, 1.801583764453362e+25, "1.8016e+25", "%0.5g"}, + {__LINE__, 1.804502093739547e+14, "2e+14", "%2.g"}, + {__LINE__, 1.805600104488678e-17, "1.805600E-17", "%E"}, + {__LINE__, 1.811342448081048e+17, "1.811342e+17", "%.6e"}, + {__LINE__, 1.812252160066930e+19, "2e+19", "%0.e"}, + {__LINE__, 1.816287564395273e+02, "182", "%2.f"}, + {__LINE__, 1.817097386762552e-13, "2E-13", "%3.1G"}, + {__LINE__, 1.817881381387254e+06, "1.81788E+06", "%G"}, + {__LINE__, 1.818416380830953e-30, "1.81842E-30", "%G"}, + {__LINE__, 1.824788290177454e-23, "1.82479e-23", "%g"}, + {__LINE__, 1.827832506678437e-18, "1.82783E-18", "%G"}, + {__LINE__, 1.828048053242609e+01, "18.2805", "%g"}, + {__LINE__, 1.828939681844203e-21, "1.82894E-21", "%.6G"}, + {__LINE__, 1.829763900251753e+12, "1829763900251.752773", "%1f"}, + {__LINE__, 1.833100407114602e-05, "0.000018", "%#f"}, + {__LINE__, 1.833672780034284e-07, "1.833673e-07", "%e"}, + {__LINE__, 1.834307692387540e+21, "2E+21", "%5.G"}, + {__LINE__, 1.835119931666753e+14, "+1.835120e+14", "%+e"}, + {__LINE__, 1.835956566920861e-26, "0.000000", "%#f"}, + {__LINE__, 1.837784131899354e+22, "1.83778E+22", "%G"}, + {__LINE__, 1.842867094224664e-09, "1.84287e-09", "%g"}, + {__LINE__, 1.844234823805319e+28, "2E+28", "%1.G"}, + {__LINE__, 1.844877682008962e+12, "1.84488e+12", "%g"}, + {__LINE__, 1.846530424340488e-22, "1.846530e-22", "%#.7g"}, + {__LINE__, 1.849111603036448e-08, "0.00", "%.2f"}, + {__LINE__, 1.850158752678734e+04, "+18502", "%+.5G"}, + {__LINE__, 1.851431391104701e-06, " +2e-06", "%+7.e"}, + {__LINE__, 1.851487430609031e-04, "0.000185149", "%G"}, + {__LINE__, 1.853171650128773e+03, "1853.17", "%G"}, + {__LINE__, 1.854743263740068e-16, "1.854743E-16", "%6.6E"}, + {__LINE__, 1.864399020932753e-11, "+0.000000", "%+f"}, + {__LINE__, 1.865028568072397e-22, "1.86503E-22", "%.6G"}, + {__LINE__, 1.868128423759223e+14, "1.86813E+14", "%4G"}, + {__LINE__, 1.868453558435480e+21, "1.86845E+21", "%G"}, + {__LINE__, 1.870663011627498e-06, "1.87E-06", "%.3G"}, + {__LINE__, 1.873273487748907e+17, "+1.873273E+17", "%+E"}, + {__LINE__, 1.878885220839805e-04, "0.000187889", "%G"}, + {__LINE__, 1.879102515623569e+17, "1.8791e+17", "%g"}, + {__LINE__, 1.889343972100858e-10, "2E-10", "%2.E"}, + {__LINE__, 1.891833282879762e-24, "+2e-24", "%+.1g"}, + {__LINE__, 1.895148778941240e-07, "0.000000", "%0f"}, + {__LINE__, 1.896950834067578e-28, "+1.89695e-28", "%+#g"}, + {__LINE__, 1.898464546986629e-05, "1.898465e-05", "%.7g"}, + {__LINE__, 1.900851427578321e-19, "1.90085e-19", "%6g"}, + {__LINE__, 1.901171344577750e+27, "1.901171e+27", "%7.7g"}, + {__LINE__, 1.903563335316359e+02, "190.356", "%0g"}, + {__LINE__, 1.903817306004701e-16, "+1.90e-16", "%+#6.2e"}, + {__LINE__, 1.910606410246797e+21, "1.91061E+21", "%2.5E"}, + {__LINE__, 1.912537759564710e+29, "1.91254E+29", "%G"}, + {__LINE__, 1.913606675894566e+27, "1.91361E+27", "%7.6G"}, + {__LINE__, 1.916473734025505e-05, " 2e-05", "%7.g"}, + {__LINE__, 1.916507511720523e+23, "+2E+23", "%+.0E"}, + {__LINE__, 1.917688262248837e-28, "1.918e-28", "%.4g"}, + {__LINE__, 1.921375594217083e-12, "1.92138E-12", "%G"}, + {__LINE__, 1.923916000932815e-25, "1.92392E-25", "%G"}, + {__LINE__, 1.926303652776462e-01, "+0.2", "%+0.G"}, + {__LINE__, 1.929411084211293e+21, "1.92941E+21", "%6G"}, + {__LINE__, 1.929996820297491e-26, "1.93e-26", "%.6g"}, + {__LINE__, 1.937301472034748e-29, "1.9373E-29", "%G"}, + {__LINE__, 1.937301660220894e-11, "+1.9373E-11", "%+G"}, + {__LINE__, 1.939038538587083e-02, "0.0193904", "%g"}, + {__LINE__, 1.940575489112444e-09, "1.94058E-09", "%G"}, + {__LINE__, 1.946550455606677e-26, "1.946550E-26", "%#1E"}, + {__LINE__, 1.954250777559491e-14, "1.95425E-14", "%G"}, + {__LINE__, 1.955646327688473e-16, "1.956E-16", "%#.3E"}, + {__LINE__, 1.956999911995683e+20, "2E+20", "%2.G"}, + {__LINE__, 1.959125254298784e-21, "1.95913E-21", "%#G"}, + {__LINE__, 1.960330340481290e+26, "1.96033e+26", "%.6g"}, + {__LINE__, 1.964156478075422e+01, "+19.6416", "%+g"}, + {__LINE__, 1.964702507676036e+19, "2.0e+19", "%2.1e"}, + {__LINE__, 1.965762191674409e-30, "2E-30", "%1.G"}, + {__LINE__, 1.967492699686803e-29, "+1.967493E-29", "%+#E"}, + {__LINE__, 1.970072604062236e-11, "+1.97007E-11", "%+G"}, + {__LINE__, 1.975631055550387e+05, "197563.105555", "%.6f"}, + {__LINE__, 1.975722425389177e+28, "+1.975722e+28", "%+5.6e"}, + {__LINE__, 1.977938328895365e-05, " 2e-05", "%6.e"}, + {__LINE__, 1.981950849019640e+13, "1.981951E+13", "%E"}, + {__LINE__, 1.982543168856985e-26, "1.98254e-26", "%4g"}, + {__LINE__, 1.984873317384181e-20, "1.98487E-20", "%4G"}, + {__LINE__, 1.986146420877074e+02, "+1.986146e+02", "%+0e"}, + {__LINE__, 1.986465942785167e-28, "1.98647e-28", "%g"}, + {__LINE__, 1.988250323235468e-05, " +0", "%+7.f"}, + {__LINE__, 1.989966623080645e-09, "1.989967e-09", "%e"}, + {__LINE__, 1.991243122514519e+11, "199124312251", "%5.f"}, + {__LINE__, 1.993287894645908e+25, "+1.993288E+25", "%+E"}, + {__LINE__, 1.995172329888890e-30, "+1.99517e-30", "%+3g"}, + {__LINE__, 1.997075127236432e+05, "1.997E+05", "%#.4G"}, + {__LINE__, 2.000651599487290e+04, "2E+04", "%2.E"}, + {__LINE__, 2.001429412876339e-28, "0.000000", "%6f"}, + {__LINE__, 2.003535646264196e+03, " 2E+03", "%7.G"}, + {__LINE__, 2.011751504116246e+06, "2E+06", "%2.G"}, + {__LINE__, 2.016361237694652e-30, "2.01636e-30", "%4g"}, + {__LINE__, 2.018266414065554e-08, "0.00", "%2.2f"}, + {__LINE__, 2.020706780608565e+03, "2E+03", "%.0G"}, + {__LINE__, 2.025098418552005e-16, "+2.025098E-16", "%+2E"}, + {__LINE__, 2.026100592518976e-11, "2.026101e-11", "%6e"}, + {__LINE__, 2.026167291572980e+11, "2.02617E+11", "%G"}, + {__LINE__, 2.026782170272331e+07, "2.026782E+07", "%E"}, + {__LINE__, 2.029133469449347e-06, " 2e-06", "%6.g"}, + {__LINE__, 2.031191570768443e-20, "2.031192E-20", "%2.7G"}, + {__LINE__, 2.031884221862863e-16, "2.031884e-16", "%e"}, + {__LINE__, 2.032992924118323e+29, "203299292411832333016263874014.84", "%#0.2f"}, + {__LINE__, 2.035799610097827e+28, "+20357996100978272835391180706.2104344", "%+3.7f"}, + {__LINE__, 2.038008183612069e+25, "20380081836120691463065668", "%7.0f"}, + {__LINE__, 2.041632205119365e-22, "+2.04163e-22", "%+g"}, + {__LINE__, 2.042769811159352e-10, "2e-10", "%.1g"}, + {__LINE__, 2.043034975847005e-08, "2.043035e-08", "%e"}, + {__LINE__, 2.045930666285880e+26, "2.04593e+26", "%g"}, + {__LINE__, 2.048377276651386e-21, "0.00000", "%2.5f"}, + {__LINE__, 2.051785787301292e-23, "0.000000", "%f"}, + {__LINE__, 2.055085792048183e-10, "0.000000", "%f"}, + {__LINE__, 2.055783028451040e-21, "+0", "%+1.f"}, + {__LINE__, 2.057641607509489e-17, "+2E-17", "%+2.G"}, + {__LINE__, 2.058348703001600e-15, "2.05835E-15", "%#G"}, + {__LINE__, 2.059594758787322e+24, "2.05959E+24", "%1G"}, + {__LINE__, 2.062710740295018e+01, "20.6271", "%G"}, + {__LINE__, 2.063240676447750e-11, "0.000000", "%f"}, + {__LINE__, 2.063799238238917e-09, "+0.000000", "%+f"}, + {__LINE__, 2.064564135160425e-17, "2.06456E-17", "%G"}, + {__LINE__, 2.066363476927650e-05, "2.07E-05", "%.3G"}, + {__LINE__, 2.066871450514214e-02, "+2.066871e-02", "%+e"}, + {__LINE__, 2.067258256169148e+07, "2.06726e+07", "%g"}, + {__LINE__, 2.068466590729350e-22, "0.0000", "%.4f"}, + {__LINE__, 2.068779510112540e-26, "2.06878e-26", "%g"}, + {__LINE__, 2.069258770387493e+19, "20692587703874929516.700831", "%3f"}, + {__LINE__, 2.069547778447951e+26, "2.069548e+26", "%6e"}, + {__LINE__, 2.070479507710941e-23, "2.07048E-23", "%G"}, + {__LINE__, 2.071809640061785e+22, "2.071810E+22", "%E"}, + {__LINE__, 2.073364463564950e-06, "2.07336e-06", "%g"}, + {__LINE__, 2.073478953644888e-10, "+2.07E-10", "%+.3G"}, + {__LINE__, 2.076039194312519e+12, "2.07604E+12", "%G"}, + {__LINE__, 2.081490398946229e-07, "+2.08149e-07", "%+g"}, + {__LINE__, 2.084941170287895e-07, "2.084941E-07", "%7E"}, + {__LINE__, 2.087035885023382e-18, "+0.000000", "%+f"}, + {__LINE__, 2.091025884363342e-09, "2.09103E-09", "%G"}, + {__LINE__, 2.091830555397864e+08, "209183056", "%0.f"}, + {__LINE__, 2.091998109232084e-22, "0.000000", "%#f"}, + {__LINE__, 2.093406473464940e-17, "2.09341e-17", "%g"}, + {__LINE__, 2.094646724039720e-26, "2.094647e-26", "%0.7g"}, + {__LINE__, 2.095218667083208e-22, "2.09522e-22", "%.5e"}, + {__LINE__, 2.096932364992728e-27, "2.1E-27", "%.2G"}, + {__LINE__, 2.097012452732083e+17, "2.09701E+17", "%G"}, + {__LINE__, 2.102022216908102e-07, "0.000000", "%f"}, + {__LINE__, 2.104648382618938e-04, "0.00021", "%.3G"}, + {__LINE__, 2.109159578853690e-19, "2.10916e-19", "%6g"}, + {__LINE__, 2.111732624641178e-13, "+2.111733E-13", "%+7.7G"}, + {__LINE__, 2.112676138351330e+21, " 2e+21", "%6.g"}, + {__LINE__, 2.117332904051741e-09, "+0.000000", "%+f"}, + {__LINE__, 2.118001353767757e-14, "2.118E-14", "%4.3E"}, + {__LINE__, 2.119413720893955e+12, "2.119414E+12", "%.7G"}, + {__LINE__, 2.119610992647014e+07, "+21196109.9265", "%+.4f"}, + {__LINE__, 2.120504022069221e+25, "21205040220692210617414730.223", "%#.3f"}, + {__LINE__, 2.120634617123718e-16, "2.120635e-16", "%#e"}, + {__LINE__, 2.128702437175385e+12, "2128702437175.385", "%.3f"}, + {__LINE__, 2.131148830213536e-12, "2.131149e-12", "%e"}, + {__LINE__, 2.132682622145255e+04, "+21326.83", "%+#5.7G"}, + {__LINE__, 2.133864841676473e-01, " +0.2", "%+6.g"}, + {__LINE__, 2.133939058496273e+16, "2E+16", "%4.G"}, + {__LINE__, 2.135087006806302e-24, "0.000000", "%f"}, + {__LINE__, 2.138983961194584e-20, "+2.138984e-20", "%+7e"}, + {__LINE__, 2.146534189669224e+17, "+2.147E+17", "%+.4G"}, + {__LINE__, 2.147375595676503e+09, "2.147376e+09", "%e"}, + {__LINE__, 2.152994986418075e+02, "2E+02", "%0.E"}, + {__LINE__, 2.154127135896811e+06, "2.15E+06", "%2.3G"}, + {__LINE__, 2.154613053892588e-29, "2.1546E-29", "%.4E"}, + {__LINE__, 2.160830430730653e-12, "2.16083E-12", "%#G"}, + {__LINE__, 2.161740591663027e+28, "2.161741e+28", "%e"}, + {__LINE__, 2.167181665934011e-06, "2e-06", "%1.e"}, + {__LINE__, 2.167348761729060e-04, "2.167349e-04", "%#e"}, + {__LINE__, 2.170270852816708e-10, "2.17027E-10", "%0G"}, + {__LINE__, 2.173048229189370e-06, "2.173e-06", "%.5g"}, + {__LINE__, 2.175554537855024e+10, "2.17555E+10", "%G"}, + {__LINE__, 2.177447372527968e-30, "+0.000000", "%+.6f"}, + {__LINE__, 2.178591661569858e+05, "2.1786e+05", "%1.5g"}, + {__LINE__, 2.184700207174818e-29, "0.000000", "%f"}, + {__LINE__, 2.190817031437247e+03, "2.E+03", "%#3.0E"}, + {__LINE__, 2.191396212145558e-01, "2.191E-01", "%6.3E"}, + {__LINE__, 2.191786353372880e-23, "2.19179e-23", "%.5e"}, + {__LINE__, 2.193471126151079e+19, "2.1935E+19", "%.4E"}, + {__LINE__, 2.194750177630526e-15, "2.194750e-15", "%e"}, + {__LINE__, 2.196145170358973e+05, "+219615", "%+g"}, + {__LINE__, 2.199602360934320e-09, "2.19960E-09", "%#6.6G"}, + {__LINE__, 2.202913988776998e+17, "+2.20291e+17", "%+g"}, + {__LINE__, 2.207094560707703e-10, "2.207095e-10", "%e"}, + {__LINE__, 2.207557214621658e-13, " 0", "%4.f"}, + {__LINE__, 2.209428206272229e+13, "2.20943e+13", "%g"}, + {__LINE__, 2.212662463615175e-11, " 0", "%4.0f"}, + {__LINE__, 2.215732710968468e-30, "2E-30", "%.1G"}, + {__LINE__, 2.218490841936778e-10, "2.21849e-10", "%2g"}, + {__LINE__, 2.218850610567959e-01, "+0.221885", "%+G"}, + {__LINE__, 2.220796939261542e-03, "0.002221", "%#f"}, + {__LINE__, 2.221819894808024e+24, "2.2e+24", "%5.1e"}, + {__LINE__, 2.223804993235507e+27, "2.223805e+27", "%1e"}, + {__LINE__, 2.224715386046563e-17, "0.00000", "%#.5f"}, + {__LINE__, 2.224864709983882e-16, "+2.224865e-16", "%+e"}, + {__LINE__, 2.225764510969505e+16, "2.E+16", "%#4.G"}, + {__LINE__, 2.233229189161284e-07, "+2.23323e-07", "%+6g"}, + {__LINE__, 2.236522807209868e+17, "2.23652e+17", "%g"}, + {__LINE__, 2.236641620849775e+20, "2E+20", "%0.G"}, + {__LINE__, 2.237089952728626e-13, "2E-13", "%5.E"}, + {__LINE__, 2.238531255563381e-11, " 2E-11", "%7.1G"}, + {__LINE__, 2.242782296436871e+12, "2242782296436.871103", "%f"}, + {__LINE__, 2.249177852069393e-02, "+2.249178E-02", "%+5E"}, + {__LINE__, 2.250735782732076e+29, "+2.25074e+29", "%+g"}, + {__LINE__, 2.257455203557544e-11, "+2e-11", "%+4.0g"}, + {__LINE__, 2.258002527939529e+24, "2.258e+24", "%g"}, + {__LINE__, 2.266955929448160e+20, "226695592944815960123.6056915", "%.7f"}, + {__LINE__, 2.269019719123250e-23, "0.000000", "%f"}, + {__LINE__, 2.269022332502809e+02, "+226.902233", "%+f"}, + {__LINE__, 2.271165222038591e-03, "0.002", "%1.1g"}, + {__LINE__, 2.273965946408021e+14, "+227396594640802.085507", "%+f"}, + {__LINE__, 2.277221153386242e+22, "22772211533862418697538.032575", "%#f"}, + {__LINE__, 2.280214920187521e-01, "0.2", "%2.G"}, + {__LINE__, 2.280762993019740e+06, "2.28076E+06", "%G"}, + {__LINE__, 2.283479263040833e-24, "+0.000000", "%+f"}, + {__LINE__, 2.283686319235123e-07, "2.2837E-07", "%5.5G"}, + {__LINE__, 2.285842617231066e-26, "0.000000", "%f"}, + {__LINE__, 2.293145864755873e-02, "0.02", "%.0g"}, + {__LINE__, 2.293439381531532e+03, "+2.293439E+03", "%+#1E"}, + {__LINE__, 2.297972500660698e-22, "2.29797e-22", "%g"}, + {__LINE__, 2.298725366073681e+23, "2.29873E+23", "%G"}, + {__LINE__, 2.303151535483950e-29, "2.30315E-29", "%G"}, + {__LINE__, 2.308565313657699e-25, " 0", "%4.f"}, + {__LINE__, 2.309305373760733e-07, "2.309305E-07", "%E"}, + {__LINE__, 2.309693761334710e-19, "2.30969e-19", "%g"}, + {__LINE__, 2.312218220995774e-20, "+2.31222E-20", "%+.6G"}, + {__LINE__, 2.313115729670525e+24, "2.31312E+24", "%G"}, + {__LINE__, 2.315325159866773e-21, "2.315325E-21", "%E"}, + {__LINE__, 2.316932917620091e-19, "0.000000", "%f"}, + {__LINE__, 2.317548394633895e+13, "2.31755E+13", "%0G"}, + {__LINE__, 2.318951465093612e+02, "+2.32e+02", "%+1.2e"}, + {__LINE__, 2.319151794905482e-17, "+0.000000", "%+0f"}, + {__LINE__, 2.319708617851078e-27, "0.000000", "%f"}, + {__LINE__, 2.320019976591725e+20, "2e+20", "%.1g"}, + {__LINE__, 2.322859962551666e+06, "2.32286e+06", "%g"}, + {__LINE__, 2.326474568074649e-29, "2.326e-29", "%.4g"}, + {__LINE__, 2.328400844172053e-07, "2.3284E-07", "%G"}, + {__LINE__, 2.333695247698112e+16, "2.3337e+16", "%g"}, + {__LINE__, 2.333717120257130e-19, "+0.000000", "%+f"}, + {__LINE__, 2.334711793234782e-19, "2.33471E-19", "%5G"}, + {__LINE__, 2.335524987281242e+02, "+233.55", "%+7.5g"}, + {__LINE__, 2.335556767836369e-30, "2.34e-30", "%.3g"}, + {__LINE__, 2.337696964360052e-10, "2.3377e-10", "%1g"}, + {__LINE__, 2.337858249184500e-14, "2.337858E-14", "%5.6E"}, + {__LINE__, 2.339984354781169e-14, "2.339984E-14", "%E"}, + {__LINE__, 2.340849041430089e-04, "0.00023", "%0.2G"}, + {__LINE__, 2.342388410373363e-24, "2.34239E-24", "%G"}, + {__LINE__, 2.343933106737237e-22, "2.34393e-22", "%g"}, + {__LINE__, 2.344870855713960e-08, "0.000000", "%.6f"}, + {__LINE__, 2.347368965433808e+24, "2347368965433808352116785", "%4.f"}, + {__LINE__, 2.349417462171421e-02, "+0.0234942", "%+G"}, + {__LINE__, 2.351071111431207e+11, "235107111143.12", "%.2f"}, + {__LINE__, 2.352756222810670e+19, "+2.e+19", "%+#4.g"}, + {__LINE__, 2.355862529217003e-22, "2.355863e-22", "%#4e"}, + {__LINE__, 2.356804045401445e-30, "2.3568E-30", "%G"}, + {__LINE__, 2.362100710185559e-26, "2.3621e-26", "%g"}, + {__LINE__, 2.368330779173562e-12, " 0", "%4.f"}, + {__LINE__, 2.383329784369796e+22, "2.38333E+22", "%G"}, + {__LINE__, 2.386845229536477e+01, "+23.868452", "%+#f"}, + {__LINE__, 2.388207830036780e-05, "0.000024", "%f"}, + {__LINE__, 2.391809468802907e+21, "+2.391809e+21", "%+e"}, + {__LINE__, 2.395172908564692e-09, "+0.000000", "%+f"}, + {__LINE__, 2.395447167030886e-29, "2.39545e-29", "%g"}, + {__LINE__, 2.397292973389182e+16, "23972929733891823", "%6.f"}, + {__LINE__, 2.401749832237687e-30, "2.401750E-30", "%E"}, + {__LINE__, 2.403610502544347e-03, "0.00240361", "%4G"}, + {__LINE__, 2.404676421776132e+17, "240467642177613206", "%6.f"}, + {__LINE__, 2.406327689091479e+25, "2.406328e+25", "%e"}, + {__LINE__, 2.407341999590511e-02, "0.024073", "%f"}, + {__LINE__, 2.407352797581004e+05, "240735.", "%#g"}, + {__LINE__, 2.407903467439017e+13, "2.4079E+13", "%G"}, + {__LINE__, 2.407979635013539e+21, " +2E+21", "%+7.G"}, + {__LINE__, 2.409513517930790e+29, "240951351793079027174826478585.600853", "%f"}, + {__LINE__, 2.412530651221551e+10, "+2.41253E+10", "%+G"}, + {__LINE__, 2.419943224673811e+00, "2.41994", "%#g"}, + {__LINE__, 2.422474399040258e-15, "0.000000", "%f"}, + {__LINE__, 2.426060206689458e+18, "2.42606E+18", "%1G"}, + {__LINE__, 2.439982659679872e+14, "+2.439983E+14", "%+E"}, + {__LINE__, 2.440528851375447e+13, "24405288513754.466173", "%f"}, + {__LINE__, 2.441340473292679e+19, "2.44134E+19", "%G"}, + {__LINE__, 2.443521636943916e+19, " 2.e+19", "%#7.g"}, + {__LINE__, 2.445925211597624e-27, "2.445925E-27", "%E"}, + {__LINE__, 2.446966179060722e+25, "2.446966e+25", "%5e"}, + {__LINE__, 2.447714218717278e+11, "2e+11", "%1.e"}, + {__LINE__, 2.448339744613286e+06, "2.4483e+06", "%3.5g"}, + {__LINE__, 2.448772993496189e+03, "2448.77", "%G"}, + {__LINE__, 2.448857654325229e+22, "+2.44886e+22", "%+g"}, + {__LINE__, 2.450082452098890e-27, "2.4501E-27", "%#3.5G"}, + {__LINE__, 2.451293602221637e+05, "245129.360222", "%f"}, + {__LINE__, 2.454531490229426e+02, "245.453", "%G"}, + {__LINE__, 2.458958369944980e+14, "2.5E+14", "%6.1E"}, + {__LINE__, 2.463666680775038e+14, "+246366668077503.80", "%+#.2f"}, + {__LINE__, 2.471582990960795e+05, "2.471583e+05", "%e"}, + {__LINE__, 2.474107179274687e+22, "2.47411e+22", "%g"}, + {__LINE__, 2.474332114849132e-08, "+2.47433e-08", "%+g"}, + {__LINE__, 2.476956762431716e+18, "+2476956762431715919.348107", "%+#f"}, + {__LINE__, 2.479091221850607e-22, "0.000000", "%f"}, + {__LINE__, 2.479819586598431e-24, "2.47982e-24", "%g"}, + {__LINE__, 2.480006174601455e-05, "2.48001E-05", "%G"}, + {__LINE__, 2.482672677638334e-26, "+0.000000", "%+f"}, + {__LINE__, 2.492816492208918e-14, " 0", "%7.f"}, + {__LINE__, 2.492964173197140e-12, " 2e-12", "%7.g"}, + {__LINE__, 2.494646635961173e+02, "249.465", "%g"}, + {__LINE__, 2.494687818780545e+11, "249468781878.054546", "%#7f"}, + {__LINE__, 2.510083560147301e-13, "+2.510084e-13", "%+#e"}, + {__LINE__, 2.511186607989480e-30, "2.51E-30", "%.3G"}, + {__LINE__, 2.514164516133643e-11, "2.51416e-11", "%g"}, + {__LINE__, 2.517242520804735e+11, "2.517243e+11", "%e"}, + {__LINE__, 2.518526126400833e-26, "2.51853e-26", "%g"}, + {__LINE__, 2.520313416401176e+09, "2520313416.401176", "%5f"}, + {__LINE__, 2.521653160738683e-29, "2.52165e-29", "%#g"}, + {__LINE__, 2.521941342615338e-16, "2.52194e-16", "%g"}, + {__LINE__, 2.523554774354461e-03, "+3E-03", "%+.0E"}, + {__LINE__, 2.525434130825058e+06, "+2.525434E+06", "%+E"}, + {__LINE__, 2.527616380113364e+02, "2.527616E+02", "%E"}, + {__LINE__, 2.531871347913505e+09, "+2.53187E+09", "%+#G"}, + {__LINE__, 2.532167428661069e+06, "2532167.428661", "%f"}, + {__LINE__, 2.545585718405995e+14, "+2.54559E+14", "%+G"}, + {__LINE__, 2.546305097286406e+10, "2.546305e+10", "%e"}, + {__LINE__, 2.547467156069069e-30, "+0.00", "%+3.2f"}, + {__LINE__, 2.548728793004506e-14, "2.54873E-14", "%G"}, + {__LINE__, 2.560375602395090e+11, "256037560239.509032", "%0f"}, + {__LINE__, 2.560555687476687e+03, "2561", "%.4g"}, + {__LINE__, 2.561709077363443e-24, "+0.000000", "%+f"}, + {__LINE__, 2.563763849675242e+10, "2.5637638E+10", "%.7E"}, + {__LINE__, 2.567482414574355e+11, "+2.567482e+11", "%+e"}, + {__LINE__, 2.571575046303674e-17, "3E-17", "%2.0E"}, + {__LINE__, 2.572997971296986e+11, "257299797129.698593", "%f"}, + {__LINE__, 2.578625935900331e+22, "25786259359003311786295.853381", "%f"}, + {__LINE__, 2.579663282966370e+12, "2.579663e+12", "%e"}, + {__LINE__, 2.593141782397610e-08, "0.000000", "%f"}, + {__LINE__, 2.594386109584196e+04, "3.E+04", "%#6.0E"}, + {__LINE__, 2.598069318007816e-22, "2.59807e-22", "%g"}, + {__LINE__, 2.600910462290091e+09, "2.600910E+09", "%6E"}, + {__LINE__, 2.602580183614458e-03, "2.602580e-03", "%e"}, + {__LINE__, 2.606540360440264e+09, "+2606540360.4", "%+#0.1f"}, + {__LINE__, 2.610016419698116e-21, "+3.e-21", "%+#3.g"}, + {__LINE__, 2.612279323574882e-20, "2.61228E-20", "%G"}, + {__LINE__, 2.614157324336453e-14, "2.61416e-14", "%g"}, + {__LINE__, 2.614688721873993e-09, "2.61e-09", "%6.2e"}, + {__LINE__, 2.616661104472416e-15, " 0", "%5.f"}, + {__LINE__, 2.616742079283195e-09, " +3E-09", "%+7.E"}, + {__LINE__, 2.618075304717427e+24, "2.618075e+24", "%e"}, + {__LINE__, 2.624118573335769e+26, "262411857333576925555007619.550452", "%1f"}, + {__LINE__, 2.625718894274227e+24, "+2625718894274227110940955.753904", "%+f"}, + {__LINE__, 2.627081999477812e+03, "2627.081999", "%f"}, + {__LINE__, 2.631061062224705e+14, "263106106222470.481626", "%f"}, + {__LINE__, 2.631545877128814e+27, "+2.63155e+27", "%+g"}, + {__LINE__, 2.632694395848818e-11, "2.63269e-11", "%g"}, + {__LINE__, 2.636728891022008e-22, "+2.63673e-22", "%+#4g"}, + {__LINE__, 2.638495675008130e-28, "+2.638496e-28", "%+e"}, + {__LINE__, 2.639704195859799e-11, "0.", "%#.0f"}, + {__LINE__, 2.641645264652665e-20, "0", "%0.f"}, + {__LINE__, 2.642012075064497e-20, "3E-20", "%0.E"}, + {__LINE__, 2.648725869006487e-29, " 3E-29", "%6.G"}, + {__LINE__, 2.649060724417770e+12, "3E+12", "%3.G"}, + {__LINE__, 2.651188545120166e-17, "0.000", "%.3f"}, + {__LINE__, 2.652302152621621e+14, "2.7E+14", "%.2G"}, + {__LINE__, 2.653309718412604e-28, "0.000000", "%f"}, + {__LINE__, 2.658344750491858e-21, " 3e-21", "%6.g"}, + {__LINE__, 2.664625074612594e+15, "3e+15", "%1.g"}, + {__LINE__, 2.666953728270800e-15, "3e-15", "%0.e"}, + {__LINE__, 2.674515419678877e-11, " 3E-11", "%6.E"}, + {__LINE__, 2.680054917256578e+15, "2680054917256577.5", "%#2.1f"}, + {__LINE__, 2.680495609883415e-11, "2.68E-11", "%#.3G"}, + {__LINE__, 2.685757893641070e+23, "268575789364107020418259.70809", "%1.5f"}, + {__LINE__, 2.695551226058178e+16, "26955512260581775.407786", "%f"}, + {__LINE__, 2.696346377519671e+04, "+26963.5", "%+G"}, + {__LINE__, 2.714262310601257e+17, "+2.714262E+17", "%+E"}, + {__LINE__, 2.719227450016317e-01, "0.271923", "%G"}, + {__LINE__, 2.724135433346056e-30, "+2.72414e-30", "%+g"}, + {__LINE__, 2.725143224130276e+14, "272514322413027.576530", "%6f"}, + {__LINE__, 2.729355189648310e-03, "+0.00273", "%+.5f"}, + {__LINE__, 2.732872167724945e+11, "2.73287E+11", "%G"}, + {__LINE__, 2.737709476435412e+17, "273770947643541218.141650", "%f"}, + {__LINE__, 2.739299356074967e-15, "2.7E-15", "%.2G"}, + {__LINE__, 2.740793612304798e-27, "2.740794E-27", "%.7G"}, + {__LINE__, 2.742030156175960e+20, "2.74203e+20", "%2.6g"}, + {__LINE__, 2.744455324096085e+11, "2.74446E+11", "%G"}, + {__LINE__, 2.745179546531160e+07, "2.7452E+07", "%.5G"}, + {__LINE__, 2.747470562525333e+04, "3E+04", "%2.E"}, + {__LINE__, 2.750243314674629e+21, "2.75024E+21", "%#G"}, + {__LINE__, 2.750250224436030e+20, "+2.750250e+20", "%+0.6e"}, + {__LINE__, 2.752712858561084e-22, "2.752713e-22", "%e"}, + {__LINE__, 2.756982987656667e-03, "0.002756983", "%#.7G"}, + {__LINE__, 2.761841188479590e+13, "3e+13", "%1.g"}, + {__LINE__, 2.769498440434419e+00, "+3e+00", "%+0.e"}, + {__LINE__, 2.769993785786379e+10, "27699937857.863794", "%f"}, + {__LINE__, 2.772900256376753e-06, "+2.7729E-06", "%+7.6G"}, + {__LINE__, 2.774401482467457e-06, "2.8e-06", "%3.2g"}, + {__LINE__, 2.777691370374757e-02, "+0.027777", "%+f"}, + {__LINE__, 2.779836227365899e-09, "3e-09", "%5.0g"}, + {__LINE__, 2.780481651765741e+01, "2.780482E+01", "%E"}, + {__LINE__, 2.782228478714809e-26, "2.782228E-26", "%4.7G"}, + {__LINE__, 2.790457781719376e+13, "+2.8e+13", "%+.2g"}, + {__LINE__, 2.792026581166417e-30, " 3.e-30", "%#7.0g"}, + {__LINE__, 2.792396278299615e-08, "0.00", "%#.2f"}, + {__LINE__, 2.793658047689995e-10, "2.794e-10", "%#0.4g"}, + {__LINE__, 2.795707358229888e-25, "0.000000", "%f"}, + {__LINE__, 2.796253091758523e-06, "2.796E-06", "%.4G"}, + {__LINE__, 2.796756564788716e-24, "2.796757e-24", "%e"}, + {__LINE__, 2.798730444798773e-14, "2.79873e-14", "%g"}, + {__LINE__, 2.799108397670447e-24, "3E-24", "%.0G"}, + {__LINE__, 2.799389008872835e+06, "2799389.", "%#0.f"}, + {__LINE__, 2.800413998518039e-14, "0.000000", "%f"}, + {__LINE__, 2.803237602587100e+15, "+2803237602587100.418816", "%+5f"}, + {__LINE__, 2.803474415574551e-04, "0.0002803", "%.7f"}, + {__LINE__, 2.804957468647181e+25, "2.80496e+25", "%g"}, + {__LINE__, 2.809345112788226e+09, "2809345112.788226", "%.6f"}, + {__LINE__, 2.811518239408899e+10, "3e+10", "%1.g"}, + {__LINE__, 2.813188439967416e-16, "0.0000000", "%5.7f"}, + {__LINE__, 2.813497118051755e+08, "2.8135e+08", "%.5g"}, + {__LINE__, 2.815748256510577e-29, "0.000000", "%2f"}, + {__LINE__, 2.816076365485207e-01, "0.281608", "%0G"}, + {__LINE__, 2.816083345772131e+24, "2.8161e+24", "%6.4e"}, + {__LINE__, 2.818686957605178e+12, "2.8187E+12", "%.5G"}, + {__LINE__, 2.821903538737691e+20, "2.8219E+20", "%G"}, + {__LINE__, 2.823659589845680e+26, "2.82366e+26", "%2g"}, + {__LINE__, 2.823685630865012e+22, "2.82369E+22", "%G"}, + {__LINE__, 2.828826386007914e+21, "2.828826E+21", "%7E"}, + {__LINE__, 2.830997901034349e+04, "+3e+04", "%+0.e"}, + {__LINE__, 2.833886731091353e-26, "+2.833887e-26", "%+4e"}, + {__LINE__, 2.839965144893913e+28, "28399651448939131626048038015.1421", "%.4f"}, + {__LINE__, 2.840651452049947e-09, "2.841E-09", "%#.3E"}, + {__LINE__, 2.843388001911757e-01, "0.284339", "%g"}, + {__LINE__, 2.846122151080671e+14, "2.84612E+14", "%.5E"}, + {__LINE__, 2.851257828837595e+05, "+285126", "%+5.f"}, + {__LINE__, 2.853823884459520e+07, "+2.85382E+07", "%+G"}, + {__LINE__, 2.853870068368765e-06, "2.854E-06", "%4.3E"}, + {__LINE__, 2.856687358149867e+01, "+28.567", "%+6.5G"}, + {__LINE__, 2.858946600073752e+06, "2.8589466e+06", "%3.7e"}, + {__LINE__, 2.861525727138818e+24, "2.8615257e+24", "%2.7e"}, + {__LINE__, 2.865104175886071e-24, "0.000000", "%f"}, + {__LINE__, 2.870678920363198e-14, "2.87068E-14", "%G"}, + {__LINE__, 2.873298537233691e+09, "+2.8733e+09", "%+g"}, + {__LINE__, 2.877939609444375e-02, "0.028779", "%f"}, + {__LINE__, 2.878253985341728e+13, "2.87825E+13", "%G"}, + {__LINE__, 2.879299244903946e+29, "287929924490394597569320467301", "%0.f"}, + {__LINE__, 2.885516225515485e-26, "0.000000", "%f"}, + {__LINE__, 2.886013724129579e+20, "+3e+20", "%+4.g"}, + {__LINE__, 2.887032786975506e-30, "0.000000", "%f"}, + {__LINE__, 2.890415456531026e-29, "2.89042E-29", "%G"}, + {__LINE__, 2.893177360548214e-13, "2.893E-13", "%#6.3E"}, + {__LINE__, 2.895265536458195e+26, "2.89527e+26", "%5g"}, + {__LINE__, 2.903844533810152e+23, "290384453381015195105874.23", "%.2f"}, + {__LINE__, 2.909060558287504e+27, "2.90906e+27", "%g"}, + {__LINE__, 2.911615480973172e-05, "2.91162E-05", "%3G"}, + {__LINE__, 2.912692297221896e+09, "2912692297.221896", "%#f"}, + {__LINE__, 2.913595662485813e-24, "2.9136E-24", "%G"}, + {__LINE__, 2.915960840341896e-27, "0.000000", "%f"}, + {__LINE__, 2.920451095973991e-16, "2.92e-16", "%2.2e"}, + {__LINE__, 2.920528295810925e+06, "2.92053E+06", "%#G"}, + {__LINE__, 2.921564671017147e+20, "2.921565E+20", "%#6E"}, + {__LINE__, 2.921628350942189e+01, "2.921628E+01", "%E"}, + {__LINE__, 2.921843377455232e+20, "2.921843E+20", "%#E"}, + {__LINE__, 2.930156950319384e+29, "2.930E+29", "%1.3E"}, + {__LINE__, 2.931229858895071e-30, "2.9e-30", "%4.2g"}, + {__LINE__, 2.931753029689166e+20, "2.93175e+20", "%g"}, + {__LINE__, 2.933023693079342e+15, "3.E+15", "%#.1G"}, + {__LINE__, 2.933078733225520e+26, "2.93308E+26", "%G"}, + {__LINE__, 2.933528170896643e-14, "+2.93353e-14", "%+g"}, + {__LINE__, 2.938648437428148e+00, "2.938648e+00", "%e"}, + {__LINE__, 2.940752238221344e+26, "+2.94075E+26", "%+G"}, + {__LINE__, 2.942836470459675e+20, "2.9428e+20", "%6.4e"}, + {__LINE__, 2.943218333254941e+29, "294321833325494086608387078740.634024", "%f"}, + {__LINE__, 2.943690574007512e-27, "0.000000", "%f"}, + {__LINE__, 2.945452907046501e+09, "+2.9455E+09", "%+.5G"}, + {__LINE__, 2.946648725966953e+16, "29466487259669534.637396", "%4f"}, + {__LINE__, 2.955119322417825e+29, "2.95512E+29", "%G"}, + {__LINE__, 2.966481197538973e+24, "2.96648e+24", "%#g"}, + {__LINE__, 2.969362862661362e+08, "+2.969363e+08", "%+7e"}, + {__LINE__, 2.970062459990078e+21, "+2.97006E+21", "%+G"}, + {__LINE__, 2.971059959019791e+10, " 3E+10", "%7.G"}, + {__LINE__, 2.973020205606034e-29, "0.000000", "%f"}, + {__LINE__, 2.977223210301426e+04, "2.977223e+04", "%#e"}, + {__LINE__, 2.977467832772786e-26, "0.000000", "%f"}, + {__LINE__, 2.981433606423384e+18, "2981433606423384397.032805", "%f"}, + {__LINE__, 2.981615513287404e-17, "2.98162e-17", "%g"}, + {__LINE__, 2.983880743847276e+19, " 3e+19", "%7.e"}, + {__LINE__, 2.983927792297305e-28, "2.983928e-28", "%e"}, + {__LINE__, 2.984807359492058e-17, " 0", "%2.f"}, + {__LINE__, 2.985123412933032e+09, "+2.98512e+09", "%+g"}, + {__LINE__, 2.991132955408629e-27, "+2.99113e-27", "%+4g"}, + {__LINE__, 2.992345635923643e+19, "3E+19", "%4.G"}, + {__LINE__, 2.993024238285395e-05, "2.99302e-05", "%2g"}, + {__LINE__, 2.995303406756363e+16, "+2.995303E+16", "%+E"}, + {__LINE__, 2.995492577854335e+19, "+29954925778543346481.797016", "%+f"}, + {__LINE__, 2.996856271675341e-25, "+3e-25", "%+0.e"}, + {__LINE__, 2.997813968112619e+12, "2.99781e+12", "%#5g"}, + {__LINE__, 3.002097486457961e-17, "3.00210e-17", "%5.5e"}, + {__LINE__, 3.006538123158692e+28, "+3.00654E+28", "%+#G"}, + {__LINE__, 3.007293721729544e+04, "30072.937217", "%f"}, + {__LINE__, 3.008301791944493e-07, "3.008e-07", "%.3e"}, + {__LINE__, 3.011742184603817e-17, "3.011742E-17", "%#E"}, + {__LINE__, 3.024641547299177e-30, "3.02464e-30", "%g"}, + {__LINE__, 3.026525135110198e-28, "3e-28", "%.0e"}, + {__LINE__, 3.026768160756558e+20, "3.02677E+20", "%G"}, + {__LINE__, 3.031928829940975e+18, "3031928829940975234.1", "%0.1f"}, + {__LINE__, 3.039787705138620e+24, "+3.E+24", "%+#5.G"}, + {__LINE__, 3.039803824423916e-02, "0.030398", "%5f"}, + {__LINE__, 3.041236293199133e-26, "3.04124e-26", "%g"}, + {__LINE__, 3.045634954037886e+04, "30456.3", "%g"}, + {__LINE__, 3.047528114241850e+25, "3e+25", "%0.e"}, + {__LINE__, 3.047958193737501e-24, "3.04796E-24", "%#G"}, + {__LINE__, 3.049537324414302e-16, "3.04954E-16", "%G"}, + {__LINE__, 3.050500637681798e-30, "3.0505E-30", "%G"}, + {__LINE__, 3.051769626625307e-01, "0.305177", "%f"}, + {__LINE__, 3.057280201971847e+11, "+3.05728E+11", "%+.5E"}, + {__LINE__, 3.060046173306881e-10, "+3.060046e-10", "%+e"}, + {__LINE__, 3.069732667347204e+27, "+3069732667347204291274920251.453295", "%+f"}, + {__LINE__, 3.075060836786916e-14, "0.000000", "%f"}, + {__LINE__, 3.078175179670964e+13, "3.07818E+13", "%G"}, + {__LINE__, 3.080305344303183e-21, "3.08031e-21", "%6g"}, + {__LINE__, 3.088881497064043e+27, "+3.088881E+27", "%+3E"}, + {__LINE__, 3.089196877931209e-20, "3.089197e-20", "%e"}, + {__LINE__, 3.091400888880487e-08, "3e-08", "%2.e"}, + {__LINE__, 3.093410684178904e+16, "3.09341e+16", "%g"}, + {__LINE__, 3.104225344208216e-21, "0.000000", "%f"}, + {__LINE__, 3.105843728248599e+13, "3.e+13", "%#.0g"}, + {__LINE__, 3.106463071390893e-07, "0.0000", "%.4f"}, + {__LINE__, 3.107784123569379e+02, "+310.778", "%+G"}, + {__LINE__, 3.110605148983672e-20, "3.1106051E-20", "%.7E"}, + {__LINE__, 3.111702418162670e-04, "0.00031117", "%.5G"}, + {__LINE__, 3.117610675303153e-17, "3.11761e-17", "%g"}, + {__LINE__, 3.123620125717981e+07, "3.1236e+07", "%2.5g"}, + {__LINE__, 3.125648558809832e-24, "+3.125649E-24", "%+#0.6E"}, + {__LINE__, 3.129025163659283e+10, "31290251636.592825", "%#f"}, + {__LINE__, 3.130689119412709e-29, "+3.131E-29", "%+#.4G"}, + {__LINE__, 3.135681222132527e+13, "3.13568E+13", "%G"}, + {__LINE__, 3.138290485073330e-10, "3.13829E-10", "%G"}, + {__LINE__, 3.141255250155306e-12, "3.14126E-12", "%G"}, + {__LINE__, 3.143224520918429e+25, "3.14322E+25", "%#1G"}, + {__LINE__, 3.144746921366173e-16, " 0", "%7.f"}, + {__LINE__, 3.151336678241994e+19, "+3.15134E+19", "%+G"}, + {__LINE__, 3.161752935716549e+24, "3.2E+24", "%2.1E"}, + {__LINE__, 3.167106521258172e-10, "+3.167107e-10", "%+e"}, + {__LINE__, 3.167266522824146e-04, "3E-04", "%3.E"}, + {__LINE__, 3.169995217131489e+27, "3169995217131488907114089670.358226", "%f"}, + {__LINE__, 3.172117220467692e-21, "3.1721E-21", "%.5G"}, + {__LINE__, 3.173824170351611e+02, " 3e+02", "%6.e"}, + {__LINE__, 3.186291452544739e-28, " 0", "%2.f"}, + {__LINE__, 3.194750094186063e+03, "+3.194750E+03", "%+E"}, + {__LINE__, 3.195001037118137e-20, "0", "%0.0f"}, + {__LINE__, 3.199271564719560e+02, "319.927", "%g"}, + {__LINE__, 3.212803450638544e+20, "3.2128e+20", "%.6g"}, + {__LINE__, 3.214502877263883e+24, "3214502877263882840708220", "%2.f"}, + {__LINE__, 3.214658414074286e-01, "+0.321466", "%+#g"}, + {__LINE__, 3.217627975823673e+00, "3.217628", "%f"}, + {__LINE__, 3.218257328710536e-07, "3.21826e-07", "%6.5e"}, + {__LINE__, 3.221949479347008e+02, "322.194948", "%f"}, + {__LINE__, 3.222862935507443e-14, "3.222863E-14", "%E"}, + {__LINE__, 3.223750247261608e+19, "3.22375e+19", "%g"}, + {__LINE__, 3.236030335782375e+05, "3.236030e+05", "%e"}, + {__LINE__, 3.241078803072735e+19, "3.24108e+19", "%g"}, + {__LINE__, 3.248069569167045e-06, "0.000003", "%#f"}, + {__LINE__, 3.248148973717269e-15, "3.24815E-15", "%G"}, + {__LINE__, 3.251150704311790e+25, "+3.25115e+25", "%+5g"}, + {__LINE__, 3.254498593372140e+15, "+3.2545e+15", "%+g"}, + {__LINE__, 3.254949399612861e-13, "+3.255E-13", "%+7.3E"}, + {__LINE__, 3.255490928554106e-17, "3.255491e-17", "%e"}, + {__LINE__, 3.262333894422112e+17, "326233389442211185.738074", "%0f"}, + {__LINE__, 3.263923577545217e-24, "0.0000000", "%.7f"}, + {__LINE__, 3.267125289609703e+23, "3.26713e+23", "%#.5e"}, + {__LINE__, 3.267906049108331e+27, "3.267906E+27", "%6E"}, + {__LINE__, 3.269885039157328e+14, "326988503915732.797722", "%f"}, + {__LINE__, 3.296011393609953e-27, " 3E-27", "%7.E"}, + {__LINE__, 3.296452164568996e+29, "3.296452E+29", "%E"}, + {__LINE__, 3.296544736555325e+10, "+3.29654E+10", "%+.6G"}, + {__LINE__, 3.301861835798572e+03, "3301.862", "%4.7g"}, + {__LINE__, 3.311292283423002e+21, " 3E+21", "%6.G"}, + {__LINE__, 3.314868440076262e-10, "3.3149E-10", "%.5G"}, + {__LINE__, 3.318057982247451e-13, "3.3181e-13", "%0.5g"}, + {__LINE__, 3.318139218410071e-22, "0.000000", "%f"}, + {__LINE__, 3.326769904839351e-11, "+3.3268e-11", "%+0.4e"}, + {__LINE__, 3.327483659787219e+27, "+3.32748e+27", "%+g"}, + {__LINE__, 3.332666889640198e+17, "333266688964019763.830038", "%f"}, + {__LINE__, 3.333807449377253e+10, "+3.33e+10", "%+.3g"}, + {__LINE__, 3.334572302792625e-09, "3.334572E-09", "%E"}, + {__LINE__, 3.337567126586765e-17, "0.0000000", "%#3.7f"}, + {__LINE__, 3.340512607683786e-04, "+3E-04", "%+1.E"}, + {__LINE__, 3.344911557516870e+04, "33449.1", "%G"}, + {__LINE__, 3.346464302017296e+26, "+3.E+26", "%+#3.G"}, + {__LINE__, 3.348503166628953e+00, "+3.348503e+00", "%+e"}, + {__LINE__, 3.350025157778138e-19, "3.35003e-19", "%6g"}, + {__LINE__, 3.352826233035342e+11, "335282623303.534200", "%7f"}, + {__LINE__, 3.357174089765502e+10, "3.35717E+10", "%G"}, + {__LINE__, 3.369253166475501e+03, "+3369.25", "%+G"}, + {__LINE__, 3.371385975046735e+02, "337.", "%#.0f"}, + {__LINE__, 3.372532650462104e-04, "+0.000", "%+.3f"}, + {__LINE__, 3.376498162710442e-26, "3.376498e-26", "%e"}, + {__LINE__, 3.383510307689135e-12, " 3e-12", "%7.0e"}, + {__LINE__, 3.388427914080631e-04, "0.0003", "%2.4f"}, + {__LINE__, 3.390757423408097e+29, "3.3908E+29", "%.4E"}, + {__LINE__, 3.393963140962879e-11, "3.39396e-11", "%g"}, + {__LINE__, 3.399245742994444e+24, "3.399246E+24", "%E"}, + {__LINE__, 3.417690405546708e-26, "0.00000", "%4.5f"}, + {__LINE__, 3.421159360472045e-30, "3.42116E-30", "%G"}, + {__LINE__, 3.421973947898887e-06, "3.42197E-06", "%4G"}, + {__LINE__, 3.425949002482817e-10, "+3.42595E-10", "%+G"}, + {__LINE__, 3.428355150377972e+08, "3.42836E+08", "%G"}, + {__LINE__, 3.445035158951526e+15, "3445035158951526.454985", "%f"}, + {__LINE__, 3.447363996077534e+28, "+3e+28", "%+2.e"}, + {__LINE__, 3.451571062654907e-03, "0.00345157", "%G"}, + {__LINE__, 3.452787236877688e+09, "3.45279e+09", "%g"}, + {__LINE__, 3.454114826574939e-22, "3.e-22", "%#3.e"}, + {__LINE__, 3.455241965974631e-03, "0.003455", "%#f"}, + {__LINE__, 3.456190496554365e-18, "3.E-18", "%#5.E"}, + {__LINE__, 3.456804565717233e-19, "+0.000", "%+2.3f"}, + {__LINE__, 3.457077651539715e-28, "3.457078E-28", "%E"}, + {__LINE__, 3.472025705376229e+22, "3.472e+22", "%7.4g"}, + {__LINE__, 3.473308270919079e+03, "3.473308e+03", "%e"}, + {__LINE__, 3.474468013243007e-30, "3.47447e-30", "%g"}, + {__LINE__, 3.477976957097698e+22, "3.47798e+22", "%g"}, + {__LINE__, 3.501174631980057e-06, "4e-06", "%4.g"}, + {__LINE__, 3.501808481702140e+20, "3.50181E+20", "%G"}, + {__LINE__, 3.511760000705324e+14, "351176000070532.357000", "%f"}, + {__LINE__, 3.511870226306087e+29, "3.51187e+29", "%.5e"}, + {__LINE__, 3.518846859147841e+02, "+351.885", "%+G"}, + {__LINE__, 3.521659149753418e+25, "35216591497534182053641416", "%5.f"}, + {__LINE__, 3.521721228178747e+21, "4e+21", "%2.e"}, + {__LINE__, 3.526696007281458e+22, "3.5267e+22", "%g"}, + {__LINE__, 3.535631031379084e-11, "4e-11", "%5.0g"}, + {__LINE__, 3.537219665456759e-01, "0.353722", "%f"}, + {__LINE__, 3.537252728287785e-09, "3.537253E-09", "%E"}, + {__LINE__, 3.542561277796815e-19, "3.542561E-19", "%E"}, + {__LINE__, 3.548942336915745e+13, "3.54894E+13", "%.5E"}, + {__LINE__, 3.574385715121768e-24, "3.57439E-24", "%#G"}, + {__LINE__, 3.578068067760211e-13, "3.578068E-13", "%E"}, + {__LINE__, 3.583296432862266e-13, "3.5833e-13", "%g"}, + {__LINE__, 3.591567367115590e+19, "3.59157e+19", "%1g"}, + {__LINE__, 3.594902312287635e-24, "3.5949E-24", "%G"}, + {__LINE__, 3.602929486764515e+12, "3602929486764.514522", "%#f"}, + {__LINE__, 3.606699462631472e-01, "+0.360670", "%+f"}, + {__LINE__, 3.608605968319811e+17, "3.60861e+17", "%g"}, + {__LINE__, 3.608780761567885e+24, "4e+24", "%0.e"}, + {__LINE__, 3.623857274715022e-15, "3.62386E-15", "%1G"}, + {__LINE__, 3.627346251764432e-22, "3.627e-22", "%.4g"}, + {__LINE__, 3.652610391698086e-16, "0.000000", "%3f"}, + {__LINE__, 3.657463146689917e-19, " 0", "%4.0f"}, + {__LINE__, 3.658538858712938e-15, "4E-15", "%5.G"}, + {__LINE__, 3.660337267266058e+16, "+36603372672660579.034698", "%+f"}, + {__LINE__, 3.661588742065142e-01, "0.366159", "%g"}, + {__LINE__, 3.662296387211376e-25, "0.000000", "%f"}, + {__LINE__, 3.668511100303393e-29, "0.000000", "%7f"}, + {__LINE__, 3.669499303661920e+06, "3.6695e+06", "%1.5g"}, + {__LINE__, 3.676856420121343e-23, "3.6769e-23", "%0.4e"}, + {__LINE__, 3.677197473476901e-19, "+3.6772e-19", "%+g"}, + {__LINE__, 3.677281754506453e+12, "+3677281754506.452671", "%+f"}, + {__LINE__, 3.678420802401506e-19, "3.67842E-19", "%G"}, + {__LINE__, 3.679970245325769e+07, "3.67997e+07", "%g"}, + {__LINE__, 3.682528947621349e+05, "4.e+05", "%#5.e"}, + {__LINE__, 3.688257471304210e+22, "36882574713042104441740.320908", "%f"}, + {__LINE__, 3.689129734472166e-01, "0.368913", "%0.6G"}, + {__LINE__, 3.693483801463324e+12, "4E+12", "%3.E"}, + {__LINE__, 3.694612396584729e-21, "+3.69461E-21", "%+G"}, + {__LINE__, 3.706150073392112e-19, "0.000000", "%2f"}, + {__LINE__, 3.706824819530577e+21, "3706824819530576853310.321894", "%f"}, + {__LINE__, 3.709583789659276e+19, "+3.709584E+19", "%+#E"}, + {__LINE__, 3.711137503697284e+22, "3.7111E+22", "%.5G"}, + {__LINE__, 3.717307412969522e-22, "3.717307E-22", "%#E"}, + {__LINE__, 3.717434999853808e+24, " 4E+24", "%6.G"}, + {__LINE__, 3.720761662524312e+14, "+372076166252431.2285", "%+1.4f"}, + {__LINE__, 3.721613815237707e-10, "3.72161e-10", "%g"}, + {__LINE__, 3.725086467464346e+14, "3.72509E+14", "%G"}, + {__LINE__, 3.727427083626536e-15, "+4E-15", "%+.0G"}, + {__LINE__, 3.731138322599465e-13, "3.73114e-13", "%5g"}, + {__LINE__, 3.732248129614146e-04, " 4E-04", "%7.E"}, + {__LINE__, 3.744422223926118e-24, "3.744422E-24", "%E"}, + {__LINE__, 3.745595428897916e+21, "+3745595428897916079336.027906", "%+f"}, + {__LINE__, 3.751308304055989e-08, "+3.751308e-08", "%+e"}, + {__LINE__, 3.755395417696132e-02, "0.037554", "%f"}, + {__LINE__, 3.769103881505159e+09, "3.7691e+09", "%g"}, + {__LINE__, 3.769535572757430e+00, "3.769536", "%6.7G"}, + {__LINE__, 3.770823872348274e-28, "+0.000000", "%+f"}, + {__LINE__, 3.771160653578178e+29, "3.77116e+29", "%g"}, + {__LINE__, 3.776563752716444e-12, "4E-12", "%.0E"}, + {__LINE__, 3.777953798674786e-28, "3.77795e-28", "%g"}, + {__LINE__, 3.785994690686598e+28, "4E+28", "%5.G"}, + {__LINE__, 3.787487488835162e+01, "+37.874875", "%+5f"}, + {__LINE__, 3.788249311173359e-18, "3.788249E-18", "%E"}, + {__LINE__, 3.798728360791314e-08, "3.798728E-08", "%2E"}, + {__LINE__, 3.799038238867092e+05, "3.79904E+05", "%.5E"}, + {__LINE__, 3.799822564549600e+06, "3.79982E+06", "%.5E"}, + {__LINE__, 3.804862840499834e-16, "0", "%1.f"}, + {__LINE__, 3.805375156822481e+12, "3805375156822.4814861", "%.7f"}, + {__LINE__, 3.821612790153376e-17, "3.821613e-17", "%e"}, + {__LINE__, 3.825193659558693e+14, "3.82519E+14", "%#G"}, + {__LINE__, 3.834180638680996e+17, "+383418063868099565.638659", "%+f"}, + {__LINE__, 3.834759760605814e-12, "3.83476E-12", "%G"}, + {__LINE__, 3.839786235582770e+16, "+3.84e+16", "%+0.3g"}, + {__LINE__, 3.843164462248778e+28, "3.84316e+28", "%#g"}, + {__LINE__, 3.845599697858050e+22, "3.8456E+22", "%G"}, + {__LINE__, 3.850147271017228e-21, "3.85e-21", "%.4g"}, + {__LINE__, 3.850283557812101e+07, "3.85028E+07", "%G"}, + {__LINE__, 3.854235609725703e+03, "3854.24", "%G"}, + {__LINE__, 3.864901885489405e-15, "3.8649E-15", "%G"}, + {__LINE__, 3.868630187629983e-24, "3.868630e-24", "%e"}, + {__LINE__, 3.876764606134972e-03, "+3.8767646e-03", "%+#0.7e"}, + {__LINE__, 3.884553592855422e+08, "+3.8845536e+08", "%+4.7e"}, + {__LINE__, 3.887417494351062e+03, "3887.42", "%0g"}, + {__LINE__, 3.887561018972304e+03, "3887.56", "%G"}, + {__LINE__, 3.888554801724658e-24, "3.888555e-24", "%e"}, + {__LINE__, 3.892806891909861e-01, "0.389281", "%f"}, + {__LINE__, 3.914459791345755e+15, "+3.914460E+15", "%+E"}, + {__LINE__, 3.918383209642759e+01, "39.1838", "%#G"}, + {__LINE__, 3.923970658741865e-10, "3.92397E-10", "%G"}, + {__LINE__, 3.928163650272335e+18, "3928163650272335161.162118", "%f"}, + {__LINE__, 3.933053127721002e-20, "3.933053e-20", "%e"}, + {__LINE__, 3.939306552155218e-29, "3.93931E-29", "%G"}, + {__LINE__, 3.953007066379472e+07, "3.953007E+07", "%E"}, + {__LINE__, 3.954924824986267e-01, "3.954925E-01", "%E"}, + {__LINE__, 3.956156072067987e+15, "3.956E+15", "%0.4G"}, + {__LINE__, 3.957756196797224e+22, "4.0E+22", "%4.1E"}, + {__LINE__, 3.960011413261009e+27, "+3960011413261008783592842519.283295", "%+f"}, + {__LINE__, 3.961789076323378e+20, "396178907632337828914.614875", "%f"}, + {__LINE__, 3.961985468081708e-28, "3.96199E-28", "%1G"}, + {__LINE__, 3.975007582283812e-27, "0.000000", "%0.6f"}, + {__LINE__, 3.987586813142132e+15, "+3.98759e+15", "%+g"}, + {__LINE__, 3.992250836957379e+21, "3.99225E+21", "%0G"}, + {__LINE__, 3.992985048620057e+00, "3.9930", "%.4f"}, + {__LINE__, 4.000000000000000e+02, "400.00", "%.2f"}, + {__LINE__, 4.000145414240556e+14, "4.00015e+14", "%#6g"}, + {__LINE__, 4.000774453529974e-25, "4E-25", "%.0E"}, + {__LINE__, 4.002041494804383e+17, "4.002041E+17", "%E"}, + {__LINE__, 4.005505415013214e+17, " 4E+17", "%6.2G"}, + {__LINE__, 4.008960306876491e-28, "0.000", "%.3f"}, + {__LINE__, 4.032337828117640e+16, "4.03234E+16", "%G"}, + {__LINE__, 4.035414597530057e+26, "403541459753005682387083652.429283", "%f"}, + {__LINE__, 4.037065874793069e-01, "0.403707", "%f"}, + {__LINE__, 4.047856284449970e-14, "4.04786E-14", "%#G"}, + {__LINE__, 4.049037221323070e-04, "4.049037e-04", "%e"}, + {__LINE__, 4.053458853142009e-07, "4E-07", "%2.0E"}, + {__LINE__, 4.056455443275955e-22, "4.056455e-22", "%e"}, + {__LINE__, 4.058158020771355e-22, "4.05816e-22", "%#2g"}, + {__LINE__, 4.067283508945137e+02, "+4.07e+02", "%+5.2e"}, + {__LINE__, 4.072155715199509e+11, "4.072e+11", "%.4g"}, + {__LINE__, 4.074643403755990e-22, "4e-22", "%0.g"}, + {__LINE__, 4.077450352325251e+08, "4.1e+08", "%3.2g"}, + {__LINE__, 4.081492619284916e-08, "4.081493E-08", "%E"}, + {__LINE__, 4.083271801996951e-10, "4.083272E-10", "%#E"}, + {__LINE__, 4.090188547940879e-15, "4.090189E-15", "%#E"}, + {__LINE__, 4.091167728938537e-11, "4e-11", "%1.g"}, + {__LINE__, 4.091916745541154e+15, "4091916745541153.588306", "%f"}, + {__LINE__, 4.092366122921161e+23, "4E+23", "%0.E"}, + {__LINE__, 4.094638368212577e-11, "4.09464E-11", "%#G"}, + {__LINE__, 4.097148443124199e-16, "0.000000", "%f"}, + {__LINE__, 4.101057893946401e+06, "4e+06", "%4.g"}, + {__LINE__, 4.101209521231476e+28, "41012095212314756409455185348.633677", "%f"}, + {__LINE__, 4.105683659045903e+05, "+4.E+05", "%+#1.G"}, + {__LINE__, 4.111553717385758e-16, "4.11155e-16", "%g"}, + {__LINE__, 4.112186409918593e+14, "+4.11219e+14", "%+g"}, + {__LINE__, 4.117782144860865e+25, "4.117782E+25", "%5E"}, + {__LINE__, 4.119420921722146e-27, "4.11942e-27", "%g"}, + {__LINE__, 4.134953783635018e+14, "+4.1349538e+14", "%+.7e"}, + {__LINE__, 4.136990822648468e+11, "4.136991E+11", "%E"}, + {__LINE__, 4.140475788523046e+14, "+4E+14", "%+5.G"}, + {__LINE__, 4.149589679915584e+24, "4.14959E+24", "%2G"}, + {__LINE__, 4.151240342256744e-22, " 0", "%2.f"}, + {__LINE__, 4.155533104307272e-04, "+0.000416", "%+f"}, + {__LINE__, 4.155890511880097e+25, "4.155891e+25", "%e"}, + {__LINE__, 4.159907901074450e-19, "4.159908E-19", "%7E"}, + {__LINE__, 4.171899783464252e+18, "4E+18", "%.0E"}, + {__LINE__, 4.177920266276382e-17, "+0.0000", "%+1.4f"}, + {__LINE__, 4.181728014477237e-20, "4.18173e-20", "%.6g"}, + {__LINE__, 4.203600495086497e-30, " 4E-30", "%6.G"}, + {__LINE__, 4.211166112605717e-08, "4E-08", "%5.1G"}, + {__LINE__, 4.219513437404614e+18, "+4E+18", "%+.0E"}, + {__LINE__, 4.221426315236785e+08, "4.221426E+08", "%#E"}, + {__LINE__, 4.226224756005934e-13, " 4.e-13", "%#7.g"}, + {__LINE__, 4.226979046489921e-01, "4.226979e-01", "%#.6e"}, + {__LINE__, 4.236283521629158e-08, "+4.23628E-08", "%+G"}, + {__LINE__, 4.239850628514233e-07, "4.23985E-07", "%.6G"}, + {__LINE__, 4.241177358638621e+04, "42411.773586", "%2f"}, + {__LINE__, 4.245306724398964e-13, "0.000000", "%#f"}, + {__LINE__, 4.246194290300334e+16, "42461942903003340.177293", "%2f"}, + {__LINE__, 4.251238996137952e-05, "4.25124E-05", "%3G"}, + {__LINE__, 4.262432542017438e+13, "42624325420174.375978", "%1f"}, + {__LINE__, 4.266383084300715e+16, "4E+16", "%4.G"}, + {__LINE__, 4.292963398931474e-11, "4.292963E-11", "%E"}, + {__LINE__, 4.296530271399131e-29, "0.000000", "%6f"}, + {__LINE__, 4.303753949741171e+19, "4.30375E+19", "%2G"}, + {__LINE__, 4.303767633827431e-28, "4.30377e-28", "%g"}, + {__LINE__, 4.316181911403991e-30, "4.3162e-30", "%#7.5g"}, + {__LINE__, 4.318654697213126e-18, "4.31865e-18", "%g"}, + {__LINE__, 4.320618603119499e+05, "432061.860312", "%3f"}, + {__LINE__, 4.322443609118441e+21, "4.32244E+21", "%7.5E"}, + {__LINE__, 4.322522446810708e-15, "4.32252e-15", "%.6g"}, + {__LINE__, 4.334728493589115e-18, "0.000000", "%f"}, + {__LINE__, 4.335342531476346e-19, "4.33534E-19", "%G"}, + {__LINE__, 4.340579325084176e-30, "4.34058e-30", "%#0.6g"}, + {__LINE__, 4.340775659883185e+04, "43407.756599", "%6f"}, + {__LINE__, 4.361131891528634e-14, " 4E-14", "%6.G"}, + {__LINE__, 4.366662624371249e-02, "+0.04", "%+4.g"}, + {__LINE__, 4.369919308458348e+28, "+4.36992e+28", "%+2g"}, + {__LINE__, 4.369930393192433e-24, "4.369930e-24", "%e"}, + {__LINE__, 4.376283118322521e-01, "0.437628", "%g"}, + {__LINE__, 4.386868840825930e+19, "4.3869E+19", "%.5G"}, + {__LINE__, 4.389349113395235e+05, "+4.389349e+05", "%+e"}, + {__LINE__, 4.392447466753053e-08, "+4.392E-08", "%+.4G"}, + {__LINE__, 4.394068619246889e-13, "4.39407e-13", "%6g"}, + {__LINE__, 4.404889573700147e-09, "4.404890E-09", "%E"}, + {__LINE__, 4.410531039373014e+05, "+4.41e+05", "%+.2e"}, + {__LINE__, 4.412466606726400e-08, " 0.0", "%6.1f"}, + {__LINE__, 4.414693719279123e+17, "4.414694e+17", "%e"}, + {__LINE__, 4.417050329080679e-01, "+0.441705", "%+g"}, + {__LINE__, 4.419509841929196e-10, "4.4195098E-10", "%.7E"}, + {__LINE__, 4.421012777695611e+07, "+4.42101E+07", "%+0.6G"}, + {__LINE__, 4.426387732151208e+11, "4.42639e+11", "%g"}, + {__LINE__, 4.439567017550398e+26, "4.439567E+26", "%E"}, + {__LINE__, 4.446479816166258e-21, "4.446480e-21", "%0e"}, + {__LINE__, 4.453486178424380e+05, "445348.617842", "%f"}, + {__LINE__, 4.455733696043438e+06, "4.45573e+06", "%3g"}, + {__LINE__, 4.455870606312063e+16, "4.45587e+16", "%g"}, + {__LINE__, 4.458776435431700e+22, "4e+22", "%1.g"}, + {__LINE__, 4.466448605584151e-30, "0.000000", "%f"}, + {__LINE__, 4.471063097005706e+16, "4.47106E+16", "%G"}, + {__LINE__, 4.482001890035190e-22, "+4.482E-22", "%+G"}, + {__LINE__, 4.493246870093631e+05, "449325", "%1G"}, + {__LINE__, 4.496089639281023e+17, "4.49609e+17", "%g"}, + {__LINE__, 4.515066070117557e+15, "4.51507e+15", "%g"}, + {__LINE__, 4.518296460916194e+24, "+4.5183E+24", "%+G"}, + {__LINE__, 4.526548719445596e+02, "452.655", "%6.3f"}, + {__LINE__, 4.532756455106440e-26, "4.53e-26", "%4.2e"}, + {__LINE__, 4.534466782633055e-14, "4.53447E-14", "%G"}, + {__LINE__, 4.541313061854649e-14, "4.54131E-14", "%G"}, + {__LINE__, 4.541848265404338e+02, " 5E+02", "%7.G"}, + {__LINE__, 4.546603085406363e-26, "5E-26", "%3.G"}, + {__LINE__, 4.557349604829375e+28, "+45573496048293753446500886639.", "%+#1.f"}, + {__LINE__, 4.560736449944898e-27, "4.56074e-27", "%g"}, + {__LINE__, 4.563726230559341e-15, "+0.0", "%+2.1f"}, + {__LINE__, 4.572650965532532e-26, "+5E-26", "%+4.G"}, + {__LINE__, 4.574369572115099e-10, "+0.000000", "%+2.6f"}, + {__LINE__, 4.576480601519729e+28, "+4.576481e+28", "%+e"}, + {__LINE__, 4.587487640650499e+02, "+458.75", "%+3.2f"}, + {__LINE__, 4.598365231538559e-27, "4.59837E-27", "%G"}, + {__LINE__, 4.599348244725009e-28, "+4.5993482e-28", "%+#3.7e"}, + {__LINE__, 4.599897524047587e+22, "45998975240475870052136.997401", "%#f"}, + {__LINE__, 4.605415604725077e-25, "0.000000", "%f"}, + {__LINE__, 4.613055015797716e+28, "4.613055E+28", "%E"}, + {__LINE__, 4.618715275814238e-10, "4.618715E-10", "%E"}, + {__LINE__, 4.619044579489540e-14, "0.00", "%.2f"}, + {__LINE__, 4.633693310095410e-20, "+0.000000", "%+f"}, + {__LINE__, 4.648505395281916e-28, "0.000000", "%f"}, + {__LINE__, 4.651232770446398e+21, "4.65e+21", "%.2e"}, + {__LINE__, 4.659743589975352e+28, "+4.65974e+28", "%+g"}, + {__LINE__, 4.660181457075208e-13, "4.66018e-13", "%g"}, + {__LINE__, 4.669787018529686e+20, "4.66979e+20", "%g"}, + {__LINE__, 4.672649286126732e-21, "4.6726E-21", "%4.5G"}, + {__LINE__, 4.675431901120643e-11, " 0", "%3.f"}, + {__LINE__, 4.684404068169945e+26, "468440406816994503458317922.2", "%6.1f"}, + {__LINE__, 4.685438834234642e+19, "5.E+19", "%#6.G"}, + {__LINE__, 4.688915890732712e+00, "+5e+00", "%+2.e"}, + {__LINE__, 4.692999432046297e+00, "4.692999E+00", "%E"}, + {__LINE__, 4.708690772584701e+11, "4.70869e+11", "%g"}, + {__LINE__, 4.711821455782105e-11, "+4.71182e-11", "%+1g"}, + {__LINE__, 4.712413965116830e-01, "4.712414E-01", "%E"}, + {__LINE__, 4.719504715401049e-08, "0.000000", "%f"}, + {__LINE__, 4.719767896031655e+27, "4.71977e+27", "%#g"}, + {__LINE__, 4.722493017411588e-05, "0.000047", "%4f"}, + {__LINE__, 4.729482386761477e+08, "4.72948e+08", "%1g"}, + {__LINE__, 4.730102169800602e-06, "0.000005", "%f"}, + {__LINE__, 4.743951614209393e+24, "4.743952E+24", "%#E"}, + {__LINE__, 4.746077075605921e-16, "4.746E-16", "%.3E"}, + {__LINE__, 4.747802537919248e+24, "4747802537919247889419694", "%4.f"}, + {__LINE__, 4.754727690703025e-26, "0", "%0.f"}, + {__LINE__, 4.756952432926979e-29, " 0.", "%#3.f"}, + {__LINE__, 4.758335147956709e+03, "4758.335148", "%f"}, + {__LINE__, 4.760141880810268e-25, "4.760142e-25", "%e"}, + {__LINE__, 4.767079423650815e-07, "5.E-07", "%#0.G"}, + {__LINE__, 4.785662728343338e-28, "4.78566e-28", "%g"}, + {__LINE__, 4.787146245774150e-13, "+4.78715E-13", "%+G"}, + {__LINE__, 4.787415449888824e-17, "4.78742e-17", "%3g"}, + {__LINE__, 4.794589807429657e-05, "4.79459E-05", "%6G"}, + {__LINE__, 4.794948299666205e+08, "4.79495e+08", "%g"}, + {__LINE__, 4.802292865915992e-04, "4.802293e-04", "%e"}, + {__LINE__, 4.802513688198601e+07, "+4.8e+07", "%+2.3g"}, + {__LINE__, 4.810456746192536e+06, "5E+06", "%0.0G"}, + {__LINE__, 4.820631660081696e+20, "+4.82063E+20", "%+G"}, + {__LINE__, 4.827865857270075e-20, "4.82787e-20", "%g"}, + {__LINE__, 4.835054268490970e+21, "4835054268490970308391.752042", "%#f"}, + {__LINE__, 4.838136770808465e-18, "4.83814E-18", "%G"}, + {__LINE__, 4.839342397882353e-26, "4.839342E-26", "%E"}, + {__LINE__, 4.845130210072029e-16, "0", "%1.f"}, + {__LINE__, 4.845161043167169e-12, "+4.845e-12", "%+.4g"}, + {__LINE__, 4.857425142494964e+01, "48.574251", "%f"}, + {__LINE__, 4.858118337285513e-10, "+4.8581e-10", "%+.5g"}, + {__LINE__, 4.867478343525339e-15, "+4.867478E-15", "%+E"}, + {__LINE__, 4.886137001331278e-11, "0.000000", "%f"}, + {__LINE__, 4.886835850687998e-20, " 0", "%6.0f"}, + {__LINE__, 4.886880737482383e+26, "488688073748238327453918827.814050", "%4f"}, + {__LINE__, 4.888812049144075e-22, "4.888812E-22", "%E"}, + {__LINE__, 4.895869618002905e+02, "+489.587", "%+.7g"}, + {__LINE__, 4.902046593298549e+09, " 5e+09", "%7.g"}, + {__LINE__, 4.907918627564751e-05, "4.91e-05", "%.3g"}, + {__LINE__, 4.916048355579009e+19, "49160483555790088772", "%2.f"}, + {__LINE__, 4.917197806128638e+14, "+5e+14", "%+5.g"}, + {__LINE__, 4.918303274189911e+13, "4.918303e+13", "%e"}, + {__LINE__, 4.922687970321108e+26, "492268797032110771993984599.2485", "%0.4f"}, + {__LINE__, 4.929263362431195e+14, "4.92926e+14", "%g"}, + {__LINE__, 4.933385398543267e-17, "0.000000", "%#f"}, + {__LINE__, 4.942367126455025e+00, " +5", "%+6.f"}, + {__LINE__, 4.947687486717652e-04, "0.000495", "%.6f"}, + {__LINE__, 4.960855200003128e-08, "+4.960855e-08", "%+e"}, + {__LINE__, 4.968485435774085e-21, "0.0000", "%#2.4f"}, + {__LINE__, 4.972088381506133e+01, "49.7", "%.1f"}, + {__LINE__, 4.977561524480392e-15, "4.977562E-15", "%7E"}, + {__LINE__, 4.984592111249502e-13, "5.0e-13", "%.1e"}, + {__LINE__, 4.997502335602838e-14, "4.998e-14", "%6.4g"}, + {__LINE__, 5.001731554335935e-09, "5.00173e-09", "%g"}, + {__LINE__, 5.006231697107042e-17, "5.0062e-17", "%1.5g"}, + {__LINE__, 5.008035801093423e+24, "5.008036E+24", "%#E"}, + {__LINE__, 5.008756965733827e-28, "5.009E-28", "%.4G"}, + {__LINE__, 5.009774027622812e+20, "+5.00977E+20", "%+G"}, + {__LINE__, 5.010576312346293e+29, "5.01058E+29", "%G"}, + {__LINE__, 5.012335250996786e+29, "5.012335e+29", "%.7g"}, + {__LINE__, 5.012404365186907e+20, "5E+20", "%3.2G"}, + {__LINE__, 5.024670103250229e-01, "+5.024670E-01", "%+E"}, + {__LINE__, 5.025765369164560e+03, "5025.77", "%g"}, + {__LINE__, 5.027173841003918e+11, "+5.02717E+11", "%+G"}, + {__LINE__, 5.032093817639893e-26, "5e-26", "%3.e"}, + {__LINE__, 5.039636818525848e-02, "0.050396", "%#f"}, + {__LINE__, 5.040626671307691e+23, "5.040627E+23", "%E"}, + {__LINE__, 5.040788233368296e-11, "+5.04079e-11", "%+g"}, + {__LINE__, 5.040903321336150e-28, "5.0409E-28", "%G"}, + {__LINE__, 5.042078512958994e+12, "5.04208E+12", "%G"}, + {__LINE__, 5.045957900223303e+15, "5045957900223303", "%0.f"}, + {__LINE__, 5.048298764352134e-14, "5.048299e-14", "%1e"}, + {__LINE__, 5.048827326763192e+28, "50488273267631917917697137454.524636", "%f"}, + {__LINE__, 5.049714558347361e-23, "5e-23", "%5.0g"}, + {__LINE__, 5.051509904923853e+16, "50515099049238534", "%5.f"}, + {__LINE__, 5.052789863743305e-10, "5.05279E-10", "%6G"}, + {__LINE__, 5.057429728861999e-29, "0.000", "%#1.3f"}, + {__LINE__, 5.075313093968501e-13, "+5.07531e-13", "%+g"}, + {__LINE__, 5.075874503501582e+29, "5e+29", "%2.e"}, + {__LINE__, 5.079468079020803e+01, "50.795", "%.5G"}, + {__LINE__, 5.086214826494080e-28, " 0.0", "%4.1f"}, + {__LINE__, 5.096054881114421e+13, "5.096e+13", "%.4g"}, + {__LINE__, 5.099045274853458e-23, "5.09905E-23", "%0G"}, + {__LINE__, 5.100248195124433e+00, "5.100248", "%f"}, + {__LINE__, 5.101016114276598e-20, "5e-20", "%4.1g"}, + {__LINE__, 5.103213528670269e+07, "51032135.286703", "%6f"}, + {__LINE__, 5.108373625126768e+08, "+510837362.512677", "%+f"}, + {__LINE__, 5.113097157183416e+07, "5.1131E+07", "%#.4E"}, + {__LINE__, 5.114452611789777e+08, "+5.11445E+08", "%+G"}, + {__LINE__, 5.117099184715288e-16, "5.1171E-16", "%G"}, + {__LINE__, 5.119910534665511e-13, "+5.11991E-13", "%+G"}, + {__LINE__, 5.121320931953720e+23, "+5.121321e+23", "%+#e"}, + {__LINE__, 5.121900318443998e+06, "5.121900e+06", "%e"}, + {__LINE__, 5.135698679084286e+00, "5.1357", "%2g"}, + {__LINE__, 5.146355903104154e-10, "+5.14636E-10", "%+.6G"}, + {__LINE__, 5.148754210958986e-24, "+5.148754e-24", "%+e"}, + {__LINE__, 5.156238368448428e+26, "5e+26", "%5.g"}, + {__LINE__, 5.159924520667922e-19, "+5E-19", "%+0.1G"}, + {__LINE__, 5.159938266135425e-27, "0.000000", "%f"}, + {__LINE__, 5.165915205175676e-01, "5.165915e-01", "%e"}, + {__LINE__, 5.190205499401547e-11, "5e-11", "%2.1g"}, + {__LINE__, 5.193260005542003e+26, "5.19326e+26", "%#g"}, + {__LINE__, 5.194732077318269e+08, "519473207.731827", "%f"}, + {__LINE__, 5.196394616633798e-19, "0", "%.0f"}, + {__LINE__, 5.206753628035638e+19, "5.20675e+19", "%6.6g"}, + {__LINE__, 5.207364136540851e-23, " 0.000", "%6.3f"}, + {__LINE__, 5.210672737132108e-09, "5.21067E-09", "%G"}, + {__LINE__, 5.211423933057123e-20, "+5.21142E-20", "%+G"}, + {__LINE__, 5.219749528363367e-27, "5.219750e-27", "%e"}, + {__LINE__, 5.228031587478653e-06, "+5.2E-06", "%+3.1E"}, + {__LINE__, 5.229303095005359e-25, "+0.000000", "%+#f"}, + {__LINE__, 5.234703511938320e-06, " 0", "%7.f"}, + {__LINE__, 5.235655046937822e-20, "5.235655E-20", "%.6E"}, + {__LINE__, 5.237924986002288e-01, "+0.52379", "%+.5f"}, + {__LINE__, 5.249148093603826e+20, "5.24915E+20", "%0G"}, + {__LINE__, 5.262680920244596e+16, "5.26268E+16", "%G"}, + {__LINE__, 5.267944704715845e-06, "5.e-06", "%#3.g"}, + {__LINE__, 5.268543533730505e-08, "5.26854E-08", "%G"}, + {__LINE__, 5.275727269515247e-02, "0.0527573", "%.6g"}, + {__LINE__, 5.279215316873723e+24, "5279215316873722585455652.733799", "%f"}, + {__LINE__, 5.284326985680811e+16, "52843269856808108.286828", "%6f"}, + {__LINE__, 5.286192957344040e-10, "5.3E-10", "%.1E"}, + {__LINE__, 5.289451976001091e-23, "5.28945E-23", "%G"}, + {__LINE__, 5.289512908209300e+09, "+5.289513e+09", "%+e"}, + {__LINE__, 5.295696957972123e+11, "5.296E+11", "%#.3E"}, + {__LINE__, 5.303584684011050e+16, "53035846840110503.208621", "%f"}, + {__LINE__, 5.310315581980172e+23, "531031558198017172855998", "%4.f"}, + {__LINE__, 5.317492728410062e-14, "5.31749e-14", "%7g"}, + {__LINE__, 5.324506949499409e+18, "5.3245E+18", "%.5G"}, + {__LINE__, 5.329238068668336e-20, "5e-20", "%4.g"}, + {__LINE__, 5.336050125161774e+14, "5.33605e+14", "%g"}, + {__LINE__, 5.349921315003169e-17, "5.34992E-17", "%.6G"}, + {__LINE__, 5.355648481782587e-20, "5.355648e-20", "%e"}, + {__LINE__, 5.358945557589489e-26, "+5e-26", "%+0.g"}, + {__LINE__, 5.359638846465574e+21, "5.359639E+21", "%E"}, + {__LINE__, 5.377048469393900e+14, "5.377E+14", "%.5G"}, + {__LINE__, 5.387471194156434e+23, "5.387471E+23", "%.7G"}, + {__LINE__, 5.401622578962497e-03, "+0.0054", "%+5.2g"}, + {__LINE__, 5.406882732497444e-16, "5.406883E-16", "%E"}, + {__LINE__, 5.421474560523198e-11, "+5.42147e-11", "%+g"}, + {__LINE__, 5.440249323479418e-26, "5.44025E-26", "%7G"}, + {__LINE__, 5.440922682921101e-05, "5e-05", "%.1g"}, + {__LINE__, 5.444400103673185e-01, "0.54444", "%G"}, + {__LINE__, 5.449339470916152e+09, "5449339470.9162", "%.4f"}, + {__LINE__, 5.451583259558706e-15, "5.e-15", "%#4.e"}, + {__LINE__, 5.460153018660573e+26, "+546015301866057267687892817.775719", "%+f"}, + {__LINE__, 5.477591210511918e+08, "+5.47759E+08", "%+0.5E"}, + {__LINE__, 5.481665401407188e-02, "5.481665e-02", "%e"}, + {__LINE__, 5.482670784411319e-16, "5E-16", "%4.G"}, + {__LINE__, 5.487494580636099e+12, "5.48749e+12", "%g"}, + {__LINE__, 5.508630369473937e-10, "5.508630e-10", "%e"}, + {__LINE__, 5.528898001438273e+20, "6e+20", "%4.g"}, + {__LINE__, 5.530331734443152e-24, " 6e-24", "%6.g"}, + {__LINE__, 5.531704398969656e-24, "0.000000", "%f"}, + {__LINE__, 5.539906569043238e+05, "+553990.656904", "%+f"}, + {__LINE__, 5.554571854756323e-08, "+0.00000", "%+4.5f"}, + {__LINE__, 5.555155069925145e-28, "5.55516e-28", "%g"}, + {__LINE__, 5.555302705075539e-08, "+5.5553e-08", "%+g"}, + {__LINE__, 5.563805076159055e-05, "5.563805e-05", "%e"}, + {__LINE__, 5.565063508995002e+07, "5.565E+07", "%.3E"}, + {__LINE__, 5.566281664807526e-08, "5.56628E-08", "%G"}, + {__LINE__, 5.575245679736338e-08, "5.575246e-08", "%e"}, + {__LINE__, 5.578254777281501e-21, "0.", "%#.0f"}, + {__LINE__, 5.582389275770848e-01, " 0.6", "%5.G"}, + {__LINE__, 5.592215029176133e-04, "0.00056", "%.2g"}, + {__LINE__, 5.593536789867047e-25, "5.59354e-25", "%g"}, + {__LINE__, 5.605652054074862e-03, " 0.006", "%8.G"}, + {__LINE__, 5.606982382643258e+20, "+5.60698E+20", "%+G"}, + {__LINE__, 5.614646821116853e-18, "5.6E-18", "%.2G"}, + {__LINE__, 5.625586848606565e-11, "+5.62559E-11", "%+G"}, + {__LINE__, 5.626300428046732e+26, " 6e+26", "%7.g"}, + {__LINE__, 5.638236626881086e-17, " 6E-17", "%6.G"}, + {__LINE__, 5.645744817075691e+04, "5.645745E+04", "%E"}, + {__LINE__, 5.651410004691736e+06, "6.E+06", "%#3.E"}, + {__LINE__, 5.659582345929256e-11, "+0.000000", "%+f"}, + {__LINE__, 5.670968861413510e+16, "+5.67097E+16", "%+G"}, + {__LINE__, 5.672476851235796e+01, "+56.724769", "%+0f"}, + {__LINE__, 5.674563779921248e+28, " 6E+28", "%6.0G"}, + {__LINE__, 5.675558529939025e+19, "5.67556e+19", "%3g"}, + {__LINE__, 5.676326888314589e-08, " 6e-08", "%7.g"}, + {__LINE__, 5.677928507840897e-30, "5.67793e-30", "%0g"}, + {__LINE__, 5.686622552402630e-15, "5.69E-15", "%1.3G"}, + {__LINE__, 5.710441686922142e-14, "5.710442E-14", "%E"}, + {__LINE__, 5.713234603280163e-21, "0.00000", "%0.5f"}, + {__LINE__, 5.714968959046963e+12, "5.71497e+12", "%g"}, + {__LINE__, 5.722025141555638e-23, "5.72e-23", "%.2e"}, + {__LINE__, 5.725398571007033e-09, "5.7254E-09", "%.6G"}, + {__LINE__, 5.751604813862738e+18, "6E+18", "%.1G"}, + {__LINE__, 5.761025444751985e+20, "5.76103e+20", "%g"}, + {__LINE__, 5.762315767948593e+15, "5.76232e+15", "%4g"}, + {__LINE__, 5.764528858586032e-15, "5.764529E-15", "%6.7G"}, + {__LINE__, 5.766408541535810e-07, " 6E-07", "%6.1G"}, + {__LINE__, 5.771831571087174e-01, "0.577183", "%f"}, + {__LINE__, 5.790102497364865e-15, "+5.790102e-15", "%+e"}, + {__LINE__, 5.790222335547785e-08, "5.79022e-08", "%0.6g"}, + {__LINE__, 5.794082127091130e+21, "5794082127091130018925.468903", "%f"}, + {__LINE__, 5.804568463644165e+28, "5.80457e+28", "%g"}, + {__LINE__, 5.827356651901066e+20, "582735665190106555400.006982", "%f"}, + {__LINE__, 5.837553387436408e+18, "5.837553E+18", "%3E"}, + {__LINE__, 5.844458110907209e+22, "5.84446e+22", "%g"}, + {__LINE__, 5.851672125746866e-19, "5.85167e-19", "%g"}, + {__LINE__, 5.868843476784172e-12, "5.868843e-12", "%1.7g"}, + {__LINE__, 5.870854146748864e-04, "0.000587", "%2.3g"}, + {__LINE__, 5.877787821470433e+01, "+58.77788", "%+#3.5f"}, + {__LINE__, 5.881333514866498e+04, "5.881334E+04", "%E"}, + {__LINE__, 5.908427816128965e+28, "+5.908428E+28", "%+E"}, + {__LINE__, 5.918139800007388e+07, "59181398.000074", "%f"}, + {__LINE__, 5.925587467475260e+21, "5925587467475259551008.548442", "%f"}, + {__LINE__, 5.930403502679683e-22, "5.9304E-22", "%.6G"}, + {__LINE__, 5.961572644847521e+02, "+596.157", "%+G"}, + {__LINE__, 5.968917715225198e-21, "5.96892e-21", "%4g"}, + {__LINE__, 5.982184454670110e-08, "0", "%0.f"}, + {__LINE__, 5.988414319040855e+09, "6E+09", "%0.G"}, + {__LINE__, 5.996263783103995e-10, "5.996264e-10", "%e"}, + {__LINE__, 6.004634162276014e-18, "6.00463e-18", "%g"}, + {__LINE__, 6.018734975519166e-08, "6.01873E-08", "%G"}, + {__LINE__, 6.029071822986717e-14, "0.0000000", "%.7f"}, + {__LINE__, 6.030392278117406e+23, "6.030392e+23", "%e"}, + {__LINE__, 6.034553399237175e-27, "+6.03455e-27", "%+g"}, + {__LINE__, 6.034938873443862e+24, "6034938873443861595546877.651941", "%f"}, + {__LINE__, 6.038929148003457e-16, "6.03893e-16", "%g"}, + {__LINE__, 6.039149626573702e-13, "6.0391E-13", "%3.5G"}, + {__LINE__, 6.041247664739301e+29, "6.041248E+29", "%3E"}, + {__LINE__, 6.044220746789781e+20, "+6.04422e+20", "%+g"}, + {__LINE__, 6.045775647107433e+19, "6E+19", "%2.G"}, + {__LINE__, 6.051917010461611e-05, "6.05192E-05", "%G"}, + {__LINE__, 6.059773181566425e+17, "+605977318156642486", "%+5.f"}, + {__LINE__, 6.082239575475813e+01, "+6E+01", "%+.1G"}, + {__LINE__, 6.093984174629151e+18, "6.093984e+18", "%e"}, + {__LINE__, 6.102368380479137e+22, "6.1E+22", "%.2G"}, + {__LINE__, 6.103097446204007e+25, "61030974462040070704915706.301108", "%#0f"}, + {__LINE__, 6.105244994410556e-24, "0.0000000", "%2.7f"}, + {__LINE__, 6.110828906801633e-14, "+6.110829e-14", "%+3e"}, + {__LINE__, 6.124439072468681e-30, "6.12444e-30", "%g"}, + {__LINE__, 6.127714703273447e-15, "0", "%0.f"}, + {__LINE__, 6.128539582408870e-15, "6.12854e-15", "%g"}, + {__LINE__, 6.145470264744349e+24, "6145470264744348654062378.045637", "%f"}, + {__LINE__, 6.159177701667455e+26, "615917770166745537352426254.074089", "%#f"}, + {__LINE__, 6.159348213337442e+17, "6.15935e+17", "%g"}, + {__LINE__, 6.161953891020492e-21, "0.000000", "%f"}, + {__LINE__, 6.169474192777305e+29, "+6.169E+29", "%+.4G"}, + {__LINE__, 6.198519761010651e-15, "+0", "%+1.f"}, + {__LINE__, 6.201381824304919e-27, " 0", "%5.f"}, + {__LINE__, 6.204387065736372e-18, "6.204387E-18", "%7E"}, + {__LINE__, 6.209270088144063e-24, "0.000000", "%f"}, + {__LINE__, 6.209871738353546e-20, "6.20987E-20", "%G"}, + {__LINE__, 6.222733191871026e+14, "+6.22273e+14", "%+g"}, + {__LINE__, 6.234670085354310e+04, "+62346.700854", "%+f"}, + {__LINE__, 6.258389346602224e-09, "6E-09", "%.0G"}, + {__LINE__, 6.263326582587503e+12, "6263326582588.", "%#5.f"}, + {__LINE__, 6.272768026763922e+03, "+6272.77", "%+g"}, + {__LINE__, 6.287747868625765e+00, "6E+00", "%.0E"}, + {__LINE__, 6.297724027388795e-17, "+6E-17", "%+3.G"}, + {__LINE__, 6.309940679156400e+27, "6.30994e+27", "%6.5e"}, + {__LINE__, 6.312960327030170e+06, "+6.312960E+06", "%+E"}, + {__LINE__, 6.317658179632976e+02, "+631.766", "%+g"}, + {__LINE__, 6.321255960699571e-27, "0.000000", "%f"}, + {__LINE__, 6.323069932833900e+03, "6323", "%.4G"}, + {__LINE__, 6.337027835933034e+21, "6.33703e+21", "%#g"}, + {__LINE__, 6.343583177899838e-03, "0.00634358", "%7G"}, + {__LINE__, 6.347464241733904e-03, "6.e-03", "%#0.e"}, + {__LINE__, 6.352038720353924e+07, "63520387.203539", "%f"}, + {__LINE__, 6.355292378565109e+23, "+635529237856510942350496.407016", "%+f"}, + {__LINE__, 6.377901944439164e-29, "0.000", "%2.3f"}, + {__LINE__, 6.378660255554053e-04, "6.37866E-04", "%.5E"}, + {__LINE__, 6.382838195255167e-18, "0.000000", "%f"}, + {__LINE__, 6.391046303581911e+22, "6E+22", "%.0G"}, + {__LINE__, 6.404812348309621e+22, "6.404812E+22", "%.7G"}, + {__LINE__, 6.406154091357119e+16, "6.40615E+16", "%G"}, + {__LINE__, 6.419414726557980e+21, "+6.41941e+21", "%+g"}, + {__LINE__, 6.432166563008504e+10, " +6e+10", "%+7.g"}, + {__LINE__, 6.438463704193190e+19, "6.438464e+19", "%e"}, + {__LINE__, 6.445270237908565e+15, "+6.445270E+15", "%+E"}, + {__LINE__, 6.445536770502964e+09, "6.446e+09", "%.3e"}, + {__LINE__, 6.446614224811444e+28, "64466142248114444157636171439.662576", "%f"}, + {__LINE__, 6.457046289718297e+18, "6457046289718297416.015957", "%4f"}, + {__LINE__, 6.457682856890173e+01, "6.457683E+01", "%2E"}, + {__LINE__, 6.471026352792729e-16, "+6.47103e-16", "%+g"}, + {__LINE__, 6.474527749567342e+19, "6E+19", "%.1G"}, + {__LINE__, 6.481178401781131e-24, "+6.48E-24", "%+6.3G"}, + {__LINE__, 6.490736647261461e-15, "+6.49074e-15", "%+g"}, + {__LINE__, 6.493196535069719e+23, "+6.4932E+23", "%+G"}, + {__LINE__, 6.500296992935538e-20, "+0.0", "%+.1f"}, + {__LINE__, 6.502867735895890e-19, "+0.000000", "%+6f"}, + {__LINE__, 6.506627529164683e+14, "650662752916468", "%5.f"}, + {__LINE__, 6.511909298966434e-15, "+6.51191E-15", "%+G"}, + {__LINE__, 6.514463062693312e+01, "65.1446", "%G"}, + {__LINE__, 6.520721469484543e+16, "6.52072E+16", "%G"}, + {__LINE__, 6.528064508731680e-05, "6.528065E-05", "%7E"}, + {__LINE__, 6.529007214194039e-24, "0.000000", "%f"}, + {__LINE__, 6.537822760557410e-23, "0.000000", "%f"}, + {__LINE__, 6.552222096390805e+29, "+6.55222E+29", "%+G"}, + {__LINE__, 6.554569862717104e+12, "6.55457E+12", "%G"}, + {__LINE__, 6.563440840359989e-30, "7E-30", "%4.G"}, + {__LINE__, 6.586246985535526e+17, "6.58625E+17", "%#4G"}, + {__LINE__, 6.593339522442827e+16, "6.6E+16", "%#4.1E"}, + {__LINE__, 6.611179030024350e+02, "7E+02", "%4.E"}, + {__LINE__, 6.617541638586767e-10, "6.617542E-10", "%E"}, + {__LINE__, 6.619147920886991e-28, "7e-28", "%.1g"}, + {__LINE__, 6.622304444772819e-21, "6.62230E-21", "%#4G"}, + {__LINE__, 6.634766655157910e+12, "6634766655158", "%2.f"}, + {__LINE__, 6.635639771921218e+25, "+6.63564E+25", "%+G"}, + {__LINE__, 6.644575305929087e+17, "+6.64458e+17", "%+g"}, + {__LINE__, 6.648697591328432e+04, "6.648698E+04", "%3E"}, + {__LINE__, 6.665426012448100e+09, "6.66543E+09", "%G"}, + {__LINE__, 6.675222780441723e-23, "6.67522e-23", "%1g"}, + {__LINE__, 6.689765053880623e+00, "6.6898", "%.5g"}, + {__LINE__, 6.693874943680238e+25, "7.e+25", "%#1.g"}, + {__LINE__, 6.695033453546435e+19, "6.695033E+19", "%E"}, + {__LINE__, 6.720131534244976e+03, "+6720.132", "%+.3f"}, + {__LINE__, 6.725287004784564e+19, "+6.72529e+19", "%+g"}, + {__LINE__, 6.743599626906313e-18, "0.0000000", "%7.7f"}, + {__LINE__, 6.755534260704152e+29, "7.e+29", "%#5.e"}, + {__LINE__, 6.761855244766418e-18, "6.8E-18", "%.2G"}, + {__LINE__, 6.764974143681080e-02, "6.765E-02", "%2.3E"}, + {__LINE__, 6.766924477711975e-17, "0.000000", "%4f"}, + {__LINE__, 6.772195434106330e-19, "6.7722E-19", "%G"}, + {__LINE__, 6.779433073319225e-23, "0.00", "%.2f"}, + {__LINE__, 6.790317710068964e+05, "6.8e+05", "%.1e"}, + {__LINE__, 6.791378160292960e+02, "679.137816", "%f"}, + {__LINE__, 6.798381262104190e-27, "0.000000", "%f"}, + {__LINE__, 6.804165939424860e-14, "7e-14", "%4.g"}, + {__LINE__, 6.810668670623699e+11, "681066867062.369852", "%f"}, + {__LINE__, 6.838942637635821e-17, "+6.84e-17", "%+.2e"}, + {__LINE__, 6.868523988329111e-09, "+6.868524e-09", "%+e"}, + {__LINE__, 6.873228061403223e-15, "6.873228E-15", "%3E"}, + {__LINE__, 6.879370500093334e-12, "+0.000000", "%+f"}, + {__LINE__, 6.891525498686674e-10, "7e-10", "%0.g"}, + {__LINE__, 6.923027319286220e+21, "7.e+21", "%#1.g"}, + {__LINE__, 6.923565533024560e-11, "+6.92357e-11", "%+g"}, + {__LINE__, 6.931415640770737e-24, "+6.93142e-24", "%+g"}, + {__LINE__, 6.936582619246057e+09, "6.936583e+09", "%7e"}, + {__LINE__, 6.938661496670582e+05, "693866.1496671", "%2.7f"}, + {__LINE__, 6.946115378286550e-25, "0.000000", "%f"}, + {__LINE__, 6.955823211921219e-09, "6.95582e-09", "%g"}, + {__LINE__, 6.962365243425770e+29, "696236524342577034474288666388.019919", "%7f"}, + {__LINE__, 6.970432274812882e+05, "+7e+05", "%+1.0e"}, + {__LINE__, 6.975784942897122e-13, "6.975785e-13", "%e"}, + {__LINE__, 6.976786489904214e-07, "6.97679E-07", "%3G"}, + {__LINE__, 6.985245976357042e-05, "0.00", "%.2f"}, + {__LINE__, 6.993402879410720e-21, "6.9934e-21", "%g"}, + {__LINE__, 7.012183985341519e+28, "+70121839853415188770213717362", "%+2.f"}, + {__LINE__, 7.028670648856025e-15, "7.028671e-15", "%.7g"}, + {__LINE__, 7.034042985683665e-03, " 0", "%2.f"}, + {__LINE__, 7.044054273278726e+18, "7.044e+18", "%7.4g"}, + {__LINE__, 7.049706744250734e-06, "7E-06", "%0.E"}, + {__LINE__, 7.061982657056197e+13, "+7.061983e+13", "%+e"}, + {__LINE__, 7.066873668945899e+21, "7.06687E+21", "%G"}, + {__LINE__, 7.087941418633258e+26, "7.08794E+26", "%G"}, + {__LINE__, 7.108268134631547e+22, "7.108268E+22", "%E"}, + {__LINE__, 7.112095848565475e-19, "7.1121e-19", "%g"}, + {__LINE__, 7.116326241291862e+16, "7.116326e+16", "%e"}, + {__LINE__, 7.118854843597607e-22, "0.000000", "%f"}, + {__LINE__, 7.121423043456375e-27, "0.000000", "%f"}, + {__LINE__, 7.131415427096460e-03, "0.007131", "%3f"}, + {__LINE__, 7.132023279679892e+22, "7.e+22", "%#3.e"}, + {__LINE__, 7.146250280189992e-18, "0.000000", "%f"}, + {__LINE__, 7.150059058390724e+03, "7150.06", "%g"}, + {__LINE__, 7.161081578177381e-02, "+0.0716108", "%+G"}, + {__LINE__, 7.164935125149336e-27, "7.2E-27", "%#6.2G"}, + {__LINE__, 7.173125717489549e-10, "7.173126e-10", "%e"}, + {__LINE__, 7.174199549624193e+00, "7.1741995", "%.7f"}, + {__LINE__, 7.192493588077649e+27, "7.19249e+27", "%g"}, + {__LINE__, 7.215209568601445e-06, "7.215210e-06", "%e"}, + {__LINE__, 7.238322284100497e+03, "7238.3", "%.1f"}, + {__LINE__, 7.239203871123613e+06, "7239204", "%4.f"}, + {__LINE__, 7.245809072577019e-09, "+7.245809E-09", "%+E"}, + {__LINE__, 7.256275686433336e+03, "7.256276e+03", "%e"}, + {__LINE__, 7.265774291605193e+27, "+7.26577E+27", "%+G"}, + {__LINE__, 7.287968172227119e-21, "+7.287968e-21", "%+e"}, + {__LINE__, 7.306020169678527e+12, "7E+12", "%2.E"}, + {__LINE__, 7.315587463572568e-04, "0.", "%#.0f"}, + {__LINE__, 7.324627764547963e+09, "7.324628E+09", "%#E"}, + {__LINE__, 7.331904966719081e-06, "+7.3319E-06", "%+G"}, + {__LINE__, 7.334448152798243e-02, "0.07", "%0.g"}, + {__LINE__, 7.334487195961240e-01, "+0.733449", "%+#g"}, + {__LINE__, 7.381283575515707e+13, "7.381284E+13", "%E"}, + {__LINE__, 7.394854567245476e-11, "0.000000", "%7f"}, + {__LINE__, 7.401950211415377e-08, " 7e-08", "%6.e"}, + {__LINE__, 7.409023867864680e+03, "7409.023868", "%#3f"}, + {__LINE__, 7.411912956257733e-20, "7e-20", "%0.g"}, + {__LINE__, 7.445275019272160e+11, "744527501927.2159511", "%.7f"}, + {__LINE__, 7.450279765616891e-16, "7.45028e-16", "%.6g"}, + {__LINE__, 7.467047411334495e+14, "7.46705E+14", "%G"}, + {__LINE__, 7.485628870972725e+28, "7E+28", "%.0G"}, + {__LINE__, 7.495391782588563e+11, "749539178258.856253", "%f"}, + {__LINE__, 7.495824101611911e+06, "7.49582e+06", "%.6g"}, + {__LINE__, 7.499759867592402e+20, "+749975986759240154100.3", "%+.1f"}, + {__LINE__, 7.508983397140368e+04, "75089.8", "%g"}, + {__LINE__, 7.512585199581016e-27, "7.512585E-27", "%.6E"}, + {__LINE__, 7.516831372212545e-29, "+7.516831e-29", "%+e"}, + {__LINE__, 7.525789465978582e+00, "7.52579", "%G"}, + {__LINE__, 7.528655653725963e+06, "7.5287e+06", "%3.4e"}, + {__LINE__, 7.533217421035612e+28, "75332174210356122046050586504.861712", "%f"}, + {__LINE__, 7.534147071756384e+03, "7534.15", "%g"}, + {__LINE__, 7.542648637430919e-25, "+0.000000", "%+#f"}, + {__LINE__, 7.554380140947798e-15, " 8e-15", "%7.g"}, + {__LINE__, 7.557366996007743e+25, " 8E+25", "%7.G"}, + {__LINE__, 7.579228950138068e+08, "8e+08", "%3.e"}, + {__LINE__, 7.579773904052487e-13, "0.000000", "%3f"}, + {__LINE__, 7.580377544554059e+27, "7.58038e+27", "%#g"}, + {__LINE__, 7.589615147875915e-15, "7.59E-15", "%.3G"}, + {__LINE__, 7.593317194045158e-12, "+0.", "%+#1.f"}, + {__LINE__, 7.605535657484387e-07, "7.60554E-07", "%G"}, + {__LINE__, 7.609171121278006e-08, " 0", "%5.f"}, + {__LINE__, 7.612033900317304e-14, "7.61203e-14", "%g"}, + {__LINE__, 7.628040858080326e+23, "+7.62804e+23", "%+g"}, + {__LINE__, 7.632109382948695e-01, "0.763211", "%#f"}, + {__LINE__, 7.633415922627254e+28, "76334159226272539910951309138.908982", "%#f"}, + {__LINE__, 7.636190400774419e+22, "+7.636190E+22", "%+E"}, + {__LINE__, 7.636228368661314e-23, "0.000000", "%f"}, + {__LINE__, 7.653292362739654e-17, "7.65329E-17", "%#G"}, + {__LINE__, 7.664044705231460e+03, "7.664045E+03", "%5E"}, + {__LINE__, 7.664257283149626e-05, "7.66426E-05", "%0G"}, + {__LINE__, 7.668996632821614e-19, " 0.0", "%4.1f"}, + {__LINE__, 7.674502669497263e-24, "7.6745e-24", "%g"}, + {__LINE__, 7.681870119755193e+12, "+7.68187e+12", "%+#g"}, + {__LINE__, 7.693453198401315e+02, "769.3453198", "%5.7f"}, + {__LINE__, 7.705080073293603e-29, "7.705080E-29", "%1E"}, + {__LINE__, 7.707244083934683e-07, "7.71E-07", "%2.3G"}, + {__LINE__, 7.749445584970652e-17, "7.75E-17", "%6.3G"}, + {__LINE__, 7.755369447889403e+09, "7.8e+09", "%#4.2g"}, + {__LINE__, 7.760378169707072e-06, "7.76038e-06", "%g"}, + {__LINE__, 7.763518882114968e-10, "0.000000", "%f"}, + {__LINE__, 7.764720069569677e-18, "0.000000", "%0f"}, + {__LINE__, 7.768821339438552e-03, "0.00776882", "%g"}, + {__LINE__, 7.774767835990679e-29, "7.774768E-29", "%7E"}, + {__LINE__, 7.805567188246987e-04, "0.000780557", "%2G"}, + {__LINE__, 7.825157442935941e-26, "0.000000", "%f"}, + {__LINE__, 7.833373563161910e+29, "+783337356316190991378789476584.643126", "%+f"}, + {__LINE__, 7.875872661746674e-16, "7.875873E-16", "%E"}, + {__LINE__, 7.880664458920439e-28, "7.881E-28", "%#0.3E"}, + {__LINE__, 7.893084198630288e+18, "7893084198630288206", "%1.f"}, + {__LINE__, 7.912222737877417e+04, "79122.2", "%G"}, + {__LINE__, 7.913004582748724e-26, "0.000000", "%f"}, + {__LINE__, 7.913749944463836e+17, "791374994446383617.230367", "%f"}, + {__LINE__, 7.923881665760883e-24, "0.000000", "%f"}, + {__LINE__, 7.926699779993694e-03, "0.007927", "%5.6f"}, + {__LINE__, 7.941991860623354e-20, "7.941992e-20", "%e"}, + {__LINE__, 7.942700358097138e+17, "794270035809713803.587329", "%2f"}, + {__LINE__, 7.945451569935757e-16, "7.94545E-16", "%#4.6G"}, + {__LINE__, 7.948277588625241e-04, "0", "%.0f"}, + {__LINE__, 7.952265062569124e+21, "+7.95227e+21", "%+#g"}, + {__LINE__, 7.959953534668040e+11, "8e+11", "%2.e"}, + {__LINE__, 7.962059154424500e-22, "7.96206E-22", "%G"}, + {__LINE__, 7.962856142535673e-26, "7.9629E-26", "%#.5G"}, + {__LINE__, 7.966528574505771e+12, "7.96653e+12", "%g"}, + {__LINE__, 8.006496880305429e-21, "8.0065E-21", "%G"}, + {__LINE__, 8.023374861440542e+06, "+8023375", "%+6.f"}, + {__LINE__, 8.041139717082990e-20, "8.04114e-20", "%g"}, + {__LINE__, 8.044201752824126e+15, "8.04420E+15", "%.5E"}, + {__LINE__, 8.044262927409321e-10, "+8E-10", "%+2.2G"}, + {__LINE__, 8.058285708061202e+02, "+8.058286e+02", "%+e"}, + {__LINE__, 8.104422320765144e+10, "8.10442e+10", "%#4g"}, + {__LINE__, 8.104572628022330e-11, "0.000000", "%f"}, + {__LINE__, 8.112156369917432e+15, "+8.11216E+15", "%+G"}, + {__LINE__, 8.114566569709531e-18, "8.11457E-18", "%G"}, + {__LINE__, 8.114795069552519e+13, "8.114795E+13", "%E"}, + {__LINE__, 8.121382719830660e+03, "8121.382720", "%f"}, + {__LINE__, 8.125179335533733e-12, "+8.125179e-12", "%+e"}, + {__LINE__, 8.126383949107055e+19, "81263839491070548604.056967", "%f"}, + {__LINE__, 8.129961701307842e-04, "+0.00081", "%+5.2g"}, + {__LINE__, 8.143780077390936e+15, "8.14378E+15", "%G"}, + {__LINE__, 8.149891507777399e+16, "+8E+16", "%+1.G"}, + {__LINE__, 8.167395708830107e+03, "8167", "%3.f"}, + {__LINE__, 8.167703619221975e+01, "81.677", "%G"}, + {__LINE__, 8.178463030771759e+06, "+8.17846E+06", "%+G"}, + {__LINE__, 8.188188531273697e+12, "8.18819E+12", "%G"}, + {__LINE__, 8.189094866416537e+11, "+8E+11", "%+.1G"}, + {__LINE__, 8.205086844365809e-18, "+8.205087E-18", "%+#E"}, + {__LINE__, 8.205762333408320e-26, "0.000000", "%f"}, + {__LINE__, 8.212370598174696e-10, "8.2124E-10", "%.5G"}, + {__LINE__, 8.228054316085489e-14, "0.000000", "%3f"}, + {__LINE__, 8.244313484402404e-16, "8.24431e-16", "%g"}, + {__LINE__, 8.244472235472472e+19, "8.24447E+19", "%G"}, + {__LINE__, 8.245421473302411e-09, "+8.2454e-09", "%+1.5g"}, + {__LINE__, 8.252286626634840e-22, "8E-22", "%3.G"}, + {__LINE__, 8.259969177912707e-19, "0.000000", "%f"}, + {__LINE__, 8.265769991725211e+18, "8.26577e+18", "%.5e"}, + {__LINE__, 8.293986939496488e+25, "8.3E+25", "%.1E"}, + {__LINE__, 8.310348813512608e-23, "+8.31035e-23", "%+g"}, + {__LINE__, 8.316951996533247e-20, "0.000000", "%f"}, + {__LINE__, 8.318818016883803e+05, " 8e+05", "%6.1g"}, + {__LINE__, 8.324896920131877e-13, "+8.324897e-13", "%+e"}, + {__LINE__, 8.325228630004624e-03, "0.0083252", "%5.7f"}, + {__LINE__, 8.332538660129034e+14, "8.3e+14", "%5.2g"}, + {__LINE__, 8.343325212751775e+07, "8.34333e+07", "%#4g"}, + {__LINE__, 8.363117398136236e+20, "+8.36e+20", "%+.3g"}, + {__LINE__, 8.364181324448165e+27, "8.36418e+27", "%#g"}, + {__LINE__, 8.372159259848738e+10, "8.37216e+10", "%4g"}, + {__LINE__, 8.379252006152759e-26, "8.37925e-26", "%g"}, + {__LINE__, 8.392670395720252e+09, "8392670395.720252", "%f"}, + {__LINE__, 8.423360059147756e+05, "+842336.", "%+#G"}, + {__LINE__, 8.425921213167943e+09, "8.425921e+09", "%1.7g"}, + {__LINE__, 8.431664412515776e-16, "8.43166E-16", "%#G"}, + {__LINE__, 8.448608859842500e+02, "844.861", "%1.3f"}, + {__LINE__, 8.456292247478771e-14, "8.45629E-14", "%G"}, + {__LINE__, 8.460077225296853e-04, "0.000846008", "%G"}, + {__LINE__, 8.478635925746218e-10, "0.000000", "%f"}, + {__LINE__, 8.492455774427448e+06, "8.49246e+06", "%g"}, + {__LINE__, 8.494450528380746e-07, "8.494451e-07", "%e"}, + {__LINE__, 8.516435842947605e-23, "+0.000000", "%+1f"}, + {__LINE__, 8.519057789029134e-18, "0.000000", "%0f"}, + {__LINE__, 8.522602111109066e+18, " 9.e+18", "%#7.g"}, + {__LINE__, 8.529176788022152e-24, "8.529E-24", "%0.4G"}, + {__LINE__, 8.534979605642793e-07, "0.000001", "%f"}, + {__LINE__, 8.546859563634342e-07, "8.546860E-07", "%E"}, + {__LINE__, 8.552370027054106e+12, "9e+12", "%.0g"}, + {__LINE__, 8.561781328234041e+10, "8.56178e+10", "%4g"}, + {__LINE__, 8.562688793145107e-20, "8.562689e-20", "%1.7g"}, + {__LINE__, 8.573130147270046e-07, " 9E-07", "%7.G"}, + {__LINE__, 8.584571984387802e-18, "8.5846e-18", "%.5g"}, + {__LINE__, 8.596407996491291e+11, "8.596408E+11", "%#E"}, + {__LINE__, 8.604843726850381e+10, " 9E+10", "%6.G"}, + {__LINE__, 8.626884271938994e-23, "8.62688e-23", "%g"}, + {__LINE__, 8.631140282429168e+24, "8631140282429168308908629.", "%#.0f"}, + {__LINE__, 8.635194331917948e-16, "8.63519E-16", "%G"}, + {__LINE__, 8.641417311588688e-24, "+0.000000", "%+f"}, + {__LINE__, 8.649120264278466e+26, "8.65E+26", "%1.3G"}, + {__LINE__, 8.649745523383894e+06, "8.649746e+06", "%e"}, + {__LINE__, 8.654720740091021e+16, "8.65472e+16", "%g"}, + {__LINE__, 8.655445556834509e-08, "9e-08", "%4.g"}, + {__LINE__, 8.658954696751902e+06, "8.65895E+06", "%G"}, + {__LINE__, 8.665712368800818e-10, "+8.66571E-10", "%+G"}, + {__LINE__, 8.666937057116442e-10, "8.666937e-10", "%e"}, + {__LINE__, 8.670981239765155e+05, "867098", "%G"}, + {__LINE__, 8.679631934294932e-25, "+9.E-25", "%+#0.G"}, + {__LINE__, 8.683595173050962e-21, "8.68360E-21", "%#G"}, + {__LINE__, 8.684938704958039e+25, "8.684939E+25", "%E"}, + {__LINE__, 8.686745463281227e-12, "8.686745E-12", "%5.7G"}, + {__LINE__, 8.700227628706534e-12, "8.700228e-12", "%#e"}, + {__LINE__, 8.705101179577200e+04, "87051.011796", "%#2f"}, + {__LINE__, 8.707824829984700e-24, "9e-24", "%.1g"}, + {__LINE__, 8.717654041009233e+21, "+9E+21", "%+6.0E"}, + {__LINE__, 8.741736299906572e-11, "8.74174E-11", "%#0G"}, + {__LINE__, 8.742228350419966e+18, "+8742228350419965821.060941", "%+f"}, + {__LINE__, 8.743045146087558e-17, "+8.74305E-17", "%+2G"}, + {__LINE__, 8.747820269457588e+18, "8.74782E+18", "%G"}, + {__LINE__, 8.756274717008537e-28, "8.756275E-28", "%E"}, + {__LINE__, 8.769782309254687e-24, "+8.769782E-24", "%+2.6E"}, + {__LINE__, 8.788151659193398e-16, " 9E-16", "%6.E"}, + {__LINE__, 8.789514812202340e-07, "9E-07", "%0.G"}, + {__LINE__, 8.792657843164822e-07, "8.79266e-07", "%0g"}, + {__LINE__, 8.810976223440985e+05, "881097.622344", "%f"}, + {__LINE__, 8.822553973113614e+10, "8.82255E+10", "%G"}, + {__LINE__, 8.839440421530611e-04, "0.000883944", "%G"}, + {__LINE__, 8.842539073558434e-12, "0", "%1.f"}, + {__LINE__, 8.882818021261782e-13, "+8.9e-13", "%+4.2g"}, + {__LINE__, 8.899833909201039e+05, "8.8998E+05", "%.5G"}, + {__LINE__, 8.903167498000181e-13, "9e-13", "%0.g"}, + {__LINE__, 8.933727737932164e+09, "+8933727738", "%+4.f"}, + {__LINE__, 8.946226267100711e+06, "8.946E+06", "%1.4G"}, + {__LINE__, 8.953203780849794e-21, "8.9532E-21", "%G"}, + {__LINE__, 8.959934262635649e+28, "+8.959934e+28", "%+e"}, + {__LINE__, 8.969485341781558e-25, "0.000000", "%#f"}, + {__LINE__, 8.970058187654221e+02, "897.005819", "%f"}, + {__LINE__, 8.979846508565979e+02, "8.979847E+02", "%E"}, + {__LINE__, 8.984561117901212e+01, "8.984561E+01", "%E"}, + {__LINE__, 8.992157765875611e-12, "8.99216e-12", "%g"}, + {__LINE__, 9.004487016708012e+01, "9.004487e+01", "%#e"}, + {__LINE__, 9.007306978283218e-24, "9.007307e-24", "%e"}, + {__LINE__, 9.014546375043562e+17, "901454637504356171.007203", "%#3f"}, + {__LINE__, 9.028177786002161e-12, "9.02818E-12", "%G"}, + {__LINE__, 9.029635986381795e-02, "0.09", "%.1g"}, + {__LINE__, 9.032389962233431e-07, "+0.000001", "%+f"}, + {__LINE__, 9.038466945070887e+10, "+9.038467E+10", "%+E"}, + {__LINE__, 9.040166964322772e-27, "9.04017e-27", "%g"}, + {__LINE__, 9.044627673694681e-11, "9.04463E-11", "%G"}, + {__LINE__, 9.045906217628948e+17, "904590621762894819.823941", "%1f"}, + {__LINE__, 9.051764975661710e+11, "9.05176e+11", "%#g"}, + {__LINE__, 9.053142913711405e+29, "9.053143e+29", "%#e"}, + {__LINE__, 9.073185209160657e-19, "+9.07319e-19", "%+g"}, + {__LINE__, 9.074372574441451e+02, "907.437", "%g"}, + {__LINE__, 9.084624602048136e+08, "9.084625E+08", "%E"}, + {__LINE__, 9.088839346363631e+18, "9.09e+18", "%.2e"}, + {__LINE__, 9.091945159170871e-15, "9.09195e-15", "%5g"}, + {__LINE__, 9.115304318083716e+10, "9.115304e+10", "%e"}, + {__LINE__, 9.115978352902710e+28, "9.11598E+28", "%G"}, + {__LINE__, 9.121415008221017e-17, "9.E-17", "%#6.G"}, + {__LINE__, 9.127270978060388e+18, "9.E+18", "%#5.G"}, + {__LINE__, 9.143941729069086e+08, "9.143942E+08", "%#E"}, + {__LINE__, 9.162740105978580e-19, " 9.E-19", "%#7.G"}, + {__LINE__, 9.221509957559626e-18, "9.22151e-18", "%g"}, + {__LINE__, 9.227419774250317e-28, " 0.", "%#3.f"}, + {__LINE__, 9.230846660807540e-21, "0.000000", "%f"}, + {__LINE__, 9.238091727277130e-06, "+0.00001", "%+.5f"}, + {__LINE__, 9.244337357684406e+10, "9E+10", "%5.G"}, + {__LINE__, 9.259179793993285e-19, " 9E-19", "%7.0G"}, + {__LINE__, 9.274068541525759e-17, "9.274069e-17", "%e"}, + {__LINE__, 9.283833535882367e+16, "9.28383e+16", "%#g"}, + {__LINE__, 9.295693096364605e+11, "+9.295693E+11", "%+E"}, + {__LINE__, 9.301820438602407e+05, "930182", "%G"}, + {__LINE__, 9.322805251555376e-29, "9e-29", "%1.e"}, + {__LINE__, 9.358443042421307e-27, "9.35844e-27", "%g"}, + {__LINE__, 9.372658444745124e-23, "9.372658e-23", "%e"}, + {__LINE__, 9.380302971355292e+05, "938030.3", "%.7g"}, + {__LINE__, 9.381976354968076e-26, "+9.382E-26", "%+5.4G"}, + {__LINE__, 9.391072061980585e-15, "9.391072E-15", "%E"}, + {__LINE__, 9.403554117166546e-06, "0.000009", "%f"}, + {__LINE__, 9.403712900426614e-28, "9.40371e-28", "%g"}, + {__LINE__, 9.419046629820578e+03, "9419.046630", "%f"}, + {__LINE__, 9.422344695378412e+22, "94223446953784115720509.362291", "%f"}, + {__LINE__, 9.433327680467576e+01, "9.433328e+01", "%e"}, + {__LINE__, 9.443975205260596e+20, "9.44398e+20", "%g"}, + {__LINE__, 9.445134851965593e-04, "+9.445135E-04", "%+E"}, + {__LINE__, 9.448403585149890e+24, "9e+24", "%0.g"}, + {__LINE__, 9.463149430113036e+18, "9.46315e+18", "%g"}, + {__LINE__, 9.465735160722534e+27, "+9465735160722534402566452627.070248", "%+f"}, + {__LINE__, 9.495210794344892e-04, "0.000950", "%f"}, + {__LINE__, 9.501916506373814e-25, "9.50192E-25", "%#G"}, + {__LINE__, 9.514641335897739e+29, "9.51464E+29", "%G"}, + {__LINE__, 9.524633436992819e-04, " 0.001", "%6.G"}, + {__LINE__, 9.530925101873022e-18, "9.530925e-18", "%2e"}, + {__LINE__, 9.557903833216979e-22, "9.557904E-22", "%E"}, + {__LINE__, 9.587709102390903e-01, " 1", "%2.f"}, + {__LINE__, 9.592723000828453e-02, "9.592723E-02", "%5E"}, + {__LINE__, 9.603430008794172e+27, "9.60343e+27", "%g"}, + {__LINE__, 9.622648414989433e-18, "9.622648e-18", "%1e"}, + {__LINE__, 9.633326171289319e-23, "9.633326e-23", "%e"}, + {__LINE__, 9.662064616152408e+19, "96620646161524081918.5146393", "%1.7f"}, + {__LINE__, 9.668544846563163e-23, "9.668545e-23", "%e"}, + {__LINE__, 9.691014191346685e+28, "9.69101E+28", "%G"}, + {__LINE__, 9.693075414840598e+02, "+969.308", "%+G"}, + {__LINE__, 9.695013453291907e-30, "9.695013e-30", "%5e"}, + {__LINE__, 9.705983477801926e-24, "9.70598e-24", "%g"}, + {__LINE__, 9.709022568030226e-17, "0.000000", "%f"}, + {__LINE__, 9.715194901854826e+20, "9.715195e+20", "%e"}, + {__LINE__, 9.721011821337717e+16, "1E+17", "%0.G"}, + {__LINE__, 9.734895542899672e+14, "9.734896e+14", "%e"}, + {__LINE__, 9.741486438769710e-29, " 1e-28", "%6.g"}, + {__LINE__, 9.766657482315475e-24, "9.76666E-24", "%7G"}, + {__LINE__, 9.767283293054552e-02, "0.09767", "%5.5f"}, + {__LINE__, 9.769553159146005e-20, "9.76955E-20", "%G"}, + {__LINE__, 9.777220880605434e-10, "0.00", "%4.2f"}, + {__LINE__, 9.790290569034575e+20, "979029056903457473027.", "%#6.f"}, + {__LINE__, 9.793128245822718e-01, "0.979313", "%G"}, + {__LINE__, 9.804758536498200e+08, "980475853.650", "%.3f"}, + {__LINE__, 9.816883664191066e-02, "0.1", "%.0g"}, + {__LINE__, 9.836109821010300e+25, "9.83611e+25", "%g"}, + {__LINE__, 9.846197559631225e+03, "1E+04", "%2.G"}, + {__LINE__, 9.863289416819924e+12, "9.86329e+12", "%g"}, + {__LINE__, 9.865893798657353e+12, "9.866e+12", "%0.4g"}, + {__LINE__, 9.868248446640862e-15, "0.000000", "%#f"}, + {__LINE__, 9.869973080775134e+04, "98699.7", "%G"}, + {__LINE__, 9.899444006312953e+20, "1E+21", "%.1G"}, + {__LINE__, 9.904083146074285e-08, "9.904083e-08", "%6e"}, + {__LINE__, 9.909790408255471e+15, "9909790408255471.305665", "%f"}, + {__LINE__, 9.930297455798394e-13, "+9.930297E-13", "%+5.6E"}, + {__LINE__, 9.933314448709083e-15, "9.93331E-15", "%#.6G"}, + {__LINE__, 9.938714530509870e+29, "9.938715e+29", "%e"}, + {__LINE__, 9.953892603540162e+07, "9.95389E+07", "%G"}, + {__LINE__, 9.962084643867200e+14, "+996208464386720.038419", "%+f"}, + {__LINE__, 9.977706708809947e-09, "9.9777E-09", "%#.4E"}, +#endif + {__LINE__, 9.978034352999867e+15, "9.978034e+15", "%2.6e"}, + {__LINE__, 9.998315286730175e-30, "9.998315e-30", "%6e"}, +// For ±(x + 0.25), we diverge from the glibc implementation in how the +// rounding happens. Glibc uses "round to nearest, ties to even", which +// rounds them to ±(x + 0.2), but we use double-conversion, which +// implements the ECMAScript spec, which instead uses "round to nearest, +// ties away from zero", which rounds them to ±(x + 0.3). +// Both behaviors are valid. + {__LINE__, 1.25, "1.3", "%.1f"}, + {__LINE__, 11.25, "11.3", "%.1f"}, + {__LINE__, 1.75, "1.8", "%.1f"}, + {__LINE__, 11.75, "11.8", "%.1f"}, + {__LINE__, -1.25, "-1.3", "%.1f"}, + {__LINE__, -11.25, "-11.3", "%.1f"}, + {__LINE__, -1.75, "-1.8", "%.1f"}, + {__LINE__, -11.75, "-11.8", "%.1f"}, + {__LINE__, 16, "0x1.0p+4", "%.1a"}, + {__LINE__, 16, "0x1.00000000000000000000p+4", "%.20a"}, + {__LINE__, 4444.88888888, "4445", "%2.F"}, + {__LINE__, 0.956, "1", "%.0g"}, + {__LINE__, 1.0956, "1.", "%#.0g"}, + {__LINE__, 0.956, "1.", "%#.0g"}, + {__LINE__, 0.0956, "0.1", "%#.0g"}, + {__LINE__, 0.00956, "0.01", "%#.0g"}, + {__LINE__, 0.000956, "0.001", "%#.0g"}, + {__LINE__, 0.000098, "0.0001", "%#.0g"}, + {__LINE__, 0.0000996, "0.00010", "%#.2g"}, + {__LINE__, 9.999999999999999e-05, "0.0001", "%g"}, + {__LINE__, 1.0, "1.000000e+00", "%e"}, + {__LINE__, .9999999999999999, "1.000000e+00", "%e"}, + + {__LINE__, 912.98, "913.0", "%#.4g"}, + {__LINE__, 50.999999, "51.000", "%#.5g"}, + {__LINE__, 0.956, "1", "%.1g"}, + {__LINE__, 0.956, "1.", "%#.1g"}, + {__LINE__, 0.996, "1", "%.2g"}, + {__LINE__, 0.996, "1.0", "%#.2g"}, + {__LINE__, 999.98, "1000", "%.4g"}, + {__LINE__, 999.98, "1000.", "%#.4g"}, + {__LINE__, 999.998, "1000", "%.5g"}, + {__LINE__, 999.998, "1000.0", "%#.5g"}, + {__LINE__, 999.9998, "1000", "%g"}, + {__LINE__, 999.9998, "1000.00", "%#g"}, + {__LINE__, 912.98, "913", "%.4g"}, + {__LINE__, 50.999999, "51", "%.5g"}, + + {__LINE__, 0.0, "0000.00000", "%010.5f"}, + {__LINE__, 0.0, " 000.00000", "% 010.5f"}, + {__LINE__, -0.0, "-000.00000", "% 010.5f"}, + + {__LINE__, NAN, "nan", "%f"}, + {__LINE__, NAN, "+nan", "%+f"}, + {__LINE__, NAN, " nan", "%010.2f"}, + {__LINE__, NAN, " +nan", "%+010.2f"}, + {__LINE__, -NAN, "-nan", "%f"}, + {__LINE__, -NAN, "-nan", "%+f"}, + {__LINE__, -NAN, " -nan", "%010.2f"}, + {__LINE__, -NAN, " -nan", "%+010.2f"}, + {__LINE__, NAN, "NAN", "%F"}, + {__LINE__, INFINITY, "inf", "%f"}, + {__LINE__, INFINITY, "+inf", "%+f"}, + {__LINE__, INFINITY, " inf", "%010.2f"}, + {__LINE__, INFINITY, " +inf", "%+010.2f"}, + {__LINE__, -INFINITY, "-inf", "%f"}, + {__LINE__, -INFINITY, "-inf", "%+f"}, + {__LINE__, -INFINITY, " -inf", "%010.2f"}, + {__LINE__, -INFINITY, " -inf", "%+010.2f"}, + {__LINE__, INFINITY, "INF", "%F"}, + + {0 } + +}; + +/* matches(s1, s2) is true if s1 is "approximately" equal to s2. + Any digits after the first required_precision digits do not have to match. + */ + +int required_precision = 13; + +static int +matches (char *result, const char *desired) +{ + int digits_seen = 0; + for (;; result++, desired++) { + if (*result == *desired) { + if (*result == 0) + return 1; + else if (*result >= '0' && *result <= '9') + digits_seen++; + } + else if (digits_seen >= required_precision + && *result >= '0' && *result <= '9' + && *desired >= '0' && *desired <= '9') + continue; + else + return 0; + } +} + +int main(int argc, char *argv[]) +{ + int errcount = 0; + int testcount = 0; + double d; +#define BSIZE 1024 + char buffer[BSIZE]; + sprint_double_type *dptr; + for (dptr = sprint_doubles; dptr->line; dptr++) + { + snprintf (buffer, sizeof(buffer), dptr->format_string, dptr->value); + if (!matches(buffer, dptr->result)) + { + errcount++; + printf( + "Error in line %d using \"%s\". Result is \"%s\"; should be: \"%s\".\n", + dptr->line, dptr->format_string, buffer, dptr->result); + } + + snprintf (buffer, sizeof(buffer), "%.999g", dptr->value); + sscanf (buffer, "%lg", &d); + if (dptr->value != d && !isnan(d)) + { + errcount++; + printf ("Error in line %d. String is \"%s\", value is %g.\n", + dptr->line, buffer, d); + } + testcount++; + } + + /* And one special test. */ + { + static const char ref[] = "1.7763568394002504646778106689453125e-15"; + int i; + d = 1.0; + for (i = 1; i < 50; ++i) + d /= 2; + snprintf (buffer, sizeof(buffer), "%.100g", d); + if (!matches (buffer, ref)) + { + ++errcount; + printf ( + "Error in line %d using \"%s\". Result is \"%s\"; should be: \"%s\".\n", + __LINE__, "%.100g", buffer, ref); + } + } + + if (errcount == 0) + { + printf("Encountered no errors in %d tests.\n", testcount); + return 0; + } + else + { + printf("Encountered %d errors in %d tests.\n", + errcount, testcount); + return 1; + } +} diff --git a/mozglue/tests/glibc_printf_tests/tiformat.c b/mozglue/tests/glibc_printf_tests/tiformat.c new file mode 100644 index 0000000000..82226be689 --- /dev/null +++ b/mozglue/tests/glibc_printf_tests/tiformat.c @@ -0,0 +1,5070 @@ +#include <stdio.h> +#include <string.h> + +/* Tests taken from Cygnus C library. */ +typedef struct { + int line; + long value; + const char *result; + const char *format_string; +} sprint_int_type; + +sprint_int_type sprint_ints[] = +{ + {__LINE__, 0x000838d2, "838d2", "%.4x"}, + {__LINE__, 0x0063be46, "63BE46", "%-6X"}, + {__LINE__, -0x1b236c0, "-28456640", "%#0.d"}, + {__LINE__, -0x0000003, "-3", "% 0d"}, + {__LINE__, 0x0000ed51, "ed51", "%2.x"}, + {__LINE__, -0x00001f2, "-498", "%1ld"}, + {__LINE__, 0x0ea3e927, "EA3E927", "%+X"}, + {__LINE__, 0xffbef8da, "FFBEF8DA", "%5X"}, + {__LINE__, 0x62ff9f56, "62ff9f56", "%0x"}, + {__LINE__, 0x00000ad2, "AD2", "%.0X"}, + {__LINE__, 0x00000000, " ", "% 6.ld"}, + {__LINE__, 0xffffffff, "FFFFFFFF", "%1.4X"}, + {__LINE__, 0x0000208c, " 208C", "%5X"}, + {__LINE__, 0x0000003c, "3c", "%x"}, + {__LINE__, 0xffff6177, "0xffff6177", "%+#x"}, + {__LINE__, 0xffffdc8d, "ffffdc8d", "%+x"}, + {__LINE__, 0x00000000, "0", "%X"}, + {__LINE__, 0x00002434, "9268", "%0d"}, + {__LINE__, 0xd2c72cdb, "d2c72cdb", "%-x"}, + {__LINE__, 0xfe38012b, "0xfe38012b", "%+#7x"}, + {__LINE__, 0x00000001, "0001", "%#.4d"}, + {__LINE__, -0x008525a, "-0545370", "%06.7ld"}, + {__LINE__, 0xffffffac, "ffffffac", "%3.7x"}, + {__LINE__, 0x007424d2, "+7611602", "%+ld"}, + {__LINE__, 0x00001a85, "1A85", "%.4X"}, + {__LINE__, -0x0000019, "-25", "%3.d"}, + {__LINE__, 0xfffffffe, "fffffffe", "%-x"}, + {__LINE__, -0x34473b2, "-54817714", "% d"}, + {__LINE__, 0x000000ea, "234", "%1.ld"}, + {__LINE__, -0x0000004, "-4 ", "%-7.ld"}, + {__LINE__, 0x00006c94, "27796", "%0d"}, + {__LINE__, 0x00000001, "1", "%ld"}, + {__LINE__, 0x00000619, "619", "%1x"}, + {__LINE__, 0x0000209c, "8348", "%ld"}, + {__LINE__, -0x327f8ff, "-52951295", "%-0ld"}, + {__LINE__, 0xffffff0b, "FFFFFF0B", "%2.X"}, + {__LINE__, 0xf199d6ae, "F199D6AE", "%X"}, + {__LINE__, 0x3ca5602e, "1017471022", "%-1.ld"}, + {__LINE__, -0xfb2080b, "-263325707", "%3.5d"}, + {__LINE__, 0x00001cb8, "7352", "%d"}, + {__LINE__, 0x00000000, "0000000", "%3.7d"}, + {__LINE__, 0xffffff40, "FFFFFF40", "%+X"}, + {__LINE__, 0x14664450, "0x14664450", "%#x"}, + {__LINE__, 0x0000002e, "2e", "%1x"}, + {__LINE__, 0xffffffff, "ffffffff", "%x"}, + {__LINE__, 0xffff606a, "0XFFFF606A", "%#X"}, + {__LINE__, 0xffffff33, "0XFFFFFF33", "%#X"}, + {__LINE__, 0x00000000, "0", "%d"}, + {__LINE__, -0x0000012, "-00018", "%-2.5d"}, + {__LINE__, 0x0001fbe6, "130022", "%1.d"}, + {__LINE__, 0xfff59dd9, "fff59dd9", "%+x"}, + {__LINE__, 0x00000002, "2", "%0.0d"}, + {__LINE__, 0x00000fe1, "fe1 ", "%-7.x"}, + {__LINE__, 0x001f8f6a, "1F8F6A", "%1.X"}, + {__LINE__, 0x0000001b, "000001B", "%04.7X"}, + {__LINE__, 0x126a2609, " 308946441", "% 4d"}, + {__LINE__, 0xffffffe3, "0XFFFFFFE3", "%+#.1X"}, + {__LINE__, 0x1858f1c9, "1858f1c9", "%.3x"}, + {__LINE__, 0x003fc672, "3fc672", "%1x"}, + {__LINE__, 0x00c0bddc, "12631516", "%#d"}, + {__LINE__, 0x000006d6, "1750", "%#d"}, + {__LINE__, 0x000006a0, "6A0", "%X"}, + {__LINE__, 0x000007be, "7BE", "%X"}, + {__LINE__, -0x1c7cd1a, "-29871386", "%ld"}, + {__LINE__, 0x000000cc, "204", "%ld"}, + {__LINE__, 0x000002db, "731 ", "%-#5d"}, + {__LINE__, 0xffff67ad, "FFFF67AD", "% X"}, + {__LINE__, 0x00000008, "8", "%d"}, + {__LINE__, 0xffe07007, "FFE07007", "%5X"}, + {__LINE__, -0x0000001, " -1", "% 7d"}, + {__LINE__, 0xfffffffe, "fffffffe", "%x"}, + {__LINE__, 0x00027b68, "+162664", "%+ld"}, + {__LINE__, 0x0a7c1997, "+175905175", "%+2ld"}, + {__LINE__, 0xfe300896, "fe300896", "%.4x"}, + {__LINE__, 0x00000537, "537", "%-0.x"}, + {__LINE__, 0x3e981779, "3e981779", "%.6x"}, + {__LINE__, 0xfffff05e, "FFFFF05E", "%1.0X"}, + {__LINE__, -0x07bc0cf, "-8110287", "%-7d"}, + {__LINE__, -0x01371bc, "-1274300", "%.7ld"}, + {__LINE__, -0x0000013, "-19", "%.2ld"}, + {__LINE__, 0x000000d1, "0xd1", "%#x"}, + {__LINE__, -0x0000003, "-3", "%-ld"}, + {__LINE__, 0xfffffffc, "FFFFFFFC", "%4.1X"}, + {__LINE__, 0x00000000, "0", "%ld"}, + {__LINE__, 0x0001c8af, "116911", "%d"}, + {__LINE__, 0x000048c3, "48C3", "%X"}, + {__LINE__, 0x00000079, "0x0000079", "%-#0.7x"}, + {__LINE__, 0x0000615a, " 24922", "% d"}, + {__LINE__, 0xfffffff5, "fffffff5", "%x"}, + {__LINE__, 0x00000308, "0000308", "%+07.7x"}, + {__LINE__, 0xfcadc983, "fcadc983", "%x"}, + {__LINE__, 0x00000097, "151", "%#d"}, + {__LINE__, 0x000001c5, "453", "%0.2ld"}, + {__LINE__, 0x00000000, "00", "%-1.2x"}, + {__LINE__, 0x00000001, "+1", "%+02.d"}, + {__LINE__, 0x01eb4354, "1EB4354", "% X"}, + {__LINE__, 0xffffa7d1, "ffffa7d1", "%5.x"}, + {__LINE__, 0x0003170e, "0x3170e", "%#x"}, + {__LINE__, 0x000001ce, "1CE", "% .2X"}, + {__LINE__, 0x3a2991fb, "975802875", "%d"}, + {__LINE__, -0xcdad8e2, "-215668962", "%-d"}, + {__LINE__, 0xfe0261c3, "fe0261c3", "%x"}, + {__LINE__, -0x0006ea7, "-28327", "% ld"}, + {__LINE__, 0x032854a3, "0X32854A3", "% #6X"}, + {__LINE__, 0x0000004b, "4b", "%x"}, + {__LINE__, 0xffff6ca3, "FFFF6CA3", "%4.X"}, + {__LINE__, 0x00000000, "0", "%X"}, + {__LINE__, 0x00611f04, "611F04", "%.1X"}, + {__LINE__, 0x00000000, " 0", "%#6d"}, + {__LINE__, 0x0002c711, "2c711", "%-02.x"}, + {__LINE__, -0x07a2fe4, "-8007652", "%d"}, + {__LINE__, -0x0000547, "-1351", "%ld"}, + {__LINE__, 0x00013954, "0x13954", "%#x"}, + {__LINE__, -0x00523b7, "-336823", "%ld"}, + {__LINE__, -0x00f6c6e, "-1010798", "%6.ld"}, + {__LINE__, 0xffea6427, "FFEA6427", "%3X"}, + {__LINE__, -0x0000005, "-5", "%d"}, + {__LINE__, -0x04fe618, "-5236248", "% .7d"}, + {__LINE__, 0xffa80a28, "ffa80a28", "%-0x"}, + {__LINE__, 0x00000047, "71", "%ld"}, + {__LINE__, 0x001dc6bb, "1951419", "%#4ld"}, + {__LINE__, 0x00000868, " 2152", "%#6d"}, + {__LINE__, 0x000002fb, "2fb", "%x"}, + {__LINE__, 0x00607fa0, "6324128", "%4d"}, + {__LINE__, 0xfffed161, "fffed161", "%x"}, + {__LINE__, 0x0001c6e4, " 116452", "% 2.5d"}, + {__LINE__, 0x0003dda8, "3DDA8", "%4X"}, + {__LINE__, 0xfffffff8, "fffffff8", "%x"}, + {__LINE__, 0xfacf664a, "facf664a", "%x"}, + {__LINE__, 0x000000c1, "C1", "% 1X"}, + {__LINE__, 0x0000839c, " 839C", "%6X"}, + {__LINE__, 0xfffff69a, "FFFFF69A", "%X"}, + {__LINE__, 0x000e5c2e, "+941102", "%+d"}, + {__LINE__, -0x0000004, "-4", "%#d"}, + {__LINE__, 0x00000000, "00000", "%#.5ld"}, + {__LINE__, 0xfffffffe, "fffffffe", "%3.1x"}, + {__LINE__, 0x0010ed68, "1109352", "%-ld"}, + {__LINE__, 0xffffffe9, "ffffffe9", "%.6x"}, + {__LINE__, 0x00000007, " 7", "% 4.ld"}, + {__LINE__, 0x0000caba, "51898", "%ld"}, + {__LINE__, -0x0000119, "-281", "% d"}, + {__LINE__, 0x0c3012a9, "204477097", "%03.6ld"}, + {__LINE__, -0x001c98f, "-117135", "%ld"}, + {__LINE__, 0x000017b1, "+6065", "%+d"}, + {__LINE__, 0x00000001, "1", "%ld"}, + {__LINE__, -0x0000019, "-25", "% .2d"}, + {__LINE__, 0x0009ae28, "9AE28", "%X"}, + {__LINE__, 0xe861c4fe, "e861c4fe", "%7.x"}, + {__LINE__, 0xffac88d3, "FFAC88D3", "%X"}, + {__LINE__, -0x00005df, "-01503", "%-5.5ld"}, + {__LINE__, 0x0000013e, "318", "%-0d"}, + {__LINE__, 0x04b6f58f, "4b6f58f", "%x"}, + {__LINE__, 0xfffe3978, "fffe3978", "%+0.6x"}, + {__LINE__, 0x57ee5244, "57ee5244", "%+7.4x"}, + {__LINE__, 0xfffb5610, "fffb5610", "% 6x"}, + {__LINE__, 0x00000006, " 6", "%#7d"}, + {__LINE__, 0x0000000c, "0x0000c", "%#.5x"}, + {__LINE__, -0xec59362, "-247829346", "%1.7d"}, + {__LINE__, -0x0000001, "-1", "%ld"}, + {__LINE__, -0x0007e4b, "-32331", "% d"}, + {__LINE__, 0x0ececa6f, "248433263", "%0d"}, + {__LINE__, 0x00000578, " 578", "%+6.x"}, + {__LINE__, -0x0000003, "-3", "%ld"}, + {__LINE__, 0x000002bb, "2bb", "% 0x"}, + {__LINE__, -0x0000006, "-6", "%-ld"}, + {__LINE__, 0x000002f9, "761", "%d"}, + {__LINE__, 0xffffd736, "ffffd736", "%3.4x"}, + {__LINE__, 0x0010ba79, "1096313", "%7.4d"}, + {__LINE__, -0x0001244, "-4676", "%#.1d"}, + {__LINE__, 0x00000dbe, "DBE", "%1X"}, + {__LINE__, -0x0000015, "-21", "%-0.d"}, + {__LINE__, 0xffffffff, "0xffffffff", "%#x"}, + {__LINE__, -0x6cdf4a3, "-114160803", "%ld"}, + {__LINE__, 0x00008ac9, "8AC9", "%1X"}, + {__LINE__, 0x00000000, "00", "%.2X"}, + {__LINE__, -0x2263dba, "-36060602", "%5.5d"}, + {__LINE__, 0x00007da9, "32169", "%0d"}, + {__LINE__, 0xfffffff7, "FFFFFFF7", "% 4.X"}, + {__LINE__, 0xfbf36cca, "FBF36CCA", "%.1X"}, + {__LINE__, 0x00000040, "64", "%.0ld"}, + {__LINE__, 0x0000001c, " 28", "% d"}, + {__LINE__, 0xfffffadb, "0xfffffadb", "%#.5x"}, + {__LINE__, 0x0eb95847, "247027783", "%d"}, + {__LINE__, 0xfffd7030, "FFFD7030", "%+02X"}, + {__LINE__, 0x00000005, "00005", "%.5x"}, + {__LINE__, 0xfffffffd, "FFFFFFFD", "%X"}, + {__LINE__, 0x98975b15, "98975B15", "%02X"}, + {__LINE__, -0x0000034, " -52", "%6ld"}, + {__LINE__, 0xffd7d0cc, "ffd7d0cc", "% 5.6x"}, + {__LINE__, 0xffffffa6, "FFFFFFA6", "%X"}, + {__LINE__, 0xfff27132, "fff27132", "%x"}, + {__LINE__, -0x065b74d, "-6666061", "%+6ld"}, + {__LINE__, 0xf6ac99d6, "0XF6AC99D6", "%#X"}, + {__LINE__, 0xfffff4bd, "FFFFF4BD", "%X"}, + {__LINE__, 0xfffffb62, "FFFFFB62", "%6X"}, + {__LINE__, 0xf8434543, "f8434543", "%-0x"}, + {__LINE__, 0x0002b374, "0x2b374", "%-#6x"}, + {__LINE__, 0x00000000, "0", "%x"}, + {__LINE__, 0xffb5751d, "ffb5751d", "%-3.x"}, + {__LINE__, 0x34ea7347, "887780167", "%d"}, + {__LINE__, -0x008f457, "-586839", "%0ld"}, + {__LINE__, 0x00000001, " 1", "%#4d"}, + {__LINE__, 0xc32d7ad4, "c32d7ad4", "%0x"}, + {__LINE__, 0xfffffb80, "FFFFFB80", "%-.2X"}, + {__LINE__, 0x00000756, "756 ", "%-6x"}, + {__LINE__, 0xfffb020f, "FFFB020F", "%+.2X"}, + {__LINE__, -0xe68619f, "-241721759", "%#1.6ld"}, + {__LINE__, 0x180166cd, "402745037", "%3.7ld"}, + {__LINE__, -0x0000001, "-1", "%+00d"}, + {__LINE__, 0xfdbc3611, "fdbc3611", "%x"}, + {__LINE__, -0x000005b, "-91", "%d"}, + {__LINE__, 0xffffe77f, "ffffe77f", "%x"}, + {__LINE__, -0x0000019, "-25", "%ld"}, + {__LINE__, 0x00000016, "22", "%#d"}, + {__LINE__, -0x0000025, "-37", "%ld"}, + {__LINE__, 0xffffe43f, "ffffe43f", "%x"}, + {__LINE__, 0x000050a5, "00050a5", "%.7x"}, + {__LINE__, 0x0000000c, " 12", "% 5.ld"}, + {__LINE__, -0x001faff, "-129791", "%4d"}, + {__LINE__, 0x239d7cf2, "597523698", "%0.7ld"}, + {__LINE__, 0x04092183, "4092183", "% 7X"}, + {__LINE__, -0x0043498, "-275608", "%-ld"}, + {__LINE__, -0x0018dbe, "-101822", "%d"}, + {__LINE__, 0xff3e09c3, "FF3E09C3", "%X"}, + {__LINE__, 0x00000000, "0", "%d"}, + {__LINE__, -0x15d57558, "-366310744", "%+0.7ld"}, + {__LINE__, 0x0004fb06, "326406", "%0.0d"}, + {__LINE__, 0x00000119, "0X119", "%#X"}, + {__LINE__, -0x14c53e19, "-348470809", "%d"}, + {__LINE__, 0x00000019, "0000019", "% .7X"}, + {__LINE__, -0x0000654, "-1620", "%0.4ld"}, + {__LINE__, -0xde5899b, "-233146779", "%#.1d"}, + {__LINE__, 0x0013c1f0, "1294832", "%4.ld"}, + {__LINE__, 0x0a9fe761, "A9FE761", "%X"}, + {__LINE__, -0x00000e5, "-229", "%1ld"}, + {__LINE__, 0x00000161, "000353", "%.6ld"}, + {__LINE__, 0x6b04e4bd, "0x6b04e4bd", "%#7.0x"}, + {__LINE__, -0x0000ef0, "-3824 ", "%-6.ld"}, + {__LINE__, -0x026306e, "-2502766", "%ld"}, + {__LINE__, 0x00000000, "0", "%-X"}, + {__LINE__, 0xfffffe1f, "fffffe1f", "%-x"}, + {__LINE__, 0x0003aecf, " 241359", "% 2.1ld"}, + {__LINE__, -0x0000014, "-20", "%d"}, + {__LINE__, 0x00000002, "0002", "%-.4ld"}, + {__LINE__, 0xfffff662, "0XFFFFF662", "% #.6X"}, + {__LINE__, 0x00000006, "6", "%0x"}, + {__LINE__, 0x0001f4c4, " 128196", "% 0d"}, + {__LINE__, 0xffffffff, "FFFFFFFF", "%5X"}, + {__LINE__, 0x05605e38, "90201656", "%ld"}, + {__LINE__, 0xffffffde, "0XFFFFFFDE", "% #X"}, + {__LINE__, 0x000001e0, " 1e0", "%7.x"}, + {__LINE__, 0x00000007, "7", "%X"}, + {__LINE__, 0x00000001, "+1", "%+d"}, + {__LINE__, 0xffffffe0, "FFFFFFE0", "% X"}, + {__LINE__, 0xffd7f77a, "FFD7F77A", "%+00.1X"}, + {__LINE__, 0xfffffffe, "0xfffffffe", "%-#x"}, + {__LINE__, 0x01b0a63b, "1b0a63b", "%x"}, + {__LINE__, -0x06554b7, "-6640823", "%d"}, + {__LINE__, 0xfffffff8, "fffffff8", "%x"}, + {__LINE__, 0x00000b2d, "b2d", "%x"}, + {__LINE__, -0x2664ba2, "-40258466", "%-d"}, + {__LINE__, 0xfffffffe, "FFFFFFFE", "%X"}, + {__LINE__, -0x0000552, " -1362", "%6.2ld"}, + {__LINE__, 0x00000009, " 00009", "%6.5x"}, + {__LINE__, 0x00035c72, "220274", "%05.ld"}, + {__LINE__, -0x014d09d, "-1364125", "%d"}, + {__LINE__, -0x0000002, " -2", "% 4d"}, + {__LINE__, 0x00000001, "1", "%+X"}, + {__LINE__, 0x00000028, "28", "%X"}, + {__LINE__, 0xf25c9eb2, "F25C9EB2", "%7.X"}, + {__LINE__, -0x7ae4b62, "-128863074", "%d"}, + {__LINE__, 0x00630d87, "630D87", "%2X"}, + {__LINE__, 0x000000f8, "F8", "%-X"}, + {__LINE__, 0xfffffff9, "FFFFFFF9", "%X"}, + {__LINE__, -0x0000007, "-7", "%0d"}, + {__LINE__, -0x2518fcd, "-38899661", "% 3ld"}, + {__LINE__, 0xfffff4b4, "FFFFF4B4", "%-5X"}, + {__LINE__, 0x01cc36c8, "30160584", "%ld"}, + {__LINE__, 0xffffffff, "ffffffff", "%-x"}, + {__LINE__, 0x0000000b, " 11", "%6.ld"}, + {__LINE__, 0x00010d55, " 68949", "% d"}, + {__LINE__, -0x0c01306, "-12587782", "% 6.1ld"}, + {__LINE__, 0x001719d7, "1513943", "%.1ld"}, + {__LINE__, -0x0000002, " -2", "%+3.ld"}, + {__LINE__, 0x0000018e, "398", "%d"}, + {__LINE__, 0x000000a6, "0xa6", "%-#3x"}, + {__LINE__, 0x00006f66, " 28518", "% #d"}, + {__LINE__, 0x0000032a, "32A", "%X"}, + {__LINE__, -0x0000145, "-325", "%0d"}, + {__LINE__, 0xffffffff, "ffffffff", "%2.x"}, + {__LINE__, 0x00287171, "2650481", "%ld"}, + {__LINE__, 0x0000004b, "+75", "%+02.ld"}, + {__LINE__, 0xfffffff8, "fffffff8", "%-0x"}, + {__LINE__, 0x00000064, "64", "% X"}, + {__LINE__, 0xfffffca2, "fffffca2", "%0x"}, + {__LINE__, 0x000010b6, "10b6", "% .0x"}, + {__LINE__, -0x003806f, "-229487", "%7d"}, + {__LINE__, -0x00000b7, "-183", "%3ld"}, + {__LINE__, 0xffffe5ba, "FFFFE5BA", "%0X"}, + {__LINE__, 0x001f0da8, "1f0da8", "%+x"}, + {__LINE__, 0x000ce185, "844165", "%0ld"}, + {__LINE__, 0x00000c96, "3222", "%0d"}, + {__LINE__, -0x0013099, "-77977", "%1.ld"}, + {__LINE__, -0x5f3c47a, "-99861626", "%.6ld"}, + {__LINE__, 0x00000cbf, " CBF", "%+5.X"}, + {__LINE__, -0x0000001, "-1", "%+ld"}, + {__LINE__, 0x0d793bc3, "d793bc3", "%6.x"}, + {__LINE__, 0xfffffffd, "0xfffffffd", "%+#x"}, + {__LINE__, -0x0000070, " -112", "%6.d"}, + {__LINE__, 0xfffffb3f, "FFFFFB3F", "%.7X"}, + {__LINE__, 0x037dfc78, "0X37DFC78", "%#X"}, + {__LINE__, 0x00009ba5, "39845", "%3ld"}, + {__LINE__, 0x0000004a, "4a", "%0x"}, + {__LINE__, 0xffffff81, "ffffff81", "%5.5x"}, + {__LINE__, -0x86ef222, "-141488674", "%ld"}, + {__LINE__, -0xac5531e, "-180704030", "% 4ld"}, + {__LINE__, 0x00002493, "2493", "%+X"}, + {__LINE__, 0xffffffff, "FFFFFFFF", "%1.3X"}, + {__LINE__, 0x0005c875, "378997", "%.0ld"}, + {__LINE__, 0x0301d345, "50451269", "%d"}, + {__LINE__, 0xfff6b589, "fff6b589", "%2.4x"}, + {__LINE__, -0x00001db, "-475 ", "%-7.0d"}, + {__LINE__, 0x0000d1e7, "0x000d1e7", "%#2.7x"}, + {__LINE__, 0xf4c4d2bb, "f4c4d2bb", "%.6x"}, + {__LINE__, 0x00c89f54, "13147988", "%ld"}, + {__LINE__, -0x6599253c, "-1704535356", "%+ld"}, + {__LINE__, 0xab7d61ea, "ab7d61ea", "% x"}, + {__LINE__, 0x00000006, "6", "%1ld"}, + {__LINE__, 0xffff5ab7, "ffff5ab7", "%0x"}, + {__LINE__, -0x1cfeca5b, "-486459995", "%#ld"}, + {__LINE__, 0x000000f3, " f3", "% 6.x"}, + {__LINE__, -0x0000021, "-33", "%d"}, + {__LINE__, 0xfe7246ac, "fe7246ac", "%-5.x"}, + {__LINE__, 0x00000017, "00017", "%-.5X"}, + {__LINE__, -0x0cc250a, "-13378826", "%0.0d"}, + {__LINE__, -0x00010ae, "-04270", "%#.5ld"}, + {__LINE__, -0x7952c8b, "-127216779", "% ld"}, + {__LINE__, 0x0002a53e, "173374", "%4.0d"}, + {__LINE__, 0x03801bbf, "3801BBF", "%0X"}, + {__LINE__, 0x0368645f, "+57173087", "%+#0.6ld"}, + {__LINE__, 0x0000002f, "47", "%d"}, + {__LINE__, 0x00000003, "3", "%x"}, + {__LINE__, 0x000005f7, "5F7", "%0X"}, + {__LINE__, -0x06e5e76, "-7233142", "% 0.1ld"}, + {__LINE__, 0x053da936, "53da936", "%1.4x"}, + {__LINE__, -0x0df0c2f, "-14617647", "%.5ld"}, + {__LINE__, 0xfffffffd, "FFFFFFFD", "%-0X"}, + {__LINE__, 0x000104bf, "104BF", "%X"}, + {__LINE__, 0x000353fc, "353FC", "%X"}, + {__LINE__, 0x182a18dc, "+405412060", "%+ld"}, + {__LINE__, 0x0000000e, " 14", "% 2d"}, + {__LINE__, -0x0000091, "-145", "%+ld"}, + {__LINE__, 0x0029a9d6, "2730454", "%d"}, + {__LINE__, 0x00009d57, "009d57", "% .6x"}, + {__LINE__, 0x4c4d4f79, "1280135033", "%5.ld"}, + {__LINE__, -0x000075b, "-1883", "% 03d"}, + {__LINE__, -0x001b718, "-112408", "%+d"}, + {__LINE__, 0xfffaaf57, "FFFAAF57", "%+1X"}, + {__LINE__, 0x00f44e52, "F44E52", "% 4X"}, + {__LINE__, 0x00000002, "0X2", "%#X"}, + {__LINE__, 0x000a85e9, "A85E9", "%+0X"}, + {__LINE__, 0xffff70bb, "ffff70bb", "%6x"}, + {__LINE__, 0x00004e15, "4e15", "%x"}, + {__LINE__, 0x000eeb60, " 977760", "% ld"}, + {__LINE__, 0xfffffff1, "fffffff1", "%0x"}, + {__LINE__, -0x294d7e2, "-43309026", "% 02.d"}, + {__LINE__, 0x0001aaeb, "0109291", "%02.7ld"}, + {__LINE__, 0x000a01fc, "0xa01fc", "%-#.5x"}, + {__LINE__, 0x002c3c30, "+2898992", "%+#d"}, + {__LINE__, 0x015667ae, "+22439854", "%+5ld"}, + {__LINE__, -0x05af42b, "-5960747", "%+d"}, + {__LINE__, 0x0000be36, "be36", "%+2x"}, + {__LINE__, 0x005e3f67, "5e3f67", "% x"}, + {__LINE__, -0x0000020, "-32", "% ld"}, + {__LINE__, -0x0003617, "-13847", "% d"}, + {__LINE__, 0x00000014, "14 ", "%-4.X"}, + {__LINE__, 0x00000001, "001", "%0.3d"}, + {__LINE__, 0xfffffea9, "fffffea9", "%3.x"}, + {__LINE__, 0xe447099e, "E447099E", "%0X"}, + {__LINE__, 0x00049c71, "302193", "%d"}, + {__LINE__, 0x76356ed3, "0x76356ed3", "%#4.1x"}, + {__LINE__, 0x00000005, " 0X5", "%#5X"}, + {__LINE__, 0x00004da2, "0x004da2", "%#1.6x"}, + {__LINE__, 0xffdfb36c, "FFDFB36C", "%4X"}, + {__LINE__, 0xffffe3c5, "FFFFE3C5", "%+4.X"}, + {__LINE__, 0x00000006, "6", "%0x"}, + {__LINE__, -0x914ff17, "-152370967", "% .5ld"}, + {__LINE__, -0x0000002, "-2", "%0ld"}, + {__LINE__, 0xffffff36, "FFFFFF36", "% 2X"}, + {__LINE__, -0x0bb1598, "-12260760", "%-ld"}, + {__LINE__, 0x00006db5, "28085", "%0.5ld"}, + {__LINE__, 0xffffffff, "ffffffff", "%x"}, + {__LINE__, -0x0000001, "-1", "%0ld"}, + {__LINE__, 0x0fa37e79, "FA37E79", "%1.X"}, + {__LINE__, -0x00000ac, "-172", "%.2d"}, + {__LINE__, 0xffffffd8, "ffffffd8", "%.7x"}, + {__LINE__, -0x000a513, "-42259", "%ld"}, + {__LINE__, 0x00001294, "4756", "%2ld"}, + {__LINE__, -0x56bae0b4, "-1455087796", "%.1ld"}, + {__LINE__, 0x3b26d5fa, "3b26d5fa", "%x"}, + {__LINE__, 0xff657013, "ff657013", "%x"}, + {__LINE__, -0x00005f5, "-1525", "%0.0ld"}, + {__LINE__, -0x0000001, " -1", "% 7d"}, + {__LINE__, -0x02396b2, "-2332338", "%d"}, + {__LINE__, -0x0000003, "-000003", "%+7.6d"}, + {__LINE__, 0xfd3ec7c1, "fd3ec7c1", "% 3.4x"}, + {__LINE__, -0x076e338, "-7791416", "%0.d"}, + {__LINE__, -0x000015e, "-350", "%3ld"}, + {__LINE__, 0x0a0f0b71, "a0f0b71", "%x"}, + {__LINE__, 0xfffe00ba, "FFFE00BA", "%X"}, + {__LINE__, 0x00036bd8, " 36bd8", "% 06.x"}, + {__LINE__, 0xfffff2df, "0XFFFFF2DF", "%#2.6X"}, + {__LINE__, 0x004687fb, "4622331", "%ld"}, + {__LINE__, -0x0000331, " -817", "%+6.d"}, + {__LINE__, 0xfcd5b090, "fcd5b090", "%+0x"}, + {__LINE__, 0x0334f94c, "334F94C", "%.6X"}, + {__LINE__, 0x00000bce, "3022", "%.3ld"}, + {__LINE__, 0xf28d9ddd, "f28d9ddd", "%x"}, + {__LINE__, 0x00003a79, " 14969", "% ld"}, + {__LINE__, 0xfffffffe, "FFFFFFFE", "%.7X"}, + {__LINE__, 0x0043d7fe, "43D7FE", "%X"}, + {__LINE__, -0x0000b82, "-2946", "%-ld"}, + {__LINE__, -0x00e29d8, "-928216", "%d"}, + {__LINE__, 0xffffffec, "ffffffec", "%-x"}, + {__LINE__, 0x0aeaf557, "0xaeaf557", "%-#x"}, + {__LINE__, 0x00c07fe6, "C07FE6", "%X"}, + {__LINE__, 0x307414d8, "307414d8", "% 0x"}, + {__LINE__, -0x045ef67, "-4583271", "%d"}, + {__LINE__, 0x0000024a, " 0000586", "% .7d"}, + {__LINE__, 0xfffffc42, "fffffc42", "%x"}, + {__LINE__, 0xfc38c249, "fc38c249", "%5.x"}, + {__LINE__, -0x000d096, "-53398", "%-d"}, + {__LINE__, -0x213a345, "-34841413", "%ld"}, + {__LINE__, -0x0000130, "-304", "%ld"}, + {__LINE__, -0x000efd1, "-61393", "%ld"}, + {__LINE__, 0xfffff867, "FFFFF867", "%2X"}, + {__LINE__, 0x00113ed0, "0x113ed0", "%-#7x"}, + {__LINE__, 0x00000677, " 1655", "% 3.ld"}, + {__LINE__, 0x0000002f, "2f", "%x"}, + {__LINE__, 0xfffffd38, "fffffd38", "%-4x"}, + {__LINE__, -0x000b3cc, "-46028", "%1.2d"}, + {__LINE__, 0x45da719b, "45DA719B", "%+0X"}, + {__LINE__, -0x000a454, " -42068", "%7.3ld"}, + {__LINE__, 0x01cc7c30, " 30178352", "% d"}, + {__LINE__, -0x05ae80e, "-5957646", "%#.4ld"}, + {__LINE__, 0x000000b9, "185", "%d"}, + {__LINE__, 0x003fcaf4, "4180724", "%1.ld"}, + {__LINE__, 0x02da1b74, "2DA1B74", "%X"}, + {__LINE__, 0xffffffd9, "FFFFFFD9", "%X"}, + {__LINE__, 0x00064359, "64359 ", "%-6X"}, + {__LINE__, 0x000000f0, "240", "%ld"}, + {__LINE__, -0x01715e5, "-1512933", "%ld"}, + {__LINE__, 0xc8f6e118, "0XC8F6E118", "%#X"}, + {__LINE__, 0xfffffd8f, "FFFFFD8F", "%X"}, + {__LINE__, 0x0000176d, "5997 ", "%-6d"}, + {__LINE__, 0x00003cab, "0x3cab", "%#0.3x"}, + {__LINE__, 0x00000001, " 1", "% d"}, + {__LINE__, 0xfffff610, "0XFFFFF610", "%#X"}, + {__LINE__, 0xfffffffe, "FFFFFFFE", "%5.7X"}, + {__LINE__, 0x00000001, "1", "%0x"}, + {__LINE__, 0x00000001, "00001", "%.5ld"}, + {__LINE__, -0x0000d51, "-3409", "%d"}, + {__LINE__, -0x08639ee, "-8796654", "%ld"}, + {__LINE__, 0x00003121, "12577", "%#1.2ld"}, + {__LINE__, 0x0000004e, "78", "%d"}, + {__LINE__, 0xfffff64f, "fffff64f", "%-x"}, + {__LINE__, 0x0000002a, "2a", "%0x"}, + {__LINE__, 0x00000020, "20", "%x"}, + {__LINE__, -0x00000b8, "-184", "%#ld"}, + {__LINE__, 0x0000000a, "10", "%-#d"}, + {__LINE__, 0x00000364, "868", "%.1ld"}, + {__LINE__, 0x00d1f9b6, "D1F9B6", "%0.X"}, + {__LINE__, 0x008cfda1, "8CFDA1", "% 6.X"}, + {__LINE__, 0xffd58cce, "FFD58CCE", "%X"}, + {__LINE__, 0x00000008, "0000008", "%6.7ld"}, + {__LINE__, 0x00000318, " 318", "%4X"}, + {__LINE__, 0x000172d8, "94936", "%2ld"}, + {__LINE__, -0x056ccbf, "-5688511", "%ld"}, + {__LINE__, 0x000000c7, "+000199", "%+0.6d"}, + {__LINE__, 0x00000002, "0X2", "%-#X"}, + {__LINE__, 0xfffff634, "FFFFF634", "%.1X"}, + {__LINE__, -0x00001f3, "-499", "%-ld"}, + {__LINE__, -0x000081b, "-2075", "%-d"}, + {__LINE__, 0x000000b5, "181", "%0d"}, + {__LINE__, 0xfe961ee8, "fe961ee8", "%0x"}, + {__LINE__, 0x000028a2, "10402", "%d"}, + {__LINE__, 0xffffffff, "FFFFFFFF", "%.4X"}, + {__LINE__, -0x000058a, "-1418", "%.3ld"}, + {__LINE__, 0x00001ceb, "1CEB", "%X"}, + {__LINE__, 0x00000021, "21", "% X"}, + {__LINE__, 0x00000002, "2", "%0d"}, + {__LINE__, -0x2f9bbf5, "-49921013", "%.4ld"}, + {__LINE__, 0x00000e91, "03729", "%#.5ld"}, + {__LINE__, 0xf1116740, "f1116740", "%2x"}, + {__LINE__, 0x00014f72, "14F72", "%X"}, + {__LINE__, 0xfffffced, "FFFFFCED", "%X"}, + {__LINE__, 0xffffffff, "ffffffff", "%.5x"}, + {__LINE__, 0x0017cf0c, " 1560332", "% 0ld"}, + {__LINE__, 0x0000000a, "A", "% X"}, + {__LINE__, 0xff3a39d4, "ff3a39d4", "%x"}, + {__LINE__, 0xfffef05f, "FFFEF05F", "%X"}, + {__LINE__, 0xfedfe708, "fedfe708", "%03.0x"}, + {__LINE__, 0x000345a0, "345A0", "% X"}, + {__LINE__, -0x1ed5b6f7, "-517322487", "%d"}, + {__LINE__, 0xd2d56c22, "D2D56C22", "%0.X"}, + {__LINE__, 0x00002cbf, "2CBF", "%.2X"}, + {__LINE__, 0x00000db4, "db4", "%3x"}, + {__LINE__, -0x000b154, "-45396", "%d"}, + {__LINE__, 0x002bf4c7, "2BF4C7", "%X"}, + {__LINE__, 0x6954abf4, "+1767156724", "%+d"}, + {__LINE__, 0xfffffffb, "fffffffb", "%x"}, + {__LINE__, -0x29e0050e, "-702547214", "%-0ld"}, + {__LINE__, -0x0014989, "-84361", "%-#0.3ld"}, + {__LINE__, 0x001a557c, "+1725820", "%+6.5d"}, + {__LINE__, -0x56689a81, "-1449695873", "%.2d"}, + {__LINE__, 0x00000016, " 16", "%05.X"}, + {__LINE__, 0x0001da1e, " 121374", "% d"}, + {__LINE__, -0x04deac7, "-5106375", "%03.4d"}, + {__LINE__, 0x000012b1, "4785", "%d"}, + {__LINE__, 0x0009a116, "9a116", "%-x"}, + {__LINE__, -0x0000003, "-000003", "%4.6ld"}, + {__LINE__, 0x00000000, "", "%.0x"}, + {__LINE__, 0x0000000a, " A", "% 3X"}, + {__LINE__, 0xfffffff0, "0xfffffff0", "%-#0.1x"}, + {__LINE__, 0x00000086, "86", "%0X"}, + {__LINE__, 0x0000001f, "31", "%-d"}, + {__LINE__, 0x0386f706, "59176710", "%3.6ld"}, + {__LINE__, 0xffe437ae, "0XFFE437AE", "%#X"}, + {__LINE__, 0x0e5405c6, "e5405c6", "%-x"}, + {__LINE__, 0xfffdee46, "FFFDEE46", "%+X"}, + {__LINE__, 0xff861e9d, "FF861E9D", "%5.X"}, + {__LINE__, 0xfffff570, "FFFFF570", "%-X"}, + {__LINE__, 0x045c90bd, "45c90bd", "%0x"}, + {__LINE__, 0x000000aa, "AA", "%+X"}, + {__LINE__, -0x747262e, "-122103342", "%0ld"}, + {__LINE__, 0xffffad89, "FFFFAD89", "%X"}, + {__LINE__, 0x00000018, "18", "%0.2X"}, + {__LINE__, 0x00057a2d, " 358957", "% #7.d"}, + {__LINE__, 0x73a0e21d, "1939923485", "%ld"}, + {__LINE__, -0x00021e1, "-8673", "%0d"}, + {__LINE__, 0x000000e9, " 233", "% 06.3ld"}, + {__LINE__, 0xffffcc0d, "ffffcc0d", "%x"}, + {__LINE__, 0x00000052, "52", "%x"}, + {__LINE__, 0x01c155af, "1C155AF", "%.5X"}, + {__LINE__, 0xffffffff, "ffffffff", "%0.x"}, + {__LINE__, -0x00400a4, "-262308", "%-d"}, + {__LINE__, -0x0000001, "-0000001", "%#.7ld"}, + {__LINE__, 0x01fb9c99, "1fb9c99", "% 03.5x"}, + {__LINE__, 0xffffffff, "ffffffff", "%6.x"}, + {__LINE__, 0xff5e5eea, "FF5E5EEA", "%X"}, + {__LINE__, 0x0000000f, "F", "%+0X"}, + {__LINE__, 0xfffffe3a, "fffffe3a", "% x"}, + {__LINE__, 0xfffffffe, "FFFFFFFE", "%-X"}, + {__LINE__, -0x0000007, "-7", "%0ld"}, + {__LINE__, -0x002f635, "-194101", "%d"}, + {__LINE__, -0x0000005, "-5", "%ld"}, + {__LINE__, -0x0000059, "-89", "%0ld"}, + {__LINE__, -0x1ccda96, "-30202518", "%d"}, + {__LINE__, -0x0000002, "-2", "%-d"}, + {__LINE__, 0xffffffda, "0XFFFFFFDA", "%#X"}, + {__LINE__, 0x00036cc5, "224453", "%#d"}, + {__LINE__, 0x00000010, "+16", "%+ld"}, + {__LINE__, 0x0b6039c7, "190855623", "%d"}, + {__LINE__, -0x0000002, "-002", "%#.3d"}, + {__LINE__, 0xfff3e746, "FFF3E746", "%-4X"}, + {__LINE__, 0x0361b3de, "361B3DE", "% 04X"}, + {__LINE__, 0xffffe6a2, "ffffe6a2", "%-x"}, + {__LINE__, 0x00032790, "0x0032790", "%#7.7x"}, + {__LINE__, 0xffe33b4e, "ffe33b4e", "%-0x"}, + {__LINE__, -0x0094663, "-607843", "%.3ld"}, + {__LINE__, -0x00a3efa, "-671482", "%4d"}, + {__LINE__, 0x000000e6, " 00E6", "%7.4X"}, + {__LINE__, 0x000125cb, "125CB", "%+X"}, + {__LINE__, 0x0000b97e, "b97e", "%x"}, + {__LINE__, -0x0000039, "-57", "%+ld"}, + {__LINE__, 0xffffe39a, "ffffe39a", "%5.4x"}, + {__LINE__, 0xffffffff, "ffffffff", "%6.1x"}, + {__LINE__, -0xdee910c, "-233738508", "%-#2d"}, + {__LINE__, -0x14044930, "-335825200", "%6.ld"}, + {__LINE__, 0x00a81259, "a81259", "%x"}, + {__LINE__, 0x020e74c6, "20E74C6", "%X"}, + {__LINE__, -0x00000fb, "-251", "%01.d"}, + {__LINE__, 0x034ceb45, "34CEB45", "%5.X"}, + {__LINE__, 0xfeacb66a, "feacb66a", "% x"}, + {__LINE__, 0x00003eb6, "3EB6", "% X"}, + {__LINE__, 0x000009ef, "9ef", "% x"}, + {__LINE__, 0xf9d9dd07, "F9D9DD07", "%0X"}, +#ifndef __PCCNECV70__ + + {__LINE__, 0x000154d7, "087255", "%.6ld"}, + {__LINE__, -0x000036e, "-878", "%-ld"}, + {__LINE__, -0x0007dcc, "-32204", "% d"}, + {__LINE__, 0x000019dc, "6620", "%d"}, + {__LINE__, 0xffffff6e, "FFFFFF6E", "%2X"}, + {__LINE__, 0x00000016, "0x16", "%-#x"}, + {__LINE__, 0xffdb3e96, "ffdb3e96", "%+.5x"}, + {__LINE__, 0xffffffef, "FFFFFFEF", "% 04.X"}, + {__LINE__, 0xffffffff, "FFFFFFFF", "%5X"}, + {__LINE__, 0x000c08cd, "C08CD", "%-X"}, + {__LINE__, 0x18de71ee, "18de71ee", "%x"}, + {__LINE__, 0xffffffff, "ffffffff", "% x"}, + {__LINE__, -0x00025bd, "-009661", "%#.6d"}, + {__LINE__, 0x2f05de14, " 788913684", "% 6.4ld"}, + {__LINE__, 0x0c99cf86, "211406726", "%.5d"}, + {__LINE__, 0x09e6a21d, " 166109725", "% 0d"}, + {__LINE__, -0x013da65, "-1301093", "%+ld"}, + {__LINE__, 0x000003d0, " 976", "% 4.2d"}, + {__LINE__, -0xef9e3cc, "-251257804", "%-7.0d"}, + {__LINE__, 0x0000000c, "+12", "%+00ld"}, + {__LINE__, 0xffffffe2, "ffffffe2", "%x"}, + {__LINE__, 0x000000fe, "fe", "%-x"}, + {__LINE__, 0xfffffc5f, "fffffc5f", "%x"}, + {__LINE__, -0x0005c55, "-23637", "%d"}, + {__LINE__, -0x0111ff2, "-1122290", "% 7.d"}, + {__LINE__, -0x1a1746e5, "-437733093", "%.4ld"}, + {__LINE__, 0x00001007, "004103", "%00.6d"}, + {__LINE__, 0xffffffe1, "ffffffe1", "%x"}, + {__LINE__, 0x0000d80f, "55311", "%d"}, + {__LINE__, 0x00000004, "4", "%+X"}, + {__LINE__, 0xfffffffd, "fffffffd", "%6x"}, + {__LINE__, -0x2afbcc3c, "-721144892", "%4.6ld"}, + {__LINE__, 0x00377f96, "377F96", "%6.X"}, + {__LINE__, 0xffffffc8, "FFFFFFC8", "%0X"}, + {__LINE__, 0x007ed9d1, "8313297", "%-3d"}, + {__LINE__, 0x00000000, "0", "%-X"}, + {__LINE__, 0x001e122d, "1970733", "%01.d"}, + {__LINE__, -0x000029b, "-667", "%ld"}, + {__LINE__, -0x002f563, "-193891", "% 2.5d"}, + {__LINE__, 0xfffffffe, "fffffffe", "%+3.7x"}, + {__LINE__, 0xfff7658c, "fff7658c", "%x"}, + {__LINE__, -0x0006d08, "-0027912", "%7.7ld"}, + {__LINE__, 0x00000080, "80", "%X"}, + {__LINE__, 0x0000078f, " 1935", "% 6ld"}, + {__LINE__, 0xfffffffb, "FFFFFFFB", "% X"}, + {__LINE__, 0x000000ae, " AE", "%7.X"}, + {__LINE__, -0x000d6aa, "-54954", "%-ld"}, + {__LINE__, 0xfffffffe, "FFFFFFFE", "%6.X"}, + {__LINE__, 0x000003bf, "+959", "%+d"}, + {__LINE__, -0x7242d04b, "-1916981323", "% ld"}, + {__LINE__, 0xf0e6546f, "F0E6546F", "% .7X"}, + {__LINE__, 0x047e196a, "75372906", "%ld"}, + {__LINE__, 0x0240174e, "240174E", "%+0X"}, + {__LINE__, -0x0000006, "-006", "%.3ld"}, + {__LINE__, 0x00068aac, "428716", "%#ld"}, + {__LINE__, 0xffffffa1, "ffffffa1", "%0x"}, + {__LINE__, 0x000adab3, "adab3", "%x"}, + {__LINE__, -0x00000ae, "-0000174", "%7.7ld"}, + {__LINE__, 0x0f0a8f4b, "f0a8f4b", "% x"}, + {__LINE__, 0xfee229bc, "fee229bc", "%x"}, + {__LINE__, -0x275b8455, "-660309077", "%.2ld"}, + {__LINE__, -0xca4b2a5, "-212120229", "%d"}, + {__LINE__, 0x000000b5, " 181", "%04.d"}, + {__LINE__, 0xfff213f2, "FFF213F2", "%X"}, + {__LINE__, 0x00000003, "3", "%-X"}, + {__LINE__, 0x0ebb978b, "247175051", "%.6ld"}, + {__LINE__, 0xffffffca, "FFFFFFCA", "%X"}, + {__LINE__, -0x0000003, "-03", "%+0.2d"}, + {__LINE__, -0x178ff86, "-24706950", "%+d"}, + {__LINE__, 0x0000003c, " 60", "%7.ld"}, + {__LINE__, -0x001012d, "-65837", "%.5d"}, + {__LINE__, 0x03673fd2, "3673fd2", "%.5x"}, + {__LINE__, -0x001fe74, "-130676", "%#0.6ld"}, + {__LINE__, 0x004f6226, "4f6226", "%-0x"}, + {__LINE__, 0xfffffa0f, "fffffa0f", "%7x"}, + {__LINE__, 0xfffffff8, "fffffff8", "%-.6x"}, + {__LINE__, -0x0000001, "-1", "%#d"}, + {__LINE__, 0xffffe43d, "ffffe43d", "%7x"}, + {__LINE__, 0x00009d47, "0040263", "%7.7ld"}, + {__LINE__, 0xff0fc79f, "FF0FC79F", "%-4.X"}, + {__LINE__, 0x2d610907, "2d610907", "%x"}, + {__LINE__, -0x00002de, "-734", "%04d"}, + {__LINE__, 0x0000036b, " 36B", "%7.1X"}, + {__LINE__, 0x00000002, "2", "%.0x"}, + {__LINE__, -0x1f577b3e, "-525826878", "%d"}, + {__LINE__, 0x0000002d, "45", "%#ld"}, + {__LINE__, 0xfffffffb, "fffffffb", "%x"}, + {__LINE__, -0x0000004, "-4", "%-.0d"}, + {__LINE__, 0x00016ba4, "16ba4", "%x"}, + {__LINE__, -0x1ad853d5, "-450384853", "% 0ld"}, + {__LINE__, 0x00366899, "366899", "%2.x"}, + {__LINE__, 0x1659158b, "1659158B", "%X"}, + {__LINE__, 0x000076c7, "76c7", "%x"}, + {__LINE__, 0x00000674, "674", "%0x"}, + {__LINE__, 0x016c9d8a, "+23895434", "%+ld"}, + {__LINE__, 0xfffffc43, "FFFFFC43", "%0X"}, + {__LINE__, 0xfff249d2, "fff249d2", "%-2.x"}, + {__LINE__, 0xffffffdd, "ffffffdd", "%5x"}, + {__LINE__, 0xffffa7ef, "FFFFA7EF", "%0.3X"}, + {__LINE__, 0x030d4d06, "30d4d06", "%x"}, + {__LINE__, -0x0e4694b, "-14969163", "%d"}, + {__LINE__, 0x00000078, "120", "%.3ld"}, + {__LINE__, -0x16c1273e, "-381757246", "%0d"}, + {__LINE__, 0xffffd183, "FFFFD183", "% 0X"}, + {__LINE__, -0x2dfdadd, "-48224989", "%ld"}, + {__LINE__, -0x5767fe5c, "-1466433116", "%ld"}, + {__LINE__, 0x01bea036, "29270070", "%#4.3d"}, + {__LINE__, 0x00000027, "27", "%-x"}, + {__LINE__, 0x0004d183, "4D183", "%X"}, + {__LINE__, 0xffdaf9c8, "FFDAF9C8", "%.7X"}, + {__LINE__, 0xfffffd95, "fffffd95", "%x"}, + {__LINE__, -0x0059acb, "-367307", "%00.d"}, + {__LINE__, 0x017d8db4, "17d8db4", "% 05.x"}, + {__LINE__, 0x0001f084, "001F084", "%5.7X"}, + {__LINE__, 0x00000006, " 6", "% ld"}, + {__LINE__, 0x011b0802, "18548738", "%.3ld"}, + {__LINE__, 0x00000006, " 6", "%#3.d"}, + {__LINE__, 0xffffffff, "ffffffff", "%6x"}, + {__LINE__, 0x00000001, "1", "% x"}, + {__LINE__, 0x00000750, " 01872", "% .5d"}, + {__LINE__, 0xfffffff9, "fffffff9", "%x"}, + {__LINE__, -0x00f842c, "-1016876", "%+d"}, + {__LINE__, -0x001dcf0, "-122096", "%-0d"}, + {__LINE__, 0x000000c9, "0XC9", "% #3X"}, + {__LINE__, 0x1e368e86, "1e368e86", "%7x"}, + {__LINE__, -0x0001157, "-4439", "% 03.4d"}, + {__LINE__, 0xfff91e87, "FFF91E87", "%3.5X"}, + {__LINE__, 0x00000006, "6", "%x"}, + {__LINE__, 0x00000bca, "3018", "%ld"}, + {__LINE__, 0x00495ff6, "495ff6", "%01.0x"}, + {__LINE__, -0x0267f7a, "-2523002", "% #5d"}, + {__LINE__, 0x00000060, "96", "%-d"}, + {__LINE__, 0x000850c6, "544966", "%#.1d"}, + {__LINE__, 0x00231457, "231457", "%-6.4X"}, + {__LINE__, 0x0000000b, "11", "%-d"}, + {__LINE__, 0x000003bd, "957", "%#d"}, + {__LINE__, 0xfd51d970, "FD51D970", "%+.7X"}, + {__LINE__, 0x000007bf, "1983", "%-ld"}, + {__LINE__, 0x000034b2, "34B2", "%0.X"}, + {__LINE__, 0x0000c18d, "C18D", "%0.X"}, + {__LINE__, 0xffffbb83, "ffffbb83", "% 3x"}, + {__LINE__, 0x000001d0, "0x1d0", "%#x"}, + {__LINE__, -0x003c784, "-247684", "%#ld"}, + {__LINE__, 0x0210cffa, "210CFFA", "%X"}, + {__LINE__, -0x33093a1, "-53515169", "% 0d"}, + {__LINE__, 0x00000001, " 1", "% #ld"}, + {__LINE__, 0xffffaa53, "ffffaa53", "%-4.5x"}, + {__LINE__, 0x2935c056, "691388502", "%1.d"}, + {__LINE__, -0x0000001, "-1", "%ld"}, + {__LINE__, 0x000013d1, "0X013D1", "%+#2.5X"}, + {__LINE__, -0x000035c, "-860", "%-.2d"}, + {__LINE__, 0x00000000, " ", "%-4.X"}, + {__LINE__, -0x0000147, "-327", "%d"}, + {__LINE__, 0x0a317eb0, "171015856", "%.7d"}, + {__LINE__, 0x00000003, "3", "%ld"}, + {__LINE__, 0x000001d6, "1d6", "%-x"}, + {__LINE__, 0x18185214, "404247060", "%-.2ld"}, + {__LINE__, 0x00574140, "0574140", "%.7X"}, + {__LINE__, 0x00002ea5, "02ea5", "%.5x"}, + {__LINE__, 0x00000005, "5", "% x"}, + {__LINE__, 0xffc47ed6, "ffc47ed6", "%-x"}, + {__LINE__, 0x0001f5da, "0X1F5DA", "%#X"}, + {__LINE__, 0xfffffcc9, "fffffcc9", "% x"}, + {__LINE__, 0x02586c98, "2586C98", "%X"}, + {__LINE__, -0x0000036, "-54", "% ld"}, + {__LINE__, 0x00064b57, "412503", "%1.5d"}, + {__LINE__, -0x0000007, "-7", "%#0.0ld"}, + {__LINE__, 0x00023a03, "145923", "%ld"}, + {__LINE__, -0x0000065, "-101", "%+#d"}, + {__LINE__, 0x00000208, "208", "%X"}, + {__LINE__, 0x00e97728, "15300392", "%-ld"}, + {__LINE__, 0x0000030a, "+778", "%+d"}, + {__LINE__, 0xf4d7deee, "F4D7DEEE", "%+X"}, + {__LINE__, -0x000009a, "-154", "%+.3ld"}, + {__LINE__, 0x000002c0, "+704", "%+ld"}, + {__LINE__, 0x0067ec23, "67ec23", "%x"}, + {__LINE__, 0x005ca7fc, "+6072316", "%+d"}, + {__LINE__, 0xfffff5f1, "fffff5f1", "%x"}, + {__LINE__, 0x00000601, " 0X601", "%#7.2X"}, + {__LINE__, -0x0000057, "-87", "% ld"}, + {__LINE__, -0x0000078, " -120", "%7.d"}, + {__LINE__, -0x000001f, "-31 ", "%-6d"}, + {__LINE__, 0x0160c000, "23117824", "%ld"}, + {__LINE__, -0x0000007, "-7", "%0ld"}, + {__LINE__, 0xfffffffe, "fffffffe", "%+x"}, + {__LINE__, 0x5b6ef898, "5b6ef898", "%+0x"}, + {__LINE__, 0x0009bfb6, "638902", "%d"}, + {__LINE__, -0x0000145, " -325", "%#6.0d"}, + {__LINE__, -0x508c048, "-84459592", "%-5.ld"}, + {__LINE__, 0x00000002, " 0x2", "% #4x"}, + {__LINE__, 0x0001486e, "84078", "%#d"}, + {__LINE__, 0x006ac76a, "6997866", "%.6d"}, + {__LINE__, 0xffffff98, "FFFFFF98", "%X"}, + {__LINE__, 0xfffffe08, "fffffe08", "%6.x"}, + {__LINE__, -0x24ef47e, "-38728830", "%1.6ld"}, + {__LINE__, 0x39d1b2db, "39D1B2DB", "% X"}, + {__LINE__, 0xfffc9ce2, "0xfffc9ce2", "%#x"}, + {__LINE__, 0x59485e14, "1497914900", "%#4ld"}, + {__LINE__, 0x000022eb, "22EB", "%3.X"}, + {__LINE__, 0xfa410352, "FA410352", "%-X"}, + {__LINE__, -0x4a342f0, "-77808368", "% ld"}, + {__LINE__, 0xef5825a4, "EF5825A4", "%.6X"}, + {__LINE__, 0x01b0185a, "0x1b0185a", "% #x"}, + {__LINE__, -0x0004290, "-17040", "%.3d"}, + {__LINE__, -0x10f88659, "-284722777", "%+6.2ld"}, + {__LINE__, 0x00003216, "12822", "%-d"}, + {__LINE__, 0x4a31a219, "1244766745", "%d"}, + {__LINE__, 0xffffffb5, "FFFFFFB5", "%-X"}, + {__LINE__, 0x0000d586, "D586", "%-2.X"}, + {__LINE__, 0x00008496, "33942", "%1.d"}, + {__LINE__, -0x0000011, "-17", "%1ld"}, + {__LINE__, 0x0000003c, "3C", "%2.X"}, + {__LINE__, 0xfffffffe, "FFFFFFFE", "%.3X"}, + {__LINE__, 0x0000152a, "152A", "%X"}, + {__LINE__, 0xfffe876e, "FFFE876E", "%X"}, + {__LINE__, -0x0003874, "-14452", "%-ld"}, + {__LINE__, -0x004918a, "-299402", "%#.6d"}, + {__LINE__, -0x000000c, "-12", "%-d"}, + {__LINE__, 0xffffd812, "FFFFD812", "%+X"}, + {__LINE__, -0x000000c, "-12", "%0d"}, + {__LINE__, -0x0000229, "-553", "%0d"}, + {__LINE__, 0x00002ab8, "2AB8", "%4.X"}, + {__LINE__, 0x0000004b, "+75", "%+ld"}, + {__LINE__, 0x001c3178, " 1847672", "% ld"}, + {__LINE__, -0x0000006, "-6", "%.0ld"}, + {__LINE__, 0x0003be65, "245349", "%ld"}, + {__LINE__, -0x0000001, "-1", "%#1ld"}, + {__LINE__, 0x0000007f, "+127", "%+ld"}, + {__LINE__, 0x0000020e, "526 ", "%-5ld"}, + {__LINE__, 0x00000002, "2", "%d"}, + {__LINE__, 0x03bd0873, "0X3BD0873", "%#X"}, + {__LINE__, 0x00093a52, "604754", "%-2d"}, + {__LINE__, -0x00823fc, "-0533500", "%+.7ld"}, + {__LINE__, 0x00000000, " ", "%4.X"}, + {__LINE__, -0x0000001, "-1", "%ld"}, + {__LINE__, 0xfff09ede, "fff09ede", "%-x"}, + {__LINE__, 0x01e28c24, "31624228", "%ld"}, + {__LINE__, 0x00001dcc, "1DCC", "%X"}, + {__LINE__, -0x0f0ea0d, "-15788557", "%-00.ld"}, + {__LINE__, 0xffff73a1, "FFFF73A1", "%X"}, + {__LINE__, -0x0004040, "-16448", "%+#d"}, + {__LINE__, 0x0073b158, "0x73b158", "%#7x"}, + {__LINE__, 0xfffff8ff, "FFFFF8FF", "% 5X"}, + {__LINE__, -0x0000004, "-4", "%0d"}, + {__LINE__, 0xffff56a3, "ffff56a3", "%1.6x"}, + {__LINE__, -0x0000041, "-65", "%0d"}, + {__LINE__, 0x00000009, "+0000009", "%+2.7d"}, + {__LINE__, 0xffffe13d, "0XFFFFE13D", "%+#2.X"}, + {__LINE__, -0x00002ce, "-00718", "%.5ld"}, + {__LINE__, 0xffc53e28, "ffc53e28", "%03.4x"}, + {__LINE__, 0x00000005, "0X5", "%#X"}, + {__LINE__, 0x00000004, "4", "%x"}, + {__LINE__, 0xe21b35eb, "e21b35eb", "%-x"}, + {__LINE__, -0x0000098, "-152", "%-ld"}, + {__LINE__, 0xffffff81, "FFFFFF81", "%2X"}, + {__LINE__, 0x00000003, "00003", "%.5ld"}, + {__LINE__, 0xfffffffc, "fffffffc", "%+x"}, + {__LINE__, 0x000491f5, "299509", "%1.ld"}, + {__LINE__, 0xf06bfd7d, "F06BFD7D", "%X"}, + {__LINE__, -0x0000006, "-6", "%0d"}, + {__LINE__, -0x0a4af7d, "-10792829", "%-0d"}, + {__LINE__, -0x11a74ef, "-18511087", "%ld"}, + {__LINE__, 0x2dc95e17, "768171543", "%-1.d"}, + {__LINE__, -0x0000001, "-1", "%#ld"}, + {__LINE__, 0x00000033, "51", "%-#d"}, + {__LINE__, 0x002d0219, "2949657", "%ld"}, + {__LINE__, 0xfffffffd, "FFFFFFFD", "%2.4X"}, + {__LINE__, 0x00000f20, "+3872", "%+ld"}, + {__LINE__, -0x0000047, "-071", "%4.3ld"}, + {__LINE__, 0x00000010, " 16", "%6d"}, + {__LINE__, 0x00000000, "0", "%0d"}, + {__LINE__, 0xffffff57, "FFFFFF57", "%X"}, + {__LINE__, -0x0000700, "-1792", "%-#d"}, + {__LINE__, -0x0000064, "-100", "%-ld"}, + {__LINE__, 0xff26d49a, "FF26D49A", "%-0X"}, + {__LINE__, -0x0000006, "-6", "%.1ld"}, + {__LINE__, 0x1391edc5, "1391edc5", "%.1x"}, + {__LINE__, 0x0000014a, "330", "%-#ld"}, + {__LINE__, 0x07ecd1e1, "7ecd1e1", "%6x"}, + {__LINE__, -0xa2b8898, "-170625176", "%3.d"}, + {__LINE__, 0x00035287, "0x35287", "%#.0x"}, + {__LINE__, 0x00000000, "00000", "%0.5X"}, + {__LINE__, -0x0000153, "-339", "%d"}, + {__LINE__, 0x0000ad6e, " 44398", "% 0.d"}, + {__LINE__, 0x00000001, "1", "%-X"}, + {__LINE__, -0x000000d, "-13 ", "%-6.2d"}, + {__LINE__, -0x01bff8a, "-1834890", "%d"}, + {__LINE__, 0x000a0fa8, "+659368", "%+ld"}, + {__LINE__, 0xac044dc0, "ac044dc0", "%.0x"}, + {__LINE__, 0x0003a729, "239401", "%ld"}, + {__LINE__, -0x00004f0, "-1264", "%+ld"}, + {__LINE__, 0x00000000, "0", "%x"}, + {__LINE__, 0x00000012, "12", "% x"}, + {__LINE__, 0xffff770b, "ffff770b", "%7.1x"}, + {__LINE__, 0xd7458572, "d7458572", "%x"}, + {__LINE__, 0x01f1b4f9, "1f1b4f9", "% 0x"}, + {__LINE__, 0xfffb2198, "0XFFFB2198", "% #1.X"}, + {__LINE__, 0x00ef6189, " 15688073", "% ld"}, + {__LINE__, -0x3f0ae63, "-66104931", "% #d"}, + {__LINE__, -0x0006075, "-24693", "%ld"}, + {__LINE__, 0x000000a1, "161", "%d"}, + {__LINE__, 0xfffffffc, "fffffffc", "%3.2x"}, + {__LINE__, 0xff6b4125, "FF6B4125", "%+5.X"}, + {__LINE__, 0x00000001, " 1", "%6.d"}, + {__LINE__, -0x0000001, "-1", "%+0d"}, + {__LINE__, -0x000096c, "-2412", "%0.4d"}, + {__LINE__, 0x00039c6f, "236655", "%.0d"}, + {__LINE__, 0x3625286c, "908404844", "%.1d"}, + {__LINE__, 0xfffffffa, "fffffffa", "%x"}, + {__LINE__, 0x00000000, "0", "%X"}, + {__LINE__, 0x00c34353, "12796755", "%#.4d"}, + {__LINE__, 0x000002df, "735", "%ld"}, + {__LINE__, 0xffff38fa, "FFFF38FA", "%5.1X"}, + {__LINE__, -0x000002a, "-42", "%0.d"}, + {__LINE__, 0x0000077e, "1918", "%ld"}, + {__LINE__, 0x000001f7, "503", "%ld"}, + {__LINE__, 0x00000004, "4", "%ld"}, + {__LINE__, 0x0021fb02, "2226946", "%d"}, + {__LINE__, 0x007c858d, "8160653", "%-0d"}, + {__LINE__, -0x0000109, "-265", "%.0d"}, + {__LINE__, 0xfffffffa, "fffffffa", "% x"}, + {__LINE__, 0x0097932d, "97932d", "%+x"}, + {__LINE__, 0xfffbbd03, "fffbbd03", "%-.5x"}, + {__LINE__, -0x0000005, "-5", "%0d"}, + {__LINE__, 0x03ee3a95, "65944213", "%ld"}, + {__LINE__, -0x0000002, " -00002", "%7.5d"}, + {__LINE__, 0xea1a4e54, "EA1A4E54", "%X"}, + {__LINE__, -0x000a9d6, "-43478", "%+d"}, + {__LINE__, -0x0fd8d65, "-16616805", "%5ld"}, + {__LINE__, 0x00000001, "1", "%-d"}, + {__LINE__, 0x00000072, "72", "%2.X"}, + {__LINE__, 0xffffff94, "FFFFFF94", "%X"}, + {__LINE__, 0x00000d2a, "D2A", "%+X"}, + {__LINE__, 0xffff614f, "ffff614f", "%x"}, + {__LINE__, -0x0000018, "-24", "%ld"}, + {__LINE__, 0x0000003b, "059", "%2.3d"}, + {__LINE__, 0x0000001a, "1A", "%0X"}, + {__LINE__, -0x0000109, "-265", "%02ld"}, + {__LINE__, 0x00000f9d, "3997", "%#1d"}, + {__LINE__, 0x0000001b, "1b", "%x"}, + {__LINE__, 0xffffff07, "ffffff07", "%x"}, + {__LINE__, 0xfff1a425, "0xfff1a425", "%#x"}, + {__LINE__, 0x00000005, "5", "%-1d"}, + {__LINE__, -0x53a6efd, "-87715581", "%ld"}, + {__LINE__, 0x042f37db, "42F37DB", "%6.X"}, + {__LINE__, 0x00053bd5, "+342997", "%+5ld"}, + {__LINE__, 0xffffffff, "ffffffff", "% x"}, + {__LINE__, 0xffffffef, "FFFFFFEF", "%6.X"}, + {__LINE__, -0x10ec29a, "-17744538", "%+2.d"}, + {__LINE__, 0x01dcc901, "1dcc901", "%x"}, + {__LINE__, 0x0007dc68, "515176", "%-3.d"}, + {__LINE__, -0x0000062, "-0098", "%#2.4d"}, + {__LINE__, 0x00000000, " ", "%3.0X"}, + {__LINE__, -0x00000de, "-222", "%ld"}, + {__LINE__, 0x000c1fb8, "C1FB8", "%0X"}, + {__LINE__, 0x0005bf58, "5BF58", "%X"}, + {__LINE__, -0x000dd1b, "-56603", "%-03.1d"}, + {__LINE__, 0x00000b14, "B14", "%+3.X"}, + {__LINE__, 0x00000799, "799", "%-x"}, + {__LINE__, 0xfffffffa, "fffffffa", "%x"}, + {__LINE__, 0x00000003, "0x00003", "% #7.5x"}, + {__LINE__, -0x002b9f1, "-178673", "%+d"}, + {__LINE__, -0x001b817, "-112663", "%0d"}, + {__LINE__, 0xf6f98533, "f6f98533", "%.1x"}, + {__LINE__, 0x00338ffc, "3379196", "%d"}, + {__LINE__, 0x0409e9b5, "0X409E9B5", "%#X"}, + {__LINE__, 0x00b8e98e, " 12118414", "% 0d"}, + {__LINE__, -0x002a8f6, "-174326", "%ld"}, + {__LINE__, 0x13bb9722, "331061026", "%0d"}, + {__LINE__, 0x00063c71, "63c71", "%x"}, + {__LINE__, 0xfffffe37, "FFFFFE37", "%.1X"}, + {__LINE__, 0xffff7b3e, "ffff7b3e", "%+.1x"}, + {__LINE__, 0x005eeab1, "5eeab1", "%+x"}, + {__LINE__, 0xffc69e99, "ffc69e99", "%x"}, + {__LINE__, -0x000005c, " -92", "%5ld"}, + {__LINE__, 0x07d8fa8a, "7d8fa8a", "%x"}, + {__LINE__, 0x00030e0a, "200202", "%-4.1d"}, + {__LINE__, 0x00001dc1, "1DC1", "%.0X"}, + {__LINE__, 0x00000008, "8", "%0ld"}, + {__LINE__, -0x01b5397, "-1790871", "%3.d"}, + {__LINE__, 0x27fbb4b5, "27fbb4b5", "%x"}, + {__LINE__, -0xe3fdcfc, "-239066364", "%4ld"}, + {__LINE__, 0x0008bcb1, "8bcb1", "%0x"}, + {__LINE__, 0xffff039e, "ffff039e", "%.6x"}, + {__LINE__, 0x000d0bd4, "d0bd4", "%-x"}, + {__LINE__, 0x00000000, "+0", "%+d"}, + {__LINE__, -0x2a49ea4, "-44342948", "%1d"}, + {__LINE__, -0x0000055, "-85", "%0d"}, + {__LINE__, -0x0000a05, "-2565", "%3.3ld"}, + {__LINE__, -0x000000b, "-11 ", "%-4d"}, + {__LINE__, 0xfff30444, "FFF30444", "%7.X"}, + {__LINE__, 0x0000003f, "63", "%-0d"}, + {__LINE__, 0x00046779, "46779", "%X"}, + {__LINE__, 0x000028d7, " 0X28D7", "%#7.X"}, + {__LINE__, 0x00000019, " 19", "%5x"}, + {__LINE__, 0x0032291d, "3287325", "%2.ld"}, + {__LINE__, 0x00057afa, "57AFA", "%X"}, + {__LINE__, 0xfffe8c64, "fffe8c64", "%3.4x"}, + {__LINE__, -0x624110d6, "-1648431318", "%+6d"}, + {__LINE__, 0x0000001a, " 26", "% 5.d"}, + {__LINE__, 0xfffffffd, "FFFFFFFD", "%6.X"}, + {__LINE__, 0x00199490, "1676432", "%-1d"}, + {__LINE__, -0xfff8265, "-268403301", "%-0ld"}, + {__LINE__, 0x0000000e, "e", "%-x"}, + {__LINE__, 0x00c38aec, "c38aec", "%x"}, + {__LINE__, -0x00658f4, "-415988", "%#ld"}, + {__LINE__, 0x0066ac4e, "6728782", "%0d"}, + {__LINE__, 0xc4a808d5, "c4a808d5", "%x"}, + {__LINE__, 0x00000000, " 0", "%6.1x"}, + {__LINE__, 0x00066d24, "66d24", "%x"}, + {__LINE__, 0x00002117, "2117", "%X"}, + {__LINE__, 0xfe06fdf4, "FE06FDF4", "%0X"}, + {__LINE__, 0xfffffff9, "fffffff9", "%x"}, + {__LINE__, 0x1c6bb795, "+476821397", "%+d"}, + {__LINE__, 0xfffffba4, "FFFFFBA4", "%X"}, + {__LINE__, 0xfffd7d44, "fffd7d44", "%0x"}, + {__LINE__, 0x0019dd17, "1694999", "%d"}, + {__LINE__, 0x6475851e, "0x6475851e", "% #7.x"}, + {__LINE__, -0x00330d5, "-209109", "%0ld"}, + {__LINE__, 0x009f53f7, "09F53F7", "%+6.7X"}, + {__LINE__, -0x00172f9, " -94969", "%7.5ld"}, + {__LINE__, 0xffffffdc, "FFFFFFDC", "%X"}, + {__LINE__, -0x013bdec, "-1293804", "%+#2ld"}, + {__LINE__, 0xfff845c0, "fff845c0", "%+7.x"}, + {__LINE__, -0x0fbe712, "-16508690", "%.7d"}, + {__LINE__, 0xffffe13f, "ffffe13f", "%x"}, + {__LINE__, 0xbbc19951, "BBC19951", "%-4X"}, + {__LINE__, -0x28d6d1b, "-42822939", "%#d"}, + {__LINE__, -0x93cda6b, "-154983019", "%-d"}, + {__LINE__, 0xffc93342, "FFC93342", "%7.X"}, + {__LINE__, 0x072ac8b3, " 120244403", "% ld"}, + {__LINE__, 0xfff8d17b, "FFF8D17B", "% X"}, + {__LINE__, 0x00000da5, "DA5", "%0.X"}, + {__LINE__, 0x055ad3ac, "0x55ad3ac", "%#1x"}, + {__LINE__, 0x00000e5a, " 3674", "% .4ld"}, + {__LINE__, 0xffffff79, "FFFFFF79", "%+X"}, + {__LINE__, 0xffffe167, "ffffe167", "%+5x"}, + {__LINE__, 0x0000fa50, "0064080", "%.7ld"}, + {__LINE__, 0x00000001, "1", "%-d"}, + {__LINE__, 0xfffffffe, "fffffffe", "%x"}, + {__LINE__, 0x000009ac, " 2476", "% #2.ld"}, + {__LINE__, 0x00000e78, "0003704", "%.7ld"}, + {__LINE__, -0x000dfc3, "-57283", "%6.d"}, + {__LINE__, 0x00000ee0, "EE0", "%0X"}, + {__LINE__, -0x293703b3, "-691471283", "%ld"}, + {__LINE__, 0x000073e8, " 73e8", "%6x"}, + {__LINE__, 0xffffdffd, "ffffdffd", "%5.5x"}, + {__LINE__, 0x00000004, "4", "%+00.x"}, + {__LINE__, 0x00000008, "0X8", "%#X"}, + {__LINE__, 0x0003044c, "3044c", "%0x"}, + {__LINE__, 0xffc2095e, "FFC2095E", "%0X"}, + {__LINE__, 0x00000aac, "0002732", "%-.7ld"}, + {__LINE__, 0xffffff79, "FFFFFF79", "%+1.X"}, + {__LINE__, 0x53a0f94a, " 1403058506", "% 2.4ld"}, + {__LINE__, 0x000e928d, "e928d", "%x"}, + {__LINE__, 0x0024127a, "2364026", "%5.d"}, + {__LINE__, 0x000f7c1c, " 1014812", "% 2.d"}, + {__LINE__, -0x000003d, "-61 ", "%-#4ld"}, + {__LINE__, -0x0000122, "-290", "%+ld"}, + {__LINE__, 0x00000006, "+6", "%+0d"}, + {__LINE__, 0x00000129, " 000129", "%7.6X"}, + {__LINE__, 0x15d873e5, " 366506981", "% 5.7d"}, + {__LINE__, 0x00000001, "1", "%d"}, + {__LINE__, 0x038d6110, "+59597072", "%+d"}, + {__LINE__, 0x0f46ccc3, "256298179", "%-1.4d"}, + {__LINE__, 0xfe434d21, "fe434d21", "%x"}, + {__LINE__, -0x179797d7, "-395810775", "%d"}, + {__LINE__, 0xe35807bf, "e35807bf", "%x"}, + {__LINE__, 0x00000003, " +3", "%+7d"}, + {__LINE__, 0x017e699e, "25061790", "%#ld"}, + {__LINE__, -0x0000029, "-41", "%d"}, + {__LINE__, 0xffffff11, "ffffff11", "%1.x"}, + {__LINE__, -0x0001bf6, "-7158", "%+0ld"}, + {__LINE__, 0xffffff4b, "FFFFFF4B", "%4X"}, + {__LINE__, -0x0000040, "-64", "%ld"}, + {__LINE__, 0x000034d6, "+13526", "%+ld"}, + {__LINE__, 0xffffffa7, "ffffffa7", "%x"}, + {__LINE__, 0xfffffe8e, "fffffe8e", "%+.3x"}, + {__LINE__, 0x00000000, "0", "%0.1d"}, + {__LINE__, -0x007cdf1, "-511473", "%ld"}, + {__LINE__, 0xfffffeef, "FFFFFEEF", "%-X"}, + {__LINE__, 0x00002d91, "2d91", "%x"}, + {__LINE__, -0x000025b, "-603", "%#ld"}, + {__LINE__, -0x000001b, "-27", "%-ld"}, + {__LINE__, 0x042b6752, "42B6752", "%+X"}, + {__LINE__, -0x75afb0a6, "-1974448294", "% ld"}, + {__LINE__, -0x0000001, "-1", "%ld"}, + {__LINE__, 0x00000542, " 1346", "%6d"}, + {__LINE__, -0x0000010, "-16", "%+0d"}, + {__LINE__, 0xfffed8f7, "FFFED8F7", "%5.X"}, + {__LINE__, 0x0541852e, "541852e", "% 2.x"}, + {__LINE__, 0xff22aec3, "ff22aec3", "% .1x"}, + {__LINE__, 0x5e34b745, "1580513093", "%ld"}, + {__LINE__, 0xfffffff3, "fffffff3", "%x"}, + {__LINE__, 0x0eda7b86, "+249199494", "%+.3d"}, + {__LINE__, 0xffffe40f, "ffffe40f", "%+x"}, + {__LINE__, -0x0003484, "-13444", "%-1.5ld"}, + {__LINE__, 0x0000334b, " 13131", "%6.ld"}, + {__LINE__, -0x0000149, "-329", "%ld"}, + {__LINE__, 0x00000001, " 001", "% 5.3x"}, + {__LINE__, 0xffeaa035, "ffeaa035", "%+3.5x"}, + {__LINE__, 0x00211dff, "2170367", "%ld"}, + {__LINE__, 0x007e91f4, "7e91f4", "%x"}, + {__LINE__, 0xfffffffc, "0xfffffffc", "%+#5x"}, + {__LINE__, 0xfffffffc, "FFFFFFFC", "%6.X"}, + {__LINE__, 0x0156a9b5, "156a9b5", "%x"}, + {__LINE__, 0xfffffe58, "fffffe58", "%+x"}, + {__LINE__, 0xffff8c66, "FFFF8C66", "%.0X"}, + {__LINE__, 0x00000000, "0", "%d"}, + {__LINE__, 0x00000005, " 5", "%7.d"}, + {__LINE__, -0x0000002, "-2", "%ld"}, + {__LINE__, 0x000076b9, " 30393", "% ld"}, + {__LINE__, 0x00003824, "3824", "%4X"}, + {__LINE__, 0x00000cdf, " CDF", "% 7X"}, + {__LINE__, 0x017c9e8f, "17C9E8F", "%-X"}, + {__LINE__, -0x20f582d, "-34560045", "%-.5d"}, + {__LINE__, 0x0000732e, " 29486", "% 7.ld"}, + {__LINE__, 0xffffffff, "FFFFFFFF", "%+0X"}, + {__LINE__, 0xf379a4a2, "F379A4A2", "% .7X"}, + {__LINE__, -0x0000028, "-40", "%-d"}, + {__LINE__, 0xe73cf79e, "e73cf79e", "%x"}, + {__LINE__, 0x00000002, " 2", "%7ld"}, + {__LINE__, -0x00029b1, "-10673", "%-#1.ld"}, + {__LINE__, -0x0002a58, "-10840", "%3.d"}, + {__LINE__, 0x00000002, "2", "%-X"}, + {__LINE__, 0xfffffffc, "FFFFFFFC", "%X"}, + {__LINE__, -0x000003a, " -58", "%5ld"}, + {__LINE__, 0x0245ef3b, "245ef3b", "%.3x"}, + {__LINE__, 0x00026cca, "158922", "%-06.3ld"}, + {__LINE__, 0x01016fd3, "16871379", "%-d"}, + {__LINE__, 0x000016fc, "5884", "%0ld"}, + {__LINE__, -0x00007fb, " -2043", "%7.3d"}, + {__LINE__, 0xfff73d3c, "fff73d3c", "%0x"}, + {__LINE__, -0x0000110, "-272", "% 04.3d"}, + {__LINE__, 0x0000000e, " 014", "% 04ld"}, + {__LINE__, -0x0000039, "-57", "%.0ld"}, + {__LINE__, 0x00000c95, "c95", "%0x"}, + {__LINE__, 0xffff3b2e, "ffff3b2e", "%x"}, + {__LINE__, 0xfffffffe, "FFFFFFFE", "%X"}, + {__LINE__, 0x00000015, "21", "%.1d"}, + {__LINE__, 0x0000000c, "+12", "%+ld"}, + {__LINE__, 0x00000000, "0", "%+X"}, + {__LINE__, 0xfffc9a14, "fffc9a14", "%-.5x"}, + {__LINE__, 0x0000003c, "60", "%.2ld"}, + {__LINE__, -0x0000023, " -35", "%6.d"}, + {__LINE__, 0x00000740, "1856", "%ld"}, + {__LINE__, 0x00000160, "160", "% 3.X"}, + {__LINE__, 0x00000006, " 6", "%5.X"}, + {__LINE__, -0x0000677, "-1655", "%.3ld"}, + {__LINE__, 0x12709c7e, "12709C7E", "%.7X"}, + {__LINE__, 0x0000090f, "+2319", "%+#d"}, + {__LINE__, 0xffffffe0, "0xffffffe0", "%#4.1x"}, + {__LINE__, 0x00132ce0, "1256672", "%ld"}, + {__LINE__, 0x00000059, "59", "%+X"}, + {__LINE__, -0x0000017, "-23", "%0d"}, + {__LINE__, -0x01fade6, "-2076134", "%-4.3ld"}, + {__LINE__, 0x000df3fa, "914426", "%.5ld"}, + {__LINE__, 0xfffffd0f, "fffffd0f", "%7x"}, + {__LINE__, -0x000c212, "-49682", "%0.1ld"}, + {__LINE__, 0xffff8bcb, "FFFF8BCB", "%.3X"}, + {__LINE__, 0x0000000a, "a", "% x"}, + {__LINE__, 0x00000021, "21", "%x"}, + {__LINE__, -0x0000040, "-64", "% 1ld"}, + {__LINE__, 0x00000009, " 9", "%6.ld"}, + {__LINE__, -0x0000d67, "-3431", "% 5.3d"}, + {__LINE__, 0x03a85f1b, "61366043", "%ld"}, + {__LINE__, 0x00000035, "000053", "%0.6d"}, + {__LINE__, 0xfffda5cb, "fffda5cb", "%+x"}, + {__LINE__, 0xffffff78, "FFFFFF78", "%-7.0X"}, + {__LINE__, 0xfffffd6d, "fffffd6d", "%7.x"}, + {__LINE__, 0xfffffc08, "fffffc08", "%7.3x"}, + {__LINE__, 0x00003335, " 13109", "% #d"}, + {__LINE__, -0x0000002, " -2", "%5.d"}, + {__LINE__, -0x13767a48, "-326531656", "%+ld"}, + {__LINE__, 0x00000be5, "3045", "%4.0ld"}, + {__LINE__, 0xfffdb28c, "FFFDB28C", "%X"}, + {__LINE__, 0x00127118, "1208600", "%ld"}, + {__LINE__, 0x00000086, " 134", "% ld"}, + {__LINE__, 0x0002214c, "2214c", "%+x"}, + {__LINE__, 0x0000a0c2, " a0c2", "%5.0x"}, + {__LINE__, -0x000007c, "-124", "%ld"}, + {__LINE__, 0x0000012a, "00298", "%3.5d"}, + {__LINE__, 0xfd4f1257, "FD4F1257", "%+2.X"}, + {__LINE__, 0xffffd8aa, "ffffd8aa", "%5x"}, + {__LINE__, -0x00000a7, "-167", "%+ld"}, + {__LINE__, -0x0009c69, "-40041", "%0.0ld"}, + {__LINE__, 0x02230418, "35849240", "%-0.6ld"}, + {__LINE__, 0x00000000, " 0", "%6x"}, + {__LINE__, 0xfffff184, "fffff184", "%02.5x"}, + {__LINE__, -0x0ae967b, "-11441787", "%d"}, + {__LINE__, 0x0015de3a, "1433146", "%-5.3d"}, + {__LINE__, -0x0071e93, "-466579", "%1.ld"}, + {__LINE__, 0xfffff8bd, "fffff8bd", "%+x"}, + {__LINE__, -0x00000e4, "-228", "%+ld"}, + {__LINE__, 0x000001d8, " 472", "% .3ld"}, + {__LINE__, 0x00315faa, "315faa", "%-0x"}, + {__LINE__, 0x00000e59, "e59", "%0x"}, + {__LINE__, -0x0000005, "-5", "%ld"}, + {__LINE__, 0x000018ec, "6380", "%ld"}, + {__LINE__, 0xfffffff9, "fffffff9", "%5.2x"}, + {__LINE__, -0xe7cfd39, "-243072313", "%-d"}, + {__LINE__, 0x0002c30b, "2C30B", "%1.2X"}, + {__LINE__, 0x0000007a, "7A", "%X"}, + {__LINE__, 0xf604ecb2, "F604ECB2", "%2X"}, + {__LINE__, -0x05f602c, "-6250540", "%d"}, + {__LINE__, 0xffff9c86, "ffff9c86", "%2.x"}, + {__LINE__, -0x00d7f55, "-884565", "%#d"}, + {__LINE__, 0xd2a37824, "D2A37824", "%4X"}, + {__LINE__, 0x00000000, "", "%-.0x"}, + {__LINE__, 0x000d4b51, "0d4b51", "%3.6x"}, + {__LINE__, 0x000ecd97, "ecd97", "% x"}, + {__LINE__, -0x0000018, "-024", "%+04ld"}, + {__LINE__, -0x009f292, "-651922", "%ld"}, + {__LINE__, -0x0002da9, "-11689", "%-0ld"}, + {__LINE__, -0x000a218, "-41496", "%4.5d"}, + {__LINE__, 0xffff85ac, "FFFF85AC", "% 04.X"}, + {__LINE__, -0x0d9ffbe, "-14286782", "%0d"}, + {__LINE__, 0xfb18cc8e, "fb18cc8e", "%x"}, + {__LINE__, 0xffffe5d3, "FFFFE5D3", "%-X"}, + {__LINE__, 0x00000000, "0", "%x"}, + {__LINE__, -0x0000006, "-6", "% d"}, + {__LINE__, 0x0002fc62, "2fc62", "% 03.x"}, + {__LINE__, 0x00000000, "00", "%.2d"}, + {__LINE__, -0x00000fd, " -253", "%6ld"}, + {__LINE__, -0x000000b, "-11", "%d"}, + {__LINE__, -0x00042a3, "-17059", "% 6d"}, + {__LINE__, 0x00000002, "2", "% X"}, + {__LINE__, -0x61ead93e, "-1642780990", "%-0d"}, + {__LINE__, 0xf2e61f6d, "f2e61f6d", "%-.4x"}, + {__LINE__, 0xffd31b93, "ffd31b93", "%x"}, + {__LINE__, -0x0000008, "-8", "%ld"}, + {__LINE__, 0xffffffff, "ffffffff", "%00x"}, + {__LINE__, 0x000000fb, "+000251", "%+2.6d"}, + {__LINE__, 0x00000015, " 15", "%4.X"}, + {__LINE__, 0x06ec6d96, "6EC6D96", "%+2.X"}, + {__LINE__, 0xffff1df7, "0xffff1df7", "% #1x"}, + {__LINE__, -0x000004b, "-75", "%-ld"}, + {__LINE__, 0x00000008, "8", "%#.0d"}, + {__LINE__, 0x00001355, "4949", "%3.4ld"}, + {__LINE__, -0x00000f7, "-247", "%d"}, + {__LINE__, 0x0000001f, "31", "%.2ld"}, + {__LINE__, 0x0000d556, "D556", "%-X"}, + {__LINE__, 0xffffd5a5, "ffffd5a5", "% x"}, + {__LINE__, -0x0000e34, "-3636", "%#d"}, + {__LINE__, -0x0000007, "-7", "%d"}, + {__LINE__, 0x01ba10a4, "1ba10a4", "%x"}, + {__LINE__, 0xfffff759, "FFFFF759", "% X"}, + {__LINE__, -0x80d0adb, "-135072475", "%d"}, + {__LINE__, 0x0002bf61, "2bf61", "%x"}, + {__LINE__, 0xffff008b, "FFFF008B", "%-7X"}, + {__LINE__, 0x00000e72, "E72", "%X"}, + {__LINE__, -0x00000ea, "-234", "%0ld"}, + {__LINE__, -0x0007b9d, "-0031645", "%0.7ld"}, + {__LINE__, 0x0001baaa, "0X1BAAA", "%#4X"}, + {__LINE__, 0x0000a57f, " a57f", "%6.x"}, + {__LINE__, -0x21a73726, "-564606758", "%ld"}, + {__LINE__, 0x06f45c9f, "0x6f45c9f", "%#.3x"}, + {__LINE__, 0x00018e5d, "101981", "%-d"}, + {__LINE__, 0x0001e69a, "124570", "%-d"}, + {__LINE__, -0x0000d26, " -3366", "% 07.d"}, + {__LINE__, 0x00000000, "+", "%+0.ld"}, + {__LINE__, 0x00003916, "14614", "%ld"}, + {__LINE__, 0xfffffd8f, "fffffd8f", "%-3.x"}, + {__LINE__, 0x00000011, "11", "%x"}, + {__LINE__, 0x000000e7, "E7", "%X"}, + {__LINE__, 0xfdf9d89b, "FDF9D89B", "%X"}, + {__LINE__, 0x00000a7c, "a7c", "%x"}, + {__LINE__, 0x0000003f, "00003F", "%.6X"}, + {__LINE__, 0xffffe729, "FFFFE729", "%X"}, + {__LINE__, 0xfffffffe, "fffffffe", "% .6x"}, + {__LINE__, -0x0000a4b, "-2635", "%+d"}, + {__LINE__, 0xffffff50, "ffffff50", "%0.7x"}, + {__LINE__, 0xfaf57e8b, "faf57e8b", "% 4.x"}, + {__LINE__, -0x0000056, "-86", "%ld"}, + {__LINE__, 0x08ed2a4d, "8ed2a4d", "%4x"}, + {__LINE__, 0xff8d9081, "FF8D9081", "%+0.X"}, + {__LINE__, 0x002730a1, "2568353", "%4d"}, + {__LINE__, 0xffff9564, "ffff9564", "%x"}, + {__LINE__, -0x0000006, " -6", "%4ld"}, + {__LINE__, -0x19890310, "-428409616", "%0ld"}, + {__LINE__, 0xd2ce636c, "d2ce636c", "%+x"}, + {__LINE__, -0x00001fe, "-510", "%d"}, + {__LINE__, 0x00000006, "0000006", "%+0.7x"}, + {__LINE__, 0xffffffc2, "0XFFFFFFC2", "%-#.5X"}, + {__LINE__, -0x00000fd, "-253", "%+d"}, + {__LINE__, 0x000216aa, "0x216aa", "%#x"}, + {__LINE__, 0xffe36c66, "ffe36c66", "%x"}, + {__LINE__, 0x00000002, "2", "%x"}, + {__LINE__, -0x0000025, "-37", "% d"}, + {__LINE__, 0x0000007e, "126", "%d"}, + {__LINE__, 0x0003c1f9, "+246265", "%+ld"}, + {__LINE__, 0x00000001, "1", "%ld"}, + {__LINE__, -0x0000001, "-1", "%ld"}, + {__LINE__, -0x00539e7, "-342503", "%06.ld"}, + {__LINE__, 0x00000d42, " 3394", "%#6.ld"}, + {__LINE__, 0xf73b7c4e, "F73B7C4E", "%X"}, + {__LINE__, 0x00000022, "22", "%.1x"}, + {__LINE__, 0xffffa883, "FFFFA883", "%X"}, + {__LINE__, 0x016ec247, "16ec247", "%x"}, + {__LINE__, 0x00000019, "19", "%+X"}, + {__LINE__, 0x0000665a, "665A", "% 2.X"}, + {__LINE__, 0xfffffffc, "FFFFFFFC", "%-5.X"}, + {__LINE__, 0x008e3fbc, "8E3FBC", "%X"}, + {__LINE__, 0xfffffffa, "FFFFFFFA", "%X"}, + {__LINE__, 0x1b806597, "461399447", "%.2d"}, + {__LINE__, 0x00000366, "0000870", "%#1.7ld"}, + {__LINE__, 0x0001d92e, "1d92e", "%x"}, + {__LINE__, 0x000104ce, "104CE", "%-X"}, + {__LINE__, 0xfffffe4c, "fffffe4c", "%x"}, + {__LINE__, 0x000000e9, "E9", "%X"}, + {__LINE__, 0x000a855c, " 689500", "% d"}, + {__LINE__, 0x00000075, "75", "% X"}, + {__LINE__, -0x0000026, "-38", "%ld"}, + {__LINE__, 0xfffff169, "fffff169", "%-04.2x"}, + {__LINE__, 0xffffff6f, "ffffff6f", "%x"}, + {__LINE__, 0x00000003, "0x3", "%#x"}, + {__LINE__, 0x00623bab, "6437803", "%.0d"}, + {__LINE__, -0x2260224c, "-576725580", "%ld"}, + {__LINE__, 0xe7e7a998, "E7E7A998", "%.1X"}, + {__LINE__, 0x00000005, "5", "%X"}, + {__LINE__, 0x0000b560, "0xb560", "%#x"}, + {__LINE__, 0xfe11854d, "fe11854d", "%-.5x"}, + {__LINE__, 0x00190014, "1638420", "%ld"}, + {__LINE__, 0x0001d22a, "0X1D22A", "%-#X"}, + {__LINE__, 0x00000c16, "c16", "%2x"}, + {__LINE__, 0x00000d1a, "3354", "%#d"}, + {__LINE__, 0x000165f6, "91638", "%d"}, + {__LINE__, 0xfffff557, "0XFFFFF557", "%#X"}, + {__LINE__, 0x00000588, " 588", "%07.X"}, + {__LINE__, 0xf4a72708, "f4a72708", "%x"}, + {__LINE__, 0xfff1cba9, "0xfff1cba9", "% #2.5x"}, + {__LINE__, 0x000333a4, "209828", "%d"}, + {__LINE__, 0x0000179b, "179B", "% 2.X"}, + {__LINE__, 0x00000460, "1120", "%0ld"}, + {__LINE__, 0xffffff89, "FFFFFF89", "%X"}, + {__LINE__, -0x0000012, "-18", "%ld"}, + {__LINE__, 0x00004fe0, "20448", "%0.d"}, + {__LINE__, 0x00532e77, "532e77", "%00.x"}, + {__LINE__, 0xf7b92efe, "f7b92efe", "%0x"}, + {__LINE__, -0x0000243, "-579", "%+1.d"}, + {__LINE__, 0x34894bb7, "+881413047", "%+d"}, + {__LINE__, 0xfffaa189, "FFFAA189", "% 6X"}, + {__LINE__, -0x0000579, "-1401", "%+ld"}, + {__LINE__, 0x005acb12, "5acb12", "%5.0x"}, + {__LINE__, 0xffff890a, "FFFF890A", "%X"}, + {__LINE__, 0x010dff2f, "10DFF2F", "%.7X"}, + {__LINE__, 0x00005a7f, "23167", "%#ld"}, + {__LINE__, 0x000005e3, "1507", "%#3d"}, + {__LINE__, -0xa815bcb, "-176249803", "%d"}, + {__LINE__, 0xffffffff, "ffffffff", "%+x"}, + {__LINE__, 0xfffe0ff7, "fffe0ff7", "%3x"}, + {__LINE__, 0xffffffff, "0XFFFFFFFF", "%#2.1X"}, + {__LINE__, -0x1cc5ea52, "-482732626", "%6.d"}, + {__LINE__, 0x000425ed, "+0271853", "%+.7d"}, + {__LINE__, 0x00000001, "1", "%+x"}, + {__LINE__, -0x48f2a72, "-76491378", "%7.0d"}, + {__LINE__, 0xfffffff7, "FFFFFFF7", "% 0X"}, + {__LINE__, 0xfd8ffc0a, "fd8ffc0a", "%.1x"}, + {__LINE__, 0x0001f56d, " 1F56D", "%+7.2X"}, + {__LINE__, 0x000000db, " DB", "%6.X"}, + {__LINE__, 0xffffffc2, "ffffffc2", "%x"}, + {__LINE__, 0x00000001, "001", "%03ld"}, + {__LINE__, 0xffd89f58, "ffd89f58", "% 0.x"}, + {__LINE__, 0xfe1a72fc, "fe1a72fc", "%x"}, + {__LINE__, 0xfffff9b6, "FFFFF9B6", "%X"}, + {__LINE__, 0x00243633, "243633", "%X"}, + {__LINE__, 0x00821bc7, "+8526791", "%+#0ld"}, + {__LINE__, 0xfe63f348, "0XFE63F348", "%#3X"}, + {__LINE__, 0xff729f19, "ff729f19", "%4x"}, + {__LINE__, 0xffff570f, "FFFF570F", "%+X"}, + {__LINE__, 0x00a57a42, "10844738", "%6.3d"}, + {__LINE__, 0xfe4a3ed0, "FE4A3ED0", "%6.X"}, + {__LINE__, 0x00000000, "0", "%d"}, + {__LINE__, 0x000001bb, "01bb", "% .4x"}, + {__LINE__, 0x02b4a1e9, "2b4a1e9", "%-7.x"}, + {__LINE__, 0x31750683, "31750683", "%x"}, + {__LINE__, 0x00001cd7, "7383", "%#ld"}, + {__LINE__, 0x0000079d, "1949", "%d"}, + {__LINE__, 0xffb8ff3b, "FFB8FF3B", "%X"}, + {__LINE__, 0x00a768b6, "0A768B6", "%-6.7X"}, + {__LINE__, 0x1c665b48, "0X1C665B48", "% #X"}, + {__LINE__, -0x000037d, "-000893", "%3.6d"}, + {__LINE__, 0x29890443, "29890443", "%+0x"}, + {__LINE__, 0x000000e5, "e5 ", "%-4x"}, + {__LINE__, 0xfff2b5fa, "FFF2B5FA", "%X"}, + {__LINE__, -0x5d32c2b, "-97725483", "%05ld"}, + {__LINE__, -0x00016c5, " -05829", "%+#7.5ld"}, + {__LINE__, 0x00000644, "644", "%X"}, + {__LINE__, -0x018c027, "-1622055", "%+.1d"}, + {__LINE__, 0x0000013c, "0x13c", "%#x"}, + {__LINE__, 0x00000009, "+9", "%+#ld"}, + {__LINE__, 0x00000000, "0", "%ld"}, + {__LINE__, -0x018eb19, "-1633049", "%4.3ld"}, + {__LINE__, 0xfffff429, "FFFFF429", "% X"}, + {__LINE__, 0x0032de67, "32DE67", "%X"}, + {__LINE__, 0x0016bdfa, " 1490426", "% .2ld"}, + {__LINE__, 0x00000065, "65", "%+.0X"}, + {__LINE__, 0x00000072, " 72", "%+4.2X"}, + {__LINE__, 0x00075119, "75119", "%2x"}, + {__LINE__, 0x000000b8, "b8", "%x"}, + {__LINE__, 0x000000d0, "d0", "%x"}, + {__LINE__, 0x000000b9, "b9", "%x"}, + {__LINE__, 0x003fda9d, " 4184733", "% #3.d"}, + {__LINE__, 0x00029a24, "170532", "%4.d"}, + {__LINE__, -0x132a337d, "-321532797", "% ld"}, + {__LINE__, 0x0000047a, "0X47A", "%-#X"}, + {__LINE__, 0x00000000, "0", "%-ld"}, + {__LINE__, -0x0d9750b, "-14251275", "% ld"}, + {__LINE__, 0x00029d6c, "29d6c", "%x"}, + {__LINE__, 0xfb060cc0, "fb060cc0", "%x"}, + {__LINE__, 0xffffd524, "0XFFFFD524", "%#X"}, + {__LINE__, 0xffffff30, "ffffff30", "%-5x"}, + {__LINE__, 0x00001650, "0005712", "%#0.7d"}, + {__LINE__, 0x0000b702, "0B702", "%03.5X"}, + {__LINE__, 0x000003c1, "3c1", "% .1x"}, + {__LINE__, -0x0290412, "-2688018", "%d"}, + {__LINE__, -0x00008bf, "-2239", "%#d"}, + {__LINE__, -0x0000001, "-1", "%d"}, + {__LINE__, 0x0001d750, "1D750", "%-X"}, + {__LINE__, 0x006eee92, "6EEE92", "%-X"}, + {__LINE__, 0xff812b02, "ff812b02", "%0x"}, + {__LINE__, 0xfffffeac, "FFFFFEAC", "%+0X"}, + {__LINE__, 0x000003a5, "3a5", "%x"}, + {__LINE__, -0x15270cee, "-354880750", "%2.ld"}, + {__LINE__, 0x06b686d2, "6b686d2", "%0x"}, + {__LINE__, 0xffe9cdcc, "ffe9cdcc", "%x"}, + {__LINE__, 0x0ecb168e, "ecb168e", "%x"}, + {__LINE__, 0x00000051, "51", "%-X"}, + {__LINE__, -0x0018860, "-100448", "%-0d"}, + {__LINE__, 0x8f08b82b, "0x8f08b82b", "%-#.5x"}, + {__LINE__, 0xffffffff, "ffffffff", "%x"}, + {__LINE__, 0x0000000e, "e", "%+01x"}, + {__LINE__, 0x04cf15bc, " 80680380", "% #ld"}, + {__LINE__, 0x50006e8a, "50006e8a", "%x"}, + {__LINE__, 0x0002f669, "194153", "%0d"}, + {__LINE__, 0x000000a6, " 166", "% 5.ld"}, + {__LINE__, -0x04036bb, "-4208315", "%7.5d"}, + {__LINE__, 0x00070dea, "70DEA", "%+X"}, + {__LINE__, 0x00000964, "0x964", "%#x"}, + {__LINE__, 0x0000004d, "0x4d", "% #3.x"}, + {__LINE__, -0x0000001, "-1", "%-0d"}, + {__LINE__, 0x0000043e, "1086", "%ld"}, + {__LINE__, -0x0000001, "-1", "%-d"}, + {__LINE__, 0x00000000, "+0", "%+d"}, + {__LINE__, 0xfffe68d9, "FFFE68D9", "%7.X"}, + {__LINE__, -0x0169199, "-1479065", "%#.5ld"}, + {__LINE__, -0x000003d, "-61", "%ld"}, + {__LINE__, 0x00000001, "+1", "%+.1d"}, + {__LINE__, 0x0001a65d, "1a65d", "%.5x"}, + {__LINE__, 0x00000141, "+000321", "%+#5.6ld"}, + {__LINE__, 0x00000000, " 0", "%4x"}, + {__LINE__, 0x00000408, "1032", "%-2.3ld"}, + {__LINE__, 0xffffffed, "FFFFFFED", "%-X"}, + {__LINE__, 0xfe832351, "FE832351", "% .4X"}, + {__LINE__, 0x003a421b, "3A421B", "%-X"}, + {__LINE__, -0x53ed7f25, "-1408073509", "%ld"}, + {__LINE__, -0x0000003, "-0003", "%-5.4ld"}, + {__LINE__, 0x00000ad0, " 2768", "%7d"}, + {__LINE__, 0xfffaf30b, "FFFAF30B", "%X"}, + {__LINE__, 0x006349d3, " 6506963", "% 6.2ld"}, + {__LINE__, 0x07f0146a, "7f0146a", "%+3.4x"}, + {__LINE__, -0x0000002, " -2", "% 3.ld"}, + {__LINE__, -0x26e94f3, "-40801523", "%-#ld"}, + {__LINE__, -0x0000004, " -004", "%7.3d"}, + {__LINE__, 0xfffe8cc9, "FFFE8CC9", "%0X"}, + {__LINE__, 0x00000018, " 18", "%5.x"}, + {__LINE__, -0x4941de83, "-1229053571", "% 0.5ld"}, + {__LINE__, 0x00000000, " 0", "%6ld"}, + {__LINE__, 0xfffffffe, "0XFFFFFFFE", "%-#X"}, + {__LINE__, -0x0142ae1, "-1321697", "%4.ld"}, + {__LINE__, -0x0319e7f, "-3251839", "%0ld"}, + {__LINE__, 0x000004ff, " 4FF", "%7.2X"}, + {__LINE__, -0x0001c3e, "-7230", "%#.2ld"}, + {__LINE__, 0xffffe522, "ffffe522", "%x"}, + {__LINE__, 0x0001d1cb, "+119243", "%+.3ld"}, + {__LINE__, 0xfad19d52, "FAD19D52", "%+X"}, + {__LINE__, 0x00002a7c, " 10876", "% 0.ld"}, + {__LINE__, 0x00001449, "1449", "%X"}, + {__LINE__, 0x000732b8, "471736", "%d"}, + {__LINE__, -0x211a496e, "-555370862", "%ld"}, + {__LINE__, -0x0000004, "-4", "%1.d"}, + {__LINE__, 0x0295c03d, "43368509", "%5.ld"}, + {__LINE__, 0x01fa5722, "33183522", "%0ld"}, + {__LINE__, 0xfffbff62, "0xfffbff62", "%#x"}, + {__LINE__, 0x000000f5, " 245", "% 2.d"}, + {__LINE__, 0x000000a0, "160", "%-d"}, + {__LINE__, 0x0000000f, "15", "%.2ld"}, + {__LINE__, 0x0bf5f077, "+200667255", "%+ld"}, + {__LINE__, -0x14d3e5a, "-21839450", "%#2.3d"}, + {__LINE__, -0x21cbe656, "-567010902", "%ld"}, + {__LINE__, 0x000002b9, "697", "%ld"}, + {__LINE__, 0xfffff9b0, "FFFFF9B0", "%X"}, + {__LINE__, 0x007fd9c7, "8378823", "%3.4d"}, + {__LINE__, 0x00036d06, "36d06", "%x"}, + {__LINE__, 0xffde8052, "FFDE8052", "%4.6X"}, + {__LINE__, 0x0000000d, "13", "%#d"}, + {__LINE__, 0x68157d93, "68157d93", "%x"}, + {__LINE__, 0x00000005, "5", "%1x"}, + {__LINE__, 0xfffffff4, "FFFFFFF4", "%X"}, + {__LINE__, 0xf5bbf7e9, "f5bbf7e9", "%+.1x"}, + {__LINE__, 0x0be98f98, "BE98F98", "%X"}, + {__LINE__, 0x00000122, "00122", "%.5X"}, + {__LINE__, -0x0000936, "-2358", "%+#3.0ld"}, + {__LINE__, 0xfd230f09, "FD230F09", "%-7X"}, + {__LINE__, 0x00000089, "137", "%ld"}, + {__LINE__, 0x00000078, " 120", "%7d"}, + {__LINE__, -0x68ef2b6b, "-1760504683", "%-ld"}, + {__LINE__, 0xc24480bb, "C24480BB", "%+4X"}, + {__LINE__, 0xfefe36a2, "FEFE36A2", "%0X"}, + {__LINE__, -0x35344ba, "-55788730", "%7.d"}, + {__LINE__, 0xffffffff, "FFFFFFFF", "%+.6X"}, + {__LINE__, -0x0000f40, "-3904", "% ld"}, + {__LINE__, 0xfffffffb, "FFFFFFFB", "%X"}, + {__LINE__, 0x58b37c4b, "58b37c4b", "%2.2x"}, + {__LINE__, 0x000000e2, "226", "%#ld"}, + {__LINE__, 0xfff5ebde, "fff5ebde", "%-7.1x"}, + {__LINE__, 0x0c6ea9be, "208579006", "%-ld"}, + {__LINE__, 0xff708c74, "FF708C74", "% 4.X"}, + {__LINE__, 0x00000000, " 0", "% 3X"}, + {__LINE__, 0x00000004, " 4", "%03.d"}, + {__LINE__, 0x0919be7c, "+152682108", "%+6.d"}, + {__LINE__, 0x00d4fd1e, "13958430", "%0d"}, + {__LINE__, 0xfe77a69e, "fe77a69e", "%-7.x"}, + {__LINE__, 0x284ded44, "676195652", "%-#2ld"}, + {__LINE__, -0x00006e6, "-1766", "% ld"}, + {__LINE__, -0x7dac7ea, "-131778538", "%.0ld"}, + {__LINE__, 0x0003c50f, "247055", "%#d"}, + {__LINE__, 0xfffffffc, "fffffffc", "%-x"}, + {__LINE__, 0xfe0f0d1f, "fe0f0d1f", "% 5.1x"}, + {__LINE__, 0x00000009, "9", "%0ld"}, + {__LINE__, -0x0175cc1, "-1531073", "%-04.d"}, + {__LINE__, 0x000e4da2, " 937378", "% 4.ld"}, + {__LINE__, 0x00000762, "762", "%-0.X"}, + {__LINE__, 0x005211bf, "5211bf", "%-x"}, + {__LINE__, 0xfffffffb, "fffffffb", "%+2.6x"}, + {__LINE__, 0x00008862, "34914", "%ld"}, + {__LINE__, 0xfffc9754, "fffc9754", "%+x"}, + {__LINE__, 0x0000716e, "716E", "%X"}, + {__LINE__, 0x000000a8, "a8", "%x"}, + {__LINE__, 0xffff84f5, "ffff84f5", "%0x"}, + {__LINE__, 0x00005032, "5032", "%x"}, + {__LINE__, 0xffe242b4, "ffe242b4", "%6x"}, + {__LINE__, 0x002df9bd, "3013053", "%ld"}, + {__LINE__, 0xffffe267, "FFFFE267", "%.5X"}, + {__LINE__, 0xffffffc7, "0xffffffc7", "%#x"}, + {__LINE__, -0x1727c38, "-24280120", "%d"}, + {__LINE__, 0x01308072, "1308072", "%x"}, + {__LINE__, -0x5883c7c, "-92814460", "%+ld"}, + {__LINE__, 0x000030ad, "30ad", "%x"}, + {__LINE__, 0xffffffff, "FFFFFFFF", "%X"}, + {__LINE__, -0x2090a708, "-546350856", "% d"}, + {__LINE__, 0x0024e6b4, "2418356", "%#ld"}, + {__LINE__, 0x02db71ac, "2DB71AC", "% 6.3X"}, + {__LINE__, 0xfffffffc, "FFFFFFFC", "%2.7X"}, + {__LINE__, -0x000074e, " -1870", "%7.ld"}, + {__LINE__, -0x0011d84, "-73092", "%d"}, + {__LINE__, 0x00160723, "160723", "%1.3x"}, + {__LINE__, 0x00412929, "4270377", "%-d"}, + {__LINE__, -0x0008fc6, "-36806", "% d"}, + {__LINE__, 0x00000002, "2", "%d"}, + {__LINE__, 0x0eb67ffa, "eb67ffa", "%.4x"}, + {__LINE__, 0x05b86c3c, "95972412", "%4.4d"}, + {__LINE__, 0xffffca9c, "FFFFCA9C", "% 4.X"}, + {__LINE__, 0x0007d571, "7d571", "%-0x"}, + {__LINE__, -0x000239b, "-09115", "%5.5d"}, + {__LINE__, -0x0000155, " -341", "%5.3ld"}, + {__LINE__, 0x00000034, "34", "%x"}, + {__LINE__, -0x0000051, "-81", "%.1ld"}, + {__LINE__, 0x309bbbe0, "309bbbe0", "%x"}, + {__LINE__, 0x0075be1e, " 7716382", "% #ld"}, + {__LINE__, 0x000002d5, "2d5", "%x"}, + {__LINE__, 0x006b466d, "6B466D", "%-0X"}, + {__LINE__, 0x01d6c30f, "1D6C30F", "%4.X"}, + {__LINE__, 0x00000003, " 3", "%07.x"}, + {__LINE__, 0xfffcc102, "FFFCC102", "%6.X"}, + {__LINE__, 0xffffffff, "0XFFFFFFFF", "%#X"}, + {__LINE__, 0xfffffffe, "fffffffe", "%+x"}, + {__LINE__, 0x00000005, "5", "%ld"}, + {__LINE__, 0xff060284, "ff060284", "%x"}, + {__LINE__, 0x007a3325, "7A3325", "%-X"}, + {__LINE__, 0xf5f595bd, "F5F595BD", "%0X"}, + {__LINE__, 0xfffa3a10, "FFFA3A10", "%0X"}, + {__LINE__, 0x00000000, "0", "%x"}, + {__LINE__, -0x15078f9, "-22051065", "%7d"}, + {__LINE__, -0x0000002, "-000002", "%.6d"}, + {__LINE__, 0x00000036, "54", "%1.1ld"}, + {__LINE__, -0xbbecf6a, "-197054314", "%ld"}, + {__LINE__, 0x0000043d, "43D", "%X"}, + {__LINE__, 0xfffffffa, "0xfffffffa", "% #6.x"}, + {__LINE__, 0x016759f8, "23550456", "%0.ld"}, + {__LINE__, 0x000052d2, "+21202", "%+6.2d"}, + {__LINE__, -0x007d232, "-512562", "%ld"}, + {__LINE__, 0x00240be0, "2362336", "%.1d"}, + {__LINE__, -0x0000001, "-1", "%0d"}, + {__LINE__, 0xfffffffa, "FFFFFFFA", "%X"}, + {__LINE__, -0x0000e88, "-3720", "%ld"}, + {__LINE__, 0xfffffa9b, "FFFFFA9B", "%.5X"}, + {__LINE__, 0x09296eeb, "153710315", "%-1.0d"}, + {__LINE__, 0x001f46fc, "0X1F46FC", "%#X"}, + {__LINE__, 0x00000006, "6", "%X"}, + {__LINE__, 0xffffff87, "0XFFFFFF87", "%+#X"}, + {__LINE__, 0x00000469, "0469", "%04X"}, + {__LINE__, -0x0000001, "-1", "%ld"}, + {__LINE__, 0x00000000, "", "%.0ld"}, + {__LINE__, -0x52bc137, "-86753591", "%-#3ld"}, + {__LINE__, -0x2bddb08, "-45996808", "%-3.d"}, + {__LINE__, 0x041367f7, "41367F7", "%+X"}, + {__LINE__, -0x0018eb5, "-102069", "%+ld"}, + {__LINE__, -0x0000537, "-1335", "%#ld"}, + {__LINE__, -0x6f159ff4, "-1863688180", "%+1.d"}, + {__LINE__, 0x00112ac8, "0X112AC8", "%#6.3X"}, + {__LINE__, 0x00000000, " ", "%3.d"}, + {__LINE__, 0x0008afe8, "08afe8", "%+3.6x"}, + {__LINE__, 0x00000015, "21", "%ld"}, + {__LINE__, -0x02b7130, "-2847024", "% #2.4d"}, + {__LINE__, 0xffffffd0, "FFFFFFD0", "%X"}, + {__LINE__, -0x6970fa1, "-110563233", "%+#.4ld"}, + {__LINE__, 0x06387dcc, "0X6387DCC", "%-#X"}, + {__LINE__, 0xffffd506, "ffffd506", "%+6x"}, + {__LINE__, -0x00fe5cd, "-1041869", "%-#7d"}, + {__LINE__, -0x0c35bf6, "-12803062", "%-03d"}, + {__LINE__, -0x0020d89, "-134537", "%#d"}, + {__LINE__, -0x0c7aed3, "-13086419", "%-ld"}, + {__LINE__, -0x000362a, "-13866", "%#.4ld"}, + {__LINE__, -0x02d05f0, "-2950640", "%#ld"}, + {__LINE__, 0x02de1321, "0x2de1321", "%#7.x"}, + {__LINE__, 0xffb56428, "0xffb56428", "%#x"}, + {__LINE__, 0xfffffffe, "FFFFFFFE", "%X"}, + {__LINE__, 0xf072292d, "F072292D", "%.5X"}, + {__LINE__, -0x0f4b0f2, "-16036082", "%.5ld"}, + {__LINE__, 0x01b81885, "28842117", "%5d"}, + {__LINE__, -0x009cfa6, "-642982", "%d"}, + {__LINE__, 0xfffffffd, "0XFFFFFFFD", "%-#.5X"}, + {__LINE__, 0x0dc97a66, "DC97A66", "%2.X"}, + {__LINE__, 0x000000be, "190", "%.0d"}, + {__LINE__, 0xffffe1c7, "FFFFE1C7", "% X"}, + {__LINE__, 0x01883b9a, "1883b9a", "%3x"}, + {__LINE__, 0xffffdde1, "ffffdde1", "%+x"}, + {__LINE__, 0x2f1b4e32, "+790318642", "%+ld"}, + {__LINE__, 0x000129aa, "129aa", "%x"}, + {__LINE__, -0x00092c2, "-37570", "%d"}, + {__LINE__, 0x00070fbd, "70fbd", "%x"}, + {__LINE__, -0x00000ea, "-234", "%+4ld"}, + {__LINE__, 0xfffffffe, "fffffffe", "%x"}, + {__LINE__, 0x0091c7f7, "91C7F7", "% .3X"}, + {__LINE__, -0x0c14fe2, "-12668898", "%d"}, + {__LINE__, -0x0017c15, "-97301", "%d"}, + {__LINE__, -0x00005d0, "-0001488", "%5.7d"}, + {__LINE__, 0x0114c36e, "18137966", "%0d"}, + {__LINE__, 0xffffffff, "ffffffff", "%+x"}, + {__LINE__, 0x0ac76e78, "180842104", "%d"}, + {__LINE__, 0x001976cd, " 1668813", "% 4.ld"}, + {__LINE__, -0x0642319, "-6562585", "% d"}, + {__LINE__, 0xffffffff, "FFFFFFFF", "%X"}, + {__LINE__, -0x035019d, "-3473821", "%.7d"}, + {__LINE__, -0x0000061, "-97", "%#3.d"}, + {__LINE__, 0x0000001f, " 31", "% 6.1ld"}, + {__LINE__, -0x0000024, "-36", "%d"}, + {__LINE__, 0x000b3785, "735109", "%ld"}, + {__LINE__, 0xfffffda9, "fffffda9", "%-x"}, + {__LINE__, 0x00254832, "2443314", "%-ld"}, + {__LINE__, 0xfffffd40, "FFFFFD40", "%0X"}, + {__LINE__, -0x0006105, "-24837", "%+#d"}, + {__LINE__, 0x00000006, "+6", "%+d"}, + {__LINE__, 0xffd40fd8, "ffd40fd8", "%3.x"}, + {__LINE__, -0x000988e, "-39054", "%03d"}, + {__LINE__, 0x0005ad8b, "5AD8B", "%0.0X"}, + {__LINE__, 0x00000001, "1", "%d"}, + {__LINE__, 0xfe1f59b7, "fe1f59b7", "%4.x"}, + {__LINE__, -0x0000022, "-34", "%#d"}, + {__LINE__, 0x0018e3e9, "18E3E9", "% X"}, + {__LINE__, 0xffffff25, "FFFFFF25", "%2.X"}, + {__LINE__, -0x00002c6, "-710", "%1.ld"}, + {__LINE__, 0x00009765, "38757", "%5ld"}, + {__LINE__, 0xfe197646, "FE197646", "%-X"}, + {__LINE__, 0x00000001, " 1", "%+5.x"}, + {__LINE__, -0x0000002, "-2", "%#ld"}, + {__LINE__, 0x0072f6be, "72F6BE", "%1X"}, + {__LINE__, -0x000f06d, "-61549", "%-d"}, + {__LINE__, 0x00000001, " 1", "%5X"}, + {__LINE__, 0x328cba28, " 848083496", "% 2.ld"}, + {__LINE__, 0x0000cddc, "cddc", "%.1x"}, + {__LINE__, 0xfffffff9, "fffffff9", "%7x"}, + {__LINE__, 0x00baf511, "BAF511", "%-5.6X"}, + {__LINE__, 0x00000001, " 1", "%4.ld"}, + {__LINE__, 0x0001e4b0, "+124080", "%+ld"}, + {__LINE__, -0x07548f3, "-7686387", "% 6.d"}, + {__LINE__, -0x01b14ea, "-1774826", "% #0d"}, + {__LINE__, -0x000005c, "-92 ", "%-04ld"}, + {__LINE__, -0x00000ae, "-174", "%+.3ld"}, + {__LINE__, 0x0000008c, "0X8C", "%+#X"}, + {__LINE__, 0x14c02360, "14C02360", "%.6X"}, + {__LINE__, 0xfffffff6, "FFFFFFF6", "%X"}, + {__LINE__, 0x009f4ee5, "9f4ee5", "%x"}, + {__LINE__, 0x00000035, "35", "%X"}, + {__LINE__, -0x00000eb, "-235", "%ld"}, + {__LINE__, 0x1552ced8, "1552CED8", "%04X"}, + {__LINE__, 0x0000002a, " 2A", "%5X"}, + {__LINE__, 0xffffffff, "ffffffff", "% x"}, + {__LINE__, -0xdec0db2, "-233573810", "%d"}, + {__LINE__, 0x001e2ef0, "1978096", "%2.d"}, + {__LINE__, 0xffffef24, "ffffef24", "%.5x"}, + {__LINE__, 0xfffe6a6c, "fffe6a6c", "%+x"}, + {__LINE__, -0x0000004, "-4", "% ld"}, + {__LINE__, 0xfe3c6743, "fe3c6743", "%.1x"}, + {__LINE__, 0x0000033d, "829", "%0ld"}, + {__LINE__, -0x00006a0, " -1696", "% 7.ld"}, + {__LINE__, 0xffffffdb, "ffffffdb", "%0.0x"}, + {__LINE__, 0xfffffffc, "FFFFFFFC", "%3.3X"}, + {__LINE__, -0x0000004, "-4", "%#ld"}, + {__LINE__, 0x00b32b56, "B32B56", "%X"}, + {__LINE__, 0xffffffff, "0xffffffff", "%+#x"}, + {__LINE__, 0x01cd1a48, "1cd1a48", "%+7x"}, + {__LINE__, 0xffffffda, "ffffffda", "%x"}, + {__LINE__, 0x1886509e, "1886509E", "% X"}, + {__LINE__, 0x000000eb, "235", "%0d"}, + {__LINE__, 0xfffffff4, "fffffff4", "%0x"}, + {__LINE__, 0x00000547, "547", "% X"}, + {__LINE__, 0x000017e6, "17e6", "%-2.x"}, + {__LINE__, 0x00001dc4, "7620", "%d"}, + {__LINE__, 0xffffffff, "FFFFFFFF", "%3.0X"}, + {__LINE__, 0xffff93fa, "0XFFFF93FA", "%#X"}, + {__LINE__, 0xfffffe1c, "FFFFFE1C", "%-X"}, + {__LINE__, 0x00000000, "", "% .0x"}, + {__LINE__, 0x000000a1, "A1", "%X"}, + {__LINE__, 0xffffffff, "0xffffffff", "%#x"}, + {__LINE__, -0x0001460, "-05216", "%+#.5d"}, + {__LINE__, 0x00000001, "1 ", "%-6.x"}, + {__LINE__, 0xfffd5324, "FFFD5324", "%+2.6X"}, + {__LINE__, 0x00000007, " 07", "%06.2d"}, + {__LINE__, 0x00000003, "3", "%-X"}, + {__LINE__, 0xfffffdb7, "0XFFFFFDB7", "%+#X"}, + {__LINE__, 0xfffff4cc, "fffff4cc", "%0x"}, + {__LINE__, 0x00000000, "0 ", "%-5X"}, + {__LINE__, 0x00758c96, "758C96", "%X"}, + {__LINE__, -0x0000001, "-1", "%-ld"}, + {__LINE__, 0x00000001, "1", "%ld"}, + {__LINE__, 0x03f03caf, "66075823", "%.2ld"}, + {__LINE__, 0x0000a87c, "a87c", "%1.x"}, + {__LINE__, 0x00000034, "34", "%X"}, + {__LINE__, 0x00000000, "0", "%x"}, + {__LINE__, 0x00010167, "10167", "%x"}, + {__LINE__, -0x00007ce, "-1998 ", "%-6ld"}, + {__LINE__, 0x00000001, "1", "%.0d"}, + {__LINE__, 0x00d94cee, "14241006", "%ld"}, + {__LINE__, -0x0000008, "-8", "% d"}, + {__LINE__, -0x71520839, "-1901201465", "%+0d"}, + {__LINE__, 0x0000035a, "35a", "%-x"}, + {__LINE__, 0xfffd2f68, "FFFD2F68", "%-X"}, + {__LINE__, 0x0006ea53, "453203", "%1d"}, + {__LINE__, 0x000000df, "DF", "% 1X"}, + {__LINE__, 0xfffffffe, "fffffffe", "% 7x"}, + {__LINE__, 0x00000156, "156", "% X"}, + {__LINE__, 0x00037ac9, " 228041", "% ld"}, + {__LINE__, 0xffffffde, "FFFFFFDE", "%0.5X"}, + {__LINE__, 0x00000025, "37", "%-0.ld"}, + {__LINE__, -0x4133686, "-68368006", "%2ld"}, + {__LINE__, -0x000003b, "-59", "%.2ld"}, + {__LINE__, 0xfffff910, "fffff910", "% 00.0x"}, + {__LINE__, 0xff9ec802, "FF9EC802", "%2.6X"}, + {__LINE__, 0x00000008, "0X8", "%#1X"}, + {__LINE__, 0xfffffff7, "FFFFFFF7", "%-X"}, + {__LINE__, 0x0029a4da, "2729178", "%04.ld"}, + {__LINE__, 0x007d1588, "8197512", "%#d"}, + {__LINE__, 0x0007e86d, "518253", "%0d"}, + {__LINE__, 0xfffffff9, "fffffff9", "%x"}, + {__LINE__, 0x00000002, "002", "%.3d"}, + {__LINE__, 0x00006ec1, "6EC1", "%2X"}, + {__LINE__, 0x0000300c, "12300", "%-d"}, + {__LINE__, -0x0000002, "-2", "%+ld"}, + {__LINE__, -0x0000002, "-2", "%ld"}, + {__LINE__, -0x00015d5, "-005589", "%#.6ld"}, + {__LINE__, 0x00000041, "65", "%d"}, + {__LINE__, 0x00000229, "0000229", "% 0.7x"}, + {__LINE__, 0xfffffffb, "fffffffb", "%03.x"}, + {__LINE__, 0x00093262, "0602722", "%.7d"}, + {__LINE__, -0x00037b4, "-14260", "%-0ld"}, + {__LINE__, 0xfffffffc, "fffffffc", "%-x"}, + {__LINE__, 0xf24cb3e6, "f24cb3e6", "%1x"}, + {__LINE__, 0x00000000, "0", "%x"}, + {__LINE__, 0xfffffffb, "FFFFFFFB", "%0X"}, + {__LINE__, 0x00000031, "0000049", "%00.7ld"}, + {__LINE__, 0x00000bb3, "BB3", "%.1X"}, + {__LINE__, -0x0000001, "-0001", "%+#4.4ld"}, + {__LINE__, -0x0000001, "-1", "% d"}, + {__LINE__, 0x00022c0c, "22c0c", "%-x"}, + {__LINE__, 0xffffffd4, "ffffffd4", "%x"}, + {__LINE__, 0x000729c6, "469446", "%3.d"}, + {__LINE__, 0xb180feae, "b180feae", "%3.x"}, + {__LINE__, 0x124fac15, "307211285", "%-0ld"}, + {__LINE__, -0x0000004, "-4", "%ld"}, + {__LINE__, 0x0000006a, "106", "%#d"}, + {__LINE__, 0x0000297f, "297F", "%+3X"}, + {__LINE__, 0x0000000e, "14", "%0ld"}, + {__LINE__, 0x00000027, "27", "%0.1X"}, + {__LINE__, 0xffeb98eb, "ffeb98eb", "% 05.2x"}, + {__LINE__, 0xfffff19b, "FFFFF19B", "%+X"}, + {__LINE__, 0x00025992, "154002", "%6.6d"}, + {__LINE__, 0x00000040, " 040", "%6.3X"}, + {__LINE__, 0x00000a9e, " 0a9e", "%5.4x"}, + {__LINE__, 0x00c7f2cc, "c7f2cc", "%x"}, + {__LINE__, 0x000325e6, " 325E6", "%6.X"}, + {__LINE__, -0x69faad3, "-111127251", "%d"}, + {__LINE__, -0x059a307, "-5874439", "% ld"}, + {__LINE__, 0xfffffff6, "0xfffffff6", "%#0.0x"}, + {__LINE__, 0x0030fdf3, "30fdf3", "%x"}, + {__LINE__, 0x00007343, "+29507", "%+#2d"}, + {__LINE__, 0x0003cf4b, "0X3CF4B", "% #.2X"}, + {__LINE__, 0x00000433, "+1075", "%+0ld"}, + {__LINE__, 0xfffffffd, "fffffffd", "%+.3x"}, + {__LINE__, 0x0ae30c4e, "ae30c4e", "%+00x"}, + {__LINE__, 0x0002540f, "02540f", "%-.6x"}, + {__LINE__, -0x0000001, " -01", "% #5.2ld"}, + {__LINE__, 0xffffffe7, "0xffffffe7", "% #5.x"}, + {__LINE__, 0x0000005b, "91", "%d"}, + {__LINE__, 0x00001f9b, "8091", "%.4ld"}, + {__LINE__, 0xfffff315, "fffff315", "%4.x"}, + {__LINE__, -0x130eec41, "-319745089", "% ld"}, + {__LINE__, 0xfff8fe13, "FFF8FE13", "%X"}, + {__LINE__, -0x0000004, "-00004", "%5.5d"}, + {__LINE__, 0x00000669, "669", "%0X"}, + {__LINE__, -0x0000004, "-4", "%d"}, + {__LINE__, 0xf5e81496, "F5E81496", "% 6X"}, + {__LINE__, -0x0000001, "-1", "% 0d"}, + {__LINE__, 0xfffffff7, "fffffff7", "%x"}, + {__LINE__, 0x000001de, "478", "%d"}, + {__LINE__, 0x0000623e, "25150", "%5.0d"}, + {__LINE__, 0xffffffa8, "FFFFFFA8", "%+X"}, + {__LINE__, -0x0000117, "-279", "% ld"}, + {__LINE__, -0x0000517, "-1303", "%d"}, + {__LINE__, 0xfffff9d5, "fffff9d5", "% x"}, + {__LINE__, 0x000001cc, "1CC", "%-X"}, + {__LINE__, 0x0000019a, "0000410", "%.7d"}, + {__LINE__, 0x00000003, "3 ", "%-06X"}, + {__LINE__, 0x00000009, "0x9", "%#0x"}, + {__LINE__, 0xfd544610, "0XFD544610", "% #X"}, + {__LINE__, 0xfffffffc, "fffffffc", "%+x"}, + {__LINE__, 0x00009437, "37943", "%ld"}, + {__LINE__, -0x00b5ea6, "-745126", "%d"}, + {__LINE__, 0xffffffd8, "ffffffd8", "%6x"}, + {__LINE__, 0x00002a2d, "10797", "%.3ld"}, + {__LINE__, 0x00275238, "2576952", "%0ld"}, + {__LINE__, 0xfff90a34, "FFF90A34", "%-7X"}, + {__LINE__, 0xfffffffe, "FFFFFFFE", "% .7X"}, + {__LINE__, -0xb3b673e, "-188442430", "% ld"}, + {__LINE__, 0xfffff67a, "0xfffff67a", "%#0.x"}, + {__LINE__, 0x0000dff6, " dff6", "%+7.x"}, + {__LINE__, 0xffee46e3, "ffee46e3", "%x"}, + {__LINE__, 0x00001a66, " 0006758", "% 7.7d"}, + {__LINE__, 0x0002b475, "2B475", "%.4X"}, + {__LINE__, 0x0002090d, "002090d", "%.7x"}, + {__LINE__, -0x0057838, "-358456", "%#ld"}, + {__LINE__, 0x00000004, "0x4", "%#x"}, + {__LINE__, 0x0574cb62, "91540322", "%#7d"}, + {__LINE__, -0x0054388, "-344968", "% #d"}, + {__LINE__, 0x000421c2, "421C2", "% X"}, + {__LINE__, 0x00000003, "3", "%0d"}, + {__LINE__, 0xffffe2e2, "ffffe2e2", "% 5.x"}, + {__LINE__, 0x00000021, "+33", "%+d"}, + {__LINE__, -0x0020033, "-131123", "%ld"}, + {__LINE__, -0x0000001, " -1", "%4.ld"}, + {__LINE__, 0x000000fd, "000fd", "%3.5x"}, + {__LINE__, 0x023e4337, "37634871", "%1d"}, + {__LINE__, 0x6e823d96, "6E823D96", "% X"}, + {__LINE__, 0xfff0e133, "fff0e133", "%x"}, + {__LINE__, -0x000decc, "-57036", "%3d"}, + {__LINE__, 0x004397b8, "4429752", "%0d"}, + {__LINE__, -0x0000023, " -35", "%6d"}, + {__LINE__, 0xffe701ca, "FFE701CA", "%3X"}, + {__LINE__, 0x000c0319, "+787225", "%+.6d"}, + {__LINE__, 0x00000000, "0", "%x"}, + {__LINE__, 0xfffffe1d, "FFFFFE1D", "%X"}, + {__LINE__, 0xfffffb33, "fffffb33", "%x"}, + {__LINE__, 0x02b5e30a, "45474570", "%d"}, + {__LINE__, 0x000074eb, "29931", "%03.ld"}, + {__LINE__, -0x4e42e310, "-1313006352", "%-0.d"}, + {__LINE__, -0x000007b, "-123", "%d"}, + {__LINE__, 0x00000160, " 352", "% d"}, + {__LINE__, -0x16af31ec, "-380580332", "%#ld"}, + {__LINE__, -0x000006e, "-000110", "%07ld"}, + {__LINE__, 0x232699a0, "232699a0", "%7.x"}, + {__LINE__, 0x00f9b8e5, "+16365797", "%+0d"}, + {__LINE__, 0x0000000c, "C", "%X"}, + {__LINE__, 0xfffffd4f, "FFFFFD4F", "%X"}, + {__LINE__, 0xffffe7bb, "FFFFE7BB", "%.7X"}, + {__LINE__, -0x0000163, "-355", "%d"}, + {__LINE__, 0x00070315, " 459541", "% #2.d"}, + {__LINE__, -0x7d5c12a, "-131449130", "% 6.ld"}, + {__LINE__, 0xffffffb2, "FFFFFFB2", "%X"}, + {__LINE__, -0x0004aad, "-19117", "%+6.4ld"}, + {__LINE__, 0x00115586, "1136006", "%0d"}, + {__LINE__, -0x0000021, "-33", "%d"}, + {__LINE__, 0x093ce01e, "93ce01e", "% 5x"}, + {__LINE__, 0x00672135, "672135", "% x"}, + {__LINE__, 0x05f4abd5, "0x5f4abd5", "%+#6.x"}, + {__LINE__, -0x0448b03, "-4492035", "%-0.2ld"}, + {__LINE__, 0x00000000, "0", "%#x"}, + {__LINE__, 0x1987217e, "428286334", "%d"}, + {__LINE__, 0x000010f6, "10f6", "%-x"}, + {__LINE__, 0x0000177a, "177a", "%1x"}, + {__LINE__, 0xfffffefd, "FFFFFEFD", "%7X"}, + {__LINE__, -0x095cbf8, "-9817080", "%-5ld"}, + {__LINE__, 0x0000008a, "138", "%2.0ld"}, + {__LINE__, 0xfffe80ad, "fffe80ad", "%0x"}, + {__LINE__, -0x1a4c8f7, "-27576567", "%d"}, + {__LINE__, 0x00000000, "0", "%-x"}, + {__LINE__, 0x0000a82b, "43051", "%-ld"}, + {__LINE__, 0x000000eb, "235", "%2.3ld"}, + {__LINE__, -0x0002747, "-10055", "%d"}, + {__LINE__, 0x00000319, "0X319", "%#X"}, + {__LINE__, 0xfffffcfe, "FFFFFCFE", "% X"}, + {__LINE__, 0x00050f0e, "50f0e", "%-1x"}, + {__LINE__, -0x398e09e, "-60350622", "%0.7ld"}, + {__LINE__, 0x00000000, " ", "%2.ld"}, + {__LINE__, 0x00000049, "00049", "% .5X"}, + {__LINE__, -0x00000dd, " -221", "%+#5ld"}, + {__LINE__, 0xf6db2fac, "f6db2fac", "%-07x"}, + {__LINE__, 0x0000004f, "4F", "% .0X"}, + {__LINE__, 0x00000000, "0", "%x"}, + {__LINE__, 0xfd07f692, "fd07f692", "%x"}, + {__LINE__, 0xdb98bde0, "db98bde0", "%x"}, + {__LINE__, 0x00000271, "0271", "% 3.4x"}, + {__LINE__, -0x000115a, "-4442", "%#1.ld"}, + {__LINE__, 0x0002f5f2, "194034", "%ld"}, + {__LINE__, -0x0000152, " -338", "%+7ld"}, + {__LINE__, 0x00059336, "365366", "%d"}, + {__LINE__, 0xfffffff6, "fffffff6", "%-7x"}, + {__LINE__, 0xfb916c51, "fb916c51", "%-x"}, + {__LINE__, 0xffff413d, "FFFF413D", "%-X"}, + {__LINE__, 0xf2576910, "F2576910", "%-4.X"}, + {__LINE__, 0xfffc7730, "fffc7730", "%5.x"}, + {__LINE__, 0x000002e6, "+742", "%+1d"}, + {__LINE__, -0x00001bf, "-447", "%3d"}, + {__LINE__, 0x000002e1, "2E1", "%-X"}, + {__LINE__, 0x00000096, "150", "%#0ld"}, + {__LINE__, 0x000002bd, "701", "%d"}, + {__LINE__, -0x0053386, "-340870", "%#d"}, + {__LINE__, 0xfffdb076, "FFFDB076", "%X"}, + {__LINE__, 0x00004dc5, " +19909", "%+07.ld"}, + {__LINE__, 0x00000f7a, " F7A", "%4X"}, + {__LINE__, 0x02405000, " 37769216", "% ld"}, + {__LINE__, 0xfffce68d, "fffce68d", "%0x"}, + {__LINE__, -0x35b3af2, "-56310514", "%-ld"}, + {__LINE__, -0x0238631, "-2328113", "%.4d"}, + {__LINE__, -0x000001e, "-030", "%.3ld"}, + {__LINE__, 0xffffffdd, "FFFFFFDD", "%.4X"}, + {__LINE__, 0x0013f6ac, "13f6ac", "%02.x"}, + {__LINE__, 0xfffffffa, "fffffffa", "%+07.x"}, + {__LINE__, -0x000192b, " -6443", "%6.d"}, + {__LINE__, 0x0000058b, "+1419", "%+d"}, + {__LINE__, 0x00001391, "5009", "%ld"}, + {__LINE__, -0x0041a57, "-268887", "%1ld"}, + {__LINE__, -0x0041a57, "-268887", "%1ld"}, + {__LINE__, 0xfffff266, "0xfffff266", "%+#6.6x"}, + {__LINE__, 0x0000018b, " 395", "%6.ld"}, + {__LINE__, 0x009ae24d, "+10150477", "%+ld"}, + {__LINE__, 0x0000007e, "7E", "%X"}, + {__LINE__, 0xfffffd53, "FFFFFD53", "% 0X"}, + {__LINE__, 0x000002fa, "2FA", "%-1.3X"}, + {__LINE__, 0x000000a4, "164", "%0d"}, + {__LINE__, 0x00001833, "6195", "%ld"}, + {__LINE__, -0x0000001, "-1", "%d"}, + {__LINE__, 0xf2fcbd9b, "F2FCBD9B", "%+0.2X"}, + {__LINE__, 0x00004a8a, "4A8A", "%X"}, + {__LINE__, -0x10695cda, "-275340506", "%d"}, + {__LINE__, 0x00000037, "37", "%X"}, + {__LINE__, 0x00082d5c, "82D5C", "%+0.5X"}, + {__LINE__, 0xfffffe01, "fffffe01", "% 5.3x"}, + {__LINE__, 0x000005ea, "05EA", "% 1.4X"}, + {__LINE__, 0x0e0b8826, "e0b8826", "%2x"}, + {__LINE__, -0x0000161, "-353", "%+d"}, + {__LINE__, 0x19e3014a, "19E3014A", "%+5X"}, + {__LINE__, 0x00000140, "140", "%x"}, + {__LINE__, 0xffff84b9, "FFFF84B9", "%X"}, + {__LINE__, -0x0005776, "-22390", "%+5ld"}, + {__LINE__, 0x0162ad61, "162AD61", "% X"}, + {__LINE__, 0x00002395, "2395", "%X"}, + {__LINE__, 0x003702ec, "3605228", "%5.2d"}, + {__LINE__, 0xffffffff, "FFFFFFFF", "%7X"}, + {__LINE__, 0x00001778, "0x1778", "%#5.x"}, + {__LINE__, 0xc52bef7f, "C52BEF7F", "%X"}, + {__LINE__, 0xfffffec4, "FFFFFEC4", "%X"}, + {__LINE__, 0x00000006, "6", "%X"}, + {__LINE__, 0x00000000, "000", "%03.3x"}, + {__LINE__, 0x00000000, "0", "%x"}, + {__LINE__, 0xffff6c46, "FFFF6C46", "% 2.2X"}, + {__LINE__, -0x1506098, "-22044824", "%01.d"}, + {__LINE__, 0x03415caf, "3415caf", "%x"}, + {__LINE__, -0x00007b4, "-1972", "%#4.4d"}, + {__LINE__, -0x00158c9, "-88265", "%03.4d"}, + {__LINE__, 0x6947c336, "1766310710", "%.6d"}, + {__LINE__, 0x0000017c, "17C", "% 0X"}, + {__LINE__, -0x00001b7, "-439", "% 0ld"}, + {__LINE__, 0x06a7575a, "6A7575A", "%-.0X"}, + {__LINE__, 0x000b3459, "734297", "%ld"}, + {__LINE__, -0x395d8dd, "-60152029", "%ld"}, + {__LINE__, 0x00036bb3, "224179", "%-6.d"}, + {__LINE__, 0xffff14ff, "FFFF14FF", "%X"}, + {__LINE__, -0x15910e4f, "-361827919", "%-0d"}, + {__LINE__, -0x0000004, "-4", "%#d"}, + {__LINE__, 0xf7f7adac, "F7F7ADAC", "% 3.X"}, + {__LINE__, 0xffffff74, "FFFFFF74", "%0X"}, + {__LINE__, 0x00000000, "0", "%d"}, + {__LINE__, -0x000007b, "-123 ", "%-7.d"}, + {__LINE__, -0x000002c, "-44", "%d"}, + {__LINE__, -0x000002b, " -43", "%5d"}, + {__LINE__, 0x0000ae5f, "44639", "%d"}, + {__LINE__, 0x003cbc63, "3CBC63", "%X"}, + {__LINE__, 0xfffffffc, "0XFFFFFFFC", "%#5.7X"}, + {__LINE__, 0x0003e044, "3E044", "%X"}, + {__LINE__, -0x0000c46, "-3142", "%.1d"}, + {__LINE__, 0xff18f4bb, "FF18F4BB", "%0X"}, + {__LINE__, 0xfffffffd, "FFFFFFFD", "%.1X"}, + {__LINE__, -0x01c0f27, "-1838887", "%0d"}, + {__LINE__, -0x1242901f, "-306352159", "%.4ld"}, + {__LINE__, 0x1775c10a, "393593098", "%6.ld"}, + {__LINE__, 0x00000001, " 1", "%03.d"}, + {__LINE__, 0xfff1fd30, "0xfff1fd30", "%#x"}, + {__LINE__, 0x00000191, "191", "%0.X"}, + {__LINE__, 0x2e597178, "777613688", "%2.ld"}, + {__LINE__, -0x4ef0cf3, "-82775283", "%+.6ld"}, + {__LINE__, -0x0000085, " -133", "%6.d"}, + {__LINE__, 0x0000014d, "333 ", "%-5ld"}, + {__LINE__, -0xc03bd74, "-201571700", "%07.6ld"}, + {__LINE__, -0x000000a, "-10", "%d"}, + {__LINE__, 0x01f5e86e, "32893038", "%#2.ld"}, + {__LINE__, 0x00000033, "0X33", "% #X"}, + {__LINE__, 0x0087c797, " 8898455", "% 7.d"}, + {__LINE__, -0x0001205, "-4613", "%5d"}, + {__LINE__, 0x00000192, " 192", "%7.X"}, + {__LINE__, 0x000e6c00, "945152", "%6.1ld"}, + {__LINE__, 0x00018629, " 99881", "% d"}, + {__LINE__, -0x04739c6, "-4667846", "%-01.3ld"}, + {__LINE__, 0x00001f42, "8002", "%d"}, + {__LINE__, 0x00000caf, "3247", "%d"}, + {__LINE__, -0x4992e24, "-77147684", "%#.6d"}, + {__LINE__, 0x0000001d, "1d", "%-0x"}, + {__LINE__, 0x14d79f03, "349675267", "%0ld"}, + {__LINE__, 0x00000dd4, " 03540", "%#6.5ld"}, + {__LINE__, 0x0005f30e, "0X5F30E", "%#X"}, + {__LINE__, 0x00000157, "157", "%X"}, + {__LINE__, -0x9281698, "-153622168", "%d"}, + {__LINE__, -0x000004a, "-74", "%d"}, + {__LINE__, -0x0010c9e, "-68766", "%-0d"}, + {__LINE__, 0x00000059, "00089", "%.5ld"}, + {__LINE__, -0x06959e3, "-6904291", "% 4.d"}, + {__LINE__, 0xfbea12b1, "0XFBEA12B1", "%+#X"}, + {__LINE__, 0xffffdfb1, "FFFFDFB1", "%2.6X"}, + {__LINE__, 0x0434faac, "434faac", "%.5x"}, + {__LINE__, 0xffffffff, "ffffffff", "% x"}, + {__LINE__, 0x00000b32, "B32", "%0X"}, + {__LINE__, 0x00000047, "71", "%.1d"}, + {__LINE__, 0x00070bef, " 0461807", "% .7d"}, + {__LINE__, 0x00000038, "38", "% 0x"}, + {__LINE__, 0x00000000, " 0", "%6X"}, + {__LINE__, 0xfff9c011, "FFF9C011", "%2X"}, + {__LINE__, 0xfffffffe, "fffffffe", "%7x"}, + {__LINE__, 0xfffffff9, "FFFFFFF9", "%X"}, + {__LINE__, -0x016a095, "-1482901", "%4.0ld"}, + {__LINE__, -0x000001c, "-28", "% ld"}, + {__LINE__, 0xfffd6133, "0xfffd6133", "%#x"}, + {__LINE__, 0x000004a4, "0x4a4", "%#x"}, + {__LINE__, 0x000003c2, "0962", "%.4d"}, + {__LINE__, -0x000323b, "-12859", "%+#d"}, + {__LINE__, 0x0f620237, "F620237", "%X"}, + {__LINE__, 0x00007863, "007863", "%.6X"}, + {__LINE__, 0x0000002c, " 44", "% #ld"}, + {__LINE__, 0xfffffff0, "fffffff0", "%03.6x"}, + {__LINE__, 0xffff6346, "FFFF6346", "%1.X"}, + {__LINE__, 0x00063188, "63188", "% x"}, + {__LINE__, 0xfffff91c, "fffff91c", "%-.3x"}, + {__LINE__, 0x004cd0b4, "4CD0B4", "%-2.X"}, + {__LINE__, 0x06b4d739, "112514873", "%0.1d"}, + {__LINE__, -0x0009beb, "-39915", "%.2d"}, + {__LINE__, 0x0000831b, "33563", "%d"}, + {__LINE__, 0x00000001, "1", "%X"}, + {__LINE__, -0x942d76b, "-155375467", "% #0ld"}, + {__LINE__, 0xffff2a95, "FFFF2A95", "%2X"}, + {__LINE__, 0x00548d5e, "5541214", "%.3ld"}, + {__LINE__, 0x0b5e1a01, "B5E1A01", "% .7X"}, + {__LINE__, 0xfffffc22, "fffffc22", "%6x"}, + {__LINE__, -0x0000dd7, "-3543", "%-.3ld"}, + {__LINE__, 0xfffff834, "FFFFF834", "%X"}, + {__LINE__, 0x0365f762, "57014114", "%ld"}, + {__LINE__, -0x0000003, "-3", "%d"}, + {__LINE__, 0x3bd998a3, "1004116131", "%d"}, + {__LINE__, 0xfffff6c9, "FFFFF6C9", "%+X"}, + {__LINE__, 0xffffded9, "FFFFDED9", "%-.1X"}, + {__LINE__, -0x00fbb5a, "-1031002", "% 1.0d"}, + {__LINE__, 0xffffffb7, "ffffffb7", "%6.x"}, + {__LINE__, 0xff1b8ac3, "FF1B8AC3", "%-X"}, + {__LINE__, 0xfffffff2, "FFFFFFF2", "%00.X"}, + {__LINE__, -0x000013d, " -317", "%05.d"}, + {__LINE__, 0x0000f5e2, "+62946", "%+0.2ld"}, + {__LINE__, 0x16ac6358, "16ac6358", "%x"}, + {__LINE__, 0xffff8728, "FFFF8728", "%+X"}, + {__LINE__, -0x0014a6d, "-084589", "%0.6ld"}, + {__LINE__, 0xfc904514, "FC904514", "%+0X"}, + {__LINE__, 0x00000004, "4", "%ld"}, + {__LINE__, 0xffffffe0, "ffffffe0", "%+x"}, + {__LINE__, -0x0a0ffdd, "-10551261", "%+02d"}, + {__LINE__, -0x0000bbc, "-3004", "%+d"}, + {__LINE__, 0x00000006, " +6", "%+7ld"}, + {__LINE__, 0x17afa6e5, "397387493", "%ld"}, + {__LINE__, 0xfffff6bf, "fffff6bf", "%+04.4x"}, + {__LINE__, 0x000001dc, "01dc", "% 02.4x"}, + {__LINE__, 0x0000000e, " 14", "% d"}, + {__LINE__, 0xfffffff9, "0XFFFFFFF9", "%#4.X"}, + {__LINE__, 0x0000005d, "93", "%0d"}, + {__LINE__, -0x004fa05, "-0326149", "%.7d"}, + {__LINE__, -0x0000018, "-24", "%ld"}, + {__LINE__, 0xfffff7eb, "FFFFF7EB", "%X"}, + {__LINE__, 0x0072b044, " 7516228", "% .7ld"}, + {__LINE__, 0xffffffed, "0xffffffed", "%#x"}, + {__LINE__, 0x0010364d, "10364D", "%X"}, + {__LINE__, 0xfff90469, "0XFFF90469", "%#X"}, + {__LINE__, 0x000001b4, " 436", "% 0d"}, + {__LINE__, 0x00000000, "0", "% X"}, + {__LINE__, 0xfffffff3, "fffffff3", "%x"}, + {__LINE__, 0x19335d40, "19335d40", "%6x"}, + {__LINE__, 0x00039c24, "236580", "%ld"}, + {__LINE__, 0x000021f7, "8695", "%0.4ld"}, + {__LINE__, -0x057b406, "-5747718", "%d"}, + {__LINE__, -0x017b371, "-1553265", "%#ld"}, + {__LINE__, 0x0003e405, "254981", "%2.d"}, + {__LINE__, 0x00000001, "1", "%-.1x"}, + {__LINE__, 0x000000ae, "AE", "%.1X"}, + {__LINE__, 0xffd85825, "ffd85825", "%-7.x"}, + {__LINE__, 0xfffad763, "fffad763", "%x"}, + {__LINE__, 0x002431d4, "2372052", "%6.ld"}, + {__LINE__, 0x00000003, "000003", "%-05.6d"}, + {__LINE__, 0xffffffeb, "0xffffffeb", "%#x"}, + {__LINE__, 0xff8cf3b0, "ff8cf3b0", "%x"}, + {__LINE__, 0xfe88d2ff, "0xfe88d2ff", "%+#.2x"}, + {__LINE__, 0xffffff6b, "ffffff6b", "%6.3x"}, + {__LINE__, 0xfffffffe, "fffffffe", "%-x"}, + {__LINE__, 0xe998945a, "e998945a", "%2.x"}, + {__LINE__, 0x03b9c50f, "3B9C50F", "%+X"}, + {__LINE__, 0xffffffff, "FFFFFFFF", "%3.7X"}, + {__LINE__, 0x000024ef, "24ef", "%+x"}, + {__LINE__, 0xfffffc04, "FFFFFC04", "%X"}, + {__LINE__, 0x0d4bef7c, " 223080316", "% .1ld"}, + {__LINE__, -0xc33f3bc, "-204731324", "%.5ld"}, + {__LINE__, 0xffffffff, "0XFFFFFFFF", "%#2X"}, + {__LINE__, 0x0000e493, "e493", "%x"}, + {__LINE__, 0x000001b4, "1b4", "%x"}, + {__LINE__, 0xffffffd6, "0xffffffd6", "% #1x"}, + {__LINE__, 0x00000001, "1", "%d"}, + {__LINE__, -0x0000f28, "-3880", "%ld"}, + {__LINE__, 0x00000277, "631", "%-1ld"}, + {__LINE__, 0x00000001, " 1", "%2x"}, + {__LINE__, -0x0ff4d2c, "-16731436", "%#ld"}, + {__LINE__, 0x0bb80344, "196608836", "%00ld"}, + {__LINE__, 0xffffffdc, "FFFFFFDC", "%X"}, + {__LINE__, 0x00000484, "1156", "%ld"}, + {__LINE__, 0x00000341, "341", "%-.2x"}, + {__LINE__, 0x0000ee62, "ee62", "%x"}, + {__LINE__, 0xfffffe20, "fffffe20", "%+x"}, + {__LINE__, -0x00076eb, "-30443", "%.2d"}, + {__LINE__, 0x2c4a7407, "2c4a7407", "% x"}, + {__LINE__, 0x00000000, " ", "%3.X"}, + {__LINE__, 0xffffff60, "ffffff60", "% 4.x"}, + {__LINE__, 0x000e4cf2, "+937202", "%+.5ld"}, + {__LINE__, 0x00000008, " 00008", "%06.5X"}, + {__LINE__, 0x0002f612, "194066", "%d"}, + {__LINE__, 0xfffffc96, "fffffc96", "%-3.3x"}, + {__LINE__, -0x0000001, "-1", "% ld"}, + {__LINE__, 0x00000000, " ", "% 6.0X"}, + {__LINE__, 0xfffffff5, "fffffff5", "%3.x"}, + {__LINE__, 0x0bfd63a1, "201155489", "%d"}, + {__LINE__, -0x1547c214, "-357024276", "%ld"}, + {__LINE__, 0x00000004, "4", "%#0.d"}, + {__LINE__, -0x000f2f3, "-62195", "%.2d"}, + {__LINE__, 0xfff843dd, "FFF843DD", "%X"}, + {__LINE__, 0x00966e36, "9858614", "%.0ld"}, + {__LINE__, 0x0000782e, "0X782E", "% #X"}, + {__LINE__, 0x00bd36c0, "12400320", "%ld"}, + {__LINE__, 0x00000000, "00", "% .2X"}, + {__LINE__, 0x00000000, "0", "%1X"}, + {__LINE__, 0x0003f416, "3f416 ", "%-6.x"}, + {__LINE__, -0x00b74bd, "-750781", "% ld"}, + {__LINE__, 0x0000138a, "138a", "% x"}, + {__LINE__, 0x024df7f0, "24DF7F0", "% 0.6X"}, + {__LINE__, 0xfffffffc, "FFFFFFFC", "%X"}, + {__LINE__, -0x0003284, "-12932 ", "%-07.ld"}, + {__LINE__, 0x000094d3, "94d3", "%x"}, + {__LINE__, 0x000000bf, "bf", "%.2x"}, + {__LINE__, 0x00000000, "00000", "%-1.5X"}, + {__LINE__, -0x04899b8, "-4757944", "%d"}, + {__LINE__, 0x2b51bf20, "+726777632", "%+d"}, + {__LINE__, -0x000000d, "-13", "%.2d"}, + {__LINE__, 0x04f78fbd, "4F78FBD", "% 7.X"}, + {__LINE__, 0x00011684, "71300", "%5.d"}, + {__LINE__, 0x0000539d, "21405", "%0d"}, + {__LINE__, 0x00000008, "000008", "%-3.6X"}, + {__LINE__, 0xfc7a2cdc, "fc7a2cdc", "%2.3x"}, + {__LINE__, 0xffc21da1, "ffc21da1", "%x"}, + {__LINE__, 0x00000273, "273", "%x"}, + {__LINE__, 0x00000000, "0", "%#X"}, + {__LINE__, -0x31cd6b9, "-52221625", "% d"}, + {__LINE__, 0xfffffff8, "FFFFFFF8", "%.0X"}, + {__LINE__, 0x00000002, "2", "%#ld"}, + {__LINE__, 0x00e8922c, "15241772", "%d"}, + {__LINE__, 0xfeb19ea7, "FEB19EA7", "%0.1X"}, + {__LINE__, 0x00003931, "014641", "%06ld"}, + {__LINE__, 0x00000015, "0X00015", "%#.5X"}, + {__LINE__, 0xfffffffe, "FFFFFFFE", "%1.X"}, + {__LINE__, 0xfff17103, "0XFFF17103", "% #X"}, + {__LINE__, 0xfffffffc, "FFFFFFFC", "%+X"}, + {__LINE__, 0x00000002, "00002", "%.5x"}, + {__LINE__, 0x00001617, " 5655", "% 7.d"}, + {__LINE__, -0x0000010, "-000016", "%00.6d"}, + {__LINE__, 0x000000b0, "B0", "% X"}, + {__LINE__, 0xfc9362b2, "FC9362B2", "%-X"}, + {__LINE__, 0xfc3d8276, "fc3d8276", "% 1x"}, + {__LINE__, 0x00001405, "5125", "%#d"}, + {__LINE__, 0x02250183, " 35979651", "% ld"}, + {__LINE__, 0xffc9b007, "ffc9b007", "% 4.x"}, + {__LINE__, 0x00000311, "785", "%0d"}, + {__LINE__, 0x0004d273, " 316019", "% d"}, + {__LINE__, 0x00000001, "1", "%1.x"}, + {__LINE__, 0xffff240f, "FFFF240F", "%4.X"}, + {__LINE__, 0x0049cfda, "+4837338", "%+ld"}, + {__LINE__, 0x012f6dd5, "12F6DD5", "% 6.2X"}, + {__LINE__, 0x00000002, "0x2", "%#2.0x"}, + {__LINE__, 0xfffd834a, "FFFD834A", "%.6X"}, + {__LINE__, 0xfe2a40f8, "0XFE2A40F8", "%#X"}, + {__LINE__, 0xffffffeb, "ffffffeb", "%x"}, + {__LINE__, -0x00ac5cb, "-705995", "%d"}, + {__LINE__, -0x3021108c, "-807473292", "% .4d"}, + {__LINE__, -0x0000035, "-53", "%-ld"}, + {__LINE__, -0x1a36475, "-27485301", "% d"}, + {__LINE__, -0x0000011, "-17", "%d"}, + {__LINE__, 0x000001a3, "000419", "%.6ld"}, + {__LINE__, 0x0030a0a8, "3186856", "%.5ld"}, + {__LINE__, 0x00000019, "25", "%d"}, + {__LINE__, 0xf32deac1, "F32DEAC1", "%06.2X"}, + {__LINE__, -0x0000004, " -00004", "%7.5ld"}, + {__LINE__, 0x00000000, "0", "%ld"}, + {__LINE__, 0x0006bf19, "442137", "%#0.d"}, + {__LINE__, 0x566f6c44, "1450142788", "%1.6d"}, + {__LINE__, 0xfffffc10, "fffffc10", "%.2x"}, + {__LINE__, -0x000e04d, "-57421", "%+0d"}, + {__LINE__, 0x00000000, "0000000", "%+.7X"}, + {__LINE__, 0x00b5c74d, " 11913037", "% ld"}, + {__LINE__, -0x028dfe2, "-2678754", "%05.1d"}, + {__LINE__, 0xfffffee0, "fffffee0", "%0x"}, + {__LINE__, -0x030c077, "-3194999", "%-.6d"}, + {__LINE__, 0x00055ca1, "055ca1", "%-.6x"}, + {__LINE__, 0x00000000, " ", "%7.X"}, + {__LINE__, 0xfffffe3b, "FFFFFE3B", "%X"}, + {__LINE__, 0xffffffff, "FFFFFFFF", "% 3X"}, + {__LINE__, 0x00000857, "2135", "%-#1.d"}, + {__LINE__, -0x00546a2, "-345762", "%0.5d"}, + {__LINE__, 0x0000000b, "0xb ", "%-#6x"}, + {__LINE__, 0x00000d2b, "d2b ", "%-4.0x"}, + {__LINE__, 0x0ae02b9e, "AE02B9E", "%X"}, + {__LINE__, 0xfffffa7b, "fffffa7b", "%-2.x"}, + {__LINE__, 0x00000001, "1", "%X"}, + {__LINE__, 0x000006ad, "01709", "%05.5ld"}, + {__LINE__, -0x0000102, "-258", "%-0.ld"}, + {__LINE__, 0x00000000, "0", "%.1X"}, + {__LINE__, -0x01daa95, "-1944213", "%-1ld"}, + {__LINE__, 0x02b99040, "2B99040", "%2.X"}, + {__LINE__, 0x1b3d5621, "1B3D5621", "%X"}, + {__LINE__, 0x0312d16b, "51564907", "%-1.7ld"}, + {__LINE__, 0x000aa76c, " 698220", "% 2d"}, + {__LINE__, 0x00000000, "0", "%ld"}, + {__LINE__, 0xfff8a4ec, "FFF8A4EC", "%-X"}, + {__LINE__, 0xffffe06d, "ffffe06d", "%x"}, + {__LINE__, 0x00000003, "3", "% x"}, + {__LINE__, 0x00000000, "0", "%x"}, + {__LINE__, 0xd3e244dd, "D3E244DD", "%+5X"}, + {__LINE__, 0x000028b3, " 28b3", "% 5.x"}, + {__LINE__, 0x0001aceb, "109803", "%6d"}, + {__LINE__, 0xfffbc5ca, "0xfffbc5ca", "%-#5x"}, + {__LINE__, 0x00000097, " 151", "% 7ld"}, + {__LINE__, 0x00001fcc, "+8140", "%+d"}, + {__LINE__, 0xffffffff, "ffffffff", "%0x"}, + {__LINE__, -0x00052a9, "-21161", "%ld"}, + {__LINE__, 0xfffffc76, "FFFFFC76", "%.1X"}, + {__LINE__, -0x2acb012, "-44871698", "%0.1ld"}, + {__LINE__, 0xffffff81, "ffffff81", "%x"}, + {__LINE__, -0x0018394, "-99220", "%-ld"}, + {__LINE__, 0x0000001b, "1B", "%X"}, + {__LINE__, 0x00000033, "51", "%1d"}, + {__LINE__, 0xffec37e5, "FFEC37E5", "%-1X"}, + {__LINE__, -0x000000a, " -10", "%#5.d"}, + {__LINE__, -0x0000412, "-1042", "%ld"}, + {__LINE__, 0x000cd0b1, "cd0b1 ", "%-7.5x"}, + {__LINE__, 0x0b445370, "B445370", "%+2.0X"}, + {__LINE__, 0xfffffff3, "0XFFFFFFF3", "%-#X"}, + {__LINE__, 0xffff9f33, "FFFF9F33", "%5.X"}, + {__LINE__, 0x00010a1e, "10A1E", "%-2X"}, + {__LINE__, -0xede156f, "-249435503", "% 7.1ld"}, + {__LINE__, 0xfc6d63aa, "fc6d63aa", "% .6x"}, + {__LINE__, 0x0000311c, " 12572", "% ld"}, + {__LINE__, 0x00001f8a, "8074", "%00.d"}, + {__LINE__, 0x00000199, "199", "%-X"}, + {__LINE__, 0xfffffc60, "FFFFFC60", "%+3X"}, + {__LINE__, -0x000022c, "-556", "%+0ld"}, + {__LINE__, 0x021d8407, "35488775", "%-#.7ld"}, + {__LINE__, 0x0000020f, "527", "%d"}, + {__LINE__, -0x000064b, "-1611", "%#ld"}, + {__LINE__, -0x0c15aef, "-12671727", "%d"}, + {__LINE__, -0x1f1a6881, "-521824385", "%ld"}, + {__LINE__, 0xffffc115, "ffffc115", "% x"}, + {__LINE__, 0x00000017, " 23", "% #ld"}, + {__LINE__, 0x00000dc1, "DC1 ", "%-4X"}, + {__LINE__, 0x000002ca, "2CA", "%X"}, + {__LINE__, 0x000f052c, " 984364", "%7.d"}, + {__LINE__, 0x0000005a, "0090", "%0.4d"}, + {__LINE__, -0x001e463, "-124003", "% 4.ld"}, + {__LINE__, 0x00000001, "0000001", "%+.7X"}, + {__LINE__, 0x00000bfb, "0000bfb", "%5.7x"}, + {__LINE__, 0x0002484b, "149579", "%#1d"}, + {__LINE__, 0x0043ba18, "4438552", "%-ld"}, + {__LINE__, 0x000078f8, "0X78F8", "%#X"}, + {__LINE__, 0x00000001, " 1", "%4d"}, + {__LINE__, 0x0cedaeed, "CEDAEED", "%X"}, + {__LINE__, 0x00000003, "+3", "%+ld"}, + {__LINE__, 0xfffffffb, "FFFFFFFB", "%7.4X"}, + {__LINE__, 0x00000169, "361", "%ld"}, + {__LINE__, 0x00003c7a, "+15482", "%+ld"}, + {__LINE__, -0x0159d93, "-1416595", "% 0.0d"}, + {__LINE__, 0x0000775c, "+30556", "%+3.d"}, + {__LINE__, 0x10284768, "+271075176", "%+0.0ld"}, + {__LINE__, -0xf8b610e, "-260792590", "%0d"}, + {__LINE__, 0xfdd8d369, "fdd8d369", "%-.3x"}, + {__LINE__, 0x000c9495, "+824469", "%+ld"}, + {__LINE__, -0x000003f, " -063", "%7.3d"}, + {__LINE__, -0x000073b, "-1851", "%+03.ld"}, + {__LINE__, 0xfffe4e23, "0xfffe4e23", "%#.5x"}, + {__LINE__, 0x0081788c, "81788c", "%5x"}, + {__LINE__, -0x015f888, "-1439880", "%+1d"}, + {__LINE__, -0x31d64b4, "-52257972", "%0.2d"}, + {__LINE__, 0xffffffff, "FFFFFFFF", "%6.5X"}, + {__LINE__, -0x0000072, "-114", "%+.1d"}, + {__LINE__, -0x00000e1, "-225", "%ld"}, + {__LINE__, 0x000006a8, "0x6a8", "%+#x"}, + {__LINE__, 0xfffe673a, "fffe673a", "%01.x"}, + {__LINE__, 0xfff2ee0d, "FFF2EE0D", "%-X"}, + {__LINE__, 0x0290320a, "43004426", "%0ld"}, + {__LINE__, -0xeb7a832, "-246917170", "% ld"}, + {__LINE__, 0x02ae9265, "2ae9265", "%1.x"}, + {__LINE__, 0xffffffd2, "ffffffd2", "%+2.x"}, + {__LINE__, 0x00e6e858, "0xe6e858", "%#0.x"}, + {__LINE__, 0x00000c4a, "+3146", "%+3.1d"}, + {__LINE__, -0x6943c1a, "-110378010", "% 0ld"}, + {__LINE__, 0x0167b119, "167b119", "%0x"}, + {__LINE__, 0xfffffffe, "fffffffe", "%+x"}, + {__LINE__, 0xfffffffe, "FFFFFFFE", "%X"}, + {__LINE__, 0x000b861e, "755230", "%ld"}, + {__LINE__, 0x0000015b, "347", "%-#ld"}, + {__LINE__, 0xfffffffa, "0XFFFFFFFA", "%#X"}, + {__LINE__, 0x000de9e5, "911845", "%d"}, + {__LINE__, 0x00000001, "01", "%.2d"}, + {__LINE__, 0x000007a5, "1957", "%0d"}, + {__LINE__, 0x0000000f, "f", "%x"}, + {__LINE__, 0x00c38cbf, " 12815551", "% .2d"}, + {__LINE__, -0x7bd1b6e, "-129833838", "% d"}, + {__LINE__, -0x000013c, "-316", "%0d"}, + {__LINE__, 0x00001aad, "1AAD", "%-0X"}, + {__LINE__, 0x0034f903, "3471619", "%00d"}, + {__LINE__, 0xff925717, "ff925717", "%4x"}, + {__LINE__, 0x00000002, "02", "%.2d"}, + {__LINE__, 0x00000f34, "F34", "%+X"}, + {__LINE__, 0xffffeefe, "FFFFEEFE", "%X"}, + {__LINE__, 0xfffeecb4, "FFFEECB4", "% 2.X"}, + {__LINE__, 0x00034421, "214049", "%0ld"}, + {__LINE__, 0x00000000, " 0", "%+6X"}, + {__LINE__, -0x0000062, "-98", "%-#2d"}, + {__LINE__, -0x0000557, "-1367", "%-4.4d"}, + {__LINE__, 0xffffe17c, "FFFFE17C", "%X"}, + {__LINE__, 0x00000097, "00097", "%.5X"}, + {__LINE__, 0xfffcb278, "FFFCB278", "%0.7X"}, + {__LINE__, -0x0000001, "-0001 ", "%-7.4d"}, + {__LINE__, -0x0000001, "-1", "%ld"}, + {__LINE__, 0x000002c4, "708", "%d"}, + {__LINE__, 0x04946f45, "4946F45", "%-2.X"}, + {__LINE__, 0x000000ff, "00ff", "% 2.4x"}, + {__LINE__, 0x00073307, "471815", "%3ld"}, + {__LINE__, 0x0085b7b6, "+8763318", "%+ld"}, + {__LINE__, -0x0000002, "-2", "%1ld"}, + {__LINE__, -0x0000001, "-1", "%-2d"}, + {__LINE__, -0x00000b5, " -181", "%7ld"}, + {__LINE__, -0x0412486, "-4269190", "%+2.1ld"}, + {__LINE__, 0xffffffff, "ffffffff", "%0x"}, + {__LINE__, 0x0000006d, "6d", "%x"}, + {__LINE__, 0x000000da, "218", "%-.3ld"}, + {__LINE__, 0xfffffcfe, "fffffcfe", "%x"}, + {__LINE__, 0x652e60c7, "652E60C7", "%7.7X"}, + {__LINE__, 0x00000035, " 53", "% #2d"}, + {__LINE__, 0x000000fc, "FC", "%-X"}, + {__LINE__, 0x00000019, " 25", "% ld"}, + {__LINE__, 0xfffffcf4, "fffffcf4", "%6x"}, + {__LINE__, 0xffffff41, "ffffff41", "%x"}, + {__LINE__, 0x00000034, " 52", "%3.ld"}, + {__LINE__, 0xffffffb2, "ffffffb2", "%-x"}, + {__LINE__, -0x6dc43e7c, "-1841577596", "% .2ld"}, + {__LINE__, 0x00000004, "4", "%d"}, + {__LINE__, -0x000023b, "-571", "%ld"}, + {__LINE__, -0x000004d, "-77", "%ld"}, + {__LINE__, 0x0000e3ba, "58298", "%.0ld"}, + {__LINE__, 0xfffffff2, "fffffff2", "%0.0x"}, + {__LINE__, -0x0000020, "-32", "%ld"}, + {__LINE__, 0xffffb33e, "0XFFFFB33E", "%-#X"}, + {__LINE__, 0x00000000, " ", "% 4.x"}, + {__LINE__, 0xffd96f92, "FFD96F92", "%X"}, + {__LINE__, 0x0000002e, " 46", "% 0ld"}, + {__LINE__, 0xffffffff, "ffffffff", "%x"}, + {__LINE__, -0x5008ba3, "-83921827", "%+d"}, + {__LINE__, 0xfdc1df3d, "FDC1DF3D", "%0X"}, + {__LINE__, 0xfc7410e5, "fc7410e5", "%7x"}, + {__LINE__, 0xfffffffa, "FFFFFFFA", "% 6.X"}, + {__LINE__, 0x00000000, "0", "%X"}, + {__LINE__, -0x040fd4e, "-4259150", "% 05.d"}, + {__LINE__, 0x0000003b, " 59", "%3.2d"}, + {__LINE__, 0xff776dd8, "ff776dd8", "%x"}, + {__LINE__, 0x0000055b, "+1371", "%+#2d"}, + {__LINE__, 0x00004841, "4841", "%.1x"}, + {__LINE__, 0x0229d710, "36296464", "%-.2ld"}, + {__LINE__, 0x00000000, "0", "%x"}, + {__LINE__, 0xfffe8376, "fffe8376", "%0x"}, + {__LINE__, 0x000a7b83, "a7b83", "%x"}, + {__LINE__, 0x0000000d, " 013", "%#7.3d"}, + {__LINE__, 0xffffffff, "FFFFFFFF", "%5.0X"}, + {__LINE__, 0x00000704, " 0x704", "%#7.0x"}, + {__LINE__, 0xfff970b9, "FFF970B9", "%.1X"}, + {__LINE__, -0x000b60a, "-46602", "%+d"}, + {__LINE__, 0x00000000, " 00000", "%6.5x"}, + {__LINE__, 0x00000003, "3", "%X"}, + {__LINE__, 0x0000c14b, "c14b", "%0x"}, + {__LINE__, 0x066d7860, "107837536", "%-d"}, + {__LINE__, 0x00000013, " 13", "%4.x"}, + {__LINE__, 0xfff96f18, "FFF96F18", "%4X"}, + {__LINE__, 0xffffff30, "FFFFFF30", "%X"}, + {__LINE__, 0xffffffff, "FFFFFFFF", "% .7X"}, + {__LINE__, -0x72a9e985, "-1923737989", "%2.d"}, + {__LINE__, 0x00000002, "0x2", "%#x"}, + {__LINE__, 0x0000050e, "50e", "%2.3x"}, + {__LINE__, 0x0000605c, "24668", "%4.1d"}, + {__LINE__, 0xffffff3e, "ffffff3e", "%x"}, + {__LINE__, -0x0000010, "-16", "%-2d"}, + {__LINE__, 0x000061aa, "61AA", "%X"}, + {__LINE__, 0x000c2ec3, "C2EC3", "%0X"}, + {__LINE__, -0x0000001, "-1", "%-0d"}, + {__LINE__, 0x00130e17, "+1248791", "%+.6ld"}, + {__LINE__, 0x000000ef, "EF", "%-X"}, + {__LINE__, -0x0000001, " -1", "%+5ld"}, + {__LINE__, 0x0000989d, "39069", "%d"}, + {__LINE__, 0x00000000, "0", "%-x"}, + {__LINE__, 0x00000417, "417", "%X"}, + {__LINE__, 0x00005e86, "5E86", "%+X"}, + {__LINE__, -0x0007c08, "-31752", "%ld"}, + {__LINE__, 0x00da8e0f, "DA8E0F", "%2.2X"}, + {__LINE__, -0x3ee095ec, "-1054905836", "% 0d"}, + {__LINE__, 0xfffdb5a3, "fffdb5a3", "%x"}, + {__LINE__, 0xffffff83, "FFFFFF83", "%X"}, + {__LINE__, 0xfff8efd7, "0xfff8efd7", "%#x"}, + {__LINE__, 0x00000020, " 32", "% d"}, + {__LINE__, 0xfff7c002, "fff7c002", "%.7x"}, + {__LINE__, -0x0000001, "-1", "%ld"}, + {__LINE__, 0x00000000, " 000", "%05.3X"}, + {__LINE__, 0xfff190ee, "FFF190EE", "%X"}, + {__LINE__, 0x00000a6c, " 2668", "%#6.ld"}, + {__LINE__, 0x00037024, "37024", "%x"}, + {__LINE__, 0xfffec37f, "FFFEC37F", "%X"}, + {__LINE__, 0x007ee9bb, "8317371", "%#0.ld"}, + {__LINE__, 0x00000122, "290", "%ld"}, + {__LINE__, -0x0013b5f, "-80735", "%ld"}, + {__LINE__, 0xfffffff6, "FFFFFFF6", "%X"}, + {__LINE__, -0x0000c16, "-0003094", "%-00.7d"}, + {__LINE__, -0x00010ed, "-4333", "%ld"}, + {__LINE__, 0x00000007, " 0X007", "%#6.3X"}, + {__LINE__, 0x0053781e, " 5470238", "% 2d"}, + {__LINE__, 0xffff03e9, "0XFFFF03E9", "%+#7.3X"}, + {__LINE__, 0xfffffdc2, "0xfffffdc2", "% #4.x"}, + {__LINE__, -0x00018e1, "-6369", "%d"}, + {__LINE__, 0xf3a4abc6, "F3A4ABC6", "%-.2X"}, + {__LINE__, 0xfff64a27, "FFF64A27", "%7.7X"}, + {__LINE__, 0x00195ae5, "195AE5", "%+03.X"}, + {__LINE__, 0xfff8849a, "fff8849a", "%.1x"}, + {__LINE__, -0x001b045, "-110661", "%+ld"}, + {__LINE__, 0x01e8a106, "32022790", "%7.ld"}, + {__LINE__, 0x0008ee94, "8EE94", "%X"}, + {__LINE__, 0xfffcdabc, "FFFCDABC", "%X"}, + {__LINE__, 0xfda75cd2, "fda75cd2", "%x"}, + {__LINE__, 0x00fd505a, "fd505a", "%x"}, + {__LINE__, 0x00003748, "3748", "%+x"}, + {__LINE__, 0xffffffe5, "ffffffe5", "% 3.6x"}, + {__LINE__, -0x1d1c4045, "-488390725", "%d"}, + {__LINE__, 0xfffffff9, "0xfffffff9", "%#x"}, + {__LINE__, -0x0000001, "-1", "%-#ld"}, + {__LINE__, 0x00000191, "191", "%X"}, + {__LINE__, 0xfffffffe, "FFFFFFFE", "%-X"}, + {__LINE__, -0x0000006, "-6", "%ld"}, + {__LINE__, 0x00000033, " 33", "% 4x"}, + {__LINE__, -0x0159a14, "-1415700", "% ld"}, + {__LINE__, 0x000001c3, "00001c3", "%07x"}, + {__LINE__, 0xffffe9e2, "ffffe9e2", "%-x"}, + {__LINE__, 0x5b19abd9, "0x5b19abd9", "%-#x"}, + {__LINE__, 0xffff1bae, "0xffff1bae", "%#x"}, + {__LINE__, 0x00000001, "+1", "%+d"}, + {__LINE__, 0x006a23c7, "6A23C7", "%2.X"}, + {__LINE__, 0x000008a4, " 8a4", "% 4.1x"}, + {__LINE__, 0xfffffcd9, "fffffcd9", "%.1x"}, + {__LINE__, 0x0000121b, "121B", "%X"}, + {__LINE__, 0x001231aa, "1192362", "%7.d"}, + {__LINE__, -0x008026d, "-524909", "%4.2d"}, + {__LINE__, 0x192ea594, "422487444", "%ld"}, + {__LINE__, 0x0001a800, "108544", "%0d"}, + {__LINE__, 0x000062f1, "62f1", "%.4x"}, + {__LINE__, 0xff8b39b0, "ff8b39b0", "% x"}, + {__LINE__, 0x00000001, "0x1", "%+#x"}, + {__LINE__, 0x0000144a, "5194", "%ld"}, + {__LINE__, 0x00002599, "9625", "%d"}, + {__LINE__, 0xffa0a2ef, "ffa0a2ef", "%0x"}, + {__LINE__, 0xff6712e1, "FF6712E1", "%0X"}, + {__LINE__, 0x0007ebb3, "519091", "%1ld"}, + {__LINE__, 0x0000000f, "15 ", "%-4.ld"}, + {__LINE__, -0x0000096, "-150", "%2.ld"}, + {__LINE__, 0x0000004f, "4F", "%.2X"}, + {__LINE__, 0xfffffc29, "fffffc29", "%-4.x"}, + {__LINE__, 0x00000019, "19", "%x"}, + {__LINE__, 0x00000c4e, "c4e", "%x"}, + {__LINE__, 0x0000006b, " 0006B", "%6.5X"}, + {__LINE__, 0x00c8d2c6, "c8d2c6", "%.0x"}, + {__LINE__, -0x256fb8de, "-628078814", "%ld"}, + {__LINE__, 0x4f51fcf4, "4F51FCF4", "%+X"}, + {__LINE__, 0x000f000e, "983054", "%1.ld"}, + {__LINE__, 0xfffff03f, "fffff03f", "%x"}, + {__LINE__, 0x0000001d, " 1D", "%05.X"}, + {__LINE__, 0x0072ea14, "72ea14", "%x"}, + {__LINE__, -0xc6098b9, "-207657145", "%-7d"}, + {__LINE__, 0x00000a76, "2678", "%3ld"}, + {__LINE__, 0xfff84db3, "FFF84DB3", "%X"}, + {__LINE__, 0x0fb52870, "fb52870", "%+x"}, + {__LINE__, 0x08bc170a, "0x8bc170a", "%#x"}, + {__LINE__, 0xfffeeb00, "fffeeb00", "%0.5x"}, + {__LINE__, 0x000ece95, "970389", "%d"}, + {__LINE__, 0x00000000, "0", "%0X"}, + {__LINE__, 0x07f98e8a, "7f98e8a", "% x"}, + {__LINE__, 0x000499eb, " 499eb", "% 6.x"}, + {__LINE__, 0xffffffff, "FFFFFFFF", "%-X"}, + {__LINE__, 0x0aa45e86, "178544262", "%#.2d"}, + {__LINE__, 0xff73387d, "FF73387D", "%03.X"}, + {__LINE__, 0xfffffb51, "FFFFFB51", "%.3X"}, + {__LINE__, 0xffffffdc, "ffffffdc", "%6.x"}, + {__LINE__, 0xffffffed, "FFFFFFED", "%-X"}, + {__LINE__, 0x0001c4c2, "1c4c2", "%0.3x"}, + {__LINE__, -0x0000001, "-0000001", "%-#.7d"}, + {__LINE__, 0x00000007, "7", "%0.x"}, + {__LINE__, 0x00000001, "1", "%ld"}, + {__LINE__, 0xffffd000, "ffffd000", "%4.3x"}, + {__LINE__, 0x00000030, "48", "%2.d"}, + {__LINE__, -0x6c121ce, "-113320398", "%.5ld"}, + {__LINE__, 0x000001ed, "1ED", "% 3.X"}, + {__LINE__, 0xfffff0f3, "FFFFF0F3", "%+.6X"}, + {__LINE__, 0xffffffec, "ffffffec", "%x"}, + {__LINE__, 0x002d5ab8, "0x2d5ab8", "%-#x"}, + {__LINE__, 0x0026acff, "2534655", "%0ld"}, + {__LINE__, 0x01d90cd7, "+31001815", "%+.2ld"}, + {__LINE__, -0x1f7abc0, "-33008576", "%7.6d"}, + {__LINE__, 0xfc5babcc, "fc5babcc", "%x"}, + {__LINE__, -0x9b74892, "-163006610", "% .6d"}, + {__LINE__, 0x03931d84, "59972996", "%0.d"}, + {__LINE__, 0x07d261ce, "131228110", "%#ld"}, + {__LINE__, 0x00000000, "0", "%x"}, + {__LINE__, 0x00000002, "2", "%+X"}, + {__LINE__, -0x0000604, "-1540", "%+d"}, + {__LINE__, 0x00000000, " ", "%7.x"}, + {__LINE__, 0x00000001, "1", "%.0X"}, + {__LINE__, -0x0005a8e, "-23182", "% d"}, + {__LINE__, 0x03fb2730, "3fb2730", "%7.x"}, + {__LINE__, 0xffffff03, "ffffff03", "%x"}, + {__LINE__, 0x00000000, " ", "%-#3.d"}, + {__LINE__, 0x04025a4b, "0X4025A4B", "%+#X"}, + {__LINE__, -0x0000001, " -1", "%4.d"}, + {__LINE__, 0xfffe00a3, "FFFE00A3", "%+X"}, + {__LINE__, -0x1b26e3c8, "-455533512", "% ld"}, + {__LINE__, 0x000017e5, "6117", "%d"}, + {__LINE__, -0x0e4f3b6, "-15004598", "%0ld"}, + {__LINE__, 0x00000034, "52", "%ld"}, + {__LINE__, 0x0000024c, " 24C", "%4X"}, + {__LINE__, 0xfffedf65, "FFFEDF65", "%-0X"}, + {__LINE__, -0x000ff1f, "-65311", "%+#d"}, + {__LINE__, 0x00000007, " 7", "%7.x"}, + {__LINE__, 0x018656ae, "18656ae", "%3.7x"}, + {__LINE__, -0x0000004, "-4", "%d"}, + {__LINE__, 0x7081292d, "1887512877", "%d"}, + {__LINE__, 0x87d3e48c, "87d3e48c", "%-x"}, + {__LINE__, 0xf99c516e, "0xf99c516e", "% #7.4x"}, + {__LINE__, 0xfffffffa, "FFFFFFFA", "%-1X"}, + {__LINE__, -0x000001d, "-29", "%#ld"}, + {__LINE__, -0x0000001, "-1", "%+ld"}, + {__LINE__, 0x28ab3350, " 682308432", "% 1d"}, + {__LINE__, -0x0000006, "-6", "%ld"}, + {__LINE__, 0xfffffffb, "FFFFFFFB", "%.5X"}, + {__LINE__, -0x0000007, "-7", "%#d"}, + {__LINE__, -0x0000328, " -808", "%6.ld"}, + {__LINE__, -0x29386f00, "-691564288", "%.3d"}, + {__LINE__, -0x059f38a, "-5895050", "%.4ld"}, + {__LINE__, 0xfffff888, "FFFFF888", "%3X"}, + {__LINE__, 0xffffffdf, "ffffffdf", "%0.0x"}, + {__LINE__, -0xb4fc354, "-189776724", "%2d"}, + {__LINE__, 0x00058c07, "363527", "%1.d"}, + {__LINE__, 0x0a256162, "a256162", "%x"}, + {__LINE__, -0x05c87b0, "-6064048", "%0ld"}, + {__LINE__, 0xfffffda1, "fffffda1", "%x"}, + {__LINE__, 0x000034b7, "13495", "%d"}, + {__LINE__, -0x6d1a8918, "-1830455576", "%.1d"}, + {__LINE__, 0x00022290, "139920", "%05ld"}, + {__LINE__, 0x00000000, "0", "%-x"}, + {__LINE__, -0x1f8eafce, "-529444814", "%4.d"}, + {__LINE__, -0x7bf32808, "-2079533064", "% ld"}, + {__LINE__, 0x0000023c, "23c", "% x"}, + {__LINE__, 0x00000039, "39", "%X"}, + {__LINE__, -0x000556d, "-21869", "%ld"}, + {__LINE__, -0x00000ca, "-202", "%ld"}, + {__LINE__, 0x00002e28, "11816", "%-d"}, + {__LINE__, 0x00000011, "17", "%-ld"}, + {__LINE__, 0xfffffffb, "FFFFFFFB", "%-X"}, + {__LINE__, 0xfffdd67f, "0XFFFDD67F", "%#X"}, + {__LINE__, -0x000003d, "-61", "%d"}, + {__LINE__, 0x0000bfce, "0XBFCE", "%-#5.2X"}, + {__LINE__, 0xfffffffc, "fffffffc", "%x"}, + {__LINE__, -0x00aea17, "-715287", "%.5d"}, + {__LINE__, -0x02fce89, "-3133065", "%0d"}, + {__LINE__, 0x007f6ed1, "7F6ED1", "%6X"}, + {__LINE__, 0xffffffff, "FFFFFFFF", "%X"}, + {__LINE__, -0x0002761, "-10081", "%#6.2d"}, + {__LINE__, 0x003be8d8, "3BE8D8", "%-X"}, + {__LINE__, 0x00000901, " 2305", "%6.d"}, + {__LINE__, 0x00049916, "0301334", "%3.7ld"}, + {__LINE__, 0x0678ffea, "0X678FFEA", "%#4X"}, + {__LINE__, 0xffffffff, "ffffffff", "%+.0x"}, + {__LINE__, -0x31d2ec61, "-835906657", "%.0d"}, + {__LINE__, 0xfffffffc, "FFFFFFFC", "%0X"}, + {__LINE__, -0x0015588, "-87432", "%d"}, + {__LINE__, 0xffffff3e, "ffffff3e", "%.4x"}, + {__LINE__, 0x0000000a, "10", "%d"}, + {__LINE__, -0x0000003, "-3", "%2.1ld"}, + {__LINE__, -0x000d29a, "-53914", "%ld"}, + {__LINE__, -0x04ccbe0, "-5032928", "%2.2ld"}, + {__LINE__, 0x0357c2a5, "357c2a5", "% 7.x"}, + {__LINE__, 0x00000009, " 9", "% 1.ld"}, + {__LINE__, -0x005908d, "-364685", "%0d"}, + {__LINE__, -0x0000de6, "-3558", "%0ld"}, + {__LINE__, 0x2d395e6b, "+758734443", "%+6d"}, + {__LINE__, 0x001aba58, "1ABA58", "% X"}, + {__LINE__, 0x0ceba5ab, "0XCEBA5AB", "%#.1X"}, + {__LINE__, -0x01a8575, "-1738101", "% 2ld"}, + {__LINE__, 0x0000000d, "13", "%#ld"}, + {__LINE__, 0x00000000, "0", "%d"}, + {__LINE__, 0x4b856407, "4b856407", "%5x"}, + {__LINE__, 0x006f333c, "6F333C", "%-X"}, + {__LINE__, 0x000000d0, "d0", "%x"}, + {__LINE__, 0x2ecf0d4e, "2ecf0d4e", "%0x"}, + {__LINE__, -0x0000188, "-0000392", "%#1.7ld"}, + {__LINE__, -0x0000037, "-55", "%d"}, + {__LINE__, 0xfffffff1, "FFFFFFF1", "% .1X"}, + {__LINE__, 0x00000001, "0x1", "%#3x"}, + {__LINE__, 0x000005ed, "5ED", "%3X"}, + {__LINE__, 0x031183a3, "31183A3", "%X"}, + {__LINE__, 0xffffffff, "ffffffff", "%x"}, + {__LINE__, -0x0018695, "-99989", "%-0.5ld"}, + {__LINE__, -0x0000001, "-01", "%0.2ld"}, + {__LINE__, 0x00007822, "7822", "%+x"}, + {__LINE__, 0x00000000, "0", "% x"}, + {__LINE__, 0xfda2461a, "FDA2461A", "%X"}, + {__LINE__, 0x065c50ef, "106713327", "%2.ld"}, + {__LINE__, 0x00000a81, "a81", "% 0x"}, + {__LINE__, -0x000022e, "-558", "%.0ld"}, + {__LINE__, 0x00013d2f, "81199", "%.2ld"}, + {__LINE__, 0xfffffffe, "FFFFFFFE", "%+X"}, + {__LINE__, 0x000015f9, "5625", "%04.ld"}, + {__LINE__, 0x0ab575a2, "179664290", "%0d"}, + {__LINE__, 0x0024d07f, "0X24D07F", "%#X"}, + {__LINE__, 0x07ee35e1, "7EE35E1", "%+X"}, + {__LINE__, -0x00c114c, "-790860", "%#ld"}, + {__LINE__, 0x00003be3, " 15331", "%07.d"}, + {__LINE__, 0xfff87570, "FFF87570", "%4.0X"}, + {__LINE__, -0x14eab419, "-350925849", "%-ld"}, + {__LINE__, -0x000001b, "-27", "%ld"}, + {__LINE__, 0x167ce2ea, "0x167ce2ea", "%#0.x"}, + {__LINE__, -0x00009bc, "-002492", "% 3.6d"}, + {__LINE__, 0xfffffdb9, "0XFFFFFDB9", "%#7.X"}, + {__LINE__, 0x008b2b9d, "8B2B9D", "% X"}, + {__LINE__, 0x000cfec3, "CFEC3", "% 05X"}, + {__LINE__, 0x05970be6, "5970be6", "%x"}, + {__LINE__, 0x00002780, "010112", "%2.6d"}, + {__LINE__, 0x00000005, "+5", "%+d"}, + {__LINE__, 0xfffffffb, "0xfffffffb", "%#3.x"}, + {__LINE__, 0x00000001, "1 ", "%-#5.ld"}, + {__LINE__, -0x0000017, "-23", "%ld"}, + {__LINE__, -0x023b943, "-2341187", "%d"}, + {__LINE__, 0x0000000d, "13", "%ld"}, + {__LINE__, 0x00025a03, "25a03", "% .5x"}, + {__LINE__, 0x3e1ebe24, "3e1ebe24", "% x"}, + {__LINE__, 0x0000013a, "0x13a", "%#5.0x"}, + {__LINE__, 0xfff6f5b3, "FFF6F5B3", "%4.X"}, + {__LINE__, -0x0000a8a, "-2698", "%.3d"}, + {__LINE__, 0x0009dd5f, "9dd5f", "%x"}, + {__LINE__, 0x000003c7, " 3C7", "%04.2X"}, + {__LINE__, 0x1bcfa2f2, "1bcfa2f2", "%+1.x"}, + {__LINE__, 0x0001b4f4, "1b4f4", "%+1.x"}, + {__LINE__, 0x1bc19a90, "465672848", "%#d"}, + {__LINE__, 0xffffffff, "ffffffff", "%+06x"}, + {__LINE__, -0x0000a88, "-2696", "%3.d"}, + {__LINE__, 0x00084a19, "84A19", "%-X"}, + {__LINE__, 0xffe1fc8f, "FFE1FC8F", "%0.1X"}, + {__LINE__, -0x0000a3a, "-2618", "%5d"}, + {__LINE__, -0x0000001, "-1", "%ld"}, + {__LINE__, -0x5c6183d, "-96868413", "%+ld"}, + {__LINE__, -0x0000001, "-1", "%-d"}, + {__LINE__, 0x0000faa8, "64168", "%05.d"}, + {__LINE__, 0x003913ef, "3740655", "%ld"}, + {__LINE__, 0x0dde330a, "232665866", "%#ld"}, + {__LINE__, 0xfcfb1a7b, "FCFB1A7B", "%+X"}, + {__LINE__, 0x07b1a81f, " 129083423", "% 0d"}, + {__LINE__, 0x00000001, "1", "%d"}, + {__LINE__, 0x00000070, " 112", "%5ld"}, + {__LINE__, 0xffff74d9, "ffff74d9", "%x"}, + {__LINE__, -0x001ab93, "-109459", "%03.d"}, + {__LINE__, 0x0eb6b497, "EB6B497", "%+X"}, + {__LINE__, 0x07adbac2, "128826050", "%#0.4d"}, + {__LINE__, 0xff747068, "FF747068", "%6.X"}, + {__LINE__, -0x190ce98f, "-420276623", "%06.6ld"}, + {__LINE__, -0x0000007, "-7", "%.0d"}, + {__LINE__, 0x0001240f, "1240f", "% 3x"}, + {__LINE__, 0x267ae4a0, "645588128", "%-#ld"}, + {__LINE__, -0x0000001, "-1", "% ld"}, + {__LINE__, -0x0000066, "-00102", "%.5ld"}, + {__LINE__, 0x868fa035, "868fa035", "%x"}, + {__LINE__, 0x00000000, " ", "%-4.ld"}, + {__LINE__, 0xfffffe72, "FFFFFE72", "%4.4X"}, + {__LINE__, 0x00a6b4f4, "10925300", "%#ld"}, + {__LINE__, -0x0000002, "-2", "%00.1d"}, + {__LINE__, -0x00000a3, "-163", "%ld"}, + {__LINE__, 0xfffffffe, "fffffffe", "% 3.x"}, + {__LINE__, 0xffe25941, "ffe25941", "% x"}, + {__LINE__, 0x0000a20f, "41487", "%ld"}, + {__LINE__, -0x000009b, "-155", "%#.3ld"}, + {__LINE__, 0x000c5dc1, "810433", "%4.ld"}, + {__LINE__, 0x00003ec4, "16068", "%ld"}, + {__LINE__, 0x000002f3, "0755", "%04ld"}, + {__LINE__, 0xffffff28, "FFFFFF28", "%.5X"}, + {__LINE__, -0x08450ff, "-8671487", "%-3.0ld"}, + {__LINE__, 0x00000092, "92", "%x"}, + {__LINE__, -0x0000317, "-791", "%d"}, + {__LINE__, 0x0000000d, "d", "%x"}, + {__LINE__, 0x00000036, "36", "%x"}, + {__LINE__, -0x112693f, "-17983807", "%ld"}, + {__LINE__, 0x00004226, "16934", "%0.d"}, + {__LINE__, 0x000001ba, " 442", "% ld"}, + {__LINE__, 0xfffffffc, "0XFFFFFFFC", "%#4X"}, + {__LINE__, 0xffdb3d23, "0xffdb3d23", "% #x"}, + {__LINE__, 0x039eb84d, "39EB84D", "%+.6X"}, + {__LINE__, 0xffffc7da, "ffffc7da", "%0x"}, + {__LINE__, 0xfffffdb6, "FFFFFDB6", "%6X"}, + {__LINE__, 0x001b75b0, "1799600", "%ld"}, + {__LINE__, 0xfffffca2, "FFFFFCA2", "%-X"}, + {__LINE__, 0xffffffba, "ffffffba", "%7.4x"}, + {__LINE__, -0x000124e, "-4686", "%3.ld"}, + {__LINE__, -0x000bec0, "-48832", "% 0d"}, + {__LINE__, 0xfffffca0, "fffffca0", "%2.x"}, + {__LINE__, 0x00b2a462, "11707490", "%ld"}, + {__LINE__, 0x0745a647, "745a647", "%+4.x"}, + {__LINE__, 0xfffffbac, "0XFFFFFBAC", "%#3.X"}, + {__LINE__, -0x0000002, "-2", "%-0.1d"}, + {__LINE__, 0x0006cfdb, "446427", "%#5.d"}, + {__LINE__, -0x0000001, "-1", "%d"}, + {__LINE__, 0x043b134e, "43B134E", "%X"}, + {__LINE__, -0x0000015, "-21", "%ld"}, + {__LINE__, -0x0000114, "-276", "%0d"}, + {__LINE__, -0x0004d4a, "-19786", "%-d"}, + {__LINE__, -0x000001c, "-28", "%ld"}, + {__LINE__, -0x0cb89fc, "-13339132", "%ld"}, + {__LINE__, 0xffffffcf, "ffffffcf", "%x"}, + {__LINE__, 0xf6d2387a, "f6d2387a", "%-x"}, + {__LINE__, -0x00000cd, "-205", "%#ld"}, + {__LINE__, 0x00000000, "0", "%#x"}, + {__LINE__, 0xfffffc81, "fffffc81", "%x"}, + {__LINE__, 0x00000000, " 0", "% ld"}, + {__LINE__, 0x00024fb5, "24fb5", "%x"}, + {__LINE__, 0x000012fa, "12FA", "%X"}, + {__LINE__, 0x0318ce7c, "51957372", "%0d"}, + {__LINE__, 0x02280a99, "2280a99", "%-x"}, + {__LINE__, 0xffffff5c, "FFFFFF5C", "%2.X"}, + {__LINE__, -0x000002e, " -46", "%7ld"}, + {__LINE__, -0x04a73e8, "-4879336", "%4ld"}, + {__LINE__, 0x000007f3, "7f3", "% 1.3x"}, + {__LINE__, 0x00000114, "0x114", "%#x"}, + {__LINE__, 0x0000030c, "30c", "%-x"}, + {__LINE__, 0x001dd0dd, "0x1dd0dd", "%+#5.5x"}, + {__LINE__, 0xfff23de3, "fff23de3", "%-x"}, + {__LINE__, -0x0178f9c, "-1544092", "%d"}, + {__LINE__, 0x02ded8da, "2DED8DA", "%X"}, + {__LINE__, -0x0991672, "-10032754", "%3.d"}, + {__LINE__, 0x6943c150, "6943c150", "%x"}, + {__LINE__, 0xffffc25d, "FFFFC25D", "%4X"}, + {__LINE__, 0x00000c9c, " 0003228", "% .7d"}, + {__LINE__, 0xffffffdf, "FFFFFFDF", "% 6.X"}, + {__LINE__, 0x0001a145, " 1a145", "%6.0x"}, + {__LINE__, 0xffdc832f, "0xffdc832f", "%#.1x"}, + {__LINE__, 0x00699f7b, "699f7b", "%x"}, + {__LINE__, 0xf9575268, "F9575268", "%+0X"}, + {__LINE__, 0x04eb4783, "82528131", "%-ld"}, + {__LINE__, -0x0000023, "-35", "%0ld"}, + {__LINE__, -0x012b08a, "-1224842", "%0d"}, + {__LINE__, 0xffffb587, "FFFFB587", "%X"}, + {__LINE__, 0xffffffe9, "ffffffe9", "%x"}, + {__LINE__, 0x006b5596, " 7034262", "% 0d"}, + {__LINE__, 0x004d0d2a, "+5049642", "%+ld"}, + {__LINE__, -0x002a099, "-172185", "%d"}, + {__LINE__, 0x00000224, "548", "%ld"}, + {__LINE__, 0x03e0cca8, "65064104", "%d"}, + {__LINE__, -0x7bb389d, "-129710237", "%.3ld"}, + {__LINE__, 0xffc630f5, "ffc630f5", "%-07.0x"}, + {__LINE__, 0xfffffff8, "FFFFFFF8", "%X"}, + {__LINE__, 0x0030225b, "3154523", "%#d"}, + {__LINE__, 0xff7f4e28, "ff7f4e28", "%+4.x"}, + {__LINE__, 0x14ee154d, "+351147341", "%+ld"}, + {__LINE__, 0x00000003, "03", "%.2d"}, + {__LINE__, 0xfe0359d6, "fe0359d6", "%x"}, + {__LINE__, 0x002b71c3, "2847171", "%d"}, + {__LINE__, 0x055c4d4a, "55C4D4A", "%-X"}, + {__LINE__, 0xfffb17d2, "fffb17d2", "% .0x"}, + {__LINE__, 0x00000002, "2", "%x"}, + {__LINE__, 0x000018e1, "18E1", "%1.1X"}, + {__LINE__, 0x00000331, "817", "%0d"}, + {__LINE__, -0x0000982, "-2434 ", "%-06.ld"}, + {__LINE__, -0x0000168, "-0000360", "%.7ld"}, + {__LINE__, 0xffffffe9, "FFFFFFE9", "%-0.4X"}, + {__LINE__, 0xe42084ef, "E42084EF", "%X"}, + {__LINE__, 0x000000aa, "aa", "%x"}, + {__LINE__, -0xe8b98b5, "-244029621", "%+d"}, + {__LINE__, -0x00000d9, "-217", "%+2.d"}, + {__LINE__, 0x0000027b, "27B", "%+.0X"}, + {__LINE__, 0x002e52db, "3035867", "%-ld"}, + {__LINE__, -0x00e2ba8, "-928680", "%0.2ld"}, + {__LINE__, 0x003b74d8, "3B74D8", "%X"}, + {__LINE__, -0x000006f, "-111", "%+ld"}, + {__LINE__, 0xf8507e22, "0XF8507E22", "%-#X"}, + {__LINE__, 0x0629f4c0, "103412928", "%-ld"}, + {__LINE__, -0x000002d, "-45", "%0d"}, + {__LINE__, -0x0001db0, "-7600", "%d"}, + {__LINE__, -0x00001bb, " -443", "%5.ld"}, + {__LINE__, 0xffffd2b9, "ffffd2b9", "%+x"}, + {__LINE__, 0xffffe685, "0XFFFFE685", "%#0.7X"}, + {__LINE__, 0x0000a4ce, "A4CE", "% X"}, + {__LINE__, 0xffffffff, "FFFFFFFF", "%X"}, + {__LINE__, 0x0000000c, "C", "% X"}, + {__LINE__, 0xfff59369, "FFF59369", "%7X"}, + {__LINE__, 0x00000156, " 156", "%5.X"}, + {__LINE__, 0x02833aa9, "+42154665", "%+2d"}, + {__LINE__, 0x0004a8f3, "+305395", "%+d"}, + {__LINE__, 0x01a09267, "1A09267", "%0.3X"}, + {__LINE__, 0x000004f1, "4f1", "%x"}, + {__LINE__, 0x00000005, "5", "%x"}, + {__LINE__, 0x00000119, " 0281", "% #2.4ld"}, + {__LINE__, -0x0001a2a, "-6698", "%ld"}, + {__LINE__, 0xa3633a57, "A3633A57", "%7X"}, + {__LINE__, -0x10bd2970, "-280832368", "%-d"}, + {__LINE__, 0xffff9c38, "ffff9c38", "%x"}, + {__LINE__, 0xffdbe81e, "ffdbe81e", "%+6.7x"}, + {__LINE__, 0xffffffe7, "FFFFFFE7", "%-3.0X"}, + {__LINE__, 0x0002f6d6, "2f6d6", "%5.x"}, + {__LINE__, 0xffffffff, "ffffffff", "% x"}, + {__LINE__, 0xffc2c07a, "ffc2c07a", "%0.x"}, + {__LINE__, 0xfcc0e13a, "FCC0E13A", "%X"}, + {__LINE__, 0x0000000d, "d", "% x"}, + {__LINE__, 0x0016cac8, "16CAC8", "%-0.3X"}, + {__LINE__, 0x00945dc1, "9723329", "%2d"}, + {__LINE__, 0xf19a1cd1, "F19A1CD1", "%-X"}, + {__LINE__, 0x00000003, "+3", "%+ld"}, + {__LINE__, 0x00000773, "1907", "%ld"}, + {__LINE__, 0x000263e6, "263e6", "%-x"}, + {__LINE__, -0x0238b55, "-2329429", "%d"}, + {__LINE__, 0x00006c56, "6C56", "%X"}, + {__LINE__, 0x0000f34b, "62283", "%#.0ld"}, + {__LINE__, -0x1620e21, "-23203361", "%0ld"}, + {__LINE__, 0xf9257d6a, "f9257d6a", "%2x"}, + {__LINE__, -0x00ed463, "-971875", "%03.d"}, + {__LINE__, 0x02bb94c6, "45847750", "%d"}, + {__LINE__, 0xffffd20c, "0XFFFFD20C", "%#X"}, + {__LINE__, 0x000087d8, "34776", "%.0d"}, + {__LINE__, 0xfffffffc, "FFFFFFFC", "%+7.X"}, + {__LINE__, -0x0009cd5, "-40149", "%-ld"}, + {__LINE__, 0xfffff85e, "FFFFF85E", "% 0.7X"}, + {__LINE__, -0x0000006, " -6", "%+#6.ld"}, + {__LINE__, 0x007ff453, "0x7ff453", "%-#1.x"}, + {__LINE__, 0xffffffe6, "FFFFFFE6", "%2X"}, + {__LINE__, 0x0000001f, "0X1F", "%#4X"}, + {__LINE__, -0x001c157, "-115031", "%.3ld"}, + {__LINE__, 0x00096e06, "617990", "%3.1ld"}, + {__LINE__, 0x0f77271d, "259467037", "%d"}, + {__LINE__, -0x0001403, "-5123", "%d"}, + {__LINE__, 0xffffff8a, "ffffff8a", "%x"}, + {__LINE__, -0x000010f, "-271", "%1.ld"}, + {__LINE__, 0xe573cc4a, "E573CC4A", "%0X"}, + {__LINE__, -0x0000d88, "-3464", "%-d"}, + {__LINE__, 0x0000000e, "14", "%#ld"}, + {__LINE__, -0x000345b, "-13403", "%#d"}, + {__LINE__, 0x00000602, "602", "%+x"}, + {__LINE__, 0x00000011, "17", "%#d"}, + {__LINE__, -0x0000002, "-2", "%+d"}, + {__LINE__, 0x030fe6e6, "51373798", "%d"}, + {__LINE__, 0x00000007, "007", "%.3X"}, + {__LINE__, 0x0001abe0, "0X1ABE0", "%+#.4X"}, + {__LINE__, 0xfffffdb7, "fffffdb7", "%-6x"}, + {__LINE__, 0xffffff8d, "FFFFFF8D", "% 02.2X"}, + {__LINE__, 0xfff12126, "FFF12126", "%X"}, + {__LINE__, 0x0208e32d, "34136877", "%.2d"}, + {__LINE__, 0x11c04b55, "297814869", "%ld"}, + {__LINE__, 0xff1e5ca3, "0xff1e5ca3", "%#x"}, + {__LINE__, 0xf9643f09, "f9643f09", "%x"}, + {__LINE__, -0x003c6da, "-247514", "%4d"}, + {__LINE__, 0xff69d0e7, "FF69D0E7", "%-X"}, + {__LINE__, 0x00006ae9, "0x6ae9", "%#x"}, + {__LINE__, -0x005aefe, "-372478", "%d"}, + {__LINE__, 0x0000000d, "00013", "%3.5ld"}, + {__LINE__, 0x00cc185c, "CC185C", "%X"}, + {__LINE__, 0x3dd14d9f, "3DD14D9F", "%1.4X"}, + {__LINE__, -0x4cc2ad3, "-80489171", "%1.d"}, + {__LINE__, 0xfffff79e, "fffff79e", "%x"}, + {__LINE__, 0xfffeeb03, "fffeeb03", "%-1x"}, + {__LINE__, -0x0b1b15c, "-11645276", "%0ld"}, + {__LINE__, -0x06b558d, "-7034253", "%ld"}, + {__LINE__, 0xfffffe61, "fffffe61", "%x"}, + {__LINE__, -0x0000002, "-2", "%d"}, + {__LINE__, -0x0000162, "-000354", "%-5.6d"}, + {__LINE__, 0x01a23780, "27408256", "%d"}, + {__LINE__, 0xffff3a9b, "ffff3a9b", "% x"}, + {__LINE__, 0xfffffdc7, "0xfffffdc7", "%#4.5x"}, + {__LINE__, 0x00000000, "00", "%.2X"}, + {__LINE__, -0x0000006, "-6", "%0ld"}, + {__LINE__, -0x16072c6, "-23098054", "%#5ld"}, + {__LINE__, -0x07152b8, "-7426744", "%01ld"}, + {__LINE__, 0x0000d6c6, "0x0d6c6", "%#1.5x"}, + {__LINE__, 0x00000f7c, "000f7c", "%+04.6x"}, + {__LINE__, 0x00000bd0, "3024", "%4ld"}, + {__LINE__, 0x0000295e, "0x295e", "%#x"}, + {__LINE__, 0x00002e38, "11832", "%.5d"}, + {__LINE__, -0xa2f5de4, "-170876388", "%#d"}, + {__LINE__, -0x001aa36, "-109110", "%-#d"}, + {__LINE__, 0x03012091, "3012091", "%+2.5X"}, + {__LINE__, -0x00009ed, "-002541", "%-0.6ld"}, + {__LINE__, 0x0000001d, "1d", "%x"}, + {__LINE__, 0xffffe315, "FFFFE315", "%+4X"}, + {__LINE__, 0x0000716b, "716B", "%X"}, + {__LINE__, 0xfffb8315, "0xfffb8315", "%+#x"}, + {__LINE__, 0x00000132, "132 ", "%-7.3x"}, + {__LINE__, 0x00000000, "+0000000", "%+1.7d"}, + {__LINE__, 0xe16d27ab, "E16D27AB", "%X"}, + {__LINE__, 0xffffffe5, "ffffffe5", "%+x"}, + {__LINE__, -0x000ab9d, "-43933", "%ld"}, + {__LINE__, 0xffd042d6, "ffd042d6", "%2x"}, + {__LINE__, 0xffffff9b, "ffffff9b", "%x"}, + {__LINE__, 0x000000fb, "0xfb", "% #1.x"}, + {__LINE__, 0x0000000f, " 15", "%#3ld"}, + {__LINE__, 0xfffba2d8, "0XFFFBA2D8", "%-#0.X"}, + {__LINE__, 0xfffffff8, "0XFFFFFFF8", "% #X"}, + {__LINE__, 0x00000000, " ", "%4.ld"}, + {__LINE__, 0x00000002, "2", "%+x"}, + {__LINE__, 0xfffff314, "FFFFF314", "%-X"}, + {__LINE__, 0x00000000, " 0", "% ld"}, + {__LINE__, -0x0000007, "-07", "%3.2ld"}, + {__LINE__, 0xffffdd80, "FFFFDD80", "%.6X"}, + {__LINE__, -0x000001b, "-27", "%-ld"}, + {__LINE__, -0x0000258, "-600", "%-0d"}, + {__LINE__, 0x00016377, "16377", "%3X"}, + {__LINE__, 0x00000001, "1", "%X"}, + {__LINE__, -0xac5d314, "-180736788", "%d"}, + {__LINE__, -0x000036e, "-878", "% ld"}, + {__LINE__, 0xfe346af4, "fe346af4", "%.5x"}, + {__LINE__, 0xffffffe1, "ffffffe1", "%-7.6x"}, + {__LINE__, -0x0a1df07, "-10608391", "%#.5d"}, + {__LINE__, 0x000000c9, " 0XC9", "%+#5.X"}, + {__LINE__, 0xfffffffe, "fffffffe", "%x"}, + {__LINE__, 0xfffffff8, "fffffff8", "%+x"}, + {__LINE__, -0x4517255, "-72446549", "%.6ld"}, + {__LINE__, 0xfffa3670, "FFFA3670", "%-6X"}, + {__LINE__, 0xffffffe0, "ffffffe0", "%2.x"}, + {__LINE__, 0xffffffff, "ffffffff", "%0x"}, + {__LINE__, 0x0000230d, "8973", "%#.1d"}, + {__LINE__, 0x00000238, "238", "%0X"}, + {__LINE__, 0xfffce5ab, "FFFCE5AB", "%-X"}, + {__LINE__, 0x0000002c, "2C", "%X"}, + {__LINE__, 0x00000001, " 1", "%4.ld"}, + {__LINE__, 0xffffff8d, "FFFFFF8D", "%-X"}, + {__LINE__, 0x00069c69, "433257", "%06d"}, + {__LINE__, 0xfffffb8d, "0xfffffb8d", "%#x"}, + {__LINE__, 0x000d6a2d, "879149", "%5.5d"}, + {__LINE__, -0x0001fcb, " -8139", "%7.ld"}, + {__LINE__, 0xfff86937, "fff86937", "%-0x"}, + {__LINE__, 0x00059cf9, "367865", "%#.0ld"}, + {__LINE__, 0x02c5d87a, "46520442", "%3.ld"}, + {__LINE__, 0x00000571, "1393", "%ld"}, + {__LINE__, 0x0003c73f, "+247615", "%+4ld"}, + {__LINE__, 0x0000004f, "4f", "% 0x"}, + {__LINE__, 0x00000000, "0", "%-ld"}, + {__LINE__, -0x0e5850f, "-15041807", "% ld"}, + {__LINE__, -0x0000002, " -0002", "%7.4d"}, + {__LINE__, 0xfffffd1b, "fffffd1b", "%05x"}, + {__LINE__, 0x0000041c, " 1052", "% #0.ld"}, + {__LINE__, -0x0000030, "-48", "%d"}, + {__LINE__, -0x0013593, "-79251", "% #d"}, + {__LINE__, 0x00000001, "1", "%+X"}, + {__LINE__, 0x0000056b, " 56b", "%4.x"}, + {__LINE__, -0x0000004, "-4 ", "%-3d"}, + {__LINE__, -0x0000003, "-3", "% ld"}, + {__LINE__, 0xffff7c14, "0XFFFF7C14", "%#X"}, + {__LINE__, 0x02f63dd8, "49692120", "%.7ld"}, + {__LINE__, -0x03c51c9, "-3953097", "%+d"}, + {__LINE__, 0x00ded685, "14603909", "%5.ld"}, + {__LINE__, 0xffffffba, "ffffffba", "%x"}, + {__LINE__, 0x000370e8, "225512", "%#ld"}, + {__LINE__, 0x00000003, "3", "%X"}, + {__LINE__, -0x0024a64, "-150116", "%7.d"}, + {__LINE__, 0xff486ca1, "ff486ca1", "%x"}, + {__LINE__, 0x000001cf, "1CF", "% X"}, + {__LINE__, -0x0002d7e, "-11646", "%-0d"}, + {__LINE__, 0x0a594c65, "a594c65", "%.4x"}, + {__LINE__, -0x0000002, "-0002", "%-5.4ld"}, + {__LINE__, 0x00000000, "", "%00.ld"}, + {__LINE__, 0x00058bc4, "58BC4", "%3X"}, + {__LINE__, 0x0002cf8e, " 184206", "% 0ld"}, + {__LINE__, 0x000009c7, "+2503", "%+4.ld"}, + {__LINE__, 0xed0c984d, "ED0C984D", "%X"}, + {__LINE__, -0x0000058, "-088", "%#.3ld"}, + {__LINE__, 0x0076083d, "0X76083D", "%#6X"}, + {__LINE__, -0x0000001, "-1", "%#d"}, + {__LINE__, -0x6bf1777, "-113186679", "%#ld"}, + {__LINE__, -0x000011f, "-287", "%ld"}, + {__LINE__, 0x001b0e7d, "1B0E7D", "%-4X"}, + {__LINE__, 0x000007ac, " 1964", "%5.ld"}, + {__LINE__, 0x00000005, "5", "%0.d"}, + {__LINE__, -0x00003ba, "-954", "%4.0ld"}, + {__LINE__, 0xfffffffe, "FFFFFFFE", "%X"}, + {__LINE__, 0x00000019, "19", "%x"}, + {__LINE__, 0xffffffe0, "ffffffe0", "%-.6x"}, + {__LINE__, 0x0000012a, " 298", "% d"}, + {__LINE__, 0x00002c74, "2c74", "% 4.3x"}, + {__LINE__, 0x000002d5, "725", "%d"}, + {__LINE__, 0x0001c9a9, "1c9a9", "%-02x"}, + {__LINE__, 0xffc928c8, "ffc928c8", "%x"}, + {__LINE__, -0x03ae51f, "-3859743", "% #ld"}, + {__LINE__, 0x0000526d, "526d", "%x"}, + {__LINE__, -0x00028f0, "-10480", "%5ld"}, + {__LINE__, -0x49b4262, "-77283938", "%+02.0d"}, + {__LINE__, 0x028d37f0, "0x28d37f0", "%+#x"}, + {__LINE__, 0xd66e0af9, "D66E0AF9", "%03.X"}, + {__LINE__, -0x00c7707, "-816903", "%0d"}, + {__LINE__, -0x00000d0, "-208", "%-.1ld"}, + {__LINE__, 0x0000c328, "49960", "%d"}, + {__LINE__, 0x34cb86f1, "34CB86F1", "%X"}, + {__LINE__, 0x000000f6, "F6", "%+X"}, + {__LINE__, 0x0a3407ec, "0xa3407ec", "%-#x"}, + {__LINE__, 0x00007ebc, "32444", "%#3.d"}, + {__LINE__, 0xfffd3d65, "fffd3d65", "%x"}, + {__LINE__, 0x0007bc82, "0507010", "%0.7d"}, + {__LINE__, 0x0005bb93, "375699", "%ld"}, + {__LINE__, 0x0ec11cc8, "0XEC11CC8", "%+#6.2X"}, + {__LINE__, 0xfffff9c6, "fffff9c6", "%.5x"}, + {__LINE__, 0xff05ab70, "0XFF05AB70", "%-#.3X"}, + {__LINE__, 0x2328b716, "589870870", "%d"}, + {__LINE__, 0x00001a79, "6777", "%.1d"}, + {__LINE__, 0xfffee573, "fffee573", "%0.1x"}, + {__LINE__, -0x0000929, " -2345", "%7.d"}, + {__LINE__, 0xffffff9a, "FFFFFF9A", "%2.X"}, + {__LINE__, 0xfffffe60, "fffffe60", "%.5x"}, + {__LINE__, 0xfffffcd2, "fffffcd2", "%.7x"}, + {__LINE__, 0x00000001, " 001", "%6.3X"}, + {__LINE__, 0x00011e9c, "+73372", "%+0ld"}, + {__LINE__, 0x0000002a, "2a", "% x"}, + {__LINE__, 0xffffffff, "ffffffff", "%.7x"}, + {__LINE__, -0x0008ee8, "-36584", "%ld"}, + {__LINE__, 0x0000001c, "28", "%-d"}, + {__LINE__, 0x000abeda, "704218", "%d"}, + {__LINE__, 0x001347f7, "1347f7", "%3.0x"}, + {__LINE__, 0x0000023e, "574", "%3.3ld"}, + {__LINE__, 0x00000000, "0", "%-ld"}, + {__LINE__, -0x0016de0, "-93664", "%0ld"}, + {__LINE__, 0xffffff45, "FFFFFF45", "% X"}, + {__LINE__, 0x02dfb08c, "48214156", "%#ld"}, + {__LINE__, 0x003d73a2, "3d73a2", "%6.x"}, + {__LINE__, 0x00147da7, "1342887", "%#d"}, + {__LINE__, -0x5ff0f1e, "-100601630", "%-01ld"}, + {__LINE__, -0x000000b, "-11", "%ld"}, + {__LINE__, 0x00000168, " 168", "%+6.x"}, + {__LINE__, 0xffffc28e, "ffffc28e", "%0x"}, + {__LINE__, -0x00003a2, "-930", "%ld"}, + {__LINE__, 0x0002e56f, " 189807", "% 1.ld"}, + {__LINE__, 0x51abf44f, "0x51abf44f", "%#.7x"}, + {__LINE__, 0x00000000, "0", "%d"}, + {__LINE__, 0x9581268f, "9581268F", "%+X"}, + {__LINE__, 0xffffff3b, "FFFFFF3B", "%X"}, + {__LINE__, 0x000083d4, " 33748", "% #d"}, + {__LINE__, 0x00000001, "1", "%x"}, + {__LINE__, -0x000e51f, "-58655", "%.0ld"}, + {__LINE__, 0x0003eacb, "256715", "%6ld"}, + {__LINE__, 0x02be09db, " 46008795", "% 0.ld"}, + {__LINE__, 0xfffffffb, "FFFFFFFB", "% X"}, + {__LINE__, 0x000084f5, " 34037", "%6ld"}, + {__LINE__, -0x1127f99, "-17989529", "%#d"}, + {__LINE__, 0x5f6512de, "5f6512de", "%.7x"}, + {__LINE__, 0x00000001, "1", "%x"}, + {__LINE__, 0xfff74ab5, "fff74ab5", "%x"}, + {__LINE__, 0x0eebeb94, "250342292", "%4.ld"}, + {__LINE__, 0x0001b846, "0x1b846", "%#1x"}, + {__LINE__, 0x001e8c32, "1E8C32", "%2X"}, + {__LINE__, -0x0dae932, "-14346546", "%5.1d"}, + {__LINE__, 0x00000001, "1", "%ld"}, + {__LINE__, -0x0000042, "-66", "%ld"}, + {__LINE__, 0xffffff51, "FFFFFF51", "%0.1X"}, + {__LINE__, 0x00000043, "43", "%x"}, + {__LINE__, 0x04c21f39, " 79830841", "% 0ld"}, + {__LINE__, 0x0000061e, "1566", "%03.3d"}, + {__LINE__, -0x0000003, "-3", "%+d"}, + {__LINE__, 0x015b32db, " 22754011", "% .4d"}, + {__LINE__, 0xfffff181, "FFFFF181", "%-X"}, + {__LINE__, 0x000017ec, "0X17EC", "%#X"}, + {__LINE__, 0x000d703e, "880702", "%#ld"}, + {__LINE__, -0x6d7533a0, "-1836397472", "%ld"}, + {__LINE__, 0x0000008a, " 138", "% d"}, + {__LINE__, -0x7368794b, "-1936226635", "%+#d"}, + {__LINE__, 0x0000000d, "0XD", "%#X"}, + {__LINE__, 0x0000030d, "30D", "% X"}, + {__LINE__, 0xfffff9fe, "FFFFF9FE", "%5X"}, + {__LINE__, 0x00001bb2, "7090", "%d"}, + {__LINE__, 0x041d2a0c, "41D2A0C", "%0X"}, + {__LINE__, 0x00012aee, " 76526", "% d"}, + {__LINE__, 0x0000055f, "55f", "%.1x"}, + {__LINE__, 0xffffffff, "0XFFFFFFFF", "%#1X"}, + {__LINE__, 0x07b0a971, "+129018225", "%+.2ld"}, + {__LINE__, 0xfffffff8, "FFFFFFF8", "%4X"}, + {__LINE__, 0x00000001, "1", "%d"}, + {__LINE__, 0x00000038, "+56", "%+2ld"}, + {__LINE__, 0xfffffc2f, "fffffc2f", "%+7.x"}, + {__LINE__, 0x00004477, "0x4477", "% #x"}, + {__LINE__, 0x0000000d, "0XD", "%+#3X"}, + {__LINE__, 0xf5bf37b1, "F5BF37B1", "%1.X"}, + {__LINE__, 0x1a96f431, "446100529", "%d"}, + {__LINE__, 0x0037edf3, "+3665395", "%+#4.5ld"}, + {__LINE__, 0x0007f2aa, "520874", "%0ld"}, + {__LINE__, -0x00051a3, "-20899", "%+#1.ld"}, + {__LINE__, 0x0000037f, "37F", "%0X"}, + {__LINE__, 0xffffffb4, "ffffffb4", "%x"}, + {__LINE__, -0xd984c00, "-228084736", "%+06ld"}, + {__LINE__, 0xffffe1be, "FFFFE1BE", "%X"}, + {__LINE__, 0xfff20d48, "FFF20D48", "%+6.0X"}, + {__LINE__, 0x000028b7, "0x028b7", "%-#7.5x"}, + {__LINE__, -0x0000001, "-1", "%ld"}, + {__LINE__, 0x03f37945, "66287941", "%d"}, + {__LINE__, 0xffffffed, "0XFFFFFFED", "%+#6.4X"}, + {__LINE__, 0x0000f046, "f046", "%x"}, + {__LINE__, 0xe9772b51, "E9772B51", "%-1.X"}, + {__LINE__, 0xfffffffe, "fffffffe", "% x"}, + {__LINE__, 0xfffffffa, "0xfffffffa", "%#x"}, + {__LINE__, 0x00000015, "15", "%-X"}, + {__LINE__, 0x0000003c, "60", "%#ld"}, + {__LINE__, 0x3103952d, "822318381", "%0.ld"}, + {__LINE__, 0x17ba68bb, "398092475", "%2d"}, + {__LINE__, 0x000006c4, "+1732", "%+.2d"}, + {__LINE__, 0x00000043, "67", "%#d"}, + {__LINE__, 0x0043fb55, "4455253", "%.1ld"}, + {__LINE__, 0x00004b76, "4b76", "% 0x"}, + {__LINE__, 0xfff4c56b, "FFF4C56B", "%7X"}, + {__LINE__, 0x00766055, "766055", "%+5x"}, + {__LINE__, -0x000009f, "-159", "%+d"}, + {__LINE__, 0xffff063f, "ffff063f", "%-2x"}, + {__LINE__, 0x00808fc8, "808fc8", "%x"}, + {__LINE__, 0xffffffff, "ffffffff", "%x"}, + {__LINE__, 0x000000f1, "00000f1", "%+02.7x"}, + {__LINE__, 0x00000002, "2", "%X"}, + {__LINE__, 0x00000000, "000", "%03d"}, + {__LINE__, 0xe63f73b2, "E63F73B2", "% 1X"}, + {__LINE__, 0xfffffffe, "FFFFFFFE", "%5.1X"}, + {__LINE__, 0x0d3fa38b, "222274443", "%1d"}, + {__LINE__, 0x00000000, "0", "% X"}, + {__LINE__, 0x00000001, " 1", "%04.ld"}, + {__LINE__, 0x00000046, "46", "%0X"}, + {__LINE__, 0x0000761f, " 30239", "% ld"}, + {__LINE__, -0x0002517, "-9495", "%+d"}, + {__LINE__, 0x00000156, "156", "% 0x"}, + {__LINE__, 0x1c55eba2, "1c55eba2", "%.6x"}, + {__LINE__, 0x005af80d, "5af80d", "%.6x"}, + {__LINE__, 0x0000002e, "46", "%d"}, + {__LINE__, -0x0007c0f, "-31759", "%ld"}, + {__LINE__, 0xffffe924, "FFFFE924", "%7X"}, + {__LINE__, 0x00000174, "372", "%ld"}, + {__LINE__, 0xffffffc9, "0xffffffc9", "%-#x"}, + {__LINE__, 0x00000020, "+000032", "%+07d"}, + {__LINE__, 0xffffe5d9, "FFFFE5D9", "%4.5X"}, + {__LINE__, 0xffffffa3, "FFFFFFA3", "%+.3X"}, + {__LINE__, 0x00000a03, "+2563", "%+2d"}, + {__LINE__, 0x001b58ca, "+1792202", "%+4d"}, + {__LINE__, 0xffffffc9, "0xffffffc9", "%+#5x"}, + {__LINE__, -0x0000003, "-3", "% d"}, + {__LINE__, 0xfffffffc, "fffffffc", "%+0x"}, + {__LINE__, -0x0000002, "-02", "%.2d"}, + {__LINE__, 0x000243fb, "148475", "%-ld"}, + {__LINE__, 0x00000001, "1", "%0d"}, + {__LINE__, -0x0000672, "-1650", "%ld"}, + {__LINE__, -0x0005413, "-0021523", "% 7.7d"}, + {__LINE__, 0x00000017, "0X17", "%#X"}, + {__LINE__, 0x00000e48, "3656", "%0d"}, + {__LINE__, 0xffe79b38, "ffe79b38", "%-x"}, + {__LINE__, 0xffffff15, "FFFFFF15", "%+6.1X"}, + {__LINE__, 0xfff0272c, "fff0272c", "%-.5x"}, + {__LINE__, -0x00b706f, "-749679", "%1.ld"}, + {__LINE__, 0x0176aeec, "176AEEC", "%.1X"}, + {__LINE__, -0x00005a1, "-1441", "% 0ld"}, + {__LINE__, -0x0000002, "-00002", "%.5d"}, + {__LINE__, -0xd2e6f5d, "-221146973", "%1ld"}, + {__LINE__, -0x0000001, " -001", "%5.3ld"}, + {__LINE__, 0x00000001, " 1", "%05.ld"}, + {__LINE__, 0x0000749e, "+29854", "%+#d"}, + {__LINE__, 0x00000005, "5", "%d"}, + {__LINE__, 0xfa7128b0, "fa7128b0", "%1.x"}, + {__LINE__, 0xffffbbb4, "0XFFFFBBB4", "%#.1X"}, + {__LINE__, 0x0006862f, "427567", "%d"}, + {__LINE__, 0x00000002, "2", "%X"}, + {__LINE__, 0x000005d4, "5d4", "% x"}, + {__LINE__, 0x001387e1, "1279969", "%-05.0ld"}, + {__LINE__, -0x0035a56, "-219734", "%ld"}, + {__LINE__, -0x4064ecc6, "-1080356038", "%ld"}, + {__LINE__, 0x0000360d, "13837", "%0ld"}, + {__LINE__, -0xea1a85b, "-245475419", "%-.2ld"}, + {__LINE__, 0xffffff2d, "FFFFFF2D", "%X"}, + {__LINE__, 0x00000004, " 0004", "%7.4x"}, + {__LINE__, 0x00f72ffd, "F72FFD", "%-X"}, + {__LINE__, 0x00000fa2, "fa2 ", "%-5x"}, + {__LINE__, 0x05ae1f9a, "95297434", "%#ld"}, + {__LINE__, 0x0000bf2e, "48942", "%3.d"}, + {__LINE__, 0x00000000, " ", "%1.X"}, + {__LINE__, 0x01bcb097, "29143191", "%ld"}, + {__LINE__, 0x0000009c, "156 ", "%-4.0d"}, + {__LINE__, 0xfffffffe, "fffffffe", "% x"}, + {__LINE__, 0x00000001, "1 ", "%-6.d"}, + {__LINE__, 0xffffffef, "ffffffef", "%7.x"}, + {__LINE__, 0xfffffec1, "fffffec1", "%.5x"}, + {__LINE__, -0x000000f, "-15", "%-1d"}, + {__LINE__, -0x0008426, "-33830", "%0ld"}, + {__LINE__, 0x005be0ae, "5BE0AE", "% X"}, + {__LINE__, 0x00000009, " 9", "%+7X"}, + {__LINE__, -0x0007e18, "-32280", "%+0.d"}, + {__LINE__, 0x01697ba1, " 23690145", "% 5.ld"}, + {__LINE__, -0x0012042, "-073794", "%7.6d"}, + {__LINE__, 0x04e9bd0d, "4e9bd0d", "%-2x"}, + {__LINE__, 0xffffffd9, "FFFFFFD9", "%.2X"}, + {__LINE__, -0x01b9632, "-1807922", "%.6ld"}, + {__LINE__, 0x000000cd, "205", "%1.ld"}, + {__LINE__, 0x00000000, " ", "% .0d"}, + {__LINE__, 0xff17bbb1, "ff17bbb1", "%x"}, + {__LINE__, -0x00146d7, "-83671", "%+1.ld"}, + {__LINE__, 0xfffff144, "fffff144", "%x"}, + {__LINE__, 0x000f8dbe, "F8DBE", "%3.X"}, + {__LINE__, -0x000381c, "-14364", "%d"}, + {__LINE__, 0x0000001e, " 30", "%4.1d"}, + {__LINE__, 0x00000016, "22", "%-0d"}, + {__LINE__, 0x00000002, "+02", "%+1.2ld"}, + {__LINE__, 0x0000e803, "59395", "%d"}, + {__LINE__, 0xb4c2448d, "b4c2448d", "%-7x"}, + {__LINE__, 0x000e697d, "944509", "%ld"}, + {__LINE__, 0xfffe6e32, "0XFFFE6E32", "%#X"}, + {__LINE__, 0x00000190, " 190", "%+5.X"}, + {__LINE__, -0x03a2219, "-3809817", "%#6.ld"}, + {__LINE__, -0x000003c, "-60 ", "%-#4.ld"}, + {__LINE__, 0x00000000, " ", "% 03.ld"}, + {__LINE__, -0x074f922, "-7665954", "%ld"}, + {__LINE__, -0x00000f0, "-240", "%-ld"}, + {__LINE__, 0xfffe42d2, "0XFFFE42D2", "%#X"}, + {__LINE__, 0x000000dd, "DD", "%X"}, + {__LINE__, 0x00359abe, "359ABE", "%.1X"}, + {__LINE__, 0xffec7bdf, "FFEC7BDF", "%0.1X"}, + {__LINE__, 0x0ecddcba, "248372410", "%-#3.d"}, + {__LINE__, 0x00ad0dbc, "11341244", "%ld"}, + {__LINE__, -0x0000001, "-1", "%-d"}, + {__LINE__, 0x00050841, "50841", "% .5x"}, + {__LINE__, 0x01d359e7, "1d359e7", "%1.x"}, + {__LINE__, 0xff9efaa3, "FF9EFAA3", "%X"}, + {__LINE__, 0x0007ea10, "+518672", "%+ld"}, + {__LINE__, -0x000671c, "-26396", "%1d"}, + {__LINE__, 0xffffff91, "0xffffff91", "%#4.4x"}, + {__LINE__, 0x00000000, "+", "%+0.0ld"}, + {__LINE__, -0x002c53e, "-181566", "%3.d"}, + {__LINE__, 0xffffff56, "ffffff56", "%x"}, + {__LINE__, 0xfffff589, "FFFFF589", "%+X"}, + {__LINE__, 0x00024d13, "24D13", "%-5.2X"}, + {__LINE__, 0x00000000, " 0", "%6.1d"}, + {__LINE__, 0x00a64f33, "A64F33", "%4.4X"}, + {__LINE__, 0xffebb57a, "FFEBB57A", "%5.4X"}, + {__LINE__, 0xfff3b4a0, "fff3b4a0", "%2.x"}, + {__LINE__, 0xffffffd9, "FFFFFFD9", "%.2X"}, + {__LINE__, 0x00d37b84, "13859716", "%d"}, + {__LINE__, 0x00001e85, "0001E85", "%0.7X"}, + {__LINE__, -0x756148f, "-123081871", "% 7.d"}, + {__LINE__, -0x0319339, "-3248953", "%+#.4d"}, + {__LINE__, -0x00798b8, "-497848", "%#5ld"}, + {__LINE__, -0x0000039, "-57", "%0d"}, + {__LINE__, -0x000b1d7, "-45527", "%+0.ld"}, + {__LINE__, 0xff811fba, "FF811FBA", "%+X"}, + {__LINE__, 0x00000042, " 66", "% ld"}, + {__LINE__, 0xfffffe31, "0XFFFFFE31", "%#X"}, + {__LINE__, 0x00000073, "115", "%ld"}, + {__LINE__, 0x45091a39, "0x45091a39", "%+#3.x"}, + {__LINE__, 0x001270f5, "1270f5", "%-5.6x"}, + {__LINE__, 0xfffd91fd, "fffd91fd", "%x"}, + {__LINE__, -0x0817bad, "-8485805", "%#ld"}, + {__LINE__, -0x32ad55f, "-53138783", "%0d"}, + {__LINE__, 0x00003e57, "15959", "%0ld"}, + {__LINE__, -0x0048756, "-296790", "%2.6d"}, + {__LINE__, 0xff90f45f, "FF90F45F", "% X"}, + {__LINE__, 0x0000e454, "0e454", "% .5x"}, + {__LINE__, 0xfffffffc, "fffffffc", "%x"}, + {__LINE__, 0xfffffff7, "FFFFFFF7", "%-2X"}, + {__LINE__, 0xffdabf0e, "FFDABF0E", "%+X"}, + {__LINE__, 0xffffffff, "0XFFFFFFFF", "%#1X"}, + {__LINE__, 0x01516650, "1516650", "% x"}, + {__LINE__, 0xffffff51, "FFFFFF51", "%X"}, + {__LINE__, 0x000000ea, "234", "%00.d"}, + {__LINE__, 0x06db60ca, "115040458", "%5.d"}, + {__LINE__, 0x00000f01, " 3841", "% d"}, + {__LINE__, 0x00000009, "9", "%x"}, + {__LINE__, 0x299b3ba0, "299b3ba0", "%6.7x"}, + {__LINE__, 0x0067f298, "67f298", "%-2.6x"}, + {__LINE__, 0xfffffff5, "FFFFFFF5", "% 2X"}, + {__LINE__, 0x00102ff9, "102FF9", "%-.0X"}, + {__LINE__, 0xffc22393, "0xffc22393", "% #6x"}, + {__LINE__, 0x00007db5, "32181", "%-2ld"}, + {__LINE__, 0x0000b0a3, "0xb0a3", "%#0x"}, + {__LINE__, 0x001ceebb, "1CEEBB", "%-3.4X"}, + {__LINE__, 0x00025101, "0x25101", "%#x"}, + {__LINE__, -0x0000001, "-1", "%0ld"}, + {__LINE__, 0xffffd846, "FFFFD846", "%+2.3X"}, + {__LINE__, 0x00001d79, " 7545", "% 0.1ld"}, + {__LINE__, -0x0000002, " -2", "%5d"}, + {__LINE__, 0xf93b9fdc, "f93b9fdc", "%5x"}, + {__LINE__, 0xffff0021, "FFFF0021", "%6X"}, + {__LINE__, 0x00007cf9, "7cf9", "%3.1x"}, + {__LINE__, -0x0000002, "-2", "%d"}, + {__LINE__, 0x00001d84, "1d84", "%0x"}, + {__LINE__, -0x033201e, "-3350558", "%-2.0d"}, + {__LINE__, 0xfff8a6fb, "FFF8A6FB", "%+.7X"}, + {__LINE__, 0x00006fff, " 28671", "% ld"}, + {__LINE__, -0x02a274a, "-2762570", "%d"}, + {__LINE__, -0x0006628, "-26152", "%00ld"}, + {__LINE__, 0x00000011, "11", "%-x"}, + {__LINE__, -0x0000ed9, "-3801", "%0.ld"}, + {__LINE__, 0xfc5b725d, "FC5B725D", "%X"}, + {__LINE__, 0x3530bd4e, "892386638", "%7.d"}, + {__LINE__, 0x03bb4ff7, "62607351", "%#4.7ld"}, + {__LINE__, 0x00009f86, "9f86", "%x"}, + {__LINE__, 0x0000e727, "59175", "%1d"}, + {__LINE__, 0xfffffdf2, "0XFFFFFDF2", "%#X"}, + {__LINE__, 0x00e60dbf, "0XE60DBF", "% #1.X"}, + {__LINE__, 0xffffffff, "FFFFFFFF", "%+X"}, + {__LINE__, 0x0a16f44e, "0XA16F44E", "%#.0X"}, + {__LINE__, 0x0001f97c, "1f97c", "% x"}, + {__LINE__, 0x0000000d, "13", "%#0ld"}, + {__LINE__, 0x0000002a, "42", "%0ld"}, + {__LINE__, 0x00000007, " +7", "%+5ld"}, + {__LINE__, 0xffffff7f, "FFFFFF7F", "%-X"}, + {__LINE__, 0x000006a6, "1702", "%ld"}, + {__LINE__, 0xfaff2fb1, "faff2fb1", "% x"}, + {__LINE__, 0xffff94ab, "ffff94ab", "%x"}, + {__LINE__, 0x011d611b, "0X11D611B", "%#2.3X"}, + {__LINE__, 0x00000000, " +000", "%+#5.3ld"}, + {__LINE__, -0x87fcc37, "-142593079", "%+d"}, + {__LINE__, -0x0417424, "-4289572", "%+d"}, + {__LINE__, 0x03fabc0c, "3fabc0c", "%x"}, + {__LINE__, 0xfffff880, "fffff880", "%-x"}, + {__LINE__, 0x000002d7, "2d7", "%-02.x"}, + {__LINE__, 0x00003e82, "16002", "%#d"}, + {__LINE__, -0x0003713, "-14099", "% 5d"}, + {__LINE__, -0x0000027, "-39", "%ld"}, + {__LINE__, 0x00013098, "0077976", "%01.7d"}, + {__LINE__, -0x000000c, "-12", "%ld"}, + {__LINE__, -0x00000c0, "-192", "% ld"}, + {__LINE__, 0xffbe9285, "FFBE9285", "%3.X"}, + {__LINE__, 0x00000046, "46", "%x"}, + {__LINE__, 0x00000000, " ", "%+4.x"}, + {__LINE__, 0x00011e72, "73330", "%2.3d"}, + {__LINE__, -0x5276dd7, "-86470103", "%d"}, + {__LINE__, 0x00000013, "19", "%-02.ld"}, + {__LINE__, 0x384118a7, "0X384118A7", "%-#X"}, + {__LINE__, 0x00000000, "0", "% #X"}, + {__LINE__, 0x00000009, "9", "%#ld"}, + {__LINE__, 0x00021145, "135493", "%-ld"}, + {__LINE__, 0xff188a0c, "FF188A0C", "%1.X"}, + {__LINE__, 0xffffffd5, "ffffffd5", "%0x"}, + {__LINE__, 0xfffffffe, "FFFFFFFE", "% 0X"}, + {__LINE__, 0xffff74a3, "ffff74a3", "%x"}, + {__LINE__, 0x01d5a2f8, "1d5a2f8", "%-0.x"}, + {__LINE__, 0xfffffef6, "fffffef6", "%+06.x"}, + {__LINE__, 0x00000001, "1", "%ld"}, + {__LINE__, 0x00000000, "+0", "%+ld"}, + {__LINE__, 0xfffffffc, "fffffffc", "%4.x"}, + {__LINE__, -0x0000344, "-0836", "% .4d"}, + {__LINE__, 0xfffffffd, "FFFFFFFD", "%0X"}, + {__LINE__, 0x000a3121, "667937", "%#4.4ld"}, + {__LINE__, 0x0001e211, "0X1E211", "%#X"}, + {__LINE__, 0x00000c8b, "00C8B", "%05X"}, + {__LINE__, 0xfffffffe, "fffffffe", "%+0x"}, + {__LINE__, 0x001a53ce, "1725390", "%d"}, + {__LINE__, 0x0000057f, "57f ", "%-7.x"}, + {__LINE__, -0x0073e4d, "-474701", "%#ld"}, + {__LINE__, -0x0000002, " -2", "% 3.d"}, + {__LINE__, 0x482dc404, "482DC404", "%X"}, + {__LINE__, 0x0000011c, "11c", "%-.1x"}, + {__LINE__, 0x00000c12, " 3090", "%5.3ld"}, + {__LINE__, 0xfffe1068, "FFFE1068", "%-.4X"}, + {__LINE__, -0x0055e88, "-351880", "%ld"}, + {__LINE__, 0x000c9cef, "+826607", "%+d"}, + {__LINE__, 0xffffffe3, "ffffffe3", "%0x"}, + {__LINE__, -0x0000ee9, "-3817", "%#ld"}, + {__LINE__, 0xff1f6daa, "FF1F6DAA", "%X"}, + {__LINE__, 0xfffffc4e, "FFFFFC4E", "%5.2X"}, + {__LINE__, -0x000040a, "-1034", "%d"}, + {__LINE__, -0x0000085, "-00133", "% 01.5d"}, + {__LINE__, -0x126c797c, "-309098876", "%5.ld"}, + {__LINE__, 0x00000039, " 57", "% d"}, + {__LINE__, 0xfffffff6, "0xfffffff6", "%#5.x"}, + {__LINE__, 0xfffffff4, "fffffff4", "%7.6x"}, + {__LINE__, 0x00000003, "3", "%ld"}, + {__LINE__, 0x0000f8ae, "f8ae", "%.0x"}, + {__LINE__, 0x003ca1cf, "3973583", "%#3.7d"}, + {__LINE__, 0x14687009, "0x14687009", "%+#3x"}, + {__LINE__, 0xfffff1f7, "0xfffff1f7", "%#4.5x"}, + {__LINE__, 0xfffff17a, "fffff17a", "%3.x"}, + {__LINE__, 0x0011ea18, "1174040", "%ld"}, + {__LINE__, -0x0000019, "-25", "%03.d"}, + {__LINE__, 0x00015d72, "0X15D72", "%-#X"}, + {__LINE__, 0xffde4a41, "ffde4a41", "%x"}, + {__LINE__, 0x00006f1f, "28447", "%d"}, + {__LINE__, 0xffffffbd, "0xffffffbd", "%#.1x"}, + {__LINE__, 0x000013b9, "13B9", "%0X"}, + {__LINE__, -0x00001b9, "-00441", "%1.5d"}, + {__LINE__, 0x00000001, "000001", "%-.6d"}, + {__LINE__, 0x7b602d8f, "+2069900687", "%+0ld"}, + {__LINE__, 0x0005e323, "+385827", "%+ld"}, + {__LINE__, 0x00002fce, "2FCE", "%+0X"}, + {__LINE__, 0x0000000e, "0014", "%.4d"}, + {__LINE__, 0xffffffd6, "0XFFFFFFD6", "%#4.X"}, + {__LINE__, 0x037bf361, "58454881", "%5.ld"}, + {__LINE__, 0xffffffff, "FFFFFFFF", "%-7.5X"}, + {__LINE__, -0x016d4dc, "-1496284", "%6ld"}, + {__LINE__, 0x0005be9a, "0x5be9a", "%-#.5x"}, + {__LINE__, 0xffffffff, "ffffffff", "%1x"}, + {__LINE__, 0x00000055, " 85", "% 7.d"}, + {__LINE__, 0xffffffff, "ffffffff", "%x"}, + {__LINE__, -0x0135392, "-1266578", "% ld"}, + {__LINE__, 0x00850f26, "8720166", "%d"}, + {__LINE__, 0xffffee39, "ffffee39", "%0x"}, + {__LINE__, 0x000000dd, "221", "%ld"}, + {__LINE__, 0x00000000, " ", "%6.d"}, + {__LINE__, 0x000000d8, "d8", "%x"}, + {__LINE__, 0x000007ec, "002028", "%4.6ld"}, + {__LINE__, 0x0dab4e67, "0XDAB4E67", "%#X"}, + {__LINE__, 0xfffd7c95, "FFFD7C95", "% 4X"}, + {__LINE__, 0x0009302d, "9302D", "%3.1X"}, + {__LINE__, 0x00000003, " 3", "% d"}, + {__LINE__, 0x0000082f, "82f", "%-x"}, + {__LINE__, 0x0652517f, "652517F", "%-.2X"}, + {__LINE__, -0x1fd8f4e3, "-534312163", "%d"}, + {__LINE__, 0x00000176, "176", "%-x"}, + {__LINE__, 0x00000001, "1", "%ld"}, + {__LINE__, 0x0000001a, "1A", "%X"}, + {__LINE__, -0x0005740, "-22336", "% 0ld"}, + {__LINE__, 0xffffffff, "ffffffff", "%6x"}, + {__LINE__, 0x1b273b80, "0x1b273b80", "%#x"}, + {__LINE__, 0x00000065, "65", "%X"}, + {__LINE__, 0x6a62138a, "1784812426", "%2ld"}, + {__LINE__, 0x000001a2, "1A2", "%X"}, + {__LINE__, -0x002aafd, "-174845", "%02.ld"}, + {__LINE__, 0x00000014, "20", "%d"}, + {__LINE__, -0x00000c7, " -199", "%7.0ld"}, + {__LINE__, 0xff545bfd, "ff545bfd", "%-x"}, + {__LINE__, -0x002beb9, "-179897", "%02.2ld"}, + {__LINE__, -0x00000df, "-223", "% 02.d"}, + {__LINE__, -0x000d569, "-54633", "%1.ld"}, + {__LINE__, 0x0000000e, "14", "%d"}, + {__LINE__, -0x0077407, "-488455", "% ld"}, + {__LINE__, 0x0001be09, "114185", "%ld"}, + {__LINE__, -0x0000514, "-01300", "%+05.5d"}, + {__LINE__, 0xf1fb397f, "f1fb397f", "%+x"}, + {__LINE__, 0xfdb6707c, "FDB6707C", "%X"}, + {__LINE__, 0x03cad111, "3cad111", "%5.x"}, + {__LINE__, -0x022e72f, "-2287407", "%d"}, + {__LINE__, -0xda35f48, "-228810568", "%d"}, + {__LINE__, -0x02d1fd4, "-2957268", "%1.3d"}, + {__LINE__, 0x0000005f, "+95", "%+2d"}, + {__LINE__, 0xfffbf8a1, "FFFBF8A1", "%4.2X"}, + {__LINE__, 0x0000071e, " 71e", "%6.x"}, + {__LINE__, 0x000cc8a5, "+837797", "%+03ld"}, + {__LINE__, 0x000eebe9, " EEBE9", "%6.X"}, + {__LINE__, -0x2896b204, "-680964612", "%d"}, + {__LINE__, -0x09553f8, "-9786360", "%d"}, + {__LINE__, 0x000015d3, "5587", "%0d"}, + {__LINE__, 0x0000056d, " 56d", "%+4.x"}, + {__LINE__, -0x00624ad, "-402605", "% ld"}, + {__LINE__, 0xfea64ba5, "0xfea64ba5", "%#1x"}, + {__LINE__, 0x004f35c6, "5191110", "%-ld"}, + {__LINE__, -0x1441fe4, "-21241828", "%d"}, + {__LINE__, 0xffffff95, "FFFFFF95", "%X"}, + {__LINE__, 0xfffecf81, "FFFECF81", "%-2.X"}, + {__LINE__, -0x00002bb, "-699", "%-d"}, + {__LINE__, -0x0000c29, "-3113", "% 02ld"}, + {__LINE__, 0x000026a3, " 9891", "% .4d"}, + {__LINE__, 0x00000003, "3", "%-x"}, + {__LINE__, 0x000007db, "+02011", "%+.5ld"}, + {__LINE__, 0xfff99558, "FFF99558", "%.1X"}, + {__LINE__, 0x0000fce6, "64742", "%4.0d"}, + {__LINE__, 0x000b7ea2, "753314", "%ld"}, + {__LINE__, 0x13e5df30, "0X13E5DF30", "%#2.X"}, + {__LINE__, 0x006761c4, "06761C4", "%6.7X"}, + {__LINE__, 0xffffff78, "FFFFFF78", "%0.X"}, + {__LINE__, 0xfffff1c0, "0xfffff1c0", "% #x"}, + {__LINE__, -0x034af1c, "-3452700", "% 06.1d"}, + {__LINE__, 0x000000de, "0000222", "%2.7ld"}, + {__LINE__, 0x000003d8, "0x3d8", "%#4.x"}, + {__LINE__, -0x001a1d1, "-106961", "%3.1d"}, + {__LINE__, -0x0000002, "-2", "% d"}, + {__LINE__, 0x00001de6, "+7654", "%+0ld"}, + {__LINE__, 0x00000001, "001", "%#.3d"}, + {__LINE__, -0x53dacf9f, "-1406848927", "%ld"}, + {__LINE__, 0xfffffffc, "fffffffc", "%3.3x"}, + {__LINE__, -0x00950e8, "-610536", "%3.6ld"}, + {__LINE__, 0xff14ade9, "FF14ADE9", "%6X"}, + {__LINE__, 0x012f5284, "0X12F5284", "% #5X"}, + {__LINE__, 0x00005a21, " 5a21", "%5x"}, + {__LINE__, 0x00000638, "1592", "%2.4ld"}, + {__LINE__, -0x063017b, "-6488443", "%ld"}, + {__LINE__, 0x00000000, "0", "% X"}, + {__LINE__, 0xfffe8ef2, "FFFE8EF2", "%+0X"}, + {__LINE__, -0x0001c96, "-7318", "%.3d"}, + {__LINE__, 0x0000ca7d, "51837", "%ld"}, + {__LINE__, 0x00000001, "1", "%+1x"}, + {__LINE__, 0xfffff7bc, "FFFFF7BC", "%X"}, + {__LINE__, -0x03d15e2, "-4003298", "%ld"}, + {__LINE__, 0xfffffffe, "fffffffe", "% x"}, + {__LINE__, 0x00066183, "+418179", "%+7.1ld"}, + {__LINE__, 0xffffef2e, "ffffef2e", "%-2.x"}, + {__LINE__, 0x00000000, " 000", "%04.3x"}, + {__LINE__, -0x000000f, "-15", "% d"}, + {__LINE__, -0x00001f5, "-501", "%d"}, + {__LINE__, 0x0c67f159, "c67f159", "%6.2x"}, + {__LINE__, -0x0000006, "-6", "%.1d"}, + {__LINE__, 0x00000005, "0X5", "%+#1X"}, + {__LINE__, -0x0000002, " -2", "%6.ld"}, + {__LINE__, 0xeb2183ec, "eb2183ec", "% x"}, + {__LINE__, -0x5e12a322, "-1578279714", "%d"}, + {__LINE__, 0x00000001, "0x1", "%#x"}, + {__LINE__, -0x0001ea4, "-7844", "%0d"}, + {__LINE__, -0x19955a3, "-26826147", "%d"}, + {__LINE__, 0x000002a6, "2A6", "%2.X"}, + {__LINE__, 0x00000002, "2", "%X"}, + {__LINE__, 0xfffffeec, "fffffeec", "%0.4x"}, + {__LINE__, 0xfffffffc, "fffffffc", "%0x"}, + {__LINE__, 0xffdfe740, "FFDFE740", "% X"}, + {__LINE__, -0x0b0b3e7, "-11580391", "%.1d"}, + {__LINE__, 0x8e01077c, "8E01077C", "%5.7X"}, + {__LINE__, 0x3b6b6d55, "996896085", "%1.1d"}, + {__LINE__, 0x000016af, "5807", "%ld"}, + {__LINE__, -0x0004900, "-18688", "%d"}, + {__LINE__, 0xfffd77bc, "FFFD77BC", "%X"}, + {__LINE__, 0x004cbd74, "5029236", "%0d"}, + {__LINE__, 0x00000003, " 00003", "%6.5d"}, + {__LINE__, 0x0004720f, "4720f", "% x"}, + {__LINE__, 0x0e44535d, "e44535d", "%4x"}, + {__LINE__, 0xfffffff9, "FFFFFFF9", "%3.3X"}, + {__LINE__, -0x0000005, "-5", "%-d"}, + {__LINE__, 0x000001a6, "00001A6", "%1.7X"}, + {__LINE__, 0x0000004a, "4A", "%-2.X"}, + {__LINE__, 0xfffff249, "FFFFF249", "%X"}, + {__LINE__, 0x00004345, "4345", "%x"}, + {__LINE__, 0x0197041d, "197041d", "%+0x"}, + {__LINE__, 0x0000019a, " 19a", "% 6.x"}, + {__LINE__, 0xfff98376, "FFF98376", "%X"}, + {__LINE__, 0xfc536c41, "FC536C41", "%X"}, + {__LINE__, -0x0008d91, "-36241", "% d"}, + {__LINE__, 0xf058d69d, "f058d69d", "%4.x"}, + {__LINE__, -0x00013ef, "-5103", "%d"}, + {__LINE__, -0x0000003, "-3", "%00ld"}, + {__LINE__, -0x09e80f0, "-10387696", "%+07ld"}, + {__LINE__, -0x0222c15, "-2239509", "%#0.4ld"}, + {__LINE__, 0x00000004, "4", "%0ld"}, + {__LINE__, -0x02c6b0d, "-2910989", "% 0ld"}, + {__LINE__, 0xfffffffe, "FFFFFFFE", "%-4X"}, + {__LINE__, 0xffffff3d, "FFFFFF3D", "%-.0X"}, + {__LINE__, -0x0000015, "-21", "%0d"}, + {__LINE__, 0x131dfc90, "+320732304", "%+d"}, + {__LINE__, 0xffffffff, "ffffffff", "%+x"}, + {__LINE__, 0x000035c8, "13768", "%0ld"}, + {__LINE__, 0x00000033, "0x0033", "%#.4x"}, + {__LINE__, 0x0ce7e8c1, "0xce7e8c1", "%#.7x"}, + {__LINE__, 0xfc18b5d2, "FC18B5D2", "%+.3X"}, + {__LINE__, 0x00000015, "21", "%d"}, + {__LINE__, 0x001a5e7c, "1a5e7c", "%05x"}, + {__LINE__, -0x000fbf0, "-64496", "%ld"}, + {__LINE__, -0x000a4f7, "-042231", "%+2.6d"}, + {__LINE__, -0x0000002, "-002", "%3.3ld"}, + {__LINE__, 0x00728878, "728878", "%0X"}, + {__LINE__, -0x0000002, "-2", "%#.1ld"}, + {__LINE__, -0x00002ef, "-751", "%-0ld"}, + {__LINE__, 0xfffffff7, "fffffff7", "%.3x"}, + {__LINE__, 0x00491867, "0491867", "%.7X"}, + {__LINE__, 0x00000d4d, "3405", "%-ld"}, + {__LINE__, 0x00234e86, "234E86", "%0X"}, + {__LINE__, -0x000001b, "-27", "%ld"}, + {__LINE__, 0x00275255, "2576981", "%.5ld"}, + {__LINE__, 0xfeacc214, "0XFEACC214", "%-#3.X"}, + {__LINE__, -0x0001ce5, "-7397", "%+#2.2ld"}, + {__LINE__, 0x089ddb6a, "144563050", "%d"}, + {__LINE__, 0x003d5a95, "3D5A95", "%1X"}, + {__LINE__, 0x002f0b6d, "3083117", "%d"}, + {__LINE__, 0x1e79e228, "511304232", "%ld"}, + {__LINE__, 0xfffffe6b, "FFFFFE6B", "%+0X"}, + {__LINE__, -0x5104ff3, "-84955123", "%.4ld"}, + {__LINE__, 0xfffff1de, "FFFFF1DE", "%X"}, + {__LINE__, 0x000000cf, "+207", "%+.1ld"}, + {__LINE__, -0x16ad0164, "-380436836", "%+5.d"}, + {__LINE__, 0x001fefa6, "2092966", "%ld"}, + {__LINE__, 0x00000000, "00", "%-.2d"}, + {__LINE__, 0x00195f45, "195f45", "%+0x"}, + {__LINE__, 0x00000001, " 1", "%3.ld"}, + {__LINE__, -0x00001c9, "-457", "% ld"}, + {__LINE__, 0x0002479e, "149406", "%#ld"}, + {__LINE__, 0x001ab18b, "1749387", "%04d"}, + {__LINE__, 0x00000045, "69", "%ld"}, + {__LINE__, 0xfbb13f7d, "fbb13f7d", "%.2x"}, + {__LINE__, 0x03ddc208, "3ddc208", "%x"}, + {__LINE__, 0x00000007, "000007", "%#.6d"}, + {__LINE__, 0x00000000, "0", "%01X"}, + {__LINE__, -0x0023110, "-143632", "%+0.3d"}, + {__LINE__, 0x00000006, "06", "%#.2d"}, + {__LINE__, 0xffff9ec9, "ffff9ec9", "%7x"}, + {__LINE__, -0x08afb12, "-9108242", "% #0.ld"}, + {__LINE__, 0x00000004, "4", "%X"}, + {__LINE__, 0x00059684, "59684", "%x"}, + {__LINE__, 0xfff8d8f5, "FFF8D8F5", "%X"}, + {__LINE__, 0x00b7027d, "+11993725", "%+0.7ld"}, + {__LINE__, 0x0000d814, "55316", "%0ld"}, + {__LINE__, 0x03c95a08, "3c95a08", "%3x"}, + {__LINE__, -0x0b65f9a, "-11952026", "%d"}, + {__LINE__, 0xfd956021, "fd956021", "%0x"}, + {__LINE__, 0x000000ed, "ed", "%0x"}, + {__LINE__, -0x006a80c, "-436236", "%.6ld"}, + {__LINE__, 0xfedb109d, "fedb109d", "%x"}, + {__LINE__, 0x00000001, "1", "%X"}, + {__LINE__, 0x00b3eb71, " 11791217", "% d"}, + {__LINE__, 0x00000016, "16", "%0X"}, + {__LINE__, 0x000001db, " 475", "% ld"}, + {__LINE__, 0x00465eaf, "4611759", "%0d"}, + {__LINE__, 0x0001b423, "1B423", "%0X"}, + {__LINE__, 0x05df95ea, "5df95ea", "%+.5x"}, + {__LINE__, 0x0000000e, " 14", "%7ld"}, + {__LINE__, 0xffffb89e, "ffffb89e", "%+x"}, + {__LINE__, 0x01259918, " 19241240", "% d"}, + {__LINE__, -0x0000f8d, "-003981", "%+#.6d"}, + {__LINE__, 0x00054ae0, "54AE0", "%X"}, + {__LINE__, -0x4589ed4, "-72916692", "% ld"}, + {__LINE__, 0x01017516, "1017516", "%6X"}, + {__LINE__, 0xfffb9f15, "FFFB9F15", "%.2X"}, + {__LINE__, 0x005f8394, "5F8394", "%.2X"}, + {__LINE__, 0xffffffff, "ffffffff", "%x"}, + {__LINE__, -0x0000001, " -1", "%7.1ld"}, + {__LINE__, 0x004367af, "4417455", "%4.ld"}, + {__LINE__, 0xffffff86, "ffffff86", "%4.5x"}, + {__LINE__, -0x0000d17, "-3351", "%2.0ld"}, + {__LINE__, 0xffffff31, "ffffff31", "%.6x"}, + {__LINE__, -0x0000001, "-1", "%ld"}, + {__LINE__, -0x3d785fab, "-1031298987", "%d"}, + {__LINE__, -0x0000002, "-2", "%-0ld"}, + {__LINE__, 0x0000019c, "412", "%d"}, + {__LINE__, -0x0006f54, "-28500", "%d"}, + {__LINE__, 0xfffe3524, "FFFE3524", "% 6.2X"}, + {__LINE__, -0x128d3c00, "-311245824", "%2d"}, + {__LINE__, -0x3be71293, "-1004999315", "%d"}, + {__LINE__, 0x0000659d, "26013", "%d"}, + {__LINE__, 0x2d947538, "+764704056", "%+5d"}, + {__LINE__, 0xffa2a420, "FFA2A420", "%0X"}, + {__LINE__, 0x00000001, "1", "%d"}, + {__LINE__, 0x04477c14, "4477c14", "%.6x"}, + {__LINE__, -0x06e145b, "-7214171", "%d"}, + {__LINE__, 0x00009c01, "39937", "%-.1d"}, + {__LINE__, 0xffde174b, "ffde174b", "% 0x"}, + {__LINE__, 0x0001fbae, "1FBAE", "%01.X"}, + {__LINE__, -0x06b8406, "-7046150", "%d"}, + {__LINE__, 0x000005b8, "005b8", "%05x"}, + {__LINE__, -0x02d780b, "-2979851", "%1d"}, + {__LINE__, 0x00209e26, "2137638", "%2d"}, + {__LINE__, 0x0000041a, "41A", "%0X"}, + {__LINE__, 0x2a77f42c, "2A77F42C", "%02X"}, + {__LINE__, 0x13b6ee14, "+330755604", "%+0d"}, + {__LINE__, -0x0000002, "-2 ", "%-6.d"}, + {__LINE__, -0x0001eee, "-7918", "% #2.ld"}, + {__LINE__, 0xffffffff, "FFFFFFFF", "% .5X"}, + {__LINE__, 0x20d11927, "20d11927", "% 0.x"}, + {__LINE__, -0x0018690, "-99984", "% ld"}, + {__LINE__, 0x000002a0, "2A0", "%-0X"}, + {__LINE__, -0x0000511, "-1297", "%+d"}, + {__LINE__, 0x0e782f31, "242757425", "%-0.ld"}, + {__LINE__, 0x2ce06da0, "752905632", "%#0ld"}, + {__LINE__, 0x00000062, "62 ", "%-07x"}, + {__LINE__, -0x015d9d8, "-1432024", "% 7d"}, + {__LINE__, 0xc9125ea8, "c9125ea8", "%x"}, + {__LINE__, 0x00000006, "6", "%.0ld"}, + {__LINE__, 0x0000019c, "19c", "%x"}, + {__LINE__, 0x00000000, " ", "%6.d"}, + {__LINE__, 0x00043e89, "278153", "%0ld"}, + {__LINE__, 0x3e422abd, "1044523709", "%ld"}, + {__LINE__, 0x0220a75d, "35694429", "%#7.6d"}, + {__LINE__, 0x0000c21c, "C21C", "% X"}, + {__LINE__, 0x000074ab, "29867", "%d"}, + {__LINE__, 0x056b22aa, "56B22AA", "%+0.X"}, + {__LINE__, 0xfffffff9, "FFFFFFF9", "%X"}, + {__LINE__, 0x00000000, "0", "%X"}, + {__LINE__, 0xf094a4f4, "f094a4f4", "%+4.x"}, + {__LINE__, -0x1dad0244, "-497877572", "%d"}, + {__LINE__, 0xffffffff, "FFFFFFFF", "%.1X"}, + {__LINE__, 0xfffffe2d, "fffffe2d", "%7.x"}, + {__LINE__, 0x0000042b, "0x42b", "%#x"}, + {__LINE__, -0x0000001, "-1", "% d"}, + {__LINE__, 0x00e77ef5, " 15171317", "% 7.ld"}, + {__LINE__, 0x00006210, " 25104", "% 06d"}, + {__LINE__, 0x0011187a, "1120378", "%-ld"}, + {__LINE__, -0x000000b, "-11", "% #ld"}, + {__LINE__, 0x14cfaff0, "14cfaff0", "%-2.3x"}, + {__LINE__, 0xffff74ea, "FFFF74EA", "%3.X"}, + {__LINE__, 0x000bfb9d, "bfb9d", "%x"}, + {__LINE__, 0xfffffffe, "FFFFFFFE", "%X"}, + {__LINE__, 0x00014500, "0X14500", "%#4.X"}, + {__LINE__, -0x0000001, "-1", "%#1d"}, + {__LINE__, -0x0000024, "-36", "%-0d"}, + {__LINE__, -0x6528828, "-106072104", "%d"}, + {__LINE__, 0x00000075, " 117", "% #d"}, + {__LINE__, 0xff027d67, "ff027d67", "%0x"}, + {__LINE__, 0x0012b8b3, "12b8b3", "%0.1x"}, + {__LINE__, 0x1070147b, "275780731", "%.6ld"}, + {__LINE__, 0xffffe88e, "FFFFE88E", "%1.3X"}, + {__LINE__, 0xfffffc0d, "fffffc0d", "%00.6x"}, + {__LINE__, 0xffd889f5, "ffd889f5", "%x"}, + {__LINE__, 0x00000470, "1136", "%0ld"}, + {__LINE__, 0x000297f8, "169976", "%ld"}, + {__LINE__, 0xfffffffe, "fffffffe", "%+1x"}, + {__LINE__, 0x0000064a, "64a", "%x"}, + {__LINE__, -0x3d71e43b, "-1030874171", "%1.d"}, + {__LINE__, -0x0000004, "-4 ", "%-3.d"}, + {__LINE__, 0xfffff132, "FFFFF132", "%0.0X"}, + {__LINE__, 0x37b6356f, "37b6356f", "%-x"}, + {__LINE__, 0xffffff7a, "FFFFFF7A", "%.0X"}, + {__LINE__, 0x00023553, "23553", "%x"}, + {__LINE__, 0x39a4c29f, "39a4c29f", "%+5.x"}, + {__LINE__, 0xffffffcc, "ffffffcc", "%x"}, + {__LINE__, 0x0003a83b, "239675", "%#6ld"}, + {__LINE__, 0x00000002, "2", "%0x"}, + {__LINE__, -0x09b4e8c, "-10178188", "%+03.ld"}, + {__LINE__, 0x00000002, "0000002", "%-#.7ld"}, + {__LINE__, 0x00000b5e, "2910", "%-04ld"}, + {__LINE__, -0x0000254, "-596", "%+0d"}, + {__LINE__, 0x00000001, "1", "%+1.x"}, + {__LINE__, 0xf8290d3e, "0XF8290D3E", "% #X"}, + {__LINE__, 0x00000035, "035", "%3.3X"}, + {__LINE__, 0x00131d7d, "131D7D ", "%-7.6X"}, + {__LINE__, 0xffdbb36a, "0xffdbb36a", "%#.6x"}, + {__LINE__, 0xc7275816, "c7275816", "%04.x"}, + {__LINE__, 0x00bae994, "12249492", "%0ld"}, + {__LINE__, 0x00000002, "+2", "%+0.0ld"}, + {__LINE__, 0xffffffff, "ffffffff", "%0x"}, + {__LINE__, 0xffffffe0, "ffffffe0", "%x"}, + {__LINE__, 0x00000036, "36", "%.0X"}, + {__LINE__, 0xfffffffd, "FFFFFFFD", "%1X"}, + {__LINE__, 0xffffffc8, "ffffffc8", "%+x"}, + {__LINE__, 0xe71c3689, "e71c3689", "%x"}, + {__LINE__, 0x00000007, "7 ", "%-3.ld"}, + {__LINE__, 0xffffffff, "ffffffff", "%x"}, + {__LINE__, 0x00030400, "030400", "%-4.6x"}, + {__LINE__, 0xbd8c6fa7, "BD8C6FA7", "%-6X"}, + {__LINE__, 0x00000233, "0000563", "%.7ld"}, + {__LINE__, 0xfef92e35, "fef92e35", "%x"}, + {__LINE__, 0xffff2172, "ffff2172", "%3x"}, + {__LINE__, 0x00001233, "1233", "% X"}, + {__LINE__, -0x000d2e1, "-53985", "%-0d"}, + {__LINE__, -0x3ba8cb5b, "-1000917851", "%0d"}, + {__LINE__, -0x4fefb9a, "-83819418", "%5.ld"}, + {__LINE__, 0xffffe9eb, "FFFFE9EB", "%X"}, + {__LINE__, 0x1e472dc3, "507981251", "%ld"}, + {__LINE__, -0x0077046, "-487494", "%0d"}, + {__LINE__, 0x0002c4c9, "+181449", "%+7.d"}, + {__LINE__, 0xfffffe63, "fffffe63", "%x"}, + {__LINE__, 0xfff5a6db, "FFF5A6DB", "%X"}, + {__LINE__, 0x00174442, "174442", "%-5.0X"}, + {__LINE__, 0x017891d9, "17891D9", "%+5.X"}, + {__LINE__, 0x0261966b, "261966B", "%-X"}, + {__LINE__, -0x3e58614d, "-1045979469", "%ld"}, + {__LINE__, 0x002c4c41, "+2903105", "%+01ld"}, + {__LINE__, 0x0000000c, "012", "%#.3d"}, + {__LINE__, 0x0380f73a, "58783546", "%ld"}, + {__LINE__, 0x000002c7, "0000711", "%2.7ld"}, + {__LINE__, 0x00000002, " 002", "% 04ld"}, + {__LINE__, 0x00687ca0, "687CA0", "%X"}, + {__LINE__, 0x0000058a, "1418", "%0ld"}, + {__LINE__, -0x0000642, "-1602", "% ld"}, + {__LINE__, 0x0001f4f1, "1F4F1", "%X"}, + {__LINE__, 0x0026d6fc, "+2545404", "%+4.ld"}, + {__LINE__, 0x05dd423c, "+98386492", "%+#ld"}, + {__LINE__, 0x0000782c, "0782C", "%3.5X"}, + {__LINE__, 0xf67d91f8, "f67d91f8", "%-x"}, + {__LINE__, 0xffffcf2d, "ffffcf2d", "%+0x"}, + {__LINE__, -0x00af8a6, "-719014", "%0ld"}, + {__LINE__, -0xe606ef9, "-241200889", "%ld"}, + {__LINE__, 0xfe9d87f7, "fe9d87f7", "%+x"}, + {__LINE__, 0x00000d44, "d44", "% 1.x"}, + {__LINE__, 0x008716f5, "8716f5", "%2x"}, + {__LINE__, 0x000027d4, "27D4", "% X"}, + {__LINE__, 0xfdfd92ea, "FDFD92EA", "%-.4X"}, + {__LINE__, 0xfffe764f, "FFFE764F", "%X"}, + {__LINE__, 0xff699032, "ff699032", "%3.x"}, + {__LINE__, 0xffffffff, "FFFFFFFF", "%-3X"}, + {__LINE__, -0x368ba995, "-915122581", "%+#.6ld"}, + {__LINE__, 0xffffffff, "ffffffff", "%0x"}, + {__LINE__, 0x0000035e, " 862", "% 1d"}, + {__LINE__, 0xfe703e67, "fe703e67", "%07.0x"}, + {__LINE__, 0xfffffffe, "fffffffe", "%4.x"}, + {__LINE__, 0x000e20cf, "e20cf", "%0x"}, + {__LINE__, 0x000063b5, "0X63B5", "%-#X"}, + {__LINE__, 0xfffffffa, "fffffffa", "%+2.x"}, + {__LINE__, 0xffffffff, "FFFFFFFF", "%2.X"}, + {__LINE__, 0xfdfd69da, "FDFD69DA", "%.0X"}, + {__LINE__, 0x00002f61, "0x2f61", "%#3.x"}, + {__LINE__, 0x00000009, "9", "%x"}, + {__LINE__, 0xffffff86, "ffffff86", "%6x"}, + {__LINE__, 0xffffffe4, "ffffffe4", "%0x"}, + {__LINE__, 0x0062e73b, "+6481723", "%+0ld"}, + {__LINE__, -0x2b059130, "-721785136", "%+d"}, + {__LINE__, -0x0000073, "-115", "%d"}, + {__LINE__, 0xf596efcf, "f596efcf", "%x"}, + {__LINE__, 0x0002f1cb, "2F1CB", "%0X"}, + {__LINE__, -0x0000635, "-01589", "%.5ld"}, + {__LINE__, 0xfffa011a, "FFFA011A", "%0X"}, + {__LINE__, 0x00000047, " 47", "%4X"}, + {__LINE__, 0xfffffbc0, "FFFFFBC0", "%5.2X"}, + {__LINE__, 0x00000015, "15", "% X"}, + {__LINE__, 0x002a87a6, "2a87a6", "%.1x"}, + {__LINE__, -0x000069c, "-1692", "%+ld"}, + {__LINE__, 0xfffffffd, "fffffffd", "%+x"}, + {__LINE__, 0xfeda51f5, "FEDA51F5", "%0X"}, + {__LINE__, 0x001781a4, "1781a4", "%-0x"}, + {__LINE__, 0xe77358d4, "e77358d4", "%+0x"}, + {__LINE__, 0x00000084, " 84", "%3.x"}, + {__LINE__, 0xffff0cb2, "FFFF0CB2", "%+5.X"}, + {__LINE__, 0x0009fa17, "653847", "%0.0ld"}, + {__LINE__, -0x0000c92, "-3218", "%d"}, + {__LINE__, 0x0004ebbb, "4ebbb", "%x"}, + {__LINE__, 0x2e72568c, "+779245196", "%+#ld"}, + {__LINE__, 0xffae86a7, "ffae86a7", "%.7x"}, + {__LINE__, 0xffdf3f04, "ffdf3f04", "%x"}, + {__LINE__, 0x000000c7, "199", "%2.d"}, + {__LINE__, -0x0004a55, "-19029", "%ld"}, + {__LINE__, 0x00564ef9, "5656313", "%.6ld"}, + {__LINE__, 0xffffc205, "ffffc205", "%+.0x"}, + {__LINE__, -0x135085d0, "-324044240", "%#.5d"}, + {__LINE__, 0xfffffffe, "fffffffe", "%-7x"}, + {__LINE__, -0x0000003, "-3", "%.1ld"}, + {__LINE__, 0xffda9e0b, "ffda9e0b", "%+0x"}, + {__LINE__, 0x00000001, "1", "%d"}, + {__LINE__, 0xffffffda, "ffffffda", "%.6x"}, + {__LINE__, 0x5efdb3d1, "+1593684945", "%+0d"}, + {__LINE__, -0x00000ca, "-202", "% 4.d"}, + {__LINE__, -0x0014433, "-82995", "%.3d"}, + {__LINE__, -0x0000002, "-000002", "% .6ld"}, + {__LINE__, 0x24aac879, "615172217", "%.7d"}, + {__LINE__, -0x1db3c1d, "-31144989", "%+#4ld"}, + {__LINE__, 0x000945ad, "0x945ad", "%#1x"}, + {__LINE__, 0x0000000a, "10", "%#.1ld"}, + {__LINE__, -0x002a2e5, "-172773", "%d"}, + {__LINE__, 0xfffffff3, "FFFFFFF3", "%5X"}, + {__LINE__, 0x0000000e, "e", "%+x"}, + {__LINE__, 0x335333dc, "861090780", "%6.d"}, + {__LINE__, 0xfffa16f4, "FFFA16F4", "%6X"}, + {__LINE__, 0xffffc727, "ffffc727", "%+2.x"}, + {__LINE__, 0x00000273, "273", "%x"}, + {__LINE__, -0x000000f, "-15", "%-0d"}, + {__LINE__, 0x00065fbd, "417725", "%5.1d"}, + {__LINE__, 0x00000ed5, "3797", "%d"}, + {__LINE__, 0x30a40024, "30A40024", "%3.7X"}, + {__LINE__, 0x00000054, "84", "%ld"}, + {__LINE__, 0x00000515, "01301", "%.5ld"}, + {__LINE__, 0x00000897, "+02199", "%+.5d"}, + {__LINE__, -0x0001bcc, "-7116", "% ld"}, + {__LINE__, 0xfffffffe, "FFFFFFFE", "%7X"}, + {__LINE__, 0x00000013, "0X13", "%#X"}, + {__LINE__, 0xffffffff, "0xffffffff", "% #x"}, + {__LINE__, 0x00000006, "6", "%ld"}, + {__LINE__, -0x00000c3, "-195", "%1.ld"}, + {__LINE__, -0x005291e, "-338206", "%+d"}, + {__LINE__, 0xffb437a7, "FFB437A7", "%-0X"}, + {__LINE__, 0xff8335cb, "ff8335cb", "%x"}, + {__LINE__, 0x0000006a, "6A", "%2X"}, + {__LINE__, -0x004f629, "-325161", "% .4d"}, + {__LINE__, 0x003698ef, "3578095", "%0ld"}, + {__LINE__, 0x6b5cdf3a, "6b5cdf3a", "%-x"}, + {__LINE__, 0xe9bc5c21, "e9bc5c21", "%+x"}, + {__LINE__, -0x0000001, " -1", "%+6.ld"}, + {__LINE__, 0xfffeffc8, "FFFEFFC8", "%6.X"}, + {__LINE__, 0xff070a73, "ff070a73", "%+.7x"}, + {__LINE__, 0x0023f8ae, "2357422", "%07.4ld"}, + {__LINE__, 0x00000000, "0", "%X"}, + {__LINE__, 0x00000016, "16", "%0X"}, + {__LINE__, -0x000005a, " -90", "%4d"}, + {__LINE__, 0x00000116, "116", "% .3x"}, + {__LINE__, 0xfffed024, "fffed024", "%-x"}, + {__LINE__, 0xfb907950, "fb907950", "%.0x"}, + {__LINE__, 0x0101a1e5, "101a1e5", "%x"}, + {__LINE__, -0x09e7684, "-10385028", "%-#ld"}, + {__LINE__, 0xffffffff, "ffffffff", "%1x"}, + {__LINE__, 0xfffffff6, "fffffff6", "%x"}, + {__LINE__, 0x00000078, "78", "%x"}, + {__LINE__, 0xe6bca9c9, "0XE6BCA9C9", "%#.5X"}, + {__LINE__, -0x0000010, "-16", "%d"}, + {__LINE__, -0x1348f685, "-323548805", "%d"}, + {__LINE__, 0x4dd85797, "1306023831", "%ld"}, + {__LINE__, -0x0000080, "-128", "%.2d"}, + {__LINE__, -0xaed13a7, "-183309223", "%+ld"}, + {__LINE__, -0x000002f, "-47", "% d"}, + {__LINE__, -0x679911f, "-108630303", "%ld"}, + {__LINE__, 0xfffff221, "FFFFF221", "% .7X"}, + {__LINE__, 0x0007476c, "7476c", "%x"}, + {__LINE__, 0xffffe02a, "0xffffe02a", "%#7x"}, + {__LINE__, 0x00000ebb, "0XEBB", "%#X"}, + {__LINE__, 0xffffffff, "0XFFFFFFFF", "%-#X"}, + {__LINE__, 0x00281896, "281896", "%+1.x"}, + {__LINE__, 0x0000085d, " 0X85D", "%#6.3X"}, + {__LINE__, 0x000112bf, "112BF", "%X"}, + {__LINE__, 0xf5518fbf, "F5518FBF", "% X"}, + {__LINE__, -0x00c945b, "-824411", "%ld"}, + {__LINE__, 0x00001b43, "0x1b43", "%-#x"}, + {__LINE__, -0x5872a246, "-1483907654", "%#7.7ld"}, + {__LINE__, 0x00001f96, "8086", "%#d"}, + {__LINE__, 0xfffffff9, "FFFFFFF9", "%X"}, + {__LINE__, -0x006e244, "-451140", "% .3d"}, + {__LINE__, -0x0382188, "-3678600", "%0.2ld"}, + {__LINE__, 0x00000006, "6", "%-x"}, + {__LINE__, 0x00000036, "54 ", "%-5.d"}, + {__LINE__, -0x0000439, " -1081", "%#6.3ld"}, + {__LINE__, 0xfff911c6, "FFF911C6", "%X"}, + {__LINE__, -0x03454dc, "-3429596", "%d"}, + {__LINE__, -0x0363e8d, "-3554957", "%-.2ld"}, + {__LINE__, 0x00000000, "000", "%.3X"}, + {__LINE__, -0x59bb030f, "-1505428239", "%0ld"}, + {__LINE__, 0x073c94d8, "121410776", "%-d"}, + {__LINE__, -0x001ef94, "-126868", "%5.1ld"}, + {__LINE__, 0xfff65fc5, "FFF65FC5", "%0X"}, + {__LINE__, 0xfffffff4, "fffffff4", "%-x"}, + {__LINE__, 0x00001d83, "07555", "%5.5d"}, + {__LINE__, 0x0012b40a, "1225738", "%ld"}, + {__LINE__, -0x0000006, "-6", "%0ld"}, + {__LINE__, 0xfffffffc, "fffffffc", "%06.0x"}, + {__LINE__, -0x0000002, "-00002", "%#.5ld"}, + {__LINE__, 0xf9932c1d, "f9932c1d", "%-x"}, + {__LINE__, -0x46a31d9, "-74068441", "%.3ld"}, + {__LINE__, 0x000987e9, "987E9", "%1X"}, + {__LINE__, 0x0000000f, "15", "%-ld"}, + {__LINE__, -0x0001c4d, "-7245", "%ld"}, + {__LINE__, -0x007deee, "-515822", "%2.6d"}, + {__LINE__, 0xffffffa2, "FFFFFFA2", "%.6X"}, + {__LINE__, 0x00000001, "1", "%-ld"}, + {__LINE__, 0xfc106bde, "fc106bde", "%.4x"}, + {__LINE__, 0x0002f1f8, "0x2f1f8", "%#7.x"}, + {__LINE__, -0x06a290b, "-6957323", "%-ld"}, + {__LINE__, -0x0000037, " -55", "%4ld"}, + {__LINE__, 0x0000152e, "5422", "%2.d"}, + {__LINE__, 0xfffffcb2, "0XFFFFFCB2", "%#3.X"}, + {__LINE__, 0x1b7ca086, "1b7ca086", "%+x"}, + {__LINE__, 0x00000001, "1", "%ld"}, + {__LINE__, 0x006775fc, "6775fc", "%3.2x"}, + {__LINE__, 0x0000000e, "0XE", "%#3.X"}, + {__LINE__, 0xffffffff, "0xffffffff", "%+#4.x"}, + {__LINE__, 0x00001246, "1246", "%X"}, + {__LINE__, 0xffffaed6, "ffffaed6", "%2x"}, + {__LINE__, -0x3a2dec78, "-976088184", "% 0ld"}, + {__LINE__, 0x0001423b, "1423b", "%01x"}, + {__LINE__, 0x00000007, " 0X7", "%#4.X"}, + {__LINE__, 0x01a7ff5a, "27787098", "%.4ld"}, + {__LINE__, -0x0020d05, "-134405", "%-d"}, + {__LINE__, 0xffffff9a, "ffffff9a", "%-.1x"}, + {__LINE__, 0xffff3557, "ffff3557", "%x"}, + {__LINE__, 0x08828e35, "8828e35", "%+.1x"}, + {__LINE__, 0x000006b2, "001714", "%04.6ld"}, + {__LINE__, 0x00000013, "19", "%.0ld"}, + {__LINE__, -0x00341a5, "-213413", "%#d"}, + {__LINE__, -0x0000043, "-67", "%d"}, + {__LINE__, -0x074dce3, "-7658723", "%.1d"}, + {__LINE__, -0x16de2df3, "-383659507", "% ld"}, + {__LINE__, 0x00000000, "0", "%x"}, + {__LINE__, 0x00000ce7, "ce7", "% 2.1x"}, + {__LINE__, 0xffecd377, "ffecd377", "% 0x"}, + {__LINE__, 0xa4e8465c, "a4e8465c", "% x"}, + {__LINE__, 0x000005e8, "1512", "%d"}, + {__LINE__, 0x00000513, "1299", "%d"}, + {__LINE__, 0xffffffb8, "FFFFFFB8", "%X"}, + {__LINE__, 0x00000003, "00003", "%3.5x"}, + {__LINE__, -0x0000003, "-3 ", "%-3.ld"}, + {__LINE__, 0xad53f01d, "ad53f01d", "%0x"}, + {__LINE__, 0x0f1c1dae, "253500846", "%-#.0d"}, + {__LINE__, 0xff1753c0, "0xff1753c0", "%#3.x"}, + {__LINE__, -0x00389c5, "-231877", "%d"}, + {__LINE__, 0x0000016e, "00016E", "%.6X"}, + {__LINE__, -0x54d3310f, "-1423126799", "%-d"}, + {__LINE__, 0x00000005, "0X5", "%#.1X"}, + {__LINE__, 0xfffffffd, "0XFFFFFFFD", "%#5.3X"}, + {__LINE__, 0xfffffe0e, "fffffe0e", "%-x"}, + {__LINE__, 0xa3c02157, "a3c02157", "%.5x"}, + {__LINE__, 0x00005765, "22373", "%d"}, + {__LINE__, -0x12668cd, "-19294413", "%+ld"}, + {__LINE__, -0x66544da, "-107300058", "%2d"}, + {__LINE__, 0x8646c605, "0X8646C605", "% #X"}, + {__LINE__, 0x000001f5, "1F5", "%X"}, + {__LINE__, 0x01b33ae4, "28523236", "%5ld"}, + {__LINE__, 0x000007ce, "7ce", "%3.x"}, + {__LINE__, 0x00000355, "355", "%X"}, + {__LINE__, 0x000001ce, "1CE", "% X"}, + {__LINE__, 0xfffffccd, "fffffccd", "%7.0x"}, + {__LINE__, -0x0000001, "-1", "%0ld"}, + {__LINE__, 0x000640b6, "640b6", "%5.x"}, + {__LINE__, 0x0038c4ec, "3720428", "%5.4d"}, + {__LINE__, -0x0f10b5e, "-15797086", "%2ld"}, + {__LINE__, 0x256c815f, "256c815f", "%4.x"}, + {__LINE__, 0x00000682, " 1666", "%6ld"}, + {__LINE__, -0x00000ab, "-171", "%2ld"}, + {__LINE__, 0x00000003, "3", "%0d"}, + {__LINE__, 0x063f5075, "63F5075", "%X"}, + {__LINE__, 0xff83b5d0, "FF83B5D0", "%7.X"}, + {__LINE__, 0xffffffec, "ffffffec", "%x"}, + {__LINE__, 0xffb551c4, "0XFFB551C4", "% #4.X"}, + {__LINE__, -0x071f057, "-7467095", "%3.7d"}, + {__LINE__, 0x00634879, "6506617", "%#ld"}, + {__LINE__, 0x000001d7, "471", "%d"}, + {__LINE__, 0x00a7da92, "A7DA92", "% .4X"}, + {__LINE__, 0x00000003, "3 ", "%-2.ld"}, + {__LINE__, -0x0000004, "-0004", "%.4d"}, + {__LINE__, -0x01dea32, "-1960498", "% #7.d"}, + {__LINE__, 0x53c4159f, "53C4159F", "%1.7X"}, + {__LINE__, -0x1ad7904, "-28145924", "%#d"}, + {__LINE__, -0x0000a85, "-2693", "%ld"}, + {__LINE__, -0x0000c7d, "-3197", "%d"}, + {__LINE__, 0xfffffffc, "FFFFFFFC", "%+0X"}, + {__LINE__, 0x00000004, "00004", "%1.5d"}, + {__LINE__, 0x0000b761, "46945", "%5ld"}, + {__LINE__, -0x0ee3b2c, "-15612716", "%.6d"}, + {__LINE__, 0x0677a73d, "677a73d", "% x"}, + {__LINE__, -0x5f96020, "-100229152", "% d"}, + {__LINE__, 0x00000000, " 0", "%#7X"}, + {__LINE__, -0x5d1cb5b, "-97635163", "% ld"}, + {__LINE__, -0x00a8d8b, "-691595", "%-0ld"}, + {__LINE__, 0xffffffa2, "FFFFFFA2", "%X"}, + {__LINE__, 0xffffffc9, "FFFFFFC9", "%X"}, + {__LINE__, 0x0b180d35, "B180D35", "%X"}, + {__LINE__, 0xfc0b94ce, "fc0b94ce", "%.1x"}, + {__LINE__, 0x0012cd7b, "12CD7B", "% X"}, + {__LINE__, -0x02f1da8, "-3087784", "%ld"}, + {__LINE__, -0x000a6f9, "-42745", "%-ld"}, + {__LINE__, -0x000006d, "-109 ", "%-6d"}, + {__LINE__, 0x00000001, "+1", "%+0d"}, + {__LINE__, 0x00000001, "1", "%1d"}, + {__LINE__, 0xfffffff8, "fffffff8", "%5.x"}, + {__LINE__, -0x00fc4d7, "-1033431", "%+0.5ld"}, + {__LINE__, 0xffff41b4, "0xffff41b4", "%#x"}, + {__LINE__, -0x0000d37, "-3383", "%ld"}, + {__LINE__, 0xffc4e405, "ffc4e405", "%7x"}, + {__LINE__, 0xffffffb0, "FFFFFFB0", "%04X"}, + {__LINE__, -0x0054477, "-345207", "%01.ld"}, + {__LINE__, 0x00512778, "0x512778", "%#3.0x"}, + {__LINE__, 0x00000007, " 7", "%7.d"}, + {__LINE__, 0x00000008, " 00008", "% .5ld"}, + {__LINE__, 0x000053fd, "21501", "%.4ld"}, + {__LINE__, 0x0000370c, "370c", "%2x"}, + {__LINE__, -0x44670a7, "-71725223", "%3d"}, + {__LINE__, 0x00000064, " 64", "%+5x"}, + {__LINE__, 0xfffffffc, "FFFFFFFC", "%X"}, + {__LINE__, 0xf7bbf094, "f7bbf094", "%3x"}, + {__LINE__, 0xfffeb268, "FFFEB268", "%X"}, + {__LINE__, 0x00001ef0, "1ef0", "%4.x"}, + {__LINE__, 0x05ad9aa4, " 95263396", "% 0.ld"}, + {__LINE__, 0xf294dca7, "F294DCA7", "% 4.X"}, + {__LINE__, -0x0000003, "-000003", "%.6d"}, + {__LINE__, 0xfff57471, "fff57471", "% x"}, + {__LINE__, 0x0a848cfd, "176459005", "%d"}, + {__LINE__, -0x000002e, "-46", "%.0d"}, + {__LINE__, 0x00000051, " +81", "%+5d"}, + {__LINE__, -0x000072b, "-0001835", "%06.7d"}, + {__LINE__, -0x0000002, " -2", "%+#6ld"}, + {__LINE__, 0x00000003, "3", "%x"}, + {__LINE__, 0xfffd1799, "FFFD1799", "%6.X"}, + {__LINE__, 0x00000005, "5", "%ld"}, + {__LINE__, 0x0052138b, "5378955", "%#7.7ld"}, + {__LINE__, 0x000000c0, " 192", "% 7ld"}, + {__LINE__, 0x005b26cd, "5B26CD", "%X"}, + {__LINE__, -0x008df17, "-581399", "%.4ld"}, + {__LINE__, 0xfffffad8, "fffffad8", "%-x"}, + {__LINE__, 0x000006a9, "6a9", "%.0x"}, + {__LINE__, -0x003b7f9, "-243705", "%+0ld"}, + {__LINE__, 0x005d7ea4, "6127268", "%ld"}, + {__LINE__, -0x003ccea, "-249066", "%+.0ld"}, + {__LINE__, 0x0000846f, "846F", "%X"}, + {__LINE__, 0x00004f79, "4F79", "%4.X"}, + {__LINE__, 0xe3e1409c, "E3E1409C", "%-X"}, + {__LINE__, 0x07cce2f4, "7CCE2F4", "%2.X"}, + {__LINE__, 0x004bcc8b, "4967563", "%-.5ld"}, + {__LINE__, 0x7d49665e, "2101962334", "%ld"}, + {__LINE__, 0xfffffffc, "FFFFFFFC", "%.5X"}, + {__LINE__, 0x0000019e, "+000414", "%+.6d"}, + {__LINE__, 0x000027ef, "10223", "%d"}, + {__LINE__, -0x0000261, "-609", "%3ld"}, + {__LINE__, -0x000000e, "-14", "% 2.ld"}, + {__LINE__, 0xff3ba3c7, "ff3ba3c7", "%+5.7x"}, + {__LINE__, 0xffffffc4, "FFFFFFC4", "%-1.X"}, + {__LINE__, 0xfffc5a70, "0xfffc5a70", "%#4.4x"}, + {__LINE__, 0x0000d156, " 53590", "% d"}, + {__LINE__, -0x0486258, "-4743768", "%0ld"}, + {__LINE__, 0x00000000, " ", "% 2.d"}, + {__LINE__, 0x04a0ef9f, "77655967", "%0.6ld"}, + {__LINE__, 0xffffffff, "FFFFFFFF", "%X"}, + {__LINE__, 0x00000019, "0000019", "%.7x"}, + {__LINE__, -0x0058d3a, "-363834", "%ld"}, + {__LINE__, 0xfffff6b3, "fffff6b3", "%x"}, + {__LINE__, 0xfffffff5, "FFFFFFF5", "%.0X"}, + {__LINE__, 0x005e1fcb, "5E1FCB", "%+6X"}, + {__LINE__, 0xffc3c866, "0xffc3c866", "%-#.4x"}, + {__LINE__, 0x00000003, "3", "%X"}, + {__LINE__, 0xffa2e961, "FFA2E961", "%7.4X"}, + {__LINE__, 0x00000029, "41", "%d"}, + {__LINE__, -0x0000035, "-53", "%d"}, + {__LINE__, 0x0001b033, "110643", "%ld"}, + {__LINE__, 0x004dd51b, "5100827", "%-2ld"}, + {__LINE__, -0x000252d, "-9517", "% d"}, + {__LINE__, 0xffffdeb2, "ffffdeb2", "%4.x"}, + {__LINE__, 0xffe94eab, "0xffe94eab", "%#x"}, + {__LINE__, 0xf921edf6, "f921edf6", "%4.2x"}, + {__LINE__, 0x0007df97, "515991", "%4.ld"}, + {__LINE__, 0x00709bc5, "709BC5", "%0X"}, + {__LINE__, -0x0000001, "-1", "%d"}, + {__LINE__, 0xfffffffb, "FFFFFFFB", "%7.3X"}, + {__LINE__, -0x0063f74, "-409460", "%+05.0d"}, + {__LINE__, 0xfffffe9a, "FFFFFE9A", "%.2X"}, + {__LINE__, -0x0001ce2, "-7394", "%.4d"}, + {__LINE__, 0x000038c5, "14533", "%ld"}, + {__LINE__, 0x000000ad, "000ad", "% 5.5x"}, + {__LINE__, 0xffffbcc4, "ffffbcc4", "%-5x"}, + {__LINE__, 0x00000000, "0", "% X"}, + {__LINE__, 0x00372877, "372877", "%.6X"}, + {__LINE__, 0xffffffd7, "FFFFFFD7", "%1.X"}, + {__LINE__, 0xfa58b14b, "fa58b14b", "%x"}, + {__LINE__, 0xfffffd9f, "FFFFFD9F", "%0.7X"}, + {__LINE__, 0x00000710, "+1808", "%+#.2ld"}, + {__LINE__, 0x00000006, " 6", "%5.0ld"}, + {__LINE__, 0x0fba9706, "263886598", "%-6.3d"}, + {__LINE__, 0xffff420a, "ffff420a", "%-2x"}, + {__LINE__, 0x14b9a825, "14b9a825", "%x"}, + {__LINE__, 0x0077ffe4, "77FFE4", "%+.2X"}, + {__LINE__, 0x00067108, "422152 ", "%-7.ld"}, + {__LINE__, 0x00000001, " 00001", "%6.5X"}, + {__LINE__, 0x00000000, "0", "%ld"}, + {__LINE__, -0x0000001, " -1", "% 7ld"}, + {__LINE__, 0x01ddc7a3, "31311779", "%d"}, + {__LINE__, -0x2a92f9b, "-44642203", "% 3ld"}, + {__LINE__, 0xfffffff9, "fffffff9", "%4.6x"}, + {__LINE__, 0x0279a274, "279a274", "%x"}, + {__LINE__, 0x2e37ed8e, "775417230", "%d"}, + {__LINE__, 0x5ba59f58, "1537580888", "%-ld"}, + {__LINE__, 0xffffffe0, "ffffffe0", "%x"}, + {__LINE__, 0x3110ae47, "0X3110AE47", "%+#X"}, + {__LINE__, 0x000e00b9, "917689", "%d"}, + {__LINE__, 0x0000002e, " 0046", "% 7.4d"}, + {__LINE__, 0x00000363, " 867", "%7.d"}, + {__LINE__, -0x000000b, "-0011", "% 0.4d"}, + {__LINE__, 0x000349c8, " 349C8", "% 7.X"}, + {__LINE__, -0x01358f1, "-1267953", "%d"}, + {__LINE__, -0x0039d3c, "-236860", "%-.3d"}, + {__LINE__, 0xffd6de19, "ffd6de19", "%04.0x"}, + {__LINE__, -0x0003c54, "-0015444", "%1.7d"}, + {__LINE__, -0x0000e17, "-3607", "%d"}, + {__LINE__, 0xff0fa3a9, "ff0fa3a9", "%+3x"}, + {__LINE__, 0x00000009, "0000009", "%1.7x"}, + {__LINE__, 0xfffffc81, "FFFFFC81", "% X"}, + {__LINE__, -0x4ef2df5, "-82783733", "%ld"}, + {__LINE__, 0x00000013, "19", "%-ld"}, + {__LINE__, 0x00000000, " 0", "%#7x"}, + {__LINE__, -0x3b9485fd, "-999589373", "%1.d"}, + {__LINE__, -0x0000006, "-00006", "%6.5d"}, + {__LINE__, 0x0000003f, "+63", "%+ld"}, + {__LINE__, 0x000004c5, "1221", "%1.d"}, + {__LINE__, 0x003f8a8c, "4164236", "%d"}, + {__LINE__, -0x000001e, "-30", "%-#3d"}, + {__LINE__, 0xfff9d230, "FFF9D230", "%+07.3X"}, + {__LINE__, 0x0147cf17, "147cf17", "% x"}, + {__LINE__, 0xffffffeb, "ffffffeb", "%x"}, + {__LINE__, 0x0000007d, "00125", "%.5ld"}, + {__LINE__, 0x000fff56, "1048406", "%#3d"}, + {__LINE__, 0x064307a0, " 105056160", "% 0.3ld"}, + {__LINE__, 0x007d2860, "8202336", "%-.2d"}, + {__LINE__, 0x00a05711, "a05711", "%x"}, + {__LINE__, 0x179a7f9e, "0x179a7f9e", "%+#.3x"}, + {__LINE__, 0x00000003, "3", "%X"}, + {__LINE__, -0x0001d7d, "-7549", "%#5d"}, + {__LINE__, 0x0025d3f2, "+2479090", "%+2.d"}, + {__LINE__, -0x0000001, "-1", "%#ld"}, + {__LINE__, 0xfffffdd5, "fffffdd5", "%.7x"}, + {__LINE__, 0x003462b9, "3433145", "%d"}, + {__LINE__, 0x00005a81, "23169", "%d"}, + {__LINE__, 0xfffffcdb, "fffffcdb", "%-x"}, + {__LINE__, 0x00000000, "0", "%d"}, + {__LINE__, 0xfff22085, "fff22085", "%x"}, + {__LINE__, 0x00016d72, " 93554", "% d"}, + {__LINE__, -0x0000002, "-2", "%0ld"}, + {__LINE__, 0xfffffd21, "fffffd21", "% 0x"}, + {__LINE__, 0x00000931, "2353", "%ld"}, + {__LINE__, 0x00010021, "10021", "%5.1X"}, + {__LINE__, -0x000007e, "-126", "%1.0ld"}, + {__LINE__, 0xfffffff8, "fffffff8", "%01.x"}, + {__LINE__, -0x001d148, "-119112", "%d"}, + {__LINE__, -0x0000008, "-8", "%ld"}, + {__LINE__, -0x000038e, "-910", "% ld"}, + {__LINE__, 0x0039e3e5, "3793893", "%0.1d"}, + {__LINE__, 0x0038fb8e, "38FB8E", "%.2X"}, + {__LINE__, -0x002dafa, "-187130", "%ld"}, + {__LINE__, -0xbec275b, "-200025947", "%ld"}, + {__LINE__, 0x1a32f999, "1A32F999", "%05.4X"}, + {__LINE__, 0x0000002f, "2f", "%x"}, + {__LINE__, -0x0001a5b, "-6747", "%+3.ld"}, + {__LINE__, 0x00000762, " 1890", "%7.ld"}, + {__LINE__, 0x000000c2, "000194", "%0.6ld"}, + {__LINE__, 0xffff002b, "FFFF002B", "%.4X"}, + {__LINE__, -0x0019dae, "-105902", "% .2d"}, + {__LINE__, 0x0000125f, "125f", "%+1x"}, + {__LINE__, -0x000b688, " -46728", "%#7.d"}, + {__LINE__, 0x000401ca, "401ca", "%0x"}, + {__LINE__, 0x00000192, "402", "%#ld"}, + {__LINE__, 0xffffffff, "ffffffff", "%x"}, + {__LINE__, 0x1e505a0c, "0X1E505A0C", "%#1.3X"}, + {__LINE__, 0x000f8f6c, "1019756", "%d"}, + {__LINE__, 0x0004a296, "4a296", "%5.2x"}, + {__LINE__, 0x00000003, "3", "% x"}, + {__LINE__, -0x000d44c, "-54348", "%#ld"}, + {__LINE__, 0x00009ae2, "39650", "%-d"}, + {__LINE__, 0xffffff3a, "FFFFFF3A", "% .7X"}, + {__LINE__, 0x0042350b, "4338955", "%d"}, + {__LINE__, 0x000071af, "71af", "%+.4x"}, + {__LINE__, 0x00000001, "0x1", "%#0x"}, + {__LINE__, 0x00000033, "00051", "%3.5d"}, + {__LINE__, 0x00000001, "1", "%X"}, + {__LINE__, -0xebfb0e7, "-247443687", "%+3ld"}, + {__LINE__, 0x00000027, "27 ", "%-07.X"}, + {__LINE__, -0x0004837, "-18487", "%01d"}, + {__LINE__, 0x1a8c53da, "1A8C53DA", "%X"}, + {__LINE__, -0x0bdb8a9, "-12433577", "%d"}, + {__LINE__, -0x0000098, "-152", "%#ld"}, + {__LINE__, -0x003b554, "-243028", "%d"}, + {__LINE__, 0x000000cb, "00000CB", "%+.7X"}, + {__LINE__, 0xfffffeda, "FFFFFEDA", "%X"}, + {__LINE__, -0x0000010, "-16", "%-#d"}, + {__LINE__, 0x00000006, "+6", "%+d"}, + {__LINE__, 0x003c294a, "+3942730", "%+4.ld"}, + {__LINE__, 0x00000009, "9", "%ld"}, + {__LINE__, 0xfbbdd2bc, "FBBDD2BC", "% X"}, + {__LINE__, 0x71c86678, "+1908958840", "%+0.6ld"}, + {__LINE__, -0x0b49bff, "-11836415", "% 6.ld"}, + {__LINE__, 0xfff416be, "fff416be", "%3.x"}, + {__LINE__, 0x00000077, "+119", "%+d"}, + {__LINE__, 0xfff1cfaa, "FFF1CFAA", "% 0.2X"}, + {__LINE__, 0xfffffffb, "fffffffb", "% x"}, + {__LINE__, 0x0000127e, "4734", "%d"}, + {__LINE__, 0x00107ad2, "107ad2", "%+2.6x"}, + {__LINE__, 0x0006b8e9, "440553", "%-#d"}, + {__LINE__, 0x0000eb6f, "eb6f", "% x"}, + {__LINE__, 0x00001f18, "7960", "%0d"}, + {__LINE__, 0xfffff225, "fffff225", "%1.5x"}, + {__LINE__, 0xfffffff9, "fffffff9", "%+6.x"}, + {__LINE__, 0x000005b9, "01465", "%.5ld"}, + {__LINE__, 0xfffc26b8, "FFFC26B8", "% X"}, + {__LINE__, 0x540d580d, "0X540D580D", "%+#X"}, + {__LINE__, 0x05ad7094, "5AD7094", "% 4.7X"}, + {__LINE__, -0x000019b, "-411", "% d"}, + {__LINE__, 0x00000006, "000006", "%6.6ld"}, + {__LINE__, 0x0000026a, "+618", "%+ld"}, + {__LINE__, 0x0000000b, "11", "%ld"}, + {__LINE__, -0x26985d5, "-40469973", "%#.0ld"}, + {__LINE__, 0x0000007d, "7D", "%+02.0X"}, + {__LINE__, -0x0079ddf, "-0499167", "%+.7ld"}, + {__LINE__, 0x0000375f, "14175", "%0d"}, + {__LINE__, -0x18de7f99, "-417234841", "%ld"}, + {__LINE__, -0x00507cc, "-329676", "%.0d"}, + {__LINE__, 0x000029d5, "29D5", "%-2.1X"}, + {__LINE__, 0x0000328c, "328C", "%-3.X"}, + {__LINE__, 0x016f6234, "24076852", "%3.ld"}, + {__LINE__, 0xfffffffc, "0XFFFFFFFC", "%#.5X"}, + {__LINE__, 0xfe6163ca, "FE6163CA", "%5.X"}, + {__LINE__, 0xffffffc2, "FFFFFFC2", "%X"}, + {__LINE__, 0x00000087, "+135", "%+ld"}, + {__LINE__, 0x00310166, "310166", "% x"}, + {__LINE__, 0x00e8c871, "15255665", "%d"}, + {__LINE__, 0x000005ee, "1518", "%ld"}, + {__LINE__, 0xfcb24306, "FCB24306", "%X"}, + {__LINE__, 0x0000000b, " 11", "%5ld"}, + {__LINE__, 0x006d11d0, "0x6d11d0", "%#.0x"}, + {__LINE__, 0x0010d416, "1102870", "%#6.5d"}, + {__LINE__, -0x0047cb3, "-294067", "%.3d"}, + {__LINE__, 0x000000c0, "c0", "%x"}, + {__LINE__, 0xffffffff, "FFFFFFFF", "%+X"}, + {__LINE__, -0x000000a, "-10", "%d"}, + {__LINE__, -0x0000007, "-7", "% ld"}, + {__LINE__, 0xfffffffe, "FFFFFFFE", "%-X"}, + {__LINE__, 0x00000014, "0x14", "%-#3.1x"}, + {__LINE__, 0x00003319, "3319", "% x"}, + {__LINE__, 0x00000000, "00", "%+#.2X"}, + {__LINE__, -0x0000009, "-9", "% ld"}, + {__LINE__, -0x0000001, "-1 ", "%-3ld"}, + {__LINE__, 0x00262909, "2500873", "%.5ld"}, + {__LINE__, 0x33e76560, "33e76560", "%7.0x"}, + {__LINE__, -0x0000002, "-2", "%ld"}, + {__LINE__, 0x00035ee9, "35EE9", "%-3.X"}, + {__LINE__, -0x0000235, "-565", "%+4.ld"}, + {__LINE__, -0x3ea63c5, "-65692613", "%d"}, + {__LINE__, 0x00000003, "3", "%ld"}, + {__LINE__, -0x003362f, "-210479", "% d"}, + {__LINE__, -0x1a819f8a, "-444702602", "%.7d"}, + {__LINE__, 0x027a4668, "41567848", "%d"}, + {__LINE__, 0x0002b025, "2b025", "%x"}, + {__LINE__, -0x0000001, "-001", "%.3ld"}, + {__LINE__, 0xfffffff0, "fffffff0", "%-0x"}, + {__LINE__, -0x0000d4f, "-3407", "%-ld"}, + {__LINE__, 0x00000146, " 326", "% 1.d"}, + {__LINE__, -0x0000006, "-6", "%d"}, + {__LINE__, 0xffffffff, "FFFFFFFF", "%06.4X"}, + {__LINE__, 0x376fddae, "+930078126", "%+#0ld"}, + {__LINE__, 0x006d9da3, "6d9da3", "%1.x"}, + {__LINE__, -0x0000f28, "-3880", "%3.2ld"}, + {__LINE__, 0x0006962c, " 431660", "% 5d"}, + {__LINE__, 0x000011db, " 4571", "%6.ld"}, + {__LINE__, 0xffffffa8, "ffffffa8", "%+x"}, + {__LINE__, 0x0d886db3, "D886DB3", "%.7X"}, + {__LINE__, 0x00000000, "000000", "%2.6ld"}, + {__LINE__, 0xfffff6b6, "FFFFF6B6", "% X"}, + {__LINE__, -0x0739068, "-7573608", "%+6.ld"}, + {__LINE__, 0xfba62553, "0XFBA62553", "%#X"}, + {__LINE__, -0x4474bc5, "-71781317", "%-2ld"}, + {__LINE__, 0xffffff95, "FFFFFF95", "% 2.X"}, + {__LINE__, -0x1d0e1ca, "-30466506", "%+.0d"}, + {__LINE__, -0x000f3aa, "-62378", "%ld"}, + {__LINE__, 0x00000026, "26", "%1x"}, + {__LINE__, 0x001c5400, "1856512", "%-#d"}, + {__LINE__, 0x03808442, "3808442", "%X"}, + {__LINE__, -0x0000081, "-129", "%+#ld"}, + {__LINE__, 0x000004ae, " 1198", "% 3.4d"}, + {__LINE__, -0x7f4ed54, "-133492052", "%d"}, + {__LINE__, 0x00000000, "+0", "%+ld"}, + {__LINE__, 0x00000000, "0", "%+x"}, + {__LINE__, 0x004c7e46, "5013062", "%.0d"}, + {__LINE__, -0x647d7a65, "-1685944933", "%0.2d"}, + {__LINE__, 0x00003b59, "15193", "%-#1d"}, + {__LINE__, 0xfff3e64d, "FFF3E64D", "%-X"}, + {__LINE__, 0x00007022, "28706", "%-d"}, + {__LINE__, 0xc28d0ad8, "c28d0ad8", "%+x"}, + {__LINE__, -0x005c208, "-377352", "% ld"}, + {__LINE__, 0x00cfbadc, "13613788", "%d"}, + {__LINE__, 0x000016ea, "5866", "%0d"}, + {__LINE__, 0x00000029, "29", "%X"}, + {__LINE__, 0xffe16813, "0XFFE16813", "%+#X"}, + {__LINE__, 0x00000004, "4", "%0X"}, + {__LINE__, -0x0000096, " -150", "%5d"}, + {__LINE__, 0x00027ac7, "162503", "%ld"}, + {__LINE__, -0x0075de0, "-482784", "%d"}, + {__LINE__, 0x005fcff6, "6279158", "%d"}, + {__LINE__, 0xffffffef, "FFFFFFEF", "%3X"}, + {__LINE__, 0x267b05ec, "645596652", "%4ld"}, + {__LINE__, 0xa487b724, "a487b724", "%x"}, + {__LINE__, 0x01da2a11, "31074833", "%-.0ld"}, + {__LINE__, 0xffffffff, "FFFFFFFF", "%X"}, + {__LINE__, 0x0000037a, "37a", "%x"}, + {__LINE__, 0x003d0314, "+3998484", "%+#1d"}, + {__LINE__, 0x0000011b, " 283", "%5.2d"}, + {__LINE__, 0x094f6066, "94f6066", "%x"}, + {__LINE__, 0x00000004, "4", "%X"}, + {__LINE__, -0x102e14bd, "-271455421", "% 7.ld"}, + {__LINE__, 0x000048da, "0018650", "%07ld"}, + {__LINE__, -0x24ae0390, "-615383952", "%0.6d"}, + {__LINE__, 0xffff5159, "0XFFFF5159", "%#0X"}, + {__LINE__, 0xfffd38c3, "fffd38c3", "%x"}, + {__LINE__, 0xfffff9ea, "FFFFF9EA", "%-X"}, + {__LINE__, -0x000fd28, "-64808", "% ld"}, + {__LINE__, 0x002c1bf4, "2890740", "%-.1d"}, + {__LINE__, 0x00d00ee7, "D00EE7", "%0.4X"}, + {__LINE__, 0x00000cf9, "3321", "%0d"}, + {__LINE__, -0x0000001, "-1", "%-#ld"}, + {__LINE__, 0xfffffd75, "FFFFFD75", "%.2X"}, + {__LINE__, 0x00000012, "18", "%-d"}, + {__LINE__, 0xfd44b4b2, "fd44b4b2", "%+5.x"}, + {__LINE__, -0x001c53f, "-116031", "%-#6.d"}, + {__LINE__, 0x000ff6fd, "ff6fd", "%2x"}, + {__LINE__, 0x00000001, "1", "%0d"}, + {__LINE__, 0x0120b478, "120B478", "%.7X"}, + {__LINE__, 0x0145591e, "145591E", "%1.4X"}, + {__LINE__, 0x001f99b9, "1F99B9", "%.1X"}, + {__LINE__, 0xffffd078, "ffffd078", "%-1.5x"}, + {__LINE__, 0xffffff1a, "FFFFFF1A", "%-4.6X"}, + {__LINE__, 0xfffffffa, "fffffffa", "%x"}, + {__LINE__, -0x00000b9, "-185", "%3ld"}, + {__LINE__, 0x00001cf0, "7408", "%-ld"}, + {__LINE__, 0xffffffff, "FFFFFFFF", "%X"}, + {__LINE__, 0x00151f30, "151F30", "%X"}, + {__LINE__, -0x0000103, "-259", "%d"}, + {__LINE__, -0x000303f, "-12351", "%d"}, + {__LINE__, 0x000002ca, "714", "%ld"}, + {__LINE__, -0x0000c55, "-3157", "%1d"}, + {__LINE__, 0x00000691, "01681", "%#.5ld"}, + {__LINE__, 0x00230537, "2295095", "%#d"}, + {__LINE__, 0x032a1faa, "53092266", "%.4d"}, + {__LINE__, 0x000050d7, "20695", "%d"}, + {__LINE__, -0x0c7ad4c, "-13086028", "%#.4ld"}, + {__LINE__, -0x00009d9, "-2521", "%+d"}, + {__LINE__, 0xfffffffe, "fffffffe", "%0x"}, + {__LINE__, 0xffdec2d9, "FFDEC2D9", "%5.1X"}, + {__LINE__, -0x000563c, "-22076", "%-.2ld"}, + {__LINE__, 0x0073b36a, "0x73b36a", "%-#x"}, + {__LINE__, 0xfffffffd, "fffffffd", "%x"}, + {__LINE__, 0x0193ba5a, "193ba5a", "%1.x"}, + {__LINE__, 0x00d147b5, "13715381", "%2.d"}, + {__LINE__, 0xffffff10, "ffffff10", "%4x"}, + {__LINE__, 0x000648d5, "648d5", "%1.3x"}, + {__LINE__, 0x00000011, "11", "%X"}, + {__LINE__, 0xff259f5b, "0xff259f5b", "% #.1x"}, + {__LINE__, 0x00000175, "175", "%x"}, + {__LINE__, 0x00000000, "0", "%ld"}, + {__LINE__, 0x00000015, "21", "%#ld"}, + {__LINE__, 0xfffffffe, "fffffffe", "%-x"}, + {__LINE__, 0x00000013, " 19", "% 0ld"}, + {__LINE__, -0x0000001, "-0000001", "% 5.7ld"}, + {__LINE__, 0x0000139d, "139D", "%X"}, + {__LINE__, 0x0041ad18, "41ad18", "%-.6x"}, + {__LINE__, -0x00000f9, "-249", "% 3.d"}, + {__LINE__, 0x00000076, "118", "%ld"}, + {__LINE__, 0x000006f8, "1784", "%d"}, + {__LINE__, -0x0000005, "-5", "%ld"}, + {__LINE__, -0x00008e7, "-2279", "%+ld"}, + {__LINE__, 0x00003f77, "3F77", "%+0X"}, + {__LINE__, 0x000ca3f8, "0XCA3F8", "%+#X"}, + {__LINE__, -0x00004bf, "-001215", "%#5.6ld"}, + {__LINE__, 0x319129ab, "0x319129ab", "%+#x"}, + {__LINE__, -0x0000002, "-2", "%ld"}, + {__LINE__, -0x0f6686e, "-16148590", "%.6ld"}, + {__LINE__, 0x0329576b, "53041003", "%2.d"}, + {__LINE__, -0x000179e, "-6046", "%d"}, + {__LINE__, 0x000002cc, "+716", "%+1.3ld"}, + {__LINE__, 0xfffffff7, "0xfffffff7", "% #x"}, + {__LINE__, 0x0016eb40, "1502016", "%ld"}, + {__LINE__, 0x00000003, "3", "%-0ld"}, + {__LINE__, 0x0023e0d6, "23E0D6", "%-05.X"}, + {__LINE__, 0xffffa6a4, "0XFFFFA6A4", "%#X"}, + {__LINE__, 0x00087664, "87664", "% .2x"}, + {__LINE__, -0x0000002, " -2", "%3.ld"}, + {__LINE__, 0x003ad85d, "3AD85D", "%X"}, + {__LINE__, 0x00002f20, "12064", "%ld"}, + {__LINE__, 0x02030bfc, "2030BFC", "%02.3X"}, + {__LINE__, 0xfffff36a, "FFFFF36A", "%X"}, + {__LINE__, 0xfe0729ff, "0xfe0729ff", "% #.0x"}, + {__LINE__, 0xfec15164, "FEC15164", "%0.2X"}, + {__LINE__, -0x00169ec, "-92652", "%+d"}, + {__LINE__, 0x006d7990, "0x6d7990", "%#x"}, + {__LINE__, 0xffcc89e6, "ffcc89e6", "%x"}, + {__LINE__, 0x002d7cca, "2981066", "%7.7ld"}, + {__LINE__, -0x1649692b, "-373909803", "%#ld"}, + {__LINE__, 0x00664f47, "664f47", "%+6.x"}, + {__LINE__, 0x00000000, "0", "% X"}, + {__LINE__, 0x00000047, "000047", "%.6X"}, + {__LINE__, 0x00000007, "0000007", "%.7ld"}, + {__LINE__, 0x00000030, "30", "%-x"}, + {__LINE__, -0x25bcbabb, "-633125563", "% ld"}, + {__LINE__, 0xfe11c031, "FE11C031", "%4.6X"}, + {__LINE__, -0x0000001, "-0000001", "%.7d"}, + {__LINE__, 0x00b56d84, "11890052", "%ld"}, + {__LINE__, -0x0000b01, "-0002817", "%+07.7ld"}, + {__LINE__, 0xffffe4ad, "ffffe4ad", "% 0.0x"}, + {__LINE__, 0x007760fc, "7760fc", "%+x"}, + {__LINE__, 0xffef44d8, "FFEF44D8", "%X"}, + {__LINE__, 0xfcb6862e, "fcb6862e", "%+x"}, + {__LINE__, -0x0000001, "-1", "%0d"}, + {__LINE__, 0xffffffe6, "FFFFFFE6", "%3.1X"}, + {__LINE__, 0xff816e27, "FF816E27", "% 6.X"}, + {__LINE__, 0x00006415, "25621", "%ld"}, + {__LINE__, -0xfae5449, "-263083081", "%0ld"}, + {__LINE__, 0x00000001, " 1", "% 0d"}, + {__LINE__, 0x00000009, " 9", "%2.X"}, + {__LINE__, 0x002d7e18, "2981400", "%7d"}, + {__LINE__, -0x00000fc, "-0000252", "%-#.7d"}, + {__LINE__, 0x00000003, "3", "%0x"}, + {__LINE__, 0x0d46e47f, "222749823", "%ld"}, + {__LINE__, 0xfffcbc52, "FFFCBC52", "%+X"}, + {__LINE__, 0x00000000, "0", "%-X"}, + {__LINE__, 0xfffff0e5, "fffff0e5", "%x"}, + {__LINE__, 0xffffffd9, "FFFFFFD9", "%07X"}, + {__LINE__, 0x000004ee, "4ee", "%0x"}, + {__LINE__, 0xffffffff, "ffffffff", "%0x"}, + {__LINE__, -0x0008cb4, "-036020", "%+3.6ld"}, + {__LINE__, 0x0000dada, "0xdada", "%#0.x"}, + {__LINE__, 0x0000001a, "26 ", "%-4d"}, + {__LINE__, -0x000000c, " -12", "%04.d"}, + {__LINE__, 0x0000025c, " 25c", "%7.x"}, + {__LINE__, 0xfe9b091e, "FE9B091E", "%X"}, + {__LINE__, 0x00000002, " 2", "%07.ld"}, + {__LINE__, -0x0004930, "-18736", "% 6.d"}, + {__LINE__, 0xffffffe9, "FFFFFFE9", "%X"}, + {__LINE__, 0x00000003, " 3", "%2.ld"}, + {__LINE__, 0x00000e00, "3584", "%.4d"}, + {__LINE__, 0xffff38c2, "ffff38c2", "%0.x"}, + {__LINE__, 0xffffffff, "FFFFFFFF", "% .3X"}, + {__LINE__, 0xff7c1b2f, "FF7C1B2F", "%-3.2X"}, + {__LINE__, -0x0000005, "-5", "%2d"}, + {__LINE__, 0x00098775, "98775", "%x"}, + {__LINE__, 0x000015ff, " 15FF", "%6.X"}, + {__LINE__, 0xfffbe3ef, "FFFBE3EF", "%0.3X"}, + {__LINE__, -0x0000048, "-72", "%ld"}, + {__LINE__, 0x0001488a, "84106", "%d"}, + {__LINE__, 0x00000001, "1", "%x"}, + {__LINE__, -0x0000002, "-2", "%-0.0ld"}, + {__LINE__, -0x0000001, "-1", "%-d"}, + {__LINE__, 0x0006616f, "6616f", "%x"}, + {__LINE__, 0x3657856d, "911705453", "%6.d"}, + {__LINE__, -0x0007222, "-0029218", "%.7d"}, + {__LINE__, 0x00041606, "267782", "%04.5d"}, + {__LINE__, 0x00000001, " 1", "%2.ld"}, + {__LINE__, 0x00000219, "219", "%0x"}, + {__LINE__, 0x0ae0184c, "ae0184c", "%x"}, + {__LINE__, 0x00003a7d, " 14973", "%7.ld"}, + {__LINE__, 0xffffffff, "0XFFFFFFFF", "%#X"}, + {__LINE__, -0x0000002, "-2", "%0ld"}, + {__LINE__, 0x00002cdf, "11487", "%0ld"}, + {__LINE__, -0x000000c, "-12", "%d"}, + {__LINE__, -0x000000e, "-14", "%-#2d"}, + {__LINE__, 0x00000371, "881", "%#ld"}, + {__LINE__, 0x000015be, " 5566", "%5.ld"}, + {__LINE__, 0x01525b91, "22174609", "%#0.0ld"}, + {__LINE__, 0xff8fc22f, "FF8FC22F", "% X"}, + {__LINE__, -0x0007f7b, "-32635", "%4.ld"}, + {__LINE__, 0x00007bca, "7bca", "%x"}, + {__LINE__, -0x0000582, "-1410", "%ld"}, + {__LINE__, 0x00000047, " +71", "%+04.2d"}, + {__LINE__, 0xf8a8dce2, "f8a8dce2", "%+.6x"}, + {__LINE__, -0x000de2e, "-56878", "%.0d"}, + {__LINE__, 0x019c03c1, "0X19C03C1", "%#X"}, + {__LINE__, -0x0f3a43e, "-15967294", "%ld"}, + {__LINE__, 0x00009e87, "40583", "%#5.1d"}, + {__LINE__, 0x000000b8, "184", "%0ld"}, + {__LINE__, -0x000befe, "-48894", "%2ld"}, + {__LINE__, -0x002ee1a, "-192026", "%.2d"}, + {__LINE__, 0x00004fd8, "4fd8", "%x"}, + {__LINE__, 0x0006d57b, "447867", "%2d"}, + {__LINE__, 0xfffffa9c, "FFFFFA9C", "%-X"}, + {__LINE__, 0x0000000f, "15", "%ld"}, + {__LINE__, 0x0005deb2, "5DEB2", "% X"}, + {__LINE__, 0x00000007, "7", "%X"}, + {__LINE__, 0xffffffc8, "0XFFFFFFC8", "% #.1X"}, + {__LINE__, 0xfffff62f, "FFFFF62F", "%X"}, + {__LINE__, -0x1a935bba, "-445864890", "% 3.5d"}, + {__LINE__, 0x000b34b4, "0x0b34b4", "%-#3.6x"}, + {__LINE__, 0xfffff430, "FFFFF430", "%X"}, + {__LINE__, 0x00000b5f, "+0002911", "%+#.7d"}, + {__LINE__, 0x00000007, " 7", "%3ld"}, + {__LINE__, 0xffffffd8, "FFFFFFD8", "% .7X"}, + {__LINE__, 0xfffff544, "FFFFF544", "%0X"}, + {__LINE__, -0x353667b9, "-892757945", "%d"}, + {__LINE__, 0x00000058, "0x00058", "%+#.5x"}, + {__LINE__, 0x39dbcc4a, "+970705994", "%+0.6ld"}, + {__LINE__, 0xfffffffd, "fffffffd", "% 01x"}, + {__LINE__, 0x00b1c28e, "+11649678", "%+0ld"}, + {__LINE__, 0x000066c2, "0X66C2", "%-#4X"}, + {__LINE__, 0x007171a2, "+7434658", "%+07.ld"}, + {__LINE__, -0x0000001, "-1", "%-d"}, + {__LINE__, 0x00000ae0, "+2784", "%+0d"}, + {__LINE__, 0x13786a57, "326658647", "%#5d"}, + {__LINE__, -0x0000001, "-1", "%2.ld"}, + {__LINE__, -0x0168a16, "-1477142", "%d"}, + {__LINE__, 0x12df7dd6, "316636630", "%ld"}, + {__LINE__, 0x00000000, "000000", "%1.6d"}, + {__LINE__, 0x266da2a9, "644719273", "%ld"}, + {__LINE__, 0x0000004a, "74", "%d"}, + {__LINE__, 0x000102ff, "00102FF", "%+06.7X"}, + {__LINE__, 0x17916237, "0x17916237", "%#5x"}, + {__LINE__, -0x0003cbb, "-0015547", "%-.7d"}, + {__LINE__, 0xe7da2010, "e7da2010", "%2.4x"}, + {__LINE__, 0xfffffff3, "fffffff3", "% .7x"}, + {__LINE__, 0xfc9b64f8, "FC9B64F8", "% 7.X"}, + {__LINE__, 0x000001af, "431", "%0d"}, + {__LINE__, 0xffc81796, "0xffc81796", "%#.5x"}, + {__LINE__, 0x057a1fbc, "+91889596", "%+04d"}, + {__LINE__, 0x00001ae1, "6881", "%ld"}, + {__LINE__, 0xfffffff6, "fffffff6", "%3.0x"}, + {__LINE__, -0x6b7d5db, "-112711131", "%-06.1d"}, + {__LINE__, 0x008e8a5c, " 8e8a5c", "%7.0x"}, + {__LINE__, 0xfe07a9bd, "fe07a9bd", "%.1x"}, + {__LINE__, -0x00278e6, "-162022", "%0ld"}, + {__LINE__, 0xf98709f5, "F98709F5", "%X"}, + {__LINE__, -0x00000c9, "-201", "% 1.d"}, + {__LINE__, -0x000001d, "-00029", "%+2.5ld"}, + {__LINE__, 0x00000030, " 48", "% d"}, + {__LINE__, 0xffd753f4, "FFD753F4", "%0X"}, + {__LINE__, 0xfffffffd, "FFFFFFFD", "%X"}, + {__LINE__, 0x0000a765, "a765", "%x"}, + {__LINE__, 0x0026e2c8, "26E2C8", "%-0X"}, + {__LINE__, -0x00003eb, "-1003", "%3.1ld"}, + {__LINE__, 0x015d53ca, "22893514", "%0ld"}, + {__LINE__, -0x0000020, "-32", "%ld"}, + {__LINE__, -0x03558dd, "-3496157", "%1.0ld"}, + {__LINE__, -0x000007f, "-127", "%1.d"}, + {__LINE__, 0xffffc737, "0xffffc737", "%+#x"}, + {__LINE__, 0x0000a70d, " a70d", "%6.x"}, + {__LINE__, 0x01ea0e31, "0X1EA0E31", "% #1.X"}, + {__LINE__, 0x00b5f406, "0X0B5F406", "%-#.7X"}, + {__LINE__, 0x0e58fa9a, "240712346", "%.2d"}, + {__LINE__, 0xf43ff8a9, "f43ff8a9", "%+0.6x"}, + {__LINE__, -0x5d0d3d3, "-97571795", "%.3ld"}, + {__LINE__, -0x000027a, "-634", "%0d"}, + {__LINE__, 0xffcef248, "FFCEF248", "%07.X"}, + {__LINE__, 0x000002dc, "2dc", "%0x"}, + {__LINE__, -0x31fa6c1, "-52405953", "%.2d"}, + {__LINE__, 0x00000014, "14", "%X"}, + {__LINE__, -0x0282f98, "-2633624", "%ld"}, + {__LINE__, 0x00005f55, "24405", "%#d"}, + {__LINE__, -0x0000004, "-4", "%d"}, + {__LINE__, 0x00000001, "1", "%+x"}, + {__LINE__, 0x013f47ce, "13f47ce", "%0.x"}, + {__LINE__, 0x00143fce, "1327054", "%.3d"}, + {__LINE__, 0x00000000, "0", "%d"}, + {__LINE__, 0x00001f9a, "1F9A", "% X"}, + {__LINE__, 0x00000cf5, "cf5", "%x"}, + {__LINE__, 0xffea9c24, "ffea9c24", "%x"}, + {__LINE__, 0xf09f85a7, "F09F85A7", "%X"}, + {__LINE__, 0x00001e04, "7684", "%ld"}, + {__LINE__, 0xffffc82a, "ffffc82a", "%7x"}, + {__LINE__, -0x0000a77, "-2679", "% d"}, + {__LINE__, 0x0019a859, "+1681497", "%+2.6ld"}, + {__LINE__, -0x00002d7, "-727", "%ld"}, + {__LINE__, 0x00011b82, "72578", "%-#ld"}, + {__LINE__, 0x00000c32, "3122", "%ld"}, + {__LINE__, -0x0000337, "-823", "%1.d"}, + {__LINE__, 0x00000004, "4", "%1ld"}, + {__LINE__, 0x0000062c, "1580", "%.1ld"}, + {__LINE__, 0xffffffd3, "FFFFFFD3", "% X"}, + {__LINE__, -0x07b1bf8, "-8068088", "%#d"}, + {__LINE__, 0xfffffff9, "fffffff9", "%x"}, + {__LINE__, 0x159e6cfb, "159e6cfb", "%0.x"}, + {__LINE__, 0x0001148d, "1148d", "%-.1x"}, + {__LINE__, 0x003373b7, "3371959", "%#.0d"}, + {__LINE__, 0x00001af2, "1af2", "%.4x"}, + {__LINE__, -0x0000001, "-000001", "%+.6ld"}, + {__LINE__, -0x00001a3, "-419", "%d"}, + {__LINE__, 0xfdb232e7, "fdb232e7", "%5.4x"}, + {__LINE__, -0x000000d, "-13", "%d"}, + {__LINE__, 0x98ec1c74, "98ec1c74", "% 3x"}, + {__LINE__, 0xfffffff8, "fffffff8", "%+1.4x"}, + {__LINE__, -0x0000d58, "-3416", "%+#0ld"}, + {__LINE__, -0x65d1973, "-106764659", "%.6d"}, + {__LINE__, -0x0737641, "-7566913", "%-d"}, + {__LINE__, 0x0000037c, "37c", "%+0x"}, + {__LINE__, 0x0000012c, "0000300", "%#3.7d"}, + {__LINE__, 0x00c12d9b, "12660123", "%1.5ld"}, + {__LINE__, 0xe7e5e77d, "e7e5e77d", "%6x"}, + {__LINE__, 0x1fe0e820, "1FE0E820", "%-4X"}, + {__LINE__, 0xc85a793a, "C85A793A", "%X"}, + {__LINE__, 0x05f9fe2c, "100269612", "%#ld"}, + {__LINE__, 0x00000032, "0X32", "%+#4.1X"}, + {__LINE__, 0xfab4ce81, "fab4ce81", "%3.x"}, + {__LINE__, -0x0000009, "-9", "%2.d"}, + {__LINE__, 0xffff8d2e, "ffff8d2e", "% 6x"}, + {__LINE__, 0xfffffffe, "0xfffffffe", "%#x"}, + {__LINE__, 0x02960f60, "2960f60", "%1.x"}, + {__LINE__, 0x34cf3cbc, " 885996732", "% 5ld"}, + {__LINE__, -0x000006e, "-110", "% d"}, + {__LINE__, -0x0000001, "-1", "%-.0d"}, + {__LINE__, 0x000000b1, " B1", "%+7X"}, + {__LINE__, 0xfffff5b4, "FFFFF5B4", "%01.7X"}, + {__LINE__, 0x00000086, "86", "%0X"}, + {__LINE__, 0x00074367, " 74367", "% 6x"}, + {__LINE__, 0x001000d7, "+1048791", "%+ld"}, + {__LINE__, 0x00000f35, " 3893", "% d"}, + {__LINE__, -0x00000e1, "-225", "%d"}, + {__LINE__, 0xffff6694, "FFFF6694", "%6.X"}, + {__LINE__, 0x00019cf7, "19cf7", "%0.x"}, + {__LINE__, 0x00000046, "0046", "%+.4X"}, + {__LINE__, -0x000001f, "-31", "%ld"}, + {__LINE__, 0xfff25859, "fff25859", "%6x"}, + {__LINE__, 0x21884061, "562577505", "%#ld"}, + {__LINE__, 0x01448b7a, "1448B7A", "%0.1X"}, + {__LINE__, 0xffffffc0, "ffffffc0", "%x"}, + {__LINE__, 0xfffffed5, "fffffed5", "% 1.0x"}, + {__LINE__, 0x0e349767, "E349767", "%.5X"}, + {__LINE__, 0x00000330, "816", "%0d"}, + {__LINE__, -0x0000016, "-22", "%d"}, + {__LINE__, 0xffff9bab, "FFFF9BAB", "%X"}, + {__LINE__, 0xffffee4e, "ffffee4e", "%x"}, + {__LINE__, 0x0026beeb, "2539243", "%6.d"}, + {__LINE__, 0x00002c6c, "11372", "%.2d"}, + {__LINE__, -0x000002c, "-44", "%ld"}, + {__LINE__, -0x001dcfa, "-122106", "% d"}, + {__LINE__, 0x0001683c, "92220", "%#4.ld"}, + {__LINE__, 0x09b51fc9, "9b51fc9", "%+.7x"}, + {__LINE__, 0x0000001d, "29", "%0ld"}, + {__LINE__, -0x83f17e5, "-138352613", "%ld"}, + {__LINE__, 0xfa4e2c1b, "fa4e2c1b", "%x"}, + {__LINE__, 0x000001f3, "499", "%0ld"}, + {__LINE__, 0xffff03e4, "ffff03e4", "%x"}, + {__LINE__, 0x000000ac, "+0172", "%+.4ld"}, + {__LINE__, 0x03c3903b, "63148091", "%00d"}, + {__LINE__, 0x0000000d, "13", "%#d"}, + {__LINE__, 0x0000002e, "0X2E", "%#X"}, + {__LINE__, 0x00006b2d, "6B2D", "%X"}, + {__LINE__, 0x0000010b, " 0010b", "%7.5x"}, + {__LINE__, 0x0000017a, " 17a", "%4x"}, + {__LINE__, 0xfffffffb, "fffffffb", "%+x"}, + {__LINE__, 0xffffac6a, "ffffac6a", "%-.3x"}, + {__LINE__, -0x0005870, "-22640", "%0d"}, + {__LINE__, 0x189c17bc, "189c17bc", "%x"}, + {__LINE__, -0x01bbc38, "-1817656", "%#6.ld"}, + {__LINE__, -0x3382b55, "-54012757", "%d"}, + {__LINE__, -0x0000007, "-7", "%-ld"}, + {__LINE__, -0x000c74b, "-51019", "%ld"}, + {__LINE__, 0x0001a6f2, "+108274", "%+#1.ld"}, + {__LINE__, 0x00077448, "77448", "%x"}, + {__LINE__, 0x00000000, "0", "%x"}, + {__LINE__, 0xfffff056, "FFFFF056", "%.3X"}, + {__LINE__, -0x6f3f9451, "-1866437713", "%-#3.0ld"}, + {__LINE__, 0x000000ac, "AC", "%X"}, + {__LINE__, 0xffc8752f, "ffc8752f", "%-x"}, + {__LINE__, 0xfffffe6d, "FFFFFE6D", "%2.X"}, + {__LINE__, -0x377f1a5, "-58192293", "% .2d"}, + {__LINE__, -0x0000294, "-660", "%d"}, + {__LINE__, 0xfffffffe, "fffffffe", "%-x"}, + {__LINE__, 0xfffcbbe8, "0xfffcbbe8", "%-#4.x"}, + {__LINE__, 0x0022e510, "+2286864", "%+ld"}, + {__LINE__, 0xfffffe2a, "FFFFFE2A", "%0.3X"}, + {__LINE__, 0xfe29f7c0, "fe29f7c0", "%x"}, + {__LINE__, 0xfffe957e, "FFFE957E", "%X"}, + {__LINE__, 0x000080f8, "33016", "%ld"}, + {__LINE__, 0x0003ee2c, " 257580", "%7d"}, + {__LINE__, 0x000003a1, "929", "%d"}, + {__LINE__, 0x0000021f, "21f", "%+x"}, + {__LINE__, 0xffffffff, "ffffffff", "%.5x"}, + {__LINE__, -0x040a988, "-4237704", "%-d"}, + {__LINE__, 0x027c8b69, "41716585", "%ld"}, + {__LINE__, 0x00000003, "3", "%-X"}, + {__LINE__, -0x0000368, " -872", "%7.d"}, + {__LINE__, 0x00000d3c, "3388", "%0.2d"}, + {__LINE__, -0x33734cc, "-53949644", "%2ld"}, + {__LINE__, 0x000003db, "3DB", "%X"}, + {__LINE__, -0x00f154d, "-988493", "%5d"}, + {__LINE__, 0x0000000b, " 11", "%4.ld"}, + {__LINE__, 0x00000067, "103", "%-ld"}, + {__LINE__, -0x0199fce, "-1679310", "%4d"}, + {__LINE__, 0x02b6266b, "2b6266b", "% x"}, + {__LINE__, -0x006b39d, "-439197", "%+d"}, + {__LINE__, 0x00000007, "7", "%ld"}, + {__LINE__, 0x0141fc98, "21101720", "%0ld"}, + {__LINE__, -0x0008420, "-33824", "%d"}, + {__LINE__, 0x0011622b, "1139243", "%6ld"}, + {__LINE__, 0x0000001b, "27", "%-d"}, + {__LINE__, -0x0030935, "-198965", "%3d"}, + {__LINE__, 0x0000001f, " 1f", "%4.x"}, + {__LINE__, -0x10782a19, "-276310553", "%#ld"}, + {__LINE__, -0x007eac4, "-518852", "%06.ld"}, + {__LINE__, 0x0bc4c681, "197445249", "%6ld"}, + {__LINE__, 0x000000f5, "245", "%.3ld"}, + {__LINE__, 0x00000197, "197", "%X"}, + {__LINE__, 0xfffffeab, "FFFFFEAB", "% X"}, + {__LINE__, 0x00000f05, "F05", "%+X"}, + {__LINE__, 0xffe1b785, "0xffe1b785", "%#x"}, + {__LINE__, -0x02d3581, "-2962817", "%05d"}, + {__LINE__, 0xffffb994, "FFFFB994", "%X"}, + {__LINE__, 0x03d139a3, "64043427", "%ld"}, + {__LINE__, -0x0000002, " -2", "%+4.d"}, + {__LINE__, -0x043da83, "-4446851", "%#ld"}, + {__LINE__, -0x6aad891, "-111859857", "%7.ld"}, + {__LINE__, 0x00003e68, "15976", "%-4.5ld"}, + {__LINE__, 0xffe4d3ee, "FFE4D3EE", "%X"}, + {__LINE__, 0x00000002, "000002", "%4.6ld"}, + {__LINE__, 0xffffee32, "ffffee32", "%4x"}, + {__LINE__, 0x0cb7dd25, "cb7dd25", "%3x"}, + {__LINE__, 0xf773d422, "F773D422", "%X"}, + {__LINE__, -0x0b590f7, "-11899127", "%#d"}, + {__LINE__, -0x0002c45, "-11333", "%-#5ld"}, + {__LINE__, -0x1efc9e4, "-32492004", "%ld"}, + {__LINE__, -0x0003b9c, "-15260", "%d"}, + {__LINE__, 0x000001b6, "0X1B6", "%#4X"}, + {__LINE__, 0x3ce93ec3, "1021918915", "%.6d"}, + {__LINE__, 0xffffffff, "ffffffff", "%-x"}, + {__LINE__, 0xfffaf0e4, "fffaf0e4", "%2.x"}, + {__LINE__, 0x00000002, "0x2", "%#x"}, + {__LINE__, 0x0000e806, "e806", "%4x"}, + {__LINE__, 0xfffffff0, "fffffff0", "% 2.x"}, + {__LINE__, 0xfffffc0e, "fffffc0e", "%+.4x"}, + {__LINE__, 0xfd45716e, "FD45716E", "%.4X"}, + {__LINE__, 0xfff96fda, "FFF96FDA", "%-X"}, + {__LINE__, 0xff99d08a, "FF99D08A", "%.5X"}, + {__LINE__, 0x00000002, "2", "%-00.d"}, + {__LINE__, -0x000000f, " -15", "%6.ld"}, + {__LINE__, -0x00d321c, "-864796", "%#1ld"}, + {__LINE__, 0xff19ff8d, "ff19ff8d", "%1.5x"}, + {__LINE__, 0x00000000, "0000000", "%-.7x"}, + {__LINE__, 0x0000f50d, " F50D", "%5X"}, + {__LINE__, 0x00001688, " 5768", "%7.ld"}, + {__LINE__, 0x00000157, "343", "%2ld"}, + {__LINE__, 0xfffffffd, "0xfffffffd", "%+#3x"}, + {__LINE__, 0x00000d52, "3410", "%ld"}, + {__LINE__, 0x00000003, "3", "%d"}, + {__LINE__, 0x0001c6d2, "01c6d2", "%.6x"}, + {__LINE__, 0x242d65d2, "606954962", "%#7.ld"}, + {__LINE__, 0x00000e9f, "0000E9F", "%5.7X"}, + {__LINE__, 0x0002d785, "2D785", "%0X"}, + {__LINE__, 0x00000167, "167", "%0X"}, + {__LINE__, 0xfffff5e5, "fffff5e5", "% x"}, + {__LINE__, 0xffb69a04, "0xffb69a04", "%#0.x"}, + {__LINE__, 0xfffa012e, "FFFA012E", "%X"}, + {__LINE__, 0x0000001a, "0x1a", "% #x"}, + {__LINE__, 0xfffb26d3, "fffb26d3", "%+x"}, + {__LINE__, -0x00003e6, " -998", "%7ld"}, + {__LINE__, 0x00000004, "4", "%-d"}, + {__LINE__, 0xfffffd76, "0XFFFFFD76", "%#7X"}, + {__LINE__, 0x00000035, "53", "%ld"}, + {__LINE__, 0xffc6c190, "0XFFC6C190", "%#X"}, + {__LINE__, 0xffffffe2, "ffffffe2", "%7.0x"}, + {__LINE__, 0x03ff1b87, "67050375", "%2.d"}, + {__LINE__, 0x00020c47, "0X20C47", "%#X"}, + {__LINE__, 0x00000000, "0", "%d"}, + {__LINE__, 0x0000000e, "0X000E", "% #.4X"}, + {__LINE__, -0x00035cd, "-13773", "%+2.d"}, + {__LINE__, 0x00005eec, "5eec", "%1x"}, + {__LINE__, 0x001da0ff, "1941759", "%0d"}, + {__LINE__, 0xf8a9e96a, "f8a9e96a", "%+x"}, + {__LINE__, -0x0000120, "-288 ", "%-#6.d"}, + {__LINE__, 0x00000000, "0", "%-X"}, + {__LINE__, 0x0000152e, "152E", "%+X"}, + {__LINE__, -0x0001077, "-4215", "% 0d"}, + {__LINE__, 0xffffffd7, "FFFFFFD7", "%0X"}, + {__LINE__, 0xfffe097d, "FFFE097D", "%2X"}, + {__LINE__, 0xcb982712, "cb982712", "%x"}, + {__LINE__, 0xfff39f2e, "FFF39F2E", "%.5X"}, + {__LINE__, -0x0058cae, "-363694", "%+.1d"}, + {__LINE__, -0x000002b, "-043", "%#1.3d"}, + {__LINE__, -0x001e989, "-125321", "%1d"}, + {__LINE__, 0x00959ecd, "9805517", "%d"}, + {__LINE__, 0xf49df846, "F49DF846", "%+3X"}, + {__LINE__, 0xfffffffd, "fffffffd", "%+.5x"}, + {__LINE__, -0x000ac76, "-44150", "%3.ld"}, + {__LINE__, 0x00000028, " 0028", "%06.4x"}, + {__LINE__, 0x000003e8, "3E8", "%-02X"}, + {__LINE__, 0x00000012, "012", "%.3X"}, + {__LINE__, 0x00000006, "0X6", "%#X"}, + {__LINE__, 0x00ef8479, "+15697017", "%+0.6d"}, + {__LINE__, 0xffffffe8, "FFFFFFE8", "%-X"}, + {__LINE__, 0x773a90f7, "773a90f7", "%3.5x"}, + {__LINE__, 0x00096d44, "96D44", "%-X"}, + {__LINE__, -0x08a3d96, "-9059734", "%ld"}, + {__LINE__, -0x000001a, "-26", "%00.ld"}, + {__LINE__, 0x32f57585, "32f57585", "% x"}, + {__LINE__, 0x2825f175, "2825F175", "%X"}, + {__LINE__, 0x000054ff, "21759", "%-2d"}, + {__LINE__, -0x0266cdc, "-2518236", "%ld"}, + {__LINE__, 0x0000031d, "0X31D", "%+#4X"}, + {__LINE__, 0x0003fc99, "3fc99", "%x"}, + {__LINE__, -0x0064624, "-411172", "%-#d"}, + {__LINE__, 0x0001b2d9, "111321", "%-ld"}, + {__LINE__, -0x000007c, "-124", "%#d"}, + {__LINE__, 0xfffffffa, "FFFFFFFA", "%+X"}, + {__LINE__, -0x007a5b8, "-501176", "%+ld"}, + {__LINE__, 0x0213b583, "213B583", "%2.X"}, + {__LINE__, 0xfff12e51, "FFF12E51", "%+X"}, + {__LINE__, 0x00052403, "0336899", "%-0.7d"}, + {__LINE__, 0xffffffff, "ffffffff", "%-04.4x"}, + {__LINE__, 0x00000010, "0010", "%0.4X"}, + {__LINE__, 0xffffffb5, "FFFFFFB5", "%-1.3X"}, + {__LINE__, -0x0000001, "-1", "%d"}, + {__LINE__, 0x0003a2d1, "3a2d1", "% x"}, + {__LINE__, 0x00000424, "1060", "%0ld"}, + {__LINE__, -0x0000078, "-120", "%1d"}, + {__LINE__, 0x00000000, "0", "%ld"}, + {__LINE__, 0x00000414, "1044", "%ld"}, + {__LINE__, 0x00000072, "0x072", "%#5.3x"}, + {__LINE__, 0x0000007a, "7A", "% X"}, + {__LINE__, 0x081542a3, "135611043", "%0d"}, + {__LINE__, 0xfffb4ce6, "fffb4ce6", "%4.6x"}, + {__LINE__, 0x00002ab2, "2ab2", "%-1x"}, + {__LINE__, 0x00000000, " 0", "% 2x"}, + {__LINE__, 0xfffffa59, "FFFFFA59", "%2.X"}, + {__LINE__, 0xffffe22b, "ffffe22b", "%-x"}, + {__LINE__, 0xfffffd52, "FFFFFD52", "%+.2X"}, + {__LINE__, 0x0000039f, "927", "%-d"}, + {__LINE__, 0x0002dea6, "2dea6", "%0x"}, + {__LINE__, 0x000216db, "136923", "%ld"}, + {__LINE__, 0x00000013, "19", "%0d"}, + {__LINE__, 0x000047cc, "18380", "%2.2ld"}, + {__LINE__, -0x0007d10, "-32016", "%d"}, + {__LINE__, 0xff9af906, "FF9AF906", "%0X"}, + {__LINE__, 0x02cfa224, "+47161892", "%+0.7d"}, + {__LINE__, -0x0000022, "-34", "%1.d"}, + {__LINE__, 0xfffffff0, "FFFFFFF0", "%-4X"}, + {__LINE__, 0x00000029, " 41", "%6.ld"}, + {__LINE__, 0xffffff76, "ffffff76", "%x"}, + {__LINE__, 0x00000000, "0", "%+X"}, + {__LINE__, 0x00000001, "1", "%d"}, + {__LINE__, 0xfff6de5a, "FFF6DE5A", "%X"}, + {__LINE__, 0x00000002, "2", "%ld"}, + {__LINE__, 0x00000025, "25", "%+x"}, + {__LINE__, 0xfffffffb, "FFFFFFFB", "%5.0X"}, + {__LINE__, 0x0011bbb5, "1162165", "%0.0ld"}, + {__LINE__, 0xfffcdc96, "0XFFFCDC96", "%#X"}, + {__LINE__, 0x00008f5f, "8f5f", "%3.1x"}, + {__LINE__, 0xffa0fa5d, "0XFFA0FA5D", "%#X"}, + {__LINE__, 0x00004534, "4534", "% 0X"}, + {__LINE__, 0xfffffff9, "fffffff9", "%.2x"}, + {__LINE__, 0xffeeae50, "ffeeae50", "%-x"}, + {__LINE__, 0x00002964, " 0010596", "% 0.7d"}, + {__LINE__, 0x000021c9, " 21c9", "%6x"}, + {__LINE__, 0x00000001, " 1", "% d"}, + {__LINE__, 0x0002abd8, "+175064", "%+d"}, + {__LINE__, 0xffffffad, "FFFFFFAD", "%+0X"}, + {__LINE__, 0x00000003, "3", "%ld"}, + {__LINE__, 0x0c036da3, " 201551267", "% .3d"}, + {__LINE__, -0x004343c, "-275516", "%+ld"}, + {__LINE__, 0x00000003, "003", "% .3x"}, + {__LINE__, 0xffffe34e, "ffffe34e", "%x"}, + {__LINE__, 0x0000e118, "e118", "%x"}, + {__LINE__, -0x007d945, "-514373", "%+#0ld"}, + {__LINE__, 0x277f1cc5, "277f1cc5", "% x"}, + {__LINE__, 0x00003bef, "03bef", "% 0.5x"}, + {__LINE__, 0xfffffcb9, "fffffcb9", "%x"}, + {__LINE__, 0x0003311a, "209178", "%ld"}, + {__LINE__, 0x00000000, " ", "%3.ld"}, + {__LINE__, 0x05942225, "5942225", "%x"}, + {__LINE__, 0xfffceb4c, "FFFCEB4C", "%-X"}, + {__LINE__, 0xffd08633, "ffd08633", "%2.x"}, + {__LINE__, -0x0000001, " -1", "%6d"}, + {__LINE__, 0x000fdb12, "1039122", "%d"}, + {__LINE__, 0x00000003, "3", "% 0X"}, + {__LINE__, -0x2fd5bf1, "-50158577", "% 5.ld"}, + {__LINE__, -0x0089a03, "-563715", "%-d"}, + {__LINE__, 0xfffffffe, "FFFFFFFE", "% X"}, + {__LINE__, 0x29fdb2ba, "29fdb2ba", "% 0x"}, + {__LINE__, 0x0000008f, "8F", "% X"}, + {__LINE__, 0x00000003, "3", "%ld"}, + {__LINE__, 0xffc58445, "FFC58445", "%.2X"}, + {__LINE__, -0x00023f6, "-9206", "%+d"}, + {__LINE__, 0x00000011, "11", "%X"}, + {__LINE__, -0x0000003, "-0000003", "%-.7d"}, + {__LINE__, -0x0000003, "-3", "%#ld"}, + {__LINE__, 0x001242d9, "1242D9", "%X"}, + {__LINE__, -0x48d0c81, "-76352641", "%7.d"}, + {__LINE__, -0x001b675, "-112245", "%#ld"}, + {__LINE__, 0x00000003, "3", "%-d"}, + {__LINE__, 0xfff93fdf, "0XFFF93FDF", "% #X"}, + {__LINE__, 0x001a9414, "1741844", "%#ld"}, + {__LINE__, 0x0001fd86, "130438", "%ld"}, + {__LINE__, 0x010f37a8, "17774504", "%4.7d"}, + {__LINE__, 0x00382838, "0382838", "%3.7X"}, + {__LINE__, -0x47fd56c2, "-1207785154", "%ld"}, + {__LINE__, 0x001981dd, "1981dd", "%x"}, + {__LINE__, 0xffffefba, "ffffefba", "% 06.x"}, + {__LINE__, -0x3f402b29, "-1061169961", "%+#ld"}, + {__LINE__, -0x395aae44, "-962244164", "%-1d"}, + {__LINE__, 0x1fa39f42, "530816834", "%#d"}, + {__LINE__, -0x000358d, "-13709", "%d"}, + {__LINE__, -0xdc17b8f, "-230783887", "%ld"}, + {__LINE__, -0x23c41583, "-600053123", "%+3.d"}, + {__LINE__, 0xfffffc16, "fffffc16", "%x"}, + {__LINE__, 0x003f1364, "4133732", "%7.ld"}, + {__LINE__, 0xffffe691, "FFFFE691", "%07X"}, + {__LINE__, 0x07b31d71, "129178993", "%.7d"}, + {__LINE__, -0x0019732, "-104242", "% ld"}, + {__LINE__, 0x02428167, "0x2428167", "%#x"}, + {__LINE__, -0x06c1ecd, "-7085773", "%.7ld"}, + {__LINE__, 0xfb01cb4e, "fb01cb4e", "%x"}, + {__LINE__, 0x00005a41, "+23105", "%+ld"}, + {__LINE__, 0xfffffff4, "FFFFFFF4", "%-5.X"}, + {__LINE__, 0x00000005, " 5", "%4.ld"}, + {__LINE__, 0xf4ab4b2d, "F4AB4B2D", "%0.X"}, + {__LINE__, -0x000fce3, "-64739", "%+#3.ld"}, + {__LINE__, 0x000076ea, " 30442", "%6.2ld"}, + {__LINE__, 0x00000cb9, " cb9", "%4.x"}, + {__LINE__, 0x002f43ef, "3097583", "%1.1ld"}, + {__LINE__, 0xf5bd0105, "F5BD0105", "%+X"}, + {__LINE__, 0x0037954a, "3642698", "%d"}, + {__LINE__, 0xfffffff7, "0XFFFFFFF7", "%-#X"}, + {__LINE__, 0xffffcedf, "0xffffcedf", "%#6.7x"}, + {__LINE__, 0xfffffde4, "FFFFFDE4", "%2.4X"}, + {__LINE__, 0x01a6f103, "1a6f103", "%5.x"}, + {__LINE__, 0x00000191, " 191", "%7.0X"}, + {__LINE__, 0x00000003, "03", "% .2X"}, + {__LINE__, -0xd4e3219, "-223228441", "%6.ld"}, + {__LINE__, 0x00b0efbd, "11595709", "%d"}, + {__LINE__, -0x00000ea, "-234", "%#3d"}, + {__LINE__, 0x00000be8, "BE8 ", "%-5X"}, + {__LINE__, 0xffffffff, "ffffffff", "%5.3x"}, + {__LINE__, -0x267f8c6, "-40368326", "%d"}, + {__LINE__, 0x000006ed, "6ED", "%X"}, + {__LINE__, 0xfdd6c9b6, "FDD6C9B6", "%X"}, + {__LINE__, 0x0007ac29, "7ac29", "%-x"}, + {__LINE__, 0x00000014, "14", "%2x"}, + {__LINE__, 0x123ca563, "+305964387", "%+ld"}, + {__LINE__, 0x00000004, " 4", "%5.d"}, + {__LINE__, 0xffff877a, "ffff877a", "%x"}, + {__LINE__, -0x00003e7, "-999", "%+0.3d"}, + {__LINE__, 0x0a68ba6e, "a68ba6e", "%3.1x"}, + {__LINE__, 0xfe29c810, "FE29C810", "%+X"}, +#endif + + {__LINE__, 0x00000000, "0", "%o"}, + {__LINE__, 0000000123, "123", "%o"}, + {__LINE__, 0000123456, "0123456", "%#o"}, + {__LINE__, 0000123456, "00123456", "%#.8o"}, + {__LINE__, 0000123456, " 00123456", "%#10.8o"}, + {__LINE__, 0x00000123, "0x00123", "%#07x"}, + {__LINE__, 0x00000000, "", "%#.0d"}, + {__LINE__, 0x00000000, "", "%#.0x"}, + {__LINE__, 0x00000000, "0", "%#.0o"}, + + {__LINE__, 0x00000000, "0", "%Id"}, + {__LINE__, 0x0000000a, "10", "%Id"}, + {__LINE__, 0x000000d2, "210", "%Id"}, + {__LINE__, 0x00000c8a, "3210", "%Id"}, + {__LINE__, 0x0000a8ca, "43210", "%Id"}, + {__LINE__, 0x000849ea, "543210", "%Id"}, + {__LINE__, 0x0063d76a, "6543210", "%Id"}, + {__LINE__, 0x048ff4ea, "76543210", "%Id"}, + {__LINE__, 0x343efcea, "876543210", "%Id"}, + + {0}, +}; + +int +main(int argc, char *argv[]) +{ + int errcount = 0; + int testcount = 0; +#define BSIZE 1024 + char buffer[BSIZE]; + sprint_int_type *iptr; + for (iptr = sprint_ints; iptr->line; iptr++) { + snprintf(buffer, sizeof(buffer), iptr->format_string, iptr->value); + if (strcmp(buffer, iptr->result) != 0) { + errcount++; + printf( + "Error in line %d using \"%s\". Result is \"%s\"; should be: \"%s\".\n", + iptr->line, iptr->format_string, buffer, iptr->result); + } + testcount++; + } + + if (errcount == 0) { + printf ("Encountered no errors in %d tests.\n", testcount); + return 0; + } + else { + printf ("Encountered %d errors in %d tests.\n", + errcount, testcount); + return 1; + } +} diff --git a/mozglue/tests/glibc_printf_tests/tllformat.c b/mozglue/tests/glibc_printf_tests/tllformat.c new file mode 100644 index 0000000000..88b2132093 --- /dev/null +++ b/mozglue/tests/glibc_printf_tests/tllformat.c @@ -0,0 +1,59 @@ +#include <stdio.h> +#include <string.h> + +/* The original file was tiformat.c and it has been changed for long long tests\ +. */ +typedef struct +{ + int line; + long long int value; + const char *result; + const char *format_string; +} sprint_int_type; + +sprint_int_type sprint_ints[] = +{ + {__LINE__, 0x00000000ULL, "0", "%llx"}, + {__LINE__, 0xffff00000000208bULL, "ffff00000000208b", "%llx"}, + {__LINE__, 0xffff00000000208bULL, "18446462598732849291", "%llu"}, + {__LINE__, 18446462598732849291ULL, "ffff00000000208b", "%llx"}, + {__LINE__, 18446462598732849291ULL, "18446462598732849291", "%llu"}, + {__LINE__, 18359476226655002763ULL, "fec9f65b0000208b", "%llx"}, + {__LINE__, 18359476226655002763ULL, "18359476226655002763", "%llu"}, + + {0}, +}; + +int +main (void) +{ + int errcount = 0; + int testcount = 0; +#define BSIZE 1024 + char buffer[BSIZE]; + sprint_int_type *iptr; + for (iptr = sprint_ints; iptr->line; iptr++) + { + snprintf (buffer, sizeof(buffer), iptr->format_string, iptr->value); + if (strcmp (buffer, iptr->result) != 0) + { + ++errcount; + printf ("\ +Error in line %d using \"%s\". Result is \"%s\"; should be: \"%s\".\n", + iptr->line, iptr->format_string, buffer, iptr->result); + } + ++testcount; + } + + if (errcount == 0) + { + printf ("Encountered no errors in %d tests.\n", testcount); + return 0; + } + else + { + printf ("Encountered %d errors in %d tests.\n", + errcount, testcount); + return 1; + } +} diff --git a/mozglue/tests/gtest/TestNativeNtGTest.cpp b/mozglue/tests/gtest/TestNativeNtGTest.cpp new file mode 100644 index 0000000000..cc48f16fdd --- /dev/null +++ b/mozglue/tests/gtest/TestNativeNtGTest.cpp @@ -0,0 +1,63 @@ +/* -*- 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 <windows.h> + +#include "gtest/gtest.h" + +#include "mozilla/NativeNt.h" +#include "nsHashKeys.h" +#include "nsTHashSet.h" + +TEST(TestNativeNtGTest, GenerateDependentModuleSet) +{ + mozilla::nt::PEHeaders executable(::GetModuleHandleW(nullptr)); + nsTHashSet<nsStringCaseInsensitiveHashKey> dependentModules; + executable.EnumImportChunks([&](const char* aModule) { + dependentModules.Insert( + mozilla::nt::GetLeafName(NS_ConvertASCIItoUTF16(aModule))); + }); + + EXPECT_TRUE(dependentModules.Contains(u"mozglue.dll"_ns)); + EXPECT_TRUE(dependentModules.Contains(u"MOZGLUE.dll"_ns)); + EXPECT_FALSE(dependentModules.Contains(u"xxx.dll"_ns)); +} + +TEST(TestNativeNtGTest, GetLeafName) +{ + nsAutoString str; + str = mozilla::nt::GetLeafName(u""_ns); + EXPECT_STREQ(str.get(), L""); + str = mozilla::nt::GetLeafName(u"\\"_ns); + EXPECT_STREQ(str.get(), L""); + str = mozilla::nt::GetLeafName(u"\\\\"_ns); + EXPECT_STREQ(str.get(), L""); + str = mozilla::nt::GetLeafName(u"abc\\def\\ghi"_ns); + EXPECT_STREQ(str.get(), L"ghi"); + str = mozilla::nt::GetLeafName(u"abcdef"_ns); + EXPECT_STREQ(str.get(), L"abcdef"); + str = mozilla::nt::GetLeafName(u"\\abcdef"_ns); + EXPECT_STREQ(str.get(), L"abcdef"); + + const auto kEntireText = + u"\\"_ns + u"\\\\abc"_ns + u"\\x\\y\\z"_ns + u"123\\456\\"_ns + u"789"_ns; + str = mozilla::nt::GetLeafName(Substring(kEntireText, 0, 0)); + EXPECT_STREQ(str.get(), L""); + str = mozilla::nt::GetLeafName(Substring(kEntireText, 0, 1)); + EXPECT_STREQ(str.get(), L""); + str = mozilla::nt::GetLeafName(Substring(kEntireText, 1, 5)); + EXPECT_STREQ(str.get(), L"abc"); + str = mozilla::nt::GetLeafName(Substring(kEntireText, 6, 6)); + EXPECT_STREQ(str.get(), L"z"); + str = mozilla::nt::GetLeafName(Substring(kEntireText, 12, 8)); + EXPECT_STREQ(str.get(), L""); + str = mozilla::nt::GetLeafName(Substring(kEntireText, 20)); + EXPECT_STREQ(str.get(), L"789"); +} diff --git a/mozglue/tests/gtest/TestStackWalk.cpp b/mozglue/tests/gtest/TestStackWalk.cpp new file mode 100644 index 0000000000..cec21ffc5e --- /dev/null +++ b/mozglue/tests/gtest/TestStackWalk.cpp @@ -0,0 +1,279 @@ +/* -*- 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/. */ + +// The header under test. +#include "mozilla/StackWalk.h" + +#include "mozilla/Assertions.h" +#include "mozilla/Attributes.h" + +#include <algorithm> + +#include "gtest/gtest.h" + +MOZ_EXPORT bool gStackWalkTesterDummy = true; + +struct StackWalkTester; + +// Descriptor of the recursive function calls wanted, and for each of them +// whether to perform tail call optimization or not. +struct CallInfo { + int (*mFunc)(int aDepth, int aLastSkipped, int aIgnored, + StackWalkTester& aTester); + bool mTailCall; + + bool TailCall() { +#if defined(__i386__) || defined(MOZ_CODE_COVERAGE) + // We can't make tail calls happen on i386 because all arguments to + // functions are on the stack, so the stack pointer needs to be updated + // before the call and restored after the call, so tail call optimization + // never happens. + // Similarly, code-coverage flags don't guarantee that tail call + // optimization will happen. + return false; +#else + return mTailCall; +#endif + } +}; + +struct PCRange { + void* mStart; + void* mEnd; +}; + +// PCRange pretty printer for gtest assertions. +std::ostream& operator<<(std::ostream& aStream, const PCRange& aRange) { + aStream << aRange.mStart; + aStream << "-"; + aStream << aRange.mEnd; + return aStream; +} + +// Allow to use EXPECT_EQ with a vector of PCRanges and a vector of plain +// addresses, allowing a more useful output when the test fails, showing +// both lists. +bool operator==(const std::vector<PCRange>& aRanges, + const std::vector<void*>& aPtrs) { + if (aRanges.size() != aPtrs.size()) { + return false; + } + for (size_t i = 0; i < aRanges.size(); i++) { + auto range = aRanges[i]; + auto ptr = reinterpret_cast<uintptr_t>(aPtrs[i]); + if (ptr <= reinterpret_cast<uintptr_t>(range.mStart) || + ptr >= reinterpret_cast<uintptr_t>(range.mEnd)) { + return false; + } + } + return true; +} + +struct StackWalkTester { + // Description of the recursion of functions to perform for the testcase. + std::vector<CallInfo> mFuncCalls; + // Collection of PCs reported by MozStackWalk. + std::vector<void*> mFramePCs; + // Collection of PCs expected per what was observed while recursing. + std::vector<PCRange> mExpectedFramePCs; + // The aFirstFramePC value that will be passed to MozStackWalk. + void* mFirstFramePC = nullptr; + + // Callback to be given to the stack walker. + // aClosure should point at an instance of this class. + static void StackWalkCallback(uint32_t aFrameNumber, void* aPC, void* aSP, + void* aClosure) { + ASSERT_NE(aClosure, nullptr); + StackWalkTester& tester = *reinterpret_cast<StackWalkTester*>(aClosure); + tester.mFramePCs.push_back(aPC); + EXPECT_EQ(tester.mFramePCs.size(), size_t(aFrameNumber)) + << "Frame number doesn't match"; + } + + // Callers of this function get a range of addresses with: + // ``` + // label: + // recursion(); + // AddExpectedPC(&&label); + // ``` + // This intends to record the range from label to the return of AddExpectedPC. + // The ideal code would be: + // ``` + // recursion(); + // label: + // AddExpectedPC(&&label); + // ``` + // and we wouldn't need to keep ranges. But while this works fine with Clang, + // GCC actually sometimes reorders code such the address received by + // AddExpectedPC is the address *before* the recursion. + // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99784 Using a label before the + // recursion and CallerPC() from a function call after the recursion makes it + // less likely for things to go wrong. + MOZ_NEVER_INLINE void AddExpectedPC(void* aPC) { + mExpectedFramePCs.push_back({aPC, CallerPC()}); + } + + // Function intended to be called in sequence for recursion. + // CallInfo lists are meant to contain a sequence of IntermediateCallback<1>, + // IntermediateCallback<2>, etc. + // aDepth is a counter of how deep the recursion has gone so far; + // aLastSkipped is the depth of the last frame we want skipped in the + // testcase; aIgnored is there to avoid the compiler merging both recursive + // function calls, which would prevent tail call optimization happening on one + // of them. aTester is the instance of this class for the testcase. + template <int Id> + MOZ_NEVER_INLINE MOZ_EXPORT static int IntermediateCallback( + int aDepth, int aLastSkipped, int aIgnored, StackWalkTester& aTester) { + auto& callInfo = aTester.mFuncCalls.at(aDepth + 1); + if (aDepth == aLastSkipped) { + aTester.mFirstFramePC = CallerPC(); + } + if (aTester.mFuncCalls.at(aDepth).TailCall()) { + return callInfo.mFunc(aDepth + 1, aLastSkipped, Id, aTester); + // Since we're doing a tail call, we're not expecting this frame appearing + // in the trace. + } + here: + callInfo.mFunc(aDepth + 1, aLastSkipped, Id + 1, aTester); + aTester.AddExpectedPC(&&here); + return 0; + } + +#if defined(__clang__) + __attribute__((no_sanitize("function"))) +#endif + MOZ_NEVER_INLINE MOZ_EXPORT static void + LeafCallback(int aDepth, int aLastSkipped, int aIgnored, + StackWalkTester& aTester) { + if (aDepth == aLastSkipped) { + aTester.mFirstFramePC = CallerPC(); + } + if (aTester.mFuncCalls.at(aDepth).TailCall()) { + // For the same reason that we have the aIgnored argument on these + // callbacks, we need to avoid both MozStackWalk calls to be merged by the + // compiler, so we use different values of aMaxFrames for that. + return MozStackWalk(StackWalkTester::StackWalkCallback, + aTester.mFirstFramePC, + /*aMaxFrames*/ 19, &aTester); + // Since we're doing a tail call, we're not expecting this frame appearing + // in the trace. + } + here: + MozStackWalk(StackWalkTester::StackWalkCallback, aTester.mFirstFramePC, + /*aMaxFrames*/ 20, &aTester); + aTester.AddExpectedPC(&&here); + // Because we return nothing from this function, simply returning here would + // produce a tail-call optimization, which we explicitly don't want to + // happen. So we add a branch that depends on an extern value to prevent + // that from happening. + MOZ_RELEASE_ASSERT(gStackWalkTesterDummy); + } + + explicit StackWalkTester(std::initializer_list<CallInfo> aFuncCalls) + : mFuncCalls(aFuncCalls) {} + + // Dump a vector of PCRanges as WalkTheStack would, for test failure output. + // Only the end of the range is shown. Not ideal, but + // MozFormatCodeAddressDetails only knows to deal with one address at a time. + // The full ranges would be printed by EXPECT_EQ anyways. + static std::string DumpFrames(std::vector<PCRange>& aFramePCRanges) { + std::vector<void*> framePCs; + framePCs.reserve(aFramePCRanges.size()); + for (auto range : aFramePCRanges) { + framePCs.push_back(range.mEnd); + } + return DumpFrames(framePCs); + } + + // Dump a vector of addresses as WalkTheStack would, for test failure output. + static std::string DumpFrames(std::vector<void*>& aFramePCs) { + size_t n = 0; + std::string result; + for (auto* framePC : aFramePCs) { + char buf[1024]; + MozCodeAddressDetails details; + result.append(" "); + n++; + if (MozDescribeCodeAddress(framePC, &details)) { + int length = + MozFormatCodeAddressDetails(buf, sizeof(buf), n, framePC, &details); + result.append(buf, std::min(length, (int)sizeof(buf) - 1)); + } else { + result.append("MozDescribeCodeAddress failed"); + } + result.append("\n"); + } + return result; + } + + // Dump a description of the given test case. + static std::string DumpFuncCalls(std::vector<CallInfo>& aFuncCalls) { + std::string result; + for (auto funcCall : aFuncCalls) { + MozCodeAddressDetails details; + result.append(" "); + if (MozDescribeCodeAddress(reinterpret_cast<void*>(funcCall.mFunc), + &details)) { + result.append(details.function); + if (funcCall.TailCall()) { + result.append(" tail call"); + } + } else { + result.append("MozDescribeCodeAddress failed"); + } + result.append("\n"); + } + return result; + } + + MOZ_EXPORT MOZ_NEVER_INLINE void RunTest(int aLastSkipped) { + ASSERT_TRUE(aLastSkipped < (int)mFuncCalls.size()); + mFramePCs.clear(); + mExpectedFramePCs.clear(); + mFirstFramePC = nullptr; + auto& callInfo = mFuncCalls.at(0); + here: + callInfo.mFunc(0, aLastSkipped, 0, *this); + AddExpectedPC(&&here); + if (aLastSkipped < 0) { + aLastSkipped = mFuncCalls.size(); + } + for (int i = (int)mFuncCalls.size() - 1; i >= aLastSkipped; i--) { + if (!mFuncCalls.at(i).TailCall()) { + mExpectedFramePCs.erase(mExpectedFramePCs.begin()); + } + } + mFramePCs.resize(std::min(mExpectedFramePCs.size(), mFramePCs.size())); + EXPECT_EQ(mExpectedFramePCs, mFramePCs) + << "Expected frames:\n" + << DumpFrames(mExpectedFramePCs) << "Found frames:\n" + << DumpFrames(mFramePCs) + << "Function calls data (last skipped: " << aLastSkipped << "):\n" + << DumpFuncCalls(mFuncCalls); + } +}; + +TEST(TestStackWalk, StackWalk) +{ + const auto foo = StackWalkTester::IntermediateCallback<1>; + const auto bar = StackWalkTester::IntermediateCallback<2>; + const auto qux = StackWalkTester::IntermediateCallback<3>; + const auto leaf = reinterpret_cast<int (*)(int, int, int, StackWalkTester&)>( + StackWalkTester::LeafCallback); + + const std::initializer_list<CallInfo> tests[] = { + {{foo, false}, {bar, false}, {qux, false}, {leaf, false}}, + {{foo, false}, {bar, true}, {qux, false}, {leaf, false}}, + {{foo, false}, {bar, false}, {qux, false}, {leaf, true}}, + {{foo, true}, {bar, false}, {qux, true}, {leaf, true}}, + }; + for (auto test : tests) { + StackWalkTester tester(test); + for (int i = -1; i < (int)test.size(); i++) { + tester.RunTest(i); + } + } +} diff --git a/mozglue/tests/gtest/moz.build b/mozglue/tests/gtest/moz.build new file mode 100644 index 0000000000..8f6ba9ad7b --- /dev/null +++ b/mozglue/tests/gtest/moz.build @@ -0,0 +1,20 @@ +# 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/. + +if CONFIG["OS_ARCH"] == "WINNT": + SOURCES += [ + "TestNativeNtGTest.cpp", + ] + + +if not CONFIG["MOZ_TSAN"]: + SOURCES += [ + "TestStackWalk.cpp", + ] + + # The test relies on optimizations being on, so we can't let --disable-optimize + # getting in the way. See details in the source file. + SOURCES["TestStackWalk.cpp"].flags += ["-O2"] + +FINAL_LIBRARY = "xul-gtest" diff --git a/mozglue/tests/moz.build b/mozglue/tests/moz.build new file mode 100644 index 0000000000..ac68508ebd --- /dev/null +++ b/mozglue/tests/moz.build @@ -0,0 +1,53 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# 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/. + +DisableStlWrapping() + +# Important: for CppUnitTests to be run, they also need to be added +# to testing/cppunittest.ini. + +GeckoCppUnitTests( + [ + "ShowSSEConfig", + ], + linkage=None, +) + +CppUnitTests( + [ + "TestBaseProfiler", + "TestIntegerPrintfMacros", + "TestPrintf", + ] +) + +with Files("TestBaseProfiler.cpp"): + BUG_COMPONENT = ("Core", "Gecko Profiler") + +if CONFIG["OS_ARCH"] == "WINNT": + GeckoCppUnitTests( + [ + "TestNativeNt", + "TestPEExportSection", + "TestStackCookie", + "TestTimeStampWin", + ], + linkage=None, + ) + OS_LIBS += [ + "ntdll", + "version", + ] + +if CONFIG["OS_TARGET"] == "WINNT" and CONFIG["CC_TYPE"] in ("gcc", "clang"): + # This allows us to use wmain as the entry point on mingw + LDFLAGS += [ + "-municode", + ] + +TEST_DIRS += [ + "gtest", +] |