Mercurial > repos > tabletprog
comparison modules/bytearray.tp @ 178:e823e104b845
Add initial work on bytearrays with support for allocating executable memory
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Sat, 24 Aug 2013 09:55:54 -0700 |
parents | |
children | 270d31c6c4cd |
comparison
equal
deleted
inserted
replaced
177:76e3d4ae1746 | 178:e823e104b845 |
---|---|
1 #{ | |
2 includeSystemHeader: "unistd.h" | |
3 includeSystemHeader: "sys/mman.h" | |
4 | |
5 executable <- :size { | |
6 buf <- #{ | |
7 llProperty: bytes withType: uint32_t | |
8 llProperty: buffer withType: (void ptr) | |
9 llMessage: _init withVars: { | |
10 sz <- obj_int32 ptr | |
11 } andCode: :sz { | |
12 bytes <- sz num | |
13 buffer <- sbrk: bytes | |
14 mprotect: buffer bytes (PROT_READ or PROT_WRITE or PROT_EXEC) | |
15 self | |
16 } | |
17 llMessage: set withVars: { | |
18 offset <- obj_int32 ptr | |
19 newval <- obj_uint8 ptr | |
20 } andCode: :offset newval { | |
21 (buffer castTo: (uint8_t ptr)) set: (offset num) (newval num) | |
22 self | |
23 } | |
24 llMessage: get withVars: { | |
25 offset <- obj_int32 ptr | |
26 ret <- obj_uint8 ptr | |
27 } andCode: :offset { | |
28 ret <- make_object: (addr_of: obj_uint8_meta) NULL 0 | |
29 ret num!: ((buffer castTo: (uint8_t ptr)) get: (offset num)) | |
30 ret | |
31 } | |
32 llMessage: run withVars: { | |
33 fun <- uint64_t funptr | |
34 funret <- obj_uint64 ptr | |
35 } andCode: { | |
36 funret <- make_object: (addr_of: obj_uint64_meta) NULL 0 | |
37 fun <- buffer | |
38 funret num!: ( fun: ) | |
39 funret | |
40 } | |
41 | |
42 llMessage: runWithArg withVars: { | |
43 fun <- uint64_t funptr: uint64_t | |
44 funret <- obj_uint64 ptr | |
45 arg <- obj_uint64 ptr | |
46 } andCode: :arg { | |
47 fun <- buffer | |
48 funret <- make_object: (addr_of: obj_uint64_meta) NULL 0 | |
49 funret num!: ( fun: (arg num) ) | |
50 funret | |
51 } | |
52 } | |
53 buf _init | |
54 } | |
55 | |
56 executableFromBytes <- :bytes { | |
57 totalSize <- bytes fold: 0 with: :acc el { | |
58 acc + (el length) | |
59 } | |
60 ba <- executable: totalSize | |
61 bytes fold: 0 with: :idx el { | |
62 el fold: idx with: :idx byte { | |
63 ba set: idx byte | |
64 idx + 1 | |
65 } | |
66 } | |
67 ba | |
68 } | |
69 } |