aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcomex2016-07-12 20:08:53 -0400
committercomex2016-07-12 20:08:53 -0400
commit7b262dfa85aab60479be332d9437405739ec915c (patch)
tree33f78066f9a31834f64a09c99ceccb94ac7eb05f
parentcomment (diff)
downloadsubstitute-7b262dfa85aab60479be332d9437405739ec915c.tar.gz
replace broken gen-manual-mach.sh with a Python version
it was dropping the #pragma pack(4) declarations, rendering it broken for like most of the last year
-rw-r--r--generated/manual-mach.inc.h245
-rw-r--r--lib/darwin/execmem.c2
-rw-r--r--script/gen-manual-mach.py52
-rwxr-xr-xscript/gen-manual-mach.sh26
4 files changed, 129 insertions, 196 deletions
diff --git a/generated/manual-mach.inc.h b/generated/manual-mach.inc.h
index 2d14eab..426af42 100644
--- a/generated/manual-mach.inc.h
+++ b/generated/manual-mach.inc.h
@@ -1,67 +1,3 @@
-
-/* Module thread_act */
-
-#include <string.h>
-#include <mach/ndr.h>
-#include <mach/boolean.h>
-#include <mach/kern_return.h>
-#include <mach/notify.h>
-#include <mach/mach_types.h>
-#include <mach/message.h>
-#include <mach/mig_errors.h>
-#include <mach/port.h>
-
-/* BEGIN VOUCHER CODE */
-
-#ifndef KERNEL
-#if defined(__has_include)
-#if __has_include(<mach/mig_voucher_support.h>)
-#define USING_VOUCHERS
-#ifndef __VOUCHER_FORWARD_TYPE_DECLS__
-#define __VOUCHER_FORWARD_TYPE_DECLS__
-#ifdef __cplusplus
-static "C" {
-#endif
-#ifdef __cplusplus
-}
-#endif
-#endif // __VOUCHER_FORWARD_TYPE_DECLS__
-#endif // __has_include(<mach/mach_voucher_types.h>)
-#endif // __has_include
-#endif // !KERNEL
-
-/* END VOUCHER CODE */
-
-
-#ifdef AUTOTEST
-#ifndef FUNCTION_PTR_T
-#define FUNCTION_PTR_T
-typedef void (*function_ptr_t)(mach_port_t, char *, mach_msg_type_number_t);
-typedef struct {
- char *name;
- function_ptr_t function;
-} function_table_entry;
-typedef function_table_entry *function_table_t;
-#endif /* FUNCTION_PTR_T */
-#endif /* AUTOTEST */
-
-#ifndef thread_act_MSG_COUNT
-#define thread_act_MSG_COUNT 28
-#endif /* thread_act_MSG_COUNT */
-
-#include <mach/std_types.h>
-#include <mach/mig.h>
-#include <mach/mig.h>
-#include <mach/mach_types.h>
-
-#ifdef __BeforeMigUserHeader
-__BeforeMigUserHeader
-#endif /* __BeforeMigUserHeader */
-
-#include <sys/cdefs.h>
-__BEGIN_DECLS
-
-
/* Routine thread_get_state */
static
kern_return_t manual_thread_get_state
@@ -71,7 +7,6 @@ kern_return_t manual_thread_get_state
thread_state_t old_state,
mach_msg_type_number_t *old_stateCnt
, mach_port_t reply_port);
-
/* Routine thread_set_state */
static
kern_return_t manual_thread_set_state
@@ -81,14 +16,21 @@ kern_return_t manual_thread_set_state
thread_state_t new_state,
mach_msg_type_number_t new_stateCnt
, mach_port_t reply_port);
-
+#ifdef __MigPackStructs
+#pragma pack(4)
+#endif
typedef struct {
mach_msg_header_t Head;
NDR_record_t NDR;
thread_state_flavor_t flavor;
mach_msg_type_number_t old_stateCnt;
} __Request__manual_thread_get_state_t __attribute__((unused));
-
+#ifdef __MigPackStructs
+#pragma pack()
+#endif
+#ifdef __MigPackStructs
+#pragma pack(4)
+#endif
typedef struct {
mach_msg_header_t Head;
NDR_record_t NDR;
@@ -96,7 +38,12 @@ kern_return_t manual_thread_set_state
mach_msg_type_number_t new_stateCnt;
natural_t new_state[224];
} __Request__manual_thread_set_state_t __attribute__((unused));
-
+#ifdef __MigPackStructs
+#pragma pack()
+#endif
+#ifdef __MigPackStructs
+#pragma pack(4)
+#endif
typedef struct {
mach_msg_header_t Head;
NDR_record_t NDR;
@@ -104,34 +51,20 @@ kern_return_t manual_thread_set_state
mach_msg_type_number_t old_stateCnt;
natural_t old_state[224];
} __Reply__manual_thread_get_state_t __attribute__((unused));
-
+#ifdef __MigPackStructs
+#pragma pack()
+#endif
+#ifdef __MigPackStructs
+#pragma pack(4)
+#endif
typedef struct {
mach_msg_header_t Head;
NDR_record_t NDR;
kern_return_t RetCode;
} __Reply__manual_thread_set_state_t __attribute__((unused));
-
-
-/*
- * IDENTIFICATION:
- * stub generated Tue Oct 27 21:33:54 2015
- * with a MiG generated by bootstrap_cmds-93
- * OPTIONS:
- */
-#define __MIG_check__Reply__manual_thread_act_subsystem__ 1
-
-
-
-#ifndef mig_internal
-#define mig_internal static __inline__
-#endif /* mig_internal */
-
-
-
-#if !defined(__MigKernelSpecificCode) && defined(_MIG_KERNEL_SPECIFIC_CODE_)
-#define __MigKernelSpecificCode _MIG_KERNEL_SPECIFIC_CODE_ /* Legacy setting */
-#endif /* !defined(__MigKernelSpecificCode) */
-
+#ifdef __MigPackStructs
+#pragma pack()
+#endif
#ifndef LimitCheck
#define LimitCheck 0
#endif /* LimitCheck */
@@ -148,9 +81,7 @@ kern_return_t manual_thread_set_state
#define _WALIGNSZ_(x) _WALIGN_(sizeof(x))
#endif /* !defined(_WALIGNSZ_) */
-#ifndef UseStaticTemplates
#define UseStaticTemplates 0
-#endif /* UseStaticTemplates */
#ifndef __MachMsgErrorWithTimeout
#define __MachMsgErrorWithTimeout(_R_) { \
@@ -211,10 +142,10 @@ kern_return_t manual_thread_set_state
-#if !defined(__MIG_check__Reply__manual_thread_get_state_t__defined)
-#define __MIG_check__Reply__manual_thread_get_state_t__defined
-mig_internal kern_return_t __MIG_check__Reply__manual_thread_get_state_t(__Reply__manual_thread_get_state_t *Out0P)
+
+
+static kern_return_t __MIG_check__Reply__manual_thread_get_state_t(__Reply__manual_thread_get_state_t *Out0P)
{
typedef __Reply__manual_thread_get_state_t __Reply __attribute__((unused));
@@ -247,7 +178,11 @@ mig_internal kern_return_t __MIG_check__Reply__manual_thread_get_state_t(__Reply
return MACH_MSG_SUCCESS;
}
-#endif /* !defined(__MIG_check__Reply__manual_thread_get_state_t__defined) */
+
+
+
+
+
/* Routine thread_get_state */
static kern_return_t manual_thread_get_state
(
@@ -317,9 +252,9 @@ static kern_return_t manual_thread_get_state
mach_msg_return_t msg_result;
-#ifdef __MIG_check__Reply__manual_thread_get_state_t__defined
+
kern_return_t check_result;
-#endif /* __MIG_check__Reply__manual_thread_get_state_t__defined */
+
__DeclareSendRpc(3603, "thread_get_state")
@@ -338,11 +273,9 @@ static kern_return_t manual_thread_get_state
InP->Head.msgh_request_port = target_act;
InP->Head.msgh_reply_port = reply_port;
InP->Head.msgh_id = 3603;
+ InP->Head.msgh_reserved = 0;
-/* BEGIN VOUCHER CODE */
-
-/* END VOUCHER CODE */
__BeforeSendRpc(3603, "thread_get_state")
msg_result = manual_mach_msg(&InP->Head, MACH_SEND_MSG|MACH_RCV_MSG|MACH_MSG_OPTION_NONE, (mach_msg_size_t)sizeof(Request), (mach_msg_size_t)sizeof(Reply), InP->Head.msgh_reply_port, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
@@ -353,11 +286,11 @@ static kern_return_t manual_thread_get_state
}
-#if defined(__MIG_check__Reply__manual_thread_get_state_t__defined)
+
check_result = __MIG_check__Reply__manual_thread_get_state_t((__Reply__manual_thread_get_state_t *)Out0P);
if (check_result != MACH_MSG_SUCCESS)
{ return check_result; }
-#endif /* defined(__MIG_check__Reply__manual_thread_get_state_t__defined) */
+
if (Out0P->old_stateCnt > *old_stateCnt) {
(void)manual_memcpy((char *) old_state, (const char *) Out0P->old_state, 4 * *old_stateCnt);
@@ -371,10 +304,12 @@ static kern_return_t manual_thread_get_state
return KERN_SUCCESS;
}
-#if !defined(__MIG_check__Reply__manual_thread_set_state_t__defined)
-#define __MIG_check__Reply__manual_thread_set_state_t__defined
-mig_internal kern_return_t __MIG_check__Reply__manual_thread_set_state_t(__Reply__manual_thread_set_state_t *Out0P)
+
+
+
+
+static kern_return_t __MIG_check__Reply__manual_thread_set_state_t(__Reply__manual_thread_set_state_t *Out0P)
{
typedef __Reply__manual_thread_set_state_t __Reply __attribute__((unused));
@@ -393,7 +328,11 @@ mig_internal kern_return_t __MIG_check__Reply__manual_thread_set_state_t(__Reply
return Out0P->RetCode;
}
}
-#endif /* !defined(__MIG_check__Reply__manual_thread_set_state_t__defined) */
+
+
+
+
+
/* Routine thread_set_state */
static kern_return_t manual_thread_set_state
(
@@ -461,9 +400,9 @@ static kern_return_t manual_thread_set_state
mach_msg_return_t msg_result;
unsigned int msgh_size;
-#ifdef __MIG_check__Reply__manual_thread_set_state_t__defined
+
kern_return_t check_result;
-#endif /* __MIG_check__Reply__manual_thread_set_state_t__defined */
+
__DeclareSendRpc(3604, "thread_set_state")
@@ -485,11 +424,9 @@ static kern_return_t manual_thread_set_state
InP->Head.msgh_request_port = target_act;
InP->Head.msgh_reply_port = reply_port;
InP->Head.msgh_id = 3604;
+ InP->Head.msgh_reserved = 0;
-/* BEGIN VOUCHER CODE */
-
-/* END VOUCHER CODE */
__BeforeSendRpc(3604, "thread_set_state")
msg_result = manual_mach_msg(&InP->Head, MACH_SEND_MSG|MACH_RCV_MSG|MACH_MSG_OPTION_NONE, msgh_size, (mach_msg_size_t)sizeof(Reply), InP->Head.msgh_reply_port, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
@@ -500,15 +437,14 @@ static kern_return_t manual_thread_set_state
}
-#if defined(__MIG_check__Reply__manual_thread_set_state_t__defined)
+
check_result = __MIG_check__Reply__manual_thread_set_state_t((__Reply__manual_thread_set_state_t *)Out0P);
if (check_result != MACH_MSG_SUCCESS)
{ return check_result; }
-#endif /* defined(__MIG_check__Reply__manual_thread_set_state_t__defined) */
+
return KERN_SUCCESS;
}
-
/* Routine mach_vm_remap */
static
kern_return_t manual_mach_vm_remap
@@ -525,7 +461,9 @@ kern_return_t manual_mach_vm_remap
vm_prot_t *max_protection,
vm_inherit_t inheritance
, mach_port_t reply_port);
-
+#ifdef __MigPackStructs
+#pragma pack(4)
+#endif
typedef struct {
mach_msg_header_t Head;
/* start of the kernel processed data */
@@ -541,7 +479,12 @@ kern_return_t manual_mach_vm_remap
boolean_t copy;
vm_inherit_t inheritance;
} __Request__manual_mach_vm_remap_t __attribute__((unused));
-
+#ifdef __MigPackStructs
+#pragma pack()
+#endif
+#ifdef __MigPackStructs
+#pragma pack(4)
+#endif
typedef struct {
mach_msg_header_t Head;
NDR_record_t NDR;
@@ -550,28 +493,9 @@ kern_return_t manual_mach_vm_remap
vm_prot_t cur_protection;
vm_prot_t max_protection;
} __Reply__manual_mach_vm_remap_t __attribute__((unused));
-
-
-/*
- * IDENTIFICATION:
- * stub generated Tue Oct 27 21:33:54 2015
- * with a MiG generated by bootstrap_cmds-93
- * OPTIONS:
- */
-#define __MIG_check__Reply__manual_mach_vm_subsystem__ 1
-
-
-
-#ifndef mig_internal
-#define mig_internal static __inline__
-#endif /* mig_internal */
-
-
-
-#if !defined(__MigKernelSpecificCode) && defined(_MIG_KERNEL_SPECIFIC_CODE_)
-#define __MigKernelSpecificCode _MIG_KERNEL_SPECIFIC_CODE_ /* Legacy setting */
-#endif /* !defined(__MigKernelSpecificCode) */
-
+#ifdef __MigPackStructs
+#pragma pack()
+#endif
#ifndef LimitCheck
#define LimitCheck 0
#endif /* LimitCheck */
@@ -588,9 +512,7 @@ kern_return_t manual_mach_vm_remap
#define _WALIGNSZ_(x) _WALIGN_(sizeof(x))
#endif /* !defined(_WALIGNSZ_) */
-#ifndef UseStaticTemplates
#define UseStaticTemplates 0
-#endif /* UseStaticTemplates */
#ifndef __MachMsgErrorWithTimeout
#define __MachMsgErrorWithTimeout(_R_) { \
@@ -651,10 +573,10 @@ kern_return_t manual_mach_vm_remap
-#if !defined(__MIG_check__Reply__manual_mach_vm_remap_t__defined)
-#define __MIG_check__Reply__manual_mach_vm_remap_t__defined
-mig_internal kern_return_t __MIG_check__Reply__manual_mach_vm_remap_t(__Reply__manual_mach_vm_remap_t *Out0P)
+
+
+static kern_return_t __MIG_check__Reply__manual_mach_vm_remap_t(__Reply__manual_mach_vm_remap_t *Out0P)
{
typedef __Reply__manual_mach_vm_remap_t __Reply __attribute__((unused));
@@ -680,7 +602,11 @@ mig_internal kern_return_t __MIG_check__Reply__manual_mach_vm_remap_t(__Reply__m
return MACH_MSG_SUCCESS;
}
-#endif /* !defined(__MIG_check__Reply__manual_mach_vm_remap_t__defined) */
+
+
+
+
+
/* Routine mach_vm_remap */
static kern_return_t manual_mach_vm_remap
(
@@ -768,31 +694,17 @@ static kern_return_t manual_mach_vm_remap
mach_msg_return_t msg_result;
-#ifdef __MIG_check__Reply__manual_mach_vm_remap_t__defined
+
kern_return_t check_result;
-#endif /* __MIG_check__Reply__manual_mach_vm_remap_t__defined */
+
__DeclareSendRpc(4813, "mach_vm_remap")
-#if UseStaticTemplates
- const static mach_msg_port_descriptor_t src_taskTemplate = {
- /* name = */ MACH_PORT_NULL,
- /* pad1 = */ 0,
- /* pad2 = */ 0,
- /* disp = */ 19,
- /* type = */ MACH_MSG_PORT_DESCRIPTOR,
- };
-#endif /* UseStaticTemplates */
InP->msgh_body.msgh_descriptor_count = 1;
-#if UseStaticTemplates
- InP->src_task = src_taskTemplate;
- InP->src_task.name = src_task;
-#else /* UseStaticTemplates */
InP->src_task.name = src_task;
InP->src_task.disposition = 19;
InP->src_task.type = MACH_MSG_PORT_DESCRIPTOR;
-#endif /* UseStaticTemplates */
InP->NDR = NDR_record;
@@ -816,11 +728,9 @@ static kern_return_t manual_mach_vm_remap
InP->Head.msgh_request_port = target_task;
InP->Head.msgh_reply_port = reply_port;
InP->Head.msgh_id = 4813;
+ InP->Head.msgh_reserved = 0;
-/* BEGIN VOUCHER CODE */
-
-/* END VOUCHER CODE */
__BeforeSendRpc(4813, "mach_vm_remap")
msg_result = manual_mach_msg(&InP->Head, MACH_SEND_MSG|MACH_RCV_MSG|MACH_MSG_OPTION_NONE, (mach_msg_size_t)sizeof(Request), (mach_msg_size_t)sizeof(Reply), InP->Head.msgh_reply_port, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
@@ -831,11 +741,11 @@ static kern_return_t manual_mach_vm_remap
}
-#if defined(__MIG_check__Reply__manual_mach_vm_remap_t__defined)
+
check_result = __MIG_check__Reply__manual_mach_vm_remap_t((__Reply__manual_mach_vm_remap_t *)Out0P);
if (check_result != MACH_MSG_SUCCESS)
{ return check_result; }
-#endif /* defined(__MIG_check__Reply__manual_mach_vm_remap_t__defined) */
+
*target_address = Out0P->target_address;
@@ -845,4 +755,3 @@ static kern_return_t manual_mach_vm_remap
return KERN_SUCCESS;
}
-
diff --git a/lib/darwin/execmem.c b/lib/darwin/execmem.c
index d2bd4ab..c7068f3 100644
--- a/lib/darwin/execmem.c
+++ b/lib/darwin/execmem.c
@@ -46,8 +46,6 @@ static void manual_memcpy(void *restrict dest, const void *src, size_t len) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
-#define __MachMsgErrorWithTimeout(_R_)
-#define __MachMsgErrorWithoutTimeout(_R_)
#include "../generated/manual-mach.inc.h"
#pragma GCC diagnostic pop
diff --git a/script/gen-manual-mach.py b/script/gen-manual-mach.py
new file mode 100644
index 0000000..00317e9
--- /dev/null
+++ b/script/gen-manual-mach.py
@@ -0,0 +1,52 @@
+import subprocess, re
+# i wish there was a good simple tooling/ast library that could do this more cleanly
+# maybe libTooling, but instability and dependencies...
+
+desired = ['thread_get_state', 'thread_set_state', 'mach_vm_remap']
+
+def pipe(cmd, stdin='', expected_returncode=0):
+ p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True)
+ stdout, _ = p.communicate(stdin)
+ if p.returncode != expected_returncode:
+ raise Exception('process failed: %s => %d (not %d)' % (cmd, p.returncode, expected_returncode))
+ return stdout
+text = pipe('mig -user /dev/stdout -server /dev/null -header /dev/stdout /usr/include/mach/{thread_act,mach_vm}.defs')
+text = pipe('unifdef -D__MigTypeCheck -Umig_external -UUseStaticTemplates', text, expected_returncode=1)
+# remove voucher/etc code
+text = re.sub('/\* BEGIN (VOUCHER|MIG_STRNCPY_ZEROFILL) CODE \*/.*?/\* END \\1 CODE \*/',
+ '', text, flags=re.S)
+# and union
+text = re.sub('\/\* union of all requests.*?#endif \/\* !__RequestUnion[^\n]*',
+ '', text, flags=re.S)
+# simplify function names
+text = text.replace('_kernelrpc_', '')
+# extern->static
+text = re.sub('(extern|mig_internal|mig_external)', 'static', text)
+# add extra argument reply_port
+text = re.sub('^\)', ', mach_port_t reply_port)', text, flags=re.M)
+text = text.replace('mig_get_reply_port()', 'reply_port')
+# don't use stdlib functions
+text = re.sub(r'\b(mach_msg|memcpy)\b', r'manual_\1', text)
+# change symbols to avoid collision: types
+text = re.sub('(Request|Reply)__', r'\1__manual_', text)
+# and functions
+text = re.sub('^([a-z].+|)kern_return_t\s+([a-z])', r'\1kern_return_t manual_\2', text, flags=re.M)
+
+text = re.sub('^#(if|ifdef|define|endif)\s.*_check__Reply.*', '', text, flags=re.M)
+
+# filter out unused parts
+patterns = '(^/\* Routine [^ ]* \*/.*?(?:^}|reply_port\);)|' \
+ '#if __MIG_check__Reply.*?#endif /\* __MIG_check__Reply[^\n]*|' \
+ '#ifdef\s+__MigPackStructs.*?typedef struct {.*?(?:Request|Reply)__.*?#endif|' \
+ '#ifndef\s+LimitCheck.*?msgh_local_port)'
+bits = re.split(patterns, text, flags=re.M | re.S)
+out = ''
+for bit in bits:
+ #print bit; print ';;;'
+ if 'subsystem_to_name_map' in bit:
+ continue
+ if 'LimitCheck' in bit or any(d in bit for d in desired):
+ out += bit + '\n'
+#print out;die
+
+open('generated/manual-mach.inc.h', 'w').write(out)
diff --git a/script/gen-manual-mach.sh b/script/gen-manual-mach.sh
deleted file mode 100755
index 2676915..0000000
--- a/script/gen-manual-mach.sh
+++ /dev/null
@@ -1,26 +0,0 @@
-#!/bin/bash
-out=generated/manual-mach.inc.h
-pat='/thread_[gs]et_state|vm_remap/'
-(mig -user /dev/stdout -server /dev/null -header /dev/stdout /usr/include/mach/{thread_act,mach_vm}.defs |
- egrep -v '^(#ifndef|#define|#endif).*_user_' |
- egrep -v '#include "stdout"' |
- unifdef -D__MigTypeCheck -Umig_external -UUSING_VOUCHERS |
- egrep -v 'voucher_mach_msg_set' |
- egrep -v '#define mig_external' |
- sed -E 's/(mach_msg|memcpy)\(/manual_\1(/g;
- s/^\)/, mach_port_t reply_port)/;
- s/_kernelrpc_//g;
- s/(Request|Reply)__/\1__manual_/g;
- s/[[:<:]]extern[[:>:]]/static/g;
- s/^([a-z].*)?kern_return_t[[:blank:]]+([a-z])/\1kern_return_t manual_\2/;
- s/mig_external/static/g;
- s/mig_get_reply_port\(\)/reply_port/g' |
- awk 'BEGIN { on = 1; }
- /^\/\* Routine / ||
- (/__MIG_check__Reply__/ && /^#[ie]/) { on = '"$pat"'; }
- on { print; }
- /typedef struct {/ { xon = 1; accum = ""; }
- xon { accum = accum $0 "\n"; }
- xon && /} / { if('"$pat"') print accum; xon = 0; }
- /#endif.*__AfterMigUserHeader/ { on = 1; }
- ' > $out) || rm -f $out