3 // BUILD: $CC a.c -dynamiclib -arch x86_64h -o $BUILD_DIR/libHaswellCheck.dylib -install_name $RUN_DIR/libHaswellCheck.dylib
4 // BUILD: $CC main.c -o $BUILD_DIR/dlopen-haswell.exe -DRUN_DIR="$RUN_DIR"
6 // RUN: ./dlopen-haswell.exe
12 #include <mach/host_info.h>
13 #include <mach/mach.h>
14 #include <mach/mach_host.h>
16 typedef bool (*BoolFunc
)(void);
19 bool isHaswell_dynamic()
21 struct host_basic_info info
;
22 mach_msg_type_number_t count
= HOST_BASIC_INFO_COUNT
;
23 mach_port_t hostPort
= mach_host_self();
24 kern_return_t result
= host_info(hostPort
, HOST_BASIC_INFO
, (host_info_t
)&info
, &count
);
25 mach_port_deallocate(mach_task_self(), hostPort
);
26 if ( result
== KERN_SUCCESS
) {
27 return (info
.cpu_subtype
== CPU_SUBTYPE_X86_64_H
);
33 int main(int arg
, const char* argv
[])
35 printf("[BEGIN] dlopen-haswell\n");
37 void* handle
= dlopen(RUN_DIR
"/libHaswellCheck.dylib", RTLD_LAZY
);
38 if ( handle
== NULL
) {
39 printf("dlerror(): %s\n", dlerror());
40 printf("[FAIL] dlopen-haswell dlopen\n");
44 BoolFunc libFunc
= (BoolFunc
)dlsym(handle
, "isHaswell");
45 if ( libFunc
== NULL
) {
46 printf("dlerror(): %s\n", dlerror());
47 printf("[FAIL] dlopen-haswell dlsym\n");
51 // check if haswell slice of libHaswellCheck.dylib was loaded on haswell machines
52 bool dylibIsHaswellSlice
= (*libFunc
)();
53 bool runtimeIsHaswell
= isHaswell_dynamic();
55 if ( dylibIsHaswellSlice
!= runtimeIsHaswell
)
56 printf("[FAIL] dlopen-haswell, dylibIsHaswellSlice=%d, runtimeIsHaswell=%d\n", dylibIsHaswellSlice
, runtimeIsHaswell
);
58 printf("[PASS] dlopen-haswell\n");