configurable trans-membrance hostcall blocklist
This commit is contained in:
+17
-3
@@ -5,12 +5,13 @@
|
||||
#include <cstdlib>
|
||||
#include <cctype>
|
||||
#include <filesystem>
|
||||
#include <fcntl.h>
|
||||
#include <sys/file.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace {
|
||||
|
||||
const u64 UCE_UNIT_ABI_VERSION = 7;
|
||||
const u64 UCE_UNIT_ABI_VERSION = 8;
|
||||
|
||||
struct SharedUnitFilesystemState
|
||||
{
|
||||
@@ -210,15 +211,28 @@ int compiler_open_lock_file(String file_name, String purpose)
|
||||
auto lock_dir = dirname(file_name);
|
||||
if(lock_dir != "")
|
||||
mkdir(lock_dir);
|
||||
int fdlock = file_open_locked(file_name, O_RDWR | O_CREAT, LOCK_EX, 0666);
|
||||
int fdlock = open(file_name.c_str(), O_RDWR | O_CREAT, 0666);
|
||||
if(fdlock == -1)
|
||||
{
|
||||
printf("(!) Could not open lock file %s\n", file_name.c_str());
|
||||
return(fdlock);
|
||||
}
|
||||
fcntl(fdlock, F_SETFD, FD_CLOEXEC);
|
||||
if(flock(fdlock, LOCK_EX) != 0)
|
||||
{
|
||||
close(fdlock);
|
||||
printf("(!) Could not lock file %s\n", file_name.c_str());
|
||||
return(-1);
|
||||
}
|
||||
return(fdlock);
|
||||
}
|
||||
|
||||
void compiler_close_lock_file(int fdlock)
|
||||
{
|
||||
file_close_locked(fdlock);
|
||||
if(fdlock == -1)
|
||||
return;
|
||||
flock(fdlock, LOCK_UN);
|
||||
close(fdlock);
|
||||
}
|
||||
|
||||
String compiler_normalize_unit_path(Request* context, String file_name)
|
||||
|
||||
@@ -344,3 +344,80 @@ f64 draw_float(f64 from, f64 to, f64 decimal_precision)
|
||||
return(gen_float(from, to, context->random_index++, context->random_seed, decimal_precision));
|
||||
}
|
||||
|
||||
|
||||
namespace {
|
||||
struct SHA256_CTX_UCE { u8 data[64]; u32 datalen; unsigned long long bitlen; u32 state[8]; };
|
||||
#define UCE_SHA256_ROTR(a,b) (((a) >> (b)) | ((a) << (32-(b))))
|
||||
#define UCE_SHA256_CH(x,y,z) (((x) & (y)) ^ (~(x) & (z)))
|
||||
#define UCE_SHA256_MAJ(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
|
||||
#define UCE_SHA256_EP0(x) (UCE_SHA256_ROTR(x,2) ^ UCE_SHA256_ROTR(x,13) ^ UCE_SHA256_ROTR(x,22))
|
||||
#define UCE_SHA256_EP1(x) (UCE_SHA256_ROTR(x,6) ^ UCE_SHA256_ROTR(x,11) ^ UCE_SHA256_ROTR(x,25))
|
||||
#define UCE_SHA256_SIG0(x) (UCE_SHA256_ROTR(x,7) ^ UCE_SHA256_ROTR(x,18) ^ ((x) >> 3))
|
||||
#define UCE_SHA256_SIG1(x) (UCE_SHA256_ROTR(x,17) ^ UCE_SHA256_ROTR(x,19) ^ ((x) >> 10))
|
||||
static const u32 uce_sha256_k[64] = {
|
||||
0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,
|
||||
0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,
|
||||
0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,
|
||||
0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,
|
||||
0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,
|
||||
0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070,
|
||||
0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,
|
||||
0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2 };
|
||||
static void uce_sha256_transform(SHA256_CTX_UCE* ctx, const u8 data[])
|
||||
{
|
||||
u32 m[64];
|
||||
for(u32 i=0,j=0; i<16; ++i,j+=4) m[i]=((u32)data[j]<<24)|((u32)data[j+1]<<16)|((u32)data[j+2]<<8)|((u32)data[j+3]);
|
||||
for(u32 i=16; i<64; ++i) m[i]=UCE_SHA256_SIG1(m[i-2])+m[i-7]+UCE_SHA256_SIG0(m[i-15])+m[i-16];
|
||||
u32 a=ctx->state[0],b=ctx->state[1],c=ctx->state[2],d=ctx->state[3],e=ctx->state[4],f=ctx->state[5],g=ctx->state[6],h=ctx->state[7];
|
||||
for(u32 i=0; i<64; ++i) { u32 t1=h+UCE_SHA256_EP1(e)+UCE_SHA256_CH(e,f,g)+uce_sha256_k[i]+m[i]; u32 t2=UCE_SHA256_EP0(a)+UCE_SHA256_MAJ(a,b,c); h=g; g=f; f=e; e=d+t1; d=c; c=b; b=a; a=t1+t2; }
|
||||
ctx->state[0]+=a; ctx->state[1]+=b; ctx->state[2]+=c; ctx->state[3]+=d; ctx->state[4]+=e; ctx->state[5]+=f; ctx->state[6]+=g; ctx->state[7]+=h;
|
||||
}
|
||||
static void uce_sha256_init(SHA256_CTX_UCE* ctx)
|
||||
{
|
||||
ctx->datalen=0; ctx->bitlen=0; ctx->state[0]=0x6a09e667; ctx->state[1]=0xbb67ae85; ctx->state[2]=0x3c6ef372; ctx->state[3]=0xa54ff53a; ctx->state[4]=0x510e527f; ctx->state[5]=0x9b05688c; ctx->state[6]=0x1f83d9ab; ctx->state[7]=0x5be0cd19;
|
||||
}
|
||||
static void uce_sha256_update(SHA256_CTX_UCE* ctx, const u8 data[], size_t len)
|
||||
{
|
||||
for(size_t i=0; i<len; ++i) { ctx->data[ctx->datalen++]=data[i]; if(ctx->datalen==64) { uce_sha256_transform(ctx,ctx->data); ctx->bitlen += 512; ctx->datalen=0; } }
|
||||
}
|
||||
static void uce_sha256_final(SHA256_CTX_UCE* ctx, u8 hash[])
|
||||
{
|
||||
u32 i=ctx->datalen;
|
||||
ctx->data[i++]=0x80;
|
||||
if(i>56) { while(i<64) ctx->data[i++]=0; uce_sha256_transform(ctx,ctx->data); i=0; }
|
||||
while(i<56) ctx->data[i++]=0;
|
||||
ctx->bitlen += (unsigned long long)ctx->datalen * 8ull;
|
||||
for(int j=7; j>=0; --j) ctx->data[63-j]=(u8)(ctx->bitlen >> (j*8));
|
||||
uce_sha256_transform(ctx,ctx->data);
|
||||
for(i=0; i<4; ++i) for(u32 j=0; j<8; ++j) hash[i + j*4] = (u8)((ctx->state[j] >> (24 - i*8)) & 0xff);
|
||||
}
|
||||
}
|
||||
|
||||
String sha256_native(String data)
|
||||
{
|
||||
u8 digest[32]; SHA256_CTX_UCE ctx; uce_sha256_init(&ctx); uce_sha256_update(&ctx, (const u8*)data.data(), data.size()); uce_sha256_final(&ctx, digest);
|
||||
return(String((const char*)digest, 32));
|
||||
}
|
||||
String sha256_hex_native(String data)
|
||||
{
|
||||
String digest = sha256_native(data), out; for(unsigned char c : digest) out += to_hex(c, 2); return(to_lower(out));
|
||||
}
|
||||
String hmac_sha256_native(String key, String data)
|
||||
{
|
||||
if(key.size() > 64) key = sha256_native(key);
|
||||
key.resize(64, '\0');
|
||||
String o(64, '\0'), i(64, '\0');
|
||||
for(size_t n=0; n<64; n++) { o[n] = key[n] ^ 0x5c; i[n] = key[n] ^ 0x36; }
|
||||
return(sha256_native(o + sha256_native(i + data)));
|
||||
}
|
||||
String hmac_sha256_hex_native(String key, String data)
|
||||
{
|
||||
String digest = hmac_sha256_native(key, data), out; for(unsigned char c : digest) out += to_hex(c, 2); return(to_lower(out));
|
||||
}
|
||||
bool crypto_equal_native(String a, String b)
|
||||
{
|
||||
u8 diff = (u8)(a.size() ^ b.size());
|
||||
size_t n = a.size() > b.size() ? a.size() : b.size();
|
||||
for(size_t i=0; i<n; i++) { u8 ca = i<a.size() ? (u8)a[i] : 0; u8 cb = i<b.size() ? (u8)b[i] : 0; diff |= ca ^ cb; }
|
||||
return(diff == 0);
|
||||
}
|
||||
|
||||
@@ -19,3 +19,13 @@ f64 gen_float(f64 from, f64 to, u64 index, u64 seed = 0, f64 decimal_precision =
|
||||
|
||||
u64 draw_int(u64 from, u64 to);
|
||||
f64 draw_float(f64 from, f64 to, f64 decimal_precision = 0.000000000001);
|
||||
String sha256_native(String data);
|
||||
String sha256_hex_native(String data);
|
||||
String hmac_sha256_native(String key, String data);
|
||||
String hmac_sha256_hex_native(String key, String data);
|
||||
bool crypto_equal_native(String a, String b);
|
||||
String sha256(String data);
|
||||
String sha256_hex(String data);
|
||||
String hmac_sha256(String key, String data);
|
||||
String hmac_sha256_hex(String key, String data);
|
||||
bool crypto_equal(String a, String b);
|
||||
|
||||
+311
-95
@@ -22,6 +22,25 @@ void uce_host_file_unlink(const char* path, size_t path_len, const char* current
|
||||
size_t uce_host_file_list(const char* path, size_t path_len, const char* current, size_t current_len, char* buf, size_t cap);
|
||||
int uce_host_file_mkdir(const char* path, size_t path_len, const char* current, size_t current_len);
|
||||
int64_t uce_host_file_mtime(const char* path, size_t path_len, const char* current, size_t current_len);
|
||||
uint64_t uce_host_file_open(const char* path, size_t path_len, const char* current, size_t current_len, const char* mode, size_t mode_len);
|
||||
size_t uce_host_file_handle_read(uint64_t handle, uint64_t len, char* buf, size_t cap);
|
||||
size_t uce_host_file_handle_pread(uint64_t handle, uint64_t offset, uint64_t len, char* buf, size_t cap);
|
||||
uint64_t uce_host_file_handle_write(uint64_t handle, const char* data, size_t data_len);
|
||||
uint64_t uce_host_file_handle_pwrite(uint64_t handle, uint64_t offset, const char* data, size_t data_len);
|
||||
int64_t uce_host_file_handle_seek(uint64_t handle, int64_t offset, int whence);
|
||||
int64_t uce_host_file_handle_tell(uint64_t handle);
|
||||
void uce_host_file_handle_close(uint64_t handle);
|
||||
size_t uce_host_file_stat(const char* path, size_t path_len, const char* current, size_t current_len, char* buf, size_t cap);
|
||||
size_t uce_host_dir_list(const char* path, size_t path_len, const char* current, size_t current_len, char* buf, size_t cap);
|
||||
int uce_host_file_rename(const char* from, size_t from_len, const char* to, size_t to_len, const char* current, size_t current_len);
|
||||
int uce_host_file_copy(const char* from, size_t from_len, const char* to, size_t to_len, const char* current, size_t current_len);
|
||||
int uce_host_file_truncate(const char* path, size_t path_len, const char* current, size_t current_len, uint64_t size);
|
||||
int uce_host_dir_remove(const char* path, size_t path_len, const char* current, size_t current_len, int recursive);
|
||||
size_t uce_host_file_temp(const char* prefix, size_t prefix_len, const char* current, size_t current_len, char* buf, size_t cap);
|
||||
int uce_host_file_chmod(const char* path, size_t path_len, const char* current, size_t current_len, uint32_t mode);
|
||||
int uce_host_file_symlink(const char* target, size_t target_len, const char* linkpath, size_t linkpath_len, const char* current, size_t current_len);
|
||||
size_t uce_host_file_readlink(const char* path, size_t path_len, const char* current, size_t current_len, char* buf, size_t cap);
|
||||
int uce_host_file_fsync(uint64_t handle);
|
||||
int uce_host_task_spawn(const char* key, size_t key_len, uint64_t callback_id, double interval, uint64_t timeout, int repeat);
|
||||
int uce_host_task_pid(const char* key, size_t key_len);
|
||||
int uce_host_task_kill(int pid, int sig);
|
||||
@@ -36,11 +55,21 @@ size_t uce_host_memcache_command(uint64_t sockfd, const char* command, size_t co
|
||||
size_t uce_host_mysql(const char* in, size_t in_len, char* out, size_t cap);
|
||||
size_t uce_host_request_perf(const char* in, size_t in_len, char* out, size_t cap);
|
||||
size_t uce_host_shell_exec(const char* cmd, size_t cmd_len, char* buf, size_t cap);
|
||||
int uce_host_file_open_locked(const char* path, size_t path_len, int open_flags, int lock_type, int create_mode, double wait_timeout_seconds, const char* purpose, size_t purpose_len, const char* current, size_t current_len);
|
||||
void uce_host_file_close_locked(int handle);
|
||||
void uce_host_file_release_process_locks(const char* reason, size_t reason_len);
|
||||
size_t uce_host_file_read_locked_fd(int handle, char* buf, size_t cap);
|
||||
int uce_host_file_write_locked_fd(int handle, const char* content, size_t content_len);
|
||||
size_t uce_host_sha256(const char* data, size_t data_len, char* out, size_t cap);
|
||||
size_t uce_host_sha256_hex(const char* data, size_t data_len, char* out, size_t cap);
|
||||
size_t uce_host_hmac_sha256(const char* key, size_t key_len, const char* data, size_t data_len, char* out, size_t cap);
|
||||
size_t uce_host_hmac_sha256_hex(const char* key, size_t key_len, const char* data, size_t data_len, char* out, size_t cap);
|
||||
size_t uce_host_base64_encode(const char* data, size_t data_len, char* out, size_t cap);
|
||||
size_t uce_host_base64_decode(const char* data, size_t data_len, char* out, size_t cap);
|
||||
int uce_host_crypto_equal(const char* a, size_t a_len, const char* b, size_t b_len);
|
||||
size_t uce_host_http_request(const char* in, size_t in_len, char* out, size_t cap);
|
||||
uint64_t uce_host_http_request_async(const char* in, size_t in_len);
|
||||
size_t uce_host_shell_exec_dv(const char* in, size_t in_len, char* out, size_t cap);
|
||||
uint64_t uce_host_shell_spawn(const char* in, size_t in_len);
|
||||
size_t uce_host_job_status(uint64_t job_id, char* out, size_t cap);
|
||||
size_t uce_host_job_result(uint64_t job_id, char* out, size_t cap);
|
||||
size_t uce_host_job_await(uint64_t job_id, uint64_t timeout_ms, char* out, size_t cap);
|
||||
int uce_host_job_cancel(uint64_t job_id);
|
||||
size_t uce_host_path_real(const char* path, size_t path_len, char* buf, size_t cap);
|
||||
int uce_host_path_is_within(const char* path, size_t path_len, const char* root, size_t root_len);
|
||||
size_t uce_host_cwd_get(char* buf, size_t cap);
|
||||
@@ -103,30 +132,6 @@ bool file_exists(String path)
|
||||
String current = wasm_current_unit_file();
|
||||
return(uce_host_file_exists(path.data(), path.size(), current.data(), current.size()) != 0);
|
||||
}
|
||||
int file_open_locked(String file_name, int open_flags, int lock_type, int create_mode, f64 wait_timeout_seconds, String purpose)
|
||||
{
|
||||
String current = wasm_current_unit_file();
|
||||
return(uce_host_file_open_locked(
|
||||
file_name.data(), file_name.size(), open_flags, lock_type, create_mode, wait_timeout_seconds,
|
||||
purpose.data(), purpose.size(), current.data(), current.size()
|
||||
));
|
||||
}
|
||||
void file_close_locked(int fd) { uce_host_file_close_locked(fd); }
|
||||
void file_release_process_locks(String reason) { uce_host_file_release_process_locks(reason.data(), reason.size()); }
|
||||
String file_get_contents_locked_fd(int fd)
|
||||
{
|
||||
size_t required = uce_host_file_read_locked_fd(fd, 0, 0);
|
||||
if(required == 0)
|
||||
return("");
|
||||
String content(required, 0);
|
||||
size_t got = uce_host_file_read_locked_fd(fd, &content[0], required);
|
||||
content.resize(got <= required ? got : 0);
|
||||
return(content);
|
||||
}
|
||||
bool file_put_contents_locked_fd(int fd, String content)
|
||||
{
|
||||
return(uce_host_file_write_locked_fd(fd, content.data(), content.size()) != 0);
|
||||
}
|
||||
String file_get_contents(String file_name)
|
||||
{
|
||||
String current = wasm_current_unit_file();
|
||||
@@ -143,11 +148,200 @@ bool file_put_contents(String file_name, String content)
|
||||
String current = wasm_current_unit_file();
|
||||
return(uce_host_file_write(file_name.data(), file_name.size(), current.data(), current.size(), content.data(), content.size(), 0) != 0);
|
||||
}
|
||||
bool file_append_contents(String file_name, String content)
|
||||
bool file_append(String file_name, String content)
|
||||
{
|
||||
String current = wasm_current_unit_file();
|
||||
return(uce_host_file_write(file_name.data(), file_name.size(), current.data(), current.size(), content.data(), content.size(), 1) != 0);
|
||||
}
|
||||
u64 file_open(String path, String mode)
|
||||
{
|
||||
String current = wasm_current_unit_file();
|
||||
return((u64)uce_host_file_open(path.data(), path.size(), current.data(), current.size(), mode.data(), mode.size()));
|
||||
}
|
||||
String file_read(u64 h, u64 len)
|
||||
{
|
||||
size_t required = uce_host_file_handle_read(h, len, 0, 0);
|
||||
if(required == 0)
|
||||
return("");
|
||||
String content(required, 0);
|
||||
size_t got = uce_host_file_handle_read(h, len, &content[0], required);
|
||||
content.resize(got <= required ? got : 0);
|
||||
return(content);
|
||||
}
|
||||
String file_pread(u64 h, u64 offset, u64 len)
|
||||
{
|
||||
size_t required = uce_host_file_handle_pread(h, offset, len, 0, 0);
|
||||
if(required == 0)
|
||||
return("");
|
||||
String content(required, 0);
|
||||
size_t got = uce_host_file_handle_pread(h, offset, len, &content[0], required);
|
||||
content.resize(got <= required ? got : 0);
|
||||
return(content);
|
||||
}
|
||||
u64 file_write(u64 h, String data) { return(uce_host_file_handle_write(h, data.data(), data.size())); }
|
||||
u64 file_pwrite(u64 h, u64 offset, String data) { return(uce_host_file_handle_pwrite(h, offset, data.data(), data.size())); }
|
||||
s64 file_seek(u64 h, s64 offset, int whence) { return((s64)uce_host_file_handle_seek(h, offset, whence)); }
|
||||
s64 file_tell(u64 h) { return((s64)uce_host_file_handle_tell(h)); }
|
||||
void file_close(u64 h) { uce_host_file_handle_close(h); }
|
||||
static DValue wasm_decode_dvalue_result(String encoded)
|
||||
{
|
||||
DValue out;
|
||||
String error;
|
||||
ucb_decode(encoded, out, &error);
|
||||
return(out);
|
||||
}
|
||||
DValue file_stat(String path)
|
||||
{
|
||||
String current = wasm_current_unit_file();
|
||||
size_t required = uce_host_file_stat(path.data(), path.size(), current.data(), current.size(), 0, 0);
|
||||
String encoded(required, 0);
|
||||
size_t got = required ? uce_host_file_stat(path.data(), path.size(), current.data(), current.size(), &encoded[0], required) : 0;
|
||||
encoded.resize(got <= required ? got : 0);
|
||||
return(wasm_decode_dvalue_result(encoded));
|
||||
}
|
||||
DValue dir_list(String path)
|
||||
{
|
||||
String current = wasm_current_unit_file();
|
||||
size_t required = uce_host_dir_list(path.data(), path.size(), current.data(), current.size(), 0, 0);
|
||||
String encoded(required, 0);
|
||||
size_t got = required ? uce_host_dir_list(path.data(), path.size(), current.data(), current.size(), &encoded[0], required) : 0;
|
||||
encoded.resize(got <= required ? got : 0);
|
||||
return(wasm_decode_dvalue_result(encoded));
|
||||
}
|
||||
bool file_rename(String from, String to)
|
||||
{
|
||||
String current = wasm_current_unit_file();
|
||||
return(uce_host_file_rename(from.data(), from.size(), to.data(), to.size(), current.data(), current.size()) != 0);
|
||||
}
|
||||
bool file_copy(String from, String to)
|
||||
{
|
||||
String current = wasm_current_unit_file();
|
||||
return(uce_host_file_copy(from.data(), from.size(), to.data(), to.size(), current.data(), current.size()) != 0);
|
||||
}
|
||||
bool file_truncate(String path, u64 size)
|
||||
{
|
||||
String current = wasm_current_unit_file();
|
||||
return(uce_host_file_truncate(path.data(), path.size(), current.data(), current.size(), size) != 0);
|
||||
}
|
||||
bool dir_remove(String path, bool recursive)
|
||||
{
|
||||
String current = wasm_current_unit_file();
|
||||
return(uce_host_dir_remove(path.data(), path.size(), current.data(), current.size(), recursive ? 1 : 0) != 0);
|
||||
}
|
||||
String file_temp(String prefix)
|
||||
{
|
||||
String current = wasm_current_unit_file();
|
||||
size_t required = uce_host_file_temp(prefix.data(), prefix.size(), current.data(), current.size(), 0, 0);
|
||||
if(required == 0) return("");
|
||||
String out(required, 0);
|
||||
size_t got = uce_host_file_temp(prefix.data(), prefix.size(), current.data(), current.size(), &out[0], required);
|
||||
out.resize(got <= required ? got : 0);
|
||||
return(out);
|
||||
}
|
||||
bool file_chmod(String path, u32 mode)
|
||||
{
|
||||
String current = wasm_current_unit_file();
|
||||
return(uce_host_file_chmod(path.data(), path.size(), current.data(), current.size(), mode) != 0);
|
||||
}
|
||||
bool file_symlink(String target, String linkpath)
|
||||
{
|
||||
String current = wasm_current_unit_file();
|
||||
return(uce_host_file_symlink(target.data(), target.size(), linkpath.data(), linkpath.size(), current.data(), current.size()) != 0);
|
||||
}
|
||||
String file_readlink(String path)
|
||||
{
|
||||
String current = wasm_current_unit_file();
|
||||
size_t required = uce_host_file_readlink(path.data(), path.size(), current.data(), current.size(), 0, 0);
|
||||
if(required == 0) return("");
|
||||
String out(required, 0);
|
||||
size_t got = uce_host_file_readlink(path.data(), path.size(), current.data(), current.size(), &out[0], required);
|
||||
out.resize(got <= required ? got : 0);
|
||||
return(out);
|
||||
}
|
||||
bool file_fsync(u64 h) { return(uce_host_file_fsync(h) != 0); }
|
||||
|
||||
|
||||
static String wasm_string_hostcall_1(size_t (*fn)(const char*, size_t, char*, size_t), String data)
|
||||
{
|
||||
size_t required = fn(data.data(), data.size(), 0, 0);
|
||||
String out(required, 0);
|
||||
size_t got = required ? fn(data.data(), data.size(), &out[0], required) : 0;
|
||||
out.resize(got <= required ? got : 0);
|
||||
return(out);
|
||||
}
|
||||
String sha256(String data) { return(wasm_string_hostcall_1(uce_host_sha256, data)); }
|
||||
String sha256_hex(String data) { return(wasm_string_hostcall_1(uce_host_sha256_hex, data)); }
|
||||
String hmac_sha256(String key, String data)
|
||||
{
|
||||
size_t required = uce_host_hmac_sha256(key.data(), key.size(), data.data(), data.size(), 0, 0);
|
||||
String out(required, 0); size_t got = required ? uce_host_hmac_sha256(key.data(), key.size(), data.data(), data.size(), &out[0], required) : 0; out.resize(got <= required ? got : 0); return(out);
|
||||
}
|
||||
String hmac_sha256_hex(String key, String data)
|
||||
{
|
||||
size_t required = uce_host_hmac_sha256_hex(key.data(), key.size(), data.data(), data.size(), 0, 0);
|
||||
String out(required, 0); size_t got = required ? uce_host_hmac_sha256_hex(key.data(), key.size(), data.data(), data.size(), &out[0], required) : 0; out.resize(got <= required ? got : 0); return(out);
|
||||
}
|
||||
String base64_decode(String raw) { return(wasm_string_hostcall_1(uce_host_base64_decode, raw)); }
|
||||
String random_bytes(u64 n)
|
||||
{
|
||||
if(n > 1024 * 1024) n = 1024 * 1024;
|
||||
String out(n, 0); size_t got = n ? uce_host_random(&out[0], n) : 0; out.resize(got <= n ? got : 0); return(out);
|
||||
}
|
||||
bool crypto_equal(String a, String b) { return(uce_host_crypto_equal(a.data(), a.size(), b.data(), b.size()) != 0); }
|
||||
|
||||
|
||||
DValue http_request(DValue req)
|
||||
{
|
||||
String encoded = ucb_encode(req);
|
||||
size_t required = uce_host_http_request(encoded.data(), encoded.size(), 0, 0);
|
||||
String out(required, 0); size_t got = required ? uce_host_http_request(encoded.data(), encoded.size(), &out[0], required) : 0; out.resize(got <= required ? got : 0); return(wasm_decode_dvalue_result(out));
|
||||
}
|
||||
u64 http_request_async(DValue req)
|
||||
{
|
||||
String encoded = ucb_encode(req);
|
||||
return((u64)uce_host_http_request_async(encoded.data(), encoded.size()));
|
||||
}
|
||||
|
||||
DValue shell_exec(DValue spec)
|
||||
{
|
||||
String encoded = ucb_encode(spec);
|
||||
size_t required = uce_host_shell_exec_dv(encoded.data(), encoded.size(), 0, 0);
|
||||
String out(required, 0);
|
||||
size_t got = required ? uce_host_shell_exec_dv(encoded.data(), encoded.size(), &out[0], required) : 0;
|
||||
out.resize(got <= required ? got : 0);
|
||||
return(wasm_decode_dvalue_result(out));
|
||||
}
|
||||
u64 shell_spawn(DValue spec)
|
||||
{
|
||||
String encoded = ucb_encode(spec);
|
||||
return((u64)uce_host_shell_spawn(encoded.data(), encoded.size()));
|
||||
}
|
||||
DValue job_status(u64 job_id)
|
||||
{
|
||||
size_t required = uce_host_job_status(job_id, 0, 0);
|
||||
String out(required, 0);
|
||||
size_t got = required ? uce_host_job_status(job_id, &out[0], required) : 0;
|
||||
out.resize(got <= required ? got : 0);
|
||||
return(wasm_decode_dvalue_result(out));
|
||||
}
|
||||
DValue job_result(u64 job_id)
|
||||
{
|
||||
size_t required = uce_host_job_result(job_id, 0, 0);
|
||||
String out(required, 0);
|
||||
size_t got = required ? uce_host_job_result(job_id, &out[0], required) : 0;
|
||||
out.resize(got <= required ? got : 0);
|
||||
return(wasm_decode_dvalue_result(out));
|
||||
}
|
||||
DValue job_await(u64 job_id, u64 timeout_ms)
|
||||
{
|
||||
size_t required = uce_host_job_await(job_id, timeout_ms, 0, 0);
|
||||
String out(required, 0);
|
||||
size_t got = required ? uce_host_job_await(job_id, timeout_ms, &out[0], required) : 0;
|
||||
out.resize(got <= required ? got : 0);
|
||||
return(wasm_decode_dvalue_result(out));
|
||||
}
|
||||
bool job_cancel(u64 job_id) { return(uce_host_job_cancel(job_id) != 0); }
|
||||
|
||||
String cwd_get()
|
||||
{
|
||||
size_t required = uce_host_cwd_get(0, 0);
|
||||
@@ -522,6 +716,15 @@ StringMap default_config()
|
||||
#include <errno.h>
|
||||
#include "sys.h"
|
||||
#include "hash.h"
|
||||
#include "uri.h"
|
||||
|
||||
String sha256(String data) { return(sha256_native(data)); }
|
||||
String sha256_hex(String data) { return(sha256_hex_native(data)); }
|
||||
String hmac_sha256(String key, String data) { return(hmac_sha256_native(key, data)); }
|
||||
String hmac_sha256_hex(String key, String data) { return(hmac_sha256_hex_native(key, data)); }
|
||||
String base64_decode(String raw) { bool ok=false; return(::base64_decode(raw, ok)); }
|
||||
String random_bytes(u64 n) { if(n > 1024*1024) n = 1024*1024; String out(n, 0); int fd=open("/dev/urandom", O_RDONLY); if(fd<0) return(""); size_t off=0; while(off<n) { ssize_t got=read(fd, &out[off], n-off); if(got<0 && errno==EINTR) continue; if(got<=0) break; off += (size_t)got; } close(fd); out.resize(off); return(out); }
|
||||
bool crypto_equal(String a, String b) { return(crypto_equal_native(a, b)); }
|
||||
|
||||
// Single definitions for the native split build (declared extern in sys.h).
|
||||
pid_t parent_pid = 0;
|
||||
@@ -529,7 +732,59 @@ pid_t my_pid = 0;
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr f64 FILE_LOCK_WAIT_TIMEOUT_SECONDS = 3.0;
|
||||
u64 file_lock_timeout_ms()
|
||||
{
|
||||
const char* raw = getenv("UCE_FILE_LOCK_TIMEOUT_MS");
|
||||
if(!raw || !*raw)
|
||||
return(2000);
|
||||
char* end = 0;
|
||||
unsigned long long parsed = strtoull(raw, &end, 10);
|
||||
if(end == raw)
|
||||
return(2000);
|
||||
return((u64)parsed);
|
||||
}
|
||||
|
||||
u64 monotonic_ms()
|
||||
{
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
return((u64)ts.tv_sec * 1000ull + (u64)ts.tv_nsec / 1000000ull);
|
||||
}
|
||||
|
||||
int open_locked_file(String file_name, int open_flags, int lock_type, int create_mode = 0644)
|
||||
{
|
||||
int fd = open(file_name.c_str(), open_flags, create_mode);
|
||||
if(fd == -1)
|
||||
return(-1);
|
||||
fcntl(fd, F_SETFD, FD_CLOEXEC);
|
||||
u64 timeout = file_lock_timeout_ms();
|
||||
u64 deadline = monotonic_ms() + timeout;
|
||||
while(true)
|
||||
{
|
||||
if(flock(fd, lock_type | LOCK_NB) == 0)
|
||||
return(fd);
|
||||
if(errno != EWOULDBLOCK && errno != EAGAIN && errno != EINTR)
|
||||
{
|
||||
close(fd);
|
||||
return(-1);
|
||||
}
|
||||
if(timeout == 0 || monotonic_ms() >= deadline)
|
||||
{
|
||||
fprintf(stderr, "(!) file lock timeout after %llums: %s\n", (unsigned long long)timeout, file_name.c_str());
|
||||
close(fd);
|
||||
return(-1);
|
||||
}
|
||||
usleep(10000);
|
||||
}
|
||||
}
|
||||
|
||||
void close_locked_file(int fd)
|
||||
{
|
||||
if(fd == -1)
|
||||
return;
|
||||
flock(fd, LOCK_UN);
|
||||
close(fd);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -737,36 +992,9 @@ bool file_exists(String path)
|
||||
return(std::filesystem::exists(fp));
|
||||
}
|
||||
|
||||
int file_open_locked(String file_name, int open_flags, int lock_type, int create_mode, f64 wait_timeout_seconds, String purpose)
|
||||
{
|
||||
(void)wait_timeout_seconds;
|
||||
(void)purpose;
|
||||
int fd = open(file_name.c_str(), open_flags, create_mode);
|
||||
if(fd == -1)
|
||||
return(-1);
|
||||
fcntl(fd, F_SETFD, FD_CLOEXEC);
|
||||
if(flock(fd, lock_type) != 0)
|
||||
{
|
||||
close(fd);
|
||||
return(-1);
|
||||
}
|
||||
return(fd);
|
||||
}
|
||||
namespace {
|
||||
|
||||
void file_close_locked(int fd)
|
||||
{
|
||||
if(fd == -1)
|
||||
return;
|
||||
flock(fd, LOCK_UN);
|
||||
close(fd);
|
||||
}
|
||||
|
||||
void file_release_process_locks(String reason)
|
||||
{
|
||||
(void)reason;
|
||||
}
|
||||
|
||||
String file_get_contents_locked_fd(int fd)
|
||||
String file_read_all(int fd)
|
||||
{
|
||||
if(fd == -1)
|
||||
return("");
|
||||
@@ -780,7 +1008,6 @@ String file_get_contents_locked_fd(int fd)
|
||||
return(content);
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
bool file_write_all(int fd, const char* data, size_t remaining)
|
||||
{
|
||||
@@ -803,41 +1030,30 @@ bool file_write_all(int fd, const char* data, size_t remaining)
|
||||
|
||||
}
|
||||
|
||||
bool file_put_contents_locked_fd(int fd, String content)
|
||||
{
|
||||
if(fd == -1)
|
||||
return(false);
|
||||
lseek(fd, 0, SEEK_SET);
|
||||
if(ftruncate(fd, 0) != 0)
|
||||
return(false);
|
||||
if(!file_write_all(fd, content.data(), content.length()))
|
||||
return(false);
|
||||
return(true);
|
||||
}
|
||||
|
||||
String file_get_contents(String file_name)
|
||||
{
|
||||
s32 fd = file_open_locked(file_name, O_RDONLY, LOCK_SH, 0644, FILE_LOCK_WAIT_TIMEOUT_SECONDS, "file_get_contents:" + file_name);
|
||||
s32 fd = open_locked_file(file_name, O_RDONLY, LOCK_SH);
|
||||
if(fd == -1)
|
||||
{
|
||||
printf("(!) Could not read %s\n", file_name.c_str());
|
||||
return("");
|
||||
}
|
||||
String content = file_get_contents_locked_fd(fd);
|
||||
file_close_locked(fd);
|
||||
String content = file_read_all(fd);
|
||||
close_locked_file(fd);
|
||||
return(content);
|
||||
}
|
||||
|
||||
bool file_put_contents(String file_name, String content)
|
||||
{
|
||||
s32 fd = file_open_locked(file_name, O_RDWR | O_CREAT, LOCK_EX, 0644, FILE_LOCK_WAIT_TIMEOUT_SECONDS, "file_put_contents:" + file_name);
|
||||
s32 fd = open_locked_file(file_name, O_RDWR | O_CREAT, LOCK_EX, 0644);
|
||||
if(fd == -1)
|
||||
{
|
||||
printf("(!) Could not write %s\n", file_name.c_str());
|
||||
return(false);
|
||||
}
|
||||
bool ok = file_put_contents_locked_fd(fd, content);
|
||||
file_close_locked(fd);
|
||||
lseek(fd, 0, SEEK_SET);
|
||||
bool ok = ftruncate(fd, 0) == 0 && file_write_all(fd, content.data(), content.length());
|
||||
close_locked_file(fd);
|
||||
if(!ok)
|
||||
{
|
||||
printf("(!) Could not fully write %s\n", file_name.c_str());
|
||||
@@ -846,9 +1062,9 @@ bool file_put_contents(String file_name, String content)
|
||||
return(true);
|
||||
}
|
||||
|
||||
bool file_append_contents(String file_name, String content)
|
||||
bool file_append(String file_name, String content)
|
||||
{
|
||||
s32 fd = file_open_locked(file_name, O_RDWR | O_CREAT, LOCK_EX, 0644, FILE_LOCK_WAIT_TIMEOUT_SECONDS, "file_append:" + file_name);
|
||||
s32 fd = open_locked_file(file_name, O_RDWR | O_CREAT, LOCK_EX, 0644);
|
||||
if(fd == -1)
|
||||
{
|
||||
printf("(!) Could not append %s\n", file_name.c_str());
|
||||
@@ -856,7 +1072,7 @@ bool file_append_contents(String file_name, String content)
|
||||
}
|
||||
lseek(fd, 0, SEEK_END);
|
||||
bool ok = file_write_all(fd, content.data(), content.length());
|
||||
file_close_locked(fd);
|
||||
close_locked_file(fd);
|
||||
if(!ok)
|
||||
{
|
||||
printf("(!) Could not fully append %s\n", file_name.c_str());
|
||||
@@ -1154,7 +1370,6 @@ pid_t spawn_subprocess(std::function<void()> exec_after_spawn)
|
||||
p = fork();
|
||||
if(p == 0)
|
||||
{
|
||||
file_release_process_locks("fork child startup");
|
||||
my_pid = getpid();
|
||||
//printf("(C) child procress started, PID:%i\n", my_pid);
|
||||
prctl(PR_SET_PDEATHSIG, SIGHUP);
|
||||
@@ -1264,7 +1479,7 @@ pid_t task_pid(String key)
|
||||
{
|
||||
String status_file_name = task_file_prefix(key);
|
||||
String lock_file_name = status_file_name + ".lock";
|
||||
int lock_fd = file_open_locked(lock_file_name, O_RDWR | O_CREAT, LOCK_EX, 0644, FILE_LOCK_WAIT_TIMEOUT_SECONDS, "task-pid:" + key);
|
||||
int lock_fd = open_locked_file(lock_file_name, O_RDWR | O_CREAT, LOCK_EX, 0644);
|
||||
if(lock_fd == -1)
|
||||
{
|
||||
fprintf(stderr, "task_pid(): could not lock task key '%s'\n", key.c_str());
|
||||
@@ -1276,12 +1491,12 @@ pid_t task_pid(String key)
|
||||
TaskStatus status = task_status_parse(status_file);
|
||||
if(task_status_is_alive(status))
|
||||
{
|
||||
file_close_locked(lock_fd);
|
||||
close_locked_file(lock_fd);
|
||||
return(status.pid);
|
||||
}
|
||||
file_unlink(status_file_name);
|
||||
}
|
||||
file_close_locked(lock_fd);
|
||||
close_locked_file(lock_fd);
|
||||
return(0);
|
||||
}
|
||||
|
||||
@@ -1289,7 +1504,7 @@ pid_t task(String key, std::function<void()> exec_after_spawn, u64 timeout)
|
||||
{
|
||||
String status_file_name = task_file_prefix(key);
|
||||
String lock_file_name = status_file_name + ".lock";
|
||||
int lock_fd = file_open_locked(lock_file_name, O_RDWR | O_CREAT, LOCK_EX, 0644, FILE_LOCK_WAIT_TIMEOUT_SECONDS, "task:" + key);
|
||||
int lock_fd = open_locked_file(lock_file_name, O_RDWR | O_CREAT, LOCK_EX, 0644);
|
||||
if(lock_fd == -1)
|
||||
{
|
||||
fprintf(stderr, "task(): could not lock task key '%s'\n", key.c_str());
|
||||
@@ -1303,7 +1518,7 @@ pid_t task(String key, std::function<void()> exec_after_spawn, u64 timeout)
|
||||
if(task_status_is_alive(status))
|
||||
{
|
||||
printf("(P) worker process '%s' already running: PID %i\n", key.c_str(), status.pid);
|
||||
file_close_locked(lock_fd);
|
||||
close_locked_file(lock_fd);
|
||||
return(status.pid);
|
||||
}
|
||||
file_unlink(status_file_name);
|
||||
@@ -1312,13 +1527,12 @@ pid_t task(String key, std::function<void()> exec_after_spawn, u64 timeout)
|
||||
if(p < 0)
|
||||
{
|
||||
fprintf(stderr, "task(): fork failed for key '%s': %s\n", key.c_str(), strerror(errno));
|
||||
file_close_locked(lock_fd);
|
||||
close_locked_file(lock_fd);
|
||||
return(0);
|
||||
}
|
||||
if(p == 0)
|
||||
{
|
||||
file_release_process_locks("task child startup");
|
||||
file_close_locked(lock_fd);
|
||||
close_locked_file(lock_fd);
|
||||
my_pid = getpid();
|
||||
signal(SIGALRM, SIG_DFL);
|
||||
if(timeout > 0)
|
||||
@@ -1331,11 +1545,11 @@ pid_t task(String key, std::function<void()> exec_after_spawn, u64 timeout)
|
||||
}
|
||||
task_close_inherited_fds();
|
||||
exec_after_spawn();
|
||||
int exit_lock_fd = file_open_locked(lock_file_name, O_RDWR | O_CREAT, LOCK_EX, 0644, FILE_LOCK_WAIT_TIMEOUT_SECONDS, "task-exit:" + key);
|
||||
int exit_lock_fd = open_locked_file(lock_file_name, O_RDWR | O_CREAT, LOCK_EX, 0644);
|
||||
if(exit_lock_fd != -1)
|
||||
{
|
||||
file_unlink(status_file_name);
|
||||
file_close_locked(exit_lock_fd);
|
||||
close_locked_file(exit_lock_fd);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -1349,10 +1563,10 @@ pid_t task(String key, std::function<void()> exec_after_spawn, u64 timeout)
|
||||
{
|
||||
fprintf(stderr, "task(): could not write status file for key '%s'; terminating child PID %i\n", key.c_str(), p);
|
||||
kill(p, SIGTERM);
|
||||
file_close_locked(lock_fd);
|
||||
close_locked_file(lock_fd);
|
||||
return(0);
|
||||
}
|
||||
file_close_locked(lock_fd);
|
||||
close_locked_file(lock_fd);
|
||||
printf("(P) worker process '%s' spawned: PID %i\n", key.c_str(), p);
|
||||
return(p);
|
||||
}
|
||||
@@ -1425,6 +1639,8 @@ StringMap make_server_settings()
|
||||
// command batches here at workspace teardown.
|
||||
cfg["WS_BROKER_SOCKET_PATH"] = "/run/uce/ws-broker.sock";
|
||||
cfg["WS_BROKER_OUTBOUND_TIMEOUT_SECONDS"] = "30";
|
||||
// Comma-separated uce_host_* names a sysadmin disables; empty = nothing blocked.
|
||||
cfg["UCE_HOSTCALL_BLOCKLIST"] = "";
|
||||
cfg["TMP_UPLOAD_PATH"] = "/tmp/uce/uploads";
|
||||
cfg["SESSION_PATH"] = "/tmp/uce/sessions";
|
||||
cfg["COMPILER_SYS_PATH"] = ".";
|
||||
|
||||
+38
-8
@@ -25,7 +25,24 @@ int usleep(unsigned int usec);
|
||||
#include <ctime>
|
||||
#include <sstream>
|
||||
|
||||
struct DValue;
|
||||
|
||||
String shell_exec(String cmd);
|
||||
DValue http_request(DValue req);
|
||||
u64 http_request_async(DValue req);
|
||||
DValue shell_exec(DValue spec);
|
||||
u64 shell_spawn(DValue spec);
|
||||
DValue job_status(u64 job_id);
|
||||
DValue job_result(u64 job_id);
|
||||
DValue job_await(u64 job_id, u64 timeout_ms);
|
||||
bool job_cancel(u64 job_id);
|
||||
String sha256(String data);
|
||||
String sha256_hex(String data);
|
||||
String hmac_sha256(String key, String data);
|
||||
String hmac_sha256_hex(String key, String data);
|
||||
String base64_decode(String raw);
|
||||
String random_bytes(u64 n);
|
||||
bool crypto_equal(String a, String b);
|
||||
String shell_escape(String raw);
|
||||
String basename(String fn);
|
||||
String dirname(String fn);
|
||||
@@ -34,20 +51,34 @@ String path_real(String path);
|
||||
bool path_is_within(String path, String root);
|
||||
bool mkdir(String path);
|
||||
bool file_exists(String path);
|
||||
int file_open_locked(String file_name, int open_flags, int lock_type = LOCK_SH, int create_mode = 0644, f64 wait_timeout_seconds = 3.0, String purpose = "");
|
||||
void file_close_locked(int fd);
|
||||
void file_release_process_locks(String reason = "");
|
||||
String file_get_contents_locked_fd(int fd);
|
||||
bool file_put_contents_locked_fd(int fd, String content);
|
||||
String file_get_contents(String file_name);
|
||||
bool file_put_contents(String file_name, String content);
|
||||
bool file_append_contents(String file_name, String content);
|
||||
bool file_append(String file_name, String content);
|
||||
u64 file_open(String path, String mode);
|
||||
String file_read(u64 h, u64 len);
|
||||
String file_pread(u64 h, u64 offset, u64 len);
|
||||
u64 file_write(u64 h, String data);
|
||||
u64 file_pwrite(u64 h, u64 offset, String data);
|
||||
s64 file_seek(u64 h, s64 offset, int whence);
|
||||
s64 file_tell(u64 h);
|
||||
void file_close(u64 h);
|
||||
DValue file_stat(String path);
|
||||
DValue dir_list(String path);
|
||||
bool file_rename(String from, String to);
|
||||
bool file_copy(String from, String to);
|
||||
bool file_truncate(String path, u64 size);
|
||||
bool dir_remove(String path, bool recursive = false);
|
||||
String file_temp(String prefix);
|
||||
bool file_chmod(String path, u32 mode);
|
||||
bool file_symlink(String target, String linkpath);
|
||||
String file_readlink(String path);
|
||||
bool file_fsync(u64 h);
|
||||
template <typename... Ts>
|
||||
inline bool file_append(String file_name, Ts... args)
|
||||
{
|
||||
std::ostringstream out;
|
||||
((out << args), ...);
|
||||
return(file_append_contents(file_name, out.str()));
|
||||
return(file_append(file_name, out.str()));
|
||||
}
|
||||
String cwd_get();
|
||||
void cwd_set(String path);
|
||||
@@ -61,7 +92,6 @@ u64 time();
|
||||
String time_format_local(String format = "", u64 timestamp = 0);
|
||||
|
||||
// Runtime timing/profiling snapshot for the active wasm request/workspace.
|
||||
struct DValue;
|
||||
DValue request_perf();
|
||||
|
||||
String time_format_utc(String format = "", u64 timestamp = 0);
|
||||
|
||||
Reference in New Issue
Block a user