Skip to content

Commit 8fbacc3

Browse files
WalterBrightGeod24
authored andcommitted
fix Issue 21914 - naked assembler functions get wrong offset to parameters
1 parent 2a1b162 commit 8fbacc3

3 files changed

Lines changed: 34 additions & 7 deletions

File tree

src/dmd/backend/cgcod.d

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -832,8 +832,9 @@ Lagain:
832832
{
833833
version (FRAMEPTR)
834834
{
835-
Para.size = ((farfunc ? 2 : 1) + needframe) * REGSIZE;
836-
if (needframe)
835+
bool frame = needframe || tyf & mTYnaked;
836+
Para.size = ((farfunc ? 2 : 1) + frame) * REGSIZE;
837+
if (frame)
837838
EBPtoESP = -REGSIZE;
838839
}
839840
else

src/dmd/backend/cod3.d

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,7 @@ void cgreg_dst_regs(reg_t* dst_integer_reg, reg_t* dst_float_reg)
752752
@trusted
753753
void cgreg_set_priorities(tym_t ty, const(reg_t)** pseq, const(reg_t)** pseqmsw)
754754
{
755+
//printf("cgreg_set_priorities %x\n", ty);
755756
const sz = tysize(ty);
756757

757758
if (tyxmmreg(ty))
@@ -5453,7 +5454,9 @@ void assignaddrc(code *c)
54535454
break;
54545455

54555456
case FLpara:
5456-
//printf("s = %s, Soffset = %d, Para.size = %d, BPoff = %d, EBPtoESP = %d\n", s.Sident.ptr, s.Soffset, Para.size, BPoff, EBPtoESP);
5457+
//printf("s = %s, Soffset = %d, Para.size = %d, BPoff = %d, EBPtoESP = %d, Vpointer = %d\n",
5458+
//s.Sident.ptr, cast(int)s.Soffset, cast(int)Para.size, cast(int)BPoff,
5459+
//cast(int)EBPtoESP, cast(int)c.IEV1.Vpointer);
54575460
soff = Para.size - BPoff; // cancel out add of BPoff
54585461
goto L1;
54595462

test/runnable/iasm.d

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4847,13 +4847,13 @@ L1: pop EAX;
48474847

48484848
extern(C) void f16400(int, ...)
48494849
{
4850-
asm {naked; ret;}
4850+
asm {naked; ret;}
48514851
}
48524852

48534853
void test16400()
48544854
{
4855-
assert(*(cast(ubyte*) &f16400) == 0xc3); // fails
4856-
f16400(0); // corrupts the stack
4855+
assert(*(cast(ubyte*) &f16400) == 0xc3); // fails
4856+
f16400(0); // corrupts the stack
48574857
}
48584858

48594859
/****************************************************/
@@ -4883,13 +4883,35 @@ void test5922()
48834883
asm
48844884
{
48854885
mov EDI, x;
4886-
mov y, EDI;
4886+
mov y, EDI;
48874887
}
48884888
assert(y == 10);
48894889
}
48904890

48914891
/****************************************************/
48924892

4893+
// https://issues.dlang.org/show_bug.cgi?id=21914
4894+
4895+
extern (C++) int insidx(int a, int b)
4896+
{
4897+
asm
4898+
{
4899+
naked ;
4900+
mov ECX,a-4+[ESP] ;
4901+
mov EAX,b-4+[ESP] ;
4902+
shl EAX,CL ;
4903+
ret ;
4904+
}
4905+
}
4906+
4907+
void test21914()
4908+
{
4909+
int x = insidx(5, 7);
4910+
assert(x == (7 << 5));
4911+
}
4912+
4913+
/****************************************************/
4914+
48934915
int main()
48944916
{
48954917
printf("Testing iasm.d\n");
@@ -4967,6 +4989,7 @@ int main()
49674989
test16400();
49684990
test16963();
49694991
test5922();
4992+
test21914();
49704993
}
49714994
printf("Success\n");
49724995
return 0;

0 commit comments

Comments
 (0)