Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 24 May 2012
Offline Last Active Dec 21 2012 08:22 AM

Posts I've Made

In Topic: Problem implementing a BMFont renderer

21 December 2012 - 08:22 AM

Oh. Yes, I inverted the Y axis and it works !

That's actually logic, we read a text from top to bottom...


Thanks for your help smile.png

In Topic: How to limit memory used by AngelScript ?

27 May 2012 - 06:45 AM

I have added my custom memory functions to scriptarray, seems to work fine.
But when I try to create a too large buffer, I get a SIGSEV too.

I realized that if we run out of sandbox's memory, AngelScript wouldn't have space to allocate for errors handling.
So I've added a condition in memFree : if memLimit is reached, memFree will return 0 only once, and will not check the limit after (because the program using the sandbox is assumed to have stopped).

I recompiled and retried my test :
When the big buffer is created, the context exception is set to "Out of memory", the script stops.
Then the console prints "An exception 'Out of memory' occurred. Please correct the code and try again."
But when the engine is released, I get SIGSEV anyway :

#0 0xb7f56dca    std::string::resize(unsigned int, char) () (/usr/lib/i386-linux-gnu/libstdc++.so.6:??)
#1 0xb7f56e9b    std::string::resize(unsigned int) () (/usr/lib/i386-linux-gnu/libstdc++.so.6:??)
#2 0x806fc44    StringResize(l=135985784, str=...) (/home/marc/PROJETS/INFO/CPP/Botcraft/angelscript/addons/scriptstdstring/scriptstdstring.cpp:242)
#3 0x809a26d    asCScriptEngine::CallObjectMethodRetInt(this=0x819f034, obj=0x81afa78, func=41) (../../source/as_scriptengine.cpp:3254)
#4 0x80fbce9    asCGarbageCollector::DestroyOldGarbage(this=0x819f784) (../../source/as_gc.cpp:436)
#5 0x80fb2a6    asCGarbageCollector::GarbageCollect(this=0x819f784, flags=1) (../../source/as_gc.cpp:137)
#6 0x809a8f8    asCScriptEngine::GarbageCollect(this=0x819f034, flags=1) (../../source/as_scriptengine.cpp:3472)
#7 0x808dd33    asCScriptEngine::~asCScriptEngine(this=0x819f034, __in_chrg=<optimized out>) (../../source/as_scriptengine.cpp:531)
#8 0x808ece3    asCScriptEngine::Release(this=0x819f034) (../../source/as_scriptengine.cpp:684)
#9 0x8079abb    program(argc=1, argv=0xbffff8a4) (/home/marc/PROJETS/INFO/CPP/Botcraft/main.cpp:167)
#10 0x8079d47    main(argc=1, argv=0xbffff8a4) (/home/marc/PROJETS/INFO/CPP/Botcraft/main.cpp:205)

I hope this will help :)

In Topic: How to limit memory used by AngelScript ?

26 May 2012 - 06:41 PM

On a "Hello World" script, the memory use peak is 63221.
If I set the limit to 63000, SIGSEV occurs with the following call stack :

#0 0x80a9efe	asCMap<unsigned int, asCThreadLocalData*>::Insert(this=0x81a04e8, key=@0xbffff444: 3083679488, value=@0xbffff448: 0x0) (../../source/as_map.h:159)
#1 0x80a9800	asCThreadManager::SetLocalData(this=0x81a04e4, threadId=3083679488, tld=0x0) (../../source/as_thread.cpp:204)
#2 0x80a98fa	asCThreadManager::GetLocalData() (../../source/as_thread.cpp:232)
#3 0x807a069	asPushActiveContext(ctx=0x81b8f6c) (../../source/as_context.cpp:148)
#4 0x807c035	asCContext::Execute(this=0x81b8f6c) (../../source/as_context.cpp:1003)
#5 0x80799d7	program(argc=1, argv=0xbffff8a4) (/home/marc/PROJETS/INFO/CPP/Botcraft/main.cpp:153)
#6 0x8079cd3	main(argc=1, argv=0xbffff8a4) (/home/marc/PROJETS/INFO/CPP/Botcraft/main.cpp:205)

I added some stuff to the script (one class with 2 attributes and 2 methods) and set the limit to 65000.
SIGSEV occured again as the new limit has been reached :

#0 0x80c66d9	cByteInstruction::AddAfter(this=0x81ba124, nextCode=0x0) (../../source/as_bytecode.cpp:2474)
#1 0x80c3cee	asCByteCode::AddInstruction(this=0xbfffe64c) (../../source/as_bytecode.cpp:1308)
#2 0x80c5810	asCByteCode::InstrSHORT_DW(this=0xbfffe64c, bc=asBC_ADDSi, a=20, b=134217741) (../../source/as_bytecode.cpp:2214)
#3 0x80e388d	asCCompiler::CompileVariableAccess(this=0xbffff0ec, name=..., scope=..., ctx=0xbfffe64c, errNode=0x81b949c, isOptional=false, noFunction=false, objType=0x0) (../../source/as_compiler.cpp:6493)
#4 0x80e4ee4	asCCompiler::CompileExpressionValue(this=0xbffff0ec, node=0x81b949c, ctx=0xbfffe64c) (../../source/as_compiler.cpp:6806)
#5 0x80e2e4d	asCCompiler::CompileExpressionTerm(this=0xbffff0ec, node=0x81b944c, ctx=0xbfffeb9c) (../../source/as_compiler.cpp:6348)
#6 0x80e2c06	asCCompiler::CompilePostFixExpression(this=0xbffff0ec, postfix=0xbfffe898, ctx=0xbfffeb9c) (../../source/as_compiler.cpp:6321)
#7 0x80e2a15	asCCompiler::CompileExpression(this=0xbffff0ec, expr=0x81b93fc, ctx=0xbfffeb9c) (../../source/as_compiler.cpp:6286)
#8 0x80e26f9	asCCompiler::CompileCondition(this=0xbffff0ec, expr=0x81b93ac, ctx=0xbfffeb9c) (../../source/as_compiler.cpp:6236)
#9 0x80e175c	asCCompiler::CompileAssignment(this=0xbffff0ec, expr=0x81b935c, ctx=0xbfffecdc) (../../source/as_compiler.cpp:6035)
#10 0x80d5a9d	asCCompiler::CompileExpressionStatement(this=0xbffff0ec, enode=0x81b930c, bc=0xbfffedf4) (../../source/as_compiler.cpp:3124)
#11 0x80d2b23	asCCompiler::CompileStatement(this=0xbffff0ec, statement=0x81b930c, hasReturn=0xbfffef76, bc=0xbfffedf4) (../../source/as_compiler.cpp:2367)
#12 0x80cbf94	asCCompiler::CompileStatementBlock(this=0xbffff0ec, block=0x81a0b84, ownVariableScope=false, hasReturn=0xbfffef76, bc=0xbfffef78) (../../source/as_compiler.cpp:907)
#13 0x80ca5fe	asCCompiler::CompileFunction(this=0xbffff0ec, builder=0x81b7a1c, script=0x81b7adc, signature=0x0, func=0x81ad2fc, outFunc=0x81b7dd4) (../../source/as_compiler.cpp:508)
#14 0x80ae503	asCBuilder::CompileFunctions(this=0x81b7a1c) (../../source/as_builder.cpp:635)
#15 0x80ac5ff	asCBuilder::Build(this=0x81b7a1c) (../../source/as_builder.cpp:194)
#16 0x80fe533	asCModule::Build(this=0x81b74cc) (../../source/as_module.cpp:205)
#17 0x805d7fe	CScriptBuilder::Build(this=0xbffff558) (/home/marc/PROJETS/INFO/CPP/Botcraft/angelscript/addons/scriptbuilder/scriptbuilder.cpp:417)
#18 0x805c001	CScriptBuilder::BuildModule(this=0xbffff558) (/home/marc/PROJETS/INFO/CPP/Botcraft/angelscript/addons/scriptbuilder/scriptbuilder.cpp:85)
#19 0x80798db	program(argc=1, argv=0xbffff8a4) (/home/marc/PROJETS/INFO/CPP/Botcraft/main.cpp:127)
#20 0x8079cd3	main(argc=1, argv=0xbffff8a4) (/home/marc/PROJETS/INFO/CPP/Botcraft/main.cpp:205)

In Topic: How to limit memory used by AngelScript ?

26 May 2012 - 06:56 AM


I've successfully implemented the custom alloc and free functions, but now I have a new problem :

When I reach the memory limit (actually just before), how do I inform the program to stop the sandbox without crash?
Because I'm currently returning 0 (out of memory) in memAlloc, but then my program gets a SIGSEV and closes immediately.

Should I throw an exception instead?

My code :
void * Sandbox::memAlloc1(size_t size)
	// Check memory used
	m_state.currentMemUse += size;
	if(m_state.currentMemUse >= m_state.memLimit)
		return 0; // out of memory

	if(m_state.currentMemUse > m_state.memUsePeak)
		m_state.memUsePeak = m_state.currentMemUse;

	// Allocate memory + hidden field size
	void * ptr = malloc(size + sizeof(hidden_field_t));
	if(ptr == 0)
		return 0;

	// Setting hidden size field
	hidden_field_t * hptr = (hidden_field_t*)ptr;
	*hptr = size;
	ptr += sizeof(hidden_field_t); // ptr is then shifted



	return ptr;

void Sandbox::memFree1(void * ptr)
	// Get hidden size field
	ptr -= sizeof(hidden_field_t); // Unshift
	hidden_field_t * hptr = (hidden_field_t*)ptr;

	m_state.currentMemUse -= *hptr;

	// Free memory


In Topic: How to limit memory used by AngelScript ?

24 May 2012 - 03:53 PM

Thanks for your quick answer.
Actually, I forgot to call cleanup functions, now everything is ok about memory leaks.

I think that adding extra-data to memory blocks will be better for portability.
I had also imagined to first allocate a big memory block the size of the memory limit (not so much),
and then using it to store script's data, taking care not run out of it... but I don't know how to manage this.

In my project, every user will be able to send scripts to a server, that will run them (each in separated sandboxes),
so I have to secure the script manager a lot to avoid spamming.
Also, are the scriptarray or scriptstdstring addons using the same alloc functions? Or do I have to modify them?

I will examine your code, it will be very useful for the moment Posted Image