Skip to content

Commit

Permalink
compiling checked enc2 interface. add stdarg user abort handler.
Browse files Browse the repository at this point in the history
Change-Id: I194cf544ded78dad2c3d027a2012793f6a3e1cc9
(cherry picked from commit 40df9f6e49ba7291f1eec8297e294156c214fdfa)
  • Loading branch information
mjcharne authored and markcharney committed Aug 9, 2019
1 parent 7878d0e commit e8b5768
Show file tree
Hide file tree
Showing 10 changed files with 269 additions and 43 deletions.
117 changes: 117 additions & 0 deletions examples/xed-enc2-2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*BEGIN_LEGAL
Copyright (c) 2019 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
END_LEGAL */

#include "xed/xed-interface.h"
#include "xed/xed-chk-enc2-m64-a64.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

static xed_uint32_t test_0_xed_enc_lea_rm_q_bisd32_a64(xed_uint8_t* output_buffer)
{
xed_enc2_req_t* r;
xed_reg_enum_t reg0;
xed_reg_enum_t base;
xed_reg_enum_t index;
xed_uint_t scale;
xed_int32_t disp32;
xed_enc2_req_t request;
xed_enc2_req_t_init(&request, output_buffer);
r = &request;
reg0 = XED_REG_XMM5; // INTENTIONAL ERROR FOR EXAMPLE. THIS SHOULD BE A GPR LIKE R11
base = XED_REG_R12;
index = XED_REG_R13;
scale = 1;
disp32 = 0x11223344;
xed_enc_lea_rm_q_bisd32_a64_chk(r /*req*/,reg0 /*gpr64*/,base /*gpr64*/,index /*gpr64*/,scale /*scale*/,disp32 /*int32*/);
return xed_enc2_encoded_length(r);
}

// The decoder drags in a lot of stuff to the final executable.
//#define DECO

#if defined(DECO)
static void disassemble(xed_decoded_inst_t* xedd)
{
xed_bool_t ok;
xed_print_info_t pi;
#define XBUFLEN 200
char buf[XBUFLEN];

xed_init_print_info(&pi);
pi.p = xedd;
pi.blen = XBUFLEN;
pi.buf = buf;
pi.buf[0]=0; //allow use of strcat

ok = xed_format_generic(&pi);
if (ok)
printf("Disassembly: %s\n", buf);
else
printf("Disassembly: %%ERROR%%\n");
}
#endif

static void dump(xed_uint8_t* buf, xed_uint32_t len) {
xed_uint_t i;
for(i=0;i<len;i++)
printf("%02x ",buf[i]);
}

void my_error_handler(const char * restrict fmt, va_list args) {
printf("LOCAL HANDLER FOR XED ENC2 ERROR: ");
vprintf(fmt, args);
printf(".\n");
exit(1);
}


int main(int argc, char** argv) {
#if defined(DECO)
xed_decoded_inst_t xedd;
xed_error_enum_t err;
xed_state_t dstate;
#endif
xed_uint8_t output_buffer[XED_MAX_INSTRUCTION_BYTES];
xed_uint32_t enclen;

#if defined(DECO)
xed_tables_init();
xed_state_zero(&dstate);
dstate.mmode=XED_MACHINE_MODE_LONG_64;
#endif
xed_enc2_set_error_handler(my_error_handler);

enclen = test_0_xed_enc_lea_rm_q_bisd32_a64(output_buffer);

printf("Encoded: ");
dump(output_buffer, enclen);
printf("\n");

#if defined(DECO)
xed_decoded_inst_zero_set_mode(&xedd, &dstate);
err = xed_decode(&xedd, output_buffer, enclen);
if (err == XED_ERROR_NONE) {
disassemble(&xedd);
return 0;
}
printf("ERROR: %s\n", xed_error_enum_t2str(err));
return 1;
#endif
(void)argc; (void)argv;
}
5 changes: 3 additions & 2 deletions examples/xed_examples_mbuild.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ def init(env):
if nchk(env,'xed_lib_dir'):
env['xed_lib_dir'] = '../lib'
if nchk(env,'xed_enc2_libs'):
env['xed_enc2_libs'] = mbuild.glob(env['xed_lib_dir'],'*xed-enc2-*')
env['xed_enc2_libs'] = mbuild.glob(env['xed_lib_dir'],'*xed-*enc2-*')
if nchk(env,'xed_inc_dir'):
env['xed_inc_dir'] = ['../include']
if nchk(env,'xed_dir'):
Expand Down Expand Up @@ -385,7 +385,8 @@ def build_examples(env, work_queue):
enc2_examples = []
small_examples = ['xed-size.c']
if env['enc2']:
enc2_examples += [ 'xed-enc2-1.c' ]
enc2_examples += [ 'xed-enc2-1.c',
'xed-enc2-2.c' ]
if env['encoder']:
small_examples += ['xed-ex5-enc.c']
other_c_examples += ['xed-ex3.c']
Expand Down
2 changes: 1 addition & 1 deletion include/private/xed-enc2-check.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ END_LEGAL */
#include "xed-types.h"
#include "xed-reg-enum.h"

extern xed_bool_t enc2_check_args_set;
extern xed_bool_t xed_enc2_check_args;

void xed_enc2_invalid_cr(xed_uint_t mode, xed_reg_enum_t reg,const char* argname,const char* pfn);
void xed_enc2_invalid_dr(xed_uint_t mode, xed_reg_enum_t reg,const char* argname,const char* pfn);
Expand Down
40 changes: 40 additions & 0 deletions include/public/xed/xed-encode-check.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*BEGIN_LEGAL
Copyright (c) 2019 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
END_LEGAL */


#ifndef XED_ENCODE_CHECK_H
# define XED_ENCODE_CHECK_H
#include "xed-common-hdrs.h"
#include "xed-types.h"
#include <stdarg.h>


/// turn off (or on) argument checking if using the checked encoder interface.
/// values 1, 0
XED_DLL_EXPORT void xed_enc2_check_args_set(xed_bool_t on);

typedef void (xed_user_abort_handler_t)(const char * restrict format, va_list args);

/// Set a function taking a variable-number-of-arguments (stdarg) to handle
/// the errors and die. The argument are like printf with a format string
/// followed by a varaible number of arguments.

XED_DLL_EXPORT void xed_enc2_set_error_handler(xed_user_abort_handler_t* fn);


#endif
6 changes: 0 additions & 6 deletions include/public/xed/xed-encode-direct.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,4 @@ static XED_INLINE xed_uint32_t xed_enc2_encoded_length(xed_enc2_req_t* r) {
return r->s.cursor;
}


/// turn off (or on) argument checking if using the checked encoder interface.
/// values 1, 0
XED_DLL_EXPORT void xed_enc2_check_args_set(xed_bool_t on);


#endif
1 change: 1 addition & 0 deletions include/public/xed/xed-interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ END_LEGAL */
# include "xed-encoder-hl.h"
# include "xed-patch.h"
# include "xed-encode-direct.h"
# include "xed-encode-check.h"
#endif

#include "xed-util.h"
Expand Down
33 changes: 24 additions & 9 deletions pysrc/enc2argcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ def _create_enc_arg_check_function(env, ii, encfn):

for argtype,argname,arginfo in args:
chk_fn.add_arg('{} {}'.format(argtype, argname), arginfo)
chk_fn.add_code('if (xed_enc2_check_args) {')
chk_fn.add_code_eol(' const char* pfn = "{}"'.format( chk_fn.get_function_name() ))


args_to_check = False
for argtype,argname,arginfo in args:
if not arginfo:
die("NO ARGINFO FOR {} {} in {}".format(argtype, argname, ii.iclass))
Expand All @@ -73,13 +73,26 @@ def _create_enc_arg_check_function(env, ii, encfn):
elif 'imm' in arginfo:
continue # don't check the integer arguments
else:
chk_fn.add_code_eol(' xed_enc2_invalid_{}({}, {},"{}",pfn)'.format(
_fixup_arg_type(arginfo),
env.mode,
argname,
argname))
args_to_check = True
break

chk_fn.add_code('}') # end of "if (xed_enc2_check_args) ..." clause
if args_to_check:
chk_fn.add_code('if (xed_enc2_check_args) {')
chk_fn.add_code_eol(' const char* pfn = "{}"'.format( chk_fn.get_function_name() ))
for argtype,argname,arginfo in args:
if arginfo == 'req':
continue # don't check the request structure
elif 'int' in arginfo:
continue # don't check the integer arguments
elif 'imm' in arginfo:
continue # don't check the integer arguments
else:
chk_fn.add_code_eol(' xed_enc2_invalid_{}({}, {},"{}",pfn)'.format(
_fixup_arg_type(arginfo),
env.mode,
argname,
argname))
chk_fn.add_code('}') # end of "if (xed_enc2_check_args) ..." clause

# call encoder function call
s = []
Expand All @@ -92,6 +105,8 @@ def _create_enc_arg_check_function(env, ii, encfn):
s.append( ')' )
chk_fn.add_code_eol(''.join(s))



_add_arg_check_function(ii, chk_fn)


Expand Down
19 changes: 12 additions & 7 deletions pysrc/enc2gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -4720,7 +4720,9 @@ def emit_encode_functions(args,
xeddb,
function_type_name='encode',
fn_list_attr='encoder_functions',
config_prefix='enc2'):
config_prefix='',
srcdir='src',
extra_headers=[]):
msge("Writing encoder '{}' functions to .c and .h files".format(function_type_name))
# group the instructions by encoding space to allow for
# better link-time garbage collection.
Expand All @@ -4731,10 +4733,10 @@ def emit_encode_functions(args,
for space in func_lists.keys():
func_list.extend(func_lists[space])

config_descriptor = '{}-m{}-a{}'.format(config_prefix, env.mode, env.asz)
fn_prefix = 'xed-{}'.format(config_descriptor)
config_descriptor = 'enc2-m{}-a{}'.format(env.mode, env.asz)
fn_prefix = 'xed-{}{}'.format(config_prefix,config_descriptor)

gen_src_dir = os.path.join(args.gendir, config_descriptor, 'src')
gen_src_dir = os.path.join(args.gendir, config_descriptor, srcdir)
gen_hdr_dir = os.path.join(args.gendir, config_descriptor, 'hdr', 'xed')
mbuild.cmkdir(gen_src_dir)
mbuild.cmkdir(gen_hdr_dir)
Expand All @@ -4744,7 +4746,7 @@ def emit_encode_functions(args,
args.xeddir,
gen_src_dir,
gen_hdr_dir,
#other_headers = extra_headers,
other_headers = extra_headers,
max_lines_per_file=15000,
is_private_header=False,
extra_public_headers=['xed/xed-interface.h'])
Expand Down Expand Up @@ -4868,15 +4870,18 @@ def prune_asz_list_for_mode(mode,alist):
xeddb,
function_type_name='encode',
fn_list_attr='encoder_functions',
config_prefix='enc2')
config_prefix='',
srcdir='src')
output_file_emitters.extend(fel)

fel = emit_encode_functions(args,
env,
xeddb,
function_type_name='encoder-check',
fn_list_attr='enc_arg_check_functions',
config_prefix='enc2-chk')
config_prefix='chk-',
srcdir='src-chk',
extra_headers = [ 'xed/xed-enc2-m{}-a{}.h'.format(env.mode, env.asz) ])
output_file_emitters.extend(fel)


Expand Down
27 changes: 22 additions & 5 deletions src/enc2/xed-enc2-check.c → src/enc2chk/xed-enc2-check.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,34 @@ Copyright (c) 2019 Intel Corporation
END_LEGAL */
#include "xed-types.h"
#include "xed-reg-enum.h"
#include "xed/xed-encode-check.h"
#include <stdarg.h> //varargs va_list etc.
#include <stdlib.h> //abort()
#include <stdio.h> //vfprintf()

/// Check functions

static xed_user_abort_handler_t* user_abort_handler = 0;

void xed_enc2_set_error_handler(xed_user_abort_handler_t* fn) {
user_abort_handler = fn;
}

static void xed_enc2_error(const char* fmt, ...) {
va_list args;
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);

if (user_abort_handler) {
va_start(args, fmt);
(*user_abort_handler)(fmt, args);
va_end(args);
}
else {
printf("XED ENC2 ERROR: ");
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
printf(".\n");
}
abort();
}

Expand Down Expand Up @@ -132,7 +149,7 @@ void xed_enc2_invalid_zmm(xed_uint_t mode, xed_reg_enum_t reg,const char* argnam
xed_enc2_error("Bad zmm %s arg_name %s in function %s", xed_reg_enum_t2str(reg), argname, pfn);
}

xed_bool_t enc2_check_args_set = 1;
xed_bool_t xed_enc2_check_args = 1;
void xed_enc2_check_args_set(xed_bool_t on) {
enc2_check_args_set = on;
xed_enc2_check_args = on;
}
Loading

0 comments on commit e8b5768

Please sign in to comment.