| 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 | |
| 143 | // A virtual instruction. We convert these to real instructions in MethodBuilder::Encode. |
| 144 | // Virtual instructions are needed to keep track of information that is not known until all of the |
| 145 | // code is generated. This information includes things like how many local registers are created and |
| 146 | // branch target locations. |
| 147 | class Instruction { |
| 148 | public: |
| 149 | // The operation performed by this instruction. These are virtual instructions that do not |
| 150 | // correspond exactly to DEX instructions. |
| Eric Holk | b392758 | 2018-11-08 16:40:16 -0800 | [diff] [blame] | 151 | enum class Op { |
| Eric Holk | b392758 | 2018-11-08 16:40:16 -0800 | [diff] [blame] | 152 | kBindLabel, |
| 153 | kBranchEqz, |
| Eric Holk | c69449d | 2018-12-13 11:35:58 -0800 | [diff] [blame] | 154 | kBranchNEqz, |
| Eric Holk | 44d8cdf | 2018-12-17 13:35:34 -0800 | [diff] [blame] | 155 | kCheckCast, |
| Eric Holk | f3b9589 | 2019-07-30 14:47:06 -0700 | [diff] [blame] | 156 | kGetInstanceField, |
| Eric Holk | 3092f99 | 2019-07-25 15:14:01 -0700 | [diff] [blame] | 157 | kGetStaticField, |
| Eric Holk | 44d8cdf | 2018-12-17 13:35:34 -0800 | [diff] [blame] | 158 | kInvokeDirect, |
| 159 | kInvokeInterface, |
| 160 | kInvokeStatic, |
| 161 | kInvokeVirtual, |
| 162 | kMove, |
| Eric Holk | d1b4383 | 2019-01-29 08:32:42 -0800 | [diff] [blame] | 163 | kMoveObject, |
| Eric Holk | 44d8cdf | 2018-12-17 13:35:34 -0800 | [diff] [blame] | 164 | kNew, |
| 165 | kReturn, |
| 166 | kReturnObject, |
| Eric Holk | f3b9589 | 2019-07-30 14:47:06 -0700 | [diff] [blame] | 167 | kSetInstanceField, |
| Eric Holk | 70445d0 | 2019-07-26 09:37:46 -0700 | [diff] [blame] | 168 | kSetStaticField |
| Eric Holk | b392758 | 2018-11-08 16:40:16 -0800 | [diff] [blame] | 169 | }; |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 170 | |
| 171 | //////////////////////// |
| 172 | // Named Constructors // |
| 173 | //////////////////////// |
| 174 | |
| 175 | // For instructions with no return value and no arguments. |
| 176 | static inline Instruction OpNoArgs(Op opcode) { |
| Eric Holk | 3092f99 | 2019-07-25 15:14:01 -0700 | [diff] [blame] | 177 | return Instruction{opcode, /*index_argument*/ 0, /*dest*/ {}}; |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 178 | } |
| 179 | // For most instructions, which take some number of arguments and have an optional return value. |
| 180 | template <typename... T> |
| 181 | static inline Instruction OpWithArgs(Op opcode, std::optional<const Value> dest, T... args) { |
| Eric Holk | 3092f99 | 2019-07-25 15:14:01 -0700 | [diff] [blame] | 182 | return Instruction{opcode, /*index_argument=*/0, /*result_is_object=*/false, dest, args...}; |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 183 | } |
| Eric Holk | 44d8cdf | 2018-12-17 13:35:34 -0800 | [diff] [blame] | 184 | |
| 185 | // A cast instruction. Basically, `(type)val` |
| 186 | static inline Instruction Cast(Value val, Value type) { |
| Eric Holk | d1b4383 | 2019-01-29 08:32:42 -0800 | [diff] [blame] | 187 | CHECK(type.is_type()); |
| Eric Holk | 44d8cdf | 2018-12-17 13:35:34 -0800 | [diff] [blame] | 188 | return OpWithArgs(Op::kCheckCast, val, type); |
| 189 | } |
| 190 | |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 191 | // For method calls. |
| 192 | template <typename... T> |
| Eric Holk | 3092f99 | 2019-07-25 15:14:01 -0700 | [diff] [blame] | 193 | 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] | 194 | Value this_arg, T... args) { |
| Eric Holk | c69449d | 2018-12-13 11:35:58 -0800 | [diff] [blame] | 195 | return Instruction{ |
| Eric Holk | 3092f99 | 2019-07-25 15:14:01 -0700 | [diff] [blame] | 196 | Op::kInvokeVirtual, index_argument, /*result_is_object=*/false, dest, this_arg, args...}; |
| Eric Holk | c69449d | 2018-12-13 11:35:58 -0800 | [diff] [blame] | 197 | } |
| 198 | // Returns an object |
| 199 | template <typename... T> |
| Eric Holk | f3b9589 | 2019-07-30 14:47:06 -0700 | [diff] [blame] | 200 | static inline Instruction InvokeVirtualObject(size_t index_argument, |
| 201 | std::optional<const Value> dest, Value this_arg, |
| 202 | T... args) { |
| Eric Holk | c69449d | 2018-12-13 11:35:58 -0800 | [diff] [blame] | 203 | return Instruction{ |
| Eric Holk | 3092f99 | 2019-07-25 15:14:01 -0700 | [diff] [blame] | 204 | Op::kInvokeVirtual, index_argument, /*result_is_object=*/true, dest, this_arg, args...}; |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 205 | } |
| Eric Holk | b392758 | 2018-11-08 16:40:16 -0800 | [diff] [blame] | 206 | // For direct calls (basically, constructors). |
| 207 | template <typename... T> |
| Eric Holk | 3092f99 | 2019-07-25 15:14:01 -0700 | [diff] [blame] | 208 | static inline Instruction InvokeDirect(size_t index_argument, std::optional<const Value> dest, |
| Eric Holk | b392758 | 2018-11-08 16:40:16 -0800 | [diff] [blame] | 209 | Value this_arg, T... args) { |
| Eric Holk | c69449d | 2018-12-13 11:35:58 -0800 | [diff] [blame] | 210 | return Instruction{ |
| Eric Holk | 3092f99 | 2019-07-25 15:14:01 -0700 | [diff] [blame] | 211 | Op::kInvokeDirect, index_argument, /*result_is_object=*/false, dest, this_arg, args...}; |
| Eric Holk | c69449d | 2018-12-13 11:35:58 -0800 | [diff] [blame] | 212 | } |
| 213 | // Returns an object |
| 214 | template <typename... T> |
| Eric Holk | f3b9589 | 2019-07-30 14:47:06 -0700 | [diff] [blame] | 215 | static inline Instruction InvokeDirectObject(size_t index_argument, |
| 216 | std::optional<const Value> dest, Value this_arg, |
| 217 | T... args) { |
| Eric Holk | c69449d | 2018-12-13 11:35:58 -0800 | [diff] [blame] | 218 | return Instruction{ |
| Eric Holk | 3092f99 | 2019-07-25 15:14:01 -0700 | [diff] [blame] | 219 | Op::kInvokeDirect, index_argument, /*result_is_object=*/true, dest, this_arg, args...}; |
| Eric Holk | c69449d | 2018-12-13 11:35:58 -0800 | [diff] [blame] | 220 | } |
| 221 | // For static calls. |
| 222 | template <typename... T> |
| Eric Holk | 3092f99 | 2019-07-25 15:14:01 -0700 | [diff] [blame] | 223 | 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] | 224 | T... args) { |
| Eric Holk | f3b9589 | 2019-07-30 14:47:06 -0700 | [diff] [blame] | 225 | return Instruction{ |
| 226 | Op::kInvokeStatic, index_argument, /*result_is_object=*/false, dest, args...}; |
| Eric Holk | c69449d | 2018-12-13 11:35:58 -0800 | [diff] [blame] | 227 | } |
| 228 | // Returns an object |
| 229 | template <typename... T> |
| Eric Holk | f3b9589 | 2019-07-30 14:47:06 -0700 | [diff] [blame] | 230 | static inline Instruction InvokeStaticObject(size_t index_argument, |
| 231 | std::optional<const Value> dest, T... args) { |
| Eric Holk | 3092f99 | 2019-07-25 15:14:01 -0700 | [diff] [blame] | 232 | return Instruction{Op::kInvokeStatic, index_argument, /*result_is_object=*/true, dest, args...}; |
| Eric Holk | c69449d | 2018-12-13 11:35:58 -0800 | [diff] [blame] | 233 | } |
| 234 | // For static calls. |
| 235 | template <typename... T> |
| Eric Holk | 3092f99 | 2019-07-25 15:14:01 -0700 | [diff] [blame] | 236 | static inline Instruction InvokeInterface(size_t index_argument, std::optional<const Value> dest, |
| Eric Holk | c69449d | 2018-12-13 11:35:58 -0800 | [diff] [blame] | 237 | T... args) { |
| Eric Holk | f3b9589 | 2019-07-30 14:47:06 -0700 | [diff] [blame] | 238 | return Instruction{ |
| 239 | Op::kInvokeInterface, index_argument, /*result_is_object=*/false, dest, args...}; |
| Eric Holk | 3092f99 | 2019-07-25 15:14:01 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | static inline Instruction GetStaticField(size_t field_id, Value dest) { |
| 243 | return Instruction{Op::kGetStaticField, field_id, dest}; |
| Eric Holk | b392758 | 2018-11-08 16:40:16 -0800 | [diff] [blame] | 244 | } |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 245 | |
| Eric Holk | 70445d0 | 2019-07-26 09:37:46 -0700 | [diff] [blame] | 246 | static inline Instruction SetStaticField(size_t field_id, Value value) { |
| Eric Holk | f3b9589 | 2019-07-30 14:47:06 -0700 | [diff] [blame] | 247 | return Instruction{ |
| 248 | Op::kSetStaticField, field_id, /*result_is_object=*/false, /*dest=*/{}, value}; |
| Eric Holk | 70445d0 | 2019-07-26 09:37:46 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| Eric Holk | f3b9589 | 2019-07-30 14:47:06 -0700 | [diff] [blame] | 251 | static inline Instruction GetField(size_t field_id, Value dest, Value object) { |
| 252 | return Instruction{Op::kGetInstanceField, field_id, /*result_is_object=*/false, dest, object}; |
| 253 | } |
| 254 | |
| 255 | static inline Instruction SetField(size_t field_id, Value object, Value value) { |
| 256 | return Instruction{ |
| 257 | Op::kSetInstanceField, field_id, /*result_is_object=*/false, /*dest=*/{}, object, value}; |
| 258 | } |
| Eric Holk | 70445d0 | 2019-07-26 09:37:46 -0700 | [diff] [blame] | 259 | |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 260 | /////////////// |
| 261 | // Accessors // |
| 262 | /////////////// |
| 263 | |
| 264 | Op opcode() const { return opcode_; } |
| Eric Holk | 3092f99 | 2019-07-25 15:14:01 -0700 | [diff] [blame] | 265 | size_t index_argument() const { return index_argument_; } |
| Eric Holk | c69449d | 2018-12-13 11:35:58 -0800 | [diff] [blame] | 266 | bool result_is_object() const { return result_is_object_; } |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 267 | const std::optional<const Value>& dest() const { return dest_; } |
| 268 | const std::vector<const Value>& args() const { return args_; } |
| 269 | |
| 270 | private: |
| Eric Holk | 3092f99 | 2019-07-25 15:14:01 -0700 | [diff] [blame] | 271 | 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] | 272 | : opcode_{opcode}, |
| 273 | index_argument_{index_argument}, |
| 274 | result_is_object_{false}, |
| 275 | dest_{dest}, |
| 276 | args_{} {} |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 277 | |
| 278 | template <typename... T> |
| Eric Holk | f3b9589 | 2019-07-30 14:47:06 -0700 | [diff] [blame] | 279 | inline Instruction(Op opcode, size_t index_argument, bool result_is_object, |
| Eric Holk | c69449d | 2018-12-13 11:35:58 -0800 | [diff] [blame] | 280 | std::optional<const Value> dest, T... args) |
| 281 | : opcode_{opcode}, |
| Eric Holk | 3092f99 | 2019-07-25 15:14:01 -0700 | [diff] [blame] | 282 | index_argument_{index_argument}, |
| Eric Holk | c69449d | 2018-12-13 11:35:58 -0800 | [diff] [blame] | 283 | result_is_object_{result_is_object}, |
| 284 | dest_{dest}, |
| 285 | args_{args...} {} |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 286 | |
| 287 | const Op opcode_; |
| 288 | // The index of the method to invoke, for kInvokeVirtual and similar opcodes. |
| Eric Holk | 3092f99 | 2019-07-25 15:14:01 -0700 | [diff] [blame] | 289 | const size_t index_argument_{0}; |
| Eric Holk | c69449d | 2018-12-13 11:35:58 -0800 | [diff] [blame] | 290 | const bool result_is_object_; |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 291 | const std::optional<const Value> dest_; |
| 292 | const std::vector<const Value> args_; |
| 293 | }; |
| 294 | |
| 295 | // Needed for CHECK_EQ, DCHECK_EQ, etc. |
| 296 | std::ostream& operator<<(std::ostream& out, const Instruction::Op& opcode); |
| 297 | |
| Eric Holk | b392758 | 2018-11-08 16:40:16 -0800 | [diff] [blame] | 298 | // Keeps track of information needed to manipulate or call a method. |
| 299 | struct MethodDeclData { |
| 300 | size_t id; |
| 301 | ir::MethodDecl* decl; |
| 302 | }; |
| 303 | |
| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 304 | // Tools to help build methods and their bodies. |
| 305 | class MethodBuilder { |
| 306 | public: |
| 307 | MethodBuilder(DexBuilder* dex, ir::Class* class_def, ir::MethodDecl* decl); |
| 308 | |
| 309 | // Encode the method into DEX format. |
| 310 | ir::EncodedMethod* Encode(); |
| 311 | |
| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 312 | // Create a new register to be used to storing values. Note that these are not SSA registers, like |
| 313 | // might be expected in similar code generators. This does no liveness tracking or anything, so |
| 314 | // it's up to the caller to reuse registers as appropriate. |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 315 | Value MakeRegister(); |
| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 316 | |
| Eric Holk | d62c5aa | 2018-11-01 15:50:24 -0700 | [diff] [blame] | 317 | Value MakeLabel(); |
| 318 | |
| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 319 | ///////////////////////////////// |
| 320 | // Instruction builder methods // |
| 321 | ///////////////////////////////// |
| 322 | |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 323 | void AddInstruction(Instruction instruction); |
| 324 | |
| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 325 | // return-void |
| 326 | void BuildReturn(); |
| Eric Holk | 3cc4afc | 2018-11-08 14:16:20 -0800 | [diff] [blame] | 327 | void BuildReturn(Value src, bool is_object = false); |
| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 328 | // const/4 |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 329 | void BuildConst4(Value target, int value); |
| Eric Holk | 3cc4afc | 2018-11-08 14:16:20 -0800 | [diff] [blame] | 330 | void BuildConstString(Value target, const std::string& value); |
| Eric Holk | b392758 | 2018-11-08 16:40:16 -0800 | [diff] [blame] | 331 | template <typename... T> |
| 332 | void BuildNew(Value target, TypeDescriptor type, Prototype constructor, T... args); |
| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 333 | |
| 334 | // TODO: add builders for more instructions |
| 335 | |
| Eric Holk | c69449d | 2018-12-13 11:35:58 -0800 | [diff] [blame] | 336 | DexBuilder* dex_file() const { return dex_; } |
| 337 | |
| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 338 | private: |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 339 | void EncodeInstructions(); |
| 340 | void EncodeInstruction(const Instruction& instruction); |
| Eric Holk | 3cc4afc | 2018-11-08 14:16:20 -0800 | [diff] [blame] | 341 | |
| 342 | // Encodes a return instruction. For instructions with no return value, the opcode field is |
| 343 | // ignored. Otherwise, this specifies which return instruction will be used (return, |
| 344 | // return-object, etc.) |
| 345 | void EncodeReturn(const Instruction& instruction, ::art::Instruction::Code opcode); |
| 346 | |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 347 | void EncodeMove(const Instruction& instruction); |
| Eric Holk | b392758 | 2018-11-08 16:40:16 -0800 | [diff] [blame] | 348 | void EncodeInvoke(const Instruction& instruction, ::art::Instruction::Code opcode); |
| Eric Holk | d62c5aa | 2018-11-01 15:50:24 -0700 | [diff] [blame] | 349 | void EncodeBranch(art::Instruction::Code op, const Instruction& instruction); |
| Eric Holk | b392758 | 2018-11-08 16:40:16 -0800 | [diff] [blame] | 350 | void EncodeNew(const Instruction& instruction); |
| Eric Holk | 44d8cdf | 2018-12-17 13:35:34 -0800 | [diff] [blame] | 351 | void EncodeCast(const Instruction& instruction); |
| Eric Holk | f3b9589 | 2019-07-30 14:47:06 -0700 | [diff] [blame] | 352 | void EncodeFieldOp(const Instruction& instruction); |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 353 | |
| Eric Holk | 1c0f3f0 | 2018-11-09 13:48:59 -0800 | [diff] [blame] | 354 | // Low-level instruction format encoding. See |
| 355 | // https://source.android.com/devices/tech/dalvik/instruction-formats for documentation of |
| 356 | // formats. |
| 357 | |
| 358 | inline void Encode10x(art::Instruction::Code opcode) { |
| 359 | // 00|op |
| 360 | buffer_.push_back(opcode); |
| 361 | } |
| 362 | |
| 363 | inline void Encode11x(art::Instruction::Code opcode, uint8_t a) { |
| 364 | // aa|op |
| 365 | buffer_.push_back((a << 8) | opcode); |
| 366 | } |
| 367 | |
| 368 | inline void Encode11n(art::Instruction::Code opcode, uint8_t a, int8_t b) { |
| 369 | // b|a|op |
| 370 | |
| 371 | // Make sure the fields are in bounds (4 bits for a, 4 bits for b). |
| 372 | CHECK_LT(a, 16); |
| 373 | CHECK_LE(-8, b); |
| 374 | CHECK_LT(b, 8); |
| 375 | |
| 376 | buffer_.push_back(((b & 0xf) << 12) | (a << 8) | opcode); |
| 377 | } |
| 378 | |
| 379 | inline void Encode21c(art::Instruction::Code opcode, uint8_t a, uint16_t b) { |
| 380 | // aa|op|bbbb |
| 381 | buffer_.push_back((a << 8) | opcode); |
| 382 | buffer_.push_back(b); |
| 383 | } |
| 384 | |
| Eric Holk | f3b9589 | 2019-07-30 14:47:06 -0700 | [diff] [blame] | 385 | inline void Encode22c(art::Instruction::Code opcode, uint8_t a, uint8_t b, uint16_t c) { |
| 386 | // b|a|op|bbbb |
| 387 | CHECK(IsShortRegister(a)); |
| 388 | CHECK(IsShortRegister(b)); |
| 389 | buffer_.push_back((b << 12) | (a << 8) | opcode); |
| 390 | buffer_.push_back(c); |
| 391 | } |
| 392 | |
| Eric Holk | d1b4383 | 2019-01-29 08:32:42 -0800 | [diff] [blame] | 393 | inline void Encode32x(art::Instruction::Code opcode, uint16_t a, uint16_t b) { |
| 394 | buffer_.push_back(opcode); |
| 395 | buffer_.push_back(a); |
| 396 | buffer_.push_back(b); |
| 397 | } |
| 398 | |
| Eric Holk | 1c0f3f0 | 2018-11-09 13:48:59 -0800 | [diff] [blame] | 399 | inline void Encode35c(art::Instruction::Code opcode, size_t a, uint16_t b, uint8_t c, uint8_t d, |
| 400 | uint8_t e, uint8_t f, uint8_t g) { |
| 401 | // a|g|op|bbbb|f|e|d|c |
| 402 | |
| 403 | CHECK_LE(a, 5); |
| Eric Holk | d1b4383 | 2019-01-29 08:32:42 -0800 | [diff] [blame] | 404 | CHECK(IsShortRegister(c)); |
| 405 | CHECK(IsShortRegister(d)); |
| 406 | CHECK(IsShortRegister(e)); |
| 407 | CHECK(IsShortRegister(f)); |
| 408 | CHECK(IsShortRegister(g)); |
| Eric Holk | 1c0f3f0 | 2018-11-09 13:48:59 -0800 | [diff] [blame] | 409 | buffer_.push_back((a << 12) | (g << 8) | opcode); |
| 410 | buffer_.push_back(b); |
| 411 | buffer_.push_back((f << 12) | (e << 8) | (d << 4) | c); |
| 412 | } |
| 413 | |
| Eric Holk | d1b4383 | 2019-01-29 08:32:42 -0800 | [diff] [blame] | 414 | inline void Encode3rc(art::Instruction::Code opcode, size_t a, uint16_t b, uint16_t c) { |
| 415 | CHECK_LE(a, 255); |
| 416 | buffer_.push_back((a << 8) | opcode); |
| 417 | buffer_.push_back(b); |
| 418 | buffer_.push_back(c); |
| 419 | } |
| 420 | |
| 421 | static constexpr bool IsShortRegister(size_t register_value) { return register_value < 16; } |
| 422 | |
| 423 | // Returns an array of num_regs scratch registers. These are guaranteed to be |
| 424 | // contiguous, so they are suitable for the invoke-*/range instructions. |
| 425 | template <int num_regs> |
| 426 | std::array<Value, num_regs> GetScratchRegisters() const { |
| 427 | static_assert(num_regs <= kMaxScratchRegisters); |
| 428 | std::array<Value, num_regs> regs; |
| 429 | for (size_t i = 0; i < num_regs; ++i) { |
| 430 | regs[i] = std::move(Value::Local(num_registers_ + i)); |
| 431 | } |
| 432 | return regs; |
| 433 | } |
| 434 | |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 435 | // Converts a register or parameter to its DEX register number. |
| Eric Holk | d62c5aa | 2018-11-01 15:50:24 -0700 | [diff] [blame] | 436 | size_t RegisterValue(const Value& value) const; |
| 437 | |
| 438 | // Sets a label's address to the current position in the instruction buffer. If there are any |
| 439 | // forward references to the label, this function will back-patch them. |
| 440 | void BindLabel(const Value& label); |
| 441 | |
| 442 | // Returns the offset of the label relative to the given instruction offset. If the label is not |
| 443 | // bound, a reference will be saved and it will automatically be patched when the label is bound. |
| 444 | ::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] | 445 | |
| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 446 | DexBuilder* dex_; |
| 447 | ir::Class* class_; |
| 448 | ir::MethodDecl* decl_; |
| 449 | |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 450 | // A list of the instructions we will eventually encode. |
| 451 | std::vector<Instruction> instructions_; |
| 452 | |
| 453 | // A buffer to hold instructions that have been encoded. |
| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 454 | std::vector<::dex::u2> buffer_; |
| 455 | |
| Eric Holk | d1b4383 | 2019-01-29 08:32:42 -0800 | [diff] [blame] | 456 | // We create some scratch registers for when we have to shuffle registers |
| 457 | // around to make legal DEX code. |
| 458 | static constexpr size_t kMaxScratchRegisters = 5; |
| 459 | |
| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 460 | // How many registers we've allocated |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 461 | size_t num_registers_{0}; |
| Eric Holk | d62c5aa | 2018-11-01 15:50:24 -0700 | [diff] [blame] | 462 | |
| 463 | // Stores information needed to back-patch a label once it is bound. We need to know the start of |
| 464 | // the instruction that refers to the label, and the offset to where the actual label value should |
| 465 | // go. |
| 466 | struct LabelReference { |
| 467 | size_t instruction_offset; |
| 468 | size_t field_offset; |
| 469 | }; |
| 470 | |
| 471 | struct LabelData { |
| 472 | std::optional<size_t> bound_address; |
| 473 | std::forward_list<LabelReference> references; |
| 474 | }; |
| 475 | |
| 476 | std::vector<LabelData> labels_; |
| Eric Holk | b392758 | 2018-11-08 16:40:16 -0800 | [diff] [blame] | 477 | |
| 478 | // During encoding, keep track of the largest number of arguments needed, so we can use it for our |
| 479 | // outs count |
| 480 | size_t max_args_{0}; |
| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 481 | }; |
| 482 | |
| 483 | // A helper to build class definitions. |
| 484 | class ClassBuilder { |
| 485 | public: |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 486 | ClassBuilder(DexBuilder* parent, const std::string& name, ir::Class* class_def); |
| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 487 | |
| 488 | void set_source_file(const std::string& source); |
| 489 | |
| 490 | // Create a method with the given name and prototype. The returned MethodBuilder can be used to |
| 491 | // fill in the method body. |
| 492 | MethodBuilder CreateMethod(const std::string& name, Prototype prototype); |
| 493 | |
| 494 | private: |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 495 | DexBuilder* const parent_; |
| 496 | const TypeDescriptor type_descriptor_; |
| 497 | ir::Class* const class_; |
| 498 | }; |
| 499 | |
| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 500 | // Builds Dex files from scratch. |
| 501 | class DexBuilder { |
| 502 | public: |
| 503 | DexBuilder(); |
| 504 | |
| 505 | // Create an in-memory image of the DEX file that can either be loaded directly or written to a |
| 506 | // file. |
| 507 | slicer::MemView CreateImage(); |
| 508 | |
| 509 | template <typename T> |
| 510 | T* Alloc() { |
| 511 | return dex_file_->Alloc<T>(); |
| 512 | } |
| 513 | |
| 514 | // Find the ir::String that matches the given string, creating it if it does not exist. |
| 515 | ir::String* GetOrAddString(const std::string& string); |
| 516 | // Create a new class of the given name. |
| 517 | ClassBuilder MakeClass(const std::string& name); |
| 518 | |
| 519 | // 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] | 520 | // See the TypeDescriptor class for help generating these. GetOrAddType can be used to declare |
| 521 | // imported classes. |
| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 522 | ir::Type* GetOrAddType(const std::string& descriptor); |
| Eric Holk | 3092f99 | 2019-07-25 15:14:01 -0700 | [diff] [blame] | 523 | inline ir::Type* GetOrAddType(TypeDescriptor descriptor) { |
| 524 | return GetOrAddType(descriptor.descriptor()); |
| 525 | } |
| 526 | |
| 527 | ir::FieldDecl* GetOrAddField(TypeDescriptor parent, const std::string& name, TypeDescriptor type); |
| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 528 | |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 529 | // Returns the method id for the method, creating it if it has not been created yet. |
| 530 | const MethodDeclData& GetOrDeclareMethod(TypeDescriptor type, const std::string& name, |
| 531 | Prototype prototype); |
| 532 | |
| Eric Holk | d1b4383 | 2019-01-29 08:32:42 -0800 | [diff] [blame] | 533 | std::optional<const Prototype> GetPrototypeByMethodId(size_t method_id) const; |
| 534 | |
| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 535 | private: |
| Eric Holk | faefd4f | 2018-10-11 16:25:57 -0700 | [diff] [blame] | 536 | // Looks up the ir::Proto* corresponding to this given prototype, or creates one if it does not |
| 537 | // exist. |
| 538 | ir::Proto* GetOrEncodeProto(Prototype prototype); |
| 539 | |
| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 540 | std::shared_ptr<ir::DexFile> dex_file_; |
| 541 | |
| 542 | // allocator_ is needed to be able to encode the image. |
| 543 | TrackingAllocator allocator_; |
| 544 | |
| 545 | // We'll need to allocate buffers for all of the encoded strings we create. This is where we store |
| 546 | // all of them. |
| 547 | std::vector<std::unique_ptr<uint8_t[]>> string_data_; |
| 548 | |
| 549 | // 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] | 550 | std::unordered_map<std::string, ir::Type*> types_by_descriptor_; |
| 551 | |
| 552 | struct MethodDescriptor { |
| 553 | TypeDescriptor type; |
| 554 | std::string name; |
| 555 | Prototype prototype; |
| 556 | |
| 557 | inline bool operator<(const MethodDescriptor& rhs) const { |
| 558 | return std::make_tuple(type, name, prototype) < |
| 559 | std::make_tuple(rhs.type, rhs.name, rhs.prototype); |
| 560 | } |
| 561 | }; |
| 562 | |
| 563 | // Maps method declarations to their method index. This is needed to encode references to them. |
| 564 | // When we go to actually write the DEX file, slicer will re-assign these after correctly sorting |
| 565 | // the methods list. |
| 566 | std::map<MethodDescriptor, MethodDeclData> method_id_map_; |
| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 567 | |
| 568 | // 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] | 569 | std::unordered_map<std::string, ir::String*> strings_; |
| 570 | |
| 571 | // Keep track of already-encoded protos. |
| 572 | std::map<Prototype, ir::Proto*> proto_map_; |
| Eric Holk | 3092f99 | 2019-07-25 15:14:01 -0700 | [diff] [blame] | 573 | |
| 574 | // Keep track of fields that have been declared |
| 575 | 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] | 576 | }; |
| 577 | |
| Eric Holk | b392758 | 2018-11-08 16:40:16 -0800 | [diff] [blame] | 578 | template <typename... T> |
| 579 | void MethodBuilder::BuildNew(Value target, TypeDescriptor type, Prototype constructor, T... args) { |
| 580 | MethodDeclData constructor_data{dex_->GetOrDeclareMethod(type, "<init>", constructor)}; |
| 581 | // allocate the object |
| 582 | ir::Type* type_def = dex_->GetOrAddType(type.descriptor()); |
| 583 | AddInstruction( |
| 584 | Instruction::OpWithArgs(Instruction::Op::kNew, target, Value::Type(type_def->orig_index))); |
| 585 | // call the constructor |
| 586 | AddInstruction(Instruction::InvokeDirect(constructor_data.id, /*dest=*/{}, target, args...)); |
| 587 | }; |
| 588 | |
| Eric Holk | dbc36e2 | 2018-09-20 12:03:10 -0700 | [diff] [blame] | 589 | } // namespace dex |
| 590 | } // namespace startop |
| 591 | |
| 592 | #endif // DEX_BUILDER_H_ |