This is an archive of the discontinued LLVM Phabricator instance.

[bolt] Fix typo in BinaryFunction::parseLSDA
AbandonedPublic

Authored by RIscRIpt on May 9 2023, 2:52 PM.

Details

Summary

According to EHStreamer::emitExceptionTable LLVM emits absptr
address in non-PIC case. Otherwise, LLVM emits pcrel offset.

Diff Detail

Event Timeline

RIscRIpt created this revision.May 9 2023, 2:52 PM
Herald added a reviewer: Amir. · View Herald Transcript
Herald added a reviewer: maksfb. · View Herald Transcript
Herald added a project: Restricted Project. · View Herald Transcript
RIscRIpt requested review of this revision.May 9 2023, 2:52 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 9 2023, 2:52 PM

I think it went unnoticed, because AFAICS lpStartEncoding is omit most of the times, which resulted MaybeLPStart == std::nullopt, and LPStart = 0.

LPStart = 0
LPOffset = LPStart + LandingPad;
LPAddress = Address + LPOffset; // == Address + 0 + LandingPad == Address + LandingPad
LPAddress < Address == false // OK, fragment is not skipped

However if lpStartEncoding was not omit, this should have resulted in skipping the fragment:

LPStart = *MaybeLPStart - Address
LPOffset = LPStart + LandingPad;
LPAddress = Address + LPOffset; // == Address + *MaybeLPStart - Address + LandingPad == *MaybeLPStart + LandingPad
LPAddress < Address == true // iff *MaybeLPStart is not absolute (i.e. *MaybeLPStart < Address)

This had to work correctly in case of non-PIC binaries (with absolute addresses in *MaybeLPStart), and it had to result in a bug for PIC binaries.

I came across this typo while studying relevant code; my thinking may be wrong.

Apparently this code was introduced at https://reviews.llvm.org/D128561 but no test case was provided showing when LPStart would be present.

Honestly I do not understand why would we subtract Address when LPStart is encoded with absptr but not for other encodings. Of course, talking about the unknown case where LPStart is actually present and not omitted - I still need to find a case where this is actually used. I don't have any test case where LPStart encoding is not set to omit. @Amir any idea on which binary would have that? It would be better to have a testcase where LPStart is actually used, so we can better understand what we are fixing.

I'm inclined towards always decrementing this Address instead of having the if(absptr). Another thing that is wrong is that LPStart can potentially be negative after subtracting Address from it, but the variable itself is declared as unsigned. Confusing. I think it needs to be clear that:

  • LPStart is ALWAYS a full pointer (when present). Therefore, if we want to extract an offset out of it, we ought to subtract Address for all cases.
  • LandingPad is ALWAYS an offset.
Amir added a comment.May 10 2023, 7:01 PM

Apparently this code was introduced at https://reviews.llvm.org/D128561 but no test case was provided showing when LPStart would be present.

Honestly I do not understand why would we subtract Address when LPStart is encoded with absptr but not for other encodings. Of course, talking about the unknown case where LPStart is actually present and not omitted - I still need to find a case where this is actually used. I don't have any test case where LPStart encoding is not set to omit. @Amir any idea on which binary would have that? It would be better to have a testcase where LPStart is actually used, so we can better understand what we are fixing.

I'm inclined towards always decrementing this Address instead of having the if(absptr). Another thing that is wrong is that LPStart can potentially be negative after subtracting Address from it, but the variable itself is declared as unsigned. Confusing. I think it needs to be clear that:

  • LPStart is ALWAYS a full pointer (when present). Therefore, if we want to extract an offset out of it, we ought to subtract Address for all cases.
  • LandingPad is ALWAYS an offset.

Sorry about that omission in test. The binary (HHVM) was compiled with Clang with -fsplit-machine-functions. Let me see if I can repro it and come up with a reduced test case.

Bump. Tbh, I am not interested in diving into Bolt project, so you might do whatever you want with this patch.

RIscRIpt abandoned this revision.Mon, Nov 20, 10:43 PM

Thanks, closing. As per your changes, looks like my fix was not correct after all.