Skip to content

Commit 184c1e1

Browse files
committed
Fixed a hanging issue for callbacks.
1 parent 0f88c82 commit 184c1e1

File tree

7 files changed

+142
-509
lines changed

7 files changed

+142
-509
lines changed

decaf/linux-headers/asm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
/home/hu/deepbits/DECAF/decaf/linux-headers/asm-x86
1+
/decafroot/DECAF/decaf/linux-headers/asm-x86

decaf/scripts/ordereddict.pyc

-4.35 KB
Binary file not shown.

decaf/scripts/qapi.pyc

-6.39 KB
Binary file not shown.

decaf/shared/DECAF_callback.c

Lines changed: 139 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ DECAF_Handle DECAF_registerOptimizedBlockBeginCallback(
257257
gva_t addr,
258258
OCB_t type)
259259
{
260-
callback_struct_t * cb_struct = (callback_struct_t *)malloc(sizeof(callback_struct_t));
260+
callback_struct_t * cb_struct = (callback_struct_t *)g_malloc(sizeof(callback_struct_t));
261261
if (cb_struct == NULL)
262262
{
263263
return (DECAF_NULL_HANDLE);
@@ -297,13 +297,13 @@ DECAF_Handle DECAF_registerOptimizedBlockBeginCallback(
297297
{
298298
if (pOBBTable == NULL)
299299
{
300-
free(cb_struct);
300+
g_free(cb_struct);
301301
return (DECAF_NULL_HANDLE);
302302
}
303303
//This is not necessarily thread-safe
304304
if (CountingHashtable_add(pOBBTable, addr) == 1)
305305
{
306-
DECAF_flushTranslationCache(BLOCK_LEVEL,addr);
306+
DECAF_flushTranslationCache(BLOCK_LEVEL, addr);
307307
}
308308
break;
309309
}
@@ -320,14 +320,14 @@ DECAF_Handle DECAF_registerOptimizedBlockBeginCallback(
320320
addr &= TARGET_PAGE_MASK;
321321
if (pOBBPageTable == NULL)
322322
{
323-
free(cb_struct);
323+
g_free(cb_struct);
324324
return (DECAF_NULL_HANDLE);
325325
}
326326

327327
//This is not necessarily thread-safe
328328
if (CountingHashtable_add(pOBBPageTable, addr) == 1)
329329
{
330-
DECAF_flushTranslationCache(PAGE_LEVEL,addr);
330+
DECAF_flushTranslationCache(PAGE_LEVEL, addr);
331331
}
332332
break;
333333
}
@@ -353,7 +353,7 @@ DECAF_Handle DECAF_registerOpcodeRangeCallbacks (
353353
return DECAF_NULL_HANDLE;
354354
}
355355

356-
callback_struct_t * cb_struct = (callback_struct_t *)malloc(sizeof(callback_struct_t));
356+
callback_struct_t * cb_struct = (callback_struct_t *)g_malloc(sizeof(callback_struct_t));
357357
if (cb_struct == NULL)
358358
{
359359
return (DECAF_NULL_HANDLE);
@@ -379,7 +379,7 @@ DECAF_Handle DECAF_registerOpcodeRangeCallbacks (
379379
LIST_INSERT_HEAD(&callback_list_heads[DECAF_OPCODE_RANGE_CB], cb_struct, link);
380380

381381
//Flush the tb
382-
DECAF_flushTranslationCache(ALL_CACHE,0);
382+
DECAF_flushTranslationCache(ALL_CACHE, 0);
383383

384384
return (DECAF_Handle)cb_struct;
385385
}
@@ -406,9 +406,9 @@ DECAF_errno_t DECAF_unregisterOpcodeRangeCallbacks(DECAF_Handle handle)
406406
instructionCallbacks[i] = NULL;
407407
}
408408

409-
LIST_REMOVE(cb_struct, link);
409+
LIST_REMOVE(cb_struct, link);
410410

411-
free(cb_struct);
411+
g_free(cb_struct);
412412

413413
return 0;
414414
}
@@ -422,79 +422,79 @@ DECAF_errno_t DECAF_unregisterOpcodeRangeCallbacks(DECAF_Handle handle)
422422

423423

424424
DECAF_Handle DECAF_registerOptimizedBlockEndCallback(
425-
DECAF_callback_func_t cb_func,
426-
int *cb_cond,
427-
gva_t from,
428-
gva_t to)
425+
DECAF_callback_func_t cb_func,
426+
int *cb_cond,
427+
gva_t from,
428+
gva_t to)
429429
{
430430

431-
callback_struct_t * cb_struct = (callback_struct_t *)malloc(sizeof(callback_struct_t));
432-
if (cb_struct == NULL)
433-
{
434-
return (DECAF_NULL_HANDLE);
435-
}
431+
callback_struct_t * cb_struct = (callback_struct_t *)g_malloc(sizeof(callback_struct_t));
432+
if (cb_struct == NULL)
433+
{
434+
return (DECAF_NULL_HANDLE);
435+
}
436436

437-
//pre-populate the info
438-
cb_struct->callback = cb_func;
439-
cb_struct->enabled = cb_cond;
440-
cb_struct->from = from;
441-
cb_struct->to = to;
442-
cb_struct->ocb_type = OCB_ALL;
437+
//pre-populate the info
438+
cb_struct->callback = cb_func;
439+
cb_struct->enabled = cb_cond;
440+
cb_struct->from = from;
441+
cb_struct->to = to;
442+
cb_struct->ocb_type = OCB_ALL;
443443

444-
if ( (from == INV_ADDR) && (to == INV_ADDR) )
445-
{
446-
enableAllBlockEndCallbacksCount++;
447-
bEnableAllBlockEndCallbacks = 1;
448-
if (enableAllBlockEndCallbacksCount == 1)
449-
{
450-
DECAF_flushTranslationCache(ALL_CACHE,0);
451-
}
452-
}
453-
else if (to == INV_ADDR) //this means only looking at the FROM list
454-
{
455-
if (pOBEFromPageTable == NULL)
456-
{
457-
free(cb_struct);
458-
return(DECAF_NULL_HANDLE);
459-
}
444+
if ( (from == INV_ADDR) && (to == INV_ADDR) )
445+
{
446+
enableAllBlockEndCallbacksCount++;
447+
bEnableAllBlockEndCallbacks = 1;
448+
if (enableAllBlockEndCallbacksCount == 1)
449+
{
450+
DECAF_flushTranslationCache(ALL_CACHE,0);
451+
}
452+
}
453+
else if (to == INV_ADDR) //this means only looking at the FROM list
454+
{
455+
if (pOBEFromPageTable == NULL)
456+
{
457+
g_free(cb_struct);
458+
return(DECAF_NULL_HANDLE);
459+
}
460460

461-
if (CountingHashtable_add(pOBEFromPageTable, from & TARGET_PAGE_MASK) == 1)
462-
{
463-
DECAF_flushTranslationCache(PAGE_LEVEL,from);
464-
}
465-
}
466-
else if (from == INV_ADDR)
467-
//this is tricky, because it involves flushing the WHOLE cache
468-
{
469-
if (pOBEToPageTable == NULL)
470-
{
471-
free(cb_struct);
472-
return(DECAF_NULL_HANDLE);
473-
}
461+
if (CountingHashtable_add(pOBEFromPageTable, from & TARGET_PAGE_MASK) == 1)
462+
{
463+
DECAF_flushTranslationCache(PAGE_LEVEL,from);
464+
}
465+
}
466+
else if (from == INV_ADDR)
467+
//this is tricky, because it involves flushing the WHOLE cache
468+
{
469+
if (pOBEToPageTable == NULL)
470+
{
471+
g_free(cb_struct);
472+
return(DECAF_NULL_HANDLE);
473+
}
474474

475-
if (CountingHashtable_add(pOBEToPageTable, to & TARGET_PAGE_MASK) == 1)
476-
{
477-
DECAF_flushTranslationCache(ALL_CACHE,0);
478-
}
479-
}
480-
else
481-
{
482-
if (pOBEPageMap == NULL)
483-
{
484-
free(cb_struct);
485-
return(DECAF_NULL_HANDLE);
486-
}
475+
if (CountingHashtable_add(pOBEToPageTable, to & TARGET_PAGE_MASK) == 1)
476+
{
477+
DECAF_flushTranslationCache(ALL_CACHE,0);
478+
}
479+
}
480+
else
481+
{
482+
if (pOBEPageMap == NULL)
483+
{
484+
g_free(cb_struct);
485+
return(DECAF_NULL_HANDLE);
486+
}
487487

488-
//if we are here then that means we need the hashmap
489-
if (CountingHashmap_add(pOBEPageMap, from & TARGET_PAGE_MASK, to & TARGET_PAGE_MASK) == 1)
490-
{
491-
DECAF_flushTranslationCache(PAGE_LEVEL,from);
492-
}
493-
}
488+
//if we are here then that means we need the hashmap
489+
if (CountingHashmap_add(pOBEPageMap, from & TARGET_PAGE_MASK, to & TARGET_PAGE_MASK) == 1)
490+
{
491+
DECAF_flushTranslationCache(PAGE_LEVEL,from);
492+
}
493+
}
494494

495-
//insert into the list
496-
LIST_INSERT_HEAD(&callback_list_heads[DECAF_BLOCK_END_CB], cb_struct, link);
497-
return ((DECAF_Handle)cb_struct);
495+
//insert into the list
496+
LIST_INSERT_HEAD(&callback_list_heads[DECAF_BLOCK_END_CB], cb_struct, link);
497+
return ((DECAF_Handle)cb_struct);
498498
}
499499

500500
//this is for backwards compatibility -
@@ -519,7 +519,7 @@ DECAF_Handle DECAF_register_callback(
519519
//if we are here then that means its either insn begin or end - this is the old logic no changes
520520

521521
callback_struct_t * cb_struct =
522-
(callback_struct_t *)malloc(sizeof(callback_struct_t));
522+
(callback_struct_t *)g_malloc(sizeof(callback_struct_t));
523523

524524
if(cb_struct == NULL)
525525
return (DECAF_NULL_HANDLE);
@@ -546,78 +546,74 @@ DECAF_Handle DECAF_register_callback(
546546

547547
DECAF_errno_t DECAF_unregisterOptimizedBlockBeginCallback(DECAF_Handle handle)
548548
{
549-
callback_struct_t *cb_struct, *cb_temp;
550-
551-
//to unregister the callback, we have to first find the
552-
// callback and its conditions and then remove it from the
553-
// corresonding hashtable
549+
callback_struct_t *cb_struct, *cb_temp;
554550

555-
LIST_FOREACH_SAFE(cb_struct, &callback_list_heads[DECAF_BLOCK_BEGIN_CB], link, cb_temp) {
556-
if((DECAF_Handle)cb_struct != handle)
557-
continue;
551+
//to unregister the callback, we have to first find the
552+
// callback and its conditions and then remove it from the
553+
// corresonding hashtable
558554

559-
//now that we have found it - check out its conditions
560-
switch(cb_struct->ocb_type)
561-
{
562-
default: //same as ALL to match the register function
563-
case (OCB_ALL):
564-
{
565-
enableAllBlockBeginCallbacksCount--;
566-
if (enableAllBlockBeginCallbacksCount == 0)
567-
{
568-
bEnableAllBlockBeginCallbacks = 0;
569-
//if its now zero flush the cache
570-
DECAF_flushTranslationCache(ALL_CACHE,0);
571-
}
572-
else if (enableAllBlockBeginCallbacksCount < 0)
573-
{
574-
//if it underflowed then reset to 0
575-
//this is really an error
576-
//notice I don't reset enableallblockbegincallbacks to 0
577-
// just in case
578-
enableAllBlockBeginCallbacksCount = 0;
579-
}
580-
break;
581-
}
582-
case (OCB_CONST):
583-
{
584-
if (pOBBTable == NULL)
585-
{
586-
return (NULL_POINTER_ERROR);
587-
}
588-
if (CountingHashtable_remove(pOBBTable, cb_struct->from) == 0)
589-
{
590-
//Heng: Comment out the line below, so we don't flush the translation block immediately.
591-
//Guest kernel reboot is observed if we immediately flush the translation block. So I
592-
//decide not to do so. It may even help to improve performance in certain cases.
555+
LIST_FOREACH_SAFE(cb_struct, &callback_list_heads[DECAF_BLOCK_BEGIN_CB], link, cb_temp) {
556+
if((DECAF_Handle)cb_struct != handle)
557+
continue;
593558

594-
DECAF_flushTranslationCache(BLOCK_LEVEL,cb_struct->from);
595-
}
596-
break;
597-
}
598-
case (OCB_PAGE):
599-
{
600-
if (pOBBPageTable == NULL)
601-
{
602-
return (NULL_POINTER_ERROR);
603-
}
604-
if (CountingHashtable_remove(pOBBPageTable, cb_struct->from) == 0)
605-
{
606-
DECAF_flushTranslationCache(PAGE_LEVEL,cb_struct->from);
607-
}
608-
break;
609-
}
610-
}
559+
//now that we have found it - check out its conditions
560+
switch(cb_struct->ocb_type)
561+
{
562+
default: //same as ALL to match the register function
563+
case (OCB_ALL):
564+
{
565+
enableAllBlockBeginCallbacksCount--;
566+
if (enableAllBlockBeginCallbacksCount == 0)
567+
{
568+
bEnableAllBlockBeginCallbacks = 0;
569+
//if its now zero flush the cache
570+
DECAF_flushTranslationCache(ALL_CACHE,0);
571+
}
572+
else if (enableAllBlockBeginCallbacksCount < 0)
573+
{
574+
//if it underflowed then reset to 0
575+
//this is really an error
576+
//notice I don't reset enableallblockbegincallbacks to 0
577+
// just in case
578+
enableAllBlockBeginCallbacksCount = 0;
579+
}
580+
break;
581+
}
582+
case (OCB_CONST):
583+
{
584+
if (pOBBTable == NULL)
585+
{
586+
return (NULL_POINTER_ERROR);
587+
}
588+
if (CountingHashtable_remove(pOBBTable, cb_struct->from) == 0)
589+
{
590+
DECAF_flushTranslationCache(BLOCK_LEVEL,cb_struct->from);
591+
}
592+
break;
593+
}
594+
case (OCB_PAGE):
595+
{
596+
if (pOBBPageTable == NULL)
597+
{
598+
return (NULL_POINTER_ERROR);
599+
}
600+
if (CountingHashtable_remove(pOBBPageTable, cb_struct->from) == 0)
601+
{
602+
DECAF_flushTranslationCache(PAGE_LEVEL,cb_struct->from);
603+
}
604+
break;
605+
}
606+
}
611607

612-
//now that we cleaned up the hashtables - we should remove the callback entry
613-
LIST_REMOVE(cb_struct, link);
614-
//and free the struct
615-
free(cb_struct);
608+
//now that we cleaned up the hashtables - we should remove the callback entry
609+
LIST_REMOVE(cb_struct, link);
610+
//and free the struct
611+
g_free(cb_struct);
616612

617-
return 0;
618-
}
613+
return 0;
614+
}
619615

620-
return -1;
616+
return -1;
621617
}
622618

623619

@@ -666,13 +662,12 @@ int DECAF_unregisterOptimizedBlockEndCallback(DECAF_Handle handle)
666662
else if (CountingHashmap_remove(pOBEPageMap, cb_struct->from & TARGET_PAGE_MASK, cb_struct->to & TARGET_PAGE_MASK) == 0)
667663
{
668664
DECAF_flushTranslationCache(PAGE_LEVEL,cb_struct->from & TARGET_PAGE_MASK);
669-
// DECAF_flushTranslationPage(cb_struct->from & TARGET_PAGE_MASK);
670665
}
671666

672667
//we can now remove the entry
673668
LIST_REMOVE(cb_struct, link);
674669
//and free the struct
675-
free(cb_struct);
670+
g_free(cb_struct);
676671

677672
return 0;
678673
}
@@ -698,7 +693,7 @@ int DECAF_unregister_callback(DECAF_callback_type_t cb_type, DECAF_Handle handle
698693
continue;
699694

700695
LIST_REMOVE(cb_struct, link);
701-
free(cb_struct);
696+
g_free(cb_struct);
702697

703698
#ifdef CONFIG_VMI_ENABLE
704699
if(cb_type == DECAF_TLB_EXEC_CB) {

0 commit comments

Comments
 (0)