Skip to content

Instantly share code, notes, and snippets.

@nikkaroraa
Last active May 5, 2022 08:05
Show Gist options
  • Save nikkaroraa/27edb398fbcc22868c0dd96fe24b2b2b to your computer and use it in GitHub Desktop.
Save nikkaroraa/27edb398fbcc22868c0dd96fe24b2b2b to your computer and use it in GitHub Desktop.

solana-bpf

As shown in the diagram above, a program author creates a program, compiles it to an ELF shared object containing BPF bytecode, and uploads it to the Solana cluster with a special deploy transaction. The cluster makes it available to clients via a program ID. The program ID is an address specified when deploying and is used to reference the program in subsequent transactions.

Upon a successful deployment the account that holds the program is marked executable. If the program is marked "final", its account data become permanently immutable. If any changes are required to the finalized program (features, patches, etc...) the new program must be deployed to a new program ID.

If a program is upgradeable, the account that holds the program is marked executable, but it is possible to redeploy a new shared object to the same program ID, provided that the program's upgrade authority signs the transaction.

The Solana command line interface supports deploying programs, for more information see the deploy command line usage documentation.


What is ELF?

Executable and linking format (ELF) ELF is the standard binary format on operating systems such as Linux. The ELF representation permits object files to be identified, parsed, and interpreted similarly, making the ELF object files compatible across multiple platforms and architectures of different size.

What are shared objects in Linux?

so (short for "shared object"). Shared libraries are the most common way to manage dependencies on Linux systems. These shared resources are loaded into memory before the application starts, and when several processes require the same library, it will be loaded only once on the system.

Job of BPF verifier

The BPF verifier prevents unsafe programs from running.

What is Bytecode?

Byte code is an intermediate code between the source code and machine code. It is a low-level code that is the result of the compilation of a source code which is written in a high-level language. It is processed by a virtual machine like Java Virtual Machine (JVM).

Byte code is a non-runnable code after it is translated by an interpreter into machine code then it is understandable by the machine. It is compiled to run on JVM, any system having JVM can run it irrespective of their operating system. That’s why Java is platform-independent. Byte code is referred to as a Portable code.

What is BPF loader?

Deploys, upgrades, and executes programs on the chain.

Program id: BPFLoaderUpgradeab1e11111111111111111111111
Instructions: LoaderInstruction

The BPF Upgradeable Loader marks itself as "owner" of the executable and program-data accounts it creates to store your program. When a user invokes an instruction via a program id, the Solana runtime will load both your the program and its owner, the BPF Upgradeable Loader. The runtime then passes your program to the BPF Upgradeable Loader to process the instruction.

What is BPF?

Berkeley Packet Filter (BPF)

Solana on-chain programs are compiled via the LLVM compiler infrastructure to an Executable and Linkable Format (ELF) containing a variation of the Berkeley Packet Filter (BPF) bytecode.


Entire workflow

Source: courses.cs.washington.edu

  • Developers
    • Write a BPF program in bytecode directly
    • Or, write a C program and compile it to BPF bytecode
  • Submit it to Solana Runtime (Sealevel)
  • The Solana runtime invokes the BPF verifier for sanity check
    • Example checks
      • No division by zero
      • No out-of-bounds memory access
      • No unbounded loops
  • Execution: attach BPF program to hook points in the runtime via ProgramId
    • Option 1: run the BPF interpreter: simple & slow, but disabled nowadays by default due to the concerns about the Spectre attacks.
    • Option 2: run a just-in-time (JIT) compiler to translate BPF programs to machine code and attach machine code instead.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment