[BOLT] Incorporate umask into the output file permission

Fix https://github.com/llvm/llvm-project/issues/65061

Reviewed By: maksfb, Amir

Differential Revision: https://reviews.llvm.org/D159407
This commit is contained in:
zhoujiapeng 2023-09-17 00:12:16 +08:00 committed by hezuoqiang
parent 16fd879980
commit 473b9dd442
3 changed files with 21 additions and 3 deletions

View File

@ -564,8 +564,10 @@ void MachORewriteInstance::rewriteFile() {
writeInstrumentationSection("I__literal16", OS);
Out->keep();
EC = sys::fs::setPermissions(opts::OutputFilename,
sys::fs::perms::all_all);
EC = sys::fs::setPermissions(
opts::OutputFilename,
static_cast<sys::fs::perms>(sys::fs::perms::all_all &
~sys::fs::getUmask()));
check_error(EC, "cannot set permissions of output file");
}

View File

@ -5366,7 +5366,10 @@ void RewriteInstance::rewriteFile() {
}
Out->keep();
EC = sys::fs::setPermissions(opts::OutputFilename, sys::fs::perms::all_all);
EC = sys::fs::setPermissions(
opts::OutputFilename,
static_cast<sys::fs::perms>(sys::fs::perms::all_all &
~sys::fs::getUmask()));
check_error(EC, "cannot set permissions of output file");
}

13
bolt/test/permission.test Normal file
View File

@ -0,0 +1,13 @@
# Ensure that the permissions of the optimized binary file comply with the
# system's umask.
# This test performs a logical AND operation on the results of the `stat -c %a
# %t.bolt` and `umask` commands (both results are displayed in octal), and
# checks whether the result is equal to 0.
REQUIRES: system-linux
RUN: %clang %cflags %p/Inputs/hello.c -o %t -Wl,-q
RUN: llvm-bolt %t -o %t.bolt
RUN: echo $(( 8#$(stat -c %a %t.bolt) & 8#$(umask) )) | FileCheck %s
CHECK: 0