[onert] Add MockSycallManager for customized system call mocking#16333
[onert] Add MockSycallManager for customized system call mocking#16333hseok-oh merged 1 commit intoSamsung:masterfrom
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 <jonghwa3.lee@samsung.com>
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
Please update on next PR
| namespace onert | |
| { | |
| namespace backend | |
| { | |
| namespace trix | |
| { | |
| namespace ops | |
| { | |
| namespace test | |
| { | |
| namespace onert::backend::trix::ops::test | |
| { |
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 jonghwa3.lee@samsung.com