-
Notifications
You must be signed in to change notification settings - Fork 171
[onert] Add MockSycallManager for customized system call mocking #16333
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
Conversation
It adds new MockSyscallsManager class to provide configurable hook system for mocking system calls in tests. ONE-DCO-1.0-Signed-off-by: Jonghwa Lee <[email protected]>
9bacbd5 to
42cb6eb
Compare
|
Sample code is in below. MockSyscallsManager::getInstance().resetAll();
// Add a hook for fread()
MockSyscallsManager::getInstance().setFreadHook(
[](void *ptr, size_t size, size_t, FILE *) -> int {
if (size == NPUBIN_META_SIZE)
{
auto meta = reinterpret_cast<npubin_meta *>(ptr);
meta->program_size = 1024;
meta->weight_size = 1024;
meta->size = 4096;
}
return 1;
});
MockSyscallsManager::getInstance().setIoctlHook(
[](int, unsigned long request, void *arg) -> int {
// Get Version
if (request == _IOR(0x88, 1, unsigned int))
{
// Return version 3.2.X.X for trix backend sanity checking
*static_cast<int *>(arg) = 0x3020000;
}
return 0;
}); |
|
|
||
| #include "mock_syscalls.h" | ||
|
|
||
| int open(const char *pathname, int flags, ...) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previous form of open() was,
int open(const char *, int, ...) { return 0; }| { | ||
| va_list args; | ||
| va_start(args, flags); | ||
| mode_t mode = va_arg(args, mode_t); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The parameter 'mode' is only required when a file is created (O_CREAT).
| return 0; // Default mock return value | ||
| } | ||
|
|
||
| void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The previous definition of mmap() was,
void *mmap(void *, size_t, int, int, int, off_t) { return (void *)0x1; }| return (void *)0x1; // Default mock return value | ||
| } | ||
|
|
||
| int munmap(void *addr, size_t length) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The previous definition of munmap() was,
int munmap(void *, size_t) { return 0; }| return 0; // Default mock return value | ||
| } | ||
|
|
||
| int close(int fd) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previous definition of close(),
int close(int) { return 0; }| return 0; // Default mock return value | ||
| } | ||
|
|
||
| int ioctl(int fd, unsigned long request, ...) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previous definition of ioctl(),
int ioctl(int, unsigned long, ...) { return 0; }| return 0; // Default mock return value | ||
| } | ||
|
|
||
| FILE *fopen(const char *path, const char *mode) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previous definition of fopen()
size_t fread(void *, size_t, size_t, FILE *) { return 1; }| return (FILE *)0x1; // Default mock return value | ||
| } | ||
|
|
||
| int fclose(FILE *stream) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fclose() is newly added.
| return 0; | ||
| } | ||
|
|
||
| size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previous definition of fread()
size_t fread(void *, size_t, size_t, FILE *) { return 1; }| return 1; // Default mock return value | ||
| } | ||
|
|
||
| int fseek(FILE *stream, long offset, int whence) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previous definition of fseek()
int fseek(FILE *, long, int) { return 0; }| namespace onert | ||
| { | ||
| namespace backend | ||
| { | ||
| namespace trix | ||
| { | ||
| namespace ops | ||
| { | ||
| namespace test | ||
| { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please update on next PR
| namespace onert | |
| { | |
| namespace backend | |
| { | |
| namespace trix | |
| { | |
| namespace ops | |
| { | |
| namespace test | |
| { | |
| namespace onert::backend::trix::ops::test | |
| { |
hseok-oh
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
llFreetimell
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
It adds new MockSyscallsManager class to provide configurable hook system for mocking system calls in tests.
ONE-DCO-1.0-Signed-off-by: Jonghwa Lee [email protected]