New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
plugin: variable not initialized properly #62430
Comments
I observe that if you add this line anywhere inside your top-level main() function, it works:
Or even just add this outside the main function:
Hence it appears to be something to do with the triggering (or suppression) of package initialization. The documentation says:
You have already imported "unicode" to access the type |
Hum, I have not been able to reproduce this behavior with another variable. If I add a init function using unicode.Categories in the plugin, there is no reason to remove its initialization.
But it prints 0.
|
Yes, what matters is whether the main program initializes unicode (which you can do using "var z = unicode.Categories" in the top-level main.go) As a complete guess:
|
I modified your reproducer to demonstrate a similar issue with the regexp library as reported on golang-nuts:
|
cc @thanm I haven't looked into this carefully, but I guess this is related to the map initialization change. My guess (without looking into details) is that as the main program imports the unicode package but not explicitly use the map, it initialize the unicode package but not the map. When the plugin is loaded, it sees the unicode package is already initialized by the main program, it doesn't initialize it again, leaving the map uninitialized. Maybe we need to initialize maps for programs that can use plugins. |
Supporting what @cherrymui said, I bisected to e2ca417 |
Thanks for the report. I will take a look. |
Change https://go.dev/cl/526115 mentions this issue: |
The regexp case is almost certainly also due to an uninitialized map, in
|
@gopherbot please backport this to Go 1.21. This is a regression that causes valid programs to run incorrectly. |
Backport issue(s) opened: #62505 (for 1.21). Remember to create the cherry-pick CL(s) as soon as the patch is submitted to master, according to https://go.dev/wiki/MinorReleases. |
Change https://go.dev/cl/526575 mentions this issue: |
…or programs using plugins If a program imports the plugin package, the mechanisms in place for detecting and deleting unused global map variables are no longer safe, since it's possibly for a given global map var to be unreferenced in the main program but referenced by a plugin. This patch changes the linker to test for plugin use and to avoid removing any unused global map variables if the main program could possibly load up a plugin. Fixes #62505. Updates #62430. Change-Id: Ie00b18b681cb0d259e3c859ac947ade5778cd6c8 (cherry picked from commit 660620d) Reviewed-on: https://go-review.googlesource.com/c/go/+/526575 Reviewed-by: Cherry Mui <cherryyz@google.com> Run-TryBot: Than McIntosh <thanm@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> TryBot-Result: Gopher Robot <gobot@golang.org>
…or programs using plugins If a program imports the plugin package, the mechanisms in place for detecting and deleting unused global map variables are no longer safe, since it's possibly for a given global map var to be unreferenced in the main program but referenced by a plugin. This patch changes the linker to test for plugin use and to avoid removing any unused global map variables if the main program could possibly load up a plugin. Fixes golang#62505. Updates golang#62430. Change-Id: Ie00b18b681cb0d259e3c859ac947ade5778cd6c8 (cherry picked from commit 660620d) Reviewed-on: https://go-review.googlesource.com/c/go/+/526575 Reviewed-by: Cherry Mui <cherryyz@google.com> Run-TryBot: Than McIntosh <thanm@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> TryBot-Result: Gopher Robot <gobot@golang.org>
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
I tried to use regexp on a plugin and it fails. After a little investigation, I saw unicode.Categories is not properly initialized.
Plugin: p1/main.go
main.go:
Execution:
$ go build -buildmode=plugin ./p1 && go run main.go unicode.Categories not properly initialized
If I add a use of unicode.Categories in the ./main.go, it is properly initialized. For example:
_ = unicode.Categories["C"]
.Note: when unicode.Categories is not properly initialized, It is not nil but an empty map.
What did you expect to see?
The unicode.Categories properly initialized.
What did you see instead?
An empty map.
The text was updated successfully, but these errors were encountered: