Merge branch 'master' into blender2.8
[blender.git] / source / blender / python / intern / bpy_driver.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Willian P. Germano, Campbell Barton
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/python/intern/bpy_driver.c
24  *  \ingroup pythonintern
25  *
26  * This file defines the 'BPY_driver_exec' to execute python driver expressions,
27  * called by the animation system, there are also some utility functions
28  * to deal with the namespace used for driver execution.
29  */
30
31 /* ****************************************** */
32 /* Drivers - PyExpression Evaluation */
33
34 #include <Python.h>
35
36 #include "DNA_anim_types.h"
37
38 #include "BLI_listbase.h"
39 #include "BLI_math_base.h"
40 #include "BLI_string.h"
41
42 #include "BKE_fcurve.h"
43 #include "BKE_global.h"
44
45 #include "bpy_rna_driver.h"  /* for pyrna_driver_get_variable_value */
46
47 #include "bpy_intern_string.h"
48
49 #include "bpy_driver.h"
50
51 #include "BPY_extern.h"
52
53 extern void BPY_update_rna_module(void);
54
55 #define USE_RNA_AS_PYOBJECT
56
57 #define USE_BYTECODE_WHITELIST
58
59 #ifdef USE_BYTECODE_WHITELIST
60 #  include <opcode.h>
61 #endif
62
63 /* for pydrivers (drivers using one-line Python expressions to express relationships between targets) */
64 PyObject *bpy_pydriver_Dict = NULL;
65
66 #ifdef USE_BYTECODE_WHITELIST
67 static PyObject *bpy_pydriver_Dict__whitelist = NULL;
68 #endif
69
70 /* For faster execution we keep a special dictionary for pydrivers, with
71  * the needed modules and aliases.
72  */
73 int bpy_pydriver_create_dict(void)
74 {
75         PyObject *d, *mod;
76
77         /* validate namespace for driver evaluation */
78         if (bpy_pydriver_Dict) return -1;
79
80         d = PyDict_New();
81         if (d == NULL)
82                 return -1;
83         else
84                 bpy_pydriver_Dict = d;
85
86         /* import some modules: builtins, bpy, math, (Blender.noise)*/
87         PyDict_SetItemString(d, "__builtins__", PyEval_GetBuiltins());
88
89         mod = PyImport_ImportModule("math");
90         if (mod) {
91                 PyDict_Merge(d, PyModule_GetDict(mod), 0); /* 0 - don't overwrite existing values */
92                 Py_DECREF(mod);
93         }
94 #ifdef USE_BYTECODE_WHITELIST
95         PyObject *mod_math = mod;
96 #endif
97
98         /* add bpy to global namespace */
99         mod = PyImport_ImportModuleLevel("bpy", NULL, NULL, NULL, 0);
100         if (mod) {
101                 PyDict_SetItemString(bpy_pydriver_Dict, "bpy", mod);
102                 Py_DECREF(mod);
103         }
104
105         /* add noise to global namespace */
106         mod = PyImport_ImportModuleLevel("mathutils", NULL, NULL, NULL, 0);
107         if (mod) {
108                 PyObject *modsub = PyDict_GetItemString(PyModule_GetDict(mod), "noise");
109                 PyDict_SetItemString(bpy_pydriver_Dict, "noise", modsub);
110                 Py_DECREF(mod);
111         }
112
113 #ifdef USE_BYTECODE_WHITELIST
114         /* setup the whitelist */
115         {
116                 bpy_pydriver_Dict__whitelist = PyDict_New();
117                 const char *whitelist[] = {
118                         /* builtins (basic) */
119                         "all",
120                         "any",
121                         "len",
122                         /* builtins (numeric) */
123                         "max",
124                         "min",
125                         "pow",
126                         "round",
127                         "sum",
128                         /* types */
129                         "bool",
130                         "float",
131                         "int",
132
133                         NULL,
134                 };
135
136                 for (int i = 0; whitelist[i]; i++) {
137                         PyDict_SetItemString(bpy_pydriver_Dict__whitelist, whitelist[i], Py_None);
138                 }
139
140                 /* Add all of 'math' functions. */
141                 if (mod_math != NULL) {
142                         PyObject *mod_math_dict = PyModule_GetDict(mod_math);
143                         PyObject *arg_key, *arg_value;
144                         Py_ssize_t arg_pos = 0;
145                         while (PyDict_Next(mod_math_dict, &arg_pos, &arg_key, &arg_value)) {
146                                 const char *arg_str = _PyUnicode_AsString(arg_key);
147                                 if (arg_str[0] && arg_str[1] != '_') {
148                                         PyDict_SetItem(bpy_pydriver_Dict__whitelist, arg_key, Py_None);
149                                 }
150                         }
151                 }
152         }
153 #endif  /* USE_BYTECODE_WHITELIST */
154
155         return 0;
156 }
157
158 /* note, this function should do nothing most runs, only when changing frame */
159 /* not thread safe but neither is python */
160 static struct {
161         float evaltime;
162
163         /* borrowed reference to the 'self' in 'bpy_pydriver_Dict'
164          * keep for as long as the same self is used. */
165         PyObject *self;
166 } g_pydriver_state_prev = {
167         .evaltime = FLT_MAX,
168         .self = NULL,
169 };
170
171 static void bpy_pydriver_namespace_update_frame(const float evaltime)
172 {
173         if (g_pydriver_state_prev.evaltime != evaltime) {
174                 PyObject *item = PyFloat_FromDouble(evaltime);
175                 PyDict_SetItem(bpy_pydriver_Dict, bpy_intern_str_frame, item);
176                 Py_DECREF(item);
177
178                 g_pydriver_state_prev.evaltime = evaltime;
179         }
180 }
181
182 static void bpy_pydriver_namespace_update_self(struct PathResolvedRNA *anim_rna)
183 {
184         if ((g_pydriver_state_prev.self == NULL) ||
185             (pyrna_driver_is_equal_anim_rna(anim_rna, g_pydriver_state_prev.self) == false))
186         {
187                 PyObject *item = pyrna_driver_self_from_anim_rna(anim_rna);
188                 PyDict_SetItem(bpy_pydriver_Dict, bpy_intern_str_self, item);
189                 Py_DECREF(item);
190
191                 g_pydriver_state_prev.self = item;
192         }
193 }
194
195 static void bpy_pydriver_namespace_clear_self(void)
196 {
197         if (g_pydriver_state_prev.self) {
198                 PyDict_DelItem(bpy_pydriver_Dict, bpy_intern_str_self);
199
200                 g_pydriver_state_prev.self = NULL;
201         }
202 }
203
204 /* Update function, it gets rid of pydrivers global dictionary, forcing
205  * BPY_driver_exec to recreate it. This function is used to force
206  * reloading the Blender text module "pydrivers.py", if available, so
207  * updates in it reach pydriver evaluation.
208  */
209 void BPY_driver_reset(void)
210 {
211         PyGILState_STATE gilstate;
212         bool use_gil = true; /* !PyC_IsInterpreterActive(); */
213
214         if (use_gil)
215                 gilstate = PyGILState_Ensure();
216
217         if (bpy_pydriver_Dict) { /* free the global dict used by pydrivers */
218                 PyDict_Clear(bpy_pydriver_Dict);
219                 Py_DECREF(bpy_pydriver_Dict);
220                 bpy_pydriver_Dict = NULL;
221         }
222
223 #ifdef USE_BYTECODE_WHITELIST
224         if (bpy_pydriver_Dict__whitelist) {
225                 PyDict_Clear(bpy_pydriver_Dict__whitelist);
226                 Py_DECREF(bpy_pydriver_Dict__whitelist);
227                 bpy_pydriver_Dict__whitelist = NULL;
228         }
229 #endif
230
231         g_pydriver_state_prev.evaltime = FLT_MAX;
232
233         /* freed when clearing driver dict */
234         g_pydriver_state_prev.self = NULL;
235
236         if (use_gil)
237                 PyGILState_Release(gilstate);
238
239         return;
240 }
241
242 /* error return function for BPY_eval_pydriver */
243 static void pydriver_error(ChannelDriver *driver)
244 {
245         driver->flag |= DRIVER_FLAG_INVALID; /* py expression failed */
246         fprintf(stderr, "\nError in Driver: The following Python expression failed:\n\t'%s'\n\n", driver->expression);
247
248         // BPy_errors_to_report(NULL); // TODO - reports
249         PyErr_Print();
250         PyErr_Clear();
251 }
252
253 #ifdef USE_BYTECODE_WHITELIST
254
255 #define OK_OP(op) [op] = 1
256
257 const char secure_opcodes[255] = {
258         OK_OP(0),
259         OK_OP(POP_TOP),
260         OK_OP(ROT_TWO),
261         OK_OP(ROT_THREE),
262         OK_OP(DUP_TOP),
263         OK_OP(DUP_TOP_TWO),
264         OK_OP(NOP),
265         OK_OP(UNARY_POSITIVE),
266         OK_OP(UNARY_NEGATIVE),
267         OK_OP(UNARY_NOT),
268         OK_OP(UNARY_INVERT),
269         OK_OP(BINARY_MATRIX_MULTIPLY),
270         OK_OP(INPLACE_MATRIX_MULTIPLY),
271         OK_OP(BINARY_POWER),
272         OK_OP(BINARY_MULTIPLY),
273         OK_OP(BINARY_MODULO),
274         OK_OP(BINARY_ADD),
275         OK_OP(BINARY_SUBTRACT),
276         OK_OP(BINARY_SUBSCR),
277         OK_OP(BINARY_FLOOR_DIVIDE),
278         OK_OP(BINARY_TRUE_DIVIDE),
279         OK_OP(INPLACE_FLOOR_DIVIDE),
280         OK_OP(INPLACE_TRUE_DIVIDE),
281         OK_OP(INPLACE_ADD),
282         OK_OP(INPLACE_SUBTRACT),
283         OK_OP(INPLACE_MULTIPLY),
284         OK_OP(INPLACE_MODULO),
285         OK_OP(BINARY_LSHIFT),
286         OK_OP(BINARY_RSHIFT),
287         OK_OP(BINARY_AND),
288         OK_OP(BINARY_XOR),
289         OK_OP(BINARY_OR),
290         OK_OP(INPLACE_POWER),
291         OK_OP(INPLACE_LSHIFT),
292         OK_OP(INPLACE_RSHIFT),
293         OK_OP(INPLACE_AND),
294         OK_OP(INPLACE_XOR),
295         OK_OP(INPLACE_OR),
296         OK_OP(RETURN_VALUE),
297         OK_OP(BUILD_TUPLE),
298         OK_OP(BUILD_LIST),
299         OK_OP(BUILD_SET),
300         OK_OP(BUILD_MAP),
301         OK_OP(COMPARE_OP),
302         OK_OP(JUMP_FORWARD),
303         OK_OP(JUMP_IF_FALSE_OR_POP),
304         OK_OP(JUMP_IF_TRUE_OR_POP),
305         OK_OP(JUMP_ABSOLUTE),
306         OK_OP(POP_JUMP_IF_FALSE),
307         OK_OP(POP_JUMP_IF_TRUE),
308         OK_OP(LOAD_GLOBAL),
309         OK_OP(LOAD_FAST),
310         OK_OP(STORE_FAST),
311         OK_OP(DELETE_FAST),
312         OK_OP(LOAD_DEREF),
313         OK_OP(STORE_DEREF),
314
315         /* special cases */
316         OK_OP(LOAD_CONST),         /* ok because constants are accepted */
317         OK_OP(LOAD_NAME),          /* ok, because PyCodeObject.names is checked */
318         OK_OP(CALL_FUNCTION),      /* ok, because we check its 'name' before calling */
319         OK_OP(CALL_FUNCTION_KW),
320         OK_OP(CALL_FUNCTION_EX),
321 };
322
323 #undef OK_OP
324
325 static bool bpy_driver_secure_bytecode_validate(PyObject *expr_code, PyObject *dict_arr[])
326 {
327         PyCodeObject *py_code = (PyCodeObject *)expr_code;
328
329         /* Check names. */
330         {
331                 for (int i = 0; i < PyTuple_GET_SIZE(py_code->co_names); i++) {
332                         PyObject *name = PyTuple_GET_ITEM(py_code->co_names, i);
333
334                         bool contains_name = false;
335                         for (int j = 0; dict_arr[j]; j++) {
336                                 if (PyDict_Contains(dict_arr[j], name)) {
337                                         contains_name = true;
338                                         break;
339                                 }
340                         }
341
342                         if (contains_name == false) {
343                                 fprintf(stderr, "\tBPY_driver_eval() - restructed access disallows name '%s', "
344                                                 "enable auto-execution to support\n", _PyUnicode_AsString(name));
345                                 return false;
346                         }
347                 }
348         }
349
350         /* Check opcodes. */
351         {
352                 const char *codestr;
353                 Py_ssize_t  code_len;
354
355                 PyBytes_AsStringAndSize(py_code->co_code, (char **)&codestr, &code_len);
356
357 #define CODESIZE(op) (HAS_ARG(op) ? 3 : 1)
358
359                 for (Py_ssize_t i = 0; i < code_len; i += CODESIZE(codestr[i])) {
360                         const int opcode = codestr[i];
361                         if (secure_opcodes[opcode] == 0) {
362                                 fprintf(stderr, "\tBPY_driver_eval() - restructed access disallows opcode '%d', "
363                                                 "enable auto-execution to support\n", opcode);
364                                 return false;
365                         }
366                 }
367
368 #undef CODESIZE
369         }
370
371         return true;
372 }
373
374 #endif  /* USE_BYTECODE_WHITELIST */
375
376
377 /* This evals py driver expressions, 'expr' is a Python expression that
378  * should evaluate to a float number, which is returned.
379  *
380  * (old)note: PyGILState_Ensure() isn't always called because python can call
381  * the bake operator which intern starts a thread which calls scene update
382  * which does a driver update. to avoid a deadlock check PyC_IsInterpreterActive()
383  * if PyGILState_Ensure() is needed - see [#27683]
384  *
385  * (new)note: checking if python is running is not threadsafe [#28114]
386  * now release the GIL on python operator execution instead, using
387  * PyEval_SaveThread() / PyEval_RestoreThread() so we don't lock up blender.
388  *
389  * For copy-on-write we always cache expressions and write errors in the
390  * original driver, otherwise these would get freed while editing. Due to
391  * the GIL this is thread-safe.
392  */
393 float BPY_driver_exec(struct PathResolvedRNA *anim_rna, ChannelDriver *driver, ChannelDriver *driver_orig, const float evaltime)
394 {
395         PyObject *driver_vars = NULL;
396         PyObject *retval = NULL;
397         PyObject *expr_vars; /* speed up by pre-hashing string & avoids re-converting unicode strings for every execution */
398         PyObject *expr_code;
399         PyGILState_STATE gilstate;
400         bool use_gil;
401
402         DriverVar *dvar;
403         double result = 0.0; /* default return */
404         const char *expr;
405         short targets_ok = 1;
406         int i;
407
408         /* get the py expression to be evaluated */
409         expr = driver_orig->expression;
410         if (expr[0] == '\0')
411                 return 0.0f;
412
413 #ifndef USE_BYTECODE_WHITELIST
414         if (!(G.f & G_SCRIPT_AUTOEXEC)) {
415                 if (!(G.f & G_SCRIPT_AUTOEXEC_FAIL_QUIET)) {
416                         G.f |= G_SCRIPT_AUTOEXEC_FAIL;
417                         BLI_snprintf(G.autoexec_fail, sizeof(G.autoexec_fail), "Driver '%s'", expr);
418
419                         printf("skipping driver '%s', automatic scripts are disabled\n", expr);
420                 }
421                 return 0.0f;
422         }
423 #else
424         bool is_recompile = false;
425 #endif
426
427         use_gil = true;  /* !PyC_IsInterpreterActive(); */
428
429         if (use_gil)
430                 gilstate = PyGILState_Ensure();
431
432         /* needed since drivers are updated directly after undo where 'main' is
433          * re-allocated [#28807] */
434         BPY_update_rna_module();
435
436         /* init global dictionary for py-driver evaluation settings */
437         if (!bpy_pydriver_Dict) {
438                 if (bpy_pydriver_create_dict() != 0) {
439                         fprintf(stderr, "PyDriver error: couldn't create Python dictionary\n");
440                         if (use_gil)
441                                 PyGILState_Release(gilstate);
442                         return 0.0f;
443                 }
444         }
445
446         /* update global namespace */
447         bpy_pydriver_namespace_update_frame(evaltime);
448
449         if (driver_orig->flag & DRIVER_FLAG_USE_SELF) {
450                 bpy_pydriver_namespace_update_self(anim_rna);
451         }
452         else {
453                 bpy_pydriver_namespace_clear_self();
454         }
455
456         if (driver_orig->expr_comp == NULL)
457                 driver_orig->flag |= DRIVER_FLAG_RECOMPILE;
458
459         /* compile the expression first if it hasn't been compiled or needs to be rebuilt */
460         if (driver_orig->flag & DRIVER_FLAG_RECOMPILE) {
461                 Py_XDECREF(driver_orig->expr_comp);
462                 driver_orig->expr_comp = PyTuple_New(2);
463
464                 expr_code = Py_CompileString(expr, "<bpy driver>", Py_eval_input);
465                 PyTuple_SET_ITEM(((PyObject *)driver_orig->expr_comp), 0, expr_code);
466
467                 driver_orig->flag &= ~DRIVER_FLAG_RECOMPILE;
468                 driver_orig->flag |= DRIVER_FLAG_RENAMEVAR; /* maybe this can be removed but for now best keep until were sure */
469 #ifdef USE_BYTECODE_WHITELIST
470                 is_recompile = true;
471 #endif
472         }
473         else {
474                 expr_code = PyTuple_GET_ITEM(((PyObject *)driver_orig->expr_comp), 0);
475         }
476
477         if (driver_orig->flag & DRIVER_FLAG_RENAMEVAR) {
478                 /* may not be set */
479                 expr_vars = PyTuple_GET_ITEM(((PyObject *)driver_orig->expr_comp), 1);
480                 Py_XDECREF(expr_vars);
481
482                 expr_vars = PyTuple_New(BLI_listbase_count(&driver_orig->variables));
483                 PyTuple_SET_ITEM(((PyObject *)driver_orig->expr_comp), 1, expr_vars);
484
485                 for (dvar = driver_orig->variables.first, i = 0; dvar; dvar = dvar->next) {
486                         PyTuple_SET_ITEM(expr_vars, i++, PyUnicode_FromString(dvar->name));
487                 }
488
489                 driver_orig->flag &= ~DRIVER_FLAG_RENAMEVAR;
490         }
491         else {
492                 expr_vars = PyTuple_GET_ITEM(((PyObject *)driver_orig->expr_comp), 1);
493         }
494
495         /* add target values to a dict that will be used as '__locals__' dict */
496         driver_vars = _PyDict_NewPresized(PyTuple_GET_SIZE(expr_vars));
497         for (dvar = driver->variables.first, i = 0; dvar; dvar = dvar->next) {
498                 PyObject *driver_arg = NULL;
499
500         /* support for any RNA data */
501 #ifdef USE_RNA_AS_PYOBJECT
502                 if (dvar->type == DVAR_TYPE_SINGLE_PROP) {
503                         driver_arg = pyrna_driver_get_variable_value(driver, &dvar->targets[0]);
504
505                         if (driver_arg == NULL) {
506                                 driver_arg = PyFloat_FromDouble(0.0);
507                                 dvar->curval = 0.0f;
508                         }
509                         else {
510                                 /* no need to worry about overflow here, values from RNA are within limits. */
511                                 if (PyFloat_CheckExact(driver_arg)) {
512                                         dvar->curval = (float)PyFloat_AsDouble(driver_arg);
513                                 }
514                                 else if (PyLong_CheckExact(driver_arg)) {
515                                         dvar->curval = (float)PyLong_AsLong(driver_arg);
516                                 }
517                                 else if (PyBool_Check(driver_arg)) {
518                                         dvar->curval = (driver_arg == Py_True);
519                                 }
520                                 else {
521                                         dvar->curval = 0.0f;
522                                 }
523                         }
524                 }
525                 else
526 #endif
527                 {
528                         /* try to get variable value */
529                         float tval = driver_get_variable_value(driver, dvar);
530                         driver_arg = PyFloat_FromDouble((double)tval);
531                 }
532
533                 /* try to add to dictionary */
534                 /* if (PyDict_SetItemString(driver_vars, dvar->name, driver_arg)) { */
535                 if (PyDict_SetItem(driver_vars, PyTuple_GET_ITEM(expr_vars, i++), driver_arg) != -1) {
536                         Py_DECREF(driver_arg);
537                 }
538                 else {
539                         /* this target failed - bad name */
540                         if (targets_ok) {
541                                 /* first one - print some extra info for easier identification */
542                                 fprintf(stderr, "\nBPY_driver_eval() - Error while evaluating PyDriver:\n");
543                                 targets_ok = 0;
544                         }
545
546                         fprintf(stderr, "\tBPY_driver_eval() - couldn't add variable '%s' to namespace\n", dvar->name);
547                         // BPy_errors_to_report(NULL); // TODO - reports
548                         PyErr_Print();
549                         PyErr_Clear();
550                 }
551         }
552
553 #ifdef USE_BYTECODE_WHITELIST
554         if (is_recompile) {
555                 if (!(G.f & G_SCRIPT_AUTOEXEC)) {
556                         if (!bpy_driver_secure_bytecode_validate(
557                                     expr_code, (PyObject *[]){
558                                         bpy_pydriver_Dict,
559                                         bpy_pydriver_Dict__whitelist,
560                                         driver_vars,
561                                         NULL,}
562                                 ))
563                         {
564                                 Py_DECREF(expr_code);
565                                 expr_code = NULL;
566                                 PyTuple_SET_ITEM(((PyObject *)driver->expr_comp), 0, NULL);
567                         }
568                 }
569         }
570 #endif  /* USE_BYTECODE_WHITELIST */
571
572 #if 0  /* slow, with this can avoid all Py_CompileString above. */
573         /* execute expression to get a value */
574         retval = PyRun_String(expr, Py_eval_input, bpy_pydriver_Dict, driver_vars);
575 #else
576         /* evaluate the compiled expression */
577         if (expr_code)
578                 retval = PyEval_EvalCode((void *)expr_code, bpy_pydriver_Dict, driver_vars);
579 #endif
580
581         /* decref the driver vars first...  */
582         Py_DECREF(driver_vars);
583
584         /* process the result */
585         if (retval == NULL) {
586                 pydriver_error(driver);
587         }
588         else if ((result = PyFloat_AsDouble(retval)) == -1.0 && PyErr_Occurred()) {
589                 pydriver_error(driver);
590                 Py_DECREF(retval);
591                 result = 0.0;
592         }
593         else {
594                 /* all fine, make sure the "invalid expression" flag is cleared */
595                 driver->flag &= ~DRIVER_FLAG_INVALID;
596                 Py_DECREF(retval);
597         }
598
599         if (use_gil)
600                 PyGILState_Release(gilstate);
601
602         if (isfinite(result)) {
603                 return (float)result;
604         }
605         else {
606                 fprintf(stderr, "\tBPY_driver_eval() - driver '%s' evaluates to '%f'\n", dvar->name, result);
607                 return 0.0f;
608         }
609 }