Merge "Work around for libraries without dt_soname"
diff --git a/libc/Android.mk b/libc/Android.mk
index d6274dd..4dd1086 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -129,8 +129,10 @@
     bionic/fchmod.cpp \
     bionic/fchmodat.cpp \
     bionic/ffs.cpp \
+    bionic/fgetxattr.cpp \
     bionic/flockfile.cpp \
     bionic/fpclassify.cpp \
+    bionic/fsetxattr.cpp \
     bionic/ftruncate.cpp \
     bionic/futimens.cpp \
     bionic/getcwd.cpp \
diff --git a/libc/SYSCALLS.TXT b/libc/SYSCALLS.TXT
index 9d7f839..4890b89 100644
--- a/libc/SYSCALLS.TXT
+++ b/libc/SYSCALLS.TXT
@@ -122,8 +122,8 @@
 int         fchown:fchown32(int, uid_t, gid_t)  arm,x86
 int         fchown:fchown(int, uid_t, gid_t)    arm64,mips,mips64,x86_64
 void        sync(void)  all
-int         fsetxattr(int, const char*, const void*, size_t, int) all
-ssize_t     fgetxattr(int, const char*, void*, size_t) all
+int         ___fsetxattr:fsetxattr(int, const char*, const void*, size_t, int) all
+ssize_t     ___fgetxattr:fgetxattr(int, const char*, void*, size_t) all
 ssize_t     flistxattr(int, char*, size_t) all
 int         fremovexattr(int, const char*) all
 
diff --git a/libc/arch-arm/syscalls/fgetxattr.S b/libc/arch-arm/syscalls/___fgetxattr.S
similarity index 81%
rename from libc/arch-arm/syscalls/fgetxattr.S
rename to libc/arch-arm/syscalls/___fgetxattr.S
index 3f1e5fc..e776cd6 100644
--- a/libc/arch-arm/syscalls/fgetxattr.S
+++ b/libc/arch-arm/syscalls/___fgetxattr.S
@@ -2,7 +2,7 @@
 
 #include <private/bionic_asm.h>
 
-ENTRY(fgetxattr)
+ENTRY(___fgetxattr)
     mov     ip, r7
     ldr     r7, =__NR_fgetxattr
     swi     #0
@@ -11,4 +11,5 @@
     bxls    lr
     neg     r0, r0
     b       __set_errno_internal
-END(fgetxattr)
+END(___fgetxattr)
+.hidden ___fgetxattr
diff --git a/libc/arch-arm/syscalls/fsetxattr.S b/libc/arch-arm/syscalls/___fsetxattr.S
similarity index 89%
rename from libc/arch-arm/syscalls/fsetxattr.S
rename to libc/arch-arm/syscalls/___fsetxattr.S
index 0e33ad2..5445191 100644
--- a/libc/arch-arm/syscalls/fsetxattr.S
+++ b/libc/arch-arm/syscalls/___fsetxattr.S
@@ -2,7 +2,7 @@
 
 #include <private/bionic_asm.h>
 
-ENTRY(fsetxattr)
+ENTRY(___fsetxattr)
     mov     ip, sp
     stmfd   sp!, {r4, r5, r6, r7}
     .cfi_def_cfa_offset 16
@@ -19,4 +19,5 @@
     bxls    lr
     neg     r0, r0
     b       __set_errno_internal
-END(fsetxattr)
+END(___fsetxattr)
+.hidden ___fsetxattr
diff --git a/libc/arch-arm64/syscalls/fgetxattr.S b/libc/arch-arm64/syscalls/___fgetxattr.S
similarity index 79%
rename from libc/arch-arm64/syscalls/fgetxattr.S
rename to libc/arch-arm64/syscalls/___fgetxattr.S
index 0d6ada7..c0334cc 100644
--- a/libc/arch-arm64/syscalls/fgetxattr.S
+++ b/libc/arch-arm64/syscalls/___fgetxattr.S
@@ -2,7 +2,7 @@
 
 #include <private/bionic_asm.h>
 
-ENTRY(fgetxattr)
+ENTRY(___fgetxattr)
     mov     x8, __NR_fgetxattr
     svc     #0
 
@@ -11,4 +11,5 @@
     b.hi    __set_errno_internal
 
     ret
-END(fgetxattr)
+END(___fgetxattr)
+.hidden ___fgetxattr
diff --git a/libc/arch-arm64/syscalls/fsetxattr.S b/libc/arch-arm64/syscalls/___fsetxattr.S
similarity index 79%
rename from libc/arch-arm64/syscalls/fsetxattr.S
rename to libc/arch-arm64/syscalls/___fsetxattr.S
index e69e718..92be8de 100644
--- a/libc/arch-arm64/syscalls/fsetxattr.S
+++ b/libc/arch-arm64/syscalls/___fsetxattr.S
@@ -2,7 +2,7 @@
 
 #include <private/bionic_asm.h>
 
-ENTRY(fsetxattr)
+ENTRY(___fsetxattr)
     mov     x8, __NR_fsetxattr
     svc     #0
 
@@ -11,4 +11,5 @@
     b.hi    __set_errno_internal
 
     ret
-END(fsetxattr)
+END(___fsetxattr)
+.hidden ___fsetxattr
diff --git a/libc/arch-mips/syscalls/fgetxattr.S b/libc/arch-mips/syscalls/___fgetxattr.S
similarity index 82%
rename from libc/arch-mips/syscalls/fgetxattr.S
rename to libc/arch-mips/syscalls/___fgetxattr.S
index 6516feb..50ab69c 100644
--- a/libc/arch-mips/syscalls/fgetxattr.S
+++ b/libc/arch-mips/syscalls/___fgetxattr.S
@@ -2,7 +2,7 @@
 
 #include <private/bionic_asm.h>
 
-ENTRY(fgetxattr)
+ENTRY(___fgetxattr)
     .set noreorder
     .cpload t9
     li v0, __NR_fgetxattr
@@ -16,4 +16,5 @@
     j t9
     nop
     .set reorder
-END(fgetxattr)
+END(___fgetxattr)
+.hidden ___fgetxattr
diff --git a/libc/arch-mips/syscalls/fsetxattr.S b/libc/arch-mips/syscalls/___fsetxattr.S
similarity index 82%
rename from libc/arch-mips/syscalls/fsetxattr.S
rename to libc/arch-mips/syscalls/___fsetxattr.S
index 663c0df..0312921 100644
--- a/libc/arch-mips/syscalls/fsetxattr.S
+++ b/libc/arch-mips/syscalls/___fsetxattr.S
@@ -2,7 +2,7 @@
 
 #include <private/bionic_asm.h>
 
-ENTRY(fsetxattr)
+ENTRY(___fsetxattr)
     .set noreorder
     .cpload t9
     li v0, __NR_fsetxattr
@@ -16,4 +16,5 @@
     j t9
     nop
     .set reorder
-END(fsetxattr)
+END(___fsetxattr)
+.hidden ___fsetxattr
diff --git a/libc/arch-mips64/syscalls/fgetxattr.S b/libc/arch-mips64/syscalls/___fgetxattr.S
similarity index 85%
rename from libc/arch-mips64/syscalls/fgetxattr.S
rename to libc/arch-mips64/syscalls/___fgetxattr.S
index 44c248a..935b080 100644
--- a/libc/arch-mips64/syscalls/fgetxattr.S
+++ b/libc/arch-mips64/syscalls/___fgetxattr.S
@@ -2,7 +2,7 @@
 
 #include <private/bionic_asm.h>
 
-ENTRY(fgetxattr)
+ENTRY(___fgetxattr)
     .set push
     .set noreorder
     li v0, __NR_fgetxattr
@@ -22,4 +22,5 @@
     j t9
     move ra, t0
     .set pop
-END(fgetxattr)
+END(___fgetxattr)
+.hidden ___fgetxattr
diff --git a/libc/arch-mips64/syscalls/fsetxattr.S b/libc/arch-mips64/syscalls/___fsetxattr.S
similarity index 85%
rename from libc/arch-mips64/syscalls/fsetxattr.S
rename to libc/arch-mips64/syscalls/___fsetxattr.S
index 0ad1f90..c02f406 100644
--- a/libc/arch-mips64/syscalls/fsetxattr.S
+++ b/libc/arch-mips64/syscalls/___fsetxattr.S
@@ -2,7 +2,7 @@
 
 #include <private/bionic_asm.h>
 
-ENTRY(fsetxattr)
+ENTRY(___fsetxattr)
     .set push
     .set noreorder
     li v0, __NR_fsetxattr
@@ -22,4 +22,5 @@
     j t9
     move ra, t0
     .set pop
-END(fsetxattr)
+END(___fsetxattr)
+.hidden ___fsetxattr
diff --git a/libc/arch-x86/syscalls/fgetxattr.S b/libc/arch-x86/syscalls/___fgetxattr.S
similarity index 92%
rename from libc/arch-x86/syscalls/fgetxattr.S
rename to libc/arch-x86/syscalls/___fgetxattr.S
index 1eff931..2891511 100644
--- a/libc/arch-x86/syscalls/fgetxattr.S
+++ b/libc/arch-x86/syscalls/___fgetxattr.S
@@ -2,7 +2,7 @@
 
 #include <private/bionic_asm.h>
 
-ENTRY(fgetxattr)
+ENTRY(___fgetxattr)
     pushl   %ebx
     .cfi_def_cfa_offset 8
     .cfi_rel_offset ebx, 0
@@ -33,4 +33,5 @@
     popl    %ecx
     popl    %ebx
     ret
-END(fgetxattr)
+END(___fgetxattr)
+.hidden ___fgetxattr
diff --git a/libc/arch-x86/syscalls/fsetxattr.S b/libc/arch-x86/syscalls/___fsetxattr.S
similarity index 93%
rename from libc/arch-x86/syscalls/fsetxattr.S
rename to libc/arch-x86/syscalls/___fsetxattr.S
index 7af0ef0..287dafc 100644
--- a/libc/arch-x86/syscalls/fsetxattr.S
+++ b/libc/arch-x86/syscalls/___fsetxattr.S
@@ -2,7 +2,7 @@
 
 #include <private/bionic_asm.h>
 
-ENTRY(fsetxattr)
+ENTRY(___fsetxattr)
     pushl   %ebx
     .cfi_def_cfa_offset 8
     .cfi_rel_offset ebx, 0
@@ -38,4 +38,5 @@
     popl    %ecx
     popl    %ebx
     ret
-END(fsetxattr)
+END(___fsetxattr)
+.hidden ___fsetxattr
diff --git a/libc/arch-x86_64/syscalls/fgetxattr.S b/libc/arch-x86_64/syscalls/___fgetxattr.S
similarity index 82%
rename from libc/arch-x86_64/syscalls/fgetxattr.S
rename to libc/arch-x86_64/syscalls/___fgetxattr.S
index 7762474..302fd77 100644
--- a/libc/arch-x86_64/syscalls/fgetxattr.S
+++ b/libc/arch-x86_64/syscalls/___fgetxattr.S
@@ -2,7 +2,7 @@
 
 #include <private/bionic_asm.h>
 
-ENTRY(fgetxattr)
+ENTRY(___fgetxattr)
     movq    %rcx, %r10
     movl    $__NR_fgetxattr, %eax
     syscall
@@ -13,4 +13,5 @@
     call    __set_errno_internal
 1:
     ret
-END(fgetxattr)
+END(___fgetxattr)
+.hidden ___fgetxattr
diff --git a/libc/arch-x86_64/syscalls/fsetxattr.S b/libc/arch-x86_64/syscalls/___fsetxattr.S
similarity index 82%
rename from libc/arch-x86_64/syscalls/fsetxattr.S
rename to libc/arch-x86_64/syscalls/___fsetxattr.S
index 97822c4..125ef20 100644
--- a/libc/arch-x86_64/syscalls/fsetxattr.S
+++ b/libc/arch-x86_64/syscalls/___fsetxattr.S
@@ -2,7 +2,7 @@
 
 #include <private/bionic_asm.h>
 
-ENTRY(fsetxattr)
+ENTRY(___fsetxattr)
     movq    %rcx, %r10
     movl    $__NR_fsetxattr, %eax
     syscall
@@ -13,4 +13,5 @@
     call    __set_errno_internal
 1:
     ret
-END(fsetxattr)
+END(___fsetxattr)
+.hidden ___fsetxattr
diff --git a/libc/bionic/fgetxattr.cpp b/libc/bionic/fgetxattr.cpp
new file mode 100644
index 0000000..6d999bf
--- /dev/null
+++ b/libc/bionic/fgetxattr.cpp
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2015 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 <sys/stat.h>
+#include <sys/types.h>
+#include <sys/xattr.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+
+extern "C" ssize_t ___fgetxattr(int, const char*, void*, size_t);
+
+ssize_t fgetxattr(int fd, const char *name, void *value, size_t size) {
+  int saved_errno = errno;
+  ssize_t result = ___fgetxattr(fd, name, value, size);
+
+  if ((result != -1) || (errno != EBADF)) {
+    return result;
+  }
+
+  // fd could be an O_PATH file descriptor, and the kernel
+  // may not directly support fgetxattr() on such a file descriptor.
+  // Use /proc/self/fd instead to emulate this support.
+  int fd_flag = fcntl(fd, F_GETFL);
+  if ((fd_flag == -1) || ((fd_flag & O_PATH) == 0)) {
+    errno = EBADF;
+    return -1;
+  }
+
+  char buf[40];
+  snprintf(buf, sizeof(buf), "/proc/self/fd/%d", fd);
+  errno = saved_errno;
+  return getxattr(buf, name, value, size);
+}
diff --git a/libc/bionic/fsetxattr.cpp b/libc/bionic/fsetxattr.cpp
new file mode 100644
index 0000000..6d2e868
--- /dev/null
+++ b/libc/bionic/fsetxattr.cpp
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2015 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 <sys/stat.h>
+#include <sys/types.h>
+#include <sys/xattr.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+
+extern "C" int ___fsetxattr(int, const char*, const void*, size_t, int);
+
+int fsetxattr(int fd, const char* name, const void* value, size_t size, int flags) {
+  int saved_errno = errno;
+  int result = ___fsetxattr(fd, name, value, size, flags);
+
+  if ((result == 0) || (errno != EBADF)) {
+    return result;
+  }
+
+  // fd could be an O_PATH file descriptor, and the kernel
+  // may not directly support fsetxattr() on such a file descriptor.
+  // Use /proc/self/fd instead to emulate this support.
+  int fd_flag = fcntl(fd, F_GETFL);
+  if ((fd_flag == -1) || ((fd_flag & O_PATH) == 0)) {
+    errno = EBADF;
+    return -1;
+  }
+
+  char buf[40];
+  snprintf(buf, sizeof(buf), "/proc/self/fd/%d", fd);
+  errno = saved_errno;
+  return setxattr(buf, name, value, size, flags);
+}
diff --git a/libc/bionic/ftruncate.cpp b/libc/bionic/ftruncate.cpp
index c073f2d..9936df0 100644
--- a/libc/bionic/ftruncate.cpp
+++ b/libc/bionic/ftruncate.cpp
@@ -18,11 +18,14 @@
 #include <sys/cdefs.h>
 #include <unistd.h>
 
-#if !defined(__USE_FILE_OFFSET64) && !defined(__LP64__)
+#if !defined(__LP64__)
+static_assert(sizeof(off_t) == 4,
+              "libc can't be built with _FILE_OFFSET_BITS=64.");
+
 // The kernel's implementation of ftruncate uses an unsigned long for the length
 // parameter, so it will not catch negative values. On the other hand
 // ftruncate64 does check for this, so just forward the call.
 int ftruncate(int filedes, off_t length) {
   return ftruncate64(filedes, length);
 }
-#endif
+#endif  // !defined(__LP64__)
diff --git a/linker/linker.cpp b/linker/linker.cpp
index 2771f68..6a1d84f 100644
--- a/linker/linker.cpp
+++ b/linker/linker.cpp
@@ -948,6 +948,17 @@
 // This is used by dlsym(3).  It performs symbol lookup only within the
 // specified soinfo object and its dependencies in breadth first order.
 const ElfW(Sym)* dlsym_handle_lookup(soinfo* si, soinfo** found, const char* name) {
+  // According to man dlopen(3) and posix docs in the case when si is handle
+  // of the main executable we need to search not only in the executable and its
+  // dependencies but also in all libraries loaded with RTLD_GLOBAL.
+  //
+  // Since RTLD_GLOBAL is always set for the main executable and all dt_needed shared
+  // libraries and they are loaded in breath-first (correct) order we can just execute
+  // dlsym(RTLD_DEFAULT, ...); instead of doing two stage lookup.
+  if (si == somain) {
+    return dlsym_linear_lookup(name, found, nullptr, RTLD_DEFAULT);
+  }
+
   SymbolName symbol_name(name);
   return dlsym_handle_lookup(si, nullptr, found, symbol_name);
 }
diff --git a/tests/Android.mk b/tests/Android.mk
index 0f3454c..37f7b4f 100644
--- a/tests/Android.mk
+++ b/tests/Android.mk
@@ -107,6 +107,7 @@
     sys_time_test.cpp \
     sys_types_test.cpp \
     sys_vfs_test.cpp \
+    sys_xattr_test.cpp \
     system_properties_test.cpp \
     time_test.cpp \
     uchar_test.cpp \
diff --git a/tests/dlfcn_test.cpp b/tests/dlfcn_test.cpp
index a5abda7..3c9b8e3 100644
--- a/tests/dlfcn_test.cpp
+++ b/tests/dlfcn_test.cpp
@@ -145,13 +145,28 @@
   dlclose(preload);
 }
 
+TEST(dlfcn, dlsym_handle_global_sym) {
+  // check that we do not look into global group
+  // when looking up symbol by handle
+  void* handle = dlopen("libtest_empty.so", RTLD_NOW);
+  dlopen("libtest_with_dependency.so", RTLD_NOW | RTLD_GLOBAL);
+  void* sym = dlsym(handle, "getRandomNumber");
+  ASSERT_TRUE(sym == nullptr);
+  ASSERT_SUBSTR("undefined symbol: getRandomNumber", dlerror());
+
+  sym = dlsym(handle, "DlSymTestFunction");
+  ASSERT_TRUE(sym == nullptr);
+  ASSERT_SUBSTR("undefined symbol: DlSymTestFunction", dlerror());
+  dlclose(handle);
+}
+
 TEST(dlfcn, dlsym_with_dependencies) {
   void* handle = dlopen("libtest_with_dependency.so", RTLD_NOW);
-  ASSERT_TRUE(handle != NULL);
+  ASSERT_TRUE(handle != nullptr);
   dlerror();
   // This symbol is in DT_NEEDED library.
   void* sym = dlsym(handle, "getRandomNumber");
-  ASSERT_TRUE(sym != NULL);
+  ASSERT_TRUE(sym != nullptr) << dlerror();
   int (*fn)(void);
   fn = reinterpret_cast<int (*)(void)>(sym);
   EXPECT_EQ(4, fn());
@@ -583,6 +598,15 @@
   // RTLD_GLOBAL implies RTLD_NODELETE, let's check that
   void* sym_after_dlclose = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
   ASSERT_EQ(sym, sym_after_dlclose);
+
+  // Check if dlsym() for main program's handle searches RTLD_GLOBAL
+  // shared libraries after symbol was not found in the main executable
+  // and dependent libraries.
+  void* handle_for_main_executable = dlopen(nullptr, RTLD_NOW);
+  sym = dlsym(handle_for_main_executable, "dlopen_testlib_simple_func");
+  ASSERT_TRUE(sym != nullptr) << dlerror();
+
+  dlclose(handle_for_main_executable);
 }
 
 // libtest_with_dependency_loop.so -> libtest_with_dependency_loop_a.so ->
diff --git a/tests/libs/Android.mk b/tests/libs/Android.mk
index 15fa55c..a5ef622 100644
--- a/tests/libs/Android.mk
+++ b/tests/libs/Android.mk
@@ -405,6 +405,14 @@
 include $(LOCAL_PATH)/Android.build.testlib.mk
 
 # -----------------------------------------------------------------------------
+# Empty library
+# -----------------------------------------------------------------------------
+libtest_empty_src_files := empty.cpp
+
+module := libtest_empty
+include $(LOCAL_PATH)/Android.build.testlib.mk
+
+# -----------------------------------------------------------------------------
 # Library with weak undefined function
 # -----------------------------------------------------------------------------
 libtest_dlopen_weak_undefined_func_src_files := \
diff --git a/tests/sys_xattr_test.cpp b/tests/sys_xattr_test.cpp
new file mode 100644
index 0000000..1842682
--- /dev/null
+++ b/tests/sys_xattr_test.cpp
@@ -0,0 +1,100 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <gtest/gtest.h>
+
+#include <sys/types.h>
+#include <sys/xattr.h>
+
+#include "TemporaryFile.h"
+
+TEST(sys_xattr, setxattr) {
+  TemporaryFile tf;
+  char buf[10];
+  ASSERT_EQ(0, setxattr(tf.filename, "user.foo", "bar", 4, 0));
+  ASSERT_EQ(4, getxattr(tf.filename, "user.foo", buf, sizeof(buf)));
+  ASSERT_STREQ("bar", buf);
+  buf[0] = '\0';
+  ASSERT_EQ(4, lgetxattr(tf.filename, "user.foo", buf, sizeof(buf)));
+  ASSERT_STREQ("bar", buf);
+}
+
+TEST(sys_xattr, fsetxattr) {
+  TemporaryFile tf;
+  char buf[10];
+  ASSERT_EQ(0, fsetxattr(tf.fd, "user.foo", "bar", 4, 0));
+  ASSERT_EQ(4, fgetxattr(tf.fd, "user.foo", buf, sizeof(buf)));
+  ASSERT_STREQ("bar", buf);
+}
+
+TEST(sys_xattr, fsetxattr_zerobuf) {
+  TemporaryFile tf;
+  char buf[10];
+  ASSERT_EQ(0, fsetxattr(tf.fd, "user.foo", "", 0, 0));
+  ASSERT_EQ(0, fgetxattr(tf.fd, "user.foo", buf, sizeof(buf)));
+}
+
+TEST(sys_xattr, fsetxattr_toosmallbuf) {
+  TemporaryFile tf;
+  char buf[10];
+  ASSERT_EQ(0, fsetxattr(tf.fd, "user.foo", "01234567890123456789", 21, 0));
+  ASSERT_EQ(-1, fgetxattr(tf.fd, "user.foo", buf, sizeof(buf)));
+  ASSERT_EQ(ERANGE, errno);
+}
+
+TEST(sys_xattr, fsetxattr_invalidfd) {
+  char buf[10];
+  errno = 0;
+  ASSERT_EQ(-1, fsetxattr(65535, "user.foo", "0123", 5, 0));
+  ASSERT_EQ(EBADF, errno);
+  errno = 0;
+  ASSERT_EQ(-1, fgetxattr(65535, "user.foo", buf, sizeof(buf)));
+  ASSERT_EQ(EBADF, errno);
+}
+
+TEST(sys_xattr, fsetxattr_with_opath) {
+  TemporaryFile tf;
+  int fd = open(tf.filename, O_PATH);
+  ASSERT_NE(-1, fd);
+
+  int res = fsetxattr(fd, "user.foo", "bar", 4, 0);
+#if defined(__BIONIC__)
+  char buf[10];
+  ASSERT_EQ(0, res);
+  ASSERT_EQ(4, fgetxattr(fd, "user.foo", buf, sizeof(buf)));
+  ASSERT_STREQ("bar", buf);
+#else
+  ASSERT_EQ(-1, res);
+  ASSERT_EQ(EBADF, errno);
+#endif
+}
+
+TEST(sys_xattr, fsetxattr_with_opath_toosmall) {
+  TemporaryFile tf;
+  int fd = open(tf.filename, O_PATH);
+  ASSERT_NE(-1, fd);
+
+  int res = fsetxattr(fd, "user.foo", "01234567890123456789", 21, 0);
+#if defined(__BIONIC__)
+  char buf[10];
+  ASSERT_EQ(0, res);
+  ASSERT_EQ(-1, fgetxattr(fd, "user.foo", buf, sizeof(buf)));
+  ASSERT_EQ(ERANGE, errno);
+#else
+  ASSERT_EQ(-1, res);
+  ASSERT_EQ(EBADF, errno);
+#endif
+}
diff --git a/tests/unistd_test.cpp b/tests/unistd_test.cpp
index a21578a..79c16d7 100644
--- a/tests/unistd_test.cpp
+++ b/tests/unistd_test.cpp
@@ -179,10 +179,8 @@
 TEST(unistd, ftruncate_negative) {
   TemporaryFile tf;
   errno = 0;
-  int rc = ftruncate(tf.fd, -123);
-  int err = errno;
-  ASSERT_EQ(-1, rc);
-  ASSERT_EQ(EINVAL, err);
+  ASSERT_EQ(-1, ftruncate(tf.fd, -123));
+  ASSERT_EQ(EINVAL, errno);
 }
 
 static bool g_pause_test_flag = false;