Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
3.8 KiB

  1. #pragma once
  2. #include "windows.h"
  3. #include "assert.h"
  4. #include <map>
  5. #include <iostream>
  6. #include <string>
  7. #include <fstream>
  8. #include <strstream>
  9. #define POINTER_ARITHMATIC_FUNC "pointer_arithmatic"
  10. int ReportInternalError(int nLine);
  11. #define ASSERT assert
  12. #define PragmaUnsafe_LogMessage printf
  13. #define PragmaUnsafe_ReportError ReportInternalError(__LINE__);printf("PragmaUnsafe: ");printf
  14. using namespace std;
  15. static const int PragmaUnsafe_MAX_PRAGMA_NEST_LEVEL = 64;
  16. static const int STATUS_BYTE_ARRAY_SIZE = 8;
  17. static const int PragmaUnsafe_STACK_SIZE_IN_BYTE = 8;
  18. #define STRING_TRIM_FLAG_LEFT 0x01
  19. #define STRING_TRIM_FLAG_RIGHT 0x02
  20. class PragmaUnsafe_FunctionStatus{
  21. private:
  22. BYTE m_status[STATUS_BYTE_ARRAY_SIZE];
  23. public:
  24. PragmaUnsafe_FunctionStatus(){
  25. ZeroMemory(&m_status, sizeof(m_status));
  26. }
  27. BYTE & operator [](const int i){ return m_status[i]; }
  28. };
  29. typedef class PragmaUnsafe_FunctionStatus PragmaUnsafe_FUNCTION_STATUS;
  30. /*--------------------------------------------------------------------------------------
  31. (1) the stack is push/pop by BIT, that is, it is a BIT stack
  32. (2) max-size of the stack is 64, since it is 1 bit per state, totally we use 8 bytes
  33. (3) for each function, it has an associated CPragmaUnsafe_UnsafeFunctionStateStack
  34. --------------------------------------------------------------------------------------*/
  35. class CPragmaUnsafe_UnsafeFunctionStateStack
  36. {
  37. private:
  38. typedef map<string, PragmaUnsafe_FUNCTION_STATUS> PragmaUnsafe_PRAGMA_UNSAFE_FUNCTIONS;
  39. PragmaUnsafe_PRAGMA_UNSAFE_FUNCTIONS m_UnsafeFuncs;
  40. DWORD m_index;
  41. bool m_fInitialized;
  42. inline BOOL IsStackFull() { return (m_index == PragmaUnsafe_MAX_PRAGMA_NEST_LEVEL) ? TRUE : FALSE; }
  43. inline BOOL IsStackEmpty(){ return (m_index == 0) ? TRUE : FALSE; }
  44. inline BOOL IsInitialized() {return (m_fInitialized == true) ? TRUE : FALSE; }
  45. //
  46. // (0) the stack is not FULL, that is, m_index < 64
  47. // (1) no push is needed because what we change is the current status
  48. // (2) the default value is always 0 because we add disabled function by "#pragma unsafe(disable: func)"
  49. //
  50. BOOL AddFunctionIntoStack(const char * strFuncNameGroups, bool fEnabled = false);
  51. BOOL ResetStack(const char * strFuncNameGroups, bool fEnable);
  52. VOID CPragmaUnsafe_UnsafeFunctionStateStack::PackStack();
  53. public:
  54. BOOL OnUnsafeDisable(const char * strFuncNameGroups);
  55. BOOL OnUnsafePush();
  56. BOOL OnUnsafeEnable(const char * strFuncNameGroups);
  57. BOOL OnUnsafePop();
  58. VOID PrintFunctionCurrentStatus(int);
  59. BOOL CheckIntegrityAtEndOfFile();
  60. //
  61. // for plugin code to check whether a func is disable or not
  62. // return TRUE if the func is (1) not in the stack, (2) in the stack but the current status is enabled,
  63. // else return FALSE
  64. BOOL IsFunctionNotUnsafe(const char * strFuncName);
  65. CPragmaUnsafe_UnsafeFunctionStateStack() : m_index(0), m_fInitialized(false) {}
  66. // put pointer_arithmatic as the first function to consider
  67. BOOL Initialize();
  68. BOOL ReInitialize();
  69. };
  70. typedef class CPragmaUnsafe_UnsafeFunctionStateStack PragmaUnsafe_UNSAFE_FUNCTIONS;
  71. extern PragmaUnsafe_UNSAFE_FUNCTIONS Sxs_PragmaUnsafedFunctions;
  72. typedef enum {
  73. PRAGMA_NOT_UNSAFE_STATEMENT = 0,
  74. PRAGMA_UNSAFE_STATEMENT_VALID,
  75. PRAGMA_UNSAFE_STATEMENT_INVALID
  76. }PRAGMA_STATEMENT;
  77. BOOL PragmaUnsafe_OnPragma(char * str, PRAGMA_STATEMENT & ePragmaUnsafe);
  78. VOID PragmaUnsafe_GetUsafeOperParameters(DWORD dwFlag, const string & strPragmaUnsafeSingleStatement, string & strFuncNameList);
  79. BOOL PragmaUnsafe_OnFileStart();
  80. BOOL PragmaUnsafe_OnFileEnd();
  81. BOOL PragmaUnsafe_OnPragma(char * str, PRAGMA_STATEMENT & ePragmaUnsafe);
  82. BOOL PragmaUnsafe_IsPointerArithmaticEnabled();