Skip to content

Commit 2093985

Browse files
cagedmantispull[bot]
authored andcommitted
runtime: validate all calls to SetFinalizer
This change moves the check for a change in the memory management system to after the SetFinalizer parameters have been validated. Moving the check ensures that invalid parameters will never pass the validation checks. Change-Id: I9f1d3454f891f7b147c0d86b6720297172e08ef9 Reviewed-on: https://go-review.googlesource.com/c/go/+/625035 Reviewed-by: Michael Knyszek <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 2950bb0 commit 2093985

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

src/runtime/mfinal.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -409,11 +409,6 @@ func blockUntilEmptyFinalizerQueue(timeout int64) bool {
409409
// need to use appropriate synchronization, such as mutexes or atomic updates,
410410
// to avoid read-write races.
411411
func SetFinalizer(obj any, finalizer any) {
412-
if debug.sbrk != 0 {
413-
// debug.sbrk never frees memory, so no finalizers run
414-
// (and we don't have the data structures to record them).
415-
return
416-
}
417412
e := efaceOf(&obj)
418413
etyp := e._type
419414
if etyp == nil {
@@ -426,11 +421,15 @@ func SetFinalizer(obj any, finalizer any) {
426421
if ot.Elem == nil {
427422
throw("nil elem type!")
428423
}
429-
430424
if inUserArenaChunk(uintptr(e.data)) {
431425
// Arena-allocated objects are not eligible for finalizers.
432426
throw("runtime.SetFinalizer: first argument was allocated into an arena")
433427
}
428+
if debug.sbrk != 0 {
429+
// debug.sbrk never frees memory, so no finalizers run
430+
// (and we don't have the data structures to record them).
431+
return
432+
}
434433

435434
// find the containing object
436435
base, span, _ := findObject(uintptr(e.data), 0, 0)

0 commit comments

Comments
 (0)