This repository has been archived by the owner on Feb 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
chaos.go
311 lines (245 loc) · 11.5 KB
/
chaos.go
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package chaos
import (
"context"
"math"
"math/rand"
"github.com/stealthrocket/wasi-go"
)
const (
maxChance = 1024 * 1024 * 1024
)
type Rule struct {
chance int64 // [0;maxChance]
system wasi.System
}
// Chance is a constructor for values of type Rule, mapping a system to a
// probably of it being picked when methods are invoked.
func Chance(chance float64, system wasi.System) Rule {
if chance < 0 || chance > 1 {
panic("invalid chance of chaos system rule is not in the range [0;1]")
}
return Rule{
chance: int64(math.Round(chance * maxChance)),
system: system,
}
}
// New constructs a new chaos system. The given random source is used for all
// random number generation. The base system is the fallback when a probability
// of method invocation did not match any of the rules. The list of rules pairs
// a probability of being selected when a method of the system is invoked to the
// system to delegate the invocation to. The systems set on rules are expected
// to be instances of wrappers created by functions of this package to perform
// fault injection.
func New(prng rand.Source, base wasi.System, rules ...Rule) wasi.System {
s := &system{
prng: prng,
base: base,
chances: make([]int64, len(rules)),
systems: make([]wasi.System, len(rules)),
}
cumulativeChance := int64(0)
for i, rule := range rules {
cumulativeChance += rule.chance
s.chances[i] = cumulativeChance
s.systems[i] = rule.system
if cumulativeChance < 0 || cumulativeChance > maxChance {
panic("cumulative chance of chaos system rules is greater than 1")
}
}
return s
}
type system struct {
prng rand.Source
base wasi.System
chances []int64
systems []wasi.System
}
func (s *system) system() wasi.System {
probability := s.prng.Int63() & (maxChance - 1)
for i, chance := range s.chances {
if chance > probability {
return s.systems[i]
}
}
return s.base
}
func (s *system) ArgsSizesGet(ctx context.Context) (int, int, wasi.Errno) {
return s.system().ArgsSizesGet(ctx)
}
func (s *system) ArgsGet(ctx context.Context) ([]string, wasi.Errno) {
return s.system().ArgsGet(ctx)
}
func (s *system) EnvironSizesGet(ctx context.Context) (int, int, wasi.Errno) {
return s.system().EnvironSizesGet(ctx)
}
func (s *system) EnvironGet(ctx context.Context) ([]string, wasi.Errno) {
return s.system().EnvironGet(ctx)
}
func (s *system) ClockResGet(ctx context.Context, id wasi.ClockID) (wasi.Timestamp, wasi.Errno) {
return s.system().ClockResGet(ctx, id)
}
func (s *system) ClockTimeGet(ctx context.Context, id wasi.ClockID, precision wasi.Timestamp) (wasi.Timestamp, wasi.Errno) {
return s.system().ClockTimeGet(ctx, id, precision)
}
func (s *system) FDAdvise(ctx context.Context, fd wasi.FD, offset, length wasi.FileSize, advice wasi.Advice) wasi.Errno {
return s.system().FDAdvise(ctx, fd, offset, length, advice)
}
func (s *system) FDAllocate(ctx context.Context, fd wasi.FD, offset, length wasi.FileSize) wasi.Errno {
return s.system().FDAllocate(ctx, fd, offset, length)
}
func (s *system) FDClose(ctx context.Context, fd wasi.FD) wasi.Errno {
return s.system().FDClose(ctx, fd)
}
func (s *system) FDDataSync(ctx context.Context, fd wasi.FD) wasi.Errno {
return s.system().FDDataSync(ctx, fd)
}
func (s *system) FDStatGet(ctx context.Context, fd wasi.FD) (wasi.FDStat, wasi.Errno) {
return s.system().FDStatGet(ctx, fd)
}
func (s *system) FDStatSetFlags(ctx context.Context, fd wasi.FD, flags wasi.FDFlags) wasi.Errno {
return s.system().FDStatSetFlags(ctx, fd, flags)
}
func (s *system) FDStatSetRights(ctx context.Context, fd wasi.FD, rightsBase, rightsInheriting wasi.Rights) wasi.Errno {
return s.system().FDStatSetRights(ctx, fd, rightsBase, rightsInheriting)
}
func (s *system) FDFileStatGet(ctx context.Context, fd wasi.FD) (wasi.FileStat, wasi.Errno) {
return s.system().FDFileStatGet(ctx, fd)
}
func (s *system) FDFileStatSetSize(ctx context.Context, fd wasi.FD, size wasi.FileSize) wasi.Errno {
return s.system().FDFileStatSetSize(ctx, fd, size)
}
func (s *system) FDFileStatSetTimes(ctx context.Context, fd wasi.FD, accessTime, modifyTime wasi.Timestamp, flags wasi.FSTFlags) wasi.Errno {
return s.system().FDFileStatSetTimes(ctx, fd, accessTime, modifyTime, flags)
}
func (s *system) FDPread(ctx context.Context, fd wasi.FD, iovecs []wasi.IOVec, offset wasi.FileSize) (wasi.Size, wasi.Errno) {
return s.system().FDPread(ctx, fd, iovecs, offset)
}
func (s *system) FDPreStatGet(ctx context.Context, fd wasi.FD) (wasi.PreStat, wasi.Errno) {
return s.system().FDPreStatGet(ctx, fd)
}
func (s *system) FDPreStatDirName(ctx context.Context, fd wasi.FD) (string, wasi.Errno) {
return s.system().FDPreStatDirName(ctx, fd)
}
func (s *system) FDPwrite(ctx context.Context, fd wasi.FD, iovecs []wasi.IOVec, offset wasi.FileSize) (wasi.Size, wasi.Errno) {
return s.system().FDPwrite(ctx, fd, iovecs, offset)
}
func (s *system) FDRead(ctx context.Context, fd wasi.FD, iovecs []wasi.IOVec) (wasi.Size, wasi.Errno) {
return s.system().FDRead(ctx, fd, iovecs)
}
func (s *system) FDReadDir(ctx context.Context, fd wasi.FD, entries []wasi.DirEntry, cookie wasi.DirCookie, bufferSizeBytes int) (int, wasi.Errno) {
return s.system().FDReadDir(ctx, fd, entries, cookie, bufferSizeBytes)
}
func (s *system) FDRenumber(ctx context.Context, from, to wasi.FD) wasi.Errno {
return s.system().FDRenumber(ctx, from, to)
}
func (s *system) FDSeek(ctx context.Context, fd wasi.FD, offset wasi.FileDelta, whence wasi.Whence) (wasi.FileSize, wasi.Errno) {
return s.system().FDSeek(ctx, fd, offset, whence)
}
func (s *system) FDSync(ctx context.Context, fd wasi.FD) wasi.Errno {
return s.system().FDSync(ctx, fd)
}
func (s *system) FDTell(ctx context.Context, fd wasi.FD) (wasi.FileSize, wasi.Errno) {
return s.system().FDTell(ctx, fd)
}
func (s *system) FDWrite(ctx context.Context, fd wasi.FD, iovecs []wasi.IOVec) (wasi.Size, wasi.Errno) {
return s.system().FDWrite(ctx, fd, iovecs)
}
func (s *system) PathCreateDirectory(ctx context.Context, fd wasi.FD, path string) wasi.Errno {
return s.system().PathCreateDirectory(ctx, fd, path)
}
func (s *system) PathFileStatGet(ctx context.Context, fd wasi.FD, lookupFlags wasi.LookupFlags, path string) (wasi.FileStat, wasi.Errno) {
return s.system().PathFileStatGet(ctx, fd, lookupFlags, path)
}
func (s *system) PathFileStatSetTimes(ctx context.Context, fd wasi.FD, lookupFlags wasi.LookupFlags, path string, accessTime, modifyTime wasi.Timestamp, flags wasi.FSTFlags) wasi.Errno {
return s.system().PathFileStatSetTimes(ctx, fd, lookupFlags, path, accessTime, modifyTime, flags)
}
func (s *system) PathLink(ctx context.Context, oldFD wasi.FD, oldFlags wasi.LookupFlags, oldPath string, newFD wasi.FD, newPath string) wasi.Errno {
return s.system().PathLink(ctx, oldFD, oldFlags, oldPath, newFD, newPath)
}
func (s *system) PathOpen(ctx context.Context, fd wasi.FD, dirFlags wasi.LookupFlags, path string, openFlags wasi.OpenFlags, rightsBase, rightsInheriting wasi.Rights, fdFlags wasi.FDFlags) (wasi.FD, wasi.Errno) {
return s.system().PathOpen(ctx, fd, dirFlags, path, openFlags, rightsBase, rightsInheriting, fdFlags)
}
func (s *system) PathReadLink(ctx context.Context, fd wasi.FD, path string, buffer []byte) (int, wasi.Errno) {
return s.system().PathReadLink(ctx, fd, path, buffer)
}
func (s *system) PathRemoveDirectory(ctx context.Context, fd wasi.FD, path string) wasi.Errno {
return s.system().PathRemoveDirectory(ctx, fd, path)
}
func (s *system) PathRename(ctx context.Context, fd wasi.FD, oldPath string, newFD wasi.FD, newPath string) wasi.Errno {
return s.system().PathRename(ctx, fd, oldPath, newFD, newPath)
}
func (s *system) PathSymlink(ctx context.Context, oldPath string, fd wasi.FD, newPath string) wasi.Errno {
return s.system().PathSymlink(ctx, oldPath, fd, newPath)
}
func (s *system) PathUnlinkFile(ctx context.Context, fd wasi.FD, path string) wasi.Errno {
return s.system().PathUnlinkFile(ctx, fd, path)
}
func (s *system) PollOneOff(ctx context.Context, subscriptions []wasi.Subscription, events []wasi.Event) (int, wasi.Errno) {
// We don't generate an error from PollOneOff itself because it usually
// results in crashing the application runtime. Instead, we injected errors
// on the collected events.
return s.system().PollOneOff(ctx, subscriptions, events)
}
func (s *system) ProcExit(ctx context.Context, exitCode wasi.ExitCode) wasi.Errno {
return s.system().ProcExit(ctx, exitCode)
}
func (s *system) ProcRaise(ctx context.Context, signal wasi.Signal) wasi.Errno {
return s.system().ProcRaise(ctx, signal)
}
func (s *system) SchedYield(ctx context.Context) wasi.Errno {
return s.system().SchedYield(ctx)
}
func (s *system) RandomGet(ctx context.Context, b []byte) wasi.Errno {
return s.system().RandomGet(ctx, b)
}
func (s *system) SockAccept(ctx context.Context, fd wasi.FD, flags wasi.FDFlags) (wasi.FD, wasi.SocketAddress, wasi.SocketAddress, wasi.Errno) {
return s.system().SockAccept(ctx, fd, flags)
}
func (s *system) SockShutdown(ctx context.Context, fd wasi.FD, flags wasi.SDFlags) wasi.Errno {
return s.system().SockShutdown(ctx, fd, flags)
}
func (s *system) SockRecv(ctx context.Context, fd wasi.FD, iovecs []wasi.IOVec, iflags wasi.RIFlags) (wasi.Size, wasi.ROFlags, wasi.Errno) {
return s.system().SockRecv(ctx, fd, iovecs, iflags)
}
func (s *system) SockSend(ctx context.Context, fd wasi.FD, iovecs []wasi.IOVec, iflags wasi.SIFlags) (wasi.Size, wasi.Errno) {
return s.system().SockSend(ctx, fd, iovecs, iflags)
}
func (s *system) SockOpen(ctx context.Context, pf wasi.ProtocolFamily, socketType wasi.SocketType, protocol wasi.Protocol, rightsBase, rightsInheriting wasi.Rights) (wasi.FD, wasi.Errno) {
return s.system().SockOpen(ctx, pf, socketType, protocol, rightsBase, rightsInheriting)
}
func (s *system) SockBind(ctx context.Context, fd wasi.FD, addr wasi.SocketAddress) (wasi.SocketAddress, wasi.Errno) {
return s.system().SockBind(ctx, fd, addr)
}
func (s *system) SockConnect(ctx context.Context, fd wasi.FD, peer wasi.SocketAddress) (wasi.SocketAddress, wasi.Errno) {
return s.system().SockConnect(ctx, fd, peer)
}
func (s *system) SockListen(ctx context.Context, fd wasi.FD, backlog int) wasi.Errno {
return s.system().SockListen(ctx, fd, backlog)
}
func (s *system) SockSendTo(ctx context.Context, fd wasi.FD, iovecs []wasi.IOVec, iflags wasi.SIFlags, addr wasi.SocketAddress) (wasi.Size, wasi.Errno) {
return s.system().SockSendTo(ctx, fd, iovecs, iflags, addr)
}
func (s *system) SockRecvFrom(ctx context.Context, fd wasi.FD, iovecs []wasi.IOVec, iflags wasi.RIFlags) (wasi.Size, wasi.ROFlags, wasi.SocketAddress, wasi.Errno) {
return s.system().SockRecvFrom(ctx, fd, iovecs, iflags)
}
func (s *system) SockGetOpt(ctx context.Context, fd wasi.FD, option wasi.SocketOption) (wasi.SocketOptionValue, wasi.Errno) {
return s.system().SockGetOpt(ctx, fd, option)
}
func (s *system) SockSetOpt(ctx context.Context, fd wasi.FD, option wasi.SocketOption, value wasi.SocketOptionValue) wasi.Errno {
return s.system().SockSetOpt(ctx, fd, option, value)
}
func (s *system) SockLocalAddress(ctx context.Context, fd wasi.FD) (wasi.SocketAddress, wasi.Errno) {
return s.system().SockLocalAddress(ctx, fd)
}
func (s *system) SockRemoteAddress(ctx context.Context, fd wasi.FD) (wasi.SocketAddress, wasi.Errno) {
return s.system().SockRemoteAddress(ctx, fd)
}
func (s *system) SockAddressInfo(ctx context.Context, name, service string, hints wasi.AddressInfo, results []wasi.AddressInfo) (int, wasi.Errno) {
return s.system().SockAddressInfo(ctx, name, service, hints, results)
}
func (s *system) Close(ctx context.Context) error {
for _, system := range s.systems {
_ = system.Close(ctx)
}
return s.base.Close(ctx)
}