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

cmd/compile: negative types are allowed, but will not be negative during program execution, with boundary checking #64895

Open
qiulaidongfeng opened this issue Dec 29, 2023 · 3 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Performance
Milestone

Comments

@qiulaidongfeng
Copy link
Contributor

Go version

go version devel go1.22-78b42a5338a Fri Dec 8 03:28:17 2023 +0000 linux/amd64

What operating system and processor architecture are you using (go env)?

set GO111MODULE=auto
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\26454\AppData\Local\go-build
set GOENV=C:\Users\26454\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=D:\file\gofile\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=D:\file\gofile
set GOPRIVATE=
set GOPROXY=https://goproxy.cn,direct
set GOROOT=C:\Users\26454\.go\current
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLCHAIN=local
set GOTOOLDIR=C:\Users\26454\.go\current\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=devel go1.22-f6509cf Thu Dec 21 00:15:58 2023 +0000
set GCCGO=gccgo
set GOAMD64=v3
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=D:\tmp\go-build1429133840=/tmp/go-build -gno-record-gcc-switches

What did you do?

package main

type s struct {
	str string
	i   int
}

func main() {
	var l s
	l.str = "100"
	for l.i < len(l.str) {
		switch l.str[l.i] {
		case '1':
			l.i++
		case '0':
			for i := l.i + 1; i < len(l.str); i++ {
				_ = l.str[l.i]
			}
		}
	}
}

What did you expect to see?

No Boundary check.

What did you see instead?

Have Boundary check.

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Dec 29, 2023
@qiulaidongfeng
Copy link
Contributor Author

l.str[l.i] memory is not accessed out of bounds, and this boundary check can be eliminated.

@qiulaidongfeng qiulaidongfeng changed the title cmd/compile: boundary check not eliminated cmd/compile: for l.i < len(l.str) and switch l.str[l.i] boundary check not eliminated Dec 30, 2023
@go101
Copy link

go101 commented Jan 2, 2024

l.i might be negative in theory. That is the cause of the bound check.
Though, for this specified case, the compiler can be smarter. (But worthy it?).

@qiulaidongfeng
Copy link
Contributor Author

qiulaidongfeng commented Jan 3, 2024

l.i might be negative in theory. That is the cause of the bound check. Though, for this specified case, the compiler can be smarter. (But worthy it?).

This issue was discovered in the lexer of a real open source project : https://gitee.com/u-language/u-language/blob/master/ucom/lex2/lex.go

If you have a way to make the code linked above have no boundary check without using unsafe.
Can prove not worth the compiler can be more smarter.

@qiulaidongfeng qiulaidongfeng changed the title cmd/compile: for l.i < len(l.str) and switch l.str[l.i] boundary check not eliminated cmd/compile: negative types are allowed, but will not be negative during program execution, with boundary checking Jan 3, 2024
@dmitshur dmitshur added this to the Backlog milestone Jan 9, 2024
@dmitshur dmitshur added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jan 9, 2024
@mknyszek mknyszek modified the milestones: Backlog, Unplanned Jan 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Performance
Projects
Development

No branches or pull requests

6 participants