Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

macOS arm64 support #183

Open
ghost opened this issue Jan 11, 2022 · 8 comments
Open

macOS arm64 support #183

ghost opened this issue Jan 11, 2022 · 8 comments
Labels
enhancement New feature or request

Comments

@ghost
Copy link

ghost commented Jan 11, 2022

(generic issue to track macOS arm64 support)
It's unclear at the moment what macOS x86 (Intel) does at the moment regarding some OS things like page sizes.

Output of clang -v:

june@MacBook hardened_malloc % clang -v
Apple clang version 13.0.0 (clang-1300.0.29.30)
Target: arm64-apple-darwin21.3.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
@ghost
Copy link
Author

ghost commented Jan 11, 2022

Apple Clang does not support -march=native so users need to set CONFIG_NATIVE to false.

clang: error: the clang compiler does not support '-march=native'

Apple Clang doesn't support -fstack-clash-protection either. Requires editing the Makefile and removing it.

clang: error: argument unused during compilation: '-fstack-clash-protection' [-Werror,-Wunused-command-line-argument]

Apple Clang doesn't support -Wcast-align=strict, but supports -Wcast-align by itself? Also requires removing it.

error: unknown warning option '-Wcast-align=strict'; did you mean '-Wcast-align'? [-Werror,-Wunknown-warning-option]

These allow you to get past compiler argument errors.

@thestinger
Copy link
Member

The safe_flag check is meant to deal with that. You should figure out why that's not working.

The issue with -fstack-clash-protection is that it's not implemented on arm64 yet.

@ghost
Copy link
Author

ghost commented Jan 11, 2022

macOS does not seem to have threads.h anywhere which is used in h_malloc.c. Nothing is provided by Xcode or Xcode Command Line utilities other than a threads.h file for libxml and is not usable here when included.

The only closest alternative is pthread.h.

threads.h by libxml:

/**
 * Summary: interfaces for thread handling
 * Description: set of generic threading related routines
 *              should work with pthreads, Windows native or TLS threads
 *
 * Copy: See Copyright for the status of this software.
 *
 * Author: Daniel Veillard
 */

@thestinger
Copy link
Member

hardened_malloc only uses threads.h for thread_local. You can refer to it as _Thread_local instead if that's missing. It's just annoying.

@ghost
Copy link
Author

ghost commented Jan 12, 2022

sys/prctl.h also does not exist in macOS. The include was removed and seems to not complain about anything, but may need to be looked into in case it breaks on other OS's.

Apple LLVM linker does not support ANY -z arguments. All -z arguments had to be removed from LDFLAGS in the Makefile.

LDFLAGS := $(LDFLAGS) -Wl

The linker is also trying to link to a shared GCC lib.
ld: library not found for -lgcc_s
Had to drop -lgcc_s from the Makefile's LDLIBS (https://github.com/GrapheneOS/hardened_malloc/blob/main/Makefile#L40-L47)

Apple arm64 also uses 16k pages as documented here which is very likely to be too small: https://developer.apple.com/library/archive/documentation/Performance/Conceptual/ManagingMemory/Articles/AboutMemory.html

In OS X and in earlier versions of iOS, the size of a page is 4 kilobytes. In later versions of iOS, A7- and A8-based systems expose 16-kilobyte pages to the 64-bit userspace backed by 4-kilobyte physical pages, while A9 systems expose 16-kilobyte pages backed by 16-kilobyte physical pages

strcat: will need to totally redo the slot counts / slab sizes
strcat: for 16k
strcat: so atm
strcat: 16 byte allocations go into 4k page
strcat: 256 slots
strcat: if pages are 16k then there are 4x as many
strcat: there isn't enough room in the bitmap atm
strcat: 256 is max # of bits
strcat: it would need a larger bitmap and that would be a fair bit less efficient too
strcat: it gets to the point where it borderline dserves a multi-layer bitmap (2 layer)

That issue will be tackled later down the road. Focus is to get the library to compile and the tests to compile. Then refine everything and make sure stuff still works for other OS's.

@ghost ghost changed the title macOS support macOS arm64 support Jan 12, 2022
@ghost
Copy link
Author

ghost commented Jan 12, 2022

Some changes had to be made to simple-memory-corruption as well.

The main development is currently done by @iraizo and is published here: https://github.com/iraizo/hardened_malloc/tree/osx-support

These changes are not final, heavily a WIP, and the changes made were just to compile and produce a library rather than based on accuracy or testing.

SMC tests after their changes: https://gist.github.com/Zanthed/1bbe99243db0e4348938294841bf9e9a

@thestinger thestinger added the enhancement New feature or request label Jan 16, 2022
@jvoisin
Copy link
Contributor

jvoisin commented Feb 7, 2022

#192 is a first step in this direction.

@iraizo
Copy link

iraizo commented Feb 7, 2022

Looks good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants