xref: /llvm-project/compiler-rt/test/asan/TestCases/Windows/sanitizer_purge.cpp (revision 5ad8bbee238f9655c55f048b6ae8c9b40b4c87e5)
181b1d3daSMatthew G McGovern #include <Windows.h>
281b1d3daSMatthew G McGovern #include <stdio.h>
381b1d3daSMatthew G McGovern #include <sanitizer/allocator_interface.h>
481b1d3daSMatthew G McGovern #include <psapi.h>
581b1d3daSMatthew G McGovern 
6*5ad8bbeeSAlvin Wong // RUN: %clang_cl_asan %Od %s %Fe%t
781b1d3daSMatthew G McGovern // RUN: %t
881b1d3daSMatthew G McGovern // REQUIRES: asan-64-bits
981b1d3daSMatthew G McGovern 
GetRSS()1081b1d3daSMatthew G McGovern size_t GetRSS() {
1181b1d3daSMatthew G McGovern   PROCESS_MEMORY_COUNTERS counters;
1281b1d3daSMatthew G McGovern   if (!GetProcessMemoryInfo(GetCurrentProcess(), &counters, sizeof(counters)))
1381b1d3daSMatthew G McGovern     return 0;
1481b1d3daSMatthew G McGovern   return counters.WorkingSetSize;
1581b1d3daSMatthew G McGovern }
1681b1d3daSMatthew G McGovern 
main()1781b1d3daSMatthew G McGovern int main(){
1881b1d3daSMatthew G McGovern     for (int i = 0; i < 1000; i++) {
1981b1d3daSMatthew G McGovern         void* a = malloc(1000);
2081b1d3daSMatthew G McGovern         free(a);
2181b1d3daSMatthew G McGovern     }
2281b1d3daSMatthew G McGovern     size_t rss_pre  = GetRSS();
2381b1d3daSMatthew G McGovern     __sanitizer_purge_allocator();
2481b1d3daSMatthew G McGovern     size_t rss_post = GetRSS();
2581b1d3daSMatthew G McGovern 
2681b1d3daSMatthew G McGovern     if (rss_pre <= rss_post){
2781b1d3daSMatthew G McGovern         return -1;
2881b1d3daSMatthew G McGovern     }
2981b1d3daSMatthew G McGovern 
3081b1d3daSMatthew G McGovern     return 0;
3181b1d3daSMatthew G McGovern }
32