emacs-module.c 38.9 KB
Newer Older
1
/* emacs-module.c - Module loading and runtime implementation
2

Paul Eggert's avatar
Paul Eggert committed
3
Copyright (C) 2015-2019 Free Software Foundation, Inc.
4

5
This file is part of GNU Emacs.
6

7 8
GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
9 10
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
11

12 13 14 15
GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
16

17
You should have received a copy of the GNU General Public License
18
along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.  */
19

Paul Eggert's avatar
Paul Eggert committed
20 21 22 23
#include <config.h>

#include "emacs-module.h"

24
#include <stdarg.h>
25 26 27
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
28
#include <stdlib.h>
29 30 31 32

#include "lisp.h"
#include "dynlib.h"
#include "coding.h"
Philipp Stephani's avatar
Philipp Stephani committed
33
#include "keyboard.h"
34
#include "syssignal.h"
Philipp Stephani's avatar
Philipp Stephani committed
35
#include "thread.h"
36 37 38

#include <intprops.h>
#include <verify.h>
39

40 41 42 43 44
/* Work around GCC bug 83162.  */
#if GNUC_PREREQ (4, 3, 0)
# pragma GCC diagnostic ignored "-Wclobbered"
#endif

45 46 47 48 49
/* This module is lackadaisical about function casts.  */
#if GNUC_PREREQ (8, 0, 0)
# pragma GCC diagnostic ignored "-Wcast-function-type"
#endif

50 51 52 53 54 55 56 57 58 59 60
/* We use different strategies for allocating the user-visible objects
   (struct emacs_runtime, emacs_env, emacs_value), depending on
   whether the user supplied the -module-assertions flag.  If
   assertions are disabled, all objects are allocated from the stack.
   If assertions are enabled, all objects are allocated from the free
   store, and objects are never freed; this guarantees that they all
   have different addresses.  We use that for checking which objects
   are live.  Without unique addresses, we might consider some dead
   objects live because their addresses would have been reused in the
   meantime.  */

61

62
/* Feature tests.  */
63

64
#ifdef WINDOWSNT
65 66
#include <windows.h>
#include "w32term.h"
67 68
#endif

69 70 71
/* Function prototype for the module init function.  */
typedef int (*emacs_init_function) (struct emacs_runtime *);

72 73 74 75 76 77
/* Function prototype for module user-pointer finalizers.  These
   should not throw C++ exceptions, so emacs-module.h declares the
   corresponding interfaces with EMACS_NOEXCEPT.  There is only C code
   in this module, though, so this constraint is not enforced here.  */
typedef void (*emacs_finalizer_function) (void *);

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114

/* Memory management.  */

/* An `emacs_value' is just a pointer to a structure holding an
   internal Lisp object.  */
struct emacs_value_tag { Lisp_Object v; };

/* Local value objects use a simple fixed-sized block allocation
   scheme without explicit deallocation.  All local values are
   deallocated when the lifetime of their environment ends.  Keep
   track of a current frame from which new values are allocated,
   appending further dynamically-allocated frames if necessary.  */

enum { value_frame_size = 512 };

/* A block from which `emacs_value' object can be allocated.  */
struct emacs_value_frame
{
  /* Storage for values.  */
  struct emacs_value_tag objects[value_frame_size];

  /* Index of the next free value in `objects'.  */
  int offset;

  /* Pointer to next frame, if any.  */
  struct emacs_value_frame *next;
};

/* A structure that holds an initial frame (so that the first local
   values require no dynamic allocation) and keeps track of the
   current frame.  */
static struct emacs_value_storage
{
  struct emacs_value_frame initial;
  struct emacs_value_frame *current;
} global_storage;

115

116
/* Private runtime and environment members.  */
117 118 119

/* The private part of an environment stores the current non local exit state
   and holds the `emacs_value' objects allocated during the lifetime
120 121 122
   of the environment.  */
struct emacs_env_private
{
123 124
  enum emacs_funcall_exit pending_non_local_exit;

125 126 127
  /* Dedicated storage for non-local exit symbol and data so that
     storage is always available for them, even in an out-of-memory
     situation.  */
128
  struct emacs_value_tag non_local_exit_symbol, non_local_exit_data;
129

130
  struct emacs_value_storage storage;
131 132 133
};

/* The private parts of an `emacs_runtime' object contain the initial
134 135 136
   environment.  */
struct emacs_runtime_private
{
137
  emacs_env *env;
138 139 140
};


141
/* Forward declarations.  */
142

143
static Lisp_Object value_to_lisp (emacs_value);
144
static emacs_value allocate_emacs_value (emacs_env *, struct emacs_value_storage *, Lisp_Object);
145
static emacs_value lisp_to_value (emacs_env *, Lisp_Object);
146
static enum emacs_funcall_exit module_non_local_exit_check (emacs_env *);
147 148 149
static void module_assert_thread (void);
static void module_assert_runtime (struct emacs_runtime *);
static void module_assert_env (emacs_env *);
Paul Eggert's avatar
Paul Eggert committed
150 151 152 153
static _Noreturn void module_abort (const char *format, ...)
  ATTRIBUTE_FORMAT_PRINTF(1, 2);
static emacs_env *initialize_environment (emacs_env *,
					  struct emacs_env_private *);
154 155 156
static void finalize_environment (emacs_env *);
static void finalize_environment_unwind (void *);
static void finalize_runtime_unwind (void *);
Paul Eggert's avatar
Paul Eggert committed
157
static void module_handle_signal (emacs_env *, Lisp_Object);
158
static void module_handle_throw (emacs_env *, Lisp_Object);
Paul Eggert's avatar
Paul Eggert committed
159 160 161 162
static void module_non_local_exit_signal_1 (emacs_env *,
					    Lisp_Object, Lisp_Object);
static void module_non_local_exit_throw_1 (emacs_env *,
					   Lisp_Object, Lisp_Object);
163
static void module_out_of_memory (emacs_env *);
164
static void module_reset_handlerlist (struct handler **);
165

166
static bool module_assertions = false;
167 168 169

/* Convenience macros for non-local exit handling.  */

170
/* FIXME: The following implementation for non-local exit handling
171
   does not support recovery from stack overflow, see sysdep.c.  */
172

173 174 175 176 177 178 179 180 181
/* Emacs uses setjmp and longjmp for non-local exits, but
   module frames cannot be skipped because they are in general
   not prepared for long jumps (e.g., the behavior in C++ is undefined
   if objects with nontrivial destructors would be skipped).
   Therefore, catch all non-local exits.  There are two kinds of
   non-local exits: `signal' and `throw'.  The macros in this section
   can be used to catch both.  Use macros to avoid additional variants
   of `internal_condition_case' etc., and to avoid worrying about
   passing information to the handler functions.  */
182 183

/* Place this macro at the beginning of a function returning a number
184 185 186
   or a pointer to handle non-local exits.  The function must have an
   ENV parameter.  The function will return the specified value if a
   signal or throw is caught.  */
187 188
/* TODO: Have Fsignal check for CATCHER_ALL so we only have to install
   one handler.  */
189 190 191
#define MODULE_HANDLE_NONLOCAL_EXIT(retval)                     \
  MODULE_SETJMP (CONDITION_CASE, module_handle_signal, retval); \
  MODULE_SETJMP (CATCHER_ALL, module_handle_throw, retval)
192 193 194 195 196 197

#define MODULE_SETJMP(handlertype, handlerfunc, retval)			       \
  MODULE_SETJMP_1 (handlertype, handlerfunc, retval,			       \
		   internal_handler_##handlertype,			       \
		   internal_cleanup_##handlertype)

198 199 200 201
#if !__has_attribute (cleanup)
 #error "__attribute__ ((cleanup)) not supported by this compiler; try GCC"
#endif

202 203 204
/* It is very important that pushing the handler doesn't itself raise
   a signal.  Install the cleanup only after the handler has been
   pushed.  Use __attribute__ ((cleanup)) to avoid
205 206 207 208 209 210 211
   non-local-exit-prone manual cleanup.

   The do-while forces uses of the macro to be followed by a semicolon.
   This macro cannot enclose its entire body inside a do-while, as the
   code after the macro may longjmp back into the macro, which means
   its local variable C must stay live in later code.  */

212
/* TODO: Make backtraces work if this macros is used.  */
213

214
#define MODULE_SETJMP_1(handlertype, handlerfunc, retval, c0, c)	\
215 216
  if (module_non_local_exit_check (env) != emacs_funcall_exit_return)	\
    return retval;							\
217 218
  struct handler *c0 = push_handler_nosignal (Qt, handlertype);		\
  if (!c0)								\
219 220 221 222
    {									\
      module_out_of_memory (env);					\
      return retval;							\
    }									\
223 224
  struct handler *c __attribute__ ((cleanup (module_reset_handlerlist))) \
    = c0;								\
225 226 227 228 229 230
  if (sys_setjmp (c->jmp))						\
    {									\
      (handlerfunc) (env, c->val);					\
      return retval;							\
    }									\
  do { } while (false)
231 232


233 234 235 236 237 238
/* Implementation of runtime and environment functions.

   These should abide by the following rules:

   1. The first argument should always be a pointer to emacs_env.

Philipp Stephani's avatar
Philipp Stephani committed
239
   2. Each function should first call check_thread.  Note that
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
      this function is a no-op unless Emacs was built with
      --enable-checking.

   3. The very next thing each function should do is check that the
      emacs_env object does not have a non-local exit indication set,
      by calling module_non_local_exit_check.  If that returns
      anything but emacs_funcall_exit_return, the function should do
      nothing and return immediately with an error indication, without
      clobbering the existing error indication in emacs_env.  This is
      needed for correct reporting of Lisp errors to the Emacs Lisp
      interpreter.

   4. Any function that needs to call Emacs facilities, such as
      encoding or decoding functions, or 'intern', or 'make_string',
      should protect itself from signals and 'throw' in the called
255 256
      Emacs functions, by placing the macro
      MODULE_HANDLE_NONLOCAL_EXIT right after the above 2 tests.
257 258 259 260 261 262 263 264

   5. Do NOT use 'eassert' for checking validity of user code in the
      module.  Instead, make those checks part of the code, and if the
      check fails, call 'module_non_local_exit_signal_1' or
      'module_non_local_exit_throw_1' to report the error.  This is
      because using 'eassert' in these situations will abort Emacs
      instead of reporting the error back to Lisp, and also because
      'eassert' is compiled to nothing in the release version.  */
265

266 267 268 269 270 271 272
/* Use MODULE_FUNCTION_BEGIN_NO_CATCH to implement steps 2 and 3 for
   environment functions that are known to never exit non-locally.  On
   error it will return its argument, which can be a sentinel
   value.  */

#define MODULE_FUNCTION_BEGIN_NO_CATCH(error_retval)                    \
  do {                                                                  \
273 274
    module_assert_thread ();                                            \
    module_assert_env (env);                                            \
275 276 277 278
    if (module_non_local_exit_check (env) != emacs_funcall_exit_return) \
      return error_retval;                                              \
  } while (false)

279 280
/* Use MODULE_FUNCTION_BEGIN to implement steps 2 through 4 for most
   environment functions.  On error it will return its argument, which
Philipp Stephani's avatar
Philipp Stephani committed
281
   can be a sentinel value.  */
282

283 284
#define MODULE_FUNCTION_BEGIN(error_retval)      \
  MODULE_FUNCTION_BEGIN_NO_CATCH (error_retval); \
285 286
  MODULE_HANDLE_NONLOCAL_EXIT (error_retval)

287 288 289
static void
CHECK_USER_PTR (Lisp_Object obj)
{
290
  CHECK_TYPE (USER_PTRP (obj), Quser_ptrp, obj);
291 292
}

293 294 295
/* Catch signals and throws only if the code can actually signal or
   throw.  If checking is enabled, abort if the current thread is not
   the Emacs main thread.  */
296

297 298
static emacs_env *
module_get_environment (struct emacs_runtime *ert)
299
{
300 301 302
  module_assert_thread ();
  module_assert_runtime (ert);
  return ert->private_members->env;
303 304
}

305 306
/* To make global refs (GC-protected global values) keep a hash that
   maps global Lisp objects to reference counts.  */
307

308 309
static emacs_value
module_make_global_ref (emacs_env *env, emacs_value ref)
310
{
311
  MODULE_FUNCTION_BEGIN (NULL);
312 313 314 315 316 317 318 319
  struct Lisp_Hash_Table *h = XHASH_TABLE (Vmodule_refs_hash);
  Lisp_Object new_obj = value_to_lisp (ref);
  EMACS_UINT hashcode;
  ptrdiff_t i = hash_lookup (h, new_obj, &hashcode);

  if (i >= 0)
    {
      Lisp_Object value = HASH_VALUE (h, i);
Tom Tromey's avatar
Tom Tromey committed
320
      EMACS_INT refcount = XFIXNAT (value) + 1;
321
      if (MOST_POSITIVE_FIXNUM < refcount)
322
	overflow_error ();
323
      value = make_fixed_natnum (refcount);
324 325 326 327
      set_hash_value_slot (h, i, value);
    }
  else
    {
328
      hash_put (h, new_obj, make_fixed_natnum (1), hashcode);
329 330
    }

331
  return allocate_emacs_value (env, &global_storage, new_obj);
332 333
}

334 335
static void
module_free_global_ref (emacs_env *env, emacs_value ref)
336
{
337
  /* TODO: This probably never signals.  */
338 339
  /* FIXME: Wait a minute.  Shouldn't this function report an error if
     the hash lookup fails?  */
340
  MODULE_FUNCTION_BEGIN ();
341 342
  struct Lisp_Hash_Table *h = XHASH_TABLE (Vmodule_refs_hash);
  Lisp_Object obj = value_to_lisp (ref);
343
  ptrdiff_t i = hash_lookup (h, obj, NULL);
344 345 346

  if (i >= 0)
    {
Tom Tromey's avatar
Tom Tromey committed
347
      EMACS_INT refcount = XFIXNAT (HASH_VALUE (h, i)) - 1;
348
      if (refcount > 0)
349
        set_hash_value_slot (h, i, make_fixed_natnum (refcount));
350
      else
Philipp Stephani's avatar
Philipp Stephani committed
351 352 353 354
        {
          eassert (refcount == 0);
          hash_remove_from_table (h, obj);
        }
355
    }
356 357 358 359

  if (module_assertions)
    {
      ptrdiff_t count = 0;
360 361
      for (struct emacs_value_frame *frame = &global_storage.initial;
           frame != NULL; frame = frame->next)
362
        {
363
          for (int i = 0; i < frame->offset; ++i)
364
            {
365 366 367
              if (&frame->objects[i] == ref)
                return;
              ++count;
368 369
            }
        }
Paul Eggert's avatar
Paul Eggert committed
370
      module_abort ("Global value was not found in list of %"pD"d globals",
371 372
                    count);
    }
373 374
}

375 376
static enum emacs_funcall_exit
module_non_local_exit_check (emacs_env *env)
377
{
378 379
  module_assert_thread ();
  module_assert_env (env);
380 381 382
  return env->private_members->pending_non_local_exit;
}

383 384
static void
module_non_local_exit_clear (emacs_env *env)
385
{
386 387
  module_assert_thread ();
  module_assert_env (env);
388 389 390
  env->private_members->pending_non_local_exit = emacs_funcall_exit_return;
}

391 392
static enum emacs_funcall_exit
module_non_local_exit_get (emacs_env *env, emacs_value *sym, emacs_value *data)
393
{
394 395
  module_assert_thread ();
  module_assert_env (env);
Paul Eggert's avatar
Paul Eggert committed
396
  struct emacs_env_private *p = env->private_members;
397 398
  if (p->pending_non_local_exit != emacs_funcall_exit_return)
    {
399 400
      *sym = &p->non_local_exit_symbol;
      *data = &p->non_local_exit_data;
401 402 403 404
    }
  return p->pending_non_local_exit;
}

405 406 407
/* Like for `signal', DATA must be a list.  */
static void
module_non_local_exit_signal (emacs_env *env, emacs_value sym, emacs_value data)
408
{
409 410
  module_assert_thread ();
  module_assert_env (env);
411 412 413
  if (module_non_local_exit_check (env) == emacs_funcall_exit_return)
    module_non_local_exit_signal_1 (env, value_to_lisp (sym),
				    value_to_lisp (data));
414 415
}

416 417
static void
module_non_local_exit_throw (emacs_env *env, emacs_value tag, emacs_value value)
418
{
419 420
  module_assert_thread ();
  module_assert_env (env);
421 422 423
  if (module_non_local_exit_check (env) == emacs_funcall_exit_return)
    module_non_local_exit_throw_1 (env, value_to_lisp (tag),
				   value_to_lisp (value));
424 425
}

426 427 428 429
static struct Lisp_Module_Function *
allocate_module_function (void)
{
  return ALLOCATE_PSEUDOVECTOR (struct Lisp_Module_Function,
430
                                documentation, PVEC_MODULE_FUNCTION);
431 432 433 434 435
}

#define XSET_MODULE_FUNCTION(var, ptr) \
  XSETPSEUDOVECTOR (var, ptr, PVEC_MODULE_FUNCTION)

Philipp Stephani's avatar
Philipp Stephani committed
436
/* A module function is a pseudovector of subtype
437
   PVEC_MODULE_FUNCTION; see lisp.h for the definition.  */
438 439

static emacs_value
440
module_make_function (emacs_env *env, ptrdiff_t min_arity, ptrdiff_t max_arity,
Paul Eggert's avatar
Paul Eggert committed
441
		      emacs_subr subr, const char *documentation,
442
		      void *data)
443
{
444
  MODULE_FUNCTION_BEGIN (NULL);
445

446 447
  if (! (0 <= min_arity
	 && (max_arity < 0
448 449 450
	     ? (min_arity <= MOST_POSITIVE_FIXNUM
		&& max_arity == emacs_variadic_function)
	     : min_arity <= max_arity && max_arity <= MOST_POSITIVE_FIXNUM)))
451
    xsignal2 (Qinvalid_arity, make_fixnum (min_arity), make_fixnum (max_arity));
452

453 454 455 456 457
  struct Lisp_Module_Function *function = allocate_module_function ();
  function->min_arity = min_arity;
  function->max_arity = max_arity;
  function->subr = subr;
  function->data = data;
458

Paul Eggert's avatar
Paul Eggert committed
459 460 461
  if (documentation)
    {
      AUTO_STRING (unibyte_doc, documentation);
462
      function->documentation =
463
        code_convert_string_norecord (unibyte_doc, Qutf_8, false);
Paul Eggert's avatar
Paul Eggert committed
464 465
    }

466 467 468
  Lisp_Object result;
  XSET_MODULE_FUNCTION (result, function);
  eassert (MODULE_FUNCTIONP (result));
469

470
  return lisp_to_value (env, result);
471 472
}

473
static emacs_value
474 475
module_funcall (emacs_env *env, emacs_value fun, ptrdiff_t nargs,
		emacs_value args[])
476
{
477
  MODULE_FUNCTION_BEGIN (NULL);
478

479 480
  /* Make a new Lisp_Object array starting with the function as the
     first arg, because that's what Ffuncall takes.  */
481 482
  Lisp_Object *newargs;
  USE_SAFE_ALLOCA;
483 484
  ptrdiff_t nargs1;
  if (INT_ADD_WRAPV (nargs, 1, &nargs1))
485
    overflow_error ();
486
  SAFE_ALLOCA_LISP (newargs, nargs1);
487
  newargs[0] = value_to_lisp (fun);
488
  for (ptrdiff_t i = 0; i < nargs; i++)
489
    newargs[1 + i] = value_to_lisp (args[i]);
490
  emacs_value result = lisp_to_value (env, Ffuncall (nargs1, newargs));
491 492
  SAFE_FREE ();
  return result;
493 494
}

495 496
static emacs_value
module_intern (emacs_env *env, const char *name)
497
{
498
  MODULE_FUNCTION_BEGIN (NULL);
499
  return lisp_to_value (env, intern (name));
500 501
}

502 503
static emacs_value
module_type_of (emacs_env *env, emacs_value value)
504
{
505
  MODULE_FUNCTION_BEGIN (NULL);
506
  return lisp_to_value (env, Ftype_of (value_to_lisp (value)));
507 508
}

509 510
static bool
module_is_not_nil (emacs_env *env, emacs_value value)
511
{
512
  MODULE_FUNCTION_BEGIN_NO_CATCH (false);
513 514 515
  return ! NILP (value_to_lisp (value));
}

516 517
static bool
module_eq (emacs_env *env, emacs_value a, emacs_value b)
518
{
519
  MODULE_FUNCTION_BEGIN_NO_CATCH (false);
520 521 522
  return EQ (value_to_lisp (a), value_to_lisp (b));
}

523
static intmax_t
524
module_extract_integer (emacs_env *env, emacs_value n)
525
{
526
  MODULE_FUNCTION_BEGIN (0);
Paul Eggert's avatar
Paul Eggert committed
527
  Lisp_Object l = value_to_lisp (n);
528
  CHECK_INTEGER (l);
529 530 531 532
  intmax_t i;
  if (! integer_to_intmax (l, &i))
    xsignal1 (Qoverflow_error, l);
  return i;
533 534
}

535
static emacs_value
536
module_make_integer (emacs_env *env, intmax_t n)
537
{
538
  MODULE_FUNCTION_BEGIN (NULL);
Paul Eggert's avatar
Paul Eggert committed
539
  return lisp_to_value (env, make_int (n));
540 541
}

542 543
static double
module_extract_float (emacs_env *env, emacs_value f)
544
{
545
  MODULE_FUNCTION_BEGIN (0);
Paul Eggert's avatar
Paul Eggert committed
546
  Lisp_Object lisp = value_to_lisp (f);
547
  CHECK_TYPE (FLOATP (lisp), Qfloatp, lisp);
548 549 550
  return XFLOAT_DATA (lisp);
}

551 552
static emacs_value
module_make_float (emacs_env *env, double d)
553
{
554
  MODULE_FUNCTION_BEGIN (NULL);
555
  return lisp_to_value (env, make_float (d));
556 557
}

558 559
static bool
module_copy_string_contents (emacs_env *env, emacs_value value, char *buffer,
560
			     ptrdiff_t *length)
561
{
562
  MODULE_FUNCTION_BEGIN (false);
563
  Lisp_Object lisp_str = value_to_lisp (value);
564
  CHECK_STRING (lisp_str);
565

566 567
  Lisp_Object lisp_str_utf8 = ENCODE_UTF_8 (lisp_str);
  ptrdiff_t raw_size = SBYTES (lisp_str_utf8);
568
  ptrdiff_t required_buf_size = raw_size + 1;
569 570 571 572 573 574 575 576

  if (buffer == NULL)
    {
      *length = required_buf_size;
      return true;
    }

  if (*length < required_buf_size)
577
    {
578
      *length = required_buf_size;
579
      xsignal0 (Qargs_out_of_range);
580 581
    }

582
  *length = required_buf_size;
583
  memcpy (buffer, SDATA (lisp_str_utf8), raw_size + 1);
584 585 586 587

  return true;
}

588
static emacs_value
589
module_make_string (emacs_env *env, const char *str, ptrdiff_t length)
590
{
591
  MODULE_FUNCTION_BEGIN (NULL);
592
  if (! (0 <= length && length <= STRING_BYTES_BOUND))
593
    overflow_error ();
594
  /* FIXME: AUTO_STRING_WITH_LEN requires STR to be NUL-terminated,
595
     but we shouldn't require that.  */
Paul Eggert's avatar
Paul Eggert committed
596
  AUTO_STRING_WITH_LEN (lstr, str, length);
597 598
  return lisp_to_value (env,
                        code_convert_string_norecord (lstr, Qutf_8, false));
599 600
}

601 602
static emacs_value
module_make_user_ptr (emacs_env *env, emacs_finalizer_function fin, void *ptr)
603
{
604
  MODULE_FUNCTION_BEGIN (NULL);
605
  return lisp_to_value (env, make_user_ptr (fin, ptr));
606 607
}

608 609
static void *
module_get_user_ptr (emacs_env *env, emacs_value uptr)
610
{
611
  MODULE_FUNCTION_BEGIN (NULL);
Paul Eggert's avatar
Paul Eggert committed
612
  Lisp_Object lisp = value_to_lisp (uptr);
613
  CHECK_USER_PTR (lisp);
614 615 616
  return XUSER_PTR (lisp)->p;
}

617 618
static void
module_set_user_ptr (emacs_env *env, emacs_value uptr, void *ptr)
619
{
620
  MODULE_FUNCTION_BEGIN ();
Paul Eggert's avatar
Paul Eggert committed
621
  Lisp_Object lisp = value_to_lisp (uptr);
622
  CHECK_USER_PTR (lisp);
623 624 625
  XUSER_PTR (lisp)->p = ptr;
}

626 627
static emacs_finalizer_function
module_get_user_finalizer (emacs_env *env, emacs_value uptr)
628
{
629
  MODULE_FUNCTION_BEGIN (NULL);
Paul Eggert's avatar
Paul Eggert committed
630
  Lisp_Object lisp = value_to_lisp (uptr);
631
  CHECK_USER_PTR (lisp);
632 633 634
  return XUSER_PTR (lisp)->finalizer;
}

635 636 637
static void
module_set_user_finalizer (emacs_env *env, emacs_value uptr,
			   emacs_finalizer_function fin)
638
{
639
  MODULE_FUNCTION_BEGIN ();
Paul Eggert's avatar
Paul Eggert committed
640
  Lisp_Object lisp = value_to_lisp (uptr);
641
  CHECK_USER_PTR (lisp);
642 643 644
  XUSER_PTR (lisp)->finalizer = fin;
}

645 646 647 648 649
static void
check_vec_index (Lisp_Object lvec, ptrdiff_t i)
{
  CHECK_VECTOR (lvec);
  if (! (0 <= i && i < ASIZE (lvec)))
650
    args_out_of_range_3 (INT_TO_INTEGER (i),
651
			 make_fixnum (0), make_fixnum (ASIZE (lvec) - 1));
652 653
}

654
static void
655
module_vec_set (emacs_env *env, emacs_value vec, ptrdiff_t i, emacs_value val)
656
{
657
  MODULE_FUNCTION_BEGIN ();
658
  Lisp_Object lvec = value_to_lisp (vec);
659
  check_vec_index (lvec, i);
660 661 662
  ASET (lvec, i, value_to_lisp (val));
}

663
static emacs_value
664
module_vec_get (emacs_env *env, emacs_value vec, ptrdiff_t i)
665
{
666
  MODULE_FUNCTION_BEGIN (NULL);
667
  Lisp_Object lvec = value_to_lisp (vec);
668
  check_vec_index (lvec, i);
669
  return lisp_to_value (env, AREF (lvec, i));
670 671
}

672
static ptrdiff_t
673
module_vec_size (emacs_env *env, emacs_value vec)
674
{
675
  MODULE_FUNCTION_BEGIN (0);
676
  Lisp_Object lvec = value_to_lisp (vec);
677
  CHECK_VECTOR (lvec);
678 679 680
  return ASIZE (lvec);
}

681 682
/* This function should return true if and only if maybe_quit would
   quit.  */
Philipp Stephani's avatar
Philipp Stephani committed
683 684 685 686
static bool
module_should_quit (emacs_env *env)
{
  MODULE_FUNCTION_BEGIN_NO_CATCH (false);
687 688 689 690 691 692 693 694 695
  return QUITP;
}

static enum emacs_process_input_result
module_process_input (emacs_env *env)
{
  MODULE_FUNCTION_BEGIN (emacs_process_input_quit);
  maybe_quit ();
  return emacs_process_input_continue;
Philipp Stephani's avatar
Philipp Stephani committed
696 697
}

698

699
/* Subroutines.  */
700

701 702 703 704 705 706 707 708
static void
module_signal_or_throw (struct emacs_env_private *env)
{
  switch (env->pending_non_local_exit)
    {
    case emacs_funcall_exit_return:
      return;
    case emacs_funcall_exit_signal:
709 710
      xsignal (value_to_lisp (&env->non_local_exit_symbol),
               value_to_lisp (&env->non_local_exit_data));
711
    case emacs_funcall_exit_throw:
712 713
      Fthrow (value_to_lisp (&env->non_local_exit_symbol),
              value_to_lisp (&env->non_local_exit_data));
714 715 716 717 718
    default:
      eassume (false);
    }
}

719 720 721 722 723 724 725 726 727
DEFUN ("module-load", Fmodule_load, Smodule_load, 1, 1, 0,
       doc: /* Load module FILE.  */)
  (Lisp_Object file)
{
  dynlib_handle_ptr handle;
  emacs_init_function module_init;
  void *gpl_sym;

  CHECK_STRING (file);
728
  handle = dynlib_open (SSDATA (file));
729
  if (!handle)
730
    xsignal2 (Qmodule_open_failed, file, build_string (dynlib_error ()));
731 732 733

  gpl_sym = dynlib_sym (handle, "plugin_is_GPL_compatible");
  if (!gpl_sym)
734
    xsignal1 (Qmodule_not_gpl_compatible, file);
735

736
  module_init = (emacs_init_function) dynlib_func (handle, "emacs_module_init");
737
  if (!module_init)
738
    xsignal1 (Qmissing_module_init_function, file);
739

740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
  struct emacs_runtime rt_pub;
  struct emacs_runtime_private rt_priv;
  emacs_env env_pub;
  struct emacs_env_private env_priv;
  rt_priv.env = initialize_environment (&env_pub, &env_priv);

  /* If we should use module assertions, reallocate the runtime object
     from the free store, but never free it.  That way the addresses
     for two different runtime objects are guaranteed to be distinct,
     which we can use for checking the liveness of runtime
     pointers.  */
  struct emacs_runtime *rt = module_assertions ? xmalloc (sizeof *rt) : &rt_pub;
  rt->size = sizeof *rt;
  rt->private_members = &rt_priv;
  rt->get_environment = module_get_environment;

756
  Vmodule_runtimes = Fcons (make_mint_ptr (rt), Vmodule_runtimes);
757
  ptrdiff_t count = SPECPDL_INDEX ();
758
  record_unwind_protect_ptr (finalize_runtime_unwind, rt);
759

760
  int r = module_init (rt);
761

762 763 764 765
  /* Process the quit flag first, so that quitting doesn't get
     overridden by other non-local exits.  */
  maybe_quit ();

766
  if (r != 0)
767
    xsignal2 (Qmodule_init_failed, file, INT_TO_INTEGER (r));
768

769
  module_signal_or_throw (&env_priv);
770
  return unbind_to (count, Qt);
771 772
}

773
Lisp_Object
774
funcall_module (Lisp_Object function, ptrdiff_t nargs, Lisp_Object *arglist)
775
{
776 777 778 779
  const struct Lisp_Module_Function *func = XMODULE_FUNCTION (function);
  eassume (0 <= func->min_arity);
  if (! (func->min_arity <= nargs
	 && (func->max_arity < 0 || nargs <= func->max_arity)))
780
    xsignal2 (Qwrong_number_of_arguments, function, make_fixnum (nargs));
781

782 783
  emacs_env pub;
  struct emacs_env_private priv;
784
  emacs_env *env = initialize_environment (&pub, &priv);
785
  ptrdiff_t count = SPECPDL_INDEX ();
786
  record_unwind_protect_ptr (finalize_environment_unwind, env);
787

788
  USE_SAFE_ALLOCA;
789 790
  emacs_value *args = nargs > 0 ? SAFE_ALLOCA (nargs * sizeof *args) : NULL;
  for (ptrdiff_t i = 0; i < nargs; ++i)
791
    {
792 793 794
      args[i] = lisp_to_value (env, arglist[i]);
      if (! args[i])
	memory_full (sizeof *args[i]);
795
    }
796

797
  emacs_value ret = func->subr (env, nargs, args, func->data);
798

799
  eassert (&priv == env->private_members);
800

Philipp Stephani's avatar
Philipp Stephani committed
801 802 803 804
  /* Process the quit flag first, so that quitting doesn't get
     overridden by other non-local exits.  */
  maybe_quit ();

805
  module_signal_or_throw (&priv);
Paul Eggert's avatar
Paul Eggert committed
806
  return SAFE_FREE_UNBIND_TO (count, value_to_lisp (ret));
807 808
}

809 810 811
Lisp_Object
module_function_arity (const struct Lisp_Module_Function *const function)
{
812 813
  ptrdiff_t minargs = function->min_arity;
  ptrdiff_t maxargs = function->max_arity;
814 815
  return Fcons (make_fixnum (minargs),
		maxargs == MANY ? Qmany : make_fixnum (maxargs));
816 817
}

818

819
/* Helper functions.  */
820

821 822 823
static void
module_assert_thread (void)
{
824
  if (!module_assertions)
825
    return;
826 827 828 829 830
  if (!in_current_thread ())
    module_abort ("Module function called from outside "
                  "the current Lisp thread");
  if (gc_in_progress)
    module_abort ("Module function called during garbage collection");
831 832 833 834 835 836 837 838 839 840
}

static void
module_assert_runtime (struct emacs_runtime *ert)
{
  if (! module_assertions)
    return;
  ptrdiff_t count = 0;
  for (Lisp_Object tail = Vmodule_runtimes; CONSP (tail); tail = XCDR (tail))
    {
841
      if (xmint_pointer (XCAR (tail)) == ert)
842 843 844
        return;
      ++count;
    }
Paul Eggert's avatar
Paul Eggert committed
845
  module_abort ("Runtime pointer not found in list of %"pD"d runtimes",
846
		count);
847 848 849 850 851 852 853 854 855 856 857
}

static void
module_assert_env (emacs_env *env)
{
  if (! module_assertions)
    return;
  ptrdiff_t count = 0;
  for (Lisp_Object tail = Vmodule_environments; CONSP (tail);
       tail = XCDR (tail))
    {
858
      if (xmint_pointer (XCAR (tail)) == env)
859 860 861
        return;
      ++count;
    }
Paul Eggert's avatar
Paul Eggert committed
862
  module_abort ("Environment pointer not found in list of %"pD"d environments",
863 864 865
                count);
}

866 867 868
static void
module_non_local_exit_signal_1 (emacs_env *env, Lisp_Object sym,
				Lisp_Object data)
869
{
Paul Eggert's avatar
Paul Eggert committed
870
  struct emacs_env_private *p = env->private_members;
871 872 873
  if (p->pending_non_local_exit == emacs_funcall_exit_return)
    {
      p->pending_non_local_exit = emacs_funcall_exit_signal;
874 875
      p->non_local_exit_symbol.v = sym;
      p->non_local_exit_data.v = data;
876
    }
877 878
}

879 880 881
static void
module_non_local_exit_throw_1 (emacs_env *env, Lisp_Object tag,
			       Lisp_Object value)
882
{
Paul Eggert's avatar
Paul Eggert committed
883
  struct emacs_env_private *p = env->private_members;
884 885 886
  if (p->pending_non_local_exit == emacs_funcall_exit_return)
    {
      p->pending_non_local_exit = emacs_funcall_exit_throw;
887 888
      p->non_local_exit_symbol.v = tag;
      p->non_local_exit_data.v = value;
889
    }
890 891
}

892 893 894
/* Signal an out-of-memory condition to the caller.  */
static void
module_out_of_memory (emacs_env *env)
895
{
896 897 898 899
  /* TODO: Reimplement this so it works even if memory-signal-data has
     been modified.  */
  module_non_local_exit_signal_1 (env, XCAR (Vmemory_signal_data),
				  XCDR (Vmemory_signal_data));
900 901 902
}


903
/* Value conversion.  */
904

905 906
/* Convert an `emacs_value' to the corresponding internal object.
   Never fails.  */
907 908

/* If V was computed from lisp_to_value (O), then return O.
909
   Exits non-locally only if the stack overflows.  */
910 911 912
static Lisp_Object
value_to_lisp (emacs_value v)
{
913 914 915 916 917 918 919 920 921
  if (module_assertions)
    {
      /* Check the liveness of the value by iterating over all live
         environments.  */
      ptrdiff_t num_environments = 0;
      ptrdiff_t num_values = 0;
      for (Lisp_Object environments = Vmodule_environments;
           CONSP (environments); environments = XCDR (environments))
        {
922
          emacs_env *env = xmint_pointer (XCAR (environments));
923 924 925 926 927 928 929 930 931 932
          struct emacs_env_private *priv = env->private_members;
          /* The value might be one of the nonlocal exit values.  Note
             that we don't check whether a nonlocal exit is currently
             pending, because the module might have cleared the flag
             in the meantime.  */
          if (&priv->non_local_exit_symbol == v
              || &priv->non_local_exit_data == v)
            goto ok;
          for (struct emacs_value_frame *frame = &priv->storage.initial;
               frame != NULL; frame = frame->next)
933
            {
934 935 936 937 938 939
              for (int i = 0; i < frame->offset; ++i)
                {
                  if (&frame->objects[i] == v)
                    goto ok;
                  ++num_values;
                }
940
            }
941
          ++num_environments;
942
        }
943 944 945 946 947 948 949 950 951 952 953
      /* Also check global values.  */
      for (struct emacs_value_frame *frame = &global_storage.initial;
           frame != NULL; frame = frame->next)
        {
          for (int i = 0; i < frame->offset; ++i)
            {
              if (&frame->objects[i] == v)
                goto ok;
              ++num_values;
            }
        }
Paul Eggert's avatar
Paul Eggert committed
954 955
      module_abort (("Emacs value not found in %"pD"d values "
		     "of %"pD"d environments"),
956 957 958
                    num_values, num_environments);
    }

959
 ok: return v->v;
960 961
}

962 963
/* Convert an internal object to an `emacs_value'.  Allocate storage
   from the environment; return NULL if allocation fails.  */
964
static emacs_value
965
lisp_to_value (emacs_env *env, Lisp_Object o)
966
{
967 968 969 970 971
  struct emacs_env_private *p = env->private_members;
  if (p->pending_non_local_exit != emacs_funcall_exit_return)
    return NULL;
  return allocate_emacs_value (env, &p->storage, o);
}
972

973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
/* Must be called for each frame before it can be used for allocation.  */
static void
initialize_frame (struct emacs_value_frame *frame)
{
  frame->offset = 0;
  frame->next = NULL;
}

/* Must be called for any storage object before it can be used for
   allocation.  */
static void
initialize_storage (struct emacs_value_storage *storage)
{
  initialize_frame (&storage->initial);
  storage->current = &storage->initial;
}

/* Must be called for any initialized storage object before its
   lifetime ends.  Free all dynamically-allocated frames.  */
static void
finalize_storage (struct emacs_value_storage *storage)
{
  struct emacs_value_frame *next = storage->initial.next;
  while (next != NULL)
997
    {
998 999 1000
      struct emacs_value_frame *current = next;
      next = current->next;
      free (current);
1001
    }
1002 1003
}

1004 1005
/* Allocate a new value from STORAGE and stores OBJ in it.  Return
   NULL if allocation fails and use ENV for non local exit reporting.  */
1006
static emacs_value
1007 1008
allocate_emacs_value (emacs_env *env, struct emacs_value_storage *storage,
		      Lisp_Object obj)
1009
{
1010 1011 1012 1013
  eassert (storage->current);
  eassert (storage->current->offset < value_frame_size);
  eassert (! storage->current->next);
  if (storage->current->offset == value_frame_size - 1)
1014
    {
1015 1016 1017 1018 1019 1020 1021 1022
      storage->current->next = malloc (sizeof *storage->current->next);
      if (! storage->current->next)
        {
          module_out_of_memory (env);
          return NULL;
        }
      initialize_frame (storage->current->next);
      storage->current = storage->current->next;
1023
    }
1024 1025 1026 1027 1028
  emacs_value value = storage->current->objects + storage->current->offset;
  value->v = obj;
  ++storage->current->offset;
  return value;
}
1029

1030 1031 1032 1033 1034 1035
/* Mark all objects allocated from local environments so that they
   don't get garbage-collected.  */
void
mark_modules (void)
{
  for (Lisp_Object tem = Vmodule_environments; CONSP (tem); tem = XCDR (tem))