Code Monkey home page Code Monkey logo

ld-preload-zig's Introduction

LD_PRELOAD Trick With Zig

This tutorial covers how to use the LD_PRELOAD trick using with Zig on Linux. mimalloc is used as an example, but any C allocator is possible to use with this trick. Dynamically linking with libc is required for the trick to work.

Prerequisites

  1. Zig v0.12.0

  2. mimalloc

    If mimalloc was successfully installed you can check like so:

    $ ls /usr/local/lib/libmimalloc.so
    /usr/local/lib/libmimalloc.so

Tutorial

  1. Make and initialize a Zig project.

    $ mkdir ld-preload-zig
    $ cd ld-preload-zig/
    $ zig init-exe
    info: Created build.zig
    info: Created src/main.zig
    info: Next, try `zig build --help` or `zig build run`
  2. Replace the contents of src/main.zig. The C allocator is required for the LD_PRELOAD trick.

    const std = @import("std");
    const c_allocator = std.heap.c_allocator;
    
    pub fn main() !void {
        var x = try c_allocator.create(i64);
        defer c_allocator.destroy(x);
    
        x.* = 100;
    
        std.debug.print("LD_PRELOAD is {}!!\n", .{x.*});
    }
  3. Add a line to build.zig which will enable and dynamically link libc.

    (snip)
    const exe = b.addExecutable(.{
        .name = "ld-preload-zig",
        // In this case the main source file is merely a path, however, in more
        // complicated build scripts, this could be a generated file.
        .root_source_file = .{ .path = "src/main.zig" },
        .target = target,
        .optimize = optimize,
    });
    
    // Lets us use the C Allocator during build
    exe.linkLibC();
    (snip)
  4. Build the project.

    $ zig build
  5. Navigate to the executable and test that the trick works.

    $ cd zig-out/bin
    $ MIMALLOC_VERBOSE=1 LD_PRELOAD=/usr/local/lib/libmimalloc.so ./ld-preload-zig
    mimalloc: option 'show_errors': 0
    mimalloc: option 'show_stats': 0
    mimalloc: option 'verbose': 1
    (snip)

    If the trick is working, there should be verbose output from mimalloc like above.

  6. Here is the final result without the verbose output, neat!:

    $ LD_PRELOAD=/usr/local/lib/libmimalloc.so ./ld-preload-zig
    LD_PRELOAD is 100!!

ld-preload-zig's People

Contributors

jhburns avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.