Skip to content

Commit 2605fa9

Browse files
fabianbs96claude
andauthored
Use TestingSrcLocation in TaintConfigTest instead of getInstruction(N) (#818)
Replace all brittle integer-ID-based IR.getInstruction(N) calls with source-coordinate-based testingLocInIR() lookups using the TestingSrcLocation facility from TestUtils/SrcCodeLocationEntry.h. Also remove the now-unnecessary ValueAnnotationPass include and resetValueID() call from SetUp(). Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 0f263cf commit 2605fa9

1 file changed

Lines changed: 35 additions & 29 deletions

File tree

unittests/PhasarLLVM/TaintConfig/TaintConfigTest.cpp

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11

22
#include "phasar/PhasarLLVM/DB/LLVMProjectIRDB.h"
3-
#include "phasar/PhasarLLVM/Passes/ValueAnnotationPass.h"
43
#include "phasar/PhasarLLVM/TaintConfig/LLVMTaintConfig.h"
54
#include "phasar/PhasarLLVM/Utils/LLVMShorthands.h"
65

76
#include "llvm/Demangle/Demangle.h"
87
#include "llvm/IR/InstrTypes.h"
98
#include "llvm/IR/Instructions.h"
109

10+
#include "../TestUtils/SrcCodeLocationEntry.h"
1111
#include "../TestUtils/TestConfig.h"
1212
#include "gtest/gtest.h"
1313
#include "nlohmann/json.hpp"
@@ -21,11 +21,11 @@ static constexpr auto PathToAttrTaintConfigTestCode =
2121
PHASAR_BUILD_SUBFOLDER("TaintConfig/AttrConfig/");
2222

2323
namespace {
24+
using namespace psr::unittest;
25+
2426
class TaintConfigTest : public ::testing::Test {
2527

2628
public:
27-
void SetUp() override { psr::ValueAnnotationPass::resetValueID(); }
28-
2929
std::string getFunctionName(std::string MangledFunctionName) {
3030
if (MangledFunctionName.find('(') != std::string::npos) {
3131
return MangledFunctionName.substr(0, MangledFunctionName.find('('));
@@ -40,7 +40,8 @@ TEST_F(TaintConfigTest, Array_01) {
4040
const std::string File = "array_01_c_dbg.ll";
4141
psr::LLVMProjectIRDB IR({PathToAttrTaintConfigTestCode + File});
4242
psr::LLVMTaintConfig Config(IR);
43-
const llvm::Value *I = IR.getInstruction(5);
43+
const llvm::Value *I = testingLocInIR(
44+
OperandOf{0, LineColFunOp{8, 3, "main", llvm::Instruction::Call}}, IR);
4445
ASSERT_TRUE(Config.isSource(I));
4546
}
4647

@@ -49,7 +50,8 @@ TEST_F(TaintConfigTest, Array_02) {
4950
psr::LLVMProjectIRDB IR({PathToAttrTaintConfigTestCode + File});
5051
psr::LLVMTaintConfig Config(IR);
5152
llvm::outs() << Config << '\n';
52-
const llvm::Value *I = IR.getInstruction(5);
53+
const llvm::Value *I = testingLocInIR(
54+
OperandOf{0, LineColFunOp{9, 3, "main", llvm::Instruction::Call}}, IR);
5355
ASSERT_TRUE(Config.isSource(I));
5456
}
5557

@@ -78,8 +80,8 @@ TEST_F(TaintConfigTest, Basic_02) {
7880
psr::LLVMProjectIRDB IR({PathToAttrTaintConfigTestCode + File});
7981
psr::LLVMTaintConfig Config(IR);
8082
llvm::outs() << Config << '\n';
81-
const llvm::Value *I1 = IR.getInstruction(9);
82-
const llvm::Value *I2 = IR.getInstruction(21);
83+
const llvm::Value *I1 = testingLocInIR(LineColFun{10, 6, "modX"}, IR);
84+
const llvm::Value *I2 = testingLocInIR(LineColFun{17, 12, "main"}, IR);
8385
ASSERT_TRUE(Config.isSource(I1));
8486
ASSERT_TRUE(Config.isSource(I2));
8587
}
@@ -103,7 +105,8 @@ TEST_F(TaintConfigTest, Basic_04) {
103105
psr::LLVMProjectIRDB IR({PathToAttrTaintConfigTestCode + File});
104106
psr::LLVMTaintConfig Config(IR);
105107
llvm::outs() << Config << '\n';
106-
const llvm::Value *I = IR.getInstruction(4);
108+
const llvm::Value *I = testingLocInIR(
109+
OperandOf{0, LineColFunOp{3, 3, "main", llvm::Instruction::Call}}, IR);
107110
ASSERT_TRUE(Config.isSource(I));
108111
}
109112

@@ -112,7 +115,7 @@ TEST_F(TaintConfigTest, DataMember_01) {
112115
psr::LLVMProjectIRDB IR({PathToAttrTaintConfigTestCode + File});
113116
psr::LLVMTaintConfig Config(IR);
114117
llvm::outs() << Config << '\n';
115-
const llvm::Value *I = IR.getInstruction(9);
118+
const llvm::Value *I = testingLocInIR(LineColFun{10, 12, "main"}, IR);
116119
ASSERT_TRUE(Config.isSource(I));
117120
}
118121

@@ -143,10 +146,11 @@ TEST_F(TaintConfigTest, FunMember_02) {
143146
psr::LLVMTaintConfig TConfig(IR);
144147
// IR.emitPreprocessedIR(llvm::outs(), false);
145148
llvm::outs() << TConfig << '\n';
146-
const llvm::Value *I1 = IR.getInstruction(20);
147-
const llvm::Value *I2 = IR.getInstruction(57);
148-
const llvm::Value *I3 = IR.getInstruction(67);
149-
const llvm::Value *I4 = IR.getInstruction(76);
149+
const llvm::Value *I1 = testingLocInIR(LineColFun{19, 12, "main"}, IR);
150+
const llvm::Value *I2 = testingLocInIR(LineColFun{4, 14, "_ZN1XC2Ei"}, IR);
151+
const llvm::Value *I3 =
152+
testingLocInIR(LineColFun{8, 76, "_ZN1X5sanitEv"}, IR);
153+
const llvm::Value *I4 = testingLocInIR(LineColFun{7, 63, "_ZN1XD2Ev"}, IR);
150154
ASSERT_TRUE(TConfig.isSource(I1));
151155
ASSERT_TRUE(TConfig.isSource(I2));
152156
ASSERT_TRUE(TConfig.isSource(I3));
@@ -203,9 +207,8 @@ TEST_F(TaintConfigTest, StaticFun_02) {
203207
psr::LLVMProjectIRDB IR({PathToAttrTaintConfigTestCode + File});
204208
psr::LLVMTaintConfig Config(IR);
205209
llvm::outs() << Config << '\n';
206-
const llvm::Value *CallInst = IR.getInstruction(16);
207-
const auto *I = llvm::dyn_cast<llvm::CallBase>(CallInst);
208-
ASSERT_TRUE(I && Config.isSource(I->getCalledFunction()->getArg(0)));
210+
const auto *FooFn = IR.getFunction("_ZN12_GLOBAL__N_13fooERi");
211+
ASSERT_TRUE(FooFn && Config.isSource(FooFn->getArg(0)));
209212
for (const auto *F : IR.getAllFunctions()) {
210213
std::string FName = getFunctionName(llvm::demangle(F->getName().str()));
211214
if (FName == "bar") {
@@ -237,7 +240,8 @@ TEST_F(TaintConfigTest, Array_01_Json) {
237240
// IR.emitPreprocessedIR(llvm::outs(), false);
238241
psr::LLVMTaintConfig TConfig(IR, JsonConfig);
239242

240-
const llvm::Value *I = IR.getInstruction(3);
243+
const llvm::Value *I = testingLocInIR(
244+
OperandOf{1, LineColFunOp{8, 9, "main", llvm::Instruction::Store}}, IR);
241245
ASSERT_TRUE(TConfig.isSource(I));
242246
}
243247

@@ -249,7 +253,8 @@ TEST_F(TaintConfigTest, Array_02_Json) {
249253
psr::LLVMProjectIRDB IR({PathToJsonTaintConfigTestCode + File});
250254
// IR.emitPreprocessedIR(llvm::outs(), false);
251255
psr::LLVMTaintConfig TConfig(IR, JsonConfig);
252-
const llvm::Value *I = IR.getInstruction(3);
256+
const llvm::Value *I = testingLocInIR(
257+
OperandOf{1, LineColFunOp{9, 9, "main", llvm::Instruction::Store}}, IR);
253258
ASSERT_TRUE(TConfig.isSource(I));
254259
}
255260

@@ -284,8 +289,8 @@ TEST_F(TaintConfigTest, Basic_02_Json) {
284289
psr::LLVMProjectIRDB IR({PathToJsonTaintConfigTestCode + File});
285290
// IR.emitPreprocessedIR(llvm::outs(), false);
286291
psr::LLVMTaintConfig TConfig(IR, JsonConfig);
287-
const llvm::Value *I1 = IR.getInstruction(7);
288-
const llvm::Value *I2 = IR.getInstruction(18);
292+
const llvm::Value *I1 = testingLocInIR(LineColFun{10, 6, "modX"}, IR);
293+
const llvm::Value *I2 = testingLocInIR(LineColFun{17, 12, "main"}, IR);
289294
ASSERT_TRUE(TConfig.isSource(I1));
290295
ASSERT_TRUE(TConfig.isSource(I2));
291296
}
@@ -315,7 +320,8 @@ TEST_F(TaintConfigTest, Basic_04_Json) {
315320
psr::LLVMProjectIRDB IR({PathToJsonTaintConfigTestCode + File});
316321
// IR.emitPreprocessedIR(llvm::outs(), false);
317322
psr::LLVMTaintConfig TConfig(IR, JsonConfig);
318-
const llvm::Value *I = IR.getInstruction(2);
323+
const llvm::Value *I = testingLocInIR(
324+
OperandOf{1, LineColFunOp{3, 7, "main", llvm::Instruction::Store}}, IR);
319325
ASSERT_TRUE(TConfig.isSource(I));
320326
}
321327

@@ -326,8 +332,8 @@ TEST_F(TaintConfigTest, DataMember_01_Json) {
326332
psr::parseTaintConfig(PathToJsonTaintConfigTestCode + Config);
327333
psr::LLVMProjectIRDB IR({PathToJsonTaintConfigTestCode + File});
328334
psr::LLVMTaintConfig TConfig(IR, JsonConfig);
329-
const llvm::Value *I = IR.getInstruction(17);
330335
// IR.emitPreprocessedIR(llvm::outs(), false);
336+
const llvm::Value *I = testingLocInIR(LineColFun{3, 7, "_ZN1XC2Ev"}, IR);
331337
ASSERT_TRUE(TConfig.isSource(I));
332338
}
333339

@@ -367,10 +373,11 @@ TEST_F(TaintConfigTest, FunMember_02_Json) {
367373
psr::LLVMTaintConfig TConfig(IR, JsonConfig);
368374
llvm::outs() << TConfig << '\n';
369375

370-
const llvm::Value *I1 = IR.getInstruction(16);
371-
const llvm::Value *I2 = IR.getInstruction(52);
372-
const llvm::Value *I3 = IR.getInstruction(61);
373-
const llvm::Value *I4 = IR.getInstruction(69);
376+
const llvm::Value *I1 = testingLocInIR(LineColFun{19, 12, "main"}, IR);
377+
const llvm::Value *I2 = testingLocInIR(LineColFun{4, 14, "_ZN1XC2Ei"}, IR);
378+
const llvm::Value *I3 =
379+
testingLocInIR(LineColFun{8, 39, "_ZN1X5sanitEv"}, IR);
380+
const llvm::Value *I4 = testingLocInIR(LineColFun{7, 31, "_ZN1XD2Ev"}, IR);
374381
ASSERT_TRUE(TConfig.isSource(I1));
375382
ASSERT_TRUE(TConfig.isSource(I2));
376383
ASSERT_TRUE(TConfig.isSource(I3));
@@ -443,9 +450,8 @@ TEST_F(TaintConfigTest, StaticFun_02_Json) {
443450
psr::LLVMProjectIRDB IR({PathToJsonTaintConfigTestCode + File});
444451
psr::LLVMTaintConfig TConfig(IR, JsonConfig);
445452
llvm::outs() << TConfig << '\n';
446-
const llvm::Value *CallInst = IR.getInstruction(13);
447-
const auto *I = llvm::dyn_cast<llvm::CallBase>(CallInst);
448-
ASSERT_TRUE(I && TConfig.isSource(I->getCalledFunction()->getArg(0)));
453+
const auto *FooFn = IR.getFunction("_ZN12_GLOBAL__N_13fooERi");
454+
ASSERT_TRUE(FooFn && TConfig.isSource(FooFn->getArg(0)));
449455
for (const auto *F : IR.getAllFunctions()) {
450456
std::string FName = getFunctionName(llvm::demangle(F->getName().str()));
451457
if (FName == "bar") {

0 commit comments

Comments
 (0)