2010-07-14 18:17:53 -06:00
|
|
|
// Copyright 2010 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.
|
|
|
|
|
runtime: scheduler, cgo reorganization
* Change use of m->g0 stack (aka scheduler stack).
* Provide runtime.mcall(f) to invoke f() on m->g0 stack.
* Replace scheduler loop entry with runtime.mcall(schedule).
Runtime.mcall eliminates the need for fake scheduler states that
exist just to run a bit of code on the m->g0 stack
(Grecovery, Gstackalloc).
The elimination of the scheduler as a loop that stops and
starts using gosave and gogo fixes a bad interaction with the
way cgo uses the m->g0 stack. Cgo runs external (gcc-compiled)
C functions on that stack, and then when calling back into Go,
it sets m->g0->sched.sp below the added call frames, so that
other uses of m->g0's stack will not interfere with those frames.
Unfortunately, gogo (longjmp) back to the scheduler loop at
this point would end up running scheduler with the lower
sp, which no longer points at a valid stack frame for
a call to scheduler. If scheduler then wrote any function call
arguments or local variables to where it expected the stack
frame to be, it would overwrite other data on the stack.
I realized this possibility while debugging a problem with
calling complex Go code in a Go -> C -> Go cgo callback.
This wasn't the bug I was looking for, it turns out, but I believe
it is a real bug nonetheless. Switching to runtime.mcall, which
only adds new frames to the stack and never jumps into
functions running in existing ones, fixes this bug.
* Move cgo-related code out of proc.c into cgocall.c.
* Add very large comment describing cgo call sequences.
* Simpilify, regularize cgo function implementations and names.
* Add test suite as misc/cgo/test.
Now the Go -> C path calls cgocall, which calls asmcgocall,
and the C -> Go path calls cgocallback, which calls cgocallbackg.
The shuffling, which affects mainly the callback case, moves
most of the callback implementation to cgocallback running
on the m->curg stack (not the m->g0 scheduler stack) and
only while accounted for with $GOMAXPROCS (between calls
to exitsyscall and entersyscall).
The previous callback code did not block in startcgocallback's
approximation to exitsyscall, so if, say, the garbage collector
were running, it would still barge in and start doing things
like call malloc. Similarly endcgocallback's approximation of
entersyscall did not call matchmg to kick off new OS threads
when necessary, which caused the bug in issue 1560.
Fixes #1560.
R=iant
CC=golang-dev
https://golang.org/cl/4253054
2011-03-07 08:37:42 -07:00
|
|
|
// Basic test cases for cgo.
|
2010-07-14 18:17:53 -06:00
|
|
|
|
runtime: scheduler, cgo reorganization
* Change use of m->g0 stack (aka scheduler stack).
* Provide runtime.mcall(f) to invoke f() on m->g0 stack.
* Replace scheduler loop entry with runtime.mcall(schedule).
Runtime.mcall eliminates the need for fake scheduler states that
exist just to run a bit of code on the m->g0 stack
(Grecovery, Gstackalloc).
The elimination of the scheduler as a loop that stops and
starts using gosave and gogo fixes a bad interaction with the
way cgo uses the m->g0 stack. Cgo runs external (gcc-compiled)
C functions on that stack, and then when calling back into Go,
it sets m->g0->sched.sp below the added call frames, so that
other uses of m->g0's stack will not interfere with those frames.
Unfortunately, gogo (longjmp) back to the scheduler loop at
this point would end up running scheduler with the lower
sp, which no longer points at a valid stack frame for
a call to scheduler. If scheduler then wrote any function call
arguments or local variables to where it expected the stack
frame to be, it would overwrite other data on the stack.
I realized this possibility while debugging a problem with
calling complex Go code in a Go -> C -> Go cgo callback.
This wasn't the bug I was looking for, it turns out, but I believe
it is a real bug nonetheless. Switching to runtime.mcall, which
only adds new frames to the stack and never jumps into
functions running in existing ones, fixes this bug.
* Move cgo-related code out of proc.c into cgocall.c.
* Add very large comment describing cgo call sequences.
* Simpilify, regularize cgo function implementations and names.
* Add test suite as misc/cgo/test.
Now the Go -> C path calls cgocall, which calls asmcgocall,
and the C -> Go path calls cgocallback, which calls cgocallbackg.
The shuffling, which affects mainly the callback case, moves
most of the callback implementation to cgocallback running
on the m->curg stack (not the m->g0 scheduler stack) and
only while accounted for with $GOMAXPROCS (between calls
to exitsyscall and entersyscall).
The previous callback code did not block in startcgocallback's
approximation to exitsyscall, so if, say, the garbage collector
were running, it would still barge in and start doing things
like call malloc. Similarly endcgocallback's approximation of
entersyscall did not call matchmg to kick off new OS threads
when necessary, which caused the bug in issue 1560.
Fixes #1560.
R=iant
CC=golang-dev
https://golang.org/cl/4253054
2011-03-07 08:37:42 -07:00
|
|
|
package cgotest
|
2010-07-14 18:17:53 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#define SHIFT(x, y) ((x)<<(y))
|
|
|
|
#define KILO SHIFT(1, 10)
|
2012-09-19 21:20:33 -06:00
|
|
|
#define UINT32VAL 0xc008427bU
|
2010-07-14 18:17:53 -06:00
|
|
|
|
2010-12-13 11:20:04 -07:00
|
|
|
enum E {
|
2010-07-14 18:17:53 -06:00
|
|
|
Enum1 = 1,
|
|
|
|
Enum2 = 2,
|
|
|
|
};
|
2010-09-21 20:41:19 -06:00
|
|
|
|
2012-07-31 05:59:06 -06:00
|
|
|
typedef unsigned char cgo_uuid_t[20];
|
2010-09-21 20:41:19 -06:00
|
|
|
|
2012-07-31 05:59:06 -06:00
|
|
|
void uuid_generate(cgo_uuid_t x) {
|
2010-09-21 20:41:19 -06:00
|
|
|
x[0] = 0;
|
|
|
|
}
|
|
|
|
|
2010-12-13 11:20:04 -07:00
|
|
|
struct S {
|
|
|
|
int x;
|
|
|
|
};
|
|
|
|
|
|
|
|
extern enum E myConstFunc(struct S* const ctx, int const id, struct S **const filter);
|
|
|
|
|
|
|
|
enum E myConstFunc(struct S *const ctx, int const id, struct S **const filter) { return 0; }
|
|
|
|
|
|
|
|
// issue 1222
|
|
|
|
typedef union {
|
|
|
|
long align;
|
|
|
|
} xxpthread_mutex_t;
|
|
|
|
|
|
|
|
struct ibv_async_event {
|
|
|
|
union {
|
|
|
|
int x;
|
|
|
|
} element;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ibv_context {
|
|
|
|
xxpthread_mutex_t mutex;
|
|
|
|
};
|
2011-08-18 10:17:09 -06:00
|
|
|
|
|
|
|
int add(int x, int y) {
|
|
|
|
return x+y;
|
|
|
|
};
|
2010-07-14 18:17:53 -06:00
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
import (
|
2012-12-20 07:43:19 -07:00
|
|
|
"runtime"
|
2012-03-06 21:27:30 -07:00
|
|
|
"syscall"
|
runtime: scheduler, cgo reorganization
* Change use of m->g0 stack (aka scheduler stack).
* Provide runtime.mcall(f) to invoke f() on m->g0 stack.
* Replace scheduler loop entry with runtime.mcall(schedule).
Runtime.mcall eliminates the need for fake scheduler states that
exist just to run a bit of code on the m->g0 stack
(Grecovery, Gstackalloc).
The elimination of the scheduler as a loop that stops and
starts using gosave and gogo fixes a bad interaction with the
way cgo uses the m->g0 stack. Cgo runs external (gcc-compiled)
C functions on that stack, and then when calling back into Go,
it sets m->g0->sched.sp below the added call frames, so that
other uses of m->g0's stack will not interfere with those frames.
Unfortunately, gogo (longjmp) back to the scheduler loop at
this point would end up running scheduler with the lower
sp, which no longer points at a valid stack frame for
a call to scheduler. If scheduler then wrote any function call
arguments or local variables to where it expected the stack
frame to be, it would overwrite other data on the stack.
I realized this possibility while debugging a problem with
calling complex Go code in a Go -> C -> Go cgo callback.
This wasn't the bug I was looking for, it turns out, but I believe
it is a real bug nonetheless. Switching to runtime.mcall, which
only adds new frames to the stack and never jumps into
functions running in existing ones, fixes this bug.
* Move cgo-related code out of proc.c into cgocall.c.
* Add very large comment describing cgo call sequences.
* Simpilify, regularize cgo function implementations and names.
* Add test suite as misc/cgo/test.
Now the Go -> C path calls cgocall, which calls asmcgocall,
and the C -> Go path calls cgocallback, which calls cgocallbackg.
The shuffling, which affects mainly the callback case, moves
most of the callback implementation to cgocallback running
on the m->curg stack (not the m->g0 scheduler stack) and
only while accounted for with $GOMAXPROCS (between calls
to exitsyscall and entersyscall).
The previous callback code did not block in startcgocallback's
approximation to exitsyscall, so if, say, the garbage collector
were running, it would still barge in and start doing things
like call malloc. Similarly endcgocallback's approximation of
entersyscall did not call matchmg to kick off new OS threads
when necessary, which caused the bug in issue 1560.
Fixes #1560.
R=iant
CC=golang-dev
https://golang.org/cl/4253054
2011-03-07 08:37:42 -07:00
|
|
|
"testing"
|
2010-07-14 18:17:53 -06:00
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
const EINVAL = C.EINVAL /* test #define */
|
|
|
|
|
|
|
|
var KILO = C.KILO
|
|
|
|
|
2010-09-21 20:41:19 -06:00
|
|
|
func uuidgen() {
|
2012-07-31 05:59:06 -06:00
|
|
|
var uuid C.cgo_uuid_t
|
2010-09-21 20:41:19 -06:00
|
|
|
C.uuid_generate(&uuid[0])
|
|
|
|
}
|
|
|
|
|
2011-11-01 20:06:05 -06:00
|
|
|
func Strtol(s string, base int) (int, error) {
|
2010-07-14 18:17:53 -06:00
|
|
|
p := C.CString(s)
|
|
|
|
n, err := C.strtol(p, nil, C.int(base))
|
|
|
|
C.free(unsafe.Pointer(p))
|
|
|
|
return int(n), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func Atol(s string) int {
|
|
|
|
p := C.CString(s)
|
|
|
|
n := C.atol(p)
|
|
|
|
C.free(unsafe.Pointer(p))
|
|
|
|
return int(n)
|
|
|
|
}
|
|
|
|
|
2011-05-02 11:55:51 -06:00
|
|
|
func testConst(t *testing.T) {
|
2010-12-13 11:20:04 -07:00
|
|
|
C.myConstFunc(nil, 0, nil)
|
|
|
|
}
|
|
|
|
|
2011-05-02 11:55:51 -06:00
|
|
|
func testEnum(t *testing.T) {
|
2010-07-14 18:17:53 -06:00
|
|
|
if C.Enum1 != 1 || C.Enum2 != 2 {
|
runtime: scheduler, cgo reorganization
* Change use of m->g0 stack (aka scheduler stack).
* Provide runtime.mcall(f) to invoke f() on m->g0 stack.
* Replace scheduler loop entry with runtime.mcall(schedule).
Runtime.mcall eliminates the need for fake scheduler states that
exist just to run a bit of code on the m->g0 stack
(Grecovery, Gstackalloc).
The elimination of the scheduler as a loop that stops and
starts using gosave and gogo fixes a bad interaction with the
way cgo uses the m->g0 stack. Cgo runs external (gcc-compiled)
C functions on that stack, and then when calling back into Go,
it sets m->g0->sched.sp below the added call frames, so that
other uses of m->g0's stack will not interfere with those frames.
Unfortunately, gogo (longjmp) back to the scheduler loop at
this point would end up running scheduler with the lower
sp, which no longer points at a valid stack frame for
a call to scheduler. If scheduler then wrote any function call
arguments or local variables to where it expected the stack
frame to be, it would overwrite other data on the stack.
I realized this possibility while debugging a problem with
calling complex Go code in a Go -> C -> Go cgo callback.
This wasn't the bug I was looking for, it turns out, but I believe
it is a real bug nonetheless. Switching to runtime.mcall, which
only adds new frames to the stack and never jumps into
functions running in existing ones, fixes this bug.
* Move cgo-related code out of proc.c into cgocall.c.
* Add very large comment describing cgo call sequences.
* Simpilify, regularize cgo function implementations and names.
* Add test suite as misc/cgo/test.
Now the Go -> C path calls cgocall, which calls asmcgocall,
and the C -> Go path calls cgocallback, which calls cgocallbackg.
The shuffling, which affects mainly the callback case, moves
most of the callback implementation to cgocallback running
on the m->curg stack (not the m->g0 scheduler stack) and
only while accounted for with $GOMAXPROCS (between calls
to exitsyscall and entersyscall).
The previous callback code did not block in startcgocallback's
approximation to exitsyscall, so if, say, the garbage collector
were running, it would still barge in and start doing things
like call malloc. Similarly endcgocallback's approximation of
entersyscall did not call matchmg to kick off new OS threads
when necessary, which caused the bug in issue 1560.
Fixes #1560.
R=iant
CC=golang-dev
https://golang.org/cl/4253054
2011-03-07 08:37:42 -07:00
|
|
|
t.Error("bad enum", C.Enum1, C.Enum2)
|
2010-07-14 18:17:53 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-02 11:55:51 -06:00
|
|
|
func testAtol(t *testing.T) {
|
2010-07-14 18:17:53 -06:00
|
|
|
l := Atol("123")
|
|
|
|
if l != 123 {
|
runtime: scheduler, cgo reorganization
* Change use of m->g0 stack (aka scheduler stack).
* Provide runtime.mcall(f) to invoke f() on m->g0 stack.
* Replace scheduler loop entry with runtime.mcall(schedule).
Runtime.mcall eliminates the need for fake scheduler states that
exist just to run a bit of code on the m->g0 stack
(Grecovery, Gstackalloc).
The elimination of the scheduler as a loop that stops and
starts using gosave and gogo fixes a bad interaction with the
way cgo uses the m->g0 stack. Cgo runs external (gcc-compiled)
C functions on that stack, and then when calling back into Go,
it sets m->g0->sched.sp below the added call frames, so that
other uses of m->g0's stack will not interfere with those frames.
Unfortunately, gogo (longjmp) back to the scheduler loop at
this point would end up running scheduler with the lower
sp, which no longer points at a valid stack frame for
a call to scheduler. If scheduler then wrote any function call
arguments or local variables to where it expected the stack
frame to be, it would overwrite other data on the stack.
I realized this possibility while debugging a problem with
calling complex Go code in a Go -> C -> Go cgo callback.
This wasn't the bug I was looking for, it turns out, but I believe
it is a real bug nonetheless. Switching to runtime.mcall, which
only adds new frames to the stack and never jumps into
functions running in existing ones, fixes this bug.
* Move cgo-related code out of proc.c into cgocall.c.
* Add very large comment describing cgo call sequences.
* Simpilify, regularize cgo function implementations and names.
* Add test suite as misc/cgo/test.
Now the Go -> C path calls cgocall, which calls asmcgocall,
and the C -> Go path calls cgocallback, which calls cgocallbackg.
The shuffling, which affects mainly the callback case, moves
most of the callback implementation to cgocallback running
on the m->curg stack (not the m->g0 scheduler stack) and
only while accounted for with $GOMAXPROCS (between calls
to exitsyscall and entersyscall).
The previous callback code did not block in startcgocallback's
approximation to exitsyscall, so if, say, the garbage collector
were running, it would still barge in and start doing things
like call malloc. Similarly endcgocallback's approximation of
entersyscall did not call matchmg to kick off new OS threads
when necessary, which caused the bug in issue 1560.
Fixes #1560.
R=iant
CC=golang-dev
https://golang.org/cl/4253054
2011-03-07 08:37:42 -07:00
|
|
|
t.Error("Atol 123: ", l)
|
2010-07-14 18:17:53 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-02 11:55:51 -06:00
|
|
|
func testErrno(t *testing.T) {
|
2012-01-19 18:59:44 -07:00
|
|
|
p := C.CString("no-such-file")
|
|
|
|
m := C.CString("r")
|
|
|
|
f, err := C.fopen(p, m)
|
|
|
|
C.free(unsafe.Pointer(p))
|
|
|
|
C.free(unsafe.Pointer(m))
|
|
|
|
if err == nil {
|
|
|
|
C.fclose(f)
|
|
|
|
t.Fatalf("C.fopen: should fail")
|
|
|
|
}
|
2012-03-06 21:27:30 -07:00
|
|
|
if err != syscall.ENOENT {
|
2012-02-15 23:21:21 -07:00
|
|
|
t.Fatalf("C.fopen: unexpected error: %v", err)
|
2010-07-14 18:17:53 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-02 11:55:51 -06:00
|
|
|
func testMultipleAssign(t *testing.T) {
|
runtime: scheduler, cgo reorganization
* Change use of m->g0 stack (aka scheduler stack).
* Provide runtime.mcall(f) to invoke f() on m->g0 stack.
* Replace scheduler loop entry with runtime.mcall(schedule).
Runtime.mcall eliminates the need for fake scheduler states that
exist just to run a bit of code on the m->g0 stack
(Grecovery, Gstackalloc).
The elimination of the scheduler as a loop that stops and
starts using gosave and gogo fixes a bad interaction with the
way cgo uses the m->g0 stack. Cgo runs external (gcc-compiled)
C functions on that stack, and then when calling back into Go,
it sets m->g0->sched.sp below the added call frames, so that
other uses of m->g0's stack will not interfere with those frames.
Unfortunately, gogo (longjmp) back to the scheduler loop at
this point would end up running scheduler with the lower
sp, which no longer points at a valid stack frame for
a call to scheduler. If scheduler then wrote any function call
arguments or local variables to where it expected the stack
frame to be, it would overwrite other data on the stack.
I realized this possibility while debugging a problem with
calling complex Go code in a Go -> C -> Go cgo callback.
This wasn't the bug I was looking for, it turns out, but I believe
it is a real bug nonetheless. Switching to runtime.mcall, which
only adds new frames to the stack and never jumps into
functions running in existing ones, fixes this bug.
* Move cgo-related code out of proc.c into cgocall.c.
* Add very large comment describing cgo call sequences.
* Simpilify, regularize cgo function implementations and names.
* Add test suite as misc/cgo/test.
Now the Go -> C path calls cgocall, which calls asmcgocall,
and the C -> Go path calls cgocallback, which calls cgocallbackg.
The shuffling, which affects mainly the callback case, moves
most of the callback implementation to cgocallback running
on the m->curg stack (not the m->g0 scheduler stack) and
only while accounted for with $GOMAXPROCS (between calls
to exitsyscall and entersyscall).
The previous callback code did not block in startcgocallback's
approximation to exitsyscall, so if, say, the garbage collector
were running, it would still barge in and start doing things
like call malloc. Similarly endcgocallback's approximation of
entersyscall did not call matchmg to kick off new OS threads
when necessary, which caused the bug in issue 1560.
Fixes #1560.
R=iant
CC=golang-dev
https://golang.org/cl/4253054
2011-03-07 08:37:42 -07:00
|
|
|
p := C.CString("234")
|
2010-09-21 20:41:19 -06:00
|
|
|
n, m := C.strtol(p, nil, 345), C.strtol(p, nil, 10)
|
2012-12-20 07:43:19 -07:00
|
|
|
if runtime.GOOS == "openbsd" {
|
|
|
|
// Bug in OpenBSD strtol(3) - base > 36 succeeds.
|
|
|
|
if (n != 0 && n != 239089) || m != 234 {
|
|
|
|
t.Fatal("Strtol x2: ", n, m)
|
|
|
|
}
|
|
|
|
} else if n != 0 || m != 234 {
|
runtime: scheduler, cgo reorganization
* Change use of m->g0 stack (aka scheduler stack).
* Provide runtime.mcall(f) to invoke f() on m->g0 stack.
* Replace scheduler loop entry with runtime.mcall(schedule).
Runtime.mcall eliminates the need for fake scheduler states that
exist just to run a bit of code on the m->g0 stack
(Grecovery, Gstackalloc).
The elimination of the scheduler as a loop that stops and
starts using gosave and gogo fixes a bad interaction with the
way cgo uses the m->g0 stack. Cgo runs external (gcc-compiled)
C functions on that stack, and then when calling back into Go,
it sets m->g0->sched.sp below the added call frames, so that
other uses of m->g0's stack will not interfere with those frames.
Unfortunately, gogo (longjmp) back to the scheduler loop at
this point would end up running scheduler with the lower
sp, which no longer points at a valid stack frame for
a call to scheduler. If scheduler then wrote any function call
arguments or local variables to where it expected the stack
frame to be, it would overwrite other data on the stack.
I realized this possibility while debugging a problem with
calling complex Go code in a Go -> C -> Go cgo callback.
This wasn't the bug I was looking for, it turns out, but I believe
it is a real bug nonetheless. Switching to runtime.mcall, which
only adds new frames to the stack and never jumps into
functions running in existing ones, fixes this bug.
* Move cgo-related code out of proc.c into cgocall.c.
* Add very large comment describing cgo call sequences.
* Simpilify, regularize cgo function implementations and names.
* Add test suite as misc/cgo/test.
Now the Go -> C path calls cgocall, which calls asmcgocall,
and the C -> Go path calls cgocallback, which calls cgocallbackg.
The shuffling, which affects mainly the callback case, moves
most of the callback implementation to cgocallback running
on the m->curg stack (not the m->g0 scheduler stack) and
only while accounted for with $GOMAXPROCS (between calls
to exitsyscall and entersyscall).
The previous callback code did not block in startcgocallback's
approximation to exitsyscall, so if, say, the garbage collector
were running, it would still barge in and start doing things
like call malloc. Similarly endcgocallback's approximation of
entersyscall did not call matchmg to kick off new OS threads
when necessary, which caused the bug in issue 1560.
Fixes #1560.
R=iant
CC=golang-dev
https://golang.org/cl/4253054
2011-03-07 08:37:42 -07:00
|
|
|
t.Fatal("Strtol x2: ", n, m)
|
2010-09-21 20:41:19 -06:00
|
|
|
}
|
|
|
|
C.free(unsafe.Pointer(p))
|
|
|
|
}
|
|
|
|
|
2010-07-14 18:17:53 -06:00
|
|
|
var (
|
2011-12-20 10:28:45 -07:00
|
|
|
cuint = (C.uint)(0)
|
|
|
|
culong C.ulong
|
|
|
|
cchar C.char
|
2010-07-14 18:17:53 -06:00
|
|
|
)
|
|
|
|
|
2010-12-13 11:20:04 -07:00
|
|
|
type Context struct {
|
|
|
|
ctx *C.struct_ibv_context
|
|
|
|
}
|
2011-08-18 10:17:09 -06:00
|
|
|
|
|
|
|
func benchCgoCall(b *testing.B) {
|
|
|
|
const x = C.int(2)
|
|
|
|
const y = C.int(3)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
C.add(x, y)
|
|
|
|
}
|
|
|
|
}
|
2012-09-19 21:20:33 -06:00
|
|
|
|
|
|
|
// Issue 2470.
|
|
|
|
func testUnsignedInt(t *testing.T) {
|
|
|
|
a := (int64)(C.UINT32VAL)
|
|
|
|
b := (int64)(0xc008427b)
|
|
|
|
if a != b {
|
|
|
|
t.Errorf("Incorrect unsigned int - got %x, want %x", a, b)
|
|
|
|
}
|
|
|
|
}
|