Skip to content

Commit f637e41

Browse files
committed
Cleanup a handful of internal exceptions: Use internal_error and use the formatting constructor
1 parent 7ec2b1b commit f637e41

File tree

3 files changed

+17
-23
lines changed

3 files changed

+17
-23
lines changed

src/from_current.cpp

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,9 @@ namespace detail {
176176
int mprotect_page_and_return_old_protections(void* page, int page_size, int protections) {
177177
DWORD old_protections;
178178
if(!VirtualProtect(page, page_size, protections, &old_protections)) {
179-
throw std::runtime_error(
180-
microfmt::format(
181-
"VirtualProtect call failed: {}",
182-
std::system_error(GetLastError(), std::system_category()).what()
183-
)
179+
throw internal_error(
180+
"VirtualProtect call failed: {}",
181+
std::system_error(GetLastError(), std::system_category()).what()
184182
);
185183
}
186184
return old_protections;
@@ -191,11 +189,9 @@ namespace detail {
191189
void* allocate_page(int page_size) {
192190
auto page = VirtualAlloc(nullptr, page_size, MEM_COMMIT | MEM_RESERVE, memory_readwrite);
193191
if(!page) {
194-
throw std::runtime_error(
195-
microfmt::format(
196-
"VirtualAlloc call failed: {}",
197-
std::system_error(GetLastError(), std::system_category()).what()
198-
)
192+
throw internal_error(
193+
"VirtualAlloc call failed: {}",
194+
std::system_error(GetLastError(), std::system_category()).what()
199195
);
200196
}
201197
return page;
@@ -240,7 +236,7 @@ namespace detail {
240236
&object
241237
);
242238
if(status == KERN_INVALID_ADDRESS) {
243-
throw std::runtime_error("vm_region failed with KERN_INVALID_ADDRESS");
239+
throw internal_error("vm_region failed with KERN_INVALID_ADDRESS");
244240
}
245241
int perms = 0;
246242
if(info.protection & VM_PROT_READ) {
@@ -303,13 +299,13 @@ namespace detail {
303299
return nullopt;
304300
}
305301
if(stream.fail()) {
306-
throw std::runtime_error("Failure reading /proc/self/maps");
302+
throw internal_error("Failure reading /proc/self/maps");
307303
}
308304
stream.ignore(1); // space
309305
char r, w, x; // there's a private/shared flag after these but we don't need it
310306
stream>>r>>w>>x;
311307
if(stream.fail() || stream.eof()) {
312-
throw std::runtime_error("Failure reading /proc/self/maps");
308+
throw internal_error("Failure reading /proc/self/maps");
313309
}
314310
int perms = 0;
315311
if(r == 'r') {
@@ -388,7 +384,7 @@ namespace detail {
388384
#endif
389385
void mprotect_page(void* page, int page_size, int protections) {
390386
if(mprotect(page, page_size, protections) != 0) {
391-
throw std::runtime_error(microfmt::format("mprotect call failed: {}", strerror(errno)));
387+
throw internal_error("mprotect call failed: {}", strerror(errno));
392388
}
393389
}
394390
int mprotect_page_and_return_old_protections(void* page, int page_size, int protections) {
@@ -399,7 +395,7 @@ namespace detail {
399395
void* allocate_page(int page_size) {
400396
auto page = mmap(nullptr, page_size, memory_readwrite, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
401397
if(page == MAP_FAILED) {
402-
throw std::runtime_error(microfmt::format("mmap call failed: {}", strerror(errno)));
398+
throw internal_error("mmap call failed: {}", strerror(errno));
403399
}
404400
return page;
405401
}
@@ -445,9 +441,7 @@ namespace detail {
445441
// shouldn't be anything other than 4096 but out of an abundance of caution
446442
auto page_size = get_page_size();
447443
if(page_size <= 0 && (page_size & (page_size - 1)) != 0) {
448-
throw std::runtime_error(
449-
microfmt::format("getpagesize() is not a power of 2 greater than zero (was {})", page_size)
450-
);
444+
throw internal_error("getpagesize() is not a power of 2 greater than zero (was {})", page_size);
451445
}
452446

453447
// allocate a page for the new vtable so it can be made read-only later
@@ -467,7 +461,7 @@ namespace detail {
467461
auto page_addr = type_info_addr & ~(page_size - 1);
468462
// make sure the memory we're going to set is within the page
469463
if(type_info_addr - page_addr + sizeof(void*) > static_cast<unsigned>(page_size)) {
470-
throw std::runtime_error("pointer crosses page boundaries");
464+
throw internal_error("pointer crosses page boundaries");
471465
}
472466
auto old_protections = mprotect_page_and_return_old_protections(
473467
reinterpret_cast<void*>(page_addr),

src/symbols/dwarf/dwarf.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace libdwarf {
2929
Dwarf_Unsigned ev = dwarf_errno(error);
3030
// dwarf_dealloc_error deallocates the message, attaching to msg is convenient
3131
auto msg = raii_wrap(dwarf_errmsg(error), [dbg, error] (char*) { dwarf_dealloc_error(dbg, error); });
32-
throw internal_error(microfmt::format("dwarf error {} {}", ev, msg.get()));
32+
throw internal_error("dwarf error {} {}", ev, msg.get());
3333
}
3434

3535
struct die_object {

src/symbols/dwarf/dwarf_utils.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace libdwarf {
2020
: dbg(dbg), dw_srcfiles(dw_srcfiles), dw_filecount(static_cast<Dwarf_Unsigned>(filecount))
2121
{
2222
if(filecount < 0) {
23-
throw internal_error(microfmt::format("Unexpected dw_filecount {}", filecount));
23+
throw internal_error("Unexpected dw_filecount {}", filecount);
2424
}
2525
}
2626
~srcfiles() {
@@ -51,11 +51,11 @@ namespace libdwarf {
5151
// note: dwarf uses 1-indexing
5252
const char* get(Dwarf_Unsigned file_i) const {
5353
if(file_i >= dw_filecount) {
54-
throw internal_error(microfmt::format(
54+
throw internal_error(
5555
"Error while accessing the srcfiles list, requested index {} is out of bounds (count = {})",
5656
file_i,
5757
dw_filecount
58-
));
58+
);
5959
}
6060
return dw_srcfiles[file_i];
6161
}

0 commit comments

Comments
 (0)