Sanbox file IO to app data directory with IO_OP_OPEN_AT and FILE_OPEN_RESTRICT

This commit is contained in:
Martin Fouilleul 2023-06-11 18:21:04 +02:00
parent a4a3423907
commit 37ab5ca5e4
6 changed files with 102 additions and 114 deletions

@ -1 +1 @@
Subproject commit c041b212ab3c3f51d7cab6bc341e6c0e6d033962 Subproject commit b2d2d2a587f3c8a0b7dae5521f3202b92f57ae08

View File

@ -78,20 +78,26 @@ void OnInit(void)
#endif // TEST_IMAGE #endif // TEST_IMAGE
//NOTE: testing file io //NOTE: testing file io
file_handle file = file_open(STR8("./test_write.txt"), FILE_OPEN_CREATE | FILE_OPEN_WRITE); file_handle file = file_open(STR8("/test_write.txt"), FILE_OPEN_CREATE | FILE_OPEN_WRITE);
if(file_last_error(file) == IO_OK)
{
str8 string = STR8("Hello, file!\n"); str8 string = STR8("Hello, file!\n");
file_write(file, string.len, string.ptr); file_write(file, string.len, string.ptr);
file_close(file); file_close(file);
/* }
file = file_open(STR8("/dir1/test_read.txt"), IO_OPEN_READ); else
{
log_error("Couldn't open file test_write.txt\n");
}
file = file_open(STR8("/dir1/test_read.txt"), FILE_OPEN_READ);
u64 size = file_size(file); u64 size = file_size(file);
char* buffer = mem_arena_alloc(mem_scratch(), size); char* buffer = mem_arena_alloc(mem_scratch(), size);
file_read(file, size, buffer); file_read(file, size, buffer);
file_close(file); file_close(file);
log_info("read file: %.*s", (int)size, buffer); log_info("read file: %.*s", (int)size, buffer);
*/
} }
void OnFrameResize(u32 width, u32 height) void OnFrameResize(u32 width, u32 height)

View File

@ -5,10 +5,12 @@
* @date: 09/05/2023 * @date: 09/05/2023
* *
*****************************************************************/ *****************************************************************/
#include"platform/platform_io.h" #include"platform/platform_io_internal.h"
#include"orca_app.h"
io_cmp orca_io_wait_single_req(io_req* wasmReq) io_cmp orca_io_wait_single_req(io_req* wasmReq)
{ {
orca_app* orca = orca_app_get();
mem_arena* scratch = mem_scratch(); mem_arena* scratch = mem_scratch();
io_cmp cmp = {0}; io_cmp cmp = {0};
@ -17,7 +19,7 @@ io_cmp orca_io_wait_single_req(io_req* wasmReq)
// for some reason, wasm3 memory doesn't start at the beginning of the block we give it. // for some reason, wasm3 memory doesn't start at the beginning of the block we give it.
u64 bufferIndex = (u64)req.buffer & 0xffffffff; u64 bufferIndex = (u64)req.buffer & 0xffffffff;
u32 memSize = 0; u32 memSize = 0;
char* memory = (char*)m3_GetMemory(__orcaApp.runtime.m3Runtime, &memSize, 0); char* memory = (char*)m3_GetMemory(orca->runtime.m3Runtime, &memSize, 0);
if(bufferIndex + req.size > memSize) if(bufferIndex + req.size > memSize)
{ {
@ -27,20 +29,16 @@ io_cmp orca_io_wait_single_req(io_req* wasmReq)
{ {
req.buffer = memory + bufferIndex; req.buffer = memory + bufferIndex;
//TODO: do some further ownership/rights checking here, and make sure we modify flags to avoid walking out the app folder
if(req.op == IO_OP_OPEN_AT) if(req.op == IO_OP_OPEN_AT)
{ {
//////////////////////////////////////////////////////////////////////// if(req.handle.h == 0)
//TODO: should change root to app local folder {
// - if file handle is null, set it to pre-opened handle to app local folder //NOTE: change root to app local folder
// - if file handle is not null, check that it is valid req.handle = orca->rootDir;
// --> this means we probably need a second indirection: from wasm file handle to native file handle req.openFlags |= FILE_OPEN_RESTRICT;
////////////////////////////////////////////////////////////////////////
} }
}
cmp = io_wait_single_req(&req); cmp = io_wait_single_req_with_table(&req, &orca->fileTable);
} }
return(cmp); return(cmp);
} }

View File

@ -14,20 +14,14 @@
#include"milepost.h" #include"milepost.h"
#include"graphics_common.h" #include"graphics_common.h"
#include"orca_runtime.h" #include"orca_app.h"
#include"memory_impl.c" #include"memory_impl.c"
#include"io_impl.c"
#define LOG_SUBSYSTEM "Orca" #define LOG_SUBSYSTEM "Orca"
void mg_matrix_push_flat(float a11, float a12, float a13,
float a21, float a22, float a23)
{
mg_mat2x3 m = {a11, a12, a13, a21, a22, a23};
mg_matrix_push(m);
}
int orca_assert(const char* file, const char* function, int line, const char* src, const char* note) int orca_assert(const char* file, const char* function, int line, const char* src, const char* note)
{ {
mem_arena* scratch = mem_scratch(); mem_arena* scratch = mem_scratch();
@ -88,56 +82,17 @@ mg_font mg_font_create_default()
} }
typedef struct log_entry
{
list_elt listElt;
u64 cap;
log_level level;
str8 file;
str8 function;
int line;
str8 msg;
u64 recordIndex;
} log_entry;
typedef struct orca_debug_overlay
{
bool show;
mg_surface surface;
mg_canvas canvas;
mg_font fontReg;
mg_font fontBold;
ui_context ui;
mem_arena logArena;
list_info logEntries;
list_info logFreeList;
u32 entryCount;
u32 maxEntries;
u64 logEntryTotalCount;
bool logScrollToLast;
} orca_debug_overlay;
typedef struct orca_app
{
mp_window window;
mg_surface surface;
mg_canvas canvas;
orca_runtime runtime;
orca_debug_overlay debugOverlay;
} orca_app;
orca_app __orcaApp = {0}; orca_app __orcaApp = {0};
#include"io_impl.c" orca_app* orca_app_get()
{
return(&__orcaApp);
}
orca_runtime* orca_runtime_get()
{
return(&__orcaApp.runtime);
}
void orca_log(log_level level, void orca_log(log_level level,
int fileLen, int fileLen,
@ -343,11 +298,6 @@ void orca_runtime_init(orca_runtime* runtime)
runtime->wasmMemory.ptr = mem_base_reserve(allocator, runtime->wasmMemory.reserved); runtime->wasmMemory.ptr = mem_base_reserve(allocator, runtime->wasmMemory.reserved);
} }
orca_runtime* orca_runtime_get()
{
return(&__orcaApp.runtime);
}
#include"bindgen_core_api.c" #include"bindgen_core_api.c"
#include"canvas_api_bind.c" #include"canvas_api_bind.c"
#include"io_api_bind_gen.c" #include"io_api_bind_gen.c"
@ -414,36 +364,7 @@ void* orca_runloop(void* user)
return((void*)-1); return((void*)-1);
} }
//NOTE: Find heap base
u32 heapBase = 0;
{
IM3Global global = m3_FindGlobal(app->runtime.m3Module, "__heap_base");
if(global)
{
M3TaggedValue val;
M3Result res = m3_GetGlobal(global, &val);
if(!res && val.type == c_m3Type_i32)
{
heapBase = val.value.i32;
}
else
{
log_error("couldn't get value of __heap_base\n");
return((void*)-1);
}
}
else
{
log_error("couldn't locate __heap_base\n");
return((void*)-1);
}
}
//NOTE: align heap base on 16Bytes
heapBase = AlignUpOnPow2(heapBase, 16);
log_info("mem_size = %u, __heap_base = %u\n", m3_GetMemorySize(app->runtime.m3Runtime), heapBase);
//NOTE: Find and type check event handlers. //NOTE: Find and type check event handlers.
for(int i=0; i<G_EVENT_COUNT; i++) for(int i=0; i<G_EVENT_COUNT; i++)
{ {
const g_event_handler_desc* desc = &G_EVENT_HANDLER_DESC[i]; const g_event_handler_desc* desc = &G_EVENT_HANDLER_DESC[i];
@ -498,7 +419,17 @@ void* orca_runloop(void* user)
} }
} }
//NOTE: setup ui context //NOTE: preopen the app local root dir
{
str8 localRootPath = path_executable_relative(mem_scratch(), STR8("../app/data"));
io_req req = {.op = IO_OP_OPEN_AT,
.openFlags = FILE_OPEN_READ,
.size = localRootPath.len,
.buffer = localRootPath.ptr};
io_cmp cmp = io_wait_single_req_with_table(&req, &app->fileTable);
app->rootDir = cmp.handle;
}
//NOTE: prepare GL surface //NOTE: prepare GL surface
mg_surface_prepare(app->surface); mg_surface_prepare(app->surface);
@ -780,6 +711,7 @@ int main(int argc, char** argv)
orca_app* orca = &__orcaApp; orca_app* orca = &__orcaApp;
//NOTE: create window and surfaces
mp_rect windowRect = {.x = 100, .y = 100, .w = 810, .h = 610}; mp_rect windowRect = {.x = 100, .y = 100, .w = 810, .h = 610};
orca->window = mp_window_create(windowRect, "orca", 0); orca->window = mp_window_create(windowRect, "orca", 0);
orca->surface = mg_surface_create_for_window(orca->window, MG_CANVAS); orca->surface = mg_surface_create_for_window(orca->window, MG_CANVAS);

View File

@ -6,7 +6,7 @@
* *
*****************************************************************/ *****************************************************************/
#include"orca_runtime.h" #include"orca_app.h"
void* wasm_memory_resize_callback(void* p, unsigned long size, void* userData) void* wasm_memory_resize_callback(void* p, unsigned long size, void* userData)
{ {

View File

@ -8,6 +8,8 @@
#ifndef __ORCA_RUNTIME_H_ #ifndef __ORCA_RUNTIME_H_
#define __ORCA_RUNTIME_H_ #define __ORCA_RUNTIME_H_
#include"platform/platform_io_internal.h"
#include"wasm3.h" #include"wasm3.h"
#include"m3_env.h" #include"m3_env.h"
#include"m3_compile.h" #include"m3_compile.h"
@ -65,7 +67,57 @@ typedef struct orca_runtime
} orca_runtime; } orca_runtime;
typedef struct log_entry
{
list_elt listElt;
u64 cap;
log_level level;
str8 file;
str8 function;
int line;
str8 msg;
u64 recordIndex;
} log_entry;
typedef struct orca_debug_overlay
{
bool show;
mg_surface surface;
mg_canvas canvas;
mg_font fontReg;
mg_font fontBold;
ui_context ui;
mem_arena logArena;
list_info logEntries;
list_info logFreeList;
u32 entryCount;
u32 maxEntries;
u64 logEntryTotalCount;
bool logScrollToLast;
} orca_debug_overlay;
typedef struct orca_app
{
mp_window window;
mg_surface surface;
mg_canvas canvas;
file_table fileTable;
file_handle rootDir;
orca_runtime runtime;
orca_debug_overlay debugOverlay;
} orca_app;
orca_app* orca_app_get();
orca_runtime* orca_runtime_get(); orca_runtime* orca_runtime_get();