-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecution_control.go
More file actions
282 lines (255 loc) · 7.73 KB
/
Copy pathexecution_control.go
File metadata and controls
282 lines (255 loc) · 7.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package ember
import (
"context"
"fmt"
)
const maxInt64Uint = uint64(1<<63 - 1)
const executionPollInterval uint32 = 256
// executionWindow keeps the hot-loop safety state local to the active direct
// loop. The controller remains the shared ownership boundary; the window
// commits charged state whenever the loop yields control.
type executionWindow struct {
controller *executionController
remaining int64
pollLeft uint32
}
func newExecutionWindow(controller *executionController) executionWindow {
window := executionWindow{controller: controller, remaining: -1}
if controller != nil {
window.remaining = controller.remaining
if controller.onStep != nil {
onStep := controller.onStep
controller.onStep = nil
onStep()
}
}
return window
}
func (window *executionWindow) stepInstruction() error {
if window == nil || window.controller == nil {
return nil
}
controller := window.controller
if window.pollLeft == 0 {
window.pollLeft = executionPollInterval
if err := controller.checkContext(); err != nil {
return err
}
}
if window.remaining >= 0 {
if window.remaining == 0 {
return window.limitError(1)
}
window.remaining--
}
window.pollLeft--
return nil
}
func (window *executionWindow) limitError(count uint64) error {
if window == nil || window.controller == nil {
return nil
}
controller := window.controller
used := controller.limits.MaxInstructions - uint64(window.remaining)
if count > ^uint64(0)-used {
used = ^uint64(0)
} else {
used += count
}
return &LimitError{Kind: LimitInstructions, Limit: controller.limits.MaxInstructions, Used: used}
}
func (window *executionWindow) flush() {
if window == nil || window.controller == nil || window.remaining < 0 {
return
}
window.controller.remaining = window.remaining
}
func (window *executionWindow) commit() {
if window == nil {
return
}
window.flush()
}
func (window *executionWindow) refresh() {
if window == nil || window.controller == nil {
return
}
window.remaining = window.controller.remaining
window.pollLeft = executionPollInterval
}
type executionController struct {
ctx context.Context
limits ExecutionLimits
remaining int64
onStep func()
callDepth uint32
moduleInitializations uint32
generatedStringBytes uint64
runtimeObjects uint64
inheritedScriptFrames []ScriptFrame
}
func (controller *executionController) pushInheritedScriptFrames(frames []ScriptFrame) func() {
if controller == nil {
return func() {}
}
previous := controller.inheritedScriptFrames
controller.inheritedScriptFrames = append([]ScriptFrame(nil), frames...)
return func() {
controller.inheritedScriptFrames = previous
}
}
func (controller *executionController) enterCall() error {
if controller == nil {
return nil
}
if controller.limits.MaxCallDepth != 0 && controller.callDepth >= controller.limits.MaxCallDepth {
return &LimitError{Kind: LimitCallDepth, Limit: uint64(controller.limits.MaxCallDepth), Used: uint64(controller.callDepth) + 1}
}
controller.callDepth++
return nil
}
func (controller *executionController) enterCalls(count uint32) error {
if controller == nil || count == 0 {
return nil
}
if controller.limits.MaxCallDepth != 0 && (controller.callDepth > controller.limits.MaxCallDepth || count > controller.limits.MaxCallDepth-controller.callDepth) {
return &LimitError{Kind: LimitCallDepth, Limit: uint64(controller.limits.MaxCallDepth), Used: uint64(controller.callDepth) + uint64(count)}
}
controller.callDepth += count
return nil
}
func (controller *executionController) leaveCall() {
if controller != nil && controller.callDepth > 0 {
controller.callDepth--
}
}
func (controller *executionController) chargeModuleInitialization() error {
if controller == nil {
return nil
}
if controller.limits.MaxModuleInitializations != 0 && controller.moduleInitializations >= controller.limits.MaxModuleInitializations {
return &LimitError{Kind: LimitModuleInitializations, Limit: uint64(controller.limits.MaxModuleInitializations), Used: uint64(controller.moduleInitializations) + 1}
}
controller.moduleInitializations++
return nil
}
func (controller *executionController) chargeGeneratedStringBytes(count uint64) error {
if controller == nil || count == 0 {
return nil
}
used := controller.generatedStringBytes
if count > ^uint64(0)-used {
used = ^uint64(0)
} else {
used += count
}
if controller.limits.MaxGeneratedStringBytes != 0 && used > controller.limits.MaxGeneratedStringBytes {
return &LimitError{Kind: LimitGeneratedStringBytes, Limit: controller.limits.MaxGeneratedStringBytes, Used: used}
}
controller.generatedStringBytes = used
return nil
}
func (controller *executionController) chargeRuntimeObject() error {
return controller.chargeRuntimeObjects(1)
}
func (controller *executionController) chargeRuntimeObjects(count uint64) error {
if controller == nil {
return nil
}
if count == 0 {
return nil
}
used := controller.runtimeObjects
if count > ^uint64(0)-used {
used = ^uint64(0)
} else {
used += count
}
if controller.limits.MaxRuntimeObjects != 0 && used > controller.limits.MaxRuntimeObjects {
return &LimitError{Kind: LimitRuntimeObjects, Limit: controller.limits.MaxRuntimeObjects, Used: used}
}
controller.runtimeObjects = used
return nil
}
func (controller *executionController) releaseRuntimeObjects(count uint64) {
if controller != nil && count <= controller.runtimeObjects {
controller.runtimeObjects -= count
}
}
func newExecutionController(ctx context.Context, limits ExecutionLimits) (*executionController, error) {
if ctx == nil {
ctx = context.Background()
}
normalized, err := normalizeExecutionLimits(0, limits)
if err != nil {
return nil, err
}
remaining, err := executionControllerRemaining(normalized)
if err != nil {
return nil, err
}
return &executionController{
ctx: ctx,
limits: normalized,
remaining: remaining,
}, nil
}
// newExecutionPolicy returns the invocation controller only when the call
// needs execution policy enforcement. An unlimited, non-cancelable context
// has no policy state to carry, so the nil controller is the unrestricted
// execution mode used by the direct VM loops.
func newExecutionPolicy(ctx context.Context, limits ExecutionLimits) (*executionController, error) {
if ctx == nil {
ctx = context.Background()
}
if limits == (ExecutionLimits{}) && ctx.Done() == nil && ctx.Err() == nil {
return nil, nil
}
return newExecutionController(ctx, limits)
}
func validateExecutionLimits(limits ExecutionLimits) error {
_, err := executionControllerRemaining(limits)
return err
}
func executionControllerRemaining(limits ExecutionLimits) (int64, error) {
if limits.MaxInstructions == 0 {
return -1, nil
}
if limits.MaxInstructions > maxInt64Uint {
return 0, fmt.Errorf("execution controller: max instructions %d exceeds int64", limits.MaxInstructions)
}
return int64(limits.MaxInstructions), nil
}
func (controller *executionController) checkContext() error {
if controller == nil || controller.ctx == nil {
return nil
}
return controller.ctx.Err()
}
func (controller *executionController) chargeInstructions(count uint64) error {
if controller == nil || controller.remaining < 0 || count == 0 {
return nil
}
if count <= uint64(controller.remaining) {
controller.remaining -= int64(count)
return nil
}
used := controller.limits.MaxInstructions - uint64(controller.remaining)
if count > ^uint64(0)-used {
used = ^uint64(0)
} else {
used += count
}
controller.remaining = 0
return &LimitError{
Kind: LimitInstructions,
Limit: controller.limits.MaxInstructions,
Used: used,
}
}
func (controller *executionController) configuredLimits() ExecutionLimits {
if controller == nil {
return ExecutionLimits{}
}
return controller.limits
}