Snap for 10453563 from a6e85254ece44850c02ba5542d1777e7a7d0e532 to mainline-ipsec-release

Change-Id: Ibff075572da84b55160d2fec09ca69471847a3ba
diff --git a/Android.bp b/Android.bp
index 68457e5..43e1dd8 100644
--- a/Android.bp
+++ b/Android.bp
@@ -54,6 +54,13 @@
     export_include_dirs: ["include"],
     local_include_dirs: ["include"],
     version_script: "libmemunreachable.map",
+
+    static: {
+        apex_available: [
+            "//apex_available:platform",
+            "com.android.runtime",
+        ],
+    },
 }
 
 // Integration test that runs against the public API of libmemunreachable
@@ -80,7 +87,11 @@
         "tests/HeapWalker_test.cpp",
         "tests/LeakFolding_test.cpp",
     ],
-
+    tidy_checks: [
+        // Ignore memory leaks after ASSERT_NE in DisableMalloc_test.cpp.
+        "-clang-analyzer-cplusplus.NewDeleteLeaks",
+        "-clang-analyzer-unix.Malloc",
+    ],
     target: {
         android: {
             srcs: [
diff --git a/PREUPLOAD.cfg b/PREUPLOAD.cfg
new file mode 100644
index 0000000..c8dbf77
--- /dev/null
+++ b/PREUPLOAD.cfg
@@ -0,0 +1,5 @@
+[Builtin Hooks]
+clang_format = true
+
+[Builtin Hooks Options]
+clang_format = --commit ${PREUPLOAD_COMMIT} --style file --extensions c,h,cc,cpp
diff --git a/README.md b/README.md
index 9cc0c9b..7b1cb7c 100644
--- a/README.md
+++ b/README.md
@@ -33,6 +33,80 @@
 adb shell setprop wrap.[process]  "''"
 ```
 
+Starting with Android U, new malloc debug options have been added
+that allow specific sized allocation to be backtraced. The three
+new options are:
+
+- backtrace\_size
+- backtrace\_min\_size
+- backtrace\_max\_size
+
+When enabling backtracing on all allocations, it is possible to have
+the process run so slowly that the app does not come up. Or the app
+runs so slowly that the leaks do not occur. The best way to avoid any
+slowdowns or timeouts is to first run libmemunreachable and look at
+the sizes of the leaking allocations. If there is only a single
+allocation size, then use backtrace\_size which will indicate that
+backtraces should only be collected for that exact size. For example,
+if the output of dumpsys is:
+
+```
+ Unreachable memory
+  24 bytes in 2 unreachable allocations
+  ABI: 'arm64'
+
+  24 bytes unreachable at 71d37787d0
+   first 20 bytes of contents:
+   71d37787d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
+   71d37787e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
+
+  24 bytes unreachable at 71d37797d0
+   first 20 bytes of contents:
+   71d37797d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
+   71d37797e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
+```
+
+Then set the malloc debug options thusly:
+
+```
+adb shell setprop libc.debug.malloc.options "'backtrace backtrace_size=24'"
+```
+
+This will backtrace only 24 byte allocations.
+
+If the output of libmemunreachable has multiple sized allocations, set
+the backtrace\_min\_size and backtrace\_max\_size options to cover all
+of the sizes. For example, if the output of dumpsys is:
+
+```
+ Unreachable memory
+  512 bytes in 2 unreachable allocations
+  ABI: 'arm64'
+
+  320 bytes unreachable at 71d37787d0
+   first 20 bytes of contents:
+   71d37787d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
+   71d37787e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
+
+  192 bytes unreachable at 71b37b2f50
+   first 20 bytes of contents:
+   71b37b2f50: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
+   71b37b2f60: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
+```
+
+Then set the malloc debug options thusly:
+
+```
+adb shell setprop libc.debug.malloc.options "'backtrace backtrace_min_size=192 backtrace_max_size=320'"
+```
+
+This will backtrace allocations of any size between 192 bytes and 320 bytes
+inclusively.
+
+After setting the backtrace size options, restart the application so
+that running dumpsys again will include the actual backtrace of the
+leaking allocations.
+
 ### C interface ###
 
 #### `bool LogUnreachableMemory(bool log_contents, size_t limit)` ####
diff --git a/ThreadCapture.cpp b/ThreadCapture.cpp
index 45eb55d..99d6494 100644
--- a/ThreadCapture.cpp
+++ b/ThreadCapture.cpp
@@ -226,16 +226,16 @@
   thread_info.regs.assign(&regs[0], &regs[num_regs]);
 
   const int sp =
-#if defined(__x86_64__)
-      offsetof(struct pt_regs, rsp) / sizeof(uintptr_t)
-#elif defined(__i386__)
-      offsetof(struct pt_regs, esp) / sizeof(uintptr_t)
+#if defined(__aarch64__)
+      offsetof(struct user_pt_regs, sp) / sizeof(uintptr_t)
 #elif defined(__arm__)
       offsetof(struct pt_regs, ARM_sp) / sizeof(uintptr_t)
-#elif defined(__aarch64__)
-      offsetof(struct user_pt_regs, sp) / sizeof(uintptr_t)
-#elif defined(__mips__) || defined(__mips64__)
-      offsetof(struct pt_regs, regs[29]) / sizeof(uintptr_t)
+#elif defined(__i386__)
+      offsetof(struct pt_regs, esp) / sizeof(uintptr_t)
+#elif defined(__riscv)
+      offsetof(struct user_regs_struct, sp) / sizeof(uintptr_t)
+#elif defined(__x86_64__)
+      offsetof(struct pt_regs, rsp) / sizeof(uintptr_t)
 #else
 #error Unrecognized architecture
 #endif