1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
base / process / process_util_unittest.cc [blame]
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#define _CRT_SECURE_NO_WARNINGS
#include <stddef.h>
#include <stdint.h>
#include <limits>
#include <string_view>
#include <tuple>
#include "base/command_line.h"
#include "base/compiler_specific.h"
#include "base/debug/alias.h"
#include "base/debug/stack_trace.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_file.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/notreached.h"
#include "base/path_service.h"
#include "base/posix/eintr_wrapper.h"
#include "base/process/kill.h"
#include "base/process/launch.h"
#include "base/process/memory.h"
#include "base/process/process.h"
#include "base/process/process_metrics.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/synchronization/waitable_event.h"
#include "base/test/multiprocess_test.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "base/test/test_timeouts.h"
#include "base/threading/platform_thread.h"
#include "base/threading/simple_thread.h"
#include "base/threading/thread.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/multiprocess_func_list.h"
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
#include <malloc.h>
#include <sched.h>
#include <sys/syscall.h>
#endif
#if BUILDFLAG(IS_POSIX)
#include <sys/resource.h>
#endif
#if BUILDFLAG(IS_POSIX)
#include <dlfcn.h>
#include <errno.h>
#include <sched.h>
#include <signal.h>
#include <sys/wait.h>
#include <unistd.h>
#endif
#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/types.h>
#endif
#if BUILDFLAG(IS_WIN)
#include <windows.h>
#endif
#if BUILDFLAG(IS_APPLE)
#include <mach/vm_param.h>
#include <malloc/malloc.h>
#endif
#if BUILDFLAG(IS_ANDROID)
#include "third_party/lss/linux_syscall_support.h"
#endif
#if BUILDFLAG(IS_FUCHSIA)
#include <lib/fdio/fdio.h>
#include <lib/fdio/limits.h>
#include <lib/fdio/spawn.h>
#include <lib/sys/cpp/component_context.h>
#include <zircon/process.h>
#include <zircon/processargs.h>
#include <zircon/syscalls.h>
#include "base/files/scoped_temp_dir.h"
#include "base/fuchsia/file_utils.h"
#include "base/fuchsia/filtered_service_directory.h"
#include "base/fuchsia/fuchsia_logging.h"
#include "base/fuchsia/process_context.h"
#include "base/test/bind.h"
#endif
#if BUILDFLAG(IS_MAC)
#include <mach/mach.h>
#include "base/apple/mach_port_rendezvous.h"
#include "base/apple/scoped_mach_port.h"
#include "base/mac/process_requirement.h"
#endif
namespace base {
namespace {
const char kSignalFileSlow[] = "SlowChildProcess.die";
const char kSignalFileKill[] = "KilledChildProcess.die";
const char kTestHelper[] = "test_child_process";
#if BUILDFLAG(IS_POSIX)
const char kSignalFileTerm[] = "TerminatedChildProcess.die";
#endif
#if BUILDFLAG(IS_FUCHSIA)
const char kSignalFileClone[] = "ClonedDir.die";
const char kFooDirHasStaged[] = "FooDirHasStaged.die";
#endif
#if BUILDFLAG(IS_WIN)
const int kExpectedStillRunningExitCode = 0x102;
const int kExpectedKilledExitCode = 1;
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
const int kExpectedStillRunningExitCode = 0;
#endif
// Sleeps until file filename is created.
void WaitToDie(const char* filename) {
FILE* fp;
do {
PlatformThread::Sleep(Milliseconds(10));
fp = fopen(filename, "r");
} while (!fp);
fclose(fp);
}
// Signals children they should die now.
void SignalChildren(const char* filename) {
FILE* fp = fopen(filename, "w");
fclose(fp);
}
// Using a pipe to the child to wait for an event was considered, but
// there were cases in the past where pipes caused problems (other
// libraries closing the fds, child deadlocking). This is a simple
// case, so it's not worth the risk. Using wait loops is discouraged
// in most instances.
TerminationStatus WaitForChildTermination(ProcessHandle handle,
int* exit_code) {
// Now we wait until the result is something other than STILL_RUNNING.
TerminationStatus status = TERMINATION_STATUS_STILL_RUNNING;
const TimeDelta kInterval = Milliseconds(20);
TimeDelta waited;
do {
status = GetTerminationStatus(handle, exit_code);
PlatformThread::Sleep(kInterval);
waited += kInterval;
} while (status == TERMINATION_STATUS_STILL_RUNNING &&
waited < TestTimeouts::action_max_timeout());
return status;
}
} // namespace
const int kSuccess = 0;
class ProcessUtilTest : public MultiProcessTest {
public:
void SetUp() override {
ASSERT_TRUE(PathService::Get(DIR_OUT_TEST_DATA_ROOT, &test_helper_path_));
test_helper_path_ = test_helper_path_.AppendASCII(kTestHelper);
}
#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
// Spawn a child process that counts how many file descriptors are open.
int CountOpenFDsInChild();
#endif
// Converts the filename to a platform specific filepath.
// On Android files can not be created in arbitrary directories.
static std::string GetSignalFilePath(const char* filename);
protected:
FilePath test_helper_path_;
};
std::string ProcessUtilTest::GetSignalFilePath(const char* filename) {
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_FUCHSIA)
FilePath tmp_dir;
PathService::Get(DIR_TEMP, &tmp_dir);
// Ensure the directory exists to avoid harder to debug issues later.
CHECK(PathExists(tmp_dir));
tmp_dir = tmp_dir.Append(filename);
return tmp_dir.value();
#else
return filename;
#endif
}
MULTIPROCESS_TEST_MAIN(SimpleChildProcess) {
return kSuccess;
}
// TODO(viettrungluu): This should be in a "MultiProcessTestTest".
TEST_F(ProcessUtilTest, SpawnChild) {
Process process = SpawnChild("SimpleChildProcess");
ASSERT_TRUE(process.IsValid());
int exit_code;
EXPECT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_max_timeout(),
&exit_code));
}
MULTIPROCESS_TEST_MAIN(SlowChildProcess) {
WaitToDie(ProcessUtilTest::GetSignalFilePath(kSignalFileSlow).c_str());
return kSuccess;
}
TEST_F(ProcessUtilTest, KillSlowChild) {
const std::string signal_file = GetSignalFilePath(kSignalFileSlow);
remove(signal_file.c_str());
Process process = SpawnChild("SlowChildProcess");
ASSERT_TRUE(process.IsValid());
SignalChildren(signal_file.c_str());
int exit_code;
EXPECT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_max_timeout(),
&exit_code));
remove(signal_file.c_str());
}
// Times out on Linux and Win, flakes on other platforms, http://crbug.com/95058
#if BUILDFLAG(IS_FUCHSIA)
#define MAYBE_GetTerminationStatusExit GetTerminationStatusExit
#else
#define MAYBE_GetTerminationStatusExit DISABLED_GetTerminationStatusExit
#endif
TEST_F(ProcessUtilTest, MAYBE_GetTerminationStatusExit) {
const std::string signal_file = GetSignalFilePath(kSignalFileSlow);
remove(signal_file.c_str());
Process process = SpawnChild("SlowChildProcess");
ASSERT_TRUE(process.IsValid());
int exit_code = 42;
EXPECT_EQ(TERMINATION_STATUS_STILL_RUNNING,
GetTerminationStatus(process.Handle(), &exit_code));
EXPECT_EQ(kExpectedStillRunningExitCode, exit_code);
SignalChildren(signal_file.c_str());
exit_code = 42;
TerminationStatus status =
WaitForChildTermination(process.Handle(), &exit_code);
EXPECT_EQ(TERMINATION_STATUS_NORMAL_TERMINATION, status);
EXPECT_EQ(kSuccess, exit_code);
remove(signal_file.c_str());
}
#if BUILDFLAG(IS_FUCHSIA)
MULTIPROCESS_TEST_MAIN(ShouldNotBeLaunched) {
return 1;
}
// Test that duplicate transfer & cloned paths cause the launch to fail.
// TODO(fxbug.dev/124840): Re-enable once the platform behaviour is fixed.
TEST_F(ProcessUtilTest, DISABLED_DuplicateTransferAndClonePaths_Fail) {
// Create a tempdir to transfer a duplicate "/data".
ScopedTempDir tmpdir;
ASSERT_TRUE(tmpdir.CreateUniqueTempDir());
LaunchOptions options;
options.spawn_flags = FDIO_SPAWN_CLONE_STDIO;
// Attach the tempdir to "data", but also try to duplicate the existing "data"
// directory.
options.paths_to_clone.push_back(FilePath(kPersistedDataDirectoryPath));
options.paths_to_clone.push_back(FilePath("/tmp"));
options.paths_to_transfer.push_back(
{FilePath(kPersistedDataDirectoryPath),
OpenDirectoryHandle(tmpdir.GetPath()).TakeChannel().release()});
// Verify that the process fails to launch.
Process process(SpawnChildWithOptions("ShouldNotBeLaunched", options));
ASSERT_FALSE(process.IsValid());
}
// Test that attempting to transfer/clone to a path (e.g. "/data"), and also to
// a sub-path of that path (e.g. "/data/staged"), causes the process launch to
// fail.
// TODO(fxbug.dev/124840): Re-enable once the platform behaviour is fixed.
TEST_F(ProcessUtilTest, DISABLED_OverlappingPaths_Fail) {
// Create a tempdir to transfer to a sub-directory path.
ScopedTempDir tmpdir;
ASSERT_TRUE(tmpdir.CreateUniqueTempDir());
LaunchOptions options;
options.spawn_flags = FDIO_SPAWN_CLONE_STDIO;
// Attach the tempdir to "data", but also try to duplicate the existing "data"
// directory.
options.paths_to_clone.push_back(FilePath(kPersistedDataDirectoryPath));
options.paths_to_clone.push_back(FilePath("/tmp"));
options.paths_to_transfer.push_back(
{FilePath(kPersistedDataDirectoryPath).Append("staged"),
OpenDirectoryHandle(tmpdir.GetPath()).TakeChannel().release()});
// Verify that the process fails to launch.
Process process(SpawnChildWithOptions("ShouldNotBeLaunched", options));
ASSERT_FALSE(process.IsValid());
}
MULTIPROCESS_TEST_MAIN(CheckMountedDir) {
if (!PathExists(FilePath("/foo/staged"))) {
return 1;
}
WaitToDie(ProcessUtilTest::GetSignalFilePath(kFooDirHasStaged).c_str());
return kSuccess;
}
// Test that we can install an opaque handle in the child process' namespace.
TEST_F(ProcessUtilTest, TransferHandleToPath) {
const std::string signal_file = GetSignalFilePath(kFooDirHasStaged);
remove(signal_file.c_str());
// Create a tempdir with "staged" as its contents.
ScopedTempDir new_tmpdir;
ASSERT_TRUE(new_tmpdir.CreateUniqueTempDir());
FilePath staged_file_path = new_tmpdir.GetPath().Append("staged");
File staged_file(staged_file_path, File::FLAG_CREATE | File::FLAG_WRITE);
ASSERT_TRUE(staged_file.created());
staged_file.Close();
// Mount the tempdir to "/foo".
zx::channel tmp_channel =
OpenDirectoryHandle(new_tmpdir.GetPath()).TakeChannel();
ASSERT_TRUE(tmp_channel.is_valid());
LaunchOptions options;
options.paths_to_clone.push_back(FilePath("/tmp"));
options.paths_to_transfer.push_back(
{FilePath("/foo"), tmp_channel.release()});
options.spawn_flags = FDIO_SPAWN_CLONE_STDIO;
// Verify from that "/foo/staged" exists from the child process' perspective.
Process process(SpawnChildWithOptions("CheckMountedDir", options));
ASSERT_TRUE(process.IsValid());
SignalChildren(signal_file.c_str());
int exit_code = 42;
EXPECT_TRUE(process.WaitForExit(&exit_code));
EXPECT_EQ(kSuccess, exit_code);
}
// Look through the filesystem to ensure that no other directories
// besides |expected_path| are in the namespace.
// Since GetSignalFilePath() uses "/tmp", tests for paths other than this must
// include two paths. "/tmp" must always be last.
int CheckOnlyOnePathExists(std::string_view expected_path) {
bool is_expected_path_tmp = expected_path == "/tmp";
std::vector<FilePath> paths;
FileEnumerator enumerator(
FilePath("/"), false,
FileEnumerator::FILES | FileEnumerator::DIRECTORIES);
for (auto path = enumerator.Next(); !path.empty(); path = enumerator.Next()) {
paths.push_back(std::move(path));
}
EXPECT_EQ(paths.size(), is_expected_path_tmp ? 1u : 2u);
EXPECT_EQ(paths[0], FilePath(expected_path))
<< "Clone policy violation: found non-tmp directory "
<< paths[0].MaybeAsASCII();
if (!is_expected_path_tmp) {
EXPECT_EQ(paths[1], FilePath("/tmp"));
}
WaitToDie(ProcessUtilTest::GetSignalFilePath(kSignalFileClone).c_str());
return kSuccess;
}
MULTIPROCESS_TEST_MAIN(CheckOnlyTmpExists) {
return CheckOnlyOnePathExists("/tmp");
}
TEST_F(ProcessUtilTest, CloneTmp) {
const std::string signal_file = GetSignalFilePath(kSignalFileClone);
remove(signal_file.c_str());
LaunchOptions options;
options.paths_to_clone.push_back(FilePath("/tmp"));
options.spawn_flags = FDIO_SPAWN_CLONE_STDIO;
Process process(SpawnChildWithOptions("CheckOnlyTmpExists", options));
ASSERT_TRUE(process.IsValid());
SignalChildren(signal_file.c_str());
int exit_code = 42;
EXPECT_TRUE(process.WaitForExit(&exit_code));
EXPECT_EQ(kSuccess, exit_code);
}
MULTIPROCESS_TEST_MAIN(NeverCalled) {
NOTREACHED() << "Process should not have been launched.";
}
TEST_F(ProcessUtilTest, TransferInvalidHandleFails) {
LaunchOptions options;
options.paths_to_clone.push_back(FilePath("/tmp"));
options.paths_to_transfer.push_back({FilePath("/foo"), ZX_HANDLE_INVALID});
options.spawn_flags = FDIO_SPAWN_CLONE_STDIO;
// Verify that the process is never constructed.
Process process(SpawnChildWithOptions("NeverCalled", options));
EXPECT_FALSE(process.IsValid());
}
TEST_F(ProcessUtilTest, CloneInvalidDirFails) {
const std::string signal_file = GetSignalFilePath(kSignalFileClone);
remove(signal_file.c_str());
LaunchOptions options;
options.paths_to_clone.push_back(FilePath("/tmp"));
options.paths_to_clone.push_back(FilePath("/definitely_not_a_dir"));
options.spawn_flags = FDIO_SPAWN_CLONE_STDIO;
Process process(SpawnChildWithOptions("NeverCalled", options));
EXPECT_FALSE(process.IsValid());
}
MULTIPROCESS_TEST_MAIN(CheckOnlyDataExists) {
return CheckOnlyOnePathExists("/data");
}
TEST_F(ProcessUtilTest, CloneAlternateDir) {
const std::string signal_file = GetSignalFilePath(kSignalFileClone);
remove(signal_file.c_str());
LaunchOptions options;
options.paths_to_clone.push_back(FilePath("/data"));
// The DIR_TEMP path is used by GetSignalFilePath().
options.paths_to_clone.push_back(FilePath("/tmp"));
options.spawn_flags = FDIO_SPAWN_CLONE_STDIO;
Process process(SpawnChildWithOptions("CheckOnlyDataExists", options));
ASSERT_TRUE(process.IsValid());
SignalChildren(signal_file.c_str());
int exit_code = 42;
EXPECT_TRUE(process.WaitForExit(&exit_code));
EXPECT_EQ(kSuccess, exit_code);
}
TEST_F(ProcessUtilTest, HandlesToTransferClosedOnSpawnFailure) {
zx::handle handles[2];
zx_status_t result = zx_channel_create(0, handles[0].reset_and_get_address(),
handles[1].reset_and_get_address());
ZX_CHECK(ZX_OK == result, result) << "zx_channel_create";
LaunchOptions options;
options.handles_to_transfer.push_back({0, handles[0].get()});
// Launch a non-existent binary, causing fdio_spawn() to fail.
CommandLine command_line(FilePath(
FILE_PATH_LITERAL("š©magical_filename_that_will_never_exist_ever")));
Process process(LaunchProcess(command_line, options));
ASSERT_FALSE(process.IsValid());
// If LaunchProcess did its job then handles[0] is no longer valid, and
// handles[1] should observe a channel-closed signal.
EXPECT_EQ(
zx_object_wait_one(handles[1].get(), ZX_CHANNEL_PEER_CLOSED, 0, nullptr),
ZX_OK);
EXPECT_EQ(ZX_ERR_BAD_HANDLE, zx_handle_close(handles[0].get()));
std::ignore = handles[0].release();
}
TEST_F(ProcessUtilTest, HandlesToTransferClosedOnBadPathToMapFailure) {
zx::handle handles[2];
zx_status_t result = zx_channel_create(0, handles[0].reset_and_get_address(),
handles[1].reset_and_get_address());
ZX_CHECK(ZX_OK == result, result) << "zx_channel_create";
LaunchOptions options;
options.handles_to_transfer.push_back({0, handles[0].get()});
options.spawn_flags = options.spawn_flags & ~FDIO_SPAWN_CLONE_NAMESPACE;
options.paths_to_clone.emplace_back(
"š©magical_path_that_will_never_exist_ever");
// LaunchProces should fail to open() the path_to_map, and fail before
// fdio_spawn().
Process process(LaunchProcess(CommandLine(FilePath()), options));
ASSERT_FALSE(process.IsValid());
// If LaunchProcess did its job then handles[0] is no longer valid, and
// handles[1] should observe a channel-closed signal.
EXPECT_EQ(
zx_object_wait_one(handles[1].get(), ZX_CHANNEL_PEER_CLOSED, 0, nullptr),
ZX_OK);
EXPECT_EQ(ZX_ERR_BAD_HANDLE, zx_handle_close(handles[0].get()));
std::ignore = handles[0].release();
}
TEST_F(ProcessUtilTest, FuchsiaProcessNameSuffix) {
LaunchOptions options;
options.process_name_suffix = "#test";
Process process(SpawnChildWithOptions("SimpleChildProcess", options));
char name[256] = {};
size_t name_size = sizeof(name);
zx_status_t status =
zx_object_get_property(process.Handle(), ZX_PROP_NAME, name, name_size);
ASSERT_EQ(status, ZX_OK);
EXPECT_EQ(std::string(name),
CommandLine::ForCurrentProcess()->GetProgram().BaseName().value() +
"#test");
}
#endif // BUILDFLAG(IS_FUCHSIA)
// On Android SpawnProcess() doesn't use LaunchProcess() and doesn't support
// LaunchOptions::current_directory.
#if !BUILDFLAG(IS_ANDROID)
static void CheckCwdIsExpected(FilePath expected) {
FilePath actual;
CHECK(GetCurrentDirectory(&actual));
actual = MakeAbsoluteFilePath(actual);
CHECK(!actual.empty());
CHECK_EQ(expected, actual);
}
// N.B. This test does extra work to check the cwd on multiple threads, because
// on macOS a per-thread cwd is set when using LaunchProcess().
MULTIPROCESS_TEST_MAIN(CheckCwdProcess) {
// Get the expected cwd.
FilePath temp_dir;
CHECK(GetTempDir(&temp_dir));
temp_dir = MakeAbsoluteFilePath(temp_dir);
CHECK(!temp_dir.empty());
// Test that the main thread has the right cwd.
CheckCwdIsExpected(temp_dir);
// Create a non-main thread.
Thread thread("CheckCwdThread");
thread.Start();
auto task_runner = thread.task_runner();
// A synchronization primitive used to wait for work done on the non-main
// thread.
WaitableEvent event(WaitableEvent::ResetPolicy::AUTOMATIC);
RepeatingClosure signal_event =
BindRepeating(&WaitableEvent::Signal, Unretained(&event));
// Test that a non-main thread has the right cwd.
task_runner->PostTask(FROM_HERE, BindOnce(&CheckCwdIsExpected, temp_dir));
task_runner->PostTask(FROM_HERE, signal_event);
event.Wait();
// Get a new cwd for the process.
const FilePath home_dir = PathService::CheckedGet(DIR_HOME);
// Change the cwd on the secondary thread. IgnoreResult is used when setting
// because it is checked immediately after.
task_runner->PostTask(FROM_HERE,
BindOnce(IgnoreResult(&SetCurrentDirectory), home_dir));
task_runner->PostTask(FROM_HERE, BindOnce(&CheckCwdIsExpected, home_dir));
task_runner->PostTask(FROM_HERE, signal_event);
event.Wait();
// Make sure the main thread sees the cwd from the secondary thread.
CheckCwdIsExpected(home_dir);
// Change the directory back on the main thread.
CHECK(SetCurrentDirectory(temp_dir));
CheckCwdIsExpected(temp_dir);
// Ensure that the secondary thread sees the new cwd too.
task_runner->PostTask(FROM_HERE, BindOnce(&CheckCwdIsExpected, temp_dir));
task_runner->PostTask(FROM_HERE, signal_event);
event.Wait();
// Change the cwd on the secondary thread one more time and join the thread.
task_runner->PostTask(FROM_HERE,
BindOnce(IgnoreResult(&SetCurrentDirectory), home_dir));
thread.Stop();
// Make sure that the main thread picked up the new cwd.
CheckCwdIsExpected(home_dir);
return kSuccess;
}
// A relative binary loader path is set in MSAN builds, so binaries must be
// run from the build directory.
#if !defined(MEMORY_SANITIZER)
TEST_F(ProcessUtilTest, CurrentDirectory) {
// TODO(rickyz): Add support for passing arguments to multiprocess children,
// then create a special directory for this test.
FilePath tmp_dir;
ASSERT_TRUE(GetTempDir(&tmp_dir));
LaunchOptions options;
options.current_directory = tmp_dir;
Process process(SpawnChildWithOptions("CheckCwdProcess", options));
ASSERT_TRUE(process.IsValid());
int exit_code = 42;
EXPECT_TRUE(process.WaitForExit(&exit_code));
EXPECT_EQ(kSuccess, exit_code);
}
#endif // !defined(MEMORY_SANITIZER)
#endif // !BUILDFLAG(IS_ANDROID)
#if BUILDFLAG(IS_WIN)
// TODO(cpu): figure out how to test this in other platforms.
TEST_F(ProcessUtilTest, GetProcId) {
ProcessId id1 = GetProcId(GetCurrentProcess());
EXPECT_NE(0ul, id1);
Process process = SpawnChild("SimpleChildProcess");
ASSERT_TRUE(process.IsValid());
ProcessId id2 = process.Pid();
EXPECT_NE(0ul, id2);
EXPECT_NE(id1, id2);
}
#endif // BUILDFLAG(IS_WIN)
#if !BUILDFLAG(IS_APPLE) && !BUILDFLAG(IS_ANDROID)
// This test is disabled on Mac, since it's flaky due to ReportCrash
// taking a variable amount of time to parse and load the debug and
// symbol data for this unit test's executable before firing the
// signal handler.
//
// TODO(gspencer): turn this test process into a very small program
// with no symbols (instead of using the multiprocess testing
// framework) to reduce the ReportCrash overhead.
//
// It is disabled on Android as MultiprocessTests are started as services that
// the framework restarts on crashes.
const char kSignalFileCrash[] = "CrashingChildProcess.die";
MULTIPROCESS_TEST_MAIN(CrashingChildProcess) {
WaitToDie(ProcessUtilTest::GetSignalFilePath(kSignalFileCrash).c_str());
#if BUILDFLAG(IS_POSIX)
// Have to disable to signal handler for segv so we can get a crash
// instead of an abnormal termination through the crash dump handler.
::signal(SIGSEGV, SIG_DFL);
#endif
// Make this process have a segmentation fault.
volatile int* oops = nullptr;
*oops = 0xDEAD;
return 1;
}
// This test intentionally crashes, so we don't need to run it under
// AddressSanitizer.
#if defined(ADDRESS_SANITIZER)
#define MAYBE_GetTerminationStatusCrash DISABLED_GetTerminationStatusCrash
#else
#define MAYBE_GetTerminationStatusCrash GetTerminationStatusCrash
#endif
TEST_F(ProcessUtilTest, MAYBE_GetTerminationStatusCrash) {
const std::string signal_file = GetSignalFilePath(kSignalFileCrash);
remove(signal_file.c_str());
Process process = SpawnChild("CrashingChildProcess");
ASSERT_TRUE(process.IsValid());
int exit_code = 42;
EXPECT_EQ(TERMINATION_STATUS_STILL_RUNNING,
GetTerminationStatus(process.Handle(), &exit_code));
EXPECT_EQ(kExpectedStillRunningExitCode, exit_code);
SignalChildren(signal_file.c_str());
exit_code = 42;
TerminationStatus status =
WaitForChildTermination(process.Handle(), &exit_code);
EXPECT_EQ(TERMINATION_STATUS_PROCESS_CRASHED, status);
#if BUILDFLAG(IS_WIN)
EXPECT_EQ(static_cast<int>(0xc0000005), exit_code);
#elif BUILDFLAG(IS_POSIX)
int signaled = WIFSIGNALED(exit_code);
EXPECT_NE(0, signaled);
int signal = WTERMSIG(exit_code);
EXPECT_EQ(SIGSEGV, signal);
#endif
// Reset signal handlers back to "normal".
debug::EnableInProcessStackDumping();
remove(signal_file.c_str());
}
#endif // !BUILDFLAG(IS_APPLE) && !BUILDFLAG(IS_ANDROID)
MULTIPROCESS_TEST_MAIN(KilledChildProcess) {
WaitToDie(ProcessUtilTest::GetSignalFilePath(kSignalFileKill).c_str());
#if BUILDFLAG(IS_WIN)
// Kill ourselves.
HANDLE handle = ::OpenProcess(PROCESS_ALL_ACCESS, 0, ::GetCurrentProcessId());
::TerminateProcess(handle, kExpectedKilledExitCode);
#elif BUILDFLAG(IS_POSIX)
// Send a SIGKILL to this process, just like the OOM killer would.
::kill(getpid(), SIGKILL);
#elif BUILDFLAG(IS_FUCHSIA)
zx_task_kill(zx_process_self());
#endif
return 1;
}
#if BUILDFLAG(IS_POSIX)
MULTIPROCESS_TEST_MAIN(TerminatedChildProcess) {
WaitToDie(ProcessUtilTest::GetSignalFilePath(kSignalFileTerm).c_str());
// Send a SIGTERM to this process.
::kill(getpid(), SIGTERM);
return 1;
}
#endif // BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
TEST_F(ProcessUtilTest, GetTerminationStatusSigKill) {
const std::string signal_file = GetSignalFilePath(kSignalFileKill);
remove(signal_file.c_str());
Process process = SpawnChild("KilledChildProcess");
ASSERT_TRUE(process.IsValid());
int exit_code = 42;
EXPECT_EQ(TERMINATION_STATUS_STILL_RUNNING,
GetTerminationStatus(process.Handle(), &exit_code));
EXPECT_EQ(kExpectedStillRunningExitCode, exit_code);
SignalChildren(signal_file.c_str());
exit_code = 42;
TerminationStatus status =
WaitForChildTermination(process.Handle(), &exit_code);
#if BUILDFLAG(IS_CHROMEOS)
EXPECT_EQ(TERMINATION_STATUS_PROCESS_WAS_KILLED_BY_OOM, status);
#else
EXPECT_EQ(TERMINATION_STATUS_PROCESS_WAS_KILLED, status);
#endif
#if BUILDFLAG(IS_WIN)
EXPECT_EQ(kExpectedKilledExitCode, exit_code);
#elif BUILDFLAG(IS_POSIX)
int signaled = WIFSIGNALED(exit_code);
EXPECT_NE(0, signaled);
int signal = WTERMSIG(exit_code);
EXPECT_EQ(SIGKILL, signal);
#endif
remove(signal_file.c_str());
}
#if BUILDFLAG(IS_POSIX)
// TODO(crbug.com/42050610): Access to the process termination reason is not
// implemented in Fuchsia. Unix signals are not implemented in Fuchsia so this
// test might not be relevant anyway.
TEST_F(ProcessUtilTest, GetTerminationStatusSigTerm) {
const std::string signal_file = GetSignalFilePath(kSignalFileTerm);
remove(signal_file.c_str());
Process process = SpawnChild("TerminatedChildProcess");
ASSERT_TRUE(process.IsValid());
int exit_code = 42;
EXPECT_EQ(TERMINATION_STATUS_STILL_RUNNING,
GetTerminationStatus(process.Handle(), &exit_code));
EXPECT_EQ(kExpectedStillRunningExitCode, exit_code);
SignalChildren(signal_file.c_str());
exit_code = 42;
TerminationStatus status =
WaitForChildTermination(process.Handle(), &exit_code);
EXPECT_EQ(TERMINATION_STATUS_PROCESS_WAS_KILLED, status);
int signaled = WIFSIGNALED(exit_code);
EXPECT_NE(0, signaled);
int signal = WTERMSIG(exit_code);
EXPECT_EQ(SIGTERM, signal);
remove(signal_file.c_str());
}
#endif // BUILDFLAG(IS_POSIX)
TEST_F(ProcessUtilTest, EnsureTerminationUndying) {
test::TaskEnvironment task_environment;
Process child_process = SpawnChild("process_util_test_never_die");
ASSERT_TRUE(child_process.IsValid());
EnsureProcessTerminated(child_process.Duplicate());
#if BUILDFLAG(IS_POSIX)
errno = 0;
#endif // BUILDFLAG(IS_POSIX)
// Allow a generous timeout, to cope with slow/loaded test bots.
bool did_exit = child_process.WaitForExitWithTimeout(
TestTimeouts::action_max_timeout(), nullptr);
#if BUILDFLAG(IS_POSIX)
// Both EnsureProcessTerminated() and WaitForExitWithTimeout() will call
// waitpid(). One will succeed, and the other will fail with ECHILD. If our
// wait failed then check for ECHILD, and assumed |did_exit| in that case.
did_exit = did_exit || (errno == ECHILD);
#endif // BUILDFLAG(IS_POSIX)
EXPECT_TRUE(did_exit);
}
MULTIPROCESS_TEST_MAIN(process_util_test_never_die) {
while (true) {
PlatformThread::Sleep(Seconds(500));
}
}
TEST_F(ProcessUtilTest, EnsureTerminationGracefulExit) {
test::TaskEnvironment task_environment;
Process child_process = SpawnChild("process_util_test_die_immediately");
ASSERT_TRUE(child_process.IsValid());
// Wait for the child process to actually exit.
child_process.Duplicate().WaitForExitWithTimeout(
TestTimeouts::action_max_timeout(), nullptr);
EnsureProcessTerminated(child_process.Duplicate());
// Verify that the process is really, truly gone.
EXPECT_TRUE(child_process.WaitForExitWithTimeout(
TestTimeouts::action_max_timeout(), nullptr));
}
MULTIPROCESS_TEST_MAIN(process_util_test_die_immediately) {
return kSuccess;
}
#if BUILDFLAG(IS_WIN)
// TODO(estade): if possible, port this test.
TEST_F(ProcessUtilTest, LaunchAsUser) {
UserTokenHandle token;
ASSERT_TRUE(OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &token));
LaunchOptions options;
options.as_user = token;
EXPECT_TRUE(
LaunchProcess(MakeCmdLine("SimpleChildProcess"), options).IsValid());
}
MULTIPROCESS_TEST_MAIN(ChildVerifiesCetDisabled) {
// Policy not defined for Win < Win10 20H1 but that's ok.
PROCESS_MITIGATION_USER_SHADOW_STACK_POLICY policy = {};
if (GetProcessMitigationPolicy(GetCurrentProcess(),
ProcessUserShadowStackPolicy, &policy,
sizeof(policy))) {
if (policy.EnableUserShadowStack)
return 1;
}
return kSuccess;
}
TEST_F(ProcessUtilTest, LaunchDisablingCetCompat) {
LaunchOptions options;
// This only has an effect on Windows > 20H2 with CET hardware but
// is safe on every platform.
options.disable_cetcompat = true;
EXPECT_TRUE(LaunchProcess(MakeCmdLine("ChildVerifiesCetDisabled"), options)
.IsValid());
}
static const char kEventToTriggerHandleSwitch[] = "event-to-trigger-handle";
MULTIPROCESS_TEST_MAIN(TriggerEventChildProcess) {
std::string handle_value_string =
CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
kEventToTriggerHandleSwitch);
CHECK(!handle_value_string.empty());
uint64_t handle_value_uint64;
CHECK(StringToUint64(handle_value_string, &handle_value_uint64));
// Give ownership of the handle to |event|.
WaitableEvent event(
win::ScopedHandle(reinterpret_cast<HANDLE>(handle_value_uint64)));
event.Signal();
return 0;
}
TEST_F(ProcessUtilTest, InheritSpecifiedHandles) {
// Manually create the event, so that it can be inheritable.
SECURITY_ATTRIBUTES security_attributes = {};
security_attributes.nLength = static_cast<DWORD>(sizeof(security_attributes));
security_attributes.lpSecurityDescriptor = NULL;
security_attributes.bInheritHandle = true;
// Takes ownership of the event handle.
WaitableEvent event(
win::ScopedHandle(CreateEvent(&security_attributes, true, false, NULL)));
LaunchOptions options;
options.handles_to_inherit.emplace_back(event.handle());
CommandLine cmd_line = MakeCmdLine("TriggerEventChildProcess");
cmd_line.AppendSwitchASCII(
kEventToTriggerHandleSwitch,
NumberToString(reinterpret_cast<uint64_t>(event.handle())));
// Launch the process and wait for it to trigger the event.
ASSERT_TRUE(LaunchProcess(cmd_line, options).IsValid());
EXPECT_TRUE(event.TimedWait(TestTimeouts::action_max_timeout()));
}
#endif // BUILDFLAG(IS_WIN)
TEST_F(ProcessUtilTest, GetAppOutput) {
CommandLine command(test_helper_path_);
command.AppendArg("hello");
command.AppendArg("there");
command.AppendArg("good");
command.AppendArg("people");
std::string output;
EXPECT_TRUE(GetAppOutput(command, &output));
EXPECT_EQ("hello there good people", output);
output.clear();
const char* kEchoMessage = "blah";
command = CommandLine(test_helper_path_);
command.AppendArg("-x");
command.AppendArg("28");
command.AppendArg(kEchoMessage);
EXPECT_FALSE(GetAppOutput(command, &output));
EXPECT_EQ(kEchoMessage, output);
}
TEST_F(ProcessUtilTest, GetAppOutputWithExitCode) {
const char* kEchoMessage1 = "doge";
int exit_code = -1;
CommandLine command(test_helper_path_);
command.AppendArg(kEchoMessage1);
std::string output;
EXPECT_TRUE(GetAppOutputWithExitCode(command, &output, &exit_code));
EXPECT_EQ(kEchoMessage1, output);
EXPECT_EQ(0, exit_code);
output.clear();
const char* kEchoMessage2 = "pupper";
const int kExpectedExitCode = 42;
command = CommandLine(test_helper_path_);
command.AppendArg("-x");
command.AppendArg(NumberToString(kExpectedExitCode));
command.AppendArg(kEchoMessage2);
#if BUILDFLAG(IS_WIN)
// On Windows, anything that quits with a nonzero status code is handled as a
// "crash", so just ignore GetAppOutputWithExitCode's return value.
GetAppOutputWithExitCode(command, &output, &exit_code);
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
EXPECT_TRUE(GetAppOutputWithExitCode(command, &output, &exit_code));
#endif
EXPECT_EQ(kEchoMessage2, output);
EXPECT_EQ(kExpectedExitCode, exit_code);
}
#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
namespace {
// Returns the maximum number of files that a process can have open.
// Returns 0 on error.
int GetMaxFilesOpenInProcess() {
#if BUILDFLAG(IS_FUCHSIA)
return FDIO_MAX_FD;
#else
struct rlimit rlim;
if (getrlimit(RLIMIT_NOFILE, &rlim) != 0) {
return 0;
}
// rlim_t is a uint64_t - clip to maxint. We do this since FD #s are ints
// which are all 32 bits on the supported platforms.
rlim_t max_int = static_cast<rlim_t>(std::numeric_limits<int32_t>::max());
if (rlim.rlim_cur > max_int) {
return max_int;
}
return rlim.rlim_cur;
#endif // BUILDFLAG(IS_FUCHSIA)
}
const int kChildPipe = 20; // FD # for write end of pipe in child process.
#if BUILDFLAG(IS_APPLE)
// Declarations from 12.3 xnu-8020.101.4/bsd/sys/guarded.h (not in the SDK).
extern "C" {
using guardid_t = uint64_t;
#define GUARD_DUP (1u << 1)
int change_fdguard_np(int fd,
const guardid_t* guard,
unsigned int guardflags,
const guardid_t* nguard,
unsigned int nguardflags,
int* fdflagsp);
} // extern "C"
// Attempt to set a file-descriptor guard on |fd|. In case of success, remove
// it and return |true| to indicate that it can be guarded. Returning |false|
// means either that |fd| is guarded by some other code, or more likely EBADF.
//
// Starting with 10.9, libdispatch began setting GUARD_DUP on a file descriptor.
// Unfortunately, it is spun up as part of +[NSApplication initialize], which is
// not really something that Chromium can avoid using on OSX. See
// <http://crbug.com/338157>. This function allows querying whether the file
// descriptor is guarded before attempting to close it.
bool CanGuardFd(int fd) {
// Saves the original flags to reset later.
int original_fdflags = 0;
// This can be any value at all, it just has to match up between the two
// calls.
const guardid_t kGuard = 15;
// Attempt to change the guard. This can fail with EBADF if the file
// descriptor is bad, or EINVAL if the fd already has a guard set.
int ret =
change_fdguard_np(fd, NULL, 0, &kGuard, GUARD_DUP, &original_fdflags);
if (ret == -1)
return false;
// Remove the guard. It should not be possible to fail in removing the guard
// just added.
ret = change_fdguard_np(fd, &kGuard, GUARD_DUP, NULL, 0, &original_fdflags);
DPCHECK(ret == 0);
return true;
}
#endif // BUILDFLAG(IS_APPLE)
} // namespace
MULTIPROCESS_TEST_MAIN(ProcessUtilsLeakFDChildProcess) {
// This child process counts the number of open FDs, it then writes that
// number out to a pipe connected to the parent.
int num_open_files = 0;
int write_pipe = kChildPipe;
int max_files = GetMaxFilesOpenInProcess();
for (int i = STDERR_FILENO + 1; i < max_files; i++) {
#if BUILDFLAG(IS_APPLE)
// Ignore guarded or invalid file descriptors.
if (!CanGuardFd(i))
continue;
#endif
if (i != kChildPipe) {
int fd;
if ((fd = HANDLE_EINTR(dup(i))) != -1) {
close(fd);
num_open_files += 1;
}
}
}
int written =
HANDLE_EINTR(write(write_pipe, &num_open_files, sizeof(num_open_files)));
DCHECK_EQ(static_cast<size_t>(written), sizeof(num_open_files));
int ret = IGNORE_EINTR(close(write_pipe));
DPCHECK(ret == 0);
return 0;
}
int ProcessUtilTest::CountOpenFDsInChild() {
int fds[2];
if (pipe(fds) < 0)
NOTREACHED();
LaunchOptions options;
options.fds_to_remap.emplace_back(fds[1], kChildPipe);
Process process =
SpawnChildWithOptions("ProcessUtilsLeakFDChildProcess", options);
CHECK(process.IsValid());
int ret = IGNORE_EINTR(close(fds[1]));
DPCHECK(ret == 0);
// Read number of open files in client process from pipe;
int num_open_files = -1;
ssize_t bytes_read =
HANDLE_EINTR(read(fds[0], &num_open_files, sizeof(num_open_files)));
CHECK_EQ(bytes_read, static_cast<ssize_t>(sizeof(num_open_files)));
#if defined(THREAD_SANITIZER)
// Compiler-based ThreadSanitizer makes this test slow.
TimeDelta timeout = Seconds(3);
#else
TimeDelta timeout = Seconds(1);
#endif
int exit_code;
CHECK(process.WaitForExitWithTimeout(timeout, &exit_code));
ret = IGNORE_EINTR(close(fds[0]));
DPCHECK(ret == 0);
return num_open_files;
}
TEST_F(ProcessUtilTest, FDRemapping) {
int fds_before = CountOpenFDsInChild();
// Open some dummy fds to make sure they don't propagate over to the
// child process.
#if BUILDFLAG(IS_FUCHSIA)
ScopedFD dev_null(fdio_fd_create_null());
#else
ScopedFD dev_null(open("/dev/null", O_RDONLY));
#endif // BUILDFLAG(IS_FUCHSIA)
DPCHECK(dev_null.get() > 0);
int sockets[2];
int ret = socketpair(AF_UNIX, SOCK_STREAM, 0, sockets);
DPCHECK(ret == 0);
int fds_after = CountOpenFDsInChild();
ASSERT_EQ(fds_after, fds_before);
ret = IGNORE_EINTR(close(sockets[0]));
DPCHECK(ret == 0);
ret = IGNORE_EINTR(close(sockets[1]));
DPCHECK(ret == 0);
}
const char kPipeValue = '\xcc';
MULTIPROCESS_TEST_MAIN(ProcessUtilsVerifyStdio) {
// Write to stdio so the parent process can observe output.
CHECK_EQ(1, HANDLE_EINTR(write(STDOUT_FILENO, &kPipeValue, 1)));
// Close all of the handles, to verify they are valid.
CHECK_EQ(0, IGNORE_EINTR(close(STDIN_FILENO)));
CHECK_EQ(0, IGNORE_EINTR(close(STDOUT_FILENO)));
CHECK_EQ(0, IGNORE_EINTR(close(STDERR_FILENO)));
return 0;
}
TEST_F(ProcessUtilTest, FDRemappingIncludesStdio) {
#if BUILDFLAG(IS_FUCHSIA)
// The fd obtained from fdio_fd_create_null cannot be cloned while spawning a
// child proc, so open a true file in a transient temp dir.
ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
FilePath temp_file_path;
ASSERT_TRUE(CreateTemporaryFileInDir(temp_dir.GetPath(), &temp_file_path));
File temp_file(temp_file_path,
File::FLAG_CREATE_ALWAYS | File::FLAG_READ | File::FLAG_WRITE);
ASSERT_TRUE(temp_file.IsValid());
ScopedFD some_fd(temp_file.TakePlatformFile());
#else // BUILDFLAG(IS_FUCHSIA)
ScopedFD some_fd(open("/dev/null", O_RDONLY));
#endif // BUILDFLAG(IS_FUCHSIA)
ASSERT_LT(2, some_fd.get());
// Backup stdio and replace it with the write end of a pipe, for our
// child process to inherit.
int pipe_fds[2];
int result = pipe(pipe_fds);
ASSERT_EQ(0, result);
int backup_stdio = HANDLE_EINTR(dup(STDOUT_FILENO));
ASSERT_LE(0, backup_stdio);
result = dup2(pipe_fds[1], STDOUT_FILENO);
ASSERT_EQ(STDOUT_FILENO, result);
// Launch the test process, which should inherit our pipe stdio.
LaunchOptions options;
options.fds_to_remap.emplace_back(some_fd.get(), some_fd.get());
Process process = SpawnChildWithOptions("ProcessUtilsVerifyStdio", options);
ASSERT_TRUE(process.IsValid());
// Restore stdio, so we can output stuff.
result = dup2(backup_stdio, STDOUT_FILENO);
ASSERT_EQ(STDOUT_FILENO, result);
// Close our copy of the write end of the pipe, so that the read()
// from the other end will see EOF if it wasn't copied to the child.
result = IGNORE_EINTR(close(pipe_fds[1]));
ASSERT_EQ(0, result);
result = IGNORE_EINTR(close(backup_stdio));
ASSERT_EQ(0, result);
// Also close the remapped descriptor.
some_fd.reset();
// Read from the pipe to verify that it is connected to the child
// process' stdio.
char buf[16] = {};
EXPECT_EQ(1, HANDLE_EINTR(read(pipe_fds[0], buf, sizeof(buf))));
EXPECT_EQ(kPipeValue, buf[0]);
result = IGNORE_EINTR(close(pipe_fds[0]));
ASSERT_EQ(0, result);
int exit_code;
ASSERT_TRUE(process.WaitForExitWithTimeout(Seconds(5), &exit_code));
EXPECT_EQ(0, exit_code);
}
#if BUILDFLAG(IS_FUCHSIA)
const uint16_t kStartupHandleId = 43;
MULTIPROCESS_TEST_MAIN(ProcessUtilsVerifyHandle) {
zx_handle_t handle =
zx_take_startup_handle(PA_HND(PA_USER0, kStartupHandleId));
CHECK_NE(ZX_HANDLE_INVALID, handle);
// Write to the pipe so the parent process can observe output.
size_t bytes_written = 0;
zx_status_t result = zx_socket_write(handle, 0, &kPipeValue,
sizeof(kPipeValue), &bytes_written);
CHECK_EQ(ZX_OK, result);
CHECK_EQ(1u, bytes_written);
CHECK_EQ(ZX_OK, zx_handle_close(handle));
return 0;
}
TEST_F(ProcessUtilTest, LaunchWithHandleTransfer) {
// Create a pipe to pass to the child process.
zx_handle_t handles[2];
zx_status_t result =
zx_socket_create(ZX_SOCKET_STREAM, &handles[0], &handles[1]);
ASSERT_EQ(ZX_OK, result);
// Launch the test process, and pass it one end of the pipe.
LaunchOptions options;
options.handles_to_transfer.push_back(
{PA_HND(PA_USER0, kStartupHandleId), handles[0]});
Process process = SpawnChildWithOptions("ProcessUtilsVerifyHandle", options);
ASSERT_TRUE(process.IsValid());
// Read from the pipe to verify that the child received it.
zx_signals_t signals = 0;
result = zx_object_wait_one(
handles[1], ZX_SOCKET_READABLE | ZX_SOCKET_PEER_CLOSED,
(TimeTicks::Now() + TestTimeouts::action_timeout()).ToZxTime(), &signals);
ASSERT_EQ(ZX_OK, result);
ASSERT_TRUE(signals & ZX_SOCKET_READABLE);
size_t bytes_read = 0;
char buf[16] = {};
result = zx_socket_read(handles[1], 0, buf, sizeof(buf), &bytes_read);
EXPECT_EQ(ZX_OK, result);
EXPECT_EQ(1u, bytes_read);
EXPECT_EQ(kPipeValue, buf[0]);
CHECK_EQ(ZX_OK, zx_handle_close(handles[1]));
int exit_code;
ASSERT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_timeout(),
&exit_code));
EXPECT_EQ(0, exit_code);
}
#endif // BUILDFLAG(IS_FUCHSIA)
// There's no such thing as a parent process id on Fuchsia.
#if !BUILDFLAG(IS_FUCHSIA)
TEST_F(ProcessUtilTest, GetParentProcessId) {
ProcessId ppid = GetParentProcessId(GetCurrentProcessHandle());
EXPECT_EQ(ppid, static_cast<ProcessId>(getppid()));
}
#endif // !BUILDFLAG(IS_FUCHSIA)
#if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_FUCHSIA) && !BUILDFLAG(IS_APPLE)
class WriteToPipeDelegate : public LaunchOptions::PreExecDelegate {
public:
explicit WriteToPipeDelegate(int fd) : fd_(fd) {}
WriteToPipeDelegate(const WriteToPipeDelegate&) = delete;
WriteToPipeDelegate& operator=(const WriteToPipeDelegate&) = delete;
~WriteToPipeDelegate() override = default;
void RunAsyncSafe() override {
RAW_CHECK(HANDLE_EINTR(write(fd_, &kPipeValue, 1)) == 1);
RAW_CHECK(IGNORE_EINTR(close(fd_)) == 0);
}
private:
int fd_;
};
TEST_F(ProcessUtilTest, PreExecHook) {
int pipe_fds[2];
ASSERT_EQ(0, pipe(pipe_fds));
ScopedFD read_fd(pipe_fds[0]);
ScopedFD write_fd(pipe_fds[1]);
WriteToPipeDelegate write_to_pipe_delegate(write_fd.get());
LaunchOptions options;
options.fds_to_remap.emplace_back(write_fd.get(), write_fd.get());
options.pre_exec_delegate = &write_to_pipe_delegate;
Process process(SpawnChildWithOptions("SimpleChildProcess", options));
ASSERT_TRUE(process.IsValid());
write_fd.reset();
char c;
ASSERT_EQ(1, HANDLE_EINTR(read(read_fd.get(), &c, 1)));
EXPECT_EQ(c, kPipeValue);
int exit_code = 42;
EXPECT_TRUE(process.WaitForExit(&exit_code));
EXPECT_EQ(0, exit_code);
}
#endif // !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_FUCHSIA)
#endif // BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
// There's no such thing as a parent process id on Fuchsia.
#if !BUILDFLAG(IS_FUCHSIA)
TEST_F(ProcessUtilTest, GetParentProcessId2) {
ProcessId id1 = GetCurrentProcId();
Process process = SpawnChild("SimpleChildProcess");
ASSERT_TRUE(process.IsValid());
ProcessId ppid = GetParentProcessId(process.Handle());
EXPECT_EQ(ppid, id1);
}
#endif // !BUILDFLAG(IS_FUCHSIA)
namespace {
std::string TestLaunchProcess(const CommandLine& cmdline,
const EnvironmentMap& env_changes,
const bool clear_environment,
const int clone_flags) {
LaunchOptions options;
options.wait = true;
options.environment = env_changes;
options.clear_environment = clear_environment;
#if BUILDFLAG(IS_WIN)
HANDLE read_handle, write_handle;
PCHECK(CreatePipe(&read_handle, &write_handle, nullptr, 0));
File read_pipe(read_handle);
File write_pipe(write_handle);
options.stdin_handle = INVALID_HANDLE_VALUE;
options.stdout_handle = write_handle;
options.stderr_handle = GetStdHandle(STD_ERROR_HANDLE);
options.handles_to_inherit.push_back(write_handle);
#else
int fds[2];
PCHECK(pipe(fds) == 0);
File read_pipe(fds[0]);
File write_pipe(fds[1]);
options.fds_to_remap.emplace_back(fds[1], STDOUT_FILENO);
#endif // BUILDFLAG(IS_WIN)
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
options.clone_flags = clone_flags;
#else
CHECK_EQ(0, clone_flags);
#endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
EXPECT_TRUE(LaunchProcess(cmdline, options).IsValid());
write_pipe.Close();
char buf[512];
int n = UNSAFE_TODO(read_pipe.ReadAtCurrentPos(buf, sizeof(buf)));
#if BUILDFLAG(IS_WIN)
// Closed pipes fail with ERROR_BROKEN_PIPE on Windows, rather than
// successfully reporting EOF.
if (n < 0 && GetLastError() == ERROR_BROKEN_PIPE) {
n = 0;
}
#endif // BUILDFLAG(IS_WIN)
PCHECK(n >= 0);
return std::string(buf, n);
}
const char kLargeString[] =
"0123456789012345678901234567890123456789012345678901234567890123456789"
"0123456789012345678901234567890123456789012345678901234567890123456789"
"0123456789012345678901234567890123456789012345678901234567890123456789"
"0123456789012345678901234567890123456789012345678901234567890123456789"
"0123456789012345678901234567890123456789012345678901234567890123456789"
"0123456789012345678901234567890123456789012345678901234567890123456789"
"0123456789012345678901234567890123456789012345678901234567890123456789";
} // namespace
TEST_F(ProcessUtilTest, LaunchProcess) {
const int no_clone_flags = 0;
const bool no_clear_environ = false;
const FilePath::CharType kBaseTest[] = FILE_PATH_LITERAL("BASE_TEST");
const CommandLine kPrintEnvCommand(CommandLine::StringVector(
{test_helper_path_.value(), FILE_PATH_LITERAL("-e"), kBaseTest}));
std::unique_ptr<Environment> env = Environment::Create();
EnvironmentMap env_changes;
env_changes[kBaseTest] = FILE_PATH_LITERAL("bar");
EXPECT_EQ("bar", TestLaunchProcess(kPrintEnvCommand, env_changes,
no_clear_environ, no_clone_flags));
env_changes.clear();
EXPECT_TRUE(env->SetVar("BASE_TEST", "testing"));
EXPECT_EQ("testing", TestLaunchProcess(kPrintEnvCommand, env_changes,
no_clear_environ, no_clone_flags));
env_changes[kBaseTest] = FilePath::StringType();
EXPECT_EQ("", TestLaunchProcess(kPrintEnvCommand, env_changes,
no_clear_environ, no_clone_flags));
env_changes[kBaseTest] = FILE_PATH_LITERAL("foo");
EXPECT_EQ("foo", TestLaunchProcess(kPrintEnvCommand, env_changes,
no_clear_environ, no_clone_flags));
env_changes.clear();
EXPECT_TRUE(env->SetVar("BASE_TEST", kLargeString));
EXPECT_EQ(std::string(kLargeString),
TestLaunchProcess(kPrintEnvCommand, env_changes, no_clear_environ,
no_clone_flags));
env_changes[kBaseTest] = FILE_PATH_LITERAL("wibble");
EXPECT_EQ("wibble", TestLaunchProcess(kPrintEnvCommand, env_changes,
no_clear_environ, no_clone_flags));
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
// Test a non-trival value for clone_flags.
EXPECT_EQ("wibble", TestLaunchProcess(kPrintEnvCommand, env_changes,
no_clear_environ, CLONE_FS));
#endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
EXPECT_EQ("wibble",
TestLaunchProcess(kPrintEnvCommand, env_changes,
true /* clear_environ */, no_clone_flags));
env_changes.clear();
EXPECT_EQ("", TestLaunchProcess(kPrintEnvCommand, env_changes,
true /* clear_environ */, no_clone_flags));
}
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
MULTIPROCESS_TEST_MAIN(CheckPidProcess) {
const pid_t kInitPid = 1;
const pid_t pid = syscall(__NR_getpid);
CHECK(pid == kInitPid);
CHECK(getpid() == pid);
return kSuccess;
}
#if defined(CLONE_NEWUSER) && defined(CLONE_NEWPID)
TEST_F(ProcessUtilTest, CloneFlags) {
if (!PathExists(FilePath("/proc/self/ns/user")) ||
!PathExists(FilePath("/proc/self/ns/pid"))) {
// User or PID namespaces are not supported.
return;
}
LaunchOptions options;
options.clone_flags = CLONE_NEWUSER | CLONE_NEWPID;
Process process(SpawnChildWithOptions("CheckPidProcess", options));
ASSERT_TRUE(process.IsValid());
int exit_code = 42;
EXPECT_TRUE(process.WaitForExit(&exit_code));
EXPECT_EQ(kSuccess, exit_code);
}
#endif // defined(CLONE_NEWUSER) && defined(CLONE_NEWPID)
TEST(ForkWithFlagsTest, UpdatesPidCache) {
// Warm up the libc pid cache, if there is one.
ASSERT_EQ(syscall(__NR_getpid), getpid());
pid_t ctid = 0;
const pid_t pid = ForkWithFlags(SIGCHLD | CLONE_CHILD_SETTID, nullptr, &ctid);
if (pid == 0) {
// In child. Check both the raw getpid syscall and the libc getpid wrapper
// (which may rely on a pid cache).
RAW_CHECK(syscall(__NR_getpid) == ctid);
RAW_CHECK(getpid() == ctid);
_exit(kSuccess);
}
ASSERT_NE(-1, pid);
int status = 42;
ASSERT_EQ(pid, HANDLE_EINTR(waitpid(pid, &status, 0)));
ASSERT_TRUE(WIFEXITED(status));
EXPECT_EQ(kSuccess, WEXITSTATUS(status));
}
TEST_F(ProcessUtilTest, InvalidCurrentDirectory) {
LaunchOptions options;
options.current_directory = FilePath("/dev/null");
Process process(SpawnChildWithOptions("SimpleChildProcess", options));
ASSERT_TRUE(process.IsValid());
int exit_code = kSuccess;
EXPECT_TRUE(process.WaitForExit(&exit_code));
EXPECT_NE(kSuccess, exit_code);
}
#endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
#if BUILDFLAG(IS_MAC)
constexpr MachPortsForRendezvous::key_type kTestChildRendezvousKey = 'test';
MULTIPROCESS_TEST_MAIN(
ProcessUtilTest_ProcessRequirement_ChildFailedValidation) {
auto* rendezvous_client = MachPortRendezvousClient::GetInstance();
CHECK(rendezvous_client);
// The parent process specifies a requirement that does not matchy any
// process. No ports will be available to this process.
CHECK_EQ(0u, rendezvous_client->GetPortCount());
return 0;
}
// Tests that a `ProcessRequirement` passed via `LaunchOptions` is applied
// during Mach port rendezvous with a child process. Tests of the validation
// behavior can be found in //base/apple/mach_port_rendezvous_unittest.cc.
TEST_F(ProcessUtilTest, ProcessRequirement) {
// TODO(crbug.com/362302761): Remove once peer validation is enforced by
// default.
test::ScopedFeatureList scoped_feature_list(
kMachPortRendezvousEnforcePeerRequirements);
apple::ScopedMachReceiveRight port;
kern_return_t kr =
mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE,
apple::ScopedMachReceiveRight::Receiver(port).get());
ASSERT_EQ(kr, KERN_SUCCESS);
MachRendezvousPort rendezvous_port(port.get(), MACH_MSG_TYPE_MAKE_SEND);
// One Mach port is registered, but the process requirement should prevent
// the child from retrieving it. Test assertions are in the child process,
// above.
LaunchOptions options;
options.mach_ports_for_rendezvous[kTestChildRendezvousKey] = rendezvous_port;
options.process_requirement =
mac::ProcessRequirement::NeverMatchesForTesting();
Process child = SpawnChildWithOptions(
"ProcessUtilTest_ProcessRequirement_ChildFailedValidation", options);
int exit_code;
ASSERT_TRUE(WaitForMultiprocessTestChildExit(
child, TestTimeouts::action_timeout(), &exit_code));
// The child exiting successfully indicates that its test assertions held.
EXPECT_EQ(0, exit_code);
}
#endif
} // namespace base