Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

basic MCode c++ array question



  • Please log in to reply
4 replies to this topic
guest3456
  • Members
  • 1704 posts
  • Last active: Nov 19 2015 11:58 AM
  • Joined: 10 Mar 2011
i'm using the new MCode generator but the same result happens with the old MCode func too

first, the simple C code
void fill_array(int array[]) {
   array[0] = 3;
   array[1] = 4;
   array[2] = 5;
}
paste that into the generator, choose C code,x86, and HEX, and you can verify my hex output below.

here is the AHK_L code:
   fill_array := MCode("1,x86:8b442404c700030000c740040400c740080500c3")
   
   VarSetCapacity(myarray, 100000, 0)
   DllCall(fill_array, "UInt", &myarray, "cdecl")

   MsgBox, % "myarray[0]=" NumGet(myarray, 0*4, "uint")    ;// should be 3
   MsgBox, % "myarray[1]=" NumGet(myarray, 1*4, "uint")    ;// should be 4
   MsgBox, % "myarray[2]=" NumGet(myarray, 2*4, "uint")    ;// should be 5
   MsgBox, % "myarray[3]=" NumGet(myarray, 3*4, "uint")    ;// should be 0 from VarSetCapacity FillByte

return



MCode(mcode)
{
  static e := {1:4, 2:1}, c := (A_PtrSize=8) ? "x64" : "x86"
  if (!regexmatch(mcode, "^([0-9]+),(" c ":|.*?," c ":)([^,]+)", m))
    return
  if (!DllCall("crypt32\CryptStringToBinary", "str", m3, "uint", 0, "uint", e[m1], "ptr", 0, "uint*", s, "ptr", 0, "ptr", 0))
    return
  p := DllCall("GlobalAlloc", "uint", 0, "ptr", s, "ptr")
  if (c="x64")
    DllCall("VirtualProtect", "ptr", p, "ptr", s, "uint", 0x40, "uint*", op)
  if (DllCall("crypt32\CryptStringToBinary", "str", m3, "uint", 0, "uint", e[m1], "ptr", p, "uint*", s, "ptr", 0, "ptr", 0))
    return p
  DllCall("GlobalFree", "ptr", p)
}
what am i doing wrong ? the msgboxes outputs a random large integer for array[0] and 0's for the others.

guest3456
  • Members
  • 1704 posts
  • Last active: Nov 19 2015 11:58 AM
  • Joined: 10 Mar 2011
uberi disassembled the C code and got this:
http://www.pasteall.org/40017

he says that maybe the compiler optimization broke the code.. unsure.png
 
 
also, if i check ErrorLevel after the DllCall, i'm getting the 0xC0000005 access violation


pretty sure the problem is with the way that the new online generator is optimizing code. when i use /Ox with the old generator, it seems to work

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
✓  Best Answer

I use the following switches for cl.exe:

/TC code.c /c /FAc /Fa"code.cod" /O1 /W 0 /nologo /GS-

The output of my script with your C code is:

code.c
_fill_array PROC                    ; COMDAT
  00000    8b 44 24 04     mov     eax, DWORD PTR _array$[esp-4]
  00004    c7 00 03 00 00
    00         mov     DWORD PTR [eax], 3
  0000a    c7 40 04 04 00
    00 00         mov     DWORD PTR [eax+4], 4
  00011    c7 40 08 05 00
    00 00         mov     DWORD PTR [eax+8], 5
  00018    c3         ret     0
_fill_array ENDP
+ CODE : 8B442404C70003000000C7400404000000C7400805000000C3

Compare that to your code:

8B442404C70003000000C7400404000000C7400805000000C3
8b442404c700030000  c740040400    c740080500    c3

There are missing zero bytes.

 

Edit: Notice that the zeroes which wrap onto a new line in my code listing above are the ones omitted from your hex code. I suppose that the online generator is parsing the code listing (code.cod) incorrectly.



guest3456
  • Members
  • 1704 posts
  • Last active: Nov 19 2015 11:58 AM
  • Joined: 10 Mar 2011
interesting Lex, thanks. i will report it to the generator thread

that hexcode that you have is the same that i got from using the /Ox with the offline MCodeGen. i also tested with /O1 just now and i got the same asm code too, so i think you're right about the parsing problem

Bentschi
  • Moderators
  • 120 posts
  • Last active: Sep 05 2014 02:12 AM
  • Joined: 26 Nov 2008

Hi,

 

I've found the file in the tmp-folder: http://bentschi.no-i...4e15478_x86.cod

This is the content of the file:
; Listing generated by Microsoft ® Optimizing Compiler Version 17.00.50727.1

    TITLE    C:\xampp\htdocs\mcode\tmp\e0abd37253e352df7e82e37144e15478.c
    .686P
    .XMM
    include listing.inc
    .model    flat

INCLUDELIB LIBCMT
INCLUDELIB OLDNAMES

PUBLIC    _fill_array
; Function compile flags: /Ogtpy
_TEXT    SEGMENT
_array$ = 8                        ; size = 4
_fill_array PROC
; File c:\xampp\htdocs\mcode\tmp\e0abd37253e352df7e82e37144e15478.c
; Line 2
  00000    8b 44 24 04     mov     eax, DWORD PTR _array$[esp-4]
  00004    c7 00 03 00 00
    00         mov     DWORD PTR [eax], 3
; Line 3
  0000a    c7 40 04 04 00
    00 00         mov     DWORD PTR [eax+4], 4
; Line 4
  00011    c7 40 08 05 00
    00 00         mov     DWORD PTR [eax+8], 5
; Line 5
  00018    c3         ret     0
_fill_array ENDP
_TEXT    ENDS
END

You are right (the red code is truncated).

I'm gonna fix this.

 

Edit:

Sorry, can't color the code

 

Edit2:

Now it works