Skip to content

Commit

Permalink
Zig implementation sandbox improvements
Browse files Browse the repository at this point in the history
aisap-zig:
 * Implement `sandbox` method
 * Refactor `bwrap` function

aisap-c:
 * Implement `aisap_appimage_sandbox` function, which wraps
   `AppImage.sanbox` from aisap-zig
 * Fix behavior of `aisap_appimage_wrapargs`
 * Now sandbox the application in `test.c`
 * Reformat `test.c` to use spaces instead of tabs
 * Move `test.c` to `examples`
 * Reformat `c_api.zig` to make it a bit more readable and remove old comments
  • Loading branch information
mgord9518 committed Sep 19, 2023
1 parent cf415de commit f0c3a6a
Show file tree
Hide file tree
Showing 6 changed files with 414 additions and 374 deletions.
17 changes: 2 additions & 15 deletions include/aisap.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,6 @@ typedef uint8_t aisap_error;
extern "C" {
#endif

// Go-implemented C functions. These are on the way out in favor of the Zig
// implementation. The Go version WILL stay and continue being maintained for
// other Go projects, it just won't have C bindings. This is because Go comes
// with a rather large runtime that would bloat C programs trying to use it,
// along with having some CGo quirks that are annoying to get around that just
// work in Zig.
// TODO: Make char get passed correctly. This may just be easiest to just
// make another AppImage run function that accepts char** instead of Go strings
//extern void aisap_appimage_init_go(aisap_appimage* ai, const char* path, aisap_error* err);
//extern void aisap_appimage_destroy_go(aisap_appimage* ai);
//extern int aisap_appimage_run(aisap_appimage* ai, char** args);
//extern int aisap_appimage_ismounted(aisap_appimage* ai);

// Zig-implemented C functions
// `aisap_appimage_new` initializes both the Zig and Go AppImage structs, so
// until I can get the rest of the functions ported over you'll still be able
Expand All @@ -100,13 +87,13 @@ extern size_t aisap_appimage_offset(aisap_appimage* ai, aisap_error*
extern const char* aisap_appimage_md5(aisap_appimage* ai, char* buf, size_t buf_len, aisap_error* err);
extern void aisap_appimage_mount(aisap_appimage* ai, char* path, aisap_error* err);
extern const char* aisap_appimage_mount_dir(aisap_appimage* ai);
extern void aisap_appimage_sandbox(aisap_appimage* ai, int argc, char** args, aisap_error* err);
extern char** aisap_appimage_wrapargs(aisap_appimage* ai, aisap_error* err);

// THESE FUNCTIONS NOT YET IMPLEMENTED
//extern uint8_t aisap_appimage_sandbox(aisap_appimage* ai, int argc, char** args);
//extern char* aisap_appimage_mountdir(aisap_appimage* ai);
//extern char* aisap_appimage_tempdir(aisap_appimage* ai);
//extern char* aisap_appimage_runid(aisap_appimage* ai);
extern char** aisap_appimage_wrapargs(aisap_appimage* ai, aisap_error* err);

// For ABI compat with libAppImage
extern off_t appimage_get_payload_offset(const char* path);
Expand Down
6 changes: 4 additions & 2 deletions zig/build_test.sh → zig/examples/build_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
# which will generate the `test` executable, which prints debug information
# about an AppImage

# TODO: also build `test.c` in `build.zig`

$CC -static \
-o test test.c \
../libaisap-x86_64.a \
-I../include
../../libaisap-x86_64.a \
-I../../include

# -lfuse3 \
9 changes: 5 additions & 4 deletions zig/examples/src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn printSockets(slice: []AppImage.SocketPermissions) void {
}

pub fn main() !void {
var allocator = std.heap.c_allocator;
var allocator = std.heap.page_allocator;
var args = std.process.args();

// Skip arg[0]
Expand Down Expand Up @@ -96,10 +96,11 @@ pub fn main() !void {
try ai.mount(.{});

const wrapArgs = try ai.wrapArgs(allocator);

try aisap.bwrap(allocator, wrapArgs);

printWrapArgs(wrapArgs);

try ai.sandbox(.{
//.args = &[_][]const u8{"build"},
});
}

fn printWrapArgs(args: []const []const u8) void {
Expand Down
13 changes: 11 additions & 2 deletions zig/test.c → zig/examples/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,19 @@ int main(int argc, char** argv) {
return err;
}

// printf(" wrapargs: %s\n", wrap_args[0]);

printf(" wrapargs:\n");
char** i = wrap_args;
for (char* str = *i; str; str = *++i) {
printf("%s ", str);
}
printf("\n");

aisap_appimage_sandbox(&ai, argc - 2, argv + 2, &err);
if (err) {
printf("%d\n", err);
return err;
}

// Test libappimage API:
printf("libappimage API:\n");
printf(" offset: %ld\n", appimage_get_payload_offset(ai.path));
Expand Down
Loading

0 comments on commit f0c3a6a

Please sign in to comment.