TEST(dynmem, test_both_allocs_with_hole_usage) { uint16_t size = 112; mem_stat_t info; void *p[size]; uint8_t *heap = (uint8_t*)malloc(size); CHECK(NULL != heap); reset_heap_error(); ns_dyn_mem_init(heap, size, &heap_fail_callback, &info); CHECK(!heap_have_failed()); void *ptr = ns_dyn_mem_alloc(15); void *ptr2 = ns_dyn_mem_alloc(4); ns_dyn_mem_free(ptr); ns_dyn_mem_free(ptr2); CHECK(info.heap_sector_allocated_bytes == 0); void *ptr3 = ns_dyn_mem_temporary_alloc(15); void *ptr4 = ns_dyn_mem_temporary_alloc(5); ns_dyn_mem_free(ptr3); ns_dyn_mem_free(ptr4); CHECK(info.heap_sector_allocated_bytes == 0); free(heap); }
TEST(dynmem, not_negative_stats) { uint16_t size = 1000; mem_stat_t info; uint8_t *heap = (uint8_t*)malloc(size); void *p; CHECK(NULL != heap); reset_heap_error(); ns_dyn_mem_init(heap, size, &heap_fail_callback, &info); CHECK(!heap_have_failed()); CHECK(info.heap_sector_allocated_bytes == 0); ns_dyn_mem_alloc(8); p = ns_dyn_mem_alloc(8); ns_dyn_mem_alloc(8); CHECK(info.heap_sector_allocated_bytes >= 24); int16_t last_value = info.heap_sector_allocated_bytes; ns_dyn_mem_free(p); CHECK(info.heap_sector_allocated_bytes >= 16); CHECK(info.heap_sector_allocated_bytes < last_value); last_value = info.heap_sector_allocated_bytes; for (int i=0; i<10; i++) { p = ns_dyn_mem_alloc(1); ns_dyn_mem_free(p); } CHECK(info.heap_sector_allocated_bytes == last_value); free(heap); }
TEST(dynmem, ns_dyn_mem_alloc) { uint16_t size = 1000; mem_stat_t info; void *p[size]; uint8_t *heap = (uint8_t*)malloc(size); CHECK(NULL != heap); reset_heap_error(); ns_dyn_mem_init(heap, size, &heap_fail_callback, &info); CHECK(!heap_have_failed()); int block = 1; int i; for (i=0; i<size; i++) { p[i] = ns_dyn_mem_alloc(block); if (!p[i]) break; } CHECK(!heap_have_failed()); CHECK(info.heap_alloc_fail_cnt == 1); CHECK(info.heap_sector_alloc_cnt == i); CHECK(info.heap_sector_allocated_bytes == info.heap_sector_allocated_bytes_max); for (; i>=0; i--) { ns_dyn_mem_free(p[i]); } CHECK(!heap_have_failed()); CHECK(info.heap_sector_alloc_cnt == 0); free(heap); }
coap_security_t *coap_security_create(int8_t socket_id, int8_t timer_id, uint8_t *address_ptr, uint16_t port, SecureConnectionMode mode, send_cb *send_cb, receive_cb *receive_cb, start_timer_cb *start_timer_cb, timer_status_cb *timer_status_cb) { if( !address_ptr || send_cb == NULL || receive_cb == NULL || start_timer_cb == NULL || timer_status_cb == NULL){ return NULL; } coap_security_t *this = ns_dyn_mem_alloc(sizeof(coap_security_t)); if( !this ){ return NULL; } memset(this, 0, sizeof(coap_security_t)); if( -1 == coap_security_handler_init(this) ){ ns_dyn_mem_free(this); return NULL; } this->_remote_port = port; memcpy(this->_remote_address, address_ptr, 16); this->_conn_mode = mode; memset(this->_pw, 0, 64); this->_pw_len = 0; this->_socket_id = socket_id; this->_timer_id = timer_id; this->_send_cb = send_cb; this->_receive_cb = receive_cb; this->_start_timer_cb = start_timer_cb; this->_timer_status_cb = timer_status_cb; return this; }
int8_t ws_pae_controller_init(protocol_interface_info_entry_t *interface_ptr) { if (!interface_ptr) { return -1; } if (ws_pae_controller_get(interface_ptr) != NULL) { return 0; } pae_controller_t *controller = ns_dyn_mem_alloc(sizeof(pae_controller_t)); if (!controller) { return -1; } memset(controller->target_eui_64, 0, 8); memset(controller->br_eui_64, 0, 8); controller->interface_ptr = interface_ptr; controller->auth_completed = NULL; controller->key_insert = NULL; controller->pae_delete = NULL; controller->pae_timer = NULL; controller->pae_br_addr_write = NULL; controller->pae_br_addr_read = NULL; sec_prot_keys_gtks_init(&controller->gtks); sec_prot_certs_init(&controller->certs); ns_list_add_to_end(&pae_controller_list, controller); return 0; }
int8_t net_nvm_wpan_params_storage_enable(int8_t interface_id, wpan_params_updated *nvm_update_cb, wpan_params_get *nvm_get_cb) { protocol_interface_info_entry_t *cur = protocol_stack_interface_info_get_by_id(interface_id); if (!cur || !nvm_update_cb || !nvm_get_cb) { return -1; } if (cur->nwk_wpan_nvm_api) { return 0; } cur->nwk_wpan_nvm_api = ns_dyn_mem_alloc(sizeof(nwk_wpan_nvm_api_t)); if (!cur->nwk_wpan_nvm_api) { return -2; } cur->nwk_wpan_nvm_api->interface = cur; cur->nwk_wpan_nvm_api->params.pan_id = 0xffff; cur->nwk_wpan_nvm_api->NVM_GET = nvm_get_cb; cur->nwk_wpan_nvm_api->NVM_PUSH = nvm_update_cb; cur->nwk_wpan_nvm_api->nvm_params_get_cb = nwk_nvm_params_get_cb; cur->nwk_wpan_nvm_api->nvm_params_update_cb = nwk_nvm_params_update_cb; return 0; }
coap_security_t *coap_security_create(int8_t socket_id, int8_t timer_id, void *handle, SecureConnectionMode mode, send_cb *socket_cb, receive_cb *receive_data_cb, start_timer_cb *timer_start_cb, timer_status_cb *timer_stat_cb) { if (socket_cb == NULL || receive_data_cb == NULL || timer_start_cb == NULL || timer_stat_cb == NULL) { return NULL; } coap_security_t *this = ns_dyn_mem_alloc(sizeof(coap_security_t)); if( !this ){ return NULL; } memset(this, 0, sizeof(coap_security_t)); if (-1 == coap_security_handler_init(this)) { ns_dyn_mem_free(this); return NULL; } this->_handle = handle; this->_conn_mode = mode; memset(this->_pw, 0, 64); this->_pw_len = 0; this->_socket_id = socket_id; this->_timer_id = timer_id; this->_send_cb = socket_cb; this->_receive_cb = receive_data_cb; this->_start_timer_cb = timer_start_cb; this->_timer_status_cb = timer_stat_cb; return this; }
/** * Load balance border router class allocate */ static lb_monitor_internal_t *lb_border_router_api_allocate(lb_internal_t *api) { if (!api->lb_border_router) { api->lb_border_router = ns_dyn_mem_alloc(sizeof(lb_monitor_internal_t)); } return api->lb_border_router; }
TEST(dynmem, corrupted_memory) { uint16_t size = 1000; mem_stat_t info; uint8_t *heap = (uint8_t*)malloc(size); uint8_t *ptr = heap; CHECK(NULL != heap); reset_heap_error(); ns_dyn_mem_init(heap, size, &heap_fail_callback, &info); CHECK(!heap_have_failed()); int *pt = (int *)ns_dyn_mem_alloc(8); CHECK(!heap_have_failed()); pt -= 2; *pt = 0; ns_dyn_mem_alloc(8); CHECK(NS_DYN_MEM_HEAP_SECTOR_CORRUPTED == current_heap_error); free(heap); }
/* * \brief Configure mesh network * */ void thread_tasklet_configure_and_connect_to_network(void) { int8_t status; link_configuration_s* temp_link_config=NULL; if (MBED_CONF_MBED_MESH_API_THREAD_DEVICE_TYPE == MESH_DEVICE_TYPE_THREAD_MINIMAL_END_DEVICE) { thread_tasklet_data_ptr->operating_mode = NET_6LOWPAN_HOST; } else if (MBED_CONF_MBED_MESH_API_THREAD_DEVICE_TYPE == MESH_DEVICE_TYPE_THREAD_SLEEPY_END_DEVICE) { thread_tasklet_data_ptr->operating_mode = NET_6LOWPAN_SLEEPY_HOST; } else { thread_tasklet_data_ptr->operating_mode = NET_6LOWPAN_ROUTER; } arm_nwk_interface_configure_6lowpan_bootstrap_set( thread_tasklet_data_ptr->nwk_if_id, thread_tasklet_data_ptr->operating_mode, NET_6LOWPAN_THREAD); thread_tasklet_data_ptr->channel_list.channel_page = (channel_page_e)MBED_CONF_MBED_MESH_API_THREAD_CONFIG_CHANNEL_PAGE; thread_tasklet_data_ptr->channel_list.channel_mask[0] = MBED_CONF_MBED_MESH_API_THREAD_CONFIG_CHANNEL_MASK; TRACE_DETAIL("channel page: %d", thread_tasklet_data_ptr->channel_list.channel_page); TRACE_DETAIL("channel mask: 0x%.8lx", thread_tasklet_data_ptr->channel_list.channel_mask[0]); // PSKd const char PSKd[] = MBED_CONF_MBED_MESH_API_THREAD_PSKD; MBED_ASSERT(sizeof(PSKd) > 5 && sizeof(PSKd) < 33); char *dyn_buf = ns_dyn_mem_alloc(sizeof(PSKd)); strcpy(dyn_buf, PSKd); ns_dyn_mem_free(device_configuration.PSKd_ptr); device_configuration.PSKd_ptr = (uint8_t*)dyn_buf; device_configuration.PSKd_len = sizeof(PSKd) - 1; if (true == MBED_CONF_MBED_MESH_API_THREAD_USE_STATIC_LINK_CONFIG) { read_link_configuration(); temp_link_config = &thread_tasklet_data_ptr->link_config; } thread_management_node_init(thread_tasklet_data_ptr->nwk_if_id, &thread_tasklet_data_ptr->channel_list, &device_configuration, temp_link_config); status = arm_nwk_interface_up(thread_tasklet_data_ptr->nwk_if_id); if (status >= 0) { thread_tasklet_data_ptr->tasklet_state = TASKLET_STATE_BOOTSTRAP_STARTED; tr_info("Start Thread bootstrap (%s mode)", thread_tasklet_data_ptr->operating_mode == NET_6LOWPAN_SLEEPY_HOST ? "SED" : "Router"); } else { thread_tasklet_data_ptr->tasklet_state = TASKLET_STATE_BOOTSTRAP_FAILED; tr_err("Bootstrap start failed, %d", status); thread_tasklet_network_state_changed(MESH_BOOTSTRAP_START_FAILED); } }
void nd_tasklet_init(void) { if (tasklet_data_ptr == NULL) { // memory allocation will not fail as memory was just initialized tasklet_data_ptr = ns_dyn_mem_alloc(sizeof(tasklet_data_str_t)); tasklet_data_ptr->tasklet_state = TASKLET_STATE_CREATED; tasklet_data_ptr->network_interface_id = INVALID_INTERFACE_ID; } }
void thread_tasklet_init(void) { if (thread_tasklet_data_ptr == NULL) { thread_tasklet_data_ptr = ns_dyn_mem_alloc(sizeof(thread_tasklet_data_str_t)); memset(thread_tasklet_data_ptr, 0, sizeof(thread_tasklet_data_str_t)); thread_tasklet_data_ptr->tasklet_state = TASKLET_STATE_CREATED; thread_tasklet_data_ptr->nwk_if_id = INVALID_INTERFACE_ID; } }
TEST(dynmem, test_merge_corrupted_previous_block) { uint16_t size = 1000; uint8_t *heap = (uint8_t*)malloc(size); uint8_t *p; CHECK(NULL != heap); reset_heap_error(); ns_dyn_mem_init(heap, size, &heap_fail_callback, NULL); CHECK(!heap_have_failed()); int *ptr = (int *)ns_dyn_mem_alloc(4); int *ptr2 = (int *)ns_dyn_mem_alloc(4); ns_dyn_mem_free(ptr); ptr = ptr2 - 2; *ptr = -2; ns_dyn_mem_free(ptr2); CHECK(NS_DYN_MEM_HEAP_SECTOR_CORRUPTED == current_heap_error); free(heap); }
TEST(dynmem, no_big_enough_sector) { uint16_t size = 112; mem_stat_t info; uint8_t *heap = (uint8_t*)malloc(size); uint8_t *ptr = heap; CHECK(NULL != heap); reset_heap_error(); ns_dyn_mem_init(heap, size, &heap_fail_callback, &info); CHECK(!heap_have_failed()); int *pt = (int *)ns_dyn_mem_alloc(8); pt = (int *)ns_dyn_mem_alloc(8); ns_dyn_mem_alloc(8); ns_dyn_mem_temporary_alloc(8); ns_dyn_mem_temporary_alloc(8); ns_dyn_mem_free(pt); pt = (int *)ns_dyn_mem_temporary_alloc(32); CHECK(NULL == pt); free(heap); }
pana_session_t *pana_session_allocate(void) { pana_session_t *p_session = ns_dyn_mem_alloc(sizeof(pana_session_t)); if (p_session) { memset(p_session, 0, sizeof(pana_session_t)); p_session->session_ready = false; p_session->key_warp = false; p_session->user_server = false; p_session->eap_id_seq = randLIB_get_8bit(); //Take Random EAP ID } return p_session; }
TEST(dynmem, different_sizes) { reset_heap_error(); for (uint16_t size = 1000; size<32768; size++) { mem_stat_t info; uint8_t *heap = (uint8_t*)malloc(size); ns_dyn_mem_init(heap, size, &heap_fail_callback, &info); CHECK(info.heap_sector_size >= (size-72)); CHECK(!heap_have_failed()); CHECK(ns_dyn_mem_alloc(10)); free(heap); } }
static dhcpv6_gua_server_entry_s *libdhcpv6_server_entry_allocate(void) { dhcpv6_gua_server_entry_s *entry = ns_dyn_mem_alloc(sizeof(dhcpv6_gua_server_entry_s)); if (entry) { entry->clientIdSequence = 0; entry->enableAddressMapping = false; entry->enableAddressAutonous = true; entry->clientIdDefaultSuffics = 0x0000000; entry->maxSuppertedClients = 200; entry->validLifetime = 7200; ns_list_init(&entry->allocatedAddressList); } return entry; }
TEST(dynmem, test_invalid_pointer_freed) { uint16_t size = 92; uint8_t *heap = (uint8_t*)malloc(size); CHECK(NULL != heap); reset_heap_error(); ns_dyn_mem_init(heap, size, &heap_fail_callback, NULL); int *ptr = (int *)ns_dyn_mem_alloc(4); ptr--; *ptr = 16; ptr++; ns_dyn_mem_free(ptr); CHECK(NS_DYN_MEM_POINTER_NOT_VALID == current_heap_error); free(heap); }
TEST(dynmem, too_big) { uint16_t size = 1000; mem_stat_t info; uint8_t *heap = (uint8_t*)malloc(size); uint8_t *ptr = heap; CHECK(NULL != heap); reset_heap_error(); ns_dyn_mem_init(heap, size, &heap_fail_callback, &info); CHECK(!heap_have_failed()); ns_dyn_mem_alloc(size); CHECK(heap_have_failed()); CHECK(NS_DYN_MEM_ALLOCATE_SIZE_NOT_VALID == current_heap_error); free(heap); }
uint8_t thread_tasklet_device_pskd_set(const char *pskd) { int len = strlen(pskd); if(len < 6 || len > 32) { return MESH_ERROR_PARAM; } char *dyn_buf = ns_dyn_mem_alloc(strlen(pskd)+1); if (!dyn_buf) { return MESH_ERROR_MEMORY; } strcpy(dyn_buf, pskd); ns_dyn_mem_free(device_configuration.PSKd_ptr); device_configuration.PSKd_ptr = (uint8_t*)dyn_buf; device_configuration.PSKd_len = strlen(pskd); return 0; }
thread_network_dynamic_data_entry_t *thread_network_synch_create(int8_t interfaceId) { thread_network_dynamic_data_entry_t *newEntry = ns_dyn_mem_alloc(sizeof(thread_network_dynamic_data_entry_t)); if (newEntry) { tr_debug("Allocate New Store"); newEntry->interfaceId = interfaceId; newEntry->networ_dynamic_data_parameters.thread_attached_state = THREAD_STATE_NETWORK_DISCOVER; newEntry->networ_dynamic_data_parameters.shortAddress = 0xfffe; newEntry->networ_dynamic_data_parameters.leader_private_data = NULL; newEntry->networ_dynamic_data_parameters.thread_endnode_parent = NULL; for (int i = 0; i < THREAD_MAX_CHILD_COUNT; ++i) { memset(&newEntry->networ_dynamic_data_parameters.children[i], 0, sizeof(thread_sync_child_info_t)); } ns_list_add_to_end(&thread_network_dynamic_data_info, newEntry); } return newEntry; }
// XXX: a ns_dyn_mem_realloc() would be nice to have uint8_t *mac_helper_beacon_payload_reallocate(protocol_interface_info_entry_t *interface, uint8_t len) { if (len == interface->mac_parameters->mac_beacon_payload_size) { // no change to size, return the existing buff //Set allways length to zero for safe beacon payload manipulate mac_helper_beacon_payload_length_set_to_mac(interface, 0); return interface->mac_parameters->mac_beacon_payload; } if (len == 0) { //SET MAC beacon payload to length to zero mac_helper_beacon_payload_length_set_to_mac(interface, 0); ns_dyn_mem_free(interface->mac_parameters->mac_beacon_payload); interface->mac_parameters->mac_beacon_payload = NULL; interface->mac_parameters->mac_beacon_payload_size = 0; return NULL; } tr_debug("mac_helper_beacon_payload_reallocate, old len: %d, new: %d", interface->mac_parameters->mac_beacon_payload_size, len); uint8_t *temp_buff = ns_dyn_mem_alloc(len); if (temp_buff == NULL) { // no need to proceed, could not allocate more space return NULL; } //SET MAC beacon payload to length to zero mac_helper_beacon_payload_length_set_to_mac(interface, 0); // copy data into new buffer before freeing old one if (interface->mac_parameters->mac_beacon_payload_size > 0) { const uint8_t min_len = MIN(len, interface->mac_parameters->mac_beacon_payload_size); memcpy(temp_buff, interface->mac_parameters->mac_beacon_payload, min_len); ns_dyn_mem_free(interface->mac_parameters->mac_beacon_payload); } //Set New Length and pointer to MAC interface->mac_parameters->mac_beacon_payload = temp_buff; interface->mac_parameters->mac_beacon_payload_size = len; return interface->mac_parameters->mac_beacon_payload; }
/** * Allocate notified network class */ static bool load_balance_network_class_allocate(lb_internal_t *lb_store_ptr, uint16_t beacon_max_payload_length) { ns_dyn_mem_free(lb_store_ptr->notified_network); lb_store_ptr->beacon_max_payload_length = 0; if (beacon_max_payload_length) { lb_store_ptr->notified_network = ns_dyn_mem_alloc(sizeof(lb_network_t) + beacon_max_payload_length); if (!lb_store_ptr->notified_network) { return false; } lb_store_ptr->notified_network->network_switch_accepted = false; lb_store_ptr->notified_network->priority = 0xff; lb_store_ptr->notified_network->state = LB_NWK_SWITCH_IDLE; lb_store_ptr->notified_network->state_timer = 0; lb_store_ptr->beacon_max_payload_length = beacon_max_payload_length; } return true; }
TEST(dynmem, over_by_one) { uint16_t size = 1000; mem_stat_t info; uint8_t *heap = (uint8_t*)malloc(size); uint8_t *p; CHECK(NULL != heap); reset_heap_error(); ns_dyn_mem_init(heap, size, &heap_fail_callback, &info); CHECK(!heap_have_failed()); p = (uint8_t *)ns_dyn_mem_alloc(100); CHECK(p); p[100] = 0xff; ns_dyn_mem_free(p); CHECK(heap_have_failed()); CHECK(NS_DYN_MEM_HEAP_SECTOR_CORRUPTED == current_heap_error); free(heap); }
TEST(dynmem, double_free) { uint16_t size = 1000; mem_stat_t info; uint8_t *heap = (uint8_t*)malloc(size); void *p; CHECK(NULL != heap); reset_heap_error(); ns_dyn_mem_init(heap, size, &heap_fail_callback, &info); CHECK(!heap_have_failed()); p = ns_dyn_mem_alloc(100); CHECK(p); ns_dyn_mem_free(p); CHECK(!heap_have_failed()); ns_dyn_mem_free(p); CHECK(heap_have_failed()); CHECK(NS_DYN_MEM_DOUBLE_FREE == current_heap_error); free(heap); }
int8_t mac_helper_nwk_id_filter_set(const uint8_t *nw_id, nwk_filter_params_s *filter) { if (!filter) { return -1; } int8_t ret_val = 0; if (nw_id) { if (filter->beacon_nwk_id_filter == 0) { filter->beacon_nwk_id_filter = ns_dyn_mem_alloc(16); } if (filter->beacon_nwk_id_filter) { memcpy(filter->beacon_nwk_id_filter, nw_id, 16); } else { ret_val = -1; } } else { ns_dyn_mem_free(filter->beacon_nwk_id_filter); filter->beacon_nwk_id_filter = 0; } return ret_val; }
TEST(dynmem, not_from_this_heap) { uint16_t size = 1000; mem_stat_t info; uint8_t *heap = (uint8_t*)malloc(size); uint8_t *p; CHECK(NULL != heap); reset_heap_error(); ns_dyn_mem_init(heap, size, &heap_fail_callback, &info); CHECK(!heap_have_failed()); p = (uint8_t *)ns_dyn_mem_alloc(100); CHECK(p); ns_dyn_mem_free(&heap[-1]); CHECK(heap_have_failed()); CHECK(NS_DYN_MEM_POINTER_NOT_VALID == current_heap_error); reset_heap_error(); ns_dyn_mem_free(&heap[1001]); CHECK(heap_have_failed()); CHECK(NS_DYN_MEM_POINTER_NOT_VALID == current_heap_error); free(heap); }
int ns_file_system_set_root_path(const char *root_path) { char *new_root_path; if (root_path == NULL) { // File system usage disabled ns_dyn_mem_free(file_system_root); file_system_root = NULL; return 0; } new_root_path = ns_dyn_mem_alloc(strlen(root_path) + 1); if (!new_root_path) { // mem alloc failed return -2; } ns_dyn_mem_free(file_system_root); file_system_root = new_root_path; strcpy(file_system_root, root_path); return 0; }
/** * Allocate Load balance class base */ static lb_internal_t *load_balance_class_allocate(void) { if (lb_store) { if (lb_store->lb_user_parent_id) { return NULL; } return lb_store; } lb_internal_t *store = ns_dyn_mem_alloc(sizeof(lb_internal_t)); if (store) { store->lb_api = &lb_api; lb_api.lb_beacon_notify = lb_beacon_notify; lb_api.lb_enable = lb_enable; lb_api.lb_initialize = lb_api_initialize; lb_api.lb_seconds_tick_update = lb_second_ticks; store->lb_border_router = NULL; store->load_balance_beacon_tx_cb = NULL; store->lb_nwk_switch_cb = NULL; store->lb_priority_get_cb = NULL; store->lb_user_parent_id = NULL; store->notified_network = NULL; store->lb_access_switch_cb = NULL; store->beacon_max_payload_length = 0; store->nwk_switch_threshold_max = 0; store->nwk_switch_threshold_min = 0; store->nwk_maX_P = 25; store->load_balance_activate = false; store->time_to_next_beacon = 0; store->triggle_period = 0; lb_store = store; } return store; }
//NOTE! This test must be last! TEST(dynmem, uninitialized_test){ void *p = ns_dyn_mem_alloc(4); ns_dyn_mem_free(p); CHECK(p == NULL); }