2014-06-17 00:03:03 -06: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
|
|
|
|
|
|
|
|
import (
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
func concatstrings(a []string) string {
|
|
|
|
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 21:58:00 -07:00
|
|
|
throw("string concatenation too long")
|
2014-06-17 00:03:03 -06:00
|
|
|
}
|
|
|
|
l += n
|
|
|
|
count++
|
|
|
|
idx = i
|
|
|
|
}
|
|
|
|
if count == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if count == 1 {
|
|
|
|
return a[idx]
|
|
|
|
}
|
|
|
|
s, b := rawstring(l)
|
|
|
|
l = 0
|
|
|
|
for _, x := range a {
|
|
|
|
copy(b[l:], x)
|
|
|
|
l += len(x)
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func concatstring2(a [2]string) string {
|
|
|
|
return concatstrings(a[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
func concatstring3(a [3]string) string {
|
|
|
|
return concatstrings(a[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
func concatstring4(a [4]string) string {
|
|
|
|
return concatstrings(a[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
func concatstring5(a [5]string) string {
|
|
|
|
return concatstrings(a[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
func slicebytetostring(b []byte) string {
|
|
|
|
if raceenabled && len(b) > 0 {
|
|
|
|
racereadrangepc(unsafe.Pointer(&b[0]),
|
2014-09-04 13:53:45 -06:00
|
|
|
uintptr(len(b)),
|
cmd/cc, runtime: preserve C runtime type names in generated Go
uintptr or uint64 in the runtime C were turning into uint in the Go,
bool was turning into uint8, and so on. Fix that.
Also delete Go wrappers for C functions.
The C functions can be called directly now
(but still eventually need to be converted to Go).
LGTM=bradfitz, minux, iant
R=golang-codereviews, bradfitz, iant, minux
CC=golang-codereviews, khr, r
https://golang.org/cl/138740043
2014-08-27 19:59:49 -06:00
|
|
|
getcallerpc(unsafe.Pointer(&b)),
|
2014-09-03 09:10:38 -06:00
|
|
|
funcPC(slicebytetostring))
|
2014-06-17 00:03:03 -06:00
|
|
|
}
|
|
|
|
s, c := rawstring(len(b))
|
|
|
|
copy(c, b)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func slicebytetostringtmp(b []byte) string {
|
|
|
|
// Return a "string" referring to the actual []byte bytes.
|
|
|
|
// This is only for use by internal compiler optimizations
|
|
|
|
// that know that the string form will be discarded before
|
|
|
|
// the calling goroutine could possibly modify the original
|
|
|
|
// slice or synchronize with another goroutine.
|
2015-01-22 07:56:12 -07:00
|
|
|
// First such case is a m[string(k)] lookup where
|
2014-06-17 00:03:03 -06:00
|
|
|
// m is a string-keyed map and k is a []byte.
|
2015-01-22 07:56:12 -07:00
|
|
|
// Second such case is "<"+string(b)+">" concatenation where b is []byte.
|
2015-01-27 13:57:12 -07:00
|
|
|
// Third such case is string(b)=="foo" comparison where b is []byte.
|
2014-06-17 00:03:03 -06:00
|
|
|
|
|
|
|
if raceenabled && len(b) > 0 {
|
|
|
|
racereadrangepc(unsafe.Pointer(&b[0]),
|
2014-09-04 13:53:45 -06:00
|
|
|
uintptr(len(b)),
|
cmd/cc, runtime: preserve C runtime type names in generated Go
uintptr or uint64 in the runtime C were turning into uint in the Go,
bool was turning into uint8, and so on. Fix that.
Also delete Go wrappers for C functions.
The C functions can be called directly now
(but still eventually need to be converted to Go).
LGTM=bradfitz, minux, iant
R=golang-codereviews, bradfitz, iant, minux
CC=golang-codereviews, khr, r
https://golang.org/cl/138740043
2014-08-27 19:59:49 -06:00
|
|
|
getcallerpc(unsafe.Pointer(&b)),
|
2014-09-03 09:10:38 -06:00
|
|
|
funcPC(slicebytetostringtmp))
|
2014-06-17 00:03:03 -06:00
|
|
|
}
|
|
|
|
return *(*string)(unsafe.Pointer(&b))
|
|
|
|
}
|
|
|
|
|
|
|
|
func stringtoslicebyte(s string) []byte {
|
|
|
|
b := rawbyteslice(len(s))
|
|
|
|
copy(b, s)
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func stringtoslicerune(s string) []rune {
|
|
|
|
// two passes.
|
|
|
|
// unlike slicerunetostring, no race because strings are immutable.
|
|
|
|
n := 0
|
|
|
|
t := s
|
|
|
|
for len(s) > 0 {
|
|
|
|
_, k := charntorune(s)
|
|
|
|
s = s[k:]
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
a := rawruneslice(n)
|
|
|
|
n = 0
|
|
|
|
for len(t) > 0 {
|
|
|
|
r, k := charntorune(t)
|
|
|
|
t = t[k:]
|
|
|
|
a[n] = r
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
func slicerunetostring(a []rune) string {
|
|
|
|
if raceenabled && len(a) > 0 {
|
|
|
|
racereadrangepc(unsafe.Pointer(&a[0]),
|
2014-09-04 13:53:45 -06:00
|
|
|
uintptr(len(a))*unsafe.Sizeof(a[0]),
|
cmd/cc, runtime: preserve C runtime type names in generated Go
uintptr or uint64 in the runtime C were turning into uint in the Go,
bool was turning into uint8, and so on. Fix that.
Also delete Go wrappers for C functions.
The C functions can be called directly now
(but still eventually need to be converted to Go).
LGTM=bradfitz, minux, iant
R=golang-codereviews, bradfitz, iant, minux
CC=golang-codereviews, khr, r
https://golang.org/cl/138740043
2014-08-27 19:59:49 -06:00
|
|
|
getcallerpc(unsafe.Pointer(&a)),
|
2014-09-03 09:10:38 -06:00
|
|
|
funcPC(slicerunetostring))
|
2014-06-17 00:03:03 -06:00
|
|
|
}
|
|
|
|
var dum [4]byte
|
|
|
|
size1 := 0
|
|
|
|
for _, r := range a {
|
|
|
|
size1 += runetochar(dum[:], r)
|
|
|
|
}
|
|
|
|
s, b := rawstring(size1 + 3)
|
|
|
|
size2 := 0
|
|
|
|
for _, r := range a {
|
|
|
|
// check for race
|
|
|
|
if size2 >= size1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
size2 += runetochar(b[size2:], r)
|
|
|
|
}
|
|
|
|
return s[:size2]
|
|
|
|
}
|
|
|
|
|
2014-06-17 22:59:50 -06:00
|
|
|
type stringStruct struct {
|
2014-07-16 15:16:19 -06:00
|
|
|
str unsafe.Pointer
|
2014-06-17 22:59:50 -06:00
|
|
|
len int
|
|
|
|
}
|
|
|
|
|
2014-06-17 00:03:03 -06:00
|
|
|
func intstring(v int64) string {
|
|
|
|
s, b := rawstring(4)
|
|
|
|
n := runetochar(b, rune(v))
|
|
|
|
return s[:n]
|
|
|
|
}
|
|
|
|
|
|
|
|
// stringiter returns the index of the next
|
|
|
|
// rune after the rune that starts at s[k].
|
|
|
|
func stringiter(s string, k int) int {
|
|
|
|
if k >= len(s) {
|
|
|
|
// 0 is end of iteration
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
c := s[k]
|
|
|
|
if c < runeself {
|
|
|
|
return k + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// multi-char rune
|
|
|
|
_, n := charntorune(s[k:])
|
|
|
|
return k + n
|
|
|
|
}
|
|
|
|
|
|
|
|
// stringiter2 returns the rune that starts at s[k]
|
|
|
|
// and the index where the next rune starts.
|
|
|
|
func stringiter2(s string, k int) (int, rune) {
|
|
|
|
if k >= len(s) {
|
|
|
|
// 0 is end of iteration
|
|
|
|
return 0, 0
|
|
|
|
}
|
|
|
|
|
|
|
|
c := s[k]
|
|
|
|
if c < runeself {
|
|
|
|
return k + 1, rune(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// multi-char rune
|
|
|
|
r, n := charntorune(s[k:])
|
|
|
|
return k + n, r
|
|
|
|
}
|
2014-07-30 10:01:52 -06: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) {
|
2014-09-08 23:08:34 -06:00
|
|
|
p := mallocgc(uintptr(size), nil, flagNoScan|flagNoZero)
|
2014-07-30 10:01:52 -06:00
|
|
|
|
|
|
|
(*stringStruct)(unsafe.Pointer(&s)).str = p
|
|
|
|
(*stringStruct)(unsafe.Pointer(&s)).len = size
|
|
|
|
|
|
|
|
(*slice)(unsafe.Pointer(&b)).array = (*uint8)(p)
|
|
|
|
(*slice)(unsafe.Pointer(&b)).len = uint(size)
|
|
|
|
(*slice)(unsafe.Pointer(&b)).cap = uint(size)
|
|
|
|
|
|
|
|
for {
|
|
|
|
ms := maxstring
|
cmd/cc, runtime: preserve C runtime type names in generated Go
uintptr or uint64 in the runtime C were turning into uint in the Go,
bool was turning into uint8, and so on. Fix that.
Also delete Go wrappers for C functions.
The C functions can be called directly now
(but still eventually need to be converted to Go).
LGTM=bradfitz, minux, iant
R=golang-codereviews, bradfitz, iant, minux
CC=golang-codereviews, khr, r
https://golang.org/cl/138740043
2014-08-27 19:59:49 -06:00
|
|
|
if uintptr(size) <= uintptr(ms) || casuintptr((*uintptr)(unsafe.Pointer(&maxstring)), uintptr(ms), uintptr(size)) {
|
2014-07-30 10:01:52 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// rawbyteslice allocates a new byte slice. The byte slice is not zeroed.
|
|
|
|
func rawbyteslice(size int) (b []byte) {
|
2014-12-29 00:16:32 -07:00
|
|
|
cap := roundupsize(uintptr(size))
|
2014-09-08 23:08:34 -06:00
|
|
|
p := mallocgc(cap, nil, flagNoScan|flagNoZero)
|
2014-07-30 10:01:52 -06:00
|
|
|
if cap != uintptr(size) {
|
|
|
|
memclr(add(p, uintptr(size)), cap-uintptr(size))
|
|
|
|
}
|
|
|
|
|
|
|
|
(*slice)(unsafe.Pointer(&b)).array = (*uint8)(p)
|
|
|
|
(*slice)(unsafe.Pointer(&b)).len = uint(size)
|
|
|
|
(*slice)(unsafe.Pointer(&b)).cap = uint(cap)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// rawruneslice allocates a new rune slice. The rune slice is not zeroed.
|
|
|
|
func rawruneslice(size int) (b []rune) {
|
2014-11-11 15:05:02 -07:00
|
|
|
if uintptr(size) > _MaxMem/4 {
|
2014-12-27 21:58:00 -07:00
|
|
|
throw("out of memory")
|
2014-07-30 10:01:52 -06:00
|
|
|
}
|
2014-12-29 00:16:32 -07:00
|
|
|
mem := roundupsize(uintptr(size) * 4)
|
2014-09-08 23:08:34 -06:00
|
|
|
p := mallocgc(mem, nil, flagNoScan|flagNoZero)
|
2014-07-30 10:01:52 -06:00
|
|
|
if mem != uintptr(size)*4 {
|
|
|
|
memclr(add(p, uintptr(size)*4), mem-uintptr(size)*4)
|
|
|
|
}
|
|
|
|
|
|
|
|
(*slice)(unsafe.Pointer(&b)).array = (*uint8)(p)
|
|
|
|
(*slice)(unsafe.Pointer(&b)).len = uint(size)
|
|
|
|
(*slice)(unsafe.Pointer(&b)).cap = uint(mem / 4)
|
|
|
|
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 12:05:23 -06:00
|
|
|
|
|
|
|
// used by cmd/cgo
|
|
|
|
func gobytes(p *byte, n int) []byte {
|
|
|
|
if n == 0 {
|
|
|
|
return make([]byte, 0)
|
|
|
|
}
|
|
|
|
x := make([]byte, n)
|
|
|
|
memmove(unsafe.Pointer(&x[0]), unsafe.Pointer(p), uintptr(n))
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
|
|
|
func gostringsize(n int) string {
|
|
|
|
s, _ := rawstring(n)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func index(s, t string) int {
|
|
|
|
if len(t) == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
for i := 0; i < len(s); i++ {
|
|
|
|
if s[i] == t[0] && hasprefix(s[i:], t) {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
func contains(s, t string) bool {
|
|
|
|
return index(s, t) >= 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func hasprefix(s, t string) bool {
|
|
|
|
return len(s) >= len(t) && s[:len(t)] == t
|
|
|
|
}
|
2014-11-11 15:05:02 -07:00
|
|
|
|
2014-12-29 00:16:32 -07:00
|
|
|
func atoi(s string) int {
|
2014-11-11 15:05:02 -07:00
|
|
|
n := 0
|
|
|
|
for len(s) > 0 && '0' <= s[0] && s[0] <= '9' {
|
|
|
|
n = n*10 + int(s[0]) - '0'
|
|
|
|
s = s[1:]
|
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|