this repo has no description
1
fork

Configure Feed

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

Merge pull request #1519 from darlinghq/coredump_cpp_and_fixes

Coredump: Migrate To C++ & Bug Fix

authored by

CuriousTommy and committed by
GitHub
203af1f6 65c5198e

+43 -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
+38 -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 + 256 + // Sometimes the absolute path may actually be the macOS path 257 + } else if (fd < 0 && filename[0] == '/') { 258 + const char* relative_macos_path = &filename[1]; // Convert the absolute path into a relative path 259 + std::filesystem::path temp_filename; 260 + 261 + // Let's see if the file exists in the upper layer 262 + temp_filename = std::filesystem::path(cprm->prefix) / relative_macos_path; 263 + fd = open(temp_filename.c_str(), O_RDONLY); 264 + 265 + // Otherwise, check the lower layer 266 + if (fd < 0) { 267 + temp_filename = std::filesystem::path(LIBEXEC_PATH) / relative_macos_path; 268 + fd = open(temp_filename.c_str(), O_RDONLY); 269 + } 255 270 } 256 271 257 272 return fd; ··· 318 333 return 1; 319 334 } 320 335 321 - cprm.universal_header = cprm.input_corefile_mapping; 336 + cprm.universal_header = (const struct elf_universal_header*)cprm.input_corefile_mapping; 322 337 323 338 if ( 324 339 cprm.universal_header->e_ident[EI_MAG0] != ELFMAG0 || ··· 345 360 switch (get_elf_machine_type(&cprm)) { 346 361 case EM_X86_64: 347 362 case EM_386: 348 - cprm.input_header = cprm.input_corefile_mapping; 363 + cprm.input_header = (const union Elf_Ehdr*)cprm.input_corefile_mapping; 349 364 break; 350 365 default: 351 366 fprintf(stderr, "Unexpected e_machine (%d) detected, aborting.\n", cprm.universal_header->e_machine); 352 367 return 1; 353 368 } 354 369 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))); 370 + 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)); 371 + 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 372 358 373 // 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))) { 374 + 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 375 if (cprm_elf(cprm.is_64_bit, program_header, p_type) == PT_LOAD) { 361 376 ++cprm.vm_area_count; 362 377 } else if (cprm_elf(cprm.is_64_bit, program_header, p_type) == PT_NOTE) { ··· 368 383 continue; 369 384 } 370 385 371 - cprm.input_notes = (const void*)((const char*)cprm.input_corefile_mapping + cprm_elf(cprm.is_64_bit, program_header, p_offset)); 386 + cprm.input_notes = (const union Elf_Nhdr*)((const char*)cprm.input_corefile_mapping + cprm_elf(cprm.is_64_bit, program_header, p_offset)); 372 387 cprm.input_notes_size = cprm_elf(cprm.is_64_bit, program_header, p_filesz); 373 388 } 374 389 } ··· 381 396 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 397 if (cprm_elf(cprm.is_64_bit, note_header, n_type) == NT_FILE) { 383 398 // allocate a copy for alignment purposes 384 - cprm.nt_file = malloc(cprm_elf(cprm.is_64_bit, note_header, n_descsz)); 399 + cprm.nt_file = (union nt_file_header*)malloc(cprm_elf(cprm.is_64_bit, note_header, n_descsz)); 385 400 if (!cprm.nt_file) { 386 401 perror("malloc"); 387 402 return 1; 388 403 } 389 404 memcpy(cprm.nt_file, note_data(&cprm, note_header), cprm_elf(cprm.is_64_bit, note_header, n_descsz)); 390 405 391 - cprm.nt_file_filenames = malloc(cprm_elf(cprm.is_64_bit, cprm.nt_file, count) * sizeof(const char*)); 406 + cprm.nt_file_filenames = (const char**)malloc(cprm_elf(cprm.is_64_bit, cprm.nt_file, count) * sizeof(const char*)); 392 407 if (!cprm.nt_file_filenames) { 393 408 perror("malloc"); 394 409 return 1; ··· 411 426 return 1; 412 427 } 413 428 414 - cprm.thread_infos = malloc(sizeof(struct thread_info) * cprm.thread_info_count); 429 + cprm.thread_infos = (struct thread_info*)malloc(sizeof(struct thread_info) * cprm.thread_info_count); 415 430 if (!cprm.thread_infos) { 416 431 perror("malloc"); 417 432 return 1; ··· 421 436 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 437 if (cprm_elf(cprm.is_64_bit, note_header, n_type) == NT_PRSTATUS) { 423 438 // allocate a copy for alignment purposes 424 - union nt_prstatus* prstatus = malloc(cprm_elf(cprm.is_64_bit, note_header, n_descsz)); 439 + union nt_prstatus* prstatus = (union nt_prstatus*)malloc(cprm_elf(cprm.is_64_bit, note_header, n_descsz)); 425 440 if (!prstatus) { 426 441 perror("malloc"); 427 442 return 1; ··· 482 497 } 483 498 484 499 // now allocate the VM area array 485 - cprm.vm_areas = malloc(sizeof(*cprm.vm_areas) * cprm.vm_area_count); 500 + cprm.vm_areas = (struct vm_area*)malloc(sizeof(*cprm.vm_areas) * cprm.vm_area_count); 486 501 if (!cprm.vm_areas) { 487 502 perror("malloc"); 488 503 return 1; ··· 798 813 } 799 814 800 815 if (!dump_emit(cprm, &mh, sizeof(mh))) 801 - goto fail; 816 + return false; 802 817 803 818 uint32_t file_offset = round_up_pow2(mh.sizeofcmds + sizeof(mh), align_page_size); 804 819 ··· 837 852 sc.maxprot = sc.initprot; 838 853 839 854 if (!dump_emit(cprm, &sc, sizeof(sc))) 840 - goto fail; 855 + return false; 841 856 842 857 file_offset += round_up_pow2(sc.filesize, align_page_size); 843 858 } 844 859 845 860 const int memsize = sizeof(struct thread_command) + statesize; 846 - uint8_t* buffer = malloc(memsize); 861 + uint8_t* buffer = (uint8_t*)malloc(memsize); 847 862 848 863 for (size_t i = 0; i < cprm->thread_info_count; ++i) { 849 864 const struct thread_info* thread_info = &cprm->thread_infos[i]; ··· 880 895 if (!dump_emit(cprm, buffer, memsize)) 881 896 { 882 897 free(buffer); 883 - goto fail; 898 + return false; 884 899 } 885 900 } 886 901 free(buffer); 887 902 888 903 return true; 889 - fail: 890 - return false; 891 904 } 892 905 893 906 static ··· 948 961 } 949 962 950 963 if (!dump_emit(cprm, &mh, sizeof(mh))) 951 - goto fail; 964 + return false; 952 965 953 966 uint64_t file_offset = round_up_pow2(mh.sizeofcmds + sizeof(mh), align_page_size); 954 967 ··· 985 998 sc.maxprot = sc.initprot; 986 999 987 1000 if (!dump_emit(cprm, &sc, sizeof(sc))) 988 - goto fail; 1001 + return false; 989 1002 990 1003 file_offset += round_up_pow2(sc.filesize, align_page_size); 991 1004 } 992 1005 993 1006 const int memsize = sizeof(struct thread_command) + statesize; 994 - uint8_t* buffer = malloc(memsize); 1007 + uint8_t* buffer = (uint8_t*)malloc(memsize); 995 1008 996 1009 for (size_t i = 0; i < cprm->thread_info_count; ++i) { 997 1010 const struct thread_info* thread_info = &cprm->thread_infos[i]; ··· 1028 1041 if (!dump_emit(cprm, buffer, memsize)) 1029 1042 { 1030 1043 free(buffer); 1031 - goto fail; 1044 + return false; 1032 1045 } 1033 1046 } 1034 1047 free(buffer); 1035 1048 1036 1049 return true; 1037 - fail: 1038 - return false; 1039 1050 } 1040 1051 1041 1052 void macho_coredump(struct coredump_params* cprm)