aorecomp

Anarchy Online classic in the browser

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


tools/client/test_share_guest_bodies.py (2586B)


      1 import pathlib
      2 import subprocess
      3 import tempfile
      4 import unittest
      5 
      6 import share_guest_bodies
      7 
      8 
      9 class SharedBodiesTest(unittest.TestCase):
     10     def test_entry_jumps_fallthrough_and_exception_resume(self):
     11         prefix = '''#include <stdint.h>
     12 #include <setjmp.h>
     13 #include <stdio.h>
     14 #define RECOMP_ENTER(address) current = address
     15 uint32_t current;
     16 int value, throw_now;
     17 jmp_buf resume;
     18 '''
     19         body = '''    RECOMP_ENTER(ADDRESS);
     20 
     21 JUMP    value += 100;
     22 L_1000:
     23     value += 3;
     24     if (setjmp(resume)) goto L_3000;
     25     if (throw_now) longjmp(resume, 1);
     26 L_2000:
     27     value += 5;
     28     return;
     29 L_3000:
     30     value += 7;
     31 }
     32 
     33 '''
     34         source = prefix
     35         for address, jump in [('1000', ''), ('2000', '    goto L_2000;\n'), ('3000', '    goto L_3000;\n')]:
     36             source += 'void sub_' + address + '(void) {\n'
     37             source += body.replace('ADDRESS', '0x' + address + 'u').replace('JUMP', jump)
     38         source += '''int main(void) {
     39     sub_1000(); printf("%u %d\\n", current, value);
     40     value = 0; sub_2000(); printf("%u %d\\n", current, value);
     41     value = 0; sub_3000(); printf("%u %d\\n", current, value);
     42     value = 0; throw_now = 1; sub_1000(); printf("%u %d\\n", current, value);
     43     return 0;
     44 }
     45 '''
     46         # Keep the test driver separate from the strict generated function input.
     47         driver = source[source.index('int main(void)'):]
     48         generated = source[:source.index('int main(void)')]
     49 
     50         transformed = share_guest_bodies.share_bodies(generated)
     51         results = []
     52         with tempfile.TemporaryDirectory() as temporary:
     53             path = pathlib.Path(temporary)
     54             for name, program in [('original', generated), ('shared', transformed)]:
     55                 (path / (name + '.c')).write_text(program + driver)
     56                 subprocess.run(['gcc', '-O2', str(path / (name + '.c')), '-o', str(path / name)], check=True)
     57                 results.append(subprocess.check_output([str(path / name)], text=True))
     58 
     59         self.assertEqual(transformed.count('static __attribute__((noinline))'), 1)
     60         self.assertEqual(results, ['4096 108\n8192 5\n12288 7\n4096 110\n'] * 2)
     61 
     62     def test_per_function_storage_is_preserved(self):
     63         source = '#include <stdint.h>\n'
     64         for address in ['1000', '2000']:
     65             source += 'void sub_' + address + '(void) {\n'
     66             source += '    static int counter;\n    RECOMP_ENTER(0x' + address + 'u);\n    counter++;\n}\n\n'
     67 
     68         transformed = share_guest_bodies.share_bodies(source)
     69 
     70         self.assertEqual(transformed, source)
     71 
     72 
     73 if __name__ == '__main__':
     74     unittest.main()