1 //===-- sanitizer_platform_limits_linux.cc --------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is a part of Sanitizer common code.
11 //
12 // Sizes and layouts of linux kernel data structures.
13 //===----------------------------------------------------------------------===//
14 
15 // This is a separate compilation unit for linux headers that conflict with
16 // userspace headers.
17 // Most "normal" includes go in sanitizer_platform_limits_posix.cc
18 
19 #include "sanitizer_platform.h"
20 #if SANITIZER_LINUX
21 
22 #include "sanitizer_internal_defs.h"
23 #include "sanitizer_platform_limits_posix.h"
24 
25 // For offsetof -> __builtin_offsetof definition.
26 #include <stddef.h>
27 
28 // With old kernels (and even new kernels on powerpc) asm/stat.h uses types that
29 // are not defined anywhere in userspace headers. Fake them. This seems to work
30 // fine with newer headers, too.
31 #include <asm/posix_types.h>
32 #if defined(__x86_64__) ||  defined(__mips__)
33 #include <sys/stat.h>
34 #else
35 #define ino_t __kernel_ino_t
36 #define mode_t __kernel_mode_t
37 #define nlink_t __kernel_nlink_t
38 #define uid_t __kernel_uid_t
39 #define gid_t __kernel_gid_t
40 #define off_t __kernel_off_t
41 // This header seems to contain the definitions of _kernel_ stat* structs.
42 #include <asm/stat.h>
43 #undef ino_t
44 #undef mode_t
45 #undef nlink_t
46 #undef uid_t
47 #undef gid_t
48 #undef off_t
49 #endif
50 
51 #include <linux/aio_abi.h>
52 
53 #if !SANITIZER_ANDROID
54 #include <sys/statfs.h>
55 #include <linux/perf_event.h>
56 #endif
57 
58 namespace __sanitizer {
59 #if !SANITIZER_ANDROID
60   unsigned struct_statfs64_sz = sizeof(struct statfs64);
61 #endif
62 }  // namespace __sanitizer
63 
64 #if !defined(__powerpc64__) && !defined(__x86_64__) && !defined(__aarch64__)\
65                             && !defined(__mips__)
66 COMPILER_CHECK(struct___old_kernel_stat_sz == sizeof(struct __old_kernel_stat));
67 #endif
68 
69 COMPILER_CHECK(struct_kernel_stat_sz == sizeof(struct stat));
70 
71 #if defined(__i386__)
72 COMPILER_CHECK(struct_kernel_stat64_sz == sizeof(struct stat64));
73 #endif
74 
75 CHECK_TYPE_SIZE(io_event);
76 CHECK_SIZE_AND_OFFSET(io_event, data);
77 CHECK_SIZE_AND_OFFSET(io_event, obj);
78 CHECK_SIZE_AND_OFFSET(io_event, res);
79 CHECK_SIZE_AND_OFFSET(io_event, res2);
80 
81 #if !SANITIZER_ANDROID
82 COMPILER_CHECK(sizeof(struct __sanitizer_perf_event_attr) <=
83                sizeof(struct perf_event_attr));
84 CHECK_SIZE_AND_OFFSET(perf_event_attr, type);
85 CHECK_SIZE_AND_OFFSET(perf_event_attr, size);
86 #endif
87 
88 COMPILER_CHECK(iocb_cmd_pread == IOCB_CMD_PREAD);
89 COMPILER_CHECK(iocb_cmd_pwrite == IOCB_CMD_PWRITE);
90 #if !SANITIZER_ANDROID
91 COMPILER_CHECK(iocb_cmd_preadv == IOCB_CMD_PREADV);
92 COMPILER_CHECK(iocb_cmd_pwritev == IOCB_CMD_PWRITEV);
93 #endif
94 
95 CHECK_TYPE_SIZE(iocb);
96 CHECK_SIZE_AND_OFFSET(iocb, aio_data);
97 // Skip aio_key, it's weird.
98 CHECK_SIZE_AND_OFFSET(iocb, aio_lio_opcode);
99 CHECK_SIZE_AND_OFFSET(iocb, aio_reqprio);
100 CHECK_SIZE_AND_OFFSET(iocb, aio_fildes);
101 CHECK_SIZE_AND_OFFSET(iocb, aio_buf);
102 CHECK_SIZE_AND_OFFSET(iocb, aio_nbytes);
103 CHECK_SIZE_AND_OFFSET(iocb, aio_offset);
104 
105 #endif  // SANITIZER_LINUX
106