View source with formatted comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2018, VU University Amsterdam
    7    All rights reserved.
    8
    9    Redistribution and use in source and binary forms, with or without
   10    modification, are permitted provided that the following conditions
   11    are met:
   12
   13    1. Redistributions of source code must retain the above copyright
   14       notice, this list of conditions and the following disclaimer.
   15
   16    2. Redistributions in binary form must reproduce the above copyright
   17       notice, this list of conditions and the following disclaimer in
   18       the documentation and/or other materials provided with the
   19       distribution.
   20
   21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   32    POSSIBILITY OF SUCH DAMAGE.
   33*/
   34
   35:- module(obfuscate, []).   36
   37/** <module> Code obfuscating
   38
   39This       module       provides       an       implementation       for
   40prolog:obfuscate_identifiers/1, a hook into   library(qsave) that allows
   41mapping atoms to other atoms while creating a saved state.
   42
   43This library is  used  by  qsave_program/2   and  the  `-c`  option. The
   44following is good way to create a binary  version of your code that runs
   45on a machine with the same version of SWI-Prolog installed.
   46
   47    swipl -o myprog -O --autoload=false --obfuscate=true -c load.pl
   48
   49This implementation is overly conservative: atoms   are  only renamed if
   50they are used once. This is  verified   by  checking  the atom reference
   51count. If this is `1`, the  atom  is   _only_  used  in the functor that
   52defines the predicate.
   53
   54@tbd Using the current obfuscation scheme we  must verify that all usage
   55of an atom refers to a predicate   name. Right now, declarations such as
   56=|:- dynamic p/1.|= ensure `p` is used in   `p/1`  and p(_) and thus has
   57two references.
   58*/
   59
   60:- dynamic
   61    nomap/1.   62
   63prolog:obfuscate_identifiers(_) :-
   64    print_message(informational, obfuscate(start)),
   65    flag('$obfuscate_id', Old, 1),
   66    forall(module_property(M, class(user)),
   67           obfuscate_module(M)),
   68    flag('$obfuscate_id', End, Old),
   69    Obfuscated is End-Old,
   70    print_message(informational, obfuscate(done(Obfuscated))).
   71
   72obfuscate_module(M) :-
   73    forall(private_predicate(M:P),
   74           obfuscate_predicate(M:P)).
   75
   76private_predicate(M:Head) :-
   77    current_predicate(M:Name/Arity),
   78    functor(Head, Name, Arity),
   79    \+ predicate_property(M:Head, imported_from(_)),
   80%   \+ predicate_property(M:Head, exported),
   81    \+ predicate_property(M:Head, foreign),
   82    \+ predicate_property(M:Head, public).
   83
   84obfuscate_predicate(_M:P) :-
   85    functor(P, Name, _Arity),
   86    '$atom_references'(Name, 1),
   87    !,
   88    flag('$obfuscate_id', Id, Id+1),
   89    atom_concat('$P$', Id, Name2),
   90    (   nomap(Name)
   91    ->  true
   92    ;   catch('$map_id'(Name, Name2), _, fail)
   93    ->  print_message(silent, obfuscate(map(Name, Name2)))
   94    ;   '$unmap_id'(Name),
   95        asserta(nomap(Name))
   96    ).
   97obfuscate_predicate(_).
   98
   99
  100		 /*******************************
  101		 *            MESSAGES		*
  102		 *******************************/
  103
  104:- multifile
  105    prolog:message//1.  106
  107prolog:message(obfuscate(Msg)) -->
  108    message(Msg).
  109
  110message(start) -->
  111    [ 'Obfuscating predicates names (conservative) ...'-[] ].
  112message(done(Count)) -->
  113    [ 'Obfuscated ~D predicate names'-[Count] ].
  114message(map(From, To)) -->
  115    [ 'Obfuscating ~q as ~q'-[From, To] ]