Thread: Can you tell me why this causes an infinite loop?

  1. #1
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587

    Can you tell me why this causes an infinite loop?

    Yes, this is a test.

    This only works on 64bit, replace 8 with 4 for 32bit.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
        void *old_sp, *p = main;
        printf("int main()\n");
        asm("mov %0, rsp#" : "m="(old_sp));
        old_sp -= 8;
        memcpy(old_sp, &p, sizeof(p));
        printf("after memcpy");
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    What are you trying to do?

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Because the return address is overwritten by the address of main, causing the return to go back to the main function. It should, however, crash with a stack underflow.

    Assuming I read it correctly, I have to say I only glanced at it ;-)

  4. #4
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    I forgot to mention the rsp register, for 32 bit, it needs to be esp.

    > It should, however, crash with a stack underflow.
    It does seg fault, but from an overflow. cdecl removes args after the return, but that part is never reached because it returns to main.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    And what do you expect sizeof(p) to be?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    The size of a pointer, on my box, 8. I printed it to test it, it is 8.

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by User Name: View Post
    The size of a void pointer, on my box, 8. I printed it to test it, it is 8.
    ....

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Are you sure it even compiles? I can't try, I'm running Linux, but this seems wrong:
    Code:
    old_sp -= 8;
    I mean... Pointer arithmetics on a void pointer doesn't make much sense, does it?

  9. #9
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    A pointer is a pointer, whether it's to an int or void. The fundamental reason you can't do arithmetic on the void itself is because there's no block size(block size must be used in asm, because it must be encoded by the assembler, so the processor will know how arithmetic should function eg whether 255 + 1 should carry or overflow), the block size of any pointer is 64bits(again, on my box).

  10. #10
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    Run it to see. Here's my super-advanced utra-awesomo test:
    Code:
    #include <stdio.h>
    
    int main()
    {
       printf("%i\n", sizeof(void*));
       return 0;
    }
    It printed 8 for me.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by User Name: View Post
    Run it to see. Here's my super-advanced utra-awesomo test:
    Code:
    #include <stdio.h>
    
    int main()
    {
       printf("%i\n", sizeof(void*));
       return 0;
    }
    It printed 8 for me.
    It may very well print 8. But:
    Code:
    int *p;
    p -= 8;
    If p is at memory address 1008, where is it after the second line there? Figure that out, and you'll figure out why they're saying you're having a problem with what you're doing to the void pointer.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    Still at 1008. Pointer arithmetic changes what memory location is pointed to, not where the variable is.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by User Name: View Post
    Still at 1008. Pointer arithmetic changes what memory location is pointed to, not where the variable is.
    Yeah, sorry, that's what I meant. I should have been clearer. What I mean is, that if it holds 1008, and you do a -= 8 on it, it's not now holding 1000.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    And you would move p back 8 by-- what now?

    Do you get it yet?

  15. #15
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    What makes you think it being a pointer has anything to do with 1008 - 8 == 1000? I move what it's pointing to so the return location is written to the right place on the stack. If you compile it and run it, it works. There's no other way to show you but for you to run it. It works.

    Same as(In unsure as to why I didn't do it this way to begin with.):
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
        void *old_sp, *p = main;
        printf("int main()\n");
        asm("mov %0, rsp#" : "m="(old_sp));
        memcpy(old_sp - 8, &p, sizeof(p));
        printf("after memcpy");
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why does this code produce infinite loop?
    By matchkop in forum C Programming
    Replies: 15
    Last Post: 05-25-2010, 09:52 AM
  2. Using while produces an infinite loop...
    By UCF43 in forum C Programming
    Replies: 4
    Last Post: 04-01-2010, 04:47 PM
  3. Replies: 2
    Last Post: 06-14-2009, 11:24 PM
  4. Cosine fucntion and infinite loop.
    By youareafever in forum C Programming
    Replies: 2
    Last Post: 11-07-2008, 04:45 AM
  5. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 12:46 AM