64-bit architecture - truncating a character when returning from a function

Environment:

Windows x64 bit with 5 GB of RAM. My binary is a 64-bit built with version compiler - "Microsoft (R) C / C ++ Compiler Optimization Version 14.00.50727.762 for x64"

Environment setup:

Microsoft suggests installing the following registry key to test 64-bit applications, and I installed them in my field. The problem does not occur if I do not install the following registry, because the program is placed at a low address. The same registry key is mentioned in the discussion - As a programmer, what do I need to worry about switching to 64-bit windows?

To force allocations from higher addresses to lower addresses for testing purposes, specify MEM_TOP_DOWN when calling VirtualAlloc or set the following registry value to 0x100000:

HKEY_LOCAL_MACHINE \ System \ CurrentControlSet \ Control \ Session Manager \ Memory Management \ AllocationPreference

Code example:

char *alloc_str()
{
    char *temp;
    temp = (char *) malloc(60);
    /* copy some data to temp */
    return temp;
}

main()
{
    char *str;
    str = (char *)alloc_str();
}

Analysis:

mallocreturns the address 0x000007fffe999b40that is stored in temp, but when the pointer returns to main(), it strgets only the second half - 0xfffffffffe999b40and I can’t access the data in this place.

+5
source share
4 answers

, . malloc, , C. . , , . , str = (char *)alloc_str(); , , C, int. malloc, , , , int. , . , C, (void) not (), ( ). 2 , C ++.

+4

. , " ". . , Windows .

, 64- Windows, , PAE . -, 32- .

, 32- , , 32 . - .

, , , .

+3

, . , stdlib.h C, , malloc, 32 . , , , .

tristopia, . .

C, ,

a.c

call_test1()
{
 char* temp;
 temp = (char *)test1();
}

b.c

include stdlib.h
char* test1()
{
 char *str;
 test = (char *)malloc(60);
 /* copy some data to test*/
 return str;
}

str test1(), 64- ( rx ( ) windbg "r rx" ), temp, 32- ).

-

  • test1() a.c
  • char *

a.c

char * test1();
call_test1()
{
 char* temp;
 temp = test1();
}

b.c

include stdlib.h
char* test1()
{
 char *str;
 test = (char *)malloc(60);
 /* copy some data to test*/
 return str;
}

, .

64

  • % p % lx 64- .
  • Windows, C/++ 64- EXE DLL, AllocationPreference.

To force allocation of allocations from higher addresses to lower addresses for testing purposes, specify the following registry value MEM_TOP_DOWNwhen calling VirtualAllocor set the parameter 0x100000:

HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Memory Management\AllocationPreference
+2
source

I need to ask if you are using the correct compiler settings and if you are linking to the appropriate C runtime library

+1
source

All Articles