Skip to content

Instantly share code, notes, and snippets.

@Gosha
Created June 25, 2020 21:40
Show Gist options
  • Save Gosha/d8a587d545435a7f00e71c3b9283d948 to your computer and use it in GitHub Desktop.
Save Gosha/d8a587d545435a7f00e71c3b9283d948 to your computer and use it in GitHub Desktop.
repro-segfault-nix-osx-IOKit
#!/bin/bash
set -e
echo ================ build
"/nix/store/71lbsnxpbnqkhndcxg1fcwxvjxmviqlg-clang-7.1.0/bin/clang-7" \
-cc1 \
-triple x86_64-apple-macosx10.12.0 \
-Wdeprecated-objc-isa-usage \
-Werror=deprecated-objc-isa-usage \
-emit-obj \
-disable-free \
-disable-llvm-verifier \
-discard-value-names \
-main-file-name test.c \
-mrelocation-model pic \
-pic-level 2 \
-mthread-model posix \
-mdisable-fp-elim \
-masm-verbose \
-munwind-tables \
-faligned-alloc-unavailable \
-target-cpu penryn \
-dwarf-column-info \
-debug-info-kind=standalone \
-dwarf-version=4 \
-debugger-tuning=lldb \
-target-linker-version 274.2 \
-nostdsysteminc \
-resource-dir /nix/store/h7nk6lxk93y23rlnm7z97ss93y893qs5-clang-wrapper-7.1.0/resource-root \
-idirafter /nix/store/kjzhj2m110xg8by1q1iajsihj2q9lmfs-Libsystem-osx-10.12.6/include \
-isystem /nix/store/4a3mw3kg40zafd54f84h53chb0m5wlsy-libc++-7.1.0/include \
-isystem /nix/store/xq93z05cjp3224ff2swi3jhl8z3m45r3-libc++abi-7.1.0/include \
-isystem /nix/store/ipgnljj7gvwm0pd8mdfhdrcrhisjbxkv-compiler-rt-7.1.0-dev/include \
-iframework /nix/store/adrcq37kv1j0s4dskaxzh6xj52j5qg1y-IOKit-osx-10.11.6/Library/Frameworks \
-iframework /nix/store/m45lr9xhns0g65f79n55xrwsqbrrxjfy-swift-corefoundation/Library/Frameworks \
-isystem /nix/store/4a3mw3kg40zafd54f84h53chb0m5wlsy-libc++-7.1.0/include \
-isystem /nix/store/xq93z05cjp3224ff2swi3jhl8z3m45r3-libc++abi-7.1.0/include \
-isystem /nix/store/ipgnljj7gvwm0pd8mdfhdrcrhisjbxkv-compiler-rt-7.1.0-dev/include \
-iframework /nix/store/adrcq37kv1j0s4dskaxzh6xj52j5qg1y-IOKit-osx-10.11.6/Library/Frameworks \
-iframework /nix/store/m45lr9xhns0g65f79n55xrwsqbrrxjfy-swift-corefoundation/Library/Frameworks \
-D _FORTIFY_SOURCE=2 \
-O2 \
-Wformat \
-Wformat-security \
-Werror=format-security \
-Wall \
-fdebug-compilation-dir /Users/gosha/Documents/git/private-nixpkgs/bashtop \
-ferror-limit 19 \
-fmessage-length 303 \
-fwrapv \
-stack-protector 2 \
-stack-protector-buffer-size 4 \
-fblocks \
-fencode-extended-block-signature \
-fregister-global-dtors-with-atexit \
-fobjc-runtime=macosx-10.12.0 \
-fmax-type-align=16 \
-fdiagnostics-show-option \
-fcolor-diagnostics \
-vectorize-loops \
-vectorize-slp \
-o test.o \
-x c test.c
# export NIX_COREFOUNDATION_RPATH="/nix/store/m45lr9xhns0g65f79n55xrwsqbrrxjfy-swift-corefoundation/Library/Frameworks"
# This makes it segfault! ⤴
export NIX_COREFOUNDATION_RPATH=
echo ================ link
"/nix/store/h7nk6lxk93y23rlnm7z97ss93y893qs5-clang-wrapper-7.1.0/bin/ld" \
-demangle \
-whyload \
-whatsloaded \
-order_file_statistics \
-lto_library /nix/store/71lbsnxpbnqkhndcxg1fcwxvjxmviqlg-clang-7.1.0/lib/libLTO.dylib \
-dynamic \
-arch x86_64 \
-macosx_version_min 10.12.0 \
-o test \
-L/nix/store/4a3mw3kg40zafd54f84h53chb0m5wlsy-libc++-7.1.0/lib \
-L/nix/store/xq93z05cjp3224ff2swi3jhl8z3m45r3-libc++abi-7.1.0/lib \
-L/nix/store/3mp7dzda0aqcfw6p3kkbbd3dq9xi0zyp-compiler-rt-7.1.0/lib \
-L/nix/store/4a3mw3kg40zafd54f84h53chb0m5wlsy-libc++-7.1.0/lib \
-L/nix/store/xq93z05cjp3224ff2swi3jhl8z3m45r3-libc++abi-7.1.0/lib \
-L/nix/store/3mp7dzda0aqcfw6p3kkbbd3dq9xi0zyp-compiler-rt-7.1.0/lib \
-L/nix/store/kjzhj2m110xg8by1q1iajsihj2q9lmfs-Libsystem-osx-10.12.6/lib \
-L/nix/store/yhl75s64pcrjq6d7j4sk572xh07jf9il-clang-7.1.0-lib/lib \
-L/nix/store/4a3mw3kg40zafd54f84h53chb0m5wlsy-libc++-7.1.0/lib \
-macosx_version_min 10.12 \
-sdk_version 10.12 \
-no_uuid test.o \
-framework IOKit \
-framework CoreFoundation \
-lSystem /nix/store/h7nk6lxk93y23rlnm7z97ss93y893qs5-clang-wrapper-7.1.0/resource-root/lib/darwin/libclang_rt.osx.a \
-F/nix/store/adrcq37kv1j0s4dskaxzh6xj52j5qg1y-IOKit-osx-10.11.6/Library/Frameworks \
-F/nix/store/m45lr9xhns0g65f79n55xrwsqbrrxjfy-swift-corefoundation/Library/Frameworks \
-F/nix/store/adrcq37kv1j0s4dskaxzh6xj52j5qg1y-IOKit-osx-10.11.6/Library/Frameworks \
-F/nix/store/m45lr9xhns0g65f79n55xrwsqbrrxjfy-swift-corefoundation/Library/Frameworks
echo ================ dsym
"/nix/store/xwwgqz0v1h7pfvj0a12sw5yqp2418gdv-cctools-binutils-darwin-927.0.2/bin/dsymutil" \
-o test.dSYM test
{ pkgs ? import (fetchTarball {
url =
"https://github.com/NixOS/nixpkgs-channels/archive/2cd2e7267e5b9a960c2997756cb30e86f0958a6b.tar.gz";
sha256 = "0ir3rk776wldyjz6l6y5c5fs8lqk95gsik6w45wxgk6zdpsvhrn5";
}) { }, stdenv ? pkgs.stdenv }:
stdenv.mkDerivation {
name = "test";
buildInputs = [ pkgs.darwin.IOKit ];
}
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/storage/IOBlockStorageDriver.h>
#include <IOKit/storage/IOMedia.h>
#include <IOKit/IOBSD.h>
int main()
{
CFDictionaryRef parent_dict;
CFDictionaryRef props_dict;
CFDictionaryRef stats_dict;
io_registry_entry_t parent;
io_registry_entry_t disk;
io_iterator_t disk_list;
printf("Start\n");
// Get list of disks
if (IOServiceGetMatchingServices(kIOMasterPortDefault,
IOServiceMatching(kIOMediaClass),
&disk_list) != kIOReturnSuccess)
{
printf("unable to get the list of disks.\n");
goto error;
}
// Iterate over disks
while ((disk = IOIteratorNext(disk_list)) != 0)
{
parent_dict = NULL;
props_dict = NULL;
stats_dict = NULL;
if (IORegistryEntryGetParentEntry(disk, kIOServicePlane, &parent) != kIOReturnSuccess)
{
printf("unable to get the disk's parent.\n");
IOObjectRelease(disk);
goto error;
}
if (IOObjectConformsTo(parent, "IOBlockStorageDriver"))
{
printf("Found IOBlockStorageDriver\n");
if (IORegistryEntryCreateCFProperties(
disk,
(CFMutableDictionaryRef *)&parent_dict,
kCFAllocatorDefault,
kNilOptions) != kIOReturnSuccess)
{
printf("unable to get the parent's properties.\n");
IOObjectRelease(disk);
IOObjectRelease(parent);
goto error;
}
if (IORegistryEntryCreateCFProperties(
parent,
(CFMutableDictionaryRef *)&props_dict,
kCFAllocatorDefault,
kNilOptions) != kIOReturnSuccess)
{
printf("unable to get the disk properties\n");
CFRelease(props_dict);
IOObjectRelease(disk);
IOObjectRelease(parent);
goto error;
}
const int kMaxDiskNameSize = 64;
CFStringRef disk_name_ref = (CFStringRef)CFDictionaryGetValue(parent_dict, CFSTR(kIOBSDNameKey));
char disk_name[kMaxDiskNameSize];
CFStringGetCString(disk_name_ref,
disk_name,
kMaxDiskNameSize,
CFStringGetSystemEncoding());
stats_dict = (CFDictionaryRef)CFDictionaryGetValue(props_dict, CFSTR(kIOBlockStorageDriverStatisticsKey));
if (stats_dict == NULL)
{
printf("Unable to get disk stats.");
goto error;
}
CFNumberRef number;
int64_t reads = 0;
// int64_t writes = 0;
// int64_t read_bytes = 0;
// int64_t write_bytes = 0;
// int64_t read_time = 0;
// int64_t write_time = 0;
// Get disk reads/writes
if ((number = (CFNumberRef)CFDictionaryGetValue(
stats_dict,
// "It has an OSNumber value"
CFSTR(kIOBlockStorageDriverStatisticsReadsKey))))
{
printf("kIOBlockStorageDriverStatisticsReadsKey: %s\n", kIOBlockStorageDriverStatisticsReadsKey);
printf(" number: %p\n", number);
CFNumberGetValue(number, kCFNumberSInt64Type, &reads);
printf("Never gets here because of segfault\n");
}
}
}
return 0;
error:
return 1;
}
#!/bin/bash
nix-shell test-clang.nix --run "bash build-native.sh && ./test"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment