this repo has no description
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

[coredump] Convert Into C++ Project

+28 -28
+5 -1
src/hosttools/CMakeLists.txt
··· 3 3 option(DARLING_COREDUMP_SANITIZE "Enable/disable ASAN and UBSAN in darling-coredump" OFF) 4 4 5 5 add_executable(darling-coredump 6 - src/coredump/main.c 6 + src/coredump/main.cpp 7 + ) 8 + 9 + target_compile_options(darling-coredump PRIVATE 10 + -std=c++17 7 11 ) 8 12 9 13 target_include_directories(darling-coredump PRIVATE
+23 -27
src/hosttools/src/coredump/main.c src/hosttools/src/coredump/main.cpp
··· 1 + #include <filesystem> 2 + 1 3 #include <stdio.h> 2 4 #include <unistd.h> 3 5 #include <fcntl.h> ··· 248 250 249 251 // if that fails, try to see if it refers to the mounted prefix; if it does, try the lower layer 250 252 if (fd < 0 && filename_length >= cprm->prefix_length && strncmp(filename, cprm->prefix, cprm->prefix_length) == 0) { 251 - char* temp_filename = malloc(sizeof(LIBEXEC_PATH "/") + (filename_length - cprm->prefix_length)); 252 - sprintf(temp_filename, "%s/%s", LIBEXEC_PATH, &filename[cprm->prefix_length]); 253 - fd = open(temp_filename, O_RDONLY); 254 - free(temp_filename); 253 + std::filesystem::path temp_filename = std::filesystem::path() / LIBEXEC_PATH / &filename[cprm->prefix_length]; 254 + fd = open(temp_filename.c_str(), O_RDONLY); 255 255 } 256 256 257 257 return fd; ··· 318 318 return 1; 319 319 } 320 320 321 - cprm.universal_header = cprm.input_corefile_mapping; 321 + cprm.universal_header = (const struct elf_universal_header*)cprm.input_corefile_mapping; 322 322 323 323 if ( 324 324 cprm.universal_header->e_ident[EI_MAG0] != ELFMAG0 || ··· 345 345 switch (get_elf_machine_type(&cprm)) { 346 346 case EM_X86_64: 347 347 case EM_386: 348 - cprm.input_header = cprm.input_corefile_mapping; 348 + cprm.input_header = (const union Elf_Ehdr*)cprm.input_corefile_mapping; 349 349 break; 350 350 default: 351 351 fprintf(stderr, "Unexpected e_machine (%d) detected, aborting.\n", cprm.universal_header->e_machine); 352 352 return 1; 353 353 } 354 354 355 - cprm.input_program_headers = (const void*)((const char*)cprm.input_corefile_mapping + cprm_elf(cprm.is_64_bit, cprm.input_header, e_phoff)); 356 - cprm.input_program_headers_end = (const void*)((const char*)cprm.input_program_headers + (cprm_elf(cprm.is_64_bit, cprm.input_header, e_phentsize) * cprm_elf(cprm.is_64_bit, cprm.input_header, e_phnum))); 355 + cprm.input_program_headers = (const union Elf_Phdr*)((const char*)cprm.input_corefile_mapping + cprm_elf(cprm.is_64_bit, cprm.input_header, e_phoff)); 356 + cprm.input_program_headers_end = (const union Elf_Phdr*)((const char*)cprm.input_program_headers + (cprm_elf(cprm.is_64_bit, cprm.input_header, e_phentsize) * cprm_elf(cprm.is_64_bit, cprm.input_header, e_phnum))); 357 357 358 358 // first, count how many VM areas we have 359 - for (const union Elf_Phdr* program_header = cprm.input_program_headers; program_header < cprm.input_program_headers_end; program_header = (const void*)((const char*)program_header + cprm_elf(cprm.is_64_bit, cprm.input_header, e_phentsize))) { 359 + for (const union Elf_Phdr* program_header = cprm.input_program_headers; program_header < cprm.input_program_headers_end; program_header = (const union Elf_Phdr*)((const char*)program_header + cprm_elf(cprm.is_64_bit, cprm.input_header, e_phentsize))) { 360 360 if (cprm_elf(cprm.is_64_bit, program_header, p_type) == PT_LOAD) { 361 361 ++cprm.vm_area_count; 362 362 } else if (cprm_elf(cprm.is_64_bit, program_header, p_type) == PT_NOTE) { ··· 368 368 continue; 369 369 } 370 370 371 - cprm.input_notes = (const void*)((const char*)cprm.input_corefile_mapping + cprm_elf(cprm.is_64_bit, program_header, p_offset)); 371 + cprm.input_notes = (const union Elf_Nhdr*)((const char*)cprm.input_corefile_mapping + cprm_elf(cprm.is_64_bit, program_header, p_offset)); 372 372 cprm.input_notes_size = cprm_elf(cprm.is_64_bit, program_header, p_filesz); 373 373 } 374 374 } ··· 381 381 for (const union Elf_Nhdr* note_header = cprm.input_notes; note_header < (const union Elf_Nhdr*)((const char*)cprm.input_notes + cprm.input_notes_size); note_header = find_next_note(&cprm, note_header)) { 382 382 if (cprm_elf(cprm.is_64_bit, note_header, n_type) == NT_FILE) { 383 383 // allocate a copy for alignment purposes 384 - cprm.nt_file = malloc(cprm_elf(cprm.is_64_bit, note_header, n_descsz)); 384 + cprm.nt_file = (union nt_file_header*)malloc(cprm_elf(cprm.is_64_bit, note_header, n_descsz)); 385 385 if (!cprm.nt_file) { 386 386 perror("malloc"); 387 387 return 1; 388 388 } 389 389 memcpy(cprm.nt_file, note_data(&cprm, note_header), cprm_elf(cprm.is_64_bit, note_header, n_descsz)); 390 390 391 - cprm.nt_file_filenames = malloc(cprm_elf(cprm.is_64_bit, cprm.nt_file, count) * sizeof(const char*)); 391 + cprm.nt_file_filenames = (const char**)malloc(cprm_elf(cprm.is_64_bit, cprm.nt_file, count) * sizeof(const char*)); 392 392 if (!cprm.nt_file_filenames) { 393 393 perror("malloc"); 394 394 return 1; ··· 411 411 return 1; 412 412 } 413 413 414 - cprm.thread_infos = malloc(sizeof(struct thread_info) * cprm.thread_info_count); 414 + cprm.thread_infos = (struct thread_info*)malloc(sizeof(struct thread_info) * cprm.thread_info_count); 415 415 if (!cprm.thread_infos) { 416 416 perror("malloc"); 417 417 return 1; ··· 421 421 for (const union Elf_Nhdr* note_header = cprm.input_notes; note_header < (const union Elf_Nhdr*)((const char*)cprm.input_notes + cprm.input_notes_size); note_header = find_next_note(&cprm, note_header)) { 422 422 if (cprm_elf(cprm.is_64_bit, note_header, n_type) == NT_PRSTATUS) { 423 423 // allocate a copy for alignment purposes 424 - union nt_prstatus* prstatus = malloc(cprm_elf(cprm.is_64_bit, note_header, n_descsz)); 424 + union nt_prstatus* prstatus = (union nt_prstatus*)malloc(cprm_elf(cprm.is_64_bit, note_header, n_descsz)); 425 425 if (!prstatus) { 426 426 perror("malloc"); 427 427 return 1; ··· 482 482 } 483 483 484 484 // now allocate the VM area array 485 - cprm.vm_areas = malloc(sizeof(*cprm.vm_areas) * cprm.vm_area_count); 485 + cprm.vm_areas = (struct vm_area*)malloc(sizeof(*cprm.vm_areas) * cprm.vm_area_count); 486 486 if (!cprm.vm_areas) { 487 487 perror("malloc"); 488 488 return 1; ··· 798 798 } 799 799 800 800 if (!dump_emit(cprm, &mh, sizeof(mh))) 801 - goto fail; 801 + return false; 802 802 803 803 uint32_t file_offset = round_up_pow2(mh.sizeofcmds + sizeof(mh), align_page_size); 804 804 ··· 837 837 sc.maxprot = sc.initprot; 838 838 839 839 if (!dump_emit(cprm, &sc, sizeof(sc))) 840 - goto fail; 840 + return false; 841 841 842 842 file_offset += round_up_pow2(sc.filesize, align_page_size); 843 843 } 844 844 845 845 const int memsize = sizeof(struct thread_command) + statesize; 846 - uint8_t* buffer = malloc(memsize); 846 + uint8_t* buffer = (uint8_t*)malloc(memsize); 847 847 848 848 for (size_t i = 0; i < cprm->thread_info_count; ++i) { 849 849 const struct thread_info* thread_info = &cprm->thread_infos[i]; ··· 880 880 if (!dump_emit(cprm, buffer, memsize)) 881 881 { 882 882 free(buffer); 883 - goto fail; 883 + return false; 884 884 } 885 885 } 886 886 free(buffer); 887 887 888 888 return true; 889 - fail: 890 - return false; 891 889 } 892 890 893 891 static ··· 948 946 } 949 947 950 948 if (!dump_emit(cprm, &mh, sizeof(mh))) 951 - goto fail; 949 + return false; 952 950 953 951 uint64_t file_offset = round_up_pow2(mh.sizeofcmds + sizeof(mh), align_page_size); 954 952 ··· 985 983 sc.maxprot = sc.initprot; 986 984 987 985 if (!dump_emit(cprm, &sc, sizeof(sc))) 988 - goto fail; 986 + return false; 989 987 990 988 file_offset += round_up_pow2(sc.filesize, align_page_size); 991 989 } 992 990 993 991 const int memsize = sizeof(struct thread_command) + statesize; 994 - uint8_t* buffer = malloc(memsize); 992 + uint8_t* buffer = (uint8_t*)malloc(memsize); 995 993 996 994 for (size_t i = 0; i < cprm->thread_info_count; ++i) { 997 995 const struct thread_info* thread_info = &cprm->thread_infos[i]; ··· 1028 1026 if (!dump_emit(cprm, buffer, memsize)) 1029 1027 { 1030 1028 free(buffer); 1031 - goto fail; 1029 + return false; 1032 1030 } 1033 1031 } 1034 1032 free(buffer); 1035 1033 1036 1034 return true; 1037 - fail: 1038 - return false; 1039 1035 } 1040 1036 1041 1037 void macho_coredump(struct coredump_params* cprm)