00_Bootstrap.py 6.9 KB

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