Security.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #pragma once
  6. class Security
  7. {
  8. private:
  9. Func *func;
  10. uint currentConstSize;
  11. IR::RegOpnd * basePlusCookieOpnd;
  12. IR::IntConstOpnd * cookieOpnd;
  13. IR::RegOpnd * baseOpnd;
  14. public:
  15. Security(Func * func) : func(func), currentConstSize(0), basePlusCookieOpnd(nullptr), cookieOpnd(nullptr), baseOpnd(nullptr) { }
  16. void EncodeLargeConstants();
  17. void InsertNOPs();
  18. static bool DontEncode(IR::Opnd *opnd);
  19. static void InsertRandomFunctionPad(IR::Instr * instrBeforeInstr);
  20. private:
  21. bool EncodeOpnd(IR::Instr *instr, IR::Opnd *opnd);
  22. static uint CalculateConstSize(IR::Opnd *opnd);
  23. IntConstType EncodeValue(IR::Instr *instr, IR::Opnd *opnd, IntConstType constValue, _Out_ IR::RegOpnd ** pNewOpnd);
  24. #if TARGET_64
  25. size_t EncodeAddress(IR::Instr *instr, IR::Opnd *opnd, size_t value, _Out_ IR::RegOpnd **pNewOpnd);
  26. #endif
  27. static IR::IntConstOpnd * BuildCookieOpnd(IRType type, Func * func);
  28. template<typename T> static uint GetByteCount(T value)
  29. {
  30. uint byteCount = 0;
  31. for (uint i = 0; i < sizeof(T); ++i)
  32. {
  33. if (IsByteSet(value, i))
  34. {
  35. ++byteCount;
  36. }
  37. }
  38. return byteCount;
  39. }
  40. template<typename T> static bool IsByteSet(T value, uint32 index)
  41. {
  42. const byte byteValue = (byte)(value >> (index * MachBits));
  43. return byteValue != 0 && byteValue != 0xFF;
  44. }
  45. void InsertNOPBefore(IR::Instr *instr);
  46. int GetNextNOPInsertPoint();
  47. // Insert 1-4 bytes of NOPs
  48. static void InsertSmallNOP(IR::Instr * instrBeforeInstr, DWORD nopSize);
  49. };