Skip to content

Commit 4522b02

Browse files
parth-07partaror
authored andcommitted
Fix stack-buffer-overflow sanitizer issue in AUIPC_JALR_TO_XQCI.test
This commit fixes stack-buffer-overflow sanitizer issue in AUIPIC_JALR_TO_XQCI.test. The root cause was that the QC_E_J relaxation patch 6 bytes instruction but the 'Instr' parameter of 'RegionFragmentEx::replaceInstruction' that stores the updated instruction is only of 4 bytes. Signed-off-by: Parth Arora <partaror@qti.qualcomm.com>
1 parent d62c734 commit 4522b02

3 files changed

Lines changed: 13 additions & 11 deletions

File tree

include/eld/Fragment/RegionFragmentEx.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class RegionFragmentEx : public Fragment {
3636

3737
static bool classof(const RegionFragmentEx *) { return true; }
3838

39-
bool replaceInstruction(uint32_t Offset, Relocation *Reloc, uint32_t Instr,
39+
bool replaceInstruction(uint32_t Offset, Relocation *Reloc, uint8_t *Instr,
4040
uint8_t Size);
4141
void deleteInstruction(uint32_t Offset, uint32_t Size);
4242
void addRequiredNops(uint32_t Offset, uint32_t NumNopsToAdd);

lib/Fragment/RegionFragmentEx.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ RegionFragmentEx::RegionFragmentEx(const char *Buf, size_t Sz, ELFSection *O,
2222
RegionFragmentEx::~RegionFragmentEx() {}
2323

2424
bool RegionFragmentEx::replaceInstruction(uint32_t Offset, Relocation *Reloc,
25-
uint32_t Instr, uint8_t Size) {
26-
std::memcpy((void *)(Data + Offset), &Instr, Size);
25+
uint8_t *Instr, uint8_t Size) {
26+
std::memcpy((void *)(Data + Offset), Instr, Size);
2727
return true;
2828
}
2929

lib/Target/RISCV/RISCVLDBackend.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ bool RISCVLDBackend::doRelaxationCall(Relocation *reloc) {
313313
->getInput()
314314
->decoratedPath();
315315

316-
region->replaceInstruction(offset, reloc, c_j, 2);
316+
region->replaceInstruction(offset, reloc, reinterpret_cast<uint8_t *>(&c_j), 2);
317317
reloc->setTargetData(c_j);
318318
reloc->setType(llvm::ELF::R_RISCV_RVC_JUMP);
319319
relaxDeleteBytes("RISCV_CALL_C", *region, offset + 2, 6,
@@ -326,7 +326,7 @@ bool RISCVLDBackend::doRelaxationCall(Relocation *reloc) {
326326
// Replace the instruction to JAL
327327
uint32_t jal = 0x6fu | rd << 7;
328328

329-
region->replaceInstruction(offset, reloc, jal, 4 /* Replace bytes */);
329+
region->replaceInstruction(offset, reloc, reinterpret_cast<uint8_t *>(&jal), 4);
330330
reloc->setTargetData(jal);
331331
reloc->setType(llvm::ELF::R_RISCV_JAL);
332332
// Delete the next instruction
@@ -345,7 +345,7 @@ bool RISCVLDBackend::doRelaxationCall(Relocation *reloc) {
345345
const char *msg =
346346
(rd == 1) ? "R_RISCV_CALL_QC_E_JAL" : "R_RISCV_CALL_QC_E_J";
347347

348-
region->replaceInstruction(offset, reloc, qc_e_j, 6);
348+
region->replaceInstruction(offset, reloc, reinterpret_cast<uint8_t *>(&qc_e_j), 6);
349349
reloc->setTargetData(qc_e_j);
350350
reloc->setType(ELF::riscv::internal::R_RISCV_QC_E_CALL_PLT);
351351
relaxDeleteBytes(msg, *region, offset + 6, 2, reloc->symInfo()->name());
@@ -406,7 +406,7 @@ bool RISCVLDBackend::doRelaxationQCCall(Relocation *reloc) {
406406
->getInput()
407407
->decoratedPath();
408408

409-
region->replaceInstruction(offset, reloc, compressed, 2);
409+
region->replaceInstruction(offset, reloc, reinterpret_cast<uint8_t *>(&compressed), 2);
410410
// Replace the reloc to R_RISCV_RVC_JUMP
411411
reloc->setType(llvm::ELF::R_RISCV_RVC_JUMP);
412412
reloc->setTargetData(compressed);
@@ -418,7 +418,7 @@ bool RISCVLDBackend::doRelaxationQCCall(Relocation *reloc) {
418418
// Replace the instruction to JAL
419419
unsigned rd = isTailCall ? /*x0*/ 0 : /*ra*/ 1;
420420
uint32_t jal_instr = 0x6fu | rd << 7;
421-
region->replaceInstruction(offset, reloc, jal_instr, 4);
421+
region->replaceInstruction(offset, reloc, reinterpret_cast<uint8_t *>(&jal_instr), 4);
422422
// Replace the reloc to R_RISCV_JAL
423423
reloc->setType(llvm::ELF::R_RISCV_JAL);
424424
reloc->setTargetData(jal_instr);
@@ -622,7 +622,7 @@ bool RISCVLDBackend::doRelaxationQCELi(Relocation *reloc, Relocator::DWord G) {
622622
if (canRelaxQcLi) {
623623
uint32_t qc_li = 0x0000001bu | rd << 7;
624624

625-
region->replaceInstruction(offset, reloc, qc_li, 4);
625+
region->replaceInstruction(offset, reloc, reinterpret_cast<uint8_t *>(&qc_li), 4);
626626
reloc->setTargetData(qc_li);
627627
reloc->setType(ELF::riscv::internal::R_RISCV_QC_ABS20_U);
628628
relaxDeleteBytes(msg, *region, offset + 4, 2, reloc->symInfo()->name());
@@ -634,7 +634,7 @@ bool RISCVLDBackend::doRelaxationQCELi(Relocation *reloc, Relocator::DWord G) {
634634
unsigned rs = 3; // x3 = gp
635635
uint32_t addi = 0x00000013u | (rd << 7) | (rs << 15);
636636

637-
region->replaceInstruction(offset, reloc, addi, 4);
637+
region->replaceInstruction(offset, reloc, reinterpret_cast<uint8_t *>(&addi), 4);
638638
reloc->setTargetData(addi);
639639
reloc->setType(ELF::riscv::internal::R_RISCV_GPREL_I);
640640
relaxDeleteBytes(msg, *region, offset + 4, 2, reloc->symInfo()->name());
@@ -669,7 +669,9 @@ bool RISCVLDBackend::doRelaxationTLSDESC(Relocation &R, bool Relax) {
669669
else {
670670
// Otherwise, the instruction is replaced with a NOP.
671671
reportMissedRelaxation(RelaxType, *region, offset, 4, Sym.name());
672-
region->replaceInstruction(offset, &R, NOP, 4);
672+
uint32_t NOPi32 = static_cast<uint32_t>(NOP);
673+
region->replaceInstruction(
674+
offset, &R, reinterpret_cast<uint8_t *>(&NOPi32), 4);
673675
}
674676
R.setType(llvm::ELF::R_RISCV_NONE);
675677
return Relaxed;

0 commit comments

Comments
 (0)