test_libos.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. #!/usr/bin/env python3
  2. import mmap
  3. import os
  4. import sys
  5. import unittest
  6. import subprocess
  7. from regression import (
  8. HAS_SGX,
  9. RegressionTestCase,
  10. expectedFailureIf,
  11. )
  12. class TC_00_Bootstrap(RegressionTestCase):
  13. def test_100_basic_bootstrapping(self):
  14. stdout, stderr = self.run_binary(['bootstrap'])
  15. # Basic Bootstrapping
  16. self.assertIn('User Program Started', stdout)
  17. # One Argument Given
  18. self.assertIn('# of Arguments: 1', stdout)
  19. self.assertIn('argv[0] = bootstrap', stdout)
  20. def test_101_basic_bootstrapping_five_arguments(self):
  21. # Five Arguments Given
  22. stdout, stderr = self.run_binary(['bootstrap', 'a', 'b', 'c', 'd'])
  23. self.assertIn('# of Arguments: 5', stdout)
  24. self.assertIn('argv[0] = bootstrap', stdout)
  25. self.assertIn('argv[1] = a', stdout)
  26. self.assertIn('argv[2] = b', stdout)
  27. self.assertIn('argv[3] = c', stdout)
  28. self.assertIn('argv[4] = d', stdout)
  29. @unittest.skipUnless(HAS_SGX,
  30. 'This test is only meaningful on SGX PAL because only SGX catches raw '
  31. 'syscalls and redirects to Graphene\'s LibOS. If we will add seccomp to '
  32. 'Linux PAL, then we should allow this test on Linux PAL as well.')
  33. def test_102_basic_bootstrapping_static(self):
  34. # bootstrap_static
  35. stdout, stderr = self.run_binary(['bootstrap_static'])
  36. self.assertIn('Hello world (bootstrap_static)!', stdout)
  37. def test_103_basic_bootstrapping_pie(self):
  38. # bootstrap_pie
  39. stdout, stderr = self.run_binary(['bootstrap_pie'])
  40. self.assertIn('User program started', stdout)
  41. self.assertIn('Local Address in Executable: 0x', stdout)
  42. self.assertIn('argv[0] = bootstrap_pie', stdout)
  43. def test_110_basic_bootstrapping_cxx(self):
  44. stdout, stderr = self.run_binary(['bootstrap-c++'])
  45. # Basic Bootstrapping (C++)
  46. self.assertIn('User Program Started', stdout)
  47. def test_200_exec(self):
  48. stdout, stderr = self.run_binary(['exec'])
  49. # 2 page child binary
  50. self.assertIn(
  51. '00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 '
  52. '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 '
  53. '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 '
  54. '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 '
  55. '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 '
  56. '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 '
  57. '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 '
  58. '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 '
  59. '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 '
  60. '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 '
  61. '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 '
  62. '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 '
  63. '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 '
  64. '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 '
  65. '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 '
  66. '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 ',
  67. stdout)
  68. def test_201_fork_and_exec(self):
  69. stdout, stderr = self.run_binary(['fork_and_exec'])
  70. # fork and exec 2 page child binary
  71. self.assertIn('child exited with status: 0', stdout)
  72. self.assertIn('test completed successfully', stdout)
  73. def test_202_vfork_and_exec(self):
  74. stdout, stderr = self.run_binary(['vfork_and_exec'])
  75. # vfork and exec 2 page child binary
  76. self.assertIn('child exited with status: 0', stdout)
  77. self.assertIn('test completed successfully', stdout)
  78. def test_210_exec_invalid_args(self):
  79. stdout, stderr = self.run_binary(['exec_invalid_args'])
  80. # Execve with invalid pointers in arguments
  81. self.assertIn(
  82. 'execve(invalid-path) correctly returned error', stdout)
  83. self.assertIn(
  84. 'execve(invalid-argv-ptr) correctly returned error', stdout)
  85. self.assertIn(
  86. 'execve(invalid-envp-ptr) correctly returned error', stdout)
  87. self.assertIn(
  88. 'execve(invalid-argv) correctly returned error', stdout)
  89. self.assertIn(
  90. 'execve(invalid-envp) correctly returned error', stdout)
  91. def test_300_shared_object(self):
  92. stdout, stderr = self.run_binary(['shared_object'])
  93. # Shared Object
  94. self.assertIn('Hello world', stdout)
  95. def test_400_exit(self):
  96. with self.expect_returncode(113):
  97. self.run_binary(['exit'])
  98. @unittest.skipIf(HAS_SGX,
  99. 'Exposes a rare memory corruption on SGX PAL. Disable for now.')
  100. def test_401_exit_group(self):
  101. try:
  102. self.run_binary(['exit_group'])
  103. except subprocess.CalledProcessError as e:
  104. self.assertTrue(1 <= e.returncode and e.returncode <= 4)
  105. def test_402_signalexit(self):
  106. with self.expect_returncode(134):
  107. self.run_binary(['abort'])
  108. def test_403_signalexit_multithread(self):
  109. with self.expect_returncode(134):
  110. self.run_binary(['abort_multithread'])
  111. def test_500_init_fail(self):
  112. try:
  113. self.run_binary(['init_fail'])
  114. self.fail('expected to return nonzero (and != 42)')
  115. except subprocess.CalledProcessError as e:
  116. self.assertNotEqual(e.returncode, 42, 'expected returncode != 42')
  117. def test_600_multi_pthread(self):
  118. stdout, stderr = self.run_binary(['multi_pthread'])
  119. # Multiple thread creation
  120. self.assertIn('128 Threads Created', stdout)
  121. @unittest.skipUnless(HAS_SGX,
  122. 'This test is only meaningful on SGX PAL because only SGX catches raw '
  123. 'syscalls and redirects to Graphene\'s LibOS. If we will add seccomp to '
  124. 'Linux PAL, then we should allow this test on Linux PAL as well.')
  125. class TC_01_OpenMP(RegressionTestCase):
  126. def test_000_simple_for_loop(self):
  127. stdout, stderr = self.run_binary(['openmp'])
  128. # OpenMP simple for loop
  129. self.assertIn('first: 0, last: 9', stdout)
  130. @unittest.skipUnless(HAS_SGX,
  131. 'This test is only meaningful on SGX PAL because file-check-policy is '
  132. 'only relevant to SGX.')
  133. class TC_02_FileCheckPolicy(RegressionTestCase):
  134. def test_000_strict_success(self):
  135. manifest = self.get_manifest('file_check_policy_strict')
  136. stdout, stderr = self.run_binary([manifest, 'trusted_testfile'])
  137. self.assertIn('file_check_policy succeeded', stdout)
  138. def test_001_strict_fail(self):
  139. manifest = self.get_manifest('file_check_policy_strict')
  140. try:
  141. stdout, stderr = self.run_binary([manifest, 'unknown_testfile'])
  142. self.fail('expected to return nonzero')
  143. except subprocess.CalledProcessError as e:
  144. self.assertEqual(e.returncode, 2, 'expected returncode == 2')
  145. def test_002_allow_all_but_log_success(self):
  146. manifest = self.get_manifest('file_check_policy_allow_all_but_log')
  147. stdout, stderr = self.run_binary([manifest, 'unknown_testfile'])
  148. self.assertIn('Allowing access to an unknown file due to file_check_policy settings: file:unknown_testfile', stderr)
  149. self.assertIn('file_check_policy succeeded', stdout)
  150. def test_003_allow_all_but_log_fail(self):
  151. manifest = self.get_manifest('file_check_policy_allow_all_but_log')
  152. stdout, stderr = self.run_binary([manifest, 'trusted_testfile'])
  153. self.assertNotIn('Allowing access to an unknown file due to file_check_policy settings: file:trusted_testfile', stderr)
  154. self.assertIn('file_check_policy succeeded', stdout)
  155. class TC_30_Syscall(RegressionTestCase):
  156. def test_000_getcwd(self):
  157. stdout, stderr = self.run_binary(['getcwd'])
  158. # Getcwd syscall
  159. self.assertIn('[bss_cwd_buf] getcwd succeeded: /', stdout)
  160. self.assertIn('[mmapped_cwd_buf] getcwd succeeded: /', stdout)
  161. def test_010_stat_invalid_args(self):
  162. stdout, stderr = self.run_binary(['stat_invalid_args'])
  163. # Stat with invalid arguments
  164. self.assertIn('stat(invalid-path-ptr) correctly returned error', stdout)
  165. self.assertIn('stat(invalid-buf-ptr) correctly returned error', stdout)
  166. self.assertIn('lstat(invalid-path-ptr) correctly returned error', stdout)
  167. self.assertIn('lstat(invalid-buf-ptr) correctly returned error', stdout)
  168. def test_011_fstat_cwd(self):
  169. stdout, stderr = self.run_binary(['fstat_cwd'])
  170. # fstat on a directory
  171. self.assertIn('fstat returned the fd type as S_IFDIR', stdout)
  172. def test_020_getdents(self):
  173. # This doesn't catch extraneous entries, but should be fine
  174. # until the LTP test can be run (need symlink support)
  175. stdout, stderr = self.run_binary(['getdents'])
  176. self.assertIn('getdents: setup ok', stdout)
  177. # Directory listing (32-bit)
  178. self.assertIn('getdents32: . [0x4]', stdout)
  179. self.assertIn('getdents32: .. [0x4]', stdout)
  180. self.assertIn('getdents32: file1 [0x8]', stdout)
  181. self.assertIn('getdents32: file2 [0x8]', stdout)
  182. self.assertIn('getdents32: dir3 [0x4]', stdout)
  183. # Directory listing (64-bit)
  184. self.assertIn('getdents64: . [0x4]', stdout)
  185. self.assertIn('getdents64: .. [0x4]', stdout)
  186. self.assertIn('getdents64: file1 [0x8]', stdout)
  187. self.assertIn('getdents64: file2 [0x8]', stdout)
  188. self.assertIn('getdents64: dir3 [0x4]', stdout)
  189. def test_021_getdents_large_dir(self):
  190. stdout, stderr = self.run_binary(['large_dir_read', 'tmp/large_dir', '3000'])
  191. self.assertIn('Success!', stdout)
  192. def test_030_fopen(self):
  193. if os.path.exists("tmp/filecreatedbygraphene"):
  194. os.remove("tmp/filecreatedbygraphene")
  195. stdout, stderr = self.run_binary(['fopen_cornercases'])
  196. # fopen corner cases
  197. self.assertIn('Successfully read from file: Hello World', stdout)
  198. def test_040_futex_wake(self):
  199. stdout, stderr = self.run_binary(['futex'])
  200. # Futex Wake Test
  201. self.assertIn('Woke all kiddos', stdout)
  202. def test_041_futex_timeout(self):
  203. stdout, stderr = self.run_binary(['futex-timeout'])
  204. # Futex Timeout Test
  205. self.assertIn('futex correctly timed out', stdout)
  206. def test_050_mmap(self):
  207. stdout, stderr = self.run_binary(['mmap-file'], timeout=60)
  208. # Private mmap beyond file range
  209. self.assertIn('mmap test 6 passed', stdout)
  210. self.assertIn('mmap test 7 passed', stdout)
  211. # Private mmap beyond file range (after fork)
  212. self.assertIn('mmap test 1 passed', stdout)
  213. self.assertIn('mmap test 2 passed', stdout)
  214. self.assertIn('mmap test 3 passed', stdout)
  215. self.assertIn('mmap test 4 passed', stdout)
  216. @unittest.skipIf(HAS_SGX,
  217. 'On SGX, SIGBUS isn\'t always implemented correctly, for lack '
  218. 'of memory protection. For now, some of these cases won\'t work.')
  219. def test_051_mmap_sgx(self):
  220. stdout, stderr = self.run_binary(['mmap-file'], timeout=60)
  221. # SIGBUS test
  222. self.assertIn('mmap test 5 passed', stdout)
  223. self.assertIn('mmap test 8 passed', stdout)
  224. def test_52_large_mmap(self):
  225. stdout, stderr = self.run_binary(['large-mmap'], timeout=480)
  226. # Ftruncate
  227. self.assertIn('large-mmap: ftruncate OK', stdout)
  228. # Large mmap
  229. self.assertIn('large-mmap: mmap 1 completed OK', stdout)
  230. self.assertIn('large-mmap: mmap 2 completed OK', stdout)
  231. @unittest.skip('sigaltstack isn\'t correctly implemented')
  232. def test_060_sigaltstack(self):
  233. stdout, stderr = self.run_binary(['sigaltstack'])
  234. # Sigaltstack Test
  235. self.assertIn('OK on sigaltstack in main thread before alarm', stdout)
  236. self.assertIn('&act == 0x', stdout)
  237. self.assertIn('sig 14 count 1 goes off with sp=0x', stdout)
  238. self.assertIn('OK on signal stack', stdout)
  239. self.assertIn('OK on sigaltstack in handler', stdout)
  240. self.assertIn('sig 14 count 2 goes off with sp=0x', stdout)
  241. self.assertIn('OK on signal stack', stdout)
  242. self.assertIn('OK on sigaltstack in handler', stdout)
  243. self.assertIn('sig 14 count 3 goes off with sp=0x', stdout)
  244. self.assertIn('OK on signal stack', stdout)
  245. self.assertIn('OK on sigaltstack in handler', stdout)
  246. self.assertIn('OK on sigaltstack in main thread', stdout)
  247. self.assertIn('done exiting', stdout)
  248. @unittest.skipUnless(HAS_SGX,
  249. 'This test is only meaningful on SGX PAL because only SGX catches raw '
  250. 'syscalls and redirects to Graphene\'s LibOS. If we will add seccomp to '
  251. 'Linux PAL, then we should allow this test on Linux PAL as well.')
  252. class TC_31_SyscallSGX(RegressionTestCase):
  253. def test_000_syscall_redirect(self):
  254. stdout, stderr = self.run_binary(['syscall'])
  255. # Syscall Instruction Redirection
  256. self.assertIn('Hello world', stdout)
  257. class TC_40_FileSystem(RegressionTestCase):
  258. def test_000_base(self):
  259. stdout, stderr = self.run_binary(['proc'])
  260. # Base /proc files present
  261. self.assertIn('/proc/1/..', stdout)
  262. self.assertIn('/proc/1/cwd', stdout)
  263. self.assertIn('/proc/1/exe', stdout)
  264. self.assertIn('/proc/1/root', stdout)
  265. self.assertIn('/proc/1/fd', stdout)
  266. self.assertIn('/proc/1/maps', stdout)
  267. self.assertIn('/proc/.', stdout)
  268. self.assertIn('/proc/1', stdout)
  269. self.assertIn('/proc/self', stdout)
  270. self.assertIn('/proc/meminfo', stdout)
  271. self.assertIn('/proc/cpuinfo', stdout)
  272. def test_010_path(self):
  273. stdout, stderr = self.run_binary(['proc-path'])
  274. # Base /proc path present
  275. self.assertIn('proc path test success', stdout)
  276. def test_020_cpuinfo(self):
  277. stdout, stderr = self.run_binary(['proc_cpuinfo'], timeout=50)
  278. # proc/cpuinfo Linux-based formatting
  279. self.assertIn('cpuinfo test passed', stdout)
  280. def test_030_fdleak(self):
  281. stdout, stderr = self.run_binary(['fdleak'], timeout=10)
  282. self.assertIn("Test succeeded.", stdout)
  283. class TC_80_Socket(RegressionTestCase):
  284. def test_000_getsockopt(self):
  285. stdout, stderr = self.run_binary(['getsockopt'])
  286. self.assertIn('getsockopt: Got socket type OK', stdout)
  287. def test_010_epoll_wait_timeout(self):
  288. stdout, stderr = self.run_binary(['epoll_wait_timeout', '8000'],
  289. timeout=50)
  290. # epoll_wait timeout
  291. self.assertIn('epoll_wait test passed', stdout)
  292. def test_100_socket_unix(self):
  293. stdout, stderr = self.run_binary(['unix'])
  294. self.assertIn('Data: This is packet 0', stdout)
  295. self.assertIn('Data: This is packet 1', stdout)
  296. self.assertIn('Data: This is packet 2', stdout)
  297. self.assertIn('Data: This is packet 3', stdout)
  298. self.assertIn('Data: This is packet 4', stdout)
  299. self.assertIn('Data: This is packet 5', stdout)
  300. self.assertIn('Data: This is packet 6', stdout)
  301. self.assertIn('Data: This is packet 7', stdout)
  302. self.assertIn('Data: This is packet 8', stdout)
  303. self.assertIn('Data: This is packet 9', stdout)
  304. def test_200_socket_udp(self):
  305. stdout, stderr = self.run_binary(['udp'], timeout=50)
  306. self.assertIn('Data: This is packet 0', stdout)
  307. self.assertIn('Data: This is packet 1', stdout)
  308. self.assertIn('Data: This is packet 2', stdout)
  309. self.assertIn('Data: This is packet 3', stdout)
  310. self.assertIn('Data: This is packet 4', stdout)
  311. self.assertIn('Data: This is packet 5', stdout)
  312. self.assertIn('Data: This is packet 6', stdout)
  313. self.assertIn('Data: This is packet 7', stdout)
  314. self.assertIn('Data: This is packet 8', stdout)
  315. self.assertIn('Data: This is packet 9', stdout)