diff --git a/mozglue/linker/ElfLoader.cpp b/mozglue/linker/ElfLoader.cpp index ec78122d3c80..23fbe3a897ba 100644 --- a/mozglue/linker/ElfLoader.cpp +++ b/mozglue/linker/ElfLoader.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -18,6 +19,7 @@ #include "Logging.h" #include "Utils.h" #include +#include "mozilla/ScopeExit.h" // From Utils.h mozilla::Atomic gPageSize; @@ -152,8 +154,8 @@ class DlIteratePhdrHelper { DlIteratePhdrHelper() { int pipefd[2]; valid_pipe = (pipe(pipefd) == 0); - read_fd.reset(pipefd[0]); - write_fd.reset(pipefd[1]); + read_fd.emplace(pipefd[0]); + write_fd.emplace(pipefd[1]); } int fill_and_call(dl_phdr_cb callback, const void* l_addr, const char* l_name, @@ -161,8 +163,8 @@ class DlIteratePhdrHelper { private: bool valid_pipe; - AutoCloseFD read_fd; - AutoCloseFD write_fd; + std::optional read_fd; + std::optional write_fd; }; // This function is called for each shared library iterated over by @@ -199,7 +201,7 @@ int DlIteratePhdrHelper::fill_and_call(dl_phdr_cb callback, const void* l_addr, static_assert(sizeof(raw_ehdr) < PIPE_BUF, "PIPE_BUF is too small"); do { // writes are atomic when smaller than PIPE_BUF, per POSIX.1-2008. - ret = write(write_fd, l_addr, sizeof(raw_ehdr)); + ret = write(*write_fd, l_addr, sizeof(raw_ehdr)); } while (ret == -1 && errno == EINTR); if (ret != sizeof(raw_ehdr)) { if (ret == -1 && errno == EFAULT) { @@ -212,7 +214,7 @@ int DlIteratePhdrHelper::fill_and_call(dl_phdr_cb callback, const void* l_addr, do { // Per POSIX.1-2008, interrupted reads can return a length smaller // than the given one instead of failing with errno EINTR. - ret = read(read_fd, raw_ehdr + nbytes, sizeof(raw_ehdr) - nbytes); + ret = read(*read_fd, raw_ehdr + nbytes, sizeof(raw_ehdr) - nbytes); if (ret > 0) nbytes += ret; } while ((nbytes != sizeof(raw_ehdr) && ret > 0) || (ret == -1 && errno == EINTR)); @@ -834,7 +836,10 @@ class EnsureWritable { /* The interesting part of the /proc/self/maps format looks like: * startAddr-endAddr rwxp */ int result = 0; - AutoCloseFILE f(fopen("/proc/self/maps", "r")); + FILE* const f = fopen("/proc/self/maps", "r"); + const auto cleanup = mozilla::MakeScopeExit([&]() { + if (f) fclose(f); + }); while (f) { unsigned long long startAddr, endAddr; char perms[5]; diff --git a/mozglue/linker/Mappable.cpp b/mozglue/linker/Mappable.cpp index e701928a2f98..55a7aadfcc0e 100644 --- a/mozglue/linker/Mappable.cpp +++ b/mozglue/linker/Mappable.cpp @@ -15,20 +15,20 @@ Mappable* Mappable::Create(const char* path) { MemoryRange Mappable::mmap(const void* addr, size_t length, int prot, int flags, off_t offset) { - MOZ_ASSERT(fd != -1); + MOZ_ASSERT(fd && *fd != -1); MOZ_ASSERT(!(flags & MAP_SHARED)); flags |= MAP_PRIVATE; - return MemoryRange::mmap(const_cast(addr), length, prot, flags, fd, + return MemoryRange::mmap(const_cast(addr), length, prot, flags, *fd, offset); } void Mappable::finalize() { /* Close file ; equivalent to close(fd.forget()) */ - fd = -1; + fd.emplace(-1); } size_t Mappable::GetLength() const { struct stat st; - return fstat(fd, &st) ? 0 : st.st_size; + return fstat(*fd, &st) ? 0 : st.st_size; } diff --git a/mozglue/linker/Mappable.h b/mozglue/linker/Mappable.h index d03c3334bbc4..3e25fc88c99b 100644 --- a/mozglue/linker/Mappable.h +++ b/mozglue/linker/Mappable.h @@ -5,6 +5,7 @@ #ifndef Mappable_h #define Mappable_h +#include #include "mozilla/RefCounted.h" #include "Utils.h" @@ -48,7 +49,7 @@ class Mappable : public mozilla::RefCounted { private: /* File descriptor */ - AutoCloseFD fd; + std::optional fd; }; #endif /* Mappable_h */ diff --git a/mozglue/linker/Utils.h b/mozglue/linker/Utils.h index d3827f1f4145..4aea454d6e65 100644 --- a/mozglue/linker/Utils.h +++ b/mozglue/linker/Utils.h @@ -12,7 +12,6 @@ #include #include "mozilla/Assertions.h" #include "mozilla/Atomics.h" -#include "mozilla/Scoped.h" /** * On architectures that are little endian and that support unaligned reads, @@ -75,29 +74,15 @@ typedef le_to_cpu le_uint16; typedef le_to_cpu le_uint32; #endif -/** - * AutoCloseFD is a RAII wrapper for POSIX file descriptors - */ -struct AutoCloseFDTraits { - typedef int type; - static int empty() { return -1; } - static void release(int fd) { +struct AutoCloseFD { + const int fd; + + MOZ_IMPLICIT AutoCloseFD(int fd) : fd(fd) {} + ~AutoCloseFD() { if (fd != -1) close(fd); } + operator int() const { return fd; } }; -typedef mozilla::Scoped AutoCloseFD; - -/** - * AutoCloseFILE is a RAII wrapper for POSIX streams - */ -struct AutoCloseFILETraits { - typedef FILE* type; - static FILE* empty() { return nullptr; } - static void release(FILE* f) { - if (f) fclose(f); - } -}; -typedef mozilla::Scoped AutoCloseFILE; extern mozilla::Atomic gPageSize;