0

I am currently doing a computer course where I have to run assembly inside of C++. In my class, everyone is running it in Windows and they have no problem running the code (I was able to do it on Windows too). When I execute the code on my Mac using Xcode, I get XC_BAD_ACCESS (code=EXC_I386_GPFLT) which I think means that macOS is protecting me from running system calls.

When debugging the line that crashes the program, I think it is the call printf line that is giving me the error as the code executes and completes successfully.

Here is the code:

#include <iostream>
#include <stdio.h>

using namespace std;

char format[] = "%s %s\n";
char hello[] = "Hello";
char world[] = "world";

int main()
{

    __asm
    {
        mov  eax, offset world
        push eax
        mov  eax, offset hello
        push eax
        mov  eax, offset format
        push eax
        call printf
        pop  eax
        pop  eax
        pop  eax

    }
}

To get this 32-bit code to work on my 64bit Mac, on Xcode, I went to Build Settings -> Architectures and selected 32-bit Intel in order to compile it.

How would I fix the EXC_I386_GPFLT runtime error on my Mac running 10.13.2?

Note: In Xcode, I created a macOS Command Line Tool.

Edit: I forgot to say that I tried disabling rootless (SIP) and ran Xcode when logged into the root user. That still did not work.

iProgram
  • 6,057
  • 9
  • 39
  • 80
  • 1
    ``__asm``: Visual C++ specific – Asesh Jan 19 '18 at 13:50
  • I wonder how you got this to compile. C Symbols are decorated with leading underscores on OS X, so your function call should be `call _printf` to even link. Same for all the variables. – fuz Jan 19 '18 at 13:50
  • @fuz @asesh when I do `call _printf` I get Use of undeclared label '_printf' and that is with the `__` and without it. – iProgram Jan 19 '18 at 13:53
  • Very interesting. This all doesn't make a lot of sense to me. – fuz Jan 19 '18 at 13:54
  • @fuz, could it be because it is running inside C++ and not just pure assembly? – iProgram Jan 19 '18 at 13:55
  • 3
    The basis of your question is incorrect: MacOS does not prevent you from running assembly code. – Sneftel Jan 19 '18 at 14:03
  • 2
    @fuz "This all doesn't make a lot of sense to me" - that's how I feel every time I see anything apple related – UKMonkey Jan 19 '18 at 14:05
  • 3
    @iProgram The error should not result from using inline assembly, however I do not understand the rules of this dialect of inline assembly so it's hard for me to understand where the problem comes from (stack misalignment seems like a possibility). I advise beginners to learn assembly by programming in actual assembly, not inline assembly as the latter is very confusing unless you are already pretty firm in assembly programming. – fuz Jan 19 '18 at 14:21
  • Your code is very different from the inline assembly shown in Clang's user manual: http://clang.llvm.org/compatibility.html#inline-asm – Cris Luengo Jan 19 '18 at 14:40
  • See also the comment below this answer: https://stackoverflow.com/a/31689418/7328782 – Cris Luengo Jan 19 '18 at 14:43
  • 1
    The error you get indicates that you did an illegal memory access (it's a *general protection fault*). It does not indicate that assembly code is forbidden or something and running it as root won't fix anything (quite on the contrary, running code as root makes the damage larger if something goes wrong). I am not sure what causes this error though. – fuz Jan 19 '18 at 14:53
  • 1
    Stack alignment issue. Read the question above. Same applies to 32-bits macOS. – Macmade Jan 19 '18 at 16:47
  • @Asesh `__asm { }` is valid syntax in Xcode for `x86` and `x86-64` assembly. – Kamil.S Jan 21 '18 at 11:37

0 Answers0