tinygo icon indicating copy to clipboard operation
tinygo copied to clipboard

linux: symbol linker could not find symbol runtime.vgetrandom with Go 1.24.x

Open deadprogram opened this issue 6 months ago • 2 comments

When attempting to compile some code for Linux target, I ran into this same error as @soypat did here: https://github.com/tinygo-org/tinygo/issues/4828#issuecomment-2813572848

/home/ron/Development/tinygo/tinygo-122/src/internal/syscall/unix/getrandom.go:26: linker could not find symbol runtime.vgetrandom

The rsa-keygen benchmark program is what triggered it on my machine. See https://github.com/tinygo-org/tinybench/issues/3

I suspect that this issue first appears in Go 1.24.x due to this commit: https://github.com/golang/go/commit/eb6f2c24cd17c0ca1df7e343f8d9187eef7d6e13

deadprogram avatar Jun 10 '25 19:06 deadprogram

I think I have a fix for this and it was surprisingly simple...

diff --git a/src/runtime/rand.go b/src/runtime/rand.go
new file mode 100644
index 00000000..531910ef
--- /dev/null
+++ b/src/runtime/rand.go
@@ -0,0 +1,11 @@
+package runtime
+
+import _ "unsafe"
+
+// TODO: Use hardware when available
+//
+//go:linkname vgetrandom
+func vgetrandom(p []byte, flags uint32) (ret int, supported bool) { return 0, false }
+
+//go:linkname fatal crypto/internal/sysrand.fatal
+func fatal(msg string) { runtimePanic(msg) }
diff --git a/tests/os/smoke/smoke_test.go b/tests/os/smoke/smoke_test.go
index 068023d6..f454909a 100644
--- a/tests/os/smoke/smoke_test.go
+++ b/tests/os/smoke/smoke_test.go
@@ -4,6 +4,8 @@ package os_smoke_test
 // Intended to catch build tag mistakes affecting bare metal targets.
 
 import (
+	"crypto/rand"
+	"crypto/rsa"
 	"fmt"
 	"path/filepath"
 	"testing"
@@ -25,3 +27,10 @@ func TestFmt(t *testing.T) {
 		t.Errorf("printf returned %d, expected 14", n)
 	}
 }
+
+// Regression test for https://github.com/tinygo-org/tinygo/issues/4921
+func TestRand(t *testing.T) {
+	if _, err := rsa.GenerateKey(rand.Reader, 2048); err != nil {
+		t.Error(err)
+	}
+}

ben-krieger avatar Sep 19 '25 01:09 ben-krieger

Labeled to close this issue on next release. Thank you for the fix @ben-krieger

deadprogram avatar Sep 19 '25 13:09 deadprogram

Closing since was included with v0.40.0

deadprogram avatar Dec 18 '25 20:12 deadprogram