summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2004-11-19 13:52:40 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2004-11-19 13:52:40 -0200
commit6f1ea817f5827523f8c7e429ab023e5984a84343 (patch)
tree778c20b8816afb4ee5cfc3b988ec1c31efbb7b75
parentcdcb236747a728e3ef0855aa16a42b73e7a9a6c6 (diff)
downloadlua-6f1ea817f5827523f8c7e429ab023e5984a84343.tar.gz
lua-6f1ea817f5827523f8c7e429ab023e5984a84343.tar.bz2
lua-6f1ea817f5827523f8c7e429ab023e5984a84343.zip
better control over memory-size overflows
-rw-r--r--lgc.c8
-rw-r--r--lmem.c21
-rw-r--r--lmem.h28
-rw-r--r--lstate.c4
-rw-r--r--lstring.c11
-rw-r--r--lstring.h7
6 files changed, 47 insertions, 32 deletions
diff --git a/lgc.c b/lgc.c
index a9a4f8cc..da93007d 100644
--- a/lgc.c
+++ b/lgc.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lgc.c,v 2.13 2004/10/06 18:34:16 roberto Exp roberto $ 2** $Id: lgc.c,v 2.14 2004/10/08 16:00:34 roberto Exp roberto $
3** Garbage Collector 3** Garbage Collector
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -139,7 +139,7 @@ size_t luaC_separateudata (lua_State *L, int all) {
139 p = &curr->gch.next; 139 p = &curr->gch.next;
140 } 140 }
141 else { /* must call its gc method */ 141 else { /* must call its gc method */
142 deadmem += sizeudata(gco2u(curr)->len); 142 deadmem += sizeudata(gco2u(curr));
143 markfinalized(gco2u(curr)); 143 markfinalized(gco2u(curr));
144 *p = curr->gch.next; 144 *p = curr->gch.next;
145 curr->gch.next = NULL; /* link `curr' at the end of `collected' list */ 145 curr->gch.next = NULL; /* link `curr' at the end of `collected' list */
@@ -388,11 +388,11 @@ static void freeobj (lua_State *L, GCObject *o) {
388 } 388 }
389 case LUA_TSTRING: { 389 case LUA_TSTRING: {
390 G(L)->strt.nuse--; 390 G(L)->strt.nuse--;
391 luaM_free(L, o, sizestring(gco2ts(o)->len)); 391 luaM_free(L, o, sizestring(gco2ts(o)));
392 break; 392 break;
393 } 393 }
394 case LUA_TUSERDATA: { 394 case LUA_TUSERDATA: {
395 luaM_free(L, o, sizeudata(gco2u(o)->len)); 395 luaM_free(L, o, sizeudata(gco2u(o)));
396 break; 396 break;
397 } 397 }
398 default: lua_assert(0); 398 default: lua_assert(0);
diff --git a/lmem.c b/lmem.c
index 5873e8dc..29252abe 100644
--- a/lmem.c
+++ b/lmem.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lmem.c,v 1.64 2004/04/30 20:13:38 roberto Exp roberto $ 2** $Id: lmem.c,v 1.65 2004/08/30 13:44:44 roberto Exp roberto $
3** Interface to Memory Manager 3** Interface to Memory Manager
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -43,10 +43,12 @@
43#define MINSIZEARRAY 4 43#define MINSIZEARRAY 4
44 44
45 45
46void *luaM_growaux (lua_State *L, void *block, int *size, int size_elems, 46void *luaM_growaux (lua_State *L, void *block, int *size, size_t size_elems,
47 int limit, const char *errormsg) { 47 int limit, const char *errormsg) {
48 void *newblock; 48 void *newblock;
49 int newsize; 49 int newsize;
50 if (cast(size_t, limit) > MAX_SIZET/size_elems)
51 limit = cast(int, MAX_SIZET/size_elems);
50 if (*size >= limit/2) { /* cannot double it? */ 52 if (*size >= limit/2) { /* cannot double it? */
51 if (*size >= limit - MINSIZEARRAY) /* try something smaller... */ 53 if (*size >= limit - MINSIZEARRAY) /* try something smaller... */
52 luaG_runerror(L, errormsg); 54 luaG_runerror(L, errormsg);
@@ -57,22 +59,25 @@ void *luaM_growaux (lua_State *L, void *block, int *size, int size_elems,
57 if (newsize < MINSIZEARRAY) 59 if (newsize < MINSIZEARRAY)
58 newsize = MINSIZEARRAY; /* minimum size */ 60 newsize = MINSIZEARRAY; /* minimum size */
59 } 61 }
60 newblock = luaM_realloc(L, block, 62 newblock = luaM_reallocv(L, block, *size, newsize, size_elems);
61 cast(lu_mem, *size)*cast(lu_mem, size_elems),
62 cast(lu_mem, newsize)*cast(lu_mem, size_elems));
63 *size = newsize; /* update only when everything else is OK */ 63 *size = newsize; /* update only when everything else is OK */
64 return newblock; 64 return newblock;
65} 65}
66 66
67 67
68void *luaM_toobig (lua_State *L) {
69 luaG_runerror(L, "memory allocation error: block too big");
70 return NULL; /* to avoid warnings */
71}
72
73
74
68/* 75/*
69** generic allocation routine. 76** generic allocation routine.
70*/ 77*/
71void *luaM_realloc (lua_State *L, void *block, lu_mem osize, lu_mem nsize) { 78void *luaM_realloc (lua_State *L, void *block, size_t osize, size_t nsize) {
72 global_State *g = G(L); 79 global_State *g = G(L);
73 lua_assert((osize == 0) == (block == NULL)); 80 lua_assert((osize == 0) == (block == NULL));
74 if (nsize >= MAX_SIZET)
75 luaG_runerror(L, "memory allocation error: block too big");
76 block = (*g->realloc)(g->ud, block, osize, nsize); 81 block = (*g->realloc)(g->ud, block, osize, nsize);
77 if (block == NULL && nsize > 0) 82 if (block == NULL && nsize > 0)
78 luaD_throw(L, LUA_ERRMEM); 83 luaD_throw(L, LUA_ERRMEM);
diff --git a/lmem.h b/lmem.h
index 1c896571..d946f56e 100644
--- a/lmem.h
+++ b/lmem.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lmem.h,v 1.25 2001/11/28 20:13:13 roberto Exp roberto $ 2** $Id: lmem.h,v 1.26 2002/05/01 20:40:42 roberto Exp roberto $
3** Interface to Memory Manager 3** Interface to Memory Manager
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -16,28 +16,34 @@
16#define MEMERRMSG "not enough memory" 16#define MEMERRMSG "not enough memory"
17 17
18 18
19void *luaM_realloc (lua_State *L, void *oldblock, lu_mem oldsize, lu_mem size); 19void *luaM_realloc (lua_State *L, void *block, size_t oldsize, size_t size);
20 20
21void *luaM_growaux (lua_State *L, void *block, int *size, int size_elem, 21void *luaM_toobig (lua_State *L);
22
23#define luaM_reallocv(L,b,on,n,e) \
24 ((cast(size_t, (n)+1) <= MAX_SIZET/(e)) ? /* +1 only to avoid warnings */ \
25 luaM_realloc(L, (b), (on)*(e), (n)*(e)) : \
26 luaM_toobig(L))
27
28
29void *luaM_growaux (lua_State *L, void *block, int *size, size_t size_elem,
22 int limit, const char *errormsg); 30 int limit, const char *errormsg);
23 31
24#define luaM_free(L, b, s) luaM_realloc(L, (b), (s), 0) 32#define luaM_free(L, b, s) luaM_realloc(L, (b), (s), 0)
25#define luaM_freelem(L, b) luaM_realloc(L, (b), sizeof(*(b)), 0) 33#define luaM_freelem(L, b) luaM_realloc(L, (b), sizeof(*(b)), 0)
26#define luaM_freearray(L, b, n, t) luaM_realloc(L, (b), \ 34#define luaM_freearray(L, b, n, t) luaM_reallocv(L, (b), n, 0, sizeof(t))
27 cast(lu_mem, n)*cast(lu_mem, sizeof(t)), 0)
28 35
29#define luaM_malloc(L, t) luaM_realloc(L, NULL, 0, (t)) 36#define luaM_malloc(L,t) luaM_realloc(L, NULL, 0, (t))
30#define luaM_new(L, t) cast(t *, luaM_malloc(L, sizeof(t))) 37#define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t)))
31#define luaM_newvector(L, n,t) cast(t *, luaM_malloc(L, \ 38#define luaM_newvector(L,n,t) \
32 cast(lu_mem, n)*cast(lu_mem, sizeof(t)))) 39 cast(t *, luaM_reallocv(L, NULL, 0, n, sizeof(t)))
33 40
34#define luaM_growvector(L,v,nelems,size,t,limit,e) \ 41#define luaM_growvector(L,v,nelems,size,t,limit,e) \
35 if (((nelems)+1) > (size)) \ 42 if (((nelems)+1) > (size)) \
36 ((v)=cast(t *, luaM_growaux(L,v,&(size),sizeof(t),limit,e))) 43 ((v)=cast(t *, luaM_growaux(L,v,&(size),sizeof(t),limit,e)))
37 44
38#define luaM_reallocvector(L, v,oldn,n,t) \ 45#define luaM_reallocvector(L, v,oldn,n,t) \
39 ((v)=cast(t *, luaM_realloc(L, v,cast(lu_mem, oldn)*cast(lu_mem, sizeof(t)), \ 46 ((v)=cast(t *, luaM_reallocv(L, v, oldn, n, sizeof(t))))
40 cast(lu_mem, n)*cast(lu_mem, sizeof(t)))))
41 47
42 48
43#endif 49#endif
diff --git a/lstate.c b/lstate.c
index 1e11c9bf..2f5031d5 100644
--- a/lstate.c
+++ b/lstate.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstate.c,v 2.14 2004/09/15 20:39:42 roberto Exp roberto $ 2** $Id: lstate.c,v 2.15 2004/10/06 18:34:16 roberto Exp roberto $
3** Global State 3** Global State
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -79,7 +79,7 @@ static void f_luaopen (lua_State *L, void *ud) {
79 Udata *u; /* head of udata list */ 79 Udata *u; /* head of udata list */
80 global_State *g = G(L); 80 global_State *g = G(L);
81 UNUSED(ud); 81 UNUSED(ud);
82 u = cast(Udata *, luaM_malloc(L, sizeudata(0))); 82 u = luaM_new(L, Udata);
83 u->uv.len = 0; 83 u->uv.len = 0;
84 u->uv.metatable = NULL; 84 u->uv.metatable = NULL;
85 g->firstudata = obj2gco(u); 85 g->firstudata = obj2gco(u);
diff --git a/lstring.c b/lstring.c
index 13b75086..99c3f0b6 100644
--- a/lstring.c
+++ b/lstring.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstring.c,v 2.2 2004/04/30 20:13:38 roberto Exp roberto $ 2** $Id: lstring.c,v 2.3 2004/08/24 20:12:06 roberto Exp roberto $
3** String table (keeps all strings handled by Lua) 3** String table (keeps all strings handled by Lua)
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -49,8 +49,11 @@ void luaS_resize (lua_State *L, int newsize) {
49 49
50static TString *newlstr (lua_State *L, const char *str, size_t l, 50static TString *newlstr (lua_State *L, const char *str, size_t l,
51 unsigned int h) { 51 unsigned int h) {
52 TString *ts = cast(TString *, luaM_malloc(L, sizestring(l))); 52 TString *ts;
53 stringtable *tb; 53 stringtable *tb;
54 if (l+1 > (MAX_SIZET - sizeof(TString))/sizeof(char))
55 luaM_toobig(L);
56 ts = cast(TString *, luaM_malloc(L, (l+1)*sizeof(char)+sizeof(TString)));
54 ts->tsv.len = l; 57 ts->tsv.len = l;
55 ts->tsv.hash = h; 58 ts->tsv.hash = h;
56 ts->tsv.marked = luaC_white(G(L)); 59 ts->tsv.marked = luaC_white(G(L));
@@ -92,7 +95,9 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
92 95
93Udata *luaS_newudata (lua_State *L, size_t s) { 96Udata *luaS_newudata (lua_State *L, size_t s) {
94 Udata *u; 97 Udata *u;
95 u = cast(Udata *, luaM_malloc(L, sizeudata(s))); 98 if (s > MAX_SIZET - sizeof(Udata))
99 luaM_toobig(L);
100 u = cast(Udata *, luaM_malloc(L, s + sizeof(Udata)));
96 u->uv.marked = luaC_white(G(L)); /* is not finalized */ 101 u->uv.marked = luaC_white(G(L)); /* is not finalized */
97 u->uv.tt = LUA_TUSERDATA; 102 u->uv.tt = LUA_TUSERDATA;
98 u->uv.len = s; 103 u->uv.len = s;
diff --git a/lstring.h b/lstring.h
index dc3dc0e1..ec2972a1 100644
--- a/lstring.h
+++ b/lstring.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstring.h,v 1.38 2003/11/17 19:50:05 roberto Exp roberto $ 2** $Id: lstring.h,v 1.39 2004/08/24 20:12:06 roberto Exp roberto $
3** String table (keep all strings handled by Lua) 3** String table (keep all strings handled by Lua)
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -13,10 +13,9 @@
13#include "lstate.h" 13#include "lstate.h"
14 14
15 15
16#define sizestring(l) (cast(lu_mem, sizeof(union TString))+ \ 16#define sizestring(s) (sizeof(union TString)+((s)->len+1)*sizeof(char))
17 (cast(lu_mem, l)+1)*sizeof(char))
18 17
19#define sizeudata(l) (cast(lu_mem, sizeof(union Udata))+(l)) 18#define sizeudata(u) (sizeof(union Udata)+(u)->len)
20 19
21#define luaS_new(L, s) (luaS_newlstr(L, s, strlen(s))) 20#define luaS_new(L, s) (luaS_newlstr(L, s, strlen(s)))
22#define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ 21#define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \