Skip to content

Commit 6a5c10b

Browse files
authored
Merge pull request #26 from ilmanzo/split-.cpp-and-.hpp-files-to-improve-c++-testing
move is_func_relevant to header
2 parents d81dc21 + a714837 commit 6a5c10b

File tree

4 files changed

+44
-91
lines changed

4 files changed

+44
-91
lines changed

FuncTracer.cpp

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
#include "pin.H"
33
#include <iostream>
44
#include <fstream>
5-
#include <string>
6-
#include <set>
7-
#include <mutex>
85
#include <unistd.h> // For getpid()
6+
#include "Functracer.hpp"
97

108
// Global set and mutex to track logged functions
119
static std::set<std::string> logged_functions;
@@ -31,21 +29,6 @@ VOID log_function_call(const char* img_name, const char* func_name)
3129
LOG(ss.str());
3230
}
3331

34-
// Determine if function name is relevant to us and if it will be logged
35-
bool func_is_relevant(const std::string_view &func_name)
36-
{
37-
static const std::set<std::string_view> blacklist = {
38-
"main", "_init", "_start", ".plt.got"
39-
};
40-
if (blacklist.contains(func_name))
41-
return false;
42-
43-
// Ignore PLT functions and internal functions (usually prefixed with __)
44-
if (func_name.ends_with("@plt") || func_name.starts_with("__"))
45-
return false;
46-
47-
return true;
48-
}
4932

5033
// Pin calls this function for every image loaded into the process's address space.
5134
// An image is either an executable or a shared library.

FuncTracer.hpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef FUNCTRACER_HPP
2+
#define FUNCTRACER_HPP
3+
4+
#include <string>
5+
#include <set>
6+
#include <mutex>
7+
8+
// Determine if function name is relevant to us and if it will be logged
9+
bool func_is_relevant(const std::string_view &func_name)
10+
{
11+
static const std::set<std::string_view> blacklist = {
12+
"main", "_init", "_start", ".plt.got"
13+
};
14+
if (blacklist.contains(func_name))
15+
return false;
16+
17+
// Ignore PLT functions and internal functions (usually prefixed with __)
18+
if (func_name.ends_with("@plt") || func_name.starts_with("__"))
19+
return false;
20+
21+
return true;
22+
}
23+
24+
#endif // FUNCTRACER_HPP

tests/run_unit_tests.sh

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
#!/bin/bash
22
set -e
33

4-
54
echo "Running Python unit tests..."
65
export PIN_ROOT="${PIN_ROOT:-/var/coverage/pin}"
76
pushd tests || exit 1
87
python3 test_coverage_analyzer.py
98
sudo -E python3 test_wrap.py
10-
popd
11-
129
echo "Building and running C++ unit tests..."
13-
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
1410
CXXFLAGS=$(pkg-config --cflags catch2 2>/dev/null || echo "")
1511
LDFLAGS=$(pkg-config --libs catch2 2>/dev/null || echo "")
16-
g++ -std=c++11 $CXXFLAGS "$SCRIPT_DIR/test_func_tracer.cpp" $LDFLAGS -o "$SCRIPT_DIR/test_func_tracer"
17-
"$SCRIPT_DIR/test_func_tracer"
12+
g++ -std=c++20 $CXXFLAGS test_func_tracer.cpp $LDFLAGS -o "test_func_tracer"
13+
./test_func_tracer && rm -f test_func_tracer
14+
popd
1815

1916
echo "All tests passed."

tests/test_func_tracer.cpp

Lines changed: 16 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,31 @@
11
#define CATCH_CONFIG_MAIN
22
#include "catch2/catch.hpp"
3-
#include <string>
4-
#include <set>
5-
#include <mutex>
3+
#include "../FuncTracer.hpp"
64

7-
// Updated is_Relevant logic (returns true if function is relevant, i.e., NOT blacklisted)
8-
bool is_Relevant(const std::string& func_name) {
9-
static const std::set<std::string> blacklist = {
10-
"main", "_init", "_start", ".plt.got"
11-
};
12-
if (func_name.length() >= 4 && func_name.compare(func_name.length() - 4, 4, "@plt") == 0) return false;
13-
if (func_name.length() >= 2 && func_name.compare(0, 2, "__") == 0) return false;
14-
return (blacklist.find(func_name) == blacklist.end());
15-
}
16-
17-
// Deduplication logic for testing
18-
static std::set<std::string> logged_functions;
19-
static std::mutex log_mutex;
20-
21-
bool log_function_call_test(const char* img_name, const char* func_name)
22-
{
23-
std::string key = std::string(img_name) + ":" + std::string(func_name);
24-
{
25-
std::lock_guard<std::mutex> guard(log_mutex);
26-
if (logged_functions.find(key) != logged_functions.end())
27-
return false; // Already logged, skip
28-
logged_functions.insert(key);
29-
}
30-
return true; // Logged for the first time
31-
}
32-
33-
TEST_CASE("is_Relevant works as expected") {
5+
TEST_CASE("func_is_relevant works as expected") {
346
SECTION("PLT functions are not relevant") {
35-
REQUIRE_FALSE(is_Relevant("foo@plt"));
36-
REQUIRE_FALSE(is_Relevant("bar@plt"));
7+
REQUIRE_FALSE(func_is_relevant("foo@plt"));
8+
REQUIRE_FALSE(func_is_relevant("bar@plt"));
379
}
3810
SECTION("Functions starting with __ are not relevant") {
39-
REQUIRE_FALSE(is_Relevant("__internal"));
40-
REQUIRE_FALSE(is_Relevant("__something"));
11+
REQUIRE_FALSE(func_is_relevant("__internal"));
12+
REQUIRE_FALSE(func_is_relevant("__something"));
4113
}
4214
SECTION("Explicit blacklist") {
43-
REQUIRE_FALSE(is_Relevant("main"));
44-
REQUIRE_FALSE(is_Relevant("_init"));
45-
REQUIRE_FALSE(is_Relevant("_start"));
46-
REQUIRE_FALSE(is_Relevant(".plt.got"));
15+
REQUIRE_FALSE(func_is_relevant("main"));
16+
REQUIRE_FALSE(func_is_relevant("_init"));
17+
REQUIRE_FALSE(func_is_relevant("_start"));
18+
REQUIRE_FALSE(func_is_relevant(".plt.got"));
4719
}
4820
SECTION("Normal functions are relevant") {
49-
REQUIRE(is_Relevant("foo"));
50-
REQUIRE(is_Relevant("bar"));
51-
REQUIRE(is_Relevant("baz"));
21+
REQUIRE(func_is_relevant("foo"));
22+
REQUIRE(func_is_relevant("bar"));
23+
REQUIRE(func_is_relevant("baz"));
5224
}
5325
SECTION("Short names are relevant") {
54-
REQUIRE(is_Relevant("a"));
55-
REQUIRE(is_Relevant("b@p"));
56-
REQUIRE(is_Relevant("_m"));
26+
REQUIRE(func_is_relevant("a"));
27+
REQUIRE(func_is_relevant("b@p"));
28+
REQUIRE(func_is_relevant("_m"));
5729
}
5830
}
5931

60-
TEST_CASE("log_function_call deduplicates function calls") {
61-
// Clear the set before testing
62-
logged_functions.clear();
63-
64-
SECTION("First call logs, second call skips") {
65-
REQUIRE(log_function_call_test("img1", "funcA") == true); // First call, should log
66-
REQUIRE(log_function_call_test("img1", "funcA") == false); // Second call, should skip
67-
}
68-
69-
SECTION("Different functions are logged separately") {
70-
REQUIRE(log_function_call_test("img1", "funcB") == true);
71-
REQUIRE(log_function_call_test("img1", "funcC") == true);
72-
REQUIRE(log_function_call_test("img1", "funcB") == false);
73-
REQUIRE(log_function_call_test("img1", "funcC") == false);
74-
}
75-
76-
SECTION("Same function name in different images are logged separately") {
77-
REQUIRE(log_function_call_test("img1", "funcD") == true);
78-
REQUIRE(log_function_call_test("img2", "funcD") == true);
79-
REQUIRE(log_function_call_test("img1", "funcD") == false);
80-
REQUIRE(log_function_call_test("img2", "funcD") == false);
81-
}
82-
}

0 commit comments

Comments
 (0)