Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #pragma once |
| 30 | |
| 31 | #include <android/fdsan.h> |
| 32 | |
| 33 | #include <errno.h> |
| 34 | #include <stdatomic.h> |
| 35 | #include <string.h> |
| 36 | #include <sys/cdefs.h> |
| 37 | #include <sys/mman.h> |
| 38 | #include <sys/resource.h> |
| 39 | #include <sys/user.h> |
| 40 | |
| 41 | #include <async_safe/log.h> |
| 42 | |
| 43 | struct FdEntry { |
| 44 | _Atomic(uint64_t) close_tag; |
| 45 | }; |
| 46 | |
| 47 | struct FdTableOverflow { |
| 48 | size_t len; |
| 49 | FdEntry entries[0]; |
| 50 | }; |
| 51 | |
| 52 | template <size_t inline_fds> |
| 53 | struct FdTable { |
| 54 | _Atomic(android_fdsan_error_level) error_level; |
| 55 | |
| 56 | FdEntry entries[inline_fds]; |
| 57 | _Atomic(FdTableOverflow*) overflow; |
| 58 | |
| 59 | FdEntry* at(size_t idx) { |
| 60 | if (idx < inline_fds) { |
| 61 | return &entries[idx]; |
| 62 | } |
| 63 | |
| 64 | // Try to create the overflow table ourselves. |
| 65 | FdTableOverflow* local_overflow = atomic_load(&overflow); |
| 66 | if (__predict_false(!local_overflow)) { |
| 67 | struct rlimit rlim = { .rlim_max = 32768 }; |
| 68 | getrlimit(RLIMIT_NOFILE, &rlim); |
| 69 | rlim_t max = rlim.rlim_max; |
| 70 | |
| 71 | if (max == RLIM_INFINITY) { |
| 72 | // This isn't actually possible (the kernel has a hard limit), but just |
| 73 | // in case... |
| 74 | max = 32768; |
| 75 | } |
| 76 | |
| 77 | if (idx > max) { |
| 78 | // This can happen if an fd is created and then the rlimit is lowered. |
| 79 | // In this case, just return nullptr and ignore the fd. |
| 80 | return nullptr; |
| 81 | } |
| 82 | |
| 83 | size_t required_count = max - inline_fds; |
| 84 | size_t required_size = sizeof(FdTableOverflow) + required_count * sizeof(FdEntry); |
| 85 | size_t aligned_size = __BIONIC_ALIGN(required_size, PAGE_SIZE); |
| 86 | size_t aligned_count = (aligned_size - sizeof(FdTableOverflow)) / sizeof(FdEntry); |
| 87 | |
| 88 | void* allocation = |
| 89 | mmap(nullptr, aligned_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 90 | if (allocation == MAP_FAILED) { |
| 91 | async_safe_fatal("fdsan: mmap failed: %s", strerror(errno)); |
| 92 | } |
| 93 | |
| 94 | FdTableOverflow* new_overflow = reinterpret_cast<FdTableOverflow*>(allocation); |
| 95 | new_overflow->len = aligned_count; |
| 96 | |
| 97 | if (atomic_compare_exchange_strong(&overflow, &local_overflow, new_overflow)) { |
| 98 | local_overflow = new_overflow; |
| 99 | } else { |
| 100 | // Someone beat us to it. Deallocate and use theirs. |
| 101 | munmap(allocation, aligned_size); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | size_t offset = idx - inline_fds; |
| 106 | if (local_overflow->len < offset) { |
| 107 | return nullptr; |
| 108 | } |
| 109 | return &local_overflow->entries[offset]; |
| 110 | } |
| 111 | }; |