-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
--passC:"-O3" with ARC and ORC miscompile slice on nested seqs #19597
Comments
The strings were a red herring:
Rather, it it's collateral damage from N_LIB_PRIVATE N_NIMCALL(void, setLen__cmdfile_71)(tySequence__PSP8snSsRoFs9cDiya9bd7UQ* s, NI newlen) {
{ {
NI T3_;
T3_ = (*s).len;
if (!(((NI) (newlen)) < T3_)) goto LA4_;
shrink__cmdfile_67(s, newlen);
}
goto LA1_;
LA4_: ;
{
NI oldLen;
NI T7_;
tyObject_NimSeqV2__k6A3SDA9bxp56RSL9bm9aXxvg* xu;
T7_ = (*s).len;
oldLen = T7_;
{
if (!(((NI) (newlen)) <= oldLen)) goto LA10_;
goto BeforeRet_;
}
LA10_: ;
xu = ((tyObject_NimSeqV2__k6A3SDA9bxp56RSL9bm9aXxvg*) (s));
{
NIM_BOOL T14_;
NI TM__Q5wkpxktOdTGvlSRo9bzt9aw_155;
void* T18_;
T14_ = (NIM_BOOL)0;
T14_ = ((*xu).p == ((tyObject_NimSeqPayload__0rETQpdPeHWsgle1jKnljg*) NIM_NIL));
if (T14_) goto LA15_;
T14_ = ((*(*xu).p).cap < ((NI) (newlen)));
LA15_: ;
if (!T14_) goto LA16_;
if (nimSubInt(((NI) (newlen)), oldLen, &TM__Q5wkpxktOdTGvlSRo9bzt9aw_155)) { raiseOverflow(); goto BeforeRet_;
};
T18_ = (void*)0;
T18_ = prepareSeqAdd(oldLen, ((void*) ((*xu).p)), (NI)(TM__Q5wkpxktOdTGvlSRo9bzt9aw_155), ((NI) 16), ((NI) 8));
(*xu).p = ((tyObject_NimSeqPayload__0rETQpdPeHWsgle1jKnljg*) (T18_));
}
LA16_: ;
(*xu).len = ((NI) (newlen));
}
LA1_: ;
}BeforeRet_: ;
} and in particular xu = ((tyObject_NimSeqV2__k6A3SDA9bxp56RSL9bm9aXxvg*) (s)); Which appear to be code generated from Lines 117 to 127 in 731eabc
Working around the type-punning/aliasing cast in the generated C code fixes this. It also doesn't seem to trigger for me so far on older gcc versions (e.g., 10.x) or any clang version. It needs some compilers settings which will generate that Lines 331 to 369 in 8ccde68
|
can't reproduce in d8d0832, neither |
Agree, can reproduce with commit |
The function
echo
outputs the wrong string. Well, at least, that's one manifestation.Example
This comes in some related varieties:
et cetera.
The key parts are (a) the nested sequences, and (b) so far, I've needed the inner scalar element to be a string to trigger this. If that element's there, and -O3 is used with:
This bug triggers when compiled using
nim c -r --passC:"-O3" --mm:orc nested_seq.nim
.Current Output
Tested with:
and
One gets (depending on exactly which assertion statement variation one chooses, but they're fundamentally the same):
One can alternatively see it this way, surveying the cartesian product of (debug, release) x (O2, O3) x (refc, arc, orc):
The necessary conditions within that space are
-O3
,-d:debug
, and ARC or ORC.Expected Output
Running successfully, without asserting.
Possible Solution
Don't use ARC or ORC, or don't use
--passC:"-O3"
(even-O2
avoids triggering it, and -O2 with all the options from https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#index-O3 which supposedly transform-O2
into-O3
don't trigger it; so far, I've needed to specify literally-O3
to trigger this) without doing so implicitly via-d:release
. The defaultrefc
memory manager also does not exhibit this problem.Debugging this, the relevant difference in behavior between
-O2
and-O3
seems to come fromnimAsgnStrV2
which gets embedded intostdlib_system.nim
not correctly copying the string of length 1, but rather not copying the string at all. This appears to be the key differentiator between memory managers which trigger this bug and which don't, that the ones which usenimAsgnStrV2
to effectively move the string, as thefrom
does not execute properly, leaving the string length at 0.
Additional Information
has a different problem for both
--gc:arc
and--gc:orc
:This provides a lower bound on relevant versions. It needs Nim 1.4 to even compile enough to play with this.
The text was updated successfully, but these errors were encountered: