News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Calling WSAStartup leads to ACCESS_VIOLATION

Started by phyisio, July 04, 2023, 10:00:55 PM

Previous topic - Next topic

mineiro

Hello sir Mikhail Tal;
Windows board game have some rules to be followed.

yes, rsp register was not pointing to return address stored in stack.
RSP : 000000A06B3FFA78


;ml64 /c ex1.asm
;link /subsystem:console /entry:stack1 ex1.obj

;in windows/linux x86_64, structures should be aligned to multiple of 8, obligatory
some_windows_structure struct 8
  foo dq ?
  foo1 dd ?
  foo2 dw ?
some_windows_structure ends


.data

.code
;In windows/linux x86_64, if our program will call a function, stack should be aligned to a multiple of 16
stack1 proc   ;Our program starts here, rsp == ???????????????8h this means that stack is not aligned
  sub rsp,8     ;rsp == ???????????????0h      ;now stack is aligned to a multiple of 16 (windows rules)
 
  ;stack aligned, we can do any call now
  ;remember that call subtract from rsp 8 bytes, so at entry of this function/procedure being called stack will not be aligned.
  ;remember that ret add 8 bytes to rsp
  call stack2
 
  add rsp,8     ;rsp == ???????????????8h
ret           ;return to caller
stack1 endp

mystack2local struct 16     ;we can create a structure aligned to 16 to be used as local variables
one dd ?    ;dword = 4 bytes
two dq ?    ;qword = 8 bytes
mystack2local ends          ;this means that size of structure will not be (4+8=12), instead, will be 16 (8+8) in this case

stack2 proc   ;rsp == ???????????????8h
  sub rsp,8               ;rsp == ???????????????0h
    ;remember that local variables act like a push, in better words, subtract from stack (rsp).
    sub rsp,sizeof mystack2local   ;rsp continues aligned to 16, because mystack2local was aligned to 16
    ;stack aligned, we can do any call now
    mov [rsp].mystack2local.one,1
    mov [rsp].mystack2local.two,2
   
    mov ecx,[rsp].mystack2local.one     ;try "mov rcx,..." and you will receive an error :)
    mov rdx,[rsp].mystack2local.two
    call stack3
   
    add rsp,sizeof mystack2local
  add rsp,8
ret
stack2 endp

stack3 proc               ;rsp == ???????????????8h
  sub rsp,8               ;rsp == ???????????????0h
    ;do pushes in pairs after stack was aligned, so stack continues aligned to a multiple of 16
    ;remember that each push qword subtract 8 from rsp
    ;remember that each pop qword add 8 to rsp
    push rax     
    push rax
    ;stack aligned
    ;we can do any call from here
    pop rax       ;do pops in pairs
    pop rax
  add rsp,8
ret
stack3 endp

END

I'd rather be this ambulant metamorphosis than to have that old opinion about everything

phyisio

Okay I  will try this tommorow, but i see why it did nt work in the first place. Does the  masm64 sdk do it automatically for you?




"You must take your opponent into a deep dark forest where 2+2=5, and the path leading out is only wide enough for one." - Mikhail Tal

jj2007


mineiro

yes, sir Hutch have coded a lot of macros to deal with that.

As you can see, there's a lot of opening books (prologue) to reach your goal (epilogue) in each game (procedure).
Choose yours. The example posted above give you one more register to play freely (rbp). Well, that code don't looks good to eyes, but works like a charm.
At learning stage it's better follow default openings done by others, until you feel confortably to create yours.
I'd rather be this ambulant metamorphosis than to have that old opinion about everything

jj2007

Attached a Masm64 SDK version of your code. This is what you should use if you want to share code with members.

Strangely enough, the SOCKADDR_IN and WSADATA structures are not present in \Masm64\include64, so I had to define them.

jj2007

Same but as JBasic:

include \Masm32\MasmBasic\Res\JBasic.inc ; ## builds in 32- or 64-bit mode with ML or AsmC ##
usedeb=1 ; 1=use the deb macro
.data
wsadata WSADATA <>
socka sockaddr_in <>
; --- equates/constants: ---
ip= 2E01A8C0h ; 192.168.1.46 little enddian
port= 5C11h ; port 4444 little endian also
.code
Init ; OPT_64 1 ; put 0 for 32 bit, 1 for 64 bit assembly
  Cls 3
  PrintLine Chr$("This program was assembled with ", @AsmUsed$(1), " in ", jbit$, "-bit format.")
  jinvoke WSAStartup, 514, addr wsadata
  mov socka.sin_port, port
  mov socka.sin_addr, ip
  mov socka.sin_family, AF_INET
  deb 4, "Results", wsadata.wVersion, x:socka.sin_port, $wsadata.lpVendorInfo
EndOfCode


Output 64 bit code:
This program was assembled with AsmC in 64-bit format.
Results
wsadata.wVersion        514
x:socka.sin_port        5c11h
$wsadata.lpVendorInfo   (null)


Output 32 bit code:
This program was assembled with ML in 32-bit format.
Results
wsadata.wVersion        514
x:socka.sin_port        5c11h
$wsadata.lpVendorInfo   (null)

C3

Quote from: jj2007 on July 06, 2023, 09:14:19 PM
Attached a Masm64 SDK version of your code. This is what you should use if you want to share code with members.

Strangely enough, the SOCKADDR_IN and WSADATA structures are not present in \Masm64\include64, so I had to define them.

This is very frustrating. I am too making include files for use of IP networks with MASM64. When all pieces work I post the work. Unless someone is faster than me.

phyisio

Hello guys, i was not able to have any assembly fun as i had too much stupid javascript work yesterday . I finally got my hands on the sdk and only using the STACKFRAME macro solved my problem! Thank you to all  who pitched in and taught me about the stack.

I have to say the sdk is very impressive, much have been a lot of work. I dont plan on using extensively, but it definitely makes the development process easier once you have set it up right.