Page 1 of 1

MTDBG, General protection fault

Posted: Mon Aug 17, 2015 3:33 pm
by upcFrost
Добрый день. Сижу корячусь над xmpp-клиентом, и в данный момент наткнулся на следующую проблему - после каждого int 40 (и не только) стабильно ловлю general protection fault. При чем даже в самом глупом случае (код ниже). Первый-второй получается проскочить просто g <следующий адрес> либо bp <адрес> + g. Дальше проскочить уже не выходит, т.к. mtdbg просто подвисает (вернее, не подвисает, но исполнение программы явно прекращается). Началось все наверно после определенного размера бинарника, хз. Кто-нибудь сталкивался?

Eng version:
Hi. I'm now trying to write a simple xmpp-client, and atm i'm stuck at the following problem - after each int 40 (and some other calls) mtdbg raises General protection fault. It happens even in simplest cases (code below). On first or second one it's possible to jump over it with g <next addr> or bp <addr> + g. But later on, mtdbg just hangs up, or, well, it doesn't hang up, but it stops executing anything at all. It started a while ago, imho after binary got big enough (not sure). Does someone know how to deal with it? Thanks.

Code: Select all

align 16
START:
 stdcall dll.Load, @IMPORT
 cmp eax,-1
 jz .close
 mcall 40,0xE7

Re: MTDBG, General protection fault

Posted: Mon Aug 17, 2015 4:15 pm
by Serge
upcFrost
Слишком мало информации.
Я трассировал в отладчике бинарники в 20Мб+.

Re: MTDBG, General protection fault

Posted: Mon Aug 17, 2015 4:28 pm
by upcFrost
если за кривизну кода ногами бить не будут - я могу залить его куда-нибудь, только хз куда чтоб было удобно (не очень хочу заливать этот ужас на svn). На форум не влезет - там 40кб в 10 файлах.

Re: MTDBG, General protection fault

Posted: Mon Aug 17, 2015 4:46 pm
by Leency
40 кб на форум влезает.

Re: MTDBG, General protection fault

Posted: Mon Aug 17, 2015 6:54 pm
by hidnplayr
Problem with demo code is that you did not initialize heap (mcall 68, 11).
Also, make sure program header is filled in correctly. (Are you using I_END and IM_END labels?)

Re: MTDBG, General protection fault

Posted: Wed Aug 19, 2015 11:30 am
by upcFrost
сорри, комп только на работе. Архив в приложении. Просьба ногами не бить и не плеваться, пишу коряво, и непрерывно что-то переписываю (например сейчас переписываю все со статических буферов на mem.Alloc). Но вся возможная критика принимается.

sorry, i have pc only at work. code attached. i know it's shitty since i'm bad with fasm, and i'm constantly rewriting smth (e.g. atm i'm switching from static buffer to mem.Alloc). But i'm open for any criticism.

Re: MTDBG, General protection fault

Posted: Tue Aug 25, 2015 8:17 pm
by hidnplayr
upcFrost: I think you need to sit down and relax. Everything looks very chaotic.
You seem to have copied code from numerous places without always understanding 100% what it does or how it works.
(Great way to learn, if you have enough self motivation to figure out why it doesn't work ;) )

Anyway, I spent some minutes reading your code and want to give some remarks:
  • You did not initialize the heap, like I told you to. It's really important when using libraries and/or using dynamic allocated memory.
  • You used 2 different macros to load 2 different libraries, better choose one and stick with it.
  • For minimum (uncompressed) binary size, you should always put the uninitialized data at the very end of the file.
  • Use readable constants instead of magic numbers where you can (mcall 40, EVM_REDRAW + ..) some common used numbers and structs are defined in macros.inc
  • '=' and 'equ' in fasm work on different levels, it's better to use '=' when possible.
  • 'shl 16' definitely looks more readable to me then '* 65536' and it even saves some clock cycles of assembling time!
  • Constantly rewriting *everything* is bad practice, make yourself a roadmap of some kind, work in steps. Plan, code, test, piece by piece.
Spoiler:

Code: Select all

--- D:/kolibrios/XMPP2/1.ASM	di mei  5 20:40:10 2015
+++ D:/kolibrios/XMPP/1.ASM	di aug 25 19:08:16 2015
@@ -6,2 +6,13 @@ dd	1, START, I_END, mem, mem, 0, 0
-; Current directory address string pointer
-dd	cur_dir_path
+
+
+; Socket buffer size
+SNDBUFFER		= 4096
+RCVBUFFER		= 4096
+
+; Network consts
+STATUS_DISCONNECTED	= 0
+STATUS_RESOLVING	= 1
+STATUS_CONNECTING	= 2
+STATUS_CONNECTED	= 3
+
+
@@ -13,2 +23,0 @@ include 'box_lib.mac'
-include 'load_lib.mac'
-	@use_library	;use load lib macros
@@ -31,12 +39,0 @@ include 'debug.asm'
-; Socket buffer size
-SNDBUFFER	equ 4096
-RCVBUFFER	equ 4096
-
-; Network consts
-STATUS_DISCONNECTED	= 0
-STATUS_RESOLVING	= 1
-STATUS_CONNECTING	= 2
-STATUS_CONNECTED	= 3
-
-
-
@@ -45 +42 @@ include 'debug.asm'
-;       CODE BLOCK
+;	CODE BLOCK
@@ -49,0 +47,4 @@ START:
+	; Initialize heap
+	mcall	68, 11
+
+	; Load libraries
@@ -51,5 +52,2 @@ START:
-	; universal load library/librarys
-	sys_load_library  library_name, cur_dir_path, library_path, system_path, err_message_found_lib, head_f_l, edit_box_import, err_message_import, head_f_i
-	;if return code =-1 then exit, else nornary work
-	cmp	eax,-1
-	jz	close_progr
+	test	eax, eax
+	jnz	close_progr
@@ -57 +55,3 @@ START:
-	mcall	40,0x27
+	mcall	40, EVM_REDRAW + EVM_KEY + EVM_BUTTON + EVM_MOUSE
+
+login_win_redraw:
@@ -72,12 +72,6 @@ wait_login:
-	; Check login box
-	push	dword login_box
-	call	[edit_box_mouse]
-	; Check password box
-	push	dword pass_box
-	call	[edit_box_mouse]
-	; Check server box
-	push	dword serv_box
-	call	[edit_box_mouse]
-	; Check DEBUG BOX
-	push	dword dbg_box
-	call	[edit_box_mouse]
+
+	invoke	edit_box_mouse, login_box
+	invoke	edit_box_mouse, pass_box
+	invoke	edit_box_mouse, serv_box
+	invoke	edit_box_mouse, dbg_box
+
@@ -85 +79,2 @@ wait_login:
-	call	network_event
+	call	network_event		; ???
+
@@ -119,5 +113,0 @@ login_btn_action:
-; Redraw action
-login_win_redraw:
-	call	login_win_draw
-	jmp	wait_login
-
@@ -126 +116 @@ login_key:
-	; Recieve pressed key into eax
+	; Receive pressed key into eax
@@ -129,11 +119,4 @@ login_key:
-	push	dword login_box
-	call	[edit_box_key]
-	; Add value into passbox if it was editbox key
-	push	dword pass_box
-	call	[edit_box_key]
-	; Add value into servbox if it was editbox key
-	push	dword serv_box
-	call	[edit_box_key]
-	; DEBUG
-	push	dword dbg_box
-	call	[edit_box_key]
+	invoke	edit_box_key, login_box
+	invoke	edit_box_key, pass_box
+	invoke	edit_box_key, serv_box
+	invoke	edit_box_key, dbg_box
@@ -145 +128 @@ login_submit:
-	construct_stream	x_stream,login_text,serv_text
+	construct_stream	x_stream, login_text, serv_text
@@ -147,2 +130,3 @@ login_submit:
-	stdcall 		strcpy,xc_xml_start,snd_buffer
-	createXmlOpen		xc_stream,xc_stream,edi
+	stdcall strcpy, xc_xml_start, snd_buffer
+	createXmlOpen		xc_stream, xc_stream, edi
+
@@ -151,2 +135,2 @@ dns_res:
-	stdcall resolve_dns,x_stream.to
-	test	eax,eax 	; Test for error
+	stdcall resolve_dns, x_stream.to
+	test	eax, eax	; Test for error
@@ -156 +140 @@ dns_res:
-	stdcall convert_IP_dec,ebx  ; If no error - get decimal IP
+	stdcall convert_IP_dec, ebx	; If no error - get decimal IP
@@ -159 +143 @@ dns_res:
-	call open_socket
+	call	open_socket
@@ -161 +145 @@ send_login:
-	stdcall strlen,snd_buffer
+	stdcall strlen, snd_buffer
@@ -189 +173 @@ network_event:
-	call	socket_recieve
+	call	socket_receive
@@ -193 +177 @@ network_event:
-socket_recieve:
+socket_receive:
@@ -196,7 +180,7 @@ network_event:
-;        push    0
-;        push    xc_starttls_ns
-;        push    xc_ns
-;        createXmlOpen   xc_starttls,'',snd_buffer
-;        createXmlClose  xc_starttls,'',eax
-;        stdcall strlen,snd_buffer
-;        mov     [msg_len],ecx
+;	 push	 0
+;	 push	 xc_starttls_ns
+;	 push	 xc_ns
+;	 createXmlOpen	 xc_starttls,'',snd_buffer
+;	 createXmlClose  xc_starttls,'',eax
+;	 stdcall strlen,snd_buffer
+;	 mov	 [msg_len],ecx
@@ -204 +188 @@ network_event:
-;        mcall   send,[socketnum],snd_buffer,[msg_len],0
+;	 mcall	 send,[socketnum],snd_buffer,[msg_len],0
@@ -209,6 +193,6 @@ network_event:
-;        construct_stream        x_stream,login_text,serv_text
-;        pushStreamStruc         x_stream
-;        stdcall                 strcpy,xc_xml_start,snd_buffer
-;        createXmlOpen           xc_stream,xc_stream,edi
-;        stdcall strlen,snd_buffer
-;        mov     [msg_len],ecx
+;	 construct_stream	 x_stream,login_text,serv_text
+;	 pushStreamStruc	 x_stream
+;	 stdcall		 strcpy,xc_xml_start,snd_buffer
+;	 createXmlOpen		 xc_stream,xc_stream,edi
+;	 stdcall strlen,snd_buffer
+;	 mov	 [msg_len],ecx
@@ -216 +200 @@ network_event:
-;        mcall   send,[socketnum],snd_buffer,[msg_len],0
+;	 mcall	 send,[socketnum],snd_buffer,[msg_len],0
@@ -250 +234 @@ close_progr:
-;       DATA BLOCK
+;	DATA BLOCK
@@ -253 +236,0 @@ close_progr:
-x_stream	xmpp_stream
@@ -255,3 +238 @@ close_progr:
-; Path where additional box_lib library is stored
-system_path	db '/rd/1/lib/box_lib.obj', 0
-library_name	db 'box_lib.obj'
+; Initialised data
@@ -259,39 +239,0 @@ close_progr:
-; Sys colours table
-sys_colours:
-	w_frames		dd ?
-	w_grab			dd ?
-	w_grab_button		dd ?
-	w_grab_button_text	dd ?
-	w_grab_text		dd ?
-	w_work			dd ?
-	w_work_button		dd ?
-	w_work_button_text	dd ?
-	w_work_text		dd ?
-	w_work_graph		dd ?
-
-; Function import pointer
-edit_box_import:
-	edit_box_draw		dd aEdit_box_draw
-	edit_box_key		dd aEdit_box_key
-	edit_box_mouse		dd aEdit_box_mouse
-	version_ed		dd aVersion_ed
-				dd 0
-				dd 0
-	aEdit_box_draw		db 'edit_box',0
-	aEdit_box_key		db 'edit_box_key',0
-	aEdit_box_mouse 	db 'edit_box_mouse',0
-	aVersion_ed		db 'version_ed',0
-
-; Current dir path string buffer
-cur_dir_path	rb 4096
-; Library dir path string buffer
-library_path	rb 4096
-
-; Common error messages if library can't be imported or loaded
-err_message_found_lib	db 'Sorry I cannot load library box_lib.obj',0
-head_f_i:
-head_f_l		db 'System error',0
-err_message_import	db 'Error on load import library box_lib.obj',0
-
-
-
@@ -302,5 +243,0 @@ test_auth	db '\0test\0test',0
-
-
-;<<<<<<<<<<<<<<<<<<<<<<<<
-; NETWORK
-;<<<<<<<<<<<<<<<<<<<<<<<<
@@ -313,3 +250,2 @@ sockaddr1:
-ip_ptr		dd ?
-socketnum	dd ?
-status	    dd STATUS_DISCONNECTED
+status		dd STATUS_DISCONNECTED
+msg_len 	dd 0
@@ -317 +252,0 @@ sockaddr1:
-
@@ -321 +256,3 @@ align 4
-library network, 'network.obj'
+library network, 'network.obj',\
+	box_lib, 'box_lib.obj'
+
@@ -325 +262 @@ import	network, \
-	inet_ntoa,	 'inet_ntoa'
+	inet_ntoa,	'inet_ntoa'
@@ -326,0 +264,4 @@ import	network, \
+import	box_lib, \
+	edit_box_draw,	'edit_box',\
+	edit_box_mouse, 'edit_box_mouse',\
+	edit_box_key,	'edit_box_key'
@@ -327,0 +269 @@ import	network, \
+
@@ -330,2 +272,12 @@ I_END:
-; Recieve buffer
-msg_len 	dd 0
+
+; Uninitialised data:
+
+ip_ptr		dd ?
+socketnum	dd ?
+
+x_stream	xmpp_stream
+
+; Sys colours table
+sys_colours	system_colors
+
+; Receive buffer

Re: MTDBG, General protection fault

Posted: Tue Aug 25, 2015 11:24 pm
by hidnplayr
Btw, if you want/need full XML support, check out this library: viewtopic.php?f=24&t=1701

Re: MTDBG, General protection fault

Posted: Wed Aug 26, 2015 12:12 pm
by upcFrost
Thanks for comments. Well, as you said, i really have a huge amount of copy-paste code in many parts of this mess. Since it was a total chaos, i've started to clean it up and rewrite it, and the version i shared was in fact somewhere in the middle of rewriting. Now it looks a bit cleaner (at least now i understand how it works except few pieces which i just want to move into a standalone lib), i'll share new one as soon as i'll fix all the stuff you pointed out.

About roadmap... Well, that's quite a sad thing that i don't know much about kolibri. E.g. i'd like to stick to multiwindow and multiprocess version, but it makes debugging quite hard (still worth it, imo). I'm just not that sure what will be the best way to do smth to create a good roadmap.

And about XML - the problem is that XMPP is a deprecated draft by default. I thought about using this lib, but it relies heavily on schemas, and to make one in such a simple way that lib asks for is pretty much impossible for XMPP. I think it'll be easier to use scan to make a table out of XML and then to analyze this table. Originally i intended to use RawXmlLib, but it turned out to be so buggy that it was just easier to write some pieces by myself

Anyway, thanks for looking at my mess.

upd.: reading through the diff you posted. Wow, it looks like i uploaded smth very old O_o need to be more careful. Sorry, it was the code from 4 month ago :(

Re: MTDBG, General protection fault

Posted: Wed Aug 26, 2015 2:20 pm
by upcFrost
Ok, now i double-checked that i'm uploading the latest snapshot. Didn't know that while cloning vbox vdi i should clone snapshot, not the disk itself.

The problem got stuck is somewhere inside msgbox_start procedure (xmpp_msgbox.asm). Since debugging multiprocess app is not that easy with mtdbg especially if app crashes at start, i'm calling it from open_chat proc (xmpp_main.asm) right in the same process just for debugging purposes. Still, can't get rid of General Protection Errors :( I've also tried to cleanup and improve code a bit.

Hope if someone will have few minutes to look through my mess :oops:

Re: MTDBG, General protection fault

Posted: Wed Aug 26, 2015 2:28 pm
by hidnplayr
The program presents me with a GUI of some textboxes and one button, but none of them invoke any reaction.
How to test?

Re: MTDBG, General protection fault

Posted: Wed Aug 26, 2015 2:43 pm
by upcFrost
oh, right... i'm running ejabberd in the second vm. I'll try to make it work with some public xmpp server so it'd be possible to test it without local server instance. Or maybe i'll just make my own public one for testing only

Re: MTDBG, General protection fault

Posted: Wed Aug 26, 2015 6:00 pm
by hidnplayr
Ah, so it's actually trying to log in, you should add a user message for that ;)

So, I've set up Prosody on my windows 2008 server, but trying to log in to it from KolibriOS seems to result in authentication error (from what I see in Wireshark..) Here Is the connection dump:
<?xml version='1.0'?><stream:stream xmlns:stream='http://etherx.jabber.org/streams' xmlns='jabber:client' xml:lang='en' version='1.0' to='test.com' from='test1@test.com'>

<?xml version='1.0'?><stream:stream xmlns:stream='http://etherx.jabber.org/streams' version='1.0' from='test.com' id='db8e0b08-261f-453a-8875-03a78168de24' xml:lang='en' xmlns='jabber:client'><stream:features><mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'><mechanism>SCRAM-SHA-1</mechanism><mechanism>DIGEST-MD5</mechanism></mechanisms><register xmlns='http://jabber.org/features/iq-register' ... m:features>

<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN'>AHRlc3QxAHRlc3Q=</auth>

<failure xmlns='urn:ietf:params:xml:ns:xmpp-sasl'><encryption-required/></failure>
Is MD5 authentication not working yet ?

PS: There is a KolibriOS library which supports MD5, SHA1 etc viewtopic.php?f=24&t=2189
PPS: I tried installing ejabberd on two windows machines but all latest installers fail...

Re: MTDBG, General protection fault

Posted: Thu Aug 27, 2015 9:03 am
by upcFrost
Yep, my bad, some kind of message is really needed. About MD5... MD5 calc works, but i haven't finished MD5 auth yet. Atm it only supports plain auth without ssl, which is simple but rarely supported :( I'll try to make MD5 auth also.
About the lib you pointed out - i think i'll try it out. It'd be also great if that lib'll support Base64 encoding.

Re: MTDBG, General protection fault

Posted: Thu Aug 27, 2015 8:22 pm
by hidnplayr
For base64, there is a library called netcode.obj, but it's not written in FASM, boo!
I am tempted to add base64 code/decode routines in network.obj