Skip to content
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

(constant -4 of type uintptr) overflows uintptr #2806

Closed
Tracked by #2667
kyleconroy opened this issue Apr 25, 2022 · 6 comments
Closed
Tracked by #2667

(constant -4 of type uintptr) overflows uintptr #2806

kyleconroy opened this issue Apr 25, 2022 · 6 comments

Comments

@kyleconroy
Copy link
Contributor

kyleconroy commented Apr 25, 2022

TinyGo can't compile the the pointer_unsafe.go file from https://github.com/protocolbuffers/protobuf-go. I get the following error message:

unsafe.Sizeof(unsafe.Pointer(nil)) - unsafe.Sizeof((MessageState literal)) (constant -4 of type uintptr) overflows uintptr
@aykevl
Copy link
Member

aykevl commented Apr 25, 2022

This comment above it is relevant:

// Static check that MessageState does not exceed the size of a pointer.

So it appears that MessageState is larger than a pointer.
Without further information, this would be hard to debug. How can I reproduce this?


That said, this is most certainly unsafe code so that throws away all promises of portability. But quickly looking at it, I don't see why it fails.

@skabbes
Copy link
Contributor

skabbes commented Apr 25, 2022

I had a comment previously that I deleted, I did not read aykevl's comment closely enough.

The MessageState struct embeds a few structs with 0 fields.

My new guess is that these structs take space in TinyGo, but do not take space in "regular Go".

@kyleconroy
Copy link
Contributor Author

Without further information, this would be hard to debug. How can I reproduce this?

Sorry about that. Here's the reproduction.

go mod init github.com/tinygo-issues
go get google.golang.org/protobuf
package main

import (
	"fmt"

	_ "google.golang.org/protobuf/runtime/protoimpl"
)

func main() {
	fmt.Println("protoc")
}
tinygo build -o example.wasm -gc=leaking -scheduler=none -wasm-abi=generic -target=wasi
# google.golang.org/protobuf/internal/impl
../../go/pkg/mod/google.golang.org/protobuf@v1.28.0/internal/impl/pointer_unsafe.go:142:16: unsafe.Sizeof(unsafe.Pointer(nil)) - unsafe.Sizeof((MessageState literal)) (constant -4 of type uintptr) overflows uintptr
make: *** [run] Error 1

@skabbes
Copy link
Contributor

skabbes commented May 18, 2022

I trimmed down the minimal repro - to be something even more minimal.

I think the bug can be restated as "sizeof 0-length arrays of functions is greater than 0.

package main

import (
	"unsafe"
)

// DoNotCompare can be embedded in a struct to prevent comparability.
// This struct, when embedded takes non-zero space
type DoNotCompare [0]func()

type MessageState struct {
	DoNotCompare
	data *int32
}

// Static check that MessageState does not exceed the size of a pointer.
const _ = unsafe.Sizeof(unsafe.Pointer(nil)) - unsafe.Sizeof((MessageState{}))

func main() {
	println("protoc minimal example")
}

@skabbes
Copy link
Contributor

skabbes commented May 19, 2022

FYI @kyleconroy #2854 fixed this issue, it is merged into the dev branch. I think you can probably close this issue.

@deadprogram
Copy link
Member

I have tagged to close on next release.

@deadprogram deadprogram removed the next-release Will be part of next release label Jul 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants