-
Couldn't load subscription status.
- Fork 15k
[libc] Add ctime_s #110676
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
[libc] Add ctime_s #110676
Conversation
| @llvm/pr-subscribers-libc Author: Rajveer Singh Bharadwaj (Rajveer100) ChangesResolves #110548 Definition: int ctime_s(char *buffer, size_t buffer_size, const time_t *t_ptr); Full diff: https://github.com/llvm/llvm-project/pull/110676.diff 2 Files Affected:
diff --git a/libc/src/time/ctime_s.cpp b/libc/src/time/ctime_s.cpp new file mode 100644 index 00000000000000..407e241ab9ef99 --- /dev/null +++ b/libc/src/time/ctime_s.cpp @@ -0,0 +1,39 @@ +//===-- Implementation of ctime_s function --------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "ctime_s.h" +#include "src/__support/CPP/limits.h" +#include "src/__support/common.h" +#include "src/__support/macros/config.h" +#include "time_utils.h" +#include <cerrno> + +namespace LIBC_NAMESPACE_DECL { + +using LIBC_NAMESPACE::time_utils::TimeConstants; + +LLVM_LIBC_FUNCTION(int, ctime_s, + (char *buffer, size_t buffer_size, const time_t *t_ptr)) { + if (t_ptr == nullptr || buffer == nullptr || + *time > cpp::numeric_limits<int32_t>::max()) { + return EINVAL; + } + + if (buffer_size < TimeConstants::ASCTIME_MAX_BYTES) { + return ERANGE; + } + + if (time_utils::asctime(time_utils::localtime(t_ptr), buffer, buffer_size) == + nullptr) { + return EINVAL; + } + + return 0; +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/time/ctime_s.h b/libc/src/time/ctime_s.h new file mode 100644 index 00000000000000..023c4b5b3cbc7f --- /dev/null +++ b/libc/src/time/ctime_s.h @@ -0,0 +1,21 @@ +//===-- Implementation header of ctime_s ------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC_TIME_CTIME_S_H +#define LLVM_LIBC_SRC_TIME_CTIME_S_H + +#include "hdr/types/time_t.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +int ctime_s(char *buffer, size_t buffer_size, const time_t *t_ptr); + +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC_TIME_CTIME_S_H |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the patch!
In addition to the implementation, we'll need to add cmake modifications to build it, and unit tests so that we have some level of confidence in the implementation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great. Thank you for the pull request. Could you also add the following:
- We also need to add
ctime_sto the cmake file so it will get included in the build,libc/src/time/CMakeLists.txt. - We should also add
ctime_sto the different tested architectures inlibc/config/<...>/<...>/entrypoints.txt. - newhdrgen:
libc/newhdrgen/yaml/time.yaml - spec file:
libc/spec/stdc.td - Could you also add some tests? These are in
libc/test/src/time/. Make sure to include the tests in the cmake file,libc/test/src/time/CMakeLists.txt. You can run tests for this withninjaormake,ninja libc.test.src.time.ctime_s_test.
| In the entry points of some of the architectures, |
| I'd recommend not adding entrypoints to any architectures you haven't tested the functions on. |
269dd2c to 6db439c Compare | ✅ With the latest revision this PR passed the C/C++ code formatter. |
6db439c to bef8896 Compare
That is correct. I have updated the list in my comment. |
| I have added the entry points only in places where I believe this should be fine? |
bef8896 to c7692cc Compare There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
| Thanks for the approval, could you land this for me?! |
| This looks good to me as well. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This patch doesn't compile for me. Mind taking a look?
| @nickdesaulniers |
| linux |
| Why are there no entry points for |
| MacOS currently has minimal support since we don't have any users who need it. I'm hoping that soon we'll get a buildbot working for MacOS, which will help with keeping it in a working state. There are also some issues around fullbuild on MacOS, which if I remember correctly boil down to "Apple doesn't want you calling syscalls directly, they want you to use their libc". |
| Makes sense, I did face errors when trying to do so. |
No worries! Apart from the constraint violation part, I have done rest of the changes. |
00248c1 to 1cede39 Compare 1cede39 to f7971dd Compare 73a5d71 to 73ad63f Compare 73ad63f to fd7a972 Compare a5c4480 to 2c47f7d Compare | I have made the changes, although a little unsure about the build failure? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry all of the intricacies around STDC_WANT_LIB_EXT1. Annex K is somewhat taboo, and you're laying much of the groundwork here for supporting it.
77dc895 to 5006fad Compare Resolves llvm#110548 Definition: ```c++ int ctime_s(char *buffer, rsize_t buffer_size, const time_t *t_ptr); ```
5006fad to 1a7dd36 Compare There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, thanks for working through the feedback. I think this is ready to land. Do you need us to merge this for you?
| Sure, I don't have access to merge :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm going to reread those two WG 21 papers to better familiarize myself towards the argument for Annex K.
For now, I'm going to request changes to block this from landing until I make a decision wrt. Annex K support. I believe we currently don't support Annex K at all, so I need to think hard about whether we intend to open this can of worms.
| return EINVAL; | ||
| | ||
| if (buffer_size < time_constants::ASCTIME_MAX_BYTES || | ||
| buffer_size > RSIZE_MAX) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm...so I went to test this with GCC and found that:
/android0/llvm-project/libc/src/time/ctime_s.cpp:30:21: error: ‘RSIZE_MAX’ was not declared in this scope; did you mean ‘SIZE_MAX’? 30 | buffer_size > RSIZE_MAX) { | ^~~~~~~~~ | SIZE_MAX Looking into it, I found that clang's stdint provides RSIZE_MAX (example: /usr/lib/llvm-16/lib/clang/16/include/stdint.h), but GCC does not (example: /usr/lib/gcc/x86_64-linux-gnu/14/include/stdint.h).
Grepping gcc's sources for __STDC_LIB_EXT1__ turns up no hits (one hit in the test suite that looks irrelevant). So looking into why there's seemingly no support for Annex K turned up this mailing list thread: https://gcc.gnu.org/pipermail/gcc/2019-December/231068.html
Which links to this paper documenting in explicit detail glibc developers experience with trying to implement Annex K: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1967.htm
95f1de3 added support for RSIZE_MAX and rsize_t without mentioning who was interested in Annex K support.
https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2336.pdf looks like a more recent rebuttal to n1967.
Resolves #110548
Definition: