From nobody Mon Sep 17 00:00:00 2001 From: Ilya Berezhniuk Date: Thu, 14 Feb 2008 05:19:11 +0300 Subject: [PATCH] --move-- moved native_modules from VMCORE to PORT --- vm/port/include/native_modules.h | 63 ++++++ vm/port/src/modules/linux/native_modules_os.c | 244 +++++++++++++++++++++++++ vm/port/src/modules/native_modules.c | 74 ++++++++ vm/port/src/modules/win/native_modules_os.c | 111 +++++++++++ vm/vmcore/include/native_modules.h | 63 ------ vm/vmcore/src/util/linux/native_modules.cpp | 244 ------------------------- vm/vmcore/src/util/native_modules_common.cpp | 74 -------- vm/vmcore/src/util/win/native_modules.cpp | 111 ----------- 8 files changed, 492 insertions(+), 492 deletions(-) create mode 100644 vm/port/include/native_modules.h create mode 100644 vm/port/src/modules/linux/native_modules_os.c create mode 100644 vm/port/src/modules/native_modules.c create mode 100644 vm/port/src/modules/win/native_modules_os.c delete mode 100644 vm/vmcore/include/native_modules.h delete mode 100644 vm/vmcore/src/util/linux/native_modules.cpp delete mode 100644 vm/vmcore/src/util/native_modules_common.cpp delete mode 100644 vm/vmcore/src/util/win/native_modules.cpp 0ad0363983b0364374990925e2a2b1c43b4d4401 diff --git a/vm/port/include/native_modules.h b/vm/port/include/native_modules.h new file mode 100644 index 0000000..53d3f3c --- /dev/null +++ b/vm/port/include/native_modules.h @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * @author Ilya Berezhniuk + * @version $Revision: 1.1.2.1 $ + */ + +#ifndef _NATIVE_MODULES_H_ +#define _NATIVE_MODULES_H_ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum { + SEGMENT_TYPE_UNKNOWN, + SEGMENT_TYPE_CODE, + SEGMENT_TYPE_DATA +} native_seg_type_t; + +typedef struct { + native_seg_type_t type; + void* base; + size_t size; +} native_segment_t; + +typedef struct native_module_t native_module_t; + +struct native_module_t { + char* filename; + size_t seg_count; + native_module_t* next; + native_segment_t segments[1]; +}; + + +bool get_all_native_modules(native_module_t**, int*); +void dump_native_modules(native_module_t* modules, FILE *out); +void clear_native_modules(native_module_t**); +native_module_t* find_native_module(native_module_t* modules, void* code_ptr); + + +#ifdef __cplusplus +} +#endif + +#endif // _NATIVE_MODULES_H_ diff --git a/vm/port/src/modules/linux/native_modules_os.c b/vm/port/src/modules/linux/native_modules_os.c new file mode 100644 index 0000000..cd7c880 --- /dev/null +++ b/vm/port/src/modules/linux/native_modules_os.c @@ -0,0 +1,244 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * @author Petr Ivanov, Ilya Berezhniuk + * @version $Revision: 1.1.2.1 $ + */ + +#include +#include +#include +#include +#include "platform_lowlevel.h" +#include "open/types.h" +#include "port_malloc.h" +#include "native_modules.h" + + +typedef struct _raw_module raw_module; + +// Structure to accumulate several segments for the same module +struct _raw_module +{ + void* start; + void* end; + bool acc_r; + bool acc_x; + char* name; + raw_module* next; +}; + +void native_clear_raw_list(raw_module*); +raw_module* native_add_raw_segment(raw_module*, void*, void*, char, char); +native_module_t* native_fill_module(raw_module*, size_t); + + +void native_clear_raw_list(raw_module* list) +{ + if (list->name) + STD_FREE(list->name); + + raw_module* cur = list->next; + + while (cur) + { + raw_module* next = cur->next; + STD_FREE(cur); + cur = next; + } +} + +raw_module* native_add_raw_segment(raw_module* last, + void* start, void* end, + char acc_r, char acc_x) +{ + if (last->next == NULL) + { + last->next = (raw_module*)STD_MALLOC(sizeof(raw_module)); + if (last->next == NULL) + return NULL; + + last->next->name = NULL; + last->next->next = NULL; + } + + last = last->next; + + last->start = start; + last->end = end; + last->acc_r = (acc_r == 'r'); + last->acc_x = (acc_x == 'x'); + + return last; +} + +native_module_t* native_fill_module(raw_module* rawModule, size_t count) +{ + native_module_t* module = + (native_module_t*)STD_MALLOC(sizeof(native_module_t) + sizeof(native_segment_t)*(count - 1)); + + if (module == NULL) + return NULL; + + module->seg_count = count; + module->filename = rawModule->name; + rawModule->name = NULL; + module->next = NULL; + + for (size_t i = 0; i < count; i++) + { + if (rawModule->acc_x) + module->segments[i].type = SEGMENT_TYPE_CODE; + else if (rawModule->acc_r) + module->segments[i].type = SEGMENT_TYPE_DATA; + else + module->segments[i].type = SEGMENT_TYPE_UNKNOWN; + + module->segments[i].base = rawModule->start; + module->segments[i].size = + (size_t)((POINTER_SIZE_INT)rawModule->end - + (POINTER_SIZE_INT)rawModule->start); + + rawModule = rawModule->next; + } + + return module; +} + +bool get_all_native_modules(native_module_t** list_ptr, int* count_ptr) +{ + char buf[_MAX_PATH]; + + if (list_ptr == NULL || count_ptr == NULL) + return false; + + pid_t pid = getpid(); + sprintf(buf, "/proc/%d/maps", pid); + + FILE* file = fopen(buf, "rt"); + if (!file) + return false; + + POINTER_SIZE_INT start, end; + char acc_r, acc_x; + char filename[_MAX_PATH]; + raw_module module; // First raw module + raw_module* lastseg = &module; // Address of last filled segment + size_t segment_count = 0; + int module_count = 0; + native_module_t** cur_next_ptr = list_ptr; + module.name = NULL; + module.next = NULL; + *list_ptr = NULL; + + while (!feof(file) && (fgets(buf, sizeof(buf), file))) + { + int res = sscanf(buf, "%" PI_FMT "x-%" PI_FMT "x %c%*c%c%*c %*" PI_FMT "x %*02x:%*02x %*u %s", + &start, &end, &acc_r, &acc_x, filename); + + if (res < 4) + continue; + + if (res < 5) + *filename = 0; + + if (module.name == NULL || // First module, or single memory region + !(*filename) || // Single memory region + strcmp(module.name, filename) != 0) // Next module + { + if (segment_count) // Add previous module + { + native_module_t* filled = + native_fill_module(&module, segment_count); + + if (!filled) + { + native_clear_raw_list(&module); + clear_native_modules(list_ptr); + fclose(file); + return false; + } + + *cur_next_ptr = filled; + cur_next_ptr = &filled->next; + } + + if (*filename) + { + module.name = (char*)STD_MALLOC(strlen(filename) + 1); + if (module.name == NULL) + { + native_clear_raw_list(&module); + clear_native_modules(list_ptr); + fclose(file); + return false; + } + + strcpy(module.name, filename); + } + else + module.name = NULL; + + // Store new module information + module.start = (void*)start; + module.end = (void*)end; + module.acc_r = (acc_r == 'r'); + module.acc_x = (acc_x == 'x'); + module.next = NULL; + ++module_count; + + lastseg = &module; + segment_count = 1; + } + else + { + lastseg = native_add_raw_segment(lastseg, + (void*)start, (void*)end, acc_r, acc_x); + + if (lastseg == NULL) + { + native_clear_raw_list(&module); + clear_native_modules(list_ptr); + fclose(file); + return false; + } + + ++segment_count; + } + } + + if (segment_count) // To process the last module + { + native_module_t* filled = native_fill_module(&module, segment_count); + + if (!filled) + { + native_clear_raw_list(&module); + clear_native_modules(list_ptr); + fclose(file); + return false; + } + + *cur_next_ptr = filled; + } + + native_clear_raw_list(&module); + fclose(file); + + *count_ptr = module_count; + return true; +} diff --git a/vm/port/src/modules/native_modules.c b/vm/port/src/modules/native_modules.c new file mode 100644 index 0000000..5cfe62e --- /dev/null +++ b/vm/port/src/modules/native_modules.c @@ -0,0 +1,74 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "open/types.h" +#include "native_modules.h" +#include "port_malloc.h" + +native_module_t* find_native_module(native_module_t* modules, void* code_ptr) +{ + for (native_module_t* module = modules; NULL != module; + module = module->next) { + for (size_t s = 0; s < module->seg_count; s++) { + void* base = module->segments[s].base; + size_t size = module->segments[s].size; + + if (code_ptr >= base && code_ptr < (char*) base + size) + return module; + } + } + + // no matching module + return NULL; +} + +void dump_native_modules(native_module_t* modules, FILE *out) +{ + for (native_module_t* module = modules; module; module = module->next) + { + if (!module->filename) + continue; + + fprintf(out, "%s:\n", module->filename); + + for (size_t i = 0; i < module->seg_count; i++) + { + size_t base = (size_t)module->segments[i].base; + + fprintf(out, "\t0x%"W_PI_FMT":0x%"W_PI_FMT"\n", + base, base + module->segments[i].size); + } + } +} + +void clear_native_modules(native_module_t** list_ptr) +{ + native_module_t* cur = *list_ptr; + + while (cur) + { + native_module_t* next = cur->next; + + if (cur->filename) + STD_FREE(cur->filename); + + STD_FREE(cur); + cur = next; + } + + *list_ptr = NULL; +} diff --git a/vm/port/src/modules/win/native_modules_os.c b/vm/port/src/modules/win/native_modules_os.c new file mode 100644 index 0000000..0de5d77 --- /dev/null +++ b/vm/port/src/modules/win/native_modules_os.c @@ -0,0 +1,111 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * @author Petr Ivanov, Ilya Berezhniuk + * @version $Revision: 1.1.2.1 $ + */ + +#include +#include +#include +#include "port_malloc.h" +#include "native_modules.h" + +static native_module_t* fill_module(MODULEENTRY32 src); + +bool get_all_native_modules(native_module_t** list_ptr, int* count_ptr) +{ + HANDLE hModuleSnap = INVALID_HANDLE_VALUE; + MODULEENTRY32 module; + native_module_t** cur_next_ptr = list_ptr; + int count = 0; + + hModuleSnap = + CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, GetCurrentProcessId()); + + if (hModuleSnap == INVALID_HANDLE_VALUE) + return false; + + *list_ptr = NULL; + + //It is required to set the size of the structure. + module.dwSize = sizeof(MODULEENTRY32); + if ( !Module32First(hModuleSnap, &module) ) + { + CloseHandle(hModuleSnap); + return false; + } + + do + { + native_module_t* filled = fill_module(module); + + if (!filled) + { + CloseHandle(hModuleSnap); + clear_native_modules(list_ptr); + return false; + } + + *cur_next_ptr = filled; + cur_next_ptr = &filled->next; + count++; + + } while (Module32Next(hModuleSnap, &module)); + + CloseHandle(hModuleSnap); + *count_ptr = count; + + return true; +} + +native_module_t* fill_module(MODULEENTRY32 src) +{ + native_module_t* module = + (native_module_t*)STD_MALLOC(sizeof(native_module_t)); + + if (module == NULL) + return NULL; + + size_t path_size = strlen(src.szExePath) + 1; + module->filename = (char*)STD_MALLOC(path_size); + if (module->filename == NULL) + { + STD_FREE(module); + return NULL; + } + + memcpy(module->filename, src.szExePath, path_size); + strlwr(module->filename); + + module->seg_count = 1; + module->segments[0].base = src.modBaseAddr; + module->segments[0].size = (size_t)src.modBaseSize; + module->next = NULL; + + MEMORY_BASIC_INFORMATION mem_info; + VirtualQuery(src.modBaseAddr, &mem_info, sizeof(mem_info)); + + if ((mem_info.Protect & (PAGE_EXECUTE | PAGE_EXECUTE_READ | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY)) != 0) + module->segments[0].type = SEGMENT_TYPE_CODE; + else if ((mem_info.Protect & (PAGE_READWRITE | PAGE_READONLY)) != 0) + module->segments[0].type = SEGMENT_TYPE_DATA; + else + module->segments[0].type = SEGMENT_TYPE_UNKNOWN; + + return module; +} diff --git a/vm/vmcore/include/native_modules.h b/vm/vmcore/include/native_modules.h deleted file mode 100644 index 53d3f3c..0000000 --- a/vm/vmcore/include/native_modules.h +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/** - * @author Ilya Berezhniuk - * @version $Revision: 1.1.2.1 $ - */ - -#ifndef _NATIVE_MODULES_H_ -#define _NATIVE_MODULES_H_ - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -typedef enum { - SEGMENT_TYPE_UNKNOWN, - SEGMENT_TYPE_CODE, - SEGMENT_TYPE_DATA -} native_seg_type_t; - -typedef struct { - native_seg_type_t type; - void* base; - size_t size; -} native_segment_t; - -typedef struct native_module_t native_module_t; - -struct native_module_t { - char* filename; - size_t seg_count; - native_module_t* next; - native_segment_t segments[1]; -}; - - -bool get_all_native_modules(native_module_t**, int*); -void dump_native_modules(native_module_t* modules, FILE *out); -void clear_native_modules(native_module_t**); -native_module_t* find_native_module(native_module_t* modules, void* code_ptr); - - -#ifdef __cplusplus -} -#endif - -#endif // _NATIVE_MODULES_H_ diff --git a/vm/vmcore/src/util/linux/native_modules.cpp b/vm/vmcore/src/util/linux/native_modules.cpp deleted file mode 100644 index cd7c880..0000000 --- a/vm/vmcore/src/util/linux/native_modules.cpp +++ /dev/null @@ -1,244 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/** - * @author Petr Ivanov, Ilya Berezhniuk - * @version $Revision: 1.1.2.1 $ - */ - -#include -#include -#include -#include -#include "platform_lowlevel.h" -#include "open/types.h" -#include "port_malloc.h" -#include "native_modules.h" - - -typedef struct _raw_module raw_module; - -// Structure to accumulate several segments for the same module -struct _raw_module -{ - void* start; - void* end; - bool acc_r; - bool acc_x; - char* name; - raw_module* next; -}; - -void native_clear_raw_list(raw_module*); -raw_module* native_add_raw_segment(raw_module*, void*, void*, char, char); -native_module_t* native_fill_module(raw_module*, size_t); - - -void native_clear_raw_list(raw_module* list) -{ - if (list->name) - STD_FREE(list->name); - - raw_module* cur = list->next; - - while (cur) - { - raw_module* next = cur->next; - STD_FREE(cur); - cur = next; - } -} - -raw_module* native_add_raw_segment(raw_module* last, - void* start, void* end, - char acc_r, char acc_x) -{ - if (last->next == NULL) - { - last->next = (raw_module*)STD_MALLOC(sizeof(raw_module)); - if (last->next == NULL) - return NULL; - - last->next->name = NULL; - last->next->next = NULL; - } - - last = last->next; - - last->start = start; - last->end = end; - last->acc_r = (acc_r == 'r'); - last->acc_x = (acc_x == 'x'); - - return last; -} - -native_module_t* native_fill_module(raw_module* rawModule, size_t count) -{ - native_module_t* module = - (native_module_t*)STD_MALLOC(sizeof(native_module_t) + sizeof(native_segment_t)*(count - 1)); - - if (module == NULL) - return NULL; - - module->seg_count = count; - module->filename = rawModule->name; - rawModule->name = NULL; - module->next = NULL; - - for (size_t i = 0; i < count; i++) - { - if (rawModule->acc_x) - module->segments[i].type = SEGMENT_TYPE_CODE; - else if (rawModule->acc_r) - module->segments[i].type = SEGMENT_TYPE_DATA; - else - module->segments[i].type = SEGMENT_TYPE_UNKNOWN; - - module->segments[i].base = rawModule->start; - module->segments[i].size = - (size_t)((POINTER_SIZE_INT)rawModule->end - - (POINTER_SIZE_INT)rawModule->start); - - rawModule = rawModule->next; - } - - return module; -} - -bool get_all_native_modules(native_module_t** list_ptr, int* count_ptr) -{ - char buf[_MAX_PATH]; - - if (list_ptr == NULL || count_ptr == NULL) - return false; - - pid_t pid = getpid(); - sprintf(buf, "/proc/%d/maps", pid); - - FILE* file = fopen(buf, "rt"); - if (!file) - return false; - - POINTER_SIZE_INT start, end; - char acc_r, acc_x; - char filename[_MAX_PATH]; - raw_module module; // First raw module - raw_module* lastseg = &module; // Address of last filled segment - size_t segment_count = 0; - int module_count = 0; - native_module_t** cur_next_ptr = list_ptr; - module.name = NULL; - module.next = NULL; - *list_ptr = NULL; - - while (!feof(file) && (fgets(buf, sizeof(buf), file))) - { - int res = sscanf(buf, "%" PI_FMT "x-%" PI_FMT "x %c%*c%c%*c %*" PI_FMT "x %*02x:%*02x %*u %s", - &start, &end, &acc_r, &acc_x, filename); - - if (res < 4) - continue; - - if (res < 5) - *filename = 0; - - if (module.name == NULL || // First module, or single memory region - !(*filename) || // Single memory region - strcmp(module.name, filename) != 0) // Next module - { - if (segment_count) // Add previous module - { - native_module_t* filled = - native_fill_module(&module, segment_count); - - if (!filled) - { - native_clear_raw_list(&module); - clear_native_modules(list_ptr); - fclose(file); - return false; - } - - *cur_next_ptr = filled; - cur_next_ptr = &filled->next; - } - - if (*filename) - { - module.name = (char*)STD_MALLOC(strlen(filename) + 1); - if (module.name == NULL) - { - native_clear_raw_list(&module); - clear_native_modules(list_ptr); - fclose(file); - return false; - } - - strcpy(module.name, filename); - } - else - module.name = NULL; - - // Store new module information - module.start = (void*)start; - module.end = (void*)end; - module.acc_r = (acc_r == 'r'); - module.acc_x = (acc_x == 'x'); - module.next = NULL; - ++module_count; - - lastseg = &module; - segment_count = 1; - } - else - { - lastseg = native_add_raw_segment(lastseg, - (void*)start, (void*)end, acc_r, acc_x); - - if (lastseg == NULL) - { - native_clear_raw_list(&module); - clear_native_modules(list_ptr); - fclose(file); - return false; - } - - ++segment_count; - } - } - - if (segment_count) // To process the last module - { - native_module_t* filled = native_fill_module(&module, segment_count); - - if (!filled) - { - native_clear_raw_list(&module); - clear_native_modules(list_ptr); - fclose(file); - return false; - } - - *cur_next_ptr = filled; - } - - native_clear_raw_list(&module); - fclose(file); - - *count_ptr = module_count; - return true; -} diff --git a/vm/vmcore/src/util/native_modules_common.cpp b/vm/vmcore/src/util/native_modules_common.cpp deleted file mode 100644 index 5cfe62e..0000000 --- a/vm/vmcore/src/util/native_modules_common.cpp +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "open/types.h" -#include "native_modules.h" -#include "port_malloc.h" - -native_module_t* find_native_module(native_module_t* modules, void* code_ptr) -{ - for (native_module_t* module = modules; NULL != module; - module = module->next) { - for (size_t s = 0; s < module->seg_count; s++) { - void* base = module->segments[s].base; - size_t size = module->segments[s].size; - - if (code_ptr >= base && code_ptr < (char*) base + size) - return module; - } - } - - // no matching module - return NULL; -} - -void dump_native_modules(native_module_t* modules, FILE *out) -{ - for (native_module_t* module = modules; module; module = module->next) - { - if (!module->filename) - continue; - - fprintf(out, "%s:\n", module->filename); - - for (size_t i = 0; i < module->seg_count; i++) - { - size_t base = (size_t)module->segments[i].base; - - fprintf(out, "\t0x%"W_PI_FMT":0x%"W_PI_FMT"\n", - base, base + module->segments[i].size); - } - } -} - -void clear_native_modules(native_module_t** list_ptr) -{ - native_module_t* cur = *list_ptr; - - while (cur) - { - native_module_t* next = cur->next; - - if (cur->filename) - STD_FREE(cur->filename); - - STD_FREE(cur); - cur = next; - } - - *list_ptr = NULL; -} diff --git a/vm/vmcore/src/util/win/native_modules.cpp b/vm/vmcore/src/util/win/native_modules.cpp deleted file mode 100644 index 0de5d77..0000000 --- a/vm/vmcore/src/util/win/native_modules.cpp +++ /dev/null @@ -1,111 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/** - * @author Petr Ivanov, Ilya Berezhniuk - * @version $Revision: 1.1.2.1 $ - */ - -#include -#include -#include -#include "port_malloc.h" -#include "native_modules.h" - -static native_module_t* fill_module(MODULEENTRY32 src); - -bool get_all_native_modules(native_module_t** list_ptr, int* count_ptr) -{ - HANDLE hModuleSnap = INVALID_HANDLE_VALUE; - MODULEENTRY32 module; - native_module_t** cur_next_ptr = list_ptr; - int count = 0; - - hModuleSnap = - CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, GetCurrentProcessId()); - - if (hModuleSnap == INVALID_HANDLE_VALUE) - return false; - - *list_ptr = NULL; - - //It is required to set the size of the structure. - module.dwSize = sizeof(MODULEENTRY32); - if ( !Module32First(hModuleSnap, &module) ) - { - CloseHandle(hModuleSnap); - return false; - } - - do - { - native_module_t* filled = fill_module(module); - - if (!filled) - { - CloseHandle(hModuleSnap); - clear_native_modules(list_ptr); - return false; - } - - *cur_next_ptr = filled; - cur_next_ptr = &filled->next; - count++; - - } while (Module32Next(hModuleSnap, &module)); - - CloseHandle(hModuleSnap); - *count_ptr = count; - - return true; -} - -native_module_t* fill_module(MODULEENTRY32 src) -{ - native_module_t* module = - (native_module_t*)STD_MALLOC(sizeof(native_module_t)); - - if (module == NULL) - return NULL; - - size_t path_size = strlen(src.szExePath) + 1; - module->filename = (char*)STD_MALLOC(path_size); - if (module->filename == NULL) - { - STD_FREE(module); - return NULL; - } - - memcpy(module->filename, src.szExePath, path_size); - strlwr(module->filename); - - module->seg_count = 1; - module->segments[0].base = src.modBaseAddr; - module->segments[0].size = (size_t)src.modBaseSize; - module->next = NULL; - - MEMORY_BASIC_INFORMATION mem_info; - VirtualQuery(src.modBaseAddr, &mem_info, sizeof(mem_info)); - - if ((mem_info.Protect & (PAGE_EXECUTE | PAGE_EXECUTE_READ | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY)) != 0) - module->segments[0].type = SEGMENT_TYPE_CODE; - else if ((mem_info.Protect & (PAGE_READWRITE | PAGE_READONLY)) != 0) - module->segments[0].type = SEGMENT_TYPE_DATA; - else - module->segments[0].type = SEGMENT_TYPE_UNKNOWN; - - return module; -} -- 1.3.3