Strengthen the pledge() polyfill

This commit is contained in:
Justine Tunney 2022-06-27 13:01:58 -07:00
parent a6f65eea7c
commit 3c92adfd6e
79 changed files with 1457 additions and 357 deletions

View file

@ -31,6 +31,7 @@
#include "libc/calls/struct/sigaction.h"
#include "libc/errno.h"
#include "libc/intrin/nomultics.internal.h"
#include "libc/intrin/pthread.h"
#include "libc/log/check.h"
#include "libc/macros.internal.h"
#include "libc/mem/mem.h"
@ -68,11 +69,19 @@ static const char *const kKeywordHints[] = {
bool lua_repl_blocking;
bool lua_repl_isterminal;
linenoiseCompletionCallback *lua_repl_completions_callback;
_Alignas(64) char lua_repl_lock;
struct linenoiseState *lua_repl_linenoise;
static lua_State *globalL;
static const char *g_progname;
static const char *g_historypath;
static pthread_mutex_t lua_repl_lock_obj;
void(lua_repl_lock)(void) {
pthread_mutex_lock(&lua_repl_lock_obj);
}
void(lua_repl_unlock)(void) {
pthread_mutex_unlock(&lua_repl_lock_obj);
}
/*
** {==================================================================
@ -239,7 +248,7 @@ static ssize_t pushline (lua_State *L, int firstline) {
prmt = strdup(get_prompt(L, firstline));
lua_pop(L, 1); /* remove prompt */
if (lua_repl_isterminal) {
LUA_REPL_UNLOCK;
lua_repl_unlock();
rc = linenoiseEdit(lua_repl_linenoise, prmt, &b, !firstline || lua_repl_blocking);
free(prmt);
if (rc != -1) {
@ -249,9 +258,9 @@ static ssize_t pushline (lua_State *L, int firstline) {
linenoiseHistorySave(g_historypath);
}
}
LUA_REPL_LOCK;
lua_repl_lock();
} else {
LUA_REPL_UNLOCK;
lua_repl_unlock();
fputs(prmt, stdout);
fflush(stdout);
b = linenoiseGetLine(stdin);
@ -262,7 +271,7 @@ static ssize_t pushline (lua_State *L, int firstline) {
} else {
rc = 0;
}
LUA_REPL_LOCK;
lua_repl_lock();
}
if (!(rc == -1 && errno == EAGAIN)) {
write(1, "\n", 1);
@ -330,7 +339,7 @@ static int multiline (lua_State *L) {
void lua_initrepl(lua_State *L, const char *progname) {
const char *prompt;
LUA_REPL_LOCK;
lua_repl_lock();
g_progname = progname;
if ((lua_repl_isterminal = linenoiseIsTerminal())) {
linenoiseSetCompletionCallback(lua_readline_completions);
@ -349,16 +358,16 @@ void lua_initrepl(lua_State *L, const char *progname) {
__replmode = true;
if (isatty(2)) __replstderr = true;
}
LUA_REPL_UNLOCK;
lua_repl_unlock();
}
void lua_freerepl(void) {
LUA_REPL_LOCK;
lua_repl_lock();
__replmode = false;
linenoiseEnd(lua_repl_linenoise);
free(g_historypath);
LUA_REPL_UNLOCK;
lua_repl_unlock();
}
@ -375,16 +384,16 @@ int lua_loadline (lua_State *L) {
ssize_t rc;
int status;
lua_settop(L, 0);
LUA_REPL_LOCK;
lua_repl_lock();
if ((rc = pushline(L, 1)) != 1) {
LUA_REPL_UNLOCK;
lua_repl_unlock();
return rc - 1; /* eof or error */
}
if ((status = addreturn(L)) != LUA_OK) /* 'return ...' did not work? */
status = multiline(L); /* try as command, maybe with continuation lines */
lua_remove(L, 1); /* remove line from the stack */
lua_assert(lua_gettop(L) == 1);
LUA_REPL_UNLOCK;
lua_repl_unlock();
return status;
}

View file

@ -1,31 +1,19 @@
#ifndef COSMOPOLITAN_THIRD_PARTY_LUA_LREPL_H_
#define COSMOPOLITAN_THIRD_PARTY_LUA_LREPL_H_
#include "libc/dce.h"
#include "libc/intrin/spinlock.h"
#include "libc/intrin/nopl.h"
#include "third_party/linenoise/linenoise.h"
#include "third_party/lua/lauxlib.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
#if !defined(STATIC) && SupportsWindows()
#define LUA_REPL_LOCK _spinlock(&lua_repl_lock)
#else
#define LUA_REPL_LOCK (void)0
#endif
#if !defined(STATIC) && SupportsWindows()
#define LUA_REPL_UNLOCK _spunlock(&lua_repl_lock)
#else
#define LUA_REPL_UNLOCK (void)0
#endif
extern char lua_repl_lock;
extern bool lua_repl_blocking;
extern bool lua_repl_isterminal;
extern struct linenoiseState *lua_repl_linenoise;
extern linenoiseCompletionCallback *lua_repl_completions_callback;
void lua_freerepl(void);
void lua_repl_lock(void);
void lua_repl_unlock(void);
int lua_loadline(lua_State *);
void lua_l_print(lua_State *);
void lua_sigint(lua_State *, int);
@ -36,6 +24,11 @@ void lua_l_message(const char *, const char *);
char *lua_readline_hint(const char *, const char **, const char **);
void lua_readline_completions(const char *, linenoiseCompletions *);
#ifdef _NOPL0
#define lua_repl_lock() _NOPL0("__threadcalls", lua_repl_lock)
#define lua_repl_unlock() _NOPL0("__threadcalls", lua_repl_unlock)
#endif
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_THIRD_PARTY_LUA_LREPL_H_ */

View file

@ -31,6 +31,7 @@
#include "libc/calls/struct/stat.h"
#include "libc/calls/struct/timespec.h"
#include "libc/calls/struct/timeval.h"
#include "libc/calls/struct/winsize.h"
#include "libc/calls/ucontext.h"
#include "libc/dns/dns.h"
#include "libc/errno.h"
@ -1803,6 +1804,29 @@ static int LuaUnixSissock(lua_State *L) {
return 1;
}
// unix.isatty(fd:int)
// ├─→ true
// └─→ nil, unix.Errno
static int LuaUnixIsatty(lua_State *L) {
int olderr = errno;
return SysretBool(L, "isatty", olderr, isatty(luaL_checkinteger(L, 1)));
}
// unix.tiocgwinsz(fd:int)
// ├─→ rows:int, cols:int
// └─→ nil, unix.Errno
static int LuaUnixTiocgwinsz(lua_State *L) {
struct winsize ws;
int olderr = errno;
if (!ioctl(luaL_checkinteger(L, 1), TIOCGWINSZ, &ws)) {
lua_pushinteger(L, ws.ws_row);
lua_pushinteger(L, ws.ws_col);
return 2;
} else {
return SysretErrno(L, "tiocgwinsz", olderr);
}
}
////////////////////////////////////////////////////////////////////////////////
// unix.Stat object
@ -2505,8 +2529,10 @@ static const luaL_Reg kLuaUnix[] = {
{"getsid", LuaUnixGetsid}, // get session id of pid
{"getsockname", LuaUnixGetsockname}, // get address of local end
{"getsockopt", LuaUnixGetsockopt}, // get socket tunings
{"tiocgwinsz", LuaUnixTiocgwinsz}, // pseudoteletypewriter dimensions
{"getuid", LuaUnixGetuid}, // get real user id of process
{"gmtime", LuaUnixGmtime}, // destructure unix timestamp
{"isatty", LuaUnixIsatty}, // detects pseudoteletypewriters
{"kill", LuaUnixKill}, // signal child process
{"link", LuaUnixLink}, // create hard link
{"listen", LuaUnixListen}, // begin listening for clients
@ -2658,6 +2684,7 @@ int LuaUnix(lua_State *L) {
LuaSetIntField(L, "MSG_PEEK", MSG_PEEK);
LuaSetIntField(L, "MSG_OOB", MSG_OOB);
LuaSetIntField(L, "MSG_NOSIGNAL", MSG_NOSIGNAL);
LuaSetIntField(L, "MSG_MORE", MSG_MORE);
// readdir() type
LuaSetIntField(L, "DT_UNKNOWN", DT_UNKNOWN);