Skip to content

Commit

Permalink
[bug-fix] fix linux compile error
Browse files Browse the repository at this point in the history
  • Loading branch information
jmpews committed Dec 17, 2020
1 parent e7dd908 commit 0aec6c1
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 44 deletions.
5 changes: 4 additions & 1 deletion CMakeLists.txt
Expand Up @@ -296,7 +296,6 @@ if(Plugin.SymbolResolver)
if(SYSTEM.Linux OR SYSTEM.Android)
set(dobby.plugin.SOURCE_FILE_LIST ${dobby.plugin.SOURCE_FILE_LIST}
builtin-plugin/SymbolResolver/elf/dobby_symbol_resolver.cc
builtin-plugin/AndroidRestriction/android_restriction.cc
)
endif()
endif()
Expand Down Expand Up @@ -408,6 +407,10 @@ if(SYSTEM.Android)
target_link_libraries(dobby log)
endif()

if(SYSTEM.Linux)
target_link_libraries(dobby dl)
endif()

if(SYSTEM.Darwin)
target_link_libraries(${dobby_output_name}
"-framework Foundation")
Expand Down
Expand Up @@ -21,7 +21,7 @@

#define LOG_TAG "PosixFileOperationMonitor"

std::unordered_map<int, const char *> posix_file_descriptors;
std::unordered_map<int, const char *> *posix_file_descriptors;

int (*orig_open)(const char *pathname, int flags, ...);
int fake_open(const char *pathname, int flags, ...) {
Expand All @@ -39,7 +39,11 @@ int fake_open(const char *pathname, int flags, ...) {
// FIXME: strncpy
strcpy(traced_filename, pathname);
LOG(1, "[-] trace open handle: %s", pathname);
posix_file_descriptors.insert(std::make_pair(result, (const char *)traced_filename));

if (posix_file_descriptors == NULL) {
posix_file_descriptors = new std::unordered_map<int, const char *>();
}
posix_file_descriptors->insert(std::make_pair(result, (const char *)traced_filename));
}
return result;
}
Expand All @@ -55,17 +59,20 @@ int fake___open(const char *pathname, int flags, int mode) {
}
int result = orig___open(pathname, flags, mode);
if (result != -1) {
posix_file_descriptors.insert(std::make_pair(result, (const char *)traced_filename));
if (posix_file_descriptors == NULL) {
posix_file_descriptors = new std::unordered_map<int, const char *>();
}
posix_file_descriptors->insert(std::make_pair(result, (const char *)traced_filename));
}
return result;
}

static const char *get_traced_filename(int fd, bool removed) {
std::unordered_map<int, const char *>::iterator it;
it = posix_file_descriptors.find(fd);
if (it != posix_file_descriptors.end()) {
it = posix_file_descriptors->find(fd);
if (it != posix_file_descriptors->end()) {
if (removed)
posix_file_descriptors.erase(it);
posix_file_descriptors->erase(it);
return it->second;
}
return NULL;
Expand Down Expand Up @@ -98,7 +105,7 @@ int fake_close(int fd) {
return orig_close(fd);
}

#if 0
#if 1
__attribute__((constructor)) static void ctor() {
DobbyHook((void *)DobbySymbolResolver(NULL, "open"), (void *)fake_open, (void **)&orig_open);

Expand Down
8 changes: 6 additions & 2 deletions builtin-plugin/SymbolResolver/elf/dobby_symbol_resolver.cc
Expand Up @@ -2,15 +2,16 @@
#include "common/headers/common_header.h"

#include <elf.h>
#include <jni.h>
#include <string>
#include <dlfcn.h>
#include <link.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>

#include <string>
#include <string.h>

#include "PlatformUtil/ProcessRuntimeUtility.h"

#include <vector>
Expand Down Expand Up @@ -171,6 +172,9 @@ PUBLIC void *DobbySymbolResolver(const char *image_name, const char *symbol_name
}
}
#endif
result = dlsym(RTLD_DEFAULT, symbol_name_pattern);
if(result)
return result;

result = resolve_elf_internal_symbol(image_name, symbol_name_pattern);
return result;
Expand Down
25 changes: 0 additions & 25 deletions example/CMakelists.txt

This file was deleted.

14 changes: 8 additions & 6 deletions source/UserMode/PlatformUtil/Linux/ProcesssRuntimeUtility.cc
@@ -1,12 +1,13 @@
#include "PlatformUtil/ProcessRuntimeUtility.h"

#include <elf.h>
#include <jni.h>
#include <string>
#include <dlfcn.h>
#include <link.h>
#include <sys/mman.h>

#include <string>
#include <string.h>

#include <vector>

#define LINE_MAX 2048
Expand Down Expand Up @@ -174,13 +175,13 @@ static std::vector<RuntimeModule> get_process_map_with_proc_maps() {
return ProcessModuleMap;
}

#if defined(__ANDROID__) && defined(__LP64__)
#if defined(__LP64__)
static std::vector<RuntimeModule> get_process_map_with_linker_iterator() {
std::vector<RuntimeModule> ProcessModuleMap;

static int (*dl_iterate_phdr_ptr)(int (*)(struct dl_phdr_info*, size_t, void*), void*);
static int (*dl_iterate_phdr_ptr)(int (*)(struct dl_phdr_info *, size_t, void *), void *);
dl_iterate_phdr_ptr = (typeof(dl_iterate_phdr_ptr))dlsym(RTLD_DEFAULT, "dl_iterate_phdr");
if(dl_iterate_phdr_ptr == NULL) {
if (dl_iterate_phdr_ptr == NULL) {
return ProcessModuleMap;
}

Expand All @@ -203,7 +204,8 @@ static std::vector<RuntimeModule> get_process_map_with_linker_iterator() {
#endif

std::vector<RuntimeModule> ProcessRuntimeUtility::GetProcessModuleMap() {
#if defined(__LP64__)
#if defined(__LP64__) && 0
// TODO: won't resolve main binary
return get_process_map_with_linker_iterator();
#else
return get_process_map_with_proc_maps();
Expand Down
6 changes: 3 additions & 3 deletions source/UserMode/Thread/platform-thread-posix.cc
@@ -1,8 +1,8 @@
#include "PlatformThread.h"

#include <unistd.h> // getpid
#include "Thread/PlatformThread.h"

#include <unistd.h> // getpid
#include <pthread.h> // pthread
#include <sys/syscall.h>

using namespace zz;

Expand Down

0 comments on commit 0aec6c1

Please sign in to comment.