]> granicus.if.org Git - strace/blob - test/x32_lseek.c
test: add mtd/ubi test helpers
[strace] / test / x32_lseek.c
1 // Test program which explores whether lseek syscall (not llseek!)
2 // on x32 uses 64-bit offset argument.
3 // IOW: does _kernel_ truncate it on entry?
4 // The answer appears to be "no, full 64-bit offset is used".
5 // strace must show it correctly too - tricky if strace itself is x32 one!
6 //
7 // Build: x86_64-gcc -static -Wall -ox32_lseek x32_lseek.c
8 // Run:   $ strace ./x32_lseek 2>&1 | grep lseek | grep 1250999896321
9 //        lseek(0, 1250999896321, SEEK_SET) = 1250999896321
10 #define _GNU_SOURCE
11 #include <sys/types.h>
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <errno.h>
15 #include <sys/stat.h>
16 #include <fcntl.h>
17 #include <stdio.h>
18 #include <sys/syscall.h>
19 // Ensure we are compiling to 64 bits
20 struct bug { int t[sizeof(long) > 4 ? 1 : -1]; };
21 int main(int argc, char **argv)
22 {
23         long ofs = 0x12345678901;
24         errno = 0;
25         close(0);
26         if (open("/etc/passwd", O_RDONLY))
27                 return 1;
28         long r = syscall(
29                 (long) (__NR_lseek | 0x40000000), // make x32 call
30                 (long) (0),
31                 (long) (ofs),
32                 (long) (SEEK_SET)
33         );
34         printf("pos:%ld(0x%lx) errno:%m\n", r, r);
35         if (!errno)
36                 printf((r == ofs) ? "64-bit offset used\n" : "Kernel truncated offset\n");
37         return 0;
38 }