Lots of work on removing malloc from driver

This commit is contained in:
2022-02-17 20:02:08 -05:00
parent 9d7bf8b0f9
commit 467f25b927
17 changed files with 1301 additions and 197 deletions

View File

@@ -1,5 +1,7 @@
#include "driver.hpp"
#include "umm-malloc.hpp"
#include "driver-util.hpp"
#include "util.hpp"
#include "drivenengine.hpp"
#include "dummycert.hpp"
@@ -31,11 +33,10 @@
using SOCKET=int;
const int INVALID_SOCKET = -1;
using PollVector = std::vector<struct pollfd>;
struct termios orig_termios;
static std::string strerror_str(int err) {
static UmmString strerror_str(int err) {
char errbuf[256];
return strerror_r(errno, errbuf, 256);
}
@@ -65,12 +66,11 @@ static void enable_tty_raw() {
assert(status >= 0);
}
static SOCKET open_connection(const std::string &target, std::string &err) {
static SOCKET open_connection(std::string_view target, UmmString &err) {
struct addrinfo *addrs = nullptr;
struct addrinfo *goodaddr = nullptr;
struct addrinfo hints;
SOCKET sock = INVALID_SOCKET;
std::string host, port;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
@@ -79,7 +79,8 @@ static SOCKET open_connection(const std::string &target, std::string &err) {
hints.ai_flags = AI_NUMERICSERV;
err.clear();
util::split_host_port(target, host, port);
UmmString host, port;
drv::split_host_port(target, host, port);
int status = getaddrinfo(host.c_str(), port.c_str(), &hints, &addrs);
if (status != 0) {
err = gai_strerror(status);
@@ -111,7 +112,7 @@ error_general:
return INVALID_SOCKET;
}
static SOCKET listen_on_port(int port, std::string &err) {
static SOCKET listen_on_port(int port, UmmString &err) {
int status, enable;
err.clear();
@@ -142,7 +143,7 @@ error_errno:
return INVALID_SOCKET;
}
static SOCKET accept_on_socket(SOCKET listen_socket, std::string &err) {
static SOCKET accept_on_socket(SOCKET listen_socket, UmmString &err) {
err.clear();
SOCKET chsock = accept(listen_socket, nullptr, nullptr);
if (chsock >= 0) {
@@ -162,7 +163,7 @@ static SOCKET accept_on_socket(SOCKET listen_socket, std::string &err) {
// zero: would block
// negative: channel closed, possibly cleanly or possibly with error
//
static int socket_send(SOCKET socket, const char *bytes, int nbytes, std::string &err) {
static int socket_send(SOCKET socket, const char *bytes, int nbytes, UmmString &err) {
err.clear();
int wbytes = send(socket, bytes, nbytes, 0);
if (wbytes < 0) {
@@ -177,7 +178,7 @@ static int socket_send(SOCKET socket, const char *bytes, int nbytes, std::string
}
}
static int socket_recv(SOCKET socket, char *bytes, int nbytes, std::string &err) {
static int socket_recv(SOCKET socket, char *bytes, int nbytes, UmmString &err) {
err.clear();
int nrecv = recv(socket, bytes, nbytes, 0);
if (nrecv < 0) {
@@ -198,8 +199,17 @@ static int socket_close(SOCKET socket) {
return close(socket);
}
static int socket_poll(PollVector &pollvec, int mstimeout, std::string &err) {
int status = poll(&pollvec[0], pollvec.size(), mstimeout);
static int socket_poll(struct pollfd *pollvec, int pollcount, int mstimeout, UmmString &err) {
// socket_poll is implicitly expected to also poll stdin,
// if the OS allows that. Linux does, so we add stdin to the
// poll vector. The poll vector is required to have at
// least one free space in order to do this.
pollvec[pollcount].fd = 0;
pollvec[pollcount].events = POLLIN;
pollcount += 1;
// Do the poll.
int status = poll(pollvec, pollcount, mstimeout);
if (status < 0) {
err = strerror_str(errno);
return -1;
@@ -207,14 +217,6 @@ static int socket_poll(PollVector &pollvec, int mstimeout, std::string &err) {
return 0;
}
static void socket_init() {
// Nothing needed on linux
}
static void socket_uninit() {
// Nothing needed on linux
}
static int console_write(const char *bytes, int nbytes) {
return write(1, bytes, nbytes);
}
@@ -223,15 +225,7 @@ static int console_read(char *bytes, int nbytes) {
return read(0, bytes, nbytes);
}
// The last element in the vector is supposed to be
// for polling stdio. But on windows, you can't poll
// stdio, so on windows, we remove the last element from
// the vector and we reduce mstimeout instead.
static void fill_stdio_pollfd(PollVector &pollvec, int &mstimeout, bool read_console_recently) {
struct pollfd &stdiopoll = pollvec.back();
stdiopoll.fd = 0;
stdiopoll.events = POLLIN;
}
static void disable_randomization(int argc, char *argv[]) {
const int old_personality = personality(ADDR_NO_RANDOMIZE);
@@ -243,6 +237,11 @@ static void disable_randomization(int argc, char *argv[]) {
}
}
void driver_sysinit(int argc, char *argv[]) {
disable_randomization(argc, argv);
enable_tty_raw();
}
class MonoClock {
private:
struct timespec base_;