Jump to content

[SOLVED] UNIGINE script call stack overflow


photo

Recommended Posts

Problem

 

While doing complex data processing including deep recursive function calls in UNIGINE script we encountered sudden UNIGINE crashes without any warning even in debug build.

 

Cause

 

Crash caused by memory corruption most probably due to exceeding hard-coded (128) , but unguarded call stack limit by recursive function calls for processing some large linked lists.

 

Proposal

 

Size of the call stack should be increased and bounds should be checked (at least with an assert for problem detection in debug build).

 

source\engine\interpreter\Machine.cpp

/* Copyright (C) 2005-2012, Unigine Corp. All rights reserved.
*
* File:	Machine.cpp
.....
.....
#define MACHINE_STACK_SIZE 128	// should be much higher, maybe 1024 ?
.....
.....
// call stack
Call calls[MACHINE_STACK_SIZE];
calls[0].function = NULL;
calls[0].address = begin;
int depth = 0;
.....
.....
while(1) {
.....  
  CASE(CALL)   { calls[depth++].address = int(c - opcodes) + 1; 	// depth should be tested
	calls[depth].function = NULL;
	calls[depth].address = *c;
	#ifndef NDEBUG
     	functions.append(calls[depth].address);
	#endif
	BREAK;
   	}
......

There are more code sections in Machine.cpp were calls[depth++] is used without array bound checks.

 

With deep function recursion it seems also possible to run out of interpreter variable stack space (which was reported by an assert within Stack class within debug build)

 

source\engine\Interpreter.h

/* Copyright (C) 2005-2012, Unigine Corp. All rights reserved.
*
* File:	Interpreter.h
.....
.....
#define INTERPRETER_INSTANCE Stack< ::Interpreter::Instance,128>
#define INTERPRETER_STATE  Stack< ::Interpreter::State,128>
#define INTERPRETER_STACK  Stack< ::Variable,2048>  // might be increased to something like 4096 ?
#define INTERPRETER_STRING  StringStack<16>
.....
.....

Link to comment

Frustum increased a call stack limit up to 1024 calls. Checks are not used not to compromise performance, since it can be quite taxing. We hope that it would solve your issue, Ulf.

Link to comment

Checks are not used not to compromise performance, since it can be quite taxing. We hope that it would solve your issue, Ulf.

 

Thanks, call stack size of 1024 will for sure handle most recusion requirements. Nevertheless an assert in debug build would be very helpfull, as such kind of memory corruption and engine crashes are quite hard to debug without any assert hint.

Link to comment
×
×
  • Create New...