Skip to content

Commit

Permalink
golang: limit size of allocations
Browse files Browse the repository at this point in the history
Don't allow to make allocations large than 64MB.

Signed-off-by: Andrei Vagin <[email protected]>
  • Loading branch information
avagin committed Nov 17, 2023
1 parent 2bc70b2 commit f3b882c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ go_download_sdk(
# time on our continuous integration.
patch = "//tools:go_types_memoize.patch",
patch_strip = 2,
version = "1.21.1",
version = "1.21.3",
)

gazelle_dependencies()
Expand Down
14 changes: 14 additions & 0 deletions tools/go_allocation_limit.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
diff --git a/src/runtime/malloc.go b/src/runtime/malloc.go
index 44479cc2be..adcee90992 100644
--- a/src/runtime/malloc.go
+++ b/src/runtime/malloc.go
@@ -946,6 +946,9 @@ func (c *mcache) nextFree(spc spanClass) (v gclinkptr, s *mspan, shouldhelpgc bo
// Small objects are allocated from the per-P cache's free lists.
// Large objects (> 32 kB) are allocated straight from the heap.
func mallocgc(size uintptr, typ *_type, needzero bool) unsafe.Pointer {
+ if size > (64 << 20) {
+ throw("large allocation")
+ }
if gcphase == _GCmarktermination {
throw("mallocgc called with gcphase == _GCmarktermination")
}
14 changes: 14 additions & 0 deletions tools/go_types_memoize.patch
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
diff --git a/src/runtime/malloc.go b/src/runtime/malloc.go
index 44479cc2be..adcee90992 100644
--- a/go/src/runtime/malloc.go
+++ b/go/src/runtime/malloc.go
@@ -946,6 +946,9 @@ func (c *mcache) nextFree(spc spanClass) (v gclinkptr, s *mspan, shouldhelpgc bo
// Small objects are allocated from the per-P cache's free lists.
// Large objects (> 32 kB) are allocated straight from the heap.
func mallocgc(size uintptr, typ *_type, needzero bool) unsafe.Pointer {
+ if size > (64 << 20) {
+ throw("large allocation")
+ }
if gcphase == _GCmarktermination {
throw("mallocgc called with gcphase == _GCmarktermination")
}
diff --git a/go/src/go/types/scope.go b/src/go/types/scope.go
index 010727eb72..0f134b872e 100644
--- a/go/src/go/types/scope.go
Expand Down

0 comments on commit f3b882c

Please sign in to comment.