From 751cd9d82180f1bd99a738acc29bc114995a42e4 Mon Sep 17 00:00:00 2001 From: Mike Pall Date: Tue, 28 Aug 2012 15:24:53 +0200 Subject: [PATCH] Don't constify upvalues that may retain large amounts of memory. --- src/Makefile.dep | 5 +++-- src/lj_record.c | 28 +++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/Makefile.dep b/src/Makefile.dep index 9b130deb..94a963ba 100644 --- a/src/Makefile.dep +++ b/src/Makefile.dep @@ -155,8 +155,9 @@ lj_parse.o: lj_parse.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ lj_state.h lj_bc.h lj_ctype.h lj_lex.h lj_parse.h lj_vm.h lj_vmevent.h lj_record.o: lj_record.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_meta.h lj_frame.h lj_bc.h \ - lj_ff.h lj_ffdef.h lj_ir.h lj_jit.h lj_ircall.h lj_iropt.h lj_trace.h \ - lj_dispatch.h lj_traceerr.h lj_record.h lj_ffrecord.h lj_snap.h lj_vm.h + lj_ctype.h lj_gc.h lj_ff.h lj_ffdef.h lj_ir.h lj_jit.h lj_ircall.h \ + lj_iropt.h lj_trace.h lj_dispatch.h lj_traceerr.h lj_record.h \ + lj_ffrecord.h lj_snap.h lj_vm.h lj_snap.o: lj_snap.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ lj_tab.h lj_state.h lj_frame.h lj_bc.h lj_ir.h lj_jit.h lj_iropt.h \ lj_trace.h lj_dispatch.h lj_traceerr.h lj_snap.h lj_target.h \ diff --git a/src/lj_record.c b/src/lj_record.c index a593af99..ce25f29e 100644 --- a/src/lj_record.c +++ b/src/lj_record.c @@ -15,6 +15,9 @@ #include "lj_tab.h" #include "lj_meta.h" #include "lj_frame.h" +#if LJ_HASFFI +#include "lj_ctype.h" +#endif #include "lj_bc.h" #include "lj_ff.h" #include "lj_ir.h" @@ -1275,6 +1278,29 @@ TRef lj_record_idx(jit_State *J, RecordIndex *ix) /* -- Upvalue access ------------------------------------------------------ */ +/* Check whether upvalue is immutable and ok to constify. */ +static int rec_upvalue_constify(jit_State *J, GCupval *uvp) +{ + if (uvp->immutable) { + cTValue *o = uvval(uvp); + /* Don't constify objects that may retain large amounts of memory. */ +#if LJ_HASFFI + if (tviscdata(o)) { + GCcdata *cd = cdataV(o); + if (!cdataisv(cd) && !(cd->marked & LJ_GC_CDATA_FIN)) { + CType *ct = ctype_raw(ctype_ctsG(J2G(J)), cd->ctypeid); + if (!ctype_hassize(ct->info) || ct->size <= 16) + return 1; + } + return 0; + } +#endif + if (!(tvistab(o) || tvisudata(o) || tvisthread(o))) + return 1; + } + return 0; +} + /* Record upvalue load/store. */ static TRef rec_upvalue(jit_State *J, uint32_t uv, TRef val) { @@ -1282,7 +1308,7 @@ static TRef rec_upvalue(jit_State *J, uint32_t uv, TRef val) TRef fn = getcurrf(J); IRRef uref; int needbarrier = 0; - if (uvp->immutable) { /* Try to constify immutable upvalue. */ + if (rec_upvalue_constify(J, uvp)) { /* Try to constify immutable upvalue. */ TRef tr, kfunc; lua_assert(val == 0); if (!tref_isk(fn)) { /* Late specialization of current function. */