Decoupling an expression removes the crash (?)

Just starting out? Need help? Post your questions and find answers here.
User avatar
mk-soft
Always Here
Always Here
Posts: 6315
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Decoupling an expression removes the crash (?)

Post by mk-soft »

It would be nice if you could put in an executable code with a call to the procedure.
So you have to assemble something again for testing
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
SMaag
Enthusiast
Enthusiast
Posts: 327
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

Re: Decoupling an expression removes the crash (?)

Post by SMaag »

here is the Forum link for ASM and C Output

https://www.purebasic.fr/german/viewtop ... 10&t=30935

What is important to do:
Install Purebasic not in the standard Windows\SysWOW
there you have problems with the rights do to some things and ASM or C Output don't work sometimes.

I use C:\APPx64\Purebasic
Joubarbe
Enthusiast
Enthusiast
Posts: 714
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: Decoupling an expression removes the crash (?)

Post by Joubarbe »

Here is my demo, and it crashes! :D

Code: Select all

EnableExplicit

;- Structures.

Structure _XY
  x.i
  y.i
EndStructure

Structure _Item
  text$
EndStructure

Structure _Control
  type.i
EndStructure

Structure _TableItem Extends _Item
  pos._XY
EndStructure

Structure _Table Extends _Control
  *selected_item._TableItem
  Array items._TableItem(0, 0)
EndStructure

;- Globals and constants.

#_TYPE_TABLE = 0

Global NewList tables._Table()

;- Procedures.

Procedure.i NewTable(cols, rows) : With tables()
  AddElement(tables())
  \type = #_TYPE_TABLE
  Dim \items(cols - 1, rows - 1)
  
  Define x, y
  For x = 0 To ArraySize(\items(), 1)
    For y = 0 To ArraySize(\items(), 2)
      \items(x, y)\text$ = Str(x) + " / " + Str(y)
      \items(x, y)\pos\x = x
      \items(x, y)\pos\y = y
    Next y
  Next x
  
  ProcedureReturn tables()
EndWith : EndProcedure

Procedure.i GetPreviousTableItem(*table._Table) : With *table\selected_item\pos
  If *table\selected_item = #Null : DebuggerError("The table has no selected element.") : EndIf
  
  If \x > 0
    ProcedureReturn *table\items(\x - 1, \y)
  ElseIf \y > 0
    ProcedureReturn *table\items(ArraySize(*table\items(), 1), \y - 1)     ; Crashes here. "Invalid memory access" error.
  EndIf
  
  ProcedureReturn #Null
EndWith : EndProcedure

;- Main.

Define *new_table._Table = NewTable(4, 4)
*new_table\selected_item = *new_table\items(0, 2) ; Does not crash if *new_table\items(1, 2).

Define *previous_item._Item = GetPreviousTableItem(*new_table)
Debug *previous_item\text$
It doesn't crash in C!

PB 6.12 Win64b
SMaag
Enthusiast
Enthusiast
Posts: 327
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

Re: Decoupling an expression removes the crash (?)

Post by SMaag »

i added a TestProcedure to get the Error.
For me it looks like a bug in the ASM Backend. See the ASM Output, I marked the region where I guess to find the error

Code: Select all

Procedure.i Test(*table._Table)
  Protected x=#False, size, ret
  
  size = ArraySize(*table\items(), 1)    ; This returns 0
  Debug size
  
  If x
    ret = *table\items(ArraySize(*table\items(), 1), 0) ; this crashes
    ProcedureReturn ret 
  Else
    ProcedureReturn *table\items(size, 0)        ; this do not crash
  EndIf
  
EndProcedure

Define MyTable._Table 
Define ret


ret = Test(MyTable)
Debug ret
here the ASM Output of Test()

Code: Select all

; Procedure.i Test(*table._Table)
_Procedure2:
  MOV    qword [rsp+8],rcx
  PUSH   rbp
  PUSH   r15
  PS2=96
  XOR    rax,rax
  PUSH   rax
  PUSH   rax
  PUSH   rax
  PUSH   rax
  SUB    rsp,40
; Protected x, size, ret
; 
; size = ArraySize(*table\items(), 1)    
  PUSH   qword 1
  MOV    rbp,qword [rsp+PS2+8]
  PUSH   rbp
  MOV    rdx,[rsp+0]
  POP    rax
  PUSH   qword [rbp+68]
  POP    rcx
  POP    rdx
  CALL   PB_ArraySize2
  MOV    qword [rsp+48],rax  ; size = RAX
; Debug size
; 
; If x
  CMP    qword [rsp+40],0
  JE    _EndIf13
; ret = *table\items(ArraySize(*table\items(), 1), 0)
  MOV    rbp,qword [rsp+PS2+0]
  PUSH   rbp
  SUB    rsp,8				; Maybe this should be ADD rsp, 8! Than this is the error!
  PUSH   qword 1
  MOV    rbp,qword [rsp+PS2+24]
  PUSH   rbp
  MOV    rdx,[rsp+0]
  POP    rax
  PUSH   qword [rbp+68]
  POP    rcx
  POP    rdx
  SUB    rsp,32
  CALL   PB_ArraySize2		; RAX = ArraySize
  
  ; I guess here is anywhere the error
  ; ---------------------------------------
  ADD    rsp,40
  MOV    rdx,[rsp+-24]
  MOV    rdx,qword [rdx+76]
  IMUL   rdx,rax     ; RDX = RDX * ArraySize
  MOV    rbp,rdx
  PUSH   rbp
  XOR    rax,rax
  POP    rbp
  ADD    rbp,rax
  IMUL   rbp,133
  MOV    rdx,[rsp+-24]
  ADD    rbp,qword [rdx+68]
  POP    rax
  LEA    rax,[rbp]
  MOV    rax,rax
  PUSH   rax
  POP    rax
  MOV    qword [rsp+56],rax ; ret =  RAX
  ; ---------------------------------------
 
; ProcedureReturn ret 
  MOV    rax,qword [rsp+56]
  JMP   _EndProcedure3
; Else
  JMP   _EndIf12
_EndIf13:
; ProcedureReturn *table\items(size, 0)        
  MOV    rbp,qword [rsp+PS2+0]
  PUSH   rbp
  MOV    rdx,[rsp+0]
  MOV    rdx,qword [rdx+76]
  IMUL   rdx,qword [rsp+56]
  MOV    rbp,rdx
  PUSH   rbp
  XOR    rax,rax
  POP    rbp
  ADD    rbp,rax
  IMUL   rbp,133
  MOV    rdx,[rsp+0]
  ADD    rbp,qword [rdx+68]
  POP    rax
  LEA    rax,[rbp]
  JMP   _EndProcedure3
; EndIf
_EndIf12:
; 
; EndProcedure
_EndProcedureZero3:
  XOR    rax,rax
_EndProcedure3:
  ADD    rsp,72
  POP    r15
  POP    rbp
  RET
Last edited by SMaag on Fri Nov 01, 2024 2:28 pm, edited 1 time in total.
Joubarbe
Enthusiast
Enthusiast
Posts: 714
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: Decoupling an expression removes the crash (?)

Post by Joubarbe »

So I should just release a C version I guess? And hope for Fred to fix something? :D (if it's really not my fault of course, which I'm still not convinced)
User avatar
mk-soft
Always Here
Always Here
Posts: 6315
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Decoupling an expression removes the crash (?)

Post by mk-soft »

Workaround ...

Code: Select all

Procedure.i GetPreviousTableItem(*table._Table)
  Protected size
  
  With *table\selected_item\pos
    
    If *table\selected_item = #Null : DebuggerError("The table has no selected element.") : EndIf
    
    If \x > 0
      ProcedureReturn *table\items(\x - 1, \y)
    ElseIf \y > 0
      size = ArraySize(*table\items(), 1)
      ProcedureReturn *table\items(size, \y - 1)     ; Crashes here. "Invalid memory access" error.
    EndIf
    
  EndWith
  
  ProcedureReturn #Null
EndProcedure
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
SMaag
Enthusiast
Enthusiast
Posts: 327
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

Re: Decoupling an expression removes the crash (?)

Post by SMaag »

So I should just release a C version I guess? And hope for Fred to fix something? :D (if it's really not my fault of course, which I'm still not convinced)
I would use the workarround and detecting the size in an extra var,
size = ArraySize(*table\items(), 1)
That's working correct!
SMaag
Enthusiast
Enthusiast
Posts: 327
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

Re: Decoupling an expression removes the crash (?)

Post by SMaag »

maybe I located the error, but I'm 100% not sure!

Code: Select all

; size = ArraySize(*table\items(), 1)    
  PUSH   qword 1
  MOV    rbp,qword [rsp+PS2+8]  ; rsp+PS2+8
  PUSH   rbp
  MOV    rdx,[rsp+0]
  POP    rax
  PUSH   qword [rbp+68]
  POP    rcx
  POP    rdx
  CALL   PB_ArraySize2
  MOV    qword [rsp+48],rax  ; size = RAX
  
  ; ret = *table\items(ArraySize(*table\items(), 1), 0)
  MOV    rbp,qword [rsp+PS2+0]		; in the working code it is [rsp+PS2+8]
  PUSH   rbp
  SUB    rsp,8			; here we sub 8 from rsp
  PUSH   qword 1
  MOV    rbp,qword [rsp+PS2+24]	;here we add rsp, 24 what is in the result  [rsp+PS2+16] correct would be [rsp+PS2+8]
  PUSH   rbp
  MOV    rdx,[rsp+0]
  POP    rax
  PUSH   qword [rbp+68]
  POP    rcx
  POP    rdx

Joubarbe
Enthusiast
Enthusiast
Posts: 714
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: Decoupling an expression removes the crash (?)

Post by Joubarbe »

Now I'm having a doubt concerning this bug as well. Going to compile in C just to be safe, and see if players report other IMA bugs. Thanks a lot guys for your precious help.
SMaag
Enthusiast
Enthusiast
Posts: 327
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

Re: Decoupling an expression removes the crash (?)

Post by SMaag »

I created a bug report:

viewtopic.php?t=85647
User avatar
mk-soft
Always Here
Always Here
Posts: 6315
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Decoupling an expression removes the crash (?)

Post by mk-soft »

This is not the first time that the CPU registers have not been sufficient for complex nesting and an error has been triggered. However, this was very rare and could be solved with an extra variable.

@Joubarbe,
please create an executable code for debugging next time. That way we would have found the bug faster ;)

We here in the forum are all working on it and helping Fred to make PureBasic a good and bug free version. :wink:
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Joubarbe
Enthusiast
Enthusiast
Posts: 714
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: Decoupling an expression removes the crash (?)

Post by Joubarbe »

Is ArraySize() the cause of the crash, or everything that is "too" nested would crash? Should I add a variable for all ArraySize()?
User avatar
mk-soft
Always Here
Always Here
Posts: 6315
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Decoupling an expression removes the crash (?)

Post by mk-soft »

Sometimes it can happen when too many functions (like ArraySize) are nested. So it is not because of ArraySize.
Fred has fixed such bugs with the next version.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Joubarbe
Enthusiast
Enthusiast
Posts: 714
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: Decoupling an expression removes the crash (?)

Post by Joubarbe »

Thanks mk-soft! Then let's hope for the next version to be released soon! :D
Post Reply