Skip to content

wasm: include wasi-libc#845

Merged
deadprogram merged 1 commit intodevfrom
wasi-libc
Jan 23, 2020
Merged

wasm: include wasi-libc#845
deadprogram merged 1 commit intodevfrom
wasi-libc

Conversation

@aykevl
Copy link
Copy Markdown
Member

@aykevl aykevl commented Jan 12, 2020

This allows CGo code to call some libc functions.

Fixes part of #844.

@aykevl
Copy link
Copy Markdown
Member Author

aykevl commented Jan 12, 2020

Note: there are no tests yet, because otherwise bare-metal CGo tests would break. I intend to add tests when a bare metal libc has been added.

@aykevl
Copy link
Copy Markdown
Member Author

aykevl commented Jan 14, 2020

This PR can be tested with the following patch:

diff --git a/testdata/cgo/main.go b/testdata/cgo/main.go
index 41bc6892..557dea5c 100644
--- a/testdata/cgo/main.go
+++ b/testdata/cgo/main.go
@@ -4,6 +4,7 @@ package main
 int fortytwo(void);
 #include "main.h"
 int mul(int, int);
+#include <string.h>
 */
 import "C"
 
@@ -109,6 +110,12 @@ func main() {
        var _ C.option3_t = C.option3A
        println("option 2A:", C.option2A)
        println("option 3A:", C.option3A)
+
+       // libc: test whether C functions work at all.
+       buf1 := []byte("foobar\x00")
+       buf2 := make([]byte, len(buf1))
+       C.strcpy((*C.char)(unsafe.Pointer(&buf2[0])), (*C.char)(unsafe.Pointer(&buf1[0])))
+       println("copied string:", string(buf2[:C.strlen((*C.char)(unsafe.Pointer(&buf2[0])))]))
 }
 
 func printUnion(union C.joined_t) C.joined_t {
diff --git a/testdata/cgo/out.txt b/testdata/cgo/out.txt
index 73cb2b99..fcb68846 100644
--- a/testdata/cgo/out.txt
+++ b/testdata/cgo/out.txt
@@ -55,3 +55,4 @@ option F: 11
 option G: 12
 option 2A: 20
 option 3A: 21
+copied string: foobar

This allows CGo code to call some libc functions. Additionally, by
putting memset/memmove/memcpy in an archive they're not included anymore
when not necessary, reducing code size for small programs.
@aykevl aykevl added the cgo CGo and libc related issues label Jan 19, 2020
@deadprogram
Copy link
Copy Markdown
Member

Look good to me. Basically since it does not cause anything else to fail, it is a step forward that we need.

Now merging.

@deadprogram deadprogram merged commit e2aa378 into dev Jan 23, 2020
@deadprogram
Copy link
Copy Markdown
Member

Hmm spoke too soon. Azure build did not succeed. Rerunning just to be sure...

@deadprogram
Copy link
Copy Markdown
Member

WebAssembly/wasi-libc#46 might be helpful?

@deadprogram
Copy link
Copy Markdown
Member

Actually i am thinking it might be the wasi-libc-sysroot-v0 cache. What do you think?

@aykevl
Copy link
Copy Markdown
Member Author

aykevl commented Jan 24, 2020

I suspect the self-test at the end somehow included system headers instead of the ones from wasi.

@deadprogram
Copy link
Copy Markdown
Member

If we want to skip those tests at the end, we can run make finish instead of just make (the default task is check which is what is failing there.

@aykevl aykevl deleted the wasi-libc branch January 25, 2020 11:41
@aykevl
Copy link
Copy Markdown
Member Author

aykevl commented Jan 25, 2020

Interesting:

2020-01-23T16:10:21.4277832Z # Generate a test file that includes all public header files.
2020-01-23T16:10:21.4488191Z cd "D:/a/1/s/lib/wasi-libc/sysroot" && \
2020-01-23T16:10:21.4489227Z for header in $(find include -type f -not -name mman.h |grep -v /bits/); do \
2020-01-23T16:10:21.4489646Z     echo '#include <'$header'>' | sed 's/include\///' ; \
2020-01-23T16:10:21.4490010Z done |LC_ALL=C sort >share/wasm32-wasi/include-all.c ; \
2020-01-23T16:10:21.4490480Z cd - >/dev/null
2020-01-23T16:10:21.4934925Z FIND: Parameter format not correct
2020-01-23T16:10:21.5222246Z # Test that it compiles.
2020-01-23T16:10:21.5386023Z "D:/a/1/s/llvm-build/bin/clang" -O2 -DNDEBUG --target=wasm32-wasi -fno-trapping-math -mthread-model single --sysroot="D:/a/1/s/lib/wasi-libc/sysroot" -fsyntax-only "D:/a/1/s/lib/wasi-libc/sysroot/share/wasm32-wasi/include-all.c" -Wno-\#warnings

Especially the FIND: Parameter format not correct part.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cgo CGo and libc related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants