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