-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
UBet specific adaptations #6553
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9fd4892
Adding the main components. No real integration yet.
hbrodin a66234e
Merge remote-tracking branch 'origin/master' into Henrik/ubetmerge
hbrodin bdd3158
Integrate control affecting dataflow logging
hbrodin f1fe70b
formatting
hbrodin 1e5759d
Python linting
hbrodin 993efd1
Unify naming of 'detail' (not 'details')
hbrodin 9590a15
Reviewer feedback: Simplify build step
hbrodin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
polytracker/include/polytracker/passes/tainted_control_flow.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright (c) 2023-present, Trail of Bits, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed in accordance with the terms specified in | ||
* the LICENSE file found in the root directory of this source tree. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <llvm/IR/InstVisitor.h> | ||
#include <llvm/IR/PassManager.h> | ||
#include <unordered_map> | ||
|
||
namespace polytracker { | ||
namespace detail { | ||
struct FunctionMappingJSONWriter; | ||
} | ||
|
||
class TaintedControlFlowPass | ||
: public llvm::PassInfoMixin<TaintedControlFlowPass>, | ||
public llvm::InstVisitor<TaintedControlFlowPass> { | ||
// | ||
llvm::IntegerType *label_ty{nullptr}; | ||
// Taint tracking startup | ||
llvm::FunctionCallee taint_start_fn; | ||
// Log taint label affecting control flow | ||
llvm::FunctionCallee cond_br_log_fn; | ||
// Log enter/leave functions | ||
llvm::FunctionCallee fn_enter_log_fn; | ||
llvm::FunctionCallee fn_leave_log_fn; | ||
|
||
// Helpers | ||
void insertCondBrLogCall(llvm::Instruction &inst, llvm::Value *val); | ||
void insertTaintStartupCall(llvm::Module &mod); | ||
void declareLoggingFunctions(llvm::Module &mod); | ||
|
||
llvm::ConstantInt *get_function_id_const(llvm::Function &f); | ||
llvm::ConstantInt *get_function_id_const(llvm::Instruction &i); | ||
|
||
public: | ||
using function_id = uint32_t; | ||
|
||
TaintedControlFlowPass(); | ||
hbrodin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
TaintedControlFlowPass(TaintedControlFlowPass &&); | ||
~TaintedControlFlowPass(); | ||
|
||
llvm::PreservedAnalyses run(llvm::Module &mod, | ||
llvm::ModuleAnalysisManager &mam); | ||
void visitGetElementPtrInst(llvm::GetElementPtrInst &gep); | ||
void visitBranchInst(llvm::BranchInst &bi); | ||
void visitSwitchInst(llvm::SwitchInst &si); | ||
void visitSelectInst(llvm::SelectInst &si); | ||
|
||
void instrumentFunctionEnter(llvm::Function &func); | ||
void visitReturnInst(llvm::ReturnInst &ri); | ||
|
||
function_id function_mapping(llvm::Function &func); | ||
|
||
std::unordered_map<uintptr_t, function_id> function_ids_; | ||
function_id function_counter_{0}; | ||
|
||
std::unique_ptr<detail::FunctionMappingJSONWriter> function_mapping_writer_; | ||
}; | ||
|
||
} // namespace polytracker |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
|
||
/* | ||
* Copyright (c) 2022-present, Trail of Bits, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed in accordance with the terms specified in | ||
* the LICENSE file found in the root directory of this source tree. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "taintdag/outputfile.h" | ||
#include "taintdag/section.h" | ||
#include "taintdag/taint.h" | ||
#include "taintdag/util.h" | ||
|
||
namespace taintdag { | ||
|
||
namespace details { | ||
hbrodin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// A uint32_t varint encoded by setting highest bit for all but the final byte. | ||
// Requires up to 5 bytes of storage as each output byte uses 7 input bits. | ||
// Total maximum need is floor(32/7) = 5. Returns number of bytes required. | ||
size_t varint_encode(uint32_t val, uint8_t *buffer) { | ||
hbrodin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
auto orig_buffer = buffer; | ||
while (val >= 0x80) { | ||
*buffer++ = 0x80 | (val & 0x7f); | ||
val >>= 7; | ||
} | ||
*buffer++ = val & 0x7f; | ||
return buffer - orig_buffer; | ||
} | ||
// TODO (hbrodin): Should probably used std::span | ||
} // namespace details | ||
|
||
struct ControlFlowLog : public SectionBase { | ||
enum EventType { | ||
EnterFunction = 0, | ||
LeaveFunction = 1, | ||
TaintedControlFlow = 2, | ||
}; | ||
|
||
static constexpr uint8_t tag{8}; | ||
static constexpr size_t align_of{1}; | ||
static constexpr size_t allocation_size{1024 * 1024 * 1024}; | ||
|
||
template <typename OF> | ||
ControlFlowLog(SectionArg<OF> of) : SectionBase(of.range) {} | ||
|
||
void function_event(EventType evt, uint32_t function_id) { | ||
surovic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
uint8_t buffer[6]; | ||
buffer[0] = static_cast<uint8_t>(evt); | ||
auto used = details::varint_encode(function_id, &buffer[1]); | ||
auto total = used + 1; | ||
|
||
if (auto wctx = write(total)) { | ||
std::copy(&buffer[0], &buffer[total], wctx->mem.begin()); | ||
} else { | ||
error_exit("Failed to write ", total, | ||
" bytes of output to the ControlFlowLog Section."); | ||
} | ||
} | ||
void enter_function(uint32_t function_id) { | ||
function_event(EnterFunction, function_id); | ||
} | ||
|
||
void leave_function(uint32_t function_id) { | ||
function_event(LeaveFunction, function_id); | ||
} | ||
|
||
void tainted_control_flow(label_t label, uint32_t function_id) { | ||
// 1 byte event, <= 5 bytes function id, <= 5 bytes label | ||
uint8_t buffer[11]; | ||
buffer[0] = static_cast<uint8_t>(TaintedControlFlow); | ||
auto used = details::varint_encode(function_id, &buffer[1]); | ||
auto total = used + 1; | ||
used = details::varint_encode(label, &buffer[total]); | ||
total += used; | ||
|
||
if (auto wctx = write(total)) { | ||
std::copy(&buffer[0], &buffer[total], wctx->mem.begin()); | ||
} else { | ||
error_exit("Failed to write ", total, | ||
" bytes of output to the ControlFlowLog Section."); | ||
} | ||
} | ||
}; | ||
|
||
} // namespace taintdag |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: I would just overwrite
bc_path
here with<path>.preopt.bc
or<path>opt.bc
and make a single_optimize_bitcode(...)
call.