Skip to content

Commit 7366a42

Browse files
authored
Trusted modules (#496)
1 parent 127e678 commit 7366a42

8 files changed

+55
-40
lines changed

playground/umka.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/umka_common.c

+6-4
Original file line numberDiff line numberDiff line change
@@ -343,17 +343,17 @@ int moduleAdd(Modules *modules, const char *path)
343343
}
344344

345345

346-
char *moduleFindSource(Modules *modules, const char *path)
346+
ModuleSource *moduleFindSource(Modules *modules, const char *path)
347347
{
348348
unsigned int pathHash = hash(path);
349349
for (int i = 0; i < modules->numModuleSources; i++)
350350
if (modules->moduleSource[i]->pathHash == pathHash && strcmp(modules->moduleSource[i]->path, path) == 0)
351-
return modules->moduleSource[i]->source;
351+
return modules->moduleSource[i];
352352
return NULL;
353353
}
354354

355355

356-
void moduleAddSource(Modules *modules, const char *path, const char *source)
356+
void moduleAddSource(Modules *modules, const char *path, const char *source, bool trusted)
357357
{
358358
if (modules->numModuleSources >= MAX_MODULES)
359359
modules->error->handler(modules->error->context, "Too many module sources");
@@ -380,6 +380,7 @@ void moduleAddSource(Modules *modules, const char *path, const char *source)
380380
moduleSource->source[sourceLen] = 0;
381381

382382
moduleSource->pathHash = hash(path);
383+
moduleSource->trusted = trusted;
383384

384385
modules->moduleSource[modules->numModuleSources++] = moduleSource;
385386
}
@@ -621,12 +622,13 @@ External *externalFind(Externals *externals, const char *name)
621622
}
622623

623624

624-
External *externalAdd(Externals *externals, const char *name, void *entry)
625+
External *externalAdd(Externals *externals, const char *name, void *entry, bool resolveInTrusted)
625626
{
626627
External *external = malloc(sizeof(External));
627628

628629
external->entry = entry;
629630
external->resolved = false;
631+
external->resolveInTrusted = resolveInTrusted;
630632

631633
strncpy(external->name, name, DEFAULT_STR_LEN);
632634
external->name[DEFAULT_STR_LEN] = 0;

src/umka_common.h

+5-4
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ typedef struct
142142
char path[DEFAULT_STR_LEN + 1], folder[DEFAULT_STR_LEN + 1], name[DEFAULT_STR_LEN + 1];
143143
unsigned int pathHash;
144144
char *source;
145+
bool trusted;
145146
} ModuleSource;
146147

147148

@@ -180,7 +181,7 @@ typedef struct tagExternal
180181
char name[DEFAULT_STR_LEN + 1];
181182
unsigned int hash;
182183
void *entry;
183-
bool resolved;
184+
bool resolved, resolveInTrusted;
184185
struct tagExternal *next;
185186
} External;
186187

@@ -222,8 +223,8 @@ void moduleNameFromPath (Modules *modules, const char *path, char *folde
222223
int moduleFind (Modules *modules, const char *path);
223224
int moduleFindImported (Modules *modules, Blocks *blocks, const char *name);
224225
int moduleAdd (Modules *modules, const char *path);
225-
char *moduleFindSource (Modules *modules, const char *path);
226-
void moduleAddSource (Modules *modules, const char *path, const char *source);
226+
ModuleSource *moduleFindSource (Modules *modules, const char *path);
227+
void moduleAddSource (Modules *modules, const char *path, const char *source, bool trusted);
227228
void *moduleGetImplLibFunc (Module *module, const char *name);
228229
char *moduleCurFolder (char *buf, int size);
229230
bool modulePathIsAbsolute (const char *path);
@@ -241,7 +242,7 @@ int blocksCurrent(Blocks *blocks);
241242
void externalInit (Externals *externals);
242243
void externalFree (Externals *externals);
243244
External *externalFind (Externals *externals, const char *name);
244-
External *externalAdd (Externals *externals, const char *name, void *entry);
245+
External *externalAdd (Externals *externals, const char *name, void *entry, bool resolveInTrusted);
245246

246247

247248
static inline unsigned int hash(const char *str)

src/umka_compiler.c

+27-25
Original file line numberDiff line numberDiff line change
@@ -172,27 +172,27 @@ static void compilerDeclareBuiltinIdents(Compiler *comp)
172172

173173
static void compilerDeclareExternalFuncs(Compiler *comp, bool fileSystemEnabled)
174174
{
175-
externalAdd(&comp->externals, "rtlmemcpy", &rtlmemcpy);
176-
externalAdd(&comp->externals, "rtlstdin", &rtlstdin);
177-
externalAdd(&comp->externals, "rtlstdout", &rtlstdout);
178-
externalAdd(&comp->externals, "rtlstderr", &rtlstderr);
179-
externalAdd(&comp->externals, "rtlfopen", fileSystemEnabled ? &rtlfopen : &rtlfopenSandbox);
180-
externalAdd(&comp->externals, "rtlfclose", fileSystemEnabled ? &rtlfclose : &rtlfcloseSandbox);
181-
externalAdd(&comp->externals, "rtlfread", fileSystemEnabled ? &rtlfread : &rtlfreadSandbox);
182-
externalAdd(&comp->externals, "rtlfwrite", fileSystemEnabled ? &rtlfwrite : &rtlfwriteSandbox);
183-
externalAdd(&comp->externals, "rtlfseek", fileSystemEnabled ? &rtlfseek : &rtlfseekSandbox);
184-
externalAdd(&comp->externals, "rtlftell", fileSystemEnabled ? &rtlftell : &rtlftellSandbox);
185-
externalAdd(&comp->externals, "rtlremove", fileSystemEnabled ? &rtlremove : &rtlremoveSandbox);
186-
externalAdd(&comp->externals, "rtlfeof", fileSystemEnabled ? &rtlfeof : &rtlfeofSandbox);
187-
externalAdd(&comp->externals, "rtlfflush", &rtlfflush);
188-
externalAdd(&comp->externals, "rtltime", &rtltime);
189-
externalAdd(&comp->externals, "rtlclock", &rtlclock);
190-
externalAdd(&comp->externals, "rtllocaltime", &rtllocaltime);
191-
externalAdd(&comp->externals, "rtlgmtime", &rtlgmtime);
192-
externalAdd(&comp->externals, "rtlmktime", &rtlmktime);
193-
externalAdd(&comp->externals, "rtlgetenv", fileSystemEnabled ? &rtlgetenv : &rtlgetenvSandbox);
194-
externalAdd(&comp->externals, "rtlsystem", fileSystemEnabled ? &rtlsystem : &rtlsystemSandbox);
195-
externalAdd(&comp->externals, "rtltrace", &rtltrace);
175+
externalAdd(&comp->externals, "rtlmemcpy", &rtlmemcpy, true);
176+
externalAdd(&comp->externals, "rtlstdin", &rtlstdin, true);
177+
externalAdd(&comp->externals, "rtlstdout", &rtlstdout, true);
178+
externalAdd(&comp->externals, "rtlstderr", &rtlstderr, true);
179+
externalAdd(&comp->externals, "rtlfopen", fileSystemEnabled ? &rtlfopen : &rtlfopenSandbox, true);
180+
externalAdd(&comp->externals, "rtlfclose", fileSystemEnabled ? &rtlfclose : &rtlfcloseSandbox, true);
181+
externalAdd(&comp->externals, "rtlfread", fileSystemEnabled ? &rtlfread : &rtlfreadSandbox, true);
182+
externalAdd(&comp->externals, "rtlfwrite", fileSystemEnabled ? &rtlfwrite : &rtlfwriteSandbox, true);
183+
externalAdd(&comp->externals, "rtlfseek", fileSystemEnabled ? &rtlfseek : &rtlfseekSandbox, true);
184+
externalAdd(&comp->externals, "rtlftell", fileSystemEnabled ? &rtlftell : &rtlftellSandbox, true);
185+
externalAdd(&comp->externals, "rtlremove", fileSystemEnabled ? &rtlremove : &rtlremoveSandbox, true);
186+
externalAdd(&comp->externals, "rtlfeof", fileSystemEnabled ? &rtlfeof : &rtlfeofSandbox, true);
187+
externalAdd(&comp->externals, "rtlfflush", &rtlfflush, true);
188+
externalAdd(&comp->externals, "rtltime", &rtltime, true);
189+
externalAdd(&comp->externals, "rtlclock", &rtlclock, true);
190+
externalAdd(&comp->externals, "rtllocaltime", &rtllocaltime, true);
191+
externalAdd(&comp->externals, "rtlgmtime", &rtlgmtime, true);
192+
externalAdd(&comp->externals, "rtlmktime", &rtlmktime, true);
193+
externalAdd(&comp->externals, "rtlgetenv", fileSystemEnabled ? &rtlgetenv : &rtlgetenvSandbox, true);
194+
externalAdd(&comp->externals, "rtlsystem", fileSystemEnabled ? &rtlsystem : &rtlsystemSandbox, true);
195+
externalAdd(&comp->externals, "rtltrace", &rtltrace, true);
196196
}
197197

198198

@@ -225,7 +225,7 @@ void compilerInit(Compiler *comp, const char *fileName, const char *sourceString
225225
comp->lex.tok.pos = 1;
226226
comp->debug.fnName = "<unknown>";
227227

228-
lexInit(&comp->lex, &comp->storage, &comp->debug, filePath, sourceString, &comp->error);
228+
lexInit(&comp->lex, &comp->storage, &comp->debug, filePath, sourceString, false, &comp->error);
229229

230230
comp->argc = argc;
231231
comp->argv = argv;
@@ -256,7 +256,9 @@ void compilerInit(Compiler *comp, const char *fileName, const char *sourceString
256256
{
257257
char runtimeModulePath[DEFAULT_STR_LEN + 1] = "";
258258
moduleAssertRegularizePath(&comp->modules, runtimeModuleNames[i], comp->modules.curFolder, runtimeModulePath, DEFAULT_STR_LEN + 1);
259-
moduleAddSource(&comp->modules, runtimeModulePath, runtimeModuleSources[i]);
259+
260+
const bool runtimeModuleTrusted = strcmp(runtimeModuleNames[i], "std.um") == 0;
261+
moduleAddSource(&comp->modules, runtimeModulePath, runtimeModuleSources[i], runtimeModuleTrusted);
260262
}
261263
}
262264

@@ -321,7 +323,7 @@ bool compilerAddModule(Compiler *comp, const char *fileName, const char *sourceS
321323
if (moduleFindSource(&comp->modules, modulePath))
322324
return false;
323325

324-
moduleAddSource(&comp->modules, modulePath, sourceString);
326+
moduleAddSource(&comp->modules, modulePath, sourceString, false);
325327
return true;
326328
}
327329

@@ -331,7 +333,7 @@ bool compilerAddFunc(Compiler *comp, const char *name, ExternFunc func)
331333
if (externalFind(&comp->externals, name))
332334
return false;
333335

334-
externalAdd(&comp->externals, name, func);
336+
externalAdd(&comp->externals, name, func, false);
335337
return true;
336338
}
337339

src/umka_decl.c

+11-2
Original file line numberDiff line numberDiff line change
@@ -826,8 +826,17 @@ static void parseImportItem(Compiler *comp)
826826

827827
// Module source strings, if any, have precedence over files
828828
char *sourceString = NULL;
829+
bool sourceTrusted = false;
830+
829831
if (moduleRegularizePath(comp->lex.tok.strVal, comp->modules.curFolder, path, DEFAULT_STR_LEN + 1))
830-
sourceString = moduleFindSource(&comp->modules, path);
832+
{
833+
const ModuleSource *sourceDesc = moduleFindSource(&comp->modules, path);
834+
if (sourceDesc)
835+
{
836+
sourceString = sourceDesc->source;
837+
sourceTrusted = sourceDesc->trusted;
838+
}
839+
}
831840

832841
if (!sourceString)
833842
moduleAssertRegularizePath(&comp->modules, comp->lex.tok.strVal, comp->modules.module[comp->blocks.module]->folder, path, DEFAULT_STR_LEN + 1);
@@ -853,7 +862,7 @@ static void parseImportItem(Compiler *comp)
853862
int currentModule = comp->blocks.module;
854863
DebugInfo currentDebug = comp->debug;
855864
Lexer currentLex = comp->lex;
856-
lexInit(&comp->lex, &comp->storage, &comp->debug, path, sourceString, &comp->error);
865+
lexInit(&comp->lex, &comp->storage, &comp->debug, path, sourceString, sourceTrusted, &comp->error);
857866

858867
lexNext(&comp->lex);
859868
importedModule = parseModule(comp);

src/umka_lexer.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ enum
109109
static unsigned int keywordHash[NUM_KEYWORDS];
110110

111111

112-
int lexInit(Lexer *lex, Storage *storage, DebugInfo *debug, const char *fileName, const char *sourceString, Error *error)
112+
int lexInit(Lexer *lex, Storage *storage, DebugInfo *debug, const char *fileName, const char *sourceString, bool trusted, Error *error)
113113
{
114114
// Fill keyword hashes
115115
for (int i = 0; i < NUM_KEYWORDS; i++)
@@ -120,6 +120,7 @@ int lexInit(Lexer *lex, Storage *storage, DebugInfo *debug, const char *fileName
120120

121121
lex->error = error;
122122
lex->hasSourceString = false;
123+
lex->trusted = trusted;
123124
lex->buf = NULL;
124125
int bufLen = 0;
125126

src/umka_lexer.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ typedef struct
118118
typedef struct
119119
{
120120
char *fileName;
121-
bool hasSourceString;
121+
bool hasSourceString, trusted;
122122
char *buf;
123123
int bufPos, line, pos;
124124
Token tok, prevTok;
@@ -128,7 +128,7 @@ typedef struct
128128
} Lexer;
129129

130130

131-
int lexInit(Lexer *lex, Storage *storage, DebugInfo *debug, const char *fileName, const char *sourceString, Error *error);
131+
int lexInit(Lexer *lex, Storage *storage, DebugInfo *debug, const char *fileName, const char *sourceString, bool trusted, Error *error);
132132
void lexFree(Lexer *lex);
133133
void lexNext(Lexer *lex);
134134
void lexNextForcedSemicolon(Lexer *lex);

src/umka_stmt.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ void doResolveExtern(Compiler *comp)
8282
if (external->resolved)
8383
comp->error.handler(comp->error.context, "External %s is already resolved", ident->name);
8484

85-
if (!comp->lex.hasSourceString)
85+
if (!comp->lex.hasSourceString || (external->resolveInTrusted && !comp->lex.trusted))
8686
comp->error.handler(comp->error.context, "Cannot resolve %s in this module", ident->name);
8787

8888
fn = external->entry;

0 commit comments

Comments
 (0)