Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[test] Add regression test for miscompilation #316

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions test/rt/x86-64-linux/zreadlink0.v3
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
def LOOKUP_SYMLINK_FOLLOW = 1; // flag indicating should follow symlinks
def SYS_readlink = 0x59; // Linux system call to read a symlink
def PATH_MAX = 15; // maximum length of a path
def EINVAL = 22; // errno for invalid argument
def ENOENT = 2; // errno for no entry


// Resolves a path without following symlinks, ensuring it stays within the sandbox.
def resolve_path_without_link(path: string) -> (string, int) {
return (path, 0);
}

// Main function to resolve a path with an option to follow symlinks, ensuring it stays within the sandbox.
def resolve_path0(fdpath: string, path: string, flags: i32) -> (string, int) {
var result = resolve_path_without_link(path);
if (result.1 != 0) return (result.0, result.1);
var normalized = result.0;

if ((flags & LOOKUP_SYMLINK_FOLLOW) == LOOKUP_SYMLINK_FOLLOW) {
var buffer = string.new(PATH_MAX);
var t = Linux.syscall(SYS_readlink, (Pointer.atContents(normalized), Pointer.atContents(buffer), PATH_MAX));

if (t.0 < 0) return (normalized, 0);
return (buffer, 0); // If the path is a symlink, return it
}
return (normalized, 0);
}

def main() -> int {
System.puts("##+resolve_path\n");
var expected = "/foo/test_path";
var result = resolve_path0("/foo", "/foo/test_path", 0).0;
if (expected == null) {
System.puts("##-fail, expected is null");
} else if (result == null) {
System.puts("##-fail, result is null");
} else if (!equal(expected, result)) {
System.puts("##-fail, got: ");
System.puts(result);
} else {
System.puts("##-ok\n");
return 0;
}
return 1;
}

// Compare two strings for equality.
def equal(arr1: string, arr2: string) -> bool {
if (arr1 == arr2) return true;
if (arr1.length != arr2.length) return false;
for (i < arr1.length) {
if (arr1[i] != arr2[i]) return false;
}
return true;
}
1 change: 1 addition & 0 deletions test/rt/x86-64-linux/zreadlink0.v3.exit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
2 changes: 2 additions & 0 deletions test/rt/x86-64-linux/zreadlink0.v3.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
##+resolve_path
##-ok
Loading