@@ -679,6 +679,7 @@ void force_sigsegv(int oldsig)
679
679
}
680
680
#endif
681
681
682
+ // called when the signal is cause by the target, and is not because of the host
682
683
void cpu_loop_exit_sigsegv (CPUState * cpu , target_ulong addr ,
683
684
MMUAccessType access_type , bool maperr , uintptr_t ra )
684
685
{
@@ -709,13 +710,18 @@ void cpu_loop_exit_sigbus(CPUState *cpu, target_ulong addr,
709
710
cpu_loop_exit_restore (cpu , ra );
710
711
}
711
712
712
- /* abort execution with signal */
713
+ /* abort host execution with signal */
713
714
static G_NORETURN
714
715
void die_with_signal (int host_sig )
715
716
{
716
- struct sigaction act = {
717
- .sa_handler = SIG_DFL ,
718
- };
717
+ //// --- Start LibAFL code ---
718
+ // We don't want to give back the signal to default handler.
719
+ // Instead, LibAFL is gonna catch the signal if it has put a handler for it
720
+ // and decide what to do
721
+
722
+ // struct sigaction act = {
723
+ // .sa_handler = SIG_DFL,
724
+ // };
719
725
720
726
/*
721
727
* The proper exit code for dying from an uncaught signal is -<signal>.
@@ -724,19 +730,25 @@ void die_with_signal(int host_sig)
724
730
* signal. Here the default signal handler is installed, we send
725
731
* the signal and we wait for it to arrive.
726
732
*/
727
- sigfillset (& act .sa_mask );
728
- sigaction (host_sig , & act , NULL );
733
+ // sigfillset(&act.sa_mask);
734
+ // sigaction(host_sig, &act, NULL);
735
+ //// --- End LibAFL code ---
729
736
730
737
kill (getpid (), host_sig );
731
738
732
739
/* Make sure the signal isn't masked (reusing the mask inside of act). */
733
- sigdelset (& act .sa_mask , host_sig );
734
- sigsuspend (& act .sa_mask );
740
+ //// --- Start LibAFL code ---
741
+ // Unused as of now
742
+ // sigdelset(&act.sa_mask, host_sig);
743
+ // sigsuspend(&act.sa_mask);
744
+ //// --- End LibAFL code ---
735
745
736
746
/* unreachable */
737
747
_exit (EXIT_FAILURE );
738
748
}
739
749
750
+ // target code signal handling.
751
+ // transform target signal into host signal.
740
752
static G_NORETURN
741
753
void dump_core_and_abort (CPUArchState * env , int target_sig )
742
754
{
@@ -771,15 +783,7 @@ void dump_core_and_abort(CPUArchState *env, int target_sig)
771
783
}
772
784
773
785
preexit_cleanup (env , 128 + target_sig );
774
-
775
- //// --- Begin LibAFL code ---
776
-
777
- libafl_dump_core_exec (host_sig );
778
-
779
- // die_with_signal_nodfl(host_sig); // to trigger LibAFL sig handler
780
786
781
- //// --- End LibAFL code ---
782
-
783
787
die_with_signal (host_sig );
784
788
}
785
789
@@ -814,6 +818,7 @@ static inline void rewind_if_in_safe_syscall(void *puc)
814
818
}
815
819
}
816
820
821
+ // QEMU handler called when a real host signal is received (and not caused by the target)
817
822
static G_NORETURN
818
823
void die_from_signal (siginfo_t * info )
819
824
{
@@ -893,12 +898,6 @@ void die_from_signal(siginfo_t *info)
893
898
error_report ("QEMU internal SIG%s {code=%s, addr=%p}" ,
894
899
sig , code , info -> si_addr );
895
900
896
- //// --- Begin LibAFL code ---
897
-
898
- libafl_dump_core_exec (info -> si_signo );
899
-
900
- //// --- End LibAFL code ---
901
-
902
901
die_with_signal (info -> si_signo );
903
902
}
904
903
@@ -977,34 +976,10 @@ static uintptr_t host_sigbus_handler(CPUState *cpu, siginfo_t *info,
977
976
return pc ;
978
977
}
979
978
980
- //// --- Begin LibAFL code ---
981
-
982
- // int libafl_qemu_is_tb_protected_write(int host_sig, siginfo_t *info,
983
- // host_sigcontext *uc);
984
-
985
- /* int libafl_qemu_is_tb_protected_write(int host_sig, siginfo_t *info,
986
- host_sigcontext *uc)
987
- {
988
- CPUState *cpu = thread_cpu;
989
- uintptr_t host_addr = (uintptr_t)info->si_addr;
990
-
991
- bool is_valid = h2g_valid(host_addr);
992
- abi_ptr guest_addr = h2g_nocheck(host_addr);
993
- uintptr_t pc = host_signal_pc(uc);
994
- bool is_write = host_signal_write(info, uc);
995
- MMUAccessType access_type = adjust_signal_pc(&pc, is_write);
996
-
997
- return is_write
998
- && is_valid
999
- && info->si_code == SEGV_ACCERR
1000
- && handle_sigsegv_accerr_write(cpu, host_signal_mask(uc),
1001
- pc, guest_addr);
1002
- } */
1003
-
1004
- //// --- End LibAFL code ---
1005
-
1006
979
//// --- Begin LibAFL code ---
1007
980
/* static */
981
+ // QEMU entrypoint for signal handling.
982
+ // it will notably determine whether the incoming signal is caused by the host or the target.
1008
983
//// --- End LibAFL code ---
1009
984
void host_signal_handler (int host_sig , siginfo_t * info , void * puc )
1010
985
{
@@ -1019,6 +994,10 @@ void host_signal_handler(int host_sig, siginfo_t *info, void *puc)
1019
994
bool sync_sig = false;
1020
995
void * sigmask ;
1021
996
997
+ //// --- Start LibAFL code ---
998
+ libafl_set_in_host_signal_ctx ();
999
+ //// --- End LibAFL code ---
1000
+
1022
1001
/*
1023
1002
* Non-spoofed SIGSEGV and SIGBUS are synchronous, and need special
1024
1003
* handling wrt signal blocking and unwinding. Non-spoofed SIGILL,
@@ -1029,7 +1008,10 @@ void host_signal_handler(int host_sig, siginfo_t *info, void *puc)
1029
1008
case SIGSEGV :
1030
1009
/* Only returns on handle_sigsegv_accerr_write success. */
1031
1010
host_sigsegv_handler (cpu , info , uc );
1032
- return ;
1011
+ //// --- Start LibAFL code ---
1012
+ goto exit ;
1013
+ // return;
1014
+ //// --- End LibAFL code ---
1033
1015
case SIGBUS :
1034
1016
pc = host_sigbus_handler (cpu , info , uc );
1035
1017
sync_sig = true;
@@ -1044,7 +1026,10 @@ void host_signal_handler(int host_sig, siginfo_t *info, void *puc)
1044
1026
/* get target signal number */
1045
1027
guest_sig = host_to_target_signal (host_sig );
1046
1028
if (guest_sig < 1 || guest_sig > TARGET_NSIG ) {
1047
- return ;
1029
+ //// --- Start LibAFL code ---
1030
+ goto exit ;
1031
+ // return;
1032
+ //// --- EndLibAFL code ---
1048
1033
}
1049
1034
trace_user_host_signal (env , host_sig , guest_sig );
1050
1035
@@ -1086,6 +1071,10 @@ void host_signal_handler(int host_sig, siginfo_t *info, void *puc)
1086
1071
1087
1072
/* interrupt the virtual CPU as soon as possible */
1088
1073
cpu_exit (thread_cpu );
1074
+ //// --- Start LibAFL code ---
1075
+ exit :
1076
+ libafl_unset_in_signal_ctx ();
1077
+ //// --- End LibAFL code ---
1089
1078
}
1090
1079
1091
1080
/* do_sigaltstack() returns target values and errnos. */
@@ -1228,6 +1217,7 @@ int libafl_force_dfl = 0;
1228
1217
1229
1218
//// --- End LibAFL code ---
1230
1219
1220
+ // Pending signal during target execution
1231
1221
static void handle_pending_signal (CPUArchState * cpu_env , int sig ,
1232
1222
struct emulated_sigtable * k )
1233
1223
{
@@ -1239,6 +1229,10 @@ static void handle_pending_signal(CPUArchState *cpu_env, int sig,
1239
1229
struct target_sigaction * sa ;
1240
1230
TaskState * ts = get_task_state (cpu );
1241
1231
1232
+ //// --- Start LibAFL code ---
1233
+ libafl_set_in_target_signal_ctx ();
1234
+ //// --- End LibAFL code ---
1235
+
1242
1236
trace_user_handle_signal (cpu_env , sig );
1243
1237
/* dequeue signal */
1244
1238
k -> pending = 0 ;
@@ -1269,8 +1263,7 @@ static void handle_pending_signal(CPUArchState *cpu_env, int sig,
1269
1263
1270
1264
//// --- Start LibAFL code ---
1271
1265
1272
- if (libafl_force_dfl && (sig == SIGABRT || sig == SIGABRT || sig == SIGSEGV
1273
- || sig == SIGILL || sig == SIGBUS )) {
1266
+ if (libafl_force_dfl && (sig == SIGABRT || sig == SIGSEGV || sig == SIGILL || sig == SIGBUS )) {
1274
1267
handler = TARGET_SIG_DFL ;
1275
1268
}
1276
1269
@@ -1333,6 +1326,9 @@ static void handle_pending_signal(CPUArchState *cpu_env, int sig,
1333
1326
sa -> _sa_handler = TARGET_SIG_DFL ;
1334
1327
}
1335
1328
}
1329
+ //// --- Start LibAFL code ---
1330
+ libafl_unset_in_signal_ctx ();
1331
+ //// --- End LibAFL code ---
1336
1332
}
1337
1333
1338
1334
void process_pending_signals (CPUArchState * cpu_env )
0 commit comments