]> Cypherpunks.ru repositories - gostls13.git/blob - src/runtime/malloc.go
runtime, syscall: add calls to asan functions
[gostls13.git] / src / runtime / malloc.go
1 // Copyright 2014 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Memory allocator.
6 //
7 // This was originally based on tcmalloc, but has diverged quite a bit.
8 // http://goog-perftools.sourceforge.net/doc/tcmalloc.html
9
10 // The main allocator works in runs of pages.
11 // Small allocation sizes (up to and including 32 kB) are
12 // rounded to one of about 70 size classes, each of which
13 // has its own free set of objects of exactly that size.
14 // Any free page of memory can be split into a set of objects
15 // of one size class, which are then managed using a free bitmap.
16 //
17 // The allocator's data structures are:
18 //
19 //      fixalloc: a free-list allocator for fixed-size off-heap objects,
20 //              used to manage storage used by the allocator.
21 //      mheap: the malloc heap, managed at page (8192-byte) granularity.
22 //      mspan: a run of in-use pages managed by the mheap.
23 //      mcentral: collects all spans of a given size class.
24 //      mcache: a per-P cache of mspans with free space.
25 //      mstats: allocation statistics.
26 //
27 // Allocating a small object proceeds up a hierarchy of caches:
28 //
29 //      1. Round the size up to one of the small size classes
30 //         and look in the corresponding mspan in this P's mcache.
31 //         Scan the mspan's free bitmap to find a free slot.
32 //         If there is a free slot, allocate it.
33 //         This can all be done without acquiring a lock.
34 //
35 //      2. If the mspan has no free slots, obtain a new mspan
36 //         from the mcentral's list of mspans of the required size
37 //         class that have free space.
38 //         Obtaining a whole span amortizes the cost of locking
39 //         the mcentral.
40 //
41 //      3. If the mcentral's mspan list is empty, obtain a run
42 //         of pages from the mheap to use for the mspan.
43 //
44 //      4. If the mheap is empty or has no page runs large enough,
45 //         allocate a new group of pages (at least 1MB) from the
46 //         operating system. Allocating a large run of pages
47 //         amortizes the cost of talking to the operating system.
48 //
49 // Sweeping an mspan and freeing objects on it proceeds up a similar
50 // hierarchy:
51 //
52 //      1. If the mspan is being swept in response to allocation, it
53 //         is returned to the mcache to satisfy the allocation.
54 //
55 //      2. Otherwise, if the mspan still has allocated objects in it,
56 //         it is placed on the mcentral free list for the mspan's size
57 //         class.
58 //
59 //      3. Otherwise, if all objects in the mspan are free, the mspan's
60 //         pages are returned to the mheap and the mspan is now dead.
61 //
62 // Allocating and freeing a large object uses the mheap
63 // directly, bypassing the mcache and mcentral.
64 //
65 // If mspan.needzero is false, then free object slots in the mspan are
66 // already zeroed. Otherwise if needzero is true, objects are zeroed as
67 // they are allocated. There are various benefits to delaying zeroing
68 // this way:
69 //
70 //      1. Stack frame allocation can avoid zeroing altogether.
71 //
72 //      2. It exhibits better temporal locality, since the program is
73 //         probably about to write to the memory.
74 //
75 //      3. We don't zero pages that never get reused.
76
77 // Virtual memory layout
78 //
79 // The heap consists of a set of arenas, which are 64MB on 64-bit and
80 // 4MB on 32-bit (heapArenaBytes). Each arena's start address is also
81 // aligned to the arena size.
82 //
83 // Each arena has an associated heapArena object that stores the
84 // metadata for that arena: the heap bitmap for all words in the arena
85 // and the span map for all pages in the arena. heapArena objects are
86 // themselves allocated off-heap.
87 //
88 // Since arenas are aligned, the address space can be viewed as a
89 // series of arena frames. The arena map (mheap_.arenas) maps from
90 // arena frame number to *heapArena, or nil for parts of the address
91 // space not backed by the Go heap. The arena map is structured as a
92 // two-level array consisting of a "L1" arena map and many "L2" arena
93 // maps; however, since arenas are large, on many architectures, the
94 // arena map consists of a single, large L2 map.
95 //
96 // The arena map covers the entire possible address space, allowing
97 // the Go heap to use any part of the address space. The allocator
98 // attempts to keep arenas contiguous so that large spans (and hence
99 // large objects) can cross arenas.
100
101 package runtime
102
103 import (
104         "internal/goarch"
105         "internal/goos"
106         "runtime/internal/atomic"
107         "runtime/internal/math"
108         "runtime/internal/sys"
109         "unsafe"
110 )
111
112 const (
113         debugMalloc = false
114
115         maxTinySize   = _TinySize
116         tinySizeClass = _TinySizeClass
117         maxSmallSize  = _MaxSmallSize
118
119         pageShift = _PageShift
120         pageSize  = _PageSize
121         pageMask  = _PageMask
122         // By construction, single page spans of the smallest object class
123         // have the most objects per span.
124         maxObjsPerSpan = pageSize / 8
125
126         concurrentSweep = _ConcurrentSweep
127
128         _PageSize = 1 << _PageShift
129         _PageMask = _PageSize - 1
130
131         // _64bit = 1 on 64-bit systems, 0 on 32-bit systems
132         _64bit = 1 << (^uintptr(0) >> 63) / 2
133
134         // Tiny allocator parameters, see "Tiny allocator" comment in malloc.go.
135         _TinySize      = 16
136         _TinySizeClass = int8(2)
137
138         _FixAllocChunk = 16 << 10 // Chunk size for FixAlloc
139
140         // Per-P, per order stack segment cache size.
141         _StackCacheSize = 32 * 1024
142
143         // Number of orders that get caching. Order 0 is FixedStack
144         // and each successive order is twice as large.
145         // We want to cache 2KB, 4KB, 8KB, and 16KB stacks. Larger stacks
146         // will be allocated directly.
147         // Since FixedStack is different on different systems, we
148         // must vary NumStackOrders to keep the same maximum cached size.
149         //   OS               | FixedStack | NumStackOrders
150         //   -----------------+------------+---------------
151         //   linux/darwin/bsd | 2KB        | 4
152         //   windows/32       | 4KB        | 3
153         //   windows/64       | 8KB        | 2
154         //   plan9            | 4KB        | 3
155         _NumStackOrders = 4 - goarch.PtrSize/4*goos.IsWindows - 1*goos.IsPlan9
156
157         // heapAddrBits is the number of bits in a heap address. On
158         // amd64, addresses are sign-extended beyond heapAddrBits. On
159         // other arches, they are zero-extended.
160         //
161         // On most 64-bit platforms, we limit this to 48 bits based on a
162         // combination of hardware and OS limitations.
163         //
164         // amd64 hardware limits addresses to 48 bits, sign-extended
165         // to 64 bits. Addresses where the top 16 bits are not either
166         // all 0 or all 1 are "non-canonical" and invalid. Because of
167         // these "negative" addresses, we offset addresses by 1<<47
168         // (arenaBaseOffset) on amd64 before computing indexes into
169         // the heap arenas index. In 2017, amd64 hardware added
170         // support for 57 bit addresses; however, currently only Linux
171         // supports this extension and the kernel will never choose an
172         // address above 1<<47 unless mmap is called with a hint
173         // address above 1<<47 (which we never do).
174         //
175         // arm64 hardware (as of ARMv8) limits user addresses to 48
176         // bits, in the range [0, 1<<48).
177         //
178         // ppc64, mips64, and s390x support arbitrary 64 bit addresses
179         // in hardware. On Linux, Go leans on stricter OS limits. Based
180         // on Linux's processor.h, the user address space is limited as
181         // follows on 64-bit architectures:
182         //
183         // Architecture  Name              Maximum Value (exclusive)
184         // ---------------------------------------------------------------------
185         // amd64         TASK_SIZE_MAX     0x007ffffffff000 (47 bit addresses)
186         // arm64         TASK_SIZE_64      0x01000000000000 (48 bit addresses)
187         // ppc64{,le}    TASK_SIZE_USER64  0x00400000000000 (46 bit addresses)
188         // mips64{,le}   TASK_SIZE64       0x00010000000000 (40 bit addresses)
189         // s390x         TASK_SIZE         1<<64 (64 bit addresses)
190         //
191         // These limits may increase over time, but are currently at
192         // most 48 bits except on s390x. On all architectures, Linux
193         // starts placing mmap'd regions at addresses that are
194         // significantly below 48 bits, so even if it's possible to
195         // exceed Go's 48 bit limit, it's extremely unlikely in
196         // practice.
197         //
198         // On 32-bit platforms, we accept the full 32-bit address
199         // space because doing so is cheap.
200         // mips32 only has access to the low 2GB of virtual memory, so
201         // we further limit it to 31 bits.
202         //
203         // On ios/arm64, although 64-bit pointers are presumably
204         // available, pointers are truncated to 33 bits. Furthermore,
205         // only the top 4 GiB of the address space are actually available
206         // to the application, but we allow the whole 33 bits anyway for
207         // simplicity.
208         // TODO(mknyszek): Consider limiting it to 32 bits and using
209         // arenaBaseOffset to offset into the top 4 GiB.
210         //
211         // WebAssembly currently has a limit of 4GB linear memory.
212         heapAddrBits = (_64bit*(1-goarch.IsWasm)*(1-goos.IsIos*goarch.IsArm64))*48 + (1-_64bit+goarch.IsWasm)*(32-(goarch.IsMips+goarch.IsMipsle)) + 33*goos.IsIos*goarch.IsArm64
213
214         // maxAlloc is the maximum size of an allocation. On 64-bit,
215         // it's theoretically possible to allocate 1<<heapAddrBits bytes. On
216         // 32-bit, however, this is one less than 1<<32 because the
217         // number of bytes in the address space doesn't actually fit
218         // in a uintptr.
219         maxAlloc = (1 << heapAddrBits) - (1-_64bit)*1
220
221         // The number of bits in a heap address, the size of heap
222         // arenas, and the L1 and L2 arena map sizes are related by
223         //
224         //   (1 << addr bits) = arena size * L1 entries * L2 entries
225         //
226         // Currently, we balance these as follows:
227         //
228         //       Platform  Addr bits  Arena size  L1 entries   L2 entries
229         // --------------  ---------  ----------  ----------  -----------
230         //       */64-bit         48        64MB           1    4M (32MB)
231         // windows/64-bit         48         4MB          64    1M  (8MB)
232         //      ios/arm64         33         4MB           1  2048  (8KB)
233         //       */32-bit         32         4MB           1  1024  (4KB)
234         //     */mips(le)         31         4MB           1   512  (2KB)
235
236         // heapArenaBytes is the size of a heap arena. The heap
237         // consists of mappings of size heapArenaBytes, aligned to
238         // heapArenaBytes. The initial heap mapping is one arena.
239         //
240         // This is currently 64MB on 64-bit non-Windows and 4MB on
241         // 32-bit and on Windows. We use smaller arenas on Windows
242         // because all committed memory is charged to the process,
243         // even if it's not touched. Hence, for processes with small
244         // heaps, the mapped arena space needs to be commensurate.
245         // This is particularly important with the race detector,
246         // since it significantly amplifies the cost of committed
247         // memory.
248         heapArenaBytes = 1 << logHeapArenaBytes
249
250         // logHeapArenaBytes is log_2 of heapArenaBytes. For clarity,
251         // prefer using heapArenaBytes where possible (we need the
252         // constant to compute some other constants).
253         logHeapArenaBytes = (6+20)*(_64bit*(1-goos.IsWindows)*(1-goarch.IsWasm)*(1-goos.IsIos*goarch.IsArm64)) + (2+20)*(_64bit*goos.IsWindows) + (2+20)*(1-_64bit) + (2+20)*goarch.IsWasm + (2+20)*goos.IsIos*goarch.IsArm64
254
255         // heapArenaBitmapBytes is the size of each heap arena's bitmap.
256         heapArenaBitmapBytes = heapArenaBytes / (goarch.PtrSize * 8 / 2)
257
258         pagesPerArena = heapArenaBytes / pageSize
259
260         // arenaL1Bits is the number of bits of the arena number
261         // covered by the first level arena map.
262         //
263         // This number should be small, since the first level arena
264         // map requires PtrSize*(1<<arenaL1Bits) of space in the
265         // binary's BSS. It can be zero, in which case the first level
266         // index is effectively unused. There is a performance benefit
267         // to this, since the generated code can be more efficient,
268         // but comes at the cost of having a large L2 mapping.
269         //
270         // We use the L1 map on 64-bit Windows because the arena size
271         // is small, but the address space is still 48 bits, and
272         // there's a high cost to having a large L2.
273         arenaL1Bits = 6 * (_64bit * goos.IsWindows)
274
275         // arenaL2Bits is the number of bits of the arena number
276         // covered by the second level arena index.
277         //
278         // The size of each arena map allocation is proportional to
279         // 1<<arenaL2Bits, so it's important that this not be too
280         // large. 48 bits leads to 32MB arena index allocations, which
281         // is about the practical threshold.
282         arenaL2Bits = heapAddrBits - logHeapArenaBytes - arenaL1Bits
283
284         // arenaL1Shift is the number of bits to shift an arena frame
285         // number by to compute an index into the first level arena map.
286         arenaL1Shift = arenaL2Bits
287
288         // arenaBits is the total bits in a combined arena map index.
289         // This is split between the index into the L1 arena map and
290         // the L2 arena map.
291         arenaBits = arenaL1Bits + arenaL2Bits
292
293         // arenaBaseOffset is the pointer value that corresponds to
294         // index 0 in the heap arena map.
295         //
296         // On amd64, the address space is 48 bits, sign extended to 64
297         // bits. This offset lets us handle "negative" addresses (or
298         // high addresses if viewed as unsigned).
299         //
300         // On aix/ppc64, this offset allows to keep the heapAddrBits to
301         // 48. Otherwise, it would be 60 in order to handle mmap addresses
302         // (in range 0x0a00000000000000 - 0x0afffffffffffff). But in this
303         // case, the memory reserved in (s *pageAlloc).init for chunks
304         // is causing important slowdowns.
305         //
306         // On other platforms, the user address space is contiguous
307         // and starts at 0, so no offset is necessary.
308         arenaBaseOffset = 0xffff800000000000*goarch.IsAmd64 + 0x0a00000000000000*goos.IsAix
309         // A typed version of this constant that will make it into DWARF (for viewcore).
310         arenaBaseOffsetUintptr = uintptr(arenaBaseOffset)
311
312         // Max number of threads to run garbage collection.
313         // 2, 3, and 4 are all plausible maximums depending
314         // on the hardware details of the machine. The garbage
315         // collector scales well to 32 cpus.
316         _MaxGcproc = 32
317
318         // minLegalPointer is the smallest possible legal pointer.
319         // This is the smallest possible architectural page size,
320         // since we assume that the first page is never mapped.
321         //
322         // This should agree with minZeroPage in the compiler.
323         minLegalPointer uintptr = 4096
324 )
325
326 // physPageSize is the size in bytes of the OS's physical pages.
327 // Mapping and unmapping operations must be done at multiples of
328 // physPageSize.
329 //
330 // This must be set by the OS init code (typically in osinit) before
331 // mallocinit.
332 var physPageSize uintptr
333
334 // physHugePageSize is the size in bytes of the OS's default physical huge
335 // page size whose allocation is opaque to the application. It is assumed
336 // and verified to be a power of two.
337 //
338 // If set, this must be set by the OS init code (typically in osinit) before
339 // mallocinit. However, setting it at all is optional, and leaving the default
340 // value is always safe (though potentially less efficient).
341 //
342 // Since physHugePageSize is always assumed to be a power of two,
343 // physHugePageShift is defined as physHugePageSize == 1 << physHugePageShift.
344 // The purpose of physHugePageShift is to avoid doing divisions in
345 // performance critical functions.
346 var (
347         physHugePageSize  uintptr
348         physHugePageShift uint
349 )
350
351 // OS memory management abstraction layer
352 //
353 // Regions of the address space managed by the runtime may be in one of four
354 // states at any given time:
355 // 1) None - Unreserved and unmapped, the default state of any region.
356 // 2) Reserved - Owned by the runtime, but accessing it would cause a fault.
357 //               Does not count against the process' memory footprint.
358 // 3) Prepared - Reserved, intended not to be backed by physical memory (though
359 //               an OS may implement this lazily). Can transition efficiently to
360 //               Ready. Accessing memory in such a region is undefined (may
361 //               fault, may give back unexpected zeroes, etc.).
362 // 4) Ready - may be accessed safely.
363 //
364 // This set of states is more than is strictly necessary to support all the
365 // currently supported platforms. One could get by with just None, Reserved, and
366 // Ready. However, the Prepared state gives us flexibility for performance
367 // purposes. For example, on POSIX-y operating systems, Reserved is usually a
368 // private anonymous mmap'd region with PROT_NONE set, and to transition
369 // to Ready would require setting PROT_READ|PROT_WRITE. However the
370 // underspecification of Prepared lets us use just MADV_FREE to transition from
371 // Ready to Prepared. Thus with the Prepared state we can set the permission
372 // bits just once early on, we can efficiently tell the OS that it's free to
373 // take pages away from us when we don't strictly need them.
374 //
375 // For each OS there is a common set of helpers defined that transition
376 // memory regions between these states. The helpers are as follows:
377 //
378 // sysAlloc transitions an OS-chosen region of memory from None to Ready.
379 // More specifically, it obtains a large chunk of zeroed memory from the
380 // operating system, typically on the order of a hundred kilobytes
381 // or a megabyte. This memory is always immediately available for use.
382 //
383 // sysFree transitions a memory region from any state to None. Therefore, it
384 // returns memory unconditionally. It is used if an out-of-memory error has been
385 // detected midway through an allocation or to carve out an aligned section of
386 // the address space. It is okay if sysFree is a no-op only if sysReserve always
387 // returns a memory region aligned to the heap allocator's alignment
388 // restrictions.
389 //
390 // sysReserve transitions a memory region from None to Reserved. It reserves
391 // address space in such a way that it would cause a fatal fault upon access
392 // (either via permissions or not committing the memory). Such a reservation is
393 // thus never backed by physical memory.
394 // If the pointer passed to it is non-nil, the caller wants the
395 // reservation there, but sysReserve can still choose another
396 // location if that one is unavailable.
397 // NOTE: sysReserve returns OS-aligned memory, but the heap allocator
398 // may use larger alignment, so the caller must be careful to realign the
399 // memory obtained by sysReserve.
400 //
401 // sysMap transitions a memory region from Reserved to Prepared. It ensures the
402 // memory region can be efficiently transitioned to Ready.
403 //
404 // sysUsed transitions a memory region from Prepared to Ready. It notifies the
405 // operating system that the memory region is needed and ensures that the region
406 // may be safely accessed. This is typically a no-op on systems that don't have
407 // an explicit commit step and hard over-commit limits, but is critical on
408 // Windows, for example.
409 //
410 // sysUnused transitions a memory region from Ready to Prepared. It notifies the
411 // operating system that the physical pages backing this memory region are no
412 // longer needed and can be reused for other purposes. The contents of a
413 // sysUnused memory region are considered forfeit and the region must not be
414 // accessed again until sysUsed is called.
415 //
416 // sysFault transitions a memory region from Ready or Prepared to Reserved. It
417 // marks a region such that it will always fault if accessed. Used only for
418 // debugging the runtime.
419
420 func mallocinit() {
421         if class_to_size[_TinySizeClass] != _TinySize {
422                 throw("bad TinySizeClass")
423         }
424
425         if heapArenaBitmapBytes&(heapArenaBitmapBytes-1) != 0 {
426                 // heapBits expects modular arithmetic on bitmap
427                 // addresses to work.
428                 throw("heapArenaBitmapBytes not a power of 2")
429         }
430
431         // Copy class sizes out for statistics table.
432         for i := range class_to_size {
433                 memstats.by_size[i].size = uint32(class_to_size[i])
434         }
435
436         // Check physPageSize.
437         if physPageSize == 0 {
438                 // The OS init code failed to fetch the physical page size.
439                 throw("failed to get system page size")
440         }
441         if physPageSize > maxPhysPageSize {
442                 print("system page size (", physPageSize, ") is larger than maximum page size (", maxPhysPageSize, ")\n")
443                 throw("bad system page size")
444         }
445         if physPageSize < minPhysPageSize {
446                 print("system page size (", physPageSize, ") is smaller than minimum page size (", minPhysPageSize, ")\n")
447                 throw("bad system page size")
448         }
449         if physPageSize&(physPageSize-1) != 0 {
450                 print("system page size (", physPageSize, ") must be a power of 2\n")
451                 throw("bad system page size")
452         }
453         if physHugePageSize&(physHugePageSize-1) != 0 {
454                 print("system huge page size (", physHugePageSize, ") must be a power of 2\n")
455                 throw("bad system huge page size")
456         }
457         if physHugePageSize > maxPhysHugePageSize {
458                 // physHugePageSize is greater than the maximum supported huge page size.
459                 // Don't throw here, like in the other cases, since a system configured
460                 // in this way isn't wrong, we just don't have the code to support them.
461                 // Instead, silently set the huge page size to zero.
462                 physHugePageSize = 0
463         }
464         if physHugePageSize != 0 {
465                 // Since physHugePageSize is a power of 2, it suffices to increase
466                 // physHugePageShift until 1<<physHugePageShift == physHugePageSize.
467                 for 1<<physHugePageShift != physHugePageSize {
468                         physHugePageShift++
469                 }
470         }
471         if pagesPerArena%pagesPerSpanRoot != 0 {
472                 print("pagesPerArena (", pagesPerArena, ") is not divisible by pagesPerSpanRoot (", pagesPerSpanRoot, ")\n")
473                 throw("bad pagesPerSpanRoot")
474         }
475         if pagesPerArena%pagesPerReclaimerChunk != 0 {
476                 print("pagesPerArena (", pagesPerArena, ") is not divisible by pagesPerReclaimerChunk (", pagesPerReclaimerChunk, ")\n")
477                 throw("bad pagesPerReclaimerChunk")
478         }
479
480         // Initialize the heap.
481         mheap_.init()
482         mcache0 = allocmcache()
483         lockInit(&gcBitsArenas.lock, lockRankGcBitsArenas)
484         lockInit(&proflock, lockRankProf)
485         lockInit(&globalAlloc.mutex, lockRankGlobalAlloc)
486
487         // Create initial arena growth hints.
488         if goarch.PtrSize == 8 {
489                 // On a 64-bit machine, we pick the following hints
490                 // because:
491                 //
492                 // 1. Starting from the middle of the address space
493                 // makes it easier to grow out a contiguous range
494                 // without running in to some other mapping.
495                 //
496                 // 2. This makes Go heap addresses more easily
497                 // recognizable when debugging.
498                 //
499                 // 3. Stack scanning in gccgo is still conservative,
500                 // so it's important that addresses be distinguishable
501                 // from other data.
502                 //
503                 // Starting at 0x00c0 means that the valid memory addresses
504                 // will begin 0x00c0, 0x00c1, ...
505                 // In little-endian, that's c0 00, c1 00, ... None of those are valid
506                 // UTF-8 sequences, and they are otherwise as far away from
507                 // ff (likely a common byte) as possible. If that fails, we try other 0xXXc0
508                 // addresses. An earlier attempt to use 0x11f8 caused out of memory errors
509                 // on OS X during thread allocations.  0x00c0 causes conflicts with
510                 // AddressSanitizer which reserves all memory up to 0x0100.
511                 // These choices reduce the odds of a conservative garbage collector
512                 // not collecting memory because some non-pointer block of memory
513                 // had a bit pattern that matched a memory address.
514                 //
515                 // However, on arm64, we ignore all this advice above and slam the
516                 // allocation at 0x40 << 32 because when using 4k pages with 3-level
517                 // translation buffers, the user address space is limited to 39 bits
518                 // On ios/arm64, the address space is even smaller.
519                 //
520                 // On AIX, mmaps starts at 0x0A00000000000000 for 64-bit.
521                 // processes.
522                 for i := 0x7f; i >= 0; i-- {
523                         var p uintptr
524                         switch {
525                         case raceenabled:
526                                 // The TSAN runtime requires the heap
527                                 // to be in the range [0x00c000000000,
528                                 // 0x00e000000000).
529                                 p = uintptr(i)<<32 | uintptrMask&(0x00c0<<32)
530                                 if p >= uintptrMask&0x00e000000000 {
531                                         continue
532                                 }
533                         case GOARCH == "arm64" && GOOS == "ios":
534                                 p = uintptr(i)<<40 | uintptrMask&(0x0013<<28)
535                         case GOARCH == "arm64":
536                                 p = uintptr(i)<<40 | uintptrMask&(0x0040<<32)
537                         case GOOS == "aix":
538                                 if i == 0 {
539                                         // We don't use addresses directly after 0x0A00000000000000
540                                         // to avoid collisions with others mmaps done by non-go programs.
541                                         continue
542                                 }
543                                 p = uintptr(i)<<40 | uintptrMask&(0xa0<<52)
544                         default:
545                                 p = uintptr(i)<<40 | uintptrMask&(0x00c0<<32)
546                         }
547                         hint := (*arenaHint)(mheap_.arenaHintAlloc.alloc())
548                         hint.addr = p
549                         hint.next, mheap_.arenaHints = mheap_.arenaHints, hint
550                 }
551         } else {
552                 // On a 32-bit machine, we're much more concerned
553                 // about keeping the usable heap contiguous.
554                 // Hence:
555                 //
556                 // 1. We reserve space for all heapArenas up front so
557                 // they don't get interleaved with the heap. They're
558                 // ~258MB, so this isn't too bad. (We could reserve a
559                 // smaller amount of space up front if this is a
560                 // problem.)
561                 //
562                 // 2. We hint the heap to start right above the end of
563                 // the binary so we have the best chance of keeping it
564                 // contiguous.
565                 //
566                 // 3. We try to stake out a reasonably large initial
567                 // heap reservation.
568
569                 const arenaMetaSize = (1 << arenaBits) * unsafe.Sizeof(heapArena{})
570                 meta := uintptr(sysReserve(nil, arenaMetaSize))
571                 if meta != 0 {
572                         mheap_.heapArenaAlloc.init(meta, arenaMetaSize, true)
573                 }
574
575                 // We want to start the arena low, but if we're linked
576                 // against C code, it's possible global constructors
577                 // have called malloc and adjusted the process' brk.
578                 // Query the brk so we can avoid trying to map the
579                 // region over it (which will cause the kernel to put
580                 // the region somewhere else, likely at a high
581                 // address).
582                 procBrk := sbrk0()
583
584                 // If we ask for the end of the data segment but the
585                 // operating system requires a little more space
586                 // before we can start allocating, it will give out a
587                 // slightly higher pointer. Except QEMU, which is
588                 // buggy, as usual: it won't adjust the pointer
589                 // upward. So adjust it upward a little bit ourselves:
590                 // 1/4 MB to get away from the running binary image.
591                 p := firstmoduledata.end
592                 if p < procBrk {
593                         p = procBrk
594                 }
595                 if mheap_.heapArenaAlloc.next <= p && p < mheap_.heapArenaAlloc.end {
596                         p = mheap_.heapArenaAlloc.end
597                 }
598                 p = alignUp(p+(256<<10), heapArenaBytes)
599                 // Because we're worried about fragmentation on
600                 // 32-bit, we try to make a large initial reservation.
601                 arenaSizes := []uintptr{
602                         512 << 20,
603                         256 << 20,
604                         128 << 20,
605                 }
606                 for _, arenaSize := range arenaSizes {
607                         a, size := sysReserveAligned(unsafe.Pointer(p), arenaSize, heapArenaBytes)
608                         if a != nil {
609                                 mheap_.arena.init(uintptr(a), size, false)
610                                 p = mheap_.arena.end // For hint below
611                                 break
612                         }
613                 }
614                 hint := (*arenaHint)(mheap_.arenaHintAlloc.alloc())
615                 hint.addr = p
616                 hint.next, mheap_.arenaHints = mheap_.arenaHints, hint
617         }
618 }
619
620 // sysAlloc allocates heap arena space for at least n bytes. The
621 // returned pointer is always heapArenaBytes-aligned and backed by
622 // h.arenas metadata. The returned size is always a multiple of
623 // heapArenaBytes. sysAlloc returns nil on failure.
624 // There is no corresponding free function.
625 //
626 // sysAlloc returns a memory region in the Reserved state. This region must
627 // be transitioned to Prepared and then Ready before use.
628 //
629 // h must be locked.
630 func (h *mheap) sysAlloc(n uintptr) (v unsafe.Pointer, size uintptr) {
631         assertLockHeld(&h.lock)
632
633         n = alignUp(n, heapArenaBytes)
634
635         // First, try the arena pre-reservation.
636         v = h.arena.alloc(n, heapArenaBytes, &memstats.heap_sys)
637         if v != nil {
638                 size = n
639                 goto mapped
640         }
641
642         // Try to grow the heap at a hint address.
643         for h.arenaHints != nil {
644                 hint := h.arenaHints
645                 p := hint.addr
646                 if hint.down {
647                         p -= n
648                 }
649                 if p+n < p {
650                         // We can't use this, so don't ask.
651                         v = nil
652                 } else if arenaIndex(p+n-1) >= 1<<arenaBits {
653                         // Outside addressable heap. Can't use.
654                         v = nil
655                 } else {
656                         v = sysReserve(unsafe.Pointer(p), n)
657                 }
658                 if p == uintptr(v) {
659                         // Success. Update the hint.
660                         if !hint.down {
661                                 p += n
662                         }
663                         hint.addr = p
664                         size = n
665                         break
666                 }
667                 // Failed. Discard this hint and try the next.
668                 //
669                 // TODO: This would be cleaner if sysReserve could be
670                 // told to only return the requested address. In
671                 // particular, this is already how Windows behaves, so
672                 // it would simplify things there.
673                 if v != nil {
674                         sysFree(v, n, nil)
675                 }
676                 h.arenaHints = hint.next
677                 h.arenaHintAlloc.free(unsafe.Pointer(hint))
678         }
679
680         if size == 0 {
681                 if raceenabled {
682                         // The race detector assumes the heap lives in
683                         // [0x00c000000000, 0x00e000000000), but we
684                         // just ran out of hints in this region. Give
685                         // a nice failure.
686                         throw("too many address space collisions for -race mode")
687                 }
688
689                 // All of the hints failed, so we'll take any
690                 // (sufficiently aligned) address the kernel will give
691                 // us.
692                 v, size = sysReserveAligned(nil, n, heapArenaBytes)
693                 if v == nil {
694                         return nil, 0
695                 }
696
697                 // Create new hints for extending this region.
698                 hint := (*arenaHint)(h.arenaHintAlloc.alloc())
699                 hint.addr, hint.down = uintptr(v), true
700                 hint.next, mheap_.arenaHints = mheap_.arenaHints, hint
701                 hint = (*arenaHint)(h.arenaHintAlloc.alloc())
702                 hint.addr = uintptr(v) + size
703                 hint.next, mheap_.arenaHints = mheap_.arenaHints, hint
704         }
705
706         // Check for bad pointers or pointers we can't use.
707         {
708                 var bad string
709                 p := uintptr(v)
710                 if p+size < p {
711                         bad = "region exceeds uintptr range"
712                 } else if arenaIndex(p) >= 1<<arenaBits {
713                         bad = "base outside usable address space"
714                 } else if arenaIndex(p+size-1) >= 1<<arenaBits {
715                         bad = "end outside usable address space"
716                 }
717                 if bad != "" {
718                         // This should be impossible on most architectures,
719                         // but it would be really confusing to debug.
720                         print("runtime: memory allocated by OS [", hex(p), ", ", hex(p+size), ") not in usable address space: ", bad, "\n")
721                         throw("memory reservation exceeds address space limit")
722                 }
723         }
724
725         if uintptr(v)&(heapArenaBytes-1) != 0 {
726                 throw("misrounded allocation in sysAlloc")
727         }
728
729 mapped:
730         // Create arena metadata.
731         for ri := arenaIndex(uintptr(v)); ri <= arenaIndex(uintptr(v)+size-1); ri++ {
732                 l2 := h.arenas[ri.l1()]
733                 if l2 == nil {
734                         // Allocate an L2 arena map.
735                         l2 = (*[1 << arenaL2Bits]*heapArena)(persistentalloc(unsafe.Sizeof(*l2), goarch.PtrSize, nil))
736                         if l2 == nil {
737                                 throw("out of memory allocating heap arena map")
738                         }
739                         atomic.StorepNoWB(unsafe.Pointer(&h.arenas[ri.l1()]), unsafe.Pointer(l2))
740                 }
741
742                 if l2[ri.l2()] != nil {
743                         throw("arena already initialized")
744                 }
745                 var r *heapArena
746                 r = (*heapArena)(h.heapArenaAlloc.alloc(unsafe.Sizeof(*r), goarch.PtrSize, &memstats.gcMiscSys))
747                 if r == nil {
748                         r = (*heapArena)(persistentalloc(unsafe.Sizeof(*r), goarch.PtrSize, &memstats.gcMiscSys))
749                         if r == nil {
750                                 throw("out of memory allocating heap arena metadata")
751                         }
752                 }
753
754                 // Add the arena to the arenas list.
755                 if len(h.allArenas) == cap(h.allArenas) {
756                         size := 2 * uintptr(cap(h.allArenas)) * goarch.PtrSize
757                         if size == 0 {
758                                 size = physPageSize
759                         }
760                         newArray := (*notInHeap)(persistentalloc(size, goarch.PtrSize, &memstats.gcMiscSys))
761                         if newArray == nil {
762                                 throw("out of memory allocating allArenas")
763                         }
764                         oldSlice := h.allArenas
765                         *(*notInHeapSlice)(unsafe.Pointer(&h.allArenas)) = notInHeapSlice{newArray, len(h.allArenas), int(size / goarch.PtrSize)}
766                         copy(h.allArenas, oldSlice)
767                         // Do not free the old backing array because
768                         // there may be concurrent readers. Since we
769                         // double the array each time, this can lead
770                         // to at most 2x waste.
771                 }
772                 h.allArenas = h.allArenas[:len(h.allArenas)+1]
773                 h.allArenas[len(h.allArenas)-1] = ri
774
775                 // Store atomically just in case an object from the
776                 // new heap arena becomes visible before the heap lock
777                 // is released (which shouldn't happen, but there's
778                 // little downside to this).
779                 atomic.StorepNoWB(unsafe.Pointer(&l2[ri.l2()]), unsafe.Pointer(r))
780         }
781
782         // Tell the race detector about the new heap memory.
783         if raceenabled {
784                 racemapshadow(v, size)
785         }
786
787         return
788 }
789
790 // sysReserveAligned is like sysReserve, but the returned pointer is
791 // aligned to align bytes. It may reserve either n or n+align bytes,
792 // so it returns the size that was reserved.
793 func sysReserveAligned(v unsafe.Pointer, size, align uintptr) (unsafe.Pointer, uintptr) {
794         // Since the alignment is rather large in uses of this
795         // function, we're not likely to get it by chance, so we ask
796         // for a larger region and remove the parts we don't need.
797         retries := 0
798 retry:
799         p := uintptr(sysReserve(v, size+align))
800         switch {
801         case p == 0:
802                 return nil, 0
803         case p&(align-1) == 0:
804                 // We got lucky and got an aligned region, so we can
805                 // use the whole thing.
806                 return unsafe.Pointer(p), size + align
807         case GOOS == "windows":
808                 // On Windows we can't release pieces of a
809                 // reservation, so we release the whole thing and
810                 // re-reserve the aligned sub-region. This may race,
811                 // so we may have to try again.
812                 sysFree(unsafe.Pointer(p), size+align, nil)
813                 p = alignUp(p, align)
814                 p2 := sysReserve(unsafe.Pointer(p), size)
815                 if p != uintptr(p2) {
816                         // Must have raced. Try again.
817                         sysFree(p2, size, nil)
818                         if retries++; retries == 100 {
819                                 throw("failed to allocate aligned heap memory; too many retries")
820                         }
821                         goto retry
822                 }
823                 // Success.
824                 return p2, size
825         default:
826                 // Trim off the unaligned parts.
827                 pAligned := alignUp(p, align)
828                 sysFree(unsafe.Pointer(p), pAligned-p, nil)
829                 end := pAligned + size
830                 endLen := (p + size + align) - end
831                 if endLen > 0 {
832                         sysFree(unsafe.Pointer(end), endLen, nil)
833                 }
834                 return unsafe.Pointer(pAligned), size
835         }
836 }
837
838 // base address for all 0-byte allocations
839 var zerobase uintptr
840
841 // nextFreeFast returns the next free object if one is quickly available.
842 // Otherwise it returns 0.
843 func nextFreeFast(s *mspan) gclinkptr {
844         theBit := sys.Ctz64(s.allocCache) // Is there a free object in the allocCache?
845         if theBit < 64 {
846                 result := s.freeindex + uintptr(theBit)
847                 if result < s.nelems {
848                         freeidx := result + 1
849                         if freeidx%64 == 0 && freeidx != s.nelems {
850                                 return 0
851                         }
852                         s.allocCache >>= uint(theBit + 1)
853                         s.freeindex = freeidx
854                         s.allocCount++
855                         return gclinkptr(result*s.elemsize + s.base())
856                 }
857         }
858         return 0
859 }
860
861 // nextFree returns the next free object from the cached span if one is available.
862 // Otherwise it refills the cache with a span with an available object and
863 // returns that object along with a flag indicating that this was a heavy
864 // weight allocation. If it is a heavy weight allocation the caller must
865 // determine whether a new GC cycle needs to be started or if the GC is active
866 // whether this goroutine needs to assist the GC.
867 //
868 // Must run in a non-preemptible context since otherwise the owner of
869 // c could change.
870 func (c *mcache) nextFree(spc spanClass) (v gclinkptr, s *mspan, shouldhelpgc bool) {
871         s = c.alloc[spc]
872         shouldhelpgc = false
873         freeIndex := s.nextFreeIndex()
874         if freeIndex == s.nelems {
875                 // The span is full.
876                 if uintptr(s.allocCount) != s.nelems {
877                         println("runtime: s.allocCount=", s.allocCount, "s.nelems=", s.nelems)
878                         throw("s.allocCount != s.nelems && freeIndex == s.nelems")
879                 }
880                 c.refill(spc)
881                 shouldhelpgc = true
882                 s = c.alloc[spc]
883
884                 freeIndex = s.nextFreeIndex()
885         }
886
887         if freeIndex >= s.nelems {
888                 throw("freeIndex is not valid")
889         }
890
891         v = gclinkptr(freeIndex*s.elemsize + s.base())
892         s.allocCount++
893         if uintptr(s.allocCount) > s.nelems {
894                 println("s.allocCount=", s.allocCount, "s.nelems=", s.nelems)
895                 throw("s.allocCount > s.nelems")
896         }
897         return
898 }
899
900 // Allocate an object of size bytes.
901 // Small objects are allocated from the per-P cache's free lists.
902 // Large objects (> 32 kB) are allocated straight from the heap.
903 func mallocgc(size uintptr, typ *_type, needzero bool) unsafe.Pointer {
904         if gcphase == _GCmarktermination {
905                 throw("mallocgc called with gcphase == _GCmarktermination")
906         }
907
908         if size == 0 {
909                 return unsafe.Pointer(&zerobase)
910         }
911         userSize := size
912         if asanenabled {
913                 // Refer to ASAN runtime library, the malloc() function allocates extra memory,
914                 // the redzone, around the user requested memory region. And the redzones are marked
915                 // as unaddressable. We perform the same operations in Go to detect the overflows or
916                 // underflows.
917                 size += computeRZlog(size)
918         }
919
920         if debug.malloc {
921                 if debug.sbrk != 0 {
922                         align := uintptr(16)
923                         if typ != nil {
924                                 // TODO(austin): This should be just
925                                 //   align = uintptr(typ.align)
926                                 // but that's only 4 on 32-bit platforms,
927                                 // even if there's a uint64 field in typ (see #599).
928                                 // This causes 64-bit atomic accesses to panic.
929                                 // Hence, we use stricter alignment that matches
930                                 // the normal allocator better.
931                                 if size&7 == 0 {
932                                         align = 8
933                                 } else if size&3 == 0 {
934                                         align = 4
935                                 } else if size&1 == 0 {
936                                         align = 2
937                                 } else {
938                                         align = 1
939                                 }
940                         }
941                         return persistentalloc(size, align, &memstats.other_sys)
942                 }
943
944                 if inittrace.active && inittrace.id == getg().goid {
945                         // Init functions are executed sequentially in a single goroutine.
946                         inittrace.allocs += 1
947                 }
948         }
949
950         // assistG is the G to charge for this allocation, or nil if
951         // GC is not currently active.
952         var assistG *g
953         if gcBlackenEnabled != 0 {
954                 // Charge the current user G for this allocation.
955                 assistG = getg()
956                 if assistG.m.curg != nil {
957                         assistG = assistG.m.curg
958                 }
959                 // Charge the allocation against the G. We'll account
960                 // for internal fragmentation at the end of mallocgc.
961                 assistG.gcAssistBytes -= int64(size)
962
963                 if assistG.gcAssistBytes < 0 {
964                         // This G is in debt. Assist the GC to correct
965                         // this before allocating. This must happen
966                         // before disabling preemption.
967                         gcAssistAlloc(assistG)
968                 }
969         }
970
971         // Set mp.mallocing to keep from being preempted by GC.
972         mp := acquirem()
973         if mp.mallocing != 0 {
974                 throw("malloc deadlock")
975         }
976         if mp.gsignal == getg() {
977                 throw("malloc during signal")
978         }
979         mp.mallocing = 1
980
981         shouldhelpgc := false
982         dataSize := userSize
983         c := getMCache(mp)
984         if c == nil {
985                 throw("mallocgc called without a P or outside bootstrapping")
986         }
987         var span *mspan
988         var x unsafe.Pointer
989         noscan := typ == nil || typ.ptrdata == 0
990         // In some cases block zeroing can profitably (for latency reduction purposes)
991         // be delayed till preemption is possible; delayedZeroing tracks that state.
992         delayedZeroing := false
993         if size <= maxSmallSize {
994                 if noscan && size < maxTinySize {
995                         // Tiny allocator.
996                         //
997                         // Tiny allocator combines several tiny allocation requests
998                         // into a single memory block. The resulting memory block
999                         // is freed when all subobjects are unreachable. The subobjects
1000                         // must be noscan (don't have pointers), this ensures that
1001                         // the amount of potentially wasted memory is bounded.
1002                         //
1003                         // Size of the memory block used for combining (maxTinySize) is tunable.
1004                         // Current setting is 16 bytes, which relates to 2x worst case memory
1005                         // wastage (when all but one subobjects are unreachable).
1006                         // 8 bytes would result in no wastage at all, but provides less
1007                         // opportunities for combining.
1008                         // 32 bytes provides more opportunities for combining,
1009                         // but can lead to 4x worst case wastage.
1010                         // The best case winning is 8x regardless of block size.
1011                         //
1012                         // Objects obtained from tiny allocator must not be freed explicitly.
1013                         // So when an object will be freed explicitly, we ensure that
1014                         // its size >= maxTinySize.
1015                         //
1016                         // SetFinalizer has a special case for objects potentially coming
1017                         // from tiny allocator, it such case it allows to set finalizers
1018                         // for an inner byte of a memory block.
1019                         //
1020                         // The main targets of tiny allocator are small strings and
1021                         // standalone escaping variables. On a json benchmark
1022                         // the allocator reduces number of allocations by ~12% and
1023                         // reduces heap size by ~20%.
1024                         off := c.tinyoffset
1025                         // Align tiny pointer for required (conservative) alignment.
1026                         if size&7 == 0 {
1027                                 off = alignUp(off, 8)
1028                         } else if goarch.PtrSize == 4 && size == 12 {
1029                                 // Conservatively align 12-byte objects to 8 bytes on 32-bit
1030                                 // systems so that objects whose first field is a 64-bit
1031                                 // value is aligned to 8 bytes and does not cause a fault on
1032                                 // atomic access. See issue 37262.
1033                                 // TODO(mknyszek): Remove this workaround if/when issue 36606
1034                                 // is resolved.
1035                                 off = alignUp(off, 8)
1036                         } else if size&3 == 0 {
1037                                 off = alignUp(off, 4)
1038                         } else if size&1 == 0 {
1039                                 off = alignUp(off, 2)
1040                         }
1041                         if off+size <= maxTinySize && c.tiny != 0 {
1042                                 // The object fits into existing tiny block.
1043                                 x = unsafe.Pointer(c.tiny + off)
1044                                 c.tinyoffset = off + size
1045                                 c.tinyAllocs++
1046                                 mp.mallocing = 0
1047                                 releasem(mp)
1048                                 return x
1049                         }
1050                         // Allocate a new maxTinySize block.
1051                         span = c.alloc[tinySpanClass]
1052                         v := nextFreeFast(span)
1053                         if v == 0 {
1054                                 v, span, shouldhelpgc = c.nextFree(tinySpanClass)
1055                         }
1056                         x = unsafe.Pointer(v)
1057                         (*[2]uint64)(x)[0] = 0
1058                         (*[2]uint64)(x)[1] = 0
1059                         // See if we need to replace the existing tiny block with the new one
1060                         // based on amount of remaining free space.
1061                         if !raceenabled && (size < c.tinyoffset || c.tiny == 0) {
1062                                 // Note: disabled when race detector is on, see comment near end of this function.
1063                                 c.tiny = uintptr(x)
1064                                 c.tinyoffset = size
1065                         }
1066                         size = maxTinySize
1067                 } else {
1068                         var sizeclass uint8
1069                         if size <= smallSizeMax-8 {
1070                                 sizeclass = size_to_class8[divRoundUp(size, smallSizeDiv)]
1071                         } else {
1072                                 sizeclass = size_to_class128[divRoundUp(size-smallSizeMax, largeSizeDiv)]
1073                         }
1074                         size = uintptr(class_to_size[sizeclass])
1075                         spc := makeSpanClass(sizeclass, noscan)
1076                         span = c.alloc[spc]
1077                         v := nextFreeFast(span)
1078                         if v == 0 {
1079                                 v, span, shouldhelpgc = c.nextFree(spc)
1080                         }
1081                         x = unsafe.Pointer(v)
1082                         if needzero && span.needzero != 0 {
1083                                 memclrNoHeapPointers(unsafe.Pointer(v), size)
1084                         }
1085                 }
1086         } else {
1087                 shouldhelpgc = true
1088                 // For large allocations, keep track of zeroed state so that
1089                 // bulk zeroing can be happen later in a preemptible context.
1090                 span = c.allocLarge(size, noscan)
1091                 span.freeindex = 1
1092                 span.allocCount = 1
1093                 size = span.elemsize
1094                 x = unsafe.Pointer(span.base())
1095                 if needzero && span.needzero != 0 {
1096                         if noscan {
1097                                 delayedZeroing = true
1098                         } else {
1099                                 memclrNoHeapPointers(x, size)
1100                                 // We've in theory cleared almost the whole span here,
1101                                 // and could take the extra step of actually clearing
1102                                 // the whole thing. However, don't. Any GC bits for the
1103                                 // uncleared parts will be zero, and it's just going to
1104                                 // be needzero = 1 once freed anyway.
1105                         }
1106                 }
1107         }
1108
1109         var scanSize uintptr
1110         if !noscan {
1111                 heapBitsSetType(uintptr(x), size, dataSize, typ)
1112                 if dataSize > typ.size {
1113                         // Array allocation. If there are any
1114                         // pointers, GC has to scan to the last
1115                         // element.
1116                         if typ.ptrdata != 0 {
1117                                 scanSize = dataSize - typ.size + typ.ptrdata
1118                         }
1119                 } else {
1120                         scanSize = typ.ptrdata
1121                 }
1122                 c.scanAlloc += scanSize
1123         }
1124
1125         // Ensure that the stores above that initialize x to
1126         // type-safe memory and set the heap bits occur before
1127         // the caller can make x observable to the garbage
1128         // collector. Otherwise, on weakly ordered machines,
1129         // the garbage collector could follow a pointer to x,
1130         // but see uninitialized memory or stale heap bits.
1131         publicationBarrier()
1132
1133         // Allocate black during GC.
1134         // All slots hold nil so no scanning is needed.
1135         // This may be racing with GC so do it atomically if there can be
1136         // a race marking the bit.
1137         if gcphase != _GCoff {
1138                 gcmarknewobject(span, uintptr(x), size, scanSize)
1139         }
1140
1141         if raceenabled {
1142                 racemalloc(x, size)
1143         }
1144
1145         if msanenabled {
1146                 msanmalloc(x, size)
1147         }
1148
1149         if asanenabled {
1150                 // We should only read/write the memory with the size asked by the user.
1151                 // The rest of the allocated memory should be poisoned, so that we can report
1152                 // errors when accessing poisoned memory.
1153                 // The allocated memory is larger than required userSize, it will also include
1154                 // redzone and some other padding bytes.
1155                 rzBeg := unsafe.Add(x, userSize)
1156                 asanpoison(rzBeg, size-userSize)
1157                 asanunpoison(x, userSize)
1158         }
1159
1160         if rate := MemProfileRate; rate > 0 {
1161                 // Note cache c only valid while m acquired; see #47302
1162                 if rate != 1 && size < c.nextSample {
1163                         c.nextSample -= size
1164                 } else {
1165                         profilealloc(mp, x, size)
1166                 }
1167         }
1168         mp.mallocing = 0
1169         releasem(mp)
1170
1171         // Pointerfree data can be zeroed late in a context where preemption can occur.
1172         // x will keep the memory alive.
1173         if delayedZeroing {
1174                 if !noscan {
1175                         throw("delayed zeroing on data that may contain pointers")
1176                 }
1177                 memclrNoHeapPointersChunked(size, x) // This is a possible preemption point: see #47302
1178         }
1179
1180         if debug.malloc {
1181                 if debug.allocfreetrace != 0 {
1182                         tracealloc(x, size, typ)
1183                 }
1184
1185                 if inittrace.active && inittrace.id == getg().goid {
1186                         // Init functions are executed sequentially in a single goroutine.
1187                         inittrace.bytes += uint64(size)
1188                 }
1189         }
1190
1191         if assistG != nil {
1192                 // Account for internal fragmentation in the assist
1193                 // debt now that we know it.
1194                 assistG.gcAssistBytes -= int64(size - dataSize)
1195         }
1196
1197         if shouldhelpgc {
1198                 if t := (gcTrigger{kind: gcTriggerHeap}); t.test() {
1199                         gcStart(t)
1200                 }
1201         }
1202
1203         if raceenabled && noscan && dataSize < maxTinySize {
1204                 // Pad tinysize allocations so they are aligned with the end
1205                 // of the tinyalloc region. This ensures that any arithmetic
1206                 // that goes off the top end of the object will be detectable
1207                 // by checkptr (issue 38872).
1208                 // Note that we disable tinyalloc when raceenabled for this to work.
1209                 // TODO: This padding is only performed when the race detector
1210                 // is enabled. It would be nice to enable it if any package
1211                 // was compiled with checkptr, but there's no easy way to
1212                 // detect that (especially at compile time).
1213                 // TODO: enable this padding for all allocations, not just
1214                 // tinyalloc ones. It's tricky because of pointer maps.
1215                 // Maybe just all noscan objects?
1216                 x = add(x, size-dataSize)
1217         }
1218
1219         return x
1220 }
1221
1222 // memclrNoHeapPointersChunked repeatedly calls memclrNoHeapPointers
1223 // on chunks of the buffer to be zeroed, with opportunities for preemption
1224 // along the way.  memclrNoHeapPointers contains no safepoints and also
1225 // cannot be preemptively scheduled, so this provides a still-efficient
1226 // block copy that can also be preempted on a reasonable granularity.
1227 //
1228 // Use this with care; if the data being cleared is tagged to contain
1229 // pointers, this allows the GC to run before it is all cleared.
1230 func memclrNoHeapPointersChunked(size uintptr, x unsafe.Pointer) {
1231         v := uintptr(x)
1232         // got this from benchmarking. 128k is too small, 512k is too large.
1233         const chunkBytes = 256 * 1024
1234         vsize := v + size
1235         for voff := v; voff < vsize; voff = voff + chunkBytes {
1236                 if getg().preempt {
1237                         // may hold locks, e.g., profiling
1238                         goschedguarded()
1239                 }
1240                 // clear min(avail, lump) bytes
1241                 n := vsize - voff
1242                 if n > chunkBytes {
1243                         n = chunkBytes
1244                 }
1245                 memclrNoHeapPointers(unsafe.Pointer(voff), n)
1246         }
1247 }
1248
1249 // implementation of new builtin
1250 // compiler (both frontend and SSA backend) knows the signature
1251 // of this function
1252 func newobject(typ *_type) unsafe.Pointer {
1253         return mallocgc(typ.size, typ, true)
1254 }
1255
1256 //go:linkname reflect_unsafe_New reflect.unsafe_New
1257 func reflect_unsafe_New(typ *_type) unsafe.Pointer {
1258         return mallocgc(typ.size, typ, true)
1259 }
1260
1261 //go:linkname reflectlite_unsafe_New internal/reflectlite.unsafe_New
1262 func reflectlite_unsafe_New(typ *_type) unsafe.Pointer {
1263         return mallocgc(typ.size, typ, true)
1264 }
1265
1266 // newarray allocates an array of n elements of type typ.
1267 func newarray(typ *_type, n int) unsafe.Pointer {
1268         if n == 1 {
1269                 return mallocgc(typ.size, typ, true)
1270         }
1271         mem, overflow := math.MulUintptr(typ.size, uintptr(n))
1272         if overflow || mem > maxAlloc || n < 0 {
1273                 panic(plainError("runtime: allocation size out of range"))
1274         }
1275         return mallocgc(mem, typ, true)
1276 }
1277
1278 //go:linkname reflect_unsafe_NewArray reflect.unsafe_NewArray
1279 func reflect_unsafe_NewArray(typ *_type, n int) unsafe.Pointer {
1280         return newarray(typ, n)
1281 }
1282
1283 func profilealloc(mp *m, x unsafe.Pointer, size uintptr) {
1284         c := getMCache(mp)
1285         if c == nil {
1286                 throw("profilealloc called without a P or outside bootstrapping")
1287         }
1288         c.nextSample = nextSample()
1289         mProf_Malloc(x, size)
1290 }
1291
1292 // nextSample returns the next sampling point for heap profiling. The goal is
1293 // to sample allocations on average every MemProfileRate bytes, but with a
1294 // completely random distribution over the allocation timeline; this
1295 // corresponds to a Poisson process with parameter MemProfileRate. In Poisson
1296 // processes, the distance between two samples follows the exponential
1297 // distribution (exp(MemProfileRate)), so the best return value is a random
1298 // number taken from an exponential distribution whose mean is MemProfileRate.
1299 func nextSample() uintptr {
1300         if MemProfileRate == 1 {
1301                 // Callers assign our return value to
1302                 // mcache.next_sample, but next_sample is not used
1303                 // when the rate is 1. So avoid the math below and
1304                 // just return something.
1305                 return 0
1306         }
1307         if GOOS == "plan9" {
1308                 // Plan 9 doesn't support floating point in note handler.
1309                 if g := getg(); g == g.m.gsignal {
1310                         return nextSampleNoFP()
1311                 }
1312         }
1313
1314         return uintptr(fastexprand(MemProfileRate))
1315 }
1316
1317 // fastexprand returns a random number from an exponential distribution with
1318 // the specified mean.
1319 func fastexprand(mean int) int32 {
1320         // Avoid overflow. Maximum possible step is
1321         // -ln(1/(1<<randomBitCount)) * mean, approximately 20 * mean.
1322         switch {
1323         case mean > 0x7000000:
1324                 mean = 0x7000000
1325         case mean == 0:
1326                 return 0
1327         }
1328
1329         // Take a random sample of the exponential distribution exp(-mean*x).
1330         // The probability distribution function is mean*exp(-mean*x), so the CDF is
1331         // p = 1 - exp(-mean*x), so
1332         // q = 1 - p == exp(-mean*x)
1333         // log_e(q) = -mean*x
1334         // -log_e(q)/mean = x
1335         // x = -log_e(q) * mean
1336         // x = log_2(q) * (-log_e(2)) * mean    ; Using log_2 for efficiency
1337         const randomBitCount = 26
1338         q := fastrandn(1<<randomBitCount) + 1
1339         qlog := fastlog2(float64(q)) - randomBitCount
1340         if qlog > 0 {
1341                 qlog = 0
1342         }
1343         const minusLog2 = -0.6931471805599453 // -ln(2)
1344         return int32(qlog*(minusLog2*float64(mean))) + 1
1345 }
1346
1347 // nextSampleNoFP is similar to nextSample, but uses older,
1348 // simpler code to avoid floating point.
1349 func nextSampleNoFP() uintptr {
1350         // Set first allocation sample size.
1351         rate := MemProfileRate
1352         if rate > 0x3fffffff { // make 2*rate not overflow
1353                 rate = 0x3fffffff
1354         }
1355         if rate != 0 {
1356                 return uintptr(fastrandn(uint32(2 * rate)))
1357         }
1358         return 0
1359 }
1360
1361 type persistentAlloc struct {
1362         base *notInHeap
1363         off  uintptr
1364 }
1365
1366 var globalAlloc struct {
1367         mutex
1368         persistentAlloc
1369 }
1370
1371 // persistentChunkSize is the number of bytes we allocate when we grow
1372 // a persistentAlloc.
1373 const persistentChunkSize = 256 << 10
1374
1375 // persistentChunks is a list of all the persistent chunks we have
1376 // allocated. The list is maintained through the first word in the
1377 // persistent chunk. This is updated atomically.
1378 var persistentChunks *notInHeap
1379
1380 // Wrapper around sysAlloc that can allocate small chunks.
1381 // There is no associated free operation.
1382 // Intended for things like function/type/debug-related persistent data.
1383 // If align is 0, uses default align (currently 8).
1384 // The returned memory will be zeroed.
1385 //
1386 // Consider marking persistentalloc'd types go:notinheap.
1387 func persistentalloc(size, align uintptr, sysStat *sysMemStat) unsafe.Pointer {
1388         var p *notInHeap
1389         systemstack(func() {
1390                 p = persistentalloc1(size, align, sysStat)
1391         })
1392         return unsafe.Pointer(p)
1393 }
1394
1395 // Must run on system stack because stack growth can (re)invoke it.
1396 // See issue 9174.
1397 //go:systemstack
1398 func persistentalloc1(size, align uintptr, sysStat *sysMemStat) *notInHeap {
1399         const (
1400                 maxBlock = 64 << 10 // VM reservation granularity is 64K on windows
1401         )
1402
1403         if size == 0 {
1404                 throw("persistentalloc: size == 0")
1405         }
1406         if align != 0 {
1407                 if align&(align-1) != 0 {
1408                         throw("persistentalloc: align is not a power of 2")
1409                 }
1410                 if align > _PageSize {
1411                         throw("persistentalloc: align is too large")
1412                 }
1413         } else {
1414                 align = 8
1415         }
1416
1417         if size >= maxBlock {
1418                 return (*notInHeap)(sysAlloc(size, sysStat))
1419         }
1420
1421         mp := acquirem()
1422         var persistent *persistentAlloc
1423         if mp != nil && mp.p != 0 {
1424                 persistent = &mp.p.ptr().palloc
1425         } else {
1426                 lock(&globalAlloc.mutex)
1427                 persistent = &globalAlloc.persistentAlloc
1428         }
1429         persistent.off = alignUp(persistent.off, align)
1430         if persistent.off+size > persistentChunkSize || persistent.base == nil {
1431                 persistent.base = (*notInHeap)(sysAlloc(persistentChunkSize, &memstats.other_sys))
1432                 if persistent.base == nil {
1433                         if persistent == &globalAlloc.persistentAlloc {
1434                                 unlock(&globalAlloc.mutex)
1435                         }
1436                         throw("runtime: cannot allocate memory")
1437                 }
1438
1439                 // Add the new chunk to the persistentChunks list.
1440                 for {
1441                         chunks := uintptr(unsafe.Pointer(persistentChunks))
1442                         *(*uintptr)(unsafe.Pointer(persistent.base)) = chunks
1443                         if atomic.Casuintptr((*uintptr)(unsafe.Pointer(&persistentChunks)), chunks, uintptr(unsafe.Pointer(persistent.base))) {
1444                                 break
1445                         }
1446                 }
1447                 persistent.off = alignUp(goarch.PtrSize, align)
1448         }
1449         p := persistent.base.add(persistent.off)
1450         persistent.off += size
1451         releasem(mp)
1452         if persistent == &globalAlloc.persistentAlloc {
1453                 unlock(&globalAlloc.mutex)
1454         }
1455
1456         if sysStat != &memstats.other_sys {
1457                 sysStat.add(int64(size))
1458                 memstats.other_sys.add(-int64(size))
1459         }
1460         return p
1461 }
1462
1463 // inPersistentAlloc reports whether p points to memory allocated by
1464 // persistentalloc. This must be nosplit because it is called by the
1465 // cgo checker code, which is called by the write barrier code.
1466 //go:nosplit
1467 func inPersistentAlloc(p uintptr) bool {
1468         chunk := atomic.Loaduintptr((*uintptr)(unsafe.Pointer(&persistentChunks)))
1469         for chunk != 0 {
1470                 if p >= chunk && p < chunk+persistentChunkSize {
1471                         return true
1472                 }
1473                 chunk = *(*uintptr)(unsafe.Pointer(chunk))
1474         }
1475         return false
1476 }
1477
1478 // linearAlloc is a simple linear allocator that pre-reserves a region
1479 // of memory and then optionally maps that region into the Ready state
1480 // as needed.
1481 //
1482 // The caller is responsible for locking.
1483 type linearAlloc struct {
1484         next   uintptr // next free byte
1485         mapped uintptr // one byte past end of mapped space
1486         end    uintptr // end of reserved space
1487
1488         mapMemory bool // transition memory from Reserved to Ready if true
1489 }
1490
1491 func (l *linearAlloc) init(base, size uintptr, mapMemory bool) {
1492         if base+size < base {
1493                 // Chop off the last byte. The runtime isn't prepared
1494                 // to deal with situations where the bounds could overflow.
1495                 // Leave that memory reserved, though, so we don't map it
1496                 // later.
1497                 size -= 1
1498         }
1499         l.next, l.mapped = base, base
1500         l.end = base + size
1501         l.mapMemory = mapMemory
1502 }
1503
1504 func (l *linearAlloc) alloc(size, align uintptr, sysStat *sysMemStat) unsafe.Pointer {
1505         p := alignUp(l.next, align)
1506         if p+size > l.end {
1507                 return nil
1508         }
1509         l.next = p + size
1510         if pEnd := alignUp(l.next-1, physPageSize); pEnd > l.mapped {
1511                 if l.mapMemory {
1512                         // Transition from Reserved to Prepared to Ready.
1513                         sysMap(unsafe.Pointer(l.mapped), pEnd-l.mapped, sysStat)
1514                         sysUsed(unsafe.Pointer(l.mapped), pEnd-l.mapped)
1515                 }
1516                 l.mapped = pEnd
1517         }
1518         return unsafe.Pointer(p)
1519 }
1520
1521 // notInHeap is off-heap memory allocated by a lower-level allocator
1522 // like sysAlloc or persistentAlloc.
1523 //
1524 // In general, it's better to use real types marked as go:notinheap,
1525 // but this serves as a generic type for situations where that isn't
1526 // possible (like in the allocators).
1527 //
1528 // TODO: Use this as the return type of sysAlloc, persistentAlloc, etc?
1529 //
1530 //go:notinheap
1531 type notInHeap struct{}
1532
1533 func (p *notInHeap) add(bytes uintptr) *notInHeap {
1534         return (*notInHeap)(unsafe.Pointer(uintptr(unsafe.Pointer(p)) + bytes))
1535 }
1536
1537 // computeRZlog computes the size of the redzone.
1538 // Refer to the implementation of the compiler-rt.
1539 func computeRZlog(userSize uintptr) uintptr {
1540         switch {
1541         case userSize <= (64 - 16):
1542                 return 16 << 0
1543         case userSize <= (128 - 32):
1544                 return 16 << 1
1545         case userSize <= (512 - 64):
1546                 return 16 << 2
1547         case userSize <= (4096 - 128):
1548                 return 16 << 3
1549         case userSize <= (1<<14)-256:
1550                 return 16 << 4
1551         case userSize <= (1<<15)-512:
1552                 return 16 << 5
1553         case userSize <= (1<<16)-1024:
1554                 return 16 << 6
1555         default:
1556                 return 16 << 7
1557         }
1558 }