Skip to content

Commit fb441ac

Browse files
author
Your Name
committed
Add DSMIL build option and port DSMIL passes
1 parent e668e91 commit fb441ac

27 files changed

+197
-102
lines changed

dsmil/CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
cmake_minimum_required(VERSION 3.20.0)
2+
3+
project(DSMIL LANGUAGES C CXX)
4+
5+
include(GNUInstallDirs)
6+
include(AddLLVM)
7+
8+
add_subdirectory(lib)
9+
10+
install(
11+
DIRECTORY include/
12+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/dsmil
13+
FILES_MATCHING
14+
PATTERN "*.h"
15+
)

dsmil/lib/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
add_subdirectory(Passes)
2+
add_subdirectory(Runtime)

dsmil/lib/Passes/CMakeLists.txt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
set(DSMIL_PASS_SOURCES
2+
DsmilBFTPass.cpp
3+
DsmilBlueRedPass.cpp
4+
DsmilConstantTimePass.cpp
5+
DsmilCrossDomainPass.cpp
6+
DsmilEdgeSecurityPass.cpp
7+
DsmilFuzzExportPass.cpp
8+
DsmilJADC2Pass.cpp
9+
DsmilMPEPass.cpp
10+
DsmilMissionPolicyPass.cpp
11+
DsmilNuclearSuretyPass.cpp
12+
DsmilRadioBridgePass.cpp
13+
DsmilStealthPass.cpp
14+
DsmilTelemetryCheckPass.cpp
15+
DsmilThreatSignaturePass.cpp
16+
)
17+
18+
add_llvm_library(DsmilPasses MODULE
19+
${DSMIL_PASS_SOURCES}
20+
PLUGIN_TOOL opt
21+
)
22+
23+
target_include_directories(DsmilPasses PRIVATE
24+
${LLVM_MAIN_SRC_DIR}/../dsmil/include
25+
)
26+
27+
set_target_properties(DsmilPasses PROPERTIES
28+
OUTPUT_NAME "DsmilPasses"
29+
)
30+
31+
install(TARGETS DsmilPasses
32+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
33+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
34+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
35+
)

dsmil/lib/Passes/DsmilBFTPass.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232
#include "llvm/IR/Instructions.h"
3333
#include "llvm/IR/IRBuilder.h"
3434
#include "llvm/IR/Attributes.h"
35-
#include "llvm/Pass.h"
35+
#include "llvm/IR/PassManager.h"
36+
#include "llvm/Passes/PassBuilder.h"
37+
#include "llvm/Passes/PassPlugin.h"
3638
#include "llvm/Support/raw_ostream.h"
3739
#include <unordered_map>
3840
#include <string>
@@ -217,7 +219,7 @@ void DsmilBFTPass::insertBFTCall(Function *F, BFTUpdateType Type) {
217219
BFTFunc = M->getOrInsertFunction(
218220
"dsmil_bft_send_status",
219221
Type::getInt32Ty(Ctx),
220-
Type::getInt8PtrTy(Ctx)
222+
PointerType::get(Type::getInt8Ty(Ctx), 0)
221223
);
222224
break;
223225

@@ -226,7 +228,7 @@ void DsmilBFTPass::insertBFTCall(Function *F, BFTUpdateType Type) {
226228
BFTFunc = M->getOrInsertFunction(
227229
"dsmil_bft_send_friendly",
228230
Type::getInt32Ty(Ctx),
229-
Type::getInt8PtrTy(Ctx)
231+
PointerType::get(Type::getInt8Ty(Ctx), 0)
230232
);
231233
break;
232234

dsmil/lib/Passes/DsmilBlueRedPass.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include "llvm/Support/Debug.h"
2323
#include "llvm/Support/JSON.h"
2424
#include "llvm/Support/raw_ostream.h"
25+
#include "llvm/Passes/PassBuilder.h"
26+
#include "llvm/Passes/PassPlugin.h"
2527
#include <map>
2628
#include <set>
2729
#include <string>
@@ -204,12 +206,9 @@ class DsmilBlueRedPass : public PassInfoMixin<DsmilBlueRedPass> {
204206
IRBuilder<> Builder(&Entry, Entry.getFirstInsertionPt());
205207

206208
// Create call to dsmil_red_log(hook_name, function_name)
209+
auto *I8Ptr = PointerType::get(Type::getInt8Ty(Ctx), 0);
207210
FunctionCallee RedLogFunc = M->getOrInsertFunction(
208-
"dsmil_red_log",
209-
Type::getVoidTy(Ctx),
210-
Type::getInt8PtrTy(Ctx), // hook_name
211-
Type::getInt8PtrTy(Ctx) // function_name
212-
);
211+
"dsmil_red_log", Type::getVoidTy(Ctx), I8Ptr, I8Ptr);
213212

214213
Value *HookNameStr = Builder.CreateGlobalStringPtr(HookName);
215214
Value *FuncNameStr = Builder.CreateGlobalStringPtr(F.getName());
@@ -274,11 +273,9 @@ class DsmilBlueRedPass : public PassInfoMixin<DsmilBlueRedPass> {
274273
IRBuilder<> Builder(&Entry, Entry.getFirstInsertionPt());
275274

276275
// Create call to dsmil_red_scenario(vuln_type)
276+
auto *I8Ptr = PointerType::get(Type::getInt8Ty(Ctx), 0);
277277
FunctionCallee ScenarioFunc = M->getOrInsertFunction(
278-
"dsmil_red_scenario",
279-
Type::getInt1Ty(Ctx), // Returns bool
280-
Type::getInt8PtrTy(Ctx) // scenario_name
281-
);
278+
"dsmil_red_scenario", Type::getInt1Ty(Ctx), I8Ptr);
282279

283280
Value *VulnTypeStr = Builder.CreateGlobalStringPtr(VulnType);
284281
Value *ShouldInject = Builder.CreateCall(ScenarioFunc, {VulnTypeStr});
@@ -385,7 +382,7 @@ class DsmilBlueRedPass : public PassInfoMixin<DsmilBlueRedPass> {
385382
std::error_code EC;
386383
raw_fd_ostream OS(OutputPath, EC);
387384
if (!EC) {
388-
OS << formatv("{0:2}", Value(std::move(Report)));
385+
OS << formatv("{0:2}", json::Value(std::move(Report)));
389386
OS.close();
390387
}
391388
}

dsmil/lib/Passes/DsmilConstantTimePass.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include "llvm/IR/IntrinsicInst.h"
2222
#include "llvm/IR/Module.h"
2323
#include "llvm/IR/PassManager.h"
24+
#include "llvm/Passes/PassBuilder.h"
25+
#include "llvm/Passes/PassPlugin.h"
2426
#include "llvm/Support/CommandLine.h"
2527
#include "llvm/Support/Debug.h"
2628
#include "llvm/Support/raw_ostream.h"
@@ -349,8 +351,9 @@ class DsmilConstantTimePass : public PassInfoMixin<DsmilConstantTimePass> {
349351

350352
// Shifting BY a secret amount is timing-dependent
351353
if (isSecretValue(BO->getOperand(1))) {
352-
std::string Msg = "Variable-time instruction: shift by secret amount " +
353-
"(shift timing may depend on shift count)";
354+
std::string Msg =
355+
std::string("Variable-time instruction: shift by secret amount ") +
356+
"(shift timing may depend on shift count)";
354357
Violations.emplace_back(ViolationType::VariableTimeInstruction,
355358
F.getName(), BO, Msg);
356359
}

dsmil/lib/Passes/DsmilCrossDomainPass.cpp

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,12 @@
3030
#include "llvm/IR/Instructions.h"
3131
#include "llvm/IR/IRBuilder.h"
3232
#include "llvm/IR/Attributes.h"
33-
#include "llvm/Pass.h"
33+
#include "llvm/IR/PassManager.h"
34+
#include "llvm/Passes/PassBuilder.h"
35+
#include "llvm/Passes/PassPlugin.h"
3436
#include "llvm/Support/raw_ostream.h"
3537
#include "llvm/Support/JSON.h"
38+
#include "llvm/ADT/SmallVector.h"
3639
#include <unordered_map>
3740
#include <unordered_set>
3841
#include <string>
@@ -78,6 +81,15 @@ std::string classificationToString(ClassificationLevel Level) {
7881
}
7982
}
8083

84+
struct PairHash {
85+
template <class T1, class T2>
86+
std::size_t operator()(const std::pair<T1, T2> &p) const {
87+
auto h1 = std::hash<T1>{}(p.first);
88+
auto h2 = std::hash<T2>{}(p.second);
89+
return h1 ^ (h2 << 1);
90+
}
91+
};
92+
8193
// Cross-domain transition record
8294
struct CrossDomainTransition {
8395
Function *Caller;
@@ -107,15 +119,6 @@ class DsmilCrossDomainPass : public PassInfoMixin<DsmilCrossDomainPass> {
107119
unsigned NumUnsafeCalls = 0;
108120
unsigned NumGuardsInserted = 0;
109121

110-
struct PairHash {
111-
template <class T1, class T2>
112-
std::size_t operator()(const std::pair<T1, T2> &p) const {
113-
auto h1 = std::hash<T1>{}(p.first);
114-
auto h2 = std::hash<T2>{}(p.second);
115-
return h1 ^ (h2 << 1);
116-
}
117-
};
118-
119122
public:
120123
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
121124

@@ -322,15 +325,16 @@ bool DsmilCrossDomainPass::insertCrossDomainGuards(Module &M) {
322325
bool Modified = false;
323326

324327
// Get or create guard runtime function
325-
FunctionType *GuardFT = FunctionType::get(
326-
Type::getInt32Ty(M.getContext()),
327-
{Type::getInt8PtrTy(M.getContext()), // data
328-
Type::getInt64Ty(M.getContext()), // length
329-
Type::getInt8PtrTy(M.getContext()), // from_level
330-
Type::getInt8PtrTy(M.getContext()), // to_level
331-
Type::getInt8PtrTy(M.getContext())}, // policy
332-
false
333-
);
328+
auto *I8Ptr = PointerType::get(Type::getInt8Ty(M.getContext()), 0);
329+
SmallVector<Type *, 5> ParamTys{
330+
I8Ptr, // data
331+
Type::getInt64Ty(M.getContext()), // length
332+
I8Ptr, // from_level
333+
I8Ptr, // to_level
334+
I8Ptr // policy
335+
};
336+
FunctionType *GuardFT =
337+
FunctionType::get(Type::getInt32Ty(M.getContext()), ParamTys, false);
334338

335339
FunctionCallee GuardFunc = M.getOrInsertFunction(
336340
"dsmil_cross_domain_guard", GuardFT);

dsmil/lib/Passes/DsmilEdgeSecurityPass.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@
4040
#include "llvm/IR/Instructions.h"
4141
#include "llvm/IR/IRBuilder.h"
4242
#include "llvm/IR/Attributes.h"
43-
#include "llvm/Pass.h"
43+
#include "llvm/IR/PassManager.h"
44+
#include "llvm/Passes/PassBuilder.h"
45+
#include "llvm/Passes/PassPlugin.h"
4446
#include "llvm/Support/raw_ostream.h"
4547
#include <unordered_map>
4648
#include <unordered_set>

dsmil/lib/Passes/DsmilFuzzExportPass.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include "llvm/IR/Type.h"
2323
#include "llvm/IR/DerivedTypes.h"
2424
#include "llvm/Pass.h"
25+
#include "llvm/Passes/PassBuilder.h"
26+
#include "llvm/Passes/PassPlugin.h"
2527
#include "llvm/Support/CommandLine.h"
2628
#include "llvm/Support/Debug.h"
2729
#include "llvm/Support/FileSystem.h"
@@ -133,12 +135,7 @@ class DsmilFuzzExportPass : public PassInfoMixin<DsmilFuzzExportPass> {
133135
} else if (Ty->isDoubleTy()) {
134136
return "double";
135137
} else if (Ty->isPointerTy()) {
136-
Type *ElementTy = Ty->getPointerElementType();
137-
if (ElementTy->isIntegerTy(8)) {
138-
return "bytes"; // uint8_t* = byte buffer
139-
} else {
140-
return typeToString(ElementTy) + "*";
141-
}
138+
return "pointer";
142139
} else if (Ty->isStructTy()) {
143140
return "struct";
144141
} else if (Ty->isArrayTy()) {

dsmil/lib/Passes/DsmilJADC2Pass.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@
3131
#include "llvm/IR/Instructions.h"
3232
#include "llvm/IR/IRBuilder.h"
3333
#include "llvm/IR/Attributes.h"
34+
#include "llvm/IR/PassManager.h"
3435
#include "llvm/Pass.h"
36+
#include "llvm/Passes/PassBuilder.h"
37+
#include "llvm/Passes/PassPlugin.h"
3538
#include "llvm/Support/raw_ostream.h"
3639
#include "llvm/Analysis/LoopInfo.h"
3740
#include "llvm/Analysis/CallGraph.h"

0 commit comments

Comments
 (0)