Skip to content

Conversation

@hctim
Copy link
Collaborator

@hctim hctim commented Sep 15, 2023

This test asserts that HWASan memory usage can be gathered from
/proc/$PID/maps. This worked because the old Android-specific versions
of the anonymous mmap patches functioned by providing a userland cstring
pointer to the kernel, and the underlying cstring was collected from
that pointer address dynamically when /proc/$PID/maps was being
decorated. This had some obvious flaws, you had to keep the string
around for the lifetime of the program.

Now, the official kernel implementation of this is to copy the string
into a kernel struct at prctl()-time. Thus, the name that you give at
prctl()-time sticks around and can't be changed by userspace outside of
another prctl call. This breaks the feature, but this is fine, it's not
like anyone's really relying on it anyway. So, let's make sure we don't
continue failing the test on new versions of Android where the kernel
has the upstream version of the named-anonymous-mappings work.

This test asserts that HWASan memory usage can be gathered from /proc/$PID/maps. This worked because the old Android-specific versions of the anonymous mmap patches functioned by providing a userland cstring pointer to the kernel, and the underlying cstring was collected from that pointer address dynamically when /proc/$PID/maps was being decorated. This had some obvious flaws, you had to keep the string around for the lifetime of the program. Now, the official kernel implementation of this is to copy the string into a kernel struct at prctl()-time. Thus, the name that you give at prctl()-time sticks around and can't be changed by userspace outside of another prctl call. This breaks the feature, but this is fine, it's not like anyone's really relying on it anyway. So, let's make sure we don't continue failing the test on new versions of Android where the kernel has the upstream version of the named-anonymous-mappings work.
@llvmbot
Copy link
Member

llvmbot commented Sep 15, 2023

@llvm/pr-subscribers-compiler-rt-sanitizer

Changes

This test asserts that HWASan memory usage can be gathered from
/proc/$PID/maps. This worked because the old Android-specific versions
of the anonymous mmap patches functioned by providing a userland cstring
pointer to the kernel, and the underlying cstring was collected from
that pointer address dynamically when /proc/$PID/maps was being
decorated. This had some obvious flaws, you had to keep the string
around for the lifetime of the program.

Now, the official kernel implementation of this is to copy the string
into a kernel struct at prctl()-time. Thus, the name that you give at
prctl()-time sticks around and can't be changed by userspace outside of
another prctl call. This breaks the feature, but this is fine, it's not
like anyone's really relying on it anyway. So, let's make sure we don't
continue failing the test on new versions of Android where the kernel
has the upstream version of the named-anonymous-mappings work.


Full diff: https://github.com/llvm/llvm-project/pull/66550.diff

1 Files Affected:

  • (removed) compiler-rt/test/hwasan/TestCases/print-memory-usage-android.c (-21)
diff --git a/compiler-rt/test/hwasan/TestCases/print-memory-usage-android.c b/compiler-rt/test/hwasan/TestCases/print-memory-usage-android.c deleted file mode 100644 index 5a057928ba7521c..000000000000000 --- a/compiler-rt/test/hwasan/TestCases/print-memory-usage-android.c +++ /dev/null @@ -1,21 +0,0 @@ -// 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 <sys/types.h> -#include <unistd.h> -#include <stdlib.h> -#include <stdio.h> - -// The function needs to be unsanitized in order for &cmd to be untagged. This -// address is passed to system() and then to execve() syscall. The tests need to -// run on unpatched linux kernel, which at this time does not accept tagged -// pointers in system call arguments (but there is hope: see -// https://lore.kernel.org/patchwork/cover/979328). -__attribute__((no_sanitize("hwaddress"))) -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]*]] -} 
@vitalybuka vitalybuka merged commit 231d3e5 into llvm:main Sep 15, 2023
@hctim hctim deleted the hwasan/delete-android-mapname branch September 15, 2023 21:26
ZijunZhaoCCK pushed a commit to ZijunZhaoCCK/llvm-project that referenced this pull request Sep 19, 2023
This test asserts that HWASan memory usage can be gathered from /proc/$PID/maps. This worked because the old Android-specific versions of the anonymous mmap patches functioned by providing a userland cstring pointer to the kernel, and the underlying cstring was collected from that pointer address dynamically when /proc/$PID/maps was being decorated. This had some obvious flaws, you had to keep the string around for the lifetime of the program. Now, the official kernel implementation of this is to copy the string into a kernel struct at prctl()-time. Thus, the name that you give at prctl()-time sticks around and can't be changed by userspace outside of another prctl call. This breaks the feature, but this is fine, it's not like anyone's really relying on it anyway. So, let's make sure we don't continue failing the test on new versions of Android where the kernel has the upstream version of the named-anonymous-mappings work.
zahiraam pushed a commit to tahonermann/llvm-project that referenced this pull request Oct 24, 2023
This test asserts that HWASan memory usage can be gathered from /proc/$PID/maps. This worked because the old Android-specific versions of the anonymous mmap patches functioned by providing a userland cstring pointer to the kernel, and the underlying cstring was collected from that pointer address dynamically when /proc/$PID/maps was being decorated. This had some obvious flaws, you had to keep the string around for the lifetime of the program. Now, the official kernel implementation of this is to copy the string into a kernel struct at prctl()-time. Thus, the name that you give at prctl()-time sticks around and can't be changed by userspace outside of another prctl call. This breaks the feature, but this is fine, it's not like anyone's really relying on it anyway. So, let's make sure we don't continue failing the test on new versions of Android where the kernel has the upstream version of the named-anonymous-mappings work.
zahiraam pushed a commit to tahonermann/llvm-project that referenced this pull request Oct 24, 2023
This test asserts that HWASan memory usage can be gathered from /proc/$PID/maps. This worked because the old Android-specific versions of the anonymous mmap patches functioned by providing a userland cstring pointer to the kernel, and the underlying cstring was collected from that pointer address dynamically when /proc/$PID/maps was being decorated. This had some obvious flaws, you had to keep the string around for the lifetime of the program. Now, the official kernel implementation of this is to copy the string into a kernel struct at prctl()-time. Thus, the name that you give at prctl()-time sticks around and can't be changed by userspace outside of another prctl call. This breaks the feature, but this is fine, it's not like anyone's really relying on it anyway. So, let's make sure we don't continue failing the test on new versions of Android where the kernel has the upstream version of the named-anonymous-mappings work.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment