github.com/Rookout/GoSDK@v0.1.48/pkg/services/instrumentation/hooker/prologue/prologue_arm64.go (about)

     1  //go:build arm64
     2  // +build arm64
     3  
     4  package prologue
     5  
     6  import (
     7  	_ "unsafe"
     8  
     9  	"github.com/Rookout/GoSDK/pkg/logger"
    10  	"github.com/Rookout/GoSDK/pkg/utils"
    11  	"golang.org/x/arch/arm64/arm64asm"
    12  
    13  	"github.com/Rookout/GoSDK/pkg/rookoutErrors"
    14  	"github.com/Rookout/GoSDK/pkg/services/assembler"
    15  	"github.com/Rookout/GoSDK/pkg/services/assembler/common"
    16  	"github.com/Rookout/GoSDK/pkg/services/instrumentation/hooker/regbackup"
    17  )
    18  
    19  var stackUsageBuffer = 0x40 + 0x400 
    20  
    21  var regsBackupBuffer = make([]regbackup.Backup, 1000)
    22  
    23  
    24  func (g *Generator) generateCheckStackUsage(b *assembler.Builder) rookoutErrors.RookoutError {
    25  	return b.AddInstructions(
    26  		b.Label(startLabel),
    27  		common.MovGToX20(b),
    28  		b.Inst(assembler.AMOVD, arm64asm.X20, assembler.Mem{Base: arm64asm.X20, Disp: common.StackguardOffset}),
    29  		b.Sub3(arm64asm.X19, arm64asm.SP, assembler.Imm(uint64(g.stackUsage+stackUsageBuffer))),
    30  		b.Cmp(arm64asm.X19, arm64asm.X20),
    31  		b.BranchToLabel(assembler.ABGT, endLabel),
    32  	)
    33  }
    34  
    35  func (g *Generator) getOriginalRegBackup() (regBackup []byte, regRestore []byte) {
    36  	for _, inst := range g.epilogueInstructions {
    37  		instBytes := utils.MakeSliceFromPointer(inst.PC, inst.Len)
    38  		switch inst.Op {
    39  		case arm64asm.STR, arm64asm.STP:
    40  			regBackup = append(regBackup, instBytes...)
    41  		case arm64asm.LDR, arm64asm.LDP:
    42  			regRestore = append(regRestore, instBytes...)
    43  		case arm64asm.MOV, arm64asm.BL, arm64asm.NOP:
    44  		default:
    45  			logger.Logger().Warningf("Found unexpected instruction in epilogue: %v", inst)
    46  		}
    47  	}
    48  	return regBackup, regRestore
    49  }
    50  
    51  func (g *Generator) generateCallMorestack(b *assembler.Builder) rookoutErrors.RookoutError {
    52  	return b.AddInstructions(
    53  		b.Inst(assembler.AMOVD, arm64asm.X3, arm64asm.X30),
    54  		b.Inst(assembler.AMOVD, arm64asm.X20, assembler.Imm(uint64(g.morestackAddr))),
    55  		b.BranchToReg(assembler.ACALL, arm64asm.X20),
    56  	)
    57  }
    58  
    59  func (g *Generator) generateJumpToStart(b *assembler.Builder) rookoutErrors.RookoutError {
    60  	return b.AddInstructions(
    61  		b.BranchToLabel(assembler.AJMP, startLabel),
    62  		b.Label(endLabel),
    63  		b.PsuedoNop(),
    64  	)
    65  }
    66  
    67  
    68  
    69  func (g *Generator) generateCallFallback(b *assembler.Builder) rookoutErrors.RookoutError {
    70  	return b.AddInstructions(
    71  		b.BranchToLabel(assembler.AJMP, "afterFallback"),
    72  		b.Label(fallbackLabel),
    73  		b.Inst(assembler.AMOVD, arm64asm.X20, assembler.Imm(uint64(g.fallbackAddr))),
    74  		b.BranchToReg(assembler.AJMP, arm64asm.X20),
    75  		b.Label("afterFallback"),
    76  	)
    77  }