Serban Constantinescu | e210488 | 2013-09-26 11:37:10 +0100 | [diff] [blame] | 1 | /* |
Dan Albert | 6a91887 | 2014-08-05 20:53:31 +0000 | [diff] [blame] | 2 | * Copyright (C) 2013 The Android Open Source Project |
Elliott Hughes | 54a7494 | 2014-01-03 16:40:37 -0800 | [diff] [blame] | 3 | * All rights reserved. |
Serban Constantinescu | e210488 | 2013-09-26 11:37:10 +0100 | [diff] [blame] | 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
Christopher Ferris | c5d3a43 | 2019-09-25 17:50:36 -0700 | [diff] [blame] | 29 | #include <platform/bionic/tls_defines.h> |
Dan Albert | 6a91887 | 2014-08-05 20:53:31 +0000 | [diff] [blame] | 30 | #include <private/bionic_asm.h> |
Peter Collingbourne | b6a592b | 2023-03-24 18:38:05 -0700 | [diff] [blame^] | 31 | #include <private/bionic_asm_offsets.h> |
Dan Albert | 6a91887 | 2014-08-05 20:53:31 +0000 | [diff] [blame] | 32 | #include <asm/signal.h> |
Elliott Hughes | faac8e6 | 2022-10-14 21:36:58 +0000 | [diff] [blame] | 33 | #include <linux/sched.h> |
Serban Constantinescu | e210488 | 2013-09-26 11:37:10 +0100 | [diff] [blame] | 34 | |
Dan Albert | 6a91887 | 2014-08-05 20:53:31 +0000 | [diff] [blame] | 35 | ENTRY(vfork) |
dimitry | fa43252 | 2017-10-25 13:07:45 +0200 | [diff] [blame] | 36 | __BIONIC_WEAK_ASM_FOR_NATIVE_BRIDGE(vfork) |
Josh Gao | 2303283 | 2020-05-07 17:02:19 -0700 | [diff] [blame] | 37 | // x9 = __get_tls()[TLS_SLOT_THREAD_ID] |
| 38 | mrs x9, tpidr_el0 |
| 39 | ldr x9, [x9, #(TLS_SLOT_THREAD_ID * 8)] |
| 40 | |
| 41 | // Set cached_pid_ to 0, vforked_ to 1, and stash the previous value. |
| 42 | mov w0, #0x80000000 |
| 43 | ldr w10, [x9, #20] |
| 44 | str w0, [x9, #20] |
Elliott Hughes | 5891abd | 2015-08-07 18:27:47 -0700 | [diff] [blame] | 45 | |
Peter Collingbourne | b6a592b | 2023-03-24 18:38:05 -0700 | [diff] [blame^] | 46 | mov x0, #SIGCHLD |
Evgenii Stepanov | 3031a7e | 2022-05-12 15:50:47 -0700 | [diff] [blame] | 47 | |
Peter Collingbourne | b6a592b | 2023-03-24 18:38:05 -0700 | [diff] [blame^] | 48 | // If either HWASan or stack MTE is enabled, set up the clone() flags to |
| 49 | // make vfork() act like fork(). We don't call the atfork handlers, so we |
| 50 | // may deadlock if the child allocates, but we have seen badly written |
| 51 | // atfork handlers themselves cause deadlocks [1]. ndk_translation already |
| 52 | // implements vfork() as fork() without calling handlers, so we have some |
| 53 | // evidence that it isn't necessary to call them. |
| 54 | // |
| 55 | // POSIX.1 defines vfork() to have the same effect as fork() except that |
| 56 | // most behavior, including heap allocation, becomes undefined in the child, |
| 57 | // so we aren't violating POSIX by doing this. |
| 58 | // |
| 59 | // [1] https://cs.android.com/android/platform/superproject/+/master:system/extras/simpleperf/app_api/cpp/simpleperf.cpp;drc=788fa4183441f4977ddbd5a055e42a7fe7691d21;l=308 |
| 60 | #if !__has_feature(hwaddress_sanitizer) |
| 61 | // if (!__libc_globals->memtag_stack) x0 |= CLONE_VM | CLONE_VFORK; |
| 62 | adrp x1, __libc_globals + OFFSETOF_libc_globals_memtag_stack |
| 63 | ldrb w1, [x1, :lo12:__libc_globals + OFFSETOF_libc_globals_memtag_stack] |
| 64 | cbnz w1, 1f |
| 65 | orr x0, x0, #CLONE_VM |
| 66 | orr x0, x0, #CLONE_VFORK |
| 67 | 1: |
| 68 | #endif |
Dan Albert | 6a91887 | 2014-08-05 20:53:31 +0000 | [diff] [blame] | 69 | mov x1, xzr |
| 70 | mov x2, xzr |
| 71 | mov x3, xzr |
| 72 | mov x4, xzr |
| 73 | |
| 74 | mov x8, __NR_clone |
| 75 | svc #0 |
| 76 | |
Josh Gao | 2303283 | 2020-05-07 17:02:19 -0700 | [diff] [blame] | 77 | cbz x0, .L_exit |
| 78 | |
| 79 | // rc != 0: reset cached_pid_ and vforked_. |
| 80 | str w10, [x9, #20] |
Dan Albert | 6a91887 | 2014-08-05 20:53:31 +0000 | [diff] [blame] | 81 | cmn x0, #(MAX_ERRNO + 1) |
| 82 | cneg x0, x0, hi |
Elliott Hughes | 011e111 | 2014-09-08 15:25:01 -0700 | [diff] [blame] | 83 | b.hi __set_errno_internal |
Dan Albert | 6a91887 | 2014-08-05 20:53:31 +0000 | [diff] [blame] | 84 | |
Evgenii Stepanov | 505168e | 2019-02-28 18:44:56 -0800 | [diff] [blame] | 85 | .L_exit: |
Dan Albert | 6a91887 | 2014-08-05 20:53:31 +0000 | [diff] [blame] | 86 | ret |
| 87 | END(vfork) |
Tamas Petz | f5bdee7 | 2020-08-31 15:09:40 +0200 | [diff] [blame] | 88 | |
| 89 | NOTE_GNU_PROPERTY() |