diff mbox

[libgo] Account for 32-bit fds_bits on Solaris 2

Message ID yddei5njo16.fsf@manam.CeBiTec.Uni-Bielefeld.DE
State New
Headers show

Commit Message

Rainer Orth March 31, 2011, 3:41 p.m. UTC
While debugging why several libgo tests on Solaris 2/SPARC were hanging
in select (cf. PR go/48242, go/48243), I found that fd_set is

typedef struct fd_set {
        long    fds_bits[__howmany(FD_SETSIZE, FD_NFDBITS)];
} fd_set;

The current implementation of the FD* funcs in sysfile_posix.go assumes
64-bit fds_bits.  While this doesn't make a difference on little-endian
hosts, on big-endian SPARC the wrong bits are set, leading to the
observed hang since there is no activity on those random fds.

The following patch fixes this by moving the FD* implementation to two
new sysfile_fdset{32, 64}.go files to fix this.  There's almost
certainly a cleaner/shorter implemenation, but this worked for me.
While the affected tests don't hang anymore now, they still don't finish
successfully, among others due to PR go/48222.

	Rainer


2011-03-24  Rainer Orth  <ro@CeBiTec.Uni-Bielefeld.DE>

	* syscalls/sysfile_posix.go (FdSet_t, FDSet, FDClr, FDIsSet)
	(FDZero): Move ...
	* syscalls/sysfile_fdset64.go: ... here.
	New file.
	* syscalls/sysfile_fdset32.go: New file.
	* Makefile.am (syscall_fdset_file): Use them.
	(go_syscall_files): Add $(syscall_fdset_file).
	* Makefile.in: Regenerate.

Comments

Ian Lance Taylor March 31, 2011, 4:03 p.m. UTC | #1
Rainer Orth <ro@CeBiTec.Uni-Bielefeld.DE> writes:

> While debugging why several libgo tests on Solaris 2/SPARC were hanging
> in select (cf. PR go/48242, go/48243), I found that fd_set is
>
> typedef struct fd_set {
>         long    fds_bits[__howmany(FD_SETSIZE, FD_NFDBITS)];
> } fd_set;
>
> The current implementation of the FD* funcs in sysfile_posix.go assumes
> 64-bit fds_bits.  While this doesn't make a difference on little-endian
> hosts, on big-endian SPARC the wrong bits are set, leading to the
> observed hang since there is no activity on those random fds.
>
> The following patch fixes this by moving the FD* implementation to two
> new sysfile_fdset{32, 64}.go files to fix this.  There's almost
> certainly a cleaner/shorter implemenation, but this worked for me.
> While the affected tests don't hang anymore now, they still don't finish
> successfully, among others due to PR go/48222.

What an annoying problem.  Sorry about that.  But why don't we just
change to byte?

Ian
Rainer Orth March 31, 2011, 4:06 p.m. UTC | #2
Ian Lance Taylor <iant@google.com> writes:

> What an annoying problem.  Sorry about that.  But why don't we just
> change to byte?

If this causes the right bits to be set for big and little-endian hosts,
certainly fine with me.  I haven't tried yet, but the less different
code paths, the better.

	Rainer
Richard Henderson March 31, 2011, 8:02 p.m. UTC | #3
On 03/31/2011 09:03 AM, Ian Lance Taylor wrote:
> What an annoying problem.  Sorry about that.  But why don't we just
> change to byte?

That would work for little-endian, but not big-endian, at least not
without even worse ugliness in two different files.


r~
Ian Lance Taylor March 31, 2011, 8:09 p.m. UTC | #4
Richard Henderson <rth@redhat.com> writes:

> On 03/31/2011 09:03 AM, Ian Lance Taylor wrote:
>> What an annoying problem.  Sorry about that.  But why don't we just
>> change to byte?
>
> That would work for little-endian, but not big-endian, at least not
> without even worse ugliness in two different files.

Yeah, I figured that out.  Different systems use different sizes for the
bitfield.  Ugh.

Ian
diff mbox

Patch

diff -r e65d996c30c7 libgo/Makefile.am
--- a/libgo/Makefile.am	Thu Mar 24 17:24:32 2011 +0100
+++ b/libgo/Makefile.am	Thu Mar 24 20:25:57 2011 +0100
@@ -1214,6 +1214,20 @@ 
 endif # !LIBGO_IS_SOLARIS
 endif # !LIBGO_IS_LINUX
 
+# Define for struct fd_set.
+if LIBGO_IS_SOLARIS
+if LIBGO_IS_386
+syscall_fdset_file = syscalls/sysfile_fdset32.go
+else # !LIBGO_IS_386
+if LIBGO_IS_SPARC
+syscall_fdset_file = syscalls/sysfile_fdset32.go
+else # !LIBGO_IS_386 && !LIBGO_IS_SPARC
+syscall_fdset_file = syscalls/sysfile_fdset64.go
+endif # !LIBGO_IS_386 && !LIBGO_IS_SPARC
+endif # !LIBGO_IS_386 && !LIBGO_IS_SPARC
+else # !LIBGO_IS_SOLARIS
+syscall_fdset_file = syscalls/sysfile_fdset64.go
+endif # !LIBGO_IS_SOLARIS
 
 # Define ForkExec, PtraceForkExec, Exec, and Waitpid.
 if LIBGO_IS_RTEMS
@@ -1307,6 +1321,7 @@ 
 	syscalls/syscall_$(GOOS).go \
 	$(GO_SYSCALLS_SYSCALL_OS_ARCH_FILE) \
 	syscalls/sysfile_posix.go \
+	$(syscall_fdset_file) \
 	sysinfo.go \
 	syscall_arch.go
 go_syscall_c_files = \
diff -r e65d996c30c7 libgo/syscalls/sysfile_fdset32.go
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libgo/syscalls/sysfile_fdset32.go	Thu Mar 24 20:25:57 2011 +0100
@@ -0,0 +1,35 @@ 
+// sysfile_fdset.go -- POSIX fd_set handling for 32-bit fds_bits.
+
+// Copyright 2010, 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.
+
+// Support for struct fd_set.
+
+package syscall
+
+type FdSet_t struct {
+	Fds_bits [(FD_SETSIZE + 31) / 32]int32;
+}
+
+func FDSet(fd int, set *FdSet_t) {
+	set.Fds_bits[fd / 32] |= (1 << (uint)(fd % 32))
+}
+
+func FDClr(fd int, set *FdSet_t) {
+	set.Fds_bits[fd / 32] &= ^(1 << (uint)(fd % 32))
+}
+
+func FDIsSet(fd int, set *FdSet_t) bool {
+	if set.Fds_bits[fd / 32] & (1 << (uint)(fd % 32)) != 0 {
+		return true
+	} else {
+		return false
+	}
+}
+
+func FDZero(set *FdSet_t) {
+	for i := 0; i < ((FD_SETSIZE + 63) / 32); i++ {
+		set.Fds_bits[i] = 0
+	}
+}
diff -r e65d996c30c7 libgo/syscalls/sysfile_fdset64.go
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libgo/syscalls/sysfile_fdset64.go	Thu Mar 24 20:25:57 2011 +0100
@@ -0,0 +1,35 @@ 
+// sysfile_fdset.go -- POSIX fd_set handling for 64-bit fds_bits.
+
+// Copyright 2010, 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.
+
+// Support for struct fd_set.
+
+package syscall
+
+type FdSet_t struct {
+	Fds_bits [(FD_SETSIZE + 63) / 64]int64;
+}
+
+func FDSet(fd int, set *FdSet_t) {
+	set.Fds_bits[fd / 64] |= (1 << (uint)(fd % 64))
+}
+
+func FDClr(fd int, set *FdSet_t) {
+	set.Fds_bits[fd / 64] &= ^(1 << (uint)(fd % 64))
+}
+
+func FDIsSet(fd int, set *FdSet_t) bool {
+	if set.Fds_bits[fd / 64] & (1 << (uint)(fd % 64)) != 0 {
+		return true
+	} else {
+		return false
+	}
+}
+
+func FDZero(set *FdSet_t) {
+	for i := 0; i < ((FD_SETSIZE + 63) / 64); i++ {
+		set.Fds_bits[i] = 0
+	}
+}
diff -r e65d996c30c7 libgo/syscalls/sysfile_posix.go
--- a/libgo/syscalls/sysfile_posix.go	Thu Mar 24 17:24:32 2011 +0100
+++ b/libgo/syscalls/sysfile_posix.go	Thu Mar 24 20:25:57 2011 +0100
@@ -181,32 +181,6 @@ 
   return;
 }
 
-type FdSet_t struct {
-	Fds_bits [(FD_SETSIZE + 63) / 64]int64;
-}
-
-func FDSet(fd int, set *FdSet_t) {
-	set.Fds_bits[fd / 64] |= (1 << (uint)(fd % 64))
-}
-
-func FDClr(fd int, set *FdSet_t) {
-	set.Fds_bits[fd / 64] &= ^(1 << (uint)(fd % 64))
-}
-
-func FDIsSet(fd int, set *FdSet_t) bool {
-	if set.Fds_bits[fd / 64] & (1 << (uint)(fd % 64)) != 0 {
-		return true
-	} else {
-		return false
-	}
-}
-
-func FDZero(set *FdSet_t) {
-	for i := 0; i < ((FD_SETSIZE + 63) / 64); i++ {
-		set.Fds_bits[i] = 0
-	}
-}
-
 func Select(nfds int, r *FdSet_t, w *FdSet_t, e *FdSet_t, timeout *Timeval) (n int, errno int) {
   n = libc_select(nfds, (*byte)(unsafe.Pointer(r)),
 		  (*byte)(unsafe.Pointer(e)),