Skip to content

Commit 4c182df

Browse files
[libc] Fix suseconds_t definition and utimes_test (#134326)
The main issue was that the kernel expected `suseconds_t` to be 64 bits but ours was 32. This caused inconsistent failures since all valid `suseconds_t` values are less than 1000000 (1 million), and some configurations caused `struct timeval` to be padded to 128 bits. Also: forgot to use TEST_FILE instead of FILE_PATH in some places.
1 parent 03604a7 commit 4c182df

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

libc/include/llvm-libc-types/suseconds_t.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
#ifndef LLVM_LIBC_TYPES_SUSECONDS_T_H
1010
#define LLVM_LIBC_TYPES_SUSECONDS_T_H
1111

12-
typedef __INT32_TYPE__ suseconds_t;
12+
// Per posix: suseconds_t shall be a signed integer type capable of storing
13+
// values at least in the range [-1, 1000000]. [...] the widths of [other
14+
// types...] and suseconds_t are no greater than the width of type long.
15+
16+
// The kernel expects 64 bit suseconds_t at least on x86_64.
17+
typedef long suseconds_t;
1318

1419
#endif // LLVM_LIBC_TYPES_SUSECONDS_T_H

libc/test/src/sys/time/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ add_libc_unittest(
88
utimes_test.cpp
99
DEPENDS
1010
libc.hdr.fcntl_macros
11+
libc.hdr.sys_stat_macros
1112
libc.src.errno.errno
1213
libc.src.fcntl.open
1314
libc.src.sys.time.utimes
@@ -16,4 +17,5 @@ add_libc_unittest(
1617
libc.src.unistd.write
1718
libc.src.stdio.remove
1819
libc.src.sys.stat.stat
20+
libc.test.UnitTest.ErrnoCheckingTest
1921
)

libc/test/src/sys/time/utimes_test.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,30 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "hdr/fcntl_macros.h"
10+
#include "hdr/sys_stat_macros.h"
1011
#include "hdr/types/struct_timeval.h"
1112
#include "src/errno/libc_errno.h"
1213
#include "src/fcntl/open.h"
1314
#include "src/stdio/remove.h"
1415
#include "src/sys/stat/stat.h"
1516
#include "src/sys/time/utimes.h"
1617
#include "src/unistd/close.h"
18+
19+
#include "test/UnitTest/ErrnoCheckingTest.h"
1720
#include "test/UnitTest/ErrnoSetterMatcher.h"
1821
#include "test/UnitTest/Test.h"
1922

23+
using LlvmLibcUtimesTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
24+
2025
// SUCCESS: Takes a file and successfully updates
2126
// its last access and modified times.
22-
TEST(LlvmLibcUtimesTest, ChangeTimesSpecific) {
27+
TEST_F(LlvmLibcUtimesTest, ChangeTimesSpecific) {
2328
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
2429

2530
constexpr const char *FILE_PATH = "utimes_pass.test";
2631
auto TEST_FILE = libc_make_test_file_path(FILE_PATH);
27-
int fd = LIBC_NAMESPACE::open(TEST_FILE, O_WRONLY | O_CREAT);
32+
int fd = LIBC_NAMESPACE::open(TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU);
33+
ASSERT_ERRNO_SUCCESS();
2834
ASSERT_GT(fd, 0);
2935
ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
3036

@@ -36,11 +42,11 @@ TEST(LlvmLibcUtimesTest, ChangeTimesSpecific) {
3642
times[1].tv_usec = 23456;
3743

3844
// ensure utimes succeeds
39-
ASSERT_THAT(LIBC_NAMESPACE::utimes(FILE_PATH, times), Succeeds(0));
45+
ASSERT_THAT(LIBC_NAMESPACE::utimes(TEST_FILE, times), Succeeds(0));
4046

4147
// verify the times values against stat of the TEST_FILE
4248
struct stat statbuf;
43-
ASSERT_EQ(LIBC_NAMESPACE::stat(FILE_PATH, &statbuf), 0);
49+
ASSERT_EQ(LIBC_NAMESPACE::stat(TEST_FILE, &statbuf), 0);
4450

4551
// seconds
4652
ASSERT_EQ(statbuf.st_atim.tv_sec, times[0].tv_sec);
@@ -57,13 +63,13 @@ TEST(LlvmLibcUtimesTest, ChangeTimesSpecific) {
5763

5864
// FAILURE: Invalid values in the timeval struct
5965
// to check that utimes rejects it.
60-
TEST(LlvmLibcUtimesTest, InvalidMicroseconds) {
66+
TEST_F(LlvmLibcUtimesTest, InvalidMicroseconds) {
6167
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
6268
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
6369

6470
constexpr const char *FILE_PATH = "utimes_fail.test";
6571
auto TEST_FILE = libc_make_test_file_path(FILE_PATH);
66-
int fd = LIBC_NAMESPACE::open(TEST_FILE, O_WRONLY | O_CREAT);
72+
int fd = LIBC_NAMESPACE::open(TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU);
6773
ASSERT_GT(fd, 0);
6874
ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
6975

@@ -76,7 +82,7 @@ TEST(LlvmLibcUtimesTest, InvalidMicroseconds) {
7682
times[1].tv_usec = 1000000; // invalid
7783

7884
// ensure utimes fails
79-
ASSERT_THAT(LIBC_NAMESPACE::utimes(FILE_PATH, times), Fails(EINVAL));
85+
ASSERT_THAT(LIBC_NAMESPACE::utimes(TEST_FILE, times), Fails(EINVAL));
8086

8187
// check for failure on
8288
// the other possible bad values
@@ -87,6 +93,6 @@ TEST(LlvmLibcUtimesTest, InvalidMicroseconds) {
8793
times[1].tv_usec = 1000;
8894

8995
// ensure utimes fails once more
90-
ASSERT_THAT(LIBC_NAMESPACE::utimes(FILE_PATH, times), Fails(EINVAL));
96+
ASSERT_THAT(LIBC_NAMESPACE::utimes(TEST_FILE, times), Fails(EINVAL));
9197
ASSERT_THAT(LIBC_NAMESPACE::remove(TEST_FILE), Succeeds(0));
9298
}

0 commit comments

Comments
 (0)