Utils and libraries Revert <<Use getentropy rather than /dev/urandom on macOS>> #15100

pull DesWurstes wants to merge 1 commits into bitcoin:master from DesWurstes:patch-7 changing 2 files +1 −21
  1. DesWurstes commented at 11:39 AM on January 4, 2019: contributor

    This reverts commit ee2d10a (#10301).

    I've checked sys/random.h (/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/sys/random.h) and it didn't have attribute((weak_import)). It was:

    __attribute__((availability(macosx,introduced=10.12))) __attribute__((availability(ios,introduced=10.0))) __attribute__((availability(tvos,introduced=10.0))) __attribute__((availability(watchos,introduced=3.0)))
    int getentropy(void* buffer, size_t size);
    

    for Clang. For GCC:

    int getentropy(void* buffer, size_t size);
    

    The unpreprocessed version (so that you can see that it has no intention to __attribute__((weak_import))):

    __BEGIN_DECLS
    __OSX_AVAILABLE(10.12) __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0)
    int getentropy(void* buffer, size_t size);
    __END_DECLS
    

    To double-check, I decided to compile this on macOS 10.14 with GCC 8.2:

    #include <stdio.h>
    #include <sys/random.h>
    
    int main(void) {
    	unsigned char ent32[10];
    	if (getentropy) puts("Have it!");
    	else puts("Don't have it!");
    	printf("%d\n", getentropy(ent32, 5));
    	return 0;
    }
    

    When I ran it on 10.10, it said:

    Have it!
    dyld: lazy symbol binding failed: Symbol not found: _getentropy
      Referenced from: /Users/me/Downloads/a.out
      Expected in: /usr/lib/libSystem.B.dylib
    
    dyld: Symbol not found: _getentropy
      Referenced from: /Users/me/Downloads/a.out
      Expected in: /usr/lib/libSystem.B.dylib
    
    Trace/BPT trap: 5
    

    My suggestion is, if we're supporting 10.11 or lower, we should avoid taking risks and continue with /dev/urandom.

  2. Revert "Check if sys/random.h is required for getentropy on OSX."
    This reverts commit ee2d10a.
    ee2d10b5d5
  3. DesWurstes renamed this:
    Revert "Check if sys/random.h is required for getentropy on OSX."
    Utils and libraries Revert <<Use getentropy rather than /dev/urandom on macOS>>
    on Jan 4, 2019
  4. fanquake added the label macOS on Jan 4, 2019
  5. fanquake added the label Utils/log/libs on Jan 4, 2019
  6. fanquake commented at 12:38 PM on January 4, 2019: member
  7. jameshilliard commented at 1:05 PM on January 4, 2019: contributor

    What happens if you change this:

    #include <stdio.h>
    #include <sys/random.h>
    
    int main(void) {
      unsigned char ent32[10];
      if (getentropy) puts("Have it!");
      else puts("Don't have it!");
      printf("%d\n", getentropy(ent32, 5));
      return 0;
    }
    

    to:

    #include <stdio.h>
    #include <sys/random.h>
    
    int main(void) {
    	unsigned char ent32[10];
    	if (&getentropy != nullptr) puts("Have it!");
    	else puts("Don't have it!");
    	printf("%d\n", getentropy(ent32, 5));
    	return 0;
    }
    
  8. jameshilliard commented at 1:34 PM on January 4, 2019: contributor

    Possible fix in #15103 if this is verified to be an issue.

  9. DesWurstes commented at 3:29 PM on January 4, 2019: contributor

    That code also failed. This one worked (printed "Don't have it!" and then crashed, as expected):

    #include <stdio.h>
    
    extern int getentropy(void *buffer, size_t size) __attribute__((weak_import));
    
    int main(void) {
    	unsigned char ent32[10];
    	if (&getentropy != NULL) puts("Have it!");
    	else puts("Don't have it!");
    	printf("%d\n", getentropy(ent32, 5));
    	return 0;
    }
    

    But note that the code above does not build on old macOS where there is no getentropy, due to linker error.

  10. jameshilliard commented at 3:39 PM on January 4, 2019: contributor

    Ok, updated my pull request with that as the fix.

  11. DesWurstes closed this on Jan 4, 2019

  12. DrahtBot locked this on Dec 16, 2021

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-13 15:15 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me