[Scilab-Dev] Hijacking built-in "set" almost works...

stephane.mottelet at utc.fr stephane.mottelet at utc.fr
Fri Mar 2 16:59:09 CET 2018


  Quoting Stéphane Mottelet <stephane.mottelet at utc.fr>:

> Le 02/03/2018 à 13:00, Stéphane Mottelet a écrit :
>
>> Le 01/03/2018 à 23:44, Samuel Gougeon a écrit :
>>
>>> Le 01/03/2018 à 23:26, Stéphane Mottelet a écrit :
>>>
>>>> Le 01/03/2018 à 21:57, Stéphane Mottelet a écrit :
>>>>
>>>>> Hello,
>>>>>
>>>>>> Le 1 mars 2018 à 20:18, Samuel Gougeon <sgougeon at free.fr>[1] a écrit :
>>>>>>
>>>>>>> Le 01/03/2018 à 20:09, Stéphane Mottelet a écrit :
>>>>>>> Hello,
>>>>>>>
>>>>>>> I have tried to implement a (naïve) hijacking mechanism that  
>>>>>>> works *like a charm* under scilab 5.5.2:
>>>>>>>
>>>>>>> newfun('scilab_set',funptr('set'));
>>>>>>> clearfun('set');
>>>>>>> function set(varargin)
>>>>>>>     scilab_set(varargin(:))
>>>>>>>     printf("...\n")
>>>>>>> endfunction
>>>>>>> // test the hijacked "set"
>>>>>>> f=gcf()
>>>>>>> set(f,"tag","1")
>>>>>>> f.tag="2"
>>>>>>>
>>>>>>> When I test this under  6.0.0 or 6.0.1, the last line always  
>>>>>>> crashes Scilab. Is there an evident reason why ? When trying  
>>>>>>> this on the command line (scilab -nw)
>>>>>>>
>>>>>>>> --> set(f,"tag","1")
>>>>>>>> ...
>>>>>>>>
>>>>>>>> --> f.tag="2"
>>>>>>>> ...
>>>>>>>> /Applications/scilab-6.0.1.app/Contents/MacOS/bin/scilab:  
>>>>>>>> line 972: 27117 Illegal instruction: 4  "$SCILABBIN" "$@"
>>>>>>>
>>>>>>> I can see that the problem occurs *after* the insertion code  
>>>>>>> f.tag="2" has delegated the operation to the hijacked "set",  
>>>>>>> hence the crash occurs in the built-in function which does the  
>>>>>>> insertion. In order to understand why this crash occurs (under  
>>>>>>> Linux or OSX), I would like to know which module is in charge  
>>>>>>> of the insertion for handles (a kind of %h_i but built-in) ?
>>>>>>
>>>>>> You may have a look at
>>>>>> SCI\modules\graphic_objects\src\cpp\setGraphicObjectProperty.cpp
>>>>>> and
>>>>>> SCI\modules\graphic_objects\src\java\org\scilab\modules\graphic_objects\*
>>>>>
>>>>> Hmm, the crash occurs *after* the property has been set. So my  
>>>>> question was rather about the generic code that handles field  
>>>>> insertion, which is delegated to “set” for handles.
>>>>>
>>>>> S.
>>>>>  
>>>>
>>>> I have localized many methods for handle type in  
>>>> modules/ast/src/cpp/types/graphichandle.cpp. Although I don't  
>>>> know much of C++, I can see that GraphicHandle::invoke is  
>>>> delegated to the overloaded %h_e macro, hence is an extraction  
>>>> method, but I am not able to find any *insertion* method in this  
>>>> file. Maybe someone knows ?
>>>
>>> Maybe
>>> edit generic_i_h
>>
>> I found it in modules/ast/src/cpp/ast/visitor_common.cpp
>>
>> When you don't have any idea of where to look, sometimes a "grep  
>> theStringToFind -r ." is useful :
>>
>>    2241[2]         else if (_pVar->isHandle())
>>    2242[3]         {
>>    2243[4]             if (_pArgs->size() == 1 && (*_pArgs)[0]->isString())
>>    2244[5]             {
>>    2245[6]                 //s(["x"])
>>    2246[7]  
>>                 types::GraphicHandle* pH = _pVar->getAs<types::GraphicHandle>();
>>    2247[8]  
>>                 types::String *pS = (*_pArgs)[0]->getAs<types::String>();
>>    2248[9]                 types::typed_list in;
>>    2249[10]                 types::typed_list out;
>>    2250[11]                 types::optional_list opt;
>>    2251[12]
>>    2252[13]                 in.push_back(pH);
>>    2253[14]                 in.push_back(pS);
>>    2254[15]                 in.push_back(_pInsert);
>>    2255[16]
>>    2256[17]  
>>                 types::Function* pCall = (types::Function*)symbol::Context::getInstance()->get(symbol::Symbol(L"set"));
>>    2257[18]                 if (pCall)
>>    2258[19]                 {
>>    2259[20]  
>>                     types::Callable::ReturnValue ret = pCall->call(in, opt, 1, out);
>>    2260[21]                     if (ret == types::Callable::OK)
>>    2261[22]                     {
>>    2262[23]                         pRet = _pVar;
>>    2263[24]                     }
>>    2264[25]                     else
>>    2265[26]                     {
>>    2266[27]  
>>                         throw ast::InternalError(ConfigVariable::getLastErrorMessage(), ConfigVariable::getLastErrorNumber(), e.getLocation());
>>    2267[28]                     }
>>    2268[29]                 }
>>    2269[30]             }
>>    2270[31]             else
>>    2271[32]             {
>>    2272[33]  
>>                 pRet = _pVar->getAs<types::GraphicHandle>()->insert(_pArgs, _pInsert);
>>    2273[34]             }
>>    2274[35]         }
>>
>> here one can see the delegation to "set" at line 2256...2259.  
>> However, the internal overload mechanism is still very obscure to  
>> me. Now that I can build scilab_master on my Linux machine, I have  
>> played a little bit and replaced lines 2246..2268 by the sole lines
>>
>> pRet = callOverload(e, L"i", _pArgs, _pInsert, _pVar);  
>> sciprint("after callOverload\n")
>>    then recompiled the ast module. The result is a scilab with a  
>> fully working graphics, e.g. typing "plot" without arguments works  
>> as usual and triggers a lot of handle insertions (a bunch of "after  
>> callOverload" are printed). However, there is no %h_i macro, so I  
>> don't understand by which miracle it could work.
>>
>>    Antoine it think that you are the author of this file  
>> (/ast/src/cpp/ast/visitor_common.cpp) can you explain how it works ?
>>
>>    Thanks in advance,
>>
>>    S.
>
> Ok, no miracle... in fact, if the callOverload is forced as  
> explained above, then generic_i_h is called (thanks Samuel for your  
> hint), and generic_i_h itself calls "set". So this is almost  
> equivalent, besides the fact that "set" is called from a Scilab  
> macro and not from visitor_common.cpp above. With this solution the  
> hijacking I had proposed does not crash anymore.
>
> But after all these complications, I guess that there may be a more  
> straightforward way of implementing new handle properties at the  
> user level...

To finish on this subject and maybe feed the conversation on this  
subject, here is the final set/get hijacking code and and a simple  
example of what if allows to do: e.g. here we add a 'xlim' property to  
the Axes entity. At the user level it is enough to write the two  
functions  %Axes_set_xlim_property and  
%Axes_get_xlim_property(varargin). Of course, this works only if the  
"set" hijacking does not crash scilab, which needs the hack into  
modules/ast/src/cpp/ast/visitor_common.cpp describe above.

S.

newfun('scilab_set',funptr('set'));
clearfun('set');
newfun('scilab_get',funptr('get'));
clearfun('get');
function set(varargin)
    try
        if length(varargin)==3           
            execstr('%'+sprintf('%s_set_%s_property(varargin(:))',...
            scilab_get(varargin(1),'type'),varargin(2)))
        end
    catch
        scilab_set(varargin(:))
    end
end

function out=get(varargin)
    try
        if length(varargin)==2    
            out=evstr('%'+sprintf('%s_get_%s_property(varargin(:))',...
            scilab_get(varargin(1),'type'),varargin(2));
        else
            out=scilab_get(varargin(:))
        end
    catch
        out=scilab_get(varargin(:))
    end
end

FUNCTION %AXES_SET_XLIM_PROPERTY(VARARGIN)
    VARARGIN(1).DATA_BOUNDS(1:2)=VARARGIN(3)(:);
END

FUNCTION OUT=%AXES_GET_XLIM_PROPERTY(VARARGIN)
    OUT=VARARGIN(1).DATA_BOUNDS(1:2);
END

CLF
PLOT
A=GCF().CHILDREN
A(2).XLIM=[2 4];
DISP(A(1).XLIM)



Links:
------
[1] mailto:sgougeon at free.fr
[2]  
http://gitweb.scilab.org/?p=scilab.git;a=blob;f=scilab/modules/ast/src/cpp/ast/visitor_common.cpp;h=6379d14e630d747599fd48e0e9cfa907cab214cd;hb=b5bcf45d3328c87b56ed5d8f9dbec623504f7c9b#l2241
[3]  
http://gitweb.scilab.org/?p=scilab.git;a=blob;f=scilab/modules/ast/src/cpp/ast/visitor_common.cpp;h=6379d14e630d747599fd48e0e9cfa907cab214cd;hb=b5bcf45d3328c87b56ed5d8f9dbec623504f7c9b#l2242
[4]  
http://gitweb.scilab.org/?p=scilab.git;a=blob;f=scilab/modules/ast/src/cpp/ast/visitor_common.cpp;h=6379d14e630d747599fd48e0e9cfa907cab214cd;hb=b5bcf45d3328c87b56ed5d8f9dbec623504f7c9b#l2243
[5]  
http://gitweb.scilab.org/?p=scilab.git;a=blob;f=scilab/modules/ast/src/cpp/ast/visitor_common.cpp;h=6379d14e630d747599fd48e0e9cfa907cab214cd;hb=b5bcf45d3328c87b56ed5d8f9dbec623504f7c9b#l2244
[6]  
http://gitweb.scilab.org/?p=scilab.git;a=blob;f=scilab/modules/ast/src/cpp/ast/visitor_common.cpp;h=6379d14e630d747599fd48e0e9cfa907cab214cd;hb=b5bcf45d3328c87b56ed5d8f9dbec623504f7c9b#l2245
[7]  
http://gitweb.scilab.org/?p=scilab.git;a=blob;f=scilab/modules/ast/src/cpp/ast/visitor_common.cpp;h=6379d14e630d747599fd48e0e9cfa907cab214cd;hb=b5bcf45d3328c87b56ed5d8f9dbec623504f7c9b#l2246
[8]  
http://gitweb.scilab.org/?p=scilab.git;a=blob;f=scilab/modules/ast/src/cpp/ast/visitor_common.cpp;h=6379d14e630d747599fd48e0e9cfa907cab214cd;hb=b5bcf45d3328c87b56ed5d8f9dbec623504f7c9b#l2247
[9]  
http://gitweb.scilab.org/?p=scilab.git;a=blob;f=scilab/modules/ast/src/cpp/ast/visitor_common.cpp;h=6379d14e630d747599fd48e0e9cfa907cab214cd;hb=b5bcf45d3328c87b56ed5d8f9dbec623504f7c9b#l2248
[10]  
http://gitweb.scilab.org/?p=scilab.git;a=blob;f=scilab/modules/ast/src/cpp/ast/visitor_common.cpp;h=6379d14e630d747599fd48e0e9cfa907cab214cd;hb=b5bcf45d3328c87b56ed5d8f9dbec623504f7c9b#l2249
[11]  
http://gitweb.scilab.org/?p=scilab.git;a=blob;f=scilab/modules/ast/src/cpp/ast/visitor_common.cpp;h=6379d14e630d747599fd48e0e9cfa907cab214cd;hb=b5bcf45d3328c87b56ed5d8f9dbec623504f7c9b#l2250
[12]  
http://gitweb.scilab.org/?p=scilab.git;a=blob;f=scilab/modules/ast/src/cpp/ast/visitor_common.cpp;h=6379d14e630d747599fd48e0e9cfa907cab214cd;hb=b5bcf45d3328c87b56ed5d8f9dbec623504f7c9b#l2251
[13]  
http://gitweb.scilab.org/?p=scilab.git;a=blob;f=scilab/modules/ast/src/cpp/ast/visitor_common.cpp;h=6379d14e630d747599fd48e0e9cfa907cab214cd;hb=b5bcf45d3328c87b56ed5d8f9dbec623504f7c9b#l2252
[14]  
http://gitweb.scilab.org/?p=scilab.git;a=blob;f=scilab/modules/ast/src/cpp/ast/visitor_common.cpp;h=6379d14e630d747599fd48e0e9cfa907cab214cd;hb=b5bcf45d3328c87b56ed5d8f9dbec623504f7c9b#l2253
[15]  
http://gitweb.scilab.org/?p=scilab.git;a=blob;f=scilab/modules/ast/src/cpp/ast/visitor_common.cpp;h=6379d14e630d747599fd48e0e9cfa907cab214cd;hb=b5bcf45d3328c87b56ed5d8f9dbec623504f7c9b#l2254
[16]  
http://gitweb.scilab.org/?p=scilab.git;a=blob;f=scilab/modules/ast/src/cpp/ast/visitor_common.cpp;h=6379d14e630d747599fd48e0e9cfa907cab214cd;hb=b5bcf45d3328c87b56ed5d8f9dbec623504f7c9b#l2255
[17]  
http://gitweb.scilab.org/?p=scilab.git;a=blob;f=scilab/modules/ast/src/cpp/ast/visitor_common.cpp;h=6379d14e630d747599fd48e0e9cfa907cab214cd;hb=b5bcf45d3328c87b56ed5d8f9dbec623504f7c9b#l2256
[18]  
http://gitweb.scilab.org/?p=scilab.git;a=blob;f=scilab/modules/ast/src/cpp/ast/visitor_common.cpp;h=6379d14e630d747599fd48e0e9cfa907cab214cd;hb=b5bcf45d3328c87b56ed5d8f9dbec623504f7c9b#l2257
[19]  
http://gitweb.scilab.org/?p=scilab.git;a=blob;f=scilab/modules/ast/src/cpp/ast/visitor_common.cpp;h=6379d14e630d747599fd48e0e9cfa907cab214cd;hb=b5bcf45d3328c87b56ed5d8f9dbec623504f7c9b#l2258
[20]  
http://gitweb.scilab.org/?p=scilab.git;a=blob;f=scilab/modules/ast/src/cpp/ast/visitor_common.cpp;h=6379d14e630d747599fd48e0e9cfa907cab214cd;hb=b5bcf45d3328c87b56ed5d8f9dbec623504f7c9b#l2259
[21]  
http://gitweb.scilab.org/?p=scilab.git;a=blob;f=scilab/modules/ast/src/cpp/ast/visitor_common.cpp;h=6379d14e630d747599fd48e0e9cfa907cab214cd;hb=b5bcf45d3328c87b56ed5d8f9dbec623504f7c9b#l2260
[22]  
http://gitweb.scilab.org/?p=scilab.git;a=blob;f=scilab/modules/ast/src/cpp/ast/visitor_common.cpp;h=6379d14e630d747599fd48e0e9cfa907cab214cd;hb=b5bcf45d3328c87b56ed5d8f9dbec623504f7c9b#l2261
[23]  
http://gitweb.scilab.org/?p=scilab.git;a=blob;f=scilab/modules/ast/src/cpp/ast/visitor_common.cpp;h=6379d14e630d747599fd48e0e9cfa907cab214cd;hb=b5bcf45d3328c87b56ed5d8f9dbec623504f7c9b#l2262
[24]  
http://gitweb.scilab.org/?p=scilab.git;a=blob;f=scilab/modules/ast/src/cpp/ast/visitor_common.cpp;h=6379d14e630d747599fd48e0e9cfa907cab214cd;hb=b5bcf45d3328c87b56ed5d8f9dbec623504f7c9b#l2263
[25]  
http://gitweb.scilab.org/?p=scilab.git;a=blob;f=scilab/modules/ast/src/cpp/ast/visitor_common.cpp;h=6379d14e630d747599fd48e0e9cfa907cab214cd;hb=b5bcf45d3328c87b56ed5d8f9dbec623504f7c9b#l2264
[26]  
http://gitweb.scilab.org/?p=scilab.git;a=blob;f=scilab/modules/ast/src/cpp/ast/visitor_common.cpp;h=6379d14e630d747599fd48e0e9cfa907cab214cd;hb=b5bcf45d3328c87b56ed5d8f9dbec623504f7c9b#l2265
[27]  
http://gitweb.scilab.org/?p=scilab.git;a=blob;f=scilab/modules/ast/src/cpp/ast/visitor_common.cpp;h=6379d14e630d747599fd48e0e9cfa907cab214cd;hb=b5bcf45d3328c87b56ed5d8f9dbec623504f7c9b#l2266
[28]  
http://gitweb.scilab.org/?p=scilab.git;a=blob;f=scilab/modules/ast/src/cpp/ast/visitor_common.cpp;h=6379d14e630d747599fd48e0e9cfa907cab214cd;hb=b5bcf45d3328c87b56ed5d8f9dbec623504f7c9b#l2267
[29]  
http://gitweb.scilab.org/?p=scilab.git;a=blob;f=scilab/modules/ast/src/cpp/ast/visitor_common.cpp;h=6379d14e630d747599fd48e0e9cfa907cab214cd;hb=b5bcf45d3328c87b56ed5d8f9dbec623504f7c9b#l2268
[30]  
http://gitweb.scilab.org/?p=scilab.git;a=blob;f=scilab/modules/ast/src/cpp/ast/visitor_common.cpp;h=6379d14e630d747599fd48e0e9cfa907cab214cd;hb=b5bcf45d3328c87b56ed5d8f9dbec623504f7c9b#l2269
[31]  
http://gitweb.scilab.org/?p=scilab.git;a=blob;f=scilab/modules/ast/src/cpp/ast/visitor_common.cpp;h=6379d14e630d747599fd48e0e9cfa907cab214cd;hb=b5bcf45d3328c87b56ed5d8f9dbec623504f7c9b#l2270
[32]  
http://gitweb.scilab.org/?p=scilab.git;a=blob;f=scilab/modules/ast/src/cpp/ast/visitor_common.cpp;h=6379d14e630d747599fd48e0e9cfa907cab214cd;hb=b5bcf45d3328c87b56ed5d8f9dbec623504f7c9b#l2271
[33]  
http://gitweb.scilab.org/?p=scilab.git;a=blob;f=scilab/modules/ast/src/cpp/ast/visitor_common.cpp;h=6379d14e630d747599fd48e0e9cfa907cab214cd;hb=b5bcf45d3328c87b56ed5d8f9dbec623504f7c9b#l2272
[34]  
http://gitweb.scilab.org/?p=scilab.git;a=blob;f=scilab/modules/ast/src/cpp/ast/visitor_common.cpp;h=6379d14e630d747599fd48e0e9cfa907cab214cd;hb=b5bcf45d3328c87b56ed5d8f9dbec623504f7c9b#l2273
[35]  
http://gitweb.scilab.org/?p=scilab.git;a=blob;f=scilab/modules/ast/src/cpp/ast/visitor_common.cpp;h=6379d14e630d747599fd48e0e9cfa907cab214cd;hb=b5bcf45d3328c87b56ed5d8f9dbec623504f7c9b#l2274
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.scilab.org/pipermail/dev/attachments/20180302/72f4a4d8/attachment.htm>


More information about the dev mailing list