Assert function in AFL

assert functions are useful for catching bugs in the code. I have seen it being used in C/C++ code.

How would an assert function look like in AFL? Anyone implemented an assert function in AFL before? Is it advisable to use assert functions in AFL code?

Thank you.

You may use Error function to terminate AFL program.

Example:

procedure MxTest( mat ) {
	if ( typeof( mat ) != "matrix" ) {
		// abort further code execution 
		// if function  argument is not type matrix
		Error( "MxTest(): first argument is not type matrix!" );
	}
	// print matrix 
	printf( "MxTest():\n%s", MxToString(mat));
}

if ( ParamToggle( "Make test", "OFF|ON" ) ) {
	mat = 1;
	MxTest(mat);
}

So if you toggle "Make test" to ON then it will result to error and abortion since mat is not matrix but there is number assigned to it.

837


If you would assign actual matrix to mat variable then matrix would be printed.

procedure MxTest( mat ) {
	if ( typeof( mat ) != "matrix" ) {
		// abort further code execution 
		// if function  argument is not type matrix
		Error( "MxTest(): first argument is not type matrix!" );
	}
	// print matrix 
	printf( "MxTest():\n%s", MxToString(mat));
}

if ( ParamToggle( "Make test", "OFF|ON" ) ) {
	mat = Matrix( 3, 3, 1 );
	MxTest(mat);
}

129

4 Likes

Here is assert function

function assert( x )
{
  if( NOT x ) Error("Assertion failed");
}
3 Likes

Thanks Tomasz.

Some custom modification of my own. description provides more information about the assertion error. For example, like where the error occurred. procedure instead of function is used because error() returns nothing.

procedure assert( x, description )
{
  if( NOT x ) Error("Assertion failed. " + description);
}
3 Likes

For what it is worth procedure and function keywords are actually synonyms in AFL. I always use function because of C/C++ convention where function returning void is still a function. But if somebody wants to write procedure for code that returns values it is valid too :slight_smile:

3 Likes