Changeset 15194


Ignore:
Timestamp:
Feb 5, 2012, 3:04:55 PM (8 years ago)
Author:
gb
Message:

Try to address some of the problems exposed by some JVM's "signal
chaining" implementation (where the JVM installs signal handlers
that override the application's and calls those handlers when its
own can't deal with the exception; those calls don't observe things
like SA_ONSTACK or SA_RESTART, and this nonsense gets in the way
of some of CCL's stack-switching nonsense.)

We might instead say:

  • when we install our signal handlers, remember any "foreign" handler that might have been installed previously.
  • if a thread gets an exception signal while in foreign code and the signal had a foreign handler, call that foreign handler rather than dropping into the kernel debugger. (Yes, this'll have some of the same problems in reverse, but we might be able to address some of those problems to at least some degree.)
  • after a JVM is initialized, reestablish CCL's signal handlers.

I don't think that signal chaining can really work in general, so
this is mostly a question of whether CCL or the JVM has to live with
its limitations. (There might also be some performance issues.)

What's here is x86-specific. Android's Dalvik VM seems to only try
to override SIGBUS and that's less intrusive than the x86 situation;
I don't know what JVMs are available in fullblown ARM Linux or whether
any of them try to do signal chaining.

(We can avoid these issues completely for asynchronous signals used
to suspend/interrupt/kill threads if we're simply able to avoid using
conflicting signal numbers for that functionality. That isn't possible
for synchronous hardware exceptions.)

OpenJDK VMs may have options (such as -Xrs) that control whether the
JVM or "native code" handle things like SIGINT/SIGQUIT.

This has been tested (some) on x86 Linux. I'll try to watch the
automatic build system to see if I broke anything on other platforms.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/source/lisp-kernel/x86-exceptions.c

    r15164 r15194  
    4949#endif
    5050#endif
     51
     52/*
     53  We do all kinds of funky things to avoid handling a signal on the lisp
     54  stack.  One of those funky things involves using the  __builtin_return_address()
     55  intrinsic so that the real handler returns to the right place when it exits,
     56  even if it returns on a different stack.  Code at "the right place" is presumed
     57  to just do a sigrereturn, however the OS does that.
     58
     59  Sadly, some JVMs (at least) do what they call "signal chaining": they install
     60  their own handlers for signals that we expect to handle, and call our handler
     61  when they realize that they don't know how to handle what we raise.  They
     62  don't observe sigaltstack, and they don't necessarily do call our handler
     63  tail-recursively (so our stack-switching code would cause our handler to
     64  return to the JVM's, running on the wrong stack.
     65
     66  Try to work around this by setting up an "early" signal handler (before any
     67  of this JVM nonsense can take effect) and noting the address it'd return to.
     68*/
     69
     70pc
     71real_sigreturn = (pc)0;
     72
     73#define SIGRETURN_ADDRESS() (real_sigreturn ? real_sigreturn : __builtin_return_address(0))
     74
     75#ifndef WINDOWS
     76#ifndef DARWIN
     77void
     78early_intn_handler(int signum, siginfo_t *info, ExceptionInformation *xp)
     79{
     80  real_sigreturn = (pc) __builtin_return_address(0);
     81  xpPC(xp) += 2;
     82}
     83
     84#endif
     85#endif
     86
     87void
     88do_intn()
     89{
     90  __asm volatile("int $0xcd");
     91}
     92
     93void
     94x86_early_exception_init()
     95{
     96#ifndef WINDOWS
     97#ifndef DARWIN
     98  struct sigaction action, oaction;
     99
     100  action.sa_sigaction = (void *) early_intn_handler;
     101  sigfillset(&action.sa_mask);
     102  action.sa_flags = SA_SIGINFO;
     103  sigaction(SIGNUM_FOR_INTN_TRAP,&action,&oaction);
     104 
     105  do_intn();
     106  sigaction(SIGNUM_FOR_INTN_TRAP,&oaction, NULL);
     107#endif
     108#endif
     109}
    51110
    52111int
     
    16671726{
    16681727  TCR* tcr = get_tcr(true);
     1728  Boolean do_stack_switch = false;
     1729  stack_t ss;
    16691730
    16701731#if WORD_SIZE==64
     
    16761737#endif
    16771738     
    1678   handle_signal_on_foreign_stack(tcr,signal_handler,signum,info,context,(LispObj)__builtin_return_address(0)
    1679 );
     1739  /* Because of signal chaining - and the possibility that libaries
     1740     that use it ignore sigaltstack-related issues - we have to check
     1741     to see if we're actually on the altstack.
     1742
     1743     When OpenJDK VMs overwrite preinstalled signal handlers (that're
     1744     there for a reason ...), they're also casual about SA_RESTART.
     1745     We care about SA_RESTART (mostly) in the PROCESS-INTERRUPT case,
     1746     and whether a JVM steals the signal used for PROCESS-INTERRUPT
     1747     is platform-dependent.  On those platforms where the same signal
     1748     is used, we should strongly consider trying to use another one.
     1749  */
     1750  sigaltstack(NULL, &ss);
     1751  if (ss.ss_flags == SS_ONSTACK) {
     1752    do_stack_switch = true;
     1753  } else {
     1754    area *vs = tcr->vs_area;
     1755    BytePtr current_sp = (BytePtr) current_stack_pointer();
     1756
     1757    if ((current_sp >= vs->low) &&
     1758        (current_sp < vs->high)) {
     1759      do_stack_switch = true;
     1760    }
     1761  }
     1762  if (do_stack_switch) {
     1763    handle_signal_on_foreign_stack(tcr,signal_handler,signum,info,context,(LispObj)SIGRETURN_ADDRESS());
     1764  } else {
     1765    signal_handler(signum,info,context);
     1766  }
    16801767}
    16811768#endif
     
    17931880
    17941881#else /* altstack works */
    1795  
     1882
     1883/*
     1884   There aren't likely any JVM-related signal-chaining issues here, since
     1885   on platforms where that could be an issue we use either an RT signal
     1886   or an unused synchronous hardware signal to raise interrupts.
     1887*/
    17961888void
    17971889altstack_interrupt_handler (int signum, siginfo_t *info, ExceptionInformation *context)
     
    20972189
    20982190#else /* altstack works */
     2191
    20992192void
    21002193altstack_suspend_resume_handler(int signum, siginfo_t *info, ExceptionInformation  *context)
     
    22342327exception_init()
    22352328{
     2329  x86_early_exception_init();
    22362330  install_pmcl_exception_handlers();
    22372331}
Note: See TracChangeset for help on using the changeset viewer.