Runtime error with cgo and certain string slices - go

I have stripped back a problem I have come across whilst wrapping some C code to work with golang using swig but the problem doesn't rest with swig.
I can pass in a basic string slice but as soon as I construct the slice with anything other than basic strings, I get a panic: runtime error: cgo argument has Go pointer to Go pointer.
go version go1.8.5 linux/amd64
This is the sample code and its output
package main
import (
"fmt"
"reflect"
"unsafe"
)
/*
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct { char *p; int n; } _gostring_;
typedef struct { void* array; int len; int cap; } _goslice_;
void prtText(char * const *txt, int len)
{
int i = 0;
for ( i=0; i<len; i++ ) {
printf("Text %d is: %s\n", i, txt[i]);
}
}
void _wrap_printText(_goslice_ _swig_go_0) {
_gostring_ *p;
char **arg1 = (char **)calloc(_swig_go_0.len, sizeof(char*));
if (arg1) {
for (int i=0; i<_swig_go_0.len; i++) {
p = &(((_gostring_*)_swig_go_0.array)[i]);
arg1[i] = calloc(1,(p->n)+1);
strncpy(arg1[i], p->p, p->n);
}
}
int arg2 = _swig_go_0.len;
prtText((char *const *)arg1,arg2);
}
*/
import "C"
func PrintText(arg1 []string) {
C._wrap_printText(*(*C._goslice_)(unsafe.Pointer(&arg1)))
}
func main() {
s := []string{}
s = append(s, "blah")
s = append(s, "hello")
s = append(s, "again")
ns := []string{}
ns = append(ns, "ns: "+s[0])
ns = append(ns, "ns: "+s[1])
ns = append(ns, "ns: "+s[2])
fmt.Println("type s:", reflect.TypeOf(s))
fmt.Println("type ns:", reflect.TypeOf(ns))
fmt.Println("s:", s)
fmt.Println("ns:", ns)
PrintText(s)
PrintText(ns)
}
go build -i -x -gcflags '-N -l' main.go
./main
type s: []string
type ns: []string
s: [blah hello again]
ns: [ns: blah ns: hello ns: again]
Text 0 is: blah
Text 1 is: hello
Text 2 is: again
panic: runtime error: cgo argument has Go pointer to Go pointer
As you can see, the first string slice works fine but as soon as I do anything other than basic strings, it fails. I've tried making new strings first before appending them to the slice but the problem remains.
What am I doing wrong?

You're basically passing the raw Go pointers.
Instead, you should build C arrays yourself.
As a general rule, seeing unsafe pretty much anywhere should make you suspicious. It is rarely the right way around issues with cgo.
Using the helpers from Passing array of string as parameter from go to C function and using them in your code:
package main
import (
"fmt"
"reflect"
)
/*
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void prtText(char * const *txt, int len)
{
int i = 0;
for ( i=0; i<len; i++ ) {
printf("Text %d is: %s\n", i, txt[i]);
}
}
static char**makeCharArray(int size) {
return calloc(sizeof(char*), size);
}
static void setArrayString(char **a, char *s, int n) {
a[n] = s;
}
static void freeCharArray(char **a, int size) {
int i;
for (i = 0; i < size; i++)
free(a[i]);
free(a);
}
*/
import "C"
func main() {
s := []string{}
s = append(s, "blah")
s = append(s, "hello")
s = append(s, "again")
ns := []string{}
ns = append(ns, "ns: "+s[0])
ns = append(ns, "ns: "+s[1])
ns = append(ns, "ns: "+s[2])
fmt.Println("type s:", reflect.TypeOf(s))
fmt.Println("type ns:", reflect.TypeOf(ns))
fmt.Println("s:", s)
fmt.Println("ns:", ns)
sargs := C.makeCharArray(C.int(len(s)))
defer C.freeCharArray(sargs, C.int(len(s)))
for i, p := range s {
C.setArrayString(sargs, C.CString(p), C.int(i))
}
nsargs := C.makeCharArray(C.int(len(ns)))
defer C.freeCharArray(nsargs, C.int(len(ns)))
for i, p := range ns {
C.setArrayString(nsargs, C.CString(p), C.int(i))
}
C.prtText(sargs, C.int(len(s)))
C.prtText(nsargs, C.int(len(ns)))
}
The output is now as expected:
$ ./main
type s: []string
type ns: []string
s: [blah hello again]
ns: [ns: blah ns: hello ns: again]
Text 0 is: blah
Text 1 is: hello
Text 2 is: again
Text 0 is: ns: blah
Text 1 is: ns: hello
Text 2 is: ns: again

Related

Convert *_Ctype_float into float32 in Go

I am new to Go and having difficulty in converting the *_Ctype_float datatype into []float32. Is there something that I am missing? I even thought of converting *_Ctype_float into string but even that was not successful.
I have this C function named predictInstance which returns float*. I am calling this function from Go by
predictionValues := C.predictInstance(
handle,
(*C.float)(unsafe.Pointer(&req.FlatInput[0])),
)
Now when I look at the type of predictionValues it says it is *Ctype_float. Now I want to convert this into []float32
I have a C function which returns a float* array which I wish to convert to []float32. I am calling this
function from Go with a float* array argument.
A working example,
package main
/*
#include <stdlib.h>
#include <float.h>
float *reverse(float *f, int len) {
float *g = calloc(len, sizeof(float));
for (int i = 0; i < len; i++) {
g[i] = f[len-1-i];
}
return g;
}
*/
import "C"
import (
"fmt"
"math"
"unsafe"
)
func main() {
a := []float32{3.14159, 2.718, 1}
r := make([]float32, len(a))
fmt.Println("a:", a, "r:", r)
c := C.reverse((*C.float)(&a[0]), C.int(len(a)))
copy(r, (*[1 << 20]float32)(unsafe.Pointer(c))[:])
C.free(unsafe.Pointer(c))
fmt.Println("a:", a, "r:", r)
}
var okCFloat = func() bool {
if C.sizeof_float != unsafe.Sizeof(float32(0)) {
panic("C float != Go float32")
}
if C.FLT_MAX != math.MaxFloat32 {
panic("C float != Go float32")
}
return true
}()
Output:
a: [3.14159 2.718 1] r: [0 0 0]
a: [3.14159 2.718 1] r: [1 2.718 3.14159]

Passing array of string as parameter from go to C function

I have one C function:
int cgroup_change_cgroup_path(const char * path, pid_t pid, const char *const controllers[])
I want to call it in go language by using cgo.
How to pass the third parameter as it accepts a C array of string.
You can build the arrays using c helper functions and then use them.
Here is a solution to the same problem:
// C helper functions:
static char**makeCharArray(int size) {
return calloc(sizeof(char*), size);
}
static void setArrayString(char **a, char *s, int n) {
a[n] = s;
}
static void freeCharArray(char **a, int size) {
int i;
for (i = 0; i < size; i++)
free(a[i]);
free(a);
}
// Build C array in Go from sargs []string
cargs := C.makeCharArray(C.int(len(sargs)))
defer C.freeCharArray(cargs, C.int(len(sargs)))
for i, s := range sargs {
C.setArrayString(cargs, C.CString(s), C.int(i))
}
golangnuts post by John Barham

Casting a int to a pointer?

in C/C++ I can cast int to a int* like this
int i = 0x1040c108;
int *p = (int*)i; // compiles
but why can't I do this in Go?
addr := 0x1040c108
p := (*int)(addr) // Error: cannot convert addr (type int) to type *int
What is the way to achieve this in golang ??
Go through the unsafe package. Please note that it is called "unsafe" for a reason and probably shouldn't be used unless you REALLY need to do operations that bypass type safeties, or operate on memory directly.
https://play.golang.org/p/WUavNAlyVP
package main
import (
"fmt"
"unsafe"
)
func main() {
fmt.Println("Hello, playground")
var intaddr int = 0x1040c108
var addr uintptr = uintptr(intaddr)
ptr := unsafe.Pointer(addr)
p := (*int)(ptr)
fmt.Printf("Type: %T, Ptr: %v, Val: %d", p, p, *p)
}

Static local variable in Go

Is it possible to define a local variable in Go that can maintain its value from one function call to another? In C, we can do this using the reserved word static.
Example in C:
int func() {
static int x = 0;
x++;
return x;
}
Use a closure:
Function literals are closures: they may refer to variables defined in
a surrounding function. Those variables are then shared between the
surrounding function and the function literal, and they survive as
long as they are accessible.
It doesn't have to be in global scope, just outside the function definition.
func main() {
x := 1
y := func() {
fmt.Println("x:", x)
x++
}
for i := 0; i < 10; i++ {
y()
}
}
(Sample on the Go Playground)
You can do something like this
package main
import (
"fmt"
)
func main() {
f := do()
f() // 1
f() // 2
}
func do() (f func()){
var i int
f = func(){
i++
fmt.Println(i)
}
return
}
Link on Playground https://play.golang.org/p/D9mv9_qKmN
Declare a var at global scope:
var i = 1
func a() {
println(i)
i++
}
Like Taric' suggestion, but with staticCounter() returning an int function
package main
import (
"fmt"
)
func staticCounter() (f func()(int)){
var i int
f = func()(int){
i++
// fmt.Println(i)
return i
}
return
}
func main() {
f := staticCounter()
g := staticCounter()
fmt.Println(f())
fmt.Println(f())
fmt.Println(f())
fmt.Println(f())
fmt.Println(g())
fmt.Println(g())
}
Use Function closure
In following example, variable sum behaves like a separate static for each closure a1 and a2.
package main
import "fmt"
func adder() func(int) int {
sum := 0
return func(x int) int {
sum += x
return sum
}
}
func main() {
a1,a2 := adder(), adder()
for i := 0; i < 10; i++ {
fmt.Println(
a1(i),
a2(-1*i),
)
}
}
Output
0 0
1 -1
3 -3
6 -6
10 -10
15 -15
21 -21
28 -28
36 -36
45 -45
In all the other answers, the function containing a static variable is assigned in the main.
Here is how you can define and assign that function in the global scope.
var myFunction = func() func(type1, type2) type3 {
myStaticVariable := []string {"hello", "world"}
return func(arg1 type1, arg2 type2) type3 {
// use arg1, arg2 and myStaticVariable here
}
}()
// A var x1 is local to main(), is not a global var.
// A static var is one that can't be accesed from others functions just
// like global vars.
// A static var dont disappears when the function ends.
// So is what x1 n x2 are pretending in this program.
package main
import (
"fmt"
)
/*
int func() { // x static inside a function.
static int x = 0;
x++;
return x;
}
*/
//
func main() {
//
var x1 int = 0
var x2 int = 100
//
for i := 0; i < 10; i++ { // call to a "static" var x
x1 = fun1(&x1)
x2 = fun2(&x2)
fmt.Printf("%d %d \n", x1, x2)
} //
test1(x1, x2) // a funct needs parameters to see x1 n x2
} //main
//
func fun1(p *int) int {
//
*p++ // save value
return *p //counter x1
}
//
func fun2(p *int) int {
*p++ // save value
return *p //counter x2
}
//
func test1(x1 int, x2 int) {
fmt.Println("\"x1\" y \"x2\" ", x1, x2)
}

Returning values through the arguments of go function, which is called from C

Suppose, we've got a Go function, which is doing something with agruments, passed to them, e.g. it could fill the buffer, allocated in the C part and changing it and for example an integer argument, which is a size of read data. It works well with an integer one, but not with a "data part". Just see a code.
package main
/*
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
extern int some(uint8_t *, int *);
static int somewrap() {
uint8_t *i = malloc(16);
int A = 1;
int *x = &A;
some(i, x);
fprintf(stderr, "c.wrapper, i=%s, %p, x=%d, %p\n", i, i, *x, x);
return 0;
}
*/
import "C"
import "fmt"
import (
"unsafe"
)
//export some
func some(i *C.uint8_t, x *C.int) C.int {
fmt.Println("i:", i, &i, *i, "x:", x, &x, *x)
p := []byte("xxx")
i = (*C.uint8_t)(unsafe.Pointer(&p[0]))
*x = C.int(42)
fmt.Println("i:", i, &i, *i, "x:", x, &x, *x)
return C.int(0)
}
func main() {
C.somewrap()
}
As a result, we've got following:
i: 0x4303a40 0xc210000018 0 x: 0x7fff5fbff874 0xc210000020 1
i: 0xc210000038 0xc210000018 120 x: 0x7fff5fbff874 0xc210000020 42
c.wrapper, i=, 0x4303a40, x=42, 0x7fff5fbff874
As you can see, it works well for integer pointer, but not for uint8_t.
You're re-assigning i within some to another address, not change the value at the given address (unless I'm misunderstanding what you're trying to accomplish)
*i = *(*C.uint8_t)(unsafe.Pointer(&p[0]))

Resources