Skip to content

cmd/compile: write barrier code is sometimes preemptible when compiled with -N [1.21 backport] #61958

Closed

Description

@randall77
package main

var g *int

func h() {
	g = nil
}

When compiled with -N (optimizations off), the empty block on the write-barrier-disabled branch is marked as preemptible when it shouldn't be.

	0x0018 00024 (/Users/khr/gowork/tmp7.go:6)	MOVWU	runtime.writeBarrier(SB), R0
	0x0020 00032 (/Users/khr/gowork/tmp7.go:6)	CBZW	R0, 40
	0x0024 00036 (/Users/khr/gowork/tmp7.go:6)	JMP	44
	0x0028 00040 (/Users/khr/gowork/tmp7.go:6)	PCDATA	$0, $-1
	0x0028 00040 (/Users/khr/gowork/tmp7.go:6)	JMP	64                     <= this one
	0x002c 00044 (/Users/khr/gowork/tmp7.go:6)	PCDATA	$0, $-2
	0x002c 00044 (/Users/khr/gowork/tmp7.go:6)	MOVD	main.g(SB), R0
	0x0034 00052 (/Users/khr/gowork/tmp7.go:6)	CALL	runtime.gcWriteBarrier1(SB)
	0x0038 00056 (/Users/khr/gowork/tmp7.go:6)	MOVD	R0, (R25)
	0x003c 00060 (/Users/khr/gowork/tmp7.go:6)	JMP	64
	0x0040 00064 (/Users/khr/gowork/tmp7.go:6)	MOVD	ZR, main.g(SB)

(PCDATA of -1 is preemptible, -2 is unpreemptible).

This could lead to very rare but nasty heap corruption when compiling with optimizations off (e.g. when running with Delve).

Activity

  1. added this to the Go1.21.1 milestone on Aug 11, 2023
  2. self-assigned this
    on Aug 11, 2023
  3. gopherbot commented on Aug 11, 2023

    @gopherbot
    Contributor

    Change https://go.dev/cl/518755 mentions this issue: [release-branch.go1.21] cmd/compile: ensure empty blocks in write barriers are marked unpreemptible

  4. dmitshur commented on Aug 16, 2023

    @dmitshur
    Member

    @randall77 Is there a corresponding upstream issue (i.e., for Go1.22 milestone) for this backport request? Does this apply to Go 1.20 too, or only Go 1.21?

    Per https://go.dev/wiki/MinorReleases:

    As soon as an interested party thinks an issue should be considered for backport, they open one or two “child” issues titled like package: title [1.17 backport]. The issue should include a link to the original issue and a short rationale about why the backport might be needed.

  5. changed the title [-]cmd/compile: write barrier code is sometimes preemptible when compiled with -N[/-] [+]cmd/compile: write barrier code is sometimes preemptible when compiled with -N [1.21 backport][/+] on Aug 16, 2023
  6. added
    CherryPickApprovedUsed during the release process for point releases
    and removed
    CherryPickCandidateUsed during the release process for point releases
    on Aug 16, 2023
  7. randall77 commented on Aug 16, 2023

    @randall77
    ContributorAuthor

    This applies only to 1.21, 1.20 does not have the bug in question.
    There's no upstream (1.22) issue, but there are CLs (518055 and 518539). They aren't easily backportable as is because the code has changed in the meantime.
    CL 518755 is a simpler, safer backport for just the version of the bug that manifests in 1.21.

  8. added a commit that references this issue on Aug 17, 2023
    25ec110
  9. gopherbot commented on Aug 17, 2023

    @gopherbot
    Contributor

    Closed by merging 25ec110 to release-branch.go1.21.

  10. locked and limited conversation to collaborators on Aug 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions