How to detect an Apple Silicon system from an x86_64 process?

Hi,

i'm looking for a way to detect, from an x86_64 executable (ostensibly running in Rosetta), whether the architecture of the current system is arm64.

Unfortunately, it seems even calling out to "/usr/bin/uname -m" via TSTask does not work, as uname seems to return "x86_64" the called from a x86_64 process, even on Apple Silicon (it returns arm64 as expected, when the calling process is arm64 too).

Is this as designed, or a bug? And does anyone have an alternative suggestion to safely detect the system architecture?
Answered by dwarfland in 632610022
https://developer.apple.com/documentation/apple_silicon/about_the_rosetta_translation_environment?language=objc#3616845

Determine Whether Your App Is Running as a Translated Binary


On Apple silicon, a universal binary may run either natively or as a translated binary. The system runs the native version whenever possible, but the user might opt to run the code using Rosetta to support older plug-ins. 

To programmatically determine when a process is running under Rosetta translation, call the sysctlbyname function with the sysctl.proc_translated flag, as shown in the following example. The example function returns the value 0 for a native process, 1 for a translated process, and -1 when an error occurs.

Code Block c
int processIsTranslated() {
int ret = 0;
size_t size = sizeof(ret);
if (sysctlbyname("sysctl.proc_translated", &ret, &size, NULL, 0) == -1)
{
if (errno == ENOENT)
return 0;
return -1;
}
return ret;
}


Accepted Answer
https://developer.apple.com/documentation/apple_silicon/about_the_rosetta_translation_environment?language=objc#3616845

Determine Whether Your App Is Running as a Translated Binary


On Apple silicon, a universal binary may run either natively or as a translated binary. The system runs the native version whenever possible, but the user might opt to run the code using Rosetta to support older plug-ins. 

To programmatically determine when a process is running under Rosetta translation, call the sysctlbyname function with the sysctl.proc_translated flag, as shown in the following example. The example function returns the value 0 for a native process, 1 for a translated process, and -1 when an error occurs.

Code Block c
int processIsTranslated() {
int ret = 0;
size_t size = sizeof(ret);
if (sysctlbyname("sysctl.proc_translated", &ret, &size, NULL, 0) == -1)
{
if (errno == ENOENT)
return 0;
return -1;
}
return ret;
}


Same problem with you. No answer yet.
All solutions not work for an App run in Rosetta.
How to detect an Apple Silicon system from an x86_64 process?
 
 
Q