Bug 2350 – Contracts with a naked body are indecent
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P4
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2008-09-09T02:50:00Z
Last change time
2015-07-01T08:08:22Z
Keywords
contracts, pull, wrong-code
Assigned to
nobody
Creator
clugdbug
Comments
Comment #0 by clugdbug — 2008-09-09T02:50:05Z
It seems that in/out contracts assume that a stack frame has been set up. This is not true for naked functions, so bad code is generated. Ideally, if the body contains the keyword 'naked', in/out contracts should create and destroy a stack frame.
----
void rude(int a)
in {
assert(a==1);
}
body {
asm { naked; }
}
void main() {
rude(1);
}
Comment #1 by smjg — 2011-09-04T15:39:44Z
In which case, what would "naked" do? If nothing, the compiler ought to disallow it.
Comment #2 by clugdbug — 2011-09-05T00:23:54Z
(In reply to comment #1)
> In which case, what would "naked" do? If nothing, the compiler ought to
> disallow it.
For non-naked functions, the contracts don't set up a stack frame, because the function already does it. The generated code is:
push EBP;
mov EBP, ESP;
<run in contract>
<run function body>
<run out contract>
pop EBP;
If no contracts are present, or with -release, the generated code is currently:
<run naked body>
which is correct.
But if contracts are present, and not in a release build, the code is:
<run in contract>
<run naked body>
<run out contract>
which causes a crash. Correct behaviour would be:
push EBP;
mov EBP, ESP;
<run in contract>
pop EBP;
<run naked body>
push EBP;
mov EBP, ESP;
<run out contract>
pop EBP;