Skip to content

Commit

Permalink
add declang codes
Browse files Browse the repository at this point in the history
  • Loading branch information
funa-tk committed Oct 19, 2023
1 parent fbc0b6e commit a74c318
Show file tree
Hide file tree
Showing 14 changed files with 231 additions and 16 deletions.
1 change: 0 additions & 1 deletion .github/workflows/repo-lockdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ permissions:
jobs:
action:
runs-on: ubuntu-latest
if: github.repository == 'llvm/llvm-project'
steps:
- uses: dessant/repo-lockdown@v2
with:
Expand Down
1 change: 1 addition & 0 deletions AntiHackOSS/src/Flattening.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "llvm/Transforms/AntiHack/Utils.h"
#include "llvm/Transforms/Scalar.h"
#include "llvm/IR/ValueSymbolTable.h"
#include "llvm/IR/Constants.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/IRReader/IRReader.h"
#include "llvm/Transforms/Utils/Cloning.h"
Expand Down
3 changes: 2 additions & 1 deletion AntiHackOSS/src/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
#include "llvm/Transforms/AntiHack/Utils.h"
#include "llvm/Support/Regex.h"
#include "llvm/Support/raw_ostream.h"
#include <sstream>
#include "llvm/IR/Module.h"
#include "llvm/IR/Constants.h"
#include <sstream>

// Shamefully borrowed from ../Scalar/RegToMem.cpp :(
bool valueEscapes(Instruction *Inst) {
Expand Down
16 changes: 16 additions & 0 deletions clang/lib/CodeGen/BackendUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,18 @@
using namespace clang;
using namespace llvm;

//DECLANG CODES BEGIN
#include "llvm/Transforms/AntiHack/AntiHack.h"
static cl::opt<bool>
FinalizerOpt("no-finalizer", cl::init(false), cl::Hidden,
cl::ZeroOrMore, cl::desc("no finalizer option"));

static cl::opt<std::string>
AntiHackOpt("antihack", cl::init(""), cl::Hidden,
cl::ZeroOrMore, cl::desc("antihack pass"));
//DECLANG CODES END


#define HANDLE_EXTENSION(Ext) \
llvm::PassPluginLibraryInfo get##Ext##PluginInfo();
#include "llvm/Support/Extension.def"
Expand Down Expand Up @@ -1027,6 +1039,10 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
break;
}

//DECLANG CODES BEGIN
MPM.addPass(AntiHack(AntiHackOpt));
//DECLANG CODES END

// Now that we have all of the passes ready, run them.
{
PrettyStackTraceString CrashInfo("Optimizer");
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/Driver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ add_clang_library(clangDriver
ToolChains/ZOS.cpp
Types.cpp
XRayArgs.cpp
DeClangExtraProcess.cpp
ToolChains/DeClangAndroidNdk.cpp


DEPENDS
ClangDriverOptions
Expand Down
87 changes: 86 additions & 1 deletion clang/lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ using namespace clang::driver;
using namespace clang;
using namespace llvm::opt;

//DECLANG CODES BEGIN
extern void DeClangExtraProcess(const Compilation &C, const std::string& homeDir, llvm::raw_fd_ostream *logFile);
//DECLANG CODES END

static llvm::Optional<llvm::Triple>
getOffloadTargetTriple(const Driver &D, const ArgList &Args) {
auto OffloadTargets = Args.getAllArgValues(options::OPT_offload_EQ);
Expand Down Expand Up @@ -1174,6 +1178,40 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {

// FIXME: What are we going to do with -V and -b?

//DECLANG CODES BEGIN
std::vector<const char*> modArgs = ArgList.vec();
modArgs.push_back("-DDECLANG");
// remove --gc-sections and --no-undefined flags if libil2cpp.so is linked on unity
bool linkIl2cpp = false;
for (std::vector<const char*>::iterator it = modArgs.begin() ; it != modArgs.end(); ++it) {
if (std::string("-o") == *it) {
++it;
std::string execFilename(llvm::sys::path::filename(*it).data());
if (execFilename == "libil2cpp.so") {
linkIl2cpp = true;
break;
}
}
}
if (linkIl2cpp) {
for (std::vector<const char*>::iterator it = modArgs.begin() ; it != modArgs.end();) {
if (std::string("--gc-sections") == *it) {
it = modArgs.erase(it);
} else if (std::string("-Wl,--gc-sections") == *it) {
it = modArgs.erase(it);
} else if (std::string("--no-undefined") == *it) {
it = modArgs.erase(it);
} else if (std::string("-Wl,--no-undefined") == *it) {
it = modArgs.erase(it);
} else {
++it;
}
}
}
ArgList = ArrayRef<const char*>(modArgs);
//DECLANG CODES END


// Arguments specified in command line.
bool ContainsError;
CLOptions = std::make_unique<InputArgList>(
Expand Down Expand Up @@ -1819,11 +1857,48 @@ int Driver::ExecuteCompilation(
for (auto &Job : C.getJobs())
setUpResponseFiles(C, Job);

//DECLANG CODES BEGIN
char* home_dir = getenv("DECLANG_HOME");
if (home_dir == nullptr) {
home_dir = getenv("HOME");
}
if (home_dir == nullptr) {
home_dir = getenv("USERPROFILE");
}
if (home_dir == nullptr) {
llvm::errs() << "[Linker]: (Warning) Cannot find $DECLANG_HOME, $HOME or %USERPROFILE%\n";
llvm::errs().flush();
std::exit(EXIT_FAILURE);
}
std::string homeDir(home_dir);
homeDir = llvm::sys::path::convert_to_slash(homeDir);

//log path
std::string logPath = homeDir + "/.DeClang/log.txt";
llvm::raw_fd_ostream *logFile;
std::error_code EC;
logFile = new llvm::raw_fd_ostream(logPath, EC, llvm::sys::fs::OF_Append);
if (EC) {
llvm::errs() << "[Linker]: Open logFile Failed. Check DECLANG_HOME maybe?: " << EC.message() << " " << logPath << "\n";
llvm::errs().flush();
}

for (Command& cmd : C.getJobs() ) {
cmd.Print(*logFile, "\n", /*Quote=*/true);
logFile->flush();
}
//DECLANG CODES END


C.ExecuteJobs(C.getJobs(), FailingCommands);

// If the command succeeded, we are done.
if (FailingCommands.empty())
if (FailingCommands.empty()) {
//DECLANG CODES BEGIN
DeClangExtraProcess(C, homeDir, logFile);
//DECLANG CODES END
return 0;
}

// Otherwise, remove result files and print extra information about abnormal
// failures.
Expand Down Expand Up @@ -4797,6 +4872,16 @@ void Driver::BuildJobs(Compilation &C) const {
continue;
}

//DECLANG CODES BEGIN
//ignore -DDECLANG to be warned
if (A->getNumValues() != 0) {
std::string valStr = A->getValue();
if (valStr == "DECLANG") {
continue;
}
}
//DECLANG CODES END

// In clang-cl, don't mention unknown arguments here since they have
// already been warned about.
if (!IsCLMode() || !A->getOption().matches(options::OPT_UNKNOWN))
Expand Down
22 changes: 14 additions & 8 deletions clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3682,14 +3682,20 @@ static void RenderModulesOptions(Compilation &C, const Driver &D,
// Users can pass -fno-cxx-modules to turn off modules support for
// C++/Objective-C++ programs.
bool HaveClangModules = false;
if (Args.hasFlag(options::OPT_fmodules, options::OPT_fno_modules, false)) {
bool AllowedInCXX = Args.hasFlag(options::OPT_fcxx_modules,
options::OPT_fno_cxx_modules, true);
if (AllowedInCXX || !types::isCXX(Input.getType())) {
CmdArgs.push_back("-fmodules");
HaveClangModules = true;
}
}

//DECLANG CODES START
//if (Args.hasFlag(options::OPT_fmodules, options::OPT_fno_modules, false)) {
// bool AllowedInCXX = Args.hasFlag(options::OPT_fcxx_modules,
// options::OPT_fno_cxx_modules, true);
// if (AllowedInCXX || !types::isCXX(Input.getType())) {
// CmdArgs.push_back("-fmodules");
// HaveClangModules = true;
// }
//}
// unused arguments errorを回避
Args.ClaimAllArgs(options::OPT_fmodules);
Args.ClaimAllArgs(options::OPT_fmodules_cache_path);
//DECLANG CODES END

HaveModules |= HaveClangModules;
if (Args.hasArg(options::OPT_fmodules_ts)) {
Expand Down
43 changes: 40 additions & 3 deletions clang/lib/Driver/ToolChains/CommonArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@
#include "llvm/Support/VirtualFileSystem.h"
#include "llvm/Support/YAMLParser.h"

//DECLANG CODES BEGIN
#include "DeClangAndroidNdk.h"
#include <filesystem>
//DECLANG CODES END

using namespace clang::driver;
using namespace clang::driver::tools;
using namespace clang;
Expand Down Expand Up @@ -226,6 +231,12 @@ void tools::AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs,
// (constructed via -Xarch_).
Args.AddAllArgValues(CmdArgs, options::OPT_Zlinker_input);

//DECLANG CODES BEGIN
if (TC.getTriple().isAndroid()) {
CmdArgs.push_back("-Bsymbolic");
}
//DECLANG CODES END

// LIBRARY_PATH are included before user inputs and only supported on native
// toolchains.
if (!TC.isCrossCompiling())
Expand Down Expand Up @@ -1570,13 +1581,27 @@ static LibGccType getLibGccType(const ToolChain &TC, const Driver &D,
static void AddUnwindLibrary(const ToolChain &TC, const Driver &D,
ArgStringList &CmdArgs, const ArgList &Args) {
ToolChain::UnwindLibType UNW = TC.GetUnwindLibType(Args);

//DECLANG CODES BEGIN
LibGccType LGT = getLibGccType(TC, D, Args);
if (TC.getTriple().isAndroid() && getNdkVersion(D.getInstalledDir()) >= 23) {
if (LGT == LibGccType::StaticLibGcc) {
CmdArgs.push_back("-l:libunwind.a");
} else {
CmdArgs.push_back("-l:libunwind.so");
}
return;
}
//DECLANG CODES END

// Targets that don't use unwind libraries.
if ((TC.getTriple().isAndroid() && UNW == ToolChain::UNW_Libgcc) ||
TC.getTriple().isOSIAMCU() || TC.getTriple().isOSBinFormatWasm() ||
TC.getTriple().isWindowsMSVCEnvironment() || UNW == ToolChain::UNW_None)
return;

LibGccType LGT = getLibGccType(TC, D, Args);
//DECLANG CODES BEGIN
//LibGccType LGT = getLibGccType(TC, D, Args);
//DECLANG CODES END
bool AsNeeded = LGT == LibGccType::UnspecifiedLibGcc &&
(UNW == ToolChain::UNW_CompilerRT || !D.CCCIsCXX()) &&
!TC.getTriple().isAndroid() &&
Expand All @@ -1601,7 +1626,11 @@ static void AddUnwindLibrary(const ToolChain &TC, const Driver &D,
if (LGT != LibGccType::StaticLibGcc)
CmdArgs.push_back("-lunwind");
} else if (LGT == LibGccType::StaticLibGcc) {
CmdArgs.push_back("-l:libunwind.a");
//DECLANG CODES BEGIN
if (!TC.getTriple().isAndroid()) {
CmdArgs.push_back("-l:libunwind.a");
}
//DECLANG CODES END
} else if (LGT == LibGccType::SharedLibGcc) {
if (TC.getTriple().isOSCygMing())
CmdArgs.push_back("-l:libunwind.dll.a");
Expand Down Expand Up @@ -1642,6 +1671,14 @@ void tools::AddRunTimeLibs(const ToolChain &TC, const Driver &D,
AddUnwindLibrary(TC, D, CmdArgs, Args);
break;
case ToolChain::RLT_Libgcc:
//DECLANG CODES BEGIN
if (TC.getTriple().isAndroid() && getNdkVersion(D.getInstalledDir()) >= 23) {
CmdArgs.push_back(TC.getCompilerRTArgString(Args, "builtins"));
AddUnwindLibrary(TC, D, CmdArgs, Args);
break;
}
//DECLANG CODES END

// Make sure libgcc is not used under MSVC environment by default
if (TC.getTriple().isKnownWindowsMSVCEnvironment()) {
// Issue error diagnostic if libgcc is explicitly specified
Expand Down
6 changes: 4 additions & 2 deletions clang/lib/Driver/ToolChains/Darwin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1218,8 +1218,10 @@ DarwinClang::DarwinClang(const Driver &D, const llvm::Triple &Triple,

void DarwinClang::addClangWarningOptions(ArgStringList &CC1Args) const {
// Always error about undefined 'TARGET_OS_*' macros.
CC1Args.push_back("-Wundef-prefix=TARGET_OS_");
CC1Args.push_back("-Werror=undef-prefix");
//DECLANG CODES START
//CC1Args.push_back("-Wundef-prefix=TARGET_OS_");
//CC1Args.push_back("-Werror=undef-prefix");
//DECLANG CODES END

// For modern targets, promote certain warnings to errors.
if (isTargetWatchOSBased() || getTriple().isArch64Bit()) {
Expand Down
52 changes: 52 additions & 0 deletions clang/lib/Driver/ToolChains/DeClangAndroidNdk.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <filesystem>
#include <fstream>
#include <sstream>
#include <regex>

static int version = -1;

int getNdkVersion(const char *path) {
if (version != -1) {
return version;
}

std::filesystem::path abs_path = std::filesystem::canonical(path);
std::filesystem::path root_path = abs_path.root_directory();

for (std::filesystem::path dir = abs_path.parent_path(); dir.compare(root_path) != 0; dir = dir.parent_path()) {
for (const std::filesystem::directory_entry& file : std::filesystem::directory_iterator(dir)) {
if (file.is_regular_file()) {
if (file.path().filename().compare("source.properties") == 0) {
std::ifstream version_file(file.path(), std::ios::in);
std::string line;
while (std::getline(version_file, line)) {
std::stringstream ss(line);
std::string key;
while (std::getline(ss, key, '=')) {
key = std::regex_replace(key, std::regex("^ *"), "");
key = std::regex_replace(key, std::regex(" *$"), "");
if (key == "Pkg.Revision") {
std::string value;
std::getline(ss, value, '=');
value = std::regex_replace(value, std::regex("^ *"), "");
value = std::regex_replace(value, std::regex(" *$"), "");
std::smatch m;
if (std::regex_match(value, m, std::regex(R"(^(\d+).+)"))) {
version = stoi(m[1].str());
goto DONE;
}
}
}
}
}
}
}
}
DONE:
return version;
}

8 changes: 8 additions & 0 deletions clang/lib/Driver/ToolChains/DeClangAndroidNdk.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_DECLANGANDROIDNDK_H
#define LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_DECLANGANDROIDNDK_H


extern int getNdkVersion(const char *path);


#endif
2 changes: 2 additions & 0 deletions llvm/lib/Transforms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ add_subdirectory(Hello)
add_subdirectory(ObjCARC)
add_subdirectory(Coroutines)
add_subdirectory(CFGuard)
add_subdirectory(AntiHack)

1 change: 1 addition & 0 deletions llvm/lib/Transforms/IPO/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,5 @@ add_llvm_component_library(LLVMipo
Vectorize
Instrumentation
Scalar
AntiHack
)
Loading

0 comments on commit a74c318

Please sign in to comment.