runtime: handle nil pointer in sysFreeOS on sbrk platforms

On sbrk platforms (Plan 9 and Wasm), sysReserveOS
returns nil when it can't reserve memory at the
requested hint address. When the caller passes that
nil result to sysUnreserve, sysFreeOS reaches
memFree(nil), which clears the region with
memclrNoHeapPointers, writing to address 0.

On Plan 9, this faults the program with a
"sys: trap: fault write" note. It is reproducible
on plan9/amd64 with TestArenaCollision, which
exhausts the arena hints and unreserves the failed
reservations.

On mmap-based platforms, sysUnreserve(nil) is a
harmless munmap(nil). This change makes sysFreeOS
return early for a nil pointer, so freeing or
unreserving nil is a no-op on sbrk platforms too.

Change-Id: I2e3057a5bb34254d600d05b5fa128c77960d4e9a
Reviewed-on: https://go-review.googlesource.com/c/go/+/793400
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Richard Miller <millerresearch@gmail.com>
diff --git a/src/runtime/mem_sbrk.go b/src/runtime/mem_sbrk.go
index 9e752df..b8a2a75 100644
--- a/src/runtime/mem_sbrk.go
+++ b/src/runtime/mem_sbrk.go
@@ -206,6 +206,10 @@
 }
 
 func sysFreeOS(v unsafe.Pointer, n uintptr) {
+	if v == nil {
+		// A failed sysReserveOS returns nil, so freeing it is a no-op.
+		return
+	}
 	systemstack(func() {
 		lock(&memlock)
 		if uintptr(v)+n == bloc {