The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | /*- |
| 2 | * Copyright (c) 1990 The Regents of the University of California. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * This code is derived from software contributed to Berkeley by |
| 6 | * William Jolitz. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in the |
| 15 | * documentation and/or other materials provided with the distribution. |
| 16 | * 3. Neither the name of the University nor the names of its contributors |
| 17 | * may be used to endorse or promote products derived from this software |
| 18 | * without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 30 | * SUCH DAMAGE. |
| 31 | */ |
| 32 | |
Elliott Hughes | 851e68a | 2014-02-19 16:53:20 -0800 | [diff] [blame] | 33 | #include <private/bionic_asm.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 34 | |
Josh Gao | 9260785 | 2016-03-29 14:03:09 -0700 | [diff] [blame] | 35 | // The internal structure of a jmp_buf is totally private. |
| 36 | // Current layout (changes from release to release): |
| 37 | // |
| 38 | // word name description |
| 39 | // 0 edx registers |
| 40 | // 1 ebx |
| 41 | // 2 esp |
| 42 | // 3 ebp |
| 43 | // 4 esi |
| 44 | // 5 edi |
Elliott Hughes | 460130b | 2018-01-31 09:05:26 -0800 | [diff] [blame] | 45 | // 6 sigmask 64-bit signal mask (not used with _setjmp / _longjmp) |
| 46 | // 7 " " |
| 47 | // 8 sigflag/cookie setjmp cookie in top 31 bits, signal mask flag in low bit |
| 48 | // 9 checksum checksum of the core registers, to give better error messages. |
Josh Gao | 9260785 | 2016-03-29 14:03:09 -0700 | [diff] [blame] | 49 | |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 50 | #define _JB_EDX 0 |
| 51 | #define _JB_EBX 1 |
| 52 | #define _JB_ESP 2 |
| 53 | #define _JB_EBP 3 |
| 54 | #define _JB_ESI 4 |
| 55 | #define _JB_EDI 5 |
| 56 | #define _JB_SIGMASK 6 |
Elliott Hughes | 460130b | 2018-01-31 09:05:26 -0800 | [diff] [blame] | 57 | #define _JB_SIGFLAG 8 |
| 58 | #define _JB_CHECKSUM 9 |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 59 | |
Josh Gao | 9260785 | 2016-03-29 14:03:09 -0700 | [diff] [blame] | 60 | .macro m_calculate_checksum dst, src |
| 61 | movl $0, \dst |
| 62 | .irp i,0,1,2,3,4,5 |
| 63 | xorl (\i*4)(\src), \dst |
| 64 | .endr |
| 65 | .endm |
| 66 | |
Elliott Hughes | d783120 | 2024-01-19 20:55:31 +0000 | [diff] [blame] | 67 | ENTRY_WEAK_FOR_NATIVE_BRIDGE(setjmp) |
Josh Gao | 85c14fb | 2015-09-15 11:30:35 -0700 | [diff] [blame] | 68 | mov $1,%eax |
| 69 | jmp .L_sigsetjmp |
Elliott Hughes | 6719500 | 2013-02-13 15:12:32 -0800 | [diff] [blame] | 70 | END(setjmp) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 71 | |
Elliott Hughes | d783120 | 2024-01-19 20:55:31 +0000 | [diff] [blame] | 72 | ENTRY_WEAK_FOR_NATIVE_BRIDGE(_setjmp) |
Josh Gao | 85c14fb | 2015-09-15 11:30:35 -0700 | [diff] [blame] | 73 | movl $0,%eax |
| 74 | jmp .L_sigsetjmp |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 75 | END(_setjmp) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 76 | |
Elliott Hughes | d783120 | 2024-01-19 20:55:31 +0000 | [diff] [blame] | 77 | ENTRY_WEAK_FOR_NATIVE_BRIDGE(sigsetjmp) |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 78 | movl 8(%esp),%eax |
| 79 | |
Josh Gao | 85c14fb | 2015-09-15 11:30:35 -0700 | [diff] [blame] | 80 | .L_sigsetjmp: |
| 81 | PIC_PROLOGUE |
| 82 | pushl %eax |
| 83 | call PIC_PLT(__bionic_setjmp_cookie_get) |
| 84 | addl $4,%esp |
| 85 | PIC_EPILOGUE |
| 86 | |
| 87 | // Record the setjmp cookie and whether or not we're saving the signal mask. |
Peter Collingbourne | 8bd83d8 | 2024-04-03 19:28:06 -0700 | [diff] [blame] | 88 | movl 4(%esp),%ecx |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 89 | movl %eax,(_JB_SIGFLAG * 4)(%ecx) |
| 90 | |
| 91 | // Do we need to save the signal mask? |
Josh Gao | 85c14fb | 2015-09-15 11:30:35 -0700 | [diff] [blame] | 92 | testl $1,%eax |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 93 | jz 1f |
| 94 | |
Elliott Hughes | 7ebafb3 | 2018-01-29 10:23:01 -0800 | [diff] [blame] | 95 | // Save the current signal mask. |
| 96 | pushl %ecx |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 97 | PIC_PROLOGUE |
Elliott Hughes | 7ebafb3 | 2018-01-29 10:23:01 -0800 | [diff] [blame] | 98 | leal (_JB_SIGMASK * 4)(%ecx),%eax |
| 99 | pushl %eax |
| 100 | pushl $0 // NULL |
| 101 | pushl $2 // SIG_SETMASK |
Elliott Hughes | 460130b | 2018-01-31 09:05:26 -0800 | [diff] [blame] | 102 | call PIC_PLT(sigprocmask64) |
Elliott Hughes | 7ebafb3 | 2018-01-29 10:23:01 -0800 | [diff] [blame] | 103 | addl $12,%esp |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 104 | PIC_EPILOGUE |
Elliott Hughes | 7ebafb3 | 2018-01-29 10:23:01 -0800 | [diff] [blame] | 105 | popl %ecx |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 106 | |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 107 | 1: |
Josh Gao | 85c14fb | 2015-09-15 11:30:35 -0700 | [diff] [blame] | 108 | // Fetch the setjmp cookie and clear the signal flag bit. |
| 109 | movl (_JB_SIGFLAG * 4)(%ecx),%eax |
| 110 | andl $-2,%eax |
| 111 | |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 112 | // Save the callee-save registers. |
| 113 | movl 0(%esp),%edx |
Elliott Hughes | c0d41db | 2021-04-02 18:02:38 -0700 | [diff] [blame] | 114 | |
| 115 | .macro m_mangle_register reg, offset |
| 116 | movl \reg,(\offset * 4)(%ecx) |
| 117 | xorl %eax,(\offset * 4)(%ecx) |
| 118 | .endm |
| 119 | m_mangle_register %edx, _JB_EDX |
| 120 | m_mangle_register %ebx, _JB_EBX |
| 121 | m_mangle_register %esp, _JB_ESP |
| 122 | m_mangle_register %ebp, _JB_EBP |
| 123 | m_mangle_register %esi, _JB_ESI |
| 124 | m_mangle_register %edi, _JB_EDI |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 125 | |
Josh Gao | 9260785 | 2016-03-29 14:03:09 -0700 | [diff] [blame] | 126 | m_calculate_checksum %eax, %ecx |
| 127 | movl %eax, (_JB_CHECKSUM * 4)(%ecx) |
| 128 | |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 129 | xorl %eax,%eax |
| 130 | ret |
| 131 | END(sigsetjmp) |
| 132 | |
Elliott Hughes | d783120 | 2024-01-19 20:55:31 +0000 | [diff] [blame] | 133 | ENTRY_WEAK_FOR_NATIVE_BRIDGE(siglongjmp) |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 134 | movl 4(%esp),%edx |
Josh Gao | 9260785 | 2016-03-29 14:03:09 -0700 | [diff] [blame] | 135 | |
| 136 | // Check the checksum before doing anything. |
| 137 | m_calculate_checksum %eax, %edx |
| 138 | xorl (_JB_CHECKSUM * 4)(%edx), %eax |
| 139 | jnz 3f |
| 140 | |
| 141 | // Do we have a signal mask to restore? |
Josh Gao | 85c14fb | 2015-09-15 11:30:35 -0700 | [diff] [blame] | 142 | movl (_JB_SIGFLAG * 4)(%edx), %eax |
| 143 | testl $1,%eax |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 144 | jz 1f |
| 145 | |
| 146 | // Restore the signal mask. |
Elliott Hughes | 7ebafb3 | 2018-01-29 10:23:01 -0800 | [diff] [blame] | 147 | leal (_JB_SIGMASK * 4)(%edx),%eax |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 148 | PIC_PROLOGUE |
Elliott Hughes | 7ebafb3 | 2018-01-29 10:23:01 -0800 | [diff] [blame] | 149 | pushl $0 // NULL |
| 150 | pushl %eax |
| 151 | pushl $2 // SIG_SETMASK |
Elliott Hughes | 460130b | 2018-01-31 09:05:26 -0800 | [diff] [blame] | 152 | call PIC_PLT(sigprocmask64) |
Elliott Hughes | 7ebafb3 | 2018-01-29 10:23:01 -0800 | [diff] [blame] | 153 | addl $12,%esp |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 154 | PIC_EPILOGUE |
| 155 | |
| 156 | 1: |
| 157 | // Restore the callee-save registers. |
| 158 | movl 4(%esp),%edx |
| 159 | movl 8(%esp),%eax |
Josh Gao | 85c14fb | 2015-09-15 11:30:35 -0700 | [diff] [blame] | 160 | |
Elliott Hughes | c0d41db | 2021-04-02 18:02:38 -0700 | [diff] [blame] | 161 | // Fetch the setjmp cookie and clear the signal flag bit. |
Josh Gao | 85c14fb | 2015-09-15 11:30:35 -0700 | [diff] [blame] | 162 | movl (_JB_SIGFLAG * 4)(%edx),%ecx |
| 163 | andl $-2,%ecx |
| 164 | |
Elliott Hughes | c0d41db | 2021-04-02 18:02:38 -0700 | [diff] [blame] | 165 | // Carefully unmangle esp/ebp without ever having an invalid value in the |
| 166 | // register (http://b/152210274). |
| 167 | movl (_JB_ESP * 4)(%edx),%edi |
| 168 | xorl %ecx,%edi |
| 169 | movl %edi,%esp |
| 170 | movl (_JB_EBP * 4)(%edx),%edi |
| 171 | xorl %ecx,%edi |
| 172 | movl %edi,%ebp |
| 173 | |
| 174 | // The others don't matter as much, but we do need to finish using the cookie |
| 175 | // from %ecx before we clobber it, so we seed each register with the cookie. |
Josh Gao | 85c14fb | 2015-09-15 11:30:35 -0700 | [diff] [blame] | 176 | movl %ecx,%ebx |
Josh Gao | 85c14fb | 2015-09-15 11:30:35 -0700 | [diff] [blame] | 177 | movl %ecx,%esi |
| 178 | movl %ecx,%edi |
| 179 | xorl (_JB_EDX * 4)(%edx),%ecx |
| 180 | xorl (_JB_EBX * 4)(%edx),%ebx |
Josh Gao | 85c14fb | 2015-09-15 11:30:35 -0700 | [diff] [blame] | 181 | xorl (_JB_ESI * 4)(%edx),%esi |
| 182 | xorl (_JB_EDI * 4)(%edx),%edi |
| 183 | |
| 184 | PIC_PROLOGUE |
| 185 | pushl %eax |
Josh Gao | 8dbf02d | 2015-10-07 13:51:59 -0700 | [diff] [blame] | 186 | pushl %ecx |
Josh Gao | 85c14fb | 2015-09-15 11:30:35 -0700 | [diff] [blame] | 187 | pushl (_JB_SIGFLAG * 4)(%edx) |
| 188 | call PIC_PLT(__bionic_setjmp_cookie_check) |
| 189 | addl $4,%esp |
Josh Gao | 8dbf02d | 2015-10-07 13:51:59 -0700 | [diff] [blame] | 190 | popl %ecx |
Josh Gao | 85c14fb | 2015-09-15 11:30:35 -0700 | [diff] [blame] | 191 | popl %eax |
| 192 | PIC_EPILOGUE |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 193 | |
| 194 | testl %eax,%eax |
| 195 | jnz 2f |
| 196 | incl %eax |
| 197 | 2: |
| 198 | movl %ecx,0(%esp) |
| 199 | ret |
Josh Gao | 9260785 | 2016-03-29 14:03:09 -0700 | [diff] [blame] | 200 | |
| 201 | 3: |
| 202 | PIC_PROLOGUE |
| 203 | pushl (_JB_SIGMASK * 4)(%edx) |
| 204 | call PIC_PLT(__bionic_setjmp_checksum_mismatch) |
Elliott Hughes | 8d4c55c | 2014-12-05 16:25:50 -0800 | [diff] [blame] | 205 | END(siglongjmp) |
| 206 | |
Elliott Hughes | d783120 | 2024-01-19 20:55:31 +0000 | [diff] [blame] | 207 | ALIAS_SYMBOL_WEAK_FOR_NATIVE_BRIDGE(longjmp, siglongjmp) |
| 208 | ALIAS_SYMBOL_WEAK_FOR_NATIVE_BRIDGE(_longjmp, siglongjmp) |