aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


client/tests/module_filename.c (2097B)


      1 #include <assert.h>
      2 #include <stdint.h>
      3 #include <stdio.h>
      4 #include <string.h>
      5 #ifdef _WIN32
      6 #include <windows.h>
      7 #else
      8 #include "linux_platform.h"
      9 #include "guest_memory.h"
     10 #include <unistd.h>
     11 #endif
     12 
     13 int main(void)
     14 {
     15     char path[4096];
     16 #ifdef _WIN32
     17     unsigned length = GetModuleFileNameA(NULL, path, sizeof(path));
     18     assert(length && length < sizeof(path));
     19 #else
     20     ssize_t size = readlink("/proc/self/exe", path, sizeof(path));
     21     assert(size > 0 && size < sizeof(path));
     22     unsigned length = size;
     23     path[length] = 0;
     24     assert(guest_map("module filename test memory", 0x50000000u, 0x10000u));
     25     g_fs_base = 0x50001000u;
     26 #endif
     27     const unsigned sizes[] = {0, 1, 12, length - 1, length, length + 1, 4096};
     28     for (unsigned i = 0; i < 7; ++i) {
     29         unsigned char output[4104], expected[4104];
     30         memset(output, 0xa5, sizeof(output));
     31         memset(expected, 0xa5, sizeof(expected));
     32         if (sizes[i]) {
     33             unsigned copied = length < sizes[i] ? length : sizes[i] - 1;
     34             memcpy(expected + 4, path, copied);
     35             expected[4 + copied] = 0;
     36         }
     37 #ifdef _WIN32
     38         SetLastError(777);
     39 #else
     40         memcpy((void *)ADDR(0x50002000u), output, sizeof(output));
     41         MEM32(g_fs_base + 0x34) = 777;
     42         g_esp = 0x5000ff00u;
     43         MEM32(g_esp + 4) = 0;
     44         MEM32(g_esp + 8) = 0x50002004u;
     45         MEM32(g_esp + 12) = sizes[i];
     46 #endif
     47 
     48 #ifdef _WIN32
     49         unsigned result = GetModuleFileNameA(NULL, (char *)output + 4, sizes[i]);
     50         unsigned error = GetLastError();
     51 #else
     52         linux_platform_lookup("KERNEL32.dll", "GetModuleFileNameA")();
     53         unsigned result = g_eax, error = MEM32(g_fs_base + 0x34);
     54         memcpy(output, (void *)ADDR(0x50002000u), sizeof(output));
     55         assert(g_esp == 0x5000ff10u);
     56 #endif
     57 
     58         assert(result == (length < sizes[i] ? length : sizes[i]));
     59         assert(error == (length < sizes[i] ? 0 : 122));
     60         assert(!memcmp(output, expected, sizeof(output)));
     61     }
     62     puts("module filename: real executable path, 7 buffer sizes, errors and canaries passed");
     63     return 0;
     64 }