Skip to content

Commit f86caa5

Browse files
authored
[release/6.0] Fix build with Clang 13 (#63314)
* Fix clang 13 induced runtime issues (#62170) The clang 13 optimizer started to assume that "this" pointer is always properly aligned. That lead to elimination of some code that was actually needed. It also takes pointer aliasing rules more strictly in one place in jit. That caused the optimizer to falsely assume that a callee with an argument passed by reference is not modifying that argument and used a stale copy of the original value at the caller site. This change fixes both of the issues. With this fix, runtime compiled using clang 13 seems to be fully functional. * Fix build with clang 13 (#60328)
1 parent 45d1f9e commit f86caa5

File tree

6 files changed

+36
-12
lines changed

6 files changed

+36
-12
lines changed

eng/native/configurecompiler.cmake

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -340,15 +340,17 @@ if (CLR_CMAKE_HOST_UNIX)
340340
#These seem to indicate real issues
341341
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-Wno-invalid-offsetof>)
342342

343-
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
343+
add_compile_options(-Wno-unused-but-set-variable)
344+
345+
if (CMAKE_C_COMPILER_ID MATCHES "Clang")
346+
add_compile_options(-Wno-unknown-warning-option)
347+
344348
# The -ferror-limit is helpful during the porting, it makes sure the compiler doesn't stop
345349
# after hitting just about 20 errors.
346350
add_compile_options(-ferror-limit=4096)
347351

348352
# Disabled warnings
349353
add_compile_options(-Wno-unused-private-field)
350-
# Explicit constructor calls are not supported by clang (this->ClassName::ClassName())
351-
add_compile_options(-Wno-microsoft)
352354
# There are constants of type BOOL used in a condition. But BOOL is defined as int
353355
# and so the compiler thinks that there is a mistake.
354356
add_compile_options(-Wno-constant-logical-operand)
@@ -363,8 +365,9 @@ if (CLR_CMAKE_HOST_UNIX)
363365
# to a struct or a class that has virtual members or a base class. In that case, clang
364366
# may not generate the same object layout as MSVC.
365367
add_compile_options(-Wno-incompatible-ms-struct)
368+
369+
add_compile_options(-Wno-reserved-identifier)
366370
else()
367-
add_compile_options(-Wno-unused-but-set-variable)
368371
add_compile_options(-Wno-unknown-pragmas)
369372
add_compile_options(-Wno-uninitialized)
370373
add_compile_options(-Wno-strict-aliasing)

src/coreclr/inc/corhlpr.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ struct COR_ILMETHOD_SECT
336336
const COR_ILMETHOD_SECT* Next() const
337337
{
338338
if (!More()) return(0);
339-
return ((COR_ILMETHOD_SECT*)(((BYTE *)this) + DataSize()))->Align();
339+
return ((COR_ILMETHOD_SECT*)Align(((BYTE *)this) + DataSize()));
340340
}
341341

342342
const BYTE* Data() const
@@ -374,9 +374,9 @@ struct COR_ILMETHOD_SECT
374374
return((AsSmall()->Kind & CorILMethod_Sect_FatFormat) != 0);
375375
}
376376

377-
const COR_ILMETHOD_SECT* Align() const
377+
static const void* Align(const void* p)
378378
{
379-
return((COR_ILMETHOD_SECT*) ((((UINT_PTR) this) + 3) & ~3));
379+
return((void*) ((((UINT_PTR) p) + 3) & ~3));
380380
}
381381

382382
protected:
@@ -579,7 +579,7 @@ typedef struct tagCOR_ILMETHOD_FAT : IMAGE_COR_ILMETHOD_FAT
579579

580580
const COR_ILMETHOD_SECT* GetSect() const {
581581
if (!More()) return (0);
582-
return(((COR_ILMETHOD_SECT*) (GetCode() + GetCodeSize()))->Align());
582+
return(((COR_ILMETHOD_SECT*) COR_ILMETHOD_SECT::Align(GetCode() + GetCodeSize())));
583583
}
584584
} COR_ILMETHOD_FAT;
585585

src/coreclr/jit/bitsetasshortlong.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ class BitSetOps</*BitSetType*/ BitSetShortLongRep,
345345
{
346346
if (IsShort(env))
347347
{
348-
(size_t&)out = (size_t)out & ((size_t)gen | (size_t)in);
348+
out = (BitSetShortLongRep)((size_t)out & ((size_t)gen | (size_t)in));
349349
}
350350
else
351351
{
@@ -361,7 +361,7 @@ class BitSetOps</*BitSetType*/ BitSetShortLongRep,
361361
{
362362
if (IsShort(env))
363363
{
364-
(size_t&)in = (size_t)use | ((size_t)out & ~(size_t)def);
364+
in = (BitSetShortLongRep)((size_t)use | ((size_t)out & ~(size_t)def));
365365
}
366366
else
367367
{

src/coreclr/jit/inlinepolicy.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ class DefaultPolicy : public LegalPolicy
185185
class ExtendedDefaultPolicy : public DefaultPolicy
186186
{
187187
public:
188-
ExtendedDefaultPolicy::ExtendedDefaultPolicy(Compiler* compiler, bool isPrejitRoot)
188+
ExtendedDefaultPolicy(Compiler* compiler, bool isPrejitRoot)
189189
: DefaultPolicy(compiler, isPrejitRoot)
190190
, m_ProfileFrequency(0.0)
191191
, m_BinaryExprWithCns(0)

src/libraries/Native/Unix/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ add_compile_options(-Wno-cast-align)
3636
add_compile_options(-Wno-typedef-redefinition)
3737
add_compile_options(-Wno-c11-extensions)
3838
add_compile_options(-Wno-unknown-pragmas)
39+
add_compile_options(-Wno-unknown-warning-option)
40+
add_compile_options(-Wno-unused-but-set-variable)
3941

4042
check_c_compiler_flag(-Wimplicit-fallthrough COMPILER_SUPPORTS_W_IMPLICIT_FALLTHROUGH)
4143
if (COMPILER_SUPPORTS_W_IMPLICIT_FALLTHROUGH)
@@ -48,6 +50,7 @@ add_compile_options(-g)
4850
if(CMAKE_C_COMPILER_ID STREQUAL Clang)
4951
add_compile_options(-Wthread-safety)
5052
add_compile_options(-Wno-thread-safety-analysis)
53+
add_compile_options(-Wno-reserved-identifier)
5154
elseif(CMAKE_C_COMPILER_ID STREQUAL GNU)
5255
add_compile_options(-Wno-stringop-truncation)
5356
endif()

src/libraries/Native/Unix/System.Native/pal_process.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,24 @@ static int SetGroups(uint32_t* userGroups, int32_t userGroupsLength, uint32_t* p
191191
return rv;
192192
}
193193

194+
typedef void (*VoidIntFn)(int);
195+
196+
static
197+
VoidIntFn
198+
handler_from_sigaction (struct sigaction *sa)
199+
{
200+
if (((unsigned int)sa->sa_flags) & SA_SIGINFO)
201+
{
202+
// work around -Wcast-function-type
203+
void (*tmp)(void) = (void (*)(void))sa->sa_sigaction;
204+
return (void (*)(int))tmp;
205+
}
206+
else
207+
{
208+
return sa->sa_handler;
209+
}
210+
}
211+
194212
int32_t SystemNative_ForkAndExecProcess(const char* filename,
195213
char* const argv[],
196214
char* const envp[],
@@ -371,7 +389,7 @@ int32_t SystemNative_ForkAndExecProcess(const char* filename,
371389
}
372390
if (!sigaction(sig, NULL, &sa_old))
373391
{
374-
void (*oldhandler)(int) = (((unsigned int)sa_old.sa_flags) & SA_SIGINFO) ? (void (*)(int))sa_old.sa_sigaction : sa_old.sa_handler;
392+
void (*oldhandler)(int) = handler_from_sigaction (&sa_old);
375393
if (oldhandler != SIG_IGN && oldhandler != SIG_DFL)
376394
{
377395
// It has a custom handler, put the default handler back.

0 commit comments

Comments
 (0)