View source with raw 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)  2014-2021, VU University Amsterdam
    7                              SWI-Prolog Solutions b.v.
    8    All rights reserved.
    9
   10    Redistribution and use in source and binary forms, with or without
   11    modification, are permitted provided that the following conditions
   12    are met:
   13
   14    1. Redistributions of source code must retain the above copyright
   15       notice, this list of conditions and the following disclaimer.
   16
   17    2. Redistributions in binary form must reproduce the above copyright
   18       notice, this list of conditions and the following disclaimer in
   19       the documentation and/or other materials provided with the
   20       distribution.
   21
   22    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   23    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   24    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   25    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   26    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   27    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   28    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   29    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   30    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   31    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   32    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   33    POSSIBILITY OF SUCH DAMAGE.
   34*/
   35
   36:- module(modules,
   37          [ in_temporary_module/3,              % ?Module, :Setup, :Goal
   38            current_temporary_module/1          % ?Module
   39          ]).

Module utility predicates

*/

   45:- meta_predicate
   46    in_temporary_module(?, 0, 0).
 in_temporary_module(?Module, :Setup, :Goal)
Run Goal on temporary loaded sources and discard the module and loaded predicates after completion. This predicate performs the following steps:
  1. If Module is unbound, create a unique identifier for it.
  2. Turn Module into a temporary module using set_module/1. Note that this requires the module to be non-existent or empty. If Module is specified, it should typically be set to a unique value as obtained from e.g. uuid/1.
  3. Run Setup in the context of Module.
  4. If setup succeeded possible choice points are discarded and Goal is started.

The logical result of this predicate is the same as `(Setup@Module -> Goal@Module)`, i.e., both Setup and Goal are resolved relative to the current module, but executed in the context of Module. If Goal must be called in Module, use call(Goal).

The module and all its predicates are destroyed after Goal terminates, as defined by setup_call_cleanup/3.

Discussion This predicate is intended to load programs in an isolated environment and reclaim all resources. This unfortunately is incomplete:

See also
- library(sandbox) determines whether unknown goals are safe to call.
- load_files/2 offers the option sandboxed(true) to load code from unknown sources safely.
   88in_temporary_module(Module, Setup, Goal) :-
   89    setup_call_cleanup(
   90        prepare_temporary_module(Module),
   91        (   @(Setup, Module)
   92        ->  @(Goal,  Module)
   93        ),
   94        destroy_module(Module)).
   95
   96prepare_temporary_module(Module) :-
   97    var(Module),
   98    !,
   99    (   thread_id(Tid),
  100        repeat,
  101        I is random(1<<62),
  102        atomic_list_concat([tmp, Tid, I], -, Module),
  103        catch(set_module(Module:class(temporary)),
  104              error(permission_error(_,_,_),_), fail)
  105    ->  b_setval('$tmp_module', Module)
  106    ).
  107prepare_temporary_module(Module) :-
  108    set_module(Module:class(temporary)),
  109    b_setval('$tmp_module', Module).
  110
  111:- if(current_prolog_flag(threads, true)).  112thread_id(Id) :-
  113    thread_self(Self),
  114    thread_property(Self, id(Id)).
  115:- else.  116thread_id(main).
  117:- endif.  118
  119destroy_module(Module) :-
  120    retractall(system:'$load_context_module'(_File, Module, _Options)),
  121    '$destroy_module'(Module).
 current_temporary_module(-Module) is semidet
True when we are executing in the given temporary module context.
  127current_temporary_module(Module) :-
  128    nb_current('$tmp_module', Module)