No error on prototyped functions without arguments

Post bugreports for the Windows version here
Joubarbe
Enthusiast
Enthusiast
Posts: 710
Joined: Wed Sep 18, 2013 11:54 am
Location: France

No error on prototyped functions without arguments

Post by Joubarbe »

Code: Select all

Procedure$ ReturnHelloWorld() ; Put "args$" as argument to avoid the crash in C backend.
  ProcedureReturn "Hello world"
EndProcedure

Prototype$ proto(args$)

Define func.proto = @ReturnHelloWorld()

Debug func("")
Works in ASM, doesn't work in C. Sometimes will crash, sometimes will print garbage.

I think it shouldn't work in ASM as well, but it should give an error instead of quitting "unexpectedly". Is it possible though?


// Moved from "Bugs - C backend" to "Coding Questions" (Kiffi)
SMaag
Enthusiast
Enthusiast
Posts: 324
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

Re: No error on prototyped functions without arguments

Post by SMaag »

That's not a real bug. With your defintion you create a Stack-Problem.
If you define the Prototype with a Stringparameter and your RetrunHelloWorld does not have a parameter, you
Push a String on the Stack what is not processed.
Because a Prototype give only the defintion of Procedure what you call over a Pointer it can not check the correctness of the Parameters

Code: Select all

Procedure$ ReturnHelloWorld(Str$) ; Put "args$" as argument to avoid the crash in C backend.
  ProcedureReturn "Hello world"
EndProcedure

Prototype$ proto(args$)

Define func.proto = @ReturnHelloWorld()

Debug func("")
or

Code: Select all

Procedure$ ReturnHelloWorld() ; Put "args$" as argument to avoid the crash in C backend.
  ProcedureReturn "Hello world"
EndProcedure

Prototype$ proto()

Define func.proto = @ReturnHelloWorld()

Debug func()
Joubarbe
Enthusiast
Enthusiast
Posts: 710
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: No error on prototyped functions without arguments

Post by Joubarbe »

I understand the problem. The bug, to me, is that there are no errors. And it's a bit weird that it works "fine" in ASM (and it probably doesn't, but it never crashes).

EDIT: Isn't the purifier supposed to detect stack-related problems?
SMaag
Enthusiast
Enthusiast
Posts: 324
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

Re: No error on prototyped functions without arguments

Post by SMaag »

here the C-Output of your definition

Code: Select all

void* r0=v_func(_S2,SYS_PopStringBasePosition());
In C there are 2 Parameters

if I remove the arg$, the output is different, and it works

Code: Select all

void* r0=v_func(SYS_PopStringBasePosition())
It seems C creates extra Parameter for the Return-String what is added to the function call!
So in the C-Backend it is done correctly what creates a Stack problem if one Parameter is missing.
PB help says for Prototype - Parameters are checked. But in reals I think the PB compiler do not check!

in the ASM Backend the function call pushes 2 String-Pointers on the Stack too!
1 for the Return String on one for the arg$. But it do not crash!

Code: Select all

; res$= func("")
  PUSH   qword [PB_StringBasePosition]
  PUSH   qword [PB_StringBasePosition]
   PUSH   rax
  POP    rcx
  SUB    rsp,32
  CALL   qword [v_func]
  ADD    rsp,40
  LEA    rcx,[v_res$]
  POP    rdx
  CALL   SYS_AllocateString4

I guess it works because the Stack is correctly managed with
SUB rsp,32 and ADD resp40

inside RetrunHelloWorld in ASM-Backend:
Stack is managed with Sub rsp, 40 and at the end with Add rsp 40
That shows, that the Stack-Correction for the Functions Parameters is not done inside the Procedure, it's done directly with the call.
That's the reason why it works in ASM. Maybe this is a workarround in the ASM Backend to prevent missing parameters.

I guess if we would disassemble the C-Exe we will find the Stack-Correction of the Procedrue Parameters inside the Procedure
what will end up in a Stack-Problem.


But generally. If you define a Parameter in a Prototype, you have to do this correctly.
Your Prototype definition for ReturnHelloWorld is wrong!
I confirm, there should be a message because PB Help says parameters are checked!
Joubarbe
Enthusiast
Enthusiast
Posts: 710
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: No error on prototyped functions without arguments

Post by Joubarbe »

Thanks for looking into it. I always appreciate your fine knowledge of ASM!

Oh I know my definition is wrong; still, I've spent the two last days trying to identify a very random crash. Developers are humans, humans make mistakes, and IDE should prevent those mistakes as much as possible. So I still consider this a bug (as a non wanted behaviour), and this shouldn't have been moved so quickly. It might not be a core bug, but not raising a proper error can also be considered a bug.

Now the real question is: even though the documentation says "type checking is done, number of of parameters is validated", would it really be doable in that case?
SMaag
Enthusiast
Enthusiast
Posts: 324
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

Re: No error on prototyped functions without arguments

Post by SMaag »

Now the real question is: even though the documentation says "type checking is done, number of of parameters is validated", would it really be doable in that case?
I'm surprised about this description: In my opinion it is not possible to check Parameters and Types for Prototyping DLL Procedures. As I know there is no information about this in a DLL.
I guess, it is not possible to check. Looks more like a bug in the Help.

For PB Functions it would be possible to check.
Fred
Administrator
Administrator
Posts: 18199
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: No error on prototyped functions without arguments

Post by Fred »

If a function is used, the check should be done to avoid such errors
Post Reply