[compiler-rt] r341735 - [hwasan] Export memory stats through /proc/$PID/maps.

Evgenii Stepanov via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 7 17:48:26 PDT 2018


The downside is that it is android specific. On vanilla linux proc/maps are
decorated by mapping a named shm region, which is not nearly as fast as
editing a buffer in memory, and AFAIK there are system-wide limits on the
number of such regions.

On Fri, Sep 7, 2018 at 5:23 PM, Peter Collingbourne <peter at pcc.me.uk> wrote:

> That's certainly a creative way of exposing process information!
>
> Now I'm wondering whether the same technique can be used for anything else.
>
> Peter
>
> On Fri, Sep 7, 2018 at 5:12 PM Evgeniy Stepanov via llvm-commits <
> llvm-commits at lists.llvm.org> wrote:
>
>> Author: eugenis
>> Date: Fri Sep  7 17:11:12 2018
>> New Revision: 341735
>>
>> URL: http://llvm.org/viewvc/llvm-project?rev=341735&view=rev
>> Log:
>> [hwasan] Export memory stats through /proc/$PID/maps.
>>
>> Adds a line to /proc/$PID/maps with more or less up-to-date memory
>> stats of the process.
>>
>> Added:
>>     compiler-rt/trunk/test/hwasan/TestCases/print-memory-usage-android.c
>> Modified:
>>     compiler-rt/trunk/lib/hwasan/hwasan.cc
>>     compiler-rt/trunk/lib/hwasan/hwasan.h
>>     compiler-rt/trunk/lib/hwasan/hwasan_allocator.h
>>     compiler-rt/trunk/lib/hwasan/hwasan_flags.inc
>>
>> Modified: compiler-rt/trunk/lib/hwasan/hwasan.cc
>> URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/
>> hwasan/hwasan.cc?rev=341735&r1=341734&r2=341735&view=diff
>> ============================================================
>> ==================
>> --- compiler-rt/trunk/lib/hwasan/hwasan.cc (original)
>> +++ compiler-rt/trunk/lib/hwasan/hwasan.cc Fri Sep  7 17:11:12 2018
>> @@ -171,22 +171,53 @@ static void HWAsanCheckFailed(const char
>>    Die();
>>  }
>>
>> -static void HwasanPrintMemoryUsage() {
>> +static constexpr uptr kMemoryUsageBufferSize = 4096;
>> +
>> +static void HwasanFormatMemoryUsage(InternalScopedString &s) {
>>    auto thread_stats = Thread::GetThreadStats();
>>    auto *sds = StackDepotGetStats();
>>    AllocatorStatCounters asc;
>>    GetAllocatorStats(asc);
>> -  Printf(
>> +  s.append(
>>        "HWASAN pid: %d rss: %zd threads: %zd stacks: %zd"
>>        " thr_aux: %zd stack_depot: %zd uniq_stacks: %zd"
>> -      " heap: %zd\n",
>> +      " heap: %zd",
>>        internal_getpid(), GetRSS(), thread_stats.n_live_threads,
>>        thread_stats.total_stack_size,
>>        thread_stats.n_live_threads * Thread::MemoryUsedPerThread(),
>> -      sds->allocated, sds->n_uniq_ids,
>> -      asc[AllocatorStatMapped]);
>> +      sds->allocated, sds->n_uniq_ids, asc[AllocatorStatMapped]);
>> +}
>> +
>> +#if SANITIZER_ANDROID
>> +static char *memory_usage_buffer = nullptr;
>> +
>> +#define PR_SET_VMA 0x53564d41
>> +#define PR_SET_VMA_ANON_NAME 0
>> +
>> +static void InitMemoryUsage() {
>> +  memory_usage_buffer =
>> +      (char *)MmapOrDie(kMemoryUsageBufferSize, "memory usage string");
>> +  CHECK(memory_usage_buffer);
>> +  memory_usage_buffer[0] = '\0';
>> +  CHECK(internal_prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME,
>> +                       (uptr)memory_usage_buffer, kMemoryUsageBufferSize,
>> +                       (uptr)memory_usage_buffer) == 0);
>>  }
>>
>> +void UpdateMemoryUsage() {
>> +  if (!flags()->export_memory_stats)
>> +    return;
>> +  if (!memory_usage_buffer)
>> +    InitMemoryUsage();
>> +  InternalScopedString s(kMemoryUsageBufferSize);
>> +  HwasanFormatMemoryUsage(s);
>> +  internal_strncpy(memory_usage_buffer, s.data(),
>> kMemoryUsageBufferSize - 1);
>> +  memory_usage_buffer[kMemoryUsageBufferSize - 1] = '\0';
>> +}
>> +#else
>> +void UpdateMemoryUsage() {}
>> +#endif
>> +
>>  } // namespace __hwasan
>>
>>  // Interface.
>> @@ -455,7 +486,11 @@ void __hwasan_handle_longjmp(const void
>>    TagMemory(sp, dst - sp, 0);
>>  }
>>
>> -void __hwasan_print_memory_usage() { HwasanPrintMemoryUsage(); }
>> +void __hwasan_print_memory_usage() {
>> +  InternalScopedString s(kMemoryUsageBufferSize);
>> +  HwasanFormatMemoryUsage(s);
>> +  Printf("%s\n", s.data());
>> +}
>>
>>  static const u8 kFallbackTag = 0xBB;
>>
>>
>> Modified: compiler-rt/trunk/lib/hwasan/hwasan.h
>> URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/
>> hwasan/hwasan.h?rev=341735&r1=341734&r2=341735&view=diff
>> ============================================================
>> ==================
>> --- compiler-rt/trunk/lib/hwasan/hwasan.h (original)
>> +++ compiler-rt/trunk/lib/hwasan/hwasan.h Fri Sep  7 17:11:12 2018
>> @@ -145,6 +145,8 @@ void HwasanTSDInit();
>>
>>  void HwasanOnDeadlySignal(int signo, void *info, void *context);
>>
>> +void UpdateMemoryUsage();
>> +
>>  }  // namespace __hwasan
>>
>>  #define HWASAN_MALLOC_HOOK(ptr, size)       \
>>
>> Modified: compiler-rt/trunk/lib/hwasan/hwasan_allocator.h
>> URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/
>> hwasan/hwasan_allocator.h?rev=341735&r1=341734&r2=341735&view=diff
>> ============================================================
>> ==================
>> --- compiler-rt/trunk/lib/hwasan/hwasan_allocator.h (original)
>> +++ compiler-rt/trunk/lib/hwasan/hwasan_allocator.h Fri Sep  7 17:11:12
>> 2018
>> @@ -34,7 +34,7 @@ struct Metadata {
>>  };
>>
>>  struct HwasanMapUnmapCallback {
>> -  void OnMap(uptr p, uptr size) const {}
>> +  void OnMap(uptr p, uptr size) const { UpdateMemoryUsage(); }
>>    void OnUnmap(uptr p, uptr size) const {
>>      // We are about to unmap a chunk of user memory.
>>      // It can return as user-requested mmap() or another thread stack.
>>
>> Modified: compiler-rt/trunk/lib/hwasan/hwasan_flags.inc
>> URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/
>> hwasan/hwasan_flags.inc?rev=341735&r1=341734&r2=341735&view=diff
>> ============================================================
>> ==================
>> --- compiler-rt/trunk/lib/hwasan/hwasan_flags.inc (original)
>> +++ compiler-rt/trunk/lib/hwasan/hwasan_flags.inc Fri Sep  7 17:11:12
>> 2018
>> @@ -49,3 +49,5 @@ HWASAN_FLAG(int, heap_history_size, 1023
>>            "The number of heap (de)allocations remembered per thread. "
>>            "Affects the quality of heap-related reports, but not the
>> ability "
>>            "to find bugs.")
>> +HWASAN_FLAG(bool, export_memory_stats, true,
>> +            "Export up-to-date memory stats through /proc")
>>
>> Added: compiler-rt/trunk/test/hwasan/TestCases/print-memory-usage-
>> android.c
>> URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/
>> test/hwasan/TestCases/print-memory-usage-android.c?rev=341735&view=auto
>> ============================================================
>> ==================
>> --- compiler-rt/trunk/test/hwasan/TestCases/print-memory-usage-android.c
>> (added)
>> +++ compiler-rt/trunk/test/hwasan/TestCases/print-memory-usage-android.c
>> Fri Sep  7 17:11:12 2018
>> @@ -0,0 +1,13 @@
>> +// Tests __hwasan_print_memory_usage through /proc/$PID/maps.
>> +// RUN: %clang_hwasan %s -o %t && %env_hwasan_opts=export_memory_stats=1
>> %run %t 2>&1 | FileCheck %s
>> +// REQUIRES: android
>> +
>> +#include <stdlib.h>
>> +#include <stdio.h>
>> +
>> +int main() {
>> +  char cmd[1024];
>> +  snprintf(cmd, sizeof(cmd), "cat /proc/%d/maps", getpid());
>> +  system(cmd);
>> +  // CHECK: HWASAN pid: [[PID:[0-9]*]] rss: {{.*}} threads: 1 stacks:
>> [[STACKS:[0-9]*]] thr_aux: {{.*}} stack_depot: {{.*}} uniq_stacks:
>> [[UNIQ_STACKS:[0-9]*]] heap: [[HEAP:[0-9]*]]
>> +}
>>
>>
>> _______________________________________________
>> llvm-commits mailing list
>> llvm-commits at lists.llvm.org
>> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
>>
>
>
> --
> --
> Peter
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180907/6f25d175/attachment.html>


More information about the llvm-commits mailing list