| Dimitry Ivanov | b996d60 | 2016-07-11 18:11:39 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2016 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 |  | 
| Elliott Hughes | 9e27e58 | 2017-03-23 17:42:49 -0700 | [diff] [blame] | 29 | #include "linker_logger.h" | 
|  | 30 |  | 
| Dimitry Ivanov | b996d60 | 2016-07-11 18:11:39 -0700 | [diff] [blame] | 31 | #include <string.h> | 
|  | 32 | #include <sys/prctl.h> | 
| Dimitry Ivanov | b996d60 | 2016-07-11 18:11:39 -0700 | [diff] [blame] | 33 | #include <unistd.h> | 
|  | 34 |  | 
|  | 35 | #include <string> | 
|  | 36 | #include <vector> | 
|  | 37 |  | 
| Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 38 | #include <async_safe/log.h> | 
|  | 39 |  | 
| Dimitry Ivanov | b996d60 | 2016-07-11 18:11:39 -0700 | [diff] [blame] | 40 | #include "android-base/strings.h" | 
| Elliott Hughes | 9e27e58 | 2017-03-23 17:42:49 -0700 | [diff] [blame] | 41 | #include "private/CachedProperty.h" | 
| Dimitry Ivanov | b996d60 | 2016-07-11 18:11:39 -0700 | [diff] [blame] | 42 |  | 
|  | 43 | LinkerLogger g_linker_logger; | 
| Elliott Hughes | 9e27e58 | 2017-03-23 17:42:49 -0700 | [diff] [blame] | 44 | bool g_greylist_disabled = false; | 
| Dimitry Ivanov | b996d60 | 2016-07-11 18:11:39 -0700 | [diff] [blame] | 45 |  | 
|  | 46 | static uint32_t ParseProperty(const std::string& value) { | 
|  | 47 | if (value.empty()) { | 
|  | 48 | return 0; | 
|  | 49 | } | 
|  | 50 |  | 
|  | 51 | std::vector<std::string> options = android::base::Split(value, ","); | 
|  | 52 |  | 
|  | 53 | uint32_t flags = 0; | 
|  | 54 |  | 
|  | 55 | for (const auto& o : options) { | 
| Elliott Hughes | 9e27e58 | 2017-03-23 17:42:49 -0700 | [diff] [blame] | 56 | if (o == "dlerror") { | 
| Dimitry Ivanov | b996d60 | 2016-07-11 18:11:39 -0700 | [diff] [blame] | 57 | flags |= kLogErrors; | 
| Elliott Hughes | 9e27e58 | 2017-03-23 17:42:49 -0700 | [diff] [blame] | 58 | } else if (o == "dlopen") { | 
| Dimitry Ivanov | b996d60 | 2016-07-11 18:11:39 -0700 | [diff] [blame] | 59 | flags |= kLogDlopen; | 
| Elliott Hughes | 9e27e58 | 2017-03-23 17:42:49 -0700 | [diff] [blame] | 60 | } else if (o == "dlsym") { | 
| Dimitry Ivanov | cf00d86 | 2016-12-14 14:30:37 -0800 | [diff] [blame] | 61 | flags |= kLogDlsym; | 
| Dimitry Ivanov | b996d60 | 2016-07-11 18:11:39 -0700 | [diff] [blame] | 62 | } else { | 
| Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 63 | async_safe_format_log(ANDROID_LOG_WARN, "linker", "Ignoring unknown debug.ld option \"%s\"", | 
|  | 64 | o.c_str()); | 
| Dimitry Ivanov | b996d60 | 2016-07-11 18:11:39 -0700 | [diff] [blame] | 65 | } | 
|  | 66 | } | 
|  | 67 |  | 
|  | 68 | return flags; | 
|  | 69 | } | 
|  | 70 |  | 
| Elliott Hughes | 9e27e58 | 2017-03-23 17:42:49 -0700 | [diff] [blame] | 71 | static void GetAppSpecificProperty(char* buffer) { | 
| Elliott Hughes | 30a3627 | 2017-02-22 17:31:41 -0800 | [diff] [blame] | 72 | // Get process basename. | 
| Dimitry Ivanov | fbe54c4 | 2017-02-17 14:41:30 -0800 | [diff] [blame] | 73 | const char* process_name_start = basename(g_argv[0]); | 
| Elliott Hughes | 30a3627 | 2017-02-22 17:31:41 -0800 | [diff] [blame] | 74 |  | 
|  | 75 | // Remove ':' and everything after it. This is the naming convention for | 
| Dimitry Ivanov | fbe54c4 | 2017-02-17 14:41:30 -0800 | [diff] [blame] | 76 | // services: https://developer.android.com/guide/components/services.html | 
|  | 77 | const char* process_name_end = strchr(process_name_start, ':'); | 
|  | 78 |  | 
|  | 79 | std::string process_name = (process_name_end != nullptr) ? | 
|  | 80 | std::string(process_name_start, (process_name_end - process_name_start)) : | 
|  | 81 | std::string(process_name_start); | 
| Dimitry Ivanov | b996d60 | 2016-07-11 18:11:39 -0700 | [diff] [blame] | 82 |  | 
| Elliott Hughes | 9e27e58 | 2017-03-23 17:42:49 -0700 | [diff] [blame] | 83 | std::string property_name = std::string("debug.ld.app.") + process_name; | 
|  | 84 | __system_property_get(property_name.c_str(), buffer); | 
|  | 85 | } | 
| Dimitry Ivanov | b996d60 | 2016-07-11 18:11:39 -0700 | [diff] [blame] | 86 |  | 
| Elliott Hughes | 9e27e58 | 2017-03-23 17:42:49 -0700 | [diff] [blame] | 87 | void LinkerLogger::ResetState() { | 
|  | 88 | // The most likely scenario app is not debuggable and | 
|  | 89 | // is running on a user build, in which case logging is disabled. | 
|  | 90 | if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0) { | 
|  | 91 | return; | 
|  | 92 | } | 
|  | 93 |  | 
|  | 94 | // This is a convenient place to check whether the greylist should be disabled for testing. | 
|  | 95 | static CachedProperty greylist_disabled("debug.ld.greylist_disabled"); | 
|  | 96 | bool old_value = g_greylist_disabled; | 
|  | 97 | g_greylist_disabled = (strcmp(greylist_disabled.Get(), "true") == 0); | 
|  | 98 | if (g_greylist_disabled != old_value) { | 
| Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 99 | async_safe_format_log(ANDROID_LOG_INFO, "linker", "%s greylist", | 
|  | 100 | g_greylist_disabled ? "Disabling" : "Enabling"); | 
| Elliott Hughes | 9e27e58 | 2017-03-23 17:42:49 -0700 | [diff] [blame] | 101 | } | 
|  | 102 |  | 
|  | 103 | flags_ = 0; | 
|  | 104 |  | 
|  | 105 | // For logging, check the flag applied to all processes first. | 
|  | 106 | static CachedProperty debug_ld_all("debug.ld.all"); | 
|  | 107 | flags_ |= ParseProperty(debug_ld_all.Get()); | 
|  | 108 |  | 
|  | 109 | // Ignore processes started without argv (http://b/33276926). | 
|  | 110 | if (g_argv[0] == nullptr) { | 
|  | 111 | return; | 
|  | 112 | } | 
|  | 113 |  | 
|  | 114 | // Otherwise check the app-specific property too. | 
|  | 115 | // We can't easily cache the property here because argv[0] changes. | 
|  | 116 | char debug_ld_app[PROP_VALUE_MAX] = {}; | 
|  | 117 | GetAppSpecificProperty(debug_ld_app); | 
|  | 118 | flags_ |= ParseProperty(debug_ld_app); | 
| Dimitry Ivanov | b996d60 | 2016-07-11 18:11:39 -0700 | [diff] [blame] | 119 | } | 
|  | 120 |  | 
|  | 121 | void LinkerLogger::Log(uint32_t type, const char* format, ...) { | 
|  | 122 | if ((flags_ & type) == 0) { | 
|  | 123 | return; | 
|  | 124 | } | 
|  | 125 |  | 
|  | 126 | va_list ap; | 
|  | 127 | va_start(ap, format); | 
| Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 128 | async_safe_format_log_va_list(ANDROID_LOG_DEBUG, "linker", format, ap); | 
| Dimitry Ivanov | b996d60 | 2016-07-11 18:11:39 -0700 | [diff] [blame] | 129 | va_end(ap); | 
|  | 130 | } |