|
43 | 43 | #include "llvm/MC/MCStreamer.h"
|
44 | 44 | #include "llvm/MC/MCSymbol.h"
|
45 | 45 | #include "llvm/MC/MCSymbolELF.h"
|
| 46 | +#include "llvm/Support/TargetRegistry.h" |
46 | 47 | #include "llvm/Target/TargetLoweringObjectFile.h"
|
47 | 48 | #include "llvm/Target/TargetMachine.h"
|
| 49 | +#include "llvm/Transforms/Instrumentation/AddressSanitizer.h" |
| 50 | +#include "llvm/Transforms/Instrumentation/AddressSanitizerCommon.h" |
48 | 51 |
|
49 | 52 | using namespace llvm;
|
50 | 53 |
|
@@ -1323,6 +1326,242 @@ void X86AsmPrinter::LowerFENTRY_CALL(const MachineInstr &MI,
|
1323 | 1326 | .addExpr(Op));
|
1324 | 1327 | }
|
1325 | 1328 |
|
| 1329 | +void X86AsmPrinter::LowerASAN_CHECK_MEMACCESS(const MachineInstr &MI) { |
| 1330 | + // FIXME: Make this work on non-ELF. |
| 1331 | + if (!TM.getTargetTriple().isOSBinFormatELF()) { |
| 1332 | + report_fatal_error("llvm.asan.check.memaccess only supported on ELF"); |
| 1333 | + return; |
| 1334 | + } |
| 1335 | + |
| 1336 | + unsigned Reg = MI.getOperand(0).getReg().id(); |
| 1337 | + ASanAccessInfo AccessInfo(MI.getOperand(1).getImm()); |
| 1338 | + |
| 1339 | + MCSymbol *&Sym = AsanMemaccessSymbols[{Reg, AccessInfo.Packed}]; |
| 1340 | + if (!Sym) { |
| 1341 | + std::string Name = AccessInfo.IsWrite ? "store" : "load"; |
| 1342 | + std::string SymName = "__asan_check_" + Name + |
| 1343 | + utostr(1 << AccessInfo.AccessSizeIndex) + "_rn" + |
| 1344 | + utostr(Reg); |
| 1345 | + Sym = OutContext.getOrCreateSymbol(SymName); |
| 1346 | + } |
| 1347 | + |
| 1348 | + EmitAndCountInstruction( |
| 1349 | + MCInstBuilder(X86::CALL64pcrel32) |
| 1350 | + .addExpr(MCSymbolRefExpr::create(Sym, OutContext))); |
| 1351 | +} |
| 1352 | + |
| 1353 | +void X86AsmPrinter::emitAsanMemaccessPartial(Module &M, unsigned Reg, |
| 1354 | + const ASanAccessInfo &AccessInfo, |
| 1355 | + MCSubtargetInfo &STI) { |
| 1356 | + assert(AccessInfo.AccessSizeIndex == 0 || AccessInfo.AccessSizeIndex == 1 || |
| 1357 | + AccessInfo.AccessSizeIndex == 2); |
| 1358 | + assert(Reg != X86::R8); |
| 1359 | + |
| 1360 | + uint64_t ShadowBase; |
| 1361 | + int MappingScale; |
| 1362 | + bool OrShadowOffset; |
| 1363 | + getAddressSanitizerParams( |
| 1364 | + Triple(M.getTargetTriple()), M.getDataLayout().getPointerSizeInBits(), |
| 1365 | + AccessInfo.CompileKernel, &ShadowBase, &MappingScale, &OrShadowOffset); |
| 1366 | + |
| 1367 | + OutStreamer->emitInstruction( |
| 1368 | + MCInstBuilder(X86::MOV64rr).addReg(X86::R8).addReg(X86::NoRegister + Reg), |
| 1369 | + STI); |
| 1370 | + OutStreamer->emitInstruction(MCInstBuilder(X86::SHR64ri) |
| 1371 | + .addReg(X86::R8) |
| 1372 | + .addReg(X86::NoRegister) |
| 1373 | + .addImm(MappingScale), |
| 1374 | + STI); |
| 1375 | + if (OrShadowOffset) { |
| 1376 | + OutStreamer->emitInstruction(MCInstBuilder(X86::OR64ri32) |
| 1377 | + .addReg(X86::NoRegister) |
| 1378 | + .addReg(X86::R8) |
| 1379 | + .addImm(ShadowBase), |
| 1380 | + STI); |
| 1381 | + OutStreamer->emitInstruction(MCInstBuilder(X86::MOV8rm) |
| 1382 | + .addReg(X86::R8B) |
| 1383 | + .addReg(X86::R8) |
| 1384 | + .addImm(1) |
| 1385 | + .addReg(X86::NoRegister) |
| 1386 | + .addImm(0) |
| 1387 | + .addReg(X86::NoRegister), |
| 1388 | + STI); |
| 1389 | + OutStreamer->emitInstruction( |
| 1390 | + MCInstBuilder(X86::TEST8rr).addReg(X86::R8B).addReg(X86::R8B), STI); |
| 1391 | + } else { |
| 1392 | + OutStreamer->emitInstruction(MCInstBuilder(X86::MOVSX32rm8) |
| 1393 | + .addReg(X86::R8D) |
| 1394 | + .addReg(X86::R8) |
| 1395 | + .addImm(1) |
| 1396 | + .addReg(X86::NoRegister) |
| 1397 | + .addImm(ShadowBase) |
| 1398 | + .addReg(X86::NoRegister), |
| 1399 | + STI); |
| 1400 | + OutStreamer->emitInstruction( |
| 1401 | + MCInstBuilder(X86::TEST32rr).addReg(X86::R8D).addReg(X86::R8D), STI); |
| 1402 | + } |
| 1403 | + MCSymbol *AdditionalCheck = OutContext.createTempSymbol(); |
| 1404 | + OutStreamer->emitInstruction( |
| 1405 | + MCInstBuilder(X86::JCC_1) |
| 1406 | + .addExpr(MCSymbolRefExpr::create(AdditionalCheck, OutContext)) |
| 1407 | + .addImm(X86::COND_NE), |
| 1408 | + STI); |
| 1409 | + MCSymbol *ReturnSym = OutContext.createTempSymbol(); |
| 1410 | + OutStreamer->emitLabel(ReturnSym); |
| 1411 | + OutStreamer->emitInstruction(MCInstBuilder(getRetOpcode(*Subtarget)), STI); |
| 1412 | + |
| 1413 | + // Shadow byte is non-zero so we need to perform additional checks. |
| 1414 | + OutStreamer->emitLabel(AdditionalCheck); |
| 1415 | + OutStreamer->emitInstruction(MCInstBuilder(X86::PUSH64r).addReg(X86::RCX), |
| 1416 | + STI); |
| 1417 | + OutStreamer->emitInstruction(MCInstBuilder(X86::MOV64rr) |
| 1418 | + .addReg(X86::RCX) |
| 1419 | + .addReg(X86::NoRegister + Reg), |
| 1420 | + STI); |
| 1421 | + const size_t Granularity = 1ULL << MappingScale; |
| 1422 | + OutStreamer->emitInstruction(MCInstBuilder(X86::AND32ri8) |
| 1423 | + .addReg(X86::NoRegister) |
| 1424 | + .addReg(X86::ECX) |
| 1425 | + .addImm(Granularity - 1), |
| 1426 | + STI); |
| 1427 | + if (AccessInfo.AccessSizeIndex == 1) { |
| 1428 | + OutStreamer->emitInstruction(MCInstBuilder(X86::ADD32ri8) |
| 1429 | + .addReg(X86::NoRegister) |
| 1430 | + .addReg(X86::ECX) |
| 1431 | + .addImm(1), |
| 1432 | + STI); |
| 1433 | + } else if (AccessInfo.AccessSizeIndex == 2) { |
| 1434 | + OutStreamer->emitInstruction(MCInstBuilder(X86::ADD32ri8) |
| 1435 | + .addReg(X86::NoRegister) |
| 1436 | + .addReg(X86::ECX) |
| 1437 | + .addImm(3), |
| 1438 | + STI); |
| 1439 | + } |
| 1440 | + |
| 1441 | + OutStreamer->emitInstruction( |
| 1442 | + MCInstBuilder(X86::CMP32rr).addReg(X86::ECX).addReg(X86::R8D).addImm(1), |
| 1443 | + STI); |
| 1444 | + OutStreamer->emitInstruction(MCInstBuilder(X86::POP64r).addReg(X86::RCX), |
| 1445 | + STI); |
| 1446 | + OutStreamer->emitInstruction( |
| 1447 | + MCInstBuilder(X86::JCC_1) |
| 1448 | + .addExpr(MCSymbolRefExpr::create(ReturnSym, OutContext)) |
| 1449 | + .addImm(X86::COND_L), |
| 1450 | + STI); |
| 1451 | + |
| 1452 | + emitAsanReportError(M, Reg, AccessInfo, STI); |
| 1453 | +} |
| 1454 | + |
| 1455 | +void X86AsmPrinter::emitAsanMemaccessFull(Module &M, unsigned Reg, |
| 1456 | + const ASanAccessInfo &AccessInfo, |
| 1457 | + MCSubtargetInfo &STI) { |
| 1458 | + assert(AccessInfo.AccessSizeIndex == 3 || AccessInfo.AccessSizeIndex == 4); |
| 1459 | + assert(Reg != X86::R8); |
| 1460 | + |
| 1461 | + uint64_t ShadowBase; |
| 1462 | + int MappingScale; |
| 1463 | + bool OrShadowOffset; |
| 1464 | + getAddressSanitizerParams( |
| 1465 | + Triple(M.getTargetTriple()), M.getDataLayout().getPointerSizeInBits(), |
| 1466 | + AccessInfo.CompileKernel, &ShadowBase, &MappingScale, &OrShadowOffset); |
| 1467 | + |
| 1468 | + OutStreamer->emitInstruction( |
| 1469 | + MCInstBuilder(X86::MOV64rr).addReg(X86::R8).addReg(X86::NoRegister + Reg), |
| 1470 | + STI); |
| 1471 | + OutStreamer->emitInstruction(MCInstBuilder(X86::SHR64ri) |
| 1472 | + .addReg(X86::R8) |
| 1473 | + .addReg(X86::R8) |
| 1474 | + .addImm(MappingScale), |
| 1475 | + STI); |
| 1476 | + if (OrShadowOffset) { |
| 1477 | + OutStreamer->emitInstruction(MCInstBuilder(X86::OR64ri32) |
| 1478 | + .addReg(X86::R8) |
| 1479 | + .addReg(X86::R8) |
| 1480 | + .addImm(ShadowBase), |
| 1481 | + STI); |
| 1482 | + auto OpCode = AccessInfo.AccessSizeIndex == 3 ? X86::CMP8mi : X86::CMP16mi8; |
| 1483 | + OutStreamer->emitInstruction(MCInstBuilder(OpCode) |
| 1484 | + .addReg(X86::R8) |
| 1485 | + .addImm(1) |
| 1486 | + .addReg(X86::NoRegister) |
| 1487 | + .addImm(0) |
| 1488 | + .addReg(X86::NoRegister) |
| 1489 | + .addImm(0), |
| 1490 | + STI); |
| 1491 | + } else { |
| 1492 | + auto OpCode = AccessInfo.AccessSizeIndex == 3 ? X86::CMP8mi : X86::CMP16mi8; |
| 1493 | + OutStreamer->emitInstruction(MCInstBuilder(OpCode) |
| 1494 | + .addReg(X86::R8) |
| 1495 | + .addImm(1) |
| 1496 | + .addReg(X86::NoRegister) |
| 1497 | + .addImm(ShadowBase) |
| 1498 | + .addReg(X86::NoRegister) |
| 1499 | + .addImm(0), |
| 1500 | + STI); |
| 1501 | + } |
| 1502 | + MCSymbol *ReportCode = OutContext.createTempSymbol(); |
| 1503 | + OutStreamer->emitInstruction( |
| 1504 | + MCInstBuilder(X86::JCC_1) |
| 1505 | + .addExpr(MCSymbolRefExpr::create(ReportCode, OutContext)) |
| 1506 | + .addImm(X86::COND_NE), |
| 1507 | + STI); |
| 1508 | + MCSymbol *ReturnSym = OutContext.createTempSymbol(); |
| 1509 | + OutStreamer->emitLabel(ReturnSym); |
| 1510 | + OutStreamer->emitInstruction(MCInstBuilder(getRetOpcode(*Subtarget)), STI); |
| 1511 | + |
| 1512 | + OutStreamer->emitLabel(ReportCode); |
| 1513 | + emitAsanReportError(M, Reg, AccessInfo, STI); |
| 1514 | +} |
| 1515 | + |
| 1516 | +void X86AsmPrinter::emitAsanReportError(Module &M, unsigned Reg, |
| 1517 | + const ASanAccessInfo &AccessInfo, |
| 1518 | + MCSubtargetInfo &STI) { |
| 1519 | + std::string Name = AccessInfo.IsWrite ? "store" : "load"; |
| 1520 | + MCSymbol *ReportError = OutContext.getOrCreateSymbol( |
| 1521 | + "__asan_report_" + Name + utostr(1 << AccessInfo.AccessSizeIndex)); |
| 1522 | + OutStreamer->emitInstruction(MCInstBuilder(X86::MOV64rr) |
| 1523 | + .addReg(X86::RDI) |
| 1524 | + .addReg(X86::NoRegister + Reg), |
| 1525 | + STI); |
| 1526 | + OutStreamer->emitInstruction( |
| 1527 | + MCInstBuilder(X86::JMP_1) |
| 1528 | + .addExpr(MCSymbolRefExpr::create(ReportError, OutContext)), |
| 1529 | + STI); |
| 1530 | +} |
| 1531 | + |
| 1532 | +void X86AsmPrinter::emitAsanMemaccessSymbols(Module &M) { |
| 1533 | + if (AsanMemaccessSymbols.empty()) |
| 1534 | + return; |
| 1535 | + |
| 1536 | + const Triple &TT = TM.getTargetTriple(); |
| 1537 | + assert(TT.isOSBinFormatELF()); |
| 1538 | + std::unique_ptr<MCSubtargetInfo> STI( |
| 1539 | + TM.getTarget().createMCSubtargetInfo(TT.str(), "", "")); |
| 1540 | + assert(STI && "Unable to create subtarget info"); |
| 1541 | + |
| 1542 | + for (auto &P : AsanMemaccessSymbols) { |
| 1543 | + MCSymbol *Sym = P.second; |
| 1544 | + OutStreamer->SwitchSection(OutContext.getELFSection( |
| 1545 | + ".text.hot", ELF::SHT_PROGBITS, |
| 1546 | + ELF::SHF_EXECINSTR | ELF::SHF_ALLOC | ELF::SHF_GROUP, 0, Sym->getName(), |
| 1547 | + /*IsComdat=*/true)); |
| 1548 | + |
| 1549 | + OutStreamer->emitSymbolAttribute(Sym, MCSA_ELF_TypeFunction); |
| 1550 | + OutStreamer->emitSymbolAttribute(Sym, MCSA_Weak); |
| 1551 | + OutStreamer->emitSymbolAttribute(Sym, MCSA_Hidden); |
| 1552 | + OutStreamer->emitLabel(Sym); |
| 1553 | + |
| 1554 | + unsigned Reg = std::get<0>(P.first); |
| 1555 | + ASanAccessInfo AccessInfo(std::get<1>(P.first)); |
| 1556 | + |
| 1557 | + if (AccessInfo.AccessSizeIndex < 3) { |
| 1558 | + emitAsanMemaccessPartial(M, Reg, AccessInfo, *STI); |
| 1559 | + } else { |
| 1560 | + emitAsanMemaccessFull(M, Reg, AccessInfo, *STI); |
| 1561 | + } |
| 1562 | + } |
| 1563 | +} |
| 1564 | + |
1326 | 1565 | void X86AsmPrinter::LowerPATCHABLE_OP(const MachineInstr &MI,
|
1327 | 1566 | X86MCInstLower &MCIL) {
|
1328 | 1567 | // PATCHABLE_OP minsize, opcode, operands
|
@@ -2563,6 +2802,9 @@ void X86AsmPrinter::emitInstruction(const MachineInstr *MI) {
|
2563 | 2802 | EmitAndCountInstruction(MCInstBuilder(getRetOpcode(*Subtarget)));
|
2564 | 2803 | return;
|
2565 | 2804 |
|
| 2805 | + case X86::ASAN_CHECK_MEMACCESS: |
| 2806 | + return LowerASAN_CHECK_MEMACCESS(*MI); |
| 2807 | + |
2566 | 2808 | case X86::MORESTACK_RET_RESTORE_R10:
|
2567 | 2809 | // Return, then restore R10.
|
2568 | 2810 | EmitAndCountInstruction(MCInstBuilder(getRetOpcode(*Subtarget)));
|
|
0 commit comments