// DebugAllocator::unaligned_realloc void * DebugAllocator::unaligned_realloc( void * i_address, size_t i_new_size ) { const size_t old_size = _do_check( i_address ); // the return value is valid only if m_heading_nomansland_size >= sizeof( void * ) void * const old_block = address_sub( i_address, m_heading_nomansland_size ); void * const new_block = dest_allocator().unaligned_realloc( old_block, i_new_size + m_heading_nomansland_size + m_tailing_nomansland_size ); if( new_block == nullptr ) return nullptr; void * const user_block = address_add( new_block, m_heading_nomansland_size ); void * const user_block_end = address_add( user_block, i_new_size ); if( old_block != new_block ) { _set_nomansland( new_block, m_heading_nomansland_size, user_block ); } _set_nomansland( user_block_end, m_tailing_nomansland_size, user_block ); if( m_heading_nomansland_size >= sizeof( void * ) ) { *reinterpret_cast< void * * >( new_block ) = _invert_address( user_block_end ); if( i_new_size > old_size ) _fill_memory( m_new_memory_fill_mode, address_add( user_block, old_size ), i_new_size - old_size ); } return user_block; }
static void LocalIAmHandler( uint8_t * service_request, uint16_t service_len, BACNET_ADDRESS * src) { int len = 0; uint32_t device_id = 0; unsigned max_apdu = 0; int segmentation = 0; uint16_t vendor_id = 0; (void) src; (void) service_len; len = iam_decode_service_request(service_request, &device_id, &max_apdu, &segmentation, &vendor_id); fprintf(stderr, "Received I-Am Request"); if (len != -1) { fprintf(stderr, " from %u!\n", device_id); address_add(device_id, max_apdu, src); } else fprintf(stderr, "!\n"); return; }
// DebugAllocator::_do_check size_t DebugAllocator::_do_check( void * i_address ) const { MEMO_ASSERT( i_address != nullptr ); // i_address can't be null void * const block = address_sub( i_address, m_heading_nomansland_size ); size_t size = 0; if( m_heading_nomansland_size >= sizeof( void * ) ) { _check_nomansland( block, m_heading_nomansland_size - sizeof( void * ), address_add( i_address, sizeof( void * ) ) ); void * user_block_end = _invert_address( *reinterpret_cast< void * * >( block ) ); size = address_diff( user_block_end, i_address ); _check_nomansland( user_block_end, m_tailing_nomansland_size, i_address ); } else { _check_nomansland( block, m_heading_nomansland_size, i_address ); } return size; }
/** Handler for I-Am responses. * Will add the responder to our cache, or update its binding. * @ingroup DMDDB * @param service_request [in] The received message to be handled. * @param service_len [in] Length of the service_request message. * @param src [in] The BACNET_ADDRESS of the message's source. */ void handler_i_am_add( uint8_t * service_request, uint16_t service_len, BACNET_ADDRESS * src) { int len = 0; uint32_t device_id = 0; unsigned max_apdu = 0; int segmentation = 0; uint16_t vendor_id = 0; (void) service_len; len = iam_decode_service_request(service_request, &device_id, &max_apdu, &segmentation, &vendor_id); #if PRINT_ENABLED fprintf(stderr, "Received I-Am Request"); #endif if (len != -1) { #if PRINT_ENABLED fprintf(stderr, " from %lu, MAC = %d.%d.%d.%d.%d.%d\n", (unsigned long) device_id, src->mac[0], src->mac[1], src->mac[2], src->mac[3], src->mac[4], src->mac[5]); #endif address_add(device_id, max_apdu, src); emit_iam(device_id, max_apdu, segmentation, vendor_id, src); } else { #if PRINT_ENABLED fprintf(stderr, ", but unable to decode it.\n"); #endif } return; }
static void address_file_init( const char *pFilename) { FILE *pFile = NULL; /* stream pointer */ char line[256] = { "" }; /* holds line from file */ long device_id = 0; unsigned snet = 0; unsigned max_apdu = 0; char mac_string[80] = { "" }, sadr_string[80] = { ""}; BACNET_ADDRESS src = { 0 }; BACNET_MAC_ADDRESS mac = { 0 }; int index = 0; pFile = fopen(pFilename, "r"); if (pFile) { while (fgets(line, (int) sizeof(line), pFile) != NULL) { /* ignore comments */ if (line[0] != ';') { if (sscanf(line, "%7ld %79s %5u %79s %4u", &device_id, &mac_string[0], &snet, &sadr_string[0], &max_apdu) == 5) { if (address_mac_from_ascii(&mac, mac_string)) { src.mac_len = mac.len; for (index = 0; index < MAX_MAC_LEN; index++) { src.mac[index] = mac.adr[index]; } } src.net = (uint16_t) snet; if (snet) { if (address_mac_from_ascii(&mac, sadr_string)) { src.len = mac.len; for (index = 0; index < MAX_MAC_LEN; index++) { src.adr[index] = mac.adr[index]; } } } else { src.len = 0; for (index = 0; index < MAX_MAC_LEN; index++) { src.adr[index] = 0; } } address_add((uint32_t) device_id, max_apdu, &src); address_set_device_TTL((uint32_t) device_id, 0, true); /* Mark as static entry */ } } } fclose(pFile); } return; }
// DebugAllocator::unaligned_alloc void * DebugAllocator::unaligned_alloc( size_t i_size ) { void * const block = dest_allocator().unaligned_alloc( i_size + m_heading_nomansland_size + m_tailing_nomansland_size ); if( block == nullptr ) return nullptr; void * const user_block = address_add( block, m_heading_nomansland_size ); void * const user_block_end = address_add( user_block, i_size ); _set_nomansland( block, m_heading_nomansland_size, user_block ); if( m_heading_nomansland_size >= sizeof( void * ) ) *reinterpret_cast< void * * >( block ) = _invert_address( user_block_end ); _fill_memory( m_new_memory_fill_mode, user_block, i_size ); _set_nomansland( user_block_end, m_tailing_nomansland_size, user_block ); return user_block; }
// DebugAllocator::_set_nomansland void DebugAllocator::_set_nomansland( void * i_start_address, size_t i_size, void * i_parameter ) { MEMO_ASSERT( ( i_size % sizeof(uintptr_t) ) == 0 ); unsigned char * dest = static_cast< unsigned char * >( i_start_address ); unsigned char * end = static_cast< unsigned char * >( address_add( i_start_address, i_size ) ); unsigned char value = ~static_cast<unsigned char>( ( reinterpret_cast< uintptr_t >( i_parameter ) & std::numeric_limits<unsigned char>::max() ) ); while( dest < end ) { *dest = value; value = ( value + 17 ) & std::numeric_limits<unsigned char>::max(); dest++; } }
void testAddress( Test * pTest) { unsigned i, count; BACNET_ADDRESS src; uint32_t device_id = 0; unsigned max_apdu = 480; BACNET_ADDRESS test_address; uint32_t test_device_id = 0; unsigned test_max_apdu = 0; /* create a fake address database */ for (i = 0; i < MAX_ADDRESS_CACHE; i++) { set_address(i, &src); device_id = i * 255; address_add(device_id, max_apdu, &src); count = address_count(); ct_test(pTest, count == (i + 1)); } for (i = 0; i < MAX_ADDRESS_CACHE; i++) { device_id = i * 255; set_address(i, &src); /* test the lookup by device id */ ct_test(pTest, address_get_by_device(device_id, &test_max_apdu, &test_address)); ct_test(pTest, test_max_apdu == max_apdu); ct_test(pTest, bacnet_address_same(&test_address, &src)); ct_test(pTest, address_get_by_index(i, &test_device_id, &test_max_apdu, &test_address)); ct_test(pTest, test_device_id == device_id); ct_test(pTest, test_max_apdu == max_apdu); ct_test(pTest, bacnet_address_same(&test_address, &src)); ct_test(pTest, address_count() == MAX_ADDRESS_CACHE); /* test the lookup by MAC */ ct_test(pTest, address_get_device_id(&src, &test_device_id)); ct_test(pTest, test_device_id == device_id); } for (i = 0; i < MAX_ADDRESS_CACHE; i++) { device_id = i * 255; address_remove_device(device_id); ct_test(pTest, !address_get_by_device(device_id, &test_max_apdu, &test_address)); count = address_count(); ct_test(pTest, count == (MAX_ADDRESS_CACHE - i - 1)); } }
// DebugAllocator::_check_nomansland void DebugAllocator::_check_nomansland( const void * i_start_address, size_t i_size, void * i_parameter ) { MEMO_ASSERT( ( i_size % sizeof(uintptr_t) ) == 0 ); const unsigned char * dest = static_cast< const unsigned char * >( i_start_address ); const unsigned char * end = static_cast< const unsigned char * >( address_add( i_start_address, i_size ) ); unsigned char value = ~static_cast<unsigned char>( ( reinterpret_cast< uintptr_t >( i_parameter ) & std::numeric_limits<unsigned char>::max() ) ); while( dest < end ) { if( *dest != value ) { memo_externals::output_message( "no mans land memory corrupted\n" ); memo_externals::debug_break(); } value = ( value + 17 ) & std::numeric_limits<unsigned char>::max(); dest++; } }
// DebugAllocator::_check_memory void DebugAllocator::_check_memory( FillMode i_mode, const void * i_start_address, size_t i_size ) { switch( i_mode ) { case eNone: break; case eNaNs: { typedef memo_externals::FloatNanIntegerRepresentationType FloatIntType; const void * const end = address_add( i_start_address, i_size ); const FloatIntType * dest = static_cast<const FloatIntType *>( i_start_address ); for( ; dest < end; dest++ ) { if( *dest != memo_externals::g_float_nan_integer_representation ) { memo_externals::output_message( "memory corruption\n" ); memo_externals::debug_break(); } } const char * c_dest = reinterpret_cast<const char *>( dest ); for( ; c_dest < end; c_dest++ ) { if( *c_dest != 'N' ) { memo_externals::output_message( "memory corruption\n" ); memo_externals::debug_break(); } } break; } case ePseudoRandom: { const void * const end = address_add( i_start_address, i_size ); const uint32_t * dest = static_cast<const uint32_t *>( i_start_address ); uint32_t random = static_cast<const uint32_t>( reinterpret_cast<uintptr_t>( dest ) &0xFFFFFFFF ); for( ; dest < end; dest++ ) { generate_rand_32( random ); if( *dest != random ) { memo_externals::output_message( "memory corruption\n" ); memo_externals::debug_break(); } } const char * c_dest = reinterpret_cast<const char *>( dest ); for( ; c_dest < end; c_dest++ ) { if( *c_dest != 'D' ) { memo_externals::output_message( "memory corruption\n" ); memo_externals::debug_break(); } } break; } case eBadF00d: { const void * const end = address_add( i_start_address, i_size ); const uint32_t * dest = static_cast<const uint32_t *>( i_start_address ); for( ; dest < end; dest++ ) { if( *dest != 0xBADF00D ) { memo_externals::output_message( "memory corruption\n" ); memo_externals::debug_break(); } } const char * c_dest = reinterpret_cast<const char *>( dest ); for( ; c_dest < end; c_dest++ ) { if( *c_dest != 'B' ) { memo_externals::output_message( "memory corruption\n" ); memo_externals::debug_break(); } } break; } default: MEMO_ASSERT( false ); // unknown value } }
// DebugAllocator::_fill_memory void DebugAllocator::_fill_memory( FillMode i_mode, void * i_start_address, size_t i_size ) { switch( i_mode ) { case eNone: break; case eNaNs: { typedef memo_externals::FloatNanIntegerRepresentationType FloatIntType; void * start = upper_align( i_start_address, MEMO_ALIGNMENT_OF(uint32_t) ); void * const end = address_add( i_start_address, i_size ); void * const aligned_end = lower_align( end, MEMO_ALIGNMENT_OF(uint32_t) ); uint32_t * dest = static_cast<uint32_t *>( start ); for( ; dest < aligned_end; dest++ ) { *dest = memo_externals::g_float_nan_integer_representation; } char * c_dest = reinterpret_cast<char *>( dest ); for( ; c_dest < end; c_dest++ ) { *c_dest = 'N'; } break; } case ePseudoRandom: { void * start = upper_align( i_start_address, MEMO_ALIGNMENT_OF(uint32_t) ); void * const end = address_add( i_start_address, i_size ); void * const aligned_end = lower_align( end, MEMO_ALIGNMENT_OF(uint32_t) ); uint32_t * dest = static_cast<uint32_t *>( start ); uint32_t random = static_cast<uint32_t>( reinterpret_cast<uintptr_t>( dest ) &0xFFFFFFFF ); for( ; dest < aligned_end; dest++ ) { generate_rand_32( random ); *dest = random; } char * c_dest = reinterpret_cast<char *>( dest ); for( ; c_dest < end; c_dest++ ) { *c_dest = 'D'; } break; } case eBadF00d: { void * start = upper_align( i_start_address, MEMO_ALIGNMENT_OF(uint32_t) ); void * const end = address_add( i_start_address, i_size ); void * const aligned_end = lower_align( end, MEMO_ALIGNMENT_OF(uint32_t) ); uint32_t * dest = static_cast<uint32_t *>( start ); for( ; dest < aligned_end; dest++ ) { *dest = 0xBADF00D; } char * c_dest = reinterpret_cast<char *>( dest ); for( ; c_dest < end; c_dest++ ) { *c_dest = 'B'; } break; } default: MEMO_ASSERT( false ); // unknown value } }