blob: addb77590f9891a4776024102fe3691351ceb368 [file] [log] [blame]
Serban Constantinescue2104882013-09-26 11:37:10 +01001/*
Dan Albert6a918872014-08-05 20:53:31 +00002 * Copyright (C) 2013 The Android Open Source Project
Elliott Hughes54a74942014-01-03 16:40:37 -08003 * All rights reserved.
Serban Constantinescue2104882013-09-26 11:37:10 +01004 *
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 Ferrisc5d3a432019-09-25 17:50:36 -070029#include <platform/bionic/tls_defines.h>
Dan Albert6a918872014-08-05 20:53:31 +000030#include <private/bionic_asm.h>
Peter Collingbourneb6a592b2023-03-24 18:38:05 -070031#include <private/bionic_asm_offsets.h>
Dan Albert6a918872014-08-05 20:53:31 +000032#include <asm/signal.h>
Elliott Hughesfaac8e62022-10-14 21:36:58 +000033#include <linux/sched.h>
Serban Constantinescue2104882013-09-26 11:37:10 +010034
Elliott Hughesd7831202024-01-19 20:55:31 +000035ENTRY_WEAK_FOR_NATIVE_BRIDGE(vfork)
Josh Gao23032832020-05-07 17:02:19 -070036 // x9 = __get_tls()[TLS_SLOT_THREAD_ID]
37 mrs x9, tpidr_el0
38 ldr x9, [x9, #(TLS_SLOT_THREAD_ID * 8)]
39
40 // Set cached_pid_ to 0, vforked_ to 1, and stash the previous value.
41 mov w0, #0x80000000
42 ldr w10, [x9, #20]
43 str w0, [x9, #20]
Elliott Hughes5891abd2015-08-07 18:27:47 -070044
Peter Collingbourneb6a592b2023-03-24 18:38:05 -070045 mov x0, #SIGCHLD
Evgenii Stepanov3031a7e2022-05-12 15:50:47 -070046
Peter Collingbourneb6a592b2023-03-24 18:38:05 -070047 // If either HWASan or stack MTE is enabled, set up the clone() flags to
48 // make vfork() act like fork(). We don't call the atfork handlers, so we
49 // may deadlock if the child allocates, but we have seen badly written
50 // atfork handlers themselves cause deadlocks [1]. ndk_translation already
51 // implements vfork() as fork() without calling handlers, so we have some
52 // evidence that it isn't necessary to call them.
53 //
54 // POSIX.1 defines vfork() to have the same effect as fork() except that
55 // most behavior, including heap allocation, becomes undefined in the child,
56 // so we aren't violating POSIX by doing this.
57 //
58 // [1] https://cs.android.com/android/platform/superproject/+/master:system/extras/simpleperf/app_api/cpp/simpleperf.cpp;drc=788fa4183441f4977ddbd5a055e42a7fe7691d21;l=308
59#if !__has_feature(hwaddress_sanitizer)
60 // if (!__libc_globals->memtag_stack) x0 |= CLONE_VM | CLONE_VFORK;
61 adrp x1, __libc_globals + OFFSETOF_libc_globals_memtag_stack
62 ldrb w1, [x1, :lo12:__libc_globals + OFFSETOF_libc_globals_memtag_stack]
63 cbnz w1, 1f
64 orr x0, x0, #CLONE_VM
65 orr x0, x0, #CLONE_VFORK
661:
67#endif
Dan Albert6a918872014-08-05 20:53:31 +000068 mov x1, xzr
69 mov x2, xzr
70 mov x3, xzr
71 mov x4, xzr
72
73 mov x8, __NR_clone
74 svc #0
75
Josh Gao23032832020-05-07 17:02:19 -070076 cbz x0, .L_exit
77
78 // rc != 0: reset cached_pid_ and vforked_.
79 str w10, [x9, #20]
Dan Albert6a918872014-08-05 20:53:31 +000080 cmn x0, #(MAX_ERRNO + 1)
81 cneg x0, x0, hi
Elliott Hughes011e1112014-09-08 15:25:01 -070082 b.hi __set_errno_internal
Dan Albert6a918872014-08-05 20:53:31 +000083
Evgenii Stepanov505168e2019-02-28 18:44:56 -080084.L_exit:
Dan Albert6a918872014-08-05 20:53:31 +000085 ret
86END(vfork)
Tamas Petzf5bdee72020-08-31 15:09:40 +020087
88NOTE_GNU_PROPERTY()