git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 2/3] ci: add build checking for side-effects in assert() calls

From
Junio C Hamano <gitster@pobox.com>
Date
Mar 14, 2025, 01:06 UTC
Message-ID
<xmqqy0x8l8tu.fsf@gitster.g>
In-Reply-To
<80dcc2ba3aa0ef72abe18f8525d571ea39ac6382.1741911652.git.gitgitgadget@gmail.com>
"Elijah Newren via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 9 quoted lines
> +make CHECK_ASSERTION_SIDE_EFFECTS=1 >compiler_output 2>compiler_error
> +if test $? != 0
> +then
> +    echo "ERROR: The compiler could not verify the following assert()" >&2
> +    echo "       calls are free of side-effects.  Please replace with" >&2
> +    echo "       BUG_IF_NOT() calls." >&2
> +    grep undefined.reference.to..not_supposed_to_survive compiler_error \
> +      | sed -e s/:[^:]*$// | sort | uniq | tr ':' ' ' \
> +      | while read f l
A few style guides:
 - doing multiple echo into the same descriptor is easier to read if
   you have redirection near the beginning, i.e.
	echo >&2 "message one"
	echo >&2 "message two that may way be longer than the previous"
        ehco >&2 "message three"
 - multi-line pipelines are easier to follow without backslash by
   ending the previous line with a '|', i.e.
	grep ... file |
	sed -e ... |
	while read file line
	do
		...
 - I thought our "one indent one tab" standard extends to shell
   scripts as well?
Show 13 quoted lines
> diff --git a/git-compat-util.h b/git-compat-util.h
> index c3415ad7e0a..0aefd763751 100644
> --- a/git-compat-util.h
> +++ b/git-compat-util.h
> @@ -1584,4 +1584,10 @@ static inline void *container_of_or_null_offset(void *ptr, size_t offset)
>  	((uintptr_t)&(ptr)->member - (uintptr_t)(ptr))
>  #endif /* !__GNUC__ */
>  
> +#ifdef CHECK_ASSERTION_SIDE_EFFECTS
> +#undef assert
> +extern int not_supposed_to_survive;
> +#define assert(expr) ((void)(not_supposed_to_survive || (expr)))
> +#endif /* CHECK_ASSERTION_SIDE_EFFECTS */

Cute. As this checking assert is in void context, the optimizing compiler knows that the entire thing can be optimized away ONLY IF it can somehow prove that (expr) has no side effect. And if it does not optimize it away, you will hit an error from the linker, saying that the undefined variable is being used.

This requires a fairly good optimizing compiler that can peek into (as in "inline") what is in expr to notice, so it cannot be free of false positive, but at least when the optimization works as expected, it is provably (modulo optimizer bugs) side-effect free.

Is this something we can use in our project? I am just double checking.

Thanks.
Previous: Elijah Newren via GitGitGadgetNext: brian m. carlson
Message 4 of 30 in “Add a static analysis job to prevent assertions with side effects”
  1. 0/3 Add a static analysis job to prevent assertions with side effectsElijah Newren via GitGitGadget, Mar 14, 2025
  2. 1/3 git-compat-util: introduce BUG_IF_NOT() macroElijah Newren via GitGitGadget, Mar 14, 2025
  3. 2/3 ci: add build checking for side-effects in assert() callsElijah Newren via GitGitGadget, Mar 14, 2025
  4. Junio C HamanoMar 14, 2025
  5. brian m. carlsonMar 14, 2025
  6. Junio C HamanoMar 14, 2025
  7. Elijah NewrenMar 14, 2025
  8. Junio C HamanoMar 14, 2025
  9. Elijah NewrenMar 16, 2025
  10. Elijah NewrenMar 17, 2025
  11. Junio C HamanoMar 17, 2025
  12. 3/3 treewide: replace assert() with BUG_IF_NOT() in special casesElijah Newren via GitGitGadget, Mar 14, 2025
  13. 0/3 Add a static analysis job to prevent assertions with side effectsElijah Newren via GitGitGadget, Mar 16, 2025
  14. 1/3 git-compat-util: introduce BUG_IF_NOT() macroElijah Newren via GitGitGadget, Mar 16, 2025
  15. Junio C HamanoMar 17, 2025
  16. Taylor BlauMar 17, 2025
  17. 2/3 ci: add build checking for side-effects in assert() callsElijah Newren via GitGitGadget, Mar 16, 2025
  18. Taylor BlauMar 17, 2025
  19. Elijah NewrenMar 19, 2025
  20. Taylor BlauMar 19, 2025
  21. Junio C HamanoMar 17, 2025
  22. 3/3 treewide: replace assert() with BUG_IF_NOT() in special casesElijah Newren via GitGitGadget, Mar 16, 2025
  23. Taylor BlauMar 17, 2025
  24. Taylor BlauMar 17, 2025
  25. 0/3 Add a static analysis job to prevent assertions with side effectsElijah Newren via GitGitGadget, Mar 19, 2025
  26. 1/3 git-compat-util: introduce ASSERT() macroElijah Newren via GitGitGadget, Mar 19, 2025
  27. 2/3 ci: add build checking for side-effects in assert() callsElijah Newren via GitGitGadget, Mar 19, 2025
  28. 3/3 treewide: replace assert() with ASSERT() in special casesElijah Newren via GitGitGadget, Mar 19, 2025
  29. Taylor BlauMar 19, 2025
  30. Taylor BlauMar 19, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.