Merge "FORTIFY_SOURCE: Add openat, fix bug"
diff --git a/libc/Android.mk b/libc/Android.mk
index 06d36fe..6b1bba4 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -140,6 +140,7 @@
 	stdio/vsscanf.c \
 	stdio/wbuf.c \
 	stdio/wsetup.c \
+	stdio/__fgets_chk.c \
 	stdio/__snprintf_chk.c \
 	stdio/__sprintf_chk.c \
 	stdio/__vsnprintf_chk.c \
diff --git a/libc/include/stdio.h b/libc/include/stdio.h
index 18b19bf..c12ddb8 100644
--- a/libc/include/stdio.h
+++ b/libc/include/stdio.h
@@ -527,6 +527,45 @@
 
 # endif /* !defined(__clang__) */
 
+extern char *__fgets_real(char *, int, FILE *)
+    __asm__(__USER_LABEL_PREFIX__ "fgets");
+extern void __fgets_too_big_error()
+    __attribute__((__error__("fgets called with size bigger than buffer")));
+extern void __fgets_too_small_error()
+    __attribute__((__error__("fgets called with size less than zero")));
+extern char *__fgets_chk(char *, int, FILE *, size_t);
+
+__BIONIC_FORTIFY_INLINE
+char *fgets(char *dest, int size, FILE *stream)
+{
+    size_t bos = __builtin_object_size(dest, 0);
+
+    // Compiler can prove, at compile time, that the passed in size
+    // is always negative. Force a compiler error.
+    if (__builtin_constant_p(size) && (size < 0)) {
+        __fgets_too_small_error();
+    }
+
+    // Compiler doesn't know destination size. Don't call __fgets_chk
+    if (bos == (size_t) -1) {
+        return __fgets_real(dest, size, stream);
+    }
+
+    // Compiler can prove, at compile time, that the passed in size
+    // is always <= the actual object size. Don't call __fgets_chk
+    if (__builtin_constant_p(size) && (size <= bos)) {
+        return __fgets_real(dest, size, stream);
+    }
+
+    // Compiler can prove, at compile time, that the passed in size
+    // is always > the actual object size. Force a compiler error.
+    if (__builtin_constant_p(size) && (size > bos)) {
+        __fgets_too_big_error();
+    }
+
+    return __fgets_chk(dest, size, stream, bos);
+}
+
 #endif /* defined(__BIONIC_FORTIFY_INLINE) */
 
 #endif /* _STDIO_H_ */
diff --git a/libc/stdio/__fgets_chk.c b/libc/stdio/__fgets_chk.c
new file mode 100644
index 0000000..19123b9
--- /dev/null
+++ b/libc/stdio/__fgets_chk.c
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <private/logd.h>
+
+/*
+ * __fgets_chk. Called in place of fgets() when we know the
+ * size of the buffer we're writing into.
+ *
+ * See
+ *   http://gcc.gnu.org/onlinedocs/gcc/Object-Size-Checking.html
+ * for details.
+ *
+ * This fgets check is called if _FORTIFY_SOURCE is defined and
+ * greater than 0.
+ */
+char *__fgets_chk(char *dest, int supplied_size,
+                  FILE *stream, size_t dest_len_from_compiler)
+{
+    if (supplied_size < 0) {
+        __libc_android_log_print(ANDROID_LOG_FATAL, "libc",
+            "*** fgets buffer size less than 0 ***\n");
+        abort();
+    }
+
+    if (((size_t) supplied_size) > dest_len_from_compiler) {
+        __libc_android_log_print(ANDROID_LOG_FATAL, "libc",
+            "*** fgets buffer overflow detected ***\n");
+        abort();
+    }
+
+    return fgets(dest, supplied_size, stream);
+}
diff --git a/linker/linker.c b/linker/linker.c
index e90ac0a..54fb22b 100644
--- a/linker/linker.c
+++ b/linker/linker.c
@@ -156,7 +156,7 @@
  * This function is an empty stub where GDB locates a breakpoint to get notified
  * about linker activity.
  */
-extern void __attribute__((noinline)) rtld_db_dlactivity(void);
+extern void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity(void);
 
 static struct r_debug _r_debug = {1, NULL, &rtld_db_dlactivity,
                                   RT_CONSISTENT, 0};
diff --git a/linker/rt.c b/linker/rt.c
index 30d5a48..afbd651 100644
--- a/linker/rt.c
+++ b/linker/rt.c
@@ -28,9 +28,9 @@
 
 /*
  * This function is an empty stub where GDB locates a breakpoint to get notified
- * about linker activity.
+ * about linker activity.  It canʼt be inlined away, canʼt be hidden.
  */
-void __attribute__((noinline)) rtld_db_dlactivity(void)
+void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity(void)
 {
 }