Skip to content
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

Error in obtaining time on ‘arm-unknown-linux-musleabihf’ platform #115921

Open
lbl8603 opened this issue Sep 18, 2023 · 9 comments
Open

Error in obtaining time on ‘arm-unknown-linux-musleabihf’ platform #115921

lbl8603 opened this issue Sep 18, 2023 · 9 comments
Labels
A-time Area: Time C-bug Category: This is a bug. O-musl Target: The musl libc T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@lbl8603
Copy link

lbl8603 commented Sep 18, 2023

fn main() {
    let now = std::time::SystemTime::now();
    println!("now={:?}",now);
}

image

rustc --version --verbose:

rustc 1.72.0-nightly (f4b80cacf 2023-06-30)
binary: rustc
commit-hash: f4b80cacf93ca216c75f6ae12f4b9dec19eba42f
commit-date: 2023-06-30
host: x86_64-unknown-linux-gnu
release: 1.72.0-nightly
LLVM version: 16.0.5

build

cargo build --target arm-unknown-linux-musleabihf
[target.arm-unknown-linux-musleabihf]
linker = "arm-linux-musleabihf-gcc" # https://musl.cc/arm-linux-musleabihf-cross.tgz

os


uname -a
================================================================
Linux LTBK 3.2.40 #15284 Sat May 19 04:36:51 CST 2018 armv7l GNU/Linux synology_armada370_213j

cat /proc/cpuinfo
================================================================
Processor	: Marvell PJ4Bv7 Processor rev 1 (v7l)
BogoMIPS	: 1196.85
Features	: swp half thumb fastmult vfp edsp vfpv3 vfpv3d16 tls 
CPU implementer	: 0x56
CPU architecture: 7
CPU variant	: 0x1
CPU part	: 0x581
CPU revision	: 1

Hardware	: Marvell Armada-370
Revision	: 0000
Serial		: 0000000000000000
@lbl8603 lbl8603 added the C-bug Category: This is a bug. label Sep 18, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Sep 18, 2023
@saethlin
Copy link
Member

Is there any chance you can run the executable with strace ./arm_time_test?

@lbl8603
Copy link
Author

lbl8603 commented Sep 18, 2023

strace ./arm_time_test
=====================================
execve("./arm_time_test", ["./arm_time_test"], [/* 19 vars */]) = 0
mmap2(NULL, 328, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x4009b000
set_tls(0x4009b070, 0xaf814, 0x37, 0xbebcfe1e, 0x4009b000) = 0
set_tid_address(0xb3904)                = 14999
poll([{fd=0, events=0}, {fd=1, events=0}, {fd=2, events=0}], 3, 0) = 0 (Timeout)
rt_sigaction(SIGPIPE, {SIG_IGN, [], SA_RESTORER|SA_RESTART, 0x89490}, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGSEGV, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigprocmask(SIG_UNBLOCK, [RT_1 RT_2], NULL, 8) = 0
rt_sigaction(SIGSEGV, {0x65f9c, [], SA_RESTORER|SA_STACK|SA_SIGINFO, 0x89498}, NULL, 8) = 0
rt_sigaction(SIGBUS, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGBUS, {0x65f9c, [], SA_RESTORER|SA_STACK|SA_SIGINFO, 0x89498}, NULL, 8) = 0
sigaltstack(NULL, {ss_sp=0, ss_flags=SS_DISABLE, ss_size=0}) = 0
mmap2(NULL, 12288, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_STACK, -1, 0) = 0x4016e000
mprotect(0x4016e000, 4096, PROT_NONE)   = 0
sigaltstack({ss_sp=0x4016f000, ss_flags=0, ss_size=8192}, NULL) = 0
brk(0)                                  = 0x1d8000
brk(0x1da000)                           = 0x1da000
mmap2(0x1d8000, 4096, PROT_NONE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x1d8000
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x40204000
rt_sigprocmask(SIG_BLOCK, ~[RTMIN RT_1 RT_2], [], 8) = 0
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
syscall_403(0, 0xbebcfa78, 0, 0, 0xbebcfaa8, 0xb3134) = -1 (errno 22)
write(2, "thread '", 8thread ')                 = 8
write(2, "main", 4main)                     = 4
write(2, "' panicked at '", 15' panicked at ')         = 15
write(2, "called `Result::unwrap()` on an "..., 109called `Result::unwrap()` on an `Err` value: Os { code: 22, kind: InvalidInput, message: "Invalid argument" }) = 109
write(2, "', ", 3', )                      = 3
write(2, "library/std/src/sys/unix/time.rs", 32library/std/src/sys/unix/time.rs) = 32
write(2, ":", 1:)                        = 1
write(2, "420", 3420)                      = 3
write(2, ":", 1:)                        = 1
write(2, "72", 272)                       = 2
write(2, "\n", 1
)                       = 1
write(2, "note: run with `RUST_BACKTRACE=1"..., 78note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
) = 78
sigaltstack({ss_sp=0, ss_flags=SS_DISABLE, ss_size=8192}, NULL) = 0
munmap(0x4016e000, 12288)               = 0
exit_group(101)                         = ?
+++ exited with 101 +++

@Nilstrieb Nilstrieb added T-libs Relevant to the library team, which will review and decide on the PR/issue. O-musl Target: The musl libc A-time Area: Time and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Sep 18, 2023
@lbl8603
Copy link
Author

lbl8603 commented Sep 18, 2023

cvt(unsafe { libc::clock_gettime(clock, t.as_mut_ptr()) }).unwrap();

'unwrap' should not be used

@Fulgen301
Copy link
Contributor

Fulgen301 commented Sep 18, 2023

unwrap is what's causing the panic, not what's causing clock_gettime to fail with EINVAL, which means that the specified clock is invalid or not supported on that system. SystemTime::now() is using CLOCK_REALTIME, and the man page for that system call says

All implementations support the system-wide real-time clock, which is identified by CLOCK_REALTIME.

@lbl8603
Copy link
Author

lbl8603 commented Sep 18, 2023

This is how I solve this problem.If there were no panic, it wouldn't be so troublesome.This is also a problem with rust robustness

    #[cfg(all(target_arch = "arm", target_os = "linux",target_pointer_width = "32"))]
    {
        let mut t = std::mem::MaybeUninit::uninit();
        let time = unsafe { libc::clock_gettime(libc::CLOCK_REALTIME, t.as_mut_ptr()) };
        if time <= 0 {
            let output = std::process::Command::new("date")
                .arg("+%s%3N")
                .output()
                .expect("'date'  not found");
            if output.status.success() {
                let date_output = String::from_utf8(output.stdout).unwrap();
                let milliseconds = date_output.trim().parse::<u64>().unwrap();
                return milliseconds;
            }
        }
    }

@Fulgen301
Copy link
Contributor

syscall_403

This is the syscall number for clock_gettime64 on ARM, which was added with Linux 5.1 (as opposed to clock_gettime, which is syscall number 263), and since you're running kernel 3.2.40, that syscall doesn't exist (and that old a version of strace doesn't know it either).

Looks like musl switched over to 64bit times a while ago, so you'd have to build musl without the redirection to the 64bit syscalls.

@lbl8603
Copy link
Author

lbl8603 commented Sep 18, 2023

Sorry, I didn't understand what you meant. Can you explain how to do it more specifically

@lbl8603
Copy link
Author

lbl8603 commented Sep 18, 2023

The 'date' command can obtain the time

root@LTBK:/# strace date

execve("/bin/date", ["date"], [/* 18 vars */]) = 0
brk(0)                                  = 0x1c6f000
uname({sys="Linux", node="LTBK", ...})  = 0
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x40004000
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=20860, ...}) = 0
mmap2(NULL, 20860, PROT_READ, MAP_PRIVATE, 3, 0) = 0x4008c000
close(3)                                = 0
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
open("/lib/arm-linux-gnueabi/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0(\0\1\0\0\0\230\204\1\0004\0\0\0"..., 512) = 512
lseek(3, 1272700, SEEK_SET)             = 1272700
read(3, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 2840) = 2840
lseek(3, 1269268, SEEK_SET)             = 1269268
read(3, "A)\0\0\0aeabi\0\1\37\0\0\0\0054T\0\6\2\10\1\t\1\22\4\23\1\24\1"..., 42) = 42
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x40167000
fstat64(3, {st_mode=S_IFREG|0755, st_size=1275540, ...}) = 0
mmap2(NULL, 1344920, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x4025c000
mprotect(0x40390000, 61440, PROT_NONE)  = 0
mmap2(0x4039f000, 12288, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x133000) = 0x4039f000
mmap2(0x403a2000, 9624, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x403a2000
close(3)                                = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x400b7000
set_tls(0x400b74c0, 0x400b7ba8, 0x4011a050, 0x400b74c0, 0x4011a050) = 0
mprotect(0x4039f000, 8192, PROT_READ)   = 0
mprotect(0x2b000, 4096, PROT_READ)      = 0
mprotect(0x40119000, 4096, PROT_READ)   = 0
munmap(0x4008c000, 20860)               = 0
brk(0)                                  = 0x1c6f000
brk(0x1c90000)                          = 0x1c90000
open("/usr/lib/locale/locale-archive", O_RDONLY|O_LARGEFILE|O_CLOEXEC) = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=3324208, ...}) = 0
mmap2(NULL, 2097152, PROT_READ, MAP_PRIVATE, 3, 0) = 0x403a5000
close(3)                                = 0
clock_gettime(CLOCK_REALTIME, {1695031207, 548680087}) = 0
open("/etc/localtime", O_RDONLY|O_CLOEXEC) = 3
fcntl64(3, F_GETFD)                     = 0x1 (flags FD_CLOEXEC)
fstat64(3, {st_mode=S_IFREG|0644, st_size=118, ...}) = 0
fstat64(3, {st_mode=S_IFREG|0644, st_size=118, ...}) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x40090000
read(3, "TZif2\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\1\0\0\0\1\0\0\0\0"..., 4096) = 118
_llseek(3, -6, [112], SEEK_CUR)         = 0
read(3, "\nUTC0\n", 4096)               = 6
close(3)                                = 0
munmap(0x40090000, 4096)                = 0
fstat64(1, {st_mode=S_IFCHR|0600, st_rdev=makedev(136, 5), ...}) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x4010a000
write(1, "Mon Sep 18 10:00:07 UTC 2023\n", 29Mon Sep 18 10:00:07 UTC 2023
) = 29
close(1)                                = 0
munmap(0x4010a000, 4096)                = 0
close(2)                                = 0
exit_group(0)                           = ?
+++ exited with 0 +++

@saethlin
Copy link
Member

Sorry, I didn't understand what you meant. Can you explain how to do it more specifically

I think what you're replying to here is an abstract suggestion, not something anyone actually expects you to try.

Personally I'm not even sure if it's correct to say that musl's 64-bit time change has dropped support for old Linux kernels (you are using an old kernel). Musl documents on https://wiki.musl-libc.org/supported-platforms:

musl is built on the Linux syscall layer. Linux kernel >=2.6.39 is necessary for POSIX conformant behaviour,

So if the 64-bit time switch made that statement wrong, the musl docs need to be changed, and we also need to document that our musl targets don't support Linux kernels before clock_gettime64 (because they still statically link musl by default).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-time Area: Time C-bug Category: This is a bug. O-musl Target: The musl libc T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants