From 4956c3437bd2f4448bcec51321f123d03731ddfc Mon Sep 17 00:00:00 2001 From: Cherry Mui Date: Mon, 27 Nov 2023 15:17:11 -0500 Subject: [PATCH] syscall: remove ptrace1 on darwin On Darwin, the ptrace syscall is called in ptrace1, which then be called in ptrace. This allows ptrace1 be disabled on iOS (by implementing ptrace differently). But we can also achieve this by adding a conditional directly in ptrace. This reduces stack usage with -N -l, while keeping ptrace disabled on iOS. For #64113. Change-Id: I89d8e317e77352fffdbb5a25ba21ee9cdf2e1e20 Reviewed-on: https://go-review.googlesource.com/c/go/+/545276 Reviewed-by: Michael Knyszek Run-TryBot: Cherry Mui TryBot-Result: Gopher Robot --- src/syscall/mksyscall.pl | 12 +++++++++++- src/syscall/ptrace_darwin.go | 14 -------------- src/syscall/ptrace_ios.go | 14 -------------- src/syscall/syscall_darwin_amd64.go | 2 +- src/syscall/syscall_darwin_arm64.go | 2 +- src/syscall/zsyscall_darwin_amd64.go | 6 +++++- src/syscall/zsyscall_darwin_arm64.go | 6 +++++- 7 files changed, 23 insertions(+), 33 deletions(-) delete mode 100644 src/syscall/ptrace_darwin.go delete mode 100644 src/syscall/ptrace_ios.go diff --git a/src/syscall/mksyscall.pl b/src/syscall/mksyscall.pl index 73d4b0f6e3..47efbffcbc 100755 --- a/src/syscall/mksyscall.pl +++ b/src/syscall/mksyscall.pl @@ -85,6 +85,9 @@ if($ARGV[0] =~ /^-/) { if($libc) { $extraimports = 'import "internal/abi"'; } +if($darwin) { + $extraimports .= "\nimport \"runtime\""; +} sub parseparamlist($) { my ($list) = @_; @@ -137,7 +140,7 @@ while(<>) { # without reading the header. $text .= "// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT\n\n"; - if (($darwin && $func =~ /^ptrace1(Ptr)?$/) || (($openbsd && $libc) && $func =~ /^ptrace(Ptr)?$/)) { + if ((($darwin || ($openbsd && $libc)) && $func =~ /^ptrace(Ptr)?$/)) { # The ptrace function is called from forkAndExecInChild where stack # growth is forbidden. $text .= "//go:nosplit\n" @@ -147,6 +150,13 @@ while(<>) { my $out_decl = @out ? sprintf(" (%s)", join(', ', @out)) : ""; $text .= sprintf "func %s(%s)%s {\n", $func, join(', ', @in), $out_decl; + # Disable ptrace on iOS. + if ($darwin && $func =~ /^ptrace(Ptr)?$/) { + $text .= "\tif runtime.GOOS == \"ios\" {\n"; + $text .= "\t\tpanic(\"unimplemented\")\n"; + $text .= "\t}\n"; + } + # Check if err return available my $errvar = ""; foreach my $p (@out) { diff --git a/src/syscall/ptrace_darwin.go b/src/syscall/ptrace_darwin.go deleted file mode 100644 index 519e451c73..0000000000 --- a/src/syscall/ptrace_darwin.go +++ /dev/null @@ -1,14 +0,0 @@ -// Copyright 2020 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -//go:build !ios - -package syscall - -// Nosplit because it is called from forkAndExecInChild. -// -//go:nosplit -func ptrace(request int, pid int, addr uintptr, data uintptr) error { - return ptrace1(request, pid, addr, data) -} diff --git a/src/syscall/ptrace_ios.go b/src/syscall/ptrace_ios.go deleted file mode 100644 index fa8d000715..0000000000 --- a/src/syscall/ptrace_ios.go +++ /dev/null @@ -1,14 +0,0 @@ -// Copyright 2020 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -//go:build ios - -package syscall - -// Nosplit because it is called from forkAndExecInChild. -// -//go:nosplit -func ptrace(request int, pid int, addr uintptr, data uintptr) (err error) { - panic("unimplemented") -} diff --git a/src/syscall/syscall_darwin_amd64.go b/src/syscall/syscall_darwin_amd64.go index ef3c1998aa..64e54ad730 100644 --- a/src/syscall/syscall_darwin_amd64.go +++ b/src/syscall/syscall_darwin_amd64.go @@ -24,7 +24,7 @@ func setTimeval(sec, usec int64) Timeval { //sys Stat(path string, stat *Stat_t) (err error) = SYS_stat64 //sys Statfs(path string, stat *Statfs_t) (err error) = SYS_statfs64 //sys fstatat(fd int, path string, stat *Stat_t, flags int) (err error) = SYS_fstatat64 -//sys ptrace1(request int, pid int, addr uintptr, data uintptr) (err error) = SYS_ptrace +//sys ptrace(request int, pid int, addr uintptr, data uintptr) (err error) func SetKevent(k *Kevent_t, fd, mode, flags int) { k.Ident = uint64(fd) diff --git a/src/syscall/syscall_darwin_arm64.go b/src/syscall/syscall_darwin_arm64.go index cea42772bb..913c748374 100644 --- a/src/syscall/syscall_darwin_arm64.go +++ b/src/syscall/syscall_darwin_arm64.go @@ -24,7 +24,7 @@ func setTimeval(sec, usec int64) Timeval { //sys Stat(path string, stat *Stat_t) (err error) //sys Statfs(path string, stat *Statfs_t) (err error) //sys fstatat(fd int, path string, stat *Stat_t, flags int) (err error) -//sys ptrace1(request int, pid int, addr uintptr, data uintptr) (err error) = SYS_ptrace +//sys ptrace(request int, pid int, addr uintptr, data uintptr) (err error) func SetKevent(k *Kevent_t, fd, mode, flags int) { k.Ident = uint64(fd) diff --git a/src/syscall/zsyscall_darwin_amd64.go b/src/syscall/zsyscall_darwin_amd64.go index 3ad9bad076..8812fb12cd 100644 --- a/src/syscall/zsyscall_darwin_amd64.go +++ b/src/syscall/zsyscall_darwin_amd64.go @@ -7,6 +7,7 @@ package syscall import "unsafe" import "internal/abi" +import "runtime" // THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT @@ -2011,7 +2012,10 @@ func libc_fstatat64_trampoline() // THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT //go:nosplit -func ptrace1(request int, pid int, addr uintptr, data uintptr) (err error) { +func ptrace(request int, pid int, addr uintptr, data uintptr) (err error) { + if runtime.GOOS == "ios" { + panic("unimplemented") + } _, _, e1 := syscall6(abi.FuncPCABI0(libc_ptrace_trampoline), uintptr(request), uintptr(pid), uintptr(addr), uintptr(data), 0, 0) if e1 != 0 { err = errnoErr(e1) diff --git a/src/syscall/zsyscall_darwin_arm64.go b/src/syscall/zsyscall_darwin_arm64.go index c2502c7842..22b096349d 100644 --- a/src/syscall/zsyscall_darwin_arm64.go +++ b/src/syscall/zsyscall_darwin_arm64.go @@ -7,6 +7,7 @@ package syscall import "unsafe" import "internal/abi" +import "runtime" // THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT @@ -2011,7 +2012,10 @@ func libc_fstatat_trampoline() // THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT //go:nosplit -func ptrace1(request int, pid int, addr uintptr, data uintptr) (err error) { +func ptrace(request int, pid int, addr uintptr, data uintptr) (err error) { + if runtime.GOOS == "ios" { + panic("unimplemented") + } _, _, e1 := syscall6(abi.FuncPCABI0(libc_ptrace_trampoline), uintptr(request), uintptr(pid), uintptr(addr), uintptr(data), 0, 0) if e1 != 0 { err = errnoErr(e1)