forked from openresty/lua-nginx-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path081-bytecode.t
376 lines (333 loc) · 13.6 KB
/
081-bytecode.t
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
364
365
366
367
368
369
370
371
372
373
374
375
376
# vim:set ft= ts=4 sw=4 et fdm=marker:
use Test::Nginx::Socket::Lua;
#worker_connections(1014);
#master_on();
#workers(2);
#log_level('warn');
repeat_each(2);
#repeat_each(1);
plan tests => repeat_each() * (blocks() * 3);
#no_diff();
#no_long_string();
run_tests();
__DATA__
=== TEST 1: bytecode ("ngx.say('hello');")
--- config
root html;
location /save_call {
content_by_lua '
ngx.req.read_body();
local b = ngx.req.get_body_data();
local f = io.open(ngx.var.realpath_root.."/test.lua", "w");
-- luajit bytecode: sub(149,-1), lua bytecode: sub(1,147)
if jit then
if not string.find(jit.version, "LuaJIT 2.0") then
ngx.say("test skipped")
return
end
f:write(string.sub(b, 149));
else
f:write(string.sub(b, 1, 147));
end
f:close();
local res = ngx.location.capture("/call");
ngx.print(res.body)
';
}
location /call {
content_by_lua_file $realpath_root/test.lua;
}
--- request eval
"POST /save_call
\x1b\x4c\x75\x61\x51\x00\x01\x04\x08\x04\x08\x00\x0a\x00\x00\x00\x00\x00\x00\x00\x40\x74\x65\x73\x74\x2e\x6c\x75\x61\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x02\x05\x00\x00\x00\x05\x00\x00\x00\x06\x40\x40\x00\x41\x80\x00\x00\x1c\x40\x00\x01\x1e\x00\x80\x00\x03\x00\x00\x00\x04\x04\x00\x00\x00\x00\x00\x00\x00\x6e\x67\x78\x00\x04\x04\x00\x00\x00\x00\x00\x00\x00\x73\x61\x79\x00\x04\x06\x00\x00\x00\x00\x00\x00\x00\x68\x65\x6c\x6c\x6f\x00\x00\x00\x00\x00\x05\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x1b\x4c\x4a\x01\x02\x29\x02\x00\x02\x00\x03\x00\x05\x34\x00\x00\x00\x37\x00\x01\x00\x25\x01\x02\x00\x3e\x00\x02\x01\x47\x00\x01\x00\x0a\x68\x65\x6c\x6c\x6f\x08\x73\x61\x79\x08\x6e\x67\x78\x00"
--- response_body_like chop
^(?:hello|test skipped)$
--- no_error_log
[error]
=== TEST 2: luajit load lua bytecode or lua load luajit bytecode
--- config
root html;
location /save_call {
content_by_lua '
ngx.req.read_body();
local b = ngx.req.get_body_data();
local f = io.open(ngx.var.realpath_root.."/test.lua", "w");
-- luajit bytecode: sub(149,-1), lua bytecode: sub(1,147)
if not package.loaded["jit"] then
f:write(string.sub(b, 149));
else
f:write(string.sub(b, 1, 147));
end
f:close();
local res = ngx.location.capture("/call");
if res.status == 200 then
ngx.print(res.body)
else
ngx.say("error")
end
';
}
location /call {
content_by_lua_file $realpath_root/test.lua;
}
--- request eval
"POST /save_call
\x1b\x4c\x75\x61\x51\x00\x01\x04\x08\x04\x08\x00\x0a\x00\x00\x00\x00\x00\x00\x00\x40\x74\x65\x73\x74\x2e\x6c\x75\x61\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x02\x05\x00\x00\x00\x05\x00\x00\x00\x06\x40\x40\x00\x41\x80\x00\x00\x1c\x40\x00\x01\x1e\x00\x80\x00\x03\x00\x00\x00\x04\x04\x00\x00\x00\x00\x00\x00\x00\x6e\x67\x78\x00\x04\x04\x00\x00\x00\x00\x00\x00\x00\x73\x61\x79\x00\x04\x06\x00\x00\x00\x00\x00\x00\x00\x68\x65\x6c\x6c\x6f\x00\x00\x00\x00\x00\x05\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x1b\x4c\x4a\x01\x02\x29\x02\x00\x02\x00\x03\x00\x05\x34\x00\x00\x00\x37\x00\x01\x00\x25\x01\x02\x00\x3e\x00\x02\x01\x47\x00\x01\x00\x0a\x68\x65\x6c\x6c\x6f\x08\x73\x61\x79\x08\x6e\x67\x78\x00"
--- response_body
error
--- error_log eval
qr/failed to load external Lua file ".*?test\.lua": .* cannot load incompatible bytecode/
=== TEST 3: unknown bytecode version
--- config
root html;
location /save_call {
content_by_lua '
ngx.req.read_body();
local b = ngx.req.get_body_data();
local f = io.open(ngx.var.realpath_root.."/test.lua", "w");
-- luajit bytecode: sub(149,-1), lua bytecode: sub(1,147)
if package.loaded["jit"] then
f:write(string.sub(b, 149));
else
f:write(string.sub(b, 1, 147));
end
f:close();
local res = ngx.location.capture("/call");
if res.status == 200 then
ngx.print(res.body)
else
ngx.say("error")
end
';
}
location /call {
content_by_lua_file $realpath_root/test.lua;
}
--- request eval
"POST /save_call
\x1b\x4c\x75\x61\x52\x00\x01\x04\x08\x04\x08\x00\x0a\x00\x00\x00\x00\x00\x00\x00\x40\x74\x65\x73\x74\x2e\x6c\x75\x61\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x02\x05\x00\x00\x00\x05\x00\x00\x00\x06\x40\x40\x00\x41\x80\x00\x00\x1c\x40\x00\x01\x1e\x00\x80\x00\x03\x00\x00\x00\x04\x04\x00\x00\x00\x00\x00\x00\x00\x6e\x67\x78\x00\x04\x04\x00\x00\x00\x00\x00\x00\x00\x73\x61\x79\x00\x04\x06\x00\x00\x00\x00\x00\x00\x00\x68\x65\x6c\x6c\x6f\x00\x00\x00\x00\x00\x05\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x1b\x4c\x4a\x80\x02\x29\x02\x00\x02\x00\x03\x00\x05\x34\x00\x00\x00\x37\x00\x01\x00\x25\x01\x02\x00\x3e\x00\x02\x01\x47\x00\x01\x00\x0a\x68\x65\x6c\x6c\x6f\x08\x73\x61\x79\x08\x6e\x67\x78\x00"
--- response_body
error
--- error_log
cannot load incompatible bytecode
=== TEST 4: bytecode (big endian)
--- config
root html;
location /save_call {
content_by_lua '
ngx.req.read_body();
local b = ngx.req.get_body_data();
local f = io.open(ngx.var.realpath_root.."/test.lua", "w");
-- luajit bytecode: sub(149,-1), lua bytecode: sub(1,147)
local do_jit
if jit then
if not string.find(jit.version, "LuaJIT 2.0") then
ngx.say("test skipped")
return
end
do_jit = true; f:write(string.sub(b, 149));
else
f:write(string.sub(b, 1, 147));
end
f:close(); res = ngx.location.capture("/call");
if do_jit and res.status == 200 then
ngx.say("ok")
elseif not do_jit and res.status == 500 then
ngx.say("ok")
else
ngx.say("error")
end
';
}
location /call {
content_by_lua_file $realpath_root/test.lua;
}
--- request eval
"POST /save_call
\x1b\x4c\x75\x61\x51\x00\x00\x04\x08\x04\x08\x00\x0a\x00\x00\x00\x00\x00\x00\x00\x40\x74\x65\x73\x74\x2e\x6c\x75\x61\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x02\x05\x00\x00\x00\x05\x00\x00\x00\x06\x40\x40\x00\x41\x80\x00\x00\x1c\x40\x00\x01\x1e\x00\x80\x00\x03\x00\x00\x00\x04\x04\x00\x00\x00\x00\x00\x00\x00\x6e\x67\x78\x00\x04\x04\x00\x00\x00\x00\x00\x00\x00\x73\x61\x79\x00\x04\x06\x00\x00\x00\x00\x00\x00\x00\x68\x65\x6c\x6c\x6f\x00\x00\x00\x00\x00\x05\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x1b\x4c\x4a\x01\x03\x29\x02\x00\x02\x00\x03\x00\x05\x00\x00\x00\x34\x00\x01\x00\x37\x00\x02\x01\x25\x01\x02\x00\x3e\x00\x01\x00\x47\x0a\x68\x65\x6c\x6c\x6f\x08\x73\x61\x79\x08\x6e\x67\x78\x00"
--- response_body_like chop
^(?:ok|test skipped)$
--- no_error_log
[error]
=== TEST 5: good header but bad body
--- config
root html;
location /save_call {
content_by_lua '
ngx.req.read_body();
local b = ngx.req.get_body_data();
local f = io.open(ngx.var.realpath_root.."/test.lua", "w");
-- luajit bytecode: sub(149,-1), lua bytecode: sub(1,147)
local jit;
if package.loaded["jit"] then
jit = true;
f:write(string.sub(b, 149));
else
f:write(string.sub(b, 1, 147));
end
if not jit then
f:close(); res = ngx.location.capture("/call");
if res.status == 200 then
ngx.print("ok")
else
ngx.say("error")
end
else
-- luajit will get a segmentation fault with bad bytecode,
-- so here just skip this case for luajit
ngx.say("error")
end
';
}
location /call {
content_by_lua_file $realpath_root/test.lua;
}
--- request eval
"POST /save_call
\x1b\x4c\x75\x61\x51\x00\x01\x04\x08\x04\x08\x00\x0a\x00\x00\x00\x00\x00\x00\x00\x40\x74\x65\x73\x74\x2e\x6c\x75\x61\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x02\x05\x00\x00\x00\xff\xff\xff\xff\x06\x40\x40\x00\x41\x80\x00\x00\x1c\x40\x00\x01\x1e\x00\x80\x00\x03\x00\x00\x00\x04\x04\x00\x00\x00\x00\x00\x00\x00\x6e\x67\x78\x00\x04\x04\x00\x00\x00\x00\x00\x00\x00\x73\x61\x79\x00\x04\x06\x00\x00\x00\x00\x00\x00\x00\x68\x65\x6c\x6c\x6f\x00\x00\x00\x00\x00\x05\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x1b\x4c\x4a\x01\x02\x29\x02\x00\x02\x00\x03\x00\x05\xff\xff\xff\xff\x37\x00\x01\x00\x25\x01\x02\x00\x3e\x00\x02\x01\x47\x00\x01\x00\x0a\x68\x65\x6c\x6c\x6f\x08\x73\x61\x79\x08\x6e\x67\x78\x00"
--- response_body
error
--- no_error_log
[error]
=== TEST 6: stripped(lua) & no stripped(luajit)
--- config
root html;
location /save_call {
content_by_lua '
ngx.req.read_body();
local b = ngx.req.get_body_data();
local f = io.open(ngx.var.realpath_root.."/test.lua", "w");
-- luajit bytecode: sub(149,-1), lua bytecode: sub(1,147)
if jit then
if not string.find(jit.version, "LuaJIT 2.0") then
ngx.say("test skipped")
return
end
f:write(string.sub(b, 119));
else
f:write(string.sub(b, 1, 117));
end
f:close(); res = ngx.location.capture("/call");
ngx.print(res.body)
';
}
location /call {
content_by_lua_file $realpath_root/test.lua;
}
--- request eval
"POST /save_call
\x1b\x4c\x75\x61\x51\x00\x01\x04\x08\x04\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x02\x05\x00\x00\x00\x05\x00\x00\x00\x06\x40\x40\x00\x41\x80\x00\x00\x1c\x40\x00\x01\x1e\x00\x80\x00\x03\x00\x00\x00\x04\x04\x00\x00\x00\x00\x00\x00\x00\x6e\x67\x78\x00\x04\x04\x00\x00\x00\x00\x00\x00\x00\x73\x61\x79\x00\x04\x06\x00\x00\x00\x00\x00\x00\x00\x68\x65\x6c\x6c\x6f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x1b\x4c\x4a\x01\x00\x09\x40\x74\x65\x73\x74\x2e\x6c\x75\x61\x32\x02\x00\x02\x00\x03\x00\x05\x06\x00\x02\x34\x00\x00\x00\x37\x00\x01\x00\x25\x01\x02\x00\x3e\x00\x02\x01\x47\x00\x01\x00\x0a\x68\x65\x6c\x6c\x6f\x08\x73\x61\x79\x08\x6e\x67\x78\x01\x01\x01\x01\x01\x00\x00"
--- response_body_like chop
^(?:hello|test skipped)$
--- no_error_log
[error]
=== TEST 7: generate & load bytecode for LuaJIT (stripped)
--- config
location = /t {
content_by_lua '
local bcsave = require "jit.bcsave"
if jit then
local prefix = "$TEST_NGINX_SERVER_ROOT"
local infile = prefix .. "/html/a.lua"
local outfile = prefix .. "/html/a.luac"
bcsave.start("-s", infile, outfile)
return ngx.exec("/call")
end
ngx.say("test skipped!")
';
}
location = /call {
content_by_lua_file html/a.luac;
}
--- request
GET /t
--- user_files
>>> a.lua
ngx.status = 201 ngx.say("hello from Lua!")
--- response_body_like chop
^(?:hello from Lua!|test skipped!)$
--- no_error_log
[error]
--- error_code: 201
=== TEST 8: generate & load bytecode for LuaJIT (not stripped)
--- config
location = /t {
content_by_lua '
local bcsave = require "jit.bcsave"
if jit then
local prefix = "$TEST_NGINX_SERVER_ROOT"
local infile = prefix .. "/html/a.lua"
local outfile = prefix .. "/html/a.luac"
bcsave.start("-g", infile, outfile)
return ngx.exec("/call")
end
ngx.say("test skipped!")
';
}
location = /call {
content_by_lua_file html/a.luac;
}
--- request
GET /t
--- user_files
>>> a.lua
ngx.status = 201 ngx.say("hello from Lua!")
--- response_body_like chop
^(?:hello from Lua!|test skipped!)$
--- no_error_log
[error]
--- error_code: 201
=== TEST 9: bytecode (not stripped)
--- config
location = /t {
content_by_lua_block {
local f = assert(loadstring("local a = 1 ngx.say('a = ', a)", "=code"))
local bc = string.dump(f)
local f = assert(io.open("$TEST_NGINX_SERVER_ROOT/html/a.luac", "w"))
f:write(bc)
f:close()
}
}
location = /t2 {
content_by_lua_file html/a.luac;
}
location = /main {
echo_location /t;
echo_location /t2;
}
--- request
GET /main
--- response_body
a = 1
--- no_error_log
[error]
=== TEST 10: bytecode (stripped)
--- config
location = /t {
content_by_lua_block {
local f = assert(loadstring("local a = 1 ngx.say('a = ', a)", "=code"))
local bc = string.dump(f, true)
local f = assert(io.open("$TEST_NGINX_SERVER_ROOT/html/a.luac", "w"))
f:write(bc)
f:close()
}
}
location = /t2 {
content_by_lua_file html/a.luac;
}
location = /main {
echo_location /t;
echo_location /t2;
}
--- request
GET /main
--- response_body
a = 1
--- no_error_log
[error]