Skip to content

Commit

Permalink
Fix Issue 24168 - Corrupted if TLS values are passed in ref parameters (
Browse files Browse the repository at this point in the history
dlang#15705)

when compiling with -fPIE
  • Loading branch information
kubo39 authored Oct 20, 2023
1 parent 0acee0c commit c2cab2c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 8 deletions.
32 changes: 24 additions & 8 deletions compiler/src/dmd/backend/elpicpie.d
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,26 @@ elem * el_var(Symbol *s)
if (config.exe & EX_posix)
{
if (config.flags3 & CFG3pie &&
s.Stype.Tty & mTYthread)
return el_pievar(s); // Position Independent Executable
s.Stype.Tty & mTYthread &&
(s.Sclass == SC.global ||
s.Sclass == SC.static_ ||
s.Sclass == SC.locstat))
{
}
else
{
if (config.flags3 & CFG3pie &&
s.Stype.Tty & mTYthread)
{
return el_pievar(s); // Position Independent Executable
}

if (config.flags3 & CFG3pic &&
!tyfunc(s.ty()))
return el_picvar(s); // Position Independent Code
if (config.flags3 & CFG3pic &&
!tyfunc(s.ty()))
{
return el_picvar(s); // Position Independent Code
}
}
}

if (config.exe & (EX_OSX | EX_OSX64))
Expand Down Expand Up @@ -128,7 +142,9 @@ else if (config.exe & EX_posix)
Obj.refGOTsym();
elem *e1 = el_calloc();
e1.EV.Vsym = s;
if (s.Sclass == SC.static_ || s.Sclass == SC.locstat)
if (s.Sclass == SC.global ||
s.Sclass == SC.static_ ||
s.Sclass == SC.locstat)
{
e1.Eoper = OPrelconst;
e1.Ety = TYnptr;
Expand Down Expand Up @@ -739,7 +755,7 @@ private elem *el_pievar(Symbol *s)
case SC.static_:
case SC.locstat:
case SC.global:
break;
assert(0);

case SC.comdat:
case SC.comdef:
Expand Down Expand Up @@ -767,7 +783,7 @@ private elem *el_pievar(Symbol *s)
case SC.static_:
case SC.locstat:
case SC.global:
break;
assert(0);

case SC.comdat:
case SC.comdef:
Expand Down
31 changes: 31 additions & 0 deletions compiler/test/runnable/issue24168.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
REQUIRED_ARGS: -fPIE
DISABLED: win32 win64
*/

//https://issues.dlang.org/show_bug.cgi?id=24168

int i = 42;

bool foo(ref int a)
{
return a == 42;
}

ref int bar()
{
return i;
}

bool baz()
{
static int i = 42;
return foo(i);
}

void main()
{
assert(foo(i));
assert(bar() == 42);
assert(baz());
}

0 comments on commit c2cab2c

Please sign in to comment.