Created
December 23, 2025 10:55
-
-
Save darealshinji/89a33d4200e1e39831089dfa4fbe84b4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <cstdint> | |
| #include <charconv> | |
| #include <iostream> | |
| #include <fstream> | |
| #include <regex> | |
| void test1(); | |
| template<class T, typename U> | |
| bool from_chars_hex(T &m, U &value) { | |
| std::from_chars(m.str().data(), m.str().data() + m.str().size(), value, 16); | |
| return (value > 0); | |
| } | |
| static std::string get_module_path(uintptr_t address) | |
| { | |
| std::smatch m; | |
| /* example: 7c818e000000-7c818e028000 r--p 00000000 103:01 2776778 /usr/lib/x86_64-linux-gnu/libc.so.6 */ | |
| const std::regex reg( | |
| "^" | |
| "([0-9a-fA-F]+)" "-" /* address start */ | |
| "([0-9a-fA-F]+)" " " /* address end */ | |
| "[r-][w-][x-][ps]" " " /* permissions */ | |
| "[0-9a-fA-F]+" " " /* offset */ | |
| "[0-9a-fA-F]+" ":" /* device major */ | |
| "[0-9a-fA-F]+" " " /* device minor */ | |
| "[0-9]+" "[ ]+" /* inode */ | |
| "(/.*)" /* path */ | |
| ); | |
| /* open maps file for reading */ | |
| std::ifstream ifs("/proc/self/maps"); | |
| if (!ifs.is_open()) { | |
| return {}; | |
| } | |
| for (std::string line; std::getline(ifs, line); ) { | |
| if (!std::regex_match(line, m, reg) || m.size() != 4) { | |
| continue; | |
| } | |
| uint64_t start = 0, end = 0; | |
| /* check if address is within range */ | |
| if (from_chars_hex(m[1], start) && | |
| from_chars_hex(m[2], end) && | |
| address > start && address < end) | |
| { | |
| return m[3].str(); | |
| } | |
| } | |
| return {}; | |
| } | |
| void test1() | |
| { | |
| std::string path = get_module_path(reinterpret_cast<uintptr_t>(malloc)); | |
| if (!path.empty()) { | |
| std::cout << path << std::endl; | |
| } | |
| } | |
| int main() | |
| { | |
| test1(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment