2014-06-16 23:03:03 -07:00
|
|
|
// Copyright 2014 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.
|
|
|
|
|
|
|
|
|
|
package runtime
|
|
|
|
|
|
2018-03-01 16:52:27 -06:00
|
|
|
import (
|
2021-05-21 13:37:19 -04:00
|
|
|
"internal/abi"
|
2018-03-01 16:52:27 -06:00
|
|
|
"internal/bytealg"
|
2021-06-17 19:01:08 +00:00
|
|
|
"internal/goarch"
|
2018-03-01 16:52:27 -06:00
|
|
|
"unsafe"
|
|
|
|
|
)
|
2014-06-16 23:03:03 -07:00
|
|
|
|
2015-01-21 17:37:59 +03:00
|
|
|
// The constant is known to the compiler.
|
|
|
|
|
// There is no fundamental theory behind this number.
|
|
|
|
|
const tmpStringBufSize = 32
|
|
|
|
|
|
|
|
|
|
type tmpBuf [tmpStringBufSize]byte
|
|
|
|
|
|
|
|
|
|
// concatstrings implements a Go string concatenation x+y+z+...
|
|
|
|
|
// The operands are passed in the slice a.
|
|
|
|
|
// If buf != nil, the compiler has determined that the result does not
|
|
|
|
|
// escape the calling function, so the string data can be stored in buf
|
|
|
|
|
// if small enough.
|
|
|
|
|
func concatstrings(buf *tmpBuf, a []string) string {
|
2014-06-16 23:03:03 -07:00
|
|
|
idx := 0
|
|
|
|
|
l := 0
|
|
|
|
|
count := 0
|
|
|
|
|
for i, x := range a {
|
|
|
|
|
n := len(x)
|
|
|
|
|
if n == 0 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if l+n < l {
|
2014-12-27 20:58:00 -08:00
|
|
|
throw("string concatenation too long")
|
2014-06-16 23:03:03 -07:00
|
|
|
}
|
|
|
|
|
l += n
|
|
|
|
|
count++
|
|
|
|
|
idx = i
|
|
|
|
|
}
|
|
|
|
|
if count == 0 {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2015-01-21 17:37:59 +03:00
|
|
|
|
|
|
|
|
// If there is just one string and either it is not on the stack
|
|
|
|
|
// or our result does not escape the calling frame (buf != nil),
|
|
|
|
|
// then we can return that string directly.
|
|
|
|
|
if count == 1 && (buf != nil || !stringDataOnStack(a[idx])) {
|
2014-06-16 23:03:03 -07:00
|
|
|
return a[idx]
|
|
|
|
|
}
|
2015-01-21 17:37:59 +03:00
|
|
|
s, b := rawstringtmp(buf, l)
|
2014-06-16 23:03:03 -07:00
|
|
|
for _, x := range a {
|
2016-08-13 18:12:21 -07:00
|
|
|
copy(b, x)
|
|
|
|
|
b = b[len(x):]
|
2014-06-16 23:03:03 -07:00
|
|
|
}
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-02 15:51:45 -04:00
|
|
|
func concatstring2(buf *tmpBuf, a0, a1 string) string {
|
|
|
|
|
return concatstrings(buf, []string{a0, a1})
|
2014-06-16 23:03:03 -07:00
|
|
|
}
|
|
|
|
|
|
2021-04-02 15:51:45 -04:00
|
|
|
func concatstring3(buf *tmpBuf, a0, a1, a2 string) string {
|
|
|
|
|
return concatstrings(buf, []string{a0, a1, a2})
|
2014-06-16 23:03:03 -07:00
|
|
|
}
|
|
|
|
|
|
2021-04-02 15:51:45 -04:00
|
|
|
func concatstring4(buf *tmpBuf, a0, a1, a2, a3 string) string {
|
|
|
|
|
return concatstrings(buf, []string{a0, a1, a2, a3})
|
2014-06-16 23:03:03 -07:00
|
|
|
}
|
|
|
|
|
|
2021-04-02 15:51:45 -04:00
|
|
|
func concatstring5(buf *tmpBuf, a0, a1, a2, a3, a4 string) string {
|
|
|
|
|
return concatstrings(buf, []string{a0, a1, a2, a3, a4})
|
2014-06-16 23:03:03 -07:00
|
|
|
}
|
|
|
|
|
|
2020-01-31 21:01:55 -08:00
|
|
|
// slicebytetostring converts a byte slice to a string.
|
|
|
|
|
// It is inserted by the compiler into generated code.
|
|
|
|
|
// ptr is a pointer to the first element of the slice;
|
|
|
|
|
// n is the length of the slice.
|
2015-01-21 17:37:59 +03:00
|
|
|
// Buf is a fixed-size buffer for the result,
|
|
|
|
|
// it is not nil if the result does not escape.
|
2022-09-07 13:23:10 +07:00
|
|
|
func slicebytetostring(buf *tmpBuf, ptr *byte, n int) string {
|
2020-01-31 21:01:55 -08:00
|
|
|
if n == 0 {
|
2015-01-21 17:37:59 +03:00
|
|
|
// Turns out to be a relatively common case.
|
|
|
|
|
// Consider that you want to parse out data between parens in "foo()bar",
|
|
|
|
|
// you find the indices and convert the subslice to string.
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2017-03-04 16:54:50 -08:00
|
|
|
if raceenabled {
|
2020-01-31 21:01:55 -08:00
|
|
|
racereadrangepc(unsafe.Pointer(ptr),
|
|
|
|
|
uintptr(n),
|
2017-09-22 15:16:26 -04:00
|
|
|
getcallerpc(),
|
2021-05-21 13:37:19 -04:00
|
|
|
abi.FuncPCABIInternal(slicebytetostring))
|
2014-06-16 23:03:03 -07:00
|
|
|
}
|
2017-03-04 16:54:50 -08:00
|
|
|
if msanenabled {
|
2020-01-31 21:01:55 -08:00
|
|
|
msanread(unsafe.Pointer(ptr), uintptr(n))
|
2015-10-21 11:04:42 -07:00
|
|
|
}
|
2021-01-05 17:52:43 +08:00
|
|
|
if asanenabled {
|
|
|
|
|
asanread(unsafe.Pointer(ptr), uintptr(n))
|
|
|
|
|
}
|
2020-01-31 21:01:55 -08:00
|
|
|
if n == 1 {
|
|
|
|
|
p := unsafe.Pointer(&staticuint64s[*ptr])
|
2021-06-17 19:01:08 +00:00
|
|
|
if goarch.BigEndian {
|
2020-03-05 00:28:05 +00:00
|
|
|
p = add(p, 7)
|
|
|
|
|
}
|
2022-09-07 13:23:10 +07:00
|
|
|
return unsafe.String((*byte)(p), 1)
|
2017-03-04 16:55:03 -08:00
|
|
|
}
|
2017-03-04 16:54:50 -08:00
|
|
|
|
|
|
|
|
var p unsafe.Pointer
|
2020-01-31 21:01:55 -08:00
|
|
|
if buf != nil && n <= len(buf) {
|
2017-03-04 16:54:50 -08:00
|
|
|
p = unsafe.Pointer(buf)
|
|
|
|
|
} else {
|
2020-01-31 21:01:55 -08:00
|
|
|
p = mallocgc(uintptr(n), nil, false)
|
2017-03-04 16:54:50 -08:00
|
|
|
}
|
2020-01-31 21:01:55 -08:00
|
|
|
memmove(p, unsafe.Pointer(ptr), uintptr(n))
|
2022-09-07 13:23:10 +07:00
|
|
|
return unsafe.String((*byte)(p), n)
|
2014-06-16 23:03:03 -07:00
|
|
|
}
|
|
|
|
|
|
2015-01-21 17:37:59 +03:00
|
|
|
// stringDataOnStack reports whether the string's data is
|
|
|
|
|
// stored on the current goroutine's stack.
|
|
|
|
|
func stringDataOnStack(s string) bool {
|
2022-09-07 13:23:10 +07:00
|
|
|
ptr := uintptr(unsafe.Pointer(unsafe.StringData(s)))
|
2015-01-21 17:37:59 +03:00
|
|
|
stk := getg().stack
|
|
|
|
|
return stk.lo <= ptr && ptr < stk.hi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func rawstringtmp(buf *tmpBuf, l int) (s string, b []byte) {
|
|
|
|
|
if buf != nil && l <= len(buf) {
|
|
|
|
|
b = buf[:l]
|
2020-01-31 21:01:55 -08:00
|
|
|
s = slicebytetostringtmp(&b[0], len(b))
|
2015-01-21 17:37:59 +03:00
|
|
|
} else {
|
|
|
|
|
s, b = rawstring(l)
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-10 22:44:00 +02:00
|
|
|
// slicebytetostringtmp returns a "string" referring to the actual []byte bytes.
|
|
|
|
|
//
|
|
|
|
|
// Callers need to ensure that the returned string will not be used after
|
|
|
|
|
// the calling goroutine modifies the original slice or synchronizes with
|
|
|
|
|
// another goroutine.
|
|
|
|
|
//
|
|
|
|
|
// The function is only called when instrumenting
|
|
|
|
|
// and otherwise intrinsified by the compiler.
|
|
|
|
|
//
|
|
|
|
|
// Some internal compiler optimizations use this function.
|
2022-02-03 14:12:08 -05:00
|
|
|
// - Used for m[T1{... Tn{..., string(k), ...} ...}] and m[string(k)]
|
|
|
|
|
// where k is []byte, T1 to Tn is a nesting of struct and array literals.
|
|
|
|
|
// - Used for "<"+string(b)+">" concatenation where b is []byte.
|
|
|
|
|
// - Used for string(b)=="foo" comparison where b is []byte.
|
2022-09-07 13:23:10 +07:00
|
|
|
func slicebytetostringtmp(ptr *byte, n int) string {
|
2020-01-31 21:01:55 -08:00
|
|
|
if raceenabled && n > 0 {
|
|
|
|
|
racereadrangepc(unsafe.Pointer(ptr),
|
|
|
|
|
uintptr(n),
|
2017-09-22 15:16:26 -04:00
|
|
|
getcallerpc(),
|
2021-05-21 13:37:19 -04:00
|
|
|
abi.FuncPCABIInternal(slicebytetostringtmp))
|
2014-06-16 23:03:03 -07:00
|
|
|
}
|
2020-01-31 21:01:55 -08:00
|
|
|
if msanenabled && n > 0 {
|
|
|
|
|
msanread(unsafe.Pointer(ptr), uintptr(n))
|
2015-10-21 11:04:42 -07:00
|
|
|
}
|
2021-01-05 17:52:43 +08:00
|
|
|
if asanenabled && n > 0 {
|
|
|
|
|
asanread(unsafe.Pointer(ptr), uintptr(n))
|
|
|
|
|
}
|
2022-09-07 13:23:10 +07:00
|
|
|
return unsafe.String(ptr, n)
|
2014-06-16 23:03:03 -07:00
|
|
|
}
|
|
|
|
|
|
2015-01-30 09:14:13 +03:00
|
|
|
func stringtoslicebyte(buf *tmpBuf, s string) []byte {
|
|
|
|
|
var b []byte
|
|
|
|
|
if buf != nil && len(s) <= len(buf) {
|
2016-04-24 17:04:32 -07:00
|
|
|
*buf = tmpBuf{}
|
|
|
|
|
b = buf[:len(s)]
|
2015-01-30 09:14:13 +03:00
|
|
|
} else {
|
|
|
|
|
b = rawbyteslice(len(s))
|
|
|
|
|
}
|
2014-06-16 23:03:03 -07:00
|
|
|
copy(b, s)
|
|
|
|
|
return b
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-30 09:14:13 +03:00
|
|
|
func stringtoslicerune(buf *[tmpStringBufSize]rune, s string) []rune {
|
2014-06-16 23:03:03 -07:00
|
|
|
// two passes.
|
|
|
|
|
// unlike slicerunetostring, no race because strings are immutable.
|
|
|
|
|
n := 0
|
2016-08-26 15:00:46 +02:00
|
|
|
for range s {
|
2014-06-16 23:03:03 -07:00
|
|
|
n++
|
|
|
|
|
}
|
2016-08-26 15:00:46 +02:00
|
|
|
|
2015-01-30 09:14:13 +03:00
|
|
|
var a []rune
|
|
|
|
|
if buf != nil && n <= len(buf) {
|
2016-04-24 17:04:32 -07:00
|
|
|
*buf = [tmpStringBufSize]rune{}
|
|
|
|
|
a = buf[:n]
|
2015-01-30 09:14:13 +03:00
|
|
|
} else {
|
|
|
|
|
a = rawruneslice(n)
|
|
|
|
|
}
|
2016-08-26 15:00:46 +02:00
|
|
|
|
2014-06-16 23:03:03 -07:00
|
|
|
n = 0
|
2016-08-26 15:00:46 +02:00
|
|
|
for _, r := range s {
|
2014-06-16 23:03:03 -07:00
|
|
|
a[n] = r
|
|
|
|
|
n++
|
|
|
|
|
}
|
|
|
|
|
return a
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-30 09:14:13 +03:00
|
|
|
func slicerunetostring(buf *tmpBuf, a []rune) string {
|
2014-06-16 23:03:03 -07:00
|
|
|
if raceenabled && len(a) > 0 {
|
|
|
|
|
racereadrangepc(unsafe.Pointer(&a[0]),
|
2014-09-04 15:53:45 -04:00
|
|
|
uintptr(len(a))*unsafe.Sizeof(a[0]),
|
2017-09-22 15:16:26 -04:00
|
|
|
getcallerpc(),
|
2021-05-21 13:37:19 -04:00
|
|
|
abi.FuncPCABIInternal(slicerunetostring))
|
2014-06-16 23:03:03 -07:00
|
|
|
}
|
2015-10-21 11:04:42 -07:00
|
|
|
if msanenabled && len(a) > 0 {
|
|
|
|
|
msanread(unsafe.Pointer(&a[0]), uintptr(len(a))*unsafe.Sizeof(a[0]))
|
|
|
|
|
}
|
2021-01-05 17:52:43 +08:00
|
|
|
if asanenabled && len(a) > 0 {
|
|
|
|
|
asanread(unsafe.Pointer(&a[0]), uintptr(len(a))*unsafe.Sizeof(a[0]))
|
|
|
|
|
}
|
2014-06-16 23:03:03 -07:00
|
|
|
var dum [4]byte
|
|
|
|
|
size1 := 0
|
|
|
|
|
for _, r := range a {
|
2016-09-02 17:04:41 +02:00
|
|
|
size1 += encoderune(dum[:], r)
|
2014-06-16 23:03:03 -07:00
|
|
|
}
|
2015-01-30 09:14:13 +03:00
|
|
|
s, b := rawstringtmp(buf, size1+3)
|
2014-06-16 23:03:03 -07:00
|
|
|
size2 := 0
|
|
|
|
|
for _, r := range a {
|
|
|
|
|
// check for race
|
|
|
|
|
if size2 >= size1 {
|
|
|
|
|
break
|
|
|
|
|
}
|
2016-09-02 17:04:41 +02:00
|
|
|
size2 += encoderune(b[size2:], r)
|
2014-06-16 23:03:03 -07:00
|
|
|
}
|
|
|
|
|
return s[:size2]
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-17 21:59:50 -07:00
|
|
|
type stringStruct struct {
|
2014-07-16 14:16:19 -07:00
|
|
|
str unsafe.Pointer
|
2014-06-17 21:59:50 -07:00
|
|
|
len int
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-20 00:35:12 -07:00
|
|
|
// Variant with *byte pointer type for DWARF debugging.
|
|
|
|
|
type stringStructDWARF struct {
|
|
|
|
|
str *byte
|
|
|
|
|
len int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func stringStructOf(sp *string) *stringStruct {
|
|
|
|
|
return (*stringStruct)(unsafe.Pointer(sp))
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-30 23:05:41 -07:00
|
|
|
func intstring(buf *[4]byte, v int64) (s string) {
|
2015-01-28 08:42:20 +03:00
|
|
|
var b []byte
|
|
|
|
|
if buf != nil {
|
|
|
|
|
b = buf[:]
|
2020-01-31 21:01:55 -08:00
|
|
|
s = slicebytetostringtmp(&b[0], len(b))
|
2015-01-28 08:42:20 +03:00
|
|
|
} else {
|
|
|
|
|
s, b = rawstring(4)
|
|
|
|
|
}
|
2016-03-31 02:04:12 -07:00
|
|
|
if int64(rune(v)) != v {
|
2016-09-02 17:04:41 +02:00
|
|
|
v = runeError
|
2016-03-31 02:04:12 -07:00
|
|
|
}
|
2016-09-02 17:04:41 +02:00
|
|
|
n := encoderune(b, rune(v))
|
2014-06-16 23:03:03 -07:00
|
|
|
return s[:n]
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-30 09:01:52 -07:00
|
|
|
// rawstring allocates storage for a new string. The returned
|
|
|
|
|
// string and byte slice both refer to the same storage.
|
|
|
|
|
// The storage is not zeroed. Callers should use
|
|
|
|
|
// b to set the string contents and then drop b.
|
|
|
|
|
func rawstring(size int) (s string, b []byte) {
|
2016-04-19 19:35:10 -07:00
|
|
|
p := mallocgc(uintptr(size), nil, false)
|
2022-09-07 13:23:10 +07:00
|
|
|
return unsafe.String((*byte)(p), size), unsafe.Slice((*byte)(p), size)
|
2014-07-30 09:01:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// rawbyteslice allocates a new byte slice. The byte slice is not zeroed.
|
|
|
|
|
func rawbyteslice(size int) (b []byte) {
|
2014-12-28 23:16:32 -08:00
|
|
|
cap := roundupsize(uintptr(size))
|
2016-04-19 19:35:10 -07:00
|
|
|
p := mallocgc(cap, nil, false)
|
2014-07-30 09:01:52 -07:00
|
|
|
if cap != uintptr(size) {
|
2016-10-17 18:41:56 -04:00
|
|
|
memclrNoHeapPointers(add(p, uintptr(size)), cap-uintptr(size))
|
2014-07-30 09:01:52 -07:00
|
|
|
}
|
|
|
|
|
|
2015-04-11 10:01:54 +12:00
|
|
|
*(*slice)(unsafe.Pointer(&b)) = slice{p, size, int(cap)}
|
2014-07-30 09:01:52 -07:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// rawruneslice allocates a new rune slice. The rune slice is not zeroed.
|
|
|
|
|
func rawruneslice(size int) (b []rune) {
|
2018-01-01 21:51:47 -05:00
|
|
|
if uintptr(size) > maxAlloc/4 {
|
2014-12-27 20:58:00 -08:00
|
|
|
throw("out of memory")
|
2014-07-30 09:01:52 -07:00
|
|
|
}
|
2014-12-28 23:16:32 -08:00
|
|
|
mem := roundupsize(uintptr(size) * 4)
|
2016-04-19 19:35:10 -07:00
|
|
|
p := mallocgc(mem, nil, false)
|
2014-07-30 09:01:52 -07:00
|
|
|
if mem != uintptr(size)*4 {
|
2016-10-17 18:41:56 -04:00
|
|
|
memclrNoHeapPointers(add(p, uintptr(size)*4), mem-uintptr(size)*4)
|
2014-07-30 09:01:52 -07:00
|
|
|
}
|
|
|
|
|
|
2015-04-11 10:01:54 +12:00
|
|
|
*(*slice)(unsafe.Pointer(&b)) = slice{p, size, int(mem / 4)}
|
2014-07-30 09:01:52 -07:00
|
|
|
return
|
|
|
|
|
}
|
liblink, runtime: diagnose and fix C code running on Go stack
This CL contains compiler+runtime changes that detect C code
running on Go (not g0, not gsignal) stacks, and it contains
corrections for what it detected.
The detection works by changing the C prologue to use a different
stack guard word in the G than Go prologue does. On the g0 and
gsignal stacks, that stack guard word is set to the usual
stack guard value. But on ordinary Go stacks, that stack
guard word is set to ^0, which will make any stack split
check fail. The C prologue then calls morestackc instead
of morestack, and morestackc aborts the program with
a message about running C code on a Go stack.
This check catches all C code running on the Go stack
except NOSPLIT code. The NOSPLIT code is allowed,
so the check is complete. Since it is a dynamic check,
the code must execute to be caught. But unlike the static
checks we've been using in cmd/ld, the dynamic check
works with function pointers and other indirect calls.
For example it caught sigpanic being pushed onto Go
stacks in the signal handlers.
Fixes #8667.
LGTM=khr, iant
R=golang-codereviews, khr, iant
CC=golang-codereviews, r
https://golang.org/cl/133700043
2014-09-08 14:05:23 -04:00
|
|
|
|
|
|
|
|
// used by cmd/cgo
|
2018-02-18 14:12:52 +01:00
|
|
|
func gobytes(p *byte, n int) (b []byte) {
|
liblink, runtime: diagnose and fix C code running on Go stack
This CL contains compiler+runtime changes that detect C code
running on Go (not g0, not gsignal) stacks, and it contains
corrections for what it detected.
The detection works by changing the C prologue to use a different
stack guard word in the G than Go prologue does. On the g0 and
gsignal stacks, that stack guard word is set to the usual
stack guard value. But on ordinary Go stacks, that stack
guard word is set to ^0, which will make any stack split
check fail. The C prologue then calls morestackc instead
of morestack, and morestackc aborts the program with
a message about running C code on a Go stack.
This check catches all C code running on the Go stack
except NOSPLIT code. The NOSPLIT code is allowed,
so the check is complete. Since it is a dynamic check,
the code must execute to be caught. But unlike the static
checks we've been using in cmd/ld, the dynamic check
works with function pointers and other indirect calls.
For example it caught sigpanic being pushed onto Go
stacks in the signal handlers.
Fixes #8667.
LGTM=khr, iant
R=golang-codereviews, khr, iant
CC=golang-codereviews, r
https://golang.org/cl/133700043
2014-09-08 14:05:23 -04:00
|
|
|
if n == 0 {
|
|
|
|
|
return make([]byte, 0)
|
|
|
|
|
}
|
2018-02-18 14:12:52 +01:00
|
|
|
|
|
|
|
|
if n < 0 || uintptr(n) > maxAlloc {
|
|
|
|
|
panic(errorString("gobytes: length out of range"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bp := mallocgc(uintptr(n), nil, false)
|
|
|
|
|
memmove(bp, unsafe.Pointer(p), uintptr(n))
|
|
|
|
|
|
|
|
|
|
*(*slice)(unsafe.Pointer(&b)) = slice{bp, n, n}
|
|
|
|
|
return
|
liblink, runtime: diagnose and fix C code running on Go stack
This CL contains compiler+runtime changes that detect C code
running on Go (not g0, not gsignal) stacks, and it contains
corrections for what it detected.
The detection works by changing the C prologue to use a different
stack guard word in the G than Go prologue does. On the g0 and
gsignal stacks, that stack guard word is set to the usual
stack guard value. But on ordinary Go stacks, that stack
guard word is set to ^0, which will make any stack split
check fail. The C prologue then calls morestackc instead
of morestack, and morestackc aborts the program with
a message about running C code on a Go stack.
This check catches all C code running on the Go stack
except NOSPLIT code. The NOSPLIT code is allowed,
so the check is complete. Since it is a dynamic check,
the code must execute to be caught. But unlike the static
checks we've been using in cmd/ld, the dynamic check
works with function pointers and other indirect calls.
For example it caught sigpanic being pushed onto Go
stacks in the signal handlers.
Fixes #8667.
LGTM=khr, iant
R=golang-codereviews, khr, iant
CC=golang-codereviews, r
https://golang.org/cl/133700043
2014-09-08 14:05:23 -04:00
|
|
|
}
|
|
|
|
|
|
2019-05-31 16:38:56 -04:00
|
|
|
// This is exported via linkname to assembly in syscall (for Plan9).
|
2022-01-30 20:13:43 -05:00
|
|
|
//
|
2019-05-31 16:38:56 -04:00
|
|
|
//go:linkname gostring
|
liblink, runtime: diagnose and fix C code running on Go stack
This CL contains compiler+runtime changes that detect C code
running on Go (not g0, not gsignal) stacks, and it contains
corrections for what it detected.
The detection works by changing the C prologue to use a different
stack guard word in the G than Go prologue does. On the g0 and
gsignal stacks, that stack guard word is set to the usual
stack guard value. But on ordinary Go stacks, that stack
guard word is set to ^0, which will make any stack split
check fail. The C prologue then calls morestackc instead
of morestack, and morestackc aborts the program with
a message about running C code on a Go stack.
This check catches all C code running on the Go stack
except NOSPLIT code. The NOSPLIT code is allowed,
so the check is complete. Since it is a dynamic check,
the code must execute to be caught. But unlike the static
checks we've been using in cmd/ld, the dynamic check
works with function pointers and other indirect calls.
For example it caught sigpanic being pushed onto Go
stacks in the signal handlers.
Fixes #8667.
LGTM=khr, iant
R=golang-codereviews, khr, iant
CC=golang-codereviews, r
https://golang.org/cl/133700043
2014-09-08 14:05:23 -04:00
|
|
|
func gostring(p *byte) string {
|
|
|
|
|
l := findnull(p)
|
|
|
|
|
if l == 0 {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
s, b := rawstring(l)
|
|
|
|
|
memmove(unsafe.Pointer(&b[0]), unsafe.Pointer(p), uintptr(l))
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-11 12:42:51 -08:00
|
|
|
// internal_syscall_gostring is a version of gostring for internal/syscall/unix.
|
|
|
|
|
//
|
|
|
|
|
//go:linkname internal_syscall_gostring internal/syscall/unix.gostring
|
|
|
|
|
func internal_syscall_gostring(p *byte) string {
|
|
|
|
|
return gostring(p)
|
|
|
|
|
}
|
|
|
|
|
|
liblink, runtime: diagnose and fix C code running on Go stack
This CL contains compiler+runtime changes that detect C code
running on Go (not g0, not gsignal) stacks, and it contains
corrections for what it detected.
The detection works by changing the C prologue to use a different
stack guard word in the G than Go prologue does. On the g0 and
gsignal stacks, that stack guard word is set to the usual
stack guard value. But on ordinary Go stacks, that stack
guard word is set to ^0, which will make any stack split
check fail. The C prologue then calls morestackc instead
of morestack, and morestackc aborts the program with
a message about running C code on a Go stack.
This check catches all C code running on the Go stack
except NOSPLIT code. The NOSPLIT code is allowed,
so the check is complete. Since it is a dynamic check,
the code must execute to be caught. But unlike the static
checks we've been using in cmd/ld, the dynamic check
works with function pointers and other indirect calls.
For example it caught sigpanic being pushed onto Go
stacks in the signal handlers.
Fixes #8667.
LGTM=khr, iant
R=golang-codereviews, khr, iant
CC=golang-codereviews, r
https://golang.org/cl/133700043
2014-09-08 14:05:23 -04:00
|
|
|
func gostringn(p *byte, l int) string {
|
|
|
|
|
if l == 0 {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
s, b := rawstring(l)
|
|
|
|
|
memmove(unsafe.Pointer(&b[0]), unsafe.Pointer(p), uintptr(l))
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-01 19:25:57 +02:00
|
|
|
func hasPrefix(s, prefix string) bool {
|
|
|
|
|
return len(s) >= len(prefix) && s[:len(prefix)] == prefix
|
liblink, runtime: diagnose and fix C code running on Go stack
This CL contains compiler+runtime changes that detect C code
running on Go (not g0, not gsignal) stacks, and it contains
corrections for what it detected.
The detection works by changing the C prologue to use a different
stack guard word in the G than Go prologue does. On the g0 and
gsignal stacks, that stack guard word is set to the usual
stack guard value. But on ordinary Go stacks, that stack
guard word is set to ^0, which will make any stack split
check fail. The C prologue then calls morestackc instead
of morestack, and morestackc aborts the program with
a message about running C code on a Go stack.
This check catches all C code running on the Go stack
except NOSPLIT code. The NOSPLIT code is allowed,
so the check is complete. Since it is a dynamic check,
the code must execute to be caught. But unlike the static
checks we've been using in cmd/ld, the dynamic check
works with function pointers and other indirect calls.
For example it caught sigpanic being pushed onto Go
stacks in the signal handlers.
Fixes #8667.
LGTM=khr, iant
R=golang-codereviews, khr, iant
CC=golang-codereviews, r
https://golang.org/cl/133700043
2014-09-08 14:05:23 -04:00
|
|
|
}
|
2014-11-11 17:05:02 -05:00
|
|
|
|
2023-02-05 15:54:33 -05:00
|
|
|
func hasSuffix(s, suffix string) bool {
|
|
|
|
|
return len(s) >= len(suffix) && s[len(s)-len(suffix):] == suffix
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-30 01:54:19 +02:00
|
|
|
const (
|
2022-02-15 00:22:20 +00:00
|
|
|
maxUint64 = ^uint64(0)
|
|
|
|
|
maxInt64 = int64(maxUint64 >> 1)
|
2016-10-30 01:54:19 +02:00
|
|
|
)
|
|
|
|
|
|
2022-02-15 00:22:20 +00:00
|
|
|
// atoi64 parses an int64 from a string s.
|
2016-10-30 01:54:19 +02:00
|
|
|
// The bool result reports whether s is a number
|
2022-02-15 00:22:20 +00:00
|
|
|
// representable by a value of type int64.
|
|
|
|
|
func atoi64(s string) (int64, bool) {
|
2016-10-30 01:54:19 +02:00
|
|
|
if s == "" {
|
|
|
|
|
return 0, false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
neg := false
|
|
|
|
|
if s[0] == '-' {
|
|
|
|
|
neg = true
|
2014-11-11 17:05:02 -05:00
|
|
|
s = s[1:]
|
|
|
|
|
}
|
2016-10-30 01:54:19 +02:00
|
|
|
|
2022-02-15 00:22:20 +00:00
|
|
|
un := uint64(0)
|
2016-10-30 01:54:19 +02:00
|
|
|
for i := 0; i < len(s); i++ {
|
|
|
|
|
c := s[i]
|
|
|
|
|
if c < '0' || c > '9' {
|
|
|
|
|
return 0, false
|
|
|
|
|
}
|
2022-02-15 00:22:20 +00:00
|
|
|
if un > maxUint64/10 {
|
2016-10-30 01:54:19 +02:00
|
|
|
// overflow
|
|
|
|
|
return 0, false
|
|
|
|
|
}
|
|
|
|
|
un *= 10
|
2022-02-15 00:22:20 +00:00
|
|
|
un1 := un + uint64(c) - '0'
|
2016-10-30 01:54:19 +02:00
|
|
|
if un1 < un {
|
|
|
|
|
// overflow
|
|
|
|
|
return 0, false
|
|
|
|
|
}
|
|
|
|
|
un = un1
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-15 00:22:20 +00:00
|
|
|
if !neg && un > uint64(maxInt64) {
|
2016-10-30 01:54:19 +02:00
|
|
|
return 0, false
|
|
|
|
|
}
|
2022-02-15 00:22:20 +00:00
|
|
|
if neg && un > uint64(maxInt64)+1 {
|
2016-10-30 01:54:19 +02:00
|
|
|
return 0, false
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-15 00:22:20 +00:00
|
|
|
n := int64(un)
|
2016-10-30 01:54:19 +02:00
|
|
|
if neg {
|
|
|
|
|
n = -n
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return n, true
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-15 00:22:20 +00:00
|
|
|
// atoi is like atoi64 but for integers
|
|
|
|
|
// that fit into an int.
|
|
|
|
|
func atoi(s string) (int, bool) {
|
|
|
|
|
if n, ok := atoi64(s); n == int64(int(n)) {
|
|
|
|
|
return int(n), ok
|
|
|
|
|
}
|
|
|
|
|
return 0, false
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-30 01:54:19 +02:00
|
|
|
// atoi32 is like atoi but for integers
|
|
|
|
|
// that fit into an int32.
|
|
|
|
|
func atoi32(s string) (int32, bool) {
|
2022-02-15 00:22:20 +00:00
|
|
|
if n, ok := atoi64(s); n == int64(int32(n)) {
|
2016-10-30 01:54:19 +02:00
|
|
|
return int32(n), ok
|
|
|
|
|
}
|
|
|
|
|
return 0, false
|
2014-11-11 17:05:02 -05:00
|
|
|
}
|
2015-10-15 23:34:56 -07:00
|
|
|
|
2022-02-15 00:22:20 +00:00
|
|
|
// parseByteCount parses a string that represents a count of bytes.
|
|
|
|
|
//
|
|
|
|
|
// s must match the following regular expression:
|
|
|
|
|
//
|
2022-05-18 16:46:20 -04:00
|
|
|
// ^[0-9]+(([KMGT]i)?B)?$
|
2022-02-15 00:22:20 +00:00
|
|
|
//
|
|
|
|
|
// In other words, an integer byte count with an optional unit
|
|
|
|
|
// suffix. Acceptable suffixes include one of
|
|
|
|
|
// - KiB, MiB, GiB, TiB which represent binary IEC/ISO 80000 units, or
|
|
|
|
|
// - B, which just represents bytes.
|
|
|
|
|
//
|
2022-07-24 13:41:16 +00:00
|
|
|
// Returns an int64 because that's what its callers want and receive,
|
2022-02-15 00:22:20 +00:00
|
|
|
// but the result is always non-negative.
|
|
|
|
|
func parseByteCount(s string) (int64, bool) {
|
|
|
|
|
// The empty string is not valid.
|
|
|
|
|
if s == "" {
|
|
|
|
|
return 0, false
|
|
|
|
|
}
|
|
|
|
|
// Handle the easy non-suffix case.
|
|
|
|
|
last := s[len(s)-1]
|
|
|
|
|
if last >= '0' && last <= '9' {
|
|
|
|
|
n, ok := atoi64(s)
|
|
|
|
|
if !ok || n < 0 {
|
|
|
|
|
return 0, false
|
|
|
|
|
}
|
|
|
|
|
return n, ok
|
|
|
|
|
}
|
|
|
|
|
// Failing a trailing digit, this must always end in 'B'.
|
|
|
|
|
// Also at this point there must be at least one digit before
|
|
|
|
|
// that B.
|
|
|
|
|
if last != 'B' || len(s) < 2 {
|
|
|
|
|
return 0, false
|
|
|
|
|
}
|
|
|
|
|
// The one before that must always be a digit or 'i'.
|
|
|
|
|
if c := s[len(s)-2]; c >= '0' && c <= '9' {
|
|
|
|
|
// Trivial 'B' suffix.
|
|
|
|
|
n, ok := atoi64(s[:len(s)-1])
|
|
|
|
|
if !ok || n < 0 {
|
|
|
|
|
return 0, false
|
|
|
|
|
}
|
|
|
|
|
return n, ok
|
|
|
|
|
} else if c != 'i' {
|
|
|
|
|
return 0, false
|
|
|
|
|
}
|
|
|
|
|
// Finally, we need at least 4 characters now, for the unit
|
|
|
|
|
// prefix and at least one digit.
|
|
|
|
|
if len(s) < 4 {
|
|
|
|
|
return 0, false
|
|
|
|
|
}
|
|
|
|
|
power := 0
|
|
|
|
|
switch s[len(s)-3] {
|
|
|
|
|
case 'K':
|
|
|
|
|
power = 1
|
|
|
|
|
case 'M':
|
|
|
|
|
power = 2
|
|
|
|
|
case 'G':
|
|
|
|
|
power = 3
|
|
|
|
|
case 'T':
|
|
|
|
|
power = 4
|
|
|
|
|
default:
|
|
|
|
|
// Invalid suffix.
|
|
|
|
|
return 0, false
|
|
|
|
|
}
|
|
|
|
|
m := uint64(1)
|
|
|
|
|
for i := 0; i < power; i++ {
|
|
|
|
|
m *= 1024
|
|
|
|
|
}
|
|
|
|
|
n, ok := atoi64(s[:len(s)-3])
|
|
|
|
|
if !ok || n < 0 {
|
|
|
|
|
return 0, false
|
|
|
|
|
}
|
|
|
|
|
un := uint64(n)
|
|
|
|
|
if un > maxUint64/m {
|
|
|
|
|
// Overflow.
|
|
|
|
|
return 0, false
|
|
|
|
|
}
|
|
|
|
|
un *= m
|
|
|
|
|
if un > uint64(maxInt64) {
|
|
|
|
|
// Overflow.
|
|
|
|
|
return 0, false
|
|
|
|
|
}
|
|
|
|
|
return int64(un), true
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-15 23:34:56 -07:00
|
|
|
//go:nosplit
|
|
|
|
|
func findnull(s *byte) int {
|
|
|
|
|
if s == nil {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
2018-03-01 16:52:27 -06:00
|
|
|
|
2018-03-14 13:36:31 +01:00
|
|
|
// Avoid IndexByteString on Plan 9 because it uses SSE instructions
|
|
|
|
|
// on x86 machines, and those are classified as floating point instructions,
|
|
|
|
|
// which are illegal in a note handler.
|
|
|
|
|
if GOOS == "plan9" {
|
|
|
|
|
p := (*[maxAlloc/2 - 1]byte)(unsafe.Pointer(s))
|
|
|
|
|
l := 0
|
|
|
|
|
for p[l] != 0 {
|
|
|
|
|
l++
|
|
|
|
|
}
|
|
|
|
|
return l
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-01 16:52:27 -06:00
|
|
|
// pageSize is the unit we scan at a time looking for NULL.
|
|
|
|
|
// It must be the minimum page size for any architecture Go
|
|
|
|
|
// runs on. It's okay (just a minor performance loss) if the
|
|
|
|
|
// actual system page size is larger than this value.
|
|
|
|
|
const pageSize = 4096
|
|
|
|
|
|
|
|
|
|
offset := 0
|
|
|
|
|
ptr := unsafe.Pointer(s)
|
|
|
|
|
// IndexByteString uses wide reads, so we need to be careful
|
|
|
|
|
// with page boundaries. Call IndexByteString on
|
|
|
|
|
// [ptr, endOfPage) interval.
|
|
|
|
|
safeLen := int(pageSize - uintptr(ptr)%pageSize)
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
t := *(*string)(unsafe.Pointer(&stringStruct{ptr, safeLen}))
|
|
|
|
|
// Check one page at a time.
|
|
|
|
|
if i := bytealg.IndexByteString(t, 0); i != -1 {
|
|
|
|
|
return offset + i
|
|
|
|
|
}
|
|
|
|
|
// Move to next page
|
|
|
|
|
ptr = unsafe.Pointer(uintptr(ptr) + uintptr(safeLen))
|
|
|
|
|
offset += safeLen
|
|
|
|
|
safeLen = pageSize
|
2018-03-01 22:22:44 +00:00
|
|
|
}
|
2015-10-15 23:34:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func findnullw(s *uint16) int {
|
|
|
|
|
if s == nil {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
2018-01-01 21:51:47 -05:00
|
|
|
p := (*[maxAlloc/2/2 - 1]uint16)(unsafe.Pointer(s))
|
2015-10-15 23:34:56 -07:00
|
|
|
l := 0
|
|
|
|
|
for p[l] != 0 {
|
|
|
|
|
l++
|
|
|
|
|
}
|
|
|
|
|
return l
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//go:nosplit
|
|
|
|
|
func gostringnocopy(str *byte) string {
|
|
|
|
|
ss := stringStruct{str: unsafe.Pointer(str), len: findnull(str)}
|
|
|
|
|
s := *(*string)(unsafe.Pointer(&ss))
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func gostringw(strw *uint16) string {
|
|
|
|
|
var buf [8]byte
|
2018-01-01 21:51:47 -05:00
|
|
|
str := (*[maxAlloc/2/2 - 1]uint16)(unsafe.Pointer(strw))
|
2015-10-15 23:34:56 -07:00
|
|
|
n1 := 0
|
|
|
|
|
for i := 0; str[i] != 0; i++ {
|
2016-09-02 17:04:41 +02:00
|
|
|
n1 += encoderune(buf[:], rune(str[i]))
|
2015-10-15 23:34:56 -07:00
|
|
|
}
|
|
|
|
|
s, b := rawstring(n1 + 4)
|
|
|
|
|
n2 := 0
|
|
|
|
|
for i := 0; str[i] != 0; i++ {
|
|
|
|
|
// check for race
|
|
|
|
|
if n2 >= n1 {
|
|
|
|
|
break
|
|
|
|
|
}
|
2016-09-02 17:04:41 +02:00
|
|
|
n2 += encoderune(b[n2:], rune(str[i]))
|
2015-10-15 23:34:56 -07:00
|
|
|
}
|
|
|
|
|
b[n2] = 0 // for luck
|
|
|
|
|
return s[:n2]
|
|
|
|
|
}
|