Josh Gao | 9727192 | 2019-11-06 13:15:00 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 <sys/cdefs.h> |
| 32 | #include <stdatomic.h> |
| 33 | |
Josh Gao | 7596250 | 2020-01-28 13:24:33 -0800 | [diff] [blame^] | 34 | #include "platform/bionic/fdtrack.h" |
Josh Gao | 9727192 | 2019-11-06 13:15:00 -0800 | [diff] [blame] | 35 | |
| 36 | #include "bionic/pthread_internal.h" |
| 37 | #include "private/bionic_tls.h" |
| 38 | #include "private/ErrnoRestorer.h" |
| 39 | |
| 40 | extern "C" _Atomic(android_fdtrack_hook_t) __android_fdtrack_hook; |
| 41 | |
| 42 | // Macro to record file descriptor creation. |
| 43 | // e.g.: |
| 44 | // int socket(int domain, int type, int protocol) { |
| 45 | // return FDTRACK_CREATE_NAME("socket", __socket(domain, type, protocol)); |
| 46 | // } |
| 47 | #define FDTRACK_CREATE_NAME(name, fd_value) \ |
| 48 | ({ \ |
| 49 | int __fd = (fd_value); \ |
| 50 | if (__fd != -1 && __predict_false(__android_fdtrack_hook)) { \ |
| 51 | bionic_tls& tls = __get_bionic_tls(); \ |
| 52 | /* fdtrack_disabled is only true during reentrant calls. */ \ |
| 53 | if (!__predict_false(tls.fdtrack_disabled)) { \ |
| 54 | ErrnoRestorer r; \ |
| 55 | tls.fdtrack_disabled = true; \ |
| 56 | android_fdtrack_event event; \ |
| 57 | event.fd = __fd; \ |
| 58 | event.type = ANDROID_FDTRACK_EVENT_TYPE_CREATE; \ |
| 59 | event.data.create.function_name = name; \ |
| 60 | __android_fdtrack_hook(&event); \ |
| 61 | tls.fdtrack_disabled = false; \ |
| 62 | } \ |
| 63 | } \ |
| 64 | __fd; \ |
| 65 | }) |
| 66 | |
| 67 | // Macro to record file descriptor creation, with the current function's name. |
| 68 | // e.g.: |
| 69 | // int socket(int domain, int type, int protocol) { |
| 70 | // return FDTRACK_CREATE_NAME(__socket(domain, type, protocol)); |
| 71 | // } |
| 72 | #define FDTRACK_CREATE(fd_value) FDTRACK_CREATE_NAME(__func__, (fd_value)) |
| 73 | |
| 74 | // Macro to record file descriptor closure. |
| 75 | // Note that this does not actually close the file descriptor. |
| 76 | #define FDTRACK_CLOSE(fd_value) \ |
| 77 | ({ \ |
| 78 | int __fd = (fd_value); \ |
| 79 | if (__fd != -1 && __predict_false(__android_fdtrack_hook)) { \ |
| 80 | bionic_tls& tls = __get_bionic_tls(); \ |
| 81 | if (!__predict_false(tls.fdtrack_disabled)) { \ |
| 82 | int saved_errno = errno; \ |
| 83 | tls.fdtrack_disabled = true; \ |
| 84 | android_fdtrack_event event; \ |
| 85 | event.fd = __fd; \ |
| 86 | event.type = ANDROID_FDTRACK_EVENT_TYPE_CLOSE; \ |
| 87 | __android_fdtrack_hook(&event); \ |
| 88 | tls.fdtrack_disabled = false; \ |
| 89 | errno = saved_errno; \ |
| 90 | } \ |
| 91 | } \ |
| 92 | __fd; \ |
| 93 | }) |