This is an archive of the discontinued LLVM Phabricator instance.

[libc] add strsignal and refactor message mapping
ClosedPublic

Authored by michaelrj on Oct 5 2022, 2:19 PM.

Details

Summary

The logic for strsignal and strerror is very similar, so I've moved them
both to use a shared utility (MessageMapper) for the basic
functionality.

Diff Detail

Event Timeline

michaelrj created this revision.Oct 5 2022, 2:19 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptOct 5 2022, 2:19 PM
michaelrj requested review of this revision.Oct 5 2022, 2:19 PM

The new MsgMapper LGTM but I have comments about other parts of the design.

libc/src/__support/StringUtil/error_to_string.cpp
195

It seems to me like we should not have this overload in the stringutil at all because the actual libc functions need to set errno. Instead, provide a function like:

bool fill_unknown_error_msg(int err_num, cpp::span<char> buffer) {
   ...
  // return true if buffer was big enough, else false.
  // This will help strerror_r set errno to ERANGE based on the return value.
}

So, strerror should be implemented like this:

LLVM_LIBC_FUNCTION(... strerror ...) {
  auto err_str = get_error_string(num);
  if (num)
    return const_cast<char *>(num.data());
  errno = EINVAL;
  // unknown_err_buffer is declared in strerror.cpp.
  // In case of strerror_r, it is instantiated from the buffer argument.
  fill_unknown_err_msg(num, unknown_err_buffer);
  return unknown_err_buffer.data();
}
libc/src/__support/StringUtil/message_mapper.h
24

This is a struct so public is default.

69

Why is the const_cast required?

libc/src/__support/StringUtil/signal_to_string.cpp
120

Delete the commented out blocks above?

michaelrj updated this revision to Diff 465580.Oct 5 2022, 3:37 PM
michaelrj marked 3 inline comments as done.

address clean up comments

michaelrj added inline comments.Oct 5 2022, 3:38 PM
libc/src/__support/StringUtil/error_to_string.cpp
195

For comments on the strerror_r design you should comment on this patch instead: https://reviews.llvm.org/D135227 but overall I think the idea makes sense.

michaelrj marked an inline comment as done.Oct 5 2022, 4:07 PM
michaelrj added inline comments.
libc/src/__support/StringUtil/error_to_string.cpp
195

after further testing, it appears that strerror and strerror_r don't actually set errno in existing implementations. Given that information, I don't think this is necessary.

sivachandra added inline comments.Oct 5 2022, 5:00 PM
libc/src/__support/StringUtil/error_to_string.cpp
195

I do not know what the existing implementations do, but https://pubs.opengroup.org/onlinepubs/9699919799/functions/strerror.html seems to indicate that errno "may be set" to indicate error. So, existing implementations have probably chosen to not set errno. Couple of things still:

  1. Our internal API should be clean about indicating errors sufficiently. So, irrespective of setting errno, the API should convey error conditions.
  2. Hidden in my previous comment is a point about changing the buffer type to cpp::span<char>.
michaelrj added inline comments.Oct 6 2022, 9:59 AM
libc/src/__support/StringUtil/error_to_string.cpp
195

The buffer type can't be span because we're not always copying the data into the provided pointer. Most of the time we're returning a static pointer, and span doesn't support changing its underlying pointer.
Additionally, with the way we currently have the function set up it doesn't have true error states. Regardless of the arguments it will always pass back a valid string (unless the pointer isn't valid, in which case it will segfault). I feel that given the situations strerror is likely to be used in, attempting to pass to the client some sort of error state is almost certainly going to be ignored.

michaelrj updated this revision to Diff 465804.Oct 6 2022, 11:22 AM

split out string building function
move to span for mutable strings

sivachandra accepted this revision.Oct 6 2022, 11:39 PM
This revision is now accepted and ready to land.Oct 6 2022, 11:39 PM
This revision was landed with ongoing or failed builds.Oct 7 2022, 11:12 AM
This revision was automatically updated to reflect the committed changes.
libc/src/__support/StringUtil/signal_to_string.cpp