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

all: add internal/asan and internal/msan packages #64611

Open
mauri870 opened this issue Dec 7, 2023 · 13 comments
Open

all: add internal/asan and internal/msan packages #64611

mauri870 opened this issue Dec 7, 2023 · 13 comments
Assignees
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@mauri870
Copy link
Member

mauri870 commented Dec 7, 2023

There are a couple issues where having asan/msan enabled throw off tests for memory allocations, see #64257 and #64256.

Most tests already skip these memory allocation assertions when they detect that race is enabled, but such functionality is not exposed for asan and msan.

Here is one example of such test:

if n := testing.AllocsPerRun(100, func() { Grow(s2, cap(s2)-len(s2)+1) }); n != 1 {
    errorf := t.Errorf
    if race.Enabled || testenv.OptimizationOff() {
        errorf = t.Logf // this allocates multiple times with race or msan enabled
    }
    errorf("Grow should allocate once when given insufficient capacity; allocated %v times", n)
}

One alternative is using the asan and msan build tags to skip certain tests, but that does not work in the middle of a test, only for entire files.

There is runtime.msanenabled, runtime.asanenabled, syscall.asanenabled we could use, but they are private to their respective packages.

It is worth noting that syscall and runtime duplicate some code related to asan:

  • syscall has asanenabled, asanRead, asanWrite that basically call into the runtime.
  • runtime has asanenabled as well

I propose we create internal/asan and internal/msan packages akin to internal/race. Then syscall and runtime would call internal/asan.Enabled and syscall could use ASanRead, ASanWrite as well, without having this logic duplicated.

The tests can also use internal/asan.Enabled to check if they should skip tests that allocate memory, which most tests already do currently by checking race.Enabled. We can also use these inside testenv.SkipIfOptimizationOff/testenv.OptimizationOff to make this process more seamless.

Here is the proposed apis for these new internal packages:

internal/asan

//go:build asan

package asan

import (
	"unsafe"
)

const Enabled = true

func Read(addr unsafe.Pointer, len int)
func Write(addr unsafe.Pointer, len int)

The !asan version would have stubs for these functions.

internal/msan

The only place that uses msan currently is the runtime, but having access to a msan.Enabled would be useful in tests as well and would make a good trio with internal/asan and internal/race. Other packages might be able to use the exported functions more easily as well.

The proposed api for internal/msan would then be:

//go:build msan

package msan

import (
	"unsafe"
)

const Enabled = true

func Read(addr unsafe.Pointer, sz uintptr)
func Write(addr unsafe.Pointer, sz uintptr)
func Malloc(addr unsafe.Pointer, sz uintptr)
func Free(addr unsafe.Pointer, sz uintptr)
func Move(dst, src unsafe.Pointer, sz uintptr)

The !msan version would have stubs for these functions.

@mauri870 mauri870 added Proposal compiler/runtime Issues related to the Go compiler and/or runtime. labels Dec 7, 2023
@gopherbot gopherbot added this to the Proposal milestone Dec 7, 2023
@mauri870
Copy link
Member Author

mauri870 commented Dec 7, 2023

cc @golang/runtime

@cherrymui
Copy link
Member

We would need to expose new runtime apis for msan, which are currently private. That is why I decided to keep the Proposal status for this issue:
// runtime
func MSanRead(addr unsafe.Pointer, sz uintptr)
func MSanWrite(addr unsafe.Pointer, sz uintptr)
func MSanMalloc(addr unsafe.Pointer, sz uintptr)
func MSanFree(addr unsafe.Pointer, sz uintptr)
func MSanMove(dst, src unsafe.Pointer, sz uintptr)

With internal/msan, do we actually need to export them in the runtime? We can arrange internal/msan to call the assembly functions (e.g. runtime.msanmalloc) directly, and other packages just use internal/msan.

@mauri870
Copy link
Member Author

mauri870 commented Dec 8, 2023

@cherrymui That works for me, I was trying to structure internal/msan the same way internal/asan is implemented, but it makes sense to call the assembly directly.

@mauri870 mauri870 changed the title proposal: all: add internal/asan and internal/msan packages all: add internal/asan and internal/msan packages Dec 9, 2023
@mauri870
Copy link
Member Author

mauri870 commented Dec 9, 2023

Taking this out of the proposal process as we can proceed without adding new runtime APIs.

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/548695 mentions this issue: internal/asan: add new package

mauri870 added a commit to mauri870/go that referenced this issue Dec 10, 2023
The internal/msan package contains helper functions for manually
instrumenting code for the memory sanitizer. It exports the private
msan routines in runtime uncoditionally, making the functions a
no-op if the build flag "msan" is not present.

For golang#64611
mauri870 added a commit to mauri870/go that referenced this issue Dec 10, 2023
The internal/msan package contains helper functions for manually
instrumenting code for the memory sanitizer. It exports the private
msan routines in runtime uncoditionally, making the functions a
no-op if the build flag "msan" is not present.

For golang#64611
mauri870 added a commit to mauri870/go that referenced this issue Dec 10, 2023
The internal/msan package contains helper functions for manually
instrumenting code for the memory sanitizer. It exports the private
msan routines in runtime uncoditionally, making the functions a
no-op if the build flag "msan" is not present.

For golang#64611
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/548676 mentions this issue: internal/msan: add new package

mauri870 added a commit to mauri870/go that referenced this issue Dec 10, 2023
The internal/msan package contains helper functions for manually
instrumenting code for the memory sanitizer. It exports the private
msan routines in runtime uncoditionally, making the functions a
no-op if the build flag "msan" is not present.

For golang#64611
@prattmic prattmic added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Dec 11, 2023
@prattmic prattmic added this to the Backlog milestone Dec 11, 2023
@mknyszek mknyszek moved this from Todo to In Progress in Go Compiler / Runtime Dec 13, 2023
@mauri870
Copy link
Member Author

mauri870 commented Jan 26, 2024

With the tree now reopened, I would like to confirm whether the addition of internal/asan and internal/msan is acceptable to the runtime team, as the NeedsDecision label is still associated with this issue. I have prepared https://go.dev/cl/548676 and https://go.dev/cl/548695 in case we decide to proceed with this. Thanks.

@aclements
Copy link
Member

This all looks reasonable to me. I'd like to see a follow-up CL that uses these new packages to clean up some of the rest of the tree. Thanks!

@aclements aclements added NeedsFix The path to resolution is known, but the work has not been done. and removed NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. labels Jan 29, 2024
@mauri870
Copy link
Member Author

This all looks reasonable to me. I'd like to see a follow-up CL that uses these new packages to clean up some of the rest of the tree. Thanks!

I'm looking forward to working on the cleanup and fixing stdlib tests that currently fail with asan/msan.

Is it okay if I also submit a CL to x/devapp/owners adding these new packages? The runtime team would be the primary maintainer, and I can take on the secondary maintainer role if that is acceptable for the team.

@cherrymui
Copy link
Member

I'm looking forward to working on the cleanup and fixing stdlib tests that currently fail with asan/msan.

Thanks for working on this. I think this can be two steps. Refactoring first, then adding the notations to tests. This is probably cleaner and clearer.

gopherbot pushed a commit that referenced this issue Feb 13, 2024
The internal/asan package contains helper functions for manually
instrumenting code for the address sanitizer. It reexports the asan
routines in runtime unconditionally, making the functions a no-op if the
build flag "asan" is not present.

For #64611

Change-Id: Ie79e698aea7a6d969afd2a5f008c084c9545b1a5
GitHub-Last-Rev: e658670
GitHub-Pull-Request: #64635
Reviewed-on: https://go-review.googlesource.com/c/go/+/548695
Reviewed-by: Austin Clements <austin@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
ezz-no pushed a commit to ezz-no/go-ezzno that referenced this issue Feb 18, 2024
The internal/asan package contains helper functions for manually
instrumenting code for the address sanitizer. It reexports the asan
routines in runtime unconditionally, making the functions a no-op if the
build flag "asan" is not present.

For golang#64611

Change-Id: Ie79e698aea7a6d969afd2a5f008c084c9545b1a5
GitHub-Last-Rev: e658670
GitHub-Pull-Request: golang#64635
Reviewed-on: https://go-review.googlesource.com/c/go/+/548695
Reviewed-by: Austin Clements <austin@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
gopherbot pushed a commit that referenced this issue Feb 20, 2024
The internal/msan package contains helper functions for manually
instrumenting code for the memory sanitizer. It exports the private
msan routines in runtime unconditionally, making the functions a
no-op if the build flag "msan" is not present.

For #64611

Change-Id: If43f29e112ac79a47083c9dbdf2c61a0641e80b1
GitHub-Last-Rev: 0a644bd
GitHub-Pull-Request: #64637
Reviewed-on: https://go-review.googlesource.com/c/go/+/548676
Reviewed-by: Austin Clements <austin@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/566735 mentions this issue: devapp/owners: add internal asan and msan packages

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/566755 mentions this issue: syscall: use internal/asan and internal/msan

gopherbot pushed a commit that referenced this issue Mar 17, 2024
Now with internal/asan and internal/msan available we can cleanup
syscall's duplicated definitions.

For #64611

Change-Id: If714d04ed2d32a4ed27305b3e3dc64ba8cdd1b61
GitHub-Last-Rev: e52fff1
GitHub-Pull-Request: #65935
Reviewed-on: https://go-review.googlesource.com/c/go/+/566755
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: qiulaidongfeng <2645477756@qq.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/572755 mentions this issue: internal/asan: match runtime.asan{read,write} len parameter type

gopherbot pushed a commit that referenced this issue Mar 19, 2024
The len parameter runtime.asan{read,write} is of type uintptr. Match its
type in Read and Write.

For #64611

Change-Id: I0be278c38a357e600521ced87c0e23038a11e8a2
Reviewed-on: https://go-review.googlesource.com/c/go/+/572755
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Tobias Klauser <tobias.klauser@gmail.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
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. NeedsFix The path to resolution is known, but the work has not been done.
Projects
Status: In Progress
Development

No branches or pull requests

5 participants