diff --git a/include/procreg.h b/include/procreg.h index 7ddb9a9cd..c406670be 100644 --- a/include/procreg.h +++ b/include/procreg.h @@ -833,9 +833,11 @@ PROCREG_PROC( void, RegisterAndCreateGlobalWithInit )( POINTER *ppGlobal, uintpt * Add a transaltion tree index at the same time. */ PROCREG_PROC( CTEXTSTR, SaveNameConcatN )( CTEXTSTR name1, ... ); -// no space stripping, saves literal text +// no space stripping, saves literal text (case insensitive indexing; '/' and '\' are the same) PROCREG_PROC( CTEXTSTR, SaveText )( CTEXTSTR text ); +// no space stripping, saves literal text (case sensitive indexing; '/' and '\' are the same) +PROCREG_PROC( CTEXTSTR, SaveTextCS )( CTEXTSTR text ); PROCREG_NAMESPACE_END diff --git a/include/sack_typelib.h b/include/sack_typelib.h index 62b212f8e..df2f846c5 100644 --- a/include/sack_typelib.h +++ b/include/sack_typelib.h @@ -3147,9 +3147,12 @@ TYPELIB_PROC CPOINTER TYPELIB_CALLTYPE FindInBinaryTree( PTREEROOT root, uintp // result of fuzzy routine is 0 = match. 100 = inexact match +// 101 = no longer matching; result with last 100 match. // 1 = no match, actual may be larger // -1 = no match, actual may be lesser // 100 = inexact match- checks nodes near for better match. +// +// Basically scans left and right from 100 match to find best match. TYPELIB_PROC CPOINTER TYPELIB_CALLTYPE LocateInBinaryTree( PTREEROOT root, uintptr_t key , int (CPROC*fuzzy)( uintptr_t psv, uintptr_t node_key ) ); diff --git a/include/sharemem.h b/include/sharemem.h index 5720a09fc..83da91d6a 100644 --- a/include/sharemem.h +++ b/include/sharemem.h @@ -677,7 +677,12 @@ MEM_PROC void MEM_API GetMemStats ( uint32_t *pFree, uint32_t *pUsed, uint32_t bTrueFalse : if TRUE, allocation logging is turned on. Enables logging when each block is Allocated, Released, or Held. */ -MEM_PROC int MEM_API SetAllocateLogging ( LOGICAL bTrueFalse ); +MEM_PROC int MEM_API SetAllocateLoggingEx ( LOGICAL bTrueFalse DBG_PASS ); +#define SetAllocateLogging(tf) SetAllocateLoggingEx( tf DBG_SRC ) +MEM_PROC int MEM_API ClearAllocateLoggingEx ( LOGICAL bTrueFalse DBG_PASS ); +#define ClearAllocateLogging(tf) ClearAllocateLoggingEx( tf DBG_SRC ) +MEM_PROC int MEM_API ResetAllocateLoggingEx ( LOGICAL bTrueFalse DBG_PASS ); +#define ResetAllocateLogging(tf) ResetAllocateLoggingEx( tf DBG_SRC ) /* disables storing file/line, also disables auto GetMemStats checking Parameters diff --git a/src/SQLlib/sqlstruc.h b/src/SQLlib/sqlstruc.h index ddd9da6f7..0da3b5365 100644 --- a/src/SQLlib/sqlstruc.h +++ b/src/SQLlib/sqlstruc.h @@ -79,7 +79,7 @@ typedef struct data_collection_tag size_t *result_len; TEXTSTR *results; //uint32_t nResults; // this is columns - TEXTSTR *fields; + CTEXTSTR *fields; PDATALIST *ppdlResults; #if defined( USE_SQLITE ) || defined( USE_SQLITE_INTERFACE ) sqlite3_stmt *stmt; @@ -172,6 +172,9 @@ struct odbc_queue PLINKQUEUE connections; }; +typedef char SQL_TIME_BUFFER[32]; +#define MAXSQL_TIME_BUFFERSPERSET 256 +DeclareSet( SQL_TIME_BUFFER ); #ifdef SQLLIB_SOURCE struct pssql_global @@ -227,6 +230,7 @@ struct pssql_global // ----- shared with option code; these need to be shared between instances. PTREEROOT tables; // some + PSQL_TIME_BUFFERSET time_buffers; }; #endif diff --git a/src/SQLlib/sqlstub.c b/src/SQLlib/sqlstub.c index e7418631e..06529515b 100644 --- a/src/SQLlib/sqlstub.c +++ b/src/SQLlib/sqlstub.c @@ -2368,10 +2368,7 @@ void ReleaseCollectionResults( PCOLLECT pCollect, int bEntire ) int idx; if( bEntire && pCollect->fields ) { - for( idx = 0; idx < pCollect->columns; idx++ ) - { - Release( (POINTER)pCollect->fields[idx] ); - } + // column text is saved in name cache Release( (POINTER)pCollect->fields ); pCollect->fields = NULL; Release( pCollect->result_len ); @@ -3436,7 +3433,7 @@ int __GetSQLResult( PODBC odbc, PCOLLECT collection, int bMore ) # if defined( USE_SQLITE ) || defined( USE_SQLITE_INTERFACE ) if( odbc->flags.bSQLite_native ) { - val->name = DupCStr( sqlite3_column_name( collection->stmt, idx - 1 ) ); + val->name = (TEXTSTR)SaveTextCS( sqlite3_column_name( collection->stmt, idx - 1 ) ); val->nameLen = StrLen( val->name ); } #endif @@ -3473,7 +3470,8 @@ int __GetSQLResult( PODBC odbc, PCOLLECT collection, int bMore ) result_cmd = WM_SQL_RESULT_ERROR; break; } - val->name = DupCStrLen( colname, namelen ); + colname[namelen] = 0; + val->name = (TEXTSTR)SaveTextCS( colname ); val->nameLen = namelen; } #endif @@ -3507,7 +3505,7 @@ int __GetSQLResult( PODBC odbc, PCOLLECT collection, int bMore ) if( !collection->fields ) { len = ( sizeof( CTEXTSTR ) * (collection->columns + 1) ); - collection->fields = NewArray( TEXTSTR, collection->columns + 1 ); + collection->fields = NewArray( CTEXTSTR, collection->columns + 1 ); MemSet( collection->fields, 0, len ); len = (sizeof( size_t ) * (collection->columns + 1)); collection->result_len = NewArray( size_t, collection->columns + 1 ); @@ -3531,7 +3529,7 @@ int __GetSQLResult( PODBC odbc, PCOLLECT collection, int bMore ) if( odbc->flags.bSQLite_native ) { collection->fields[idx-1] = - DupCStr( sqlite3_column_name(collection->stmt + SaveTextCS( sqlite3_column_name(collection->stmt , idx - 1 ) ); collection->column_types[idx-1] = sqlite3_column_type( collection->stmt, idx - 1 ); } @@ -3583,7 +3581,7 @@ int __GetSQLResult( PODBC odbc, PCOLLECT collection, int bMore ) } //lprintf( "col %s is %d %d", colname, collection->colsizes[idx-1], collection->coltypes[idx-1] ); colname[namelen] = 0; // always nul terminate this. - collection->fields[idx-1] = StrDup( colname ); + collection->fields[idx-1] = SaveTextCS( colname ); } #endif } // for @@ -3865,6 +3863,7 @@ int __GetSQLResult( PODBC odbc, PCOLLECT collection, int bMore ) case SQL_VARCHAR: case SQL_LONGVARCHAR: val->value_type = JSOX_VALUE_STRING; + if( val->string ) Release( val->string ); do { rc = SQLGetData( collection->hstmt , (short)(idx) @@ -3912,6 +3911,7 @@ int __GetSQLResult( PODBC odbc, PCOLLECT collection, int bMore ) case SQL_WCHAR: case SQL_WVARCHAR: case SQL_WLONGVARCHAR: + if( val->string ) Release( val->string ); val->value_type = JSOX_VALUE_STRING; rc = SQLGetData( collection->hstmt , (short)(idx) @@ -3962,9 +3962,10 @@ int __GetSQLResult( PODBC odbc, PCOLLECT collection, int bMore ) if( rc == SQL_SUCCESS ) { if( ResultLen == SQL_NULL_DATA ) { val->value_type = JSOX_VALUE_NULL; + val->string = NULL; if( pvtData )vtprintf( pvtData, "%sNULL", idx > 1 ? "," : "" ); }else { - char *isoTime = NewArray( char, 32 ); + char *isoTime = (char*)GetFromSet( SQL_TIME_BUFFER, &g.time_buffers ); val->stringLen = snprintf( isoTime, 32, "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ" , ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second, ts.fraction ); val->value_type = JSOX_VALUE_DATE; @@ -4907,7 +4908,23 @@ void SQLEndQuery( PODBC odbc ) //----------------------------------------------------------------------- void ReleaseSQLResults( PDATALIST *ppdlResults ) { - jsox_dispose_message( ppdlResults ); + if( ppdlResults && *ppdlResults ) { + PDATALIST pdlResults = *ppdlResults; + INDEX idx; + struct jsox_value_container * val; + DATA_FORALL( pdlResults, idx, struct jsox_value_container *, val ) { + // names are saved in cache and shouldn't be released... + //if( val->base.name ) Release( val->base.name ); + //lprintf( "Value type:%s %s %d", val->string, val->name, val->value_type ); + if( val->value_type == JSOX_VALUE_DATE ){ + DeleteFromSet( SQL_TIME_BUFFER, &g.time_buffers, val->string ); + } else + if( val->string ) Release( val->string ); + } + DeleteDataList( ppdlResults ); + *ppdlResults = NULL; + } + //jsox_dispose_message( ppdlResults ); } int SQLRecordQuery_js( PODBC odbc @@ -4921,7 +4938,7 @@ int SQLRecordQuery_js( PODBC odbc int once = 0; PCOLLECT collection; if( !(*pdlResults) ) - (*pdlResults) = CreateDataList( sizeof ( struct jsox_value_container ) ); + (*pdlResults) = CreateDataList( sizeof ( struct jsox_value_container ) ); // clean up what we think of as our result set data (reset to nothing) diff --git a/src/configlib/configscript.c b/src/configlib/configscript.c index 66c605330..fe43372c7 100644 --- a/src/configlib/configscript.c +++ b/src/configlib/configscript.c @@ -3113,7 +3113,7 @@ CONFIGSCR_PROC( PCONFIG_HANDLER, CreateConfigurationEvaluator )( void ) { if( !(g._disabled_allocate_logging++) ) { - g._last_allocate_logging = SetAllocateLogging( FALSE ); + g._last_allocate_logging = ClearAllocateLogging( FALSE ); } } pch = (PCONFIG_HANDLER)Allocate( sizeof( CONFIG_HANDLER ) ); @@ -3299,7 +3299,7 @@ CONFIGSCR_PROC( void, DestroyConfigurationEvaluator )( PCONFIG_HANDLER pch ) g._disabled_allocate_logging--; if( !g._disabled_allocate_logging ) { - SetAllocateLogging( g._last_allocate_logging ); + ResetAllocateLogging( g._last_allocate_logging ); } } DeleteList( &pch->states ); diff --git a/src/filesyslib/winfiles.c b/src/filesyslib/winfiles.c index f4a76aa68..83d73ef3f 100644 --- a/src/filesyslib/winfiles.c +++ b/src/filesyslib/winfiles.c @@ -578,7 +578,7 @@ TEXTSTR ExpandPathExx( CTEXTSTR path, struct file_system_interface* fsi DBG_PASS { TEXTSTR tmp_path; if( !path ) return NULL; - tmp_path = StrDup( path ); + tmp_path = StrDupEx( path DBG_RELAY ); //LocalInit(); #if !defined( __FILESYS_NO_FILE_LOGGING__ ) if( ( *winfile_local ).flags.bLogOpenClose ) @@ -707,7 +707,8 @@ TEXTSTR ExpandPathExx( CTEXTSTR path, struct file_system_interface* fsi DBG_PASS } else if( StrChr( path, '%' ) != NULL ) { tmp_path = ExpandPathVariable( path ); } else { - tmp_path = StrDupEx( path DBG_RELAY ); + // is already duplicated, no changes were made. + //tmp_path = StrDupEx( path DBG_RELAY ); } } { @@ -1182,8 +1183,10 @@ struct file* FindFileByName( INDEX group, char const* filename, struct file_syst INDEX idx; LocalInit(); EnterCriticalSec( &( *winfile_local ).cs_files ); + //lprintf( "Find file: %s in %p", filename, winfile_local->files ); LIST_FORALL( ( *winfile_local ).files, idx, struct file*, file ) { + //lprintf( "Is it %d %d %s?", !mount, file->mount == mount, file->name ); if( ( file->group == group ) && ( PathCmp( file->name, filename ) == 0 ) && ( ( !mount ) || file->mount == mount ) ) { @@ -1196,6 +1199,7 @@ struct file* FindFileByName( INDEX group, char const* filename, struct file_syst } LeaveCriticalSec( &( *winfile_local ).cs_files ); + //if( file ) lprintf( "found file" ); else lprintf( "file not found" ); return file; } @@ -1746,6 +1750,7 @@ FILE* sack_fopenEx( INDEX group, CTEXTSTR filename, CTEXTSTR opts, struct file_s } } EnterCriticalSec( &( *winfile_local ).cs_files ); + //lprintf( "Adding file to winfile_local.files... %p", winfile_local->files); AddLink( &( *winfile_local ).files, file ); allocedIndex = 0; LeaveCriticalSec( &( *winfile_local ).cs_files ); @@ -2909,7 +2914,7 @@ static void* CPROC sack_filesys_open( uintptr_t psv, const char* filename, const #else char *tmpFilename = StrDup( filename ); { char* tmp; if( LONG_PATHCHAR ) for( tmp = tmpFilename; tmp[0]; tmp++ ) if( tmp[0] == '\\' ) tmp[0] = LONG_PATHCHAR; } - result = fopen( filename, opts ); + result = fopen( tmpFilename, opts ); { int h = fileno( (FILE*)result ); if( h >= 0 ) { @@ -2917,6 +2922,7 @@ static void* CPROC sack_filesys_open( uintptr_t psv, const char* filename, const if( flags >= 0 ) fcntl( h, F_SETFD, flags | FD_CLOEXEC ); } } + Release( tmpFilename ); #endif return result; diff --git a/src/memlib/memory_operations.c b/src/memlib/memory_operations.c index 1ba6136af..70ef29cfb 100644 --- a/src/memlib/memory_operations.c +++ b/src/memlib/memory_operations.c @@ -503,7 +503,7 @@ TEXTSTR DupCStrLenEx( const char * original, size_t chars DBG_PASS ) TEXTSTR result, _result; if( !original ) return NULL; - _result = result = NewArray( TEXTCHAR, chars + 1 );// (TEXTSTR)AllocateEx( (len + 1) * sizeof( result[0] ) DBG_RELAY ); + _result = result = /*NewArray( TEXTCHAR, chars + 1 );//*/ (TEXTSTR)AllocateEx( (chars + 1) * sizeof( result[0] ) DBG_RELAY ); len = 0; while( len < chars ) ((*result++) = (*original++)), len++; result[0] = 0; diff --git a/src/memlib/sharemem.c b/src/memlib/sharemem.c index c00c8eb13..cf23da3ad 100644 --- a/src/memlib/sharemem.c +++ b/src/memlib/sharemem.c @@ -193,6 +193,9 @@ struct global_memory_tag { uint32_t bMemInstanced; // set if anybody starts to DIG. LOGICAL deadstart_finished; PMEM pMemInstance; + uint32_t last_set_allocate; + int nLogAllocateClears; + int bDefaultLogAllocate; }; #ifdef __STATIC__ @@ -311,7 +314,7 @@ PRIORITY_PRELOAD( InitGlobal, DEFAULT_PRELOAD_PRIORITY ) { #ifndef __NO_OPTIONS__ g.bLogCritical = SACK_GetProfileIntEx( GetProgramName(), "SACK/Memory Library/Log critical sections", g.bLogCritical, TRUE ); - g.bLogAllocate = SACK_GetProfileIntEx( GetProgramName(), "SACK/Memory Library/Enable Logging", g.bLogAllocate, TRUE ); + g.bDefaultLogAllocate = g.bLogAllocate = SACK_GetProfileIntEx( GetProgramName(), "SACK/Memory Library/Enable Logging", g.bLogAllocate, TRUE ); if( g.bLogAllocate ) ll_lprintf( "Memory allocate logging enabled." ); g.bLogAllocateWithHold = SACK_GetProfileIntEx( GetProgramName(), "SACK/Memory Library/Enable Logging Holds", g.bLogAllocateWithHold, TRUE ); @@ -2034,13 +2037,26 @@ POINTER HeapAllocateAlignedEx( PMEM pHeap, size_t dwSize, uint16_t alignment DBG #endif DropMem( pCurMem ); DropMem( pMem ); +/* + if( pCurMem->cs.dwLocks ) { + fprintf( stderr, "(pcurmem) Memory block %p has %d locks on it.", pCurMem, pCurMem->cs.dwLocks ); + //DebugBreak(); + } + if( pMem->cs.dwLocks ) { + fprintf( stderr, "(pmem) Memory block %p has %d locks on it.", pMem, pMem->cs.dwLocks ); + //DebugBreak(); + } +*/ //#if DBG_AVAILABLE #ifndef NO_LOGGING # ifdef _DEBUG if( g.bLogAllocate && g.allowLogging ) { _xlprintf( 2 DBG_RELAY )("Allocate : %p(%p) - %" _PTRSZVALfs " bytes", pc->byData, pc, pc->dwSize); - } + }else + { + //fprintf(stderr, DBG_FILELINEFMT " (disabled %d)Allocate : %p(%p) - %" _PTRSZVALfs " bytes\n" DBG_RELAY, global_memory_data.last_set_allocate, pc->byData, pc, pc->dwSize); + } # endif #endif //#endif @@ -2335,6 +2351,8 @@ POINTER ReleaseEx ( POINTER pData DBG_PASS ) else _xlprintf( 2 DBG_RELAY )("Release : %p(%p) - %" _PTRSZVALfs " bytes", pc->byData, pc, pc->dwSize); } + else + ;//fprintf(stderr, DBG_FILELINEFMT " (disabled %d)Release : %p(%p) - %" _PTRSZVALfs " bytes\n" DBG_RELAY, global_memory_data.last_set_allocate, pc->byData, pc, pc->dwSize); # endif #endif @@ -2599,23 +2617,23 @@ POINTER ReleaseEx ( POINTER pData DBG_PASS ) { PCHUNK pc = (PCHUNK)(((uintptr_t)pData) - ( ( (uint16_t*)pData)[-1] + offsetof( CHUNK, byData ) ) ); - PMEM pMem = GrabMem( pc->pRoot ); - #ifndef NO_LOGGING if( g.bLogAllocate ) { _xlprintf( 2 DBG_RELAY)( "Hold : %p - %" _PTRSZVALfs " bytes",pc, pc->dwSize ); } #endif + PMEM pMem = GrabMem( pc->pRoot ); + if( !pc->info.dwOwners ) { _xlprintf( 2 DBG_RELAY)( "Held block has already been released! too late to hold it!" ); DebugBreak(); - DropMem( pMem ); + DropMemEx( pMem DBG_RELAY ); return pData; } pc->info.dwOwners++; - DropMem(pMem ); + DropMemEx(pMem DBG_RELAY ); #ifdef _DEBUG if( !g.bDisableAutoCheck ) GetHeapMemStatsEx(pc->pRoot, &dwFree,&dwAllocated,&dwBlocks,&dwFreeBlocks DBG_RELAY); @@ -2959,7 +2977,9 @@ void DebugDumpHeapMemEx ( PMEM pHeap, LOGICAL bVerbose ) #if USE_CUSTOM_ALLOCER if( !pHeap ) pHeap = g.pMemInstance; + //fprintf( stderr, "Grab Heap: %d %p\n", nLine, pHeap ); pMem = GrabMem( pHeap ); + //fprintf( stderr, "Grabbed Heap: %p\n", pMem ); pMemSpace = FindSpace( pMem ); while( pMemSpace ) { @@ -3057,9 +3077,15 @@ void DebugDumpHeapMemEx ( PMEM pHeap, LOGICAL bVerbose ) } nSpaces++; pMemSpace = pMemSpace->next; - DropMem( pMemCheck ); + DropMemEx( pMemCheck DBG_RELAY ); } - DropMem( pMem ); + DropMemEx( pMem DBG_RELAY ); + if( pMem->cs.dwLocks) { + fprintf( stderr, "Locks is still set?\n"); + //DebugBreak(); + } + + //fprintf( stderr, "Dropped Mem: %d %p\n", nLine, pMem ); if( pFree ) *pFree = (uint32_t)nFree; if( pUsed ) @@ -3079,10 +3105,38 @@ void DebugDumpHeapMemEx ( PMEM pHeap, LOGICAL bVerbose ) } //------------------------------------------------------------------------------------------------------ - int SetAllocateLogging ( LOGICAL bTrueFalse ) + int SetAllocateLoggingEx ( LOGICAL bTrueFalse DBG_PASS ) +{ + LOGICAL prior = g.bLogAllocate; +#ifdef _DEBUG + g.last_set_allocate = nLine; +#endif + g.bDefaultLogAllocate = g.bLogAllocate = bTrueFalse; + _lprintf(DBG_RELAY)( "--------- USE CLEAR OR RESET LOGGING!" ); + return prior; +} + +//------------------------------------------------------------------------------------------------------ +int ClearAllocateLoggingEx ( LOGICAL bTrueFalse DBG_PASS ) +{ + LOGICAL prior = g.bLogAllocate; + g.nLogAllocateClears++; +#ifdef _DEBUG + g.last_set_allocate = nLine; +#endif + g.bLogAllocate = 0; + return prior; +} +//------------------------------------------------------------------------------------------------------ +int ResetAllocateLoggingEx ( LOGICAL bTrueFalse DBG_PASS ) { LOGICAL prior = g.bLogAllocate; - g.bLogAllocate = bTrueFalse; +#ifdef _DEBUG + g.last_set_allocate = nLine; +#endif + g.nLogAllocateClears--; + if( !g.nLogAllocateClears ) + g.bLogAllocate = g.bDefaultLogAllocate; return prior; } diff --git a/src/netlib/network_addresses.c b/src/netlib/network_addresses.c index 1193d5834..0f011f154 100644 --- a/src/netlib/network_addresses.c +++ b/src/netlib/network_addresses.c @@ -341,6 +341,7 @@ static void setupInterfaces() { } mac_data.addressCount = addressCount; mac_data.addresses = NewArray( struct addressNode*, addressCount ); + memset( mac_data.addresses, 0, sizeof( struct addressNode* ) * addressCount ); mac_data.netmasks = NewArray( uint8_t*, addressCount ); mac_data.addr_ifIndexes = NewArray( int, addressCount ); @@ -350,9 +351,18 @@ static void setupInterfaces() { if( current_ifa->ifa_addr->sa_family != AF_INET && current_ifa->ifa_addr->sa_family != AF_INET6 ) continue; // don't care about non IP addresses. - struct addressNode *newAddress = (struct addressNode*)AllocateEx( sizeof( struct addressNode ) DBG_SRC ); - SOCKADDR *sa = AllocAddr(); - newAddress->remote = sa; + struct addressNode newAddress;// = (struct addressNode*)AllocateEx( sizeof( struct addressNode ) DBG_SRC ); + uint8_t addrbuf[MAGIC_SOCKADDR_LENGTH + 2 * sizeof( uintptr_t )]; + + memset( addrbuf, 0, MAGIC_SOCKADDR_LENGTH + 2 * sizeof( uintptr_t ) ); + //initialize socket length to something identifiable? + ((uintptr_t*)addrbuf)[0] = 3; + ((uintptr_t*)addrbuf)[1] = 0; // string representation of address + + SOCKADDR *sa = (SOCKADDR*)(addrbuf + sizeof(uintptr_t) * 2);//AllocAddr(); + //(SOCKADDR*)AllocateEx( MAGIC_SOCKADDR_LENGTH + 2 * sizeof( uintptr_t ) DBG_RELAY ) + + newAddress.remote = sa; //lprintf( "Got ifaddr on %s %d", current_ifa->ifa_name, current_ifa->ifa_addr->sa_family ); //LogBinary( (const uint8_t*)current_ifa->ifa_addr, sizeof( *current_ifa->ifa_addr)); sa->sa_family = current_ifa->ifa_addr->sa_family; @@ -389,18 +399,24 @@ static void setupInterfaces() { LogBinary( mac_data.netmasks[addressCount], 16 ); #endif } - memcpy( newAddress->localHw, mac_data.hwaddrs[i], 6); - memcpy( newAddress->remoteHw, mac_data.hwaddrs[i], 6); + memcpy( newAddress.localHw, mac_data.hwaddrs[i], 6); + memcpy( newAddress.remoteHw, mac_data.hwaddrs[i], 6); #ifdef DEBUG_MAC_ADDRESS_LOOKUP LogMacAddress( newAddress ); -#endif - if( AddBinaryNode( mac_data.pbtAddresses, (CPOINTER)newAddress, (uintptr_t)sa ) ) { - //lprintf( "Added address to tree" ); - mac_data.addresses[addressCount] = newAddress; - } else { - //lprintf( "Failed to add address to tree" ); - ReleaseAddress( sa ); - Deallocate( struct addressNode *, newAddress ); +#endif + if( !FindInBinaryTree( mac_data.pbtAddresses, (uintptr_t)sa )) { + struct addressNode *storeAddress = (struct addressNode*)AllocateEx( sizeof( struct addressNode ) DBG_SRC ); + storeAddress[0] = newAddress; + storeAddress->remote = DuplicateAddress( storeAddress->remote ); + + if( AddBinaryNode( mac_data.pbtAddresses, (CPOINTER)storeAddress, (uintptr_t)sa ) ) { + //lprintf( "Added address to tree" ); + mac_data.addresses[addressCount] = storeAddress; + } else { + //lprintf( "Failed to add address to tree" ); + //ReleaseAddress( sa ); + //Deallocate( struct addressNode *, newAddress ); + } } break; } @@ -439,223 +455,254 @@ ATEXIT( CloseMacThread ){ static uintptr_t MacThread( PTHREAD thread ) { - int stat; - int rtnetlink_socket = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE); - if( rtnetlink_socket < 0 ) - { - threadFailed = 1; - if( errno == ESOCKTNOSUPPORT ){ - lprintf( "Socket No Support?"); - }else - lprintf( "Unable to create netlink socket %d", errno ); - return 0; - } - struct sockaddr rtnetlink_addr; - rtnetlink_addr.sa_family = AF_NETLINK; - rtnetlink_addr.sa_data[0] = 0; - rtnetlink_addr.sa_data[1] = 0; - int ppid = (int)(GetMyThreadID() >> 32); - rtnetlink_addr.sa_data[2] = ppid & 0xFF; - rtnetlink_addr.sa_data[3] = (ppid >> 8) & 0xFF; - rtnetlink_addr.sa_data[4] = (ppid >> 16) & 0xFF; - rtnetlink_addr.sa_data[5] = (ppid >> 24) & 0xFF; - int grp = RTMGRP_NEIGH; - rtnetlink_addr.sa_data[6] = grp & 0xFF; - rtnetlink_addr.sa_data[7] = 0; - rtnetlink_addr.sa_data[8] = 0; - rtnetlink_addr.sa_data[9] = 0; - - stat = bind(rtnetlink_socket, &rtnetlink_addr, 12 ); - if( stat < 0 ) - { - threadFailed = 1; - lprintf( "Unable to bind netlink socket %s", strerror( errno ) ); - return 0; - } - int seq; + int stat; + int rtnetlink_socket = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE); + if( rtnetlink_socket < 0 ) + { + threadFailed = 1; + if( errno == ESOCKTNOSUPPORT ){ + lprintf( "Socket No Support?"); + }else + lprintf( "Unable to create netlink socket %d", errno ); + return 0; + } + struct sockaddr rtnetlink_addr; + rtnetlink_addr.sa_family = AF_NETLINK; + rtnetlink_addr.sa_data[0] = 0; + rtnetlink_addr.sa_data[1] = 0; + int ppid = (int)(GetMyThreadID() >> 32); + rtnetlink_addr.sa_data[2] = ppid & 0xFF; + rtnetlink_addr.sa_data[3] = (ppid >> 8) & 0xFF; + rtnetlink_addr.sa_data[4] = (ppid >> 16) & 0xFF; + rtnetlink_addr.sa_data[5] = (ppid >> 24) & 0xFF; + int grp = RTMGRP_NEIGH; + rtnetlink_addr.sa_data[6] = grp & 0xFF; + rtnetlink_addr.sa_data[7] = 0; + rtnetlink_addr.sa_data[8] = 0; + rtnetlink_addr.sa_data[9] = 0; + + stat = bind(rtnetlink_socket, &rtnetlink_addr, 12 ); + if( stat < 0 ) + { + threadFailed = 1; + lprintf( "Unable to bind netlink socket %s", strerror( errno ) ); + return 0; + } + int seq; - struct { - struct nlmsghdr nlh; - struct ndmsg ndm; - char buf[256]; - } req; - req.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg)); - req.nlh.nlmsg_type = RTM_GETNEIGH; - req.nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST; - req.nlh.nlmsg_seq = ++seq; - req.ndm.ndm_family = 0; + struct { + struct nlmsghdr nlh; + struct ndmsg ndm; + char buf[256]; + } req; + req.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg)); + req.nlh.nlmsg_type = RTM_GETNEIGH; + req.nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST; + req.nlh.nlmsg_seq = ++seq; + req.ndm.ndm_family = 0; - send( rtnetlink_socket, (struct msghdr*)&req, sizeof(req), 0); + send( rtnetlink_socket, (struct msghdr*)&req, sizeof(req), 0); while( !macThreadEnd ) { - //sendmsg( rtnetlink_socket, (struct msghdr*)&req, 0); - ssize_t rstat; - static uint8_t buf[8192]; - int loop = 1; - do { - rstat = recv( rtnetlink_socket, buf, sizeof(buf), 0/*MSG_DONTWAIT*/ ); - if( rstat < 0) { - if( errno == EAGAIN || errno == EWOULDBLOCK ) { - //lprintf( "No data available" ); - Relinquish(); - } else{ - lprintf( "Error: %s", strerror( errno ) ); - loop = 0; - } + //sendmsg( rtnetlink_socket, (struct msghdr*)&req, 0); + ssize_t rstat; + static uint8_t buf[8192]; + int loop = 1; + do { + rstat = recv( rtnetlink_socket, buf, sizeof(buf), 0/*MSG_DONTWAIT*/ ); + if( rstat < 0) { + if( errno == EAGAIN || errno == EWOULDBLOCK ) { + //lprintf( "No data available" ); + Relinquish(); + } else{ + lprintf( "Error: %s", strerror( errno ) ); + loop = 0; } - else { - struct response { - struct nlmsghdr nl; - struct ndmsg rt; - } *res; - int msgLen; - int priorLen = 0; + } + else { + struct response { + struct nlmsghdr nl; + struct ndmsg rt; + } *res; + int msgLen; + int priorLen = 0; #ifdef DEBUG_MAC_ADDRESS_LOOKUP - lprintf( "Got some data from socket:%d", rstat); - lprintf( "Flag Values %d %d %d",NTF_SELF, NTF_PROXY, NTF_ROUTER ); + lprintf( "Got some data from socket:%d", rstat); + lprintf( "Flag Values %d %d %d",NTF_SELF, NTF_PROXY, NTF_ROUTER ); #endif - while( priorLen < rstat ) { - struct addressNode *newAddress = (struct addressNode*)AllocateEx( sizeof( struct addressNode ) DBG_SRC ); - res = (struct response*)(buf+priorLen); + while( priorLen < rstat ) { + struct addressNode newAddress;// = (struct addressNode*)AllocateEx( sizeof( struct addressNode ) DBG_SRC ); + res = (struct response*)(buf+priorLen); #ifdef DEBUG_MAC_ADDRESS_LOOKUP - lprintf( "Stuff: %p %d", res, priorLen ); + lprintf( "Stuff: %p %d", res, priorLen ); #endif - struct rtattr *attr = (struct rtattr*)(res+1); - switch( res->nl.nlmsg_type ) { - case NLMSG_DONE: - // end of dump; should send information here. - break; - case RTM_DELNEIGH: - // should probably delete the entry in the tree here... - break; - case RTM_NEWNEIGH: - //LogBinary( (uint8_t*)(res), res->nl.nlmsg_len ); - /* - lprintf( "First message: type:%d len:%d flg:%d pid:%d seq:%d", res->nl.nlmsg_type - , res->nl.nlmsg_len-sizeof( res) - , res->nl.nlmsg_flags, res->nl.nlmsg_pid, res->nl.nlmsg_seq ); - lprintf( "fam:%d ifi:%d st:%d fl:%d type:%d" - , res->rt.ndm_family, res->rt.ndm_ifindex - , res->rt.ndm_state, res->rt.ndm_flags, res->rt.ndm_type ); - */ - // state == NUD_INCOMPLETE, NUD_REACHABLE, NUD_STALE, NUD_DELAY, NUD_PROBE, NUD_FAILED, NUD_NORARP,NUD_PERMANENT - // flags == NTF_PROXY, NTF_ROUTER - // + struct rtattr *attr = (struct rtattr*)(res+1); + //lprintf( "message:%d", res->nl.nlmsg_type ); + switch( res->nl.nlmsg_type ) { + case NLMSG_DONE: + // end of dump; should send information here. + break; + case RTM_DELNEIGH: + // should probably delete the entry in the tree here... + break; + case RTM_NEWNEIGH: + //LogBinary( (uint8_t*)(res), res->nl.nlmsg_len ); + /* + lprintf( "First message: type:%d len:%d flg:%d pid:%d seq:%d", res->nl.nlmsg_type + , res->nl.nlmsg_len-sizeof( res) + , res->nl.nlmsg_flags, res->nl.nlmsg_pid, res->nl.nlmsg_seq ); + lprintf( "fam:%d ifi:%d st:%d fl:%d type:%d" + , res->rt.ndm_family, res->rt.ndm_ifindex + , res->rt.ndm_state, res->rt.ndm_flags, res->rt.ndm_type ); + */ + // state == NUD_INCOMPLETE, NUD_REACHABLE, NUD_STALE, NUD_DELAY, NUD_PROBE, NUD_FAILED, NUD_NORARP,NUD_PERMANENT + // flags == NTF_PROXY, NTF_ROUTER + // + { + uint8_t addrbuf[MAGIC_SOCKADDR_LENGTH + 2 * sizeof( uintptr_t )]; + + memset( addrbuf, 0, MAGIC_SOCKADDR_LENGTH + 2 * sizeof( uintptr_t ) ); + //initialize socket length to something identifiable? + ((uintptr_t*)addrbuf)[0] = 3; + ((uintptr_t*)addrbuf)[1] = 0; // string representation of address + SOCKADDR *sa = (SOCKADDR*)(addrbuf + sizeof(uintptr_t) * 2);//AllocAddr(); + + sa->sa_family = res->rt.ndm_family; + sa->sa_data[0] = 0; + sa->sa_data[1] = 0; + newAddress.remote = sa; + for( int i = 0; i < mac_data.interfaceCount; i++ ) { + if( mac_data.ifIndexes[i] == res->rt.ndm_ifindex ) { + memcpy( newAddress.localHw, mac_data.hwaddrs[i], 6); + break; + } + } + { - SOCKADDR *sa = AllocAddr(); - - sa->sa_family = res->rt.ndm_family; - sa->sa_data[0] = 0; - sa->sa_data[1] = 0; - newAddress->remote = sa; - for( int i = 0; i < mac_data.interfaceCount; i++ ) { - if( mac_data.ifIndexes[i] == res->rt.ndm_ifindex ) { - memcpy( newAddress->localHw, mac_data.hwaddrs[i], 6); + LOGICAL duplicated = FALSE; + LOGICAL destFound, linkFound; + size_t attLen = res->nl.nlmsg_len-sizeof( *res); + size_t attOfs = priorLen + sizeof( *res ); + destFound = linkFound = FALSE; + do { + struct rtattr *attr = (struct rtattr*)(buf+attOfs); + if( !attr->rta_len ) break; - } - } - - { - LOGICAL duplicated = FALSE; - size_t attLen = res->nl.nlmsg_len-sizeof( *res); - size_t attOfs = priorLen + sizeof( *res ); - do { - struct rtattr *attr = (struct rtattr*)(buf+attOfs); - if( !attr->rta_len ) - break; - //lprintf( "Attr:%d %d %d", attLen, attr->rta_len, attr->rta_type ); - switch( attr->rta_type ) { - case NDA_DST: #ifdef DEBUG_MAC_ADDRESS_LOOKUP - lprintf( "Destination Address" ); -#endif - if( ( sa->sa_family = res->rt.ndm_family ) == AF_INET ){ - ((uint32_t*)(sa->sa_data+2))[0] = ((uint32_t*)(attr+1))[0]; - SET_SOCKADDR_LENGTH( sa, IN_SOCKADDR_LENGTH ); - } else { - ((uint32_t*)( sa->sa_data + 2))[0] = 0; - ((uint64_t*)( sa->sa_data + 6))[0] = ((uint64_t*)(attr+1))[0]; - ((uint64_t*)( sa->sa_data + 6))[1] = ((uint64_t*)(attr+1))[1]; - SET_SOCKADDR_LENGTH( sa, IN6_SOCKADDR_LENGTH ); - } - if( FindInBinaryTree( mac_data.pbtAddresses, (uintptr_t)sa ) ) { - duplicated = TRUE; - //DumpAddr( "Duplicate address notification", sa ); - ReleaseAddress( sa ); - Deallocate( struct addressNode *, newAddress ); - break; - } - - break; - case NDA_UNSPEC: - lprintf( "Unknown type" ); - break; - case NDA_LLADDR: + lprintf( "Attr:%d %d %d", attLen, attr->rta_len, attr->rta_type ); +#endif + switch( attr->rta_type ) { + case NDA_DST: + destFound = TRUE; #ifdef DEBUG_MAC_ADDRESS_LOOKUP - lprintf( "Link Layer Address" ); -#endif - memcpy( newAddress->remoteHw, (attr+1), 6 ); - break; - case NDA_PROBES: { - //uint32_t *probes = (uint32_t*)(attr+1); - //lprintf( "Probes: %d %d %d", probes[0], probes[1], probes[2] ); - break; + lprintf( "Destination Address" ); +#endif + if( ( sa->sa_family = res->rt.ndm_family ) == AF_INET ){ + ((uint32_t*)(sa->sa_data+2))[0] = ((uint32_t*)(attr+1))[0]; + SET_SOCKADDR_LENGTH( sa, IN_SOCKADDR_LENGTH ); + } else { + ((uint32_t*)( sa->sa_data + 2))[0] = 0; + ((uint64_t*)( sa->sa_data + 6))[0] = ((uint64_t*)(attr+1))[0]; + ((uint64_t*)( sa->sa_data + 6))[1] = ((uint64_t*)(attr+1))[1]; + SET_SOCKADDR_LENGTH( sa, IN6_SOCKADDR_LENGTH ); } - case NDA_CACHEINFO:{ - struct nda_cacheinfo *ci = (struct nda_cacheinfo*)(attr+1); - + //LogMacAddress( &newAddress ); + if( FindInBinaryTree( mac_data.pbtAddresses, (uintptr_t)sa ) ) { + duplicated = TRUE; #ifdef DEBUG_MAC_ADDRESS_LOOKUP - lprintf( "Cache Info conf:%d used:%d upd:%d cnt:%d", ci->ndm_confirmed, ci->ndm_used, ci->ndm_updated, ci->ndm_refcnt ); + DumpAddr( "Duplicate address notification", sa ); #endif + //ReleaseAddress( sa ); + //Deallocate( struct addressNode *, newAddress ); break; + }else { + DumpAddr( "New address notification", sa ); } - default: - lprintf( "Unknown attribute type: %d", attr->rta_type ); - break; + + break; + case NDA_UNSPEC: + lprintf( "Unknown type" ); + break; + case NDA_LLADDR: + linkFound = TRUE; +#ifdef DEBUG_MAC_ADDRESS_LOOKUP + lprintf( "Link Layer Address" ); +#endif + memcpy( newAddress.remoteHw, (attr+1), 6 ); + break; + case NDA_PROBES: { + uint32_t *probes = (uint32_t*)(attr+1); + lprintf( "Probes: %d %d %d", probes[0], probes[1], probes[2] ); + + break; } - attOfs += attr->rta_len; - attLen -= attr->rta_len; - } while( !duplicated && attLen ); - if( duplicated ) { - break; + case NDA_CACHEINFO:{ + struct nda_cacheinfo *ci = (struct nda_cacheinfo*)(attr+1); + +#ifdef DEBUG_MAC_ADDRESS_LOOKUP + lprintf( "Cache Info conf:%d used:%d upd:%d cnt:%d", ci->ndm_confirmed, ci->ndm_used, ci->ndm_updated, ci->ndm_refcnt ); +#endif + break; + } + default: + lprintf( "Unknown attribute type: %d", attr->rta_type ); + break; } + attOfs += attr->rta_len; + attLen -= attr->rta_len; + } while( !duplicated && attLen ); + if( !linkFound || !destFound || duplicated ) { +//#ifdef DEBUG_MAC_ADDRESS_LOOKUP + if( !duplicated ) + if( !linkFound || !destFound ) + lprintf( "Network Address was incomplete: %s %s", linkFound?"":"Link Address", destFound?"":"Destination Address" ); +//#endif + break; + } else { + lprintf( "duplicated wasn't found?"); + } #ifdef DEBUG_MAC_ADDRESS_LOOKUP - LogMacAddress( newAddress ); + LogMacAddress( &newAddress ); #endif - if( !AddBinaryNode( mac_data.pbtAddresses, (CPOINTER)newAddress, (uintptr_t)newAddress->remote ) ) { - ReleaseAddress( newAddress->remote ); - Deallocate( struct addressNode *, newAddress ); - } - //LogBinary( (uint8_t*)(buf+attOfs), res->nl.nlmsg_len-attOfs ); + struct addressNode *storeAddress = (struct addressNode*)AllocateEx( sizeof( struct addressNode ) DBG_SRC ); + storeAddress[0] = newAddress; + storeAddress->remote = DuplicateAddress( storeAddress->remote ); + if( !AddBinaryNode( mac_data.pbtAddresses, (CPOINTER)storeAddress, (uintptr_t)storeAddress->remote ) ) { + lprintf( "Failed to add address to tree; should have already been marked as duplicated and in tree?" ); +#ifdef DEBUG_MAC_ADDRESS_LOOKUP + LogMacAddress( storeAddress ); +#endif + ReleaseAddress( storeAddress->remote ); + Deallocate( struct addressNode *, storeAddress ); } + //LogBinary( (uint8_t*)(buf+attOfs), res->nl.nlmsg_len-attOfs ); } + } - break; - default: - lprintf( "Default message: type:%d len:%d flg:%d pid:%d seq:%d", res->nl.nlmsg_type, res->nl.nlmsg_len - , res->nl.nlmsg_flags, res->nl.nlmsg_pid, res->nl.nlmsg_seq ); - break; - } - //lprintf( ""); - priorLen += res->nl.nlmsg_len; + break; + default: + lprintf( "Default message: type:%d len:%d flg:%d pid:%d seq:%d", res->nl.nlmsg_type, res->nl.nlmsg_len + , res->nl.nlmsg_flags, res->nl.nlmsg_pid, res->nl.nlmsg_seq ); + break; } - //LogBinary( buf, rstat ); + //lprintf( ""); + priorLen += res->nl.nlmsg_len; } - { - INDEX idx; - PTHREAD waiter; - macTableUpdated = TRUE; - LIST_FORALL( macWaiters, idx, PTHREAD, waiter ) { - WakeThread( waiter ); - } + //LogBinary( buf, rstat ); + } + { + INDEX idx; + PTHREAD waiter; + macTableUpdated = TRUE; + LIST_FORALL( macWaiters, idx, PTHREAD, waiter ) { + WakeThread( waiter ); } - } while( loop ); - - - } - close( rtnetlink_socket ); - return 0; + } + } while( loop ); + } + close( rtnetlink_socket ); + return 0; } @@ -929,7 +976,7 @@ SOCKADDR *AllocAddrEx( DBG_VOIDPASS ) #ifdef DEBUG_ADDRESSES lprintf( "New Length: %d", MAGIC_SOCKADDR_LENGTH); #endif - memset( lpsaAddr, 0, MAGIC_SOCKADDR_LENGTH ); + memset( lpsaAddr, 0, MAGIC_SOCKADDR_LENGTH + 2 * sizeof( uintptr_t ) ); //initialize socket length to something identifiable? ((uintptr_t*)lpsaAddr)[0] = 3; ((uintptr_t*)lpsaAddr)[1] = 0; // string representation of address @@ -1010,6 +1057,12 @@ SOCKADDR* DuplicateAddressEx( SOCKADDR *pAddr DBG_PASS ) if( ((char**)( ( (uintptr_t)pAddr ) - sizeof(char*) ))[0] ) ( (char**)( ( (uintptr_t)dup ) - sizeof( char* ) ) )[0] = strdup( ((char**)( ( (uintptr_t)pAddr ) - sizeof( char* ) ))[0] ); + + //lprintf( "original:"); + //LogBinary( (const uint8_t*)tmp, MAGIC_SOCKADDR_LENGTH + 2*sizeof(uintptr_t) ); + //lprintf( "duplicate:"); + //LogBinary( (const uint8_t*)tmp2, MAGIC_SOCKADDR_LENGTH + 2*sizeof(uintptr_t) ); + return dup; } diff --git a/src/netlib/tcpnetwork.c b/src/netlib/tcpnetwork.c index aebd32813..747d7c35f 100644 --- a/src/netlib/tcpnetwork.c +++ b/src/netlib/tcpnetwork.c @@ -1474,7 +1474,7 @@ int TCPWriteEx(PCLIENT pc DBG_PASS) #endif } else { - lprintf( "nothing was pending??" ); + lprintf( "nothing was pending?" ); nSent = 0; } @@ -1820,7 +1820,7 @@ LOGICAL doTCPWriteV2( PCLIENT lpClient NetworkUnlockEx( lpClient, 0 DBG_SRC ); return TRUE; } - //lprintf("Sending immediate?? %p", lpClient); + //lprintf("Sending immediate? %p", lpClient); if( TCPWriteEx( lpClient DBG_RELAY ) ) { #ifdef VERBOSE_DEBUG diff --git a/src/procreglib/names.c b/src/procreglib/names.c index 4730d930d..4e1e3ce51 100644 --- a/src/procreglib/names.c +++ b/src/procreglib/names.c @@ -55,6 +55,7 @@ struct procreg_local_tag { PTREEDEF Names; PTREEROOT NameIndex; + PTREEROOT NameIndex_literal; PTREEDEFSET TreeNodes; PNAMESET NameSet; @@ -97,7 +98,7 @@ PTREEDEF GetClassTreeEx( PCTREEDEF root //--------------------------------------------------------------------------- -static int CPROC SavedNameCmpEx(CTEXTSTR dst, CTEXTSTR src, size_t srclen) +static int CPROC SavedNameCmpEx(CTEXTSTR dst, CTEXTSTR src, size_t srclen, LOGICAL case_sensitive ) { // NUL does not nessecarily terminate strings // instead slave off the length... @@ -116,9 +117,9 @@ static int CPROC SavedNameCmpEx(CTEXTSTR dst, CTEXTSTR src, size_t srclen) l1 = 0; // no more length .. should have gotten a matched length on dst... break; } - if ( ((f = (TEXTCHAR)(*(dst++))) >= 'A') && (f <= 'Z') ) + if ( !case_sensitive && ((f = (TEXTCHAR)(*(dst++))) >= 'A') && (f <= 'Z') ) f -= ('A' - 'a'); - if ( ((last = (TEXTCHAR)(*(src++))) >= 'A') && (last <= 'Z') ) + if ( !case_sensitive && ((last = (TEXTCHAR)(*(src++))) >= 'A') && (last <= 'Z') ) last -= ('A' - 'a'); --l2; --l1; @@ -156,7 +157,23 @@ static int CPROC SavedNameCmp(CTEXTSTR dst, CTEXTSTR src) if( !dst && src ) return -1; - return SavedNameCmpEx( dst, src, src[-1]-2 ); + return SavedNameCmpEx( dst, src, src[-1]-2, FALSE ); +} +//--------------------------------------------------------------------------- + +static int CPROC SavedNameCmpCS(CTEXTSTR dst, CTEXTSTR src) +{ + //lprintf( "Compare names... (tree) %s,%s", dst, src ); + if( !src && !dst ) + return 0; + if( !src ) { + DebugBreak(); + return 1; + } + if( !dst && src ) + return -1; + + return SavedNameCmpEx( dst, src, src[-1]-2, TRUE ); } //--------------------------------------------------------------------------- @@ -247,8 +264,8 @@ static CTEXTSTR DressName( TEXTSTR buf, CTEXTSTR name ) //--------------------------------------------------------------------------- -static CTEXTSTR DoSaveNameEx( CTEXTSTR stripped, size_t len DBG_PASS ) -#define DoSaveName(a,b) DoSaveNameEx(a,b DBG_SRC ) +static CTEXTSTR DoSaveNameEx( CTEXTSTR stripped, size_t len, LOGICAL case_sensitive DBG_PASS ) +#define DoSaveName(a,b,c) DoSaveNameEx(a,b,c DBG_SRC ) { PNAMESPACE space = l.NameSpace; TEXTCHAR *p = NULL; @@ -272,7 +289,7 @@ static CTEXTSTR DoSaveNameEx( CTEXTSTR stripped, size_t len DBG_PASS ) if( l.flags.bIndexNameTable ) { POINTER p; - p = (POINTER)FindInBinaryTree( l.NameIndex, (uintptr_t)stripped ); + p = (POINTER)FindInBinaryTree( case_sensitive?l.NameIndex_literal:l.NameIndex, (uintptr_t)stripped ); if( p ) { // otherwise it will be single threaded? @@ -295,7 +312,7 @@ static CTEXTSTR DoSaveNameEx( CTEXTSTR stripped, size_t len DBG_PASS ) while( p[0] && len ) { //lprintf( "Compare %s(%d) vs %s(%d)", p+1, p[0], stripped,len ); - if( SavedNameCmpEx( p+1, stripped, len ) == 0 ) + if( SavedNameCmpEx( p+1, stripped, len, case_sensitive ) == 0 ) { // otherwise it will be single threaded? if( procreg_local_private_data.flags.enable_critical_sections ) @@ -353,7 +370,7 @@ static CTEXTSTR DoSaveNameEx( CTEXTSTR stripped, size_t len DBG_PASS ) if( l.flags.bIndexNameTable ) { AddBinaryNode( l.NameIndex, p, (uintptr_t)p ); - //BalanceBinaryTree( l.NameIndex ); + AddBinaryNode( l.NameIndex_literal, p, (uintptr_t)p ); } } // otherwise it will be single threaded? @@ -404,7 +421,7 @@ static CTEXTSTR SaveName( CTEXTSTR name ) StrCpyEx( stripped + 1, name, len + 1 ); // allow +1 length for null after string; otherwise strncpy dropps the nul early stripped[0] = (TEXTCHAR)(len + 2); { - CTEXTSTR result = DoSaveName( stripped + 1, len ); + CTEXTSTR result = DoSaveName( stripped + 1, len, FALSE ); EnqueLink( &l.tmp_names, tmp_namebuf ); return result; } @@ -414,7 +431,7 @@ static CTEXTSTR SaveName( CTEXTSTR name ) //--------------------------------------------------------------------------- CTEXTSTR SaveNameConcatN( CTEXTSTR name1, ... ) -#define SaveNameConcat(n1,n2) SaveNameConcatN( (n1),(n2),NULL ) +//#define SaveNameConcat(n1,n2) SaveNameConcatN( (n1),(n2),NULL ) { // space concat since that's eaten by strip... TEXTCHAR _stripbuffer[256]; @@ -442,23 +459,46 @@ CTEXTSTR SaveNameConcatN( CTEXTSTR name1, ... ) // and add another - final part of string is \0\0 //stripbuffer[len] = 0; //len++; - return DoSaveName( stripbuffer, len ); + return DoSaveName( stripbuffer, len, FALSE ); } //--------------------------------------------------------------------------- CTEXTSTR SaveText( CTEXTSTR text ) -#define SaveNameConcat(n1,n2) SaveNameConcatN( (n1),(n2),NULL ) { size_t len = StrLen( text ); TEXTSTR stripped = NewArray( TEXTCHAR, len + 2 ); CTEXTSTR result; StrCpyEx( stripped + 1, text, len + 1 ); stripped[0] = (TEXTCHAR)(len + 2); - result = DoSaveName( stripped + 1, len); + result = DoSaveName( stripped + 1, len, FALSE); Release( stripped ); return result; } +//--------------------------------------------------------------------------- +CTEXTSTR SaveTextCS( CTEXTSTR text ) +{ + static uint32_t volatile lock; + static char stripped[258]; +#ifdef XCHG + while( XCHG( &lock, 1 ) ) + Relinquish(); +#else + while( LockedExchange( &lock, 1 ) ) + Relinquish(); + +#endif + size_t len = StrLen( text ); + //TEXTSTR stripped = NewArray( TEXTCHAR, len + 2 ); + CTEXTSTR result; + StrCpyEx( stripped + 1, text, len + 1 ); + stripped[0] = (TEXTCHAR)(len + 2); + result = DoSaveName( stripped + 1, len, TRUE); + //Release( stripped ); + lock = 0; + return result; +} + //--------------------------------------------------------------------------- static void CPROC KillName( CPOINTER user, uintptr_t key ) @@ -494,6 +534,7 @@ static void CPROC InitGlobalSpace( POINTER p, uintptr_t size ) // if we have 500 names, 9 searches is much less than 250 avg (*(struct procreg_local_tag*)p).flags.bIndexNameTable = 1; (*(struct procreg_local_tag*)p).NameIndex = CreateBinaryTreeExx( BT_OPT_NODUPLICATES, (int(CPROC *)(uintptr_t,uintptr_t))SavedNameCmp, KillName ); + (*(struct procreg_local_tag*)p).NameIndex_literal = CreateBinaryTreeExx( BT_OPT_NODUPLICATES, (int(CPROC *)(uintptr_t,uintptr_t))SavedNameCmpCS, KillName ); (*(struct procreg_local_tag*)p).reference_count++; } diff --git a/src/sysloglib/syslog.c b/src/sysloglib/syslog.c index 0ba0abfbb..291252390 100644 --- a/src/sysloglib/syslog.c +++ b/src/sysloglib/syslog.c @@ -1205,29 +1205,83 @@ LOGICAL IsBadReadPtr( CPOINTER pointer, uintptr_t len ) # else //--------------------------------------------------------------------------- +struct map_entry { + uintptr_t low; + uintptr_t high; +}; +static int compare_addr( uintptr_t a, uintptr_t b ) +{ + struct map_entry *ma = (struct map_entry *)a; + struct map_entry *mb = (struct map_entry *)b; + if( ma->low < mb->low || ma->high <= mb->low ) + return -1; + if( ma->high > mb->high || ma->low >= mb->high ) + return 1; + // else ma->low <= b && ma->high >= b + return 0; +} + +static int check_addr( uintptr_t psv, uintptr_t key ) +{ + struct map_entry *ma = (struct map_entry *)key; + if( psv < ma->low ) + return -1; + if( psv > ma->high ) + return 1; + return 0; +} + +static void delete_addr( CPOINTER data, uintptr_t key ) +{ + Release( (POINTER)key ); +} + LOGICAL IsBadReadPtr( CPOINTER pointer, uintptr_t len ) { - (void)len; // reference unused. + static size_t last_low, last_high; + static PTREEROOT map_index; + (void)len; // reference unused. static FILE *maps; - //return FALSE; - //DebugBreak(); + uintptr_t ptr = (uintptr_t)pointer; + // quick check last known result + if( ptr >= last_low && ptr <= last_high ) + return FALSE; + if( !map_index ){ + map_index = CreateBinaryTreeExtended( BT_OPT_NODUPLICATES, compare_addr, delete_addr DBG_SRC ); + } if( !maps ) maps = fopen( "/proc/self/maps", "rt" ); - else + else { + struct map_entry *found; + if( found = (struct map_entry *)LocateInBinaryTree( map_index, ptr, check_addr ) ) { + last_low = found->low; + last_high = found->high; + return FALSE; + } fseek( maps, 0, SEEK_SET ); + } //fprintf( stderr, "Testing a pointer..\n" ); if( maps ) { - uintptr_t ptr = (uintptr_t)pointer; + struct map_entry *tmp; char line[256]; while( fgets( line, sizeof(line)-1, maps ) ) { size_t low, high; - sscanf( line, "%zd-%zd" cPTRSZVALfx, &low, &high ); + tmp = (struct map_entry *)Allocate( sizeof( struct map_entry ) ); + sscanf( line, "%zx-%zx", &tmp->low, &tmp->high ); //fprintf( stderr, "%s" "Find: %08" PTRSZVALfx " Low: %08" PTRSZVALfx " High: %08" PTRSZVALfx "\n" // , line, pointer, low, high ); - if( ptr >= low && ptr <= high ) + if( !AddBinaryNode( map_index, tmp, (uintptr_t)tmp ) ) + { + // if the node existed before, then it didn't match... + Release( (POINTER)tmp ); + continue; + } + if( ptr >= tmp->low && ptr <= tmp->high ) { + last_low = tmp->low; + last_high = tmp->high; return FALSE; } } diff --git a/src/timerlib/timers.c b/src/timerlib/timers.c index b87ae64ec..ba10331ca 100644 --- a/src/timerlib/timers.c +++ b/src/timerlib/timers.c @@ -77,7 +77,7 @@ namespace sack { //#define LOG_INSERTS //#define LOG_DISPATCH //#define DEBUG_PIPE_USAGE - +const char *default_thread_name = "ThreadSignal"; typedef struct thread_event THREAD_EVENT; typedef struct thread_event *PTHREAD_EVENT; @@ -117,7 +117,7 @@ struct threads_tag uintptr_t param; uintptr_t (CPROC*proc)( struct threads_tag * ); uintptr_t (CPROC*simple_proc)( POINTER ); - TEXTSTR thread_event_name; // might be not a real thread. + CTEXTSTR thread_event_name; // might be not a real thread. volatile THREAD_ID thread_ident; PTHREAD_EVENT thread_event; #ifdef _WIN32 @@ -270,9 +270,9 @@ static struct my_thread_info* GetThreadTLS( void ) # if defined( WIN32 ) if( !( _MyThreadInfo = (struct my_thread_info*)TlsGetValue( global_timer_structure->my_thread_info_tls ) ) ) { - int old = SetAllocateLogging( FALSE ); + int old = ClearAllocateLogging( FALSE ); TlsSetValue( global_timer_structure->my_thread_info_tls, _MyThreadInfo = New( struct my_thread_info ) ); - SetAllocateLogging( old ); + ResetAllocateLogging( old ); _MyThreadInfo->nThread = 0; _MyThreadInfo->pThread = 0; } @@ -421,12 +421,13 @@ static void InitWakeup( PTHREAD thread, CTEXTSTR event_name ) { #ifdef _DEBUG int prior; - prior = SetAllocateLogging( FALSE ); + prior = ClearAllocateLogging( FALSE ); #endif if( !event_name ) - event_name = "ThreadSignal"; - thread->thread_event_name = StrDup( event_name ); + thread->thread_event_name = event_name = default_thread_name; + else + thread->thread_event_name = (TEXTSTR)StrDup( event_name ); #ifdef _WIN32 if( !thread->thread_event ) { @@ -506,7 +507,7 @@ static void InitWakeup( PTHREAD thread, CTEXTSTR event_name ) #endif #endif #ifdef _DEBUG - SetAllocateLogging( prior ); + ResetAllocateLogging( prior ); #endif } @@ -622,7 +623,7 @@ uintptr_t CPROC check_thread( POINTER p, uintptr_t psv ) THREAD_ID ID = *((THREAD_ID*)psv); //lprintf( "Check thread %016llx %016llx %s", thread->thread_ident, ID, thread->thread_event_name ); if( ( thread->thread_ident == ID ) - && ( StrCmp( thread->thread_event_name, "ThreadSignal" ) == 0 ) ) + && ( StrCmp( thread->thread_event_name, default_thread_name ) == 0 ) ) return (uintptr_t)p; return 0; } @@ -810,44 +811,44 @@ static void InternalWakeableNamedSleepEx( CTEXTSTR name, uint32_t n, LOGICAL th if( pThread ) { #ifdef _WIN32 -#ifndef NO_LOGGING +# ifndef NO_LOGGING if( globalTimerData.flags.bLogSleeps ) _xlprintf(1 DBG_RELAY )( "About to sleep on %d Thread event created...%s:%016llx" , pThread->thread_event->hEvent , pThread->thread_event_name , pThread->thread_ident ); -#endif +# endif if( WaitForSingleObject( pThread->thread_event->hEvent , n==SLEEP_FOREVER?INFINITE:(n) ) != WAIT_TIMEOUT ) { -#ifdef LOG_LATENCY +# ifdef LOG_LATENCY _lprintf(DBG_RELAY)( "Woke up- reset event" ); -#endif +# endif ResetEvent( pThread->thread_event->hEvent ); //if( n == SLEEP_FOREVER ) // DebugBreak(); } -#ifdef LOG_LATENCY +# ifdef LOG_LATENCY else _lprintf(DBG_RELAY)( "Timed out from %d", n ); -#endif +# endif #else { -#ifndef USE_PIPE_SEMS -#ifdef _NO_SEMTIMEDOP_ +# ifndef USE_PIPE_SEMS +# ifdef _NO_SEMTIMEDOP_ int nTimer = 0; if( n != SLEEP_FOREVER ) { //lprintf( "Wakeable sleep in %ld (oneshot, no frequency)", n ); nTimer = AddTimerExx( n, 0, TimerWake, (uintptr_t)pThread DBG_RELAY ); } -#endif -#endif +# endif if( pThread->semaphore == -1 ) { //lprintf( "Invalid semaphore...fixing?" ); InitWakeup( pThread, name ); } +# endif //if( pThread->semaphore != -1 ) { while(1) @@ -1125,7 +1126,7 @@ static void UnmakeThread( void ) //if( ( (*pThread->me)=pThread->next ) ) // pThread->next->me = pThread->me; { - int tmp = SetAllocateLogging( FALSE ); + int tmp = ClearAllocateLogging( FALSE ); #ifdef _WIN32 //lprintf( "Unmaking thread event! on thread %016" _64fx"x", pThread->thread_ident ); CloseHandle( pThread->thread_event->hEvent ); @@ -1142,7 +1143,8 @@ static void UnmakeThread( void ) #else closesem( (POINTER)pThread, 0 ); #endif - Deallocate( TEXTSTR, pThread->thread_event_name ); + if( pThread->thread_event_name != default_thread_name ) + Deallocate( CTEXTSTR, pThread->thread_event_name ); #ifdef _WIN32 Deallocate( TEXTSTR, pThread->thread_event->name ); if( global_timer_structure ) @@ -1151,7 +1153,7 @@ static void UnmakeThread( void ) #endif if( global_timer_structure ) DeleteFromSet( THREAD, globalTimerData.threadset, pThread ) /*Release( pThread )*/; - SetAllocateLogging( tmp ); + ResetAllocateLogging( tmp ); } } globalTimerData.lock_thread_create = 0; diff --git a/src/typelib/typecode.c b/src/typelib/typecode.c index 29cf96635..9e4181d09 100644 --- a/src/typelib/typecode.c +++ b/src/typelib/typecode.c @@ -805,7 +805,7 @@ namespace sack { INDEX size; int prior_logging; size = MY_OFFSETOF( pplq, pNode[plq->Cnt + entries] ); - prior_logging = SetAllocateLogging( FALSE ); + prior_logging = ClearAllocateLogging( FALSE ); plqNew = (PLINKQUEUE)AllocateEx( size DBG_RELAY ); plqNew->Cnt = plq->Cnt + entries; plqNew->Bottom = 0; @@ -824,7 +824,7 @@ namespace sack { //need to make sure plq is always valid; can be trying to get a lock (*pplq) = plqNew; Release( plq ); - SetAllocateLogging( prior_logging ); + ResetAllocateLogging( prior_logging ); } #if USE_CUSTOM_ALLOCER if (_link_queue_local)