[WIP] Implement owner-goroutine check for QuickJS Runtime/Context#727
Closed
Copilot wants to merge 1 commit into
Closed
[WIP] Implement owner-goroutine check for QuickJS Runtime/Context#727Copilot wants to merge 1 commit into
Copilot wants to merge 1 commit into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #727 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 11 11
Lines 2872 2872
=========================================
Hits 2872 2872 Continue to review full report in Codecov by Sentry.
🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
internal/goidpackage withGet() int64usingruntime.Stack()parsing (portable, Go 1.21-1.26 safe)ownerGID int64field toRuntimestruct, set inNewRuntime()ownerGID int64field toContextstruct, set inNewContext()checkOwner()helper to bothRuntimeandContext(panics with clear message on goroutine mismatch)checkOwner()intoContext.hasValidRef(),Context.Close(),Context.ProcessJobs(),Context.Globals(),Context.NewString(),Context.Eval()checkOwner()intoRuntime.NewContext()andRuntime.Close()owner_test.gowith tests verifying the owner-goroutine check panics from a non-owner goroutine and passes from the owner goroutineOriginal prompt
Repo: buke/quickjs-go
Goal
Background
Requirements
Implementation guidance
Notes
The following is the prior conversation context from the user's chat exploration (may be truncated):
User: assembly goid
Assistant: In Go assembly,
goidusually means the current goroutine’s ID—but there is no supported, stable Go API or ABI to read it directly from assembly.What you can do depends on your goal:
1) If you just need a per-goroutine value: use TLS / g pointer (assembly)
On most architectures the runtime keeps a register pointing to the current
g(goroutine) struct (often referred to asg/g_m/ “G pointer”). From assembly you can often access fields ofg, but the layout and offsets are runtime-internal and version-dependent (can break across Go versions).Typical pattern (conceptually):
gpointer (arch-specific)g.goidfield (offset varies)To do this safely you must generate offsets from the exact Go version you build with (people use
go tool compile -S, runtime headers, orgo:linkname-based hacks). It’s not portable.2) If you need it in Go code: don’t (preferred)
Go intentionally doesn’t expose goroutine IDs. Usual alternatives:
runtime/pproflabels for tracinggoidonly for debugging (best-effort hacks)3) If you want a “best effort” debugging hack
Some projects parse
runtime.Stack()output ("goroutine 1234 [running]:") to extract an ID. This is slow and not guaranteed, but doesn’t rely on runtime struct offsets.Clarifying questions (so I can give the exact assembly you need)
go1.22.5,go1.23.x, etc.)?If you answer those 3, I can show the correct Go assembly skeleton for that arch (Plan 9 syntax), and explain where the
gpointer comes from and how to wire it to a Go wrapper.User: 推荐一些 current goroutine’s ID 的库吧
Assistant: 下面这些库都能拿到“当前 goroutine 的 ID(非官方、仅供调试/观测)”。提醒一句:Go 官方明确不保证 goroutine id 的可用性/稳定性,很多实现要么读 runtime 内部结构、要么解析 runtime.Stack,跨 Go 版本可能会碎。(stackoverflow.com)...
This pull request was created from Copilot chat.