test_libos.py 19 KB

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