From 11c374a4b36d49a940ea0a58734dcddbef0395f8 Mon Sep 17 00:00:00 2001 From: ptitSeb Date: Sat, 16 Nov 2019 13:55:46 +0100 Subject: [PATCH] Added a test for swapcontext and friends --- CMakeLists.txt | 7 ++++++- tests/ref13.txt | 8 +++++++ tests/test13 | Bin 0 -> 7512 bytes tests/test13.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 tests/ref13.txt create mode 100755 tests/test13 create mode 100644 tests/test13.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 57b7933a..fffcfc49 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -306,7 +306,12 @@ add_test(test12 ${CMAKE_COMMAND} -D TEST_PROGRAM=${CMAKE_BINARY_DIR}/${BOX86} -D TEST_REFERENCE=${CMAKE_SOURCE_DIR}/tests/ref12.txt -P ${CMAKE_SOURCE_DIR}/runTest.cmake ) -file(GLOB extension_tests "${CMAKE_SOURCE_DIR}/tests/extensions/*.c") +add_test(test13 ${CMAKE_COMMAND} -D TEST_PROGRAM=${CMAKE_BINARY_DIR}/${BOX86} + -D TEST_ARGS=${CMAKE_SOURCE_DIR}/tests/test13 -D TEST_OUTPUT=tmpfile.txt + -D TEST_REFERENCE=${CMAKE_SOURCE_DIR}/tests/ref13.txt + -P ${CMAKE_SOURCE_DIR}/runTest.cmake ) + + file(GLOB extension_tests "${CMAKE_SOURCE_DIR}/tests/extensions/*.c") foreach(file ${extension_tests}) get_filename_component(testname "${file}" NAME_WE) add_test(NAME "${testname}" COMMAND ${CMAKE_COMMAND} -D TEST_PROGRAM=${CMAKE_BINARY_DIR}/${BOX86} diff --git a/tests/ref13.txt b/tests/ref13.txt new file mode 100644 index 00000000..33118c0d --- /dev/null +++ b/tests/ref13.txt @@ -0,0 +1,8 @@ +main: swapcontext(&uctx_main, &uctx_func2) +func2: started (1, 2) +func2: swapcontext(&uctx_func2, &uctx_func1) +func1: started +func1: swapcontext(&uctx_func1, &uctx_func2) +func2: returning +func1: returning +main: exiting diff --git a/tests/test13 b/tests/test13 new file mode 100755 index 0000000000000000000000000000000000000000..823cb65b8ec08095b478b5fa037c89bf7a55b6e8 GIT binary patch literal 7512 zcmeHMeQ;FO6~Frd28>I$!@y)Rs(f3 z@E~DL*U4Dgaq34=oY6n%)G4-g#yTzr0jIX4YCF!@Y3YdVON|a4EK)_bzu$fPl9z=} z{ipwVGUwg%yXV|qP7oxOQ@#p*hT!yznk36~%`GhT=VCh@pJs_YXt2aYuh)l5jGw^qu7P>egy_?Gu?;xI$XE?&liqpPacK0cSIB2xNv3|5n zKcAnp=RA~szo2<`?`b?6w~0)T0kdq6AMwtdJ>GMM3&9V$=d`RpW90L}Y>(+{DSTyn zx?z!VR~T0nJm(yf_B@R`{Z$UZ(f4YhgV*x;0-SKWN}cB0G77r~9jTNlw?Z|)y_f%2fy0eDZgmm|G*W6*dSkS~^VXq(NK zg1>A}Bg`{rgT|duA9pk0!l;4V_Y{Xk6TJ>5LBmsMKU`|6wmFPxwA(9(-_183YQihp z8cvW6|L~>k_@g&`AK#bxD<&w|w9c4`es&GyuoklV;1FY`cd}^Qm_N)3Hcq0|r+w2a z23q^Azp<#b-#AStuYZqD9$5)FyPMD3PiQyx?dAcCHGDPZUmq@S%n#z--yG=AYO}@J zf$T4#6~=RT?0S7E+HbHsm~$fY;rb)zotXiB*nQ-q8IJdUe_ku^du$n9zO7Bfb_q7- z--g4!Ef?M#$ehdmS{~2nYQ1qcLa=M#@7G~|huz(;Y(*Q9zPV55QpI>CBcl1kwE)H) zW?YPgXmL#6iYmt?{3VtK8ioE*e_yng`;K;Oz4 z#d_-ix+kl1V(vwFyU{_=cpR1H^47|;`ZX}cfPzLqR!`kDkkvV9vhGIIVMTC(8$QVB zf2Zt!qU;~dXUq8=2VGY0FZBA^7qVCO#$L^hFjKncred!PtsZ^@Tm9(RUY|#u)$1#e zt;xsxZ@@-2K10-Y4fI~i_?GsyS5#&XyAcIr5p1)%fH>#ak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jt4&ef0<|Qyl$LI7h3V-1j;eL2Exh4L;dUpS zP}|xQ&7pQRtR<2uHI(iWEs2iK_J|ei@Xp>RNo87e6Z0&K6aRrsLba z&0Ru`ghN^gjxJG#p&WAo=l5IpZM;u2@A5|Ls$fl5<}I4M;~T3SbH&D=GGo=@R6>o0 z;$dFL72hKk-TT=aj{Zm*t@YXC!g;a(4jW_E+Nnk#u-`w9(W=dEX>SUh?V9k)ka!__+Y*5C4%W-1 z4x5Br4&F$#u(+dPxUi7f)Li94wyyQgao;0=WYJtun$Dna3tXX~)cCBbcd>^oIv?Smv!3(Ux={n7JnHm`jnF16%#F zp1d8*+>|nNEOHRCy0sI|2Qw!l`ytbgnT=)leaIXu z``L#|@=cH}q8+IhY}qk~Blk;<`Cv?F`xw}=^P@8|brx}KQEj#=Fp^EKZwb?PJBx{%PcN})iR54vL zm&pNGeW6Oq#QYx%^E=uxCp!9H?EV5h=Yn?3mH7X*^MyJT(jVi;T#dt#{UT@0!MPvU zBKAo(z?Pj*01#NVx5ygnC#YaO{V57$*e$b^tOJwIg6Xe{07ScQnWB7BXEEo(q6M%U ogg@?owxw%S$u-J8Epj6%{++EMeUbS4&TjWM(sAaC7Fl-x0c*zR +#include +#include + +static ucontext_t uctx_main, uctx_func1, uctx_func2; + +#define handle_error(msg) \ + do { perror(msg); exit(EXIT_FAILURE); } while (0) + +static void func1(void) +{ + printf("func1: started\n"); + printf("func1: swapcontext(&uctx_func1, &uctx_func2)\n"); + if (swapcontext(&uctx_func1, &uctx_func2) == -1) + handle_error("swapcontext"); + printf("func1: returning\n"); +} + +static void func2(int a, int b) +{ + printf("func2: started (%d, %d)\n", a, b); + printf("func2: swapcontext(&uctx_func2, &uctx_func1)\n"); + if (swapcontext(&uctx_func2, &uctx_func1) == -1) + handle_error("swapcontext"); + printf("func2: returning\n"); +} + +int main(int argc, char *argv[]) +{ + char func1_stack[16384]; + char func2_stack[16384]; + + if (getcontext(&uctx_func1) == -1) + handle_error("getcontext"); + uctx_func1.uc_stack.ss_sp = func1_stack; + uctx_func1.uc_stack.ss_size = sizeof(func1_stack); + uctx_func1.uc_link = &uctx_main; + makecontext(&uctx_func1, func1, 0); + + if (getcontext(&uctx_func2) == -1) + handle_error("getcontext"); + uctx_func2.uc_stack.ss_sp = func2_stack; + uctx_func2.uc_stack.ss_size = sizeof(func2_stack); + /* Successor context is f1(), unless argc > 1 */ + uctx_func2.uc_link = (argc > 1) ? NULL : &uctx_func1; + makecontext(&uctx_func2, func2, 2, 1, 2); + + printf("main: swapcontext(&uctx_main, &uctx_func2)\n"); + if (swapcontext(&uctx_main, &uctx_func2) == -1) + handle_error("swapcontext"); + + printf("main: exiting\n"); + exit(EXIT_SUCCESS); +} \ No newline at end of file