github.com/bytedance/mockey@v1.2.10/internal/monkey/mem/write_darwin_arm64.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 NOP64 WORD $0x1f2003d5; WORD $0x1f2003d5;
    20  #define NOP512 NOP64; NOP64; NOP64; NOP64; NOP64; NOP64; NOP64; NOP64;
    21  #define NOP4096 NOP512; NOP512; NOP512; NOP512; NOP512; NOP512; NOP512; NOP512;
    22  #define NOP16384 NOP4096; NOP4096; NOP4096; NOP4096; NOP4096; NOP4096; NOP4096; NOP4096;
    23  
    24  #define protRW $(0x1|0x2|0x10)
    25  #define mProtect $(0x2000000+74)
    26  
    27  TEXT ·write(SB),NOSPLIT,$24
    28      B START
    29      NOP16384
    30  START:
    31      MOVD    mProtect, R16
    32      MOVD    page+24(FP), R0
    33      MOVD    pageSize+32(FP), R1
    34      MOVD    protRW, R2
    35      SVC     $0x80
    36      CMP     $0, R0
    37      BEQ     PROTECT_OK
    38      CALL    mach_task_self(SB)
    39      MOVD    target+0(FP), R1
    40      MOVD    len+16(FP), R2
    41      MOVD    $0, R3
    42      MOVD    protRW, R4
    43      CALL    mach_vm_protect(SB)
    44      CMP     $0, R0
    45      BNE     RETURN
    46  PROTECT_OK:
    47      MOVD    target+0(FP), R0
    48      MOVD    data+8(FP), R1
    49      MOVD    len+16(FP), R2
    50      MOVD    R0, to-24(SP)
    51      MOVD    R1, from-16(SP)
    52      MOVD    R2, n-8(SP)
    53      CALL    runtime·memmove(SB)
    54      MOVD    mProtect, R16
    55      MOVD    page+24(FP), R0
    56      MOVD    pageSize+32(FP), R1
    57      MOVD    oriProt+40(FP), R2
    58      SVC     $0x80
    59      B       RETURN
    60      NOP16384
    61  RETURN:
    62      MOVD R0, ret+48(FP)
    63      RET