Merge "Enable exporting the non-uapi scsi headers."
diff --git a/libc/SECCOMP_WHITELIST.TXT b/libc/SECCOMP_WHITELIST.TXT
index a2a54c6..d2ab20e 100644
--- a/libc/SECCOMP_WHITELIST.TXT
+++ b/libc/SECCOMP_WHITELIST.TXT
@@ -115,3 +115,6 @@
 int mlock2(const void* addr, size_t len, int flags) all
 ssize_t preadv2(int fd, const struct iovec* iov, int iovcnt, off_t offset, int flags) all
 ssize_t pwritev2(int fd, const struct iovec* iov, int iovcnt, off_t offset, int flags) all
+
+# b/37769298
+int dup2(int oldfd, int newfd)	arm,x86,mips
diff --git a/libc/malloc_debug/README.md b/libc/malloc_debug/README.md
index 59bd525..b77af6d 100644
--- a/libc/malloc_debug/README.md
+++ b/libc/malloc_debug/README.md
@@ -396,22 +396,27 @@
     adb shell setprop libc.debug.malloc.options "\"backtrace guards\""
     adb shell start
 
-Enable malloc debug when multiple processes have the same name. This method
-can be used to enable malloc debug for only a very specific process if
-multiple processes have the same name.
+Note: The two levels of quoting in the adb shell command is necessary.
+The outer layer of quoting is for the shell on the host, to ensure that the
+inner layer of quoting is sent to the device, to make 'backtrace guards'
+a single argument.
 
-Note: The double quotes in the adb shell command are necessary. Otherwise,
-the setprop command will fail since the backtrace guards options will look
-like two arguments instead of one.
+Enable malloc debug using an environment variable (pre-O Android release):
 
     adb shell
-    # setprop libc.debug.malloc.env_enabled
+    # setprop libc.debug.malloc.env_enabled 1
     # setprop libc.debug.malloc.options backtrace
-    # export LIBC_DEBUG_MALLOC_ENABLE 1
+    # export LIBC_DEBUG_MALLOC_ENABLE=1
     # ls
 
-Enable malloc debug and dump the native allocation with backtraces to
-a file. This only works for zygote based java processes.
+Enable malloc debug using an environment variable (Android O or later):
+
+    adb shell
+    # export LIBC_DEBUG_MALLOC_OPTIONS=backtrace
+    # ls
+
+Any process spawned from this shell will run with malloc debug enabled
+using the backtrace option.
 
     adb shell stop
     adb shell setprop libc.debug.malloc.options backtrace
@@ -419,5 +424,32 @@
     adb shell am dumpheap -n <PID_TO_DUMP> /data/local/tmp/heap.txt
 
 It is possible to use the backtrace\_enable\_on\_signal option as well,
-but it must be enabled through the signal before the file will contain
-any data.
+but, obviously, it must be enabled through the signal before the file will
+contain any data.
+
+To analyze the data produced by the dumpheap command, run this script:
+
+    development/scripts/native_heapdump_viewer.py
+
+In order for the script to properly symbolize the stacks in the file,
+make sure the script is executed from the tree that built the image.
+Below is an example of how to execute the script using the dump created by the
+above command:
+
+    adb shell pull /data/local/tmp/heap.txt .
+    development/scripts/native_heapdump_viewer.py heap.txt > heap_info.txt
+
+Enable malloc debug for a specific program/application (Android O or later):
+
+    adb shell setprop wrap.<APP> '"LIBC_DEBUG_MALLOC_OPTIONS=backtrace logwrapper"'
+
+For example, to enable malloc debug for the google search box (Android O or later):
+
+    adb shell setprop wrap.com.google.android.googlequicksearchbox '"LIBC_DEBUG_MALLOC_OPTIONS=backtrace logwrapper"'
+    adb shell am force-stop com.google.android.googlequicksearchbox
+
+NOTE: On pre-O versions of the Android OS, property names had a length limit
+of 32. This meant that to create a wrap property with the name of the app, it
+was necessary to truncate the name to fit. On O, property names can be
+an order of magnitude larger, so there should be no need to truncate the name
+at all.
diff --git a/libc/malloc_debug/backtrace.cpp b/libc/malloc_debug/backtrace.cpp
index 88f5a1d..907944f 100644
--- a/libc/malloc_debug/backtrace.cpp
+++ b/libc/malloc_debug/backtrace.cpp
@@ -153,18 +153,27 @@
     if (soname == nullptr) {
       soname = "<unknown>";
     }
+
+    char offset_buf[128];
+    if (entry != nullptr && entry->offset != 0) {
+      snprintf(offset_buf, sizeof(offset_buf), " (offset 0x%" PRIxPTR ")", entry->offset);
+    } else {
+      offset_buf[0] = '\0';
+    }
+
     char buf[1024];
     if (symbol != nullptr) {
       char* demangled_symbol = __cxa_demangle(symbol, nullptr, nullptr, nullptr);
       const char* best_name = (demangled_symbol != nullptr) ? demangled_symbol : symbol;
 
       async_safe_format_buffer(
-          buf, sizeof(buf), "          #%02zd  pc %" PAD_PTR "  %s (%s+%" PRIuPTR ")\n", frame_num,
-          rel_pc, soname, best_name, frames[frame_num] - offset);
+          buf, sizeof(buf), "          #%02zd  pc %" PAD_PTR "  %s%s (%s+%" PRIuPTR ")\n", frame_num,
+          rel_pc, soname, offset_buf, best_name, frames[frame_num] - offset);
       free(demangled_symbol);
     } else {
       async_safe_format_buffer(
-          buf, sizeof(buf), "          #%02zd  pc %" PAD_PTR "  %s\n", frame_num, rel_pc, soname);
+          buf, sizeof(buf), "          #%02zd  pc %" PAD_PTR "  %s%s\n", frame_num, rel_pc, soname,
+          offset_buf);
     }
     str += buf;
   }
diff --git a/libc/seccomp/arm_policy.cpp b/libc/seccomp/arm_policy.cpp
index f565d63..a395188 100644
--- a/libc/seccomp/arm_policy.cpp
+++ b/libc/seccomp/arm_policy.cpp
@@ -35,9 +35,9 @@
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 56, 103, 102), //ioctl|fcntl
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 58, 102, 101), //setpgid
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 66, 3, 0),
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 64, 1, 0),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 63, 1, 0),
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 62, 99, 98), //umask|chroot
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 65, 98, 97), //getppid
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 65, 98, 97), //dup2|getppid
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 74, 1, 0),
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 68, 96, 95), //setsid|sigaction
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 76, 95, 94), //sethostname|setrlimit
diff --git a/libc/seccomp/mips_policy.cpp b/libc/seccomp/mips_policy.cpp
index 57f3210..12fb1a9 100644
--- a/libc/seccomp/mips_policy.cpp
+++ b/libc/seccomp/mips_policy.cpp
@@ -32,11 +32,11 @@
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 4054, 1, 0),
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 4053, 89, 88), //geteuid|getegid|acct|umount2
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 4056, 88, 87), //ioctl|fcntl
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 4064, 3, 0),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 4063, 3, 0),
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 4060, 1, 0),
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 4058, 85, 84), //setpgid
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 4062, 84, 83), //umask|chroot
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 4065, 83, 82), //getppid
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 4065, 83, 82), //dup2|getppid
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 4103, 13, 0),
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 4088, 7, 0),
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 4074, 3, 0),
diff --git a/libc/seccomp/x86_policy.cpp b/libc/seccomp/x86_policy.cpp
index 254db43..3247e45 100644
--- a/libc/seccomp/x86_policy.cpp
+++ b/libc/seccomp/x86_policy.cpp
@@ -32,11 +32,11 @@
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 54, 1, 0),
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 53, 91, 90), //acct|umount2
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 56, 90, 89), //ioctl|fcntl
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 64, 3, 0),
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 63, 3, 0),
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 60, 1, 0),
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 58, 87, 86), //setpgid
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 62, 86, 85), //umask|chroot
-BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 65, 85, 84), //getppid
+BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 65, 85, 84), //dup2|getppid
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 96, 13, 0),
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 88, 7, 0),
 BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 77, 3, 0),
diff --git a/tests/sys_sysinfo_test.cpp b/tests/sys_sysinfo_test.cpp
index d7d0f6e..ea35b4e 100644
--- a/tests/sys_sysinfo_test.cpp
+++ b/tests/sys_sysinfo_test.cpp
@@ -28,13 +28,14 @@
   ASSERT_GE(nprocs_conf, nprocs);
   ASSERT_EQ(sysconf(_SC_NPROCESSORS_CONF), nprocs_conf);
 
-  long avail_phys_pages = get_avphys_pages();
-  ASSERT_GT(avail_phys_pages, 0);
-  ASSERT_EQ(sysconf(_SC_AVPHYS_PAGES), avail_phys_pages);
-
   long phys_pages = get_phys_pages();
+  long avail_phys_pages = get_avphys_pages();
+
   ASSERT_GE(phys_pages, avail_phys_pages);
   ASSERT_EQ(sysconf(_SC_PHYS_PAGES), phys_pages);
+
+  ASSERT_GT(avail_phys_pages, 0);
+  ASSERT_GE(phys_pages, sysconf(_SC_AVPHYS_PAGES));
 }
 
 TEST(sys_sysinfo, sysinfo) {