test_libos.py 17 KB

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