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