xref: /redis-3.2.3/src/scripting.c (revision f3fd58eb)
1 /*
2  * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  *   * Redistributions of source code must retain the above copyright notice,
9  *     this list of conditions and the following disclaimer.
10  *   * Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  *   * Neither the name of Redis nor the names of its contributors may be used
14  *     to endorse or promote products derived from this software without
15  *     specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include "redis.h"
31 #include "sha1.h"
32 #include "rand.h"
33 #include "cluster.h"
34 
35 #include <lua.h>
36 #include <lauxlib.h>
37 #include <lualib.h>
38 #include <ctype.h>
39 #include <math.h>
40 
41 char *redisProtocolToLuaType_Int(lua_State *lua, char *reply);
42 char *redisProtocolToLuaType_Bulk(lua_State *lua, char *reply);
43 char *redisProtocolToLuaType_Status(lua_State *lua, char *reply);
44 char *redisProtocolToLuaType_Error(lua_State *lua, char *reply);
45 char *redisProtocolToLuaType_MultiBulk(lua_State *lua, char *reply);
46 int redis_math_random (lua_State *L);
47 int redis_math_randomseed (lua_State *L);
48 void sha1hex(char *digest, char *script, size_t len);
49 
50 /* Take a Redis reply in the Redis protocol format and convert it into a
51  * Lua type. Thanks to this function, and the introduction of not connected
52  * clients, it is trivial to implement the redis() lua function.
53  *
54  * Basically we take the arguments, execute the Redis command in the context
55  * of a non connected client, then take the generated reply and convert it
56  * into a suitable Lua type. With this trick the scripting feature does not
57  * need the introduction of a full Redis internals API. Basically the script
58  * is like a normal client that bypasses all the slow I/O paths.
59  *
60  * Note: in this function we do not do any sanity check as the reply is
61  * generated by Redis directly. This allows us to go faster.
62  * The reply string can be altered during the parsing as it is discarded
63  * after the conversion is completed.
64  *
65  * Errors are returned as a table with a single 'err' field set to the
66  * error string.
67  */
68 
69 char *redisProtocolToLuaType(lua_State *lua, char* reply) {
70     char *p = reply;
71 
72     switch(*p) {
73     case ':':
74         p = redisProtocolToLuaType_Int(lua,reply);
75         break;
76     case '$':
77         p = redisProtocolToLuaType_Bulk(lua,reply);
78         break;
79     case '+':
80         p = redisProtocolToLuaType_Status(lua,reply);
81         break;
82     case '-':
83         p = redisProtocolToLuaType_Error(lua,reply);
84         break;
85     case '*':
86         p = redisProtocolToLuaType_MultiBulk(lua,reply);
87         break;
88     }
89     return p;
90 }
91 
92 char *redisProtocolToLuaType_Int(lua_State *lua, char *reply) {
93     char *p = strchr(reply+1,'\r');
94     long long value;
95 
96     string2ll(reply+1,p-reply-1,&value);
97     lua_pushnumber(lua,(lua_Number)value);
98     return p+2;
99 }
100 
101 char *redisProtocolToLuaType_Bulk(lua_State *lua, char *reply) {
102     char *p = strchr(reply+1,'\r');
103     long long bulklen;
104 
105     string2ll(reply+1,p-reply-1,&bulklen);
106     if (bulklen == -1) {
107         lua_pushboolean(lua,0);
108         return p+2;
109     } else {
110         lua_pushlstring(lua,p+2,bulklen);
111         return p+2+bulklen+2;
112     }
113 }
114 
115 char *redisProtocolToLuaType_Status(lua_State *lua, char *reply) {
116     char *p = strchr(reply+1,'\r');
117 
118     lua_newtable(lua);
119     lua_pushstring(lua,"ok");
120     lua_pushlstring(lua,reply+1,p-reply-1);
121     lua_settable(lua,-3);
122     return p+2;
123 }
124 
125 char *redisProtocolToLuaType_Error(lua_State *lua, char *reply) {
126     char *p = strchr(reply+1,'\r');
127 
128     lua_newtable(lua);
129     lua_pushstring(lua,"err");
130     lua_pushlstring(lua,reply+1,p-reply-1);
131     lua_settable(lua,-3);
132     return p+2;
133 }
134 
135 char *redisProtocolToLuaType_MultiBulk(lua_State *lua, char *reply) {
136     char *p = strchr(reply+1,'\r');
137     long long mbulklen;
138     int j = 0;
139 
140     string2ll(reply+1,p-reply-1,&mbulklen);
141     p += 2;
142     if (mbulklen == -1) {
143         lua_pushboolean(lua,0);
144         return p;
145     }
146     lua_newtable(lua);
147     for (j = 0; j < mbulklen; j++) {
148         lua_pushnumber(lua,j+1);
149         p = redisProtocolToLuaType(lua,p);
150         lua_settable(lua,-3);
151     }
152     return p;
153 }
154 
155 void luaPushError(lua_State *lua, char *error) {
156     lua_Debug dbg;
157 
158     lua_newtable(lua);
159     lua_pushstring(lua,"err");
160 
161     /* Attempt to figure out where this function was called, if possible */
162     if(lua_getstack(lua, 1, &dbg) && lua_getinfo(lua, "nSl", &dbg)) {
163         sds msg = sdscatprintf(sdsempty(), "%s: %d: %s",
164             dbg.source, dbg.currentline, error);
165         lua_pushstring(lua, msg);
166         sdsfree(msg);
167     } else {
168         lua_pushstring(lua, error);
169     }
170     lua_settable(lua,-3);
171 }
172 
173 /* Sort the array currently in the stack. We do this to make the output
174  * of commands like KEYS or SMEMBERS something deterministic when called
175  * from Lua (to play well with AOf/replication).
176  *
177  * The array is sorted using table.sort itself, and assuming all the
178  * list elements are strings. */
179 void luaSortArray(lua_State *lua) {
180     /* Initial Stack: array */
181     lua_getglobal(lua,"table");
182     lua_pushstring(lua,"sort");
183     lua_gettable(lua,-2);       /* Stack: array, table, table.sort */
184     lua_pushvalue(lua,-3);      /* Stack: array, table, table.sort, array */
185     if (lua_pcall(lua,1,0,0)) {
186         /* Stack: array, table, error */
187 
188         /* We are not interested in the error, we assume that the problem is
189          * that there are 'false' elements inside the array, so we try
190          * again with a slower function but able to handle this case, that
191          * is: table.sort(table, __redis__compare_helper) */
192         lua_pop(lua,1);             /* Stack: array, table */
193         lua_pushstring(lua,"sort"); /* Stack: array, table, sort */
194         lua_gettable(lua,-2);       /* Stack: array, table, table.sort */
195         lua_pushvalue(lua,-3);      /* Stack: array, table, table.sort, array */
196         lua_getglobal(lua,"__redis__compare_helper");
197         /* Stack: array, table, table.sort, array, __redis__compare_helper */
198         lua_call(lua,2,0);
199     }
200     /* Stack: array (sorted), table */
201     lua_pop(lua,1);             /* Stack: array (sorted) */
202 }
203 
204 #define LUA_CMD_OBJCACHE_SIZE 32
205 #define LUA_CMD_OBJCACHE_MAX_LEN 64
206 int luaRedisGenericCommand(lua_State *lua, int raise_error) {
207     int j, argc = lua_gettop(lua);
208     struct redisCommand *cmd;
209     redisClient *c = server.lua_client;
210     sds reply;
211 
212     /* Cached across calls. */
213     static robj **argv = NULL;
214     static int argv_size = 0;
215     static robj *cached_objects[LUA_CMD_OBJCACHE_SIZE];
216     static size_t cached_objects_len[LUA_CMD_OBJCACHE_SIZE];
217 
218     /* Require at least one argument */
219     if (argc == 0) {
220         luaPushError(lua,
221             "Please specify at least one argument for redis.call()");
222         return 1;
223     }
224 
225     /* Build the arguments vector */
226     if (argv_size < argc) {
227         argv = zrealloc(argv,sizeof(robj*)*argc);
228         argv_size = argc;
229     }
230 
231     for (j = 0; j < argc; j++) {
232         char *obj_s;
233         size_t obj_len;
234         char dbuf[64];
235 
236         if (lua_type(lua,j+1) == LUA_TNUMBER) {
237             /* We can't use lua_tolstring() for number -> string conversion
238              * since Lua uses a format specifier that loses precision. */
239             lua_Number num = lua_tonumber(lua,j+1);
240 
241             obj_len = snprintf(dbuf,sizeof(dbuf),"%.17g",(double)num);
242             obj_s = dbuf;
243         } else {
244             obj_s = (char*)lua_tolstring(lua,j+1,&obj_len);
245             if (obj_s == NULL) break; /* Not a string. */
246         }
247 
248         /* Try to use a cached object. */
249         if (j < LUA_CMD_OBJCACHE_SIZE && cached_objects[j] &&
250             cached_objects_len[j] >= obj_len)
251         {
252             char *s = cached_objects[j]->ptr;
253             struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr)));
254 
255             argv[j] = cached_objects[j];
256             cached_objects[j] = NULL;
257             memcpy(s,obj_s,obj_len+1);
258             sh->free += sh->len - obj_len;
259             sh->len = obj_len;
260         } else {
261             argv[j] = createStringObject(obj_s, obj_len);
262         }
263     }
264 
265     /* Check if one of the arguments passed by the Lua script
266      * is not a string or an integer (lua_isstring() return true for
267      * integers as well). */
268     if (j != argc) {
269         j--;
270         while (j >= 0) {
271             decrRefCount(argv[j]);
272             j--;
273         }
274         luaPushError(lua,
275             "Lua redis() command arguments must be strings or integers");
276         return 1;
277     }
278 
279     /* Setup our fake client for command execution */
280     c->argv = argv;
281     c->argc = argc;
282 
283     /* Command lookup */
284     cmd = lookupCommand(argv[0]->ptr);
285     if (!cmd || ((cmd->arity > 0 && cmd->arity != argc) ||
286                    (argc < -cmd->arity)))
287     {
288         if (cmd)
289             luaPushError(lua,
290                 "Wrong number of args calling Redis command From Lua script");
291         else
292             luaPushError(lua,"Unknown Redis command called from Lua script");
293         goto cleanup;
294     }
295     c->cmd = cmd;
296 
297     /* There are commands that are not allowed inside scripts. */
298     if (cmd->flags & REDIS_CMD_NOSCRIPT) {
299         luaPushError(lua, "This Redis command is not allowed from scripts");
300         goto cleanup;
301     }
302 
303     /* Write commands are forbidden against read-only slaves, or if a
304      * command marked as non-deterministic was already called in the context
305      * of this script. */
306     if (cmd->flags & REDIS_CMD_WRITE) {
307         if (server.lua_random_dirty) {
308             luaPushError(lua,
309                 "Write commands not allowed after non deterministic commands");
310             goto cleanup;
311         } else if (server.masterhost && server.repl_slave_ro &&
312                    !server.loading &&
313                    !(server.lua_caller->flags & REDIS_MASTER))
314         {
315             luaPushError(lua, shared.roslaveerr->ptr);
316             goto cleanup;
317         } else if (server.stop_writes_on_bgsave_err &&
318                    server.saveparamslen > 0 &&
319                    server.lastbgsave_status == REDIS_ERR)
320         {
321             luaPushError(lua, shared.bgsaveerr->ptr);
322             goto cleanup;
323         }
324     }
325 
326     /* If we reached the memory limit configured via maxmemory, commands that
327      * could enlarge the memory usage are not allowed, but only if this is the
328      * first write in the context of this script, otherwise we can't stop
329      * in the middle. */
330     if (server.maxmemory && server.lua_write_dirty == 0 &&
331         (cmd->flags & REDIS_CMD_DENYOOM))
332     {
333         if (freeMemoryIfNeeded() == REDIS_ERR) {
334             luaPushError(lua, shared.oomerr->ptr);
335             goto cleanup;
336         }
337     }
338 
339     if (cmd->flags & REDIS_CMD_RANDOM) server.lua_random_dirty = 1;
340     if (cmd->flags & REDIS_CMD_WRITE) server.lua_write_dirty = 1;
341 
342     /* If this is a Redis Cluster node, we need to make sure Lua is not
343      * trying to access non-local keys. */
344     if (server.cluster_enabled) {
345         /* Duplicate relevant flags in the lua client. */
346         c->flags &= ~(REDIS_READONLY|REDIS_ASKING);
347         c->flags |= server.lua_caller->flags & (REDIS_READONLY|REDIS_ASKING);
348         if (getNodeByQuery(c,c->cmd,c->argv,c->argc,NULL,NULL) !=
349                            server.cluster->myself)
350         {
351             luaPushError(lua,
352                 "Lua script attempted to access a non local key in a "
353                 "cluster node");
354             goto cleanup;
355         }
356     }
357 
358     /* Run the command */
359     call(c,REDIS_CALL_SLOWLOG | REDIS_CALL_STATS);
360 
361     /* Convert the result of the Redis command into a suitable Lua type.
362      * The first thing we need is to create a single string from the client
363      * output buffers. */
364     if (listLength(c->reply) == 0 && c->bufpos < REDIS_REPLY_CHUNK_BYTES) {
365         /* This is a fast path for the common case of a reply inside the
366          * client static buffer. Don't create an SDS string but just use
367          * the client buffer directly. */
368         c->buf[c->bufpos] = '\0';
369         reply = c->buf;
370         c->bufpos = 0;
371     } else {
372         reply = sdsnewlen(c->buf,c->bufpos);
373         c->bufpos = 0;
374         while(listLength(c->reply)) {
375             robj *o = listNodeValue(listFirst(c->reply));
376 
377             reply = sdscatlen(reply,o->ptr,sdslen(o->ptr));
378             listDelNode(c->reply,listFirst(c->reply));
379         }
380     }
381     if (raise_error && reply[0] != '-') raise_error = 0;
382     redisProtocolToLuaType(lua,reply);
383     /* Sort the output array if needed, assuming it is a non-null multi bulk
384      * reply as expected. */
385     if ((cmd->flags & REDIS_CMD_SORT_FOR_SCRIPT) &&
386         (reply[0] == '*' && reply[1] != '-')) {
387             luaSortArray(lua);
388     }
389     if (reply != c->buf) sdsfree(reply);
390     c->reply_bytes = 0;
391 
392 cleanup:
393     /* Clean up. Command code may have changed argv/argc so we use the
394      * argv/argc of the client instead of the local variables. */
395     for (j = 0; j < c->argc; j++) {
396         robj *o = c->argv[j];
397 
398         /* Try to cache the object in the cached_objects array.
399          * The object must be small, SDS-encoded, and with refcount = 1
400          * (we must be the only owner) for us to cache it. */
401         if (j < LUA_CMD_OBJCACHE_SIZE &&
402             o->refcount == 1 &&
403             (o->encoding == REDIS_ENCODING_RAW ||
404              o->encoding == REDIS_ENCODING_EMBSTR) &&
405             sdslen(o->ptr) <= LUA_CMD_OBJCACHE_MAX_LEN)
406         {
407             struct sdshdr *sh = (void*)(((char*)(o->ptr))-(sizeof(struct sdshdr)));
408 
409             if (cached_objects[j]) decrRefCount(cached_objects[j]);
410             cached_objects[j] = o;
411             cached_objects_len[j] = sh->free + sh->len;
412         } else {
413             decrRefCount(o);
414         }
415     }
416 
417     if (c->argv != argv) {
418         zfree(c->argv);
419         argv = NULL;
420         argv_size = 0;
421     }
422 
423     if (raise_error) {
424         /* If we are here we should have an error in the stack, in the
425          * form of a table with an "err" field. Extract the string to
426          * return the plain error. */
427         lua_pushstring(lua,"err");
428         lua_gettable(lua,-2);
429         return lua_error(lua);
430     }
431     return 1;
432 }
433 
434 int luaRedisCallCommand(lua_State *lua) {
435     return luaRedisGenericCommand(lua,1);
436 }
437 
438 int luaRedisPCallCommand(lua_State *lua) {
439     return luaRedisGenericCommand(lua,0);
440 }
441 
442 /* This adds redis.sha1hex(string) to Lua scripts using the same hashing
443  * function used for sha1ing lua scripts. */
444 int luaRedisSha1hexCommand(lua_State *lua) {
445     int argc = lua_gettop(lua);
446     char digest[41];
447     size_t len;
448     char *s;
449 
450     if (argc != 1) {
451         luaPushError(lua, "wrong number of arguments");
452         return 1;
453     }
454 
455     s = (char*)lua_tolstring(lua,1,&len);
456     sha1hex(digest,s,len);
457     lua_pushstring(lua,digest);
458     return 1;
459 }
460 
461 /* Returns a table with a single field 'field' set to the string value
462  * passed as argument. This helper function is handy when returning
463  * a Redis Protocol error or status reply from Lua:
464  *
465  * return redis.error_reply("ERR Some Error")
466  * return redis.status_reply("ERR Some Error")
467  */
468 int luaRedisReturnSingleFieldTable(lua_State *lua, char *field) {
469     if (lua_gettop(lua) != 1 || lua_type(lua,-1) != LUA_TSTRING) {
470         luaPushError(lua, "wrong number or type of arguments");
471         return 1;
472     }
473 
474     lua_newtable(lua);
475     lua_pushstring(lua, field);
476     lua_pushvalue(lua, -3);
477     lua_settable(lua, -3);
478     return 1;
479 }
480 
481 int luaRedisErrorReplyCommand(lua_State *lua) {
482     return luaRedisReturnSingleFieldTable(lua,"err");
483 }
484 
485 int luaRedisStatusReplyCommand(lua_State *lua) {
486     return luaRedisReturnSingleFieldTable(lua,"ok");
487 }
488 
489 int luaLogCommand(lua_State *lua) {
490     int j, argc = lua_gettop(lua);
491     int level;
492     sds log;
493 
494     if (argc < 2) {
495         luaPushError(lua, "redis.log() requires two arguments or more.");
496         return 1;
497     } else if (!lua_isnumber(lua,-argc)) {
498         luaPushError(lua, "First argument must be a number (log level).");
499         return 1;
500     }
501     level = lua_tonumber(lua,-argc);
502     if (level < REDIS_DEBUG || level > REDIS_WARNING) {
503         luaPushError(lua, "Invalid debug level.");
504         return 1;
505     }
506 
507     /* Glue together all the arguments */
508     log = sdsempty();
509     for (j = 1; j < argc; j++) {
510         size_t len;
511         char *s;
512 
513         s = (char*)lua_tolstring(lua,(-argc)+j,&len);
514         if (s) {
515             if (j != 1) log = sdscatlen(log," ",1);
516             log = sdscatlen(log,s,len);
517         }
518     }
519     redisLogRaw(level,log);
520     sdsfree(log);
521     return 0;
522 }
523 
524 void luaMaskCountHook(lua_State *lua, lua_Debug *ar) {
525     long long elapsed;
526     REDIS_NOTUSED(ar);
527     REDIS_NOTUSED(lua);
528 
529     elapsed = mstime() - server.lua_time_start;
530     if (elapsed >= server.lua_time_limit && server.lua_timedout == 0) {
531         redisLog(REDIS_WARNING,"Lua slow script detected: still in execution after %lld milliseconds. You can try killing the script using the SCRIPT KILL command.",elapsed);
532         server.lua_timedout = 1;
533         /* Once the script timeouts we reenter the event loop to permit others
534          * to call SCRIPT KILL or SHUTDOWN NOSAVE if needed. For this reason
535          * we need to mask the client executing the script from the event loop.
536          * If we don't do that the client may disconnect and could no longer be
537          * here when the EVAL command will return. */
538          aeDeleteFileEvent(server.el, server.lua_caller->fd, AE_READABLE);
539     }
540     if (server.lua_timedout) processEventsWhileBlocked();
541     if (server.lua_kill) {
542         redisLog(REDIS_WARNING,"Lua script killed by user with SCRIPT KILL.");
543         lua_pushstring(lua,"Script killed by user with SCRIPT KILL...");
544         lua_error(lua);
545     }
546 }
547 
548 void luaLoadLib(lua_State *lua, const char *libname, lua_CFunction luafunc) {
549   lua_pushcfunction(lua, luafunc);
550   lua_pushstring(lua, libname);
551   lua_call(lua, 1, 0);
552 }
553 
554 LUALIB_API int (luaopen_cjson) (lua_State *L);
555 LUALIB_API int (luaopen_struct) (lua_State *L);
556 LUALIB_API int (luaopen_cmsgpack) (lua_State *L);
557 LUALIB_API int (luaopen_bit) (lua_State *L);
558 
559 void luaLoadLibraries(lua_State *lua) {
560     luaLoadLib(lua, "", luaopen_base);
561     luaLoadLib(lua, LUA_TABLIBNAME, luaopen_table);
562     luaLoadLib(lua, LUA_STRLIBNAME, luaopen_string);
563     luaLoadLib(lua, LUA_MATHLIBNAME, luaopen_math);
564     luaLoadLib(lua, LUA_DBLIBNAME, luaopen_debug);
565     luaLoadLib(lua, "cjson", luaopen_cjson);
566     luaLoadLib(lua, "struct", luaopen_struct);
567     luaLoadLib(lua, "cmsgpack", luaopen_cmsgpack);
568     luaLoadLib(lua, "bit", luaopen_bit);
569 
570 #if 0 /* Stuff that we don't load currently, for sandboxing concerns. */
571     luaLoadLib(lua, LUA_LOADLIBNAME, luaopen_package);
572     luaLoadLib(lua, LUA_OSLIBNAME, luaopen_os);
573 #endif
574 }
575 
576 /* Remove a functions that we don't want to expose to the Redis scripting
577  * environment. */
578 void luaRemoveUnsupportedFunctions(lua_State *lua) {
579     lua_pushnil(lua);
580     lua_setglobal(lua,"loadfile");
581 }
582 
583 /* This function installs metamethods in the global table _G that prevent
584  * the creation of globals accidentally.
585  *
586  * It should be the last to be called in the scripting engine initialization
587  * sequence, because it may interact with creation of globals. */
588 void scriptingEnableGlobalsProtection(lua_State *lua) {
589     char *s[32];
590     sds code = sdsempty();
591     int j = 0;
592 
593     /* strict.lua from: http://metalua.luaforge.net/src/lib/strict.lua.html.
594      * Modified to be adapted to Redis. */
595     s[j++]="local mt = {}\n";
596     s[j++]="setmetatable(_G, mt)\n";
597     s[j++]="mt.__newindex = function (t, n, v)\n";
598     s[j++]="  if debug.getinfo(2) then\n";
599     s[j++]="    local w = debug.getinfo(2, \"S\").what\n";
600     s[j++]="    if w ~= \"main\" and w ~= \"C\" then\n";
601     s[j++]="      error(\"Script attempted to create global variable '\"..tostring(n)..\"'\", 2)\n";
602     s[j++]="    end\n";
603     s[j++]="  end\n";
604     s[j++]="  rawset(t, n, v)\n";
605     s[j++]="end\n";
606     s[j++]="mt.__index = function (t, n)\n";
607     s[j++]="  if debug.getinfo(2) and debug.getinfo(2, \"S\").what ~= \"C\" then\n";
608     s[j++]="    error(\"Script attempted to access unexisting global variable '\"..tostring(n)..\"'\", 2)\n";
609     s[j++]="  end\n";
610     s[j++]="  return rawget(t, n)\n";
611     s[j++]="end\n";
612     s[j++]=NULL;
613 
614     for (j = 0; s[j] != NULL; j++) code = sdscatlen(code,s[j],strlen(s[j]));
615     luaL_loadbuffer(lua,code,sdslen(code),"@enable_strict_lua");
616     lua_pcall(lua,0,0,0);
617     sdsfree(code);
618 }
619 
620 /* Initialize the scripting environment.
621  * It is possible to call this function to reset the scripting environment
622  * assuming that we call scriptingRelease() before.
623  * See scriptingReset() for more information. */
624 void scriptingInit(void) {
625     lua_State *lua = lua_open();
626 
627     luaLoadLibraries(lua);
628     luaRemoveUnsupportedFunctions(lua);
629 
630     /* Initialize a dictionary we use to map SHAs to scripts.
631      * This is useful for replication, as we need to replicate EVALSHA
632      * as EVAL, so we need to remember the associated script. */
633     server.lua_scripts = dictCreate(&shaScriptObjectDictType,NULL);
634 
635     /* Register the redis commands table and fields */
636     lua_newtable(lua);
637 
638     /* redis.call */
639     lua_pushstring(lua,"call");
640     lua_pushcfunction(lua,luaRedisCallCommand);
641     lua_settable(lua,-3);
642 
643     /* redis.pcall */
644     lua_pushstring(lua,"pcall");
645     lua_pushcfunction(lua,luaRedisPCallCommand);
646     lua_settable(lua,-3);
647 
648     /* redis.log and log levels. */
649     lua_pushstring(lua,"log");
650     lua_pushcfunction(lua,luaLogCommand);
651     lua_settable(lua,-3);
652 
653     lua_pushstring(lua,"LOG_DEBUG");
654     lua_pushnumber(lua,REDIS_DEBUG);
655     lua_settable(lua,-3);
656 
657     lua_pushstring(lua,"LOG_VERBOSE");
658     lua_pushnumber(lua,REDIS_VERBOSE);
659     lua_settable(lua,-3);
660 
661     lua_pushstring(lua,"LOG_NOTICE");
662     lua_pushnumber(lua,REDIS_NOTICE);
663     lua_settable(lua,-3);
664 
665     lua_pushstring(lua,"LOG_WARNING");
666     lua_pushnumber(lua,REDIS_WARNING);
667     lua_settable(lua,-3);
668 
669     /* redis.sha1hex */
670     lua_pushstring(lua, "sha1hex");
671     lua_pushcfunction(lua, luaRedisSha1hexCommand);
672     lua_settable(lua, -3);
673 
674     /* redis.error_reply and redis.status_reply */
675     lua_pushstring(lua, "error_reply");
676     lua_pushcfunction(lua, luaRedisErrorReplyCommand);
677     lua_settable(lua, -3);
678     lua_pushstring(lua, "status_reply");
679     lua_pushcfunction(lua, luaRedisStatusReplyCommand);
680     lua_settable(lua, -3);
681 
682     /* Finally set the table as 'redis' global var. */
683     lua_setglobal(lua,"redis");
684 
685     /* Replace math.random and math.randomseed with our implementations. */
686     lua_getglobal(lua,"math");
687 
688     lua_pushstring(lua,"random");
689     lua_pushcfunction(lua,redis_math_random);
690     lua_settable(lua,-3);
691 
692     lua_pushstring(lua,"randomseed");
693     lua_pushcfunction(lua,redis_math_randomseed);
694     lua_settable(lua,-3);
695 
696     lua_setglobal(lua,"math");
697 
698     /* Add a helper function that we use to sort the multi bulk output of non
699      * deterministic commands, when containing 'false' elements. */
700     {
701         char *compare_func =    "function __redis__compare_helper(a,b)\n"
702                                 "  if a == false then a = '' end\n"
703                                 "  if b == false then b = '' end\n"
704                                 "  return a<b\n"
705                                 "end\n";
706         luaL_loadbuffer(lua,compare_func,strlen(compare_func),"@cmp_func_def");
707         lua_pcall(lua,0,0,0);
708     }
709 
710     /* Add a helper function we use for pcall error reporting.
711      * Note that when the error is in the C function we want to report the
712      * information about the caller, that's what makes sense from the point
713      * of view of the user debugging a script. */
714     {
715         char *errh_func =       "function __redis__err__handler(err)\n"
716                                 "  local i = debug.getinfo(2,'nSl')\n"
717                                 "  if i and i.what == 'C' then\n"
718                                 "    i = debug.getinfo(3,'nSl')\n"
719                                 "  end\n"
720                                 "  if i then\n"
721                                 "    return i.source .. ':' .. i.currentline .. ': ' .. err\n"
722                                 "  else\n"
723                                 "    return err\n"
724                                 "  end\n"
725                                 "end\n";
726         luaL_loadbuffer(lua,errh_func,strlen(errh_func),"@err_handler_def");
727         lua_pcall(lua,0,0,0);
728     }
729 
730     /* Create the (non connected) client that we use to execute Redis commands
731      * inside the Lua interpreter.
732      * Note: there is no need to create it again when this function is called
733      * by scriptingReset(). */
734     if (server.lua_client == NULL) {
735         server.lua_client = createClient(-1);
736         server.lua_client->flags |= REDIS_LUA_CLIENT;
737     }
738 
739     /* Lua beginners often don't use "local", this is likely to introduce
740      * subtle bugs in their code. To prevent problems we protect accesses
741      * to global variables. */
742     scriptingEnableGlobalsProtection(lua);
743 
744     server.lua = lua;
745 }
746 
747 /* Release resources related to Lua scripting.
748  * This function is used in order to reset the scripting environment. */
749 void scriptingRelease(void) {
750     dictRelease(server.lua_scripts);
751     lua_close(server.lua);
752 }
753 
754 void scriptingReset(void) {
755     scriptingRelease();
756     scriptingInit();
757 }
758 
759 /* Perform the SHA1 of the input string. We use this both for hashing script
760  * bodies in order to obtain the Lua function name, and in the implementation
761  * of redis.sha1().
762  *
763  * 'digest' should point to a 41 bytes buffer: 40 for SHA1 converted into an
764  * hexadecimal number, plus 1 byte for null term. */
765 void sha1hex(char *digest, char *script, size_t len) {
766     SHA1_CTX ctx;
767     unsigned char hash[20];
768     char *cset = "0123456789abcdef";
769     int j;
770 
771     SHA1Init(&ctx);
772     SHA1Update(&ctx,(unsigned char*)script,len);
773     SHA1Final(hash,&ctx);
774 
775     for (j = 0; j < 20; j++) {
776         digest[j*2] = cset[((hash[j]&0xF0)>>4)];
777         digest[j*2+1] = cset[(hash[j]&0xF)];
778     }
779     digest[40] = '\0';
780 }
781 
782 void luaReplyToRedisReply(redisClient *c, lua_State *lua) {
783     int t = lua_type(lua,-1);
784 
785     switch(t) {
786     case LUA_TSTRING:
787         addReplyBulkCBuffer(c,(char*)lua_tostring(lua,-1),lua_strlen(lua,-1));
788         break;
789     case LUA_TBOOLEAN:
790         addReply(c,lua_toboolean(lua,-1) ? shared.cone : shared.nullbulk);
791         break;
792     case LUA_TNUMBER:
793         addReplyLongLong(c,(long long)lua_tonumber(lua,-1));
794         break;
795     case LUA_TTABLE:
796         /* We need to check if it is an array, an error, or a status reply.
797          * Error are returned as a single element table with 'err' field.
798          * Status replies are returned as single element table with 'ok' field */
799         lua_pushstring(lua,"err");
800         lua_gettable(lua,-2);
801         t = lua_type(lua,-1);
802         if (t == LUA_TSTRING) {
803             sds err = sdsnew(lua_tostring(lua,-1));
804             sdsmapchars(err,"\r\n","  ",2);
805             addReplySds(c,sdscatprintf(sdsempty(),"-%s\r\n",err));
806             sdsfree(err);
807             lua_pop(lua,2);
808             return;
809         }
810 
811         lua_pop(lua,1);
812         lua_pushstring(lua,"ok");
813         lua_gettable(lua,-2);
814         t = lua_type(lua,-1);
815         if (t == LUA_TSTRING) {
816             sds ok = sdsnew(lua_tostring(lua,-1));
817             sdsmapchars(ok,"\r\n","  ",2);
818             addReplySds(c,sdscatprintf(sdsempty(),"+%s\r\n",ok));
819             sdsfree(ok);
820             lua_pop(lua,1);
821         } else {
822             void *replylen = addDeferredMultiBulkLength(c);
823             int j = 1, mbulklen = 0;
824 
825             lua_pop(lua,1); /* Discard the 'ok' field value we popped */
826             while(1) {
827                 lua_pushnumber(lua,j++);
828                 lua_gettable(lua,-2);
829                 t = lua_type(lua,-1);
830                 if (t == LUA_TNIL) {
831                     lua_pop(lua,1);
832                     break;
833                 }
834                 luaReplyToRedisReply(c, lua);
835                 mbulklen++;
836             }
837             setDeferredMultiBulkLength(c,replylen,mbulklen);
838         }
839         break;
840     default:
841         addReply(c,shared.nullbulk);
842     }
843     lua_pop(lua,1);
844 }
845 
846 /* Set an array of Redis String Objects as a Lua array (table) stored into a
847  * global variable. */
848 void luaSetGlobalArray(lua_State *lua, char *var, robj **elev, int elec) {
849     int j;
850 
851     lua_newtable(lua);
852     for (j = 0; j < elec; j++) {
853         lua_pushlstring(lua,(char*)elev[j]->ptr,sdslen(elev[j]->ptr));
854         lua_rawseti(lua,-2,j+1);
855     }
856     lua_setglobal(lua,var);
857 }
858 
859 /* Define a lua function with the specified function name and body.
860  * The function name musts be a 2 characters long string, since all the
861  * functions we defined in the Lua context are in the form:
862  *
863  *   f_<hex sha1 sum>
864  *
865  * On success REDIS_OK is returned, and nothing is left on the Lua stack.
866  * On error REDIS_ERR is returned and an appropriate error is set in the
867  * client context. */
868 int luaCreateFunction(redisClient *c, lua_State *lua, char *funcname, robj *body) {
869     sds funcdef = sdsempty();
870 
871     funcdef = sdscat(funcdef,"function ");
872     funcdef = sdscatlen(funcdef,funcname,42);
873     funcdef = sdscatlen(funcdef,"() ",3);
874     funcdef = sdscatlen(funcdef,body->ptr,sdslen(body->ptr));
875     funcdef = sdscatlen(funcdef," end",4);
876 
877     if (luaL_loadbuffer(lua,funcdef,sdslen(funcdef),"@user_script")) {
878         addReplyErrorFormat(c,"Error compiling script (new function): %s\n",
879             lua_tostring(lua,-1));
880         lua_pop(lua,1);
881         sdsfree(funcdef);
882         return REDIS_ERR;
883     }
884     sdsfree(funcdef);
885     if (lua_pcall(lua,0,0,0)) {
886         addReplyErrorFormat(c,"Error running script (new function): %s\n",
887             lua_tostring(lua,-1));
888         lua_pop(lua,1);
889         return REDIS_ERR;
890     }
891 
892     /* We also save a SHA1 -> Original script map in a dictionary
893      * so that we can replicate / write in the AOF all the
894      * EVALSHA commands as EVAL using the original script. */
895     {
896         int retval = dictAdd(server.lua_scripts,
897                              sdsnewlen(funcname+2,40),body);
898         redisAssertWithInfo(c,NULL,retval == DICT_OK);
899         incrRefCount(body);
900     }
901     return REDIS_OK;
902 }
903 
904 void evalGenericCommand(redisClient *c, int evalsha) {
905     lua_State *lua = server.lua;
906     char funcname[43];
907     long long numkeys;
908     int delhook = 0, err;
909 
910     /* We want the same PRNG sequence at every call so that our PRNG is
911      * not affected by external state. */
912     redisSrand48(0);
913 
914     /* We set this flag to zero to remember that so far no random command
915      * was called. This way we can allow the user to call commands like
916      * SRANDMEMBER or RANDOMKEY from Lua scripts as far as no write command
917      * is called (otherwise the replication and AOF would end with non
918      * deterministic sequences).
919      *
920      * Thanks to this flag we'll raise an error every time a write command
921      * is called after a random command was used. */
922     server.lua_random_dirty = 0;
923     server.lua_write_dirty = 0;
924 
925     /* Get the number of arguments that are keys */
926     if (getLongLongFromObjectOrReply(c,c->argv[2],&numkeys,NULL) != REDIS_OK)
927         return;
928     if (numkeys > (c->argc - 3)) {
929         addReplyError(c,"Number of keys can't be greater than number of args");
930         return;
931     } else if (numkeys < 0) {
932         addReplyError(c,"Number of keys can't be negative");
933         return;
934     }
935 
936     /* We obtain the script SHA1, then check if this function is already
937      * defined into the Lua state */
938     funcname[0] = 'f';
939     funcname[1] = '_';
940     if (!evalsha) {
941         /* Hash the code if this is an EVAL call */
942         sha1hex(funcname+2,c->argv[1]->ptr,sdslen(c->argv[1]->ptr));
943     } else {
944         /* We already have the SHA if it is a EVALSHA */
945         int j;
946         char *sha = c->argv[1]->ptr;
947 
948         /* Convert to lowercase. We don't use tolower since the function
949          * managed to always show up in the profiler output consuming
950          * a non trivial amount of time. */
951         for (j = 0; j < 40; j++)
952             funcname[j+2] = (sha[j] >= 'A' && sha[j] <= 'Z') ?
953                 sha[j]+('a'-'A') : sha[j];
954         funcname[42] = '\0';
955     }
956 
957     /* Push the pcall error handler function on the stack. */
958     lua_getglobal(lua, "__redis__err__handler");
959 
960     /* Try to lookup the Lua function */
961     lua_getglobal(lua, funcname);
962     if (lua_isnil(lua,-1)) {
963         lua_pop(lua,1); /* remove the nil from the stack */
964         /* Function not defined... let's define it if we have the
965          * body of the function. If this is an EVALSHA call we can just
966          * return an error. */
967         if (evalsha) {
968             lua_pop(lua,1); /* remove the error handler from the stack. */
969             addReply(c, shared.noscripterr);
970             return;
971         }
972         if (luaCreateFunction(c,lua,funcname,c->argv[1]) == REDIS_ERR) {
973             lua_pop(lua,1); /* remove the error handler from the stack. */
974             /* The error is sent to the client by luaCreateFunction()
975              * itself when it returns REDIS_ERR. */
976             return;
977         }
978         /* Now the following is guaranteed to return non nil */
979         lua_getglobal(lua, funcname);
980         redisAssert(!lua_isnil(lua,-1));
981     }
982 
983     /* Populate the argv and keys table accordingly to the arguments that
984      * EVAL received. */
985     luaSetGlobalArray(lua,"KEYS",c->argv+3,numkeys);
986     luaSetGlobalArray(lua,"ARGV",c->argv+3+numkeys,c->argc-3-numkeys);
987 
988     /* Select the right DB in the context of the Lua client */
989     selectDb(server.lua_client,c->db->id);
990 
991     /* Set a hook in order to be able to stop the script execution if it
992      * is running for too much time.
993      * We set the hook only if the time limit is enabled as the hook will
994      * make the Lua script execution slower. */
995     server.lua_caller = c;
996     server.lua_time_start = mstime();
997     server.lua_kill = 0;
998     if (server.lua_time_limit > 0 && server.masterhost == NULL) {
999         lua_sethook(lua,luaMaskCountHook,LUA_MASKCOUNT,100000);
1000         delhook = 1;
1001     }
1002 
1003     /* At this point whether this script was never seen before or if it was
1004      * already defined, we can call it. We have zero arguments and expect
1005      * a single return value. */
1006     err = lua_pcall(lua,0,1,-2);
1007 
1008     /* Perform some cleanup that we need to do both on error and success. */
1009     if (delhook) lua_sethook(lua,luaMaskCountHook,0,0); /* Disable hook */
1010     if (server.lua_timedout) {
1011         server.lua_timedout = 0;
1012         /* Restore the readable handler that was unregistered when the
1013          * script timeout was detected. */
1014         aeCreateFileEvent(server.el,c->fd,AE_READABLE,
1015                           readQueryFromClient,c);
1016     }
1017     server.lua_caller = NULL;
1018 
1019     /* Call the Lua garbage collector from time to time to avoid a
1020      * full cycle performed by Lua, which adds too latency.
1021      *
1022      * The call is performed every LUA_GC_CYCLE_PERIOD executed commands
1023      * (and for LUA_GC_CYCLE_PERIOD collection steps) because calling it
1024      * for every command uses too much CPU. */
1025     #define LUA_GC_CYCLE_PERIOD 50
1026     {
1027         static long gc_count = 0;
1028 
1029         gc_count++;
1030         if (gc_count == LUA_GC_CYCLE_PERIOD) {
1031             lua_gc(lua,LUA_GCSTEP,LUA_GC_CYCLE_PERIOD);
1032             gc_count = 0;
1033         }
1034     }
1035 
1036     if (err) {
1037         addReplyErrorFormat(c,"Error running script (call to %s): %s\n",
1038             funcname, lua_tostring(lua,-1));
1039         lua_pop(lua,2); /* Consume the Lua reply and remove error handler. */
1040     } else {
1041         /* On success convert the Lua return value into Redis protocol, and
1042          * send it to * the client. */
1043         luaReplyToRedisReply(c,lua); /* Convert and consume the reply. */
1044         lua_pop(lua,1); /* Remove the error handler. */
1045     }
1046 
1047     /* EVALSHA should be propagated to Slave and AOF file as full EVAL, unless
1048      * we are sure that the script was already in the context of all the
1049      * attached slaves *and* the current AOF file if enabled.
1050      *
1051      * To do so we use a cache of SHA1s of scripts that we already propagated
1052      * as full EVAL, that's called the Replication Script Cache.
1053      *
1054      * For repliation, everytime a new slave attaches to the master, we need to
1055      * flush our cache of scripts that can be replicated as EVALSHA, while
1056      * for AOF we need to do so every time we rewrite the AOF file. */
1057     if (evalsha) {
1058         if (!replicationScriptCacheExists(c->argv[1]->ptr)) {
1059             /* This script is not in our script cache, replicate it as
1060              * EVAL, then add it into the script cache, as from now on
1061              * slaves and AOF know about it. */
1062             robj *script = dictFetchValue(server.lua_scripts,c->argv[1]->ptr);
1063 
1064             replicationScriptCacheAdd(c->argv[1]->ptr);
1065             redisAssertWithInfo(c,NULL,script != NULL);
1066             rewriteClientCommandArgument(c,0,
1067                 resetRefCount(createStringObject("EVAL",4)));
1068             rewriteClientCommandArgument(c,1,script);
1069             forceCommandPropagation(c,REDIS_PROPAGATE_REPL|REDIS_PROPAGATE_AOF);
1070         }
1071     }
1072 }
1073 
1074 void evalCommand(redisClient *c) {
1075     evalGenericCommand(c,0);
1076 }
1077 
1078 void evalShaCommand(redisClient *c) {
1079     if (sdslen(c->argv[1]->ptr) != 40) {
1080         /* We know that a match is not possible if the provided SHA is
1081          * not the right length. So we return an error ASAP, this way
1082          * evalGenericCommand() can be implemented without string length
1083          * sanity check */
1084         addReply(c, shared.noscripterr);
1085         return;
1086     }
1087     evalGenericCommand(c,1);
1088 }
1089 
1090 /* We replace math.random() with our implementation that is not affected
1091  * by specific libc random() implementations and will output the same sequence
1092  * (for the same seed) in every arch. */
1093 
1094 /* The following implementation is the one shipped with Lua itself but with
1095  * rand() replaced by redisLrand48(). */
1096 int redis_math_random (lua_State *L) {
1097   /* the `%' avoids the (rare) case of r==1, and is needed also because on
1098      some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */
1099   lua_Number r = (lua_Number)(redisLrand48()%REDIS_LRAND48_MAX) /
1100                                 (lua_Number)REDIS_LRAND48_MAX;
1101   switch (lua_gettop(L)) {  /* check number of arguments */
1102     case 0: {  /* no arguments */
1103       lua_pushnumber(L, r);  /* Number between 0 and 1 */
1104       break;
1105     }
1106     case 1: {  /* only upper limit */
1107       int u = luaL_checkint(L, 1);
1108       luaL_argcheck(L, 1<=u, 1, "interval is empty");
1109       lua_pushnumber(L, floor(r*u)+1);  /* int between 1 and `u' */
1110       break;
1111     }
1112     case 2: {  /* lower and upper limits */
1113       int l = luaL_checkint(L, 1);
1114       int u = luaL_checkint(L, 2);
1115       luaL_argcheck(L, l<=u, 2, "interval is empty");
1116       lua_pushnumber(L, floor(r*(u-l+1))+l);  /* int between `l' and `u' */
1117       break;
1118     }
1119     default: return luaL_error(L, "wrong number of arguments");
1120   }
1121   return 1;
1122 }
1123 
1124 int redis_math_randomseed (lua_State *L) {
1125   redisSrand48(luaL_checkint(L, 1));
1126   return 0;
1127 }
1128 
1129 /* ---------------------------------------------------------------------------
1130  * SCRIPT command for script environment introspection and control
1131  * ------------------------------------------------------------------------- */
1132 
1133 void scriptCommand(redisClient *c) {
1134     if (c->argc == 2 && !strcasecmp(c->argv[1]->ptr,"flush")) {
1135         scriptingReset();
1136         addReply(c,shared.ok);
1137         replicationScriptCacheFlush();
1138         server.dirty++; /* Propagating this command is a good idea. */
1139     } else if (c->argc >= 2 && !strcasecmp(c->argv[1]->ptr,"exists")) {
1140         int j;
1141 
1142         addReplyMultiBulkLen(c, c->argc-2);
1143         for (j = 2; j < c->argc; j++) {
1144             if (dictFind(server.lua_scripts,c->argv[j]->ptr))
1145                 addReply(c,shared.cone);
1146             else
1147                 addReply(c,shared.czero);
1148         }
1149     } else if (c->argc == 3 && !strcasecmp(c->argv[1]->ptr,"load")) {
1150         char funcname[43];
1151         sds sha;
1152 
1153         funcname[0] = 'f';
1154         funcname[1] = '_';
1155         sha1hex(funcname+2,c->argv[2]->ptr,sdslen(c->argv[2]->ptr));
1156         sha = sdsnewlen(funcname+2,40);
1157         if (dictFind(server.lua_scripts,sha) == NULL) {
1158             if (luaCreateFunction(c,server.lua,funcname,c->argv[2])
1159                     == REDIS_ERR) {
1160                 sdsfree(sha);
1161                 return;
1162             }
1163         }
1164         addReplyBulkCBuffer(c,funcname+2,40);
1165         sdsfree(sha);
1166         forceCommandPropagation(c,REDIS_PROPAGATE_REPL|REDIS_PROPAGATE_AOF);
1167     } else if (c->argc == 2 && !strcasecmp(c->argv[1]->ptr,"kill")) {
1168         if (server.lua_caller == NULL) {
1169             addReplySds(c,sdsnew("-NOTBUSY No scripts in execution right now.\r\n"));
1170         } else if (server.lua_write_dirty) {
1171             addReplySds(c,sdsnew("-UNKILLABLE Sorry the script already executed write commands against the dataset. You can either wait the script termination or kill the server in a hard way using the SHUTDOWN NOSAVE command.\r\n"));
1172         } else {
1173             server.lua_kill = 1;
1174             addReply(c,shared.ok);
1175         }
1176     } else {
1177         addReplyError(c, "Unknown SCRIPT subcommand or wrong # of args.");
1178     }
1179 }
1180