Skip to content

Commit

Permalink
Merge pull request #3261 from BsAtHome/fix_clang-code-optimize-removal
Browse files Browse the repository at this point in the history
Don't let clang's optimizer remove important initialization code.
  • Loading branch information
andypugh authored Jan 9, 2025
2 parents 10fc808 + f7c5cb1 commit 41574cf
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/rtapi/uspace_rtapi_app.cc
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,22 @@ static void configure_memory()
"mallopt(M_MMAP_MAX, -1) failed\n");
}
#endif
char *buf = static_cast<char *>(malloc(PRE_ALLOC_SIZE));
/*
* The following code seems pointless, but there is a non-observable effect
* in the allocation and loop.
*
* The malloc() is forced to set brk() because mmap() allocation is
* disabled in a call to mallopt() above. All touched pages become resident
* and locked in the loop because of above mlockall() call (see notes in
* mlockall(2)). The mallopt() trim setting prevents the brk() from being
* reduced after free(), effectively creating an open space for future
* allocations that will not generate page faults.
*
* The qualifier 'volatile' on the buffer pointer is required because newer
* clang would remove the malloc(), for()-loop and free() completely.
* Marking 'buf' volatile ensures that the code will remain in place.
*/
volatile char *buf = static_cast<volatile char *>(malloc(PRE_ALLOC_SIZE));
if (buf == NULL) {
rtapi_print_msg(RTAPI_MSG_WARN, "malloc(PRE_ALLOC_SIZE) failed\n");
return;
Expand All @@ -717,7 +732,7 @@ static void configure_memory()
* memory and never given back to the system. */
buf[i] = 0;
}
free(buf);
free((void *)buf);
}

static int harden_rt()
Expand Down

0 comments on commit 41574cf

Please sign in to comment.