From 473b9dd442e452fbcfac352c0ae0a586f019a8f2 Mon Sep 17 00:00:00 2001 From: zhoujiapeng Date: Sun, 17 Sep 2023 00:12:16 +0800 Subject: [PATCH] [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 --- bolt/lib/Rewrite/MachORewriteInstance.cpp | 6 ++++-- bolt/lib/Rewrite/RewriteInstance.cpp | 5 ++++- bolt/test/permission.test | 13 +++++++++++++ 3 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 bolt/test/permission.test diff --git a/bolt/lib/Rewrite/MachORewriteInstance.cpp b/bolt/lib/Rewrite/MachORewriteInstance.cpp index 8214cade8280..b827a196c826 100644 --- a/bolt/lib/Rewrite/MachORewriteInstance.cpp +++ b/bolt/lib/Rewrite/MachORewriteInstance.cpp @@ -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::all_all & + ~sys::fs::getUmask())); check_error(EC, "cannot set permissions of output file"); } diff --git a/bolt/lib/Rewrite/RewriteInstance.cpp b/bolt/lib/Rewrite/RewriteInstance.cpp index ea785e335df0..3a22203b6f0e 100644 --- a/bolt/lib/Rewrite/RewriteInstance.cpp +++ b/bolt/lib/Rewrite/RewriteInstance.cpp @@ -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::all_all & + ~sys::fs::getUmask())); check_error(EC, "cannot set permissions of output file"); } diff --git a/bolt/test/permission.test b/bolt/test/permission.test new file mode 100644 index 000000000000..a5a98599eb83 --- /dev/null +++ b/bolt/test/permission.test @@ -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