cmd/viewcore: account for partially allocated arenas

Arenas used to be allocated completely (or at least, have a span
allocated to represent the unused space). Since 1.13.3, the latest
arena only has spans up to its highest allocation point.

Fixes viewcore for 1.13.3.

This change was CL 189957, backported to 1.13 at CL 198485.

Update golang/go#38638

Change-Id: Ia8cb14925cf6f4226a80e7ff4f011aa93e8b41d1
Reviewed-on: https://go-review.googlesource.com/c/debug/+/232158
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
diff --git a/internal/gocore/gocore_test.go b/internal/gocore/gocore_test.go
index 00f10b4..bdb843d 100644
--- a/internal/gocore/gocore_test.go
+++ b/internal/gocore/gocore_test.go
@@ -269,4 +269,5 @@
 	loadExampleVersion(t, "1.11")
 	loadExampleVersion(t, "1.12.zip")
 	loadExampleVersion(t, "1.13.zip")
+	loadExampleVersion(t, "1.13.3.zip")
 }
diff --git a/internal/gocore/process.go b/internal/gocore/process.go
index a3fe7b1..a2426a0 100644
--- a/internal/gocore/process.go
+++ b/internal/gocore/process.go
@@ -306,6 +306,14 @@
 			panic("weird mapping " + m.Perm().String())
 		}
 	}
+	if mheap.HasField("curArena") {
+		// Subtract from the heap unallocated space
+		// in the current arena.
+		ca := mheap.Field("curArena")
+		unused := int64(ca.Field("end").Uintptr() - ca.Field("base").Uintptr())
+		heap -= unused
+		all -= unused
+	}
 	pageSize := p.rtConstants["_PageSize"]
 
 	// Span types
diff --git a/internal/gocore/testdata/1.13.3.zip b/internal/gocore/testdata/1.13.3.zip
new file mode 100644
index 0000000..5e13ea6
--- /dev/null
+++ b/internal/gocore/testdata/1.13.3.zip
Binary files differ