tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

TaggedAnonymousMemory.h (3006B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 // Linux kernels since 5.17 have a feature for assigning names to
      8 // ranges of anonymous memory (i.e., memory that doesn't have a "name"
      9 // in the form of an underlying mapped file).  These names are
     10 // reported in /proc/<pid>/smaps alongside system-level memory usage
     11 // information such as Proportional Set Size (memory usage adjusted
     12 // for sharing between processes), which allows reporting this
     13 // information at a finer granularity than would otherwise be possible
     14 // (e.g., separating malloc() heap from JS heap).
     15 //
     16 // Existing memory can be tagged with MozTagAnonymousMemory(); it will
     17 // tag the range of complete pages containing the given interval, so
     18 // the results may be inexact if the range isn't page-aligned.
     19 // MozTaggedAnonymousMmap() can be used like mmap() with an extra
     20 // parameter, and will tag the returned memory if the mapping was
     21 // successful (and if it was in fact anonymous).
     22 //
     23 // NOTE: The pointer given as the "tag" argument MUST remain valid as
     24 // long as the mapping exists.  The referenced string is read when
     25 // /proc/<pid>/smaps or /proc/<pid>/maps is read, not when the tag is
     26 // established, so freeing it or changing its contents will have
     27 // unexpected results.  Using a static string is probably best.
     28 //
     29 // Also note that this header can be used by both C and C++ code.
     30 
     31 #ifndef mozilla_TaggedAnonymousMemory_h
     32 #define mozilla_TaggedAnonymousMemory_h
     33 
     34 #ifndef XP_WIN
     35 
     36 #  ifdef __wasi__
     37 #    include <stdlib.h>
     38 #  else
     39 #    include <sys/types.h>
     40 #    include <sys/mman.h>
     41 #  endif  // __wasi__
     42 
     43 #  include "mozilla/Types.h"
     44 
     45 #  ifdef XP_LINUX
     46 
     47 #    ifdef __cplusplus
     48 extern "C" {
     49 #    endif
     50 
     51 MFBT_API void MozTagAnonymousMemory(const void* aPtr, size_t aLength,
     52                                    const char* aTag);
     53 
     54 MFBT_API void* MozTaggedAnonymousMmap(void* aAddr, size_t aLength, int aProt,
     55                                      int aFlags, int aFd, off_t aOffset,
     56                                      const char* aTag);
     57 
     58 #    ifdef __cplusplus
     59 }  // extern "C"
     60 #    endif
     61 
     62 #  else  // XP_LINUX
     63 
     64 static inline void MozTagAnonymousMemory(const void* aPtr, size_t aLength,
     65                                         const char* aTag) {}
     66 
     67 static inline void* MozTaggedAnonymousMmap(void* aAddr, size_t aLength,
     68                                           int aProt, int aFlags, int aFd,
     69                                           off_t aOffset, const char* aTag) {
     70 #    ifdef __wasi__
     71  MOZ_CRASH("We don't use this memory for WASI right now.");
     72  return nullptr;
     73 #    else
     74  return mmap(aAddr, aLength, aProt, aFlags, aFd, aOffset);
     75 #    endif
     76 }
     77 
     78 #  endif  // XP_LINUX
     79 
     80 #endif  // !XP_WIN
     81 
     82 #endif  // mozilla_TaggedAnonymousMemory_h