00_Bootstrap.py 6.8 KB

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