Is there a sysctl that will show that an x86 process is running on amd64? hw.machine and hw.machine_arch both seem to return i386 in this case.
Last edited:
I am confused how an AMD64 system could display x86 as its machine.
I am confused how an AMD64 system could display x86 as its machine. If it's AMD64, it's a fair bet it's running a 64 bit kernel.
What does calling sysctl(3) give you? Pass it CTL_HW and HW_MACHINE. It should return AMD64 for a 64 bit hardware platform.
In fact, HW_MACHINE_ARCH would probably also return AMD64.
Determining the program's format might just be as easy as using elf(5). It definitely knows what bit width it's compiled for.
It should be enough to check /usr/lib32 or /libexec/ld-elf32.so.1 paths for existence.
See PR 240432; same thing happens with native FreeBSD executables. Most likely this is an oversight.
Seriously? I just tried this and on a 64 bit machine it's answering AMD64. That's FreeBSD 12.1R and 11.3R. What's the environment?Currently the code does call sysctl with CTL_HW and HW_MACHINE, but it returns x86 even though the kernel is amd64. One thing that I haven't looked at is the fact that the wrapper application is amd64 and it does an exec of the x86 process. I'll try to see what happens if I launch the x86 process directly.
I always know the bitness of the executable. It's the bitness of the kernel that I need to determine.
Seriously? I just tried this and on a 64 bit machine it's answering AMD64. That's FreeBSD 12.1R and 11.3R. What's the environment?
#include <stdio.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
#if defined(__x86_64)
printf("running on amd64\n");
#else
printf("running on x86\n");
#endif
char machbuf[32];
int oid[] = { CTL_HW, HW_MACHINE};
size_t len = sizeof(machbuf);
int error = sysctl(oid, 2, machbuf, &len, NULL, 0);
if (error)
{
perror("sysctl failed");
exit(1);
}
printf("sysctl returned %s\n", machbuf);
#if defined(__x86_64)
error = execve("./sysctl32", argv, NULL);
if (error)
{
perror("execve failed\n");
}
#endif
}
clang -o sysctl64 sysctl.c
clang -m32 -o sysctl32 sysctl.c
SysRes res;
struct vg_stat stat_buf;
res = VG_(stat)("/libexec/ld-elf32.so.1", &stat_buf);
if (!sr_isError(res)) {
// file exists, we're running on amd64
VG_(debugLog)(1, "check-os-bitness", "i386 executable on amd64 kernel\n");
return True;
} else {
VG_(debugLog)(1, "check-os-bitness", "i386 executable on i386 kernel\n");
return False;
}