Revert "Revert "base: add support for tagged fd closure to unique_fd.""

Use __attribute__((weak)) to check for fdsan availability at runtime,
to support being used when built with the NDK while targeting older
platform versions.

This reverts commit af798acaea49b8d9150b94eb9f5f11450a6b8893.

Bug: http://b/110100358
Test: m libjnitest
Change-Id: Ie59d041d69cc3d4b6c59412d706f702dd1d2dab2
This commit is contained in:
Josh Gao 2018-07-20 12:42:14 -07:00
parent a570644b7d
commit 22666feacc
3 changed files with 129 additions and 14 deletions

View file

@ -135,6 +135,7 @@ cc_test {
"strings_test.cpp",
"test_main.cpp",
"test_utils_test.cpp",
"unique_fd_test.cpp",
],
target: {
android: {

View file

@ -42,10 +42,35 @@
//
// unique_fd is also known as ScopedFd/ScopedFD/scoped_fd; mentioned here to help
// you find this class if you're searching for one of those names.
#if defined(__BIONIC__)
#include <android/fdsan.h>
#endif
namespace android {
namespace base {
struct DefaultCloser {
#if defined(__BIONIC__)
static void Tag(int fd, void* old_addr, void* new_addr) {
if (android_fdsan_exchange_owner_tag) {
uint64_t old_tag = android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_UNIQUE_FD,
reinterpret_cast<uint64_t>(old_addr));
uint64_t new_tag = android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_UNIQUE_FD,
reinterpret_cast<uint64_t>(new_addr));
android_fdsan_exchange_owner_tag(fd, old_tag, new_tag);
}
}
static void Close(int fd, void* addr) {
if (android_fdsan_close_with_tag) {
uint64_t tag = android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_UNIQUE_FD,
reinterpret_cast<uint64_t>(addr));
android_fdsan_close_with_tag(fd, tag);
} else {
close(fd);
}
}
#else
static void Close(int fd) {
// Even if close(2) fails with EINTR, the fd will have been closed.
// Using TEMP_FAILURE_RETRY will either lead to EBADF or closing someone
@ -53,40 +78,75 @@ struct DefaultCloser {
// http://lkml.indiana.edu/hypermail/linux/kernel/0509.1/0877.html
::close(fd);
}
#endif
};
template <typename Closer>
class unique_fd_impl final {
public:
unique_fd_impl() : value_(-1) {}
unique_fd_impl() {}
explicit unique_fd_impl(int value) : value_(value) {}
explicit unique_fd_impl(int fd) { reset(fd); }
~unique_fd_impl() { reset(); }
unique_fd_impl(unique_fd_impl&& other) : value_(other.release()) {}
unique_fd_impl(unique_fd_impl&& other) { reset(other.release()); }
unique_fd_impl& operator=(unique_fd_impl&& s) {
reset(s.release());
int fd = s.fd_;
s.fd_ = -1;
reset(fd, &s);
return *this;
}
void reset(int new_value = -1) {
if (value_ != -1) {
Closer::Close(value_);
}
value_ = new_value;
}
void reset(int new_value = -1) { reset(new_value, nullptr); }
int get() const { return value_; }
int get() const { return fd_; }
operator int() const { return get(); }
int release() __attribute__((warn_unused_result)) {
int ret = value_;
value_ = -1;
tag(fd_, this, nullptr);
int ret = fd_;
fd_ = -1;
return ret;
}
private:
int value_;
void reset(int new_value, void* previous_tag) {
if (fd_ != -1) {
close(fd_, this);
}
fd_ = new_value;
if (new_value != -1) {
tag(new_value, previous_tag, this);
}
}
int fd_ = -1;
// Template magic to use Closer::Tag if available, and do nothing if not.
// If Closer::Tag exists, this implementation is preferred, because int is a better match.
// If not, this implementation is SFINAEd away, and the no-op below is the only one that exists.
template <typename T = Closer>
static auto tag(int fd, void* old_tag, void* new_tag)
-> decltype(T::Tag(fd, old_tag, new_tag), void()) {
T::Tag(fd, old_tag, new_tag);
}
template <typename T = Closer>
static void tag(long, void*, void*) {
// No-op.
}
// Same as above, to select between Closer::Close(int) and Closer::Close(int, void*).
template <typename T = Closer>
static auto close(int fd, void* tag_value) -> decltype(T::Close(fd, tag_value), void()) {
T::Close(fd, tag_value);
}
template <typename T = Closer>
static auto close(int fd, void*) -> decltype(T::Close(fd), void()) {
T::Close(fd);
}
unique_fd_impl(const unique_fd_impl&);
void operator=(const unique_fd_impl&);

54
base/unique_fd_test.cpp Normal file
View file

@ -0,0 +1,54 @@
/*
* Copyright (C) 2018 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 "android-base/unique_fd.h"
#include <gtest/gtest.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
using android::base::unique_fd;
TEST(unique_fd, unowned_close) {
#if defined(__BIONIC__)
unique_fd fd(open("/dev/null", O_RDONLY));
EXPECT_DEATH(close(fd.get()), "incorrect tag");
#endif
}
TEST(unique_fd, untag_on_release) {
unique_fd fd(open("/dev/null", O_RDONLY));
close(fd.release());
}
TEST(unique_fd, move) {
unique_fd fd(open("/dev/null", O_RDONLY));
unique_fd fd_moved = std::move(fd);
ASSERT_EQ(-1, fd.get());
ASSERT_GT(fd_moved.get(), -1);
}
TEST(unique_fd, unowned_close_after_move) {
#if defined(__BIONIC__)
unique_fd fd(open("/dev/null", O_RDONLY));
unique_fd fd_moved = std::move(fd);
ASSERT_EQ(-1, fd.get());
ASSERT_GT(fd_moved.get(), -1);
EXPECT_DEATH(close(fd_moved.get()), "incorrect tag");
#endif
}