Skip to content

Commit

Permalink
bump sdk, wasi dlfcn poc
Browse files Browse the repository at this point in the history
  • Loading branch information
pmp-p committed Sep 23, 2024
1 parent 3c2f09d commit 893023c
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 81 deletions.
2 changes: 1 addition & 1 deletion .buildconfig
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
PG_VERSION=16.4
SDK_VERSION=3.1.68.0bi
SDK_VERSION=3.1.68.1bi
15 changes: 10 additions & 5 deletions cibuild/linkwasi.sh
Original file line number Diff line number Diff line change
Expand Up @@ -770,19 +770,24 @@ llvm-ar cr ../../libpglite.a $PGOBJ

# just linking



$CC -o postgres \
-O2 -g0 main/main.o \
../../libpglite.a \
-fno-strict-aliasing \
-fwrapv \
\
-O2 ../../src/common/libpgcommon_srv.a ../../src/port/libpgport_srv.a \
\
../../libpglite.a main/main.o ../../wasi_dlfcn.o \
\
../../src/common/libpgcommon_srv.a \
../../src/port/libpgport_srv.a \
../../wasi_dlfcn.o \
../../src/backend/snowball/libdict_snowball.a \
../../src/pl/plpgsql/src/libplpgsql.a \
-lz -lm -lwasi-emulated-mman -lwasi-emulated-signal -lc \
-Wl,--export=pg_initdb \
-Wl,--export=interactive_one \
-Wl,--export=use_socketfile \
-Wl,--export=interactive_write \
-Wl,--export=interactive_read
-Wl,--global-base=33554432

cp -vf postgres postgres.wasi || exit 192
Expand Down
52 changes: 13 additions & 39 deletions patches/imports/plpgsql
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
___stack_pointer
___memory_base
___table_base
_SearchSysCache1
_errstart_cold
_errmsg_internal
Expand Down Expand Up @@ -105,35 +108,19 @@ _SPI_copytuple
_expanded_record_lookup_field
_FreeExecutorState
_ResourceOwnerReleaseAllPlanCacheRefs
_invoke_vi
___wasm_setjmp_test
_setTempRet0
_getTempRet0
_invoke_viiiiii
_invoke_viii
_invoke_viiii
_invoke_iiiiii
_invoke_iii
_invoke_ii
___wasm_setjmp
_BeginInternalSubTransaction
_get_typlenbyval
_datumTransfer
_invoke_iiii
_invoke_v
_invoke_i
_ReThrowError
_unpack_sql_state
_cstring_to_text
_invoke_viiiii
_MemoryContextGetParent
_emscripten_longjmp
_ReleaseCurrentSubTransaction
_CreateExecutorState
_ResourceOwnerCreate
_CreateExprContext
_GetCurrentSubTransactionId
_SPI_plan_get_plan_sources
_SPI_execute_plan_extended
_SPI_result_code_string
_cstring_to_text
_unpack_sql_state
_GetErrorContextStack
_Int64GetDatum
_SPI_cursor_close
Expand All @@ -143,6 +130,7 @@ _get_element_type
_pg_detoast_datum_copy
_array_create_iterator
_array_iterate
_MemoryContextGetParent
_MakeExpandedObjectReadOnlyInternal
_tuplestore_putvalues
_tuplestore_puttuple
Expand All @@ -163,6 +151,7 @@ _SPI_commit_and_chain
_SPI_commit
_SPI_rollback_and_chain
_SPI_rollback
_ReThrowError
_err_generic_string
_SPI_prepare_extended
_SPI_keepplan
Expand Down Expand Up @@ -208,9 +197,9 @@ _SPI_freeplan
_MemoryContextDelete
_pg_printf
_fflush
_list_free
_get_collation_oid
_memmove
_list_free
_typeStringToTypeName
_raw_parser
_quote_identifier
Expand All @@ -230,12 +219,11 @@ _RegisterSubXactCallback
_find_rendezvous_variable
_pg_strcasecmp
_SplitIdentifierString
___errno_location
_pre_format_elog_string
_format_elog_string
_guc_malloc
_invoke_vii
_invoke_iiiiiii
_SPI_connect_ext
_ResourceOwnerDelete
_SPI_finish
_CheckFunctionValidatorAccess
_get_typtype
Expand All @@ -248,25 +236,14 @@ _internalerrquery
_strchr
_scanner_init
_scanner_finish
___stack_pointer
___memory_base
___table_base
_error_context_stack
_TopMemoryContext
_CurrentMemoryContext
_check_function_bodies
_InterruptPending
_CurrentResourceOwner
_plpgsql_parser_setup
___THREW__
___threwValue
_BeginInternalSubTransaction
_PG_exception_stack
_ReleaseCurrentSubTransaction
_CopyErrorData
_FlushErrorState
_RollbackAndReleaseCurrentSubTransaction
_MemoryContextDeleteChildren
_MyProc
_TopTransactionContext
_TopTransactionResourceOwner
Expand All @@ -276,8 +253,5 @@ _SPI_result
_namein
_work_mem
_stdout
_errno
_GUC_check_errdetail_string
_SPI_connect_ext
_plpgsql_compile
_ResourceOwnerDelete
_pg_re_throw
4 changes: 2 additions & 2 deletions patches/interactive_one.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ interactive_one() {
}


} // is_node
} // is_node && is_repl


doing_extended_query_message = false;
Expand Down Expand Up @@ -362,7 +362,7 @@ printf("# 352 : node+repl is wire : %c\n", firstchar);

} // ok lck

} // is_node + is_repl
} // is_node && is_repl

if (cma_rsize) {
PDEBUG("wire message in cma buffer !");
Expand Down
19 changes: 19 additions & 0 deletions patches/postgresql-wasm-16.4/src-backend-utils-fmgr-dfmgr.c.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--- postgresql/src/backend/utils/fmgr/dfmgr.c
+++ postgresql-wasm/src/backend/utils/fmgr/dfmgr.c
@@ -121,12 +121,15 @@

/* Look up the function within the library. */
retval = dlsym(lib_handle, funcname);
-
+#if !defined(__wasi__)
if (retval == NULL && signalNotFound)
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_FUNCTION),
errmsg("could not find function \"%s\" in file \"%s\"",
funcname, fullname)));
+#else
+ fprintf(stderr, "could not find function \"%s\" in file \"%s\" rv=%p snf=%b\n", funcname, fullname, retval, signalNotFound);
+#endif

pfree(fullname);
return retval;
30 changes: 2 additions & 28 deletions patches/postgresql-wasm/src-include-port-wasi.h.diff
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
--- postgresql/src/include/port/wasi.h
+++ postgresql-wasm/src/include/port/wasi.h
@@ -0,0 +1,271 @@
@@ -0,0 +1,245 @@
+#ifndef I_WASI
+#define I_WASI
+
Expand Down Expand Up @@ -181,7 +181,7 @@
+
+static int
+semget(key_t key, int nsems, int semflg) {
+#if PGDEBUG
+#if 0 // PGDEBUG
+ printf("# 213: semget(key_t key = %d, int nsems=%d, int semflg=%d)\n", key, nsems, semflg);
+#endif
+ return 1;
Expand All @@ -194,29 +194,8 @@
+
+
+
+
+
+
+
+
+#include <sys/mman.h>
+/*
+static void *
+mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset) {
+#if PGDEBUG
+ printf("# 227: mmap(void* %p, size_t length=%d, int prot=%d, int flags=%d, int fd=%d, off_t offset=%d)\n", addr, length,prot,flags,fd,offset);
+#endif
+ return NULL;
+
+}
+
+static int
+munmap(void *addr, size_t length) {
+ puts("# 228: munmap");
+ return 0;
+}
+
+*/
+static int
+shm_open(const char *name, int oflag, mode_t mode) {
+ char tmpnam[128];
Expand Down Expand Up @@ -258,11 +237,6 @@
+ puts("# 259: tzset(void) STUB");
+}
+
+
+// HACK TODO FIXME !
+
+// #define getpwuid(...) (NULL)
+
+#if defined(PG_INITDB) || defined(FE_UTILS_PRINT) || defined(PG_DUMP_PARALLEL)
+static void
+__SIG_IGN(int param) {
Expand Down
12 changes: 7 additions & 5 deletions patches/wasi_dlfcn.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,10 @@ dlopen(const char *filename, int flags) {
void *
dlsym(void *__restrict handle, const char *__restrict symbol) {
void *sym = NULL;
if ( !strcmp(symbol, "Pg_magic_func") )
//sym = &STUB_Pg_magic_func;
return &STUB_Pg_magic_func;
if ( !strcmp(symbol, "Pg_magic_func") ) {
sym = &STUB_Pg_magic_func;
goto report;
}

if ( !strcmp(symbol, "_PG_init") )
// sym = &STUB__PG_init;
Expand Down Expand Up @@ -185,10 +186,11 @@ dlsym(void *__restrict handle, const char *__restrict symbol) {
return &plpgsql_inline_handler;

if ( !strcmp(symbol, "plpgsql_validator") ) {
return &plpgsql_validator;
sym = &plpgsql_validator;
goto report;
}


report:;
fprintf(stderr, "void *dlsym(void *handle = %p, const char *symbol = %s) => %p\n", handle, symbol, sym);
return sym;
}
Expand Down
2 changes: 1 addition & 1 deletion patches/wasi_port.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ sigemptyset(sigset_t *set) {

int
sigaction(int signum, const struct sigaction *act, struct sigaction *oldact) {
puts("# 96: sigaction 0");
puts("# 96: sigaction STUB");
return 0;
}

Expand Down

0 comments on commit 893023c

Please sign in to comment.