
11b3a0c2c5db
115412c37195
#explicit zero 1131456ac632
#brk 1158ebc243ea
#random 115323b39f7e
#undefined 116a91d9dddb
#merge 11a042b5a0ba
#vla formatting 119ec639de1b
#pthread 1149571a0a49
#read only 11149cc5ccb8
#zero 112e613ccbe7
#fork mmap 11e239c7dff8
#memprot pthread 110b03d92b7f
#xor 11de08419b82
#junk 11897d4903e2
#guard 11648cd68ca3
#ptrhread guard 110bc4dbcbd2
#stack rand 10aa9cc05d07
10a8cdbb6352
#explicit zero 10b28302c668
#brk 109f8be7d07c
#random 10cb91a7ee3a
#undefined 1008279e2fdd
#merge 106a18bd565d
#vla formatting 102f392c2d08
#pthread 108bbce1bc50
#read only 10725f61db82
#zero 104cd257135f
#fork mmap 109220cf622b
#memprot pthread 108ef71d1ffd
#memprot exit 100eaef1abbd
#xor 1064f1cc2148
#junk 105c42a527cf
#guard 105cc8c34e60
#pthread guard 107f61cc8a1c
#stack rand 9abdf523d26
9e4b9b31e6f
#explicit zero 9a3a22a63d2
#brk 97444dbc3cf
#random 9dcd3b72ac9
#undefined 9543e1df342
#merge 9611e5691f7
#vla formatting 98de97ce864
#pthread 9a475717042
#read only 97f0947cc0e
#zero 9e9751d3370
#fork mmap 983cd86d0d5
#memprot pthread 91ebb165455
#memprot exit 9488ba483cf
#xor 9f9351d884b
#junk 985e5bca0a5
#move Signed-off-by: Tad <tad@spotco.us>
86 lines
3.5 KiB
Diff
86 lines
3.5 KiB
Diff
From a8cdbb6352e4ff708b791c7f3a976de8a6383105 Mon Sep 17 00:00:00 2001
|
|
From: Daniel Micay <danielmicay@gmail.com>
|
|
Date: Thu, 5 Feb 2015 21:53:16 -0500
|
|
Subject: [PATCH] add a real explicit_bzero implementation
|
|
|
|
Clang, GCC and other compilers special-case standard C functions like
|
|
memset. Calls to memset will be optimized out.
|
|
|
|
OpenBSD provides explicit_bzero to work around this but Android simply
|
|
defines it as memset so nothing prevents it from being optimized away.
|
|
|
|
This implementation uses a memory read constraint via empty inline
|
|
assembly rather than something that may be broken via link-time
|
|
optimization in the future.
|
|
---
|
|
libc/Android.bp | 1 +
|
|
libc/bionic/explicit_bzero.cpp | 7 +++++++
|
|
libc/include/string.h | 1 +
|
|
libc/libc.map.txt | 1 +
|
|
libc/upstream-openbsd/android/include/openbsd-compat.h | 2 --
|
|
5 files changed, 10 insertions(+), 2 deletions(-)
|
|
create mode 100644 libc/bionic/explicit_bzero.cpp
|
|
|
|
diff --git a/libc/Android.bp b/libc/Android.bp
|
|
index a5990288d2..47fccde7e7 100644
|
|
--- a/libc/Android.bp
|
|
+++ b/libc/Android.bp
|
|
@@ -1041,6 +1041,7 @@ cc_library_static {
|
|
"bionic/eventfd_read.cpp",
|
|
"bionic/eventfd_write.cpp",
|
|
"bionic/exec.cpp",
|
|
+ "bionic/explicit_bzero.cpp",
|
|
"bionic/faccessat.cpp",
|
|
"bionic/fchmod.cpp",
|
|
"bionic/fchmodat.cpp",
|
|
diff --git a/libc/bionic/explicit_bzero.cpp b/libc/bionic/explicit_bzero.cpp
|
|
new file mode 100644
|
|
index 0000000000..b06daa1386
|
|
--- /dev/null
|
|
+++ b/libc/bionic/explicit_bzero.cpp
|
|
@@ -0,0 +1,7 @@
|
|
+#include <string.h>
|
|
+
|
|
+void* explicit_bzero(void* s, size_t n) {
|
|
+ void *ptr = memset(s, 0, n);
|
|
+ __asm__ __volatile__("" : : "r"(ptr) : "memory");
|
|
+ return ptr;
|
|
+}
|
|
diff --git a/libc/include/string.h b/libc/include/string.h
|
|
index 25f6673a99..6c7b4eba0f 100644
|
|
--- a/libc/include/string.h
|
|
+++ b/libc/include/string.h
|
|
@@ -56,6 +56,7 @@ void* mempcpy(void* __dst, const void* __src, size_t __n) __INTRODUCED_IN(23);
|
|
#endif
|
|
void* memmove(void* __dst, const void* __src, size_t __n);
|
|
void* memset(void* __dst, int __ch, size_t __n);
|
|
+void* explicit_bzero(void *s, size_t n);
|
|
void* memmem(const void* __haystack, size_t __haystack_size, const void* __needle, size_t __needle_size) __attribute_pure__;
|
|
|
|
char* strchr(const char* __s, int __ch) __attribute_pure__;
|
|
diff --git a/libc/libc.map.txt b/libc/libc.map.txt
|
|
index 88192239a1..0ec519b9c8 100644
|
|
--- a/libc/libc.map.txt
|
|
+++ b/libc/libc.map.txt
|
|
@@ -332,6 +332,7 @@ LIBC {
|
|
execvp;
|
|
execvpe; # introduced=21
|
|
exit;
|
|
+ explicit_bzero;
|
|
faccessat; # introduced-arm=16 introduced-arm64=21 introduced-mips=16 introduced-mips64=21 introduced-x86=16 introduced-x86_64=21
|
|
fallocate; # introduced=21
|
|
fallocate64; # introduced=21
|
|
diff --git a/libc/upstream-openbsd/android/include/openbsd-compat.h b/libc/upstream-openbsd/android/include/openbsd-compat.h
|
|
index c99e2ce9df..62cb51e9fc 100644
|
|
--- a/libc/upstream-openbsd/android/include/openbsd-compat.h
|
|
+++ b/libc/upstream-openbsd/android/include/openbsd-compat.h
|
|
@@ -65,8 +65,6 @@ extern const char* __progname;
|
|
/* OpenBSD has this, but we can't really implement it correctly on Linux. */
|
|
#define issetugid() 0
|
|
|
|
-#define explicit_bzero(p, s) memset(p, 0, s)
|
|
-
|
|
/* OpenBSD has these in <sys/param.h>, but "ALIGN" isn't something we want to reserve. */
|
|
#define ALIGNBYTES (sizeof(uintptr_t) - 1)
|
|
#define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) &~ ALIGNBYTES)
|