Skip to content

Commit 835695a

Browse files
authored
Merge pull request #29 from CoreTrace/28-refactor-clean-up-codebase-and-reduce-duplication
28 refactor clean up codebase and reduce duplication
2 parents 506687a + 0819c14 commit 835695a

51 files changed

Lines changed: 7206 additions & 5142 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.github/workflows/commit-check.yml‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: Commit conventions
2+
3+
on:
4+
push:
5+
6+
jobs:
7+
commit_check:
8+
name: Check conventional commits
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v4
12+
with:
13+
fetch-depth: 0
14+
- name: Validate commit messages
15+
run: |
16+
python3 scripts/ci/commit_checker.py

‎.gitignore‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Prerequisites
2+
*.d
3+
4+
# Compiled Object files
5+
*.slo
6+
*.lo
7+
*.o
8+
*.obj
9+
10+
# Precompiled Headers
11+
*.gch
12+
*.pch
13+
14+
# Compiled Dynamic libraries
15+
*.so
16+
*.dylib
17+
*.dll
18+
19+
# Fortran module files
20+
*.mod
21+
*.smod
22+
23+
# Compiled Static libraries
24+
*.lai
25+
*.la
26+
*.a
27+
*.lib
28+
29+
# Executables
30+
*.exe
31+
*.out
32+
*.app
33+
34+
# Build directories
35+
/build/

‎.pre-commit-config.yaml‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
repos:
2+
- repo: local
3+
hooks:
4+
- id: conventional-commit-msg
5+
name: conventional commit message
6+
entry: python3 scripts/git/commit_msg_checker.py
7+
language: system
8+
stages: [commit-msg]
9+
pass_filenames: true

‎CMakeLists.txt‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,23 @@ option(BUILD_SHARED_LIB "Build shared library variant" ON)
4848
# ===========================
4949
set(STACK_ANALYZER_SOURCES
5050
src/StackUsageAnalyzer.cpp
51+
src/analysis/AllocaUsage.cpp
52+
src/analysis/AnalyzerUtils.cpp
53+
src/analysis/ConstParamAnalysis.cpp
54+
src/analysis/DynamicAlloca.cpp
55+
src/analysis/FunctionFilter.cpp
56+
src/analysis/IRValueUtils.cpp
57+
src/analysis/IntRanges.cpp
58+
src/analysis/InputPipeline.cpp
59+
src/analysis/InvalidBaseReconstruction.cpp
60+
src/analysis/MemIntrinsicOverflow.cpp
61+
src/analysis/SizeMinusKWrites.cpp
62+
src/analysis/StackBufferAnalysis.cpp
63+
src/analysis/StackComputation.cpp
64+
src/analysis/StackPointerEscape.cpp
65+
src/report/ReportSerialization.cpp
5166
src/mangle.cpp
67+
src/passes/ModulePasses.cpp
5268
)
5369

5470
include_directories(${LLVM_INCLUDE_DIRS})

‎include/StackUsageAnalyzer.hpp‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,13 @@ namespace ctrace::stack
106106
AllocaTooLarge = 8,
107107
AllocaUsageWarning = 9,
108108
InvalidBaseReconstruction = 10,
109-
ConstParameterNotModified = 11
109+
ConstParameterNotModified = 11,
110+
SizeMinusOneWrite = 12
110111
};
111112

112113
template <> struct EnumTraits<DescriptiveErrorCode>
113114
{
114-
static constexpr std::array<std::string_view, 12> names = {"None",
115+
static constexpr std::array<std::string_view, 13> names = {"None",
115116
"StackBufferOverflow",
116117
"NegativeStackIndex",
117118
"VLAUsage",
@@ -122,7 +123,8 @@ namespace ctrace::stack
122123
"AllocaTooLarge",
123124
"AllocaUsageWarning",
124125
"InvalidBaseReconstruction",
125-
"ConstParameterNotModified"};
126+
"ConstParameterNotModified",
127+
"SizeMinusOneWrite"};
126128
};
127129

128130
/*

‎include/analysis/AllocaUsage.hpp‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#pragma once
2+
3+
#include <functional>
4+
#include <set>
5+
#include <string>
6+
#include <vector>
7+
8+
#include "StackUsageAnalyzer.hpp"
9+
10+
namespace llvm
11+
{
12+
class AllocaInst;
13+
class DataLayout;
14+
class Function;
15+
class Module;
16+
} // namespace llvm
17+
18+
namespace ctrace::stack::analysis
19+
{
20+
struct AllocaUsageIssue
21+
{
22+
std::string funcName;
23+
std::string varName;
24+
const llvm::AllocaInst* allocaInst = nullptr;
25+
26+
bool userControlled = false; // size derived from argument / non-local value
27+
bool sizeIsConst = false; // size known exactly
28+
bool hasUpperBound = false; // bounded size (from ICmp-derived range)
29+
bool isRecursive = false; // function participates in a recursion cycle
30+
bool isInfiniteRecursive = false; // unconditional self recursion
31+
32+
StackSize sizeBytes = 0; // exact size in bytes (if sizeIsConst)
33+
StackSize upperBoundBytes = 0; // upper bound in bytes (if hasUpperBound)
34+
};
35+
36+
std::vector<AllocaUsageIssue>
37+
analyzeAllocaUsage(llvm::Module& mod, const llvm::DataLayout& DL,
38+
const std::set<const llvm::Function*>& recursiveFuncs,
39+
const std::set<const llvm::Function*>& infiniteRecursionFuncs,
40+
const std::function<bool(const llvm::Function&)>& shouldAnalyze);
41+
} // namespace ctrace::stack::analysis

‎include/analysis/AnalyzerUtils.hpp‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#pragma once
2+
3+
#include <string>
4+
5+
#include "StackUsageAnalyzer.hpp"
6+
#include "analysis/StackComputation.hpp"
7+
8+
namespace llvm
9+
{
10+
class Function;
11+
} // namespace llvm
12+
13+
namespace ctrace::stack::analysis
14+
{
15+
std::string formatFunctionNameForMessage(const std::string& name);
16+
17+
std::string getFunctionSourcePath(const llvm::Function& F);
18+
19+
bool getFunctionSourceLocation(const llvm::Function& F, unsigned& line, unsigned& column);
20+
21+
std::string buildMaxStackCallPath(const llvm::Function* F, const CallGraph& CG,
22+
const InternalAnalysisState& state);
23+
24+
bool shouldIncludePath(const std::string& path, const AnalysisConfig& config);
25+
26+
bool functionNameMatches(const llvm::Function& F, const AnalysisConfig& config);
27+
} // namespace ctrace::stack::analysis
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#pragma once
2+
3+
#include <functional>
4+
#include <string>
5+
#include <vector>
6+
7+
namespace llvm
8+
{
9+
class Function;
10+
class Module;
11+
} // namespace llvm
12+
13+
namespace ctrace::stack::analysis
14+
{
15+
struct ConstParamIssue
16+
{
17+
std::string funcName;
18+
std::string paramName;
19+
std::string currentType;
20+
std::string suggestedType;
21+
std::string suggestedTypeAlt;
22+
bool pointerConstOnly = false; // ex: T * const param
23+
bool isReference = false;
24+
bool isRvalueRef = false;
25+
unsigned line = 0;
26+
unsigned column = 0;
27+
};
28+
29+
std::vector<ConstParamIssue>
30+
analyzeConstParams(llvm::Module& mod,
31+
const std::function<bool(const llvm::Function&)>& shouldAnalyze);
32+
} // namespace ctrace::stack::analysis

‎include/analysis/DynamicAlloca.hpp‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#pragma once
2+
3+
#include <functional>
4+
#include <string>
5+
#include <vector>
6+
7+
namespace llvm
8+
{
9+
class AllocaInst;
10+
class Function;
11+
class Module;
12+
} // namespace llvm
13+
14+
namespace ctrace::stack::analysis
15+
{
16+
struct DynamicAllocaIssue
17+
{
18+
std::string funcName;
19+
std::string varName;
20+
std::string typeName;
21+
const llvm::AllocaInst* allocaInst = nullptr;
22+
};
23+
24+
std::vector<DynamicAllocaIssue>
25+
analyzeDynamicAllocas(llvm::Module& mod,
26+
const std::function<bool(const llvm::Function&)>& shouldAnalyze);
27+
} // namespace ctrace::stack::analysis
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#pragma once
2+
3+
#include <string>
4+
5+
#include "StackUsageAnalyzer.hpp"
6+
7+
namespace llvm
8+
{
9+
class Function;
10+
class Module;
11+
} // namespace llvm
12+
13+
namespace ctrace::stack::analysis
14+
{
15+
struct FunctionFilter
16+
{
17+
bool hasPathFilter = false;
18+
bool hasFuncFilter = false;
19+
bool hasFilter = false;
20+
std::string moduleSourcePath;
21+
const AnalysisConfig* config = nullptr;
22+
23+
bool shouldAnalyze(const llvm::Function& F) const;
24+
};
25+
26+
FunctionFilter buildFunctionFilter(const llvm::Module& mod, const AnalysisConfig& config);
27+
} // namespace ctrace::stack::analysis

0 commit comments

Comments
 (0)