Skip to content

Commit bf9b566

Browse files
committed
Add smoke test for relative virtual address
1 parent 9c2bfa2 commit bf9b566

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

test/test.cpp

+47-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <boost/stacktrace.hpp>
1010
#include <stdexcept>
11+
#include <fstream>
1112
#include <iostream>
1213
#include <sstream>
1314
#include <cctype>
@@ -263,7 +264,51 @@ void test_stacktrace_limits()
263264
BOOST_TEST_EQ(boost::stacktrace::stacktrace(1, 1).size(), 1);
264265
}
265266

266-
int main() {
267+
std::size_t get_file_size(const char* file_name) {
268+
std::ifstream file(file_name, std::ios::binary | std::ios::ate);
269+
const auto file_size = file.tellg();
270+
BOOST_TEST(file_size > 0);
271+
return static_cast<std::size_t>(file_size);
272+
}
273+
274+
uintptr_t get_address_from_frame(const std::string& frame) {
275+
std::size_t address = 0;
276+
std::string hex_address;
277+
std::size_t pos = frame.find("0x");
278+
279+
if (pos != std::string::npos) {
280+
// Extract the hex address substring
281+
hex_address = frame.substr(pos + 2); // Skip "0x"
282+
283+
// Convert hex string to std::size_t
284+
std::stringstream ss;
285+
ss << std::hex << hex_address;
286+
ss >> address;
287+
}
288+
289+
return address;
290+
}
291+
292+
void test_relative_virtual_address(const char* file_path)
293+
{
294+
const auto frame = to_string(boost::stacktrace::stacktrace(0, 1).as_vector().front());
295+
296+
// Skip the test if the frame does not contain an address
297+
if (frame.find("0x") == std::string::npos) {
298+
return;
299+
}
300+
301+
const auto file_size = get_file_size(file_path);
302+
BOOST_TEST(file_size > 0);
303+
304+
const auto address = get_address_from_frame(frame);
305+
BOOST_TEST(address > 0);
306+
307+
// Verify that the address is within the binary
308+
BOOST_TEST(address <= file_size);
309+
}
310+
311+
int main(const int, const char* argv[]) {
267312
test_deeply_nested_namespaces();
268313
test_frames_string_data_validity();
269314
test_nested<15>();
@@ -283,6 +328,7 @@ int main() {
283328
test_nested<260>(false);
284329

285330
test_stacktrace_limits();
331+
test_relative_virtual_address(argv[0]);
286332

287333
return boost::report_errors();
288334
}

0 commit comments

Comments
 (0)