Commit graph

6543 commits

Author SHA1 Message Date
Marius Gerbershagen
eca39581a5 fix build failures with --disable-threads 2018-03-02 18:56:23 +01:00
Marius Gerbershagen
d87c5b2c5a threading: fix resource leak on windows
Thread handles were never closed. Also fixed the ugly hack in
    process->thread, where a pthread_t object was used on windows instead
    of the correct HANDLE object.
2018-02-28 21:20:06 +01:00
Marius Gerbershagen
fd900d9c64 threading: fix race conditions in ecl_unlist_process/thread_cleanup
ecl_unlist_process is called in thread_cleanup after interrupts have
    been disabled, however it uses unwind-protect, which will disenable and
    then reenable interrupts. Since on windows, we don't have an equivalent
    of sigmask, we can't use unwind-protect and instead rely on disabled
    interrupts to make sure, that the spinlock is always released.
2018-02-28 20:55:49 +01:00
Marius Gerbershagen
47029db7b2 threading: fix race conditions on windows
ecl_check_pending_interrupts is used in ecl_enable_interrupts_env,
    which may not always be in a place where we can handle signals without
    safety measures. _ecl_w32_exception_filter needs to be protected too. Here,
    the switch statement could also fall through to EXCEPTION_INT_DIVIDE_BY_ZERO,
    leading to wrong errors being displayed.
2018-02-24 14:31:44 +01:00
Marius Gerbershagen
2ccc5de365 rename function arguments to avoid collision with identically named define
Fixes mingw build error.
2018-02-23 20:33:51 +01:00
Marius Gerbershagen
a8d7305fb6 threading: fix race condition in stacks_scanner
The garbage collector can call stacks_scanner in a thread before
    pthread_setspecific, leading to a wrong error message. The
    solution is simply not to mark the environment, if
    pthread_setspecific has not yet been called.
2018-02-20 21:40:04 +01:00
Marius Gerbershagen
f9630fa8b3 threading: fix race conditions in CLOS cache
If a thread is interrupted after a call to fill_spec_vector, but
    before it can call ecl_search_cache, the cache may change during
    the interrupt, leading to crashes. We can't use
    env->disable_interrupts since fill_spec_vector calls methods which
    write in the thread-local environment. Disabling interrupts in
    ecl_search_cache and clear_list_from_cache is now redundant and
    has been removed.
2018-02-20 20:24:08 +01:00
Marius Gerbershagen
25ec43b498 fix typo in stacks.h 2018-02-20 20:15:31 +01:00
Marius Gerbershagen
7d6112d0e8 threading: fix race condition in pop_signal
The pending interrupts list may be modified after we have checked
    whether it is nil, but before we aquire the spinlock, leading to
    segmentation faults.
2018-02-18 21:03:07 +01:00
Marius Gerbershagen
24e4c13d58 threading: block interrupts during execution of cleanup forms in unwind-protect
If we don't do this, execution of the cleanup forms may be
    interrupted or they may not be executed at all. This behaviour
    would probably be acceptable for external code, however the
    unwind-protect mechanism is also used internally to protect
    against deadlocks (e.g. in ECL_WITH_(SPIN)LOCK).
2018-02-18 21:02:26 +01:00
Marius Gerbershagen
3f0fc4f855 threading: fix race conditions in ECL_WITH_GLOBAL_ENV_RD/WRLOCK
We can't use ecl_disable_interrupts, because often writes in the
    thread local environment happen while we hold the locks (e.g.
    env->packages_to_be_created is written in find_pending_package
    while the lock is held in ecl_make_package). Therefore we use the
    lisp interrupt blocking mechanism. For this, the order of
    operations in cl_boot has to be modified a bit.
2018-02-18 21:01:44 +01:00
Marius Gerbershagen
e5281a4685 threading: add explanation about stack interrupt safety 2018-02-17 20:58:49 +01:00
Marius Gerbershagen
2193e4b55d fix typo in frs_set_size 2018-02-17 19:13:34 +01:00
Marius Gerbershagen
f0506f511e threading: fix possible race conditions in ecl_wakeup_waiters
Checking process.phase without holding the start_stop_spinlock
    looks dangerous, the thread may exit after the check but before we
    interrupt it. Also, we can't call mp_process_kill while interrupts
    are disabled, so we have to use the lower level ecl_interrupt_process.
2018-02-17 16:24:38 +01:00
Marius Gerbershagen
0ecea9487c move ECL_STACK_RESIZE_DIS/ENABLE_INTERRUPTS in a separate header file
Compilation of lisp files will sometimes fail otherwise, since
    .eclh files can include internal.h
2018-02-16 20:53:59 +01:00
Marius Gerbershagen
bad90d0f65 threading: safer handling of overflows in frame and binding stacks
Previously, the dummy tag was written behind the stack
    boundary. Also added race condition protection to non-inlined
    ecl_bds_bind/push. The memory barriers have been reworked,
    too. AO_store_full has been replaced by AO_full_nop. This is
    sufficient to insert the required memory barrier instructions and
    is implemented in a simpler way by libatomic_ops in some cases.
2018-02-16 19:58:20 +01:00
Marius Gerbershagen
fc29c08d93 threading: use safer method to disable interrupts when resizing stacks
Due to the use of mprotect() for fast interrupt dispatch it is
    not possible to write in the thread local environment when
    interrupts are disabled. We need to use sigprocmask to block
    interrupts in this case.
2018-02-16 19:07:27 +01:00
Marius Gerbershagen
8a68a5c225 threading: fix race condition in ecl_unwind
If ecl_unwind is interrupted with another call to ecl_unwind
    before it has decremented env->frs_top, the second call of
    ecl_unwind may stop too early with its unwinding, leading to
    potential segfaults.
2018-02-14 22:52:22 +01:00
Marius Gerbershagen
e7838e4b86 threading: fix race conditions in CLOS cache
Writes in the cache were not protected against interrupts, leading
    to segfaults when clear_list_from_cache or ecl_search_cache were
    interrupted.
2018-02-14 20:41:58 +01:00
Marius Gerbershagen
3c7085798d threading: only save/restore thread local variables in handle_all_queued when actually needed
We don't need to save/restore outside of signal handlers. Also,
    bignum_registers were not saved. Allocation of the values array
    has been changed to heap allocation, since this array is quite
    large and we may overflow the C stack, if we allocate it there.
2018-02-11 23:22:43 +01:00
Marius Gerbershagen
6ce7ebc19f threading: fix race conditions when interrupted while pushing in the bindings stack
If ecl_bds_push or ecl_bds_bind were interrupted by a call to
    ecl_bds_unwind, segementation faults could occur, because
    env->bds_top->symbol may not have pointed to a valid symbol.
    Also, memory corruption was possible if the functions were
    interrupted after setting slot->symbol but before setting
    slot->value.
2018-02-11 22:20:24 +01:00
Marius Gerbershagen
fac5f3f7fc documentation: add a few sentences to the description of ecl_disable_interrupts
A few typos were also fixed
2018-02-11 22:04:55 +01:00
Marius Gerbershagen
59a6d0ae44 threading: ensure that we don't get interrupted during setjmp
Interrupting a thread during setjmp with a call to ecl_unwind
    leads to segmentation faults, since we try to call longjmp
    before the corresponding setjmp has finished. Thus, we also need
    to wait until setjmp has finished before we can set frs_val of
    the frame.
2018-02-10 21:47:39 +01:00
Marius Gerbershagen
ca5ef0f977 threading: fix race condition when _ecl_frs_push is interrupted with a call to ecl_unwind
If by chance env->frs_top->frs_val has the value ECL_PROTECT_TAG,
    ecl_unwind will stop and call longjmp. However, at this point
    setjmp has not yet been called, leading to a segmentation fault.
2018-02-10 18:11:27 +01:00
Marius Gerbershagen
6d7ec733eb threading: more race condition fixes for interruptions during stack manipulations 2018-02-10 17:54:35 +01:00
Marius Gerbershagen
3ec7c3b749 threading: fix race conditions when interrupted while pushing in the stack
We have to make sure that the stack pointers always point to a
    valid object. This means that we have to increase env->stack_top
    before we change things in the stack.
2018-02-04 21:53:45 +01:00
Marius Gerbershagen
276f4c79ff threading: save/restore more environment elements in handle_all_queued to prevent race conditions
env->stack_top has to be temporarily increased too, to prevent
    it from being overwritten from the interrupting code.
2018-02-04 21:26:08 +01:00
Marius Gerbershagen
b92f30d263 threading: use safer allocation method for interrupt_struct in _ecl_alloc_env 2018-02-03 22:45:33 +01:00
Marius Gerbershagen
11f495f2b3 threading: restore env->function in handle_all_queued
If a thread is interrupted directly after a call to
    ecl_function_dispatch, env->function may be overwritten before
    it is used. Thus we need to save and restore when we
    execute queued signals.
2018-02-03 22:29:04 +01:00
Marius Gerbershagen
1beabdf9a2 threading: fix ecl_import/release_current_thread
Due to the recent changes introduced in ECL_WITH_SPINLOCK_BEGIN,
    we need a functioning environment when we use this macros.
2018-02-02 20:00:24 +01:00
Marius Gerbershagen
e458caf652 threading: fix barrier implementation
The logic im mp_barrier_wait is wrong. decrement_counter returns
    the value of the counter __before__ it is decremented. Before
    the fix, the counter decremented until it reached 0 and then the
    next arriving thread would get stuck in decrement_counter. Also,
    interrupts were not reenabled in all cases.
2018-01-26 20:56:16 +01:00
Marius Gerbershagen
6449d67337 threading: prevent deadlock in ecl_get_spinlock if we already own the lock 2018-01-22 21:58:40 +01:00
Marius Gerbershagen
3946e2031f threading: lock signal_queue_spinlock in queue_signal with the right thread 2018-01-22 21:56:46 +01:00
Marius Gerbershagen
34ca2a2f38 threading: fix newly introduced race condition in mp_process_enable
If mp_process_enable is interrupted after pthread_create, but
    before its exit code is examined, the cleanup code may be run
    even when pthread_create did not fail, so we need to disable
    interrupts in this region.
2018-01-22 21:52:25 +01:00
Marius Gerbershagen
79b77fc7e5 add another forgotten ecl_enable_interrupts 2018-01-22 21:13:07 +01:00
Marius Gerbershagen
30a4e64c97 fix typo in ecl_clear_interrupts_env() 2018-01-22 21:11:24 +01:00
Marius Gerbershagen
1265ab111a threading: add error message for forgotten ecl_enable_interrupts 2018-01-22 21:11:24 +01:00
Marius Gerbershagen
5b28a8fc1f threading: make sure that spinlocks are unlocked
If a thread is killed while it holds a spinlock, the lock will
    never be released, leading to deadlocks. Hence we have to clean
    up spinlocks in ECL_WITH_SPINLOCK_END. In mp_process_enable,
    other cleanup (deallocating the environment, unlisting the
    process) has to performed too.
2018-01-22 21:08:34 +01:00
Marius Gerbershagen
ba8b85fc22 make sure interrupts are enabled again after having been disabled
This is important to prevent race conditions. If interrupts are
    left disabled, the environment may be wrongly write protected by
    an interrupting thread and completely harmless writes in the
    environment can lead to segmentation faults.
2018-01-14 20:26:15 +01:00
Marius Gerbershagen
6316012408 fix race condition when a process during cleanup is interrupted too early by a call to mp_exit_process
If a process, that has already unwound its whole frame stack
  (after ECL_CATCH_ALL_END in thread_entry_point) is interrupted by
  a call to mp_exit_process, ECL will crash with a segmentation
  fault. We thus need to aquire the start_stop_spinlock before we
  unwind the frame stack.
2018-01-07 16:31:40 +01:00
Marius Gerbershagen
f5a503c862 fix segmentation faults when a signal is queued for a thread whose environment is write protected
If a thread is interrupted while interrupts are disabled by C,
    then the signal is queued and the environment is write protected
    by mprotect. If another thread then calls queue_signal, it will
    try to write in the protected environment, leading to a
    segmentation fault. Since mprotect can only protect whole memory
    pages, we need to allocate the pending interrupts and the signal
    queue in a separate struct.
2018-01-06 17:58:59 +01:00
Marius Gerbershagen
9227f4e342 fix #409: order of evaluation of values forms
the fix for #330 is unaffected
2017-12-29 16:58:27 +01:00
Marius Gerbershagen
39000946e3 bytecmp: Make sure that load time forms are applied in the correct order. Fixes #312 2017-12-19 21:13:11 +01:00
Daniel Kochmanski
2e9c58b3d4 mulithreading: fix semaphore-signal
It didn't wake up all processes to check the condition what caused n+1 lag in
condition check for signal-process (when called with n>1). Fixes #421. No
regression test, because this is already tested in sem-signal-* tests (they were
failing).
2017-12-08 13:40:34 +01:00
Daniel Kochmanski
a51f28f6a5 tests: improve some fail explanations, add last-fail var 2017-12-08 13:40:34 +01:00
Daniel Kochmanski
5bb14d94c7 cosmetic: add ignore declaration
see #16.
2017-12-08 13:40:34 +01:00
Daniel Kochmański
fead4ce858 Merge branch 'develop' into 'develop'
Fix for #292

See merge request embeddable-common-lisp/ecl!97
2017-12-08 07:23:29 +00:00
Marius Gerbershagen
31ed58b7c3 add regression test for #292 2017-12-02 22:08:39 +01:00
Marius Gerbershagen
a0a1a54747 don't check type declarations for default values of optional and keyword function arguments
almost all other implementations do the same, so we should also
allow this edge case
2017-12-02 21:49:46 +01:00
Tomek Kurcz
f34938c506 Port the porting ECL section from the old doc 2017-11-25 13:00:47 +01:00