ソースを参照

fixed ESP bug, check valid ESP on return

Nicolas Cannasse 9 年 前
コミット
0ff4092a05
1 ファイル変更12 行追加2 行削除
  1. 12 2
      src/jit.c

+ 12 - 2
src/jit.c

@@ -253,6 +253,7 @@ struct jit_ctx {
 #define jit_exit() { hl_debug_break(); exit(-1); }
 #define jit_error(msg)	_jit_error(ctx,msg,__LINE__)
 static void _jit_error( jit_ctx *ctx, const char *msg, int line );
+static void on_jit_error( const char *msg, int_val line );
 
 static preg *pmem( preg *r, CpuReg reg, int offset ) {
 	r->kind = RMEM;
@@ -1114,7 +1115,7 @@ static int prepare_call_args( jit_ctx *ctx, int count, int *args, vreg *vregs, b
 		size += r->size;
 	}
 	paddedSize = pad_stack(ctx,size);
-	size = paddedSize - size;
+	size = ctx->totalRegsSize + (paddedSize - size);
 	for(i=0;i<stackRegs;i++) {
 		// RTL
 		vreg *r = vregs + args[count - (i + 1)];
@@ -1182,7 +1183,7 @@ static void call_native( jit_ctx *ctx, void *nativeFun, int size ) {
 	// native function, already resolved
 	op64(ctx,MOV,PEAX,pconst64(&p,(int_val)nativeFun));
 	op64(ctx,CALL,PEAX,UNUSED);
-	if( nativeFun == hl_null_access || nativeFun == hl_throw )
+	if( nativeFun == hl_null_access || nativeFun == hl_throw || nativeFun == on_jit_error )
 		return;
 	discard_regs(ctx, true);
 	op64(ctx,ADD,PESP,pconst(&p,size));
@@ -1232,6 +1233,15 @@ static void op_ret( jit_ctx *ctx, vreg *r ) {
 	preg p;
 	op64(ctx, MOV, IS_FLOAT(r) ? PXMM(0) : PEAX, fetch(r));
 	if( ctx->totalRegsSize ) op64(ctx, ADD, PESP, pconst(&p, ctx->totalRegsSize));
+#	ifdef HL_DEBUG
+	{
+		int jeq;
+		op64(ctx, CMP, PESP, PEBP);
+		XJump_small(JEq,jeq);
+		jit_error("invalid ESP");
+		patch_jump(ctx,jeq);
+	}
+#	endif
 	op64(ctx, POP, PEBP, UNUSED);
 	op64(ctx, RET, UNUSED, UNUSED);
 }