test_libos.py 17 KB

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