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

x/tools/go/ssa: tracking issue for ssa support of slice to array casts #47326

Closed
16 tasks done
timothy-king opened this issue Jul 21, 2021 · 9 comments
Closed
16 tasks done
Labels
Analysis Issues related to static analysis (vet, x/tools/go/analysis) FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@timothy-king
Copy link
Contributor

timothy-king commented Jul 21, 2021

A new SliceToArrayPointer instruction is being added to x/tools/go/ssa to support the new Go 1.17 slice to pointer-to-an-array cast. Support for this new instruction should be propagated to the golang.org packages that could be impacted by this change. Most are in golang.org/x/tools.

This issue tracks the status of golang.org packages that use ssa that may be impacted by this change.

  • golang.org/x/tools/go/analysis/passes/nilness - precision can be improved.
  • golang.org/x/tools/go/callgraph/vta
  • golang.org/x/tools/go/pointer
  • golang.org/x/tools/cmd/guru
  • golang.org/x/tools/cmd/callgraph (indirect through x/tools/go/pointer)
  • golang.org/x/tools/godoc/analysis (indirect through x/tools/go/pointer)
  • golang.org/x/text/message/pipeline (indirect through x/tools/go/pointer)
  • golang.org/x/tools/go/ssa/interp

Other direct users of ssa that are not expected to be directly impacted:

  • golang.org/x/tools/go/callgraph
  • golang.org/x/exp/vulndb/internal/audit
  • golang.org/x/tools/cmd/ssadump
  • golang.org/x/tools/go/analysis/passes/buildssa
  • golang.org/x/tools/go/callgraph/cha
  • golang.org/x/tools/go/callgraph/rta
  • golang.org/x/tools/go/callgraph/static
  • golang.org/x/tools/go/ssa/ssautil
@timothy-king timothy-king added Tools This label describes issues relating to any tools in the x/tools repository. Analysis Issues related to static analysis (vet, x/tools/go/analysis) labels Jul 21, 2021
@timothy-king timothy-king added this to the Unplanned milestone Jul 21, 2021
@thanm thanm added the NeedsFix The path to resolution is known, but the work has not been done. label Jul 23, 2021
@timothy-king
Copy link
Contributor Author

golang.org/x/tools/go/analysis/passes/nilness - The precision can be improved by propagating that the post condition of [reaching the next instruction after] a SliceToArrayPointer is non-nil if the array type has length >= 1 and if it is 0, the nilness of the instruction follows the nilness of the operand.

Caveat: The bit in [] is that otherwise the SliceToArrayPointer instruction panic'd. I don't think this instruction can be referred to if a panic happened so I think this is an always okay interpretation. (Needs some thought. I doubt this is a serious concern for the checker though.)

@timothy-king
Copy link
Contributor Author

For the (indirect through x/tools/go/pointer) packages, no direct modifications are expected to be needed. x/tools/go/pointer just needs to be addressed before these packages are compatible with Go code containing slice to pointer-to-array casts.

@cuonglm
Copy link
Member

cuonglm commented Jul 24, 2021

@timothy-king For example:

func f() {
	var s []int
	t := (*[0]int)(s)
	println(*t)       // want "nil dereference in load"
	_ = *(*[0]int)(s) // want "nil dereference in load"

	// this operation is panic
	_ = *(*[1]int)(s) // want "nil dereference in load"
}

What should we do with the third conversion which can panic?

@timothy-king
Copy link
Contributor Author

What should we do with the third conversion which can panic?

If we know that the input to the cast is nil and the type has length >= 1, we can warn that the cast will always panic due to the nil. If it is not too much work a clearer message than "nil dereference in load" might be nice. Maybe "nil slice being cast to an array of len > 0 will always panic"?

@gopherbot
Copy link

Change https://golang.org/cl/337709 mentions this issue: go/analysis: add slice to array pointer conversion to nilness

@gopherbot
Copy link

Change https://golang.org/cl/338849 mentions this issue: go/callgraph/vta: Support the SliceToArrayPointer instruction.

gopherbot pushed a commit to golang/tools that referenced this issue Aug 4, 2021
No interesting type flows so the change to vta itself is to not reject the instruction.

Updates golang/go#47326

Change-Id: Ifd11a7ef854afaee3978796f3113ca3254301d19
Reviewed-on: https://go-review.googlesource.com/c/tools/+/338849
Reviewed-by: Zvonimir Pavlinovic <zpavlinovic@google.com>
Run-TryBot: Zvonimir Pavlinovic <zpavlinovic@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Zvonimir Pavlinovic <zpavlinovic@google.com>
Trust: Roland Shoemaker <roland@golang.org>
@gopherbot
Copy link

Change https://golang.org/cl/339890 mentions this issue: go/pointer: Support ssa.SliceToArrayPointer.

gopherbot pushed a commit to golang/tools that referenced this issue Aug 13, 2021
Updates golang/go#47326

Change-Id: I6b9b59e82b1b93f7a328ba802ad473d4104d7577
Reviewed-on: https://go-review.googlesource.com/c/tools/+/339890
Run-TryBot: Tim King <taking@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Guodong Li <guodongli@google.com>
Trust: Robert Findley <rfindley@google.com>
gopherbot pushed a commit to golang/tools that referenced this issue Aug 13, 2021
If we know that a nil slice is converted to a non-zero length array
pointer, warn user that this operation will always panic.

Updates golang/go#47326

Change-Id: Ic8adcc0255ddc621c5626dc5c525899b13e1c6b3
Reviewed-on: https://go-review.googlesource.com/c/tools/+/337709
Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Tim King <taking@google.com>
@scott-cotton
Copy link

Here are some data points on guru

given

package main

import "fmt"

func main() {
	i := 0
	var sl = make([]*int, 12)
	sl[2] = &i
	var a = (*[10]*int)(sl)
	_ = a[2]
	fmt.Println(a[2])
}

inside of a directory named "guru"

while running guru from x/tools @6e9046bfcd341

with go version devel go1.18-a6ff433d6a Thu Aug 26 02:06:43 2021 +0000 darwin/amd64

I get

scott@pavillion 47326 % guru  -scope guru/guru pointsto  guru/guru.go:#119,#123
/Users/scott/Dev/47326/guru/guru.go:10.6-10.9: this *int may not point to anything.
scott@pavillion 47326 % guru  -scope guru/guru pointsto  guru/guru.go:#78,#83
/Users/scott/Dev/47326/guru/guru.go:8.2-8.6: this *int may point to these objects:
/Users/scott/Dev/47326/guru/guru.go:6:2: 	I

So it does not quite work right (#119,#123 refers to 'a[2]', #78,#83 refers to 'sl[2]')

Looking at the guru code, it does not seem to need to treat *ssa.SliceToArrayPointer explicitly, it seems the problem is elsewhere.

@gopherbot
Copy link

Change https://go.dev/cl/403354 mentions this issue: guru: Add a TODO list to the guru cmd.

gopherbot pushed a commit to golang/tools that referenced this issue May 2, 2022
Adds a TODO list for the guru cmd. The main goal is document know
issues that we have not addressed.

Updates golang/go#47326
Updates golang/go#52503

Change-Id: I126b0f06081b606124d89a13f8805fa1ac6e56e3
Reviewed-on: https://go-review.googlesource.com/c/tools/+/403354
TryBot-Result: Gopher Robot <gobot@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
@golang golang locked and limited conversation to collaborators Apr 30, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Analysis Issues related to static analysis (vet, x/tools/go/analysis) FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

5 participants