In
computer programming
Computer programming is the process of performing a particular computation (or more generally, accomplishing a specific computing result), usually by designing and building an executable computer program. Programming involves tasks such as anal ...
, the word trampoline has a number of meanings, and is generally associated with
jump instruction
A branch is an instruction in a computer program that can cause a computer to begin executing a different instruction sequence and thus deviate from its default behavior of executing instructions in order. ''Branch'' (or ''branching'', ''branc ...
s (i.e. moving to different code paths).
Low-level programming
Trampolines (sometimes referred to as
indirect jump vectors) are memory locations holding addresses pointing to
interrupt
In digital computers, an interrupt (sometimes referred to as a trap) is a request for the processor to ''interrupt'' currently executing code (when permitted), so that the event can be processed in a timely manner. If the request is accepted, ...
service routines,
I/O routines, etc. Execution jumps into the trampoline and then immediately jumps out, or bounces, hence the term ''trampoline''. They have many uses:
* Trampoline can be used to overcome the limitations imposed by a
central processing unit
A central processing unit (CPU), also called a central processor, main processor or just Processor (computing), processor, is the electronic circuitry that executes Instruction (computing), instructions comprising a computer program. The CPU per ...
(CPU) architecture that expects to always find vectors in fixed locations.
* When an
operating system
An operating system (OS) is system software that manages computer hardware, software resources, and provides common services for computer programs.
Time-sharing operating systems schedule tasks for efficient use of the system and may also i ...
is booted on a
symmetric multiprocessing (SMP) machine, only one processor, the bootstrap processor, will be active. After the operating system has configured itself, it will instruct the other processors to jump to a piece of trampoline code that will initialize the processors and wait for the operating system to start scheduling threads on them.
High-level programming
* As used in some
Lisp implementations, a trampoline is a loop that iteratively invokes
thunk
In computer programming, a thunk is a subroutine used to inject a calculation into another subroutine. Thunks are primarily used to delay a calculation until its result is needed, or to insert operations at the beginning or end of the other subr ...
-returning functions (
continuation-passing style In functional programming, continuation-passing style (CPS) is a style of programming in which control is passed explicitly in the form of a continuation. This is contrasted with direct style, which is the usual style of programming. Gerald Jay Suss ...
). A single trampoline suffices to express all control transfers of a program; a program so expressed is trampolined, or in ''trampolined style''; converting a program to trampolined style is trampolining. Programmers can use trampolined functions to implement
tail-recursive function calls in
stack-oriented programming languages.
Continuation-passing style In functional programming, continuation-passing style (CPS) is a style of programming in which control is passed explicitly in the form of a continuation. This is contrasted with direct style, which is the usual style of programming. Gerald Jay Suss ...
is a popular intermediate format for compilers of functional languages, because many control flow constructs can be elegantly expressed and tail call optimization is easy. When compiling to a language without optimized tail calls, one can avoid stack growth via a technique called trampolining. The idea is to not make the final continuation call inside the function, but to exit and to return the continuation to a trampoline. That trampoline is simply a loop that invokes the returned continuations. Hence, there are no nested function calls and the stack won’t grow.
* In
Java
Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's mos ...
, ''trampoline'' refers to using
reflection Reflection or reflexion may refer to:
Science and technology
* Reflection (physics), a common wave phenomenon
** Specular reflection, reflection from a smooth surface
*** Mirror image, a reflection in a mirror or in water
** Signal reflection, in ...
to avoid using
inner class In object-oriented programming (OOP), an inner class or nested class is a class declared entirely within the body of another class or interface. It is distinguished from a subclass.
Overview
An instance of a normal or top-level class can exist on ...
es, for example in event listeners. The time overhead of a reflection call is traded for the space overhead of an inner class. Trampolines in Java usually involve the creation of a ''GenericListener'' to pass events to an outer class.
* In
Mono Runtime, trampolines are small, hand-written pieces of assembly code used to perform various tasks.
* When interfacing pieces of code with incompatible
calling convention
In computer science, a calling convention is an implementation-level (low-level) scheme for how subroutines or functions receive parameters from their caller and how they return a result. When some code calls a function, design choices have bee ...
s, a trampoline is used to convert the caller's convention into the callee's convention.
** In
embedded system
An embedded system is a computer system—a combination of a computer processor, computer memory, and input/output peripheral devices—that has a dedicated function within a larger mechanical or electronic system. It is ''embedded'' ...
s, trampolines are short snippets of code that start up other snippets of code. For example, rather than write interrupt handlers entirely in assembly language, another option is to write interrupt handlers mostly in C, and use a short trampoline to convert the assembly-language interrupt calling convention into the C calling convention.
** When passing a
callback
Callback may refer to:
* Callback (comedy), a joke which refers to one previously told
* Callback (computer programming), executable code that is passed as a parameter to other code
* Callback (telecommunications), the telecommunications event th ...
to a system that expects to call a
C function, but one wants it to execute the method of a particular instance of a class written in
C++
C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
, one uses a short ''trampoline'' to convert the C function-calling convention to the C++ method-calling convention. One way of writing such a trampoline is to use a
thunk
In computer programming, a thunk is a subroutine used to inject a calculation into another subroutine. Thunks are primarily used to delay a calculation until its result is needed, or to insert operations at the beginning or end of the other subr ...
.
Another method is to use a ''generic listener''.
* In
Objective-C
Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. Originally developed by Brad Cox and Tom Love in the early 1980s, it was selected by NeXT for its NeXT ...
, a trampoline is an object returned by a method that captures and
reifies all messages sent to it and then "bounces" those messages on to another object, for example in
higher order messaging.
* In the
GCC compiler
In computing, a compiler is a computer program that translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primarily used for programs tha ...
, trampoline refers to a technique for implementing pointers to
nested function
In computer programming, a nested function (or nested procedure or subroutine) is a function which is defined within another function, the ''enclosing function''. Due to simple recursive scope rules, a nested function is itself invisible outside ...
s.
The trampoline is a small piece of code which is constructed on the fly on the stack when the address of a nested function is taken. The trampoline sets up the static link pointer, which allows the nested function to access local variables of the enclosing function. The function pointer is then simply the address of the trampoline. This avoids having to use
"fat" function pointers for nested functions which carry both the code address and the static link.
This, however, conflicts with the desire to make the stack non-executable for security reasons.
* In the
esoteric programming language
An esoteric programming language (sometimes shortened to esolang) is a programming language designed to test the boundaries of computer programming language design, as a proof of concept, as software art, as a hacking interface to another language ...
Befunge
Befunge is a two-dimensional stack-based, reflective, esoteric programming language. It differs from conventional languages in that programs are arranged on a two-dimensional grid. "Arrow" instructions direct the control flow to the left, rig ...
, a trampoline is an instruction to skip the next cell in the
control flow
In computer science, control flow (or flow of control) is the order in which individual statements, instructions or function calls of an imperative program are executed or evaluated. The emphasis on explicit control flow distinguishes an ''im ...
.
No-execute stacks
Some implementations of trampolines cause a loss of
no-execute stacks (NX stack). In the
GNU Compiler Collection
The GNU Compiler Collection (GCC) is an optimizing compiler produced by the GNU Project supporting various programming languages, hardware architectures and operating systems. The Free Software Foundation (FSF) distributes GCC as free softwar ...
(GCC) in particular, a nested function builds a trampoline on the stack at runtime, and then calls the nested function through the data on stack. The trampoline requires the stack to be executable.
No-execute stacks and nested functions are mutually exclusive under GCC. If a nested function is used in the development of a program, then the NX stack is silently lost. GCC offers the
-Wtrampolines
warning to alert of the condition.
Software engineered using
secure development lifecycle often do not allow the use of nested functions due to the loss of NX stacks.
See also
*
DLL trampolining
*
Retpoline
Spectre refers to one of the two original transient execution CPU vulnerabilities (the other being Meltdown), which involve microarchitectural timing side-channel attacks. These affect modern microprocessors that perform branch prediction a ...
References
{{Reflist, refs=
[{{cite journal , author-last=Baker , author-first=Henry G. , author-link=Henry Baker (computer scientist) , date=September 1995 , title=CONS Should Not CONS Its Arguments, Part II: Cheney on the M.T.A. , journal=]ACM SIGPLAN Notices
SIGPLAN is the Association for Computing Machinery's Special Interest Group on programming language
A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, bu ...
, volume=30 , issue=9 , pages=17–20 , doi=10.1145/214448.214454 , s2cid=20720831 , url=http://home.pipeline.com/~hbaker1/CheneyMTA.html , url-status=dead , archive-url=https://web.archive.org/web/20161111222522/http://home.pipeline.com/~hbaker1/CheneyMTA.html , archive-date=2016-11-11
[{{cite web , title=C-Based Toolchain Hardening , author-last1=Walton , author-first1=Jeffrey , author-first2=Jim , author-last2=Manico , author-first3=Kevin , author-last3=Wall , date=2018-03-02 , orig-year=2013 , publisher=The Open Web Application Security Project (OWASP) , url=http://www.owasp.org/index.php/C-Based_Toolchain_Hardening#GCC.2FBinutils , access-date=2018-03-02 , url-status=live , archive-url=https://web.archive.org/web/20180527105523/https://www.owasp.org/index.php/C-Based_Toolchain_Hardening , archive-date=2018-05-27]
[{{cite web , title=Asserting Control Over the GUI: Commands, Defaults, and Resource Bundles , author-first=Hans , author-last=Muller , date=2005-01-31 , website=today.java.net , at=Trampolines , url=http://today.java.net/pub/a/today/2005/01/31/controlGUI.html#trampolines , access-date=2015-11-06 }]
https://web.archive.org/web/20180527102717/https://community.oracle.com/docs/DOC-983126 -->
/ref>
[{{cite journal , author-first=Joseph M. , author-last=Link , title=Trampolines for Embedded Systems: Minimizing interrupt handlers latency , date=2001-09-01 , journal= Dr. Dobb's Journal , url=http://www.drdobbs.com/184404772 , access-date=2018-05-26 , url-status=live , archive-url=https://web.archive.org/web/20180527101123/http://www.drdobbs.com/embedded-systems/trampolines-for-embedded-systems/184404772 , archive-date=2018-05-27]
[{{cite web , title=Thunking in Win32 with C++ , author-first=Einar Otto , author-last=Stangvik , date=2006-08-16 , url=http://www.codeproject.com/Articles/16785/Thunking-in-Win32-Simplifying-Callbacks-to-Non-sta , url-status=dead , archive-url=https://web.archive.org/web/20121015180318/http://www.codeproject.com/Articles/16785/Thunking-in-Win32-Simplifying-Callbacks-to-Non-sta , archive-date=2012-10-15]
[{{cite web , title=Higher Order Messaging (HOM) , author-first=Marcel , author-last=Weiher , date=2004 , url=http://www.metaobject.com/papers/HOM-Presentation.pdf , access-date=2018-05-26 , url-status=live , archive-url=https://web.archive.org/web/20180527101632/http://www.metaobject.com/papers/HOM-Presentation.pdf , archive-date=2018-05-27]
[{{cite web , title=Trampolines for Nested Functions , at=18.11 , date=2018 , orig-year=2002 , work=Using the GNU Compiler Collection (GCC) , url=http://gcc.gnu.org/onlinedocs/gccint/Trampolines.html , access-date=2018-05-26 , url-status=live , archive-url=https://web.archive.org/web/20180527105902/http://gcc.gnu.org/onlinedocs/gccint/Trampolines.html , archive-date=2018-05-27]
[{{cite web , title=Nested functions , at=6.4 , date=2018 , orig-year=2002 , work=Using the GNU Compiler Collection (GCC) , url=http://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html , access-date=2018-05-26 , url-status=live , archive-url=https://web.archive.org/web/20180527105902/http://gcc.gnu.org/onlinedocs/gccint/Trampolines.html , archive-date=2018-05-27]
[{{cite web , author-first=Thomas M. , author-last=Breuel , title=Lexical Closures for C++ , date=2013 , url=http://www-cs-students.stanford.edu/~blynn/files/lexic.pdf , access-date=2018-05-26 , url-status=live , archive-url=https://web.archive.org/web/20171212030215/http://www-cs-students.stanford.edu/~blynn//files/lexic.pdf , archive-date=2017-12-12]
[{{cite web , author=fuz , title=Implementation of nested functions , date=2011-11-18 , work=]StackOverflow
In software, a stack overflow occurs if the call stack pointer exceeds the stack bound. The call stack may consist of a limited amount of address space, often determined at the start of the program. The size of the call stack depends on many facto ...
, url=https://stackoverflow.com/questions/8179521/implementation-of-nested-functions , access-date=2018-05-26 , url-status=live , archive-url=https://web.archive.org/web/20160329071658/http://stackoverflow.com/questions/8179521/implementation-of-nested-functions , archive-date=2016-03-29
Computing terminology