1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-03-09 15:50:02 +00:00

Introduce usage of __builtin_unreachable() and noreturn (#248)

This commit adds an UNREACHABLE() macro that expands to either the
__builtin_unreachable() compiler builtin (for release builds) or
abort(3) (for development builds). This is used to mark code paths
that are never to be reached.

It also adds the 'noreturn' attribute to functions that never
return: path_exec(), sh_done() and sh_syntax(). The UNREACHABLE()
macro is not added after calling these.

The purpose of these is:
* to slightly improve GCC/Clang compiler optimizations;
* to fix a few compiler warnings;
* to add code clarity.

Changes of note:

src/cmd/ksh93/sh/io.c: outexcept():
- Avoid using __builtin_unreachable() here since errormsg can
  return despite using ERROR_system(1), as shp->jmplist->mode is
  temporarily set to 0. See: https://github.com/att/ast/issues/1336

src/cmd/ksh93/tests/io.sh:
- Add a regression test for the ksh2020 bug referenced above.

src/lib/libast/features/common:
- Detect the existence of either the C11 stdnoreturn.h header or
  the GCC noreturn attribute, preferring the former when available.
- Test for the existence of __builtin_unreachable(). Use it for
  release builds. On development builds, use abort() instead, which
  crahses reliably for debugging when unreachable code is reached.

Co-authored-by: Martijn Dekker <martijn@inlv.org>
This commit is contained in:
Johnothan King 2021-04-04 16:28:24 -07:00 committed by GitHub
parent 56913f8c2a
commit c4f980eb29
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
87 changed files with 1170 additions and 122 deletions

View file

@ -191,7 +191,7 @@ waitpid(pid_t pid, int* status, int flags)
zp->next = zombies;
zombies = zp;
}
/*NOTREACHED*/
UNREACHABLE();
}
#endif

View file

@ -55,7 +55,7 @@ readdir(register DIR* dirp)
dirp->dd_loc += dp->d_reclen;
if (dp->d_fileno) return(dp);
}
/*NOTREACHED*/
UNREACHABLE();
}
#endif

View file

@ -55,12 +55,12 @@ nomalloc(Vmalloc_t* region, int type, void* obj, Vmdisc_t* disc)
#ifdef VM_BADADDR
case VM_BADADDR:
error(ERROR_SYSTEM|3, "invalid pointer %p passed to free or realloc", obj);
return(-1);
UNREACHABLE();
#endif
case VM_NOMEM:
vmstat(region, &st);
error(ERROR_SYSTEM|3, "storage allocator out of memory on %lu byte request ( region %lu segments %lu busy %lu:%lu:%lu free %lu:%lu:%lu )", (size_t)obj, st.extent, st.n_seg, st.n_busy, st.s_busy, st.m_busy, st.n_free, st.s_free, st.m_free);
return(-1);
UNREACHABLE();
}
return(0);
}

View file

@ -1,5 +1,5 @@
iff AST_COMMON
hdr pthread,stdarg,stddef,stdint,inttypes,types,unistd
hdr pthread,stdarg,stddef,stdint,stdnoreturn,inttypes,types,unistd
sys types
typ long.double,size_t,ssize_t
typ __va_list stdio.h
@ -145,6 +145,16 @@ cat{
# define __EXTERN__(T,obj) extern T obj
# define __DEFINE__(T,obj,val) T obj = val
#endif
#if !_hdr_stdnoreturn
#if defined(__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4))
#define noreturn __attribute__((noreturn))
#else
#define noreturn
#endif /* __GNUC__ */
#else
#include <stdnoreturn.h>
#endif /* _hdr_stdnoreturn */
}end
if tst - note{ <stdarg.h>+<wchar.h> works }end compile{
@ -634,3 +644,37 @@ run{
#endif
!
}end
tst - note{ does this compiler have __builtin_unreachable() }end output{
/*
* To avoid optimizing out the __builtin_unreachable() test call while also avoiding
* executing it, put it in a test function that is called with argc as the argument.
* argc should never be zero on init, but hopefully optimizers aren't that smart.
*/
#include <stdio.h>
void testfn(int a)
{
switch(a)
{
case 0:
__builtin_unreachable();
default:
printf("#if _AST_ksh_release\n");
printf("# define UNREACHABLE()\t__builtin_unreachable()\n");
printf("#else\n");
printf("# define UNREACHABLE()\tabort()\n");
printf("#endif\n");
}
}
int main(int argc, char *argv[])
{
testfn(argc);
return 0;
}
}end fail{
echo '#if _AST_ksh_release'
echo '# define UNREACHABLE() 0'
echo '#else'
echo '# define UNREACHABLE() abort()'
echo '#endif'
}end

View file

@ -120,13 +120,12 @@ static const char Omsg[] = "malloc failed while growing stack\n";
/*
* default overflow exception
*/
static char *overflow(int n)
static noreturn char *overflow(int n)
{
NoP(n);
write(2,Omsg, sizeof(Omsg)-1);
exit(2);
/* NOTREACHED */
return(0);
UNREACHABLE();
}
/*