Skip to content

Commit 1233e2e

Browse files
committed
[Support] Don't set "all_exe" mode by default for file written by llvm::writeToOutput
Differential Revision: https://reviews.llvm.org/D153652
1 parent 71249fd commit 1233e2e

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

llvm/lib/Support/raw_ostream.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1007,7 +1007,7 @@ Error llvm::writeToOutput(StringRef OutputFileName,
10071007
return Write(Out);
10081008
}
10091009

1010-
unsigned Mode = sys::fs::all_read | sys::fs::all_write | sys::fs::all_exe;
1010+
unsigned Mode = sys::fs::all_read | sys::fs::all_write;
10111011
Expected<sys::fs::TempFile> Temp =
10121012
sys::fs::TempFile::create(OutputFileName + ".temp-stream-%%%%%%", Mode);
10131013
if (!Temp)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
## The Unix version of this test must use umask(1) because
2+
## llvm-dwarfutil respects the umask in setting output permissions.
3+
## Setting the umask to 0 ensures deterministic permissions across
4+
## test environments.
5+
# UNSUPPORTED: system-windows
6+
# REQUIRES: shell
7+
8+
# RUN: touch %t
9+
# RUN: chmod 0777 %t
10+
# RUN: ls -l %t | cut -f 1 -d ' ' > %t.0777
11+
# RUN: chmod 0666 %t
12+
# RUN: ls -l %t | cut -f 1 -d ' ' > %t.0666
13+
# RUN: chmod 0640 %t
14+
# RUN: ls -l %t | cut -f 1 -d ' ' > %t.0640
15+
16+
## Set umask to be permissive of all permissions,
17+
## only test mirroring of permissions.
18+
# RUN: umask 0
19+
20+
# RUN: yaml2obj %s -o %t
21+
22+
# RUN: chmod 0777 %t
23+
# RUN: llvm-dwarfutil --no-garbage-collection %t %t1
24+
# RUN: ls -l %t1 | cut -f 1 -d ' ' > %t1.perms
25+
# RUN: cmp %t1.perms %t.0777
26+
# RUN: llvm-dwarfutil --garbage-collection --separate-debug-file %t %t2
27+
# RUN: ls -l %t2 | cut -f 1 -d ' ' > %t2.perms
28+
# RUN: cmp %t2.perms %t.0777
29+
30+
# RUN: chmod 0666 %t
31+
# RUN: llvm-dwarfutil --no-garbage-collection %t %t1
32+
# RUN: ls -l %t1 | cut -f 1 -d ' ' > %t1.perms
33+
# RUN: cmp %t1.perms %t.0666
34+
# RUN: llvm-dwarfutil --garbage-collection --separate-debug-file %t %t2
35+
# RUN: ls -l %t2 | cut -f 1 -d ' ' > %t2.perms
36+
# RUN: cmp %t2.perms %t.0666
37+
38+
# RUN: chmod 0640 %t
39+
# RUN: llvm-dwarfutil --no-garbage-collection %t %t1
40+
# RUN: ls -l %t1 | cut -f 1 -d ' ' > %t1.perms
41+
# RUN: cmp %t1.perms %t.0640
42+
# RUN: llvm-dwarfutil --garbage-collection --separate-debug-file %t %t2
43+
# RUN: ls -l %t2 | cut -f 1 -d ' ' > %t2.perms
44+
# RUN: cmp %t2.perms %t.0640
45+
46+
--- !ELF
47+
FileHeader:
48+
Class: ELFCLASS64
49+
Data: ELFDATA2LSB
50+
Type: ET_EXEC
51+
Machine: EM_X86_64

llvm/unittests/Support/raw_ostream_test.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,31 @@ TEST(raw_ostreamTest, writeToOutputFile) {
504504
checkFileData(Path, "HelloWorld");
505505
}
506506

507+
#ifndef _WIN32
508+
TEST(raw_ostreamTest, filePermissions) {
509+
// Set umask to be permissive of all permissions.
510+
unsigned OldMask = ::umask(0);
511+
512+
llvm::unittest::TempDir RootTestDirectory("writToOutput", /*Unique*/ true);
513+
SmallString<128> Path(RootTestDirectory.path());
514+
sys::path::append(Path, "test.txt");
515+
516+
ASSERT_THAT_ERROR(writeToOutput(Path,
517+
[](raw_ostream &Out) -> Error {
518+
Out << "HelloWorld";
519+
return Error::success();
520+
}),
521+
Succeeded());
522+
523+
ErrorOr<llvm::sys::fs::perms> Perms = llvm::sys::fs::getPermissions(Path);
524+
ASSERT_TRUE(Perms) << "should be able to get permissions";
525+
// Verify the permission bits set by writeToOutput are read and write only.
526+
EXPECT_EQ(Perms.get(), llvm::sys::fs::all_read | llvm::sys::fs::all_write);
527+
528+
::umask(OldMask);
529+
}
530+
#endif
531+
507532
TEST(raw_ostreamTest, writeToNonexistingPath) {
508533
StringRef FileName = "/_bad/_path";
509534
std::string ErrorMessage = toString(createFileError(

0 commit comments

Comments
 (0)