| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #ifndef DEX_BUILDER_H_ |
| 17 | #define DEX_BUILDER_H_ |
| 18 | |
| Eric Holk | d1b4383 | 2019-01-29 08:32:42 -0800 | [diff] [blame] | 19 | #include <array> |
| Eric Holk | d62c5aa | 2018-11-01 15:50:24 -0700 | [diff] [blame] | 20 | #include <forward_list> |
| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 21 | #include <map> |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 22 | #include <optional> |
| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 23 | #include <string> |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 24 | #include <unordered_map> |
| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 25 | #include <vector> |
| 26 | |
| Eric Holk | d62c5aa | 2018-11-01 15:50:24 -0700 | [diff] [blame] | 27 | #include "dex/dex_instruction.h" |
| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 28 | #include "slicer/dex_ir.h" |
| 29 | #include "slicer/writer.h" |
| 30 | |
| 31 | namespace startop { |
| 32 | namespace dex { |
| 33 | |
| 34 | // TODO: remove this once the dex generation code is complete. |
| 35 | void WriteTestDexFile(const std::string& filename); |
| 36 | |
| 37 | ////////////////////////// |
| 38 | // Forward declarations // |
| 39 | ////////////////////////// |
| 40 | class DexBuilder; |
| 41 | |
| 42 | // Our custom allocator for dex::Writer |
| 43 | // |
| 44 | // This keeps track of all allocations and ensures they are freed when |
| 45 | // TrackingAllocator is destroyed. Pointers to memory allocated by this |
| 46 | // allocator must not outlive the allocator. |
| 47 | class TrackingAllocator : public ::dex::Writer::Allocator { |
| 48 | public: |
| 49 | virtual void* Allocate(size_t size); |
| 50 | virtual void Free(void* ptr); |
| 51 | |
| 52 | private: |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 53 | std::unordered_map<void*, std::unique_ptr<uint8_t[]>> allocations_; |
| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | // Represents a DEX type descriptor. |
| 57 | // |
| 58 | // TODO: add a way to create a descriptor for a reference of a class type. |
| 59 | class TypeDescriptor { |
| 60 | public: |
| 61 | // Named constructors for base type descriptors. |
| 62 | static const TypeDescriptor Int(); |
| 63 | static const TypeDescriptor Void(); |
| 64 | |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 65 | // Creates a type descriptor from a fully-qualified class name. For example, it turns the class |
| 66 | // name java.lang.Object into the descriptor Ljava/lang/Object. |
| 67 | static TypeDescriptor FromClassname(const std::string& name); |
| 68 | |
| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 69 | // Return the full descriptor, such as I or Ljava/lang/Object |
| 70 | const std::string& descriptor() const { return descriptor_; } |
| 71 | // Return the shorty descriptor, such as I or L |
| 72 | std::string short_descriptor() const { return descriptor().substr(0, 1); } |
| 73 | |
| Eric Holk | d1b4383 | 2019-01-29 08:32:42 -0800 | [diff] [blame] | 74 | bool is_object() const { return short_descriptor() == "L"; } |
| 75 | |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 76 | bool operator<(const TypeDescriptor& rhs) const { return descriptor_ < rhs.descriptor_; } |
| 77 | |
| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 78 | private: |
| Chih-Hung Hsieh | 81aff0f | 2018-12-20 13:53:28 -0800 | [diff] [blame] | 79 | explicit TypeDescriptor(std::string descriptor) : descriptor_{descriptor} {} |
| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 80 | |
| 81 | const std::string descriptor_; |
| 82 | }; |
| 83 | |
| 84 | // Defines a function signature. For example, Prototype{TypeDescriptor::VOID, TypeDescriptor::Int} |
| 85 | // represents the function type (Int) -> Void. |
| 86 | class Prototype { |
| 87 | public: |
| 88 | template <typename... TypeDescriptors> |
| Chih-Hung Hsieh | 81aff0f | 2018-12-20 13:53:28 -0800 | [diff] [blame] | 89 | explicit Prototype(TypeDescriptor return_type, TypeDescriptors... param_types) |
| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 90 | : return_type_{return_type}, param_types_{param_types...} {} |
| 91 | |
| 92 | // Encode this prototype into the dex file. |
| 93 | ir::Proto* Encode(DexBuilder* dex) const; |
| 94 | |
| 95 | // Get the shorty descriptor, such as VII for (Int, Int) -> Void |
| 96 | std::string Shorty() const; |
| 97 | |
| Eric Holk | d1b4383 | 2019-01-29 08:32:42 -0800 | [diff] [blame] | 98 | const TypeDescriptor& ArgType(size_t index) const; |
| 99 | |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 100 | bool operator<(const Prototype& rhs) const { |
| 101 | return std::make_tuple(return_type_, param_types_) < |
| 102 | std::make_tuple(rhs.return_type_, rhs.param_types_); |
| 103 | } |
| 104 | |
| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 105 | private: |
| 106 | const TypeDescriptor return_type_; |
| 107 | const std::vector<TypeDescriptor> param_types_; |
| 108 | }; |
| 109 | |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 110 | // Represents a DEX register or constant. We separate regular registers and parameters |
| 111 | // because we will not know the real parameter id until after all instructions |
| 112 | // have been generated. |
| 113 | class Value { |
| 114 | public: |
| 115 | static constexpr Value Local(size_t id) { return Value{id, Kind::kLocalRegister}; } |
| 116 | static constexpr Value Parameter(size_t id) { return Value{id, Kind::kParameter}; } |
| 117 | static constexpr Value Immediate(size_t value) { return Value{value, Kind::kImmediate}; } |
| Eric Holk | 3cc4afc | 2018-11-08 14:16:20 -0800 | [diff] [blame] | 118 | static constexpr Value String(size_t value) { return Value{value, Kind::kString}; } |
| Eric Holk | d62c5aa | 2018-11-01 15:50:24 -0700 | [diff] [blame] | 119 | static constexpr Value Label(size_t id) { return Value{id, Kind::kLabel}; } |
| Eric Holk | b392758 | 2018-11-08 16:40:16 -0800 | [diff] [blame] | 120 | static constexpr Value Type(size_t id) { return Value{id, Kind::kType}; } |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 121 | |
| 122 | bool is_register() const { return kind_ == Kind::kLocalRegister; } |
| 123 | bool is_parameter() const { return kind_ == Kind::kParameter; } |
| Eric Holk | d62c5aa | 2018-11-01 15:50:24 -0700 | [diff] [blame] | 124 | bool is_variable() const { return is_register() || is_parameter(); } |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 125 | bool is_immediate() const { return kind_ == Kind::kImmediate; } |
| Eric Holk | 3cc4afc | 2018-11-08 14:16:20 -0800 | [diff] [blame] | 126 | bool is_string() const { return kind_ == Kind::kString; } |
| Eric Holk | d62c5aa | 2018-11-01 15:50:24 -0700 | [diff] [blame] | 127 | bool is_label() const { return kind_ == Kind::kLabel; } |
| Eric Holk | b392758 | 2018-11-08 16:40:16 -0800 | [diff] [blame] | 128 | bool is_type() const { return kind_ == Kind::kType; } |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 129 | |
| 130 | size_t value() const { return value_; } |
| 131 | |
| Eric Holk | d1b4383 | 2019-01-29 08:32:42 -0800 | [diff] [blame] | 132 | constexpr Value() : value_{0}, kind_{Kind::kInvalid} {} |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 133 | |
| Eric Holk | d1b4383 | 2019-01-29 08:32:42 -0800 | [diff] [blame] | 134 | private: |
| 135 | enum class Kind { kInvalid, kLocalRegister, kParameter, kImmediate, kString, kLabel, kType }; |
| 136 | |
| 137 | size_t value_; |
| 138 | Kind kind_; |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 139 | |
| 140 | constexpr Value(size_t value, Kind kind) : value_{value}, kind_{kind} {} |
| 141 | }; |
| 142 | |
| Eric Holk | 5c6a1a5 | 2019-09-17 13:28:34 -0700 | [diff] [blame^] | 143 | // Represents an allocated register returned by MethodBuilder::AllocRegister |
| 144 | class LiveRegister { |
| 145 | friend class MethodBuilder; |
| 146 | |
| 147 | public: |
| 148 | LiveRegister(LiveRegister&& other) : liveness_{other.liveness_}, index_{other.index_} { |
| 149 | other.index_ = {}; |
| 150 | }; |
| 151 | ~LiveRegister() { |
| 152 | if (index_.has_value()) { |
| 153 | (*liveness_)[*index_] = false; |
| 154 | } |
| 155 | }; |
| 156 | |
| 157 | operator const Value() const { return Value::Local(*index_); } |
| 158 | |
| 159 | private: |
| 160 | LiveRegister(std::vector<bool>* liveness, size_t index) : liveness_{liveness}, index_{index} {} |
| 161 | |
| 162 | std::vector<bool>* const liveness_; |
| 163 | std::optional<size_t> index_; |
| 164 | }; |
| 165 | |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 166 | // A virtual instruction. We convert these to real instructions in MethodBuilder::Encode. |
| 167 | // Virtual instructions are needed to keep track of information that is not known until all of the |
| 168 | // code is generated. This information includes things like how many local registers are created and |
| 169 | // branch target locations. |
| 170 | class Instruction { |
| 171 | public: |
| 172 | // The operation performed by this instruction. These are virtual instructions that do not |
| 173 | // correspond exactly to DEX instructions. |
| Eric Holk | b392758 | 2018-11-08 16:40:16 -0800 | [diff] [blame] | 174 | enum class Op { |
| Eric Holk | b392758 | 2018-11-08 16:40:16 -0800 | [diff] [blame] | 175 | kBindLabel, |
| 176 | kBranchEqz, |
| Eric Holk | c69449d | 2018-12-13 11:35:58 -0800 | [diff] [blame] | 177 | kBranchNEqz, |
| Eric Holk | 44d8cdf | 2018-12-17 13:35:34 -0800 | [diff] [blame] | 178 | kCheckCast, |
| Eric Holk | f3b9589 | 2019-07-30 14:47:06 -0700 | [diff] [blame] | 179 | kGetInstanceField, |
| Eric Holk | 3092f99 | 2019-07-25 15:14:01 -0700 | [diff] [blame] | 180 | kGetStaticField, |
| Eric Holk | 44d8cdf | 2018-12-17 13:35:34 -0800 | [diff] [blame] | 181 | kInvokeDirect, |
| 182 | kInvokeInterface, |
| 183 | kInvokeStatic, |
| 184 | kInvokeVirtual, |
| 185 | kMove, |
| Eric Holk | d1b4383 | 2019-01-29 08:32:42 -0800 | [diff] [blame] | 186 | kMoveObject, |
| Eric Holk | 44d8cdf | 2018-12-17 13:35:34 -0800 | [diff] [blame] | 187 | kNew, |
| 188 | kReturn, |
| 189 | kReturnObject, |
| Eric Holk | f3b9589 | 2019-07-30 14:47:06 -0700 | [diff] [blame] | 190 | kSetInstanceField, |
| Eric Holk | 70445d0 | 2019-07-26 09:37:46 -0700 | [diff] [blame] | 191 | kSetStaticField |
| Eric Holk | b392758 | 2018-11-08 16:40:16 -0800 | [diff] [blame] | 192 | }; |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 193 | |
| 194 | //////////////////////// |
| 195 | // Named Constructors // |
| 196 | //////////////////////// |
| 197 | |
| 198 | // For instructions with no return value and no arguments. |
| 199 | static inline Instruction OpNoArgs(Op opcode) { |
| Eric Holk | 3092f99 | 2019-07-25 15:14:01 -0700 | [diff] [blame] | 200 | return Instruction{opcode, /*index_argument*/ 0, /*dest*/ {}}; |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 201 | } |
| 202 | // For most instructions, which take some number of arguments and have an optional return value. |
| 203 | template <typename... T> |
| Eric Holk | 5c6a1a5 | 2019-09-17 13:28:34 -0700 | [diff] [blame^] | 204 | static inline Instruction OpWithArgs(Op opcode, std::optional<const Value> dest, |
| 205 | const T&... args) { |
| Eric Holk | 3092f99 | 2019-07-25 15:14:01 -0700 | [diff] [blame] | 206 | return Instruction{opcode, /*index_argument=*/0, /*result_is_object=*/false, dest, args...}; |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 207 | } |
| Eric Holk | 44d8cdf | 2018-12-17 13:35:34 -0800 | [diff] [blame] | 208 | |
| 209 | // A cast instruction. Basically, `(type)val` |
| 210 | static inline Instruction Cast(Value val, Value type) { |
| Eric Holk | d1b4383 | 2019-01-29 08:32:42 -0800 | [diff] [blame] | 211 | CHECK(type.is_type()); |
| Eric Holk | 44d8cdf | 2018-12-17 13:35:34 -0800 | [diff] [blame] | 212 | return OpWithArgs(Op::kCheckCast, val, type); |
| 213 | } |
| 214 | |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 215 | // For method calls. |
| 216 | template <typename... T> |
| Eric Holk | 3092f99 | 2019-07-25 15:14:01 -0700 | [diff] [blame] | 217 | static inline Instruction InvokeVirtual(size_t index_argument, std::optional<const Value> dest, |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 218 | Value this_arg, T... args) { |
| Eric Holk | c69449d | 2018-12-13 11:35:58 -0800 | [diff] [blame] | 219 | return Instruction{ |
| Eric Holk | 3092f99 | 2019-07-25 15:14:01 -0700 | [diff] [blame] | 220 | Op::kInvokeVirtual, index_argument, /*result_is_object=*/false, dest, this_arg, args...}; |
| Eric Holk | c69449d | 2018-12-13 11:35:58 -0800 | [diff] [blame] | 221 | } |
| 222 | // Returns an object |
| 223 | template <typename... T> |
| Eric Holk | f3b9589 | 2019-07-30 14:47:06 -0700 | [diff] [blame] | 224 | static inline Instruction InvokeVirtualObject(size_t index_argument, |
| 225 | std::optional<const Value> dest, Value this_arg, |
| Eric Holk | 5c6a1a5 | 2019-09-17 13:28:34 -0700 | [diff] [blame^] | 226 | const T&... args) { |
| Eric Holk | c69449d | 2018-12-13 11:35:58 -0800 | [diff] [blame] | 227 | return Instruction{ |
| Eric Holk | 3092f99 | 2019-07-25 15:14:01 -0700 | [diff] [blame] | 228 | Op::kInvokeVirtual, index_argument, /*result_is_object=*/true, dest, this_arg, args...}; |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 229 | } |
| Eric Holk | b392758 | 2018-11-08 16:40:16 -0800 | [diff] [blame] | 230 | // For direct calls (basically, constructors). |
| 231 | template <typename... T> |
| Eric Holk | 3092f99 | 2019-07-25 15:14:01 -0700 | [diff] [blame] | 232 | static inline Instruction InvokeDirect(size_t index_argument, std::optional<const Value> dest, |
| Eric Holk | 5c6a1a5 | 2019-09-17 13:28:34 -0700 | [diff] [blame^] | 233 | Value this_arg, const T&... args) { |
| Eric Holk | c69449d | 2018-12-13 11:35:58 -0800 | [diff] [blame] | 234 | return Instruction{ |
| Eric Holk | 3092f99 | 2019-07-25 15:14:01 -0700 | [diff] [blame] | 235 | Op::kInvokeDirect, index_argument, /*result_is_object=*/false, dest, this_arg, args...}; |
| Eric Holk | c69449d | 2018-12-13 11:35:58 -0800 | [diff] [blame] | 236 | } |
| 237 | // Returns an object |
| 238 | template <typename... T> |
| Eric Holk | f3b9589 | 2019-07-30 14:47:06 -0700 | [diff] [blame] | 239 | static inline Instruction InvokeDirectObject(size_t index_argument, |
| 240 | std::optional<const Value> dest, Value this_arg, |
| 241 | T... args) { |
| Eric Holk | c69449d | 2018-12-13 11:35:58 -0800 | [diff] [blame] | 242 | return Instruction{ |
| Eric Holk | 3092f99 | 2019-07-25 15:14:01 -0700 | [diff] [blame] | 243 | Op::kInvokeDirect, index_argument, /*result_is_object=*/true, dest, this_arg, args...}; |
| Eric Holk | c69449d | 2018-12-13 11:35:58 -0800 | [diff] [blame] | 244 | } |
| 245 | // For static calls. |
| 246 | template <typename... T> |
| Eric Holk | 3092f99 | 2019-07-25 15:14:01 -0700 | [diff] [blame] | 247 | static inline Instruction InvokeStatic(size_t index_argument, std::optional<const Value> dest, |
| Eric Holk | c69449d | 2018-12-13 11:35:58 -0800 | [diff] [blame] | 248 | T... args) { |
| Eric Holk | f3b9589 | 2019-07-30 14:47:06 -0700 | [diff] [blame] | 249 | return Instruction{ |
| 250 | Op::kInvokeStatic, index_argument, /*result_is_object=*/false, dest, args...}; |
| Eric Holk | c69449d | 2018-12-13 11:35:58 -0800 | [diff] [blame] | 251 | } |
| 252 | // Returns an object |
| 253 | template <typename... T> |
| Eric Holk | f3b9589 | 2019-07-30 14:47:06 -0700 | [diff] [blame] | 254 | static inline Instruction InvokeStaticObject(size_t index_argument, |
| 255 | std::optional<const Value> dest, T... args) { |
| Eric Holk | 3092f99 | 2019-07-25 15:14:01 -0700 | [diff] [blame] | 256 | return Instruction{Op::kInvokeStatic, index_argument, /*result_is_object=*/true, dest, args...}; |
| Eric Holk | c69449d | 2018-12-13 11:35:58 -0800 | [diff] [blame] | 257 | } |
| 258 | // For static calls. |
| 259 | template <typename... T> |
| Eric Holk | 3092f99 | 2019-07-25 15:14:01 -0700 | [diff] [blame] | 260 | static inline Instruction InvokeInterface(size_t index_argument, std::optional<const Value> dest, |
| Eric Holk | 5c6a1a5 | 2019-09-17 13:28:34 -0700 | [diff] [blame^] | 261 | const T&... args) { |
| Eric Holk | f3b9589 | 2019-07-30 14:47:06 -0700 | [diff] [blame] | 262 | return Instruction{ |
| 263 | Op::kInvokeInterface, index_argument, /*result_is_object=*/false, dest, args...}; |
| Eric Holk | 3092f99 | 2019-07-25 15:14:01 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | static inline Instruction GetStaticField(size_t field_id, Value dest) { |
| 267 | return Instruction{Op::kGetStaticField, field_id, dest}; |
| Eric Holk | b392758 | 2018-11-08 16:40:16 -0800 | [diff] [blame] | 268 | } |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 269 | |
| Eric Holk | 70445d0 | 2019-07-26 09:37:46 -0700 | [diff] [blame] | 270 | static inline Instruction SetStaticField(size_t field_id, Value value) { |
| Eric Holk | f3b9589 | 2019-07-30 14:47:06 -0700 | [diff] [blame] | 271 | return Instruction{ |
| 272 | Op::kSetStaticField, field_id, /*result_is_object=*/false, /*dest=*/{}, value}; |
| Eric Holk | 70445d0 | 2019-07-26 09:37:46 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| Eric Holk | f3b9589 | 2019-07-30 14:47:06 -0700 | [diff] [blame] | 275 | static inline Instruction GetField(size_t field_id, Value dest, Value object) { |
| 276 | return Instruction{Op::kGetInstanceField, field_id, /*result_is_object=*/false, dest, object}; |
| 277 | } |
| 278 | |
| 279 | static inline Instruction SetField(size_t field_id, Value object, Value value) { |
| 280 | return Instruction{ |
| 281 | Op::kSetInstanceField, field_id, /*result_is_object=*/false, /*dest=*/{}, object, value}; |
| 282 | } |
| Eric Holk | 70445d0 | 2019-07-26 09:37:46 -0700 | [diff] [blame] | 283 | |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 284 | /////////////// |
| 285 | // Accessors // |
| 286 | /////////////// |
| 287 | |
| 288 | Op opcode() const { return opcode_; } |
| Eric Holk | 3092f99 | 2019-07-25 15:14:01 -0700 | [diff] [blame] | 289 | size_t index_argument() const { return index_argument_; } |
| Eric Holk | c69449d | 2018-12-13 11:35:58 -0800 | [diff] [blame] | 290 | bool result_is_object() const { return result_is_object_; } |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 291 | const std::optional<const Value>& dest() const { return dest_; } |
| 292 | const std::vector<const Value>& args() const { return args_; } |
| 293 | |
| 294 | private: |
| Eric Holk | 3092f99 | 2019-07-25 15:14:01 -0700 | [diff] [blame] | 295 | inline Instruction(Op opcode, size_t index_argument, std::optional<const Value> dest) |
| Eric Holk | f3b9589 | 2019-07-30 14:47:06 -0700 | [diff] [blame] | 296 | : opcode_{opcode}, |
| 297 | index_argument_{index_argument}, |
| 298 | result_is_object_{false}, |
| 299 | dest_{dest}, |
| 300 | args_{} {} |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 301 | |
| 302 | template <typename... T> |
| Eric Holk | f3b9589 | 2019-07-30 14:47:06 -0700 | [diff] [blame] | 303 | inline Instruction(Op opcode, size_t index_argument, bool result_is_object, |
| Eric Holk | 5c6a1a5 | 2019-09-17 13:28:34 -0700 | [diff] [blame^] | 304 | std::optional<const Value> dest, const T&... args) |
| Eric Holk | c69449d | 2018-12-13 11:35:58 -0800 | [diff] [blame] | 305 | : opcode_{opcode}, |
| Eric Holk | 3092f99 | 2019-07-25 15:14:01 -0700 | [diff] [blame] | 306 | index_argument_{index_argument}, |
| Eric Holk | c69449d | 2018-12-13 11:35:58 -0800 | [diff] [blame] | 307 | result_is_object_{result_is_object}, |
| 308 | dest_{dest}, |
| 309 | args_{args...} {} |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 310 | |
| 311 | const Op opcode_; |
| 312 | // The index of the method to invoke, for kInvokeVirtual and similar opcodes. |
| Eric Holk | 3092f99 | 2019-07-25 15:14:01 -0700 | [diff] [blame] | 313 | const size_t index_argument_{0}; |
| Eric Holk | c69449d | 2018-12-13 11:35:58 -0800 | [diff] [blame] | 314 | const bool result_is_object_; |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 315 | const std::optional<const Value> dest_; |
| 316 | const std::vector<const Value> args_; |
| 317 | }; |
| 318 | |
| 319 | // Needed for CHECK_EQ, DCHECK_EQ, etc. |
| 320 | std::ostream& operator<<(std::ostream& out, const Instruction::Op& opcode); |
| 321 | |
| Eric Holk | b392758 | 2018-11-08 16:40:16 -0800 | [diff] [blame] | 322 | // Keeps track of information needed to manipulate or call a method. |
| 323 | struct MethodDeclData { |
| 324 | size_t id; |
| 325 | ir::MethodDecl* decl; |
| 326 | }; |
| 327 | |
| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 328 | // Tools to help build methods and their bodies. |
| 329 | class MethodBuilder { |
| 330 | public: |
| 331 | MethodBuilder(DexBuilder* dex, ir::Class* class_def, ir::MethodDecl* decl); |
| 332 | |
| 333 | // Encode the method into DEX format. |
| 334 | ir::EncodedMethod* Encode(); |
| 335 | |
| Eric Holk | 5c6a1a5 | 2019-09-17 13:28:34 -0700 | [diff] [blame^] | 336 | // Create a new register to be used to storing values. |
| 337 | LiveRegister AllocRegister(); |
| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 338 | |
| Eric Holk | d62c5aa | 2018-11-01 15:50:24 -0700 | [diff] [blame] | 339 | Value MakeLabel(); |
| 340 | |
| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 341 | ///////////////////////////////// |
| 342 | // Instruction builder methods // |
| 343 | ///////////////////////////////// |
| 344 | |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 345 | void AddInstruction(Instruction instruction); |
| 346 | |
| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 347 | // return-void |
| 348 | void BuildReturn(); |
| Eric Holk | 3cc4afc | 2018-11-08 14:16:20 -0800 | [diff] [blame] | 349 | void BuildReturn(Value src, bool is_object = false); |
| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 350 | // const/4 |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 351 | void BuildConst4(Value target, int value); |
| Eric Holk | 3cc4afc | 2018-11-08 14:16:20 -0800 | [diff] [blame] | 352 | void BuildConstString(Value target, const std::string& value); |
| Eric Holk | b392758 | 2018-11-08 16:40:16 -0800 | [diff] [blame] | 353 | template <typename... T> |
| Eric Holk | 5c6a1a5 | 2019-09-17 13:28:34 -0700 | [diff] [blame^] | 354 | void BuildNew(Value target, TypeDescriptor type, Prototype constructor, const T&... args); |
| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 355 | |
| 356 | // TODO: add builders for more instructions |
| 357 | |
| Eric Holk | c69449d | 2018-12-13 11:35:58 -0800 | [diff] [blame] | 358 | DexBuilder* dex_file() const { return dex_; } |
| 359 | |
| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 360 | private: |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 361 | void EncodeInstructions(); |
| 362 | void EncodeInstruction(const Instruction& instruction); |
| Eric Holk | 3cc4afc | 2018-11-08 14:16:20 -0800 | [diff] [blame] | 363 | |
| 364 | // Encodes a return instruction. For instructions with no return value, the opcode field is |
| 365 | // ignored. Otherwise, this specifies which return instruction will be used (return, |
| 366 | // return-object, etc.) |
| 367 | void EncodeReturn(const Instruction& instruction, ::art::Instruction::Code opcode); |
| 368 | |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 369 | void EncodeMove(const Instruction& instruction); |
| Eric Holk | b392758 | 2018-11-08 16:40:16 -0800 | [diff] [blame] | 370 | void EncodeInvoke(const Instruction& instruction, ::art::Instruction::Code opcode); |
| Eric Holk | d62c5aa | 2018-11-01 15:50:24 -0700 | [diff] [blame] | 371 | void EncodeBranch(art::Instruction::Code op, const Instruction& instruction); |
| Eric Holk | b392758 | 2018-11-08 16:40:16 -0800 | [diff] [blame] | 372 | void EncodeNew(const Instruction& instruction); |
| Eric Holk | 44d8cdf | 2018-12-17 13:35:34 -0800 | [diff] [blame] | 373 | void EncodeCast(const Instruction& instruction); |
| Eric Holk | f3b9589 | 2019-07-30 14:47:06 -0700 | [diff] [blame] | 374 | void EncodeFieldOp(const Instruction& instruction); |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 375 | |
| Eric Holk | 1c0f3f0 | 2018-11-09 13:48:59 -0800 | [diff] [blame] | 376 | // Low-level instruction format encoding. See |
| 377 | // https://source.android.com/devices/tech/dalvik/instruction-formats for documentation of |
| 378 | // formats. |
| 379 | |
| 380 | inline void Encode10x(art::Instruction::Code opcode) { |
| 381 | // 00|op |
| 382 | buffer_.push_back(opcode); |
| 383 | } |
| 384 | |
| 385 | inline void Encode11x(art::Instruction::Code opcode, uint8_t a) { |
| 386 | // aa|op |
| 387 | buffer_.push_back((a << 8) | opcode); |
| 388 | } |
| 389 | |
| 390 | inline void Encode11n(art::Instruction::Code opcode, uint8_t a, int8_t b) { |
| 391 | // b|a|op |
| 392 | |
| 393 | // Make sure the fields are in bounds (4 bits for a, 4 bits for b). |
| 394 | CHECK_LT(a, 16); |
| 395 | CHECK_LE(-8, b); |
| 396 | CHECK_LT(b, 8); |
| 397 | |
| 398 | buffer_.push_back(((b & 0xf) << 12) | (a << 8) | opcode); |
| 399 | } |
| 400 | |
| 401 | inline void Encode21c(art::Instruction::Code opcode, uint8_t a, uint16_t b) { |
| 402 | // aa|op|bbbb |
| 403 | buffer_.push_back((a << 8) | opcode); |
| 404 | buffer_.push_back(b); |
| 405 | } |
| 406 | |
| Eric Holk | f3b9589 | 2019-07-30 14:47:06 -0700 | [diff] [blame] | 407 | inline void Encode22c(art::Instruction::Code opcode, uint8_t a, uint8_t b, uint16_t c) { |
| 408 | // b|a|op|bbbb |
| 409 | CHECK(IsShortRegister(a)); |
| 410 | CHECK(IsShortRegister(b)); |
| 411 | buffer_.push_back((b << 12) | (a << 8) | opcode); |
| 412 | buffer_.push_back(c); |
| 413 | } |
| 414 | |
| Eric Holk | d1b4383 | 2019-01-29 08:32:42 -0800 | [diff] [blame] | 415 | inline void Encode32x(art::Instruction::Code opcode, uint16_t a, uint16_t b) { |
| 416 | buffer_.push_back(opcode); |
| 417 | buffer_.push_back(a); |
| 418 | buffer_.push_back(b); |
| 419 | } |
| 420 | |
| Eric Holk | 1c0f3f0 | 2018-11-09 13:48:59 -0800 | [diff] [blame] | 421 | inline void Encode35c(art::Instruction::Code opcode, size_t a, uint16_t b, uint8_t c, uint8_t d, |
| 422 | uint8_t e, uint8_t f, uint8_t g) { |
| 423 | // a|g|op|bbbb|f|e|d|c |
| 424 | |
| 425 | CHECK_LE(a, 5); |
| Eric Holk | d1b4383 | 2019-01-29 08:32:42 -0800 | [diff] [blame] | 426 | CHECK(IsShortRegister(c)); |
| 427 | CHECK(IsShortRegister(d)); |
| 428 | CHECK(IsShortRegister(e)); |
| 429 | CHECK(IsShortRegister(f)); |
| 430 | CHECK(IsShortRegister(g)); |
| Eric Holk | 1c0f3f0 | 2018-11-09 13:48:59 -0800 | [diff] [blame] | 431 | buffer_.push_back((a << 12) | (g << 8) | opcode); |
| 432 | buffer_.push_back(b); |
| 433 | buffer_.push_back((f << 12) | (e << 8) | (d << 4) | c); |
| 434 | } |
| 435 | |
| Eric Holk | d1b4383 | 2019-01-29 08:32:42 -0800 | [diff] [blame] | 436 | inline void Encode3rc(art::Instruction::Code opcode, size_t a, uint16_t b, uint16_t c) { |
| 437 | CHECK_LE(a, 255); |
| 438 | buffer_.push_back((a << 8) | opcode); |
| 439 | buffer_.push_back(b); |
| 440 | buffer_.push_back(c); |
| 441 | } |
| 442 | |
| 443 | static constexpr bool IsShortRegister(size_t register_value) { return register_value < 16; } |
| 444 | |
| 445 | // Returns an array of num_regs scratch registers. These are guaranteed to be |
| 446 | // contiguous, so they are suitable for the invoke-*/range instructions. |
| 447 | template <int num_regs> |
| 448 | std::array<Value, num_regs> GetScratchRegisters() const { |
| 449 | static_assert(num_regs <= kMaxScratchRegisters); |
| 450 | std::array<Value, num_regs> regs; |
| 451 | for (size_t i = 0; i < num_regs; ++i) { |
| Eric Holk | 5c6a1a5 | 2019-09-17 13:28:34 -0700 | [diff] [blame^] | 452 | regs[i] = std::move(Value::Local(NumRegisters() + i)); |
| Eric Holk | d1b4383 | 2019-01-29 08:32:42 -0800 | [diff] [blame] | 453 | } |
| 454 | return regs; |
| 455 | } |
| 456 | |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 457 | // Converts a register or parameter to its DEX register number. |
| Eric Holk | d62c5aa | 2018-11-01 15:50:24 -0700 | [diff] [blame] | 458 | size_t RegisterValue(const Value& value) const; |
| 459 | |
| 460 | // Sets a label's address to the current position in the instruction buffer. If there are any |
| 461 | // forward references to the label, this function will back-patch them. |
| 462 | void BindLabel(const Value& label); |
| 463 | |
| 464 | // Returns the offset of the label relative to the given instruction offset. If the label is not |
| 465 | // bound, a reference will be saved and it will automatically be patched when the label is bound. |
| 466 | ::dex::u2 LabelValue(const Value& label, size_t instruction_offset, size_t field_offset); |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 467 | |
| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 468 | DexBuilder* dex_; |
| 469 | ir::Class* class_; |
| 470 | ir::MethodDecl* decl_; |
| 471 | |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 472 | // A list of the instructions we will eventually encode. |
| 473 | std::vector<Instruction> instructions_; |
| 474 | |
| 475 | // A buffer to hold instructions that have been encoded. |
| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 476 | std::vector<::dex::u2> buffer_; |
| 477 | |
| Eric Holk | d1b4383 | 2019-01-29 08:32:42 -0800 | [diff] [blame] | 478 | // We create some scratch registers for when we have to shuffle registers |
| 479 | // around to make legal DEX code. |
| 480 | static constexpr size_t kMaxScratchRegisters = 5; |
| 481 | |
| Eric Holk | 5c6a1a5 | 2019-09-17 13:28:34 -0700 | [diff] [blame^] | 482 | size_t NumRegisters() const { |
| 483 | return register_liveness_.size(); |
| 484 | } |
| Eric Holk | d62c5aa | 2018-11-01 15:50:24 -0700 | [diff] [blame] | 485 | |
| 486 | // Stores information needed to back-patch a label once it is bound. We need to know the start of |
| 487 | // the instruction that refers to the label, and the offset to where the actual label value should |
| 488 | // go. |
| 489 | struct LabelReference { |
| 490 | size_t instruction_offset; |
| 491 | size_t field_offset; |
| 492 | }; |
| 493 | |
| 494 | struct LabelData { |
| 495 | std::optional<size_t> bound_address; |
| 496 | std::forward_list<LabelReference> references; |
| 497 | }; |
| 498 | |
| 499 | std::vector<LabelData> labels_; |
| Eric Holk | b392758 | 2018-11-08 16:40:16 -0800 | [diff] [blame] | 500 | |
| 501 | // During encoding, keep track of the largest number of arguments needed, so we can use it for our |
| 502 | // outs count |
| 503 | size_t max_args_{0}; |
| Eric Holk | 5c6a1a5 | 2019-09-17 13:28:34 -0700 | [diff] [blame^] | 504 | |
| 505 | std::vector<bool> register_liveness_; |
| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 506 | }; |
| 507 | |
| 508 | // A helper to build class definitions. |
| 509 | class ClassBuilder { |
| 510 | public: |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 511 | ClassBuilder(DexBuilder* parent, const std::string& name, ir::Class* class_def); |
| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 512 | |
| 513 | void set_source_file(const std::string& source); |
| 514 | |
| 515 | // Create a method with the given name and prototype. The returned MethodBuilder can be used to |
| 516 | // fill in the method body. |
| 517 | MethodBuilder CreateMethod(const std::string& name, Prototype prototype); |
| 518 | |
| 519 | private: |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 520 | DexBuilder* const parent_; |
| 521 | const TypeDescriptor type_descriptor_; |
| 522 | ir::Class* const class_; |
| 523 | }; |
| 524 | |
| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 525 | // Builds Dex files from scratch. |
| 526 | class DexBuilder { |
| 527 | public: |
| 528 | DexBuilder(); |
| 529 | |
| 530 | // Create an in-memory image of the DEX file that can either be loaded directly or written to a |
| 531 | // file. |
| 532 | slicer::MemView CreateImage(); |
| 533 | |
| 534 | template <typename T> |
| 535 | T* Alloc() { |
| 536 | return dex_file_->Alloc<T>(); |
| 537 | } |
| 538 | |
| 539 | // Find the ir::String that matches the given string, creating it if it does not exist. |
| 540 | ir::String* GetOrAddString(const std::string& string); |
| 541 | // Create a new class of the given name. |
| 542 | ClassBuilder MakeClass(const std::string& name); |
| 543 | |
| 544 | // Add a type for the given descriptor, or return the existing one if it already exists. |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 545 | // See the TypeDescriptor class for help generating these. GetOrAddType can be used to declare |
| 546 | // imported classes. |
| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 547 | ir::Type* GetOrAddType(const std::string& descriptor); |
| Eric Holk | 3092f99 | 2019-07-25 15:14:01 -0700 | [diff] [blame] | 548 | inline ir::Type* GetOrAddType(TypeDescriptor descriptor) { |
| 549 | return GetOrAddType(descriptor.descriptor()); |
| 550 | } |
| 551 | |
| 552 | ir::FieldDecl* GetOrAddField(TypeDescriptor parent, const std::string& name, TypeDescriptor type); |
| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 553 | |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 554 | // Returns the method id for the method, creating it if it has not been created yet. |
| 555 | const MethodDeclData& GetOrDeclareMethod(TypeDescriptor type, const std::string& name, |
| 556 | Prototype prototype); |
| 557 | |
| Eric Holk | d1b4383 | 2019-01-29 08:32:42 -0800 | [diff] [blame] | 558 | std::optional<const Prototype> GetPrototypeByMethodId(size_t method_id) const; |
| 559 | |
| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 560 | private: |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 561 | // Looks up the ir::Proto* corresponding to this given prototype, or creates one if it does not |
| 562 | // exist. |
| 563 | ir::Proto* GetOrEncodeProto(Prototype prototype); |
| 564 | |
| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 565 | std::shared_ptr<ir::DexFile> dex_file_; |
| 566 | |
| 567 | // allocator_ is needed to be able to encode the image. |
| 568 | TrackingAllocator allocator_; |
| 569 | |
| 570 | // We'll need to allocate buffers for all of the encoded strings we create. This is where we store |
| 571 | // all of them. |
| 572 | std::vector<std::unique_ptr<uint8_t[]>> string_data_; |
| 573 | |
| 574 | // Keep track of what types we've defined so we can look them up later. |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 575 | std::unordered_map<std::string, ir::Type*> types_by_descriptor_; |
| 576 | |
| 577 | struct MethodDescriptor { |
| 578 | TypeDescriptor type; |
| 579 | std::string name; |
| 580 | Prototype prototype; |
| 581 | |
| 582 | inline bool operator<(const MethodDescriptor& rhs) const { |
| 583 | return std::make_tuple(type, name, prototype) < |
| 584 | std::make_tuple(rhs.type, rhs.name, rhs.prototype); |
| 585 | } |
| 586 | }; |
| 587 | |
| 588 | // Maps method declarations to their method index. This is needed to encode references to them. |
| 589 | // When we go to actually write the DEX file, slicer will re-assign these after correctly sorting |
| 590 | // the methods list. |
| 591 | std::map<MethodDescriptor, MethodDeclData> method_id_map_; |
| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 592 | |
| 593 | // Keep track of what strings we've defined so we can look them up later. |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 594 | std::unordered_map<std::string, ir::String*> strings_; |
| 595 | |
| 596 | // Keep track of already-encoded protos. |
| 597 | std::map<Prototype, ir::Proto*> proto_map_; |
| Eric Holk | 3092f99 | 2019-07-25 15:14:01 -0700 | [diff] [blame] | 598 | |
| 599 | // Keep track of fields that have been declared |
| 600 | std::map<std::tuple<TypeDescriptor, std::string>, ir::FieldDecl*> field_decls_by_key_; |
| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 601 | }; |
| 602 | |
| Eric Holk | b392758 | 2018-11-08 16:40:16 -0800 | [diff] [blame] | 603 | template <typename... T> |
| Eric Holk | 5c6a1a5 | 2019-09-17 13:28:34 -0700 | [diff] [blame^] | 604 | void MethodBuilder::BuildNew(Value target, TypeDescriptor type, Prototype constructor, |
| 605 | const T&... args) { |
| Eric Holk | b392758 | 2018-11-08 16:40:16 -0800 | [diff] [blame] | 606 | MethodDeclData constructor_data{dex_->GetOrDeclareMethod(type, "<init>", constructor)}; |
| 607 | // allocate the object |
| 608 | ir::Type* type_def = dex_->GetOrAddType(type.descriptor()); |
| 609 | AddInstruction( |
| 610 | Instruction::OpWithArgs(Instruction::Op::kNew, target, Value::Type(type_def->orig_index))); |
| 611 | // call the constructor |
| 612 | AddInstruction(Instruction::InvokeDirect(constructor_data.id, /*dest=*/{}, target, args...)); |
| 613 | }; |
| 614 | |
| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 615 | } // namespace dex |
| 616 | } // namespace startop |
| 617 | |
| 618 | #endif // DEX_BUILDER_H_ |