Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/patchestry/AST/FunctionBuilder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ namespace patchestry::ast {
std::reference_wrapper< std::unordered_map< std::string, clang::VarDecl * > >
global_var_list;

bool is_stack_canary_operation(const Operation &op) const;

std::unordered_map< std::string, clang::VarDecl * > local_variables;
std::unordered_map< std::string, clang::LabelDecl * > labels_declaration;
std::unordered_map< std::string, clang::Stmt * > operation_stmts;
Expand Down
35 changes: 35 additions & 0 deletions lib/patchestry/AST/FunctionBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,36 @@ namespace patchestry::ast {
return stmt_vec;
}

/**
* @brief Detects whether an operation is part of compiler-inserted stack canary
* boilerplate (__stack_chk_guard load/compare, __stack_chk_fail call).
*/
bool FunctionBuilder::is_stack_canary_operation(const Operation &op) const {
auto is_canary_global = [this](const std::optional< std::string > &key) -> bool {
if (!key.has_value()) return false;
auto it = global_var_list.get().find(*key);
if (it == global_var_list.get().end()) return false;
const auto &name = it->second->getNameAsString();
return name == "__stack_chk_guard" || name == "___stack_chk_guard";
};

for (const auto &input : op.inputs) {
if (is_canary_global(input.global)) return true;
}

if (op.output.has_value() && is_canary_global(op.output->global)) return true;

if (op.target.has_value() && op.target->function.has_value()) {
auto it = function_list.get().find(*op.target->function);
if (it != function_list.get().end()) {
const auto &name = it->second->getNameAsString();
if (name == "__stack_chk_fail" || name == "___stack_chk_fail") return true;
}
}

return false;
}

/**
* @brief Generates a vector of `clang::Stmt*` representing the operations in a basic block.
*
Expand Down Expand Up @@ -727,6 +757,11 @@ namespace patchestry::ast {

const auto &operation = block.operations.at(operation_key);

// Skip compiler-inserted stack canary boilerplate
if (is_stack_canary_operation(operation)) {
continue;
}

// Save pending_materialized in case create_operation re-enters
// (e.g., create_temporary resolving a forward reference triggers
// nested operation building that appends to the same vector).
Expand Down
3 changes: 3 additions & 0 deletions test/patchir-decomp/bl_device__process_entry.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
// RUN: test -e %t.c
// RUN: %patchir-decomp -input %t.json -emit-cir -emit-mlir -emit-llvm -print-tu -output %t >> /dev/null 2>&1
// RUN: test -e %t.c
// RUN: %file-check -vv -check-prefix=CGEN %s --input-file %t.c
// CGEN: bl_device__process_entry
// CGEN-NOT: __stack_chk_fail
// RUN: %file-check -vv -check-prefix=CIR %s --input-file %t.cir
// CIR: cir.func @bl_device__process_entry
// RUN: %file-check -vv -check-prefix=MLIR %s --input-file %t.mlir
Expand Down
Loading