treeNode * DeleteAddress(treeNode *node, ADDRINT address) { treeNode *temp; if(node == NULL) { // assert(0); return NULL; } if(address < node->address) { node->left = DeleteAddress(node->left, address); } else if(address > node->address) { node->right = DeleteAddress(node->right, address); } else { /* Now We can delete this node and replace with either minimum element in the right sub tree or maximum element in the left subtree */ if(node->right && node->left) { /* Here we will replace with minimum element in the right sub tree */ temp = FindMinAddress(node->right); node -> address = temp->address; node->size = temp->size; node->usedforread = temp->usedforread; if(node->path != NULL) free(node->path); node->path = temp->path; temp->path = NULL; node->readid = temp->readid; /* As we replaced it with some other node, we have to delete that node */ node -> right = DeleteAddress(node->right,temp->address); } else { /* If there is only one or zero children then we can directly remove it from the tree and connect its parent to its child */ temp = node; if(node->left == NULL) node = node->right; else if(node->right == NULL) node = node->left; else assert(0); if(temp->path != NULL) free(temp->path); free(temp); /* temp is longer required */ } } return node; }
/* * Fini * Called when process ends * Clean up mems & locks */ VOID Fini(INT32 code, VOID *v) { PIN_RWMutexWriteLock(&malloc_lock); while(malloc_root != NULL) malloc_root = DeleteAddress(malloc_root, malloc_root->address); PIN_RWMutexUnlock(&malloc_lock); PIN_RWMutexWriteLock(&data_lock); while(data_root != NULL) data_root = DeleteAddress(data_root, data_root->address); PIN_RWMutexUnlock(&data_lock); PIN_RWMutexWriteLock(&thread_lock); while(root_thread != NULL) root_thread = DeleteThread(root_thread->tid, root_thread); PIN_RWMutexUnlock(&thread_lock); PIN_RWMutexFini(&thread_lock); PIN_RWMutexFini(&data_lock); PIN_RWMutexFini(&malloc_lock); PIN_MutexFini(&readid_lock); fclose(trace); }
/* * Return * Called when native procedure returns * Delete read info of removed stack area */ VOID Return(ADDRINT sp) { PIN_THREAD_UID threadid = PIN_ThreadUid(); PIN_RWMutexReadLock(&thread_lock); THREAD* thread = FindThread(threadid); PIN_RWMutexUnlock(&thread_lock); treeNode *node = FindMinAddress(thread->stack); if(node == NULL) return; while(node->address <= sp) { thread->stack = DeleteAddress(thread->stack, node->address); node = FindMinAddress(thread->stack); if(node == NULL) return; } }
THREAD* DeleteThread(PIN_THREAD_UID threadid, THREAD*node) { THREAD *temp; assert(node); if(threadid < node->tid) { node->left = DeleteThread(threadid, node->left); } else if(threadid > node->tid) { node->right = DeleteThread(threadid, node->right); } else { /* Now We can delete this node and replace with either minimum element in the right sub tree or maximum element in the left subtree */ while(node->stack != NULL) node->stack = DeleteAddress(node->stack, node->stack->address); if(node->right && node->left) { /* Here we will replace with minimum element in the right sub tree */ temp = FindMinThread(node->right); node -> tid = temp->tid; node->stack = temp->stack; /* As we replaced it with some other node, we have to delete that node */ node -> right = DeleteThread(temp->tid, node->right); } else { /* If there is only one or zero children then we can directly remove it from the tree and connect its parent to its child */ temp = node; if(node->left == NULL) node = node->right; else if(node->right == NULL) node = node->left; free(temp); /* temp is longer required */ } } return node; }
static DWORD HandleAddressMessage (address_message_t *msg, undo_lists_t *lists) { DWORD err; PMIB_UNICASTIPADDRESS_ROW addr_row; BOOL add = msg->header.type == msg_add_address; typedef NETIOAPI_API (*CreateUnicastIpAddressEntryFn) (const PMIB_UNICASTIPADDRESS_ROW); typedef NETIOAPI_API (*InitializeUnicastIpAddressEntryFn) (PMIB_UNICASTIPADDRESS_ROW); static CreateUnicastIpAddressEntryFn CreateUnicastIpAddressEntry = NULL; static InitializeUnicastIpAddressEntryFn InitializeUnicastIpAddressEntry = NULL; if (!CreateUnicastIpAddressEntry || !InitializeUnicastIpAddressEntry) { HMODULE iphlpapi = GetModuleHandle (TEXT("iphlpapi.dll")); if (iphlpapi == NULL) return GetLastError (); CreateUnicastIpAddressEntry = (CreateUnicastIpAddressEntryFn) GetProcAddress (iphlpapi, "CreateUnicastIpAddressEntry"); if (!CreateUnicastIpAddressEntry) return GetLastError (); InitializeUnicastIpAddressEntry = (InitializeUnicastIpAddressEntryFn) GetProcAddress (iphlpapi, "InitializeUnicastIpAddressEntry"); if (!InitializeUnicastIpAddressEntry) return GetLastError (); } addr_row = malloc (sizeof (*addr_row)); if (addr_row == NULL) return ERROR_OUTOFMEMORY; InitializeUnicastIpAddressEntry (addr_row); addr_row->Address = sockaddr_inet (msg->family, &msg->address); addr_row->OnLinkPrefixLength = (UINT8) msg->prefix_len; if (msg->iface.index != -1) { addr_row->InterfaceIndex = msg->iface.index; } else { NET_LUID luid; err = InterfaceLuid (msg->iface.name, &luid); if (err) goto out; addr_row->InterfaceLuid = luid; } if (add) { err = CreateUnicastIpAddressEntry (addr_row); if (err) goto out; err = AddListItem (&(*lists)[address], addr_row); if (err) DeleteAddress (addr_row); } else { err = DeleteAddress (addr_row); if (err) goto out; free (RemoveListItem (&(*lists)[address], CmpAddress, addr_row)); } out: if (!add || err) free (addr_row); return err; }