Skip to content

Commit f1c21fa

Browse files
committed
[AArch64] Switch magic numbers to library functions in fixup
Using masks and bounds that are magic numbers means that defects in bounds checking and offset masking are subtle and hard to detect. https://reviews.llvm.org/D152841 is an example of this type of defect. Switching to clearly readable library functions makes defects less obfuscated. Differential Revision: https://reviews.llvm.org/D152843
1 parent 723e424 commit f1c21fa

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

llvm/lib/Target/AArch64/MCTargetDesc/AArch64AsmBackend.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,14 @@ static uint64_t adjustFixupValue(const MCFixup &Fixup, const MCValue &Target,
181181
if (TheTriple.isOSBinFormatCOFF() && !IsResolved)
182182
Value &= 0xfff;
183183
// Unsigned 12-bit immediate
184-
if (Value >= 0x1000)
184+
if (!isUInt<12>(Value))
185185
Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
186186
return Value;
187187
case AArch64::fixup_aarch64_ldst_imm12_scale2:
188188
if (TheTriple.isOSBinFormatCOFF() && !IsResolved)
189189
Value &= 0xfff;
190190
// Unsigned 12-bit immediate which gets multiplied by 2
191-
if (Value >= 0x2000)
191+
if (!isUInt<13>(Value))
192192
Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
193193
if (Value & 0x1)
194194
Ctx.reportError(Fixup.getLoc(), "fixup must be 2-byte aligned");
@@ -197,7 +197,7 @@ static uint64_t adjustFixupValue(const MCFixup &Fixup, const MCValue &Target,
197197
if (TheTriple.isOSBinFormatCOFF() && !IsResolved)
198198
Value &= 0xfff;
199199
// Unsigned 12-bit immediate which gets multiplied by 4
200-
if (Value >= 0x4000)
200+
if (!isUInt<14>(Value))
201201
Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
202202
if (Value & 0x3)
203203
Ctx.reportError(Fixup.getLoc(), "fixup must be 4-byte aligned");
@@ -206,7 +206,7 @@ static uint64_t adjustFixupValue(const MCFixup &Fixup, const MCValue &Target,
206206
if (TheTriple.isOSBinFormatCOFF() && !IsResolved)
207207
Value &= 0xfff;
208208
// Unsigned 12-bit immediate which gets multiplied by 8
209-
if (Value >= 0x8000)
209+
if (!isUInt<15>(Value))
210210
Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
211211
if (Value & 0x7)
212212
Ctx.reportError(Fixup.getLoc(), "fixup must be 8-byte aligned");
@@ -215,7 +215,7 @@ static uint64_t adjustFixupValue(const MCFixup &Fixup, const MCValue &Target,
215215
if (TheTriple.isOSBinFormatCOFF() && !IsResolved)
216216
Value &= 0xfff;
217217
// Unsigned 12-bit immediate which gets multiplied by 16
218-
if (Value >= 0x10000)
218+
if (!isUInt<16>(Value))
219219
Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
220220
if (Value & 0xf)
221221
Ctx.reportError(Fixup.getLoc(), "fixup must be 16-byte aligned");
@@ -306,7 +306,7 @@ static uint64_t adjustFixupValue(const MCFixup &Fixup, const MCValue &Target,
306306
}
307307
case AArch64::fixup_aarch64_pcrel_branch14:
308308
// Signed 16-bit immediate
309-
if (SignedValue > 32767 || SignedValue < -32768)
309+
if (!isInt<16>(SignedValue))
310310
Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
311311
// Low two bits are not encoded (4-byte alignment assumed).
312312
if (Value & 0x3)
@@ -315,7 +315,7 @@ static uint64_t adjustFixupValue(const MCFixup &Fixup, const MCValue &Target,
315315
case AArch64::fixup_aarch64_pcrel_branch26:
316316
case AArch64::fixup_aarch64_pcrel_call26:
317317
// Signed 28-bit immediate
318-
if (SignedValue > 134217727 || SignedValue < -134217728)
318+
if (!isInt<28>(SignedValue))
319319
Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
320320
// Low two bits are not encoded (4-byte alignment assumed).
321321
if (Value & 0x3)

0 commit comments

Comments
 (0)