00_Bootstrap.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import os, sys, mmap
  2. from regression import Regression
  3. loader = os.environ['PAL_LOADER']
  4. try:
  5. sgx = os.environ['SGX_RUN']
  6. except KeyError:
  7. sgx = False
  8. def manifest_file(file):
  9. if 'SGX_RUN' in os.environ and os.environ['SGX_RUN'] == '1':
  10. return file + '.manifest.sgx'
  11. else:
  12. return file + '.manifest'
  13. # Running Bootstrap
  14. regression = Regression(loader, "Bootstrap")
  15. regression.add_check(name="Basic Bootstrapping",
  16. check=lambda res: "User Program Started" in res[0].log)
  17. regression.add_check(name="Control Block: Executable Name",
  18. check=lambda res: "Loaded Executable: file:Bootstrap" in res[0].log)
  19. regression.add_check(name="One Argument Given",
  20. check=lambda res: "# of Arguments: 1" in res[0].log and \
  21. "argv[0] = file:Bootstrap" in res[0].log)
  22. regression.add_check(name="Five Arguments Given",
  23. args = ['a', 'b', 'c', 'd'],
  24. check=lambda res: "# of Arguments: 5" in res[0].log and \
  25. "argv[1] = a" in res[0].log and "argv[2] = b" in res[0].log and \
  26. "argv[3] = c" in res[0].log and "argv[4] = d" in res[0].log)
  27. regression.add_check(name="Control Block: Debug Stream (Inline)",
  28. check=lambda res: "Written to Debug Stream" in res[0].out)
  29. regression.add_check(name="Control Block: Page Size",
  30. check=lambda res: ("Page Size: %d" % (mmap.PAGESIZE)) in res[0].log)
  31. regression.add_check(name="Control Block: Allocation Alignment",
  32. check=lambda res: ("Allocation Alignment: %d" % (mmap.ALLOCATIONGRANULARITY)) in res[0].log)
  33. regression.add_check(name="Control Block: Executable Range",
  34. check=lambda res: "Executable Range OK" in res[0].log)
  35. def check_cpu_info(res):
  36. cpu_num = cpu_model = cpu_family = cpu_stepping = 0
  37. cpu_vendor = cpu_brand = cpu_flags = None
  38. f = open("/proc/cpuinfo", "r")
  39. for line in f:
  40. line = line.strip()
  41. pos = line.find(":")
  42. if pos == -1:
  43. continue
  44. key = line[:pos].strip()
  45. val = line[pos+1:].strip()
  46. if key == "processor": cpu_num += 1
  47. if key == "vendor_id": cpu_vendor = val
  48. if key == "cpu family": cpu_family = int(val)
  49. if key == "model": cpu_model = int(val)
  50. if key == "model name": cpu_brand = val
  51. if key == "stepping": cpu_stepping = int(val)
  52. if key == "flags":
  53. cpu_flags = []
  54. for flag in val.split(" "):
  55. if flag in ["fpu", "vme", "de", "pse", "tsc", "msr", "pae",
  56. "mce", "cx8", "apic", "sep", "mtrr", "pge", "mca",
  57. "cmov", "pat", "pse36", "pn", "clflush", "dts", "acpi",
  58. "mmx", "fxsr", "sse", "sse2", "ss", "ht", "tm",
  59. "ia64", "pbe"]:
  60. cpu_flags.append(flag)
  61. cpu_flags = " ".join(cpu_flags)
  62. return ("CPU num: %d" % cpu_num) in res[0].log and \
  63. ("CPU vendor: %s" % cpu_vendor) in res[0].log and \
  64. ("CPU brand: %s" % cpu_brand) in res[0].log and \
  65. ("CPU family: %d" % cpu_family) in res[0].log and \
  66. ("CPU model: %d" % cpu_model) in res[0].log and \
  67. ("CPU stepping: %d" % cpu_stepping) in res[0].log and \
  68. ("CPU flags: %s" % cpu_flags) in res[0].log
  69. regression.add_check(name="Control Block: CPU Info",
  70. check=check_cpu_info)
  71. rv = regression.run_checks()
  72. if rv: sys.exit(rv)
  73. # Running ..Bootstrap
  74. regression = Regression(loader, "..Bootstrap")
  75. regression.add_check(name="Dotdot handled properly",
  76. check=lambda res: "User Program Started" in res[0].log)
  77. rv = regression.run_checks()
  78. if rv: sys.exit(rv)
  79. # Running Bootstrap2
  80. regression = Regression(loader, manifest_file("Bootstrap2"))
  81. regression.add_check(name="Control Block: Manifest as Executable Name",
  82. check=lambda res: "Loaded Manifest: file:" + manifest_file("Bootstrap2") in res[0].log
  83. and "User Program Started" in res[0].log)
  84. rv = regression.run_checks()
  85. if rv: sys.exit(rv)
  86. # Running Bootstrap3
  87. regression = Regression(loader, "Bootstrap3")
  88. regression.add_check(name="Preload Libraries",
  89. check=lambda res: "Binary 1 Preloaded" in res[0].log and
  90. "Binary 2 Preloaded" in res[0].log)
  91. regression.add_check(name="Preload Libraries Linking",
  92. check=lambda res: "Preloaded Function 1 Called" in res[0].log and
  93. "Preloaded Function 2 Called" in res[0].log)
  94. rv = regression.run_checks()
  95. if rv: sys.exit(rv)
  96. # Running Bootstrap4
  97. regression = Regression(loader, manifest_file("Bootstrap4"))
  98. regression.add_check(name="Control Block: Manifest as Argument",
  99. check=lambda res: any([line.startswith("Loaded Manifest: file:" + manifest_file("Bootstrap4")) for line in res[0].log]))
  100. regression.add_check(name="Control Block: Executable as in Manifest",
  101. check=lambda res: "Loaded Executable: file:Bootstrap" in res[0].log)
  102. rv = regression.run_checks()
  103. if rv: sys.exit(rv)
  104. # Running Bootstrap4.manifest
  105. regression = Regression(executable = "./" + manifest_file("Bootstrap4"))
  106. regression.add_check(name="Control Block: Manifest as Argument (Load by Shebang)",
  107. check=lambda res: "Loaded Manifest: file:" + manifest_file("Bootstrap4") in res[0].log)
  108. regression.add_check(name="Control Block: Executable as in Manifest (Load by Shebang)",
  109. check=lambda res: "Loaded Executable: file:Bootstrap" in res[0].log)
  110. regression.add_check(name="Arguments: loader.execname in Manifest",
  111. check=lambda res: "argv[0] = Bootstrap" in res[0].log)
  112. rv = regression.run_checks()
  113. if rv: sys.exit(rv)
  114. # Running Bootstrap5.manifest
  115. regression = Regression(loader, manifest_file("Bootstrap5"))
  116. regression.add_check(name="Bootstrap without Executable but Preload Libraries",
  117. check=lambda res: "Binary 1 Preloaded" in res[0].log and
  118. "Binary 2 Preloaded" in res[0].log)
  119. rv = regression.run_checks()
  120. if rv: sys.exit(rv)
  121. # Running Bootstrap6.manifest - SGX-specific test
  122. if sgx:
  123. regression = Regression(loader, manifest_file("Bootstrap6"), timeout = 100000)
  124. regression.add_check(name="8GB Enclave Creation (SGX Only)",
  125. check=lambda res: "Loaded Manifest: file:Bootstrap6.manifest.sgx" in res[0].log and
  126. "Executable Range OK" in res[0].log)
  127. rv = regression.run_checks()
  128. if rv: sys.exit(rv)
  129. # Running Bootstrap7.manifest
  130. regression = Regression(loader, manifest_file("Bootstrap7"))
  131. regression.add_check(name="Load Large Number of Items in Manifest",
  132. check=lambda res: "key1000=na" in res[0].log and
  133. "key1=na" in res[0].log)
  134. rv = regression.run_checks()
  135. if rv: sys.exit(rv)
  136. # Running an executable that doesn't exist, should at least warn you
  137. regression = Regression(loader, "fakenews")
  138. regression.add_check(name="Error on missing executable and manifest",
  139. check=lambda res: "Executable not found" in res[0].log and
  140. any([line.startswith("USAGE: ") for line in res[0].log]))
  141. rv = regression.run_checks()
  142. if rv: sys.exit(rv)