Add cpu / mem / fsz limits to build system

Thanks to all the refactorings we now have the ability to enforce
reasonable limitations on the amount of resources any individual
compile or test can consume. Those limits are currently:

- `-C 8` seconds of 3.1ghz CPU time
- `-M 256mebibytes` of virtual memory
- `-F 100megabyte` limit on file size

Only one file currently needs to exceed these limits:

    o/$(MODE)/third_party/python/Objects/unicodeobject.o: \
        QUOTA += -C16  # overrides cpu limit to 16 seconds

This change introduces a new sizetol() function to LIBC_FMT for parsing
byte or bit size strings with Si unit suffixes. Functions like atoi()
have been rewritten too.
This commit is contained in:
Justine Tunney 2021-08-13 11:18:25 -07:00
parent 9b29358511
commit e963d9c8e3
49 changed files with 1802 additions and 553 deletions

View file

@ -14,6 +14,7 @@
#include "libc/bits/morton.h"
#include "libc/bits/popcnt.h"
#include "libc/calls/calls.h"
#include "libc/errno.h"
#include "libc/fmt/conv.h"
#include "libc/fmt/fmt.h"
#include "libc/fmt/itoa.h"
@ -439,18 +440,26 @@ bool CallFunction(const char *sym) {
return false;
}
volatile const char *literal_;
bool ConsumeLiteral(const char *literal) {
bool r;
char *e;
struct Value x;
literal_ = literal;
errno = 0;
x.t = kInt;
x.i = *literal == '-' ? strtoimax(literal, &e, 0) : strtoumax(literal, &e, 0);
if (!e || *e) {
x.i = strtoimax(literal, &e, 0);
if (*e) {
x.t = kFloat;
x.f = strtod(literal, &e);
if (!e || *e) return false;
r = !(!e || *e);
} else if (errno == ERANGE) {
r = false;
} else {
r = true;
}
Push(x);
return true;
return r;
}
void ConsumeToken(void) {
@ -465,7 +474,7 @@ void ConsumeToken(void) {
default:
if (CallFunction(token.p)) return;
if (ConsumeLiteral(token.p)) return;
Warning("bad token: %s", token.p);
Warning("bad token: %`'s", token.p);
break;
case kUnderflow:
Warning("stack underflow");

View file

@ -37,7 +37,7 @@ nan isnormal ! assert
# BIT ARITHMETIC
-1 ~ 0 = assert
0xffffffffffffffffffffffffffffffff ~ 0 = assert
0 0x7fffffffffffffffffffffffffffffff - -0x7fffffffffffffffffffffffffffffff = assert
0b1010101 popcnt 4 = assert
0b1010101 0b0110101 ^ 0b1100000 = assert
0b1010101 0b0110101 | 0b1110101 = assert

View file

@ -16,22 +16,33 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/assert.h"
#include "libc/bits/safemacros.internal.h"
#include "libc/calls/calls.h"
#include "libc/calls/copyfile.h"
#include "libc/calls/sigbits.h"
#include "libc/calls/struct/rusage.h"
#include "libc/calls/struct/sigset.h"
#include "libc/errno.h"
#include "libc/fmt/conv.h"
#include "libc/log/color.internal.h"
#include "libc/log/log.h"
#include "libc/macros.internal.h"
#include "libc/math.h"
#include "libc/mem/mem.h"
#include "libc/nexgen32e/kcpuids.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/append.internal.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/rlimit.h"
#include "libc/sysv/consts/sig.h"
#include "libc/x/x.h"
#include "third_party/getopt/getopt.h"
long cpuquota = 8; /* secs */
long fszquota = 100 * 1000 * 1000; /* bytes */
long memquota = 256 * 1024 * 1024; /* bytes */
#define MANUAL \
"\
SYNOPSIS\n\
@ -185,9 +196,9 @@ const char *const kGccOnlyFlags[] = {
char *DescribeCommand(void) {
if (iscc) {
if (isgcc) {
return xasprintf("gcc %d", ccversion);
return xasprintf("%s %d", "gcc", ccversion);
} else if (isclang) {
return xasprintf("clang %d", ccversion);
return xasprintf("%s %d", "clang", ccversion);
}
}
return basename(cmd);
@ -273,35 +284,76 @@ void AddArg(char *s) {
command.n += n;
}
} else {
command.p = realloc(command.p, command.n + 2);
command.p[command.n++] = '\r';
command.p = realloc(command.p, command.n + 1);
command.p[command.n++] = '\n';
}
}
int Launch(void) {
int GetBaseCpuFreqMhz(void) {
return KCPUIDS(16H, EAX) & 0x7fff;
}
void SetCpuLimit(int secs) {
int mhz;
struct rlimit rlim;
if (secs < 0) return;
if (IsWindows()) return;
if (!(mhz = GetBaseCpuFreqMhz())) return;
if (getrlimit(RLIMIT_CPU, &rlim) == -1) return;
rlim.rlim_cur = ceil(3100. / mhz * secs);
setrlimit(RLIMIT_CPU, &rlim);
}
void SetFszLimit(long n) {
struct rlimit rlim;
if (n < 0) return;
if (IsWindows()) return;
if (getrlimit(RLIMIT_FSIZE, &rlim) == -1) return;
rlim.rlim_cur = n;
setrlimit(RLIMIT_FSIZE, &rlim);
}
void SetMemLimit(long n) {
struct rlimit rlim = {n, n};
if (n < 0) return;
if (IsWindows() || IsXnu()) return;
setrlimit(!IsOpenbsd() ? RLIMIT_AS : RLIMIT_DATA, &rlim);
}
int Launch(struct rusage *ru) {
int ws, pid;
if ((pid = vfork()) == -1) exit(errno);
if (!pid) {
SetCpuLimit(cpuquota);
SetFszLimit(fszquota);
SetMemLimit(memquota);
sigprocmask(SIG_SETMASK, &savemask, NULL);
execve(cmd, args.p, env.p);
_exit(127);
}
while (waitpid(pid, &ws, 0) == -1) {
while (wait4(pid, &ws, 0, ru) == -1) {
if (errno != EINTR) exit(errno);
}
return ws;
}
char *GetResourceReport(struct rusage *ru) {
char *report = 0;
appendf(&report, "\n");
AppendResourceReport(&report, ru, "\n");
return report;
}
int main(int argc, char *argv[]) {
size_t n;
char *p, **envp;
struct rusage ru;
int i, ws, rc, opt;
/*
* parse prefix arguments
*/
while ((opt = getopt(argc, argv, "?hntA:T:V:")) != -1) {
while ((opt = getopt(argc, argv, "?hntC:M:F:A:T:V:")) != -1) {
switch (opt) {
case 'n':
exit(0);
@ -317,6 +369,15 @@ int main(int argc, char *argv[]) {
case 'V':
ccversion = atoi(optarg);
break;
case 'C':
cpuquota = atoi(optarg);
break;
case 'M':
memquota = sizetol(optarg, 1024);
break;
case 'F':
fszquota = sizetol(optarg, 1000);
break;
case '?':
case 'h':
write(1, MANUAL, sizeof(MANUAL) - 1);
@ -432,7 +493,7 @@ int main(int argc, char *argv[]) {
if (isclang) AddArg(argv[i]);
} else if (isclang && startswith(argv[i], "--debug-prefix-map")) {
/* llvm doesn't provide a gas interface so simulate w/ clang */
AddArg(xasprintf("-f%s", argv[i] + 2));
AddArg(xasprintf("%s%s", "-f", argv[i] + 2));
} else if (isgcc && (!strcmp(argv[i], "-Os") || !strcmp(argv[i], "-O2") ||
!strcmp(argv[i], "-O3"))) {
/* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97623 */
@ -526,8 +587,8 @@ int main(int argc, char *argv[]) {
* log command being run
*/
if (!strcmp(nulltoempty(getenv("V")), "0") && !IsTerminalInarticulate()) {
p = xasprintf("\r\e[K%-15s%s\r", firstnonnull(action, "BUILD"),
firstnonnull(target, nulltoempty(outpath)));
p = (xasprintf)("\r\e[K%-15s%s\r", firstnonnull(action, "BUILD"),
firstnonnull(target, nulltoempty(outpath)));
n = strlen(p);
} else {
if (IsTerminalInarticulate() &&
@ -535,10 +596,9 @@ int main(int argc, char *argv[]) {
command.n > columns + 2) {
/* emacs command window is very slow so truncate lines */
command.n = columns + 2;
command.p[command.n - 5] = '.';
command.p[command.n - 4] = '.';
command.p[command.n - 3] = '.';
command.p[command.n - 2] = '\r';
command.p[command.n - 2] = '.';
command.p[command.n - 1] = '\n';
}
p = command.p;
@ -551,7 +611,7 @@ int main(int argc, char *argv[]) {
* and we help FindDebugBinary to find debug symbols
*/
if (!IsWindows() && endswith(cmd, ".com")) {
comdbg = xasprintf("%s.dbg", cmd);
comdbg = xasprintf("%s%s", cmd, ".dbg");
cachedcmd = xasprintf("o/%s", cmd);
if (fileexists(comdbg)) {
AddEnv(xasprintf("COMDBG=%s", comdbg));
@ -578,7 +638,7 @@ int main(int argc, char *argv[]) {
*/
sigfillset(&mask);
sigprocmask(SIG_BLOCK, &mask, &savemask);
ws = Launch();
ws = Launch(&ru);
/*
* if execve() failed unzip gcc and try again
@ -587,7 +647,7 @@ int main(int argc, char *argv[]) {
startswith(cmd, "o/third_party/gcc") &&
fileexists("third_party/gcc/unbundle.sh")) {
system("third_party/gcc/unbundle.sh");
ws = Launch();
ws = Launch(&ru);
}
/*
@ -613,13 +673,15 @@ int main(int argc, char *argv[]) {
}
return 0;
} else {
p = xasprintf("%s%s EXITED WITH %d%s: %.*s\r\n", RED2, DescribeCommand(),
WEXITSTATUS(ws), RESET, command.n, command.p);
p = xasprintf("%s%s %s %d%s: %.*s%s\n", RED2, DescribeCommand(),
"exited with", WEXITSTATUS(ws), RESET, command.n, command.p,
GetResourceReport(&ru));
rc = WEXITSTATUS(ws);
}
} else {
p = xasprintf("%s%s TERMINATED BY %s%s: %.*s\r\n", RED2, DescribeCommand(),
strsignal(WTERMSIG(ws)), RESET, command.n, command.p);
p = xasprintf("%s%s %s %s%s: %.*s%s\n", RED2, DescribeCommand(),
"terminated by", strsignal(WTERMSIG(ws)), RESET, command.n,
command.p, GetResourceReport(&ru));
rc = 128 + WTERMSIG(ws);
}

View file

@ -325,67 +325,55 @@ void SendRequest(void) {
CHECK_NE(-1, close(fd));
}
bool Recv(unsigned char *p, size_t n) {
size_t i, rc;
static long backoff;
for (i = 0; i < n; i += rc) {
do {
rc = mbedtls_ssl_read(&ezssl, p + i, n - i);
} while (rc == MBEDTLS_ERR_SSL_WANT_READ);
if (!rc || rc == MBEDTLS_ERR_NET_CONN_RESET) {
usleep((backoff = (backoff + 1000) * 2));
return false;
} else if (rc < 0) {
EzTlsDie("read response failed", rc);
}
}
return true;
}
int ReadResponse(void) {
int res;
ssize_t rc;
size_t n, m;
uint32_t size;
unsigned char *p;
enum RunitCommand cmd;
static long backoff;
static unsigned char msg[512];
res = -1;
for (;;) {
while ((rc = mbedtls_ssl_read(&ezssl, msg, sizeof(msg))) < 0) {
if (rc == MBEDTLS_ERR_SSL_WANT_READ) {
continue;
} else if (rc == MBEDTLS_ERR_NET_CONN_RESET) {
CHECK_EQ(ECONNRESET, errno);
usleep((backoff = (backoff + 1000) * 2));
close(g_sock);
return -1;
} else {
EzTlsDie("read response failed", rc);
}
unsigned char b[512];
for (res = -1; res == -1;) {
if (!Recv(b, 5)) break;
CHECK_EQ(RUNITD_MAGIC, READ32BE(b));
switch (b[4]) {
case kRunitExit:
if (!Recv(b, 1)) break;
if ((res = *b)) {
WARNF("%s on %s exited with %d", g_prog, g_hostname, res);
}
break;
case kRunitStderr:
if (!Recv(b, 4)) break;
size = READ32BE(b);
for (; size; size -= n) {
n = MIN(size, sizeof(b));
if (!Recv(b, n)) goto drop;
CHECK_EQ(n, write(2, b, n));
}
break;
default:
fprintf(stderr, "error: received invalid runit command\n");
_exit(1);
}
p = &msg[0];
n = (size_t)rc;
if (!n) break;
do {
CHECK_GE(n, 4 + 1);
CHECK_EQ(RUNITD_MAGIC, READ32BE(p));
p += 4, n -= 4;
cmd = *p++, n--;
switch (cmd) {
case kRunitExit:
CHECK_GE(n, 1);
if ((res = *p & 0xff)) {
WARNF("%s on %s exited with %d", g_prog, g_hostname, res);
}
goto drop;
case kRunitStderr:
CHECK_GE(n, 4);
size = READ32BE(p), p += 4, n -= 4;
while (size) {
if (n) {
CHECK_NE(-1, (rc = write(STDERR_FILENO, p, MIN(n, size))));
CHECK_NE(0, (m = (size_t)rc));
p += m, n -= m, size -= m;
} else {
CHECK_NE(-1, (rc = recv(g_sock, msg, sizeof(msg), 0)));
p = &msg[0];
n = (size_t)rc;
if (!n) goto drop;
}
}
break;
default:
__die();
}
} while (n);
}
drop:
CHECK_NE(-1, close(g_sock));
close(g_sock);
return res;
}