static int HostBitTest03 (void) { int ret = 0; HostInitConfig(TRUE); Host *h = HostAlloc(); if (h == NULL) goto end; HostBitAdd(h, 0, 30); XBit *fb = HostBitGet(h,0); if (fb == NULL) { printf("fb == NULL although it was just added: "); goto end; } HostBitRemove(h, 0); fb = HostBitGet(h,0); if (fb != NULL) { printf("fb != NULL although it was just removed: "); goto end; } else { ret = 1; } HostFree(h); end: HostCleanup(); return ret; }
static int HostBitTest11 (void) { int ret = 0; HostInitConfig(TRUE); Host *h = HostAlloc(); if (h == NULL) goto end; HostBitAdd(h, 0, 90); HostBitAdd(h, 1, 90); HostBitAdd(h, 2, 90); HostBitAdd(h, 3, 90); XBit *fb = HostBitGet(h,3); if (fb == NULL) goto end; HostBitRemove(h,3); fb = HostBitGet(h,3); if (fb != NULL) { printf("fb != NULL even though it was removed: "); goto end; } ret = 1; HostFree(h); end: HostCleanup(); return ret; }
Host *HostNew(Address *a) { Host *h = HostAlloc(); if (h == NULL) goto error; /* copy address */ COPY_ADDRESS(a, &h->a); return h; error: return NULL; }
static int HostBitTest02 (void) { int ret = 0; HostInitConfig(TRUE); Host *h = HostAlloc(); if (h == NULL) goto end; XBit *fb = HostBitGet(h,0); if (fb == NULL) ret = 1; HostFree(h); end: HostCleanup(); return ret; }
bool FrameImpl::LoadFromPDB(void* address) { TraceFunc(); LogTraceLn(); bool ret = false; { size_t size = sizeof(SYMBOL_INFOW) + MAX_SYM_NAME * sizeof(wchar_t); SYMBOL_INFOW* symInfo = static_cast<SYMBOL_INFOW*>(HostAlloc(memory::heap::DefaultStat, size)); memory::zero(symInfo, sizeof(*symInfo)); symInfo->SizeOfStruct = sizeof(*symInfo); symInfo->MaxNameLen = MAX_SYM_NAME; DWORD64 displacement; bool res = os::Dbghelp_dll::inst().SymFromAddrW(::GetCurrentProcess(), reinterpret_cast<DWORD64>(address), &displacement, symInfo); LogWarnIf(!res, L"%s\n", totext::api_error().c_str()); if (res) { m_address = reinterpret_cast<void*>(symInfo->Address); m_function = symInfo->Name; m_offset = (size_t)displacement; ret = true; } HostFree(memory::heap::DefaultStat, symInfo); } { IMAGEHLP_LINEW64 info; memory::zero(info); info.SizeOfStruct = sizeof(info); DWORD dwLineDisplacement = 0; bool res = os::Dbghelp_dll::inst().SymGetLineFromAddrW64(::GetCurrentProcess(), reinterpret_cast<DWORD64>(address), &dwLineDisplacement, &info); // LogErrorIf(!res, L"%s\n", totext::api_error().c_str()); if (res) { m_line = info.LineNumber; m_file = info.FileName; } } TraceFunc(); return ret; }
/** \brief initialize the configuration * \warning Not thread safe */ void HostInitConfig(char quiet) { SCLogDebug("initializing host engine..."); memset(&host_config, 0, sizeof(host_config)); //SC_ATOMIC_INIT(flow_flags); SC_ATOMIC_INIT(host_counter); SC_ATOMIC_INIT(host_memuse); SC_ATOMIC_INIT(host_prune_idx); HostQueueInit(&host_spare_q); unsigned int seed = RandomTimePreseed(); /* set defaults */ host_config.hash_rand = (int)( HOST_DEFAULT_HASHSIZE * (rand_r(&seed) / RAND_MAX + 1.0)); host_config.hash_size = HOST_DEFAULT_HASHSIZE; host_config.memcap = HOST_DEFAULT_MEMCAP; host_config.prealloc = HOST_DEFAULT_PREALLOC; /* Check if we have memcap and hash_size defined at config */ char *conf_val; uint32_t configval = 0; /** set config values for memcap, prealloc and hash_size */ if ((ConfGet("host.memcap", &conf_val)) == 1) { if (ParseSizeStringU64(conf_val, &host_config.memcap) < 0) { SCLogError(SC_ERR_SIZE_PARSE, "Error parsing host.memcap " "from conf file - %s. Killing engine", conf_val); exit(EXIT_FAILURE); } } if ((ConfGet("host.hash-size", &conf_val)) == 1) { if (ByteExtractStringUint32(&configval, 10, strlen(conf_val), conf_val) > 0) { host_config.hash_size = configval; } } if ((ConfGet("host.prealloc", &conf_val)) == 1) { if (ByteExtractStringUint32(&configval, 10, strlen(conf_val), conf_val) > 0) { host_config.prealloc = configval; } } SCLogDebug("Host config from suricata.yaml: memcap: %"PRIu64", hash-size: " "%"PRIu32", prealloc: %"PRIu32, host_config.memcap, host_config.hash_size, host_config.prealloc); /* alloc hash memory */ uint64_t hash_size = host_config.hash_size * sizeof(HostHashRow); if (!(HOST_CHECK_MEMCAP(hash_size))) { SCLogError(SC_ERR_HOST_INIT, "allocating host hash failed: " "max host memcap is smaller than projected hash size. " "Memcap: %"PRIu64", Hash table size %"PRIu64". Calculate " "total hash size by multiplying \"host.hash-size\" with %"PRIuMAX", " "which is the hash bucket size.", host_config.memcap, hash_size, (uintmax_t)sizeof(HostHashRow)); exit(EXIT_FAILURE); } host_hash = SCCalloc(host_config.hash_size, sizeof(HostHashRow)); if (unlikely(host_hash == NULL)) { SCLogError(SC_ERR_FATAL, "Fatal error encountered in HostInitConfig. Exiting..."); exit(EXIT_FAILURE); } memset(host_hash, 0, host_config.hash_size * sizeof(HostHashRow)); uint32_t i = 0; for (i = 0; i < host_config.hash_size; i++) { HRLOCK_INIT(&host_hash[i]); } (void) SC_ATOMIC_ADD(host_memuse, (host_config.hash_size * sizeof(HostHashRow))); if (quiet == FALSE) { SCLogInfo("allocated %llu bytes of memory for the host hash... " "%" PRIu32 " buckets of size %" PRIuMAX "", SC_ATOMIC_GET(host_memuse), host_config.hash_size, (uintmax_t)sizeof(HostHashRow)); } /* pre allocate hosts */ for (i = 0; i < host_config.prealloc; i++) { if (!(HOST_CHECK_MEMCAP(sizeof(Host)))) { SCLogError(SC_ERR_HOST_INIT, "preallocating hosts failed: " "max host memcap reached. Memcap %"PRIu64", " "Memuse %"PRIu64".", host_config.memcap, ((uint64_t)SC_ATOMIC_GET(host_memuse) + (uint64_t)sizeof(Host))); exit(EXIT_FAILURE); } Host *h = HostAlloc(); if (h == NULL) { SCLogError(SC_ERR_HOST_INIT, "preallocating host failed: %s", strerror(errno)); exit(EXIT_FAILURE); } HostEnqueue(&host_spare_q,h); } if (quiet == FALSE) { SCLogInfo("preallocated %" PRIu32 " hosts of size %" PRIuMAX "", host_spare_q.len, (uintmax_t)sizeof(Host)); SCLogInfo("host memory usage: %llu bytes, maximum: %"PRIu64, SC_ATOMIC_GET(host_memuse), host_config.memcap); } return; }
void test_memory() { LogWarn(""); auto heap = ::GetProcessHeap(); PROCESS_HEAP_ENTRY entry; memory::zero(entry); LogReport("Walking heap %p...", heap); while (::HeapWalk(heap, &entry) != FALSE) { if ((entry.wFlags & PROCESS_HEAP_ENTRY_BUSY) != 0) { LogReport("Allocated block"); if ((entry.wFlags & PROCESS_HEAP_ENTRY_MOVEABLE) != 0) { LogReport(", movable with HANDLE %p", entry.Block.hMem); } if ((entry.wFlags & PROCESS_HEAP_ENTRY_DDESHARE) != 0) { LogReport(", DDESHARE"); } } else if ((entry.wFlags & PROCESS_HEAP_REGION) != 0) { LogReport("Region\n %d bytes committed" \ " %d bytes uncommitted\n First block address: %p" \ " Last block address: %p", entry.Region.dwCommittedSize, entry.Region.dwUnCommittedSize, entry.Region.lpFirstBlock, entry.Region.lpLastBlock); } else if ((entry.wFlags & PROCESS_HEAP_UNCOMMITTED_RANGE) != 0) { LogReport("Uncommitted range"); } else { LogReport("Block"); } LogReport(" Data portion begins at: %p\n Size: %d bytes" \ " Overhead: %d bytes\n Region index: %d", entry.lpData, entry.cbData, entry.cbOverhead, entry.iRegionIndex); } // LastError = GetLastError(); // if (LastError != ERROR_NO_MORE_ITEMS) { // _tprintf(TEXT("HeapWalk failed with LastError %d."), LastError); // } return; struct TypeTag {}; typedef typename memory::heap::DecoratorStat<memory::heap::Default, memory::heap::StatLog, TypeTag> heap_type; struct TypeTag1 {}; typedef typename memory::heap::DecoratorStat<memory::heap::Default, memory::heap::StatLog, TypeTag1> heap_type1; struct TypeTag2 {}; typedef typename memory::heap::DecoratorStat<memory::heap::Default, memory::heap::StatLog, TypeTag2> heap_type2; auto ptr = HostAlloc(heap_type, 47); HostFree(heap_type, ptr); HostAlloc(heap_type, 42); { const auto stat = heap_type::get_stat(); LogReport("stat alloc: %I64u, %I64u", stat.get_allocations(), stat.get_allocations_size()); LogReport("stat free : %I64u, %I64u", stat.get_frees(), stat.get_frees_size()); LogReport("stat diff : %I64d", stat.get_allocations_size() - stat.get_frees_size()); } { const auto stat = heap_type1::get_stat(); LogReport("stat1 alloc: %I64u, %I64u", stat.get_allocations(), stat.get_allocations_size()); LogReport("stat1 free : %I64u, %I64u", stat.get_frees(), stat.get_frees_size()); LogReport("stat1 diff : %I64d", stat.get_allocations_size() - stat.get_frees_size()); } HostAlloc(heap_type1, 17); HostAlloc(heap_type1, 12); HostAlloc(heap_type2, 71); HostAlloc(heap_type2, 22); { const auto stat = heap_type1::get_stat(); LogReport("stat1 alloc: %I64u, %I64u", stat.get_allocations(), stat.get_allocations_size()); LogReport("stat1 free : %I64u, %I64u", stat.get_frees(), stat.get_frees_size()); LogReport("stat1 diff : %I64d", stat.get_allocations_size() - stat.get_frees_size()); } { const auto stat = heap_type2::get_stat(); LogReport("stat2 alloc: %I64u, %I64u", stat.get_allocations(), stat.get_allocations_size()); LogReport("stat2 free : %I64u, %I64u", stat.get_frees(), stat.get_frees_size()); LogReport("stat2 diff : %I64d", stat.get_allocations_size() - stat.get_frees_size()); } }