lightning.git - Portable just-in-time compiler library

index : lightning.git
Portable just-in-time compiler library
summary refs log tree commit diff
diff options
context:
space:
mode:
authorpcpa <paulo.cesar.pereira.de.andrade@gmail.com>2022年10月06日 11:26:27 -0300
committerpcpa <paulo.cesar.pereira.de.andrade@gmail.com>2022年10月06日 11:26:27 -0300
commitf401a03fbd9c2e7547776369ff732c48cbe29ba7 (patch)
tree5507f517a3ff61337351302165cf9f89002b0935
parent69cbcc48a6bb238a425134142b7df8d12047882d (diff)
downloadlightning-f401a03fbd9c2e7547776369ff732c48cbe29ba7.tar.gz
Minor fix to pass all tests in NetBSD
For NetBSD need to use the PROT_MPROTECT() macro to tell intended map protections. Then, not mix PROT_EXEC and PROT_WRITE, so need to switch flags in mprotect() calls.
Diffstat
-rw-r--r--check/setcode.c 22
-rw-r--r--lib/lightning.c 32
2 files changed, 37 insertions, 17 deletions
diff --git a/check/setcode.c b/check/setcode.c
index 209c5fe..62719ee 100644
--- a/check/setcode.c
+++ b/check/setcode.c
@@ -31,17 +31,24 @@ main(int argc, char *argv[])
int mmap_fd;
#endif
void (*function)(void);
+ int mmap_prot, mmap_flags;
#if defined(__sgi)
mmap_fd = open("/dev/zero", O_RDWR);
#endif
- ptr = mmap(NULL, 1024 * 1024,
+ mmap_prot = PROT_READ | PROT_WRITE;
#if !__OpenBSD__
- PROT_EXEC |
+ mmap_prot |= PROT_EXEC;
#endif
- PROT_READ | PROT_WRITE,
- MAP_PRIVATE | MAP_ANON, mmap_fd, 0);
+#if __NetBSD__
+ mmap_prot = PROT_MPROTECT(mmap_prot);
+ mmap_flags = 0;
+#else
+ mmap_flags = MAP_PRIVATE;
+#endif
+ mmap_flags |= MAP_ANON;
+ ptr = mmap(NULL, 1024 * 1024, mmap_prot, mmap_flags, mmap_fd, 0);
assert(ptr != MAP_FAILED);
#if defined(__sgi)
close(mmap_fd);
@@ -75,6 +82,9 @@ main(int argc, char *argv[])
if (function != NULL)
abort();
+#if __NetBSD__
+ assert(mprotect(ptr, 1024 * 1024, PROT_READ | PROT_WRITE) == 0);
+#endif
/* and calling again with enough space works */
jit_set_code(ptr, 1024 * 1024);
function = jit_emit();
@@ -82,9 +92,7 @@ main(int argc, char *argv[])
abort();
jit_clear_state();
-#if __OpenBSD__
- /* Need to remove PROT_EXEC if reusing the memory, and
- * set again PROT_EXEC before executing jit code.*/
+#if __NetBSD__ || __OpenBSD__
assert(mprotect(ptr, 1024 * 1024, PROT_READ | PROT_EXEC) == 0);
#endif
(*function)();
diff --git a/lib/lightning.c b/lib/lightning.c
index 96a5fbe..7f6253a 100644
--- a/lib/lightning.c
+++ b/lib/lightning.c
@@ -2210,6 +2210,7 @@ _jit_emit(jit_state_t *_jit)
#if defined(__sgi)
int mmap_fd;
#endif
+ int mmap_prot, mmap_flags;
if (!_jitc->realize)
jit_realize();
@@ -2223,15 +2224,22 @@ _jit_emit(jit_state_t *_jit)
assert(_jit->user_code);
#else
if (!_jit->user_code) {
+ mmap_prot = PROT_READ | PROT_WRITE;
+#if !__OpenBSD__
+ mmap_prot |= PROT_EXEC;
+#endif
+#if __NetBSD__
+ mmap_prot = PROT_MPROTECT(mmap_prot);
+ mmap_flags = 0;
+#else
+ mmap_flags = MAP_PRIVATE;
+#endif
+ mmap_flags |= MAP_ANON;
#if defined(__sgi)
mmap_fd = open("/dev/zero", O_RDWR);
#endif
_jit->code.ptr = mmap(NULL, _jit->code.length,
-#if !__OpenBSD__
- PROT_EXEC |
-#endif
- PROT_READ | PROT_WRITE,
- MAP_PRIVATE | MAP_ANON, mmap_fd, 0);
+ mmap_prot, mmap_flags, mmap_fd, 0);
assert(_jit->code.ptr != MAP_FAILED);
}
#endif /* !HAVE_MMAP */
@@ -2240,6 +2248,11 @@ _jit_emit(jit_state_t *_jit)
_jit->pc.uc = _jit->code.ptr;
for (;;) {
+#if __NetBSD__
+ result = mprotect(_jit->code.ptr, _jit->code.length,
+ PROT_READ | PROT_WRITE);
+ assert(result == 0);
+#endif
if ((code = emit_code()) == NULL) {
_jitc->patches.offset = 0;
for (node = _jitc->head; node; node = node->next) {
@@ -2308,13 +2321,12 @@ _jit_emit(jit_state_t *_jit)
assert(result == 0);
}
if (!_jit->user_code) {
- result = mprotect(_jit->code.ptr,
- _jit->pc.uc - _jit->code.ptr
+ length = _jit->pc.uc - _jit->code.ptr;
# if __riscv && __WORDSIZE == 64
- - (_jitc->consts.hash.count * sizeof(jit_word_t))
+ /* FIXME should start adding consts at a page boundary */
+ length -= _jitc->consts.hash.count * sizeof(jit_word_t);
# endif
- ,
- PROT_READ | PROT_EXEC);
+ result = mprotect(_jit->code.ptr, length, PROT_READ | PROT_EXEC);
assert(result == 0);
}
#endif /* HAVE_MMAP */
generated by cgit v1.2.3 (git 2.25.1) at 2025年09月14日 00:52:56 +0000

AltStyle によって変換されたページ (->オリジナル) /