From f47b72d98b01163f7bd28c79ba81a96d63ebc269 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Thu, 19 Nov 2009 01:49:26 -0600 Subject: [PATCH] Fix potential assertion failure if GC was invoked while enabling profiling --- basis/tools/profiler/profiler-tests.factor | 8 +++++++ vm/profiler.cpp | 13 +++++++++++ vm/words.cpp | 26 +++++++--------------- 3 files changed, 29 insertions(+), 18 deletions(-) diff --git a/basis/tools/profiler/profiler-tests.factor b/basis/tools/profiler/profiler-tests.factor index 6e5177fbae..8f3260d649 100644 --- a/basis/tools/profiler/profiler-tests.factor +++ b/basis/tools/profiler/profiler-tests.factor @@ -64,3 +64,11 @@ IN: tools.profiler.tests : crash-bug-2 ( -- ) 100000 [ crash-bug-1 drop ] times ; [ ] [ [ crash-bug-2 ] profile ] unit-test + +[ 1 ] [ + [ + [ [ ] (( -- )) define-temp ] with-compilation-unit + dup execute( -- ) + ] profile + counter>> +] unit-test diff --git a/vm/profiler.cpp b/vm/profiler.cpp index 403d26f0ca..19348be4ef 100755 --- a/vm/profiler.cpp +++ b/vm/profiler.cpp @@ -37,8 +37,21 @@ void factor_vm::set_profiling(bool profiling) for(cell i = 0; i < length; i++) { tagged word(array_nth(words.untagged(),i)); + + /* Note: can't do w->profiling = ... since LHS evaluates + before RHS, and if RHS does a GC, we will have an + invalid pointer on the LHS */ if(profiling) + { + if(!word->profiling) + { + code_block *profiling_block = compile_profiling_stub(word.value()); + word->profiling = profiling_block; + } + word->counter = tag_fixnum(0); + } + update_word_xt(word.untagged()); } diff --git a/vm/words.cpp b/vm/words.cpp index dfaeed2496..a9375cfbc6 100644 --- a/vm/words.cpp +++ b/vm/words.cpp @@ -23,10 +23,14 @@ word *factor_vm::allot_word(cell name_, cell vocab_, cell hashcode_) new_word->code = NULL; jit_compile_word(new_word.value(),new_word->def,true); - update_word_xt(new_word.untagged()); - if(profiling_p) + { + code_block *profiling_block = compile_profiling_stub(new_word.value()); + new_word->profiling = profiling_block; relocate_code_block(new_word->profiling); + } + + update_word_xt(new_word.untagged()); return new_word.untagged(); } @@ -58,24 +62,10 @@ void factor_vm::primitive_word_xt() } } -/* Allocates memory */ -void factor_vm::update_word_xt(word *w_) +void factor_vm::update_word_xt(word *w) { - data_root w(w_,this); - - if(profiling_p) - { - if(!w->profiling) - { - /* Note: can't do w->profiling = ... since LHS evaluates - before RHS, and if RHS does a GC, we will have an - invalid pointer on the LHS */ - code_block *profiling = compile_profiling_stub(w.value()); - w->profiling = profiling; - } - + if(profiling_p && w->profiling) w->xt = w->profiling->xt(); - } else w->xt = w->code->xt(); }