Skip to content

Commit 5b40375

Browse files
committed
Auto merge of #1129 - gnzlbg:sgx, r=gnzlbg
Add SGX C types cc @jethrogb
2 parents e85479b + 5c1a6b8 commit 5b40375

File tree

18 files changed

+1063
-223
lines changed

18 files changed

+1063
-223
lines changed

ci/style.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ fn walk(path: &Path, err: &mut Errors) {
7272

7373
"dox.rs" |
7474
"lib.rs" |
75+
"ctypes.rs" |
76+
"libc.rs" |
7577
"macros.rs" => continue,
7678

7779
_ => {}

libc-test/build.rs

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -354,9 +354,10 @@ fn main() {
354354
cfg.type_name(move |ty, is_struct, is_union| {
355355
match ty {
356356
// Just pass all these through, no need for a "struct" prefix
357-
"FILE" | "fd_set" | "Dl_info" | "DIR" | "Elf32_Phdr" | "Elf64_Phdr" | "Elf32_Shdr"
358-
| "Elf64_Shdr" | "Elf32_Sym" | "Elf64_Sym" | "Elf32_Ehdr" | "Elf64_Ehdr"
359-
| "Elf32_Chdr" | "Elf64_Chdr" => ty.to_string(),
357+
"FILE" | "fd_set" | "Dl_info" | "DIR" | "Elf32_Phdr"
358+
| "Elf64_Phdr" | "Elf32_Shdr" | "Elf64_Shdr" | "Elf32_Sym"
359+
| "Elf64_Sym" | "Elf32_Ehdr" | "Elf64_Ehdr" | "Elf32_Chdr"
360+
| "Elf64_Chdr" => ty.to_string(),
360361

361362
// Fixup a few types on windows that don't actually exist.
362363
"time64_t" if windows => "__time64_t".to_string(),
@@ -391,8 +392,12 @@ fn main() {
391392
let target2 = target.clone();
392393
cfg.field_name(move |struct_, field| {
393394
match field {
394-
"st_birthtime" if openbsd && struct_ == "stat" => "__st_birthtime".to_string(),
395-
"st_birthtime_nsec" if openbsd && struct_ == "stat" => "__st_birthtimensec".to_string(),
395+
"st_birthtime" if openbsd && struct_ == "stat" => {
396+
"__st_birthtime".to_string()
397+
}
398+
"st_birthtime_nsec" if openbsd && struct_ == "stat" => {
399+
"__st_birthtimensec".to_string()
400+
}
396401
// Our stat *_nsec fields normally don't actually exist but are part
397402
// of a timeval struct
398403
s if s.ends_with("_nsec") && struct_.starts_with("stat") => {
@@ -459,8 +464,9 @@ fn main() {
459464

460465
// Present on historical versions of iOS but missing in more recent
461466
// SDKs
462-
"bpf_hdr" | "proc_taskinfo" | "proc_taskallinfo" | "proc_bsdinfo"
463-
| "proc_threadinfo" | "sockaddr_inarp" | "sockaddr_ctl" | "arphdr"
467+
"bpf_hdr" | "proc_taskinfo" | "proc_taskallinfo"
468+
| "proc_bsdinfo" | "proc_threadinfo" | "sockaddr_inarp"
469+
| "sockaddr_ctl" | "arphdr"
464470
if ios =>
465471
{
466472
true
@@ -472,7 +478,10 @@ fn main() {
472478

473479
cfg.skip_signededness(move |c| {
474480
match c {
475-
"LARGE_INTEGER" | "mach_timebase_info_data_t" | "float" | "double" => true,
481+
"LARGE_INTEGER"
482+
| "mach_timebase_info_data_t"
483+
| "float"
484+
| "double" => true,
476485
// uuid_t is a struct, not an integer.
477486
"uuid_t" if dragonfly => true,
478487
n if n.starts_with("pthread") => true,
@@ -512,7 +521,12 @@ fn main() {
512521

513522
// Skip constants not defined in MUSL but just passed down to the
514523
// kernel regardless
515-
"RLIMIT_NLIMITS" | "TCP_COOKIE_TRANSACTIONS" | "RLIMIT_RTTIME" | "MSG_COPY" if musl => {
524+
"RLIMIT_NLIMITS"
525+
| "TCP_COOKIE_TRANSACTIONS"
526+
| "RLIMIT_RTTIME"
527+
| "MSG_COPY"
528+
if musl =>
529+
{
516530
true
517531
}
518532
// work around super old mips toolchain
@@ -532,11 +546,16 @@ fn main() {
532546
// These constants were removed in FreeBSD 11 (svn r262489),
533547
// and they've never had any legitimate use outside of the
534548
// base system anyway.
535-
"CTL_MAXID" | "KERN_MAXID" | "HW_MAXID" | "NET_MAXID" | "USER_MAXID" if freebsd => true,
549+
"CTL_MAXID" | "KERN_MAXID" | "HW_MAXID" | "NET_MAXID"
550+
| "USER_MAXID"
551+
if freebsd =>
552+
{
553+
true
554+
}
536555

537556
// These constants were added in FreeBSD 11
538-
"EVFILT_PROCDESC" | "EVFILT_SENDFILE" | "EVFILT_EMPTY" | "PD_CLOEXEC"
539-
| "PD_ALLOWED_AT_FORK"
557+
"EVFILT_PROCDESC" | "EVFILT_SENDFILE" | "EVFILT_EMPTY"
558+
| "PD_CLOEXEC" | "PD_ALLOWED_AT_FORK"
540559
if freebsd =>
541560
{
542561
true
@@ -613,13 +632,19 @@ fn main() {
613632
// These constants are tested in a separate test program generated below because there
614633
// are header conflicts if we try to include the headers that define them here.
615634
"F_CANCELLK" | "F_ADD_SEALS" | "F_GET_SEALS" => true,
616-
"F_SEAL_SEAL" | "F_SEAL_SHRINK" | "F_SEAL_GROW" | "F_SEAL_WRITE" => true,
617-
"QFMT_VFS_OLD" | "QFMT_VFS_V0" | "QFMT_VFS_V1" if mips && linux => true, // Only on MIPS
635+
"F_SEAL_SEAL" | "F_SEAL_SHRINK" | "F_SEAL_GROW"
636+
| "F_SEAL_WRITE" => true,
637+
"QFMT_VFS_OLD" | "QFMT_VFS_V0" | "QFMT_VFS_V1"
638+
if mips && linux =>
639+
{
640+
true
641+
} // Only on MIPS
618642
"BOTHER" => true,
619643

620644
"MFD_CLOEXEC" | "MFD_ALLOW_SEALING" if !mips && musl => true,
621645

622-
"DT_FIFO" | "DT_CHR" | "DT_DIR" | "DT_BLK" | "DT_REG" | "DT_LNK" | "DT_SOCK"
646+
"DT_FIFO" | "DT_CHR" | "DT_DIR" | "DT_BLK" | "DT_REG"
647+
| "DT_LNK" | "DT_SOCK"
623648
if solaris =>
624649
{
625650
true
@@ -628,7 +653,10 @@ fn main() {
628653
"PRIO_MIN" | "PRIO_MAX" if solaris => true,
629654

630655
// These are defined for Solaris 11, but the crate is tested on illumos, where they are currently not defined
631-
"EADI" | "PORT_SOURCE_POSTWAIT" | "PORT_SOURCE_SIGNAL" | "PTHREAD_STACK_MIN" => true,
656+
"EADI"
657+
| "PORT_SOURCE_POSTWAIT"
658+
| "PORT_SOURCE_SIGNAL"
659+
| "PTHREAD_STACK_MIN" => true,
632660

633661
// These change all the time from release to release of linux
634662
// distros, let's just not bother trying to verify them. They
@@ -899,8 +927,13 @@ fn main() {
899927
cfg.header("asm/termbits.h");
900928
cfg.skip_const(move |name| match name {
901929
"F_CANCELLK" | "F_ADD_SEALS" | "F_GET_SEALS" => false,
902-
"F_SEAL_SEAL" | "F_SEAL_SHRINK" | "F_SEAL_GROW" | "F_SEAL_WRITE" => false,
903-
"QFMT_VFS_OLD" | "QFMT_VFS_V0" | "QFMT_VFS_V1" if mips && linux => false,
930+
"F_SEAL_SEAL" | "F_SEAL_SHRINK" | "F_SEAL_GROW"
931+
| "F_SEAL_WRITE" => false,
932+
"QFMT_VFS_OLD" | "QFMT_VFS_V0" | "QFMT_VFS_V1"
933+
if mips && linux =>
934+
{
935+
false
936+
}
904937
"BOTHER" => false,
905938
_ => true,
906939
});

src/cloudabi/mod.rs

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,32 @@
1+
2+
pub type int8_t = i8;
3+
pub type int16_t = i16;
4+
pub type int32_t = i32;
5+
pub type int64_t = i64;
6+
pub type uint8_t = u8;
7+
pub type uint16_t = u16;
8+
pub type uint32_t = u32;
9+
pub type uint64_t = u64;
10+
11+
pub type c_schar = i8;
12+
pub type c_uchar = u8;
13+
pub type c_short = i16;
14+
pub type c_ushort = u16;
15+
pub type c_int = i32;
16+
pub type c_uint = u32;
17+
pub type c_float = f32;
18+
pub type c_double = f64;
19+
pub type c_longlong = i64;
20+
pub type c_ulonglong = u64;
21+
pub type intmax_t = i64;
22+
pub type uintmax_t = u64;
23+
24+
pub type size_t = usize;
25+
pub type ptrdiff_t = isize;
26+
pub type intptr_t = isize;
27+
pub type uintptr_t = usize;
28+
pub type ssize_t = isize;
29+
130
pub type in_addr_t = u32;
231
pub type in_port_t = u16;
332
pub type pthread_key_t = usize;
@@ -56,6 +85,9 @@ s! {
5685
}
5786
}
5887

88+
pub const INT_MIN: c_int = -2147483648;
89+
pub const INT_MAX: c_int = 2147483647;
90+
5991
pub const _SC_NPROCESSORS_ONLN: ::c_int = 52;
6092
pub const _SC_PAGESIZE: ::c_int = 54;
6193

@@ -89,7 +121,109 @@ pub const PTHREAD_STACK_MIN: ::size_t = 1024;
89121
pub const SOCK_DGRAM: ::c_int = 128;
90122
pub const SOCK_STREAM: ::c_int = 130;
91123

124+
pub enum FILE {}
125+
pub enum fpos_t {} // TODO: fill this out with a struct
126+
92127
extern {
128+
pub fn isalnum(c: c_int) -> c_int;
129+
pub fn isalpha(c: c_int) -> c_int;
130+
pub fn iscntrl(c: c_int) -> c_int;
131+
pub fn isdigit(c: c_int) -> c_int;
132+
pub fn isgraph(c: c_int) -> c_int;
133+
pub fn islower(c: c_int) -> c_int;
134+
pub fn isprint(c: c_int) -> c_int;
135+
pub fn ispunct(c: c_int) -> c_int;
136+
pub fn isspace(c: c_int) -> c_int;
137+
pub fn isupper(c: c_int) -> c_int;
138+
pub fn isxdigit(c: c_int) -> c_int;
139+
pub fn tolower(c: c_int) -> c_int;
140+
pub fn toupper(c: c_int) -> c_int;
141+
pub fn fopen(filename: *const c_char, mode: *const c_char) -> *mut FILE;
142+
pub fn freopen(filename: *const c_char, mode: *const c_char,
143+
file: *mut FILE) -> *mut FILE;
144+
pub fn fflush(file: *mut FILE) -> c_int;
145+
pub fn fclose(file: *mut FILE) -> c_int;
146+
pub fn remove(filename: *const c_char) -> c_int;
147+
pub fn rename(oldname: *const c_char, newname: *const c_char) -> c_int;
148+
pub fn tmpfile() -> *mut FILE;
149+
pub fn setvbuf(stream: *mut FILE, buffer: *mut c_char, mode: c_int,
150+
size: size_t) -> c_int;
151+
pub fn setbuf(stream: *mut FILE, buf: *mut c_char);
152+
pub fn getchar() -> c_int;
153+
pub fn putchar(c: c_int) -> c_int;
154+
pub fn fgetc(stream: *mut FILE) -> c_int;
155+
pub fn fgets(buf: *mut c_char, n: c_int, stream: *mut FILE) -> *mut c_char;
156+
pub fn fputc(c: c_int, stream: *mut FILE) -> c_int;
157+
pub fn fputs(s: *const c_char, stream: *mut FILE) -> c_int;
158+
pub fn puts(s: *const c_char) -> c_int;
159+
pub fn ungetc(c: c_int, stream: *mut FILE) -> c_int;
160+
pub fn fread(ptr: *mut c_void, size: size_t, nobj: size_t,
161+
stream: *mut FILE) -> size_t;
162+
pub fn fwrite(ptr: *const c_void, size: size_t, nobj: size_t,
163+
stream: *mut FILE) -> size_t;
164+
pub fn fseek(stream: *mut FILE, offset: c_long, whence: c_int) -> c_int;
165+
pub fn ftell(stream: *mut FILE) -> c_long;
166+
pub fn rewind(stream: *mut FILE);
167+
pub fn fgetpos(stream: *mut FILE, ptr: *mut fpos_t) -> c_int;
168+
pub fn fsetpos(stream: *mut FILE, ptr: *const fpos_t) -> c_int;
169+
pub fn feof(stream: *mut FILE) -> c_int;
170+
pub fn ferror(stream: *mut FILE) -> c_int;
171+
pub fn perror(s: *const c_char);
172+
pub fn atoi(s: *const c_char) -> c_int;
173+
pub fn strtod(s: *const c_char, endp: *mut *mut c_char) -> c_double;
174+
pub fn strtol(s: *const c_char, endp: *mut *mut c_char,
175+
base: c_int) -> c_long;
176+
pub fn strtoul(s: *const c_char, endp: *mut *mut c_char,
177+
base: c_int) -> c_ulong;
178+
pub fn calloc(nobj: size_t, size: size_t) -> *mut c_void;
179+
pub fn malloc(size: size_t) -> *mut c_void;
180+
pub fn realloc(p: *mut c_void, size: size_t) -> *mut c_void;
181+
pub fn free(p: *mut c_void);
182+
pub fn abort() -> !;
183+
pub fn exit(status: c_int) -> !;
184+
pub fn _exit(status: c_int) -> !;
185+
pub fn atexit(cb: extern fn()) -> c_int;
186+
pub fn system(s: *const c_char) -> c_int;
187+
pub fn getenv(s: *const c_char) -> *mut c_char;
188+
189+
pub fn strcpy(dst: *mut c_char, src: *const c_char) -> *mut c_char;
190+
pub fn strncpy(dst: *mut c_char, src: *const c_char,
191+
n: size_t) -> *mut c_char;
192+
pub fn strcat(s: *mut c_char, ct: *const c_char) -> *mut c_char;
193+
pub fn strncat(s: *mut c_char, ct: *const c_char, n: size_t) -> *mut c_char;
194+
pub fn strcmp(cs: *const c_char, ct: *const c_char) -> c_int;
195+
pub fn strncmp(cs: *const c_char, ct: *const c_char, n: size_t) -> c_int;
196+
pub fn strcoll(cs: *const c_char, ct: *const c_char) -> c_int;
197+
pub fn strchr(cs: *const c_char, c: c_int) -> *mut c_char;
198+
pub fn strrchr(cs: *const c_char, c: c_int) -> *mut c_char;
199+
pub fn strspn(cs: *const c_char, ct: *const c_char) -> size_t;
200+
pub fn strcspn(cs: *const c_char, ct: *const c_char) -> size_t;
201+
pub fn strdup(cs: *const c_char) -> *mut c_char;
202+
pub fn strpbrk(cs: *const c_char, ct: *const c_char) -> *mut c_char;
203+
pub fn strstr(cs: *const c_char, ct: *const c_char) -> *mut c_char;
204+
pub fn strlen(cs: *const c_char) -> size_t;
205+
pub fn strnlen(cs: *const c_char, maxlen: size_t) -> size_t;
206+
pub fn strerror(n: c_int) -> *mut c_char;
207+
pub fn strtok(s: *mut c_char, t: *const c_char) -> *mut c_char;
208+
pub fn strxfrm(s: *mut c_char, ct: *const c_char, n: size_t) -> size_t;
209+
pub fn wcslen(buf: *const wchar_t) -> size_t;
210+
pub fn wcstombs(dest: *mut c_char, src: *const wchar_t,
211+
n: size_t) -> ::size_t;
212+
213+
pub fn memchr(cx: *const c_void, c: c_int, n: size_t) -> *mut c_void;
214+
pub fn memcmp(cx: *const c_void, ct: *const c_void, n: size_t) -> c_int;
215+
pub fn memcpy(dest: *mut c_void, src: *const c_void,
216+
n: size_t) -> *mut c_void;
217+
pub fn memmove(dest: *mut c_void, src: *const c_void,
218+
n: size_t) -> *mut c_void;
219+
pub fn memset(dest: *mut c_void, c: c_int, n: size_t) -> *mut c_void;
220+
221+
pub fn abs(i: c_int) -> c_int;
222+
pub fn atof(s: *const c_char) -> c_double;
223+
pub fn labs(i: c_long) -> c_long;
224+
pub fn rand() -> c_int;
225+
pub fn srand(seed: c_uint);
226+
93227
pub fn arc4random_buf(buf: *const ::c_void, len: ::size_t);
94228
pub fn freeaddrinfo(res: *mut addrinfo);
95229
pub fn gai_strerror(errcode: ::c_int) -> *const ::c_char;
@@ -164,3 +298,21 @@ cfg_if! {
164298
// Unknown target_arch
165299
}
166300
}
301+
302+
cfg_if! {
303+
if #[cfg(core_cvoid)] {
304+
pub use core::ffi::c_void;
305+
} else {
306+
// Use repr(u8) as LLVM expects `void*` to be the same as `i8*` to help
307+
// enable more optimization opportunities around it recognizing things
308+
// like malloc/free.
309+
#[repr(u8)]
310+
pub enum c_void {
311+
// Two dummy variants so the #[repr] attribute can be used.
312+
#[doc(hidden)]
313+
__variant1,
314+
#[doc(hidden)]
315+
__variant2,
316+
}
317+
}
318+
}

0 commit comments

Comments
 (0)