Skip to content

[libc] Allow time conversions to compile on bare metal #102014

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

Merged
merged 2 commits into from
Aug 7, 2024

Conversation

statham-arm
Copy link
Collaborator

@statham-arm statham-arm commented Aug 5, 2024

The <time.h> functions asctime, gmtime, mktime and their _r variants are purely computational functions which convert between well defined time representations and/or strings. There's no reason these shouldn't be available in bare-metal builds of the library as well as hosted ones: even if the library has no way to find out the time in POSIX format, it might still see POSIX-style time_t values in input data (e.g. network protocols) and need to interpret them.

The only obstacle to this was that the out_of_range() helper function set errno to EOVERFLOW, which fails in a bare-metal build because the extra POSIX error values aren't defined, including EOVERFLOW. So I've made that assignment conditional on EOVERFLOW being defined.


Fixes #85556

The `<time.h>` functions `asctime`, `gmtime`, `mktime` and their `_r`
variants are purely computational functions which convert between well
defined time representations and/or strings. There's no reason these
shouldn't be available in bare-metal builds of the library as well as
hosted ones: even if the library has no way to find out the time in
POSIX format, it might still see POSIX-style `time_t` values in input
data (e.g. network protocols) and need to interpret them.

The only obstacle to this was that the `out_of_range()` helper
function set `errno` to `EOVERFLOW`, which fails in a bare-metal build
because the extra POSIX error values aren't defined, including
`EOVERFLOW`. So I've made that assignment conditional on `EOVERFLOW`
being defined.
@llvmbot
Copy link
Member

llvmbot commented Aug 5, 2024

@llvm/pr-subscribers-libc

Author: Simon Tatham (statham-arm)

Changes

The &lt;time.h&gt; functions asctime, gmtime, mktime and their _r variants are purely computational functions which convert between well defined time representations and/or strings. There's no reason these shouldn't be available in bare-metal builds of the library as well as hosted ones: even if the library has no way to find out the time in POSIX format, it might still see POSIX-style time_t values in input data (e.g. network protocols) and need to interpret them.

The only obstacle to this was that the out_of_range() helper function set errno to EOVERFLOW, which fails in a bare-metal build because the extra POSIX error values aren't defined, including EOVERFLOW. So I've made that assignment conditional on EOVERFLOW being defined.


Full diff: https://github.com/llvm/llvm-project/pull/102014.diff

2 Files Affected:

  • (modified) libc/config/baremetal/arm/entrypoints.txt (+5)
  • (modified) libc/src/time/time_utils.h (+5)
diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index b083745d5e082..d0f7bdc974252 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -201,7 +201,12 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.stdlib.strtoull
 
     # time.h entrypoints
+    libc.src.time.asctime
+    libc.src.time.asctime_r
     libc.src.time.difftime
+    libc.src.time.gmtime
+    libc.src.time.gmtime_r
+    libc.src.time.mktime
 
     # internal entrypoints
     libc.startup.baremetal.init
diff --git a/libc/src/time/time_utils.h b/libc/src/time/time_utils.h
index 106870af72997..47f55f7d38912 100644
--- a/libc/src/time/time_utils.h
+++ b/libc/src/time/time_utils.h
@@ -92,7 +92,12 @@ extern int64_t update_from_seconds(int64_t total_seconds, struct tm *tm);
 
 // POSIX.1-2017 requires this.
 LIBC_INLINE time_t out_of_range() {
+#ifdef EOVERFLOW
+  // For non-POSIX uses of the standard C time functions, where EOVERFLOW is
+  // not defined, it's OK not to set errno at all. The plain C standard doesn't
+  // require it.
   libc_errno = EOVERFLOW;
+#endif
   return TimeConstants::OUT_OF_RANGE_RETURN_VALUE;
 }
 

@petrhosek
Copy link
Member

Can you also include these in the riscv baremetal entrypoints? Can you include "fixes #85556" in the description?

@statham-arm
Copy link
Collaborator Author

Can you also include these in the riscv baremetal entrypoints?

Done, in the followup commit I just pushed.

Can you include "fixes #85556" in the description?

I can try, but I don't think I've had to do that before in this repository! In Github repos that don't enforce squash-and-merge, I'd do it by force-pushing the PR branch. But here I don't know whether I need to do that, or if editing the PR description is enough. If you know which of those will work, please let me know. Otherwise I'll do my best, and manually close the bug afterwards if it doesn't do the right thing.

@petrhosek
Copy link
Member

Can you also include these in the riscv baremetal entrypoints?

Done, in the followup commit I just pushed.

Can you include "fixes #85556" in the description?

I can try, but I don't think I've had to do that before in this repository! In Github repos that don't enforce squash-and-merge, I'd do it by force-pushing the PR branch. But here I don't know whether I need to do that, or if editing the PR description is enough. If you know which of those will work, please let me know. Otherwise I'll do my best, and manually close the bug afterwards if it doesn't do the right thing.

You can just add it to the PR description, GitHub will pick it up.

Copy link
Contributor

@michaelrj-google michaelrj-google left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@SchrodingerZhu
Copy link
Contributor

I have added issue link for you. Feel free to adjust in any way you want.

@statham-arm statham-arm merged commit efd71d9 into llvm:main Aug 7, 2024
6 checks passed
@statham-arm statham-arm deleted the baremetal-time-conversions branch August 7, 2024 08:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

src/time/time_utils.h unconditionally uses EOVERFLOW
5 participants