From 4fab09e515eaead568256aedf733594bfd9188ce Mon Sep 17 00:00:00 2001 From: Andrea Barisani Date: Thu, 15 Feb 2024 18:22:23 +0100 Subject: [PATCH] align GOOS=tamago support go go1.22.0 --- src/net/dnsclient_tamago.go | 76 ++++++++++++++++++++------------ src/net/lookup_tamago.go | 19 ++++---- src/net/sockopt_fake.go | 2 +- src/net/sockopt_tamago.go | 37 ++++++++++++++++ src/os/exec/lp_tamago.go | 6 +++ src/os/file_open_tamago.go | 17 ------- src/os/file_open_unix.go | 2 +- src/os/stat_js.go | 2 +- src/runtime/lock_tamago.go | 4 ++ src/runtime/os_tamago_arm.go | 7 ++- src/runtime/os_tamago_riscv64.go | 7 ++- src/runtime/stubs_nontamago.go | 1 - 12 files changed, 118 insertions(+), 62 deletions(-) create mode 100644 src/net/sockopt_tamago.go delete mode 100644 src/os/file_open_tamago.go diff --git a/src/net/dnsclient_tamago.go b/src/net/dnsclient_tamago.go index a3ff003b297f54..3f9f0ad1374089 100644 --- a/src/net/dnsclient_tamago.go +++ b/src/net/dnsclient_tamago.go @@ -15,6 +15,7 @@ package net import ( "context" "errors" + "internal/bytealg" "io" "time" @@ -198,7 +199,9 @@ func (r *Resolver) exchange(ctx context.Context, server string, q dnsmessage.Que // checkHeader performs basic sanity checks on the header. func checkHeader(p *dnsmessage.Parser, h dnsmessage.Header) error { - if h.RCode == dnsmessage.RCodeNameError { + rcode := extractExtendedRCode(*p, h) + + if rcode == dnsmessage.RCodeNameError { return errNoSuchHost } @@ -209,17 +212,17 @@ func checkHeader(p *dnsmessage.Parser, h dnsmessage.Header) error { // libresolv continues to the next server when it receives // an invalid referral response. See golang.org/issue/15434. - if h.RCode == dnsmessage.RCodeSuccess && !h.Authoritative && !h.RecursionAvailable && err == dnsmessage.ErrSectionDone { + if rcode == dnsmessage.RCodeSuccess && !h.Authoritative && !h.RecursionAvailable && err == dnsmessage.ErrSectionDone { return errLameReferral } - if h.RCode != dnsmessage.RCodeSuccess && h.RCode != dnsmessage.RCodeNameError { + if rcode != dnsmessage.RCodeSuccess && rcode != dnsmessage.RCodeNameError { // None of the error codes make sense // for the query we sent. If we didn't get // a name error and we didn't get success, // the server is behaving incorrectly or // having temporary trouble. - if h.RCode == dnsmessage.RCodeServerFailure { + if rcode == dnsmessage.RCodeServerFailure { return errServerTemporarilyMisbehaving } return errServerMisbehaving @@ -246,6 +249,23 @@ func skipToAnswer(p *dnsmessage.Parser, qtype dnsmessage.Type) error { } } +// extractExtendedRCode extracts the extended RCode from the OPT resource (EDNS(0)) +// If an OPT record is not found, the RCode from the hdr is returned. +func extractExtendedRCode(p dnsmessage.Parser, hdr dnsmessage.Header) dnsmessage.RCode { + p.SkipAllAnswers() + p.SkipAllAuthorities() + for { + ahdr, err := p.AdditionalHeader() + if err != nil { + return hdr.RCode + } + if ahdr.Type == dnsmessage.TypeOPT { + return ahdr.ExtendedRCode(hdr.RCode) + } + p.SkipAdditional() + } +} + // Do a lookup for a single name, which must be rooted // (otherwise answer will not find the answers). func (r *Resolver) tryOneName(ctx context.Context, cfg *dnsConfig, name string, qtype dnsmessage.Type) (dnsmessage.Parser, string, error) { @@ -385,10 +405,6 @@ func avoidDNS(name string) bool { // nameList returns a list of names for sequential DNS queries. func (conf *dnsConfig) nameList(name string) []string { - if avoidDNS(name) { - return nil - } - // Check name length (see isDomainName). l := len(name) rooted := l > 0 && name[l-1] == '.' @@ -398,27 +414,31 @@ func (conf *dnsConfig) nameList(name string) []string { // If name is rooted (trailing dot), try only that name. if rooted { + if avoidDNS(name) { + return nil + } return []string{name} } - hasNdots := count(name, '.') >= conf.ndots + hasNdots := bytealg.CountString(name, '.') >= conf.ndots name += "." l++ // Build list of search choices. names := make([]string, 0, 1+len(conf.search)) // If name has enough dots, try unsuffixed first. - if hasNdots { + if hasNdots && !avoidDNS(name) { names = append(names, name) } // Try suffixes that are not too long (see isDomainName). for _, suffix := range conf.search { - if l+len(suffix) <= 254 { - names = append(names, name+suffix) + fqdn := name + suffix + if !avoidDNS(fqdn) && len(fqdn) <= 254 { + names = append(names, fqdn) } } // Try unsuffixed, if not tried first above. - if !hasNdots { + if !hasNdots && !avoidDNS(name) { names = append(names, name) } return names @@ -520,7 +540,7 @@ func (r *Resolver) goLookupIPCNAME(ctx context.Context, network, name string, co h, err := result.p.AnswerHeader() if err != nil && err != dnsmessage.ErrSectionDone { lastErr = &DNSError{ - Err: "cannot marshal DNS message", + Err: errCannotUnmarshalDNSMessage.Error(), Name: name, Server: result.server, } @@ -533,7 +553,7 @@ func (r *Resolver) goLookupIPCNAME(ctx context.Context, network, name string, co a, err := result.p.AResource() if err != nil { lastErr = &DNSError{ - Err: "cannot marshal DNS message", + Err: errCannotUnmarshalDNSMessage.Error(), Name: name, Server: result.server, } @@ -548,7 +568,7 @@ func (r *Resolver) goLookupIPCNAME(ctx context.Context, network, name string, co aaaa, err := result.p.AAAAResource() if err != nil { lastErr = &DNSError{ - Err: "cannot marshal DNS message", + Err: errCannotUnmarshalDNSMessage.Error(), Name: name, Server: result.server, } @@ -563,7 +583,7 @@ func (r *Resolver) goLookupIPCNAME(ctx context.Context, network, name string, co c, err := result.p.CNAMEResource() if err != nil { lastErr = &DNSError{ - Err: "cannot marshal DNS message", + Err: errCannotUnmarshalDNSMessage.Error(), Name: name, Server: result.server, } @@ -576,7 +596,7 @@ func (r *Resolver) goLookupIPCNAME(ctx context.Context, network, name string, co default: if err := result.p.SkipAnswer(); err != nil { lastErr = &DNSError{ - Err: "cannot marshal DNS message", + Err: errCannotUnmarshalDNSMessage.Error(), Name: name, Server: result.server, } @@ -612,16 +632,14 @@ func (r *Resolver) goLookupIPCNAME(ctx context.Context, network, name string, co return addrs, cname, nil } +// goLookupCNAME is the native Go (non-cgo) implementation of LookupCNAME. +func (r *Resolver) goLookupCNAME(ctx context.Context, host string, conf *dnsConfig) (string, error) { + _, cname, err := r.goLookupIPCNAME(ctx, "CNAME", host, conf) + return cname.String(), err +} + // goLookupPTR is the native Go implementation of LookupAddr. -// Used only if cgoLookupPTR refuses to handle the request (that is, -// only if cgoLookupPTR is the stub in cgo_stub.go). -// Normally we let cgo use the C library resolver instead of depending -// on our lookup code, so that Go and C get the same answers. func (r *Resolver) goLookupPTR(ctx context.Context, addr string, conf *dnsConfig) ([]string, error) { - names := lookupStaticAddr(addr) - if len(names) > 0 { - return names, nil - } arpa, err := reverseaddr(addr) if err != nil { return nil, err @@ -638,7 +656,7 @@ func (r *Resolver) goLookupPTR(ctx context.Context, addr string, conf *dnsConfig } if err != nil { return nil, &DNSError{ - Err: "cannot marshal DNS message", + Err: errCannotUnmarshalDNSMessage.Error(), Name: addr, Server: server, } @@ -647,7 +665,7 @@ func (r *Resolver) goLookupPTR(ctx context.Context, addr string, conf *dnsConfig err := p.SkipAnswer() if err != nil { return nil, &DNSError{ - Err: "cannot marshal DNS message", + Err: errCannotUnmarshalDNSMessage.Error(), Name: addr, Server: server, } @@ -657,7 +675,7 @@ func (r *Resolver) goLookupPTR(ctx context.Context, addr string, conf *dnsConfig ptr, err := p.PTRResource() if err != nil { return nil, &DNSError{ - Err: "cannot marshal DNS message", + Err: errCannotUnmarshalDNSMessage.Error(), Name: addr, Server: server, } diff --git a/src/net/lookup_tamago.go b/src/net/lookup_tamago.go index af8472d957040e..cd9d6dee0df4a5 100644 --- a/src/net/lookup_tamago.go +++ b/src/net/lookup_tamago.go @@ -24,37 +24,36 @@ func (r *Resolver) lookupHost(ctx context.Context, host string) (addrs []string, return } -func (r *Resolver) lookupIP(ctx context.Context, network, name string) (addrs []IPAddr, err error) { - ips, _, err := r.goLookupIPCNAME(ctx, network, name, getSystemDNSConfig()) +func (r *Resolver) lookupIP(ctx context.Context, network, host string) (addrs []IPAddr, err error) { + ips, _, err := r.goLookupIPCNAME(ctx, network, host, getSystemDNSConfig()) return ips, err } -func (*Resolver) lookupPort(ctx context.Context, network, service string) (port int, err error) { +func (r *Resolver) lookupPort(ctx context.Context, network, service string) (int, error) { return goLookupPort(network, service) } func (r *Resolver) lookupCNAME(ctx context.Context, name string) (string, error) { - _, cname, err := r.goLookupIPCNAME(ctx, "CNAME", name, getSystemDNSConfig()) - return cname.String(), err + return r.goLookupCNAME(ctx, name, getSystemDNSConfig()) } -func (r *Resolver) lookupSRV(ctx context.Context, service, proto, name string) (cname string, srvs []*SRV, err error) { +func (r *Resolver) lookupSRV(ctx context.Context, service, proto, name string) (string, []*SRV, error) { return r.goLookupSRV(ctx, service, proto, name) } -func (r *Resolver) lookupMX(ctx context.Context, name string) (mxs []*MX, err error) { +func (r *Resolver) lookupMX(ctx context.Context, name string) ([]*MX, error) { return r.goLookupMX(ctx, name) } -func (r *Resolver) lookupNS(ctx context.Context, name string) (nss []*NS, err error) { +func (r *Resolver) lookupNS(ctx context.Context, name string) ([]*NS, error) { return r.goLookupNS(ctx, name) } -func (r *Resolver) lookupTXT(ctx context.Context, name string) (txts []string, err error) { +func (r *Resolver) lookupTXT(ctx context.Context, name string) ([]string, error) { return r.goLookupTXT(ctx, name) } -func (r *Resolver) lookupAddr(ctx context.Context, addr string) (ptrs []string, err error) { +func (r *Resolver) lookupAddr(ctx context.Context, addr string) ([]string, error) { return r.goLookupPTR(ctx, addr, getSystemDNSConfig()) } diff --git a/src/net/sockopt_fake.go b/src/net/sockopt_fake.go index 5d5fe0eabc0827..9d9f7ea951cc48 100644 --- a/src/net/sockopt_fake.go +++ b/src/net/sockopt_fake.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -//go:build js || wasip1 || tamago +//go:build js || wasip1 package net diff --git a/src/net/sockopt_tamago.go b/src/net/sockopt_tamago.go new file mode 100644 index 00000000000000..f7cc44213ce91d --- /dev/null +++ b/src/net/sockopt_tamago.go @@ -0,0 +1,37 @@ +// Copyright 2011 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 tamago + +package net + +import "syscall" + +func setDefaultSockopts(s, family, sotype int, ipv6only bool) error { + return nil +} + +func setDefaultListenerSockopts(s int) error { + return nil +} + +func setDefaultMulticastSockopts(s int) error { + return nil +} + +func setReadBuffer(fd *netFD, bytes int) error { + return syscall.ENOPROTOOPT +} + +func setWriteBuffer(fd *netFD, bytes int) error { + return syscall.ENOPROTOOPT +} + +func setKeepAlive(fd *netFD, keepalive bool) error { + return syscall.ENOPROTOOPT +} + +func setLinger(fd *netFD, sec int) error { + return syscall.ENOPROTOOPT +} diff --git a/src/os/exec/lp_tamago.go b/src/os/exec/lp_tamago.go index 1e6ad9c6aef423..1d52ab91aaee26 100644 --- a/src/os/exec/lp_tamago.go +++ b/src/os/exec/lp_tamago.go @@ -21,3 +21,9 @@ func LookPath(file string) (string, error) { // tamago can not execute processes, so act as if there are no executables at all. return "", &Error{file, ErrNotFound} } + +// lookExtensions is a no-op on non-Windows platforms, since +// they do not restrict executables to specific extensions. +func lookExtensions(path, dir string) (string, error) { + return path, nil +} diff --git a/src/os/file_open_tamago.go b/src/os/file_open_tamago.go deleted file mode 100644 index c0c4f8a0229a69..00000000000000 --- a/src/os/file_open_tamago.go +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright 2023 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 tamago - -package os - -import ( - "internal/poll" - "syscall" -) - -func open(path string, flag int, perm uint32) (int, poll.SysFile, error) { - fd, err := syscall.Open(path, flag, perm) - return fd, poll.SysFile{}, err -} diff --git a/src/os/file_open_unix.go b/src/os/file_open_unix.go index a3336eac81a5d1..d1cbea848e3ade 100644 --- a/src/os/file_open_unix.go +++ b/src/os/file_open_unix.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -//go:build unix || (js && wasm) +//go:build unix || (js && wasm) || tamago package os diff --git a/src/os/stat_js.go b/src/os/stat_js.go index 456fc8ba28b4f7..a137172e66de6f 100644 --- a/src/os/stat_js.go +++ b/src/os/stat_js.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -//go:build (js && wasm) +//go:build js && wasm package os diff --git a/src/runtime/lock_tamago.go b/src/runtime/lock_tamago.go index ad6a2025f3de9e..f7a5553f3f217e 100644 --- a/src/runtime/lock_tamago.go +++ b/src/runtime/lock_tamago.go @@ -23,6 +23,10 @@ const ( passive_spin = 1 ) +func mutexContended(l *mutex) bool { + return false +} + func lock(l *mutex) { lockWithRank(l, getLockRank(l)) } diff --git a/src/runtime/os_tamago_arm.go b/src/runtime/os_tamago_arm.go index 01a03bff397a9d..e238a437bb8424 100644 --- a/src/runtime/os_tamago_arm.go +++ b/src/runtime/os_tamago_arm.go @@ -110,6 +110,12 @@ func osinit() { initBloc() } +func readRandom(r []byte) int { + initRNG() + getRandomData(r) + return len(r) +} + func signame(sig uint32) string { return "" } @@ -123,7 +129,6 @@ func checkgoarm() { //go:nosplit func cputicks() int64 { - // Currently cputicks() is used in blocking profiler and to seed runtime·fastrand(). // runtime·nanotime() is a poor approximation of CPU ticks that is enough for the profiler. return nanotime() } diff --git a/src/runtime/os_tamago_riscv64.go b/src/runtime/os_tamago_riscv64.go index bc4ee8a62b98ed..96ac52154e51d7 100644 --- a/src/runtime/os_tamago_riscv64.go +++ b/src/runtime/os_tamago_riscv64.go @@ -92,13 +92,18 @@ func osinit() { initBloc() } +func readRandom(r []byte) int { + initRNG() + getRandomData(r) + return len(r) +} + func signame(sig uint32) string { return "" } //go:nosplit func cputicks() int64 { - // Currently cputicks() is used in blocking profiler and to seed runtime·fastrand(). // runtime·nanotime() is a poor approximation of CPU ticks that is enough for the profiler. return nanotime() } diff --git a/src/runtime/stubs_nontamago.go b/src/runtime/stubs_nontamago.go index 151029e3c504d9..02a8ebcc9d0a77 100644 --- a/src/runtime/stubs_nontamago.go +++ b/src/runtime/stubs_nontamago.go @@ -7,4 +7,3 @@ package runtime var ramSize uint32 -func initRNG() {}