Skip to content

Commit

Permalink
[libc] move strerror and strsignal to OS msg maps
Browse files Browse the repository at this point in the history
Other OSes may have different mappings from error number to message.
This creates a system to allow new platforms to define their own
mappings.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D147967
  • Loading branch information
michaelrj-google committed Apr 18, 2023
1 parent 6e9ca8c commit 0acb639
Show file tree
Hide file tree
Showing 15 changed files with 501 additions and 208 deletions.
34 changes: 19 additions & 15 deletions libc/src/__support/StringUtil/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,41 @@ add_header_library(
HDRS
message_mapper.h
DEPENDS
libc.src.__support.CPP.string_view
libc.src.__support.CPP.optional
libc.src.__support.CPP.string_view
libc.src.__support.CPP.optional
)

# The table maps depend on message_mapper.
add_subdirectory(tables)

add_object_library(
error_to_string
HDRS
error_to_string.h
SRCS
error_to_string.cpp
DEPENDS
.message_mapper
libc.src.errno.errno
libc.src.__support.CPP.span
libc.src.__support.CPP.string_view
libc.src.__support.CPP.stringstream
libc.src.__support.integer_to_string
.message_mapper
libc.src.errno.errno
libc.src.__support.CPP.span
libc.src.__support.CPP.string_view
libc.src.__support.CPP.stringstream
libc.src.__support.integer_to_string
libc.src.__support.StringUtil.tables.error_table
)


add_object_library(
signal_to_string
HDRS
signal_to_string.h
SRCS
signal_to_string.cpp
DEPENDS
.message_mapper
libc.include.signal
libc.src.__support.CPP.span
libc.src.__support.CPP.string_view
libc.src.__support.CPP.stringstream
libc.src.__support.integer_to_string
.message_mapper
libc.include.signal
libc.src.__support.CPP.span
libc.src.__support.CPP.string_view
libc.src.__support.CPP.stringstream
libc.src.__support.integer_to_string
libc.src.__support.StringUtil.tables.signal_table
)
164 changes: 16 additions & 148 deletions libc/src/__support/StringUtil/error_to_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@

#include "src/__support/StringUtil/error_to_string.h"

#include "src/errno/libc_errno.h" // For error macros

#include "src/__support/CPP/array.h"
#include "src/__support/CPP/span.h"
#include "src/__support/CPP/string_view.h"
#include "src/__support/CPP/stringstream.h"
#include "src/__support/StringUtil/message_mapper.h"
#include "src/__support/integer_to_string.h"
#include "src/errno/libc_errno.h" // For error macros

#include "src/__support/StringUtil/tables/error_table.h"

#include <stddef.h>

Expand All @@ -33,157 +37,21 @@ constexpr size_t max_buff_size() {
constexpr size_t ERR_BUFFER_SIZE = max_buff_size();
thread_local char error_buffer[ERR_BUFFER_SIZE];

constexpr size_t RAW_ARRAY_LEN = PLATFORM_ERRORS.size();
constexpr size_t TOTAL_STR_LEN =
total_str_len(PLATFORM_ERRORS.data(), RAW_ARRAY_LEN);

// Since the StringMappings array is a map from error numbers to their
// corresponding strings, we have to have an array large enough we can use the
// error numbers as indexes. Thankfully there are 132 errors in the above list
// (41 and 58 are skipped) and the highest number is 133. If other platforms use
// different error numbers, then this number may need to be adjusted.
// Also if negative numbers or particularly large numbers are used, then the
// array should be turned into a proper hashmap.
constexpr size_t ERR_ARRAY_SIZE = 134;

constexpr MsgMapping raw_err_array[] = {
MsgMapping(0, "Success"),
MsgMapping(EPERM, "Operation not permitted"),
MsgMapping(ENOENT, "No such file or directory"),
MsgMapping(ESRCH, "No such process"),
MsgMapping(EINTR, "Interrupted system call"),
MsgMapping(EIO, "Input/output error"),
MsgMapping(ENXIO, "No such device or address"),
MsgMapping(E2BIG, "Argument list too long"),
MsgMapping(ENOEXEC, "Exec format error"),
MsgMapping(EBADF, "Bad file descriptor"),
MsgMapping(ECHILD, "No child processes"),
MsgMapping(EAGAIN, "Resource temporarily unavailable"),
MsgMapping(ENOMEM, "Cannot allocate memory"),
MsgMapping(EACCES, "Permission denied"),
MsgMapping(EFAULT, "Bad address"),
MsgMapping(ENOTBLK, "Block device required"),
MsgMapping(EBUSY, "Device or resource busy"),
MsgMapping(EEXIST, "File exists"),
MsgMapping(EXDEV, "Invalid cross-device link"),
MsgMapping(ENODEV, "No such device"),
MsgMapping(ENOTDIR, "Not a directory"),
MsgMapping(EISDIR, "Is a directory"),
MsgMapping(EINVAL, "Invalid argument"),
MsgMapping(ENFILE, "Too many open files in system"),
MsgMapping(EMFILE, "Too many open files"),
MsgMapping(ENOTTY, "Inappropriate ioctl for device"),
MsgMapping(ETXTBSY, "Text file busy"),
MsgMapping(EFBIG, "File too large"),
MsgMapping(ENOSPC, "No space left on device"),
MsgMapping(ESPIPE, "Illegal seek"),
MsgMapping(EROFS, "Read-only file system"),
MsgMapping(EMLINK, "Too many links"),
MsgMapping(EPIPE, "Broken pipe"),
MsgMapping(EDOM, "Numerical argument out of domain"),
MsgMapping(ERANGE, "Numerical result out of range"),
MsgMapping(EDEADLK, "Resource deadlock avoided"),
MsgMapping(ENAMETOOLONG, "File name too long"),
MsgMapping(ENOLCK, "No locks available"),
MsgMapping(ENOSYS, "Function not implemented"),
MsgMapping(ENOTEMPTY, "Directory not empty"),
MsgMapping(ELOOP, "Too many levels of symbolic links"),
// No error for 41. Would be EWOULDBLOCK
MsgMapping(ENOMSG, "No message of desired type"),
MsgMapping(EIDRM, "Identifier removed"),
MsgMapping(ECHRNG, "Channel number out of range"),
MsgMapping(EL2NSYNC, "Level 2 not synchronized"),
MsgMapping(EL3HLT, "Level 3 halted"),
MsgMapping(EL3RST, "Level 3 reset"),
MsgMapping(ELNRNG, "Link number out of range"),
MsgMapping(EUNATCH, "Protocol driver not attached"),
MsgMapping(ENOCSI, "No CSI structure available"),
MsgMapping(EL2HLT, "Level 2 halted"),
MsgMapping(EBADE, "Invalid exchange"),
MsgMapping(EBADR, "Invalid request descriptor"),
MsgMapping(EXFULL, "Exchange full"),
MsgMapping(ENOANO, "No anode"),
MsgMapping(EBADRQC, "Invalid request code"),
MsgMapping(EBADSLT, "Invalid slot"),
// No error for 58. Would be EDEADLOCK.
MsgMapping(EBFONT, "Bad font file format"),
MsgMapping(ENOSTR, "Device not a stream"),
MsgMapping(ENODATA, "No data available"),
MsgMapping(ETIME, "Timer expired"),
MsgMapping(ENOSR, "Out of streams resources"),
MsgMapping(ENONET, "Machine is not on the network"),
MsgMapping(ENOPKG, "Package not installed"),
MsgMapping(EREMOTE, "Object is remote"),
MsgMapping(ENOLINK, "Link has been severed"),
MsgMapping(EADV, "Advertise error"),
MsgMapping(ESRMNT, "Srmount error"),
MsgMapping(ECOMM, "Communication error on send"),
MsgMapping(EPROTO, "Protocol error"),
MsgMapping(EMULTIHOP, "Multihop attempted"),
MsgMapping(EDOTDOT, "RFS specific error"),
MsgMapping(EBADMSG, "Bad message"),
MsgMapping(EOVERFLOW, "Value too large for defined data type"),
MsgMapping(ENOTUNIQ, "Name not unique on network"),
MsgMapping(EBADFD, "File descriptor in bad state"),
MsgMapping(EREMCHG, "Remote address changed"),
MsgMapping(ELIBACC, "Can not access a needed shared library"),
MsgMapping(ELIBBAD, "Accessing a corrupted shared library"),
MsgMapping(ELIBSCN, ".lib section in a.out corrupted"),
MsgMapping(ELIBMAX, "Attempting to link in too many shared libraries"),
MsgMapping(ELIBEXEC, "Cannot exec a shared library directly"),
MsgMapping(EILSEQ, "Invalid or incomplete multibyte or wide character"),
MsgMapping(ERESTART, "Interrupted system call should be restarted"),
MsgMapping(ESTRPIPE, "Streams pipe error"),
MsgMapping(EUSERS, "Too many users"),
MsgMapping(ENOTSOCK, "Socket operation on non-socket"),
MsgMapping(EDESTADDRREQ, "Destination address required"),
MsgMapping(EMSGSIZE, "Message too long"),
MsgMapping(EPROTOTYPE, "Protocol wrong type for socket"),
MsgMapping(ENOPROTOOPT, "Protocol not available"),
MsgMapping(EPROTONOSUPPORT, "Protocol not supported"),
MsgMapping(ESOCKTNOSUPPORT, "Socket type not supported"),
MsgMapping(ENOTSUP, "Operation not supported"),
MsgMapping(EPFNOSUPPORT, "Protocol family not supported"),
MsgMapping(EAFNOSUPPORT, "Address family not supported by protocol"),
MsgMapping(EADDRINUSE, "Address already in use"),
MsgMapping(EADDRNOTAVAIL, "Cannot assign requested address"),
MsgMapping(ENETDOWN, "Network is down"),
MsgMapping(ENETUNREACH, "Network is unreachable"),
MsgMapping(ENETRESET, "Network dropped connection on reset"),
MsgMapping(ECONNABORTED, "Software caused connection abort"),
MsgMapping(ECONNRESET, "Connection reset by peer"),
MsgMapping(ENOBUFS, "No buffer space available"),
MsgMapping(EISCONN, "Transport endpoint is already connected"),
MsgMapping(ENOTCONN, "Transport endpoint is not connected"),
MsgMapping(ESHUTDOWN, "Cannot send after transport endpoint shutdown"),
MsgMapping(ETOOMANYREFS, "Too many references: cannot splice"),
MsgMapping(ETIMEDOUT, "Connection timed out"),
MsgMapping(ECONNREFUSED, "Connection refused"),
MsgMapping(EHOSTDOWN, "Host is down"),
MsgMapping(EHOSTUNREACH, "No route to host"),
MsgMapping(EALREADY, "Operation already in progress"),
MsgMapping(EINPROGRESS, "Operation now in progress"),
MsgMapping(ESTALE, "Stale file handle"),
MsgMapping(EUCLEAN, "Structure needs cleaning"),
MsgMapping(ENOTNAM, "Not a XENIX named type file"),
MsgMapping(ENAVAIL, "No XENIX semaphores available"),
MsgMapping(EISNAM, "Is a named type file"),
MsgMapping(EREMOTEIO, "Remote I/O error"),
MsgMapping(EDQUOT, "Disk quota exceeded"),
MsgMapping(ENOMEDIUM, "No medium found"),
MsgMapping(EMEDIUMTYPE, "Wrong medium type"),
MsgMapping(ECANCELED, "Operation canceled"),
MsgMapping(ENOKEY, "Required key not available"),
MsgMapping(EKEYEXPIRED, "Key has expired"),
MsgMapping(EKEYREVOKED, "Key has been revoked"),
MsgMapping(EKEYREJECTED, "Key was rejected by service"),
MsgMapping(EOWNERDEAD, "Owner died"),
MsgMapping(ENOTRECOVERABLE, "State not recoverable"),
MsgMapping(ERFKILL, "Operation not possible due to RF-kill"),
MsgMapping(EHWPOISON, "Memory page has hardware error"),
};

constexpr size_t RAW_ARRAY_LEN = sizeof(raw_err_array) / sizeof(MsgMapping);
constexpr size_t TOTAL_STR_LEN = total_str_len(raw_err_array, RAW_ARRAY_LEN);
// error numbers as indexes. The current linux configuration has 132 values with
// the maximum value being 133 (41 and 58 are skipped). If other platforms use
// negative numbers or discontiguous ranges, then the array should be turned
// into a proper hashmap.
constexpr size_t ERR_ARRAY_SIZE =
max_key_val(PLATFORM_ERRORS.data(), RAW_ARRAY_LEN) + 1;

static constexpr MessageMapper<ERR_ARRAY_SIZE, TOTAL_STR_LEN>
error_mapper(raw_err_array, RAW_ARRAY_LEN);
error_mapper(PLATFORM_ERRORS.data(), RAW_ARRAY_LEN);

cpp::string_view build_error_string(int err_num, cpp::span<char> buffer) {
// if the buffer can't hold "Unknown error" + ' ' + num_str, then just
Expand Down
6 changes: 3 additions & 3 deletions libc/src/__support/StringUtil/error_to_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
//
//===----------------------------------------------------------------------===//

#include "src/__support/CPP/span.h"
#include "src/__support/CPP/string_view.h"

#ifndef LLVM_LIBC_SRC_SUPPORT_ERROR_TO_STRING_H
#define LLVM_LIBC_SRC_SUPPORT_ERROR_TO_STRING_H

#include "src/__support/CPP/span.h"
#include "src/__support/CPP/string_view.h"

namespace __llvm_libc {

cpp::string_view get_error_string(int err_num);
Expand Down
28 changes: 28 additions & 0 deletions libc/src/__support/StringUtil/message_mapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#ifndef LLVM_LIBC_SRC_SUPPORT_STRING_UTIL_MESSAGE_MAPPER_H
#define LLVM_LIBC_SRC_SUPPORT_STRING_UTIL_MESSAGE_MAPPER_H

#include "src/__support/CPP/array.h"
#include "src/__support/CPP/optional.h"
#include "src/__support/CPP/string_view.h"
#include <stddef.h>
Expand All @@ -20,6 +21,8 @@ struct MsgMapping {
int num;
cpp::string_view msg;

constexpr MsgMapping() : num(0), msg() { ; }

constexpr MsgMapping(int init_num, const char *init_msg)
: num(init_num), msg(init_msg) {
;
Expand All @@ -35,6 +38,18 @@ constexpr size_t total_str_len(const MsgMapping *array, size_t len) {
return total;
}

constexpr size_t max_key_val(const MsgMapping *array, size_t len) {
int max = 0;
for (size_t i = 0; i < len; ++i) {
if (array[i].num > max) {
max = array[i].num;
}
}
// max will never be negative since the starting value is 0. This is good,
// since it's used as a length.
return static_cast<size_t>(max);
}

template <size_t ARR_SIZE, size_t TOTAL_STR_LEN> class MessageMapper {
int msg_offsets[ARR_SIZE] = {-1};
char string_array[TOTAL_STR_LEN] = {'\0'};
Expand Down Expand Up @@ -71,6 +86,19 @@ template <size_t ARR_SIZE, size_t TOTAL_STR_LEN> class MessageMapper {
}
};

template <size_t N> using MsgTable = cpp::array<MsgMapping, N>;

template <size_t N1, size_t N2>
constexpr MsgTable<N1 + N2> operator+(const MsgTable<N1> &t1,
const MsgTable<N2> &t2) {
MsgTable<N1 + N2> res{};
for (size_t i = 0; i < N1; ++i)
res[i] = t1[i];
for (size_t i = 0; i < N2; ++i)
res[N1 + i] = t2[i];
return res;
}

} // namespace internal
} // namespace __llvm_libc

Expand Down
50 changes: 8 additions & 42 deletions libc/src/__support/StringUtil/signal_to_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "src/__support/CPP/string_view.h"
#include "src/__support/CPP/stringstream.h"
#include "src/__support/StringUtil/message_mapper.h"
#include "src/__support/StringUtil/tables/signal_table.h"
#include "src/__support/integer_to_string.h"

#include <signal.h>
Expand All @@ -33,50 +34,15 @@ constexpr size_t max_buff_size() {
constexpr size_t SIG_BUFFER_SIZE = max_buff_size();
thread_local char signal_buffer[SIG_BUFFER_SIZE];

constexpr MsgMapping raw_sig_array[] = {
MsgMapping(SIGHUP, "Hangup"), MsgMapping(SIGINT, "Interrupt"),
MsgMapping(SIGQUIT, "Quit"), MsgMapping(SIGILL, "Illegal instruction"),
MsgMapping(SIGTRAP, "Trace/breakpoint trap"),
MsgMapping(SIGABRT, "Aborted"), MsgMapping(SIGBUS, "Bus error"),
MsgMapping(SIGFPE, "Floating point exception"),
MsgMapping(SIGKILL, "Killed"), MsgMapping(SIGUSR1, "User defined signal 1"),
MsgMapping(SIGSEGV, "Segmentation fault"),
MsgMapping(SIGUSR2, "User defined signal 2"),
MsgMapping(SIGPIPE, "Broken pipe"), MsgMapping(SIGALRM, "Alarm clock"),
MsgMapping(SIGTERM, "Terminated"),
// SIGSTKFLT (may not exist)
MsgMapping(SIGCHLD, "Child exited"), MsgMapping(SIGCONT, "Continued"),
MsgMapping(SIGSTOP, "Stopped (signal)"), MsgMapping(SIGTSTP, "Stopped"),
MsgMapping(SIGTTIN, "Stopped (tty input)"),
MsgMapping(SIGTTOU, "Stopped (tty output)"),
MsgMapping(SIGURG, "Urgent I/O condition"),
MsgMapping(SIGXCPU, "CPU time limit exceeded"),
MsgMapping(SIGXFSZ, "File size limit exceeded"),
MsgMapping(SIGVTALRM, "Virtual timer expired"),
MsgMapping(SIGPROF, "Profiling timer expired"),
MsgMapping(SIGWINCH, "Window changed"), MsgMapping(SIGPOLL, "I/O possible"),
// SIGPWR (may not exist)
MsgMapping(SIGSYS, "Bad system call"),

#ifdef SIGSTKFLT
MsgMapping(SIGSTKFLT, "Stack fault"), // unused
#endif
#ifdef SIGPWR
MsgMapping(SIGPWR, "Power failure"), // ignored
#endif
};

// Since the string_mappings array is a map from signal numbers to their
// corresponding strings, we have to have an array large enough we can use the
// signal numbers as indexes. The highest signal is SIGSYS at 31, so an array of
// 32 elements will be large enough to hold all of them.
constexpr size_t SIG_ARRAY_SIZE = 32;

constexpr size_t RAW_ARRAY_LEN = sizeof(raw_sig_array) / sizeof(MsgMapping);
constexpr size_t TOTAL_STR_LEN = total_str_len(raw_sig_array, RAW_ARRAY_LEN);
constexpr size_t RAW_ARRAY_LEN = PLATFORM_SIGNALS.size();
constexpr size_t TOTAL_STR_LEN =
total_str_len(PLATFORM_SIGNALS.data(), RAW_ARRAY_LEN);

constexpr size_t SIG_ARRAY_SIZE =
max_key_val(PLATFORM_SIGNALS.data(), RAW_ARRAY_LEN) + 1;

static constexpr MessageMapper<SIG_ARRAY_SIZE, TOTAL_STR_LEN>
signal_mapper(raw_sig_array, RAW_ARRAY_LEN);
signal_mapper(PLATFORM_SIGNALS.data(), RAW_ARRAY_LEN);

cpp::string_view build_signal_string(int sig_num, cpp::span<char> buffer) {
cpp::string_view base_str;
Expand Down
Loading

0 comments on commit 0acb639

Please sign in to comment.