[PATCH 0/1] Add returns_twice attribute to setjmp()

Richard Earnshaw (lists) Richard.Earnshaw@arm.com
Thu Oct 2 16:43:47 GMT 2025


On 30/09/2025 15:49, Joel Sherrill wrote:
> Hi
> 
> This patch set cover letter is longer than usual because even though
> the change is small, the rationale for the change is complicated.
> 
> This patch adds the "returns_twice" attribute to setjmp(). This attribute
> is related to the processing of -Wclobbered. -Wclobbered is enabled
> as part of -Wextra and documented as follows:
> 
> '-Wclobbered'
>      Warn for variables that might be changed by 'longjmp' or 'vfork'.
>      This warning is also enabled by '-Wextra'.
> 
> I believe vfork() probably also needs the "returns_twice" attribute but
> that function is not supported by RTEMS so I did not have any test cases
> to verify the change against.

I'm not sure that's the same.  vfork creates two processes, but each process returns only once.

R.

> 
> The "returns_twice" attribute was introduced in GCC in 2005 by this
> commit. Given how long ago this was added, I did not see any need to
> conditionalize the use on GCC version.
> 
> commit 6e9a32219ba643ca53c2b68822f0eddbf3280503
> Author: Alexey Neyman <alex.neyman@auriga.ru>
> Date:   Tue Mar 8 13:19:40 2005 +0000
> 
>     re PR c/14411 (Request for setjmp/longjmp attributes)
>     
>             PR c/14411
>             * calls.c (flags_from_decl_or_type): Handle eturns_twice' attribute.
>             * c-common.c (handle_returns_twice): New function.
>             (c_common_attribute_table): Declare eturns_twice' attribute.
>             * doc/extend.texi: Document eturns_twice' attribute.
>             * tree.h (DECL_IS_RETURNS_TWICE): New macro.
>             (struct tree_decl): Add returns_twice_flag.
>     
>     From-SVN: r96101
> 
> The "returns_twice" attribute is documented by GCC as follows:
> 
> returns_twice'
>      The 'returns_twice' attribute tells the compiler that a function
>      may return more than one time.  The compiler ensures that all
>      registers are dead before calling such a function and emits a
>      warning about the variables that may be clobbered after the second
>      return from the function.  Examples of such functions are 'setjmp'
>      and 'vfork'.  The 'longjmp'-like counterpart of such function, if
>      any, might need to be marked with the 'noreturn' attribute.
> 
> RTEMS had about 25 -wclobbered warnings across the entire source base.
> Adding this attribute addressed all but two of the them. We modified 
> both of those cases to address the warning and ensure any variables
> used were safe from being clobbered.
> 
> -Wclobbered warns about code where it is hard to see what is wrong.
> The Linux man page for setjmp() includes this:
> 
> 
>        The  compiler  may  optimize  variables  into  registers, and
>        longjmp() may restore the values of other registers in  addi‐
>        tion to the stack pointer and program counter.  Consequently,
>        the values of automatic variables  are  unspecified  after  a
>        call to longjmp() if they meet all the following criteria:
> 
>        •  they are local to the function that made the corresponding
>           setjmp() call;
> 
>        •  their values are changed between the calls to setjmp() and
>           longjmp(); and
> 
>        •  they are not declared as volatile.
> 
>        Analogous remarks apply for siglongjmp().
> Joel Sherrill (1):
>   newlib/libc/include/setjmp.h: Add returns_twice attribute to setjmp()
> 
>  newlib/libc/include/setjmp.h | 7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)
> 



More information about the Newlib mailing list