Skip to content

Commit 0c70e89

Browse files
committed
Add notpossible keyword
This is the same as `unreachable` except it always traps violations, even if we're not running in MODE=dbg. This is useful for impossible conditions relating to system calls. It avoids terrifying bugs where control falls through to an unrelated function.
1 parent b66bd06 commit 0c70e89

File tree

12 files changed

+27
-26
lines changed

12 files changed

+27
-26
lines changed

.vscode/c_cpp_properties.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@
4646
"testonly=",
4747
"donothing=",
4848
"nosideeffect=",
49-
"unreachable=",
49+
"unreachable=",,
50+
"notpossible=",
5051
"thatispacked=",
5152
"dontthrow=",
5253
"nocallback=",

libc/calls/execve-nt.greg.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,5 +121,5 @@ textwindows int sys_execve_nt(const char *program, char *const argv[],
121121
} while (dwExitCode == kNtStillActive);
122122
__imp_CloseHandle(procinfo.hProcess);
123123
__imp_ExitProcess(dwExitCode);
124-
unreachable;
124+
notpossible;
125125
}

libc/calls/pledge-linux.c

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,6 @@
7474
#define PLEDGE(pledge) pledge, ARRAYLEN(pledge)
7575
#define OFF(f) offsetof(struct seccomp_data, f)
7676

77-
#define AbortPledge(reason) \
78-
do { \
79-
asm("hlt"); \
80-
unreachable; \
81-
} while (0)
82-
8377
struct Filter {
8478
size_t n;
8579
struct sock_filter p[700];
@@ -992,7 +986,7 @@ static privileged void OnSigSys(int sig, siginfo_t *si, void *vctx) {
992986
// fallthrough
993987
case PLEDGE_PENALTY_KILL_THREAD:
994988
KillThisThread();
995-
unreachable;
989+
notpossible;
996990
default:
997991
break;
998992
}
@@ -1006,14 +1000,14 @@ static privileged void MonitorSigSys(void) {
10061000
};
10071001
// we block changing sigsys once pledge is installed
10081002
// so we aren't terribly concerned if this will fail
1009-
if (SigAction(Sigsys, &sa, 0) == -1) asm("hlt");
1003+
if (SigAction(Sigsys, &sa, 0) == -1) {
1004+
notpossible;
1005+
}
10101006
}
10111007

10121008
static privileged void AppendFilter(struct Filter *f, struct sock_filter *p,
10131009
size_t n) {
1014-
if (UNLIKELY(f->n + n > ARRAYLEN(f->p))) {
1015-
AbortPledge("need to increase array size");
1016-
}
1010+
if (UNLIKELY(f->n + n > ARRAYLEN(f->p))) notpossible;
10171011
MemCpy(f->p + f->n, p, n * sizeof(*f->p));
10181012
f->n += n;
10191013
}
@@ -1857,7 +1851,7 @@ static privileged void AppendPledge(struct Filter *f, //
18571851
};
18581852
AppendFilter(f, PLEDGE(fragment));
18591853
} else {
1860-
AbortPledge("list of ordinals exceeds max displacement");
1854+
notpossible;
18611855
}
18621856
}
18631857

@@ -1953,7 +1947,7 @@ static privileged void AppendPledge(struct Filter *f, //
19531947
AllowTkillSelf(f);
19541948
break;
19551949
default:
1956-
AbortPledge("switch forgot to define a special ordinal");
1950+
notpossible;
19571951
}
19581952
}
19591953
}
@@ -1987,7 +1981,7 @@ privileged int sys_pledge_linux(unsigned long ipromises, int mode) {
19871981
if (kPledge[i].len) {
19881982
AppendPledge(&f, kPledge[i].syscalls, kPledge[i].len);
19891983
} else {
1990-
AbortPledge("bad ipromises");
1984+
notpossible;
19911985
}
19921986
}
19931987
}
@@ -2020,7 +2014,7 @@ privileged int sys_pledge_linux(unsigned long ipromises, int mode) {
20202014
sf[0].k = SECCOMP_RET_ERRNO | Eperm;
20212015
break;
20222016
default:
2023-
unreachable;
2017+
return -Einval;
20242018
}
20252019
AppendFilter(&f, PLEDGE(sf));
20262020
}

libc/calls/sigenter-xnu.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,5 +531,5 @@ privileged void __sigenter_xnu(void *fn, int infostyle, int sig,
531531
: "=a"(ax)
532532
: "0"(0x20000b8 /* sigreturn */), "D"(xnuctx), "S"(infostyle)
533533
: "rcx", "r11", "memory", "cc");
534-
unreachable;
534+
notpossible;
535535
}

libc/calls/statfs2cosmo.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,7 @@ void statfs2cosmo(struct statfs *f, const union statfs_meta *m) {
281281
memcpy(f_fstypename, m->netbsd.f_fstypename, 16);
282282

283283
} else {
284-
asm("hlt");
285-
unreachable;
284+
notpossible;
286285
}
287286

288287
f->f_type = f_type;

libc/calls/tmpfd.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@ int tmpfd(void) {
9898
0600)) != -1) {
9999
if (!IsWindows()) {
100100
if (unlink(path)) {
101-
asm("hlt");
102-
unreachable;
101+
notpossible;
103102
}
104103
}
105104
return fd;

libc/integral/c.inc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,12 @@ typedef struct {
591591
#endif
592592
#endif
593593

594+
#define notpossible \
595+
do { \
596+
asm("hlt"); \
597+
unreachable; \
598+
} while (0)
599+
594600
#define donothing \
595601
do { \
596602
} while (0)

libc/runtime/abort.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,5 @@ wontreturn void abort(void) {
4040
raise(SIGABRT);
4141
signal(SIGABRT, SIG_DFL);
4242
raise(SIGABRT);
43-
asm("hlt");
44-
unreachable;
43+
notpossible;
4544
}

libc/sock/nointernet.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@
6666
do { \
6767
if (UNLIKELY((x) == -1)) { \
6868
DEBUG("%s:%d: %s failed %m\n", __FILE__, __LINE__, #x); \
69-
asm("hlt"); \
70-
unreachable; \
69+
notpossible; \
7170
} \
7271
} while (0)
7372

tool/emacs/c.lang

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ Keywords={
117117
"reallocesque",
118118
"nullterminated",
119119
"unreachable",
120+
"notpossible",
120121
"hidden",
121122
"privileged",
122123
"hasatleast",

0 commit comments

Comments
 (0)