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 static const char secure_opcodes[255] = {
258         OK_OP(POP_TOP),
259         OK_OP(ROT_TWO),
260         OK_OP(ROT_THREE),
261         OK_OP(DUP_TOP),
262         OK_OP(DUP_TOP_TWO),
263         OK_OP(NOP),
264         OK_OP(UNARY_POSITIVE),
265         OK_OP(UNARY_NEGATIVE),
266         OK_OP(UNARY_NOT),
267         OK_OP(UNARY_INVERT),
268         OK_OP(BINARY_MATRIX_MULTIPLY),
269         OK_OP(INPLACE_MATRIX_MULTIPLY),
270         OK_OP(BINARY_POWER),
271         OK_OP(BINARY_MULTIPLY),
272         OK_OP(BINARY_MODULO),
273         OK_OP(BINARY_ADD),
274         OK_OP(BINARY_SUBTRACT),
275         OK_OP(BINARY_SUBSCR),
276         OK_OP(BINARY_FLOOR_DIVIDE),
277         OK_OP(BINARY_TRUE_DIVIDE),
278         OK_OP(INPLACE_FLOOR_DIVIDE),
279         OK_OP(INPLACE_TRUE_DIVIDE),
280         OK_OP(INPLACE_ADD),
281         OK_OP(INPLACE_SUBTRACT),
282         OK_OP(INPLACE_MULTIPLY),
283         OK_OP(INPLACE_MODULO),
284         OK_OP(BINARY_LSHIFT),
285         OK_OP(BINARY_RSHIFT),
286         OK_OP(BINARY_AND),
287         OK_OP(BINARY_XOR),
288         OK_OP(BINARY_OR),
289         OK_OP(INPLACE_POWER),
290         OK_OP(INPLACE_LSHIFT),
291         OK_OP(INPLACE_RSHIFT),
292         OK_OP(INPLACE_AND),
293         OK_OP(INPLACE_XOR),
294         OK_OP(INPLACE_OR),
295         OK_OP(RETURN_VALUE),
296         OK_OP(BUILD_TUPLE),
297         OK_OP(BUILD_LIST),
298         OK_OP(BUILD_SET),
299         OK_OP(BUILD_MAP),
300         OK_OP(COMPARE_OP),
301         OK_OP(JUMP_FORWARD),
302         OK_OP(JUMP_IF_FALSE_OR_POP),
303         OK_OP(JUMP_IF_TRUE_OR_POP),
304         OK_OP(JUMP_ABSOLUTE),
305         OK_OP(POP_JUMP_IF_FALSE),
306         OK_OP(POP_JUMP_IF_TRUE),
307         OK_OP(LOAD_GLOBAL),
308         OK_OP(LOAD_FAST),
309         OK_OP(STORE_FAST),
310         OK_OP(DELETE_FAST),
311         OK_OP(LOAD_DEREF),
312         OK_OP(STORE_DEREF),
313
314         /* special cases */
315         OK_OP(LOAD_CONST),         /* ok because constants are accepted */
316         OK_OP(LOAD_NAME),          /* ok, because PyCodeObject.names is checked */
317         OK_OP(CALL_FUNCTION),      /* ok, because we check its 'name' before calling */
318         OK_OP(CALL_FUNCTION_KW),
319         OK_OP(CALL_FUNCTION_EX),
320 };
321
322 #undef OK_OP
323
324 static bool bpy_driver_secure_bytecode_validate(PyObject *expr_code, PyObject *dict_arr[])
325 {
326         PyCodeObject *py_code = (PyCodeObject *)expr_code;
327
328         /* Check names. */
329         {
330                 for (int i = 0; i < PyTuple_GET_SIZE(py_code->co_names); i++) {
331                         PyObject *name = PyTuple_GET_ITEM(py_code->co_names, i);
332
333                         bool contains_name = false;
334                         for (int j = 0; dict_arr[j]; j++) {
335                                 if (PyDict_Contains(dict_arr[j], name)) {
336                                         contains_name = true;
337                                         break;
338                                 }
339                         }
340
341                         if (contains_name == false) {
342                                 fprintf(stderr, "\tBPY_driver_eval() - restructed access disallows name '%s', "
343                                                 "enable auto-execution to support\n", _PyUnicode_AsString(name));
344                                 return false;
345                         }
346                 }
347         }
348
349         /* Check opcodes. */
350         {
351                 const _Py_CODEUNIT *codestr;
352                 Py_ssize_t  code_len;
353
354                 PyBytes_AsStringAndSize(py_code->co_code, (char **)&codestr, &code_len);
355                 code_len /= sizeof(*codestr);
356
357                 for (Py_ssize_t i = 0; i < code_len; i++) {
358                         const int opcode = _Py_OPCODE(codestr[i]);
359                         if (secure_opcodes[opcode] == 0) {
360                                 fprintf(stderr, "\tBPY_driver_eval() - restructed access disallows opcode '%d', "
361                                                 "enable auto-execution to support\n", opcode);
362                                 return false;
363                         }
364                 }
365
366 #undef CODESIZE
367         }
368
369         return true;
370 }
371
372 #endif  /* USE_BYTECODE_WHITELIST */
373
374
375 /* This evals py driver expressions, 'expr' is a Python expression that
376  * should evaluate to a float number, which is returned.
377  *
378  * (old)note: PyGILState_Ensure() isn't always called because python can call
379  * the bake operator which intern starts a thread which calls scene update
380  * which does a driver update. to avoid a deadlock check PyC_IsInterpreterActive()
381  * if PyGILState_Ensure() is needed - see [#27683]
382  *
383  * (new)note: checking if python is running is not threadsafe [#28114]
384  * now release the GIL on python operator execution instead, using
385  * PyEval_SaveThread() / PyEval_RestoreThread() so we don't lock up blender.
386  *
387  * For copy-on-write we always cache expressions and write errors in the
388  * original driver, otherwise these would get freed while editing. Due to
389  * the GIL this is thread-safe.
390  */
391 float BPY_driver_exec(struct PathResolvedRNA *anim_rna, ChannelDriver *driver, ChannelDriver *driver_orig, const float evaltime)
392 {
393         PyObject *driver_vars = NULL;
394         PyObject *retval = NULL;
395         PyObject *expr_vars; /* speed up by pre-hashing string & avoids re-converting unicode strings for every execution */
396         PyObject *expr_code;
397         PyGILState_STATE gilstate;
398         bool use_gil;
399
400         DriverVar *dvar;
401         double result = 0.0; /* default return */
402         const char *expr;
403         short targets_ok = 1;
404         int i;
405
406         /* get the py expression to be evaluated */
407         expr = driver_orig->expression;
408         if (expr[0] == '\0')
409                 return 0.0f;
410
411 #ifndef USE_BYTECODE_WHITELIST
412         if (!(G.f & G_SCRIPT_AUTOEXEC)) {
413                 if (!(G.f & G_SCRIPT_AUTOEXEC_FAIL_QUIET)) {
414                         G.f |= G_SCRIPT_AUTOEXEC_FAIL;
415                         BLI_snprintf(G.autoexec_fail, sizeof(G.autoexec_fail), "Driver '%s'", expr);
416
417                         printf("skipping driver '%s', automatic scripts are disabled\n", expr);
418                 }
419                 return 0.0f;
420         }
421 #else
422         bool is_recompile = false;
423 #endif
424
425         use_gil = true;  /* !PyC_IsInterpreterActive(); */
426
427         if (use_gil)
428                 gilstate = PyGILState_Ensure();
429
430         /* needed since drivers are updated directly after undo where 'main' is
431          * re-allocated [#28807] */
432         BPY_update_rna_module();
433
434         /* init global dictionary for py-driver evaluation settings */
435         if (!bpy_pydriver_Dict) {
436                 if (bpy_pydriver_create_dict() != 0) {
437                         fprintf(stderr, "PyDriver error: couldn't create Python dictionary\n");
438                         if (use_gil)
439                                 PyGILState_Release(gilstate);
440                         return 0.0f;
441                 }
442         }
443
444         /* update global namespace */
445         bpy_pydriver_namespace_update_frame(evaltime);
446
447         if (driver_orig->flag & DRIVER_FLAG_USE_SELF) {
448                 bpy_pydriver_namespace_update_self(anim_rna);
449         }
450         else {
451                 bpy_pydriver_namespace_clear_self();
452         }
453
454         if (driver_orig->expr_comp == NULL)
455                 driver_orig->flag |= DRIVER_FLAG_RECOMPILE;
456
457         /* compile the expression first if it hasn't been compiled or needs to be rebuilt */
458         if (driver_orig->flag & DRIVER_FLAG_RECOMPILE) {
459                 Py_XDECREF(driver_orig->expr_comp);
460                 driver_orig->expr_comp = PyTuple_New(2);
461
462                 expr_code = Py_CompileString(expr, "<bpy driver>", Py_eval_input);
463                 PyTuple_SET_ITEM(((PyObject *)driver_orig->expr_comp), 0, expr_code);
464
465                 driver_orig->flag &= ~DRIVER_FLAG_RECOMPILE;
466                 driver_orig->flag |= DRIVER_FLAG_RENAMEVAR; /* maybe this can be removed but for now best keep until were sure */
467 #ifdef USE_BYTECODE_WHITELIST
468                 is_recompile = true;
469 #endif
470         }
471         else {
472                 expr_code = PyTuple_GET_ITEM(((PyObject *)driver_orig->expr_comp), 0);
473         }
474
475         if (driver_orig->flag & DRIVER_FLAG_RENAMEVAR) {
476                 /* may not be set */
477                 expr_vars = PyTuple_GET_ITEM(((PyObject *)driver_orig->expr_comp), 1);
478                 Py_XDECREF(expr_vars);
479
480                 expr_vars = PyTuple_New(BLI_listbase_count(&driver_orig->variables));
481                 PyTuple_SET_ITEM(((PyObject *)driver_orig->expr_comp), 1, expr_vars);
482
483                 for (dvar = driver_orig->variables.first, i = 0; dvar; dvar = dvar->next) {
484                         PyTuple_SET_ITEM(expr_vars, i++, PyUnicode_FromString(dvar->name));
485                 }
486
487                 driver_orig->flag &= ~DRIVER_FLAG_RENAMEVAR;
488         }
489         else {
490                 expr_vars = PyTuple_GET_ITEM(((PyObject *)driver_orig->expr_comp), 1);
491         }
492
493         /* add target values to a dict that will be used as '__locals__' dict */
494         driver_vars = _PyDict_NewPresized(PyTuple_GET_SIZE(expr_vars));
495         for (dvar = driver->variables.first, i = 0; dvar; dvar = dvar->next) {
496                 PyObject *driver_arg = NULL;
497
498         /* support for any RNA data */
499 #ifdef USE_RNA_AS_PYOBJECT
500                 if (dvar->type == DVAR_TYPE_SINGLE_PROP) {
501                         driver_arg = pyrna_driver_get_variable_value(driver, &dvar->targets[0]);
502
503                         if (driver_arg == NULL) {
504                                 driver_arg = PyFloat_FromDouble(0.0);
505                                 dvar->curval = 0.0f;
506                         }
507                         else {
508                                 /* no need to worry about overflow here, values from RNA are within limits. */
509                                 if (PyFloat_CheckExact(driver_arg)) {
510                                         dvar->curval = (float)PyFloat_AsDouble(driver_arg);
511                                 }
512                                 else if (PyLong_CheckExact(driver_arg)) {
513                                         dvar->curval = (float)PyLong_AsLong(driver_arg);
514                                 }
515                                 else if (PyBool_Check(driver_arg)) {
516                                         dvar->curval = (driver_arg == Py_True);
517                                 }
518                                 else {
519                                         dvar->curval = 0.0f;
520                                 }
521                         }
522                 }
523                 else
524 #endif
525                 {
526                         /* try to get variable value */
527                         float tval = driver_get_variable_value(driver, dvar);
528                         driver_arg = PyFloat_FromDouble((double)tval);
529                 }
530
531                 /* try to add to dictionary */
532                 /* if (PyDict_SetItemString(driver_vars, dvar->name, driver_arg)) { */
533                 if (PyDict_SetItem(driver_vars, PyTuple_GET_ITEM(expr_vars, i++), driver_arg) != -1) {
534                         Py_DECREF(driver_arg);
535                 }
536                 else {
537                         /* this target failed - bad name */
538                         if (targets_ok) {
539                                 /* first one - print some extra info for easier identification */
540                                 fprintf(stderr, "\nBPY_driver_eval() - Error while evaluating PyDriver:\n");
541                                 targets_ok = 0;
542                         }
543
544                         fprintf(stderr, "\tBPY_driver_eval() - couldn't add variable '%s' to namespace\n", dvar->name);
545                         // BPy_errors_to_report(NULL); // TODO - reports
546                         PyErr_Print();
547                         PyErr_Clear();
548                 }
549         }
550
551 #ifdef USE_BYTECODE_WHITELIST
552         if (is_recompile) {
553                 if (!(G.f & G_SCRIPT_AUTOEXEC)) {
554                         if (!bpy_driver_secure_bytecode_validate(
555                                     expr_code, (PyObject *[]){
556                                         bpy_pydriver_Dict,
557                                         bpy_pydriver_Dict__whitelist,
558                                         driver_vars,
559                                         NULL,}
560                                 ))
561                         {
562                                 Py_DECREF(expr_code);
563                                 expr_code = NULL;
564                                 PyTuple_SET_ITEM(((PyObject *)driver_orig->expr_comp), 0, NULL);
565                         }
566                 }
567         }
568 #endif  /* USE_BYTECODE_WHITELIST */
569
570 #if 0  /* slow, with this can avoid all Py_CompileString above. */
571         /* execute expression to get a value */
572         retval = PyRun_String(expr, Py_eval_input, bpy_pydriver_Dict, driver_vars);
573 #else
574         /* evaluate the compiled expression */
575         if (expr_code)
576                 retval = PyEval_EvalCode((void *)expr_code, bpy_pydriver_Dict, driver_vars);
577 #endif
578
579         /* decref the driver vars first...  */
580         Py_DECREF(driver_vars);
581
582         /* process the result */
583         if (retval == NULL) {
584                 pydriver_error(driver);
585         }
586         else if ((result = PyFloat_AsDouble(retval)) == -1.0 && PyErr_Occurred()) {
587                 pydriver_error(driver);
588                 Py_DECREF(retval);
589                 result = 0.0;
590         }
591         else {
592                 /* all fine, make sure the "invalid expression" flag is cleared */
593                 driver->flag &= ~DRIVER_FLAG_INVALID;
594                 Py_DECREF(retval);
595         }
596
597         if (use_gil)
598                 PyGILState_Release(gilstate);
599
600         if (isfinite(result)) {
601                 return (float)result;
602         }
603         else {
604                 fprintf(stderr, "\tBPY_driver_eval() - driver '%s' evaluates to '%f'\n", dvar->name, result);
605                 return 0.0f;
606         }
607 }