Debian Codegraph @

internal/engine/wazevo/backend/isa/amd64/stack.go

raw
1 package amd64
2
3 import (
4 "encoding/binary"
5 "reflect"
6 "unsafe"
7
8 "github.com/tetratelabs/wazero/internal/wasmdebug"
9 )
10
11 func stackView(rbp, top uintptr) []byte {
12 l := int(top - rbp)
13 var stackBuf []byte
14 {
15 //nolint:staticcheck
16 hdr := (*reflect.SliceHeader)(unsafe.Pointer(&stackBuf))
17 hdr.Data = rbp
18 hdr.Len = l
19 hdr.Cap = l
20 }
21 return stackBuf
22 }
23
24 // UnwindStack implements wazevo.unwindStack.
25 func UnwindStack(_, rbp, top uintptr, returnAddresses []uintptr) []uintptr {
26 stackBuf := stackView(rbp, top)
27
28 for i := uint64(0); i < uint64(len(stackBuf)); {
29 // (high address)
30 // +-----------------+
31 // | ....... |
32 // | ret Y |
33 // | ....... |
34 // | ret 0 |
35 // | arg X |
36 // | ....... |
37 // | arg 1 |
38 // | arg 0 |
39 // | ReturnAddress |
40 // | Caller_RBP |
41 // +-----------------+ <---- Caller_RBP
42 // | ........... |
43 // | clobbered M |
44 // | ............ |
45 // | clobbered 0 |
46 // | spill slot N |
47 // | ............ |
48 // | spill slot 0 |
49 // | ReturnAddress |
50 // | Caller_RBP |
51 // +-----------------+ <---- RBP
52 // (low address)
53
54 callerRBP := binary.LittleEndian.Uint64(stackBuf[i:])
55 retAddr := binary.LittleEndian.Uint64(stackBuf[i+8:])
56 returnAddresses = append(returnAddresses, uintptr(retAddr))
57 i = callerRBP - uint64(rbp)
58 if len(returnAddresses) == wasmdebug.MaxFrames {
59 break
60 }
61 }
62 return returnAddresses
63 }
64
65 // GoCallStackView implements wazevo.goCallStackView.
66 func GoCallStackView(stackPointerBeforeGoCall *uint64) []uint64 {
67 // (high address)
68 // +-----------------+ <----+
69 // | xxxxxxxxxxx | | ;; optional unused space to make it 16-byte aligned.
70 // ^ | arg[N]/ret[M] | |
71 // sliceSize | | ............ | | SizeInBytes/8
72 // | | arg[1]/ret[1] | |
73 // v | arg[0]/ret[0] | <----+
74 // | SizeInBytes |
75 // +-----------------+ <---- stackPointerBeforeGoCall
76 // (low address)
77 data := unsafe.Add(unsafe.Pointer(stackPointerBeforeGoCall), 8)
78 size := *stackPointerBeforeGoCall / 8
79 return unsafe.Slice((*uint64)(data), size)
80 }
81
82 func AdjustClonedStack(oldRsp, oldTop, rsp, rbp, top uintptr) {
83 diff := uint64(rsp - oldRsp)
84
85 newBuf := stackView(rbp, top)
86 for i := uint64(0); i < uint64(len(newBuf)); {
87 // (high address)
88 // +-----------------+
89 // | ....... |
90 // | ret Y |
91 // | ....... |
92 // | ret 0 |
93 // | arg X |
94 // | ....... |
95 // | arg 1 |
96 // | arg 0 |
97 // | ReturnAddress |
98 // | Caller_RBP |
99 // +-----------------+ <---- Caller_RBP
100 // | ........... |
101 // | clobbered M |
102 // | ............ |
103 // | clobbered 0 |
104 // | spill slot N |
105 // | ............ |
106 // | spill slot 0 |
107 // | ReturnAddress |
108 // | Caller_RBP |
109 // +-----------------+ <---- RBP
110 // (low address)
111
112 callerRBP := binary.LittleEndian.Uint64(newBuf[i:])
113 if callerRBP == 0 {
114 // End of stack.
115 break
116 }
117 if i64 := int64(callerRBP); i64 < int64(oldRsp) || i64 >= int64(oldTop) {
118 panic("BUG: callerRBP is out of range")
119 }
120 if int(callerRBP) < 0 {
121 panic("BUG: callerRBP is negative")
122 }
123 adjustedCallerRBP := callerRBP + diff
124 if int(adjustedCallerRBP) < 0 {
125 panic("BUG: adjustedCallerRBP is negative")
126 }
127 binary.LittleEndian.PutUint64(newBuf[i:], adjustedCallerRBP)
128 i = adjustedCallerRBP - uint64(rbp)
129 }
130 }