aboutsummaryrefslogtreecommitdiffstats
path: root/package/lua/patches/300-opcode_performance.patch
blob: f6204ae19bb93436774a452e05d5e35dc773b13b (plain)
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
--- a/src/lvm.c
+++ b/src/lvm.c
@@ -31,6 +31,9 @@
 /* limit for table tag-method chains (to avoid loops) */
 #define MAXTAGLOOP	100
 
+#ifdef __GNUC__
+#define COMPUTED_GOTO 1
+#endif
 
 /*
  * If 'obj' is a string, it is tried to be interpreted as a number.
@@ -562,12 +565,63 @@
     ARITH_OP1_END
 #endif
 
+#ifdef COMPUTED_GOTO
+#define OPCODE_TARGET(op) DO_OP_##op:
+#define CALL_OPCODE(op) goto *opcodes[op];
+#define OPCODE_PTR(op) [OP_##op] = &&DO_OP_##op
+#else
+#define OPCODE_TARGET(op) case OP_##op:
+#define CALL_OPCODE(op) switch (op)
+#endif
+
 
 void luaV_execute (lua_State *L, int nexeccalls) {
   LClosure *cl;
   StkId base;
   TValue *k;
   const Instruction *pc;
+#ifdef COMPUTED_GOTO
+  static const void *opcodes[] = {
+   OPCODE_PTR(MOVE),
+   OPCODE_PTR(LOADK),
+   OPCODE_PTR(LOADBOOL),
+   OPCODE_PTR(LOADNIL),
+   OPCODE_PTR(GETUPVAL),
+   OPCODE_PTR(GETGLOBAL),
+   OPCODE_PTR(GETTABLE),
+   OPCODE_PTR(SETGLOBAL),
+   OPCODE_PTR(SETUPVAL),
+   OPCODE_PTR(SETTABLE),
+   OPCODE_PTR(NEWTABLE),
+   OPCODE_PTR(SELF),
+   OPCODE_PTR(ADD),
+   OPCODE_PTR(SUB),
+   OPCODE_PTR(MUL),
+   OPCODE_PTR(DIV),
+   OPCODE_PTR(MOD),
+   OPCODE_PTR(POW),
+   OPCODE_PTR(UNM),
+   OPCODE_PTR(NOT),
+   OPCODE_PTR(LEN),
+   OPCODE_PTR(CONCAT),
+   OPCODE_PTR(JMP),
+   OPCODE_PTR(EQ),
+   OPCODE_PTR(LT),
+   OPCODE_PTR(LE),
+   OPCODE_PTR(TEST),
+   OPCODE_PTR(TESTSET),
+   OPCODE_PTR(CALL),
+   OPCODE_PTR(TAILCALL),
+   OPCODE_PTR(RETURN),
+   OPCODE_PTR(FORLOOP),
+   OPCODE_PTR(FORPREP),
+   OPCODE_PTR(TFORLOOP),
+   OPCODE_PTR(SETLIST),
+   OPCODE_PTR(CLOSE),
+   OPCODE_PTR(CLOSURE),
+   OPCODE_PTR(VARARG)
+  };
+#endif
  reentry:  /* entry point */
   lua_assert(isLua(L->ci));
   pc = L->savedpc;
@@ -592,33 +646,33 @@
     lua_assert(base == L->base && L->base == L->ci->base);
     lua_assert(base <= L->top && L->top <= L->stack + L->stacksize);
     lua_assert(L->top == L->ci->top || luaG_checkopenop(i));
-    switch (GET_OPCODE(i)) {
-      case OP_MOVE: {
+    CALL_OPCODE(GET_OPCODE(i)) {
+      OPCODE_TARGET(MOVE) {
         setobjs2s(L, ra, RB(i));
         continue;
       }
-      case OP_LOADK: {
+      OPCODE_TARGET(LOADK) {
         setobj2s(L, ra, KBx(i));
         continue;
       }
-      case OP_LOADBOOL: {
+      OPCODE_TARGET(LOADBOOL) {
         setbvalue(ra, GETARG_B(i));
         if (GETARG_C(i)) pc++;  /* skip next instruction (if C) */
         continue;
       }
-      case OP_LOADNIL: {
+      OPCODE_TARGET(LOADNIL) {
         TValue *rb = RB(i);
         do {
           setnilvalue(rb--);
         } while (rb >= ra);
         continue;
       }
-      case OP_GETUPVAL: {
+      OPCODE_TARGET(GETUPVAL) {
         int b = GETARG_B(i);
         setobj2s(L, ra, cl->upvals[b]->v);
         continue;
       }
-      case OP_GETGLOBAL: {
+      OPCODE_TARGET(GETGLOBAL) {
         TValue g;
         TValue *rb = KBx(i);
         sethvalue(L, &g, cl->env);
@@ -626,88 +680,88 @@
         Protect(luaV_gettable(L, &g, rb, ra));
         continue;
       }
-      case OP_GETTABLE: {
+      OPCODE_TARGET(GETTABLE) {
         Protect(luaV_gettable(L, RB(i), RKC(i), ra));
         continue;
       }
-      case OP_SETGLOBAL: {
+      OPCODE_TARGET(SETGLOBAL) {
         TValue g;
         sethvalue(L, &g, cl->env);
         lua_assert(ttisstring(KBx(i)));
         Protect(luaV_settable(L, &g, KBx(i), ra));
         continue;
       }
-      case OP_SETUPVAL: {
+      OPCODE_TARGET(SETUPVAL) {
         UpVal *uv = cl->upvals[GETARG_B(i)];
         setobj(L, uv->v, ra);
         luaC_barrier(L, uv, ra);
         continue;
       }
-      case OP_SETTABLE: {
+      OPCODE_TARGET(SETTABLE) {
         Protect(luaV_settable(L, ra, RKB(i), RKC(i)));
         continue;
       }
-      case OP_NEWTABLE: {
+      OPCODE_TARGET(NEWTABLE) {
         int b = GETARG_B(i);
         int c = GETARG_C(i);
         sethvalue(L, ra, luaH_new(L, luaO_fb2int(b), luaO_fb2int(c)));
         Protect(luaC_checkGC(L));
         continue;
       }
-      case OP_SELF: {
+      OPCODE_TARGET(SELF) {
         StkId rb = RB(i);
         setobjs2s(L, ra+1, rb);
         Protect(luaV_gettable(L, rb, RKC(i), ra));
         continue;
       }
-      case OP_ADD: {
+      OPCODE_TARGET(ADD) {
         TValue *rb = RKB(i), *rc= RKC(i);
         arith_op_continue( luai_numadd, try_addint, luai_vectadd );
         Protect(Arith(L, ra, rb, rc, TM_ADD)); \
         continue;
       }
-      case OP_SUB: {
+      OPCODE_TARGET(SUB) {
         TValue *rb = RKB(i), *rc= RKC(i);
         arith_op_continue( luai_numsub, try_subint, luai_vectsub );
         Protect(Arith(L, ra, rb, rc, TM_SUB));
         continue;
       }
-      case OP_MUL: {
+      OPCODE_TARGET(MUL) {
         TValue *rb = RKB(i), *rc= RKC(i);
         arith_op_continue(luai_nummul, try_mulint, luai_vectmul);
         Protect(Arith(L, ra, rb, rc, TM_MUL));
         continue;
       }
-      case OP_DIV: {
+      OPCODE_TARGET(DIV) {
         TValue *rb = RKB(i), *rc= RKC(i);
         arith_op_continue(luai_numdiv, try_divint, luai_vectdiv);
         Protect(Arith(L, ra, rb, rc, TM_DIV));
         continue;
       }
-      case OP_MOD: {
+      OPCODE_TARGET(MOD) {
         TValue *rb = RKB(i), *rc= RKC(i);
         arith_op_continue_scalar(luai_nummod, try_modint);  /* scalars only */
         Protect(Arith(L, ra, rb, rc, TM_MOD));
         continue;
       }
-      case OP_POW: {
+      OPCODE_TARGET(POW) {
         TValue *rb = RKB(i), *rc= RKC(i);
         arith_op_continue(luai_numpow, try_powint, luai_vectpow);
         Protect(Arith(L, ra, rb, rc, TM_POW));
         continue;
       }
-      case OP_UNM: {
+      OPCODE_TARGET(UNM) {
         TValue *rb = RB(i);
         arith_op1_continue(luai_numunm, try_unmint, luai_vectunm);
         Protect(Arith(L, ra, rb, rb, TM_UNM));
         continue;
       }
-      case OP_NOT: {
+      OPCODE_TARGET(NOT) {
         int res = l_isfalse(RB(i));  /* next assignment may change this value */
         setbvalue(ra, res);
         continue;
       }
-      case OP_LEN: {
+      OPCODE_TARGET(LEN) {
         const TValue *rb = RB(i);
         switch (ttype(rb)) {
           case LUA_TTABLE: {
@@ -727,18 +781,18 @@
         }
         continue;
       }
-      case OP_CONCAT: {
+      OPCODE_TARGET(CONCAT) {
         int b = GETARG_B(i);
         int c = GETARG_C(i);
         Protect(luaV_concat(L, c-b+1, c); luaC_checkGC(L));
         setobjs2s(L, RA(i), base+b);
         continue;
       }
-      case OP_JMP: {
+      OPCODE_TARGET(JMP) {
         dojump(L, pc, GETARG_sBx(i));
         continue;
       }
-      case OP_EQ: {
+      OPCODE_TARGET(EQ) {
         TValue *rb = RKB(i);
         TValue *rc = RKC(i);
         Protect(
@@ -748,7 +802,7 @@
         pc++;
         continue;
       }
-      case OP_LT: {
+      OPCODE_TARGET(LT) {
         Protect(
           if (luaV_lessthan(L, RKB(i), RKC(i)) == GETARG_A(i))
             dojump(L, pc, GETARG_sBx(*pc));
@@ -756,7 +810,7 @@
         pc++;
         continue;
       }
-      case OP_LE: {
+      OPCODE_TARGET(LE) {
         Protect(
           if (lessequal(L, RKB(i), RKC(i)) == GETARG_A(i))
             dojump(L, pc, GETARG_sBx(*pc));
@@ -764,13 +818,13 @@
         pc++;
         continue;
       }
-      case OP_TEST: {
+      OPCODE_TARGET(TEST) {
         if (l_isfalse(ra) != GETARG_C(i))
           dojump(L, pc, GETARG_sBx(*pc));
         pc++;
         continue;
       }
-      case OP_TESTSET: {
+      OPCODE_TARGET(TESTSET) {
         TValue *rb = RB(i);
         if (l_isfalse(rb) != GETARG_C(i)) {
           setobjs2s(L, ra, rb);
@@ -779,7 +833,7 @@
         pc++;
         continue;
       }
-      case OP_CALL: {
+      OPCODE_TARGET(CALL) {
         int b = GETARG_B(i);
         int nresults = GETARG_C(i) - 1;
         if (b != 0) L->top = ra+b;  /* else previous instruction set top */
@@ -800,7 +854,7 @@
           }
         }
       }
-      case OP_TAILCALL: {
+      OPCODE_TARGET(TAILCALL) {
         int b = GETARG_B(i);
         if (b != 0) L->top = ra+b;  /* else previous instruction set top */
         L->savedpc = pc;
@@ -832,7 +886,7 @@
           }
         }
       }
-      case OP_RETURN: {
+      OPCODE_TARGET(RETURN) {
         int b = GETARG_B(i);
         if (b != 0) L->top = ra+b-1;
         if (L->openupval) luaF_close(L, base);
@@ -847,7 +901,7 @@
           goto reentry;
         }
       }
-      case OP_FORLOOP: {
+      OPCODE_TARGET(FORLOOP) {
         /* If start,step and limit are all integers, we don't need to check
          * against overflow in the looping.
          */
@@ -875,7 +929,7 @@
         }
         continue;
       }
-      case OP_FORPREP: {
+      OPCODE_TARGET(FORPREP) {
         const TValue *init = ra;
         const TValue *plimit = ra+1;
         const TValue *pstep = ra+2;
@@ -898,7 +952,7 @@
         dojump(L, pc, GETARG_sBx(i));
         continue;
       }
-      case OP_TFORLOOP: {
+      OPCODE_TARGET(TFORLOOP) {
         StkId cb = ra + 3;  /* call base */
         setobjs2s(L, cb+2, ra+2);
         setobjs2s(L, cb+1, ra+1);
@@ -914,7 +968,7 @@
         pc++;
         continue;
       }
-      case OP_SETLIST: {
+      OPCODE_TARGET(SETLIST) {
         int n = GETARG_B(i);
         int c = GETARG_C(i);
         int last;
@@ -936,11 +990,11 @@
         }
         continue;
       }
-      case OP_CLOSE: {
+      OPCODE_TARGET(CLOSE) {
         luaF_close(L, ra);
         continue;
       }
-      case OP_CLOSURE: {
+      OPCODE_TARGET(CLOSURE) {
         Proto *p;
         Closure *ncl;
         int nup, j;
@@ -960,7 +1014,7 @@
         Protect(luaC_checkGC(L));
         continue;
       }
-      case OP_VARARG: {
+      OPCODE_TARGET(VARARG) {
         int b = GETARG_B(i) - 1;
         int j;
         CallInfo *ci = L->ci;
ss="p">) == s._hash(r2) tctx.configure(s, server_replay_ignore_content=True) r = tflow.tflow(resp=True) r2 = tflow.tflow(resp=True) r.request.content = b"foo" r2.request.content = b"foo" assert s._hash(r) == s._hash(r2) r2.request.content = b"bar" assert s._hash(r) == s._hash(r2) r2.request.content = b"" assert s._hash(r) == s._hash(r2) r2.request.content = None assert s._hash(r) == s._hash(r2) def test_ignore_content_wins_over_params(): s = serverplayback.ServerPlayback() with taddons.context(s) as tctx: tctx.configure( s, server_replay_ignore_content=True, server_replay_ignore_payload_params=[ "param1", "param2" ] ) # NOTE: parameters are mutually exclusive in options r = tflow.tflow(resp=True) r.request.headers["Content-Type"] = "application/x-www-form-urlencoded" r.request.content = b"paramx=y" r2 = tflow.tflow(resp=True) r2.request.headers["Content-Type"] = "application/x-www-form-urlencoded" r2.request.content = b"paramx=x" # same parameters assert s._hash(r) == s._hash(r2) def test_ignore_payload_params_other_content_type(): s = serverplayback.ServerPlayback() with taddons.context(s) as tctx: tctx.configure( s, server_replay_ignore_content=False, server_replay_ignore_payload_params=[ "param1", "param2" ] ) r = tflow.tflow(resp=True) r.request.headers["Content-Type"] = "application/json" r.request.content = b'{"param1":"1"}' r2 = tflow.tflow(resp=True) r2.request.headers["Content-Type"] = "application/json" r2.request.content = b'{"param1":"1"}' # same content assert s._hash(r) == s._hash(r2) # distint content (note only x-www-form-urlencoded payload is analysed) r2.request.content = b'{"param1":"2"}' assert not s._hash(r) == s._hash(r2) def test_hash(): s = serverplayback.ServerPlayback() with taddons.context(s) as tctx: tctx.configure(s) r = tflow.tflow() r2 = tflow.tflow() assert s._hash(r) assert s._hash(r) == s._hash(r2) r.request.headers["foo"] = "bar" assert s._hash(r) == s._hash(r2) r.request.path = "voing" assert s._hash(r) != s._hash(r2) r.request.path = "path?blank_value" r2.request.path = "path?" assert s._hash(r) != s._hash(r2) def test_headers(): s = serverplayback.ServerPlayback() with taddons.context(s) as tctx: tctx.configure(s, server_replay_use_headers=["foo"]) r = tflow.tflow(resp=True) r.request.headers["foo"] = "bar" r2 = tflow.tflow(resp=True) assert not s._hash(r) == s._hash(r2) r2.request.headers["foo"] = "bar" assert s._hash(r) == s._hash(r2) r2.request.headers["oink"] = "bar" assert s._hash(r) == s._hash(r2) r = tflow.tflow(resp=True) r2 = tflow.tflow(resp=True) assert s._hash(r) == s._hash(r2) def test_load(): s = serverplayback.ServerPlayback() with taddons.context(s) as tctx: tctx.configure(s) r = tflow.tflow(resp=True) r.request.headers["key"] = "one" r2 = tflow.tflow(resp=True) r2.request.headers["key"] = "two" s.load_flows([r, r2]) assert s.count() == 2 n = s.next_flow(r) assert n.request.headers["key"] == "one" assert s.count() == 1 n = s.next_flow(r) assert n.request.headers["key"] == "two" assert not s.flowmap assert s.count() == 0 assert not s.next_flow(r) def test_load_with_server_replay_nopop(): s = serverplayback.ServerPlayback() with taddons.context(s) as tctx: tctx.configure(s, server_replay_nopop=True) r = tflow.tflow(resp=True) r.request.headers["key"] = "one" r2 = tflow.tflow(resp=True) r2.request.headers["key"] = "two" s.load_flows([r, r2]) assert s.count() == 2 s.next_flow(r) assert s.count() == 2 def test_ignore_params(): s = serverplayback.ServerPlayback() with taddons.context(s) as tctx: tctx.configure( s, server_replay_ignore_params=["param1", "param2"] ) r = tflow.tflow(resp=True) r.request.path = "/test?param1=1" r2 = tflow.tflow(resp=True) r2.request.path = "/test" assert s._hash(r) == s._hash(r2) r2.request.path = "/test?param1=2" assert s._hash(r) == s._hash(r2) r2.request.path = "/test?param2=1" assert s._hash(r) == s._hash(r2) r2.request.path = "/test?param3=2" assert not s._hash(r) == s._hash(r2) def thash(r, r2, setter): s = serverplayback.ServerPlayback() with taddons.context(s) as tctx: s = serverplayback.ServerPlayback() tctx.configure( s, server_replay_ignore_payload_params=["param1", "param2"] ) setter(r, paramx="x", param1="1") setter(r2, paramx="x", param1="1") # same parameters assert s._hash(r) == s._hash(r2) # ignored parameters != setter(r2, paramx="x", param1="2") assert s._hash(r) == s._hash(r2) # missing parameter setter(r2, paramx="x") assert s._hash(r) == s._hash(r2) # ignorable parameter added setter(r2, paramx="x", param1="2") assert s._hash(r) == s._hash(r2) # not ignorable parameter changed setter(r2, paramx="y", param1="1") assert not s._hash(r) == s._hash(r2) # not ignorable parameter missing setter(r2, param1="1") r2.request.content = b"param1=1" assert not s._hash(r) == s._hash(r2) def test_ignore_payload_params(): def urlencode_setter(r, **kwargs): r.request.content = urllib.parse.urlencode(kwargs).encode() r = tflow.tflow(resp=True) r.request.headers["Content-Type"] = "application/x-www-form-urlencoded" r2 = tflow.tflow(resp=True) r2.request.headers["Content-Type"] = "application/x-www-form-urlencoded" thash(r, r2, urlencode_setter) boundary = 'somefancyboundary' def multipart_setter(r, **kwargs): b = "--{0}\n".format(boundary) parts = [] for k, v in kwargs.items(): parts.append( "Content-Disposition: form-data; name=\"%s\"\n\n" "%s\n" % (k, v) ) c = b + b.join(parts) + b r.request.content = c.encode() r.request.headers["content-type"] = 'multipart/form-data; boundary=' +\ boundary r = tflow.tflow(resp=True) r2 = tflow.tflow(resp=True) thash(r, r2, multipart_setter) def test_server_playback_full(): s = serverplayback.ServerPlayback() with taddons.context(s) as tctx: tctx.configure( s, server_replay_refresh=True, ) f = tflow.tflow() f.response = mitmproxy.test.tutils.tresp(content=f.request.content) s.load_flows([f, f]) tf = tflow.tflow() assert not tf.response s.request(tf) assert tf.response == f.response tf = tflow.tflow() tf.request.content = b"gibble" assert not tf.response s.request(tf) assert not tf.response def test_server_playback_kill(): s = serverplayback.ServerPlayback() with taddons.context(s) as tctx: tctx.configure( s, server_replay_refresh=True, server_replay_kill_extra=True ) f = tflow.tflow() f.response = mitmproxy.test.tutils.tresp(content=f.request.content) s.load_flows([f]) f = tflow.tflow() f.request.host = "nonexistent" tctx.cycle(s, f) assert f.reply.value == exceptions.Kill def test_server_playback_response_deleted(): """ The server playback addon holds references to flows that can be modified by the user in the meantime. One thing that can happen is that users remove the response object. This happens for example when doing a client replay at the same time. """ sp = serverplayback.ServerPlayback() with taddons.context(sp) as tctx: tctx.configure(sp) f1 = tflow.tflow(resp=True) f2 = tflow.tflow(resp=True) assert not sp.flowmap sp.load_flows([f1, f2]) assert sp.flowmap f1.response = f2.response = None assert not sp.next_flow(f1) assert not sp.flowmap