Home | History | Annotate | Download | only in linker
      1 /*
      2  * Copyright (C) 2017 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/api-level.h>
     32 
     33 #include <stdlib.h>
     34 #include <limits.h>
     35 
     36 #include <memory>
     37 #include <string>
     38 #include <vector>
     39 #include <unordered_map>
     40 
     41 #include <android-base/macros.h>
     42 
     43 class NamespaceLinkConfig {
     44  public:
     45   NamespaceLinkConfig() = default;
     46   NamespaceLinkConfig(const std::string& ns_name, const std::string& shared_libs,
     47                       bool allow_all_shared_libs)
     48       : ns_name_(ns_name), shared_libs_(shared_libs),
     49         allow_all_shared_libs_(allow_all_shared_libs) {}
     50 
     51   const std::string& ns_name() const {
     52     return ns_name_;
     53   }
     54 
     55   const std::string& shared_libs() const {
     56     return shared_libs_;
     57   }
     58 
     59   bool allow_all_shared_libs() const {
     60     return allow_all_shared_libs_;
     61   }
     62 
     63  private:
     64   std::string ns_name_;
     65   std::string shared_libs_;
     66   bool allow_all_shared_libs_;
     67 };
     68 
     69 class NamespaceConfig {
     70  public:
     71   explicit NamespaceConfig(const std::string& name)
     72       : name_(name), isolated_(false), visible_(false)
     73   {}
     74 
     75   const char* name() const {
     76     return name_.c_str();
     77   }
     78 
     79   bool isolated() const {
     80     return isolated_;
     81   }
     82 
     83   bool visible() const {
     84     return visible_;
     85   }
     86 
     87   const std::vector<std::string>& search_paths() const {
     88     return search_paths_;
     89   }
     90 
     91   const std::vector<std::string>& permitted_paths() const {
     92     return permitted_paths_;
     93   }
     94 
     95   const std::vector<std::string>& whitelisted_libs() const {
     96     return whitelisted_libs_;
     97   }
     98 
     99   const std::vector<NamespaceLinkConfig>& links() const {
    100     return namespace_links_;
    101   }
    102 
    103   void add_namespace_link(const std::string& ns_name, const std::string& shared_libs,
    104                           bool allow_all_shared_libs) {
    105     namespace_links_.push_back(NamespaceLinkConfig(ns_name, shared_libs, allow_all_shared_libs));
    106   }
    107 
    108   void set_isolated(bool isolated) {
    109     isolated_ = isolated;
    110   }
    111 
    112   void set_visible(bool visible) {
    113     visible_ = visible;
    114   }
    115 
    116   void set_search_paths(std::vector<std::string>&& search_paths) {
    117     search_paths_ = std::move(search_paths);
    118   }
    119 
    120   void set_permitted_paths(std::vector<std::string>&& permitted_paths) {
    121     permitted_paths_ = std::move(permitted_paths);
    122   }
    123 
    124   void set_whitelisted_libs(std::vector<std::string>&& whitelisted_libs) {
    125     whitelisted_libs_ = std::move(whitelisted_libs);
    126   }
    127  private:
    128   const std::string name_;
    129   bool isolated_;
    130   bool visible_;
    131   std::vector<std::string> search_paths_;
    132   std::vector<std::string> permitted_paths_;
    133   std::vector<std::string> whitelisted_libs_;
    134   std::vector<NamespaceLinkConfig> namespace_links_;
    135 
    136   DISALLOW_IMPLICIT_CONSTRUCTORS(NamespaceConfig);
    137 };
    138 
    139 class Config {
    140  public:
    141   Config() : target_sdk_version_(__ANDROID_API__) {}
    142 
    143   const std::vector<std::unique_ptr<NamespaceConfig>>& namespace_configs() const {
    144     return namespace_configs_;
    145   }
    146 
    147   const NamespaceConfig* default_namespace_config() const {
    148     auto it = namespace_configs_map_.find("default");
    149     return it == namespace_configs_map_.end() ? nullptr : it->second;
    150   }
    151 
    152   int target_sdk_version() const {
    153     return target_sdk_version_;
    154   }
    155 
    156   // note that this is one time event and therefore there is no need to
    157   // read every section of the config. Every linker instance needs at
    158   // most one configuration.
    159   // Returns false in case of an error. If binary config was not found
    160   // sets *config = nullptr.
    161   static bool read_binary_config(const char* ld_config_file_path,
    162                                  const char* binary_realpath,
    163                                  bool is_asan,
    164                                  const Config** config,
    165                                  std::string* error_msg);
    166 
    167   static std::string get_vndk_version_string(const char delimiter);
    168  private:
    169   void clear();
    170 
    171   void set_target_sdk_version(int target_sdk_version) {
    172     target_sdk_version_ = target_sdk_version;
    173   }
    174 
    175   NamespaceConfig* create_namespace_config(const std::string& name);
    176 
    177   std::vector<std::unique_ptr<NamespaceConfig>> namespace_configs_;
    178   std::unordered_map<std::string, NamespaceConfig*> namespace_configs_map_;
    179   int target_sdk_version_;
    180 
    181   DISALLOW_COPY_AND_ASSIGN(Config);
    182 };
    183