github.com/bytedance/mockey@v1.2.10/internal/monkey/inst/inst_arm64.go (about)

     1  /*
     2   * Copyright 2022 ByteDance Inc.
     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  
    17  package inst
    18  
    19  import "unsafe"
    20  
    21  func BranchTo(to uintptr) (res []byte) {
    22  	res = append(res, x26MOV(to)...)                     // MOV x26, to // fake
    23  	res = append(res, []byte{0x40, 0x03, 0x1f, 0xd6}...) // BR x26
    24  	return
    25  }
    26  
    27  // create a branch into command
    28  //
    29  // Go supports passing function arguments from go 1.17 (see https://go.dev/doc/go1.17).
    30  // We could not use x0~x18 register. As an alternative, we use R19 register (see https://go.googlesource.com/go/+/refs/heads/master/src/cmd/compile/abi-internal.md).
    31  func BranchInto(to uintptr) (res []byte) {
    32  	// do not use x0~x18
    33  	res = append(res, x26MOV(to)...)                     // MOV x26, to // fake
    34  	res = append(res, []byte{0x53, 0x03, 0x40, 0xf9}...) // LDR x19, [x26]
    35  	res = append(res, []byte{0x60, 0x02, 0x1f, 0xd6}...) // BR x19
    36  	return
    37  }
    38  
    39  const x26 uint32 = 0b11010
    40  
    41  // x26MOV moves the 64bit value to x26 register, using the following four instructions:
    42  // MOVZ x26, val[0:16]
    43  // MOVK x26, val[16:32]
    44  // MOVK x26, val[32:48]
    45  // MOVK x26, val[48:64]
    46  func x26MOV(val uintptr) (res []byte) {
    47  	res = append(res, x26MOVZ(val)...)
    48  	res = append(res, x26MOVK(val, 1)...)
    49  	res = append(res, x26MOVK(val, 2)...)
    50  	res = append(res, x26MOVK(val, 3)...)
    51  	return res
    52  }
    53  
    54  // x26MOVZ see https://developer.arm.com/documentation/ddi0596/2021-12/Base-Instructions/MOVZ--Move-wide-with-zero-
    55  func x26MOVZ(val uintptr) []byte {
    56  	imm := uint32(val & 0xffff)
    57  	inst := 0b1<<31 | 0b1010010100<<21 | imm<<5 | x26
    58  	res := make([]byte, 4)
    59  	*(*uint32)(unsafe.Pointer(&res[0])) = inst
    60  	return res
    61  }
    62  
    63  // x26MOVK see https://developer.arm.com/documentation/ddi0596/2021-12/Base-Instructions/MOVK--Move-wide-with-keep-
    64  func x26MOVK(val uintptr, shift int) []byte {
    65  	imm := uint32((val >> (shift * 0x10)) & 0xffff)
    66  	inst := 0b1<<31 | 0b111100101<<23 | uint32(shift)&0b11<<21 | imm<<5 | 26
    67  	res := make([]byte, 4)
    68  	*(*uint32)(unsafe.Pointer(&res[0])) = inst
    69  	return res
    70  }