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

@ -53,7 +53,7 @@
static int canexecute(Shell_t*,char*,int);
static void funload(Shell_t*,int,const char*);
static void exscript(Shell_t*,char*, char*[], char**);
static void noreturn exscript(Shell_t*,char*, char*[], char**);
static int path_chkpaths(Shell_t*,Pathcomp_t*,Pathcomp_t*,Pathcomp_t*,int);
static void path_checkdup(Shell_t *shp,register Pathcomp_t*);
static Pathcomp_t *defpath_init(Shell_t *shp);
@ -489,7 +489,10 @@ static int path_opentype(Shell_t *shp,const char *name, register Pathcomp_t *pp,
if(!fun && strchr(name,'/'))
{
if(sh_isoption(SH_RESTRICTED))
{
errormsg(SH_DICT,ERROR_exit(1),e_restricted,name);
UNREACHABLE();
}
}
nextpp = pp;
@ -602,7 +605,10 @@ static void funload(Shell_t *shp,int fno, const char *name)
if(!loopdetect_tree)
loopdetect_tree = dtopen(&_Nvdisc,Dtoset);
else if(nv_search(pname,loopdetect_tree,0))
{
errormsg(SH_DICT,ERROR_exit(ERROR_NOEXEC),"autoload loop: %s in %s",name,pname);
UNREACHABLE();
}
np_loopdetect = nv_search(pname,loopdetect_tree,NV_ADD);
sh_onstate(SH_NOALIAS);
shp->readscript = (char*)name;
@ -630,7 +636,10 @@ static void funload(Shell_t *shp,int fno, const char *name)
sh_setstate(savestates);
nv_delete(np_loopdetect,loopdetect_tree,0);
if(pname)
{
errormsg(SH_DICT,ERROR_exit(ERROR_NOEXEC),e_funload,name,pname);
UNREACHABLE();
}
}
/*
@ -991,7 +1000,7 @@ char *path_relative(Shell_t *shp,register const char* file)
return((char*)file);
}
void path_exec(Shell_t *shp,register const char *arg0,register char *argv[],struct argnod *local)
noreturn void path_exec(Shell_t *shp,register const char *arg0,register char *argv[],struct argnod *local)
{
char **envp;
const char *opath;
@ -1004,7 +1013,10 @@ void path_exec(Shell_t *shp,register const char *arg0,register char *argv[],stru
slash=1;
/* name containing / not allowed for restricted shell */
if(sh_isoption(SH_RESTRICTED))
{
errormsg(SH_DICT,ERROR_exit(1),e_restricted,arg0);
UNREACHABLE();
}
}
else
pp=path_get(shp,arg0);
@ -1035,6 +1047,7 @@ void path_exec(Shell_t *shp,register const char *arg0,register char *argv[],stru
errormsg(SH_DICT,ERROR_exit(ERROR_NOENT),e_found,arg0);
else
errormsg(SH_DICT,ERROR_system(ERROR_NOEXEC),e_exec,arg0);
UNREACHABLE();
}
pid_t path_spawn(Shell_t *shp,const char *opath,register char **argv, char **envp, Pathcomp_t *libpath, int spawn)
@ -1191,6 +1204,7 @@ pid_t path_spawn(Shell_t *shp,const char *opath,register char **argv, char **env
((struct checkpt*)shp->jmplist)->mode = SH_JMPEXIT;
}
exscript(shp,path,argv,envp);
UNREACHABLE();
case EACCES:
{
struct stat statb;
@ -1200,7 +1214,10 @@ pid_t path_spawn(Shell_t *shp,const char *opath,register char **argv, char **env
errno = EISDIR;
#ifdef S_ISSOCK
if(S_ISSOCK(statb.st_mode))
{
exscript(shp,path,argv,envp);
UNREACHABLE();
}
#endif
}
}
@ -1226,12 +1243,16 @@ pid_t path_spawn(Shell_t *shp,const char *opath,register char **argv, char **env
{
pid = path_xargs(shp,opath, &argv[0] ,envp,spawn);
if(pid<0)
{
errormsg(SH_DICT,ERROR_system(ERROR_NOEXEC),"command -x: could not execute %s",path);
UNREACHABLE();
}
return(pid);
}
/* FALLTHROUGH */
default:
errormsg(SH_DICT,ERROR_system(ERROR_NOEXEC),e_exec,path);
UNREACHABLE();
}
return 0;
}
@ -1241,7 +1262,7 @@ pid_t path_spawn(Shell_t *shp,const char *opath,register char **argv, char **env
* Assume file is a Shell script and execute it.
*/
static void exscript(Shell_t *shp,register char *path,register char *argv[],char **envp)
static noreturn void exscript(Shell_t *shp,register char *path,register char *argv[],char **envp)
{
register Sfio_t *sp;
path = path_relative(shp,path);
@ -1303,7 +1324,10 @@ static void exscript(Shell_t *shp,register char *path,register char *argv[],char
* The following code is just for compatibility
*/
if((n=open(path,O_RDONLY,0)) < 0)
{
errormsg(SH_DICT,ERROR_system(ERROR_NOEXEC),e_exec,path);
UNREACHABLE();
}
if(savet)
*argv++ = savet;
openok:
@ -1311,7 +1335,10 @@ static void exscript(Shell_t *shp,register char *path,register char *argv[],char
}
#else
if((shp->infd = sh_open(path,O_RDONLY,0)) < 0)
{
errormsg(SH_DICT,ERROR_system(ERROR_NOEXEC),e_exec,path);
UNREACHABLE();
}
#endif
shp->infd = sh_iomovefd(shp->infd);
#if SHOPT_ACCT