emacs/test/src
Philipp Stephani 23974cfa48 Fix incorrect handling of module runtime and environment pointers.
We used to store module runtime and environment pointers in the static
lists Vmodule_runtimes and Vmodule_environments.  However, this is
incorrect because these objects have to be kept per-thread.  With this
naive approach, interleaving module function calls in separate threads
leads to environments being removed in the wrong order, which in turn
can cause local module values to be incorrectly garbage-collected.
The fix isn't completely trivial: specbinding the lists wouldn't work
either, because then the garbage collector wouldn't find the
environments in other threads than the current ones, again leading to
objects being garbage-collected incorrectly.  While introducing custom
pseudovector types would fix this, it's simpler to put the runtime and
environment pointers into the specbinding list as new specbinding
kinds.  This works since we need to unwind them anyway, and we only
ever treat the lists as a stack.  The thread switching machinery
ensures that the specbinding lists are thread-local, and that all
elements of the specbinding lists in all threads are marked during
garbage collection.

Module assertions now have to walk the specbinding list for the
current thread, which is more correct since they now only find
environments for the current thread.  As a result, we can now remove
the faulty Vmodule_runtimes and Vmodule_environments variables
entirely.

Also add a unit test that exemplifies the problem.  It interleaves two
module calls in two threads so that the first call ends while the
second one is still active.  Without this change, this test triggers
an assertion failure.

* src/lisp.h (enum specbind_tag): Add new tags for module runtimes and
environments.

* src/eval.c (record_unwind_protect_module): New function to record a
module object in the specpdl list.
(do_one_unbind): Unwind module objects.
(backtrace_eval_unrewind, default_toplevel_binding, lexbound_p)
(Fbacktrace__locals): Deal with new specbinding types.
(mark_specpdl): Mark module environments as needed.

* src/alloc.c (garbage_collect): Remove call to 'mark-modules'.
Garbage collection of module values is now handled as part of marking
the specpdl of each thread.

* src/emacs-module.c (Fmodule_load, funcall_module): Use specpdl to
record module runtimes and environments.
(module_assert_runtime, module_assert_env, value_to_lisp): Walk
through specpdl list instead of list variables.
(mark_module_environment): Rename from 'mark_modules'.  Don't attempt
to walk though current thread's environments only, since that would
miss other threads.
(initialize_environment, finalize_environment): Don't change
Vmodule_environments variable; environments are now in the specpdl
list.
(finalize_environment_unwind, finalize_runtime_unwind): Make 'extern'
since do_one_unbind now calls them.
(finalize_runtime_unwind): Don't change Vmodule_runtimes variable;
runtimes are now in the specpdl list.
(syms_of_module): Remove Vmodule_runtimes and Vmodule_environments.

* test/data/emacs-module/mod-test.c (Fmod_test_funcall): New test
function.
(emacs_module_init): Bind it.

* test/src/emacs-module-tests.el (emacs-module-tests--variable): New
helper type to guard access to state in a thread-safe way.
(emacs-module-tests--wait-for-variable)
(emacs-module-tests--change-variable): New helper functions.
(emacs-module-tests/interleaved-threads): New unit test.
2020-11-27 20:15:33 +01:00
..
emacs-module-resources Fix incorrect handling of module runtime and environment pointers. 2020-11-27 20:15:33 +01:00
lread-resources Move some test data to follow our conventions 2020-10-23 16:29:46 +02:00
regex-resources ; Fix more typos 2020-10-03 12:53:51 +02:00
syntax-resources Enhance syntax-tests.el to test nestable comments (Lisp style) 2020-11-17 16:51:49 +00:00
alloc-tests.el Fix crash when sending Gnus message (Bug#39207) 2020-01-22 23:44:34 -08:00
buffer-tests.el Merge from origin/emacs-27 2020-06-07 09:03:59 -07:00
callint-tests.el Adapt some tests for Emacs's excitingly variable quoting format 2020-09-12 11:57:42 -07:00
callproc-tests.el Fix error in 'call-process-region' when START is nil (Bug#40576) 2020-04-12 19:04:11 +02:00
casefiddle-tests.el Update copyright year to 2020 2020-01-01 00:59:52 +00:00
charset-tests.el ; Fix license statements. 2020-08-27 02:53:25 +02:00
chartab-tests.el ; Fix license statements. 2020-08-27 02:53:25 +02:00
cmds-tests.el ; Fix license statements. 2020-08-27 02:53:25 +02:00
coding-tests.el Merge from origin/emacs-27 2020-10-02 09:38:24 -07:00
data-tests.el * src/data.c (set_internal): Fix bug#44733 2020-11-19 17:13:04 -05:00
decompress-tests.el Use lexical-binding in most src tests 2020-04-24 18:25:13 +02:00
editfns-tests.el Use lexical-binding in more tests 2020-08-29 22:41:51 +02:00
emacs-module-tests.el Fix incorrect handling of module runtime and environment pointers. 2020-11-27 20:15:33 +01:00
eval-tests.el Update copyright year to 2020 2020-01-01 00:59:52 +00:00
fileio-tests.el Fix 'expand-file-name' for remote files 2020-09-03 20:16:33 +03:00
floatfns-tests.el Use lexical-binding in most src tests 2020-04-24 18:25:13 +02:00
fns-tests.el More string-search optimisations 2020-11-19 14:58:48 +01:00
font-tests.el Use lexical-binding in most src tests 2020-04-24 18:25:13 +02:00
indent-tests.el Fix 'move-to-column' when invisible text follows a TAB 2020-09-30 17:33:58 +03:00
inotify-tests.el Update copyright year to 2020 2020-01-01 00:59:52 +00:00
json-tests.el Fix undefined behavior in json.c (Bug#42113) 2020-06-29 12:32:56 +02:00
keyboard-tests.el Give Lisp control on the lossage size 2020-09-17 16:32:18 +02:00
keymap-tests.el Say which command shadows a key binding 2020-11-22 02:48:53 +01:00
lcms-tests.el Update copyright year to 2020 2020-01-01 00:59:52 +00:00
lread-tests.el Move some test data to follow our conventions 2020-10-23 16:29:46 +02:00
marker-tests.el Update copyright year to 2020 2020-01-01 00:59:52 +00:00
minibuf-tests.el Update copyright year to 2020 2020-01-01 00:59:52 +00:00
print-tests.el Reduce integer-output-format to print-integers-as-characters 2020-11-06 13:55:32 +01:00
process-tests.el ; Fix license statements. 2020-08-27 02:53:25 +02:00
regex-emacs-tests.el Revert "Signal an error for the regexp "[:alnum:]"" 2020-02-27 18:58:05 +01:00
syntax-tests.el Enhance syntax-tests.el to test nestable comments (Lisp style) 2020-11-17 16:51:49 +00:00
textprop-tests.el Use lexical-binding in most src tests 2020-04-24 18:25:13 +02:00
thread-tests.el Use lexical-binding in most src tests 2020-04-24 18:25:13 +02:00
timefns-tests.el ; Fix license statements. 2020-08-27 02:53:25 +02:00
undo-tests.el ; Fix license statement in several files 2020-10-28 03:41:55 +01:00
xdisp-tests.el ; Silence byte-compiler warning in xdisp-tests.el 2020-11-05 15:44:17 +01:00
xfaces-tests.el Minor improvements as followup to recent RGB string-parsing change 2020-06-22 18:32:01 +03:00
xml-tests.el Repurpose libxml test for obsolete argument 2020-09-26 11:48:09 +02:00