diff options
author | Douglas Leung <douglas.leung@imgtec.com> | 2015-07-02 16:42:08 -0700 |
---|---|---|
committer | Roland Levillain <rpl@google.com> | 2015-07-03 17:29:27 +0100 |
commit | ccbbda2b716bcc0dd9ad7b6c7bf9079efa3fca23 (patch) | |
tree | 52b01bc48dadce390c2e4ceffacdc16f674d3dd0 /runtime | |
parent | 3abd437507f8ba30a238a52c273c9944dcb9d5a1 (diff) | |
download | art-ccbbda2b716bcc0dd9ad7b6c7bf9079efa3fca23.zip art-ccbbda2b716bcc0dd9ad7b6c7bf9079efa3fca23.tar.gz art-ccbbda2b716bcc0dd9ad7b6c7bf9079efa3fca23.tar.bz2 |
Add implicit null pointer and stack overflow checks for Mips.
(cherry picked from commit 22bb5a2ebc1e2724179faf4660b2735dcb185f21)
Bug: 21555893
Change-Id: I2a995be128a5603d08753c14956dd8c8240ac63c
Diffstat (limited to 'runtime')
-rw-r--r-- | runtime/arch/mips/fault_handler_mips.cc | 105 | ||||
-rw-r--r-- | runtime/arch/mips64/fault_handler_mips64.cc | 105 | ||||
-rw-r--r-- | runtime/runtime.cc | 2 |
3 files changed, 192 insertions, 20 deletions
diff --git a/runtime/arch/mips/fault_handler_mips.cc b/runtime/arch/mips/fault_handler_mips.cc index abe495b..8ea78eb 100644 --- a/runtime/arch/mips/fault_handler_mips.cc +++ b/runtime/arch/mips/fault_handler_mips.cc @@ -17,11 +17,17 @@ #include "fault_handler.h" #include <sys/ucontext.h> +#include "art_method-inl.h" #include "base/macros.h" #include "globals.h" #include "base/logging.h" #include "base/hex_dump.h" +#include "registers_mips.h" +#include "thread.h" +#include "thread-inl.h" +extern "C" void art_quick_throw_stack_overflow(); +extern "C" void art_quick_throw_null_pointer_exception(); // // Mips specific fault handler functions. @@ -33,16 +39,52 @@ void FaultManager::HandleNestedSignal(int sig ATTRIBUTE_UNUSED, siginfo_t* info void* context ATTRIBUTE_UNUSED) { } -void FaultManager::GetMethodAndReturnPcAndSp(siginfo_t* siginfo ATTRIBUTE_UNUSED, - void* context ATTRIBUTE_UNUSED, - ArtMethod** out_method ATTRIBUTE_UNUSED, - uintptr_t* out_return_pc ATTRIBUTE_UNUSED, - uintptr_t* out_sp ATTRIBUTE_UNUSED) { +void FaultManager::GetMethodAndReturnPcAndSp(siginfo_t* siginfo, void* context, + ArtMethod** out_method, + uintptr_t* out_return_pc, uintptr_t* out_sp) { + struct ucontext* uc = reinterpret_cast<struct ucontext*>(context); + struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext); + *out_sp = static_cast<uintptr_t>(sc->sc_regs[29]); // SP register + VLOG(signals) << "sp: " << *out_sp; + if (*out_sp == 0) { + return; + } + + // In the case of a stack overflow, the stack is not valid and we can't + // get the method from the top of the stack. However it's in r0. + uintptr_t* fault_addr = reinterpret_cast<uintptr_t*>(siginfo->si_addr); // BVA addr + uintptr_t* overflow_addr = reinterpret_cast<uintptr_t*>( + reinterpret_cast<uint8_t*>(*out_sp) - GetStackOverflowReservedBytes(kMips)); + if (overflow_addr == fault_addr) { + *out_method = reinterpret_cast<ArtMethod*>(sc->sc_regs[4]); // A0 register + } else { + // The method is at the top of the stack. + *out_method = *reinterpret_cast<ArtMethod**>(*out_sp); + } + + // Work out the return PC. This will be the address of the instruction + // following the faulting ldr/str instruction. + + VLOG(signals) << "pc: " << std::hex + << static_cast<void*>(reinterpret_cast<uint8_t*>(sc->sc_pc)); + + *out_return_pc = sc->sc_pc + 4; } bool NullPointerHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* info ATTRIBUTE_UNUSED, - void* context ATTRIBUTE_UNUSED) { - return false; + void* context) { + // The code that looks for the catch location needs to know the value of the + // PC at the point of call. For Null checks we insert a GC map that is immediately after + // the load/store instruction that might cause the fault. + + struct ucontext *uc = reinterpret_cast<struct ucontext*>(context); + struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext); + + sc->sc_regs[31] = sc->sc_pc + 4; // RA needs to point to gc map location + sc->sc_pc = reinterpret_cast<uintptr_t>(art_quick_throw_null_pointer_exception); + sc->sc_regs[25] = sc->sc_pc; // make sure T9 points to the function + VLOG(signals) << "Generating null pointer exception"; + return true; } bool SuspensionHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* info ATTRIBUTE_UNUSED, @@ -50,8 +92,51 @@ bool SuspensionHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* info ATTRIBU return false; } -bool StackOverflowHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* info ATTRIBUTE_UNUSED, - void* context ATTRIBUTE_UNUSED) { - return false; +// Stack overflow fault handler. +// +// This checks that the fault address is equal to the current stack pointer +// minus the overflow region size (16K typically). The instruction that +// generates this signal is: +// +// lw zero, -16384(sp) +// +// It will fault if sp is inside the protected region on the stack. +// +// If we determine this is a stack overflow we need to move the stack pointer +// to the overflow region below the protected region. + +bool StackOverflowHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* info, void* context) { + struct ucontext* uc = reinterpret_cast<struct ucontext*>(context); + struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext); + VLOG(signals) << "stack overflow handler with sp at " << std::hex << &uc; + VLOG(signals) << "sigcontext: " << std::hex << sc; + + uintptr_t sp = sc->sc_regs[29]; // SP register + VLOG(signals) << "sp: " << std::hex << sp; + + uintptr_t fault_addr = reinterpret_cast<uintptr_t>(info->si_addr); // BVA addr + VLOG(signals) << "fault_addr: " << std::hex << fault_addr; + VLOG(signals) << "checking for stack overflow, sp: " << std::hex << sp << + ", fault_addr: " << fault_addr; + + uintptr_t overflow_addr = sp - GetStackOverflowReservedBytes(kMips); + + // Check that the fault address is the value expected for a stack overflow. + if (fault_addr != overflow_addr) { + VLOG(signals) << "Not a stack overflow"; + return false; + } + + VLOG(signals) << "Stack overflow found"; + + // Now arrange for the signal handler to return to art_quick_throw_stack_overflow_from. + // The value of RA must be the same as it was when we entered the code that + // caused this fault. This will be inserted into a callee save frame by + // the function to which this handler returns (art_quick_throw_stack_overflow). + sc->sc_pc = reinterpret_cast<uintptr_t>(art_quick_throw_stack_overflow); + sc->sc_regs[25] = sc->sc_pc; // make sure T9 points to the function + + // The kernel will now return to the address in sc->arm_pc. + return true; } } // namespace art diff --git a/runtime/arch/mips64/fault_handler_mips64.cc b/runtime/arch/mips64/fault_handler_mips64.cc index 277c2b2..4abfcf1 100644 --- a/runtime/arch/mips64/fault_handler_mips64.cc +++ b/runtime/arch/mips64/fault_handler_mips64.cc @@ -17,11 +17,17 @@ #include "fault_handler.h" #include <sys/ucontext.h> +#include "art_method-inl.h" #include "base/macros.h" #include "globals.h" #include "base/logging.h" #include "base/hex_dump.h" +#include "registers_mips64.h" +#include "thread.h" +#include "thread-inl.h" +extern "C" void art_quick_throw_stack_overflow(); +extern "C" void art_quick_throw_null_pointer_exception(); // // Mips64 specific fault handler functions. @@ -33,16 +39,52 @@ void FaultManager::HandleNestedSignal(int sig ATTRIBUTE_UNUSED, siginfo_t* info void* context ATTRIBUTE_UNUSED) { } -void FaultManager::GetMethodAndReturnPcAndSp(siginfo_t* siginfo ATTRIBUTE_UNUSED, - void* context ATTRIBUTE_UNUSED, - ArtMethod** out_method ATTRIBUTE_UNUSED, - uintptr_t* out_return_pc ATTRIBUTE_UNUSED, - uintptr_t* out_sp ATTRIBUTE_UNUSED) { +void FaultManager::GetMethodAndReturnPcAndSp(siginfo_t* siginfo, void* context, + ArtMethod** out_method, + uintptr_t* out_return_pc, uintptr_t* out_sp) { + struct ucontext* uc = reinterpret_cast<struct ucontext*>(context); + struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext); + *out_sp = static_cast<uintptr_t>(sc->sc_regs[29]); // SP register + VLOG(signals) << "sp: " << *out_sp; + if (*out_sp == 0) { + return; + } + + // In the case of a stack overflow, the stack is not valid and we can't + // get the method from the top of the stack. However it's in r0. + uintptr_t* fault_addr = reinterpret_cast<uintptr_t*>(siginfo->si_addr); // BVA addr + uintptr_t* overflow_addr = reinterpret_cast<uintptr_t*>( + reinterpret_cast<uint8_t*>(*out_sp) - GetStackOverflowReservedBytes(kMips64)); + if (overflow_addr == fault_addr) { + *out_method = reinterpret_cast<ArtMethod*>(sc->sc_regs[4]); // A0 register + } else { + // The method is at the top of the stack. + *out_method = *reinterpret_cast<ArtMethod**>(*out_sp); + } + + // Work out the return PC. This will be the address of the instruction + // following the faulting ldr/str instruction. + + VLOG(signals) << "pc: " << std::hex + << static_cast<void*>(reinterpret_cast<uint8_t*>(sc->sc_pc)); + + *out_return_pc = sc->sc_pc + 4; } bool NullPointerHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* info ATTRIBUTE_UNUSED, - void* context ATTRIBUTE_UNUSED) { - return false; + void* context) { + // The code that looks for the catch location needs to know the value of the + // PC at the point of call. For Null checks we insert a GC map that is immediately after + // the load/store instruction that might cause the fault. + + struct ucontext *uc = reinterpret_cast<struct ucontext*>(context); + struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext); + + sc->sc_regs[31] = sc->sc_pc + 4; // RA needs to point to gc map location + sc->sc_pc = reinterpret_cast<uintptr_t>(art_quick_throw_null_pointer_exception); + sc->sc_regs[25] = sc->sc_pc; // make sure T9 points to the function + VLOG(signals) << "Generating null pointer exception"; + return true; } bool SuspensionHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* info ATTRIBUTE_UNUSED, @@ -50,8 +92,51 @@ bool SuspensionHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* info ATTRIBU return false; } -bool StackOverflowHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* info ATTRIBUTE_UNUSED, - void* context ATTRIBUTE_UNUSED) { - return false; +// Stack overflow fault handler. +// +// This checks that the fault address is equal to the current stack pointer +// minus the overflow region size (16K typically). The instruction that +// generates this signal is: +// +// lw zero, -16384(sp) +// +// It will fault if sp is inside the protected region on the stack. +// +// If we determine this is a stack overflow we need to move the stack pointer +// to the overflow region below the protected region. + +bool StackOverflowHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* info, void* context) { + struct ucontext* uc = reinterpret_cast<struct ucontext*>(context); + struct sigcontext *sc = reinterpret_cast<struct sigcontext*>(&uc->uc_mcontext); + VLOG(signals) << "stack overflow handler with sp at " << std::hex << &uc; + VLOG(signals) << "sigcontext: " << std::hex << sc; + + uintptr_t sp = sc->sc_regs[29]; // SP register + VLOG(signals) << "sp: " << std::hex << sp; + + uintptr_t fault_addr = reinterpret_cast<uintptr_t>(info->si_addr); // BVA addr + VLOG(signals) << "fault_addr: " << std::hex << fault_addr; + VLOG(signals) << "checking for stack overflow, sp: " << std::hex << sp << + ", fault_addr: " << fault_addr; + + uintptr_t overflow_addr = sp - GetStackOverflowReservedBytes(kMips64); + + // Check that the fault address is the value expected for a stack overflow. + if (fault_addr != overflow_addr) { + VLOG(signals) << "Not a stack overflow"; + return false; + } + + VLOG(signals) << "Stack overflow found"; + + // Now arrange for the signal handler to return to art_quick_throw_stack_overflow_from. + // The value of RA must be the same as it was when we entered the code that + // caused this fault. This will be inserted into a callee save frame by + // the function to which this handler returns (art_quick_throw_stack_overflow). + sc->sc_pc = reinterpret_cast<uintptr_t>(art_quick_throw_stack_overflow); + sc->sc_regs[25] = sc->sc_pc; // make sure T9 points to the function + + // The kernel will now return to the address in sc->arm_pc. + return true; } } // namespace art diff --git a/runtime/runtime.cc b/runtime/runtime.cc index 6c55129..1453e9f 100644 --- a/runtime/runtime.cc +++ b/runtime/runtime.cc @@ -921,6 +921,8 @@ bool Runtime::Init(const RuntimeOptions& raw_options, bool ignore_unrecognized) case kX86: case kArm64: case kX86_64: + case kMips: + case kMips64: implicit_null_checks_ = true; // Installing stack protection does not play well with valgrind. implicit_so_checks_ = (RUNNING_ON_VALGRIND == 0); |