I've found a strange "bug" in my kernel and i can't figure out how and why it happens.
When i call snprintf somehow it uses 400.000 bytes from the stack that causes it to overflow. I also write the esp values before and after the call to serial port to catch debug information.
The caller code is the following:
Code: Select all
__asm__ __volatile__( "cli\n" );
__asm__ __volatile__( "mov %%esp, %%eax" : "=a" ( esp ) );
do {
m = esp % 10;
esp = esp / 10;
while ( inb( SERIAL_PORT( SERIAL_LINE_STATUS_REGISTER ) & 0x40 ) == 0 ) ;
outb( '0' + m, SERIAL_PORT( SERIAL_TRANSMIT_BUFFER ) );
} while ( esp != 0 );
while ( inb( SERIAL_PORT( SERIAL_LINE_STATUS_REGISTER ) & 0x40 ) == 0 ) ;
outb( '\n', SERIAL_PORT( SERIAL_TRANSMIT_BUFFER ) );
snprintf( acFullPath, 128, "/dev/disk/%s", sEntry.de_acName );
And snprintf code is:
Code: Select all
int snprintf( char* pcBuffer, size_t nSize, const char* pcFormat, ... ) {
int esp, m;
int nResult;
va_list sArgs;
__asm__ __volatile__( "mov %%esp, %%eax" : "=a" ( esp ) );
do {
m = esp % 10;
esp = esp / 10;
while ( inb( SERIAL_PORT( SERIAL_LINE_STATUS_REGISTER ) & 0x40 ) == 0 ) ;
outb( '0' + m, SERIAL_PORT( SERIAL_TRANSMIT_BUFFER ) );
} while ( esp != 0 );
while ( inb( SERIAL_PORT( SERIAL_LINE_STATUS_REGISTER ) & 0x40 ) == 0 ) ;
outb( '\n', SERIAL_PORT( SERIAL_TRANSMIT_BUFFER ) );
...
- before snprintf call, ESP=4643625
- after snprintf call, ESP=4243625
Those numbers are in decimal format, and the difference between two ESPs is 400.000 bytes!
I also have the disassembled code of the binary. It can be found here: http://phpfi.com/313985
Could you suggest me something where to start fixing it or something? Unfortunately i ran out of ideas
Thanks, c0x