diff options
author | ian <ian@138bc75d-0d04-0410-961f-82ee72b054a4> | 2012-09-28 21:25:20 +0000 |
---|---|---|
committer | ian <ian@138bc75d-0d04-0410-961f-82ee72b054a4> | 2012-09-28 21:25:20 +0000 |
commit | aef67bb4faf41195e234abd28ff916414e112c0c (patch) | |
tree | c312f5f6a11f323dc9f8d70c8f1604219de561bb /libgo | |
parent | 877584e4ea0ece6c88a22ccec3f2a4218c98212e (diff) | |
download | gcc-aef67bb4faf41195e234abd28ff916414e112c0c.tar.gz |
runtime: Better detection of memory allocation request overflow.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@191841 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'libgo')
-rw-r--r-- | libgo/runtime/chan.c | 4 | ||||
-rw-r--r-- | libgo/runtime/go-append.c | 3 | ||||
-rw-r--r-- | libgo/runtime/go-make-slice.c | 2 | ||||
-rw-r--r-- | libgo/runtime/malloc.h | 9 |
4 files changed, 16 insertions, 2 deletions
diff --git a/libgo/runtime/chan.c b/libgo/runtime/chan.c index c8ee10e8c88..d0a1612ad2a 100644 --- a/libgo/runtime/chan.c +++ b/libgo/runtime/chan.c @@ -3,6 +3,8 @@ // license that can be found in the LICENSE file. #include "runtime.h" +#include "arch.h" +#include "malloc.h" #include "go-type.h" #define NOSELGEN 1 @@ -88,7 +90,7 @@ runtime_makechan_c(ChanType *t, int64 hint) elem = t->__element_type; - if(hint < 0 || (int32)hint != hint || (elem->__size > 0 && (uintptr)hint > ((uintptr)-1) / elem->__size)) + if(hint < 0 || (int32)hint != hint || (elem->__size > 0 && (uintptr)hint > MaxMem / elem->__size)) runtime_panicstring("makechan: size out of range"); n = sizeof(*c); diff --git a/libgo/runtime/go-append.c b/libgo/runtime/go-append.c index 3a0c7781126..dac4c902c15 100644 --- a/libgo/runtime/go-append.c +++ b/libgo/runtime/go-append.c @@ -54,6 +54,9 @@ __go_append (struct __go_open_array a, void *bvalues, uintptr_t bcount, while (m < count); } + if ((uintptr) m > MaxMem / element_size) + runtime_panicstring ("growslice: cap out of range"); + n = __go_alloc (m * element_size); __builtin_memcpy (n, a.__values, a.__count * element_size); diff --git a/libgo/runtime/go-make-slice.c b/libgo/runtime/go-make-slice.c index 42b412c772b..822c9b68f0a 100644 --- a/libgo/runtime/go-make-slice.c +++ b/libgo/runtime/go-make-slice.c @@ -37,7 +37,7 @@ __go_make_slice2 (const struct __go_type_descriptor *td, uintptr_t len, if (cap < len || (uintptr_t) icap != cap || (std->__element_type->__size > 0 - && cap > (uintptr_t) -1U / std->__element_type->__size)) + && cap > MaxMem / std->__element_type->__size)) runtime_panicstring ("makeslice: cap out of range"); ret.__count = ilen; diff --git a/libgo/runtime/malloc.h b/libgo/runtime/malloc.h index 16bb449f128..96cb609367f 100644 --- a/libgo/runtime/malloc.h +++ b/libgo/runtime/malloc.h @@ -128,6 +128,15 @@ enum MaxGcproc = 4, }; +// Maximum memory allocation size, a hint for callers. +// This must be a #define instead of an enum because it +// is so large. +#if __SIZEOF_POINTER__ == 8 +#define MaxMem (16ULL<<30) /* 16 GB */ +#else +#define MaxMem ((uintptr)-1) +#endif + // A generic linked list of blocks. (Typically the block is bigger than sizeof(MLink).) struct MLink { |