Skip to content

Commit

Permalink
macOS ci added and zig library test
Browse files Browse the repository at this point in the history
  • Loading branch information
kassane committed Sep 25, 2024
1 parent 4c87f35 commit 9796b25
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 27 deletions.
19 changes: 15 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Zig build
name: Build & Test

on: [push]

Expand All @@ -7,19 +7,30 @@ jobs:
strategy:
fail-fast: false
matrix:
runs-on: [ubuntu-latest]
runs-on: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.runs-on }}
steps:
- uses: actions/checkout@v4
with:
submodules: recursive
fetch-depth: 0

- name: Download & Extract packages
- name: Download & Extract packages (Linux)
if: startsWith(matrix.runs-on, 'ubuntu')
run: |
curl -LO https://github.com/kassane/zig-mos-bootstrap/releases/download/0.2/zig-mos-x86_64-linux-musl-baseline.tar.xz
tar -xf zig-mos-x86_64-linux-musl-baseline.tar.xz
echo "$PWD/zig-mos-x86_64-linux-musl-baseline" >> $GITHUB_PATH
- name: Download & Extract packages (MacOS M1)
if: startsWith(matrix.runs-on, 'macos')
run: |
curl -LO https://github.com/kassane/zig-mos-bootstrap/releases/download/0.2/zig-mos-aarch64-macos-apple_m1.tar.xz
tar -xf zig-mos-aarch64-macos-apple_m1.tar.xz
echo "$PWD/zig-mos-aarch64-macos-apple_m1" >> $GITHUB_PATH
- name: Build
run: zig build rtsan -Dtests --summary all -freference-trace
run: zig build -Dtests --summary all -freference-trace

- name: Run
run: zig build rtsan -Dtests -freference-trace
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ CTestTestfile.cmake
_deps
CMakeUserPresets.json
*zig-*
*.d
45 changes: 27 additions & 18 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,41 @@ pub fn build(b: *std.Build) void {
const tests = b.option(bool, "tests", "build tests") orelse false;

// FIXME
// {
// const exe = b.addExecutable(.{
// .name = "test-asan",
// .target = target,
// .optimize = optimize,
// });
// exe.addCSourceFile(.{
// .file = b.path("tests/vec_test.cc"),
// .flags = &.{"-fsanitize=address"},
// });
// buildASan(b, exe);

// if (tests) {
// b.installArtifact(exe);
// const run = b.addRunArtifact(exe);
// const run_step = b.step("asan", "Run the test-asan test");
// run_step.dependOn(&run.step);
// }
// }
// LLVM 20
{
const exe = b.addExecutable(.{
.name = "test-asan",
const libzig = b.addStaticLibrary(.{
.name = "zig",
.target = target,
.optimize = optimize,
.root_source_file = b.path("tests/array.zig"),
});
exe.addCSourceFile(.{
.file = b.path("tests/vec_test.cc"),
.flags = &.{"-fsanitize=address"},
});
buildASan(b, exe);
libzig.linkLibC();

if (tests) {
b.installArtifact(exe);
const run = b.addRunArtifact(exe);
const run_step = b.step("asan", "Run the test-asan test");
run_step.dependOn(&run.step);
}
}
// LLVM 20
{
const exe = b.addExecutable(.{
.name = "test-rtsan",
.target = target,
.optimize = optimize,
});
exe.linkLibrary(libzig);
exe.addCSourceFile(.{
.file = b.path("tests/vec_test.cc"),
.flags = &.{"-fsanitize=realtime"},
Expand Down Expand Up @@ -122,8 +131,8 @@ pub fn buildASan(b: *std.Build, lib: *std.Build.Step.Compile) void {
if (lib.rootModuleTarget().cpu.arch.isX86())
lib.addAssemblyFile(dep.path("lib/asan/asan_rtl_x86_64.S"));

buildLSan(lib, dep);
buildUBSan(lib, dep);
// buildLSan(lib, dep);
// buildUBSan(lib, dep);
buildInterception(lib, dep);
buildSanCommon(lib, dep);

Expand Down
32 changes: 32 additions & 0 deletions tests/array.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const std = @import("std");

// [[clang::nonblocking]]
fn array() callconv(.C) void {

// raw_c_allocator: Real-time violation: intercepted call to real-time unsafe function
// `malloc` in real-time context!
// c_allocator: Real-time violation: intercepted call to real-time unsafe function
// `posix_memalign` in real-time context!
// page_allocator (no-libc/syscalls): None

var arr = std.ArrayList([]const u8).init(std.heap.raw_c_allocator);
defer arr.deinit();
arr.append("foo") catch unreachable;
arr.append("bar") catch unreachable;
arr.append("baz") catch unreachable;
}

fn openfile() callconv(.C) void {
const file = std.fs.cwd().openFile("tests/malloc.d", .{}) catch unreachable;
defer file.close();
}

comptime {
@export(&array, .{
.name = "ffi_zigarray",
.linkage = .weak,
});
@export(&openfile, .{
.name = "ffi_zig_openfile",
});
}
24 changes: 19 additions & 5 deletions tests/vec_test.cc
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
/// Reference:
/// https://github.com/CppCon/CppCon2024/blob/main/Presentations/LLVMs_Realtime_Safety_Revolution.pdf

#include <vector>
// #include <vector>

int process(int) [[clang::nonblocking]] {
std::vector<int> v(16);
return v[16];
extern "C" {
void ffi_zigarray();
void ffi_zig_openfile();
}

// int process(int) [[clang::nonblocking]] {
// std::vector<int> v(16);
// return v[16];
// }

void zig_main() [[clang::nonblocking]] {
ffi_zigarray();
ffi_zig_openfile();
}

int main() {
zig_main();
return 0;
}
int main() { return process(0); }

0 comments on commit 9796b25

Please sign in to comment.