github.com/agiledragon/gomonkey/v2@v2.11.1-0.20240427155748-d56c6823ec17/write_darwin_amd64.s (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  #include "textflag.h"
    18  
    19  #define NOP8 BYTE $0x90; BYTE $0x90; BYTE $0x90; BYTE $0x90; BYTE $0x90; BYTE $0x90; BYTE $0x90; BYTE $0x90;
    20  #define NOP64 NOP8; NOP8; NOP8; NOP8; NOP8; NOP8; NOP8; NOP8;
    21  #define NOP512 NOP64; NOP64; NOP64; NOP64; NOP64; NOP64; NOP64; NOP64;
    22  #define NOP4096 NOP512; NOP512; NOP512; NOP512; NOP512; NOP512; NOP512; NOP512;
    23  
    24  #define protRW $(0x1|0x2|0x10)
    25  #define mProtect $(0x2000000+74)
    26  
    27  TEXT ·write(SB),NOSPLIT,$24
    28      JMP START
    29      NOP4096
    30  START:
    31      MOVQ    mProtect, AX
    32      MOVQ    page+24(FP), DI
    33      MOVQ    pageSize+32(FP), SI
    34      MOVQ    protRW, DX
    35      SYSCALL
    36      CMPQ    AX, $0
    37      JZ      PROTECT_OK
    38      CALL    mach_task_self(SB)
    39      MOVQ    AX, DI
    40      MOVQ    target+0(FP), SI
    41      MOVQ    len+16(FP), DX
    42      MOVQ    $0, CX
    43      MOVQ    protRW, R8
    44      CALL    mach_vm_protect(SB)
    45      CMPQ    AX, $0
    46      JNZ     RETURN
    47  PROTECT_OK:
    48      MOVQ    target+0(FP), DI
    49      MOVQ    data+8(FP), SI
    50      MOVQ    len+16(FP), CX
    51      MOVQ    DI, to-24(SP)
    52      MOVQ    SI, from-16(SP)
    53      MOVQ    CX, n-8(SP)
    54      CALL    runtime·memmove(SB)
    55      MOVQ    mProtect, AX
    56      MOVQ    page+24(FP), DI
    57      MOVQ    pageSize+32(FP), SI
    58      MOVQ    oriProt+40(FP), DX
    59      SYSCALL
    60      JMP     RETURN
    61      NOP4096
    62  RETURN:
    63      MOVQ AX, ret+48(FP)
    64      RET