@@ -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,32 @@ 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
+
736
+ // make sure signal is not blocked
737
+ sigset_t host_sig_set ;
738
+ sigemptyset (& host_sig_set );
739
+ sigaddset (& host_sig_set , host_sig );
740
+
741
+ sigprocmask (SIG_UNBLOCK , & host_sig_set , NULL );
742
+ //// --- End LibAFL code ---
729
743
730
744
kill (getpid (), host_sig );
731
745
732
746
/* 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 );
747
+ //// --- Start LibAFL code ---
748
+ // Unused as of now
749
+ // sigdelset(&act.sa_mask, host_sig);
750
+ // sigsuspend(&act.sa_mask);
751
+ //// --- End LibAFL code ---
735
752
736
753
/* unreachable */
737
754
_exit (EXIT_FAILURE );
738
755
}
739
756
757
+ // target code signal handling.
758
+ // transform target signal into host signal.
740
759
static G_NORETURN
741
760
void dump_core_and_abort (CPUArchState * env , int target_sig )
742
761
{
@@ -771,15 +790,7 @@ void dump_core_and_abort(CPUArchState *env, int target_sig)
771
790
}
772
791
773
792
preexit_cleanup (env , 128 + target_sig );
774
-
775
- //// --- Begin LibAFL code ---
776
-
777
- libafl_dump_core_exec (host_sig );
778
793
779
- // die_with_signal_nodfl(host_sig); // to trigger LibAFL sig handler
780
-
781
- //// --- End LibAFL code ---
782
-
783
794
die_with_signal (host_sig );
784
795
}
785
796
@@ -814,6 +825,7 @@ static inline void rewind_if_in_safe_syscall(void *puc)
814
825
}
815
826
}
816
827
828
+ // QEMU handler called when a real host signal is received (and not caused by the target)
817
829
static G_NORETURN
818
830
void die_from_signal (siginfo_t * info )
819
831
{
@@ -893,12 +905,6 @@ void die_from_signal(siginfo_t *info)
893
905
error_report ("QEMU internal SIG%s {code=%s, addr=%p}" ,
894
906
sig , code , info -> si_addr );
895
907
896
- //// --- Begin LibAFL code ---
897
-
898
- libafl_dump_core_exec (info -> si_signo );
899
-
900
- //// --- End LibAFL code ---
901
-
902
908
die_with_signal (info -> si_signo );
903
909
}
904
910
@@ -977,34 +983,10 @@ static uintptr_t host_sigbus_handler(CPUState *cpu, siginfo_t *info,
977
983
return pc ;
978
984
}
979
985
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
986
//// --- Begin LibAFL code ---
1007
987
/* static */
988
+ // QEMU entrypoint for signal handling.
989
+ // it will notably determine whether the incoming signal is caused by the host or the target.
1008
990
//// --- End LibAFL code ---
1009
991
void host_signal_handler (int host_sig , siginfo_t * info , void * puc )
1010
992
{
@@ -1019,6 +1001,10 @@ void host_signal_handler(int host_sig, siginfo_t *info, void *puc)
1019
1001
bool sync_sig = false;
1020
1002
void * sigmask ;
1021
1003
1004
+ //// --- Start LibAFL code ---
1005
+ libafl_set_in_host_signal_ctx ();
1006
+ //// --- End LibAFL code ---
1007
+
1022
1008
/*
1023
1009
* Non-spoofed SIGSEGV and SIGBUS are synchronous, and need special
1024
1010
* handling wrt signal blocking and unwinding. Non-spoofed SIGILL,
@@ -1029,7 +1015,10 @@ void host_signal_handler(int host_sig, siginfo_t *info, void *puc)
1029
1015
case SIGSEGV :
1030
1016
/* Only returns on handle_sigsegv_accerr_write success. */
1031
1017
host_sigsegv_handler (cpu , info , uc );
1032
- return ;
1018
+ //// --- Start LibAFL code ---
1019
+ goto exit ;
1020
+ // return;
1021
+ //// --- End LibAFL code ---
1033
1022
case SIGBUS :
1034
1023
pc = host_sigbus_handler (cpu , info , uc );
1035
1024
sync_sig = true;
@@ -1044,7 +1033,10 @@ void host_signal_handler(int host_sig, siginfo_t *info, void *puc)
1044
1033
/* get target signal number */
1045
1034
guest_sig = host_to_target_signal (host_sig );
1046
1035
if (guest_sig < 1 || guest_sig > TARGET_NSIG ) {
1047
- return ;
1036
+ //// --- Start LibAFL code ---
1037
+ goto exit ;
1038
+ // return;
1039
+ //// --- EndLibAFL code ---
1048
1040
}
1049
1041
trace_user_host_signal (env , host_sig , guest_sig );
1050
1042
@@ -1086,6 +1078,10 @@ void host_signal_handler(int host_sig, siginfo_t *info, void *puc)
1086
1078
1087
1079
/* interrupt the virtual CPU as soon as possible */
1088
1080
cpu_exit (thread_cpu );
1081
+ //// --- Start LibAFL code ---
1082
+ exit :
1083
+ libafl_unset_in_signal_ctx ();
1084
+ //// --- End LibAFL code ---
1089
1085
}
1090
1086
1091
1087
/* do_sigaltstack() returns target values and errnos. */
@@ -1228,6 +1224,7 @@ int libafl_force_dfl = 0;
1228
1224
1229
1225
//// --- End LibAFL code ---
1230
1226
1227
+ // Pending signal during target execution
1231
1228
static void handle_pending_signal (CPUArchState * cpu_env , int sig ,
1232
1229
struct emulated_sigtable * k )
1233
1230
{
@@ -1239,6 +1236,10 @@ static void handle_pending_signal(CPUArchState *cpu_env, int sig,
1239
1236
struct target_sigaction * sa ;
1240
1237
TaskState * ts = get_task_state (cpu );
1241
1238
1239
+ //// --- Start LibAFL code ---
1240
+ libafl_set_in_target_signal_ctx ();
1241
+ //// --- End LibAFL code ---
1242
+
1242
1243
trace_user_handle_signal (cpu_env , sig );
1243
1244
/* dequeue signal */
1244
1245
k -> pending = 0 ;
@@ -1269,8 +1270,7 @@ static void handle_pending_signal(CPUArchState *cpu_env, int sig,
1269
1270
1270
1271
//// --- Start LibAFL code ---
1271
1272
1272
- if (libafl_force_dfl && (sig == SIGABRT || sig == SIGABRT || sig == SIGSEGV
1273
- || sig == SIGILL || sig == SIGBUS )) {
1273
+ if (libafl_force_dfl && (sig == SIGABRT || sig == SIGSEGV || sig == SIGILL || sig == SIGBUS )) {
1274
1274
handler = TARGET_SIG_DFL ;
1275
1275
}
1276
1276
@@ -1333,6 +1333,9 @@ static void handle_pending_signal(CPUArchState *cpu_env, int sig,
1333
1333
sa -> _sa_handler = TARGET_SIG_DFL ;
1334
1334
}
1335
1335
}
1336
+ //// --- Start LibAFL code ---
1337
+ libafl_unset_in_signal_ctx ();
1338
+ //// --- End LibAFL code ---
1336
1339
}
1337
1340
1338
1341
void process_pending_signals (CPUArchState * cpu_env )
0 commit comments