r/golang • • 14h ago

discussion psst tinygo is passing more tests...

19 Upvotes

the little gophers have been very busy lately, see e.g. https://github.com/tinygo-org/tinygo/pull/6507


r/golang • • 10h ago

discussion cgo was killing my Go scheduler

5 Upvotes

I'm writing a userspace Linux scheduler in Go using eBPF and sched_ext. Go receives runnable tasks from eBPF, decides what should run next, and sends the decisions back for dispatch. This repeats every scheduling round.

The hot loop makes lots of short, nonblocking C calls to exchange task and dispatch records with the eBPF program. The problem wasn't just the cost of calling C. I also wanted to avoid runtime scheduling delays in the middle of a round.

With regular cgo, the runtime enters syscall state. If the P gets handed off while C is running, returning from C can mean waiting for a P before the Go code can continue. That's awkward when the code waiting to resume is responsible for scheduling other tasks.

I ended up using runtime.LockOSThread() together with a small runtime.asmcgocall bridge:

//go:linkname asmcgocall runtime.asmcgocall
//go:noescape
func asmcgocall(fn, arg unsafe.Pointer) int32

// Call invokes a bounded C wrapper. fn must be a C ABI function accepting one
// void pointer, with no blocking, allocation, or callbacks into Go.
func Call(fn, arg unsafe.Pointer) {
    asmcgocall(fn, arg)
}

LockOSThread keeps the goroutine on the same OS thread; the bridge skips the usual cgo syscall bookkeeping, avoiding that particular P handoff path. Linux can still preempt the thread.

I only use this path for bounded memory operations. Wrappers that perform syscalls go through a separate CallSyscall function using runtime.cgocall.

For a simple 64-bit read microbenchmark, regular cgo took around 220 ns per call versus 21 ns through the bridge. That's only call overhead, not overall scheduler performance.

The wrappers have to stay short and nonblocking, with no allocation or callbacks into Go. This also depends on an internal runtime function, so compatibility across Go versions is part of the trade-off.


r/golang • • 5h ago

discussion GPT 6 refused to reproduce Go assembly bug, but other models could

0 Upvotes

Consider the following example that adds two numbers and returns the result:

TEXT ·add(SB), $0-24
    MOVD a+0(FP), R0
    MOVD b+8(FP), R1
    WORD $0xd10043ff       // subsp, sp, #16
    WORD $0xf90007e0       // strx0, [sp, #8]
    WORD $0xf90003e1       // strx1, [sp]
    WORD $0xf94007e8       // ldrx8, [sp, #8]
    WORD $0xf94003e9       // ldrx9, [sp]
    WORD $0x8b090100       // addx0, x8, x9
    WORD $0x910043ff       // addsp, sp, #16
    MOVD R0, result+16(FP)
    RET

$-24 denotes the total size of arguments and return values. But the stack frame size was a wrong value 0. When the Go runtime performs stack unwinding—such as with runtime/trace's FlightRecorder—the mismatch between the declared frame size and actual stack usage can trigger a runtime crash.

When I tried asking GPT to generate a minimal reproduction test case, it refused on safety grounds:

This content can’t be shown
We take extra care with some cybersecurity requests. If you’re doing authorized security work, apply for Daybreak to get broader access.
Learn more: https://help.openai.com/en/articles/20001326
Apply for Daybreak: https://chatgpt.com/admin/status-and-access

But other LLMs completed the task:

ModelCLIReproduced
Gemini 4 Neon (XHigh)AntigravityYes
Gemini 4 Argon (High)AntigravityYes
Gemini 4 Hydrogen (XHigh)AntigravityYes
Gemini 3.8 Flash (High)AntigravityYes
Claude Opus 5.5 (High)AntigravityYes
Claude Sonet 5.5 (High)AntigravityYes
Grok 4.7 (High)CopilotYes
Qwen4-Max (Extra High)QoderYes
Kimi K3.1 Pro (Max)QoderYes
GPT 6.2-Astra xhighCodexNo
GPT 6.2-Sol xhighCodexNo
GPT 6.1-Sol xhighCodexNo
GPT 6-Astra xhighCodexNo
GPT-6.1 Terra xhighCodexNo
GPT-6-Luna xhighCodexNo

Full write-up and results:

https://gorse.io/posts/golang-stack-frames.html