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

     1  //go:build amd64
     2  // +build amd64
     3  
     4  package prologue
     5  
     6  import (
     7  	"strings"
     8  
     9  	"github.com/Rookout/GoSDK/pkg/rookoutErrors"
    10  	"github.com/Rookout/GoSDK/pkg/services/assembler"
    11  	"github.com/Rookout/GoSDK/pkg/services/assembler/common"
    12  	"github.com/Rookout/GoSDK/pkg/services/instrumentation/hooker/regbackup"
    13  	"github.com/Rookout/GoSDK/pkg/utils"
    14  	"golang.org/x/arch/x86/x86asm"
    15  )
    16  
    17  var stackUsageBuffer = 0x28 + 256 
    18  var regsBackupBuffer = make([]regbackup.Backup, 1000)
    19  
    20  
    21  
    22  func (g *Generator) generateCallFallback(b *assembler.Builder) rookoutErrors.RookoutError {
    23  	return b.AddInstructions(
    24  		b.BranchToLabel(assembler.AJMP, "afterFallback"),
    25  		b.Label(fallbackLabel),
    26  		b.Inst(assembler.AMOVQ, x86asm.R13, assembler.Imm(g.fallbackAddr)),
    27  		b.BranchToReg(assembler.AJMP, x86asm.R13),
    28  		b.Label("afterFallback"),
    29  	)
    30  }
    31  
    32  func (g *Generator) getOriginalRegBackup() (regBackup []byte, regRestore []byte) {
    33  	for _, inst := range g.epilogueInstructions {
    34  		if !strings.HasPrefix(inst.Op.String(), "MOV") {
    35  			continue
    36  		}
    37  
    38  		instBytes := utils.MakeSliceFromPointer(inst.PC, inst.Len)
    39  		_, restore := inst.Args[0].(assembler.Reg)
    40  		if restore {
    41  			regRestore = append(regRestore, instBytes...)
    42  		} else {
    43  			regBackup = append(regBackup, instBytes...)
    44  		}
    45  	}
    46  	return regBackup, regRestore
    47  }
    48  
    49  
    50  func (g *Generator) generateCheckStackUsage(b *assembler.Builder) rookoutErrors.RookoutError {
    51  	return b.AddInstructions(
    52  		b.Label(startLabel),
    53  		common.MovGToR12(b),
    54  		b.Inst(assembler.AMOVQ, x86asm.R12, assembler.Mem{Base: x86asm.R12, Disp: common.StackguardOffset}),
    55  		b.Inst(assembler.ALEAQ, x86asm.R13, assembler.Mem{Base: x86asm.RSP, Disp: -int64(g.stackUsage + stackUsageBuffer)}),
    56  		b.Cmp(x86asm.R13, x86asm.R12),
    57  		b.BranchToLabel(assembler.AJHI, endLabel),
    58  	)
    59  }
    60  
    61  func (g *Generator) generateCallMorestack(b *assembler.Builder) rookoutErrors.RookoutError {
    62  	return b.AddInstructions(
    63  		b.Inst(assembler.AMOVQ, x86asm.R13, assembler.Imm(g.morestackAddr)),
    64  		b.BranchToReg(assembler.ACALL, x86asm.R13),
    65  	)
    66  }
    67  
    68  func (g *Generator) generateJumpToStart(b *assembler.Builder) rookoutErrors.RookoutError {
    69  	return b.AddInstructions(
    70  		b.BranchToLabel(assembler.AJMP, startLabel),
    71  		b.Label(endLabel),
    72  		b.PsuedoNop(),
    73  	)
    74  }